xref: /linux/drivers/usb/misc/iowarrior.c (revision d4cdecadbd400a017c3d2a0150fd16d973950e5d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Native support for the I/O-Warrior USB devices
4  *
5  *  Copyright (c) 2003-2005, 2020  Code Mercenaries GmbH
6  *  written by Christian Lucht <lucht@codemercs.com> and
7  *  Christoph Jung <jung@codemercs.com>
8  *
9  *  based on
10 
11  *  usb-skeleton.c by Greg Kroah-Hartman  <greg@kroah.com>
12  *  brlvger.c by Stephane Dalton  <sdalton@videotron.ca>
13  *           and Stephane Doyon   <s.doyon@videotron.ca>
14  *
15  *  Released under the GPLv2.
16  */
17 
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/slab.h>
21 #include <linux/sched.h>
22 #include <linux/mutex.h>
23 #include <linux/poll.h>
24 #include <linux/hid.h>
25 #include <linux/usb/iowarrior.h>
26 
27 #define DRIVER_AUTHOR "Christian Lucht <lucht@codemercs.com>"
28 #define DRIVER_DESC "USB IO-Warrior driver"
29 
30 #define USB_VENDOR_ID_CODEMERCS		1984
31 /* low speed iowarrior */
32 #define USB_DEVICE_ID_CODEMERCS_IOW40	0x1500
33 #define USB_DEVICE_ID_CODEMERCS_IOW24	0x1501
34 #define USB_DEVICE_ID_CODEMERCS_IOWPV1	0x1511
35 #define USB_DEVICE_ID_CODEMERCS_IOWPV2	0x1512
36 /* full speed iowarrior */
37 #define USB_DEVICE_ID_CODEMERCS_IOW56	0x1503
38 /* fuller speed iowarrior */
39 #define USB_DEVICE_ID_CODEMERCS_IOW28	0x1504
40 #define USB_DEVICE_ID_CODEMERCS_IOW28L	0x1505
41 #define USB_DEVICE_ID_CODEMERCS_IOW100	0x1506
42 
43 /* OEMed devices */
44 #define USB_DEVICE_ID_CODEMERCS_IOW24SAG	0x158a
45 #define USB_DEVICE_ID_CODEMERCS_IOW56AM		0x158b
46 
47 /* Get a minor range for your devices from the usb maintainer */
48 #ifdef CONFIG_USB_DYNAMIC_MINORS
49 #define IOWARRIOR_MINOR_BASE	0
50 #else
51 #define IOWARRIOR_MINOR_BASE	208	// SKELETON_MINOR_BASE 192 + 16, not official yet
52 #endif
53 
54 /* interrupt input queue size */
55 #define MAX_INTERRUPT_BUFFER 16
56 /*
57    maximum number of urbs that are submitted for writes at the same time,
58    this applies to the IOWarrior56 only!
59    IOWarrior24 and IOWarrior40 use synchronous usb_control_msg calls.
60 */
61 #define MAX_WRITES_IN_FLIGHT 4
62 
63 MODULE_AUTHOR(DRIVER_AUTHOR);
64 MODULE_DESCRIPTION(DRIVER_DESC);
65 MODULE_LICENSE("GPL");
66 
67 static struct usb_driver iowarrior_driver;
68 
69 /*--------------*/
70 /*     data     */
71 /*--------------*/
72 
73 /* Structure to hold all of our device specific stuff */
74 struct iowarrior {
75 	struct mutex mutex;			/* locks this structure */
76 	struct usb_device *udev;		/* save off the usb device pointer */
77 	struct usb_interface *interface;	/* the interface for this device */
78 	struct usb_endpoint_descriptor *int_out_endpoint;	/* endpoint for reading (needed for IOW56 only) */
79 	struct usb_endpoint_descriptor *int_in_endpoint;	/* endpoint for reading */
80 	struct urb *int_in_urb;		/* the urb for reading data */
81 	unsigned char *int_in_buffer;	/* buffer for data to be read */
82 	unsigned char serial_number;	/* to detect lost packages */
83 	unsigned char *read_queue;	/* size is MAX_INTERRUPT_BUFFER * packet size */
84 	wait_queue_head_t read_wait;
85 	wait_queue_head_t write_wait;	/* wait-queue for writing to the device */
86 	atomic_t write_busy;		/* number of write-urbs submitted */
87 	atomic_t read_idx;
88 	atomic_t intr_idx;
89 	atomic_t overflow_flag;		/* signals an index 'rollover' */
90 	int present;			/* this is 1 as long as the device is connected */
91 	int opened;			/* this is 1 if the device is currently open */
92 	char chip_serial[9];		/* the serial number string of the chip connected */
93 	int report_size;		/* number of bytes in a report */
94 	u16 product_id;
95 	struct usb_anchor submitted;
96 };
97 
98 /*--------------*/
99 /*    globals   */
100 /*--------------*/
101 
102 //#if 0
103 static int usb_get_report(struct usb_device *dev,
104 			  struct usb_host_interface *inter, unsigned char type,
105 			  unsigned char id, void *buf, int size)
106 {
107 	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
108 			       HID_REQ_GET_REPORT,
109 			       USB_DIR_IN | USB_TYPE_CLASS |
110 			       USB_RECIP_INTERFACE, (type << 8) + id,
111 			       inter->desc.bInterfaceNumber, buf, size,
112 			       USB_CTRL_GET_TIMEOUT);
113 }
114 //#endif
115 
116 static int usb_set_report(struct usb_interface *intf, unsigned char type,
117 			  unsigned char id, void *buf, int size)
118 {
119 	return usb_control_msg(interface_to_usbdev(intf),
120 			       usb_sndctrlpipe(interface_to_usbdev(intf), 0),
121 			       HID_REQ_SET_REPORT,
122 			       USB_TYPE_CLASS | USB_RECIP_INTERFACE,
123 			       (type << 8) + id,
124 			       intf->cur_altsetting->desc.bInterfaceNumber, buf,
125 			       size, 1000);
126 }
127 
128 /*---------------------*/
129 /* driver registration */
130 /*---------------------*/
131 /* table of devices that work with this driver */
132 static const struct usb_device_id iowarrior_ids[] = {
133 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW40)},
134 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24)},
135 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV1)},
136 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOWPV2)},
137 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56)},
138 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW24SAG)},
139 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW56AM)},
140 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28)},
141 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW28L)},
142 	{USB_DEVICE(USB_VENDOR_ID_CODEMERCS, USB_DEVICE_ID_CODEMERCS_IOW100)},
143 	{}			/* Terminating entry */
144 };
145 MODULE_DEVICE_TABLE(usb, iowarrior_ids);
146 
147 /*
148  * USB callback handler for reading data
149  */
150 static void iowarrior_callback(struct urb *urb)
151 {
152 	struct iowarrior *dev = urb->context;
153 	int intr_idx;
154 	int read_idx;
155 	int aux_idx;
156 	int offset;
157 	int status = urb->status;
158 	int retval;
159 
160 	switch (status) {
161 	case 0:
162 		/* success */
163 		break;
164 	case -ECONNRESET:
165 	case -ENOENT:
166 	case -ESHUTDOWN:
167 		return;
168 	default:
169 		goto exit;
170 	}
171 
172 	intr_idx = atomic_read(&dev->intr_idx);
173 	/* aux_idx become previous intr_idx */
174 	aux_idx = (intr_idx == 0) ? (MAX_INTERRUPT_BUFFER - 1) : (intr_idx - 1);
175 	read_idx = atomic_read(&dev->read_idx);
176 
177 	/* queue is not empty and it's interface 0 */
178 	if ((intr_idx != read_idx)
179 	    && (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0)) {
180 		/* + 1 for serial number */
181 		offset = aux_idx * (dev->report_size + 1);
182 		if (!memcmp
183 		    (dev->read_queue + offset, urb->transfer_buffer,
184 		     dev->report_size)) {
185 			/* equal values on interface 0 will be ignored */
186 			goto exit;
187 		}
188 	}
189 
190 	/* aux_idx become next intr_idx */
191 	aux_idx = (intr_idx == (MAX_INTERRUPT_BUFFER - 1)) ? 0 : (intr_idx + 1);
192 	if (read_idx == aux_idx) {
193 		/* queue full, dropping oldest input */
194 		read_idx = (++read_idx == MAX_INTERRUPT_BUFFER) ? 0 : read_idx;
195 		atomic_set(&dev->read_idx, read_idx);
196 		atomic_set(&dev->overflow_flag, 1);
197 	}
198 
199 	/* +1 for serial number */
200 	offset = intr_idx * (dev->report_size + 1);
201 	memcpy(dev->read_queue + offset, urb->transfer_buffer,
202 	       dev->report_size);
203 	*(dev->read_queue + offset + (dev->report_size)) = dev->serial_number++;
204 
205 	atomic_set(&dev->intr_idx, aux_idx);
206 	/* tell the blocking read about the new data */
207 	wake_up_interruptible(&dev->read_wait);
208 
209 exit:
210 	retval = usb_submit_urb(urb, GFP_ATOMIC);
211 	if (retval)
212 		dev_err(&dev->interface->dev, "%s - usb_submit_urb failed with result %d\n",
213 			__func__, retval);
214 
215 }
216 
217 /*
218  * USB Callback handler for write-ops
219  */
220 static void iowarrior_write_callback(struct urb *urb)
221 {
222 	struct iowarrior *dev;
223 	int status = urb->status;
224 
225 	dev = urb->context;
226 	/* sync/async unlink faults aren't errors */
227 	if (status &&
228 	    !(status == -ENOENT ||
229 	      status == -ECONNRESET || status == -ESHUTDOWN)) {
230 		dev_dbg(&dev->interface->dev,
231 			"nonzero write bulk status received: %d\n", status);
232 	}
233 	/* free up our allocated buffer */
234 	kfree(urb->transfer_buffer);
235 	/* tell a waiting writer the interrupt-out-pipe is available again */
236 	atomic_dec(&dev->write_busy);
237 	wake_up_interruptible(&dev->write_wait);
238 }
239 
240 /*
241  *	iowarrior_delete
242  */
243 static inline void iowarrior_delete(struct iowarrior *dev)
244 {
245 	kfree(dev->int_in_buffer);
246 	usb_free_urb(dev->int_in_urb);
247 	kfree(dev->read_queue);
248 	usb_put_intf(dev->interface);
249 	kfree(dev);
250 }
251 
252 /*---------------------*/
253 /* fops implementation */
254 /*---------------------*/
255 
256 static int read_index(struct iowarrior *dev)
257 {
258 	int intr_idx, read_idx;
259 
260 	read_idx = atomic_read(&dev->read_idx);
261 	intr_idx = atomic_read(&dev->intr_idx);
262 
263 	return (read_idx == intr_idx ? -1 : read_idx);
264 }
265 
266 /*
267  *  iowarrior_read
268  */
269 static ssize_t iowarrior_read(struct file *file, char __user *buffer,
270 			      size_t count, loff_t *ppos)
271 {
272 	struct iowarrior *dev;
273 	int read_idx;
274 	int offset;
275 	int retval;
276 
277 	dev = file->private_data;
278 
279 	if (file->f_flags & O_NONBLOCK) {
280 		retval = mutex_trylock(&dev->mutex);
281 		if (!retval)
282 			return -EAGAIN;
283 	} else {
284 		retval = mutex_lock_interruptible(&dev->mutex);
285 		if (retval)
286 			return -ERESTARTSYS;
287 	}
288 
289 	/* verify that the device wasn't unplugged */
290 	if (!dev->present) {
291 		retval = -ENODEV;
292 		goto exit;
293 	}
294 
295 	/* read count must be packet size (+ time stamp) */
296 	if ((count != dev->report_size)
297 	    && (count != (dev->report_size + 1))) {
298 		retval = -EINVAL;
299 		goto exit;
300 	}
301 
302 	/* repeat until no buffer overrun in callback handler occur */
303 	do {
304 		atomic_set(&dev->overflow_flag, 0);
305 		if ((read_idx = read_index(dev)) == -1) {
306 			/* queue empty */
307 			if (file->f_flags & O_NONBLOCK) {
308 				retval = -EAGAIN;
309 				goto exit;
310 			}
311 			else {
312 				//next line will return when there is either new data, or the device is unplugged
313 				int r = wait_event_interruptible(dev->read_wait,
314 								 (!dev->present
315 								  || (read_idx =
316 								      read_index
317 								      (dev)) !=
318 								  -1));
319 				if (r) {
320 					//we were interrupted by a signal
321 					retval = -ERESTART;
322 					goto exit;
323 				}
324 				if (!dev->present) {
325 					//The device was unplugged
326 					retval = -ENODEV;
327 					goto exit;
328 				}
329 				if (read_idx == -1) {
330 					// Can this happen ???
331 					retval = 0;
332 					goto exit;
333 				}
334 			}
335 		}
336 
337 		offset = read_idx * (dev->report_size + 1);
338 		if (copy_to_user(buffer, dev->read_queue + offset, count)) {
339 			retval = -EFAULT;
340 			goto exit;
341 		}
342 	} while (atomic_read(&dev->overflow_flag));
343 
344 	read_idx = ++read_idx == MAX_INTERRUPT_BUFFER ? 0 : read_idx;
345 	atomic_set(&dev->read_idx, read_idx);
346 	mutex_unlock(&dev->mutex);
347 	return count;
348 
349 exit:
350 	mutex_unlock(&dev->mutex);
351 	return retval;
352 }
353 
354 /*
355  * iowarrior_write
356  */
357 static ssize_t iowarrior_write(struct file *file,
358 			       const char __user *user_buffer,
359 			       size_t count, loff_t *ppos)
360 {
361 	struct iowarrior *dev;
362 	int retval;
363 	char *buf = NULL;	/* for IOW24 and IOW56 we need a buffer */
364 	struct urb *int_out_urb = NULL;
365 
366 	dev = file->private_data;
367 
368 	retval = mutex_lock_interruptible(&dev->mutex);
369 	if (retval < 0)
370 		return -EINTR;
371 
372 	/* verify that the device wasn't unplugged */
373 	if (!dev->present) {
374 		retval = -ENODEV;
375 		goto exit;
376 	}
377 	/* if count is 0 we're already done */
378 	if (count == 0) {
379 		retval = 0;
380 		goto exit;
381 	}
382 	/* We only accept full reports */
383 	if (count != dev->report_size) {
384 		retval = -EINVAL;
385 		goto exit;
386 	}
387 	switch (dev->product_id) {
388 	case USB_DEVICE_ID_CODEMERCS_IOW24:
389 	case USB_DEVICE_ID_CODEMERCS_IOW24SAG:
390 	case USB_DEVICE_ID_CODEMERCS_IOWPV1:
391 	case USB_DEVICE_ID_CODEMERCS_IOWPV2:
392 	case USB_DEVICE_ID_CODEMERCS_IOW40:
393 		/* IOW24 and IOW40 use a synchronous call */
394 		buf = memdup_user(user_buffer, count);
395 		if (IS_ERR(buf)) {
396 			retval = PTR_ERR(buf);
397 			goto exit;
398 		}
399 		retval = usb_set_report(dev->interface, 2, 0, buf, count);
400 		kfree(buf);
401 		goto exit;
402 	case USB_DEVICE_ID_CODEMERCS_IOW56:
403 	case USB_DEVICE_ID_CODEMERCS_IOW56AM:
404 	case USB_DEVICE_ID_CODEMERCS_IOW28:
405 	case USB_DEVICE_ID_CODEMERCS_IOW28L:
406 	case USB_DEVICE_ID_CODEMERCS_IOW100:
407 		/* The IOW56 uses asynchronous IO and more urbs */
408 		if (atomic_read(&dev->write_busy) == MAX_WRITES_IN_FLIGHT) {
409 			/* Wait until we are below the limit for submitted urbs */
410 			if (file->f_flags & O_NONBLOCK) {
411 				retval = -EAGAIN;
412 				goto exit;
413 			} else {
414 				retval = wait_event_interruptible(dev->write_wait,
415 								  (!dev->present || (atomic_read (&dev-> write_busy) < MAX_WRITES_IN_FLIGHT)));
416 				if (retval) {
417 					/* we were interrupted by a signal */
418 					retval = -ERESTART;
419 					goto exit;
420 				}
421 				if (!dev->present) {
422 					/* The device was unplugged */
423 					retval = -ENODEV;
424 					goto exit;
425 				}
426 				if (!dev->opened) {
427 					/* We were closed while waiting for an URB */
428 					retval = -ENODEV;
429 					goto exit;
430 				}
431 			}
432 		}
433 		atomic_inc(&dev->write_busy);
434 		int_out_urb = usb_alloc_urb(0, GFP_KERNEL);
435 		if (!int_out_urb) {
436 			retval = -ENOMEM;
437 			goto error_no_urb;
438 		}
439 		buf = kmalloc(dev->report_size, GFP_KERNEL);
440 		if (!buf) {
441 			retval = -ENOMEM;
442 			dev_dbg(&dev->interface->dev,
443 				"Unable to allocate buffer\n");
444 			goto error_no_buffer;
445 		}
446 		usb_fill_int_urb(int_out_urb, dev->udev,
447 				 usb_sndintpipe(dev->udev,
448 						dev->int_out_endpoint->bEndpointAddress),
449 				 buf, dev->report_size,
450 				 iowarrior_write_callback, dev,
451 				 dev->int_out_endpoint->bInterval);
452 		if (copy_from_user(buf, user_buffer, count)) {
453 			retval = -EFAULT;
454 			goto error;
455 		}
456 		usb_anchor_urb(int_out_urb, &dev->submitted);
457 		retval = usb_submit_urb(int_out_urb, GFP_KERNEL);
458 		if (retval) {
459 			dev_dbg(&dev->interface->dev,
460 				"submit error %d for urb nr.%d\n",
461 				retval, atomic_read(&dev->write_busy));
462 			usb_unanchor_urb(int_out_urb);
463 			goto error;
464 		}
465 		/* submit was ok */
466 		retval = count;
467 		usb_free_urb(int_out_urb);
468 		goto exit;
469 	default:
470 		/* what do we have here ? An unsupported Product-ID ? */
471 		dev_err(&dev->interface->dev, "%s - not supported for product=0x%x\n",
472 			__func__, dev->product_id);
473 		retval = -EFAULT;
474 		goto exit;
475 	}
476 error:
477 	kfree(buf);
478 error_no_buffer:
479 	usb_free_urb(int_out_urb);
480 error_no_urb:
481 	atomic_dec(&dev->write_busy);
482 	wake_up_interruptible(&dev->write_wait);
483 exit:
484 	mutex_unlock(&dev->mutex);
485 	return retval;
486 }
487 
488 /*
489  *	iowarrior_ioctl
490  */
491 static long iowarrior_ioctl(struct file *file, unsigned int cmd,
492 							unsigned long arg)
493 {
494 	struct iowarrior *dev = NULL;
495 	__u8 *buffer;
496 	__u8 __user *user_buffer;
497 	int retval;
498 	int io_res;		/* checks for bytes read/written and copy_to/from_user results */
499 
500 	dev = file->private_data;
501 	if (!dev)
502 		return -ENODEV;
503 
504 	buffer = kzalloc(dev->report_size, GFP_KERNEL);
505 	if (!buffer)
506 		return -ENOMEM;
507 
508 	mutex_lock(&dev->mutex);
509 
510 	/* verify that the device wasn't unplugged */
511 	if (!dev->present) {
512 		retval = -ENODEV;
513 		goto error_out;
514 	}
515 
516 	retval = 0;
517 	switch (cmd) {
518 	case IOW_WRITE:
519 		if (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24 ||
520 		    dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW24SAG ||
521 		    dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV1 ||
522 		    dev->product_id == USB_DEVICE_ID_CODEMERCS_IOWPV2 ||
523 		    dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW40) {
524 			user_buffer = (__u8 __user *)arg;
525 			io_res = copy_from_user(buffer, user_buffer,
526 						dev->report_size);
527 			if (io_res) {
528 				retval = -EFAULT;
529 			} else {
530 				io_res = usb_set_report(dev->interface, 2, 0,
531 							buffer,
532 							dev->report_size);
533 				if (io_res < 0)
534 					retval = io_res;
535 			}
536 		} else {
537 			retval = -EINVAL;
538 			dev_err(&dev->interface->dev,
539 				"ioctl 'IOW_WRITE' is not supported for product=0x%x.\n",
540 				dev->product_id);
541 		}
542 		break;
543 	case IOW_READ:
544 		user_buffer = (__u8 __user *)arg;
545 		io_res = usb_get_report(dev->udev,
546 					dev->interface->cur_altsetting, 1, 0,
547 					buffer, dev->report_size);
548 		if (io_res < 0)
549 			retval = io_res;
550 		else {
551 			io_res = copy_to_user(user_buffer, buffer, dev->report_size);
552 			if (io_res)
553 				retval = -EFAULT;
554 		}
555 		break;
556 	case IOW_GETINFO:
557 		{
558 			/* Report available information for the device */
559 			struct iowarrior_info info;
560 			/* needed for power consumption */
561 			struct usb_config_descriptor *cfg_descriptor = &dev->udev->actconfig->desc;
562 
563 			memset(&info, 0, sizeof(info));
564 			/* directly from the descriptor */
565 			info.vendor = le16_to_cpu(dev->udev->descriptor.idVendor);
566 			info.product = dev->product_id;
567 			info.revision = le16_to_cpu(dev->udev->descriptor.bcdDevice);
568 
569 			/* 0==UNKNOWN, 1==LOW(usb1.1) ,2=FULL(usb1.1), 3=HIGH(usb2.0) */
570 			info.speed = dev->udev->speed;
571 			info.if_num = dev->interface->cur_altsetting->desc.bInterfaceNumber;
572 			info.report_size = dev->report_size;
573 
574 			/* serial number string has been read earlier 8 chars or empty string */
575 			memcpy(info.serial, dev->chip_serial,
576 			       sizeof(dev->chip_serial));
577 			if (cfg_descriptor == NULL) {
578 				info.power = -1;	/* no information available */
579 			} else {
580 				/* the MaxPower is stored in units of 2mA to make it fit into a byte-value */
581 				info.power = cfg_descriptor->bMaxPower * 2;
582 			}
583 			io_res = copy_to_user((struct iowarrior_info __user *)arg, &info,
584 					 sizeof(struct iowarrior_info));
585 			if (io_res)
586 				retval = -EFAULT;
587 			break;
588 		}
589 	default:
590 		/* return that we did not understand this ioctl call */
591 		retval = -ENOTTY;
592 		break;
593 	}
594 error_out:
595 	/* unlock the device */
596 	mutex_unlock(&dev->mutex);
597 	kfree(buffer);
598 	return retval;
599 }
600 
601 /*
602  *	iowarrior_open
603  */
604 static int iowarrior_open(struct inode *inode, struct file *file)
605 {
606 	struct iowarrior *dev = NULL;
607 	struct usb_interface *interface;
608 	int subminor;
609 	int retval = 0;
610 
611 	subminor = iminor(inode);
612 
613 	interface = usb_find_interface(&iowarrior_driver, subminor);
614 	if (!interface) {
615 		pr_err("%s - error, can't find device for minor %d\n",
616 		       __func__, subminor);
617 		return -ENODEV;
618 	}
619 
620 	dev = usb_get_intfdata(interface);
621 	if (!dev)
622 		return -ENODEV;
623 
624 	mutex_lock(&dev->mutex);
625 
626 	/* Only one process can open each device, no sharing. */
627 	if (dev->opened) {
628 		retval = -EBUSY;
629 		goto out;
630 	}
631 
632 	/* setup interrupt handler for receiving values */
633 	if ((retval = usb_submit_urb(dev->int_in_urb, GFP_KERNEL)) < 0) {
634 		dev_err(&interface->dev, "Error %d while submitting URB\n", retval);
635 		retval = -EFAULT;
636 		goto out;
637 	}
638 	/* increment our usage count for the driver */
639 	++dev->opened;
640 	/* save our object in the file's private structure */
641 	file->private_data = dev;
642 	retval = 0;
643 
644 out:
645 	mutex_unlock(&dev->mutex);
646 	return retval;
647 }
648 
649 /*
650  *	iowarrior_release
651  */
652 static int iowarrior_release(struct inode *inode, struct file *file)
653 {
654 	struct iowarrior *dev;
655 	int retval = 0;
656 
657 	dev = file->private_data;
658 	if (!dev)
659 		return -ENODEV;
660 
661 	/* lock our device */
662 	mutex_lock(&dev->mutex);
663 
664 	if (dev->opened <= 0) {
665 		retval = -ENODEV;	/* close called more than once */
666 		mutex_unlock(&dev->mutex);
667 	} else {
668 		dev->opened = 0;	/* we're closing now */
669 		retval = 0;
670 		if (dev->present) {
671 			/*
672 			   The device is still connected so we only shutdown
673 			   pending read-/write-ops.
674 			 */
675 			usb_kill_urb(dev->int_in_urb);
676 			wake_up_interruptible(&dev->read_wait);
677 			wake_up_interruptible(&dev->write_wait);
678 			mutex_unlock(&dev->mutex);
679 		} else {
680 			/* The device was unplugged, cleanup resources */
681 			mutex_unlock(&dev->mutex);
682 			iowarrior_delete(dev);
683 		}
684 	}
685 	return retval;
686 }
687 
688 static __poll_t iowarrior_poll(struct file *file, poll_table * wait)
689 {
690 	struct iowarrior *dev = file->private_data;
691 	__poll_t mask = 0;
692 
693 	if (!dev->present)
694 		return EPOLLERR | EPOLLHUP;
695 
696 	poll_wait(file, &dev->read_wait, wait);
697 	poll_wait(file, &dev->write_wait, wait);
698 
699 	if (!dev->present)
700 		return EPOLLERR | EPOLLHUP;
701 
702 	if (read_index(dev) != -1)
703 		mask |= EPOLLIN | EPOLLRDNORM;
704 
705 	if (atomic_read(&dev->write_busy) < MAX_WRITES_IN_FLIGHT)
706 		mask |= EPOLLOUT | EPOLLWRNORM;
707 	return mask;
708 }
709 
710 /*
711  * File operations needed when we register this driver.
712  * This assumes that this driver NEEDS file operations,
713  * of course, which means that the driver is expected
714  * to have a node in the /dev directory. If the USB
715  * device were for a network interface then the driver
716  * would use "struct net_driver" instead, and a serial
717  * device would use "struct tty_driver".
718  */
719 static const struct file_operations iowarrior_fops = {
720 	.owner = THIS_MODULE,
721 	.write = iowarrior_write,
722 	.read = iowarrior_read,
723 	.unlocked_ioctl = iowarrior_ioctl,
724 	.open = iowarrior_open,
725 	.release = iowarrior_release,
726 	.poll = iowarrior_poll,
727 	.llseek = noop_llseek,
728 };
729 
730 static char *iowarrior_devnode(const struct device *dev, umode_t *mode)
731 {
732 	return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
733 }
734 
735 /*
736  * usb class driver info in order to get a minor number from the usb core,
737  * and to have the device registered with devfs and the driver core
738  */
739 static struct usb_class_driver iowarrior_class = {
740 	.name = "iowarrior%d",
741 	.devnode = iowarrior_devnode,
742 	.fops = &iowarrior_fops,
743 	.minor_base = IOWARRIOR_MINOR_BASE,
744 };
745 
746 /*---------------------------------*/
747 /*  probe and disconnect functions */
748 /*---------------------------------*/
749 /*
750  *	iowarrior_probe
751  *
752  *	Called by the usb core when a new device is connected that it thinks
753  *	this driver might be interested in.
754  */
755 static int iowarrior_probe(struct usb_interface *interface,
756 			   const struct usb_device_id *id)
757 {
758 	struct usb_device *udev = interface_to_usbdev(interface);
759 	struct iowarrior *dev = NULL;
760 	struct usb_host_interface *iface_desc;
761 	int retval = -ENOMEM;
762 	int res;
763 	int minor;
764 
765 	/* allocate memory for our device state and initialize it */
766 	dev = kzalloc_obj(struct iowarrior);
767 	if (!dev)
768 		return retval;
769 
770 	mutex_init(&dev->mutex);
771 
772 	atomic_set(&dev->intr_idx, 0);
773 	atomic_set(&dev->read_idx, 0);
774 	atomic_set(&dev->overflow_flag, 0);
775 	init_waitqueue_head(&dev->read_wait);
776 	atomic_set(&dev->write_busy, 0);
777 	init_waitqueue_head(&dev->write_wait);
778 
779 	dev->udev = udev;
780 	dev->interface = usb_get_intf(interface);
781 
782 	iface_desc = interface->cur_altsetting;
783 	dev->product_id = le16_to_cpu(udev->descriptor.idProduct);
784 
785 	init_usb_anchor(&dev->submitted);
786 
787 	res = usb_find_last_int_in_endpoint(iface_desc, &dev->int_in_endpoint);
788 	if (res) {
789 		dev_err(&interface->dev, "no interrupt-in endpoint found\n");
790 		retval = res;
791 		goto error;
792 	}
793 
794 	if ((dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56) ||
795 	    (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW56AM) ||
796 	    (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28) ||
797 	    (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW28L) ||
798 	    (dev->product_id == USB_DEVICE_ID_CODEMERCS_IOW100)) {
799 		res = usb_find_last_int_out_endpoint(iface_desc,
800 				&dev->int_out_endpoint);
801 		if (res) {
802 			dev_err(&interface->dev, "no interrupt-out endpoint found\n");
803 			retval = res;
804 			goto error;
805 		}
806 	}
807 
808 	/* we have to check the report_size often, so remember it in the endianness suitable for our machine */
809 	dev->report_size = usb_endpoint_maxp(dev->int_in_endpoint);
810 
811 	/*
812 	 * Some devices need the report size to be different than the
813 	 * endpoint size.
814 	 */
815 	if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) {
816 		switch (dev->product_id) {
817 		case USB_DEVICE_ID_CODEMERCS_IOW56:
818 		case USB_DEVICE_ID_CODEMERCS_IOW56AM:
819 			dev->report_size = 7;
820 			break;
821 
822 		case USB_DEVICE_ID_CODEMERCS_IOW28:
823 		case USB_DEVICE_ID_CODEMERCS_IOW28L:
824 			dev->report_size = 4;
825 			break;
826 
827 		case USB_DEVICE_ID_CODEMERCS_IOW100:
828 			dev->report_size = 12;
829 			break;
830 		}
831 	}
832 
833 	/* create the urb and buffer for reading */
834 	dev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
835 	if (!dev->int_in_urb)
836 		goto error;
837 	dev->int_in_buffer = kmalloc(dev->report_size, GFP_KERNEL);
838 	if (!dev->int_in_buffer)
839 		goto error;
840 	usb_fill_int_urb(dev->int_in_urb, dev->udev,
841 			 usb_rcvintpipe(dev->udev,
842 					dev->int_in_endpoint->bEndpointAddress),
843 			 dev->int_in_buffer, dev->report_size,
844 			 iowarrior_callback, dev,
845 			 dev->int_in_endpoint->bInterval);
846 	/* create an internal buffer for interrupt data from the device */
847 	dev->read_queue =
848 	    kmalloc_array(dev->report_size + 1, MAX_INTERRUPT_BUFFER,
849 			  GFP_KERNEL);
850 	if (!dev->read_queue)
851 		goto error;
852 	/* Get the serial-number of the chip */
853 	memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
854 	usb_string(udev, udev->descriptor.iSerialNumber, dev->chip_serial,
855 		   sizeof(dev->chip_serial));
856 	if (strlen(dev->chip_serial) != 8)
857 		memset(dev->chip_serial, 0x00, sizeof(dev->chip_serial));
858 
859 	/* Set the idle timeout to 0, if this is interface 0 */
860 	if (dev->interface->cur_altsetting->desc.bInterfaceNumber == 0) {
861 	    usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
862 			    0x0A,
863 			    USB_TYPE_CLASS | USB_RECIP_INTERFACE, 0,
864 			    0, NULL, 0, USB_CTRL_SET_TIMEOUT);
865 	}
866 	/* allow device read and ioctl */
867 	dev->present = 1;
868 
869 	/* we can register the device now, as it is ready */
870 	usb_set_intfdata(interface, dev);
871 
872 	retval = usb_register_dev(interface, &iowarrior_class);
873 	if (retval) {
874 		/* something prevented us from registering this driver */
875 		dev_err(&interface->dev, "Not able to get a minor for this device.\n");
876 		goto error;
877 	}
878 
879 	minor = interface->minor;
880 
881 	/* let the user know what node this device is now attached to */
882 	dev_info(&interface->dev, "IOWarrior product=0x%x, serial=%s interface=%d "
883 		 "now attached to iowarrior%d\n", dev->product_id, dev->chip_serial,
884 		 iface_desc->desc.bInterfaceNumber, minor - IOWARRIOR_MINOR_BASE);
885 	return retval;
886 
887 error:
888 	iowarrior_delete(dev);
889 	return retval;
890 }
891 
892 /*
893  *	iowarrior_disconnect
894  *
895  *	Called by the usb core when the device is removed from the system.
896  */
897 static void iowarrior_disconnect(struct usb_interface *interface)
898 {
899 	struct iowarrior *dev = usb_get_intfdata(interface);
900 
901 	usb_deregister_dev(interface, &iowarrior_class);
902 
903 	mutex_lock(&dev->mutex);
904 
905 	/* prevent device read, write and ioctl */
906 	dev->present = 0;
907 
908 	if (dev->opened) {
909 		/* There is a process that holds a filedescriptor to the device ,
910 		   so we only shutdown read-/write-ops going on.
911 		   Deleting the device is postponed until close() was called.
912 		 */
913 		usb_kill_urb(dev->int_in_urb);
914 		usb_kill_anchored_urbs(&dev->submitted);
915 		wake_up_interruptible(&dev->read_wait);
916 		wake_up_interruptible(&dev->write_wait);
917 		mutex_unlock(&dev->mutex);
918 	} else {
919 		/* no process is using the device, cleanup now */
920 		mutex_unlock(&dev->mutex);
921 		iowarrior_delete(dev);
922 	}
923 }
924 
925 /* usb specific object needed to register this driver with the usb subsystem */
926 static struct usb_driver iowarrior_driver = {
927 	.name = "iowarrior",
928 	.probe = iowarrior_probe,
929 	.disconnect = iowarrior_disconnect,
930 	.id_table = iowarrior_ids,
931 };
932 
933 module_usb_driver(iowarrior_driver);
934