1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Meywa-Denki & KAYAC YUREX
5 * Copyright (C) 2010 Tomoki Sekiyama (tomoki.sekiyama@gmail.com)
8 #include <linux/kernel.h>
9 #include <linux/errno.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/kref.h>
13 #include <linux/mutex.h>
14 #include <linux/uaccess.h>
15 #include <linux/usb.h>
16 #include <linux/hid.h>
18 #define DRIVER_AUTHOR "Tomoki Sekiyama"
19 #define DRIVER_DESC "Driver for Meywa-Denki & KAYAC YUREX"
21 #define YUREX_VENDOR_ID 0x0c45
22 #define YUREX_PRODUCT_ID 0x1010
25 #define CMD_ANIMATE 'A'
30 #define CMD_VERSION 'V'
32 #define CMD_PADDING 0xff
34 #define YUREX_BUF_SIZE 8
35 #define YUREX_WRITE_TIMEOUT (HZ*2)
37 /* table of devices that work with this driver */
38 static struct usb_device_id yurex_table[] = {
39 { USB_DEVICE(YUREX_VENDOR_ID, YUREX_PRODUCT_ID) },
40 { } /* Terminating entry */
42 MODULE_DEVICE_TABLE(usb, yurex_table);
44 #ifdef CONFIG_USB_DYNAMIC_MINORS
45 #define YUREX_MINOR_BASE 0
47 #define YUREX_MINOR_BASE 192
50 /* Structure to hold all of our device specific stuff */
52 struct usb_device *udev;
53 struct usb_interface *interface;
54 __u8 int_in_endpointAddr;
55 struct urb *urb; /* URB for interrupt in */
56 unsigned char *int_buffer; /* buffer for intterupt in */
57 struct urb *cntl_urb; /* URB for control msg */
58 struct usb_ctrlrequest *cntl_req; /* req for control msg */
59 unsigned char *cntl_buffer; /* buffer for control msg */
62 struct mutex io_mutex;
63 unsigned long disconnected:1;
64 struct fasync_struct *async_queue;
65 wait_queue_head_t waitq;
68 __s64 bbu; /* BBU from device */
70 #define to_yurex_dev(d) container_of(d, struct usb_yurex, kref)
72 static struct usb_driver yurex_driver;
73 static const struct file_operations yurex_fops;
76 static void yurex_control_callback(struct urb *urb)
78 struct usb_yurex *dev = urb->context;
79 int status = urb->status;
82 dev_err(&urb->dev->dev, "%s - control failed: %d\n",
84 wake_up_interruptible(&dev->waitq);
87 /* on success, sender woken up by CMD_ACK int in, or timeout */
90 static void yurex_delete(struct kref *kref)
92 struct usb_yurex *dev = to_yurex_dev(kref);
94 dev_dbg(&dev->interface->dev, "%s\n", __func__);
97 usb_kill_urb(dev->cntl_urb);
99 usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
100 dev->cntl_buffer, dev->cntl_urb->transfer_dma);
101 usb_free_urb(dev->cntl_urb);
104 usb_kill_urb(dev->urb);
105 usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
106 dev->int_buffer, dev->urb->transfer_dma);
107 usb_free_urb(dev->urb);
109 usb_put_intf(dev->interface);
110 usb_put_dev(dev->udev);
115 * usb class driver info in order to get a minor number from the usb core,
116 * and to have the device registered with the driver core
118 static struct usb_class_driver yurex_class = {
121 .minor_base = YUREX_MINOR_BASE,
124 static void yurex_interrupt(struct urb *urb)
126 struct usb_yurex *dev = urb->context;
127 unsigned char *buf = dev->int_buffer;
128 int status = urb->status;
135 /* The device is terminated or messed up, give up */
137 dev_err(&dev->interface->dev,
138 "%s - overflow with length %d, actual length is %d\n",
139 __func__, YUREX_BUF_SIZE, dev->urb->actual_length);
148 dev_err(&dev->interface->dev,
149 "%s - unknown status received: %d\n", __func__, status);
153 /* handle received message */
157 if (buf[6] == CMD_EOF) {
158 spin_lock_irqsave(&dev->lock, flags);
160 for (i = 1; i < 6; i++) {
165 dev_dbg(&dev->interface->dev, "%s count: %lld\n",
167 spin_unlock_irqrestore(&dev->lock, flags);
169 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
172 dev_dbg(&dev->interface->dev,
173 "data format error - no EOF\n");
176 dev_dbg(&dev->interface->dev, "%s ack: %c\n",
178 wake_up_interruptible(&dev->waitq);
182 retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
184 dev_err(&dev->interface->dev, "%s - usb_submit_urb failed: %d\n",
189 static int yurex_probe(struct usb_interface *interface, const struct usb_device_id *id)
191 struct usb_yurex *dev;
192 struct usb_host_interface *iface_desc;
193 struct usb_endpoint_descriptor *endpoint;
194 int retval = -ENOMEM;
198 /* allocate memory for our device state and initialize it */
199 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
202 kref_init(&dev->kref);
203 mutex_init(&dev->io_mutex);
204 spin_lock_init(&dev->lock);
205 init_waitqueue_head(&dev->waitq);
207 dev->udev = usb_get_dev(interface_to_usbdev(interface));
208 dev->interface = usb_get_intf(interface);
210 /* set up the endpoint information */
211 iface_desc = interface->cur_altsetting;
212 res = usb_find_int_in_endpoint(iface_desc, &endpoint);
214 dev_err(&interface->dev, "Could not find endpoints\n");
219 dev->int_in_endpointAddr = endpoint->bEndpointAddress;
221 /* allocate control URB */
222 dev->cntl_urb = usb_alloc_urb(0, GFP_KERNEL);
226 /* allocate buffer for control req */
227 dev->cntl_req = kmalloc(YUREX_BUF_SIZE, GFP_KERNEL);
231 /* allocate buffer for control msg */
232 dev->cntl_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
234 &dev->cntl_urb->transfer_dma);
235 if (!dev->cntl_buffer) {
236 dev_err(&interface->dev, "Could not allocate cntl_buffer\n");
240 /* configure control URB */
241 dev->cntl_req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS |
243 dev->cntl_req->bRequest = HID_REQ_SET_REPORT;
244 dev->cntl_req->wValue = cpu_to_le16((HID_OUTPUT_REPORT + 1) << 8);
245 dev->cntl_req->wIndex = cpu_to_le16(iface_desc->desc.bInterfaceNumber);
246 dev->cntl_req->wLength = cpu_to_le16(YUREX_BUF_SIZE);
248 usb_fill_control_urb(dev->cntl_urb, dev->udev,
249 usb_sndctrlpipe(dev->udev, 0),
250 (void *)dev->cntl_req, dev->cntl_buffer,
251 YUREX_BUF_SIZE, yurex_control_callback, dev);
252 dev->cntl_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
255 /* allocate interrupt URB */
256 dev->urb = usb_alloc_urb(0, GFP_KERNEL);
260 /* allocate buffer for interrupt in */
261 dev->int_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
262 GFP_KERNEL, &dev->urb->transfer_dma);
263 if (!dev->int_buffer) {
264 dev_err(&interface->dev, "Could not allocate int_buffer\n");
268 /* configure interrupt URB */
269 usb_fill_int_urb(dev->urb, dev->udev,
270 usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
271 dev->int_buffer, YUREX_BUF_SIZE, yurex_interrupt,
273 dev->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
274 if (usb_submit_urb(dev->urb, GFP_KERNEL)) {
276 dev_err(&interface->dev, "Could not submitting URB\n");
280 /* save our data pointer in this interface device */
281 usb_set_intfdata(interface, dev);
284 /* we can register the device now, as it is ready */
285 retval = usb_register_dev(interface, &yurex_class);
287 dev_err(&interface->dev,
288 "Not able to get a minor for this device.\n");
289 usb_set_intfdata(interface, NULL);
293 dev_info(&interface->dev,
294 "USB YUREX device now attached to Yurex #%d\n",
301 /* this frees allocated memory */
302 kref_put(&dev->kref, yurex_delete);
306 static void yurex_disconnect(struct usb_interface *interface)
308 struct usb_yurex *dev;
309 int minor = interface->minor;
311 dev = usb_get_intfdata(interface);
312 usb_set_intfdata(interface, NULL);
314 /* give back our minor */
315 usb_deregister_dev(interface, &yurex_class);
317 /* prevent more I/O from starting */
318 usb_poison_urb(dev->urb);
319 usb_poison_urb(dev->cntl_urb);
320 mutex_lock(&dev->io_mutex);
321 dev->disconnected = 1;
322 mutex_unlock(&dev->io_mutex);
325 kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
326 wake_up_interruptible(&dev->waitq);
328 /* decrement our usage count */
329 kref_put(&dev->kref, yurex_delete);
331 dev_info(&interface->dev, "USB YUREX #%d now disconnected\n", minor);
334 static struct usb_driver yurex_driver = {
336 .probe = yurex_probe,
337 .disconnect = yurex_disconnect,
338 .id_table = yurex_table,
342 static int yurex_fasync(int fd, struct file *file, int on)
344 struct usb_yurex *dev;
346 dev = file->private_data;
347 return fasync_helper(fd, file, on, &dev->async_queue);
350 static int yurex_open(struct inode *inode, struct file *file)
352 struct usb_yurex *dev;
353 struct usb_interface *interface;
357 subminor = iminor(inode);
359 interface = usb_find_interface(&yurex_driver, subminor);
361 printk(KERN_ERR "%s - error, can't find device for minor %d",
367 dev = usb_get_intfdata(interface);
373 /* increment our usage count for the device */
374 kref_get(&dev->kref);
376 /* save our object in the file's private structure */
377 mutex_lock(&dev->io_mutex);
378 file->private_data = dev;
379 mutex_unlock(&dev->io_mutex);
385 static int yurex_release(struct inode *inode, struct file *file)
387 struct usb_yurex *dev;
389 dev = file->private_data;
393 /* decrement the count on our device */
394 kref_put(&dev->kref, yurex_delete);
398 static ssize_t yurex_read(struct file *file, char __user *buffer, size_t count,
401 struct usb_yurex *dev;
406 dev = file->private_data;
408 mutex_lock(&dev->io_mutex);
409 if (dev->disconnected) { /* already disconnected */
410 mutex_unlock(&dev->io_mutex);
414 spin_lock_irqsave(&dev->lock, flags);
415 len = snprintf(in_buffer, 20, "%lld\n", dev->bbu);
416 spin_unlock_irqrestore(&dev->lock, flags);
417 mutex_unlock(&dev->io_mutex);
419 if (WARN_ON_ONCE(len >= sizeof(in_buffer)))
422 return simple_read_from_buffer(buffer, count, ppos, in_buffer, len);
425 static ssize_t yurex_write(struct file *file, const char __user *user_buffer,
426 size_t count, loff_t *ppos)
428 struct usb_yurex *dev;
429 int i, set = 0, retval = 0;
432 unsigned long long c, c2 = 0;
433 signed long timeout = 0;
436 count = min(sizeof(buffer) - 1, count);
437 dev = file->private_data;
439 /* verify that we actually have some data to write */
443 mutex_lock(&dev->io_mutex);
444 if (dev->disconnected) { /* already disconnected */
445 mutex_unlock(&dev->io_mutex);
450 if (copy_from_user(buffer, user_buffer, count)) {
451 mutex_unlock(&dev->io_mutex);
456 memset(dev->cntl_buffer, CMD_PADDING, YUREX_BUF_SIZE);
461 dev->cntl_buffer[0] = buffer[0];
462 dev->cntl_buffer[1] = buffer[1];
463 dev->cntl_buffer[2] = CMD_EOF;
467 dev->cntl_buffer[0] = buffer[0];
468 dev->cntl_buffer[1] = 0x00;
469 dev->cntl_buffer[2] = CMD_EOF;
476 c = c2 = simple_strtoull(data, NULL, 0);
477 dev->cntl_buffer[0] = CMD_SET;
478 for (i = 1; i < 6; i++) {
479 dev->cntl_buffer[i] = (c>>32) & 0xff;
485 mutex_unlock(&dev->io_mutex);
489 /* send the data as the control msg */
490 prepare_to_wait(&dev->waitq, &wait, TASK_INTERRUPTIBLE);
491 dev_dbg(&dev->interface->dev, "%s - submit %c\n", __func__,
492 dev->cntl_buffer[0]);
493 retval = usb_submit_urb(dev->cntl_urb, GFP_ATOMIC);
495 timeout = schedule_timeout(YUREX_WRITE_TIMEOUT);
496 finish_wait(&dev->waitq, &wait);
498 /* make sure URB is idle after timeout or (spurious) CMD_ACK */
499 usb_kill_urb(dev->cntl_urb);
501 mutex_unlock(&dev->io_mutex);
504 dev_err(&dev->interface->dev,
505 "%s - failed to send bulk msg, error %d\n",
511 return timeout ? count : -EIO;
517 static const struct file_operations yurex_fops = {
518 .owner = THIS_MODULE,
520 .write = yurex_write,
522 .release = yurex_release,
523 .fasync = yurex_fasync,
524 .llseek = default_llseek,
527 module_usb_driver(yurex_driver);
529 MODULE_LICENSE("GPL");