legousbtower.c 27 KB

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