ioctl Device Example
Description
Character device demonstrating ioctl (input/output control) commands for device configuration.
Features
- Reset device to default state
- Get/set single values
- Get/set complex configuration structures
- Input validation
- Shared header between kernel and userspace
Build
make # Builds both kernel module and test program
Test
# Load module
sudo insmod ioctl_device.ko
# Run test program
./test_ioctl
# Expected output shows all tests passing
# Unload module
sudo rmmod ioctl_device
Manual Testing
# You can also test manually using the test program
./test_ioctl
# Or use ioctl from other programs
# See test_ioctl.c for example usage
ioctl Commands
| Command | Type | Description |
|---|---|---|
| IOCTL_RESET | _IO | Reset device to defaults |
| IOCTL_GET_VALUE | _IOR | Read current value |
| IOCTL_SET_VALUE | _IOW | Write new value (0-1000) |
| IOCTL_GET_CONFIG | _IOR | Read full configuration |
| IOCTL_SET_CONFIG | _IOW | Write full configuration |
Configuration Structure
struct ioctl_config {
int value; // Device value (0-1000)
int speed; // Speed setting
char name[32]; // Device name
bool enabled; // Enable flag
};
Learning Points
- ioctl command encoding (_IO, _IOR, _IOW)
- Magic numbers for command validation
- Copying data to/from userspace
- Input validation
- Shared header files