drivers/hid/hidraw.c
Source file repositories/reference/linux-study-clean/drivers/hid/hidraw.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/hid/hidraw.c- Extension
.c- Size
- 16166 bytes
- Lines
- 711
- 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/fs.hlinux/module.hlinux/errno.hlinux/kernel.hlinux/init.hlinux/cdev.hlinux/poll.hlinux/device.hlinux/major.hlinux/slab.hlinux/hid.hlinux/mutex.hlinux/sched/signal.hlinux/string.hlinux/hidraw.h
Detected Declarations
function hidraw_is_revokedfunction hidraw_readfunction hidraw_send_reportfunction hidraw_writefunction hidraw_get_reportfunction hid_hw_raw_requestfunction hidraw_pollfunction hidraw_openfunction hidraw_fasyncfunction drop_reffunction hidraw_releasefunction hidraw_revokefunction hidraw_fixed_size_ioctlfunction hidraw_rw_variable_size_ioctlfunction hidraw_ro_variable_size_ioctlfunction hidraw_ioctlfunction hidraw_report_eventfunction hidraw_connectfunction hidraw_disconnectfunction hidraw_initfunction hidraw_exitexport hidraw_report_eventexport hidraw_connectexport hidraw_disconnect
Annotated Snippet
static const struct file_operations hidraw_ops = {
.owner = THIS_MODULE,
.read = hidraw_read,
.write = hidraw_write,
.poll = hidraw_poll,
.open = hidraw_open,
.release = hidraw_release,
.unlocked_ioctl = hidraw_ioctl,
.fasync = hidraw_fasync,
.compat_ioctl = compat_ptr_ioctl,
.llseek = noop_llseek,
};
int hidraw_report_event(struct hid_device *hid, u8 *data, int len)
{
struct hidraw *dev = hid->hidraw;
struct hidraw_list *list;
int ret = 0;
unsigned long flags;
spin_lock_irqsave(&dev->list_lock, flags);
list_for_each_entry(list, &dev->list, node) {
int new_head = (list->head + 1) & (HIDRAW_BUFFER_SIZE - 1);
if (hidraw_is_revoked(list) || new_head == list->tail)
continue;
if (!(list->buffer[list->head].value = kmemdup(data, len, GFP_ATOMIC))) {
ret = -ENOMEM;
break;
}
list->buffer[list->head].len = len;
list->head = new_head;
kill_fasync(&list->fasync, SIGIO, POLL_IN);
}
spin_unlock_irqrestore(&dev->list_lock, flags);
wake_up_interruptible(&dev->wait);
return ret;
}
EXPORT_SYMBOL_GPL(hidraw_report_event);
int hidraw_connect(struct hid_device *hid)
{
int minor, result;
struct hidraw *dev;
/* we accept any HID device, all applications */
dev = kzalloc_obj(struct hidraw);
if (!dev)
return -ENOMEM;
result = -EINVAL;
down_write(&minors_rwsem);
for (minor = 0; minor < HIDRAW_MAX_DEVICES; minor++) {
if (hidraw_table[minor])
continue;
hidraw_table[minor] = dev;
result = 0;
break;
}
if (result) {
up_write(&minors_rwsem);
kfree(dev);
goto out;
}
dev->dev = device_create(&hidraw_class, &hid->dev, MKDEV(hidraw_major, minor),
NULL, "%s%d", "hidraw", minor);
if (IS_ERR(dev->dev)) {
hidraw_table[minor] = NULL;
up_write(&minors_rwsem);
result = PTR_ERR(dev->dev);
kfree(dev);
goto out;
}
init_waitqueue_head(&dev->wait);
spin_lock_init(&dev->list_lock);
INIT_LIST_HEAD(&dev->list);
dev->hid = hid;
dev->minor = minor;
dev->exist = 1;
Annotation
- Immediate include surface: `linux/fs.h`, `linux/module.h`, `linux/errno.h`, `linux/kernel.h`, `linux/init.h`, `linux/cdev.h`, `linux/poll.h`, `linux/device.h`.
- Detected declarations: `function hidraw_is_revoked`, `function hidraw_read`, `function hidraw_send_report`, `function hidraw_write`, `function hidraw_get_report`, `function hid_hw_raw_request`, `function hidraw_poll`, `function hidraw_open`, `function hidraw_fasync`, `function drop_ref`.
- 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.