/*************************************** * *********************************** Driver framework 1. Write led_open, led_read2, how to tell kernel a, define a file_operationsb, tell kernel register_chrdevc about this structure, and who will call callback (this is called the driver's entry function) d. Modify ************************************* **************************************** * *************/# include <linux/module. h> # include <linux/kernel. h> # include <linux/fs. h> # include <linux/init. h> # include <linux/delay. h> # include <asm/uaccess. h> # include <asm/irq. h> # include <asm/io. h> # include <asm/arch/regs-gpio.h> # include <asm/hardware. h>/* Open the device function */static int my_led_open (struct inode * inode, struct file * file) {printk ("my_led here \ n"); return 0 ;} /* device write function */static ssize_t my_led_write (struct file * file, const char _ usr * buf, size_t count, loff_t * ppos) {printk ("my_led out \ n"); return 0;}/* defines a file_operations structure */static struct file_operations my_led_fops = {. owner = THIS_MODULE,/* This is a macro, which is automatically created when the module is pushed to compile */. open = my_led_open ,. open = my_led_write,}; int my_led_init (void) // entry function {register_chrdev (111, "my_led", & my_led_fops); // register the driver return 0 ;} int my_led_exit (void) // exit function {unregister_chrdev (111, "my_led"); // deregister the driver return 0;} module_init (my_led_init ); // modify (a macro is used to define a struct) module_exit (my_led_init); // modify (a macro is used to define a struct)
#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>int main(int argc,char **argv){int fd;int val = 1;fd = open("/dev/my_led",O_RDWR);if(fd<0)printf("open err\n");write(fd,&val,4);return 0;}