blob: 426bfc72b9b4a0bbb8292c5f5be2356ffc90dcb4 [file] [log] [blame]
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002 * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07003 *
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>
Ilpo Järvinen857cc4d2008-10-30 13:56:47 +020024#include <linux/kernel.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070025#include <linux/fs.h>
26#include <linux/uaccess.h>
27#include <linux/kref.h>
28#include <linux/mutex.h>
Oliver Neukum86266452010-01-13 15:33:15 +010029#include <linux/smp_lock.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070030#include <linux/usb.h>
31#include <linux/usb/tmc.h>
32
33
34#define USBTMC_MINOR_BASE 176
35
36/*
37 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
38 * large as wMaxPacketSize (which is usually 512 bytes).
39 */
40#define USBTMC_SIZE_IOBUFFER 2048
41
42/* Default USB timeout (in milliseconds) */
Gergely Imreh35f76e892009-09-15 16:03:31 +080043#define USBTMC_TIMEOUT 5000
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070044
45/*
46 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
47 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
48 * packet is never read.
49 */
50#define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
51
Németh Márton6ef48522010-01-10 15:33:45 +010052static const struct usb_device_id usbtmc_devices[] = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070053 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
Greg Kroah-Hartman228dd052009-03-11 13:51:42 -070054 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070055 { 0, } /* terminating entry */
56};
Greg Kroah-Hartman5413aa42008-12-03 16:33:09 -080057MODULE_DEVICE_TABLE(usb, usbtmc_devices);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070058
59/*
60 * This structure is the capabilities for the device
Gergely Imrehd0a38362009-09-07 10:47:01 +080061 * See section 4.2.1.8 of the USBTMC specification,
62 * and section 4.2.2 of the USBTMC usb488 subclass
63 * specification for details.
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070064 */
65struct usbtmc_dev_capabilities {
66 __u8 interface_capabilities;
67 __u8 device_capabilities;
68 __u8 usb488_interface_capabilities;
69 __u8 usb488_device_capabilities;
70};
71
72/* This structure holds private data for each USBTMC device. One copy is
73 * allocated for each USBTMC device in the driver's probe function.
74 */
75struct usbtmc_device_data {
76 const struct usb_device_id *id;
77 struct usb_device *usb_dev;
78 struct usb_interface *intf;
79
80 unsigned int bulk_in;
81 unsigned int bulk_out;
82
83 u8 bTag;
84 u8 bTag_last_write; /* needed for abort */
85 u8 bTag_last_read; /* needed for abort */
86
87 /* attributes from the USB TMC spec for this device */
88 u8 TermChar;
89 bool TermCharEnabled;
90 bool auto_abort;
91
Oliver Neukum86286882009-07-02 11:36:30 +020092 bool zombie; /* fd of disconnected device */
93
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070094 struct usbtmc_dev_capabilities capabilities;
95 struct kref kref;
96 struct mutex io_mutex; /* only one i/o function running at a time */
97};
98#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
99
100/* Forward declarations */
101static struct usb_driver usbtmc_driver;
102
103static void usbtmc_delete(struct kref *kref)
104{
105 struct usbtmc_device_data *data = to_usbtmc_data(kref);
106
107 usb_put_dev(data->usb_dev);
108 kfree(data);
109}
110
111static int usbtmc_open(struct inode *inode, struct file *filp)
112{
113 struct usb_interface *intf;
114 struct usbtmc_device_data *data;
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700115 int retval = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700116
Oliver Neukum86266452010-01-13 15:33:15 +0100117 lock_kernel();
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700118 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
119 if (!intf) {
120 printk(KERN_ERR KBUILD_MODNAME
121 ": can not find device for minor %d", iminor(inode));
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700122 retval = -ENODEV;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700123 goto exit;
124 }
125
126 data = usb_get_intfdata(intf);
127 kref_get(&data->kref);
128
129 /* Store pointer in file structure's private data field */
130 filp->private_data = data;
131
132exit:
Oliver Neukum86266452010-01-13 15:33:15 +0100133 unlock_kernel();
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700134 return retval;
135}
136
137static int usbtmc_release(struct inode *inode, struct file *file)
138{
139 struct usbtmc_device_data *data = file->private_data;
140
141 kref_put(&data->kref, usbtmc_delete);
142 return 0;
143}
144
145static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
146{
Chris Malleyb361a6e2008-10-25 22:07:32 +0100147 u8 *buffer;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700148 struct device *dev;
149 int rv;
150 int n;
151 int actual;
152 struct usb_host_interface *current_setting;
153 int max_size;
154
155 dev = &data->intf->dev;
156 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
157 if (!buffer)
158 return -ENOMEM;
159
160 rv = usb_control_msg(data->usb_dev,
161 usb_rcvctrlpipe(data->usb_dev, 0),
162 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
163 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
164 data->bTag_last_read, data->bulk_in,
165 buffer, 2, USBTMC_TIMEOUT);
166
167 if (rv < 0) {
168 dev_err(dev, "usb_control_msg returned %d\n", rv);
169 goto exit;
170 }
171
172 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
173
174 if (buffer[0] == USBTMC_STATUS_FAILED) {
175 rv = 0;
176 goto exit;
177 }
178
179 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
180 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
181 buffer[0]);
182 rv = -EPERM;
183 goto exit;
184 }
185
186 max_size = 0;
187 current_setting = data->intf->cur_altsetting;
188 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
189 if (current_setting->endpoint[n].desc.bEndpointAddress ==
190 data->bulk_in)
191 max_size = le16_to_cpu(current_setting->endpoint[n].
192 desc.wMaxPacketSize);
193
194 if (max_size == 0) {
195 dev_err(dev, "Couldn't get wMaxPacketSize\n");
196 rv = -EPERM;
197 goto exit;
198 }
199
200 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
201
202 n = 0;
203
204 do {
205 dev_dbg(dev, "Reading from bulk in EP\n");
206
207 rv = usb_bulk_msg(data->usb_dev,
208 usb_rcvbulkpipe(data->usb_dev,
209 data->bulk_in),
210 buffer, USBTMC_SIZE_IOBUFFER,
211 &actual, USBTMC_TIMEOUT);
212
213 n++;
214
215 if (rv < 0) {
216 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
217 goto exit;
218 }
219 } while ((actual == max_size) &&
220 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
221
222 if (actual == max_size) {
223 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
224 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
225 rv = -EPERM;
226 goto exit;
227 }
228
229 n = 0;
230
231usbtmc_abort_bulk_in_status:
232 rv = usb_control_msg(data->usb_dev,
233 usb_rcvctrlpipe(data->usb_dev, 0),
234 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
235 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
236 0, data->bulk_in, buffer, 0x08,
237 USBTMC_TIMEOUT);
238
239 if (rv < 0) {
240 dev_err(dev, "usb_control_msg returned %d\n", rv);
241 goto exit;
242 }
243
244 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
245
246 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
247 rv = 0;
248 goto exit;
249 }
250
251 if (buffer[0] != USBTMC_STATUS_PENDING) {
252 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
253 rv = -EPERM;
254 goto exit;
255 }
256
257 if (buffer[1] == 1)
258 do {
259 dev_dbg(dev, "Reading from bulk in EP\n");
260
261 rv = usb_bulk_msg(data->usb_dev,
262 usb_rcvbulkpipe(data->usb_dev,
263 data->bulk_in),
264 buffer, USBTMC_SIZE_IOBUFFER,
265 &actual, USBTMC_TIMEOUT);
266
267 n++;
268
269 if (rv < 0) {
270 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
271 goto exit;
272 }
273 } while ((actual = max_size) &&
274 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
275
276 if (actual == max_size) {
277 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
278 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
279 rv = -EPERM;
280 goto exit;
281 }
282
283 goto usbtmc_abort_bulk_in_status;
284
285exit:
286 kfree(buffer);
287 return rv;
288
289}
290
291static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
292{
293 struct device *dev;
294 u8 *buffer;
295 int rv;
296 int n;
297
298 dev = &data->intf->dev;
299
300 buffer = kmalloc(8, GFP_KERNEL);
301 if (!buffer)
302 return -ENOMEM;
303
304 rv = usb_control_msg(data->usb_dev,
305 usb_rcvctrlpipe(data->usb_dev, 0),
306 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
307 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
308 data->bTag_last_write, data->bulk_out,
309 buffer, 2, USBTMC_TIMEOUT);
310
311 if (rv < 0) {
312 dev_err(dev, "usb_control_msg returned %d\n", rv);
313 goto exit;
314 }
315
316 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
317
318 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
319 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
320 buffer[0]);
321 rv = -EPERM;
322 goto exit;
323 }
324
325 n = 0;
326
327usbtmc_abort_bulk_out_check_status:
328 rv = usb_control_msg(data->usb_dev,
329 usb_rcvctrlpipe(data->usb_dev, 0),
330 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
331 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
332 0, data->bulk_out, buffer, 0x08,
333 USBTMC_TIMEOUT);
334 n++;
335 if (rv < 0) {
336 dev_err(dev, "usb_control_msg returned %d\n", rv);
337 goto exit;
338 }
339
340 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
341
342 if (buffer[0] == USBTMC_STATUS_SUCCESS)
343 goto usbtmc_abort_bulk_out_clear_halt;
344
345 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
346 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
347 goto usbtmc_abort_bulk_out_check_status;
348
349 rv = -EPERM;
350 goto exit;
351
352usbtmc_abort_bulk_out_clear_halt:
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800353 rv = usb_clear_halt(data->usb_dev,
354 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700355
356 if (rv < 0) {
357 dev_err(dev, "usb_control_msg returned %d\n", rv);
358 goto exit;
359 }
360 rv = 0;
361
362exit:
363 kfree(buffer);
364 return rv;
365}
366
367static ssize_t usbtmc_read(struct file *filp, char __user *buf,
368 size_t count, loff_t *f_pos)
369{
370 struct usbtmc_device_data *data;
371 struct device *dev;
Guus Sliepen665d7662009-07-22 17:39:42 +0200372 u32 n_characters;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700373 u8 *buffer;
374 int actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200375 size_t done;
376 size_t remaining;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700377 int retval;
Guus Sliepen665d7662009-07-22 17:39:42 +0200378 size_t this_part;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700379
380 /* Get pointer to private data structure */
381 data = filp->private_data;
382 dev = &data->intf->dev;
383
384 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
385 if (!buffer)
386 return -ENOMEM;
387
388 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200389 if (data->zombie) {
390 retval = -ENODEV;
391 goto exit;
392 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700393
394 remaining = count;
395 done = 0;
396
397 while (remaining > 0) {
398 if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3)
399 this_part = USBTMC_SIZE_IOBUFFER - 12 - 3;
400 else
401 this_part = remaining;
402
403 /* Setup IO buffer for DEV_DEP_MSG_IN message
404 * Refer to class specs for details
405 */
406 buffer[0] = 2;
407 buffer[1] = data->bTag;
408 buffer[2] = ~(data->bTag);
409 buffer[3] = 0; /* Reserved */
Steve Hollandc2cd26e2009-06-18 17:37:49 -0500410 buffer[4] = (this_part) & 255;
411 buffer[5] = ((this_part) >> 8) & 255;
412 buffer[6] = ((this_part) >> 16) & 255;
413 buffer[7] = ((this_part) >> 24) & 255;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700414 buffer[8] = data->TermCharEnabled * 2;
415 /* Use term character? */
416 buffer[9] = data->TermChar;
417 buffer[10] = 0; /* Reserved */
418 buffer[11] = 0; /* Reserved */
419
420 /* Send bulk URB */
421 retval = usb_bulk_msg(data->usb_dev,
422 usb_sndbulkpipe(data->usb_dev,
423 data->bulk_out),
424 buffer, 12, &actual, USBTMC_TIMEOUT);
425
426 /* Store bTag (in case we need to abort) */
427 data->bTag_last_write = data->bTag;
428
429 /* Increment bTag -- and increment again if zero */
430 data->bTag++;
431 if (!data->bTag)
432 (data->bTag)++;
433
434 if (retval < 0) {
435 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
436 if (data->auto_abort)
437 usbtmc_ioctl_abort_bulk_out(data);
438 goto exit;
439 }
440
441 /* Send bulk URB */
442 retval = usb_bulk_msg(data->usb_dev,
443 usb_rcvbulkpipe(data->usb_dev,
444 data->bulk_in),
445 buffer, USBTMC_SIZE_IOBUFFER, &actual,
446 USBTMC_TIMEOUT);
447
448 /* Store bTag (in case we need to abort) */
449 data->bTag_last_read = data->bTag;
450
451 if (retval < 0) {
452 dev_err(dev, "Unable to read data, error %d\n", retval);
453 if (data->auto_abort)
454 usbtmc_ioctl_abort_bulk_in(data);
455 goto exit;
456 }
457
458 /* How many characters did the instrument send? */
459 n_characters = buffer[4] +
460 (buffer[5] << 8) +
461 (buffer[6] << 16) +
462 (buffer[7] << 24);
463
Guus Sliepen665d7662009-07-22 17:39:42 +0200464 /* Ensure the instrument doesn't lie about it */
465 if(n_characters > actual - 12) {
Randy Dunlapa2fbf102009-07-28 11:22:41 -0700466 dev_err(dev, "Device lies about message size: %u > %d\n", n_characters, actual - 12);
Guus Sliepen665d7662009-07-22 17:39:42 +0200467 n_characters = actual - 12;
468 }
469
470 /* Ensure the instrument doesn't send more back than requested */
471 if(n_characters > this_part) {
472 dev_err(dev, "Device returns more than requested: %zu > %zu\n", done + n_characters, done + this_part);
473 n_characters = this_part;
474 }
475
Steve Holland92d07e42009-06-18 17:37:49 -0500476 /* Bound amount of data received by amount of data requested */
477 if (n_characters > this_part)
478 n_characters = this_part;
479
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700480 /* Copy buffer to user space */
481 if (copy_to_user(buf + done, &buffer[12], n_characters)) {
482 /* There must have been an addressing problem */
483 retval = -EFAULT;
484 goto exit;
485 }
486
487 done += n_characters;
Steve Holland4143d172009-06-18 17:37:49 -0500488 /* Terminate if end-of-message bit recieved from device */
489 if ((buffer[8] & 0x01) && (actual >= n_characters + 12))
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700490 remaining = 0;
Guus Sliepen665d7662009-07-22 17:39:42 +0200491 else
492 remaining -= n_characters;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700493 }
494
495 /* Update file position value */
496 *f_pos = *f_pos + done;
497 retval = done;
498
499exit:
500 mutex_unlock(&data->io_mutex);
501 kfree(buffer);
502 return retval;
503}
504
505static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
506 size_t count, loff_t *f_pos)
507{
508 struct usbtmc_device_data *data;
509 u8 *buffer;
510 int retval;
511 int actual;
512 unsigned long int n_bytes;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700513 int remaining;
514 int done;
515 int this_part;
516
517 data = filp->private_data;
518
519 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
520 if (!buffer)
521 return -ENOMEM;
522
523 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200524 if (data->zombie) {
525 retval = -ENODEV;
526 goto exit;
527 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700528
529 remaining = count;
530 done = 0;
531
532 while (remaining > 0) {
533 if (remaining > USBTMC_SIZE_IOBUFFER - 12) {
534 this_part = USBTMC_SIZE_IOBUFFER - 12;
535 buffer[8] = 0;
536 } else {
537 this_part = remaining;
538 buffer[8] = 1;
539 }
540
541 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
542 buffer[0] = 1;
543 buffer[1] = data->bTag;
544 buffer[2] = ~(data->bTag);
545 buffer[3] = 0; /* Reserved */
546 buffer[4] = this_part & 255;
547 buffer[5] = (this_part >> 8) & 255;
548 buffer[6] = (this_part >> 16) & 255;
549 buffer[7] = (this_part >> 24) & 255;
550 /* buffer[8] is set above... */
551 buffer[9] = 0; /* Reserved */
552 buffer[10] = 0; /* Reserved */
553 buffer[11] = 0; /* Reserved */
554
555 if (copy_from_user(&buffer[12], buf + done, this_part)) {
556 retval = -EFAULT;
557 goto exit;
558 }
559
Ilpo Järvinen857cc4d2008-10-30 13:56:47 +0200560 n_bytes = roundup(12 + this_part, 4);
561 memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700562
Andre Hermsec412b92009-11-19 18:14:49 +0100563 do {
564 retval = usb_bulk_msg(data->usb_dev,
565 usb_sndbulkpipe(data->usb_dev,
566 data->bulk_out),
567 buffer, n_bytes,
568 &actual, USBTMC_TIMEOUT);
569 if (retval != 0)
570 break;
571 n_bytes -= actual;
572 } while (n_bytes);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700573
574 data->bTag_last_write = data->bTag;
575 data->bTag++;
576
577 if (!data->bTag)
578 data->bTag++;
579
580 if (retval < 0) {
581 dev_err(&data->intf->dev,
582 "Unable to send data, error %d\n", retval);
583 if (data->auto_abort)
584 usbtmc_ioctl_abort_bulk_out(data);
585 goto exit;
586 }
587
588 remaining -= this_part;
589 done += this_part;
590 }
591
592 retval = count;
593exit:
594 mutex_unlock(&data->io_mutex);
595 kfree(buffer);
596 return retval;
597}
598
599static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
600{
601 struct usb_host_interface *current_setting;
602 struct usb_endpoint_descriptor *desc;
603 struct device *dev;
604 u8 *buffer;
605 int rv;
606 int n;
607 int actual;
608 int max_size;
609
610 dev = &data->intf->dev;
611
612 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
613
614 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
615 if (!buffer)
616 return -ENOMEM;
617
618 rv = usb_control_msg(data->usb_dev,
619 usb_rcvctrlpipe(data->usb_dev, 0),
620 USBTMC_REQUEST_INITIATE_CLEAR,
621 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
622 0, 0, buffer, 1, USBTMC_TIMEOUT);
623 if (rv < 0) {
624 dev_err(dev, "usb_control_msg returned %d\n", rv);
625 goto exit;
626 }
627
628 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
629
630 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
631 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
632 rv = -EPERM;
633 goto exit;
634 }
635
636 max_size = 0;
637 current_setting = data->intf->cur_altsetting;
638 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
639 desc = &current_setting->endpoint[n].desc;
640 if (desc->bEndpointAddress == data->bulk_in)
641 max_size = le16_to_cpu(desc->wMaxPacketSize);
642 }
643
644 if (max_size == 0) {
645 dev_err(dev, "Couldn't get wMaxPacketSize\n");
646 rv = -EPERM;
647 goto exit;
648 }
649
650 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
651
652 n = 0;
653
654usbtmc_clear_check_status:
655
656 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
657
658 rv = usb_control_msg(data->usb_dev,
659 usb_rcvctrlpipe(data->usb_dev, 0),
660 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
661 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
662 0, 0, buffer, 2, USBTMC_TIMEOUT);
663 if (rv < 0) {
664 dev_err(dev, "usb_control_msg returned %d\n", rv);
665 goto exit;
666 }
667
668 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
669
670 if (buffer[0] == USBTMC_STATUS_SUCCESS)
671 goto usbtmc_clear_bulk_out_halt;
672
673 if (buffer[0] != USBTMC_STATUS_PENDING) {
674 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
675 rv = -EPERM;
676 goto exit;
677 }
678
679 if (buffer[1] == 1)
680 do {
681 dev_dbg(dev, "Reading from bulk in EP\n");
682
683 rv = usb_bulk_msg(data->usb_dev,
684 usb_rcvbulkpipe(data->usb_dev,
685 data->bulk_in),
686 buffer, USBTMC_SIZE_IOBUFFER,
687 &actual, USBTMC_TIMEOUT);
688 n++;
689
690 if (rv < 0) {
691 dev_err(dev, "usb_control_msg returned %d\n",
692 rv);
693 goto exit;
694 }
695 } while ((actual == max_size) &&
696 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
697
698 if (actual == max_size) {
699 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
700 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
701 rv = -EPERM;
702 goto exit;
703 }
704
705 goto usbtmc_clear_check_status;
706
707usbtmc_clear_bulk_out_halt:
708
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800709 rv = usb_clear_halt(data->usb_dev,
710 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700711 if (rv < 0) {
712 dev_err(dev, "usb_control_msg returned %d\n", rv);
713 goto exit;
714 }
715 rv = 0;
716
717exit:
718 kfree(buffer);
719 return rv;
720}
721
722static int usbtmc_ioctl_clear_out_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
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800731 rv = usb_clear_halt(data->usb_dev,
732 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700733
734 if (rv < 0) {
735 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
736 rv);
737 goto exit;
738 }
739 rv = 0;
740
741exit:
742 kfree(buffer);
743 return rv;
744}
745
746static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
747{
748 u8 *buffer;
749 int rv;
750
751 buffer = kmalloc(2, GFP_KERNEL);
752 if (!buffer)
753 return -ENOMEM;
754
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800755 rv = usb_clear_halt(data->usb_dev,
756 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700757
758 if (rv < 0) {
759 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
760 rv);
761 goto exit;
762 }
763 rv = 0;
764
765exit:
766 kfree(buffer);
767 return rv;
768}
769
770static int get_capabilities(struct usbtmc_device_data *data)
771{
772 struct device *dev = &data->usb_dev->dev;
773 char *buffer;
Oliver Neukumca157c42009-07-02 16:41:39 +0200774 int rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700775
776 buffer = kmalloc(0x18, GFP_KERNEL);
777 if (!buffer)
778 return -ENOMEM;
779
780 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
781 USBTMC_REQUEST_GET_CAPABILITIES,
782 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
783 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
784 if (rv < 0) {
785 dev_err(dev, "usb_control_msg returned %d\n", rv);
Oliver Neukumca157c42009-07-02 16:41:39 +0200786 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700787 }
788
789 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700790 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
791 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Oliver Neukumca157c42009-07-02 16:41:39 +0200792 rv = -EPERM;
793 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700794 }
Gergely Imrehd0a38362009-09-07 10:47:01 +0800795 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
796 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
797 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
798 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700799
800 data->capabilities.interface_capabilities = buffer[4];
801 data->capabilities.device_capabilities = buffer[5];
802 data->capabilities.usb488_interface_capabilities = buffer[14];
803 data->capabilities.usb488_device_capabilities = buffer[15];
Gergely Imrehd0a38362009-09-07 10:47:01 +0800804 rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700805
Oliver Neukumca157c42009-07-02 16:41:39 +0200806err_out:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700807 kfree(buffer);
Oliver Neukumca157c42009-07-02 16:41:39 +0200808 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700809}
810
811#define capability_attribute(name) \
812static ssize_t show_##name(struct device *dev, \
813 struct device_attribute *attr, char *buf) \
814{ \
815 struct usb_interface *intf = to_usb_interface(dev); \
816 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
817 \
818 return sprintf(buf, "%d\n", data->capabilities.name); \
819} \
820static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
821
822capability_attribute(interface_capabilities);
823capability_attribute(device_capabilities);
824capability_attribute(usb488_interface_capabilities);
825capability_attribute(usb488_device_capabilities);
826
827static struct attribute *capability_attrs[] = {
828 &dev_attr_interface_capabilities.attr,
829 &dev_attr_device_capabilities.attr,
830 &dev_attr_usb488_interface_capabilities.attr,
831 &dev_attr_usb488_device_capabilities.attr,
832 NULL,
833};
834
835static struct attribute_group capability_attr_grp = {
836 .attrs = capability_attrs,
837};
838
839static ssize_t show_TermChar(struct device *dev,
840 struct device_attribute *attr, char *buf)
841{
842 struct usb_interface *intf = to_usb_interface(dev);
843 struct usbtmc_device_data *data = usb_get_intfdata(intf);
844
845 return sprintf(buf, "%c\n", data->TermChar);
846}
847
848static ssize_t store_TermChar(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
855 if (count < 1)
856 return -EINVAL;
857 data->TermChar = buf[0];
858 return count;
859}
860static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
861
862#define data_attribute(name) \
863static ssize_t show_##name(struct device *dev, \
864 struct device_attribute *attr, char *buf) \
865{ \
866 struct usb_interface *intf = to_usb_interface(dev); \
867 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
868 \
869 return sprintf(buf, "%d\n", data->name); \
870} \
871static ssize_t store_##name(struct device *dev, \
872 struct device_attribute *attr, \
873 const char *buf, size_t count) \
874{ \
875 struct usb_interface *intf = to_usb_interface(dev); \
876 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
877 ssize_t result; \
878 unsigned val; \
879 \
880 result = sscanf(buf, "%u\n", &val); \
881 if (result != 1) \
882 result = -EINVAL; \
883 data->name = val; \
884 if (result < 0) \
885 return result; \
886 else \
887 return count; \
888} \
889static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
890
891data_attribute(TermCharEnabled);
892data_attribute(auto_abort);
893
894static struct attribute *data_attrs[] = {
895 &dev_attr_TermChar.attr,
896 &dev_attr_TermCharEnabled.attr,
897 &dev_attr_auto_abort.attr,
898 NULL,
899};
900
901static struct attribute_group data_attr_grp = {
902 .attrs = data_attrs,
903};
904
905static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
906{
907 struct device *dev;
908 u8 *buffer;
909 int rv;
910
911 dev = &data->intf->dev;
912
913 buffer = kmalloc(2, GFP_KERNEL);
914 if (!buffer)
915 return -ENOMEM;
916
917 rv = usb_control_msg(data->usb_dev,
918 usb_rcvctrlpipe(data->usb_dev, 0),
919 USBTMC_REQUEST_INDICATOR_PULSE,
920 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
921 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
922
923 if (rv < 0) {
924 dev_err(dev, "usb_control_msg returned %d\n", rv);
925 goto exit;
926 }
927
928 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
929
930 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
931 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
932 rv = -EPERM;
933 goto exit;
934 }
935 rv = 0;
936
937exit:
938 kfree(buffer);
939 return rv;
940}
941
942static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
943{
944 struct usbtmc_device_data *data;
945 int retval = -EBADRQC;
946
947 data = file->private_data;
948 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200949 if (data->zombie) {
950 retval = -ENODEV;
951 goto skip_io_on_zombie;
952 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700953
954 switch (cmd) {
955 case USBTMC_IOCTL_CLEAR_OUT_HALT:
956 retval = usbtmc_ioctl_clear_out_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700957 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700958
959 case USBTMC_IOCTL_CLEAR_IN_HALT:
960 retval = usbtmc_ioctl_clear_in_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700961 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700962
963 case USBTMC_IOCTL_INDICATOR_PULSE:
964 retval = usbtmc_ioctl_indicator_pulse(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700965 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700966
967 case USBTMC_IOCTL_CLEAR:
968 retval = usbtmc_ioctl_clear(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700969 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700970
971 case USBTMC_IOCTL_ABORT_BULK_OUT:
972 retval = usbtmc_ioctl_abort_bulk_out(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700973 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700974
975 case USBTMC_IOCTL_ABORT_BULK_IN:
976 retval = usbtmc_ioctl_abort_bulk_in(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700977 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700978 }
979
Oliver Neukum86286882009-07-02 11:36:30 +0200980skip_io_on_zombie:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700981 mutex_unlock(&data->io_mutex);
982 return retval;
983}
984
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700985static const struct file_operations fops = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700986 .owner = THIS_MODULE,
987 .read = usbtmc_read,
988 .write = usbtmc_write,
989 .open = usbtmc_open,
990 .release = usbtmc_release,
991 .unlocked_ioctl = usbtmc_ioctl,
992};
993
994static struct usb_class_driver usbtmc_class = {
995 .name = "usbtmc%d",
996 .fops = &fops,
997 .minor_base = USBTMC_MINOR_BASE,
998};
999
1000
1001static int usbtmc_probe(struct usb_interface *intf,
1002 const struct usb_device_id *id)
1003{
1004 struct usbtmc_device_data *data;
1005 struct usb_host_interface *iface_desc;
1006 struct usb_endpoint_descriptor *endpoint;
1007 int n;
1008 int retcode;
1009
1010 dev_dbg(&intf->dev, "%s called\n", __func__);
1011
1012 data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
1013 if (!data) {
1014 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
1015 return -ENOMEM;
1016 }
1017
1018 data->intf = intf;
1019 data->id = id;
1020 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1021 usb_set_intfdata(intf, data);
1022 kref_init(&data->kref);
1023 mutex_init(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001024 data->zombie = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001025
1026 /* Initialize USBTMC bTag and other fields */
1027 data->bTag = 1;
1028 data->TermCharEnabled = 0;
1029 data->TermChar = '\n';
1030
1031 /* USBTMC devices have only one setting, so use that */
1032 iface_desc = data->intf->cur_altsetting;
1033
1034 /* Find bulk in endpoint */
1035 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1036 endpoint = &iface_desc->endpoint[n].desc;
1037
1038 if (usb_endpoint_is_bulk_in(endpoint)) {
1039 data->bulk_in = endpoint->bEndpointAddress;
1040 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1041 data->bulk_in);
1042 break;
1043 }
1044 }
1045
1046 /* Find bulk out endpoint */
1047 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1048 endpoint = &iface_desc->endpoint[n].desc;
1049
1050 if (usb_endpoint_is_bulk_out(endpoint)) {
1051 data->bulk_out = endpoint->bEndpointAddress;
1052 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1053 data->bulk_out);
1054 break;
1055 }
1056 }
1057
1058 retcode = get_capabilities(data);
1059 if (retcode)
1060 dev_err(&intf->dev, "can't read capabilities\n");
1061 else
1062 retcode = sysfs_create_group(&intf->dev.kobj,
1063 &capability_attr_grp);
1064
1065 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1066
1067 retcode = usb_register_dev(intf, &usbtmc_class);
1068 if (retcode) {
1069 dev_err(&intf->dev, "Not able to get a minor"
1070 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1071 retcode);
1072 goto error_register;
1073 }
1074 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1075
1076 return 0;
1077
1078error_register:
1079 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1080 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1081 kref_put(&data->kref, usbtmc_delete);
1082 return retcode;
1083}
1084
1085static void usbtmc_disconnect(struct usb_interface *intf)
1086{
1087 struct usbtmc_device_data *data;
1088
1089 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1090
1091 data = usb_get_intfdata(intf);
1092 usb_deregister_dev(intf, &usbtmc_class);
1093 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1094 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
Oliver Neukum86286882009-07-02 11:36:30 +02001095 mutex_lock(&data->io_mutex);
1096 data->zombie = 1;
1097 mutex_unlock(&data->io_mutex);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001098 kref_put(&data->kref, usbtmc_delete);
1099}
1100
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001101static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
Oliver Neukuma4708102009-07-02 11:44:33 +02001102{
1103 /* this driver does not have pending URBs */
1104 return 0;
1105}
1106
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001107static int usbtmc_resume(struct usb_interface *intf)
Oliver Neukuma4708102009-07-02 11:44:33 +02001108{
1109 return 0;
1110}
1111
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001112static struct usb_driver usbtmc_driver = {
1113 .name = "usbtmc",
1114 .id_table = usbtmc_devices,
1115 .probe = usbtmc_probe,
Oliver Neukuma4708102009-07-02 11:44:33 +02001116 .disconnect = usbtmc_disconnect,
1117 .suspend = usbtmc_suspend,
1118 .resume = usbtmc_resume,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001119};
1120
1121static int __init usbtmc_init(void)
1122{
1123 int retcode;
1124
1125 retcode = usb_register(&usbtmc_driver);
1126 if (retcode)
1127 printk(KERN_ERR KBUILD_MODNAME": Unable to register driver\n");
1128 return retcode;
1129}
1130module_init(usbtmc_init);
1131
1132static void __exit usbtmc_exit(void)
1133{
1134 usb_deregister(&usbtmc_driver);
1135}
1136module_exit(usbtmc_exit);
1137
1138MODULE_LICENSE("GPL");