blob: 609dbc2f7151d52a08cf2f7b62d229637491ce55 [file] [log] [blame]
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002 * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07003 *
4 * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
5 * Copyright (C) 2008 Novell, Inc.
6 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * The GNU General Public License is available at
19 * http://www.gnu.org/copyleft/gpl.html.
20 */
21
22#include <linux/init.h>
23#include <linux/module.h>
Ilpo Järvinen857cc4d2008-10-30 13:56:47 +020024#include <linux/kernel.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070025#include <linux/fs.h>
26#include <linux/uaccess.h>
27#include <linux/kref.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070029#include <linux/mutex.h>
30#include <linux/usb.h>
31#include <linux/usb/tmc.h>
32
33
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -050034#define RIGOL 1
35#define USBTMC_HEADER_SIZE 12
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070036#define USBTMC_MINOR_BASE 176
37
38/*
39 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
40 * large as wMaxPacketSize (which is usually 512 bytes).
41 */
42#define USBTMC_SIZE_IOBUFFER 2048
43
44/* Default USB timeout (in milliseconds) */
Gergely Imreh35f76e892009-09-15 16:03:31 +080045#define USBTMC_TIMEOUT 5000
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070046
47/*
48 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
49 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
50 * packet is never read.
51 */
52#define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
53
Németh Márton6ef48522010-01-10 15:33:45 +010054static const struct usb_device_id usbtmc_devices[] = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070055 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
Greg Kroah-Hartman228dd052009-03-11 13:51:42 -070056 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070057 { 0, } /* terminating entry */
58};
Greg Kroah-Hartman5413aa42008-12-03 16:33:09 -080059MODULE_DEVICE_TABLE(usb, usbtmc_devices);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070060
61/*
62 * This structure is the capabilities for the device
Gergely Imrehd0a38362009-09-07 10:47:01 +080063 * See section 4.2.1.8 of the USBTMC specification,
64 * and section 4.2.2 of the USBTMC usb488 subclass
65 * specification for details.
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070066 */
67struct usbtmc_dev_capabilities {
68 __u8 interface_capabilities;
69 __u8 device_capabilities;
70 __u8 usb488_interface_capabilities;
71 __u8 usb488_device_capabilities;
72};
73
74/* This structure holds private data for each USBTMC device. One copy is
75 * allocated for each USBTMC device in the driver's probe function.
76 */
77struct usbtmc_device_data {
78 const struct usb_device_id *id;
79 struct usb_device *usb_dev;
80 struct usb_interface *intf;
81
82 unsigned int bulk_in;
83 unsigned int bulk_out;
84
85 u8 bTag;
86 u8 bTag_last_write; /* needed for abort */
87 u8 bTag_last_read; /* needed for abort */
88
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -050089 u8 rigol_quirk;
90
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070091 /* attributes from the USB TMC spec for this device */
92 u8 TermChar;
93 bool TermCharEnabled;
94 bool auto_abort;
95
Oliver Neukum86286882009-07-02 11:36:30 +020096 bool zombie; /* fd of disconnected device */
97
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070098 struct usbtmc_dev_capabilities capabilities;
99 struct kref kref;
100 struct mutex io_mutex; /* only one i/o function running at a time */
101};
102#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
103
Alexandre Peixoto Ferreira8402db52013-04-30 00:51:50 -0500104struct usbtmc_ID_rigol_quirk {
105 __u16 idVendor;
106 __u16 idProduct;
107};
108
109static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
110 { 0x1ab1, 0x0588 },
111 { 0, 0 }
112};
113
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700114/* Forward declarations */
115static struct usb_driver usbtmc_driver;
116
117static void usbtmc_delete(struct kref *kref)
118{
119 struct usbtmc_device_data *data = to_usbtmc_data(kref);
120
121 usb_put_dev(data->usb_dev);
122 kfree(data);
123}
124
125static int usbtmc_open(struct inode *inode, struct file *filp)
126{
127 struct usb_interface *intf;
128 struct usbtmc_device_data *data;
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700129 int retval = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700130
131 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
132 if (!intf) {
133 printk(KERN_ERR KBUILD_MODNAME
134 ": can not find device for minor %d", iminor(inode));
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700135 retval = -ENODEV;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700136 goto exit;
137 }
138
139 data = usb_get_intfdata(intf);
140 kref_get(&data->kref);
141
142 /* Store pointer in file structure's private data field */
143 filp->private_data = data;
144
145exit:
146 return retval;
147}
148
149static int usbtmc_release(struct inode *inode, struct file *file)
150{
151 struct usbtmc_device_data *data = file->private_data;
152
153 kref_put(&data->kref, usbtmc_delete);
154 return 0;
155}
156
157static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
158{
Chris Malleyb361a6e2008-10-25 22:07:32 +0100159 u8 *buffer;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700160 struct device *dev;
161 int rv;
162 int n;
163 int actual;
164 struct usb_host_interface *current_setting;
165 int max_size;
166
167 dev = &data->intf->dev;
168 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
169 if (!buffer)
170 return -ENOMEM;
171
172 rv = usb_control_msg(data->usb_dev,
173 usb_rcvctrlpipe(data->usb_dev, 0),
174 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
175 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
176 data->bTag_last_read, data->bulk_in,
177 buffer, 2, USBTMC_TIMEOUT);
178
179 if (rv < 0) {
180 dev_err(dev, "usb_control_msg returned %d\n", rv);
181 goto exit;
182 }
183
184 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
185
186 if (buffer[0] == USBTMC_STATUS_FAILED) {
187 rv = 0;
188 goto exit;
189 }
190
191 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
192 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
193 buffer[0]);
194 rv = -EPERM;
195 goto exit;
196 }
197
198 max_size = 0;
199 current_setting = data->intf->cur_altsetting;
200 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
201 if (current_setting->endpoint[n].desc.bEndpointAddress ==
202 data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700203 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700204
205 if (max_size == 0) {
206 dev_err(dev, "Couldn't get wMaxPacketSize\n");
207 rv = -EPERM;
208 goto exit;
209 }
210
211 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
212
213 n = 0;
214
215 do {
216 dev_dbg(dev, "Reading from bulk in EP\n");
217
218 rv = usb_bulk_msg(data->usb_dev,
219 usb_rcvbulkpipe(data->usb_dev,
220 data->bulk_in),
221 buffer, USBTMC_SIZE_IOBUFFER,
222 &actual, USBTMC_TIMEOUT);
223
224 n++;
225
226 if (rv < 0) {
227 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
228 goto exit;
229 }
230 } while ((actual == max_size) &&
231 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
232
233 if (actual == max_size) {
234 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
235 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
236 rv = -EPERM;
237 goto exit;
238 }
239
240 n = 0;
241
242usbtmc_abort_bulk_in_status:
243 rv = usb_control_msg(data->usb_dev,
244 usb_rcvctrlpipe(data->usb_dev, 0),
245 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
246 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
247 0, data->bulk_in, buffer, 0x08,
248 USBTMC_TIMEOUT);
249
250 if (rv < 0) {
251 dev_err(dev, "usb_control_msg returned %d\n", rv);
252 goto exit;
253 }
254
255 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
256
257 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
258 rv = 0;
259 goto exit;
260 }
261
262 if (buffer[0] != USBTMC_STATUS_PENDING) {
263 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
264 rv = -EPERM;
265 goto exit;
266 }
267
268 if (buffer[1] == 1)
269 do {
270 dev_dbg(dev, "Reading from bulk in EP\n");
271
272 rv = usb_bulk_msg(data->usb_dev,
273 usb_rcvbulkpipe(data->usb_dev,
274 data->bulk_in),
275 buffer, USBTMC_SIZE_IOBUFFER,
276 &actual, USBTMC_TIMEOUT);
277
278 n++;
279
280 if (rv < 0) {
281 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
282 goto exit;
283 }
Maxim Nikulin4f1a7a32011-07-09 23:44:44 +0700284 } while ((actual == max_size) &&
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700285 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
286
287 if (actual == max_size) {
288 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
289 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
290 rv = -EPERM;
291 goto exit;
292 }
293
294 goto usbtmc_abort_bulk_in_status;
295
296exit:
297 kfree(buffer);
298 return rv;
299
300}
301
302static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
303{
304 struct device *dev;
305 u8 *buffer;
306 int rv;
307 int n;
308
309 dev = &data->intf->dev;
310
311 buffer = kmalloc(8, GFP_KERNEL);
312 if (!buffer)
313 return -ENOMEM;
314
315 rv = usb_control_msg(data->usb_dev,
316 usb_rcvctrlpipe(data->usb_dev, 0),
317 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
318 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
319 data->bTag_last_write, data->bulk_out,
320 buffer, 2, USBTMC_TIMEOUT);
321
322 if (rv < 0) {
323 dev_err(dev, "usb_control_msg returned %d\n", rv);
324 goto exit;
325 }
326
327 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
328
329 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
330 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
331 buffer[0]);
332 rv = -EPERM;
333 goto exit;
334 }
335
336 n = 0;
337
338usbtmc_abort_bulk_out_check_status:
339 rv = usb_control_msg(data->usb_dev,
340 usb_rcvctrlpipe(data->usb_dev, 0),
341 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
342 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
343 0, data->bulk_out, buffer, 0x08,
344 USBTMC_TIMEOUT);
345 n++;
346 if (rv < 0) {
347 dev_err(dev, "usb_control_msg returned %d\n", rv);
348 goto exit;
349 }
350
351 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
352
353 if (buffer[0] == USBTMC_STATUS_SUCCESS)
354 goto usbtmc_abort_bulk_out_clear_halt;
355
356 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
357 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
358 goto usbtmc_abort_bulk_out_check_status;
359
360 rv = -EPERM;
361 goto exit;
362
363usbtmc_abort_bulk_out_clear_halt:
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800364 rv = usb_clear_halt(data->usb_dev,
365 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700366
367 if (rv < 0) {
368 dev_err(dev, "usb_control_msg returned %d\n", rv);
369 goto exit;
370 }
371 rv = 0;
372
373exit:
374 kfree(buffer);
375 return rv;
376}
377
Alexandre Peixoto Ferreira88d9b2b2013-04-30 00:51:51 -0500378/*
379 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
380 * @transfer_size: number of bytes to request from the device.
381 *
382 * See the USBTMC specification, Table 4.
383 *
384 * Also updates bTag_last_write.
385 */
386static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
387{
388 int retval;
389 u8 buffer[USBTMC_HEADER_SIZE];
390 int actual;
391
392 /* 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;
397 buffer[2] = ~(data->bTag);
398 buffer[3] = 0; /* Reserved */
399 buffer[4] = (transfer_size) & 255;
400 buffer[5] = ((transfer_size) >> 8) & 255;
401 buffer[6] = ((transfer_size) >> 16) & 255;
402 buffer[7] = ((transfer_size) >> 24) & 255;
403 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)
421 (data->bTag)++;
422
423 if (retval < 0) {
424 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
425 return retval;
426 }
427
428 return 0;
429}
430
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700431static ssize_t usbtmc_read(struct file *filp, char __user *buf,
432 size_t count, loff_t *f_pos)
433{
434 struct usbtmc_device_data *data;
435 struct device *dev;
Guus Sliepen665d7662009-07-22 17:39:42 +0200436 u32 n_characters;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700437 u8 *buffer;
438 int actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200439 size_t done;
440 size_t remaining;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700441 int retval;
Guus Sliepen665d7662009-07-22 17:39:42 +0200442 size_t this_part;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700443
444 /* Get pointer to private data structure */
445 data = filp->private_data;
446 dev = &data->intf->dev;
447
448 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
449 if (!buffer)
450 return -ENOMEM;
451
452 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200453 if (data->zombie) {
454 retval = -ENODEV;
455 goto exit;
456 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700457
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500458 if (data->rigol_quirk) {
459 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700460
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500461 retval = send_request_dev_dep_msg_in(data, count);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700462
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700463 if (retval < 0) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700464 if (data->auto_abort)
465 usbtmc_ioctl_abort_bulk_out(data);
466 goto exit;
467 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500468 }
469
470 /* Loop until we have fetched everything we requested */
471 remaining = count;
472 this_part = remaining;
473 done = 0;
474
475 while (remaining > 0) {
476 if (!(data->rigol_quirk)) {
477 dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
478
479 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
480 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
481 else
482 this_part = remaining;
483
484 retval = send_request_dev_dep_msg_in(data, this_part);
485 if (retval < 0) {
486 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
487 if (data->auto_abort)
488 usbtmc_ioctl_abort_bulk_out(data);
489 goto exit;
490 }
491 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700492
493 /* Send bulk URB */
494 retval = usb_bulk_msg(data->usb_dev,
495 usb_rcvbulkpipe(data->usb_dev,
496 data->bulk_in),
497 buffer, USBTMC_SIZE_IOBUFFER, &actual,
498 USBTMC_TIMEOUT);
499
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500500 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
501
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700502 /* Store bTag (in case we need to abort) */
503 data->bTag_last_read = data->bTag;
504
505 if (retval < 0) {
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500506 dev_dbg(dev, "Unable to read data, error %d\n", retval);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700507 if (data->auto_abort)
508 usbtmc_ioctl_abort_bulk_in(data);
509 goto exit;
510 }
511
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500512 /* Parse header in first packet */
513 if ((done == 0) || (!(data->rigol_quirk))) {
514 /* Sanity checks for the header */
515 if (actual < USBTMC_HEADER_SIZE) {
516 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
517 if (data->auto_abort)
518 usbtmc_ioctl_abort_bulk_in(data);
519 goto exit;
520 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700521
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500522 if (buffer[0] != 2) {
523 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
524 if (data->auto_abort)
525 usbtmc_ioctl_abort_bulk_in(data);
526 goto exit;
527 }
528
529 if (buffer[1] != data->bTag_last_write) {
530 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
531 if (data->auto_abort)
532 usbtmc_ioctl_abort_bulk_in(data);
533 goto exit;
534 }
535
536 /* How many characters did the instrument send? */
537 n_characters = buffer[4] +
538 (buffer[5] << 8) +
539 (buffer[6] << 16) +
540 (buffer[7] << 24);
541
542 if (n_characters > this_part) {
543 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
544 if (data->auto_abort)
545 usbtmc_ioctl_abort_bulk_in(data);
546 goto exit;
547 }
548
549 /* Remove the USBTMC header */
550 actual -= USBTMC_HEADER_SIZE;
551
552 /* Check if the message is smaller than requested */
553 if (data->rigol_quirk) {
554 if (remaining > n_characters)
555 remaining = n_characters;
556 /* Remove padding if it exists */
557 if (actual > remaining)
558 actual = remaining;
559 }
560 else {
561 if (this_part > n_characters)
562 this_part = n_characters;
563 /* Remove padding if it exists */
564 if (actual > this_part)
565 actual = this_part;
566 }
567
568 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
569
570 remaining -= actual;
571
572 /* Terminate if end-of-message bit received from device */
573 if ((buffer[8] & 0x01) && (actual >= n_characters))
574 remaining = 0;
575
576 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
577
578
579 /* Copy buffer to user space */
580 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
581 /* There must have been an addressing problem */
582 retval = -EFAULT;
583 goto exit;
584 }
585 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200586 }
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500587 else {
588 if (actual > remaining)
589 actual = remaining;
Guus Sliepen665d7662009-07-22 17:39:42 +0200590
Alexandre Peixoto Ferreirad2ddce32013-04-30 00:51:53 -0500591 remaining -= actual;
592
593 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
594
595 /* Copy buffer to user space */
596 if (copy_to_user(buf + done, buffer, actual)) {
597 /* There must have been an addressing problem */
598 retval = -EFAULT;
599 goto exit;
600 }
601 done += actual;
Guus Sliepen665d7662009-07-22 17:39:42 +0200602 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700603 }
604
605 /* Update file position value */
606 *f_pos = *f_pos + done;
607 retval = done;
608
609exit:
610 mutex_unlock(&data->io_mutex);
611 kfree(buffer);
612 return retval;
613}
614
615static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
616 size_t count, loff_t *f_pos)
617{
618 struct usbtmc_device_data *data;
619 u8 *buffer;
620 int retval;
621 int actual;
622 unsigned long int n_bytes;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700623 int remaining;
624 int done;
625 int this_part;
626
627 data = filp->private_data;
628
629 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
630 if (!buffer)
631 return -ENOMEM;
632
633 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +0200634 if (data->zombie) {
635 retval = -ENODEV;
636 goto exit;
637 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700638
639 remaining = count;
640 done = 0;
641
642 while (remaining > 0) {
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500643 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
644 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700645 buffer[8] = 0;
646 } else {
647 this_part = remaining;
648 buffer[8] = 1;
649 }
650
651 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
652 buffer[0] = 1;
653 buffer[1] = data->bTag;
654 buffer[2] = ~(data->bTag);
655 buffer[3] = 0; /* Reserved */
656 buffer[4] = this_part & 255;
657 buffer[5] = (this_part >> 8) & 255;
658 buffer[6] = (this_part >> 16) & 255;
659 buffer[7] = (this_part >> 24) & 255;
660 /* buffer[8] is set above... */
661 buffer[9] = 0; /* Reserved */
662 buffer[10] = 0; /* Reserved */
663 buffer[11] = 0; /* Reserved */
664
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500665 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700666 retval = -EFAULT;
667 goto exit;
668 }
669
Alexandre Peixoto Ferreira50c9ba32013-04-30 00:51:54 -0500670 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
671 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700672
Andre Hermsec412b92009-11-19 18:14:49 +0100673 do {
674 retval = usb_bulk_msg(data->usb_dev,
675 usb_sndbulkpipe(data->usb_dev,
676 data->bulk_out),
677 buffer, n_bytes,
678 &actual, USBTMC_TIMEOUT);
679 if (retval != 0)
680 break;
681 n_bytes -= actual;
682 } while (n_bytes);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700683
684 data->bTag_last_write = data->bTag;
685 data->bTag++;
686
687 if (!data->bTag)
688 data->bTag++;
689
690 if (retval < 0) {
691 dev_err(&data->intf->dev,
692 "Unable to send data, error %d\n", retval);
693 if (data->auto_abort)
694 usbtmc_ioctl_abort_bulk_out(data);
695 goto exit;
696 }
697
698 remaining -= this_part;
699 done += this_part;
700 }
701
702 retval = count;
703exit:
704 mutex_unlock(&data->io_mutex);
705 kfree(buffer);
706 return retval;
707}
708
709static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
710{
711 struct usb_host_interface *current_setting;
712 struct usb_endpoint_descriptor *desc;
713 struct device *dev;
714 u8 *buffer;
715 int rv;
716 int n;
717 int actual;
718 int max_size;
719
720 dev = &data->intf->dev;
721
722 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
723
724 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
725 if (!buffer)
726 return -ENOMEM;
727
728 rv = usb_control_msg(data->usb_dev,
729 usb_rcvctrlpipe(data->usb_dev, 0),
730 USBTMC_REQUEST_INITIATE_CLEAR,
731 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
732 0, 0, buffer, 1, USBTMC_TIMEOUT);
733 if (rv < 0) {
734 dev_err(dev, "usb_control_msg returned %d\n", rv);
735 goto exit;
736 }
737
738 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
739
740 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
741 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
742 rv = -EPERM;
743 goto exit;
744 }
745
746 max_size = 0;
747 current_setting = data->intf->cur_altsetting;
748 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
749 desc = &current_setting->endpoint[n].desc;
750 if (desc->bEndpointAddress == data->bulk_in)
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700751 max_size = usb_endpoint_maxp(desc);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700752 }
753
754 if (max_size == 0) {
755 dev_err(dev, "Couldn't get wMaxPacketSize\n");
756 rv = -EPERM;
757 goto exit;
758 }
759
760 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
761
762 n = 0;
763
764usbtmc_clear_check_status:
765
766 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
767
768 rv = usb_control_msg(data->usb_dev,
769 usb_rcvctrlpipe(data->usb_dev, 0),
770 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
771 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
772 0, 0, buffer, 2, USBTMC_TIMEOUT);
773 if (rv < 0) {
774 dev_err(dev, "usb_control_msg returned %d\n", rv);
775 goto exit;
776 }
777
778 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
779
780 if (buffer[0] == USBTMC_STATUS_SUCCESS)
781 goto usbtmc_clear_bulk_out_halt;
782
783 if (buffer[0] != USBTMC_STATUS_PENDING) {
784 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
785 rv = -EPERM;
786 goto exit;
787 }
788
789 if (buffer[1] == 1)
790 do {
791 dev_dbg(dev, "Reading from bulk in EP\n");
792
793 rv = usb_bulk_msg(data->usb_dev,
794 usb_rcvbulkpipe(data->usb_dev,
795 data->bulk_in),
796 buffer, USBTMC_SIZE_IOBUFFER,
797 &actual, USBTMC_TIMEOUT);
798 n++;
799
800 if (rv < 0) {
801 dev_err(dev, "usb_control_msg returned %d\n",
802 rv);
803 goto exit;
804 }
805 } while ((actual == max_size) &&
806 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
807
808 if (actual == max_size) {
809 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
810 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
811 rv = -EPERM;
812 goto exit;
813 }
814
815 goto usbtmc_clear_check_status;
816
817usbtmc_clear_bulk_out_halt:
818
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800819 rv = usb_clear_halt(data->usb_dev,
820 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700821 if (rv < 0) {
822 dev_err(dev, "usb_control_msg returned %d\n", rv);
823 goto exit;
824 }
825 rv = 0;
826
827exit:
828 kfree(buffer);
829 return rv;
830}
831
832static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
833{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700834 int rv;
835
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800836 rv = usb_clear_halt(data->usb_dev,
837 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700838
839 if (rv < 0) {
840 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
841 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800842 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700843 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800844 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700845}
846
847static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
848{
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700849 int rv;
850
Sarah Sharp3342ecd2009-12-03 11:35:59 -0800851 rv = usb_clear_halt(data->usb_dev,
852 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700853
854 if (rv < 0) {
855 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
856 rv);
Ming Leiac9e59c2013-04-18 12:17:38 +0800857 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700858 }
Ming Leiac9e59c2013-04-18 12:17:38 +0800859 return 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700860}
861
862static int get_capabilities(struct usbtmc_device_data *data)
863{
864 struct device *dev = &data->usb_dev->dev;
865 char *buffer;
Oliver Neukumca157c42009-07-02 16:41:39 +0200866 int rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700867
868 buffer = kmalloc(0x18, GFP_KERNEL);
869 if (!buffer)
870 return -ENOMEM;
871
872 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
873 USBTMC_REQUEST_GET_CAPABILITIES,
874 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
875 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
876 if (rv < 0) {
877 dev_err(dev, "usb_control_msg returned %d\n", rv);
Oliver Neukumca157c42009-07-02 16:41:39 +0200878 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700879 }
880
881 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700882 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
883 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
Oliver Neukumca157c42009-07-02 16:41:39 +0200884 rv = -EPERM;
885 goto err_out;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700886 }
Gergely Imrehd0a38362009-09-07 10:47:01 +0800887 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
888 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
889 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
890 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700891
892 data->capabilities.interface_capabilities = buffer[4];
893 data->capabilities.device_capabilities = buffer[5];
894 data->capabilities.usb488_interface_capabilities = buffer[14];
895 data->capabilities.usb488_device_capabilities = buffer[15];
Gergely Imrehd0a38362009-09-07 10:47:01 +0800896 rv = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700897
Oliver Neukumca157c42009-07-02 16:41:39 +0200898err_out:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700899 kfree(buffer);
Oliver Neukumca157c42009-07-02 16:41:39 +0200900 return rv;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700901}
902
903#define capability_attribute(name) \
904static ssize_t show_##name(struct device *dev, \
905 struct device_attribute *attr, char *buf) \
906{ \
907 struct usb_interface *intf = to_usb_interface(dev); \
908 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
909 \
910 return sprintf(buf, "%d\n", data->capabilities.name); \
911} \
912static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
913
914capability_attribute(interface_capabilities);
915capability_attribute(device_capabilities);
916capability_attribute(usb488_interface_capabilities);
917capability_attribute(usb488_device_capabilities);
918
919static struct attribute *capability_attrs[] = {
920 &dev_attr_interface_capabilities.attr,
921 &dev_attr_device_capabilities.attr,
922 &dev_attr_usb488_interface_capabilities.attr,
923 &dev_attr_usb488_device_capabilities.attr,
924 NULL,
925};
926
927static struct attribute_group capability_attr_grp = {
928 .attrs = capability_attrs,
929};
930
931static ssize_t show_TermChar(struct device *dev,
932 struct device_attribute *attr, char *buf)
933{
934 struct usb_interface *intf = to_usb_interface(dev);
935 struct usbtmc_device_data *data = usb_get_intfdata(intf);
936
937 return sprintf(buf, "%c\n", data->TermChar);
938}
939
940static ssize_t store_TermChar(struct device *dev,
941 struct device_attribute *attr,
942 const char *buf, size_t count)
943{
944 struct usb_interface *intf = to_usb_interface(dev);
945 struct usbtmc_device_data *data = usb_get_intfdata(intf);
946
947 if (count < 1)
948 return -EINVAL;
949 data->TermChar = buf[0];
950 return count;
951}
952static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
953
954#define data_attribute(name) \
955static ssize_t show_##name(struct device *dev, \
956 struct device_attribute *attr, char *buf) \
957{ \
958 struct usb_interface *intf = to_usb_interface(dev); \
959 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
960 \
961 return sprintf(buf, "%d\n", data->name); \
962} \
963static ssize_t store_##name(struct device *dev, \
964 struct device_attribute *attr, \
965 const char *buf, size_t count) \
966{ \
967 struct usb_interface *intf = to_usb_interface(dev); \
968 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
969 ssize_t result; \
970 unsigned val; \
971 \
972 result = sscanf(buf, "%u\n", &val); \
973 if (result != 1) \
974 result = -EINVAL; \
975 data->name = val; \
976 if (result < 0) \
977 return result; \
978 else \
979 return count; \
980} \
981static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
982
983data_attribute(TermCharEnabled);
984data_attribute(auto_abort);
985
986static struct attribute *data_attrs[] = {
987 &dev_attr_TermChar.attr,
988 &dev_attr_TermCharEnabled.attr,
989 &dev_attr_auto_abort.attr,
990 NULL,
991};
992
993static struct attribute_group data_attr_grp = {
994 .attrs = data_attrs,
995};
996
997static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
998{
999 struct device *dev;
1000 u8 *buffer;
1001 int rv;
1002
1003 dev = &data->intf->dev;
1004
1005 buffer = kmalloc(2, GFP_KERNEL);
1006 if (!buffer)
1007 return -ENOMEM;
1008
1009 rv = usb_control_msg(data->usb_dev,
1010 usb_rcvctrlpipe(data->usb_dev, 0),
1011 USBTMC_REQUEST_INDICATOR_PULSE,
1012 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1013 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1014
1015 if (rv < 0) {
1016 dev_err(dev, "usb_control_msg returned %d\n", rv);
1017 goto exit;
1018 }
1019
1020 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1021
1022 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1023 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1024 rv = -EPERM;
1025 goto exit;
1026 }
1027 rv = 0;
1028
1029exit:
1030 kfree(buffer);
1031 return rv;
1032}
1033
1034static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1035{
1036 struct usbtmc_device_data *data;
1037 int retval = -EBADRQC;
1038
1039 data = file->private_data;
1040 mutex_lock(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001041 if (data->zombie) {
1042 retval = -ENODEV;
1043 goto skip_io_on_zombie;
1044 }
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001045
1046 switch (cmd) {
1047 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1048 retval = usbtmc_ioctl_clear_out_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001049 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001050
1051 case USBTMC_IOCTL_CLEAR_IN_HALT:
1052 retval = usbtmc_ioctl_clear_in_halt(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001053 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001054
1055 case USBTMC_IOCTL_INDICATOR_PULSE:
1056 retval = usbtmc_ioctl_indicator_pulse(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001057 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001058
1059 case USBTMC_IOCTL_CLEAR:
1060 retval = usbtmc_ioctl_clear(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001061 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001062
1063 case USBTMC_IOCTL_ABORT_BULK_OUT:
1064 retval = usbtmc_ioctl_abort_bulk_out(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001065 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001066
1067 case USBTMC_IOCTL_ABORT_BULK_IN:
1068 retval = usbtmc_ioctl_abort_bulk_in(data);
Greg Kroah-Hartmana92b63e2009-06-15 13:13:05 -07001069 break;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001070 }
1071
Oliver Neukum86286882009-07-02 11:36:30 +02001072skip_io_on_zombie:
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001073 mutex_unlock(&data->io_mutex);
1074 return retval;
1075}
1076
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001077static const struct file_operations fops = {
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001078 .owner = THIS_MODULE,
1079 .read = usbtmc_read,
1080 .write = usbtmc_write,
1081 .open = usbtmc_open,
1082 .release = usbtmc_release,
1083 .unlocked_ioctl = usbtmc_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001084 .llseek = default_llseek,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001085};
1086
1087static struct usb_class_driver usbtmc_class = {
1088 .name = "usbtmc%d",
1089 .fops = &fops,
1090 .minor_base = USBTMC_MINOR_BASE,
1091};
1092
1093
1094static int usbtmc_probe(struct usb_interface *intf,
1095 const struct usb_device_id *id)
1096{
1097 struct usbtmc_device_data *data;
1098 struct usb_host_interface *iface_desc;
1099 struct usb_endpoint_descriptor *endpoint;
1100 int n;
1101 int retcode;
1102
1103 dev_dbg(&intf->dev, "%s called\n", __func__);
1104
1105 data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
1106 if (!data) {
1107 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
1108 return -ENOMEM;
1109 }
1110
1111 data->intf = intf;
1112 data->id = id;
1113 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1114 usb_set_intfdata(intf, data);
1115 kref_init(&data->kref);
1116 mutex_init(&data->io_mutex);
Oliver Neukum86286882009-07-02 11:36:30 +02001117 data->zombie = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001118
Alexandre Peixoto Ferreirac2e31482013-04-30 00:51:52 -05001119 /* Determine if it is a Rigol or not */
1120 data->rigol_quirk = 0;
1121 dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1122 data->usb_dev->descriptor.idVendor,
1123 data->usb_dev->descriptor.idProduct);
1124 for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
1125 if ((usbtmc_id_quirk[n].idVendor == data->usb_dev->descriptor.idVendor) &&
1126 (usbtmc_id_quirk[n].idProduct == data->usb_dev->descriptor.idProduct)) {
1127 dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1128 data->rigol_quirk = 1;
1129 break;
1130 }
1131 }
1132
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001133 /* Initialize USBTMC bTag and other fields */
1134 data->bTag = 1;
1135 data->TermCharEnabled = 0;
1136 data->TermChar = '\n';
1137
1138 /* USBTMC devices have only one setting, so use that */
1139 iface_desc = data->intf->cur_altsetting;
1140
1141 /* Find bulk in endpoint */
1142 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1143 endpoint = &iface_desc->endpoint[n].desc;
1144
1145 if (usb_endpoint_is_bulk_in(endpoint)) {
1146 data->bulk_in = endpoint->bEndpointAddress;
1147 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1148 data->bulk_in);
1149 break;
1150 }
1151 }
1152
1153 /* Find bulk out endpoint */
1154 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1155 endpoint = &iface_desc->endpoint[n].desc;
1156
1157 if (usb_endpoint_is_bulk_out(endpoint)) {
1158 data->bulk_out = endpoint->bEndpointAddress;
1159 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1160 data->bulk_out);
1161 break;
1162 }
1163 }
1164
1165 retcode = get_capabilities(data);
1166 if (retcode)
1167 dev_err(&intf->dev, "can't read capabilities\n");
1168 else
1169 retcode = sysfs_create_group(&intf->dev.kobj,
1170 &capability_attr_grp);
1171
1172 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1173
1174 retcode = usb_register_dev(intf, &usbtmc_class);
1175 if (retcode) {
1176 dev_err(&intf->dev, "Not able to get a minor"
1177 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1178 retcode);
1179 goto error_register;
1180 }
1181 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1182
1183 return 0;
1184
1185error_register:
1186 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1187 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1188 kref_put(&data->kref, usbtmc_delete);
1189 return retcode;
1190}
1191
1192static void usbtmc_disconnect(struct usb_interface *intf)
1193{
1194 struct usbtmc_device_data *data;
1195
1196 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1197
1198 data = usb_get_intfdata(intf);
1199 usb_deregister_dev(intf, &usbtmc_class);
1200 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1201 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
Oliver Neukum86286882009-07-02 11:36:30 +02001202 mutex_lock(&data->io_mutex);
1203 data->zombie = 1;
1204 mutex_unlock(&data->io_mutex);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001205 kref_put(&data->kref, usbtmc_delete);
1206}
1207
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001208static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
Oliver Neukuma4708102009-07-02 11:44:33 +02001209{
1210 /* this driver does not have pending URBs */
1211 return 0;
1212}
1213
Oliver Neukumdca8cd02009-09-24 00:33:45 +02001214static int usbtmc_resume(struct usb_interface *intf)
Oliver Neukuma4708102009-07-02 11:44:33 +02001215{
1216 return 0;
1217}
1218
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001219static struct usb_driver usbtmc_driver = {
1220 .name = "usbtmc",
1221 .id_table = usbtmc_devices,
1222 .probe = usbtmc_probe,
Oliver Neukuma4708102009-07-02 11:44:33 +02001223 .disconnect = usbtmc_disconnect,
1224 .suspend = usbtmc_suspend,
1225 .resume = usbtmc_resume,
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001226};
1227
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -08001228module_usb_driver(usbtmc_driver);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001229
1230MODULE_LICENSE("GPL");