linux device driver
Contents
kernel modules
Linux kernel Licence
GPLv2
kernel subsystem
Process Managerment
Memory Managermanet
Filesystems
Device control
Networking
Classes of device and modules
char module 1. Character Device
- /dev/console, /dev/ttyS0
- systemcall: open, close, read, write
- diff with regular file
- you can move back and forth in the regular file 1. you can only access sequentially in char device
block module 1. Block Device
- disk
- handle I/O operations
network module 1. Network interfaces
- NIC, loopback
Basic module
|
|
|
|
If you have multi file, then would be
|
|
- parameters
|
|
lsmod
read /proc/modules
info about modules /sys/modules
kernel symbol tables
export by: EXPORT_SYMBOL(“name”); EPORT_SYSBOL_GPL(“name”);
/proc/kallsyms
Char device
dev_t type
defined in <linux/types.h>
12 bits set aside for the major number and 20 for the minor number.
MAJOR(dev_t dev); / MINOR(dev_t dev); / MKDEV(int major, int minor);
register_chrdev_region
<linux/fs.h>
know which major numbers 1.
int register_chrdev_region(dev_t first, unsigned int count, char *name);
Do not know which major numbers 1.
int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name);
void unregister_chrdev_region(dev_t first, unsigned int count);
use alloc_chrdev_region instead of register_chrdev_region
Data Structures
sturct file_operations
struct file 1. <linux/fs.h>
struct inode 1. unsigned int iminor(struct inode *inode); 1. unsigned int imajor(struct inode *inode);
Char Device Registration
<linux/cdev.h>
void cdev_init(struct cdev *cdev, struct file_operations *fops);
int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
void cdev_del(struct cdev *dev);
Memory Usage
<linux/slab.h>
void *kmalloc(size_t size, int flags); (ref: vmalloc) 1. vmalloc: allocates a contiguous memory region in the virtual address space. returns a pointer to a linear memory area of size at least size .
void kfree(void *ptr);
read/write
ssize_t read(struct file *filp, char __user *buff, size_t count, loff_t *offp);
ssize_t write(struct file *filp, const char __user *buff, size_t count, loff_t *offp);
<asm/uaccess.h> 1. unsigned long copy_to_user(void __user *to, const void *from, unsigned long count); 1. unsigned long copy_from_user(void *to, const void __user *from, unsigned long count);
Lock, mutual exclusion
Semaphore 1. mutex 1. read/write semaphore
Spinlocks 1. used in code that can not sleep, such as interrupt handlers. 1. Reader/Writer Spinlocks
seqlocks
Read-Copy-Update(RCU)
Memory
- Memory Zone
- DMA-capable memory, first 16MB of RAM
- normal memory, user space? 0-3G
- low memory, kernel physic address, 0-896M, other 128M are left for I/O dev
- high memory, physic memory that kernel cant mapped directly
Network Driver
- struct net_device
- <linux/netdevice.h>
- struct net_device *alloc_netdev(int sizeof_priv, const char *name, void (*setup)(struct net_device *));
- struct net_device *alloc_etherdev(int sizeof_priv); 1. <linux/etherdevice.h>
- register_netdev() / unregister_netdev() / free_netdev()
- void netif_start_queue(struct net_device *dev);
- void netif_stop_queue(struct net_device *dev);
- hard_start_transmit()
- ether_setup() 1. driver/net/net_init.c
Author Hangbin Liu
LastMod 2018-12-13 (1672b97)