Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Generic USB driver for report based interrupt in/out devices
0004  * like LD Didactic's USB devices. LD Didactic's USB devices are
0005  * HID devices which do not use HID report definitons (they use
0006  * raw interrupt in and our reports only for communication).
0007  *
0008  * This driver uses a ring buffer for time critical reading of
0009  * interrupt in reports and provides read and write methods for
0010  * raw interrupt reports (similar to the Windows HID driver).
0011  * Devices based on the book USB COMPLETE by Jan Axelson may need
0012  * such a compatibility to the Windows HID driver.
0013  *
0014  * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
0015  *
0016  * Derived from Lego USB Tower driver
0017  * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net>
0018  *       2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
0019  */
0020 
0021 #include <linux/kernel.h>
0022 #include <linux/errno.h>
0023 #include <linux/slab.h>
0024 #include <linux/module.h>
0025 #include <linux/mutex.h>
0026 
0027 #include <linux/uaccess.h>
0028 #include <linux/input.h>
0029 #include <linux/usb.h>
0030 #include <linux/poll.h>
0031 
0032 /* Define these values to match your devices */
0033 #define USB_VENDOR_ID_LD        0x0f11  /* USB Vendor ID of LD Didactic GmbH */
0034 #define USB_DEVICE_ID_LD_CASSY      0x1000  /* USB Product ID of CASSY-S modules with 8 bytes endpoint size */
0035 #define USB_DEVICE_ID_LD_CASSY2     0x1001  /* USB Product ID of CASSY-S modules with 64 bytes endpoint size */
0036 #define USB_DEVICE_ID_LD_POCKETCASSY    0x1010  /* USB Product ID of Pocket-CASSY */
0037 #define USB_DEVICE_ID_LD_POCKETCASSY2   0x1011  /* USB Product ID of Pocket-CASSY 2 (reserved) */
0038 #define USB_DEVICE_ID_LD_MOBILECASSY    0x1020  /* USB Product ID of Mobile-CASSY */
0039 #define USB_DEVICE_ID_LD_MOBILECASSY2   0x1021  /* USB Product ID of Mobile-CASSY 2 (reserved) */
0040 #define USB_DEVICE_ID_LD_MICROCASSYVOLTAGE  0x1031  /* USB Product ID of Micro-CASSY Voltage */
0041 #define USB_DEVICE_ID_LD_MICROCASSYCURRENT  0x1032  /* USB Product ID of Micro-CASSY Current */
0042 #define USB_DEVICE_ID_LD_MICROCASSYTIME     0x1033  /* USB Product ID of Micro-CASSY Time (reserved) */
0043 #define USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE  0x1035  /* USB Product ID of Micro-CASSY Temperature */
0044 #define USB_DEVICE_ID_LD_MICROCASSYPH       0x1038  /* USB Product ID of Micro-CASSY pH */
0045 #define USB_DEVICE_ID_LD_POWERANALYSERCASSY 0x1040  /* USB Product ID of Power Analyser CASSY */
0046 #define USB_DEVICE_ID_LD_CONVERTERCONTROLLERCASSY   0x1042  /* USB Product ID of Converter Controller CASSY */
0047 #define USB_DEVICE_ID_LD_MACHINETESTCASSY   0x1043  /* USB Product ID of Machine Test CASSY */
0048 #define USB_DEVICE_ID_LD_JWM        0x1080  /* USB Product ID of Joule and Wattmeter */
0049 #define USB_DEVICE_ID_LD_DMMP       0x1081  /* USB Product ID of Digital Multimeter P (reserved) */
0050 #define USB_DEVICE_ID_LD_UMIP       0x1090  /* USB Product ID of UMI P */
0051 #define USB_DEVICE_ID_LD_UMIC       0x10A0  /* USB Product ID of UMI C */
0052 #define USB_DEVICE_ID_LD_UMIB       0x10B0  /* USB Product ID of UMI B */
0053 #define USB_DEVICE_ID_LD_XRAY       0x1100  /* USB Product ID of X-Ray Apparatus 55481 */
0054 #define USB_DEVICE_ID_LD_XRAY2      0x1101  /* USB Product ID of X-Ray Apparatus 554800 */
0055 #define USB_DEVICE_ID_LD_XRAYCT     0x1110  /* USB Product ID of X-Ray Apparatus CT 554821*/
0056 #define USB_DEVICE_ID_LD_VIDEOCOM   0x1200  /* USB Product ID of VideoCom */
0057 #define USB_DEVICE_ID_LD_MOTOR      0x1210  /* USB Product ID of Motor (reserved) */
0058 #define USB_DEVICE_ID_LD_COM3LAB    0x2000  /* USB Product ID of COM3LAB */
0059 #define USB_DEVICE_ID_LD_TELEPORT   0x2010  /* USB Product ID of Terminal Adapter */
0060 #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */
0061 #define USB_DEVICE_ID_LD_POWERCONTROL   0x2030  /* USB Product ID of Converter Control Unit */
0062 #define USB_DEVICE_ID_LD_MACHINETEST    0x2040  /* USB Product ID of Machine Test System */
0063 #define USB_DEVICE_ID_LD_MOSTANALYSER   0x2050  /* USB Product ID of MOST Protocol Analyser */
0064 #define USB_DEVICE_ID_LD_MOSTANALYSER2  0x2051  /* USB Product ID of MOST Protocol Analyser 2 */
0065 #define USB_DEVICE_ID_LD_ABSESP     0x2060  /* USB Product ID of ABS ESP */
0066 #define USB_DEVICE_ID_LD_AUTODATABUS    0x2070  /* USB Product ID of Automotive Data Buses */
0067 #define USB_DEVICE_ID_LD_MCT        0x2080  /* USB Product ID of Microcontroller technique */
0068 #define USB_DEVICE_ID_LD_HYBRID     0x2090  /* USB Product ID of Automotive Hybrid */
0069 #define USB_DEVICE_ID_LD_HEATCONTROL    0x20A0  /* USB Product ID of Heat control */
0070 
0071 #ifdef CONFIG_USB_DYNAMIC_MINORS
0072 #define USB_LD_MINOR_BASE   0
0073 #else
0074 #define USB_LD_MINOR_BASE   176
0075 #endif
0076 
0077 /* table of devices that work with this driver */
0078 static const struct usb_device_id ld_usb_table[] = {
0079     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) },
0080     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY2) },
0081     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) },
0082     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY2) },
0083     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) },
0084     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY2) },
0085     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYVOLTAGE) },
0086     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYCURRENT) },
0087     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTIME) },
0088     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE) },
0089     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYPH) },
0090     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERANALYSERCASSY) },
0091     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CONVERTERCONTROLLERCASSY) },
0092     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETESTCASSY) },
0093     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) },
0094     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) },
0095     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) },
0096     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIC) },
0097     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIB) },
0098     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY) },
0099     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) },
0100     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) },
0101     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOTOR) },
0102     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) },
0103     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) },
0104     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) },
0105     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) },
0106     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) },
0107     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER) },
0108     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER2) },
0109     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_ABSESP) },
0110     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_AUTODATABUS) },
0111     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MCT) },
0112     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HYBRID) },
0113     { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HEATCONTROL) },
0114     { }                 /* Terminating entry */
0115 };
0116 MODULE_DEVICE_TABLE(usb, ld_usb_table);
0117 MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
0118 MODULE_DESCRIPTION("LD USB Driver");
0119 MODULE_LICENSE("GPL");
0120 
0121 /* All interrupt in transfers are collected in a ring buffer to
0122  * avoid racing conditions and get better performance of the driver.
0123  */
0124 static int ring_buffer_size = 128;
0125 module_param(ring_buffer_size, int, 0000);
0126 MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports");
0127 
0128 /* The write_buffer can contain more than one interrupt out transfer.
0129  */
0130 static int write_buffer_size = 10;
0131 module_param(write_buffer_size, int, 0000);
0132 MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports");
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 2ms for out transfers,
0139  * which should be fast enough.
0140  * Increase the interval to allow more devices that do interrupt transfers,
0141  * or set to 1 to use the standard interval from the endpoint descriptors.
0142  */
0143 static int min_interrupt_in_interval = 2;
0144 module_param(min_interrupt_in_interval, int, 0000);
0145 MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms");
0146 
0147 static int min_interrupt_out_interval = 2;
0148 module_param(min_interrupt_out_interval, int, 0000);
0149 MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms");
0150 
0151 /* Structure to hold all of our device specific stuff */
0152 struct ld_usb {
0153     struct mutex        mutex;      /* locks this structure */
0154     struct usb_interface    *intf;      /* save off the usb interface pointer */
0155     unsigned long       disconnected:1;
0156 
0157     int         open_count; /* number of times this port has been opened */
0158 
0159     char            *ring_buffer;
0160     unsigned int        ring_head;
0161     unsigned int        ring_tail;
0162 
0163     wait_queue_head_t   read_wait;
0164     wait_queue_head_t   write_wait;
0165 
0166     char            *interrupt_in_buffer;
0167     struct usb_endpoint_descriptor *interrupt_in_endpoint;
0168     struct urb      *interrupt_in_urb;
0169     int         interrupt_in_interval;
0170     size_t          interrupt_in_endpoint_size;
0171     int         interrupt_in_running;
0172     int         interrupt_in_done;
0173     int         buffer_overflow;
0174     spinlock_t      rbsl;
0175 
0176     char            *interrupt_out_buffer;
0177     struct usb_endpoint_descriptor *interrupt_out_endpoint;
0178     struct urb      *interrupt_out_urb;
0179     int         interrupt_out_interval;
0180     size_t          interrupt_out_endpoint_size;
0181     int         interrupt_out_busy;
0182 };
0183 
0184 static struct usb_driver ld_usb_driver;
0185 
0186 /*
0187  *  ld_usb_abort_transfers
0188  *      aborts transfers and frees associated data structures
0189  */
0190 static void ld_usb_abort_transfers(struct ld_usb *dev)
0191 {
0192     /* shutdown transfer */
0193     if (dev->interrupt_in_running) {
0194         dev->interrupt_in_running = 0;
0195         usb_kill_urb(dev->interrupt_in_urb);
0196     }
0197     if (dev->interrupt_out_busy)
0198         usb_kill_urb(dev->interrupt_out_urb);
0199 }
0200 
0201 /*
0202  *  ld_usb_delete
0203  */
0204 static void ld_usb_delete(struct ld_usb *dev)
0205 {
0206     /* free data structures */
0207     usb_free_urb(dev->interrupt_in_urb);
0208     usb_free_urb(dev->interrupt_out_urb);
0209     kfree(dev->ring_buffer);
0210     kfree(dev->interrupt_in_buffer);
0211     kfree(dev->interrupt_out_buffer);
0212     kfree(dev);
0213 }
0214 
0215 /*
0216  *  ld_usb_interrupt_in_callback
0217  */
0218 static void ld_usb_interrupt_in_callback(struct urb *urb)
0219 {
0220     struct ld_usb *dev = urb->context;
0221     size_t *actual_buffer;
0222     unsigned int next_ring_head;
0223     int status = urb->status;
0224     unsigned long flags;
0225     int retval;
0226 
0227     if (status) {
0228         if (status == -ENOENT ||
0229             status == -ECONNRESET ||
0230             status == -ESHUTDOWN) {
0231             goto exit;
0232         } else {
0233             dev_dbg(&dev->intf->dev,
0234                 "%s: nonzero status received: %d\n", __func__,
0235                 status);
0236             spin_lock_irqsave(&dev->rbsl, flags);
0237             goto resubmit; /* maybe we can recover */
0238         }
0239     }
0240 
0241     spin_lock_irqsave(&dev->rbsl, flags);
0242     if (urb->actual_length > 0) {
0243         next_ring_head = (dev->ring_head+1) % ring_buffer_size;
0244         if (next_ring_head != dev->ring_tail) {
0245             actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_head * (sizeof(size_t)+dev->interrupt_in_endpoint_size));
0246             /* actual_buffer gets urb->actual_length + interrupt_in_buffer */
0247             *actual_buffer = urb->actual_length;
0248             memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length);
0249             dev->ring_head = next_ring_head;
0250             dev_dbg(&dev->intf->dev, "%s: received %d bytes\n",
0251                 __func__, urb->actual_length);
0252         } else {
0253             dev_warn(&dev->intf->dev,
0254                  "Ring buffer overflow, %d bytes dropped\n",
0255                  urb->actual_length);
0256             dev->buffer_overflow = 1;
0257         }
0258     }
0259 
0260 resubmit:
0261     /* resubmit if we're still running */
0262     if (dev->interrupt_in_running && !dev->buffer_overflow) {
0263         retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
0264         if (retval) {
0265             dev_err(&dev->intf->dev,
0266                 "usb_submit_urb failed (%d)\n", retval);
0267             dev->buffer_overflow = 1;
0268         }
0269     }
0270     spin_unlock_irqrestore(&dev->rbsl, flags);
0271 exit:
0272     dev->interrupt_in_done = 1;
0273     wake_up_interruptible(&dev->read_wait);
0274 }
0275 
0276 /*
0277  *  ld_usb_interrupt_out_callback
0278  */
0279 static void ld_usb_interrupt_out_callback(struct urb *urb)
0280 {
0281     struct ld_usb *dev = urb->context;
0282     int status = urb->status;
0283 
0284     /* sync/async unlink faults aren't errors */
0285     if (status && !(status == -ENOENT ||
0286             status == -ECONNRESET ||
0287             status == -ESHUTDOWN))
0288         dev_dbg(&dev->intf->dev,
0289             "%s - nonzero write interrupt status received: %d\n",
0290             __func__, status);
0291 
0292     dev->interrupt_out_busy = 0;
0293     wake_up_interruptible(&dev->write_wait);
0294 }
0295 
0296 /*
0297  *  ld_usb_open
0298  */
0299 static int ld_usb_open(struct inode *inode, struct file *file)
0300 {
0301     struct ld_usb *dev;
0302     int subminor;
0303     int retval;
0304     struct usb_interface *interface;
0305 
0306     stream_open(inode, file);
0307     subminor = iminor(inode);
0308 
0309     interface = usb_find_interface(&ld_usb_driver, subminor);
0310 
0311     if (!interface) {
0312         printk(KERN_ERR "%s - error, can't find device for minor %d\n",
0313                __func__, subminor);
0314         return -ENODEV;
0315     }
0316 
0317     dev = usb_get_intfdata(interface);
0318 
0319     if (!dev)
0320         return -ENODEV;
0321 
0322     /* lock this device */
0323     if (mutex_lock_interruptible(&dev->mutex))
0324         return -ERESTARTSYS;
0325 
0326     /* allow opening only once */
0327     if (dev->open_count) {
0328         retval = -EBUSY;
0329         goto unlock_exit;
0330     }
0331     dev->open_count = 1;
0332 
0333     /* initialize in direction */
0334     dev->ring_head = 0;
0335     dev->ring_tail = 0;
0336     dev->buffer_overflow = 0;
0337     usb_fill_int_urb(dev->interrupt_in_urb,
0338              interface_to_usbdev(interface),
0339              usb_rcvintpipe(interface_to_usbdev(interface),
0340                     dev->interrupt_in_endpoint->bEndpointAddress),
0341              dev->interrupt_in_buffer,
0342              dev->interrupt_in_endpoint_size,
0343              ld_usb_interrupt_in_callback,
0344              dev,
0345              dev->interrupt_in_interval);
0346 
0347     dev->interrupt_in_running = 1;
0348     dev->interrupt_in_done = 0;
0349 
0350     retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
0351     if (retval) {
0352         dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval);
0353         dev->interrupt_in_running = 0;
0354         dev->open_count = 0;
0355         goto unlock_exit;
0356     }
0357 
0358     /* save device in the file's private structure */
0359     file->private_data = dev;
0360 
0361 unlock_exit:
0362     mutex_unlock(&dev->mutex);
0363 
0364     return retval;
0365 }
0366 
0367 /*
0368  *  ld_usb_release
0369  */
0370 static int ld_usb_release(struct inode *inode, struct file *file)
0371 {
0372     struct ld_usb *dev;
0373     int retval = 0;
0374 
0375     dev = file->private_data;
0376 
0377     if (dev == NULL) {
0378         retval = -ENODEV;
0379         goto exit;
0380     }
0381 
0382     mutex_lock(&dev->mutex);
0383 
0384     if (dev->open_count != 1) {
0385         retval = -ENODEV;
0386         goto unlock_exit;
0387     }
0388     if (dev->disconnected) {
0389         /* the device was unplugged before the file was released */
0390         mutex_unlock(&dev->mutex);
0391         /* unlock here as ld_usb_delete frees dev */
0392         ld_usb_delete(dev);
0393         goto exit;
0394     }
0395 
0396     /* wait until write transfer is finished */
0397     if (dev->interrupt_out_busy)
0398         wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
0399     ld_usb_abort_transfers(dev);
0400     dev->open_count = 0;
0401 
0402 unlock_exit:
0403     mutex_unlock(&dev->mutex);
0404 
0405 exit:
0406     return retval;
0407 }
0408 
0409 /*
0410  *  ld_usb_poll
0411  */
0412 static __poll_t ld_usb_poll(struct file *file, poll_table *wait)
0413 {
0414     struct ld_usb *dev;
0415     __poll_t mask = 0;
0416 
0417     dev = file->private_data;
0418 
0419     if (dev->disconnected)
0420         return EPOLLERR | EPOLLHUP;
0421 
0422     poll_wait(file, &dev->read_wait, wait);
0423     poll_wait(file, &dev->write_wait, wait);
0424 
0425     if (dev->ring_head != dev->ring_tail)
0426         mask |= EPOLLIN | EPOLLRDNORM;
0427     if (!dev->interrupt_out_busy)
0428         mask |= EPOLLOUT | EPOLLWRNORM;
0429 
0430     return mask;
0431 }
0432 
0433 /*
0434  *  ld_usb_read
0435  */
0436 static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count,
0437                loff_t *ppos)
0438 {
0439     struct ld_usb *dev;
0440     size_t *actual_buffer;
0441     size_t bytes_to_read;
0442     int retval = 0;
0443     int rv;
0444 
0445     dev = file->private_data;
0446 
0447     /* verify that we actually have some data to read */
0448     if (count == 0)
0449         goto exit;
0450 
0451     /* lock this object */
0452     if (mutex_lock_interruptible(&dev->mutex)) {
0453         retval = -ERESTARTSYS;
0454         goto exit;
0455     }
0456 
0457     /* verify that the device wasn't unplugged */
0458     if (dev->disconnected) {
0459         retval = -ENODEV;
0460         printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval);
0461         goto unlock_exit;
0462     }
0463 
0464     /* wait for data */
0465     spin_lock_irq(&dev->rbsl);
0466     while (dev->ring_head == dev->ring_tail) {
0467         dev->interrupt_in_done = 0;
0468         spin_unlock_irq(&dev->rbsl);
0469         if (file->f_flags & O_NONBLOCK) {
0470             retval = -EAGAIN;
0471             goto unlock_exit;
0472         }
0473         retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done);
0474         if (retval < 0)
0475             goto unlock_exit;
0476 
0477         spin_lock_irq(&dev->rbsl);
0478     }
0479     spin_unlock_irq(&dev->rbsl);
0480 
0481     /* actual_buffer contains actual_length + interrupt_in_buffer */
0482     actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_tail * (sizeof(size_t)+dev->interrupt_in_endpoint_size));
0483     if (*actual_buffer > dev->interrupt_in_endpoint_size) {
0484         retval = -EIO;
0485         goto unlock_exit;
0486     }
0487     bytes_to_read = min(count, *actual_buffer);
0488     if (bytes_to_read < *actual_buffer)
0489         dev_warn(&dev->intf->dev, "Read buffer overflow, %zu bytes dropped\n",
0490              *actual_buffer-bytes_to_read);
0491 
0492     /* copy one interrupt_in_buffer from ring_buffer into userspace */
0493     if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) {
0494         retval = -EFAULT;
0495         goto unlock_exit;
0496     }
0497     retval = bytes_to_read;
0498 
0499     spin_lock_irq(&dev->rbsl);
0500     dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
0501 
0502     if (dev->buffer_overflow) {
0503         dev->buffer_overflow = 0;
0504         spin_unlock_irq(&dev->rbsl);
0505         rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
0506         if (rv < 0)
0507             dev->buffer_overflow = 1;
0508     } else {
0509         spin_unlock_irq(&dev->rbsl);
0510     }
0511 
0512 unlock_exit:
0513     /* unlock the device */
0514     mutex_unlock(&dev->mutex);
0515 
0516 exit:
0517     return retval;
0518 }
0519 
0520 /*
0521  *  ld_usb_write
0522  */
0523 static ssize_t ld_usb_write(struct file *file, const char __user *buffer,
0524                 size_t count, loff_t *ppos)
0525 {
0526     struct ld_usb *dev;
0527     size_t bytes_to_write;
0528     int retval = 0;
0529 
0530     dev = file->private_data;
0531 
0532     /* verify that we actually have some data to write */
0533     if (count == 0)
0534         goto exit;
0535 
0536     /* lock this object */
0537     if (mutex_lock_interruptible(&dev->mutex)) {
0538         retval = -ERESTARTSYS;
0539         goto exit;
0540     }
0541 
0542     /* verify that the device wasn't unplugged */
0543     if (dev->disconnected) {
0544         retval = -ENODEV;
0545         printk(KERN_ERR "ldusb: No device or device unplugged %d\n", retval);
0546         goto unlock_exit;
0547     }
0548 
0549     /* wait until previous transfer is finished */
0550     if (dev->interrupt_out_busy) {
0551         if (file->f_flags & O_NONBLOCK) {
0552             retval = -EAGAIN;
0553             goto unlock_exit;
0554         }
0555         retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy);
0556         if (retval < 0) {
0557             goto unlock_exit;
0558         }
0559     }
0560 
0561     /* write the data into interrupt_out_buffer from userspace */
0562     bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size);
0563     if (bytes_to_write < count)
0564         dev_warn(&dev->intf->dev, "Write buffer overflow, %zu bytes dropped\n",
0565             count - bytes_to_write);
0566     dev_dbg(&dev->intf->dev, "%s: count = %zu, bytes_to_write = %zu\n",
0567         __func__, count, bytes_to_write);
0568 
0569     if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
0570         retval = -EFAULT;
0571         goto unlock_exit;
0572     }
0573 
0574     if (dev->interrupt_out_endpoint == NULL) {
0575         /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */
0576         retval = usb_control_msg(interface_to_usbdev(dev->intf),
0577                      usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0),
0578                      9,
0579                      USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
0580                      1 << 8, 0,
0581                      dev->interrupt_out_buffer,
0582                      bytes_to_write,
0583                      USB_CTRL_SET_TIMEOUT);
0584         if (retval < 0)
0585             dev_err(&dev->intf->dev,
0586                 "Couldn't submit HID_REQ_SET_REPORT %d\n",
0587                 retval);
0588         goto unlock_exit;
0589     }
0590 
0591     /* send off the urb */
0592     usb_fill_int_urb(dev->interrupt_out_urb,
0593              interface_to_usbdev(dev->intf),
0594              usb_sndintpipe(interface_to_usbdev(dev->intf),
0595                     dev->interrupt_out_endpoint->bEndpointAddress),
0596              dev->interrupt_out_buffer,
0597              bytes_to_write,
0598              ld_usb_interrupt_out_callback,
0599              dev,
0600              dev->interrupt_out_interval);
0601 
0602     dev->interrupt_out_busy = 1;
0603     wmb();
0604 
0605     retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
0606     if (retval) {
0607         dev->interrupt_out_busy = 0;
0608         dev_err(&dev->intf->dev,
0609             "Couldn't submit interrupt_out_urb %d\n", retval);
0610         goto unlock_exit;
0611     }
0612     retval = bytes_to_write;
0613 
0614 unlock_exit:
0615     /* unlock the device */
0616     mutex_unlock(&dev->mutex);
0617 
0618 exit:
0619     return retval;
0620 }
0621 
0622 /* file operations needed when we register this driver */
0623 static const struct file_operations ld_usb_fops = {
0624     .owner =    THIS_MODULE,
0625     .read  =    ld_usb_read,
0626     .write =    ld_usb_write,
0627     .open =     ld_usb_open,
0628     .release =  ld_usb_release,
0629     .poll =     ld_usb_poll,
0630     .llseek =   no_llseek,
0631 };
0632 
0633 /*
0634  * usb class driver info in order to get a minor number from the usb core,
0635  * and to have the device registered with the driver core
0636  */
0637 static struct usb_class_driver ld_usb_class = {
0638     .name =     "ldusb%d",
0639     .fops =     &ld_usb_fops,
0640     .minor_base =   USB_LD_MINOR_BASE,
0641 };
0642 
0643 /*
0644  *  ld_usb_probe
0645  *
0646  *  Called by the usb core when a new device is connected that it thinks
0647  *  this driver might be interested in.
0648  */
0649 static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
0650 {
0651     struct usb_device *udev = interface_to_usbdev(intf);
0652     struct ld_usb *dev = NULL;
0653     struct usb_host_interface *iface_desc;
0654     char *buffer;
0655     int retval = -ENOMEM;
0656     int res;
0657 
0658     /* allocate memory for our device state and initialize it */
0659 
0660     dev = kzalloc(sizeof(*dev), GFP_KERNEL);
0661     if (!dev)
0662         goto exit;
0663     mutex_init(&dev->mutex);
0664     spin_lock_init(&dev->rbsl);
0665     dev->intf = intf;
0666     init_waitqueue_head(&dev->read_wait);
0667     init_waitqueue_head(&dev->write_wait);
0668 
0669     /* workaround for early firmware versions on fast computers */
0670     if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) &&
0671         ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) ||
0672          (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) &&
0673         (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) {
0674         buffer = kmalloc(256, GFP_KERNEL);
0675         if (!buffer)
0676             goto error;
0677         /* usb_string makes SETUP+STALL to leave always ControlReadLoop */
0678         usb_string(udev, 255, buffer, 256);
0679         kfree(buffer);
0680     }
0681 
0682     iface_desc = intf->cur_altsetting;
0683 
0684     res = usb_find_last_int_in_endpoint(iface_desc,
0685             &dev->interrupt_in_endpoint);
0686     if (res) {
0687         dev_err(&intf->dev, "Interrupt in endpoint not found\n");
0688         retval = res;
0689         goto error;
0690     }
0691 
0692     res = usb_find_last_int_out_endpoint(iface_desc,
0693             &dev->interrupt_out_endpoint);
0694     if (res)
0695         dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n");
0696 
0697     dev->interrupt_in_endpoint_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
0698     dev->ring_buffer = kcalloc(ring_buffer_size,
0699             sizeof(size_t) + dev->interrupt_in_endpoint_size,
0700             GFP_KERNEL);
0701     if (!dev->ring_buffer)
0702         goto error;
0703     dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
0704     if (!dev->interrupt_in_buffer)
0705         goto error;
0706     dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
0707     if (!dev->interrupt_in_urb)
0708         goto error;
0709     dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? usb_endpoint_maxp(dev->interrupt_out_endpoint) :
0710                                      udev->descriptor.bMaxPacketSize0;
0711     dev->interrupt_out_buffer =
0712         kmalloc_array(write_buffer_size,
0713                   dev->interrupt_out_endpoint_size, GFP_KERNEL);
0714     if (!dev->interrupt_out_buffer)
0715         goto error;
0716     dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
0717     if (!dev->interrupt_out_urb)
0718         goto error;
0719     dev->interrupt_in_interval = max_t(int, min_interrupt_in_interval,
0720                        dev->interrupt_in_endpoint->bInterval);
0721     if (dev->interrupt_out_endpoint)
0722         dev->interrupt_out_interval = max_t(int, min_interrupt_out_interval,
0723                             dev->interrupt_out_endpoint->bInterval);
0724 
0725     /* we can register the device now, as it is ready */
0726     usb_set_intfdata(intf, dev);
0727 
0728     retval = usb_register_dev(intf, &ld_usb_class);
0729     if (retval) {
0730         /* something prevented us from registering this driver */
0731         dev_err(&intf->dev, "Not able to get a minor for this device.\n");
0732         usb_set_intfdata(intf, NULL);
0733         goto error;
0734     }
0735 
0736     /* let the user know what node this device is now attached to */
0737     dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n",
0738         (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor);
0739 
0740 exit:
0741     return retval;
0742 
0743 error:
0744     ld_usb_delete(dev);
0745 
0746     return retval;
0747 }
0748 
0749 /*
0750  *  ld_usb_disconnect
0751  *
0752  *  Called by the usb core when the device is removed from the system.
0753  */
0754 static void ld_usb_disconnect(struct usb_interface *intf)
0755 {
0756     struct ld_usb *dev;
0757     int minor;
0758 
0759     dev = usb_get_intfdata(intf);
0760     usb_set_intfdata(intf, NULL);
0761 
0762     minor = intf->minor;
0763 
0764     /* give back our minor */
0765     usb_deregister_dev(intf, &ld_usb_class);
0766 
0767     usb_poison_urb(dev->interrupt_in_urb);
0768     usb_poison_urb(dev->interrupt_out_urb);
0769 
0770     mutex_lock(&dev->mutex);
0771 
0772     /* if the device is not opened, then we clean up right now */
0773     if (!dev->open_count) {
0774         mutex_unlock(&dev->mutex);
0775         ld_usb_delete(dev);
0776     } else {
0777         dev->disconnected = 1;
0778         /* wake up pollers */
0779         wake_up_interruptible_all(&dev->read_wait);
0780         wake_up_interruptible_all(&dev->write_wait);
0781         mutex_unlock(&dev->mutex);
0782     }
0783 
0784     dev_info(&intf->dev, "LD USB Device #%d now disconnected\n",
0785          (minor - USB_LD_MINOR_BASE));
0786 }
0787 
0788 /* usb specific object needed to register this driver with the usb subsystem */
0789 static struct usb_driver ld_usb_driver = {
0790     .name =     "ldusb",
0791     .probe =    ld_usb_probe,
0792     .disconnect =   ld_usb_disconnect,
0793     .id_table = ld_usb_table,
0794 };
0795 
0796 module_usb_driver(ld_usb_driver);
0797