legousbtower.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * LEGO USB Tower driver
  4. *
  5. * Copyright (C) 2003 David Glance <davidgsf@sourceforge.net>
  6. * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
  7. *
  8. * derived from USB Skeleton driver - 0.5
  9. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  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 (david@csse.uwa.edu.au)
  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 (david@csse.uwa.edu.au)
  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 (david@csse.uwa.edu.au)
  36. * - initial import into LegoUSB project
  37. * - merge of existing LegoUSB.c driver
  38. * 2003-07-18 - 0.56 davidgsf (david@csse.uwa.edu.au)
  39. * - port to 2.6 style driver
  40. * 2004-02-29 - 0.6 Juergen Stuber <starblue@users.sourceforge.net>
  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 <starblue@users.sourceforge.net>
  52. * - timeout read now only after inactivity, shorten default accordingly
  53. * 2004-03-11 - 0.8 Juergen Stuber <starblue@users.sourceforge.net>
  54. * - log major, minor instead of possibly confusing device filename
  55. * - whitespace cleanup
  56. * 2004-03-12 - 0.9 Juergen Stuber <starblue@users.sourceforge.net>
  57. * - normalize whitespace in debug messages
  58. * - take care about endianness in control message responses
  59. * 2004-03-13 - 0.91 Juergen Stuber <starblue@users.sourceforge.net>
  60. * - make default intervals longer to accommodate current EHCI driver
  61. * 2004-03-19 - 0.92 Juergen Stuber <starblue@users.sourceforge.net>
  62. * - replaced atomic_t by memory barriers
  63. * 2004-04-21 - 0.93 Juergen Stuber <starblue@users.sourceforge.net>
  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 <starblue@users.sourceforge.net>
  67. * - make device locking interruptible
  68. * 2004-04-30 - 0.95 Juergen Stuber <starblue@users.sourceforge.net>
  69. * - check for valid udev on resubmitting and unlinking urbs
  70. * 2004-08-03 - 0.96 Juergen Stuber <starblue@users.sourceforge.net>
  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 <starblue@sourceforge.net>"
  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; /* little-endian */
  146. __u8 err_code;
  147. __u8 spare;
  148. } __attribute__ ((packed));
  149. struct tower_get_version_reply {
  150. __le16 size; /* little-endian */
  151. __u8 err_code;
  152. __u8 spare;
  153. __u8 major;
  154. __u8 minor;
  155. __le16 build_no; /* little-endian */
  156. } __attribute__ ((packed));
  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. static DEFINE_MUTEX(open_disc_mutex);
  164. #define LEGO_USB_TOWER_MINOR_BASE 160
  165. /* Structure to hold all of our device specific stuff */
  166. struct lego_usb_tower {
  167. struct mutex lock; /* locks this structure */
  168. struct usb_device* udev; /* save off the usb device pointer */
  169. unsigned char minor; /* the starting minor number for this device */
  170. int open_count; /* number of times this port has been opened */
  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_running;
  184. int interrupt_in_done;
  185. char* interrupt_out_buffer;
  186. struct usb_endpoint_descriptor* interrupt_out_endpoint;
  187. struct urb* interrupt_out_urb;
  188. int interrupt_out_interval;
  189. int interrupt_out_busy;
  190. };
  191. /* local function prototypes */
  192. static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos);
  193. static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
  194. static inline void tower_delete (struct lego_usb_tower *dev);
  195. static int tower_open (struct inode *inode, struct file *file);
  196. static int tower_release (struct inode *inode, struct file *file);
  197. static __poll_t tower_poll (struct file *file, poll_table *wait);
  198. static loff_t tower_llseek (struct file *file, loff_t off, int whence);
  199. static void tower_abort_transfers (struct lego_usb_tower *dev);
  200. static void tower_check_for_read_packet (struct lego_usb_tower *dev);
  201. static void tower_interrupt_in_callback (struct urb *urb);
  202. static void tower_interrupt_out_callback (struct urb *urb);
  203. static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id);
  204. static void tower_disconnect (struct usb_interface *interface);
  205. /* file operations needed when we register this driver */
  206. static const struct file_operations tower_fops = {
  207. .owner = THIS_MODULE,
  208. .read = tower_read,
  209. .write = tower_write,
  210. .open = tower_open,
  211. .release = tower_release,
  212. .poll = tower_poll,
  213. .llseek = tower_llseek,
  214. };
  215. static char *legousbtower_devnode(struct device *dev, umode_t *mode)
  216. {
  217. return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
  218. }
  219. /*
  220. * usb class driver info in order to get a minor number from the usb core,
  221. * and to have the device registered with the driver core
  222. */
  223. static struct usb_class_driver tower_class = {
  224. .name = "legousbtower%d",
  225. .devnode = legousbtower_devnode,
  226. .fops = &tower_fops,
  227. .minor_base = LEGO_USB_TOWER_MINOR_BASE,
  228. };
  229. /* usb specific object needed to register this driver with the usb subsystem */
  230. static struct usb_driver tower_driver = {
  231. .name = "legousbtower",
  232. .probe = tower_probe,
  233. .disconnect = tower_disconnect,
  234. .id_table = tower_table,
  235. };
  236. /**
  237. * lego_usb_tower_debug_data
  238. */
  239. static inline void lego_usb_tower_debug_data(struct device *dev,
  240. const char *function, int size,
  241. const unsigned char *data)
  242. {
  243. dev_dbg(dev, "%s - length = %d, data = %*ph\n",
  244. function, size, size, data);
  245. }
  246. /**
  247. * tower_delete
  248. */
  249. static inline void tower_delete (struct lego_usb_tower *dev)
  250. {
  251. tower_abort_transfers (dev);
  252. /* free data structures */
  253. usb_free_urb(dev->interrupt_in_urb);
  254. usb_free_urb(dev->interrupt_out_urb);
  255. kfree (dev->read_buffer);
  256. kfree (dev->interrupt_in_buffer);
  257. kfree (dev->interrupt_out_buffer);
  258. kfree (dev);
  259. }
  260. /**
  261. * tower_open
  262. */
  263. static int tower_open (struct inode *inode, struct file *file)
  264. {
  265. struct lego_usb_tower *dev = NULL;
  266. int subminor;
  267. int retval = 0;
  268. struct usb_interface *interface;
  269. struct tower_reset_reply *reset_reply;
  270. int result;
  271. reset_reply = kmalloc(sizeof(*reset_reply), GFP_KERNEL);
  272. if (!reset_reply) {
  273. retval = -ENOMEM;
  274. goto exit;
  275. }
  276. nonseekable_open(inode, file);
  277. subminor = iminor(inode);
  278. interface = usb_find_interface (&tower_driver, subminor);
  279. if (!interface) {
  280. pr_err("error, can't find device for minor %d\n", subminor);
  281. retval = -ENODEV;
  282. goto exit;
  283. }
  284. mutex_lock(&open_disc_mutex);
  285. dev = usb_get_intfdata(interface);
  286. if (!dev) {
  287. mutex_unlock(&open_disc_mutex);
  288. retval = -ENODEV;
  289. goto exit;
  290. }
  291. /* lock this device */
  292. if (mutex_lock_interruptible(&dev->lock)) {
  293. mutex_unlock(&open_disc_mutex);
  294. retval = -ERESTARTSYS;
  295. goto exit;
  296. }
  297. /* allow opening only once */
  298. if (dev->open_count) {
  299. mutex_unlock(&open_disc_mutex);
  300. retval = -EBUSY;
  301. goto unlock_exit;
  302. }
  303. dev->open_count = 1;
  304. mutex_unlock(&open_disc_mutex);
  305. /* reset the tower */
  306. result = usb_control_msg (dev->udev,
  307. usb_rcvctrlpipe(dev->udev, 0),
  308. LEGO_USB_TOWER_REQUEST_RESET,
  309. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  310. 0,
  311. 0,
  312. reset_reply,
  313. sizeof(*reset_reply),
  314. 1000);
  315. if (result < 0) {
  316. dev_err(&dev->udev->dev,
  317. "LEGO USB Tower reset control request failed\n");
  318. retval = result;
  319. goto unlock_exit;
  320. }
  321. /* initialize in direction */
  322. dev->read_buffer_length = 0;
  323. dev->read_packet_length = 0;
  324. usb_fill_int_urb (dev->interrupt_in_urb,
  325. dev->udev,
  326. usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress),
  327. dev->interrupt_in_buffer,
  328. usb_endpoint_maxp(dev->interrupt_in_endpoint),
  329. tower_interrupt_in_callback,
  330. dev,
  331. dev->interrupt_in_interval);
  332. dev->interrupt_in_running = 1;
  333. dev->interrupt_in_done = 0;
  334. mb();
  335. retval = usb_submit_urb (dev->interrupt_in_urb, GFP_KERNEL);
  336. if (retval) {
  337. dev_err(&dev->udev->dev,
  338. "Couldn't submit interrupt_in_urb %d\n", retval);
  339. dev->interrupt_in_running = 0;
  340. dev->open_count = 0;
  341. goto unlock_exit;
  342. }
  343. /* save device in the file's private structure */
  344. file->private_data = dev;
  345. unlock_exit:
  346. mutex_unlock(&dev->lock);
  347. exit:
  348. kfree(reset_reply);
  349. return retval;
  350. }
  351. /**
  352. * tower_release
  353. */
  354. static int tower_release (struct inode *inode, struct file *file)
  355. {
  356. struct lego_usb_tower *dev;
  357. int retval = 0;
  358. dev = file->private_data;
  359. if (dev == NULL) {
  360. retval = -ENODEV;
  361. goto exit_nolock;
  362. }
  363. mutex_lock(&open_disc_mutex);
  364. if (mutex_lock_interruptible(&dev->lock)) {
  365. retval = -ERESTARTSYS;
  366. goto exit;
  367. }
  368. if (dev->open_count != 1) {
  369. dev_dbg(&dev->udev->dev, "%s: device not opened exactly once\n",
  370. __func__);
  371. retval = -ENODEV;
  372. goto unlock_exit;
  373. }
  374. if (dev->udev == NULL) {
  375. /* the device was unplugged before the file was released */
  376. /* unlock here as tower_delete frees dev */
  377. mutex_unlock(&dev->lock);
  378. tower_delete (dev);
  379. goto exit;
  380. }
  381. /* wait until write transfer is finished */
  382. if (dev->interrupt_out_busy) {
  383. wait_event_interruptible_timeout (dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
  384. }
  385. tower_abort_transfers (dev);
  386. dev->open_count = 0;
  387. unlock_exit:
  388. mutex_unlock(&dev->lock);
  389. exit:
  390. mutex_unlock(&open_disc_mutex);
  391. exit_nolock:
  392. return retval;
  393. }
  394. /**
  395. * tower_abort_transfers
  396. * aborts transfers and frees associated data structures
  397. */
  398. static void tower_abort_transfers (struct lego_usb_tower *dev)
  399. {
  400. if (dev == NULL)
  401. return;
  402. /* shutdown transfer */
  403. if (dev->interrupt_in_running) {
  404. dev->interrupt_in_running = 0;
  405. mb();
  406. if (dev->udev)
  407. usb_kill_urb (dev->interrupt_in_urb);
  408. }
  409. if (dev->interrupt_out_busy && dev->udev)
  410. usb_kill_urb(dev->interrupt_out_urb);
  411. }
  412. /**
  413. * tower_check_for_read_packet
  414. *
  415. * To get correct semantics for signals and non-blocking I/O
  416. * with packetizing we pretend not to see any data in the read buffer
  417. * until it has been there unchanged for at least
  418. * dev->packet_timeout_jiffies, or until the buffer is full.
  419. */
  420. static void tower_check_for_read_packet (struct lego_usb_tower *dev)
  421. {
  422. spin_lock_irq (&dev->read_buffer_lock);
  423. if (!packet_timeout
  424. || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies)
  425. || dev->read_buffer_length == read_buffer_size) {
  426. dev->read_packet_length = dev->read_buffer_length;
  427. }
  428. dev->interrupt_in_done = 0;
  429. spin_unlock_irq (&dev->read_buffer_lock);
  430. }
  431. /**
  432. * tower_poll
  433. */
  434. static __poll_t tower_poll (struct file *file, poll_table *wait)
  435. {
  436. struct lego_usb_tower *dev;
  437. __poll_t mask = 0;
  438. dev = file->private_data;
  439. if (!dev->udev)
  440. return EPOLLERR | EPOLLHUP;
  441. poll_wait(file, &dev->read_wait, wait);
  442. poll_wait(file, &dev->write_wait, wait);
  443. tower_check_for_read_packet(dev);
  444. if (dev->read_packet_length > 0) {
  445. mask |= EPOLLIN | EPOLLRDNORM;
  446. }
  447. if (!dev->interrupt_out_busy) {
  448. mask |= EPOLLOUT | EPOLLWRNORM;
  449. }
  450. return mask;
  451. }
  452. /**
  453. * tower_llseek
  454. */
  455. static loff_t tower_llseek (struct file *file, loff_t off, int whence)
  456. {
  457. return -ESPIPE; /* unseekable */
  458. }
  459. /**
  460. * tower_read
  461. */
  462. static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
  463. {
  464. struct lego_usb_tower *dev;
  465. size_t bytes_to_read;
  466. int i;
  467. int retval = 0;
  468. unsigned long timeout = 0;
  469. dev = file->private_data;
  470. /* lock this object */
  471. if (mutex_lock_interruptible(&dev->lock)) {
  472. retval = -ERESTARTSYS;
  473. goto exit;
  474. }
  475. /* verify that the device wasn't unplugged */
  476. if (dev->udev == NULL) {
  477. retval = -ENODEV;
  478. pr_err("No device or device unplugged %d\n", retval);
  479. goto unlock_exit;
  480. }
  481. /* verify that we actually have some data to read */
  482. if (count == 0) {
  483. dev_dbg(&dev->udev->dev, "read request of 0 bytes\n");
  484. goto unlock_exit;
  485. }
  486. if (read_timeout) {
  487. timeout = jiffies + msecs_to_jiffies(read_timeout);
  488. }
  489. /* wait for data */
  490. tower_check_for_read_packet (dev);
  491. while (dev->read_packet_length == 0) {
  492. if (file->f_flags & O_NONBLOCK) {
  493. retval = -EAGAIN;
  494. goto unlock_exit;
  495. }
  496. retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies);
  497. if (retval < 0) {
  498. goto unlock_exit;
  499. }
  500. /* reset read timeout during read or write activity */
  501. if (read_timeout
  502. && (dev->read_buffer_length || dev->interrupt_out_busy)) {
  503. timeout = jiffies + msecs_to_jiffies(read_timeout);
  504. }
  505. /* check for read timeout */
  506. if (read_timeout && time_after (jiffies, timeout)) {
  507. retval = -ETIMEDOUT;
  508. goto unlock_exit;
  509. }
  510. tower_check_for_read_packet (dev);
  511. }
  512. /* copy the data from read_buffer into userspace */
  513. bytes_to_read = min(count, dev->read_packet_length);
  514. if (copy_to_user (buffer, dev->read_buffer, bytes_to_read)) {
  515. retval = -EFAULT;
  516. goto unlock_exit;
  517. }
  518. spin_lock_irq (&dev->read_buffer_lock);
  519. dev->read_buffer_length -= bytes_to_read;
  520. dev->read_packet_length -= bytes_to_read;
  521. for (i=0; i<dev->read_buffer_length; i++) {
  522. dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read];
  523. }
  524. spin_unlock_irq (&dev->read_buffer_lock);
  525. retval = bytes_to_read;
  526. unlock_exit:
  527. /* unlock the device */
  528. mutex_unlock(&dev->lock);
  529. exit:
  530. return retval;
  531. }
  532. /**
  533. * tower_write
  534. */
  535. static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
  536. {
  537. struct lego_usb_tower *dev;
  538. size_t bytes_to_write;
  539. int retval = 0;
  540. dev = file->private_data;
  541. /* lock this object */
  542. if (mutex_lock_interruptible(&dev->lock)) {
  543. retval = -ERESTARTSYS;
  544. goto exit;
  545. }
  546. /* verify that the device wasn't unplugged */
  547. if (dev->udev == NULL) {
  548. retval = -ENODEV;
  549. pr_err("No device or device unplugged %d\n", retval);
  550. goto unlock_exit;
  551. }
  552. /* verify that we actually have some data to write */
  553. if (count == 0) {
  554. dev_dbg(&dev->udev->dev, "write request of 0 bytes\n");
  555. goto unlock_exit;
  556. }
  557. /* wait until previous transfer is finished */
  558. while (dev->interrupt_out_busy) {
  559. if (file->f_flags & O_NONBLOCK) {
  560. retval = -EAGAIN;
  561. goto unlock_exit;
  562. }
  563. retval = wait_event_interruptible (dev->write_wait, !dev->interrupt_out_busy);
  564. if (retval) {
  565. goto unlock_exit;
  566. }
  567. }
  568. /* write the data into interrupt_out_buffer from userspace */
  569. bytes_to_write = min_t(int, count, write_buffer_size);
  570. dev_dbg(&dev->udev->dev, "%s: count = %zd, bytes_to_write = %zd\n",
  571. __func__, count, bytes_to_write);
  572. if (copy_from_user (dev->interrupt_out_buffer, buffer, bytes_to_write)) {
  573. retval = -EFAULT;
  574. goto unlock_exit;
  575. }
  576. /* send off the urb */
  577. usb_fill_int_urb(dev->interrupt_out_urb,
  578. dev->udev,
  579. usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
  580. dev->interrupt_out_buffer,
  581. bytes_to_write,
  582. tower_interrupt_out_callback,
  583. dev,
  584. dev->interrupt_out_interval);
  585. dev->interrupt_out_busy = 1;
  586. wmb();
  587. retval = usb_submit_urb (dev->interrupt_out_urb, GFP_KERNEL);
  588. if (retval) {
  589. dev->interrupt_out_busy = 0;
  590. dev_err(&dev->udev->dev,
  591. "Couldn't submit interrupt_out_urb %d\n", retval);
  592. goto unlock_exit;
  593. }
  594. retval = bytes_to_write;
  595. unlock_exit:
  596. /* unlock the device */
  597. mutex_unlock(&dev->lock);
  598. exit:
  599. return retval;
  600. }
  601. /**
  602. * tower_interrupt_in_callback
  603. */
  604. static void tower_interrupt_in_callback (struct urb *urb)
  605. {
  606. struct lego_usb_tower *dev = urb->context;
  607. int status = urb->status;
  608. int retval;
  609. unsigned long flags;
  610. lego_usb_tower_debug_data(&dev->udev->dev, __func__,
  611. urb->actual_length, urb->transfer_buffer);
  612. if (status) {
  613. if (status == -ENOENT ||
  614. status == -ECONNRESET ||
  615. status == -ESHUTDOWN) {
  616. goto exit;
  617. } else {
  618. dev_dbg(&dev->udev->dev,
  619. "%s: nonzero status received: %d\n", __func__,
  620. status);
  621. goto resubmit; /* maybe we can recover */
  622. }
  623. }
  624. if (urb->actual_length > 0) {
  625. spin_lock_irqsave(&dev->read_buffer_lock, flags);
  626. if (dev->read_buffer_length + urb->actual_length < read_buffer_size) {
  627. memcpy (dev->read_buffer + dev->read_buffer_length,
  628. dev->interrupt_in_buffer,
  629. urb->actual_length);
  630. dev->read_buffer_length += urb->actual_length;
  631. dev->read_last_arrival = jiffies;
  632. dev_dbg(&dev->udev->dev, "%s: received %d bytes\n",
  633. __func__, urb->actual_length);
  634. } else {
  635. pr_warn("read_buffer overflow, %d bytes dropped\n",
  636. urb->actual_length);
  637. }
  638. spin_unlock_irqrestore(&dev->read_buffer_lock, flags);
  639. }
  640. resubmit:
  641. /* resubmit if we're still running */
  642. if (dev->interrupt_in_running && dev->udev) {
  643. retval = usb_submit_urb (dev->interrupt_in_urb, GFP_ATOMIC);
  644. if (retval)
  645. dev_err(&dev->udev->dev,
  646. "%s: usb_submit_urb failed (%d)\n",
  647. __func__, retval);
  648. }
  649. exit:
  650. dev->interrupt_in_done = 1;
  651. wake_up_interruptible (&dev->read_wait);
  652. }
  653. /**
  654. * tower_interrupt_out_callback
  655. */
  656. static void tower_interrupt_out_callback (struct urb *urb)
  657. {
  658. struct lego_usb_tower *dev = urb->context;
  659. int status = urb->status;
  660. lego_usb_tower_debug_data(&dev->udev->dev, __func__,
  661. urb->actual_length, urb->transfer_buffer);
  662. /* sync/async unlink faults aren't errors */
  663. if (status && !(status == -ENOENT ||
  664. status == -ECONNRESET ||
  665. status == -ESHUTDOWN)) {
  666. dev_dbg(&dev->udev->dev,
  667. "%s: nonzero write bulk status received: %d\n", __func__,
  668. status);
  669. }
  670. dev->interrupt_out_busy = 0;
  671. wake_up_interruptible(&dev->write_wait);
  672. }
  673. /**
  674. * tower_probe
  675. *
  676. * Called by the usb core when a new device is connected that it thinks
  677. * this driver might be interested in.
  678. */
  679. static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id)
  680. {
  681. struct device *idev = &interface->dev;
  682. struct usb_device *udev = interface_to_usbdev(interface);
  683. struct lego_usb_tower *dev = NULL;
  684. struct tower_get_version_reply *get_version_reply = NULL;
  685. int retval = -ENOMEM;
  686. int result;
  687. /* allocate memory for our device state and initialize it */
  688. dev = kmalloc (sizeof(struct lego_usb_tower), GFP_KERNEL);
  689. if (!dev)
  690. goto exit;
  691. mutex_init(&dev->lock);
  692. dev->udev = udev;
  693. dev->open_count = 0;
  694. dev->read_buffer = NULL;
  695. dev->read_buffer_length = 0;
  696. dev->read_packet_length = 0;
  697. spin_lock_init (&dev->read_buffer_lock);
  698. dev->packet_timeout_jiffies = msecs_to_jiffies(packet_timeout);
  699. dev->read_last_arrival = jiffies;
  700. init_waitqueue_head (&dev->read_wait);
  701. init_waitqueue_head (&dev->write_wait);
  702. dev->interrupt_in_buffer = NULL;
  703. dev->interrupt_in_endpoint = NULL;
  704. dev->interrupt_in_urb = NULL;
  705. dev->interrupt_in_running = 0;
  706. dev->interrupt_in_done = 0;
  707. dev->interrupt_out_buffer = NULL;
  708. dev->interrupt_out_endpoint = NULL;
  709. dev->interrupt_out_urb = NULL;
  710. dev->interrupt_out_busy = 0;
  711. result = usb_find_common_endpoints_reverse(interface->cur_altsetting,
  712. NULL, NULL,
  713. &dev->interrupt_in_endpoint,
  714. &dev->interrupt_out_endpoint);
  715. if (result) {
  716. dev_err(idev, "interrupt endpoints not found\n");
  717. retval = result;
  718. goto error;
  719. }
  720. dev->read_buffer = kmalloc (read_buffer_size, GFP_KERNEL);
  721. if (!dev->read_buffer)
  722. goto error;
  723. dev->interrupt_in_buffer = kmalloc (usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL);
  724. if (!dev->interrupt_in_buffer)
  725. goto error;
  726. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  727. if (!dev->interrupt_in_urb)
  728. goto error;
  729. dev->interrupt_out_buffer = kmalloc (write_buffer_size, GFP_KERNEL);
  730. if (!dev->interrupt_out_buffer)
  731. goto error;
  732. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  733. if (!dev->interrupt_out_urb)
  734. goto error;
  735. dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
  736. dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
  737. get_version_reply = kmalloc(sizeof(*get_version_reply), GFP_KERNEL);
  738. if (!get_version_reply) {
  739. retval = -ENOMEM;
  740. goto error;
  741. }
  742. /* get the firmware version and log it */
  743. result = usb_control_msg (udev,
  744. usb_rcvctrlpipe(udev, 0),
  745. LEGO_USB_TOWER_REQUEST_GET_VERSION,
  746. USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
  747. 0,
  748. 0,
  749. get_version_reply,
  750. sizeof(*get_version_reply),
  751. 1000);
  752. if (result < 0) {
  753. dev_err(idev, "LEGO USB Tower get version control request failed\n");
  754. retval = result;
  755. goto error;
  756. }
  757. dev_info(&interface->dev,
  758. "LEGO USB Tower firmware version is %d.%d build %d\n",
  759. get_version_reply->major,
  760. get_version_reply->minor,
  761. le16_to_cpu(get_version_reply->build_no));
  762. /* we can register the device now, as it is ready */
  763. usb_set_intfdata (interface, dev);
  764. retval = usb_register_dev (interface, &tower_class);
  765. if (retval) {
  766. /* something prevented us from registering this driver */
  767. dev_err(idev, "Not able to get a minor for this device.\n");
  768. usb_set_intfdata (interface, NULL);
  769. goto error;
  770. }
  771. dev->minor = interface->minor;
  772. /* let the user know what node this device is now attached to */
  773. dev_info(&interface->dev, "LEGO USB Tower #%d now attached to major "
  774. "%d minor %d\n", (dev->minor - LEGO_USB_TOWER_MINOR_BASE),
  775. USB_MAJOR, dev->minor);
  776. exit:
  777. kfree(get_version_reply);
  778. return retval;
  779. error:
  780. kfree(get_version_reply);
  781. tower_delete(dev);
  782. return retval;
  783. }
  784. /**
  785. * tower_disconnect
  786. *
  787. * Called by the usb core when the device is removed from the system.
  788. */
  789. static void tower_disconnect (struct usb_interface *interface)
  790. {
  791. struct lego_usb_tower *dev;
  792. int minor;
  793. dev = usb_get_intfdata (interface);
  794. mutex_lock(&open_disc_mutex);
  795. usb_set_intfdata (interface, NULL);
  796. minor = dev->minor;
  797. /* give back our minor */
  798. usb_deregister_dev (interface, &tower_class);
  799. mutex_lock(&dev->lock);
  800. mutex_unlock(&open_disc_mutex);
  801. /* if the device is not opened, then we clean up right now */
  802. if (!dev->open_count) {
  803. mutex_unlock(&dev->lock);
  804. tower_delete (dev);
  805. } else {
  806. dev->udev = NULL;
  807. /* wake up pollers */
  808. wake_up_interruptible_all(&dev->read_wait);
  809. wake_up_interruptible_all(&dev->write_wait);
  810. mutex_unlock(&dev->lock);
  811. }
  812. dev_info(&interface->dev, "LEGO USB Tower #%d now disconnected\n",
  813. (minor - LEGO_USB_TOWER_MINOR_BASE));
  814. }
  815. module_usb_driver(tower_driver);
  816. MODULE_AUTHOR(DRIVER_AUTHOR);
  817. MODULE_DESCRIPTION(DRIVER_DESC);
  818. #ifdef MODULE_LICENSE
  819. MODULE_LICENSE("GPL");
  820. #endif