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