blob: c450b047e3652d7e392dfaea6328ccff289c5140 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070029#include <linux/mutex.h>
30#include <linux/usb.h>
31#include <linux/usb/tmc.h>
32
33
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -050034#define RIGOL 1
35#define USBTMC_HEADER_SIZE 12
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070036#define USBTMC_MINOR_BASE 176
37
38/*
39 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
40 * large as wMaxPacketSize (which is usually 512 bytes).
41 */
42#define USBTMC_SIZE_IOBUFFER 2048
43
44/* Default USB timeout (in milliseconds) */
Gergely Imreh35f76e892009-09-15 16:03:31 +080045#define USBTMC_TIMEOUT 5000
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070046
47/*
48 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
49 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
50 * packet is never read.
51 */
52#define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
53
Németh Márton6ef48522010-01-10 15:33:45 +010054static const struct usb_device_id usbtmc_devices[] = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070055 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
Greg Kroah-Hartman228dd052009-03-11 13:51:42 -070056 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070057 { 0, } /* terminating entry */
58};
Greg Kroah-Hartman5413aa42008-12-03 16:33:09 -080059MODULE_DEVICE_TABLE(usb, usbtmc_devices);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070060
61/*
62 * This structure is the capabilities for the device
Gergely Imrehd0a38362009-09-07 10:47:01 +080063 * See section 4.2.1.8 of the USBTMC specification,
64 * and section 4.2.2 of the USBTMC usb488 subclass
65 * specification for details.
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070066 */
67struct usbtmc_dev_capabilities {
68 __u8 interface_capabilities;
69 __u8 device_capabilities;
70 __u8 usb488_interface_capabilities;
71 __u8 usb488_device_capabilities;
72};
73
74/* This structure holds private data for each USBTMC device. One copy is
75 * allocated for each USBTMC device in the driver's probe function.
76 */
77struct usbtmc_device_data {
78 const struct usb_device_id *id;
79 struct usb_device *usb_dev;
80 struct usb_interface *intf;
81
82 unsigned int bulk_in;
83 unsigned int bulk_out;
84
85 u8 bTag;
86 u8 bTag_last_write; /* needed for abort */
87 u8 bTag_last_read; /* needed for abort */
88
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -050089 u8 rigol_quirk;
90
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070091 /* attributes from the USB TMC spec for this device */
92 u8 TermChar;
93 bool TermCharEnabled;
94 bool auto_abort;
95
Oliver Neukum86286882009-07-02 11:36:30 +020096 bool zombie; /* fd of disconnected device */
97
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070098 struct usbtmc_dev_capabilities capabilities;
99 struct kref kref;
100 struct mutex io_mutex; /* only one i/o function running at a time */
101};
102#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
103
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500104struct usbtmc_ID_rigol_quirk {
105 __u16 idVendor;
106 __u16 idProduct;
107};
108
109static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
110 { 0x1ab1, 0x0588 },
111 { 0, 0 }
112};
113
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700114/* Forward declarations */
115static struct usb_driver usbtmc_driver;
116
117static void usbtmc_delete(struct kref *kref)
118{
119 struct usbtmc_device_data *data = to_usbtmc_data(kref);
120
121 usb_put_dev(data->usb_dev);
122 kfree(data);
123}
124
125static int usbtmc_open(struct inode *inode, struct file *filp)
126{
127 struct usb_interface *intf;
128 struct usbtmc_device_data *data;
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700129 int retval = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700130
131 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
132 if (!intf) {
133 printk(KERN_ERR KBUILD_MODNAME
134 ": can not find device for minor %d", iminor(inode));
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700135 retval = -ENODEV;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700136 goto exit;
137 }
138
139 data = usb_get_intfdata(intf);
140 kref_get(&data->kref);
141
142 /* Store pointer in file structure's private data field */
143 filp->private_data = data;
144
145exit:
146 return retval;
147}
148
149static int usbtmc_release(struct inode *inode, struct file *file)
150{
151 struct usbtmc_device_data *data = file->private_data;
152
153 kref_put(&data->kref, usbtmc_delete);
154 return 0;
155}
156
157static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
158{
Chris Malleyb361a6e2008-10-25 22:07:32 +0100159 u8 *buffer;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700160 struct device *dev;
161 int rv;
162 int n;
163 int actual;
164 struct usb_host_interface *current_setting;
165 int max_size;
166
167 dev = &data->intf->dev;
168 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
169 if (!buffer)
170 return -ENOMEM;
171
172 rv = usb_control_msg(data->usb_dev,
173 usb_rcvctrlpipe(data->usb_dev, 0),
174 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
175 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
176 data->bTag_last_read, data->bulk_in,
177 buffer, 2, USBTMC_TIMEOUT);
178
179 if (rv < 0) {
180 dev_err(dev, "usb_control_msg returned %d\n", rv);
181 goto exit;
182 }
183
184 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
185
186 if (buffer[0] == USBTMC_STATUS_FAILED) {
187 rv = 0;
188 goto exit;
189 }
190
191 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
192 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
193 buffer[0]);
194 rv = -EPERM;
195 goto exit;
196 }
197
198 max_size = 0;
199 current_setting = data->intf->cur_altsetting;
200 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
201 if (current_setting->endpoint[n].desc.bEndpointAddress ==
202 data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700203 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700204
205 if (max_size == 0) {
206 dev_err(dev, "Couldn't get wMaxPacketSize\n");
207 rv = -EPERM;
208 goto exit;
209 }
210
211 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
212
213 n = 0;
214
215 do {
216 dev_dbg(dev, "Reading from bulk in EP\n");
217
218 rv = usb_bulk_msg(data->usb_dev,
219 usb_rcvbulkpipe(data->usb_dev,
220 data->bulk_in),
221 buffer, USBTMC_SIZE_IOBUFFER,
222 &actual, USBTMC_TIMEOUT);
223
224 n++;
225
226 if (rv < 0) {
227 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
228 goto exit;
229 }
230 } while ((actual == max_size) &&
231 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
232
233 if (actual == max_size) {
234 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
235 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
236 rv = -EPERM;
237 goto exit;
238 }
239
240 n = 0;
241
242usbtmc_abort_bulk_in_status:
243 rv = usb_control_msg(data->usb_dev,
244 usb_rcvctrlpipe(data->usb_dev, 0),
245 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
246 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
247 0, data->bulk_in, buffer, 0x08,
248 USBTMC_TIMEOUT);
249
250 if (rv < 0) {
251 dev_err(dev, "usb_control_msg returned %d\n", rv);
252 goto exit;
253 }
254
255 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
256
257 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
258 rv = 0;
259 goto exit;
260 }
261
262 if (buffer[0] != USBTMC_STATUS_PENDING) {
263 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
264 rv = -EPERM;
265 goto exit;
266 }
267
268 if (buffer[1] == 1)
269 do {
270 dev_dbg(dev, "Reading from bulk in EP\n");
271
272 rv = usb_bulk_msg(data->usb_dev,
273 usb_rcvbulkpipe(data->usb_dev,
274 data->bulk_in),
275 buffer, USBTMC_SIZE_IOBUFFER,
276 &actual, USBTMC_TIMEOUT);
277
278 n++;
279
280 if (rv < 0) {
281 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
282 goto exit;
283 }
Maxim Nikulin4f1a7a32011-07-09 23:44:44 +0700284 } while ((actual == max_size) &&
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700285 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
286
287 if (actual == max_size) {
288 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
289 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
290 rv = -EPERM;
291 goto exit;
292 }
293
294 goto usbtmc_abort_bulk_in_status;
295
296exit:
297 kfree(buffer);
298 return rv;
299
300}
301
302static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
303{
304 struct device *dev;
305 u8 *buffer;
306 int rv;
307 int n;
308
309 dev = &data->intf->dev;
310
311 buffer = kmalloc(8, GFP_KERNEL);
312 if (!buffer)
313 return -ENOMEM;
314
315 rv = usb_control_msg(data->usb_dev,
316 usb_rcvctrlpipe(data->usb_dev, 0),
317 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
318 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
319 data->bTag_last_write, data->bulk_out,
320 buffer, 2, USBTMC_TIMEOUT);
321
322 if (rv < 0) {
323 dev_err(dev, "usb_control_msg returned %d\n", rv);
324 goto exit;
325 }
326
327 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
328
329 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
330 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
331 buffer[0]);
332 rv = -EPERM;
333 goto exit;
334 }
335
336 n = 0;
337
338usbtmc_abort_bulk_out_check_status:
339 rv = usb_control_msg(data->usb_dev,
340 usb_rcvctrlpipe(data->usb_dev, 0),
341 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
342 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
343 0, data->bulk_out, buffer, 0x08,
344 USBTMC_TIMEOUT);
345 n++;
346 if (rv < 0) {
347 dev_err(dev, "usb_control_msg returned %d\n", rv);
348 goto exit;
349 }
350
351 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
352
353 if (buffer[0] == USBTMC_STATUS_SUCCESS)
354 goto usbtmc_abort_bulk_out_clear_halt;
355
356 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
357 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
358 goto usbtmc_abort_bulk_out_check_status;
359
360 rv = -EPERM;
361 goto exit;
362
363usbtmc_abort_bulk_out_clear_halt:
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800364 rv = usb_clear_halt(data->usb_dev,
365 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700366
367 if (rv < 0) {
368 dev_err(dev, "usb_control_msg returned %d\n", rv);
369 goto exit;
370 }
371 rv = 0;
372
373exit:
374 kfree(buffer);
375 return rv;
376}
377
378static ssize_t usbtmc_read(struct file *filp, char __user *buf,
379 size_t count, loff_t *f_pos)
380{
381 struct usbtmc_device_data *data;
382 struct device *dev;
Guus Sliepen665d7662009-07-22 17:39:42 +0200383 u32 n_characters;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700384 u8 *buffer;
385 int actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200386 size_t done;
387 size_t remaining;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700388 int retval;
Guus Sliepen665d7662009-07-22 17:39:42 +0200389 size_t this_part;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700390
391 /* Get pointer to private data structure */
392 data = filp->private_data;
393 dev = &data->intf->dev;
394
395 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
396 if (!buffer)
397 return -ENOMEM;
398
399 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200400 if (data->zombie) {
401 retval = -ENODEV;
402 goto exit;
403 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700404
405 remaining = count;
406 done = 0;
407
408 while (remaining > 0) {
409 if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3)
410 this_part = USBTMC_SIZE_IOBUFFER - 12 - 3;
411 else
412 this_part = remaining;
413
414 /* Setup IO buffer for DEV_DEP_MSG_IN message
415 * Refer to class specs for details
416 */
417 buffer[0] = 2;
418 buffer[1] = data->bTag;
419 buffer[2] = ~(data->bTag);
420 buffer[3] = 0; /* Reserved */
Steve Hollandc2cd26e2009-06-18 17:37:49 -0500421 buffer[4] = (this_part) & 255;
422 buffer[5] = ((this_part) >> 8) & 255;
423 buffer[6] = ((this_part) >> 16) & 255;
424 buffer[7] = ((this_part) >> 24) & 255;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700425 buffer[8] = data->TermCharEnabled * 2;
426 /* Use term character? */
427 buffer[9] = data->TermChar;
428 buffer[10] = 0; /* Reserved */
429 buffer[11] = 0; /* Reserved */
430
431 /* Send bulk URB */
432 retval = usb_bulk_msg(data->usb_dev,
433 usb_sndbulkpipe(data->usb_dev,
434 data->bulk_out),
435 buffer, 12, &actual, USBTMC_TIMEOUT);
436
437 /* Store bTag (in case we need to abort) */
438 data->bTag_last_write = data->bTag;
439
440 /* Increment bTag -- and increment again if zero */
441 data->bTag++;
442 if (!data->bTag)
443 (data->bTag)++;
444
445 if (retval < 0) {
446 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
447 if (data->auto_abort)
448 usbtmc_ioctl_abort_bulk_out(data);
449 goto exit;
450 }
451
452 /* Send bulk URB */
453 retval = usb_bulk_msg(data->usb_dev,
454 usb_rcvbulkpipe(data->usb_dev,
455 data->bulk_in),
456 buffer, USBTMC_SIZE_IOBUFFER, &actual,
457 USBTMC_TIMEOUT);
458
459 /* Store bTag (in case we need to abort) */
460 data->bTag_last_read = data->bTag;
461
462 if (retval < 0) {
463 dev_err(dev, "Unable to read data, error %d\n", retval);
464 if (data->auto_abort)
465 usbtmc_ioctl_abort_bulk_in(data);
466 goto exit;
467 }
468
469 /* How many characters did the instrument send? */
470 n_characters = buffer[4] +
471 (buffer[5] << 8) +
472 (buffer[6] << 16) +
473 (buffer[7] << 24);
474
Guus Sliepen665d7662009-07-22 17:39:42 +0200475 /* Ensure the instrument doesn't lie about it */
476 if(n_characters > actual - 12) {
Randy Dunlapa2fbf102009-07-28 11:22:41 -0700477 dev_err(dev, "Device lies about message size: %u > %d\n", n_characters, actual - 12);
Guus Sliepen665d7662009-07-22 17:39:42 +0200478 n_characters = actual - 12;
479 }
480
481 /* Ensure the instrument doesn't send more back than requested */
482 if(n_characters > this_part) {
483 dev_err(dev, "Device returns more than requested: %zu > %zu\n", done + n_characters, done + this_part);
484 n_characters = this_part;
485 }
486
Steve Holland92d07e422009-06-18 17:37:49 -0500487 /* Bound amount of data received by amount of data requested */
488 if (n_characters > this_part)
489 n_characters = this_part;
490
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700491 /* Copy buffer to user space */
492 if (copy_to_user(buf + done, &buffer[12], n_characters)) {
493 /* There must have been an addressing problem */
494 retval = -EFAULT;
495 goto exit;
496 }
497
498 done += n_characters;
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300499 /* Terminate if end-of-message bit received from device */
Steve Holland4143d172009-06-18 17:37:49 -0500500 if ((buffer[8] & 0x01) && (actual >= n_characters + 12))
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700501 remaining = 0;
Guus Sliepen665d7662009-07-22 17:39:42 +0200502 else
503 remaining -= n_characters;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700504 }
505
506 /* Update file position value */
507 *f_pos = *f_pos + done;
508 retval = done;
509
510exit:
511 mutex_unlock(&data->io_mutex);
512 kfree(buffer);
513 return retval;
514}
515
516static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
517 size_t count, loff_t *f_pos)
518{
519 struct usbtmc_device_data *data;
520 u8 *buffer;
521 int retval;
522 int actual;
523 unsigned long int n_bytes;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700524 int remaining;
525 int done;
526 int this_part;
527
528 data = filp->private_data;
529
530 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
531 if (!buffer)
532 return -ENOMEM;
533
534 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200535 if (data->zombie) {
536 retval = -ENODEV;
537 goto exit;
538 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700539
540 remaining = count;
541 done = 0;
542
543 while (remaining > 0) {
544 if (remaining > USBTMC_SIZE_IOBUFFER - 12) {
545 this_part = USBTMC_SIZE_IOBUFFER - 12;
546 buffer[8] = 0;
547 } else {
548 this_part = remaining;
549 buffer[8] = 1;
550 }
551
552 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
553 buffer[0] = 1;
554 buffer[1] = data->bTag;
555 buffer[2] = ~(data->bTag);
556 buffer[3] = 0; /* Reserved */
557 buffer[4] = this_part & 255;
558 buffer[5] = (this_part >> 8) & 255;
559 buffer[6] = (this_part >> 16) & 255;
560 buffer[7] = (this_part >> 24) & 255;
561 /* buffer[8] is set above... */
562 buffer[9] = 0; /* Reserved */
563 buffer[10] = 0; /* Reserved */
564 buffer[11] = 0; /* Reserved */
565
566 if (copy_from_user(&buffer[12], buf + done, this_part)) {
567 retval = -EFAULT;
568 goto exit;
569 }
570
Ilpo Järvinen857cc4d2008-10-30 13:56:47 +0200571 n_bytes = roundup(12 + this_part, 4);
572 memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700573
Andre Hermsec412b92009-11-19 18:14:49 +0100574 do {
575 retval = usb_bulk_msg(data->usb_dev,
576 usb_sndbulkpipe(data->usb_dev,
577 data->bulk_out),
578 buffer, n_bytes,
579 &actual, USBTMC_TIMEOUT);
580 if (retval != 0)
581 break;
582 n_bytes -= actual;
583 } while (n_bytes);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700584
585 data->bTag_last_write = data->bTag;
586 data->bTag++;
587
588 if (!data->bTag)
589 data->bTag++;
590
591 if (retval < 0) {
592 dev_err(&data->intf->dev,
593 "Unable to send data, error %d\n", retval);
594 if (data->auto_abort)
595 usbtmc_ioctl_abort_bulk_out(data);
596 goto exit;
597 }
598
599 remaining -= this_part;
600 done += this_part;
601 }
602
603 retval = count;
604exit:
605 mutex_unlock(&data->io_mutex);
606 kfree(buffer);
607 return retval;
608}
609
610static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
611{
612 struct usb_host_interface *current_setting;
613 struct usb_endpoint_descriptor *desc;
614 struct device *dev;
615 u8 *buffer;
616 int rv;
617 int n;
618 int actual;
619 int max_size;
620
621 dev = &data->intf->dev;
622
623 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
624
625 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
626 if (!buffer)
627 return -ENOMEM;
628
629 rv = usb_control_msg(data->usb_dev,
630 usb_rcvctrlpipe(data->usb_dev, 0),
631 USBTMC_REQUEST_INITIATE_CLEAR,
632 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
633 0, 0, buffer, 1, USBTMC_TIMEOUT);
634 if (rv < 0) {
635 dev_err(dev, "usb_control_msg returned %d\n", rv);
636 goto exit;
637 }
638
639 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
640
641 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
642 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
643 rv = -EPERM;
644 goto exit;
645 }
646
647 max_size = 0;
648 current_setting = data->intf->cur_altsetting;
649 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
650 desc = &current_setting->endpoint[n].desc;
651 if (desc->bEndpointAddress == data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700652 max_size = usb_endpoint_maxp(desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700653 }
654
655 if (max_size == 0) {
656 dev_err(dev, "Couldn't get wMaxPacketSize\n");
657 rv = -EPERM;
658 goto exit;
659 }
660
661 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
662
663 n = 0;
664
665usbtmc_clear_check_status:
666
667 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
668
669 rv = usb_control_msg(data->usb_dev,
670 usb_rcvctrlpipe(data->usb_dev, 0),
671 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
672 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
673 0, 0, buffer, 2, USBTMC_TIMEOUT);
674 if (rv < 0) {
675 dev_err(dev, "usb_control_msg returned %d\n", rv);
676 goto exit;
677 }
678
679 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
680
681 if (buffer[0] == USBTMC_STATUS_SUCCESS)
682 goto usbtmc_clear_bulk_out_halt;
683
684 if (buffer[0] != USBTMC_STATUS_PENDING) {
685 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
686 rv = -EPERM;
687 goto exit;
688 }
689
690 if (buffer[1] == 1)
691 do {
692 dev_dbg(dev, "Reading from bulk in EP\n");
693
694 rv = usb_bulk_msg(data->usb_dev,
695 usb_rcvbulkpipe(data->usb_dev,
696 data->bulk_in),
697 buffer, USBTMC_SIZE_IOBUFFER,
698 &actual, USBTMC_TIMEOUT);
699 n++;
700
701 if (rv < 0) {
702 dev_err(dev, "usb_control_msg returned %d\n",
703 rv);
704 goto exit;
705 }
706 } while ((actual == max_size) &&
707 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
708
709 if (actual == max_size) {
710 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
711 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
712 rv = -EPERM;
713 goto exit;
714 }
715
716 goto usbtmc_clear_check_status;
717
718usbtmc_clear_bulk_out_halt:
719
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800720 rv = usb_clear_halt(data->usb_dev,
721 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700722 if (rv < 0) {
723 dev_err(dev, "usb_control_msg returned %d\n", rv);
724 goto exit;
725 }
726 rv = 0;
727
728exit:
729 kfree(buffer);
730 return rv;
731}
732
733static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
734{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700735 int rv;
736
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800737 rv = usb_clear_halt(data->usb_dev,
738 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700739
740 if (rv < 0) {
741 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
742 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800743 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700744 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800745 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700746}
747
748static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
749{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700750 int rv;
751
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800752 rv = usb_clear_halt(data->usb_dev,
753 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700754
755 if (rv < 0) {
756 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
757 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800758 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700759 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800760 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700761}
762
763static int get_capabilities(struct usbtmc_device_data *data)
764{
765 struct device *dev = &data->usb_dev->dev;
766 char *buffer;
Oliver Neukumca157c42009-07-02 16:41:39 +0200767 int rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700768
769 buffer = kmalloc(0x18, GFP_KERNEL);
770 if (!buffer)
771 return -ENOMEM;
772
773 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
774 USBTMC_REQUEST_GET_CAPABILITIES,
775 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
776 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
777 if (rv < 0) {
778 dev_err(dev, "usb_control_msg returned %d\n", rv);
Oliver Neukumca157c42009-07-02 16:41:39 +0200779 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700780 }
781
782 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700783 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
784 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Oliver Neukumca157c42009-07-02 16:41:39 +0200785 rv = -EPERM;
786 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700787 }
Gergely Imrehd0a38362009-09-07 10:47:01 +0800788 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
789 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
790 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
791 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700792
793 data->capabilities.interface_capabilities = buffer[4];
794 data->capabilities.device_capabilities = buffer[5];
795 data->capabilities.usb488_interface_capabilities = buffer[14];
796 data->capabilities.usb488_device_capabilities = buffer[15];
Gergely Imrehd0a38362009-09-07 10:47:01 +0800797 rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700798
Oliver Neukumca157c42009-07-02 16:41:39 +0200799err_out:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700800 kfree(buffer);
Oliver Neukumca157c42009-07-02 16:41:39 +0200801 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700802}
803
804#define capability_attribute(name) \
805static ssize_t show_##name(struct device *dev, \
806 struct device_attribute *attr, char *buf) \
807{ \
808 struct usb_interface *intf = to_usb_interface(dev); \
809 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
810 \
811 return sprintf(buf, "%d\n", data->capabilities.name); \
812} \
813static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
814
815capability_attribute(interface_capabilities);
816capability_attribute(device_capabilities);
817capability_attribute(usb488_interface_capabilities);
818capability_attribute(usb488_device_capabilities);
819
820static struct attribute *capability_attrs[] = {
821 &dev_attr_interface_capabilities.attr,
822 &dev_attr_device_capabilities.attr,
823 &dev_attr_usb488_interface_capabilities.attr,
824 &dev_attr_usb488_device_capabilities.attr,
825 NULL,
826};
827
828static struct attribute_group capability_attr_grp = {
829 .attrs = capability_attrs,
830};
831
832static ssize_t show_TermChar(struct device *dev,
833 struct device_attribute *attr, char *buf)
834{
835 struct usb_interface *intf = to_usb_interface(dev);
836 struct usbtmc_device_data *data = usb_get_intfdata(intf);
837
838 return sprintf(buf, "%c\n", data->TermChar);
839}
840
841static ssize_t store_TermChar(struct device *dev,
842 struct device_attribute *attr,
843 const char *buf, size_t count)
844{
845 struct usb_interface *intf = to_usb_interface(dev);
846 struct usbtmc_device_data *data = usb_get_intfdata(intf);
847
848 if (count < 1)
849 return -EINVAL;
850 data->TermChar = buf[0];
851 return count;
852}
853static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
854
855#define data_attribute(name) \
856static ssize_t show_##name(struct device *dev, \
857 struct device_attribute *attr, char *buf) \
858{ \
859 struct usb_interface *intf = to_usb_interface(dev); \
860 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
861 \
862 return sprintf(buf, "%d\n", data->name); \
863} \
864static ssize_t store_##name(struct device *dev, \
865 struct device_attribute *attr, \
866 const char *buf, size_t count) \
867{ \
868 struct usb_interface *intf = to_usb_interface(dev); \
869 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
870 ssize_t result; \
871 unsigned val; \
872 \
873 result = sscanf(buf, "%u\n", &val); \
874 if (result != 1) \
875 result = -EINVAL; \
876 data->name = val; \
877 if (result < 0) \
878 return result; \
879 else \
880 return count; \
881} \
882static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
883
884data_attribute(TermCharEnabled);
885data_attribute(auto_abort);
886
887static struct attribute *data_attrs[] = {
888 &dev_attr_TermChar.attr,
889 &dev_attr_TermCharEnabled.attr,
890 &dev_attr_auto_abort.attr,
891 NULL,
892};
893
894static struct attribute_group data_attr_grp = {
895 .attrs = data_attrs,
896};
897
898static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
899{
900 struct device *dev;
901 u8 *buffer;
902 int rv;
903
904 dev = &data->intf->dev;
905
906 buffer = kmalloc(2, GFP_KERNEL);
907 if (!buffer)
908 return -ENOMEM;
909
910 rv = usb_control_msg(data->usb_dev,
911 usb_rcvctrlpipe(data->usb_dev, 0),
912 USBTMC_REQUEST_INDICATOR_PULSE,
913 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
914 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
915
916 if (rv < 0) {
917 dev_err(dev, "usb_control_msg returned %d\n", rv);
918 goto exit;
919 }
920
921 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
922
923 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
924 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
925 rv = -EPERM;
926 goto exit;
927 }
928 rv = 0;
929
930exit:
931 kfree(buffer);
932 return rv;
933}
934
935static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
936{
937 struct usbtmc_device_data *data;
938 int retval = -EBADRQC;
939
940 data = file->private_data;
941 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200942 if (data->zombie) {
943 retval = -ENODEV;
944 goto skip_io_on_zombie;
945 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700946
947 switch (cmd) {
948 case USBTMC_IOCTL_CLEAR_OUT_HALT:
949 retval = usbtmc_ioctl_clear_out_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700950 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700951
952 case USBTMC_IOCTL_CLEAR_IN_HALT:
953 retval = usbtmc_ioctl_clear_in_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700954 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700955
956 case USBTMC_IOCTL_INDICATOR_PULSE:
957 retval = usbtmc_ioctl_indicator_pulse(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700958 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700959
960 case USBTMC_IOCTL_CLEAR:
961 retval = usbtmc_ioctl_clear(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700962 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700963
964 case USBTMC_IOCTL_ABORT_BULK_OUT:
965 retval = usbtmc_ioctl_abort_bulk_out(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700966 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700967
968 case USBTMC_IOCTL_ABORT_BULK_IN:
969 retval = usbtmc_ioctl_abort_bulk_in(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -0700970 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700971 }
972
Oliver Neukum86286882009-07-02 11:36:30 +0200973skip_io_on_zombie:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700974 mutex_unlock(&data->io_mutex);
975 return retval;
976}
977
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700978static const struct file_operations fops = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700979 .owner = THIS_MODULE,
980 .read = usbtmc_read,
981 .write = usbtmc_write,
982 .open = usbtmc_open,
983 .release = usbtmc_release,
984 .unlocked_ioctl = usbtmc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200985 .llseek = default_llseek,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700986};
987
988static struct usb_class_driver usbtmc_class = {
989 .name = "usbtmc%d",
990 .fops = &fops,
991 .minor_base = USBTMC_MINOR_BASE,
992};
993
994
995static int usbtmc_probe(struct usb_interface *intf,
996 const struct usb_device_id *id)
997{
998 struct usbtmc_device_data *data;
999 struct usb_host_interface *iface_desc;
1000 struct usb_endpoint_descriptor *endpoint;
1001 int n;
1002 int retcode;
1003
1004 dev_dbg(&intf->dev, "%s called\n", __func__);
1005
1006 data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
1007 if (!data) {
1008 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
1009 return -ENOMEM;
1010 }
1011
1012 data->intf = intf;
1013 data->id = id;
1014 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1015 usb_set_intfdata(intf, data);
1016 kref_init(&data->kref);
1017 mutex_init(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001018 data->zombie = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001019
1020 /* Initialize USBTMC bTag and other fields */
1021 data->bTag = 1;
1022 data->TermCharEnabled = 0;
1023 data->TermChar = '\n';
1024
1025 /* USBTMC devices have only one setting, so use that */
1026 iface_desc = data->intf->cur_altsetting;
1027
1028 /* Find bulk in endpoint */
1029 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1030 endpoint = &iface_desc->endpoint[n].desc;
1031
1032 if (usb_endpoint_is_bulk_in(endpoint)) {
1033 data->bulk_in = endpoint->bEndpointAddress;
1034 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1035 data->bulk_in);
1036 break;
1037 }
1038 }
1039
1040 /* Find bulk out endpoint */
1041 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1042 endpoint = &iface_desc->endpoint[n].desc;
1043
1044 if (usb_endpoint_is_bulk_out(endpoint)) {
1045 data->bulk_out = endpoint->bEndpointAddress;
1046 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1047 data->bulk_out);
1048 break;
1049 }
1050 }
1051
1052 retcode = get_capabilities(data);
1053 if (retcode)
1054 dev_err(&intf->dev, "can't read capabilities\n");
1055 else
1056 retcode = sysfs_create_group(&intf->dev.kobj,
1057 &capability_attr_grp);
1058
1059 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1060
1061 retcode = usb_register_dev(intf, &usbtmc_class);
1062 if (retcode) {
1063 dev_err(&intf->dev, "Not able to get a minor"
1064 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1065 retcode);
1066 goto error_register;
1067 }
1068 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1069
1070 return 0;
1071
1072error_register:
1073 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1074 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1075 kref_put(&data->kref, usbtmc_delete);
1076 return retcode;
1077}
1078
1079static void usbtmc_disconnect(struct usb_interface *intf)
1080{
1081 struct usbtmc_device_data *data;
1082
1083 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1084
1085 data = usb_get_intfdata(intf);
1086 usb_deregister_dev(intf, &usbtmc_class);
1087 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1088 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
Oliver Neukum86286882009-07-02 11:36:30 +02001089 mutex_lock(&data->io_mutex);
1090 data->zombie = 1;
1091 mutex_unlock(&data->io_mutex);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001092 kref_put(&data->kref, usbtmc_delete);
1093}
1094
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001095static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
Oliver Neukuma47081032009-07-02 11:44:33 +02001096{
1097 /* this driver does not have pending URBs */
1098 return 0;
1099}
1100
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001101static int usbtmc_resume(struct usb_interface *intf)
Oliver Neukuma47081032009-07-02 11:44:33 +02001102{
1103 return 0;
1104}
1105
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001106static struct usb_driver usbtmc_driver = {
1107 .name = "usbtmc",
1108 .id_table = usbtmc_devices,
1109 .probe = usbtmc_probe,
Oliver Neukuma47081032009-07-02 11:44:33 +02001110 .disconnect = usbtmc_disconnect,
1111 .suspend = usbtmc_suspend,
1112 .resume = usbtmc_resume,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001113};
1114
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001115module_usb_driver(usbtmc_driver);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001116
1117MODULE_LICENSE("GPL");