legousbtower.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * LEGO USB Tower driver
  4. *
  5. * Copyright (C) 2003 David Glance <[email protected]>
  6. * 2001-2004 Juergen Stuber <[email protected]>
  7. *
  8. * derived from USB Skeleton driver - 0.5
  9. * Copyright (C) 2001 Greg Kroah-Hartman ([email protected])
  10. *
  11. * History:
  12. *
  13. * 2001-10-13 - 0.1 js
  14. * - first version
  15. * 2001-11-03 - 0.2 js
  16. * - simplified buffering, one-shot URBs for writing
  17. * 2001-11-10 - 0.3 js
  18. * - removed IOCTL (setting power/mode is more complicated, postponed)
  19. * 2001-11-28 - 0.4 js
  20. * - added vendor commands for mode of operation and power level in open
  21. * 2001-12-04 - 0.5 js
  22. * - set IR mode by default (by oversight 0.4 set VLL mode)
  23. * 2002-01-11 - 0.5? pcchan
  24. * - make read buffer reusable and work around bytes_to_write issue between
  25. * uhci and legusbtower
  26. * 2002-09-23 - 0.52 david ([email protected])
  27. * - imported into lejos project
  28. * - changed wake_up to wake_up_interruptible
  29. * - changed to use lego0 rather than tower0
  30. * - changed dbg() to use __func__ rather than deprecated __func__
  31. * 2003-01-12 - 0.53 david ([email protected])
  32. * - changed read and write to write everything or
  33. * timeout (from a patch by Chris Riesen and Brett Thaeler driver)
  34. * - added ioctl functionality to set timeouts
  35. * 2003-07-18 - 0.54 davidgsf ([email protected])
  36. * - initial import into LegoUSB project
  37. * - merge of existing LegoUSB.c driver
  38. * 2003-07-18 - 0.56 davidgsf ([email protected])
  39. * - port to 2.6 style driver
  40. * 2004-02-29 - 0.6 Juergen Stuber <[email protected]>
  41. * - fix locking
  42. * - unlink read URBs which are no longer needed
  43. * - allow increased buffer size, eliminates need for timeout on write
  44. * - have read URB running continuously
  45. * - added poll
  46. * - forbid seeking
  47. * - added nonblocking I/O
  48. * - changed back __func__ to __func__
  49. * - read and log tower firmware version
  50. * - reset tower on probe, avoids failure of first write
  51. * 2004-03-09 - 0.7 Juergen Stuber <[email protected]>
  52. * - timeout read now only after inactivity, shorten default accordingly
  53. * 2004-03-11 - 0.8 Juergen Stuber <[email protected]>
  54. * - log major, minor instead of possibly confusing device filename
  55. * - whitespace cleanup
  56. * 2004-03-12 - 0.9 Juergen Stuber <[email protected]>
  57. * - normalize whitespace in debug messages
  58. * - take care about endianness in control message responses
  59. * 2004-03-13 - 0.91 Juergen Stuber <[email protected]>
  60. * - make default intervals longer to accommodate current EHCI driver
  61. * 2004-03-19 - 0.92 Juergen Stuber <[email protected]>
  62. * - replaced atomic_t by memory barriers
  63. * 2004-04-21 - 0.93 Juergen Stuber <[email protected]>
  64. * - wait for completion of write urb in release (needed for remotecontrol)
  65. * - corrected poll for write direction (missing negation)
  66. * 2004-04-22 - 0.94 Juergen Stuber <[email protected]>
  67. * - make device locking interruptible
  68. * 2004-04-30 - 0.95 Juergen Stuber <[email protected]>
  69. * - check for valid udev on resubmitting and unlinking urbs
  70. * 2004-08-03 - 0.96 Juergen Stuber <[email protected]>
  71. * - move reset into open to clean out spurious data
  72. */
  73. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  74. #include <linux/kernel.h>
  75. #include <linux/errno.h>
  76. #include <linux/slab.h>
  77. #include <linux/module.h>
  78. #include <linux/completion.h>
  79. #include <linux/mutex.h>
  80. #include <linux/uaccess.h>
  81. #include <linux/usb.h>
  82. #include <linux/poll.h>
  83. #define DRIVER_AUTHOR "Juergen Stuber <[email protected]>"
  84. #define DRIVER_DESC "LEGO USB Tower Driver"
  85. /* The defaults are chosen to work with the latest versions of leJOS and NQC.
  86. */
  87. /* Some legacy software likes to receive packets in one piece.
  88. * In this case read_buffer_size should exceed the maximal packet length
  89. * (417 for datalog uploads), and packet_timeout should be set.
  90. */
  91. static int read_buffer_size = 480;
  92. module_param(read_buffer_size, int, 0);
  93. MODULE_PARM_DESC(read_buffer_size, "Read buffer size");
  94. /* Some legacy software likes to send packets in one piece.
  95. * In this case write_buffer_size should exceed the maximal packet length
  96. * (417 for firmware and program downloads).
  97. * A problem with long writes is that the following read may time out
  98. * if the software is not prepared to wait long enough.
  99. */
  100. static int write_buffer_size = 480;
  101. module_param(write_buffer_size, int, 0);
  102. MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
  103. /* Some legacy software expects reads to contain whole LASM packets.
  104. * To achieve this, characters which arrive before a packet timeout
  105. * occurs will be returned in a single read operation.
  106. * A problem with long reads is that the software may time out
  107. * if it is not prepared to wait long enough.
  108. * The packet timeout should be greater than the time between the
  109. * reception of subsequent characters, which should arrive about
  110. * every 5ms for the standard 2400 baud.
  111. * Set it to 0 to disable.
  112. */
  113. static int packet_timeout = 50;
  114. module_param(packet_timeout, int, 0);
  115. MODULE_PARM_DESC(packet_timeout, "Packet timeout in ms");
  116. /* Some legacy software expects blocking reads to time out.
  117. * Timeout occurs after the specified time of read and write inactivity.
  118. * Set it to 0 to disable.
  119. */
  120. static int read_timeout = 200;
  121. module_param(read_timeout, int, 0);
  122. MODULE_PARM_DESC(read_timeout, "Read timeout in ms");
  123. /* As of kernel version 2.6.4 ehci-hcd uses an
  124. * "only one interrupt transfer per frame" shortcut
  125. * to simplify the scheduling of periodic transfers.
  126. * This conflicts with our standard 1ms intervals for in and out URBs.
  127. * We use default intervals of 2ms for in and 8ms for out transfers,
  128. * which is fast enough for 2400 baud and allows a small additional load.
  129. * Increase the interval to allow more devices that do interrupt transfers,
  130. * or set to 0 to use the standard interval from the endpoint descriptors.
  131. */
  132. static int interrupt_in_interval = 2;
  133. module_param(interrupt_in_interval, int, 0);
  134. MODULE_PARM_DESC(interrupt_in_interval, "Interrupt in interval in ms");
  135. static int interrupt_out_interval = 8;
  136. module_param(interrupt_out_interval, int, 0);
  137. MODULE_PARM_DESC(interrupt_out_interval, "Interrupt out interval in ms");
  138. /* Define these values to match your device */
  139. #define LEGO_USB_TOWER_VENDOR_ID 0x0694
  140. #define LEGO_USB_TOWER_PRODUCT_ID 0x0001
  141. /* Vendor requests */
  142. #define LEGO_USB_TOWER_REQUEST_RESET 0x04
  143. #define LEGO_USB_TOWER_REQUEST_GET_VERSION 0xFD
  144. struct tower_reset_reply {
  145. __le16 size;
  146. __u8 err_code;
  147. __u8 spare;
  148. };
  149. struct tower_get_version_reply {
  150. __le16 size;
  151. __u8 err_code;
  152. __u8 spare;
  153. __u8 major;
  154. __u8 minor;
  155. __le16 build_no;
  156. };
  157. /* table of devices that work with this driver */
  158. static const struct usb_device_id tower_table[] = {
  159. { USB_DEVICE(LEGO_USB_TOWER_VENDOR_ID, LEGO_USB_TOWER_PRODUCT_ID) },
  160. { } /* Terminating entry */
  161. };
  162. MODULE_DEVICE_TABLE(usb, tower_table);
  163. #define LEGO_USB_TOWER_MINOR_BASE 160
  164. /* Structure to hold all of our device specific stuff */
  165. struct lego_usb_tower {
  166. struct mutex lock; /* locks this structure */
  167. struct usb_device *udev; /* save off the usb device pointer */
  168. unsigned char minor; /* the starting minor number for this device */
  169. int open_count; /* number of times this port has been opened */
  170. unsigned long disconnected:1;
  171. char *read_buffer;
  172. size_t read_buffer_length; /* this much came in */
  173. size_t read_packet_length; /* this much will be returned on read */
  174. spinlock_t read_buffer_lock;
  175. int packet_timeout_jiffies;
  176. unsigned long read_last_arrival;
  177. wait_queue_head_t read_wait;
  178. wait_queue_head_t write_wait;
  179. char *interrupt_in_buffer;
  180. struct usb_endpoint_descriptor *interrupt_in_endpoint;
  181. struct urb *interrupt_in_urb;
  182. int interrupt_in_interval;
  183. int interrupt_in_done;
  184. char *interrupt_out_buffer;
  185. struct usb_endpoint_descriptor *interrupt_out_endpoint;
  186. struct urb *interrupt_out_urb;
  187. int interrupt_out_interval;
  188. int interrupt_out_busy;
  189. };
  190. /* local function prototypes */
  191. static ssize_t tower_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos);
  192. static ssize_t tower_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
  193. static inline void tower_delete(struct lego_usb_tower *dev);
  194. static int tower_open(struct inode *inode, struct file *file);
  195. static int tower_release(struct inode *inode, struct file *file);
  196. static __poll_t tower_poll(struct file *file, poll_table *wait);
  197. static loff_t tower_llseek(struct file *file, loff_t off, int whence);
  198. static void tower_check_for_read_packet(struct lego_usb_tower *dev);
  199. static void tower_interrupt_in_callback(struct urb *urb);
  200. static void tower_interrupt_out_callback(struct urb *urb);
  201. static int tower_probe(struct usb_interface *interface, const struct usb_device_id *id);
  202. static void tower_disconnect(struct usb_interface *interface);
  203. /* file operations needed when we register this driver */
  204. static const struct file_operations tower_fops = {
  205. .owner = THIS_MODULE,
  206. .read = tower_read,
  207. .write = tower_write,
  208. .open = tower_open,
  209. .release = tower_release,
  210. .poll = tower_poll,
  211. .llseek = tower_llseek,
  212. };
  213. static char *legousbtower_devnode(struct device *dev, umode_t *mode)
  214. {
  215. return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
  216. }
  217. /*
  218. * usb class driver info in order to get a minor number from the usb core,
  219. * and to have the device registered with the driver core
  220. */
  221. static struct usb_class_driver tower_class = {
  222. .name = "legousbtower%d",
  223. .devnode = legousbtower_devnode,
  224. .fops = &tower_fops,
  225. .minor_base = LEGO_USB_TOWER_MINOR_BASE,
  226. };
  227. /* usb specific object needed to register this driver with the usb subsystem */
  228. static struct usb_driver tower_driver = {
  229. .name = "legousbtower",
  230. .probe = tower_probe,
  231. .disconnect = tower_disconnect,
  232. .id_table = tower_table,
  233. };
  234. /*
  235. * lego_usb_tower_debug_data
  236. */
  237. static inline void lego_usb_tower_debug_data(struct device *dev,
  238. const char *function, int size,
  239. const unsigned char *data)
  240. {
  241. dev_dbg(dev, "%s - length = %d, data = %*ph\n",
  242. function, size, size, data);
  243. }
  244. /*
  245. * tower_delete
  246. */
  247. static inline void tower_delete(struct lego_usb_tower *dev)
  248. {
  249. /* free data structures */
  250. usb_free_urb(dev->interrupt_in_urb);
  251. usb_free_urb(dev->interrupt_out_urb);
  252. kfree(dev->read_buffer);
  253. kfree(dev->interrupt_in_buffer);
  254. kfree(dev->interrupt_out_buffer);
  255. usb_put_dev(dev->udev);
  256. kfree(dev);
  257. }
  258. /*
  259. * tower_open
  260. */
  261. static int tower_open(struct inode *inode, struct file *file)
  262. {
  263. struct lego_usb_tower *dev = NULL;
  264. int subminor;
  265. int retval = 0;
  266. struct usb_interface *interface;
  267. struct tower_reset_reply reset_reply;
  268. int result;
  269. nonseekable_open(inode, file);
  270. subminor = iminor(inode);
  271. interface = usb_find_interface(&tower_driver, subminor);
  272. if (!interface) {
  273. pr_err("error, can't find device for minor %d\n", subminor);
  274. retval = -ENODEV;
  275. goto exit;
  276. }
  277. dev = usb_get_intfdata(interface);
  278. if (!dev) {
  279. retval = -ENODEV;
  280. goto exit;
  281. }
  282. /* lock this device */
  283. if (mutex_lock_interruptible(&dev->lock)) {
  284. retval = -ERESTARTSYS;
  285. goto exit;
  286. }
  287. /* allow opening only once */
  288. if (dev->open_count) {
  289. retval = -EBUSY;
  290. goto unlock_exit;
  291. }
  292. /* reset the tower */
  293. result = usb_control_msg_recv(dev->udev, 0,
  294. LEGO_USB_TOWER_REQUEST_RESET,
  295. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  296. 0, 0,
  297. &reset_reply, sizeof(reset_reply), 1000,
  298. GFP_KERNEL);
  299. if (result < 0) {
  300. dev_err(&dev->udev->dev,
  301. "LEGO USB Tower reset control request failed\n");
  302. retval = result;
  303. goto unlock_exit;
  304. }
  305. /* initialize in direction */
  306. dev->read_buffer_length = 0;
  307. dev->read_packet_length = 0;
  308. usb_fill_int_urb(dev->interrupt_in_urb,
  309. dev->udev,
  310. usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress),
  311. dev->interrupt_in_buffer,
  312. usb_endpoint_maxp(dev->interrupt_in_endpoint),
  313. tower_interrupt_in_callback,
  314. dev,
  315. dev->interrupt_in_interval);
  316. dev->interrupt_in_done = 0;
  317. mb();
  318. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  319. if (retval) {
  320. dev_err(&dev->udev->dev,
  321. "Couldn't submit interrupt_in_urb %d\n", retval);
  322. goto unlock_exit;
  323. }
  324. /* save device in the file's private structure */
  325. file->private_data = dev;
  326. dev->open_count = 1;
  327. unlock_exit:
  328. mutex_unlock(&dev->lock);
  329. exit:
  330. return retval;
  331. }
  332. /*
  333. * tower_release
  334. */
  335. static int tower_release(struct inode *inode, struct file *file)
  336. {
  337. struct lego_usb_tower *dev;
  338. int retval = 0;
  339. dev = file->private_data;
  340. if (dev == NULL) {
  341. retval = -ENODEV;
  342. goto exit;
  343. }
  344. mutex_lock(&dev->lock);
  345. if (dev->disconnected) {
  346. /* the device was unplugged before the file was released */
  347. /* unlock here as tower_delete frees dev */
  348. mutex_unlock(&dev->lock);
  349. tower_delete(dev);
  350. goto exit;
  351. }
  352. /* wait until write transfer is finished */
  353. if (dev->interrupt_out_busy) {
  354. wait_event_interruptible_timeout(dev->write_wait, !dev->interrupt_out_busy,
  355. 2 * HZ);
  356. }
  357. /* shutdown transfers */
  358. usb_kill_urb(dev->interrupt_in_urb);
  359. usb_kill_urb(dev->interrupt_out_urb);
  360. dev->open_count = 0;
  361. mutex_unlock(&dev->lock);
  362. exit:
  363. return retval;
  364. }
  365. /*
  366. * tower_check_for_read_packet
  367. *
  368. * To get correct semantics for signals and non-blocking I/O
  369. * with packetizing we pretend not to see any data in the read buffer
  370. * until it has been there unchanged for at least
  371. * dev->packet_timeout_jiffies, or until the buffer is full.
  372. */
  373. static void tower_check_for_read_packet(struct lego_usb_tower *dev)
  374. {
  375. spin_lock_irq(&dev->read_buffer_lock);
  376. if (!packet_timeout
  377. || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies)
  378. || dev->read_buffer_length == read_buffer_size) {
  379. dev->read_packet_length = dev->read_buffer_length;
  380. }
  381. dev->interrupt_in_done = 0;
  382. spin_unlock_irq(&dev->read_buffer_lock);
  383. }
  384. /*
  385. * tower_poll
  386. */
  387. static __poll_t tower_poll(struct file *file, poll_table *wait)
  388. {
  389. struct lego_usb_tower *dev;
  390. __poll_t mask = 0;
  391. dev = file->private_data;
  392. if (dev->disconnected)
  393. return EPOLLERR | EPOLLHUP;
  394. poll_wait(file, &dev->read_wait, wait);
  395. poll_wait(file, &dev->write_wait, wait);
  396. tower_check_for_read_packet(dev);
  397. if (dev->read_packet_length > 0)
  398. mask |= EPOLLIN | EPOLLRDNORM;
  399. if (!dev->interrupt_out_busy)
  400. mask |= EPOLLOUT | EPOLLWRNORM;
  401. return mask;
  402. }
  403. /*
  404. * tower_llseek
  405. */
  406. static loff_t tower_llseek(struct file *file, loff_t off, int whence)
  407. {
  408. return -ESPIPE; /* unseekable */
  409. }
  410. /*
  411. * tower_read
  412. */
  413. static ssize_t tower_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  414. {
  415. struct lego_usb_tower *dev;
  416. size_t bytes_to_read;
  417. int i;
  418. int retval = 0;
  419. unsigned long timeout = 0;
  420. dev = file->private_data;
  421. /* lock this object */
  422. if (mutex_lock_interruptible(&dev->lock)) {
  423. retval = -ERESTARTSYS;
  424. goto exit;
  425. }
  426. /* verify that the device wasn't unplugged */
  427. if (dev->disconnected) {
  428. retval = -ENODEV;
  429. goto unlock_exit;
  430. }
  431. /* verify that we actually have some data to read */
  432. if (count == 0) {
  433. dev_dbg(&dev->udev->dev, "read request of 0 bytes\n");
  434. goto unlock_exit;
  435. }
  436. if (read_timeout)
  437. timeout = jiffies + msecs_to_jiffies(read_timeout);
  438. /* wait for data */
  439. tower_check_for_read_packet(dev);
  440. while (dev->read_packet_length == 0) {
  441. if (file->f_flags & O_NONBLOCK) {
  442. retval = -EAGAIN;
  443. goto unlock_exit;
  444. }
  445. retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies);
  446. if (retval < 0)
  447. goto unlock_exit;
  448. /* reset read timeout during read or write activity */
  449. if (read_timeout
  450. && (dev->read_buffer_length || dev->interrupt_out_busy)) {
  451. timeout = jiffies + msecs_to_jiffies(read_timeout);
  452. }
  453. /* check for read timeout */
  454. if (read_timeout && time_after(jiffies, timeout)) {
  455. retval = -ETIMEDOUT;
  456. goto unlock_exit;
  457. }
  458. tower_check_for_read_packet(dev);
  459. }
  460. /* copy the data from read_buffer into userspace */
  461. bytes_to_read = min(count, dev->read_packet_length);
  462. if (copy_to_user(buffer, dev->read_buffer, bytes_to_read)) {
  463. retval = -EFAULT;
  464. goto unlock_exit;
  465. }
  466. spin_lock_irq(&dev->read_buffer_lock);
  467. dev->read_buffer_length -= bytes_to_read;
  468. dev->read_packet_length -= bytes_to_read;
  469. for (i = 0; i < dev->read_buffer_length; i++)
  470. dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read];
  471. spin_unlock_irq(&dev->read_buffer_lock);
  472. retval = bytes_to_read;
  473. unlock_exit:
  474. /* unlock the device */
  475. mutex_unlock(&dev->lock);
  476. exit:
  477. return retval;
  478. }
  479. /*
  480. * tower_write
  481. */
  482. static ssize_t tower_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  483. {
  484. struct lego_usb_tower *dev;
  485. size_t bytes_to_write;
  486. int retval = 0;
  487. dev = file->private_data;
  488. /* lock this object */
  489. if (mutex_lock_interruptible(&dev->lock)) {
  490. retval = -ERESTARTSYS;
  491. goto exit;
  492. }
  493. /* verify that the device wasn't unplugged */
  494. if (dev->disconnected) {
  495. retval = -ENODEV;
  496. goto unlock_exit;
  497. }
  498. /* verify that we actually have some data to write */
  499. if (count == 0) {
  500. dev_dbg(&dev->udev->dev, "write request of 0 bytes\n");
  501. goto unlock_exit;
  502. }
  503. /* wait until previous transfer is finished */
  504. while (dev->interrupt_out_busy) {
  505. if (file->f_flags & O_NONBLOCK) {
  506. retval = -EAGAIN;
  507. goto unlock_exit;
  508. }
  509. retval = wait_event_interruptible(dev->write_wait,
  510. !dev->interrupt_out_busy);
  511. if (retval)
  512. goto unlock_exit;
  513. }
  514. /* write the data into interrupt_out_buffer from userspace */
  515. bytes_to_write = min_t(int, count, write_buffer_size);
  516. dev_dbg(&dev->udev->dev, "%s: count = %zd, bytes_to_write = %zd\n",
  517. __func__, count, bytes_to_write);
  518. if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
  519. retval = -EFAULT;
  520. goto unlock_exit;
  521. }
  522. /* send off the urb */
  523. usb_fill_int_urb(dev->interrupt_out_urb,
  524. dev->udev,
  525. usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
  526. dev->interrupt_out_buffer,
  527. bytes_to_write,
  528. tower_interrupt_out_callback,
  529. dev,
  530. dev->interrupt_out_interval);
  531. dev->interrupt_out_busy = 1;
  532. wmb();
  533. retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
  534. if (retval) {
  535. dev->interrupt_out_busy = 0;
  536. dev_err(&dev->udev->dev,
  537. "Couldn't submit interrupt_out_urb %d\n", retval);
  538. goto unlock_exit;
  539. }
  540. retval = bytes_to_write;
  541. unlock_exit:
  542. /* unlock the device */
  543. mutex_unlock(&dev->lock);
  544. exit:
  545. return retval;
  546. }
  547. /*
  548. * tower_interrupt_in_callback
  549. */
  550. static void tower_interrupt_in_callback(struct urb *urb)
  551. {
  552. struct lego_usb_tower *dev = urb->context;
  553. int status = urb->status;
  554. int retval;
  555. unsigned long flags;
  556. lego_usb_tower_debug_data(&dev->udev->dev, __func__,
  557. urb->actual_length, urb->transfer_buffer);
  558. if (status) {
  559. if (status == -ENOENT ||
  560. status == -ECONNRESET ||
  561. status == -ESHUTDOWN) {
  562. goto exit;
  563. } else {
  564. dev_dbg(&dev->udev->dev,
  565. "%s: nonzero status received: %d\n", __func__,
  566. status);
  567. goto resubmit; /* maybe we can recover */
  568. }
  569. }
  570. if (urb->actual_length > 0) {
  571. spin_lock_irqsave(&dev->read_buffer_lock, flags);
  572. if (dev->read_buffer_length + urb->actual_length < read_buffer_size) {
  573. memcpy(dev->read_buffer + dev->read_buffer_length,
  574. dev->interrupt_in_buffer,
  575. urb->actual_length);
  576. dev->read_buffer_length += urb->actual_length;
  577. dev->read_last_arrival = jiffies;
  578. dev_dbg(&dev->udev->dev, "%s: received %d bytes\n",
  579. __func__, urb->actual_length);
  580. } else {
  581. pr_warn("read_buffer overflow, %d bytes dropped\n",
  582. urb->actual_length);
  583. }
  584. spin_unlock_irqrestore(&dev->read_buffer_lock, flags);
  585. }
  586. resubmit:
  587. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
  588. if (retval) {
  589. dev_err(&dev->udev->dev, "%s: usb_submit_urb failed (%d)\n",
  590. __func__, retval);
  591. }
  592. exit:
  593. dev->interrupt_in_done = 1;
  594. wake_up_interruptible(&dev->read_wait);
  595. }
  596. /*
  597. * tower_interrupt_out_callback
  598. */
  599. static void tower_interrupt_out_callback(struct urb *urb)
  600. {
  601. struct lego_usb_tower *dev = urb->context;
  602. int status = urb->status;
  603. lego_usb_tower_debug_data(&dev->udev->dev, __func__,
  604. urb->actual_length, urb->transfer_buffer);
  605. /* sync/async unlink faults aren't errors */
  606. if (status && !(status == -ENOENT ||
  607. status == -ECONNRESET ||
  608. status == -ESHUTDOWN)) {
  609. dev_dbg(&dev->udev->dev,
  610. "%s: nonzero write bulk status received: %d\n", __func__,
  611. status);
  612. }
  613. dev->interrupt_out_busy = 0;
  614. wake_up_interruptible(&dev->write_wait);
  615. }
  616. /*
  617. * tower_probe
  618. *
  619. * Called by the usb core when a new device is connected that it thinks
  620. * this driver might be interested in.
  621. */
  622. static int tower_probe(struct usb_interface *interface, const struct usb_device_id *id)
  623. {
  624. struct device *idev = &interface->dev;
  625. struct usb_device *udev = interface_to_usbdev(interface);
  626. struct lego_usb_tower *dev;
  627. struct tower_get_version_reply get_version_reply;
  628. int retval = -ENOMEM;
  629. int result;
  630. /* allocate memory for our device state and initialize it */
  631. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  632. if (!dev)
  633. goto exit;
  634. mutex_init(&dev->lock);
  635. dev->udev = usb_get_dev(udev);
  636. spin_lock_init(&dev->read_buffer_lock);
  637. dev->packet_timeout_jiffies = msecs_to_jiffies(packet_timeout);
  638. dev->read_last_arrival = jiffies;
  639. init_waitqueue_head(&dev->read_wait);
  640. init_waitqueue_head(&dev->write_wait);
  641. result = usb_find_common_endpoints_reverse(interface->cur_altsetting,
  642. NULL, NULL,
  643. &dev->interrupt_in_endpoint,
  644. &dev->interrupt_out_endpoint);
  645. if (result) {
  646. dev_err(idev, "interrupt endpoints not found\n");
  647. retval = result;
  648. goto error;
  649. }
  650. dev->read_buffer = kmalloc(read_buffer_size, GFP_KERNEL);
  651. if (!dev->read_buffer)
  652. goto error;
  653. dev->interrupt_in_buffer = kmalloc(usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL);
  654. if (!dev->interrupt_in_buffer)
  655. goto error;
  656. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  657. if (!dev->interrupt_in_urb)
  658. goto error;
  659. dev->interrupt_out_buffer = kmalloc(write_buffer_size, GFP_KERNEL);
  660. if (!dev->interrupt_out_buffer)
  661. goto error;
  662. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  663. if (!dev->interrupt_out_urb)
  664. goto error;
  665. dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
  666. dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
  667. /* get the firmware version and log it */
  668. result = usb_control_msg_recv(udev, 0,
  669. LEGO_USB_TOWER_REQUEST_GET_VERSION,
  670. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  671. 0,
  672. 0,
  673. &get_version_reply,
  674. sizeof(get_version_reply),
  675. 1000, GFP_KERNEL);
  676. if (result) {
  677. dev_err(idev, "get version request failed: %d\n", result);
  678. retval = result;
  679. goto error;
  680. }
  681. dev_info(&interface->dev,
  682. "LEGO USB Tower firmware version is %d.%d build %d\n",
  683. get_version_reply.major,
  684. get_version_reply.minor,
  685. le16_to_cpu(get_version_reply.build_no));
  686. /* we can register the device now, as it is ready */
  687. usb_set_intfdata(interface, dev);
  688. retval = usb_register_dev(interface, &tower_class);
  689. if (retval) {
  690. /* something prevented us from registering this driver */
  691. dev_err(idev, "Not able to get a minor for this device.\n");
  692. goto error;
  693. }
  694. dev->minor = interface->minor;
  695. /* let the user know what node this device is now attached to */
  696. dev_info(&interface->dev, "LEGO USB Tower #%d now attached to major "
  697. "%d minor %d\n", (dev->minor - LEGO_USB_TOWER_MINOR_BASE),
  698. USB_MAJOR, dev->minor);
  699. exit:
  700. return retval;
  701. error:
  702. tower_delete(dev);
  703. return retval;
  704. }
  705. /*
  706. * tower_disconnect
  707. *
  708. * Called by the usb core when the device is removed from the system.
  709. */
  710. static void tower_disconnect(struct usb_interface *interface)
  711. {
  712. struct lego_usb_tower *dev;
  713. int minor;
  714. dev = usb_get_intfdata(interface);
  715. minor = dev->minor;
  716. /* give back our minor and prevent further open() */
  717. usb_deregister_dev(interface, &tower_class);
  718. /* stop I/O */
  719. usb_poison_urb(dev->interrupt_in_urb);
  720. usb_poison_urb(dev->interrupt_out_urb);
  721. mutex_lock(&dev->lock);
  722. /* if the device is not opened, then we clean up right now */
  723. if (!dev->open_count) {
  724. mutex_unlock(&dev->lock);
  725. tower_delete(dev);
  726. } else {
  727. dev->disconnected = 1;
  728. /* wake up pollers */
  729. wake_up_interruptible_all(&dev->read_wait);
  730. wake_up_interruptible_all(&dev->write_wait);
  731. mutex_unlock(&dev->lock);
  732. }
  733. dev_info(&interface->dev, "LEGO USB Tower #%d now disconnected\n",
  734. (minor - LEGO_USB_TOWER_MINOR_BASE));
  735. }
  736. module_usb_driver(tower_driver);
  737. MODULE_AUTHOR(DRIVER_AUTHOR);
  738. MODULE_DESCRIPTION(DRIVER_DESC);
  739. MODULE_LICENSE("GPL");