OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [usb/] [rio500.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/* -*- linux-c -*- */
2
 
3
/*
4
 * Driver for USB Rio 500
5
 *
6
 * Cesar Miquel (miquel@df.uba.ar)
7
 *
8
 * based on hp_scanner.c by David E. Nelson (dnelson@jump.net)
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License as
12
 * published by the Free Software Foundation; either version 2 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful, but
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 * General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
 *
24
 * Based upon mouse.c (Brad Keryan) and printer.c (Michael Gee).
25
 *
26
 * */
27
 
28
#include <linux/module.h>
29
#include <linux/kernel.h>
30
#include <linux/signal.h>
31
#include <linux/sched.h>
32
#include <linux/errno.h>
33
#include <linux/miscdevice.h>
34
#include <linux/random.h>
35
#include <linux/poll.h>
36
#include <linux/init.h>
37
#include <linux/slab.h>
38
#include <linux/spinlock.h>
39
#include <linux/usb.h>
40
#include <linux/smp_lock.h>
41
#include <linux/devfs_fs_kernel.h>
42
 
43
#include "rio500_usb.h"
44
 
45
/*
46
 * Version Information
47
 */
48
#define DRIVER_VERSION "v1.1"
49
#define DRIVER_AUTHOR "Cesar Miquel <miquel@df.uba.ar>"
50
#define DRIVER_DESC "USB Rio 500 driver"
51
 
52
#define RIO_MINOR   64
53
 
54
/* stall/wait timeout for rio */
55
#define NAK_TIMEOUT (HZ)
56
 
57
#define IBUF_SIZE 0x1000
58
 
59
/* Size of the rio buffer */
60
#define OBUF_SIZE 0x10000
61
 
62
struct rio_usb_data {
63
        struct usb_device *rio_dev;     /* init: probe_rio */
64
        devfs_handle_t devfs;           /* devfs device */
65
        unsigned int ifnum;             /* Interface number of the USB device */
66
        int isopen;                     /* nz if open */
67
        int present;                    /* Device is present on the bus */
68
        char *obuf, *ibuf;              /* transfer buffers */
69
        char bulk_in_ep, bulk_out_ep;   /* Endpoint assignments */
70
        wait_queue_head_t wait_q;       /* for timeouts */
71
        struct semaphore lock;          /* general race avoidance */
72
};
73
 
74
extern devfs_handle_t usb_devfs_handle; /* /dev/usb dir. */
75
 
76
static struct rio_usb_data rio_instance;
77
 
78
static int open_rio(struct inode *inode, struct file *file)
79
{
80
        struct rio_usb_data *rio = &rio_instance;
81
 
82
        lock_kernel();
83
 
84
        if (rio->isopen || !rio->present) {
85
                unlock_kernel();
86
                return -EBUSY;
87
        }
88
        rio->isopen = 1;
89
 
90
        init_waitqueue_head(&rio->wait_q);
91
 
92
        MOD_INC_USE_COUNT;
93
 
94
        unlock_kernel();
95
 
96
        info("Rio opened.");
97
 
98
        return 0;
99
}
100
 
101
static int close_rio(struct inode *inode, struct file *file)
102
{
103
        struct rio_usb_data *rio = &rio_instance;
104
 
105
        rio->isopen = 0;
106
 
107
        MOD_DEC_USE_COUNT;
108
 
109
        info("Rio closed.");
110
        return 0;
111
}
112
 
113
static int
114
ioctl_rio(struct inode *inode, struct file *file, unsigned int cmd,
115
          unsigned long arg)
116
{
117
        struct RioCommand rio_cmd;
118
        struct rio_usb_data *rio = &rio_instance;
119
        void *data;
120
        unsigned char *buffer;
121
        int result, requesttype;
122
        int retries;
123
        int retval=0;
124
 
125
        down(&(rio->lock));
126
        /* Sanity check to make sure rio is connected, powered, etc */
127
        if ( rio == NULL ||
128
             rio->present == 0 ||
129
             rio->rio_dev == NULL )
130
        {
131
                retval = -ENODEV;
132
                goto err_out;
133
        }
134
 
135
        switch (cmd) {
136
        case RIO_RECV_COMMAND:
137
                data = (void *) arg;
138
                if (data == NULL)
139
                        break;
140
                if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
141
                        retval = -EFAULT;
142
                        goto err_out;
143
                }
144
                if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
145
                        retval = -EINVAL;
146
                        goto err_out;
147
                }
148
                buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
149
                if (buffer == NULL) {
150
                        retval = -ENOMEM;
151
                        goto err_out;
152
                }
153
                if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
154
                        retval = -EFAULT;
155
                        free_page((unsigned long) buffer);
156
                        goto err_out;
157
                }
158
 
159
                requesttype = rio_cmd.requesttype | USB_DIR_IN |
160
                    USB_TYPE_VENDOR | USB_RECIP_DEVICE;
161
                dbg
162
                    ("sending command:reqtype=%0x req=%0x value=%0x index=%0x len=%0x",
163
                     requesttype, rio_cmd.request, rio_cmd.value,
164
                     rio_cmd.index, rio_cmd.length);
165
                /* Send rio control message */
166
                retries = 3;
167
                while (retries) {
168
                        result = usb_control_msg(rio->rio_dev,
169
                                                 usb_rcvctrlpipe(rio-> rio_dev, 0),
170
                                                 rio_cmd.request,
171
                                                 requesttype,
172
                                                 rio_cmd.value,
173
                                                 rio_cmd.index, buffer,
174
                                                 rio_cmd.length,
175
                                                 rio_cmd.timeout);
176
                        if (result == -ETIMEDOUT)
177
                                retries--;
178
                        else if (result < 0) {
179
                                err("Error executing ioctrl. code = %d",
180
                                     le32_to_cpu(result));
181
                                retries = 0;
182
                        } else {
183
                                dbg("Executed ioctl. Result = %d (data=%04x)",
184
                                     le32_to_cpu(result),
185
                                     le32_to_cpu(*((long *) buffer)));
186
                                if (copy_to_user(rio_cmd.buffer, buffer,
187
                                                 rio_cmd.length)) {
188
                                        free_page((unsigned long) buffer);
189
                                        retval = -EFAULT;
190
                                        goto err_out;
191
                                }
192
                                retries = 0;
193
                        }
194
 
195
                        /* rio_cmd.buffer contains a raw stream of single byte
196
                           data which has been returned from rio.  Data is
197
                           interpreted at application level.  For data that
198
                           will be cast to data types longer than 1 byte, data
199
                           will be little_endian and will potentially need to
200
                           be swapped at the app level */
201
 
202
                }
203
                free_page((unsigned long) buffer);
204
                break;
205
 
206
        case RIO_SEND_COMMAND:
207
                data = (void *) arg;
208
                if (data == NULL)
209
                        break;
210
                if (copy_from_user(&rio_cmd, data, sizeof(struct RioCommand))) {
211
                        retval = -EFAULT;
212
                        goto err_out;
213
                }
214
                if (rio_cmd.length < 0 || rio_cmd.length > PAGE_SIZE) {
215
                        retval = -EINVAL;
216
                        goto err_out;
217
                }
218
                buffer = (unsigned char *) __get_free_page(GFP_KERNEL);
219
                if (buffer == NULL) {
220
                        retval = -ENOMEM;
221
                        goto err_out;
222
                }
223
                if (copy_from_user(buffer, rio_cmd.buffer, rio_cmd.length)) {
224
                        free_page((unsigned long)buffer);
225
                        retval = -EFAULT;
226
                        goto err_out;
227
                }
228
 
229
                requesttype = rio_cmd.requesttype | USB_DIR_OUT |
230
                    USB_TYPE_VENDOR | USB_RECIP_DEVICE;
231
                dbg("sending command: reqtype=%0x req=%0x value=%0x index=%0x len=%0x",
232
                     requesttype, rio_cmd.request, rio_cmd.value,
233
                     rio_cmd.index, rio_cmd.length);
234
                /* Send rio control message */
235
                retries = 3;
236
                while (retries) {
237
                        result = usb_control_msg(rio->rio_dev,
238
                                                 usb_sndctrlpipe(rio-> rio_dev, 0),
239
                                                 rio_cmd.request,
240
                                                 requesttype,
241
                                                 rio_cmd.value,
242
                                                 rio_cmd.index, buffer,
243
                                                 rio_cmd.length,
244
                                                 rio_cmd.timeout);
245
                        if (result == -ETIMEDOUT)
246
                                retries--;
247
                        else if (result < 0) {
248
                                err("Error executing ioctrl. code = %d",
249
                                     le32_to_cpu(result));
250
                                retries = 0;
251
                        } else {
252
                                dbg("Executed ioctl. Result = %d",
253
                                       le32_to_cpu(result));
254
                                retries = 0;
255
 
256
                        }
257
 
258
                }
259
                free_page((unsigned long) buffer);
260
                break;
261
 
262
        default:
263
                retval = -ENOTTY;
264
                break;
265
        }
266
 
267
 
268
err_out:
269
        up(&(rio->lock));
270
        return retval;
271
}
272
 
273
static ssize_t
274
write_rio(struct file *file, const char *buffer,
275
          size_t count, loff_t * ppos)
276
{
277
        struct rio_usb_data *rio = &rio_instance;
278
 
279
        unsigned long copy_size;
280
        unsigned long bytes_written = 0;
281
        unsigned int partial;
282
 
283
        int result = 0;
284
        int maxretry;
285
        int errn = 0;
286
 
287
        down(&(rio->lock));
288
        /* Sanity check to make sure rio is connected, powered, etc */
289
        if ( rio == NULL ||
290
             rio->present == 0 ||
291
             rio->rio_dev == NULL )
292
        {
293
                up(&(rio->lock));
294
                return -ENODEV;
295
        }
296
 
297
 
298
 
299
        do {
300
                unsigned long thistime;
301
                char *obuf = rio->obuf;
302
 
303
                thistime = copy_size =
304
                    (count >= OBUF_SIZE) ? OBUF_SIZE : count;
305
                if (copy_from_user(rio->obuf, buffer, copy_size)) {
306
                        errn = -EFAULT;
307
                        goto error;
308
                }
309
                maxretry = 5;
310
                while (thistime) {
311
                        if (!rio->rio_dev) {
312
                                errn = -ENODEV;
313
                                goto error;
314
                        }
315
                        if (signal_pending(current)) {
316
                                up(&(rio->lock));
317
                                return bytes_written ? bytes_written : -EINTR;
318
                        }
319
 
320
                        result = usb_bulk_msg(rio->rio_dev,
321
                                         usb_sndbulkpipe(rio->rio_dev, 2),
322
                                         obuf, thistime, &partial, 5 * HZ);
323
 
324
                        dbg("write stats: result:%d thistime:%lu partial:%u",
325
                             result, thistime, partial);
326
 
327
                        if (result == USB_ST_TIMEOUT) { /* NAK - so hold for a while */
328
                                if (!maxretry--) {
329
                                        errn = -ETIME;
330
                                        goto error;
331
                                }
332
                                interruptible_sleep_on_timeout(&rio-> wait_q, NAK_TIMEOUT);
333
                                continue;
334
                        } else if (!result & partial) {
335
                                obuf += partial;
336
                                thistime -= partial;
337
                        } else
338
                                break;
339
                };
340
                if (result) {
341
                        err("Write Whoops - %x", result);
342
                        errn = -EIO;
343
                        goto error;
344
                }
345
                bytes_written += copy_size;
346
                count -= copy_size;
347
                buffer += copy_size;
348
        } while (count > 0);
349
 
350
        up(&(rio->lock));
351
 
352
        return bytes_written ? bytes_written : -EIO;
353
 
354
error:
355
        up(&(rio->lock));
356
        return errn;
357
}
358
 
359
static ssize_t
360
read_rio(struct file *file, char *buffer, size_t count, loff_t * ppos)
361
{
362
        struct rio_usb_data *rio = &rio_instance;
363
        ssize_t read_count;
364
        unsigned int partial;
365
        int this_read;
366
        int result;
367
        int maxretry = 10;
368
        char *ibuf;
369
 
370
        down(&(rio->lock));
371
        /* Sanity check to make sure rio is connected, powered, etc */
372
        if ( rio == NULL ||
373
             rio->present == 0 ||
374
             rio->rio_dev == NULL )
375
        {
376
                up(&(rio->lock));
377
                return -ENODEV;
378
        }
379
 
380
        ibuf = rio->ibuf;
381
 
382
        read_count = 0;
383
 
384
 
385
        while (count > 0) {
386
                if (signal_pending(current)) {
387
                        up(&(rio->lock));
388
                        return read_count ? read_count : -EINTR;
389
                }
390
                if (!rio->rio_dev) {
391
                        up(&(rio->lock));
392
                        return -ENODEV;
393
                }
394
                this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
395
 
396
                result = usb_bulk_msg(rio->rio_dev,
397
                                      usb_rcvbulkpipe(rio->rio_dev, 1),
398
                                      ibuf, this_read, &partial,
399
                                      (int) (HZ * 8));
400
 
401
                dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
402
                       result, this_read, partial);
403
 
404
                if (partial) {
405
                        count = this_read = partial;
406
                } else if (result == USB_ST_TIMEOUT || result == 15) {  /* FIXME: 15 ??? */
407
                        if (!maxretry--) {
408
                                up(&(rio->lock));
409
                                err("read_rio: maxretry timeout");
410
                                return -ETIME;
411
                        }
412
                        interruptible_sleep_on_timeout(&rio->wait_q,
413
                                                       NAK_TIMEOUT);
414
                        continue;
415
                } else if (result != USB_ST_DATAUNDERRUN) {
416
                        up(&(rio->lock));
417
                        err("Read Whoops - result:%u partial:%u this_read:%u",
418
                             result, partial, this_read);
419
                        return -EIO;
420
                } else {
421
                        up(&(rio->lock));
422
                        return (0);
423
                }
424
 
425
                if (this_read) {
426
                        if (copy_to_user(buffer, ibuf, this_read)) {
427
                                up(&(rio->lock));
428
                                return -EFAULT;
429
                        }
430
                        count -= this_read;
431
                        read_count += this_read;
432
                        buffer += this_read;
433
                }
434
        }
435
        up(&(rio->lock));
436
        return read_count;
437
}
438
 
439
static struct
440
file_operations usb_rio_fops = {
441
        read:           read_rio,
442
        write:          write_rio,
443
        ioctl:          ioctl_rio,
444
        open:           open_rio,
445
        release:        close_rio,
446
};
447
 
448
static void *probe_rio(struct usb_device *dev, unsigned int ifnum,
449
                       const struct usb_device_id *id)
450
{
451
        struct rio_usb_data *rio = &rio_instance;
452
 
453
        info("USB Rio found at address %d", dev->devnum);
454
 
455
        rio->present = 1;
456
        rio->rio_dev = dev;
457
 
458
        if (!(rio->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
459
                err("probe_rio: Not enough memory for the output buffer");
460
                return NULL;
461
        }
462
        dbg("probe_rio: obuf address:%p", rio->obuf);
463
 
464
        if (!(rio->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
465
                err("probe_rio: Not enough memory for the input buffer");
466
                kfree(rio->obuf);
467
                return NULL;
468
        }
469
        dbg("probe_rio: ibuf address:%p", rio->ibuf);
470
 
471
        rio->devfs = devfs_register(usb_devfs_handle, "rio500",
472
                                    DEVFS_FL_DEFAULT, USB_MAJOR,
473
                                    RIO_MINOR,
474
                                    S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP |
475
                                    S_IWGRP, &usb_rio_fops, NULL);
476
        if (rio->devfs == NULL)
477
                dbg("probe_rio: device node registration failed");
478
 
479
        init_MUTEX(&(rio->lock));
480
 
481
        return rio;
482
}
483
 
484
static void disconnect_rio(struct usb_device *dev, void *ptr)
485
{
486
        struct rio_usb_data *rio = (struct rio_usb_data *) ptr;
487
 
488
        devfs_unregister(rio->devfs);
489
 
490
        down(&(rio->lock));
491
        if (rio->isopen) {
492
                rio->isopen = 0;
493
                /* better let it finish - the release will do whats needed */
494
                rio->rio_dev = NULL;
495
                up(&(rio->lock));
496
                return;
497
        }
498
        kfree(rio->ibuf);
499
        kfree(rio->obuf);
500
 
501
        info("USB Rio disconnected.");
502
 
503
        rio->present = 0;
504
        up(&(rio->lock));
505
}
506
 
507
static struct usb_device_id rio_table [] = {
508
        { USB_DEVICE(0x0841, 1) },              /* Rio 500 */
509
        { }                                     /* Terminating entry */
510
};
511
 
512
MODULE_DEVICE_TABLE (usb, rio_table);
513
 
514
static struct usb_driver rio_driver = {
515
        name:           "rio500",
516
        probe:          probe_rio,
517
        disconnect:     disconnect_rio,
518
        fops:           &usb_rio_fops,
519
        minor:          RIO_MINOR,
520
        id_table:       rio_table,
521
};
522
 
523
int usb_rio_init(void)
524
{
525
        if (usb_register(&rio_driver) < 0)
526
                return -1;
527
 
528
        info(DRIVER_VERSION ":" DRIVER_DESC);
529
 
530
        return 0;
531
}
532
 
533
 
534
void usb_rio_cleanup(void)
535
{
536
        struct rio_usb_data *rio = &rio_instance;
537
 
538
        rio->present = 0;
539
        usb_deregister(&rio_driver);
540
 
541
 
542
}
543
 
544
module_init(usb_rio_init);
545
module_exit(usb_rio_cleanup);
546
 
547
MODULE_AUTHOR( DRIVER_AUTHOR );
548
MODULE_DESCRIPTION( DRIVER_DESC );
549
MODULE_LICENSE("GPL");
550
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.