xref: /kernel/linux/linux-5.10/drivers/usb/misc/yurex.c (revision 8c2ecf20)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for Meywa-Denki & KAYAC YUREX
4 *
5 * Copyright (C) 2010 Tomoki Sekiyama (tomoki.sekiyama@gmail.com)
6 */
7
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>
17
18#define DRIVER_AUTHOR "Tomoki Sekiyama"
19#define DRIVER_DESC "Driver for Meywa-Denki & KAYAC YUREX"
20
21#define YUREX_VENDOR_ID		0x0c45
22#define YUREX_PRODUCT_ID	0x1010
23
24#define CMD_ACK		'!'
25#define CMD_ANIMATE	'A'
26#define CMD_COUNT	'C'
27#define CMD_LED		'L'
28#define CMD_READ	'R'
29#define CMD_SET		'S'
30#define CMD_VERSION	'V'
31#define CMD_EOF		0x0d
32#define CMD_PADDING	0xff
33
34#define YUREX_BUF_SIZE		8
35#define YUREX_WRITE_TIMEOUT	(HZ*2)
36
37/* table of devices that work with this driver */
38static struct usb_device_id yurex_table[] = {
39	{ USB_DEVICE(YUREX_VENDOR_ID, YUREX_PRODUCT_ID) },
40	{ }					/* Terminating entry */
41};
42MODULE_DEVICE_TABLE(usb, yurex_table);
43
44#ifdef CONFIG_USB_DYNAMIC_MINORS
45#define YUREX_MINOR_BASE	0
46#else
47#define YUREX_MINOR_BASE	192
48#endif
49
50/* Structure to hold all of our device specific stuff */
51struct usb_yurex {
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 */
60
61	struct kref		kref;
62	struct mutex		io_mutex;
63	unsigned long		disconnected:1;
64	struct fasync_struct	*async_queue;
65	wait_queue_head_t	waitq;
66
67	spinlock_t		lock;
68	__s64			bbu;		/* BBU from device */
69};
70#define to_yurex_dev(d) container_of(d, struct usb_yurex, kref)
71
72static struct usb_driver yurex_driver;
73static const struct file_operations yurex_fops;
74
75
76static void yurex_control_callback(struct urb *urb)
77{
78	struct usb_yurex *dev = urb->context;
79	int status = urb->status;
80
81	if (status) {
82		dev_err(&urb->dev->dev, "%s - control failed: %d\n",
83			__func__, status);
84		wake_up_interruptible(&dev->waitq);
85		return;
86	}
87	/* on success, sender woken up by CMD_ACK int in, or timeout */
88}
89
90static void yurex_delete(struct kref *kref)
91{
92	struct usb_yurex *dev = to_yurex_dev(kref);
93
94	dev_dbg(&dev->interface->dev, "%s\n", __func__);
95
96	if (dev->cntl_urb) {
97		usb_kill_urb(dev->cntl_urb);
98		kfree(dev->cntl_req);
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);
102	}
103	if (dev->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);
108	}
109	usb_put_intf(dev->interface);
110	usb_put_dev(dev->udev);
111	kfree(dev);
112}
113
114/*
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
117 */
118static struct usb_class_driver yurex_class = {
119	.name =		"yurex%d",
120	.fops =		&yurex_fops,
121	.minor_base =	YUREX_MINOR_BASE,
122};
123
124static void yurex_interrupt(struct urb *urb)
125{
126	struct usb_yurex *dev = urb->context;
127	unsigned char *buf = dev->int_buffer;
128	int status = urb->status;
129	unsigned long flags;
130	int retval, i;
131
132	switch (status) {
133	case 0: /*success*/
134		break;
135	/* The device is terminated or messed up, give up */
136	case -EOVERFLOW:
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);
140	case -ECONNRESET:
141	case -ENOENT:
142	case -ESHUTDOWN:
143	case -EILSEQ:
144	case -EPROTO:
145	case -ETIME:
146		return;
147	default:
148		dev_err(&dev->interface->dev,
149			"%s - unknown status received: %d\n", __func__, status);
150		return;
151	}
152
153	/* handle received message */
154	switch (buf[0]) {
155	case CMD_COUNT:
156	case CMD_READ:
157		if (buf[6] == CMD_EOF) {
158			spin_lock_irqsave(&dev->lock, flags);
159			dev->bbu = 0;
160			for (i = 1; i < 6; i++) {
161				dev->bbu += buf[i];
162				if (i != 5)
163					dev->bbu <<= 8;
164			}
165			dev_dbg(&dev->interface->dev, "%s count: %lld\n",
166				__func__, dev->bbu);
167			spin_unlock_irqrestore(&dev->lock, flags);
168
169			kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
170		}
171		else
172			dev_dbg(&dev->interface->dev,
173				"data format error - no EOF\n");
174		break;
175	case CMD_ACK:
176		dev_dbg(&dev->interface->dev, "%s ack: %c\n",
177			__func__, buf[1]);
178		wake_up_interruptible(&dev->waitq);
179		break;
180	}
181
182	retval = usb_submit_urb(dev->urb, GFP_ATOMIC);
183	if (retval) {
184		dev_err(&dev->interface->dev, "%s - usb_submit_urb failed: %d\n",
185			__func__, retval);
186	}
187}
188
189static int yurex_probe(struct usb_interface *interface, const struct usb_device_id *id)
190{
191	struct usb_yurex *dev;
192	struct usb_host_interface *iface_desc;
193	struct usb_endpoint_descriptor *endpoint;
194	int retval = -ENOMEM;
195	DEFINE_WAIT(wait);
196	int res;
197
198	/* allocate memory for our device state and initialize it */
199	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
200	if (!dev)
201		goto error;
202	kref_init(&dev->kref);
203	mutex_init(&dev->io_mutex);
204	spin_lock_init(&dev->lock);
205	init_waitqueue_head(&dev->waitq);
206
207	dev->udev = usb_get_dev(interface_to_usbdev(interface));
208	dev->interface = usb_get_intf(interface);
209
210	/* set up the endpoint information */
211	iface_desc = interface->cur_altsetting;
212	res = usb_find_int_in_endpoint(iface_desc, &endpoint);
213	if (res) {
214		dev_err(&interface->dev, "Could not find endpoints\n");
215		retval = res;
216		goto error;
217	}
218
219	dev->int_in_endpointAddr = endpoint->bEndpointAddress;
220
221	/* allocate control URB */
222	dev->cntl_urb = usb_alloc_urb(0, GFP_KERNEL);
223	if (!dev->cntl_urb)
224		goto error;
225
226	/* allocate buffer for control req */
227	dev->cntl_req = kmalloc(YUREX_BUF_SIZE, GFP_KERNEL);
228	if (!dev->cntl_req)
229		goto error;
230
231	/* allocate buffer for control msg */
232	dev->cntl_buffer = usb_alloc_coherent(dev->udev, YUREX_BUF_SIZE,
233					      GFP_KERNEL,
234					      &dev->cntl_urb->transfer_dma);
235	if (!dev->cntl_buffer) {
236		dev_err(&interface->dev, "Could not allocate cntl_buffer\n");
237		goto error;
238	}
239
240	/* configure control URB */
241	dev->cntl_req->bRequestType = USB_DIR_OUT | USB_TYPE_CLASS |
242				      USB_RECIP_INTERFACE;
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);
247
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;
253
254
255	/* allocate interrupt URB */
256	dev->urb = usb_alloc_urb(0, GFP_KERNEL);
257	if (!dev->urb)
258		goto error;
259
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");
265		goto error;
266	}
267
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,
272			 dev, 1);
273	dev->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
274	if (usb_submit_urb(dev->urb, GFP_KERNEL)) {
275		retval = -EIO;
276		dev_err(&interface->dev, "Could not submitting URB\n");
277		goto error;
278	}
279
280	/* save our data pointer in this interface device */
281	usb_set_intfdata(interface, dev);
282	dev->bbu = -1;
283
284	/* we can register the device now, as it is ready */
285	retval = usb_register_dev(interface, &yurex_class);
286	if (retval) {
287		dev_err(&interface->dev,
288			"Not able to get a minor for this device.\n");
289		usb_set_intfdata(interface, NULL);
290		goto error;
291	}
292
293	dev_info(&interface->dev,
294		 "USB YUREX device now attached to Yurex #%d\n",
295		 interface->minor);
296
297	return 0;
298
299error:
300	if (dev)
301		/* this frees allocated memory */
302		kref_put(&dev->kref, yurex_delete);
303	return retval;
304}
305
306static void yurex_disconnect(struct usb_interface *interface)
307{
308	struct usb_yurex *dev;
309	int minor = interface->minor;
310
311	dev = usb_get_intfdata(interface);
312	usb_set_intfdata(interface, NULL);
313
314	/* give back our minor */
315	usb_deregister_dev(interface, &yurex_class);
316
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);
323
324	/* wakeup waiters */
325	kill_fasync(&dev->async_queue, SIGIO, POLL_IN);
326	wake_up_interruptible(&dev->waitq);
327
328	/* decrement our usage count */
329	kref_put(&dev->kref, yurex_delete);
330
331	dev_info(&interface->dev, "USB YUREX #%d now disconnected\n", minor);
332}
333
334static struct usb_driver yurex_driver = {
335	.name =		"yurex",
336	.probe =	yurex_probe,
337	.disconnect =	yurex_disconnect,
338	.id_table =	yurex_table,
339};
340
341
342static int yurex_fasync(int fd, struct file *file, int on)
343{
344	struct usb_yurex *dev;
345
346	dev = file->private_data;
347	return fasync_helper(fd, file, on, &dev->async_queue);
348}
349
350static int yurex_open(struct inode *inode, struct file *file)
351{
352	struct usb_yurex *dev;
353	struct usb_interface *interface;
354	int subminor;
355	int retval = 0;
356
357	subminor = iminor(inode);
358
359	interface = usb_find_interface(&yurex_driver, subminor);
360	if (!interface) {
361		printk(KERN_ERR "%s - error, can't find device for minor %d",
362		       __func__, subminor);
363		retval = -ENODEV;
364		goto exit;
365	}
366
367	dev = usb_get_intfdata(interface);
368	if (!dev) {
369		retval = -ENODEV;
370		goto exit;
371	}
372
373	/* increment our usage count for the device */
374	kref_get(&dev->kref);
375
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);
380
381exit:
382	return retval;
383}
384
385static int yurex_release(struct inode *inode, struct file *file)
386{
387	struct usb_yurex *dev;
388
389	dev = file->private_data;
390	if (dev == NULL)
391		return -ENODEV;
392
393	/* decrement the count on our device */
394	kref_put(&dev->kref, yurex_delete);
395	return 0;
396}
397
398static ssize_t yurex_read(struct file *file, char __user *buffer, size_t count,
399			  loff_t *ppos)
400{
401	struct usb_yurex *dev;
402	int len = 0;
403	char in_buffer[20];
404	unsigned long flags;
405
406	dev = file->private_data;
407
408	mutex_lock(&dev->io_mutex);
409	if (dev->disconnected) {		/* already disconnected */
410		mutex_unlock(&dev->io_mutex);
411		return -ENODEV;
412	}
413
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);
418
419	if (WARN_ON_ONCE(len >= sizeof(in_buffer)))
420		return -EIO;
421
422	return simple_read_from_buffer(buffer, count, ppos, in_buffer, len);
423}
424
425static ssize_t yurex_write(struct file *file, const char __user *user_buffer,
426			   size_t count, loff_t *ppos)
427{
428	struct usb_yurex *dev;
429	int i, set = 0, retval = 0;
430	char buffer[16 + 1];
431	char *data = buffer;
432	unsigned long long c, c2 = 0;
433	signed long timeout = 0;
434	DEFINE_WAIT(wait);
435
436	count = min(sizeof(buffer) - 1, count);
437	dev = file->private_data;
438
439	/* verify that we actually have some data to write */
440	if (count == 0)
441		goto error;
442
443	mutex_lock(&dev->io_mutex);
444	if (dev->disconnected) {		/* already disconnected */
445		mutex_unlock(&dev->io_mutex);
446		retval = -ENODEV;
447		goto error;
448	}
449
450	if (copy_from_user(buffer, user_buffer, count)) {
451		mutex_unlock(&dev->io_mutex);
452		retval = -EFAULT;
453		goto error;
454	}
455	buffer[count] = 0;
456	memset(dev->cntl_buffer, CMD_PADDING, YUREX_BUF_SIZE);
457
458	switch (buffer[0]) {
459	case CMD_ANIMATE:
460	case CMD_LED:
461		dev->cntl_buffer[0] = buffer[0];
462		dev->cntl_buffer[1] = buffer[1];
463		dev->cntl_buffer[2] = CMD_EOF;
464		break;
465	case CMD_READ:
466	case CMD_VERSION:
467		dev->cntl_buffer[0] = buffer[0];
468		dev->cntl_buffer[1] = 0x00;
469		dev->cntl_buffer[2] = CMD_EOF;
470		break;
471	case CMD_SET:
472		data++;
473		fallthrough;
474	case '0' ... '9':
475		set = 1;
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;
480			c <<= 8;
481		}
482		buffer[6] = CMD_EOF;
483		break;
484	default:
485		mutex_unlock(&dev->io_mutex);
486		return -EINVAL;
487	}
488
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);
494	if (retval >= 0)
495		timeout = schedule_timeout(YUREX_WRITE_TIMEOUT);
496	finish_wait(&dev->waitq, &wait);
497
498	/* make sure URB is idle after timeout or (spurious) CMD_ACK */
499	usb_kill_urb(dev->cntl_urb);
500
501	mutex_unlock(&dev->io_mutex);
502
503	if (retval < 0) {
504		dev_err(&dev->interface->dev,
505			"%s - failed to send bulk msg, error %d\n",
506			__func__, retval);
507		goto error;
508	}
509	if (set && timeout)
510		dev->bbu = c2;
511	return timeout ? count : -EIO;
512
513error:
514	return retval;
515}
516
517static const struct file_operations yurex_fops = {
518	.owner =	THIS_MODULE,
519	.read =		yurex_read,
520	.write =	yurex_write,
521	.open =		yurex_open,
522	.release =	yurex_release,
523	.fasync	=	yurex_fasync,
524	.llseek =	default_llseek,
525};
526
527module_usb_driver(yurex_driver);
528
529MODULE_LICENSE("GPL");
530