blob: ec978408a2ee324bfaae685aaac40c36514cbd81 [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
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -050090 u8 rigol_quirk;
91
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070092 /* attributes from the USB TMC spec for this device */
93 u8 TermChar;
94 bool TermCharEnabled;
95 bool auto_abort;
96
Oliver Neukum86286882009-07-02 11:36:30 +020097 bool zombie; /* fd of disconnected device */
98
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070099 struct usbtmc_dev_capabilities capabilities;
100 struct kref kref;
101 struct mutex io_mutex; /* only one i/o function running at a time */
102};
103#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
104
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500105struct usbtmc_ID_rigol_quirk {
106 __u16 idVendor;
107 __u16 idProduct;
108};
109
110static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
111 { 0x1ab1, 0x0588 },
112 { 0, 0 }
113};
114
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700115/* Forward declarations */
116static struct usb_driver usbtmc_driver;
117
118static void usbtmc_delete(struct kref *kref)
119{
120 struct usbtmc_device_data *data = to_usbtmc_data(kref);
121
122 usb_put_dev(data->usb_dev);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700123}
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) {
Andy Shevchenkof4d844c2013-07-15 11:59:42 +0300133 pr_err("can not find device for minor %d", iminor(inode));
134 return -ENODEV;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700135 }
136
137 data = usb_get_intfdata(intf);
138 kref_get(&data->kref);
139
140 /* Store pointer in file structure's private data field */
141 filp->private_data = data;
142
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700143 return retval;
144}
145
146static int usbtmc_release(struct inode *inode, struct file *file)
147{
148 struct usbtmc_device_data *data = file->private_data;
149
150 kref_put(&data->kref, usbtmc_delete);
151 return 0;
152}
153
154static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
155{
Chris Malleyb361a6e2008-10-25 22:07:32 +0100156 u8 *buffer;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700157 struct device *dev;
158 int rv;
159 int n;
160 int actual;
161 struct usb_host_interface *current_setting;
162 int max_size;
163
164 dev = &data->intf->dev;
165 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
166 if (!buffer)
167 return -ENOMEM;
168
169 rv = usb_control_msg(data->usb_dev,
170 usb_rcvctrlpipe(data->usb_dev, 0),
171 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
172 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
173 data->bTag_last_read, data->bulk_in,
174 buffer, 2, USBTMC_TIMEOUT);
175
176 if (rv < 0) {
177 dev_err(dev, "usb_control_msg returned %d\n", rv);
178 goto exit;
179 }
180
181 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
182
183 if (buffer[0] == USBTMC_STATUS_FAILED) {
184 rv = 0;
185 goto exit;
186 }
187
188 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
189 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
190 buffer[0]);
191 rv = -EPERM;
192 goto exit;
193 }
194
195 max_size = 0;
196 current_setting = data->intf->cur_altsetting;
197 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
198 if (current_setting->endpoint[n].desc.bEndpointAddress ==
199 data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700200 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700201
202 if (max_size == 0) {
203 dev_err(dev, "Couldn't get wMaxPacketSize\n");
204 rv = -EPERM;
205 goto exit;
206 }
207
208 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
209
210 n = 0;
211
212 do {
213 dev_dbg(dev, "Reading from bulk in EP\n");
214
215 rv = usb_bulk_msg(data->usb_dev,
216 usb_rcvbulkpipe(data->usb_dev,
217 data->bulk_in),
218 buffer, USBTMC_SIZE_IOBUFFER,
219 &actual, USBTMC_TIMEOUT);
220
221 n++;
222
223 if (rv < 0) {
224 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
225 goto exit;
226 }
227 } while ((actual == max_size) &&
228 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
229
230 if (actual == max_size) {
231 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
232 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
233 rv = -EPERM;
234 goto exit;
235 }
236
237 n = 0;
238
239usbtmc_abort_bulk_in_status:
240 rv = usb_control_msg(data->usb_dev,
241 usb_rcvctrlpipe(data->usb_dev, 0),
242 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
243 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
244 0, data->bulk_in, buffer, 0x08,
245 USBTMC_TIMEOUT);
246
247 if (rv < 0) {
248 dev_err(dev, "usb_control_msg returned %d\n", rv);
249 goto exit;
250 }
251
252 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
253
254 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
255 rv = 0;
256 goto exit;
257 }
258
259 if (buffer[0] != USBTMC_STATUS_PENDING) {
260 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
261 rv = -EPERM;
262 goto exit;
263 }
264
265 if (buffer[1] == 1)
266 do {
267 dev_dbg(dev, "Reading from bulk in EP\n");
268
269 rv = usb_bulk_msg(data->usb_dev,
270 usb_rcvbulkpipe(data->usb_dev,
271 data->bulk_in),
272 buffer, USBTMC_SIZE_IOBUFFER,
273 &actual, USBTMC_TIMEOUT);
274
275 n++;
276
277 if (rv < 0) {
278 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
279 goto exit;
280 }
Maxim Nikulin4f1a7a32011-07-09 23:44:44 +0700281 } while ((actual == max_size) &&
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700282 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
283
284 if (actual == max_size) {
285 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
286 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
287 rv = -EPERM;
288 goto exit;
289 }
290
291 goto usbtmc_abort_bulk_in_status;
292
293exit:
294 kfree(buffer);
295 return rv;
296
297}
298
299static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
300{
301 struct device *dev;
302 u8 *buffer;
303 int rv;
304 int n;
305
306 dev = &data->intf->dev;
307
308 buffer = kmalloc(8, GFP_KERNEL);
309 if (!buffer)
310 return -ENOMEM;
311
312 rv = usb_control_msg(data->usb_dev,
313 usb_rcvctrlpipe(data->usb_dev, 0),
314 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
315 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
316 data->bTag_last_write, data->bulk_out,
317 buffer, 2, USBTMC_TIMEOUT);
318
319 if (rv < 0) {
320 dev_err(dev, "usb_control_msg returned %d\n", rv);
321 goto exit;
322 }
323
324 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
325
326 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
327 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
328 buffer[0]);
329 rv = -EPERM;
330 goto exit;
331 }
332
333 n = 0;
334
335usbtmc_abort_bulk_out_check_status:
336 rv = usb_control_msg(data->usb_dev,
337 usb_rcvctrlpipe(data->usb_dev, 0),
338 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
339 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
340 0, data->bulk_out, buffer, 0x08,
341 USBTMC_TIMEOUT);
342 n++;
343 if (rv < 0) {
344 dev_err(dev, "usb_control_msg returned %d\n", rv);
345 goto exit;
346 }
347
348 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
349
350 if (buffer[0] == USBTMC_STATUS_SUCCESS)
351 goto usbtmc_abort_bulk_out_clear_halt;
352
353 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
354 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
355 goto usbtmc_abort_bulk_out_check_status;
356
357 rv = -EPERM;
358 goto exit;
359
360usbtmc_abort_bulk_out_clear_halt:
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800361 rv = usb_clear_halt(data->usb_dev,
362 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700363
364 if (rv < 0) {
365 dev_err(dev, "usb_control_msg returned %d\n", rv);
366 goto exit;
367 }
368 rv = 0;
369
370exit:
371 kfree(buffer);
372 return rv;
373}
374
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500375/*
376 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
377 * @transfer_size: number of bytes to request from the device.
378 *
379 * See the USBTMC specification, Table 4.
380 *
381 * Also updates bTag_last_write.
382 */
383static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
384{
385 int retval;
Oliver Neukumd846b762014-05-19 13:54:57 +0200386 u8 *buffer;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500387 int actual;
388
Oliver Neukumd846b762014-05-19 13:54:57 +0200389 buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
390 if (!buffer)
391 return -ENOMEM;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500392 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
393 * Refer to class specs for details
394 */
395 buffer[0] = 2;
396 buffer[1] = data->bTag;
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300397 buffer[2] = ~data->bTag;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500398 buffer[3] = 0; /* Reserved */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300399 buffer[4] = transfer_size >> 0;
400 buffer[5] = transfer_size >> 8;
401 buffer[6] = transfer_size >> 16;
402 buffer[7] = transfer_size >> 24;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500403 buffer[8] = data->TermCharEnabled * 2;
404 /* Use term character? */
405 buffer[9] = data->TermChar;
406 buffer[10] = 0; /* Reserved */
407 buffer[11] = 0; /* Reserved */
408
409 /* Send bulk URB */
410 retval = usb_bulk_msg(data->usb_dev,
411 usb_sndbulkpipe(data->usb_dev,
412 data->bulk_out),
413 buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
414
415 /* Store bTag (in case we need to abort) */
416 data->bTag_last_write = data->bTag;
417
418 /* Increment bTag -- and increment again if zero */
419 data->bTag++;
420 if (!data->bTag)
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300421 data->bTag++;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500422
Oliver Neukumd846b762014-05-19 13:54:57 +0200423 kfree(buffer);
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500424 if (retval < 0) {
425 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
426 return retval;
427 }
428
429 return 0;
430}
431
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700432static ssize_t usbtmc_read(struct file *filp, char __user *buf,
433 size_t count, loff_t *f_pos)
434{
435 struct usbtmc_device_data *data;
436 struct device *dev;
Guus Sliepen665d7662009-07-22 17:39:42 +0200437 u32 n_characters;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700438 u8 *buffer;
439 int actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200440 size_t done;
441 size_t remaining;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700442 int retval;
Guus Sliepen665d7662009-07-22 17:39:42 +0200443 size_t this_part;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700444
445 /* Get pointer to private data structure */
446 data = filp->private_data;
447 dev = &data->intf->dev;
448
449 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
450 if (!buffer)
451 return -ENOMEM;
452
453 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200454 if (data->zombie) {
455 retval = -ENODEV;
456 goto exit;
457 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700458
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500459 if (data->rigol_quirk) {
460 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700461
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500462 retval = send_request_dev_dep_msg_in(data, count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700463
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700464 if (retval < 0) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700465 if (data->auto_abort)
466 usbtmc_ioctl_abort_bulk_out(data);
467 goto exit;
468 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500469 }
470
471 /* Loop until we have fetched everything we requested */
472 remaining = count;
473 this_part = remaining;
474 done = 0;
475
476 while (remaining > 0) {
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300477 if (!data->rigol_quirk) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500478 dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
479
480 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
481 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
482 else
483 this_part = remaining;
484
485 retval = send_request_dev_dep_msg_in(data, this_part);
486 if (retval < 0) {
487 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
488 if (data->auto_abort)
489 usbtmc_ioctl_abort_bulk_out(data);
490 goto exit;
491 }
492 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700493
494 /* Send bulk URB */
495 retval = usb_bulk_msg(data->usb_dev,
496 usb_rcvbulkpipe(data->usb_dev,
497 data->bulk_in),
498 buffer, USBTMC_SIZE_IOBUFFER, &actual,
499 USBTMC_TIMEOUT);
500
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500501 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
502
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700503 /* Store bTag (in case we need to abort) */
504 data->bTag_last_read = data->bTag;
505
506 if (retval < 0) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500507 dev_dbg(dev, "Unable to read data, error %d\n", retval);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700508 if (data->auto_abort)
509 usbtmc_ioctl_abort_bulk_in(data);
510 goto exit;
511 }
512
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500513 /* Parse header in first packet */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300514 if ((done == 0) || !data->rigol_quirk) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500515 /* Sanity checks for the header */
516 if (actual < USBTMC_HEADER_SIZE) {
517 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
518 if (data->auto_abort)
519 usbtmc_ioctl_abort_bulk_in(data);
520 goto exit;
521 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700522
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500523 if (buffer[0] != 2) {
524 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
525 if (data->auto_abort)
526 usbtmc_ioctl_abort_bulk_in(data);
527 goto exit;
528 }
529
530 if (buffer[1] != data->bTag_last_write) {
531 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
532 if (data->auto_abort)
533 usbtmc_ioctl_abort_bulk_in(data);
534 goto exit;
535 }
536
537 /* How many characters did the instrument send? */
538 n_characters = buffer[4] +
539 (buffer[5] << 8) +
540 (buffer[6] << 16) +
541 (buffer[7] << 24);
542
543 if (n_characters > this_part) {
544 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
545 if (data->auto_abort)
546 usbtmc_ioctl_abort_bulk_in(data);
547 goto exit;
548 }
549
550 /* Remove the USBTMC header */
551 actual -= USBTMC_HEADER_SIZE;
552
553 /* Check if the message is smaller than requested */
554 if (data->rigol_quirk) {
555 if (remaining > n_characters)
556 remaining = n_characters;
557 /* Remove padding if it exists */
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300558 if (actual > remaining)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500559 actual = remaining;
560 }
561 else {
562 if (this_part > n_characters)
563 this_part = n_characters;
564 /* Remove padding if it exists */
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300565 if (actual > this_part)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500566 actual = this_part;
567 }
568
569 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
570
571 remaining -= actual;
572
573 /* Terminate if end-of-message bit received from device */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300574 if ((buffer[8] & 0x01) && (actual >= n_characters))
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500575 remaining = 0;
576
577 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
578
579
580 /* Copy buffer to user space */
581 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
582 /* There must have been an addressing problem */
583 retval = -EFAULT;
584 goto exit;
585 }
586 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200587 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500588 else {
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300589 if (actual > remaining)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500590 actual = remaining;
Guus Sliepen665d7662009-07-22 17:39:42 +0200591
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500592 remaining -= actual;
593
594 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
595
596 /* Copy buffer to user space */
597 if (copy_to_user(buf + done, buffer, actual)) {
598 /* There must have been an addressing problem */
599 retval = -EFAULT;
600 goto exit;
601 }
602 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200603 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700604 }
605
606 /* Update file position value */
607 *f_pos = *f_pos + done;
608 retval = done;
609
610exit:
611 mutex_unlock(&data->io_mutex);
612 kfree(buffer);
613 return retval;
614}
615
616static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
617 size_t count, loff_t *f_pos)
618{
619 struct usbtmc_device_data *data;
620 u8 *buffer;
621 int retval;
622 int actual;
623 unsigned long int n_bytes;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700624 int remaining;
625 int done;
626 int this_part;
627
628 data = filp->private_data;
629
630 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
631 if (!buffer)
632 return -ENOMEM;
633
634 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200635 if (data->zombie) {
636 retval = -ENODEV;
637 goto exit;
638 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700639
640 remaining = count;
641 done = 0;
642
643 while (remaining > 0) {
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500644 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
645 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700646 buffer[8] = 0;
647 } else {
648 this_part = remaining;
649 buffer[8] = 1;
650 }
651
652 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
653 buffer[0] = 1;
654 buffer[1] = data->bTag;
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300655 buffer[2] = ~data->bTag;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700656 buffer[3] = 0; /* Reserved */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300657 buffer[4] = this_part >> 0;
658 buffer[5] = this_part >> 8;
659 buffer[6] = this_part >> 16;
660 buffer[7] = this_part >> 24;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700661 /* buffer[8] is set above... */
662 buffer[9] = 0; /* Reserved */
663 buffer[10] = 0; /* Reserved */
664 buffer[11] = 0; /* Reserved */
665
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500666 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700667 retval = -EFAULT;
668 goto exit;
669 }
670
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500671 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
672 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700673
Andre Hermsec412b92009-11-19 18:14:49 +0100674 do {
675 retval = usb_bulk_msg(data->usb_dev,
676 usb_sndbulkpipe(data->usb_dev,
677 data->bulk_out),
678 buffer, n_bytes,
679 &actual, USBTMC_TIMEOUT);
680 if (retval != 0)
681 break;
682 n_bytes -= actual;
683 } while (n_bytes);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700684
685 data->bTag_last_write = data->bTag;
686 data->bTag++;
687
688 if (!data->bTag)
689 data->bTag++;
690
691 if (retval < 0) {
692 dev_err(&data->intf->dev,
693 "Unable to send data, error %d\n", retval);
694 if (data->auto_abort)
695 usbtmc_ioctl_abort_bulk_out(data);
696 goto exit;
697 }
698
699 remaining -= this_part;
700 done += this_part;
701 }
702
703 retval = count;
704exit:
705 mutex_unlock(&data->io_mutex);
706 kfree(buffer);
707 return retval;
708}
709
710static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
711{
712 struct usb_host_interface *current_setting;
713 struct usb_endpoint_descriptor *desc;
714 struct device *dev;
715 u8 *buffer;
716 int rv;
717 int n;
Rickard Strandqvistab53eb92014-06-01 15:43:19 +0200718 int actual = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700719 int max_size;
720
721 dev = &data->intf->dev;
722
723 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
724
725 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
726 if (!buffer)
727 return -ENOMEM;
728
729 rv = usb_control_msg(data->usb_dev,
730 usb_rcvctrlpipe(data->usb_dev, 0),
731 USBTMC_REQUEST_INITIATE_CLEAR,
732 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
733 0, 0, buffer, 1, USBTMC_TIMEOUT);
734 if (rv < 0) {
735 dev_err(dev, "usb_control_msg returned %d\n", rv);
736 goto exit;
737 }
738
739 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
740
741 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
742 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
743 rv = -EPERM;
744 goto exit;
745 }
746
747 max_size = 0;
748 current_setting = data->intf->cur_altsetting;
749 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
750 desc = &current_setting->endpoint[n].desc;
751 if (desc->bEndpointAddress == data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700752 max_size = usb_endpoint_maxp(desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700753 }
754
755 if (max_size == 0) {
756 dev_err(dev, "Couldn't get wMaxPacketSize\n");
757 rv = -EPERM;
758 goto exit;
759 }
760
761 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
762
763 n = 0;
764
765usbtmc_clear_check_status:
766
767 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
768
769 rv = usb_control_msg(data->usb_dev,
770 usb_rcvctrlpipe(data->usb_dev, 0),
771 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
772 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
773 0, 0, buffer, 2, USBTMC_TIMEOUT);
774 if (rv < 0) {
775 dev_err(dev, "usb_control_msg returned %d\n", rv);
776 goto exit;
777 }
778
779 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
780
781 if (buffer[0] == USBTMC_STATUS_SUCCESS)
782 goto usbtmc_clear_bulk_out_halt;
783
784 if (buffer[0] != USBTMC_STATUS_PENDING) {
785 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
786 rv = -EPERM;
787 goto exit;
788 }
789
790 if (buffer[1] == 1)
791 do {
792 dev_dbg(dev, "Reading from bulk in EP\n");
793
794 rv = usb_bulk_msg(data->usb_dev,
795 usb_rcvbulkpipe(data->usb_dev,
796 data->bulk_in),
797 buffer, USBTMC_SIZE_IOBUFFER,
798 &actual, USBTMC_TIMEOUT);
799 n++;
800
801 if (rv < 0) {
802 dev_err(dev, "usb_control_msg returned %d\n",
803 rv);
804 goto exit;
805 }
806 } while ((actual == max_size) &&
807 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
808
809 if (actual == max_size) {
810 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
811 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
812 rv = -EPERM;
813 goto exit;
814 }
815
816 goto usbtmc_clear_check_status;
817
818usbtmc_clear_bulk_out_halt:
819
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800820 rv = usb_clear_halt(data->usb_dev,
821 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700822 if (rv < 0) {
823 dev_err(dev, "usb_control_msg returned %d\n", rv);
824 goto exit;
825 }
826 rv = 0;
827
828exit:
829 kfree(buffer);
830 return rv;
831}
832
833static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
834{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700835 int rv;
836
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800837 rv = usb_clear_halt(data->usb_dev,
838 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700839
840 if (rv < 0) {
841 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
842 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800843 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700844 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800845 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700846}
847
848static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
849{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700850 int rv;
851
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800852 rv = usb_clear_halt(data->usb_dev,
853 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700854
855 if (rv < 0) {
856 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
857 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800858 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700859 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800860 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700861}
862
863static int get_capabilities(struct usbtmc_device_data *data)
864{
865 struct device *dev = &data->usb_dev->dev;
866 char *buffer;
Oliver Neukumca157c42009-07-02 16:41:39 +0200867 int rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700868
869 buffer = kmalloc(0x18, GFP_KERNEL);
870 if (!buffer)
871 return -ENOMEM;
872
873 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
874 USBTMC_REQUEST_GET_CAPABILITIES,
875 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
876 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
877 if (rv < 0) {
878 dev_err(dev, "usb_control_msg returned %d\n", rv);
Oliver Neukumca157c42009-07-02 16:41:39 +0200879 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700880 }
881
882 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700883 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
884 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Oliver Neukumca157c42009-07-02 16:41:39 +0200885 rv = -EPERM;
886 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700887 }
Gergely Imrehd0a38362009-09-07 10:47:01 +0800888 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
889 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
890 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
891 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700892
893 data->capabilities.interface_capabilities = buffer[4];
894 data->capabilities.device_capabilities = buffer[5];
895 data->capabilities.usb488_interface_capabilities = buffer[14];
896 data->capabilities.usb488_device_capabilities = buffer[15];
Gergely Imrehd0a38362009-09-07 10:47:01 +0800897 rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700898
Oliver Neukumca157c42009-07-02 16:41:39 +0200899err_out:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700900 kfree(buffer);
Oliver Neukumca157c42009-07-02 16:41:39 +0200901 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700902}
903
904#define capability_attribute(name) \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -0700905static ssize_t name##_show(struct device *dev, \
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700906 struct device_attribute *attr, char *buf) \
907{ \
908 struct usb_interface *intf = to_usb_interface(dev); \
909 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
910 \
911 return sprintf(buf, "%d\n", data->capabilities.name); \
912} \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -0700913static DEVICE_ATTR_RO(name)
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700914
915capability_attribute(interface_capabilities);
916capability_attribute(device_capabilities);
917capability_attribute(usb488_interface_capabilities);
918capability_attribute(usb488_device_capabilities);
919
920static struct attribute *capability_attrs[] = {
921 &dev_attr_interface_capabilities.attr,
922 &dev_attr_device_capabilities.attr,
923 &dev_attr_usb488_interface_capabilities.attr,
924 &dev_attr_usb488_device_capabilities.attr,
925 NULL,
926};
927
928static struct attribute_group capability_attr_grp = {
929 .attrs = capability_attrs,
930};
931
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -0700932static ssize_t TermChar_show(struct device *dev,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700933 struct device_attribute *attr, char *buf)
934{
935 struct usb_interface *intf = to_usb_interface(dev);
936 struct usbtmc_device_data *data = usb_get_intfdata(intf);
937
938 return sprintf(buf, "%c\n", data->TermChar);
939}
940
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -0700941static ssize_t TermChar_store(struct device *dev,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700942 struct device_attribute *attr,
943 const char *buf, size_t count)
944{
945 struct usb_interface *intf = to_usb_interface(dev);
946 struct usbtmc_device_data *data = usb_get_intfdata(intf);
947
948 if (count < 1)
949 return -EINVAL;
950 data->TermChar = buf[0];
951 return count;
952}
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -0700953static DEVICE_ATTR_RW(TermChar);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700954
955#define data_attribute(name) \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -0700956static ssize_t name##_show(struct device *dev, \
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700957 struct device_attribute *attr, char *buf) \
958{ \
959 struct usb_interface *intf = to_usb_interface(dev); \
960 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
961 \
962 return sprintf(buf, "%d\n", data->name); \
963} \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -0700964static ssize_t name##_store(struct device *dev, \
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700965 struct device_attribute *attr, \
966 const char *buf, size_t count) \
967{ \
968 struct usb_interface *intf = to_usb_interface(dev); \
969 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
970 ssize_t result; \
971 unsigned val; \
972 \
973 result = sscanf(buf, "%u\n", &val); \
974 if (result != 1) \
975 result = -EINVAL; \
976 data->name = val; \
977 if (result < 0) \
978 return result; \
979 else \
980 return count; \
981} \
Greg Kroah-Hartman2a6eb8a2013-08-23 16:09:33 -0700982static DEVICE_ATTR_RW(name)
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700983
984data_attribute(TermCharEnabled);
985data_attribute(auto_abort);
986
987static struct attribute *data_attrs[] = {
988 &dev_attr_TermChar.attr,
989 &dev_attr_TermCharEnabled.attr,
990 &dev_attr_auto_abort.attr,
991 NULL,
992};
993
994static struct attribute_group data_attr_grp = {
995 .attrs = data_attrs,
996};
997
998static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
999{
1000 struct device *dev;
1001 u8 *buffer;
1002 int rv;
1003
1004 dev = &data->intf->dev;
1005
1006 buffer = kmalloc(2, GFP_KERNEL);
1007 if (!buffer)
1008 return -ENOMEM;
1009
1010 rv = usb_control_msg(data->usb_dev,
1011 usb_rcvctrlpipe(data->usb_dev, 0),
1012 USBTMC_REQUEST_INDICATOR_PULSE,
1013 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1014 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1015
1016 if (rv < 0) {
1017 dev_err(dev, "usb_control_msg returned %d\n", rv);
1018 goto exit;
1019 }
1020
1021 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1022
1023 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1024 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1025 rv = -EPERM;
1026 goto exit;
1027 }
1028 rv = 0;
1029
1030exit:
1031 kfree(buffer);
1032 return rv;
1033}
1034
1035static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1036{
1037 struct usbtmc_device_data *data;
1038 int retval = -EBADRQC;
1039
1040 data = file->private_data;
1041 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001042 if (data->zombie) {
1043 retval = -ENODEV;
1044 goto skip_io_on_zombie;
1045 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001046
1047 switch (cmd) {
1048 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1049 retval = usbtmc_ioctl_clear_out_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001050 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001051
1052 case USBTMC_IOCTL_CLEAR_IN_HALT:
1053 retval = usbtmc_ioctl_clear_in_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001054 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001055
1056 case USBTMC_IOCTL_INDICATOR_PULSE:
1057 retval = usbtmc_ioctl_indicator_pulse(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001058 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001059
1060 case USBTMC_IOCTL_CLEAR:
1061 retval = usbtmc_ioctl_clear(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001062 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001063
1064 case USBTMC_IOCTL_ABORT_BULK_OUT:
1065 retval = usbtmc_ioctl_abort_bulk_out(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001066 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001067
1068 case USBTMC_IOCTL_ABORT_BULK_IN:
1069 retval = usbtmc_ioctl_abort_bulk_in(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001070 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001071 }
1072
Oliver Neukum86286882009-07-02 11:36:30 +02001073skip_io_on_zombie:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001074 mutex_unlock(&data->io_mutex);
1075 return retval;
1076}
1077
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001078static const struct file_operations fops = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001079 .owner = THIS_MODULE,
1080 .read = usbtmc_read,
1081 .write = usbtmc_write,
1082 .open = usbtmc_open,
1083 .release = usbtmc_release,
1084 .unlocked_ioctl = usbtmc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001085 .llseek = default_llseek,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001086};
1087
1088static struct usb_class_driver usbtmc_class = {
1089 .name = "usbtmc%d",
1090 .fops = &fops,
1091 .minor_base = USBTMC_MINOR_BASE,
1092};
1093
1094
1095static int usbtmc_probe(struct usb_interface *intf,
1096 const struct usb_device_id *id)
1097{
1098 struct usbtmc_device_data *data;
1099 struct usb_host_interface *iface_desc;
1100 struct usb_endpoint_descriptor *endpoint;
1101 int n;
1102 int retcode;
1103
1104 dev_dbg(&intf->dev, "%s called\n", __func__);
1105
Andy Shevchenkoe6c7efd2013-07-15 11:59:41 +03001106 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001107 if (!data) {
1108 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
1109 return -ENOMEM;
1110 }
1111
1112 data->intf = intf;
1113 data->id = id;
1114 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1115 usb_set_intfdata(intf, data);
1116 kref_init(&data->kref);
1117 mutex_init(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001118 data->zombie = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001119
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001120 /* Determine if it is a Rigol or not */
1121 data->rigol_quirk = 0;
1122 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 +02001123 le16_to_cpu(data->usb_dev->descriptor.idVendor),
1124 le16_to_cpu(data->usb_dev->descriptor.idProduct));
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001125 for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
Johan Hovoldc7861382013-08-11 16:49:21 +02001126 if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) &&
1127 (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) {
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001128 dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1129 data->rigol_quirk = 1;
1130 break;
1131 }
1132 }
1133
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001134 /* Initialize USBTMC bTag and other fields */
1135 data->bTag = 1;
1136 data->TermCharEnabled = 0;
1137 data->TermChar = '\n';
1138
1139 /* USBTMC devices have only one setting, so use that */
1140 iface_desc = data->intf->cur_altsetting;
1141
1142 /* Find bulk in endpoint */
1143 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1144 endpoint = &iface_desc->endpoint[n].desc;
1145
1146 if (usb_endpoint_is_bulk_in(endpoint)) {
1147 data->bulk_in = endpoint->bEndpointAddress;
1148 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1149 data->bulk_in);
1150 break;
1151 }
1152 }
1153
1154 /* Find bulk out endpoint */
1155 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1156 endpoint = &iface_desc->endpoint[n].desc;
1157
1158 if (usb_endpoint_is_bulk_out(endpoint)) {
1159 data->bulk_out = endpoint->bEndpointAddress;
1160 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1161 data->bulk_out);
1162 break;
1163 }
1164 }
1165
1166 retcode = get_capabilities(data);
1167 if (retcode)
1168 dev_err(&intf->dev, "can't read capabilities\n");
1169 else
1170 retcode = sysfs_create_group(&intf->dev.kobj,
1171 &capability_attr_grp);
1172
1173 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1174
1175 retcode = usb_register_dev(intf, &usbtmc_class);
1176 if (retcode) {
1177 dev_err(&intf->dev, "Not able to get a minor"
1178 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1179 retcode);
1180 goto error_register;
1181 }
1182 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1183
1184 return 0;
1185
1186error_register:
1187 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1188 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1189 kref_put(&data->kref, usbtmc_delete);
1190 return retcode;
1191}
1192
1193static void usbtmc_disconnect(struct usb_interface *intf)
1194{
1195 struct usbtmc_device_data *data;
1196
1197 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1198
1199 data = usb_get_intfdata(intf);
1200 usb_deregister_dev(intf, &usbtmc_class);
1201 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1202 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
Oliver Neukum86286882009-07-02 11:36:30 +02001203 mutex_lock(&data->io_mutex);
1204 data->zombie = 1;
1205 mutex_unlock(&data->io_mutex);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001206 kref_put(&data->kref, usbtmc_delete);
1207}
1208
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001209static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
Oliver Neukuma4708102009-07-02 11:44:33 +02001210{
1211 /* this driver does not have pending URBs */
1212 return 0;
1213}
1214
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001215static int usbtmc_resume(struct usb_interface *intf)
Oliver Neukuma4708102009-07-02 11:44:33 +02001216{
1217 return 0;
1218}
1219
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001220static struct usb_driver usbtmc_driver = {
1221 .name = "usbtmc",
1222 .id_table = usbtmc_devices,
1223 .probe = usbtmc_probe,
Oliver Neukuma4708102009-07-02 11:44:33 +02001224 .disconnect = usbtmc_disconnect,
1225 .suspend = usbtmc_suspend,
1226 .resume = usbtmc_resume,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001227};
1228
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001229module_usb_driver(usbtmc_driver);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001230
1231MODULE_LICENSE("GPL");