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