Lines Matching +full:micro +full:- +full:usb
1 // SPDX-License-Identifier: GPL-2.0+
3 * Generic USB driver for report based interrupt in/out devices
4 * like LD Didactic's USB devices. LD Didactic's USB devices are
11 * Devices based on the book USB COMPLETE by Jan Axelson may need
14 * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
16 * Derived from Lego USB Tower driver
18 * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
29 #include <linux/usb.h>
33 #define USB_VENDOR_ID_LD 0x0f11 /* USB Vendor ID of LD Didactic GmbH */
34 #define USB_DEVICE_ID_LD_CASSY 0x1000 /* USB Product ID of CASSY-S modules with 8 bytes endpoint s…
35 #define USB_DEVICE_ID_LD_CASSY2 0x1001 /* USB Product ID of CASSY-S modules with 64 bytes endpoint…
36 #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010 /* USB Product ID of Pocket-CASSY */
37 #define USB_DEVICE_ID_LD_POCKETCASSY2 0x1011 /* USB Product ID of Pocket-CASSY 2 (reserved) */
38 #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020 /* USB Product ID of Mobile-CASSY */
39 #define USB_DEVICE_ID_LD_MOBILECASSY2 0x1021 /* USB Product ID of Mobile-CASSY 2 (reserved) */
40 #define USB_DEVICE_ID_LD_MICROCASSYVOLTAGE 0x1031 /* USB Product ID of Micro-CASSY Voltage */
41 #define USB_DEVICE_ID_LD_MICROCASSYCURRENT 0x1032 /* USB Product ID of Micro-CASSY Current */
42 #define USB_DEVICE_ID_LD_MICROCASSYTIME 0x1033 /* USB Product ID of Micro-CASSY Time (reserved) */
43 #define USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE 0x1035 /* USB Product ID of Micro-CASSY Temperature …
44 #define USB_DEVICE_ID_LD_MICROCASSYPH 0x1038 /* USB Product ID of Micro-CASSY pH */
45 #define USB_DEVICE_ID_LD_POWERANALYSERCASSY 0x1040 /* USB Product ID of Power Analyser CASSY */
46 #define USB_DEVICE_ID_LD_CONVERTERCONTROLLERCASSY 0x1042 /* USB Product ID of Converter Controller …
47 #define USB_DEVICE_ID_LD_MACHINETESTCASSY 0x1043 /* USB Product ID of Machine Test CASSY */
48 #define USB_DEVICE_ID_LD_JWM 0x1080 /* USB Product ID of Joule and Wattmeter */
49 #define USB_DEVICE_ID_LD_DMMP 0x1081 /* USB Product ID of Digital Multimeter P (reserved) */
50 #define USB_DEVICE_ID_LD_UMIP 0x1090 /* USB Product ID of UMI P */
51 #define USB_DEVICE_ID_LD_UMIC 0x10A0 /* USB Product ID of UMI C */
52 #define USB_DEVICE_ID_LD_UMIB 0x10B0 /* USB Product ID of UMI B */
53 #define USB_DEVICE_ID_LD_XRAY 0x1100 /* USB Product ID of X-Ray Apparatus 55481 */
54 #define USB_DEVICE_ID_LD_XRAY2 0x1101 /* USB Product ID of X-Ray Apparatus 554800 */
55 #define USB_DEVICE_ID_LD_XRAYCT 0x1110 /* USB Product ID of X-Ray Apparatus CT 554821*/
56 #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200 /* USB Product ID of VideoCom */
57 #define USB_DEVICE_ID_LD_MOTOR 0x1210 /* USB Product ID of Motor (reserved) */
58 #define USB_DEVICE_ID_LD_COM3LAB 0x2000 /* USB Product ID of COM3LAB */
59 #define USB_DEVICE_ID_LD_TELEPORT 0x2010 /* USB Product ID of Terminal Adapter */
60 #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */
61 #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030 /* USB Product ID of Converter Control Unit */
62 #define USB_DEVICE_ID_LD_MACHINETEST 0x2040 /* USB Product ID of Machine Test System */
63 #define USB_DEVICE_ID_LD_MOSTANALYSER 0x2050 /* USB Product ID of MOST Protocol Analyser */
64 #define USB_DEVICE_ID_LD_MOSTANALYSER2 0x2051 /* USB Product ID of MOST Protocol Analyser 2 */
65 #define USB_DEVICE_ID_LD_ABSESP 0x2060 /* USB Product ID of ABS ESP */
66 #define USB_DEVICE_ID_LD_AUTODATABUS 0x2070 /* USB Product ID of Automotive Data Buses */
67 #define USB_DEVICE_ID_LD_MCT 0x2080 /* USB Product ID of Microcontroller technique */
68 #define USB_DEVICE_ID_LD_HYBRID 0x2090 /* USB Product ID of Automotive Hybrid */
69 #define USB_DEVICE_ID_LD_HEATCONTROL 0x20A0 /* USB Product ID of Heat control */
116 MODULE_DEVICE_TABLE(usb, ld_usb_table);
117 MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>");
118 MODULE_DESCRIPTION("LD USB Driver");
134 /* As of kernel version 2.6.4 ehci-hcd uses an
154 struct usb_interface *intf; /* save off the usb interface pointer */
193 if (dev->interrupt_in_running) { in ld_usb_abort_transfers()
194 dev->interrupt_in_running = 0; in ld_usb_abort_transfers()
195 usb_kill_urb(dev->interrupt_in_urb); in ld_usb_abort_transfers()
197 if (dev->interrupt_out_busy) in ld_usb_abort_transfers()
198 usb_kill_urb(dev->interrupt_out_urb); in ld_usb_abort_transfers()
207 usb_free_urb(dev->interrupt_in_urb); in ld_usb_delete()
208 usb_free_urb(dev->interrupt_out_urb); in ld_usb_delete()
209 kfree(dev->ring_buffer); in ld_usb_delete()
210 kfree(dev->interrupt_in_buffer); in ld_usb_delete()
211 kfree(dev->interrupt_out_buffer); in ld_usb_delete()
220 struct ld_usb *dev = urb->context; in ld_usb_interrupt_in_callback()
223 int status = urb->status; in ld_usb_interrupt_in_callback()
228 if (status == -ENOENT || in ld_usb_interrupt_in_callback()
229 status == -ECONNRESET || in ld_usb_interrupt_in_callback()
230 status == -ESHUTDOWN) { in ld_usb_interrupt_in_callback()
233 dev_dbg(&dev->intf->dev, in ld_usb_interrupt_in_callback()
236 spin_lock_irqsave(&dev->rbsl, flags); in ld_usb_interrupt_in_callback()
241 spin_lock_irqsave(&dev->rbsl, flags); in ld_usb_interrupt_in_callback()
242 if (urb->actual_length > 0) { in ld_usb_interrupt_in_callback()
243 next_ring_head = (dev->ring_head+1) % ring_buffer_size; in ld_usb_interrupt_in_callback()
244 if (next_ring_head != dev->ring_tail) { in ld_usb_interrupt_in_callback()
245 …actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_head * (sizeof(size_t)+dev->interrupt_in_e… in ld_usb_interrupt_in_callback()
246 /* actual_buffer gets urb->actual_length + interrupt_in_buffer */ in ld_usb_interrupt_in_callback()
247 *actual_buffer = urb->actual_length; in ld_usb_interrupt_in_callback()
248 memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length); in ld_usb_interrupt_in_callback()
249 dev->ring_head = next_ring_head; in ld_usb_interrupt_in_callback()
250 dev_dbg(&dev->intf->dev, "%s: received %d bytes\n", in ld_usb_interrupt_in_callback()
251 __func__, urb->actual_length); in ld_usb_interrupt_in_callback()
253 dev_warn(&dev->intf->dev, in ld_usb_interrupt_in_callback()
255 urb->actual_length); in ld_usb_interrupt_in_callback()
256 dev->buffer_overflow = 1; in ld_usb_interrupt_in_callback()
262 if (dev->interrupt_in_running && !dev->buffer_overflow) { in ld_usb_interrupt_in_callback()
263 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); in ld_usb_interrupt_in_callback()
265 dev_err(&dev->intf->dev, in ld_usb_interrupt_in_callback()
267 dev->buffer_overflow = 1; in ld_usb_interrupt_in_callback()
270 spin_unlock_irqrestore(&dev->rbsl, flags); in ld_usb_interrupt_in_callback()
272 dev->interrupt_in_done = 1; in ld_usb_interrupt_in_callback()
273 wake_up_interruptible(&dev->read_wait); in ld_usb_interrupt_in_callback()
281 struct ld_usb *dev = urb->context; in ld_usb_interrupt_out_callback()
282 int status = urb->status; in ld_usb_interrupt_out_callback()
285 if (status && !(status == -ENOENT || in ld_usb_interrupt_out_callback()
286 status == -ECONNRESET || in ld_usb_interrupt_out_callback()
287 status == -ESHUTDOWN)) in ld_usb_interrupt_out_callback()
288 dev_dbg(&dev->intf->dev, in ld_usb_interrupt_out_callback()
289 "%s - nonzero write interrupt status received: %d\n", in ld_usb_interrupt_out_callback()
292 dev->interrupt_out_busy = 0; in ld_usb_interrupt_out_callback()
293 wake_up_interruptible(&dev->write_wait); in ld_usb_interrupt_out_callback()
312 printk(KERN_ERR "%s - error, can't find device for minor %d\n", in ld_usb_open()
314 return -ENODEV; in ld_usb_open()
320 return -ENODEV; in ld_usb_open()
323 if (mutex_lock_interruptible(&dev->mutex)) in ld_usb_open()
324 return -ERESTARTSYS; in ld_usb_open()
327 if (dev->open_count) { in ld_usb_open()
328 retval = -EBUSY; in ld_usb_open()
331 dev->open_count = 1; in ld_usb_open()
334 dev->ring_head = 0; in ld_usb_open()
335 dev->ring_tail = 0; in ld_usb_open()
336 dev->buffer_overflow = 0; in ld_usb_open()
337 usb_fill_int_urb(dev->interrupt_in_urb, in ld_usb_open()
340 dev->interrupt_in_endpoint->bEndpointAddress), in ld_usb_open()
341 dev->interrupt_in_buffer, in ld_usb_open()
342 dev->interrupt_in_endpoint_size, in ld_usb_open()
345 dev->interrupt_in_interval); in ld_usb_open()
347 dev->interrupt_in_running = 1; in ld_usb_open()
348 dev->interrupt_in_done = 0; in ld_usb_open()
350 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); in ld_usb_open()
352 dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval); in ld_usb_open()
353 dev->interrupt_in_running = 0; in ld_usb_open()
354 dev->open_count = 0; in ld_usb_open()
359 file->private_data = dev; in ld_usb_open()
362 mutex_unlock(&dev->mutex); in ld_usb_open()
375 dev = file->private_data; in ld_usb_release()
378 retval = -ENODEV; in ld_usb_release()
382 mutex_lock(&dev->mutex); in ld_usb_release()
384 if (dev->open_count != 1) { in ld_usb_release()
385 retval = -ENODEV; in ld_usb_release()
388 if (dev->disconnected) { in ld_usb_release()
390 mutex_unlock(&dev->mutex); in ld_usb_release()
397 if (dev->interrupt_out_busy) in ld_usb_release()
398 wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ); in ld_usb_release()
400 dev->open_count = 0; in ld_usb_release()
403 mutex_unlock(&dev->mutex); in ld_usb_release()
417 dev = file->private_data; in ld_usb_poll()
419 if (dev->disconnected) in ld_usb_poll()
422 poll_wait(file, &dev->read_wait, wait); in ld_usb_poll()
423 poll_wait(file, &dev->write_wait, wait); in ld_usb_poll()
425 if (dev->ring_head != dev->ring_tail) in ld_usb_poll()
427 if (!dev->interrupt_out_busy) in ld_usb_poll()
445 dev = file->private_data; in ld_usb_read()
452 if (mutex_lock_interruptible(&dev->mutex)) { in ld_usb_read()
453 retval = -ERESTARTSYS; in ld_usb_read()
458 if (dev->disconnected) { in ld_usb_read()
459 retval = -ENODEV; in ld_usb_read()
465 spin_lock_irq(&dev->rbsl); in ld_usb_read()
466 while (dev->ring_head == dev->ring_tail) { in ld_usb_read()
467 dev->interrupt_in_done = 0; in ld_usb_read()
468 spin_unlock_irq(&dev->rbsl); in ld_usb_read()
469 if (file->f_flags & O_NONBLOCK) { in ld_usb_read()
470 retval = -EAGAIN; in ld_usb_read()
473 retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done); in ld_usb_read()
477 spin_lock_irq(&dev->rbsl); in ld_usb_read()
479 spin_unlock_irq(&dev->rbsl); in ld_usb_read()
482 …actual_buffer = (size_t *)(dev->ring_buffer + dev->ring_tail * (sizeof(size_t)+dev->interrupt_in_e… in ld_usb_read()
483 if (*actual_buffer > dev->interrupt_in_endpoint_size) { in ld_usb_read()
484 retval = -EIO; in ld_usb_read()
489 dev_warn(&dev->intf->dev, "Read buffer overflow, %zu bytes dropped\n", in ld_usb_read()
490 *actual_buffer-bytes_to_read); in ld_usb_read()
494 retval = -EFAULT; in ld_usb_read()
499 spin_lock_irq(&dev->rbsl); in ld_usb_read()
500 dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size; in ld_usb_read()
502 if (dev->buffer_overflow) { in ld_usb_read()
503 dev->buffer_overflow = 0; in ld_usb_read()
504 spin_unlock_irq(&dev->rbsl); in ld_usb_read()
505 rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); in ld_usb_read()
507 dev->buffer_overflow = 1; in ld_usb_read()
509 spin_unlock_irq(&dev->rbsl); in ld_usb_read()
514 mutex_unlock(&dev->mutex); in ld_usb_read()
530 dev = file->private_data; in ld_usb_write()
537 if (mutex_lock_interruptible(&dev->mutex)) { in ld_usb_write()
538 retval = -ERESTARTSYS; in ld_usb_write()
543 if (dev->disconnected) { in ld_usb_write()
544 retval = -ENODEV; in ld_usb_write()
550 if (dev->interrupt_out_busy) { in ld_usb_write()
551 if (file->f_flags & O_NONBLOCK) { in ld_usb_write()
552 retval = -EAGAIN; in ld_usb_write()
555 retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy); in ld_usb_write()
562 bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size); in ld_usb_write()
564 dev_warn(&dev->intf->dev, "Write buffer overflow, %zu bytes dropped\n", in ld_usb_write()
565 count - bytes_to_write); in ld_usb_write()
566 dev_dbg(&dev->intf->dev, "%s: count = %zu, bytes_to_write = %zu\n", in ld_usb_write()
569 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) { in ld_usb_write()
570 retval = -EFAULT; in ld_usb_write()
574 if (dev->interrupt_out_endpoint == NULL) { in ld_usb_write()
576 retval = usb_control_msg(interface_to_usbdev(dev->intf), in ld_usb_write()
577 usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0), in ld_usb_write()
581 dev->interrupt_out_buffer, in ld_usb_write()
585 dev_err(&dev->intf->dev, in ld_usb_write()
592 usb_fill_int_urb(dev->interrupt_out_urb, in ld_usb_write()
593 interface_to_usbdev(dev->intf), in ld_usb_write()
594 usb_sndintpipe(interface_to_usbdev(dev->intf), in ld_usb_write()
595 dev->interrupt_out_endpoint->bEndpointAddress), in ld_usb_write()
596 dev->interrupt_out_buffer, in ld_usb_write()
600 dev->interrupt_out_interval); in ld_usb_write()
602 dev->interrupt_out_busy = 1; in ld_usb_write()
605 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); in ld_usb_write()
607 dev->interrupt_out_busy = 0; in ld_usb_write()
608 dev_err(&dev->intf->dev, in ld_usb_write()
616 mutex_unlock(&dev->mutex); in ld_usb_write()
633 * usb class driver info in order to get a minor number from the usb core,
645 * Called by the usb core when a new device is connected that it thinks
654 int retval = -ENOMEM; in ld_usb_probe()
662 mutex_init(&dev->mutex); in ld_usb_probe()
663 spin_lock_init(&dev->rbsl); in ld_usb_probe()
664 dev->intf = intf; in ld_usb_probe()
665 init_waitqueue_head(&dev->read_wait); in ld_usb_probe()
666 init_waitqueue_head(&dev->write_wait); in ld_usb_probe()
669 if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) && in ld_usb_probe()
670 ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) || in ld_usb_probe()
671 (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) && in ld_usb_probe()
672 (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) { in ld_usb_probe()
681 iface_desc = intf->cur_altsetting; in ld_usb_probe()
684 &dev->interrupt_in_endpoint); in ld_usb_probe()
686 dev_err(&intf->dev, "Interrupt in endpoint not found\n"); in ld_usb_probe()
692 &dev->interrupt_out_endpoint); in ld_usb_probe()
694 dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n"); in ld_usb_probe()
696 dev->interrupt_in_endpoint_size = usb_endpoint_maxp(dev->interrupt_in_endpoint); in ld_usb_probe()
697 dev->ring_buffer = kcalloc(ring_buffer_size, in ld_usb_probe()
698 sizeof(size_t) + dev->interrupt_in_endpoint_size, in ld_usb_probe()
700 if (!dev->ring_buffer) in ld_usb_probe()
702 dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL); in ld_usb_probe()
703 if (!dev->interrupt_in_buffer) in ld_usb_probe()
705 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); in ld_usb_probe()
706 if (!dev->interrupt_in_urb) in ld_usb_probe()
708 …dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? usb_endpoint_maxp(dev->interrupt_… in ld_usb_probe()
709 udev->descriptor.bMaxPacketSize0; in ld_usb_probe()
710 dev->interrupt_out_buffer = in ld_usb_probe()
712 dev->interrupt_out_endpoint_size, GFP_KERNEL); in ld_usb_probe()
713 if (!dev->interrupt_out_buffer) in ld_usb_probe()
715 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); in ld_usb_probe()
716 if (!dev->interrupt_out_urb) in ld_usb_probe()
718 dev->interrupt_in_interval = max_t(int, min_interrupt_in_interval, in ld_usb_probe()
719 dev->interrupt_in_endpoint->bInterval); in ld_usb_probe()
720 if (dev->interrupt_out_endpoint) in ld_usb_probe()
721 dev->interrupt_out_interval = max_t(int, min_interrupt_out_interval, in ld_usb_probe()
722 dev->interrupt_out_endpoint->bInterval); in ld_usb_probe()
730 dev_err(&intf->dev, "Not able to get a minor for this device.\n"); in ld_usb_probe()
736 dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n", in ld_usb_probe()
737 (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor); in ld_usb_probe()
751 * Called by the usb core when the device is removed from the system.
761 minor = intf->minor; in ld_usb_disconnect()
766 usb_poison_urb(dev->interrupt_in_urb); in ld_usb_disconnect()
767 usb_poison_urb(dev->interrupt_out_urb); in ld_usb_disconnect()
769 mutex_lock(&dev->mutex); in ld_usb_disconnect()
772 if (!dev->open_count) { in ld_usb_disconnect()
773 mutex_unlock(&dev->mutex); in ld_usb_disconnect()
776 dev->disconnected = 1; in ld_usb_disconnect()
778 wake_up_interruptible_all(&dev->read_wait); in ld_usb_disconnect()
779 wake_up_interruptible_all(&dev->write_wait); in ld_usb_disconnect()
780 mutex_unlock(&dev->mutex); in ld_usb_disconnect()
783 dev_info(&intf->dev, "LD USB Device #%d now disconnected\n", in ld_usb_disconnect()
784 (minor - USB_LD_MINOR_BASE)); in ld_usb_disconnect()
787 /* usb specific object needed to register this driver with the usb subsystem */