xref: /linux/drivers/usb/class/usbtmc.c (revision 367b8112fe2ea5c39a7bb4d263dcdd9b612fae18)
1 /**
2  * drivers/usb/class/usbtmc.c - USB Test & Measurment class driver
3  *
4  * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
5  * Copyright (C) 2008 Novell, Inc.
6  * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * The GNU General Public License is available at
19  * http://www.gnu.org/copyleft/gpl.html.
20  */
21 
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/fs.h>
25 #include <linux/uaccess.h>
26 #include <linux/kref.h>
27 #include <linux/mutex.h>
28 #include <linux/usb.h>
29 #include <linux/usb/tmc.h>
30 
31 
32 #define USBTMC_MINOR_BASE	176
33 
34 /*
35  * Size of driver internal IO buffer. Must be multiple of 4 and at least as
36  * large as wMaxPacketSize (which is usually 512 bytes).
37  */
38 #define USBTMC_SIZE_IOBUFFER	2048
39 
40 /* Default USB timeout (in milliseconds) */
41 #define USBTMC_TIMEOUT		10
42 
43 /*
44  * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
45  * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
46  * packet is never read.
47  */
48 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN	100
49 
50 static struct usb_device_id usbtmc_devices[] = {
51 	{ USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
52 	{ 0, } /* terminating entry */
53 };
54 
55 /*
56  * This structure is the capabilities for the device
57  * See section 4.2.1.8 of the USBTMC specification for details.
58  */
59 struct usbtmc_dev_capabilities {
60 	__u8 interface_capabilities;
61 	__u8 device_capabilities;
62 	__u8 usb488_interface_capabilities;
63 	__u8 usb488_device_capabilities;
64 };
65 
66 /* This structure holds private data for each USBTMC device. One copy is
67  * allocated for each USBTMC device in the driver's probe function.
68  */
69 struct usbtmc_device_data {
70 	const struct usb_device_id *id;
71 	struct usb_device *usb_dev;
72 	struct usb_interface *intf;
73 
74 	unsigned int bulk_in;
75 	unsigned int bulk_out;
76 
77 	u8 bTag;
78 	u8 bTag_last_write;	/* needed for abort */
79 	u8 bTag_last_read;	/* needed for abort */
80 
81 	/* attributes from the USB TMC spec for this device */
82 	u8 TermChar;
83 	bool TermCharEnabled;
84 	bool auto_abort;
85 
86 	struct usbtmc_dev_capabilities	capabilities;
87 	struct kref kref;
88 	struct mutex io_mutex;	/* only one i/o function running at a time */
89 };
90 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
91 
92 /* Forward declarations */
93 static struct usb_driver usbtmc_driver;
94 
95 static void usbtmc_delete(struct kref *kref)
96 {
97 	struct usbtmc_device_data *data = to_usbtmc_data(kref);
98 
99 	usb_put_dev(data->usb_dev);
100 	kfree(data);
101 }
102 
103 static int usbtmc_open(struct inode *inode, struct file *filp)
104 {
105 	struct usb_interface *intf;
106 	struct usbtmc_device_data *data;
107 	int retval = -ENODEV;
108 
109 	intf = usb_find_interface(&usbtmc_driver, iminor(inode));
110 	if (!intf) {
111 		printk(KERN_ERR KBUILD_MODNAME
112 		       ": can not find device for minor %d", iminor(inode));
113 		goto exit;
114 	}
115 
116 	data = usb_get_intfdata(intf);
117 	kref_get(&data->kref);
118 
119 	/* Store pointer in file structure's private data field */
120 	filp->private_data = data;
121 
122 exit:
123 	return retval;
124 }
125 
126 static int usbtmc_release(struct inode *inode, struct file *file)
127 {
128 	struct usbtmc_device_data *data = file->private_data;
129 
130 	kref_put(&data->kref, usbtmc_delete);
131 	return 0;
132 }
133 
134 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
135 {
136 	u8 *buffer;
137 	struct device *dev;
138 	int rv;
139 	int n;
140 	int actual;
141 	struct usb_host_interface *current_setting;
142 	int max_size;
143 
144 	dev = &data->intf->dev;
145 	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
146 	if (!buffer)
147 		return -ENOMEM;
148 
149 	rv = usb_control_msg(data->usb_dev,
150 			     usb_rcvctrlpipe(data->usb_dev, 0),
151 			     USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
152 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
153 			     data->bTag_last_read, data->bulk_in,
154 			     buffer, 2, USBTMC_TIMEOUT);
155 
156 	if (rv < 0) {
157 		dev_err(dev, "usb_control_msg returned %d\n", rv);
158 		goto exit;
159 	}
160 
161 	dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
162 
163 	if (buffer[0] == USBTMC_STATUS_FAILED) {
164 		rv = 0;
165 		goto exit;
166 	}
167 
168 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
169 		dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
170 			buffer[0]);
171 		rv = -EPERM;
172 		goto exit;
173 	}
174 
175 	max_size = 0;
176 	current_setting = data->intf->cur_altsetting;
177 	for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
178 		if (current_setting->endpoint[n].desc.bEndpointAddress ==
179 			data->bulk_in)
180 			max_size = le16_to_cpu(current_setting->endpoint[n].
181 						desc.wMaxPacketSize);
182 
183 	if (max_size == 0) {
184 		dev_err(dev, "Couldn't get wMaxPacketSize\n");
185 		rv = -EPERM;
186 		goto exit;
187 	}
188 
189 	dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
190 
191 	n = 0;
192 
193 	do {
194 		dev_dbg(dev, "Reading from bulk in EP\n");
195 
196 		rv = usb_bulk_msg(data->usb_dev,
197 				  usb_rcvbulkpipe(data->usb_dev,
198 						  data->bulk_in),
199 				  buffer, USBTMC_SIZE_IOBUFFER,
200 				  &actual, USBTMC_TIMEOUT);
201 
202 		n++;
203 
204 		if (rv < 0) {
205 			dev_err(dev, "usb_bulk_msg returned %d\n", rv);
206 			goto exit;
207 		}
208 	} while ((actual == max_size) &&
209 		 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
210 
211 	if (actual == max_size) {
212 		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
213 			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
214 		rv = -EPERM;
215 		goto exit;
216 	}
217 
218 	n = 0;
219 
220 usbtmc_abort_bulk_in_status:
221 	rv = usb_control_msg(data->usb_dev,
222 			     usb_rcvctrlpipe(data->usb_dev, 0),
223 			     USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
224 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
225 			     0, data->bulk_in, buffer, 0x08,
226 			     USBTMC_TIMEOUT);
227 
228 	if (rv < 0) {
229 		dev_err(dev, "usb_control_msg returned %d\n", rv);
230 		goto exit;
231 	}
232 
233 	dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
234 
235 	if (buffer[0] == USBTMC_STATUS_SUCCESS) {
236 		rv = 0;
237 		goto exit;
238 	}
239 
240 	if (buffer[0] != USBTMC_STATUS_PENDING) {
241 		dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
242 		rv = -EPERM;
243 		goto exit;
244 	}
245 
246 	if (buffer[1] == 1)
247 		do {
248 			dev_dbg(dev, "Reading from bulk in EP\n");
249 
250 			rv = usb_bulk_msg(data->usb_dev,
251 					  usb_rcvbulkpipe(data->usb_dev,
252 							  data->bulk_in),
253 					  buffer, USBTMC_SIZE_IOBUFFER,
254 					  &actual, USBTMC_TIMEOUT);
255 
256 			n++;
257 
258 			if (rv < 0) {
259 				dev_err(dev, "usb_bulk_msg returned %d\n", rv);
260 				goto exit;
261 			}
262 		} while ((actual = max_size) &&
263 			 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
264 
265 	if (actual == max_size) {
266 		dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
267 			USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
268 		rv = -EPERM;
269 		goto exit;
270 	}
271 
272 	goto usbtmc_abort_bulk_in_status;
273 
274 exit:
275 	kfree(buffer);
276 	return rv;
277 
278 }
279 
280 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
281 {
282 	struct device *dev;
283 	u8 *buffer;
284 	int rv;
285 	int n;
286 
287 	dev = &data->intf->dev;
288 
289 	buffer = kmalloc(8, GFP_KERNEL);
290 	if (!buffer)
291 		return -ENOMEM;
292 
293 	rv = usb_control_msg(data->usb_dev,
294 			     usb_rcvctrlpipe(data->usb_dev, 0),
295 			     USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
296 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
297 			     data->bTag_last_write, data->bulk_out,
298 			     buffer, 2, USBTMC_TIMEOUT);
299 
300 	if (rv < 0) {
301 		dev_err(dev, "usb_control_msg returned %d\n", rv);
302 		goto exit;
303 	}
304 
305 	dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
306 
307 	if (buffer[0] != USBTMC_STATUS_SUCCESS) {
308 		dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
309 			buffer[0]);
310 		rv = -EPERM;
311 		goto exit;
312 	}
313 
314 	n = 0;
315 
316 usbtmc_abort_bulk_out_check_status:
317 	rv = usb_control_msg(data->usb_dev,
318 			     usb_rcvctrlpipe(data->usb_dev, 0),
319 			     USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
320 			     USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
321 			     0, data->bulk_out, buffer, 0x08,
322 			     USBTMC_TIMEOUT);
323 	n++;
324 	if (rv < 0) {
325 		dev_err(dev, "usb_control_msg returned %d\n", rv);
326 		goto exit;
327 	}
328 
329 	dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
330 
331 	if (buffer[0] == USBTMC_STATUS_SUCCESS)
332 		goto usbtmc_abort_bulk_out_clear_halt;
333 
334 	if ((buffer[0] == USBTMC_STATUS_PENDING) &&
335 	    (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
336 		goto usbtmc_abort_bulk_out_check_status;
337 
338 	rv = -EPERM;
339 	goto exit;
340 
341 usbtmc_abort_bulk_out_clear_halt:
342 	rv = usb_control_msg(data->usb_dev,
343 			     usb_sndctrlpipe(data->usb_dev, 0),
344 			     USB_REQ_CLEAR_FEATURE,
345 			     USB_DIR_OUT | USB_TYPE_STANDARD |
346 			     USB_RECIP_ENDPOINT,
347 			     USB_ENDPOINT_HALT, data->bulk_out, buffer,
348 			     0, USBTMC_TIMEOUT);
349 
350 	if (rv < 0) {
351 		dev_err(dev, "usb_control_msg returned %d\n", rv);
352 		goto exit;
353 	}
354 	rv = 0;
355 
356 exit:
357 	kfree(buffer);
358 	return rv;
359 }
360 
361 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
362 			   size_t count, loff_t *f_pos)
363 {
364 	struct usbtmc_device_data *data;
365 	struct device *dev;
366 	unsigned long int n_characters;
367 	u8 *buffer;
368 	int actual;
369 	int done;
370 	int remaining;
371 	int retval;
372 	int this_part;
373 
374 	/* Get pointer to private data structure */
375 	data = filp->private_data;
376 	dev = &data->intf->dev;
377 
378 	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
379 	if (!buffer)
380 		return -ENOMEM;
381 
382 	mutex_lock(&data->io_mutex);
383 
384 	remaining = count;
385 	done = 0;
386 
387 	while (remaining > 0) {
388 		if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3)
389 			this_part = USBTMC_SIZE_IOBUFFER - 12 - 3;
390 		else
391 			this_part = remaining;
392 
393 		/* Setup IO buffer for DEV_DEP_MSG_IN message
394 		 * Refer to class specs for details
395 		 */
396 		buffer[0] = 2;
397 		buffer[1] = data->bTag;
398 		buffer[2] = ~(data->bTag);
399 		buffer[3] = 0; /* Reserved */
400 		buffer[4] = (this_part - 12 - 3) & 255;
401 		buffer[5] = ((this_part - 12 - 3) >> 8) & 255;
402 		buffer[6] = ((this_part - 12 - 3) >> 16) & 255;
403 		buffer[7] = ((this_part - 12 - 3) >> 24) & 255;
404 		buffer[8] = data->TermCharEnabled * 2;
405 		/* Use term character? */
406 		buffer[9] = data->TermChar;
407 		buffer[10] = 0; /* Reserved */
408 		buffer[11] = 0; /* Reserved */
409 
410 		/* Send bulk URB */
411 		retval = usb_bulk_msg(data->usb_dev,
412 				      usb_sndbulkpipe(data->usb_dev,
413 						      data->bulk_out),
414 				      buffer, 12, &actual, USBTMC_TIMEOUT);
415 
416 		/* Store bTag (in case we need to abort) */
417 		data->bTag_last_write = data->bTag;
418 
419 		/* Increment bTag -- and increment again if zero */
420 		data->bTag++;
421 		if (!data->bTag)
422 			(data->bTag)++;
423 
424 		if (retval < 0) {
425 			dev_err(dev, "usb_bulk_msg returned %d\n", retval);
426 			if (data->auto_abort)
427 				usbtmc_ioctl_abort_bulk_out(data);
428 			goto exit;
429 		}
430 
431 		/* Send bulk URB */
432 		retval = usb_bulk_msg(data->usb_dev,
433 				      usb_rcvbulkpipe(data->usb_dev,
434 						      data->bulk_in),
435 				      buffer, USBTMC_SIZE_IOBUFFER, &actual,
436 				      USBTMC_TIMEOUT);
437 
438 		/* Store bTag (in case we need to abort) */
439 		data->bTag_last_read = data->bTag;
440 
441 		if (retval < 0) {
442 			dev_err(dev, "Unable to read data, error %d\n", retval);
443 			if (data->auto_abort)
444 				usbtmc_ioctl_abort_bulk_in(data);
445 			goto exit;
446 		}
447 
448 		/* How many characters did the instrument send? */
449 		n_characters = buffer[4] +
450 			       (buffer[5] << 8) +
451 			       (buffer[6] << 16) +
452 			       (buffer[7] << 24);
453 
454 		/* Copy buffer to user space */
455 		if (copy_to_user(buf + done, &buffer[12], n_characters)) {
456 			/* There must have been an addressing problem */
457 			retval = -EFAULT;
458 			goto exit;
459 		}
460 
461 		done += n_characters;
462 		if (n_characters < USBTMC_SIZE_IOBUFFER)
463 			remaining = 0;
464 	}
465 
466 	/* Update file position value */
467 	*f_pos = *f_pos + done;
468 	retval = done;
469 
470 exit:
471 	mutex_unlock(&data->io_mutex);
472 	kfree(buffer);
473 	return retval;
474 }
475 
476 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
477 			    size_t count, loff_t *f_pos)
478 {
479 	struct usbtmc_device_data *data;
480 	u8 *buffer;
481 	int retval;
482 	int actual;
483 	unsigned long int n_bytes;
484 	int n;
485 	int remaining;
486 	int done;
487 	int this_part;
488 
489 	data = filp->private_data;
490 
491 	buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
492 	if (!buffer)
493 		return -ENOMEM;
494 
495 	mutex_lock(&data->io_mutex);
496 
497 	remaining = count;
498 	done = 0;
499 
500 	while (remaining > 0) {
501 		if (remaining > USBTMC_SIZE_IOBUFFER - 12) {
502 			this_part = USBTMC_SIZE_IOBUFFER - 12;
503 			buffer[8] = 0;
504 		} else {
505 			this_part = remaining;
506 			buffer[8] = 1;
507 		}
508 
509 		/* Setup IO buffer for DEV_DEP_MSG_OUT message */
510 		buffer[0] = 1;
511 		buffer[1] = data->bTag;
512 		buffer[2] = ~(data->bTag);
513 		buffer[3] = 0; /* Reserved */
514 		buffer[4] = this_part & 255;
515 		buffer[5] = (this_part >> 8) & 255;
516 		buffer[6] = (this_part >> 16) & 255;
517 		buffer[7] = (this_part >> 24) & 255;
518 		/* buffer[8] is set above... */
519 		buffer[9] = 0; /* Reserved */
520 		buffer[10] = 0; /* Reserved */
521 		buffer[11] = 0; /* Reserved */
522 
523 		if (copy_from_user(&buffer[12], buf + done, this_part)) {
524 			retval = -EFAULT;
525 			goto exit;
526 		}
527 
528 		n_bytes = 12 + this_part;
529 		if (this_part % 4)
530 			n_bytes += 4 - this_part % 4;
531 			for (n = 12 + this_part; n < n_bytes; n++)
532 				buffer[n] = 0;
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");
1088