Structure Framework of drivers

Source: Internet
Author: User

/*************************************** * *********************************** 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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.