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