mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / usb / class / usbtmc.c
blob917a55c4480dc4de6e46ab1bc6cf5f9a759aae21
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/fs.h>
27 #include <linux/uaccess.h>
28 #include <linux/kref.h>
29 #include <linux/slab.h>
30 #include <linux/poll.h>
31 #include <linux/mutex.h>
32 #include <linux/usb.h>
33 #include <linux/usb/tmc.h>
36 #define RIGOL 1
37 #define USBTMC_HEADER_SIZE 12
38 #define USBTMC_MINOR_BASE 176
41 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
42 * large as wMaxPacketSize (which is usually 512 bytes).
44 #define USBTMC_SIZE_IOBUFFER 2048
46 /* Default USB timeout (in milliseconds) */
47 #define USBTMC_TIMEOUT 5000
50 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
51 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
52 * packet is never read.
54 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
56 static const struct usb_device_id usbtmc_devices[] = {
57 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
58 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
59 { 0, } /* terminating entry */
61 MODULE_DEVICE_TABLE(usb, usbtmc_devices);
64 * This structure is the capabilities for the device
65 * See section 4.2.1.8 of the USBTMC specification,
66 * and section 4.2.2 of the USBTMC usb488 subclass
67 * specification for details.
69 struct usbtmc_dev_capabilities {
70 __u8 interface_capabilities;
71 __u8 device_capabilities;
72 __u8 usb488_interface_capabilities;
73 __u8 usb488_device_capabilities;
76 /* This structure holds private data for each USBTMC device. One copy is
77 * allocated for each USBTMC device in the driver's probe function.
79 struct usbtmc_device_data {
80 const struct usb_device_id *id;
81 struct usb_device *usb_dev;
82 struct usb_interface *intf;
84 unsigned int bulk_in;
85 unsigned int bulk_out;
87 u8 bTag;
88 u8 bTag_last_write; /* needed for abort */
89 u8 bTag_last_read; /* needed for abort */
91 /* data for interrupt in endpoint handling */
92 u8 bNotify1;
93 u8 bNotify2;
94 u16 ifnum;
95 u8 iin_bTag;
96 u8 *iin_buffer;
97 atomic_t iin_data_valid;
98 unsigned int iin_ep;
99 int iin_ep_present;
100 int iin_interval;
101 struct urb *iin_urb;
102 u16 iin_wMaxPacketSize;
103 atomic_t srq_asserted;
105 /* coalesced usb488_caps from usbtmc_dev_capabilities */
106 __u8 usb488_caps;
108 u8 rigol_quirk;
110 /* attributes from the USB TMC spec for this device */
111 u8 TermChar;
112 bool TermCharEnabled;
113 bool auto_abort;
115 bool zombie; /* fd of disconnected device */
117 struct usbtmc_dev_capabilities capabilities;
118 struct kref kref;
119 struct mutex io_mutex; /* only one i/o function running at a time */
120 wait_queue_head_t waitq;
121 struct fasync_struct *fasync;
123 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
125 struct usbtmc_ID_rigol_quirk {
126 __u16 idVendor;
127 __u16 idProduct;
130 static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
131 { 0x1ab1, 0x0588 },
132 { 0x1ab1, 0x04b0 },
133 { 0, 0 }
136 /* Forward declarations */
137 static struct usb_driver usbtmc_driver;
139 static void usbtmc_delete(struct kref *kref)
141 struct usbtmc_device_data *data = to_usbtmc_data(kref);
143 usb_put_dev(data->usb_dev);
146 static int usbtmc_open(struct inode *inode, struct file *filp)
148 struct usb_interface *intf;
149 struct usbtmc_device_data *data;
150 int retval = 0;
152 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
153 if (!intf) {
154 pr_err("can not find device for minor %d", iminor(inode));
155 return -ENODEV;
158 data = usb_get_intfdata(intf);
159 kref_get(&data->kref);
161 /* Store pointer in file structure's private data field */
162 filp->private_data = data;
164 return retval;
167 static int usbtmc_release(struct inode *inode, struct file *file)
169 struct usbtmc_device_data *data = file->private_data;
171 kref_put(&data->kref, usbtmc_delete);
172 return 0;
175 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
177 u8 *buffer;
178 struct device *dev;
179 int rv;
180 int n;
181 int actual;
182 struct usb_host_interface *current_setting;
183 int max_size;
185 dev = &data->intf->dev;
186 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
187 if (!buffer)
188 return -ENOMEM;
190 rv = usb_control_msg(data->usb_dev,
191 usb_rcvctrlpipe(data->usb_dev, 0),
192 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
193 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
194 data->bTag_last_read, data->bulk_in,
195 buffer, 2, USBTMC_TIMEOUT);
197 if (rv < 0) {
198 dev_err(dev, "usb_control_msg returned %d\n", rv);
199 goto exit;
202 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
204 if (buffer[0] == USBTMC_STATUS_FAILED) {
205 rv = 0;
206 goto exit;
209 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
210 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
211 buffer[0]);
212 rv = -EPERM;
213 goto exit;
216 max_size = 0;
217 current_setting = data->intf->cur_altsetting;
218 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
219 if (current_setting->endpoint[n].desc.bEndpointAddress ==
220 data->bulk_in)
221 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
223 if (max_size == 0) {
224 dev_err(dev, "Couldn't get wMaxPacketSize\n");
225 rv = -EPERM;
226 goto exit;
229 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
231 n = 0;
233 do {
234 dev_dbg(dev, "Reading from bulk in EP\n");
236 rv = usb_bulk_msg(data->usb_dev,
237 usb_rcvbulkpipe(data->usb_dev,
238 data->bulk_in),
239 buffer, USBTMC_SIZE_IOBUFFER,
240 &actual, USBTMC_TIMEOUT);
242 n++;
244 if (rv < 0) {
245 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
246 goto exit;
248 } while ((actual == max_size) &&
249 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
251 if (actual == max_size) {
252 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
253 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
254 rv = -EPERM;
255 goto exit;
258 n = 0;
260 usbtmc_abort_bulk_in_status:
261 rv = usb_control_msg(data->usb_dev,
262 usb_rcvctrlpipe(data->usb_dev, 0),
263 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
264 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
265 0, data->bulk_in, buffer, 0x08,
266 USBTMC_TIMEOUT);
268 if (rv < 0) {
269 dev_err(dev, "usb_control_msg returned %d\n", rv);
270 goto exit;
273 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
275 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
276 rv = 0;
277 goto exit;
280 if (buffer[0] != USBTMC_STATUS_PENDING) {
281 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
282 rv = -EPERM;
283 goto exit;
286 if (buffer[1] == 1)
287 do {
288 dev_dbg(dev, "Reading from bulk in EP\n");
290 rv = usb_bulk_msg(data->usb_dev,
291 usb_rcvbulkpipe(data->usb_dev,
292 data->bulk_in),
293 buffer, USBTMC_SIZE_IOBUFFER,
294 &actual, USBTMC_TIMEOUT);
296 n++;
298 if (rv < 0) {
299 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
300 goto exit;
302 } while ((actual == max_size) &&
303 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
305 if (actual == max_size) {
306 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
307 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
308 rv = -EPERM;
309 goto exit;
312 goto usbtmc_abort_bulk_in_status;
314 exit:
315 kfree(buffer);
316 return rv;
320 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
322 struct device *dev;
323 u8 *buffer;
324 int rv;
325 int n;
327 dev = &data->intf->dev;
329 buffer = kmalloc(8, GFP_KERNEL);
330 if (!buffer)
331 return -ENOMEM;
333 rv = usb_control_msg(data->usb_dev,
334 usb_rcvctrlpipe(data->usb_dev, 0),
335 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
336 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
337 data->bTag_last_write, data->bulk_out,
338 buffer, 2, USBTMC_TIMEOUT);
340 if (rv < 0) {
341 dev_err(dev, "usb_control_msg returned %d\n", rv);
342 goto exit;
345 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
347 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
348 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
349 buffer[0]);
350 rv = -EPERM;
351 goto exit;
354 n = 0;
356 usbtmc_abort_bulk_out_check_status:
357 rv = usb_control_msg(data->usb_dev,
358 usb_rcvctrlpipe(data->usb_dev, 0),
359 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
360 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
361 0, data->bulk_out, buffer, 0x08,
362 USBTMC_TIMEOUT);
363 n++;
364 if (rv < 0) {
365 dev_err(dev, "usb_control_msg returned %d\n", rv);
366 goto exit;
369 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
371 if (buffer[0] == USBTMC_STATUS_SUCCESS)
372 goto usbtmc_abort_bulk_out_clear_halt;
374 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
375 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
376 goto usbtmc_abort_bulk_out_check_status;
378 rv = -EPERM;
379 goto exit;
381 usbtmc_abort_bulk_out_clear_halt:
382 rv = usb_clear_halt(data->usb_dev,
383 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
385 if (rv < 0) {
386 dev_err(dev, "usb_control_msg returned %d\n", rv);
387 goto exit;
389 rv = 0;
391 exit:
392 kfree(buffer);
393 return rv;
396 static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data *data,
397 void __user *arg)
399 struct device *dev = &data->intf->dev;
400 u8 *buffer;
401 u8 tag;
402 __u8 stb;
403 int rv;
405 dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n",
406 data->iin_ep_present);
408 buffer = kmalloc(8, GFP_KERNEL);
409 if (!buffer)
410 return -ENOMEM;
412 atomic_set(&data->iin_data_valid, 0);
414 /* must issue read_stb before using poll or select */
415 atomic_set(&data->srq_asserted, 0);
417 rv = usb_control_msg(data->usb_dev,
418 usb_rcvctrlpipe(data->usb_dev, 0),
419 USBTMC488_REQUEST_READ_STATUS_BYTE,
420 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
421 data->iin_bTag,
422 data->ifnum,
423 buffer, 0x03, USBTMC_TIMEOUT);
424 if (rv < 0) {
425 dev_err(dev, "stb usb_control_msg returned %d\n", rv);
426 goto exit;
429 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
430 dev_err(dev, "control status returned %x\n", buffer[0]);
431 rv = -EIO;
432 goto exit;
435 if (data->iin_ep_present) {
436 rv = wait_event_interruptible_timeout(
437 data->waitq,
438 atomic_read(&data->iin_data_valid) != 0,
439 USBTMC_TIMEOUT);
440 if (rv < 0) {
441 dev_dbg(dev, "wait interrupted %d\n", rv);
442 goto exit;
445 if (rv == 0) {
446 dev_dbg(dev, "wait timed out\n");
447 rv = -ETIME;
448 goto exit;
451 tag = data->bNotify1 & 0x7f;
452 if (tag != data->iin_bTag) {
453 dev_err(dev, "expected bTag %x got %x\n",
454 data->iin_bTag, tag);
457 stb = data->bNotify2;
458 } else {
459 stb = buffer[2];
462 rv = copy_to_user(arg, &stb, sizeof(stb));
463 if (rv)
464 rv = -EFAULT;
466 exit:
467 /* bump interrupt bTag */
468 data->iin_bTag += 1;
469 if (data->iin_bTag > 127)
470 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
471 data->iin_bTag = 2;
473 kfree(buffer);
474 return rv;
477 static int usbtmc488_ioctl_simple(struct usbtmc_device_data *data,
478 void __user *arg, unsigned int cmd)
480 struct device *dev = &data->intf->dev;
481 __u8 val;
482 u8 *buffer;
483 u16 wValue;
484 int rv;
486 if (!(data->usb488_caps & USBTMC488_CAPABILITY_SIMPLE))
487 return -EINVAL;
489 buffer = kmalloc(8, GFP_KERNEL);
490 if (!buffer)
491 return -ENOMEM;
493 if (cmd == USBTMC488_REQUEST_REN_CONTROL) {
494 rv = copy_from_user(&val, arg, sizeof(val));
495 if (rv) {
496 rv = -EFAULT;
497 goto exit;
499 wValue = val ? 1 : 0;
500 } else {
501 wValue = 0;
504 rv = usb_control_msg(data->usb_dev,
505 usb_rcvctrlpipe(data->usb_dev, 0),
506 cmd,
507 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
508 wValue,
509 data->ifnum,
510 buffer, 0x01, USBTMC_TIMEOUT);
511 if (rv < 0) {
512 dev_err(dev, "simple usb_control_msg failed %d\n", rv);
513 goto exit;
514 } else if (rv != 1) {
515 dev_warn(dev, "simple usb_control_msg returned %d\n", rv);
516 rv = -EIO;
517 goto exit;
520 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
521 dev_err(dev, "simple control status returned %x\n", buffer[0]);
522 rv = -EIO;
523 goto exit;
525 rv = 0;
527 exit:
528 kfree(buffer);
529 return rv;
533 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
534 * @transfer_size: number of bytes to request from the device.
536 * See the USBTMC specification, Table 4.
538 * Also updates bTag_last_write.
540 static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
542 int retval;
543 u8 *buffer;
544 int actual;
546 buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
547 if (!buffer)
548 return -ENOMEM;
549 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
550 * Refer to class specs for details
552 buffer[0] = 2;
553 buffer[1] = data->bTag;
554 buffer[2] = ~data->bTag;
555 buffer[3] = 0; /* Reserved */
556 buffer[4] = transfer_size >> 0;
557 buffer[5] = transfer_size >> 8;
558 buffer[6] = transfer_size >> 16;
559 buffer[7] = transfer_size >> 24;
560 buffer[8] = data->TermCharEnabled * 2;
561 /* Use term character? */
562 buffer[9] = data->TermChar;
563 buffer[10] = 0; /* Reserved */
564 buffer[11] = 0; /* Reserved */
566 /* Send bulk URB */
567 retval = usb_bulk_msg(data->usb_dev,
568 usb_sndbulkpipe(data->usb_dev,
569 data->bulk_out),
570 buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
572 /* Store bTag (in case we need to abort) */
573 data->bTag_last_write = data->bTag;
575 /* Increment bTag -- and increment again if zero */
576 data->bTag++;
577 if (!data->bTag)
578 data->bTag++;
580 kfree(buffer);
581 if (retval < 0) {
582 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
583 return retval;
586 return 0;
589 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
590 size_t count, loff_t *f_pos)
592 struct usbtmc_device_data *data;
593 struct device *dev;
594 u32 n_characters;
595 u8 *buffer;
596 int actual;
597 size_t done;
598 size_t remaining;
599 int retval;
600 size_t this_part;
602 /* Get pointer to private data structure */
603 data = filp->private_data;
604 dev = &data->intf->dev;
606 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
607 if (!buffer)
608 return -ENOMEM;
610 mutex_lock(&data->io_mutex);
611 if (data->zombie) {
612 retval = -ENODEV;
613 goto exit;
616 if (data->rigol_quirk) {
617 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
619 retval = send_request_dev_dep_msg_in(data, count);
621 if (retval < 0) {
622 if (data->auto_abort)
623 usbtmc_ioctl_abort_bulk_out(data);
624 goto exit;
628 /* Loop until we have fetched everything we requested */
629 remaining = count;
630 this_part = remaining;
631 done = 0;
633 while (remaining > 0) {
634 if (!data->rigol_quirk) {
635 dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
637 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
638 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
639 else
640 this_part = remaining;
642 retval = send_request_dev_dep_msg_in(data, this_part);
643 if (retval < 0) {
644 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
645 if (data->auto_abort)
646 usbtmc_ioctl_abort_bulk_out(data);
647 goto exit;
651 /* Send bulk URB */
652 retval = usb_bulk_msg(data->usb_dev,
653 usb_rcvbulkpipe(data->usb_dev,
654 data->bulk_in),
655 buffer, USBTMC_SIZE_IOBUFFER, &actual,
656 USBTMC_TIMEOUT);
658 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
660 /* Store bTag (in case we need to abort) */
661 data->bTag_last_read = data->bTag;
663 if (retval < 0) {
664 dev_dbg(dev, "Unable to read data, error %d\n", retval);
665 if (data->auto_abort)
666 usbtmc_ioctl_abort_bulk_in(data);
667 goto exit;
670 /* Parse header in first packet */
671 if ((done == 0) || !data->rigol_quirk) {
672 /* Sanity checks for the header */
673 if (actual < USBTMC_HEADER_SIZE) {
674 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
675 if (data->auto_abort)
676 usbtmc_ioctl_abort_bulk_in(data);
677 goto exit;
680 if (buffer[0] != 2) {
681 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
682 if (data->auto_abort)
683 usbtmc_ioctl_abort_bulk_in(data);
684 goto exit;
687 if (buffer[1] != data->bTag_last_write) {
688 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
689 if (data->auto_abort)
690 usbtmc_ioctl_abort_bulk_in(data);
691 goto exit;
694 /* How many characters did the instrument send? */
695 n_characters = buffer[4] +
696 (buffer[5] << 8) +
697 (buffer[6] << 16) +
698 (buffer[7] << 24);
700 if (n_characters > this_part) {
701 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
702 if (data->auto_abort)
703 usbtmc_ioctl_abort_bulk_in(data);
704 goto exit;
707 /* Remove the USBTMC header */
708 actual -= USBTMC_HEADER_SIZE;
710 /* Check if the message is smaller than requested */
711 if (data->rigol_quirk) {
712 if (remaining > n_characters)
713 remaining = n_characters;
714 /* Remove padding if it exists */
715 if (actual > remaining)
716 actual = remaining;
718 else {
719 if (this_part > n_characters)
720 this_part = n_characters;
721 /* Remove padding if it exists */
722 if (actual > this_part)
723 actual = this_part;
726 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
728 remaining -= actual;
730 /* Terminate if end-of-message bit received from device */
731 if ((buffer[8] & 0x01) && (actual >= n_characters))
732 remaining = 0;
734 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
737 /* Copy buffer to user space */
738 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
739 /* There must have been an addressing problem */
740 retval = -EFAULT;
741 goto exit;
743 done += actual;
745 else {
746 if (actual > remaining)
747 actual = remaining;
749 remaining -= actual;
751 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
753 /* Copy buffer to user space */
754 if (copy_to_user(buf + done, buffer, actual)) {
755 /* There must have been an addressing problem */
756 retval = -EFAULT;
757 goto exit;
759 done += actual;
763 /* Update file position value */
764 *f_pos = *f_pos + done;
765 retval = done;
767 exit:
768 mutex_unlock(&data->io_mutex);
769 kfree(buffer);
770 return retval;
773 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
774 size_t count, loff_t *f_pos)
776 struct usbtmc_device_data *data;
777 u8 *buffer;
778 int retval;
779 int actual;
780 unsigned long int n_bytes;
781 int remaining;
782 int done;
783 int this_part;
785 data = filp->private_data;
787 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
788 if (!buffer)
789 return -ENOMEM;
791 mutex_lock(&data->io_mutex);
792 if (data->zombie) {
793 retval = -ENODEV;
794 goto exit;
797 remaining = count;
798 done = 0;
800 while (remaining > 0) {
801 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
802 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
803 buffer[8] = 0;
804 } else {
805 this_part = remaining;
806 buffer[8] = 1;
809 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
810 buffer[0] = 1;
811 buffer[1] = data->bTag;
812 buffer[2] = ~data->bTag;
813 buffer[3] = 0; /* Reserved */
814 buffer[4] = this_part >> 0;
815 buffer[5] = this_part >> 8;
816 buffer[6] = this_part >> 16;
817 buffer[7] = this_part >> 24;
818 /* buffer[8] is set above... */
819 buffer[9] = 0; /* Reserved */
820 buffer[10] = 0; /* Reserved */
821 buffer[11] = 0; /* Reserved */
823 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
824 retval = -EFAULT;
825 goto exit;
828 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
829 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
831 do {
832 retval = usb_bulk_msg(data->usb_dev,
833 usb_sndbulkpipe(data->usb_dev,
834 data->bulk_out),
835 buffer, n_bytes,
836 &actual, USBTMC_TIMEOUT);
837 if (retval != 0)
838 break;
839 n_bytes -= actual;
840 } while (n_bytes);
842 data->bTag_last_write = data->bTag;
843 data->bTag++;
845 if (!data->bTag)
846 data->bTag++;
848 if (retval < 0) {
849 dev_err(&data->intf->dev,
850 "Unable to send data, error %d\n", retval);
851 if (data->auto_abort)
852 usbtmc_ioctl_abort_bulk_out(data);
853 goto exit;
856 remaining -= this_part;
857 done += this_part;
860 retval = count;
861 exit:
862 mutex_unlock(&data->io_mutex);
863 kfree(buffer);
864 return retval;
867 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
869 struct usb_host_interface *current_setting;
870 struct usb_endpoint_descriptor *desc;
871 struct device *dev;
872 u8 *buffer;
873 int rv;
874 int n;
875 int actual = 0;
876 int max_size;
878 dev = &data->intf->dev;
880 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
882 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
883 if (!buffer)
884 return -ENOMEM;
886 rv = usb_control_msg(data->usb_dev,
887 usb_rcvctrlpipe(data->usb_dev, 0),
888 USBTMC_REQUEST_INITIATE_CLEAR,
889 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
890 0, 0, buffer, 1, USBTMC_TIMEOUT);
891 if (rv < 0) {
892 dev_err(dev, "usb_control_msg returned %d\n", rv);
893 goto exit;
896 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
898 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
899 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
900 rv = -EPERM;
901 goto exit;
904 max_size = 0;
905 current_setting = data->intf->cur_altsetting;
906 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
907 desc = &current_setting->endpoint[n].desc;
908 if (desc->bEndpointAddress == data->bulk_in)
909 max_size = usb_endpoint_maxp(desc);
912 if (max_size == 0) {
913 dev_err(dev, "Couldn't get wMaxPacketSize\n");
914 rv = -EPERM;
915 goto exit;
918 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
920 n = 0;
922 usbtmc_clear_check_status:
924 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
926 rv = usb_control_msg(data->usb_dev,
927 usb_rcvctrlpipe(data->usb_dev, 0),
928 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
929 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
930 0, 0, buffer, 2, USBTMC_TIMEOUT);
931 if (rv < 0) {
932 dev_err(dev, "usb_control_msg returned %d\n", rv);
933 goto exit;
936 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
938 if (buffer[0] == USBTMC_STATUS_SUCCESS)
939 goto usbtmc_clear_bulk_out_halt;
941 if (buffer[0] != USBTMC_STATUS_PENDING) {
942 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
943 rv = -EPERM;
944 goto exit;
947 if (buffer[1] == 1)
948 do {
949 dev_dbg(dev, "Reading from bulk in EP\n");
951 rv = usb_bulk_msg(data->usb_dev,
952 usb_rcvbulkpipe(data->usb_dev,
953 data->bulk_in),
954 buffer, USBTMC_SIZE_IOBUFFER,
955 &actual, USBTMC_TIMEOUT);
956 n++;
958 if (rv < 0) {
959 dev_err(dev, "usb_control_msg returned %d\n",
960 rv);
961 goto exit;
963 } while ((actual == max_size) &&
964 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
966 if (actual == max_size) {
967 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
968 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
969 rv = -EPERM;
970 goto exit;
973 goto usbtmc_clear_check_status;
975 usbtmc_clear_bulk_out_halt:
977 rv = usb_clear_halt(data->usb_dev,
978 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
979 if (rv < 0) {
980 dev_err(dev, "usb_control_msg returned %d\n", rv);
981 goto exit;
983 rv = 0;
985 exit:
986 kfree(buffer);
987 return rv;
990 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
992 int rv;
994 rv = usb_clear_halt(data->usb_dev,
995 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
997 if (rv < 0) {
998 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
999 rv);
1000 return rv;
1002 return 0;
1005 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
1007 int rv;
1009 rv = usb_clear_halt(data->usb_dev,
1010 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
1012 if (rv < 0) {
1013 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
1014 rv);
1015 return rv;
1017 return 0;
1020 static int get_capabilities(struct usbtmc_device_data *data)
1022 struct device *dev = &data->usb_dev->dev;
1023 char *buffer;
1024 int rv = 0;
1026 buffer = kmalloc(0x18, GFP_KERNEL);
1027 if (!buffer)
1028 return -ENOMEM;
1030 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
1031 USBTMC_REQUEST_GET_CAPABILITIES,
1032 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1033 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
1034 if (rv < 0) {
1035 dev_err(dev, "usb_control_msg returned %d\n", rv);
1036 goto err_out;
1039 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
1040 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1041 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
1042 rv = -EPERM;
1043 goto err_out;
1045 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
1046 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
1047 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
1048 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
1050 data->capabilities.interface_capabilities = buffer[4];
1051 data->capabilities.device_capabilities = buffer[5];
1052 data->capabilities.usb488_interface_capabilities = buffer[14];
1053 data->capabilities.usb488_device_capabilities = buffer[15];
1054 data->usb488_caps = (buffer[14] & 0x07) | ((buffer[15] & 0x0f) << 4);
1055 rv = 0;
1057 err_out:
1058 kfree(buffer);
1059 return rv;
1062 #define capability_attribute(name) \
1063 static ssize_t name##_show(struct device *dev, \
1064 struct device_attribute *attr, char *buf) \
1066 struct usb_interface *intf = to_usb_interface(dev); \
1067 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1069 return sprintf(buf, "%d\n", data->capabilities.name); \
1071 static DEVICE_ATTR_RO(name)
1073 capability_attribute(interface_capabilities);
1074 capability_attribute(device_capabilities);
1075 capability_attribute(usb488_interface_capabilities);
1076 capability_attribute(usb488_device_capabilities);
1078 static struct attribute *capability_attrs[] = {
1079 &dev_attr_interface_capabilities.attr,
1080 &dev_attr_device_capabilities.attr,
1081 &dev_attr_usb488_interface_capabilities.attr,
1082 &dev_attr_usb488_device_capabilities.attr,
1083 NULL,
1086 static struct attribute_group capability_attr_grp = {
1087 .attrs = capability_attrs,
1090 static ssize_t TermChar_show(struct device *dev,
1091 struct device_attribute *attr, char *buf)
1093 struct usb_interface *intf = to_usb_interface(dev);
1094 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1096 return sprintf(buf, "%c\n", data->TermChar);
1099 static ssize_t TermChar_store(struct device *dev,
1100 struct device_attribute *attr,
1101 const char *buf, size_t count)
1103 struct usb_interface *intf = to_usb_interface(dev);
1104 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1106 if (count < 1)
1107 return -EINVAL;
1108 data->TermChar = buf[0];
1109 return count;
1111 static DEVICE_ATTR_RW(TermChar);
1113 #define data_attribute(name) \
1114 static ssize_t name##_show(struct device *dev, \
1115 struct device_attribute *attr, char *buf) \
1117 struct usb_interface *intf = to_usb_interface(dev); \
1118 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1120 return sprintf(buf, "%d\n", data->name); \
1122 static ssize_t name##_store(struct device *dev, \
1123 struct device_attribute *attr, \
1124 const char *buf, size_t count) \
1126 struct usb_interface *intf = to_usb_interface(dev); \
1127 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1128 ssize_t result; \
1129 unsigned val; \
1131 result = sscanf(buf, "%u\n", &val); \
1132 if (result != 1) \
1133 result = -EINVAL; \
1134 data->name = val; \
1135 if (result < 0) \
1136 return result; \
1137 else \
1138 return count; \
1140 static DEVICE_ATTR_RW(name)
1142 data_attribute(TermCharEnabled);
1143 data_attribute(auto_abort);
1145 static struct attribute *data_attrs[] = {
1146 &dev_attr_TermChar.attr,
1147 &dev_attr_TermCharEnabled.attr,
1148 &dev_attr_auto_abort.attr,
1149 NULL,
1152 static struct attribute_group data_attr_grp = {
1153 .attrs = data_attrs,
1156 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
1158 struct device *dev;
1159 u8 *buffer;
1160 int rv;
1162 dev = &data->intf->dev;
1164 buffer = kmalloc(2, GFP_KERNEL);
1165 if (!buffer)
1166 return -ENOMEM;
1168 rv = usb_control_msg(data->usb_dev,
1169 usb_rcvctrlpipe(data->usb_dev, 0),
1170 USBTMC_REQUEST_INDICATOR_PULSE,
1171 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1172 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1174 if (rv < 0) {
1175 dev_err(dev, "usb_control_msg returned %d\n", rv);
1176 goto exit;
1179 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1181 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1182 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1183 rv = -EPERM;
1184 goto exit;
1186 rv = 0;
1188 exit:
1189 kfree(buffer);
1190 return rv;
1193 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1195 struct usbtmc_device_data *data;
1196 int retval = -EBADRQC;
1198 data = file->private_data;
1199 mutex_lock(&data->io_mutex);
1200 if (data->zombie) {
1201 retval = -ENODEV;
1202 goto skip_io_on_zombie;
1205 switch (cmd) {
1206 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1207 retval = usbtmc_ioctl_clear_out_halt(data);
1208 break;
1210 case USBTMC_IOCTL_CLEAR_IN_HALT:
1211 retval = usbtmc_ioctl_clear_in_halt(data);
1212 break;
1214 case USBTMC_IOCTL_INDICATOR_PULSE:
1215 retval = usbtmc_ioctl_indicator_pulse(data);
1216 break;
1218 case USBTMC_IOCTL_CLEAR:
1219 retval = usbtmc_ioctl_clear(data);
1220 break;
1222 case USBTMC_IOCTL_ABORT_BULK_OUT:
1223 retval = usbtmc_ioctl_abort_bulk_out(data);
1224 break;
1226 case USBTMC_IOCTL_ABORT_BULK_IN:
1227 retval = usbtmc_ioctl_abort_bulk_in(data);
1228 break;
1230 case USBTMC488_IOCTL_GET_CAPS:
1231 retval = copy_to_user((void __user *)arg,
1232 &data->usb488_caps,
1233 sizeof(data->usb488_caps));
1234 if (retval)
1235 retval = -EFAULT;
1236 break;
1238 case USBTMC488_IOCTL_READ_STB:
1239 retval = usbtmc488_ioctl_read_stb(data, (void __user *)arg);
1240 break;
1242 case USBTMC488_IOCTL_REN_CONTROL:
1243 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1244 USBTMC488_REQUEST_REN_CONTROL);
1245 break;
1247 case USBTMC488_IOCTL_GOTO_LOCAL:
1248 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1249 USBTMC488_REQUEST_GOTO_LOCAL);
1250 break;
1252 case USBTMC488_IOCTL_LOCAL_LOCKOUT:
1253 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1254 USBTMC488_REQUEST_LOCAL_LOCKOUT);
1255 break;
1258 skip_io_on_zombie:
1259 mutex_unlock(&data->io_mutex);
1260 return retval;
1263 static int usbtmc_fasync(int fd, struct file *file, int on)
1265 struct usbtmc_device_data *data = file->private_data;
1267 return fasync_helper(fd, file, on, &data->fasync);
1270 static unsigned int usbtmc_poll(struct file *file, poll_table *wait)
1272 struct usbtmc_device_data *data = file->private_data;
1273 unsigned int mask;
1275 mutex_lock(&data->io_mutex);
1277 if (data->zombie) {
1278 mask = POLLHUP | POLLERR;
1279 goto no_poll;
1282 poll_wait(file, &data->waitq, wait);
1284 mask = (atomic_read(&data->srq_asserted)) ? POLLIN | POLLRDNORM : 0;
1286 no_poll:
1287 mutex_unlock(&data->io_mutex);
1288 return mask;
1291 static const struct file_operations fops = {
1292 .owner = THIS_MODULE,
1293 .read = usbtmc_read,
1294 .write = usbtmc_write,
1295 .open = usbtmc_open,
1296 .release = usbtmc_release,
1297 .unlocked_ioctl = usbtmc_ioctl,
1298 .fasync = usbtmc_fasync,
1299 .poll = usbtmc_poll,
1300 .llseek = default_llseek,
1303 static struct usb_class_driver usbtmc_class = {
1304 .name = "usbtmc%d",
1305 .fops = &fops,
1306 .minor_base = USBTMC_MINOR_BASE,
1309 static void usbtmc_interrupt(struct urb *urb)
1311 struct usbtmc_device_data *data = urb->context;
1312 struct device *dev = &data->intf->dev;
1313 int status = urb->status;
1314 int rv;
1316 dev_dbg(&data->intf->dev, "int status: %d len %d\n",
1317 status, urb->actual_length);
1319 switch (status) {
1320 case 0: /* SUCCESS */
1321 /* check for valid STB notification */
1322 if (data->iin_buffer[0] > 0x81) {
1323 data->bNotify1 = data->iin_buffer[0];
1324 data->bNotify2 = data->iin_buffer[1];
1325 atomic_set(&data->iin_data_valid, 1);
1326 wake_up_interruptible(&data->waitq);
1327 goto exit;
1329 /* check for SRQ notification */
1330 if (data->iin_buffer[0] == 0x81) {
1331 if (data->fasync)
1332 kill_fasync(&data->fasync,
1333 SIGIO, POLL_IN);
1335 atomic_set(&data->srq_asserted, 1);
1336 wake_up_interruptible(&data->waitq);
1337 goto exit;
1339 dev_warn(dev, "invalid notification: %x\n", data->iin_buffer[0]);
1340 break;
1341 case -EOVERFLOW:
1342 dev_err(dev, "overflow with length %d, actual length is %d\n",
1343 data->iin_wMaxPacketSize, urb->actual_length);
1344 case -ECONNRESET:
1345 case -ENOENT:
1346 case -ESHUTDOWN:
1347 case -EILSEQ:
1348 case -ETIME:
1349 /* urb terminated, clean up */
1350 dev_dbg(dev, "urb terminated, status: %d\n", status);
1351 return;
1352 default:
1353 dev_err(dev, "unknown status received: %d\n", status);
1355 exit:
1356 rv = usb_submit_urb(urb, GFP_ATOMIC);
1357 if (rv)
1358 dev_err(dev, "usb_submit_urb failed: %d\n", rv);
1361 static void usbtmc_free_int(struct usbtmc_device_data *data)
1363 if (!data->iin_ep_present || !data->iin_urb)
1364 return;
1365 usb_kill_urb(data->iin_urb);
1366 kfree(data->iin_buffer);
1367 usb_free_urb(data->iin_urb);
1368 kref_put(&data->kref, usbtmc_delete);
1371 static int usbtmc_probe(struct usb_interface *intf,
1372 const struct usb_device_id *id)
1374 struct usbtmc_device_data *data;
1375 struct usb_host_interface *iface_desc;
1376 struct usb_endpoint_descriptor *endpoint;
1377 int n;
1378 int retcode;
1380 dev_dbg(&intf->dev, "%s called\n", __func__);
1382 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
1383 if (!data)
1384 return -ENOMEM;
1386 data->intf = intf;
1387 data->id = id;
1388 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1389 usb_set_intfdata(intf, data);
1390 kref_init(&data->kref);
1391 mutex_init(&data->io_mutex);
1392 init_waitqueue_head(&data->waitq);
1393 atomic_set(&data->iin_data_valid, 0);
1394 atomic_set(&data->srq_asserted, 0);
1395 data->zombie = 0;
1397 /* Determine if it is a Rigol or not */
1398 data->rigol_quirk = 0;
1399 dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1400 le16_to_cpu(data->usb_dev->descriptor.idVendor),
1401 le16_to_cpu(data->usb_dev->descriptor.idProduct));
1402 for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
1403 if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) &&
1404 (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) {
1405 dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1406 data->rigol_quirk = 1;
1407 break;
1411 /* Initialize USBTMC bTag and other fields */
1412 data->bTag = 1;
1413 data->TermCharEnabled = 0;
1414 data->TermChar = '\n';
1415 /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
1416 data->iin_bTag = 2;
1418 /* USBTMC devices have only one setting, so use that */
1419 iface_desc = data->intf->cur_altsetting;
1420 data->ifnum = iface_desc->desc.bInterfaceNumber;
1422 /* Find bulk in endpoint */
1423 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1424 endpoint = &iface_desc->endpoint[n].desc;
1426 if (usb_endpoint_is_bulk_in(endpoint)) {
1427 data->bulk_in = endpoint->bEndpointAddress;
1428 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1429 data->bulk_in);
1430 break;
1434 /* Find bulk out endpoint */
1435 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1436 endpoint = &iface_desc->endpoint[n].desc;
1438 if (usb_endpoint_is_bulk_out(endpoint)) {
1439 data->bulk_out = endpoint->bEndpointAddress;
1440 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1441 data->bulk_out);
1442 break;
1445 /* Find int endpoint */
1446 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1447 endpoint = &iface_desc->endpoint[n].desc;
1449 if (usb_endpoint_is_int_in(endpoint)) {
1450 data->iin_ep_present = 1;
1451 data->iin_ep = endpoint->bEndpointAddress;
1452 data->iin_wMaxPacketSize = usb_endpoint_maxp(endpoint);
1453 data->iin_interval = endpoint->bInterval;
1454 dev_dbg(&intf->dev, "Found Int in endpoint at %u\n",
1455 data->iin_ep);
1456 break;
1460 retcode = get_capabilities(data);
1461 if (retcode)
1462 dev_err(&intf->dev, "can't read capabilities\n");
1463 else
1464 retcode = sysfs_create_group(&intf->dev.kobj,
1465 &capability_attr_grp);
1467 if (data->iin_ep_present) {
1468 /* allocate int urb */
1469 data->iin_urb = usb_alloc_urb(0, GFP_KERNEL);
1470 if (!data->iin_urb) {
1471 dev_err(&intf->dev, "Failed to allocate int urb\n");
1472 goto error_register;
1475 /* will reference data in int urb */
1476 kref_get(&data->kref);
1478 /* allocate buffer for interrupt in */
1479 data->iin_buffer = kmalloc(data->iin_wMaxPacketSize,
1480 GFP_KERNEL);
1481 if (!data->iin_buffer) {
1482 dev_err(&intf->dev, "Failed to allocate int buf\n");
1483 goto error_register;
1486 /* fill interrupt urb */
1487 usb_fill_int_urb(data->iin_urb, data->usb_dev,
1488 usb_rcvintpipe(data->usb_dev, data->iin_ep),
1489 data->iin_buffer, data->iin_wMaxPacketSize,
1490 usbtmc_interrupt,
1491 data, data->iin_interval);
1493 retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL);
1494 if (retcode) {
1495 dev_err(&intf->dev, "Failed to submit iin_urb\n");
1496 goto error_register;
1500 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1502 retcode = usb_register_dev(intf, &usbtmc_class);
1503 if (retcode) {
1504 dev_err(&intf->dev, "Not able to get a minor"
1505 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1506 retcode);
1507 goto error_register;
1509 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1511 return 0;
1513 error_register:
1514 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1515 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1516 usbtmc_free_int(data);
1517 kref_put(&data->kref, usbtmc_delete);
1518 return retcode;
1521 static void usbtmc_disconnect(struct usb_interface *intf)
1523 struct usbtmc_device_data *data;
1525 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1527 data = usb_get_intfdata(intf);
1528 usb_deregister_dev(intf, &usbtmc_class);
1529 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1530 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1531 mutex_lock(&data->io_mutex);
1532 data->zombie = 1;
1533 wake_up_all(&data->waitq);
1534 mutex_unlock(&data->io_mutex);
1535 usbtmc_free_int(data);
1536 kref_put(&data->kref, usbtmc_delete);
1539 static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
1541 /* this driver does not have pending URBs */
1542 return 0;
1545 static int usbtmc_resume(struct usb_interface *intf)
1547 return 0;
1550 static struct usb_driver usbtmc_driver = {
1551 .name = "usbtmc",
1552 .id_table = usbtmc_devices,
1553 .probe = usbtmc_probe,
1554 .disconnect = usbtmc_disconnect,
1555 .suspend = usbtmc_suspend,
1556 .resume = usbtmc_resume,
1559 module_usb_driver(usbtmc_driver);
1561 MODULE_LICENSE("GPL");