legousbtower.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. /*
  233. * usb class driver info in order to get a minor number from the usb core,
  234. * and to have the device registered with the driver core
  235. */
  236. static struct usb_class_driver tower_class = {
  237. .name = "legousbtower%d",
  238. .fops = &tower_fops,
  239. .minor_base = LEGO_USB_TOWER_MINOR_BASE,
  240. };
  241. /* usb specific object needed to register this driver with the usb subsystem */
  242. static struct usb_driver tower_driver = {
  243. .name = "legousbtower",
  244. .probe = tower_probe,
  245. .disconnect = tower_disconnect,
  246. .id_table = tower_table,
  247. };
  248. /**
  249. * lego_usb_tower_debug_data
  250. */
  251. static inline void lego_usb_tower_debug_data (int level, const char *function, int size, const unsigned char *data)
  252. {
  253. int i;
  254. if (debug < level)
  255. return;
  256. printk (KERN_DEBUG __FILE__": %s - length = %d, data = ", function, size);
  257. for (i = 0; i < size; ++i) {
  258. printk ("%.2x ", data[i]);
  259. }
  260. printk ("\n");
  261. }
  262. /**
  263. * tower_delete
  264. */
  265. static inline void tower_delete (struct lego_usb_tower *dev)
  266. {
  267. dbg(2, "%s: enter", __func__);
  268. tower_abort_transfers (dev);
  269. /* free data structures */
  270. usb_free_urb(dev->interrupt_in_urb);
  271. usb_free_urb(dev->interrupt_out_urb);
  272. kfree (dev->read_buffer);
  273. kfree (dev->interrupt_in_buffer);
  274. kfree (dev->interrupt_out_buffer);
  275. kfree (dev);
  276. dbg(2, "%s: leave", __func__);
  277. }
  278. /**
  279. * tower_open
  280. */
  281. static int tower_open (struct inode *inode, struct file *file)
  282. {
  283. struct lego_usb_tower *dev = NULL;
  284. int subminor;
  285. int retval = 0;
  286. struct usb_interface *interface;
  287. struct tower_reset_reply reset_reply;
  288. int result;
  289. dbg(2, "%s: enter", __func__);
  290. nonseekable_open(inode, file);
  291. subminor = iminor(inode);
  292. interface = usb_find_interface (&tower_driver, subminor);
  293. if (!interface) {
  294. err ("%s - error, can't find device for minor %d",
  295. __func__, subminor);
  296. retval = -ENODEV;
  297. goto exit;
  298. }
  299. mutex_lock(&open_disc_mutex);
  300. dev = usb_get_intfdata(interface);
  301. if (!dev) {
  302. mutex_unlock(&open_disc_mutex);
  303. retval = -ENODEV;
  304. goto exit;
  305. }
  306. /* lock this device */
  307. if (mutex_lock_interruptible(&dev->lock)) {
  308. mutex_unlock(&open_disc_mutex);
  309. retval = -ERESTARTSYS;
  310. goto exit;
  311. }
  312. /* allow opening only once */
  313. if (dev->open_count) {
  314. mutex_unlock(&open_disc_mutex);
  315. retval = -EBUSY;
  316. goto unlock_exit;
  317. }
  318. dev->open_count = 1;
  319. mutex_unlock(&open_disc_mutex);
  320. /* reset the tower */
  321. result = usb_control_msg (dev->udev,
  322. usb_rcvctrlpipe(dev->udev, 0),
  323. LEGO_USB_TOWER_REQUEST_RESET,
  324. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  325. 0,
  326. 0,
  327. &reset_reply,
  328. sizeof(reset_reply),
  329. 1000);
  330. if (result < 0) {
  331. err("LEGO USB Tower reset control request failed");
  332. retval = result;
  333. goto unlock_exit;
  334. }
  335. /* initialize in direction */
  336. dev->read_buffer_length = 0;
  337. dev->read_packet_length = 0;
  338. usb_fill_int_urb (dev->interrupt_in_urb,
  339. dev->udev,
  340. usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress),
  341. dev->interrupt_in_buffer,
  342. le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
  343. tower_interrupt_in_callback,
  344. dev,
  345. dev->interrupt_in_interval);
  346. dev->interrupt_in_running = 1;
  347. dev->interrupt_in_done = 0;
  348. mb();
  349. retval = usb_submit_urb (dev->interrupt_in_urb, GFP_KERNEL);
  350. if (retval) {
  351. err("Couldn't submit interrupt_in_urb %d", retval);
  352. dev->interrupt_in_running = 0;
  353. dev->open_count = 0;
  354. goto unlock_exit;
  355. }
  356. /* save device in the file's private structure */
  357. file->private_data = dev;
  358. unlock_exit:
  359. mutex_unlock(&dev->lock);
  360. exit:
  361. dbg(2, "%s: leave, return value %d ", __func__, retval);
  362. return retval;
  363. }
  364. /**
  365. * tower_release
  366. */
  367. static int tower_release (struct inode *inode, struct file *file)
  368. {
  369. struct lego_usb_tower *dev;
  370. int retval = 0;
  371. dbg(2, "%s: enter", __func__);
  372. dev = (struct lego_usb_tower *)file->private_data;
  373. if (dev == NULL) {
  374. dbg(1, "%s: object is NULL", __func__);
  375. retval = -ENODEV;
  376. goto exit_nolock;
  377. }
  378. mutex_lock(&open_disc_mutex);
  379. if (mutex_lock_interruptible(&dev->lock)) {
  380. retval = -ERESTARTSYS;
  381. goto exit;
  382. }
  383. if (dev->open_count != 1) {
  384. dbg(1, "%s: device not opened exactly once", __func__);
  385. retval = -ENODEV;
  386. goto unlock_exit;
  387. }
  388. if (dev->udev == NULL) {
  389. /* the device was unplugged before the file was released */
  390. /* unlock here as tower_delete frees dev */
  391. mutex_unlock(&dev->lock);
  392. tower_delete (dev);
  393. goto exit;
  394. }
  395. /* wait until write transfer is finished */
  396. if (dev->interrupt_out_busy) {
  397. wait_event_interruptible_timeout (dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
  398. }
  399. tower_abort_transfers (dev);
  400. dev->open_count = 0;
  401. unlock_exit:
  402. mutex_unlock(&dev->lock);
  403. exit:
  404. mutex_unlock(&open_disc_mutex);
  405. exit_nolock:
  406. dbg(2, "%s: leave, return value %d", __func__, retval);
  407. return retval;
  408. }
  409. /**
  410. * tower_abort_transfers
  411. * aborts transfers and frees associated data structures
  412. */
  413. static void tower_abort_transfers (struct lego_usb_tower *dev)
  414. {
  415. dbg(2, "%s: enter", __func__);
  416. if (dev == NULL) {
  417. dbg(1, "%s: dev is null", __func__);
  418. goto exit;
  419. }
  420. /* shutdown transfer */
  421. if (dev->interrupt_in_running) {
  422. dev->interrupt_in_running = 0;
  423. mb();
  424. if (dev->udev)
  425. usb_kill_urb (dev->interrupt_in_urb);
  426. }
  427. if (dev->interrupt_out_busy && dev->udev)
  428. usb_kill_urb(dev->interrupt_out_urb);
  429. exit:
  430. dbg(2, "%s: leave", __func__);
  431. }
  432. /**
  433. * tower_check_for_read_packet
  434. *
  435. * To get correct semantics for signals and non-blocking I/O
  436. * with packetizing we pretend not to see any data in the read buffer
  437. * until it has been there unchanged for at least
  438. * dev->packet_timeout_jiffies, or until the buffer is full.
  439. */
  440. static void tower_check_for_read_packet (struct lego_usb_tower *dev)
  441. {
  442. spin_lock_irq (&dev->read_buffer_lock);
  443. if (!packet_timeout
  444. || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies)
  445. || dev->read_buffer_length == read_buffer_size) {
  446. dev->read_packet_length = dev->read_buffer_length;
  447. }
  448. dev->interrupt_in_done = 0;
  449. spin_unlock_irq (&dev->read_buffer_lock);
  450. }
  451. /**
  452. * tower_poll
  453. */
  454. static unsigned int tower_poll (struct file *file, poll_table *wait)
  455. {
  456. struct lego_usb_tower *dev;
  457. unsigned int mask = 0;
  458. dbg(2, "%s: enter", __func__);
  459. dev = file->private_data;
  460. poll_wait(file, &dev->read_wait, wait);
  461. poll_wait(file, &dev->write_wait, wait);
  462. tower_check_for_read_packet(dev);
  463. if (dev->read_packet_length > 0) {
  464. mask |= POLLIN | POLLRDNORM;
  465. }
  466. if (!dev->interrupt_out_busy) {
  467. mask |= POLLOUT | POLLWRNORM;
  468. }
  469. dbg(2, "%s: leave, mask = %d", __func__, mask);
  470. return mask;
  471. }
  472. /**
  473. * tower_llseek
  474. */
  475. static loff_t tower_llseek (struct file *file, loff_t off, int whence)
  476. {
  477. return -ESPIPE; /* unseekable */
  478. }
  479. /**
  480. * tower_read
  481. */
  482. static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  483. {
  484. struct lego_usb_tower *dev;
  485. size_t bytes_to_read;
  486. int i;
  487. int retval = 0;
  488. unsigned long timeout = 0;
  489. dbg(2, "%s: enter, count = %Zd", __func__, count);
  490. dev = (struct lego_usb_tower *)file->private_data;
  491. /* lock this object */
  492. if (mutex_lock_interruptible(&dev->lock)) {
  493. retval = -ERESTARTSYS;
  494. goto exit;
  495. }
  496. /* verify that the device wasn't unplugged */
  497. if (dev->udev == NULL) {
  498. retval = -ENODEV;
  499. err("No device or device unplugged %d", retval);
  500. goto unlock_exit;
  501. }
  502. /* verify that we actually have some data to read */
  503. if (count == 0) {
  504. dbg(1, "%s: read request of 0 bytes", __func__);
  505. goto unlock_exit;
  506. }
  507. if (read_timeout) {
  508. timeout = jiffies + read_timeout * HZ / 1000;
  509. }
  510. /* wait for data */
  511. tower_check_for_read_packet (dev);
  512. while (dev->read_packet_length == 0) {
  513. if (file->f_flags & O_NONBLOCK) {
  514. retval = -EAGAIN;
  515. goto unlock_exit;
  516. }
  517. retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies);
  518. if (retval < 0) {
  519. goto unlock_exit;
  520. }
  521. /* reset read timeout during read or write activity */
  522. if (read_timeout
  523. && (dev->read_buffer_length || dev->interrupt_out_busy)) {
  524. timeout = jiffies + read_timeout * HZ / 1000;
  525. }
  526. /* check for read timeout */
  527. if (read_timeout && time_after (jiffies, timeout)) {
  528. retval = -ETIMEDOUT;
  529. goto unlock_exit;
  530. }
  531. tower_check_for_read_packet (dev);
  532. }
  533. /* copy the data from read_buffer into userspace */
  534. bytes_to_read = min(count, dev->read_packet_length);
  535. if (copy_to_user (buffer, dev->read_buffer, bytes_to_read)) {
  536. retval = -EFAULT;
  537. goto unlock_exit;
  538. }
  539. spin_lock_irq (&dev->read_buffer_lock);
  540. dev->read_buffer_length -= bytes_to_read;
  541. dev->read_packet_length -= bytes_to_read;
  542. for (i=0; i<dev->read_buffer_length; i++) {
  543. dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read];
  544. }
  545. spin_unlock_irq (&dev->read_buffer_lock);
  546. retval = bytes_to_read;
  547. unlock_exit:
  548. /* unlock the device */
  549. mutex_unlock(&dev->lock);
  550. exit:
  551. dbg(2, "%s: leave, return value %d", __func__, retval);
  552. return retval;
  553. }
  554. /**
  555. * tower_write
  556. */
  557. static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  558. {
  559. struct lego_usb_tower *dev;
  560. size_t bytes_to_write;
  561. int retval = 0;
  562. dbg(2, "%s: enter, count = %Zd", __func__, count);
  563. dev = (struct lego_usb_tower *)file->private_data;
  564. /* lock this object */
  565. if (mutex_lock_interruptible(&dev->lock)) {
  566. retval = -ERESTARTSYS;
  567. goto exit;
  568. }
  569. /* verify that the device wasn't unplugged */
  570. if (dev->udev == NULL) {
  571. retval = -ENODEV;
  572. err("No device or device unplugged %d", retval);
  573. goto unlock_exit;
  574. }
  575. /* verify that we actually have some data to write */
  576. if (count == 0) {
  577. dbg(1, "%s: write request of 0 bytes", __func__);
  578. goto unlock_exit;
  579. }
  580. /* wait until previous transfer is finished */
  581. while (dev->interrupt_out_busy) {
  582. if (file->f_flags & O_NONBLOCK) {
  583. retval = -EAGAIN;
  584. goto unlock_exit;
  585. }
  586. retval = wait_event_interruptible (dev->write_wait, !dev->interrupt_out_busy);
  587. if (retval) {
  588. goto unlock_exit;
  589. }
  590. }
  591. /* write the data into interrupt_out_buffer from userspace */
  592. bytes_to_write = min_t(int, count, write_buffer_size);
  593. dbg(4, "%s: count = %Zd, bytes_to_write = %Zd", __func__, count, bytes_to_write);
  594. if (copy_from_user (dev->interrupt_out_buffer, buffer, bytes_to_write)) {
  595. retval = -EFAULT;
  596. goto unlock_exit;
  597. }
  598. /* send off the urb */
  599. usb_fill_int_urb(dev->interrupt_out_urb,
  600. dev->udev,
  601. usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
  602. dev->interrupt_out_buffer,
  603. bytes_to_write,
  604. tower_interrupt_out_callback,
  605. dev,
  606. dev->interrupt_out_interval);
  607. dev->interrupt_out_busy = 1;
  608. wmb();
  609. retval = usb_submit_urb (dev->interrupt_out_urb, GFP_KERNEL);
  610. if (retval) {
  611. dev->interrupt_out_busy = 0;
  612. err("Couldn't submit interrupt_out_urb %d", retval);
  613. goto unlock_exit;
  614. }
  615. retval = bytes_to_write;
  616. unlock_exit:
  617. /* unlock the device */
  618. mutex_unlock(&dev->lock);
  619. exit:
  620. dbg(2, "%s: leave, return value %d", __func__, retval);
  621. return retval;
  622. }
  623. /**
  624. * tower_interrupt_in_callback
  625. */
  626. static void tower_interrupt_in_callback (struct urb *urb)
  627. {
  628. struct lego_usb_tower *dev = urb->context;
  629. int status = urb->status;
  630. int retval;
  631. dbg(4, "%s: enter, status %d", __func__, status);
  632. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  633. if (status) {
  634. if (status == -ENOENT ||
  635. status == -ECONNRESET ||
  636. status == -ESHUTDOWN) {
  637. goto exit;
  638. } else {
  639. dbg(1, "%s: nonzero status received: %d", __func__, status);
  640. goto resubmit; /* maybe we can recover */
  641. }
  642. }
  643. if (urb->actual_length > 0) {
  644. spin_lock (&dev->read_buffer_lock);
  645. if (dev->read_buffer_length + urb->actual_length < read_buffer_size) {
  646. memcpy (dev->read_buffer + dev->read_buffer_length,
  647. dev->interrupt_in_buffer,
  648. urb->actual_length);
  649. dev->read_buffer_length += urb->actual_length;
  650. dev->read_last_arrival = jiffies;
  651. dbg(3, "%s: received %d bytes", __func__, urb->actual_length);
  652. } else {
  653. printk(KERN_WARNING "%s: read_buffer overflow, %d bytes dropped", __func__, urb->actual_length);
  654. }
  655. spin_unlock (&dev->read_buffer_lock);
  656. }
  657. resubmit:
  658. /* resubmit if we're still running */
  659. if (dev->interrupt_in_running && dev->udev) {
  660. retval = usb_submit_urb (dev->interrupt_in_urb, GFP_ATOMIC);
  661. if (retval) {
  662. err("%s: usb_submit_urb failed (%d)", __func__, retval);
  663. }
  664. }
  665. exit:
  666. dev->interrupt_in_done = 1;
  667. wake_up_interruptible (&dev->read_wait);
  668. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  669. dbg(4, "%s: leave, status %d", __func__, status);
  670. }
  671. /**
  672. * tower_interrupt_out_callback
  673. */
  674. static void tower_interrupt_out_callback (struct urb *urb)
  675. {
  676. struct lego_usb_tower *dev = urb->context;
  677. int status = urb->status;
  678. dbg(4, "%s: enter, status %d", __func__, status);
  679. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  680. /* sync/async unlink faults aren't errors */
  681. if (status && !(status == -ENOENT ||
  682. status == -ECONNRESET ||
  683. status == -ESHUTDOWN)) {
  684. dbg(1, "%s - nonzero write bulk status received: %d",
  685. __func__, status);
  686. }
  687. dev->interrupt_out_busy = 0;
  688. wake_up_interruptible(&dev->write_wait);
  689. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  690. dbg(4, "%s: leave, status %d", __func__, status);
  691. }
  692. /**
  693. * tower_probe
  694. *
  695. * Called by the usb core when a new device is connected that it thinks
  696. * this driver might be interested in.
  697. */
  698. static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id)
  699. {
  700. struct usb_device *udev = interface_to_usbdev(interface);
  701. struct lego_usb_tower *dev = NULL;
  702. struct usb_host_interface *iface_desc;
  703. struct usb_endpoint_descriptor* endpoint;
  704. struct tower_get_version_reply get_version_reply;
  705. int i;
  706. int retval = -ENOMEM;
  707. int result;
  708. dbg(2, "%s: enter", __func__);
  709. if (udev == NULL) {
  710. info ("udev is NULL.");
  711. }
  712. /* allocate memory for our device state and initialize it */
  713. dev = kmalloc (sizeof(struct lego_usb_tower), GFP_KERNEL);
  714. if (dev == NULL) {
  715. err ("Out of memory");
  716. goto exit;
  717. }
  718. mutex_init(&dev->lock);
  719. dev->udev = udev;
  720. dev->open_count = 0;
  721. dev->read_buffer = NULL;
  722. dev->read_buffer_length = 0;
  723. dev->read_packet_length = 0;
  724. spin_lock_init (&dev->read_buffer_lock);
  725. dev->packet_timeout_jiffies = packet_timeout * HZ / 1000;
  726. dev->read_last_arrival = jiffies;
  727. init_waitqueue_head (&dev->read_wait);
  728. init_waitqueue_head (&dev->write_wait);
  729. dev->interrupt_in_buffer = NULL;
  730. dev->interrupt_in_endpoint = NULL;
  731. dev->interrupt_in_urb = NULL;
  732. dev->interrupt_in_running = 0;
  733. dev->interrupt_in_done = 0;
  734. dev->interrupt_out_buffer = NULL;
  735. dev->interrupt_out_endpoint = NULL;
  736. dev->interrupt_out_urb = NULL;
  737. dev->interrupt_out_busy = 0;
  738. iface_desc = interface->cur_altsetting;
  739. /* set up the endpoint information */
  740. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  741. endpoint = &iface_desc->endpoint[i].desc;
  742. if (usb_endpoint_xfer_int(endpoint)) {
  743. if (usb_endpoint_dir_in(endpoint))
  744. dev->interrupt_in_endpoint = endpoint;
  745. else
  746. dev->interrupt_out_endpoint = endpoint;
  747. }
  748. }
  749. if(dev->interrupt_in_endpoint == NULL) {
  750. err("interrupt in endpoint not found");
  751. goto error;
  752. }
  753. if (dev->interrupt_out_endpoint == NULL) {
  754. err("interrupt out endpoint not found");
  755. goto error;
  756. }
  757. dev->read_buffer = kmalloc (read_buffer_size, GFP_KERNEL);
  758. if (!dev->read_buffer) {
  759. err("Couldn't allocate read_buffer");
  760. goto error;
  761. }
  762. dev->interrupt_in_buffer = kmalloc (le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize), GFP_KERNEL);
  763. if (!dev->interrupt_in_buffer) {
  764. err("Couldn't allocate interrupt_in_buffer");
  765. goto error;
  766. }
  767. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  768. if (!dev->interrupt_in_urb) {
  769. err("Couldn't allocate interrupt_in_urb");
  770. goto error;
  771. }
  772. dev->interrupt_out_buffer = kmalloc (write_buffer_size, GFP_KERNEL);
  773. if (!dev->interrupt_out_buffer) {
  774. err("Couldn't allocate interrupt_out_buffer");
  775. goto error;
  776. }
  777. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  778. if (!dev->interrupt_out_urb) {
  779. err("Couldn't allocate interrupt_out_urb");
  780. goto error;
  781. }
  782. dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
  783. dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
  784. /* we can register the device now, as it is ready */
  785. usb_set_intfdata (interface, dev);
  786. retval = usb_register_dev (interface, &tower_class);
  787. if (retval) {
  788. /* something prevented us from registering this driver */
  789. err ("Not able to get a minor for this device.");
  790. usb_set_intfdata (interface, NULL);
  791. goto error;
  792. }
  793. dev->minor = interface->minor;
  794. /* let the user know what node this device is now attached to */
  795. info ("LEGO USB Tower #%d now attached to major %d minor %d", (dev->minor - LEGO_USB_TOWER_MINOR_BASE), USB_MAJOR, dev->minor);
  796. /* get the firmware version and log it */
  797. result = usb_control_msg (udev,
  798. usb_rcvctrlpipe(udev, 0),
  799. LEGO_USB_TOWER_REQUEST_GET_VERSION,
  800. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  801. 0,
  802. 0,
  803. &get_version_reply,
  804. sizeof(get_version_reply),
  805. 1000);
  806. if (result < 0) {
  807. err("LEGO USB Tower get version control request failed");
  808. retval = result;
  809. goto error;
  810. }
  811. info("LEGO USB Tower firmware version is %d.%d build %d",
  812. get_version_reply.major,
  813. get_version_reply.minor,
  814. le16_to_cpu(get_version_reply.build_no));
  815. exit:
  816. dbg(2, "%s: leave, return value 0x%.8lx (dev)", __func__, (long) dev);
  817. return retval;
  818. error:
  819. tower_delete(dev);
  820. return retval;
  821. }
  822. /**
  823. * tower_disconnect
  824. *
  825. * Called by the usb core when the device is removed from the system.
  826. */
  827. static void tower_disconnect (struct usb_interface *interface)
  828. {
  829. struct lego_usb_tower *dev;
  830. int minor;
  831. dbg(2, "%s: enter", __func__);
  832. dev = usb_get_intfdata (interface);
  833. mutex_lock(&open_disc_mutex);
  834. usb_set_intfdata (interface, NULL);
  835. minor = dev->minor;
  836. /* give back our minor */
  837. usb_deregister_dev (interface, &tower_class);
  838. mutex_lock(&dev->lock);
  839. mutex_unlock(&open_disc_mutex);
  840. /* if the device is not opened, then we clean up right now */
  841. if (!dev->open_count) {
  842. mutex_unlock(&dev->lock);
  843. tower_delete (dev);
  844. } else {
  845. dev->udev = NULL;
  846. mutex_unlock(&dev->lock);
  847. }
  848. info("LEGO USB Tower #%d now disconnected", (minor - LEGO_USB_TOWER_MINOR_BASE));
  849. dbg(2, "%s: leave", __func__);
  850. }
  851. /**
  852. * lego_usb_tower_init
  853. */
  854. static int __init lego_usb_tower_init(void)
  855. {
  856. int result;
  857. int retval = 0;
  858. dbg(2, "%s: enter", __func__);
  859. /* register this driver with the USB subsystem */
  860. result = usb_register(&tower_driver);
  861. if (result < 0) {
  862. err("usb_register failed for the "__FILE__" driver. Error number %d", result);
  863. retval = -1;
  864. goto exit;
  865. }
  866. info(DRIVER_DESC " " DRIVER_VERSION);
  867. exit:
  868. dbg(2, "%s: leave, return value %d", __func__, retval);
  869. return retval;
  870. }
  871. /**
  872. * lego_usb_tower_exit
  873. */
  874. static void __exit lego_usb_tower_exit(void)
  875. {
  876. dbg(2, "%s: enter", __func__);
  877. /* deregister this driver with the USB subsystem */
  878. usb_deregister (&tower_driver);
  879. dbg(2, "%s: leave", __func__);
  880. }
  881. module_init (lego_usb_tower_init);
  882. module_exit (lego_usb_tower_exit);
  883. MODULE_AUTHOR(DRIVER_AUTHOR);
  884. MODULE_DESCRIPTION(DRIVER_DESC);
  885. #ifdef MODULE_LICENSE
  886. MODULE_LICENSE("GPL");
  887. #endif