legousbtower.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /*
  2. * LEGO USB Tower driver
  3. *
  4. * Copyright (C) 2003 David Glance <davidgsf@sourceforge.net>
  5. * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * derived from USB Skeleton driver - 0.5
  13. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  14. *
  15. * History:
  16. *
  17. * 2001-10-13 - 0.1 js
  18. * - first version
  19. * 2001-11-03 - 0.2 js
  20. * - simplified buffering, one-shot URBs for writing
  21. * 2001-11-10 - 0.3 js
  22. * - removed IOCTL (setting power/mode is more complicated, postponed)
  23. * 2001-11-28 - 0.4 js
  24. * - added vendor commands for mode of operation and power level in open
  25. * 2001-12-04 - 0.5 js
  26. * - set IR mode by default (by oversight 0.4 set VLL mode)
  27. * 2002-01-11 - 0.5? pcchan
  28. * - make read buffer reusable and work around bytes_to_write issue between
  29. * uhci and legusbtower
  30. * 2002-09-23 - 0.52 david (david@csse.uwa.edu.au)
  31. * - imported into lejos project
  32. * - changed wake_up to wake_up_interruptible
  33. * - changed to use lego0 rather than tower0
  34. * - changed dbg() to use __func__ rather than deprecated __func__
  35. * 2003-01-12 - 0.53 david (david@csse.uwa.edu.au)
  36. * - changed read and write to write everything or
  37. * timeout (from a patch by Chris Riesen and Brett Thaeler driver)
  38. * - added ioctl functionality to set timeouts
  39. * 2003-07-18 - 0.54 davidgsf (david@csse.uwa.edu.au)
  40. * - initial import into LegoUSB project
  41. * - merge of existing LegoUSB.c driver
  42. * 2003-07-18 - 0.56 davidgsf (david@csse.uwa.edu.au)
  43. * - port to 2.6 style driver
  44. * 2004-02-29 - 0.6 Juergen Stuber <starblue@users.sourceforge.net>
  45. * - fix locking
  46. * - unlink read URBs which are no longer needed
  47. * - allow increased buffer size, eliminates need for timeout on write
  48. * - have read URB running continuously
  49. * - added poll
  50. * - forbid seeking
  51. * - added nonblocking I/O
  52. * - changed back __func__ to __func__
  53. * - read and log tower firmware version
  54. * - reset tower on probe, avoids failure of first write
  55. * 2004-03-09 - 0.7 Juergen Stuber <starblue@users.sourceforge.net>
  56. * - timeout read now only after inactivity, shorten default accordingly
  57. * 2004-03-11 - 0.8 Juergen Stuber <starblue@users.sourceforge.net>
  58. * - log major, minor instead of possibly confusing device filename
  59. * - whitespace cleanup
  60. * 2004-03-12 - 0.9 Juergen Stuber <starblue@users.sourceforge.net>
  61. * - normalize whitespace in debug messages
  62. * - take care about endianness in control message responses
  63. * 2004-03-13 - 0.91 Juergen Stuber <starblue@users.sourceforge.net>
  64. * - make default intervals longer to accommodate current EHCI driver
  65. * 2004-03-19 - 0.92 Juergen Stuber <starblue@users.sourceforge.net>
  66. * - replaced atomic_t by memory barriers
  67. * 2004-04-21 - 0.93 Juergen Stuber <starblue@users.sourceforge.net>
  68. * - wait for completion of write urb in release (needed for remotecontrol)
  69. * - corrected poll for write direction (missing negation)
  70. * 2004-04-22 - 0.94 Juergen Stuber <starblue@users.sourceforge.net>
  71. * - make device locking interruptible
  72. * 2004-04-30 - 0.95 Juergen Stuber <starblue@users.sourceforge.net>
  73. * - check for valid udev on resubmitting and unlinking urbs
  74. * 2004-08-03 - 0.96 Juergen Stuber <starblue@users.sourceforge.net>
  75. * - move reset into open to clean out spurious data
  76. */
  77. #include <linux/kernel.h>
  78. #include <linux/errno.h>
  79. #include <linux/init.h>
  80. #include <linux/slab.h>
  81. #include <linux/module.h>
  82. #include <linux/completion.h>
  83. #include <linux/mutex.h>
  84. #include <asm/uaccess.h>
  85. #include <linux/usb.h>
  86. #include <linux/poll.h>
  87. #ifdef CONFIG_USB_DEBUG
  88. static int debug = 4;
  89. #else
  90. static int debug = 0;
  91. #endif
  92. /* Use our own dbg macro */
  93. #undef dbg
  94. #define dbg(lvl, format, arg...) do { if (debug >= lvl) printk(KERN_DEBUG __FILE__ ": " format "\n", ## arg); } while (0)
  95. /* Version Information */
  96. #define DRIVER_VERSION "v0.96"
  97. #define DRIVER_AUTHOR "Juergen Stuber <starblue@sourceforge.net>"
  98. #define DRIVER_DESC "LEGO USB Tower Driver"
  99. /* Module parameters */
  100. module_param(debug, int, S_IRUGO | S_IWUSR);
  101. MODULE_PARM_DESC(debug, "Debug enabled or not");
  102. /* The defaults are chosen to work with the latest versions of leJOS and NQC.
  103. */
  104. /* Some legacy software likes to receive packets in one piece.
  105. * In this case read_buffer_size should exceed the maximal packet length
  106. * (417 for datalog uploads), and packet_timeout should be set.
  107. */
  108. static int read_buffer_size = 480;
  109. module_param(read_buffer_size, int, 0);
  110. MODULE_PARM_DESC(read_buffer_size, "Read buffer size");
  111. /* Some legacy software likes to send packets in one piece.
  112. * In this case write_buffer_size should exceed the maximal packet length
  113. * (417 for firmware and program downloads).
  114. * A problem with long writes is that the following read may time out
  115. * if the software is not prepared to wait long enough.
  116. */
  117. static int write_buffer_size = 480;
  118. module_param(write_buffer_size, int, 0);
  119. MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
  120. /* Some legacy software expects reads to contain whole LASM packets.
  121. * To achieve this, characters which arrive before a packet timeout
  122. * occurs will be returned in a single read operation.
  123. * A problem with long reads is that the software may time out
  124. * if it is not prepared to wait long enough.
  125. * The packet timeout should be greater than the time between the
  126. * reception of subsequent characters, which should arrive about
  127. * every 5ms for the standard 2400 baud.
  128. * Set it to 0 to disable.
  129. */
  130. static int packet_timeout = 50;
  131. module_param(packet_timeout, int, 0);
  132. MODULE_PARM_DESC(packet_timeout, "Packet timeout in ms");
  133. /* Some legacy software expects blocking reads to time out.
  134. * Timeout occurs after the specified time of read and write inactivity.
  135. * Set it to 0 to disable.
  136. */
  137. static int read_timeout = 200;
  138. module_param(read_timeout, int, 0);
  139. MODULE_PARM_DESC(read_timeout, "Read timeout in ms");
  140. /* As of kernel version 2.6.4 ehci-hcd uses an
  141. * "only one interrupt transfer per frame" shortcut
  142. * to simplify the scheduling of periodic transfers.
  143. * This conflicts with our standard 1ms intervals for in and out URBs.
  144. * We use default intervals of 2ms for in and 8ms for out transfers,
  145. * which is fast enough for 2400 baud and allows a small additional load.
  146. * Increase the interval to allow more devices that do interrupt transfers,
  147. * or set to 0 to use the standard interval from the endpoint descriptors.
  148. */
  149. static int interrupt_in_interval = 2;
  150. module_param(interrupt_in_interval, int, 0);
  151. MODULE_PARM_DESC(interrupt_in_interval, "Interrupt in interval in ms");
  152. static int interrupt_out_interval = 8;
  153. module_param(interrupt_out_interval, int, 0);
  154. MODULE_PARM_DESC(interrupt_out_interval, "Interrupt out interval in ms");
  155. /* Define these values to match your device */
  156. #define LEGO_USB_TOWER_VENDOR_ID 0x0694
  157. #define LEGO_USB_TOWER_PRODUCT_ID 0x0001
  158. /* Vendor requests */
  159. #define LEGO_USB_TOWER_REQUEST_RESET 0x04
  160. #define LEGO_USB_TOWER_REQUEST_GET_VERSION 0xFD
  161. struct tower_reset_reply {
  162. __le16 size; /* little-endian */
  163. __u8 err_code;
  164. __u8 spare;
  165. } __attribute__ ((packed));
  166. struct tower_get_version_reply {
  167. __le16 size; /* little-endian */
  168. __u8 err_code;
  169. __u8 spare;
  170. __u8 major;
  171. __u8 minor;
  172. __le16 build_no; /* little-endian */
  173. } __attribute__ ((packed));
  174. /* table of devices that work with this driver */
  175. static struct usb_device_id tower_table [] = {
  176. { USB_DEVICE(LEGO_USB_TOWER_VENDOR_ID, LEGO_USB_TOWER_PRODUCT_ID) },
  177. { } /* Terminating entry */
  178. };
  179. MODULE_DEVICE_TABLE (usb, tower_table);
  180. static DEFINE_MUTEX(open_disc_mutex);
  181. #define LEGO_USB_TOWER_MINOR_BASE 160
  182. /* Structure to hold all of our device specific stuff */
  183. struct lego_usb_tower {
  184. struct mutex lock; /* locks this structure */
  185. struct usb_device* udev; /* save off the usb device pointer */
  186. unsigned char minor; /* the starting minor number for this device */
  187. int open_count; /* number of times this port has been opened */
  188. char* read_buffer;
  189. size_t read_buffer_length; /* this much came in */
  190. size_t read_packet_length; /* this much will be returned on read */
  191. spinlock_t read_buffer_lock;
  192. int packet_timeout_jiffies;
  193. unsigned long read_last_arrival;
  194. wait_queue_head_t read_wait;
  195. wait_queue_head_t write_wait;
  196. char* interrupt_in_buffer;
  197. struct usb_endpoint_descriptor* interrupt_in_endpoint;
  198. struct urb* interrupt_in_urb;
  199. int interrupt_in_interval;
  200. int interrupt_in_running;
  201. int interrupt_in_done;
  202. char* interrupt_out_buffer;
  203. struct usb_endpoint_descriptor* interrupt_out_endpoint;
  204. struct urb* interrupt_out_urb;
  205. int interrupt_out_interval;
  206. int interrupt_out_busy;
  207. };
  208. /* local function prototypes */
  209. static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos);
  210. static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
  211. static inline void tower_delete (struct lego_usb_tower *dev);
  212. static int tower_open (struct inode *inode, struct file *file);
  213. static int tower_release (struct inode *inode, struct file *file);
  214. static unsigned int tower_poll (struct file *file, poll_table *wait);
  215. static loff_t tower_llseek (struct file *file, loff_t off, int whence);
  216. static void tower_abort_transfers (struct lego_usb_tower *dev);
  217. static void tower_check_for_read_packet (struct lego_usb_tower *dev);
  218. static void tower_interrupt_in_callback (struct urb *urb);
  219. static void tower_interrupt_out_callback (struct urb *urb);
  220. static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id);
  221. static void tower_disconnect (struct usb_interface *interface);
  222. /* file operations needed when we register this driver */
  223. static const struct file_operations tower_fops = {
  224. .owner = THIS_MODULE,
  225. .read = tower_read,
  226. .write = tower_write,
  227. .open = tower_open,
  228. .release = tower_release,
  229. .poll = tower_poll,
  230. .llseek = tower_llseek,
  231. };
  232. static char *legousbtower_nodename(struct device *dev)
  233. {
  234. return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
  235. }
  236. /*
  237. * usb class driver info in order to get a minor number from the usb core,
  238. * and to have the device registered with the driver core
  239. */
  240. static struct usb_class_driver tower_class = {
  241. .name = "legousbtower%d",
  242. .nodename = legousbtower_nodename,
  243. .fops = &tower_fops,
  244. .minor_base = LEGO_USB_TOWER_MINOR_BASE,
  245. };
  246. /* usb specific object needed to register this driver with the usb subsystem */
  247. static struct usb_driver tower_driver = {
  248. .name = "legousbtower",
  249. .probe = tower_probe,
  250. .disconnect = tower_disconnect,
  251. .id_table = tower_table,
  252. };
  253. /**
  254. * lego_usb_tower_debug_data
  255. */
  256. static inline void lego_usb_tower_debug_data (int level, const char *function, int size, const unsigned char *data)
  257. {
  258. int i;
  259. if (debug < level)
  260. return;
  261. printk (KERN_DEBUG __FILE__": %s - length = %d, data = ", function, size);
  262. for (i = 0; i < size; ++i) {
  263. printk ("%.2x ", data[i]);
  264. }
  265. printk ("\n");
  266. }
  267. /**
  268. * tower_delete
  269. */
  270. static inline void tower_delete (struct lego_usb_tower *dev)
  271. {
  272. dbg(2, "%s: enter", __func__);
  273. tower_abort_transfers (dev);
  274. /* free data structures */
  275. usb_free_urb(dev->interrupt_in_urb);
  276. usb_free_urb(dev->interrupt_out_urb);
  277. kfree (dev->read_buffer);
  278. kfree (dev->interrupt_in_buffer);
  279. kfree (dev->interrupt_out_buffer);
  280. kfree (dev);
  281. dbg(2, "%s: leave", __func__);
  282. }
  283. /**
  284. * tower_open
  285. */
  286. static int tower_open (struct inode *inode, struct file *file)
  287. {
  288. struct lego_usb_tower *dev = NULL;
  289. int subminor;
  290. int retval = 0;
  291. struct usb_interface *interface;
  292. struct tower_reset_reply reset_reply;
  293. int result;
  294. dbg(2, "%s: enter", __func__);
  295. nonseekable_open(inode, file);
  296. subminor = iminor(inode);
  297. interface = usb_find_interface (&tower_driver, subminor);
  298. if (!interface) {
  299. err ("%s - error, can't find device for minor %d",
  300. __func__, subminor);
  301. retval = -ENODEV;
  302. goto exit;
  303. }
  304. mutex_lock(&open_disc_mutex);
  305. dev = usb_get_intfdata(interface);
  306. if (!dev) {
  307. mutex_unlock(&open_disc_mutex);
  308. retval = -ENODEV;
  309. goto exit;
  310. }
  311. /* lock this device */
  312. if (mutex_lock_interruptible(&dev->lock)) {
  313. mutex_unlock(&open_disc_mutex);
  314. retval = -ERESTARTSYS;
  315. goto exit;
  316. }
  317. /* allow opening only once */
  318. if (dev->open_count) {
  319. mutex_unlock(&open_disc_mutex);
  320. retval = -EBUSY;
  321. goto unlock_exit;
  322. }
  323. dev->open_count = 1;
  324. mutex_unlock(&open_disc_mutex);
  325. /* reset the tower */
  326. result = usb_control_msg (dev->udev,
  327. usb_rcvctrlpipe(dev->udev, 0),
  328. LEGO_USB_TOWER_REQUEST_RESET,
  329. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  330. 0,
  331. 0,
  332. &reset_reply,
  333. sizeof(reset_reply),
  334. 1000);
  335. if (result < 0) {
  336. err("LEGO USB Tower reset control request failed");
  337. retval = result;
  338. goto unlock_exit;
  339. }
  340. /* initialize in direction */
  341. dev->read_buffer_length = 0;
  342. dev->read_packet_length = 0;
  343. usb_fill_int_urb (dev->interrupt_in_urb,
  344. dev->udev,
  345. usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress),
  346. dev->interrupt_in_buffer,
  347. le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
  348. tower_interrupt_in_callback,
  349. dev,
  350. dev->interrupt_in_interval);
  351. dev->interrupt_in_running = 1;
  352. dev->interrupt_in_done = 0;
  353. mb();
  354. retval = usb_submit_urb (dev->interrupt_in_urb, GFP_KERNEL);
  355. if (retval) {
  356. err("Couldn't submit interrupt_in_urb %d", retval);
  357. dev->interrupt_in_running = 0;
  358. dev->open_count = 0;
  359. goto unlock_exit;
  360. }
  361. /* save device in the file's private structure */
  362. file->private_data = dev;
  363. unlock_exit:
  364. mutex_unlock(&dev->lock);
  365. exit:
  366. dbg(2, "%s: leave, return value %d ", __func__, retval);
  367. return retval;
  368. }
  369. /**
  370. * tower_release
  371. */
  372. static int tower_release (struct inode *inode, struct file *file)
  373. {
  374. struct lego_usb_tower *dev;
  375. int retval = 0;
  376. dbg(2, "%s: enter", __func__);
  377. dev = (struct lego_usb_tower *)file->private_data;
  378. if (dev == NULL) {
  379. dbg(1, "%s: object is NULL", __func__);
  380. retval = -ENODEV;
  381. goto exit_nolock;
  382. }
  383. mutex_lock(&open_disc_mutex);
  384. if (mutex_lock_interruptible(&dev->lock)) {
  385. retval = -ERESTARTSYS;
  386. goto exit;
  387. }
  388. if (dev->open_count != 1) {
  389. dbg(1, "%s: device not opened exactly once", __func__);
  390. retval = -ENODEV;
  391. goto unlock_exit;
  392. }
  393. if (dev->udev == NULL) {
  394. /* the device was unplugged before the file was released */
  395. /* unlock here as tower_delete frees dev */
  396. mutex_unlock(&dev->lock);
  397. tower_delete (dev);
  398. goto exit;
  399. }
  400. /* wait until write transfer is finished */
  401. if (dev->interrupt_out_busy) {
  402. wait_event_interruptible_timeout (dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
  403. }
  404. tower_abort_transfers (dev);
  405. dev->open_count = 0;
  406. unlock_exit:
  407. mutex_unlock(&dev->lock);
  408. exit:
  409. mutex_unlock(&open_disc_mutex);
  410. exit_nolock:
  411. dbg(2, "%s: leave, return value %d", __func__, retval);
  412. return retval;
  413. }
  414. /**
  415. * tower_abort_transfers
  416. * aborts transfers and frees associated data structures
  417. */
  418. static void tower_abort_transfers (struct lego_usb_tower *dev)
  419. {
  420. dbg(2, "%s: enter", __func__);
  421. if (dev == NULL) {
  422. dbg(1, "%s: dev is null", __func__);
  423. goto exit;
  424. }
  425. /* shutdown transfer */
  426. if (dev->interrupt_in_running) {
  427. dev->interrupt_in_running = 0;
  428. mb();
  429. if (dev->udev)
  430. usb_kill_urb (dev->interrupt_in_urb);
  431. }
  432. if (dev->interrupt_out_busy && dev->udev)
  433. usb_kill_urb(dev->interrupt_out_urb);
  434. exit:
  435. dbg(2, "%s: leave", __func__);
  436. }
  437. /**
  438. * tower_check_for_read_packet
  439. *
  440. * To get correct semantics for signals and non-blocking I/O
  441. * with packetizing we pretend not to see any data in the read buffer
  442. * until it has been there unchanged for at least
  443. * dev->packet_timeout_jiffies, or until the buffer is full.
  444. */
  445. static void tower_check_for_read_packet (struct lego_usb_tower *dev)
  446. {
  447. spin_lock_irq (&dev->read_buffer_lock);
  448. if (!packet_timeout
  449. || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies)
  450. || dev->read_buffer_length == read_buffer_size) {
  451. dev->read_packet_length = dev->read_buffer_length;
  452. }
  453. dev->interrupt_in_done = 0;
  454. spin_unlock_irq (&dev->read_buffer_lock);
  455. }
  456. /**
  457. * tower_poll
  458. */
  459. static unsigned int tower_poll (struct file *file, poll_table *wait)
  460. {
  461. struct lego_usb_tower *dev;
  462. unsigned int mask = 0;
  463. dbg(2, "%s: enter", __func__);
  464. dev = file->private_data;
  465. poll_wait(file, &dev->read_wait, wait);
  466. poll_wait(file, &dev->write_wait, wait);
  467. tower_check_for_read_packet(dev);
  468. if (dev->read_packet_length > 0) {
  469. mask |= POLLIN | POLLRDNORM;
  470. }
  471. if (!dev->interrupt_out_busy) {
  472. mask |= POLLOUT | POLLWRNORM;
  473. }
  474. dbg(2, "%s: leave, mask = %d", __func__, mask);
  475. return mask;
  476. }
  477. /**
  478. * tower_llseek
  479. */
  480. static loff_t tower_llseek (struct file *file, loff_t off, int whence)
  481. {
  482. return -ESPIPE; /* unseekable */
  483. }
  484. /**
  485. * tower_read
  486. */
  487. static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  488. {
  489. struct lego_usb_tower *dev;
  490. size_t bytes_to_read;
  491. int i;
  492. int retval = 0;
  493. unsigned long timeout = 0;
  494. dbg(2, "%s: enter, count = %Zd", __func__, count);
  495. dev = (struct lego_usb_tower *)file->private_data;
  496. /* lock this object */
  497. if (mutex_lock_interruptible(&dev->lock)) {
  498. retval = -ERESTARTSYS;
  499. goto exit;
  500. }
  501. /* verify that the device wasn't unplugged */
  502. if (dev->udev == NULL) {
  503. retval = -ENODEV;
  504. err("No device or device unplugged %d", retval);
  505. goto unlock_exit;
  506. }
  507. /* verify that we actually have some data to read */
  508. if (count == 0) {
  509. dbg(1, "%s: read request of 0 bytes", __func__);
  510. goto unlock_exit;
  511. }
  512. if (read_timeout) {
  513. timeout = jiffies + read_timeout * HZ / 1000;
  514. }
  515. /* wait for data */
  516. tower_check_for_read_packet (dev);
  517. while (dev->read_packet_length == 0) {
  518. if (file->f_flags & O_NONBLOCK) {
  519. retval = -EAGAIN;
  520. goto unlock_exit;
  521. }
  522. retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies);
  523. if (retval < 0) {
  524. goto unlock_exit;
  525. }
  526. /* reset read timeout during read or write activity */
  527. if (read_timeout
  528. && (dev->read_buffer_length || dev->interrupt_out_busy)) {
  529. timeout = jiffies + read_timeout * HZ / 1000;
  530. }
  531. /* check for read timeout */
  532. if (read_timeout && time_after (jiffies, timeout)) {
  533. retval = -ETIMEDOUT;
  534. goto unlock_exit;
  535. }
  536. tower_check_for_read_packet (dev);
  537. }
  538. /* copy the data from read_buffer into userspace */
  539. bytes_to_read = min(count, dev->read_packet_length);
  540. if (copy_to_user (buffer, dev->read_buffer, bytes_to_read)) {
  541. retval = -EFAULT;
  542. goto unlock_exit;
  543. }
  544. spin_lock_irq (&dev->read_buffer_lock);
  545. dev->read_buffer_length -= bytes_to_read;
  546. dev->read_packet_length -= bytes_to_read;
  547. for (i=0; i<dev->read_buffer_length; i++) {
  548. dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read];
  549. }
  550. spin_unlock_irq (&dev->read_buffer_lock);
  551. retval = bytes_to_read;
  552. unlock_exit:
  553. /* unlock the device */
  554. mutex_unlock(&dev->lock);
  555. exit:
  556. dbg(2, "%s: leave, return value %d", __func__, retval);
  557. return retval;
  558. }
  559. /**
  560. * tower_write
  561. */
  562. static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  563. {
  564. struct lego_usb_tower *dev;
  565. size_t bytes_to_write;
  566. int retval = 0;
  567. dbg(2, "%s: enter, count = %Zd", __func__, count);
  568. dev = (struct lego_usb_tower *)file->private_data;
  569. /* lock this object */
  570. if (mutex_lock_interruptible(&dev->lock)) {
  571. retval = -ERESTARTSYS;
  572. goto exit;
  573. }
  574. /* verify that the device wasn't unplugged */
  575. if (dev->udev == NULL) {
  576. retval = -ENODEV;
  577. err("No device or device unplugged %d", retval);
  578. goto unlock_exit;
  579. }
  580. /* verify that we actually have some data to write */
  581. if (count == 0) {
  582. dbg(1, "%s: write request of 0 bytes", __func__);
  583. goto unlock_exit;
  584. }
  585. /* wait until previous transfer is finished */
  586. while (dev->interrupt_out_busy) {
  587. if (file->f_flags & O_NONBLOCK) {
  588. retval = -EAGAIN;
  589. goto unlock_exit;
  590. }
  591. retval = wait_event_interruptible (dev->write_wait, !dev->interrupt_out_busy);
  592. if (retval) {
  593. goto unlock_exit;
  594. }
  595. }
  596. /* write the data into interrupt_out_buffer from userspace */
  597. bytes_to_write = min_t(int, count, write_buffer_size);
  598. dbg(4, "%s: count = %Zd, bytes_to_write = %Zd", __func__, count, bytes_to_write);
  599. if (copy_from_user (dev->interrupt_out_buffer, buffer, bytes_to_write)) {
  600. retval = -EFAULT;
  601. goto unlock_exit;
  602. }
  603. /* send off the urb */
  604. usb_fill_int_urb(dev->interrupt_out_urb,
  605. dev->udev,
  606. usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
  607. dev->interrupt_out_buffer,
  608. bytes_to_write,
  609. tower_interrupt_out_callback,
  610. dev,
  611. dev->interrupt_out_interval);
  612. dev->interrupt_out_busy = 1;
  613. wmb();
  614. retval = usb_submit_urb (dev->interrupt_out_urb, GFP_KERNEL);
  615. if (retval) {
  616. dev->interrupt_out_busy = 0;
  617. err("Couldn't submit interrupt_out_urb %d", retval);
  618. goto unlock_exit;
  619. }
  620. retval = bytes_to_write;
  621. unlock_exit:
  622. /* unlock the device */
  623. mutex_unlock(&dev->lock);
  624. exit:
  625. dbg(2, "%s: leave, return value %d", __func__, retval);
  626. return retval;
  627. }
  628. /**
  629. * tower_interrupt_in_callback
  630. */
  631. static void tower_interrupt_in_callback (struct urb *urb)
  632. {
  633. struct lego_usb_tower *dev = urb->context;
  634. int status = urb->status;
  635. int retval;
  636. dbg(4, "%s: enter, status %d", __func__, status);
  637. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  638. if (status) {
  639. if (status == -ENOENT ||
  640. status == -ECONNRESET ||
  641. status == -ESHUTDOWN) {
  642. goto exit;
  643. } else {
  644. dbg(1, "%s: nonzero status received: %d", __func__, status);
  645. goto resubmit; /* maybe we can recover */
  646. }
  647. }
  648. if (urb->actual_length > 0) {
  649. spin_lock (&dev->read_buffer_lock);
  650. if (dev->read_buffer_length + urb->actual_length < read_buffer_size) {
  651. memcpy (dev->read_buffer + dev->read_buffer_length,
  652. dev->interrupt_in_buffer,
  653. urb->actual_length);
  654. dev->read_buffer_length += urb->actual_length;
  655. dev->read_last_arrival = jiffies;
  656. dbg(3, "%s: received %d bytes", __func__, urb->actual_length);
  657. } else {
  658. printk(KERN_WARNING "%s: read_buffer overflow, %d bytes dropped", __func__, urb->actual_length);
  659. }
  660. spin_unlock (&dev->read_buffer_lock);
  661. }
  662. resubmit:
  663. /* resubmit if we're still running */
  664. if (dev->interrupt_in_running && dev->udev) {
  665. retval = usb_submit_urb (dev->interrupt_in_urb, GFP_ATOMIC);
  666. if (retval) {
  667. err("%s: usb_submit_urb failed (%d)", __func__, retval);
  668. }
  669. }
  670. exit:
  671. dev->interrupt_in_done = 1;
  672. wake_up_interruptible (&dev->read_wait);
  673. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  674. dbg(4, "%s: leave, status %d", __func__, status);
  675. }
  676. /**
  677. * tower_interrupt_out_callback
  678. */
  679. static void tower_interrupt_out_callback (struct urb *urb)
  680. {
  681. struct lego_usb_tower *dev = urb->context;
  682. int status = urb->status;
  683. dbg(4, "%s: enter, status %d", __func__, status);
  684. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  685. /* sync/async unlink faults aren't errors */
  686. if (status && !(status == -ENOENT ||
  687. status == -ECONNRESET ||
  688. status == -ESHUTDOWN)) {
  689. dbg(1, "%s - nonzero write bulk status received: %d",
  690. __func__, status);
  691. }
  692. dev->interrupt_out_busy = 0;
  693. wake_up_interruptible(&dev->write_wait);
  694. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  695. dbg(4, "%s: leave, status %d", __func__, status);
  696. }
  697. /**
  698. * tower_probe
  699. *
  700. * Called by the usb core when a new device is connected that it thinks
  701. * this driver might be interested in.
  702. */
  703. static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id)
  704. {
  705. struct usb_device *udev = interface_to_usbdev(interface);
  706. struct lego_usb_tower *dev = NULL;
  707. struct usb_host_interface *iface_desc;
  708. struct usb_endpoint_descriptor* endpoint;
  709. struct tower_get_version_reply get_version_reply;
  710. int i;
  711. int retval = -ENOMEM;
  712. int result;
  713. dbg(2, "%s: enter", __func__);
  714. if (udev == NULL)
  715. dev_info(&interface->dev, "udev is NULL.\n");
  716. /* allocate memory for our device state and initialize it */
  717. dev = kmalloc (sizeof(struct lego_usb_tower), GFP_KERNEL);
  718. if (dev == NULL) {
  719. err ("Out of memory");
  720. goto exit;
  721. }
  722. mutex_init(&dev->lock);
  723. dev->udev = udev;
  724. dev->open_count = 0;
  725. dev->read_buffer = NULL;
  726. dev->read_buffer_length = 0;
  727. dev->read_packet_length = 0;
  728. spin_lock_init (&dev->read_buffer_lock);
  729. dev->packet_timeout_jiffies = packet_timeout * HZ / 1000;
  730. dev->read_last_arrival = jiffies;
  731. init_waitqueue_head (&dev->read_wait);
  732. init_waitqueue_head (&dev->write_wait);
  733. dev->interrupt_in_buffer = NULL;
  734. dev->interrupt_in_endpoint = NULL;
  735. dev->interrupt_in_urb = NULL;
  736. dev->interrupt_in_running = 0;
  737. dev->interrupt_in_done = 0;
  738. dev->interrupt_out_buffer = NULL;
  739. dev->interrupt_out_endpoint = NULL;
  740. dev->interrupt_out_urb = NULL;
  741. dev->interrupt_out_busy = 0;
  742. iface_desc = interface->cur_altsetting;
  743. /* set up the endpoint information */
  744. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  745. endpoint = &iface_desc->endpoint[i].desc;
  746. if (usb_endpoint_xfer_int(endpoint)) {
  747. if (usb_endpoint_dir_in(endpoint))
  748. dev->interrupt_in_endpoint = endpoint;
  749. else
  750. dev->interrupt_out_endpoint = endpoint;
  751. }
  752. }
  753. if(dev->interrupt_in_endpoint == NULL) {
  754. err("interrupt in endpoint not found");
  755. goto error;
  756. }
  757. if (dev->interrupt_out_endpoint == NULL) {
  758. err("interrupt out endpoint not found");
  759. goto error;
  760. }
  761. dev->read_buffer = kmalloc (read_buffer_size, GFP_KERNEL);
  762. if (!dev->read_buffer) {
  763. err("Couldn't allocate read_buffer");
  764. goto error;
  765. }
  766. dev->interrupt_in_buffer = kmalloc (le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), GFP_KERNEL);
  767. if (!dev->interrupt_in_buffer) {
  768. err("Couldn't allocate interrupt_in_buffer");
  769. goto error;
  770. }
  771. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  772. if (!dev->interrupt_in_urb) {
  773. err("Couldn't allocate interrupt_in_urb");
  774. goto error;
  775. }
  776. dev->interrupt_out_buffer = kmalloc (write_buffer_size, GFP_KERNEL);
  777. if (!dev->interrupt_out_buffer) {
  778. err("Couldn't allocate interrupt_out_buffer");
  779. goto error;
  780. }
  781. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  782. if (!dev->interrupt_out_urb) {
  783. err("Couldn't allocate interrupt_out_urb");
  784. goto error;
  785. }
  786. dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
  787. dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
  788. /* we can register the device now, as it is ready */
  789. usb_set_intfdata (interface, dev);
  790. retval = usb_register_dev (interface, &tower_class);
  791. if (retval) {
  792. /* something prevented us from registering this driver */
  793. err ("Not able to get a minor for this device.");
  794. usb_set_intfdata (interface, NULL);
  795. goto error;
  796. }
  797. dev->minor = interface->minor;
  798. /* let the user know what node this device is now attached to */
  799. dev_info(&interface->dev, "LEGO USB Tower #%d now attached to major "
  800. "%d minor %d\n", (dev->minor - LEGO_USB_TOWER_MINOR_BASE),
  801. USB_MAJOR, dev->minor);
  802. /* get the firmware version and log it */
  803. result = usb_control_msg (udev,
  804. usb_rcvctrlpipe(udev, 0),
  805. LEGO_USB_TOWER_REQUEST_GET_VERSION,
  806. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  807. 0,
  808. 0,
  809. &get_version_reply,
  810. sizeof(get_version_reply),
  811. 1000);
  812. if (result < 0) {
  813. err("LEGO USB Tower get version control request failed");
  814. retval = result;
  815. goto error;
  816. }
  817. dev_info(&interface->dev, "LEGO USB Tower firmware version is %d.%d "
  818. "build %d\n", get_version_reply.major,
  819. get_version_reply.minor,
  820. le16_to_cpu(get_version_reply.build_no));
  821. exit:
  822. dbg(2, "%s: leave, return value 0x%.8lx (dev)", __func__, (long) dev);
  823. return retval;
  824. error:
  825. tower_delete(dev);
  826. return retval;
  827. }
  828. /**
  829. * tower_disconnect
  830. *
  831. * Called by the usb core when the device is removed from the system.
  832. */
  833. static void tower_disconnect (struct usb_interface *interface)
  834. {
  835. struct lego_usb_tower *dev;
  836. int minor;
  837. dbg(2, "%s: enter", __func__);
  838. dev = usb_get_intfdata (interface);
  839. mutex_lock(&open_disc_mutex);
  840. usb_set_intfdata (interface, NULL);
  841. minor = dev->minor;
  842. /* give back our minor */
  843. usb_deregister_dev (interface, &tower_class);
  844. mutex_lock(&dev->lock);
  845. mutex_unlock(&open_disc_mutex);
  846. /* if the device is not opened, then we clean up right now */
  847. if (!dev->open_count) {
  848. mutex_unlock(&dev->lock);
  849. tower_delete (dev);
  850. } else {
  851. dev->udev = NULL;
  852. mutex_unlock(&dev->lock);
  853. }
  854. dev_info(&interface->dev, "LEGO USB Tower #%d now disconnected\n",
  855. (minor - LEGO_USB_TOWER_MINOR_BASE));
  856. dbg(2, "%s: leave", __func__);
  857. }
  858. /**
  859. * lego_usb_tower_init
  860. */
  861. static int __init lego_usb_tower_init(void)
  862. {
  863. int result;
  864. int retval = 0;
  865. dbg(2, "%s: enter", __func__);
  866. /* register this driver with the USB subsystem */
  867. result = usb_register(&tower_driver);
  868. if (result < 0) {
  869. err("usb_register failed for the "__FILE__" driver. Error number %d", result);
  870. retval = -1;
  871. goto exit;
  872. }
  873. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  874. DRIVER_DESC "\n");
  875. exit:
  876. dbg(2, "%s: leave, return value %d", __func__, retval);
  877. return retval;
  878. }
  879. /**
  880. * lego_usb_tower_exit
  881. */
  882. static void __exit lego_usb_tower_exit(void)
  883. {
  884. dbg(2, "%s: enter", __func__);
  885. /* deregister this driver with the USB subsystem */
  886. usb_deregister (&tower_driver);
  887. dbg(2, "%s: leave", __func__);
  888. }
  889. module_init (lego_usb_tower_init);
  890. module_exit (lego_usb_tower_exit);
  891. MODULE_AUTHOR(DRIVER_AUTHOR);
  892. MODULE_DESCRIPTION(DRIVER_DESC);
  893. #ifdef MODULE_LICENSE
  894. MODULE_LICENSE("GPL");
  895. #endif