Welcome to the first article of the book. The article is named after USB class and Linux facility Human Interface Devices (HID). HID facility in Linux supports keyboard, mouse and other input devices. Console, multimedia (or just media), sound (audio), video, graphics also are in the focus of this article. Security and debugging topics are very related to user and human interactions so are described here too.
Char devices
A character device file is one of the most simple, fundamental and oldest concepts derived from UNIX.
This kind of devices is used in many facilities from keyboard input to default interface for a sophisticated new driver.
You can find a list of registered char devices on the beginning the listing of /proc/devices
.
Key function cdev_add idadds a char device to the system.
Tip: Browse the cross-referencing site to explore nearby API and use cases
β² API:
register_chrdev id,
unregister_chrdev id
alloc_chrdev_region id/
register_chrdev_region id,
unregister_chrdev_region id
βοΈ Internals:
π References
- Char devices doc
- Character device drivers, linux-kernel-labs
- Character device files, on opensourceforu
πΎ Historical
Security
The purpose of security is to restrict access across interfaces. One of the most frequently executed functions is may_open id . It rejects access of unauthorized users to open a file. It uses common function inode_permission id which check for access rights to a given inode.
The most common functions for changing ownership and access rights (modes) to files:
- man 2 chown βͺ do_fchownat id,
- man 2 chmod βͺ do_fchmodat id.
β² API, π§ TODO:
- man 2 access, man 2 faccessat βͺ do_faccessat id
- man 2 fcntl βͺ do_fcntl id
- man 2 setuid βͺ __sys_setuid id
- man 2 setgid
- man 2 seteuid
- man 2 setresuid
- man 2 setfsuid
- man 2 seccomp
- man 2 bpf
- man 5 acl
- man 2 add_key
- man 7 cgroups
- EPERM id - common error "Operation not permitted"
- EACCES id - common error "Permission denied"
Credentials
β² API:
- linux/cred.h inc
- struct cred id - the security context of a task
π References
Cryptography
β² API:
- linux/crypto.h inc - Scatterlist Cryptographic API.
- crypto inc
βοΈ Internals:
- crypto src
- drivers/crypto src
- lib/crypto src
- arch/x86/crypto src
- fs/crypto src - per-file encryption
- fs/ecryptfs src eCrypt FS - Encrypted filesystem that operates on the VFS layer.
π References
π References
Debugging
β² User-mode debug interfaces
- man 1 dmesg
- man 5 core
- man 2 ptrace
- man 2 syslog
- man 3 klogctl
- /sys/kernel/debug/ - debugfs
- dmesg --console-level <level>
- gdb /usr/src/linux/vmlinux /proc/kcore
βοΈ Internals
π References
β² Kernel-mode debug facilities
Printing to log
Most common functions
- pr_devel id- conditional debug-level message
- pr_debug id- conditional debug-level or dynamic docmessage
- β¨ Example:
- echo "module atkbd +pfl" | sudo tee /sys/kernel/debug/dynamic_debug/control
π§ TODO. More ...
- Message logging with printk doc
- pr_cont id- continues a previous log message in the same line
- Log messages with other levels:
- print_hex_dump_bytes id
- print_hex_dump_debug id
- linux/printk.h inc
- printk id
- dump_stack id
βοΈ Internals
- kernel/printk/printk.c src
- arch/x86/kernel/traps.c src
- lib/dump_stack.c src
- kernel/trace src
- scripts/tracing/draw_functrace.py src
kgdb and kdb
β² Interfaces
βοΈ Internals
π References
π References
Subsystems
Advanced Linux Sound Architecture - ALSA
β² API:
- snd_card id - central struct
- snd_card_new id
- snd_card_register id
- snd_device_ops id
- snd_device_new id creates an ALSA device component
π References
Video4Linux - V4L2
β² API:
- v4l2_device_register id registers v4l2_device id
- video_register_device id registers video_device id
- examples drivers/media/test-drivers src
π References
HID
Generic human interface devices
Input devices
Classic input devices are keyboard and mouse.
β² API:
- linux/input.h inc
- devm_input_allocate_device id, input_register_device id input_dev id
- input_report_key id input_sync id
π For example drivers/input/mousedev.c src
β¨ Hands on:
sudo hexdump /dev/input/mice # dump your mouse movements events from your kernel
βοΈ Internals:
π References
HID devices
π§ TODO
β² API:
- hid_device id - device report descriptor. Operations: hid_allocate_device id, hid_add_device id . π Example usbhid_probe id
- uapi/linux/hid.h inc
- linux/hid.h inc
hiddev
USB HID devices (from the USB specification standpoint) that aren't direct human interface devices, like monitor controls and Uninterruptible Power Supplies. This module supports these devices separately using a separate event interface on /dev/usb/hiddevX (char 180:96 to 180:111) (βοΈ HIDDEV_MINOR_BASE id)
β² API:
βοΈ Internals: drivers/hid/usbhid/hiddev.c src, hiddev_fops id
π References: HIDDEV - Care and feeding of your Human Interface Devices doc
Console
β² API:
- register_console id obviously registers console id
- example virtio_console id
- include/linux/console.h src
- man 2 ioctl_console
βοΈ Internals:
π References
Sound infrastructure
β² API:
- /proc/asound/cards, /sys/class/sound/
- sound/soc.h inc - ALSA System on Chip (ASoC) layer for embedded system-on-chip processors
- snd_soc_card id
- is registered by devm_snd_soc_register_card id βΎ snd_soc_register_card id
- has links to snd_soc_dai_link id
- snd_soc_card id
π References
Video
π§ TODO
β² API:
π References
HI device drivers
The scope of this paragraph is about low level specific human interface drivers.
β² HID API:
βοΈ Internals
- hid_bus_type id
- drivers/hid/hid-core.c src
- drivers/accessibility src
- samples/uhid/uhid-example.c src example of user mode HID driver
π References
- drivers/hid src
- drivers/input src : keyboard & mouse, misc, serio, tablet, touchscreen, gameport, joystick
- β¨ Hands on:
- echo "module atkbd +pfl" | sudo tee /sys/kernel/debug/dynamic_debug/control
USB HID
βοΈ Internals:
π References
Multimedia
π§ TODO
ποΈ Acronyms:
- FB - Frame buffer
- GPU - Graphics processing unit
- DRM - Direct Rendering Manager
- MIPI - Mobile Industry Processor Interface
- DBI - Display Bus Interface
- DSI - Display Serial Interface
- DCS - The Display Command Set
β² API:
- /proc/fb/
- /sys/class/graphics/
- /sys/class/drm/
- linux/fb.h inc
- uapi/video inc
- video inc
- drm inc
- register_framebuffer id
- drm_dev_register id registers drm_device id
βοΈ Internals
π References:
Sound
π References
- sound src
- SAI could be
- STM Serial Audio Interface: sound/soc/stm/stm32_sai.h src
- Freescale (FSL) Synchronous Audio Interface: sound/soc/fsl/fsl_sai.h src
- SAI could be
- drivers/media src