yurex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * Driver for Meywa-Denki & KAYAC YUREX
  3. *
  4. * Copyright (C) 2010 Tomoki Sekiyama (tomoki.sekiyama@gmail.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/module.h>
  16. #include <linux/kref.h>
  17. #include <linux/mutex.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/usb.h>
  20. #include <linux/hid.h>
  21. #define DRIVER_AUTHOR "Tomoki Sekiyama"
  22. #define DRIVER_DESC "Driver for Meywa-Denki & KAYAC YUREX"
  23. #define YUREX_VENDOR_ID 0x0c45
  24. #define YUREX_PRODUCT_ID 0x1010
  25. #define CMD_ACK '!'
  26. #define CMD_ANIMATE 'A'
  27. #define CMD_COUNT 'C'
  28. #define CMD_LED 'L'
  29. #define CMD_READ 'R'
  30. #define CMD_SET 'S'
  31. #define CMD_VERSION 'V'
  32. #define CMD_EOF 0x0d
  33. #define CMD_PADDING 0xff
  34. #define YUREX_BUF_SIZE 8
  35. #define YUREX_WRITE_TIMEOUT (HZ*2)
  36. /* table of devices that work with this driver */
  37. static struct usb_device_id yurex_table[] = {
  38. { USB_DEVICE(YUREX_VENDOR_ID, YUREX_PRODUCT_ID) },
  39. { } /* Terminating entry */
  40. };
  41. MODULE_DEVICE_TABLE(usb, yurex_table);
  42. #ifdef CONFIG_USB_DYNAMIC_MINORS
  43. #define YUREX_MINOR_BASE 0
  44. #else
  45. #define YUREX_MINOR_BASE 192
  46. #endif
  47. /* Structure to hold all of our device specific stuff */
  48. struct usb_yurex {
  49. struct usb_device *udev;
  50. struct usb_interface *interface;
  51. __u8 int_in_endpointAddr;
  52. struct urb *urb; /* URB for interrupt in */
  53. unsigned char *int_buffer; /* buffer for intterupt in */
  54. struct urb *cntl_urb; /* URB for control msg */
  55. struct usb_ctrlrequest *cntl_req; /* req for control msg */
  56. unsigned char *cntl_buffer; /* buffer for control msg */
  57. struct kref kref;
  58. struct mutex io_mutex;
  59. struct fasync_struct *async_queue;
  60. wait_queue_head_t waitq;
  61. spinlock_t lock;
  62. __s64 bbu; /* BBU from device */
  63. };
  64. #define to_yurex_dev(d) container_of(d, struct usb_yurex, kref)
  65. static struct usb_driver yurex_driver;
  66. static const struct file_operations yurex_fops;
  67. static void yurex_control_callback(struct urb *urb)
  68. {
  69. struct usb_yurex *dev = urb->context;
  70. int status = urb->status;
  71. if (status) {
  72. err("%s - control failed: %d\n", __func__, status);
  73. wake_up_interruptible(&dev->waitq);
  74. return;
  75. }
  76. /* on success, sender woken up by CMD_ACK int in, or timeout */
  77. }
  78. static void yurex_delete(struct kref *kref)
  79. {
  80. struct usb_yurex *dev = to_yurex_dev(kref);
  81. dbg("yurex_delete");
  82. usb_put_dev(dev->udev);
  83. if (dev->cntl_urb) {
  84. usb_kill_urb(dev->cntl_urb);
  85. if (dev->cntl_req)
  86. usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
  87. dev->cntl_req, dev->cntl_urb->setup_dma);
  88. if (dev->cntl_buffer)
  89. usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
  90. dev->cntl_buffer, dev->cntl_urb->transfer_dma);
  91. usb_free_urb(dev->cntl_urb);
  92. }
  93. if (dev->urb) {
  94. usb_kill_urb(dev->urb);
  95. if (dev->int_buffer)
  96. usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
  97. dev->int_buffer, dev->urb->transfer_dma);
  98. usb_free_urb(dev->urb);
  99. }
  100. kfree(dev);
  101. }
  102. /*
  103. * usb class driver info in order to get a minor number from the usb core,
  104. * and to have the device registered with the driver core
  105. */
  106. static struct usb_class_driver yurex_class = {
  107. .name = "yurex%d",
  108. .fops = &yurex_fops,
  109. .minor_base = YUREX_MINOR_BASE,
  110. };
  111. static void yurex_interrupt(struct urb *urb)
  112. {
  113. struct usb_yurex *dev = urb->context;
  114. unsigned char *buf = dev->int_buffer;
  115. int status = urb->status;
  116. unsigned long flags;
  117. int retval, i;
  118. switch (status) {
  119. case 0: /*success*/
  120. break;
  121. case -EOVERFLOW:
  122. err("%s - overflow with length %d, actual length is %d",
  123. __func__, YUREX_BUF_SIZE, dev->urb->actual_length);
  124. case -ECONNRESET:
  125. case -ENOENT:
  126. case -ESHUTDOWN:
  127. case -EILSEQ:
  128. /* The device is terminated, clean up */
  129. return;
  130. default:
  131. err("%s - unknown status received: %d", __func__, status);
  132. goto exit;
  133. }
  134. /* handle received message */
  135. switch (buf[0]) {
  136. case CMD_COUNT:
  137. case CMD_READ:
  138. if (buf[6] == CMD_EOF) {
  139. spin_lock_irqsave(&dev->lock, flags);
  140. dev->bbu = 0;
  141. for (i = 1; i < 6; i++) {
  142. dev->bbu += buf[i];
  143. if (i != 5)
  144. dev->bbu <<= 8;
  145. }
  146. dbg("%s count: %lld", __func__, dev->bbu);
  147. spin_unlock_irqrestore(&dev->lock, flags);
  148. kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
  149. }
  150. else
  151. dbg("data format error - no EOF");
  152. break;
  153. case CMD_ACK:
  154. dbg("%s ack: %c", __func__, buf[1]);
  155. wake_up_interruptible(&dev->waitq);
  156. break;
  157. }
  158. exit:
  159. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  160. if (retval) {
  161. err("%s - usb_submit_urb failed: %d",
  162. __func__, retval);
  163. }
  164. }
  165. static int yurex_probe(struct usb_interface *interface, const struct usb_device_id *id)
  166. {
  167. struct usb_yurex *dev;
  168. struct usb_host_interface *iface_desc;
  169. struct usb_endpoint_descriptor *endpoint;
  170. int retval = -ENOMEM;
  171. int i;
  172. DEFINE_WAIT(wait);
  173. /* allocate memory for our device state and initialize it */
  174. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  175. if (!dev) {
  176. err("Out of memory");
  177. goto error;
  178. }
  179. kref_init(&dev->kref);
  180. mutex_init(&dev->io_mutex);
  181. spin_lock_init(&dev->lock);
  182. init_waitqueue_head(&dev->waitq);
  183. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  184. dev->interface = interface;
  185. /* set up the endpoint information */
  186. iface_desc = interface->cur_altsetting;
  187. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  188. endpoint = &iface_desc->endpoint[i].desc;
  189. if (usb_endpoint_is_int_in(endpoint)) {
  190. dev->int_in_endpointAddr = endpoint->bEndpointAddress;
  191. break;
  192. }
  193. }
  194. if (!dev->int_in_endpointAddr) {
  195. retval = -ENODEV;
  196. err("Could not find endpoints");
  197. goto error;
  198. }
  199. /* allocate control URB */
  200. dev->cntl_urb = usb_alloc_urb(0, GFP_KERNEL);
  201. if (!dev->cntl_urb) {
  202. err("Could not allocate control URB");
  203. goto error;
  204. }
  205. /* allocate buffer for control req */
  206. dev->cntl_req = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
  207. GFP_KERNEL,
  208. &dev->cntl_urb->setup_dma);
  209. if (!dev->cntl_req) {
  210. err("Could not allocate cntl_req");
  211. goto error;
  212. }
  213. /* allocate buffer for control msg */
  214. dev->cntl_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
  215. GFP_KERNEL,
  216. &dev->cntl_urb->transfer_dma);
  217. if (!dev->cntl_buffer) {
  218. err("Could not allocate cntl_buffer");
  219. goto error;
  220. }
  221. /* configure control URB */
  222. dev->cntl_req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS |
  223. USB_RECIP_INTERFACE;
  224. dev->cntl_req->bRequest = HID_REQ_SET_REPORT;
  225. dev->cntl_req->wValue = cpu_to_le16((HID_OUTPUT_REPORT + 1) << 8);
  226. dev->cntl_req->wIndex = cpu_to_le16(iface_desc->desc.bInterfaceNumber);
  227. dev->cntl_req->wLength = cpu_to_le16(YUREX_BUF_SIZE);
  228. usb_fill_control_urb(dev->cntl_urb, dev->udev,
  229. usb_sndctrlpipe(dev->udev, 0),
  230. (void *)dev->cntl_req, dev->cntl_buffer,
  231. YUREX_BUF_SIZE, yurex_control_callback, dev);
  232. dev->cntl_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  233. /* allocate interrupt URB */
  234. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  235. if (!dev->urb) {
  236. err("Could not allocate URB");
  237. goto error;
  238. }
  239. /* allocate buffer for interrupt in */
  240. dev->int_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
  241. GFP_KERNEL, &dev->urb->transfer_dma);
  242. if (!dev->int_buffer) {
  243. err("Could not allocate int_buffer");
  244. goto error;
  245. }
  246. /* configure interrupt URB */
  247. usb_fill_int_urb(dev->urb, dev->udev,
  248. usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
  249. dev->int_buffer, YUREX_BUF_SIZE, yurex_interrupt,
  250. dev, 1);
  251. dev->cntl_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  252. if (usb_submit_urb(dev->urb, GFP_KERNEL)) {
  253. retval = -EIO;
  254. err("Could not submitting URB");
  255. goto error;
  256. }
  257. /* save our data pointer in this interface device */
  258. usb_set_intfdata(interface, dev);
  259. /* we can register the device now, as it is ready */
  260. retval = usb_register_dev(interface, &yurex_class);
  261. if (retval) {
  262. err("Not able to get a minor for this device.");
  263. usb_set_intfdata(interface, NULL);
  264. goto error;
  265. }
  266. dev->bbu = -1;
  267. dev_info(&interface->dev,
  268. "USB YUREX device now attached to Yurex #%d\n",
  269. interface->minor);
  270. return 0;
  271. error:
  272. if (dev)
  273. /* this frees allocated memory */
  274. kref_put(&dev->kref, yurex_delete);
  275. return retval;
  276. }
  277. static void yurex_disconnect(struct usb_interface *interface)
  278. {
  279. struct usb_yurex *dev;
  280. int minor = interface->minor;
  281. dev = usb_get_intfdata(interface);
  282. usb_set_intfdata(interface, NULL);
  283. /* give back our minor */
  284. usb_deregister_dev(interface, &yurex_class);
  285. /* prevent more I/O from starting */
  286. mutex_lock(&dev->io_mutex);
  287. dev->interface = NULL;
  288. mutex_unlock(&dev->io_mutex);
  289. /* wakeup waiters */
  290. kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
  291. wake_up_interruptible(&dev->waitq);
  292. /* decrement our usage count */
  293. kref_put(&dev->kref, yurex_delete);
  294. dev_info(&interface->dev, "USB YUREX #%d now disconnected\n", minor);
  295. }
  296. static struct usb_driver yurex_driver = {
  297. .name = "yurex",
  298. .probe = yurex_probe,
  299. .disconnect = yurex_disconnect,
  300. .id_table = yurex_table,
  301. };
  302. static int yurex_fasync(int fd, struct file *file, int on)
  303. {
  304. struct usb_yurex *dev;
  305. dev = (struct usb_yurex *)file->private_data;
  306. return fasync_helper(fd, file, on, &dev->async_queue);
  307. }
  308. static int yurex_open(struct inode *inode, struct file *file)
  309. {
  310. struct usb_yurex *dev;
  311. struct usb_interface *interface;
  312. int subminor;
  313. int retval = 0;
  314. subminor = iminor(inode);
  315. interface = usb_find_interface(&yurex_driver, subminor);
  316. if (!interface) {
  317. err("%s - error, can't find device for minor %d",
  318. __func__, subminor);
  319. retval = -ENODEV;
  320. goto exit;
  321. }
  322. dev = usb_get_intfdata(interface);
  323. if (!dev) {
  324. retval = -ENODEV;
  325. goto exit;
  326. }
  327. /* increment our usage count for the device */
  328. kref_get(&dev->kref);
  329. /* save our object in the file's private structure */
  330. mutex_lock(&dev->io_mutex);
  331. file->private_data = dev;
  332. mutex_unlock(&dev->io_mutex);
  333. exit:
  334. return retval;
  335. }
  336. static int yurex_release(struct inode *inode, struct file *file)
  337. {
  338. struct usb_yurex *dev;
  339. dev = (struct usb_yurex *)file->private_data;
  340. if (dev == NULL)
  341. return -ENODEV;
  342. yurex_fasync(-1, file, 0);
  343. /* decrement the count on our device */
  344. kref_put(&dev->kref, yurex_delete);
  345. return 0;
  346. }
  347. static ssize_t yurex_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
  348. {
  349. struct usb_yurex *dev;
  350. int retval = 0;
  351. int bytes_read = 0;
  352. char in_buffer[20];
  353. unsigned long flags;
  354. dev = (struct usb_yurex *)file->private_data;
  355. mutex_lock(&dev->io_mutex);
  356. if (!dev->interface) { /* already disconnected */
  357. retval = -ENODEV;
  358. goto exit;
  359. }
  360. spin_lock_irqsave(&dev->lock, flags);
  361. bytes_read = snprintf(in_buffer, 20, "%lld\n", dev->bbu);
  362. spin_unlock_irqrestore(&dev->lock, flags);
  363. if (*ppos < bytes_read) {
  364. if (copy_to_user(buffer, in_buffer + *ppos, bytes_read - *ppos))
  365. retval = -EFAULT;
  366. else {
  367. retval = bytes_read - *ppos;
  368. *ppos += bytes_read;
  369. }
  370. }
  371. exit:
  372. mutex_unlock(&dev->io_mutex);
  373. return retval;
  374. }
  375. static ssize_t yurex_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
  376. {
  377. struct usb_yurex *dev;
  378. int i, set = 0, retval = 0;
  379. char buffer[16];
  380. char *data = buffer;
  381. unsigned long long c, c2 = 0;
  382. signed long timeout = 0;
  383. DEFINE_WAIT(wait);
  384. count = min(sizeof(buffer), count);
  385. dev = (struct usb_yurex *)file->private_data;
  386. /* verify that we actually have some data to write */
  387. if (count == 0)
  388. goto error;
  389. mutex_lock(&dev->io_mutex);
  390. if (!dev->interface) { /* alreaday disconnected */
  391. mutex_unlock(&dev->io_mutex);
  392. retval = -ENODEV;
  393. goto error;
  394. }
  395. if (copy_from_user(buffer, user_buffer, count)) {
  396. mutex_unlock(&dev->io_mutex);
  397. retval = -EFAULT;
  398. goto error;
  399. }
  400. memset(dev->cntl_buffer, CMD_PADDING, YUREX_BUF_SIZE);
  401. switch (buffer[0]) {
  402. case CMD_ANIMATE:
  403. case CMD_LED:
  404. dev->cntl_buffer[0] = buffer[0];
  405. dev->cntl_buffer[1] = buffer[1];
  406. dev->cntl_buffer[2] = CMD_EOF;
  407. break;
  408. case CMD_READ:
  409. case CMD_VERSION:
  410. dev->cntl_buffer[0] = buffer[0];
  411. dev->cntl_buffer[1] = 0x00;
  412. dev->cntl_buffer[2] = CMD_EOF;
  413. break;
  414. case CMD_SET:
  415. data++;
  416. /* FALL THROUGH */
  417. case '0' ... '9':
  418. set = 1;
  419. c = c2 = simple_strtoull(data, NULL, 0);
  420. dev->cntl_buffer[0] = CMD_SET;
  421. for (i = 1; i < 6; i++) {
  422. dev->cntl_buffer[i] = (c>>32) & 0xff;
  423. c <<= 8;
  424. }
  425. buffer[6] = CMD_EOF;
  426. break;
  427. default:
  428. mutex_unlock(&dev->io_mutex);
  429. return -EINVAL;
  430. }
  431. /* send the data as the control msg */
  432. prepare_to_wait(&dev->waitq, &wait, TASK_INTERRUPTIBLE);
  433. dbg("%s - submit %c", __func__, dev->cntl_buffer[0]);
  434. retval = usb_submit_urb(dev->cntl_urb, GFP_KERNEL);
  435. if (retval >= 0)
  436. timeout = schedule_timeout(YUREX_WRITE_TIMEOUT);
  437. finish_wait(&dev->waitq, &wait);
  438. mutex_unlock(&dev->io_mutex);
  439. if (retval < 0) {
  440. err("%s - failed to send bulk msg, error %d", __func__, retval);
  441. goto error;
  442. }
  443. if (set && timeout)
  444. dev->bbu = c2;
  445. return timeout ? count : -EIO;
  446. error:
  447. return retval;
  448. }
  449. static const struct file_operations yurex_fops = {
  450. .owner = THIS_MODULE,
  451. .read = yurex_read,
  452. .write = yurex_write,
  453. .open = yurex_open,
  454. .release = yurex_release,
  455. .fasync = yurex_fasync,
  456. .llseek = default_llseek,
  457. };
  458. static int __init usb_yurex_init(void)
  459. {
  460. int result;
  461. /* register this driver with the USB subsystem */
  462. result = usb_register(&yurex_driver);
  463. if (result)
  464. err("usb_register failed. Error number %d", result);
  465. return result;
  466. }
  467. static void __exit usb_yurex_exit(void)
  468. {
  469. /* deregister this driver with the USB subsystem */
  470. usb_deregister(&yurex_driver);
  471. }
  472. module_init(usb_yurex_init);
  473. module_exit(usb_yurex_exit);
  474. MODULE_LICENSE("GPL");