yurex.c 13 KB

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