yurex.c 13 KB

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