Save sram context after changing MPU, DSP or core clocks
[linux-ginger.git] / drivers / usb / class / usbtmc.c
blob2473cf0c6b1d4ba294677c4259941f56151c41e4
1 /**
2 * drivers/usb/class/usbtmc.c - USB Test & Measurment class driver
4 * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
5 * Copyright (C) 2008 Novell, Inc.
6 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
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.
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.
18 * The GNU General Public License is available at
19 * http://www.gnu.org/copyleft/gpl.html.
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>
33 #define USBTMC_MINOR_BASE 176
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).
39 #define USBTMC_SIZE_IOBUFFER 2048
41 /* Default USB timeout (in milliseconds) */
42 #define USBTMC_TIMEOUT 5000
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.
49 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
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 */
56 MODULE_DEVICE_TABLE(usb, usbtmc_devices);
59 * This structure is the capabilities for the device
60 * See section 4.2.1.8 of the USBTMC specification,
61 * and section 4.2.2 of the USBTMC usb488 subclass
62 * specification for details.
64 struct usbtmc_dev_capabilities {
65 __u8 interface_capabilities;
66 __u8 device_capabilities;
67 __u8 usb488_interface_capabilities;
68 __u8 usb488_device_capabilities;
71 /* This structure holds private data for each USBTMC device. One copy is
72 * allocated for each USBTMC device in the driver's probe function.
74 struct usbtmc_device_data {
75 const struct usb_device_id *id;
76 struct usb_device *usb_dev;
77 struct usb_interface *intf;
79 unsigned int bulk_in;
80 unsigned int bulk_out;
82 u8 bTag;
83 u8 bTag_last_write; /* needed for abort */
84 u8 bTag_last_read; /* needed for abort */
86 /* attributes from the USB TMC spec for this device */
87 u8 TermChar;
88 bool TermCharEnabled;
89 bool auto_abort;
91 bool zombie; /* fd of disconnected device */
93 struct usbtmc_dev_capabilities capabilities;
94 struct kref kref;
95 struct mutex io_mutex; /* only one i/o function running at a time */
97 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
99 /* Forward declarations */
100 static struct usb_driver usbtmc_driver;
102 static void usbtmc_delete(struct kref *kref)
104 struct usbtmc_device_data *data = to_usbtmc_data(kref);
106 usb_put_dev(data->usb_dev);
107 kfree(data);
110 static int usbtmc_open(struct inode *inode, struct file *filp)
112 struct usb_interface *intf;
113 struct usbtmc_device_data *data;
114 int retval = 0;
116 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
117 if (!intf) {
118 printk(KERN_ERR KBUILD_MODNAME
119 ": can not find device for minor %d", iminor(inode));
120 retval = -ENODEV;
121 goto exit;
124 data = usb_get_intfdata(intf);
125 kref_get(&data->kref);
127 /* Store pointer in file structure's private data field */
128 filp->private_data = data;
130 exit:
131 return retval;
134 static int usbtmc_release(struct inode *inode, struct file *file)
136 struct usbtmc_device_data *data = file->private_data;
138 kref_put(&data->kref, usbtmc_delete);
139 return 0;
142 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
144 u8 *buffer;
145 struct device *dev;
146 int rv;
147 int n;
148 int actual;
149 struct usb_host_interface *current_setting;
150 int max_size;
152 dev = &data->intf->dev;
153 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
154 if (!buffer)
155 return -ENOMEM;
157 rv = usb_control_msg(data->usb_dev,
158 usb_rcvctrlpipe(data->usb_dev, 0),
159 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
160 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
161 data->bTag_last_read, data->bulk_in,
162 buffer, 2, USBTMC_TIMEOUT);
164 if (rv < 0) {
165 dev_err(dev, "usb_control_msg returned %d\n", rv);
166 goto exit;
169 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
171 if (buffer[0] == USBTMC_STATUS_FAILED) {
172 rv = 0;
173 goto exit;
176 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
177 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
178 buffer[0]);
179 rv = -EPERM;
180 goto exit;
183 max_size = 0;
184 current_setting = data->intf->cur_altsetting;
185 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
186 if (current_setting->endpoint[n].desc.bEndpointAddress ==
187 data->bulk_in)
188 max_size = le16_to_cpu(current_setting->endpoint[n].
189 desc.wMaxPacketSize);
191 if (max_size == 0) {
192 dev_err(dev, "Couldn't get wMaxPacketSize\n");
193 rv = -EPERM;
194 goto exit;
197 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
199 n = 0;
201 do {
202 dev_dbg(dev, "Reading from bulk in EP\n");
204 rv = usb_bulk_msg(data->usb_dev,
205 usb_rcvbulkpipe(data->usb_dev,
206 data->bulk_in),
207 buffer, USBTMC_SIZE_IOBUFFER,
208 &actual, USBTMC_TIMEOUT);
210 n++;
212 if (rv < 0) {
213 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
214 goto exit;
216 } while ((actual == max_size) &&
217 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
219 if (actual == max_size) {
220 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
221 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
222 rv = -EPERM;
223 goto exit;
226 n = 0;
228 usbtmc_abort_bulk_in_status:
229 rv = usb_control_msg(data->usb_dev,
230 usb_rcvctrlpipe(data->usb_dev, 0),
231 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
232 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
233 0, data->bulk_in, buffer, 0x08,
234 USBTMC_TIMEOUT);
236 if (rv < 0) {
237 dev_err(dev, "usb_control_msg returned %d\n", rv);
238 goto exit;
241 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
243 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
244 rv = 0;
245 goto exit;
248 if (buffer[0] != USBTMC_STATUS_PENDING) {
249 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
250 rv = -EPERM;
251 goto exit;
254 if (buffer[1] == 1)
255 do {
256 dev_dbg(dev, "Reading from bulk in EP\n");
258 rv = usb_bulk_msg(data->usb_dev,
259 usb_rcvbulkpipe(data->usb_dev,
260 data->bulk_in),
261 buffer, USBTMC_SIZE_IOBUFFER,
262 &actual, USBTMC_TIMEOUT);
264 n++;
266 if (rv < 0) {
267 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
268 goto exit;
270 } while ((actual = max_size) &&
271 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
273 if (actual == max_size) {
274 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
275 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
276 rv = -EPERM;
277 goto exit;
280 goto usbtmc_abort_bulk_in_status;
282 exit:
283 kfree(buffer);
284 return rv;
288 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
290 struct device *dev;
291 u8 *buffer;
292 int rv;
293 int n;
295 dev = &data->intf->dev;
297 buffer = kmalloc(8, GFP_KERNEL);
298 if (!buffer)
299 return -ENOMEM;
301 rv = usb_control_msg(data->usb_dev,
302 usb_rcvctrlpipe(data->usb_dev, 0),
303 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
304 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
305 data->bTag_last_write, data->bulk_out,
306 buffer, 2, USBTMC_TIMEOUT);
308 if (rv < 0) {
309 dev_err(dev, "usb_control_msg returned %d\n", rv);
310 goto exit;
313 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
315 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
316 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
317 buffer[0]);
318 rv = -EPERM;
319 goto exit;
322 n = 0;
324 usbtmc_abort_bulk_out_check_status:
325 rv = usb_control_msg(data->usb_dev,
326 usb_rcvctrlpipe(data->usb_dev, 0),
327 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
328 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
329 0, data->bulk_out, buffer, 0x08,
330 USBTMC_TIMEOUT);
331 n++;
332 if (rv < 0) {
333 dev_err(dev, "usb_control_msg returned %d\n", rv);
334 goto exit;
337 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
339 if (buffer[0] == USBTMC_STATUS_SUCCESS)
340 goto usbtmc_abort_bulk_out_clear_halt;
342 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
343 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
344 goto usbtmc_abort_bulk_out_check_status;
346 rv = -EPERM;
347 goto exit;
349 usbtmc_abort_bulk_out_clear_halt:
350 rv = usb_control_msg(data->usb_dev,
351 usb_sndctrlpipe(data->usb_dev, 0),
352 USB_REQ_CLEAR_FEATURE,
353 USB_DIR_OUT | USB_TYPE_STANDARD |
354 USB_RECIP_ENDPOINT,
355 USB_ENDPOINT_HALT, data->bulk_out, buffer,
356 0, USBTMC_TIMEOUT);
358 if (rv < 0) {
359 dev_err(dev, "usb_control_msg returned %d\n", rv);
360 goto exit;
362 rv = 0;
364 exit:
365 kfree(buffer);
366 return rv;
369 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
370 size_t count, loff_t *f_pos)
372 struct usbtmc_device_data *data;
373 struct device *dev;
374 u32 n_characters;
375 u8 *buffer;
376 int actual;
377 size_t done;
378 size_t remaining;
379 int retval;
380 size_t this_part;
382 /* Get pointer to private data structure */
383 data = filp->private_data;
384 dev = &data->intf->dev;
386 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
387 if (!buffer)
388 return -ENOMEM;
390 mutex_lock(&data->io_mutex);
391 if (data->zombie) {
392 retval = -ENODEV;
393 goto exit;
396 remaining = count;
397 done = 0;
399 while (remaining > 0) {
400 if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3)
401 this_part = USBTMC_SIZE_IOBUFFER - 12 - 3;
402 else
403 this_part = remaining;
405 /* Setup IO buffer for DEV_DEP_MSG_IN message
406 * Refer to class specs for details
408 buffer[0] = 2;
409 buffer[1] = data->bTag;
410 buffer[2] = ~(data->bTag);
411 buffer[3] = 0; /* Reserved */
412 buffer[4] = (this_part) & 255;
413 buffer[5] = ((this_part) >> 8) & 255;
414 buffer[6] = ((this_part) >> 16) & 255;
415 buffer[7] = ((this_part) >> 24) & 255;
416 buffer[8] = data->TermCharEnabled * 2;
417 /* Use term character? */
418 buffer[9] = data->TermChar;
419 buffer[10] = 0; /* Reserved */
420 buffer[11] = 0; /* Reserved */
422 /* Send bulk URB */
423 retval = usb_bulk_msg(data->usb_dev,
424 usb_sndbulkpipe(data->usb_dev,
425 data->bulk_out),
426 buffer, 12, &actual, USBTMC_TIMEOUT);
428 /* Store bTag (in case we need to abort) */
429 data->bTag_last_write = data->bTag;
431 /* Increment bTag -- and increment again if zero */
432 data->bTag++;
433 if (!data->bTag)
434 (data->bTag)++;
436 if (retval < 0) {
437 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
438 if (data->auto_abort)
439 usbtmc_ioctl_abort_bulk_out(data);
440 goto exit;
443 /* Send bulk URB */
444 retval = usb_bulk_msg(data->usb_dev,
445 usb_rcvbulkpipe(data->usb_dev,
446 data->bulk_in),
447 buffer, USBTMC_SIZE_IOBUFFER, &actual,
448 USBTMC_TIMEOUT);
450 /* Store bTag (in case we need to abort) */
451 data->bTag_last_read = data->bTag;
453 if (retval < 0) {
454 dev_err(dev, "Unable to read data, error %d\n", retval);
455 if (data->auto_abort)
456 usbtmc_ioctl_abort_bulk_in(data);
457 goto exit;
460 /* How many characters did the instrument send? */
461 n_characters = buffer[4] +
462 (buffer[5] << 8) +
463 (buffer[6] << 16) +
464 (buffer[7] << 24);
466 /* Ensure the instrument doesn't lie about it */
467 if(n_characters > actual - 12) {
468 dev_err(dev, "Device lies about message size: %u > %d\n", n_characters, actual - 12);
469 n_characters = actual - 12;
472 /* Ensure the instrument doesn't send more back than requested */
473 if(n_characters > this_part) {
474 dev_err(dev, "Device returns more than requested: %zu > %zu\n", done + n_characters, done + this_part);
475 n_characters = this_part;
478 /* Bound amount of data received by amount of data requested */
479 if (n_characters > this_part)
480 n_characters = this_part;
482 /* Copy buffer to user space */
483 if (copy_to_user(buf + done, &buffer[12], n_characters)) {
484 /* There must have been an addressing problem */
485 retval = -EFAULT;
486 goto exit;
489 done += n_characters;
490 /* Terminate if end-of-message bit recieved from device */
491 if ((buffer[8] & 0x01) && (actual >= n_characters + 12))
492 remaining = 0;
493 else
494 remaining -= n_characters;
497 /* Update file position value */
498 *f_pos = *f_pos + done;
499 retval = done;
501 exit:
502 mutex_unlock(&data->io_mutex);
503 kfree(buffer);
504 return retval;
507 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
508 size_t count, loff_t *f_pos)
510 struct usbtmc_device_data *data;
511 u8 *buffer;
512 int retval;
513 int actual;
514 unsigned long int n_bytes;
515 int remaining;
516 int done;
517 int this_part;
519 data = filp->private_data;
521 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
522 if (!buffer)
523 return -ENOMEM;
525 mutex_lock(&data->io_mutex);
526 if (data->zombie) {
527 retval = -ENODEV;
528 goto exit;
531 remaining = count;
532 done = 0;
534 while (remaining > 0) {
535 if (remaining > USBTMC_SIZE_IOBUFFER - 12) {
536 this_part = USBTMC_SIZE_IOBUFFER - 12;
537 buffer[8] = 0;
538 } else {
539 this_part = remaining;
540 buffer[8] = 1;
543 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
544 buffer[0] = 1;
545 buffer[1] = data->bTag;
546 buffer[2] = ~(data->bTag);
547 buffer[3] = 0; /* Reserved */
548 buffer[4] = this_part & 255;
549 buffer[5] = (this_part >> 8) & 255;
550 buffer[6] = (this_part >> 16) & 255;
551 buffer[7] = (this_part >> 24) & 255;
552 /* buffer[8] is set above... */
553 buffer[9] = 0; /* Reserved */
554 buffer[10] = 0; /* Reserved */
555 buffer[11] = 0; /* Reserved */
557 if (copy_from_user(&buffer[12], buf + done, this_part)) {
558 retval = -EFAULT;
559 goto exit;
562 n_bytes = roundup(12 + this_part, 4);
563 memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part));
565 retval = usb_bulk_msg(data->usb_dev,
566 usb_sndbulkpipe(data->usb_dev,
567 data->bulk_out),
568 buffer, n_bytes, &actual, USBTMC_TIMEOUT);
570 data->bTag_last_write = data->bTag;
571 data->bTag++;
573 if (!data->bTag)
574 data->bTag++;
576 if (retval < 0) {
577 dev_err(&data->intf->dev,
578 "Unable to send data, error %d\n", retval);
579 if (data->auto_abort)
580 usbtmc_ioctl_abort_bulk_out(data);
581 goto exit;
584 remaining -= this_part;
585 done += this_part;
588 retval = count;
589 exit:
590 mutex_unlock(&data->io_mutex);
591 kfree(buffer);
592 return retval;
595 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
597 struct usb_host_interface *current_setting;
598 struct usb_endpoint_descriptor *desc;
599 struct device *dev;
600 u8 *buffer;
601 int rv;
602 int n;
603 int actual;
604 int max_size;
606 dev = &data->intf->dev;
608 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
610 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
611 if (!buffer)
612 return -ENOMEM;
614 rv = usb_control_msg(data->usb_dev,
615 usb_rcvctrlpipe(data->usb_dev, 0),
616 USBTMC_REQUEST_INITIATE_CLEAR,
617 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
618 0, 0, buffer, 1, USBTMC_TIMEOUT);
619 if (rv < 0) {
620 dev_err(dev, "usb_control_msg returned %d\n", rv);
621 goto exit;
624 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
626 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
627 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
628 rv = -EPERM;
629 goto exit;
632 max_size = 0;
633 current_setting = data->intf->cur_altsetting;
634 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
635 desc = &current_setting->endpoint[n].desc;
636 if (desc->bEndpointAddress == data->bulk_in)
637 max_size = le16_to_cpu(desc->wMaxPacketSize);
640 if (max_size == 0) {
641 dev_err(dev, "Couldn't get wMaxPacketSize\n");
642 rv = -EPERM;
643 goto exit;
646 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
648 n = 0;
650 usbtmc_clear_check_status:
652 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
654 rv = usb_control_msg(data->usb_dev,
655 usb_rcvctrlpipe(data->usb_dev, 0),
656 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
657 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
658 0, 0, buffer, 2, USBTMC_TIMEOUT);
659 if (rv < 0) {
660 dev_err(dev, "usb_control_msg returned %d\n", rv);
661 goto exit;
664 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
666 if (buffer[0] == USBTMC_STATUS_SUCCESS)
667 goto usbtmc_clear_bulk_out_halt;
669 if (buffer[0] != USBTMC_STATUS_PENDING) {
670 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
671 rv = -EPERM;
672 goto exit;
675 if (buffer[1] == 1)
676 do {
677 dev_dbg(dev, "Reading from bulk in EP\n");
679 rv = usb_bulk_msg(data->usb_dev,
680 usb_rcvbulkpipe(data->usb_dev,
681 data->bulk_in),
682 buffer, USBTMC_SIZE_IOBUFFER,
683 &actual, USBTMC_TIMEOUT);
684 n++;
686 if (rv < 0) {
687 dev_err(dev, "usb_control_msg returned %d\n",
688 rv);
689 goto exit;
691 } while ((actual == max_size) &&
692 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
694 if (actual == max_size) {
695 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
696 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
697 rv = -EPERM;
698 goto exit;
701 goto usbtmc_clear_check_status;
703 usbtmc_clear_bulk_out_halt:
705 rv = usb_control_msg(data->usb_dev,
706 usb_sndctrlpipe(data->usb_dev, 0),
707 USB_REQ_CLEAR_FEATURE,
708 USB_DIR_OUT | USB_TYPE_STANDARD |
709 USB_RECIP_ENDPOINT,
710 USB_ENDPOINT_HALT,
711 data->bulk_out, buffer, 0,
712 USBTMC_TIMEOUT);
713 if (rv < 0) {
714 dev_err(dev, "usb_control_msg returned %d\n", rv);
715 goto exit;
717 rv = 0;
719 exit:
720 kfree(buffer);
721 return rv;
724 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
726 u8 *buffer;
727 int rv;
729 buffer = kmalloc(2, GFP_KERNEL);
730 if (!buffer)
731 return -ENOMEM;
733 rv = usb_control_msg(data->usb_dev,
734 usb_sndctrlpipe(data->usb_dev, 0),
735 USB_REQ_CLEAR_FEATURE,
736 USB_DIR_OUT | USB_TYPE_STANDARD |
737 USB_RECIP_ENDPOINT,
738 USB_ENDPOINT_HALT, data->bulk_out,
739 buffer, 0, USBTMC_TIMEOUT);
741 if (rv < 0) {
742 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
743 rv);
744 goto exit;
746 rv = 0;
748 exit:
749 kfree(buffer);
750 return rv;
753 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
755 u8 *buffer;
756 int rv;
758 buffer = kmalloc(2, GFP_KERNEL);
759 if (!buffer)
760 return -ENOMEM;
762 rv = usb_control_msg(data->usb_dev, usb_sndctrlpipe(data->usb_dev, 0),
763 USB_REQ_CLEAR_FEATURE,
764 USB_DIR_OUT | USB_TYPE_STANDARD |
765 USB_RECIP_ENDPOINT,
766 USB_ENDPOINT_HALT, data->bulk_in, buffer, 0,
767 USBTMC_TIMEOUT);
769 if (rv < 0) {
770 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
771 rv);
772 goto exit;
774 rv = 0;
776 exit:
777 kfree(buffer);
778 return rv;
781 static int get_capabilities(struct usbtmc_device_data *data)
783 struct device *dev = &data->usb_dev->dev;
784 char *buffer;
785 int rv = 0;
787 buffer = kmalloc(0x18, GFP_KERNEL);
788 if (!buffer)
789 return -ENOMEM;
791 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
792 USBTMC_REQUEST_GET_CAPABILITIES,
793 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
794 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
795 if (rv < 0) {
796 dev_err(dev, "usb_control_msg returned %d\n", rv);
797 goto err_out;
800 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
801 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
802 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
803 rv = -EPERM;
804 goto err_out;
806 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
807 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
808 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
809 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
811 data->capabilities.interface_capabilities = buffer[4];
812 data->capabilities.device_capabilities = buffer[5];
813 data->capabilities.usb488_interface_capabilities = buffer[14];
814 data->capabilities.usb488_device_capabilities = buffer[15];
815 rv = 0;
817 err_out:
818 kfree(buffer);
819 return rv;
822 #define capability_attribute(name) \
823 static ssize_t show_##name(struct device *dev, \
824 struct device_attribute *attr, char *buf) \
826 struct usb_interface *intf = to_usb_interface(dev); \
827 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
829 return sprintf(buf, "%d\n", data->capabilities.name); \
831 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
833 capability_attribute(interface_capabilities);
834 capability_attribute(device_capabilities);
835 capability_attribute(usb488_interface_capabilities);
836 capability_attribute(usb488_device_capabilities);
838 static struct attribute *capability_attrs[] = {
839 &dev_attr_interface_capabilities.attr,
840 &dev_attr_device_capabilities.attr,
841 &dev_attr_usb488_interface_capabilities.attr,
842 &dev_attr_usb488_device_capabilities.attr,
843 NULL,
846 static struct attribute_group capability_attr_grp = {
847 .attrs = capability_attrs,
850 static ssize_t show_TermChar(struct device *dev,
851 struct device_attribute *attr, char *buf)
853 struct usb_interface *intf = to_usb_interface(dev);
854 struct usbtmc_device_data *data = usb_get_intfdata(intf);
856 return sprintf(buf, "%c\n", data->TermChar);
859 static ssize_t store_TermChar(struct device *dev,
860 struct device_attribute *attr,
861 const char *buf, size_t count)
863 struct usb_interface *intf = to_usb_interface(dev);
864 struct usbtmc_device_data *data = usb_get_intfdata(intf);
866 if (count < 1)
867 return -EINVAL;
868 data->TermChar = buf[0];
869 return count;
871 static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
873 #define data_attribute(name) \
874 static ssize_t show_##name(struct device *dev, \
875 struct device_attribute *attr, char *buf) \
877 struct usb_interface *intf = to_usb_interface(dev); \
878 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
880 return sprintf(buf, "%d\n", data->name); \
882 static ssize_t store_##name(struct device *dev, \
883 struct device_attribute *attr, \
884 const char *buf, size_t count) \
886 struct usb_interface *intf = to_usb_interface(dev); \
887 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
888 ssize_t result; \
889 unsigned val; \
891 result = sscanf(buf, "%u\n", &val); \
892 if (result != 1) \
893 result = -EINVAL; \
894 data->name = val; \
895 if (result < 0) \
896 return result; \
897 else \
898 return count; \
900 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
902 data_attribute(TermCharEnabled);
903 data_attribute(auto_abort);
905 static struct attribute *data_attrs[] = {
906 &dev_attr_TermChar.attr,
907 &dev_attr_TermCharEnabled.attr,
908 &dev_attr_auto_abort.attr,
909 NULL,
912 static struct attribute_group data_attr_grp = {
913 .attrs = data_attrs,
916 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
918 struct device *dev;
919 u8 *buffer;
920 int rv;
922 dev = &data->intf->dev;
924 buffer = kmalloc(2, GFP_KERNEL);
925 if (!buffer)
926 return -ENOMEM;
928 rv = usb_control_msg(data->usb_dev,
929 usb_rcvctrlpipe(data->usb_dev, 0),
930 USBTMC_REQUEST_INDICATOR_PULSE,
931 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
932 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
934 if (rv < 0) {
935 dev_err(dev, "usb_control_msg returned %d\n", rv);
936 goto exit;
939 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
941 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
942 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
943 rv = -EPERM;
944 goto exit;
946 rv = 0;
948 exit:
949 kfree(buffer);
950 return rv;
953 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
955 struct usbtmc_device_data *data;
956 int retval = -EBADRQC;
958 data = file->private_data;
959 mutex_lock(&data->io_mutex);
960 if (data->zombie) {
961 retval = -ENODEV;
962 goto skip_io_on_zombie;
965 switch (cmd) {
966 case USBTMC_IOCTL_CLEAR_OUT_HALT:
967 retval = usbtmc_ioctl_clear_out_halt(data);
968 break;
970 case USBTMC_IOCTL_CLEAR_IN_HALT:
971 retval = usbtmc_ioctl_clear_in_halt(data);
972 break;
974 case USBTMC_IOCTL_INDICATOR_PULSE:
975 retval = usbtmc_ioctl_indicator_pulse(data);
976 break;
978 case USBTMC_IOCTL_CLEAR:
979 retval = usbtmc_ioctl_clear(data);
980 break;
982 case USBTMC_IOCTL_ABORT_BULK_OUT:
983 retval = usbtmc_ioctl_abort_bulk_out(data);
984 break;
986 case USBTMC_IOCTL_ABORT_BULK_IN:
987 retval = usbtmc_ioctl_abort_bulk_in(data);
988 break;
991 skip_io_on_zombie:
992 mutex_unlock(&data->io_mutex);
993 return retval;
996 static const struct file_operations fops = {
997 .owner = THIS_MODULE,
998 .read = usbtmc_read,
999 .write = usbtmc_write,
1000 .open = usbtmc_open,
1001 .release = usbtmc_release,
1002 .unlocked_ioctl = usbtmc_ioctl,
1005 static struct usb_class_driver usbtmc_class = {
1006 .name = "usbtmc%d",
1007 .fops = &fops,
1008 .minor_base = USBTMC_MINOR_BASE,
1012 static int usbtmc_probe(struct usb_interface *intf,
1013 const struct usb_device_id *id)
1015 struct usbtmc_device_data *data;
1016 struct usb_host_interface *iface_desc;
1017 struct usb_endpoint_descriptor *endpoint;
1018 int n;
1019 int retcode;
1021 dev_dbg(&intf->dev, "%s called\n", __func__);
1023 data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
1024 if (!data) {
1025 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
1026 return -ENOMEM;
1029 data->intf = intf;
1030 data->id = id;
1031 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1032 usb_set_intfdata(intf, data);
1033 kref_init(&data->kref);
1034 mutex_init(&data->io_mutex);
1035 data->zombie = 0;
1037 /* Initialize USBTMC bTag and other fields */
1038 data->bTag = 1;
1039 data->TermCharEnabled = 0;
1040 data->TermChar = '\n';
1042 /* USBTMC devices have only one setting, so use that */
1043 iface_desc = data->intf->cur_altsetting;
1045 /* Find bulk in endpoint */
1046 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1047 endpoint = &iface_desc->endpoint[n].desc;
1049 if (usb_endpoint_is_bulk_in(endpoint)) {
1050 data->bulk_in = endpoint->bEndpointAddress;
1051 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1052 data->bulk_in);
1053 break;
1057 /* Find bulk out endpoint */
1058 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1059 endpoint = &iface_desc->endpoint[n].desc;
1061 if (usb_endpoint_is_bulk_out(endpoint)) {
1062 data->bulk_out = endpoint->bEndpointAddress;
1063 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1064 data->bulk_out);
1065 break;
1069 retcode = get_capabilities(data);
1070 if (retcode)
1071 dev_err(&intf->dev, "can't read capabilities\n");
1072 else
1073 retcode = sysfs_create_group(&intf->dev.kobj,
1074 &capability_attr_grp);
1076 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1078 retcode = usb_register_dev(intf, &usbtmc_class);
1079 if (retcode) {
1080 dev_err(&intf->dev, "Not able to get a minor"
1081 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1082 retcode);
1083 goto error_register;
1085 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1087 return 0;
1089 error_register:
1090 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1091 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1092 kref_put(&data->kref, usbtmc_delete);
1093 return retcode;
1096 static void usbtmc_disconnect(struct usb_interface *intf)
1098 struct usbtmc_device_data *data;
1100 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1102 data = usb_get_intfdata(intf);
1103 usb_deregister_dev(intf, &usbtmc_class);
1104 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1105 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1106 mutex_lock(&data->io_mutex);
1107 data->zombie = 1;
1108 mutex_unlock(&data->io_mutex);
1109 kref_put(&data->kref, usbtmc_delete);
1112 static int usbtmc_suspend (struct usb_interface *intf, pm_message_t message)
1114 /* this driver does not have pending URBs */
1115 return 0;
1118 static int usbtmc_resume (struct usb_interface *intf)
1120 return 0;
1123 static struct usb_driver usbtmc_driver = {
1124 .name = "usbtmc",
1125 .id_table = usbtmc_devices,
1126 .probe = usbtmc_probe,
1127 .disconnect = usbtmc_disconnect,
1128 .suspend = usbtmc_suspend,
1129 .resume = usbtmc_resume,
1132 static int __init usbtmc_init(void)
1134 int retcode;
1136 retcode = usb_register(&usbtmc_driver);
1137 if (retcode)
1138 printk(KERN_ERR KBUILD_MODNAME": Unable to register driver\n");
1139 return retcode;
1141 module_init(usbtmc_init);
1143 static void __exit usbtmc_exit(void)
1145 usb_deregister(&usbtmc_driver);
1147 module_exit(usbtmc_exit);
1149 MODULE_LICENSE("GPL");