Add support for receiving USBTMC USB488 SRQ notifications via poll/select
[deliverable/linux.git] / drivers / usb / class / usbtmc.c
1 /**
2 * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
3 *
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/fs.h>
27 #include <linux/uaccess.h>
28 #include <linux/kref.h>
29 #include <linux/slab.h>
30 #include <linux/poll.h>
31 #include <linux/mutex.h>
32 #include <linux/usb.h>
33 #include <linux/usb/tmc.h>
34
35
36 #define RIGOL 1
37 #define USBTMC_HEADER_SIZE 12
38 #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) */
47 #define USBTMC_TIMEOUT 5000
48
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
56 static const struct usb_device_id usbtmc_devices[] = {
57 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
58 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
59 { 0, } /* terminating entry */
60 };
61 MODULE_DEVICE_TABLE(usb, usbtmc_devices);
62
63 /*
64 * This structure is the capabilities for the device
65 * 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.
68 */
69 struct 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 */
79 struct 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
91 /* data for interrupt in endpoint handling */
92 u8 bNotify1;
93 u8 bNotify2;
94 u16 ifnum;
95 u8 iin_bTag;
96 u8 *iin_buffer;
97 atomic_t iin_data_valid;
98 unsigned int iin_ep;
99 int iin_ep_present;
100 int iin_interval;
101 struct urb *iin_urb;
102 u16 iin_wMaxPacketSize;
103 atomic_t srq_asserted;
104
105 u8 rigol_quirk;
106
107 /* attributes from the USB TMC spec for this device */
108 u8 TermChar;
109 bool TermCharEnabled;
110 bool auto_abort;
111
112 bool zombie; /* fd of disconnected device */
113
114 struct usbtmc_dev_capabilities capabilities;
115 struct kref kref;
116 struct mutex io_mutex; /* only one i/o function running at a time */
117 wait_queue_head_t waitq;
118 struct fasync_struct *fasync;
119 };
120 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
121
122 struct usbtmc_ID_rigol_quirk {
123 __u16 idVendor;
124 __u16 idProduct;
125 };
126
127 static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
128 { 0x1ab1, 0x0588 },
129 { 0x1ab1, 0x04b0 },
130 { 0, 0 }
131 };
132
133 /* Forward declarations */
134 static struct usb_driver usbtmc_driver;
135
136 static void usbtmc_delete(struct kref *kref)
137 {
138 struct usbtmc_device_data *data = to_usbtmc_data(kref);
139
140 usb_put_dev(data->usb_dev);
141 }
142
143 static int usbtmc_open(struct inode *inode, struct file *filp)
144 {
145 struct usb_interface *intf;
146 struct usbtmc_device_data *data;
147 int retval = 0;
148
149 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
150 if (!intf) {
151 pr_err("can not find device for minor %d", iminor(inode));
152 return -ENODEV;
153 }
154
155 data = usb_get_intfdata(intf);
156 kref_get(&data->kref);
157
158 /* Store pointer in file structure's private data field */
159 filp->private_data = data;
160
161 return retval;
162 }
163
164 static int usbtmc_release(struct inode *inode, struct file *file)
165 {
166 struct usbtmc_device_data *data = file->private_data;
167
168 kref_put(&data->kref, usbtmc_delete);
169 return 0;
170 }
171
172 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
173 {
174 u8 *buffer;
175 struct device *dev;
176 int rv;
177 int n;
178 int actual;
179 struct usb_host_interface *current_setting;
180 int max_size;
181
182 dev = &data->intf->dev;
183 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
184 if (!buffer)
185 return -ENOMEM;
186
187 rv = usb_control_msg(data->usb_dev,
188 usb_rcvctrlpipe(data->usb_dev, 0),
189 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
190 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
191 data->bTag_last_read, data->bulk_in,
192 buffer, 2, USBTMC_TIMEOUT);
193
194 if (rv < 0) {
195 dev_err(dev, "usb_control_msg returned %d\n", rv);
196 goto exit;
197 }
198
199 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
200
201 if (buffer[0] == USBTMC_STATUS_FAILED) {
202 rv = 0;
203 goto exit;
204 }
205
206 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
207 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
208 buffer[0]);
209 rv = -EPERM;
210 goto exit;
211 }
212
213 max_size = 0;
214 current_setting = data->intf->cur_altsetting;
215 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
216 if (current_setting->endpoint[n].desc.bEndpointAddress ==
217 data->bulk_in)
218 max_size = usb_endpoint_maxp(&current_setting->endpoint[n].desc);
219
220 if (max_size == 0) {
221 dev_err(dev, "Couldn't get wMaxPacketSize\n");
222 rv = -EPERM;
223 goto exit;
224 }
225
226 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
227
228 n = 0;
229
230 do {
231 dev_dbg(dev, "Reading from bulk in EP\n");
232
233 rv = usb_bulk_msg(data->usb_dev,
234 usb_rcvbulkpipe(data->usb_dev,
235 data->bulk_in),
236 buffer, USBTMC_SIZE_IOBUFFER,
237 &actual, USBTMC_TIMEOUT);
238
239 n++;
240
241 if (rv < 0) {
242 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
243 goto exit;
244 }
245 } while ((actual == max_size) &&
246 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
247
248 if (actual == max_size) {
249 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
250 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
251 rv = -EPERM;
252 goto exit;
253 }
254
255 n = 0;
256
257 usbtmc_abort_bulk_in_status:
258 rv = usb_control_msg(data->usb_dev,
259 usb_rcvctrlpipe(data->usb_dev, 0),
260 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
261 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
262 0, data->bulk_in, buffer, 0x08,
263 USBTMC_TIMEOUT);
264
265 if (rv < 0) {
266 dev_err(dev, "usb_control_msg returned %d\n", rv);
267 goto exit;
268 }
269
270 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
271
272 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
273 rv = 0;
274 goto exit;
275 }
276
277 if (buffer[0] != USBTMC_STATUS_PENDING) {
278 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
279 rv = -EPERM;
280 goto exit;
281 }
282
283 if (buffer[1] == 1)
284 do {
285 dev_dbg(dev, "Reading from bulk in EP\n");
286
287 rv = usb_bulk_msg(data->usb_dev,
288 usb_rcvbulkpipe(data->usb_dev,
289 data->bulk_in),
290 buffer, USBTMC_SIZE_IOBUFFER,
291 &actual, USBTMC_TIMEOUT);
292
293 n++;
294
295 if (rv < 0) {
296 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
297 goto exit;
298 }
299 } while ((actual == max_size) &&
300 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
301
302 if (actual == max_size) {
303 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
304 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
305 rv = -EPERM;
306 goto exit;
307 }
308
309 goto usbtmc_abort_bulk_in_status;
310
311 exit:
312 kfree(buffer);
313 return rv;
314
315 }
316
317 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
318 {
319 struct device *dev;
320 u8 *buffer;
321 int rv;
322 int n;
323
324 dev = &data->intf->dev;
325
326 buffer = kmalloc(8, GFP_KERNEL);
327 if (!buffer)
328 return -ENOMEM;
329
330 rv = usb_control_msg(data->usb_dev,
331 usb_rcvctrlpipe(data->usb_dev, 0),
332 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
333 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
334 data->bTag_last_write, data->bulk_out,
335 buffer, 2, USBTMC_TIMEOUT);
336
337 if (rv < 0) {
338 dev_err(dev, "usb_control_msg returned %d\n", rv);
339 goto exit;
340 }
341
342 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
343
344 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
345 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
346 buffer[0]);
347 rv = -EPERM;
348 goto exit;
349 }
350
351 n = 0;
352
353 usbtmc_abort_bulk_out_check_status:
354 rv = usb_control_msg(data->usb_dev,
355 usb_rcvctrlpipe(data->usb_dev, 0),
356 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
357 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
358 0, data->bulk_out, buffer, 0x08,
359 USBTMC_TIMEOUT);
360 n++;
361 if (rv < 0) {
362 dev_err(dev, "usb_control_msg returned %d\n", rv);
363 goto exit;
364 }
365
366 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
367
368 if (buffer[0] == USBTMC_STATUS_SUCCESS)
369 goto usbtmc_abort_bulk_out_clear_halt;
370
371 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
372 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
373 goto usbtmc_abort_bulk_out_check_status;
374
375 rv = -EPERM;
376 goto exit;
377
378 usbtmc_abort_bulk_out_clear_halt:
379 rv = usb_clear_halt(data->usb_dev,
380 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
381
382 if (rv < 0) {
383 dev_err(dev, "usb_control_msg returned %d\n", rv);
384 goto exit;
385 }
386 rv = 0;
387
388 exit:
389 kfree(buffer);
390 return rv;
391 }
392
393 static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data *data,
394 void __user *arg)
395 {
396 struct device *dev = &data->intf->dev;
397 u8 *buffer;
398 u8 tag;
399 __u8 stb;
400 int rv;
401
402 dev_dbg(dev, "Enter ioctl_read_stb iin_ep_present: %d\n",
403 data->iin_ep_present);
404
405 buffer = kmalloc(8, GFP_KERNEL);
406 if (!buffer)
407 return -ENOMEM;
408
409 atomic_set(&data->iin_data_valid, 0);
410
411 /* must issue read_stb before using poll or select */
412 atomic_set(&data->srq_asserted, 0);
413
414 rv = usb_control_msg(data->usb_dev,
415 usb_rcvctrlpipe(data->usb_dev, 0),
416 USBTMC488_REQUEST_READ_STATUS_BYTE,
417 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
418 data->iin_bTag,
419 data->ifnum,
420 buffer, 0x03, USBTMC_TIMEOUT);
421 if (rv < 0) {
422 dev_err(dev, "stb usb_control_msg returned %d\n", rv);
423 goto exit;
424 }
425
426 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
427 dev_err(dev, "control status returned %x\n", buffer[0]);
428 rv = -EIO;
429 goto exit;
430 }
431
432 if (data->iin_ep_present) {
433 rv = wait_event_interruptible_timeout(
434 data->waitq,
435 atomic_read(&data->iin_data_valid) != 0,
436 USBTMC_TIMEOUT);
437 if (rv < 0) {
438 dev_dbg(dev, "wait interrupted %d\n", rv);
439 goto exit;
440 }
441
442 if (rv == 0) {
443 dev_dbg(dev, "wait timed out\n");
444 rv = -ETIME;
445 goto exit;
446 }
447
448 tag = data->bNotify1 & 0x7f;
449 if (tag != data->iin_bTag) {
450 dev_err(dev, "expected bTag %x got %x\n",
451 data->iin_bTag, tag);
452 }
453
454 stb = data->bNotify2;
455 } else {
456 stb = buffer[2];
457 }
458
459 rv = copy_to_user(arg, &stb, sizeof(stb));
460 if (rv)
461 rv = -EFAULT;
462
463 exit:
464 /* bump interrupt bTag */
465 data->iin_bTag += 1;
466 if (data->iin_bTag > 127)
467 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
468 data->iin_bTag = 2;
469
470 kfree(buffer);
471 return rv;
472 }
473
474 /*
475 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
476 * @transfer_size: number of bytes to request from the device.
477 *
478 * See the USBTMC specification, Table 4.
479 *
480 * Also updates bTag_last_write.
481 */
482 static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
483 {
484 int retval;
485 u8 *buffer;
486 int actual;
487
488 buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
489 if (!buffer)
490 return -ENOMEM;
491 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
492 * Refer to class specs for details
493 */
494 buffer[0] = 2;
495 buffer[1] = data->bTag;
496 buffer[2] = ~data->bTag;
497 buffer[3] = 0; /* Reserved */
498 buffer[4] = transfer_size >> 0;
499 buffer[5] = transfer_size >> 8;
500 buffer[6] = transfer_size >> 16;
501 buffer[7] = transfer_size >> 24;
502 buffer[8] = data->TermCharEnabled * 2;
503 /* Use term character? */
504 buffer[9] = data->TermChar;
505 buffer[10] = 0; /* Reserved */
506 buffer[11] = 0; /* Reserved */
507
508 /* Send bulk URB */
509 retval = usb_bulk_msg(data->usb_dev,
510 usb_sndbulkpipe(data->usb_dev,
511 data->bulk_out),
512 buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
513
514 /* Store bTag (in case we need to abort) */
515 data->bTag_last_write = data->bTag;
516
517 /* Increment bTag -- and increment again if zero */
518 data->bTag++;
519 if (!data->bTag)
520 data->bTag++;
521
522 kfree(buffer);
523 if (retval < 0) {
524 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
525 return retval;
526 }
527
528 return 0;
529 }
530
531 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
532 size_t count, loff_t *f_pos)
533 {
534 struct usbtmc_device_data *data;
535 struct device *dev;
536 u32 n_characters;
537 u8 *buffer;
538 int actual;
539 size_t done;
540 size_t remaining;
541 int retval;
542 size_t this_part;
543
544 /* Get pointer to private data structure */
545 data = filp->private_data;
546 dev = &data->intf->dev;
547
548 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
549 if (!buffer)
550 return -ENOMEM;
551
552 mutex_lock(&data->io_mutex);
553 if (data->zombie) {
554 retval = -ENODEV;
555 goto exit;
556 }
557
558 if (data->rigol_quirk) {
559 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
560
561 retval = send_request_dev_dep_msg_in(data, count);
562
563 if (retval < 0) {
564 if (data->auto_abort)
565 usbtmc_ioctl_abort_bulk_out(data);
566 goto exit;
567 }
568 }
569
570 /* Loop until we have fetched everything we requested */
571 remaining = count;
572 this_part = remaining;
573 done = 0;
574
575 while (remaining > 0) {
576 if (!data->rigol_quirk) {
577 dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
578
579 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
580 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
581 else
582 this_part = remaining;
583
584 retval = send_request_dev_dep_msg_in(data, this_part);
585 if (retval < 0) {
586 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
587 if (data->auto_abort)
588 usbtmc_ioctl_abort_bulk_out(data);
589 goto exit;
590 }
591 }
592
593 /* Send bulk URB */
594 retval = usb_bulk_msg(data->usb_dev,
595 usb_rcvbulkpipe(data->usb_dev,
596 data->bulk_in),
597 buffer, USBTMC_SIZE_IOBUFFER, &actual,
598 USBTMC_TIMEOUT);
599
600 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
601
602 /* Store bTag (in case we need to abort) */
603 data->bTag_last_read = data->bTag;
604
605 if (retval < 0) {
606 dev_dbg(dev, "Unable to read data, error %d\n", retval);
607 if (data->auto_abort)
608 usbtmc_ioctl_abort_bulk_in(data);
609 goto exit;
610 }
611
612 /* Parse header in first packet */
613 if ((done == 0) || !data->rigol_quirk) {
614 /* Sanity checks for the header */
615 if (actual < USBTMC_HEADER_SIZE) {
616 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
617 if (data->auto_abort)
618 usbtmc_ioctl_abort_bulk_in(data);
619 goto exit;
620 }
621
622 if (buffer[0] != 2) {
623 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
624 if (data->auto_abort)
625 usbtmc_ioctl_abort_bulk_in(data);
626 goto exit;
627 }
628
629 if (buffer[1] != data->bTag_last_write) {
630 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
631 if (data->auto_abort)
632 usbtmc_ioctl_abort_bulk_in(data);
633 goto exit;
634 }
635
636 /* How many characters did the instrument send? */
637 n_characters = buffer[4] +
638 (buffer[5] << 8) +
639 (buffer[6] << 16) +
640 (buffer[7] << 24);
641
642 if (n_characters > this_part) {
643 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
644 if (data->auto_abort)
645 usbtmc_ioctl_abort_bulk_in(data);
646 goto exit;
647 }
648
649 /* Remove the USBTMC header */
650 actual -= USBTMC_HEADER_SIZE;
651
652 /* Check if the message is smaller than requested */
653 if (data->rigol_quirk) {
654 if (remaining > n_characters)
655 remaining = n_characters;
656 /* Remove padding if it exists */
657 if (actual > remaining)
658 actual = remaining;
659 }
660 else {
661 if (this_part > n_characters)
662 this_part = n_characters;
663 /* Remove padding if it exists */
664 if (actual > this_part)
665 actual = this_part;
666 }
667
668 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
669
670 remaining -= actual;
671
672 /* Terminate if end-of-message bit received from device */
673 if ((buffer[8] & 0x01) && (actual >= n_characters))
674 remaining = 0;
675
676 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
677
678
679 /* Copy buffer to user space */
680 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
681 /* There must have been an addressing problem */
682 retval = -EFAULT;
683 goto exit;
684 }
685 done += actual;
686 }
687 else {
688 if (actual > remaining)
689 actual = remaining;
690
691 remaining -= actual;
692
693 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
694
695 /* Copy buffer to user space */
696 if (copy_to_user(buf + done, buffer, actual)) {
697 /* There must have been an addressing problem */
698 retval = -EFAULT;
699 goto exit;
700 }
701 done += actual;
702 }
703 }
704
705 /* Update file position value */
706 *f_pos = *f_pos + done;
707 retval = done;
708
709 exit:
710 mutex_unlock(&data->io_mutex);
711 kfree(buffer);
712 return retval;
713 }
714
715 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
716 size_t count, loff_t *f_pos)
717 {
718 struct usbtmc_device_data *data;
719 u8 *buffer;
720 int retval;
721 int actual;
722 unsigned long int n_bytes;
723 int remaining;
724 int done;
725 int this_part;
726
727 data = filp->private_data;
728
729 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
730 if (!buffer)
731 return -ENOMEM;
732
733 mutex_lock(&data->io_mutex);
734 if (data->zombie) {
735 retval = -ENODEV;
736 goto exit;
737 }
738
739 remaining = count;
740 done = 0;
741
742 while (remaining > 0) {
743 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
744 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
745 buffer[8] = 0;
746 } else {
747 this_part = remaining;
748 buffer[8] = 1;
749 }
750
751 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
752 buffer[0] = 1;
753 buffer[1] = data->bTag;
754 buffer[2] = ~data->bTag;
755 buffer[3] = 0; /* Reserved */
756 buffer[4] = this_part >> 0;
757 buffer[5] = this_part >> 8;
758 buffer[6] = this_part >> 16;
759 buffer[7] = this_part >> 24;
760 /* buffer[8] is set above... */
761 buffer[9] = 0; /* Reserved */
762 buffer[10] = 0; /* Reserved */
763 buffer[11] = 0; /* Reserved */
764
765 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
766 retval = -EFAULT;
767 goto exit;
768 }
769
770 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
771 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
772
773 do {
774 retval = usb_bulk_msg(data->usb_dev,
775 usb_sndbulkpipe(data->usb_dev,
776 data->bulk_out),
777 buffer, n_bytes,
778 &actual, USBTMC_TIMEOUT);
779 if (retval != 0)
780 break;
781 n_bytes -= actual;
782 } while (n_bytes);
783
784 data->bTag_last_write = data->bTag;
785 data->bTag++;
786
787 if (!data->bTag)
788 data->bTag++;
789
790 if (retval < 0) {
791 dev_err(&data->intf->dev,
792 "Unable to send data, error %d\n", retval);
793 if (data->auto_abort)
794 usbtmc_ioctl_abort_bulk_out(data);
795 goto exit;
796 }
797
798 remaining -= this_part;
799 done += this_part;
800 }
801
802 retval = count;
803 exit:
804 mutex_unlock(&data->io_mutex);
805 kfree(buffer);
806 return retval;
807 }
808
809 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
810 {
811 struct usb_host_interface *current_setting;
812 struct usb_endpoint_descriptor *desc;
813 struct device *dev;
814 u8 *buffer;
815 int rv;
816 int n;
817 int actual = 0;
818 int max_size;
819
820 dev = &data->intf->dev;
821
822 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
823
824 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
825 if (!buffer)
826 return -ENOMEM;
827
828 rv = usb_control_msg(data->usb_dev,
829 usb_rcvctrlpipe(data->usb_dev, 0),
830 USBTMC_REQUEST_INITIATE_CLEAR,
831 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
832 0, 0, buffer, 1, USBTMC_TIMEOUT);
833 if (rv < 0) {
834 dev_err(dev, "usb_control_msg returned %d\n", rv);
835 goto exit;
836 }
837
838 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
839
840 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
841 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
842 rv = -EPERM;
843 goto exit;
844 }
845
846 max_size = 0;
847 current_setting = data->intf->cur_altsetting;
848 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
849 desc = &current_setting->endpoint[n].desc;
850 if (desc->bEndpointAddress == data->bulk_in)
851 max_size = usb_endpoint_maxp(desc);
852 }
853
854 if (max_size == 0) {
855 dev_err(dev, "Couldn't get wMaxPacketSize\n");
856 rv = -EPERM;
857 goto exit;
858 }
859
860 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
861
862 n = 0;
863
864 usbtmc_clear_check_status:
865
866 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
867
868 rv = usb_control_msg(data->usb_dev,
869 usb_rcvctrlpipe(data->usb_dev, 0),
870 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
871 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
872 0, 0, buffer, 2, USBTMC_TIMEOUT);
873 if (rv < 0) {
874 dev_err(dev, "usb_control_msg returned %d\n", rv);
875 goto exit;
876 }
877
878 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
879
880 if (buffer[0] == USBTMC_STATUS_SUCCESS)
881 goto usbtmc_clear_bulk_out_halt;
882
883 if (buffer[0] != USBTMC_STATUS_PENDING) {
884 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
885 rv = -EPERM;
886 goto exit;
887 }
888
889 if (buffer[1] == 1)
890 do {
891 dev_dbg(dev, "Reading from bulk in EP\n");
892
893 rv = usb_bulk_msg(data->usb_dev,
894 usb_rcvbulkpipe(data->usb_dev,
895 data->bulk_in),
896 buffer, USBTMC_SIZE_IOBUFFER,
897 &actual, USBTMC_TIMEOUT);
898 n++;
899
900 if (rv < 0) {
901 dev_err(dev, "usb_control_msg returned %d\n",
902 rv);
903 goto exit;
904 }
905 } while ((actual == max_size) &&
906 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
907
908 if (actual == max_size) {
909 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
910 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
911 rv = -EPERM;
912 goto exit;
913 }
914
915 goto usbtmc_clear_check_status;
916
917 usbtmc_clear_bulk_out_halt:
918
919 rv = usb_clear_halt(data->usb_dev,
920 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
921 if (rv < 0) {
922 dev_err(dev, "usb_control_msg returned %d\n", rv);
923 goto exit;
924 }
925 rv = 0;
926
927 exit:
928 kfree(buffer);
929 return rv;
930 }
931
932 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
933 {
934 int rv;
935
936 rv = usb_clear_halt(data->usb_dev,
937 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
938
939 if (rv < 0) {
940 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
941 rv);
942 return rv;
943 }
944 return 0;
945 }
946
947 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
948 {
949 int rv;
950
951 rv = usb_clear_halt(data->usb_dev,
952 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
953
954 if (rv < 0) {
955 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
956 rv);
957 return rv;
958 }
959 return 0;
960 }
961
962 static int get_capabilities(struct usbtmc_device_data *data)
963 {
964 struct device *dev = &data->usb_dev->dev;
965 char *buffer;
966 int rv = 0;
967
968 buffer = kmalloc(0x18, GFP_KERNEL);
969 if (!buffer)
970 return -ENOMEM;
971
972 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
973 USBTMC_REQUEST_GET_CAPABILITIES,
974 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
975 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
976 if (rv < 0) {
977 dev_err(dev, "usb_control_msg returned %d\n", rv);
978 goto err_out;
979 }
980
981 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
982 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
983 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
984 rv = -EPERM;
985 goto err_out;
986 }
987 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
988 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
989 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
990 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
991
992 data->capabilities.interface_capabilities = buffer[4];
993 data->capabilities.device_capabilities = buffer[5];
994 data->capabilities.usb488_interface_capabilities = buffer[14];
995 data->capabilities.usb488_device_capabilities = buffer[15];
996 rv = 0;
997
998 err_out:
999 kfree(buffer);
1000 return rv;
1001 }
1002
1003 #define capability_attribute(name) \
1004 static ssize_t name##_show(struct device *dev, \
1005 struct device_attribute *attr, char *buf) \
1006 { \
1007 struct usb_interface *intf = to_usb_interface(dev); \
1008 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1009 \
1010 return sprintf(buf, "%d\n", data->capabilities.name); \
1011 } \
1012 static DEVICE_ATTR_RO(name)
1013
1014 capability_attribute(interface_capabilities);
1015 capability_attribute(device_capabilities);
1016 capability_attribute(usb488_interface_capabilities);
1017 capability_attribute(usb488_device_capabilities);
1018
1019 static struct attribute *capability_attrs[] = {
1020 &dev_attr_interface_capabilities.attr,
1021 &dev_attr_device_capabilities.attr,
1022 &dev_attr_usb488_interface_capabilities.attr,
1023 &dev_attr_usb488_device_capabilities.attr,
1024 NULL,
1025 };
1026
1027 static struct attribute_group capability_attr_grp = {
1028 .attrs = capability_attrs,
1029 };
1030
1031 static ssize_t TermChar_show(struct device *dev,
1032 struct device_attribute *attr, char *buf)
1033 {
1034 struct usb_interface *intf = to_usb_interface(dev);
1035 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1036
1037 return sprintf(buf, "%c\n", data->TermChar);
1038 }
1039
1040 static ssize_t TermChar_store(struct device *dev,
1041 struct device_attribute *attr,
1042 const char *buf, size_t count)
1043 {
1044 struct usb_interface *intf = to_usb_interface(dev);
1045 struct usbtmc_device_data *data = usb_get_intfdata(intf);
1046
1047 if (count < 1)
1048 return -EINVAL;
1049 data->TermChar = buf[0];
1050 return count;
1051 }
1052 static DEVICE_ATTR_RW(TermChar);
1053
1054 #define data_attribute(name) \
1055 static ssize_t name##_show(struct device *dev, \
1056 struct device_attribute *attr, char *buf) \
1057 { \
1058 struct usb_interface *intf = to_usb_interface(dev); \
1059 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1060 \
1061 return sprintf(buf, "%d\n", data->name); \
1062 } \
1063 static ssize_t name##_store(struct device *dev, \
1064 struct device_attribute *attr, \
1065 const char *buf, size_t count) \
1066 { \
1067 struct usb_interface *intf = to_usb_interface(dev); \
1068 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1069 ssize_t result; \
1070 unsigned val; \
1071 \
1072 result = sscanf(buf, "%u\n", &val); \
1073 if (result != 1) \
1074 result = -EINVAL; \
1075 data->name = val; \
1076 if (result < 0) \
1077 return result; \
1078 else \
1079 return count; \
1080 } \
1081 static DEVICE_ATTR_RW(name)
1082
1083 data_attribute(TermCharEnabled);
1084 data_attribute(auto_abort);
1085
1086 static struct attribute *data_attrs[] = {
1087 &dev_attr_TermChar.attr,
1088 &dev_attr_TermCharEnabled.attr,
1089 &dev_attr_auto_abort.attr,
1090 NULL,
1091 };
1092
1093 static struct attribute_group data_attr_grp = {
1094 .attrs = data_attrs,
1095 };
1096
1097 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
1098 {
1099 struct device *dev;
1100 u8 *buffer;
1101 int rv;
1102
1103 dev = &data->intf->dev;
1104
1105 buffer = kmalloc(2, GFP_KERNEL);
1106 if (!buffer)
1107 return -ENOMEM;
1108
1109 rv = usb_control_msg(data->usb_dev,
1110 usb_rcvctrlpipe(data->usb_dev, 0),
1111 USBTMC_REQUEST_INDICATOR_PULSE,
1112 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1113 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1114
1115 if (rv < 0) {
1116 dev_err(dev, "usb_control_msg returned %d\n", rv);
1117 goto exit;
1118 }
1119
1120 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1121
1122 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1123 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1124 rv = -EPERM;
1125 goto exit;
1126 }
1127 rv = 0;
1128
1129 exit:
1130 kfree(buffer);
1131 return rv;
1132 }
1133
1134 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1135 {
1136 struct usbtmc_device_data *data;
1137 int retval = -EBADRQC;
1138
1139 data = file->private_data;
1140 mutex_lock(&data->io_mutex);
1141 if (data->zombie) {
1142 retval = -ENODEV;
1143 goto skip_io_on_zombie;
1144 }
1145
1146 switch (cmd) {
1147 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1148 retval = usbtmc_ioctl_clear_out_halt(data);
1149 break;
1150
1151 case USBTMC_IOCTL_CLEAR_IN_HALT:
1152 retval = usbtmc_ioctl_clear_in_halt(data);
1153 break;
1154
1155 case USBTMC_IOCTL_INDICATOR_PULSE:
1156 retval = usbtmc_ioctl_indicator_pulse(data);
1157 break;
1158
1159 case USBTMC_IOCTL_CLEAR:
1160 retval = usbtmc_ioctl_clear(data);
1161 break;
1162
1163 case USBTMC_IOCTL_ABORT_BULK_OUT:
1164 retval = usbtmc_ioctl_abort_bulk_out(data);
1165 break;
1166
1167 case USBTMC_IOCTL_ABORT_BULK_IN:
1168 retval = usbtmc_ioctl_abort_bulk_in(data);
1169 break;
1170
1171 case USBTMC488_IOCTL_READ_STB:
1172 retval = usbtmc488_ioctl_read_stb(data, (void __user *)arg);
1173 break;
1174 }
1175
1176 skip_io_on_zombie:
1177 mutex_unlock(&data->io_mutex);
1178 return retval;
1179 }
1180
1181 static int usbtmc_fasync(int fd, struct file *file, int on)
1182 {
1183 struct usbtmc_device_data *data = file->private_data;
1184
1185 return fasync_helper(fd, file, on, &data->fasync);
1186 }
1187
1188 static unsigned int usbtmc_poll(struct file *file, poll_table *wait)
1189 {
1190 struct usbtmc_device_data *data = file->private_data;
1191 unsigned int mask;
1192
1193 mutex_lock(&data->io_mutex);
1194
1195 if (data->zombie) {
1196 mask = POLLHUP | POLLERR;
1197 goto no_poll;
1198 }
1199
1200 poll_wait(file, &data->waitq, wait);
1201
1202 mask = (atomic_read(&data->srq_asserted)) ? POLLIN | POLLRDNORM : 0;
1203
1204 no_poll:
1205 mutex_unlock(&data->io_mutex);
1206 return mask;
1207 }
1208
1209 static const struct file_operations fops = {
1210 .owner = THIS_MODULE,
1211 .read = usbtmc_read,
1212 .write = usbtmc_write,
1213 .open = usbtmc_open,
1214 .release = usbtmc_release,
1215 .unlocked_ioctl = usbtmc_ioctl,
1216 .fasync = usbtmc_fasync,
1217 .poll = usbtmc_poll,
1218 .llseek = default_llseek,
1219 };
1220
1221 static struct usb_class_driver usbtmc_class = {
1222 .name = "usbtmc%d",
1223 .fops = &fops,
1224 .minor_base = USBTMC_MINOR_BASE,
1225 };
1226
1227 static void usbtmc_interrupt(struct urb *urb)
1228 {
1229 struct usbtmc_device_data *data = urb->context;
1230 struct device *dev = &data->intf->dev;
1231 int status = urb->status;
1232 int rv;
1233
1234 dev_dbg(&data->intf->dev, "int status: %d len %d\n",
1235 status, urb->actual_length);
1236
1237 switch (status) {
1238 case 0: /* SUCCESS */
1239 /* check for valid STB notification */
1240 if (data->iin_buffer[0] > 0x81) {
1241 data->bNotify1 = data->iin_buffer[0];
1242 data->bNotify2 = data->iin_buffer[1];
1243 atomic_set(&data->iin_data_valid, 1);
1244 wake_up_interruptible(&data->waitq);
1245 goto exit;
1246 }
1247 /* check for SRQ notification */
1248 if (data->iin_buffer[0] == 0x81) {
1249 if (data->fasync)
1250 kill_fasync(&data->fasync,
1251 SIGIO, POLL_IN);
1252
1253 atomic_set(&data->srq_asserted, 1);
1254 wake_up_interruptible(&data->waitq);
1255 goto exit;
1256 }
1257 dev_warn(dev, "invalid notification: %x\n", data->iin_buffer[0]);
1258 break;
1259 case -EOVERFLOW:
1260 dev_err(dev, "overflow with length %d, actual length is %d\n",
1261 data->iin_wMaxPacketSize, urb->actual_length);
1262 case -ECONNRESET:
1263 case -ENOENT:
1264 case -ESHUTDOWN:
1265 case -EILSEQ:
1266 case -ETIME:
1267 /* urb terminated, clean up */
1268 dev_dbg(dev, "urb terminated, status: %d\n", status);
1269 return;
1270 default:
1271 dev_err(dev, "unknown status received: %d\n", status);
1272 }
1273 exit:
1274 rv = usb_submit_urb(urb, GFP_ATOMIC);
1275 if (rv)
1276 dev_err(dev, "usb_submit_urb failed: %d\n", rv);
1277 }
1278
1279 static void usbtmc_free_int(struct usbtmc_device_data *data)
1280 {
1281 if (!data->iin_ep_present || !data->iin_urb)
1282 return;
1283 usb_kill_urb(data->iin_urb);
1284 kfree(data->iin_buffer);
1285 usb_free_urb(data->iin_urb);
1286 kref_put(&data->kref, usbtmc_delete);
1287 }
1288
1289 static int usbtmc_probe(struct usb_interface *intf,
1290 const struct usb_device_id *id)
1291 {
1292 struct usbtmc_device_data *data;
1293 struct usb_host_interface *iface_desc;
1294 struct usb_endpoint_descriptor *endpoint;
1295 int n;
1296 int retcode;
1297
1298 dev_dbg(&intf->dev, "%s called\n", __func__);
1299
1300 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
1301 if (!data)
1302 return -ENOMEM;
1303
1304 data->intf = intf;
1305 data->id = id;
1306 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1307 usb_set_intfdata(intf, data);
1308 kref_init(&data->kref);
1309 mutex_init(&data->io_mutex);
1310 init_waitqueue_head(&data->waitq);
1311 atomic_set(&data->iin_data_valid, 0);
1312 atomic_set(&data->srq_asserted, 0);
1313 data->zombie = 0;
1314
1315 /* Determine if it is a Rigol or not */
1316 data->rigol_quirk = 0;
1317 dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1318 le16_to_cpu(data->usb_dev->descriptor.idVendor),
1319 le16_to_cpu(data->usb_dev->descriptor.idProduct));
1320 for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
1321 if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) &&
1322 (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) {
1323 dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1324 data->rigol_quirk = 1;
1325 break;
1326 }
1327 }
1328
1329 /* Initialize USBTMC bTag and other fields */
1330 data->bTag = 1;
1331 data->TermCharEnabled = 0;
1332 data->TermChar = '\n';
1333 /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
1334 data->iin_bTag = 2;
1335
1336 /* USBTMC devices have only one setting, so use that */
1337 iface_desc = data->intf->cur_altsetting;
1338 data->ifnum = iface_desc->desc.bInterfaceNumber;
1339
1340 /* Find bulk in endpoint */
1341 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1342 endpoint = &iface_desc->endpoint[n].desc;
1343
1344 if (usb_endpoint_is_bulk_in(endpoint)) {
1345 data->bulk_in = endpoint->bEndpointAddress;
1346 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1347 data->bulk_in);
1348 break;
1349 }
1350 }
1351
1352 /* Find bulk out endpoint */
1353 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1354 endpoint = &iface_desc->endpoint[n].desc;
1355
1356 if (usb_endpoint_is_bulk_out(endpoint)) {
1357 data->bulk_out = endpoint->bEndpointAddress;
1358 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1359 data->bulk_out);
1360 break;
1361 }
1362 }
1363 /* Find int endpoint */
1364 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1365 endpoint = &iface_desc->endpoint[n].desc;
1366
1367 if (usb_endpoint_is_int_in(endpoint)) {
1368 data->iin_ep_present = 1;
1369 data->iin_ep = endpoint->bEndpointAddress;
1370 data->iin_wMaxPacketSize = usb_endpoint_maxp(endpoint);
1371 data->iin_interval = endpoint->bInterval;
1372 dev_dbg(&intf->dev, "Found Int in endpoint at %u\n",
1373 data->iin_ep);
1374 break;
1375 }
1376 }
1377
1378 retcode = get_capabilities(data);
1379 if (retcode)
1380 dev_err(&intf->dev, "can't read capabilities\n");
1381 else
1382 retcode = sysfs_create_group(&intf->dev.kobj,
1383 &capability_attr_grp);
1384
1385 if (data->iin_ep_present) {
1386 /* allocate int urb */
1387 data->iin_urb = usb_alloc_urb(0, GFP_KERNEL);
1388 if (!data->iin_urb) {
1389 dev_err(&intf->dev, "Failed to allocate int urb\n");
1390 goto error_register;
1391 }
1392
1393 /* will reference data in int urb */
1394 kref_get(&data->kref);
1395
1396 /* allocate buffer for interrupt in */
1397 data->iin_buffer = kmalloc(data->iin_wMaxPacketSize,
1398 GFP_KERNEL);
1399 if (!data->iin_buffer) {
1400 dev_err(&intf->dev, "Failed to allocate int buf\n");
1401 goto error_register;
1402 }
1403
1404 /* fill interrupt urb */
1405 usb_fill_int_urb(data->iin_urb, data->usb_dev,
1406 usb_rcvintpipe(data->usb_dev, data->iin_ep),
1407 data->iin_buffer, data->iin_wMaxPacketSize,
1408 usbtmc_interrupt,
1409 data, data->iin_interval);
1410
1411 retcode = usb_submit_urb(data->iin_urb, GFP_KERNEL);
1412 if (retcode) {
1413 dev_err(&intf->dev, "Failed to submit iin_urb\n");
1414 goto error_register;
1415 }
1416 }
1417
1418 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1419
1420 retcode = usb_register_dev(intf, &usbtmc_class);
1421 if (retcode) {
1422 dev_err(&intf->dev, "Not able to get a minor"
1423 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1424 retcode);
1425 goto error_register;
1426 }
1427 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1428
1429 return 0;
1430
1431 error_register:
1432 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1433 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1434 usbtmc_free_int(data);
1435 kref_put(&data->kref, usbtmc_delete);
1436 return retcode;
1437 }
1438
1439 static void usbtmc_disconnect(struct usb_interface *intf)
1440 {
1441 struct usbtmc_device_data *data;
1442
1443 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1444
1445 data = usb_get_intfdata(intf);
1446 usbtmc_free_int(data);
1447 usb_deregister_dev(intf, &usbtmc_class);
1448 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1449 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1450 mutex_lock(&data->io_mutex);
1451 data->zombie = 1;
1452 mutex_unlock(&data->io_mutex);
1453 kref_put(&data->kref, usbtmc_delete);
1454 }
1455
1456 static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
1457 {
1458 /* this driver does not have pending URBs */
1459 return 0;
1460 }
1461
1462 static int usbtmc_resume(struct usb_interface *intf)
1463 {
1464 return 0;
1465 }
1466
1467 static struct usb_driver usbtmc_driver = {
1468 .name = "usbtmc",
1469 .id_table = usbtmc_devices,
1470 .probe = usbtmc_probe,
1471 .disconnect = usbtmc_disconnect,
1472 .suspend = usbtmc_suspend,
1473 .resume = usbtmc_resume,
1474 };
1475
1476 module_usb_driver(usbtmc_driver);
1477
1478 MODULE_LICENSE("GPL");
This page took 0.060573 seconds and 6 git commands to generate.