blob: c567aa7a41ead3643de3c38ab4b286a3c13c283b [file] [log] [blame]
Steven Haigh03270632006-08-09 07:42:06 +10001/*
2 * adutux - driver for ADU devices from Ontrak Control Systems
3 * This is an experimental driver. Use at your own risk.
4 * This driver is not supported by Ontrak Control Systems.
5 *
6 * Copyright (c) 2003 John Homppi (SCO, leave this notice here)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * derived from the Lego USB Tower driver 0.56:
14 * Copyright (c) 2003 David Glance <davidgsf@sourceforge.net>
15 * 2001 Juergen Stuber <stuber@loria.fr>
16 * that was derived from USB Skeleton driver - 0.5
17 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
18 *
19 */
20
21#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/init.h>
24#include <linux/slab.h>
25#include <linux/module.h>
26#include <linux/usb.h>
Matthias Kaehlcke8293c562007-07-13 21:28:31 +020027#include <linux/mutex.h>
Steven Haigh03270632006-08-09 07:42:06 +100028#include <asm/uaccess.h>
29
30#ifdef CONFIG_USB_DEBUG
31static int debug = 5;
32#else
33static int debug = 1;
34#endif
35
36/* Use our own dbg macro */
37#undef dbg
38#define dbg(lvl, format, arg...) \
39do { \
40 if (debug >= lvl) \
41 printk(KERN_DEBUG __FILE__ " : " format " \n", ## arg); \
42} while (0)
43
44
45/* Version Information */
46#define DRIVER_VERSION "v0.0.13"
47#define DRIVER_AUTHOR "John Homppi"
48#define DRIVER_DESC "adutux (see www.ontrak.net)"
49
50/* Module parameters */
51module_param(debug, int, S_IRUGO | S_IWUSR);
52MODULE_PARM_DESC(debug, "Debug enabled or not");
53
54/* Define these values to match your device */
55#define ADU_VENDOR_ID 0x0a07
56#define ADU_PRODUCT_ID 0x0064
57
58/* table of devices that work with this driver */
59static struct usb_device_id device_table [] = {
60 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */
61 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */
62 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */
63 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */
64 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */
65 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */
66 { }/* Terminating entry */
67};
68
69MODULE_DEVICE_TABLE(usb, device_table);
70
71#ifdef CONFIG_USB_DYNAMIC_MINORS
72#define ADU_MINOR_BASE 0
73#else
74#define ADU_MINOR_BASE 67
75#endif
76
77/* we can have up to this number of device plugged in at once */
78#define MAX_DEVICES 16
79
80#define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */
81
82/* Structure to hold all of our device specific stuff */
83struct adu_device {
Matthias Kaehlcke8293c562007-07-13 21:28:31 +020084 struct mutex mtx; /* locks this structure */
Steven Haigh03270632006-08-09 07:42:06 +100085 struct usb_device* udev; /* save off the usb device pointer */
86 struct usb_interface* interface;
87 unsigned char minor; /* the starting minor number for this device */
88 char serial_number[8];
89
90 int open_count; /* number of times this port has been opened */
91
92 char* read_buffer_primary;
93 int read_buffer_length;
94 char* read_buffer_secondary;
95 int secondary_head;
96 int secondary_tail;
97 spinlock_t buflock;
98
99 wait_queue_head_t read_wait;
100 wait_queue_head_t write_wait;
101
102 char* interrupt_in_buffer;
103 struct usb_endpoint_descriptor* interrupt_in_endpoint;
104 struct urb* interrupt_in_urb;
105 int read_urb_finished;
106
107 char* interrupt_out_buffer;
108 struct usb_endpoint_descriptor* interrupt_out_endpoint;
109 struct urb* interrupt_out_urb;
110};
111
Steven Haigh03270632006-08-09 07:42:06 +1000112static struct usb_driver adu_driver;
113
114static void adu_debug_data(int level, const char *function, int size,
115 const unsigned char *data)
116{
117 int i;
118
119 if (debug < level)
120 return;
121
122 printk(KERN_DEBUG __FILE__": %s - length = %d, data = ",
123 function, size);
124 for (i = 0; i < size; ++i)
125 printk("%.2x ", data[i]);
126 printk("\n");
127}
128
129/**
130 * adu_abort_transfers
131 * aborts transfers and frees associated data structures
132 */
133static void adu_abort_transfers(struct adu_device *dev)
134{
135 dbg(2," %s : enter", __FUNCTION__);
136
137 if (dev == NULL) {
138 dbg(1," %s : dev is null", __FUNCTION__);
139 goto exit;
140 }
141
142 if (dev->udev == NULL) {
143 dbg(1," %s : udev is null", __FUNCTION__);
144 goto exit;
145 }
146
147 dbg(2," %s : udev state %d", __FUNCTION__, dev->udev->state);
148 if (dev->udev->state == USB_STATE_NOTATTACHED) {
149 dbg(1," %s : udev is not attached", __FUNCTION__);
150 goto exit;
151 }
152
153 /* shutdown transfer */
154 usb_unlink_urb(dev->interrupt_in_urb);
155 usb_unlink_urb(dev->interrupt_out_urb);
156
157exit:
158 dbg(2," %s : leave", __FUNCTION__);
159}
160
161static void adu_delete(struct adu_device *dev)
162{
163 dbg(2, "%s enter", __FUNCTION__);
164
165 adu_abort_transfers(dev);
166
167 /* free data structures */
168 usb_free_urb(dev->interrupt_in_urb);
169 usb_free_urb(dev->interrupt_out_urb);
170 kfree(dev->read_buffer_primary);
171 kfree(dev->read_buffer_secondary);
172 kfree(dev->interrupt_in_buffer);
173 kfree(dev->interrupt_out_buffer);
174 kfree(dev);
175
176 dbg(2, "%s : leave", __FUNCTION__);
177}
178
David Howells7d12e782006-10-05 14:55:46 +0100179static void adu_interrupt_in_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000180{
181 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700182 int status = urb->status;
Steven Haigh03270632006-08-09 07:42:06 +1000183
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700184 dbg(4," %s : enter, status %d", __FUNCTION__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000185 adu_debug_data(5, __FUNCTION__, urb->actual_length,
186 urb->transfer_buffer);
187
188 spin_lock(&dev->buflock);
189
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700190 if (status != 0) {
Oliver Neukumf6c1cea2007-08-16 16:02:08 +0200191 if ((status != -ENOENT) && (status != -ECONNRESET) &&
192 (status != -ESHUTDOWN)) {
Steven Haigh03270632006-08-09 07:42:06 +1000193 dbg(1," %s : nonzero status received: %d",
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700194 __FUNCTION__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000195 }
196 goto exit;
197 }
198
199 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
200 if (dev->read_buffer_length <
201 (4 * le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize)) -
202 (urb->actual_length)) {
203 memcpy (dev->read_buffer_primary +
204 dev->read_buffer_length,
205 dev->interrupt_in_buffer, urb->actual_length);
206
207 dev->read_buffer_length += urb->actual_length;
208 dbg(2," %s reading %d ", __FUNCTION__,
209 urb->actual_length);
210 } else {
211 dbg(1," %s : read_buffer overflow", __FUNCTION__);
212 }
213 }
214
215exit:
216 dev->read_urb_finished = 1;
217 spin_unlock(&dev->buflock);
218 /* always wake up so we recover from errors */
219 wake_up_interruptible(&dev->read_wait);
220 adu_debug_data(5, __FUNCTION__, urb->actual_length,
221 urb->transfer_buffer);
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700222 dbg(4," %s : leave, status %d", __FUNCTION__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000223}
224
David Howells7d12e782006-10-05 14:55:46 +0100225static void adu_interrupt_out_callback(struct urb *urb)
Steven Haigh03270632006-08-09 07:42:06 +1000226{
227 struct adu_device *dev = urb->context;
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700228 int status = urb->status;
Steven Haigh03270632006-08-09 07:42:06 +1000229
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700230 dbg(4," %s : enter, status %d", __FUNCTION__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000231 adu_debug_data(5,__FUNCTION__, urb->actual_length, urb->transfer_buffer);
232
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700233 if (status != 0) {
234 if ((status != -ENOENT) &&
235 (status != -ECONNRESET)) {
Steven Haigh03270632006-08-09 07:42:06 +1000236 dbg(1, " %s :nonzero status received: %d",
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700237 __FUNCTION__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000238 }
239 goto exit;
240 }
241
242 wake_up_interruptible(&dev->write_wait);
243exit:
244
245 adu_debug_data(5, __FUNCTION__, urb->actual_length,
246 urb->transfer_buffer);
Greg Kroah-Hartman24497a02007-07-18 10:58:02 -0700247 dbg(4," %s : leave, status %d", __FUNCTION__, status);
Steven Haigh03270632006-08-09 07:42:06 +1000248}
249
250static int adu_open(struct inode *inode, struct file *file)
251{
252 struct adu_device *dev = NULL;
253 struct usb_interface *interface;
254 int subminor;
255 int retval = 0;
256
257 dbg(2,"%s : enter", __FUNCTION__);
258
259 subminor = iminor(inode);
260
Steven Haigh03270632006-08-09 07:42:06 +1000261 interface = usb_find_interface(&adu_driver, subminor);
262 if (!interface) {
263 err("%s - error, can't find device for minor %d",
264 __FUNCTION__, subminor);
265 retval = -ENODEV;
266 goto exit_no_device;
267 }
268
269 dev = usb_get_intfdata(interface);
270 if (!dev) {
271 retval = -ENODEV;
272 goto exit_no_device;
273 }
274
275 /* lock this device */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200276 if ((retval = mutex_lock_interruptible(&dev->mtx))) {
277 dbg(2, "%s : mutex lock failed", __FUNCTION__);
Steven Haigh03270632006-08-09 07:42:06 +1000278 goto exit_no_device;
279 }
280
281 /* increment our usage count for the device */
282 ++dev->open_count;
283 dbg(2,"%s : open count %d", __FUNCTION__, dev->open_count);
284
285 /* save device in the file's private structure */
286 file->private_data = dev;
287
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200288 if (dev->open_count == 1) {
289 /* initialize in direction */
290 dev->read_buffer_length = 0;
Steven Haigh03270632006-08-09 07:42:06 +1000291
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200292 /* fixup first read by having urb waiting for it */
293 usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
294 usb_rcvintpipe(dev->udev,
295 dev->interrupt_in_endpoint->bEndpointAddress),
296 dev->interrupt_in_buffer,
297 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
298 adu_interrupt_in_callback, dev,
299 dev->interrupt_in_endpoint->bInterval);
300 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
301 dev->read_urb_finished = 0;
302 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
303 if (retval)
304 --dev->open_count;
305 }
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200306 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000307
308exit_no_device:
Steven Haigh03270632006-08-09 07:42:06 +1000309 dbg(2,"%s : leave, return value %d ", __FUNCTION__, retval);
310
311 return retval;
312}
313
314static int adu_release_internal(struct adu_device *dev)
315{
316 int retval = 0;
317
318 dbg(2," %s : enter", __FUNCTION__);
319
Steven Haigh03270632006-08-09 07:42:06 +1000320 /* decrement our usage count for the device */
321 --dev->open_count;
322 dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);
323 if (dev->open_count <= 0) {
324 adu_abort_transfers(dev);
325 dev->open_count = 0;
326 }
327
Steven Haigh03270632006-08-09 07:42:06 +1000328 dbg(2," %s : leave", __FUNCTION__);
329 return retval;
330}
331
332static int adu_release(struct inode *inode, struct file *file)
333{
334 struct adu_device *dev = NULL;
335 int retval = 0;
336
337 dbg(2," %s : enter", __FUNCTION__);
338
339 if (file == NULL) {
340 dbg(1," %s : file is NULL", __FUNCTION__);
341 retval = -ENODEV;
342 goto exit;
343 }
344
345 dev = file->private_data;
346
347 if (dev == NULL) {
348 dbg(1," %s : object is NULL", __FUNCTION__);
349 retval = -ENODEV;
350 goto exit;
351 }
352
353 /* lock our device */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200354 mutex_lock(&dev->mtx); /* not interruptible */
Steven Haigh03270632006-08-09 07:42:06 +1000355
356 if (dev->open_count <= 0) {
357 dbg(1," %s : device not opened", __FUNCTION__);
358 retval = -ENODEV;
359 goto exit;
360 }
361
Alan Sternd4ead162007-05-22 11:46:41 -0400362 if (dev->udev == NULL) {
363 /* the device was unplugged before the file was released */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200364 mutex_unlock(&dev->mtx);
Alan Sternd4ead162007-05-22 11:46:41 -0400365 adu_delete(dev);
366 dev = NULL;
367 } else {
368 /* do the work */
369 retval = adu_release_internal(dev);
370 }
Steven Haigh03270632006-08-09 07:42:06 +1000371
372exit:
Eric Sesterhenna65dc302006-10-06 00:09:29 +0200373 if (dev)
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200374 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000375 dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
376 return retval;
377}
378
379static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
380 loff_t *ppos)
381{
382 struct adu_device *dev;
383 size_t bytes_read = 0;
384 size_t bytes_to_read = count;
385 int i;
386 int retval = 0;
387 int timeout = 0;
388 int should_submit = 0;
389 unsigned long flags;
390 DECLARE_WAITQUEUE(wait, current);
391
392 dbg(2," %s : enter, count = %Zd, file=%p", __FUNCTION__, count, file);
393
394 dev = file->private_data;
395 dbg(2," %s : dev=%p", __FUNCTION__, dev);
396 /* lock this object */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200397 if (mutex_lock_interruptible(&dev->mtx))
Steven Haigh03270632006-08-09 07:42:06 +1000398 return -ERESTARTSYS;
399
400 /* verify that the device wasn't unplugged */
401 if (dev->udev == NULL || dev->minor == 0) {
402 retval = -ENODEV;
403 err("No device or device unplugged %d", retval);
404 goto exit;
405 }
406
407 /* verify that some data was requested */
408 if (count == 0) {
409 dbg(1," %s : read request of 0 bytes", __FUNCTION__);
410 goto exit;
411 }
412
413 timeout = COMMAND_TIMEOUT;
414 dbg(2," %s : about to start looping", __FUNCTION__);
415 while (bytes_to_read) {
416 int data_in_secondary = dev->secondary_tail - dev->secondary_head;
417 dbg(2," %s : while, data_in_secondary=%d, status=%d",
418 __FUNCTION__, data_in_secondary,
419 dev->interrupt_in_urb->status);
420
421 if (data_in_secondary) {
422 /* drain secondary buffer */
423 int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
424 i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
425 if (i < 0) {
426 retval = -EFAULT;
427 goto exit;
428 }
429 dev->secondary_head += (amount - i);
430 bytes_read += (amount - i);
431 bytes_to_read -= (amount - i);
432 if (i) {
433 retval = bytes_read ? bytes_read : -EFAULT;
434 goto exit;
435 }
436 } else {
437 /* we check the primary buffer */
438 spin_lock_irqsave (&dev->buflock, flags);
439 if (dev->read_buffer_length) {
440 /* we secure access to the primary */
441 char *tmp;
442 dbg(2," %s : swap, read_buffer_length = %d",
443 __FUNCTION__, dev->read_buffer_length);
444 tmp = dev->read_buffer_secondary;
445 dev->read_buffer_secondary = dev->read_buffer_primary;
446 dev->read_buffer_primary = tmp;
447 dev->secondary_head = 0;
448 dev->secondary_tail = dev->read_buffer_length;
449 dev->read_buffer_length = 0;
450 spin_unlock_irqrestore(&dev->buflock, flags);
451 /* we have a free buffer so use it */
452 should_submit = 1;
453 } else {
454 /* even the primary was empty - we may need to do IO */
455 if (dev->interrupt_in_urb->status == -EINPROGRESS) {
456 /* somebody is doing IO */
457 spin_unlock_irqrestore(&dev->buflock, flags);
458 dbg(2," %s : submitted already", __FUNCTION__);
459 } else {
460 /* we must initiate input */
461 dbg(2," %s : initiate input", __FUNCTION__);
462 dev->read_urb_finished = 0;
463
464 usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
465 usb_rcvintpipe(dev->udev,
466 dev->interrupt_in_endpoint->bEndpointAddress),
467 dev->interrupt_in_buffer,
468 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
469 adu_interrupt_in_callback,
470 dev,
471 dev->interrupt_in_endpoint->bInterval);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200472 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_ATOMIC);
Steven Haigh03270632006-08-09 07:42:06 +1000473 if (!retval) {
474 spin_unlock_irqrestore(&dev->buflock, flags);
475 dbg(2," %s : submitted OK", __FUNCTION__);
476 } else {
477 if (retval == -ENOMEM) {
478 retval = bytes_read ? bytes_read : -ENOMEM;
479 }
480 spin_unlock_irqrestore(&dev->buflock, flags);
481 dbg(2," %s : submit failed", __FUNCTION__);
482 goto exit;
483 }
484 }
485
486 /* we wait for I/O to complete */
487 set_current_state(TASK_INTERRUPTIBLE);
488 add_wait_queue(&dev->read_wait, &wait);
489 if (!dev->read_urb_finished)
490 timeout = schedule_timeout(COMMAND_TIMEOUT);
491 else
492 set_current_state(TASK_RUNNING);
493 remove_wait_queue(&dev->read_wait, &wait);
494
495 if (timeout <= 0) {
496 dbg(2," %s : timeout", __FUNCTION__);
497 retval = bytes_read ? bytes_read : -ETIMEDOUT;
498 goto exit;
499 }
500
501 if (signal_pending(current)) {
502 dbg(2," %s : signal pending", __FUNCTION__);
503 retval = bytes_read ? bytes_read : -EINTR;
504 goto exit;
505 }
506 }
507 }
508 }
509
510 retval = bytes_read;
511 /* if the primary buffer is empty then use it */
512 if (should_submit && !dev->interrupt_in_urb->status==-EINPROGRESS) {
513 usb_fill_int_urb(dev->interrupt_in_urb,dev->udev,
514 usb_rcvintpipe(dev->udev,
515 dev->interrupt_in_endpoint->bEndpointAddress),
516 dev->interrupt_in_buffer,
517 le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize),
518 adu_interrupt_in_callback,
519 dev,
520 dev->interrupt_in_endpoint->bInterval);
521 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
522 dev->read_urb_finished = 0;
523 usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
524 /* we ignore failure */
525 }
526
527exit:
528 /* unlock the device */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200529 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000530
531 dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
532 return retval;
533}
534
535static ssize_t adu_write(struct file *file, const __user char *buffer,
536 size_t count, loff_t *ppos)
537{
538 struct adu_device *dev;
539 size_t bytes_written = 0;
540 size_t bytes_to_write;
541 size_t buffer_size;
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200542 int retval;
Steven Haigh03270632006-08-09 07:42:06 +1000543 int timeout = 0;
544
545 dbg(2," %s : enter, count = %Zd", __FUNCTION__, count);
546
547 dev = file->private_data;
548
549 /* lock this object */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200550 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200551 if (retval)
552 goto exit_nolock;
Steven Haigh03270632006-08-09 07:42:06 +1000553
554 /* verify that the device wasn't unplugged */
555 if (dev->udev == NULL || dev->minor == 0) {
556 retval = -ENODEV;
557 err("No device or device unplugged %d", retval);
558 goto exit;
559 }
560
561 /* verify that we actually have some data to write */
562 if (count == 0) {
563 dbg(1," %s : write request of 0 bytes", __FUNCTION__);
564 goto exit;
565 }
566
567
568 while (count > 0) {
569 if (dev->interrupt_out_urb->status == -EINPROGRESS) {
570 timeout = COMMAND_TIMEOUT;
571
572 while (timeout > 0) {
573 if (signal_pending(current)) {
574 dbg(1," %s : interrupted", __FUNCTION__);
575 retval = -EINTR;
576 goto exit;
577 }
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200578 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000579 timeout = interruptible_sleep_on_timeout(&dev->write_wait, timeout);
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200580 retval = mutex_lock_interruptible(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200581 if (retval) {
582 retval = bytes_written ? bytes_written : retval;
583 goto exit_nolock;
584 }
Steven Haigh03270632006-08-09 07:42:06 +1000585 if (timeout > 0) {
586 break;
587 }
588 dbg(1," %s : interrupted timeout: %d", __FUNCTION__, timeout);
589 }
590
591
592 dbg(1," %s : final timeout: %d", __FUNCTION__, timeout);
593
594 if (timeout == 0) {
595 dbg(1, "%s - command timed out.", __FUNCTION__);
596 retval = -ETIMEDOUT;
597 goto exit;
598 }
599
600 dbg(4," %s : in progress, count = %Zd", __FUNCTION__, count);
601
602 } else {
603 dbg(4," %s : sending, count = %Zd", __FUNCTION__, count);
604
605 /* write the data into interrupt_out_buffer from userspace */
606 buffer_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize);
607 bytes_to_write = count > buffer_size ? buffer_size : count;
608 dbg(4," %s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd",
609 __FUNCTION__, buffer_size, count, bytes_to_write);
610
611 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
612 retval = -EFAULT;
613 goto exit;
614 }
615
616 /* send off the urb */
617 usb_fill_int_urb(
618 dev->interrupt_out_urb,
619 dev->udev,
620 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
621 dev->interrupt_out_buffer,
622 bytes_to_write,
623 adu_interrupt_out_callback,
624 dev,
625 dev->interrupt_in_endpoint->bInterval);
626 /* dev->interrupt_in_urb->transfer_flags |= URB_ASYNC_UNLINK; */
627 dev->interrupt_out_urb->actual_length = bytes_to_write;
628 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
629 if (retval < 0) {
630 err("Couldn't submit interrupt_out_urb %d", retval);
631 goto exit;
632 }
633
634 buffer += bytes_to_write;
635 count -= bytes_to_write;
636
637 bytes_written += bytes_to_write;
638 }
639 }
640
641 retval = bytes_written;
642
643exit:
644 /* unlock the device */
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200645 mutex_unlock(&dev->mtx);
Oliver Neukumebc3ac12007-04-02 15:16:36 +0200646exit_nolock:
Steven Haigh03270632006-08-09 07:42:06 +1000647
648 dbg(2," %s : leave, return value %d", __FUNCTION__, retval);
649
650 return retval;
651}
652
653/* file operations needed when we register this driver */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800654static const struct file_operations adu_fops = {
Steven Haigh03270632006-08-09 07:42:06 +1000655 .owner = THIS_MODULE,
656 .read = adu_read,
657 .write = adu_write,
658 .open = adu_open,
659 .release = adu_release,
660};
661
662/*
663 * usb class driver info in order to get a minor number from the usb core,
664 * and to have the device registered with devfs and the driver core
665 */
666static struct usb_class_driver adu_class = {
667 .name = "usb/adutux%d",
668 .fops = &adu_fops,
669 .minor_base = ADU_MINOR_BASE,
670};
671
672/**
673 * adu_probe
674 *
675 * Called by the usb core when a new device is connected that it thinks
676 * this driver might be interested in.
677 */
678static int adu_probe(struct usb_interface *interface,
679 const struct usb_device_id *id)
680{
681 struct usb_device *udev = interface_to_usbdev(interface);
682 struct adu_device *dev = NULL;
683 struct usb_host_interface *iface_desc;
684 struct usb_endpoint_descriptor *endpoint;
685 int retval = -ENODEV;
686 int in_end_size;
687 int out_end_size;
688 int i;
689
690 dbg(2," %s : enter", __FUNCTION__);
691
692 if (udev == NULL) {
693 dev_err(&interface->dev, "udev is NULL.\n");
694 goto exit;
695 }
696
697 /* allocate memory for our device state and intialize it */
698 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
699 if (dev == NULL) {
700 dev_err(&interface->dev, "Out of memory\n");
701 retval = -ENOMEM;
702 goto exit;
703 }
704
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200705 mutex_init(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000706 spin_lock_init(&dev->buflock);
707 dev->udev = udev;
708 init_waitqueue_head(&dev->read_wait);
709 init_waitqueue_head(&dev->write_wait);
710
711 iface_desc = &interface->altsetting[0];
712
713 /* set up the endpoint information */
714 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
715 endpoint = &iface_desc->endpoint[i].desc;
716
717 if (usb_endpoint_is_int_in(endpoint))
718 dev->interrupt_in_endpoint = endpoint;
719
720 if (usb_endpoint_is_int_out(endpoint))
721 dev->interrupt_out_endpoint = endpoint;
722 }
723 if (dev->interrupt_in_endpoint == NULL) {
724 dev_err(&interface->dev, "interrupt in endpoint not found\n");
725 goto error;
726 }
727 if (dev->interrupt_out_endpoint == NULL) {
728 dev_err(&interface->dev, "interrupt out endpoint not found\n");
729 goto error;
730 }
731
732 in_end_size = le16_to_cpu(dev->interrupt_in_endpoint->wMaxPacketSize);
733 out_end_size = le16_to_cpu(dev->interrupt_out_endpoint->wMaxPacketSize);
734
735 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
736 if (!dev->read_buffer_primary) {
737 dev_err(&interface->dev, "Couldn't allocate read_buffer_primary\n");
738 retval = -ENOMEM;
739 goto error;
740 }
741
742 /* debug code prime the buffer */
743 memset(dev->read_buffer_primary, 'a', in_end_size);
744 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
745 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
746 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
747
748 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
749 if (!dev->read_buffer_secondary) {
750 dev_err(&interface->dev, "Couldn't allocate read_buffer_secondary\n");
751 retval = -ENOMEM;
752 goto error;
753 }
754
755 /* debug code prime the buffer */
756 memset(dev->read_buffer_secondary, 'e', in_end_size);
757 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
758 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
759 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
760
761 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
762 if (!dev->interrupt_in_buffer) {
763 dev_err(&interface->dev, "Couldn't allocate interrupt_in_buffer\n");
764 goto error;
765 }
766
767 /* debug code prime the buffer */
768 memset(dev->interrupt_in_buffer, 'i', in_end_size);
769
770 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
771 if (!dev->interrupt_in_urb) {
772 dev_err(&interface->dev, "Couldn't allocate interrupt_in_urb\n");
773 goto error;
774 }
775 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
776 if (!dev->interrupt_out_buffer) {
777 dev_err(&interface->dev, "Couldn't allocate interrupt_out_buffer\n");
778 goto error;
779 }
780 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
781 if (!dev->interrupt_out_urb) {
782 dev_err(&interface->dev, "Couldn't allocate interrupt_out_urb\n");
783 goto error;
784 }
785
786 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
787 sizeof(dev->serial_number))) {
788 dev_err(&interface->dev, "Could not retrieve serial number\n");
789 goto error;
790 }
791 dbg(2," %s : serial_number=%s", __FUNCTION__, dev->serial_number);
792
793 /* we can register the device now, as it is ready */
794 usb_set_intfdata(interface, dev);
795
796 retval = usb_register_dev(interface, &adu_class);
797
798 if (retval) {
799 /* something prevented us from registering this driver */
800 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
801 usb_set_intfdata(interface, NULL);
802 goto error;
803 }
804
805 dev->minor = interface->minor;
806
807 /* let the user know what node this device is now attached to */
Joe Perches898eb712007-10-18 03:06:30 -0700808 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
Steven Haigh03270632006-08-09 07:42:06 +1000809 udev->descriptor.idProduct, dev->serial_number,
810 (dev->minor - ADU_MINOR_BASE));
811exit:
812 dbg(2," %s : leave, return value %p (dev)", __FUNCTION__, dev);
813
814 return retval;
815
816error:
817 adu_delete(dev);
818 return retval;
819}
820
821/**
822 * adu_disconnect
823 *
824 * Called by the usb core when the device is removed from the system.
825 */
826static void adu_disconnect(struct usb_interface *interface)
827{
828 struct adu_device *dev;
829 int minor;
830
831 dbg(2," %s : enter", __FUNCTION__);
832
Steven Haigh03270632006-08-09 07:42:06 +1000833 dev = usb_get_intfdata(interface);
834 usb_set_intfdata(interface, NULL);
835
Steven Haigh03270632006-08-09 07:42:06 +1000836 minor = dev->minor;
837
838 /* give back our minor */
839 usb_deregister_dev(interface, &adu_class);
840 dev->minor = 0;
841
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200842 mutex_lock(&dev->mtx); /* not interruptible */
Alan Sternd4ead162007-05-22 11:46:41 -0400843
Steven Haigh03270632006-08-09 07:42:06 +1000844 /* if the device is not opened, then we clean up right now */
845 dbg(2," %s : open count %d", __FUNCTION__, dev->open_count);
846 if (!dev->open_count) {
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200847 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000848 adu_delete(dev);
849 } else {
850 dev->udev = NULL;
Matthias Kaehlcke8293c562007-07-13 21:28:31 +0200851 mutex_unlock(&dev->mtx);
Steven Haigh03270632006-08-09 07:42:06 +1000852 }
853
Joe Perches898eb712007-10-18 03:06:30 -0700854 dev_info(&interface->dev, "ADU device adutux%d now disconnected\n",
Steven Haigh03270632006-08-09 07:42:06 +1000855 (minor - ADU_MINOR_BASE));
856
857 dbg(2," %s : leave", __FUNCTION__);
858}
859
860/* usb specific object needed to register this driver with the usb subsystem */
861static struct usb_driver adu_driver = {
862 .name = "adutux",
863 .probe = adu_probe,
864 .disconnect = adu_disconnect,
865 .id_table = device_table,
866};
867
868static int __init adu_init(void)
869{
870 int result;
871
872 dbg(2," %s : enter", __FUNCTION__);
873
874 /* register this driver with the USB subsystem */
875 result = usb_register(&adu_driver);
876 if (result < 0) {
877 err("usb_register failed for the "__FILE__" driver. "
878 "Error number %d", result);
879 goto exit;
880 }
881
882 info("adutux " DRIVER_DESC " " DRIVER_VERSION);
883 info("adutux is an experimental driver. Use at your own risk");
884
885exit:
886 dbg(2," %s : leave, return value %d", __FUNCTION__, result);
887
888 return result;
889}
890
891static void __exit adu_exit(void)
892{
893 dbg(2," %s : enter", __FUNCTION__);
894 /* deregister this driver with the USB subsystem */
895 usb_deregister(&adu_driver);
896 dbg(2," %s : leave", __FUNCTION__);
897}
898
899module_init(adu_init);
900module_exit(adu_exit);
901
902MODULE_AUTHOR(DRIVER_AUTHOR);
903MODULE_DESCRIPTION(DRIVER_DESC);
904MODULE_LICENSE("GPL");