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