legousbtower.c 26 KB

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