46 #include <linux/kernel.h>
47 #include <linux/errno.h>
49 #include <linux/slab.h>
50 #include <linux/module.h>
55 #include "../comedidev.h"
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
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
81 #define F020_MASK_ADC0CF_AMP0GN0 0x01
82 #define F020_MASK_ADC0CF_AMP0GN1 0x02
83 #define F020_MASK_ADC0CF_AMP0GN2 0x04
85 #define F020_MASK_ADC0CN_AD0EN 0x80
86 #define F020_MASK_ADC0CN_AD0INT 0x20
87 #define F020_MASK_ADC0CN_AD0BUSY 0x10
89 #define F020_MASK_DACxCN_DACxEN 0x80
198 #define DT9812_MAX_NUM_MULTI_BYTE_RDS \
199 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(u8))
211 #define DT9812_MAX_NUM_MULTI_BYTE_WRTS \
212 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
213 sizeof(struct dt9812_write_byte))
226 #define DT9812_MAX_NUM_MULTI_BYTE_RMWS \
227 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
228 sizeof(struct dt9812_rmw_byte))
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;
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;
264 #define DT9812_NUM_SLOTS 16
269 {USB_DEVICE(0x0867, 0x9812)},
304 static const struct comedi_lrange dt9812_10_ain_range = { 1, {
309 static const struct comedi_lrange dt9812_2pt5_ain_range = { 1, {
314 static const struct comedi_lrange dt9812_10_aout_range = { 1, {
319 static const struct comedi_lrange dt9812_2pt5_aout_range = { 1, {
327 #define devpriv ((struct comedi_dt9812 *)dev->private)
334 static void dt9812_delete(
struct kref *
kref)
349 cmd.u.flash_data_info.address =
356 usb_sndbulkpipe(dev->
udev,
358 &
cmd, 32, &count,
HZ * 1);
362 usb_rcvbulkpipe(dev->
udev,
364 buf, buf_size, &count,
HZ * 1);
368 static int dt9812_read_multiple_registers(
struct usb_dt9812 *dev,
int reg_count,
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];
382 usb_sndbulkpipe(dev->
udev,
384 &
cmd, 32, &count,
HZ * 1);
388 usb_rcvbulkpipe(dev->
udev,
390 value, reg_count, &count,
HZ * 1);
394 static int dt9812_write_multiple_registers(
struct usb_dt9812 *dev,
395 int reg_count,
u8 *address,
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];
409 usb_sndbulkpipe(dev->
udev,
411 &
cmd, 32, &count,
HZ * 1);
415 static int dt9812_rmw_multiple_registers(
struct usb_dt9812 *dev,
int reg_count,
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];
428 usb_sndbulkpipe(dev->
udev,
430 &
cmd, 32, &count,
HZ * 1);
443 result = dt9812_read_multiple_registers(slot->
usb, 2, reg,
451 *bits = (value[0] & 0x7f) | ((value[1] & 0x08) << 4);
461 static int dt9812_digital_out(
struct slot_dt9812 *slot,
u8 bits)
472 result = dt9812_write_multiple_registers(slot->
usb, 1, reg,
474 slot->
usb->digital_out_shadow =
bits;
480 static int dt9812_digital_out_shadow(
struct slot_dt9812 *slot,
u8 *bits)
486 *bits = slot->
usb->digital_out_shadow;
493 static void dt9812_configure_mux(
struct usb_dt9812 *dev,
509 static void dt9812_configure_gain(
struct usb_dt9812 *dev,
556 static int dt9812_analog_in(
struct slot_dt9812 *slot,
int channel,
u16 *value,
573 dt9812_configure_gain(slot->
usb, &rmw[0], gain);
576 dt9812_configure_mux(slot->
usb, &rmw[1], channel);
583 result = dt9812_rmw_multiple_registers(slot->
usb, 3, rmw);
588 result = dt9812_read_multiple_registers(slot->
usb, 3, reg, val);
602 switch (slot->
usb->device) {
609 *value = ((val[1] << 8) | val[2]) + 0x800;
612 *value = (val[1] << 8) | val[2];
622 static int dt9812_analog_out_shadow(
struct slot_dt9812 *slot,
int channel,
629 *value = slot->
usb->analog_out_shadow[
channel];
637 static int dt9812_analog_out(
struct slot_dt9812 *slot,
int channel,
u16 value)
661 rmw[2].
or_value = (value >> 8) & 0xf;
679 rmw[2].
or_value = (value >> 8) & 0xf;
682 result = dt9812_rmw_multiple_registers(slot->
usb, 3, rmw);
699 struct usb_host_interface *iface_desc;
707 dev_err(&interface->dev,
"Out of memory\n");
710 kref_init(&dev->
kref);
716 iface_desc = interface->cur_altsetting;
718 if (iface_desc->desc.bNumEndpoints != 5) {
719 dev_err(&interface->dev,
"Wrong number of endpoints.\n");
724 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++
i) {
726 endpoint = &iface_desc->endpoint[
i].desc;
762 "Endpoint has wrong direction.\n");
767 if (dt9812_read_info(dev, 0, &fw,
sizeof(fw)) != 0) {
773 for (i = 0; i < 10; i++) {
774 retval = dt9812_read_info(dev, 1, &fw,
sizeof(fw));
777 "usb_reset_configuration succeeded "
778 "after %d iterations\n", i);
784 if (dt9812_read_info(dev, 1, &dev->
vendor,
sizeof(dev->
vendor)) != 0) {
785 dev_err(&interface->dev,
"Failed to read vendor.\n");
789 if (dt9812_read_info(dev, 3, &dev->
product,
sizeof(dev->
product)) != 0) {
790 dev_err(&interface->dev,
"Failed to read product.\n");
794 if (dt9812_read_info(dev, 5, &dev->
device,
sizeof(dev->
device)) != 0) {
795 dev_err(&interface->dev,
"Failed to read device.\n");
799 if (dt9812_read_info(dev, 7, &dev->
serial,
sizeof(dev->
serial)) != 0) {
800 dev_err(&interface->dev,
"Failed to read serial.\n");
822 usb_set_intfdata(interface, dev);
825 dev_info(&interface->dev,
"USB DT9812 (%4.4x.%4.4x.%4.4x) #0x%8.8x\n",
835 if (!first && !dt9812[i].
usb && dt9812[i].
serial == 0)
857 kref_put(&dev->
kref, dt9812_delete);
861 static void dt9812_disconnect(
struct usb_interface *interface)
864 int minor = interface->minor;
867 dev = usb_get_intfdata(interface);
874 usb_set_intfdata(interface,
NULL);
878 kref_put(&dev->
kref, dt9812_delete);
880 dev_info(&interface->dev,
"USB Dt9812 #%d now disconnected\n", minor);
883 static struct usb_driver dt9812_usb_driver = {
885 .probe = dt9812_probe,
886 .disconnect = dt9812_disconnect,
887 .id_table = dt9812_table,
913 switch (
devpriv->slot->usb->device) {
928 switch (
devpriv->slot->usb->device) {
953 dt9812_digital_in(
devpriv->slot, &bits);
954 for (n = 0; n < insn->
n; n++)
955 data[n] = ((1 << insn->
chanspec) & bits) != 0;
966 dt9812_digital_out_shadow(
devpriv->slot, &bits);
967 for (n = 0; n < insn->
n; n++) {
974 dt9812_digital_out(
devpriv->slot, bits);
984 for (n = 0; n < insn->
n; n++) {
1001 for (n = 0; n < insn->
n; n++) {
1015 for (n = 0; n < insn->
n; n++)
1035 dev->
open = dt9812_comedi_open;
1083 down(&dt9812_mutex);
1089 if (!first && !dt9812[i].
comedi) {
1122 .driver_name =
"dt9812",
1123 .attach = dt9812_attach,
1124 .detach = dt9812_detach,
1127 static int __init usb_dt9812_init(
void)
1133 sema_init(&dt9812[i].
mutex, 1);
1134 dt9812[
i].serial = 0;
1135 dt9812[
i].usb =
NULL;
1136 dt9812[
i].comedi =
NULL;
1138 dt9812[12].serial = 0x0;
1141 result = usb_register(&dt9812_usb_driver);
1144 ": usb_register failed. Error number %d\n", result);
1152 ": comedi_driver_register failed. Error number %d\n",
1159 static void __exit usb_dt9812_exit(
void)