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