legousbtower.c 27 KB

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