alphatrack.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * Frontier Designs Alphatrack driver
  3. *
  4. * Copyright (C) 2007 Michael Taht (m@taht.net)
  5. *
  6. * Based on the usbled driver and ldusb drivers by
  7. *
  8. * Copyright (C) 2004 Greg Kroah-Hartman (greg@kroah.com)
  9. * Copyright (C) 2005 Michael Hund <mhund@ld-didactic.de>
  10. *
  11. * The ldusb driver was, in turn, derived from Lego USB Tower driver
  12. * Copyright (C) 2003 David Glance <advidgsf@sourceforge.net>
  13. * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation, version 2.
  18. *
  19. */
  20. /**
  21. * This driver uses a ring buffer for time critical reading of
  22. * interrupt in reports and provides read and write methods for
  23. * raw interrupt reports.
  24. */
  25. /* Note: this currently uses a dumb ringbuffer for reads and writes.
  26. * A more optimal driver would cache and kill off outstanding urbs that are
  27. * now invalid, and ignore ones that already were in the queue but valid
  28. * as we only have 30 commands for the alphatrack. In particular this is
  29. * key for getting lights to flash in time as otherwise many commands
  30. * can be buffered up before the light change makes it to the interface.
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/module.h>
  37. #include <linux/kobject.h>
  38. #include <linux/mutex.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/input.h>
  41. #include <linux/usb.h>
  42. #include <linux/poll.h>
  43. #include "alphatrack.h"
  44. #define VENDOR_ID 0x165b
  45. #define PRODUCT_ID 0xfad1
  46. #ifdef CONFIG_USB_DYNAMIC_MINORS
  47. #define USB_ALPHATRACK_MINOR_BASE 0
  48. #else
  49. /* FIXME 176 - is another driver's minor - apply for that */
  50. #define USB_ALPHATRACK_MINOR_BASE 176
  51. #endif
  52. /* table of devices that work with this driver */
  53. static const struct usb_device_id usb_alphatrack_table[] = {
  54. {USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
  55. {} /* Terminating entry */
  56. };
  57. MODULE_DEVICE_TABLE(usb, usb_alphatrack_table);
  58. MODULE_VERSION("0.41");
  59. MODULE_AUTHOR("Mike Taht <m@taht.net>");
  60. MODULE_DESCRIPTION("Alphatrack USB Driver");
  61. MODULE_LICENSE("GPL");
  62. MODULE_SUPPORTED_DEVICE("Frontier Designs Alphatrack Control Surface");
  63. /* These aren't done yet */
  64. #define SUPPRESS_EXTRA_ONLINE_EVENTS 0
  65. #define BUFFERED_WRITES 0
  66. #define SUPPRESS_EXTRA_OFFLINE_EVENTS 0
  67. #define COMPRESS_FADER_EVENTS 0
  68. #define BUFFERED_READS 1
  69. #define RING_BUFFER_SIZE 512
  70. #define WRITE_BUFFER_SIZE 34
  71. #define ALPHATRACK_USB_TIMEOUT 10
  72. #define OUTPUT_CMD_SIZE 8
  73. #define INPUT_CMD_SIZE 12
  74. #define ALPHATRACK_DEBUG 0
  75. static int debug = ALPHATRACK_DEBUG;
  76. /* Use our own dbg macro */
  77. #define dbg_info(dev, format, arg...) do \
  78. { if (debug) dev_info(dev , format , ## arg); } while (0)
  79. #define alphatrack_ocmd_info(dev, cmd, format, arg...)
  80. #define alphatrack_icmd_info(dev, cmd, format, arg...)
  81. /* Module parameters */
  82. module_param(debug, int, S_IRUGO | S_IWUSR);
  83. MODULE_PARM_DESC(debug, "Debug enabled or not");
  84. /* All interrupt in transfers are collected in a ring buffer to
  85. * avoid racing conditions and get better performance of the driver.
  86. */
  87. static int ring_buffer_size = RING_BUFFER_SIZE;
  88. module_param(ring_buffer_size, int, S_IRUGO);
  89. MODULE_PARM_DESC(ring_buffer_size, "Read ring buffer size");
  90. /* The write_buffer can one day contain more than one interrupt out transfer.
  91. */
  92. static int write_buffer_size = WRITE_BUFFER_SIZE;
  93. module_param(write_buffer_size, int, S_IRUGO);
  94. MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
  95. /*
  96. * Increase the interval for debugging purposes.
  97. * or set to 1 to use the standard interval from the endpoint descriptors.
  98. */
  99. static int min_interrupt_in_interval = ALPHATRACK_USB_TIMEOUT;
  100. module_param(min_interrupt_in_interval, int, 0);
  101. MODULE_PARM_DESC(min_interrupt_in_interval,
  102. "Minimum interrupt in interval in ms");
  103. static int min_interrupt_out_interval = ALPHATRACK_USB_TIMEOUT;
  104. module_param(min_interrupt_out_interval, int, 0);
  105. MODULE_PARM_DESC(min_interrupt_out_interval,
  106. "Minimum interrupt out interval in ms");
  107. /* Structure to hold all of our device specific stuff */
  108. struct usb_alphatrack {
  109. struct mutex mtx; /* locks this structure */
  110. struct usb_interface *intf; /* save off the usb interface pointer */
  111. int open_count; /* number of times this port has been opened */
  112. /* make gcc happy */
  113. struct alphatrack_icmd (*ring_buffer)[RING_BUFFER_SIZE];
  114. struct alphatrack_ocmd (*write_buffer)[WRITE_BUFFER_SIZE];
  115. unsigned int ring_head;
  116. unsigned int ring_tail;
  117. wait_queue_head_t read_wait;
  118. wait_queue_head_t write_wait;
  119. unsigned char *interrupt_in_buffer;
  120. unsigned char *oldi_buffer;
  121. struct usb_endpoint_descriptor *interrupt_in_endpoint;
  122. struct urb *interrupt_in_urb;
  123. int interrupt_in_interval;
  124. size_t interrupt_in_endpoint_size;
  125. int interrupt_in_running;
  126. int interrupt_in_done;
  127. char *interrupt_out_buffer;
  128. struct usb_endpoint_descriptor *interrupt_out_endpoint;
  129. struct urb *interrupt_out_urb;
  130. int interrupt_out_interval;
  131. size_t interrupt_out_endpoint_size;
  132. int interrupt_out_busy;
  133. atomic_t writes_pending;
  134. int event; /* alternate interface to events */
  135. int fader; /* 10 bits */
  136. int lights; /* 23 bits */
  137. unsigned char dump_state; /* 0 if disabled 1 if enabled */
  138. unsigned char enable; /* 0 if disabled 1 if enabled */
  139. unsigned char offline; /* if the device is out of range or asleep */
  140. unsigned char verbose; /* be verbose in error reporting */
  141. unsigned char last_cmd[OUTPUT_CMD_SIZE];
  142. unsigned char screen[32];
  143. };
  144. /* prevent races between open() and disconnect() */
  145. static DEFINE_MUTEX(disconnect_mutex);
  146. /* forward declaration */
  147. static struct usb_driver usb_alphatrack_driver;
  148. /**
  149. * usb_alphatrack_abort_transfers
  150. * aborts transfers and frees associated data structures
  151. */
  152. static void usb_alphatrack_abort_transfers(struct usb_alphatrack *dev)
  153. {
  154. /* shutdown transfer */
  155. if (dev->interrupt_in_running) {
  156. dev->interrupt_in_running = 0;
  157. if (dev->intf)
  158. usb_kill_urb(dev->interrupt_in_urb);
  159. }
  160. if (dev->interrupt_out_busy)
  161. if (dev->intf)
  162. usb_kill_urb(dev->interrupt_out_urb);
  163. }
  164. /**
  165. * usb_alphatrack_delete
  166. */
  167. static void usb_alphatrack_delete(struct usb_alphatrack *dev)
  168. {
  169. usb_alphatrack_abort_transfers(dev);
  170. usb_free_urb(dev->interrupt_in_urb);
  171. usb_free_urb(dev->interrupt_out_urb);
  172. kfree(dev->ring_buffer);
  173. kfree(dev->interrupt_in_buffer);
  174. kfree(dev->interrupt_out_buffer);
  175. kfree(dev); /* fixme oldi_buffer */
  176. }
  177. /**
  178. * usb_alphatrack_interrupt_in_callback
  179. */
  180. static void usb_alphatrack_interrupt_in_callback(struct urb *urb)
  181. {
  182. struct usb_alphatrack *dev = urb->context;
  183. unsigned int next_ring_head;
  184. int retval = -1;
  185. if (urb->status) {
  186. if (urb->status == -ENOENT ||
  187. urb->status == -ECONNRESET || urb->status == -ESHUTDOWN) {
  188. goto exit;
  189. } else {
  190. dbg_info(&dev->intf->dev,
  191. "%s: nonzero status received: %d\n", __func__,
  192. urb->status);
  193. goto resubmit; /* maybe we can recover */
  194. }
  195. }
  196. if (urb->actual_length != INPUT_CMD_SIZE) {
  197. dev_warn(&dev->intf->dev,
  198. "Urb length was %d bytes!!"
  199. "Do something intelligent\n", urb->actual_length);
  200. } else {
  201. alphatrack_ocmd_info(&dev->intf->dev,
  202. &(*dev->ring_buffer)[dev->ring_tail].cmd,
  203. "%s", "bla");
  204. if (memcmp
  205. (dev->interrupt_in_buffer, dev->oldi_buffer,
  206. INPUT_CMD_SIZE) == 0) {
  207. goto resubmit;
  208. }
  209. memcpy(dev->oldi_buffer, dev->interrupt_in_buffer,
  210. INPUT_CMD_SIZE);
  211. #if SUPPRESS_EXTRA_OFFLINE_EVENTS
  212. if (dev->offline == 2 && dev->interrupt_in_buffer[1] == 0xff)
  213. goto resubmit;
  214. if (dev->offline == 1 && dev->interrupt_in_buffer[1] == 0xff) {
  215. dev->offline = 2;
  216. goto resubmit;
  217. }
  218. /* Always pass one offline event up the stack */
  219. if (dev->offline > 0 && dev->interrupt_in_buffer[1] != 0xff)
  220. dev->offline = 0;
  221. if (dev->offline == 0 && dev->interrupt_in_buffer[1] == 0xff)
  222. dev->offline = 1;
  223. #endif
  224. dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
  225. __func__, dev->ring_head, dev->ring_tail);
  226. next_ring_head = (dev->ring_head + 1) % ring_buffer_size;
  227. if (next_ring_head != dev->ring_tail) {
  228. memcpy(&((*dev->ring_buffer)[dev->ring_head]),
  229. dev->interrupt_in_buffer, urb->actual_length);
  230. dev->ring_head = next_ring_head;
  231. retval = 0;
  232. memset(dev->interrupt_in_buffer, 0, urb->actual_length);
  233. } else {
  234. dev_warn(&dev->intf->dev,
  235. "Ring buffer overflow, %d bytes dropped\n",
  236. urb->actual_length);
  237. memset(dev->interrupt_in_buffer, 0, urb->actual_length);
  238. }
  239. }
  240. resubmit:
  241. /* resubmit if we're still running */
  242. if (dev->interrupt_in_running && dev->intf) {
  243. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
  244. if (retval)
  245. dev_err(&dev->intf->dev,
  246. "usb_submit_urb failed (%d)\n", retval);
  247. }
  248. exit:
  249. dev->interrupt_in_done = 1;
  250. wake_up_interruptible(&dev->read_wait);
  251. }
  252. /**
  253. * usb_alphatrack_interrupt_out_callback
  254. */
  255. static void usb_alphatrack_interrupt_out_callback(struct urb *urb)
  256. {
  257. struct usb_alphatrack *dev = urb->context;
  258. /* sync/async unlink faults aren't errors */
  259. if (urb->status && !(urb->status == -ENOENT ||
  260. urb->status == -ECONNRESET ||
  261. urb->status == -ESHUTDOWN))
  262. dbg_info(&dev->intf->dev,
  263. "%s - nonzero write interrupt status received: %d\n",
  264. __func__, urb->status);
  265. atomic_dec(&dev->writes_pending);
  266. dev->interrupt_out_busy = 0;
  267. wake_up_interruptible(&dev->write_wait);
  268. }
  269. /**
  270. * usb_alphatrack_open
  271. */
  272. static int usb_alphatrack_open(struct inode *inode, struct file *file)
  273. {
  274. struct usb_alphatrack *dev;
  275. int subminor;
  276. int retval = 0;
  277. struct usb_interface *interface;
  278. nonseekable_open(inode, file);
  279. subminor = iminor(inode);
  280. mutex_lock(&disconnect_mutex);
  281. interface = usb_find_interface(&usb_alphatrack_driver, subminor);
  282. if (!interface) {
  283. err("%s - error, can't find device for minor %d\n",
  284. __func__, subminor);
  285. retval = -ENODEV;
  286. goto unlock_disconnect_exit;
  287. }
  288. dev = usb_get_intfdata(interface);
  289. if (!dev) {
  290. retval = -ENODEV;
  291. goto unlock_disconnect_exit;
  292. }
  293. /* lock this device */
  294. if (mutex_lock_interruptible(&dev->mtx)) {
  295. retval = -ERESTARTSYS;
  296. goto unlock_disconnect_exit;
  297. }
  298. /* allow opening only once */
  299. if (dev->open_count) {
  300. retval = -EBUSY;
  301. goto unlock_exit;
  302. }
  303. dev->open_count = 1;
  304. /* initialize in direction */
  305. dev->ring_head = 0;
  306. dev->ring_tail = 0;
  307. usb_fill_int_urb(dev->interrupt_in_urb,
  308. interface_to_usbdev(interface),
  309. usb_rcvintpipe(interface_to_usbdev(interface),
  310. dev->interrupt_in_endpoint->
  311. bEndpointAddress),
  312. dev->interrupt_in_buffer,
  313. dev->interrupt_in_endpoint_size,
  314. usb_alphatrack_interrupt_in_callback, dev,
  315. dev->interrupt_in_interval);
  316. dev->interrupt_in_running = 1;
  317. dev->interrupt_in_done = 0;
  318. dev->enable = 1;
  319. dev->offline = 0;
  320. retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
  321. if (retval) {
  322. dev_err(&interface->dev,
  323. "Couldn't submit interrupt_in_urb %d\n", retval);
  324. dev->interrupt_in_running = 0;
  325. dev->open_count = 0;
  326. goto unlock_exit;
  327. }
  328. /* save device in the file's private structure */
  329. file->private_data = dev;
  330. unlock_exit:
  331. mutex_unlock(&dev->mtx);
  332. unlock_disconnect_exit:
  333. mutex_unlock(&disconnect_mutex);
  334. return retval;
  335. }
  336. /**
  337. * usb_alphatrack_release
  338. */
  339. static int usb_alphatrack_release(struct inode *inode, struct file *file)
  340. {
  341. struct usb_alphatrack *dev;
  342. int retval = 0;
  343. dev = file->private_data;
  344. if (dev == NULL) {
  345. retval = -ENODEV;
  346. goto exit;
  347. }
  348. if (mutex_lock_interruptible(&dev->mtx)) {
  349. retval = -ERESTARTSYS;
  350. goto exit;
  351. }
  352. if (dev->open_count != 1) {
  353. retval = -ENODEV;
  354. goto unlock_exit;
  355. }
  356. if (dev->intf == NULL) {
  357. /* the device was unplugged before the file was released */
  358. mutex_unlock(&dev->mtx);
  359. /* unlock here as usb_alphatrack_delete frees dev */
  360. usb_alphatrack_delete(dev);
  361. retval = -ENODEV;
  362. goto exit;
  363. }
  364. /* wait until write transfer is finished */
  365. if (dev->interrupt_out_busy)
  366. wait_event_interruptible_timeout(dev->write_wait,
  367. !dev->interrupt_out_busy,
  368. 2 * HZ);
  369. usb_alphatrack_abort_transfers(dev);
  370. dev->open_count = 0;
  371. unlock_exit:
  372. mutex_unlock(&dev->mtx);
  373. exit:
  374. return retval;
  375. }
  376. /**
  377. * usb_alphatrack_poll
  378. */
  379. static unsigned int usb_alphatrack_poll(struct file *file, poll_table * wait)
  380. {
  381. struct usb_alphatrack *dev;
  382. unsigned int mask = 0;
  383. dev = file->private_data;
  384. poll_wait(file, &dev->read_wait, wait);
  385. poll_wait(file, &dev->write_wait, wait);
  386. if (dev->ring_head != dev->ring_tail)
  387. mask |= POLLIN | POLLRDNORM;
  388. if (!dev->interrupt_out_busy)
  389. mask |= POLLOUT | POLLWRNORM;
  390. return mask;
  391. }
  392. /**
  393. * usb_alphatrack_read
  394. */
  395. static ssize_t usb_alphatrack_read(struct file *file, char __user *buffer,
  396. size_t count, loff_t *ppos)
  397. {
  398. struct usb_alphatrack *dev;
  399. int retval = 0;
  400. int c = 0;
  401. dev = file->private_data;
  402. /* verify that we actually have some data to read */
  403. if (count == 0)
  404. goto exit;
  405. /* lock this object */
  406. if (mutex_lock_interruptible(&dev->mtx)) {
  407. retval = -ERESTARTSYS;
  408. goto exit;
  409. }
  410. /* verify that the device wasn't unplugged */
  411. if (dev->intf == NULL) {
  412. retval = -ENODEV;
  413. err("No device or device unplugged %d\n", retval);
  414. goto unlock_exit;
  415. }
  416. while (dev->ring_head == dev->ring_tail) {
  417. if (file->f_flags & O_NONBLOCK) {
  418. retval = -EAGAIN;
  419. goto unlock_exit;
  420. }
  421. dev->interrupt_in_done = 0;
  422. retval =
  423. wait_event_interruptible(dev->read_wait,
  424. dev->interrupt_in_done);
  425. if (retval < 0)
  426. goto unlock_exit;
  427. }
  428. alphatrack_ocmd_info(&dev->intf->dev,
  429. &(*dev->ring_buffer)[dev->ring_tail].cmd, "%s",
  430. ": copying to userspace");
  431. c = 0;
  432. while ((c < count) && (dev->ring_tail != dev->ring_head)) {
  433. if (copy_to_user
  434. (&buffer[c], &(*dev->ring_buffer)[dev->ring_tail],
  435. INPUT_CMD_SIZE)) {
  436. retval = -EFAULT;
  437. goto unlock_exit;
  438. }
  439. dev->ring_tail = (dev->ring_tail + 1) % ring_buffer_size;
  440. c += INPUT_CMD_SIZE;
  441. dbg_info(&dev->intf->dev, "%s: head, tail are %x, %x\n",
  442. __func__, dev->ring_head, dev->ring_tail);
  443. }
  444. retval = c;
  445. unlock_exit:
  446. /* unlock the device */
  447. mutex_unlock(&dev->mtx);
  448. exit:
  449. return retval;
  450. }
  451. /**
  452. * usb_alphatrack_write
  453. */
  454. static ssize_t usb_alphatrack_write(struct file *file,
  455. const char __user *buffer, size_t count,
  456. loff_t *ppos)
  457. {
  458. struct usb_alphatrack *dev;
  459. size_t bytes_to_write;
  460. int retval = 0;
  461. dev = file->private_data;
  462. /* verify that we actually have some data to write */
  463. if (count == 0)
  464. goto exit;
  465. /* lock this object */
  466. if (mutex_lock_interruptible(&dev->mtx)) {
  467. retval = -ERESTARTSYS;
  468. goto exit;
  469. }
  470. /* verify that the device wasn't unplugged */
  471. if (dev->intf == NULL) {
  472. retval = -ENODEV;
  473. err("No device or device unplugged %d\n", retval);
  474. goto unlock_exit;
  475. }
  476. /* wait until previous transfer is finished */
  477. if (dev->interrupt_out_busy) {
  478. if (file->f_flags & O_NONBLOCK) {
  479. retval = -EAGAIN;
  480. goto unlock_exit;
  481. }
  482. retval =
  483. wait_event_interruptible(dev->write_wait,
  484. !dev->interrupt_out_busy);
  485. if (retval < 0)
  486. goto unlock_exit;
  487. }
  488. /* write the data into interrupt_out_buffer from userspace */
  489. /* FIXME - if you write more than 12 bytes this breaks */
  490. bytes_to_write =
  491. min(count, write_buffer_size * dev->interrupt_out_endpoint_size);
  492. if (bytes_to_write < count)
  493. dev_warn(&dev->intf->dev,
  494. "Write buffer overflow, %zd bytes dropped\n",
  495. count - bytes_to_write);
  496. dbg_info(&dev->intf->dev, "%s: count = %zd, bytes_to_write = %zd\n",
  497. __func__, count, bytes_to_write);
  498. if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write)) {
  499. retval = -EFAULT;
  500. goto unlock_exit;
  501. }
  502. if (dev->interrupt_out_endpoint == NULL) {
  503. err("Endpoint should not be be null!\n");
  504. goto unlock_exit;
  505. }
  506. /* send off the urb */
  507. usb_fill_int_urb(dev->interrupt_out_urb,
  508. interface_to_usbdev(dev->intf),
  509. usb_sndintpipe(interface_to_usbdev(dev->intf),
  510. dev->interrupt_out_endpoint->
  511. bEndpointAddress),
  512. dev->interrupt_out_buffer, bytes_to_write,
  513. usb_alphatrack_interrupt_out_callback, dev,
  514. dev->interrupt_out_interval);
  515. dev->interrupt_out_busy = 1;
  516. atomic_inc(&dev->writes_pending);
  517. wmb();
  518. retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
  519. if (retval) {
  520. dev->interrupt_out_busy = 0;
  521. err("Couldn't submit interrupt_out_urb %d\n", retval);
  522. atomic_dec(&dev->writes_pending);
  523. goto unlock_exit;
  524. }
  525. retval = bytes_to_write;
  526. unlock_exit:
  527. /* unlock the device */
  528. mutex_unlock(&dev->mtx);
  529. exit:
  530. return retval;
  531. }
  532. /* file operations needed when we register this driver */
  533. static const struct file_operations usb_alphatrack_fops = {
  534. .owner = THIS_MODULE,
  535. .read = usb_alphatrack_read,
  536. .write = usb_alphatrack_write,
  537. .open = usb_alphatrack_open,
  538. .release = usb_alphatrack_release,
  539. .poll = usb_alphatrack_poll,
  540. .llseek = no_llseek,
  541. };
  542. /*
  543. * usb class driver info in order to get a minor number from the usb core,
  544. * and to have the device registered with the driver core
  545. */
  546. static struct usb_class_driver usb_alphatrack_class = {
  547. .name = "alphatrack%d",
  548. .fops = &usb_alphatrack_fops,
  549. .minor_base = USB_ALPHATRACK_MINOR_BASE,
  550. };
  551. /**
  552. * usb_alphatrack_probe
  553. *
  554. * Called by the usb core when a new device is connected that it thinks
  555. * this driver might be interested in.
  556. */
  557. static int usb_alphatrack_probe(struct usb_interface *intf,
  558. const struct usb_device_id *id)
  559. {
  560. struct usb_device *udev = interface_to_usbdev(intf);
  561. struct usb_alphatrack *dev = NULL;
  562. struct usb_host_interface *iface_desc;
  563. struct usb_endpoint_descriptor *endpoint;
  564. int i;
  565. int true_size;
  566. int retval = -ENOMEM;
  567. /* allocate memory for our device state and initialize it */
  568. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  569. if (dev == NULL) {
  570. dev_err(&intf->dev, "Out of memory\n");
  571. goto exit;
  572. }
  573. mutex_init(&dev->mtx);
  574. dev->intf = intf;
  575. init_waitqueue_head(&dev->read_wait);
  576. init_waitqueue_head(&dev->write_wait);
  577. iface_desc = intf->cur_altsetting;
  578. /* set up the endpoint information */
  579. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  580. endpoint = &iface_desc->endpoint[i].desc;
  581. if (usb_endpoint_is_int_in(endpoint))
  582. dev->interrupt_in_endpoint = endpoint;
  583. if (usb_endpoint_is_int_out(endpoint))
  584. dev->interrupt_out_endpoint = endpoint;
  585. }
  586. if (dev->interrupt_in_endpoint == NULL) {
  587. dev_err(&intf->dev, "Interrupt in endpoint not found\n");
  588. goto error;
  589. }
  590. if (dev->interrupt_out_endpoint == NULL)
  591. dev_warn(&intf->dev,
  592. "Interrupt out endpoint not found"
  593. "(using control endpoint instead)\n");
  594. dev->interrupt_in_endpoint_size =
  595. le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
  596. if (dev->interrupt_in_endpoint_size != 64)
  597. dev_warn(&intf->dev, "Interrupt in endpoint size is not 64!\n");
  598. if (ring_buffer_size == 0)
  599. ring_buffer_size = RING_BUFFER_SIZE;
  600. true_size = min(ring_buffer_size, RING_BUFFER_SIZE);
  601. /* FIXME - there are more usb_alloc routines for dma correctness.
  602. Needed? */
  603. dev->ring_buffer =
  604. kmalloc((true_size * sizeof(struct alphatrack_icmd)), GFP_KERNEL);
  605. if (!dev->ring_buffer) {
  606. dev_err(&intf->dev,
  607. "Couldn't allocate input ring_buffer of size %d\n",
  608. true_size);
  609. goto error;
  610. }
  611. dev->interrupt_in_buffer =
  612. kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
  613. if (!dev->interrupt_in_buffer) {
  614. dev_err(&intf->dev, "Couldn't allocate interrupt_in_buffer\n");
  615. goto error;
  616. }
  617. dev->oldi_buffer = kmalloc(dev->interrupt_in_endpoint_size, GFP_KERNEL);
  618. if (!dev->oldi_buffer) {
  619. dev_err(&intf->dev, "Couldn't allocate old buffer\n");
  620. goto error;
  621. }
  622. dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  623. if (!dev->interrupt_in_urb) {
  624. dev_err(&intf->dev, "Couldn't allocate interrupt_in_urb\n");
  625. goto error;
  626. }
  627. dev->interrupt_out_endpoint_size =
  628. dev->interrupt_out_endpoint ? le16_to_cpu(dev->
  629. interrupt_out_endpoint->
  630. wMaxPacketSize) : udev->
  631. descriptor.bMaxPacketSize0;
  632. if (dev->interrupt_out_endpoint_size != 64)
  633. dev_warn(&intf->dev,
  634. "Interrupt out endpoint size is not 64!)\n");
  635. if (write_buffer_size == 0)
  636. write_buffer_size = WRITE_BUFFER_SIZE;
  637. true_size = min(write_buffer_size, WRITE_BUFFER_SIZE);
  638. dev->interrupt_out_buffer =
  639. kmalloc(true_size * dev->interrupt_out_endpoint_size, GFP_KERNEL);
  640. if (!dev->interrupt_out_buffer) {
  641. dev_err(&intf->dev, "Couldn't allocate interrupt_out_buffer\n");
  642. goto error;
  643. }
  644. dev->write_buffer =
  645. kmalloc(true_size * sizeof(struct alphatrack_ocmd), GFP_KERNEL);
  646. if (!dev->write_buffer) {
  647. dev_err(&intf->dev, "Couldn't allocate write_buffer\n");
  648. goto error;
  649. }
  650. dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
  651. if (!dev->interrupt_out_urb) {
  652. dev_err(&intf->dev, "Couldn't allocate interrupt_out_urb\n");
  653. goto error;
  654. }
  655. dev->interrupt_in_interval =
  656. min_interrupt_in_interval >
  657. dev->interrupt_in_endpoint->
  658. bInterval ? min_interrupt_in_interval : dev->interrupt_in_endpoint->
  659. bInterval;
  660. if (dev->interrupt_out_endpoint)
  661. dev->interrupt_out_interval =
  662. min_interrupt_out_interval >
  663. dev->interrupt_out_endpoint->
  664. bInterval ? min_interrupt_out_interval : dev->
  665. interrupt_out_endpoint->bInterval;
  666. /* we can register the device now, as it is ready */
  667. usb_set_intfdata(intf, dev);
  668. atomic_set(&dev->writes_pending, 0);
  669. retval = usb_register_dev(intf, &usb_alphatrack_class);
  670. if (retval) {
  671. /* something prevented us from registering this driver */
  672. dev_err(&intf->dev,
  673. "Not able to get a minor for this device.\n");
  674. usb_set_intfdata(intf, NULL);
  675. goto error;
  676. }
  677. /* let the user know what node this device is now attached to */
  678. dev_info(&intf->dev,
  679. "Alphatrack Device #%d now attached to major %d minor %d\n",
  680. (intf->minor - USB_ALPHATRACK_MINOR_BASE), USB_MAJOR,
  681. intf->minor);
  682. exit:
  683. return retval;
  684. error:
  685. usb_alphatrack_delete(dev);
  686. return retval;
  687. }
  688. /**
  689. * usb_alphatrack_disconnect
  690. *
  691. * Called by the usb core when the device is removed from the system.
  692. */
  693. static void usb_alphatrack_disconnect(struct usb_interface *intf)
  694. {
  695. struct usb_alphatrack *dev;
  696. int minor;
  697. mutex_lock(&disconnect_mutex);
  698. dev = usb_get_intfdata(intf);
  699. usb_set_intfdata(intf, NULL);
  700. mutex_lock(&dev->mtx);
  701. minor = intf->minor;
  702. /* give back our minor */
  703. usb_deregister_dev(intf, &usb_alphatrack_class);
  704. /* if the device is not opened, then we clean up right now */
  705. if (!dev->open_count) {
  706. mutex_unlock(&dev->mtx);
  707. usb_alphatrack_delete(dev);
  708. } else {
  709. dev->intf = NULL;
  710. mutex_unlock(&dev->mtx);
  711. }
  712. atomic_set(&dev->writes_pending, 0);
  713. mutex_unlock(&disconnect_mutex);
  714. dev_info(&intf->dev, "Alphatrack Surface #%d now disconnected\n",
  715. (minor - USB_ALPHATRACK_MINOR_BASE));
  716. }
  717. /* usb specific object needed to register this driver with the usb subsystem */
  718. static struct usb_driver usb_alphatrack_driver = {
  719. .name = "alphatrack",
  720. .probe = usb_alphatrack_probe,
  721. .disconnect = usb_alphatrack_disconnect,
  722. .id_table = usb_alphatrack_table,
  723. };
  724. /**
  725. * usb_alphatrack_init
  726. */
  727. static int __init usb_alphatrack_init(void)
  728. {
  729. int retval;
  730. /* register this driver with the USB subsystem */
  731. retval = usb_register(&usb_alphatrack_driver);
  732. if (retval)
  733. err("usb_register failed for the " __FILE__
  734. " driver. Error number %d\n", retval);
  735. return retval;
  736. }
  737. /**
  738. * usb_alphatrack_exit
  739. */
  740. static void __exit usb_alphatrack_exit(void)
  741. {
  742. /* deregister this driver with the USB subsystem */
  743. usb_deregister(&usb_alphatrack_driver);
  744. }
  745. module_init(usb_alphatrack_init);
  746. module_exit(usb_alphatrack_exit);