Lines Matching +full:ps +full:- +full:speed
1 // SPDX-License-Identifier: GPL-2.0+
5 * devio.c -- User space communication with USB devices.
7 * Copyright (C) 1999-2000 Thomas Sailer (sailer@ife.ee.ethz.ch)
18 * 30.09.2005 0.3 Fix user-triggerable oops in async URB delivery
19 * (CAN-2005-3055)
42 #include <linux/dma-mapping.h>
56 #define USB_SG_SIZE 16384 /* split-size for large txs */
58 /* Mutual exclusion for ps->list in resume vs. release and remove */
91 struct usb_dev_state *ps; member
96 struct usb_dev_state *ps; member
140 #define USBFS_XFER_MAX (UINT_MAX / 2 - 1000000)
159 ret = -ENOMEM; in usbfs_increase_memory_usage()
176 usbfs_memory_usage -= amount; in usbfs_decrease_memory_usage()
180 static int connected(struct usb_dev_state *ps) in connected() argument
182 return (!list_empty(&ps->list) && in connected()
183 ps->dev->state != USB_STATE_NOTATTACHED); in connected()
188 struct usb_dev_state *ps = usbm->ps; in dec_usb_memory_use_count() local
189 struct usb_hcd *hcd = bus_to_hcd(ps->dev->bus); in dec_usb_memory_use_count()
192 spin_lock_irqsave(&ps->lock, flags); in dec_usb_memory_use_count()
193 --*count; in dec_usb_memory_use_count()
194 if (usbm->urb_use_count == 0 && usbm->vma_use_count == 0) { in dec_usb_memory_use_count()
195 list_del(&usbm->memlist); in dec_usb_memory_use_count()
196 spin_unlock_irqrestore(&ps->lock, flags); in dec_usb_memory_use_count()
198 hcd_buffer_free_pages(hcd, usbm->size, in dec_usb_memory_use_count()
199 usbm->mem, usbm->dma_handle); in dec_usb_memory_use_count()
201 usbm->size + sizeof(struct usb_memory)); in dec_usb_memory_use_count()
204 spin_unlock_irqrestore(&ps->lock, flags); in dec_usb_memory_use_count()
210 struct usb_memory *usbm = vma->vm_private_data; in usbdev_vm_open()
213 spin_lock_irqsave(&usbm->ps->lock, flags); in usbdev_vm_open()
214 ++usbm->vma_use_count; in usbdev_vm_open()
215 spin_unlock_irqrestore(&usbm->ps->lock, flags); in usbdev_vm_open()
220 struct usb_memory *usbm = vma->vm_private_data; in usbdev_vm_close()
222 dec_usb_memory_use_count(usbm, &usbm->vma_use_count); in usbdev_vm_close()
233 struct usb_dev_state *ps = file->private_data; in usbdev_mmap() local
234 struct usb_hcd *hcd = bus_to_hcd(ps->dev->bus); in usbdev_mmap()
235 size_t size = vma->vm_end - vma->vm_start; in usbdev_mmap()
247 ret = -ENOMEM; in usbdev_mmap()
254 ret = -ENOMEM; in usbdev_mmap()
260 usbm->mem = mem; in usbdev_mmap()
261 usbm->dma_handle = dma_handle; in usbdev_mmap()
262 usbm->size = size; in usbdev_mmap()
263 usbm->ps = ps; in usbdev_mmap()
264 usbm->vm_start = vma->vm_start; in usbdev_mmap()
265 usbm->vma_use_count = 1; in usbdev_mmap()
266 INIT_LIST_HEAD(&usbm->memlist); in usbdev_mmap()
269 * In DMA-unavailable cases, hcd_buffer_alloc_pages allocates in usbdev_mmap()
276 if (remap_pfn_range(vma, vma->vm_start, in usbdev_mmap()
277 virt_to_phys(usbm->mem) >> PAGE_SHIFT, in usbdev_mmap()
278 size, vma->vm_page_prot) < 0) { in usbdev_mmap()
279 dec_usb_memory_use_count(usbm, &usbm->vma_use_count); in usbdev_mmap()
280 return -EAGAIN; in usbdev_mmap()
283 if (dma_mmap_coherent(hcd->self.sysdev, vma, mem, dma_handle, in usbdev_mmap()
285 dec_usb_memory_use_count(usbm, &usbm->vma_use_count); in usbdev_mmap()
286 return -EAGAIN; in usbdev_mmap()
291 vma->vm_ops = &usbdev_vm_ops; in usbdev_mmap()
292 vma->vm_private_data = usbm; in usbdev_mmap()
294 spin_lock_irqsave(&ps->lock, flags); in usbdev_mmap()
295 list_add_tail(&usbm->memlist, &ps->memory_list); in usbdev_mmap()
296 spin_unlock_irqrestore(&ps->lock, flags); in usbdev_mmap()
311 struct usb_dev_state *ps = file->private_data; in usbdev_read() local
312 struct usb_device *dev = ps->dev; in usbdev_read()
320 if (!connected(ps)) { in usbdev_read()
321 ret = -ENODEV; in usbdev_read()
324 ret = -EINVAL; in usbdev_read()
329 /* 18 bytes - fits on the stack */ in usbdev_read()
332 memcpy(&temp_desc, &dev->descriptor, sizeof(dev->descriptor)); in usbdev_read()
338 len = sizeof(struct usb_device_descriptor) - pos; in usbdev_read()
342 ret = -EFAULT; in usbdev_read()
348 nbytes -= len; in usbdev_read()
353 for (i = 0; nbytes && i < dev->descriptor.bNumConfigurations; i++) { in usbdev_read()
355 (struct usb_config_descriptor *)dev->rawdescriptors[i]; in usbdev_read()
356 unsigned int length = le16_to_cpu(config->wTotalLength); in usbdev_read()
363 le16_to_cpu(dev->config[i].desc.wTotalLength); in usbdev_read()
365 len = length - (*ppos - pos); in usbdev_read()
370 if (alloclen > (*ppos - pos)) { in usbdev_read()
371 alloclen -= (*ppos - pos); in usbdev_read()
373 dev->rawdescriptors[i] + (*ppos - pos), in usbdev_read()
375 ret = -EFAULT; in usbdev_read()
382 nbytes -= len; in usbdev_read()
405 as->urb = usb_alloc_urb(numisoframes, GFP_KERNEL); in alloc_async()
406 if (!as->urb) { in alloc_async()
417 put_pid(as->pid); in free_async()
418 if (as->cred) in free_async()
419 put_cred(as->cred); in free_async()
420 for (i = 0; i < as->urb->num_sgs; i++) { in free_async()
421 if (sg_page(&as->urb->sg[i])) in free_async()
422 kfree(sg_virt(&as->urb->sg[i])); in free_async()
425 kfree(as->urb->sg); in free_async()
426 if (as->usbm == NULL) in free_async()
427 kfree(as->urb->transfer_buffer); in free_async()
429 dec_usb_memory_use_count(as->usbm, &as->usbm->urb_use_count); in free_async()
431 kfree(as->urb->setup_packet); in free_async()
432 usb_free_urb(as->urb); in free_async()
433 usbfs_decrease_memory_usage(as->mem_usage); in free_async()
439 struct usb_dev_state *ps = as->ps; in async_newpending() local
442 spin_lock_irqsave(&ps->lock, flags); in async_newpending()
443 list_add_tail(&as->asynclist, &ps->async_pending); in async_newpending()
444 spin_unlock_irqrestore(&ps->lock, flags); in async_newpending()
449 struct usb_dev_state *ps = as->ps; in async_removepending() local
452 spin_lock_irqsave(&ps->lock, flags); in async_removepending()
453 list_del_init(&as->asynclist); in async_removepending()
454 spin_unlock_irqrestore(&ps->lock, flags); in async_removepending()
457 static struct async *async_getcompleted(struct usb_dev_state *ps) in async_getcompleted() argument
462 spin_lock_irqsave(&ps->lock, flags); in async_getcompleted()
463 if (!list_empty(&ps->async_completed)) { in async_getcompleted()
464 as = list_entry(ps->async_completed.next, struct async, in async_getcompleted()
466 list_del_init(&as->asynclist); in async_getcompleted()
468 spin_unlock_irqrestore(&ps->lock, flags); in async_getcompleted()
472 static struct async *async_getpending(struct usb_dev_state *ps, in async_getpending() argument
477 list_for_each_entry(as, &ps->async_pending, asynclist) in async_getpending()
478 if (as->userurb == userurb) { in async_getpending()
479 list_del_init(&as->asynclist); in async_getpending()
505 dev_info(&udev->dev, "userurb %px, ep%d %s-%s, " in snoop_urb()
509 dev_info(&udev->dev, "userurb %px, ep%d %s-%s, " in snoop_urb()
515 dev_info(&udev->dev, "ep%d %s-%s, length %u, " in snoop_urb()
519 dev_info(&udev->dev, "ep%d %s-%s, actual_length %u, " in snoop_urb()
539 if (urb->num_sgs == 0) { in snoop_urb_data()
541 urb->transfer_buffer, len, 1); in snoop_urb_data()
545 for (i = 0; i < urb->num_sgs && len; i++) { in snoop_urb_data()
548 sg_virt(&urb->sg[i]), size, 1); in snoop_urb_data()
549 len -= size; in snoop_urb_data()
557 if (urb->number_of_packets > 0) /* Isochronous */ in copy_urb_data_to_user()
558 len = urb->transfer_buffer_length; in copy_urb_data_to_user()
559 else /* Non-Isoc */ in copy_urb_data_to_user()
560 len = urb->actual_length; in copy_urb_data_to_user()
562 if (urb->num_sgs == 0) { in copy_urb_data_to_user()
563 if (copy_to_user(userbuffer, urb->transfer_buffer, len)) in copy_urb_data_to_user()
564 return -EFAULT; in copy_urb_data_to_user()
568 for (i = 0; i < urb->num_sgs && len; i++) { in copy_urb_data_to_user()
570 if (copy_to_user(userbuffer, sg_virt(&urb->sg[i]), size)) in copy_urb_data_to_user()
571 return -EFAULT; in copy_urb_data_to_user()
573 len -= size; in copy_urb_data_to_user()
582 static void cancel_bulk_urbs(struct usb_dev_state *ps, unsigned bulk_addr) in cancel_bulk_urbs() argument
583 __releases(ps->lock) in cancel_bulk_urbs()
584 __acquires(ps->lock) in cancel_bulk_urbs()
594 list_for_each_entry(as, &ps->async_pending, asynclist) { in cancel_bulk_urbs()
595 if (as->bulk_addr == bulk_addr) { in cancel_bulk_urbs()
596 if (as->bulk_status != AS_CONTINUATION) in cancel_bulk_urbs()
598 as->bulk_status = AS_UNLINK; in cancel_bulk_urbs()
599 as->bulk_addr = 0; in cancel_bulk_urbs()
602 ps->disabled_bulk_eps |= (1 << bulk_addr); in cancel_bulk_urbs()
606 list_for_each_entry_reverse(as, &ps->async_pending, asynclist) { in cancel_bulk_urbs()
607 if (as->bulk_status == AS_UNLINK) { in cancel_bulk_urbs()
608 as->bulk_status = 0; /* Only once */ in cancel_bulk_urbs()
609 urb = as->urb; in cancel_bulk_urbs()
611 spin_unlock(&ps->lock); /* Allow completions */ in cancel_bulk_urbs()
614 spin_lock(&ps->lock); in cancel_bulk_urbs()
622 struct async *as = urb->context; in async_completed()
623 struct usb_dev_state *ps = as->ps; in async_completed() local
630 spin_lock_irqsave(&ps->lock, flags); in async_completed()
631 list_move_tail(&as->asynclist, &ps->async_completed); in async_completed()
632 as->status = urb->status; in async_completed()
633 signr = as->signr; in async_completed()
635 errno = as->status; in async_completed()
636 addr = as->userurb_sigval; in async_completed()
637 pid = get_pid(as->pid); in async_completed()
638 cred = get_cred(as->cred); in async_completed()
640 snoop(&urb->dev->dev, "urb complete\n"); in async_completed()
641 snoop_urb(urb->dev, as->userurb, urb->pipe, urb->actual_length, in async_completed()
642 as->status, COMPLETE, NULL, 0); in async_completed()
644 snoop_urb_data(urb, urb->actual_length); in async_completed()
646 if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET && in async_completed()
647 as->status != -ENOENT) in async_completed()
648 cancel_bulk_urbs(ps, as->bulk_addr); in async_completed()
650 wake_up(&ps->wait); in async_completed()
651 spin_unlock_irqrestore(&ps->lock, flags); in async_completed()
660 static void destroy_async(struct usb_dev_state *ps, struct list_head *list) in destroy_async() argument
666 spin_lock_irqsave(&ps->lock, flags); in destroy_async()
669 list_del_init(&as->asynclist); in destroy_async()
670 urb = as->urb; in destroy_async()
674 spin_unlock_irqrestore(&ps->lock, flags); in destroy_async()
677 spin_lock_irqsave(&ps->lock, flags); in destroy_async()
679 spin_unlock_irqrestore(&ps->lock, flags); in destroy_async()
682 static void destroy_async_on_interface(struct usb_dev_state *ps, in destroy_async_on_interface() argument
689 spin_lock_irqsave(&ps->lock, flags); in destroy_async_on_interface()
690 list_for_each_safe(p, q, &ps->async_pending) in destroy_async_on_interface()
691 if (ifnum == list_entry(p, struct async, asynclist)->ifnum) in destroy_async_on_interface()
693 spin_unlock_irqrestore(&ps->lock, flags); in destroy_async_on_interface()
694 destroy_async(ps, &hitlist); in destroy_async_on_interface()
697 static void destroy_all_async(struct usb_dev_state *ps) in destroy_all_async() argument
699 destroy_async(ps, &ps->async_pending); in destroy_all_async()
711 return -ENODEV; in driver_probe()
716 struct usb_dev_state *ps = usb_get_intfdata(intf); in driver_disconnect() local
717 unsigned int ifnum = intf->altsetting->desc.bInterfaceNumber; in driver_disconnect()
719 if (!ps) in driver_disconnect()
726 if (likely(ifnum < 8*sizeof(ps->ifclaimed))) in driver_disconnect()
727 clear_bit(ifnum, &ps->ifclaimed); in driver_disconnect()
729 dev_warn(&intf->dev, "interface number %u out of range\n", in driver_disconnect()
735 destroy_async_on_interface(ps, ifnum); in driver_disconnect()
758 struct usb_dev_state *ps; in usbfs_notify_resume() local
762 list_for_each_entry(ps, &udev->filelist, list) { in usbfs_notify_resume()
763 WRITE_ONCE(ps->not_yet_resumed, 0); in usbfs_notify_resume()
764 wake_up_all(&ps->wait_for_resume); in usbfs_notify_resume()
779 static int claimintf(struct usb_dev_state *ps, unsigned int ifnum) in claimintf() argument
781 struct usb_device *dev = ps->dev; in claimintf()
785 if (ifnum >= 8*sizeof(ps->ifclaimed)) in claimintf()
786 return -EINVAL; in claimintf()
788 if (test_bit(ifnum, &ps->ifclaimed)) in claimintf()
791 if (ps->privileges_dropped && in claimintf()
792 !test_bit(ifnum, &ps->interface_allowed_mask)) in claimintf()
793 return -EACCES; in claimintf()
797 err = -ENOENT; in claimintf()
802 old_suppress = dev_get_uevent_suppress(&intf->dev); in claimintf()
803 dev_set_uevent_suppress(&intf->dev, 1); in claimintf()
804 err = usb_driver_claim_interface(&usbfs_driver, intf, ps); in claimintf()
805 dev_set_uevent_suppress(&intf->dev, old_suppress); in claimintf()
808 set_bit(ifnum, &ps->ifclaimed); in claimintf()
812 static int releaseintf(struct usb_dev_state *ps, unsigned int ifnum) in releaseintf() argument
818 err = -EINVAL; in releaseintf()
819 if (ifnum >= 8*sizeof(ps->ifclaimed)) in releaseintf()
821 dev = ps->dev; in releaseintf()
824 err = -ENOENT; in releaseintf()
825 else if (test_and_clear_bit(ifnum, &ps->ifclaimed)) { in releaseintf()
829 old_suppress = dev_get_uevent_suppress(&intf->dev); in releaseintf()
830 dev_set_uevent_suppress(&intf->dev, 1); in releaseintf()
832 dev_set_uevent_suppress(&intf->dev, old_suppress); in releaseintf()
838 static int checkintf(struct usb_dev_state *ps, unsigned int ifnum) in checkintf() argument
840 if (ps->dev->state != USB_STATE_CONFIGURED) in checkintf()
841 return -EHOSTUNREACH; in checkintf()
842 if (ifnum >= 8*sizeof(ps->ifclaimed)) in checkintf()
843 return -EINVAL; in checkintf()
844 if (test_bit(ifnum, &ps->ifclaimed)) in checkintf()
847 dev_warn(&ps->dev->dev, "usbfs: process %d (%s) did not claim " in checkintf()
849 current->comm, ifnum); in checkintf()
850 return claimintf(ps, ifnum); in checkintf()
861 return -EINVAL; in findintfep()
862 if (!dev->actconfig) in findintfep()
863 return -ESRCH; in findintfep()
864 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) { in findintfep()
865 intf = dev->actconfig->interface[i]; in findintfep()
866 for (j = 0; j < intf->num_altsetting; j++) { in findintfep()
867 alts = &intf->altsetting[j]; in findintfep()
868 for (e = 0; e < alts->desc.bNumEndpoints; e++) { in findintfep()
869 endpt = &alts->endpoint[e].desc; in findintfep()
870 if (endpt->bEndpointAddress == ep) in findintfep()
871 return alts->desc.bInterfaceNumber; in findintfep()
875 return -ENOENT; in findintfep()
878 static int check_ctrlrecip(struct usb_dev_state *ps, unsigned int requesttype, in check_ctrlrecip() argument
884 if (ps->dev->state != USB_STATE_UNAUTHENTICATED in check_ctrlrecip()
885 && ps->dev->state != USB_STATE_ADDRESS in check_ctrlrecip()
886 && ps->dev->state != USB_STATE_CONFIGURED) in check_ctrlrecip()
887 return -EHOSTUNREACH; in check_ctrlrecip()
897 alt_setting = usb_find_alt_setting(ps->dev->actconfig, in check_ctrlrecip()
900 && alt_setting->desc.bInterfaceClass == USB_CLASS_PRINTER) in check_ctrlrecip()
909 ret = findintfep(ps->dev, index); in check_ctrlrecip()
919 ret = findintfep(ps->dev, index ^ 0x80); in check_ctrlrecip()
921 dev_info(&ps->dev->dev, in check_ctrlrecip()
924 current->comm, index, index ^ 0x80); in check_ctrlrecip()
927 ret = checkintf(ps, ret); in check_ctrlrecip()
931 ret = checkintf(ps, index); in check_ctrlrecip()
941 return dev->ep_in[ep & USB_ENDPOINT_NUMBER_MASK]; in ep_to_host_endpoint()
943 return dev->ep_out[ep & USB_ENDPOINT_NUMBER_MASK]; in ep_to_host_endpoint()
946 static int parse_usbdevfs_streams(struct usb_dev_state *ps, in parse_usbdevfs_streams() argument
959 if (get_user(num_streams, &streams->num_streams) || in parse_usbdevfs_streams()
960 get_user(num_eps, &streams->num_eps)) in parse_usbdevfs_streams()
961 return -EFAULT; in parse_usbdevfs_streams()
964 return -EINVAL; in parse_usbdevfs_streams()
968 return -EINVAL; in parse_usbdevfs_streams()
972 return -ENOMEM; in parse_usbdevfs_streams()
975 if (get_user(ep, &streams->eps[i])) { in parse_usbdevfs_streams()
976 ret = -EFAULT; in parse_usbdevfs_streams()
979 eps[i] = ep_to_host_endpoint(ps->dev, ep); in parse_usbdevfs_streams()
981 ret = -EINVAL; in parse_usbdevfs_streams()
986 ifnum = findintfep(ps->dev, ep); in parse_usbdevfs_streams()
993 ret = checkintf(ps, ifnum); in parse_usbdevfs_streams()
996 intf = usb_ifnum_to_if(ps->dev, ifnum); in parse_usbdevfs_streams()
999 if (ifnum != intf->altsetting->desc.bInterfaceNumber) { in parse_usbdevfs_streams()
1000 ret = -EINVAL; in parse_usbdevfs_streams()
1035 struct usb_dev_state *ps; in usbdev_open() local
1038 ret = -ENOMEM; in usbdev_open()
1039 ps = kzalloc(sizeof(struct usb_dev_state), GFP_KERNEL); in usbdev_open()
1040 if (!ps) in usbdev_open()
1043 ret = -ENODEV; in usbdev_open()
1045 /* usbdev device-node */ in usbdev_open()
1047 dev = usbdev_lookup_by_devt(inode->i_rdev); in usbdev_open()
1052 if (dev->state == USB_STATE_NOTATTACHED) in usbdev_open()
1059 ps->dev = dev; in usbdev_open()
1060 ps->file = file; in usbdev_open()
1061 ps->interface_allowed_mask = 0xFFFFFFFF; /* 32 bits */ in usbdev_open()
1062 spin_lock_init(&ps->lock); in usbdev_open()
1063 INIT_LIST_HEAD(&ps->list); in usbdev_open()
1064 INIT_LIST_HEAD(&ps->async_pending); in usbdev_open()
1065 INIT_LIST_HEAD(&ps->async_completed); in usbdev_open()
1066 INIT_LIST_HEAD(&ps->memory_list); in usbdev_open()
1067 init_waitqueue_head(&ps->wait); in usbdev_open()
1068 init_waitqueue_head(&ps->wait_for_resume); in usbdev_open()
1069 ps->disc_pid = get_pid(task_pid(current)); in usbdev_open()
1070 ps->cred = get_current_cred(); in usbdev_open()
1074 list_add_tail(&ps->list, &dev->filelist); in usbdev_open()
1075 file->private_data = ps; in usbdev_open()
1077 snoop(&dev->dev, "opened by process %d: %s\n", task_pid_nr(current), in usbdev_open()
1078 current->comm); in usbdev_open()
1085 kfree(ps); in usbdev_open()
1091 struct usb_dev_state *ps = file->private_data; in usbdev_release() local
1092 struct usb_device *dev = ps->dev; in usbdev_release()
1097 usb_hub_release_all_ports(dev, ps); in usbdev_release()
1101 list_del_init(&ps->list); in usbdev_release()
1104 for (ifnum = 0; ps->ifclaimed && ifnum < 8*sizeof(ps->ifclaimed); in usbdev_release()
1106 if (test_bit(ifnum, &ps->ifclaimed)) in usbdev_release()
1107 releaseintf(ps, ifnum); in usbdev_release()
1109 destroy_all_async(ps); in usbdev_release()
1110 if (!ps->suspend_allowed) in usbdev_release()
1114 put_pid(ps->disc_pid); in usbdev_release()
1115 put_cred(ps->cred); in usbdev_release()
1117 as = async_getcompleted(ps); in usbdev_release()
1120 as = async_getcompleted(ps); in usbdev_release()
1123 kfree(ps); in usbdev_release()
1129 complete((struct completion *) urb->context); in usbfs_blocking_completion()
1143 urb->context = &ctx; in usbfs_start_wait_urb()
1144 urb->complete = usbfs_blocking_completion; in usbfs_start_wait_urb()
1154 *actlen = urb->actual_length; in usbfs_start_wait_urb()
1155 if (urb->status != -ENOENT) in usbfs_start_wait_urb()
1158 return -EINTR; in usbfs_start_wait_urb()
1160 return -ETIMEDOUT; in usbfs_start_wait_urb()
1162 *actlen = urb->actual_length; in usbfs_start_wait_urb()
1163 return urb->status; in usbfs_start_wait_urb()
1166 static int do_proc_control(struct usb_dev_state *ps, in do_proc_control() argument
1169 struct usb_device *dev = ps->dev; in do_proc_control()
1177 ret = check_ctrlrecip(ps, ctrl->bRequestType, ctrl->bRequest, in do_proc_control()
1178 ctrl->wIndex); in do_proc_control()
1181 wLength = ctrl->wLength; /* To suppress 64k PAGE_SIZE warning */ in do_proc_control()
1183 return -EINVAL; in do_proc_control()
1189 ret = -ENOMEM; in do_proc_control()
1200 dr->bRequestType = ctrl->bRequestType; in do_proc_control()
1201 dr->bRequest = ctrl->bRequest; in do_proc_control()
1202 dr->wValue = cpu_to_le16(ctrl->wValue); in do_proc_control()
1203 dr->wIndex = cpu_to_le16(ctrl->wIndex); in do_proc_control()
1204 dr->wLength = cpu_to_le16(ctrl->wLength); in do_proc_control()
1206 tmo = ctrl->timeout; in do_proc_control()
1207 snoop(&dev->dev, "control urb: bRequestType=%02x " in do_proc_control()
1210 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue, in do_proc_control()
1211 ctrl->wIndex, ctrl->wLength); in do_proc_control()
1213 if ((ctrl->bRequestType & USB_DIR_IN) && wLength) { in do_proc_control()
1223 if (dev->quirks & USB_QUIRK_DELAY_CTRL_MSG) in do_proc_control()
1228 if (copy_to_user(ctrl->data, tbuf, actlen)) { in do_proc_control()
1229 ret = -EFAULT; in do_proc_control()
1235 if (copy_from_user(tbuf, ctrl->data, wLength)) { in do_proc_control()
1236 ret = -EFAULT; in do_proc_control()
1249 if (dev->quirks & USB_QUIRK_DELAY_CTRL_MSG) in do_proc_control()
1254 if (i < 0 && i != -EPIPE) { in do_proc_control()
1255 dev_printk(KERN_DEBUG, &dev->dev, "usbfs: USBDEVFS_CONTROL " in do_proc_control()
1257 current->comm, ctrl->bRequestType, ctrl->bRequest, in do_proc_control()
1258 ctrl->wLength, i); in do_proc_control()
1271 static int proc_control(struct usb_dev_state *ps, void __user *arg) in proc_control() argument
1276 return -EFAULT; in proc_control()
1277 return do_proc_control(ps, &ctrl); in proc_control()
1280 static int do_proc_bulk(struct usb_dev_state *ps, in do_proc_bulk() argument
1283 struct usb_device *dev = ps->dev; in do_proc_bulk()
1290 ret = findintfep(ps->dev, bulk->ep); in do_proc_bulk()
1293 ret = checkintf(ps, ret); in do_proc_bulk()
1297 len1 = bulk->len; in do_proc_bulk()
1298 if (len1 < 0 || len1 >= (INT_MAX - sizeof(struct urb))) in do_proc_bulk()
1299 return -EINVAL; in do_proc_bulk()
1301 if (bulk->ep & USB_DIR_IN) in do_proc_bulk()
1302 pipe = usb_rcvbulkpipe(dev, bulk->ep & 0x7f); in do_proc_bulk()
1304 pipe = usb_sndbulkpipe(dev, bulk->ep & 0x7f); in do_proc_bulk()
1306 if (!ep || !usb_endpoint_maxp(&ep->desc)) in do_proc_bulk()
1307 return -EINVAL; in do_proc_bulk()
1316 ret = -ENOMEM; in do_proc_bulk()
1324 if ((ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == in do_proc_bulk()
1328 NULL, NULL, ep->desc.bInterval); in do_proc_bulk()
1333 tmo = bulk->timeout; in do_proc_bulk()
1334 if (bulk->ep & 0x80) { in do_proc_bulk()
1343 if (copy_to_user(bulk->data, tbuf, len2)) { in do_proc_bulk()
1344 ret = -EFAULT; in do_proc_bulk()
1350 if (copy_from_user(tbuf, bulk->data, len1)) { in do_proc_bulk()
1351 ret = -EFAULT; in do_proc_bulk()
1370 static int proc_bulk(struct usb_dev_state *ps, void __user *arg) in proc_bulk() argument
1375 return -EFAULT; in proc_bulk()
1376 return do_proc_bulk(ps, &bulk); in proc_bulk()
1385 eps = (epnum & USB_DIR_IN) ? udev->ep_in : udev->ep_out; in check_reset_of_active_ep()
1387 if (ep && !list_empty(&ep->urb_list)) in check_reset_of_active_ep()
1388 dev_warn(&udev->dev, "Process %d (%s) called USBDEVFS_%s for active endpoint 0x%02x\n", in check_reset_of_active_ep()
1389 task_pid_nr(current), current->comm, in check_reset_of_active_ep()
1393 static int proc_resetep(struct usb_dev_state *ps, void __user *arg) in proc_resetep() argument
1399 return -EFAULT; in proc_resetep()
1400 ret = findintfep(ps->dev, ep); in proc_resetep()
1403 ret = checkintf(ps, ret); in proc_resetep()
1406 check_reset_of_active_ep(ps->dev, ep, "RESETEP"); in proc_resetep()
1407 usb_reset_endpoint(ps->dev, ep); in proc_resetep()
1411 static int proc_clearhalt(struct usb_dev_state *ps, void __user *arg) in proc_clearhalt() argument
1418 return -EFAULT; in proc_clearhalt()
1419 ret = findintfep(ps->dev, ep); in proc_clearhalt()
1422 ret = checkintf(ps, ret); in proc_clearhalt()
1425 check_reset_of_active_ep(ps->dev, ep, "CLEAR_HALT"); in proc_clearhalt()
1427 pipe = usb_rcvbulkpipe(ps->dev, ep & 0x7f); in proc_clearhalt()
1429 pipe = usb_sndbulkpipe(ps->dev, ep & 0x7f); in proc_clearhalt()
1431 return usb_clear_halt(ps->dev, pipe); in proc_clearhalt()
1434 static int proc_getdriver(struct usb_dev_state *ps, void __user *arg) in proc_getdriver() argument
1441 return -EFAULT; in proc_getdriver()
1442 intf = usb_ifnum_to_if(ps->dev, gd.interface); in proc_getdriver()
1443 if (!intf || !intf->dev.driver) in proc_getdriver()
1444 ret = -ENODATA; in proc_getdriver()
1446 strscpy(gd.driver, intf->dev.driver->name, in proc_getdriver()
1448 ret = (copy_to_user(arg, &gd, sizeof(gd)) ? -EFAULT : 0); in proc_getdriver()
1453 static int proc_connectinfo(struct usb_dev_state *ps, void __user *arg) in proc_connectinfo() argument
1458 ci.devnum = ps->dev->devnum; in proc_connectinfo()
1459 ci.slow = ps->dev->speed == USB_SPEED_LOW; in proc_connectinfo()
1462 return -EFAULT; in proc_connectinfo()
1466 static int proc_conninfo_ex(struct usb_dev_state *ps, in proc_conninfo_ex() argument
1470 struct usb_device *udev = ps->dev; in proc_conninfo_ex()
1473 return -EINVAL; in proc_conninfo_ex()
1477 ci.busnum = udev->bus->busnum; in proc_conninfo_ex()
1478 ci.devnum = udev->devnum; in proc_conninfo_ex()
1479 ci.speed = udev->speed; in proc_conninfo_ex()
1481 while (udev && udev->portnum != 0) { in proc_conninfo_ex()
1483 ci.ports[ARRAY_SIZE(ci.ports) - ci.num_ports] = in proc_conninfo_ex()
1484 udev->portnum; in proc_conninfo_ex()
1485 udev = udev->parent; in proc_conninfo_ex()
1490 &ci.ports[ARRAY_SIZE(ci.ports) - ci.num_ports], in proc_conninfo_ex()
1494 return -EFAULT; in proc_conninfo_ex()
1499 static int proc_resetdevice(struct usb_dev_state *ps) in proc_resetdevice() argument
1501 struct usb_host_config *actconfig = ps->dev->actconfig; in proc_resetdevice()
1509 if (ps->privileges_dropped && actconfig) { in proc_resetdevice()
1510 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { in proc_resetdevice()
1511 interface = actconfig->interface[i]; in proc_resetdevice()
1512 number = interface->cur_altsetting->desc.bInterfaceNumber; in proc_resetdevice()
1514 !test_bit(number, &ps->ifclaimed)) { in proc_resetdevice()
1515 dev_warn(&ps->dev->dev, in proc_resetdevice()
1517 number, interface->dev.driver->name, current->comm); in proc_resetdevice()
1518 return -EACCES; in proc_resetdevice()
1523 return usb_reset_device(ps->dev); in proc_resetdevice()
1526 static int proc_setintf(struct usb_dev_state *ps, void __user *arg) in proc_setintf() argument
1532 return -EFAULT; in proc_setintf()
1533 ret = checkintf(ps, setintf.interface); in proc_setintf()
1537 destroy_async_on_interface(ps, setintf.interface); in proc_setintf()
1539 return usb_set_interface(ps->dev, setintf.interface, in proc_setintf()
1543 static int proc_setconfig(struct usb_dev_state *ps, void __user *arg) in proc_setconfig() argument
1550 return -EFAULT; in proc_setconfig()
1552 actconfig = ps->dev->actconfig; in proc_setconfig()
1561 for (i = 0; i < actconfig->desc.bNumInterfaces; ++i) { in proc_setconfig()
1562 if (usb_interface_claimed(actconfig->interface[i])) { in proc_setconfig()
1563 dev_warn(&ps->dev->dev, in proc_setconfig()
1566 actconfig->interface[i] in proc_setconfig()
1567 ->cur_altsetting in proc_setconfig()
1568 ->desc.bInterfaceNumber, in proc_setconfig()
1569 actconfig->interface[i] in proc_setconfig()
1570 ->dev.driver->name, in proc_setconfig()
1571 current->comm, u); in proc_setconfig()
1572 status = -EBUSY; in proc_setconfig()
1582 if (actconfig && actconfig->desc.bConfigurationValue == u) in proc_setconfig()
1583 status = usb_reset_configuration(ps->dev); in proc_setconfig()
1585 status = usb_set_configuration(ps->dev, u); in proc_setconfig()
1592 find_memory_area(struct usb_dev_state *ps, const struct usbdevfs_urb *uurb) in find_memory_area() argument
1596 unsigned long uurb_start = (unsigned long)uurb->buffer; in find_memory_area()
1598 spin_lock_irqsave(&ps->lock, flags); in find_memory_area()
1599 list_for_each_entry(iter, &ps->memory_list, memlist) { in find_memory_area()
1600 if (uurb_start >= iter->vm_start && in find_memory_area()
1601 uurb_start < iter->vm_start + iter->size) { in find_memory_area()
1602 if (uurb->buffer_length > iter->vm_start + iter->size - in find_memory_area()
1604 usbm = ERR_PTR(-EINVAL); in find_memory_area()
1607 usbm->urb_use_count++; in find_memory_area()
1612 spin_unlock_irqrestore(&ps->lock, flags); in find_memory_area()
1616 static int proc_do_submiturb(struct usb_dev_state *ps, struct usbdevfs_urb *uurb, in proc_do_submiturb() argument
1625 int i, ret, num_sgs = 0, ifnum = -1; in proc_do_submiturb()
1638 if (uurb->type == USBDEVFS_URB_TYPE_ISO) in proc_do_submiturb()
1641 if (uurb->flags & ~mask) in proc_do_submiturb()
1642 return -EINVAL; in proc_do_submiturb()
1644 if ((unsigned int)uurb->buffer_length >= USBFS_XFER_MAX) in proc_do_submiturb()
1645 return -EINVAL; in proc_do_submiturb()
1646 if (uurb->buffer_length > 0 && !uurb->buffer) in proc_do_submiturb()
1647 return -EINVAL; in proc_do_submiturb()
1648 if (!(uurb->type == USBDEVFS_URB_TYPE_CONTROL && in proc_do_submiturb()
1649 (uurb->endpoint & ~USB_ENDPOINT_DIR_MASK) == 0)) { in proc_do_submiturb()
1650 ifnum = findintfep(ps->dev, uurb->endpoint); in proc_do_submiturb()
1653 ret = checkintf(ps, ifnum); in proc_do_submiturb()
1657 ep = ep_to_host_endpoint(ps->dev, uurb->endpoint); in proc_do_submiturb()
1659 return -ENOENT; in proc_do_submiturb()
1660 is_in = (uurb->endpoint & USB_ENDPOINT_DIR_MASK) != 0; in proc_do_submiturb()
1663 switch (uurb->type) { in proc_do_submiturb()
1665 if (!usb_endpoint_xfer_control(&ep->desc)) in proc_do_submiturb()
1666 return -EINVAL; in proc_do_submiturb()
1668 if (uurb->buffer_length < 8) in proc_do_submiturb()
1669 return -EINVAL; in proc_do_submiturb()
1672 return -ENOMEM; in proc_do_submiturb()
1673 if (copy_from_user(dr, uurb->buffer, 8)) { in proc_do_submiturb()
1674 ret = -EFAULT; in proc_do_submiturb()
1677 if (uurb->buffer_length < (le16_to_cpu(dr->wLength) + 8)) { in proc_do_submiturb()
1678 ret = -EINVAL; in proc_do_submiturb()
1681 ret = check_ctrlrecip(ps, dr->bRequestType, dr->bRequest, in proc_do_submiturb()
1682 le16_to_cpu(dr->wIndex)); in proc_do_submiturb()
1685 uurb->buffer_length = le16_to_cpu(dr->wLength); in proc_do_submiturb()
1686 uurb->buffer += 8; in proc_do_submiturb()
1687 if ((dr->bRequestType & USB_DIR_IN) && uurb->buffer_length) { in proc_do_submiturb()
1689 uurb->endpoint |= USB_DIR_IN; in proc_do_submiturb()
1692 uurb->endpoint &= ~USB_DIR_IN; in proc_do_submiturb()
1696 snoop(&ps->dev->dev, "control urb: bRequestType=%02x " in proc_do_submiturb()
1699 dr->bRequestType, dr->bRequest, in proc_do_submiturb()
1700 __le16_to_cpu(dr->wValue), in proc_do_submiturb()
1701 __le16_to_cpu(dr->wIndex), in proc_do_submiturb()
1702 __le16_to_cpu(dr->wLength)); in proc_do_submiturb()
1711 switch (usb_endpoint_type(&ep->desc)) { in proc_do_submiturb()
1714 return -EINVAL; in proc_do_submiturb()
1716 /* allow single-shot interrupt transfers */ in proc_do_submiturb()
1717 uurb->type = USBDEVFS_URB_TYPE_INTERRUPT; in proc_do_submiturb()
1720 num_sgs = DIV_ROUND_UP(uurb->buffer_length, USB_SG_SIZE); in proc_do_submiturb()
1721 if (num_sgs == 1 || num_sgs > ps->dev->bus->sg_tablesize) in proc_do_submiturb()
1723 if (ep->streams) in proc_do_submiturb()
1724 stream_id = uurb->stream_id; in proc_do_submiturb()
1728 if (!usb_endpoint_xfer_int(&ep->desc)) in proc_do_submiturb()
1729 return -EINVAL; in proc_do_submiturb()
1739 if (uurb->number_of_packets < 1 || in proc_do_submiturb()
1740 uurb->number_of_packets > 128) in proc_do_submiturb()
1741 return -EINVAL; in proc_do_submiturb()
1742 if (!usb_endpoint_xfer_isoc(&ep->desc)) in proc_do_submiturb()
1743 return -EINVAL; in proc_do_submiturb()
1744 number_of_packets = uurb->number_of_packets; in proc_do_submiturb()
1759 ret = -EINVAL; in proc_do_submiturb()
1765 uurb->buffer_length = totlen; in proc_do_submiturb()
1769 return -EINVAL; in proc_do_submiturb()
1772 if (uurb->buffer_length > 0 && in proc_do_submiturb()
1773 !access_ok(uurb->buffer, uurb->buffer_length)) { in proc_do_submiturb()
1774 ret = -EFAULT; in proc_do_submiturb()
1779 ret = -ENOMEM; in proc_do_submiturb()
1783 as->usbm = find_memory_area(ps, uurb); in proc_do_submiturb()
1784 if (IS_ERR(as->usbm)) { in proc_do_submiturb()
1785 ret = PTR_ERR(as->usbm); in proc_do_submiturb()
1786 as->usbm = NULL; in proc_do_submiturb()
1793 if (as->usbm) in proc_do_submiturb()
1797 (as->usbm ? 0 : uurb->buffer_length) + in proc_do_submiturb()
1802 as->mem_usage = u; in proc_do_submiturb()
1805 as->urb->sg = kmalloc_array(num_sgs, in proc_do_submiturb()
1808 if (!as->urb->sg) { in proc_do_submiturb()
1809 ret = -ENOMEM; in proc_do_submiturb()
1812 as->urb->num_sgs = num_sgs; in proc_do_submiturb()
1813 sg_init_table(as->urb->sg, as->urb->num_sgs); in proc_do_submiturb()
1815 totlen = uurb->buffer_length; in proc_do_submiturb()
1816 for (i = 0; i < as->urb->num_sgs; i++) { in proc_do_submiturb()
1820 ret = -ENOMEM; in proc_do_submiturb()
1823 sg_set_buf(&as->urb->sg[i], buf, u); in proc_do_submiturb()
1826 if (copy_from_user(buf, uurb->buffer, u)) { in proc_do_submiturb()
1827 ret = -EFAULT; in proc_do_submiturb()
1830 uurb->buffer += u; in proc_do_submiturb()
1832 totlen -= u; in proc_do_submiturb()
1834 } else if (uurb->buffer_length > 0) { in proc_do_submiturb()
1835 if (as->usbm) { in proc_do_submiturb()
1836 unsigned long uurb_start = (unsigned long)uurb->buffer; in proc_do_submiturb()
1838 as->urb->transfer_buffer = as->usbm->mem + in proc_do_submiturb()
1839 (uurb_start - as->usbm->vm_start); in proc_do_submiturb()
1841 as->urb->transfer_buffer = kmalloc(uurb->buffer_length, in proc_do_submiturb()
1843 if (!as->urb->transfer_buffer) { in proc_do_submiturb()
1844 ret = -ENOMEM; in proc_do_submiturb()
1848 if (copy_from_user(as->urb->transfer_buffer, in proc_do_submiturb()
1849 uurb->buffer, in proc_do_submiturb()
1850 uurb->buffer_length)) { in proc_do_submiturb()
1851 ret = -EFAULT; in proc_do_submiturb()
1854 } else if (uurb->type == USBDEVFS_URB_TYPE_ISO) { in proc_do_submiturb()
1861 memset(as->urb->transfer_buffer, 0, in proc_do_submiturb()
1862 uurb->buffer_length); in proc_do_submiturb()
1866 as->urb->dev = ps->dev; in proc_do_submiturb()
1867 as->urb->pipe = (uurb->type << 30) | in proc_do_submiturb()
1868 __create_pipe(ps->dev, uurb->endpoint & 0xf) | in proc_do_submiturb()
1869 (uurb->endpoint & USB_DIR_IN); in proc_do_submiturb()
1876 if (uurb->flags & USBDEVFS_URB_ISO_ASAP) in proc_do_submiturb()
1878 if (allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK) in proc_do_submiturb()
1880 if (allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET) in proc_do_submiturb()
1882 if (uurb->flags & USBDEVFS_URB_NO_INTERRUPT) in proc_do_submiturb()
1884 as->urb->transfer_flags = u; in proc_do_submiturb()
1886 if (!allow_short && uurb->flags & USBDEVFS_URB_SHORT_NOT_OK) in proc_do_submiturb()
1887 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_SHORT_NOT_OK.\n"); in proc_do_submiturb()
1888 if (!allow_zero && uurb->flags & USBDEVFS_URB_ZERO_PACKET) in proc_do_submiturb()
1889 dev_warn(&ps->dev->dev, "Requested nonsensical USBDEVFS_URB_ZERO_PACKET.\n"); in proc_do_submiturb()
1891 as->urb->transfer_buffer_length = uurb->buffer_length; in proc_do_submiturb()
1892 as->urb->setup_packet = (unsigned char *)dr; in proc_do_submiturb()
1894 as->urb->start_frame = uurb->start_frame; in proc_do_submiturb()
1895 as->urb->number_of_packets = number_of_packets; in proc_do_submiturb()
1896 as->urb->stream_id = stream_id; in proc_do_submiturb()
1898 if (ep->desc.bInterval) { in proc_do_submiturb()
1899 if (uurb->type == USBDEVFS_URB_TYPE_ISO || in proc_do_submiturb()
1900 ps->dev->speed == USB_SPEED_HIGH || in proc_do_submiturb()
1901 ps->dev->speed >= USB_SPEED_SUPER) in proc_do_submiturb()
1902 as->urb->interval = 1 << in proc_do_submiturb()
1903 min(15, ep->desc.bInterval - 1); in proc_do_submiturb()
1905 as->urb->interval = ep->desc.bInterval; in proc_do_submiturb()
1908 as->urb->context = as; in proc_do_submiturb()
1909 as->urb->complete = async_completed; in proc_do_submiturb()
1911 as->urb->iso_frame_desc[u].offset = totlen; in proc_do_submiturb()
1912 as->urb->iso_frame_desc[u].length = isopkt[u].length; in proc_do_submiturb()
1917 as->ps = ps; in proc_do_submiturb()
1918 as->userurb = arg; in proc_do_submiturb()
1919 as->userurb_sigval = userurb_sigval; in proc_do_submiturb()
1920 if (as->usbm) { in proc_do_submiturb()
1921 unsigned long uurb_start = (unsigned long)uurb->buffer; in proc_do_submiturb()
1923 as->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; in proc_do_submiturb()
1924 as->urb->transfer_dma = as->usbm->dma_handle + in proc_do_submiturb()
1925 (uurb_start - as->usbm->vm_start); in proc_do_submiturb()
1926 } else if (is_in && uurb->buffer_length > 0) in proc_do_submiturb()
1927 as->userbuffer = uurb->buffer; in proc_do_submiturb()
1928 as->signr = uurb->signr; in proc_do_submiturb()
1929 as->ifnum = ifnum; in proc_do_submiturb()
1930 as->pid = get_pid(task_pid(current)); in proc_do_submiturb()
1931 as->cred = get_current_cred(); in proc_do_submiturb()
1932 snoop_urb(ps->dev, as->userurb, as->urb->pipe, in proc_do_submiturb()
1933 as->urb->transfer_buffer_length, 0, SUBMIT, in proc_do_submiturb()
1936 snoop_urb_data(as->urb, as->urb->transfer_buffer_length); in proc_do_submiturb()
1940 if (usb_endpoint_xfer_bulk(&ep->desc)) { in proc_do_submiturb()
1941 spin_lock_irq(&ps->lock); in proc_do_submiturb()
1947 as->bulk_addr = usb_endpoint_num(&ep->desc) | in proc_do_submiturb()
1948 ((ep->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) in proc_do_submiturb()
1951 /* If this bulk URB is the start of a new transfer, re-enable in proc_do_submiturb()
1954 if (uurb->flags & USBDEVFS_URB_BULK_CONTINUATION) in proc_do_submiturb()
1955 as->bulk_status = AS_CONTINUATION; in proc_do_submiturb()
1957 ps->disabled_bulk_eps &= ~(1 << as->bulk_addr); in proc_do_submiturb()
1962 if (ps->disabled_bulk_eps & (1 << as->bulk_addr)) in proc_do_submiturb()
1963 ret = -EREMOTEIO; in proc_do_submiturb()
1965 ret = usb_submit_urb(as->urb, GFP_ATOMIC); in proc_do_submiturb()
1966 spin_unlock_irq(&ps->lock); in proc_do_submiturb()
1968 ret = usb_submit_urb(as->urb, GFP_KERNEL); in proc_do_submiturb()
1972 dev_printk(KERN_DEBUG, &ps->dev->dev, in proc_do_submiturb()
1974 snoop_urb(ps->dev, as->userurb, as->urb->pipe, in proc_do_submiturb()
1989 static int proc_submiturb(struct usb_dev_state *ps, void __user *arg) in proc_submiturb() argument
1995 return -EFAULT; in proc_submiturb()
2000 return proc_do_submiturb(ps, &uurb, in proc_submiturb()
2001 (((struct usbdevfs_urb __user *)arg)->iso_frame_desc), in proc_submiturb()
2005 static int proc_unlinkurb(struct usb_dev_state *ps, void __user *arg) in proc_unlinkurb() argument
2011 spin_lock_irqsave(&ps->lock, flags); in proc_unlinkurb()
2012 as = async_getpending(ps, arg); in proc_unlinkurb()
2014 spin_unlock_irqrestore(&ps->lock, flags); in proc_unlinkurb()
2015 return -EINVAL; in proc_unlinkurb()
2018 urb = as->urb; in proc_unlinkurb()
2020 spin_unlock_irqrestore(&ps->lock, flags); in proc_unlinkurb()
2032 if (urb->number_of_packets > 0) { in compute_isochronous_actual_length()
2033 urb->actual_length = 0; in compute_isochronous_actual_length()
2034 for (i = 0; i < urb->number_of_packets; i++) in compute_isochronous_actual_length()
2035 urb->actual_length += in compute_isochronous_actual_length()
2036 urb->iso_frame_desc[i].actual_length; in compute_isochronous_actual_length()
2042 struct urb *urb = as->urb; in processcompl()
2043 struct usbdevfs_urb __user *userurb = as->userurb; in processcompl()
2044 void __user *addr = as->userurb; in processcompl()
2048 if (as->userbuffer && urb->actual_length) { in processcompl()
2049 if (copy_urb_data_to_user(as->userbuffer, urb)) in processcompl()
2052 if (put_user(as->status, &userurb->status)) in processcompl()
2054 if (put_user(urb->actual_length, &userurb->actual_length)) in processcompl()
2056 if (put_user(urb->error_count, &userurb->error_count)) in processcompl()
2059 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { in processcompl()
2060 for (i = 0; i < urb->number_of_packets; i++) { in processcompl()
2061 if (put_user(urb->iso_frame_desc[i].actual_length, in processcompl()
2062 &userurb->iso_frame_desc[i].actual_length)) in processcompl()
2064 if (put_user(urb->iso_frame_desc[i].status, in processcompl()
2065 &userurb->iso_frame_desc[i].status)) in processcompl()
2071 return -EFAULT; in processcompl()
2075 return -EFAULT; in processcompl()
2078 static struct async *reap_as(struct usb_dev_state *ps) in reap_as() argument
2082 struct usb_device *dev = ps->dev; in reap_as()
2084 add_wait_queue(&ps->wait, &wait); in reap_as()
2087 as = async_getcompleted(ps); in reap_as()
2088 if (as || !connected(ps)) in reap_as()
2096 remove_wait_queue(&ps->wait, &wait); in reap_as()
2101 static int proc_reapurb(struct usb_dev_state *ps, void __user *arg) in proc_reapurb() argument
2103 struct async *as = reap_as(ps); in proc_reapurb()
2108 snoop(&ps->dev->dev, "reap %px\n", as->userurb); in proc_reapurb()
2114 return -EINTR; in proc_reapurb()
2115 return -ENODEV; in proc_reapurb()
2118 static int proc_reapurbnonblock(struct usb_dev_state *ps, void __user *arg) in proc_reapurbnonblock() argument
2123 as = async_getcompleted(ps); in proc_reapurbnonblock()
2125 snoop(&ps->dev->dev, "reap %px\n", as->userurb); in proc_reapurbnonblock()
2129 retval = (connected(ps) ? -EAGAIN : -ENODEV); in proc_reapurbnonblock()
2135 static int proc_control_compat(struct usb_dev_state *ps, in proc_control_compat() argument
2141 if (copy_from_user(&ctrl, p32, sizeof(*p32) - sizeof(compat_caddr_t)) || in proc_control_compat()
2142 get_user(udata, &p32->data)) in proc_control_compat()
2143 return -EFAULT; in proc_control_compat()
2145 return do_proc_control(ps, &ctrl); in proc_control_compat()
2148 static int proc_bulk_compat(struct usb_dev_state *ps, in proc_bulk_compat() argument
2154 if (get_user(bulk.ep, &p32->ep) || in proc_bulk_compat()
2155 get_user(bulk.len, &p32->len) || in proc_bulk_compat()
2156 get_user(bulk.timeout, &p32->timeout) || in proc_bulk_compat()
2157 get_user(addr, &p32->data)) in proc_bulk_compat()
2158 return -EFAULT; in proc_bulk_compat()
2160 return do_proc_bulk(ps, &bulk); in proc_bulk_compat()
2163 static int proc_disconnectsignal_compat(struct usb_dev_state *ps, void __user *arg) in proc_disconnectsignal_compat() argument
2168 return -EFAULT; in proc_disconnectsignal_compat()
2169 ps->discsignr = ds.signr; in proc_disconnectsignal_compat()
2170 ps->disccontext.sival_int = ds.context; in proc_disconnectsignal_compat()
2179 return -EFAULT; in get_urb32()
2180 kurb->type = urb32.type; in get_urb32()
2181 kurb->endpoint = urb32.endpoint; in get_urb32()
2182 kurb->status = urb32.status; in get_urb32()
2183 kurb->flags = urb32.flags; in get_urb32()
2184 kurb->buffer = compat_ptr(urb32.buffer); in get_urb32()
2185 kurb->buffer_length = urb32.buffer_length; in get_urb32()
2186 kurb->actual_length = urb32.actual_length; in get_urb32()
2187 kurb->start_frame = urb32.start_frame; in get_urb32()
2188 kurb->number_of_packets = urb32.number_of_packets; in get_urb32()
2189 kurb->error_count = urb32.error_count; in get_urb32()
2190 kurb->signr = urb32.signr; in get_urb32()
2191 kurb->usercontext = compat_ptr(urb32.usercontext); in get_urb32()
2195 static int proc_submiturb_compat(struct usb_dev_state *ps, void __user *arg) in proc_submiturb_compat() argument
2201 return -EFAULT; in proc_submiturb_compat()
2206 return proc_do_submiturb(ps, &uurb, in proc_submiturb_compat()
2207 ((struct usbdevfs_urb32 __user *)arg)->iso_frame_desc, in proc_submiturb_compat()
2213 struct urb *urb = as->urb; in processcompl_compat()
2214 struct usbdevfs_urb32 __user *userurb = as->userurb; in processcompl_compat()
2215 void __user *addr = as->userurb; in processcompl_compat()
2219 if (as->userbuffer && urb->actual_length) { in processcompl_compat()
2220 if (copy_urb_data_to_user(as->userbuffer, urb)) in processcompl_compat()
2221 return -EFAULT; in processcompl_compat()
2223 if (put_user(as->status, &userurb->status)) in processcompl_compat()
2224 return -EFAULT; in processcompl_compat()
2225 if (put_user(urb->actual_length, &userurb->actual_length)) in processcompl_compat()
2226 return -EFAULT; in processcompl_compat()
2227 if (put_user(urb->error_count, &userurb->error_count)) in processcompl_compat()
2228 return -EFAULT; in processcompl_compat()
2230 if (usb_endpoint_xfer_isoc(&urb->ep->desc)) { in processcompl_compat()
2231 for (i = 0; i < urb->number_of_packets; i++) { in processcompl_compat()
2232 if (put_user(urb->iso_frame_desc[i].actual_length, in processcompl_compat()
2233 &userurb->iso_frame_desc[i].actual_length)) in processcompl_compat()
2234 return -EFAULT; in processcompl_compat()
2235 if (put_user(urb->iso_frame_desc[i].status, in processcompl_compat()
2236 &userurb->iso_frame_desc[i].status)) in processcompl_compat()
2237 return -EFAULT; in processcompl_compat()
2242 return -EFAULT; in processcompl_compat()
2246 static int proc_reapurb_compat(struct usb_dev_state *ps, void __user *arg) in proc_reapurb_compat() argument
2248 struct async *as = reap_as(ps); in proc_reapurb_compat()
2253 snoop(&ps->dev->dev, "reap %px\n", as->userurb); in proc_reapurb_compat()
2259 return -EINTR; in proc_reapurb_compat()
2260 return -ENODEV; in proc_reapurb_compat()
2263 static int proc_reapurbnonblock_compat(struct usb_dev_state *ps, void __user *arg) in proc_reapurbnonblock_compat() argument
2268 as = async_getcompleted(ps); in proc_reapurbnonblock_compat()
2270 snoop(&ps->dev->dev, "reap %px\n", as->userurb); in proc_reapurbnonblock_compat()
2274 retval = (connected(ps) ? -EAGAIN : -ENODEV); in proc_reapurbnonblock_compat()
2282 static int proc_disconnectsignal(struct usb_dev_state *ps, void __user *arg) in proc_disconnectsignal() argument
2287 return -EFAULT; in proc_disconnectsignal()
2288 ps->discsignr = ds.signr; in proc_disconnectsignal()
2289 ps->disccontext.sival_ptr = ds.context; in proc_disconnectsignal()
2293 static int proc_claiminterface(struct usb_dev_state *ps, void __user *arg) in proc_claiminterface() argument
2298 return -EFAULT; in proc_claiminterface()
2299 return claimintf(ps, ifnum); in proc_claiminterface()
2302 static int proc_releaseinterface(struct usb_dev_state *ps, void __user *arg) in proc_releaseinterface() argument
2308 return -EFAULT; in proc_releaseinterface()
2309 ret = releaseintf(ps, ifnum); in proc_releaseinterface()
2312 destroy_async_on_interface(ps, ifnum); in proc_releaseinterface()
2316 static int proc_ioctl(struct usb_dev_state *ps, struct usbdevfs_ioctl *ctl) in proc_ioctl() argument
2324 if (ps->privileges_dropped) in proc_ioctl()
2325 return -EACCES; in proc_ioctl()
2327 if (!connected(ps)) in proc_ioctl()
2328 return -ENODEV; in proc_ioctl()
2331 size = _IOC_SIZE(ctl->ioctl_code); in proc_ioctl()
2335 return -ENOMEM; in proc_ioctl()
2336 if ((_IOC_DIR(ctl->ioctl_code) & _IOC_WRITE)) { in proc_ioctl()
2337 if (copy_from_user(buf, ctl->data, size)) { in proc_ioctl()
2339 return -EFAULT; in proc_ioctl()
2346 if (ps->dev->state != USB_STATE_CONFIGURED) in proc_ioctl()
2347 retval = -EHOSTUNREACH; in proc_ioctl()
2348 else if (!(intf = usb_ifnum_to_if(ps->dev, ctl->ifno))) in proc_ioctl()
2349 retval = -EINVAL; in proc_ioctl()
2350 else switch (ctl->ioctl_code) { in proc_ioctl()
2354 if (intf->dev.driver) { in proc_ioctl()
2355 driver = to_usb_driver(intf->dev.driver); in proc_ioctl()
2356 dev_dbg(&intf->dev, "disconnect by usbfs\n"); in proc_ioctl()
2359 retval = -ENODATA; in proc_ioctl()
2364 if (!intf->dev.driver) in proc_ioctl()
2365 retval = device_attach(&intf->dev); in proc_ioctl()
2367 retval = -EBUSY; in proc_ioctl()
2372 if (intf->dev.driver) in proc_ioctl()
2373 driver = to_usb_driver(intf->dev.driver); in proc_ioctl()
2374 if (driver == NULL || driver->unlocked_ioctl == NULL) { in proc_ioctl()
2375 retval = -ENOTTY; in proc_ioctl()
2377 retval = driver->unlocked_ioctl(intf, ctl->ioctl_code, buf); in proc_ioctl()
2378 if (retval == -ENOIOCTLCMD) in proc_ioctl()
2379 retval = -ENOTTY; in proc_ioctl()
2385 && (_IOC_DIR(ctl->ioctl_code) & _IOC_READ) != 0 in proc_ioctl()
2387 && copy_to_user(ctl->data, buf, size) != 0) in proc_ioctl()
2388 retval = -EFAULT; in proc_ioctl()
2394 static int proc_ioctl_default(struct usb_dev_state *ps, void __user *arg) in proc_ioctl_default() argument
2399 return -EFAULT; in proc_ioctl_default()
2400 return proc_ioctl(ps, &ctrl); in proc_ioctl_default()
2404 static int proc_ioctl_compat(struct usb_dev_state *ps, compat_uptr_t arg) in proc_ioctl_compat() argument
2410 return -EFAULT; in proc_ioctl_compat()
2414 return proc_ioctl(ps, &ctrl); in proc_ioctl_compat()
2418 static int proc_claim_port(struct usb_dev_state *ps, void __user *arg) in proc_claim_port() argument
2424 return -EFAULT; in proc_claim_port()
2425 rc = usb_hub_claim_port(ps->dev, portnum, ps); in proc_claim_port()
2427 snoop(&ps->dev->dev, "port %d claimed by process %d: %s\n", in proc_claim_port()
2428 portnum, task_pid_nr(current), current->comm); in proc_claim_port()
2432 static int proc_release_port(struct usb_dev_state *ps, void __user *arg) in proc_release_port() argument
2437 return -EFAULT; in proc_release_port()
2438 return usb_hub_release_port(ps->dev, portnum, ps); in proc_release_port()
2441 static int proc_get_capabilities(struct usb_dev_state *ps, void __user *arg) in proc_get_capabilities() argument
2449 if (!ps->dev->bus->no_stop_on_short) in proc_get_capabilities()
2451 if (ps->dev->bus->sg_tablesize) in proc_get_capabilities()
2455 return -EFAULT; in proc_get_capabilities()
2460 static int proc_disconnect_claim(struct usb_dev_state *ps, void __user *arg) in proc_disconnect_claim() argument
2466 return -EFAULT; in proc_disconnect_claim()
2468 intf = usb_ifnum_to_if(ps->dev, dc.interface); in proc_disconnect_claim()
2470 return -EINVAL; in proc_disconnect_claim()
2472 if (intf->dev.driver) { in proc_disconnect_claim()
2473 struct usb_driver *driver = to_usb_driver(intf->dev.driver); in proc_disconnect_claim()
2475 if (ps->privileges_dropped) in proc_disconnect_claim()
2476 return -EACCES; in proc_disconnect_claim()
2479 strncmp(dc.driver, intf->dev.driver->name, in proc_disconnect_claim()
2481 return -EBUSY; in proc_disconnect_claim()
2484 strncmp(dc.driver, intf->dev.driver->name, in proc_disconnect_claim()
2486 return -EBUSY; in proc_disconnect_claim()
2488 dev_dbg(&intf->dev, "disconnect by usbfs\n"); in proc_disconnect_claim()
2492 return claimintf(ps, dc.interface); in proc_disconnect_claim()
2495 static int proc_alloc_streams(struct usb_dev_state *ps, void __user *arg) in proc_alloc_streams() argument
2502 r = parse_usbdevfs_streams(ps, arg, &num_streams, &num_eps, in proc_alloc_streams()
2507 destroy_async_on_interface(ps, in proc_alloc_streams()
2508 intf->altsetting[0].desc.bInterfaceNumber); in proc_alloc_streams()
2515 static int proc_free_streams(struct usb_dev_state *ps, void __user *arg) in proc_free_streams() argument
2522 r = parse_usbdevfs_streams(ps, arg, NULL, &num_eps, &eps, &intf); in proc_free_streams()
2526 destroy_async_on_interface(ps, in proc_free_streams()
2527 intf->altsetting[0].desc.bInterfaceNumber); in proc_free_streams()
2534 static int proc_drop_privileges(struct usb_dev_state *ps, void __user *arg) in proc_drop_privileges() argument
2539 return -EFAULT; in proc_drop_privileges()
2545 ps->interface_allowed_mask &= data; in proc_drop_privileges()
2546 ps->privileges_dropped = true; in proc_drop_privileges()
2551 static int proc_forbid_suspend(struct usb_dev_state *ps) in proc_forbid_suspend() argument
2555 if (ps->suspend_allowed) { in proc_forbid_suspend()
2556 ret = usb_autoresume_device(ps->dev); in proc_forbid_suspend()
2558 ps->suspend_allowed = false; in proc_forbid_suspend()
2559 else if (ret != -ENODEV) in proc_forbid_suspend()
2560 ret = -EIO; in proc_forbid_suspend()
2565 static int proc_allow_suspend(struct usb_dev_state *ps) in proc_allow_suspend() argument
2567 if (!connected(ps)) in proc_allow_suspend()
2568 return -ENODEV; in proc_allow_suspend()
2570 WRITE_ONCE(ps->not_yet_resumed, 1); in proc_allow_suspend()
2571 if (!ps->suspend_allowed) { in proc_allow_suspend()
2572 usb_autosuspend_device(ps->dev); in proc_allow_suspend()
2573 ps->suspend_allowed = true; in proc_allow_suspend()
2578 static int proc_wait_for_resume(struct usb_dev_state *ps) in proc_wait_for_resume() argument
2582 usb_unlock_device(ps->dev); in proc_wait_for_resume()
2583 ret = wait_event_interruptible(ps->wait_for_resume, in proc_wait_for_resume()
2584 READ_ONCE(ps->not_yet_resumed) == 0); in proc_wait_for_resume()
2585 usb_lock_device(ps->dev); in proc_wait_for_resume()
2588 return -EINTR; in proc_wait_for_resume()
2589 return proc_forbid_suspend(ps); in proc_wait_for_resume()
2600 struct usb_dev_state *ps = file->private_data; in usbdev_do_ioctl() local
2602 struct usb_device *dev = ps->dev; in usbdev_do_ioctl()
2603 int ret = -ENOTTY; in usbdev_do_ioctl()
2605 if (!(file->f_mode & FMODE_WRITE)) in usbdev_do_ioctl()
2606 return -EPERM; in usbdev_do_ioctl()
2613 snoop(&dev->dev, "%s: REAPURB\n", __func__); in usbdev_do_ioctl()
2614 ret = proc_reapurb(ps, p); in usbdev_do_ioctl()
2618 snoop(&dev->dev, "%s: REAPURBNDELAY\n", __func__); in usbdev_do_ioctl()
2619 ret = proc_reapurbnonblock(ps, p); in usbdev_do_ioctl()
2624 snoop(&dev->dev, "%s: REAPURB32\n", __func__); in usbdev_do_ioctl()
2625 ret = proc_reapurb_compat(ps, p); in usbdev_do_ioctl()
2629 snoop(&dev->dev, "%s: REAPURBNDELAY32\n", __func__); in usbdev_do_ioctl()
2630 ret = proc_reapurbnonblock_compat(ps, p); in usbdev_do_ioctl()
2635 if (!connected(ps)) { in usbdev_do_ioctl()
2637 return -ENODEV; in usbdev_do_ioctl()
2642 snoop(&dev->dev, "%s: CONTROL\n", __func__); in usbdev_do_ioctl()
2643 ret = proc_control(ps, p); in usbdev_do_ioctl()
2650 snoop(&dev->dev, "%s: BULK\n", __func__); in usbdev_do_ioctl()
2651 ret = proc_bulk(ps, p); in usbdev_do_ioctl()
2658 snoop(&dev->dev, "%s: RESETEP\n", __func__); in usbdev_do_ioctl()
2659 ret = proc_resetep(ps, p); in usbdev_do_ioctl()
2666 snoop(&dev->dev, "%s: RESET\n", __func__); in usbdev_do_ioctl()
2667 ret = proc_resetdevice(ps); in usbdev_do_ioctl()
2671 snoop(&dev->dev, "%s: CLEAR_HALT\n", __func__); in usbdev_do_ioctl()
2672 ret = proc_clearhalt(ps, p); in usbdev_do_ioctl()
2679 snoop(&dev->dev, "%s: GETDRIVER\n", __func__); in usbdev_do_ioctl()
2680 ret = proc_getdriver(ps, p); in usbdev_do_ioctl()
2684 snoop(&dev->dev, "%s: CONNECTINFO\n", __func__); in usbdev_do_ioctl()
2685 ret = proc_connectinfo(ps, p); in usbdev_do_ioctl()
2689 snoop(&dev->dev, "%s: SETINTERFACE\n", __func__); in usbdev_do_ioctl()
2690 ret = proc_setintf(ps, p); in usbdev_do_ioctl()
2694 snoop(&dev->dev, "%s: SETCONFIGURATION\n", __func__); in usbdev_do_ioctl()
2695 ret = proc_setconfig(ps, p); in usbdev_do_ioctl()
2699 snoop(&dev->dev, "%s: SUBMITURB\n", __func__); in usbdev_do_ioctl()
2700 ret = proc_submiturb(ps, p); in usbdev_do_ioctl()
2708 snoop(&dev->dev, "%s: CONTROL32\n", __func__); in usbdev_do_ioctl()
2709 ret = proc_control_compat(ps, p); in usbdev_do_ioctl()
2716 snoop(&dev->dev, "%s: BULK32\n", __func__); in usbdev_do_ioctl()
2717 ret = proc_bulk_compat(ps, p); in usbdev_do_ioctl()
2724 snoop(&dev->dev, "%s: DISCSIGNAL32\n", __func__); in usbdev_do_ioctl()
2725 ret = proc_disconnectsignal_compat(ps, p); in usbdev_do_ioctl()
2729 snoop(&dev->dev, "%s: SUBMITURB32\n", __func__); in usbdev_do_ioctl()
2730 ret = proc_submiturb_compat(ps, p); in usbdev_do_ioctl()
2737 snoop(&dev->dev, "%s: IOCTL32\n", __func__); in usbdev_do_ioctl()
2738 ret = proc_ioctl_compat(ps, ptr_to_compat(p)); in usbdev_do_ioctl()
2743 snoop(&dev->dev, "%s: DISCARDURB %px\n", __func__, p); in usbdev_do_ioctl()
2744 ret = proc_unlinkurb(ps, p); in usbdev_do_ioctl()
2748 snoop(&dev->dev, "%s: DISCSIGNAL\n", __func__); in usbdev_do_ioctl()
2749 ret = proc_disconnectsignal(ps, p); in usbdev_do_ioctl()
2753 snoop(&dev->dev, "%s: CLAIMINTERFACE\n", __func__); in usbdev_do_ioctl()
2754 ret = proc_claiminterface(ps, p); in usbdev_do_ioctl()
2758 snoop(&dev->dev, "%s: RELEASEINTERFACE\n", __func__); in usbdev_do_ioctl()
2759 ret = proc_releaseinterface(ps, p); in usbdev_do_ioctl()
2763 snoop(&dev->dev, "%s: IOCTL\n", __func__); in usbdev_do_ioctl()
2764 ret = proc_ioctl_default(ps, p); in usbdev_do_ioctl()
2768 snoop(&dev->dev, "%s: CLAIM_PORT\n", __func__); in usbdev_do_ioctl()
2769 ret = proc_claim_port(ps, p); in usbdev_do_ioctl()
2773 snoop(&dev->dev, "%s: RELEASE_PORT\n", __func__); in usbdev_do_ioctl()
2774 ret = proc_release_port(ps, p); in usbdev_do_ioctl()
2777 ret = proc_get_capabilities(ps, p); in usbdev_do_ioctl()
2780 ret = proc_disconnect_claim(ps, p); in usbdev_do_ioctl()
2783 ret = proc_alloc_streams(ps, p); in usbdev_do_ioctl()
2786 ret = proc_free_streams(ps, p); in usbdev_do_ioctl()
2789 ret = proc_drop_privileges(ps, p); in usbdev_do_ioctl()
2792 ret = ps->dev->speed; in usbdev_do_ioctl()
2795 ret = proc_forbid_suspend(ps); in usbdev_do_ioctl()
2798 ret = proc_allow_suspend(ps); in usbdev_do_ioctl()
2801 ret = proc_wait_for_resume(ps); in usbdev_do_ioctl()
2805 /* Handle variable-length commands */ in usbdev_do_ioctl()
2808 ret = proc_conninfo_ex(ps, p, _IOC_SIZE(cmd)); in usbdev_do_ioctl()
2829 /* No kernel lock - fine */
2833 struct usb_dev_state *ps = file->private_data; in usbdev_poll() local
2836 poll_wait(file, &ps->wait, wait); in usbdev_poll()
2837 if (file->f_mode & FMODE_WRITE && !list_empty(&ps->async_completed)) in usbdev_poll()
2839 if (!connected(ps)) in usbdev_poll()
2841 if (list_empty(&ps->list)) in usbdev_poll()
2860 struct usb_dev_state *ps; in usbdev_remove() local
2864 while (!list_empty(&udev->filelist)) { in usbdev_remove()
2865 ps = list_entry(udev->filelist.next, struct usb_dev_state, list); in usbdev_remove()
2866 destroy_all_async(ps); in usbdev_remove()
2867 wake_up_all(&ps->wait); in usbdev_remove()
2868 WRITE_ONCE(ps->not_yet_resumed, 0); in usbdev_remove()
2869 wake_up_all(&ps->wait_for_resume); in usbdev_remove()
2870 list_del_init(&ps->list); in usbdev_remove()
2871 if (ps->discsignr) in usbdev_remove()
2872 kill_pid_usb_asyncio(ps->discsignr, EPIPE, ps->disccontext, in usbdev_remove()
2873 ps->disc_pid, ps->cred); in usbdev_remove()