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