xref: /openbmc/linux/drivers/usb/misc/yurex.c (revision 65db43054065790a75291b0834657445fea2cf56)
16bc235a2STomoki Sekiyama /*
26bc235a2STomoki Sekiyama  * Driver for Meywa-Denki & KAYAC YUREX
36bc235a2STomoki Sekiyama  *
46bc235a2STomoki Sekiyama  * Copyright (C) 2010 Tomoki Sekiyama (tomoki.sekiyama@gmail.com)
56bc235a2STomoki Sekiyama  *
66bc235a2STomoki Sekiyama  *	This program is free software; you can redistribute it and/or
76bc235a2STomoki Sekiyama  *	modify it under the terms of the GNU General Public License as
86bc235a2STomoki Sekiyama  *	published by the Free Software Foundation, version 2.
96bc235a2STomoki Sekiyama  *
106bc235a2STomoki Sekiyama  */
116bc235a2STomoki Sekiyama 
126bc235a2STomoki Sekiyama #include <linux/kernel.h>
136bc235a2STomoki Sekiyama #include <linux/errno.h>
146bc235a2STomoki Sekiyama #include <linux/init.h>
156bc235a2STomoki Sekiyama #include <linux/slab.h>
166bc235a2STomoki Sekiyama #include <linux/module.h>
176bc235a2STomoki Sekiyama #include <linux/kref.h>
186bc235a2STomoki Sekiyama #include <linux/mutex.h>
196bc235a2STomoki Sekiyama #include <linux/uaccess.h>
206bc235a2STomoki Sekiyama #include <linux/usb.h>
216bc235a2STomoki Sekiyama #include <linux/hid.h>
226bc235a2STomoki Sekiyama 
236bc235a2STomoki Sekiyama #define DRIVER_AUTHOR "Tomoki Sekiyama"
246bc235a2STomoki Sekiyama #define DRIVER_DESC "Driver for Meywa-Denki & KAYAC YUREX"
256bc235a2STomoki Sekiyama 
266bc235a2STomoki Sekiyama #define YUREX_VENDOR_ID		0x0c45
276bc235a2STomoki Sekiyama #define YUREX_PRODUCT_ID	0x1010
286bc235a2STomoki Sekiyama 
296bc235a2STomoki Sekiyama #define CMD_ACK		'!'
306bc235a2STomoki Sekiyama #define CMD_ANIMATE	'A'
316bc235a2STomoki Sekiyama #define CMD_COUNT	'C'
326bc235a2STomoki Sekiyama #define CMD_LED		'L'
336bc235a2STomoki Sekiyama #define CMD_READ	'R'
346bc235a2STomoki Sekiyama #define CMD_SET		'S'
356bc235a2STomoki Sekiyama #define CMD_VERSION	'V'
366bc235a2STomoki Sekiyama #define CMD_EOF		0x0d
376bc235a2STomoki Sekiyama #define CMD_PADDING	0xff
386bc235a2STomoki Sekiyama 
396bc235a2STomoki Sekiyama #define YUREX_BUF_SIZE		8
40e06ea97fSTomoki Sekiyama #define YUREX_WRITE_TIMEOUT	(HZ*2)
416bc235a2STomoki Sekiyama 
426bc235a2STomoki Sekiyama /* table of devices that work with this driver */
436bc235a2STomoki Sekiyama static struct usb_device_id yurex_table[] = {
446bc235a2STomoki Sekiyama 	{ USB_DEVICE(YUREX_VENDOR_ID, YUREX_PRODUCT_ID) },
456bc235a2STomoki Sekiyama 	{ }					/* Terminating entry */
466bc235a2STomoki Sekiyama };
476bc235a2STomoki Sekiyama MODULE_DEVICE_TABLE(usb, yurex_table);
486bc235a2STomoki Sekiyama 
496bc235a2STomoki Sekiyama #ifdef CONFIG_USB_DYNAMIC_MINORS
506bc235a2STomoki Sekiyama #define YUREX_MINOR_BASE	0
516bc235a2STomoki Sekiyama #else
521b62d258SGreg Kroah-Hartman #define YUREX_MINOR_BASE	192
536bc235a2STomoki Sekiyama #endif
546bc235a2STomoki Sekiyama 
556bc235a2STomoki Sekiyama /* Structure to hold all of our device specific stuff */
566bc235a2STomoki Sekiyama struct usb_yurex {
576bc235a2STomoki Sekiyama 	struct usb_device	*udev;
586bc235a2STomoki Sekiyama 	struct usb_interface	*interface;
596bc235a2STomoki Sekiyama 	__u8			int_in_endpointAddr;
606bc235a2STomoki Sekiyama 	struct urb		*urb;		/* URB for interrupt in */
616bc235a2STomoki Sekiyama 	unsigned char           *int_buffer;	/* buffer for intterupt in */
626bc235a2STomoki Sekiyama 	struct urb		*cntl_urb;	/* URB for control msg */
636bc235a2STomoki Sekiyama 	struct usb_ctrlrequest	*cntl_req;	/* req for control msg */
646bc235a2STomoki Sekiyama 	unsigned char		*cntl_buffer;	/* buffer for control msg */
656bc235a2STomoki Sekiyama 
666bc235a2STomoki Sekiyama 	struct kref		kref;
676bc235a2STomoki Sekiyama 	struct mutex		io_mutex;
686bc235a2STomoki Sekiyama 	struct fasync_struct	*async_queue;
696bc235a2STomoki Sekiyama 	wait_queue_head_t	waitq;
706bc235a2STomoki Sekiyama 
716bc235a2STomoki Sekiyama 	spinlock_t		lock;
726bc235a2STomoki Sekiyama 	__s64			bbu;		/* BBU from device */
736bc235a2STomoki Sekiyama };
746bc235a2STomoki Sekiyama #define to_yurex_dev(d) container_of(d, struct usb_yurex, kref)
756bc235a2STomoki Sekiyama 
766bc235a2STomoki Sekiyama static struct usb_driver yurex_driver;
776bc235a2STomoki Sekiyama static const struct file_operations yurex_fops;
786bc235a2STomoki Sekiyama 
796bc235a2STomoki Sekiyama 
806bc235a2STomoki Sekiyama static void yurex_control_callback(struct urb *urb)
816bc235a2STomoki Sekiyama {
826bc235a2STomoki Sekiyama 	struct usb_yurex *dev = urb->context;
836bc235a2STomoki Sekiyama 	int status = urb->status;
846bc235a2STomoki Sekiyama 
856bc235a2STomoki Sekiyama 	if (status) {
86e06ea97fSTomoki Sekiyama 		err("%s - control failed: %d\n", __func__, status);
876bc235a2STomoki Sekiyama 		wake_up_interruptible(&dev->waitq);
886bc235a2STomoki Sekiyama 		return;
896bc235a2STomoki Sekiyama 	}
906bc235a2STomoki Sekiyama 	/* on success, sender woken up by CMD_ACK int in, or timeout */
916bc235a2STomoki Sekiyama }
926bc235a2STomoki Sekiyama 
936bc235a2STomoki Sekiyama static void yurex_delete(struct kref *kref)
946bc235a2STomoki Sekiyama {
956bc235a2STomoki Sekiyama 	struct usb_yurex *dev = to_yurex_dev(kref);
966bc235a2STomoki Sekiyama 
976bc235a2STomoki Sekiyama 	dbg("yurex_delete");
986bc235a2STomoki Sekiyama 
996bc235a2STomoki Sekiyama 	usb_put_dev(dev->udev);
100e06ea97fSTomoki Sekiyama 	if (dev->cntl_urb) {
101e06ea97fSTomoki Sekiyama 		usb_kill_urb(dev->cntl_urb);
102e06ea97fSTomoki Sekiyama 		if (dev->cntl_req)
103e06ea97fSTomoki Sekiyama 			usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
104e06ea97fSTomoki Sekiyama 				dev->cntl_req, dev->cntl_urb->setup_dma);
105e06ea97fSTomoki Sekiyama 		if (dev->cntl_buffer)
106e06ea97fSTomoki Sekiyama 			usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
107e06ea97fSTomoki Sekiyama 				dev->cntl_buffer, dev->cntl_urb->transfer_dma);
108e06ea97fSTomoki Sekiyama 		usb_free_urb(dev->cntl_urb);
109e06ea97fSTomoki Sekiyama 	}
1106bc235a2STomoki Sekiyama 	if (dev->urb) {
1116bc235a2STomoki Sekiyama 		usb_kill_urb(dev->urb);
1126bc235a2STomoki Sekiyama 		if (dev->int_buffer)
1136bc235a2STomoki Sekiyama 			usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
1146bc235a2STomoki Sekiyama 				dev->int_buffer, dev->urb->transfer_dma);
1156bc235a2STomoki Sekiyama 		usb_free_urb(dev->urb);
1166bc235a2STomoki Sekiyama 	}
1176bc235a2STomoki Sekiyama 	kfree(dev);
1186bc235a2STomoki Sekiyama }
1196bc235a2STomoki Sekiyama 
1206bc235a2STomoki Sekiyama /*
1216bc235a2STomoki Sekiyama  * usb class driver info in order to get a minor number from the usb core,
1226bc235a2STomoki Sekiyama  * and to have the device registered with the driver core
1236bc235a2STomoki Sekiyama  */
1246bc235a2STomoki Sekiyama static struct usb_class_driver yurex_class = {
1256bc235a2STomoki Sekiyama 	.name =		"yurex%d",
1266bc235a2STomoki Sekiyama 	.fops =		&yurex_fops,
1276bc235a2STomoki Sekiyama 	.minor_base =	YUREX_MINOR_BASE,
1286bc235a2STomoki Sekiyama };
1296bc235a2STomoki Sekiyama 
1306bc235a2STomoki Sekiyama static void yurex_interrupt(struct urb *urb)
1316bc235a2STomoki Sekiyama {
1326bc235a2STomoki Sekiyama 	struct usb_yurex *dev = urb->context;
1336bc235a2STomoki Sekiyama 	unsigned char *buf = dev->int_buffer;
1346bc235a2STomoki Sekiyama 	int status = urb->status;
1356bc235a2STomoki Sekiyama 	unsigned long flags;
1366bc235a2STomoki Sekiyama 	int retval, i;
1376bc235a2STomoki Sekiyama 
1386bc235a2STomoki Sekiyama 	switch (status) {
1396bc235a2STomoki Sekiyama 	case 0: /*success*/
1406bc235a2STomoki Sekiyama 		break;
1416bc235a2STomoki Sekiyama 	case -EOVERFLOW:
1426bc235a2STomoki Sekiyama 		err("%s - overflow with length %d, actual length is %d",
1436bc235a2STomoki Sekiyama 		    __func__, YUREX_BUF_SIZE, dev->urb->actual_length);
1446bc235a2STomoki Sekiyama 	case -ECONNRESET:
1456bc235a2STomoki Sekiyama 	case -ENOENT:
1466bc235a2STomoki Sekiyama 	case -ESHUTDOWN:
1476bc235a2STomoki Sekiyama 	case -EILSEQ:
1486bc235a2STomoki Sekiyama 		/* The device is terminated, clean up */
1496bc235a2STomoki Sekiyama 		return;
1506bc235a2STomoki Sekiyama 	default:
1516bc235a2STomoki Sekiyama 		err("%s - unknown status received: %d", __func__, status);
1526bc235a2STomoki Sekiyama 		goto exit;
1536bc235a2STomoki Sekiyama 	}
1546bc235a2STomoki Sekiyama 
1556bc235a2STomoki Sekiyama 	/* handle received message */
1566bc235a2STomoki Sekiyama 	switch (buf[0]) {
1576bc235a2STomoki Sekiyama 	case CMD_COUNT:
1586bc235a2STomoki Sekiyama 	case CMD_READ:
1596bc235a2STomoki Sekiyama 		if (buf[6] == CMD_EOF) {
1606bc235a2STomoki Sekiyama 			spin_lock_irqsave(&dev->lock, flags);
1616bc235a2STomoki Sekiyama 			dev->bbu = 0;
1626bc235a2STomoki Sekiyama 			for (i = 1; i < 6; i++) {
1636bc235a2STomoki Sekiyama 				dev->bbu += buf[i];
1646bc235a2STomoki Sekiyama 				if (i != 5)
1656bc235a2STomoki Sekiyama 					dev->bbu <<= 8;
1666bc235a2STomoki Sekiyama 			}
1676bc235a2STomoki Sekiyama 			dbg("%s count: %lld", __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
1736bc235a2STomoki Sekiyama 			dbg("data format error - no EOF");
1746bc235a2STomoki Sekiyama 		break;
1756bc235a2STomoki Sekiyama 	case CMD_ACK:
1766bc235a2STomoki Sekiyama 		dbg("%s ack: %c", __func__, buf[1]);
1776bc235a2STomoki Sekiyama 		wake_up_interruptible(&dev->waitq);
1786bc235a2STomoki Sekiyama 		break;
1796bc235a2STomoki Sekiyama 	}
1806bc235a2STomoki Sekiyama 
1816bc235a2STomoki Sekiyama exit:
1826bc235a2STomoki Sekiyama 	retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
1836bc235a2STomoki Sekiyama 	if (retval) {
1846bc235a2STomoki Sekiyama 		err("%s - usb_submit_urb failed: %d",
1856bc235a2STomoki Sekiyama 			__func__, retval);
1866bc235a2STomoki Sekiyama 	}
1876bc235a2STomoki Sekiyama }
1886bc235a2STomoki Sekiyama 
1896bc235a2STomoki Sekiyama static int yurex_probe(struct usb_interface *interface, const struct usb_device_id *id)
1906bc235a2STomoki Sekiyama {
1916bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
1926bc235a2STomoki Sekiyama 	struct usb_host_interface *iface_desc;
1936bc235a2STomoki Sekiyama 	struct usb_endpoint_descriptor *endpoint;
1946bc235a2STomoki Sekiyama 	int retval = -ENOMEM;
1956bc235a2STomoki Sekiyama 	int i;
1966bc235a2STomoki Sekiyama 	DEFINE_WAIT(wait);
1976bc235a2STomoki Sekiyama 
1986bc235a2STomoki Sekiyama 	/* allocate memory for our device state and initialize it */
1996bc235a2STomoki Sekiyama 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2006bc235a2STomoki Sekiyama 	if (!dev) {
2016bc235a2STomoki Sekiyama 		err("Out of memory");
2026bc235a2STomoki Sekiyama 		goto error;
2036bc235a2STomoki Sekiyama 	}
2046bc235a2STomoki Sekiyama 	kref_init(&dev->kref);
2056bc235a2STomoki Sekiyama 	mutex_init(&dev->io_mutex);
2066bc235a2STomoki Sekiyama 	spin_lock_init(&dev->lock);
2076bc235a2STomoki Sekiyama 	init_waitqueue_head(&dev->waitq);
2086bc235a2STomoki Sekiyama 
2096bc235a2STomoki Sekiyama 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
2106bc235a2STomoki Sekiyama 	dev->interface = interface;
2116bc235a2STomoki Sekiyama 
2126bc235a2STomoki Sekiyama 	/* set up the endpoint information */
2136bc235a2STomoki Sekiyama 	iface_desc = interface->cur_altsetting;
2146bc235a2STomoki Sekiyama 	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2156bc235a2STomoki Sekiyama 		endpoint = &iface_desc->endpoint[i].desc;
2166bc235a2STomoki Sekiyama 
2176bc235a2STomoki Sekiyama 		if (usb_endpoint_is_int_in(endpoint)) {
2186bc235a2STomoki Sekiyama 			dev->int_in_endpointAddr = endpoint->bEndpointAddress;
2196bc235a2STomoki Sekiyama 			break;
2206bc235a2STomoki Sekiyama 		}
2216bc235a2STomoki Sekiyama 	}
2226bc235a2STomoki Sekiyama 	if (!dev->int_in_endpointAddr) {
2236bc235a2STomoki Sekiyama 		retval = -ENODEV;
2246bc235a2STomoki Sekiyama 		err("Could not find endpoints");
2256bc235a2STomoki Sekiyama 		goto error;
2266bc235a2STomoki Sekiyama 	}
2276bc235a2STomoki Sekiyama 
2286bc235a2STomoki Sekiyama 
2296bc235a2STomoki Sekiyama 	/* allocate control URB */
2306bc235a2STomoki Sekiyama 	dev->cntl_urb = usb_alloc_urb(0, GFP_KERNEL);
2316bc235a2STomoki Sekiyama 	if (!dev->cntl_urb) {
2326bc235a2STomoki Sekiyama 		err("Could not allocate control URB");
2336bc235a2STomoki Sekiyama 		goto error;
2346bc235a2STomoki Sekiyama 	}
2356bc235a2STomoki Sekiyama 
2366bc235a2STomoki Sekiyama 	/* allocate buffer for control req */
2376bc235a2STomoki Sekiyama 	dev->cntl_req = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
2386bc235a2STomoki Sekiyama 					   GFP_KERNEL,
2396bc235a2STomoki Sekiyama 					   &dev->cntl_urb->setup_dma);
2406bc235a2STomoki Sekiyama 	if (!dev->cntl_req) {
2416bc235a2STomoki Sekiyama 		err("Could not allocate cntl_req");
2426bc235a2STomoki Sekiyama 		goto error;
2436bc235a2STomoki Sekiyama 	}
2446bc235a2STomoki Sekiyama 
2456bc235a2STomoki Sekiyama 	/* allocate buffer for control msg */
2466bc235a2STomoki Sekiyama 	dev->cntl_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
2476bc235a2STomoki Sekiyama 					      GFP_KERNEL,
2486bc235a2STomoki Sekiyama 					      &dev->cntl_urb->transfer_dma);
2496bc235a2STomoki Sekiyama 	if (!dev->cntl_buffer) {
2506bc235a2STomoki Sekiyama 		err("Could not allocate cntl_buffer");
2516bc235a2STomoki Sekiyama 		goto error;
2526bc235a2STomoki Sekiyama 	}
2536bc235a2STomoki Sekiyama 
2546bc235a2STomoki Sekiyama 	/* configure control URB */
2556bc235a2STomoki Sekiyama 	dev->cntl_req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS |
2566bc235a2STomoki Sekiyama 				      USB_RECIP_INTERFACE;
2576bc235a2STomoki Sekiyama 	dev->cntl_req->bRequest	= HID_REQ_SET_REPORT;
2586bc235a2STomoki Sekiyama 	dev->cntl_req->wValue	= cpu_to_le16((HID_OUTPUT_REPORT + 1) << 8);
2596bc235a2STomoki Sekiyama 	dev->cntl_req->wIndex	= cpu_to_le16(iface_desc->desc.bInterfaceNumber);
2606bc235a2STomoki Sekiyama 	dev->cntl_req->wLength	= cpu_to_le16(YUREX_BUF_SIZE);
2616bc235a2STomoki Sekiyama 
2626bc235a2STomoki Sekiyama 	usb_fill_control_urb(dev->cntl_urb, dev->udev,
2636bc235a2STomoki Sekiyama 			     usb_sndctrlpipe(dev->udev, 0),
2646bc235a2STomoki Sekiyama 			     (void *)dev->cntl_req, dev->cntl_buffer,
2656bc235a2STomoki Sekiyama 			     YUREX_BUF_SIZE, yurex_control_callback, dev);
2666bc235a2STomoki Sekiyama 	dev->cntl_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2676bc235a2STomoki Sekiyama 
2686bc235a2STomoki Sekiyama 
2696bc235a2STomoki Sekiyama 	/* allocate interrupt URB */
2706bc235a2STomoki Sekiyama 	dev->urb = usb_alloc_urb(0, GFP_KERNEL);
2716bc235a2STomoki Sekiyama 	if (!dev->urb) {
2726bc235a2STomoki Sekiyama 		err("Could not allocate URB");
2736bc235a2STomoki Sekiyama 		goto error;
2746bc235a2STomoki Sekiyama 	}
2756bc235a2STomoki Sekiyama 
2766bc235a2STomoki Sekiyama 	/* allocate buffer for interrupt in */
2776bc235a2STomoki Sekiyama 	dev->int_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
2786bc235a2STomoki Sekiyama 					GFP_KERNEL, &dev->urb->transfer_dma);
2796bc235a2STomoki Sekiyama 	if (!dev->int_buffer) {
2806bc235a2STomoki Sekiyama 		err("Could not allocate int_buffer");
2816bc235a2STomoki Sekiyama 		goto error;
2826bc235a2STomoki Sekiyama 	}
2836bc235a2STomoki Sekiyama 
2846bc235a2STomoki Sekiyama 	/* configure interrupt URB */
2856bc235a2STomoki Sekiyama 	usb_fill_int_urb(dev->urb, dev->udev,
2866bc235a2STomoki Sekiyama 			 usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
2876bc235a2STomoki Sekiyama 			 dev->int_buffer, YUREX_BUF_SIZE, yurex_interrupt,
2886bc235a2STomoki Sekiyama 			 dev, 1);
289e06ea97fSTomoki Sekiyama 	dev->cntl_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2906bc235a2STomoki Sekiyama 	if (usb_submit_urb(dev->urb, GFP_KERNEL)) {
2916bc235a2STomoki Sekiyama 		retval = -EIO;
2926bc235a2STomoki Sekiyama 		err("Could not submitting URB");
2936bc235a2STomoki Sekiyama 		goto error;
2946bc235a2STomoki Sekiyama 	}
2956bc235a2STomoki Sekiyama 
2966bc235a2STomoki Sekiyama 	/* save our data pointer in this interface device */
2976bc235a2STomoki Sekiyama 	usb_set_intfdata(interface, dev);
2986bc235a2STomoki Sekiyama 
2996bc235a2STomoki Sekiyama 	/* we can register the device now, as it is ready */
3006bc235a2STomoki Sekiyama 	retval = usb_register_dev(interface, &yurex_class);
3016bc235a2STomoki Sekiyama 	if (retval) {
3026bc235a2STomoki Sekiyama 		err("Not able to get a minor for this device.");
3036bc235a2STomoki Sekiyama 		usb_set_intfdata(interface, NULL);
3046bc235a2STomoki Sekiyama 		goto error;
3056bc235a2STomoki Sekiyama 	}
3066bc235a2STomoki Sekiyama 
3076bc235a2STomoki Sekiyama 	dev->bbu = -1;
3086bc235a2STomoki Sekiyama 
3096bc235a2STomoki Sekiyama 	dev_info(&interface->dev,
310e06ea97fSTomoki Sekiyama 		 "USB YUREX device now attached to Yurex #%d\n",
3116bc235a2STomoki Sekiyama 		 interface->minor);
3126bc235a2STomoki Sekiyama 
3136bc235a2STomoki Sekiyama 	return 0;
3146bc235a2STomoki Sekiyama 
3156bc235a2STomoki Sekiyama error:
3166bc235a2STomoki Sekiyama 	if (dev)
3176bc235a2STomoki Sekiyama 		/* this frees allocated memory */
3186bc235a2STomoki Sekiyama 		kref_put(&dev->kref, yurex_delete);
3196bc235a2STomoki Sekiyama 	return retval;
3206bc235a2STomoki Sekiyama }
3216bc235a2STomoki Sekiyama 
3226bc235a2STomoki Sekiyama static void yurex_disconnect(struct usb_interface *interface)
3236bc235a2STomoki Sekiyama {
3246bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
3256bc235a2STomoki Sekiyama 	int minor = interface->minor;
3266bc235a2STomoki Sekiyama 
3276bc235a2STomoki Sekiyama 	dev = usb_get_intfdata(interface);
3286bc235a2STomoki Sekiyama 	usb_set_intfdata(interface, NULL);
3296bc235a2STomoki Sekiyama 
3306bc235a2STomoki Sekiyama 	/* give back our minor */
3316bc235a2STomoki Sekiyama 	usb_deregister_dev(interface, &yurex_class);
3326bc235a2STomoki Sekiyama 
3336bc235a2STomoki Sekiyama 	/* prevent more I/O from starting */
3346bc235a2STomoki Sekiyama 	mutex_lock(&dev->io_mutex);
3356bc235a2STomoki Sekiyama 	dev->interface = NULL;
3366bc235a2STomoki Sekiyama 	mutex_unlock(&dev->io_mutex);
3376bc235a2STomoki Sekiyama 
3386bc235a2STomoki Sekiyama 	/* wakeup waiters */
3396bc235a2STomoki Sekiyama 	kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
3406bc235a2STomoki Sekiyama 	wake_up_interruptible(&dev->waitq);
3416bc235a2STomoki Sekiyama 
3426bc235a2STomoki Sekiyama 	/* decrement our usage count */
3436bc235a2STomoki Sekiyama 	kref_put(&dev->kref, yurex_delete);
3446bc235a2STomoki Sekiyama 
345e06ea97fSTomoki Sekiyama 	dev_info(&interface->dev, "USB YUREX #%d now disconnected\n", minor);
3466bc235a2STomoki Sekiyama }
3476bc235a2STomoki Sekiyama 
3486bc235a2STomoki Sekiyama static struct usb_driver yurex_driver = {
3496bc235a2STomoki Sekiyama 	.name =		"yurex",
3506bc235a2STomoki Sekiyama 	.probe =	yurex_probe,
3516bc235a2STomoki Sekiyama 	.disconnect =	yurex_disconnect,
3526bc235a2STomoki Sekiyama 	.id_table =	yurex_table,
3536bc235a2STomoki Sekiyama };
3546bc235a2STomoki Sekiyama 
3556bc235a2STomoki Sekiyama 
3566bc235a2STomoki Sekiyama static int yurex_fasync(int fd, struct file *file, int on)
3576bc235a2STomoki Sekiyama {
3586bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
3596bc235a2STomoki Sekiyama 
3606bc235a2STomoki Sekiyama 	dev = (struct usb_yurex *)file->private_data;
3616bc235a2STomoki Sekiyama 	return fasync_helper(fd, file, on, &dev->async_queue);
3626bc235a2STomoki Sekiyama }
3636bc235a2STomoki Sekiyama 
3646bc235a2STomoki Sekiyama static int yurex_open(struct inode *inode, struct file *file)
3656bc235a2STomoki Sekiyama {
3666bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
3676bc235a2STomoki Sekiyama 	struct usb_interface *interface;
3686bc235a2STomoki Sekiyama 	int subminor;
3696bc235a2STomoki Sekiyama 	int retval = 0;
3706bc235a2STomoki Sekiyama 
3716bc235a2STomoki Sekiyama 	subminor = iminor(inode);
3726bc235a2STomoki Sekiyama 
3736bc235a2STomoki Sekiyama 	interface = usb_find_interface(&yurex_driver, subminor);
3746bc235a2STomoki Sekiyama 	if (!interface) {
3756bc235a2STomoki Sekiyama 		err("%s - error, can't find device for minor %d",
3766bc235a2STomoki Sekiyama 		    __func__, subminor);
3776bc235a2STomoki Sekiyama 		retval = -ENODEV;
3786bc235a2STomoki Sekiyama 		goto exit;
3796bc235a2STomoki Sekiyama 	}
3806bc235a2STomoki Sekiyama 
3816bc235a2STomoki Sekiyama 	dev = usb_get_intfdata(interface);
3826bc235a2STomoki Sekiyama 	if (!dev) {
3836bc235a2STomoki Sekiyama 		retval = -ENODEV;
3846bc235a2STomoki Sekiyama 		goto exit;
3856bc235a2STomoki Sekiyama 	}
3866bc235a2STomoki Sekiyama 
3876bc235a2STomoki Sekiyama 	/* increment our usage count for the device */
3886bc235a2STomoki Sekiyama 	kref_get(&dev->kref);
3896bc235a2STomoki Sekiyama 
3906bc235a2STomoki Sekiyama 	/* save our object in the file's private structure */
3916bc235a2STomoki Sekiyama 	mutex_lock(&dev->io_mutex);
3926bc235a2STomoki Sekiyama 	file->private_data = dev;
3936bc235a2STomoki Sekiyama 	mutex_unlock(&dev->io_mutex);
3946bc235a2STomoki Sekiyama 
3956bc235a2STomoki Sekiyama exit:
3966bc235a2STomoki Sekiyama 	return retval;
3976bc235a2STomoki Sekiyama }
3986bc235a2STomoki Sekiyama 
3996bc235a2STomoki Sekiyama static int yurex_release(struct inode *inode, struct file *file)
4006bc235a2STomoki Sekiyama {
4016bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
4026bc235a2STomoki Sekiyama 
4036bc235a2STomoki Sekiyama 	dev = (struct usb_yurex *)file->private_data;
4046bc235a2STomoki Sekiyama 	if (dev == NULL)
4056bc235a2STomoki Sekiyama 		return -ENODEV;
4066bc235a2STomoki Sekiyama 
4076bc235a2STomoki Sekiyama 	yurex_fasync(-1, file, 0);
4086bc235a2STomoki Sekiyama 
4096bc235a2STomoki Sekiyama 	/* decrement the count on our device */
4106bc235a2STomoki Sekiyama 	kref_put(&dev->kref, yurex_delete);
4116bc235a2STomoki Sekiyama 	return 0;
4126bc235a2STomoki Sekiyama }
4136bc235a2STomoki Sekiyama 
4146bc235a2STomoki Sekiyama static ssize_t yurex_read(struct file *file, char *buffer, size_t count, loff_t *ppos)
4156bc235a2STomoki Sekiyama {
4166bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
4176bc235a2STomoki Sekiyama 	int retval = 0;
4186bc235a2STomoki Sekiyama 	int bytes_read = 0;
4196bc235a2STomoki Sekiyama 	char in_buffer[20];
4206bc235a2STomoki Sekiyama 	unsigned long flags;
4216bc235a2STomoki Sekiyama 
4226bc235a2STomoki Sekiyama 	dev = (struct usb_yurex *)file->private_data;
4236bc235a2STomoki Sekiyama 
4246bc235a2STomoki Sekiyama 	mutex_lock(&dev->io_mutex);
4256bc235a2STomoki Sekiyama 	if (!dev->interface) {		/* already disconnected */
4266bc235a2STomoki Sekiyama 		retval = -ENODEV;
4276bc235a2STomoki Sekiyama 		goto exit;
4286bc235a2STomoki Sekiyama 	}
4296bc235a2STomoki Sekiyama 
4306bc235a2STomoki Sekiyama 	spin_lock_irqsave(&dev->lock, flags);
431e06ea97fSTomoki Sekiyama 	bytes_read = snprintf(in_buffer, 20, "%lld\n", dev->bbu);
4326bc235a2STomoki Sekiyama 	spin_unlock_irqrestore(&dev->lock, flags);
4336bc235a2STomoki Sekiyama 
4346bc235a2STomoki Sekiyama 	if (*ppos < bytes_read) {
4356bc235a2STomoki Sekiyama 		if (copy_to_user(buffer, in_buffer + *ppos, bytes_read - *ppos))
4366bc235a2STomoki Sekiyama 			retval = -EFAULT;
4376bc235a2STomoki Sekiyama 		else {
4386bc235a2STomoki Sekiyama 			retval = bytes_read - *ppos;
4396bc235a2STomoki Sekiyama 			*ppos += bytes_read;
4406bc235a2STomoki Sekiyama 		}
4416bc235a2STomoki Sekiyama 	}
4426bc235a2STomoki Sekiyama 
4436bc235a2STomoki Sekiyama exit:
4446bc235a2STomoki Sekiyama 	mutex_unlock(&dev->io_mutex);
4456bc235a2STomoki Sekiyama 	return retval;
4466bc235a2STomoki Sekiyama }
4476bc235a2STomoki Sekiyama 
4486bc235a2STomoki Sekiyama static ssize_t yurex_write(struct file *file, const char *user_buffer, size_t count, loff_t *ppos)
4496bc235a2STomoki Sekiyama {
4506bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
4516bc235a2STomoki Sekiyama 	int i, set = 0, retval = 0;
4526bc235a2STomoki Sekiyama 	char buffer[16];
4536bc235a2STomoki Sekiyama 	char *data = buffer;
4546bc235a2STomoki Sekiyama 	unsigned long long c, c2 = 0;
4556bc235a2STomoki Sekiyama 	signed long timeout = 0;
4566bc235a2STomoki Sekiyama 	DEFINE_WAIT(wait);
4576bc235a2STomoki Sekiyama 
4586bc235a2STomoki Sekiyama 	count = min(sizeof(buffer), count);
4596bc235a2STomoki Sekiyama 	dev = (struct usb_yurex *)file->private_data;
4606bc235a2STomoki Sekiyama 
4616bc235a2STomoki Sekiyama 	/* verify that we actually have some data to write */
4626bc235a2STomoki Sekiyama 	if (count == 0)
4636bc235a2STomoki Sekiyama 		goto error;
4646bc235a2STomoki Sekiyama 
4656bc235a2STomoki Sekiyama 	mutex_lock(&dev->io_mutex);
4666bc235a2STomoki Sekiyama 	if (!dev->interface) {		/* alreaday disconnected */
4676bc235a2STomoki Sekiyama 		mutex_unlock(&dev->io_mutex);
4686bc235a2STomoki Sekiyama 		retval = -ENODEV;
4696bc235a2STomoki Sekiyama 		goto error;
4706bc235a2STomoki Sekiyama 	}
4716bc235a2STomoki Sekiyama 
4726bc235a2STomoki Sekiyama 	if (copy_from_user(buffer, user_buffer, count)) {
4736bc235a2STomoki Sekiyama 		mutex_unlock(&dev->io_mutex);
4746bc235a2STomoki Sekiyama 		retval = -EFAULT;
4756bc235a2STomoki Sekiyama 		goto error;
4766bc235a2STomoki Sekiyama 	}
4776bc235a2STomoki Sekiyama 	memset(dev->cntl_buffer, CMD_PADDING, YUREX_BUF_SIZE);
4786bc235a2STomoki Sekiyama 
4796bc235a2STomoki Sekiyama 	switch (buffer[0]) {
4806bc235a2STomoki Sekiyama 	case CMD_ANIMATE:
4816bc235a2STomoki Sekiyama 	case CMD_LED:
4826bc235a2STomoki Sekiyama 		dev->cntl_buffer[0] = buffer[0];
4836bc235a2STomoki Sekiyama 		dev->cntl_buffer[1] = buffer[1];
4846bc235a2STomoki Sekiyama 		dev->cntl_buffer[2] = CMD_EOF;
4856bc235a2STomoki Sekiyama 		break;
4866bc235a2STomoki Sekiyama 	case CMD_READ:
4876bc235a2STomoki Sekiyama 	case CMD_VERSION:
4886bc235a2STomoki Sekiyama 		dev->cntl_buffer[0] = buffer[0];
4896bc235a2STomoki Sekiyama 		dev->cntl_buffer[1] = 0x00;
4906bc235a2STomoki Sekiyama 		dev->cntl_buffer[2] = CMD_EOF;
4916bc235a2STomoki Sekiyama 		break;
4926bc235a2STomoki Sekiyama 	case CMD_SET:
4936bc235a2STomoki Sekiyama 		data++;
4946bc235a2STomoki Sekiyama 		/* FALL THROUGH */
4956bc235a2STomoki Sekiyama 	case '0' ... '9':
4966bc235a2STomoki Sekiyama 		set = 1;
4976bc235a2STomoki Sekiyama 		c = c2 = simple_strtoull(data, NULL, 0);
4986bc235a2STomoki Sekiyama 		dev->cntl_buffer[0] = CMD_SET;
4996bc235a2STomoki Sekiyama 		for (i = 1; i < 6; i++) {
5006bc235a2STomoki Sekiyama 			dev->cntl_buffer[i] = (c>>32) & 0xff;
5016bc235a2STomoki Sekiyama 			c <<= 8;
5026bc235a2STomoki Sekiyama 		}
5036bc235a2STomoki Sekiyama 		buffer[6] = CMD_EOF;
5046bc235a2STomoki Sekiyama 		break;
5056bc235a2STomoki Sekiyama 	default:
5066bc235a2STomoki Sekiyama 		mutex_unlock(&dev->io_mutex);
5076bc235a2STomoki Sekiyama 		return -EINVAL;
5086bc235a2STomoki Sekiyama 	}
5096bc235a2STomoki Sekiyama 
5106bc235a2STomoki Sekiyama 	/* send the data as the control msg */
5116bc235a2STomoki Sekiyama 	prepare_to_wait(&dev->waitq, &wait, TASK_INTERRUPTIBLE);
5126bc235a2STomoki Sekiyama 	dbg("%s - submit %c", __func__, dev->cntl_buffer[0]);
5136bc235a2STomoki Sekiyama 	retval = usb_submit_urb(dev->cntl_urb, GFP_KERNEL);
5146bc235a2STomoki Sekiyama 	if (retval >= 0)
5156bc235a2STomoki Sekiyama 		timeout = schedule_timeout(YUREX_WRITE_TIMEOUT);
5166bc235a2STomoki Sekiyama 	finish_wait(&dev->waitq, &wait);
5176bc235a2STomoki Sekiyama 
5186bc235a2STomoki Sekiyama 	mutex_unlock(&dev->io_mutex);
5196bc235a2STomoki Sekiyama 
5206bc235a2STomoki Sekiyama 	if (retval < 0) {
5216bc235a2STomoki Sekiyama 		err("%s - failed to send bulk msg, error %d", __func__, retval);
5226bc235a2STomoki Sekiyama 		goto error;
5236bc235a2STomoki Sekiyama 	}
5246bc235a2STomoki Sekiyama 	if (set && timeout)
5256bc235a2STomoki Sekiyama 		dev->bbu = c2;
5266bc235a2STomoki Sekiyama 	return timeout ? count : -EIO;
5276bc235a2STomoki Sekiyama 
5286bc235a2STomoki Sekiyama error:
5296bc235a2STomoki Sekiyama 	return retval;
5306bc235a2STomoki Sekiyama }
5316bc235a2STomoki Sekiyama 
5326bc235a2STomoki Sekiyama static const struct file_operations yurex_fops = {
5336bc235a2STomoki Sekiyama 	.owner =	THIS_MODULE,
5346bc235a2STomoki Sekiyama 	.read =		yurex_read,
5356bc235a2STomoki Sekiyama 	.write =	yurex_write,
5366bc235a2STomoki Sekiyama 	.open =		yurex_open,
5376bc235a2STomoki Sekiyama 	.release =	yurex_release,
5386bc235a2STomoki Sekiyama 	.fasync	=	yurex_fasync,
53927f485b5STomoki Sekiyama 	.llseek =	default_llseek,
5406bc235a2STomoki Sekiyama };
5416bc235a2STomoki Sekiyama 
542*65db4305SGreg Kroah-Hartman module_usb_driver(yurex_driver);
5436bc235a2STomoki Sekiyama 
5446bc235a2STomoki Sekiyama MODULE_LICENSE("GPL");
545