1 // SPDX-License-Identifier: GPL-2.0+
3 * comedi/drivers/ni_usb6501.c
4 * Comedi driver for National Instruments USB-6501
6 * COMEDI - Linux Control and Measurement Device Interface
7 * Copyright (C) 2014 Luca Ellero <luca.ellero@brickedbrain.com>
12 * Description: National Instruments USB-6501 module
13 * Devices: [National Instruments] USB-6501 (ni_usb6501)
14 * Author: Luca Ellero <luca.ellero@brickedbrain.com>
19 * Configuration Options:
24 * NI-6501 - USB PROTOCOL DESCRIPTION
26 * Every command is composed by two USB packets:
30 * Every packet is at least 12 bytes long, here is the meaning of
31 * every field (all values are hex):
36 * byte 3 is the total packet length
39 * byte 5 is the total packet length - 4
41 * byte 7 is the command
43 * byte 8 is 02 (request) or 00 (response)
44 * byte 9 is 00 (response) or 10 (port request) or 20 (counter request)
45 * byte 10 is always 00
46 * byte 11 is 00 (request) or 02 (response)
51 * REQ: 00 01 00 10 00 0C 01 0E 02 10 00 00 00 03 <PORT> 00
52 * RES: 00 01 00 10 00 0C 01 00 00 00 00 02 00 03 <BMAP> 00
55 * REQ: 00 01 00 14 00 10 01 0F 02 10 00 00 00 03 <PORT> 00 03 <BMAP> 00 00
56 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
58 * CMD: 0x12 SET_PORT_DIR (0 = input, 1 = output)
59 * REQ: 00 01 00 18 00 14 01 12 02 10 00 00
60 * 00 05 <PORT 0> <PORT 1> <PORT 2> 00 05 00 00 00 00 00
61 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
65 * CMD 0x9: START_COUNTER
66 * REQ: 00 01 00 0C 00 08 01 09 02 20 00 00
67 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
69 * CMD 0xC: STOP_COUNTER
70 * REQ: 00 01 00 0C 00 08 01 0C 02 20 00 00
71 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
73 * CMD 0xE: READ_COUNTER
74 * REQ: 00 01 00 0C 00 08 01 0E 02 20 00 00
75 * RES: 00 01 00 10 00 0C 01 00 00 00 00 02 <u32 counter value, Big Endian>
77 * CMD 0xF: WRITE_COUNTER
78 * REQ: 00 01 00 10 00 0C 01 0F 02 20 00 00 <u32 counter value, Big Endian>
79 * RES: 00 01 00 0C 00 08 01 00 00 00 00 02
82 * Please visit https://www.brickedbrain.com if you need
83 * additional information or have any questions.
87 #include <linux/kernel.h>
88 #include <linux/module.h>
89 #include <linux/slab.h>
91 #include "../comedi_usb.h"
93 #define NI6501_TIMEOUT 1000
95 /* Port request packets */
96 static const u8 READ_PORT_REQUEST[] = {0x00, 0x01, 0x00, 0x10,
97 0x00, 0x0C, 0x01, 0x0E,
98 0x02, 0x10, 0x00, 0x00,
99 0x00, 0x03, 0x00, 0x00};
101 static const u8 WRITE_PORT_REQUEST[] = {0x00, 0x01, 0x00, 0x14,
102 0x00, 0x10, 0x01, 0x0F,
103 0x02, 0x10, 0x00, 0x00,
104 0x00, 0x03, 0x00, 0x00,
105 0x03, 0x00, 0x00, 0x00};
107 static const u8 SET_PORT_DIR_REQUEST[] = {0x00, 0x01, 0x00, 0x18,
108 0x00, 0x14, 0x01, 0x12,
109 0x02, 0x10, 0x00, 0x00,
110 0x00, 0x05, 0x00, 0x00,
111 0x00, 0x00, 0x05, 0x00,
112 0x00, 0x00, 0x00, 0x00};
114 /* Counter request packets */
115 static const u8 START_COUNTER_REQUEST[] = {0x00, 0x01, 0x00, 0x0C,
116 0x00, 0x08, 0x01, 0x09,
117 0x02, 0x20, 0x00, 0x00};
119 static const u8 STOP_COUNTER_REQUEST[] = {0x00, 0x01, 0x00, 0x0C,
120 0x00, 0x08, 0x01, 0x0C,
121 0x02, 0x20, 0x00, 0x00};
123 static const u8 READ_COUNTER_REQUEST[] = {0x00, 0x01, 0x00, 0x0C,
124 0x00, 0x08, 0x01, 0x0E,
125 0x02, 0x20, 0x00, 0x00};
127 static const u8 WRITE_COUNTER_REQUEST[] = {0x00, 0x01, 0x00, 0x10,
128 0x00, 0x0C, 0x01, 0x0F,
129 0x02, 0x20, 0x00, 0x00,
130 0x00, 0x00, 0x00, 0x00};
132 /* Response packets */
133 static const u8 GENERIC_RESPONSE[] = {0x00, 0x01, 0x00, 0x0C,
134 0x00, 0x08, 0x01, 0x00,
135 0x00, 0x00, 0x00, 0x02};
137 static const u8 READ_PORT_RESPONSE[] = {0x00, 0x01, 0x00, 0x10,
138 0x00, 0x0C, 0x01, 0x00,
139 0x00, 0x00, 0x00, 0x02,
140 0x00, 0x03, 0x00, 0x00};
142 static const u8 READ_COUNTER_RESPONSE[] = {0x00, 0x01, 0x00, 0x10,
143 0x00, 0x0C, 0x01, 0x00,
144 0x00, 0x00, 0x00, 0x02,
145 0x00, 0x00, 0x00, 0x00};
157 struct ni6501_private {
158 struct usb_endpoint_descriptor *ep_rx;
159 struct usb_endpoint_descriptor *ep_tx;
165 static int ni6501_port_command(struct comedi_device *dev, int command,
166 unsigned int val, u8 *bitmap)
168 struct usb_device *usb = comedi_to_usb_dev(dev);
169 struct ni6501_private *devpriv = dev->private;
170 int request_size, response_size;
171 u8 *tx = devpriv->usb_tx_buf;
174 if (command != SET_PORT_DIR && !bitmap)
177 mutex_lock(&devpriv->mut);
181 request_size = sizeof(READ_PORT_REQUEST);
182 response_size = sizeof(READ_PORT_RESPONSE);
183 memcpy(tx, READ_PORT_REQUEST, request_size);
187 request_size = sizeof(WRITE_PORT_REQUEST);
188 response_size = sizeof(GENERIC_RESPONSE);
189 memcpy(tx, WRITE_PORT_REQUEST, request_size);
194 request_size = sizeof(SET_PORT_DIR_REQUEST);
195 response_size = sizeof(GENERIC_RESPONSE);
196 memcpy(tx, SET_PORT_DIR_REQUEST, request_size);
198 tx[15] = (val >> 8) & 0xff;
199 tx[16] = (val >> 16) & 0xff;
206 ret = usb_bulk_msg(usb,
208 devpriv->ep_tx->bEndpointAddress),
216 ret = usb_bulk_msg(usb,
218 devpriv->ep_rx->bEndpointAddress),
226 /* Check if results are valid */
228 if (command == READ_PORT) {
229 *bitmap = devpriv->usb_rx_buf[14];
230 /* mask bitmap for comparing */
231 devpriv->usb_rx_buf[14] = 0x00;
233 if (memcmp(devpriv->usb_rx_buf, READ_PORT_RESPONSE,
234 sizeof(READ_PORT_RESPONSE))) {
237 } else if (memcmp(devpriv->usb_rx_buf, GENERIC_RESPONSE,
238 sizeof(GENERIC_RESPONSE))) {
242 mutex_unlock(&devpriv->mut);
247 static int ni6501_counter_command(struct comedi_device *dev, int command,
250 struct usb_device *usb = comedi_to_usb_dev(dev);
251 struct ni6501_private *devpriv = dev->private;
252 int request_size, response_size;
253 u8 *tx = devpriv->usb_tx_buf;
256 if ((command == READ_COUNTER || command == WRITE_COUNTER) && !val)
259 mutex_lock(&devpriv->mut);
263 request_size = sizeof(START_COUNTER_REQUEST);
264 response_size = sizeof(GENERIC_RESPONSE);
265 memcpy(tx, START_COUNTER_REQUEST, request_size);
268 request_size = sizeof(STOP_COUNTER_REQUEST);
269 response_size = sizeof(GENERIC_RESPONSE);
270 memcpy(tx, STOP_COUNTER_REQUEST, request_size);
273 request_size = sizeof(READ_COUNTER_REQUEST);
274 response_size = sizeof(READ_COUNTER_RESPONSE);
275 memcpy(tx, READ_COUNTER_REQUEST, request_size);
278 request_size = sizeof(WRITE_COUNTER_REQUEST);
279 response_size = sizeof(GENERIC_RESPONSE);
280 memcpy(tx, WRITE_COUNTER_REQUEST, request_size);
281 /* Setup tx packet: bytes 12,13,14,15 hold the */
282 /* u32 counter value (Big Endian) */
283 *((__be32 *)&tx[12]) = cpu_to_be32(*val);
290 ret = usb_bulk_msg(usb,
292 devpriv->ep_tx->bEndpointAddress),
300 ret = usb_bulk_msg(usb,
302 devpriv->ep_rx->bEndpointAddress),
310 /* Check if results are valid */
312 if (command == READ_COUNTER) {
315 /* Read counter value: bytes 12,13,14,15 of rx packet */
316 /* hold the u32 counter value (Big Endian) */
317 *val = be32_to_cpu(*((__be32 *)&devpriv->usb_rx_buf[12]));
319 /* mask counter value for comparing */
320 for (i = 12; i < sizeof(READ_COUNTER_RESPONSE); ++i)
321 devpriv->usb_rx_buf[i] = 0x00;
323 if (memcmp(devpriv->usb_rx_buf, READ_COUNTER_RESPONSE,
324 sizeof(READ_COUNTER_RESPONSE))) {
327 } else if (memcmp(devpriv->usb_rx_buf, GENERIC_RESPONSE,
328 sizeof(GENERIC_RESPONSE))) {
332 mutex_unlock(&devpriv->mut);
337 static int ni6501_dio_insn_config(struct comedi_device *dev,
338 struct comedi_subdevice *s,
339 struct comedi_insn *insn,
344 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
348 ret = ni6501_port_command(dev, SET_PORT_DIR, s->io_bits, NULL);
355 static int ni6501_dio_insn_bits(struct comedi_device *dev,
356 struct comedi_subdevice *s,
357 struct comedi_insn *insn,
365 mask = comedi_dio_update_state(s, data);
367 for (port = 0; port < 3; port++) {
368 if (mask & (0xFF << port * 8)) {
369 bitmap = (s->state >> port * 8) & 0xFF;
370 ret = ni6501_port_command(dev, WRITE_PORT,
379 for (port = 0; port < 3; port++) {
380 ret = ni6501_port_command(dev, READ_PORT, port, &bitmap);
383 data[1] |= bitmap << port * 8;
389 static int ni6501_cnt_insn_config(struct comedi_device *dev,
390 struct comedi_subdevice *s,
391 struct comedi_insn *insn,
398 case INSN_CONFIG_ARM:
399 ret = ni6501_counter_command(dev, START_COUNTER, NULL);
401 case INSN_CONFIG_DISARM:
402 ret = ni6501_counter_command(dev, STOP_COUNTER, NULL);
404 case INSN_CONFIG_RESET:
405 ret = ni6501_counter_command(dev, STOP_COUNTER, NULL);
408 ret = ni6501_counter_command(dev, WRITE_COUNTER, &val);
414 return ret ? ret : insn->n;
417 static int ni6501_cnt_insn_read(struct comedi_device *dev,
418 struct comedi_subdevice *s,
419 struct comedi_insn *insn,
426 for (i = 0; i < insn->n; i++) {
427 ret = ni6501_counter_command(dev, READ_COUNTER, &val);
436 static int ni6501_cnt_insn_write(struct comedi_device *dev,
437 struct comedi_subdevice *s,
438 struct comedi_insn *insn,
444 u32 val = data[insn->n - 1];
446 ret = ni6501_counter_command(dev, WRITE_COUNTER, &val);
454 static int ni6501_alloc_usb_buffers(struct comedi_device *dev)
456 struct ni6501_private *devpriv = dev->private;
459 size = usb_endpoint_maxp(devpriv->ep_rx);
460 devpriv->usb_rx_buf = kzalloc(size, GFP_KERNEL);
461 if (!devpriv->usb_rx_buf)
464 size = usb_endpoint_maxp(devpriv->ep_tx);
465 devpriv->usb_tx_buf = kzalloc(size, GFP_KERNEL);
466 if (!devpriv->usb_tx_buf)
472 static int ni6501_find_endpoints(struct comedi_device *dev)
474 struct usb_interface *intf = comedi_to_usb_interface(dev);
475 struct ni6501_private *devpriv = dev->private;
476 struct usb_host_interface *iface_desc = intf->cur_altsetting;
477 struct usb_endpoint_descriptor *ep_desc;
480 if (iface_desc->desc.bNumEndpoints != 2) {
481 dev_err(dev->class_dev, "Wrong number of endpoints\n");
485 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
486 ep_desc = &iface_desc->endpoint[i].desc;
488 if (usb_endpoint_is_bulk_in(ep_desc)) {
490 devpriv->ep_rx = ep_desc;
494 if (usb_endpoint_is_bulk_out(ep_desc)) {
496 devpriv->ep_tx = ep_desc;
501 if (!devpriv->ep_rx || !devpriv->ep_tx)
507 static int ni6501_auto_attach(struct comedi_device *dev,
508 unsigned long context)
510 struct usb_interface *intf = comedi_to_usb_interface(dev);
511 struct ni6501_private *devpriv;
512 struct comedi_subdevice *s;
515 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
519 mutex_init(&devpriv->mut);
520 usb_set_intfdata(intf, devpriv);
522 ret = ni6501_find_endpoints(dev);
526 ret = ni6501_alloc_usb_buffers(dev);
530 ret = comedi_alloc_subdevices(dev, 2);
534 /* Digital Input/Output subdevice */
535 s = &dev->subdevices[0];
536 s->type = COMEDI_SUBD_DIO;
537 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
540 s->range_table = &range_digital;
541 s->insn_bits = ni6501_dio_insn_bits;
542 s->insn_config = ni6501_dio_insn_config;
544 /* Counter subdevice */
545 s = &dev->subdevices[1];
546 s->type = COMEDI_SUBD_COUNTER;
547 s->subdev_flags = SDF_READABLE | SDF_WRITABLE | SDF_LSAMPL;
549 s->maxdata = 0xffffffff;
550 s->insn_read = ni6501_cnt_insn_read;
551 s->insn_write = ni6501_cnt_insn_write;
552 s->insn_config = ni6501_cnt_insn_config;
557 static void ni6501_detach(struct comedi_device *dev)
559 struct usb_interface *intf = comedi_to_usb_interface(dev);
560 struct ni6501_private *devpriv = dev->private;
565 mutex_destroy(&devpriv->mut);
567 usb_set_intfdata(intf, NULL);
569 kfree(devpriv->usb_rx_buf);
570 kfree(devpriv->usb_tx_buf);
573 static struct comedi_driver ni6501_driver = {
574 .module = THIS_MODULE,
575 .driver_name = "ni6501",
576 .auto_attach = ni6501_auto_attach,
577 .detach = ni6501_detach,
580 static int ni6501_usb_probe(struct usb_interface *intf,
581 const struct usb_device_id *id)
583 return comedi_usb_auto_config(intf, &ni6501_driver, id->driver_info);
586 static const struct usb_device_id ni6501_usb_table[] = {
587 { USB_DEVICE(0x3923, 0x718a) },
590 MODULE_DEVICE_TABLE(usb, ni6501_usb_table);
592 static struct usb_driver ni6501_usb_driver = {
594 .id_table = ni6501_usb_table,
595 .probe = ni6501_usb_probe,
596 .disconnect = comedi_usb_auto_unconfig,
598 module_comedi_usb_driver(ni6501_driver, ni6501_usb_driver);
600 MODULE_AUTHOR("Luca Ellero");
601 MODULE_DESCRIPTION("Comedi driver for National Instruments USB-6501");
602 MODULE_LICENSE("GPL");