yurex.c 13 KB

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