2 * comedi/drivers/ni_usb6501.c
3 * Comedi driver for National Instruments USB-6501
5 * COMEDI - Linux Control and Measurement Device Interface
6 * Copyright (C) 2014 Luca Ellero <luca.ellero@brickedbrain.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
21 * Description: National Instruments USB-6501 module
22 * Devices: [National Instruments] USB-6501 (ni_usb6501)
23 * Author: Luca Ellero <luca.ellero@brickedbrain.com>
28 * Configuration Options:
33 * NI-6501 - USB PROTOCOL DESCRIPTION
35 * Every command is composed by two USB packets:
39 * Every packet is at least 12 bytes long, here is the meaning of
40 * every field (all values are hex):
45 * byte 3 is the total packet length
48 * byte 5 is is the total packet length - 4
50 * byte 7 is the command
52 * byte 8 is 02 (request) or 00 (response)
53 * byte 9 is 00 (response) or 10 (port request) or 20 (counter request)
54 * byte 10 is always 00
55 * byte 11 is 00 (request) or 02 (response)
60 * REQ: 00 01 00 10 00 0C 01 0E 02 10 00 00 00 03 <PORT> 00
61 * RES: 00 01 00 10 00 0C 01 00 00 00 00 02 00 03 <BMAP> 00
64 * REQ: 00 01 00 14 00 10 01 0F 02 10 00 00 00 03 <PORT> 00 03 <BMAP> 00 00
65 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
67 * CMD: 0x12 SET_PORT_DIR (0 = input, 1 = output)
68 * REQ: 00 01 00 18 00 14 01 12 02 10 00 00
69 * 00 05 <PORT 0> <PORT 1> <PORT 2> 00 05 00 00 00 00 00
70 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
74 * CMD 0x9: START_COUNTER
75 * REQ: 00 01 00 0C 00 08 01 09 02 20 00 00
76 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
78 * CMD 0xC: STOP_COUNTER
79 * REQ: 00 01 00 0C 00 08 01 0C 02 20 00 00
80 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
82 * CMD 0xE: READ_COUNTER
83 * REQ: 00 01 00 0C 00 08 01 0E 02 20 00 00
84 * RES: 00 01 00 10 00 0C 01 00 00 00 00 02 <u32 counter value, Big Endian>
86 * CMD 0xF: WRITE_COUNTER
87 * REQ: 00 01 00 10 00 0C 01 0F 02 20 00 00 <u32 counter value, Big Endian>
88 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
91 * Please visit http://www.brickedbrain.com if you need
92 * additional information or have any questions.
96 #include <linux/kernel.h>
97 #include <linux/module.h>
98 #include <linux/slab.h>
100 #include "../comedi_usb.h"
102 #define NI6501_TIMEOUT 1000
104 /* Port request packets */
105 static const u8 READ_PORT_REQUEST[] = {0x00, 0x01, 0x00, 0x10,
106 0x00, 0x0C, 0x01, 0x0E,
107 0x02, 0x10, 0x00, 0x00,
108 0x00, 0x03, 0x00, 0x00};
110 static const u8 WRITE_PORT_REQUEST[] = {0x00, 0x01, 0x00, 0x14,
111 0x00, 0x10, 0x01, 0x0F,
112 0x02, 0x10, 0x00, 0x00,
113 0x00, 0x03, 0x00, 0x00,
114 0x03, 0x00, 0x00, 0x00};
116 static const u8 SET_PORT_DIR_REQUEST[] = {0x00, 0x01, 0x00, 0x18,
117 0x00, 0x14, 0x01, 0x12,
118 0x02, 0x10, 0x00, 0x00,
119 0x00, 0x05, 0x00, 0x00,
120 0x00, 0x00, 0x05, 0x00,
121 0x00, 0x00, 0x00, 0x00};
123 /* Counter request packets */
124 static const u8 START_COUNTER_REQUEST[] = {0x00, 0x01, 0x00, 0x0C,
125 0x00, 0x08, 0x01, 0x09,
126 0x02, 0x20, 0x00, 0x00};
128 static const u8 STOP_COUNTER_REQUEST[] = {0x00, 0x01, 0x00, 0x0C,
129 0x00, 0x08, 0x01, 0x0C,
130 0x02, 0x20, 0x00, 0x00};
132 static const u8 READ_COUNTER_REQUEST[] = {0x00, 0x01, 0x00, 0x0C,
133 0x00, 0x08, 0x01, 0x0E,
134 0x02, 0x20, 0x00, 0x00};
136 static const u8 WRITE_COUNTER_REQUEST[] = {0x00, 0x01, 0x00, 0x10,
137 0x00, 0x0C, 0x01, 0x0F,
138 0x02, 0x20, 0x00, 0x00,
139 0x00, 0x00, 0x00, 0x00};
141 /* Response packets */
142 static const u8 GENERIC_RESPONSE[] = {0x00, 0x01, 0x00, 0x0C,
143 0x00, 0x08, 0x01, 0x00,
144 0x00, 0x00, 0x00, 0x02};
146 static const u8 READ_PORT_RESPONSE[] = {0x00, 0x01, 0x00, 0x10,
147 0x00, 0x0C, 0x01, 0x00,
148 0x00, 0x00, 0x00, 0x02,
149 0x00, 0x03, 0x00, 0x00};
151 static const u8 READ_COUNTER_RESPONSE[] = {0x00, 0x01, 0x00, 0x10,
152 0x00, 0x0C, 0x01, 0x00,
153 0x00, 0x00, 0x00, 0x02,
154 0x00, 0x00, 0x00, 0x00};
166 struct ni6501_private {
167 struct usb_endpoint_descriptor *ep_rx;
168 struct usb_endpoint_descriptor *ep_tx;
169 struct semaphore sem;
174 static int ni6501_port_command(struct comedi_device *dev, int command,
175 const u8 *port, u8 *bitmap)
177 struct usb_device *usb = comedi_to_usb_dev(dev);
178 struct ni6501_private *devpriv = dev->private;
179 int request_size, response_size;
180 u8 *tx = devpriv->usb_tx_buf;
183 if (command != SET_PORT_DIR && !bitmap)
190 request_size = sizeof(READ_PORT_REQUEST);
191 response_size = sizeof(READ_PORT_RESPONSE);
192 memcpy(tx, READ_PORT_REQUEST, request_size);
196 request_size = sizeof(WRITE_PORT_REQUEST);
197 response_size = sizeof(GENERIC_RESPONSE);
198 memcpy(tx, WRITE_PORT_REQUEST, request_size);
203 request_size = sizeof(SET_PORT_DIR_REQUEST);
204 response_size = sizeof(GENERIC_RESPONSE);
205 memcpy(tx, SET_PORT_DIR_REQUEST, request_size);
215 ret = usb_bulk_msg(usb,
217 devpriv->ep_tx->bEndpointAddress),
225 ret = usb_bulk_msg(usb,
227 devpriv->ep_rx->bEndpointAddress),
235 /* Check if results are valid */
237 if (command == READ_PORT) {
238 bitmap[0] = devpriv->usb_rx_buf[14];
239 /* mask bitmap for comparing */
240 devpriv->usb_rx_buf[14] = 0x00;
242 if (memcmp(devpriv->usb_rx_buf, READ_PORT_RESPONSE,
243 sizeof(READ_PORT_RESPONSE))) {
246 } else if (memcmp(devpriv->usb_rx_buf, GENERIC_RESPONSE,
247 sizeof(GENERIC_RESPONSE))) {
256 static int ni6501_counter_command(struct comedi_device *dev, int command,
259 struct usb_device *usb = comedi_to_usb_dev(dev);
260 struct ni6501_private *devpriv = dev->private;
261 int request_size, response_size;
262 u8 *tx = devpriv->usb_tx_buf;
265 if ((command == READ_COUNTER || command == WRITE_COUNTER) && !val)
272 request_size = sizeof(START_COUNTER_REQUEST);
273 response_size = sizeof(GENERIC_RESPONSE);
274 memcpy(tx, START_COUNTER_REQUEST, request_size);
277 request_size = sizeof(STOP_COUNTER_REQUEST);
278 response_size = sizeof(GENERIC_RESPONSE);
279 memcpy(tx, STOP_COUNTER_REQUEST, request_size);
282 request_size = sizeof(READ_COUNTER_REQUEST);
283 response_size = sizeof(READ_COUNTER_RESPONSE);
284 memcpy(tx, READ_COUNTER_REQUEST, request_size);
287 request_size = sizeof(WRITE_COUNTER_REQUEST);
288 response_size = sizeof(GENERIC_RESPONSE);
289 memcpy(tx, WRITE_COUNTER_REQUEST, request_size);
290 /* Setup tx packet: bytes 12,13,14,15 hold the */
291 /* u32 counter value (Big Endian) */
292 *((__be32 *)&tx[12]) = cpu_to_be32(*val);
299 ret = usb_bulk_msg(usb,
301 devpriv->ep_tx->bEndpointAddress),
309 ret = usb_bulk_msg(usb,
311 devpriv->ep_rx->bEndpointAddress),
319 /* Check if results are valid */
321 if (command == READ_COUNTER) {
324 /* Read counter value: bytes 12,13,14,15 of rx packet */
325 /* hold the u32 counter value (Big Endian) */
326 *val = be32_to_cpu(*((__be32 *)&devpriv->usb_rx_buf[12]));
328 /* mask counter value for comparing */
329 for (i = 12; i < sizeof(READ_COUNTER_RESPONSE); ++i)
330 devpriv->usb_rx_buf[i] = 0x00;
332 if (memcmp(devpriv->usb_rx_buf, READ_COUNTER_RESPONSE,
333 sizeof(READ_COUNTER_RESPONSE))) {
336 } else if (memcmp(devpriv->usb_rx_buf, GENERIC_RESPONSE,
337 sizeof(GENERIC_RESPONSE))) {
346 static int ni6501_dio_insn_config(struct comedi_device *dev,
347 struct comedi_subdevice *s,
348 struct comedi_insn *insn,
354 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
358 port[0] = (s->io_bits) & 0xff;
359 port[1] = (s->io_bits >> 8) & 0xff;
360 port[2] = (s->io_bits >> 16) & 0xff;
362 ret = ni6501_port_command(dev, SET_PORT_DIR, port, NULL);
369 static int ni6501_dio_insn_bits(struct comedi_device *dev,
370 struct comedi_subdevice *s,
371 struct comedi_insn *insn,
379 mask = comedi_dio_update_state(s, data);
381 for (port = 0; port < 3; port++) {
382 if (mask & (0xFF << port * 8)) {
383 bitmap = (s->state >> port * 8) & 0xFF;
384 ret = ni6501_port_command(dev, WRITE_PORT,
393 for (port = 0; port < 3; port++) {
394 ret = ni6501_port_command(dev, READ_PORT, &port, &bitmap);
397 data[1] |= bitmap << port * 8;
403 static int ni6501_cnt_insn_config(struct comedi_device *dev,
404 struct comedi_subdevice *s,
405 struct comedi_insn *insn,
412 case INSN_CONFIG_ARM:
413 ret = ni6501_counter_command(dev, START_COUNTER, NULL);
415 case INSN_CONFIG_DISARM:
416 ret = ni6501_counter_command(dev, STOP_COUNTER, NULL);
418 case INSN_CONFIG_RESET:
419 ret = ni6501_counter_command(dev, STOP_COUNTER, NULL);
422 ret = ni6501_counter_command(dev, WRITE_COUNTER, &val);
428 return ret ? ret : insn->n;
431 static int ni6501_cnt_insn_read(struct comedi_device *dev,
432 struct comedi_subdevice *s,
433 struct comedi_insn *insn,
440 for (i = 0; i < insn->n; i++) {
441 ret = ni6501_counter_command(dev, READ_COUNTER, &val);
450 static int ni6501_cnt_insn_write(struct comedi_device *dev,
451 struct comedi_subdevice *s,
452 struct comedi_insn *insn,
458 u32 val = data[insn->n - 1];
460 ret = ni6501_counter_command(dev, WRITE_COUNTER, &val);
468 static int ni6501_alloc_usb_buffers(struct comedi_device *dev)
470 struct ni6501_private *devpriv = dev->private;
473 size = le16_to_cpu(devpriv->ep_rx->wMaxPacketSize);
474 devpriv->usb_rx_buf = kzalloc(size, GFP_KERNEL);
475 if (!devpriv->usb_rx_buf)
478 size = le16_to_cpu(devpriv->ep_tx->wMaxPacketSize);
479 devpriv->usb_tx_buf = kzalloc(size, GFP_KERNEL);
480 if (!devpriv->usb_tx_buf) {
481 kfree(devpriv->usb_rx_buf);
488 static int ni6501_find_endpoints(struct comedi_device *dev)
490 struct usb_interface *intf = comedi_to_usb_interface(dev);
491 struct ni6501_private *devpriv = dev->private;
492 struct usb_host_interface *iface_desc = intf->cur_altsetting;
493 struct usb_endpoint_descriptor *ep_desc;
496 if (iface_desc->desc.bNumEndpoints != 2) {
497 dev_err(dev->class_dev, "Wrong number of endpoints\n");
501 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
502 ep_desc = &iface_desc->endpoint[i].desc;
504 if (usb_endpoint_is_bulk_in(ep_desc)) {
506 devpriv->ep_rx = ep_desc;
510 if (usb_endpoint_is_bulk_out(ep_desc)) {
512 devpriv->ep_tx = ep_desc;
517 if (!devpriv->ep_rx || !devpriv->ep_tx)
523 static int ni6501_auto_attach(struct comedi_device *dev,
524 unsigned long context)
526 struct usb_interface *intf = comedi_to_usb_interface(dev);
527 struct ni6501_private *devpriv;
528 struct comedi_subdevice *s;
531 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
535 ret = ni6501_find_endpoints(dev);
539 ret = ni6501_alloc_usb_buffers(dev);
543 sema_init(&devpriv->sem, 1);
544 usb_set_intfdata(intf, devpriv);
546 ret = comedi_alloc_subdevices(dev, 2);
550 /* Digital Input/Output subdevice */
551 s = &dev->subdevices[0];
552 s->type = COMEDI_SUBD_DIO;
553 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
556 s->range_table = &range_digital;
557 s->insn_bits = ni6501_dio_insn_bits;
558 s->insn_config = ni6501_dio_insn_config;
560 /* Counter subdevice */
561 s = &dev->subdevices[1];
562 s->type = COMEDI_SUBD_COUNTER;
563 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
565 s->maxdata = 0xffffffff;
566 s->insn_read = ni6501_cnt_insn_read;
567 s->insn_write = ni6501_cnt_insn_write;
568 s->insn_config = ni6501_cnt_insn_config;
573 static void ni6501_detach(struct comedi_device *dev)
575 struct usb_interface *intf = comedi_to_usb_interface(dev);
576 struct ni6501_private *devpriv = dev->private;
583 usb_set_intfdata(intf, NULL);
585 kfree(devpriv->usb_rx_buf);
586 kfree(devpriv->usb_tx_buf);
591 static struct comedi_driver ni6501_driver = {
592 .module = THIS_MODULE,
593 .driver_name = "ni6501",
594 .auto_attach = ni6501_auto_attach,
595 .detach = ni6501_detach,
598 static int ni6501_usb_probe(struct usb_interface *intf,
599 const struct usb_device_id *id)
601 return comedi_usb_auto_config(intf, &ni6501_driver, id->driver_info);
604 static const struct usb_device_id ni6501_usb_table[] = {
605 { USB_DEVICE(0x3923, 0x718a) },
608 MODULE_DEVICE_TABLE(usb, ni6501_usb_table);
610 static struct usb_driver ni6501_usb_driver = {
612 .id_table = ni6501_usb_table,
613 .probe = ni6501_usb_probe,
614 .disconnect = comedi_usb_auto_unconfig,
616 module_comedi_usb_driver(ni6501_driver, ni6501_usb_driver);
618 MODULE_AUTHOR("Luca Ellero");
619 MODULE_DESCRIPTION("Comedi driver for National Instruments USB-6501");
620 MODULE_LICENSE("GPL");