yurex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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)
  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 224 /* not official yet */
  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->urb) {
  84. usb_kill_urb(dev->urb);
  85. if (dev->int_buffer)
  86. usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
  87. dev->int_buffer, dev->urb->transfer_dma);
  88. usb_free_urb(dev->urb);
  89. }
  90. kfree(dev);
  91. }
  92. /*
  93. * usb class driver info in order to get a minor number from the usb core,
  94. * and to have the device registered with the driver core
  95. */
  96. static struct usb_class_driver yurex_class = {
  97. .name = "yurex%d",
  98. .fops = &yurex_fops,
  99. .minor_base = YUREX_MINOR_BASE,
  100. };
  101. static void yurex_interrupt(struct urb *urb)
  102. {
  103. struct usb_yurex *dev = urb->context;
  104. unsigned char *buf = dev->int_buffer;
  105. int status = urb->status;
  106. unsigned long flags;
  107. int retval, i;
  108. switch (status) {
  109. case 0: /*success*/
  110. break;
  111. case -EOVERFLOW:
  112. err("%s - overflow with length %d, actual length is %d",
  113. __func__, YUREX_BUF_SIZE, dev->urb->actual_length);
  114. case -ECONNRESET:
  115. case -ENOENT:
  116. case -ESHUTDOWN:
  117. case -EILSEQ:
  118. /* The device is terminated, clean up */
  119. return;
  120. default:
  121. err("%s - unknown status received: %d", __func__, status);
  122. goto exit;
  123. }
  124. /* handle received message */
  125. switch (buf[0]) {
  126. case CMD_COUNT:
  127. case CMD_READ:
  128. if (buf[6] == CMD_EOF) {
  129. spin_lock_irqsave(&dev->lock, flags);
  130. dev->bbu = 0;
  131. for (i = 1; i < 6; i++) {
  132. dev->bbu += buf[i];
  133. if (i != 5)
  134. dev->bbu <<= 8;
  135. }
  136. dbg("%s count: %lld", __func__, dev->bbu);
  137. spin_unlock_irqrestore(&dev->lock, flags);
  138. kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
  139. }
  140. else
  141. dbg("data format error - no EOF");
  142. break;
  143. case CMD_ACK:
  144. dbg("%s ack: %c", __func__, buf[1]);
  145. wake_up_interruptible(&dev->waitq);
  146. break;
  147. }
  148. exit:
  149. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  150. if (retval) {
  151. err("%s - usb_submit_urb failed: %d",
  152. __func__, retval);
  153. }
  154. }
  155. static int yurex_probe(struct usb_interface *interface, const struct usb_device_id *id)
  156. {
  157. struct usb_yurex *dev;
  158. struct usb_host_interface *iface_desc;
  159. struct usb_endpoint_descriptor *endpoint;
  160. int retval = -ENOMEM;
  161. int i;
  162. DEFINE_WAIT(wait);
  163. /* allocate memory for our device state and initialize it */
  164. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  165. if (!dev) {
  166. err("Out of memory");
  167. goto error;
  168. }
  169. kref_init(&dev->kref);
  170. mutex_init(&dev->io_mutex);
  171. spin_lock_init(&dev->lock);
  172. init_waitqueue_head(&dev->waitq);
  173. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  174. dev->interface = interface;
  175. /* set up the endpoint information */
  176. iface_desc = interface->cur_altsetting;
  177. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  178. endpoint = &iface_desc->endpoint[i].desc;
  179. if (usb_endpoint_is_int_in(endpoint)) {
  180. dev->int_in_endpointAddr = endpoint->bEndpointAddress;
  181. break;
  182. }
  183. }
  184. if (!dev->int_in_endpointAddr) {
  185. retval = -ENODEV;
  186. err("Could not find endpoints");
  187. goto error;
  188. }
  189. /* allocate control URB */
  190. dev->cntl_urb = usb_alloc_urb(0, GFP_KERNEL);
  191. if (!dev->cntl_urb) {
  192. err("Could not allocate control URB");
  193. goto error;
  194. }
  195. /* allocate buffer for control req */
  196. dev->cntl_req = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
  197. GFP_KERNEL,
  198. &dev->cntl_urb->setup_dma);
  199. if (!dev->cntl_req) {
  200. err("Could not allocate cntl_req");
  201. goto error;
  202. }
  203. /* allocate buffer for control msg */
  204. dev->cntl_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
  205. GFP_KERNEL,
  206. &dev->cntl_urb->transfer_dma);
  207. if (!dev->cntl_buffer) {
  208. err("Could not allocate cntl_buffer");
  209. goto error;
  210. }
  211. /* configure control URB */
  212. dev->cntl_req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS |
  213. USB_RECIP_INTERFACE;
  214. dev->cntl_req->bRequest = HID_REQ_SET_REPORT;
  215. dev->cntl_req->wValue = cpu_to_le16((HID_OUTPUT_REPORT + 1) << 8);
  216. dev->cntl_req->wIndex = cpu_to_le16(iface_desc->desc.bInterfaceNumber);
  217. dev->cntl_req->wLength = cpu_to_le16(YUREX_BUF_SIZE);
  218. usb_fill_control_urb(dev->cntl_urb, dev->udev,
  219. usb_sndctrlpipe(dev->udev, 0),
  220. (void *)dev->cntl_req, dev->cntl_buffer,
  221. YUREX_BUF_SIZE, yurex_control_callback, dev);
  222. dev->cntl_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  223. /* allocate interrupt URB */
  224. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  225. if (!dev->urb) {
  226. err("Could not allocate URB");
  227. goto error;
  228. }
  229. /* allocate buffer for interrupt in */
  230. dev->int_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
  231. GFP_KERNEL, &dev->urb->transfer_dma);
  232. if (!dev->int_buffer) {
  233. err("Could not allocate int_buffer");
  234. goto error;
  235. }
  236. /* configure interrupt URB */
  237. usb_fill_int_urb(dev->urb, dev->udev,
  238. usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
  239. dev->int_buffer, YUREX_BUF_SIZE, yurex_interrupt,
  240. dev, 1);
  241. if (usb_submit_urb(dev->urb, GFP_KERNEL)) {
  242. retval = -EIO;
  243. err("Could not submitting URB");
  244. goto error;
  245. }
  246. /* save our data pointer in this interface device */
  247. usb_set_intfdata(interface, dev);
  248. /* we can register the device now, as it is ready */
  249. retval = usb_register_dev(interface, &yurex_class);
  250. if (retval) {
  251. err("Not able to get a minor for this device.");
  252. usb_set_intfdata(interface, NULL);
  253. goto error;
  254. }
  255. dev->bbu = -1;
  256. dev_info(&interface->dev,
  257. "USB Yurex device now attached to Yurex-%d¥n",
  258. interface->minor);
  259. return 0;
  260. error:
  261. if (dev)
  262. /* this frees allocated memory */
  263. kref_put(&dev->kref, yurex_delete);
  264. return retval;
  265. }
  266. static void yurex_disconnect(struct usb_interface *interface)
  267. {
  268. struct usb_yurex *dev;
  269. int minor = interface->minor;
  270. dev = usb_get_intfdata(interface);
  271. usb_set_intfdata(interface, NULL);
  272. /* give back our minor */
  273. usb_deregister_dev(interface, &yurex_class);
  274. /* prevent more I/O from starting */
  275. mutex_lock(&dev->io_mutex);
  276. dev->interface = NULL;
  277. mutex_unlock(&dev->io_mutex);
  278. /* wakeup waiters */
  279. kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
  280. wake_up_interruptible(&dev->waitq);
  281. /* decrement our usage count */
  282. kref_put(&dev->kref, yurex_delete);
  283. dev_info(&interface->dev, "USB Yurex #%d now disconnected", minor);
  284. }
  285. static struct usb_driver yurex_driver = {
  286. .name = "yurex",
  287. .probe = yurex_probe,
  288. .disconnect = yurex_disconnect,
  289. .id_table = yurex_table,
  290. };
  291. static int yurex_fasync(int fd, struct file *file, int on)
  292. {
  293. struct usb_yurex *dev;
  294. dev = (struct usb_yurex *)file->private_data;
  295. return fasync_helper(fd, file, on, &dev->async_queue);
  296. }
  297. static int yurex_open(struct inode *inode, struct file *file)
  298. {
  299. struct usb_yurex *dev;
  300. struct usb_interface *interface;
  301. int subminor;
  302. int retval = 0;
  303. subminor = iminor(inode);
  304. interface = usb_find_interface(&yurex_driver, subminor);
  305. if (!interface) {
  306. err("%s - error, can't find device for minor %d",
  307. __func__, subminor);
  308. retval = -ENODEV;
  309. goto exit;
  310. }
  311. dev = usb_get_intfdata(interface);
  312. if (!dev) {
  313. retval = -ENODEV;
  314. goto exit;
  315. }
  316. /* increment our usage count for the device */
  317. kref_get(&dev->kref);
  318. /* save our object in the file's private structure */
  319. mutex_lock(&dev->io_mutex);
  320. file->private_data = dev;
  321. mutex_unlock(&dev->io_mutex);
  322. exit:
  323. return retval;
  324. }
  325. static int yurex_release(struct inode *inode, struct file *file)
  326. {
  327. struct usb_yurex *dev;
  328. dev = (struct usb_yurex *)file->private_data;
  329. if (dev == NULL)
  330. return -ENODEV;
  331. yurex_fasync(-1, file, 0);
  332. /* decrement the count on our device */
  333. kref_put(&dev->kref, yurex_delete);
  334. return 0;
  335. }
  336. static ssize_t yurex_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
  337. {
  338. struct usb_yurex *dev;
  339. int retval = 0;
  340. int bytes_read = 0;
  341. char in_buffer[20];
  342. unsigned long flags;
  343. dev = (struct usb_yurex *)file->private_data;
  344. mutex_lock(&dev->io_mutex);
  345. if (!dev->interface) { /* already disconnected */
  346. retval = -ENODEV;
  347. goto exit;
  348. }
  349. spin_lock_irqsave(&dev->lock, flags);
  350. bytes_read = snprintf(in_buffer, 20, "%lld¥n", dev->bbu);
  351. spin_unlock_irqrestore(&dev->lock, flags);
  352. if (*ppos < bytes_read) {
  353. if (copy_to_user(buffer, in_buffer + *ppos, bytes_read - *ppos))
  354. retval = -EFAULT;
  355. else {
  356. retval = bytes_read - *ppos;
  357. *ppos += bytes_read;
  358. }
  359. }
  360. exit:
  361. mutex_unlock(&dev->io_mutex);
  362. return retval;
  363. }
  364. static ssize_t yurex_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
  365. {
  366. struct usb_yurex *dev;
  367. int i, set = 0, retval = 0;
  368. char buffer[16];
  369. char *data = buffer;
  370. unsigned long long c, c2 = 0;
  371. signed long timeout = 0;
  372. DEFINE_WAIT(wait);
  373. count = min(sizeof(buffer), count);
  374. dev = (struct usb_yurex *)file->private_data;
  375. /* verify that we actually have some data to write */
  376. if (count == 0)
  377. goto error;
  378. mutex_lock(&dev->io_mutex);
  379. if (!dev->interface) { /* alreaday disconnected */
  380. mutex_unlock(&dev->io_mutex);
  381. retval = -ENODEV;
  382. goto error;
  383. }
  384. if (copy_from_user(buffer, user_buffer, count)) {
  385. mutex_unlock(&dev->io_mutex);
  386. retval = -EFAULT;
  387. goto error;
  388. }
  389. memset(dev->cntl_buffer, CMD_PADDING, YUREX_BUF_SIZE);
  390. switch (buffer[0]) {
  391. case CMD_ANIMATE:
  392. case CMD_LED:
  393. dev->cntl_buffer[0] = buffer[0];
  394. dev->cntl_buffer[1] = buffer[1];
  395. dev->cntl_buffer[2] = CMD_EOF;
  396. break;
  397. case CMD_READ:
  398. case CMD_VERSION:
  399. dev->cntl_buffer[0] = buffer[0];
  400. dev->cntl_buffer[1] = 0x00;
  401. dev->cntl_buffer[2] = CMD_EOF;
  402. break;
  403. case CMD_SET:
  404. data++;
  405. /* FALL THROUGH */
  406. case '0' ... '9':
  407. set = 1;
  408. c = c2 = simple_strtoull(data, NULL, 0);
  409. dev->cntl_buffer[0] = CMD_SET;
  410. for (i = 1; i < 6; i++) {
  411. dev->cntl_buffer[i] = (c>>32) & 0xff;
  412. c <<= 8;
  413. }
  414. buffer[6] = CMD_EOF;
  415. break;
  416. default:
  417. mutex_unlock(&dev->io_mutex);
  418. return -EINVAL;
  419. }
  420. /* send the data as the control msg */
  421. prepare_to_wait(&dev->waitq, &wait, TASK_INTERRUPTIBLE);
  422. dbg("%s - submit %c", __func__, dev->cntl_buffer[0]);
  423. retval = usb_submit_urb(dev->cntl_urb, GFP_KERNEL);
  424. if (retval >= 0)
  425. timeout = schedule_timeout(YUREX_WRITE_TIMEOUT);
  426. finish_wait(&dev->waitq, &wait);
  427. mutex_unlock(&dev->io_mutex);
  428. if (retval < 0) {
  429. err("%s - failed to send bulk msg, error %d", __func__, retval);
  430. goto error;
  431. }
  432. if (set && timeout)
  433. dev->bbu = c2;
  434. return timeout ? count : -EIO;
  435. error:
  436. return retval;
  437. }
  438. static const struct file_operations yurex_fops = {
  439. .owner = THIS_MODULE,
  440. .read = yurex_read,
  441. .write = yurex_write,
  442. .open = yurex_open,
  443. .release = yurex_release,
  444. .fasync = yurex_fasync,
  445. };
  446. static int __init usb_yurex_init(void)
  447. {
  448. int result;
  449. /* register this driver with the USB subsystem */
  450. result = usb_register(&yurex_driver);
  451. if (result)
  452. err("usb_register failed. Error number %d", result);
  453. return result;
  454. }
  455. static void __exit usb_yurex_exit(void)
  456. {
  457. /* deregister this driver with the USB subsystem */
  458. usb_deregister(&yurex_driver);
  459. }
  460. module_init(usb_yurex_init);
  461. module_exit(usb_yurex_exit);
  462. MODULE_LICENSE("GPL");