blob: d77da2fd7b7053792543419ecbb30d64078ca7ee [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
Andy Shevchenkof4d844c2013-07-15 11:59:42 +030022#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070024#include <linux/module.h>
Ilpo Järvinen857cc4d2008-10-30 13:56:47 +020025#include <linux/kernel.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070026#include <linux/fs.h>
27#include <linux/uaccess.h>
28#include <linux/kref.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070030#include <linux/mutex.h>
31#include <linux/usb.h>
32#include <linux/usb/tmc.h>
33
34
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -050035#define RIGOL 1
36#define USBTMC_HEADER_SIZE 12
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070037#define USBTMC_MINOR_BASE 176
38
39/*
40 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
41 * large as wMaxPacketSize (which is usually 512 bytes).
42 */
43#define USBTMC_SIZE_IOBUFFER 2048
44
45/* Default USB timeout (in milliseconds) */
Gergely Imreh35f76e892009-09-15 16:03:31 +080046#define USBTMC_TIMEOUT 5000
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070047
48/*
49 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
50 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
51 * packet is never read.
52 */
53#define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
54
Németh Márton6ef48522010-01-10 15:33:45 +010055static const struct usb_device_id usbtmc_devices[] = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070056 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
Greg Kroah-Hartman228dd052009-03-11 13:51:42 -070057 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070058 { 0, } /* terminating entry */
59};
Greg Kroah-Hartman5413aa42008-12-03 16:33:09 -080060MODULE_DEVICE_TABLE(usb, usbtmc_devices);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070061
62/*
63 * This structure is the capabilities for the device
Gergely Imrehd0a38362009-09-07 10:47:01 +080064 * See section 4.2.1.8 of the USBTMC specification,
65 * and section 4.2.2 of the USBTMC usb488 subclass
66 * specification for details.
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070067 */
68struct usbtmc_dev_capabilities {
69 __u8 interface_capabilities;
70 __u8 device_capabilities;
71 __u8 usb488_interface_capabilities;
72 __u8 usb488_device_capabilities;
73};
74
75/* This structure holds private data for each USBTMC device. One copy is
76 * allocated for each USBTMC device in the driver's probe function.
77 */
78struct usbtmc_device_data {
79 const struct usb_device_id *id;
80 struct usb_device *usb_dev;
81 struct usb_interface *intf;
82
83 unsigned int bulk_in;
84 unsigned int bulk_out;
85
86 u8 bTag;
87 u8 bTag_last_write; /* needed for abort */
88 u8 bTag_last_read; /* needed for abort */
89
Dave Penklerdbf3e7f2016-01-27 19:09:24 +010090 /* data for interrupt in endpoint handling */
91 u8 bNotify1;
92 u8 bNotify2;
93 u16 ifnum;
94 u8 iin_bTag;
95 u8 *iin_buffer;
96 atomic_t iin_data_valid;
97 unsigned int iin_ep;
98 int iin_ep_present;
99 int iin_interval;
100 struct urb *iin_urb;
101 u16 iin_wMaxPacketSize;
Dave Penkler82ed3382016-01-27 19:15:15 +0100102 atomic_t srq_asserted;
Dave Penklerdbf3e7f2016-01-27 19:09:24 +0100103
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500104 u8 rigol_quirk;
105
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700106 /* attributes from the USB TMC spec for this device */
107 u8 TermChar;
108 bool TermCharEnabled;
109 bool auto_abort;
110
Oliver Neukum86286882009-07-02 11:36:30 +0200111 bool zombie; /* fd of disconnected device */
112
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700113 struct usbtmc_dev_capabilities capabilities;
114 struct kref kref;
115 struct mutex io_mutex; /* only one i/o function running at a time */
Dave Penklerdbf3e7f2016-01-27 19:09:24 +0100116 wait_queue_head_t waitq;
Dave Penkler82ed3382016-01-27 19:15:15 +0100117 struct fasync_struct *fasync;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700118};
119#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
120
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500121struct usbtmc_ID_rigol_quirk {
122 __u16 idVendor;
123 __u16 idProduct;
124};
125
126static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
127 { 0x1ab1, 0x0588 },
Teunis van Beelenf5042022015-05-31 09:36:22 +0200128 { 0x1ab1, 0x04b0 },
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500129 { 0, 0 }
130};
131
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700132/* Forward declarations */
133static struct usb_driver usbtmc_driver;
134
135static void usbtmc_delete(struct kref *kref)
136{
137 struct usbtmc_device_data *data = to_usbtmc_data(kref);
138
139 usb_put_dev(data->usb_dev);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700140}
141
142static int usbtmc_open(struct inode *inode, struct file *filp)
143{
144 struct usb_interface *intf;
145 struct usbtmc_device_data *data;
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700146 int retval = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700147
148 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
149 if (!intf) {
Andy Shevchenkof4d844c2013-07-15 11:59:42 +0300150 pr_err("can not find device for minor %d", iminor(inode));
151 return -ENODEV;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700152 }
153
154 data = usb_get_intfdata(intf);
155 kref_get(&data->kref);
156
157 /* Store pointer in file structure's private data field */
158 filp->private_data = data;
159
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700160 return retval;
161}
162
163static int usbtmc_release(struct inode *inode, struct file *file)
164{
165 struct usbtmc_device_data *data = file->private_data;
166
167 kref_put(&data->kref, usbtmc_delete);
168 return 0;
169}
170
171static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
172{
Chris Malleyb361a6e2008-10-25 22:07:32 +0100173 u8 *buffer;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700174 struct device *dev;
175 int rv;
176 int n;
177 int actual;
178 struct usb_host_interface *current_setting;
179 int max_size;
180
181 dev = &data->intf->dev;
182 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
183 if (!buffer)
184 return -ENOMEM;
185
186 rv = usb_control_msg(data->usb_dev,
187 usb_rcvctrlpipe(data->usb_dev, 0),
188 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
189 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
190 data->bTag_last_read, data->bulk_in,
191 buffer, 2, USBTMC_TIMEOUT);
192
193 if (rv < 0) {
194 dev_err(dev, "usb_control_msg returned %d\n", rv);
195 goto exit;
196 }
197
198 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
199
200 if (buffer[0] == USBTMC_STATUS_FAILED) {
201 rv = 0;
202 goto exit;
203 }
204
205 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
206 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
207 buffer[0]);
208 rv = -EPERM;
209 goto exit;
210 }
211
212 max_size = 0;
213 current_setting = data->intf->cur_altsetting;
214 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
215 if (current_setting->endpoint[n].desc.bEndpointAddress ==
216 data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700217 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700218
219 if (max_size == 0) {
220 dev_err(dev, "Couldn't get wMaxPacketSize\n");
221 rv = -EPERM;
222 goto exit;
223 }
224
225 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
226
227 n = 0;
228
229 do {
230 dev_dbg(dev, "Reading from bulk in EP\n");
231
232 rv = usb_bulk_msg(data->usb_dev,
233 usb_rcvbulkpipe(data->usb_dev,
234 data->bulk_in),
235 buffer, USBTMC_SIZE_IOBUFFER,
236 &actual, USBTMC_TIMEOUT);
237
238 n++;
239
240 if (rv < 0) {
241 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
242 goto exit;
243 }
244 } while ((actual == max_size) &&
245 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
246
247 if (actual == max_size) {
248 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
249 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
250 rv = -EPERM;
251 goto exit;
252 }
253
254 n = 0;
255
256usbtmc_abort_bulk_in_status:
257 rv = usb_control_msg(data->usb_dev,
258 usb_rcvctrlpipe(data->usb_dev, 0),
259 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
260 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
261 0, data->bulk_in, buffer, 0x08,
262 USBTMC_TIMEOUT);
263
264 if (rv < 0) {
265 dev_err(dev, "usb_control_msg returned %d\n", rv);
266 goto exit;
267 }
268
269 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
270
271 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
272 rv = 0;
273 goto exit;
274 }
275
276 if (buffer[0] != USBTMC_STATUS_PENDING) {
277 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
278 rv = -EPERM;
279 goto exit;
280 }
281
282 if (buffer[1] == 1)
283 do {
284 dev_dbg(dev, "Reading from bulk in EP\n");
285
286 rv = usb_bulk_msg(data->usb_dev,
287 usb_rcvbulkpipe(data->usb_dev,
288 data->bulk_in),
289 buffer, USBTMC_SIZE_IOBUFFER,
290 &actual, USBTMC_TIMEOUT);
291
292 n++;
293
294 if (rv < 0) {
295 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
296 goto exit;
297 }
Maxim Nikulin4f1a7a32011-07-09 23:44:44 +0700298 } while ((actual == max_size) &&
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700299 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
300
301 if (actual == max_size) {
302 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
303 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
304 rv = -EPERM;
305 goto exit;
306 }
307
308 goto usbtmc_abort_bulk_in_status;
309
310exit:
311 kfree(buffer);
312 return rv;
313
314}
315
316static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
317{
318 struct device *dev;
319 u8 *buffer;
320 int rv;
321 int n;
322
323 dev = &data->intf->dev;
324
325 buffer = kmalloc(8, GFP_KERNEL);
326 if (!buffer)
327 return -ENOMEM;
328
329 rv = usb_control_msg(data->usb_dev,
330 usb_rcvctrlpipe(data->usb_dev, 0),
331 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
332 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
333 data->bTag_last_write, data->bulk_out,
334 buffer, 2, USBTMC_TIMEOUT);
335
336 if (rv < 0) {
337 dev_err(dev, "usb_control_msg returned %d\n", rv);
338 goto exit;
339 }
340
341 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
342
343 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
344 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
345 buffer[0]);
346 rv = -EPERM;
347 goto exit;
348 }
349
350 n = 0;
351
352usbtmc_abort_bulk_out_check_status:
353 rv = usb_control_msg(data->usb_dev,
354 usb_rcvctrlpipe(data->usb_dev, 0),
355 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
356 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
357 0, data->bulk_out, buffer, 0x08,
358 USBTMC_TIMEOUT);
359 n++;
360 if (rv < 0) {
361 dev_err(dev, "usb_control_msg returned %d\n", rv);
362 goto exit;
363 }
364
365 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
366
367 if (buffer[0] == USBTMC_STATUS_SUCCESS)
368 goto usbtmc_abort_bulk_out_clear_halt;
369
370 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
371 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
372 goto usbtmc_abort_bulk_out_check_status;
373
374 rv = -EPERM;
375 goto exit;
376
377usbtmc_abort_bulk_out_clear_halt:
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800378 rv = usb_clear_halt(data->usb_dev,
379 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700380
381 if (rv < 0) {
382 dev_err(dev, "usb_control_msg returned %d\n", rv);
383 goto exit;
384 }
385 rv = 0;
386
387exit:
388 kfree(buffer);
389 return rv;
390}
391
Dave Penklerdbf3e7f2016-01-27 19:09:24 +0100392static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data *data,
393 void __user *arg)
394{
395 struct device *dev = &data->intf->dev;
396 u8 *buffer;
397 u8 tag;
398 __u8 stb;
399 int rv;
400
401 dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n",
402 data->iin_ep_present);
403
404 buffer = kmalloc(8, GFP_KERNEL);
405 if (!buffer)
406 return -ENOMEM;
407
408 atomic_set(&data->iin_data_valid, 0);
409
Dave Penkler82ed3382016-01-27 19:15:15 +0100410 /* must issue read_stb before using poll or select */
411 atomic_set(&data->srq_asserted, 0);
412
Dave Penklerdbf3e7f2016-01-27 19:09:24 +0100413 rv = usb_control_msg(data->usb_dev,
414 usb_rcvctrlpipe(data->usb_dev, 0),
415 USBTMC488_REQUEST_READ_STATUS_BYTE,
416 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
417 data->iin_bTag,
418 data->ifnum,
419 buffer, 0x03, USBTMC_TIMEOUT);
420 if (rv < 0) {
421 dev_err(dev, "stb usb_control_msg returned %d\n", rv);
422 goto exit;
423 }
424
425 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
426 dev_err(dev, "control status returned %x\n", buffer[0]);
427 rv = -EIO;
428 goto exit;
429 }
430
431 if (data->iin_ep_present) {
432 rv = wait_event_interruptible_timeout(
433 data->waitq,
434 atomic_read(&data->iin_data_valid) != 0,
435 USBTMC_TIMEOUT);
436 if (rv < 0) {
437 dev_dbg(dev, "wait interrupted %d\n", rv);
438 goto exit;
439 }
440
441 if (rv == 0) {
442 dev_dbg(dev, "wait timed out\n");
443 rv = -ETIME;
444 goto exit;
445 }
446
447 tag = data->bNotify1 & 0x7f;
448 if (tag != data->iin_bTag) {
449 dev_err(dev, "expected bTag %x got %x\n",
450 data->iin_bTag, tag);
451 }
452
453 stb = data->bNotify2;
454 } else {
455 stb = buffer[2];
456 }
457
458 rv = copy_to_user(arg, &stb, sizeof(stb));
459 if (rv)
460 rv = -EFAULT;
461
462 exit:
463 /* bump interrupt bTag */
464 data->iin_bTag += 1;
465 if (data->iin_bTag > 127)
466 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
467 data->iin_bTag = 2;
468
469 kfree(buffer);
470 return rv;
471}
472
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500473/*
474 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
475 * @transfer_size: number of bytes to request from the device.
476 *
477 * See the USBTMC specification, Table 4.
478 *
479 * Also updates bTag_last_write.
480 */
481static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
482{
483 int retval;
Oliver Neukumd846b762014-05-19 13:54:57 +0200484 u8 *buffer;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500485 int actual;
486
Oliver Neukumd846b762014-05-19 13:54:57 +0200487 buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
488 if (!buffer)
489 return -ENOMEM;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500490 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
491 * Refer to class specs for details
492 */
493 buffer[0] = 2;
494 buffer[1] = data->bTag;
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300495 buffer[2] = ~data->bTag;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500496 buffer[3] = 0; /* Reserved */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300497 buffer[4] = transfer_size >> 0;
498 buffer[5] = transfer_size >> 8;
499 buffer[6] = transfer_size >> 16;
500 buffer[7] = transfer_size >> 24;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500501 buffer[8] = data->TermCharEnabled * 2;
502 /* Use term character? */
503 buffer[9] = data->TermChar;
504 buffer[10] = 0; /* Reserved */
505 buffer[11] = 0; /* Reserved */
506
507 /* Send bulk URB */
508 retval = usb_bulk_msg(data->usb_dev,
509 usb_sndbulkpipe(data->usb_dev,
510 data->bulk_out),
511 buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
512
513 /* Store bTag (in case we need to abort) */
514 data->bTag_last_write = data->bTag;
515
516 /* Increment bTag -- and increment again if zero */
517 data->bTag++;
518 if (!data->bTag)
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300519 data->bTag++;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500520
Oliver Neukumd846b762014-05-19 13:54:57 +0200521 kfree(buffer);
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500522 if (retval < 0) {
523 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
524 return retval;
525 }
526
527 return 0;
528}
529
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700530static ssize_t usbtmc_read(struct file *filp, char __user *buf,
531 size_t count, loff_t *f_pos)
532{
533 struct usbtmc_device_data *data;
534 struct device *dev;
Guus Sliepen665d7662009-07-22 17:39:42 +0200535 u32 n_characters;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700536 u8 *buffer;
537 int actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200538 size_t done;
539 size_t remaining;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700540 int retval;
Guus Sliepen665d7662009-07-22 17:39:42 +0200541 size_t this_part;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700542
543 /* Get pointer to private data structure */
544 data = filp->private_data;
545 dev = &data->intf->dev;
546
547 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
548 if (!buffer)
549 return -ENOMEM;
550
551 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200552 if (data->zombie) {
553 retval = -ENODEV;
554 goto exit;
555 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700556
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500557 if (data->rigol_quirk) {
558 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700559
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500560 retval = send_request_dev_dep_msg_in(data, count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700561
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700562 if (retval < 0) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700563 if (data->auto_abort)
564 usbtmc_ioctl_abort_bulk_out(data);
565 goto exit;
566 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500567 }
568
569 /* Loop until we have fetched everything we requested */
570 remaining = count;
571 this_part = remaining;
572 done = 0;
573
574 while (remaining > 0) {
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300575 if (!data->rigol_quirk) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500576 dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
577
578 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
579 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
580 else
581 this_part = remaining;
582
583 retval = send_request_dev_dep_msg_in(data, this_part);
584 if (retval < 0) {
585 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
586 if (data->auto_abort)
587 usbtmc_ioctl_abort_bulk_out(data);
588 goto exit;
589 }
590 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700591
592 /* Send bulk URB */
593 retval = usb_bulk_msg(data->usb_dev,
594 usb_rcvbulkpipe(data->usb_dev,
595 data->bulk_in),
596 buffer, USBTMC_SIZE_IOBUFFER, &actual,
597 USBTMC_TIMEOUT);
598
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500599 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
600
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700601 /* Store bTag (in case we need to abort) */
602 data->bTag_last_read = data->bTag;
603
604 if (retval < 0) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500605 dev_dbg(dev, "Unable to read data, error %d\n", retval);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700606 if (data->auto_abort)
607 usbtmc_ioctl_abort_bulk_in(data);
608 goto exit;
609 }
610
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500611 /* Parse header in first packet */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300612 if ((done == 0) || !data->rigol_quirk) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500613 /* Sanity checks for the header */
614 if (actual < USBTMC_HEADER_SIZE) {
615 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
616 if (data->auto_abort)
617 usbtmc_ioctl_abort_bulk_in(data);
618 goto exit;
619 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700620
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500621 if (buffer[0] != 2) {
622 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
623 if (data->auto_abort)
624 usbtmc_ioctl_abort_bulk_in(data);
625 goto exit;
626 }
627
628 if (buffer[1] != data->bTag_last_write) {
629 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
630 if (data->auto_abort)
631 usbtmc_ioctl_abort_bulk_in(data);
632 goto exit;
633 }
634
635 /* How many characters did the instrument send? */
636 n_characters = buffer[4] +
637 (buffer[5] << 8) +
638 (buffer[6] << 16) +
639 (buffer[7] << 24);
640
641 if (n_characters > this_part) {
642 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
643 if (data->auto_abort)
644 usbtmc_ioctl_abort_bulk_in(data);
645 goto exit;
646 }
647
648 /* Remove the USBTMC header */
649 actual -= USBTMC_HEADER_SIZE;
650
651 /* Check if the message is smaller than requested */
652 if (data->rigol_quirk) {
653 if (remaining > n_characters)
654 remaining = n_characters;
655 /* Remove padding if it exists */
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300656 if (actual > remaining)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500657 actual = remaining;
658 }
659 else {
660 if (this_part > n_characters)
661 this_part = n_characters;
662 /* Remove padding if it exists */
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300663 if (actual > this_part)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500664 actual = this_part;
665 }
666
667 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
668
669 remaining -= actual;
670
671 /* Terminate if end-of-message bit received from device */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300672 if ((buffer[8] & 0x01) && (actual >= n_characters))
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500673 remaining = 0;
674
675 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
676
677
678 /* Copy buffer to user space */
679 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
680 /* There must have been an addressing problem */
681 retval = -EFAULT;
682 goto exit;
683 }
684 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200685 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500686 else {
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300687 if (actual > remaining)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500688 actual = remaining;
Guus Sliepen665d7662009-07-22 17:39:42 +0200689
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500690 remaining -= actual;
691
692 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
693
694 /* Copy buffer to user space */
695 if (copy_to_user(buf + done, buffer, actual)) {
696 /* There must have been an addressing problem */
697 retval = -EFAULT;
698 goto exit;
699 }
700 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200701 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700702 }
703
704 /* Update file position value */
705 *f_pos = *f_pos + done;
706 retval = done;
707
708exit:
709 mutex_unlock(&data->io_mutex);
710 kfree(buffer);
711 return retval;
712}
713
714static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
715 size_t count, loff_t *f_pos)
716{
717 struct usbtmc_device_data *data;
718 u8 *buffer;
719 int retval;
720 int actual;
721 unsigned long int n_bytes;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700722 int remaining;
723 int done;
724 int this_part;
725
726 data = filp->private_data;
727
728 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
729 if (!buffer)
730 return -ENOMEM;
731
732 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200733 if (data->zombie) {
734 retval = -ENODEV;
735 goto exit;
736 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700737
738 remaining = count;
739 done = 0;
740
741 while (remaining > 0) {
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500742 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
743 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700744 buffer[8] = 0;
745 } else {
746 this_part = remaining;
747 buffer[8] = 1;
748 }
749
750 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
751 buffer[0] = 1;
752 buffer[1] = data->bTag;
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300753 buffer[2] = ~data->bTag;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700754 buffer[3] = 0; /* Reserved */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300755 buffer[4] = this_part >> 0;
756 buffer[5] = this_part >> 8;
757 buffer[6] = this_part >> 16;
758 buffer[7] = this_part >> 24;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700759 /* buffer[8] is set above... */
760 buffer[9] = 0; /* Reserved */
761 buffer[10] = 0; /* Reserved */
762 buffer[11] = 0; /* Reserved */
763
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500764 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700765 retval = -EFAULT;
766 goto exit;
767 }
768
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500769 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
770 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700771
Andre Hermsec412b92009-11-19 18:14:49 +0100772 do {
773 retval = usb_bulk_msg(data->usb_dev,
774 usb_sndbulkpipe(data->usb_dev,
775 data->bulk_out),
776 buffer, n_bytes,
777 &actual, USBTMC_TIMEOUT);
778 if (retval != 0)
779 break;
780 n_bytes -= actual;
781 } while (n_bytes);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700782
783 data->bTag_last_write = data->bTag;
784 data->bTag++;
785
786 if (!data->bTag)
787 data->bTag++;
788
789 if (retval < 0) {
790 dev_err(&data->intf->dev,
791 "Unable to send data, error %d\n", retval);
792 if (data->auto_abort)
793 usbtmc_ioctl_abort_bulk_out(data);
794 goto exit;
795 }
796
797 remaining -= this_part;
798 done += this_part;
799 }
800
801 retval = count;
802exit:
803 mutex_unlock(&data->io_mutex);
804 kfree(buffer);
805 return retval;
806}
807
808static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
809{
810 struct usb_host_interface *current_setting;
811 struct usb_endpoint_descriptor *desc;
812 struct device *dev;
813 u8 *buffer;
814 int rv;
815 int n;
Rickard Strandqvistab53eb92014-06-01 15:43:19 +0200816 int actual = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700817 int max_size;
818
819 dev = &data->intf->dev;
820
821 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
822
823 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
824 if (!buffer)
825 return -ENOMEM;
826
827 rv = usb_control_msg(data->usb_dev,
828 usb_rcvctrlpipe(data->usb_dev, 0),
829 USBTMC_REQUEST_INITIATE_CLEAR,
830 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
831 0, 0, buffer, 1, USBTMC_TIMEOUT);
832 if (rv < 0) {
833 dev_err(dev, "usb_control_msg returned %d\n", rv);
834 goto exit;
835 }
836
837 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
838
839 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
840 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
841 rv = -EPERM;
842 goto exit;
843 }
844
845 max_size = 0;
846 current_setting = data->intf->cur_altsetting;
847 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
848 desc = &current_setting->endpoint[n].desc;
849 if (desc->bEndpointAddress == data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700850 max_size = usb_endpoint_maxp(desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700851 }
852
853 if (max_size == 0) {
854 dev_err(dev, "Couldn't get wMaxPacketSize\n");
855 rv = -EPERM;
856 goto exit;
857 }
858
859 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
860
861 n = 0;
862
863usbtmc_clear_check_status:
864
865 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
866
867 rv = usb_control_msg(data->usb_dev,
868 usb_rcvctrlpipe(data->usb_dev, 0),
869 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
870 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
871 0, 0, buffer, 2, USBTMC_TIMEOUT);
872 if (rv < 0) {
873 dev_err(dev, "usb_control_msg returned %d\n", rv);
874 goto exit;
875 }
876
877 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
878
879 if (buffer[0] == USBTMC_STATUS_SUCCESS)
880 goto usbtmc_clear_bulk_out_halt;
881
882 if (buffer[0] != USBTMC_STATUS_PENDING) {
883 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
884 rv = -EPERM;
885 goto exit;
886 }
887
888 if (buffer[1] == 1)
889 do {
890 dev_dbg(dev, "Reading from bulk in EP\n");
891
892 rv = usb_bulk_msg(data->usb_dev,
893 usb_rcvbulkpipe(data->usb_dev,
894 data->bulk_in),
895 buffer, USBTMC_SIZE_IOBUFFER,
896 &actual, USBTMC_TIMEOUT);
897 n++;
898
899 if (rv < 0) {
900 dev_err(dev, "usb_control_msg returned %d\n",
901 rv);
902 goto exit;
903 }
904 } while ((actual == max_size) &&
905 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
906
907 if (actual == max_size) {
908 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
909 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
910 rv = -EPERM;
911 goto exit;
912 }
913
914 goto usbtmc_clear_check_status;
915
916usbtmc_clear_bulk_out_halt:
917
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800918 rv = usb_clear_halt(data->usb_dev,
919 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700920 if (rv < 0) {
921 dev_err(dev, "usb_control_msg returned %d\n", rv);
922 goto exit;
923 }
924 rv = 0;
925
926exit:
927 kfree(buffer);
928 return rv;
929}
930
931static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
932{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700933 int rv;
934
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800935 rv = usb_clear_halt(data->usb_dev,
936 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700937
938 if (rv < 0) {
939 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
940 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800941 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700942 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800943 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700944}
945
946static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
947{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700948 int rv;
949
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800950 rv = usb_clear_halt(data->usb_dev,
951 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700952
953 if (rv < 0) {
954 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
955 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800956 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700957 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800958 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700959}
960
961static int get_capabilities(struct usbtmc_device_data *data)
962{
963 struct device *dev = &data->usb_dev->dev;
964 char *buffer;
Oliver Neukumca157c42009-07-02 16:41:39 +0200965 int rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700966
967 buffer = kmalloc(0x18, GFP_KERNEL);
968 if (!buffer)
969 return -ENOMEM;
970
971 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
972 USBTMC_REQUEST_GET_CAPABILITIES,
973 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
974 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
975 if (rv < 0) {
976 dev_err(dev, "usb_control_msg returned %d\n", rv);
Oliver Neukumca157c42009-07-02 16:41:39 +0200977 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700978 }
979
980 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700981 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
982 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Oliver Neukumca157c42009-07-02 16:41:39 +0200983 rv = -EPERM;
984 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700985 }
Gergely Imrehd0a38362009-09-07 10:47:01 +0800986 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
987 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
988 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
989 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700990
991 data->capabilities.interface_capabilities = buffer[4];
992 data->capabilities.device_capabilities = buffer[5];
993 data->capabilities.usb488_interface_capabilities = buffer[14];
994 data->capabilities.usb488_device_capabilities = buffer[15];
Gergely Imrehd0a38362009-09-07 10:47:01 +0800995 rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700996
Oliver Neukumca157c42009-07-02 16:41:39 +0200997err_out:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700998 kfree(buffer);
Oliver Neukumca157c42009-07-02 16:41:39 +0200999 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001000}
1001
1002#define capability_attribute(name) \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001003static ssize_t name##_show(struct device *dev, \
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001004 struct device_attribute *attr, char *buf) \
1005{ \
1006 struct usb_interface *intf = to_usb_interface(dev); \
1007 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1008 \
1009 return sprintf(buf, "%d\n", data->capabilities.name); \
1010} \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001011static DEVICE_ATTR_RO(name)
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001012
1013capability_attribute(interface_capabilities);
1014capability_attribute(device_capabilities);
1015capability_attribute(usb488_interface_capabilities);
1016capability_attribute(usb488_device_capabilities);
1017
1018static struct attribute *capability_attrs[] = {
1019 &dev_attr_interface_capabilities.attr,
1020 &dev_attr_device_capabilities.attr,
1021 &dev_attr_usb488_interface_capabilities.attr,
1022 &dev_attr_usb488_device_capabilities.attr,
1023 NULL,
1024};
1025
1026static struct attribute_group capability_attr_grp = {
1027 .attrs = capability_attrs,
1028};
1029
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001030static ssize_t TermChar_show(struct device *dev,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001031 struct device_attribute *attr, char *buf)
1032{
1033 struct usb_interface *intf = to_usb_interface(dev);
1034 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1035
1036 return sprintf(buf, "%c\n", data->TermChar);
1037}
1038
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001039static ssize_t TermChar_store(struct device *dev,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001040 struct device_attribute *attr,
1041 const char *buf, size_t count)
1042{
1043 struct usb_interface *intf = to_usb_interface(dev);
1044 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1045
1046 if (count < 1)
1047 return -EINVAL;
1048 data->TermChar = buf[0];
1049 return count;
1050}
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001051static DEVICE_ATTR_RW(TermChar);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001052
1053#define data_attribute(name) \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001054static ssize_t name##_show(struct device *dev, \
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001055 struct device_attribute *attr, char *buf) \
1056{ \
1057 struct usb_interface *intf = to_usb_interface(dev); \
1058 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1059 \
1060 return sprintf(buf, "%d\n", data->name); \
1061} \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001062static ssize_t name##_store(struct device *dev, \
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001063 struct device_attribute *attr, \
1064 const char *buf, size_t count) \
1065{ \
1066 struct usb_interface *intf = to_usb_interface(dev); \
1067 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1068 ssize_t result; \
1069 unsigned val; \
1070 \
1071 result = sscanf(buf, "%u\n", &val); \
1072 if (result != 1) \
1073 result = -EINVAL; \
1074 data->name = val; \
1075 if (result < 0) \
1076 return result; \
1077 else \
1078 return count; \
1079} \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -07001080static DEVICE_ATTR_RW(name)
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001081
1082data_attribute(TermCharEnabled);
1083data_attribute(auto_abort);
1084
1085static struct attribute *data_attrs[] = {
1086 &dev_attr_TermChar.attr,
1087 &dev_attr_TermCharEnabled.attr,
1088 &dev_attr_auto_abort.attr,
1089 NULL,
1090};
1091
1092static struct attribute_group data_attr_grp = {
1093 .attrs = data_attrs,
1094};
1095
1096static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
1097{
1098 struct device *dev;
1099 u8 *buffer;
1100 int rv;
1101
1102 dev = &data->intf->dev;
1103
1104 buffer = kmalloc(2, GFP_KERNEL);
1105 if (!buffer)
1106 return -ENOMEM;
1107
1108 rv = usb_control_msg(data->usb_dev,
1109 usb_rcvctrlpipe(data->usb_dev, 0),
1110 USBTMC_REQUEST_INDICATOR_PULSE,
1111 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1112 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1113
1114 if (rv < 0) {
1115 dev_err(dev, "usb_control_msg returned %d\n", rv);
1116 goto exit;
1117 }
1118
1119 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1120
1121 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1122 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1123 rv = -EPERM;
1124 goto exit;
1125 }
1126 rv = 0;
1127
1128exit:
1129 kfree(buffer);
1130 return rv;
1131}
1132
1133static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1134{
1135 struct usbtmc_device_data *data;
1136 int retval = -EBADRQC;
1137
1138 data = file->private_data;
1139 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001140 if (data->zombie) {
1141 retval = -ENODEV;
1142 goto skip_io_on_zombie;
1143 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001144
1145 switch (cmd) {
1146 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1147 retval = usbtmc_ioctl_clear_out_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001148 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001149
1150 case USBTMC_IOCTL_CLEAR_IN_HALT:
1151 retval = usbtmc_ioctl_clear_in_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001152 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001153
1154 case USBTMC_IOCTL_INDICATOR_PULSE:
1155 retval = usbtmc_ioctl_indicator_pulse(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001156 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001157
1158 case USBTMC_IOCTL_CLEAR:
1159 retval = usbtmc_ioctl_clear(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001160 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001161
1162 case USBTMC_IOCTL_ABORT_BULK_OUT:
1163 retval = usbtmc_ioctl_abort_bulk_out(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001164 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001165
1166 case USBTMC_IOCTL_ABORT_BULK_IN:
1167 retval = usbtmc_ioctl_abort_bulk_in(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001168 break;
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001169
1170 case USBTMC488_IOCTL_READ_STB:
1171 retval = usbtmc488_ioctl_read_stb(data, (void __user *)arg);
1172 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001173 }
1174
Oliver Neukum86286882009-07-02 11:36:30 +02001175skip_io_on_zombie:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001176 mutex_unlock(&data->io_mutex);
1177 return retval;
1178}
1179
Dave Penkler82ed3382016-01-27 19:15:15 +01001180static int usbtmc_fasync(int fd, struct file *file, int on)
1181{
1182 struct usbtmc_device_data *data = file->private_data;
1183
1184 return fasync_helper(fd, file, on, &data->fasync);
1185}
1186
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001187static const struct file_operations fops = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001188 .owner = THIS_MODULE,
1189 .read = usbtmc_read,
1190 .write = usbtmc_write,
1191 .open = usbtmc_open,
1192 .release = usbtmc_release,
1193 .unlocked_ioctl = usbtmc_ioctl,
Dave Penkler82ed3382016-01-27 19:15:15 +01001194 .fasync = usbtmc_fasync,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001195 .llseek = default_llseek,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001196};
1197
1198static struct usb_class_driver usbtmc_class = {
1199 .name = "usbtmc%d",
1200 .fops = &fops,
1201 .minor_base = USBTMC_MINOR_BASE,
1202};
1203
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001204static void usbtmc_interrupt(struct urb *urb)
1205{
1206 struct usbtmc_device_data *data = urb->context;
1207 struct device *dev = &data->intf->dev;
1208 int status = urb->status;
1209 int rv;
1210
1211 dev_dbg(&data->intf->dev, "int status: %d len %d\n",
1212 status, urb->actual_length);
1213
1214 switch (status) {
1215 case 0: /* SUCCESS */
1216 /* check for valid STB notification */
1217 if (data->iin_buffer[0] > 0x81) {
1218 data->bNotify1 = data->iin_buffer[0];
1219 data->bNotify2 = data->iin_buffer[1];
1220 atomic_set(&data->iin_data_valid, 1);
1221 wake_up_interruptible(&data->waitq);
1222 goto exit;
1223 }
Dave Penkler82ed3382016-01-27 19:15:15 +01001224 /* check for SRQ notification */
1225 if (data->iin_buffer[0] == 0x81) {
1226 if (data->fasync)
1227 kill_fasync(&data->fasync,
1228 SIGIO, POLL_IN);
1229
1230 atomic_set(&data->srq_asserted, 1);
1231 wake_up_interruptible(&data->waitq);
1232 goto exit;
1233 }
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001234 dev_warn(dev, "invalid notification: %x\n", data->iin_buffer[0]);
1235 break;
1236 case -EOVERFLOW:
1237 dev_err(dev, "overflow with length %d, actual length is %d\n",
1238 data->iin_wMaxPacketSize, urb->actual_length);
1239 case -ECONNRESET:
1240 case -ENOENT:
1241 case -ESHUTDOWN:
1242 case -EILSEQ:
1243 case -ETIME:
1244 /* urb terminated, clean up */
1245 dev_dbg(dev, "urb terminated, status: %d\n", status);
1246 return;
1247 default:
1248 dev_err(dev, "unknown status received: %d\n", status);
1249 }
1250exit:
1251 rv = usb_submit_urb(urb, GFP_ATOMIC);
1252 if (rv)
1253 dev_err(dev, "usb_submit_urb failed: %d\n", rv);
1254}
1255
1256static void usbtmc_free_int(struct usbtmc_device_data *data)
1257{
1258 if (!data->iin_ep_present || !data->iin_urb)
1259 return;
1260 usb_kill_urb(data->iin_urb);
1261 kfree(data->iin_buffer);
1262 usb_free_urb(data->iin_urb);
1263 kref_put(&data->kref, usbtmc_delete);
1264}
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001265
1266static int usbtmc_probe(struct usb_interface *intf,
1267 const struct usb_device_id *id)
1268{
1269 struct usbtmc_device_data *data;
1270 struct usb_host_interface *iface_desc;
1271 struct usb_endpoint_descriptor *endpoint;
1272 int n;
1273 int retcode;
1274
1275 dev_dbg(&intf->dev, "%s called\n", __func__);
1276
Andy Shevchenkoe6c7efd2013-07-15 11:59:41 +03001277 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Peter Chenb8f28542014-10-14 15:55:56 +08001278 if (!data)
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001279 return -ENOMEM;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001280
1281 data->intf = intf;
1282 data->id = id;
1283 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1284 usb_set_intfdata(intf, data);
1285 kref_init(&data->kref);
1286 mutex_init(&data->io_mutex);
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001287 init_waitqueue_head(&data->waitq);
1288 atomic_set(&data->iin_data_valid, 0);
Dave Penkler82ed3382016-01-27 19:15:15 +01001289 atomic_set(&data->srq_asserted, 0);
Oliver Neukum86286882009-07-02 11:36:30 +02001290 data->zombie = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001291
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001292 /* Determine if it is a Rigol or not */
1293 data->rigol_quirk = 0;
1294 dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
Johan Hovoldc7861382013-08-11 16:49:21 +02001295 le16_to_cpu(data->usb_dev->descriptor.idVendor),
1296 le16_to_cpu(data->usb_dev->descriptor.idProduct));
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001297 for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
Johan Hovoldc7861382013-08-11 16:49:21 +02001298 if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) &&
1299 (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) {
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001300 dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1301 data->rigol_quirk = 1;
1302 break;
1303 }
1304 }
1305
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001306 /* Initialize USBTMC bTag and other fields */
1307 data->bTag = 1;
1308 data->TermCharEnabled = 0;
1309 data->TermChar = '\n';
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001310 /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
1311 data->iin_bTag = 2;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001312
1313 /* USBTMC devices have only one setting, so use that */
1314 iface_desc = data->intf->cur_altsetting;
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001315 data->ifnum = iface_desc->desc.bInterfaceNumber;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001316
1317 /* Find bulk in endpoint */
1318 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1319 endpoint = &iface_desc->endpoint[n].desc;
1320
1321 if (usb_endpoint_is_bulk_in(endpoint)) {
1322 data->bulk_in = endpoint->bEndpointAddress;
1323 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1324 data->bulk_in);
1325 break;
1326 }
1327 }
1328
1329 /* Find bulk out endpoint */
1330 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1331 endpoint = &iface_desc->endpoint[n].desc;
1332
1333 if (usb_endpoint_is_bulk_out(endpoint)) {
1334 data->bulk_out = endpoint->bEndpointAddress;
1335 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1336 data->bulk_out);
1337 break;
1338 }
1339 }
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001340 /* Find int endpoint */
1341 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1342 endpoint = &iface_desc->endpoint[n].desc;
1343
1344 if (usb_endpoint_is_int_in(endpoint)) {
1345 data->iin_ep_present = 1;
1346 data->iin_ep = endpoint->bEndpointAddress;
1347 data->iin_wMaxPacketSize = usb_endpoint_maxp(endpoint);
1348 data->iin_interval = endpoint->bInterval;
1349 dev_dbg(&intf->dev, "Found Int in endpoint at %u\n",
1350 data->iin_ep);
1351 break;
1352 }
1353 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001354
1355 retcode = get_capabilities(data);
1356 if (retcode)
1357 dev_err(&intf->dev, "can't read capabilities\n");
1358 else
1359 retcode = sysfs_create_group(&intf->dev.kobj,
1360 &capability_attr_grp);
1361
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001362 if (data->iin_ep_present) {
1363 /* allocate int urb */
1364 data->iin_urb = usb_alloc_urb(0, GFP_KERNEL);
1365 if (!data->iin_urb) {
1366 dev_err(&intf->dev, "Failed to allocate int urb\n");
1367 goto error_register;
1368 }
1369
1370 /* will reference data in int urb */
1371 kref_get(&data->kref);
1372
1373 /* allocate buffer for interrupt in */
1374 data->iin_buffer = kmalloc(data->iin_wMaxPacketSize,
1375 GFP_KERNEL);
1376 if (!data->iin_buffer) {
1377 dev_err(&intf->dev, "Failed to allocate int buf\n");
1378 goto error_register;
1379 }
1380
1381 /* fill interrupt urb */
1382 usb_fill_int_urb(data->iin_urb, data->usb_dev,
1383 usb_rcvintpipe(data->usb_dev, data->iin_ep),
1384 data->iin_buffer, data->iin_wMaxPacketSize,
1385 usbtmc_interrupt,
1386 data, data->iin_interval);
1387
1388 retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL);
1389 if (retcode) {
1390 dev_err(&intf->dev, "Failed to submit iin_urb\n");
1391 goto error_register;
1392 }
1393 }
1394
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001395 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1396
1397 retcode = usb_register_dev(intf, &usbtmc_class);
1398 if (retcode) {
1399 dev_err(&intf->dev, "Not able to get a minor"
1400 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1401 retcode);
1402 goto error_register;
1403 }
1404 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1405
1406 return 0;
1407
1408error_register:
1409 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1410 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001411 usbtmc_free_int(data);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001412 kref_put(&data->kref, usbtmc_delete);
1413 return retcode;
1414}
1415
1416static void usbtmc_disconnect(struct usb_interface *intf)
1417{
1418 struct usbtmc_device_data *data;
1419
1420 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1421
1422 data = usb_get_intfdata(intf);
Dave Penklerdbf3e7f2016-01-27 19:09:24 +01001423 usbtmc_free_int(data);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001424 usb_deregister_dev(intf, &usbtmc_class);
1425 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1426 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
Oliver Neukum86286882009-07-02 11:36:30 +02001427 mutex_lock(&data->io_mutex);
1428 data->zombie = 1;
1429 mutex_unlock(&data->io_mutex);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001430 kref_put(&data->kref, usbtmc_delete);
1431}
1432
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001433static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
Oliver Neukuma4708102009-07-02 11:44:33 +02001434{
1435 /* this driver does not have pending URBs */
1436 return 0;
1437}
1438
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001439static int usbtmc_resume(struct usb_interface *intf)
Oliver Neukuma4708102009-07-02 11:44:33 +02001440{
1441 return 0;
1442}
1443
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001444static struct usb_driver usbtmc_driver = {
1445 .name = "usbtmc",
1446 .id_table = usbtmc_devices,
1447 .probe = usbtmc_probe,
Oliver Neukuma4708102009-07-02 11:44:33 +02001448 .disconnect = usbtmc_disconnect,
1449 .suspend = usbtmc_suspend,
1450 .resume = usbtmc_resume,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001451};
1452
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001453module_usb_driver(usbtmc_driver);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001454
1455MODULE_LICENSE("GPL");