Linux 4.16.11
[linux/fpc-iii.git] / drivers / usb / misc / adutux.c
blob4b8712733fc7dee3908fe3c783a571453f1e0065
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * adutux - driver for ADU devices from Ontrak Control Systems
4 * This is an experimental driver. Use at your own risk.
5 * This driver is not supported by Ontrak Control Systems.
7 * Copyright (c) 2003 John Homppi (SCO, leave this notice here)
9 * derived from the Lego USB Tower driver 0.56:
10 * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net>
11 * 2001 Juergen Stuber <stuber@loria.fr>
12 * that was derived from USB Skeleton driver - 0.5
13 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/kernel.h>
20 #include <linux/sched/signal.h>
21 #include <linux/errno.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/usb.h>
25 #include <linux/mutex.h>
26 #include <linux/uaccess.h>
28 #define DRIVER_AUTHOR "John Homppi"
29 #define DRIVER_DESC "adutux (see www.ontrak.net)"
31 /* Define these values to match your device */
32 #define ADU_VENDOR_ID 0x0a07
33 #define ADU_PRODUCT_ID 0x0064
35 /* table of devices that work with this driver */
36 static const struct usb_device_id device_table[] = {
37 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */
38 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */
39 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */
40 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */
41 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */
42 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */
43 { } /* Terminating entry */
46 MODULE_DEVICE_TABLE(usb, device_table);
48 #ifdef CONFIG_USB_DYNAMIC_MINORS
49 #define ADU_MINOR_BASE 0
50 #else
51 #define ADU_MINOR_BASE 67
52 #endif
54 /* we can have up to this number of device plugged in at once */
55 #define MAX_DEVICES 16
57 #define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */
60 * The locking scheme is a vanilla 3-lock:
61 * adu_device.buflock: A spinlock, covers what IRQs touch.
62 * adutux_mutex: A Static lock to cover open_count. It would also cover
63 * any globals, but we don't have them in 2.6.
64 * adu_device.mtx: A mutex to hold across sleepers like copy_from_user.
65 * It covers all of adu_device, except the open_count
66 * and what .buflock covers.
69 /* Structure to hold all of our device specific stuff */
70 struct adu_device {
71 struct mutex mtx;
72 struct usb_device *udev; /* save off the usb device pointer */
73 struct usb_interface *interface;
74 unsigned int minor; /* the starting minor number for this device */
75 char serial_number[8];
77 int open_count; /* number of times this port has been opened */
79 char *read_buffer_primary;
80 int read_buffer_length;
81 char *read_buffer_secondary;
82 int secondary_head;
83 int secondary_tail;
84 spinlock_t buflock;
86 wait_queue_head_t read_wait;
87 wait_queue_head_t write_wait;
89 char *interrupt_in_buffer;
90 struct usb_endpoint_descriptor *interrupt_in_endpoint;
91 struct urb *interrupt_in_urb;
92 int read_urb_finished;
94 char *interrupt_out_buffer;
95 struct usb_endpoint_descriptor *interrupt_out_endpoint;
96 struct urb *interrupt_out_urb;
97 int out_urb_finished;
100 static DEFINE_MUTEX(adutux_mutex);
102 static struct usb_driver adu_driver;
104 static inline void adu_debug_data(struct device *dev, const char *function,
105 int size, const unsigned char *data)
107 dev_dbg(dev, "%s - length = %d, data = %*ph\n",
108 function, size, size, data);
112 * adu_abort_transfers
113 * aborts transfers and frees associated data structures
115 static void adu_abort_transfers(struct adu_device *dev)
117 unsigned long flags;
119 if (dev->udev == NULL)
120 return;
122 /* shutdown transfer */
124 /* XXX Anchor these instead */
125 spin_lock_irqsave(&dev->buflock, flags);
126 if (!dev->read_urb_finished) {
127 spin_unlock_irqrestore(&dev->buflock, flags);
128 usb_kill_urb(dev->interrupt_in_urb);
129 } else
130 spin_unlock_irqrestore(&dev->buflock, flags);
132 spin_lock_irqsave(&dev->buflock, flags);
133 if (!dev->out_urb_finished) {
134 spin_unlock_irqrestore(&dev->buflock, flags);
135 usb_kill_urb(dev->interrupt_out_urb);
136 } else
137 spin_unlock_irqrestore(&dev->buflock, flags);
140 static void adu_delete(struct adu_device *dev)
142 /* free data structures */
143 usb_free_urb(dev->interrupt_in_urb);
144 usb_free_urb(dev->interrupt_out_urb);
145 kfree(dev->read_buffer_primary);
146 kfree(dev->read_buffer_secondary);
147 kfree(dev->interrupt_in_buffer);
148 kfree(dev->interrupt_out_buffer);
149 kfree(dev);
152 static void adu_interrupt_in_callback(struct urb *urb)
154 struct adu_device *dev = urb->context;
155 int status = urb->status;
157 adu_debug_data(&dev->udev->dev, __func__,
158 urb->actual_length, urb->transfer_buffer);
160 spin_lock(&dev->buflock);
162 if (status != 0) {
163 if ((status != -ENOENT) && (status != -ECONNRESET) &&
164 (status != -ESHUTDOWN)) {
165 dev_dbg(&dev->udev->dev,
166 "%s : nonzero status received: %d\n",
167 __func__, status);
169 goto exit;
172 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
173 if (dev->read_buffer_length <
174 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
175 (urb->actual_length)) {
176 memcpy (dev->read_buffer_primary +
177 dev->read_buffer_length,
178 dev->interrupt_in_buffer, urb->actual_length);
180 dev->read_buffer_length += urb->actual_length;
181 dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__,
182 urb->actual_length);
183 } else {
184 dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
185 __func__);
189 exit:
190 dev->read_urb_finished = 1;
191 spin_unlock(&dev->buflock);
192 /* always wake up so we recover from errors */
193 wake_up_interruptible(&dev->read_wait);
196 static void adu_interrupt_out_callback(struct urb *urb)
198 struct adu_device *dev = urb->context;
199 int status = urb->status;
201 adu_debug_data(&dev->udev->dev, __func__,
202 urb->actual_length, urb->transfer_buffer);
204 if (status != 0) {
205 if ((status != -ENOENT) &&
206 (status != -ECONNRESET)) {
207 dev_dbg(&dev->udev->dev,
208 "%s :nonzero status received: %d\n", __func__,
209 status);
211 return;
214 spin_lock(&dev->buflock);
215 dev->out_urb_finished = 1;
216 wake_up(&dev->write_wait);
217 spin_unlock(&dev->buflock);
220 static int adu_open(struct inode *inode, struct file *file)
222 struct adu_device *dev = NULL;
223 struct usb_interface *interface;
224 int subminor;
225 int retval;
227 subminor = iminor(inode);
229 retval = mutex_lock_interruptible(&adutux_mutex);
230 if (retval)
231 goto exit_no_lock;
233 interface = usb_find_interface(&adu_driver, subminor);
234 if (!interface) {
235 pr_err("%s - error, can't find device for minor %d\n",
236 __func__, subminor);
237 retval = -ENODEV;
238 goto exit_no_device;
241 dev = usb_get_intfdata(interface);
242 if (!dev || !dev->udev) {
243 retval = -ENODEV;
244 goto exit_no_device;
247 /* check that nobody else is using the device */
248 if (dev->open_count) {
249 retval = -EBUSY;
250 goto exit_no_device;
253 ++dev->open_count;
254 dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
255 dev->open_count);
257 /* save device in the file's private structure */
258 file->private_data = dev;
260 /* initialize in direction */
261 dev->read_buffer_length = 0;
263 /* fixup first read by having urb waiting for it */
264 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
265 usb_rcvintpipe(dev->udev,
266 dev->interrupt_in_endpoint->bEndpointAddress),
267 dev->interrupt_in_buffer,
268 usb_endpoint_maxp(dev->interrupt_in_endpoint),
269 adu_interrupt_in_callback, dev,
270 dev->interrupt_in_endpoint->bInterval);
271 dev->read_urb_finished = 0;
272 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
273 dev->read_urb_finished = 1;
274 /* we ignore failure */
275 /* end of fixup for first read */
277 /* initialize out direction */
278 dev->out_urb_finished = 1;
280 retval = 0;
282 exit_no_device:
283 mutex_unlock(&adutux_mutex);
284 exit_no_lock:
285 return retval;
288 static void adu_release_internal(struct adu_device *dev)
290 /* decrement our usage count for the device */
291 --dev->open_count;
292 dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
293 dev->open_count);
294 if (dev->open_count <= 0) {
295 adu_abort_transfers(dev);
296 dev->open_count = 0;
300 static int adu_release(struct inode *inode, struct file *file)
302 struct adu_device *dev;
303 int retval = 0;
305 if (file == NULL) {
306 retval = -ENODEV;
307 goto exit;
310 dev = file->private_data;
311 if (dev == NULL) {
312 retval = -ENODEV;
313 goto exit;
316 mutex_lock(&adutux_mutex); /* not interruptible */
318 if (dev->open_count <= 0) {
319 dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
320 retval = -ENODEV;
321 goto unlock;
324 adu_release_internal(dev);
325 if (dev->udev == NULL) {
326 /* the device was unplugged before the file was released */
327 if (!dev->open_count) /* ... and we're the last user */
328 adu_delete(dev);
330 unlock:
331 mutex_unlock(&adutux_mutex);
332 exit:
333 return retval;
336 static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
337 loff_t *ppos)
339 struct adu_device *dev;
340 size_t bytes_read = 0;
341 size_t bytes_to_read = count;
342 int i;
343 int retval = 0;
344 int timeout = 0;
345 int should_submit = 0;
346 unsigned long flags;
347 DECLARE_WAITQUEUE(wait, current);
349 dev = file->private_data;
350 if (mutex_lock_interruptible(&dev->mtx))
351 return -ERESTARTSYS;
353 /* verify that the device wasn't unplugged */
354 if (dev->udev == NULL) {
355 retval = -ENODEV;
356 pr_err("No device or device unplugged %d\n", retval);
357 goto exit;
360 /* verify that some data was requested */
361 if (count == 0) {
362 dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
363 __func__);
364 goto exit;
367 timeout = COMMAND_TIMEOUT;
368 dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
369 while (bytes_to_read) {
370 int data_in_secondary = dev->secondary_tail - dev->secondary_head;
371 dev_dbg(&dev->udev->dev,
372 "%s : while, data_in_secondary=%d, status=%d\n",
373 __func__, data_in_secondary,
374 dev->interrupt_in_urb->status);
376 if (data_in_secondary) {
377 /* drain secondary buffer */
378 int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
379 i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
380 if (i) {
381 retval = -EFAULT;
382 goto exit;
384 dev->secondary_head += (amount - i);
385 bytes_read += (amount - i);
386 bytes_to_read -= (amount - i);
387 } else {
388 /* we check the primary buffer */
389 spin_lock_irqsave (&dev->buflock, flags);
390 if (dev->read_buffer_length) {
391 /* we secure access to the primary */
392 char *tmp;
393 dev_dbg(&dev->udev->dev,
394 "%s : swap, read_buffer_length = %d\n",
395 __func__, dev->read_buffer_length);
396 tmp = dev->read_buffer_secondary;
397 dev->read_buffer_secondary = dev->read_buffer_primary;
398 dev->read_buffer_primary = tmp;
399 dev->secondary_head = 0;
400 dev->secondary_tail = dev->read_buffer_length;
401 dev->read_buffer_length = 0;
402 spin_unlock_irqrestore(&dev->buflock, flags);
403 /* we have a free buffer so use it */
404 should_submit = 1;
405 } else {
406 /* even the primary was empty - we may need to do IO */
407 if (!dev->read_urb_finished) {
408 /* somebody is doing IO */
409 spin_unlock_irqrestore(&dev->buflock, flags);
410 dev_dbg(&dev->udev->dev,
411 "%s : submitted already\n",
412 __func__);
413 } else {
414 /* we must initiate input */
415 dev_dbg(&dev->udev->dev,
416 "%s : initiate input\n",
417 __func__);
418 dev->read_urb_finished = 0;
419 spin_unlock_irqrestore(&dev->buflock, flags);
421 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
422 usb_rcvintpipe(dev->udev,
423 dev->interrupt_in_endpoint->bEndpointAddress),
424 dev->interrupt_in_buffer,
425 usb_endpoint_maxp(dev->interrupt_in_endpoint),
426 adu_interrupt_in_callback,
427 dev,
428 dev->interrupt_in_endpoint->bInterval);
429 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
430 if (retval) {
431 dev->read_urb_finished = 1;
432 if (retval == -ENOMEM) {
433 retval = bytes_read ? bytes_read : -ENOMEM;
435 dev_dbg(&dev->udev->dev,
436 "%s : submit failed\n",
437 __func__);
438 goto exit;
442 /* we wait for I/O to complete */
443 set_current_state(TASK_INTERRUPTIBLE);
444 add_wait_queue(&dev->read_wait, &wait);
445 spin_lock_irqsave(&dev->buflock, flags);
446 if (!dev->read_urb_finished) {
447 spin_unlock_irqrestore(&dev->buflock, flags);
448 timeout = schedule_timeout(COMMAND_TIMEOUT);
449 } else {
450 spin_unlock_irqrestore(&dev->buflock, flags);
451 set_current_state(TASK_RUNNING);
453 remove_wait_queue(&dev->read_wait, &wait);
455 if (timeout <= 0) {
456 dev_dbg(&dev->udev->dev,
457 "%s : timeout\n", __func__);
458 retval = bytes_read ? bytes_read : -ETIMEDOUT;
459 goto exit;
462 if (signal_pending(current)) {
463 dev_dbg(&dev->udev->dev,
464 "%s : signal pending\n",
465 __func__);
466 retval = bytes_read ? bytes_read : -EINTR;
467 goto exit;
473 retval = bytes_read;
474 /* if the primary buffer is empty then use it */
475 spin_lock_irqsave(&dev->buflock, flags);
476 if (should_submit && dev->read_urb_finished) {
477 dev->read_urb_finished = 0;
478 spin_unlock_irqrestore(&dev->buflock, flags);
479 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
480 usb_rcvintpipe(dev->udev,
481 dev->interrupt_in_endpoint->bEndpointAddress),
482 dev->interrupt_in_buffer,
483 usb_endpoint_maxp(dev->interrupt_in_endpoint),
484 adu_interrupt_in_callback,
485 dev,
486 dev->interrupt_in_endpoint->bInterval);
487 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
488 dev->read_urb_finished = 1;
489 /* we ignore failure */
490 } else {
491 spin_unlock_irqrestore(&dev->buflock, flags);
494 exit:
495 /* unlock the device */
496 mutex_unlock(&dev->mtx);
498 return retval;
501 static ssize_t adu_write(struct file *file, const __user char *buffer,
502 size_t count, loff_t *ppos)
504 DECLARE_WAITQUEUE(waita, current);
505 struct adu_device *dev;
506 size_t bytes_written = 0;
507 size_t bytes_to_write;
508 size_t buffer_size;
509 unsigned long flags;
510 int retval;
512 dev = file->private_data;
514 retval = mutex_lock_interruptible(&dev->mtx);
515 if (retval)
516 goto exit_nolock;
518 /* verify that the device wasn't unplugged */
519 if (dev->udev == NULL) {
520 retval = -ENODEV;
521 pr_err("No device or device unplugged %d\n", retval);
522 goto exit;
525 /* verify that we actually have some data to write */
526 if (count == 0) {
527 dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
528 __func__);
529 goto exit;
532 while (count > 0) {
533 add_wait_queue(&dev->write_wait, &waita);
534 set_current_state(TASK_INTERRUPTIBLE);
535 spin_lock_irqsave(&dev->buflock, flags);
536 if (!dev->out_urb_finished) {
537 spin_unlock_irqrestore(&dev->buflock, flags);
539 mutex_unlock(&dev->mtx);
540 if (signal_pending(current)) {
541 dev_dbg(&dev->udev->dev, "%s : interrupted\n",
542 __func__);
543 set_current_state(TASK_RUNNING);
544 retval = -EINTR;
545 goto exit_onqueue;
547 if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
548 dev_dbg(&dev->udev->dev,
549 "%s - command timed out.\n", __func__);
550 retval = -ETIMEDOUT;
551 goto exit_onqueue;
553 remove_wait_queue(&dev->write_wait, &waita);
554 retval = mutex_lock_interruptible(&dev->mtx);
555 if (retval) {
556 retval = bytes_written ? bytes_written : retval;
557 goto exit_nolock;
560 dev_dbg(&dev->udev->dev,
561 "%s : in progress, count = %zd\n",
562 __func__, count);
563 } else {
564 spin_unlock_irqrestore(&dev->buflock, flags);
565 set_current_state(TASK_RUNNING);
566 remove_wait_queue(&dev->write_wait, &waita);
567 dev_dbg(&dev->udev->dev, "%s : sending, count = %zd\n",
568 __func__, count);
570 /* write the data into interrupt_out_buffer from userspace */
571 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
572 bytes_to_write = count > buffer_size ? buffer_size : count;
573 dev_dbg(&dev->udev->dev,
574 "%s : buffer_size = %zd, count = %zd, bytes_to_write = %zd\n",
575 __func__, buffer_size, count, bytes_to_write);
577 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
578 retval = -EFAULT;
579 goto exit;
582 /* send off the urb */
583 usb_fill_int_urb(
584 dev->interrupt_out_urb,
585 dev->udev,
586 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
587 dev->interrupt_out_buffer,
588 bytes_to_write,
589 adu_interrupt_out_callback,
590 dev,
591 dev->interrupt_out_endpoint->bInterval);
592 dev->interrupt_out_urb->actual_length = bytes_to_write;
593 dev->out_urb_finished = 0;
594 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
595 if (retval < 0) {
596 dev->out_urb_finished = 1;
597 dev_err(&dev->udev->dev, "Couldn't submit "
598 "interrupt_out_urb %d\n", retval);
599 goto exit;
602 buffer += bytes_to_write;
603 count -= bytes_to_write;
605 bytes_written += bytes_to_write;
608 mutex_unlock(&dev->mtx);
609 return bytes_written;
611 exit:
612 mutex_unlock(&dev->mtx);
613 exit_nolock:
614 return retval;
616 exit_onqueue:
617 remove_wait_queue(&dev->write_wait, &waita);
618 return retval;
621 /* file operations needed when we register this driver */
622 static const struct file_operations adu_fops = {
623 .owner = THIS_MODULE,
624 .read = adu_read,
625 .write = adu_write,
626 .open = adu_open,
627 .release = adu_release,
628 .llseek = noop_llseek,
632 * usb class driver info in order to get a minor number from the usb core,
633 * and to have the device registered with devfs and the driver core
635 static struct usb_class_driver adu_class = {
636 .name = "usb/adutux%d",
637 .fops = &adu_fops,
638 .minor_base = ADU_MINOR_BASE,
642 * adu_probe
644 * Called by the usb core when a new device is connected that it thinks
645 * this driver might be interested in.
647 static int adu_probe(struct usb_interface *interface,
648 const struct usb_device_id *id)
650 struct usb_device *udev = interface_to_usbdev(interface);
651 struct adu_device *dev = NULL;
652 int retval = -ENOMEM;
653 int in_end_size;
654 int out_end_size;
655 int res;
657 /* allocate memory for our device state and initialize it */
658 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
659 if (!dev)
660 return -ENOMEM;
662 mutex_init(&dev->mtx);
663 spin_lock_init(&dev->buflock);
664 dev->udev = udev;
665 init_waitqueue_head(&dev->read_wait);
666 init_waitqueue_head(&dev->write_wait);
668 res = usb_find_common_endpoints_reverse(&interface->altsetting[0],
669 NULL, NULL,
670 &dev->interrupt_in_endpoint,
671 &dev->interrupt_out_endpoint);
672 if (res) {
673 dev_err(&interface->dev, "interrupt endpoints not found\n");
674 retval = res;
675 goto error;
678 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
679 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
681 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
682 if (!dev->read_buffer_primary)
683 goto error;
685 /* debug code prime the buffer */
686 memset(dev->read_buffer_primary, 'a', in_end_size);
687 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
688 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
689 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
691 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
692 if (!dev->read_buffer_secondary)
693 goto error;
695 /* debug code prime the buffer */
696 memset(dev->read_buffer_secondary, 'e', in_end_size);
697 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
698 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
699 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
701 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
702 if (!dev->interrupt_in_buffer)
703 goto error;
705 /* debug code prime the buffer */
706 memset(dev->interrupt_in_buffer, 'i', in_end_size);
708 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
709 if (!dev->interrupt_in_urb)
710 goto error;
711 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
712 if (!dev->interrupt_out_buffer)
713 goto error;
714 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
715 if (!dev->interrupt_out_urb)
716 goto error;
718 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
719 sizeof(dev->serial_number))) {
720 dev_err(&interface->dev, "Could not retrieve serial number\n");
721 retval = -EIO;
722 goto error;
724 dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
726 /* we can register the device now, as it is ready */
727 usb_set_intfdata(interface, dev);
729 retval = usb_register_dev(interface, &adu_class);
731 if (retval) {
732 /* something prevented us from registering this driver */
733 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
734 usb_set_intfdata(interface, NULL);
735 goto error;
738 dev->minor = interface->minor;
740 /* let the user know what node this device is now attached to */
741 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
742 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
743 (dev->minor - ADU_MINOR_BASE));
745 return 0;
747 error:
748 adu_delete(dev);
749 return retval;
753 * adu_disconnect
755 * Called by the usb core when the device is removed from the system.
757 static void adu_disconnect(struct usb_interface *interface)
759 struct adu_device *dev;
761 dev = usb_get_intfdata(interface);
763 mutex_lock(&dev->mtx); /* not interruptible */
764 dev->udev = NULL; /* poison */
765 usb_deregister_dev(interface, &adu_class);
766 mutex_unlock(&dev->mtx);
768 mutex_lock(&adutux_mutex);
769 usb_set_intfdata(interface, NULL);
771 /* if the device is not opened, then we clean up right now */
772 if (!dev->open_count)
773 adu_delete(dev);
775 mutex_unlock(&adutux_mutex);
778 /* usb specific object needed to register this driver with the usb subsystem */
779 static struct usb_driver adu_driver = {
780 .name = "adutux",
781 .probe = adu_probe,
782 .disconnect = adu_disconnect,
783 .id_table = device_table,
786 module_usb_driver(adu_driver);
788 MODULE_AUTHOR(DRIVER_AUTHOR);
789 MODULE_DESCRIPTION(DRIVER_DESC);
790 MODULE_LICENSE("GPL");