Version:  2.0.40 2.2.26 2.4.37 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9

Linux/drivers/usb/class/usbtmc.c

  1 /**
  2  * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
  3  *
  4  * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
  5  * Copyright (C) 2008 Novell, Inc.
  6  * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
  7  *
  8  * This program is free software; you can redistribute it and/or
  9  * modify it under the terms of the GNU General Public License
 10  * as published by the Free Software Foundation; either version 2
 11  * of the License, or (at your option) any later version.
 12  *
 13  * This program is distributed in the hope that it will be useful,
 14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  * GNU General Public License for more details.
 17  *
 18  * The GNU General Public License is available at
 19  * http://www.gnu.org/copyleft/gpl.html.
 20  */
 21 
 22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 23 
 24 #include <linux/module.h>
 25 #include <linux/kernel.h>
 26 #include <linux/fs.h>
 27 #include <linux/uaccess.h>
 28 #include <linux/kref.h>
 29 #include <linux/slab.h>
 30 #include <linux/poll.h>
 31 #include <linux/mutex.h>
 32 #include <linux/usb.h>
 33 #include <linux/usb/tmc.h>
 34 
 35 
 36 #define RIGOL                   1
 37 #define USBTMC_HEADER_SIZE      12
 38 #define USBTMC_MINOR_BASE       176
 39 
 40 /*
 41  * Size of driver internal IO buffer. Must be multiple of 4 and at least as
 42  * large as wMaxPacketSize (which is usually 512 bytes).
 43  */
 44 #define USBTMC_SIZE_IOBUFFER    2048
 45 
 46 /* Default USB timeout (in milliseconds) */
 47 #define USBTMC_TIMEOUT          5000
 48 
 49 /*
 50  * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
 51  * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
 52  * packet is never read.
 53  */
 54 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN       100
 55 
 56 static const struct usb_device_id usbtmc_devices[] = {
 57         { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
 58         { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
 59         { 0, } /* terminating entry */
 60 };
 61 MODULE_DEVICE_TABLE(usb, usbtmc_devices);
 62 
 63 /*
 64  * This structure is the capabilities for the device
 65  * See section 4.2.1.8 of the USBTMC specification,
 66  * and section 4.2.2 of the USBTMC usb488 subclass
 67  * specification for details.
 68  */
 69 struct usbtmc_dev_capabilities {
 70         __u8 interface_capabilities;
 71         __u8 device_capabilities;
 72         __u8 usb488_interface_capabilities;
 73         __u8 usb488_device_capabilities;
 74 };
 75 
 76 /* This structure holds private data for each USBTMC device. One copy is
 77  * allocated for each USBTMC device in the driver's probe function.
 78  */
 79 struct usbtmc_device_data {
 80         const struct usb_device_id *id;
 81         struct usb_device *usb_dev;
 82         struct usb_interface *intf;
 83 
 84         unsigned int bulk_in;
 85         unsigned int bulk_out;
 86 
 87         u8 bTag;
 88         u8 bTag_last_write;     /* needed for abort */
 89         u8 bTag_last_read;      /* needed for abort */
 90 
 91         /* data for interrupt in endpoint handling */
 92         u8             bNotify1;
 93         u8             bNotify2;
 94         u16            ifnum;
 95         u8             iin_bTag;
 96         u8            *iin_buffer;
 97         atomic_t       iin_data_valid;
 98         unsigned int   iin_ep;
 99         int            iin_ep_present;
100         int            iin_interval;
101         struct urb    *iin_urb;
102         u16            iin_wMaxPacketSize;
103         atomic_t       srq_asserted;
104 
105         /* coalesced usb488_caps from usbtmc_dev_capabilities */
106         __u8 usb488_caps;
107 
108         u8 rigol_quirk;
109 
110         /* attributes from the USB TMC spec for this device */
111         u8 TermChar;
112         bool TermCharEnabled;
113         bool auto_abort;
114 
115         bool zombie; /* fd of disconnected device */
116 
117         struct usbtmc_dev_capabilities  capabilities;
118         struct kref kref;
119         struct mutex io_mutex;  /* only one i/o function running at a time */
120         wait_queue_head_t waitq;
121         struct fasync_struct *fasync;
122 };
123 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
124 
125 struct usbtmc_ID_rigol_quirk {
126         __u16 idVendor;
127         __u16 idProduct;
128 };
129 
130 static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
131         { 0x1ab1, 0x0588 },
132         { 0x1ab1, 0x04b0 },
133         { 0, 0 }
134 };
135 
136 /* Forward declarations */
137 static struct usb_driver usbtmc_driver;
138 
139 static void usbtmc_delete(struct kref *kref)
140 {
141         struct usbtmc_device_data *data = to_usbtmc_data(kref);
142 
143         usb_put_dev(data->usb_dev);
144         kfree(data);
145 }
146 
147 static int usbtmc_open(struct inode *inode, struct file *filp)
148 {
149         struct usb_interface *intf;
150         struct usbtmc_device_data *data;
151         int retval = 0;
152 
153         intf = usb_find_interface(&usbtmc_driver, iminor(inode));
154         if (!intf) {
155                 pr_err("can not find device for minor %d", iminor(inode));
156                 return -ENODEV;
157         }
158 
159         data = usb_get_intfdata(intf);
160         kref_get(&data->kref);
161 
162         /* Store pointer in file structure's private data field */
163         filp->private_data = data;
164 
165         return retval;
166 }
167 
168 static int usbtmc_release(struct inode *inode, struct file *file)
169 {
170         struct usbtmc_device_data *data = file->private_data;
171 
172         kref_put(&data->kref, usbtmc_delete);
173         return 0;
174 }
175 
176 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
177 {
178         u8 *buffer;
179         struct device *dev;
180         int rv;
181         int n;
182         int actual;
183         struct usb_host_interface *current_setting;
184         int max_size;
185 
186         dev = &data->intf->dev;
187         buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
188         if (!buffer)
189                 return -ENOMEM;
190 
191         rv = usb_control_msg(data->usb_dev,
192                              usb_rcvctrlpipe(data->usb_dev, 0),
193                              USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
194                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
195                              data->bTag_last_read, data->bulk_in,
196                              buffer, 2, USBTMC_TIMEOUT);
197 
198         if (rv < 0) {
199                 dev_err(dev, "usb_control_msg returned %d\n", rv);
200                 goto exit;
201         }
202 
203         dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
204 
205         if (buffer[0] == USBTMC_STATUS_FAILED) {
206                 rv = 0;
207                 goto exit;
208         }
209 
210         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
211                 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
212                         buffer[0]);
213                 rv = -EPERM;
214                 goto exit;
215         }
216 
217         max_size = 0;
218         current_setting = data->intf->cur_altsetting;
219         for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
220                 if (current_setting->endpoint[n].desc.bEndpointAddress ==
221                         data->bulk_in)
222                         max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
223 
224         if (max_size == 0) {
225                 dev_err(dev, "Couldn't get wMaxPacketSize\n");
226                 rv = -EPERM;
227                 goto exit;
228         }
229 
230         dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
231 
232         n = 0;
233 
234         do {
235                 dev_dbg(dev, "Reading from bulk in EP\n");
236 
237                 rv = usb_bulk_msg(data->usb_dev,
238                                   usb_rcvbulkpipe(data->usb_dev,
239                                                   data->bulk_in),
240                                   buffer, USBTMC_SIZE_IOBUFFER,
241                                   &actual, USBTMC_TIMEOUT);
242 
243                 n++;
244 
245                 if (rv < 0) {
246                         dev_err(dev, "usb_bulk_msg returned %d\n", rv);
247                         goto exit;
248                 }
249         } while ((actual == max_size) &&
250                  (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
251 
252         if (actual == max_size) {
253                 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
254                         USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
255                 rv = -EPERM;
256                 goto exit;
257         }
258 
259         n = 0;
260 
261 usbtmc_abort_bulk_in_status:
262         rv = usb_control_msg(data->usb_dev,
263                              usb_rcvctrlpipe(data->usb_dev, 0),
264                              USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
265                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
266                              0, data->bulk_in, buffer, 0x08,
267                              USBTMC_TIMEOUT);
268 
269         if (rv < 0) {
270                 dev_err(dev, "usb_control_msg returned %d\n", rv);
271                 goto exit;
272         }
273 
274         dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
275 
276         if (buffer[0] == USBTMC_STATUS_SUCCESS) {
277                 rv = 0;
278                 goto exit;
279         }
280 
281         if (buffer[0] != USBTMC_STATUS_PENDING) {
282                 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
283                 rv = -EPERM;
284                 goto exit;
285         }
286 
287         if (buffer[1] == 1)
288                 do {
289                         dev_dbg(dev, "Reading from bulk in EP\n");
290 
291                         rv = usb_bulk_msg(data->usb_dev,
292                                           usb_rcvbulkpipe(data->usb_dev,
293                                                           data->bulk_in),
294                                           buffer, USBTMC_SIZE_IOBUFFER,
295                                           &actual, USBTMC_TIMEOUT);
296 
297                         n++;
298 
299                         if (rv < 0) {
300                                 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
301                                 goto exit;
302                         }
303                 } while ((actual == max_size) &&
304                          (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
305 
306         if (actual == max_size) {
307                 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
308                         USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
309                 rv = -EPERM;
310                 goto exit;
311         }
312 
313         goto usbtmc_abort_bulk_in_status;
314 
315 exit:
316         kfree(buffer);
317         return rv;
318 
319 }
320 
321 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
322 {
323         struct device *dev;
324         u8 *buffer;
325         int rv;
326         int n;
327 
328         dev = &data->intf->dev;
329 
330         buffer = kmalloc(8, GFP_KERNEL);
331         if (!buffer)
332                 return -ENOMEM;
333 
334         rv = usb_control_msg(data->usb_dev,
335                              usb_rcvctrlpipe(data->usb_dev, 0),
336                              USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
337                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
338                              data->bTag_last_write, data->bulk_out,
339                              buffer, 2, USBTMC_TIMEOUT);
340 
341         if (rv < 0) {
342                 dev_err(dev, "usb_control_msg returned %d\n", rv);
343                 goto exit;
344         }
345 
346         dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
347 
348         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
349                 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
350                         buffer[0]);
351                 rv = -EPERM;
352                 goto exit;
353         }
354 
355         n = 0;
356 
357 usbtmc_abort_bulk_out_check_status:
358         rv = usb_control_msg(data->usb_dev,
359                              usb_rcvctrlpipe(data->usb_dev, 0),
360                              USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
361                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
362                              0, data->bulk_out, buffer, 0x08,
363                              USBTMC_TIMEOUT);
364         n++;
365         if (rv < 0) {
366                 dev_err(dev, "usb_control_msg returned %d\n", rv);
367                 goto exit;
368         }
369 
370         dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
371 
372         if (buffer[0] == USBTMC_STATUS_SUCCESS)
373                 goto usbtmc_abort_bulk_out_clear_halt;
374 
375         if ((buffer[0] == USBTMC_STATUS_PENDING) &&
376             (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
377                 goto usbtmc_abort_bulk_out_check_status;
378 
379         rv = -EPERM;
380         goto exit;
381 
382 usbtmc_abort_bulk_out_clear_halt:
383         rv = usb_clear_halt(data->usb_dev,
384                             usb_sndbulkpipe(data->usb_dev, data->bulk_out));
385 
386         if (rv < 0) {
387                 dev_err(dev, "usb_control_msg returned %d\n", rv);
388                 goto exit;
389         }
390         rv = 0;
391 
392 exit:
393         kfree(buffer);
394         return rv;
395 }
396 
397 static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data *data,
398                                 void __user *arg)
399 {
400         struct device *dev = &data->intf->dev;
401         u8 *buffer;
402         u8 tag;
403         __u8 stb;
404         int rv;
405 
406         dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n",
407                 data->iin_ep_present);
408 
409         buffer = kmalloc(8, GFP_KERNEL);
410         if (!buffer)
411                 return -ENOMEM;
412 
413         atomic_set(&data->iin_data_valid, 0);
414 
415         /* must issue read_stb before using poll or select */
416         atomic_set(&data->srq_asserted, 0);
417 
418         rv = usb_control_msg(data->usb_dev,
419                         usb_rcvctrlpipe(data->usb_dev, 0),
420                         USBTMC488_REQUEST_READ_STATUS_BYTE,
421                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
422                         data->iin_bTag,
423                         data->ifnum,
424                         buffer, 0x03, USBTMC_TIMEOUT);
425         if (rv < 0) {
426                 dev_err(dev, "stb usb_control_msg returned %d\n", rv);
427                 goto exit;
428         }
429 
430         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
431                 dev_err(dev, "control status returned %x\n", buffer[0]);
432                 rv = -EIO;
433                 goto exit;
434         }
435 
436         if (data->iin_ep_present) {
437                 rv = wait_event_interruptible_timeout(
438                         data->waitq,
439                         atomic_read(&data->iin_data_valid) != 0,
440                         USBTMC_TIMEOUT);
441                 if (rv < 0) {
442                         dev_dbg(dev, "wait interrupted %d\n", rv);
443                         goto exit;
444                 }
445 
446                 if (rv == 0) {
447                         dev_dbg(dev, "wait timed out\n");
448                         rv = -ETIME;
449                         goto exit;
450                 }
451 
452                 tag = data->bNotify1 & 0x7f;
453                 if (tag != data->iin_bTag) {
454                         dev_err(dev, "expected bTag %x got %x\n",
455                                 data->iin_bTag, tag);
456                 }
457 
458                 stb = data->bNotify2;
459         } else {
460                 stb = buffer[2];
461         }
462 
463         rv = copy_to_user(arg, &stb, sizeof(stb));
464         if (rv)
465                 rv = -EFAULT;
466 
467  exit:
468         /* bump interrupt bTag */
469         data->iin_bTag += 1;
470         if (data->iin_bTag > 127)
471                 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
472                 data->iin_bTag = 2;
473 
474         kfree(buffer);
475         return rv;
476 }
477 
478 static int usbtmc488_ioctl_simple(struct usbtmc_device_data *data,
479                                 void __user *arg, unsigned int cmd)
480 {
481         struct device *dev = &data->intf->dev;
482         __u8 val;
483         u8 *buffer;
484         u16 wValue;
485         int rv;
486 
487         if (!(data->usb488_caps & USBTMC488_CAPABILITY_SIMPLE))
488                 return -EINVAL;
489 
490         buffer = kmalloc(8, GFP_KERNEL);
491         if (!buffer)
492                 return -ENOMEM;
493 
494         if (cmd == USBTMC488_REQUEST_REN_CONTROL) {
495                 rv = copy_from_user(&val, arg, sizeof(val));
496                 if (rv) {
497                         rv = -EFAULT;
498                         goto exit;
499                 }
500                 wValue = val ? 1 : 0;
501         } else {
502                 wValue = 0;
503         }
504 
505         rv = usb_control_msg(data->usb_dev,
506                         usb_rcvctrlpipe(data->usb_dev, 0),
507                         cmd,
508                         USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
509                         wValue,
510                         data->ifnum,
511                         buffer, 0x01, USBTMC_TIMEOUT);
512         if (rv < 0) {
513                 dev_err(dev, "simple usb_control_msg failed %d\n", rv);
514                 goto exit;
515         } else if (rv != 1) {
516                 dev_warn(dev, "simple usb_control_msg returned %d\n", rv);
517                 rv = -EIO;
518                 goto exit;
519         }
520 
521         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
522                 dev_err(dev, "simple control status returned %x\n", buffer[0]);
523                 rv = -EIO;
524                 goto exit;
525         }
526         rv = 0;
527 
528  exit:
529         kfree(buffer);
530         return rv;
531 }
532 
533 /*
534  * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
535  * @transfer_size: number of bytes to request from the device.
536  *
537  * See the USBTMC specification, Table 4.
538  *
539  * Also updates bTag_last_write.
540  */
541 static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
542 {
543         int retval;
544         u8 *buffer;
545         int actual;
546 
547         buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
548         if (!buffer)
549                 return -ENOMEM;
550         /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
551          * Refer to class specs for details
552          */
553         buffer[0] = 2;
554         buffer[1] = data->bTag;
555         buffer[2] = ~data->bTag;
556         buffer[3] = 0; /* Reserved */
557         buffer[4] = transfer_size >> 0;
558         buffer[5] = transfer_size >> 8;
559         buffer[6] = transfer_size >> 16;
560         buffer[7] = transfer_size >> 24;
561         buffer[8] = data->TermCharEnabled * 2;
562         /* Use term character? */
563         buffer[9] = data->TermChar;
564         buffer[10] = 0; /* Reserved */
565         buffer[11] = 0; /* Reserved */
566 
567         /* Send bulk URB */
568         retval = usb_bulk_msg(data->usb_dev,
569                               usb_sndbulkpipe(data->usb_dev,
570                                               data->bulk_out),
571                               buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
572 
573         /* Store bTag (in case we need to abort) */
574         data->bTag_last_write = data->bTag;
575 
576         /* Increment bTag -- and increment again if zero */
577         data->bTag++;
578         if (!data->bTag)
579                 data->bTag++;
580 
581         kfree(buffer);
582         if (retval < 0) {
583                 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
584                 return retval;
585         }
586 
587         return 0;
588 }
589 
590 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
591                            size_t count, loff_t *f_pos)
592 {
593         struct usbtmc_device_data *data;
594         struct device *dev;
595         u32 n_characters;
596         u8 *buffer;
597         int actual;
598         size_t done;
599         size_t remaining;
600         int retval;
601         size_t this_part;
602 
603         /* Get pointer to private data structure */
604         data = filp->private_data;
605         dev = &data->intf->dev;
606 
607         buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
608         if (!buffer)
609                 return -ENOMEM;
610 
611         mutex_lock(&data->io_mutex);
612         if (data->zombie) {
613                 retval = -ENODEV;
614                 goto exit;
615         }
616 
617         if (data->rigol_quirk) {
618                 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
619 
620                 retval = send_request_dev_dep_msg_in(data, count);
621 
622                 if (retval < 0) {
623                         if (data->auto_abort)
624                                 usbtmc_ioctl_abort_bulk_out(data);
625                         goto exit;
626                 }
627         }
628 
629         /* Loop until we have fetched everything we requested */
630         remaining = count;
631         this_part = remaining;
632         done = 0;
633 
634         while (remaining > 0) {
635                 if (!data->rigol_quirk) {
636                         dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
637 
638                         if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
639                                 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
640                         else
641                                 this_part = remaining;
642 
643                         retval = send_request_dev_dep_msg_in(data, this_part);
644                         if (retval < 0) {
645                         dev_err(dev, "usb_bulk_msg returned %d\n", retval);
646                                 if (data->auto_abort)
647                                         usbtmc_ioctl_abort_bulk_out(data);
648                                 goto exit;
649                         }
650                 }
651 
652                 /* Send bulk URB */
653                 retval = usb_bulk_msg(data->usb_dev,
654                                       usb_rcvbulkpipe(data->usb_dev,
655                                                       data->bulk_in),
656                                       buffer, USBTMC_SIZE_IOBUFFER, &actual,
657                                       USBTMC_TIMEOUT);
658 
659                 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
660 
661                 /* Store bTag (in case we need to abort) */
662                 data->bTag_last_read = data->bTag;
663 
664                 if (retval < 0) {
665                         dev_dbg(dev, "Unable to read data, error %d\n", retval);
666                         if (data->auto_abort)
667                                 usbtmc_ioctl_abort_bulk_in(data);
668                         goto exit;
669                 }
670 
671                 /* Parse header in first packet */
672                 if ((done == 0) || !data->rigol_quirk) {
673                         /* Sanity checks for the header */
674                         if (actual < USBTMC_HEADER_SIZE) {
675                                 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
676                                 if (data->auto_abort)
677                                         usbtmc_ioctl_abort_bulk_in(data);
678                                 goto exit;
679                         }
680 
681                         if (buffer[0] != 2) {
682                                 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
683                                 if (data->auto_abort)
684                                         usbtmc_ioctl_abort_bulk_in(data);
685                                 goto exit;
686                         }
687 
688                         if (buffer[1] != data->bTag_last_write) {
689                                 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
690                                 if (data->auto_abort)
691                                         usbtmc_ioctl_abort_bulk_in(data);
692                                 goto exit;
693                         }
694 
695                         /* How many characters did the instrument send? */
696                         n_characters = buffer[4] +
697                                        (buffer[5] << 8) +
698                                        (buffer[6] << 16) +
699                                        (buffer[7] << 24);
700 
701                         if (n_characters > this_part) {
702                                 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
703                                 if (data->auto_abort)
704                                         usbtmc_ioctl_abort_bulk_in(data);
705                                 goto exit;
706                         }
707 
708                         /* Remove the USBTMC header */
709                         actual -= USBTMC_HEADER_SIZE;
710 
711                         /* Check if the message is smaller than requested */
712                         if (data->rigol_quirk) {
713                                 if (remaining > n_characters)
714                                         remaining = n_characters;
715                                 /* Remove padding if it exists */
716                                 if (actual > remaining)
717                                         actual = remaining;
718                         }
719                         else {
720                                 if (this_part > n_characters)
721                                         this_part = n_characters;
722                                 /* Remove padding if it exists */
723                                 if (actual > this_part)
724                                         actual = this_part;
725                         }
726 
727                         dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
728 
729                         remaining -= actual;
730 
731                         /* Terminate if end-of-message bit received from device */
732                         if ((buffer[8] & 0x01) && (actual >= n_characters))
733                                 remaining = 0;
734 
735                         dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
736 
737 
738                         /* Copy buffer to user space */
739                         if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
740                                 /* There must have been an addressing problem */
741                                 retval = -EFAULT;
742                                 goto exit;
743                         }
744                         done += actual;
745                 }
746                 else  {
747                         if (actual > remaining)
748                                 actual = remaining;
749 
750                         remaining -= actual;
751 
752                         dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
753 
754                         /* Copy buffer to user space */
755                         if (copy_to_user(buf + done, buffer, actual)) {
756                                 /* There must have been an addressing problem */
757                                 retval = -EFAULT;
758                                 goto exit;
759                         }
760                         done += actual;
761                 }
762         }
763 
764         /* Update file position value */
765         *f_pos = *f_pos + done;
766         retval = done;
767 
768 exit:
769         mutex_unlock(&data->io_mutex);
770         kfree(buffer);
771         return retval;
772 }
773 
774 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
775                             size_t count, loff_t *f_pos)
776 {
777         struct usbtmc_device_data *data;
778         u8 *buffer;
779         int retval;
780         int actual;
781         unsigned long int n_bytes;
782         int remaining;
783         int done;
784         int this_part;
785 
786         data = filp->private_data;
787 
788         buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
789         if (!buffer)
790                 return -ENOMEM;
791 
792         mutex_lock(&data->io_mutex);
793         if (data->zombie) {
794                 retval = -ENODEV;
795                 goto exit;
796         }
797 
798         remaining = count;
799         done = 0;
800 
801         while (remaining > 0) {
802                 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
803                         this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
804                         buffer[8] = 0;
805                 } else {
806                         this_part = remaining;
807                         buffer[8] = 1;
808                 }
809 
810                 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
811                 buffer[0] = 1;
812                 buffer[1] = data->bTag;
813                 buffer[2] = ~data->bTag;
814                 buffer[3] = 0; /* Reserved */
815                 buffer[4] = this_part >> 0;
816                 buffer[5] = this_part >> 8;
817                 buffer[6] = this_part >> 16;
818                 buffer[7] = this_part >> 24;
819                 /* buffer[8] is set above... */
820                 buffer[9] = 0; /* Reserved */
821                 buffer[10] = 0; /* Reserved */
822                 buffer[11] = 0; /* Reserved */
823 
824                 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
825                         retval = -EFAULT;
826                         goto exit;
827                 }
828 
829                 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
830                 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
831 
832                 do {
833                         retval = usb_bulk_msg(data->usb_dev,
834                                               usb_sndbulkpipe(data->usb_dev,
835                                                               data->bulk_out),
836                                               buffer, n_bytes,
837                                               &actual, USBTMC_TIMEOUT);
838                         if (retval != 0)
839                                 break;
840                         n_bytes -= actual;
841                 } while (n_bytes);
842 
843                 data->bTag_last_write = data->bTag;
844                 data->bTag++;
845 
846                 if (!data->bTag)
847                         data->bTag++;
848 
849                 if (retval < 0) {
850                         dev_err(&data->intf->dev,
851                                 "Unable to send data, error %d\n", retval);
852                         if (data->auto_abort)
853                                 usbtmc_ioctl_abort_bulk_out(data);
854                         goto exit;
855                 }
856 
857                 remaining -= this_part;
858                 done += this_part;
859         }
860 
861         retval = count;
862 exit:
863         mutex_unlock(&data->io_mutex);
864         kfree(buffer);
865         return retval;
866 }
867 
868 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
869 {
870         struct usb_host_interface *current_setting;
871         struct usb_endpoint_descriptor *desc;
872         struct device *dev;
873         u8 *buffer;
874         int rv;
875         int n;
876         int actual = 0;
877         int max_size;
878 
879         dev = &data->intf->dev;
880 
881         dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
882 
883         buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
884         if (!buffer)
885                 return -ENOMEM;
886 
887         rv = usb_control_msg(data->usb_dev,
888                              usb_rcvctrlpipe(data->usb_dev, 0),
889                              USBTMC_REQUEST_INITIATE_CLEAR,
890                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
891                              0, 0, buffer, 1, USBTMC_TIMEOUT);
892         if (rv < 0) {
893                 dev_err(dev, "usb_control_msg returned %d\n", rv);
894                 goto exit;
895         }
896 
897         dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
898 
899         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
900                 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
901                 rv = -EPERM;
902                 goto exit;
903         }
904 
905         max_size = 0;
906         current_setting = data->intf->cur_altsetting;
907         for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
908                 desc = &current_setting->endpoint[n].desc;
909                 if (desc->bEndpointAddress == data->bulk_in)
910                         max_size = usb_endpoint_maxp(desc);
911         }
912 
913         if (max_size == 0) {
914                 dev_err(dev, "Couldn't get wMaxPacketSize\n");
915                 rv = -EPERM;
916                 goto exit;
917         }
918 
919         dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
920 
921         n = 0;
922 
923 usbtmc_clear_check_status:
924 
925         dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
926 
927         rv = usb_control_msg(data->usb_dev,
928                              usb_rcvctrlpipe(data->usb_dev, 0),
929                              USBTMC_REQUEST_CHECK_CLEAR_STATUS,
930                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
931                              0, 0, buffer, 2, USBTMC_TIMEOUT);
932         if (rv < 0) {
933                 dev_err(dev, "usb_control_msg returned %d\n", rv);
934                 goto exit;
935         }
936 
937         dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
938 
939         if (buffer[0] == USBTMC_STATUS_SUCCESS)
940                 goto usbtmc_clear_bulk_out_halt;
941 
942         if (buffer[0] != USBTMC_STATUS_PENDING) {
943                 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
944                 rv = -EPERM;
945                 goto exit;
946         }
947 
948         if (buffer[1] == 1)
949                 do {
950                         dev_dbg(dev, "Reading from bulk in EP\n");
951 
952                         rv = usb_bulk_msg(data->usb_dev,
953                                           usb_rcvbulkpipe(data->usb_dev,
954                                                           data->bulk_in),
955                                           buffer, USBTMC_SIZE_IOBUFFER,
956                                           &actual, USBTMC_TIMEOUT);
957                         n++;
958 
959                         if (rv < 0) {
960                                 dev_err(dev, "usb_control_msg returned %d\n",
961                                         rv);
962                                 goto exit;
963                         }
964                 } while ((actual == max_size) &&
965                           (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
966 
967         if (actual == max_size) {
968                 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
969                         USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
970                 rv = -EPERM;
971                 goto exit;
972         }
973 
974         goto usbtmc_clear_check_status;
975 
976 usbtmc_clear_bulk_out_halt:
977 
978         rv = usb_clear_halt(data->usb_dev,
979                             usb_sndbulkpipe(data->usb_dev, data->bulk_out));
980         if (rv < 0) {
981                 dev_err(dev, "usb_control_msg returned %d\n", rv);
982                 goto exit;
983         }
984         rv = 0;
985 
986 exit:
987         kfree(buffer);
988         return rv;
989 }
990 
991 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
992 {
993         int rv;
994 
995         rv = usb_clear_halt(data->usb_dev,
996                             usb_sndbulkpipe(data->usb_dev, data->bulk_out));
997 
998         if (rv < 0) {
999                 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
1000                         rv);
1001                 return rv;
1002         }
1003         return 0;
1004 }
1005 
1006 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
1007 {
1008         int rv;
1009 
1010         rv = usb_clear_halt(data->usb_dev,
1011                             usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
1012 
1013         if (rv < 0) {
1014                 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
1015                         rv);
1016                 return rv;
1017         }
1018         return 0;
1019 }
1020 
1021 static int get_capabilities(struct usbtmc_device_data *data)
1022 {
1023         struct device *dev = &data->usb_dev->dev;
1024         char *buffer;
1025         int rv = 0;
1026 
1027         buffer = kmalloc(0x18, GFP_KERNEL);
1028         if (!buffer)
1029                 return -ENOMEM;
1030 
1031         rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
1032                              USBTMC_REQUEST_GET_CAPABILITIES,
1033                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1034                              0, 0, buffer, 0x18, USBTMC_TIMEOUT);
1035         if (rv < 0) {
1036                 dev_err(dev, "usb_control_msg returned %d\n", rv);
1037                 goto err_out;
1038         }
1039 
1040         dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
1041         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1042                 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
1043                 rv = -EPERM;
1044                 goto err_out;
1045         }
1046         dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
1047         dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
1048         dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
1049         dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
1050 
1051         data->capabilities.interface_capabilities = buffer[4];
1052         data->capabilities.device_capabilities = buffer[5];
1053         data->capabilities.usb488_interface_capabilities = buffer[14];
1054         data->capabilities.usb488_device_capabilities = buffer[15];
1055         data->usb488_caps = (buffer[14] & 0x07) | ((buffer[15] & 0x0f) << 4);
1056         rv = 0;
1057 
1058 err_out:
1059         kfree(buffer);
1060         return rv;
1061 }
1062 
1063 #define capability_attribute(name)                                      \
1064 static ssize_t name##_show(struct device *dev,                          \
1065                            struct device_attribute *attr, char *buf)    \
1066 {                                                                       \
1067         struct usb_interface *intf = to_usb_interface(dev);             \
1068         struct usbtmc_device_data *data = usb_get_intfdata(intf);       \
1069                                                                         \
1070         return sprintf(buf, "%d\n", data->capabilities.name);           \
1071 }                                                                       \
1072 static DEVICE_ATTR_RO(name)
1073 
1074 capability_attribute(interface_capabilities);
1075 capability_attribute(device_capabilities);
1076 capability_attribute(usb488_interface_capabilities);
1077 capability_attribute(usb488_device_capabilities);
1078 
1079 static struct attribute *capability_attrs[] = {
1080         &dev_attr_interface_capabilities.attr,
1081         &dev_attr_device_capabilities.attr,
1082         &dev_attr_usb488_interface_capabilities.attr,
1083         &dev_attr_usb488_device_capabilities.attr,
1084         NULL,
1085 };
1086 
1087 static struct attribute_group capability_attr_grp = {
1088         .attrs = capability_attrs,
1089 };
1090 
1091 static ssize_t TermChar_show(struct device *dev,
1092                              struct device_attribute *attr, char *buf)
1093 {
1094         struct usb_interface *intf = to_usb_interface(dev);
1095         struct usbtmc_device_data *data = usb_get_intfdata(intf);
1096 
1097         return sprintf(buf, "%c\n", data->TermChar);
1098 }
1099 
1100 static ssize_t TermChar_store(struct device *dev,
1101                               struct device_attribute *attr,
1102                               const char *buf, size_t count)
1103 {
1104         struct usb_interface *intf = to_usb_interface(dev);
1105         struct usbtmc_device_data *data = usb_get_intfdata(intf);
1106 
1107         if (count < 1)
1108                 return -EINVAL;
1109         data->TermChar = buf[0];
1110         return count;
1111 }
1112 static DEVICE_ATTR_RW(TermChar);
1113 
1114 #define data_attribute(name)                                            \
1115 static ssize_t name##_show(struct device *dev,                          \
1116                            struct device_attribute *attr, char *buf)    \
1117 {                                                                       \
1118         struct usb_interface *intf = to_usb_interface(dev);             \
1119         struct usbtmc_device_data *data = usb_get_intfdata(intf);       \
1120                                                                         \
1121         return sprintf(buf, "%d\n", data->name);                        \
1122 }                                                                       \
1123 static ssize_t name##_store(struct device *dev,                         \
1124                             struct device_attribute *attr,              \
1125                             const char *buf, size_t count)              \
1126 {                                                                       \
1127         struct usb_interface *intf = to_usb_interface(dev);             \
1128         struct usbtmc_device_data *data = usb_get_intfdata(intf);       \
1129         ssize_t result;                                                 \
1130         unsigned val;                                                   \
1131                                                                         \
1132         result = sscanf(buf, "%u\n", &val);                             \
1133         if (result != 1)                                                \
1134                 result = -EINVAL;                                       \
1135         data->name = val;                                               \
1136         if (result < 0)                                                 \
1137                 return result;                                          \
1138         else                                                            \
1139                 return count;                                           \
1140 }                                                                       \
1141 static DEVICE_ATTR_RW(name)
1142 
1143 data_attribute(TermCharEnabled);
1144 data_attribute(auto_abort);
1145 
1146 static struct attribute *data_attrs[] = {
1147         &dev_attr_TermChar.attr,
1148         &dev_attr_TermCharEnabled.attr,
1149         &dev_attr_auto_abort.attr,
1150         NULL,
1151 };
1152 
1153 static struct attribute_group data_attr_grp = {
1154         .attrs = data_attrs,
1155 };
1156 
1157 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
1158 {
1159         struct device *dev;
1160         u8 *buffer;
1161         int rv;
1162 
1163         dev = &data->intf->dev;
1164 
1165         buffer = kmalloc(2, GFP_KERNEL);
1166         if (!buffer)
1167                 return -ENOMEM;
1168 
1169         rv = usb_control_msg(data->usb_dev,
1170                              usb_rcvctrlpipe(data->usb_dev, 0),
1171                              USBTMC_REQUEST_INDICATOR_PULSE,
1172                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1173                              0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1174 
1175         if (rv < 0) {
1176                 dev_err(dev, "usb_control_msg returned %d\n", rv);
1177                 goto exit;
1178         }
1179 
1180         dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1181 
1182         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1183                 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1184                 rv = -EPERM;
1185                 goto exit;
1186         }
1187         rv = 0;
1188 
1189 exit:
1190         kfree(buffer);
1191         return rv;
1192 }
1193 
1194 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1195 {
1196         struct usbtmc_device_data *data;
1197         int retval = -EBADRQC;
1198 
1199         data = file->private_data;
1200         mutex_lock(&data->io_mutex);
1201         if (data->zombie) {
1202                 retval = -ENODEV;
1203                 goto skip_io_on_zombie;
1204         }
1205 
1206         switch (cmd) {
1207         case USBTMC_IOCTL_CLEAR_OUT_HALT:
1208                 retval = usbtmc_ioctl_clear_out_halt(data);
1209                 break;
1210 
1211         case USBTMC_IOCTL_CLEAR_IN_HALT:
1212                 retval = usbtmc_ioctl_clear_in_halt(data);
1213                 break;
1214 
1215         case USBTMC_IOCTL_INDICATOR_PULSE:
1216                 retval = usbtmc_ioctl_indicator_pulse(data);
1217                 break;
1218 
1219         case USBTMC_IOCTL_CLEAR:
1220                 retval = usbtmc_ioctl_clear(data);
1221                 break;
1222 
1223         case USBTMC_IOCTL_ABORT_BULK_OUT:
1224                 retval = usbtmc_ioctl_abort_bulk_out(data);
1225                 break;
1226 
1227         case USBTMC_IOCTL_ABORT_BULK_IN:
1228                 retval = usbtmc_ioctl_abort_bulk_in(data);
1229                 break;
1230 
1231         case USBTMC488_IOCTL_GET_CAPS:
1232                 retval = copy_to_user((void __user *)arg,
1233                                 &data->usb488_caps,
1234                                 sizeof(data->usb488_caps));
1235                 if (retval)
1236                         retval = -EFAULT;
1237                 break;
1238 
1239         case USBTMC488_IOCTL_READ_STB:
1240                 retval = usbtmc488_ioctl_read_stb(data, (void __user *)arg);
1241                 break;
1242 
1243         case USBTMC488_IOCTL_REN_CONTROL:
1244                 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1245                                                 USBTMC488_REQUEST_REN_CONTROL);
1246                 break;
1247 
1248         case USBTMC488_IOCTL_GOTO_LOCAL:
1249                 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1250                                                 USBTMC488_REQUEST_GOTO_LOCAL);
1251                 break;
1252 
1253         case USBTMC488_IOCTL_LOCAL_LOCKOUT:
1254                 retval = usbtmc488_ioctl_simple(data, (void __user *)arg,
1255                                                 USBTMC488_REQUEST_LOCAL_LOCKOUT);
1256                 break;
1257         }
1258 
1259 skip_io_on_zombie:
1260         mutex_unlock(&data->io_mutex);
1261         return retval;
1262 }
1263 
1264 static int usbtmc_fasync(int fd, struct file *file, int on)
1265 {
1266         struct usbtmc_device_data *data = file->private_data;
1267 
1268         return fasync_helper(fd, file, on, &data->fasync);
1269 }
1270 
1271 static unsigned int usbtmc_poll(struct file *file, poll_table *wait)
1272 {
1273         struct usbtmc_device_data *data = file->private_data;
1274         unsigned int mask;
1275 
1276         mutex_lock(&data->io_mutex);
1277 
1278         if (data->zombie) {
1279                 mask = POLLHUP | POLLERR;
1280                 goto no_poll;
1281         }
1282 
1283         poll_wait(file, &data->waitq, wait);
1284 
1285         mask = (atomic_read(&data->srq_asserted)) ? POLLIN | POLLRDNORM : 0;
1286 
1287 no_poll:
1288         mutex_unlock(&data->io_mutex);
1289         return mask;
1290 }
1291 
1292 static const struct file_operations fops = {
1293         .owner          = THIS_MODULE,
1294         .read           = usbtmc_read,
1295         .write          = usbtmc_write,
1296         .open           = usbtmc_open,
1297         .release        = usbtmc_release,
1298         .unlocked_ioctl = usbtmc_ioctl,
1299         .fasync         = usbtmc_fasync,
1300         .poll           = usbtmc_poll,
1301         .llseek         = default_llseek,
1302 };
1303 
1304 static struct usb_class_driver usbtmc_class = {
1305         .name =         "usbtmc%d",
1306         .fops =         &fops,
1307         .minor_base =   USBTMC_MINOR_BASE,
1308 };
1309 
1310 static void usbtmc_interrupt(struct urb *urb)
1311 {
1312         struct usbtmc_device_data *data = urb->context;
1313         struct device *dev = &data->intf->dev;
1314         int status = urb->status;
1315         int rv;
1316 
1317         dev_dbg(&data->intf->dev, "int status: %d len %d\n",
1318                 status, urb->actual_length);
1319 
1320         switch (status) {
1321         case 0: /* SUCCESS */
1322                 /* check for valid STB notification */
1323                 if (data->iin_buffer[0] > 0x81) {
1324                         data->bNotify1 = data->iin_buffer[0];
1325                         data->bNotify2 = data->iin_buffer[1];
1326                         atomic_set(&data->iin_data_valid, 1);
1327                         wake_up_interruptible(&data->waitq);
1328                         goto exit;
1329                 }
1330                 /* check for SRQ notification */
1331                 if (data->iin_buffer[0] == 0x81) {
1332                         if (data->fasync)
1333                                 kill_fasync(&data->fasync,
1334                                         SIGIO, POLL_IN);
1335 
1336                         atomic_set(&data->srq_asserted, 1);
1337                         wake_up_interruptible(&data->waitq);
1338                         goto exit;
1339                 }
1340                 dev_warn(dev, "invalid notification: %x\n", data->iin_buffer[0]);
1341                 break;
1342         case -EOVERFLOW:
1343                 dev_err(dev, "overflow with length %d, actual length is %d\n",
1344                         data->iin_wMaxPacketSize, urb->actual_length);
1345         case -ECONNRESET:
1346         case -ENOENT:
1347         case -ESHUTDOWN:
1348         case -EILSEQ:
1349         case -ETIME:
1350                 /* urb terminated, clean up */
1351                 dev_dbg(dev, "urb terminated, status: %d\n", status);
1352                 return;
1353         default:
1354                 dev_err(dev, "unknown status received: %d\n", status);
1355         }
1356 exit:
1357         rv = usb_submit_urb(urb, GFP_ATOMIC);
1358         if (rv)
1359                 dev_err(dev, "usb_submit_urb failed: %d\n", rv);
1360 }
1361 
1362 static void usbtmc_free_int(struct usbtmc_device_data *data)
1363 {
1364         if (!data->iin_ep_present || !data->iin_urb)
1365                 return;
1366         usb_kill_urb(data->iin_urb);
1367         kfree(data->iin_buffer);
1368         usb_free_urb(data->iin_urb);
1369         kref_put(&data->kref, usbtmc_delete);
1370 }
1371 
1372 static int usbtmc_probe(struct usb_interface *intf,
1373                         const struct usb_device_id *id)
1374 {
1375         struct usbtmc_device_data *data;
1376         struct usb_host_interface *iface_desc;
1377         struct usb_endpoint_descriptor *endpoint;
1378         int n;
1379         int retcode;
1380 
1381         dev_dbg(&intf->dev, "%s called\n", __func__);
1382 
1383         data = kmalloc(sizeof(*data), GFP_KERNEL);
1384         if (!data)
1385                 return -ENOMEM;
1386 
1387         data->intf = intf;
1388         data->id = id;
1389         data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1390         usb_set_intfdata(intf, data);
1391         kref_init(&data->kref);
1392         mutex_init(&data->io_mutex);
1393         init_waitqueue_head(&data->waitq);
1394         atomic_set(&data->iin_data_valid, 0);
1395         atomic_set(&data->srq_asserted, 0);
1396         data->zombie = 0;
1397 
1398         /* Determine if it is a Rigol or not */
1399         data->rigol_quirk = 0;
1400         dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1401                 le16_to_cpu(data->usb_dev->descriptor.idVendor),
1402                 le16_to_cpu(data->usb_dev->descriptor.idProduct));
1403         for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
1404                 if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) &&
1405                     (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) {
1406                         dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1407                         data->rigol_quirk = 1;
1408                         break;
1409                 }
1410         }
1411 
1412         /* Initialize USBTMC bTag and other fields */
1413         data->bTag      = 1;
1414         data->TermCharEnabled = 0;
1415         data->TermChar = '\n';
1416         /*  2 <= bTag <= 127   USBTMC-USB488 subclass specification 4.3.1 */
1417         data->iin_bTag = 2;
1418 
1419         /* USBTMC devices have only one setting, so use that */
1420         iface_desc = data->intf->cur_altsetting;
1421         data->ifnum = iface_desc->desc.bInterfaceNumber;
1422 
1423         /* Find bulk in endpoint */
1424         for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1425                 endpoint = &iface_desc->endpoint[n].desc;
1426 
1427                 if (usb_endpoint_is_bulk_in(endpoint)) {
1428                         data->bulk_in = endpoint->bEndpointAddress;
1429                         dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1430                                 data->bulk_in);
1431                         break;
1432                 }
1433         }
1434 
1435         /* Find bulk out endpoint */
1436         for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1437                 endpoint = &iface_desc->endpoint[n].desc;
1438 
1439                 if (usb_endpoint_is_bulk_out(endpoint)) {
1440                         data->bulk_out = endpoint->bEndpointAddress;
1441                         dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1442                                 data->bulk_out);
1443                         break;
1444                 }
1445         }
1446         /* Find int endpoint */
1447         for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1448                 endpoint = &iface_desc->endpoint[n].desc;
1449 
1450                 if (usb_endpoint_is_int_in(endpoint)) {
1451                         data->iin_ep_present = 1;
1452                         data->iin_ep = endpoint->bEndpointAddress;
1453                         data->iin_wMaxPacketSize = usb_endpoint_maxp(endpoint);
1454                         data->iin_interval = endpoint->bInterval;
1455                         dev_dbg(&intf->dev, "Found Int in endpoint at %u\n",
1456                                 data->iin_ep);
1457                         break;
1458                 }
1459         }
1460 
1461         retcode = get_capabilities(data);
1462         if (retcode)
1463                 dev_err(&intf->dev, "can't read capabilities\n");
1464         else
1465                 retcode = sysfs_create_group(&intf->dev.kobj,
1466                                              &capability_attr_grp);
1467 
1468         if (data->iin_ep_present) {
1469                 /* allocate int urb */
1470                 data->iin_urb = usb_alloc_urb(0, GFP_KERNEL);
1471                 if (!data->iin_urb)
1472                         goto error_register;
1473 
1474                 /* will reference data in int urb */
1475                 kref_get(&data->kref);
1476 
1477                 /* allocate buffer for interrupt in */
1478                 data->iin_buffer = kmalloc(data->iin_wMaxPacketSize,
1479                                         GFP_KERNEL);
1480                 if (!data->iin_buffer)
1481                         goto error_register;
1482 
1483                 /* fill interrupt urb */
1484                 usb_fill_int_urb(data->iin_urb, data->usb_dev,
1485                                 usb_rcvintpipe(data->usb_dev, data->iin_ep),
1486                                 data->iin_buffer, data->iin_wMaxPacketSize,
1487                                 usbtmc_interrupt,
1488                                 data, data->iin_interval);
1489 
1490                 retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL);
1491                 if (retcode) {
1492                         dev_err(&intf->dev, "Failed to submit iin_urb\n");
1493                         goto error_register;
1494                 }
1495         }
1496 
1497         retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1498 
1499         retcode = usb_register_dev(intf, &usbtmc_class);
1500         if (retcode) {
1501                 dev_err(&intf->dev, "Not able to get a minor"
1502                         " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1503                         retcode);
1504                 goto error_register;
1505         }
1506         dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1507 
1508         return 0;
1509 
1510 error_register:
1511         sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1512         sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1513         usbtmc_free_int(data);
1514         kref_put(&data->kref, usbtmc_delete);
1515         return retcode;
1516 }
1517 
1518 static void usbtmc_disconnect(struct usb_interface *intf)
1519 {
1520         struct usbtmc_device_data *data;
1521 
1522         dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1523 
1524         data = usb_get_intfdata(intf);
1525         usb_deregister_dev(intf, &usbtmc_class);
1526         sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1527         sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1528         mutex_lock(&data->io_mutex);
1529         data->zombie = 1;
1530         wake_up_all(&data->waitq);
1531         mutex_unlock(&data->io_mutex);
1532         usbtmc_free_int(data);
1533         kref_put(&data->kref, usbtmc_delete);
1534 }
1535 
1536 static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
1537 {
1538         /* this driver does not have pending URBs */
1539         return 0;
1540 }
1541 
1542 static int usbtmc_resume(struct usb_interface *intf)
1543 {
1544         return 0;
1545 }
1546 
1547 static struct usb_driver usbtmc_driver = {
1548         .name           = "usbtmc",
1549         .id_table       = usbtmc_devices,
1550         .probe          = usbtmc_probe,
1551         .disconnect     = usbtmc_disconnect,
1552         .suspend        = usbtmc_suspend,
1553         .resume         = usbtmc_resume,
1554 };
1555 
1556 module_usb_driver(usbtmc_driver);
1557 
1558 MODULE_LICENSE("GPL");
1559 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us