Linux/drivers/usb/class/usbtmc.c

  1 /**
  2  * drivers/usb/class/usbtmc.c - USB Test & Measurment 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 #include <linux/init.h>
 23 #include <linux/module.h>
 24 #include <linux/kernel.h>
 25 #include <linux/fs.h>
 26 #include <linux/uaccess.h>
 27 #include <linux/kref.h>
 28 #include <linux/mutex.h>
 29 #include <linux/usb.h>
 30 #include <linux/usb/tmc.h>
 31 
 32 
 33 #define USBTMC_MINOR_BASE       176
 34 
 35 /*
 36  * Size of driver internal IO buffer. Must be multiple of 4 and at least as
 37  * large as wMaxPacketSize (which is usually 512 bytes).
 38  */
 39 #define USBTMC_SIZE_IOBUFFER    2048
 40 
 41 /* Default USB timeout (in milliseconds) */
 42 #define USBTMC_TIMEOUT          5000
 43 
 44 /*
 45  * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
 46  * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
 47  * packet is never read.
 48  */
 49 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN       100
 50 
 51 static struct usb_device_id usbtmc_devices[] = {
 52         { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
 53         { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
 54         { 0, } /* terminating entry */
 55 };
 56 MODULE_DEVICE_TABLE(usb, usbtmc_devices);
 57 
 58 /*
 59  * This structure is the capabilities for the device
 60  * See section 4.2.1.8 of the USBTMC specification,
 61  * and section 4.2.2 of the USBTMC usb488 subclass
 62  * specification for details.
 63  */
 64 struct usbtmc_dev_capabilities {
 65         __u8 interface_capabilities;
 66         __u8 device_capabilities;
 67         __u8 usb488_interface_capabilities;
 68         __u8 usb488_device_capabilities;
 69 };
 70 
 71 /* This structure holds private data for each USBTMC device. One copy is
 72  * allocated for each USBTMC device in the driver's probe function.
 73  */
 74 struct usbtmc_device_data {
 75         const struct usb_device_id *id;
 76         struct usb_device *usb_dev;
 77         struct usb_interface *intf;
 78 
 79         unsigned int bulk_in;
 80         unsigned int bulk_out;
 81 
 82         u8 bTag;
 83         u8 bTag_last_write;     /* needed for abort */
 84         u8 bTag_last_read;      /* needed for abort */
 85 
 86         /* attributes from the USB TMC spec for this device */
 87         u8 TermChar;
 88         bool TermCharEnabled;
 89         bool auto_abort;
 90 
 91         bool zombie; /* fd of disconnected device */
 92 
 93         struct usbtmc_dev_capabilities  capabilities;
 94         struct kref kref;
 95         struct mutex io_mutex;  /* only one i/o function running at a time */
 96 };
 97 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
 98 
 99 /* Forward declarations */
100 static struct usb_driver usbtmc_driver;
101 
102 static void usbtmc_delete(struct kref *kref)
103 {
104         struct usbtmc_device_data *data = to_usbtmc_data(kref);
105 
106         usb_put_dev(data->usb_dev);
107         kfree(data);
108 }
109 
110 static int usbtmc_open(struct inode *inode, struct file *filp)
111 {
112         struct usb_interface *intf;
113         struct usbtmc_device_data *data;
114         int retval = 0;
115 
116         intf = usb_find_interface(&usbtmc_driver, iminor(inode));
117         if (!intf) {
118                 printk(KERN_ERR KBUILD_MODNAME
119                        ": can not find device for minor %d", iminor(inode));
120                 retval = -ENODEV;
121                 goto exit;
122         }
123 
124         data = usb_get_intfdata(intf);
125         kref_get(&data->kref);
126 
127         /* Store pointer in file structure's private data field */
128         filp->private_data = data;
129 
130 exit:
131         return retval;
132 }
133 
134 static int usbtmc_release(struct inode *inode, struct file *file)
135 {
136         struct usbtmc_device_data *data = file->private_data;
137 
138         kref_put(&data->kref, usbtmc_delete);
139         return 0;
140 }
141 
142 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
143 {
144         u8 *buffer;
145         struct device *dev;
146         int rv;
147         int n;
148         int actual;
149         struct usb_host_interface *current_setting;
150         int max_size;
151 
152         dev = &data->intf->dev;
153         buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
154         if (!buffer)
155                 return -ENOMEM;
156 
157         rv = usb_control_msg(data->usb_dev,
158                              usb_rcvctrlpipe(data->usb_dev, 0),
159                              USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
160                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
161                              data->bTag_last_read, data->bulk_in,
162                              buffer, 2, USBTMC_TIMEOUT);
163 
164         if (rv < 0) {
165                 dev_err(dev, "usb_control_msg returned %d\n", rv);
166                 goto exit;
167         }
168 
169         dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
170 
171         if (buffer[0] == USBTMC_STATUS_FAILED) {
172                 rv = 0;
173                 goto exit;
174         }
175 
176         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
177                 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
178                         buffer[0]);
179                 rv = -EPERM;
180                 goto exit;
181         }
182 
183         max_size = 0;
184         current_setting = data->intf->cur_altsetting;
185         for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
186                 if (current_setting->endpoint[n].desc.bEndpointAddress ==
187                         data->bulk_in)
188                         max_size = le16_to_cpu(current_setting->endpoint[n].
189                                                 desc.wMaxPacketSize);
190 
191         if (max_size == 0) {
192                 dev_err(dev, "Couldn't get wMaxPacketSize\n");
193                 rv = -EPERM;
194                 goto exit;
195         }
196 
197         dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
198 
199         n = 0;
200 
201         do {
202                 dev_dbg(dev, "Reading from bulk in EP\n");
203 
204                 rv = usb_bulk_msg(data->usb_dev,
205                                   usb_rcvbulkpipe(data->usb_dev,
206                                                   data->bulk_in),
207                                   buffer, USBTMC_SIZE_IOBUFFER,
208                                   &actual, USBTMC_TIMEOUT);
209 
210                 n++;
211 
212                 if (rv < 0) {
213                         dev_err(dev, "usb_bulk_msg returned %d\n", rv);
214                         goto exit;
215                 }
216         } while ((actual == max_size) &&
217                  (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
218 
219         if (actual == max_size) {
220                 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
221                         USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
222                 rv = -EPERM;
223                 goto exit;
224         }
225 
226         n = 0;
227 
228 usbtmc_abort_bulk_in_status:
229         rv = usb_control_msg(data->usb_dev,
230                              usb_rcvctrlpipe(data->usb_dev, 0),
231                              USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
232                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
233                              0, data->bulk_in, buffer, 0x08,
234                              USBTMC_TIMEOUT);
235 
236         if (rv < 0) {
237                 dev_err(dev, "usb_control_msg returned %d\n", rv);
238                 goto exit;
239         }
240 
241         dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
242 
243         if (buffer[0] == USBTMC_STATUS_SUCCESS) {
244                 rv = 0;
245                 goto exit;
246         }
247 
248         if (buffer[0] != USBTMC_STATUS_PENDING) {
249                 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
250                 rv = -EPERM;
251                 goto exit;
252         }
253 
254         if (buffer[1] == 1)
255                 do {
256                         dev_dbg(dev, "Reading from bulk in EP\n");
257 
258                         rv = usb_bulk_msg(data->usb_dev,
259                                           usb_rcvbulkpipe(data->usb_dev,
260                                                           data->bulk_in),
261                                           buffer, USBTMC_SIZE_IOBUFFER,
262                                           &actual, USBTMC_TIMEOUT);
263 
264                         n++;
265 
266                         if (rv < 0) {
267                                 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
268                                 goto exit;
269                         }
270                 } while ((actual == max_size) &&
271                          (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
272 
273         if (actual == max_size) {
274                 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
275                         USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
276                 rv = -EPERM;
277                 goto exit;
278         }
279 
280         goto usbtmc_abort_bulk_in_status;
281 
282 exit:
283         kfree(buffer);
284         return rv;
285 
286 }
287 
288 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
289 {
290         struct device *dev;
291         u8 *buffer;
292         int rv;
293         int n;
294 
295         dev = &data->intf->dev;
296 
297         buffer = kmalloc(8, GFP_KERNEL);
298         if (!buffer)
299                 return -ENOMEM;
300 
301         rv = usb_control_msg(data->usb_dev,
302                              usb_rcvctrlpipe(data->usb_dev, 0),
303                              USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
304                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
305                              data->bTag_last_write, data->bulk_out,
306                              buffer, 2, USBTMC_TIMEOUT);
307 
308         if (rv < 0) {
309                 dev_err(dev, "usb_control_msg returned %d\n", rv);
310                 goto exit;
311         }
312 
313         dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
314 
315         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
316                 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
317                         buffer[0]);
318                 rv = -EPERM;
319                 goto exit;
320         }
321 
322         n = 0;
323 
324 usbtmc_abort_bulk_out_check_status:
325         rv = usb_control_msg(data->usb_dev,
326                              usb_rcvctrlpipe(data->usb_dev, 0),
327                              USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
328                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
329                              0, data->bulk_out, buffer, 0x08,
330                              USBTMC_TIMEOUT);
331         n++;
332         if (rv < 0) {
333                 dev_err(dev, "usb_control_msg returned %d\n", rv);
334                 goto exit;
335         }
336 
337         dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
338 
339         if (buffer[0] == USBTMC_STATUS_SUCCESS)
340                 goto usbtmc_abort_bulk_out_clear_halt;
341 
342         if ((buffer[0] == USBTMC_STATUS_PENDING) &&
343             (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
344                 goto usbtmc_abort_bulk_out_check_status;
345 
346         rv = -EPERM;
347         goto exit;
348 
349 usbtmc_abort_bulk_out_clear_halt:
350         rv = usb_control_msg(data->usb_dev,
351                              usb_sndctrlpipe(data->usb_dev, 0),
352                              USB_REQ_CLEAR_FEATURE,
353                              USB_DIR_OUT | USB_TYPE_STANDARD |
354                              USB_RECIP_ENDPOINT,
355                              USB_ENDPOINT_HALT, data->bulk_out, buffer,
356                              0, USBTMC_TIMEOUT);
357 
358         if (rv < 0) {
359                 dev_err(dev, "usb_control_msg returned %d\n", rv);
360                 goto exit;
361         }
362         rv = 0;
363 
364 exit:
365         kfree(buffer);
366         return rv;
367 }
368 
369 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
370                            size_t count, loff_t *f_pos)
371 {
372         struct usbtmc_device_data *data;
373         struct device *dev;
374         u32 n_characters;
375         u8 *buffer;
376         int actual;
377         size_t done;
378         size_t remaining;
379         int retval;
380         size_t this_part;
381 
382         /* Get pointer to private data structure */
383         data = filp->private_data;
384         dev = &data->intf->dev;
385 
386         buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
387         if (!buffer)
388                 return -ENOMEM;
389 
390         mutex_lock(&data->io_mutex);
391         if (data->zombie) {
392                 retval = -ENODEV;
393                 goto exit;
394         }
395 
396         remaining = count;
397         done = 0;
398 
399         while (remaining > 0) {
400                 if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3)
401                         this_part = USBTMC_SIZE_IOBUFFER - 12 - 3;
402                 else
403                         this_part = remaining;
404 
405                 /* Setup IO buffer for DEV_DEP_MSG_IN message
406                  * Refer to class specs for details
407                  */
408                 buffer[0] = 2;
409                 buffer[1] = data->bTag;
410                 buffer[2] = ~(data->bTag);
411                 buffer[3] = 0; /* Reserved */
412                 buffer[4] = (this_part) & 255;
413                 buffer[5] = ((this_part) >> 8) & 255;
414                 buffer[6] = ((this_part) >> 16) & 255;
415                 buffer[7] = ((this_part) >> 24) & 255;
416                 buffer[8] = data->TermCharEnabled * 2;
417                 /* Use term character? */
418                 buffer[9] = data->TermChar;
419                 buffer[10] = 0; /* Reserved */
420                 buffer[11] = 0; /* Reserved */
421 
422                 /* Send bulk URB */
423                 retval = usb_bulk_msg(data->usb_dev,
424                                       usb_sndbulkpipe(data->usb_dev,
425                                                       data->bulk_out),
426                                       buffer, 12, &actual, USBTMC_TIMEOUT);
427 
428                 /* Store bTag (in case we need to abort) */
429                 data->bTag_last_write = data->bTag;
430 
431                 /* Increment bTag -- and increment again if zero */
432                 data->bTag++;
433                 if (!data->bTag)
434                         (data->bTag)++;
435 
436                 if (retval < 0) {
437                         dev_err(dev, "usb_bulk_msg returned %d\n", retval);
438                         if (data->auto_abort)
439                                 usbtmc_ioctl_abort_bulk_out(data);
440                         goto exit;
441                 }
442 
443                 /* Send bulk URB */
444                 retval = usb_bulk_msg(data->usb_dev,
445                                       usb_rcvbulkpipe(data->usb_dev,
446                                                       data->bulk_in),
447                                       buffer, USBTMC_SIZE_IOBUFFER, &actual,
448                                       USBTMC_TIMEOUT);
449 
450                 /* Store bTag (in case we need to abort) */
451                 data->bTag_last_read = data->bTag;
452 
453                 if (retval < 0) {
454                         dev_err(dev, "Unable to read data, error %d\n", retval);
455                         if (data->auto_abort)
456                                 usbtmc_ioctl_abort_bulk_in(data);
457                         goto exit;
458                 }
459 
460                 /* How many characters did the instrument send? */
461                 n_characters = buffer[4] +
462                                (buffer[5] << 8) +
463                                (buffer[6] << 16) +
464                                (buffer[7] << 24);
465 
466                 /* Ensure the instrument doesn't lie about it */
467                 if(n_characters > actual - 12) {
468                         dev_err(dev, "Device lies about message size: %u > %d\n", n_characters, actual - 12);
469                         n_characters = actual - 12;
470                 }
471 
472                 /* Ensure the instrument doesn't send more back than requested */
473                 if(n_characters > this_part) {
474                         dev_err(dev, "Device returns more than requested: %zu > %zu\n", done + n_characters, done + this_part);
475                         n_characters = this_part;
476                 }
477 
478                 /* Bound amount of data received by amount of data requested */
479                 if (n_characters > this_part)
480                         n_characters = this_part;
481 
482                 /* Copy buffer to user space */
483                 if (copy_to_user(buf + done, &buffer[12], n_characters)) {
484                         /* There must have been an addressing problem */
485                         retval = -EFAULT;
486                         goto exit;
487                 }
488 
489                 done += n_characters;
490                 /* Terminate if end-of-message bit recieved from device */
491                 if ((buffer[8] &  0x01) && (actual >= n_characters + 12))
492                         remaining = 0;
493                 else
494                         remaining -= n_characters;
495         }
496 
497         /* Update file position value */
498         *f_pos = *f_pos + done;
499         retval = done;
500 
501 exit:
502         mutex_unlock(&data->io_mutex);
503         kfree(buffer);
504         return retval;
505 }
506 
507 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
508                             size_t count, loff_t *f_pos)
509 {
510         struct usbtmc_device_data *data;
511         u8 *buffer;
512         int retval;
513         int actual;
514         unsigned long int n_bytes;
515         int remaining;
516         int done;
517         int this_part;
518 
519         data = filp->private_data;
520 
521         buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
522         if (!buffer)
523                 return -ENOMEM;
524 
525         mutex_lock(&data->io_mutex);
526         if (data->zombie) {
527                 retval = -ENODEV;
528                 goto exit;
529         }
530 
531         remaining = count;
532         done = 0;
533 
534         while (remaining > 0) {
535                 if (remaining > USBTMC_SIZE_IOBUFFER - 12) {
536                         this_part = USBTMC_SIZE_IOBUFFER - 12;
537                         buffer[8] = 0;
538                 } else {
539                         this_part = remaining;
540                         buffer[8] = 1;
541                 }
542 
543                 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
544                 buffer[0] = 1;
545                 buffer[1] = data->bTag;
546                 buffer[2] = ~(data->bTag);
547                 buffer[3] = 0; /* Reserved */
548                 buffer[4] = this_part & 255;
549                 buffer[5] = (this_part >> 8) & 255;
550                 buffer[6] = (this_part >> 16) & 255;
551                 buffer[7] = (this_part >> 24) & 255;
552                 /* buffer[8] is set above... */
553                 buffer[9] = 0; /* Reserved */
554                 buffer[10] = 0; /* Reserved */
555                 buffer[11] = 0; /* Reserved */
556 
557                 if (copy_from_user(&buffer[12], buf + done, this_part)) {
558                         retval = -EFAULT;
559                         goto exit;
560                 }
561 
562                 n_bytes = roundup(12 + this_part, 4);
563                 memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part));
564 
565                 do {
566                         retval = usb_bulk_msg(data->usb_dev,
567                                               usb_sndbulkpipe(data->usb_dev,
568                                                               data->bulk_out),
569                                               buffer, n_bytes,
570                                               &actual, USBTMC_TIMEOUT);
571                         if (retval != 0)
572                                 break;
573                         n_bytes -= actual;
574                 } while (n_bytes);
575 
576                 data->bTag_last_write = data->bTag;
577                 data->bTag++;
578 
579                 if (!data->bTag)
580                         data->bTag++;
581 
582                 if (retval < 0) {
583                         dev_err(&data->intf->dev,
584                                 "Unable to send data, error %d\n", retval);
585                         if (data->auto_abort)
586                                 usbtmc_ioctl_abort_bulk_out(data);
587                         goto exit;
588                 }
589 
590                 remaining -= this_part;
591                 done += this_part;
592         }
593 
594         retval = count;
595 exit:
596         mutex_unlock(&data->io_mutex);
597         kfree(buffer);
598         return retval;
599 }
600 
601 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
602 {
603         struct usb_host_interface *current_setting;
604         struct usb_endpoint_descriptor *desc;
605         struct device *dev;
606         u8 *buffer;
607         int rv;
608         int n;
609         int actual;
610         int max_size;
611 
612         dev = &data->intf->dev;
613 
614         dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
615 
616         buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
617         if (!buffer)
618                 return -ENOMEM;
619 
620         rv = usb_control_msg(data->usb_dev,
621                              usb_rcvctrlpipe(data->usb_dev, 0),
622                              USBTMC_REQUEST_INITIATE_CLEAR,
623                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
624                              0, 0, buffer, 1, USBTMC_TIMEOUT);
625         if (rv < 0) {
626                 dev_err(dev, "usb_control_msg returned %d\n", rv);
627                 goto exit;
628         }
629 
630         dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
631 
632         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
633                 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
634                 rv = -EPERM;
635                 goto exit;
636         }
637 
638         max_size = 0;
639         current_setting = data->intf->cur_altsetting;
640         for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
641                 desc = &current_setting->endpoint[n].desc;
642                 if (desc->bEndpointAddress == data->bulk_in)
643                         max_size = le16_to_cpu(desc->wMaxPacketSize);
644         }
645 
646         if (max_size == 0) {
647                 dev_err(dev, "Couldn't get wMaxPacketSize\n");
648                 rv = -EPERM;
649                 goto exit;
650         }
651 
652         dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
653 
654         n = 0;
655 
656 usbtmc_clear_check_status:
657 
658         dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
659 
660         rv = usb_control_msg(data->usb_dev,
661                              usb_rcvctrlpipe(data->usb_dev, 0),
662                              USBTMC_REQUEST_CHECK_CLEAR_STATUS,
663                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
664                              0, 0, buffer, 2, USBTMC_TIMEOUT);
665         if (rv < 0) {
666                 dev_err(dev, "usb_control_msg returned %d\n", rv);
667                 goto exit;
668         }
669 
670         dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
671 
672         if (buffer[0] == USBTMC_STATUS_SUCCESS)
673                 goto usbtmc_clear_bulk_out_halt;
674 
675         if (buffer[0] != USBTMC_STATUS_PENDING) {
676                 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
677                 rv = -EPERM;
678                 goto exit;
679         }
680 
681         if (buffer[1] == 1)
682                 do {
683                         dev_dbg(dev, "Reading from bulk in EP\n");
684 
685                         rv = usb_bulk_msg(data->usb_dev,
686                                           usb_rcvbulkpipe(data->usb_dev,
687                                                           data->bulk_in),
688                                           buffer, USBTMC_SIZE_IOBUFFER,
689                                           &actual, USBTMC_TIMEOUT);
690                         n++;
691 
692                         if (rv < 0) {
693                                 dev_err(dev, "usb_control_msg returned %d\n",
694                                         rv);
695                                 goto exit;
696                         }
697                 } while ((actual == max_size) &&
698                           (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
699 
700         if (actual == max_size) {
701                 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
702                         USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
703                 rv = -EPERM;
704                 goto exit;
705         }
706 
707         goto usbtmc_clear_check_status;
708 
709 usbtmc_clear_bulk_out_halt:
710 
711         rv = usb_control_msg(data->usb_dev,
712                              usb_sndctrlpipe(data->usb_dev, 0),
713                              USB_REQ_CLEAR_FEATURE,
714                              USB_DIR_OUT | USB_TYPE_STANDARD |
715                              USB_RECIP_ENDPOINT,
716                              USB_ENDPOINT_HALT,
717                              data->bulk_out, buffer, 0,
718                              USBTMC_TIMEOUT);
719         if (rv < 0) {
720                 dev_err(dev, "usb_control_msg returned %d\n", rv);
721                 goto exit;
722         }
723         rv = 0;
724 
725 exit:
726         kfree(buffer);
727         return rv;
728 }
729 
730 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
731 {
732         u8 *buffer;
733         int rv;
734 
735         buffer = kmalloc(2, GFP_KERNEL);
736         if (!buffer)
737                 return -ENOMEM;
738 
739         rv = usb_control_msg(data->usb_dev,
740                              usb_sndctrlpipe(data->usb_dev, 0),
741                              USB_REQ_CLEAR_FEATURE,
742                              USB_DIR_OUT | USB_TYPE_STANDARD |
743                              USB_RECIP_ENDPOINT,
744                              USB_ENDPOINT_HALT, data->bulk_out,
745                              buffer, 0, USBTMC_TIMEOUT);
746 
747         if (rv < 0) {
748                 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
749                         rv);
750                 goto exit;
751         }
752         rv = 0;
753 
754 exit:
755         kfree(buffer);
756         return rv;
757 }
758 
759 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
760 {
761         u8 *buffer;
762         int rv;
763 
764         buffer = kmalloc(2, GFP_KERNEL);
765         if (!buffer)
766                 return -ENOMEM;
767 
768         rv = usb_control_msg(data->usb_dev, usb_sndctrlpipe(data->usb_dev, 0),
769                              USB_REQ_CLEAR_FEATURE,
770                              USB_DIR_OUT | USB_TYPE_STANDARD |
771                              USB_RECIP_ENDPOINT,
772                              USB_ENDPOINT_HALT, data->bulk_in, buffer, 0,
773                              USBTMC_TIMEOUT);
774 
775         if (rv < 0) {
776                 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
777                         rv);
778                 goto exit;
779         }
780         rv = 0;
781 
782 exit:
783         kfree(buffer);
784         return rv;
785 }
786 
787 static int get_capabilities(struct usbtmc_device_data *data)
788 {
789         struct device *dev = &data->usb_dev->dev;
790         char *buffer;
791         int rv = 0;
792 
793         buffer = kmalloc(0x18, GFP_KERNEL);
794         if (!buffer)
795                 return -ENOMEM;
796 
797         rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
798                              USBTMC_REQUEST_GET_CAPABILITIES,
799                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
800                              0, 0, buffer, 0x18, USBTMC_TIMEOUT);
801         if (rv < 0) {
802                 dev_err(dev, "usb_control_msg returned %d\n", rv);
803                 goto err_out;
804         }
805 
806         dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
807         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
808                 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
809                 rv = -EPERM;
810                 goto err_out;
811         }
812         dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
813         dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
814         dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
815         dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
816 
817         data->capabilities.interface_capabilities = buffer[4];
818         data->capabilities.device_capabilities = buffer[5];
819         data->capabilities.usb488_interface_capabilities = buffer[14];
820         data->capabilities.usb488_device_capabilities = buffer[15];
821         rv = 0;
822 
823 err_out:
824         kfree(buffer);
825         return rv;
826 }
827 
828 #define capability_attribute(name)                                      \
829 static ssize_t show_##name(struct device *dev,                          \
830                            struct device_attribute *attr, char *buf)    \
831 {                                                                       \
832         struct usb_interface *intf = to_usb_interface(dev);             \
833         struct usbtmc_device_data *data = usb_get_intfdata(intf);       \
834                                                                         \
835         return sprintf(buf, "%d\n", data->capabilities.name);           \
836 }                                                                       \
837 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
838 
839 capability_attribute(interface_capabilities);
840 capability_attribute(device_capabilities);
841 capability_attribute(usb488_interface_capabilities);
842 capability_attribute(usb488_device_capabilities);
843 
844 static struct attribute *capability_attrs[] = {
845         &dev_attr_interface_capabilities.attr,
846         &dev_attr_device_capabilities.attr,
847         &dev_attr_usb488_interface_capabilities.attr,
848         &dev_attr_usb488_device_capabilities.attr,
849         NULL,
850 };
851 
852 static struct attribute_group capability_attr_grp = {
853         .attrs = capability_attrs,
854 };
855 
856 static ssize_t show_TermChar(struct device *dev,
857                              struct device_attribute *attr, char *buf)
858 {
859         struct usb_interface *intf = to_usb_interface(dev);
860         struct usbtmc_device_data *data = usb_get_intfdata(intf);
861 
862         return sprintf(buf, "%c\n", data->TermChar);
863 }
864 
865 static ssize_t store_TermChar(struct device *dev,
866                               struct device_attribute *attr,
867                               const char *buf, size_t count)
868 {
869         struct usb_interface *intf = to_usb_interface(dev);
870         struct usbtmc_device_data *data = usb_get_intfdata(intf);
871 
872         if (count < 1)
873                 return -EINVAL;
874         data->TermChar = buf[0];
875         return count;
876 }
877 static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
878 
879 #define data_attribute(name)                                            \
880 static ssize_t show_##name(struct device *dev,                          \
881                            struct device_attribute *attr, char *buf)    \
882 {                                                                       \
883         struct usb_interface *intf = to_usb_interface(dev);             \
884         struct usbtmc_device_data *data = usb_get_intfdata(intf);       \
885                                                                         \
886         return sprintf(buf, "%d\n", data->name);                        \
887 }                                                                       \
888 static ssize_t store_##name(struct device *dev,                         \
889                             struct device_attribute *attr,              \
890                             const char *buf, size_t count)              \
891 {                                                                       \
892         struct usb_interface *intf = to_usb_interface(dev);             \
893         struct usbtmc_device_data *data = usb_get_intfdata(intf);       \
894         ssize_t result;                                                 \
895         unsigned val;                                                   \
896                                                                         \
897         result = sscanf(buf, "%u\n", &val);                             \
898         if (result != 1)                                                \
899                 result = -EINVAL;                                       \
900         data->name = val;                                               \
901         if (result < 0)                                                 \
902                 return result;                                          \
903         else                                                            \
904                 return count;                                           \
905 }                                                                       \
906 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
907 
908 data_attribute(TermCharEnabled);
909 data_attribute(auto_abort);
910 
911 static struct attribute *data_attrs[] = {
912         &dev_attr_TermChar.attr,
913         &dev_attr_TermCharEnabled.attr,
914         &dev_attr_auto_abort.attr,
915         NULL,
916 };
917 
918 static struct attribute_group data_attr_grp = {
919         .attrs = data_attrs,
920 };
921 
922 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
923 {
924         struct device *dev;
925         u8 *buffer;
926         int rv;
927 
928         dev = &data->intf->dev;
929 
930         buffer = kmalloc(2, GFP_KERNEL);
931         if (!buffer)
932                 return -ENOMEM;
933 
934         rv = usb_control_msg(data->usb_dev,
935                              usb_rcvctrlpipe(data->usb_dev, 0),
936                              USBTMC_REQUEST_INDICATOR_PULSE,
937                              USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
938                              0, 0, buffer, 0x01, USBTMC_TIMEOUT);
939 
940         if (rv < 0) {
941                 dev_err(dev, "usb_control_msg returned %d\n", rv);
942                 goto exit;
943         }
944 
945         dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
946 
947         if (buffer[0] != USBTMC_STATUS_SUCCESS) {
948                 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
949                 rv = -EPERM;
950                 goto exit;
951         }
952         rv = 0;
953 
954 exit:
955         kfree(buffer);
956         return rv;
957 }
958 
959 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
960 {
961         struct usbtmc_device_data *data;
962         int retval = -EBADRQC;
963 
964         data = file->private_data;
965         mutex_lock(&data->io_mutex);
966         if (data->zombie) {
967                 retval = -ENODEV;
968                 goto skip_io_on_zombie;
969         }
970 
971         switch (cmd) {
972         case USBTMC_IOCTL_CLEAR_OUT_HALT:
973                 retval = usbtmc_ioctl_clear_out_halt(data);
974                 break;
975 
976         case USBTMC_IOCTL_CLEAR_IN_HALT:
977                 retval = usbtmc_ioctl_clear_in_halt(data);
978                 break;
979 
980         case USBTMC_IOCTL_INDICATOR_PULSE:
981                 retval = usbtmc_ioctl_indicator_pulse(data);
982                 break;
983 
984         case USBTMC_IOCTL_CLEAR:
985                 retval = usbtmc_ioctl_clear(data);
986                 break;
987 
988         case USBTMC_IOCTL_ABORT_BULK_OUT:
989                 retval = usbtmc_ioctl_abort_bulk_out(data);
990                 break;
991 
992         case USBTMC_IOCTL_ABORT_BULK_IN:
993                 retval = usbtmc_ioctl_abort_bulk_in(data);
994                 break;
995         }
996 
997 skip_io_on_zombie:
998         mutex_unlock(&data->io_mutex);
999         return retval;
1000 }
1001 
1002 static const struct file_operations fops = {
1003         .owner          = THIS_MODULE,
1004         .read           = usbtmc_read,
1005         .write          = usbtmc_write,
1006         .open           = usbtmc_open,
1007         .release        = usbtmc_release,
1008         .unlocked_ioctl = usbtmc_ioctl,
1009 };
1010 
1011 static struct usb_class_driver usbtmc_class = {
1012         .name =         "usbtmc%d",
1013         .fops =         &fops,
1014         .minor_base =   USBTMC_MINOR_BASE,
1015 };
1016 
1017 
1018 static int usbtmc_probe(struct usb_interface *intf,
1019                         const struct usb_device_id *id)
1020 {
1021         struct usbtmc_device_data *data;
1022         struct usb_host_interface *iface_desc;
1023         struct usb_endpoint_descriptor *endpoint;
1024         int n;
1025         int retcode;
1026 
1027         dev_dbg(&intf->dev, "%s called\n", __func__);
1028 
1029         data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
1030         if (!data) {
1031                 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
1032                 return -ENOMEM;
1033         }
1034 
1035         data->intf = intf;
1036         data->id = id;
1037         data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1038         usb_set_intfdata(intf, data);
1039         kref_init(&data->kref);
1040         mutex_init(&data->io_mutex);
1041         data->zombie = 0;
1042 
1043         /* Initialize USBTMC bTag and other fields */
1044         data->bTag      = 1;
1045         data->TermCharEnabled = 0;
1046         data->TermChar = '\n';
1047 
1048         /* USBTMC devices have only one setting, so use that */
1049         iface_desc = data->intf->cur_altsetting;
1050 
1051         /* Find bulk in endpoint */
1052         for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1053                 endpoint = &iface_desc->endpoint[n].desc;
1054 
1055                 if (usb_endpoint_is_bulk_in(endpoint)) {
1056                         data->bulk_in = endpoint->bEndpointAddress;
1057                         dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1058                                 data->bulk_in);
1059                         break;
1060                 }
1061         }
1062 
1063         /* Find bulk out endpoint */
1064         for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1065                 endpoint = &iface_desc->endpoint[n].desc;
1066 
1067                 if (usb_endpoint_is_bulk_out(endpoint)) {
1068                         data->bulk_out = endpoint->bEndpointAddress;
1069                         dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1070                                 data->bulk_out);
1071                         break;
1072                 }
1073         }
1074 
1075         retcode = get_capabilities(data);
1076         if (retcode)
1077                 dev_err(&intf->dev, "can't read capabilities\n");
1078         else
1079                 retcode = sysfs_create_group(&intf->dev.kobj,
1080                                              &capability_attr_grp);
1081 
1082         retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1083 
1084         retcode = usb_register_dev(intf, &usbtmc_class);
1085         if (retcode) {
1086                 dev_err(&intf->dev, "Not able to get a minor"
1087                         " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1088                         retcode);
1089                 goto error_register;
1090         }
1091         dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1092 
1093         return 0;
1094 
1095 error_register:
1096         sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1097         sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1098         kref_put(&data->kref, usbtmc_delete);
1099         return retcode;
1100 }
1101 
1102 static void usbtmc_disconnect(struct usb_interface *intf)
1103 {
1104         struct usbtmc_device_data *data;
1105 
1106         dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1107 
1108         data = usb_get_intfdata(intf);
1109         usb_deregister_dev(intf, &usbtmc_class);
1110         sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1111         sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1112         mutex_lock(&data->io_mutex);
1113         data->zombie = 1;
1114         mutex_unlock(&data->io_mutex);
1115         kref_put(&data->kref, usbtmc_delete);
1116 }
1117 
1118 static int usbtmc_suspend (struct usb_interface *intf, pm_message_t message)
1119 {
1120         /* this driver does not have pending URBs */
1121         return 0;
1122 }
1123 
1124 static int usbtmc_resume (struct usb_interface *intf)
1125 {
1126         return 0;
1127 }
1128 
1129 static struct usb_driver usbtmc_driver = {
1130         .name           = "usbtmc",
1131         .id_table       = usbtmc_devices,
1132         .probe          = usbtmc_probe,
1133         .disconnect     = usbtmc_disconnect,
1134         .suspend        = usbtmc_suspend,
1135         .resume         = usbtmc_resume,
1136 };
1137 
1138 static int __init usbtmc_init(void)
1139 {
1140         int retcode;
1141 
1142         retcode = usb_register(&usbtmc_driver);
1143         if (retcode)
1144                 printk(KERN_ERR KBUILD_MODNAME": Unable to register driver\n");
1145         return retcode;
1146 }
1147 module_init(usbtmc_init);
1148 
1149 static void __exit usbtmc_exit(void)
1150 {
1151         usb_deregister(&usbtmc_driver);
1152 }
1153 module_exit(usbtmc_exit);
1154 
1155 MODULE_LICENSE("GPL");
1156 

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