]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/usb/core/devio.c
efi/arm: Fix boot crash with CONFIG_CPUMASK_OFFSTACK=y
[mirror_ubuntu-artful-kernel.git] / drivers / usb / core / devio.c
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 * This file implements the usbfs/x/y files, where
23 * x is the bus number and y the device number.
24 *
25 * It allows user space programs/"drivers" to communicate directly
26 * with USB devices without intervening kernel driver.
27 *
28 * Revision history
29 * 22.12.1999 0.1 Initial release (split from proc_usb.c)
30 * 04.01.2000 0.2 Turned into its own filesystem
31 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
32 * (CAN-2005-3055)
33 */
34
35 /*****************************************************************************/
36
37 #include <linux/fs.h>
38 #include <linux/mm.h>
39 #include <linux/slab.h>
40 #include <linux/signal.h>
41 #include <linux/poll.h>
42 #include <linux/module.h>
43 #include <linux/string.h>
44 #include <linux/usb.h>
45 #include <linux/usbdevice_fs.h>
46 #include <linux/usb/hcd.h> /* for usbcore internals */
47 #include <linux/cdev.h>
48 #include <linux/notifier.h>
49 #include <linux/security.h>
50 #include <linux/user_namespace.h>
51 #include <linux/scatterlist.h>
52 #include <linux/uaccess.h>
53 #include <linux/dma-mapping.h>
54 #include <asm/byteorder.h>
55 #include <linux/moduleparam.h>
56
57 #include "usb.h"
58
59 #define USB_MAXBUS 64
60 #define USB_DEVICE_MAX (USB_MAXBUS * 128)
61 #define USB_SG_SIZE 16384 /* split-size for large txs */
62
63 /* Mutual exclusion for removal, open, and release */
64 DEFINE_MUTEX(usbfs_mutex);
65
66 struct usb_dev_state {
67 struct list_head list; /* state list */
68 struct usb_device *dev;
69 struct file *file;
70 spinlock_t lock; /* protects the async urb lists */
71 struct list_head async_pending;
72 struct list_head async_completed;
73 struct list_head memory_list;
74 wait_queue_head_t wait; /* wake up if a request completed */
75 unsigned int discsignr;
76 struct pid *disc_pid;
77 const struct cred *cred;
78 void __user *disccontext;
79 unsigned long ifclaimed;
80 u32 secid;
81 u32 disabled_bulk_eps;
82 bool privileges_dropped;
83 unsigned long interface_allowed_mask;
84 };
85
86 struct usb_memory {
87 struct list_head memlist;
88 int vma_use_count;
89 int urb_use_count;
90 u32 size;
91 void *mem;
92 dma_addr_t dma_handle;
93 unsigned long vm_start;
94 struct usb_dev_state *ps;
95 };
96
97 struct async {
98 struct list_head asynclist;
99 struct usb_dev_state *ps;
100 struct pid *pid;
101 const struct cred *cred;
102 unsigned int signr;
103 unsigned int ifnum;
104 void __user *userbuffer;
105 void __user *userurb;
106 struct urb *urb;
107 struct usb_memory *usbm;
108 unsigned int mem_usage;
109 int status;
110 u32 secid;
111 u8 bulk_addr;
112 u8 bulk_status;
113 };
114
115 static bool usbfs_snoop;
116 module_param(usbfs_snoop, bool, S_IRUGO | S_IWUSR);
117 MODULE_PARM_DESC(usbfs_snoop, "true to log all usbfs traffic");
118
119 static unsigned usbfs_snoop_max = 65536;
120 module_param(usbfs_snoop_max, uint, S_IRUGO | S_IWUSR);
121 MODULE_PARM_DESC(usbfs_snoop_max,
122 "maximum number of bytes to print while snooping");
123
124 #define snoop(dev, format, arg...) \
125 do { \
126 if (usbfs_snoop) \
127 dev_info(dev, format, ## arg); \
128 } while (0)
129
130 enum snoop_when {
131 SUBMIT, COMPLETE
132 };
133
134 #define USB_DEVICE_DEV MKDEV(USB_DEVICE_MAJOR, 0)
135
136 /* Limit on the total amount of memory we can allocate for transfers */
137 static u32 usbfs_memory_mb = 16;
138 module_param(usbfs_memory_mb, uint, 0644);
139 MODULE_PARM_DESC(usbfs_memory_mb,
140 "maximum MB allowed for usbfs buffers (0 = no limit)");
141
142 static atomic64_t usbfs_memory_usage; /* Total memory currently allocated */
143
144 /* Check whether it's okay to allocate more memory for a transfer */
145 static int usbfs_increase_memory_usage(u64 amount)
146 {
147 u64 lim;
148
149 lim = ACCESS_ONCE(usbfs_memory_mb);
150 lim <<= 20;
151
152 atomic64_add(amount, &usbfs_memory_usage);
153
154 if (lim > 0 && atomic64_read(&usbfs_memory_usage) > lim) {
155 atomic64_sub(amount, &usbfs_memory_usage);
156 return -ENOMEM;
157 }
158
159 return 0;
160 }
161
162 /* Memory for a transfer is being deallocated */
163 static void usbfs_decrease_memory_usage(u64 amount)
164 {
165 atomic64_sub(amount, &usbfs_memory_usage);
166 }
167
168 static int connected(struct usb_dev_state *ps)
169 {
170 return (!list_empty(&ps->list) &&
171 ps->dev->state != USB_STATE_NOTATTACHED);
172 }
173
174 static void dec_usb_memory_use_count(struct usb_memory *usbm, int *count)
175 {
176 struct usb_dev_state *ps = usbm->ps;
177 unsigned long flags;
178
179 spin_lock_irqsave(&ps->lock, flags);
180 --*count;
181 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) {
182 list_del(&usbm->memlist);
183 spin_unlock_irqrestore(&ps->lock, flags);
184
185 usb_free_coherent(ps->dev, usbm->size, usbm->mem,
186 usbm->dma_handle);
187 usbfs_decrease_memory_usage(
188 usbm->size + sizeof(struct usb_memory));
189 kfree(usbm);
190 } else {
191 spin_unlock_irqrestore(&ps->lock, flags);
192 }
193 }
194
195 static void usbdev_vm_open(struct vm_area_struct *vma)
196 {
197 struct usb_memory *usbm = vma->vm_private_data;
198 unsigned long flags;
199
200 spin_lock_irqsave(&usbm->ps->lock, flags);
201 ++usbm->vma_use_count;
202 spin_unlock_irqrestore(&usbm->ps->lock, flags);
203 }
204
205 static void usbdev_vm_close(struct vm_area_struct *vma)
206 {
207 struct usb_memory *usbm = vma->vm_private_data;
208
209 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
210 }
211
212 static struct vm_operations_struct usbdev_vm_ops = {
213 .open = usbdev_vm_open,
214 .close = usbdev_vm_close
215 };
216
217 static int usbdev_mmap(struct file *file, struct vm_area_struct *vma)
218 {
219 struct usb_memory *usbm = NULL;
220 struct usb_dev_state *ps = file->private_data;
221 size_t size = vma->vm_end - vma->vm_start;
222 void *mem;
223 unsigned long flags;
224 dma_addr_t dma_handle;
225 int ret;
226
227 ret = usbfs_increase_memory_usage(size + sizeof(struct usb_memory));
228 if (ret)
229 goto error;
230
231 usbm = kzalloc(sizeof(struct usb_memory), GFP_KERNEL);
232 if (!usbm) {
233 ret = -ENOMEM;
234 goto error_decrease_mem;
235 }
236
237 mem = usb_alloc_coherent(ps->dev, size, GFP_USER | __GFP_NOWARN,
238 &dma_handle);
239 if (!mem) {
240 ret = -ENOMEM;
241 goto error_free_usbm;
242 }
243
244 memset(mem, 0, size);
245
246 usbm->mem = mem;
247 usbm->dma_handle = dma_handle;
248 usbm->size = size;
249 usbm->ps = ps;
250 usbm->vm_start = vma->vm_start;
251 usbm->vma_use_count = 1;
252 INIT_LIST_HEAD(&usbm->memlist);
253
254 if (remap_pfn_range(vma, vma->vm_start,
255 virt_to_phys(usbm->mem) >> PAGE_SHIFT,
256 size, vma->vm_page_prot) < 0) {
257 dec_usb_memory_use_count(usbm, &usbm->vma_use_count);
258 return -EAGAIN;
259 }
260
261 vma->vm_flags |= VM_IO;
262 vma->vm_flags |= (VM_DONTEXPAND | VM_DONTDUMP);
263 vma->vm_ops = &usbdev_vm_ops;
264 vma->vm_private_data = usbm;
265
266 spin_lock_irqsave(&ps->lock, flags);
267 list_add_tail(&usbm->memlist, &ps->memory_list);
268 spin_unlock_irqrestore(&ps->lock, flags);
269
270 return 0;
271
272 error_free_usbm:
273 kfree(usbm);
274 error_decrease_mem:
275 usbfs_decrease_memory_usage(size + sizeof(struct usb_memory));
276 error:
277 return ret;
278 }
279
280 static ssize_t usbdev_read(struct file *file, char __user *buf, size_t nbytes,
281 loff_t *ppos)
282 {
283 struct usb_dev_state *ps = file->private_data;
284 struct usb_device *dev = ps->dev;
285 ssize_t ret = 0;
286 unsigned len;
287 loff_t pos;
288 int i;
289
290 pos = *ppos;
291 usb_lock_device(dev);
292 if (!connected(ps)) {
293 ret = -ENODEV;
294 goto err;
295 } else if (pos < 0) {
296 ret = -EINVAL;
297 goto err;
298 }
299
300 if (pos < sizeof(struct usb_device_descriptor)) {
301 /* 18 bytes - fits on the stack */
302 struct usb_device_descriptor temp_desc;
303
304 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor));
305 le16_to_cpus(&temp_desc.bcdUSB);
306 le16_to_cpus(&temp_desc.idVendor);
307 le16_to_cpus(&temp_desc.idProduct);
308 le16_to_cpus(&temp_desc.bcdDevice);
309
310 len = sizeof(struct usb_device_descriptor) - pos;
311 if (len > nbytes)
312 len = nbytes;
313 if (copy_to_user(buf, ((char *)&temp_desc) + pos, len)) {
314 ret = -EFAULT;
315 goto err;
316 }
317
318 *ppos += len;
319 buf += len;
320 nbytes -= len;
321 ret += len;
322 }
323
324 pos = sizeof(struct usb_device_descriptor);
325 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) {
326 struct usb_config_descriptor *config =
327 (struct usb_config_descriptor *)dev->rawdescriptors[i];
328 unsigned int length = le16_to_cpu(config->wTotalLength);
329
330 if (*ppos < pos + length) {
331
332 /* The descriptor may claim to be longer than it
333 * really is. Here is the actual allocated length. */
334 unsigned alloclen =
335 le16_to_cpu(dev->config[i].desc.wTotalLength);
336
337 len = length - (*ppos - pos);
338 if (len > nbytes)
339 len = nbytes;
340
341 /* Simply don't write (skip over) unallocated parts */
342 if (alloclen > (*ppos - pos)) {
343 alloclen -= (*ppos - pos);
344 if (copy_to_user(buf,
345 dev->rawdescriptors[i] + (*ppos - pos),
346 min(len, alloclen))) {
347 ret = -EFAULT;
348 goto err;
349 }
350 }
351
352 *ppos += len;
353 buf += len;
354 nbytes -= len;
355 ret += len;
356 }
357
358 pos += length;
359 }
360
361 err:
362 usb_unlock_device(dev);
363 return ret;
364 }
365
366 /*
367 * async list handling
368 */
369
370 static struct async *alloc_async(unsigned int numisoframes)
371 {
372 struct async *as;
373
374 as = kzalloc(sizeof(struct async), GFP_KERNEL);
375 if (!as)
376 return NULL;
377 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL);
378 if (!as->urb) {
379 kfree(as);
380 return NULL;
381 }
382 return as;
383 }
384
385 static void free_async(struct async *as)
386 {
387 int i;
388
389 put_pid(as->pid);
390 if (as->cred)
391 put_cred(as->cred);
392 for (i = 0; i < as->urb->num_sgs; i++) {
393 if (sg_page(&as->urb->sg[i]))
394 kfree(sg_virt(&as->urb->sg[i]));
395 }
396
397 kfree(as->urb->sg);
398 if (as->usbm == NULL)
399 kfree(as->urb->transfer_buffer);
400 else
401 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
402
403 kfree(as->urb->setup_packet);
404 usb_free_urb(as->urb);
405 usbfs_decrease_memory_usage(as->mem_usage);
406 kfree(as);
407 }
408
409 static void async_newpending(struct async *as)
410 {
411 struct usb_dev_state *ps = as->ps;
412 unsigned long flags;
413
414 spin_lock_irqsave(&ps->lock, flags);
415 list_add_tail(&as->asynclist, &ps->async_pending);
416 spin_unlock_irqrestore(&ps->lock, flags);
417 }
418
419 static void async_removepending(struct async *as)
420 {
421 struct usb_dev_state *ps = as->ps;
422 unsigned long flags;
423
424 spin_lock_irqsave(&ps->lock, flags);
425 list_del_init(&as->asynclist);
426 spin_unlock_irqrestore(&ps->lock, flags);
427 }
428
429 static struct async *async_getcompleted(struct usb_dev_state *ps)
430 {
431 unsigned long flags;
432 struct async *as = NULL;
433
434 spin_lock_irqsave(&ps->lock, flags);
435 if (!list_empty(&ps->async_completed)) {
436 as = list_entry(ps->async_completed.next, struct async,
437 asynclist);
438 list_del_init(&as->asynclist);
439 }
440 spin_unlock_irqrestore(&ps->lock, flags);
441 return as;
442 }
443
444 static struct async *async_getpending(struct usb_dev_state *ps,
445 void __user *userurb)
446 {
447 struct async *as;
448
449 list_for_each_entry(as, &ps->async_pending, asynclist)
450 if (as->userurb == userurb) {
451 list_del_init(&as->asynclist);
452 return as;
453 }
454
455 return NULL;
456 }
457
458 static void snoop_urb(struct usb_device *udev,
459 void __user *userurb, int pipe, unsigned length,
460 int timeout_or_status, enum snoop_when when,
461 unsigned char *data, unsigned data_len)
462 {
463 static const char *types[] = {"isoc", "int", "ctrl", "bulk"};
464 static const char *dirs[] = {"out", "in"};
465 int ep;
466 const char *t, *d;
467
468 if (!usbfs_snoop)
469 return;
470
471 ep = usb_pipeendpoint(pipe);
472 t = types[usb_pipetype(pipe)];
473 d = dirs[!!usb_pipein(pipe)];
474
475 if (userurb) { /* Async */
476 if (when == SUBMIT)
477 dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
478 "length %u\n",
479 userurb, ep, t, d, length);
480 else
481 dev_info(&udev->dev, "userurb %p, ep%d %s-%s, "
482 "actual_length %u status %d\n",
483 userurb, ep, t, d, length,
484 timeout_or_status);
485 } else {
486 if (when == SUBMIT)
487 dev_info(&udev->dev, "ep%d %s-%s, length %u, "
488 "timeout %d\n",
489 ep, t, d, length, timeout_or_status);
490 else
491 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, "
492 "status %d\n",
493 ep, t, d, length, timeout_or_status);
494 }
495
496 data_len = min(data_len, usbfs_snoop_max);
497 if (data && data_len > 0) {
498 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
499 data, data_len, 1);
500 }
501 }
502
503 static void snoop_urb_data(struct urb *urb, unsigned len)
504 {
505 int i, size;
506
507 len = min(len, usbfs_snoop_max);
508 if (!usbfs_snoop || len == 0)
509 return;
510
511 if (urb->num_sgs == 0) {
512 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
513 urb->transfer_buffer, len, 1);
514 return;
515 }
516
517 for (i = 0; i < urb->num_sgs && len; i++) {
518 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
519 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE, 32, 1,
520 sg_virt(&urb->sg[i]), size, 1);
521 len -= size;
522 }
523 }
524
525 static int copy_urb_data_to_user(u8 __user *userbuffer, struct urb *urb)
526 {
527 unsigned i, len, size;
528
529 if (urb->number_of_packets > 0) /* Isochronous */
530 len = urb->transfer_buffer_length;
531 else /* Non-Isoc */
532 len = urb->actual_length;
533
534 if (urb->num_sgs == 0) {
535 if (copy_to_user(userbuffer, urb->transfer_buffer, len))
536 return -EFAULT;
537 return 0;
538 }
539
540 for (i = 0; i < urb->num_sgs && len; i++) {
541 size = (len > USB_SG_SIZE) ? USB_SG_SIZE : len;
542 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size))
543 return -EFAULT;
544 userbuffer += size;
545 len -= size;
546 }
547
548 return 0;
549 }
550
551 #define AS_CONTINUATION 1
552 #define AS_UNLINK 2
553
554 static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr)
555 __releases(ps->lock)
556 __acquires(ps->lock)
557 {
558 struct urb *urb;
559 struct async *as;
560
561 /* Mark all the pending URBs that match bulk_addr, up to but not
562 * including the first one without AS_CONTINUATION. If such an
563 * URB is encountered then a new transfer has already started so
564 * the endpoint doesn't need to be disabled; otherwise it does.
565 */
566 list_for_each_entry(as, &ps->async_pending, asynclist) {
567 if (as->bulk_addr == bulk_addr) {
568 if (as->bulk_status != AS_CONTINUATION)
569 goto rescan;
570 as->bulk_status = AS_UNLINK;
571 as->bulk_addr = 0;
572 }
573 }
574 ps->disabled_bulk_eps |= (1 << bulk_addr);
575
576 /* Now carefully unlink all the marked pending URBs */
577 rescan:
578 list_for_each_entry(as, &ps->async_pending, asynclist) {
579 if (as->bulk_status == AS_UNLINK) {
580 as->bulk_status = 0; /* Only once */
581 urb = as->urb;
582 usb_get_urb(urb);
583 spin_unlock(&ps->lock); /* Allow completions */
584 usb_unlink_urb(urb);
585 usb_put_urb(urb);
586 spin_lock(&ps->lock);
587 goto rescan;
588 }
589 }
590 }
591
592 static void async_completed(struct urb *urb)
593 {
594 struct async *as = urb->context;
595 struct usb_dev_state *ps = as->ps;
596 struct siginfo sinfo;
597 struct pid *pid = NULL;
598 u32 secid = 0;
599 const struct cred *cred = NULL;
600 int signr;
601
602 spin_lock(&ps->lock);
603 list_move_tail(&as->asynclist, &ps->async_completed);
604 as->status = urb->status;
605 signr = as->signr;
606 if (signr) {
607 memset(&sinfo, 0, sizeof(sinfo));
608 sinfo.si_signo = as->signr;
609 sinfo.si_errno = as->status;
610 sinfo.si_code = SI_ASYNCIO;
611 sinfo.si_addr = as->userurb;
612 pid = get_pid(as->pid);
613 cred = get_cred(as->cred);
614 secid = as->secid;
615 }
616 snoop(&urb->dev->dev, "urb complete\n");
617 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length,
618 as->status, COMPLETE, NULL, 0);
619 if ((urb->transfer_flags & URB_DIR_MASK) == URB_DIR_IN)
620 snoop_urb_data(urb, urb->actual_length);
621
622 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
623 as->status != -ENOENT)
624 cancel_bulk_urbs(ps, as->bulk_addr);
625 spin_unlock(&ps->lock);
626
627 if (signr) {
628 kill_pid_info_as_cred(sinfo.si_signo, &sinfo, pid, cred, secid);
629 put_pid(pid);
630 put_cred(cred);
631 }
632
633 wake_up(&ps->wait);
634 }
635
636 static void destroy_async(struct usb_dev_state *ps, struct list_head *list)
637 {
638 struct urb *urb;
639 struct async *as;
640 unsigned long flags;
641
642 spin_lock_irqsave(&ps->lock, flags);
643 while (!list_empty(list)) {
644 as = list_entry(list->next, struct async, asynclist);
645 list_del_init(&as->asynclist);
646 urb = as->urb;
647 usb_get_urb(urb);
648
649 /* drop the spinlock so the completion handler can run */
650 spin_unlock_irqrestore(&ps->lock, flags);
651 usb_kill_urb(urb);
652 usb_put_urb(urb);
653 spin_lock_irqsave(&ps->lock, flags);
654 }
655 spin_unlock_irqrestore(&ps->lock, flags);
656 }
657
658 static void destroy_async_on_interface(struct usb_dev_state *ps,
659 unsigned int ifnum)
660 {
661 struct list_head *p, *q, hitlist;
662 unsigned long flags;
663
664 INIT_LIST_HEAD(&hitlist);
665 spin_lock_irqsave(&ps->lock, flags);
666 list_for_each_safe(p, q, &ps->async_pending)
667 if (ifnum == list_entry(p, struct async, asynclist)->ifnum)
668 list_move_tail(p, &hitlist);
669 spin_unlock_irqrestore(&ps->lock, flags);
670 destroy_async(ps, &hitlist);
671 }
672
673 static void destroy_all_async(struct usb_dev_state *ps)
674 {
675 destroy_async(ps, &ps->async_pending);
676 }
677
678 /*
679 * interface claims are made only at the request of user level code,
680 * which can also release them (explicitly or by closing files).
681 * they're also undone when devices disconnect.
682 */
683
684 static int driver_probe(struct usb_interface *intf,
685 const struct usb_device_id *id)
686 {
687 return -ENODEV;
688 }
689
690 static void driver_disconnect(struct usb_interface *intf)
691 {
692 struct usb_dev_state *ps = usb_get_intfdata(intf);
693 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber;
694
695 if (!ps)
696 return;
697
698 /* NOTE: this relies on usbcore having canceled and completed
699 * all pending I/O requests; 2.6 does that.
700 */
701
702 if (likely(ifnum < 8*sizeof(ps->ifclaimed)))
703 clear_bit(ifnum, &ps->ifclaimed);
704 else
705 dev_warn(&intf->dev, "interface number %u out of range\n",
706 ifnum);
707
708 usb_set_intfdata(intf, NULL);
709
710 /* force async requests to complete */
711 destroy_async_on_interface(ps, ifnum);
712 }
713
714 /* The following routines are merely placeholders. There is no way
715 * to inform a user task about suspend or resumes.
716 */
717 static int driver_suspend(struct usb_interface *intf, pm_message_t msg)
718 {
719 return 0;
720 }
721
722 static int driver_resume(struct usb_interface *intf)
723 {
724 return 0;
725 }
726
727 struct usb_driver usbfs_driver = {
728 .name = "usbfs",
729 .probe = driver_probe,
730 .disconnect = driver_disconnect,
731 .suspend = driver_suspend,
732 .resume = driver_resume,
733 };
734
735 static int claimintf(struct usb_dev_state *ps, unsigned int ifnum)
736 {
737 struct usb_device *dev = ps->dev;
738 struct usb_interface *intf;
739 int err;
740
741 if (ifnum >= 8*sizeof(ps->ifclaimed))
742 return -EINVAL;
743 /* already claimed */
744 if (test_bit(ifnum, &ps->ifclaimed))
745 return 0;
746
747 if (ps->privileges_dropped &&
748 !test_bit(ifnum, &ps->interface_allowed_mask))
749 return -EACCES;
750
751 intf = usb_ifnum_to_if(dev, ifnum);
752 if (!intf)
753 err = -ENOENT;
754 else
755 err = usb_driver_claim_interface(&usbfs_driver, intf, ps);
756 if (err == 0)
757 set_bit(ifnum, &ps->ifclaimed);
758 return err;
759 }
760
761 static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum)
762 {
763 struct usb_device *dev;
764 struct usb_interface *intf;
765 int err;
766
767 err = -EINVAL;
768 if (ifnum >= 8*sizeof(ps->ifclaimed))
769 return err;
770 dev = ps->dev;
771 intf = usb_ifnum_to_if(dev, ifnum);
772 if (!intf)
773 err = -ENOENT;
774 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) {
775 usb_driver_release_interface(&usbfs_driver, intf);
776 err = 0;
777 }
778 return err;
779 }
780
781 static int checkintf(struct usb_dev_state *ps, unsigned int ifnum)
782 {
783 if (ps->dev->state != USB_STATE_CONFIGURED)
784 return -EHOSTUNREACH;
785 if (ifnum >= 8*sizeof(ps->ifclaimed))
786 return -EINVAL;
787 if (test_bit(ifnum, &ps->ifclaimed))
788 return 0;
789 /* if not yet claimed, claim it for the driver */
790 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim "
791 "interface %u before use\n", task_pid_nr(current),
792 current->comm, ifnum);
793 return claimintf(ps, ifnum);
794 }
795
796 static int findintfep(struct usb_device *dev, unsigned int ep)
797 {
798 unsigned int i, j, e;
799 struct usb_interface *intf;
800 struct usb_host_interface *alts;
801 struct usb_endpoint_descriptor *endpt;
802
803 if (ep & ~(USB_DIR_IN|0xf))
804 return -EINVAL;
805 if (!dev->actconfig)
806 return -ESRCH;
807 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
808 intf = dev->actconfig->interface[i];
809 for (j = 0; j < intf->num_altsetting; j++) {
810 alts = &intf->altsetting[j];
811 for (e = 0; e < alts->desc.bNumEndpoints; e++) {
812 endpt = &alts->endpoint[e].desc;
813 if (endpt->bEndpointAddress == ep)
814 return alts->desc.bInterfaceNumber;
815 }
816 }
817 }
818 return -ENOENT;
819 }
820
821 static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype,
822 unsigned int request, unsigned int index)
823 {
824 int ret = 0;
825 struct usb_host_interface *alt_setting;
826
827 if (ps->dev->state != USB_STATE_UNAUTHENTICATED
828 && ps->dev->state != USB_STATE_ADDRESS
829 && ps->dev->state != USB_STATE_CONFIGURED)
830 return -EHOSTUNREACH;
831 if (USB_TYPE_VENDOR == (USB_TYPE_MASK & requesttype))
832 return 0;
833
834 /*
835 * check for the special corner case 'get_device_id' in the printer
836 * class specification, which we always want to allow as it is used
837 * to query things like ink level, etc.
838 */
839 if (requesttype == 0xa1 && request == 0) {
840 alt_setting = usb_find_alt_setting(ps->dev->actconfig,
841 index >> 8, index & 0xff);
842 if (alt_setting
843 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER)
844 return 0;
845 }
846
847 index &= 0xff;
848 switch (requesttype & USB_RECIP_MASK) {
849 case USB_RECIP_ENDPOINT:
850 if ((index & ~USB_DIR_IN) == 0)
851 return 0;
852 ret = findintfep(ps->dev, index);
853 if (ret < 0) {
854 /*
855 * Some not fully compliant Win apps seem to get
856 * index wrong and have the endpoint number here
857 * rather than the endpoint address (with the
858 * correct direction). Win does let this through,
859 * so we'll not reject it here but leave it to
860 * the device to not break KVM. But we warn.
861 */
862 ret = findintfep(ps->dev, index ^ 0x80);
863 if (ret >= 0)
864 dev_info(&ps->dev->dev,
865 "%s: process %i (%s) requesting ep %02x but needs %02x\n",
866 __func__, task_pid_nr(current),
867 current->comm, index, index ^ 0x80);
868 }
869 if (ret >= 0)
870 ret = checkintf(ps, ret);
871 break;
872
873 case USB_RECIP_INTERFACE:
874 ret = checkintf(ps, index);
875 break;
876 }
877 return ret;
878 }
879
880 static struct usb_host_endpoint *ep_to_host_endpoint(struct usb_device *dev,
881 unsigned char ep)
882 {
883 if (ep & USB_ENDPOINT_DIR_MASK)
884 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK];
885 else
886 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK];
887 }
888
889 static int parse_usbdevfs_streams(struct usb_dev_state *ps,
890 struct usbdevfs_streams __user *streams,
891 unsigned int *num_streams_ret,
892 unsigned int *num_eps_ret,
893 struct usb_host_endpoint ***eps_ret,
894 struct usb_interface **intf_ret)
895 {
896 unsigned int i, num_streams, num_eps;
897 struct usb_host_endpoint **eps;
898 struct usb_interface *intf = NULL;
899 unsigned char ep;
900 int ifnum, ret;
901
902 if (get_user(num_streams, &streams->num_streams) ||
903 get_user(num_eps, &streams->num_eps))
904 return -EFAULT;
905
906 if (num_eps < 1 || num_eps > USB_MAXENDPOINTS)
907 return -EINVAL;
908
909 /* The XHCI controller allows max 2 ^ 16 streams */
910 if (num_streams_ret && (num_streams < 2 || num_streams > 65536))
911 return -EINVAL;
912
913 eps = kmalloc(num_eps * sizeof(*eps), GFP_KERNEL);
914 if (!eps)
915 return -ENOMEM;
916
917 for (i = 0; i < num_eps; i++) {
918 if (get_user(ep, &streams->eps[i])) {
919 ret = -EFAULT;
920 goto error;
921 }
922 eps[i] = ep_to_host_endpoint(ps->dev, ep);
923 if (!eps[i]) {
924 ret = -EINVAL;
925 goto error;
926 }
927
928 /* usb_alloc/free_streams operate on an usb_interface */
929 ifnum = findintfep(ps->dev, ep);
930 if (ifnum < 0) {
931 ret = ifnum;
932 goto error;
933 }
934
935 if (i == 0) {
936 ret = checkintf(ps, ifnum);
937 if (ret < 0)
938 goto error;
939 intf = usb_ifnum_to_if(ps->dev, ifnum);
940 } else {
941 /* Verify all eps belong to the same interface */
942 if (ifnum != intf->altsetting->desc.bInterfaceNumber) {
943 ret = -EINVAL;
944 goto error;
945 }
946 }
947 }
948
949 if (num_streams_ret)
950 *num_streams_ret = num_streams;
951 *num_eps_ret = num_eps;
952 *eps_ret = eps;
953 *intf_ret = intf;
954
955 return 0;
956
957 error:
958 kfree(eps);
959 return ret;
960 }
961
962 static int match_devt(struct device *dev, void *data)
963 {
964 return dev->devt == (dev_t) (unsigned long) data;
965 }
966
967 static struct usb_device *usbdev_lookup_by_devt(dev_t devt)
968 {
969 struct device *dev;
970
971 dev = bus_find_device(&usb_bus_type, NULL,
972 (void *) (unsigned long) devt, match_devt);
973 if (!dev)
974 return NULL;
975 return to_usb_device(dev);
976 }
977
978 /*
979 * file operations
980 */
981 static int usbdev_open(struct inode *inode, struct file *file)
982 {
983 struct usb_device *dev = NULL;
984 struct usb_dev_state *ps;
985 int ret;
986
987 ret = -ENOMEM;
988 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL);
989 if (!ps)
990 goto out_free_ps;
991
992 ret = -ENODEV;
993
994 /* Protect against simultaneous removal or release */
995 mutex_lock(&usbfs_mutex);
996
997 /* usbdev device-node */
998 if (imajor(inode) == USB_DEVICE_MAJOR)
999 dev = usbdev_lookup_by_devt(inode->i_rdev);
1000
1001 mutex_unlock(&usbfs_mutex);
1002
1003 if (!dev)
1004 goto out_free_ps;
1005
1006 usb_lock_device(dev);
1007 if (dev->state == USB_STATE_NOTATTACHED)
1008 goto out_unlock_device;
1009
1010 ret = usb_autoresume_device(dev);
1011 if (ret)
1012 goto out_unlock_device;
1013
1014 ps->dev = dev;
1015 ps->file = file;
1016 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */
1017 spin_lock_init(&ps->lock);
1018 INIT_LIST_HEAD(&ps->list);
1019 INIT_LIST_HEAD(&ps->async_pending);
1020 INIT_LIST_HEAD(&ps->async_completed);
1021 INIT_LIST_HEAD(&ps->memory_list);
1022 init_waitqueue_head(&ps->wait);
1023 ps->disc_pid = get_pid(task_pid(current));
1024 ps->cred = get_current_cred();
1025 security_task_getsecid(current, &ps->secid);
1026 smp_wmb();
1027 list_add_tail(&ps->list, &dev->filelist);
1028 file->private_data = ps;
1029 usb_unlock_device(dev);
1030 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current),
1031 current->comm);
1032 return ret;
1033
1034 out_unlock_device:
1035 usb_unlock_device(dev);
1036 usb_put_dev(dev);
1037 out_free_ps:
1038 kfree(ps);
1039 return ret;
1040 }
1041
1042 static int usbdev_release(struct inode *inode, struct file *file)
1043 {
1044 struct usb_dev_state *ps = file->private_data;
1045 struct usb_device *dev = ps->dev;
1046 unsigned int ifnum;
1047 struct async *as;
1048
1049 usb_lock_device(dev);
1050 usb_hub_release_all_ports(dev, ps);
1051
1052 list_del_init(&ps->list);
1053
1054 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed);
1055 ifnum++) {
1056 if (test_bit(ifnum, &ps->ifclaimed))
1057 releaseintf(ps, ifnum);
1058 }
1059 destroy_all_async(ps);
1060 usb_autosuspend_device(dev);
1061 usb_unlock_device(dev);
1062 usb_put_dev(dev);
1063 put_pid(ps->disc_pid);
1064 put_cred(ps->cred);
1065
1066 as = async_getcompleted(ps);
1067 while (as) {
1068 free_async(as);
1069 as = async_getcompleted(ps);
1070 }
1071
1072 kfree(ps);
1073 return 0;
1074 }
1075
1076 static int proc_control(struct usb_dev_state *ps, void __user *arg)
1077 {
1078 struct usb_device *dev = ps->dev;
1079 struct usbdevfs_ctrltransfer ctrl;
1080 unsigned int tmo;
1081 unsigned char *tbuf;
1082 unsigned wLength;
1083 int i, pipe, ret;
1084
1085 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
1086 return -EFAULT;
1087 ret = check_ctrlrecip(ps, ctrl.bRequestType, ctrl.bRequest,
1088 ctrl.wIndex);
1089 if (ret)
1090 return ret;
1091 wLength = ctrl.wLength; /* To suppress 64k PAGE_SIZE warning */
1092 if (wLength > PAGE_SIZE)
1093 return -EINVAL;
1094 ret = usbfs_increase_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1095 sizeof(struct usb_ctrlrequest));
1096 if (ret)
1097 return ret;
1098 tbuf = (unsigned char *)__get_free_page(GFP_KERNEL);
1099 if (!tbuf) {
1100 ret = -ENOMEM;
1101 goto done;
1102 }
1103 tmo = ctrl.timeout;
1104 snoop(&dev->dev, "control urb: bRequestType=%02x "
1105 "bRequest=%02x wValue=%04x "
1106 "wIndex=%04x wLength=%04x\n",
1107 ctrl.bRequestType, ctrl.bRequest, ctrl.wValue,
1108 ctrl.wIndex, ctrl.wLength);
1109 if (ctrl.bRequestType & 0x80) {
1110 if (ctrl.wLength && !access_ok(VERIFY_WRITE, ctrl.data,
1111 ctrl.wLength)) {
1112 ret = -EINVAL;
1113 goto done;
1114 }
1115 pipe = usb_rcvctrlpipe(dev, 0);
1116 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT, NULL, 0);
1117
1118 usb_unlock_device(dev);
1119 i = usb_control_msg(dev, pipe, ctrl.bRequest,
1120 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1121 tbuf, ctrl.wLength, tmo);
1122 usb_lock_device(dev);
1123 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE,
1124 tbuf, max(i, 0));
1125 if ((i > 0) && ctrl.wLength) {
1126 if (copy_to_user(ctrl.data, tbuf, i)) {
1127 ret = -EFAULT;
1128 goto done;
1129 }
1130 }
1131 } else {
1132 if (ctrl.wLength) {
1133 if (copy_from_user(tbuf, ctrl.data, ctrl.wLength)) {
1134 ret = -EFAULT;
1135 goto done;
1136 }
1137 }
1138 pipe = usb_sndctrlpipe(dev, 0);
1139 snoop_urb(dev, NULL, pipe, ctrl.wLength, tmo, SUBMIT,
1140 tbuf, ctrl.wLength);
1141
1142 usb_unlock_device(dev);
1143 i = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), ctrl.bRequest,
1144 ctrl.bRequestType, ctrl.wValue, ctrl.wIndex,
1145 tbuf, ctrl.wLength, tmo);
1146 usb_lock_device(dev);
1147 snoop_urb(dev, NULL, pipe, max(i, 0), min(i, 0), COMPLETE, NULL, 0);
1148 }
1149 if (i < 0 && i != -EPIPE) {
1150 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL "
1151 "failed cmd %s rqt %u rq %u len %u ret %d\n",
1152 current->comm, ctrl.bRequestType, ctrl.bRequest,
1153 ctrl.wLength, i);
1154 }
1155 ret = i;
1156 done:
1157 free_page((unsigned long) tbuf);
1158 usbfs_decrease_memory_usage(PAGE_SIZE + sizeof(struct urb) +
1159 sizeof(struct usb_ctrlrequest));
1160 return ret;
1161 }
1162
1163 static int proc_bulk(struct usb_dev_state *ps, void __user *arg)
1164 {
1165 struct usb_device *dev = ps->dev;
1166 struct usbdevfs_bulktransfer bulk;
1167 unsigned int tmo, len1, pipe;
1168 int len2;
1169 unsigned char *tbuf;
1170 int i, ret;
1171
1172 if (copy_from_user(&bulk, arg, sizeof(bulk)))
1173 return -EFAULT;
1174 ret = findintfep(ps->dev, bulk.ep);
1175 if (ret < 0)
1176 return ret;
1177 ret = checkintf(ps, ret);
1178 if (ret)
1179 return ret;
1180 if (bulk.ep & USB_DIR_IN)
1181 pipe = usb_rcvbulkpipe(dev, bulk.ep & 0x7f);
1182 else
1183 pipe = usb_sndbulkpipe(dev, bulk.ep & 0x7f);
1184 if (!usb_maxpacket(dev, pipe, !(bulk.ep & USB_DIR_IN)))
1185 return -EINVAL;
1186 len1 = bulk.len;
1187 if (len1 >= (INT_MAX - sizeof(struct urb)))
1188 return -EINVAL;
1189 ret = usbfs_increase_memory_usage(len1 + sizeof(struct urb));
1190 if (ret)
1191 return ret;
1192 tbuf = kmalloc(len1, GFP_KERNEL);
1193 if (!tbuf) {
1194 ret = -ENOMEM;
1195 goto done;
1196 }
1197 tmo = bulk.timeout;
1198 if (bulk.ep & 0x80) {
1199 if (len1 && !access_ok(VERIFY_WRITE, bulk.data, len1)) {
1200 ret = -EINVAL;
1201 goto done;
1202 }
1203 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, NULL, 0);
1204
1205 usb_unlock_device(dev);
1206 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1207 usb_lock_device(dev);
1208 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, tbuf, len2);
1209
1210 if (!i && len2) {
1211 if (copy_to_user(bulk.data, tbuf, len2)) {
1212 ret = -EFAULT;
1213 goto done;
1214 }
1215 }
1216 } else {
1217 if (len1) {
1218 if (copy_from_user(tbuf, bulk.data, len1)) {
1219 ret = -EFAULT;
1220 goto done;
1221 }
1222 }
1223 snoop_urb(dev, NULL, pipe, len1, tmo, SUBMIT, tbuf, len1);
1224
1225 usb_unlock_device(dev);
1226 i = usb_bulk_msg(dev, pipe, tbuf, len1, &len2, tmo);
1227 usb_lock_device(dev);
1228 snoop_urb(dev, NULL, pipe, len2, i, COMPLETE, NULL, 0);
1229 }
1230 ret = (i < 0 ? i : len2);
1231 done:
1232 kfree(tbuf);
1233 usbfs_decrease_memory_usage(len1 + sizeof(struct urb));
1234 return ret;
1235 }
1236
1237 static void check_reset_of_active_ep(struct usb_device *udev,
1238 unsigned int epnum, char *ioctl_name)
1239 {
1240 struct usb_host_endpoint **eps;
1241 struct usb_host_endpoint *ep;
1242
1243 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out;
1244 ep = eps[epnum & 0x0f];
1245 if (ep && !list_empty(&ep->urb_list))
1246 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n",
1247 task_pid_nr(current), current->comm,
1248 ioctl_name, epnum);
1249 }
1250
1251 static int proc_resetep(struct usb_dev_state *ps, void __user *arg)
1252 {
1253 unsigned int ep;
1254 int ret;
1255
1256 if (get_user(ep, (unsigned int __user *)arg))
1257 return -EFAULT;
1258 ret = findintfep(ps->dev, ep);
1259 if (ret < 0)
1260 return ret;
1261 ret = checkintf(ps, ret);
1262 if (ret)
1263 return ret;
1264 check_reset_of_active_ep(ps->dev, ep, "RESETEP");
1265 usb_reset_endpoint(ps->dev, ep);
1266 return 0;
1267 }
1268
1269 static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg)
1270 {
1271 unsigned int ep;
1272 int pipe;
1273 int ret;
1274
1275 if (get_user(ep, (unsigned int __user *)arg))
1276 return -EFAULT;
1277 ret = findintfep(ps->dev, ep);
1278 if (ret < 0)
1279 return ret;
1280 ret = checkintf(ps, ret);
1281 if (ret)
1282 return ret;
1283 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT");
1284 if (ep & USB_DIR_IN)
1285 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f);
1286 else
1287 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f);
1288
1289 return usb_clear_halt(ps->dev, pipe);
1290 }
1291
1292 static int proc_getdriver(struct usb_dev_state *ps, void __user *arg)
1293 {
1294 struct usbdevfs_getdriver gd;
1295 struct usb_interface *intf;
1296 int ret;
1297
1298 if (copy_from_user(&gd, arg, sizeof(gd)))
1299 return -EFAULT;
1300 intf = usb_ifnum_to_if(ps->dev, gd.interface);
1301 if (!intf || !intf->dev.driver)
1302 ret = -ENODATA;
1303 else {
1304 strlcpy(gd.driver, intf->dev.driver->name,
1305 sizeof(gd.driver));
1306 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0);
1307 }
1308 return ret;
1309 }
1310
1311 static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg)
1312 {
1313 struct usbdevfs_connectinfo ci;
1314
1315 memset(&ci, 0, sizeof(ci));
1316 ci.devnum = ps->dev->devnum;
1317 ci.slow = ps->dev->speed == USB_SPEED_LOW;
1318
1319 if (copy_to_user(arg, &ci, sizeof(ci)))
1320 return -EFAULT;
1321 return 0;
1322 }
1323
1324 static int proc_resetdevice(struct usb_dev_state *ps)
1325 {
1326 struct usb_host_config *actconfig = ps->dev->actconfig;
1327 struct usb_interface *interface;
1328 int i, number;
1329
1330 /* Don't allow a device reset if the process has dropped the
1331 * privilege to do such things and any of the interfaces are
1332 * currently claimed.
1333 */
1334 if (ps->privileges_dropped && actconfig) {
1335 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1336 interface = actconfig->interface[i];
1337 number = interface->cur_altsetting->desc.bInterfaceNumber;
1338 if (usb_interface_claimed(interface) &&
1339 !test_bit(number, &ps->ifclaimed)) {
1340 dev_warn(&ps->dev->dev,
1341 "usbfs: interface %d claimed by %s while '%s' resets device\n",
1342 number, interface->dev.driver->name, current->comm);
1343 return -EACCES;
1344 }
1345 }
1346 }
1347
1348 return usb_reset_device(ps->dev);
1349 }
1350
1351 static int proc_setintf(struct usb_dev_state *ps, void __user *arg)
1352 {
1353 struct usbdevfs_setinterface setintf;
1354 int ret;
1355
1356 if (copy_from_user(&setintf, arg, sizeof(setintf)))
1357 return -EFAULT;
1358 ret = checkintf(ps, setintf.interface);
1359 if (ret)
1360 return ret;
1361
1362 destroy_async_on_interface(ps, setintf.interface);
1363
1364 return usb_set_interface(ps->dev, setintf.interface,
1365 setintf.altsetting);
1366 }
1367
1368 static int proc_setconfig(struct usb_dev_state *ps, void __user *arg)
1369 {
1370 int u;
1371 int status = 0;
1372 struct usb_host_config *actconfig;
1373
1374 if (get_user(u, (int __user *)arg))
1375 return -EFAULT;
1376
1377 actconfig = ps->dev->actconfig;
1378
1379 /* Don't touch the device if any interfaces are claimed.
1380 * It could interfere with other drivers' operations, and if
1381 * an interface is claimed by usbfs it could easily deadlock.
1382 */
1383 if (actconfig) {
1384 int i;
1385
1386 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) {
1387 if (usb_interface_claimed(actconfig->interface[i])) {
1388 dev_warn(&ps->dev->dev,
1389 "usbfs: interface %d claimed by %s "
1390 "while '%s' sets config #%d\n",
1391 actconfig->interface[i]
1392 ->cur_altsetting
1393 ->desc.bInterfaceNumber,
1394 actconfig->interface[i]
1395 ->dev.driver->name,
1396 current->comm, u);
1397 status = -EBUSY;
1398 break;
1399 }
1400 }
1401 }
1402
1403 /* SET_CONFIGURATION is often abused as a "cheap" driver reset,
1404 * so avoid usb_set_configuration()'s kick to sysfs
1405 */
1406 if (status == 0) {
1407 if (actconfig && actconfig->desc.bConfigurationValue == u)
1408 status = usb_reset_configuration(ps->dev);
1409 else
1410 status = usb_set_configuration(ps->dev, u);
1411 }
1412
1413 return status;
1414 }
1415
1416 static struct usb_memory *
1417 find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb)
1418 {
1419 struct usb_memory *usbm = NULL, *iter;
1420 unsigned long flags;
1421 unsigned long uurb_start = (unsigned long)uurb->buffer;
1422
1423 spin_lock_irqsave(&ps->lock, flags);
1424 list_for_each_entry(iter, &ps->memory_list, memlist) {
1425 if (uurb_start >= iter->vm_start &&
1426 uurb_start < iter->vm_start + iter->size) {
1427 if (uurb->buffer_length > iter->vm_start + iter->size -
1428 uurb_start) {
1429 usbm = ERR_PTR(-EINVAL);
1430 } else {
1431 usbm = iter;
1432 usbm->urb_use_count++;
1433 }
1434 break;
1435 }
1436 }
1437 spin_unlock_irqrestore(&ps->lock, flags);
1438 return usbm;
1439 }
1440
1441 static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb,
1442 struct usbdevfs_iso_packet_desc __user *iso_frame_desc,
1443 void __user *arg)
1444 {
1445 struct usbdevfs_iso_packet_desc *isopkt = NULL;
1446 struct usb_host_endpoint *ep;
1447 struct async *as = NULL;
1448 struct usb_ctrlrequest *dr = NULL;
1449 unsigned int u, totlen, isofrmlen;
1450 int i, ret, is_in, num_sgs = 0, ifnum = -1;
1451 int number_of_packets = 0;
1452 unsigned int stream_id = 0;
1453 void *buf;
1454
1455 if (uurb->flags & ~(USBDEVFS_URB_ISO_ASAP |
1456 USBDEVFS_URB_SHORT_NOT_OK |
1457 USBDEVFS_URB_BULK_CONTINUATION |
1458 USBDEVFS_URB_NO_FSBR |
1459 USBDEVFS_URB_ZERO_PACKET |
1460 USBDEVFS_URB_NO_INTERRUPT))
1461 return -EINVAL;
1462 if (uurb->buffer_length > 0 && !uurb->buffer)
1463 return -EINVAL;
1464 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL &&
1465 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) {
1466 ifnum = findintfep(ps->dev, uurb->endpoint);
1467 if (ifnum < 0)
1468 return ifnum;
1469 ret = checkintf(ps, ifnum);
1470 if (ret)
1471 return ret;
1472 }
1473 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint);
1474 if (!ep)
1475 return -ENOENT;
1476 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0;
1477
1478 u = 0;
1479 switch (uurb->type) {
1480 case USBDEVFS_URB_TYPE_CONTROL:
1481 if (!usb_endpoint_xfer_control(&ep->desc))
1482 return -EINVAL;
1483 /* min 8 byte setup packet */
1484 if (uurb->buffer_length < 8)
1485 return -EINVAL;
1486 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
1487 if (!dr)
1488 return -ENOMEM;
1489 if (copy_from_user(dr, uurb->buffer, 8)) {
1490 ret = -EFAULT;
1491 goto error;
1492 }
1493 if (uurb->buffer_length < (le16_to_cpup(&dr->wLength) + 8)) {
1494 ret = -EINVAL;
1495 goto error;
1496 }
1497 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest,
1498 le16_to_cpup(&dr->wIndex));
1499 if (ret)
1500 goto error;
1501 uurb->buffer_length = le16_to_cpup(&dr->wLength);
1502 uurb->buffer += 8;
1503 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) {
1504 is_in = 1;
1505 uurb->endpoint |= USB_DIR_IN;
1506 } else {
1507 is_in = 0;
1508 uurb->endpoint &= ~USB_DIR_IN;
1509 }
1510 snoop(&ps->dev->dev, "control urb: bRequestType=%02x "
1511 "bRequest=%02x wValue=%04x "
1512 "wIndex=%04x wLength=%04x\n",
1513 dr->bRequestType, dr->bRequest,
1514 __le16_to_cpup(&dr->wValue),
1515 __le16_to_cpup(&dr->wIndex),
1516 __le16_to_cpup(&dr->wLength));
1517 u = sizeof(struct usb_ctrlrequest);
1518 break;
1519
1520 case USBDEVFS_URB_TYPE_BULK:
1521 switch (usb_endpoint_type(&ep->desc)) {
1522 case USB_ENDPOINT_XFER_CONTROL:
1523 case USB_ENDPOINT_XFER_ISOC:
1524 return -EINVAL;
1525 case USB_ENDPOINT_XFER_INT:
1526 /* allow single-shot interrupt transfers */
1527 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT;
1528 goto interrupt_urb;
1529 }
1530 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE);
1531 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize)
1532 num_sgs = 0;
1533 if (ep->streams)
1534 stream_id = uurb->stream_id;
1535 break;
1536
1537 case USBDEVFS_URB_TYPE_INTERRUPT:
1538 if (!usb_endpoint_xfer_int(&ep->desc))
1539 return -EINVAL;
1540 interrupt_urb:
1541 break;
1542
1543 case USBDEVFS_URB_TYPE_ISO:
1544 /* arbitrary limit */
1545 if (uurb->number_of_packets < 1 ||
1546 uurb->number_of_packets > 128)
1547 return -EINVAL;
1548 if (!usb_endpoint_xfer_isoc(&ep->desc))
1549 return -EINVAL;
1550 number_of_packets = uurb->number_of_packets;
1551 isofrmlen = sizeof(struct usbdevfs_iso_packet_desc) *
1552 number_of_packets;
1553 isopkt = memdup_user(iso_frame_desc, isofrmlen);
1554 if (IS_ERR(isopkt)) {
1555 ret = PTR_ERR(isopkt);
1556 isopkt = NULL;
1557 goto error;
1558 }
1559 for (totlen = u = 0; u < number_of_packets; u++) {
1560 /*
1561 * arbitrary limit need for USB 3.0
1562 * bMaxBurst (0~15 allowed, 1~16 packets)
1563 * bmAttributes (bit 1:0, mult 0~2, 1~3 packets)
1564 * sizemax: 1024 * 16 * 3 = 49152
1565 */
1566 if (isopkt[u].length > 49152) {
1567 ret = -EINVAL;
1568 goto error;
1569 }
1570 totlen += isopkt[u].length;
1571 }
1572 u *= sizeof(struct usb_iso_packet_descriptor);
1573 uurb->buffer_length = totlen;
1574 break;
1575
1576 default:
1577 return -EINVAL;
1578 }
1579
1580 if (uurb->buffer_length > 0 &&
1581 !access_ok(is_in ? VERIFY_WRITE : VERIFY_READ,
1582 uurb->buffer, uurb->buffer_length)) {
1583 ret = -EFAULT;
1584 goto error;
1585 }
1586 as = alloc_async(number_of_packets);
1587 if (!as) {
1588 ret = -ENOMEM;
1589 goto error;
1590 }
1591
1592 as->usbm = find_memory_area(ps, uurb);
1593 if (IS_ERR(as->usbm)) {
1594 ret = PTR_ERR(as->usbm);
1595 as->usbm = NULL;
1596 goto error;
1597 }
1598
1599 /* do not use SG buffers when memory mapped segments
1600 * are in use
1601 */
1602 if (as->usbm)
1603 num_sgs = 0;
1604
1605 u += sizeof(struct async) + sizeof(struct urb) + uurb->buffer_length +
1606 num_sgs * sizeof(struct scatterlist);
1607 ret = usbfs_increase_memory_usage(u);
1608 if (ret)
1609 goto error;
1610 as->mem_usage = u;
1611
1612 if (num_sgs) {
1613 as->urb->sg = kmalloc(num_sgs * sizeof(struct scatterlist),
1614 GFP_KERNEL);
1615 if (!as->urb->sg) {
1616 ret = -ENOMEM;
1617 goto error;
1618 }
1619 as->urb->num_sgs = num_sgs;
1620 sg_init_table(as->urb->sg, as->urb->num_sgs);
1621
1622 totlen = uurb->buffer_length;
1623 for (i = 0; i < as->urb->num_sgs; i++) {
1624 u = (totlen > USB_SG_SIZE) ? USB_SG_SIZE : totlen;
1625 buf = kmalloc(u, GFP_KERNEL);
1626 if (!buf) {
1627 ret = -ENOMEM;
1628 goto error;
1629 }
1630 sg_set_buf(&as->urb->sg[i], buf, u);
1631
1632 if (!is_in) {
1633 if (copy_from_user(buf, uurb->buffer, u)) {
1634 ret = -EFAULT;
1635 goto error;
1636 }
1637 uurb->buffer += u;
1638 }
1639 totlen -= u;
1640 }
1641 } else if (uurb->buffer_length > 0) {
1642 if (as->usbm) {
1643 unsigned long uurb_start = (unsigned long)uurb->buffer;
1644
1645 as->urb->transfer_buffer = as->usbm->mem +
1646 (uurb_start - as->usbm->vm_start);
1647 } else {
1648 as->urb->transfer_buffer = kmalloc(uurb->buffer_length,
1649 GFP_KERNEL);
1650 if (!as->urb->transfer_buffer) {
1651 ret = -ENOMEM;
1652 goto error;
1653 }
1654 if (!is_in) {
1655 if (copy_from_user(as->urb->transfer_buffer,
1656 uurb->buffer,
1657 uurb->buffer_length)) {
1658 ret = -EFAULT;
1659 goto error;
1660 }
1661 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) {
1662 /*
1663 * Isochronous input data may end up being
1664 * discontiguous if some of the packets are
1665 * short. Clear the buffer so that the gaps
1666 * don't leak kernel data to userspace.
1667 */
1668 memset(as->urb->transfer_buffer, 0,
1669 uurb->buffer_length);
1670 }
1671 }
1672 }
1673 as->urb->dev = ps->dev;
1674 as->urb->pipe = (uurb->type << 30) |
1675 __create_pipe(ps->dev, uurb->endpoint & 0xf) |
1676 (uurb->endpoint & USB_DIR_IN);
1677
1678 /* This tedious sequence is necessary because the URB_* flags
1679 * are internal to the kernel and subject to change, whereas
1680 * the USBDEVFS_URB_* flags are a user API and must not be changed.
1681 */
1682 u = (is_in ? URB_DIR_IN : URB_DIR_OUT);
1683 if (uurb->flags & USBDEVFS_URB_ISO_ASAP)
1684 u |= URB_ISO_ASAP;
1685 if (uurb->flags & USBDEVFS_URB_SHORT_NOT_OK && is_in)
1686 u |= URB_SHORT_NOT_OK;
1687 if (uurb->flags & USBDEVFS_URB_NO_FSBR)
1688 u |= URB_NO_FSBR;
1689 if (uurb->flags & USBDEVFS_URB_ZERO_PACKET)
1690 u |= URB_ZERO_PACKET;
1691 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT)
1692 u |= URB_NO_INTERRUPT;
1693 as->urb->transfer_flags = u;
1694
1695 as->urb->transfer_buffer_length = uurb->buffer_length;
1696 as->urb->setup_packet = (unsigned char *)dr;
1697 dr = NULL;
1698 as->urb->start_frame = uurb->start_frame;
1699 as->urb->number_of_packets = number_of_packets;
1700 as->urb->stream_id = stream_id;
1701
1702 if (ep->desc.bInterval) {
1703 if (uurb->type == USBDEVFS_URB_TYPE_ISO ||
1704 ps->dev->speed == USB_SPEED_HIGH ||
1705 ps->dev->speed >= USB_SPEED_SUPER)
1706 as->urb->interval = 1 <<
1707 min(15, ep->desc.bInterval - 1);
1708 else
1709 as->urb->interval = ep->desc.bInterval;
1710 }
1711
1712 as->urb->context = as;
1713 as->urb->complete = async_completed;
1714 for (totlen = u = 0; u < number_of_packets; u++) {
1715 as->urb->iso_frame_desc[u].offset = totlen;
1716 as->urb->iso_frame_desc[u].length = isopkt[u].length;
1717 totlen += isopkt[u].length;
1718 }
1719 kfree(isopkt);
1720 isopkt = NULL;
1721 as->ps = ps;
1722 as->userurb = arg;
1723 if (as->usbm) {
1724 unsigned long uurb_start = (unsigned long)uurb->buffer;
1725
1726 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
1727 as->urb->transfer_dma = as->usbm->dma_handle +
1728 (uurb_start - as->usbm->vm_start);
1729 } else if (is_in && uurb->buffer_length > 0)
1730 as->userbuffer = uurb->buffer;
1731 as->signr = uurb->signr;
1732 as->ifnum = ifnum;
1733 as->pid = get_pid(task_pid(current));
1734 as->cred = get_current_cred();
1735 security_task_getsecid(current, &as->secid);
1736 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1737 as->urb->transfer_buffer_length, 0, SUBMIT,
1738 NULL, 0);
1739 if (!is_in)
1740 snoop_urb_data(as->urb, as->urb->transfer_buffer_length);
1741
1742 async_newpending(as);
1743
1744 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1745 spin_lock_irq(&ps->lock);
1746
1747 /* Not exactly the endpoint address; the direction bit is
1748 * shifted to the 0x10 position so that the value will be
1749 * between 0 and 31.
1750 */
1751 as->bulk_addr = usb_endpoint_num(&ep->desc) |
1752 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK)
1753 >> 3);
1754
1755 /* If this bulk URB is the start of a new transfer, re-enable
1756 * the endpoint. Otherwise mark it as a continuation URB.
1757 */
1758 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION)
1759 as->bulk_status = AS_CONTINUATION;
1760 else
1761 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr);
1762
1763 /* Don't accept continuation URBs if the endpoint is
1764 * disabled because of an earlier error.
1765 */
1766 if (ps->disabled_bulk_eps & (1 << as->bulk_addr))
1767 ret = -EREMOTEIO;
1768 else
1769 ret = usb_submit_urb(as->urb, GFP_ATOMIC);
1770 spin_unlock_irq(&ps->lock);
1771 } else {
1772 ret = usb_submit_urb(as->urb, GFP_KERNEL);
1773 }
1774
1775 if (ret) {
1776 dev_printk(KERN_DEBUG, &ps->dev->dev,
1777 "usbfs: usb_submit_urb returned %d\n", ret);
1778 snoop_urb(ps->dev, as->userurb, as->urb->pipe,
1779 0, ret, COMPLETE, NULL, 0);
1780 async_removepending(as);
1781 goto error;
1782 }
1783 return 0;
1784
1785 error:
1786 if (as && as->usbm)
1787 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count);
1788 kfree(isopkt);
1789 kfree(dr);
1790 if (as)
1791 free_async(as);
1792 return ret;
1793 }
1794
1795 static int proc_submiturb(struct usb_dev_state *ps, void __user *arg)
1796 {
1797 struct usbdevfs_urb uurb;
1798
1799 if (copy_from_user(&uurb, arg, sizeof(uurb)))
1800 return -EFAULT;
1801
1802 return proc_do_submiturb(ps, &uurb,
1803 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc),
1804 arg);
1805 }
1806
1807 static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg)
1808 {
1809 struct urb *urb;
1810 struct async *as;
1811 unsigned long flags;
1812
1813 spin_lock_irqsave(&ps->lock, flags);
1814 as = async_getpending(ps, arg);
1815 if (!as) {
1816 spin_unlock_irqrestore(&ps->lock, flags);
1817 return -EINVAL;
1818 }
1819
1820 urb = as->urb;
1821 usb_get_urb(urb);
1822 spin_unlock_irqrestore(&ps->lock, flags);
1823
1824 usb_kill_urb(urb);
1825 usb_put_urb(urb);
1826
1827 return 0;
1828 }
1829
1830 static int processcompl(struct async *as, void __user * __user *arg)
1831 {
1832 struct urb *urb = as->urb;
1833 struct usbdevfs_urb __user *userurb = as->userurb;
1834 void __user *addr = as->userurb;
1835 unsigned int i;
1836
1837 if (as->userbuffer && urb->actual_length) {
1838 if (copy_urb_data_to_user(as->userbuffer, urb))
1839 goto err_out;
1840 }
1841 if (put_user(as->status, &userurb->status))
1842 goto err_out;
1843 if (put_user(urb->actual_length, &userurb->actual_length))
1844 goto err_out;
1845 if (put_user(urb->error_count, &userurb->error_count))
1846 goto err_out;
1847
1848 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
1849 for (i = 0; i < urb->number_of_packets; i++) {
1850 if (put_user(urb->iso_frame_desc[i].actual_length,
1851 &userurb->iso_frame_desc[i].actual_length))
1852 goto err_out;
1853 if (put_user(urb->iso_frame_desc[i].status,
1854 &userurb->iso_frame_desc[i].status))
1855 goto err_out;
1856 }
1857 }
1858
1859 if (put_user(addr, (void __user * __user *)arg))
1860 return -EFAULT;
1861 return 0;
1862
1863 err_out:
1864 return -EFAULT;
1865 }
1866
1867 static struct async *reap_as(struct usb_dev_state *ps)
1868 {
1869 DECLARE_WAITQUEUE(wait, current);
1870 struct async *as = NULL;
1871 struct usb_device *dev = ps->dev;
1872
1873 add_wait_queue(&ps->wait, &wait);
1874 for (;;) {
1875 __set_current_state(TASK_INTERRUPTIBLE);
1876 as = async_getcompleted(ps);
1877 if (as || !connected(ps))
1878 break;
1879 if (signal_pending(current))
1880 break;
1881 usb_unlock_device(dev);
1882 schedule();
1883 usb_lock_device(dev);
1884 }
1885 remove_wait_queue(&ps->wait, &wait);
1886 set_current_state(TASK_RUNNING);
1887 return as;
1888 }
1889
1890 static int proc_reapurb(struct usb_dev_state *ps, void __user *arg)
1891 {
1892 struct async *as = reap_as(ps);
1893
1894 if (as) {
1895 int retval;
1896
1897 snoop(&ps->dev->dev, "reap %p\n", as->userurb);
1898 retval = processcompl(as, (void __user * __user *)arg);
1899 free_async(as);
1900 return retval;
1901 }
1902 if (signal_pending(current))
1903 return -EINTR;
1904 return -ENODEV;
1905 }
1906
1907 static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg)
1908 {
1909 int retval;
1910 struct async *as;
1911
1912 as = async_getcompleted(ps);
1913 if (as) {
1914 snoop(&ps->dev->dev, "reap %p\n", as->userurb);
1915 retval = processcompl(as, (void __user * __user *)arg);
1916 free_async(as);
1917 } else {
1918 retval = (connected(ps) ? -EAGAIN : -ENODEV);
1919 }
1920 return retval;
1921 }
1922
1923 #ifdef CONFIG_COMPAT
1924 static int proc_control_compat(struct usb_dev_state *ps,
1925 struct usbdevfs_ctrltransfer32 __user *p32)
1926 {
1927 struct usbdevfs_ctrltransfer __user *p;
1928 __u32 udata;
1929 p = compat_alloc_user_space(sizeof(*p));
1930 if (copy_in_user(p, p32, (sizeof(*p32) - sizeof(compat_caddr_t))) ||
1931 get_user(udata, &p32->data) ||
1932 put_user(compat_ptr(udata), &p->data))
1933 return -EFAULT;
1934 return proc_control(ps, p);
1935 }
1936
1937 static int proc_bulk_compat(struct usb_dev_state *ps,
1938 struct usbdevfs_bulktransfer32 __user *p32)
1939 {
1940 struct usbdevfs_bulktransfer __user *p;
1941 compat_uint_t n;
1942 compat_caddr_t addr;
1943
1944 p = compat_alloc_user_space(sizeof(*p));
1945
1946 if (get_user(n, &p32->ep) || put_user(n, &p->ep) ||
1947 get_user(n, &p32->len) || put_user(n, &p->len) ||
1948 get_user(n, &p32->timeout) || put_user(n, &p->timeout) ||
1949 get_user(addr, &p32->data) || put_user(compat_ptr(addr), &p->data))
1950 return -EFAULT;
1951
1952 return proc_bulk(ps, p);
1953 }
1954 static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg)
1955 {
1956 struct usbdevfs_disconnectsignal32 ds;
1957
1958 if (copy_from_user(&ds, arg, sizeof(ds)))
1959 return -EFAULT;
1960 ps->discsignr = ds.signr;
1961 ps->disccontext = compat_ptr(ds.context);
1962 return 0;
1963 }
1964
1965 static int get_urb32(struct usbdevfs_urb *kurb,
1966 struct usbdevfs_urb32 __user *uurb)
1967 {
1968 __u32 uptr;
1969 if (!access_ok(VERIFY_READ, uurb, sizeof(*uurb)) ||
1970 __get_user(kurb->type, &uurb->type) ||
1971 __get_user(kurb->endpoint, &uurb->endpoint) ||
1972 __get_user(kurb->status, &uurb->status) ||
1973 __get_user(kurb->flags, &uurb->flags) ||
1974 __get_user(kurb->buffer_length, &uurb->buffer_length) ||
1975 __get_user(kurb->actual_length, &uurb->actual_length) ||
1976 __get_user(kurb->start_frame, &uurb->start_frame) ||
1977 __get_user(kurb->number_of_packets, &uurb->number_of_packets) ||
1978 __get_user(kurb->error_count, &uurb->error_count) ||
1979 __get_user(kurb->signr, &uurb->signr))
1980 return -EFAULT;
1981
1982 if (__get_user(uptr, &uurb->buffer))
1983 return -EFAULT;
1984 kurb->buffer = compat_ptr(uptr);
1985 if (__get_user(uptr, &uurb->usercontext))
1986 return -EFAULT;
1987 kurb->usercontext = compat_ptr(uptr);
1988
1989 return 0;
1990 }
1991
1992 static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg)
1993 {
1994 struct usbdevfs_urb uurb;
1995
1996 if (get_urb32(&uurb, (struct usbdevfs_urb32 __user *)arg))
1997 return -EFAULT;
1998
1999 return proc_do_submiturb(ps, &uurb,
2000 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc,
2001 arg);
2002 }
2003
2004 static int processcompl_compat(struct async *as, void __user * __user *arg)
2005 {
2006 struct urb *urb = as->urb;
2007 struct usbdevfs_urb32 __user *userurb = as->userurb;
2008 void __user *addr = as->userurb;
2009 unsigned int i;
2010
2011 if (as->userbuffer && urb->actual_length) {
2012 if (copy_urb_data_to_user(as->userbuffer, urb))
2013 return -EFAULT;
2014 }
2015 if (put_user(as->status, &userurb->status))
2016 return -EFAULT;
2017 if (put_user(urb->actual_length, &userurb->actual_length))
2018 return -EFAULT;
2019 if (put_user(urb->error_count, &userurb->error_count))
2020 return -EFAULT;
2021
2022 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) {
2023 for (i = 0; i < urb->number_of_packets; i++) {
2024 if (put_user(urb->iso_frame_desc[i].actual_length,
2025 &userurb->iso_frame_desc[i].actual_length))
2026 return -EFAULT;
2027 if (put_user(urb->iso_frame_desc[i].status,
2028 &userurb->iso_frame_desc[i].status))
2029 return -EFAULT;
2030 }
2031 }
2032
2033 if (put_user(ptr_to_compat(addr), (u32 __user *)arg))
2034 return -EFAULT;
2035 return 0;
2036 }
2037
2038 static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg)
2039 {
2040 struct async *as = reap_as(ps);
2041
2042 if (as) {
2043 int retval;
2044
2045 snoop(&ps->dev->dev, "reap %p\n", as->userurb);
2046 retval = processcompl_compat(as, (void __user * __user *)arg);
2047 free_async(as);
2048 return retval;
2049 }
2050 if (signal_pending(current))
2051 return -EINTR;
2052 return -ENODEV;
2053 }
2054
2055 static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg)
2056 {
2057 int retval;
2058 struct async *as;
2059
2060 as = async_getcompleted(ps);
2061 if (as) {
2062 snoop(&ps->dev->dev, "reap %p\n", as->userurb);
2063 retval = processcompl_compat(as, (void __user * __user *)arg);
2064 free_async(as);
2065 } else {
2066 retval = (connected(ps) ? -EAGAIN : -ENODEV);
2067 }
2068 return retval;
2069 }
2070
2071
2072 #endif
2073
2074 static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg)
2075 {
2076 struct usbdevfs_disconnectsignal ds;
2077
2078 if (copy_from_user(&ds, arg, sizeof(ds)))
2079 return -EFAULT;
2080 ps->discsignr = ds.signr;
2081 ps->disccontext = ds.context;
2082 return 0;
2083 }
2084
2085 static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg)
2086 {
2087 unsigned int ifnum;
2088
2089 if (get_user(ifnum, (unsigned int __user *)arg))
2090 return -EFAULT;
2091 return claimintf(ps, ifnum);
2092 }
2093
2094 static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg)
2095 {
2096 unsigned int ifnum;
2097 int ret;
2098
2099 if (get_user(ifnum, (unsigned int __user *)arg))
2100 return -EFAULT;
2101 ret = releaseintf(ps, ifnum);
2102 if (ret < 0)
2103 return ret;
2104 destroy_async_on_interface(ps, ifnum);
2105 return 0;
2106 }
2107
2108 static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl)
2109 {
2110 int size;
2111 void *buf = NULL;
2112 int retval = 0;
2113 struct usb_interface *intf = NULL;
2114 struct usb_driver *driver = NULL;
2115
2116 if (ps->privileges_dropped)
2117 return -EACCES;
2118
2119 /* alloc buffer */
2120 size = _IOC_SIZE(ctl->ioctl_code);
2121 if (size > 0) {
2122 buf = kmalloc(size, GFP_KERNEL);
2123 if (buf == NULL)
2124 return -ENOMEM;
2125 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) {
2126 if (copy_from_user(buf, ctl->data, size)) {
2127 kfree(buf);
2128 return -EFAULT;
2129 }
2130 } else {
2131 memset(buf, 0, size);
2132 }
2133 }
2134
2135 if (!connected(ps)) {
2136 kfree(buf);
2137 return -ENODEV;
2138 }
2139
2140 if (ps->dev->state != USB_STATE_CONFIGURED)
2141 retval = -EHOSTUNREACH;
2142 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno)))
2143 retval = -EINVAL;
2144 else switch (ctl->ioctl_code) {
2145
2146 /* disconnect kernel driver from interface */
2147 case USBDEVFS_DISCONNECT:
2148 if (intf->dev.driver) {
2149 driver = to_usb_driver(intf->dev.driver);
2150 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2151 usb_driver_release_interface(driver, intf);
2152 } else
2153 retval = -ENODATA;
2154 break;
2155
2156 /* let kernel drivers try to (re)bind to the interface */
2157 case USBDEVFS_CONNECT:
2158 if (!intf->dev.driver)
2159 retval = device_attach(&intf->dev);
2160 else
2161 retval = -EBUSY;
2162 break;
2163
2164 /* talk directly to the interface's driver */
2165 default:
2166 if (intf->dev.driver)
2167 driver = to_usb_driver(intf->dev.driver);
2168 if (driver == NULL || driver->unlocked_ioctl == NULL) {
2169 retval = -ENOTTY;
2170 } else {
2171 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf);
2172 if (retval == -ENOIOCTLCMD)
2173 retval = -ENOTTY;
2174 }
2175 }
2176
2177 /* cleanup and return */
2178 if (retval >= 0
2179 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0
2180 && size > 0
2181 && copy_to_user(ctl->data, buf, size) != 0)
2182 retval = -EFAULT;
2183
2184 kfree(buf);
2185 return retval;
2186 }
2187
2188 static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg)
2189 {
2190 struct usbdevfs_ioctl ctrl;
2191
2192 if (copy_from_user(&ctrl, arg, sizeof(ctrl)))
2193 return -EFAULT;
2194 return proc_ioctl(ps, &ctrl);
2195 }
2196
2197 #ifdef CONFIG_COMPAT
2198 static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg)
2199 {
2200 struct usbdevfs_ioctl32 __user *uioc;
2201 struct usbdevfs_ioctl ctrl;
2202 u32 udata;
2203
2204 uioc = compat_ptr((long)arg);
2205 if (!access_ok(VERIFY_READ, uioc, sizeof(*uioc)) ||
2206 __get_user(ctrl.ifno, &uioc->ifno) ||
2207 __get_user(ctrl.ioctl_code, &uioc->ioctl_code) ||
2208 __get_user(udata, &uioc->data))
2209 return -EFAULT;
2210 ctrl.data = compat_ptr(udata);
2211
2212 return proc_ioctl(ps, &ctrl);
2213 }
2214 #endif
2215
2216 static int proc_claim_port(struct usb_dev_state *ps, void __user *arg)
2217 {
2218 unsigned portnum;
2219 int rc;
2220
2221 if (get_user(portnum, (unsigned __user *) arg))
2222 return -EFAULT;
2223 rc = usb_hub_claim_port(ps->dev, portnum, ps);
2224 if (rc == 0)
2225 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n",
2226 portnum, task_pid_nr(current), current->comm);
2227 return rc;
2228 }
2229
2230 static int proc_release_port(struct usb_dev_state *ps, void __user *arg)
2231 {
2232 unsigned portnum;
2233
2234 if (get_user(portnum, (unsigned __user *) arg))
2235 return -EFAULT;
2236 return usb_hub_release_port(ps->dev, portnum, ps);
2237 }
2238
2239 static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg)
2240 {
2241 __u32 caps;
2242
2243 caps = USBDEVFS_CAP_ZERO_PACKET | USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
2244 USBDEVFS_CAP_REAP_AFTER_DISCONNECT | USBDEVFS_CAP_MMAP |
2245 USBDEVFS_CAP_DROP_PRIVILEGES;
2246 if (!ps->dev->bus->no_stop_on_short)
2247 caps |= USBDEVFS_CAP_BULK_CONTINUATION;
2248 if (ps->dev->bus->sg_tablesize)
2249 caps |= USBDEVFS_CAP_BULK_SCATTER_GATHER;
2250
2251 if (put_user(caps, (__u32 __user *)arg))
2252 return -EFAULT;
2253
2254 return 0;
2255 }
2256
2257 static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg)
2258 {
2259 struct usbdevfs_disconnect_claim dc;
2260 struct usb_interface *intf;
2261
2262 if (copy_from_user(&dc, arg, sizeof(dc)))
2263 return -EFAULT;
2264
2265 intf = usb_ifnum_to_if(ps->dev, dc.interface);
2266 if (!intf)
2267 return -EINVAL;
2268
2269 if (intf->dev.driver) {
2270 struct usb_driver *driver = to_usb_driver(intf->dev.driver);
2271
2272 if (ps->privileges_dropped)
2273 return -EACCES;
2274
2275 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_IF_DRIVER) &&
2276 strncmp(dc.driver, intf->dev.driver->name,
2277 sizeof(dc.driver)) != 0)
2278 return -EBUSY;
2279
2280 if ((dc.flags & USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER) &&
2281 strncmp(dc.driver, intf->dev.driver->name,
2282 sizeof(dc.driver)) == 0)
2283 return -EBUSY;
2284
2285 dev_dbg(&intf->dev, "disconnect by usbfs\n");
2286 usb_driver_release_interface(driver, intf);
2287 }
2288
2289 return claimintf(ps, dc.interface);
2290 }
2291
2292 static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg)
2293 {
2294 unsigned num_streams, num_eps;
2295 struct usb_host_endpoint **eps;
2296 struct usb_interface *intf;
2297 int r;
2298
2299 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps,
2300 &eps, &intf);
2301 if (r)
2302 return r;
2303
2304 destroy_async_on_interface(ps,
2305 intf->altsetting[0].desc.bInterfaceNumber);
2306
2307 r = usb_alloc_streams(intf, eps, num_eps, num_streams, GFP_KERNEL);
2308 kfree(eps);
2309 return r;
2310 }
2311
2312 static int proc_free_streams(struct usb_dev_state *ps, void __user *arg)
2313 {
2314 unsigned num_eps;
2315 struct usb_host_endpoint **eps;
2316 struct usb_interface *intf;
2317 int r;
2318
2319 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf);
2320 if (r)
2321 return r;
2322
2323 destroy_async_on_interface(ps,
2324 intf->altsetting[0].desc.bInterfaceNumber);
2325
2326 r = usb_free_streams(intf, eps, num_eps, GFP_KERNEL);
2327 kfree(eps);
2328 return r;
2329 }
2330
2331 static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg)
2332 {
2333 u32 data;
2334
2335 if (copy_from_user(&data, arg, sizeof(data)))
2336 return -EFAULT;
2337
2338 /* This is a one way operation. Once privileges are
2339 * dropped, you cannot regain them. You may however reissue
2340 * this ioctl to shrink the allowed interfaces mask.
2341 */
2342 ps->interface_allowed_mask &= data;
2343 ps->privileges_dropped = true;
2344
2345 return 0;
2346 }
2347
2348 /*
2349 * NOTE: All requests here that have interface numbers as parameters
2350 * are assuming that somehow the configuration has been prevented from
2351 * changing. But there's no mechanism to ensure that...
2352 */
2353 static long usbdev_do_ioctl(struct file *file, unsigned int cmd,
2354 void __user *p)
2355 {
2356 struct usb_dev_state *ps = file->private_data;
2357 struct inode *inode = file_inode(file);
2358 struct usb_device *dev = ps->dev;
2359 int ret = -ENOTTY;
2360
2361 if (!(file->f_mode & FMODE_WRITE))
2362 return -EPERM;
2363
2364 usb_lock_device(dev);
2365
2366 /* Reap operations are allowed even after disconnection */
2367 switch (cmd) {
2368 case USBDEVFS_REAPURB:
2369 snoop(&dev->dev, "%s: REAPURB\n", __func__);
2370 ret = proc_reapurb(ps, p);
2371 goto done;
2372
2373 case USBDEVFS_REAPURBNDELAY:
2374 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__);
2375 ret = proc_reapurbnonblock(ps, p);
2376 goto done;
2377
2378 #ifdef CONFIG_COMPAT
2379 case USBDEVFS_REAPURB32:
2380 snoop(&dev->dev, "%s: REAPURB32\n", __func__);
2381 ret = proc_reapurb_compat(ps, p);
2382 goto done;
2383
2384 case USBDEVFS_REAPURBNDELAY32:
2385 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__);
2386 ret = proc_reapurbnonblock_compat(ps, p);
2387 goto done;
2388 #endif
2389 }
2390
2391 if (!connected(ps)) {
2392 usb_unlock_device(dev);
2393 return -ENODEV;
2394 }
2395
2396 switch (cmd) {
2397 case USBDEVFS_CONTROL:
2398 snoop(&dev->dev, "%s: CONTROL\n", __func__);
2399 ret = proc_control(ps, p);
2400 if (ret >= 0)
2401 inode->i_mtime = current_time(inode);
2402 break;
2403
2404 case USBDEVFS_BULK:
2405 snoop(&dev->dev, "%s: BULK\n", __func__);
2406 ret = proc_bulk(ps, p);
2407 if (ret >= 0)
2408 inode->i_mtime = current_time(inode);
2409 break;
2410
2411 case USBDEVFS_RESETEP:
2412 snoop(&dev->dev, "%s: RESETEP\n", __func__);
2413 ret = proc_resetep(ps, p);
2414 if (ret >= 0)
2415 inode->i_mtime = current_time(inode);
2416 break;
2417
2418 case USBDEVFS_RESET:
2419 snoop(&dev->dev, "%s: RESET\n", __func__);
2420 ret = proc_resetdevice(ps);
2421 break;
2422
2423 case USBDEVFS_CLEAR_HALT:
2424 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__);
2425 ret = proc_clearhalt(ps, p);
2426 if (ret >= 0)
2427 inode->i_mtime = current_time(inode);
2428 break;
2429
2430 case USBDEVFS_GETDRIVER:
2431 snoop(&dev->dev, "%s: GETDRIVER\n", __func__);
2432 ret = proc_getdriver(ps, p);
2433 break;
2434
2435 case USBDEVFS_CONNECTINFO:
2436 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__);
2437 ret = proc_connectinfo(ps, p);
2438 break;
2439
2440 case USBDEVFS_SETINTERFACE:
2441 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__);
2442 ret = proc_setintf(ps, p);
2443 break;
2444
2445 case USBDEVFS_SETCONFIGURATION:
2446 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__);
2447 ret = proc_setconfig(ps, p);
2448 break;
2449
2450 case USBDEVFS_SUBMITURB:
2451 snoop(&dev->dev, "%s: SUBMITURB\n", __func__);
2452 ret = proc_submiturb(ps, p);
2453 if (ret >= 0)
2454 inode->i_mtime = current_time(inode);
2455 break;
2456
2457 #ifdef CONFIG_COMPAT
2458 case USBDEVFS_CONTROL32:
2459 snoop(&dev->dev, "%s: CONTROL32\n", __func__);
2460 ret = proc_control_compat(ps, p);
2461 if (ret >= 0)
2462 inode->i_mtime = current_time(inode);
2463 break;
2464
2465 case USBDEVFS_BULK32:
2466 snoop(&dev->dev, "%s: BULK32\n", __func__);
2467 ret = proc_bulk_compat(ps, p);
2468 if (ret >= 0)
2469 inode->i_mtime = current_time(inode);
2470 break;
2471
2472 case USBDEVFS_DISCSIGNAL32:
2473 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__);
2474 ret = proc_disconnectsignal_compat(ps, p);
2475 break;
2476
2477 case USBDEVFS_SUBMITURB32:
2478 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__);
2479 ret = proc_submiturb_compat(ps, p);
2480 if (ret >= 0)
2481 inode->i_mtime = current_time(inode);
2482 break;
2483
2484 case USBDEVFS_IOCTL32:
2485 snoop(&dev->dev, "%s: IOCTL32\n", __func__);
2486 ret = proc_ioctl_compat(ps, ptr_to_compat(p));
2487 break;
2488 #endif
2489
2490 case USBDEVFS_DISCARDURB:
2491 snoop(&dev->dev, "%s: DISCARDURB %p\n", __func__, p);
2492 ret = proc_unlinkurb(ps, p);
2493 break;
2494
2495 case USBDEVFS_DISCSIGNAL:
2496 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__);
2497 ret = proc_disconnectsignal(ps, p);
2498 break;
2499
2500 case USBDEVFS_CLAIMINTERFACE:
2501 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__);
2502 ret = proc_claiminterface(ps, p);
2503 break;
2504
2505 case USBDEVFS_RELEASEINTERFACE:
2506 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__);
2507 ret = proc_releaseinterface(ps, p);
2508 break;
2509
2510 case USBDEVFS_IOCTL:
2511 snoop(&dev->dev, "%s: IOCTL\n", __func__);
2512 ret = proc_ioctl_default(ps, p);
2513 break;
2514
2515 case USBDEVFS_CLAIM_PORT:
2516 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__);
2517 ret = proc_claim_port(ps, p);
2518 break;
2519
2520 case USBDEVFS_RELEASE_PORT:
2521 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__);
2522 ret = proc_release_port(ps, p);
2523 break;
2524 case USBDEVFS_GET_CAPABILITIES:
2525 ret = proc_get_capabilities(ps, p);
2526 break;
2527 case USBDEVFS_DISCONNECT_CLAIM:
2528 ret = proc_disconnect_claim(ps, p);
2529 break;
2530 case USBDEVFS_ALLOC_STREAMS:
2531 ret = proc_alloc_streams(ps, p);
2532 break;
2533 case USBDEVFS_FREE_STREAMS:
2534 ret = proc_free_streams(ps, p);
2535 break;
2536 case USBDEVFS_DROP_PRIVILEGES:
2537 ret = proc_drop_privileges(ps, p);
2538 break;
2539 }
2540
2541 done:
2542 usb_unlock_device(dev);
2543 if (ret >= 0)
2544 inode->i_atime = current_time(inode);
2545 return ret;
2546 }
2547
2548 static long usbdev_ioctl(struct file *file, unsigned int cmd,
2549 unsigned long arg)
2550 {
2551 int ret;
2552
2553 ret = usbdev_do_ioctl(file, cmd, (void __user *)arg);
2554
2555 return ret;
2556 }
2557
2558 #ifdef CONFIG_COMPAT
2559 static long usbdev_compat_ioctl(struct file *file, unsigned int cmd,
2560 unsigned long arg)
2561 {
2562 int ret;
2563
2564 ret = usbdev_do_ioctl(file, cmd, compat_ptr(arg));
2565
2566 return ret;
2567 }
2568 #endif
2569
2570 /* No kernel lock - fine */
2571 static unsigned int usbdev_poll(struct file *file,
2572 struct poll_table_struct *wait)
2573 {
2574 struct usb_dev_state *ps = file->private_data;
2575 unsigned int mask = 0;
2576
2577 poll_wait(file, &ps->wait, wait);
2578 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed))
2579 mask |= POLLOUT | POLLWRNORM;
2580 if (!connected(ps))
2581 mask |= POLLHUP;
2582 if (list_empty(&ps->list))
2583 mask |= POLLERR;
2584 return mask;
2585 }
2586
2587 const struct file_operations usbdev_file_operations = {
2588 .owner = THIS_MODULE,
2589 .llseek = no_seek_end_llseek,
2590 .read = usbdev_read,
2591 .poll = usbdev_poll,
2592 .unlocked_ioctl = usbdev_ioctl,
2593 #ifdef CONFIG_COMPAT
2594 .compat_ioctl = usbdev_compat_ioctl,
2595 #endif
2596 .mmap = usbdev_mmap,
2597 .open = usbdev_open,
2598 .release = usbdev_release,
2599 };
2600
2601 static void usbdev_remove(struct usb_device *udev)
2602 {
2603 struct usb_dev_state *ps;
2604 struct siginfo sinfo;
2605
2606 while (!list_empty(&udev->filelist)) {
2607 ps = list_entry(udev->filelist.next, struct usb_dev_state, list);
2608 destroy_all_async(ps);
2609 wake_up_all(&ps->wait);
2610 list_del_init(&ps->list);
2611 if (ps->discsignr) {
2612 memset(&sinfo, 0, sizeof(sinfo));
2613 sinfo.si_signo = ps->discsignr;
2614 sinfo.si_errno = EPIPE;
2615 sinfo.si_code = SI_ASYNCIO;
2616 sinfo.si_addr = ps->disccontext;
2617 kill_pid_info_as_cred(ps->discsignr, &sinfo,
2618 ps->disc_pid, ps->cred, ps->secid);
2619 }
2620 }
2621 }
2622
2623 static int usbdev_notify(struct notifier_block *self,
2624 unsigned long action, void *dev)
2625 {
2626 switch (action) {
2627 case USB_DEVICE_ADD:
2628 break;
2629 case USB_DEVICE_REMOVE:
2630 usbdev_remove(dev);
2631 break;
2632 }
2633 return NOTIFY_OK;
2634 }
2635
2636 static struct notifier_block usbdev_nb = {
2637 .notifier_call = usbdev_notify,
2638 };
2639
2640 static struct cdev usb_device_cdev;
2641
2642 int __init usb_devio_init(void)
2643 {
2644 int retval;
2645
2646 retval = register_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX,
2647 "usb_device");
2648 if (retval) {
2649 printk(KERN_ERR "Unable to register minors for usb_device\n");
2650 goto out;
2651 }
2652 cdev_init(&usb_device_cdev, &usbdev_file_operations);
2653 retval = cdev_add(&usb_device_cdev, USB_DEVICE_DEV, USB_DEVICE_MAX);
2654 if (retval) {
2655 printk(KERN_ERR "Unable to get usb_device major %d\n",
2656 USB_DEVICE_MAJOR);
2657 goto error_cdev;
2658 }
2659 usb_register_notify(&usbdev_nb);
2660 out:
2661 return retval;
2662
2663 error_cdev:
2664 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2665 goto out;
2666 }
2667
2668 void usb_devio_cleanup(void)
2669 {
2670 usb_unregister_notify(&usbdev_nb);
2671 cdev_del(&usb_device_cdev);
2672 unregister_chrdev_region(USB_DEVICE_DEV, USB_DEVICE_MAX);
2673 }