12824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 22824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * Generic USB driver for report based interrupt in/out devices 32824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * like LD Didactic's USB devices. LD Didactic's USB devices are 42824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * HID devices which do not use HID report definitons (they use 52824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * raw interrupt in and our reports only for communication). 62824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * 72824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * This driver uses a ring buffer for time critical reading of 82824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * interrupt in reports and provides read and write methods for 92824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * raw interrupt reports (similar to the Windows HID driver). 102824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * Devices based on the book USB COMPLETE by Jan Axelson may need 112824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * such a compatibility to the Windows HID driver. 122824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * 132824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de> 142824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * 152824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * This program is free software; you can redistribute it and/or 162824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * modify it under the terms of the GNU General Public License as 172824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * published by the Free Software Foundation; either version 2 of 182824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * the License, or (at your option) any later version. 192824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * 202824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * Derived from Lego USB Tower driver 212824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net> 222824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net> 232824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 242824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 252824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#include <linux/kernel.h> 262824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#include <linux/errno.h> 272824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#include <linux/init.h> 282824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#include <linux/slab.h> 292824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#include <linux/module.h> 304186ecf8ad16dd05759a09594de6a87e48759ba6Arjan van de Ven#include <linux/mutex.h> 312824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 322824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#include <asm/uaccess.h> 332824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#include <linux/input.h> 342824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#include <linux/usb.h> 352824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#include <linux/poll.h> 362824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 372824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* Define these values to match your devices */ 382824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#define USB_VENDOR_ID_LD 0x0f11 /* USB Vendor ID of LD Didactic GmbH */ 39ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_CASSY 0x1000 /* USB Product ID of CASSY-S modules with 8 bytes endpoint size */ 40ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_CASSY2 0x1001 /* USB Product ID of CASSY-S modules with 64 bytes endpoint size */ 41ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_POCKETCASSY 0x1010 /* USB Product ID of Pocket-CASSY */ 42ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_POCKETCASSY2 0x1011 /* USB Product ID of Pocket-CASSY 2 (reserved) */ 43ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_MOBILECASSY 0x1020 /* USB Product ID of Mobile-CASSY */ 44ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MOBILECASSY2 0x1021 /* USB Product ID of Mobile-CASSY 2 (reserved) */ 45ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MICROCASSYVOLTAGE 0x1031 /* USB Product ID of Micro-CASSY Voltage */ 46ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MICROCASSYCURRENT 0x1032 /* USB Product ID of Micro-CASSY Current */ 47ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MICROCASSYTIME 0x1033 /* USB Product ID of Micro-CASSY Time (reserved) */ 48ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE 0x1035 /* USB Product ID of Micro-CASSY Temperature */ 49ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MICROCASSYPH 0x1038 /* USB Product ID of Micro-CASSY pH */ 50ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_JWM 0x1080 /* USB Product ID of Joule and Wattmeter */ 51ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_DMMP 0x1081 /* USB Product ID of Digital Multimeter P (reserved) */ 52ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_UMIP 0x1090 /* USB Product ID of UMI P */ 53ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_UMIC 0x10A0 /* USB Product ID of UMI C */ 54ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_UMIB 0x10B0 /* USB Product ID of UMI B */ 55ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_XRAY 0x1100 /* USB Product ID of X-Ray Apparatus 55481 */ 56ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_XRAY2 0x1101 /* USB Product ID of X-Ray Apparatus 554800 */ 57ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_XRAYCT 0x1110 /* USB Product ID of X-Ray Apparatus CT 554821*/ 58ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_VIDEOCOM 0x1200 /* USB Product ID of VideoCom */ 59ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MOTOR 0x1210 /* USB Product ID of Motor (reserved) */ 60ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_COM3LAB 0x2000 /* USB Product ID of COM3LAB */ 61ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_TELEPORT 0x2010 /* USB Product ID of Terminal Adapter */ 62ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_NETWORKANALYSER 0x2020 /* USB Product ID of Network Analyser */ 63ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_POWERCONTROL 0x2030 /* USB Product ID of Converter Control Unit */ 64ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund#define USB_DEVICE_ID_LD_MACHINETEST 0x2040 /* USB Product ID of Machine Test System */ 65ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MOSTANALYSER 0x2050 /* USB Product ID of MOST Protocol Analyser */ 66ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MOSTANALYSER2 0x2051 /* USB Product ID of MOST Protocol Analyser 2 */ 67ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_ABSESP 0x2060 /* USB Product ID of ABS ESP */ 68ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_AUTODATABUS 0x2070 /* USB Product ID of Automotive Data Buses */ 69ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_MCT 0x2080 /* USB Product ID of Microcontroller technique */ 70ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_HYBRID 0x2090 /* USB Product ID of Automotive Hybrid */ 71ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund#define USB_DEVICE_ID_LD_HEATCONTROL 0x20A0 /* USB Product ID of Heat control */ 722824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 732824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#define USB_VENDOR_ID_VERNIER 0x08f7 742824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#define USB_DEVICE_ID_VERNIER_GOTEMP 0x0002 752824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#define USB_DEVICE_ID_VERNIER_SKIP 0x0003 762824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#define USB_DEVICE_ID_VERNIER_CYCLOPS 0x0004 775b0a4d66a11df34b632e48ce80ebe81da94bdb65Stephen Ware#define USB_DEVICE_ID_VERNIER_LCSPEC 0x0006 782824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 792824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#ifdef CONFIG_USB_DYNAMIC_MINORS 802824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#define USB_LD_MINOR_BASE 0 812824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#else 822824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#define USB_LD_MINOR_BASE 176 832824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#endif 842824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 852824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* table of devices that work with this driver */ 8633b9e16243fd69493be3ddda7be73226c8be586aNémeth Mártonstatic const struct usb_device_id ld_usb_table[] = { 87ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY) }, 88ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_CASSY2) }, 89ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY) }, 90ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POCKETCASSY2) }, 91ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY) }, 92ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOBILECASSY2) }, 93ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYVOLTAGE) }, 94ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYCURRENT) }, 95ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTIME) }, 96ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYTEMPERATURE) }, 97ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MICROCASSYPH) }, 98ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_JWM) }, 99ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_DMMP) }, 100ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIP) }, 101ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIC) }, 102ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_UMIB) }, 103ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY) }, 104ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_XRAY2) }, 105ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_VIDEOCOM) }, 106ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOTOR) }, 107ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_COM3LAB) }, 108ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_TELEPORT) }, 109ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_NETWORKANALYSER) }, 110ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_POWERCONTROL) }, 111ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MACHINETEST) }, 112ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER) }, 113ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MOSTANALYSER2) }, 114ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_ABSESP) }, 115ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_AUTODATABUS) }, 116ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_MCT) }, 117ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HYBRID) }, 118ce97cac813340eb8ecb1c5410041c9eade58f870Michael Hund { USB_DEVICE(USB_VENDOR_ID_LD, USB_DEVICE_ID_LD_HEATCONTROL) }, 1192824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_GOTEMP) }, 1202824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_SKIP) }, 1212824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_CYCLOPS) }, 1225b0a4d66a11df34b632e48ce80ebe81da94bdb65Stephen Ware { USB_DEVICE(USB_VENDOR_ID_VERNIER, USB_DEVICE_ID_VERNIER_LCSPEC) }, 1232824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund { } /* Terminating entry */ 1242824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund}; 1252824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_DEVICE_TABLE(usb, ld_usb_table); 126ce97cac813340eb8ecb1c5410041c9eade58f870Michael HundMODULE_VERSION("V0.14"); 1272824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_AUTHOR("Michael Hund <mhund@ld-didactic.de>"); 1282824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_DESCRIPTION("LD USB Driver"); 1292824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_LICENSE("GPL"); 1302824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_SUPPORTED_DEVICE("LD USB Devices"); 1312824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1322824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#ifdef CONFIG_USB_DEBUG 1332824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund static int debug = 1; 1342824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#else 1352824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund static int debug = 0; 1362824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#endif 1372824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1382824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* Use our own dbg macro */ 1392824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund#define dbg_info(dev, format, arg...) do { if (debug) dev_info(dev , format , ## arg); } while (0) 1402824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1412824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* Module parameters */ 1422824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundmodule_param(debug, int, S_IRUGO | S_IWUSR); 1432824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_PARM_DESC(debug, "Debug enabled or not"); 1442824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1452824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* All interrupt in transfers are collected in a ring buffer to 1462824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * avoid racing conditions and get better performance of the driver. 1472824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 1482824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic int ring_buffer_size = 128; 1492824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundmodule_param(ring_buffer_size, int, 0); 1502824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size in reports"); 1512824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1522824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* The write_buffer can contain more than one interrupt out transfer. 1532824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 1542824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic int write_buffer_size = 10; 1552824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundmodule_param(write_buffer_size, int, 0); 1562824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_PARM_DESC(write_buffer_size, "Write buffer size in reports"); 1572824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1582824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* As of kernel version 2.6.4 ehci-hcd uses an 1592824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * "only one interrupt transfer per frame" shortcut 1602824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * to simplify the scheduling of periodic transfers. 1612824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * This conflicts with our standard 1ms intervals for in and out URBs. 1622824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * We use default intervals of 2ms for in and 2ms for out transfers, 1632824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * which should be fast enough. 1642824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * Increase the interval to allow more devices that do interrupt transfers, 1652824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * or set to 1 to use the standard interval from the endpoint descriptors. 1662824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 1672824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic int min_interrupt_in_interval = 2; 1682824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundmodule_param(min_interrupt_in_interval, int, 0); 1692824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_PARM_DESC(min_interrupt_in_interval, "Minimum interrupt in interval in ms"); 1702824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1712824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic int min_interrupt_out_interval = 2; 1722824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundmodule_param(min_interrupt_out_interval, int, 0); 1732824bd250f0be1551747cc3ed5ae07facc285b57Michael HundMODULE_PARM_DESC(min_interrupt_out_interval, "Minimum interrupt out interval in ms"); 1742824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1752824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* Structure to hold all of our device specific stuff */ 1762824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstruct ld_usb { 177ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker struct mutex mutex; /* locks this structure */ 1782824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct usb_interface* intf; /* save off the usb interface pointer */ 1792824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1802824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int open_count; /* number of times this port has been opened */ 1812824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1822824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund char* ring_buffer; 1832824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund unsigned int ring_head; 1842824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund unsigned int ring_tail; 1852824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1862824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund wait_queue_head_t read_wait; 1872824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund wait_queue_head_t write_wait; 1882824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1892824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund char* interrupt_in_buffer; 1902824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct usb_endpoint_descriptor* interrupt_in_endpoint; 1912824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct urb* interrupt_in_urb; 1922824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int interrupt_in_interval; 1932824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund size_t interrupt_in_endpoint_size; 1942824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int interrupt_in_running; 1952824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int interrupt_in_done; 1969d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum int buffer_overflow; 1979d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spinlock_t rbsl; 1982824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1992824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund char* interrupt_out_buffer; 2002824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct usb_endpoint_descriptor* interrupt_out_endpoint; 2012824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct urb* interrupt_out_urb; 2022824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int interrupt_out_interval; 2032824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund size_t interrupt_out_endpoint_size; 2042824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int interrupt_out_busy; 2052824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund}; 2062824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 2072824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic struct usb_driver ld_usb_driver; 2082824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 2092824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 2102824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_abort_transfers 2112824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * aborts transfers and frees associated data structures 2122824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 2132824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic void ld_usb_abort_transfers(struct ld_usb *dev) 2142824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 2152824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* shutdown transfer */ 2162824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->interrupt_in_running) { 2172824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_running = 0; 2182824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->intf) 2192824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_kill_urb(dev->interrupt_in_urb); 2202824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 2212824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->interrupt_out_busy) 2222824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->intf) 2232824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_kill_urb(dev->interrupt_out_urb); 2242824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 2252824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 2262824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 2272824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_delete 2282824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 2292824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic void ld_usb_delete(struct ld_usb *dev) 2302824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 2312824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund ld_usb_abort_transfers(dev); 2322824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 2332824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* free data structures */ 2342824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_free_urb(dev->interrupt_in_urb); 2352824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_free_urb(dev->interrupt_out_urb); 2362824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund kfree(dev->ring_buffer); 2372824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund kfree(dev->interrupt_in_buffer); 2382824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund kfree(dev->interrupt_out_buffer); 2392824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund kfree(dev); 2402824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 2412824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 2422824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 2432824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_interrupt_in_callback 2442824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 2457d12e780e003f93433d49ce78cfedf4b4c52adc5David Howellsstatic void ld_usb_interrupt_in_callback(struct urb *urb) 2462824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 2472824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct ld_usb *dev = urb->context; 2482824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund size_t *actual_buffer; 2492824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund unsigned int next_ring_head; 250491c021e0beafe4146f6a1c9a1c58bd0fb2a42d0Greg Kroah-Hartman int status = urb->status; 2512824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int retval; 2522824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 253491c021e0beafe4146f6a1c9a1c58bd0fb2a42d0Greg Kroah-Hartman if (status) { 254491c021e0beafe4146f6a1c9a1c58bd0fb2a42d0Greg Kroah-Hartman if (status == -ENOENT || 255491c021e0beafe4146f6a1c9a1c58bd0fb2a42d0Greg Kroah-Hartman status == -ECONNRESET || 256491c021e0beafe4146f6a1c9a1c58bd0fb2a42d0Greg Kroah-Hartman status == -ESHUTDOWN) { 2572824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto exit; 2582824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } else { 2592824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dbg_info(&dev->intf->dev, "%s: nonzero status received: %d\n", 260441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison __func__, status); 2619d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_lock(&dev->rbsl); 2622824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto resubmit; /* maybe we can recover */ 2632824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 2642824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 2652824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 2669d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_lock(&dev->rbsl); 2672824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (urb->actual_length > 0) { 2682824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund next_ring_head = (dev->ring_head+1) % ring_buffer_size; 2692824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (next_ring_head != dev->ring_tail) { 2702824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_head*(sizeof(size_t)+dev->interrupt_in_endpoint_size)); 2712824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* actual_buffer gets urb->actual_length + interrupt_in_buffer */ 2722824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund *actual_buffer = urb->actual_length; 2732824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund memcpy(actual_buffer+1, dev->interrupt_in_buffer, urb->actual_length); 2742824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->ring_head = next_ring_head; 2752824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dbg_info(&dev->intf->dev, "%s: received %d bytes\n", 276441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison __func__, urb->actual_length); 2779d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum } else { 2782824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_warn(&dev->intf->dev, 2792824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund "Ring buffer overflow, %d bytes dropped\n", 2802824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund urb->actual_length); 2819d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum dev->buffer_overflow = 1; 2829d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum } 2832824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 2842824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 2852824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundresubmit: 2862824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* resubmit if we're still running */ 2879d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum if (dev->interrupt_in_running && !dev->buffer_overflow && dev->intf) { 2882824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC); 2899d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum if (retval) { 2902824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&dev->intf->dev, 2912824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund "usb_submit_urb failed (%d)\n", retval); 2929d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum dev->buffer_overflow = 1; 2939d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum } 2942824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 2959d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_unlock(&dev->rbsl); 2962824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundexit: 2972824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_done = 1; 2982824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund wake_up_interruptible(&dev->read_wait); 2992824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 3002824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3012824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 3022824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_interrupt_out_callback 3032824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 3047d12e780e003f93433d49ce78cfedf4b4c52adc5David Howellsstatic void ld_usb_interrupt_out_callback(struct urb *urb) 3052824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 3062824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct ld_usb *dev = urb->context; 307491c021e0beafe4146f6a1c9a1c58bd0fb2a42d0Greg Kroah-Hartman int status = urb->status; 3082824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3092824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* sync/async unlink faults aren't errors */ 310491c021e0beafe4146f6a1c9a1c58bd0fb2a42d0Greg Kroah-Hartman if (status && !(status == -ENOENT || 311491c021e0beafe4146f6a1c9a1c58bd0fb2a42d0Greg Kroah-Hartman status == -ECONNRESET || 312491c021e0beafe4146f6a1c9a1c58bd0fb2a42d0Greg Kroah-Hartman status == -ESHUTDOWN)) 3132824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dbg_info(&dev->intf->dev, 3142824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund "%s - nonzero write interrupt status received: %d\n", 315441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison __func__, status); 3162824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3172824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_busy = 0; 3182824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund wake_up_interruptible(&dev->write_wait); 3192824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 3202824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3212824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 3222824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_open 3232824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 3242824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic int ld_usb_open(struct inode *inode, struct file *file) 3252824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 3262824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct ld_usb *dev; 3272824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int subminor; 328d4ead16f50f9ad30bdc7276ec8fee7a24c72f294Alan Stern int retval; 3292824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct usb_interface *interface; 3302824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3312824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund nonseekable_open(inode, file); 3322824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund subminor = iminor(inode); 3332824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3342824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund interface = usb_find_interface(&ld_usb_driver, subminor); 3352824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3362824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (!interface) { 3372824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund err("%s - error, can't find device for minor %d\n", 338441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison __func__, subminor); 339d4ead16f50f9ad30bdc7276ec8fee7a24c72f294Alan Stern return -ENODEV; 3402824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 3412824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3422824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev = usb_get_intfdata(interface); 3432824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3446248c52f6abd5783ecdd042f6107bd172168961eOliver Neukum if (!dev) 345d4ead16f50f9ad30bdc7276ec8fee7a24c72f294Alan Stern return -ENODEV; 3462824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3472824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* lock this device */ 3486248c52f6abd5783ecdd042f6107bd172168961eOliver Neukum if (mutex_lock_interruptible(&dev->mutex)) 349d4ead16f50f9ad30bdc7276ec8fee7a24c72f294Alan Stern return -ERESTARTSYS; 3502824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3512824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* allow opening only once */ 3522824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->open_count) { 3532824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -EBUSY; 3542824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 3552824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 3562824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->open_count = 1; 3572824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3582824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* initialize in direction */ 3592824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->ring_head = 0; 3602824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->ring_tail = 0; 3619d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum dev->buffer_overflow = 0; 3622824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_fill_int_urb(dev->interrupt_in_urb, 3632824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund interface_to_usbdev(interface), 3642824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_rcvintpipe(interface_to_usbdev(interface), 3652824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_endpoint->bEndpointAddress), 3662824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_buffer, 3672824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_endpoint_size, 3682824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund ld_usb_interrupt_in_callback, 3692824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev, 3702824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_interval); 3712824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3722824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_running = 1; 3732824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_done = 0; 3742824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3752824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 3762824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (retval) { 3772824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&interface->dev, "Couldn't submit interrupt_in_urb %d\n", retval); 3782824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_running = 0; 3792824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->open_count = 0; 3802824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 3812824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 3822824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3832824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* save device in the file's private structure */ 3842824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund file->private_data = dev; 3852824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3862824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundunlock_exit: 387ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker mutex_unlock(&dev->mutex); 3882824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3892824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund return retval; 3902824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 3912824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 3922824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 3932824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_release 3942824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 3952824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic int ld_usb_release(struct inode *inode, struct file *file) 3962824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 3972824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct ld_usb *dev; 3982824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int retval = 0; 3992824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4002824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev = file->private_data; 4012824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4022824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev == NULL) { 4032824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -ENODEV; 4042824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto exit; 4052824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 4062824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 407ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker if (mutex_lock_interruptible(&dev->mutex)) { 4082824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -ERESTARTSYS; 4092824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto exit; 4102824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 4112824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4122824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->open_count != 1) { 4132824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -ENODEV; 4142824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 4152824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 4162824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->intf == NULL) { 4172824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* the device was unplugged before the file was released */ 418ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker mutex_unlock(&dev->mutex); 4192824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* unlock here as ld_usb_delete frees dev */ 4202824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund ld_usb_delete(dev); 4212824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto exit; 4222824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 4232824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4242824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* wait until write transfer is finished */ 4252824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->interrupt_out_busy) 4262824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy, 2 * HZ); 4272824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund ld_usb_abort_transfers(dev); 4282824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->open_count = 0; 4292824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4302824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundunlock_exit: 431ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker mutex_unlock(&dev->mutex); 4322824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4332824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundexit: 4342824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund return retval; 4352824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 4362824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4372824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 4382824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_poll 4392824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 4402824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic unsigned int ld_usb_poll(struct file *file, poll_table *wait) 4412824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 4422824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct ld_usb *dev; 4432824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund unsigned int mask = 0; 4442824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4452824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev = file->private_data; 4462824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 447d12b85e7de1abce4db940ebb169f064583b5796eOliver Neukum if (!dev->intf) 448d12b85e7de1abce4db940ebb169f064583b5796eOliver Neukum return POLLERR | POLLHUP; 449d12b85e7de1abce4db940ebb169f064583b5796eOliver Neukum 4502824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund poll_wait(file, &dev->read_wait, wait); 4512824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund poll_wait(file, &dev->write_wait, wait); 4522824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4532824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->ring_head != dev->ring_tail) 4542824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund mask |= POLLIN | POLLRDNORM; 4552824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (!dev->interrupt_out_busy) 4562824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund mask |= POLLOUT | POLLWRNORM; 4572824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4582824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund return mask; 4592824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 4602824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4612824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 4622824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_read 4632824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 4642824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic ssize_t ld_usb_read(struct file *file, char __user *buffer, size_t count, 4652824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund loff_t *ppos) 4662824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 4672824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct ld_usb *dev; 4682824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund size_t *actual_buffer; 4692824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund size_t bytes_to_read; 4702824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int retval = 0; 4719d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum int rv; 4722824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4732824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev = file->private_data; 4742824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4752824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* verify that we actually have some data to read */ 4762824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (count == 0) 4772824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto exit; 4782824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4792824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* lock this object */ 480ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker if (mutex_lock_interruptible(&dev->mutex)) { 4812824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -ERESTARTSYS; 4822824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto exit; 4832824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 4842824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4852824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* verify that the device wasn't unplugged */ 4862824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->intf == NULL) { 4872824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -ENODEV; 4882824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund err("No device or device unplugged %d\n", retval); 4892824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 4902824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 4912824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 4922824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* wait for data */ 4939d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_lock_irq(&dev->rbsl); 4942824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->ring_head == dev->ring_tail) { 4959d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum dev->interrupt_in_done = 0; 4969d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_unlock_irq(&dev->rbsl); 4972824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (file->f_flags & O_NONBLOCK) { 4982824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -EAGAIN; 4992824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 5002824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 5012824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = wait_event_interruptible(dev->read_wait, dev->interrupt_in_done); 5022824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (retval < 0) 5032824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 5049d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum } else { 5059d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_unlock_irq(&dev->rbsl); 5062824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 5072824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5082824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* actual_buffer contains actual_length + interrupt_in_buffer */ 5092824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund actual_buffer = (size_t*)(dev->ring_buffer + dev->ring_tail*(sizeof(size_t)+dev->interrupt_in_endpoint_size)); 5102824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund bytes_to_read = min(count, *actual_buffer); 5112824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (bytes_to_read < *actual_buffer) 512dd7d50081f5dafd9392bd79f1ec90d553a7303c9Alexey Dobriyan dev_warn(&dev->intf->dev, "Read buffer overflow, %zd bytes dropped\n", 5132824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund *actual_buffer-bytes_to_read); 5142824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5152824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* copy one interrupt_in_buffer from ring_buffer into userspace */ 5162824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (copy_to_user(buffer, actual_buffer+1, bytes_to_read)) { 5172824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -EFAULT; 5182824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 5192824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 5202824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->ring_tail = (dev->ring_tail+1) % ring_buffer_size; 5212824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5222824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = bytes_to_read; 5232824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5249d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_lock_irq(&dev->rbsl); 5259d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum if (dev->buffer_overflow) { 5269d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum dev->buffer_overflow = 0; 5279d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_unlock_irq(&dev->rbsl); 5289d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum rv = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL); 5299d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum if (rv < 0) 5309d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum dev->buffer_overflow = 1; 5319d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum } else { 5329d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_unlock_irq(&dev->rbsl); 5339d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum } 5349d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum 5352824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundunlock_exit: 5362824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* unlock the device */ 537ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker mutex_unlock(&dev->mutex); 5382824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5392824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundexit: 5402824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund return retval; 5412824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 5422824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5432824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 5442824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_write 5452824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 5462824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic ssize_t ld_usb_write(struct file *file, const char __user *buffer, 5472824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund size_t count, loff_t *ppos) 5482824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 5492824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct ld_usb *dev; 5502824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund size_t bytes_to_write; 5512824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int retval = 0; 5522824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5532824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev = file->private_data; 5542824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5552824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* verify that we actually have some data to write */ 5562824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (count == 0) 5572824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto exit; 5582824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5592824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* lock this object */ 560ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker if (mutex_lock_interruptible(&dev->mutex)) { 5612824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -ERESTARTSYS; 5622824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto exit; 5632824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 5642824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5652824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* verify that the device wasn't unplugged */ 5662824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->intf == NULL) { 5672824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -ENODEV; 5682824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund err("No device or device unplugged %d\n", retval); 5692824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 5702824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 5712824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5722824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* wait until previous transfer is finished */ 5732824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->interrupt_out_busy) { 5742824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (file->f_flags & O_NONBLOCK) { 5752824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -EAGAIN; 5762824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 5772824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 5782824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = wait_event_interruptible(dev->write_wait, !dev->interrupt_out_busy); 5792824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (retval < 0) { 5802824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 5812824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 5822824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 5832824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5842824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* write the data into interrupt_out_buffer from userspace */ 5852824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund bytes_to_write = min(count, write_buffer_size*dev->interrupt_out_endpoint_size); 5862824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (bytes_to_write < count) 587dd7d50081f5dafd9392bd79f1ec90d553a7303c9Alexey Dobriyan dev_warn(&dev->intf->dev, "Write buffer overflow, %zd bytes dropped\n",count-bytes_to_write); 588441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n", __func__, count, bytes_to_write); 5892824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5902824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) { 5912824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = -EFAULT; 5922824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 5932824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 5942824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 5952824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->interrupt_out_endpoint == NULL) { 5962824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* try HID_REQ_SET_REPORT=9 on control_endpoint instead of interrupt_out_endpoint */ 5972824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = usb_control_msg(interface_to_usbdev(dev->intf), 5982824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_sndctrlpipe(interface_to_usbdev(dev->intf), 0), 5992824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 9, 6002824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, 6012824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 1 << 8, 0, 6022824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_buffer, 6032824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund bytes_to_write, 6042824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund USB_CTRL_SET_TIMEOUT * HZ); 6052824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (retval < 0) 6062824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund err("Couldn't submit HID_REQ_SET_REPORT %d\n", retval); 6072824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 6082824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 6092824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6102824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* send off the urb */ 6112824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_fill_int_urb(dev->interrupt_out_urb, 6122824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund interface_to_usbdev(dev->intf), 6132824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_sndintpipe(interface_to_usbdev(dev->intf), 6142824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_endpoint->bEndpointAddress), 6152824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_buffer, 6162824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund bytes_to_write, 6172824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund ld_usb_interrupt_out_callback, 6182824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev, 6192824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_interval); 6202824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6212824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_busy = 1; 6222824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund wmb(); 6232824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6242824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL); 6252824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (retval) { 6262824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_busy = 0; 6272824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund err("Couldn't submit interrupt_out_urb %d\n", retval); 6282824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto unlock_exit; 6292824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 6302824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = bytes_to_write; 6312824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6322824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundunlock_exit: 6332824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* unlock the device */ 634ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker mutex_unlock(&dev->mutex); 6352824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6362824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundexit: 6372824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund return retval; 6382824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 6392824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6402824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* file operations needed when we register this driver */ 641066202dd48cf3296b6cc22b5fcf89aef33fa0efcLuiz Fernando N. Capitulinostatic const struct file_operations ld_usb_fops = { 6422824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .owner = THIS_MODULE, 6432824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .read = ld_usb_read, 6442824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .write = ld_usb_write, 6452824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .open = ld_usb_open, 6462824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .release = ld_usb_release, 6472824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .poll = ld_usb_poll, 6486038f373a3dc1f1c26496e60b6c40b164716f07eArnd Bergmann .llseek = no_llseek, 6492824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund}; 6502824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6512824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* 6522824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * usb class driver info in order to get a minor number from the usb core, 653595b14cbccb2f9122bccfa6b55f2d9a380e9adebGreg Kroah-Hartman * and to have the device registered with the driver core 6542824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 6552824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic struct usb_class_driver ld_usb_class = { 6562824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .name = "ldusb%d", 6572824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .fops = &ld_usb_fops, 6582824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .minor_base = USB_LD_MINOR_BASE, 6592824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund}; 6602824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6612824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 6622824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_probe 6632824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * 6642824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * Called by the usb core when a new device is connected that it thinks 6652824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * this driver might be interested in. 6662824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 6672824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic int ld_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) 6682824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 6692824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct usb_device *udev = interface_to_usbdev(intf); 6702824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct ld_usb *dev = NULL; 6712824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct usb_host_interface *iface_desc; 6722824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct usb_endpoint_descriptor *endpoint; 6732824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund char *buffer; 6742824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int i; 6752824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int retval = -ENOMEM; 6762824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 677b595076a180a56d1bb170e6eceda6eb9d76f4cd3Uwe Kleine-König /* allocate memory for our device state and initialize it */ 6782824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6791144cf7af2ff8e6816e360d03f867439f7efde40Oliver Neukum dev = kzalloc(sizeof(*dev), GFP_KERNEL); 6802824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev == NULL) { 6812824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&intf->dev, "Out of memory\n"); 6822824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto exit; 6832824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 684ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker mutex_init(&dev->mutex); 6859d33efd9a791041bbe3a9e879925ef8fbb94d812Oliver Neukum spin_lock_init(&dev->rbsl); 6862824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->intf = intf; 6872824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund init_waitqueue_head(&dev->read_wait); 6882824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund init_waitqueue_head(&dev->write_wait); 6892824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 6902824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* workaround for early firmware versions on fast computers */ 6912824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if ((le16_to_cpu(udev->descriptor.idVendor) == USB_VENDOR_ID_LD) && 692ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund ((le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_CASSY) || 693ba3e66e94b9fb8c2a370a90729e068314845549dMichael Hund (le16_to_cpu(udev->descriptor.idProduct) == USB_DEVICE_ID_LD_COM3LAB)) && 6942824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x103)) { 6952824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund buffer = kmalloc(256, GFP_KERNEL); 696a6db592e1624bb7ec62cf56629c9556442169ac5Michael Hund if (buffer == NULL) { 697a6db592e1624bb7ec62cf56629c9556442169ac5Michael Hund dev_err(&intf->dev, "Couldn't allocate string buffer\n"); 698a6db592e1624bb7ec62cf56629c9556442169ac5Michael Hund goto error; 699a6db592e1624bb7ec62cf56629c9556442169ac5Michael Hund } 7002824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* usb_string makes SETUP+STALL to leave always ControlReadLoop */ 7012824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_string(udev, 255, buffer, 256); 7022824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund kfree(buffer); 7032824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 7042824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7052824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund iface_desc = intf->cur_altsetting; 7062824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7072824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* set up the endpoint information */ 7082824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 7092824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund endpoint = &iface_desc->endpoint[i].desc; 7102824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7115482687b8be4dedb8a5879f07c734ff11a88a7d5Luiz Fernando N. Capitulino if (usb_endpoint_is_int_in(endpoint)) 7122824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_endpoint = endpoint; 7132824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7145482687b8be4dedb8a5879f07c734ff11a88a7d5Luiz Fernando N. Capitulino if (usb_endpoint_is_int_out(endpoint)) 7152824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_endpoint = endpoint; 7162824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 7172824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->interrupt_in_endpoint == NULL) { 7182824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&intf->dev, "Interrupt in endpoint not found\n"); 7192824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto error; 7202824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 7212824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->interrupt_out_endpoint == NULL) 7222824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_warn(&intf->dev, "Interrupt out endpoint not found (using control endpoint instead)\n"); 7232824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 72429cc88979a8818cd8c5019426e945aed118b400eKuninori Morimoto dev->interrupt_in_endpoint_size = usb_endpoint_maxp(dev->interrupt_in_endpoint); 7252824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->ring_buffer = kmalloc(ring_buffer_size*(sizeof(size_t)+dev->interrupt_in_endpoint_size), GFP_KERNEL); 7262824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (!dev->ring_buffer) { 7272824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&intf->dev, "Couldn't allocate ring_buffer\n"); 7282824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto error; 7292824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 7302824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL); 7312824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (!dev->interrupt_in_buffer) { 7322824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n"); 7332824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto error; 7342824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 7352824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL); 7362824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (!dev->interrupt_in_urb) { 7372824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n"); 7382824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto error; 7392824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 74029cc88979a8818cd8c5019426e945aed118b400eKuninori Morimoto dev->interrupt_out_endpoint_size = dev->interrupt_out_endpoint ? usb_endpoint_maxp(dev->interrupt_out_endpoint) : 7412824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund udev->descriptor.bMaxPacketSize0; 7422824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_buffer = kmalloc(write_buffer_size*dev->interrupt_out_endpoint_size, GFP_KERNEL); 7432824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (!dev->interrupt_out_buffer) { 7442824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n"); 7452824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto error; 7462824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 7472824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL); 7482824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (!dev->interrupt_out_urb) { 7492824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n"); 7502824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto error; 7512824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 7522824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_in_interval = min_interrupt_in_interval > dev->interrupt_in_endpoint->bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->bInterval; 7532824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (dev->interrupt_out_endpoint) 7542824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->interrupt_out_interval = min_interrupt_out_interval > dev->interrupt_out_endpoint->bInterval ? min_interrupt_out_interval : dev->interrupt_out_endpoint->bInterval; 7552824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7562824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* we can register the device now, as it is ready */ 7572824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_set_intfdata(intf, dev); 7582824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7592824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund retval = usb_register_dev(intf, &ld_usb_class); 7602824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (retval) { 7612824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* something prevented us from registering this driver */ 7622824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_err(&intf->dev, "Not able to get a minor for this device.\n"); 7632824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_set_intfdata(intf, NULL); 7642824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund goto error; 7652824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 7662824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7672824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* let the user know what node this device is now attached to */ 7682824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_info(&intf->dev, "LD USB Device #%d now attached to major %d minor %d\n", 7692824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund (intf->minor - USB_LD_MINOR_BASE), USB_MAJOR, intf->minor); 7702824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7712824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundexit: 7722824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund return retval; 7732824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7742824bd250f0be1551747cc3ed5ae07facc285b57Michael Hunderror: 7752824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund ld_usb_delete(dev); 7762824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7772824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund return retval; 7782824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 7792824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7802824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/** 7812824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * ld_usb_disconnect 7822824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * 7832824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund * Called by the usb core when the device is removed from the system. 7842824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund */ 7852824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic void ld_usb_disconnect(struct usb_interface *intf) 7862824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund{ 7872824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund struct ld_usb *dev; 7882824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund int minor; 7892824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7902824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev = usb_get_intfdata(intf); 7912824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_set_intfdata(intf, NULL); 7922824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7932824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund minor = intf->minor; 7942824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 7952824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* give back our minor */ 7962824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund usb_deregister_dev(intf, &ld_usb_class); 7972824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 798ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker mutex_lock(&dev->mutex); 799d4ead16f50f9ad30bdc7276ec8fee7a24c72f294Alan Stern 8002824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund /* if the device is not opened, then we clean up right now */ 8012824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund if (!dev->open_count) { 802ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker mutex_unlock(&dev->mutex); 8032824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund ld_usb_delete(dev); 8042824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } else { 8052824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev->intf = NULL; 806d12b85e7de1abce4db940ebb169f064583b5796eOliver Neukum /* wake up pollers */ 807d12b85e7de1abce4db940ebb169f064583b5796eOliver Neukum wake_up_interruptible_all(&dev->read_wait); 808d12b85e7de1abce4db940ebb169f064583b5796eOliver Neukum wake_up_interruptible_all(&dev->write_wait); 809ce0d7d3f575fc1ba6a89c3c651e710355590daffDaniel Walker mutex_unlock(&dev->mutex); 8102824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund } 8112824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 8122824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund dev_info(&intf->dev, "LD USB Device #%d now disconnected\n", 8132824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund (minor - USB_LD_MINOR_BASE)); 8142824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund} 8152824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 8162824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund/* usb specific object needed to register this driver with the usb subsystem */ 8172824bd250f0be1551747cc3ed5ae07facc285b57Michael Hundstatic struct usb_driver ld_usb_driver = { 8182824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .name = "ldusb", 8192824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .probe = ld_usb_probe, 8202824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .disconnect = ld_usb_disconnect, 8212824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund .id_table = ld_usb_table, 8222824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund}; 8232824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 82465db43054065790a75291b0834657445fea2cf56Greg Kroah-Hartmanmodule_usb_driver(ld_usb_driver); 8252824bd250f0be1551747cc3ed5ae07facc285b57Michael Hund 826