Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[linux-2.6.git] / drivers / usb / class / usbtmc.c
blob609dbc2f7151d52a08cf2f7b62d229637491ce55
1 /**
2 * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
4 * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
5 * Copyright (C) 2008 Novell, Inc.
6 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (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.
18 * The GNU General Public License is available at
19 * http://www.gnu.org/copyleft/gpl.html.
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/fs.h>
26 #include <linux/uaccess.h>
27 #include <linux/kref.h>
28 #include <linux/slab.h>
29 #include <linux/mutex.h>
30 #include <linux/usb.h>
31 #include <linux/usb/tmc.h>
34 #define RIGOL 1
35 #define USBTMC_HEADER_SIZE 12
36 #define USBTMC_MINOR_BASE 176
39 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
40 * large as wMaxPacketSize (which is usually 512 bytes).
42 #define USBTMC_SIZE_IOBUFFER 2048
44 /* Default USB timeout (in milliseconds) */
45 #define USBTMC_TIMEOUT 5000
48 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
49 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
50 * packet is never read.
52 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
54 static const struct usb_device_id usbtmc_devices[] = {
55 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
56 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
57 { 0, } /* terminating entry */
59 MODULE_DEVICE_TABLE(usb, usbtmc_devices);
62 * This structure is the capabilities for the device
63 * See section 4.2.1.8 of the USBTMC specification,
64 * and section 4.2.2 of the USBTMC usb488 subclass
65 * specification for details.
67 struct usbtmc_dev_capabilities {
68 __u8 interface_capabilities;
69 __u8 device_capabilities;
70 __u8 usb488_interface_capabilities;
71 __u8 usb488_device_capabilities;
74 /* This structure holds private data for each USBTMC device. One copy is
75 * allocated for each USBTMC device in the driver's probe function.
77 struct usbtmc_device_data {
78 const struct usb_device_id *id;
79 struct usb_device *usb_dev;
80 struct usb_interface *intf;
82 unsigned int bulk_in;
83 unsigned int bulk_out;
85 u8 bTag;
86 u8 bTag_last_write; /* needed for abort */
87 u8 bTag_last_read; /* needed for abort */
89 u8 rigol_quirk;
91 /* attributes from the USB TMC spec for this device */
92 u8 TermChar;
93 bool TermCharEnabled;
94 bool auto_abort;
96 bool zombie; /* fd of disconnected device */
98 struct usbtmc_dev_capabilities capabilities;
99 struct kref kref;
100 struct mutex io_mutex; /* only one i/o function running at a time */
102 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
104 struct usbtmc_ID_rigol_quirk {
105 __u16 idVendor;
106 __u16 idProduct;
109 static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
110 { 0x1ab1, 0x0588 },
111 { 0, 0 }
114 /* Forward declarations */
115 static struct usb_driver usbtmc_driver;
117 static void usbtmc_delete(struct kref *kref)
119 struct usbtmc_device_data *data = to_usbtmc_data(kref);
121 usb_put_dev(data->usb_dev);
122 kfree(data);
125 static int usbtmc_open(struct inode *inode, struct file *filp)
127 struct usb_interface *intf;
128 struct usbtmc_device_data *data;
129 int retval = 0;
131 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
132 if (!intf) {
133 printk(KERN_ERR KBUILD_MODNAME
134 ": can not find device for minor %d", iminor(inode));
135 retval = -ENODEV;
136 goto exit;
139 data = usb_get_intfdata(intf);
140 kref_get(&data->kref);
142 /* Store pointer in file structure's private data field */
143 filp->private_data = data;
145 exit:
146 return retval;
149 static int usbtmc_release(struct inode *inode, struct file *file)
151 struct usbtmc_device_data *data = file->private_data;
153 kref_put(&data->kref, usbtmc_delete);
154 return 0;
157 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
159 u8 *buffer;
160 struct device *dev;
161 int rv;
162 int n;
163 int actual;
164 struct usb_host_interface *current_setting;
165 int max_size;
167 dev = &data->intf->dev;
168 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
169 if (!buffer)
170 return -ENOMEM;
172 rv = usb_control_msg(data->usb_dev,
173 usb_rcvctrlpipe(data->usb_dev, 0),
174 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
175 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
176 data->bTag_last_read, data->bulk_in,
177 buffer, 2, USBTMC_TIMEOUT);
179 if (rv < 0) {
180 dev_err(dev, "usb_control_msg returned %d\n", rv);
181 goto exit;
184 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
186 if (buffer[0] == USBTMC_STATUS_FAILED) {
187 rv = 0;
188 goto exit;
191 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
192 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
193 buffer[0]);
194 rv = -EPERM;
195 goto exit;
198 max_size = 0;
199 current_setting = data->intf->cur_altsetting;
200 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
201 if (current_setting->endpoint[n].desc.bEndpointAddress ==
202 data->bulk_in)
203 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
205 if (max_size == 0) {
206 dev_err(dev, "Couldn't get wMaxPacketSize\n");
207 rv = -EPERM;
208 goto exit;
211 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
213 n = 0;
215 do {
216 dev_dbg(dev, "Reading from bulk in EP\n");
218 rv = usb_bulk_msg(data->usb_dev,
219 usb_rcvbulkpipe(data->usb_dev,
220 data->bulk_in),
221 buffer, USBTMC_SIZE_IOBUFFER,
222 &actual, USBTMC_TIMEOUT);
224 n++;
226 if (rv < 0) {
227 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
228 goto exit;
230 } while ((actual == max_size) &&
231 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
233 if (actual == max_size) {
234 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
235 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
236 rv = -EPERM;
237 goto exit;
240 n = 0;
242 usbtmc_abort_bulk_in_status:
243 rv = usb_control_msg(data->usb_dev,
244 usb_rcvctrlpipe(data->usb_dev, 0),
245 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
246 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
247 0, data->bulk_in, buffer, 0x08,
248 USBTMC_TIMEOUT);
250 if (rv < 0) {
251 dev_err(dev, "usb_control_msg returned %d\n", rv);
252 goto exit;
255 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
257 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
258 rv = 0;
259 goto exit;
262 if (buffer[0] != USBTMC_STATUS_PENDING) {
263 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
264 rv = -EPERM;
265 goto exit;
268 if (buffer[1] == 1)
269 do {
270 dev_dbg(dev, "Reading from bulk in EP\n");
272 rv = usb_bulk_msg(data->usb_dev,
273 usb_rcvbulkpipe(data->usb_dev,
274 data->bulk_in),
275 buffer, USBTMC_SIZE_IOBUFFER,
276 &actual, USBTMC_TIMEOUT);
278 n++;
280 if (rv < 0) {
281 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
282 goto exit;
284 } while ((actual == max_size) &&
285 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
287 if (actual == max_size) {
288 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
289 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
290 rv = -EPERM;
291 goto exit;
294 goto usbtmc_abort_bulk_in_status;
296 exit:
297 kfree(buffer);
298 return rv;
302 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
304 struct device *dev;
305 u8 *buffer;
306 int rv;
307 int n;
309 dev = &data->intf->dev;
311 buffer = kmalloc(8, GFP_KERNEL);
312 if (!buffer)
313 return -ENOMEM;
315 rv = usb_control_msg(data->usb_dev,
316 usb_rcvctrlpipe(data->usb_dev, 0),
317 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
318 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
319 data->bTag_last_write, data->bulk_out,
320 buffer, 2, USBTMC_TIMEOUT);
322 if (rv < 0) {
323 dev_err(dev, "usb_control_msg returned %d\n", rv);
324 goto exit;
327 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
329 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
330 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
331 buffer[0]);
332 rv = -EPERM;
333 goto exit;
336 n = 0;
338 usbtmc_abort_bulk_out_check_status:
339 rv = usb_control_msg(data->usb_dev,
340 usb_rcvctrlpipe(data->usb_dev, 0),
341 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
342 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
343 0, data->bulk_out, buffer, 0x08,
344 USBTMC_TIMEOUT);
345 n++;
346 if (rv < 0) {
347 dev_err(dev, "usb_control_msg returned %d\n", rv);
348 goto exit;
351 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
353 if (buffer[0] == USBTMC_STATUS_SUCCESS)
354 goto usbtmc_abort_bulk_out_clear_halt;
356 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
357 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
358 goto usbtmc_abort_bulk_out_check_status;
360 rv = -EPERM;
361 goto exit;
363 usbtmc_abort_bulk_out_clear_halt:
364 rv = usb_clear_halt(data->usb_dev,
365 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
367 if (rv < 0) {
368 dev_err(dev, "usb_control_msg returned %d\n", rv);
369 goto exit;
371 rv = 0;
373 exit:
374 kfree(buffer);
375 return rv;
379 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
380 * @transfer_size: number of bytes to request from the device.
382 * See the USBTMC specification, Table 4.
384 * Also updates bTag_last_write.
386 static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
388 int retval;
389 u8 buffer[USBTMC_HEADER_SIZE];
390 int actual;
392 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
393 * Refer to class specs for details
395 buffer[0] = 2;
396 buffer[1] = data->bTag;
397 buffer[2] = ~(data->bTag);
398 buffer[3] = 0; /* Reserved */
399 buffer[4] = (transfer_size) & 255;
400 buffer[5] = ((transfer_size) >> 8) & 255;
401 buffer[6] = ((transfer_size) >> 16) & 255;
402 buffer[7] = ((transfer_size) >> 24) & 255;
403 buffer[8] = data->TermCharEnabled * 2;
404 /* Use term character? */
405 buffer[9] = data->TermChar;
406 buffer[10] = 0; /* Reserved */
407 buffer[11] = 0; /* Reserved */
409 /* Send bulk URB */
410 retval = usb_bulk_msg(data->usb_dev,
411 usb_sndbulkpipe(data->usb_dev,
412 data->bulk_out),
413 buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
415 /* Store bTag (in case we need to abort) */
416 data->bTag_last_write = data->bTag;
418 /* Increment bTag -- and increment again if zero */
419 data->bTag++;
420 if (!data->bTag)
421 (data->bTag)++;
423 if (retval < 0) {
424 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
425 return retval;
428 return 0;
431 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
432 size_t count, loff_t *f_pos)
434 struct usbtmc_device_data *data;
435 struct device *dev;
436 u32 n_characters;
437 u8 *buffer;
438 int actual;
439 size_t done;
440 size_t remaining;
441 int retval;
442 size_t this_part;
444 /* Get pointer to private data structure */
445 data = filp->private_data;
446 dev = &data->intf->dev;
448 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
449 if (!buffer)
450 return -ENOMEM;
452 mutex_lock(&data->io_mutex);
453 if (data->zombie) {
454 retval = -ENODEV;
455 goto exit;
458 if (data->rigol_quirk) {
459 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
461 retval = send_request_dev_dep_msg_in(data, count);
463 if (retval < 0) {
464 if (data->auto_abort)
465 usbtmc_ioctl_abort_bulk_out(data);
466 goto exit;
470 /* Loop until we have fetched everything we requested */
471 remaining = count;
472 this_part = remaining;
473 done = 0;
475 while (remaining > 0) {
476 if (!(data->rigol_quirk)) {
477 dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
479 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
480 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
481 else
482 this_part = remaining;
484 retval = send_request_dev_dep_msg_in(data, this_part);
485 if (retval < 0) {
486 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
487 if (data->auto_abort)
488 usbtmc_ioctl_abort_bulk_out(data);
489 goto exit;
493 /* Send bulk URB */
494 retval = usb_bulk_msg(data->usb_dev,
495 usb_rcvbulkpipe(data->usb_dev,
496 data->bulk_in),
497 buffer, USBTMC_SIZE_IOBUFFER, &actual,
498 USBTMC_TIMEOUT);
500 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
502 /* Store bTag (in case we need to abort) */
503 data->bTag_last_read = data->bTag;
505 if (retval < 0) {
506 dev_dbg(dev, "Unable to read data, error %d\n", retval);
507 if (data->auto_abort)
508 usbtmc_ioctl_abort_bulk_in(data);
509 goto exit;
512 /* Parse header in first packet */
513 if ((done == 0) || (!(data->rigol_quirk))) {
514 /* Sanity checks for the header */
515 if (actual < USBTMC_HEADER_SIZE) {
516 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
517 if (data->auto_abort)
518 usbtmc_ioctl_abort_bulk_in(data);
519 goto exit;
522 if (buffer[0] != 2) {
523 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
524 if (data->auto_abort)
525 usbtmc_ioctl_abort_bulk_in(data);
526 goto exit;
529 if (buffer[1] != data->bTag_last_write) {
530 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
531 if (data->auto_abort)
532 usbtmc_ioctl_abort_bulk_in(data);
533 goto exit;
536 /* How many characters did the instrument send? */
537 n_characters = buffer[4] +
538 (buffer[5] << 8) +
539 (buffer[6] << 16) +
540 (buffer[7] << 24);
542 if (n_characters > this_part) {
543 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
544 if (data->auto_abort)
545 usbtmc_ioctl_abort_bulk_in(data);
546 goto exit;
549 /* Remove the USBTMC header */
550 actual -= USBTMC_HEADER_SIZE;
552 /* Check if the message is smaller than requested */
553 if (data->rigol_quirk) {
554 if (remaining > n_characters)
555 remaining = n_characters;
556 /* Remove padding if it exists */
557 if (actual > remaining)
558 actual = remaining;
560 else {
561 if (this_part > n_characters)
562 this_part = n_characters;
563 /* Remove padding if it exists */
564 if (actual > this_part)
565 actual = this_part;
568 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
570 remaining -= actual;
572 /* Terminate if end-of-message bit received from device */
573 if ((buffer[8] & 0x01) && (actual >= n_characters))
574 remaining = 0;
576 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
579 /* Copy buffer to user space */
580 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
581 /* There must have been an addressing problem */
582 retval = -EFAULT;
583 goto exit;
585 done += actual;
587 else {
588 if (actual > remaining)
589 actual = remaining;
591 remaining -= actual;
593 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
595 /* Copy buffer to user space */
596 if (copy_to_user(buf + done, buffer, actual)) {
597 /* There must have been an addressing problem */
598 retval = -EFAULT;
599 goto exit;
601 done += actual;
605 /* Update file position value */
606 *f_pos = *f_pos + done;
607 retval = done;
609 exit:
610 mutex_unlock(&data->io_mutex);
611 kfree(buffer);
612 return retval;
615 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
616 size_t count, loff_t *f_pos)
618 struct usbtmc_device_data *data;
619 u8 *buffer;
620 int retval;
621 int actual;
622 unsigned long int n_bytes;
623 int remaining;
624 int done;
625 int this_part;
627 data = filp->private_data;
629 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
630 if (!buffer)
631 return -ENOMEM;
633 mutex_lock(&data->io_mutex);
634 if (data->zombie) {
635 retval = -ENODEV;
636 goto exit;
639 remaining = count;
640 done = 0;
642 while (remaining > 0) {
643 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
644 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
645 buffer[8] = 0;
646 } else {
647 this_part = remaining;
648 buffer[8] = 1;
651 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
652 buffer[0] = 1;
653 buffer[1] = data->bTag;
654 buffer[2] = ~(data->bTag);
655 buffer[3] = 0; /* Reserved */
656 buffer[4] = this_part & 255;
657 buffer[5] = (this_part >> 8) & 255;
658 buffer[6] = (this_part >> 16) & 255;
659 buffer[7] = (this_part >> 24) & 255;
660 /* buffer[8] is set above... */
661 buffer[9] = 0; /* Reserved */
662 buffer[10] = 0; /* Reserved */
663 buffer[11] = 0; /* Reserved */
665 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
666 retval = -EFAULT;
667 goto exit;
670 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
671 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
673 do {
674 retval = usb_bulk_msg(data->usb_dev,
675 usb_sndbulkpipe(data->usb_dev,
676 data->bulk_out),
677 buffer, n_bytes,
678 &actual, USBTMC_TIMEOUT);
679 if (retval != 0)
680 break;
681 n_bytes -= actual;
682 } while (n_bytes);
684 data->bTag_last_write = data->bTag;
685 data->bTag++;
687 if (!data->bTag)
688 data->bTag++;
690 if (retval < 0) {
691 dev_err(&data->intf->dev,
692 "Unable to send data, error %d\n", retval);
693 if (data->auto_abort)
694 usbtmc_ioctl_abort_bulk_out(data);
695 goto exit;
698 remaining -= this_part;
699 done += this_part;
702 retval = count;
703 exit:
704 mutex_unlock(&data->io_mutex);
705 kfree(buffer);
706 return retval;
709 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
711 struct usb_host_interface *current_setting;
712 struct usb_endpoint_descriptor *desc;
713 struct device *dev;
714 u8 *buffer;
715 int rv;
716 int n;
717 int actual;
718 int max_size;
720 dev = &data->intf->dev;
722 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
724 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
725 if (!buffer)
726 return -ENOMEM;
728 rv = usb_control_msg(data->usb_dev,
729 usb_rcvctrlpipe(data->usb_dev, 0),
730 USBTMC_REQUEST_INITIATE_CLEAR,
731 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
732 0, 0, buffer, 1, USBTMC_TIMEOUT);
733 if (rv < 0) {
734 dev_err(dev, "usb_control_msg returned %d\n", rv);
735 goto exit;
738 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
740 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
741 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
742 rv = -EPERM;
743 goto exit;
746 max_size = 0;
747 current_setting = data->intf->cur_altsetting;
748 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
749 desc = &current_setting->endpoint[n].desc;
750 if (desc->bEndpointAddress == data->bulk_in)
751 max_size = usb_endpoint_maxp(desc);
754 if (max_size == 0) {
755 dev_err(dev, "Couldn't get wMaxPacketSize\n");
756 rv = -EPERM;
757 goto exit;
760 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
762 n = 0;
764 usbtmc_clear_check_status:
766 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
768 rv = usb_control_msg(data->usb_dev,
769 usb_rcvctrlpipe(data->usb_dev, 0),
770 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
771 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
772 0, 0, buffer, 2, USBTMC_TIMEOUT);
773 if (rv < 0) {
774 dev_err(dev, "usb_control_msg returned %d\n", rv);
775 goto exit;
778 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
780 if (buffer[0] == USBTMC_STATUS_SUCCESS)
781 goto usbtmc_clear_bulk_out_halt;
783 if (buffer[0] != USBTMC_STATUS_PENDING) {
784 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
785 rv = -EPERM;
786 goto exit;
789 if (buffer[1] == 1)
790 do {
791 dev_dbg(dev, "Reading from bulk in EP\n");
793 rv = usb_bulk_msg(data->usb_dev,
794 usb_rcvbulkpipe(data->usb_dev,
795 data->bulk_in),
796 buffer, USBTMC_SIZE_IOBUFFER,
797 &actual, USBTMC_TIMEOUT);
798 n++;
800 if (rv < 0) {
801 dev_err(dev, "usb_control_msg returned %d\n",
802 rv);
803 goto exit;
805 } while ((actual == max_size) &&
806 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
808 if (actual == max_size) {
809 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
810 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
811 rv = -EPERM;
812 goto exit;
815 goto usbtmc_clear_check_status;
817 usbtmc_clear_bulk_out_halt:
819 rv = usb_clear_halt(data->usb_dev,
820 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
821 if (rv < 0) {
822 dev_err(dev, "usb_control_msg returned %d\n", rv);
823 goto exit;
825 rv = 0;
827 exit:
828 kfree(buffer);
829 return rv;
832 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
834 int rv;
836 rv = usb_clear_halt(data->usb_dev,
837 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
839 if (rv < 0) {
840 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
841 rv);
842 return rv;
844 return 0;
847 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
849 int rv;
851 rv = usb_clear_halt(data->usb_dev,
852 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
854 if (rv < 0) {
855 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
856 rv);
857 return rv;
859 return 0;
862 static int get_capabilities(struct usbtmc_device_data *data)
864 struct device *dev = &data->usb_dev->dev;
865 char *buffer;
866 int rv = 0;
868 buffer = kmalloc(0x18, GFP_KERNEL);
869 if (!buffer)
870 return -ENOMEM;
872 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
873 USBTMC_REQUEST_GET_CAPABILITIES,
874 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
875 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
876 if (rv < 0) {
877 dev_err(dev, "usb_control_msg returned %d\n", rv);
878 goto err_out;
881 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
882 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
883 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
884 rv = -EPERM;
885 goto err_out;
887 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
888 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
889 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
890 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
892 data->capabilities.interface_capabilities = buffer[4];
893 data->capabilities.device_capabilities = buffer[5];
894 data->capabilities.usb488_interface_capabilities = buffer[14];
895 data->capabilities.usb488_device_capabilities = buffer[15];
896 rv = 0;
898 err_out:
899 kfree(buffer);
900 return rv;
903 #define capability_attribute(name) \
904 static ssize_t show_##name(struct device *dev, \
905 struct device_attribute *attr, char *buf) \
907 struct usb_interface *intf = to_usb_interface(dev); \
908 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
910 return sprintf(buf, "%d\n", data->capabilities.name); \
912 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
914 capability_attribute(interface_capabilities);
915 capability_attribute(device_capabilities);
916 capability_attribute(usb488_interface_capabilities);
917 capability_attribute(usb488_device_capabilities);
919 static struct attribute *capability_attrs[] = {
920 &dev_attr_interface_capabilities.attr,
921 &dev_attr_device_capabilities.attr,
922 &dev_attr_usb488_interface_capabilities.attr,
923 &dev_attr_usb488_device_capabilities.attr,
924 NULL,
927 static struct attribute_group capability_attr_grp = {
928 .attrs = capability_attrs,
931 static ssize_t show_TermChar(struct device *dev,
932 struct device_attribute *attr, char *buf)
934 struct usb_interface *intf = to_usb_interface(dev);
935 struct usbtmc_device_data *data = usb_get_intfdata(intf);
937 return sprintf(buf, "%c\n", data->TermChar);
940 static ssize_t store_TermChar(struct device *dev,
941 struct device_attribute *attr,
942 const char *buf, size_t count)
944 struct usb_interface *intf = to_usb_interface(dev);
945 struct usbtmc_device_data *data = usb_get_intfdata(intf);
947 if (count < 1)
948 return -EINVAL;
949 data->TermChar = buf[0];
950 return count;
952 static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
954 #define data_attribute(name) \
955 static ssize_t show_##name(struct device *dev, \
956 struct device_attribute *attr, char *buf) \
958 struct usb_interface *intf = to_usb_interface(dev); \
959 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
961 return sprintf(buf, "%d\n", data->name); \
963 static ssize_t store_##name(struct device *dev, \
964 struct device_attribute *attr, \
965 const char *buf, size_t count) \
967 struct usb_interface *intf = to_usb_interface(dev); \
968 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
969 ssize_t result; \
970 unsigned val; \
972 result = sscanf(buf, "%u\n", &val); \
973 if (result != 1) \
974 result = -EINVAL; \
975 data->name = val; \
976 if (result < 0) \
977 return result; \
978 else \
979 return count; \
981 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
983 data_attribute(TermCharEnabled);
984 data_attribute(auto_abort);
986 static struct attribute *data_attrs[] = {
987 &dev_attr_TermChar.attr,
988 &dev_attr_TermCharEnabled.attr,
989 &dev_attr_auto_abort.attr,
990 NULL,
993 static struct attribute_group data_attr_grp = {
994 .attrs = data_attrs,
997 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
999 struct device *dev;
1000 u8 *buffer;
1001 int rv;
1003 dev = &data->intf->dev;
1005 buffer = kmalloc(2, GFP_KERNEL);
1006 if (!buffer)
1007 return -ENOMEM;
1009 rv = usb_control_msg(data->usb_dev,
1010 usb_rcvctrlpipe(data->usb_dev, 0),
1011 USBTMC_REQUEST_INDICATOR_PULSE,
1012 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1013 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1015 if (rv < 0) {
1016 dev_err(dev, "usb_control_msg returned %d\n", rv);
1017 goto exit;
1020 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1022 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1023 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1024 rv = -EPERM;
1025 goto exit;
1027 rv = 0;
1029 exit:
1030 kfree(buffer);
1031 return rv;
1034 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1036 struct usbtmc_device_data *data;
1037 int retval = -EBADRQC;
1039 data = file->private_data;
1040 mutex_lock(&data->io_mutex);
1041 if (data->zombie) {
1042 retval = -ENODEV;
1043 goto skip_io_on_zombie;
1046 switch (cmd) {
1047 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1048 retval = usbtmc_ioctl_clear_out_halt(data);
1049 break;
1051 case USBTMC_IOCTL_CLEAR_IN_HALT:
1052 retval = usbtmc_ioctl_clear_in_halt(data);
1053 break;
1055 case USBTMC_IOCTL_INDICATOR_PULSE:
1056 retval = usbtmc_ioctl_indicator_pulse(data);
1057 break;
1059 case USBTMC_IOCTL_CLEAR:
1060 retval = usbtmc_ioctl_clear(data);
1061 break;
1063 case USBTMC_IOCTL_ABORT_BULK_OUT:
1064 retval = usbtmc_ioctl_abort_bulk_out(data);
1065 break;
1067 case USBTMC_IOCTL_ABORT_BULK_IN:
1068 retval = usbtmc_ioctl_abort_bulk_in(data);
1069 break;
1072 skip_io_on_zombie:
1073 mutex_unlock(&data->io_mutex);
1074 return retval;
1077 static const struct file_operations fops = {
1078 .owner = THIS_MODULE,
1079 .read = usbtmc_read,
1080 .write = usbtmc_write,
1081 .open = usbtmc_open,
1082 .release = usbtmc_release,
1083 .unlocked_ioctl = usbtmc_ioctl,
1084 .llseek = default_llseek,
1087 static struct usb_class_driver usbtmc_class = {
1088 .name = "usbtmc%d",
1089 .fops = &fops,
1090 .minor_base = USBTMC_MINOR_BASE,
1094 static int usbtmc_probe(struct usb_interface *intf,
1095 const struct usb_device_id *id)
1097 struct usbtmc_device_data *data;
1098 struct usb_host_interface *iface_desc;
1099 struct usb_endpoint_descriptor *endpoint;
1100 int n;
1101 int retcode;
1103 dev_dbg(&intf->dev, "%s called\n", __func__);
1105 data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
1106 if (!data) {
1107 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
1108 return -ENOMEM;
1111 data->intf = intf;
1112 data->id = id;
1113 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1114 usb_set_intfdata(intf, data);
1115 kref_init(&data->kref);
1116 mutex_init(&data->io_mutex);
1117 data->zombie = 0;
1119 /* Determine if it is a Rigol or not */
1120 data->rigol_quirk = 0;
1121 dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1122 data->usb_dev->descriptor.idVendor,
1123 data->usb_dev->descriptor.idProduct);
1124 for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
1125 if ((usbtmc_id_quirk[n].idVendor == data->usb_dev->descriptor.idVendor) &&
1126 (usbtmc_id_quirk[n].idProduct == data->usb_dev->descriptor.idProduct)) {
1127 dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1128 data->rigol_quirk = 1;
1129 break;
1133 /* Initialize USBTMC bTag and other fields */
1134 data->bTag = 1;
1135 data->TermCharEnabled = 0;
1136 data->TermChar = '\n';
1138 /* USBTMC devices have only one setting, so use that */
1139 iface_desc = data->intf->cur_altsetting;
1141 /* Find bulk in endpoint */
1142 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1143 endpoint = &iface_desc->endpoint[n].desc;
1145 if (usb_endpoint_is_bulk_in(endpoint)) {
1146 data->bulk_in = endpoint->bEndpointAddress;
1147 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1148 data->bulk_in);
1149 break;
1153 /* Find bulk out endpoint */
1154 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1155 endpoint = &iface_desc->endpoint[n].desc;
1157 if (usb_endpoint_is_bulk_out(endpoint)) {
1158 data->bulk_out = endpoint->bEndpointAddress;
1159 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1160 data->bulk_out);
1161 break;
1165 retcode = get_capabilities(data);
1166 if (retcode)
1167 dev_err(&intf->dev, "can't read capabilities\n");
1168 else
1169 retcode = sysfs_create_group(&intf->dev.kobj,
1170 &capability_attr_grp);
1172 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1174 retcode = usb_register_dev(intf, &usbtmc_class);
1175 if (retcode) {
1176 dev_err(&intf->dev, "Not able to get a minor"
1177 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1178 retcode);
1179 goto error_register;
1181 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1183 return 0;
1185 error_register:
1186 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1187 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1188 kref_put(&data->kref, usbtmc_delete);
1189 return retcode;
1192 static void usbtmc_disconnect(struct usb_interface *intf)
1194 struct usbtmc_device_data *data;
1196 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1198 data = usb_get_intfdata(intf);
1199 usb_deregister_dev(intf, &usbtmc_class);
1200 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1201 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1202 mutex_lock(&data->io_mutex);
1203 data->zombie = 1;
1204 mutex_unlock(&data->io_mutex);
1205 kref_put(&data->kref, usbtmc_delete);
1208 static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
1210 /* this driver does not have pending URBs */
1211 return 0;
1214 static int usbtmc_resume(struct usb_interface *intf)
1216 return 0;
1219 static struct usb_driver usbtmc_driver = {
1220 .name = "usbtmc",
1221 .id_table = usbtmc_devices,
1222 .probe = usbtmc_probe,
1223 .disconnect = usbtmc_disconnect,
1224 .suspend = usbtmc_suspend,
1225 .resume = usbtmc_resume,
1228 module_usb_driver(usbtmc_driver);
1230 MODULE_LICENSE("GPL");