0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
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
0033 #define USB_VENDOR_ID_LD 0x0f11
0034 #define USB_DEVICE_ID_LD_CASSY 0x1000
0035 #define USB_DEVICE_ID_LD_CASSY2 0x1001
0036 #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010
0037 #define USB_DEVICE_ID_LD_POCKETCASSY2 0x1011
0038 #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020
0039 #define USB_DEVICE_ID_LD_MOBILECASSY2 0x1021
0040 #define USB_DEVICE_ID_LD_MICROCASSYVOLTAGE 0x1031
0041 #define USB_DEVICE_ID_LD_MICROCASSYCURRENT 0x1032
0042 #define USB_DEVICE_ID_LD_MICROCASSYTIME 0x1033
0043 #define USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE 0x1035
0044 #define USB_DEVICE_ID_LD_MICROCASSYPH 0x1038
0045 #define USB_DEVICE_ID_LD_POWERANALYSERCASSY 0x1040
0046 #define USB_DEVICE_ID_LD_CONVERTERCONTROLLERCASSY 0x1042
0047 #define USB_DEVICE_ID_LD_MACHINETESTCASSY 0x1043
0048 #define USB_DEVICE_ID_LD_JWM 0x1080
0049 #define USB_DEVICE_ID_LD_DMMP 0x1081
0050 #define USB_DEVICE_ID_LD_UMIP 0x1090
0051 #define USB_DEVICE_ID_LD_UMIC 0x10A0
0052 #define USB_DEVICE_ID_LD_UMIB 0x10B0
0053 #define USB_DEVICE_ID_LD_XRAY 0x1100
0054 #define USB_DEVICE_ID_LD_XRAY2 0x1101
0055 #define USB_DEVICE_ID_LD_XRAYCT 0x1110
0056 #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200
0057 #define USB_DEVICE_ID_LD_MOTOR 0x1210
0058 #define USB_DEVICE_ID_LD_COM3LAB 0x2000
0059 #define USB_DEVICE_ID_LD_TELEPORT 0x2010
0060 #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020
0061 #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030
0062 #define USB_DEVICE_ID_LD_MACHINETEST 0x2040
0063 #define USB_DEVICE_ID_LD_MOSTANALYSER 0x2050
0064 #define USB_DEVICE_ID_LD_MOSTANALYSER2 0x2051
0065 #define USB_DEVICE_ID_LD_ABSESP 0x2060
0066 #define USB_DEVICE_ID_LD_AUTODATABUS 0x2070
0067 #define USB_DEVICE_ID_LD_MCT 0x2080
0068 #define USB_DEVICE_ID_LD_HYBRID 0x2090
0069 #define USB_DEVICE_ID_LD_HEATCONTROL 0x20A0
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
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 { }
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
0122
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
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
0135
0136
0137
0138
0139
0140
0141
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
0152 struct ld_usb {
0153 struct mutex mutex;
0154 struct usb_interface *intf;
0155 unsigned long disconnected:1;
0156
0157 int open_count;
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
0188
0189
0190 static void ld_usb_abort_transfers(struct ld_usb *dev)
0191 {
0192
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
0203
0204 static void ld_usb_delete(struct ld_usb *dev)
0205 {
0206
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
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;
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
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
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
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
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
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
0323 if (mutex_lock_interruptible(&dev->mutex))
0324 return -ERESTARTSYS;
0325
0326
0327 if (dev->open_count) {
0328 retval = -EBUSY;
0329 goto unlock_exit;
0330 }
0331 dev->open_count = 1;
0332
0333
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
0359 file->private_data = dev;
0360
0361 unlock_exit:
0362 mutex_unlock(&dev->mutex);
0363
0364 return retval;
0365 }
0366
0367
0368
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
0390 mutex_unlock(&dev->mutex);
0391
0392 ld_usb_delete(dev);
0393 goto exit;
0394 }
0395
0396
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
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
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
0448 if (count == 0)
0449 goto exit;
0450
0451
0452 if (mutex_lock_interruptible(&dev->mutex)) {
0453 retval = -ERESTARTSYS;
0454 goto exit;
0455 }
0456
0457
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
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
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
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
0514 mutex_unlock(&dev->mutex);
0515
0516 exit:
0517 return retval;
0518 }
0519
0520
0521
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
0533 if (count == 0)
0534 goto exit;
0535
0536
0537 if (mutex_lock_interruptible(&dev->mutex)) {
0538 retval = -ERESTARTSYS;
0539 goto exit;
0540 }
0541
0542
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
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
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
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
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
0616 mutex_unlock(&dev->mutex);
0617
0618 exit:
0619 return retval;
0620 }
0621
0622
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
0635
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
0645
0646
0647
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
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
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
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
0726 usb_set_intfdata(intf, dev);
0727
0728 retval = usb_register_dev(intf, &ld_usb_class);
0729 if (retval) {
0730
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
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
0751
0752
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
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
0773 if (!dev->open_count) {
0774 mutex_unlock(&dev->mutex);
0775 ld_usb_delete(dev);
0776 } else {
0777 dev->disconnected = 1;
0778
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
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