2 * LEGO USB Tower driver
4 * Copyright (C) 2003 David Glance <davidgsf@sourceforge.net>
5 * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * derived from USB Skeleton driver - 0.5
13 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
20 * - simplified buffering, one-shot URBs for writing
22 * - removed IOCTL (setting power/mode is more complicated, postponed)
24 * - added vendor commands for mode of operation and power level in open
26 * - set IR mode by default (by oversight 0.4 set VLL mode)
27 * 2002-01-11 - 0.5? pcchan
28 * - make read buffer reusable and work around bytes_to_write issue between
29 * uhci and legusbtower
30 * 2002-09-23 - 0.52 david (david@csse.uwa.edu.au)
31 * - imported into lejos project
32 * - changed wake_up to wake_up_interruptible
33 * - changed to use lego0 rather than tower0
34 * - changed dbg() to use __func__ rather than deprecated __func__
35 * 2003-01-12 - 0.53 david (david@csse.uwa.edu.au)
36 * - changed read and write to write everything or
37 * timeout (from a patch by Chris Riesen and Brett Thaeler driver)
38 * - added ioctl functionality to set timeouts
39 * 2003-07-18 - 0.54 davidgsf (david@csse.uwa.edu.au)
40 * - initial import into LegoUSB project
41 * - merge of existing LegoUSB.c driver
42 * 2003-07-18 - 0.56 davidgsf (david@csse.uwa.edu.au)
43 * - port to 2.6 style driver
44 * 2004-02-29 - 0.6 Juergen Stuber <starblue@users.sourceforge.net>
46 * - unlink read URBs which are no longer needed
47 * - allow increased buffer size, eliminates need for timeout on write
48 * - have read URB running continuously
51 * - added nonblocking I/O
52 * - changed back __func__ to __func__
53 * - read and log tower firmware version
54 * - reset tower on probe, avoids failure of first write
55 * 2004-03-09 - 0.7 Juergen Stuber <starblue@users.sourceforge.net>
56 * - timeout read now only after inactivity, shorten default accordingly
57 * 2004-03-11 - 0.8 Juergen Stuber <starblue@users.sourceforge.net>
58 * - log major, minor instead of possibly confusing device filename
59 * - whitespace cleanup
60 * 2004-03-12 - 0.9 Juergen Stuber <starblue@users.sourceforge.net>
61 * - normalize whitespace in debug messages
62 * - take care about endianness in control message responses
63 * 2004-03-13 - 0.91 Juergen Stuber <starblue@users.sourceforge.net>
64 * - make default intervals longer to accommodate current EHCI driver
65 * 2004-03-19 - 0.92 Juergen Stuber <starblue@users.sourceforge.net>
66 * - replaced atomic_t by memory barriers
67 * 2004-04-21 - 0.93 Juergen Stuber <starblue@users.sourceforge.net>
68 * - wait for completion of write urb in release (needed for remotecontrol)
69 * - corrected poll for write direction (missing negation)
70 * 2004-04-22 - 0.94 Juergen Stuber <starblue@users.sourceforge.net>
71 * - make device locking interruptible
72 * 2004-04-30 - 0.95 Juergen Stuber <starblue@users.sourceforge.net>
73 * - check for valid udev on resubmitting and unlinking urbs
74 * 2004-08-03 - 0.96 Juergen Stuber <starblue@users.sourceforge.net>
75 * - move reset into open to clean out spurious data
78 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
80 #include <linux/kernel.h>
81 #include <linux/errno.h>
82 #include <linux/init.h>
83 #include <linux/slab.h>
84 #include <linux/module.h>
85 #include <linux/completion.h>
86 #include <linux/mutex.h>
87 #include <asm/uaccess.h>
88 #include <linux/usb.h>
89 #include <linux/poll.h>
92 /* Version Information */
93 #define DRIVER_VERSION "v0.96"
94 #define DRIVER_AUTHOR "Juergen Stuber <starblue@sourceforge.net>"
95 #define DRIVER_DESC "LEGO USB Tower Driver"
98 /* The defaults are chosen to work with the latest versions of leJOS and NQC.
101 /* Some legacy software likes to receive packets in one piece.
102 * In this case read_buffer_size should exceed the maximal packet length
103 * (417 for datalog uploads), and packet_timeout should be set.
105 static int read_buffer_size = 480;
106 module_param(read_buffer_size, int, 0);
107 MODULE_PARM_DESC(read_buffer_size, "Read buffer size");
109 /* Some legacy software likes to send packets in one piece.
110 * In this case write_buffer_size should exceed the maximal packet length
111 * (417 for firmware and program downloads).
112 * A problem with long writes is that the following read may time out
113 * if the software is not prepared to wait long enough.
115 static int write_buffer_size = 480;
116 module_param(write_buffer_size, int, 0);
117 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
119 /* Some legacy software expects reads to contain whole LASM packets.
120 * To achieve this, characters which arrive before a packet timeout
121 * occurs will be returned in a single read operation.
122 * A problem with long reads is that the software may time out
123 * if it is not prepared to wait long enough.
124 * The packet timeout should be greater than the time between the
125 * reception of subsequent characters, which should arrive about
126 * every 5ms for the standard 2400 baud.
127 * Set it to 0 to disable.
129 static int packet_timeout = 50;
130 module_param(packet_timeout, int, 0);
131 MODULE_PARM_DESC(packet_timeout, "Packet timeout in ms");
133 /* Some legacy software expects blocking reads to time out.
134 * Timeout occurs after the specified time of read and write inactivity.
135 * Set it to 0 to disable.
137 static int read_timeout = 200;
138 module_param(read_timeout, int, 0);
139 MODULE_PARM_DESC(read_timeout, "Read timeout in ms");
141 /* As of kernel version 2.6.4 ehci-hcd uses an
142 * "only one interrupt transfer per frame" shortcut
143 * to simplify the scheduling of periodic transfers.
144 * This conflicts with our standard 1ms intervals for in and out URBs.
145 * We use default intervals of 2ms for in and 8ms for out transfers,
146 * which is fast enough for 2400 baud and allows a small additional load.
147 * Increase the interval to allow more devices that do interrupt transfers,
148 * or set to 0 to use the standard interval from the endpoint descriptors.
150 static int interrupt_in_interval = 2;
151 module_param(interrupt_in_interval, int, 0);
152 MODULE_PARM_DESC(interrupt_in_interval, "Interrupt in interval in ms");
154 static int interrupt_out_interval = 8;
155 module_param(interrupt_out_interval, int, 0);
156 MODULE_PARM_DESC(interrupt_out_interval, "Interrupt out interval in ms");
158 /* Define these values to match your device */
159 #define LEGO_USB_TOWER_VENDOR_ID 0x0694
160 #define LEGO_USB_TOWER_PRODUCT_ID 0x0001
162 /* Vendor requests */
163 #define LEGO_USB_TOWER_REQUEST_RESET 0x04
164 #define LEGO_USB_TOWER_REQUEST_GET_VERSION 0xFD
166 struct tower_reset_reply {
167 __le16 size; /* little-endian */
170 } __attribute__ ((packed));
172 struct tower_get_version_reply {
173 __le16 size; /* little-endian */
178 __le16 build_no; /* little-endian */
179 } __attribute__ ((packed));
182 /* table of devices that work with this driver */
183 static const struct usb_device_id tower_table[] = {
184 { USB_DEVICE(LEGO_USB_TOWER_VENDOR_ID, LEGO_USB_TOWER_PRODUCT_ID) },
185 { } /* Terminating entry */
188 MODULE_DEVICE_TABLE (usb, tower_table);
189 static DEFINE_MUTEX(open_disc_mutex);
191 #define LEGO_USB_TOWER_MINOR_BASE 160
194 /* Structure to hold all of our device specific stuff */
195 struct lego_usb_tower {
196 struct mutex lock; /* locks this structure */
197 struct usb_device* udev; /* save off the usb device pointer */
198 unsigned char minor; /* the starting minor number for this device */
200 int open_count; /* number of times this port has been opened */
203 size_t read_buffer_length; /* this much came in */
204 size_t read_packet_length; /* this much will be returned on read */
205 spinlock_t read_buffer_lock;
206 int packet_timeout_jiffies;
207 unsigned long read_last_arrival;
209 wait_queue_head_t read_wait;
210 wait_queue_head_t write_wait;
212 char* interrupt_in_buffer;
213 struct usb_endpoint_descriptor* interrupt_in_endpoint;
214 struct urb* interrupt_in_urb;
215 int interrupt_in_interval;
216 int interrupt_in_running;
217 int interrupt_in_done;
219 char* interrupt_out_buffer;
220 struct usb_endpoint_descriptor* interrupt_out_endpoint;
221 struct urb* interrupt_out_urb;
222 int interrupt_out_interval;
223 int interrupt_out_busy;
228 /* local function prototypes */
229 static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos);
230 static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
231 static inline void tower_delete (struct lego_usb_tower *dev);
232 static int tower_open (struct inode *inode, struct file *file);
233 static int tower_release (struct inode *inode, struct file *file);
234 static unsigned int tower_poll (struct file *file, poll_table *wait);
235 static loff_t tower_llseek (struct file *file, loff_t off, int whence);
237 static void tower_abort_transfers (struct lego_usb_tower *dev);
238 static void tower_check_for_read_packet (struct lego_usb_tower *dev);
239 static void tower_interrupt_in_callback (struct urb *urb);
240 static void tower_interrupt_out_callback (struct urb *urb);
242 static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id);
243 static void tower_disconnect (struct usb_interface *interface);
246 /* file operations needed when we register this driver */
247 static const struct file_operations tower_fops = {
248 .owner = THIS_MODULE,
250 .write = tower_write,
252 .release = tower_release,
254 .llseek = tower_llseek,
257 static char *legousbtower_devnode(struct device *dev, umode_t *mode)
259 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
263 * usb class driver info in order to get a minor number from the usb core,
264 * and to have the device registered with the driver core
266 static struct usb_class_driver tower_class = {
267 .name = "legousbtower%d",
268 .devnode = legousbtower_devnode,
270 .minor_base = LEGO_USB_TOWER_MINOR_BASE,
274 /* usb specific object needed to register this driver with the usb subsystem */
275 static struct usb_driver tower_driver = {
276 .name = "legousbtower",
277 .probe = tower_probe,
278 .disconnect = tower_disconnect,
279 .id_table = tower_table,
284 * lego_usb_tower_debug_data
286 static inline void lego_usb_tower_debug_data(struct device *dev,
287 const char *function, int size,
288 const unsigned char *data)
290 dev_dbg(dev, "%s - length = %d, data = %*ph\n",
291 function, size, size, data);
298 static inline void tower_delete (struct lego_usb_tower *dev)
300 tower_abort_transfers (dev);
302 /* free data structures */
303 usb_free_urb(dev->interrupt_in_urb);
304 usb_free_urb(dev->interrupt_out_urb);
305 kfree (dev->read_buffer);
306 kfree (dev->interrupt_in_buffer);
307 kfree (dev->interrupt_out_buffer);
315 static int tower_open (struct inode *inode, struct file *file)
317 struct lego_usb_tower *dev = NULL;
320 struct usb_interface *interface;
321 struct tower_reset_reply reset_reply;
324 nonseekable_open(inode, file);
325 subminor = iminor(inode);
327 interface = usb_find_interface (&tower_driver, subminor);
330 pr_err("error, can't find device for minor %d\n", subminor);
335 mutex_lock(&open_disc_mutex);
336 dev = usb_get_intfdata(interface);
339 mutex_unlock(&open_disc_mutex);
344 /* lock this device */
345 if (mutex_lock_interruptible(&dev->lock)) {
346 mutex_unlock(&open_disc_mutex);
347 retval = -ERESTARTSYS;
352 /* allow opening only once */
353 if (dev->open_count) {
354 mutex_unlock(&open_disc_mutex);
359 mutex_unlock(&open_disc_mutex);
361 /* reset the tower */
362 result = usb_control_msg (dev->udev,
363 usb_rcvctrlpipe(dev->udev, 0),
364 LEGO_USB_TOWER_REQUEST_RESET,
365 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
372 dev_err(&dev->udev->dev,
373 "LEGO USB Tower reset control request failed\n");
378 /* initialize in direction */
379 dev->read_buffer_length = 0;
380 dev->read_packet_length = 0;
381 usb_fill_int_urb (dev->interrupt_in_urb,
383 usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress),
384 dev->interrupt_in_buffer,
385 usb_endpoint_maxp(dev->interrupt_in_endpoint),
386 tower_interrupt_in_callback,
388 dev->interrupt_in_interval);
390 dev->interrupt_in_running = 1;
391 dev->interrupt_in_done = 0;
394 retval = usb_submit_urb (dev->interrupt_in_urb, GFP_KERNEL);
396 dev_err(&dev->udev->dev,
397 "Couldn't submit interrupt_in_urb %d\n", retval);
398 dev->interrupt_in_running = 0;
403 /* save device in the file's private structure */
404 file->private_data = dev;
407 mutex_unlock(&dev->lock);
416 static int tower_release (struct inode *inode, struct file *file)
418 struct lego_usb_tower *dev;
421 dev = file->private_data;
428 mutex_lock(&open_disc_mutex);
429 if (mutex_lock_interruptible(&dev->lock)) {
430 retval = -ERESTARTSYS;
434 if (dev->open_count != 1) {
435 dev_dbg(&dev->udev->dev, "%s: device not opened exactly once\n",
440 if (dev->udev == NULL) {
441 /* the device was unplugged before the file was released */
443 /* unlock here as tower_delete frees dev */
444 mutex_unlock(&dev->lock);
449 /* wait until write transfer is finished */
450 if (dev->interrupt_out_busy) {
451 wait_event_interruptible_timeout (dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
453 tower_abort_transfers (dev);
457 mutex_unlock(&dev->lock);
460 mutex_unlock(&open_disc_mutex);
467 * tower_abort_transfers
468 * aborts transfers and frees associated data structures
470 static void tower_abort_transfers (struct lego_usb_tower *dev)
475 /* shutdown transfer */
476 if (dev->interrupt_in_running) {
477 dev->interrupt_in_running = 0;
480 usb_kill_urb (dev->interrupt_in_urb);
482 if (dev->interrupt_out_busy && dev->udev)
483 usb_kill_urb(dev->interrupt_out_urb);
488 * tower_check_for_read_packet
490 * To get correct semantics for signals and non-blocking I/O
491 * with packetizing we pretend not to see any data in the read buffer
492 * until it has been there unchanged for at least
493 * dev->packet_timeout_jiffies, or until the buffer is full.
495 static void tower_check_for_read_packet (struct lego_usb_tower *dev)
497 spin_lock_irq (&dev->read_buffer_lock);
499 || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies)
500 || dev->read_buffer_length == read_buffer_size) {
501 dev->read_packet_length = dev->read_buffer_length;
503 dev->interrupt_in_done = 0;
504 spin_unlock_irq (&dev->read_buffer_lock);
511 static unsigned int tower_poll (struct file *file, poll_table *wait)
513 struct lego_usb_tower *dev;
514 unsigned int mask = 0;
516 dev = file->private_data;
519 return POLLERR | POLLHUP;
521 poll_wait(file, &dev->read_wait, wait);
522 poll_wait(file, &dev->write_wait, wait);
524 tower_check_for_read_packet(dev);
525 if (dev->read_packet_length > 0) {
526 mask |= POLLIN | POLLRDNORM;
528 if (!dev->interrupt_out_busy) {
529 mask |= POLLOUT | POLLWRNORM;
539 static loff_t tower_llseek (struct file *file, loff_t off, int whence)
541 return -ESPIPE; /* unseekable */
548 static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
550 struct lego_usb_tower *dev;
551 size_t bytes_to_read;
554 unsigned long timeout = 0;
556 dev = file->private_data;
558 /* lock this object */
559 if (mutex_lock_interruptible(&dev->lock)) {
560 retval = -ERESTARTSYS;
564 /* verify that the device wasn't unplugged */
565 if (dev->udev == NULL) {
567 pr_err("No device or device unplugged %d\n", retval);
571 /* verify that we actually have some data to read */
573 dev_dbg(&dev->udev->dev, "read request of 0 bytes\n");
578 timeout = jiffies + read_timeout * HZ / 1000;
582 tower_check_for_read_packet (dev);
583 while (dev->read_packet_length == 0) {
584 if (file->f_flags & O_NONBLOCK) {
588 retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies);
593 /* reset read timeout during read or write activity */
595 && (dev->read_buffer_length || dev->interrupt_out_busy)) {
596 timeout = jiffies + read_timeout * HZ / 1000;
598 /* check for read timeout */
599 if (read_timeout && time_after (jiffies, timeout)) {
603 tower_check_for_read_packet (dev);
606 /* copy the data from read_buffer into userspace */
607 bytes_to_read = min(count, dev->read_packet_length);
609 if (copy_to_user (buffer, dev->read_buffer, bytes_to_read)) {
614 spin_lock_irq (&dev->read_buffer_lock);
615 dev->read_buffer_length -= bytes_to_read;
616 dev->read_packet_length -= bytes_to_read;
617 for (i=0; i<dev->read_buffer_length; i++) {
618 dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read];
620 spin_unlock_irq (&dev->read_buffer_lock);
622 retval = bytes_to_read;
625 /* unlock the device */
626 mutex_unlock(&dev->lock);
636 static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
638 struct lego_usb_tower *dev;
639 size_t bytes_to_write;
642 dev = file->private_data;
644 /* lock this object */
645 if (mutex_lock_interruptible(&dev->lock)) {
646 retval = -ERESTARTSYS;
650 /* verify that the device wasn't unplugged */
651 if (dev->udev == NULL) {
653 pr_err("No device or device unplugged %d\n", retval);
657 /* verify that we actually have some data to write */
659 dev_dbg(&dev->udev->dev, "write request of 0 bytes\n");
663 /* wait until previous transfer is finished */
664 while (dev->interrupt_out_busy) {
665 if (file->f_flags & O_NONBLOCK) {
669 retval = wait_event_interruptible (dev->write_wait, !dev->interrupt_out_busy);
675 /* write the data into interrupt_out_buffer from userspace */
676 bytes_to_write = min_t(int, count, write_buffer_size);
677 dev_dbg(&dev->udev->dev, "%s: count = %Zd, bytes_to_write = %Zd\n",
678 __func__, count, bytes_to_write);
680 if (copy_from_user (dev->interrupt_out_buffer, buffer, bytes_to_write)) {
685 /* send off the urb */
686 usb_fill_int_urb(dev->interrupt_out_urb,
688 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
689 dev->interrupt_out_buffer,
691 tower_interrupt_out_callback,
693 dev->interrupt_out_interval);
695 dev->interrupt_out_busy = 1;
698 retval = usb_submit_urb (dev->interrupt_out_urb, GFP_KERNEL);
700 dev->interrupt_out_busy = 0;
701 dev_err(&dev->udev->dev,
702 "Couldn't submit interrupt_out_urb %d\n", retval);
705 retval = bytes_to_write;
708 /* unlock the device */
709 mutex_unlock(&dev->lock);
717 * tower_interrupt_in_callback
719 static void tower_interrupt_in_callback (struct urb *urb)
721 struct lego_usb_tower *dev = urb->context;
722 int status = urb->status;
725 lego_usb_tower_debug_data(&dev->udev->dev, __func__,
726 urb->actual_length, urb->transfer_buffer);
729 if (status == -ENOENT ||
730 status == -ECONNRESET ||
731 status == -ESHUTDOWN) {
734 dev_dbg(&dev->udev->dev,
735 "%s: nonzero status received: %d\n", __func__,
737 goto resubmit; /* maybe we can recover */
741 if (urb->actual_length > 0) {
742 spin_lock (&dev->read_buffer_lock);
743 if (dev->read_buffer_length + urb->actual_length < read_buffer_size) {
744 memcpy (dev->read_buffer + dev->read_buffer_length,
745 dev->interrupt_in_buffer,
747 dev->read_buffer_length += urb->actual_length;
748 dev->read_last_arrival = jiffies;
749 dev_dbg(&dev->udev->dev, "%s: received %d bytes\n",
750 __func__, urb->actual_length);
752 pr_warn("read_buffer overflow, %d bytes dropped\n",
755 spin_unlock (&dev->read_buffer_lock);
759 /* resubmit if we're still running */
760 if (dev->interrupt_in_running && dev->udev) {
761 retval = usb_submit_urb (dev->interrupt_in_urb, GFP_ATOMIC);
763 dev_err(&dev->udev->dev,
764 "%s: usb_submit_urb failed (%d)\n",
769 dev->interrupt_in_done = 1;
770 wake_up_interruptible (&dev->read_wait);
775 * tower_interrupt_out_callback
777 static void tower_interrupt_out_callback (struct urb *urb)
779 struct lego_usb_tower *dev = urb->context;
780 int status = urb->status;
782 lego_usb_tower_debug_data(&dev->udev->dev, __func__,
783 urb->actual_length, urb->transfer_buffer);
785 /* sync/async unlink faults aren't errors */
786 if (status && !(status == -ENOENT ||
787 status == -ECONNRESET ||
788 status == -ESHUTDOWN)) {
789 dev_dbg(&dev->udev->dev,
790 "%s: nonzero write bulk status received: %d\n", __func__,
794 dev->interrupt_out_busy = 0;
795 wake_up_interruptible(&dev->write_wait);
802 * Called by the usb core when a new device is connected that it thinks
803 * this driver might be interested in.
805 static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id)
807 struct device *idev = &interface->dev;
808 struct usb_device *udev = interface_to_usbdev(interface);
809 struct lego_usb_tower *dev = NULL;
810 struct usb_host_interface *iface_desc;
811 struct usb_endpoint_descriptor* endpoint;
812 struct tower_get_version_reply get_version_reply;
814 int retval = -ENOMEM;
817 /* allocate memory for our device state and initialize it */
819 dev = kmalloc (sizeof(struct lego_usb_tower), GFP_KERNEL);
822 dev_err(idev, "Out of memory\n");
826 mutex_init(&dev->lock);
831 dev->read_buffer = NULL;
832 dev->read_buffer_length = 0;
833 dev->read_packet_length = 0;
834 spin_lock_init (&dev->read_buffer_lock);
835 dev->packet_timeout_jiffies = packet_timeout * HZ / 1000;
836 dev->read_last_arrival = jiffies;
838 init_waitqueue_head (&dev->read_wait);
839 init_waitqueue_head (&dev->write_wait);
841 dev->interrupt_in_buffer = NULL;
842 dev->interrupt_in_endpoint = NULL;
843 dev->interrupt_in_urb = NULL;
844 dev->interrupt_in_running = 0;
845 dev->interrupt_in_done = 0;
847 dev->interrupt_out_buffer = NULL;
848 dev->interrupt_out_endpoint = NULL;
849 dev->interrupt_out_urb = NULL;
850 dev->interrupt_out_busy = 0;
852 iface_desc = interface->cur_altsetting;
854 /* set up the endpoint information */
855 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
856 endpoint = &iface_desc->endpoint[i].desc;
858 if (usb_endpoint_xfer_int(endpoint)) {
859 if (usb_endpoint_dir_in(endpoint))
860 dev->interrupt_in_endpoint = endpoint;
862 dev->interrupt_out_endpoint = endpoint;
865 if(dev->interrupt_in_endpoint == NULL) {
866 dev_err(idev, "interrupt in endpoint not found\n");
869 if (dev->interrupt_out_endpoint == NULL) {
870 dev_err(idev, "interrupt out endpoint not found\n");
874 dev->read_buffer = kmalloc (read_buffer_size, GFP_KERNEL);
875 if (!dev->read_buffer) {
876 dev_err(idev, "Couldn't allocate read_buffer\n");
879 dev->interrupt_in_buffer = kmalloc (usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL);
880 if (!dev->interrupt_in_buffer) {
881 dev_err(idev, "Couldn't allocate interrupt_in_buffer\n");
884 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
885 if (!dev->interrupt_in_urb) {
886 dev_err(idev, "Couldn't allocate interrupt_in_urb\n");
889 dev->interrupt_out_buffer = kmalloc (write_buffer_size, GFP_KERNEL);
890 if (!dev->interrupt_out_buffer) {
891 dev_err(idev, "Couldn't allocate interrupt_out_buffer\n");
894 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
895 if (!dev->interrupt_out_urb) {
896 dev_err(idev, "Couldn't allocate interrupt_out_urb\n");
899 dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
900 dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
902 /* we can register the device now, as it is ready */
903 usb_set_intfdata (interface, dev);
905 retval = usb_register_dev (interface, &tower_class);
908 /* something prevented us from registering this driver */
909 dev_err(idev, "Not able to get a minor for this device.\n");
910 usb_set_intfdata (interface, NULL);
913 dev->minor = interface->minor;
915 /* let the user know what node this device is now attached to */
916 dev_info(&interface->dev, "LEGO USB Tower #%d now attached to major "
917 "%d minor %d\n", (dev->minor - LEGO_USB_TOWER_MINOR_BASE),
918 USB_MAJOR, dev->minor);
920 /* get the firmware version and log it */
921 result = usb_control_msg (udev,
922 usb_rcvctrlpipe(udev, 0),
923 LEGO_USB_TOWER_REQUEST_GET_VERSION,
924 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
928 sizeof(get_version_reply),
931 dev_err(idev, "LEGO USB Tower get version control request failed\n");
935 dev_info(&interface->dev, "LEGO USB Tower firmware version is %d.%d "
936 "build %d\n", get_version_reply.major,
937 get_version_reply.minor,
938 le16_to_cpu(get_version_reply.build_no));
953 * Called by the usb core when the device is removed from the system.
955 static void tower_disconnect (struct usb_interface *interface)
957 struct lego_usb_tower *dev;
960 dev = usb_get_intfdata (interface);
961 mutex_lock(&open_disc_mutex);
962 usb_set_intfdata (interface, NULL);
966 /* give back our minor */
967 usb_deregister_dev (interface, &tower_class);
969 mutex_lock(&dev->lock);
970 mutex_unlock(&open_disc_mutex);
972 /* if the device is not opened, then we clean up right now */
973 if (!dev->open_count) {
974 mutex_unlock(&dev->lock);
978 /* wake up pollers */
979 wake_up_interruptible_all(&dev->read_wait);
980 wake_up_interruptible_all(&dev->write_wait);
981 mutex_unlock(&dev->lock);
984 dev_info(&interface->dev, "LEGO USB Tower #%d now disconnected\n",
985 (minor - LEGO_USB_TOWER_MINOR_BASE));
988 module_usb_driver(tower_driver);
990 MODULE_AUTHOR(DRIVER_AUTHOR);
991 MODULE_DESCRIPTION(DRIVER_DESC);
992 #ifdef MODULE_LICENSE
993 MODULE_LICENSE("GPL");