版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、CHP8 Linux Device Management and Driver,The main task of Linux device management is to control the device to complete the input and output operation, so it is also called the input and output (I/O) subsystem. Its task is to shield the details of the complex physical characteristics of various device
2、s and hardware, and to provide an interface to operate different devices in a unified way. Linux regards devices as special files. The system manages and controls all kinds of devices through VFS, the interface of processing files.,1. Classification of Linux Devices,There are three types of Linux de
3、vices: 1)Character devices(字符设备) Character devices usually refer to devices that read and write in byte order like ordinary files, such as parallel port devices, virtual consoles, etc. Character devices can be accessed through device file nodes. The difference between them and ordinary files is that
4、 ordinary files can be accessed randomly (pointers can be moved back and forth), while most character devices can only provide sequential access, because they usually do not use buffer to read and write.,8.1 Overview of Linux Device Management and Drivers,3,2)Block devices(块设备) Their input and outpu
5、t data are in units of block(certain size). Such as disk, CD, etc. Block devices are also accessed through file nodes, which can not only provide random access, but also accommodate file systems (such as hard disks, flash memory, etc.). 3)Network devices(网络设备) Theyre devices that transmit data throu
6、gh communication networks, generally referring to network adapters (network cards) connected to communication networks. For example, ethernet card(以太网卡), Linux uses socket to provide access to network data in the form of I/O file. The communication between the kernel and the network device driver ca
7、lls a set of data package processing functions, which are completely different from the communication between the kernel and characters device driver , block device driver (read (), write ().,4,The device number The device number is a number, which is the symbol of the device. As mentioned earlier,
8、a device file (that is, the device node) can be created by the mknod command, which specifies the major device number(主设备号) and the minor device number(次设备号). The major device number indicates the type of device (e.g. serial device, SCSI hard disk) corresponding to a determined driver. The minor dev
9、ice number is usually used to indicate different attributes, such as different usage methods, different locations, different operations, etc., which indicates a specific physical device.,$ ls l /dev crw-rw- 1 root uucp 4, 64 08-30 22:58 ttyS0 /*串口设备, c表示字符设备*/ brw-r- 1 root floppy 2, 0 08-30 22:58 f
10、d0 /*软盘设备,b表示块设备*/,5,2. Function of device driver,Device driver is composed of: Device service subroutine The device service subroutine includes the code to perform various operations on the device. Interrupt processing program. The interrupt processing subroutine handles the device interrupt. The m
11、ain functions of device driver : Initialize the device. Start or stop the operation of the device. Transfer data from the device to memory. Transfer data from memory to device. Testing the status of device.,6,3. Implementation of Access Device,Accessing the device consists of two steps: 1) Identific
12、ation of device The identification of device uses device type, major number and minor number. The device type refers to the character device or block device. The major number corresponds to the driver one by one; The minor number is used to distinguish individual devices using the same driver. 2) Op
13、erate the corresponding device files Device files are generally placed in /dev directory, such as: /dev/hda2, /dev/lp0, etc.,7,Linux uses virtual file system VFS as a unified operating interface to process files and devices. Like ordinary directories and files, each device is described by a VFS inod
14、e, which contains the major number and minor number of the device.,4. Control Mode of Linux Device,There are three I/O control modes in Linux: Query waiting mode(查询等待模式); Interrupt mode; DMA (direct memory access) mode.,8,1)Query waiting mode: Query waiting mode is also called polling mode(轮询模式). Fo
15、r machines that do not support interruption, only this way can be used to control the I/O process. So Linux is equipped with query waiting mode. For example, the default control mode in parallel interface driver is query waiting mode. 2) Interrupt mode In the case of hardware support interruption, d
16、river can use interruption mode to control I/O process. When a device needs service, it sends an interrupt pulse signal to the CPU. After receiving the signal, the CPU starts the interrupt service routine according to the interrupt request number IRQ.,9,In interrupt mode, an important task of Linux
17、device management is to execute the interrupt service routine of the device driver after the CPU receives the interrupt request. Linux has set up an interrupt routine descriptor table(中断例程描述符表) named irq_action: static struct irqaction *irq_actionNR_IRQS+1; Where NR_IRQS denotes the number of interr
18、upt sources. Irq_action is an array of pointers to the IRQ action structure. The irqaction structure contains descriptors for each devices interrupt service routine.,10,3)DMA mode: DMA is a hardware mechanism that allows bidirectional data transmission between peripherals and system RAM without CPU
19、participation. Using DMA can make the system CPU get rid of the actual I/O data transmission process, thus greatly improving the system throughput(吞吐率). The whole data transmission process of DMA is completed by the DMA Controller (DMAC). In the meantime, the CPU can perform other tasks concurrently
20、. When the DMA is over, the DMAC informs the CPU that the data transmission is over by interruption, and then the CPU executes the corresponding ISR for post-processing.,11,Driving hardware is the most basic function of operating system, operating system controls hardware devices through various dri
21、vers, which shield users from various devices and provides a unified mode of operation. Device driver is the most basic part of the operating system. It accounts for more than 60% of the Linux kernel source programs.,8.2 Development of Linux Device Driver,12,Kernel code: The device driver is part of
22、 the kernel, and if the driver fails, it may cause the system to crash. 2) Kernel interface: The device driver must provide a standard interface for the kernel or its subsystems. For example, a terminal driver must provide a file I/O interface for the kernel; A SCSI device driver should provide a SC
23、SI device interface for the SCSI subsystem, and the SCSI subsystem must also provide the file I/O interface and buffer for the kernel.,1. Characteristics of Device Driver,13,3) Kernel mechanisms and services: Device drivers use some standard kernel services, such as memory allocation. 4) Loadable: M
24、ost Linux device drivers can be loaded into the kernel when needed and unloaded from the kernel when not needed. 5) Settable: Linux device drivers can be integrated into the kernel according to need, which only needs to be set up at system compilation time.,14,6) Dynamics: After the system is starte
25、d and each device driver is initialized, the driver will maintain the device under its control. If the device controlled by the device driver does not exist and does not affect the operation of the system, the device driver only takes up a little more system memory.,2. Hierarchy of the Device Driver
26、,15,3. Interface between Device Driver and the Outside,16,17,The Linux kernel adopts a loadable modular design (LKMs, Loadable Kernel Modules). Generally, the compiled Linux kernel supports pluggable modules. Compile the most basic core code into the kernel. Other code can be compiled into the kerne
27、l or into a module file of the kernel (loaded dynamically when needed). The driver module Common drivers are loaded dynamically as kernel modules, such as sound card driver and network card driver.,4. Driver modules and related commands,18,The most basic drivers of Linux, such as CPU, PCI bus, TCP/I
28、P protocol, APM (Advanced Power Management), VFS and so on, are directly compiled into the kernel files. Drivers content is not necessarily(不一定) hardware, such as ext3 file system driver. Therefore, loading driver is to load the kernel module. A module of the Linux kernel can be compiled and loaded
29、in two ways. It is compiled into the Linux kernel directly and loaded with Linux at startup. Compile it into a module that can be loaded and deleted. Use insmod to load and rmmod to delete. This controls the size of the kernel, and once a module is inserted into the kernel, it is the same as the res
30、t of the kernel.,19,Main Related Commands of Kernel Module lsmod List the modules loaded in the current system, display module name, module size, number of objects using the module. $ lsmod Module Size Used by Autofs 12068 0 (autoclean) (unused) iptable_nat 19252 0 (autoclean) (unused) ip_conntrack
31、18540 1 (autoclean) iptable_nat iptable_mangle 2272 0 (autoclean) (unused) iptable_filter 2272 0 (autoclean) (unused) usb-ohci 19328 0 (unused) usbcore 54528 1 usb-ohci ext3 67728 2 jbd 44480 2 ext3 sd_mod 11584 3 scsi_mod 98512 2 aic7xxx sd_mod,20,Insmod Used to load the current module. But it does
32、 not automatically resolve dependencies, that is, if the module to be loaded refers to symbols that do not exist in the current kernel symbol table, it cannot be loaded, nor does it check whether the symbol is defined in other modules that have not yet been loaded; Modprobe It can automatically load
33、 other dependent modules based on the dependencies between modules and the contents in the /etc/ modules.conf file. Rmmod It is used to uninstall the current module.,21,Device drivers of Linux are organized into a set of functions to accomplish different tasks. These functions enable Linux devices t
34、o operate as files. In the view of the application program, the hardware device is only a device file. The application program can operate the hardware device like ordinary files, such as open (), close (), read (), write (), etc.,5. Character Device Driver,22,The module is loaded when the insmod co
35、mmand is invoked, and the entry point is the init_module() function, which usually completes the device registration. After the device is registered and loaded, the users application program can perform certain operations on the device, such as open (), read (), write (). The module is unloaded when
36、 the rmmod command is invoked, and the entry point is the cleanup_ module () function, in which the device is unloaded.,Important data structure,file_operations structure struct file_operations loff_t (*llseek) (struct file *, loff_t, int); ssize_t (*read) (struct file *filp, char *buff, size_t coun
37、t, loff_t *offp); ssize_t (*write) (struct file *filp, const char *buff, size_t count, loff_t *offp); int (*readdir) (struct file *, void *, filldir_t); unsigned int (*poll) (struct file *, struct poll_table_struct *); int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long); int (*
38、mmap) (struct file *, struct vm_area_struct *); int (*open) (struct inode *, struct file *); int (*flush) (struct file *); int (*release) (struct inode *, struct file *); int (*fsync) (struct file *, struct dentry *); int (*fasync) (int, struct file *, int); int (*check_media_change) (kdev_t dev); i
39、nt (*revalidate) (kdev_t dev); int (*lock) (struct file *, int, struct file_lock *); ; Note: The driver of each device does not have to implement all the function operations in it. If you do not need to define the implementation, just set it to null.,24,Important data structure File structure,struct
40、 file mode_t f_mode; /*标识文件是否可读或可写,FMODE_READ或FMODE_WRITE*/ dev_t f_rdev; /* 用于/dev/tty */ off_t f_pos; /* 当前文件位移 */ unsigned short f_flags; /* 文件标志,如O_RDONLY、O_NONBLOCK和O_SYNC */ unsigned short f_count; /* 打开的文件数目 */ unsigned short f_reada; struct inode *f_inode; /*指向inode的结构指针 */ struct file_opera
41、tions *f_op; /* 文件索引指针 */ ;,Related functions of charactor device driver (1). Open() When an open () system call is called for the device file, the open () function of the driver is called:,25,Int (* open) (struct inode *, struct file *); inode - the pointer to the inode (index node) structure of th
42、e device file, file - the pointer to the file structure of the device. The main tasks of open () : Determine if the hardware is ready; Verify the legity(确认合法性) of the minor device number (the minor device number can be obtained by MINOR (inode - i-rdev); Control the number of processes(进程) using the
43、 device; Return the status code (0 indicates success, negative indicates error) according to the implementation, etc. Devices are usually opened multiple times when they are in use, and can be used by different processes, so if a process wants to delete the device, it must be guaranteed that other p
44、rocesses do not use the device.,26,When the last user process that opens the device executes a close () system call, the kernel calls the release () function of the driver: Void (* release) (struct inode *, struct file *); The main tasks of this function: Clean up the unfinished input and output ope
45、rations; Release resources; Reset user-defined exclusive flags(独占标志), and so on. Difference between release() and close() When a process releases the device, other processes can continue to use the device, just this process temporarily stops using the device. When a process close the device, other p
46、rocesses must reopen the device to use it.,(2). release(),27,Read and write device The main task of read() and write() is to copy data from kernel space to user space,and copy user space data to kernel space.,28,When a read () system call is called for the device file, the driver read () function is
47、 called: Ssize_t(* read) (struct file*, char*, size_t, loff_t*); The read () function is used to read data from the device. When the function pointer is assigned a NULL value, it will cause the read system call to fail and return - EINVAL (illegal parameter). If the return value is non-negative, it
48、indicates the number of bytes successfully read.,(3). read(),29,When a write() system call is called for the device file, the driver write() function is called: Ssize_t(* write) (struct file*, const char*, size_t, loff_t*); The write() function is used to write data to the device. When the function
49、pointer is assigned a NULL value, it will cause the write system call to fail and return - EINVAL (illegal parameter). If the return value is non-negative, it indicates the number of bytes successfully written.,(4). write(),30,Data Exchange between Kernel Space and User Space The functions of copy_t
50、o_user() or copy_from_user() are used to realize data exchange between user space and kernel space.,31,In addition to reading and writing operations, most devices also need hardware configuration and control (for example, setting baud rate of serial devices) and many other operations. In the charact
51、er device driver, the IOCTL function interface provides the user with a non-read-write operation mechanism for the device. This function is a special control function, through which control information can be transmitted to the device or state information can be obtained from the device.,(5). ioctl(
52、),32,(6) Functions about Device Number In Linux 2.6, the device number is described by dev_t type . dev_t : 32-digit high 12 bits - the major device number low 20 bits - the minor device number MAJOR(macro) : Obtain major device number MINOR(macro): Obtain minor device number MKDEV(macro): Combine t
53、he major device number and the minor device number to obtain the dev_t type device number.,33,(7) Registration of character device In the Linux kernel, we use struct CDEV structure to describe character devices. In the driver program, we must assign(赋予) the assigned(已分配到的) device number and device o
54、peration interface (struct file_operations) to the struct CDEV variable. cdev_alloc() function: apply for the allocation of struct CDEV structure to the system cdev_init() function: initialize the allocated structure and relate it to the file_ operations structure. cdev_add() function: relate the de
55、vice number to the struct CDEV structure and formally report the registration of the new device to the kernel so that the new device can be used. cdev_del() function: delete a device from the system,34,35,(8) Obtain memory,36,37,38,39,(9) Print information Printing information is sometimes a good de
56、bugging tool. In kernel space, the function printk() is used instead of the normal function printf(). Printk () can define the priority of printing messages. These different priority messages can output to the system log file and sometimes to the virtual console. Among them, there is a specific prio
57、rity for the information output to the console console. Only when the priority of printing information is less than this integer value can the information be output to the virtual console. Otherwise, the information is only written to the system log file. If no priority option is added, the message
58、is output to the system log file by default.,40,41,Example driver of EEPROM(I2C) Loading and Unloading of Drivers module_init(E2PROM_init); module_exit(E2PROM_exit);,42,static int _init E2PROM_init (void) register_chrdev(IIC_MAJOR, DEVICE_NAME, ,43,void _exit E2PROM_exit(void) devfs_remove(DEVICE_NAME); unregister_chrdev(IIC_MAJOR, DEVICE_NAME) ,44,struct file_operations Fops = read: at24c02_rd, write: at24c02_wr, ioctl: at24c02_ioctl, open: at24c02_open, release: at24c02_release, ;,45,应用程序中 返回值=Open(“设备名”,打开方式) 驱动中 sta
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 珠宝行业智能零售管理平台开发方案
- 协商采购价格达成共识函6篇
- 高级行政管理人员文件守秘管理指导书
- 重庆市江北区2026届初三下学期模拟检测试题语文试题含解析
- 财务预算编制与执行报告模板成本控制型
- 浙江省上杭县2025-2026学年初三入学调研物理试题(2)试卷含解析
- 凝心聚力共促发展承诺书8篇范文
- 2026届四川省遂宁市射洪中学初三(语文试题文)4月第一次综合练习试卷含解析
- 建筑行业安全生产操作指南手册
- (正式版)DB32∕T 2641-2014 《靖江香沙芋生产技术规程》
- 2025年初中劳动技术教师招聘考试测试卷及答案
- 2026广东中山市神湾镇神湾社区居民委员会招聘1人考试备考试题及答案解析
- 《红领巾相约中国梦》课件2025-2026学年湖南文艺版音乐三年级下册
- 2026江苏徐州地铁集团下属运营公司招聘笔试备考题库及答案解析
- 2026甘肃平凉华亭市招聘社区工作者10人考试参考试题及答案解析
- 优先内部采购制度
- 医药招商业务管理制度
- 国开2026年春季《形势与政策》大作业答案
- 2026年南京机电职业技术学院单招职业技能考试题库及答案详解(历年真题)
- (高清正版)JJF(浙)1090—2014薄片千分尺校准规范
- 屏柜安装施工方案.
评论
0/150
提交评论