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