legousbtower.c 28 KB

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