Merge master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6] / drivers / usb / misc / adutux.c
1 /*
2  * adutux - driver for ADU devices from Ontrak Control Systems
3  * This is an experimental driver. Use at your own risk.
4  * This driver is not supported by Ontrak Control Systems.
5  *
6  * Copyright (c) 2003 John Homppi (SCO, leave this notice here)
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 as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * derived from the Lego USB Tower driver 0.56:
14  * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net>
15  *               2001 Juergen Stuber <stuber@loria.fr>
16  * that was derived from USB Skeleton driver - 0.5
17  * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
18  *
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/usb.h>
27 #include <asm/uaccess.h>
28
29 #ifdef CONFIG_USB_DEBUG
30 static int debug = 5;
31 #else
32 static int debug = 1;
33 #endif
34
35 /* Use our own dbg macro */
36 #undef dbg
37 #define dbg(lvl, format, arg...)                                        \
38 do {                                                                    \
39         if (debug >= lvl)                                               \
40                 printk(KERN_DEBUG __FILE__ " : " format " \n", ## arg); \
41 } while (0)
42
43
44 /* Version Information */
45 #define DRIVER_VERSION "v0.0.13"
46 #define DRIVER_AUTHOR "John Homppi"
47 #define DRIVER_DESC "adutux (see www.ontrak.net)"
48
49 /* Module parameters */
50 module_param(debug, int, S_IRUGO | S_IWUSR);
51 MODULE_PARM_DESC(debug, "Debug enabled or not");
52
53 /* Define these values to match your device */
54 #define ADU_VENDOR_ID 0x0a07
55 #define ADU_PRODUCT_ID 0x0064
56
57 /* table of devices that work with this driver */
58 static struct usb_device_id device_table [] = {
59         { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) },          /* ADU100 */
60         { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) },       /* ADU120 */
61         { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) },       /* ADU130 */
62         { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) },      /* ADU200 */
63         { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) },      /* ADU208 */
64         { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) },      /* ADU218 */
65         { }/* Terminating entry */
66 };
67
68 MODULE_DEVICE_TABLE(usb, device_table);
69
70 #ifdef CONFIG_USB_DYNAMIC_MINORS
71 #define ADU_MINOR_BASE  0
72 #else
73 #define ADU_MINOR_BASE  67
74 #endif
75
76 /* we can have up to this number of device plugged in at once */
77 #define MAX_DEVICES     16
78
79 #define COMMAND_TIMEOUT (2*HZ)  /* 60 second timeout for a command */
80
81 /* Structure to hold all of our device specific stuff */
82 struct adu_device {
83         struct semaphore        sem; /* locks this structure */
84         struct usb_device*      udev; /* save off the usb device pointer */
85         struct usb_interface*   interface;
86         unsigned char           minor; /* the starting minor number for this device */
87         char                    serial_number[8];
88
89         int                     open_count; /* number of times this port has been opened */
90
91         char*                   read_buffer_primary;
92         int                     read_buffer_length;
93         char*                   read_buffer_secondary;
94         int                     secondary_head;
95         int                     secondary_tail;
96         spinlock_t              buflock;
97
98         wait_queue_head_t       read_wait;
99         wait_queue_head_t       write_wait;
100
101         char*                   interrupt_in_buffer;
102         struct usb_endpoint_descriptor* interrupt_in_endpoint;
103         struct urb*             interrupt_in_urb;
104         int                     read_urb_finished;
105
106         char*                   interrupt_out_buffer;
107         struct usb_endpoint_descriptor* interrupt_out_endpoint;
108         struct urb*             interrupt_out_urb;
109 };
110
111 static struct usb_driver adu_driver;
112
113 static void adu_debug_data(int level, const char *function, int size,
114                            const unsigned char *data)
115 {
116         int i;
117
118         if (debug < level)
119                 return;
120
121         printk(KERN_DEBUG __FILE__": %s - length = %d, data = ",
122                function, size);
123         for (i = 0; i < size; ++i)
124                 printk("%.2x ", data[i]);
125         printk("\n");
126 }
127
128 /**
129  * adu_abort_transfers
130  *      aborts transfers and frees associated data structures
131  */
132 static void adu_abort_transfers(struct adu_device *dev)
133 {
134         dbg(2," %s : enter", __FUNCTION__);
135
136         if (dev == NULL) {
137                 dbg(1," %s : dev is null", __FUNCTION__);
138                 goto exit;
139         }
140
141         if (dev->udev == NULL) {
142                 dbg(1," %s : udev is null", __FUNCTION__);
143                 goto exit;
144         }
145
146         dbg(2," %s : udev state %d", __FUNCTION__, dev->udev->state);
147         if (dev->udev->state == USB_STATE_NOTATTACHED) {
148                 dbg(1," %s : udev is not attached", __FUNCTION__);
149                 goto exit;
150         }
151
152         /* shutdown transfer */
153         usb_unlink_urb(dev->interrupt_in_urb);
154         usb_unlink_urb(dev->interrupt_out_urb);
155
156 exit:
157         dbg(2," %s : leave", __FUNCTION__);
158 }
159
160 static void adu_delete(struct adu_device *dev)
161 {
162         dbg(2, "%s enter", __FUNCTION__);
163
164         adu_abort_transfers(dev);
165
166         /* free data structures */
167         usb_free_urb(dev->interrupt_in_urb);
168         usb_free_urb(dev->interrupt_out_urb);
169         kfree(dev->read_buffer_primary);
170         kfree(dev->read_buffer_secondary);
171         kfree(dev->interrupt_in_buffer);
172         kfree(dev->interrupt_out_buffer);
173         kfree(dev);
174
175         dbg(2, "%s : leave", __FUNCTION__);
176 }
177
178 static void adu_interrupt_in_callback(struct urb *urb)
179 {
180         struct adu_device *dev = urb->context;
181
182         dbg(4," %s : enter, status %d", __FUNCTION__, urb->status);
183         adu_debug_data(5, __FUNCTION__, urb->actual_length,
184                        urb->transfer_buffer);
185
186         spin_lock(&dev->buflock);
187
188         if (urb->status != 0) {
189                 if ((urb->status != -ENOENT) && (urb->status != -ECONNRESET)) {
190                         dbg(1," %s : nonzero status received: %d",
191                             __FUNCTION__, urb->status);
192                 }
193                 goto exit;
194         }
195
196         if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
197                 if (dev->read_buffer_length <
198                     (4 * le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize)) -
199                      (urb->actual_length)) {
200                         memcpy (dev->read_buffer_primary +
201                                 dev->read_buffer_length,
202                                 dev->interrupt_in_buffer, urb->actual_length);
203
204                         dev->read_buffer_length += urb->actual_length;
205                         dbg(2," %s reading  %d ", __FUNCTION__,
206                             urb->actual_length);
207                 } else {
208                         dbg(1," %s : read_buffer overflow", __FUNCTION__);
209                 }
210         }
211
212 exit:
213         dev->read_urb_finished = 1;
214         spin_unlock(&dev->buflock);
215         /* always wake up so we recover from errors */
216         wake_up_interruptible(&dev->read_wait);
217         adu_debug_data(5, __FUNCTION__, urb->actual_length,
218                        urb->transfer_buffer);
219         dbg(4," %s : leave, status %d", __FUNCTION__, urb->status);
220 }
221
222 static void adu_interrupt_out_callback(struct urb *urb)
223 {
224         struct adu_device *dev = urb->context;
225
226         dbg(4," %s : enter, status %d", __FUNCTION__, urb->status);
227         adu_debug_data(5,__FUNCTION__, urb->actual_length, urb->transfer_buffer);
228
229         if (urb->status != 0) {
230                 if ((urb->status != -ENOENT) &&
231                     (urb->status != -ECONNRESET)) {
232                         dbg(1, " %s :nonzero status received: %d",
233                             __FUNCTION__, urb->status);
234                 }
235                 goto exit;
236         }
237
238         wake_up_interruptible(&dev->write_wait);
239 exit:
240
241         adu_debug_data(5, __FUNCTION__, urb->actual_length,
242                        urb->transfer_buffer);
243         dbg(4," %s : leave, status %d", __FUNCTION__, urb->status);
244 }
245
246 static int adu_open(struct inode *inode, struct file *file)
247 {
248         struct adu_device *dev = NULL;
249         struct usb_interface *interface;
250         int subminor;
251         int retval = 0;
252
253         dbg(2,"%s : enter", __FUNCTION__);
254
255         subminor = iminor(inode);
256
257         interface = usb_find_interface(&adu_driver, subminor);
258         if (!interface) {
259                 err("%s - error, can't find device for minor %d",
260                     __FUNCTION__, subminor);
261                 retval = -ENODEV;
262                 goto exit_no_device;
263         }
264
265         dev = usb_get_intfdata(interface);
266         if (!dev) {
267                 retval = -ENODEV;
268                 goto exit_no_device;
269         }
270
271         /* lock this device */
272         if ((retval = down_interruptible(&dev->sem))) {
273                 dbg(2, "%s : sem down failed", __FUNCTION__);
274                 goto exit_no_device;
275         }
276
277         /* increment our usage count for the device */
278         ++dev->open_count;
279         dbg(2,"%s : open count %d", __FUNCTION__, dev->open_count);
280
281         /* save device in the file's private structure */
282         file->private_data = dev;
283
284         if (dev->open_count == 1) {
285                 /* initialize in direction */
286                 dev->read_buffer_length = 0;
287
288                 /* fixup first read by having urb waiting for it */
289                 usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
290                                  usb_rcvintpipe(dev->udev,
291                                                 dev->interrupt_in_endpoint->bEndpointAddress),
292                                  dev->interrupt_in_buffer,
293                                  le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
294                                  adu_interrupt_in_callback, dev,
295                                  dev->interrupt_in_endpoint->bInterval);
296                 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
297                 dev->read_urb_finished = 0;
298                 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
299                 if (retval)
300                         --dev->open_count;
301         }
302         up(&dev->sem);
303
304 exit_no_device:
305         dbg(2,"%s : leave, return value %d ", __FUNCTION__, retval);
306
307         return retval;
308 }
309
310 static int adu_release_internal(struct adu_device *dev)
311 {
312         int retval = 0;
313
314         dbg(2," %s : enter", __FUNCTION__);
315
316         /* decrement our usage count for the device */
317         --dev->open_count;
318         dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);
319         if (dev->open_count <= 0) {
320                 adu_abort_transfers(dev);
321                 dev->open_count = 0;
322         }
323
324         dbg(2," %s : leave", __FUNCTION__);
325         return retval;
326 }
327
328 static int adu_release(struct inode *inode, struct file *file)
329 {
330         struct adu_device *dev = NULL;
331         int retval = 0;
332
333         dbg(2," %s : enter", __FUNCTION__);
334
335         if (file == NULL) {
336                 dbg(1," %s : file is NULL", __FUNCTION__);
337                 retval = -ENODEV;
338                 goto exit;
339         }
340
341         dev = file->private_data;
342
343         if (dev == NULL) {
344                 dbg(1," %s : object is NULL", __FUNCTION__);
345                 retval = -ENODEV;
346                 goto exit;
347         }
348
349         /* lock our device */
350         down(&dev->sem); /* not interruptible */
351
352         if (dev->open_count <= 0) {
353                 dbg(1," %s : device not opened", __FUNCTION__);
354                 retval = -ENODEV;
355                 goto exit;
356         }
357
358         if (dev->udev == NULL) {
359                 /* the device was unplugged before the file was released */
360                 up(&dev->sem);
361                 adu_delete(dev);
362                 dev = NULL;
363         } else {
364                 /* do the work */
365                 retval = adu_release_internal(dev);
366         }
367
368 exit:
369         if (dev)
370                 up(&dev->sem);
371         dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
372         return retval;
373 }
374
375 static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
376                         loff_t *ppos)
377 {
378         struct adu_device *dev;
379         size_t bytes_read = 0;
380         size_t bytes_to_read = count;
381         int i;
382         int retval = 0;
383         int timeout = 0;
384         int should_submit = 0;
385         unsigned long flags;
386         DECLARE_WAITQUEUE(wait, current);
387
388         dbg(2," %s : enter, count = %Zd, file=%p", __FUNCTION__, count, file);
389
390         dev = file->private_data;
391         dbg(2," %s : dev=%p", __FUNCTION__, dev);
392         /* lock this object */
393         if (down_interruptible(&dev->sem))
394                 return -ERESTARTSYS;
395
396         /* verify that the device wasn't unplugged */
397         if (dev->udev == NULL || dev->minor == 0) {
398                 retval = -ENODEV;
399                 err("No device or device unplugged %d", retval);
400                 goto exit;
401         }
402
403         /* verify that some data was requested */
404         if (count == 0) {
405                 dbg(1," %s : read request of 0 bytes", __FUNCTION__);
406                 goto exit;
407         }
408
409         timeout = COMMAND_TIMEOUT;
410         dbg(2," %s : about to start looping", __FUNCTION__);
411         while (bytes_to_read) {
412                 int data_in_secondary = dev->secondary_tail - dev->secondary_head;
413                 dbg(2," %s : while, data_in_secondary=%d, status=%d",
414                     __FUNCTION__, data_in_secondary,
415                     dev->interrupt_in_urb->status);
416
417                 if (data_in_secondary) {
418                         /* drain secondary buffer */
419                         int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
420                         i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
421                         if (i < 0) {
422                                 retval = -EFAULT;
423                                 goto exit;
424                         }
425                         dev->secondary_head += (amount - i);
426                         bytes_read += (amount - i);
427                         bytes_to_read -= (amount - i);
428                         if (i) {
429                                 retval = bytes_read ? bytes_read : -EFAULT;
430                                 goto exit;
431                         }
432                 } else {
433                         /* we check the primary buffer */
434                         spin_lock_irqsave (&dev->buflock, flags);
435                         if (dev->read_buffer_length) {
436                                 /* we secure access to the primary */
437                                 char *tmp;
438                                 dbg(2," %s : swap, read_buffer_length = %d",
439                                     __FUNCTION__, dev->read_buffer_length);
440                                 tmp = dev->read_buffer_secondary;
441                                 dev->read_buffer_secondary = dev->read_buffer_primary;
442                                 dev->read_buffer_primary = tmp;
443                                 dev->secondary_head = 0;
444                                 dev->secondary_tail = dev->read_buffer_length;
445                                 dev->read_buffer_length = 0;
446                                 spin_unlock_irqrestore(&dev->buflock, flags);
447                                 /* we have a free buffer so use it */
448                                 should_submit = 1;
449                         } else {
450                                 /* even the primary was empty - we may need to do IO */
451                                 if (dev->interrupt_in_urb->status == -EINPROGRESS) {
452                                         /* somebody is doing IO */
453                                         spin_unlock_irqrestore(&dev->buflock, flags);
454                                         dbg(2," %s : submitted already", __FUNCTION__);
455                                 } else {
456                                         /* we must initiate input */
457                                         dbg(2," %s : initiate input", __FUNCTION__);
458                                         dev->read_urb_finished = 0;
459
460                                         usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
461                                                          usb_rcvintpipe(dev->udev,
462                                                                         dev->interrupt_in_endpoint->bEndpointAddress),
463                                                          dev->interrupt_in_buffer,
464                                                          le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
465                                                          adu_interrupt_in_callback,
466                                                          dev,
467                                                          dev->interrupt_in_endpoint->bInterval);
468                                         retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
469                                         if (!retval) {
470                                                 spin_unlock_irqrestore(&dev->buflock, flags);
471                                                 dbg(2," %s : submitted OK", __FUNCTION__);
472                                         } else {
473                                                 if (retval == -ENOMEM) {
474                                                         retval = bytes_read ? bytes_read : -ENOMEM;
475                                                 }
476                                                 spin_unlock_irqrestore(&dev->buflock, flags);
477                                                 dbg(2," %s : submit failed", __FUNCTION__);
478                                                 goto exit;
479                                         }
480                                 }
481
482                                 /* we wait for I/O to complete */
483                                 set_current_state(TASK_INTERRUPTIBLE);
484                                 add_wait_queue(&dev->read_wait, &wait);
485                                 if (!dev->read_urb_finished)
486                                         timeout = schedule_timeout(COMMAND_TIMEOUT);
487                                 else
488                                         set_current_state(TASK_RUNNING);
489                                 remove_wait_queue(&dev->read_wait, &wait);
490
491                                 if (timeout <= 0) {
492                                         dbg(2," %s : timeout", __FUNCTION__);
493                                         retval = bytes_read ? bytes_read : -ETIMEDOUT;
494                                         goto exit;
495                                 }
496
497                                 if (signal_pending(current)) {
498                                         dbg(2," %s : signal pending", __FUNCTION__);
499                                         retval = bytes_read ? bytes_read : -EINTR;
500                                         goto exit;
501                                 }
502                         }
503                 }
504         }
505
506         retval = bytes_read;
507         /* if the primary buffer is empty then use it */
508         if (should_submit && !dev->interrupt_in_urb->status==-EINPROGRESS) {
509                 usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
510                                  usb_rcvintpipe(dev->udev,
511                                                 dev->interrupt_in_endpoint->bEndpointAddress),
512                                                 dev->interrupt_in_buffer,
513                                                 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
514                                                 adu_interrupt_in_callback,
515                                                 dev,
516                                                 dev->interrupt_in_endpoint->bInterval);
517                 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
518                 dev->read_urb_finished = 0;
519                 usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
520                 /* we ignore failure */
521         }
522
523 exit:
524         /* unlock the device */
525         up(&dev->sem);
526
527         dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
528         return retval;
529 }
530
531 static ssize_t adu_write(struct file *file, const __user char *buffer,
532                          size_t count, loff_t *ppos)
533 {
534         struct adu_device *dev;
535         size_t bytes_written = 0;
536         size_t bytes_to_write;
537         size_t buffer_size;
538         int retval;
539         int timeout = 0;
540
541         dbg(2," %s : enter, count = %Zd", __FUNCTION__, count);
542
543         dev = file->private_data;
544
545         /* lock this object */
546         retval = down_interruptible(&dev->sem);
547         if (retval)
548                 goto exit_nolock;
549
550         /* verify that the device wasn't unplugged */
551         if (dev->udev == NULL || dev->minor == 0) {
552                 retval = -ENODEV;
553                 err("No device or device unplugged %d", retval);
554                 goto exit;
555         }
556
557         /* verify that we actually have some data to write */
558         if (count == 0) {
559                 dbg(1," %s : write request of 0 bytes", __FUNCTION__);
560                 goto exit;
561         }
562
563
564         while (count > 0) {
565                 if (dev->interrupt_out_urb->status == -EINPROGRESS) {
566                         timeout = COMMAND_TIMEOUT;
567
568                         while (timeout > 0) {
569                                 if (signal_pending(current)) {
570                                 dbg(1," %s : interrupted", __FUNCTION__);
571                                 retval = -EINTR;
572                                 goto exit;
573                         }
574                         up(&dev->sem);
575                         timeout = interruptible_sleep_on_timeout(&dev->write_wait, timeout);
576                         retval = down_interruptible(&dev->sem);
577                         if (retval) {
578                                 retval = bytes_written ? bytes_written : retval;
579                                 goto exit_nolock;
580                         }
581                         if (timeout > 0) {
582                                 break;
583                         }
584                         dbg(1," %s : interrupted timeout: %d", __FUNCTION__, timeout);
585                 }
586
587
588                 dbg(1," %s : final timeout: %d", __FUNCTION__, timeout);
589
590                 if (timeout == 0) {
591                         dbg(1, "%s - command timed out.", __FUNCTION__);
592                         retval = -ETIMEDOUT;
593                         goto exit;
594                 }
595
596                 dbg(4," %s : in progress, count = %Zd", __FUNCTION__, count);
597
598                 } else {
599                         dbg(4," %s : sending, count = %Zd", __FUNCTION__, count);
600
601                         /* write the data into interrupt_out_buffer from userspace */
602                         buffer_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize);
603                         bytes_to_write = count > buffer_size ? buffer_size : count;
604                         dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
605                             __FUNCTION__, buffer_size, count, bytes_to_write);
606
607                         if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
608                                 retval = -EFAULT;
609                                 goto exit;
610                         }
611
612                         /* send off the urb */
613                         usb_fill_int_urb(
614                                 dev->interrupt_out_urb,
615                                 dev->udev,
616                                 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
617                                 dev->interrupt_out_buffer,
618                                 bytes_to_write,
619                                 adu_interrupt_out_callback,
620                                 dev,
621                                 dev->interrupt_in_endpoint->bInterval);
622                         /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
623                         dev->interrupt_out_urb->actual_length = bytes_to_write;
624                         retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
625                         if (retval < 0) {
626                                 err("Couldn't submit interrupt_out_urb %d", retval);
627                                 goto exit;
628                         }
629
630                         buffer += bytes_to_write;
631                         count -= bytes_to_write;
632
633                         bytes_written += bytes_to_write;
634                 }
635         }
636
637         retval = bytes_written;
638
639 exit:
640         /* unlock the device */
641         up(&dev->sem);
642 exit_nolock:
643
644         dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
645
646         return retval;
647 }
648
649 /* file operations needed when we register this driver */
650 static const struct file_operations adu_fops = {
651         .owner = THIS_MODULE,
652         .read  = adu_read,
653         .write = adu_write,
654         .open = adu_open,
655         .release = adu_release,
656 };
657
658 /*
659  * usb class driver info in order to get a minor number from the usb core,
660  * and to have the device registered with devfs and the driver core
661  */
662 static struct usb_class_driver adu_class = {
663         .name = "usb/adutux%d",
664         .fops = &adu_fops,
665         .minor_base = ADU_MINOR_BASE,
666 };
667
668 /**
669  * adu_probe
670  *
671  * Called by the usb core when a new device is connected that it thinks
672  * this driver might be interested in.
673  */
674 static int adu_probe(struct usb_interface *interface,
675                      const struct usb_device_id *id)
676 {
677         struct usb_device *udev = interface_to_usbdev(interface);
678         struct adu_device *dev = NULL;
679         struct usb_host_interface *iface_desc;
680         struct usb_endpoint_descriptor *endpoint;
681         int retval = -ENODEV;
682         int in_end_size;
683         int out_end_size;
684         int i;
685
686         dbg(2," %s : enter", __FUNCTION__);
687
688         if (udev == NULL) {
689                 dev_err(&interface->dev, "udev is NULL.\n");
690                 goto exit;
691         }
692
693         /* allocate memory for our device state and intialize it */
694         dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
695         if (dev == NULL) {
696                 dev_err(&interface->dev, "Out of memory\n");
697                 retval = -ENOMEM;
698                 goto exit;
699         }
700
701         init_MUTEX(&dev->sem);
702         spin_lock_init(&dev->buflock);
703         dev->udev = udev;
704         init_waitqueue_head(&dev->read_wait);
705         init_waitqueue_head(&dev->write_wait);
706
707         iface_desc = &interface->altsetting[0];
708
709         /* set up the endpoint information */
710         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
711                 endpoint = &iface_desc->endpoint[i].desc;
712
713                 if (usb_endpoint_is_int_in(endpoint))
714                         dev->interrupt_in_endpoint = endpoint;
715
716                 if (usb_endpoint_is_int_out(endpoint))
717                         dev->interrupt_out_endpoint = endpoint;
718         }
719         if (dev->interrupt_in_endpoint == NULL) {
720                 dev_err(&interface->dev, "interrupt in endpoint not found\n");
721                 goto error;
722         }
723         if (dev->interrupt_out_endpoint == NULL) {
724                 dev_err(&interface->dev, "interrupt out endpoint not found\n");
725                 goto error;
726         }
727
728         in_end_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
729         out_end_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize);
730
731         dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
732         if (!dev->read_buffer_primary) {
733                 dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n");
734                 retval = -ENOMEM;
735                 goto error;
736         }
737
738         /* debug code prime the buffer */
739         memset(dev->read_buffer_primary, 'a', in_end_size);
740         memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
741         memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
742         memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
743
744         dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
745         if (!dev->read_buffer_secondary) {
746                 dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n");
747                 retval = -ENOMEM;
748                 goto error;
749         }
750
751         /* debug code prime the buffer */
752         memset(dev->read_buffer_secondary, 'e', in_end_size);
753         memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
754         memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
755         memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
756
757         dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
758         if (!dev->interrupt_in_buffer) {
759                 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
760                 goto error;
761         }
762
763         /* debug code prime the buffer */
764         memset(dev->interrupt_in_buffer, 'i', in_end_size);
765
766         dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
767         if (!dev->interrupt_in_urb) {
768                 dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
769                 goto error;
770         }
771         dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
772         if (!dev->interrupt_out_buffer) {
773                 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
774                 goto error;
775         }
776         dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
777         if (!dev->interrupt_out_urb) {
778                 dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n");
779                 goto error;
780         }
781
782         if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
783                         sizeof(dev->serial_number))) {
784                 dev_err(&interface->dev, "Could not retrieve serial number\n");
785                 goto error;
786         }
787         dbg(2," %s : serial_number=%s", __FUNCTION__, dev->serial_number);
788
789         /* we can register the device now, as it is ready */
790         usb_set_intfdata(interface, dev);
791
792         retval = usb_register_dev(interface, &adu_class);
793
794         if (retval) {
795                 /* something prevented us from registering this driver */
796                 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
797                 usb_set_intfdata(interface, NULL);
798                 goto error;
799         }
800
801         dev->minor = interface->minor;
802
803         /* let the user know what node this device is now attached to */
804         dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d",
805                  udev->descriptor.idProduct, dev->serial_number,
806                  (dev->minor - ADU_MINOR_BASE));
807 exit:
808         dbg(2," %s : leave, return value %p (dev)", __FUNCTION__, dev);
809
810         return retval;
811
812 error:
813         adu_delete(dev);
814         return retval;
815 }
816
817 /**
818  * adu_disconnect
819  *
820  * Called by the usb core when the device is removed from the system.
821  */
822 static void adu_disconnect(struct usb_interface *interface)
823 {
824         struct adu_device *dev;
825         int minor;
826
827         dbg(2," %s : enter", __FUNCTION__);
828
829         dev = usb_get_intfdata(interface);
830         usb_set_intfdata(interface, NULL);
831
832         minor = dev->minor;
833
834         /* give back our minor */
835         usb_deregister_dev(interface, &adu_class);
836         dev->minor = 0;
837
838         down(&dev->sem); /* not interruptible */
839
840         /* if the device is not opened, then we clean up right now */
841         dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);
842         if (!dev->open_count) {
843                 up(&dev->sem);
844                 adu_delete(dev);
845         } else {
846                 dev->udev = NULL;
847                 up(&dev->sem);
848         }
849
850         dev_info(&interface->dev, "ADU device adutux%d now disconnected",
851                  (minor - ADU_MINOR_BASE));
852
853         dbg(2," %s : leave", __FUNCTION__);
854 }
855
856 /* usb specific object needed to register this driver with the usb subsystem */
857 static struct usb_driver adu_driver = {
858         .name = "adutux",
859         .probe = adu_probe,
860         .disconnect = adu_disconnect,
861         .id_table = device_table,
862 };
863
864 static int __init adu_init(void)
865 {
866         int result;
867
868         dbg(2," %s : enter", __FUNCTION__);
869
870         /* register this driver with the USB subsystem */
871         result = usb_register(&adu_driver);
872         if (result < 0) {
873                 err("usb_register failed for the "__FILE__" driver. "
874                     "Error number %d", result);
875                 goto exit;
876         }
877
878         info("adutux " DRIVER_DESC " " DRIVER_VERSION);
879         info("adutux is an experimental driver. Use at your own risk");
880
881 exit:
882         dbg(2," %s : leave, return value %d", __FUNCTION__, result);
883
884         return result;
885 }
886
887 static void __exit adu_exit(void)
888 {
889         dbg(2," %s : enter", __FUNCTION__);
890         /* deregister this driver with the USB subsystem */
891         usb_deregister(&adu_driver);
892         dbg(2," %s : leave", __FUNCTION__);
893 }
894
895 module_init(adu_init);
896 module_exit(adu_exit);
897
898 MODULE_AUTHOR(DRIVER_AUTHOR);
899 MODULE_DESCRIPTION(DRIVER_DESC);
900 MODULE_LICENSE("GPL");