blob: d683f90a505b0831cbce3872ff67cc1c6b50cb47 [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/init.h>
25#include <linux/module.h>
Ilpo Järvinen857cc4d2008-10-30 13:56:47 +020026#include <linux/kernel.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070027#include <linux/fs.h>
28#include <linux/uaccess.h>
29#include <linux/kref.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070031#include <linux/mutex.h>
32#include <linux/usb.h>
33#include <linux/usb/tmc.h>
34
35
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -050036#define RIGOL 1
37#define USBTMC_HEADER_SIZE 12
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070038#define USBTMC_MINOR_BASE 176
39
40/*
41 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
42 * large as wMaxPacketSize (which is usually 512 bytes).
43 */
44#define USBTMC_SIZE_IOBUFFER 2048
45
46/* Default USB timeout (in milliseconds) */
Gergely Imreh35f76e892009-09-15 16:03:31 +080047#define USBTMC_TIMEOUT 5000
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070048
49/*
50 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
51 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
52 * packet is never read.
53 */
54#define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
55
Németh Márton6ef48522010-01-10 15:33:45 +010056static const struct usb_device_id usbtmc_devices[] = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070057 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
Greg Kroah-Hartman228dd052009-03-11 13:51:42 -070058 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070059 { 0, } /* terminating entry */
60};
Greg Kroah-Hartman5413aa42008-12-03 16:33:09 -080061MODULE_DEVICE_TABLE(usb, usbtmc_devices);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070062
63/*
64 * This structure is the capabilities for the device
Gergely Imrehd0a38362009-09-07 10:47:01 +080065 * See section 4.2.1.8 of the USBTMC specification,
66 * and section 4.2.2 of the USBTMC usb488 subclass
67 * specification for details.
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070068 */
69struct usbtmc_dev_capabilities {
70 __u8 interface_capabilities;
71 __u8 device_capabilities;
72 __u8 usb488_interface_capabilities;
73 __u8 usb488_device_capabilities;
74};
75
76/* This structure holds private data for each USBTMC device. One copy is
77 * allocated for each USBTMC device in the driver's probe function.
78 */
79struct usbtmc_device_data {
80 const struct usb_device_id *id;
81 struct usb_device *usb_dev;
82 struct usb_interface *intf;
83
84 unsigned int bulk_in;
85 unsigned int bulk_out;
86
87 u8 bTag;
88 u8 bTag_last_write; /* needed for abort */
89 u8 bTag_last_read; /* needed for abort */
90
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -050091 u8 rigol_quirk;
92
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070093 /* attributes from the USB TMC spec for this device */
94 u8 TermChar;
95 bool TermCharEnabled;
96 bool auto_abort;
97
Oliver Neukum86286882009-07-02 11:36:30 +020098 bool zombie; /* fd of disconnected device */
99
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700100 struct usbtmc_dev_capabilities capabilities;
101 struct kref kref;
102 struct mutex io_mutex; /* only one i/o function running at a time */
103};
104#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
105
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500106struct usbtmc_ID_rigol_quirk {
107 __u16 idVendor;
108 __u16 idProduct;
109};
110
111static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
112 { 0x1ab1, 0x0588 },
113 { 0, 0 }
114};
115
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700116/* Forward declarations */
117static struct usb_driver usbtmc_driver;
118
119static void usbtmc_delete(struct kref *kref)
120{
121 struct usbtmc_device_data *data = to_usbtmc_data(kref);
122
123 usb_put_dev(data->usb_dev);
124 kfree(data);
125}
126
127static int usbtmc_open(struct inode *inode, struct file *filp)
128{
129 struct usb_interface *intf;
130 struct usbtmc_device_data *data;
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700131 int retval = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700132
133 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
134 if (!intf) {
Andy Shevchenkof4d844c2013-07-15 11:59:42 +0300135 pr_err("can not find device for minor %d", iminor(inode));
136 return -ENODEV;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700137 }
138
139 data = usb_get_intfdata(intf);
140 kref_get(&data->kref);
141
142 /* Store pointer in file structure's private data field */
143 filp->private_data = data;
144
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700145 return retval;
146}
147
148static int usbtmc_release(struct inode *inode, struct file *file)
149{
150 struct usbtmc_device_data *data = file->private_data;
151
152 kref_put(&data->kref, usbtmc_delete);
153 return 0;
154}
155
156static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
157{
Chris Malleyb361a6e2008-10-25 22:07:32 +0100158 u8 *buffer;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700159 struct device *dev;
160 int rv;
161 int n;
162 int actual;
163 struct usb_host_interface *current_setting;
164 int max_size;
165
166 dev = &data->intf->dev;
167 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
168 if (!buffer)
169 return -ENOMEM;
170
171 rv = usb_control_msg(data->usb_dev,
172 usb_rcvctrlpipe(data->usb_dev, 0),
173 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
174 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
175 data->bTag_last_read, data->bulk_in,
176 buffer, 2, USBTMC_TIMEOUT);
177
178 if (rv < 0) {
179 dev_err(dev, "usb_control_msg returned %d\n", rv);
180 goto exit;
181 }
182
183 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
184
185 if (buffer[0] == USBTMC_STATUS_FAILED) {
186 rv = 0;
187 goto exit;
188 }
189
190 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
191 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
192 buffer[0]);
193 rv = -EPERM;
194 goto exit;
195 }
196
197 max_size = 0;
198 current_setting = data->intf->cur_altsetting;
199 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
200 if (current_setting->endpoint[n].desc.bEndpointAddress ==
201 data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700202 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700203
204 if (max_size == 0) {
205 dev_err(dev, "Couldn't get wMaxPacketSize\n");
206 rv = -EPERM;
207 goto exit;
208 }
209
210 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
211
212 n = 0;
213
214 do {
215 dev_dbg(dev, "Reading from bulk in EP\n");
216
217 rv = usb_bulk_msg(data->usb_dev,
218 usb_rcvbulkpipe(data->usb_dev,
219 data->bulk_in),
220 buffer, USBTMC_SIZE_IOBUFFER,
221 &actual, USBTMC_TIMEOUT);
222
223 n++;
224
225 if (rv < 0) {
226 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
227 goto exit;
228 }
229 } while ((actual == max_size) &&
230 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
231
232 if (actual == max_size) {
233 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
234 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
235 rv = -EPERM;
236 goto exit;
237 }
238
239 n = 0;
240
241usbtmc_abort_bulk_in_status:
242 rv = usb_control_msg(data->usb_dev,
243 usb_rcvctrlpipe(data->usb_dev, 0),
244 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
245 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
246 0, data->bulk_in, buffer, 0x08,
247 USBTMC_TIMEOUT);
248
249 if (rv < 0) {
250 dev_err(dev, "usb_control_msg returned %d\n", rv);
251 goto exit;
252 }
253
254 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
255
256 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
257 rv = 0;
258 goto exit;
259 }
260
261 if (buffer[0] != USBTMC_STATUS_PENDING) {
262 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
263 rv = -EPERM;
264 goto exit;
265 }
266
267 if (buffer[1] == 1)
268 do {
269 dev_dbg(dev, "Reading from bulk in EP\n");
270
271 rv = usb_bulk_msg(data->usb_dev,
272 usb_rcvbulkpipe(data->usb_dev,
273 data->bulk_in),
274 buffer, USBTMC_SIZE_IOBUFFER,
275 &actual, USBTMC_TIMEOUT);
276
277 n++;
278
279 if (rv < 0) {
280 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
281 goto exit;
282 }
Maxim Nikulin4f1a7a32011-07-09 23:44:44 +0700283 } while ((actual == max_size) &&
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700284 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
285
286 if (actual == max_size) {
287 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
288 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
289 rv = -EPERM;
290 goto exit;
291 }
292
293 goto usbtmc_abort_bulk_in_status;
294
295exit:
296 kfree(buffer);
297 return rv;
298
299}
300
301static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
302{
303 struct device *dev;
304 u8 *buffer;
305 int rv;
306 int n;
307
308 dev = &data->intf->dev;
309
310 buffer = kmalloc(8, GFP_KERNEL);
311 if (!buffer)
312 return -ENOMEM;
313
314 rv = usb_control_msg(data->usb_dev,
315 usb_rcvctrlpipe(data->usb_dev, 0),
316 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
317 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
318 data->bTag_last_write, data->bulk_out,
319 buffer, 2, USBTMC_TIMEOUT);
320
321 if (rv < 0) {
322 dev_err(dev, "usb_control_msg returned %d\n", rv);
323 goto exit;
324 }
325
326 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
327
328 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
329 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
330 buffer[0]);
331 rv = -EPERM;
332 goto exit;
333 }
334
335 n = 0;
336
337usbtmc_abort_bulk_out_check_status:
338 rv = usb_control_msg(data->usb_dev,
339 usb_rcvctrlpipe(data->usb_dev, 0),
340 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
341 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
342 0, data->bulk_out, buffer, 0x08,
343 USBTMC_TIMEOUT);
344 n++;
345 if (rv < 0) {
346 dev_err(dev, "usb_control_msg returned %d\n", rv);
347 goto exit;
348 }
349
350 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
351
352 if (buffer[0] == USBTMC_STATUS_SUCCESS)
353 goto usbtmc_abort_bulk_out_clear_halt;
354
355 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
356 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
357 goto usbtmc_abort_bulk_out_check_status;
358
359 rv = -EPERM;
360 goto exit;
361
362usbtmc_abort_bulk_out_clear_halt:
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800363 rv = usb_clear_halt(data->usb_dev,
364 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700365
366 if (rv < 0) {
367 dev_err(dev, "usb_control_msg returned %d\n", rv);
368 goto exit;
369 }
370 rv = 0;
371
372exit:
373 kfree(buffer);
374 return rv;
375}
376
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500377/*
378 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
379 * @transfer_size: number of bytes to request from the device.
380 *
381 * See the USBTMC specification, Table 4.
382 *
383 * Also updates bTag_last_write.
384 */
385static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
386{
387 int retval;
388 u8 buffer[USBTMC_HEADER_SIZE];
389 int actual;
390
391 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
392 * Refer to class specs for details
393 */
394 buffer[0] = 2;
395 buffer[1] = data->bTag;
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300396 buffer[2] = ~data->bTag;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500397 buffer[3] = 0; /* Reserved */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300398 buffer[4] = transfer_size >> 0;
399 buffer[5] = transfer_size >> 8;
400 buffer[6] = transfer_size >> 16;
401 buffer[7] = transfer_size >> 24;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500402 buffer[8] = data->TermCharEnabled * 2;
403 /* Use term character? */
404 buffer[9] = data->TermChar;
405 buffer[10] = 0; /* Reserved */
406 buffer[11] = 0; /* Reserved */
407
408 /* Send bulk URB */
409 retval = usb_bulk_msg(data->usb_dev,
410 usb_sndbulkpipe(data->usb_dev,
411 data->bulk_out),
412 buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
413
414 /* Store bTag (in case we need to abort) */
415 data->bTag_last_write = data->bTag;
416
417 /* Increment bTag -- and increment again if zero */
418 data->bTag++;
419 if (!data->bTag)
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300420 data->bTag++;
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500421
422 if (retval < 0) {
423 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
424 return retval;
425 }
426
427 return 0;
428}
429
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700430static ssize_t usbtmc_read(struct file *filp, char __user *buf,
431 size_t count, loff_t *f_pos)
432{
433 struct usbtmc_device_data *data;
434 struct device *dev;
Guus Sliepen665d7662009-07-22 17:39:42 +0200435 u32 n_characters;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700436 u8 *buffer;
437 int actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200438 size_t done;
439 size_t remaining;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700440 int retval;
Guus Sliepen665d7662009-07-22 17:39:42 +0200441 size_t this_part;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700442
443 /* Get pointer to private data structure */
444 data = filp->private_data;
445 dev = &data->intf->dev;
446
447 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
448 if (!buffer)
449 return -ENOMEM;
450
451 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200452 if (data->zombie) {
453 retval = -ENODEV;
454 goto exit;
455 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700456
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500457 if (data->rigol_quirk) {
458 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700459
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500460 retval = send_request_dev_dep_msg_in(data, count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700461
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700462 if (retval < 0) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700463 if (data->auto_abort)
464 usbtmc_ioctl_abort_bulk_out(data);
465 goto exit;
466 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500467 }
468
469 /* Loop until we have fetched everything we requested */
470 remaining = count;
471 this_part = remaining;
472 done = 0;
473
474 while (remaining > 0) {
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300475 if (!data->rigol_quirk) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500476 dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
477
478 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
479 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
480 else
481 this_part = remaining;
482
483 retval = send_request_dev_dep_msg_in(data, this_part);
484 if (retval < 0) {
485 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
486 if (data->auto_abort)
487 usbtmc_ioctl_abort_bulk_out(data);
488 goto exit;
489 }
490 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700491
492 /* Send bulk URB */
493 retval = usb_bulk_msg(data->usb_dev,
494 usb_rcvbulkpipe(data->usb_dev,
495 data->bulk_in),
496 buffer, USBTMC_SIZE_IOBUFFER, &actual,
497 USBTMC_TIMEOUT);
498
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500499 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
500
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700501 /* Store bTag (in case we need to abort) */
502 data->bTag_last_read = data->bTag;
503
504 if (retval < 0) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500505 dev_dbg(dev, "Unable to read data, error %d\n", retval);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700506 if (data->auto_abort)
507 usbtmc_ioctl_abort_bulk_in(data);
508 goto exit;
509 }
510
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500511 /* Parse header in first packet */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300512 if ((done == 0) || !data->rigol_quirk) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500513 /* Sanity checks for the header */
514 if (actual < USBTMC_HEADER_SIZE) {
515 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
516 if (data->auto_abort)
517 usbtmc_ioctl_abort_bulk_in(data);
518 goto exit;
519 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700520
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500521 if (buffer[0] != 2) {
522 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
523 if (data->auto_abort)
524 usbtmc_ioctl_abort_bulk_in(data);
525 goto exit;
526 }
527
528 if (buffer[1] != data->bTag_last_write) {
529 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
530 if (data->auto_abort)
531 usbtmc_ioctl_abort_bulk_in(data);
532 goto exit;
533 }
534
535 /* How many characters did the instrument send? */
536 n_characters = buffer[4] +
537 (buffer[5] << 8) +
538 (buffer[6] << 16) +
539 (buffer[7] << 24);
540
541 if (n_characters > this_part) {
542 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
543 if (data->auto_abort)
544 usbtmc_ioctl_abort_bulk_in(data);
545 goto exit;
546 }
547
548 /* Remove the USBTMC header */
549 actual -= USBTMC_HEADER_SIZE;
550
551 /* Check if the message is smaller than requested */
552 if (data->rigol_quirk) {
553 if (remaining > n_characters)
554 remaining = n_characters;
555 /* Remove padding if it exists */
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300556 if (actual > remaining)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500557 actual = remaining;
558 }
559 else {
560 if (this_part > n_characters)
561 this_part = n_characters;
562 /* Remove padding if it exists */
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300563 if (actual > this_part)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500564 actual = this_part;
565 }
566
567 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
568
569 remaining -= actual;
570
571 /* Terminate if end-of-message bit received from device */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300572 if ((buffer[8] & 0x01) && (actual >= n_characters))
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500573 remaining = 0;
574
575 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
576
577
578 /* Copy buffer to user space */
579 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
580 /* There must have been an addressing problem */
581 retval = -EFAULT;
582 goto exit;
583 }
584 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200585 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500586 else {
Andy Shevchenko92f78dd2013-07-15 11:59:40 +0300587 if (actual > remaining)
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500588 actual = remaining;
Guus Sliepen665d7662009-07-22 17:39:42 +0200589
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500590 remaining -= actual;
591
592 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
593
594 /* Copy buffer to user space */
595 if (copy_to_user(buf + done, buffer, actual)) {
596 /* There must have been an addressing problem */
597 retval = -EFAULT;
598 goto exit;
599 }
600 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200601 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700602 }
603
604 /* Update file position value */
605 *f_pos = *f_pos + done;
606 retval = done;
607
608exit:
609 mutex_unlock(&data->io_mutex);
610 kfree(buffer);
611 return retval;
612}
613
614static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
615 size_t count, loff_t *f_pos)
616{
617 struct usbtmc_device_data *data;
618 u8 *buffer;
619 int retval;
620 int actual;
621 unsigned long int n_bytes;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700622 int remaining;
623 int done;
624 int this_part;
625
626 data = filp->private_data;
627
628 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
629 if (!buffer)
630 return -ENOMEM;
631
632 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200633 if (data->zombie) {
634 retval = -ENODEV;
635 goto exit;
636 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700637
638 remaining = count;
639 done = 0;
640
641 while (remaining > 0) {
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500642 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
643 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700644 buffer[8] = 0;
645 } else {
646 this_part = remaining;
647 buffer[8] = 1;
648 }
649
650 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
651 buffer[0] = 1;
652 buffer[1] = data->bTag;
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300653 buffer[2] = ~data->bTag;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700654 buffer[3] = 0; /* Reserved */
Andy Shevchenkobbf49762013-07-15 11:59:43 +0300655 buffer[4] = this_part >> 0;
656 buffer[5] = this_part >> 8;
657 buffer[6] = this_part >> 16;
658 buffer[7] = this_part >> 24;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700659 /* buffer[8] is set above... */
660 buffer[9] = 0; /* Reserved */
661 buffer[10] = 0; /* Reserved */
662 buffer[11] = 0; /* Reserved */
663
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500664 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700665 retval = -EFAULT;
666 goto exit;
667 }
668
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500669 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
670 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700671
Andre Hermsec412b92009-11-19 18:14:49 +0100672 do {
673 retval = usb_bulk_msg(data->usb_dev,
674 usb_sndbulkpipe(data->usb_dev,
675 data->bulk_out),
676 buffer, n_bytes,
677 &actual, USBTMC_TIMEOUT);
678 if (retval != 0)
679 break;
680 n_bytes -= actual;
681 } while (n_bytes);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700682
683 data->bTag_last_write = data->bTag;
684 data->bTag++;
685
686 if (!data->bTag)
687 data->bTag++;
688
689 if (retval < 0) {
690 dev_err(&data->intf->dev,
691 "Unable to send data, error %d\n", retval);
692 if (data->auto_abort)
693 usbtmc_ioctl_abort_bulk_out(data);
694 goto exit;
695 }
696
697 remaining -= this_part;
698 done += this_part;
699 }
700
701 retval = count;
702exit:
703 mutex_unlock(&data->io_mutex);
704 kfree(buffer);
705 return retval;
706}
707
708static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
709{
710 struct usb_host_interface *current_setting;
711 struct usb_endpoint_descriptor *desc;
712 struct device *dev;
713 u8 *buffer;
714 int rv;
715 int n;
716 int actual;
717 int max_size;
718
719 dev = &data->intf->dev;
720
721 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
722
723 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
724 if (!buffer)
725 return -ENOMEM;
726
727 rv = usb_control_msg(data->usb_dev,
728 usb_rcvctrlpipe(data->usb_dev, 0),
729 USBTMC_REQUEST_INITIATE_CLEAR,
730 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
731 0, 0, buffer, 1, USBTMC_TIMEOUT);
732 if (rv < 0) {
733 dev_err(dev, "usb_control_msg returned %d\n", rv);
734 goto exit;
735 }
736
737 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
738
739 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
740 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
741 rv = -EPERM;
742 goto exit;
743 }
744
745 max_size = 0;
746 current_setting = data->intf->cur_altsetting;
747 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
748 desc = &current_setting->endpoint[n].desc;
749 if (desc->bEndpointAddress == data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700750 max_size = usb_endpoint_maxp(desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700751 }
752
753 if (max_size == 0) {
754 dev_err(dev, "Couldn't get wMaxPacketSize\n");
755 rv = -EPERM;
756 goto exit;
757 }
758
759 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
760
761 n = 0;
762
763usbtmc_clear_check_status:
764
765 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
766
767 rv = usb_control_msg(data->usb_dev,
768 usb_rcvctrlpipe(data->usb_dev, 0),
769 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
770 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
771 0, 0, buffer, 2, USBTMC_TIMEOUT);
772 if (rv < 0) {
773 dev_err(dev, "usb_control_msg returned %d\n", rv);
774 goto exit;
775 }
776
777 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
778
779 if (buffer[0] == USBTMC_STATUS_SUCCESS)
780 goto usbtmc_clear_bulk_out_halt;
781
782 if (buffer[0] != USBTMC_STATUS_PENDING) {
783 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
784 rv = -EPERM;
785 goto exit;
786 }
787
788 if (buffer[1] == 1)
789 do {
790 dev_dbg(dev, "Reading from bulk in EP\n");
791
792 rv = usb_bulk_msg(data->usb_dev,
793 usb_rcvbulkpipe(data->usb_dev,
794 data->bulk_in),
795 buffer, USBTMC_SIZE_IOBUFFER,
796 &actual, USBTMC_TIMEOUT);
797 n++;
798
799 if (rv < 0) {
800 dev_err(dev, "usb_control_msg returned %d\n",
801 rv);
802 goto exit;
803 }
804 } while ((actual == max_size) &&
805 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
806
807 if (actual == max_size) {
808 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
809 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
810 rv = -EPERM;
811 goto exit;
812 }
813
814 goto usbtmc_clear_check_status;
815
816usbtmc_clear_bulk_out_halt:
817
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800818 rv = usb_clear_halt(data->usb_dev,
819 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700820 if (rv < 0) {
821 dev_err(dev, "usb_control_msg returned %d\n", rv);
822 goto exit;
823 }
824 rv = 0;
825
826exit:
827 kfree(buffer);
828 return rv;
829}
830
831static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
832{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700833 int rv;
834
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800835 rv = usb_clear_halt(data->usb_dev,
836 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700837
838 if (rv < 0) {
839 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
840 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800841 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700842 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800843 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700844}
845
846static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
847{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700848 int rv;
849
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800850 rv = usb_clear_halt(data->usb_dev,
851 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700852
853 if (rv < 0) {
854 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
855 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800856 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700857 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800858 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700859}
860
861static int get_capabilities(struct usbtmc_device_data *data)
862{
863 struct device *dev = &data->usb_dev->dev;
864 char *buffer;
Oliver Neukumca157c42009-07-02 16:41:39 +0200865 int rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700866
867 buffer = kmalloc(0x18, GFP_KERNEL);
868 if (!buffer)
869 return -ENOMEM;
870
871 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
872 USBTMC_REQUEST_GET_CAPABILITIES,
873 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
874 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
875 if (rv < 0) {
876 dev_err(dev, "usb_control_msg returned %d\n", rv);
Oliver Neukumca157c42009-07-02 16:41:39 +0200877 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700878 }
879
880 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700881 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
882 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Oliver Neukumca157c42009-07-02 16:41:39 +0200883 rv = -EPERM;
884 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700885 }
Gergely Imrehd0a38362009-09-07 10:47:01 +0800886 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
887 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
888 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
889 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700890
891 data->capabilities.interface_capabilities = buffer[4];
892 data->capabilities.device_capabilities = buffer[5];
893 data->capabilities.usb488_interface_capabilities = buffer[14];
894 data->capabilities.usb488_device_capabilities = buffer[15];
Gergely Imrehd0a38362009-09-07 10:47:01 +0800895 rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700896
Oliver Neukumca157c42009-07-02 16:41:39 +0200897err_out:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700898 kfree(buffer);
Oliver Neukumca157c42009-07-02 16:41:39 +0200899 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700900}
901
902#define capability_attribute(name) \
903static ssize_t show_##name(struct device *dev, \
904 struct device_attribute *attr, char *buf) \
905{ \
906 struct usb_interface *intf = to_usb_interface(dev); \
907 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
908 \
909 return sprintf(buf, "%d\n", data->capabilities.name); \
910} \
911static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
912
913capability_attribute(interface_capabilities);
914capability_attribute(device_capabilities);
915capability_attribute(usb488_interface_capabilities);
916capability_attribute(usb488_device_capabilities);
917
918static struct attribute *capability_attrs[] = {
919 &dev_attr_interface_capabilities.attr,
920 &dev_attr_device_capabilities.attr,
921 &dev_attr_usb488_interface_capabilities.attr,
922 &dev_attr_usb488_device_capabilities.attr,
923 NULL,
924};
925
926static struct attribute_group capability_attr_grp = {
927 .attrs = capability_attrs,
928};
929
930static ssize_t show_TermChar(struct device *dev,
931 struct device_attribute *attr, char *buf)
932{
933 struct usb_interface *intf = to_usb_interface(dev);
934 struct usbtmc_device_data *data = usb_get_intfdata(intf);
935
936 return sprintf(buf, "%c\n", data->TermChar);
937}
938
939static ssize_t store_TermChar(struct device *dev,
940 struct device_attribute *attr,
941 const char *buf, size_t count)
942{
943 struct usb_interface *intf = to_usb_interface(dev);
944 struct usbtmc_device_data *data = usb_get_intfdata(intf);
945
946 if (count < 1)
947 return -EINVAL;
948 data->TermChar = buf[0];
949 return count;
950}
951static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
952
953#define data_attribute(name) \
954static ssize_t show_##name(struct device *dev, \
955 struct device_attribute *attr, char *buf) \
956{ \
957 struct usb_interface *intf = to_usb_interface(dev); \
958 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
959 \
960 return sprintf(buf, "%d\n", data->name); \
961} \
962static ssize_t store_##name(struct device *dev, \
963 struct device_attribute *attr, \
964 const char *buf, size_t count) \
965{ \
966 struct usb_interface *intf = to_usb_interface(dev); \
967 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
968 ssize_t result; \
969 unsigned val; \
970 \
971 result = sscanf(buf, "%u\n", &val); \
972 if (result != 1) \
973 result = -EINVAL; \
974 data->name = val; \
975 if (result < 0) \
976 return result; \
977 else \
978 return count; \
979} \
980static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
981
982data_attribute(TermCharEnabled);
983data_attribute(auto_abort);
984
985static struct attribute *data_attrs[] = {
986 &dev_attr_TermChar.attr,
987 &dev_attr_TermCharEnabled.attr,
988 &dev_attr_auto_abort.attr,
989 NULL,
990};
991
992static struct attribute_group data_attr_grp = {
993 .attrs = data_attrs,
994};
995
996static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
997{
998 struct device *dev;
999 u8 *buffer;
1000 int rv;
1001
1002 dev = &data->intf->dev;
1003
1004 buffer = kmalloc(2, GFP_KERNEL);
1005 if (!buffer)
1006 return -ENOMEM;
1007
1008 rv = usb_control_msg(data->usb_dev,
1009 usb_rcvctrlpipe(data->usb_dev, 0),
1010 USBTMC_REQUEST_INDICATOR_PULSE,
1011 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1012 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1013
1014 if (rv < 0) {
1015 dev_err(dev, "usb_control_msg returned %d\n", rv);
1016 goto exit;
1017 }
1018
1019 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1020
1021 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1022 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1023 rv = -EPERM;
1024 goto exit;
1025 }
1026 rv = 0;
1027
1028exit:
1029 kfree(buffer);
1030 return rv;
1031}
1032
1033static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1034{
1035 struct usbtmc_device_data *data;
1036 int retval = -EBADRQC;
1037
1038 data = file->private_data;
1039 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001040 if (data->zombie) {
1041 retval = -ENODEV;
1042 goto skip_io_on_zombie;
1043 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001044
1045 switch (cmd) {
1046 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1047 retval = usbtmc_ioctl_clear_out_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001048 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001049
1050 case USBTMC_IOCTL_CLEAR_IN_HALT:
1051 retval = usbtmc_ioctl_clear_in_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001052 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001053
1054 case USBTMC_IOCTL_INDICATOR_PULSE:
1055 retval = usbtmc_ioctl_indicator_pulse(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001056 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001057
1058 case USBTMC_IOCTL_CLEAR:
1059 retval = usbtmc_ioctl_clear(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001060 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001061
1062 case USBTMC_IOCTL_ABORT_BULK_OUT:
1063 retval = usbtmc_ioctl_abort_bulk_out(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001064 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001065
1066 case USBTMC_IOCTL_ABORT_BULK_IN:
1067 retval = usbtmc_ioctl_abort_bulk_in(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001068 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001069 }
1070
Oliver Neukum86286882009-07-02 11:36:30 +02001071skip_io_on_zombie:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001072 mutex_unlock(&data->io_mutex);
1073 return retval;
1074}
1075
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001076static const struct file_operations fops = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001077 .owner = THIS_MODULE,
1078 .read = usbtmc_read,
1079 .write = usbtmc_write,
1080 .open = usbtmc_open,
1081 .release = usbtmc_release,
1082 .unlocked_ioctl = usbtmc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001083 .llseek = default_llseek,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001084};
1085
1086static struct usb_class_driver usbtmc_class = {
1087 .name = "usbtmc%d",
1088 .fops = &fops,
1089 .minor_base = USBTMC_MINOR_BASE,
1090};
1091
1092
1093static int usbtmc_probe(struct usb_interface *intf,
1094 const struct usb_device_id *id)
1095{
1096 struct usbtmc_device_data *data;
1097 struct usb_host_interface *iface_desc;
1098 struct usb_endpoint_descriptor *endpoint;
1099 int n;
1100 int retcode;
1101
1102 dev_dbg(&intf->dev, "%s called\n", __func__);
1103
1104 data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
1105 if (!data) {
1106 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
1107 return -ENOMEM;
1108 }
1109
1110 data->intf = intf;
1111 data->id = id;
1112 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1113 usb_set_intfdata(intf, data);
1114 kref_init(&data->kref);
1115 mutex_init(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001116 data->zombie = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001117
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001118 /* Determine if it is a Rigol or not */
1119 data->rigol_quirk = 0;
1120 dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1121 data->usb_dev->descriptor.idVendor,
1122 data->usb_dev->descriptor.idProduct);
1123 for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
1124 if ((usbtmc_id_quirk[n].idVendor == data->usb_dev->descriptor.idVendor) &&
1125 (usbtmc_id_quirk[n].idProduct == data->usb_dev->descriptor.idProduct)) {
1126 dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1127 data->rigol_quirk = 1;
1128 break;
1129 }
1130 }
1131
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001132 /* Initialize USBTMC bTag and other fields */
1133 data->bTag = 1;
1134 data->TermCharEnabled = 0;
1135 data->TermChar = '\n';
1136
1137 /* USBTMC devices have only one setting, so use that */
1138 iface_desc = data->intf->cur_altsetting;
1139
1140 /* Find bulk in endpoint */
1141 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1142 endpoint = &iface_desc->endpoint[n].desc;
1143
1144 if (usb_endpoint_is_bulk_in(endpoint)) {
1145 data->bulk_in = endpoint->bEndpointAddress;
1146 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1147 data->bulk_in);
1148 break;
1149 }
1150 }
1151
1152 /* Find bulk out endpoint */
1153 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1154 endpoint = &iface_desc->endpoint[n].desc;
1155
1156 if (usb_endpoint_is_bulk_out(endpoint)) {
1157 data->bulk_out = endpoint->bEndpointAddress;
1158 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1159 data->bulk_out);
1160 break;
1161 }
1162 }
1163
1164 retcode = get_capabilities(data);
1165 if (retcode)
1166 dev_err(&intf->dev, "can't read capabilities\n");
1167 else
1168 retcode = sysfs_create_group(&intf->dev.kobj,
1169 &capability_attr_grp);
1170
1171 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1172
1173 retcode = usb_register_dev(intf, &usbtmc_class);
1174 if (retcode) {
1175 dev_err(&intf->dev, "Not able to get a minor"
1176 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1177 retcode);
1178 goto error_register;
1179 }
1180 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1181
1182 return 0;
1183
1184error_register:
1185 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1186 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1187 kref_put(&data->kref, usbtmc_delete);
1188 return retcode;
1189}
1190
1191static void usbtmc_disconnect(struct usb_interface *intf)
1192{
1193 struct usbtmc_device_data *data;
1194
1195 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1196
1197 data = usb_get_intfdata(intf);
1198 usb_deregister_dev(intf, &usbtmc_class);
1199 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1200 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
Oliver Neukum86286882009-07-02 11:36:30 +02001201 mutex_lock(&data->io_mutex);
1202 data->zombie = 1;
1203 mutex_unlock(&data->io_mutex);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001204 kref_put(&data->kref, usbtmc_delete);
1205}
1206
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001207static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
Oliver Neukuma4708102009-07-02 11:44:33 +02001208{
1209 /* this driver does not have pending URBs */
1210 return 0;
1211}
1212
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001213static int usbtmc_resume(struct usb_interface *intf)
Oliver Neukuma4708102009-07-02 11:44:33 +02001214{
1215 return 0;
1216}
1217
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001218static struct usb_driver usbtmc_driver = {
1219 .name = "usbtmc",
1220 .id_table = usbtmc_devices,
1221 .probe = usbtmc_probe,
Oliver Neukuma4708102009-07-02 11:44:33 +02001222 .disconnect = usbtmc_disconnect,
1223 .suspend = usbtmc_suspend,
1224 .resume = usbtmc_resume,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001225};
1226
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001227module_usb_driver(usbtmc_driver);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001228
1229MODULE_LICENSE("GPL");