15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 26bc235a2STomoki Sekiyama /* 36bc235a2STomoki Sekiyama * Driver for Meywa-Denki & KAYAC YUREX 46bc235a2STomoki Sekiyama * 56bc235a2STomoki Sekiyama * Copyright (C) 2010 Tomoki Sekiyama (tomoki.sekiyama@gmail.com) 66bc235a2STomoki Sekiyama */ 76bc235a2STomoki Sekiyama 86bc235a2STomoki Sekiyama #include <linux/kernel.h> 96bc235a2STomoki Sekiyama #include <linux/errno.h> 106bc235a2STomoki Sekiyama #include <linux/slab.h> 116bc235a2STomoki Sekiyama #include <linux/module.h> 126bc235a2STomoki Sekiyama #include <linux/kref.h> 136bc235a2STomoki Sekiyama #include <linux/mutex.h> 146bc235a2STomoki Sekiyama #include <linux/uaccess.h> 156bc235a2STomoki Sekiyama #include <linux/usb.h> 166bc235a2STomoki Sekiyama #include <linux/hid.h> 176bc235a2STomoki Sekiyama 186bc235a2STomoki Sekiyama #define DRIVER_AUTHOR "Tomoki Sekiyama" 196bc235a2STomoki Sekiyama #define DRIVER_DESC "Driver for Meywa-Denki & KAYAC YUREX" 206bc235a2STomoki Sekiyama 216bc235a2STomoki Sekiyama #define YUREX_VENDOR_ID 0x0c45 226bc235a2STomoki Sekiyama #define YUREX_PRODUCT_ID 0x1010 236bc235a2STomoki Sekiyama 246bc235a2STomoki Sekiyama #define CMD_ACK '!' 256bc235a2STomoki Sekiyama #define CMD_ANIMATE 'A' 266bc235a2STomoki Sekiyama #define CMD_COUNT 'C' 276bc235a2STomoki Sekiyama #define CMD_LED 'L' 286bc235a2STomoki Sekiyama #define CMD_READ 'R' 296bc235a2STomoki Sekiyama #define CMD_SET 'S' 306bc235a2STomoki Sekiyama #define CMD_VERSION 'V' 316bc235a2STomoki Sekiyama #define CMD_EOF 0x0d 326bc235a2STomoki Sekiyama #define CMD_PADDING 0xff 336bc235a2STomoki Sekiyama 346bc235a2STomoki Sekiyama #define YUREX_BUF_SIZE 8 35e06ea97fSTomoki Sekiyama #define YUREX_WRITE_TIMEOUT (HZ*2) 366bc235a2STomoki Sekiyama 376bc235a2STomoki Sekiyama /* table of devices that work with this driver */ 386bc235a2STomoki Sekiyama static struct usb_device_id yurex_table[] = { 396bc235a2STomoki Sekiyama { USB_DEVICE(YUREX_VENDOR_ID, YUREX_PRODUCT_ID) }, 406bc235a2STomoki Sekiyama { } /* Terminating entry */ 416bc235a2STomoki Sekiyama }; 426bc235a2STomoki Sekiyama MODULE_DEVICE_TABLE(usb, yurex_table); 436bc235a2STomoki Sekiyama 446bc235a2STomoki Sekiyama #ifdef CONFIG_USB_DYNAMIC_MINORS 456bc235a2STomoki Sekiyama #define YUREX_MINOR_BASE 0 466bc235a2STomoki Sekiyama #else 471b62d258SGreg Kroah-Hartman #define YUREX_MINOR_BASE 192 486bc235a2STomoki Sekiyama #endif 496bc235a2STomoki Sekiyama 506bc235a2STomoki Sekiyama /* Structure to hold all of our device specific stuff */ 516bc235a2STomoki Sekiyama struct usb_yurex { 526bc235a2STomoki Sekiyama struct usb_device *udev; 536bc235a2STomoki Sekiyama struct usb_interface *interface; 546bc235a2STomoki Sekiyama __u8 int_in_endpointAddr; 556bc235a2STomoki Sekiyama struct urb *urb; /* URB for interrupt in */ 566bc235a2STomoki Sekiyama unsigned char *int_buffer; /* buffer for intterupt in */ 576bc235a2STomoki Sekiyama struct urb *cntl_urb; /* URB for control msg */ 586bc235a2STomoki Sekiyama struct usb_ctrlrequest *cntl_req; /* req for control msg */ 596bc235a2STomoki Sekiyama unsigned char *cntl_buffer; /* buffer for control msg */ 606bc235a2STomoki Sekiyama 616bc235a2STomoki Sekiyama struct kref kref; 626bc235a2STomoki Sekiyama struct mutex io_mutex; 63aafb00a9SJohan Hovold unsigned long disconnected:1; 646bc235a2STomoki Sekiyama struct fasync_struct *async_queue; 656bc235a2STomoki Sekiyama wait_queue_head_t waitq; 666bc235a2STomoki Sekiyama 676bc235a2STomoki Sekiyama spinlock_t lock; 686bc235a2STomoki Sekiyama __s64 bbu; /* BBU from device */ 696bc235a2STomoki Sekiyama }; 706bc235a2STomoki Sekiyama #define to_yurex_dev(d) container_of(d, struct usb_yurex, kref) 716bc235a2STomoki Sekiyama 726bc235a2STomoki Sekiyama static struct usb_driver yurex_driver; 736bc235a2STomoki Sekiyama static const struct file_operations yurex_fops; 746bc235a2STomoki Sekiyama 756bc235a2STomoki Sekiyama 766bc235a2STomoki Sekiyama static void yurex_control_callback(struct urb *urb) 776bc235a2STomoki Sekiyama { 786bc235a2STomoki Sekiyama struct usb_yurex *dev = urb->context; 796bc235a2STomoki Sekiyama int status = urb->status; 806bc235a2STomoki Sekiyama 816bc235a2STomoki Sekiyama if (status) { 8245714104SGreg Kroah-Hartman dev_err(&urb->dev->dev, "%s - control failed: %d\n", 8345714104SGreg Kroah-Hartman __func__, status); 846bc235a2STomoki Sekiyama wake_up_interruptible(&dev->waitq); 856bc235a2STomoki Sekiyama return; 866bc235a2STomoki Sekiyama } 876bc235a2STomoki Sekiyama /* on success, sender woken up by CMD_ACK int in, or timeout */ 886bc235a2STomoki Sekiyama } 896bc235a2STomoki Sekiyama 906bc235a2STomoki Sekiyama static void yurex_delete(struct kref *kref) 916bc235a2STomoki Sekiyama { 926bc235a2STomoki Sekiyama struct usb_yurex *dev = to_yurex_dev(kref); 936bc235a2STomoki Sekiyama 94aadd6472SGreg Kroah-Hartman dev_dbg(&dev->interface->dev, "%s\n", __func__); 956bc235a2STomoki Sekiyama 96e06ea97fSTomoki Sekiyama if (dev->cntl_urb) { 97e06ea97fSTomoki Sekiyama usb_kill_urb(dev->cntl_urb); 98523fc5c1STomoki Sekiyama kfree(dev->cntl_req); 99e06ea97fSTomoki Sekiyama usb_free_coherent(dev->udev, YUREX_BUF_SIZE, 100e06ea97fSTomoki Sekiyama dev->cntl_buffer, dev->cntl_urb->transfer_dma); 101e06ea97fSTomoki Sekiyama usb_free_urb(dev->cntl_urb); 102e06ea97fSTomoki Sekiyama } 1036bc235a2STomoki Sekiyama if (dev->urb) { 1046bc235a2STomoki Sekiyama usb_kill_urb(dev->urb); 1056bc235a2STomoki Sekiyama usb_free_coherent(dev->udev, YUREX_BUF_SIZE, 1066bc235a2STomoki Sekiyama dev->int_buffer, dev->urb->transfer_dma); 1076bc235a2STomoki Sekiyama usb_free_urb(dev->urb); 1086bc235a2STomoki Sekiyama } 109aafb00a9SJohan Hovold usb_put_intf(dev->interface); 110fc05481bSSuzuki K Poulose usb_put_dev(dev->udev); 1116bc235a2STomoki Sekiyama kfree(dev); 1126bc235a2STomoki Sekiyama } 1136bc235a2STomoki Sekiyama 1146bc235a2STomoki Sekiyama /* 1156bc235a2STomoki Sekiyama * usb class driver info in order to get a minor number from the usb core, 1166bc235a2STomoki Sekiyama * and to have the device registered with the driver core 1176bc235a2STomoki Sekiyama */ 1186bc235a2STomoki Sekiyama static struct usb_class_driver yurex_class = { 1196bc235a2STomoki Sekiyama .name = "yurex%d", 1206bc235a2STomoki Sekiyama .fops = &yurex_fops, 1216bc235a2STomoki Sekiyama .minor_base = YUREX_MINOR_BASE, 1226bc235a2STomoki Sekiyama }; 1236bc235a2STomoki Sekiyama 1246bc235a2STomoki Sekiyama static void yurex_interrupt(struct urb *urb) 1256bc235a2STomoki Sekiyama { 1266bc235a2STomoki Sekiyama struct usb_yurex *dev = urb->context; 1276bc235a2STomoki Sekiyama unsigned char *buf = dev->int_buffer; 1286bc235a2STomoki Sekiyama int status = urb->status; 1296bc235a2STomoki Sekiyama unsigned long flags; 1306bc235a2STomoki Sekiyama int retval, i; 1316bc235a2STomoki Sekiyama 1326bc235a2STomoki Sekiyama switch (status) { 1336bc235a2STomoki Sekiyama case 0: /*success*/ 1346bc235a2STomoki Sekiyama break; 13532a0721cSAlan Stern /* The device is terminated or messed up, give up */ 1366bc235a2STomoki Sekiyama case -EOVERFLOW: 13745714104SGreg Kroah-Hartman dev_err(&dev->interface->dev, 13845714104SGreg Kroah-Hartman "%s - overflow with length %d, actual length is %d\n", 1396bc235a2STomoki Sekiyama __func__, YUREX_BUF_SIZE, dev->urb->actual_length); 140*93c747edSGustavo A. R. Silva return; 1416bc235a2STomoki Sekiyama case -ECONNRESET: 1426bc235a2STomoki Sekiyama case -ENOENT: 1436bc235a2STomoki Sekiyama case -ESHUTDOWN: 1446bc235a2STomoki Sekiyama case -EILSEQ: 14532a0721cSAlan Stern case -EPROTO: 14632a0721cSAlan Stern case -ETIME: 1476bc235a2STomoki Sekiyama return; 1486bc235a2STomoki Sekiyama default: 14945714104SGreg Kroah-Hartman dev_err(&dev->interface->dev, 15045714104SGreg Kroah-Hartman "%s - unknown status received: %d\n", __func__, status); 15132a0721cSAlan Stern return; 1526bc235a2STomoki Sekiyama } 1536bc235a2STomoki Sekiyama 1546bc235a2STomoki Sekiyama /* handle received message */ 1556bc235a2STomoki Sekiyama switch (buf[0]) { 1566bc235a2STomoki Sekiyama case CMD_COUNT: 1576bc235a2STomoki Sekiyama case CMD_READ: 1586bc235a2STomoki Sekiyama if (buf[6] == CMD_EOF) { 1596bc235a2STomoki Sekiyama spin_lock_irqsave(&dev->lock, flags); 1606bc235a2STomoki Sekiyama dev->bbu = 0; 1616bc235a2STomoki Sekiyama for (i = 1; i < 6; i++) { 1626bc235a2STomoki Sekiyama dev->bbu += buf[i]; 1636bc235a2STomoki Sekiyama if (i != 5) 1646bc235a2STomoki Sekiyama dev->bbu <<= 8; 1656bc235a2STomoki Sekiyama } 166aadd6472SGreg Kroah-Hartman dev_dbg(&dev->interface->dev, "%s count: %lld\n", 167aadd6472SGreg Kroah-Hartman __func__, dev->bbu); 1686bc235a2STomoki Sekiyama spin_unlock_irqrestore(&dev->lock, flags); 1696bc235a2STomoki Sekiyama 1706bc235a2STomoki Sekiyama kill_fasync(&dev->async_queue, SIGIO, POLL_IN); 1716bc235a2STomoki Sekiyama } 1726bc235a2STomoki Sekiyama else 173aadd6472SGreg Kroah-Hartman dev_dbg(&dev->interface->dev, 174aadd6472SGreg Kroah-Hartman "data format error - no EOF\n"); 1756bc235a2STomoki Sekiyama break; 1766bc235a2STomoki Sekiyama case CMD_ACK: 177aadd6472SGreg Kroah-Hartman dev_dbg(&dev->interface->dev, "%s ack: %c\n", 178aadd6472SGreg Kroah-Hartman __func__, buf[1]); 1796bc235a2STomoki Sekiyama wake_up_interruptible(&dev->waitq); 1806bc235a2STomoki Sekiyama break; 1816bc235a2STomoki Sekiyama } 1826bc235a2STomoki Sekiyama 1836bc235a2STomoki Sekiyama retval = usb_submit_urb(dev->urb, GFP_ATOMIC); 1846bc235a2STomoki Sekiyama if (retval) { 18545714104SGreg Kroah-Hartman dev_err(&dev->interface->dev, "%s - usb_submit_urb failed: %d\n", 1866bc235a2STomoki Sekiyama __func__, retval); 1876bc235a2STomoki Sekiyama } 1886bc235a2STomoki Sekiyama } 1896bc235a2STomoki Sekiyama 1906bc235a2STomoki Sekiyama static int yurex_probe(struct usb_interface *interface, const struct usb_device_id *id) 1916bc235a2STomoki Sekiyama { 1926bc235a2STomoki Sekiyama struct usb_yurex *dev; 1936bc235a2STomoki Sekiyama struct usb_host_interface *iface_desc; 1946bc235a2STomoki Sekiyama struct usb_endpoint_descriptor *endpoint; 1956bc235a2STomoki Sekiyama int retval = -ENOMEM; 1966bc235a2STomoki Sekiyama DEFINE_WAIT(wait); 197499841e6SJohan Hovold int res; 1986bc235a2STomoki Sekiyama 1996bc235a2STomoki Sekiyama /* allocate memory for our device state and initialize it */ 2006bc235a2STomoki Sekiyama dev = kzalloc(sizeof(*dev), GFP_KERNEL); 2010c2bc5c2SWolfram Sang if (!dev) 2026bc235a2STomoki Sekiyama goto error; 2036bc235a2STomoki Sekiyama kref_init(&dev->kref); 2046bc235a2STomoki Sekiyama mutex_init(&dev->io_mutex); 2056bc235a2STomoki Sekiyama spin_lock_init(&dev->lock); 2066bc235a2STomoki Sekiyama init_waitqueue_head(&dev->waitq); 2076bc235a2STomoki Sekiyama 2086bc235a2STomoki Sekiyama dev->udev = usb_get_dev(interface_to_usbdev(interface)); 209aafb00a9SJohan Hovold dev->interface = usb_get_intf(interface); 2106bc235a2STomoki Sekiyama 2116bc235a2STomoki Sekiyama /* set up the endpoint information */ 2126bc235a2STomoki Sekiyama iface_desc = interface->cur_altsetting; 213499841e6SJohan Hovold res = usb_find_int_in_endpoint(iface_desc, &endpoint); 214499841e6SJohan Hovold if (res) { 21545714104SGreg Kroah-Hartman dev_err(&interface->dev, "Could not find endpoints\n"); 216499841e6SJohan Hovold retval = res; 2176bc235a2STomoki Sekiyama goto error; 2186bc235a2STomoki Sekiyama } 2196bc235a2STomoki Sekiyama 220499841e6SJohan Hovold dev->int_in_endpointAddr = endpoint->bEndpointAddress; 2216bc235a2STomoki Sekiyama 2226bc235a2STomoki Sekiyama /* allocate control URB */ 2236bc235a2STomoki Sekiyama dev->cntl_urb = usb_alloc_urb(0, GFP_KERNEL); 2240450ba40SWolfram Sang if (!dev->cntl_urb) 2256bc235a2STomoki Sekiyama goto error; 2266bc235a2STomoki Sekiyama 2276bc235a2STomoki Sekiyama /* allocate buffer for control req */ 228523fc5c1STomoki Sekiyama dev->cntl_req = kmalloc(YUREX_BUF_SIZE, GFP_KERNEL); 2290c2bc5c2SWolfram Sang if (!dev->cntl_req) 2306bc235a2STomoki Sekiyama goto error; 2316bc235a2STomoki Sekiyama 2326bc235a2STomoki Sekiyama /* allocate buffer for control msg */ 2336bc235a2STomoki Sekiyama dev->cntl_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE, 2346bc235a2STomoki Sekiyama GFP_KERNEL, 2356bc235a2STomoki Sekiyama &dev->cntl_urb->transfer_dma); 2366bc235a2STomoki Sekiyama if (!dev->cntl_buffer) { 23745714104SGreg Kroah-Hartman dev_err(&interface->dev, "Could not allocate cntl_buffer\n"); 2386bc235a2STomoki Sekiyama goto error; 2396bc235a2STomoki Sekiyama } 2406bc235a2STomoki Sekiyama 2416bc235a2STomoki Sekiyama /* configure control URB */ 2426bc235a2STomoki Sekiyama dev->cntl_req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS | 2436bc235a2STomoki Sekiyama USB_RECIP_INTERFACE; 2446bc235a2STomoki Sekiyama dev->cntl_req->bRequest = HID_REQ_SET_REPORT; 2456bc235a2STomoki Sekiyama dev->cntl_req->wValue = cpu_to_le16((HID_OUTPUT_REPORT + 1) << 8); 2466bc235a2STomoki Sekiyama dev->cntl_req->wIndex = cpu_to_le16(iface_desc->desc.bInterfaceNumber); 2476bc235a2STomoki Sekiyama dev->cntl_req->wLength = cpu_to_le16(YUREX_BUF_SIZE); 2486bc235a2STomoki Sekiyama 2496bc235a2STomoki Sekiyama usb_fill_control_urb(dev->cntl_urb, dev->udev, 2506bc235a2STomoki Sekiyama usb_sndctrlpipe(dev->udev, 0), 2516bc235a2STomoki Sekiyama (void *)dev->cntl_req, dev->cntl_buffer, 2526bc235a2STomoki Sekiyama YUREX_BUF_SIZE, yurex_control_callback, dev); 2536bc235a2STomoki Sekiyama dev->cntl_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 2546bc235a2STomoki Sekiyama 2556bc235a2STomoki Sekiyama 2566bc235a2STomoki Sekiyama /* allocate interrupt URB */ 2576bc235a2STomoki Sekiyama dev->urb = usb_alloc_urb(0, GFP_KERNEL); 2580450ba40SWolfram Sang if (!dev->urb) 2596bc235a2STomoki Sekiyama goto error; 2606bc235a2STomoki Sekiyama 2616bc235a2STomoki Sekiyama /* allocate buffer for interrupt in */ 2626bc235a2STomoki Sekiyama dev->int_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE, 2636bc235a2STomoki Sekiyama GFP_KERNEL, &dev->urb->transfer_dma); 2646bc235a2STomoki Sekiyama if (!dev->int_buffer) { 26545714104SGreg Kroah-Hartman dev_err(&interface->dev, "Could not allocate int_buffer\n"); 2666bc235a2STomoki Sekiyama goto error; 2676bc235a2STomoki Sekiyama } 2686bc235a2STomoki Sekiyama 2696bc235a2STomoki Sekiyama /* configure interrupt URB */ 2706bc235a2STomoki Sekiyama usb_fill_int_urb(dev->urb, dev->udev, 2716bc235a2STomoki Sekiyama usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr), 2726bc235a2STomoki Sekiyama dev->int_buffer, YUREX_BUF_SIZE, yurex_interrupt, 2736bc235a2STomoki Sekiyama dev, 1); 274532f17b5STomoki Sekiyama dev->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 2756bc235a2STomoki Sekiyama if (usb_submit_urb(dev->urb, GFP_KERNEL)) { 2766bc235a2STomoki Sekiyama retval = -EIO; 27745714104SGreg Kroah-Hartman dev_err(&interface->dev, "Could not submitting URB\n"); 2786bc235a2STomoki Sekiyama goto error; 2796bc235a2STomoki Sekiyama } 2806bc235a2STomoki Sekiyama 2816bc235a2STomoki Sekiyama /* save our data pointer in this interface device */ 2826bc235a2STomoki Sekiyama usb_set_intfdata(interface, dev); 283c78d1ecfSOliver Neukum dev->bbu = -1; 2846bc235a2STomoki Sekiyama 2856bc235a2STomoki Sekiyama /* we can register the device now, as it is ready */ 2866bc235a2STomoki Sekiyama retval = usb_register_dev(interface, &yurex_class); 2876bc235a2STomoki Sekiyama if (retval) { 28845714104SGreg Kroah-Hartman dev_err(&interface->dev, 28945714104SGreg Kroah-Hartman "Not able to get a minor for this device.\n"); 2906bc235a2STomoki Sekiyama usb_set_intfdata(interface, NULL); 2916bc235a2STomoki Sekiyama goto error; 2926bc235a2STomoki Sekiyama } 2936bc235a2STomoki Sekiyama 2946bc235a2STomoki Sekiyama dev_info(&interface->dev, 295e06ea97fSTomoki Sekiyama "USB YUREX device now attached to Yurex #%d\n", 2966bc235a2STomoki Sekiyama interface->minor); 2976bc235a2STomoki Sekiyama 2986bc235a2STomoki Sekiyama return 0; 2996bc235a2STomoki Sekiyama 3006bc235a2STomoki Sekiyama error: 3016bc235a2STomoki Sekiyama if (dev) 3026bc235a2STomoki Sekiyama /* this frees allocated memory */ 3036bc235a2STomoki Sekiyama kref_put(&dev->kref, yurex_delete); 3046bc235a2STomoki Sekiyama return retval; 3056bc235a2STomoki Sekiyama } 3066bc235a2STomoki Sekiyama 3076bc235a2STomoki Sekiyama static void yurex_disconnect(struct usb_interface *interface) 3086bc235a2STomoki Sekiyama { 3096bc235a2STomoki Sekiyama struct usb_yurex *dev; 3106bc235a2STomoki Sekiyama int minor = interface->minor; 3116bc235a2STomoki Sekiyama 3126bc235a2STomoki Sekiyama dev = usb_get_intfdata(interface); 3136bc235a2STomoki Sekiyama usb_set_intfdata(interface, NULL); 3146bc235a2STomoki Sekiyama 3156bc235a2STomoki Sekiyama /* give back our minor */ 3166bc235a2STomoki Sekiyama usb_deregister_dev(interface, &yurex_class); 3176bc235a2STomoki Sekiyama 3186bc235a2STomoki Sekiyama /* prevent more I/O from starting */ 319ef61eb43SAlan Stern usb_poison_urb(dev->urb); 320aafb00a9SJohan Hovold usb_poison_urb(dev->cntl_urb); 3216bc235a2STomoki Sekiyama mutex_lock(&dev->io_mutex); 322aafb00a9SJohan Hovold dev->disconnected = 1; 3236bc235a2STomoki Sekiyama mutex_unlock(&dev->io_mutex); 3246bc235a2STomoki Sekiyama 3256bc235a2STomoki Sekiyama /* wakeup waiters */ 3266bc235a2STomoki Sekiyama kill_fasync(&dev->async_queue, SIGIO, POLL_IN); 3276bc235a2STomoki Sekiyama wake_up_interruptible(&dev->waitq); 3286bc235a2STomoki Sekiyama 3296bc235a2STomoki Sekiyama /* decrement our usage count */ 3306bc235a2STomoki Sekiyama kref_put(&dev->kref, yurex_delete); 3316bc235a2STomoki Sekiyama 332e06ea97fSTomoki Sekiyama dev_info(&interface->dev, "USB YUREX #%d now disconnected\n", minor); 3336bc235a2STomoki Sekiyama } 3346bc235a2STomoki Sekiyama 3356bc235a2STomoki Sekiyama static struct usb_driver yurex_driver = { 3366bc235a2STomoki Sekiyama .name = "yurex", 3376bc235a2STomoki Sekiyama .probe = yurex_probe, 3386bc235a2STomoki Sekiyama .disconnect = yurex_disconnect, 3396bc235a2STomoki Sekiyama .id_table = yurex_table, 3406bc235a2STomoki Sekiyama }; 3416bc235a2STomoki Sekiyama 3426bc235a2STomoki Sekiyama 3436bc235a2STomoki Sekiyama static int yurex_fasync(int fd, struct file *file, int on) 3446bc235a2STomoki Sekiyama { 3456bc235a2STomoki Sekiyama struct usb_yurex *dev; 3466bc235a2STomoki Sekiyama 347113ad911SArjun Sreedharan dev = file->private_data; 3486bc235a2STomoki Sekiyama return fasync_helper(fd, file, on, &dev->async_queue); 3496bc235a2STomoki Sekiyama } 3506bc235a2STomoki Sekiyama 3516bc235a2STomoki Sekiyama static int yurex_open(struct inode *inode, struct file *file) 3526bc235a2STomoki Sekiyama { 3536bc235a2STomoki Sekiyama struct usb_yurex *dev; 3546bc235a2STomoki Sekiyama struct usb_interface *interface; 3556bc235a2STomoki Sekiyama int subminor; 3566bc235a2STomoki Sekiyama int retval = 0; 3576bc235a2STomoki Sekiyama 3586bc235a2STomoki Sekiyama subminor = iminor(inode); 3596bc235a2STomoki Sekiyama 3606bc235a2STomoki Sekiyama interface = usb_find_interface(&yurex_driver, subminor); 3616bc235a2STomoki Sekiyama if (!interface) { 36245714104SGreg Kroah-Hartman printk(KERN_ERR "%s - error, can't find device for minor %d", 3636bc235a2STomoki Sekiyama __func__, subminor); 3646bc235a2STomoki Sekiyama retval = -ENODEV; 3656bc235a2STomoki Sekiyama goto exit; 3666bc235a2STomoki Sekiyama } 3676bc235a2STomoki Sekiyama 3686bc235a2STomoki Sekiyama dev = usb_get_intfdata(interface); 3696bc235a2STomoki Sekiyama if (!dev) { 3706bc235a2STomoki Sekiyama retval = -ENODEV; 3716bc235a2STomoki Sekiyama goto exit; 3726bc235a2STomoki Sekiyama } 3736bc235a2STomoki Sekiyama 3746bc235a2STomoki Sekiyama /* increment our usage count for the device */ 3756bc235a2STomoki Sekiyama kref_get(&dev->kref); 3766bc235a2STomoki Sekiyama 3776bc235a2STomoki Sekiyama /* save our object in the file's private structure */ 3786bc235a2STomoki Sekiyama mutex_lock(&dev->io_mutex); 3796bc235a2STomoki Sekiyama file->private_data = dev; 3806bc235a2STomoki Sekiyama mutex_unlock(&dev->io_mutex); 3816bc235a2STomoki Sekiyama 3826bc235a2STomoki Sekiyama exit: 3836bc235a2STomoki Sekiyama return retval; 3846bc235a2STomoki Sekiyama } 3856bc235a2STomoki Sekiyama 3866bc235a2STomoki Sekiyama static int yurex_release(struct inode *inode, struct file *file) 3876bc235a2STomoki Sekiyama { 3886bc235a2STomoki Sekiyama struct usb_yurex *dev; 3896bc235a2STomoki Sekiyama 390113ad911SArjun Sreedharan dev = file->private_data; 3916bc235a2STomoki Sekiyama if (dev == NULL) 3926bc235a2STomoki Sekiyama return -ENODEV; 3936bc235a2STomoki Sekiyama 3946bc235a2STomoki Sekiyama /* decrement the count on our device */ 3956bc235a2STomoki Sekiyama kref_put(&dev->kref, yurex_delete); 3966bc235a2STomoki Sekiyama return 0; 3976bc235a2STomoki Sekiyama } 3986bc235a2STomoki Sekiyama 3991cc373c6SSudip Mukherjee static ssize_t yurex_read(struct file *file, char __user *buffer, size_t count, 4001cc373c6SSudip Mukherjee loff_t *ppos) 4016bc235a2STomoki Sekiyama { 4026bc235a2STomoki Sekiyama struct usb_yurex *dev; 403f1e255d6SJann Horn int len = 0; 4046bc235a2STomoki Sekiyama char in_buffer[20]; 4056bc235a2STomoki Sekiyama unsigned long flags; 4066bc235a2STomoki Sekiyama 407113ad911SArjun Sreedharan dev = file->private_data; 4086bc235a2STomoki Sekiyama 4096bc235a2STomoki Sekiyama mutex_lock(&dev->io_mutex); 410aafb00a9SJohan Hovold if (dev->disconnected) { /* already disconnected */ 411f1e255d6SJann Horn mutex_unlock(&dev->io_mutex); 412f1e255d6SJann Horn return -ENODEV; 4136bc235a2STomoki Sekiyama } 4146bc235a2STomoki Sekiyama 4156bc235a2STomoki Sekiyama spin_lock_irqsave(&dev->lock, flags); 416f1e255d6SJann Horn len = snprintf(in_buffer, 20, "%lld\n", dev->bbu); 4176bc235a2STomoki Sekiyama spin_unlock_irqrestore(&dev->lock, flags); 4186bc235a2STomoki Sekiyama mutex_unlock(&dev->io_mutex); 419f1e255d6SJann Horn 42014427b86SBen Hutchings if (WARN_ON_ONCE(len >= sizeof(in_buffer))) 42114427b86SBen Hutchings return -EIO; 42214427b86SBen Hutchings 423f1e255d6SJann Horn return simple_read_from_buffer(buffer, count, ppos, in_buffer, len); 4246bc235a2STomoki Sekiyama } 4256bc235a2STomoki Sekiyama 4261cc373c6SSudip Mukherjee static ssize_t yurex_write(struct file *file, const char __user *user_buffer, 4271cc373c6SSudip Mukherjee size_t count, loff_t *ppos) 4286bc235a2STomoki Sekiyama { 4296bc235a2STomoki Sekiyama struct usb_yurex *dev; 4306bc235a2STomoki Sekiyama int i, set = 0, retval = 0; 4317e10f14eSBen Hutchings char buffer[16 + 1]; 4326bc235a2STomoki Sekiyama char *data = buffer; 4336bc235a2STomoki Sekiyama unsigned long long c, c2 = 0; 4346bc235a2STomoki Sekiyama signed long timeout = 0; 4356bc235a2STomoki Sekiyama DEFINE_WAIT(wait); 4366bc235a2STomoki Sekiyama 4377e10f14eSBen Hutchings count = min(sizeof(buffer) - 1, count); 438113ad911SArjun Sreedharan dev = file->private_data; 4396bc235a2STomoki Sekiyama 4406bc235a2STomoki Sekiyama /* verify that we actually have some data to write */ 4416bc235a2STomoki Sekiyama if (count == 0) 4426bc235a2STomoki Sekiyama goto error; 4436bc235a2STomoki Sekiyama 4446bc235a2STomoki Sekiyama mutex_lock(&dev->io_mutex); 445aafb00a9SJohan Hovold if (dev->disconnected) { /* already disconnected */ 4466bc235a2STomoki Sekiyama mutex_unlock(&dev->io_mutex); 4476bc235a2STomoki Sekiyama retval = -ENODEV; 4486bc235a2STomoki Sekiyama goto error; 4496bc235a2STomoki Sekiyama } 4506bc235a2STomoki Sekiyama 4516bc235a2STomoki Sekiyama if (copy_from_user(buffer, user_buffer, count)) { 4526bc235a2STomoki Sekiyama mutex_unlock(&dev->io_mutex); 4536bc235a2STomoki Sekiyama retval = -EFAULT; 4546bc235a2STomoki Sekiyama goto error; 4556bc235a2STomoki Sekiyama } 4567e10f14eSBen Hutchings buffer[count] = 0; 4576bc235a2STomoki Sekiyama memset(dev->cntl_buffer, CMD_PADDING, YUREX_BUF_SIZE); 4586bc235a2STomoki Sekiyama 4596bc235a2STomoki Sekiyama switch (buffer[0]) { 4606bc235a2STomoki Sekiyama case CMD_ANIMATE: 4616bc235a2STomoki Sekiyama case CMD_LED: 4626bc235a2STomoki Sekiyama dev->cntl_buffer[0] = buffer[0]; 4636bc235a2STomoki Sekiyama dev->cntl_buffer[1] = buffer[1]; 4646bc235a2STomoki Sekiyama dev->cntl_buffer[2] = CMD_EOF; 4656bc235a2STomoki Sekiyama break; 4666bc235a2STomoki Sekiyama case CMD_READ: 4676bc235a2STomoki Sekiyama case CMD_VERSION: 4686bc235a2STomoki Sekiyama dev->cntl_buffer[0] = buffer[0]; 4696bc235a2STomoki Sekiyama dev->cntl_buffer[1] = 0x00; 4706bc235a2STomoki Sekiyama dev->cntl_buffer[2] = CMD_EOF; 4716bc235a2STomoki Sekiyama break; 4726bc235a2STomoki Sekiyama case CMD_SET: 4736bc235a2STomoki Sekiyama data++; 4740d9b6d49SGustavo A. R. Silva fallthrough; 4756bc235a2STomoki Sekiyama case '0' ... '9': 4766bc235a2STomoki Sekiyama set = 1; 4776bc235a2STomoki Sekiyama c = c2 = simple_strtoull(data, NULL, 0); 4786bc235a2STomoki Sekiyama dev->cntl_buffer[0] = CMD_SET; 4796bc235a2STomoki Sekiyama for (i = 1; i < 6; i++) { 4806bc235a2STomoki Sekiyama dev->cntl_buffer[i] = (c>>32) & 0xff; 4816bc235a2STomoki Sekiyama c <<= 8; 4826bc235a2STomoki Sekiyama } 4836bc235a2STomoki Sekiyama buffer[6] = CMD_EOF; 4846bc235a2STomoki Sekiyama break; 4856bc235a2STomoki Sekiyama default: 4866bc235a2STomoki Sekiyama mutex_unlock(&dev->io_mutex); 4876bc235a2STomoki Sekiyama return -EINVAL; 4886bc235a2STomoki Sekiyama } 4896bc235a2STomoki Sekiyama 4906bc235a2STomoki Sekiyama /* send the data as the control msg */ 4916bc235a2STomoki Sekiyama prepare_to_wait(&dev->waitq, &wait, TASK_INTERRUPTIBLE); 492aadd6472SGreg Kroah-Hartman dev_dbg(&dev->interface->dev, "%s - submit %c\n", __func__, 493aadd6472SGreg Kroah-Hartman dev->cntl_buffer[0]); 494f176ede3SAlan Stern retval = usb_submit_urb(dev->cntl_urb, GFP_ATOMIC); 4956bc235a2STomoki Sekiyama if (retval >= 0) 4966bc235a2STomoki Sekiyama timeout = schedule_timeout(YUREX_WRITE_TIMEOUT); 4976bc235a2STomoki Sekiyama finish_wait(&dev->waitq, &wait); 4986bc235a2STomoki Sekiyama 4996bc235a2STomoki Sekiyama mutex_unlock(&dev->io_mutex); 5006bc235a2STomoki Sekiyama 5016bc235a2STomoki Sekiyama if (retval < 0) { 50245714104SGreg Kroah-Hartman dev_err(&dev->interface->dev, 50345714104SGreg Kroah-Hartman "%s - failed to send bulk msg, error %d\n", 50445714104SGreg Kroah-Hartman __func__, retval); 5056bc235a2STomoki Sekiyama goto error; 5066bc235a2STomoki Sekiyama } 5076bc235a2STomoki Sekiyama if (set && timeout) 5086bc235a2STomoki Sekiyama dev->bbu = c2; 5096bc235a2STomoki Sekiyama return timeout ? count : -EIO; 5106bc235a2STomoki Sekiyama 5116bc235a2STomoki Sekiyama error: 5126bc235a2STomoki Sekiyama return retval; 5136bc235a2STomoki Sekiyama } 5146bc235a2STomoki Sekiyama 5156bc235a2STomoki Sekiyama static const struct file_operations yurex_fops = { 5166bc235a2STomoki Sekiyama .owner = THIS_MODULE, 5176bc235a2STomoki Sekiyama .read = yurex_read, 5186bc235a2STomoki Sekiyama .write = yurex_write, 5196bc235a2STomoki Sekiyama .open = yurex_open, 5206bc235a2STomoki Sekiyama .release = yurex_release, 5216bc235a2STomoki Sekiyama .fasync = yurex_fasync, 52227f485b5STomoki Sekiyama .llseek = default_llseek, 5236bc235a2STomoki Sekiyama }; 5246bc235a2STomoki Sekiyama 52565db4305SGreg Kroah-Hartman module_usb_driver(yurex_driver); 5266bc235a2STomoki Sekiyama 5276bc235a2STomoki Sekiyama MODULE_LICENSE("GPL"); 528