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