legousbtower.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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...) \
  95. do { \
  96. if (debug >= lvl) \
  97. printk(KERN_DEBUG "%s: " format "\n", __FILE__, ##arg); \
  98. } while (0)
  99. /* Version Information */
  100. #define DRIVER_VERSION "v0.96"
  101. #define DRIVER_AUTHOR "Juergen Stuber <starblue@sourceforge.net>"
  102. #define DRIVER_DESC "LEGO USB Tower Driver"
  103. /* Module parameters */
  104. module_param(debug, int, S_IRUGO | S_IWUSR);
  105. MODULE_PARM_DESC(debug, "Debug enabled or not");
  106. /* The defaults are chosen to work with the latest versions of leJOS and NQC.
  107. */
  108. /* Some legacy software likes to receive packets in one piece.
  109. * In this case read_buffer_size should exceed the maximal packet length
  110. * (417 for datalog uploads), and packet_timeout should be set.
  111. */
  112. static int read_buffer_size = 480;
  113. module_param(read_buffer_size, int, 0);
  114. MODULE_PARM_DESC(read_buffer_size, "Read buffer size");
  115. /* Some legacy software likes to send packets in one piece.
  116. * In this case write_buffer_size should exceed the maximal packet length
  117. * (417 for firmware and program downloads).
  118. * A problem with long writes is that the following read may time out
  119. * if the software is not prepared to wait long enough.
  120. */
  121. static int write_buffer_size = 480;
  122. module_param(write_buffer_size, int, 0);
  123. MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
  124. /* Some legacy software expects reads to contain whole LASM packets.
  125. * To achieve this, characters which arrive before a packet timeout
  126. * occurs will be returned in a single read operation.
  127. * A problem with long reads is that the software may time out
  128. * if it is not prepared to wait long enough.
  129. * The packet timeout should be greater than the time between the
  130. * reception of subsequent characters, which should arrive about
  131. * every 5ms for the standard 2400 baud.
  132. * Set it to 0 to disable.
  133. */
  134. static int packet_timeout = 50;
  135. module_param(packet_timeout, int, 0);
  136. MODULE_PARM_DESC(packet_timeout, "Packet timeout in ms");
  137. /* Some legacy software expects blocking reads to time out.
  138. * Timeout occurs after the specified time of read and write inactivity.
  139. * Set it to 0 to disable.
  140. */
  141. static int read_timeout = 200;
  142. module_param(read_timeout, int, 0);
  143. MODULE_PARM_DESC(read_timeout, "Read timeout in ms");
  144. /* As of kernel version 2.6.4 ehci-hcd uses an
  145. * "only one interrupt transfer per frame" shortcut
  146. * to simplify the scheduling of periodic transfers.
  147. * This conflicts with our standard 1ms intervals for in and out URBs.
  148. * We use default intervals of 2ms for in and 8ms for out transfers,
  149. * which is fast enough for 2400 baud and allows a small additional load.
  150. * Increase the interval to allow more devices that do interrupt transfers,
  151. * or set to 0 to use the standard interval from the endpoint descriptors.
  152. */
  153. static int interrupt_in_interval = 2;
  154. module_param(interrupt_in_interval, int, 0);
  155. MODULE_PARM_DESC(interrupt_in_interval, "Interrupt in interval in ms");
  156. static int interrupt_out_interval = 8;
  157. module_param(interrupt_out_interval, int, 0);
  158. MODULE_PARM_DESC(interrupt_out_interval, "Interrupt out interval in ms");
  159. /* Define these values to match your device */
  160. #define LEGO_USB_TOWER_VENDOR_ID 0x0694
  161. #define LEGO_USB_TOWER_PRODUCT_ID 0x0001
  162. /* Vendor requests */
  163. #define LEGO_USB_TOWER_REQUEST_RESET 0x04
  164. #define LEGO_USB_TOWER_REQUEST_GET_VERSION 0xFD
  165. struct tower_reset_reply {
  166. __le16 size; /* little-endian */
  167. __u8 err_code;
  168. __u8 spare;
  169. } __attribute__ ((packed));
  170. struct tower_get_version_reply {
  171. __le16 size; /* little-endian */
  172. __u8 err_code;
  173. __u8 spare;
  174. __u8 major;
  175. __u8 minor;
  176. __le16 build_no; /* little-endian */
  177. } __attribute__ ((packed));
  178. /* table of devices that work with this driver */
  179. static const struct usb_device_id tower_table[] = {
  180. { USB_DEVICE(LEGO_USB_TOWER_VENDOR_ID, LEGO_USB_TOWER_PRODUCT_ID) },
  181. { } /* Terminating entry */
  182. };
  183. MODULE_DEVICE_TABLE (usb, tower_table);
  184. static DEFINE_MUTEX(open_disc_mutex);
  185. #define LEGO_USB_TOWER_MINOR_BASE 160
  186. /* Structure to hold all of our device specific stuff */
  187. struct lego_usb_tower {
  188. struct mutex lock; /* locks this structure */
  189. struct usb_device* udev; /* save off the usb device pointer */
  190. unsigned char minor; /* the starting minor number for this device */
  191. int open_count; /* number of times this port has been opened */
  192. char* read_buffer;
  193. size_t read_buffer_length; /* this much came in */
  194. size_t read_packet_length; /* this much will be returned on read */
  195. spinlock_t read_buffer_lock;
  196. int packet_timeout_jiffies;
  197. unsigned long read_last_arrival;
  198. wait_queue_head_t read_wait;
  199. wait_queue_head_t write_wait;
  200. char* interrupt_in_buffer;
  201. struct usb_endpoint_descriptor* interrupt_in_endpoint;
  202. struct urb* interrupt_in_urb;
  203. int interrupt_in_interval;
  204. int interrupt_in_running;
  205. int interrupt_in_done;
  206. char* interrupt_out_buffer;
  207. struct usb_endpoint_descriptor* interrupt_out_endpoint;
  208. struct urb* interrupt_out_urb;
  209. int interrupt_out_interval;
  210. int interrupt_out_busy;
  211. };
  212. /* local function prototypes */
  213. static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos);
  214. static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
  215. static inline void tower_delete (struct lego_usb_tower *dev);
  216. static int tower_open (struct inode *inode, struct file *file);
  217. static int tower_release (struct inode *inode, struct file *file);
  218. static unsigned int tower_poll (struct file *file, poll_table *wait);
  219. static loff_t tower_llseek (struct file *file, loff_t off, int whence);
  220. static void tower_abort_transfers (struct lego_usb_tower *dev);
  221. static void tower_check_for_read_packet (struct lego_usb_tower *dev);
  222. static void tower_interrupt_in_callback (struct urb *urb);
  223. static void tower_interrupt_out_callback (struct urb *urb);
  224. static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id);
  225. static void tower_disconnect (struct usb_interface *interface);
  226. /* file operations needed when we register this driver */
  227. static const struct file_operations tower_fops = {
  228. .owner = THIS_MODULE,
  229. .read = tower_read,
  230. .write = tower_write,
  231. .open = tower_open,
  232. .release = tower_release,
  233. .poll = tower_poll,
  234. .llseek = tower_llseek,
  235. };
  236. static char *legousbtower_devnode(struct device *dev, umode_t *mode)
  237. {
  238. return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
  239. }
  240. /*
  241. * usb class driver info in order to get a minor number from the usb core,
  242. * and to have the device registered with the driver core
  243. */
  244. static struct usb_class_driver tower_class = {
  245. .name = "legousbtower%d",
  246. .devnode = legousbtower_devnode,
  247. .fops = &tower_fops,
  248. .minor_base = LEGO_USB_TOWER_MINOR_BASE,
  249. };
  250. /* usb specific object needed to register this driver with the usb subsystem */
  251. static struct usb_driver tower_driver = {
  252. .name = "legousbtower",
  253. .probe = tower_probe,
  254. .disconnect = tower_disconnect,
  255. .id_table = tower_table,
  256. };
  257. /**
  258. * lego_usb_tower_debug_data
  259. */
  260. static inline void lego_usb_tower_debug_data (int level, const char *function, int size, const unsigned char *data)
  261. {
  262. int i;
  263. if (debug < level)
  264. return;
  265. printk (KERN_DEBUG "%s: %s - length = %d, data = ", __FILE__, function, size);
  266. for (i = 0; i < size; ++i) {
  267. printk ("%.2x ", data[i]);
  268. }
  269. printk ("\n");
  270. }
  271. /**
  272. * tower_delete
  273. */
  274. static inline void tower_delete (struct lego_usb_tower *dev)
  275. {
  276. tower_abort_transfers (dev);
  277. /* free data structures */
  278. usb_free_urb(dev->interrupt_in_urb);
  279. usb_free_urb(dev->interrupt_out_urb);
  280. kfree (dev->read_buffer);
  281. kfree (dev->interrupt_in_buffer);
  282. kfree (dev->interrupt_out_buffer);
  283. kfree (dev);
  284. }
  285. /**
  286. * tower_open
  287. */
  288. static int tower_open (struct inode *inode, struct file *file)
  289. {
  290. struct lego_usb_tower *dev = NULL;
  291. int subminor;
  292. int retval = 0;
  293. struct usb_interface *interface;
  294. struct tower_reset_reply reset_reply;
  295. int result;
  296. nonseekable_open(inode, file);
  297. subminor = iminor(inode);
  298. interface = usb_find_interface (&tower_driver, subminor);
  299. if (!interface) {
  300. printk(KERN_ERR "%s - error, can't find device for minor %d\n",
  301. __func__, subminor);
  302. retval = -ENODEV;
  303. goto exit;
  304. }
  305. mutex_lock(&open_disc_mutex);
  306. dev = usb_get_intfdata(interface);
  307. if (!dev) {
  308. mutex_unlock(&open_disc_mutex);
  309. retval = -ENODEV;
  310. goto exit;
  311. }
  312. /* lock this device */
  313. if (mutex_lock_interruptible(&dev->lock)) {
  314. mutex_unlock(&open_disc_mutex);
  315. retval = -ERESTARTSYS;
  316. goto exit;
  317. }
  318. /* allow opening only once */
  319. if (dev->open_count) {
  320. mutex_unlock(&open_disc_mutex);
  321. retval = -EBUSY;
  322. goto unlock_exit;
  323. }
  324. dev->open_count = 1;
  325. mutex_unlock(&open_disc_mutex);
  326. /* reset the tower */
  327. result = usb_control_msg (dev->udev,
  328. usb_rcvctrlpipe(dev->udev, 0),
  329. LEGO_USB_TOWER_REQUEST_RESET,
  330. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  331. 0,
  332. 0,
  333. &reset_reply,
  334. sizeof(reset_reply),
  335. 1000);
  336. if (result < 0) {
  337. dev_err(&dev->udev->dev,
  338. "LEGO USB Tower reset control request failed\n");
  339. retval = result;
  340. goto unlock_exit;
  341. }
  342. /* initialize in direction */
  343. dev->read_buffer_length = 0;
  344. dev->read_packet_length = 0;
  345. usb_fill_int_urb (dev->interrupt_in_urb,
  346. dev->udev,
  347. usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress),
  348. dev->interrupt_in_buffer,
  349. usb_endpoint_maxp(dev->interrupt_in_endpoint),
  350. tower_interrupt_in_callback,
  351. dev,
  352. dev->interrupt_in_interval);
  353. dev->interrupt_in_running = 1;
  354. dev->interrupt_in_done = 0;
  355. mb();
  356. retval = usb_submit_urb (dev->interrupt_in_urb, GFP_KERNEL);
  357. if (retval) {
  358. dev_err(&dev->udev->dev,
  359. "Couldn't submit interrupt_in_urb %d\n", retval);
  360. dev->interrupt_in_running = 0;
  361. dev->open_count = 0;
  362. goto unlock_exit;
  363. }
  364. /* save device in the file's private structure */
  365. file->private_data = dev;
  366. unlock_exit:
  367. mutex_unlock(&dev->lock);
  368. exit:
  369. return retval;
  370. }
  371. /**
  372. * tower_release
  373. */
  374. static int tower_release (struct inode *inode, struct file *file)
  375. {
  376. struct lego_usb_tower *dev;
  377. int retval = 0;
  378. dev = file->private_data;
  379. if (dev == NULL) {
  380. dbg(1, "%s: object is NULL", __func__);
  381. retval = -ENODEV;
  382. goto exit_nolock;
  383. }
  384. mutex_lock(&open_disc_mutex);
  385. if (mutex_lock_interruptible(&dev->lock)) {
  386. retval = -ERESTARTSYS;
  387. goto exit;
  388. }
  389. if (dev->open_count != 1) {
  390. dbg(1, "%s: device not opened exactly once", __func__);
  391. retval = -ENODEV;
  392. goto unlock_exit;
  393. }
  394. if (dev->udev == NULL) {
  395. /* the device was unplugged before the file was released */
  396. /* unlock here as tower_delete frees dev */
  397. mutex_unlock(&dev->lock);
  398. tower_delete (dev);
  399. goto exit;
  400. }
  401. /* wait until write transfer is finished */
  402. if (dev->interrupt_out_busy) {
  403. wait_event_interruptible_timeout (dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
  404. }
  405. tower_abort_transfers (dev);
  406. dev->open_count = 0;
  407. unlock_exit:
  408. mutex_unlock(&dev->lock);
  409. exit:
  410. mutex_unlock(&open_disc_mutex);
  411. exit_nolock:
  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. if (dev == NULL) {
  421. dbg(1, "%s: dev is null", __func__);
  422. return;
  423. }
  424. /* shutdown transfer */
  425. if (dev->interrupt_in_running) {
  426. dev->interrupt_in_running = 0;
  427. mb();
  428. if (dev->udev)
  429. usb_kill_urb (dev->interrupt_in_urb);
  430. }
  431. if (dev->interrupt_out_busy && dev->udev)
  432. usb_kill_urb(dev->interrupt_out_urb);
  433. }
  434. /**
  435. * tower_check_for_read_packet
  436. *
  437. * To get correct semantics for signals and non-blocking I/O
  438. * with packetizing we pretend not to see any data in the read buffer
  439. * until it has been there unchanged for at least
  440. * dev->packet_timeout_jiffies, or until the buffer is full.
  441. */
  442. static void tower_check_for_read_packet (struct lego_usb_tower *dev)
  443. {
  444. spin_lock_irq (&dev->read_buffer_lock);
  445. if (!packet_timeout
  446. || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies)
  447. || dev->read_buffer_length == read_buffer_size) {
  448. dev->read_packet_length = dev->read_buffer_length;
  449. }
  450. dev->interrupt_in_done = 0;
  451. spin_unlock_irq (&dev->read_buffer_lock);
  452. }
  453. /**
  454. * tower_poll
  455. */
  456. static unsigned int tower_poll (struct file *file, poll_table *wait)
  457. {
  458. struct lego_usb_tower *dev;
  459. unsigned int mask = 0;
  460. dev = file->private_data;
  461. if (!dev->udev)
  462. return POLLERR | POLLHUP;
  463. poll_wait(file, &dev->read_wait, wait);
  464. poll_wait(file, &dev->write_wait, wait);
  465. tower_check_for_read_packet(dev);
  466. if (dev->read_packet_length > 0) {
  467. mask |= POLLIN | POLLRDNORM;
  468. }
  469. if (!dev->interrupt_out_busy) {
  470. mask |= POLLOUT | POLLWRNORM;
  471. }
  472. return mask;
  473. }
  474. /**
  475. * tower_llseek
  476. */
  477. static loff_t tower_llseek (struct file *file, loff_t off, int whence)
  478. {
  479. return -ESPIPE; /* unseekable */
  480. }
  481. /**
  482. * tower_read
  483. */
  484. static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  485. {
  486. struct lego_usb_tower *dev;
  487. size_t bytes_to_read;
  488. int i;
  489. int retval = 0;
  490. unsigned long timeout = 0;
  491. dev = file->private_data;
  492. /* lock this object */
  493. if (mutex_lock_interruptible(&dev->lock)) {
  494. retval = -ERESTARTSYS;
  495. goto exit;
  496. }
  497. /* verify that the device wasn't unplugged */
  498. if (dev->udev == NULL) {
  499. retval = -ENODEV;
  500. printk(KERN_ERR "legousbtower: No device or device unplugged %d\n", retval);
  501. goto unlock_exit;
  502. }
  503. /* verify that we actually have some data to read */
  504. if (count == 0) {
  505. dbg(1, "%s: read request of 0 bytes", __func__);
  506. goto unlock_exit;
  507. }
  508. if (read_timeout) {
  509. timeout = jiffies + read_timeout * HZ / 1000;
  510. }
  511. /* wait for data */
  512. tower_check_for_read_packet (dev);
  513. while (dev->read_packet_length == 0) {
  514. if (file->f_flags & O_NONBLOCK) {
  515. retval = -EAGAIN;
  516. goto unlock_exit;
  517. }
  518. retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies);
  519. if (retval < 0) {
  520. goto unlock_exit;
  521. }
  522. /* reset read timeout during read or write activity */
  523. if (read_timeout
  524. && (dev->read_buffer_length || dev->interrupt_out_busy)) {
  525. timeout = jiffies + read_timeout * HZ / 1000;
  526. }
  527. /* check for read timeout */
  528. if (read_timeout && time_after (jiffies, timeout)) {
  529. retval = -ETIMEDOUT;
  530. goto unlock_exit;
  531. }
  532. tower_check_for_read_packet (dev);
  533. }
  534. /* copy the data from read_buffer into userspace */
  535. bytes_to_read = min(count, dev->read_packet_length);
  536. if (copy_to_user (buffer, dev->read_buffer, bytes_to_read)) {
  537. retval = -EFAULT;
  538. goto unlock_exit;
  539. }
  540. spin_lock_irq (&dev->read_buffer_lock);
  541. dev->read_buffer_length -= bytes_to_read;
  542. dev->read_packet_length -= bytes_to_read;
  543. for (i=0; i<dev->read_buffer_length; i++) {
  544. dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read];
  545. }
  546. spin_unlock_irq (&dev->read_buffer_lock);
  547. retval = bytes_to_read;
  548. unlock_exit:
  549. /* unlock the device */
  550. mutex_unlock(&dev->lock);
  551. exit:
  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. dev = file->private_data;
  563. /* lock this object */
  564. if (mutex_lock_interruptible(&dev->lock)) {
  565. retval = -ERESTARTSYS;
  566. goto exit;
  567. }
  568. /* verify that the device wasn't unplugged */
  569. if (dev->udev == NULL) {
  570. retval = -ENODEV;
  571. printk(KERN_ERR "legousbtower: No device or device unplugged %d\n", retval);
  572. goto unlock_exit;
  573. }
  574. /* verify that we actually have some data to write */
  575. if (count == 0) {
  576. dbg(1, "%s: write request of 0 bytes", __func__);
  577. goto unlock_exit;
  578. }
  579. /* wait until previous transfer is finished */
  580. while (dev->interrupt_out_busy) {
  581. if (file->f_flags & O_NONBLOCK) {
  582. retval = -EAGAIN;
  583. goto unlock_exit;
  584. }
  585. retval = wait_event_interruptible (dev->write_wait, !dev->interrupt_out_busy);
  586. if (retval) {
  587. goto unlock_exit;
  588. }
  589. }
  590. /* write the data into interrupt_out_buffer from userspace */
  591. bytes_to_write = min_t(int, count, write_buffer_size);
  592. dbg(4, "%s: count = %Zd, bytes_to_write = %Zd", __func__, count, bytes_to_write);
  593. if (copy_from_user (dev->interrupt_out_buffer, buffer, bytes_to_write)) {
  594. retval = -EFAULT;
  595. goto unlock_exit;
  596. }
  597. /* send off the urb */
  598. usb_fill_int_urb(dev->interrupt_out_urb,
  599. dev->udev,
  600. usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
  601. dev->interrupt_out_buffer,
  602. bytes_to_write,
  603. tower_interrupt_out_callback,
  604. dev,
  605. dev->interrupt_out_interval);
  606. dev->interrupt_out_busy = 1;
  607. wmb();
  608. retval = usb_submit_urb (dev->interrupt_out_urb, GFP_KERNEL);
  609. if (retval) {
  610. dev->interrupt_out_busy = 0;
  611. dev_err(&dev->udev->dev,
  612. "Couldn't submit interrupt_out_urb %d\n", 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. return retval;
  621. }
  622. /**
  623. * tower_interrupt_in_callback
  624. */
  625. static void tower_interrupt_in_callback (struct urb *urb)
  626. {
  627. struct lego_usb_tower *dev = urb->context;
  628. int status = urb->status;
  629. int retval;
  630. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  631. if (status) {
  632. if (status == -ENOENT ||
  633. status == -ECONNRESET ||
  634. status == -ESHUTDOWN) {
  635. goto exit;
  636. } else {
  637. dbg(1, "%s: nonzero status received: %d", __func__, status);
  638. goto resubmit; /* maybe we can recover */
  639. }
  640. }
  641. if (urb->actual_length > 0) {
  642. spin_lock (&dev->read_buffer_lock);
  643. if (dev->read_buffer_length + urb->actual_length < read_buffer_size) {
  644. memcpy (dev->read_buffer + dev->read_buffer_length,
  645. dev->interrupt_in_buffer,
  646. urb->actual_length);
  647. dev->read_buffer_length += urb->actual_length;
  648. dev->read_last_arrival = jiffies;
  649. dbg(3, "%s: received %d bytes", __func__, urb->actual_length);
  650. } else {
  651. printk(KERN_WARNING "%s: read_buffer overflow, %d bytes dropped", __func__, urb->actual_length);
  652. }
  653. spin_unlock (&dev->read_buffer_lock);
  654. }
  655. resubmit:
  656. /* resubmit if we're still running */
  657. if (dev->interrupt_in_running && dev->udev) {
  658. retval = usb_submit_urb (dev->interrupt_in_urb, GFP_ATOMIC);
  659. if (retval)
  660. dev_err(&dev->udev->dev,
  661. "%s: usb_submit_urb failed (%d)\n",
  662. __func__, retval);
  663. }
  664. exit:
  665. dev->interrupt_in_done = 1;
  666. wake_up_interruptible (&dev->read_wait);
  667. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  668. }
  669. /**
  670. * tower_interrupt_out_callback
  671. */
  672. static void tower_interrupt_out_callback (struct urb *urb)
  673. {
  674. struct lego_usb_tower *dev = urb->context;
  675. int status = urb->status;
  676. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  677. /* sync/async unlink faults aren't errors */
  678. if (status && !(status == -ENOENT ||
  679. status == -ECONNRESET ||
  680. status == -ESHUTDOWN)) {
  681. dbg(1, "%s - nonzero write bulk status received: %d",
  682. __func__, status);
  683. }
  684. dev->interrupt_out_busy = 0;
  685. wake_up_interruptible(&dev->write_wait);
  686. lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
  687. }
  688. /**
  689. * tower_probe
  690. *
  691. * Called by the usb core when a new device is connected that it thinks
  692. * this driver might be interested in.
  693. */
  694. static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id)
  695. {
  696. struct device *idev = &interface->dev;
  697. struct usb_device *udev = interface_to_usbdev(interface);
  698. struct lego_usb_tower *dev = NULL;
  699. struct usb_host_interface *iface_desc;
  700. struct usb_endpoint_descriptor* endpoint;
  701. struct tower_get_version_reply get_version_reply;
  702. int i;
  703. int retval = -ENOMEM;
  704. int result;
  705. /* allocate memory for our device state and initialize it */
  706. dev = kmalloc (sizeof(struct lego_usb_tower), GFP_KERNEL);
  707. if (dev == NULL) {
  708. dev_err(idev, "Out of memory\n");
  709. goto exit;
  710. }
  711. mutex_init(&dev->lock);
  712. dev->udev = udev;
  713. dev->open_count = 0;
  714. dev->read_buffer = NULL;
  715. dev->read_buffer_length = 0;
  716. dev->read_packet_length = 0;
  717. spin_lock_init (&dev->read_buffer_lock);
  718. dev->packet_timeout_jiffies = packet_timeout * HZ / 1000;
  719. dev->read_last_arrival = jiffies;
  720. init_waitqueue_head (&dev->read_wait);
  721. init_waitqueue_head (&dev->write_wait);
  722. dev->interrupt_in_buffer = NULL;
  723. dev->interrupt_in_endpoint = NULL;
  724. dev->interrupt_in_urb = NULL;
  725. dev->interrupt_in_running = 0;
  726. dev->interrupt_in_done = 0;
  727. dev->interrupt_out_buffer = NULL;
  728. dev->interrupt_out_endpoint = NULL;
  729. dev->interrupt_out_urb = NULL;
  730. dev->interrupt_out_busy = 0;
  731. iface_desc = interface->cur_altsetting;
  732. /* set up the endpoint information */
  733. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  734. endpoint = &iface_desc->endpoint[i].desc;
  735. if (usb_endpoint_xfer_int(endpoint)) {
  736. if (usb_endpoint_dir_in(endpoint))
  737. dev->interrupt_in_endpoint = endpoint;
  738. else
  739. dev->interrupt_out_endpoint = endpoint;
  740. }
  741. }
  742. if(dev->interrupt_in_endpoint == NULL) {
  743. dev_err(idev, "interrupt in endpoint not found\n");
  744. goto error;
  745. }
  746. if (dev->interrupt_out_endpoint == NULL) {
  747. dev_err(idev, "interrupt out endpoint not found\n");
  748. goto error;
  749. }
  750. dev->read_buffer = kmalloc (read_buffer_size, GFP_KERNEL);
  751. if (!dev->read_buffer) {
  752. dev_err(idev, "Couldn't allocate read_buffer\n");
  753. goto error;
  754. }
  755. dev->interrupt_in_buffer = kmalloc (usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL);
  756. if (!dev->interrupt_in_buffer) {
  757. dev_err(idev, "Couldn't allocate interrupt_in_buffer\n");
  758. goto error;
  759. }
  760. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  761. if (!dev->interrupt_in_urb) {
  762. dev_err(idev, "Couldn't allocate interrupt_in_urb\n");
  763. goto error;
  764. }
  765. dev->interrupt_out_buffer = kmalloc (write_buffer_size, GFP_KERNEL);
  766. if (!dev->interrupt_out_buffer) {
  767. dev_err(idev, "Couldn't allocate interrupt_out_buffer\n");
  768. goto error;
  769. }
  770. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  771. if (!dev->interrupt_out_urb) {
  772. dev_err(idev, "Couldn't allocate interrupt_out_urb\n");
  773. goto error;
  774. }
  775. dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
  776. dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
  777. /* we can register the device now, as it is ready */
  778. usb_set_intfdata (interface, dev);
  779. retval = usb_register_dev (interface, &tower_class);
  780. if (retval) {
  781. /* something prevented us from registering this driver */
  782. dev_err(idev, "Not able to get a minor for this device.\n");
  783. usb_set_intfdata (interface, NULL);
  784. goto error;
  785. }
  786. dev->minor = interface->minor;
  787. /* let the user know what node this device is now attached to */
  788. dev_info(&interface->dev, "LEGO USB Tower #%d now attached to major "
  789. "%d minor %d\n", (dev->minor - LEGO_USB_TOWER_MINOR_BASE),
  790. USB_MAJOR, dev->minor);
  791. /* get the firmware version and log it */
  792. result = usb_control_msg (udev,
  793. usb_rcvctrlpipe(udev, 0),
  794. LEGO_USB_TOWER_REQUEST_GET_VERSION,
  795. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  796. 0,
  797. 0,
  798. &get_version_reply,
  799. sizeof(get_version_reply),
  800. 1000);
  801. if (result < 0) {
  802. dev_err(idev, "LEGO USB Tower get version control request failed\n");
  803. retval = result;
  804. goto error;
  805. }
  806. dev_info(&interface->dev, "LEGO USB Tower firmware version is %d.%d "
  807. "build %d\n", get_version_reply.major,
  808. get_version_reply.minor,
  809. le16_to_cpu(get_version_reply.build_no));
  810. exit:
  811. return retval;
  812. error:
  813. tower_delete(dev);
  814. return retval;
  815. }
  816. /**
  817. * tower_disconnect
  818. *
  819. * Called by the usb core when the device is removed from the system.
  820. */
  821. static void tower_disconnect (struct usb_interface *interface)
  822. {
  823. struct lego_usb_tower *dev;
  824. int minor;
  825. dev = usb_get_intfdata (interface);
  826. mutex_lock(&open_disc_mutex);
  827. usb_set_intfdata (interface, NULL);
  828. minor = dev->minor;
  829. /* give back our minor */
  830. usb_deregister_dev (interface, &tower_class);
  831. mutex_lock(&dev->lock);
  832. mutex_unlock(&open_disc_mutex);
  833. /* if the device is not opened, then we clean up right now */
  834. if (!dev->open_count) {
  835. mutex_unlock(&dev->lock);
  836. tower_delete (dev);
  837. } else {
  838. dev->udev = NULL;
  839. /* wake up pollers */
  840. wake_up_interruptible_all(&dev->read_wait);
  841. wake_up_interruptible_all(&dev->write_wait);
  842. mutex_unlock(&dev->lock);
  843. }
  844. dev_info(&interface->dev, "LEGO USB Tower #%d now disconnected\n",
  845. (minor - LEGO_USB_TOWER_MINOR_BASE));
  846. }
  847. module_usb_driver(tower_driver);
  848. MODULE_AUTHOR(DRIVER_AUTHOR);
  849. MODULE_DESCRIPTION(DRIVER_DESC);
  850. #ifdef MODULE_LICENSE
  851. MODULE_LICENSE("GPL");
  852. #endif