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