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