1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
/* -*- linux-c -*- */
/*
* Driver for USB MPIO
*
* Yuji Touya (salmoon@users.sourceforge.net)
*
* small adaptions for Kernel 2.4.x support by
* Markus Germeier (mager@tzi.de)
*
* based on rio500.c by Cesar Miquel (miquel@df.uba.ar)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* */
#include <linux/config.h>
#if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
# define MODVERSIONS
#endif
#if defined(MODVERSIONS) && !defined(__GENKSYMS__)
# include <linux/modversions.h>
#endif
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/random.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/usb.h>
#include <linux/smp_lock.h>
#include <linux/devfs_fs_kernel.h>
#ifdef CONFIG_USB_DEBUG
# define DEBUG
#else
# undef DEBUG
#endif
/*
* Version Information
*/
#define DRIVER_VERSION "0.0.2"
#define DRIVER_AUTHOR "Yuji Touya <salmoon@users.sourceforge.net>"
#define DRIVER_DESC "USB MPIO driver"
#define MPIO_MINOR 70
/* stall/wait timeout for mpio */
#define NAK_TIMEOUT (HZ)
/* Size of the mpio buffer */
#define IBUF_SIZE 0x10000 /* enough? :salmoon */
#define OBUF_SIZE 0x10000 /* enough? :salmoon */
struct mpio_usb_data {
struct usb_device *mpio_dev; /* init: probe_mpio */
devfs_handle_t devfs; /* devfs device */
unsigned int ifnum; /* Interface number of the USB device */
int isopen; /* nz if open */
int present; /* Device is present on the bus */
char *obuf, *ibuf; /* transfer buffers */
char bulk_in_ep, bulk_out_ep; /* Endpoint assignments */
wait_queue_head_t wait_q; /* for timeouts */
struct semaphore lock; /* general race avoidance */
};
extern devfs_handle_t usb_devfs_handle; /* /dev/usb dir. */
static struct mpio_usb_data mpio_instance;
static int open_mpio(struct inode *inode, struct file *file)
{
struct mpio_usb_data *mpio = &mpio_instance;
lock_kernel();
if (mpio->isopen || !mpio->present) {
unlock_kernel();
return -EBUSY;
}
mpio->isopen = 1;
init_waitqueue_head(&mpio->wait_q);
MOD_INC_USE_COUNT;
unlock_kernel();
info("MPIO opened.");
return 0;
}
static int close_mpio(struct inode *inode, struct file *file)
{
struct mpio_usb_data *mpio = &mpio_instance;
mpio->isopen = 0;
MOD_DEC_USE_COUNT;
info("MPIO closed.");
return 0;
}
static ssize_t
write_mpio(struct file *file, const char *buffer, size_t count, loff_t * ppos)
{
struct mpio_usb_data *mpio = &mpio_instance;
unsigned long copy_size;
unsigned long bytes_written = 0;
unsigned int partial;
int result = 0;
int maxretry;
int errn = 0;
/* Sanity check to make sure mpio is connected, powered, etc */
if ( mpio == NULL ||
mpio->present == 0 ||
mpio->mpio_dev == NULL )
return -1;
down(&(mpio->lock));
do {
unsigned long thistime;
char *obuf = mpio->obuf;
thistime = copy_size =
(count >= OBUF_SIZE) ? OBUF_SIZE : count;
if (copy_from_user(mpio->obuf, buffer, copy_size)) {
errn = -EFAULT;
goto error;
}
maxretry = 5;
while (thistime) {
if (!mpio->mpio_dev) {
errn = -ENODEV;
goto error;
}
if (signal_pending(current)) {
errn = bytes_written ? bytes_written : -EINTR;
goto error;
}
result =
usb_bulk_msg(mpio->mpio_dev,
usb_sndbulkpipe(mpio->mpio_dev, 1),
obuf, thistime, &partial, 5 * HZ);
dbg("write stats: result:%d thistime:%lu partial:%u",
result, thistime, partial);
/* NAK - so hold for a while */
if (result == USB_ST_TIMEOUT) {
if (!maxretry--) {
errn = -ETIME;
goto error;
}
interruptible_sleep_on_timeout(&mpio-> wait_q,
NAK_TIMEOUT);
continue;
} else if (!result & partial) {
obuf += partial;
thistime -= partial;
} else
break;
};
if (result) {
err("Write Whoops - %x", result);
errn = -EIO;
goto error;
}
bytes_written += copy_size;
count -= copy_size;
buffer += copy_size;
} while (count > 0);
errn = bytes_written ? bytes_written : -EIO;
error:
up(&(mpio->lock));
return errn;
}
static ssize_t
read_mpio(struct file *file, char *buffer, size_t count, loff_t * ppos)
{
struct mpio_usb_data *mpio = &mpio_instance;
ssize_t read_count;
unsigned int partial;
int this_read;
int result;
int maxretry = 5;
char *ibuf = mpio->ibuf;
/* Sanity check to make sure mpio is connected, powered, etc */
if ( mpio == NULL ||
mpio->present == 0 ||
mpio->mpio_dev == NULL )
return -1;
read_count = 0;
down(&(mpio->lock));
while (count > 0) {
if (signal_pending(current)) {
up(&(mpio->lock));
return read_count ? read_count : -EINTR;
}
if (!mpio->mpio_dev) {
up(&(mpio->lock));
return -ENODEV;
}
this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
result = usb_bulk_msg(mpio->mpio_dev,
usb_rcvbulkpipe(mpio->mpio_dev, 2),
ibuf, this_read, &partial,
(int) (HZ * 8));
dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
result, this_read, partial);
if (partial) {
count = this_read = partial;
} else if (result == USB_ST_TIMEOUT || result == 15) { /* FIXME: 15 ? */
if (!maxretry--) {
up(&(mpio->lock));
err("read_mpio: maxretry timeout");
return -ETIME;
}
interruptible_sleep_on_timeout(&mpio->wait_q,
NAK_TIMEOUT);
continue;
} else if (result != USB_ST_DATAUNDERRUN) {
up(&(mpio->lock));
err("Read Whoops - result:%u partial:%u this_read:%u",
result, partial, this_read);
return -EIO;
} else {
up(&(mpio->lock));
return (0);
}
if (this_read) {
if (copy_to_user(buffer, ibuf, this_read)) {
up(&(mpio->lock));
return -EFAULT;
}
count -= this_read;
read_count += this_read;
buffer += this_read;
}
}
up(&(mpio->lock));
return read_count;
}
static struct
file_operations usb_mpio_fops = {
read: read_mpio,
write: write_mpio,
/* ioctl: ioctl_mpio, */
open: open_mpio,
release: close_mpio,
};
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0) /* Hmm, .... */
static void *probe_mpio(struct usb_device *dev, unsigned int ifnum,
const struct usb_device_id *id)
#else
static void *probe_mpio(struct usb_device *dev, unsigned int ifnum)
#endif
{
struct mpio_usb_data *mpio = &mpio_instance;
if (dev->descriptor.idVendor != 0x2735 /* Digit@lway */ ) {
return NULL;
}
if (dev->descriptor.idProduct != 0x1 /* MPIO all models??? */ ) {
warn(KERN_INFO "MPIO player model not supported/tested.");
return NULL;
}
info("USB MPIO found at address %d", dev->devnum);
#ifdef DEBUG
usb_show_device(dev); /* Show device info */
#endif
mpio->present = 1;
mpio->mpio_dev = dev;
if (!(mpio->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
err("probe_mpio: Not enough memory for the output buffer");
return NULL;
}
dbg("probe_mpio: obuf address:%p", mpio->obuf);
if (!(mpio->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
err("probe_mpio: Not enough memory for the input buffer");
kfree(mpio->obuf);
return NULL;
}
dbg("probe_mpio: ibuf address:%p", mpio->ibuf);
mpio->devfs = devfs_register(usb_devfs_handle, "mpio",
DEVFS_FL_DEFAULT, USB_MAJOR,
MPIO_MINOR,
S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
S_IWGRP, &usb_mpio_fops, NULL);
if (mpio->devfs == NULL)
dbg("probe_mpio: device node registration failed");
init_MUTEX(&(mpio->lock));
return mpio;
}
static void disconnect_mpio(struct usb_device *dev, void *ptr)
{
struct mpio_usb_data *mpio = (struct mpio_usb_data *) ptr;
devfs_unregister(mpio->devfs);
if (mpio->isopen) {
mpio->isopen = 0;
/* better let it finish - the release will do whats needed */
mpio->mpio_dev = NULL;
return;
}
kfree(mpio->ibuf);
kfree(mpio->obuf);
info("USB MPIO disconnected.");
mpio->present = 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
static struct usb_device_id mpio_table [] = {
{ USB_DEVICE(0x2735, 1) }, /* MPIO-* (all models?) */
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, mpio_table);
#endif
static struct usb_driver mpio_driver = {
name: "mpio",
probe: probe_mpio,
disconnect: disconnect_mpio,
fops: &usb_mpio_fops,
minor: MPIO_MINOR,
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
id_table: mpio_table,
#endif
};
int usb_mpio_init(void)
{
if (usb_register(&mpio_driver) < 0)
return -1;
info("USB MPIO support registered.");
info(DRIVER_VERSION ":" DRIVER_DESC);
return 0;
}
void usb_mpio_cleanup(void)
{
struct mpio_usb_data *mpio = &mpio_instance;
mpio->present = 0;
usb_deregister(&mpio_driver);
}
#ifdef DEBUG
/* ---------- debugging helpers ----------- */
static void usb_show_endpoint(struct usb_endpoint_descriptor *endpoint)
{
usb_show_endpoint_descriptor(endpoint);
}
static void usb_show_interface(struct usb_interface_descriptor *altsetting)
{
int i;
usb_show_interface_descriptor(altsetting);
for (i = 0; i < altsetting->bNumEndpoints; i++)
usb_show_endpoint(altsetting->endpoint + i);
}
static void usb_show_config(struct usb_config_descriptor *config)
{
int i, j;
struct usb_interface *ifp;
usb_show_config_descriptor(config);
for (i = 0; i < config->bNumInterfaces; i++) {
ifp = config->interface + i;
if (!ifp)
break;
printk("\n Interface: %d\n", i);
for (j = 0; j < ifp->num_altsetting; j++)
usb_show_interface(ifp->altsetting + j);
}
}
void usb_show_device(struct usb_device *dev)
{
int i;
usb_show_device_descriptor(&dev->descriptor);
for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
usb_show_config(dev->config + i);
}
/*
* Parse and show the different USB descriptors.
*/
void usb_show_device_descriptor(struct usb_device_descriptor *desc)
{
if (!desc)
{
printk("Invalid USB device descriptor (NULL POINTER)\n");
return;
}
printk(" Length = %2d%s\n", desc->bLength,
desc->bLength == USB_DT_DEVICE_SIZE ? "" : " (!!!)");
printk(" DescriptorType = %02x\n", desc->bDescriptorType);
printk(" USB version = %x.%02x\n",
desc->bcdUSB >> 8, desc->bcdUSB & 0xff);
printk(" Vendor:Product = %04x:%04x\n",
desc->idVendor, desc->idProduct);
printk(" MaxPacketSize0 = %d\n", desc->bMaxPacketSize0);
printk(" NumConfigurations = %d\n", desc->bNumConfigurations);
printk(" Device version = %x.%02x\n",
desc->bcdDevice >> 8, desc->bcdDevice & 0xff);
printk(" Device Class:SubClass:Protocol = %02x:%02x:%02x\n",
desc->bDeviceClass, desc->bDeviceSubClass, desc->bDeviceProtocol);
switch (desc->bDeviceClass) {
case 0:
printk(" Per-interface classes\n");
break;
case USB_CLASS_AUDIO:
printk(" Audio device class\n");
break;
case USB_CLASS_COMM:
printk(" Communications class\n");
break;
case USB_CLASS_HID:
printk(" Human Interface Devices class\n");
break;
case USB_CLASS_PRINTER:
printk(" Printer device class\n");
break;
case USB_CLASS_MASS_STORAGE:
printk(" Mass Storage device class\n");
break;
case USB_CLASS_HUB:
printk(" Hub device class\n");
break;
case USB_CLASS_VENDOR_SPEC:
printk(" Vendor class\n");
break;
default:
printk(" Unknown class\n");
}
}
void usb_show_config_descriptor(struct usb_config_descriptor *desc)
{
printk("Configuration:\n");
printk(" bLength = %4d%s\n", desc->bLength,
desc->bLength == USB_DT_CONFIG_SIZE ? "" : " (!!!)");
printk(" bDescriptorType = %02x\n", desc->bDescriptorType);
printk(" wTotalLength = %04x\n", desc->wTotalLength);
printk(" bNumInterfaces = %02x\n", desc->bNumInterfaces);
printk(" bConfigurationValue = %02x\n", desc->bConfigurationValue);
printk(" iConfiguration = %02x\n", desc->iConfiguration);
printk(" bmAttributes = %02x\n", desc->bmAttributes);
printk(" MaxPower = %4dmA\n", desc->MaxPower * 2);
}
void usb_show_interface_descriptor(struct usb_interface_descriptor *desc)
{
printk(" Alternate Setting: %2d\n", desc->bAlternateSetting);
printk(" bLength = %4d%s\n", desc->bLength,
desc->bLength == USB_DT_INTERFACE_SIZE ? "" : " (!!!)");
printk(" bDescriptorType = %02x\n", desc->bDescriptorType);
printk(" bInterfaceNumber = %02x\n", desc->bInterfaceNumber);
printk(" bAlternateSetting = %02x\n", desc->bAlternateSetting);
printk(" bNumEndpoints = %02x\n", desc->bNumEndpoints);
printk(" bInterface Class:SubClass:Protocol = %02x:%02x:%02x\n",
desc->bInterfaceClass, desc->bInterfaceSubClass, desc->bInterfaceProtocol);
printk(" iInterface = %02x\n", desc->iInterface);
}
void usb_show_endpoint_descriptor(struct usb_endpoint_descriptor *desc)
{
char *LengthCommentString = (desc->bLength ==
USB_DT_ENDPOINT_AUDIO_SIZE) ? " (Audio)" : (desc->bLength ==
USB_DT_ENDPOINT_SIZE) ? "" : " (!!!)";
char *EndpointType[4] = { "Control", "Isochronous", "Bulk", "Interrupt" };
printk(" Endpoint:\n");
printk(" bLength = %4d%s\n",
desc->bLength, LengthCommentString);
printk(" bDescriptorType = %02x\n", desc->bDescriptorType);
printk(" bEndpointAddress = %02x (%s)\n", desc->bEndpointAddress,
(desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
USB_ENDPOINT_XFER_CONTROL ? "i/o" :
(desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? "in" : "out");
printk(" bmAttributes = %02x (%s)\n", desc->bmAttributes,
EndpointType[USB_ENDPOINT_XFERTYPE_MASK & desc->bmAttributes]);
printk(" wMaxPacketSize = %04x\n", desc->wMaxPacketSize);
printk(" bInterval = %02x\n", desc->bInterval);
/* Audio extensions to the endpoint descriptor */
if (desc->bLength == USB_DT_ENDPOINT_AUDIO_SIZE) {
printk(" bRefresh = %02x\n", desc->bRefresh);
printk(" bSynchAddress = %02x\n", desc->bSynchAddress);
}
}
void usb_show_string(struct usb_device *dev, char *id, int index)
{
char *buf;
if (!index)
return;
if (!(buf = kmalloc(256, GFP_KERNEL)))
return;
if (usb_string(dev, index, buf, 256) > 0)
printk(KERN_INFO "%s: %s\n", id, buf);
kfree(buf);
}
void usb_dump_urb (purb_t purb)
{
printk ("urb :%p\n", purb);
printk ("next :%p\n", purb->next);
printk ("dev :%p\n", purb->dev);
printk ("pipe :%08X\n", purb->pipe);
printk ("status :%d\n", purb->status);
printk ("transfer_flags :%08X\n", purb->transfer_flags);
printk ("transfer_buffer :%p\n", purb->transfer_buffer);
printk ("transfer_buffer_length:%d\n", purb->transfer_buffer_length);
printk ("actual_length :%d\n", purb->actual_length);
printk ("setup_packet :%p\n", purb->setup_packet);
printk ("start_frame :%d\n", purb->start_frame);
printk ("number_of_packets :%d\n", purb->number_of_packets);
printk ("interval :%d\n", purb->interval);
printk ("error_count :%d\n", purb->error_count);
printk ("context :%p\n", purb->context);
printk ("complete :%p\n", purb->complete);
}
/* --------- end of helpers ---------- */
#endif /* DEBUG */
module_init(usb_mpio_init);
module_exit(usb_mpio_cleanup);
MODULE_AUTHOR( DRIVER_AUTHOR );
MODULE_DESCRIPTION( DRIVER_DESC );
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,10)
MODULE_LICENSE("GPL");
#endif
|