driver_struct
2016-08-25 14:58:31 0 举报
AI智能生成
`driver_struct`是一个在Linux内核中定义的结构体,主要用于描述和操作设备驱动程序。它包含了许多字段,如设备名称、设备ID、设备类、设备方法等,这些字段可以帮助内核管理和控制设备。此外,`driver_struct`还包含了一些用于处理设备中断的方法,如`open`、`release`、`read`、`write`等。通过这个结构体,设备驱动程序可以与内核进行交互,实现对设备的控制和管理。同时,`driver_struct`也是设备驱动程序的核心数据结构,它包含了驱动程序的所有信息,是驱动程序运行的基础。
作者其他创作
大纲/内容
Linux 内核增加程序
复制代码到内核源代码对应的位置
在目录Kconfig文件中增加编译选项
菜单入口
菜单结构
menu
...子菜单
endmenu
...子菜单
endmenu
依赖关系生成菜单
config MODULES
bool "enable loadable modules support"
config MODVERSIONS
bool "set version information on all modules sysmbols"
depends on MODULES
bool "enable loadable modules support"
config MODVERSIONS
bool "set version information on all modules sysmbols"
depends on MODULES
通过依赖生成菜单结构
Makefile 增加源代码的编译条目
目标定义
obj-y +=foo.o
foo.c or foo.s 编译得到foo.o并链接进内核
obj-m +=foo.o
编译成模块foo.ko
多文件模块的定义
obj-$(CONFIG_EXT2_FS) +=ext2.o
ext2-y :=foo.o balloc.o或 ext2-objs := foo.o balloc.o
ext2-$(CONFIG_EXT2_FS_ATTR) +=xattr.o
ext2-y :=foo.o balloc.o或 ext2-objs := foo.o balloc.o
ext2-$(CONFIG_EXT2_FS_ATTR) +=xattr.o
balloc.o 和foo.o 编译生成ext2.o
ext2.o生成ext2.ko 或直接链接到内核中
xattr.o是否编译到ext2.o取决 CONFIG_EXT2_FS_ATTR
目录层次的迭代
obj-$(CONFIG_EXT2_FS) +=ext2/
kbuild 把ext2 列入向下迭代的目标中
模块的编译
make -C /xxx/kernel -M=/xxx/modules_dir modules
Linux 内核模块简介
insmod
modprobe
lsmod
modinfo <模块名>
rmmod or modprobe -r <filename>
modprobe
lsmod
modinfo <模块名>
rmmod or modprobe -r <filename>
linux 内核模块程序结构
模块加载函数(必须)
insmod or modprobe
模块卸载函数(必须)
rmmod or modprobe -r
模块许可声明(必须)
模块参数(可选)
模块导出符号(可选)
EXPORT_SYMBOL
/proc/kallsyms
模块作者等信息声明(可选)
linux 文件操作
文件操作系统调用
open close write read umask
c 库函数的文件操作
fopen fclose fread fwrite fseek ftell fgetpos fsetpos
文件系统和设备驱动
1.应用程序
open
2.系统调用
file system --->/usr/include/asm/unistd_32.h(#define __NR_open 5)
kernel ----> kernel\arch\x86\kernel\syscall_table_32.S(ENTRY(sys_call_table))---> .long sys_open /* 5*/
指针函数
指针函数
kernel---> kernel\fs\open.c SYSCALL_DEFINE3(open, const char __user *, filename, int, flags, int, mode)
== asmlinkage long sys_open(const char __user *filename, int flags, umode_t mode);
kernel---> kernel\fs\open.c SYSCALL_DEFINE3(read, unsigned int, fd, char __user *, buf, size_t, count)
== asmlinkage long sys_read(unsigned int fd, char __user *buf, size_t count);
3.virtual file system(VFS)
kernel---> kernel\fs\read_write.c ret = vfs_read(file, buf, count, &pos);
4.设备驱动
一个打开的文件(设备对应设备文件)
kernel\include\linux\fs.h
struct file
磁盘文件
inode 译成中文就是索引节点。每个存储设备或存储设备的分区(存储设备是硬盘、软盘、U盘 ... ... )被格式化为文件系统后,应该有两部份,一部份是inode,另一部份是Block,Block是用来存储数据用的。而inode呢,就是用来存储这些数据的信息,这些信息包括文件大小、属主、归属的用户组、读写权限等。inode为每个文件进行信息索引,所以就有了inode的数值。操作系统根据指令,能通过inode值最快的找到相对应的文件
设备文件系统
1. /sys/bus
pci
drivers
devices
2. /sys/drivers
3. /sys/devices
2. 符号链接
4. /sys/class
2. 符号链接
Linux 设备模型组件
struct device
描述设备相关的设备信息之间的层次,设备与总线,驱动之间的关系
/sys/devices
struct device_driver
系统中的每个驱动程序
struct bus_type
系统中的总线
/sys/bus/
/sys/bus/devices
描述在该总线上的所有设备
/sys/bus/drivers
与总线相关联的所有驱动程序
struct class
一类设备
/sys/class
struct class_device
表示逻辑设备,并且指向一个物理设备(struct device* dev)
struct cdev
字符设备
0 条评论
下一页