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