Summary - Driver basics
- C course
- Expert
- Lesson 12/47
- 5 min
Combine module structure and fops to write a character driver skeleton.
Summary - Driver basics
This group covered the basics of writing a kernel module: loading with insmod/rmmod, the module_init/module_exit skeleton, logging with printk, the device number dev_t, and the user-facing file_operations.
On load, the init function runs, takes resources and registers a device number. When the user opens the /dev file, .open fires; reads and writes go through .read and .write. Now you'll write a mini task that combines the five concepts.
Practice Print driver info
Define void print_driver_info(const char *topic, const char *detail) with body printf("%s -> %s\n", topic, detail);. In main call it for five topics: ("insmod", "load"), ("module_init", "start"), ("printk", "log"), ("dev_t", "major:minor"), ("fops", "open/read/write").
Starter code
#include <stdio.h>
// Define print_driver_info function
// Print five topics: insmod, module_init, printk, dev_t, fops
int main(void) {
return 0;
}Sample solution
#include <stdio.h>
void print_driver_info(const char *topic, const char *detail) {
printf("%s -> %s\n", topic, detail);
}
int main(void) {
print_driver_info("insmod", "load");
print_driver_info("module_init", "start");
print_driver_info("printk", "log");
print_driver_info("dev_t", "major:minor");
print_driver_info("fops", "open/read/write");
return 0;
}Expected output
insmod -> load module_init -> start printk -> log dev_t -> major:minor fops -> open/read/write
Driver basics done
Congratulations! You've finished the driver basics. You saw the whole skeleton of writing a kernel module: loading, init/exit, logging, device numbers and file operations. You can now lay out a simple character driver.
The next group moves to compiler construction: lexer, parser, AST, code generation and symbol table. You'll build a tool that analyses code; the C skills you learned for drivers apply there too. Continue when ready.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When is a module's init function called?
- On removal
- Only at build time
- On every read call
- On load Correct answer
Explanation: The module_init function runs on load.
2 Match five driver concepts
| Term | Meaning |
|---|---|
insmod |
Loads a module |
module_init |
Initialization |
printk |
Writes a log |
dev_t |
Device number |
fops |
User interface |