#include <linux/module.h> #include <linux/fs.h> #include <linux/sched.h> #include "ioctl_led.h" # Define Dev_name "Test-dev" volatile bool empty = true;//A pointer variable that defines a process resource struct task_struct *task;int test_open (struct inode * Inode, struct file *filp) {PRINTK ("Test open\n"); return 0;} int test_close (struct inode *inode, struct file *filp) {PRINTK ("Test close\n"); return 0;} ssize_t test_read (struct file *filp, char __user *buf, size_t size, loff_t *off) {int ret;//If true, start reading while (empty) {//F_FL AGS refers to the Flag//o_nonblock non-blocking form that is specified when the corresponding open call is opened if (Filp->f_flags & o_nonblock) {return 0;} Current (point to Task_struct)//(bottom thread_info.task of kernel stack) task = current;//Sets the status of the current process to Task_interruptible//task_ Interruptible is a blocking state, and the process is currently waiting for other system resources other than the CPU to be awakened by the signal. Set_current_state (task_interruptible);//notifies the scheduler to perform the dispatch. Schedule (); if (signal_pending (current)) RETURN-ERESTARTSYS;//RETURN-EAGAIN;PRINTK ("Read:wake up\n");} ret = Size;empty = True;return ret;} ssize_t test_write (struct file *filp, const char __user *buf, size_t size, loff_t *off) {int ret;empty = false;wake_up_process (Task); ret = Size;return ret;} int major = 0;struct File_operations FoPs = {. open = Test_open,.release = Test_close,.read = Test_read,.write = Test_write ,};//module, int test_init (void) {int ret;printk ("Test init\n");//Register a character device driver ret = Register_chrdev (Major, Dev_name, & FoPs); if (Ret < 0) return ret;else{if (0 = = Major) {major = RET;PRINTK ("major =%d\n", major);}} return 0;} void Test_exit (void) {PRINTK ("Test exit\n");//undo Character Device Unregister_chrdev (major, Dev_name);} Module_init (Test_init); Module_exit (Test_exit); Module_license ("GPL"); Module_author ("Yang.yx"); Module_version ("1.1");
Makefile
obj-m+= test.orootfs =/rootfskernel_src=/lib/modules/' uname-r '/buildall:make-c $ (kernel_src) M= ' pwd ' Modulesclean: Make-c $ (kernel_src) m= ' pwd ' cleanrm-rf appinstall:make-c $ (kernel_src) m= ' pwd ' Modules_install install_mod_path=$ (ROO TFS) APP:ARM-LINUX-GCC App.c-o app
Ioctl.c
#ifndef__IOCTL_H__ # define __ioctl_h__#include <linux/ioctl.h> #define Led_type0x1#define led_allon_io (led_ TYPE, 0) #define LED_ALLOFF_IO (Led_type, 1) #define LED_ON_IOW (Led_type, 2, int) #define LED_OFF_IOW (Led_type, 3, int) # endif
Linux device driver-wait queue implementation