2 * Driver for Meywa-Denki & KAYAC YUREX
4 * Copyright (C) 2010 Tomoki Sekiyama (tomoki.sekiyama@gmail.com)
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.
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>
23 #define DRIVER_AUTHOR "Tomoki Sekiyama"
24 #define DRIVER_DESC "Driver for Meywa-Denki & KAYAC YUREX"
26 #define YUREX_VENDOR_ID 0x0c45
27 #define YUREX_PRODUCT_ID 0x1010
30 #define CMD_ANIMATE 'A'
35 #define CMD_VERSION 'V'
37 #define CMD_PADDING 0xff
39 #define YUREX_BUF_SIZE 8
40 #define YUREX_WRITE_TIMEOUT (HZ)
42 /* table of devices that work with this driver */
43 static struct usb_device_id yurex_table
[] = {
44 { USB_DEVICE(YUREX_VENDOR_ID
, YUREX_PRODUCT_ID
) },
45 { } /* Terminating entry */
47 MODULE_DEVICE_TABLE(usb
, yurex_table
);
49 #ifdef CONFIG_USB_DYNAMIC_MINORS
50 #define YUREX_MINOR_BASE 0
52 #define YUREX_MINOR_BASE 192
55 /* Structure to hold all of our device specific stuff */
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 */
67 struct mutex io_mutex
;
68 struct fasync_struct
*async_queue
;
69 wait_queue_head_t waitq
;
72 __s64 bbu
; /* BBU from device */
74 #define to_yurex_dev(d) container_of(d, struct usb_yurex, kref)
76 static struct usb_driver yurex_driver
;
77 static const struct file_operations yurex_fops
;
80 static void yurex_control_callback(struct urb
*urb
)
82 struct usb_yurex
*dev
= urb
->context
;
83 int status
= urb
->status
;
86 err("%s - control failed: %d¥n", __func__
, status
);
87 wake_up_interruptible(&dev
->waitq
);
90 /* on success, sender woken up by CMD_ACK int in, or timeout */
93 static void yurex_delete(struct kref
*kref
)
95 struct usb_yurex
*dev
= to_yurex_dev(kref
);
99 usb_put_dev(dev
->udev
);
101 usb_kill_urb(dev
->urb
);
103 usb_free_coherent(dev
->udev
, YUREX_BUF_SIZE
,
104 dev
->int_buffer
, dev
->urb
->transfer_dma
);
105 usb_free_urb(dev
->urb
);
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
114 static struct usb_class_driver yurex_class
= {
117 .minor_base
= YUREX_MINOR_BASE
,
120 static void yurex_interrupt(struct urb
*urb
)
122 struct usb_yurex
*dev
= urb
->context
;
123 unsigned char *buf
= dev
->int_buffer
;
124 int status
= urb
->status
;
132 err("%s - overflow with length %d, actual length is %d",
133 __func__
, YUREX_BUF_SIZE
, dev
->urb
->actual_length
);
138 /* The device is terminated, clean up */
141 err("%s - unknown status received: %d", __func__
, status
);
145 /* handle received message */
149 if (buf
[6] == CMD_EOF
) {
150 spin_lock_irqsave(&dev
->lock
, flags
);
152 for (i
= 1; i
< 6; i
++) {
157 dbg("%s count: %lld", __func__
, dev
->bbu
);
158 spin_unlock_irqrestore(&dev
->lock
, flags
);
160 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
163 dbg("data format error - no EOF");
166 dbg("%s ack: %c", __func__
, buf
[1]);
167 wake_up_interruptible(&dev
->waitq
);
172 retval
= usb_submit_urb(dev
->urb
, GFP_ATOMIC
);
174 err("%s - usb_submit_urb failed: %d",
179 static int yurex_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
181 struct usb_yurex
*dev
;
182 struct usb_host_interface
*iface_desc
;
183 struct usb_endpoint_descriptor
*endpoint
;
184 int retval
= -ENOMEM
;
188 /* allocate memory for our device state and initialize it */
189 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
191 err("Out of memory");
194 kref_init(&dev
->kref
);
195 mutex_init(&dev
->io_mutex
);
196 spin_lock_init(&dev
->lock
);
197 init_waitqueue_head(&dev
->waitq
);
199 dev
->udev
= usb_get_dev(interface_to_usbdev(interface
));
200 dev
->interface
= interface
;
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
;
207 if (usb_endpoint_is_int_in(endpoint
)) {
208 dev
->int_in_endpointAddr
= endpoint
->bEndpointAddress
;
212 if (!dev
->int_in_endpointAddr
) {
214 err("Could not find endpoints");
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");
226 /* allocate buffer for control req */
227 dev
->cntl_req
= usb_alloc_coherent(dev
->udev
, YUREX_BUF_SIZE
,
229 &dev
->cntl_urb
->setup_dma
);
230 if (!dev
->cntl_req
) {
231 err("Could not allocate cntl_req");
235 /* allocate buffer for control msg */
236 dev
->cntl_buffer
= usb_alloc_coherent(dev
->udev
, YUREX_BUF_SIZE
,
238 &dev
->cntl_urb
->transfer_dma
);
239 if (!dev
->cntl_buffer
) {
240 err("Could not allocate cntl_buffer");
244 /* configure control URB */
245 dev
->cntl_req
->bRequestType
= USB_DIR_OUT
| USB_TYPE_CLASS
|
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
);
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
;
259 /* allocate interrupt URB */
260 dev
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
262 err("Could not allocate URB");
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");
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
,
279 if (usb_submit_urb(dev
->urb
, GFP_KERNEL
)) {
281 err("Could not submitting URB");
285 /* save our data pointer in this interface device */
286 usb_set_intfdata(interface
, dev
);
288 /* we can register the device now, as it is ready */
289 retval
= usb_register_dev(interface
, &yurex_class
);
291 err("Not able to get a minor for this device.");
292 usb_set_intfdata(interface
, NULL
);
298 dev_info(&interface
->dev
,
299 "USB Yurex device now attached to Yurex-%d¥n",
306 /* this frees allocated memory */
307 kref_put(&dev
->kref
, yurex_delete
);
311 static void yurex_disconnect(struct usb_interface
*interface
)
313 struct usb_yurex
*dev
;
314 int minor
= interface
->minor
;
316 dev
= usb_get_intfdata(interface
);
317 usb_set_intfdata(interface
, NULL
);
319 /* give back our minor */
320 usb_deregister_dev(interface
, &yurex_class
);
322 /* prevent more I/O from starting */
323 mutex_lock(&dev
->io_mutex
);
324 dev
->interface
= NULL
;
325 mutex_unlock(&dev
->io_mutex
);
328 kill_fasync(&dev
->async_queue
, SIGIO
, POLL_IN
);
329 wake_up_interruptible(&dev
->waitq
);
331 /* decrement our usage count */
332 kref_put(&dev
->kref
, yurex_delete
);
334 dev_info(&interface
->dev
, "USB Yurex #%d now disconnected", minor
);
337 static struct usb_driver yurex_driver
= {
339 .probe
= yurex_probe
,
340 .disconnect
= yurex_disconnect
,
341 .id_table
= yurex_table
,
345 static int yurex_fasync(int fd
, struct file
*file
, int on
)
347 struct usb_yurex
*dev
;
349 dev
= (struct usb_yurex
*)file
->private_data
;
350 return fasync_helper(fd
, file
, on
, &dev
->async_queue
);
353 static int yurex_open(struct inode
*inode
, struct file
*file
)
355 struct usb_yurex
*dev
;
356 struct usb_interface
*interface
;
360 subminor
= iminor(inode
);
362 interface
= usb_find_interface(&yurex_driver
, subminor
);
364 err("%s - error, can't find device for minor %d",
370 dev
= usb_get_intfdata(interface
);
376 /* increment our usage count for the device */
377 kref_get(&dev
->kref
);
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
);
388 static int yurex_release(struct inode
*inode
, struct file
*file
)
390 struct usb_yurex
*dev
;
392 dev
= (struct usb_yurex
*)file
->private_data
;
396 yurex_fasync(-1, file
, 0);
398 /* decrement the count on our device */
399 kref_put(&dev
->kref
, yurex_delete
);
403 static ssize_t
yurex_read(struct file
*file
, char *buffer
, size_t count
, loff_t
*ppos
)
405 struct usb_yurex
*dev
;
411 dev
= (struct usb_yurex
*)file
->private_data
;
413 mutex_lock(&dev
->io_mutex
);
414 if (!dev
->interface
) { /* already disconnected */
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
);
423 if (*ppos
< bytes_read
) {
424 if (copy_to_user(buffer
, in_buffer
+ *ppos
, bytes_read
- *ppos
))
427 retval
= bytes_read
- *ppos
;
433 mutex_unlock(&dev
->io_mutex
);
437 static ssize_t
yurex_write(struct file
*file
, const char *user_buffer
, size_t count
, loff_t
*ppos
)
439 struct usb_yurex
*dev
;
440 int i
, set
= 0, retval
= 0;
443 unsigned long long c
, c2
= 0;
444 signed long timeout
= 0;
447 count
= min(sizeof(buffer
), count
);
448 dev
= (struct usb_yurex
*)file
->private_data
;
450 /* verify that we actually have some data to write */
454 mutex_lock(&dev
->io_mutex
);
455 if (!dev
->interface
) { /* alreaday disconnected */
456 mutex_unlock(&dev
->io_mutex
);
461 if (copy_from_user(buffer
, user_buffer
, count
)) {
462 mutex_unlock(&dev
->io_mutex
);
466 memset(dev
->cntl_buffer
, CMD_PADDING
, YUREX_BUF_SIZE
);
471 dev
->cntl_buffer
[0] = buffer
[0];
472 dev
->cntl_buffer
[1] = buffer
[1];
473 dev
->cntl_buffer
[2] = CMD_EOF
;
477 dev
->cntl_buffer
[0] = buffer
[0];
478 dev
->cntl_buffer
[1] = 0x00;
479 dev
->cntl_buffer
[2] = CMD_EOF
;
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;
495 mutex_unlock(&dev
->io_mutex
);
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
);
504 timeout
= schedule_timeout(YUREX_WRITE_TIMEOUT
);
505 finish_wait(&dev
->waitq
, &wait
);
507 mutex_unlock(&dev
->io_mutex
);
510 err("%s - failed to send bulk msg, error %d", __func__
, retval
);
515 return timeout
? count
: -EIO
;
521 static const struct file_operations yurex_fops
= {
522 .owner
= THIS_MODULE
,
524 .write
= yurex_write
,
526 .release
= yurex_release
,
527 .fasync
= yurex_fasync
,
531 static int __init
usb_yurex_init(void)
535 /* register this driver with the USB subsystem */
536 result
= usb_register(&yurex_driver
);
538 err("usb_register failed. Error number %d", result
);
543 static void __exit
usb_yurex_exit(void)
545 /* deregister this driver with the USB subsystem */
546 usb_deregister(&yurex_driver
);
549 module_init(usb_yurex_init
);
550 module_exit(usb_yurex_exit
);
552 MODULE_LICENSE("GPL");