Minimal header-only Linux usbfs access for one CHDKbridge camera. More...
#include <dirent.h>#include <errno.h>#include <fcntl.h>#include <limits.h>#include <linux/usbdevice_fs.h>#include <poll.h>#include <signal.h>#include <stdbool.h>#include <stddef.h>#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <sys/ioctl.h>#include <sys/stat.h>#include <sys/sysmacros.h>#include <time.h>#include <unistd.h>

Go to the source code of this file.
Data Structures | |
| struct | usbfs_device |
| One enumerated USB device; a value copy, valid after enumeration ends. More... | |
| struct | usbfs_endpoint |
| struct | usbfs_interface |
| struct | usbfs_devices |
| struct | usbfs_handle |
| Open usbfs file; fd is -1 when closed or dropped after a lost URB. More... | |
Macros | |
| #define | USBFS_SYSFS_ROOT "/sys/bus/usb/devices" |
| #define | USBFS_DEVICE_ROOT "/dev/bus/usb" |
Enumerations | |
| enum | { USBFS_SUCCESS = 0 , USBFS_ERROR_IO = -1 , USBFS_ERROR_INVALID_PARAM = -2 , USBFS_ERROR_ACCESS = -3 , USBFS_ERROR_NO_DEVICE = -4 , USBFS_ERROR_NOT_FOUND = -5 , USBFS_ERROR_BUSY = -6 , USBFS_ERROR_TIMEOUT = -7 , USBFS_ERROR_OVERFLOW = -8 , USBFS_ERROR_PIPE = -9 , USBFS_ERROR_INTERRUPTED = -10 , USBFS_ERROR_NO_MEM = -11 , USBFS_ERROR_OTHER = -99 } |
| Result codes; negative values are errors, as in libusb. More... | |
| enum | { USBFS_ENDPOINT_IN = 0x80 , USBFS_TRANSFER_TYPE_MASK = 0x03 , USBFS_TRANSFER_TYPE_BULK = 2 , USBFS_TRANSFER_TYPE_INTERRUPT = 3 , USBFS_MAX_ENDPOINTS = 30 , USBFS_MAX_PORTS = 7 , USBFS_DESCRIPTOR_CAPACITY = 16384 , USBFS_LEGACY_TRANSFER = 16384 , USBFS_DISCARD_MS = 1000 , USBFS_DEVICE_MAJOR = 189 } |
Functions | |
| static const char * | usbfs_error_name (int error) |
| Get the diagnostic name of a usbfs result. | |
| static bool | usbfs_available (void) |
| True when sysfs device listing and usbfs nodes are reachable. | |
| static bool | usbfs_read_number (const char *device, const char *attribute, int base, unsigned long maximum, unsigned long *value) |
| Read one bounded unsigned numeric sysfs attribute. | |
| static bool | usbfs_parse_name (const char *name, struct usbfs_device *device) |
| Parse "BUS-PORT[.PORT...]"; rejects root hubs and interface entries. | |
| static int | usbfs_devices_open (struct usbfs_devices *devices) |
| Start a streaming sysfs enumeration. | |
| static int | usbfs_devices_next (struct usbfs_devices *devices, struct usbfs_device *device) |
| static void | usbfs_devices_close (struct usbfs_devices *devices) |
| Release the sysfs enumeration stream. | |
| static int | usbfs_select_config (const uint8_t *descriptors, size_t length, unsigned value, const uint8_t **config, size_t *config_length) |
Locate configuration value in raw sysfs descriptors. | |
| static int | usbfs_active_config (const struct usbfs_device *device, uint8_t *buffer, size_t capacity, const uint8_t **config, size_t *config_length) |
Read the active configuration into buffer; config points inside it. | |
| static bool | usbfs_next_interface (const uint8_t *config, size_t length, size_t *offset, struct usbfs_interface *interface) |
| Return the next complete interface/alternate setting in a configuration. | |
| static bool | usbfs_same_device (const struct usbfs_device *device) |
| Compare a previously discovered USB identity with current sysfs. | |
| static int | usbfs_open (const struct usbfs_device *device, struct usbfs_handle *handle) |
| Open the device node, rejecting a node now owned by a different device. | |
| static void | usbfs_close (struct usbfs_handle *handle) |
| Close the node; the kernel releases claimed interfaces and kills pending URBs. | |
| static int | usbfs_interface_ioctl (struct usbfs_handle *handle, unsigned long request, void *argument) |
| Run a synchronous interface ioctl and translate its result. | |
| static int | usbfs_claim_interface (struct usbfs_handle *handle, int interface) |
| Claim without detaching another owner; EBUSY is reported to the caller. | |
| static int | usbfs_release_interface (struct usbfs_handle *handle, int interface) |
| Release the numbered interface on an open device. | |
| static int | usbfs_set_alternate (struct usbfs_handle *handle, int interface, int alternate) |
| Select an alternate setting for an interface. | |
| static int | usbfs_control_transfer (struct usbfs_handle *handle, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, unsigned char *data, uint16_t length, unsigned timeout) |
| Synchronous control transfer on endpoint zero. | |
| static int | usbfs_clear_halt (struct usbfs_handle *handle, unsigned char endpoint) |
| Clear a halted endpoint of a claimed interface; data toggles are reset by the kernel. | |
| static uint64_t | usbfs_now_ms (void) |
| Read the clock used for bulk transfer deadlines. | |
| static int | usbfs_drop (struct usbfs_handle *handle, int result) |
| Abandon the fd; closing it is the only way the kernel guarantees the URB is gone. | |
| static int | usbfs_bulk_urb (struct usbfs_handle *handle, unsigned char endpoint, unsigned char *data, int length, int *actual, uint64_t deadline) |
Submit one bulk URB and wait until it is reaped or deadline passes. | |
| static int | usbfs_bulk_transfer (struct usbfs_handle *handle, unsigned char endpoint, unsigned char *data, int length, int *transferred, unsigned timeout) |
| Synchronous bulk transfer with libusb_bulk_transfer() semantics. | |
Detailed Description
Minimal header-only Linux usbfs access for one CHDKbridge camera.
Discovers devices through sysfs, parses the active configuration, owns one interface and performs synchronous bulk transfers through /dev/bus/usb ioctls. The wrapper allocates no heap storage and creates no threads.
- Warning
- Serialize all access to a handle, including close. A bulk call owns its submitted URB until completion or descriptor teardown; another caller must not reap it or close and reuse the descriptor.
Definition in file usbfs.h.
Macro Definition Documentation
◆ USBFS_DEVICE_ROOT
◆ USBFS_SYSFS_ROOT
Enumeration Type Documentation
◆ anonymous enum
| anonymous enum |
Result codes; negative values are errors, as in libusb.
◆ anonymous enum
| anonymous enum |
Function Documentation
◆ usbfs_active_config()
|
inlinestatic |
◆ usbfs_available()
|
inlinestatic |
◆ usbfs_bulk_transfer()
|
inlinestatic |
Synchronous bulk transfer with libusb_bulk_transfer() semantics.
- Parameters
-
handle Serialized device handle holding the interface claim. endpoint Bulk endpoint address including the direction bit. [in,out] data Buffer for lengthbytes.length Requested bytes, nonnegative. [out] transferred Required byte count; may be nonzero on failure. timeout Nonzero total timeout in milliseconds.
- Returns
- USBFS_SUCCESS or a negative wrapper error.
IN transfers are capped at one URB; OUT transfers are split only on kernels without NO_PACKET_SIZE_LIM, sharing one deadline.
◆ usbfs_bulk_urb()
|
inlinestatic |
Submit one bulk URB and wait until it is reaped or deadline passes.
On expiry the URB is discarded and reaped, preserving any bytes already received. Data reaches data only when the URB is reaped, so a URB that cannot be reaped within the discard grace is made harmless by closing.
◆ usbfs_claim_interface()
|
inlinestatic |
◆ usbfs_clear_halt()
|
inlinestatic |
◆ usbfs_close()
|
inlinestatic |
◆ usbfs_control_transfer()
|
inlinestatic |
Synchronous control transfer on endpoint zero.
- Parameters
-
handle Serialized device handle. request_type USB bmRequestType, including direction. request USB bRequest. value USB wValue. index USB wIndex. [in,out] data Caller buffer; may be NULL only for a zero-length transfer. length Size of the data stage in bytes. timeout Nonzero kernel transfer timeout in milliseconds.
- Returns
- Bytes transferred (>= 0) or a negative USBFS error.
A short successful transfer can be meaningful; callers validate the returned length against the particular request's contract.
◆ usbfs_devices_close()
|
inlinestatic |
◆ usbfs_devices_next()
|
inlinestatic |
◆ usbfs_devices_open()
|
inlinestatic |
◆ usbfs_drop()
|
inlinestatic |
◆ usbfs_error_name()
|
inlinestatic |
◆ usbfs_interface_ioctl()
|
inlinestatic |
Run a synchronous interface ioctl and translate its result.
- Parameters
-
handle Serialized open device handle. request Linux usbfs ioctl number. argument Request-specific argument storage.
- Returns
- USBFS_SUCCESS or a negative wrapper error.
Retry interruptions, preserving the distinction between busy, disconnected and inaccessible devices for the calling policy.
◆ usbfs_next_interface()
|
inlinestatic |
Return the next complete interface/alternate setting in a configuration.
- Parameters
-
config Borrowed descriptor block for the active configuration. length Readable bytes in config.offset Start with 0; advanced past the returned interface. [out] interface Parsed interface; use only when the function returns true.
- Returns
- True when a complete interface was found.
Interfaces whose endpoint descriptors disagree with bNumEndpoints are skipped. Parsing stops at the first malformed descriptor header.
◆ usbfs_now_ms()
|
inlinestatic |
◆ usbfs_open()
|
inlinestatic |
◆ usbfs_parse_name()
|
inlinestatic |
◆ usbfs_read_number()
|
inlinestatic |
Read one bounded unsigned numeric sysfs attribute.
- Parameters
-
device Sysfs device directory name. attribute Attribute basename. base Number base, 10 or 16 for supported callers. maximum Largest acceptable value. [out] value Result written only on success.
- Returns
- True if the attribute could be read and parsed in range.
Allow the newline used by sysfs, but reject signs and nonnumeric prefixes before conversion.
◆ usbfs_release_interface()
|
inlinestatic |
◆ usbfs_same_device()
|
inlinestatic |
Compare a previously discovered USB identity with current sysfs.
- Parameters
-
device Device snapshot with sysfs name, bus, address and IDs.
- Returns
- True if the checked attributes still match.
Recheck after opening because disconnects and re-enumeration can invalidate a previously discovered bus address.
◆ usbfs_select_config()
|
inlinestatic |
◆ usbfs_set_alternate()
|
inlinestatic |
Select an alternate setting for an interface.
- Parameters
-
handle Serialized device handle. interface Nonnegative USB interface number. alternate Nonnegative alternate-setting number.
- Returns
- USBFS_SUCCESS or a negative wrapper error.
The caller must pair the setting with its matching endpoint descriptors.