Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dt9812.c
Go to the documentation of this file.
1 /*
2  * comedi/drivers/dt9812.c
3  * COMEDI driver for DataTranslation DT9812 USB module
4  *
5  * Copyright (C) 2005 Anders Blomdell <[email protected]>
6  *
7  * COMEDI - Linux Control and Measurement Device Interface
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24 
25 /*
26 Driver: dt9812
27 Description: Data Translation DT9812 USB module
28 Author: [email protected] (Anders Blomdell)
29 Status: in development
30 Devices: [Data Translation] DT9812 (dt9812)
31 Updated: Sun Nov 20 20:18:34 EST 2005
32 
33 This driver works, but bulk transfers not implemented. Might be a starting point
34 for someone else. I found out too late that USB has too high latencies (>1 ms)
35 for my needs.
36 */
37 
38 /*
39  * Nota Bene:
40  * 1. All writes to command pipe has to be 32 bytes (ISP1181B SHRTP=0 ?)
41  * 2. The DDK source (as of sep 2005) is in error regarding the
42  * input MUX bits (example code says P4, but firmware schematics
43  * says P1).
44  */
45 
46 #include <linux/kernel.h>
47 #include <linux/errno.h>
48 #include <linux/init.h>
49 #include <linux/slab.h>
50 #include <linux/module.h>
51 #include <linux/kref.h>
52 #include <linux/uaccess.h>
53 #include <linux/usb.h>
54 
55 #include "../comedidev.h"
56 
57 #define DT9812_DIAGS_BOARD_INFO_ADDR 0xFBFF
58 #define DT9812_MAX_WRITE_CMD_PIPE_SIZE 32
59 #define DT9812_MAX_READ_CMD_PIPE_SIZE 32
60 
61 /*
62  * See Silican Laboratories C8051F020/1/2/3 manual
63  */
64 #define F020_SFR_P4 0x84
65 #define F020_SFR_P1 0x90
66 #define F020_SFR_P2 0xa0
67 #define F020_SFR_P3 0xb0
68 #define F020_SFR_AMX0CF 0xba
69 #define F020_SFR_AMX0SL 0xbb
70 #define F020_SFR_ADC0CF 0xbc
71 #define F020_SFR_ADC0L 0xbe
72 #define F020_SFR_ADC0H 0xbf
73 #define F020_SFR_DAC0L 0xd2
74 #define F020_SFR_DAC0H 0xd3
75 #define F020_SFR_DAC0CN 0xd4
76 #define F020_SFR_DAC1L 0xd5
77 #define F020_SFR_DAC1H 0xd6
78 #define F020_SFR_DAC1CN 0xd7
79 #define F020_SFR_ADC0CN 0xe8
80 
81 #define F020_MASK_ADC0CF_AMP0GN0 0x01
82 #define F020_MASK_ADC0CF_AMP0GN1 0x02
83 #define F020_MASK_ADC0CF_AMP0GN2 0x04
84 
85 #define F020_MASK_ADC0CN_AD0EN 0x80
86 #define F020_MASK_ADC0CN_AD0INT 0x20
87 #define F020_MASK_ADC0CN_AD0BUSY 0x10
88 
89 #define F020_MASK_DACxCN_DACxEN 0x80
90 
91 enum {
92  /* A/D D/A DI DO CT */
93  DT9812_DEVID_DT9812_10, /* 8 2 8 8 1 +/- 10V */
94  DT9812_DEVID_DT9812_2PT5, /* 8 2 8 8 1 0-2.44V */
95 #if 0
96  DT9812_DEVID_DT9813, /* 16 2 4 4 1 +/- 10V */
97  DT9812_DEVID_DT9814 /* 24 2 0 0 1 +/- 10V */
98 #endif
99 };
100 
109 };
110 
111 enum {
113  /* Write Flash memory */
115  /* Read Flash memory misc config info */
117 
118  /*
119  * Register read/write commands for processor
120  */
121 
122  /* Read a single byte of USB memory */
124  /* Write a single byte of USB memory */
126  /* Multiple Reads of USB memory */
128  /* Multiple Writes of USB memory */
130  /* Read, (AND) with mask, OR value, then write (single) */
132  /* Read, (AND) with mask, OR value, then write (multiple) */
134 
135  /*
136  * Register read/write commands for SMBus
137  */
138 
139  /* Read a single byte of SMBus */
141  /* Write a single byte of SMBus */
143  /* Multiple Reads of SMBus */
145  /* Multiple Writes of SMBus */
147 
148  /*
149  * Register read/write commands for a device
150  */
151 
152  /* Read a single byte of a device */
154  /* Write a single byte of a device */
156  /* Multiple Reads of a device */
158  /* Multiple Writes of a device */
160 
161  /* Not sure if we'll need this */
163 
164  /* Set interrupt on change mask */
166 
167  /* Write (or Clear) the CGL for the ADC */
169  /* Multiple Reads of USB memory */
171  /* Multiple Writes to USB memory */
173 
174  /* Issue a start command to a given subsystem */
176  /* Issue a stop command to a given subsystem */
178 
179  /* calibrate the board using CAL_POT_CMD */
181  /* set the DAC FIFO size */
183  /* Write or Clear the CGL for the DAC */
185  /* Read a single value from a subsystem */
187  /* Write a single value to a subsystem */
189  /* Valid DT9812_USB_FIRMWARE_CMD_CODE's will be less than this number */
191 };
192 
196 };
197 
198 #define DT9812_MAX_NUM_MULTI_BYTE_RDS \
199  ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(u8))
200 
204 };
205 
209 };
210 
211 #define DT9812_MAX_NUM_MULTI_BYTE_WRTS \
212  ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
213  sizeof(struct dt9812_write_byte))
214 
218 };
219 
224 };
225 
226 #define DT9812_MAX_NUM_MULTI_BYTE_RMWS \
227  ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
228  sizeof(struct dt9812_rmw_byte))
229 
233 };
234 
237  union {
242  } u;
243 #if 0
244  WRITE_BYTE_INFO WriteByteInfo;
245  READ_BYTE_INFO ReadByteInfo;
246  WRITE_MULTI_INFO WriteMultiInfo;
247  READ_MULTI_INFO ReadMultiInfo;
248  RMW_BYTE_INFO RMWByteInfo;
249  RMW_MULTI_INFO RMWMultiInfo;
250  DAC_THRESHOLD_INFO DacThresholdInfo;
251  INT_ON_CHANGE_MASK_INFO IntOnChangeMaskInfo;
252  CGL_INFO CglInfo;
253  SUBSYSTEM_INFO SubsystemInfo;
254  CAL_POT_CMD CalPotCmd;
255  WRITE_DEV_BYTE_INFO WriteDevByteInfo;
256  READ_DEV_BYTE_INFO ReadDevByteInfo;
257  WRITE_DEV_MULTI_INFO WriteDevMultiInfo;
258  READ_DEV_MULTI_INFO ReadDevMultiInfo;
259  READ_SINGLE_VALUE_INFO ReadSingleValueInfo;
260  WRITE_SINGLE_VALUE_INFO WriteSingleValueInfo;
261 #endif
262 };
263 
264 #define DT9812_NUM_SLOTS 16
265 
266 static DEFINE_SEMAPHORE(dt9812_mutex);
267 
268 static const struct usb_device_id dt9812_table[] = {
269  {USB_DEVICE(0x0867, 0x9812)},
270  {} /* Terminating entry */
271 };
272 
273 MODULE_DEVICE_TABLE(usb, dt9812_table);
274 
275 struct usb_dt9812 {
276  struct slot_dt9812 *slot;
277  struct usb_device *udev;
283  struct {
285  size_t size;
287  struct kref kref;
290 };
291 
293  struct slot_dt9812 *slot;
295 };
296 
297 struct slot_dt9812 {
298  struct semaphore mutex;
300  struct usb_dt9812 *usb;
302 };
303 
304 static const struct comedi_lrange dt9812_10_ain_range = { 1, {
305  BIP_RANGE(10),
306  }
307 };
308 
309 static const struct comedi_lrange dt9812_2pt5_ain_range = { 1, {
310  UNI_RANGE(2.5),
311  }
312 };
313 
314 static const struct comedi_lrange dt9812_10_aout_range = { 1, {
315  BIP_RANGE(10),
316  }
317 };
318 
319 static const struct comedi_lrange dt9812_2pt5_aout_range = { 1, {
320  UNI_RANGE(2.5),
321  }
322 };
323 
324 static struct slot_dt9812 dt9812[DT9812_NUM_SLOTS];
325 
326 /* Useful shorthand access to private data */
327 #define devpriv ((struct comedi_dt9812 *)dev->private)
328 
329 static inline struct usb_dt9812 *to_dt9812_dev(struct kref *d)
330 {
331  return container_of(d, struct usb_dt9812, kref);
332 }
333 
334 static void dt9812_delete(struct kref *kref)
335 {
336  struct usb_dt9812 *dev = to_dt9812_dev(kref);
337 
338  usb_put_dev(dev->udev);
339  kfree(dev);
340 }
341 
342 static int dt9812_read_info(struct usb_dt9812 *dev, int offset, void *buf,
343  size_t buf_size)
344 {
345  struct dt9812_usb_cmd cmd;
346  int count, retval;
347 
349  cmd.u.flash_data_info.address =
351  cmd.u.flash_data_info.numbytes = cpu_to_le16(buf_size);
352 
353  /* DT9812 only responds to 32 byte writes!! */
354  count = 32;
355  retval = usb_bulk_msg(dev->udev,
356  usb_sndbulkpipe(dev->udev,
357  dev->command_write.addr),
358  &cmd, 32, &count, HZ * 1);
359  if (retval)
360  return retval;
361  retval = usb_bulk_msg(dev->udev,
362  usb_rcvbulkpipe(dev->udev,
363  dev->command_read.addr),
364  buf, buf_size, &count, HZ * 1);
365  return retval;
366 }
367 
368 static int dt9812_read_multiple_registers(struct usb_dt9812 *dev, int reg_count,
369  u8 *address, u8 *value)
370 {
371  struct dt9812_usb_cmd cmd;
372  int i, count, retval;
373 
375  cmd.u.read_multi_info.count = reg_count;
376  for (i = 0; i < reg_count; i++)
377  cmd.u.read_multi_info.address[i] = address[i];
378 
379  /* DT9812 only responds to 32 byte writes!! */
380  count = 32;
381  retval = usb_bulk_msg(dev->udev,
382  usb_sndbulkpipe(dev->udev,
383  dev->command_write.addr),
384  &cmd, 32, &count, HZ * 1);
385  if (retval)
386  return retval;
387  retval = usb_bulk_msg(dev->udev,
388  usb_rcvbulkpipe(dev->udev,
389  dev->command_read.addr),
390  value, reg_count, &count, HZ * 1);
391  return retval;
392 }
393 
394 static int dt9812_write_multiple_registers(struct usb_dt9812 *dev,
395  int reg_count, u8 *address,
396  u8 *value)
397 {
398  struct dt9812_usb_cmd cmd;
399  int i, count, retval;
400 
402  cmd.u.read_multi_info.count = reg_count;
403  for (i = 0; i < reg_count; i++) {
404  cmd.u.write_multi_info.write[i].address = address[i];
405  cmd.u.write_multi_info.write[i].value = value[i];
406  }
407  /* DT9812 only responds to 32 byte writes!! */
408  retval = usb_bulk_msg(dev->udev,
409  usb_sndbulkpipe(dev->udev,
410  dev->command_write.addr),
411  &cmd, 32, &count, HZ * 1);
412  return retval;
413 }
414 
415 static int dt9812_rmw_multiple_registers(struct usb_dt9812 *dev, int reg_count,
416  struct dt9812_rmw_byte *rmw)
417 {
418  struct dt9812_usb_cmd cmd;
419  int i, count, retval;
420 
422  cmd.u.rmw_multi_info.count = reg_count;
423  for (i = 0; i < reg_count; i++)
424  cmd.u.rmw_multi_info.rmw[i] = rmw[i];
425 
426  /* DT9812 only responds to 32 byte writes!! */
427  retval = usb_bulk_msg(dev->udev,
428  usb_sndbulkpipe(dev->udev,
429  dev->command_write.addr),
430  &cmd, 32, &count, HZ * 1);
431  return retval;
432 }
433 
434 static int dt9812_digital_in(struct slot_dt9812 *slot, u8 *bits)
435 {
436  int result = -ENODEV;
437 
438  down(&slot->mutex);
439  if (slot->usb) {
440  u8 reg[2] = { F020_SFR_P3, F020_SFR_P1 };
441  u8 value[2];
442 
443  result = dt9812_read_multiple_registers(slot->usb, 2, reg,
444  value);
445  if (result == 0) {
446  /*
447  * bits 0-6 in F020_SFR_P3 are bits 0-6 in the digital
448  * input port bit 3 in F020_SFR_P1 is bit 7 in the
449  * digital input port
450  */
451  *bits = (value[0] & 0x7f) | ((value[1] & 0x08) << 4);
452  /* printk("%2.2x, %2.2x -> %2.2x\n",
453  value[0], value[1], *bits); */
454  }
455  }
456  up(&slot->mutex);
457 
458  return result;
459 }
460 
461 static int dt9812_digital_out(struct slot_dt9812 *slot, u8 bits)
462 {
463  int result = -ENODEV;
464 
465  down(&slot->mutex);
466  if (slot->usb) {
467  u8 reg[1];
468  u8 value[1];
469 
470  reg[0] = F020_SFR_P2;
471  value[0] = bits;
472  result = dt9812_write_multiple_registers(slot->usb, 1, reg,
473  value);
474  slot->usb->digital_out_shadow = bits;
475  }
476  up(&slot->mutex);
477  return result;
478 }
479 
480 static int dt9812_digital_out_shadow(struct slot_dt9812 *slot, u8 *bits)
481 {
482  int result = -ENODEV;
483 
484  down(&slot->mutex);
485  if (slot->usb) {
486  *bits = slot->usb->digital_out_shadow;
487  result = 0;
488  }
489  up(&slot->mutex);
490  return result;
491 }
492 
493 static void dt9812_configure_mux(struct usb_dt9812 *dev,
494  struct dt9812_rmw_byte *rmw, int channel)
495 {
496  if (dev->device == DT9812_DEVID_DT9812_10) {
497  /* In the DT9812/10V MUX is selected by P1.5-7 */
498  rmw->address = F020_SFR_P1;
499  rmw->and_mask = 0xe0;
500  rmw->or_value = channel << 5;
501  } else {
502  /* In the DT9812/2.5V, internal mux is selected by bits 0:2 */
503  rmw->address = F020_SFR_AMX0SL;
504  rmw->and_mask = 0xff;
505  rmw->or_value = channel & 0x07;
506  }
507 }
508 
509 static void dt9812_configure_gain(struct usb_dt9812 *dev,
510  struct dt9812_rmw_byte *rmw,
511  enum dt9812_gain gain)
512 {
513  if (dev->device == DT9812_DEVID_DT9812_10) {
514  /* In the DT9812/10V, there is an external gain of 0.5 */
515  gain <<= 1;
516  }
517 
518  rmw->address = F020_SFR_ADC0CF;
521  switch (gain) {
522  /*
523  * 000 -> Gain = 1
524  * 001 -> Gain = 2
525  * 010 -> Gain = 4
526  * 011 -> Gain = 8
527  * 10x -> Gain = 16
528  * 11x -> Gain = 0.5
529  */
530  case DT9812_GAIN_0PT5:
533  break;
534  case DT9812_GAIN_1:
535  rmw->or_value = 0x00;
536  break;
537  case DT9812_GAIN_2:
539  break;
540  case DT9812_GAIN_4:
542  break;
543  case DT9812_GAIN_8:
546  break;
547  case DT9812_GAIN_16:
549  break;
550  default:
551  dev_err(&dev->interface->dev, "Illegal gain %d\n", gain);
552 
553  }
554 }
555 
556 static int dt9812_analog_in(struct slot_dt9812 *slot, int channel, u16 *value,
557  enum dt9812_gain gain)
558 {
559  struct dt9812_rmw_byte rmw[3];
560  u8 reg[3] = {
564  };
565  u8 val[3];
566  int result = -ENODEV;
567 
568  down(&slot->mutex);
569  if (!slot->usb)
570  goto exit;
571 
572  /* 1 select the gain */
573  dt9812_configure_gain(slot->usb, &rmw[0], gain);
574 
575  /* 2 set the MUX to select the channel */
576  dt9812_configure_mux(slot->usb, &rmw[1], channel);
577 
578  /* 3 start conversion */
579  rmw[2].address = F020_SFR_ADC0CN;
580  rmw[2].and_mask = 0xff;
582 
583  result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
584  if (result)
585  goto exit;
586 
587  /* read the status and ADC */
588  result = dt9812_read_multiple_registers(slot->usb, 3, reg, val);
589  if (result)
590  goto exit;
591  /*
592  * An ADC conversion takes 16 SAR clocks cycles, i.e. about 9us.
593  * Therefore, between the instant that AD0BUSY was set via
594  * dt9812_rmw_multiple_registers and the read of AD0BUSY via
595  * dt9812_read_multiple_registers, the conversion should be complete
596  * since these two operations require two USB transactions each taking
597  * at least a millisecond to complete. However, lets make sure that
598  * conversion is finished.
599  */
602  switch (slot->usb->device) {
604  /*
605  * For DT9812-10V the personality module set the
606  * encoding to 2's complement. Hence, convert it before
607  * returning it
608  */
609  *value = ((val[1] << 8) | val[2]) + 0x800;
610  break;
612  *value = (val[1] << 8) | val[2];
613  break;
614  }
615  }
616 
617 exit:
618  up(&slot->mutex);
619  return result;
620 }
621 
622 static int dt9812_analog_out_shadow(struct slot_dt9812 *slot, int channel,
623  u16 *value)
624 {
625  int result = -ENODEV;
626 
627  down(&slot->mutex);
628  if (slot->usb) {
629  *value = slot->usb->analog_out_shadow[channel];
630  result = 0;
631  }
632  up(&slot->mutex);
633 
634  return result;
635 }
636 
637 static int dt9812_analog_out(struct slot_dt9812 *slot, int channel, u16 value)
638 {
639  int result = -ENODEV;
640 
641  down(&slot->mutex);
642  if (slot->usb) {
643  struct dt9812_rmw_byte rmw[3];
644 
645  switch (channel) {
646  case 0:
647  /* 1. Set DAC mode */
648  rmw[0].address = F020_SFR_DAC0CN;
649  rmw[0].and_mask = 0xff;
651 
652  /* 2 load low byte of DAC value first */
653  rmw[1].address = F020_SFR_DAC0L;
654  rmw[1].and_mask = 0xff;
655  rmw[1].or_value = value & 0xff;
656 
657  /* 3 load high byte of DAC value next to latch the
658  12-bit value */
659  rmw[2].address = F020_SFR_DAC0H;
660  rmw[2].and_mask = 0xff;
661  rmw[2].or_value = (value >> 8) & 0xf;
662  break;
663 
664  case 1:
665  /* 1. Set DAC mode */
666  rmw[0].address = F020_SFR_DAC1CN;
667  rmw[0].and_mask = 0xff;
669 
670  /* 2 load low byte of DAC value first */
671  rmw[1].address = F020_SFR_DAC1L;
672  rmw[1].and_mask = 0xff;
673  rmw[1].or_value = value & 0xff;
674 
675  /* 3 load high byte of DAC value next to latch the
676  12-bit value */
677  rmw[2].address = F020_SFR_DAC1H;
678  rmw[2].and_mask = 0xff;
679  rmw[2].or_value = (value >> 8) & 0xf;
680  break;
681  }
682  result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
683  slot->usb->analog_out_shadow[channel] = value;
684  }
685  up(&slot->mutex);
686 
687  return result;
688 }
689 
690 /*
691  * USB framework functions
692  */
693 
694 static int dt9812_probe(struct usb_interface *interface,
695  const struct usb_device_id *id)
696 {
697  int retval = -ENOMEM;
698  struct usb_dt9812 *dev = NULL;
699  struct usb_host_interface *iface_desc;
701  int i;
702  u8 fw;
703 
704  /* allocate memory for our device state and initialize it */
705  dev = kzalloc(sizeof(*dev), GFP_KERNEL);
706  if (dev == NULL) {
707  dev_err(&interface->dev, "Out of memory\n");
708  goto error;
709  }
710  kref_init(&dev->kref);
711 
712  dev->udev = usb_get_dev(interface_to_usbdev(interface));
713  dev->interface = interface;
714 
715  /* Check endpoints */
716  iface_desc = interface->cur_altsetting;
717 
718  if (iface_desc->desc.bNumEndpoints != 5) {
719  dev_err(&interface->dev, "Wrong number of endpoints.\n");
720  retval = -ENODEV;
721  goto error;
722  }
723 
724  for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
725  int direction = -1;
726  endpoint = &iface_desc->endpoint[i].desc;
727  switch (i) {
728  case 0:
729  direction = USB_DIR_IN;
730  dev->message_pipe.addr = endpoint->bEndpointAddress;
731  dev->message_pipe.size =
732  le16_to_cpu(endpoint->wMaxPacketSize);
733 
734  break;
735  case 1:
736  direction = USB_DIR_OUT;
737  dev->command_write.addr = endpoint->bEndpointAddress;
738  dev->command_write.size =
739  le16_to_cpu(endpoint->wMaxPacketSize);
740  break;
741  case 2:
742  direction = USB_DIR_IN;
743  dev->command_read.addr = endpoint->bEndpointAddress;
744  dev->command_read.size =
745  le16_to_cpu(endpoint->wMaxPacketSize);
746  break;
747  case 3:
748  direction = USB_DIR_OUT;
749  dev->write_stream.addr = endpoint->bEndpointAddress;
750  dev->write_stream.size =
751  le16_to_cpu(endpoint->wMaxPacketSize);
752  break;
753  case 4:
754  direction = USB_DIR_IN;
755  dev->read_stream.addr = endpoint->bEndpointAddress;
756  dev->read_stream.size =
757  le16_to_cpu(endpoint->wMaxPacketSize);
758  break;
759  }
760  if ((endpoint->bEndpointAddress & USB_DIR_IN) != direction) {
761  dev_err(&interface->dev,
762  "Endpoint has wrong direction.\n");
763  retval = -ENODEV;
764  goto error;
765  }
766  }
767  if (dt9812_read_info(dev, 0, &fw, sizeof(fw)) != 0) {
768  /*
769  * Seems like a configuration reset is necessary if driver is
770  * reloaded while device is attached
771  */
773  for (i = 0; i < 10; i++) {
774  retval = dt9812_read_info(dev, 1, &fw, sizeof(fw));
775  if (retval == 0) {
776  dev_info(&interface->dev,
777  "usb_reset_configuration succeeded "
778  "after %d iterations\n", i);
779  break;
780  }
781  }
782  }
783 
784  if (dt9812_read_info(dev, 1, &dev->vendor, sizeof(dev->vendor)) != 0) {
785  dev_err(&interface->dev, "Failed to read vendor.\n");
786  retval = -ENODEV;
787  goto error;
788  }
789  if (dt9812_read_info(dev, 3, &dev->product, sizeof(dev->product)) != 0) {
790  dev_err(&interface->dev, "Failed to read product.\n");
791  retval = -ENODEV;
792  goto error;
793  }
794  if (dt9812_read_info(dev, 5, &dev->device, sizeof(dev->device)) != 0) {
795  dev_err(&interface->dev, "Failed to read device.\n");
796  retval = -ENODEV;
797  goto error;
798  }
799  if (dt9812_read_info(dev, 7, &dev->serial, sizeof(dev->serial)) != 0) {
800  dev_err(&interface->dev, "Failed to read serial.\n");
801  retval = -ENODEV;
802  goto error;
803  }
804 
805  dev->vendor = le16_to_cpu(dev->vendor);
806  dev->product = le16_to_cpu(dev->product);
807  dev->device = le16_to_cpu(dev->device);
808  dev->serial = le32_to_cpu(dev->serial);
809  switch (dev->device) {
811  dev->analog_out_shadow[0] = 0x0800;
812  dev->analog_out_shadow[1] = 0x800;
813  break;
815  dev->analog_out_shadow[0] = 0x0000;
816  dev->analog_out_shadow[1] = 0x0000;
817  break;
818  }
819  dev->digital_out_shadow = 0;
820 
821  /* save our data pointer in this interface device */
822  usb_set_intfdata(interface, dev);
823 
824  /* let the user know what node this device is now attached to */
825  dev_info(&interface->dev, "USB DT9812 (%4.4x.%4.4x.%4.4x) #0x%8.8x\n",
826  dev->vendor, dev->product, dev->device, dev->serial);
827 
828  down(&dt9812_mutex);
829  {
830  /* Find a slot for the USB device */
831  struct slot_dt9812 *first = NULL;
832  struct slot_dt9812 *best = NULL;
833 
834  for (i = 0; i < DT9812_NUM_SLOTS; i++) {
835  if (!first && !dt9812[i].usb && dt9812[i].serial == 0)
836  first = &dt9812[i];
837  if (!best && dt9812[i].serial == dev->serial)
838  best = &dt9812[i];
839  }
840 
841  if (!best)
842  best = first;
843 
844  if (best) {
845  down(&best->mutex);
846  best->usb = dev;
847  dev->slot = best;
848  up(&best->mutex);
849  }
850  }
851  up(&dt9812_mutex);
852 
853  return 0;
854 
855 error:
856  if (dev)
857  kref_put(&dev->kref, dt9812_delete);
858  return retval;
859 }
860 
861 static void dt9812_disconnect(struct usb_interface *interface)
862 {
863  struct usb_dt9812 *dev;
864  int minor = interface->minor;
865 
866  down(&dt9812_mutex);
867  dev = usb_get_intfdata(interface);
868  if (dev->slot) {
869  down(&dev->slot->mutex);
870  dev->slot->usb = NULL;
871  up(&dev->slot->mutex);
872  dev->slot = NULL;
873  }
874  usb_set_intfdata(interface, NULL);
875  up(&dt9812_mutex);
876 
877  /* queue final destruction */
878  kref_put(&dev->kref, dt9812_delete);
879 
880  dev_info(&interface->dev, "USB Dt9812 #%d now disconnected\n", minor);
881 }
882 
883 static struct usb_driver dt9812_usb_driver = {
884  .name = "dt9812",
885  .probe = dt9812_probe,
886  .disconnect = dt9812_disconnect,
887  .id_table = dt9812_table,
888 };
889 
890 /*
891  * Comedi functions
892  */
893 
894 static int dt9812_comedi_open(struct comedi_device *dev)
895 {
896  int result = -ENODEV;
897 
898  down(&devpriv->slot->mutex);
899  if (devpriv->slot->usb) {
900  /* We have an attached device, fill in current range info */
901  struct comedi_subdevice *s;
902 
903  s = &dev->subdevices[0];
904  s->n_chan = 8;
905  s->maxdata = 1;
906 
907  s = &dev->subdevices[1];
908  s->n_chan = 8;
909  s->maxdata = 1;
910 
911  s = &dev->subdevices[2];
912  s->n_chan = 8;
913  switch (devpriv->slot->usb->device) {
914  case 0:{
915  s->maxdata = 4095;
916  s->range_table = &dt9812_10_ain_range;
917  }
918  break;
919  case 1:{
920  s->maxdata = 4095;
921  s->range_table = &dt9812_2pt5_ain_range;
922  }
923  break;
924  }
925 
926  s = &dev->subdevices[3];
927  s->n_chan = 2;
928  switch (devpriv->slot->usb->device) {
929  case 0:{
930  s->maxdata = 4095;
931  s->range_table = &dt9812_10_aout_range;
932  }
933  break;
934  case 1:{
935  s->maxdata = 4095;
936  s->range_table = &dt9812_2pt5_aout_range;
937  }
938  break;
939  }
940  result = 0;
941  }
942  up(&devpriv->slot->mutex);
943  return result;
944 }
945 
946 static int dt9812_di_rinsn(struct comedi_device *dev,
947  struct comedi_subdevice *s, struct comedi_insn *insn,
948  unsigned int *data)
949 {
950  int n;
951  u8 bits = 0;
952 
953  dt9812_digital_in(devpriv->slot, &bits);
954  for (n = 0; n < insn->n; n++)
955  data[n] = ((1 << insn->chanspec) & bits) != 0;
956  return n;
957 }
958 
959 static int dt9812_do_winsn(struct comedi_device *dev,
960  struct comedi_subdevice *s, struct comedi_insn *insn,
961  unsigned int *data)
962 {
963  int n;
964  u8 bits = 0;
965 
966  dt9812_digital_out_shadow(devpriv->slot, &bits);
967  for (n = 0; n < insn->n; n++) {
968  u8 mask = 1 << insn->chanspec;
969 
970  bits &= ~mask;
971  if (data[n])
972  bits |= mask;
973  }
974  dt9812_digital_out(devpriv->slot, bits);
975  return n;
976 }
977 
978 static int dt9812_ai_rinsn(struct comedi_device *dev,
979  struct comedi_subdevice *s, struct comedi_insn *insn,
980  unsigned int *data)
981 {
982  int n;
983 
984  for (n = 0; n < insn->n; n++) {
985  u16 value = 0;
986 
987  dt9812_analog_in(devpriv->slot, insn->chanspec, &value,
988  DT9812_GAIN_1);
989  data[n] = value;
990  }
991  return n;
992 }
993 
994 static int dt9812_ao_rinsn(struct comedi_device *dev,
995  struct comedi_subdevice *s, struct comedi_insn *insn,
996  unsigned int *data)
997 {
998  int n;
999  u16 value;
1000 
1001  for (n = 0; n < insn->n; n++) {
1002  value = 0;
1003  dt9812_analog_out_shadow(devpriv->slot, insn->chanspec, &value);
1004  data[n] = value;
1005  }
1006  return n;
1007 }
1008 
1009 static int dt9812_ao_winsn(struct comedi_device *dev,
1010  struct comedi_subdevice *s, struct comedi_insn *insn,
1011  unsigned int *data)
1012 {
1013  int n;
1014 
1015  for (n = 0; n < insn->n; n++)
1016  dt9812_analog_out(devpriv->slot, insn->chanspec, data[n]);
1017  return n;
1018 }
1019 
1020 static int dt9812_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1021 {
1022  int i;
1023  struct comedi_subdevice *s;
1024  int ret;
1025 
1026  dev->board_name = "dt9812";
1027 
1028  if (alloc_private(dev, sizeof(struct comedi_dt9812)) < 0)
1029  return -ENOMEM;
1030 
1031  /*
1032  * Special open routine, since USB unit may be unattached at
1033  * comedi_config time, hence range can not be determined
1034  */
1035  dev->open = dt9812_comedi_open;
1036 
1037  devpriv->serial = it->options[0];
1038 
1039  ret = comedi_alloc_subdevices(dev, 4);
1040  if (ret)
1041  return ret;
1042 
1043  /* digital input subdevice */
1044  s = &dev->subdevices[0];
1045  s->type = COMEDI_SUBD_DI;
1047  s->n_chan = 0;
1048  s->maxdata = 1;
1049  s->range_table = &range_digital;
1050  s->insn_read = &dt9812_di_rinsn;
1051 
1052  /* digital output subdevice */
1053  s = &dev->subdevices[1];
1054  s->type = COMEDI_SUBD_DO;
1056  s->n_chan = 0;
1057  s->maxdata = 1;
1058  s->range_table = &range_digital;
1059  s->insn_write = &dt9812_do_winsn;
1060 
1061  /* analog input subdevice */
1062  s = &dev->subdevices[2];
1063  s->type = COMEDI_SUBD_AI;
1065  s->n_chan = 0;
1066  s->maxdata = 1;
1067  s->range_table = NULL;
1068  s->insn_read = &dt9812_ai_rinsn;
1069 
1070  /* analog output subdevice */
1071  s = &dev->subdevices[3];
1072  s->type = COMEDI_SUBD_AO;
1074  s->n_chan = 0;
1075  s->maxdata = 1;
1076  s->range_table = NULL;
1077  s->insn_write = &dt9812_ao_winsn;
1078  s->insn_read = &dt9812_ao_rinsn;
1079 
1080  printk(KERN_INFO "comedi%d: successfully attached to dt9812.\n",
1081  dev->minor);
1082 
1083  down(&dt9812_mutex);
1084  /* Find a slot for the comedi device */
1085  {
1086  struct slot_dt9812 *first = NULL;
1087  struct slot_dt9812 *best = NULL;
1088  for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1089  if (!first && !dt9812[i].comedi) {
1090  /* First free slot from comedi side */
1091  first = &dt9812[i];
1092  }
1093  if (!best &&
1094  dt9812[i].usb &&
1095  dt9812[i].usb->serial == devpriv->serial) {
1096  /* We have an attaced device with matching ID */
1097  best = &dt9812[i];
1098  }
1099  }
1100  if (!best)
1101  best = first;
1102  if (best) {
1103  down(&best->mutex);
1104  best->comedi = devpriv;
1105  best->serial = devpriv->serial;
1106  devpriv->slot = best;
1107  up(&best->mutex);
1108  }
1109  }
1110  up(&dt9812_mutex);
1111 
1112  return 0;
1113 }
1114 
1115 static void dt9812_detach(struct comedi_device *dev)
1116 {
1117  /* Nothing to cleanup */
1118 }
1119 
1120 static struct comedi_driver dt9812_comedi_driver = {
1121  .module = THIS_MODULE,
1122  .driver_name = "dt9812",
1123  .attach = dt9812_attach,
1124  .detach = dt9812_detach,
1125 };
1126 
1127 static int __init usb_dt9812_init(void)
1128 {
1129  int result, i;
1130 
1131  /* Initialize all driver slots */
1132  for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1133  sema_init(&dt9812[i].mutex, 1);
1134  dt9812[i].serial = 0;
1135  dt9812[i].usb = NULL;
1136  dt9812[i].comedi = NULL;
1137  }
1138  dt9812[12].serial = 0x0;
1139 
1140  /* register with the USB subsystem */
1141  result = usb_register(&dt9812_usb_driver);
1142  if (result) {
1143  printk(KERN_ERR KBUILD_MODNAME
1144  ": usb_register failed. Error number %d\n", result);
1145  return result;
1146  }
1147  /* register with comedi */
1148  result = comedi_driver_register(&dt9812_comedi_driver);
1149  if (result) {
1150  usb_deregister(&dt9812_usb_driver);
1151  printk(KERN_ERR KBUILD_MODNAME
1152  ": comedi_driver_register failed. Error number %d\n",
1153  result);
1154  }
1155 
1156  return result;
1157 }
1158 
1159 static void __exit usb_dt9812_exit(void)
1160 {
1161  /* unregister with comedi */
1162  comedi_driver_unregister(&dt9812_comedi_driver);
1163 
1164  /* deregister this driver with the USB subsystem */
1165  usb_deregister(&dt9812_usb_driver);
1166 }
1167 
1168 module_init(usb_dt9812_init);
1169 module_exit(usb_dt9812_exit);
1170 
1171 MODULE_AUTHOR("Anders Blomdell <[email protected]>");
1172 MODULE_DESCRIPTION("Comedi DT9812 driver");
1173 MODULE_LICENSE("GPL");