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