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