xref: /openbmc/linux/drivers/usb/misc/yurex.c (revision eff6dde4c3a0e2f93b1a9ababdbca78f3005b098)
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 
37*eff6dde4SLee Jones #define MAX_S64_STRLEN 20 /* {-}922337203685477580{7,8} */
38*eff6dde4SLee Jones 
396bc235a2STomoki Sekiyama /* table of devices that work with this driver */
406bc235a2STomoki Sekiyama static struct usb_device_id yurex_table[] = {
416bc235a2STomoki Sekiyama 	{ USB_DEVICE(YUREX_VENDOR_ID, YUREX_PRODUCT_ID) },
426bc235a2STomoki Sekiyama 	{ }					/* Terminating entry */
436bc235a2STomoki Sekiyama };
446bc235a2STomoki Sekiyama MODULE_DEVICE_TABLE(usb, yurex_table);
456bc235a2STomoki Sekiyama 
466bc235a2STomoki Sekiyama #ifdef CONFIG_USB_DYNAMIC_MINORS
476bc235a2STomoki Sekiyama #define YUREX_MINOR_BASE	0
486bc235a2STomoki Sekiyama #else
491b62d258SGreg Kroah-Hartman #define YUREX_MINOR_BASE	192
506bc235a2STomoki Sekiyama #endif
516bc235a2STomoki Sekiyama 
526bc235a2STomoki Sekiyama /* Structure to hold all of our device specific stuff */
536bc235a2STomoki Sekiyama struct usb_yurex {
546bc235a2STomoki Sekiyama 	struct usb_device	*udev;
556bc235a2STomoki Sekiyama 	struct usb_interface	*interface;
566bc235a2STomoki Sekiyama 	__u8			int_in_endpointAddr;
576bc235a2STomoki Sekiyama 	struct urb		*urb;		/* URB for interrupt in */
586bc235a2STomoki Sekiyama 	unsigned char           *int_buffer;	/* buffer for intterupt in */
596bc235a2STomoki Sekiyama 	struct urb		*cntl_urb;	/* URB for control msg */
606bc235a2STomoki Sekiyama 	struct usb_ctrlrequest	*cntl_req;	/* req for control msg */
616bc235a2STomoki Sekiyama 	unsigned char		*cntl_buffer;	/* buffer for control msg */
626bc235a2STomoki Sekiyama 
636bc235a2STomoki Sekiyama 	struct kref		kref;
646bc235a2STomoki Sekiyama 	struct mutex		io_mutex;
65aafb00a9SJohan Hovold 	unsigned long		disconnected:1;
666bc235a2STomoki Sekiyama 	struct fasync_struct	*async_queue;
676bc235a2STomoki Sekiyama 	wait_queue_head_t	waitq;
686bc235a2STomoki Sekiyama 
696bc235a2STomoki Sekiyama 	spinlock_t		lock;
706bc235a2STomoki Sekiyama 	__s64			bbu;		/* BBU from device */
716bc235a2STomoki Sekiyama };
726bc235a2STomoki Sekiyama #define to_yurex_dev(d) container_of(d, struct usb_yurex, kref)
736bc235a2STomoki Sekiyama 
746bc235a2STomoki Sekiyama static struct usb_driver yurex_driver;
756bc235a2STomoki Sekiyama static const struct file_operations yurex_fops;
766bc235a2STomoki Sekiyama 
776bc235a2STomoki Sekiyama 
786bc235a2STomoki Sekiyama static void yurex_control_callback(struct urb *urb)
796bc235a2STomoki Sekiyama {
806bc235a2STomoki Sekiyama 	struct usb_yurex *dev = urb->context;
816bc235a2STomoki Sekiyama 	int status = urb->status;
826bc235a2STomoki Sekiyama 
836bc235a2STomoki Sekiyama 	if (status) {
8445714104SGreg Kroah-Hartman 		dev_err(&urb->dev->dev, "%s - control failed: %d\n",
8545714104SGreg Kroah-Hartman 			__func__, status);
866bc235a2STomoki Sekiyama 		wake_up_interruptible(&dev->waitq);
876bc235a2STomoki Sekiyama 		return;
886bc235a2STomoki Sekiyama 	}
896bc235a2STomoki Sekiyama 	/* on success, sender woken up by CMD_ACK int in, or timeout */
906bc235a2STomoki Sekiyama }
916bc235a2STomoki Sekiyama 
926bc235a2STomoki Sekiyama static void yurex_delete(struct kref *kref)
936bc235a2STomoki Sekiyama {
946bc235a2STomoki Sekiyama 	struct usb_yurex *dev = to_yurex_dev(kref);
956bc235a2STomoki Sekiyama 
96aadd6472SGreg Kroah-Hartman 	dev_dbg(&dev->interface->dev, "%s\n", __func__);
976bc235a2STomoki Sekiyama 
98e06ea97fSTomoki Sekiyama 	if (dev->cntl_urb) {
99e06ea97fSTomoki Sekiyama 		usb_kill_urb(dev->cntl_urb);
100523fc5c1STomoki Sekiyama 		kfree(dev->cntl_req);
101e06ea97fSTomoki Sekiyama 		usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
102e06ea97fSTomoki Sekiyama 				dev->cntl_buffer, dev->cntl_urb->transfer_dma);
103e06ea97fSTomoki Sekiyama 		usb_free_urb(dev->cntl_urb);
104e06ea97fSTomoki Sekiyama 	}
1056bc235a2STomoki Sekiyama 	if (dev->urb) {
1066bc235a2STomoki Sekiyama 		usb_kill_urb(dev->urb);
1076bc235a2STomoki Sekiyama 		usb_free_coherent(dev->udev, YUREX_BUF_SIZE,
1086bc235a2STomoki Sekiyama 				dev->int_buffer, dev->urb->transfer_dma);
1096bc235a2STomoki Sekiyama 		usb_free_urb(dev->urb);
1106bc235a2STomoki Sekiyama 	}
111aafb00a9SJohan Hovold 	usb_put_intf(dev->interface);
112fc05481bSSuzuki K Poulose 	usb_put_dev(dev->udev);
1136bc235a2STomoki Sekiyama 	kfree(dev);
1146bc235a2STomoki Sekiyama }
1156bc235a2STomoki Sekiyama 
1166bc235a2STomoki Sekiyama /*
1176bc235a2STomoki Sekiyama  * usb class driver info in order to get a minor number from the usb core,
1186bc235a2STomoki Sekiyama  * and to have the device registered with the driver core
1196bc235a2STomoki Sekiyama  */
1206bc235a2STomoki Sekiyama static struct usb_class_driver yurex_class = {
1216bc235a2STomoki Sekiyama 	.name =		"yurex%d",
1226bc235a2STomoki Sekiyama 	.fops =		&yurex_fops,
1236bc235a2STomoki Sekiyama 	.minor_base =	YUREX_MINOR_BASE,
1246bc235a2STomoki Sekiyama };
1256bc235a2STomoki Sekiyama 
1266bc235a2STomoki Sekiyama static void yurex_interrupt(struct urb *urb)
1276bc235a2STomoki Sekiyama {
1286bc235a2STomoki Sekiyama 	struct usb_yurex *dev = urb->context;
1296bc235a2STomoki Sekiyama 	unsigned char *buf = dev->int_buffer;
1306bc235a2STomoki Sekiyama 	int status = urb->status;
1316bc235a2STomoki Sekiyama 	unsigned long flags;
1326bc235a2STomoki Sekiyama 	int retval, i;
1336bc235a2STomoki Sekiyama 
1346bc235a2STomoki Sekiyama 	switch (status) {
1356bc235a2STomoki Sekiyama 	case 0: /*success*/
1366bc235a2STomoki Sekiyama 		break;
13732a0721cSAlan Stern 	/* The device is terminated or messed up, give up */
1386bc235a2STomoki Sekiyama 	case -EOVERFLOW:
13945714104SGreg Kroah-Hartman 		dev_err(&dev->interface->dev,
14045714104SGreg Kroah-Hartman 			"%s - overflow with length %d, actual length is %d\n",
1416bc235a2STomoki Sekiyama 			__func__, YUREX_BUF_SIZE, dev->urb->actual_length);
14293c747edSGustavo A. R. Silva 		return;
1436bc235a2STomoki Sekiyama 	case -ECONNRESET:
1446bc235a2STomoki Sekiyama 	case -ENOENT:
1456bc235a2STomoki Sekiyama 	case -ESHUTDOWN:
1466bc235a2STomoki Sekiyama 	case -EILSEQ:
14732a0721cSAlan Stern 	case -EPROTO:
14832a0721cSAlan Stern 	case -ETIME:
1496bc235a2STomoki Sekiyama 		return;
1506bc235a2STomoki Sekiyama 	default:
15145714104SGreg Kroah-Hartman 		dev_err(&dev->interface->dev,
15245714104SGreg Kroah-Hartman 			"%s - unknown status received: %d\n", __func__, status);
15332a0721cSAlan Stern 		return;
1546bc235a2STomoki Sekiyama 	}
1556bc235a2STomoki Sekiyama 
1566bc235a2STomoki Sekiyama 	/* handle received message */
1576bc235a2STomoki Sekiyama 	switch (buf[0]) {
1586bc235a2STomoki Sekiyama 	case CMD_COUNT:
1596bc235a2STomoki Sekiyama 	case CMD_READ:
1606bc235a2STomoki Sekiyama 		if (buf[6] == CMD_EOF) {
1616bc235a2STomoki Sekiyama 			spin_lock_irqsave(&dev->lock, flags);
1626bc235a2STomoki Sekiyama 			dev->bbu = 0;
1636bc235a2STomoki Sekiyama 			for (i = 1; i < 6; i++) {
1646bc235a2STomoki Sekiyama 				dev->bbu += buf[i];
1656bc235a2STomoki Sekiyama 				if (i != 5)
1666bc235a2STomoki Sekiyama 					dev->bbu <<= 8;
1676bc235a2STomoki Sekiyama 			}
168aadd6472SGreg Kroah-Hartman 			dev_dbg(&dev->interface->dev, "%s count: %lld\n",
169aadd6472SGreg Kroah-Hartman 				__func__, dev->bbu);
1706bc235a2STomoki Sekiyama 			spin_unlock_irqrestore(&dev->lock, flags);
1716bc235a2STomoki Sekiyama 
1726bc235a2STomoki Sekiyama 			kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
1736bc235a2STomoki Sekiyama 		}
1746bc235a2STomoki Sekiyama 		else
175aadd6472SGreg Kroah-Hartman 			dev_dbg(&dev->interface->dev,
176aadd6472SGreg Kroah-Hartman 				"data format error - no EOF\n");
1776bc235a2STomoki Sekiyama 		break;
1786bc235a2STomoki Sekiyama 	case CMD_ACK:
179aadd6472SGreg Kroah-Hartman 		dev_dbg(&dev->interface->dev, "%s ack: %c\n",
180aadd6472SGreg Kroah-Hartman 			__func__, buf[1]);
1816bc235a2STomoki Sekiyama 		wake_up_interruptible(&dev->waitq);
1826bc235a2STomoki Sekiyama 		break;
1836bc235a2STomoki Sekiyama 	}
1846bc235a2STomoki Sekiyama 
1856bc235a2STomoki Sekiyama 	retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
1866bc235a2STomoki Sekiyama 	if (retval) {
18745714104SGreg Kroah-Hartman 		dev_err(&dev->interface->dev, "%s - usb_submit_urb failed: %d\n",
1886bc235a2STomoki Sekiyama 			__func__, retval);
1896bc235a2STomoki Sekiyama 	}
1906bc235a2STomoki Sekiyama }
1916bc235a2STomoki Sekiyama 
1926bc235a2STomoki Sekiyama static int yurex_probe(struct usb_interface *interface, const struct usb_device_id *id)
1936bc235a2STomoki Sekiyama {
1946bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
1956bc235a2STomoki Sekiyama 	struct usb_host_interface *iface_desc;
1966bc235a2STomoki Sekiyama 	struct usb_endpoint_descriptor *endpoint;
1976bc235a2STomoki Sekiyama 	int retval = -ENOMEM;
1986bc235a2STomoki Sekiyama 	DEFINE_WAIT(wait);
199499841e6SJohan Hovold 	int res;
2006bc235a2STomoki Sekiyama 
2016bc235a2STomoki Sekiyama 	/* allocate memory for our device state and initialize it */
2026bc235a2STomoki Sekiyama 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2030c2bc5c2SWolfram Sang 	if (!dev)
2046bc235a2STomoki Sekiyama 		goto error;
2056bc235a2STomoki Sekiyama 	kref_init(&dev->kref);
2066bc235a2STomoki Sekiyama 	mutex_init(&dev->io_mutex);
2076bc235a2STomoki Sekiyama 	spin_lock_init(&dev->lock);
2086bc235a2STomoki Sekiyama 	init_waitqueue_head(&dev->waitq);
2096bc235a2STomoki Sekiyama 
2106bc235a2STomoki Sekiyama 	dev->udev = usb_get_dev(interface_to_usbdev(interface));
211aafb00a9SJohan Hovold 	dev->interface = usb_get_intf(interface);
2126bc235a2STomoki Sekiyama 
2136bc235a2STomoki Sekiyama 	/* set up the endpoint information */
2146bc235a2STomoki Sekiyama 	iface_desc = interface->cur_altsetting;
215499841e6SJohan Hovold 	res = usb_find_int_in_endpoint(iface_desc, &endpoint);
216499841e6SJohan Hovold 	if (res) {
21745714104SGreg Kroah-Hartman 		dev_err(&interface->dev, "Could not find endpoints\n");
218499841e6SJohan Hovold 		retval = res;
2196bc235a2STomoki Sekiyama 		goto error;
2206bc235a2STomoki Sekiyama 	}
2216bc235a2STomoki Sekiyama 
222499841e6SJohan Hovold 	dev->int_in_endpointAddr = endpoint->bEndpointAddress;
2236bc235a2STomoki Sekiyama 
2246bc235a2STomoki Sekiyama 	/* allocate control URB */
2256bc235a2STomoki Sekiyama 	dev->cntl_urb = usb_alloc_urb(0, GFP_KERNEL);
2260450ba40SWolfram Sang 	if (!dev->cntl_urb)
2276bc235a2STomoki Sekiyama 		goto error;
2286bc235a2STomoki Sekiyama 
2296bc235a2STomoki Sekiyama 	/* allocate buffer for control req */
230523fc5c1STomoki Sekiyama 	dev->cntl_req = kmalloc(YUREX_BUF_SIZE, GFP_KERNEL);
2310c2bc5c2SWolfram Sang 	if (!dev->cntl_req)
2326bc235a2STomoki Sekiyama 		goto error;
2336bc235a2STomoki Sekiyama 
2346bc235a2STomoki Sekiyama 	/* allocate buffer for control msg */
2356bc235a2STomoki Sekiyama 	dev->cntl_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
2366bc235a2STomoki Sekiyama 					      GFP_KERNEL,
2376bc235a2STomoki Sekiyama 					      &dev->cntl_urb->transfer_dma);
2386bc235a2STomoki Sekiyama 	if (!dev->cntl_buffer) {
23945714104SGreg Kroah-Hartman 		dev_err(&interface->dev, "Could not allocate cntl_buffer\n");
2406bc235a2STomoki Sekiyama 		goto error;
2416bc235a2STomoki Sekiyama 	}
2426bc235a2STomoki Sekiyama 
2436bc235a2STomoki Sekiyama 	/* configure control URB */
2446bc235a2STomoki Sekiyama 	dev->cntl_req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS |
2456bc235a2STomoki Sekiyama 				      USB_RECIP_INTERFACE;
2466bc235a2STomoki Sekiyama 	dev->cntl_req->bRequest	= HID_REQ_SET_REPORT;
2476bc235a2STomoki Sekiyama 	dev->cntl_req->wValue	= cpu_to_le16((HID_OUTPUT_REPORT + 1) << 8);
2486bc235a2STomoki Sekiyama 	dev->cntl_req->wIndex	= cpu_to_le16(iface_desc->desc.bInterfaceNumber);
2496bc235a2STomoki Sekiyama 	dev->cntl_req->wLength	= cpu_to_le16(YUREX_BUF_SIZE);
2506bc235a2STomoki Sekiyama 
2516bc235a2STomoki Sekiyama 	usb_fill_control_urb(dev->cntl_urb, dev->udev,
2526bc235a2STomoki Sekiyama 			     usb_sndctrlpipe(dev->udev, 0),
2536bc235a2STomoki Sekiyama 			     (void *)dev->cntl_req, dev->cntl_buffer,
2546bc235a2STomoki Sekiyama 			     YUREX_BUF_SIZE, yurex_control_callback, dev);
2556bc235a2STomoki Sekiyama 	dev->cntl_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2566bc235a2STomoki Sekiyama 
2576bc235a2STomoki Sekiyama 
2586bc235a2STomoki Sekiyama 	/* allocate interrupt URB */
2596bc235a2STomoki Sekiyama 	dev->urb = usb_alloc_urb(0, GFP_KERNEL);
2600450ba40SWolfram Sang 	if (!dev->urb)
2616bc235a2STomoki Sekiyama 		goto error;
2626bc235a2STomoki Sekiyama 
2636bc235a2STomoki Sekiyama 	/* allocate buffer for interrupt in */
2646bc235a2STomoki Sekiyama 	dev->int_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
2656bc235a2STomoki Sekiyama 					GFP_KERNEL, &dev->urb->transfer_dma);
2666bc235a2STomoki Sekiyama 	if (!dev->int_buffer) {
26745714104SGreg Kroah-Hartman 		dev_err(&interface->dev, "Could not allocate int_buffer\n");
2686bc235a2STomoki Sekiyama 		goto error;
2696bc235a2STomoki Sekiyama 	}
2706bc235a2STomoki Sekiyama 
2716bc235a2STomoki Sekiyama 	/* configure interrupt URB */
2726bc235a2STomoki Sekiyama 	usb_fill_int_urb(dev->urb, dev->udev,
2736bc235a2STomoki Sekiyama 			 usb_rcvintpipe(dev->udev, dev->int_in_endpointAddr),
2746bc235a2STomoki Sekiyama 			 dev->int_buffer, YUREX_BUF_SIZE, yurex_interrupt,
2756bc235a2STomoki Sekiyama 			 dev, 1);
276532f17b5STomoki Sekiyama 	dev->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
2776bc235a2STomoki Sekiyama 	if (usb_submit_urb(dev->urb, GFP_KERNEL)) {
2786bc235a2STomoki Sekiyama 		retval = -EIO;
27945714104SGreg Kroah-Hartman 		dev_err(&interface->dev, "Could not submitting URB\n");
2806bc235a2STomoki Sekiyama 		goto error;
2816bc235a2STomoki Sekiyama 	}
2826bc235a2STomoki Sekiyama 
2836bc235a2STomoki Sekiyama 	/* save our data pointer in this interface device */
2846bc235a2STomoki Sekiyama 	usb_set_intfdata(interface, dev);
285c78d1ecfSOliver Neukum 	dev->bbu = -1;
2866bc235a2STomoki Sekiyama 
2876bc235a2STomoki Sekiyama 	/* we can register the device now, as it is ready */
2886bc235a2STomoki Sekiyama 	retval = usb_register_dev(interface, &yurex_class);
2896bc235a2STomoki Sekiyama 	if (retval) {
29045714104SGreg Kroah-Hartman 		dev_err(&interface->dev,
29145714104SGreg Kroah-Hartman 			"Not able to get a minor for this device.\n");
2926bc235a2STomoki Sekiyama 		usb_set_intfdata(interface, NULL);
2936bc235a2STomoki Sekiyama 		goto error;
2946bc235a2STomoki Sekiyama 	}
2956bc235a2STomoki Sekiyama 
2966bc235a2STomoki Sekiyama 	dev_info(&interface->dev,
297e06ea97fSTomoki Sekiyama 		 "USB YUREX device now attached to Yurex #%d\n",
2986bc235a2STomoki Sekiyama 		 interface->minor);
2996bc235a2STomoki Sekiyama 
3006bc235a2STomoki Sekiyama 	return 0;
3016bc235a2STomoki Sekiyama 
3026bc235a2STomoki Sekiyama error:
3036bc235a2STomoki Sekiyama 	if (dev)
3046bc235a2STomoki Sekiyama 		/* this frees allocated memory */
3056bc235a2STomoki Sekiyama 		kref_put(&dev->kref, yurex_delete);
3066bc235a2STomoki Sekiyama 	return retval;
3076bc235a2STomoki Sekiyama }
3086bc235a2STomoki Sekiyama 
3096bc235a2STomoki Sekiyama static void yurex_disconnect(struct usb_interface *interface)
3106bc235a2STomoki Sekiyama {
3116bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
3126bc235a2STomoki Sekiyama 	int minor = interface->minor;
3136bc235a2STomoki Sekiyama 
3146bc235a2STomoki Sekiyama 	dev = usb_get_intfdata(interface);
3156bc235a2STomoki Sekiyama 	usb_set_intfdata(interface, NULL);
3166bc235a2STomoki Sekiyama 
3176bc235a2STomoki Sekiyama 	/* give back our minor */
3186bc235a2STomoki Sekiyama 	usb_deregister_dev(interface, &yurex_class);
3196bc235a2STomoki Sekiyama 
3206bc235a2STomoki Sekiyama 	/* prevent more I/O from starting */
321ef61eb43SAlan Stern 	usb_poison_urb(dev->urb);
322aafb00a9SJohan Hovold 	usb_poison_urb(dev->cntl_urb);
3236bc235a2STomoki Sekiyama 	mutex_lock(&dev->io_mutex);
324aafb00a9SJohan Hovold 	dev->disconnected = 1;
3256bc235a2STomoki Sekiyama 	mutex_unlock(&dev->io_mutex);
3266bc235a2STomoki Sekiyama 
3276bc235a2STomoki Sekiyama 	/* wakeup waiters */
3286bc235a2STomoki Sekiyama 	kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
3296bc235a2STomoki Sekiyama 	wake_up_interruptible(&dev->waitq);
3306bc235a2STomoki Sekiyama 
3316bc235a2STomoki Sekiyama 	/* decrement our usage count */
3326bc235a2STomoki Sekiyama 	kref_put(&dev->kref, yurex_delete);
3336bc235a2STomoki Sekiyama 
334e06ea97fSTomoki Sekiyama 	dev_info(&interface->dev, "USB YUREX #%d now disconnected\n", minor);
3356bc235a2STomoki Sekiyama }
3366bc235a2STomoki Sekiyama 
3376bc235a2STomoki Sekiyama static struct usb_driver yurex_driver = {
3386bc235a2STomoki Sekiyama 	.name =		"yurex",
3396bc235a2STomoki Sekiyama 	.probe =	yurex_probe,
3406bc235a2STomoki Sekiyama 	.disconnect =	yurex_disconnect,
3416bc235a2STomoki Sekiyama 	.id_table =	yurex_table,
3426bc235a2STomoki Sekiyama };
3436bc235a2STomoki Sekiyama 
3446bc235a2STomoki Sekiyama 
3456bc235a2STomoki Sekiyama static int yurex_fasync(int fd, struct file *file, int on)
3466bc235a2STomoki Sekiyama {
3476bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
3486bc235a2STomoki Sekiyama 
349113ad911SArjun Sreedharan 	dev = file->private_data;
3506bc235a2STomoki Sekiyama 	return fasync_helper(fd, file, on, &dev->async_queue);
3516bc235a2STomoki Sekiyama }
3526bc235a2STomoki Sekiyama 
3536bc235a2STomoki Sekiyama static int yurex_open(struct inode *inode, struct file *file)
3546bc235a2STomoki Sekiyama {
3556bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
3566bc235a2STomoki Sekiyama 	struct usb_interface *interface;
3576bc235a2STomoki Sekiyama 	int subminor;
3586bc235a2STomoki Sekiyama 	int retval = 0;
3596bc235a2STomoki Sekiyama 
3606bc235a2STomoki Sekiyama 	subminor = iminor(inode);
3616bc235a2STomoki Sekiyama 
3626bc235a2STomoki Sekiyama 	interface = usb_find_interface(&yurex_driver, subminor);
3636bc235a2STomoki Sekiyama 	if (!interface) {
36445714104SGreg Kroah-Hartman 		printk(KERN_ERR "%s - error, can't find device for minor %d",
3656bc235a2STomoki Sekiyama 		       __func__, subminor);
3666bc235a2STomoki Sekiyama 		retval = -ENODEV;
3676bc235a2STomoki Sekiyama 		goto exit;
3686bc235a2STomoki Sekiyama 	}
3696bc235a2STomoki Sekiyama 
3706bc235a2STomoki Sekiyama 	dev = usb_get_intfdata(interface);
3716bc235a2STomoki Sekiyama 	if (!dev) {
3726bc235a2STomoki Sekiyama 		retval = -ENODEV;
3736bc235a2STomoki Sekiyama 		goto exit;
3746bc235a2STomoki Sekiyama 	}
3756bc235a2STomoki Sekiyama 
3766bc235a2STomoki Sekiyama 	/* increment our usage count for the device */
3776bc235a2STomoki Sekiyama 	kref_get(&dev->kref);
3786bc235a2STomoki Sekiyama 
3796bc235a2STomoki Sekiyama 	/* save our object in the file's private structure */
3806bc235a2STomoki Sekiyama 	mutex_lock(&dev->io_mutex);
3816bc235a2STomoki Sekiyama 	file->private_data = dev;
3826bc235a2STomoki Sekiyama 	mutex_unlock(&dev->io_mutex);
3836bc235a2STomoki Sekiyama 
3846bc235a2STomoki Sekiyama exit:
3856bc235a2STomoki Sekiyama 	return retval;
3866bc235a2STomoki Sekiyama }
3876bc235a2STomoki Sekiyama 
3886bc235a2STomoki Sekiyama static int yurex_release(struct inode *inode, struct file *file)
3896bc235a2STomoki Sekiyama {
3906bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
3916bc235a2STomoki Sekiyama 
392113ad911SArjun Sreedharan 	dev = file->private_data;
3936bc235a2STomoki Sekiyama 	if (dev == NULL)
3946bc235a2STomoki Sekiyama 		return -ENODEV;
3956bc235a2STomoki Sekiyama 
3966bc235a2STomoki Sekiyama 	/* decrement the count on our device */
3976bc235a2STomoki Sekiyama 	kref_put(&dev->kref, yurex_delete);
3986bc235a2STomoki Sekiyama 	return 0;
3996bc235a2STomoki Sekiyama }
4006bc235a2STomoki Sekiyama 
4011cc373c6SSudip Mukherjee static ssize_t yurex_read(struct file *file, char __user *buffer, size_t count,
4021cc373c6SSudip Mukherjee 			  loff_t *ppos)
4036bc235a2STomoki Sekiyama {
4046bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
405f1e255d6SJann Horn 	int len = 0;
406*eff6dde4SLee Jones 	char in_buffer[MAX_S64_STRLEN];
4076bc235a2STomoki Sekiyama 	unsigned long flags;
4086bc235a2STomoki Sekiyama 
409113ad911SArjun Sreedharan 	dev = file->private_data;
4106bc235a2STomoki Sekiyama 
4116bc235a2STomoki Sekiyama 	mutex_lock(&dev->io_mutex);
412aafb00a9SJohan Hovold 	if (dev->disconnected) {		/* already disconnected */
413f1e255d6SJann Horn 		mutex_unlock(&dev->io_mutex);
414f1e255d6SJann Horn 		return -ENODEV;
4156bc235a2STomoki Sekiyama 	}
4166bc235a2STomoki Sekiyama 
417*eff6dde4SLee Jones 	if (WARN_ON_ONCE(dev->bbu > S64_MAX || dev->bbu < S64_MIN))
418*eff6dde4SLee Jones 		return -EIO;
419*eff6dde4SLee Jones 
4206bc235a2STomoki Sekiyama 	spin_lock_irqsave(&dev->lock, flags);
421*eff6dde4SLee Jones 	scnprintf(in_buffer, MAX_S64_STRLEN, "%lld\n", dev->bbu);
4226bc235a2STomoki Sekiyama 	spin_unlock_irqrestore(&dev->lock, flags);
4236bc235a2STomoki Sekiyama 	mutex_unlock(&dev->io_mutex);
424f1e255d6SJann Horn 
425f1e255d6SJann Horn 	return simple_read_from_buffer(buffer, count, ppos, in_buffer, len);
4266bc235a2STomoki Sekiyama }
4276bc235a2STomoki Sekiyama 
4281cc373c6SSudip Mukherjee static ssize_t yurex_write(struct file *file, const char __user *user_buffer,
4291cc373c6SSudip Mukherjee 			   size_t count, loff_t *ppos)
4306bc235a2STomoki Sekiyama {
4316bc235a2STomoki Sekiyama 	struct usb_yurex *dev;
4326bc235a2STomoki Sekiyama 	int i, set = 0, retval = 0;
4337e10f14eSBen Hutchings 	char buffer[16 + 1];
4346bc235a2STomoki Sekiyama 	char *data = buffer;
4356bc235a2STomoki Sekiyama 	unsigned long long c, c2 = 0;
4366bc235a2STomoki Sekiyama 	signed long timeout = 0;
4376bc235a2STomoki Sekiyama 	DEFINE_WAIT(wait);
4386bc235a2STomoki Sekiyama 
4397e10f14eSBen Hutchings 	count = min(sizeof(buffer) - 1, count);
440113ad911SArjun Sreedharan 	dev = file->private_data;
4416bc235a2STomoki Sekiyama 
4426bc235a2STomoki Sekiyama 	/* verify that we actually have some data to write */
4436bc235a2STomoki Sekiyama 	if (count == 0)
4446bc235a2STomoki Sekiyama 		goto error;
4456bc235a2STomoki Sekiyama 
4466bc235a2STomoki Sekiyama 	mutex_lock(&dev->io_mutex);
447aafb00a9SJohan Hovold 	if (dev->disconnected) {		/* already disconnected */
4486bc235a2STomoki Sekiyama 		mutex_unlock(&dev->io_mutex);
4496bc235a2STomoki Sekiyama 		retval = -ENODEV;
4506bc235a2STomoki Sekiyama 		goto error;
4516bc235a2STomoki Sekiyama 	}
4526bc235a2STomoki Sekiyama 
4536bc235a2STomoki Sekiyama 	if (copy_from_user(buffer, user_buffer, count)) {
4546bc235a2STomoki Sekiyama 		mutex_unlock(&dev->io_mutex);
4556bc235a2STomoki Sekiyama 		retval = -EFAULT;
4566bc235a2STomoki Sekiyama 		goto error;
4576bc235a2STomoki Sekiyama 	}
4587e10f14eSBen Hutchings 	buffer[count] = 0;
4596bc235a2STomoki Sekiyama 	memset(dev->cntl_buffer, CMD_PADDING, YUREX_BUF_SIZE);
4606bc235a2STomoki Sekiyama 
4616bc235a2STomoki Sekiyama 	switch (buffer[0]) {
4626bc235a2STomoki Sekiyama 	case CMD_ANIMATE:
4636bc235a2STomoki Sekiyama 	case CMD_LED:
4646bc235a2STomoki Sekiyama 		dev->cntl_buffer[0] = buffer[0];
4656bc235a2STomoki Sekiyama 		dev->cntl_buffer[1] = buffer[1];
4666bc235a2STomoki Sekiyama 		dev->cntl_buffer[2] = CMD_EOF;
4676bc235a2STomoki Sekiyama 		break;
4686bc235a2STomoki Sekiyama 	case CMD_READ:
4696bc235a2STomoki Sekiyama 	case CMD_VERSION:
4706bc235a2STomoki Sekiyama 		dev->cntl_buffer[0] = buffer[0];
4716bc235a2STomoki Sekiyama 		dev->cntl_buffer[1] = 0x00;
4726bc235a2STomoki Sekiyama 		dev->cntl_buffer[2] = CMD_EOF;
4736bc235a2STomoki Sekiyama 		break;
4746bc235a2STomoki Sekiyama 	case CMD_SET:
4756bc235a2STomoki Sekiyama 		data++;
4760d9b6d49SGustavo A. R. Silva 		fallthrough;
4776bc235a2STomoki Sekiyama 	case '0' ... '9':
4786bc235a2STomoki Sekiyama 		set = 1;
4796bc235a2STomoki Sekiyama 		c = c2 = simple_strtoull(data, NULL, 0);
4806bc235a2STomoki Sekiyama 		dev->cntl_buffer[0] = CMD_SET;
4816bc235a2STomoki Sekiyama 		for (i = 1; i < 6; i++) {
4826bc235a2STomoki Sekiyama 			dev->cntl_buffer[i] = (c>>32) & 0xff;
4836bc235a2STomoki Sekiyama 			c <<= 8;
4846bc235a2STomoki Sekiyama 		}
4856bc235a2STomoki Sekiyama 		buffer[6] = CMD_EOF;
4866bc235a2STomoki Sekiyama 		break;
4876bc235a2STomoki Sekiyama 	default:
4886bc235a2STomoki Sekiyama 		mutex_unlock(&dev->io_mutex);
4896bc235a2STomoki Sekiyama 		return -EINVAL;
4906bc235a2STomoki Sekiyama 	}
4916bc235a2STomoki Sekiyama 
4926bc235a2STomoki Sekiyama 	/* send the data as the control msg */
4936bc235a2STomoki Sekiyama 	prepare_to_wait(&dev->waitq, &wait, TASK_INTERRUPTIBLE);
494aadd6472SGreg Kroah-Hartman 	dev_dbg(&dev->interface->dev, "%s - submit %c\n", __func__,
495aadd6472SGreg Kroah-Hartman 		dev->cntl_buffer[0]);
496f176ede3SAlan Stern 	retval = usb_submit_urb(dev->cntl_urb, GFP_ATOMIC);
4976bc235a2STomoki Sekiyama 	if (retval >= 0)
4986bc235a2STomoki Sekiyama 		timeout = schedule_timeout(YUREX_WRITE_TIMEOUT);
4996bc235a2STomoki Sekiyama 	finish_wait(&dev->waitq, &wait);
5006bc235a2STomoki Sekiyama 
501372c9313SJohan Hovold 	/* make sure URB is idle after timeout or (spurious) CMD_ACK */
502372c9313SJohan Hovold 	usb_kill_urb(dev->cntl_urb);
503372c9313SJohan Hovold 
5046bc235a2STomoki Sekiyama 	mutex_unlock(&dev->io_mutex);
5056bc235a2STomoki Sekiyama 
5066bc235a2STomoki Sekiyama 	if (retval < 0) {
50745714104SGreg Kroah-Hartman 		dev_err(&dev->interface->dev,
50845714104SGreg Kroah-Hartman 			"%s - failed to send bulk msg, error %d\n",
50945714104SGreg Kroah-Hartman 			__func__, retval);
5106bc235a2STomoki Sekiyama 		goto error;
5116bc235a2STomoki Sekiyama 	}
5126bc235a2STomoki Sekiyama 	if (set && timeout)
5136bc235a2STomoki Sekiyama 		dev->bbu = c2;
5146bc235a2STomoki Sekiyama 	return timeout ? count : -EIO;
5156bc235a2STomoki Sekiyama 
5166bc235a2STomoki Sekiyama error:
5176bc235a2STomoki Sekiyama 	return retval;
5186bc235a2STomoki Sekiyama }
5196bc235a2STomoki Sekiyama 
5206bc235a2STomoki Sekiyama static const struct file_operations yurex_fops = {
5216bc235a2STomoki Sekiyama 	.owner =	THIS_MODULE,
5226bc235a2STomoki Sekiyama 	.read =		yurex_read,
5236bc235a2STomoki Sekiyama 	.write =	yurex_write,
5246bc235a2STomoki Sekiyama 	.open =		yurex_open,
5256bc235a2STomoki Sekiyama 	.release =	yurex_release,
5266bc235a2STomoki Sekiyama 	.fasync	=	yurex_fasync,
52727f485b5STomoki Sekiyama 	.llseek =	default_llseek,
5286bc235a2STomoki Sekiyama };
5296bc235a2STomoki Sekiyama 
53065db4305SGreg Kroah-Hartman module_usb_driver(yurex_driver);
5316bc235a2STomoki Sekiyama 
5326bc235a2STomoki Sekiyama MODULE_LICENSE("GPL");
533