1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
5 * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
6 * Copyright (C) 2008 Novell, Inc.
7 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
8 * Copyright (C) 2018 IVI Foundation, Inc.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/kernel.h>
16 #include <linux/uaccess.h>
17 #include <linux/kref.h>
18 #include <linux/slab.h>
19 #include <linux/poll.h>
20 #include <linux/mutex.h>
21 #include <linux/usb.h>
22 #include <linux/compat.h>
23 #include <linux/usb/tmc.h>
25 /* Increment API VERSION when changing tmc.h with new flags or ioctls
26 * or when changing a significant behavior of the driver.
28 #define USBTMC_API_VERSION (2)
30 #define USBTMC_HEADER_SIZE 12
31 #define USBTMC_MINOR_BASE 176
33 /* Minimum USB timeout (in milliseconds) */
34 #define USBTMC_MIN_TIMEOUT 100
35 /* Default USB timeout (in milliseconds) */
36 #define USBTMC_TIMEOUT 5000
38 /* Max number of urbs used in write transfers */
39 #define MAX_URBS_IN_FLIGHT 16
40 /* I/O buffer size used in generic read/write functions */
41 #define USBTMC_BUFSIZE (4096)
44 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
45 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
46 * packet is never read.
48 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
50 static const struct usb_device_id usbtmc_devices
[] = {
51 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 0), },
52 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 1), },
53 { 0, } /* terminating entry */
55 MODULE_DEVICE_TABLE(usb
, usbtmc_devices
);
58 * This structure is the capabilities for the device
59 * See section 4.2.1.8 of the USBTMC specification,
60 * and section 4.2.2 of the USBTMC usb488 subclass
61 * specification for details.
63 struct usbtmc_dev_capabilities
{
64 __u8 interface_capabilities
;
65 __u8 device_capabilities
;
66 __u8 usb488_interface_capabilities
;
67 __u8 usb488_device_capabilities
;
70 /* This structure holds private data for each USBTMC device. One copy is
71 * allocated for each USBTMC device in the driver's probe function.
73 struct usbtmc_device_data
{
74 const struct usb_device_id
*id
;
75 struct usb_device
*usb_dev
;
76 struct usb_interface
*intf
;
77 struct list_head file_list
;
80 unsigned int bulk_out
;
83 u8 bTag_last_write
; /* needed for abort */
84 u8 bTag_last_read
; /* needed for abort */
86 /* packet size of IN bulk */
89 /* data for interrupt in endpoint handling */
95 atomic_t iin_data_valid
;
100 u16 iin_wMaxPacketSize
;
102 /* coalesced usb488_caps from usbtmc_dev_capabilities */
105 bool zombie
; /* fd of disconnected device */
107 struct usbtmc_dev_capabilities capabilities
;
109 struct mutex io_mutex
; /* only one i/o function running at a time */
110 wait_queue_head_t waitq
;
111 struct fasync_struct
*fasync
;
112 spinlock_t dev_lock
; /* lock for file_list */
114 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
117 * This structure holds private data for each USBTMC file handle.
119 struct usbtmc_file_data
{
120 struct usbtmc_device_data
*data
;
121 struct list_head file_elem
;
125 atomic_t srq_asserted
;
127 u8 bmTransferAttributes
; /* member of DEV_DEP_MSG_IN */
131 bool term_char_enabled
;
134 spinlock_t err_lock
; /* lock for errors */
136 struct usb_anchor submitted
;
138 /* data for generic_write */
139 struct semaphore limit_write_sem
;
140 u32 out_transfer_size
;
143 /* data for generic_read */
144 u32 in_transfer_size
;
147 struct usb_anchor in_anchor
;
148 wait_queue_head_t wait_bulk_in
;
151 /* Forward declarations */
152 static struct usb_driver usbtmc_driver
;
153 static void usbtmc_draw_down(struct usbtmc_file_data
*file_data
);
155 static void usbtmc_delete(struct kref
*kref
)
157 struct usbtmc_device_data
*data
= to_usbtmc_data(kref
);
159 usb_put_dev(data
->usb_dev
);
163 static int usbtmc_open(struct inode
*inode
, struct file
*filp
)
165 struct usb_interface
*intf
;
166 struct usbtmc_device_data
*data
;
167 struct usbtmc_file_data
*file_data
;
169 intf
= usb_find_interface(&usbtmc_driver
, iminor(inode
));
171 pr_err("can not find device for minor %d", iminor(inode
));
175 file_data
= kzalloc(sizeof(*file_data
), GFP_KERNEL
);
179 spin_lock_init(&file_data
->err_lock
);
180 sema_init(&file_data
->limit_write_sem
, MAX_URBS_IN_FLIGHT
);
181 init_usb_anchor(&file_data
->submitted
);
182 init_usb_anchor(&file_data
->in_anchor
);
183 init_waitqueue_head(&file_data
->wait_bulk_in
);
185 data
= usb_get_intfdata(intf
);
186 /* Protect reference to data from file structure until release */
187 kref_get(&data
->kref
);
189 mutex_lock(&data
->io_mutex
);
190 file_data
->data
= data
;
192 atomic_set(&file_data
->closing
, 0);
194 file_data
->timeout
= USBTMC_TIMEOUT
;
195 file_data
->term_char
= '\n';
196 file_data
->term_char_enabled
= 0;
197 file_data
->auto_abort
= 0;
198 file_data
->eom_val
= 1;
200 INIT_LIST_HEAD(&file_data
->file_elem
);
201 spin_lock_irq(&data
->dev_lock
);
202 list_add_tail(&file_data
->file_elem
, &data
->file_list
);
203 spin_unlock_irq(&data
->dev_lock
);
204 mutex_unlock(&data
->io_mutex
);
206 /* Store pointer in file structure's private data field */
207 filp
->private_data
= file_data
;
213 * usbtmc_flush - called before file handle is closed
215 static int usbtmc_flush(struct file
*file
, fl_owner_t id
)
217 struct usbtmc_file_data
*file_data
;
218 struct usbtmc_device_data
*data
;
220 file_data
= file
->private_data
;
221 if (file_data
== NULL
)
224 atomic_set(&file_data
->closing
, 1);
225 data
= file_data
->data
;
227 /* wait for io to stop */
228 mutex_lock(&data
->io_mutex
);
230 usbtmc_draw_down(file_data
);
232 spin_lock_irq(&file_data
->err_lock
);
233 file_data
->in_status
= 0;
234 file_data
->in_transfer_size
= 0;
235 file_data
->in_urbs_used
= 0;
236 file_data
->out_status
= 0;
237 file_data
->out_transfer_size
= 0;
238 spin_unlock_irq(&file_data
->err_lock
);
240 wake_up_interruptible_all(&data
->waitq
);
241 mutex_unlock(&data
->io_mutex
);
246 static int usbtmc_release(struct inode
*inode
, struct file
*file
)
248 struct usbtmc_file_data
*file_data
= file
->private_data
;
250 /* prevent IO _AND_ usbtmc_interrupt */
251 mutex_lock(&file_data
->data
->io_mutex
);
252 spin_lock_irq(&file_data
->data
->dev_lock
);
254 list_del(&file_data
->file_elem
);
256 spin_unlock_irq(&file_data
->data
->dev_lock
);
257 mutex_unlock(&file_data
->data
->io_mutex
);
259 kref_put(&file_data
->data
->kref
, usbtmc_delete
);
260 file_data
->data
= NULL
;
265 static int usbtmc_ioctl_abort_bulk_in_tag(struct usbtmc_device_data
*data
,
274 dev
= &data
->intf
->dev
;
275 buffer
= kmalloc(USBTMC_BUFSIZE
, GFP_KERNEL
);
279 rv
= usb_control_msg(data
->usb_dev
,
280 usb_rcvctrlpipe(data
->usb_dev
, 0),
281 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN
,
282 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
284 buffer
, 2, USB_CTRL_GET_TIMEOUT
);
287 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
291 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x with tag %02x\n",
292 buffer
[0], buffer
[1]);
294 if (buffer
[0] == USBTMC_STATUS_FAILED
) {
295 /* No transfer in progress and the Bulk-OUT FIFO is empty. */
300 if (buffer
[0] == USBTMC_STATUS_TRANSFER_NOT_IN_PROGRESS
) {
301 /* The device returns this status if either:
302 * - There is a transfer in progress, but the specified bTag
304 * - There is no transfer in progress, but the Bulk-OUT FIFO
311 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
312 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n",
320 usbtmc_abort_bulk_in_status
:
321 dev_dbg(dev
, "Reading from bulk in EP\n");
323 /* Data must be present. So use low timeout 300 ms */
325 rv
= usb_bulk_msg(data
->usb_dev
,
326 usb_rcvbulkpipe(data
->usb_dev
,
328 buffer
, USBTMC_BUFSIZE
,
331 print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE
, 16, 1,
332 buffer
, actual
, true);
337 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
338 if (rv
!= -ETIMEDOUT
)
342 if (actual
== USBTMC_BUFSIZE
)
343 goto usbtmc_abort_bulk_in_status
;
345 if (n
>= USBTMC_MAX_READS_TO_CLEAR_BULK_IN
) {
346 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
347 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
352 rv
= usb_control_msg(data
->usb_dev
,
353 usb_rcvctrlpipe(data
->usb_dev
, 0),
354 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS
,
355 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
356 0, data
->bulk_in
, buffer
, 0x08,
357 USB_CTRL_GET_TIMEOUT
);
360 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
364 dev_dbg(dev
, "CHECK_ABORT_BULK_IN returned %x\n", buffer
[0]);
366 if (buffer
[0] == USBTMC_STATUS_SUCCESS
) {
371 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
372 dev_err(dev
, "CHECK_ABORT_BULK_IN returned %x\n", buffer
[0]);
377 if ((buffer
[1] & 1) > 0) {
378 /* The device has 1 or more queued packets the Host can read */
379 goto usbtmc_abort_bulk_in_status
;
382 /* The Host must send CHECK_ABORT_BULK_IN_STATUS at a later time. */
389 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data
*data
)
391 return usbtmc_ioctl_abort_bulk_in_tag(data
, data
->bTag_last_read
);
394 static int usbtmc_ioctl_abort_bulk_out_tag(struct usbtmc_device_data
*data
,
402 dev
= &data
->intf
->dev
;
404 buffer
= kmalloc(8, GFP_KERNEL
);
408 rv
= usb_control_msg(data
->usb_dev
,
409 usb_rcvctrlpipe(data
->usb_dev
, 0),
410 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT
,
411 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
413 buffer
, 2, USB_CTRL_GET_TIMEOUT
);
416 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
420 dev_dbg(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer
[0]);
422 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
423 dev_err(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n",
431 usbtmc_abort_bulk_out_check_status
:
432 /* do not stress device with subsequent requests */
434 rv
= usb_control_msg(data
->usb_dev
,
435 usb_rcvctrlpipe(data
->usb_dev
, 0),
436 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS
,
437 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
438 0, data
->bulk_out
, buffer
, 0x08,
439 USB_CTRL_GET_TIMEOUT
);
442 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
446 dev_dbg(dev
, "CHECK_ABORT_BULK_OUT returned %x\n", buffer
[0]);
448 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
449 goto usbtmc_abort_bulk_out_clear_halt
;
451 if ((buffer
[0] == USBTMC_STATUS_PENDING
) &&
452 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
))
453 goto usbtmc_abort_bulk_out_check_status
;
458 usbtmc_abort_bulk_out_clear_halt
:
459 rv
= usb_clear_halt(data
->usb_dev
,
460 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
463 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
473 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data
*data
)
475 return usbtmc_ioctl_abort_bulk_out_tag(data
, data
->bTag_last_write
);
478 static int usbtmc488_ioctl_read_stb(struct usbtmc_file_data
*file_data
,
481 struct usbtmc_device_data
*data
= file_data
->data
;
482 struct device
*dev
= &data
->intf
->dev
;
483 int srq_asserted
= 0;
489 dev_dbg(dev
, "Enter ioctl_read_stb iin_ep_present: %d\n",
490 data
->iin_ep_present
);
492 spin_lock_irq(&data
->dev_lock
);
493 srq_asserted
= atomic_xchg(&file_data
->srq_asserted
, srq_asserted
);
495 /* a STB with SRQ is already received */
496 stb
= file_data
->srq_byte
;
497 spin_unlock_irq(&data
->dev_lock
);
498 rv
= put_user(stb
, (__u8 __user
*)arg
);
499 dev_dbg(dev
, "stb:0x%02x with srq received %d\n",
500 (unsigned int)stb
, rv
);
503 spin_unlock_irq(&data
->dev_lock
);
505 buffer
= kmalloc(8, GFP_KERNEL
);
509 atomic_set(&data
->iin_data_valid
, 0);
511 rv
= usb_control_msg(data
->usb_dev
,
512 usb_rcvctrlpipe(data
->usb_dev
, 0),
513 USBTMC488_REQUEST_READ_STATUS_BYTE
,
514 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
517 buffer
, 0x03, USB_CTRL_GET_TIMEOUT
);
519 dev_err(dev
, "stb usb_control_msg returned %d\n", rv
);
523 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
524 dev_err(dev
, "control status returned %x\n", buffer
[0]);
529 if (data
->iin_ep_present
) {
530 rv
= wait_event_interruptible_timeout(
532 atomic_read(&data
->iin_data_valid
) != 0,
535 dev_dbg(dev
, "wait interrupted %d\n", rv
);
540 dev_dbg(dev
, "wait timed out\n");
545 tag
= data
->bNotify1
& 0x7f;
546 if (tag
!= data
->iin_bTag
) {
547 dev_err(dev
, "expected bTag %x got %x\n",
548 data
->iin_bTag
, tag
);
551 stb
= data
->bNotify2
;
556 rv
= put_user(stb
, (__u8 __user
*)arg
);
557 dev_dbg(dev
, "stb:0x%02x received %d\n", (unsigned int)stb
, rv
);
560 /* bump interrupt bTag */
562 if (data
->iin_bTag
> 127)
563 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
570 static int usbtmc488_ioctl_wait_srq(struct usbtmc_file_data
*file_data
,
573 struct usbtmc_device_data
*data
= file_data
->data
;
574 struct device
*dev
= &data
->intf
->dev
;
577 unsigned long expire
;
579 if (!data
->iin_ep_present
) {
580 dev_dbg(dev
, "no interrupt endpoint present\n");
584 if (get_user(timeout
, arg
))
587 expire
= msecs_to_jiffies(timeout
);
589 mutex_unlock(&data
->io_mutex
);
591 rv
= wait_event_interruptible_timeout(
593 atomic_read(&file_data
->srq_asserted
) != 0 ||
594 atomic_read(&file_data
->closing
),
597 mutex_lock(&data
->io_mutex
);
599 /* Note! disconnect or close could be called in the meantime */
600 if (atomic_read(&file_data
->closing
) || data
->zombie
)
604 /* dev can be invalid now! */
605 pr_debug("%s - wait interrupted %d\n", __func__
, rv
);
610 dev_dbg(dev
, "%s - wait timed out\n", __func__
);
614 dev_dbg(dev
, "%s - srq asserted\n", __func__
);
618 static int usbtmc488_ioctl_simple(struct usbtmc_device_data
*data
,
619 void __user
*arg
, unsigned int cmd
)
621 struct device
*dev
= &data
->intf
->dev
;
627 if (!(data
->usb488_caps
& USBTMC488_CAPABILITY_SIMPLE
))
630 buffer
= kmalloc(8, GFP_KERNEL
);
634 if (cmd
== USBTMC488_REQUEST_REN_CONTROL
) {
635 rv
= copy_from_user(&val
, arg
, sizeof(val
));
640 wValue
= val
? 1 : 0;
645 rv
= usb_control_msg(data
->usb_dev
,
646 usb_rcvctrlpipe(data
->usb_dev
, 0),
648 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
651 buffer
, 0x01, USB_CTRL_GET_TIMEOUT
);
653 dev_err(dev
, "simple usb_control_msg failed %d\n", rv
);
655 } else if (rv
!= 1) {
656 dev_warn(dev
, "simple usb_control_msg returned %d\n", rv
);
661 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
662 dev_err(dev
, "simple control status returned %x\n", buffer
[0]);
674 * Sends a TRIGGER Bulk-OUT command message
675 * See the USBTMC-USB488 specification, Table 2.
677 * Also updates bTag_last_write.
679 static int usbtmc488_ioctl_trigger(struct usbtmc_file_data
*file_data
)
681 struct usbtmc_device_data
*data
= file_data
->data
;
686 buffer
= kzalloc(USBTMC_HEADER_SIZE
, GFP_KERNEL
);
691 buffer
[1] = data
->bTag
;
692 buffer
[2] = ~data
->bTag
;
694 retval
= usb_bulk_msg(data
->usb_dev
,
695 usb_sndbulkpipe(data
->usb_dev
,
697 buffer
, USBTMC_HEADER_SIZE
,
698 &actual
, file_data
->timeout
);
700 /* Store bTag (in case we need to abort) */
701 data
->bTag_last_write
= data
->bTag
;
703 /* Increment bTag -- and increment again if zero */
710 dev_err(&data
->intf
->dev
, "%s returned %d\n",
718 static struct urb
*usbtmc_create_urb(void)
720 const size_t bufsize
= USBTMC_BUFSIZE
;
722 struct urb
*urb
= usb_alloc_urb(0, GFP_KERNEL
);
727 dmabuf
= kmalloc(bufsize
, GFP_KERNEL
);
733 urb
->transfer_buffer
= dmabuf
;
734 urb
->transfer_buffer_length
= bufsize
;
735 urb
->transfer_flags
|= URB_FREE_BUFFER
;
739 static void usbtmc_read_bulk_cb(struct urb
*urb
)
741 struct usbtmc_file_data
*file_data
= urb
->context
;
742 int status
= urb
->status
;
745 /* sync/async unlink faults aren't errors */
747 if (!(/* status == -ENOENT || */
748 status
== -ECONNRESET
||
749 status
== -EREMOTEIO
|| /* Short packet */
750 status
== -ESHUTDOWN
))
751 dev_err(&file_data
->data
->intf
->dev
,
752 "%s - nonzero read bulk status received: %d\n",
755 spin_lock_irqsave(&file_data
->err_lock
, flags
);
756 if (!file_data
->in_status
)
757 file_data
->in_status
= status
;
758 spin_unlock_irqrestore(&file_data
->err_lock
, flags
);
761 spin_lock_irqsave(&file_data
->err_lock
, flags
);
762 file_data
->in_transfer_size
+= urb
->actual_length
;
763 dev_dbg(&file_data
->data
->intf
->dev
,
764 "%s - total size: %u current: %d status: %d\n",
765 __func__
, file_data
->in_transfer_size
,
766 urb
->actual_length
, status
);
767 spin_unlock_irqrestore(&file_data
->err_lock
, flags
);
768 usb_anchor_urb(urb
, &file_data
->in_anchor
);
770 wake_up_interruptible(&file_data
->wait_bulk_in
);
771 wake_up_interruptible(&file_data
->data
->waitq
);
774 static inline bool usbtmc_do_transfer(struct usbtmc_file_data
*file_data
)
778 spin_lock_irq(&file_data
->err_lock
);
779 data_or_error
= !usb_anchor_empty(&file_data
->in_anchor
)
780 || file_data
->in_status
;
781 spin_unlock_irq(&file_data
->err_lock
);
782 dev_dbg(&file_data
->data
->intf
->dev
, "%s: returns %d\n", __func__
,
784 return data_or_error
;
787 static ssize_t
usbtmc_generic_read(struct usbtmc_file_data
*file_data
,
788 void __user
*user_buffer
,
793 struct usbtmc_device_data
*data
= file_data
->data
;
794 struct device
*dev
= &data
->intf
->dev
;
797 const u32 bufsize
= USBTMC_BUFSIZE
;
799 u32 max_transfer_size
;
800 unsigned long expire
;
804 /* mutex already locked */
808 max_transfer_size
= transfer_size
;
810 if (flags
& USBTMC_FLAG_IGNORE_TRAILER
) {
811 /* The device may send extra alignment bytes (up to
812 * wMaxPacketSize – 1) to avoid sending a zero-length
815 remaining
= transfer_size
;
816 if ((max_transfer_size
% data
->wMaxPacketSize
) == 0)
817 max_transfer_size
+= (data
->wMaxPacketSize
- 1);
819 /* round down to bufsize to avoid truncated data left */
820 if (max_transfer_size
> bufsize
) {
822 roundup(max_transfer_size
+ 1 - bufsize
,
825 remaining
= max_transfer_size
;
828 spin_lock_irq(&file_data
->err_lock
);
830 if (file_data
->in_status
) {
831 /* return the very first error */
832 retval
= file_data
->in_status
;
833 spin_unlock_irq(&file_data
->err_lock
);
837 if (flags
& USBTMC_FLAG_ASYNC
) {
838 if (usb_anchor_empty(&file_data
->in_anchor
))
841 if (file_data
->in_urbs_used
== 0) {
842 file_data
->in_transfer_size
= 0;
843 file_data
->in_status
= 0;
846 file_data
->in_transfer_size
= 0;
847 file_data
->in_status
= 0;
850 if (max_transfer_size
== 0) {
853 bufcount
= roundup(max_transfer_size
, bufsize
) / bufsize
;
854 if (bufcount
> file_data
->in_urbs_used
)
855 bufcount
-= file_data
->in_urbs_used
;
859 if (bufcount
+ file_data
->in_urbs_used
> MAX_URBS_IN_FLIGHT
) {
860 bufcount
= MAX_URBS_IN_FLIGHT
-
861 file_data
->in_urbs_used
;
864 spin_unlock_irq(&file_data
->err_lock
);
866 dev_dbg(dev
, "%s: requested=%u flags=0x%X size=%u bufs=%d used=%d\n",
867 __func__
, transfer_size
, flags
,
868 max_transfer_size
, bufcount
, file_data
->in_urbs_used
);
870 while (bufcount
> 0) {
872 struct urb
*urb
= usbtmc_create_urb();
879 dmabuf
= urb
->transfer_buffer
;
881 usb_fill_bulk_urb(urb
, data
->usb_dev
,
882 usb_rcvbulkpipe(data
->usb_dev
, data
->bulk_in
),
884 usbtmc_read_bulk_cb
, file_data
);
886 usb_anchor_urb(urb
, &file_data
->submitted
);
887 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
888 /* urb is anchored. We can release our reference. */
890 if (unlikely(retval
)) {
891 usb_unanchor_urb(urb
);
894 file_data
->in_urbs_used
++;
899 dev_dbg(dev
, "%s: ret=again\n", __func__
);
903 if (user_buffer
== NULL
)
906 expire
= msecs_to_jiffies(file_data
->timeout
);
908 while (max_transfer_size
> 0) {
910 struct urb
*urb
= NULL
;
912 if (!(flags
& USBTMC_FLAG_ASYNC
)) {
913 dev_dbg(dev
, "%s: before wait time %lu\n",
915 retval
= wait_event_interruptible_timeout(
916 file_data
->wait_bulk_in
,
917 usbtmc_do_transfer(file_data
),
920 dev_dbg(dev
, "%s: wait returned %d\n",
930 urb
= usb_get_from_anchor(&file_data
->in_anchor
);
932 if (!(flags
& USBTMC_FLAG_ASYNC
)) {
933 /* synchronous case: must not happen */
938 /* asynchronous case: ready, do not block or wait */
940 dev_dbg(dev
, "%s: (async) done=%u ret=0\n",
945 file_data
->in_urbs_used
--;
947 if (max_transfer_size
> urb
->actual_length
)
948 max_transfer_size
-= urb
->actual_length
;
950 max_transfer_size
= 0;
952 if (remaining
> urb
->actual_length
)
953 this_part
= urb
->actual_length
;
955 this_part
= remaining
;
957 print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE
, 16, 1,
958 urb
->transfer_buffer
, urb
->actual_length
, true);
960 if (copy_to_user(user_buffer
+ done
,
961 urb
->transfer_buffer
, this_part
)) {
967 remaining
-= this_part
;
970 spin_lock_irq(&file_data
->err_lock
);
972 /* return the very first error */
973 retval
= file_data
->in_status
;
974 spin_unlock_irq(&file_data
->err_lock
);
978 spin_unlock_irq(&file_data
->err_lock
);
980 if (urb
->actual_length
< bufsize
) {
981 /* short packet or ZLP received => ready */
987 if (!(flags
& USBTMC_FLAG_ASYNC
) &&
988 max_transfer_size
> (bufsize
* file_data
->in_urbs_used
)) {
989 /* resubmit, since other buffers still not enough */
990 usb_anchor_urb(urb
, &file_data
->submitted
);
991 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
992 if (unlikely(retval
)) {
993 usb_unanchor_urb(urb
);
997 file_data
->in_urbs_used
++;
1004 *transferred
= done
;
1006 dev_dbg(dev
, "%s: before kill\n", __func__
);
1007 /* Attention: killing urbs can take long time (2 ms) */
1008 usb_kill_anchored_urbs(&file_data
->submitted
);
1009 dev_dbg(dev
, "%s: after kill\n", __func__
);
1010 usb_scuttle_anchored_urbs(&file_data
->in_anchor
);
1011 file_data
->in_urbs_used
= 0;
1012 file_data
->in_status
= 0; /* no spinlock needed here */
1013 dev_dbg(dev
, "%s: done=%u ret=%d\n", __func__
, done
, retval
);
1018 static ssize_t
usbtmc_ioctl_generic_read(struct usbtmc_file_data
*file_data
,
1021 struct usbtmc_message msg
;
1024 /* mutex already locked */
1026 if (copy_from_user(&msg
, arg
, sizeof(struct usbtmc_message
)))
1029 retval
= usbtmc_generic_read(file_data
, msg
.message
,
1030 msg
.transfer_size
, &msg
.transferred
,
1033 if (put_user(msg
.transferred
,
1034 &((struct usbtmc_message __user
*)arg
)->transferred
))
1040 static void usbtmc_write_bulk_cb(struct urb
*urb
)
1042 struct usbtmc_file_data
*file_data
= urb
->context
;
1044 unsigned long flags
;
1046 spin_lock_irqsave(&file_data
->err_lock
, flags
);
1047 file_data
->out_transfer_size
+= urb
->actual_length
;
1049 /* sync/async unlink faults aren't errors */
1051 if (!(urb
->status
== -ENOENT
||
1052 urb
->status
== -ECONNRESET
||
1053 urb
->status
== -ESHUTDOWN
))
1054 dev_err(&file_data
->data
->intf
->dev
,
1055 "%s - nonzero write bulk status received: %d\n",
1056 __func__
, urb
->status
);
1058 if (!file_data
->out_status
) {
1059 file_data
->out_status
= urb
->status
;
1063 spin_unlock_irqrestore(&file_data
->err_lock
, flags
);
1065 dev_dbg(&file_data
->data
->intf
->dev
,
1066 "%s - write bulk total size: %u\n",
1067 __func__
, file_data
->out_transfer_size
);
1069 up(&file_data
->limit_write_sem
);
1070 if (usb_anchor_empty(&file_data
->submitted
) || wakeup
)
1071 wake_up_interruptible(&file_data
->data
->waitq
);
1074 static ssize_t
usbtmc_generic_write(struct usbtmc_file_data
*file_data
,
1075 const void __user
*user_buffer
,
1080 struct usbtmc_device_data
*data
= file_data
->data
;
1084 unsigned long expire
;
1085 const u32 bufsize
= USBTMC_BUFSIZE
;
1086 struct urb
*urb
= NULL
;
1092 /* Get pointer to private data structure */
1093 dev
= &data
->intf
->dev
;
1095 dev_dbg(dev
, "%s: size=%u flags=0x%X sema=%u\n",
1096 __func__
, transfer_size
, flags
,
1097 file_data
->limit_write_sem
.count
);
1099 if (flags
& USBTMC_FLAG_APPEND
) {
1100 spin_lock_irq(&file_data
->err_lock
);
1101 retval
= file_data
->out_status
;
1102 spin_unlock_irq(&file_data
->err_lock
);
1106 spin_lock_irq(&file_data
->err_lock
);
1107 file_data
->out_transfer_size
= 0;
1108 file_data
->out_status
= 0;
1109 spin_unlock_irq(&file_data
->err_lock
);
1112 remaining
= transfer_size
;
1113 if (remaining
> INT_MAX
)
1114 remaining
= INT_MAX
;
1116 timeout
= file_data
->timeout
;
1117 expire
= msecs_to_jiffies(timeout
);
1119 while (remaining
> 0) {
1120 u32 this_part
, aligned
;
1123 if (flags
& USBTMC_FLAG_ASYNC
) {
1124 if (down_trylock(&file_data
->limit_write_sem
)) {
1125 retval
= (done
)?(0):(-EAGAIN
);
1129 retval
= down_timeout(&file_data
->limit_write_sem
,
1132 retval
= -ETIMEDOUT
;
1137 spin_lock_irq(&file_data
->err_lock
);
1138 retval
= file_data
->out_status
;
1139 spin_unlock_irq(&file_data
->err_lock
);
1141 up(&file_data
->limit_write_sem
);
1145 /* prepare next urb to send */
1146 urb
= usbtmc_create_urb();
1149 up(&file_data
->limit_write_sem
);
1152 buffer
= urb
->transfer_buffer
;
1154 if (remaining
> bufsize
)
1155 this_part
= bufsize
;
1157 this_part
= remaining
;
1159 if (copy_from_user(buffer
, user_buffer
+ done
, this_part
)) {
1161 up(&file_data
->limit_write_sem
);
1165 print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE
,
1166 16, 1, buffer
, this_part
, true);
1168 /* fill bulk with 32 bit alignment to meet USBTMC specification
1169 * (size + 3 & ~3) rounds up and simplifies user code
1171 aligned
= (this_part
+ 3) & ~3;
1172 dev_dbg(dev
, "write(size:%u align:%u done:%u)\n",
1173 (unsigned int)this_part
,
1174 (unsigned int)aligned
,
1175 (unsigned int)done
);
1177 usb_fill_bulk_urb(urb
, data
->usb_dev
,
1178 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
),
1179 urb
->transfer_buffer
, aligned
,
1180 usbtmc_write_bulk_cb
, file_data
);
1182 usb_anchor_urb(urb
, &file_data
->submitted
);
1183 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1184 if (unlikely(retval
)) {
1185 usb_unanchor_urb(urb
);
1186 up(&file_data
->limit_write_sem
);
1191 urb
= NULL
; /* urb will be finally released by usb driver */
1193 remaining
-= this_part
;
1197 /* All urbs are on the fly */
1198 if (!(flags
& USBTMC_FLAG_ASYNC
)) {
1199 if (!usb_wait_anchor_empty_timeout(&file_data
->submitted
,
1201 retval
= -ETIMEDOUT
;
1210 usb_kill_anchored_urbs(&file_data
->submitted
);
1214 spin_lock_irq(&file_data
->err_lock
);
1215 if (!(flags
& USBTMC_FLAG_ASYNC
))
1216 done
= file_data
->out_transfer_size
;
1217 if (!retval
&& file_data
->out_status
)
1218 retval
= file_data
->out_status
;
1219 spin_unlock_irq(&file_data
->err_lock
);
1221 *transferred
= done
;
1223 dev_dbg(dev
, "%s: done=%u, retval=%d, urbstat=%d\n",
1224 __func__
, done
, retval
, file_data
->out_status
);
1229 static ssize_t
usbtmc_ioctl_generic_write(struct usbtmc_file_data
*file_data
,
1232 struct usbtmc_message msg
;
1235 /* mutex already locked */
1237 if (copy_from_user(&msg
, arg
, sizeof(struct usbtmc_message
)))
1240 retval
= usbtmc_generic_write(file_data
, msg
.message
,
1241 msg
.transfer_size
, &msg
.transferred
,
1244 if (put_user(msg
.transferred
,
1245 &((struct usbtmc_message __user
*)arg
)->transferred
))
1252 * Get the generic write result
1254 static ssize_t
usbtmc_ioctl_write_result(struct usbtmc_file_data
*file_data
,
1260 spin_lock_irq(&file_data
->err_lock
);
1261 transferred
= file_data
->out_transfer_size
;
1262 retval
= file_data
->out_status
;
1263 spin_unlock_irq(&file_data
->err_lock
);
1265 if (put_user(transferred
, (__u32 __user
*)arg
))
1272 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-OUT endpoint.
1273 * @transfer_size: number of bytes to request from the device.
1275 * See the USBTMC specification, Table 4.
1277 * Also updates bTag_last_write.
1279 static int send_request_dev_dep_msg_in(struct usbtmc_file_data
*file_data
,
1282 struct usbtmc_device_data
*data
= file_data
->data
;
1287 buffer
= kmalloc(USBTMC_HEADER_SIZE
, GFP_KERNEL
);
1290 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
1291 * Refer to class specs for details
1294 buffer
[1] = data
->bTag
;
1295 buffer
[2] = ~data
->bTag
;
1296 buffer
[3] = 0; /* Reserved */
1297 buffer
[4] = transfer_size
>> 0;
1298 buffer
[5] = transfer_size
>> 8;
1299 buffer
[6] = transfer_size
>> 16;
1300 buffer
[7] = transfer_size
>> 24;
1301 buffer
[8] = file_data
->term_char_enabled
* 2;
1302 /* Use term character? */
1303 buffer
[9] = file_data
->term_char
;
1304 buffer
[10] = 0; /* Reserved */
1305 buffer
[11] = 0; /* Reserved */
1308 retval
= usb_bulk_msg(data
->usb_dev
,
1309 usb_sndbulkpipe(data
->usb_dev
,
1311 buffer
, USBTMC_HEADER_SIZE
,
1312 &actual
, file_data
->timeout
);
1314 /* Store bTag (in case we need to abort) */
1315 data
->bTag_last_write
= data
->bTag
;
1317 /* Increment bTag -- and increment again if zero */
1324 dev_err(&data
->intf
->dev
, "%s returned %d\n",
1330 static ssize_t
usbtmc_read(struct file
*filp
, char __user
*buf
,
1331 size_t count
, loff_t
*f_pos
)
1333 struct usbtmc_file_data
*file_data
;
1334 struct usbtmc_device_data
*data
;
1336 const u32 bufsize
= USBTMC_BUFSIZE
;
1344 /* Get pointer to private data structure */
1345 file_data
= filp
->private_data
;
1346 data
= file_data
->data
;
1347 dev
= &data
->intf
->dev
;
1349 buffer
= kmalloc(bufsize
, GFP_KERNEL
);
1353 mutex_lock(&data
->io_mutex
);
1359 if (count
> INT_MAX
)
1362 dev_dbg(dev
, "%s(count:%zu)\n", __func__
, count
);
1364 retval
= send_request_dev_dep_msg_in(file_data
, count
);
1367 if (file_data
->auto_abort
)
1368 usbtmc_ioctl_abort_bulk_out(data
);
1372 /* Loop until we have fetched everything we requested */
1377 retval
= usb_bulk_msg(data
->usb_dev
,
1378 usb_rcvbulkpipe(data
->usb_dev
,
1380 buffer
, bufsize
, &actual
,
1381 file_data
->timeout
);
1383 dev_dbg(dev
, "%s: bulk_msg retval(%u), actual(%d)\n",
1384 __func__
, retval
, actual
);
1386 /* Store bTag (in case we need to abort) */
1387 data
->bTag_last_read
= data
->bTag
;
1390 if (file_data
->auto_abort
)
1391 usbtmc_ioctl_abort_bulk_in(data
);
1395 /* Sanity checks for the header */
1396 if (actual
< USBTMC_HEADER_SIZE
) {
1397 dev_err(dev
, "Device sent too small first packet: %u < %u\n",
1398 actual
, USBTMC_HEADER_SIZE
);
1399 if (file_data
->auto_abort
)
1400 usbtmc_ioctl_abort_bulk_in(data
);
1404 if (buffer
[0] != 2) {
1405 dev_err(dev
, "Device sent reply with wrong MsgID: %u != 2\n",
1407 if (file_data
->auto_abort
)
1408 usbtmc_ioctl_abort_bulk_in(data
);
1412 if (buffer
[1] != data
->bTag_last_write
) {
1413 dev_err(dev
, "Device sent reply with wrong bTag: %u != %u\n",
1414 buffer
[1], data
->bTag_last_write
);
1415 if (file_data
->auto_abort
)
1416 usbtmc_ioctl_abort_bulk_in(data
);
1420 /* How many characters did the instrument send? */
1421 n_characters
= buffer
[4] +
1426 file_data
->bmTransferAttributes
= buffer
[8];
1428 dev_dbg(dev
, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n",
1429 n_characters
, buffer
[8]);
1431 if (n_characters
> remaining
) {
1432 dev_err(dev
, "Device wants to return more data than requested: %u > %zu\n",
1433 n_characters
, count
);
1434 if (file_data
->auto_abort
)
1435 usbtmc_ioctl_abort_bulk_in(data
);
1439 print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE
,
1440 16, 1, buffer
, actual
, true);
1442 remaining
= n_characters
;
1444 /* Remove the USBTMC header */
1445 actual
-= USBTMC_HEADER_SIZE
;
1447 /* Remove padding if it exists */
1448 if (actual
> remaining
)
1451 remaining
-= actual
;
1453 /* Copy buffer to user space */
1454 if (copy_to_user(buf
, &buffer
[USBTMC_HEADER_SIZE
], actual
)) {
1455 /* There must have been an addressing problem */
1460 if ((actual
+ USBTMC_HEADER_SIZE
) == bufsize
) {
1461 retval
= usbtmc_generic_read(file_data
, buf
+ actual
,
1464 USBTMC_FLAG_IGNORE_TRAILER
);
1470 /* Update file position value */
1471 *f_pos
= *f_pos
+ done
;
1475 mutex_unlock(&data
->io_mutex
);
1480 static ssize_t
usbtmc_write(struct file
*filp
, const char __user
*buf
,
1481 size_t count
, loff_t
*f_pos
)
1483 struct usbtmc_file_data
*file_data
;
1484 struct usbtmc_device_data
*data
;
1485 struct urb
*urb
= NULL
;
1488 u32 remaining
, done
;
1489 u32 transfersize
, aligned
, buflen
;
1491 file_data
= filp
->private_data
;
1492 data
= file_data
->data
;
1494 mutex_lock(&data
->io_mutex
);
1503 spin_lock_irq(&file_data
->err_lock
);
1504 file_data
->out_transfer_size
= 0;
1505 file_data
->out_status
= 0;
1506 spin_unlock_irq(&file_data
->err_lock
);
1511 if (down_trylock(&file_data
->limit_write_sem
)) {
1512 /* previous calls were async */
1517 urb
= usbtmc_create_urb();
1520 up(&file_data
->limit_write_sem
);
1524 buffer
= urb
->transfer_buffer
;
1525 buflen
= urb
->transfer_buffer_length
;
1527 if (count
> INT_MAX
) {
1528 transfersize
= INT_MAX
;
1531 transfersize
= count
;
1532 buffer
[8] = file_data
->eom_val
;
1535 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
1537 buffer
[1] = data
->bTag
;
1538 buffer
[2] = ~data
->bTag
;
1539 buffer
[3] = 0; /* Reserved */
1540 buffer
[4] = transfersize
>> 0;
1541 buffer
[5] = transfersize
>> 8;
1542 buffer
[6] = transfersize
>> 16;
1543 buffer
[7] = transfersize
>> 24;
1544 /* buffer[8] is set above... */
1545 buffer
[9] = 0; /* Reserved */
1546 buffer
[10] = 0; /* Reserved */
1547 buffer
[11] = 0; /* Reserved */
1549 remaining
= transfersize
;
1551 if (transfersize
+ USBTMC_HEADER_SIZE
> buflen
) {
1552 transfersize
= buflen
- USBTMC_HEADER_SIZE
;
1555 aligned
= (transfersize
+ (USBTMC_HEADER_SIZE
+ 3)) & ~3;
1558 if (copy_from_user(&buffer
[USBTMC_HEADER_SIZE
], buf
, transfersize
)) {
1560 up(&file_data
->limit_write_sem
);
1564 dev_dbg(&data
->intf
->dev
, "%s(size:%u align:%u)\n", __func__
,
1565 (unsigned int)transfersize
, (unsigned int)aligned
);
1567 print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE
,
1568 16, 1, buffer
, aligned
, true);
1570 usb_fill_bulk_urb(urb
, data
->usb_dev
,
1571 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
),
1572 urb
->transfer_buffer
, aligned
,
1573 usbtmc_write_bulk_cb
, file_data
);
1575 usb_anchor_urb(urb
, &file_data
->submitted
);
1576 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
1577 if (unlikely(retval
)) {
1578 usb_unanchor_urb(urb
);
1579 up(&file_data
->limit_write_sem
);
1583 remaining
-= transfersize
;
1585 data
->bTag_last_write
= data
->bTag
;
1591 /* call generic_write even when remaining = 0 */
1592 retval
= usbtmc_generic_write(file_data
, buf
+ transfersize
, remaining
,
1593 &done
, USBTMC_FLAG_APPEND
);
1594 /* truncate alignment bytes */
1595 if (done
> remaining
)
1598 /*add size of first urb*/
1599 done
+= transfersize
;
1602 usb_kill_anchored_urbs(&file_data
->submitted
);
1604 dev_err(&data
->intf
->dev
,
1605 "Unable to send data, error %d\n", (int)retval
);
1606 if (file_data
->auto_abort
)
1607 usbtmc_ioctl_abort_bulk_out(data
);
1614 mutex_unlock(&data
->io_mutex
);
1618 static int usbtmc_ioctl_clear(struct usbtmc_device_data
*data
)
1626 dev
= &data
->intf
->dev
;
1628 dev_dbg(dev
, "Sending INITIATE_CLEAR request\n");
1630 buffer
= kmalloc(USBTMC_BUFSIZE
, GFP_KERNEL
);
1634 rv
= usb_control_msg(data
->usb_dev
,
1635 usb_rcvctrlpipe(data
->usb_dev
, 0),
1636 USBTMC_REQUEST_INITIATE_CLEAR
,
1637 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1638 0, 0, buffer
, 1, USB_CTRL_GET_TIMEOUT
);
1640 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1644 dev_dbg(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
1646 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1647 dev_err(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
1654 usbtmc_clear_check_status
:
1656 dev_dbg(dev
, "Sending CHECK_CLEAR_STATUS request\n");
1658 rv
= usb_control_msg(data
->usb_dev
,
1659 usb_rcvctrlpipe(data
->usb_dev
, 0),
1660 USBTMC_REQUEST_CHECK_CLEAR_STATUS
,
1661 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1662 0, 0, buffer
, 2, USB_CTRL_GET_TIMEOUT
);
1664 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1668 dev_dbg(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
1670 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
1671 goto usbtmc_clear_bulk_out_halt
;
1673 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
1674 dev_err(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
1679 if ((buffer
[1] & 1) != 0) {
1681 dev_dbg(dev
, "Reading from bulk in EP\n");
1684 rv
= usb_bulk_msg(data
->usb_dev
,
1685 usb_rcvbulkpipe(data
->usb_dev
,
1687 buffer
, USBTMC_BUFSIZE
,
1688 &actual
, USB_CTRL_GET_TIMEOUT
);
1690 print_hex_dump_debug("usbtmc ", DUMP_PREFIX_NONE
,
1691 16, 1, buffer
, actual
, true);
1696 dev_err(dev
, "usb_control_msg returned %d\n",
1700 } while ((actual
== USBTMC_BUFSIZE
) &&
1701 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
1703 /* do not stress device with subsequent requests */
1708 if (n
>= USBTMC_MAX_READS_TO_CLEAR_BULK_IN
) {
1709 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
1710 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
1715 goto usbtmc_clear_check_status
;
1717 usbtmc_clear_bulk_out_halt
:
1719 rv
= usb_clear_halt(data
->usb_dev
,
1720 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
1722 dev_err(dev
, "usb_clear_halt returned %d\n", rv
);
1732 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data
*data
)
1736 rv
= usb_clear_halt(data
->usb_dev
,
1737 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
1740 dev_err(&data
->usb_dev
->dev
, "%s returned %d\n", __func__
, rv
);
1744 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data
*data
)
1748 rv
= usb_clear_halt(data
->usb_dev
,
1749 usb_rcvbulkpipe(data
->usb_dev
, data
->bulk_in
));
1752 dev_err(&data
->usb_dev
->dev
, "%s returned %d\n", __func__
, rv
);
1756 static int usbtmc_ioctl_cancel_io(struct usbtmc_file_data
*file_data
)
1758 spin_lock_irq(&file_data
->err_lock
);
1759 file_data
->in_status
= -ECANCELED
;
1760 file_data
->out_status
= -ECANCELED
;
1761 spin_unlock_irq(&file_data
->err_lock
);
1762 usb_kill_anchored_urbs(&file_data
->submitted
);
1766 static int usbtmc_ioctl_cleanup_io(struct usbtmc_file_data
*file_data
)
1768 usb_kill_anchored_urbs(&file_data
->submitted
);
1769 usb_scuttle_anchored_urbs(&file_data
->in_anchor
);
1770 spin_lock_irq(&file_data
->err_lock
);
1771 file_data
->in_status
= 0;
1772 file_data
->in_transfer_size
= 0;
1773 file_data
->out_status
= 0;
1774 file_data
->out_transfer_size
= 0;
1775 spin_unlock_irq(&file_data
->err_lock
);
1777 file_data
->in_urbs_used
= 0;
1781 static int get_capabilities(struct usbtmc_device_data
*data
)
1783 struct device
*dev
= &data
->usb_dev
->dev
;
1787 buffer
= kmalloc(0x18, GFP_KERNEL
);
1791 rv
= usb_control_msg(data
->usb_dev
, usb_rcvctrlpipe(data
->usb_dev
, 0),
1792 USBTMC_REQUEST_GET_CAPABILITIES
,
1793 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1794 0, 0, buffer
, 0x18, USB_CTRL_GET_TIMEOUT
);
1796 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1800 dev_dbg(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
1801 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1802 dev_err(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
1806 dev_dbg(dev
, "Interface capabilities are %x\n", buffer
[4]);
1807 dev_dbg(dev
, "Device capabilities are %x\n", buffer
[5]);
1808 dev_dbg(dev
, "USB488 interface capabilities are %x\n", buffer
[14]);
1809 dev_dbg(dev
, "USB488 device capabilities are %x\n", buffer
[15]);
1811 data
->capabilities
.interface_capabilities
= buffer
[4];
1812 data
->capabilities
.device_capabilities
= buffer
[5];
1813 data
->capabilities
.usb488_interface_capabilities
= buffer
[14];
1814 data
->capabilities
.usb488_device_capabilities
= buffer
[15];
1815 data
->usb488_caps
= (buffer
[14] & 0x07) | ((buffer
[15] & 0x0f) << 4);
1823 #define capability_attribute(name) \
1824 static ssize_t name##_show(struct device *dev, \
1825 struct device_attribute *attr, char *buf) \
1827 struct usb_interface *intf = to_usb_interface(dev); \
1828 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1830 return sprintf(buf, "%d\n", data->capabilities.name); \
1832 static DEVICE_ATTR_RO(name)
1834 capability_attribute(interface_capabilities
);
1835 capability_attribute(device_capabilities
);
1836 capability_attribute(usb488_interface_capabilities
);
1837 capability_attribute(usb488_device_capabilities
);
1839 static struct attribute
*capability_attrs
[] = {
1840 &dev_attr_interface_capabilities
.attr
,
1841 &dev_attr_device_capabilities
.attr
,
1842 &dev_attr_usb488_interface_capabilities
.attr
,
1843 &dev_attr_usb488_device_capabilities
.attr
,
1847 static const struct attribute_group capability_attr_grp
= {
1848 .attrs
= capability_attrs
,
1851 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data
*data
)
1857 dev
= &data
->intf
->dev
;
1859 buffer
= kmalloc(2, GFP_KERNEL
);
1863 rv
= usb_control_msg(data
->usb_dev
,
1864 usb_rcvctrlpipe(data
->usb_dev
, 0),
1865 USBTMC_REQUEST_INDICATOR_PULSE
,
1866 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1867 0, 0, buffer
, 0x01, USB_CTRL_GET_TIMEOUT
);
1870 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1874 dev_dbg(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1876 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1877 dev_err(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1888 static int usbtmc_ioctl_request(struct usbtmc_device_data
*data
,
1891 struct device
*dev
= &data
->intf
->dev
;
1892 struct usbtmc_ctrlrequest request
;
1897 res
= copy_from_user(&request
, arg
, sizeof(struct usbtmc_ctrlrequest
));
1901 if (request
.req
.wLength
> USBTMC_BUFSIZE
)
1904 if (request
.req
.wLength
) {
1905 buffer
= kmalloc(request
.req
.wLength
, GFP_KERNEL
);
1909 if ((request
.req
.bRequestType
& USB_DIR_IN
) == 0) {
1910 /* Send control data to device */
1911 res
= copy_from_user(buffer
, request
.data
,
1912 request
.req
.wLength
);
1920 rv
= usb_control_msg(data
->usb_dev
,
1921 usb_rcvctrlpipe(data
->usb_dev
, 0),
1922 request
.req
.bRequest
,
1923 request
.req
.bRequestType
,
1926 buffer
, request
.req
.wLength
, USB_CTRL_GET_TIMEOUT
);
1929 dev_err(dev
, "%s failed %d\n", __func__
, rv
);
1933 if (rv
&& (request
.req
.bRequestType
& USB_DIR_IN
)) {
1934 /* Read control data from device */
1935 res
= copy_to_user(request
.data
, buffer
, rv
);
1946 * Get the usb timeout value
1948 static int usbtmc_ioctl_get_timeout(struct usbtmc_file_data
*file_data
,
1953 timeout
= file_data
->timeout
;
1955 return put_user(timeout
, (__u32 __user
*)arg
);
1959 * Set the usb timeout value
1961 static int usbtmc_ioctl_set_timeout(struct usbtmc_file_data
*file_data
,
1966 if (get_user(timeout
, (__u32 __user
*)arg
))
1969 /* Note that timeout = 0 means
1970 * MAX_SCHEDULE_TIMEOUT in usb_control_msg
1972 if (timeout
< USBTMC_MIN_TIMEOUT
)
1975 file_data
->timeout
= timeout
;
1981 * enables/disables sending EOM on write
1983 static int usbtmc_ioctl_eom_enable(struct usbtmc_file_data
*file_data
,
1988 if (copy_from_user(&eom_enable
, arg
, sizeof(eom_enable
)))
1994 file_data
->eom_val
= eom_enable
;
2000 * Configure termination character for read()
2002 static int usbtmc_ioctl_config_termc(struct usbtmc_file_data
*file_data
,
2005 struct usbtmc_termchar termc
;
2007 if (copy_from_user(&termc
, arg
, sizeof(termc
)))
2010 if ((termc
.term_char_enabled
> 1) ||
2011 (termc
.term_char_enabled
&&
2012 !(file_data
->data
->capabilities
.device_capabilities
& 1)))
2015 file_data
->term_char
= termc
.term_char
;
2016 file_data
->term_char_enabled
= termc
.term_char_enabled
;
2021 static long usbtmc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
2023 struct usbtmc_file_data
*file_data
;
2024 struct usbtmc_device_data
*data
;
2025 int retval
= -EBADRQC
;
2028 file_data
= file
->private_data
;
2029 data
= file_data
->data
;
2031 mutex_lock(&data
->io_mutex
);
2034 goto skip_io_on_zombie
;
2038 case USBTMC_IOCTL_CLEAR_OUT_HALT
:
2039 retval
= usbtmc_ioctl_clear_out_halt(data
);
2042 case USBTMC_IOCTL_CLEAR_IN_HALT
:
2043 retval
= usbtmc_ioctl_clear_in_halt(data
);
2046 case USBTMC_IOCTL_INDICATOR_PULSE
:
2047 retval
= usbtmc_ioctl_indicator_pulse(data
);
2050 case USBTMC_IOCTL_CLEAR
:
2051 retval
= usbtmc_ioctl_clear(data
);
2054 case USBTMC_IOCTL_ABORT_BULK_OUT
:
2055 retval
= usbtmc_ioctl_abort_bulk_out(data
);
2058 case USBTMC_IOCTL_ABORT_BULK_IN
:
2059 retval
= usbtmc_ioctl_abort_bulk_in(data
);
2062 case USBTMC_IOCTL_CTRL_REQUEST
:
2063 retval
= usbtmc_ioctl_request(data
, (void __user
*)arg
);
2066 case USBTMC_IOCTL_GET_TIMEOUT
:
2067 retval
= usbtmc_ioctl_get_timeout(file_data
,
2068 (void __user
*)arg
);
2071 case USBTMC_IOCTL_SET_TIMEOUT
:
2072 retval
= usbtmc_ioctl_set_timeout(file_data
,
2073 (void __user
*)arg
);
2076 case USBTMC_IOCTL_EOM_ENABLE
:
2077 retval
= usbtmc_ioctl_eom_enable(file_data
,
2078 (void __user
*)arg
);
2081 case USBTMC_IOCTL_CONFIG_TERMCHAR
:
2082 retval
= usbtmc_ioctl_config_termc(file_data
,
2083 (void __user
*)arg
);
2086 case USBTMC_IOCTL_WRITE
:
2087 retval
= usbtmc_ioctl_generic_write(file_data
,
2088 (void __user
*)arg
);
2091 case USBTMC_IOCTL_READ
:
2092 retval
= usbtmc_ioctl_generic_read(file_data
,
2093 (void __user
*)arg
);
2096 case USBTMC_IOCTL_WRITE_RESULT
:
2097 retval
= usbtmc_ioctl_write_result(file_data
,
2098 (void __user
*)arg
);
2101 case USBTMC_IOCTL_API_VERSION
:
2102 retval
= put_user(USBTMC_API_VERSION
,
2103 (__u32 __user
*)arg
);
2106 case USBTMC488_IOCTL_GET_CAPS
:
2107 retval
= put_user(data
->usb488_caps
,
2108 (unsigned char __user
*)arg
);
2111 case USBTMC488_IOCTL_READ_STB
:
2112 retval
= usbtmc488_ioctl_read_stb(file_data
,
2113 (void __user
*)arg
);
2116 case USBTMC488_IOCTL_REN_CONTROL
:
2117 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
2118 USBTMC488_REQUEST_REN_CONTROL
);
2121 case USBTMC488_IOCTL_GOTO_LOCAL
:
2122 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
2123 USBTMC488_REQUEST_GOTO_LOCAL
);
2126 case USBTMC488_IOCTL_LOCAL_LOCKOUT
:
2127 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
2128 USBTMC488_REQUEST_LOCAL_LOCKOUT
);
2131 case USBTMC488_IOCTL_TRIGGER
:
2132 retval
= usbtmc488_ioctl_trigger(file_data
);
2135 case USBTMC488_IOCTL_WAIT_SRQ
:
2136 retval
= usbtmc488_ioctl_wait_srq(file_data
,
2137 (__u32 __user
*)arg
);
2140 case USBTMC_IOCTL_MSG_IN_ATTR
:
2141 retval
= put_user(file_data
->bmTransferAttributes
,
2142 (__u8 __user
*)arg
);
2145 case USBTMC_IOCTL_AUTO_ABORT
:
2146 retval
= get_user(tmp_byte
, (unsigned char __user
*)arg
);
2148 file_data
->auto_abort
= !!tmp_byte
;
2151 case USBTMC_IOCTL_CANCEL_IO
:
2152 retval
= usbtmc_ioctl_cancel_io(file_data
);
2155 case USBTMC_IOCTL_CLEANUP_IO
:
2156 retval
= usbtmc_ioctl_cleanup_io(file_data
);
2161 mutex_unlock(&data
->io_mutex
);
2165 static int usbtmc_fasync(int fd
, struct file
*file
, int on
)
2167 struct usbtmc_file_data
*file_data
= file
->private_data
;
2169 return fasync_helper(fd
, file
, on
, &file_data
->data
->fasync
);
2172 static __poll_t
usbtmc_poll(struct file
*file
, poll_table
*wait
)
2174 struct usbtmc_file_data
*file_data
= file
->private_data
;
2175 struct usbtmc_device_data
*data
= file_data
->data
;
2178 mutex_lock(&data
->io_mutex
);
2181 mask
= EPOLLHUP
| EPOLLERR
;
2185 poll_wait(file
, &data
->waitq
, wait
);
2187 /* Note that EPOLLPRI is now assigned to SRQ, and
2188 * EPOLLIN|EPOLLRDNORM to normal read data.
2191 if (atomic_read(&file_data
->srq_asserted
))
2194 /* Note that the anchor submitted includes all urbs for BULK IN
2195 * and OUT. So EPOLLOUT is signaled when BULK OUT is empty and
2196 * all BULK IN urbs are completed and moved to in_anchor.
2198 if (usb_anchor_empty(&file_data
->submitted
))
2199 mask
|= (EPOLLOUT
| EPOLLWRNORM
);
2200 if (!usb_anchor_empty(&file_data
->in_anchor
))
2201 mask
|= (EPOLLIN
| EPOLLRDNORM
);
2203 spin_lock_irq(&file_data
->err_lock
);
2204 if (file_data
->in_status
|| file_data
->out_status
)
2206 spin_unlock_irq(&file_data
->err_lock
);
2208 dev_dbg(&data
->intf
->dev
, "poll mask = %x\n", mask
);
2211 mutex_unlock(&data
->io_mutex
);
2215 static const struct file_operations fops
= {
2216 .owner
= THIS_MODULE
,
2217 .read
= usbtmc_read
,
2218 .write
= usbtmc_write
,
2219 .open
= usbtmc_open
,
2220 .release
= usbtmc_release
,
2221 .flush
= usbtmc_flush
,
2222 .unlocked_ioctl
= usbtmc_ioctl
,
2223 #ifdef CONFIG_COMPAT
2224 .compat_ioctl
= usbtmc_ioctl
,
2226 .fasync
= usbtmc_fasync
,
2227 .poll
= usbtmc_poll
,
2228 .llseek
= default_llseek
,
2231 static struct usb_class_driver usbtmc_class
= {
2234 .minor_base
= USBTMC_MINOR_BASE
,
2237 static void usbtmc_interrupt(struct urb
*urb
)
2239 struct usbtmc_device_data
*data
= urb
->context
;
2240 struct device
*dev
= &data
->intf
->dev
;
2241 int status
= urb
->status
;
2244 dev_dbg(&data
->intf
->dev
, "int status: %d len %d\n",
2245 status
, urb
->actual_length
);
2248 case 0: /* SUCCESS */
2249 /* check for valid STB notification */
2250 if (data
->iin_buffer
[0] > 0x81) {
2251 data
->bNotify1
= data
->iin_buffer
[0];
2252 data
->bNotify2
= data
->iin_buffer
[1];
2253 atomic_set(&data
->iin_data_valid
, 1);
2254 wake_up_interruptible(&data
->waitq
);
2257 /* check for SRQ notification */
2258 if (data
->iin_buffer
[0] == 0x81) {
2259 unsigned long flags
;
2260 struct list_head
*elem
;
2263 kill_fasync(&data
->fasync
,
2266 spin_lock_irqsave(&data
->dev_lock
, flags
);
2267 list_for_each(elem
, &data
->file_list
) {
2268 struct usbtmc_file_data
*file_data
;
2270 file_data
= list_entry(elem
,
2271 struct usbtmc_file_data
,
2273 file_data
->srq_byte
= data
->iin_buffer
[1];
2274 atomic_set(&file_data
->srq_asserted
, 1);
2276 spin_unlock_irqrestore(&data
->dev_lock
, flags
);
2278 dev_dbg(dev
, "srq received bTag %x stb %x\n",
2279 (unsigned int)data
->iin_buffer
[0],
2280 (unsigned int)data
->iin_buffer
[1]);
2281 wake_up_interruptible_all(&data
->waitq
);
2284 dev_warn(dev
, "invalid notification: %x\n",
2285 data
->iin_buffer
[0]);
2288 dev_err(dev
, "overflow with length %d, actual length is %d\n",
2289 data
->iin_wMaxPacketSize
, urb
->actual_length
);
2297 /* urb terminated, clean up */
2298 dev_dbg(dev
, "urb terminated, status: %d\n", status
);
2301 dev_err(dev
, "unknown status received: %d\n", status
);
2304 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
2306 dev_err(dev
, "usb_submit_urb failed: %d\n", rv
);
2309 static void usbtmc_free_int(struct usbtmc_device_data
*data
)
2311 if (!data
->iin_ep_present
|| !data
->iin_urb
)
2313 usb_kill_urb(data
->iin_urb
);
2314 kfree(data
->iin_buffer
);
2315 data
->iin_buffer
= NULL
;
2316 usb_free_urb(data
->iin_urb
);
2317 data
->iin_urb
= NULL
;
2318 kref_put(&data
->kref
, usbtmc_delete
);
2321 static int usbtmc_probe(struct usb_interface
*intf
,
2322 const struct usb_device_id
*id
)
2324 struct usbtmc_device_data
*data
;
2325 struct usb_host_interface
*iface_desc
;
2326 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
, *int_in
;
2329 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
2331 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
2337 data
->usb_dev
= usb_get_dev(interface_to_usbdev(intf
));
2338 usb_set_intfdata(intf
, data
);
2339 kref_init(&data
->kref
);
2340 mutex_init(&data
->io_mutex
);
2341 init_waitqueue_head(&data
->waitq
);
2342 atomic_set(&data
->iin_data_valid
, 0);
2343 INIT_LIST_HEAD(&data
->file_list
);
2344 spin_lock_init(&data
->dev_lock
);
2348 /* Initialize USBTMC bTag and other fields */
2350 /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
2353 /* USBTMC devices have only one setting, so use that */
2354 iface_desc
= data
->intf
->cur_altsetting
;
2355 data
->ifnum
= iface_desc
->desc
.bInterfaceNumber
;
2357 /* Find bulk endpoints */
2358 retcode
= usb_find_common_endpoints(iface_desc
,
2359 &bulk_in
, &bulk_out
, NULL
, NULL
);
2361 dev_err(&intf
->dev
, "bulk endpoints not found\n");
2365 data
->bulk_in
= bulk_in
->bEndpointAddress
;
2366 data
->wMaxPacketSize
= usb_endpoint_maxp(bulk_in
);
2367 dev_dbg(&intf
->dev
, "Found bulk in endpoint at %u\n", data
->bulk_in
);
2369 data
->bulk_out
= bulk_out
->bEndpointAddress
;
2370 dev_dbg(&intf
->dev
, "Found Bulk out endpoint at %u\n", data
->bulk_out
);
2372 /* Find int endpoint */
2373 retcode
= usb_find_int_in_endpoint(iface_desc
, &int_in
);
2375 data
->iin_ep_present
= 1;
2376 data
->iin_ep
= int_in
->bEndpointAddress
;
2377 data
->iin_wMaxPacketSize
= usb_endpoint_maxp(int_in
);
2378 data
->iin_interval
= int_in
->bInterval
;
2379 dev_dbg(&intf
->dev
, "Found Int in endpoint at %u\n",
2383 retcode
= get_capabilities(data
);
2385 dev_err(&intf
->dev
, "can't read capabilities\n");
2387 retcode
= sysfs_create_group(&intf
->dev
.kobj
,
2388 &capability_attr_grp
);
2390 if (data
->iin_ep_present
) {
2391 /* allocate int urb */
2392 data
->iin_urb
= usb_alloc_urb(0, GFP_KERNEL
);
2393 if (!data
->iin_urb
) {
2395 goto error_register
;
2398 /* Protect interrupt in endpoint data until iin_urb is freed */
2399 kref_get(&data
->kref
);
2401 /* allocate buffer for interrupt in */
2402 data
->iin_buffer
= kmalloc(data
->iin_wMaxPacketSize
,
2404 if (!data
->iin_buffer
) {
2406 goto error_register
;
2409 /* fill interrupt urb */
2410 usb_fill_int_urb(data
->iin_urb
, data
->usb_dev
,
2411 usb_rcvintpipe(data
->usb_dev
, data
->iin_ep
),
2412 data
->iin_buffer
, data
->iin_wMaxPacketSize
,
2414 data
, data
->iin_interval
);
2416 retcode
= usb_submit_urb(data
->iin_urb
, GFP_KERNEL
);
2418 dev_err(&intf
->dev
, "Failed to submit iin_urb\n");
2419 goto error_register
;
2423 retcode
= usb_register_dev(intf
, &usbtmc_class
);
2425 dev_err(&intf
->dev
, "Not able to get a minor (base %u, slice default): %d\n",
2428 goto error_register
;
2430 dev_dbg(&intf
->dev
, "Using minor number %d\n", intf
->minor
);
2435 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
2436 usbtmc_free_int(data
);
2438 kref_put(&data
->kref
, usbtmc_delete
);
2442 static void usbtmc_disconnect(struct usb_interface
*intf
)
2444 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
2445 struct list_head
*elem
;
2447 usb_deregister_dev(intf
, &usbtmc_class
);
2448 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
2449 mutex_lock(&data
->io_mutex
);
2451 wake_up_interruptible_all(&data
->waitq
);
2452 list_for_each(elem
, &data
->file_list
) {
2453 struct usbtmc_file_data
*file_data
;
2455 file_data
= list_entry(elem
,
2456 struct usbtmc_file_data
,
2458 usb_kill_anchored_urbs(&file_data
->submitted
);
2459 usb_scuttle_anchored_urbs(&file_data
->in_anchor
);
2461 mutex_unlock(&data
->io_mutex
);
2462 usbtmc_free_int(data
);
2463 kref_put(&data
->kref
, usbtmc_delete
);
2466 static void usbtmc_draw_down(struct usbtmc_file_data
*file_data
)
2470 time
= usb_wait_anchor_empty_timeout(&file_data
->submitted
, 1000);
2472 usb_kill_anchored_urbs(&file_data
->submitted
);
2473 usb_scuttle_anchored_urbs(&file_data
->in_anchor
);
2476 static int usbtmc_suspend(struct usb_interface
*intf
, pm_message_t message
)
2478 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
2479 struct list_head
*elem
;
2484 mutex_lock(&data
->io_mutex
);
2485 list_for_each(elem
, &data
->file_list
) {
2486 struct usbtmc_file_data
*file_data
;
2488 file_data
= list_entry(elem
,
2489 struct usbtmc_file_data
,
2491 usbtmc_draw_down(file_data
);
2494 if (data
->iin_ep_present
&& data
->iin_urb
)
2495 usb_kill_urb(data
->iin_urb
);
2497 mutex_unlock(&data
->io_mutex
);
2501 static int usbtmc_resume(struct usb_interface
*intf
)
2503 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
2506 if (data
->iin_ep_present
&& data
->iin_urb
)
2507 retcode
= usb_submit_urb(data
->iin_urb
, GFP_KERNEL
);
2509 dev_err(&intf
->dev
, "Failed to submit iin_urb\n");
2514 static int usbtmc_pre_reset(struct usb_interface
*intf
)
2516 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
2517 struct list_head
*elem
;
2522 mutex_lock(&data
->io_mutex
);
2524 list_for_each(elem
, &data
->file_list
) {
2525 struct usbtmc_file_data
*file_data
;
2527 file_data
= list_entry(elem
,
2528 struct usbtmc_file_data
,
2530 usbtmc_ioctl_cancel_io(file_data
);
2536 static int usbtmc_post_reset(struct usb_interface
*intf
)
2538 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
2540 mutex_unlock(&data
->io_mutex
);
2545 static struct usb_driver usbtmc_driver
= {
2547 .id_table
= usbtmc_devices
,
2548 .probe
= usbtmc_probe
,
2549 .disconnect
= usbtmc_disconnect
,
2550 .suspend
= usbtmc_suspend
,
2551 .resume
= usbtmc_resume
,
2552 .pre_reset
= usbtmc_pre_reset
,
2553 .post_reset
= usbtmc_post_reset
,
2556 module_usb_driver(usbtmc_driver
);
2558 MODULE_LICENSE("GPL");