Version:  2.0.40 2.2.26 2.4.37 3.10 3.11 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

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

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