summaryrefslogtreecommitdiff
path: root/sys/dev/usb/usb_dev.c
AgeCommit message (Collapse)Author
2025-10-18knotes: kqueue: handle copy for trivial filtersKonstantin Belousov
Reviewed by: markj Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D52045
2025-06-11machine/stdarg.h -> sys/stdarg.hBrooks Davis
Switch to using sys/stdarg.h for va_list type and va_* builtins. Make an attempt to insert the include in a sensible place. Where style(9) was followed this is easy, where it was ignored, aim for the first block of sys/*.h headers and don't get too fussy or try to fix other style bugs. Reviewed by: imp Exp-run by: antoine (PR 286274) Pull Request: https://github.com/freebsd/freebsd-src/pull/1595
2024-11-26kern: Make fileops and filterops tables const where possibleMark Johnston
No functional change intended. MFC after: 1 week
2023-08-16sys: Remove $FreeBSD$: one-line .c comment patternWarner Losh
Remove /^/[*/]\s*\$FreeBSD\$.*\n/
2023-05-12spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSDWarner Losh
The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch up to that fact and revert to their recommended match of BSD-2-Clause. Discussed with: pfg MFC After: 3 days Sponsored by: Netflix
2023-04-08usb(4): Separate the fast path and the slow path to avoid races and ↵Hans Petter Selasky
use-after-free for the USB FS interface. Bad behaving user-space USB applicatoins may crash the kernel by issuing USB FS related ioctl(2)'s out of their expected order. By default the USB FS ioctl(2) interface is only available to the administrator, root, and driver applications like webcamd(8) needs to be hijacked in order for this to happen. The issue is the fast-path code does not always see updates made by the slow-path code, and may then work on freed memory. This is easily fixed by using an EPOCH(9) type of synchronization mechanism. A SX(9) lock will be used as a substitute for EPOCH(9), due to the need for sleepability. In addition most calls going into the fast-path originate from a single user-space process and the need for multi-thread performance is not present. Differential Revision: https://reviews.freebsd.org/D39373 Reviewed by: markj@ Reported by: C Turt <ecturt@gmail.com> admbugs: 994 MFC after: 1 week Sponsored by: NVIDIA Networking
2023-02-04usb: purge EOL release compatibilityElliott Mitchell
Remove conditionsals for FreeBSD 6, 7, 8 and 9 compatibility. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/603 Differential Revision: https://reviews.freebsd.org/D35560
2021-12-17usb: real freebsd32 support for most ioctlsBrooks Davis
Use thunks or alternative access methods to support ioctls without the COMPAT_32BIT hacks that store pointers in uint64_t's on 32-bit platforms. This should allow a normal i386 libusb to work. On CheriBSD, the sizes of the structs will differ between CheriABI (the default) and freebsd64 no matter what so we need proper compat support there. This change paves the way. Reviewed by: hselasky, jrtc27 (prior version)
2021-10-24usb(4): Fix for use after free in combination with EVDEV_SUPPORT.Hans Petter Selasky
When EVDEV_SUPPORT was introduced, the USB transfers may be running after the main FIFO is closed. In connection to this a race may appear which can lead to use-after-free scenarios. Fix this for all FIFO consumers by initializing and resetting the FIFO queues under the lock used by the client. Then the client driver will see an empty queue in all cases a race may appear. Found by: pho@ MFC after: 1 week Sponsored by: NVIDIA Networking
2020-09-01usb: clean up empty lines in .c and .h filesMateusz Guzik
Notes: svn path=/head/; revision=365084
2020-07-22usb(4): Stop checking for failures from malloc(M_WAITOK).Mark Johnston
Handle the fact that parts of usb(4) can be compiled into the boot loader, where M_WAITOK does not guarantee a successful allocation. PR: 240545 Submitted by: Andrew Reiter <arr@watson.org> (original version) Reviewed by: hselasky MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D25706 Notes: svn path=/head/; revision=363420
2020-02-15Mark more nodes as CTLFLAG_MPSAFE or CTLFLAG_NEEDGIANT (2 of many)Pawel Biernacki
r357614 added CTLFLAG_NEEDGIANT to make it easier to find nodes that are still not MPSAFE (or already are but aren’t properly marked). Use it in preparation for a general review of all nodes. This is non-functional change that adds annotations to SYSCTL_NODE and SYSCTL_PROC nodes using one of the soon-to-be-required flags. Reviewed by: hselasky, kib Approved by: kib (mentor) Differential Revision: https://reviews.freebsd.org/D23632 Notes: svn path=/head/; revision=357972
2017-12-25Do pass removing some write-only variables from the kernel.Alexander Kabaev
This reduces noise when kernel is compiled by newer GCC versions, such as one used by external toolchain ports. Reviewed by: kib, andrew(sys/arm and sys/arm64), emaste(partial), erj(partial) Reviewed by: jhb (sys/dev/pci/* sys/kern/vfs_aio.c and sys/kern/kern_synch.c) Differential Revision: https://reviews.freebsd.org/D10385 Notes: svn path=/head/; revision=327173
2017-11-27sys/dev: further adoption of SPDX licensing ID tags.Pedro F. Giffuni
Mainly focus on files that use BSD 2-Clause license, however the tool I was using misidentified many licenses so this was mostly a manual - error prone - task. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Notes: svn path=/head/; revision=326255
2016-09-14Improve USB polling mode by not locking any mutexes, asserting anyHans Petter Selasky
mutexes or using any callouts when active. Trying to lock a mutex when KDB is active or the scheduler is stopped can result in infinite wait loops. The same goes for calling callout related functions which in turn lock mutexes. If the USB controller at which a USB keyboard is connected is idle when KDB is entered, polling the USB keyboard via USB will always succeed. Else polling may fail depending on which state the USB subsystem and USB interrupt handler is in. This is unavoidable unless KDB can wait for USB interrupt threads to complete before stalling the CPU(s). Tested by: Bruce Evans <bde@freebsd.org> MFC after: 4 weeks Notes: svn path=/head/; revision=305806
2016-05-25Check for signals when locking the USB enumeration thread fromHans Petter Selasky
userspace, so that USB applications can be killed if an enumeration thread should be stuck for various reasons. MFC after: 1 week Notes: svn path=/head/; revision=300667
2016-05-02dev/usb: minor spelling fixes in comments.Pedro F. Giffuni
No functional change. Reviewed by: hselasky Notes: svn path=/head/; revision=298932
2015-06-05Don't wait forever for USB data to be flushed.Hans Petter Selasky
MFC after: 1 week Notes: svn path=/head/; revision=284011
2015-01-13Resolve a special case deadlock: When two or more threads areHans Petter Selasky
simultaneously detaching kernel drivers on the same USB device we can get stuck in the "usb_wait_pending_ref_locked()" function because the conditions needed for allowing detach are not met. The "destroy_dev()" function waits for all system calls involving the given character device to return. Character device system calls may lock the USB enumeration lock, which is also held when "destroy_dev()" is called. This can sometimes lead to a deadlock not noticed by WITNESS. The current solution is to ensure the calling thread is the only one holding the USB enumeration lock and prevent other threads from getting refs while a USB device detach is ongoing. This turned out not to be sufficient. To solve this deadlock we could use "destroy_dev_sched()" to schedule the device destruction in the background, but then we don't know when it is safe to free() the private data of the character device. Instead a callback function is executed by the USB explore process to kill off any leftover USB character devices synchronously after the USB device explore code is finished and the USB enumeration lock is no longer locked. This makes porting easier and also ensures us that character devices must eventually go away after a USB device detach. While at it ensure that "flag_iserror" is only written when "priv_mtx" is locked, which is protecting it. MFC after: 5 days Notes: svn path=/head/; revision=277136
2014-10-03When we fail to get a USB reference we should just return, becauseHans Petter Selasky
there are no more references held. MFC after: 3 days Notes: svn path=/head/; revision=272480
2014-06-28Pull in r267961 and r267973 again. Fix for issues reported will follow.Hans Petter Selasky
Notes: svn path=/head/; revision=267992
2014-06-27Revert r267961, r267973:Glen Barber
These changes prevent sysctl(8) from returning proper output, such as: 1) no output from sysctl(8) 2) erroneously returning ENOMEM with tools like truss(1) or uname(1) truss: can not get etype: Cannot allocate memory Notes: svn path=/head/; revision=267985
2014-06-27Extend the meaning of the CTLFLAG_TUN flag to automatically check ifHans Petter Selasky
there is an environment variable which shall initialize the SYSCTL during early boot. This works for all SYSCTL types both statically and dynamically created ones, except for the SYSCTL NODE type and SYSCTLs which belong to VNETs. A new flag, CTLFLAG_NOFETCH, has been added to be used in the case a tunable sysctl has a custom initialisation function allowing the sysctl to still be marked as a tunable. The kernel SYSCTL API is mostly the same, with a few exceptions for some special operations like iterating childrens of a static/extern SYSCTL node. This operation should probably be made into a factored out common macro, hence some device drivers use this. The reason for changing the SYSCTL API was the need for a SYSCTL parent OID pointer and not only the SYSCTL parent OID list pointer in order to quickly generate the sysctl path. The motivation behind this patch is to avoid parameter loading cludges inside the OFED driver subsystem. Instead of adding special code to the OFED driver subsystem to post-load tunables into dynamically created sysctls, we generalize this in the kernel. Other changes: - Corrected a possibly incorrect sysctl name from "hw.cbb.intr_mask" to "hw.pcic.intr_mask". - Removed redundant TUNABLE statements throughout the kernel. - Some minor code rewrites in connection to removing not needed TUNABLE statements. - Added a missing SYSCTL_DECL(). - Wrapped two very long lines. - Avoid malloc()/free() inside sysctl string handling, in case it is called to initialize a sysctl from a tunable, hence malloc()/free() is not ready when sysctls from the sysctl dataset are registered. - Bumped FreeBSD version to indicate SYSCTL API change. MFC after: 2 weeks Sponsored by: Mellanox Technologies Notes: svn path=/head/; revision=267961
2014-06-08Resolve a deadlock setting the USB configuration index from userspaceHans Petter Selasky
on USB HUBs by moving the code into the USB explore threads. The deadlock happens because child devices of the USB HUB don't have the expected reference count when called from outside the explore thread. Only the HUB device itself, which the IOCTL interface locks, gets the correct reference count. MFC after: 3 days Notes: svn path=/head/; revision=267240
2014-03-20Try to resolve a possible deadlock when detaching USB devices whichHans Petter Selasky
create character devices. The deadlock can happen if an application is issuing IOCTLs which require USB refcounting, at the same time the USB device is detaching. There is already a counter in place in the USB device structure to detect this situation, but it was not always checked ahead of invoking functions that might destroy character devices, like detach, set configuration, set alternate interface or detach active kernel driver. Reported by: Daniel O'Connor <doconnor@gsoft.com.au> MFC after: 1 week Notes: svn path=/head/; revision=263423
2014-02-27Clear the knote list before destroying it.Hans Petter Selasky
Add seldrain() to wakeup any pollers. Submitted by: Kohji Okuno <okuno.kohji@jp.panasonic.com> MFC after: 2 weeks Notes: svn path=/head/; revision=262554
2014-02-27Add missing call to "knlist_destroy()".Hans Petter Selasky
Found by: John-Mark Gurney <jmg@funkthat.com> MFC after: 2 weeks Notes: svn path=/head/; revision=262551
2014-02-27Add support for kqfilter to USB character devices.Hans Petter Selasky
Submitted by: Kohji Okuno <okuno.kohji@jp.panasonic.com> MFC after: 2 weeks Notes: svn path=/head/; revision=262550
2014-01-24Hide now harmless warning from dmesg.Hans Petter Selasky
MFC after: 1 week Notes: svn path=/head/; revision=261123
2014-01-17Fix a possible memory use after free and leak situation associatedHans Petter Selasky
with USB device detach when using character device handles. This also includes LibUSB. It turns out that "usb_close()" cannot always get a reference to clean up its USB transfers and such, if called during the kernel USB device detach. Analysis by: hselasky @ Reported by: Juergen Lock <nox@jelal.kn-bremen.de> MFC after: 1 week Notes: svn path=/head/; revision=260814
2014-01-17Close a minor deadlock.Hans Petter Selasky
MFC after: 1 week Notes: svn path=/head/; revision=260808
2013-10-27Fix a deadlock when trying to power off a USB device. The deadlockHans Petter Selasky
happens because the code in question is trying to modify the parent USB port registers outside the USB explore thread. MFC after: 3 days Notes: svn path=/head/; revision=257206
2013-02-13Resolve a LOR after r246616. Protect control requests using the USB deviceHans Petter Selasky
enumeration lock. Make sure all callers of usbd_enum_lock() check the return value. Remove the control transfer specific lock. Bump the FreeBSD version number, hence external USB modules may need to be recompiled due to a USB device structure change. MFC after: 1 week Notes: svn path=/head/; revision=246759
2013-01-30Modify the FreeBSD USB kernel code so that it can be compiled directlyHans Petter Selasky
into the FreeBSD boot loader, typically for non-USB aware BIOSes, EFI systems or embedded platforms. This is also useful for out of the system compilation of the FreeBSD USB stack for various purposes. The USB kernel files can now optionally include a global header file which should include all needed definitions required to compile the FreeBSD USB stack. When the global USB header file is included, no other USB header files will be included by default. Add new file containing the USB stack configuration for the FreeBSD loader build. Replace some __FBSDID()'s by /* $FreeBSD$ */ comments. Now all USB files follow the same style. Use cases: - console in loader via USB - loading kernel via USB Discussed with: Hiroki Sato, hrs @ EuroBSDCon Notes: svn path=/head/; revision=246122
2012-10-26Add missing CTLFLAG_TUN flag to tunable sysctls in USB stack.Hans Petter Selasky
Rearrange the tunables and belonging sysctl declarations, so that they are next to eachother. Submitted by: n_hibma @ MFC after: 1 week Notes: svn path=/head/; revision=242126
2012-04-02Fix compiler warnings, mostly signed issues,Hans Petter Selasky
when USB modules are compiled with WARNS=9. MFC after: 1 weeks Notes: svn path=/head/; revision=233774
2012-02-10Remove direct access to si_name.Ed Schouten
Code should just use the devtoname() function to obtain the name of a character device. Also add const keywords to pieces of code that need it to build properly. MFC after: 2 weeks Notes: svn path=/head/; revision=231378
2011-11-12Style change.Hans Petter Selasky
- Make it easier to port the USB code to other platforms by only using one set of memory functions for clearing and copying memory. None of the memory copies are overlapping. This means using bcopy() is not required. - Fix a compile warning when USB_HAVE_BUSDMA=0 - Add missing semicolon in avr32dci. - Update some comments. MFC after: 1 week Notes: svn path=/head/; revision=227461
2011-11-07Mark all SYSCTL_NODEs static that have no corresponding SYSCTL_DECLs.Ed Schouten
The SYSCTL_NODE macro defines a list that stores all child-elements of that node. If there's no SYSCTL_DECL macro anywhere else, there's no reason why it shouldn't be static. Notes: svn path=/head/; revision=227309
2011-09-16In order to maximize the re-usability of kernel code in user space thisKip Macy
patch modifies makesyscalls.sh to prefix all of the non-compatibility calls (e.g. not linux_, freebsd32_) with sys_ and updates the kernel entry points and all places in the code that use them. It also fixes an additional name space collision between the kernel function psignal and the libc function of the same name by renaming the kernel psignal kern_psignal(). By introducing this change now we will ease future MFCs that change syscalls. Reviewed by: rwatson Approved by: re (bz) Notes: svn path=/head/; revision=225617
2011-08-20Fix for recursive locking in usb_close() after change 224777.Hans Petter Selasky
Approved by: re (kib) MFC after: 3 days Reported by: kwm @ Notes: svn path=/head/; revision=225038
2011-08-11Use synchronous device destruction instead of asynchronous, so that a newHans Petter Selasky
device having the same name like a previous one is not created before the old one is gone. This fixes some panics due to asserts in the devfs code which were added recently. Approved by: re (kib) MFC after: 1 week Notes: svn path=/head/; revision=224777
2011-01-11Remove unneeded includes of <sys/linker_set.h>. Other headers that useJohn Baldwin
it internally contain nested includes. Reviewed by: bde Notes: svn path=/head/; revision=217265
2010-10-04Serialise USB re-enumeration with the USB explore thread.Hans Petter Selasky
This patch can solve problems when multiple USB devices are re-enumerated at the same time on the same bus. Approved by: thompsa (mentor) Notes: svn path=/head/; revision=213432
2010-10-04Correct IOCTL return code.Hans Petter Selasky
Approved by: thompsa (mentor) Notes: svn path=/head/; revision=213431
2010-06-11fix a few cases where a string is passed via format argument instead ofAndriy Gapon
via %s Most of the cases looked harmless, but this is done for the sake of correctness. In one case it even allowed to drop an intermediate buffer. Found by: clang MFC after: 2 week Notes: svn path=/head/; revision=209062
2010-05-12Staticise usb_ref_device and usb_unref_device.Andrew Thompson
Submitted by: Hans Petter Selasky Notes: svn path=/head/; revision=208007
2010-04-22Use a more obvious prefix for the USB control (endpoint 0) transfers ratherAndrew Thompson
than default_*. Notes: svn path=/head/; revision=207080
2010-04-22Properly name the sxlocks, mutexes and condvars.Andrew Thompson
Notes: svn path=/head/; revision=207079
2009-11-26Remove overuse of exclamation marks in kernel printfs, there mere fact aAndrew Thompson
message has been printed is enough to get someones attention. Also remove the line number for DPRINTF/DPRINTFN, it already prints the funtion name and a unique message. Notes: svn path=/head/; revision=199816