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