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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*****************************************************************************/
2
 
3
/*
4
 *      devio.c  --  User space communication with USB devices.
5
 *
6
 *      Copyright (C) 1999-2000  Thomas Sailer (sailer@ife.ee.ethz.ch)
7
 *
8
 *      This program is free software; you can redistribute it and/or modify
9
 *      it under the terms of the GNU General Public License as published by
10
 *      the Free Software Foundation; either version 2 of the License, or
11
 *      (at your option) any later version.
12
 *
13
 *      This program is distributed in the hope that it will be useful,
14
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *      GNU General Public License for more details.
17
 *
18
 *      You should have received a copy of the GNU General Public License
19
 *      along with this program; if not, write to the Free Software
20
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
 *
22
 *  $Id: devio.c,v 1.1.1.1 2004-04-15 01:52:37 phoenix Exp $
23
 *
24
 *  This file implements the usbdevfs/x/y files, where
25
 *  x is the bus number and y the device number.
26
 *
27
 *  It allows user space programs/"drivers" to communicate directly
28
 *  with USB devices without intervening kernel driver.
29
 *
30
 *  Revision history
31
 *    22.12.1999   0.1   Initial release (split from proc_usb.c)
32
 *    04.01.2000   0.2   Turned into its own filesystem
33
 */
34
 
35
/*****************************************************************************/
36
 
37
#include <linux/fs.h>
38
#include <linux/mm.h>
39
#include <linux/slab.h>
40
#include <linux/smp_lock.h>
41
#include <linux/signal.h>
42
#include <linux/poll.h>
43
#include <linux/usb.h>
44
#include <linux/usbdevice_fs.h>
45
#include <asm/uaccess.h>
46
 
47
 
48
struct async {
49
        struct list_head asynclist;
50
        struct dev_state *ps;
51
        struct task_struct *task;
52
        unsigned int signr;
53
        unsigned int intf;
54
        void *userbuffer;
55
        void *userurb;
56
        struct urb urb;
57
};
58
 
59
static loff_t usbdev_lseek(struct file *file, loff_t offset, int orig)
60
{
61
        switch (orig) {
62
        case 0:
63
                file->f_pos = offset;
64
                return file->f_pos;
65
 
66
        case 1:
67
                file->f_pos += offset;
68
                return file->f_pos;
69
 
70
        case 2:
71
                return -EINVAL;
72
 
73
        default:
74
                return -EINVAL;
75
        }
76
}
77
 
78
static ssize_t usbdev_read(struct file *file, char * buf, size_t nbytes, loff_t *ppos)
79
{
80
        struct dev_state *ps = (struct dev_state *)file->private_data;
81
        ssize_t ret = 0;
82
        unsigned len;
83
        loff_t pos;
84
        int i;
85
 
86
        pos = *ppos;
87
        down_read(&ps->devsem);
88
        if (!ps->dev) {
89
                ret = -ENODEV;
90
                goto err;
91
        } else if (pos < 0) {
92
                ret = -EINVAL;
93
                goto err;
94
        }
95
 
96
        if (pos < sizeof(struct usb_device_descriptor)) {
97
                len = sizeof(struct usb_device_descriptor) - pos;
98
                if (len > nbytes)
99
                        len = nbytes;
100
                if (copy_to_user(buf, ((char *)&ps->dev->descriptor) + pos, len)) {
101
                        ret = -EFAULT;
102
                        goto err;
103
                }
104
 
105
                *ppos += len;
106
                buf += len;
107
                nbytes -= len;
108
                ret += len;
109
        }
110
 
111
        pos = sizeof(struct usb_device_descriptor);
112
        for (i = 0; nbytes && i < ps->dev->descriptor.bNumConfigurations; i++) {
113
                struct usb_config_descriptor *config =
114
                        (struct usb_config_descriptor *)ps->dev->rawdescriptors[i];
115
                unsigned int length = le16_to_cpu(config->wTotalLength);
116
 
117
                if (*ppos < pos + length) {
118
                        len = length - (*ppos - pos);
119
                        if (len > nbytes)
120
                                len = nbytes;
121
 
122
                        if (copy_to_user(buf,
123
                            ps->dev->rawdescriptors[i] + (*ppos - pos), len)) {
124
                                ret = -EFAULT;
125
                                goto err;
126
                        }
127
 
128
                        *ppos += len;
129
                        buf += len;
130
                        nbytes -= len;
131
                        ret += len;
132
                }
133
 
134
                pos += length;
135
        }
136
 
137
err:
138
        up_read(&ps->devsem);
139
        return ret;
140
}
141
 
142
static inline unsigned int ld2(unsigned int x)
143
{
144
        unsigned int r = 0;
145
 
146
        if (x >= 0x10000) {
147
                x >>= 16;
148
                r += 16;
149
        }
150
        if (x >= 0x100) {
151
                x >>= 8;
152
                r += 8;
153
        }
154
        if (x >= 0x10) {
155
                x >>= 4;
156
                r += 4;
157
        }
158
        if (x >= 4) {
159
                x >>= 2;
160
                r += 2;
161
        }
162
        if (x >= 2)
163
                r++;
164
        return r;
165
}
166
 
167
/*
168
 * async list handling
169
 */
170
 
171
static struct async *alloc_async(unsigned int numisoframes)
172
{
173
        unsigned int assize = sizeof(struct async) + numisoframes * sizeof(struct iso_packet_descriptor);
174
        struct async *as = kmalloc(assize, GFP_KERNEL);
175
        if (!as)
176
                return NULL;
177
        memset(as, 0, assize);
178
        as->urb.number_of_packets = numisoframes;
179
        spin_lock_init(&as->urb.lock);
180
        return as;
181
}
182
 
183
static void free_async(struct async *as)
184
{
185
        if (as->urb.transfer_buffer)
186
                kfree(as->urb.transfer_buffer);
187
        if (as->urb.setup_packet)
188
                kfree(as->urb.setup_packet);
189
        kfree(as);
190
}
191
 
192
static inline void async_newpending(struct async *as)
193
{
194
        struct dev_state *ps = as->ps;
195
        unsigned long flags;
196
 
197
        spin_lock_irqsave(&ps->lock, flags);
198
        list_add_tail(&as->asynclist, &ps->async_pending);
199
        spin_unlock_irqrestore(&ps->lock, flags);
200
}
201
 
202
static inline void async_removepending(struct async *as)
203
{
204
        struct dev_state *ps = as->ps;
205
        unsigned long flags;
206
 
207
        spin_lock_irqsave(&ps->lock, flags);
208
        list_del_init(&as->asynclist);
209
        spin_unlock_irqrestore(&ps->lock, flags);
210
}
211
 
212
static inline struct async *async_getcompleted(struct dev_state *ps)
213
{
214
        unsigned long flags;
215
        struct async *as = NULL;
216
 
217
        spin_lock_irqsave(&ps->lock, flags);
218
        if (!list_empty(&ps->async_completed)) {
219
                as = list_entry(ps->async_completed.next, struct async, asynclist);
220
                list_del_init(&as->asynclist);
221
        }
222
        spin_unlock_irqrestore(&ps->lock, flags);
223
        return as;
224
}
225
 
226
static inline struct async *async_getpending(struct dev_state *ps, void *userurb)
227
{
228
        unsigned long flags;
229
        struct async *as;
230
 
231
        spin_lock_irqsave(&ps->lock, flags);
232
        list_for_each_entry(as, &ps->async_pending, asynclist)
233
                if (as->userurb == userurb) {
234
                        list_del_init(&as->asynclist);
235
                        spin_unlock_irqrestore(&ps->lock, flags);
236
                        return as;
237
                }
238
        spin_unlock_irqrestore(&ps->lock, flags);
239
        return NULL;
240
}
241
 
242
static void async_completed(struct urb *urb)
243
{
244
        struct async *as = (struct async *)urb->context;
245
        struct dev_state *ps = as->ps;
246
        struct siginfo sinfo;
247
 
248
        spin_lock(&ps->lock);
249
        list_move_tail(&as->asynclist, &ps->async_completed);
250
        spin_unlock(&ps->lock);
251
        wake_up(&ps->wait);
252
        if (as->signr) {
253
                sinfo.si_signo = as->signr;
254
                sinfo.si_errno = as->urb.status;
255
                sinfo.si_code = SI_ASYNCIO;
256
                sinfo.si_addr = as->userurb;
257
                send_sig_info(as->signr, &sinfo, as->task);
258
        }
259
}
260
 
261
static void destroy_async (struct dev_state *ps, struct list_head *list)
262
{
263
        struct async *as;
264
        unsigned long flags;
265
 
266
        spin_lock_irqsave(&ps->lock, flags);
267
        while (!list_empty(list)) {
268
                as = list_entry(list->next, struct async, asynclist);
269
                list_del_init(&as->asynclist);
270
                spin_unlock_irqrestore(&ps->lock, flags);
271
                /* usb_unlink_urb calls the completion handler with status == USB_ST_URB_KILLED */
272
                usb_unlink_urb(&as->urb);
273
                spin_lock_irqsave(&ps->lock, flags);
274
        }
275
        spin_unlock_irqrestore(&ps->lock, flags);
276
        while ((as = async_getcompleted(ps)))
277
                free_async(as);
278
}
279
 
280
static void destroy_async_on_interface (struct dev_state *ps, unsigned int intf)
281
{
282
        struct list_head *p, *q, hitlist;
283
        unsigned long flags;
284
 
285
        INIT_LIST_HEAD(&hitlist);
286
        spin_lock_irqsave(&ps->lock, flags);
287
        list_for_each_safe(p, q, &ps->async_pending)
288
                if (intf == list_entry(p, struct async, asynclist)->intf)
289
                        list_move_tail(p, &hitlist);
290
        spin_unlock_irqrestore(&ps->lock, flags);
291
        destroy_async(ps, &hitlist);
292
}
293
 
294
static inline void destroy_all_async(struct dev_state *ps)
295
{
296
        destroy_async(ps, &ps->async_pending);
297
}
298
 
299
/*
300
 * interface claims are made only at the request of user level code,
301
 * which can also release them (explicitly or by closing files).
302
 * they're also undone when devices disconnect.
303
 */
304
 
305
static void *driver_probe(struct usb_device *dev, unsigned int intf,
306
                          const struct usb_device_id *id)
307
{
308
        return NULL;
309
}
310
 
311
static void driver_disconnect(struct usb_device *dev, void *context)
312
{
313
        struct dev_state *ps = (struct dev_state *)context;
314
 
315
        if (!ps)
316
                return;
317
 
318
        /* this waits till synchronous requests complete */
319
        down_write (&ps->devsem);
320
 
321
        /* prevent new I/O requests */
322
        ps->dev = 0;
323
        ps->ifclaimed = 0;
324
 
325
        /* force async requests to complete */
326
        destroy_all_async (ps);
327
 
328
        up_write (&ps->devsem);
329
}
330
 
331
struct usb_driver usbdevfs_driver = {
332
        name:           "usbdevfs",
333
        probe:          driver_probe,
334
        disconnect:     driver_disconnect,
335
};
336
 
337
static int claimintf(struct dev_state *ps, unsigned int intf)
338
{
339
        struct usb_device *dev = ps->dev;
340
        struct usb_interface *iface;
341
        int err;
342
 
343
        if (intf >= 8*sizeof(ps->ifclaimed) || !dev || intf >= dev->actconfig->bNumInterfaces)
344
                return -EINVAL;
345
        /* already claimed */
346
        if (test_bit(intf, &ps->ifclaimed))
347
                return 0;
348
        iface = &dev->actconfig->interface[intf];
349
        err = -EBUSY;
350
        lock_kernel();
351
        if (!usb_interface_claimed(iface)) {
352
                usb_driver_claim_interface(&usbdevfs_driver, iface, ps);
353
                set_bit(intf, &ps->ifclaimed);
354
                err = 0;
355
        }
356
        unlock_kernel();
357
        return err;
358
}
359
 
360
static int releaseintf(struct dev_state *ps, unsigned int intf)
361
{
362
        struct usb_device *dev;
363
        struct usb_interface *iface;
364
        int err;
365
 
366
        if (intf >= 8*sizeof(ps->ifclaimed))
367
                return -EINVAL;
368
        err = -EINVAL;
369
        lock_kernel();
370
        dev = ps->dev;
371
        if (dev && test_and_clear_bit(intf, &ps->ifclaimed)) {
372
                iface = &dev->actconfig->interface[intf];
373
                usb_driver_release_interface(&usbdevfs_driver, iface);
374
                err = 0;
375
        }
376
        unlock_kernel();
377
        return err;
378
}
379
 
380
static int checkintf(struct dev_state *ps, unsigned int intf)
381
{
382
        if (intf >= 8*sizeof(ps->ifclaimed))
383
                return -EINVAL;
384
        if (test_bit(intf, &ps->ifclaimed))
385
                return 0;
386
        /* if not yet claimed, claim it for the driver */
387
        printk(KERN_WARNING "usbdevfs: process %d (%s) did not claim interface %u before use\n",
388
               current->pid, current->comm, intf);
389
        return claimintf(ps, intf);
390
}
391
 
392
static int findintfep(struct usb_device *dev, unsigned int ep)
393
{
394
        unsigned int i, j, e;
395
        struct usb_interface *iface;
396
        struct usb_interface_descriptor *alts;
397
        struct usb_endpoint_descriptor *endpt;
398
 
399
        if (ep & ~(USB_DIR_IN|0xf))
400
                return -EINVAL;
401
        for (i = 0; i < dev->actconfig->bNumInterfaces; i++) {
402
                iface = &dev->actconfig->interface[i];
403
                for (j = 0; j < iface->num_altsetting; j++) {
404
                        alts = &iface->altsetting[j];
405
                        for (e = 0; e < alts->bNumEndpoints; e++) {
406
                                endpt = &alts->endpoint[e];
407
                                if (endpt->bEndpointAddress == ep)
408
                                        return i;
409
                        }
410
                }
411
        }
412
        return -ENOENT;
413
}
414
 
415
static int findintfif(struct usb_device *dev, unsigned int ifn)
416
{
417
        unsigned int i, j;
418
        struct usb_interface *iface;
419
        struct usb_interface_descriptor *alts;
420
 
421
        if (ifn & ~0xff)
422
                return -EINVAL;
423
        for (i = 0; i < dev->actconfig->bNumInterfaces; i++) {
424
                iface = &dev->actconfig->interface[i];
425
                for (j = 0; j < iface->num_altsetting; j++) {
426
                        alts = &iface->altsetting[j];
427
                        if (alts->bInterfaceNumber == ifn)
428
                                return i;
429
                }
430
        }
431
        return -ENOENT;
432
}
433
 
434
extern struct list_head usb_driver_list;
435
 
436
#if 0
437
static int finddriver(struct usb_driver **driver, char *name)
438
{
439
        struct list_head *tmp;
440
 
441
        tmp = usb_driver_list.next;
442
        while (tmp != &usb_driver_list) {
443
                struct usb_driver *d = list_entry(tmp, struct usb_driver,
444
                                                        driver_list);
445
 
446
                if (!strcmp(d->name, name)) {
447
                        *driver = d;
448
                        return 0;
449
                }
450
 
451
                tmp = tmp->next;
452
        }
453
 
454
        return -EINVAL;
455
}
456
#endif
457
 
458
static int check_ctrlrecip(struct dev_state *ps, unsigned int requesttype, unsigned int index)
459
{
460
        int ret;
461
 
462
        if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
463
                return 0;
464
 
465
        switch (requesttype & USB_RECIP_MASK) {
466
        case USB_RECIP_ENDPOINT:
467
                if ((ret = findintfep(ps->dev, index & 0xff)) < 0)
468
                        return ret;
469
                if ((ret = checkintf(ps, ret)))
470
                        return ret;
471
                break;
472
 
473
        case USB_RECIP_INTERFACE:
474
                if ((ret = findintfif(ps->dev, index & 0xff)) < 0)
475
                        return ret;
476
                if ((ret = checkintf(ps, ret)))
477
                        return ret;
478
                break;
479
        }
480
        return 0;
481
}
482
 
483
/*
484
 * file operations
485
 */
486
static int usbdev_open(struct inode *inode, struct file *file)
487
{
488
        struct usb_device *dev;
489
        struct dev_state *ps;
490
        int ret;
491
 
492
        /*
493
         * no locking necessary here, as both sys_open (actually filp_open)
494
         * and the hub thread have the kernel lock
495
         * (still acquire the kernel lock for safety)
496
         */
497
        lock_kernel();
498
        ret = -ENOENT;
499
        if (ITYPE(inode->i_ino) != IDEVICE)
500
                goto out;
501
        dev = inode->u.usbdev_i.p.dev;
502
        if (!dev)
503
                goto out;
504
        ret = -ENOMEM;
505
        if (!(ps = kmalloc(sizeof(struct dev_state), GFP_KERNEL)))
506
                goto out;
507
        ret = 0;
508
        ps->dev = dev;
509
        ps->file = file;
510
        spin_lock_init(&ps->lock);
511
        INIT_LIST_HEAD(&ps->async_pending);
512
        INIT_LIST_HEAD(&ps->async_completed);
513
        init_waitqueue_head(&ps->wait);
514
        init_rwsem(&ps->devsem);
515
        ps->discsignr = 0;
516
        ps->disctask = current;
517
        ps->disccontext = NULL;
518
        ps->ifclaimed = 0;
519
        wmb();
520
        list_add_tail(&ps->list, &dev->filelist);
521
        file->private_data = ps;
522
 out:
523
        unlock_kernel();
524
        return ret;
525
}
526
 
527
static int usbdev_release(struct inode *inode, struct file *file)
528
{
529
        struct dev_state *ps = (struct dev_state *)file->private_data;
530
        unsigned int i;
531
 
532
        lock_kernel();
533
        list_del_init(&ps->list);
534
        if (ps->dev) {
535
                for (i = 0; ps->ifclaimed && i < 8*sizeof(ps->ifclaimed); i++)
536
                        if (test_bit(i, &ps->ifclaimed))
537
                                releaseintf(ps, i);
538
        }
539
        unlock_kernel();
540
        destroy_all_async(ps);
541
        kfree(ps);
542
        return 0;
543
}
544
 
545
static int proc_control(struct dev_state *ps, void *arg)
546
{
547
        struct usb_device *dev = ps->dev;
548
        struct usbdevfs_ctrltransfer ctrl;
549
        unsigned int tmo;
550
        unsigned char *tbuf;
551
        int i, ret;
552
 
553
        if (copy_from_user(&ctrl, (void *)arg, sizeof(ctrl)))
554
                return -EFAULT;
555
        if ((ret = check_ctrlrecip(ps, ctrl.requesttype, ctrl.index)))
556
                return ret;
557
        if (ctrl.length > PAGE_SIZE)
558
                return -EINVAL;
559
        if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
560
                return -ENOMEM;
561
        tmo = (ctrl.timeout * HZ + 999) / 1000;
562
        if (ctrl.requesttype & 0x80) {
563
                if (ctrl.length && !access_ok(VERIFY_WRITE, ctrl.data, ctrl.length)) {
564
                        free_page((unsigned long)tbuf);
565
                        return -EINVAL;
566
                }
567
                i = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), ctrl.request, ctrl.requesttype,
568
                                       ctrl.value, ctrl.index, tbuf, ctrl.length, tmo);
569
                if ((i > 0) && ctrl.length) {
570
                        if (copy_to_user(ctrl.data, tbuf, ctrl.length)) {
571
                                free_page((unsigned long)tbuf);
572
                                return -EFAULT;
573
                        }
574
                }
575
        } else {
576
                if (ctrl.length) {
577
                        if (copy_from_user(tbuf, ctrl.data, ctrl.length)) {
578
                                free_page((unsigned long)tbuf);
579
                                return -EFAULT;
580
                        }
581
                }
582
                i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.request, ctrl.requesttype,
583
                                       ctrl.value, ctrl.index, tbuf, ctrl.length, tmo);
584
        }
585
        free_page((unsigned long)tbuf);
586
        if (i<0) {
587
                printk(KERN_DEBUG "usbdevfs: USBDEVFS_CONTROL failed dev %d rqt %u rq %u len %u ret %d\n",
588
                       dev->devnum, ctrl.requesttype, ctrl.request, ctrl.length, i);
589
        }
590
        return i;
591
}
592
 
593
static int proc_bulk(struct dev_state *ps, void *arg)
594
{
595
        struct usb_device *dev = ps->dev;
596
        struct usbdevfs_bulktransfer bulk;
597
        unsigned int tmo, len1, pipe;
598
        int len2;
599
        unsigned char *tbuf;
600
        int i, ret;
601
 
602
        if (copy_from_user(&bulk, (void *)arg, sizeof(bulk)))
603
                return -EFAULT;
604
        if ((ret = findintfep(ps->dev, bulk.ep)) < 0)
605
                return ret;
606
        if ((ret = checkintf(ps, ret)))
607
                return ret;
608
        if (bulk.ep & USB_DIR_IN)
609
                pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
610
        else
611
                pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
612
        if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
613
                return -EINVAL;
614
        len1 = bulk.len;
615
        if (len1 > PAGE_SIZE)
616
                return -EINVAL;
617
        if (!(tbuf = (unsigned char *)__get_free_page(GFP_KERNEL)))
618
                return -ENOMEM;
619
        tmo = (bulk.timeout * HZ + 999) / 1000;
620
        if (bulk.ep & 0x80) {
621
                if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
622
                        free_page((unsigned long)tbuf);
623
                        return -EINVAL;
624
                }
625
                i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
626
                if (!i && len2) {
627
                        if (copy_to_user(bulk.data, tbuf, len2)) {
628
                                free_page((unsigned long)tbuf);
629
                                return -EFAULT;
630
                        }
631
                }
632
        } else {
633
                if (len1) {
634
                        if (copy_from_user(tbuf, bulk.data, len1)) {
635
                                free_page((unsigned long)tbuf);
636
                                return -EFAULT;
637
                        }
638
                }
639
                i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
640
        }
641
        free_page((unsigned long)tbuf);
642
        if (i < 0) {
643
                printk(KERN_WARNING "usbdevfs: USBDEVFS_BULK failed dev %d ep 0x%x len %u ret %d\n",
644
                       dev->devnum, bulk.ep, bulk.len, i);
645
                return i;
646
        }
647
        return len2;
648
}
649
 
650
static int proc_resetep(struct dev_state *ps, void *arg)
651
{
652
        unsigned int ep;
653
        int ret;
654
 
655
        if (get_user(ep, (unsigned int *)arg))
656
                return -EFAULT;
657
        if ((ret = findintfep(ps->dev, ep)) < 0)
658
                return ret;
659
        if ((ret = checkintf(ps, ret)))
660
                return ret;
661
        usb_settoggle(ps->dev, ep & 0xf, !(ep & USB_DIR_IN), 0);
662
        return 0;
663
}
664
 
665
static int proc_clearhalt(struct dev_state *ps, void *arg)
666
{
667
        unsigned int ep;
668
        int pipe;
669
        int ret;
670
 
671
        if (get_user(ep, (unsigned int *)arg))
672
                return -EFAULT;
673
        if ((ret = findintfep(ps->dev, ep)) < 0)
674
                return ret;
675
        if ((ret = checkintf(ps, ret)))
676
                return ret;
677
        if (ep & USB_DIR_IN)
678
                pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
679
        else
680
                pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
681
 
682
        return usb_clear_halt(ps->dev, pipe);
683
}
684
 
685
 
686
static int proc_getdriver(struct dev_state *ps, void *arg)
687
{
688
        struct usbdevfs_getdriver gd;
689
        struct usb_interface *interface;
690
        int ret;
691
 
692
        if (copy_from_user(&gd, arg, sizeof(gd)))
693
                return -EFAULT;
694
        if ((ret = findintfif(ps->dev, gd.interface)) < 0)
695
                return ret;
696
        interface = usb_ifnum_to_if(ps->dev, gd.interface);
697
        if (!interface)
698
                return -EINVAL;
699
        if (!interface->driver)
700
                return -ENODATA;
701
        strcpy(gd.driver, interface->driver->name);
702
        if (copy_to_user(arg, &gd, sizeof(gd)))
703
                return -EFAULT;
704
        return 0;
705
}
706
 
707
static int proc_connectinfo(struct dev_state *ps, void *arg)
708
{
709
        struct usbdevfs_connectinfo ci;
710
 
711
        ci.devnum = ps->dev->devnum;
712
        ci.slow = ps->dev->speed == USB_SPEED_LOW;
713
        if (copy_to_user(arg, &ci, sizeof(ci)))
714
                return -EFAULT;
715
        return 0;
716
}
717
 
718
static int proc_resetdevice(struct dev_state *ps)
719
{
720
        int i, ret;
721
 
722
        ret = usb_reset_device(ps->dev);
723
        if (ret < 0)
724
                return ret;
725
 
726
        for (i = 0; i < ps->dev->actconfig->bNumInterfaces; i++) {
727
                struct usb_interface *intf = &ps->dev->actconfig->interface[i];
728
 
729
                /* Don't simulate interfaces we've claimed */
730
                if (test_bit(i, &ps->ifclaimed))
731
                        continue;
732
 
733
                if (intf->driver) {
734
                        const struct usb_device_id *id;
735
                        down(&intf->driver->serialize);
736
                        intf->driver->disconnect(ps->dev, intf->private_data);
737
                        id = usb_match_id(ps->dev,intf,intf->driver->id_table);
738
                        intf->driver->probe(ps->dev, i, id);
739
                        up(&intf->driver->serialize);
740
                }
741
        }
742
 
743
        return 0;
744
}
745
 
746
static int proc_setintf(struct dev_state *ps, void *arg)
747
{
748
        struct usbdevfs_setinterface setintf;
749
        struct usb_interface *interface;
750
        int ret;
751
 
752
        if (copy_from_user(&setintf, arg, sizeof(setintf)))
753
                return -EFAULT;
754
        if ((ret = findintfif(ps->dev, setintf.interface)) < 0)
755
                return ret;
756
        interface = usb_ifnum_to_if(ps->dev, setintf.interface);
757
        if (!interface)
758
                return -EINVAL;
759
        if (interface->driver) {
760
                if ((ret = checkintf(ps, ret)))
761
                        return ret;
762
        }
763
        if (usb_set_interface(ps->dev, setintf.interface, setintf.altsetting))
764
                return -EINVAL;
765
        return 0;
766
}
767
 
768
static int proc_setconfig(struct dev_state *ps, void *arg)
769
{
770
        unsigned int u;
771
 
772
        if (get_user(u, (unsigned int *)arg))
773
                return -EFAULT;
774
        if (usb_set_configuration(ps->dev, u) < 0)
775
                return -EINVAL;
776
        return 0;
777
}
778
 
779
static int proc_submiturb(struct dev_state *ps, void *arg)
780
{
781
        struct usbdevfs_urb uurb;
782
        struct usbdevfs_iso_packet_desc *isopkt = NULL;
783
        struct usb_endpoint_descriptor *ep_desc;
784
        struct async *as;
785
        struct usb_ctrlrequest *dr = NULL;
786
        unsigned int u, totlen, isofrmlen;
787
        int ret;
788
        int intf = -1;
789
 
790
        if (copy_from_user(&uurb, arg, sizeof(uurb)))
791
                return -EFAULT;
792
        if (uurb.flags & ~(USBDEVFS_URB_ISO_ASAP|USBDEVFS_URB_DISABLE_SPD|USBDEVFS_URB_QUEUE_BULK|
793
                           USB_NO_FSBR|USB_ZERO_PACKET))
794
                return -EINVAL;
795
        if (!uurb.buffer)
796
                return -EINVAL;
797
        if (uurb.signr != 0 && (uurb.signr < SIGRTMIN || uurb.signr > SIGRTMAX))
798
                return -EINVAL;
799
        if (!(uurb.type == USBDEVFS_URB_TYPE_CONTROL && (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
800
                if ((intf = findintfep(ps->dev, uurb.endpoint)) < 0)
801
                        return intf;
802
                if ((ret = checkintf(ps, intf)))
803
                        return ret;
804
        }
805
        switch(uurb.type) {
806
        case USBDEVFS_URB_TYPE_CONTROL:
807
                if ((uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) != 0) {
808
                        if (!(ep_desc = usb_epnum_to_ep_desc(ps->dev, uurb.endpoint)))
809
                                return -ENOENT;
810
                        if ((ep_desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_CONTROL)
811
                                return -EINVAL;
812
                }
813
                /* min 8 byte setup packet, max arbitrary */
814
                if (uurb.buffer_length < 8 || uurb.buffer_length > PAGE_SIZE)
815
                        return -EINVAL;
816
                if (!(dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
817
                        return -ENOMEM;
818
                if (copy_from_user(dr, (unsigned char*)uurb.buffer, 8)) {
819
                        kfree(dr);
820
                        return -EFAULT;
821
                }
822
                if (uurb.buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
823
                        kfree(dr);
824
                        return -EINVAL;
825
                }
826
                if ((ret = check_ctrlrecip(ps, dr->bRequestType, le16_to_cpup(&dr->wIndex)))) {
827
                        kfree(dr);
828
                        return ret;
829
                }
830
                uurb.endpoint = (uurb.endpoint & ~USB_ENDPOINT_DIR_MASK) | (dr->bRequestType & USB_ENDPOINT_DIR_MASK);
831
                uurb.number_of_packets = 0;
832
                uurb.buffer_length = le16_to_cpup(&dr->wLength);
833
                uurb.buffer += 8;
834
                if (!access_ok((uurb.endpoint & USB_DIR_IN) ?  VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length)) {
835
                        kfree(dr);
836
                        return -EFAULT;
837
                }
838
                break;
839
 
840
        case USBDEVFS_URB_TYPE_BULK:
841
                uurb.number_of_packets = 0;
842
                if (uurb.buffer_length > 16384)
843
                        return -EINVAL;
844
                if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
845
                        return -EFAULT;
846
                break;
847
 
848
        case USBDEVFS_URB_TYPE_ISO:
849
                /* arbitrary limit */
850
                if (uurb.number_of_packets < 1 || uurb.number_of_packets > 128)
851
                        return -EINVAL;
852
                isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) * uurb.number_of_packets;
853
                if (!(isopkt = kmalloc(isofrmlen, GFP_KERNEL)))
854
                        return -ENOMEM;
855
                if (copy_from_user(isopkt, &((struct usbdevfs_urb *)arg)->iso_frame_desc, isofrmlen)) {
856
                        kfree(isopkt);
857
                        return -EFAULT;
858
                }
859
                for (totlen = u = 0; u < uurb.number_of_packets; u++) {
860
                        if (isopkt[u].length > 1023) {
861
                                kfree(isopkt);
862
                                return -EINVAL;
863
                        }
864
                        totlen += isopkt[u].length;
865
                }
866
                if (totlen > 32768) {
867
                        kfree(isopkt);
868
                        return -EINVAL;
869
                }
870
                uurb.buffer_length = totlen;
871
                break;
872
 
873
        case USBDEVFS_URB_TYPE_INTERRUPT:
874
                uurb.number_of_packets = 0;
875
                if (uurb.buffer_length > 16384)
876
                        return -EINVAL;
877
                if (!access_ok((uurb.endpoint & USB_DIR_IN) ? VERIFY_WRITE : VERIFY_READ, uurb.buffer, uurb.buffer_length))
878
                        return -EFAULT;
879
                break;
880
 
881
        default:
882
                return -EINVAL;
883
        }
884
        if (!(as = alloc_async(uurb.number_of_packets))) {
885
                if (isopkt)
886
                        kfree(isopkt);
887
                if (dr)
888
                        kfree(dr);
889
                return -ENOMEM;
890
        }
891
        if (!(as->urb.transfer_buffer = kmalloc(uurb.buffer_length, GFP_KERNEL))) {
892
                if (isopkt)
893
                        kfree(isopkt);
894
                if (dr)
895
                        kfree(dr);
896
                free_async(as);
897
                return -ENOMEM;
898
        }
899
        as->urb.next = NULL;
900
        as->urb.dev = ps->dev;
901
        as->urb.pipe = (uurb.type << 30) | __create_pipe(ps->dev, uurb.endpoint & 0xf) | (uurb.endpoint & USB_DIR_IN);
902
        as->urb.transfer_flags = uurb.flags;
903
        as->urb.transfer_buffer_length = uurb.buffer_length;
904
        as->urb.setup_packet = (unsigned char*)dr;
905
        as->urb.start_frame = uurb.start_frame;
906
        as->urb.number_of_packets = uurb.number_of_packets;
907
        as->urb.context = as;
908
        as->urb.complete = async_completed;
909
        for (totlen = u = 0; u < uurb.number_of_packets; u++) {
910
                as->urb.iso_frame_desc[u].offset = totlen;
911
                as->urb.iso_frame_desc[u].length = isopkt[u].length;
912
                totlen += isopkt[u].length;
913
        }
914
        if (isopkt)
915
                kfree(isopkt);
916
        as->ps = ps;
917
        as->userurb = arg;
918
        if (uurb.endpoint & USB_DIR_IN)
919
                as->userbuffer = uurb.buffer;
920
        else
921
                as->userbuffer = NULL;
922
        as->signr = uurb.signr;
923
        as->intf = intf;
924
        as->task = current;
925
        if (!(uurb.endpoint & USB_DIR_IN)) {
926
                if (copy_from_user(as->urb.transfer_buffer, uurb.buffer, as->urb.transfer_buffer_length)) {
927
                        free_async(as);
928
                        return -EFAULT;
929
                }
930
        }
931
        async_newpending(as);
932
        if ((ret = usb_submit_urb(&as->urb))) {
933
                printk(KERN_DEBUG "usbdevfs: usb_submit_urb returned %d\n", ret);
934
                async_removepending(as);
935
                free_async(as);
936
                return ret;
937
        }
938
        return 0;
939
}
940
 
941
static int proc_unlinkurb(struct dev_state *ps, void *arg)
942
{
943
        struct async *as;
944
 
945
        as = async_getpending(ps, arg);
946
        if (!as)
947
                return -EINVAL;
948
        usb_unlink_urb(&as->urb);
949
        return 0;
950
}
951
 
952
static int processcompl(struct async *as)
953
{
954
        unsigned int i;
955
 
956
        if (as->userbuffer)
957
                if (copy_to_user(as->userbuffer, as->urb.transfer_buffer, as->urb.transfer_buffer_length))
958
                        return -EFAULT;
959
        if (put_user(as->urb.status,
960
                     &((struct usbdevfs_urb *)as->userurb)->status))
961
                return -EFAULT;
962
        if (put_user(as->urb.actual_length,
963
                     &((struct usbdevfs_urb *)as->userurb)->actual_length))
964
                return -EFAULT;
965
        if (put_user(as->urb.error_count,
966
                     &((struct usbdevfs_urb *)as->userurb)->error_count))
967
                return -EFAULT;
968
 
969
        if (!(usb_pipeisoc(as->urb.pipe)))
970
                return 0;
971
        for (i = 0; i < as->urb.number_of_packets; i++) {
972
                if (put_user(as->urb.iso_frame_desc[i].actual_length,
973
                             &((struct usbdevfs_urb *)as->userurb)->iso_frame_desc[i].actual_length))
974
                        return -EFAULT;
975
                if (put_user(as->urb.iso_frame_desc[i].status,
976
                             &((struct usbdevfs_urb *)as->userurb)->iso_frame_desc[i].status))
977
                        return -EFAULT;
978
        }
979
        return 0;
980
}
981
 
982
static int proc_reapurb(struct dev_state *ps, void *arg)
983
{
984
        DECLARE_WAITQUEUE(wait, current);
985
        struct async *as = NULL;
986
        void *addr;
987
        int ret;
988
 
989
        add_wait_queue(&ps->wait, &wait);
990
        while (ps->dev) {
991
                __set_current_state(TASK_INTERRUPTIBLE);
992
                if ((as = async_getcompleted(ps)))
993
                        break;
994
                if (signal_pending(current))
995
                        break;
996
                up_read(&ps->devsem);
997
                schedule();
998
                down_read(&ps->devsem);
999
        }
1000
        remove_wait_queue(&ps->wait, &wait);
1001
        set_current_state(TASK_RUNNING);
1002
        if (as) {
1003
                ret = processcompl(as);
1004
                addr = as->userurb;
1005
                free_async(as);
1006
                if (ret)
1007
                        return ret;
1008
                if (put_user(addr, (void **)arg))
1009
                        return -EFAULT;
1010
                return 0;
1011
        }
1012
        if (signal_pending(current))
1013
                return -EINTR;
1014
        return -EIO;
1015
}
1016
 
1017
static int proc_reapurbnonblock(struct dev_state *ps, void *arg)
1018
{
1019
        struct async *as;
1020
        void *addr;
1021
        int ret;
1022
 
1023
        if (!(as = async_getcompleted(ps)))
1024
                return -EAGAIN;
1025
        ret = processcompl(as);
1026
        addr = as->userurb;
1027
        free_async(as);
1028
        if (ret)
1029
                return ret;
1030
        if (put_user(addr, (void **)arg))
1031
                return -EFAULT;
1032
        return 0;
1033
}
1034
 
1035
static int proc_disconnectsignal(struct dev_state *ps, void *arg)
1036
{
1037
        struct usbdevfs_disconnectsignal ds;
1038
 
1039
        if (copy_from_user(&ds, arg, sizeof(ds)))
1040
                return -EFAULT;
1041
        if (ds.signr != 0 && (ds.signr < SIGRTMIN || ds.signr > SIGRTMAX))
1042
                return -EINVAL;
1043
        ps->discsignr = ds.signr;
1044
        ps->disccontext = ds.context;
1045
        return 0;
1046
}
1047
 
1048
static int proc_claiminterface(struct dev_state *ps, void *arg)
1049
{
1050
        unsigned int intf;
1051
        int ret;
1052
 
1053
        if (get_user(intf, (unsigned int *)arg))
1054
                return -EFAULT;
1055
        if ((ret = findintfif(ps->dev, intf)) < 0)
1056
                return ret;
1057
        return claimintf(ps, ret);
1058
}
1059
 
1060
static int proc_releaseinterface(struct dev_state *ps, void *arg)
1061
{
1062
        unsigned int intf;
1063
        int ret;
1064
 
1065
        if (get_user(intf, (unsigned int *)arg))
1066
                return -EFAULT;
1067
        if ((ret = findintfif(ps->dev, intf)) < 0)
1068
                return ret;
1069
        if ((ret = releaseintf(ps, intf)) < 0)
1070
                return ret;
1071
        destroy_async_on_interface (ps, intf);
1072
        return 0;
1073
}
1074
 
1075
static int proc_ioctl (struct dev_state *ps, void *arg)
1076
{
1077
        struct usbdevfs_ioctl   ctrl;
1078
        int                     size;
1079
        void                    *buf = 0;
1080
        int                     retval = 0;
1081
       struct usb_interface    *ifp = 0;
1082
       struct usb_driver       *driver = 0;
1083
 
1084
        /* get input parameters and alloc buffer */
1085
        if (copy_from_user(&ctrl, (void *) arg, sizeof (ctrl)))
1086
                return -EFAULT;
1087
        if ((size = _IOC_SIZE (ctrl.ioctl_code)) > 0) {
1088
                if ((buf = kmalloc (size, GFP_KERNEL)) == 0)
1089
                        return -ENOMEM;
1090
                if ((_IOC_DIR(ctrl.ioctl_code) & _IOC_WRITE)) {
1091
                        if (copy_from_user (buf, ctrl.data, size)) {
1092
                                kfree (buf);
1093
                                return -EFAULT;
1094
                        }
1095
                } else {
1096
                        memset (buf, 0, size);
1097
                }
1098
        }
1099
 
1100
       if (!ps->dev)
1101
               retval = -ENODEV;
1102
       else if (!(ifp = usb_ifnum_to_if (ps->dev, ctrl.ifno)))
1103
               retval = -EINVAL;
1104
       else switch (ctrl.ioctl_code) {
1105
 
1106
       /* disconnect kernel driver from interface, leaving it unbound.  */
1107
       case USBDEVFS_DISCONNECT:
1108
               driver = ifp->driver;
1109
               if (driver) {
1110
                       down (&driver->serialize);
1111
                       dbg ("disconnect '%s' from dev %d interface %d",
1112
                               driver->name, ps->dev->devnum, ctrl.ifno);
1113
                       driver->disconnect (ps->dev, ifp->private_data);
1114
                       usb_driver_release_interface (driver, ifp);
1115
                       up (&driver->serialize);
1116
               } else
1117
                       retval = -ENODATA;
1118
               break;
1119
 
1120
       /* let kernel drivers try to (re)bind to the interface */
1121
       case USBDEVFS_CONNECT:
1122
               usb_find_interface_driver_for_ifnum (ps->dev, ctrl.ifno);
1123
               break;
1124
 
1125
       /* talk directly to the interface's driver */
1126
       default:
1127
               driver = ifp->driver;
1128
               if (driver == 0 || driver->ioctl == 0)
1129
                       retval = -ENOSYS;
1130
                else {
1131
                        /* ifno might usefully be passed ... */
1132
                       retval = driver->ioctl (ps->dev, ctrl.ioctl_code, buf);
1133
                        /* size = min_t(int, size, retval)? */
1134
               }
1135
        }
1136
 
1137
        /* cleanup and return */
1138
        if (retval >= 0
1139
                        && (_IOC_DIR (ctrl.ioctl_code) & _IOC_READ) != 0
1140
                        && size > 0
1141
                        && copy_to_user (ctrl.data, buf, size) != 0)
1142
                retval = -EFAULT;
1143
        if (buf != 0)
1144
                kfree (buf);
1145
        return retval;
1146
}
1147
 
1148
static int usbdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
1149
{
1150
        struct dev_state *ps = (struct dev_state *)file->private_data;
1151
        int ret = -ENOIOCTLCMD;
1152
 
1153
        if (!(file->f_mode & FMODE_WRITE))
1154
                return -EPERM;
1155
        down_read(&ps->devsem);
1156
        if (!ps->dev) {
1157
                up_read(&ps->devsem);
1158
                return -ENODEV;
1159
        }
1160
        switch (cmd) {
1161
        case USBDEVFS_CONTROL:
1162
                ret = proc_control(ps, (void *)arg);
1163
                if (ret >= 0)
1164
                        inode->i_mtime = CURRENT_TIME;
1165
                break;
1166
 
1167
        case USBDEVFS_BULK:
1168
                ret = proc_bulk(ps, (void *)arg);
1169
                if (ret >= 0)
1170
                        inode->i_mtime = CURRENT_TIME;
1171
                break;
1172
 
1173
        case USBDEVFS_RESETEP:
1174
                ret = proc_resetep(ps, (void *)arg);
1175
                if (ret >= 0)
1176
                        inode->i_mtime = CURRENT_TIME;
1177
                break;
1178
 
1179
        case USBDEVFS_RESET:
1180
                ret = proc_resetdevice(ps);
1181
                break;
1182
 
1183
        case USBDEVFS_CLEAR_HALT:
1184
                ret = proc_clearhalt(ps, (void *)arg);
1185
                if (ret >= 0)
1186
                        inode->i_mtime = CURRENT_TIME;
1187
                break;
1188
 
1189
        case USBDEVFS_GETDRIVER:
1190
                ret = proc_getdriver(ps, (void *)arg);
1191
                break;
1192
 
1193
        case USBDEVFS_CONNECTINFO:
1194
                ret = proc_connectinfo(ps, (void *)arg);
1195
                break;
1196
 
1197
        case USBDEVFS_SETINTERFACE:
1198
                ret = proc_setintf(ps, (void *)arg);
1199
                break;
1200
 
1201
        case USBDEVFS_SETCONFIGURATION:
1202
                ret = proc_setconfig(ps, (void *)arg);
1203
                break;
1204
 
1205
        case USBDEVFS_SUBMITURB:
1206
                ret = proc_submiturb(ps, (void *)arg);
1207
                if (ret >= 0)
1208
                        inode->i_mtime = CURRENT_TIME;
1209
                break;
1210
 
1211
        case USBDEVFS_DISCARDURB:
1212
                ret = proc_unlinkurb(ps, (void *)arg);
1213
                break;
1214
 
1215
        case USBDEVFS_REAPURB:
1216
                ret = proc_reapurb(ps, (void *)arg);
1217
                break;
1218
 
1219
        case USBDEVFS_REAPURBNDELAY:
1220
                ret = proc_reapurbnonblock(ps, (void *)arg);
1221
                break;
1222
 
1223
        case USBDEVFS_DISCSIGNAL:
1224
                ret = proc_disconnectsignal(ps, (void *)arg);
1225
                break;
1226
 
1227
        case USBDEVFS_CLAIMINTERFACE:
1228
                ret = proc_claiminterface(ps, (void *)arg);
1229
                break;
1230
 
1231
        case USBDEVFS_RELEASEINTERFACE:
1232
                ret = proc_releaseinterface(ps, (void *)arg);
1233
                break;
1234
 
1235
        case USBDEVFS_IOCTL:
1236
                ret = proc_ioctl(ps, (void *) arg);
1237
                break;
1238
        }
1239
        up_read(&ps->devsem);
1240
        if (ret >= 0)
1241
                inode->i_atime = CURRENT_TIME;
1242
        return ret;
1243
}
1244
 
1245
/* No kernel lock - fine */
1246
static unsigned int usbdev_poll(struct file *file, struct poll_table_struct *wait)
1247
{
1248
        struct dev_state *ps = (struct dev_state *)file->private_data;
1249
        unsigned int mask = 0;
1250
 
1251
        poll_wait(file, &ps->wait, wait);
1252
        if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
1253
                mask |= POLLOUT | POLLWRNORM;
1254
        if (!ps->dev)
1255
                mask |= POLLERR | POLLHUP;
1256
        return mask;
1257
}
1258
 
1259
struct file_operations usbdevfs_device_file_operations = {
1260
        llseek:         usbdev_lseek,
1261
        read:           usbdev_read,
1262
        poll:           usbdev_poll,
1263
        ioctl:          usbdev_ioctl,
1264
        open:           usbdev_open,
1265
        release:        usbdev_release,
1266
};

powered by: WebSVN 2.1.0

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