legousbtower.c 26 KB

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