yurex.c 13 KB

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