Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * LEGO USB Tower driver
0004  *
0005  * Copyright (C) 2003 David Glance <davidgsf@sourceforge.net>
0006  *               2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
0007  *
0008  * derived from USB Skeleton driver - 0.5
0009  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
0010  *
0011  * History:
0012  *
0013  * 2001-10-13 - 0.1 js
0014  *   - first version
0015  * 2001-11-03 - 0.2 js
0016  *   - simplified buffering, one-shot URBs for writing
0017  * 2001-11-10 - 0.3 js
0018  *   - removed IOCTL (setting power/mode is more complicated, postponed)
0019  * 2001-11-28 - 0.4 js
0020  *   - added vendor commands for mode of operation and power level in open
0021  * 2001-12-04 - 0.5 js
0022  *   - set IR mode by default (by oversight 0.4 set VLL mode)
0023  * 2002-01-11 - 0.5? pcchan
0024  *   - make read buffer reusable and work around bytes_to_write issue between
0025  *     uhci and legusbtower
0026  * 2002-09-23 - 0.52 david (david@csse.uwa.edu.au)
0027  *   - imported into lejos project
0028  *   - changed wake_up to wake_up_interruptible
0029  *   - changed to use lego0 rather than tower0
0030  *   - changed dbg() to use __func__ rather than deprecated __func__
0031  * 2003-01-12 - 0.53 david (david@csse.uwa.edu.au)
0032  *   - changed read and write to write everything or
0033  *     timeout (from a patch by Chris Riesen and Brett Thaeler driver)
0034  *   - added ioctl functionality to set timeouts
0035  * 2003-07-18 - 0.54 davidgsf (david@csse.uwa.edu.au)
0036  *   - initial import into LegoUSB project
0037  *   - merge of existing LegoUSB.c driver
0038  * 2003-07-18 - 0.56 davidgsf (david@csse.uwa.edu.au)
0039  *   - port to 2.6 style driver
0040  * 2004-02-29 - 0.6 Juergen Stuber <starblue@users.sourceforge.net>
0041  *   - fix locking
0042  *   - unlink read URBs which are no longer needed
0043  *   - allow increased buffer size, eliminates need for timeout on write
0044  *   - have read URB running continuously
0045  *   - added poll
0046  *   - forbid seeking
0047  *   - added nonblocking I/O
0048  *   - changed back __func__ to __func__
0049  *   - read and log tower firmware version
0050  *   - reset tower on probe, avoids failure of first write
0051  * 2004-03-09 - 0.7 Juergen Stuber <starblue@users.sourceforge.net>
0052  *   - timeout read now only after inactivity, shorten default accordingly
0053  * 2004-03-11 - 0.8 Juergen Stuber <starblue@users.sourceforge.net>
0054  *   - log major, minor instead of possibly confusing device filename
0055  *   - whitespace cleanup
0056  * 2004-03-12 - 0.9 Juergen Stuber <starblue@users.sourceforge.net>
0057  *   - normalize whitespace in debug messages
0058  *   - take care about endianness in control message responses
0059  * 2004-03-13 - 0.91 Juergen Stuber <starblue@users.sourceforge.net>
0060  *   - make default intervals longer to accommodate current EHCI driver
0061  * 2004-03-19 - 0.92 Juergen Stuber <starblue@users.sourceforge.net>
0062  *   - replaced atomic_t by memory barriers
0063  * 2004-04-21 - 0.93 Juergen Stuber <starblue@users.sourceforge.net>
0064  *   - wait for completion of write urb in release (needed for remotecontrol)
0065  *   - corrected poll for write direction (missing negation)
0066  * 2004-04-22 - 0.94 Juergen Stuber <starblue@users.sourceforge.net>
0067  *   - make device locking interruptible
0068  * 2004-04-30 - 0.95 Juergen Stuber <starblue@users.sourceforge.net>
0069  *   - check for valid udev on resubmitting and unlinking urbs
0070  * 2004-08-03 - 0.96 Juergen Stuber <starblue@users.sourceforge.net>
0071  *   - move reset into open to clean out spurious data
0072  */
0073 
0074 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0075 
0076 #include <linux/kernel.h>
0077 #include <linux/errno.h>
0078 #include <linux/slab.h>
0079 #include <linux/module.h>
0080 #include <linux/completion.h>
0081 #include <linux/mutex.h>
0082 #include <linux/uaccess.h>
0083 #include <linux/usb.h>
0084 #include <linux/poll.h>
0085 
0086 
0087 #define DRIVER_AUTHOR "Juergen Stuber <starblue@sourceforge.net>"
0088 #define DRIVER_DESC "LEGO USB Tower Driver"
0089 
0090 
0091 /* The defaults are chosen to work with the latest versions of leJOS and NQC.
0092  */
0093 
0094 /* Some legacy software likes to receive packets in one piece.
0095  * In this case read_buffer_size should exceed the maximal packet length
0096  * (417 for datalog uploads), and packet_timeout should be set.
0097  */
0098 static int read_buffer_size = 480;
0099 module_param(read_buffer_size, int, 0);
0100 MODULE_PARM_DESC(read_buffer_size, "Read buffer size");
0101 
0102 /* Some legacy software likes to send packets in one piece.
0103  * In this case write_buffer_size should exceed the maximal packet length
0104  * (417 for firmware and program downloads).
0105  * A problem with long writes is that the following read may time out
0106  * if the software is not prepared to wait long enough.
0107  */
0108 static int write_buffer_size = 480;
0109 module_param(write_buffer_size, int, 0);
0110 MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
0111 
0112 /* Some legacy software expects reads to contain whole LASM packets.
0113  * To achieve this, characters which arrive before a packet timeout
0114  * occurs will be returned in a single read operation.
0115  * A problem with long reads is that the software may time out
0116  * if it is not prepared to wait long enough.
0117  * The packet timeout should be greater than the time between the
0118  * reception of subsequent characters, which should arrive about
0119  * every 5ms for the standard 2400 baud.
0120  * Set it to 0 to disable.
0121  */
0122 static int packet_timeout = 50;
0123 module_param(packet_timeout, int, 0);
0124 MODULE_PARM_DESC(packet_timeout, "Packet timeout in ms");
0125 
0126 /* Some legacy software expects blocking reads to time out.
0127  * Timeout occurs after the specified time of read and write inactivity.
0128  * Set it to 0 to disable.
0129  */
0130 static int read_timeout = 200;
0131 module_param(read_timeout, int, 0);
0132 MODULE_PARM_DESC(read_timeout, "Read timeout in ms");
0133 
0134 /* As of kernel version 2.6.4 ehci-hcd uses an
0135  * "only one interrupt transfer per frame" shortcut
0136  * to simplify the scheduling of periodic transfers.
0137  * This conflicts with our standard 1ms intervals for in and out URBs.
0138  * We use default intervals of 2ms for in and 8ms for out transfers,
0139  * which is fast enough for 2400 baud and allows a small additional load.
0140  * Increase the interval to allow more devices that do interrupt transfers,
0141  * or set to 0 to use the standard interval from the endpoint descriptors.
0142  */
0143 static int interrupt_in_interval = 2;
0144 module_param(interrupt_in_interval, int, 0);
0145 MODULE_PARM_DESC(interrupt_in_interval, "Interrupt in interval in ms");
0146 
0147 static int interrupt_out_interval = 8;
0148 module_param(interrupt_out_interval, int, 0);
0149 MODULE_PARM_DESC(interrupt_out_interval, "Interrupt out interval in ms");
0150 
0151 /* Define these values to match your device */
0152 #define LEGO_USB_TOWER_VENDOR_ID    0x0694
0153 #define LEGO_USB_TOWER_PRODUCT_ID   0x0001
0154 
0155 /* Vendor requests */
0156 #define LEGO_USB_TOWER_REQUEST_RESET        0x04
0157 #define LEGO_USB_TOWER_REQUEST_GET_VERSION  0xFD
0158 
0159 struct tower_reset_reply {
0160     __le16 size;
0161     __u8 err_code;
0162     __u8 spare;
0163 };
0164 
0165 struct tower_get_version_reply {
0166     __le16 size;
0167     __u8 err_code;
0168     __u8 spare;
0169     __u8 major;
0170     __u8 minor;
0171     __le16 build_no;
0172 };
0173 
0174 
0175 /* table of devices that work with this driver */
0176 static const struct usb_device_id tower_table[] = {
0177     { USB_DEVICE(LEGO_USB_TOWER_VENDOR_ID, LEGO_USB_TOWER_PRODUCT_ID) },
0178     { }                 /* Terminating entry */
0179 };
0180 
0181 MODULE_DEVICE_TABLE(usb, tower_table);
0182 
0183 #define LEGO_USB_TOWER_MINOR_BASE   160
0184 
0185 
0186 /* Structure to hold all of our device specific stuff */
0187 struct lego_usb_tower {
0188     struct mutex        lock;       /* locks this structure */
0189     struct usb_device   *udev;      /* save off the usb device pointer */
0190     unsigned char       minor;      /* the starting minor number for this device */
0191 
0192     int         open_count; /* number of times this port has been opened */
0193     unsigned long       disconnected:1;
0194 
0195     char            *read_buffer;
0196     size_t          read_buffer_length; /* this much came in */
0197     size_t          read_packet_length; /* this much will be returned on read */
0198     spinlock_t      read_buffer_lock;
0199     int         packet_timeout_jiffies;
0200     unsigned long       read_last_arrival;
0201 
0202     wait_queue_head_t   read_wait;
0203     wait_queue_head_t   write_wait;
0204 
0205     char            *interrupt_in_buffer;
0206     struct usb_endpoint_descriptor *interrupt_in_endpoint;
0207     struct urb      *interrupt_in_urb;
0208     int         interrupt_in_interval;
0209     int         interrupt_in_done;
0210 
0211     char            *interrupt_out_buffer;
0212     struct usb_endpoint_descriptor *interrupt_out_endpoint;
0213     struct urb      *interrupt_out_urb;
0214     int         interrupt_out_interval;
0215     int         interrupt_out_busy;
0216 
0217 };
0218 
0219 
0220 /* local function prototypes */
0221 static ssize_t tower_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos);
0222 static ssize_t tower_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
0223 static inline void tower_delete(struct lego_usb_tower *dev);
0224 static int tower_open(struct inode *inode, struct file *file);
0225 static int tower_release(struct inode *inode, struct file *file);
0226 static __poll_t tower_poll(struct file *file, poll_table *wait);
0227 static loff_t tower_llseek(struct file *file, loff_t off, int whence);
0228 
0229 static void tower_check_for_read_packet(struct lego_usb_tower *dev);
0230 static void tower_interrupt_in_callback(struct urb *urb);
0231 static void tower_interrupt_out_callback(struct urb *urb);
0232 
0233 static int  tower_probe(struct usb_interface *interface, const struct usb_device_id *id);
0234 static void tower_disconnect(struct usb_interface *interface);
0235 
0236 
0237 /* file operations needed when we register this driver */
0238 static const struct file_operations tower_fops = {
0239     .owner =    THIS_MODULE,
0240     .read  =    tower_read,
0241     .write =    tower_write,
0242     .open =     tower_open,
0243     .release =  tower_release,
0244     .poll =     tower_poll,
0245     .llseek =   tower_llseek,
0246 };
0247 
0248 static char *legousbtower_devnode(struct device *dev, umode_t *mode)
0249 {
0250     return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
0251 }
0252 
0253 /*
0254  * usb class driver info in order to get a minor number from the usb core,
0255  * and to have the device registered with the driver core
0256  */
0257 static struct usb_class_driver tower_class = {
0258     .name =     "legousbtower%d",
0259     .devnode =  legousbtower_devnode,
0260     .fops =     &tower_fops,
0261     .minor_base =   LEGO_USB_TOWER_MINOR_BASE,
0262 };
0263 
0264 
0265 /* usb specific object needed to register this driver with the usb subsystem */
0266 static struct usb_driver tower_driver = {
0267     .name =     "legousbtower",
0268     .probe =    tower_probe,
0269     .disconnect =   tower_disconnect,
0270     .id_table = tower_table,
0271 };
0272 
0273 
0274 /*
0275  *  lego_usb_tower_debug_data
0276  */
0277 static inline void lego_usb_tower_debug_data(struct device *dev,
0278                          const char *function, int size,
0279                          const unsigned char *data)
0280 {
0281     dev_dbg(dev, "%s - length = %d, data = %*ph\n",
0282         function, size, size, data);
0283 }
0284 
0285 
0286 /*
0287  *  tower_delete
0288  */
0289 static inline void tower_delete(struct lego_usb_tower *dev)
0290 {
0291     /* free data structures */
0292     usb_free_urb(dev->interrupt_in_urb);
0293     usb_free_urb(dev->interrupt_out_urb);
0294     kfree(dev->read_buffer);
0295     kfree(dev->interrupt_in_buffer);
0296     kfree(dev->interrupt_out_buffer);
0297     usb_put_dev(dev->udev);
0298     kfree(dev);
0299 }
0300 
0301 
0302 /*
0303  *  tower_open
0304  */
0305 static int tower_open(struct inode *inode, struct file *file)
0306 {
0307     struct lego_usb_tower *dev = NULL;
0308     int subminor;
0309     int retval = 0;
0310     struct usb_interface *interface;
0311     struct tower_reset_reply reset_reply;
0312     int result;
0313 
0314     nonseekable_open(inode, file);
0315     subminor = iminor(inode);
0316 
0317     interface = usb_find_interface(&tower_driver, subminor);
0318     if (!interface) {
0319         pr_err("error, can't find device for minor %d\n", subminor);
0320         retval = -ENODEV;
0321         goto exit;
0322     }
0323 
0324     dev = usb_get_intfdata(interface);
0325     if (!dev) {
0326         retval = -ENODEV;
0327         goto exit;
0328     }
0329 
0330     /* lock this device */
0331     if (mutex_lock_interruptible(&dev->lock)) {
0332             retval = -ERESTARTSYS;
0333         goto exit;
0334     }
0335 
0336 
0337     /* allow opening only once */
0338     if (dev->open_count) {
0339         retval = -EBUSY;
0340         goto unlock_exit;
0341     }
0342 
0343     /* reset the tower */
0344     result = usb_control_msg_recv(dev->udev, 0,
0345                       LEGO_USB_TOWER_REQUEST_RESET,
0346                       USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
0347                       0, 0,
0348                       &reset_reply, sizeof(reset_reply), 1000,
0349                       GFP_KERNEL);
0350     if (result < 0) {
0351         dev_err(&dev->udev->dev,
0352             "LEGO USB Tower reset control request failed\n");
0353         retval = result;
0354         goto unlock_exit;
0355     }
0356 
0357     /* initialize in direction */
0358     dev->read_buffer_length = 0;
0359     dev->read_packet_length = 0;
0360     usb_fill_int_urb(dev->interrupt_in_urb,
0361              dev->udev,
0362              usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress),
0363              dev->interrupt_in_buffer,
0364              usb_endpoint_maxp(dev->interrupt_in_endpoint),
0365              tower_interrupt_in_callback,
0366              dev,
0367              dev->interrupt_in_interval);
0368 
0369     dev->interrupt_in_done = 0;
0370     mb();
0371 
0372     retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
0373     if (retval) {
0374         dev_err(&dev->udev->dev,
0375             "Couldn't submit interrupt_in_urb %d\n", retval);
0376         goto unlock_exit;
0377     }
0378 
0379     /* save device in the file's private structure */
0380     file->private_data = dev;
0381 
0382     dev->open_count = 1;
0383 
0384 unlock_exit:
0385     mutex_unlock(&dev->lock);
0386 
0387 exit:
0388     return retval;
0389 }
0390 
0391 /*
0392  *  tower_release
0393  */
0394 static int tower_release(struct inode *inode, struct file *file)
0395 {
0396     struct lego_usb_tower *dev;
0397     int retval = 0;
0398 
0399     dev = file->private_data;
0400     if (dev == NULL) {
0401         retval = -ENODEV;
0402         goto exit;
0403     }
0404 
0405     mutex_lock(&dev->lock);
0406 
0407     if (dev->disconnected) {
0408         /* the device was unplugged before the file was released */
0409 
0410         /* unlock here as tower_delete frees dev */
0411         mutex_unlock(&dev->lock);
0412         tower_delete(dev);
0413         goto exit;
0414     }
0415 
0416     /* wait until write transfer is finished */
0417     if (dev->interrupt_out_busy) {
0418         wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy,
0419                          2 * HZ);
0420     }
0421 
0422     /* shutdown transfers */
0423     usb_kill_urb(dev->interrupt_in_urb);
0424     usb_kill_urb(dev->interrupt_out_urb);
0425 
0426     dev->open_count = 0;
0427 
0428     mutex_unlock(&dev->lock);
0429 exit:
0430     return retval;
0431 }
0432 
0433 /*
0434  *  tower_check_for_read_packet
0435  *
0436  *      To get correct semantics for signals and non-blocking I/O
0437  *      with packetizing we pretend not to see any data in the read buffer
0438  *      until it has been there unchanged for at least
0439  *      dev->packet_timeout_jiffies, or until the buffer is full.
0440  */
0441 static void tower_check_for_read_packet(struct lego_usb_tower *dev)
0442 {
0443     spin_lock_irq(&dev->read_buffer_lock);
0444     if (!packet_timeout
0445         || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies)
0446         || dev->read_buffer_length == read_buffer_size) {
0447         dev->read_packet_length = dev->read_buffer_length;
0448     }
0449     dev->interrupt_in_done = 0;
0450     spin_unlock_irq(&dev->read_buffer_lock);
0451 }
0452 
0453 
0454 /*
0455  *  tower_poll
0456  */
0457 static __poll_t tower_poll(struct file *file, poll_table *wait)
0458 {
0459     struct lego_usb_tower *dev;
0460     __poll_t mask = 0;
0461 
0462     dev = file->private_data;
0463 
0464     if (dev->disconnected)
0465         return EPOLLERR | EPOLLHUP;
0466 
0467     poll_wait(file, &dev->read_wait, wait);
0468     poll_wait(file, &dev->write_wait, wait);
0469 
0470     tower_check_for_read_packet(dev);
0471     if (dev->read_packet_length > 0)
0472         mask |= EPOLLIN | EPOLLRDNORM;
0473     if (!dev->interrupt_out_busy)
0474         mask |= EPOLLOUT | EPOLLWRNORM;
0475 
0476     return mask;
0477 }
0478 
0479 
0480 /*
0481  *  tower_llseek
0482  */
0483 static loff_t tower_llseek(struct file *file, loff_t off, int whence)
0484 {
0485     return -ESPIPE;     /* unseekable */
0486 }
0487 
0488 
0489 /*
0490  *  tower_read
0491  */
0492 static ssize_t tower_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
0493 {
0494     struct lego_usb_tower *dev;
0495     size_t bytes_to_read;
0496     int i;
0497     int retval = 0;
0498     unsigned long timeout = 0;
0499 
0500     dev = file->private_data;
0501 
0502     /* lock this object */
0503     if (mutex_lock_interruptible(&dev->lock)) {
0504         retval = -ERESTARTSYS;
0505         goto exit;
0506     }
0507 
0508     /* verify that the device wasn't unplugged */
0509     if (dev->disconnected) {
0510         retval = -ENODEV;
0511         goto unlock_exit;
0512     }
0513 
0514     /* verify that we actually have some data to read */
0515     if (count == 0) {
0516         dev_dbg(&dev->udev->dev, "read request of 0 bytes\n");
0517         goto unlock_exit;
0518     }
0519 
0520     if (read_timeout)
0521         timeout = jiffies + msecs_to_jiffies(read_timeout);
0522 
0523     /* wait for data */
0524     tower_check_for_read_packet(dev);
0525     while (dev->read_packet_length == 0) {
0526         if (file->f_flags & O_NONBLOCK) {
0527             retval = -EAGAIN;
0528             goto unlock_exit;
0529         }
0530         retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies);
0531         if (retval < 0)
0532             goto unlock_exit;
0533 
0534         /* reset read timeout during read or write activity */
0535         if (read_timeout
0536             && (dev->read_buffer_length || dev->interrupt_out_busy)) {
0537             timeout = jiffies + msecs_to_jiffies(read_timeout);
0538         }
0539         /* check for read timeout */
0540         if (read_timeout && time_after(jiffies, timeout)) {
0541             retval = -ETIMEDOUT;
0542             goto unlock_exit;
0543         }
0544         tower_check_for_read_packet(dev);
0545     }
0546 
0547     /* copy the data from read_buffer into userspace */
0548     bytes_to_read = min(count, dev->read_packet_length);
0549 
0550     if (copy_to_user(buffer, dev->read_buffer, bytes_to_read)) {
0551         retval = -EFAULT;
0552         goto unlock_exit;
0553     }
0554 
0555     spin_lock_irq(&dev->read_buffer_lock);
0556     dev->read_buffer_length -= bytes_to_read;
0557     dev->read_packet_length -= bytes_to_read;
0558     for (i = 0; i < dev->read_buffer_length; i++)
0559         dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read];
0560     spin_unlock_irq(&dev->read_buffer_lock);
0561 
0562     retval = bytes_to_read;
0563 
0564 unlock_exit:
0565     /* unlock the device */
0566     mutex_unlock(&dev->lock);
0567 
0568 exit:
0569     return retval;
0570 }
0571 
0572 
0573 /*
0574  *  tower_write
0575  */
0576 static ssize_t tower_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
0577 {
0578     struct lego_usb_tower *dev;
0579     size_t bytes_to_write;
0580     int retval = 0;
0581 
0582     dev = file->private_data;
0583 
0584     /* lock this object */
0585     if (mutex_lock_interruptible(&dev->lock)) {
0586         retval = -ERESTARTSYS;
0587         goto exit;
0588     }
0589 
0590     /* verify that the device wasn't unplugged */
0591     if (dev->disconnected) {
0592         retval = -ENODEV;
0593         goto unlock_exit;
0594     }
0595 
0596     /* verify that we actually have some data to write */
0597     if (count == 0) {
0598         dev_dbg(&dev->udev->dev, "write request of 0 bytes\n");
0599         goto unlock_exit;
0600     }
0601 
0602     /* wait until previous transfer is finished */
0603     while (dev->interrupt_out_busy) {
0604         if (file->f_flags & O_NONBLOCK) {
0605             retval = -EAGAIN;
0606             goto unlock_exit;
0607         }
0608         retval = wait_event_interruptible(dev->write_wait,
0609                           !dev->interrupt_out_busy);
0610         if (retval)
0611             goto unlock_exit;
0612     }
0613 
0614     /* write the data into interrupt_out_buffer from userspace */
0615     bytes_to_write = min_t(int, count, write_buffer_size);
0616     dev_dbg(&dev->udev->dev, "%s: count = %zd, bytes_to_write = %zd\n",
0617         __func__, count, bytes_to_write);
0618 
0619     if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
0620         retval = -EFAULT;
0621         goto unlock_exit;
0622     }
0623 
0624     /* send off the urb */
0625     usb_fill_int_urb(dev->interrupt_out_urb,
0626              dev->udev,
0627              usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
0628              dev->interrupt_out_buffer,
0629              bytes_to_write,
0630              tower_interrupt_out_callback,
0631              dev,
0632              dev->interrupt_out_interval);
0633 
0634     dev->interrupt_out_busy = 1;
0635     wmb();
0636 
0637     retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
0638     if (retval) {
0639         dev->interrupt_out_busy = 0;
0640         dev_err(&dev->udev->dev,
0641             "Couldn't submit interrupt_out_urb %d\n", retval);
0642         goto unlock_exit;
0643     }
0644     retval = bytes_to_write;
0645 
0646 unlock_exit:
0647     /* unlock the device */
0648     mutex_unlock(&dev->lock);
0649 
0650 exit:
0651     return retval;
0652 }
0653 
0654 
0655 /*
0656  *  tower_interrupt_in_callback
0657  */
0658 static void tower_interrupt_in_callback(struct urb *urb)
0659 {
0660     struct lego_usb_tower *dev = urb->context;
0661     int status = urb->status;
0662     int retval;
0663     unsigned long flags;
0664 
0665     lego_usb_tower_debug_data(&dev->udev->dev, __func__,
0666                   urb->actual_length, urb->transfer_buffer);
0667 
0668     if (status) {
0669         if (status == -ENOENT ||
0670             status == -ECONNRESET ||
0671             status == -ESHUTDOWN) {
0672             goto exit;
0673         } else {
0674             dev_dbg(&dev->udev->dev,
0675                 "%s: nonzero status received: %d\n", __func__,
0676                 status);
0677             goto resubmit; /* maybe we can recover */
0678         }
0679     }
0680 
0681     if (urb->actual_length > 0) {
0682         spin_lock_irqsave(&dev->read_buffer_lock, flags);
0683         if (dev->read_buffer_length + urb->actual_length < read_buffer_size) {
0684             memcpy(dev->read_buffer + dev->read_buffer_length,
0685                    dev->interrupt_in_buffer,
0686                    urb->actual_length);
0687             dev->read_buffer_length += urb->actual_length;
0688             dev->read_last_arrival = jiffies;
0689             dev_dbg(&dev->udev->dev, "%s: received %d bytes\n",
0690                 __func__, urb->actual_length);
0691         } else {
0692             pr_warn("read_buffer overflow, %d bytes dropped\n",
0693                 urb->actual_length);
0694         }
0695         spin_unlock_irqrestore(&dev->read_buffer_lock, flags);
0696     }
0697 
0698 resubmit:
0699     retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
0700     if (retval) {
0701         dev_err(&dev->udev->dev, "%s: usb_submit_urb failed (%d)\n",
0702             __func__, retval);
0703     }
0704 exit:
0705     dev->interrupt_in_done = 1;
0706     wake_up_interruptible(&dev->read_wait);
0707 }
0708 
0709 
0710 /*
0711  *  tower_interrupt_out_callback
0712  */
0713 static void tower_interrupt_out_callback(struct urb *urb)
0714 {
0715     struct lego_usb_tower *dev = urb->context;
0716     int status = urb->status;
0717 
0718     lego_usb_tower_debug_data(&dev->udev->dev, __func__,
0719                   urb->actual_length, urb->transfer_buffer);
0720 
0721     /* sync/async unlink faults aren't errors */
0722     if (status && !(status == -ENOENT ||
0723             status == -ECONNRESET ||
0724             status == -ESHUTDOWN)) {
0725         dev_dbg(&dev->udev->dev,
0726             "%s: nonzero write bulk status received: %d\n", __func__,
0727             status);
0728     }
0729 
0730     dev->interrupt_out_busy = 0;
0731     wake_up_interruptible(&dev->write_wait);
0732 }
0733 
0734 
0735 /*
0736  *  tower_probe
0737  *
0738  *  Called by the usb core when a new device is connected that it thinks
0739  *  this driver might be interested in.
0740  */
0741 static int tower_probe(struct usb_interface *interface, const struct usb_device_id *id)
0742 {
0743     struct device *idev = &interface->dev;
0744     struct usb_device *udev = interface_to_usbdev(interface);
0745     struct lego_usb_tower *dev;
0746     struct tower_get_version_reply get_version_reply;
0747     int retval = -ENOMEM;
0748     int result;
0749 
0750     /* allocate memory for our device state and initialize it */
0751     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0752     if (!dev)
0753         goto exit;
0754 
0755     mutex_init(&dev->lock);
0756     dev->udev = usb_get_dev(udev);
0757     spin_lock_init(&dev->read_buffer_lock);
0758     dev->packet_timeout_jiffies = msecs_to_jiffies(packet_timeout);
0759     dev->read_last_arrival = jiffies;
0760     init_waitqueue_head(&dev->read_wait);
0761     init_waitqueue_head(&dev->write_wait);
0762 
0763     result = usb_find_common_endpoints_reverse(interface->cur_altsetting,
0764             NULL, NULL,
0765             &dev->interrupt_in_endpoint,
0766             &dev->interrupt_out_endpoint);
0767     if (result) {
0768         dev_err(idev, "interrupt endpoints not found\n");
0769         retval = result;
0770         goto error;
0771     }
0772 
0773     dev->read_buffer = kmalloc(read_buffer_size, GFP_KERNEL);
0774     if (!dev->read_buffer)
0775         goto error;
0776     dev->interrupt_in_buffer = kmalloc(usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL);
0777     if (!dev->interrupt_in_buffer)
0778         goto error;
0779     dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
0780     if (!dev->interrupt_in_urb)
0781         goto error;
0782     dev->interrupt_out_buffer = kmalloc(write_buffer_size, GFP_KERNEL);
0783     if (!dev->interrupt_out_buffer)
0784         goto error;
0785     dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
0786     if (!dev->interrupt_out_urb)
0787         goto error;
0788     dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
0789     dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
0790 
0791     /* get the firmware version and log it */
0792     result = usb_control_msg_recv(udev, 0,
0793                       LEGO_USB_TOWER_REQUEST_GET_VERSION,
0794                       USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
0795                       0,
0796                       0,
0797                       &get_version_reply,
0798                       sizeof(get_version_reply),
0799                       1000, GFP_KERNEL);
0800     if (result) {
0801         dev_err(idev, "get version request failed: %d\n", result);
0802         retval = result;
0803         goto error;
0804     }
0805     dev_info(&interface->dev,
0806          "LEGO USB Tower firmware version is %d.%d build %d\n",
0807          get_version_reply.major,
0808          get_version_reply.minor,
0809          le16_to_cpu(get_version_reply.build_no));
0810 
0811     /* we can register the device now, as it is ready */
0812     usb_set_intfdata(interface, dev);
0813 
0814     retval = usb_register_dev(interface, &tower_class);
0815     if (retval) {
0816         /* something prevented us from registering this driver */
0817         dev_err(idev, "Not able to get a minor for this device.\n");
0818         goto error;
0819     }
0820     dev->minor = interface->minor;
0821 
0822     /* let the user know what node this device is now attached to */
0823     dev_info(&interface->dev, "LEGO USB Tower #%d now attached to major "
0824          "%d minor %d\n", (dev->minor - LEGO_USB_TOWER_MINOR_BASE),
0825          USB_MAJOR, dev->minor);
0826 
0827 exit:
0828     return retval;
0829 
0830 error:
0831     tower_delete(dev);
0832     return retval;
0833 }
0834 
0835 
0836 /*
0837  *  tower_disconnect
0838  *
0839  *  Called by the usb core when the device is removed from the system.
0840  */
0841 static void tower_disconnect(struct usb_interface *interface)
0842 {
0843     struct lego_usb_tower *dev;
0844     int minor;
0845 
0846     dev = usb_get_intfdata(interface);
0847 
0848     minor = dev->minor;
0849 
0850     /* give back our minor and prevent further open() */
0851     usb_deregister_dev(interface, &tower_class);
0852 
0853     /* stop I/O */
0854     usb_poison_urb(dev->interrupt_in_urb);
0855     usb_poison_urb(dev->interrupt_out_urb);
0856 
0857     mutex_lock(&dev->lock);
0858 
0859     /* if the device is not opened, then we clean up right now */
0860     if (!dev->open_count) {
0861         mutex_unlock(&dev->lock);
0862         tower_delete(dev);
0863     } else {
0864         dev->disconnected = 1;
0865         /* wake up pollers */
0866         wake_up_interruptible_all(&dev->read_wait);
0867         wake_up_interruptible_all(&dev->write_wait);
0868         mutex_unlock(&dev->lock);
0869     }
0870 
0871     dev_info(&interface->dev, "LEGO USB Tower #%d now disconnected\n",
0872          (minor - LEGO_USB_TOWER_MINOR_BASE));
0873 }
0874 
0875 module_usb_driver(tower_driver);
0876 
0877 MODULE_AUTHOR(DRIVER_AUTHOR);
0878 MODULE_DESCRIPTION(DRIVER_DESC);
0879 MODULE_LICENSE("GPL");