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