drivers/hid/usbhid/hiddev.c
Source file repositories/reference/linux-study-clean/drivers/hid/usbhid/hiddev.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hid/usbhid/hiddev.c- Extension
.c- Size
- 22711 bytes
- Lines
- 946
- Domain
- Driver Families
- Bucket
- drivers/hid
- Inferred role
- Driver Families: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/poll.hlinux/slab.hlinux/sched/signal.hlinux/module.hlinux/init.hlinux/input.hlinux/usb.hlinux/hid.hlinux/hiddev.hlinux/compat.hlinux/vmalloc.hlinux/nospec.husbhid.h
Detected Declarations
struct hiddev_listfunction REPORT_ID_FIRSTfunction hiddev_lookup_usagefunction list_for_each_entryfunction hiddev_send_eventfunction hiddev_hid_eventfunction hiddev_report_eventfunction hiddev_fasyncfunction hiddev_releasefunction __hiddev_openfunction hiddev_openfunction hiddev_writefunction hiddev_readfunction hiddev_pollfunction hiddev_ioctl_usagefunction hiddev_ioctl_stringfunction hiddev_ioctlfunction hiddev_connectfunction hiddev_disconnectexport hiddev_hid_event
Annotated Snippet
static const struct file_operations hiddev_fops = {
.owner = THIS_MODULE,
.read = hiddev_read,
.write = hiddev_write,
.poll = hiddev_poll,
.open = hiddev_open,
.release = hiddev_release,
.unlocked_ioctl = hiddev_ioctl,
.fasync = hiddev_fasync,
.compat_ioctl = compat_ptr_ioctl,
.llseek = noop_llseek,
};
static char *hiddev_devnode(const struct device *dev, umode_t *mode)
{
return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
}
static struct usb_class_driver hiddev_class = {
.name = "hiddev%d",
.devnode = hiddev_devnode,
.fops = &hiddev_fops,
.minor_base = HIDDEV_MINOR_BASE,
};
/*
* This is where hid.c calls us to connect a hid device to the hiddev driver
*/
int hiddev_connect(struct hid_device *hid, unsigned int force)
{
struct hiddev *hiddev;
struct usbhid_device *usbhid = hid->driver_data;
int retval;
if (!force) {
unsigned int i;
for (i = 0; i < hid->maxcollection; i++)
if (hid->collection[i].type ==
HID_COLLECTION_APPLICATION &&
!IS_INPUT_APPLICATION(hid->collection[i].usage))
break;
if (i == hid->maxcollection)
return -EINVAL;
}
if (!(hiddev = kzalloc_obj(struct hiddev)))
return -ENOMEM;
init_waitqueue_head(&hiddev->wait);
INIT_LIST_HEAD(&hiddev->list);
spin_lock_init(&hiddev->list_lock);
mutex_init(&hiddev->existancelock);
hid->hiddev = hiddev;
hiddev->hid = hid;
hiddev->exist = 1;
retval = usb_register_dev(usbhid->intf, &hiddev_class);
if (retval) {
hid_err(hid, "Not able to get a minor for this device\n");
hid->hiddev = NULL;
kfree(hiddev);
return retval;
}
/*
* If HID_QUIRK_NO_INIT_REPORTS is set, make sure we don't initialize
* the reports.
*/
hiddev->initialized = hid->quirks & HID_QUIRK_NO_INIT_REPORTS;
hiddev->minor = usbhid->intf->minor;
return 0;
}
/*
* This is where hid.c calls us to disconnect a hiddev device from the
* corresponding hid device (usually because the usb device has disconnected)
*/
static struct usb_class_driver hiddev_class;
void hiddev_disconnect(struct hid_device *hid)
{
struct hiddev *hiddev = hid->hiddev;
struct usbhid_device *usbhid = hid->driver_data;
usb_deregister_dev(usbhid->intf, &hiddev_class);
mutex_lock(&hiddev->existancelock);
hiddev->exist = 0;
Annotation
- Immediate include surface: `linux/poll.h`, `linux/slab.h`, `linux/sched/signal.h`, `linux/module.h`, `linux/init.h`, `linux/input.h`, `linux/usb.h`, `linux/hid.h`.
- Detected declarations: `struct hiddev_list`, `function REPORT_ID_FIRST`, `function hiddev_lookup_usage`, `function list_for_each_entry`, `function hiddev_send_event`, `function hiddev_hid_event`, `function hiddev_report_event`, `function hiddev_fasync`, `function hiddev_release`, `function __hiddev_open`.
- Atlas domain: Driver Families / drivers/hid.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.