yurex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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. dbg("yurex_delete");
  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. dbg("%s count: %lld", __func__, dev->bbu);
  148. spin_unlock_irqrestore(&dev->lock, flags);
  149. kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
  150. }
  151. else
  152. dbg("data format error - no EOF");
  153. break;
  154. case CMD_ACK:
  155. dbg("%s ack: %c", __func__, buf[1]);
  156. wake_up_interruptible(&dev->waitq);
  157. break;
  158. }
  159. exit:
  160. retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
  161. if (retval) {
  162. dev_err(&dev->interface->dev, "%s - usb_submit_urb failed: %d\n",
  163. __func__, retval);
  164. }
  165. }
  166. static int yurex_probe(struct usb_interface *interface, const struct usb_device_id *id)
  167. {
  168. struct usb_yurex *dev;
  169. struct usb_host_interface *iface_desc;
  170. struct usb_endpoint_descriptor *endpoint;
  171. int retval = -ENOMEM;
  172. int i;
  173. DEFINE_WAIT(wait);
  174. /* allocate memory for our device state and initialize it */
  175. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  176. if (!dev) {
  177. dev_err(&interface->dev, "Out of memory\n");
  178. goto error;
  179. }
  180. kref_init(&dev->kref);
  181. mutex_init(&dev->io_mutex);
  182. spin_lock_init(&dev->lock);
  183. init_waitqueue_head(&dev->waitq);
  184. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  185. dev->interface = interface;
  186. /* set up the endpoint information */
  187. iface_desc = interface->cur_altsetting;
  188. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  189. endpoint = &iface_desc->endpoint[i].desc;
  190. if (usb_endpoint_is_int_in(endpoint)) {
  191. dev->int_in_endpointAddr = endpoint->bEndpointAddress;
  192. break;
  193. }
  194. }
  195. if (!dev->int_in_endpointAddr) {
  196. retval = -ENODEV;
  197. dev_err(&interface->dev, "Could not find endpoints\n");
  198. goto error;
  199. }
  200. /* allocate control URB */
  201. dev->cntl_urb = usb_alloc_urb(0, GFP_KERNEL);
  202. if (!dev->cntl_urb) {
  203. dev_err(&interface->dev, "Could not allocate control URB\n");
  204. goto error;
  205. }
  206. /* allocate buffer for control req */
  207. dev->cntl_req = kmalloc(YUREX_BUF_SIZE, GFP_KERNEL);
  208. if (!dev->cntl_req) {
  209. dev_err(&interface->dev, "Could not allocate cntl_req\n");
  210. goto error;
  211. }
  212. /* allocate buffer for control msg */
  213. dev->cntl_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
  214. GFP_KERNEL,
  215. &dev->cntl_urb->transfer_dma);
  216. if (!dev->cntl_buffer) {
  217. dev_err(&interface->dev, "Could not allocate cntl_buffer\n");
  218. goto error;
  219. }
  220. /* configure control URB */
  221. dev->cntl_req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS |
  222. USB_RECIP_INTERFACE;
  223. dev->cntl_req->bRequest = HID_REQ_SET_REPORT;
  224. dev->cntl_req->wValue = cpu_to_le16((HID_OUTPUT_REPORT + 1) << 8);
  225. dev->cntl_req->wIndex = cpu_to_le16(iface_desc->desc.bInterfaceNumber);
  226. dev->cntl_req->wLength = cpu_to_le16(YUREX_BUF_SIZE);
  227. usb_fill_control_urb(dev->cntl_urb, dev->udev,
  228. usb_sndctrlpipe(dev->udev, 0),
  229. (void *)dev->cntl_req, dev->cntl_buffer,
  230. YUREX_BUF_SIZE, yurex_control_callback, dev);
  231. dev->cntl_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  232. /* allocate interrupt URB */
  233. dev->urb = usb_alloc_urb(0, GFP_KERNEL);
  234. if (!dev->urb) {
  235. dev_err(&interface->dev, "Could not allocate URB\n");
  236. goto error;
  237. }
  238. /* allocate buffer for interrupt in */
  239. dev->int_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
  240. GFP_KERNEL, &dev->urb->transfer_dma);
  241. if (!dev->int_buffer) {
  242. dev_err(&interface->dev, "Could not allocate int_buffer\n");
  243. goto error;
  244. }
  245. /* configure interrupt URB */
  246. usb_fill_int_urb(dev->urb, dev->udev,
  247. usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
  248. dev->int_buffer, YUREX_BUF_SIZE, yurex_interrupt,
  249. dev, 1);
  250. dev->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  251. if (usb_submit_urb(dev->urb, GFP_KERNEL)) {
  252. retval = -EIO;
  253. dev_err(&interface->dev, "Could not submitting URB\n");
  254. goto error;
  255. }
  256. /* save our data pointer in this interface device */
  257. usb_set_intfdata(interface, dev);
  258. /* we can register the device now, as it is ready */
  259. retval = usb_register_dev(interface, &yurex_class);
  260. if (retval) {
  261. dev_err(&interface->dev,
  262. "Not able to get a minor for this device.\n");
  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. printk(KERN_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. dev_err(&dev->interface->dev,
  441. "%s - failed to send bulk msg, error %d\n",
  442. __func__, retval);
  443. goto error;
  444. }
  445. if (set && timeout)
  446. dev->bbu = c2;
  447. return timeout ? count : -EIO;
  448. error:
  449. return retval;
  450. }
  451. static const struct file_operations yurex_fops = {
  452. .owner = THIS_MODULE,
  453. .read = yurex_read,
  454. .write = yurex_write,
  455. .open = yurex_open,
  456. .release = yurex_release,
  457. .fasync = yurex_fasync,
  458. .llseek = default_llseek,
  459. };
  460. module_usb_driver(yurex_driver);
  461. MODULE_LICENSE("GPL");