ALSA: usb-audio: mixer: volume quirk for ESS Technology Asus USB DAC
[linux/fpc-iii.git] / drivers / usb / class / usbtmc.c
blobe6a7c86b70f250ee4077948b2bdbcb2ec3d868d4
1 // SPDX-License-Identifier: GPL-2.0+
2 /**
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 */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/fs.h>
15 #include <linux/uaccess.h>
16 #include <linux/kref.h>
17 #include <linux/slab.h>
18 #include <linux/poll.h>
19 #include <linux/mutex.h>
20 #include <linux/usb.h>
21 #include <linux/compat.h>
22 #include <linux/usb/tmc.h>
25 #define USBTMC_HEADER_SIZE 12
26 #define USBTMC_MINOR_BASE 176
29 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
30 * large as wMaxPacketSize (which is usually 512 bytes).
32 #define USBTMC_SIZE_IOBUFFER 2048
34 /* Minimum USB timeout (in milliseconds) */
35 #define USBTMC_MIN_TIMEOUT 100
36 /* Default USB timeout (in milliseconds) */
37 #define USBTMC_TIMEOUT 5000
40 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
41 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
42 * packet is never read.
44 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
46 static const struct usb_device_id usbtmc_devices[] = {
47 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
48 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
49 { 0, } /* terminating entry */
51 MODULE_DEVICE_TABLE(usb, usbtmc_devices);
54 * This structure is the capabilities for the device
55 * See section 4.2.1.8 of the USBTMC specification,
56 * and section 4.2.2 of the USBTMC usb488 subclass
57 * specification for details.
59 struct usbtmc_dev_capabilities {
60 __u8 interface_capabilities;
61 __u8 device_capabilities;
62 __u8 usb488_interface_capabilities;
63 __u8 usb488_device_capabilities;
66 /* This structure holds private data for each USBTMC device. One copy is
67 * allocated for each USBTMC device in the driver's probe function.
69 struct usbtmc_device_data {
70 const struct usb_device_id *id;
71 struct usb_device *usb_dev;
72 struct usb_interface *intf;
73 struct list_head file_list;
75 unsigned int bulk_in;
76 unsigned int bulk_out;
78 u8 bTag;
79 u8 bTag_last_write; /* needed for abort */
80 u8 bTag_last_read; /* needed for abort */
82 /* data for interrupt in endpoint handling */
83 u8 bNotify1;
84 u8 bNotify2;
85 u16 ifnum;
86 u8 iin_bTag;
87 u8 *iin_buffer;
88 atomic_t iin_data_valid;
89 unsigned int iin_ep;
90 int iin_ep_present;
91 int iin_interval;
92 struct urb *iin_urb;
93 u16 iin_wMaxPacketSize;
95 /* coalesced usb488_caps from usbtmc_dev_capabilities */
96 __u8 usb488_caps;
98 /* attributes from the USB TMC spec for this device */
99 u8 TermChar;
100 bool TermCharEnabled;
101 bool auto_abort;
103 bool zombie; /* fd of disconnected device */
105 struct usbtmc_dev_capabilities capabilities;
106 struct kref kref;
107 struct mutex io_mutex; /* only one i/o function running at a time */
108 wait_queue_head_t waitq;
109 struct fasync_struct *fasync;
110 spinlock_t dev_lock; /* lock for file_list */
112 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
115 * This structure holds private data for each USBTMC file handle.
117 struct usbtmc_file_data {
118 struct usbtmc_device_data *data;
119 struct list_head file_elem;
121 u32 timeout;
122 u8 srq_byte;
123 atomic_t srq_asserted;
124 u8 eom_val;
125 u8 term_char;
126 bool term_char_enabled;
129 /* Forward declarations */
130 static struct usb_driver usbtmc_driver;
132 static void usbtmc_delete(struct kref *kref)
134 struct usbtmc_device_data *data = to_usbtmc_data(kref);
136 usb_put_dev(data->usb_dev);
137 kfree(data);
140 static int usbtmc_open(struct inode *inode, struct file *filp)
142 struct usb_interface *intf;
143 struct usbtmc_device_data *data;
144 struct usbtmc_file_data *file_data;
146 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
147 if (!intf) {
148 pr_err("can not find device for minor %d", iminor(inode));
149 return -ENODEV;
152 file_data = kzalloc(sizeof(*file_data), GFP_KERNEL);
153 if (!file_data)
154 return -ENOMEM;
156 data = usb_get_intfdata(intf);
157 /* Protect reference to data from file structure until release */
158 kref_get(&data->kref);
160 mutex_lock(&data->io_mutex);
161 file_data->data = data;
163 /* copy default values from device settings */
164 file_data->timeout = USBTMC_TIMEOUT;
165 file_data->term_char = data->TermChar;
166 file_data->term_char_enabled = data->TermCharEnabled;
167 file_data->eom_val = 1;
169 INIT_LIST_HEAD(&file_data->file_elem);
170 spin_lock_irq(&data->dev_lock);
171 list_add_tail(&file_data->file_elem, &data->file_list);
172 spin_unlock_irq(&data->dev_lock);
173 mutex_unlock(&data->io_mutex);
175 /* Store pointer in file structure's private data field */
176 filp->private_data = file_data;
178 return 0;
181 static int usbtmc_release(struct inode *inode, struct file *file)
183 struct usbtmc_file_data *file_data = file->private_data;
185 /* prevent IO _AND_ usbtmc_interrupt */
186 mutex_lock(&file_data->data->io_mutex);
187 spin_lock_irq(&file_data->data->dev_lock);
189 list_del(&file_data->file_elem);
191 spin_unlock_irq(&file_data->data->dev_lock);
192 mutex_unlock(&file_data->data->io_mutex);
194 kref_put(&file_data->data->kref, usbtmc_delete);
195 file_data->data = NULL;
196 kfree(file_data);
197 return 0;
200 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
202 u8 *buffer;
203 struct device *dev;
204 int rv;
205 int n;
206 int actual;
207 struct usb_host_interface *current_setting;
208 int max_size;
210 dev = &data->intf->dev;
211 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
212 if (!buffer)
213 return -ENOMEM;
215 rv = usb_control_msg(data->usb_dev,
216 usb_rcvctrlpipe(data->usb_dev, 0),
217 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
218 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
219 data->bTag_last_read, data->bulk_in,
220 buffer, 2, USBTMC_TIMEOUT);
222 if (rv < 0) {
223 dev_err(dev, "usb_control_msg returned %d\n", rv);
224 goto exit;
227 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
229 if (buffer[0] == USBTMC_STATUS_FAILED) {
230 rv = 0;
231 goto exit;
234 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
235 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
236 buffer[0]);
237 rv = -EPERM;
238 goto exit;
241 max_size = 0;
242 current_setting = data->intf->cur_altsetting;
243 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
244 if (current_setting->endpoint[n].desc.bEndpointAddress ==
245 data->bulk_in)
246 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
248 if (max_size == 0) {
249 dev_err(dev, "Couldn't get wMaxPacketSize\n");
250 rv = -EPERM;
251 goto exit;
254 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
256 n = 0;
258 do {
259 dev_dbg(dev, "Reading from bulk in EP\n");
261 rv = usb_bulk_msg(data->usb_dev,
262 usb_rcvbulkpipe(data->usb_dev,
263 data->bulk_in),
264 buffer, USBTMC_SIZE_IOBUFFER,
265 &actual, USBTMC_TIMEOUT);
267 n++;
269 if (rv < 0) {
270 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
271 goto exit;
273 } while ((actual == max_size) &&
274 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
276 if (actual == max_size) {
277 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
278 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
279 rv = -EPERM;
280 goto exit;
283 n = 0;
285 usbtmc_abort_bulk_in_status:
286 rv = usb_control_msg(data->usb_dev,
287 usb_rcvctrlpipe(data->usb_dev, 0),
288 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
289 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
290 0, data->bulk_in, buffer, 0x08,
291 USBTMC_TIMEOUT);
293 if (rv < 0) {
294 dev_err(dev, "usb_control_msg returned %d\n", rv);
295 goto exit;
298 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
300 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
301 rv = 0;
302 goto exit;
305 if (buffer[0] != USBTMC_STATUS_PENDING) {
306 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
307 rv = -EPERM;
308 goto exit;
311 if (buffer[1] == 1)
312 do {
313 dev_dbg(dev, "Reading from bulk in EP\n");
315 rv = usb_bulk_msg(data->usb_dev,
316 usb_rcvbulkpipe(data->usb_dev,
317 data->bulk_in),
318 buffer, USBTMC_SIZE_IOBUFFER,
319 &actual, USBTMC_TIMEOUT);
321 n++;
323 if (rv < 0) {
324 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
325 goto exit;
327 } while ((actual == max_size) &&
328 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
330 if (actual == max_size) {
331 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
332 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
333 rv = -EPERM;
334 goto exit;
337 goto usbtmc_abort_bulk_in_status;
339 exit:
340 kfree(buffer);
341 return rv;
345 static int usbtmc_ioctl_abort_bulk_out_tag(struct usbtmc_device_data *data,
346 u8 tag)
348 struct device *dev;
349 u8 *buffer;
350 int rv;
351 int n;
353 dev = &data->intf->dev;
355 buffer = kmalloc(8, GFP_KERNEL);
356 if (!buffer)
357 return -ENOMEM;
359 rv = usb_control_msg(data->usb_dev,
360 usb_rcvctrlpipe(data->usb_dev, 0),
361 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
362 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
363 tag, data->bulk_out,
364 buffer, 2, USB_CTRL_GET_TIMEOUT);
366 if (rv < 0) {
367 dev_err(dev, "usb_control_msg returned %d\n", rv);
368 goto exit;
371 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
373 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
374 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
375 buffer[0]);
376 rv = -EPERM;
377 goto exit;
380 n = 0;
382 usbtmc_abort_bulk_out_check_status:
383 /* do not stress device with subsequent requests */
384 msleep(50);
385 rv = usb_control_msg(data->usb_dev,
386 usb_rcvctrlpipe(data->usb_dev, 0),
387 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
388 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
389 0, data->bulk_out, buffer, 0x08,
390 USB_CTRL_GET_TIMEOUT);
391 n++;
392 if (rv < 0) {
393 dev_err(dev, "usb_control_msg returned %d\n", rv);
394 goto exit;
397 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
399 if (buffer[0] == USBTMC_STATUS_SUCCESS)
400 goto usbtmc_abort_bulk_out_clear_halt;
402 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
403 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
404 goto usbtmc_abort_bulk_out_check_status;
406 rv = -EPERM;
407 goto exit;
409 usbtmc_abort_bulk_out_clear_halt:
410 rv = usb_clear_halt(data->usb_dev,
411 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
413 if (rv < 0) {
414 dev_err(dev, "usb_control_msg returned %d\n", rv);
415 goto exit;
417 rv = 0;
419 exit:
420 kfree(buffer);
421 return rv;
424 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
426 return usbtmc_ioctl_abort_bulk_out_tag(data, data->bTag_last_write);
429 static int usbtmc488_ioctl_read_stb(struct usbtmc_file_data *file_data,
430 void __user *arg)
432 struct usbtmc_device_data *data = file_data->data;
433 struct device *dev = &data->intf->dev;
434 int srq_asserted = 0;
435 u8 *buffer;
436 u8 tag;
437 __u8 stb;
438 int rv;
440 dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n",
441 data->iin_ep_present);
443 spin_lock_irq(&data->dev_lock);
444 srq_asserted = atomic_xchg(&file_data->srq_asserted, srq_asserted);
445 if (srq_asserted) {
446 /* a STB with SRQ is already received */
447 stb = file_data->srq_byte;
448 spin_unlock_irq(&data->dev_lock);
449 rv = put_user(stb, (__u8 __user *)arg);
450 dev_dbg(dev, "stb:0x%02x with srq received %d\n",
451 (unsigned int)stb, rv);
452 return rv;
454 spin_unlock_irq(&data->dev_lock);
456 buffer = kmalloc(8, GFP_KERNEL);
457 if (!buffer)
458 return -ENOMEM;
460 atomic_set(&data->iin_data_valid, 0);
462 rv = usb_control_msg(data->usb_dev,
463 usb_rcvctrlpipe(data->usb_dev, 0),
464 USBTMC488_REQUEST_READ_STATUS_BYTE,
465 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
466 data->iin_bTag,
467 data->ifnum,
468 buffer, 0x03, USBTMC_TIMEOUT);
469 if (rv < 0) {
470 dev_err(dev, "stb usb_control_msg returned %d\n", rv);
471 goto exit;
474 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
475 dev_err(dev, "control status returned %x\n", buffer[0]);
476 rv = -EIO;
477 goto exit;
480 if (data->iin_ep_present) {
481 rv = wait_event_interruptible_timeout(
482 data->waitq,
483 atomic_read(&data->iin_data_valid) != 0,
484 file_data->timeout);
485 if (rv < 0) {
486 dev_dbg(dev, "wait interrupted %d\n", rv);
487 goto exit;
490 if (rv == 0) {
491 dev_dbg(dev, "wait timed out\n");
492 rv = -ETIMEDOUT;
493 goto exit;
496 tag = data->bNotify1 & 0x7f;
497 if (tag != data->iin_bTag) {
498 dev_err(dev, "expected bTag %x got %x\n",
499 data->iin_bTag, tag);
502 stb = data->bNotify2;
503 } else {
504 stb = buffer[2];
507 rv = put_user(stb, (__u8 __user *)arg);
508 dev_dbg(dev, "stb:0x%02x received %d\n", (unsigned int)stb, rv);
510 exit:
511 /* bump interrupt bTag */
512 data->iin_bTag += 1;
513 if (data->iin_bTag > 127)
514 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
515 data->iin_bTag = 2;
517 kfree(buffer);
518 return rv;
521 static int usbtmc488_ioctl_simple(struct usbtmc_device_data *data,
522 void __user *arg, unsigned int cmd)
524 struct device *dev = &data->intf->dev;
525 __u8 val;
526 u8 *buffer;
527 u16 wValue;
528 int rv;
530 if (!(data->usb488_caps & USBTMC488_CAPABILITY_SIMPLE))
531 return -EINVAL;
533 buffer = kmalloc(8, GFP_KERNEL);
534 if (!buffer)
535 return -ENOMEM;
537 if (cmd == USBTMC488_REQUEST_REN_CONTROL) {
538 rv = copy_from_user(&val, arg, sizeof(val));
539 if (rv) {
540 rv = -EFAULT;
541 goto exit;
543 wValue = val ? 1 : 0;
544 } else {
545 wValue = 0;
548 rv = usb_control_msg(data->usb_dev,
549 usb_rcvctrlpipe(data->usb_dev, 0),
550 cmd,
551 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
552 wValue,
553 data->ifnum,
554 buffer, 0x01, USBTMC_TIMEOUT);
555 if (rv < 0) {
556 dev_err(dev, "simple usb_control_msg failed %d\n", rv);
557 goto exit;
558 } else if (rv != 1) {
559 dev_warn(dev, "simple usb_control_msg returned %d\n", rv);
560 rv = -EIO;
561 goto exit;
564 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
565 dev_err(dev, "simple control status returned %x\n", buffer[0]);
566 rv = -EIO;
567 goto exit;
569 rv = 0;
571 exit:
572 kfree(buffer);
573 return rv;
577 * Sends a TRIGGER Bulk-OUT command message
578 * See the USBTMC-USB488 specification, Table 2.
580 * Also updates bTag_last_write.
582 static int usbtmc488_ioctl_trigger(struct usbtmc_file_data *file_data)
584 struct usbtmc_device_data *data = file_data->data;
585 int retval;
586 u8 *buffer;
587 int actual;
589 buffer = kzalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
590 if (!buffer)
591 return -ENOMEM;
593 buffer[0] = 128;
594 buffer[1] = data->bTag;
595 buffer[2] = ~data->bTag;
597 retval = usb_bulk_msg(data->usb_dev,
598 usb_sndbulkpipe(data->usb_dev,
599 data->bulk_out),
600 buffer, USBTMC_HEADER_SIZE,
601 &actual, file_data->timeout);
603 /* Store bTag (in case we need to abort) */
604 data->bTag_last_write = data->bTag;
606 /* Increment bTag -- and increment again if zero */
607 data->bTag++;
608 if (!data->bTag)
609 data->bTag++;
611 kfree(buffer);
612 if (retval < 0) {
613 dev_err(&data->intf->dev, "%s returned %d\n",
614 __func__, retval);
615 return retval;
618 return 0;
622 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-OUT endpoint.
623 * @transfer_size: number of bytes to request from the device.
625 * See the USBTMC specification, Table 4.
627 * Also updates bTag_last_write.
629 static int send_request_dev_dep_msg_in(struct usbtmc_file_data *file_data,
630 size_t transfer_size)
632 struct usbtmc_device_data *data = file_data->data;
633 int retval;
634 u8 *buffer;
635 int actual;
637 buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
638 if (!buffer)
639 return -ENOMEM;
640 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
641 * Refer to class specs for details
643 buffer[0] = 2;
644 buffer[1] = data->bTag;
645 buffer[2] = ~data->bTag;
646 buffer[3] = 0; /* Reserved */
647 buffer[4] = transfer_size >> 0;
648 buffer[5] = transfer_size >> 8;
649 buffer[6] = transfer_size >> 16;
650 buffer[7] = transfer_size >> 24;
651 buffer[8] = file_data->term_char_enabled * 2;
652 /* Use term character? */
653 buffer[9] = file_data->term_char;
654 buffer[10] = 0; /* Reserved */
655 buffer[11] = 0; /* Reserved */
657 /* Send bulk URB */
658 retval = usb_bulk_msg(data->usb_dev,
659 usb_sndbulkpipe(data->usb_dev,
660 data->bulk_out),
661 buffer, USBTMC_HEADER_SIZE,
662 &actual, file_data->timeout);
664 /* Store bTag (in case we need to abort) */
665 data->bTag_last_write = data->bTag;
667 /* Increment bTag -- and increment again if zero */
668 data->bTag++;
669 if (!data->bTag)
670 data->bTag++;
672 kfree(buffer);
673 if (retval < 0) {
674 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
675 return retval;
678 return 0;
681 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
682 size_t count, loff_t *f_pos)
684 struct usbtmc_file_data *file_data;
685 struct usbtmc_device_data *data;
686 struct device *dev;
687 u32 n_characters;
688 u8 *buffer;
689 int actual;
690 size_t done;
691 size_t remaining;
692 int retval;
693 size_t this_part;
695 /* Get pointer to private data structure */
696 file_data = filp->private_data;
697 data = file_data->data;
698 dev = &data->intf->dev;
700 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
701 if (!buffer)
702 return -ENOMEM;
704 mutex_lock(&data->io_mutex);
705 if (data->zombie) {
706 retval = -ENODEV;
707 goto exit;
710 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
712 retval = send_request_dev_dep_msg_in(file_data, count);
714 if (retval < 0) {
715 if (data->auto_abort)
716 usbtmc_ioctl_abort_bulk_out(data);
717 goto exit;
720 /* Loop until we have fetched everything we requested */
721 remaining = count;
722 this_part = remaining;
723 done = 0;
725 while (remaining > 0) {
726 /* Send bulk URB */
727 retval = usb_bulk_msg(data->usb_dev,
728 usb_rcvbulkpipe(data->usb_dev,
729 data->bulk_in),
730 buffer, USBTMC_SIZE_IOBUFFER, &actual,
731 file_data->timeout);
733 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
735 /* Store bTag (in case we need to abort) */
736 data->bTag_last_read = data->bTag;
738 if (retval < 0) {
739 dev_dbg(dev, "Unable to read data, error %d\n", retval);
740 if (data->auto_abort)
741 usbtmc_ioctl_abort_bulk_in(data);
742 goto exit;
745 /* Parse header in first packet */
746 if (done == 0) {
747 /* Sanity checks for the header */
748 if (actual < USBTMC_HEADER_SIZE) {
749 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
750 if (data->auto_abort)
751 usbtmc_ioctl_abort_bulk_in(data);
752 goto exit;
755 if (buffer[0] != 2) {
756 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
757 if (data->auto_abort)
758 usbtmc_ioctl_abort_bulk_in(data);
759 goto exit;
762 if (buffer[1] != data->bTag_last_write) {
763 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
764 if (data->auto_abort)
765 usbtmc_ioctl_abort_bulk_in(data);
766 goto exit;
769 /* How many characters did the instrument send? */
770 n_characters = buffer[4] +
771 (buffer[5] << 8) +
772 (buffer[6] << 16) +
773 (buffer[7] << 24);
775 if (n_characters > this_part) {
776 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
777 if (data->auto_abort)
778 usbtmc_ioctl_abort_bulk_in(data);
779 goto exit;
782 /* Remove the USBTMC header */
783 actual -= USBTMC_HEADER_SIZE;
785 /* Check if the message is smaller than requested */
786 if (remaining > n_characters)
787 remaining = n_characters;
788 /* Remove padding if it exists */
789 if (actual > remaining)
790 actual = remaining;
792 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
794 remaining -= actual;
796 /* Terminate if end-of-message bit received from device */
797 if ((buffer[8] & 0x01) && (actual >= n_characters))
798 remaining = 0;
800 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
803 /* Copy buffer to user space */
804 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
805 /* There must have been an addressing problem */
806 retval = -EFAULT;
807 goto exit;
809 done += actual;
811 else {
812 if (actual > remaining)
813 actual = remaining;
815 remaining -= actual;
817 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
819 /* Copy buffer to user space */
820 if (copy_to_user(buf + done, buffer, actual)) {
821 /* There must have been an addressing problem */
822 retval = -EFAULT;
823 goto exit;
825 done += actual;
829 /* Update file position value */
830 *f_pos = *f_pos + done;
831 retval = done;
833 exit:
834 mutex_unlock(&data->io_mutex);
835 kfree(buffer);
836 return retval;
839 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
840 size_t count, loff_t *f_pos)
842 struct usbtmc_file_data *file_data;
843 struct usbtmc_device_data *data;
844 u8 *buffer;
845 int retval;
846 int actual;
847 unsigned long int n_bytes;
848 int remaining;
849 int done;
850 int this_part;
852 file_data = filp->private_data;
853 data = file_data->data;
855 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
856 if (!buffer)
857 return -ENOMEM;
859 mutex_lock(&data->io_mutex);
860 if (data->zombie) {
861 retval = -ENODEV;
862 goto exit;
865 remaining = count;
866 done = 0;
868 while (remaining > 0) {
869 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
870 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
871 buffer[8] = 0;
872 } else {
873 this_part = remaining;
874 buffer[8] = file_data->eom_val;
877 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
878 buffer[0] = 1;
879 buffer[1] = data->bTag;
880 buffer[2] = ~data->bTag;
881 buffer[3] = 0; /* Reserved */
882 buffer[4] = this_part >> 0;
883 buffer[5] = this_part >> 8;
884 buffer[6] = this_part >> 16;
885 buffer[7] = this_part >> 24;
886 /* buffer[8] is set above... */
887 buffer[9] = 0; /* Reserved */
888 buffer[10] = 0; /* Reserved */
889 buffer[11] = 0; /* Reserved */
891 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
892 retval = -EFAULT;
893 goto exit;
896 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
897 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
899 do {
900 retval = usb_bulk_msg(data->usb_dev,
901 usb_sndbulkpipe(data->usb_dev,
902 data->bulk_out),
903 buffer, n_bytes,
904 &actual, file_data->timeout);
905 if (retval != 0)
906 break;
907 n_bytes -= actual;
908 } while (n_bytes);
910 data->bTag_last_write = data->bTag;
911 data->bTag++;
913 if (!data->bTag)
914 data->bTag++;
916 if (retval < 0) {
917 dev_err(&data->intf->dev,
918 "Unable to send data, error %d\n", retval);
919 if (data->auto_abort)
920 usbtmc_ioctl_abort_bulk_out(data);
921 goto exit;
924 remaining -= this_part;
925 done += this_part;
928 retval = count;
929 exit:
930 mutex_unlock(&data->io_mutex);
931 kfree(buffer);
932 return retval;
935 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
937 struct usb_host_interface *current_setting;
938 struct usb_endpoint_descriptor *desc;
939 struct device *dev;
940 u8 *buffer;
941 int rv;
942 int n;
943 int actual = 0;
944 int max_size;
946 dev = &data->intf->dev;
948 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
950 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
951 if (!buffer)
952 return -ENOMEM;
954 rv = usb_control_msg(data->usb_dev,
955 usb_rcvctrlpipe(data->usb_dev, 0),
956 USBTMC_REQUEST_INITIATE_CLEAR,
957 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
958 0, 0, buffer, 1, USBTMC_TIMEOUT);
959 if (rv < 0) {
960 dev_err(dev, "usb_control_msg returned %d\n", rv);
961 goto exit;
964 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
966 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
967 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
968 rv = -EPERM;
969 goto exit;
972 max_size = 0;
973 current_setting = data->intf->cur_altsetting;
974 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
975 desc = &current_setting->endpoint[n].desc;
976 if (desc->bEndpointAddress == data->bulk_in)
977 max_size = usb_endpoint_maxp(desc);
980 if (max_size == 0) {
981 dev_err(dev, "Couldn't get wMaxPacketSize\n");
982 rv = -EPERM;
983 goto exit;
986 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
988 n = 0;
990 usbtmc_clear_check_status:
992 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
994 rv = usb_control_msg(data->usb_dev,
995 usb_rcvctrlpipe(data->usb_dev, 0),
996 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
997 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
998 0, 0, buffer, 2, USBTMC_TIMEOUT);
999 if (rv < 0) {
1000 dev_err(dev, "usb_control_msg returned %d\n", rv);
1001 goto exit;
1004 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
1006 if (buffer[0] == USBTMC_STATUS_SUCCESS)
1007 goto usbtmc_clear_bulk_out_halt;
1009 if (buffer[0] != USBTMC_STATUS_PENDING) {
1010 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
1011 rv = -EPERM;
1012 goto exit;
1015 if (buffer[1] == 1)
1016 do {
1017 dev_dbg(dev, "Reading from bulk in EP\n");
1019 actual = 0;
1020 rv = usb_bulk_msg(data->usb_dev,
1021 usb_rcvbulkpipe(data->usb_dev,
1022 data->bulk_in),
1023 buffer, USBTMC_SIZE_IOBUFFER,
1024 &actual, USBTMC_TIMEOUT);
1025 n++;
1027 if (rv < 0) {
1028 dev_err(dev, "usb_control_msg returned %d\n",
1029 rv);
1030 goto exit;
1032 } while ((actual == max_size) &&
1033 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
1035 if (actual == max_size) {
1036 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
1037 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
1038 rv = -EPERM;
1039 goto exit;
1042 goto usbtmc_clear_check_status;
1044 usbtmc_clear_bulk_out_halt:
1046 rv = usb_clear_halt(data->usb_dev,
1047 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
1048 if (rv < 0) {
1049 dev_err(dev, "usb_control_msg returned %d\n", rv);
1050 goto exit;
1052 rv = 0;
1054 exit:
1055 kfree(buffer);
1056 return rv;
1059 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
1061 int rv;
1063 rv = usb_clear_halt(data->usb_dev,
1064 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
1066 if (rv < 0) {
1067 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
1068 rv);
1069 return rv;
1071 return 0;
1074 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
1076 int rv;
1078 rv = usb_clear_halt(data->usb_dev,
1079 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
1081 if (rv < 0) {
1082 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
1083 rv);
1084 return rv;
1086 return 0;
1089 static int get_capabilities(struct usbtmc_device_data *data)
1091 struct device *dev = &data->usb_dev->dev;
1092 char *buffer;
1093 int rv = 0;
1095 buffer = kmalloc(0x18, GFP_KERNEL);
1096 if (!buffer)
1097 return -ENOMEM;
1099 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
1100 USBTMC_REQUEST_GET_CAPABILITIES,
1101 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1102 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
1103 if (rv < 0) {
1104 dev_err(dev, "usb_control_msg returned %d\n", rv);
1105 goto err_out;
1108 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
1109 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1110 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
1111 rv = -EPERM;
1112 goto err_out;
1114 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
1115 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
1116 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
1117 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
1119 data->capabilities.interface_capabilities = buffer[4];
1120 data->capabilities.device_capabilities = buffer[5];
1121 data->capabilities.usb488_interface_capabilities = buffer[14];
1122 data->capabilities.usb488_device_capabilities = buffer[15];
1123 data->usb488_caps = (buffer[14] & 0x07) | ((buffer[15] & 0x0f) << 4);
1124 rv = 0;
1126 err_out:
1127 kfree(buffer);
1128 return rv;
1131 #define capability_attribute(name) \
1132 static ssize_t name##_show(struct device *dev, \
1133 struct device_attribute *attr, char *buf) \
1135 struct usb_interface *intf = to_usb_interface(dev); \
1136 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1138 return sprintf(buf, "%d\n", data->capabilities.name); \
1140 static DEVICE_ATTR_RO(name)
1142 capability_attribute(interface_capabilities);
1143 capability_attribute(device_capabilities);
1144 capability_attribute(usb488_interface_capabilities);
1145 capability_attribute(usb488_device_capabilities);
1147 static struct attribute *capability_attrs[] = {
1148 &dev_attr_interface_capabilities.attr,
1149 &dev_attr_device_capabilities.attr,
1150 &dev_attr_usb488_interface_capabilities.attr,
1151 &dev_attr_usb488_device_capabilities.attr,
1152 NULL,
1155 static const struct attribute_group capability_attr_grp = {
1156 .attrs = capability_attrs,
1159 static ssize_t TermChar_show(struct device *dev,
1160 struct device_attribute *attr, char *buf)
1162 struct usb_interface *intf = to_usb_interface(dev);
1163 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1165 return sprintf(buf, "%c\n", data->TermChar);
1168 static ssize_t TermChar_store(struct device *dev,
1169 struct device_attribute *attr,
1170 const char *buf, size_t count)
1172 struct usb_interface *intf = to_usb_interface(dev);
1173 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1175 if (count < 1)
1176 return -EINVAL;
1177 data->TermChar = buf[0];
1178 return count;
1180 static DEVICE_ATTR_RW(TermChar);
1182 #define data_attribute(name) \
1183 static ssize_t name##_show(struct device *dev, \
1184 struct device_attribute *attr, char *buf) \
1186 struct usb_interface *intf = to_usb_interface(dev); \
1187 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1189 return sprintf(buf, "%d\n", data->name); \
1191 static ssize_t name##_store(struct device *dev, \
1192 struct device_attribute *attr, \
1193 const char *buf, size_t count) \
1195 struct usb_interface *intf = to_usb_interface(dev); \
1196 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1197 ssize_t result; \
1198 unsigned val; \
1200 result = sscanf(buf, "%u\n", &val); \
1201 if (result != 1) \
1202 result = -EINVAL; \
1203 data->name = val; \
1204 if (result < 0) \
1205 return result; \
1206 else \
1207 return count; \
1209 static DEVICE_ATTR_RW(name)
1211 data_attribute(TermCharEnabled);
1212 data_attribute(auto_abort);
1214 static struct attribute *data_attrs[] = {
1215 &dev_attr_TermChar.attr,
1216 &dev_attr_TermCharEnabled.attr,
1217 &dev_attr_auto_abort.attr,
1218 NULL,
1221 static const struct attribute_group data_attr_grp = {
1222 .attrs = data_attrs,
1225 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
1227 struct device *dev;
1228 u8 *buffer;
1229 int rv;
1231 dev = &data->intf->dev;
1233 buffer = kmalloc(2, GFP_KERNEL);
1234 if (!buffer)
1235 return -ENOMEM;
1237 rv = usb_control_msg(data->usb_dev,
1238 usb_rcvctrlpipe(data->usb_dev, 0),
1239 USBTMC_REQUEST_INDICATOR_PULSE,
1240 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1241 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1243 if (rv < 0) {
1244 dev_err(dev, "usb_control_msg returned %d\n", rv);
1245 goto exit;
1248 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1250 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1251 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1252 rv = -EPERM;
1253 goto exit;
1255 rv = 0;
1257 exit:
1258 kfree(buffer);
1259 return rv;
1263 * Get the usb timeout value
1265 static int usbtmc_ioctl_get_timeout(struct usbtmc_file_data *file_data,
1266 void __user *arg)
1268 u32 timeout;
1270 timeout = file_data->timeout;
1272 return put_user(timeout, (__u32 __user *)arg);
1276 * Set the usb timeout value
1278 static int usbtmc_ioctl_set_timeout(struct usbtmc_file_data *file_data,
1279 void __user *arg)
1281 u32 timeout;
1283 if (get_user(timeout, (__u32 __user *)arg))
1284 return -EFAULT;
1286 /* Note that timeout = 0 means
1287 * MAX_SCHEDULE_TIMEOUT in usb_control_msg
1289 if (timeout < USBTMC_MIN_TIMEOUT)
1290 return -EINVAL;
1292 file_data->timeout = timeout;
1294 return 0;
1298 * enables/disables sending EOM on write
1300 static int usbtmc_ioctl_eom_enable(struct usbtmc_file_data *file_data,
1301 void __user *arg)
1303 u8 eom_enable;
1305 if (copy_from_user(&eom_enable, arg, sizeof(eom_enable)))
1306 return -EFAULT;
1308 if (eom_enable > 1)
1309 return -EINVAL;
1311 file_data->eom_val = eom_enable;
1313 return 0;
1317 * Configure termination character for read()
1319 static int usbtmc_ioctl_config_termc(struct usbtmc_file_data *file_data,
1320 void __user *arg)
1322 struct usbtmc_termchar termc;
1324 if (copy_from_user(&termc, arg, sizeof(termc)))
1325 return -EFAULT;
1327 if ((termc.term_char_enabled > 1) ||
1328 (termc.term_char_enabled &&
1329 !(file_data->data->capabilities.device_capabilities & 1)))
1330 return -EINVAL;
1332 file_data->term_char = termc.term_char;
1333 file_data->term_char_enabled = termc.term_char_enabled;
1335 return 0;
1338 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1340 struct usbtmc_file_data *file_data;
1341 struct usbtmc_device_data *data;
1342 int retval = -EBADRQC;
1344 file_data = file->private_data;
1345 data = file_data->data;
1347 mutex_lock(&data->io_mutex);
1348 if (data->zombie) {
1349 retval = -ENODEV;
1350 goto skip_io_on_zombie;
1353 switch (cmd) {
1354 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1355 retval = usbtmc_ioctl_clear_out_halt(data);
1356 break;
1358 case USBTMC_IOCTL_CLEAR_IN_HALT:
1359 retval = usbtmc_ioctl_clear_in_halt(data);
1360 break;
1362 case USBTMC_IOCTL_INDICATOR_PULSE:
1363 retval = usbtmc_ioctl_indicator_pulse(data);
1364 break;
1366 case USBTMC_IOCTL_CLEAR:
1367 retval = usbtmc_ioctl_clear(data);
1368 break;
1370 case USBTMC_IOCTL_ABORT_BULK_OUT:
1371 retval = usbtmc_ioctl_abort_bulk_out(data);
1372 break;
1374 case USBTMC_IOCTL_ABORT_BULK_IN:
1375 retval = usbtmc_ioctl_abort_bulk_in(data);
1376 break;
1378 case USBTMC_IOCTL_GET_TIMEOUT:
1379 retval = usbtmc_ioctl_get_timeout(file_data,
1380 (void __user *)arg);
1381 break;
1383 case USBTMC_IOCTL_SET_TIMEOUT:
1384 retval = usbtmc_ioctl_set_timeout(file_data,
1385 (void __user *)arg);
1386 break;
1388 case USBTMC_IOCTL_EOM_ENABLE:
1389 retval = usbtmc_ioctl_eom_enable(file_data,
1390 (void __user *)arg);
1391 break;
1393 case USBTMC_IOCTL_CONFIG_TERMCHAR:
1394 retval = usbtmc_ioctl_config_termc(file_data,
1395 (void __user *)arg);
1396 break;
1398 case USBTMC488_IOCTL_GET_CAPS:
1399 retval = copy_to_user((void __user *)arg,
1400 &data->usb488_caps,
1401 sizeof(data->usb488_caps));
1402 if (retval)
1403 retval = -EFAULT;
1404 break;
1406 case USBTMC488_IOCTL_READ_STB:
1407 retval = usbtmc488_ioctl_read_stb(file_data,
1408 (void __user *)arg);
1409 break;
1411 case USBTMC488_IOCTL_REN_CONTROL:
1412 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1413 USBTMC488_REQUEST_REN_CONTROL);
1414 break;
1416 case USBTMC488_IOCTL_GOTO_LOCAL:
1417 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1418 USBTMC488_REQUEST_GOTO_LOCAL);
1419 break;
1421 case USBTMC488_IOCTL_LOCAL_LOCKOUT:
1422 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1423 USBTMC488_REQUEST_LOCAL_LOCKOUT);
1424 break;
1426 case USBTMC488_IOCTL_TRIGGER:
1427 retval = usbtmc488_ioctl_trigger(file_data);
1428 break;
1431 skip_io_on_zombie:
1432 mutex_unlock(&data->io_mutex);
1433 return retval;
1436 static int usbtmc_fasync(int fd, struct file *file, int on)
1438 struct usbtmc_file_data *file_data = file->private_data;
1440 return fasync_helper(fd, file, on, &file_data->data->fasync);
1443 static __poll_t usbtmc_poll(struct file *file, poll_table *wait)
1445 struct usbtmc_file_data *file_data = file->private_data;
1446 struct usbtmc_device_data *data = file_data->data;
1447 __poll_t mask;
1449 mutex_lock(&data->io_mutex);
1451 if (data->zombie) {
1452 mask = EPOLLHUP | EPOLLERR;
1453 goto no_poll;
1456 poll_wait(file, &data->waitq, wait);
1458 mask = (atomic_read(&file_data->srq_asserted)) ? EPOLLPRI : 0;
1460 no_poll:
1461 mutex_unlock(&data->io_mutex);
1462 return mask;
1465 static const struct file_operations fops = {
1466 .owner = THIS_MODULE,
1467 .read = usbtmc_read,
1468 .write = usbtmc_write,
1469 .open = usbtmc_open,
1470 .release = usbtmc_release,
1471 .unlocked_ioctl = usbtmc_ioctl,
1472 #ifdef CONFIG_COMPAT
1473 .compat_ioctl = usbtmc_ioctl,
1474 #endif
1475 .fasync = usbtmc_fasync,
1476 .poll = usbtmc_poll,
1477 .llseek = default_llseek,
1480 static struct usb_class_driver usbtmc_class = {
1481 .name = "usbtmc%d",
1482 .fops = &fops,
1483 .minor_base = USBTMC_MINOR_BASE,
1486 static void usbtmc_interrupt(struct urb *urb)
1488 struct usbtmc_device_data *data = urb->context;
1489 struct device *dev = &data->intf->dev;
1490 int status = urb->status;
1491 int rv;
1493 dev_dbg(&data->intf->dev, "int status: %d len %d\n",
1494 status, urb->actual_length);
1496 switch (status) {
1497 case 0: /* SUCCESS */
1498 /* check for valid STB notification */
1499 if (data->iin_buffer[0] > 0x81) {
1500 data->bNotify1 = data->iin_buffer[0];
1501 data->bNotify2 = data->iin_buffer[1];
1502 atomic_set(&data->iin_data_valid, 1);
1503 wake_up_interruptible(&data->waitq);
1504 goto exit;
1506 /* check for SRQ notification */
1507 if (data->iin_buffer[0] == 0x81) {
1508 unsigned long flags;
1509 struct list_head *elem;
1511 if (data->fasync)
1512 kill_fasync(&data->fasync,
1513 SIGIO, POLL_PRI);
1515 spin_lock_irqsave(&data->dev_lock, flags);
1516 list_for_each(elem, &data->file_list) {
1517 struct usbtmc_file_data *file_data;
1519 file_data = list_entry(elem,
1520 struct usbtmc_file_data,
1521 file_elem);
1522 file_data->srq_byte = data->iin_buffer[1];
1523 atomic_set(&file_data->srq_asserted, 1);
1525 spin_unlock_irqrestore(&data->dev_lock, flags);
1527 dev_dbg(dev, "srq received bTag %x stb %x\n",
1528 (unsigned int)data->iin_buffer[0],
1529 (unsigned int)data->iin_buffer[1]);
1530 wake_up_interruptible_all(&data->waitq);
1531 goto exit;
1533 dev_warn(dev, "invalid notification: %x\n",
1534 data->iin_buffer[0]);
1535 break;
1536 case -EOVERFLOW:
1537 dev_err(dev, "overflow with length %d, actual length is %d\n",
1538 data->iin_wMaxPacketSize, urb->actual_length);
1539 /* fall through */
1540 case -ECONNRESET:
1541 case -ENOENT:
1542 case -ESHUTDOWN:
1543 case -EILSEQ:
1544 case -ETIME:
1545 case -EPIPE:
1546 /* urb terminated, clean up */
1547 dev_dbg(dev, "urb terminated, status: %d\n", status);
1548 return;
1549 default:
1550 dev_err(dev, "unknown status received: %d\n", status);
1552 exit:
1553 rv = usb_submit_urb(urb, GFP_ATOMIC);
1554 if (rv)
1555 dev_err(dev, "usb_submit_urb failed: %d\n", rv);
1558 static void usbtmc_free_int(struct usbtmc_device_data *data)
1560 if (!data->iin_ep_present || !data->iin_urb)
1561 return;
1562 usb_kill_urb(data->iin_urb);
1563 kfree(data->iin_buffer);
1564 usb_free_urb(data->iin_urb);
1565 kref_put(&data->kref, usbtmc_delete);
1568 static int usbtmc_probe(struct usb_interface *intf,
1569 const struct usb_device_id *id)
1571 struct usbtmc_device_data *data;
1572 struct usb_host_interface *iface_desc;
1573 struct usb_endpoint_descriptor *bulk_in, *bulk_out, *int_in;
1574 int retcode;
1576 dev_dbg(&intf->dev, "%s called\n", __func__);
1578 data = kzalloc(sizeof(*data), GFP_KERNEL);
1579 if (!data)
1580 return -ENOMEM;
1582 data->intf = intf;
1583 data->id = id;
1584 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1585 usb_set_intfdata(intf, data);
1586 kref_init(&data->kref);
1587 mutex_init(&data->io_mutex);
1588 init_waitqueue_head(&data->waitq);
1589 atomic_set(&data->iin_data_valid, 0);
1590 INIT_LIST_HEAD(&data->file_list);
1591 spin_lock_init(&data->dev_lock);
1593 data->zombie = 0;
1595 /* Initialize USBTMC bTag and other fields */
1596 data->bTag = 1;
1597 data->TermCharEnabled = 0;
1598 data->TermChar = '\n';
1599 /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
1600 data->iin_bTag = 2;
1602 /* USBTMC devices have only one setting, so use that */
1603 iface_desc = data->intf->cur_altsetting;
1604 data->ifnum = iface_desc->desc.bInterfaceNumber;
1606 /* Find bulk endpoints */
1607 retcode = usb_find_common_endpoints(iface_desc,
1608 &bulk_in, &bulk_out, NULL, NULL);
1609 if (retcode) {
1610 dev_err(&intf->dev, "bulk endpoints not found\n");
1611 goto err_put;
1614 data->bulk_in = bulk_in->bEndpointAddress;
1615 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n", data->bulk_in);
1617 data->bulk_out = bulk_out->bEndpointAddress;
1618 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n", data->bulk_out);
1620 /* Find int endpoint */
1621 retcode = usb_find_int_in_endpoint(iface_desc, &int_in);
1622 if (!retcode) {
1623 data->iin_ep_present = 1;
1624 data->iin_ep = int_in->bEndpointAddress;
1625 data->iin_wMaxPacketSize = usb_endpoint_maxp(int_in);
1626 data->iin_interval = int_in->bInterval;
1627 dev_dbg(&intf->dev, "Found Int in endpoint at %u\n",
1628 data->iin_ep);
1631 retcode = get_capabilities(data);
1632 if (retcode)
1633 dev_err(&intf->dev, "can't read capabilities\n");
1634 else
1635 retcode = sysfs_create_group(&intf->dev.kobj,
1636 &capability_attr_grp);
1638 if (data->iin_ep_present) {
1639 /* allocate int urb */
1640 data->iin_urb = usb_alloc_urb(0, GFP_KERNEL);
1641 if (!data->iin_urb) {
1642 retcode = -ENOMEM;
1643 goto error_register;
1646 /* Protect interrupt in endpoint data until iin_urb is freed */
1647 kref_get(&data->kref);
1649 /* allocate buffer for interrupt in */
1650 data->iin_buffer = kmalloc(data->iin_wMaxPacketSize,
1651 GFP_KERNEL);
1652 if (!data->iin_buffer) {
1653 retcode = -ENOMEM;
1654 goto error_register;
1657 /* fill interrupt urb */
1658 usb_fill_int_urb(data->iin_urb, data->usb_dev,
1659 usb_rcvintpipe(data->usb_dev, data->iin_ep),
1660 data->iin_buffer, data->iin_wMaxPacketSize,
1661 usbtmc_interrupt,
1662 data, data->iin_interval);
1664 retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL);
1665 if (retcode) {
1666 dev_err(&intf->dev, "Failed to submit iin_urb\n");
1667 goto error_register;
1671 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1673 retcode = usb_register_dev(intf, &usbtmc_class);
1674 if (retcode) {
1675 dev_err(&intf->dev, "Not able to get a minor"
1676 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1677 retcode);
1678 goto error_register;
1680 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1682 return 0;
1684 error_register:
1685 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1686 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1687 usbtmc_free_int(data);
1688 err_put:
1689 kref_put(&data->kref, usbtmc_delete);
1690 return retcode;
1693 static void usbtmc_disconnect(struct usb_interface *intf)
1695 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1697 usb_deregister_dev(intf, &usbtmc_class);
1698 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1699 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1700 mutex_lock(&data->io_mutex);
1701 data->zombie = 1;
1702 wake_up_interruptible_all(&data->waitq);
1703 mutex_unlock(&data->io_mutex);
1704 usbtmc_free_int(data);
1705 kref_put(&data->kref, usbtmc_delete);
1708 static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
1710 /* this driver does not have pending URBs */
1711 return 0;
1714 static int usbtmc_resume(struct usb_interface *intf)
1716 return 0;
1719 static struct usb_driver usbtmc_driver = {
1720 .name = "usbtmc",
1721 .id_table = usbtmc_devices,
1722 .probe = usbtmc_probe,
1723 .disconnect = usbtmc_disconnect,
1724 .suspend = usbtmc_suspend,
1725 .resume = usbtmc_resume,
1728 module_usb_driver(usbtmc_driver);
1730 MODULE_LICENSE("GPL");