Merge remote-tracking branch 'usb/usb-next'
[deliverable/linux.git] / drivers / usb / misc / adutux.c
CommitLineData
03270634
SH
1/*
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
28f47c34
GKH
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
03270634
SH
23#include <linux/kernel.h>
24#include <linux/errno.h>
03270634
SH
25#include <linux/slab.h>
26#include <linux/module.h>
27#include <linux/usb.h>
8293c568 28#include <linux/mutex.h>
40cf4833 29#include <linux/uaccess.h>
03270634 30
03270634
SH
31/* Version Information */
32#define DRIVER_VERSION "v0.0.13"
33#define DRIVER_AUTHOR "John Homppi"
34#define DRIVER_DESC "adutux (see www.ontrak.net)"
35
03270634
SH
36/* Define these values to match your device */
37#define ADU_VENDOR_ID 0x0a07
38#define ADU_PRODUCT_ID 0x0064
39
40/* table of devices that work with this driver */
33b9e162 41static const struct usb_device_id device_table[] = {
03270634 42 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID) }, /* ADU100 */
eb79c01a
LN
43 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+20) }, /* ADU120 */
44 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+30) }, /* ADU130 */
03270634
SH
45 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+100) }, /* ADU200 */
46 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+108) }, /* ADU208 */
47 { USB_DEVICE(ADU_VENDOR_ID, ADU_PRODUCT_ID+118) }, /* ADU218 */
83fc1fcd 48 { } /* Terminating entry */
03270634
SH
49};
50
51MODULE_DEVICE_TABLE(usb, device_table);
52
53#ifdef CONFIG_USB_DYNAMIC_MINORS
54#define ADU_MINOR_BASE 0
55#else
56#define ADU_MINOR_BASE 67
57#endif
58
59/* we can have up to this number of device plugged in at once */
60#define MAX_DEVICES 16
61
62#define COMMAND_TIMEOUT (2*HZ) /* 60 second timeout for a command */
63
f08812d5
PZ
64/*
65 * The locking scheme is a vanilla 3-lock:
66 * adu_device.buflock: A spinlock, covers what IRQs touch.
67 * adutux_mutex: A Static lock to cover open_count. It would also cover
68 * any globals, but we don't have them in 2.6.
69 * adu_device.mtx: A mutex to hold across sleepers like copy_from_user.
70 * It covers all of adu_device, except the open_count
71 * and what .buflock covers.
72 */
73
03270634
SH
74/* Structure to hold all of our device specific stuff */
75struct adu_device {
f08812d5 76 struct mutex mtx;
30c5c649
LN
77 struct usb_device *udev; /* save off the usb device pointer */
78 struct usb_interface *interface;
f08812d5 79 unsigned int minor; /* the starting minor number for this device */
03270634
SH
80 char serial_number[8];
81
82 int open_count; /* number of times this port has been opened */
83
30c5c649 84 char *read_buffer_primary;
03270634 85 int read_buffer_length;
30c5c649 86 char *read_buffer_secondary;
03270634
SH
87 int secondary_head;
88 int secondary_tail;
89 spinlock_t buflock;
90
91 wait_queue_head_t read_wait;
92 wait_queue_head_t write_wait;
93
30c5c649
LN
94 char *interrupt_in_buffer;
95 struct usb_endpoint_descriptor *interrupt_in_endpoint;
96 struct urb *interrupt_in_urb;
03270634
SH
97 int read_urb_finished;
98
30c5c649
LN
99 char *interrupt_out_buffer;
100 struct usb_endpoint_descriptor *interrupt_out_endpoint;
101 struct urb *interrupt_out_urb;
f08812d5 102 int out_urb_finished;
03270634
SH
103};
104
f08812d5
PZ
105static DEFINE_MUTEX(adutux_mutex);
106
03270634
SH
107static struct usb_driver adu_driver;
108
1ef37c60
GKH
109static inline void adu_debug_data(struct device *dev, const char *function,
110 int size, const unsigned char *data)
03270634 111{
1ef37c60
GKH
112 dev_dbg(dev, "%s - length = %d, data = %*ph\n",
113 function, size, size, data);
03270634
SH
114}
115
116/**
117 * adu_abort_transfers
118 * aborts transfers and frees associated data structures
119 */
120static void adu_abort_transfers(struct adu_device *dev)
121{
f08812d5 122 unsigned long flags;
03270634 123
66d4bc30 124 if (dev->udev == NULL)
6e42a158 125 return;
03270634 126
03270634 127 /* shutdown transfer */
f08812d5
PZ
128
129 /* XXX Anchor these instead */
130 spin_lock_irqsave(&dev->buflock, flags);
131 if (!dev->read_urb_finished) {
132 spin_unlock_irqrestore(&dev->buflock, flags);
133 usb_kill_urb(dev->interrupt_in_urb);
134 } else
135 spin_unlock_irqrestore(&dev->buflock, flags);
136
137 spin_lock_irqsave(&dev->buflock, flags);
138 if (!dev->out_urb_finished) {
139 spin_unlock_irqrestore(&dev->buflock, flags);
140 usb_kill_urb(dev->interrupt_out_urb);
141 } else
142 spin_unlock_irqrestore(&dev->buflock, flags);
03270634
SH
143}
144
145static void adu_delete(struct adu_device *dev)
146{
03270634
SH
147 /* free data structures */
148 usb_free_urb(dev->interrupt_in_urb);
149 usb_free_urb(dev->interrupt_out_urb);
150 kfree(dev->read_buffer_primary);
151 kfree(dev->read_buffer_secondary);
152 kfree(dev->interrupt_in_buffer);
153 kfree(dev->interrupt_out_buffer);
154 kfree(dev);
03270634
SH
155}
156
7d12e780 157static void adu_interrupt_in_callback(struct urb *urb)
03270634
SH
158{
159 struct adu_device *dev = urb->context;
24497a00 160 int status = urb->status;
03270634 161
1ef37c60
GKH
162 adu_debug_data(&dev->udev->dev, __func__,
163 urb->actual_length, urb->transfer_buffer);
03270634
SH
164
165 spin_lock(&dev->buflock);
166
24497a00 167 if (status != 0) {
f6c1ceaa
ON
168 if ((status != -ENOENT) && (status != -ECONNRESET) &&
169 (status != -ESHUTDOWN)) {
66d4bc30
GKH
170 dev_dbg(&dev->udev->dev,
171 "%s : nonzero status received: %d\n",
172 __func__, status);
03270634
SH
173 }
174 goto exit;
175 }
176
177 if (urb->actual_length > 0 && dev->interrupt_in_buffer[0] != 0x00) {
178 if (dev->read_buffer_length <
29cc8897 179 (4 * usb_endpoint_maxp(dev->interrupt_in_endpoint)) -
03270634
SH
180 (urb->actual_length)) {
181 memcpy (dev->read_buffer_primary +
182 dev->read_buffer_length,
183 dev->interrupt_in_buffer, urb->actual_length);
184
185 dev->read_buffer_length += urb->actual_length;
66d4bc30
GKH
186 dev_dbg(&dev->udev->dev,"%s reading %d\n", __func__,
187 urb->actual_length);
03270634 188 } else {
66d4bc30
GKH
189 dev_dbg(&dev->udev->dev,"%s : read_buffer overflow\n",
190 __func__);
03270634
SH
191 }
192 }
193
194exit:
195 dev->read_urb_finished = 1;
196 spin_unlock(&dev->buflock);
197 /* always wake up so we recover from errors */
198 wake_up_interruptible(&dev->read_wait);
03270634
SH
199}
200
7d12e780 201static void adu_interrupt_out_callback(struct urb *urb)
03270634
SH
202{
203 struct adu_device *dev = urb->context;
24497a00 204 int status = urb->status;
03270634 205
1ef37c60
GKH
206 adu_debug_data(&dev->udev->dev, __func__,
207 urb->actual_length, urb->transfer_buffer);
03270634 208
24497a00
GKH
209 if (status != 0) {
210 if ((status != -ENOENT) &&
211 (status != -ECONNRESET)) {
66d4bc30
GKH
212 dev_dbg(&dev->udev->dev,
213 "%s :nonzero status received: %d\n", __func__,
214 status);
03270634 215 }
1ef37c60 216 return;
03270634
SH
217 }
218
f08812d5
PZ
219 spin_lock(&dev->buflock);
220 dev->out_urb_finished = 1;
221 wake_up(&dev->write_wait);
222 spin_unlock(&dev->buflock);
03270634
SH
223}
224
225static int adu_open(struct inode *inode, struct file *file)
226{
227 struct adu_device *dev = NULL;
228 struct usb_interface *interface;
229 int subminor;
f08812d5 230 int retval;
03270634 231
03270634
SH
232 subminor = iminor(inode);
233
e77c4e6a 234 retval = mutex_lock_interruptible(&adutux_mutex);
66d4bc30 235 if (retval)
f08812d5 236 goto exit_no_lock;
f08812d5 237
03270634
SH
238 interface = usb_find_interface(&adu_driver, subminor);
239 if (!interface) {
28f47c34
GKH
240 pr_err("%s - error, can't find device for minor %d\n",
241 __func__, subminor);
03270634
SH
242 retval = -ENODEV;
243 goto exit_no_device;
244 }
245
246 dev = usb_get_intfdata(interface);
f08812d5 247 if (!dev || !dev->udev) {
03270634
SH
248 retval = -ENODEV;
249 goto exit_no_device;
250 }
251
f08812d5
PZ
252 /* check that nobody else is using the device */
253 if (dev->open_count) {
254 retval = -EBUSY;
03270634
SH
255 goto exit_no_device;
256 }
257
03270634 258 ++dev->open_count;
66d4bc30
GKH
259 dev_dbg(&dev->udev->dev, "%s: open count %d\n", __func__,
260 dev->open_count);
03270634
SH
261
262 /* save device in the file's private structure */
263 file->private_data = dev;
264
f08812d5
PZ
265 /* initialize in direction */
266 dev->read_buffer_length = 0;
03270634 267
f08812d5 268 /* fixup first read by having urb waiting for it */
05d76399 269 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
f08812d5
PZ
270 usb_rcvintpipe(dev->udev,
271 dev->interrupt_in_endpoint->bEndpointAddress),
272 dev->interrupt_in_buffer,
29cc8897 273 usb_endpoint_maxp(dev->interrupt_in_endpoint),
f08812d5
PZ
274 adu_interrupt_in_callback, dev,
275 dev->interrupt_in_endpoint->bInterval);
276 dev->read_urb_finished = 0;
277 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL))
278 dev->read_urb_finished = 1;
279 /* we ignore failure */
280 /* end of fixup for first read */
281
282 /* initialize out direction */
283 dev->out_urb_finished = 1;
284
285 retval = 0;
03270634
SH
286
287exit_no_device:
f08812d5
PZ
288 mutex_unlock(&adutux_mutex);
289exit_no_lock:
03270634
SH
290 return retval;
291}
292
f08812d5 293static void adu_release_internal(struct adu_device *dev)
03270634 294{
03270634
SH
295 /* decrement our usage count for the device */
296 --dev->open_count;
66d4bc30
GKH
297 dev_dbg(&dev->udev->dev, "%s : open count %d\n", __func__,
298 dev->open_count);
03270634
SH
299 if (dev->open_count <= 0) {
300 adu_abort_transfers(dev);
301 dev->open_count = 0;
302 }
03270634
SH
303}
304
305static int adu_release(struct inode *inode, struct file *file)
306{
f08812d5 307 struct adu_device *dev;
03270634
SH
308 int retval = 0;
309
03270634 310 if (file == NULL) {
03270634
SH
311 retval = -ENODEV;
312 goto exit;
313 }
314
315 dev = file->private_data;
03270634 316 if (dev == NULL) {
03270634
SH
317 retval = -ENODEV;
318 goto exit;
319 }
320
f08812d5 321 mutex_lock(&adutux_mutex); /* not interruptible */
03270634
SH
322
323 if (dev->open_count <= 0) {
66d4bc30 324 dev_dbg(&dev->udev->dev, "%s : device not opened\n", __func__);
03270634 325 retval = -ENODEV;
46c9844c 326 goto unlock;
03270634
SH
327 }
328
f08812d5 329 adu_release_internal(dev);
d4ead16f
AS
330 if (dev->udev == NULL) {
331 /* the device was unplugged before the file was released */
f08812d5
PZ
332 if (!dev->open_count) /* ... and we're the last user */
333 adu_delete(dev);
d4ead16f 334 }
46c9844c 335unlock:
f08812d5 336 mutex_unlock(&adutux_mutex);
46c9844c 337exit:
03270634
SH
338 return retval;
339}
340
341static ssize_t adu_read(struct file *file, __user char *buffer, size_t count,
342 loff_t *ppos)
343{
344 struct adu_device *dev;
345 size_t bytes_read = 0;
346 size_t bytes_to_read = count;
347 int i;
348 int retval = 0;
349 int timeout = 0;
350 int should_submit = 0;
351 unsigned long flags;
352 DECLARE_WAITQUEUE(wait, current);
353
03270634 354 dev = file->private_data;
8293c568 355 if (mutex_lock_interruptible(&dev->mtx))
03270634
SH
356 return -ERESTARTSYS;
357
358 /* verify that the device wasn't unplugged */
f08812d5 359 if (dev->udev == NULL) {
03270634 360 retval = -ENODEV;
28f47c34 361 pr_err("No device or device unplugged %d\n", retval);
03270634
SH
362 goto exit;
363 }
364
365 /* verify that some data was requested */
366 if (count == 0) {
66d4bc30
GKH
367 dev_dbg(&dev->udev->dev, "%s : read request of 0 bytes\n",
368 __func__);
03270634
SH
369 goto exit;
370 }
371
372 timeout = COMMAND_TIMEOUT;
66d4bc30 373 dev_dbg(&dev->udev->dev, "%s : about to start looping\n", __func__);
03270634
SH
374 while (bytes_to_read) {
375 int data_in_secondary = dev->secondary_tail - dev->secondary_head;
66d4bc30
GKH
376 dev_dbg(&dev->udev->dev,
377 "%s : while, data_in_secondary=%d, status=%d\n",
378 __func__, data_in_secondary,
379 dev->interrupt_in_urb->status);
03270634
SH
380
381 if (data_in_secondary) {
382 /* drain secondary buffer */
383 int amount = bytes_to_read < data_in_secondary ? bytes_to_read : data_in_secondary;
384 i = copy_to_user(buffer, dev->read_buffer_secondary+dev->secondary_head, amount);
1865a9c3 385 if (i) {
03270634
SH
386 retval = -EFAULT;
387 goto exit;
388 }
389 dev->secondary_head += (amount - i);
390 bytes_read += (amount - i);
391 bytes_to_read -= (amount - i);
392 if (i) {
393 retval = bytes_read ? bytes_read : -EFAULT;
394 goto exit;
395 }
396 } else {
397 /* we check the primary buffer */
398 spin_lock_irqsave (&dev->buflock, flags);
399 if (dev->read_buffer_length) {
400 /* we secure access to the primary */
401 char *tmp;
66d4bc30
GKH
402 dev_dbg(&dev->udev->dev,
403 "%s : swap, read_buffer_length = %d\n",
404 __func__, dev->read_buffer_length);
03270634
SH
405 tmp = dev->read_buffer_secondary;
406 dev->read_buffer_secondary = dev->read_buffer_primary;
407 dev->read_buffer_primary = tmp;
408 dev->secondary_head = 0;
409 dev->secondary_tail = dev->read_buffer_length;
410 dev->read_buffer_length = 0;
411 spin_unlock_irqrestore(&dev->buflock, flags);
412 /* we have a free buffer so use it */
413 should_submit = 1;
414 } else {
415 /* even the primary was empty - we may need to do IO */
f08812d5 416 if (!dev->read_urb_finished) {
03270634
SH
417 /* somebody is doing IO */
418 spin_unlock_irqrestore(&dev->buflock, flags);
66d4bc30
GKH
419 dev_dbg(&dev->udev->dev,
420 "%s : submitted already\n",
421 __func__);
03270634
SH
422 } else {
423 /* we must initiate input */
66d4bc30
GKH
424 dev_dbg(&dev->udev->dev,
425 "%s : initiate input\n",
426 __func__);
03270634 427 dev->read_urb_finished = 0;
f08812d5 428 spin_unlock_irqrestore(&dev->buflock, flags);
03270634 429
05d76399 430 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
eb79c01a
LN
431 usb_rcvintpipe(dev->udev,
432 dev->interrupt_in_endpoint->bEndpointAddress),
03270634 433 dev->interrupt_in_buffer,
29cc8897 434 usb_endpoint_maxp(dev->interrupt_in_endpoint),
03270634
SH
435 adu_interrupt_in_callback,
436 dev,
437 dev->interrupt_in_endpoint->bInterval);
f08812d5
PZ
438 retval = usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL);
439 if (retval) {
440 dev->read_urb_finished = 1;
03270634
SH
441 if (retval == -ENOMEM) {
442 retval = bytes_read ? bytes_read : -ENOMEM;
443 }
66d4bc30
GKH
444 dev_dbg(&dev->udev->dev,
445 "%s : submit failed\n",
446 __func__);
03270634
SH
447 goto exit;
448 }
449 }
450
451 /* we wait for I/O to complete */
452 set_current_state(TASK_INTERRUPTIBLE);
453 add_wait_queue(&dev->read_wait, &wait);
f08812d5
PZ
454 spin_lock_irqsave(&dev->buflock, flags);
455 if (!dev->read_urb_finished) {
456 spin_unlock_irqrestore(&dev->buflock, flags);
03270634 457 timeout = schedule_timeout(COMMAND_TIMEOUT);
f08812d5
PZ
458 } else {
459 spin_unlock_irqrestore(&dev->buflock, flags);
03270634 460 set_current_state(TASK_RUNNING);
f08812d5 461 }
03270634
SH
462 remove_wait_queue(&dev->read_wait, &wait);
463
464 if (timeout <= 0) {
66d4bc30
GKH
465 dev_dbg(&dev->udev->dev,
466 "%s : timeout\n", __func__);
03270634
SH
467 retval = bytes_read ? bytes_read : -ETIMEDOUT;
468 goto exit;
469 }
470
471 if (signal_pending(current)) {
66d4bc30
GKH
472 dev_dbg(&dev->udev->dev,
473 "%s : signal pending\n",
474 __func__);
03270634
SH
475 retval = bytes_read ? bytes_read : -EINTR;
476 goto exit;
477 }
478 }
479 }
480 }
481
482 retval = bytes_read;
483 /* if the primary buffer is empty then use it */
f08812d5
PZ
484 spin_lock_irqsave(&dev->buflock, flags);
485 if (should_submit && dev->read_urb_finished) {
486 dev->read_urb_finished = 0;
487 spin_unlock_irqrestore(&dev->buflock, flags);
05d76399 488 usb_fill_int_urb(dev->interrupt_in_urb, dev->udev,
03270634 489 usb_rcvintpipe(dev->udev,
eb79c01a 490 dev->interrupt_in_endpoint->bEndpointAddress),
f08812d5 491 dev->interrupt_in_buffer,
29cc8897 492 usb_endpoint_maxp(dev->interrupt_in_endpoint),
f08812d5
PZ
493 adu_interrupt_in_callback,
494 dev,
495 dev->interrupt_in_endpoint->bInterval);
496 if (usb_submit_urb(dev->interrupt_in_urb, GFP_KERNEL) != 0)
497 dev->read_urb_finished = 1;
03270634 498 /* we ignore failure */
f08812d5
PZ
499 } else {
500 spin_unlock_irqrestore(&dev->buflock, flags);
03270634
SH
501 }
502
503exit:
504 /* unlock the device */
8293c568 505 mutex_unlock(&dev->mtx);
03270634 506
03270634
SH
507 return retval;
508}
509
510static ssize_t adu_write(struct file *file, const __user char *buffer,
511 size_t count, loff_t *ppos)
512{
f08812d5 513 DECLARE_WAITQUEUE(waita, current);
03270634
SH
514 struct adu_device *dev;
515 size_t bytes_written = 0;
516 size_t bytes_to_write;
517 size_t buffer_size;
f08812d5 518 unsigned long flags;
ebc3ac14 519 int retval;
03270634 520
03270634
SH
521 dev = file->private_data;
522
8293c568 523 retval = mutex_lock_interruptible(&dev->mtx);
ebc3ac14
ON
524 if (retval)
525 goto exit_nolock;
03270634
SH
526
527 /* verify that the device wasn't unplugged */
f08812d5 528 if (dev->udev == NULL) {
03270634 529 retval = -ENODEV;
28f47c34 530 pr_err("No device or device unplugged %d\n", retval);
03270634
SH
531 goto exit;
532 }
533
534 /* verify that we actually have some data to write */
535 if (count == 0) {
66d4bc30
GKH
536 dev_dbg(&dev->udev->dev, "%s : write request of 0 bytes\n",
537 __func__);
03270634
SH
538 goto exit;
539 }
540
03270634 541 while (count > 0) {
f08812d5
PZ
542 add_wait_queue(&dev->write_wait, &waita);
543 set_current_state(TASK_INTERRUPTIBLE);
544 spin_lock_irqsave(&dev->buflock, flags);
545 if (!dev->out_urb_finished) {
546 spin_unlock_irqrestore(&dev->buflock, flags);
03270634 547
f08812d5
PZ
548 mutex_unlock(&dev->mtx);
549 if (signal_pending(current)) {
66d4bc30
GKH
550 dev_dbg(&dev->udev->dev, "%s : interrupted\n",
551 __func__);
f08812d5 552 set_current_state(TASK_RUNNING);
03270634 553 retval = -EINTR;
f08812d5 554 goto exit_onqueue;
03270634 555 }
f08812d5 556 if (schedule_timeout(COMMAND_TIMEOUT) == 0) {
66d4bc30
GKH
557 dev_dbg(&dev->udev->dev,
558 "%s - command timed out.\n", __func__);
f08812d5
PZ
559 retval = -ETIMEDOUT;
560 goto exit_onqueue;
561 }
562 remove_wait_queue(&dev->write_wait, &waita);
8293c568 563 retval = mutex_lock_interruptible(&dev->mtx);
ebc3ac14
ON
564 if (retval) {
565 retval = bytes_written ? bytes_written : retval;
566 goto exit_nolock;
567 }
03270634 568
66d4bc30
GKH
569 dev_dbg(&dev->udev->dev,
570 "%s : in progress, count = %Zd\n",
571 __func__, count);
03270634 572 } else {
f08812d5
PZ
573 spin_unlock_irqrestore(&dev->buflock, flags);
574 set_current_state(TASK_RUNNING);
575 remove_wait_queue(&dev->write_wait, &waita);
66d4bc30
GKH
576 dev_dbg(&dev->udev->dev, "%s : sending, count = %Zd\n",
577 __func__, count);
03270634
SH
578
579 /* write the data into interrupt_out_buffer from userspace */
29cc8897 580 buffer_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
03270634 581 bytes_to_write = count > buffer_size ? buffer_size : count;
66d4bc30
GKH
582 dev_dbg(&dev->udev->dev,
583 "%s : buffer_size = %Zd, count = %Zd, bytes_to_write = %Zd\n",
584 __func__, buffer_size, count, bytes_to_write);
03270634
SH
585
586 if (copy_from_user(dev->interrupt_out_buffer, buffer, bytes_to_write) != 0) {
587 retval = -EFAULT;
588 goto exit;
589 }
590
591 /* send off the urb */
592 usb_fill_int_urb(
593 dev->interrupt_out_urb,
594 dev->udev,
595 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
596 dev->interrupt_out_buffer,
597 bytes_to_write,
598 adu_interrupt_out_callback,
599 dev,
f08812d5 600 dev->interrupt_out_endpoint->bInterval);
03270634 601 dev->interrupt_out_urb->actual_length = bytes_to_write;
f08812d5 602 dev->out_urb_finished = 0;
03270634
SH
603 retval = usb_submit_urb(dev->interrupt_out_urb, GFP_KERNEL);
604 if (retval < 0) {
f08812d5 605 dev->out_urb_finished = 1;
fd3f1917
GKH
606 dev_err(&dev->udev->dev, "Couldn't submit "
607 "interrupt_out_urb %d\n", retval);
03270634
SH
608 goto exit;
609 }
610
611 buffer += bytes_to_write;
612 count -= bytes_to_write;
613
614 bytes_written += bytes_to_write;
615 }
616 }
f08812d5
PZ
617 mutex_unlock(&dev->mtx);
618 return bytes_written;
03270634
SH
619
620exit:
8293c568 621 mutex_unlock(&dev->mtx);
ebc3ac14 622exit_nolock:
f08812d5 623 return retval;
03270634 624
f08812d5
PZ
625exit_onqueue:
626 remove_wait_queue(&dev->write_wait, &waita);
03270634
SH
627 return retval;
628}
629
630/* file operations needed when we register this driver */
00977a59 631static const struct file_operations adu_fops = {
03270634
SH
632 .owner = THIS_MODULE,
633 .read = adu_read,
634 .write = adu_write,
635 .open = adu_open,
636 .release = adu_release,
6038f373 637 .llseek = noop_llseek,
03270634
SH
638};
639
640/*
641 * usb class driver info in order to get a minor number from the usb core,
642 * and to have the device registered with devfs and the driver core
643 */
644static struct usb_class_driver adu_class = {
645 .name = "usb/adutux%d",
646 .fops = &adu_fops,
647 .minor_base = ADU_MINOR_BASE,
648};
649
650/**
651 * adu_probe
652 *
653 * Called by the usb core when a new device is connected that it thinks
654 * this driver might be interested in.
655 */
656static int adu_probe(struct usb_interface *interface,
657 const struct usb_device_id *id)
658{
659 struct usb_device *udev = interface_to_usbdev(interface);
660 struct adu_device *dev = NULL;
661 struct usb_host_interface *iface_desc;
662 struct usb_endpoint_descriptor *endpoint;
663 int retval = -ENODEV;
664 int in_end_size;
665 int out_end_size;
666 int i;
667
03270634
SH
668 if (udev == NULL) {
669 dev_err(&interface->dev, "udev is NULL.\n");
670 goto exit;
671 }
672
b595076a 673 /* allocate memory for our device state and initialize it */
03270634 674 dev = kzalloc(sizeof(struct adu_device), GFP_KERNEL);
a02b55c8 675 if (!dev) {
03270634
SH
676 retval = -ENOMEM;
677 goto exit;
678 }
679
8293c568 680 mutex_init(&dev->mtx);
03270634
SH
681 spin_lock_init(&dev->buflock);
682 dev->udev = udev;
683 init_waitqueue_head(&dev->read_wait);
684 init_waitqueue_head(&dev->write_wait);
685
686 iface_desc = &interface->altsetting[0];
687
688 /* set up the endpoint information */
689 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
690 endpoint = &iface_desc->endpoint[i].desc;
691
692 if (usb_endpoint_is_int_in(endpoint))
693 dev->interrupt_in_endpoint = endpoint;
694
695 if (usb_endpoint_is_int_out(endpoint))
696 dev->interrupt_out_endpoint = endpoint;
697 }
698 if (dev->interrupt_in_endpoint == NULL) {
699 dev_err(&interface->dev, "interrupt in endpoint not found\n");
700 goto error;
701 }
702 if (dev->interrupt_out_endpoint == NULL) {
703 dev_err(&interface->dev, "interrupt out endpoint not found\n");
704 goto error;
705 }
706
29cc8897
KM
707 in_end_size = usb_endpoint_maxp(dev->interrupt_in_endpoint);
708 out_end_size = usb_endpoint_maxp(dev->interrupt_out_endpoint);
03270634
SH
709
710 dev->read_buffer_primary = kmalloc((4 * in_end_size), GFP_KERNEL);
711 if (!dev->read_buffer_primary) {
03270634
SH
712 retval = -ENOMEM;
713 goto error;
714 }
715
716 /* debug code prime the buffer */
717 memset(dev->read_buffer_primary, 'a', in_end_size);
718 memset(dev->read_buffer_primary + in_end_size, 'b', in_end_size);
719 memset(dev->read_buffer_primary + (2 * in_end_size), 'c', in_end_size);
720 memset(dev->read_buffer_primary + (3 * in_end_size), 'd', in_end_size);
721
722 dev->read_buffer_secondary = kmalloc((4 * in_end_size), GFP_KERNEL);
723 if (!dev->read_buffer_secondary) {
03270634
SH
724 retval = -ENOMEM;
725 goto error;
726 }
727
728 /* debug code prime the buffer */
729 memset(dev->read_buffer_secondary, 'e', in_end_size);
730 memset(dev->read_buffer_secondary + in_end_size, 'f', in_end_size);
731 memset(dev->read_buffer_secondary + (2 * in_end_size), 'g', in_end_size);
732 memset(dev->read_buffer_secondary + (3 * in_end_size), 'h', in_end_size);
733
734 dev->interrupt_in_buffer = kmalloc(in_end_size, GFP_KERNEL);
a02b55c8 735 if (!dev->interrupt_in_buffer)
03270634 736 goto error;
03270634
SH
737
738 /* debug code prime the buffer */
739 memset(dev->interrupt_in_buffer, 'i', in_end_size);
740
741 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
71574a55 742 if (!dev->interrupt_in_urb)
03270634 743 goto error;
03270634 744 dev->interrupt_out_buffer = kmalloc(out_end_size, GFP_KERNEL);
a02b55c8 745 if (!dev->interrupt_out_buffer)
03270634 746 goto error;
03270634 747 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
71574a55 748 if (!dev->interrupt_out_urb)
03270634 749 goto error;
03270634
SH
750
751 if (!usb_string(udev, udev->descriptor.iSerialNumber, dev->serial_number,
752 sizeof(dev->serial_number))) {
753 dev_err(&interface->dev, "Could not retrieve serial number\n");
754 goto error;
755 }
66d4bc30 756 dev_dbg(&interface->dev,"serial_number=%s", dev->serial_number);
03270634
SH
757
758 /* we can register the device now, as it is ready */
759 usb_set_intfdata(interface, dev);
760
761 retval = usb_register_dev(interface, &adu_class);
762
763 if (retval) {
764 /* something prevented us from registering this driver */
765 dev_err(&interface->dev, "Not able to get a minor for this device.\n");
766 usb_set_intfdata(interface, NULL);
767 goto error;
768 }
769
770 dev->minor = interface->minor;
771
772 /* let the user know what node this device is now attached to */
898eb71c 773 dev_info(&interface->dev, "ADU%d %s now attached to /dev/usb/adutux%d\n",
d482b9d5 774 le16_to_cpu(udev->descriptor.idProduct), dev->serial_number,
03270634
SH
775 (dev->minor - ADU_MINOR_BASE));
776exit:
03270634
SH
777 return retval;
778
779error:
780 adu_delete(dev);
781 return retval;
782}
783
784/**
785 * adu_disconnect
786 *
787 * Called by the usb core when the device is removed from the system.
788 */
789static void adu_disconnect(struct usb_interface *interface)
790{
791 struct adu_device *dev;
792 int minor;
793
03270634 794 dev = usb_get_intfdata(interface);
03270634 795
f08812d5
PZ
796 mutex_lock(&dev->mtx); /* not interruptible */
797 dev->udev = NULL; /* poison */
03270634 798 minor = dev->minor;
03270634 799 usb_deregister_dev(interface, &adu_class);
f08812d5 800 mutex_unlock(&dev->mtx);
03270634 801
f08812d5
PZ
802 mutex_lock(&adutux_mutex);
803 usb_set_intfdata(interface, NULL);
d4ead16f 804
03270634 805 /* if the device is not opened, then we clean up right now */
f08812d5 806 if (!dev->open_count)
03270634 807 adu_delete(dev);
f08812d5
PZ
808
809 mutex_unlock(&adutux_mutex);
03270634
SH
810}
811
812/* usb specific object needed to register this driver with the usb subsystem */
813static struct usb_driver adu_driver = {
814 .name = "adutux",
815 .probe = adu_probe,
816 .disconnect = adu_disconnect,
817 .id_table = device_table,
818};
819
65db4305 820module_usb_driver(adu_driver);
03270634
SH
821
822MODULE_AUTHOR(DRIVER_AUTHOR);
823MODULE_DESCRIPTION(DRIVER_DESC);
824MODULE_LICENSE("GPL");
This page took 1.0418 seconds and 5 git commands to generate.