12824bd25SMichael Hund /** 22824bd25SMichael Hund * Generic USB driver for report based interrupt in/out devices 32824bd25SMichael Hund * like LD Didactic's USB devices. LD Didactic's USB devices are 42824bd25SMichael Hund * HID devices which do not use HID report definitons (they use 52824bd25SMichael Hund * raw interrupt in and our reports only for communication). 62824bd25SMichael Hund * 72824bd25SMichael Hund * This driver uses a ring buffer for time critical reading of 82824bd25SMichael Hund * interrupt in reports and provides read and write methods for 92824bd25SMichael Hund * raw interrupt reports (similar to the Windows HID driver). 102824bd25SMichael Hund * Devices based on the book USB COMPLETE by Jan Axelson may need 112824bd25SMichael Hund * such a compatibility to the Windows HID driver. 122824bd25SMichael Hund * 132824bd25SMichael Hund * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de> 142824bd25SMichael Hund * 152824bd25SMichael Hund * This program is free software; you can redistribute it and/or 162824bd25SMichael Hund * modify it under the terms of the GNU General Public License as 172824bd25SMichael Hund * published by the Free Software Foundation; either version 2 of 182824bd25SMichael Hund * the License, or (at your option) any later version. 192824bd25SMichael Hund * 202824bd25SMichael Hund * Derived from Lego USB Tower driver 212824bd25SMichael Hund * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net> 222824bd25SMichael Hund * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net> 232824bd25SMichael Hund * 242824bd25SMichael Hund * V0.1 (mh) Initial version 252824bd25SMichael Hund * V0.11 (mh) Added raw support for HID 1.0 devices (no interrupt out endpoint) 26a6db592eSMichael Hund * V0.12 (mh) Added kmalloc check for string buffer 27ba3e66e9SMichael Hund * V0.13 (mh) Added support for LD X-Ray and Machine Test System 282824bd25SMichael Hund */ 292824bd25SMichael Hund 302824bd25SMichael Hund #include <linux/config.h> 312824bd25SMichael Hund #include <linux/kernel.h> 322824bd25SMichael Hund #include <linux/errno.h> 332824bd25SMichael Hund #include <linux/init.h> 342824bd25SMichael Hund #include <linux/slab.h> 352824bd25SMichael Hund #include <linux/module.h> 362824bd25SMichael Hund 372824bd25SMichael Hund #include <asm/uaccess.h> 382824bd25SMichael Hund #include <linux/input.h> 392824bd25SMichael Hund #include <linux/usb.h> 402824bd25SMichael Hund #include <linux/poll.h> 412824bd25SMichael Hund 422824bd25SMichael Hund /* Define these values to match your devices */ 432824bd25SMichael Hund #define USB_VENDOR_ID_LD 0x0f11 /* USB Vendor ID of LD Didactic GmbH */ 44ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_CASSY 0x1000 /* USB Product ID of CASSY-S */ 45ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_POCKETCASSY 0x1010 /* USB Product ID of Pocket-CASSY */ 46ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_MOBILECASSY 0x1020 /* USB Product ID of Mobile-CASSY */ 47ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_JWM 0x1080 /* USB Product ID of Joule and Wattmeter */ 48ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_DMMP 0x1081 /* USB Product ID of Digital Multimeter P (reserved) */ 49ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_UMIP 0x1090 /* USB Product ID of UMI P */ 50ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_XRAY1 0x1100 /* USB Product ID of X-Ray Apparatus */ 51ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_XRAY2 0x1101 /* USB Product ID of X-Ray Apparatus */ 52ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_VIDEOCOM 0x1200 /* USB Product ID of VideoCom */ 53ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_COM3LAB 0x2000 /* USB Product ID of COM3LAB */ 54ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_TELEPORT 0x2010 /* USB Product ID of Terminal Adapter */ 55ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */ 56ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_POWERCONTROL 0x2030 /* USB Product ID of Converter Control Unit */ 57ba3e66e9SMichael Hund #define USB_DEVICE_ID_LD_MACHINETEST 0x2040 /* USB Product ID of Machine Test System */ 582824bd25SMichael Hund 592824bd25SMichael Hund #define USB_VENDOR_ID_VERNIER 0x08f7 602824bd25SMichael Hund #define USB_DEVICE_ID_VERNIER_LABPRO 0x0001 612824bd25SMichael Hund #define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002 622824bd25SMichael Hund #define USB_DEVICE_ID_VERNIER_SKIP 0x0003 632824bd25SMichael Hund #define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004 642824bd25SMichael Hund 652824bd25SMichael Hund 662824bd25SMichael Hund #ifdef CONFIG_USB_DYNAMIC_MINORS 672824bd25SMichael Hund #define USB_LD_MINOR_BASE 0 682824bd25SMichael Hund #else 692824bd25SMichael Hund #define USB_LD_MINOR_BASE 176 702824bd25SMichael Hund #endif 712824bd25SMichael Hund 722824bd25SMichael Hund /* table of devices that work with this driver */ 732824bd25SMichael Hund static struct usb_device_id ld_usb_table [] = { 74ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) }, 75ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) }, 76ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) }, 77ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) }, 78ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) }, 79ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) }, 80ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY1) }, 81ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) }, 82ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) }, 83ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) }, 84ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) }, 85ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) }, 86ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) }, 87ba3e66e9SMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) }, 882824bd25SMichael Hund { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LABPRO) }, 892824bd25SMichael Hund { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) }, 902824bd25SMichael Hund { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) }, 912824bd25SMichael Hund { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) }, 922824bd25SMichael Hund { } /* Terminating entry */ 932824bd25SMichael Hund }; 942824bd25SMichael Hund MODULE_DEVICE_TABLE(usb, ld_usb_table); 95ba3e66e9SMichael Hund MODULE_VERSION("V0.13"); 962824bd25SMichael Hund MODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>"); 972824bd25SMichael Hund MODULE_DESCRIPTION("LD USB Driver"); 982824bd25SMichael Hund MODULE_LICENSE("GPL"); 992824bd25SMichael Hund MODULE_SUPPORTED_DEVICE("LD USB Devices"); 1002824bd25SMichael Hund 1012824bd25SMichael Hund #ifdef CONFIG_USB_DEBUG 1022824bd25SMichael Hund static int debug = 1; 1032824bd25SMichael Hund #else 1042824bd25SMichael Hund static int debug = 0; 1052824bd25SMichael Hund #endif 1062824bd25SMichael Hund 1072824bd25SMichael Hund /* Use our own dbg macro */ 1082824bd25SMichael Hund #define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0) 1092824bd25SMichael Hund 1102824bd25SMichael Hund /* Module parameters */ 1112824bd25SMichael Hund module_param(debug, int, S_IRUGO | S_IWUSR); 1122824bd25SMichael Hund MODULE_PARM_DESC(debug, "Debug enabled or not"); 1132824bd25SMichael Hund 1142824bd25SMichael Hund /* All interrupt in transfers are collected in a ring buffer to 1152824bd25SMichael Hund * avoid racing conditions and get better performance of the driver. 1162824bd25SMichael Hund */ 1172824bd25SMichael Hund static int ring_buffer_size = 128; 1182824bd25SMichael Hund module_param(ring_buffer_size, int, 0); 1192824bd25SMichael Hund MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports"); 1202824bd25SMichael Hund 1212824bd25SMichael Hund /* The write_buffer can contain more than one interrupt out transfer. 1222824bd25SMichael Hund */ 1232824bd25SMichael Hund static int write_buffer_size = 10; 1242824bd25SMichael Hund module_param(write_buffer_size, int, 0); 1252824bd25SMichael Hund MODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports"); 1262824bd25SMichael Hund 1272824bd25SMichael Hund /* As of kernel version 2.6.4 ehci-hcd uses an 1282824bd25SMichael Hund * "only one interrupt transfer per frame" shortcut 1292824bd25SMichael Hund * to simplify the scheduling of periodic transfers. 1302824bd25SMichael Hund * This conflicts with our standard 1ms intervals for in and out URBs. 1312824bd25SMichael Hund * We use default intervals of 2ms for in and 2ms for out transfers, 1322824bd25SMichael Hund * which should be fast enough. 1332824bd25SMichael Hund * Increase the interval to allow more devices that do interrupt transfers, 1342824bd25SMichael Hund * or set to 1 to use the standard interval from the endpoint descriptors. 1352824bd25SMichael Hund */ 1362824bd25SMichael Hund static int min_interrupt_in_interval = 2; 1372824bd25SMichael Hund module_param(min_interrupt_in_interval, int, 0); 1382824bd25SMichael Hund MODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms"); 1392824bd25SMichael Hund 1402824bd25SMichael Hund static int min_interrupt_out_interval = 2; 1412824bd25SMichael Hund module_param(min_interrupt_out_interval, int, 0); 1422824bd25SMichael Hund MODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms"); 1432824bd25SMichael Hund 1442824bd25SMichael Hund /* Structure to hold all of our device specific stuff */ 1452824bd25SMichael Hund struct ld_usb { 1462824bd25SMichael Hund struct semaphore sem; /* locks this structure */ 1472824bd25SMichael Hund struct usb_interface* intf; /* save off the usb interface pointer */ 1482824bd25SMichael Hund 1492824bd25SMichael Hund int open_count; /* number of times this port has been opened */ 1502824bd25SMichael Hund 1512824bd25SMichael Hund char* ring_buffer; 1522824bd25SMichael Hund unsigned int ring_head; 1532824bd25SMichael Hund unsigned int ring_tail; 1542824bd25SMichael Hund 1552824bd25SMichael Hund wait_queue_head_t read_wait; 1562824bd25SMichael Hund wait_queue_head_t write_wait; 1572824bd25SMichael Hund 1582824bd25SMichael Hund char* interrupt_in_buffer; 1592824bd25SMichael Hund struct usb_endpoint_descriptor* interrupt_in_endpoint; 1602824bd25SMichael Hund struct urb* interrupt_in_urb; 1612824bd25SMichael Hund int interrupt_in_interval; 1622824bd25SMichael Hund size_t interrupt_in_endpoint_size; 1632824bd25SMichael Hund int interrupt_in_running; 1642824bd25SMichael Hund int interrupt_in_done; 1652824bd25SMichael Hund 1662824bd25SMichael Hund char* interrupt_out_buffer; 1672824bd25SMichael Hund struct usb_endpoint_descriptor* interrupt_out_endpoint; 1682824bd25SMichael Hund struct urb* interrupt_out_urb; 1692824bd25SMichael Hund int interrupt_out_interval; 1702824bd25SMichael Hund size_t interrupt_out_endpoint_size; 1712824bd25SMichael Hund int interrupt_out_busy; 1722824bd25SMichael Hund }; 1732824bd25SMichael Hund 1742824bd25SMichael Hund /* prevent races between open() and disconnect() */ 1752824bd25SMichael Hund static DECLARE_MUTEX(disconnect_sem); 1762824bd25SMichael Hund 1772824bd25SMichael Hund static struct usb_driver ld_usb_driver; 1782824bd25SMichael Hund 1792824bd25SMichael Hund /** 1802824bd25SMichael Hund * ld_usb_abort_transfers 1812824bd25SMichael Hund * aborts transfers and frees associated data structures 1822824bd25SMichael Hund */ 1832824bd25SMichael Hund static void ld_usb_abort_transfers(struct ld_usb *dev) 1842824bd25SMichael Hund { 1852824bd25SMichael Hund /* shutdown transfer */ 1862824bd25SMichael Hund if (dev->interrupt_in_running) { 1872824bd25SMichael Hund dev->interrupt_in_running = 0; 1882824bd25SMichael Hund if (dev->intf) 1892824bd25SMichael Hund usb_kill_urb(dev->interrupt_in_urb); 1902824bd25SMichael Hund } 1912824bd25SMichael Hund if (dev->interrupt_out_busy) 1922824bd25SMichael Hund if (dev->intf) 1932824bd25SMichael Hund usb_kill_urb(dev->interrupt_out_urb); 1942824bd25SMichael Hund } 1952824bd25SMichael Hund 1962824bd25SMichael Hund /** 1972824bd25SMichael Hund * ld_usb_delete 1982824bd25SMichael Hund */ 1992824bd25SMichael Hund static void ld_usb_delete(struct ld_usb *dev) 2002824bd25SMichael Hund { 2012824bd25SMichael Hund ld_usb_abort_transfers(dev); 2022824bd25SMichael Hund 2032824bd25SMichael Hund /* free data structures */ 2042824bd25SMichael Hund usb_free_urb(dev->interrupt_in_urb); 2052824bd25SMichael Hund usb_free_urb(dev->interrupt_out_urb); 2062824bd25SMichael Hund kfree(dev->ring_buffer); 2072824bd25SMichael Hund kfree(dev->interrupt_in_buffer); 2082824bd25SMichael Hund kfree(dev->interrupt_out_buffer); 2092824bd25SMichael Hund kfree(dev); 2102824bd25SMichael Hund } 2112824bd25SMichael Hund 2122824bd25SMichael Hund /** 2132824bd25SMichael Hund * ld_usb_interrupt_in_callback 2142824bd25SMichael Hund */ 2152824bd25SMichael Hund static void ld_usb_interrupt_in_callback(struct urb *urb, struct pt_regs *regs) 2162824bd25SMichael Hund { 2172824bd25SMichael Hund struct ld_usb *dev = urb->context; 2182824bd25SMichael Hund size_t *actual_buffer; 2192824bd25SMichael Hund unsigned int next_ring_head; 2202824bd25SMichael Hund int retval; 2212824bd25SMichael Hund 2222824bd25SMichael Hund if (urb->status) { 2232824bd25SMichael Hund if (urb->status == -ENOENT || 2242824bd25SMichael Hund urb->status == -ECONNRESET || 2252824bd25SMichael Hund urb->status == -ESHUTDOWN) { 2262824bd25SMichael Hund goto exit; 2272824bd25SMichael Hund } else { 2282824bd25SMichael Hund dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n", 2292824bd25SMichael Hund __FUNCTION__, urb->status); 2302824bd25SMichael Hund goto resubmit; /* maybe we can recover */ 2312824bd25SMichael Hund } 2322824bd25SMichael Hund } 2332824bd25SMichael Hund 2342824bd25SMichael Hund if (urb->actual_length > 0) { 2352824bd25SMichael Hund next_ring_head = (dev->ring_head+1) % ring_buffer_size; 2362824bd25SMichael Hund if (next_ring_head != dev->ring_tail) { 2372824bd25SMichael Hund actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size)); 2382824bd25SMichael Hund /* actual_buffer gets urb->actual_length + interrupt_in_buffer */ 2392824bd25SMichael Hund *actual_buffer = urb->actual_length; 2402824bd25SMichael Hund memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length); 2412824bd25SMichael Hund dev->ring_head = next_ring_head; 2422824bd25SMichael Hund dbg_info(&dev->intf->dev, "%s: received %d bytes\n", 2432824bd25SMichael Hund __FUNCTION__, urb->actual_length); 2442824bd25SMichael Hund } else 2452824bd25SMichael Hund dev_warn(&dev->intf->dev, 2462824bd25SMichael Hund "Ring buffer overflow, %d bytes dropped\n", 2472824bd25SMichael Hund urb->actual_length); 2482824bd25SMichael Hund } 2492824bd25SMichael Hund 2502824bd25SMichael Hund resubmit: 2512824bd25SMichael Hund /* resubmit if we're still running */ 2522824bd25SMichael Hund if (dev->interrupt_in_running && dev->intf) { 2532824bd25SMichael Hund retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); 2542824bd25SMichael Hund if (retval) 2552824bd25SMichael Hund dev_err(&dev->intf->dev, 2562824bd25SMichael Hund "usb_submit_urb failed (%d)\n", retval); 2572824bd25SMichael Hund } 2582824bd25SMichael Hund 2592824bd25SMichael Hund exit: 2602824bd25SMichael Hund dev->interrupt_in_done = 1; 2612824bd25SMichael Hund wake_up_interruptible(&dev->read_wait); 2622824bd25SMichael Hund } 2632824bd25SMichael Hund 2642824bd25SMichael Hund /** 2652824bd25SMichael Hund * ld_usb_interrupt_out_callback 2662824bd25SMichael Hund */ 2672824bd25SMichael Hund static void ld_usb_interrupt_out_callback(struct urb *urb, struct pt_regs *regs) 2682824bd25SMichael Hund { 2692824bd25SMichael Hund struct ld_usb *dev = urb->context; 2702824bd25SMichael Hund 2712824bd25SMichael Hund /* sync/async unlink faults aren't errors */ 2722824bd25SMichael Hund if (urb->status && !(urb->status == -ENOENT || 2732824bd25SMichael Hund urb->status == -ECONNRESET || 2742824bd25SMichael Hund urb->status == -ESHUTDOWN)) 2752824bd25SMichael Hund dbg_info(&dev->intf->dev, 2762824bd25SMichael Hund "%s - nonzero write interrupt status received: %d\n", 2772824bd25SMichael Hund __FUNCTION__, urb->status); 2782824bd25SMichael Hund 2792824bd25SMichael Hund dev->interrupt_out_busy = 0; 2802824bd25SMichael Hund wake_up_interruptible(&dev->write_wait); 2812824bd25SMichael Hund } 2822824bd25SMichael Hund 2832824bd25SMichael Hund /** 2842824bd25SMichael Hund * ld_usb_open 2852824bd25SMichael Hund */ 2862824bd25SMichael Hund static int ld_usb_open(struct inode *inode, struct file *file) 2872824bd25SMichael Hund { 2882824bd25SMichael Hund struct ld_usb *dev; 2892824bd25SMichael Hund int subminor; 2902824bd25SMichael Hund int retval = 0; 2912824bd25SMichael Hund struct usb_interface *interface; 2922824bd25SMichael Hund 2932824bd25SMichael Hund nonseekable_open(inode, file); 2942824bd25SMichael Hund subminor = iminor(inode); 2952824bd25SMichael Hund 2962824bd25SMichael Hund down(&disconnect_sem); 2972824bd25SMichael Hund 2982824bd25SMichael Hund interface = usb_find_interface(&ld_usb_driver, subminor); 2992824bd25SMichael Hund 3002824bd25SMichael Hund if (!interface) { 3012824bd25SMichael Hund err("%s - error, can't find device for minor %d\n", 3022824bd25SMichael Hund __FUNCTION__, subminor); 3032824bd25SMichael Hund retval = -ENODEV; 3042824bd25SMichael Hund goto unlock_disconnect_exit; 3052824bd25SMichael Hund } 3062824bd25SMichael Hund 3072824bd25SMichael Hund dev = usb_get_intfdata(interface); 3082824bd25SMichael Hund 3092824bd25SMichael Hund if (!dev) { 3102824bd25SMichael Hund retval = -ENODEV; 3112824bd25SMichael Hund goto unlock_disconnect_exit; 3122824bd25SMichael Hund } 3132824bd25SMichael Hund 3142824bd25SMichael Hund /* lock this device */ 3152824bd25SMichael Hund if (down_interruptible(&dev->sem)) { 3162824bd25SMichael Hund retval = -ERESTARTSYS; 3172824bd25SMichael Hund goto unlock_disconnect_exit; 3182824bd25SMichael Hund } 3192824bd25SMichael Hund 3202824bd25SMichael Hund /* allow opening only once */ 3212824bd25SMichael Hund if (dev->open_count) { 3222824bd25SMichael Hund retval = -EBUSY; 3232824bd25SMichael Hund goto unlock_exit; 3242824bd25SMichael Hund } 3252824bd25SMichael Hund dev->open_count = 1; 3262824bd25SMichael Hund 3272824bd25SMichael Hund /* initialize in direction */ 3282824bd25SMichael Hund dev->ring_head = 0; 3292824bd25SMichael Hund dev->ring_tail = 0; 3302824bd25SMichael Hund usb_fill_int_urb(dev->interrupt_in_urb, 3312824bd25SMichael Hund interface_to_usbdev(interface), 3322824bd25SMichael Hund usb_rcvintpipe(interface_to_usbdev(interface), 3332824bd25SMichael Hund dev->interrupt_in_endpoint->bEndpointAddress), 3342824bd25SMichael Hund dev->interrupt_in_buffer, 3352824bd25SMichael Hund dev->interrupt_in_endpoint_size, 3362824bd25SMichael Hund ld_usb_interrupt_in_callback, 3372824bd25SMichael Hund dev, 3382824bd25SMichael Hund dev->interrupt_in_interval); 3392824bd25SMichael Hund 3402824bd25SMichael Hund dev->interrupt_in_running = 1; 3412824bd25SMichael Hund dev->interrupt_in_done = 0; 3422824bd25SMichael Hund 3432824bd25SMichael Hund retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 3442824bd25SMichael Hund if (retval) { 3452824bd25SMichael Hund dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval); 3462824bd25SMichael Hund dev->interrupt_in_running = 0; 3472824bd25SMichael Hund dev->open_count = 0; 3482824bd25SMichael Hund goto unlock_exit; 3492824bd25SMichael Hund } 3502824bd25SMichael Hund 3512824bd25SMichael Hund /* save device in the file's private structure */ 3522824bd25SMichael Hund file->private_data = dev; 3532824bd25SMichael Hund 3542824bd25SMichael Hund unlock_exit: 3552824bd25SMichael Hund up(&dev->sem); 3562824bd25SMichael Hund 3572824bd25SMichael Hund unlock_disconnect_exit: 3582824bd25SMichael Hund up(&disconnect_sem); 3592824bd25SMichael Hund 3602824bd25SMichael Hund return retval; 3612824bd25SMichael Hund } 3622824bd25SMichael Hund 3632824bd25SMichael Hund /** 3642824bd25SMichael Hund * ld_usb_release 3652824bd25SMichael Hund */ 3662824bd25SMichael Hund static int ld_usb_release(struct inode *inode, struct file *file) 3672824bd25SMichael Hund { 3682824bd25SMichael Hund struct ld_usb *dev; 3692824bd25SMichael Hund int retval = 0; 3702824bd25SMichael Hund 3712824bd25SMichael Hund dev = file->private_data; 3722824bd25SMichael Hund 3732824bd25SMichael Hund if (dev == NULL) { 3742824bd25SMichael Hund retval = -ENODEV; 3752824bd25SMichael Hund goto exit; 3762824bd25SMichael Hund } 3772824bd25SMichael Hund 3782824bd25SMichael Hund if (down_interruptible(&dev->sem)) { 3792824bd25SMichael Hund retval = -ERESTARTSYS; 3802824bd25SMichael Hund goto exit; 3812824bd25SMichael Hund } 3822824bd25SMichael Hund 3832824bd25SMichael Hund if (dev->open_count != 1) { 3842824bd25SMichael Hund retval = -ENODEV; 3852824bd25SMichael Hund goto unlock_exit; 3862824bd25SMichael Hund } 3872824bd25SMichael Hund if (dev->intf == NULL) { 3882824bd25SMichael Hund /* the device was unplugged before the file was released */ 3892824bd25SMichael Hund up(&dev->sem); 3902824bd25SMichael Hund /* unlock here as ld_usb_delete frees dev */ 3912824bd25SMichael Hund ld_usb_delete(dev); 3922824bd25SMichael Hund goto exit; 3932824bd25SMichael Hund } 3942824bd25SMichael Hund 3952824bd25SMichael Hund /* wait until write transfer is finished */ 3962824bd25SMichael Hund if (dev->interrupt_out_busy) 3972824bd25SMichael Hund wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ); 3982824bd25SMichael Hund ld_usb_abort_transfers(dev); 3992824bd25SMichael Hund dev->open_count = 0; 4002824bd25SMichael Hund 4012824bd25SMichael Hund unlock_exit: 4022824bd25SMichael Hund up(&dev->sem); 4032824bd25SMichael Hund 4042824bd25SMichael Hund exit: 4052824bd25SMichael Hund return retval; 4062824bd25SMichael Hund } 4072824bd25SMichael Hund 4082824bd25SMichael Hund /** 4092824bd25SMichael Hund * ld_usb_poll 4102824bd25SMichael Hund */ 4112824bd25SMichael Hund static unsigned int ld_usb_poll(struct file *file, poll_table *wait) 4122824bd25SMichael Hund { 4132824bd25SMichael Hund struct ld_usb *dev; 4142824bd25SMichael Hund unsigned int mask = 0; 4152824bd25SMichael Hund 4162824bd25SMichael Hund dev = file->private_data; 4172824bd25SMichael Hund 4182824bd25SMichael Hund poll_wait(file, &dev->read_wait, wait); 4192824bd25SMichael Hund poll_wait(file, &dev->write_wait, wait); 4202824bd25SMichael Hund 4212824bd25SMichael Hund if (dev->ring_head != dev->ring_tail) 4222824bd25SMichael Hund mask |= POLLIN | POLLRDNORM; 4232824bd25SMichael Hund if (!dev->interrupt_out_busy) 4242824bd25SMichael Hund mask |= POLLOUT | POLLWRNORM; 4252824bd25SMichael Hund 4262824bd25SMichael Hund return mask; 4272824bd25SMichael Hund } 4282824bd25SMichael Hund 4292824bd25SMichael Hund /** 4302824bd25SMichael Hund * ld_usb_read 4312824bd25SMichael Hund */ 4322824bd25SMichael Hund static ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count, 4332824bd25SMichael Hund loff_t *ppos) 4342824bd25SMichael Hund { 4352824bd25SMichael Hund struct ld_usb *dev; 4362824bd25SMichael Hund size_t *actual_buffer; 4372824bd25SMichael Hund size_t bytes_to_read; 4382824bd25SMichael Hund int retval = 0; 4392824bd25SMichael Hund 4402824bd25SMichael Hund dev = file->private_data; 4412824bd25SMichael Hund 4422824bd25SMichael Hund /* verify that we actually have some data to read */ 4432824bd25SMichael Hund if (count == 0) 4442824bd25SMichael Hund goto exit; 4452824bd25SMichael Hund 4462824bd25SMichael Hund /* lock this object */ 4472824bd25SMichael Hund if (down_interruptible(&dev->sem)) { 4482824bd25SMichael Hund retval = -ERESTARTSYS; 4492824bd25SMichael Hund goto exit; 4502824bd25SMichael Hund } 4512824bd25SMichael Hund 4522824bd25SMichael Hund /* verify that the device wasn't unplugged */ 4532824bd25SMichael Hund if (dev->intf == NULL) { 4542824bd25SMichael Hund retval = -ENODEV; 4552824bd25SMichael Hund err("No device or device unplugged %d\n", retval); 4562824bd25SMichael Hund goto unlock_exit; 4572824bd25SMichael Hund } 4582824bd25SMichael Hund 4592824bd25SMichael Hund /* wait for data */ 4602824bd25SMichael Hund if (dev->ring_head == dev->ring_tail) { 4612824bd25SMichael Hund if (file->f_flags & O_NONBLOCK) { 4622824bd25SMichael Hund retval = -EAGAIN; 4632824bd25SMichael Hund goto unlock_exit; 4642824bd25SMichael Hund } 4652824bd25SMichael Hund retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done); 4662824bd25SMichael Hund if (retval < 0) 4672824bd25SMichael Hund goto unlock_exit; 4682824bd25SMichael Hund } 4692824bd25SMichael Hund 4702824bd25SMichael Hund /* actual_buffer contains actual_length + interrupt_in_buffer */ 4712824bd25SMichael Hund actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size)); 4722824bd25SMichael Hund bytes_to_read = min(count, *actual_buffer); 4732824bd25SMichael Hund if (bytes_to_read < *actual_buffer) 474dd7d5008SAlexey Dobriyan dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n", 4752824bd25SMichael Hund *actual_buffer-bytes_to_read); 4762824bd25SMichael Hund 4772824bd25SMichael Hund /* copy one interrupt_in_buffer from ring_buffer into userspace */ 4782824bd25SMichael Hund if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) { 4792824bd25SMichael Hund retval = -EFAULT; 4802824bd25SMichael Hund goto unlock_exit; 4812824bd25SMichael Hund } 4822824bd25SMichael Hund dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size; 4832824bd25SMichael Hund 4842824bd25SMichael Hund retval = bytes_to_read; 4852824bd25SMichael Hund 4862824bd25SMichael Hund unlock_exit: 4872824bd25SMichael Hund /* unlock the device */ 4882824bd25SMichael Hund up(&dev->sem); 4892824bd25SMichael Hund 4902824bd25SMichael Hund exit: 4912824bd25SMichael Hund return retval; 4922824bd25SMichael Hund } 4932824bd25SMichael Hund 4942824bd25SMichael Hund /** 4952824bd25SMichael Hund * ld_usb_write 4962824bd25SMichael Hund */ 4972824bd25SMichael Hund static ssize_t ld_usb_write(struct file *file, const char __user *buffer, 4982824bd25SMichael Hund size_t count, loff_t *ppos) 4992824bd25SMichael Hund { 5002824bd25SMichael Hund struct ld_usb *dev; 5012824bd25SMichael Hund size_t bytes_to_write; 5022824bd25SMichael Hund int retval = 0; 5032824bd25SMichael Hund 5042824bd25SMichael Hund dev = file->private_data; 5052824bd25SMichael Hund 5062824bd25SMichael Hund /* verify that we actually have some data to write */ 5072824bd25SMichael Hund if (count == 0) 5082824bd25SMichael Hund goto exit; 5092824bd25SMichael Hund 5102824bd25SMichael Hund /* lock this object */ 5112824bd25SMichael Hund if (down_interruptible(&dev->sem)) { 5122824bd25SMichael Hund retval = -ERESTARTSYS; 5132824bd25SMichael Hund goto exit; 5142824bd25SMichael Hund } 5152824bd25SMichael Hund 5162824bd25SMichael Hund /* verify that the device wasn't unplugged */ 5172824bd25SMichael Hund if (dev->intf == NULL) { 5182824bd25SMichael Hund retval = -ENODEV; 5192824bd25SMichael Hund err("No device or device unplugged %d\n", retval); 5202824bd25SMichael Hund goto unlock_exit; 5212824bd25SMichael Hund } 5222824bd25SMichael Hund 5232824bd25SMichael Hund /* wait until previous transfer is finished */ 5242824bd25SMichael Hund if (dev->interrupt_out_busy) { 5252824bd25SMichael Hund if (file->f_flags & O_NONBLOCK) { 5262824bd25SMichael Hund retval = -EAGAIN; 5272824bd25SMichael Hund goto unlock_exit; 5282824bd25SMichael Hund } 5292824bd25SMichael Hund retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy); 5302824bd25SMichael Hund if (retval < 0) { 5312824bd25SMichael Hund goto unlock_exit; 5322824bd25SMichael Hund } 5332824bd25SMichael Hund } 5342824bd25SMichael Hund 5352824bd25SMichael Hund /* write the data into interrupt_out_buffer from userspace */ 5362824bd25SMichael Hund bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size); 5372824bd25SMichael Hund if (bytes_to_write < count) 538dd7d5008SAlexey Dobriyan dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write); 539dd7d5008SAlexey Dobriyan dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __FUNCTION__, count, bytes_to_write); 5402824bd25SMichael Hund 5412824bd25SMichael Hund if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) { 5422824bd25SMichael Hund retval = -EFAULT; 5432824bd25SMichael Hund goto unlock_exit; 5442824bd25SMichael Hund } 5452824bd25SMichael Hund 5462824bd25SMichael Hund if (dev->interrupt_out_endpoint == NULL) { 5472824bd25SMichael Hund /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */ 5482824bd25SMichael Hund retval = usb_control_msg(interface_to_usbdev(dev->intf), 5492824bd25SMichael Hund usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0), 5502824bd25SMichael Hund 9, 5512824bd25SMichael Hund USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, 5522824bd25SMichael Hund 1 << 8, 0, 5532824bd25SMichael Hund dev->interrupt_out_buffer, 5542824bd25SMichael Hund bytes_to_write, 5552824bd25SMichael Hund USB_CTRL_SET_TIMEOUT * HZ); 5562824bd25SMichael Hund if (retval < 0) 5572824bd25SMichael Hund err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval); 5582824bd25SMichael Hund goto unlock_exit; 5592824bd25SMichael Hund } 5602824bd25SMichael Hund 5612824bd25SMichael Hund /* send off the urb */ 5622824bd25SMichael Hund usb_fill_int_urb(dev->interrupt_out_urb, 5632824bd25SMichael Hund interface_to_usbdev(dev->intf), 5642824bd25SMichael Hund usb_sndintpipe(interface_to_usbdev(dev->intf), 5652824bd25SMichael Hund dev->interrupt_out_endpoint->bEndpointAddress), 5662824bd25SMichael Hund dev->interrupt_out_buffer, 5672824bd25SMichael Hund bytes_to_write, 5682824bd25SMichael Hund ld_usb_interrupt_out_callback, 5692824bd25SMichael Hund dev, 5702824bd25SMichael Hund dev->interrupt_out_interval); 5712824bd25SMichael Hund 5722824bd25SMichael Hund dev->interrupt_out_busy = 1; 5732824bd25SMichael Hund wmb(); 5742824bd25SMichael Hund 5752824bd25SMichael Hund retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 5762824bd25SMichael Hund if (retval) { 5772824bd25SMichael Hund dev->interrupt_out_busy = 0; 5782824bd25SMichael Hund err("Couldn't submit interrupt_out_urb %d\n", retval); 5792824bd25SMichael Hund goto unlock_exit; 5802824bd25SMichael Hund } 5812824bd25SMichael Hund retval = bytes_to_write; 5822824bd25SMichael Hund 5832824bd25SMichael Hund unlock_exit: 5842824bd25SMichael Hund /* unlock the device */ 5852824bd25SMichael Hund up(&dev->sem); 5862824bd25SMichael Hund 5872824bd25SMichael Hund exit: 5882824bd25SMichael Hund return retval; 5892824bd25SMichael Hund } 5902824bd25SMichael Hund 5912824bd25SMichael Hund /* file operations needed when we register this driver */ 5922824bd25SMichael Hund static struct file_operations ld_usb_fops = { 5932824bd25SMichael Hund .owner = THIS_MODULE, 5942824bd25SMichael Hund .read = ld_usb_read, 5952824bd25SMichael Hund .write = ld_usb_write, 5962824bd25SMichael Hund .open = ld_usb_open, 5972824bd25SMichael Hund .release = ld_usb_release, 5982824bd25SMichael Hund .poll = ld_usb_poll, 5992824bd25SMichael Hund }; 6002824bd25SMichael Hund 6012824bd25SMichael Hund /* 6022824bd25SMichael Hund * usb class driver info in order to get a minor number from the usb core, 603595b14cbSGreg Kroah-Hartman * and to have the device registered with the driver core 6042824bd25SMichael Hund */ 6052824bd25SMichael Hund static struct usb_class_driver ld_usb_class = { 6062824bd25SMichael Hund .name = "ldusb%d", 6072824bd25SMichael Hund .fops = &ld_usb_fops, 6082824bd25SMichael Hund .minor_base = USB_LD_MINOR_BASE, 6092824bd25SMichael Hund }; 6102824bd25SMichael Hund 6112824bd25SMichael Hund /** 6122824bd25SMichael Hund * ld_usb_probe 6132824bd25SMichael Hund * 6142824bd25SMichael Hund * Called by the usb core when a new device is connected that it thinks 6152824bd25SMichael Hund * this driver might be interested in. 6162824bd25SMichael Hund */ 6172824bd25SMichael Hund static int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) 6182824bd25SMichael Hund { 6192824bd25SMichael Hund struct usb_device *udev = interface_to_usbdev(intf); 6202824bd25SMichael Hund struct ld_usb *dev = NULL; 6212824bd25SMichael Hund struct usb_host_interface *iface_desc; 6222824bd25SMichael Hund struct usb_endpoint_descriptor *endpoint; 6232824bd25SMichael Hund char *buffer; 6242824bd25SMichael Hund int i; 6252824bd25SMichael Hund int retval = -ENOMEM; 6262824bd25SMichael Hund 6272824bd25SMichael Hund /* allocate memory for our device state and intialize it */ 6282824bd25SMichael Hund 6292824bd25SMichael Hund dev = kmalloc(sizeof(*dev), GFP_KERNEL); 6302824bd25SMichael Hund if (dev == NULL) { 6312824bd25SMichael Hund dev_err(&intf->dev, "Out of memory\n"); 6322824bd25SMichael Hund goto exit; 6332824bd25SMichael Hund } 6342824bd25SMichael Hund memset(dev, 0x00, sizeof(*dev)); 6352824bd25SMichael Hund init_MUTEX(&dev->sem); 6362824bd25SMichael Hund dev->intf = intf; 6372824bd25SMichael Hund init_waitqueue_head(&dev->read_wait); 6382824bd25SMichael Hund init_waitqueue_head(&dev->write_wait); 6392824bd25SMichael Hund 6402824bd25SMichael Hund /* workaround for early firmware versions on fast computers */ 6412824bd25SMichael Hund if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) && 642ba3e66e9SMichael Hund ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) || 643ba3e66e9SMichael Hund (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) && 6442824bd25SMichael Hund (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) { 6452824bd25SMichael Hund buffer = kmalloc(256, GFP_KERNEL); 646a6db592eSMichael Hund if (buffer == NULL) { 647a6db592eSMichael Hund dev_err(&intf->dev, "Couldn't allocate string buffer\n"); 648a6db592eSMichael Hund goto error; 649a6db592eSMichael Hund } 6502824bd25SMichael Hund /* usb_string makes SETUP+STALL to leave always ControlReadLoop */ 6512824bd25SMichael Hund usb_string(udev, 255, buffer, 256); 6522824bd25SMichael Hund kfree(buffer); 6532824bd25SMichael Hund } 6542824bd25SMichael Hund 6552824bd25SMichael Hund iface_desc = intf->cur_altsetting; 6562824bd25SMichael Hund 6572824bd25SMichael Hund /* set up the endpoint information */ 6582824bd25SMichael Hund for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 6592824bd25SMichael Hund endpoint = &iface_desc->endpoint[i].desc; 6602824bd25SMichael Hund 6612824bd25SMichael Hund if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) && 6622824bd25SMichael Hund ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) { 6632824bd25SMichael Hund dev->interrupt_in_endpoint = endpoint; 6642824bd25SMichael Hund } 6652824bd25SMichael Hund 6662824bd25SMichael Hund if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) && 6672824bd25SMichael Hund ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) { 6682824bd25SMichael Hund dev->interrupt_out_endpoint = endpoint; 6692824bd25SMichael Hund } 6702824bd25SMichael Hund } 6712824bd25SMichael Hund if (dev->interrupt_in_endpoint == NULL) { 6722824bd25SMichael Hund dev_err(&intf->dev, "Interrupt in endpoint not found\n"); 6732824bd25SMichael Hund goto error; 6742824bd25SMichael Hund } 6752824bd25SMichael Hund if (dev->interrupt_out_endpoint == NULL) 6762824bd25SMichael Hund dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n"); 6772824bd25SMichael Hund 6782824bd25SMichael Hund dev->interrupt_in_endpoint_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize); 6792824bd25SMichael Hund dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL); 6802824bd25SMichael Hund if (!dev->ring_buffer) { 6812824bd25SMichael Hund dev_err(&intf->dev, "Couldn't allocate ring_buffer\n"); 6822824bd25SMichael Hund goto error; 6832824bd25SMichael Hund } 6842824bd25SMichael Hund dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL); 6852824bd25SMichael Hund if (!dev->interrupt_in_buffer) { 6862824bd25SMichael Hund dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n"); 6872824bd25SMichael Hund goto error; 6882824bd25SMichael Hund } 6892824bd25SMichael Hund dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 6902824bd25SMichael Hund if (!dev->interrupt_in_urb) { 6912824bd25SMichael Hund dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n"); 6922824bd25SMichael Hund goto error; 6932824bd25SMichael Hund } 6942824bd25SMichael Hund dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize) : 6952824bd25SMichael Hund udev->descriptor.bMaxPacketSize0; 6962824bd25SMichael Hund dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL); 6972824bd25SMichael Hund if (!dev->interrupt_out_buffer) { 6982824bd25SMichael Hund dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n"); 6992824bd25SMichael Hund goto error; 7002824bd25SMichael Hund } 7012824bd25SMichael Hund dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 7022824bd25SMichael Hund if (!dev->interrupt_out_urb) { 7032824bd25SMichael Hund dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n"); 7042824bd25SMichael Hund goto error; 7052824bd25SMichael Hund } 7062824bd25SMichael Hund dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval; 7072824bd25SMichael Hund if (dev->interrupt_out_endpoint) 7082824bd25SMichael Hund dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval; 7092824bd25SMichael Hund 7102824bd25SMichael Hund /* we can register the device now, as it is ready */ 7112824bd25SMichael Hund usb_set_intfdata(intf, dev); 7122824bd25SMichael Hund 7132824bd25SMichael Hund retval = usb_register_dev(intf, &ld_usb_class); 7142824bd25SMichael Hund if (retval) { 7152824bd25SMichael Hund /* something prevented us from registering this driver */ 7162824bd25SMichael Hund dev_err(&intf->dev, "Not able to get a minor for this device.\n"); 7172824bd25SMichael Hund usb_set_intfdata(intf, NULL); 7182824bd25SMichael Hund goto error; 7192824bd25SMichael Hund } 7202824bd25SMichael Hund 7212824bd25SMichael Hund /* let the user know what node this device is now attached to */ 7222824bd25SMichael Hund dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n", 7232824bd25SMichael Hund (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor); 7242824bd25SMichael Hund 7252824bd25SMichael Hund exit: 7262824bd25SMichael Hund return retval; 7272824bd25SMichael Hund 7282824bd25SMichael Hund error: 7292824bd25SMichael Hund ld_usb_delete(dev); 7302824bd25SMichael Hund 7312824bd25SMichael Hund return retval; 7322824bd25SMichael Hund } 7332824bd25SMichael Hund 7342824bd25SMichael Hund /** 7352824bd25SMichael Hund * ld_usb_disconnect 7362824bd25SMichael Hund * 7372824bd25SMichael Hund * Called by the usb core when the device is removed from the system. 7382824bd25SMichael Hund */ 7392824bd25SMichael Hund static void ld_usb_disconnect(struct usb_interface *intf) 7402824bd25SMichael Hund { 7412824bd25SMichael Hund struct ld_usb *dev; 7422824bd25SMichael Hund int minor; 7432824bd25SMichael Hund 7442824bd25SMichael Hund down(&disconnect_sem); 7452824bd25SMichael Hund 7462824bd25SMichael Hund dev = usb_get_intfdata(intf); 7472824bd25SMichael Hund usb_set_intfdata(intf, NULL); 7482824bd25SMichael Hund 7492824bd25SMichael Hund down(&dev->sem); 7502824bd25SMichael Hund 7512824bd25SMichael Hund minor = intf->minor; 7522824bd25SMichael Hund 7532824bd25SMichael Hund /* give back our minor */ 7542824bd25SMichael Hund usb_deregister_dev(intf, &ld_usb_class); 7552824bd25SMichael Hund 7562824bd25SMichael Hund /* if the device is not opened, then we clean up right now */ 7572824bd25SMichael Hund if (!dev->open_count) { 7582824bd25SMichael Hund up(&dev->sem); 7592824bd25SMichael Hund ld_usb_delete(dev); 7602824bd25SMichael Hund } else { 7612824bd25SMichael Hund dev->intf = NULL; 7622824bd25SMichael Hund up(&dev->sem); 7632824bd25SMichael Hund } 7642824bd25SMichael Hund 7652824bd25SMichael Hund up(&disconnect_sem); 7662824bd25SMichael Hund 7672824bd25SMichael Hund dev_info(&intf->dev, "LD USB Device #%d now disconnected\n", 7682824bd25SMichael Hund (minor - USB_LD_MINOR_BASE)); 7692824bd25SMichael Hund } 7702824bd25SMichael Hund 7712824bd25SMichael Hund /* usb specific object needed to register this driver with the usb subsystem */ 7722824bd25SMichael Hund static struct usb_driver ld_usb_driver = { 7732824bd25SMichael Hund .name = "ldusb", 7742824bd25SMichael Hund .probe = ld_usb_probe, 7752824bd25SMichael Hund .disconnect = ld_usb_disconnect, 7762824bd25SMichael Hund .id_table = ld_usb_table, 7772824bd25SMichael Hund }; 7782824bd25SMichael Hund 7792824bd25SMichael Hund /** 7802824bd25SMichael Hund * ld_usb_init 7812824bd25SMichael Hund */ 7822824bd25SMichael Hund static int __init ld_usb_init(void) 7832824bd25SMichael Hund { 7842824bd25SMichael Hund int retval; 7852824bd25SMichael Hund 7862824bd25SMichael Hund /* register this driver with the USB subsystem */ 7872824bd25SMichael Hund retval = usb_register(&ld_usb_driver); 7882824bd25SMichael Hund if (retval) 7892824bd25SMichael Hund err("usb_register failed for the "__FILE__" driver. Error number %d\n", retval); 7902824bd25SMichael Hund 7912824bd25SMichael Hund return retval; 7922824bd25SMichael Hund } 7932824bd25SMichael Hund 7942824bd25SMichael Hund /** 7952824bd25SMichael Hund * ld_usb_exit 7962824bd25SMichael Hund */ 7972824bd25SMichael Hund static void __exit ld_usb_exit(void) 7982824bd25SMichael Hund { 7992824bd25SMichael Hund /* deregister this driver with the USB subsystem */ 8002824bd25SMichael Hund usb_deregister(&ld_usb_driver); 8012824bd25SMichael Hund } 8022824bd25SMichael Hund 8032824bd25SMichael Hund module_init(ld_usb_init); 8042824bd25SMichael Hund module_exit(ld_usb_exit); 8052824bd25SMichael Hund 806