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