Linux Kernel learning-MISC miscellaneous Device Driver

Source: Internet
Author: User

The MISC miscellaneous device in Linux is a driver with the master device Number 10. It is easy to register and use, so it is suitable for devices with simple functions.

It has its own device structure:

Struct miscdevice {
Int minor;
Const char * Name;
Const struct file_operations * fops;
Struct list_head list;
Struct device * parent;
Struct device * this_device;
Const char * nodename;
Mode_t mode;
};
It is defined in the header file Linux/miscdevice. h,

Minor is the sub-device number of the misc device. The MISC device depends mainly on minor. If it is set to misc_dynamic_minor, the system automatically allocates unused minor.

Nodename is a device driver node created under/dev,

Fops is the entry pointer to the main processing function.

 

The main functions used are:

Int misc_register (struct miscdevice * MISC );
Int misc_deregister (struct miscdevice * MISC );

Misc01.c File

# Include <Linux/init. h> # include <Linux/module. h> # include <Linux/Fs. h> # include <Linux/miscdevice. h> ////////////////////////////////////// ///////// module_license ("dual BSD/GPL "); int open_state = 0; //////////////////////////////////////// //// // int misc01_open (struct inode * inode, struct file * filp) {If (open_state = 0) {open_state = 1; printk ("misc01 open! \ N "); Return 0;} printk (" misc01 has been open! \ N "); Return-1;} int misc01_release (struct inode * inode, struct file * filp) {If (open_state = 1) {open_state = 0; printk ("misc01 release! \ N "); Return 0;} printk (" misc01 has not been open yet! \ N "); Return-1;} ssize_t misc01_read (struct file * filp, char * Buf, size_t count, loff_t FPOs) {printk (" misc01 read! \ N "); Return 0;} ssize_t misc01_write (struct file * filp, char * Buf, size_t count, loff_t FPOs) {printk (" misc01 write! \ N "); Return 0;} int misc01_ioctl (struct inode * inode, struct file * filp, unsigned int cmd, unsigned long Arg) {printk (" IOCTL is called! \ N "); printk (" cmd: % D Arg: % d \ n ", CMD, ARG); Return 0 ;} //////////////////////////////////////// /// // struct file_operations fops = {. owner = this_module ,. open = misc01_open ,. release = misc01_release ,. write = misc01_write ,. read = misc01_read ,. IOCTL = misc01_ioctl}; struct miscdevice Dev = {. minor = misc_dynamic_minor ,. foPs = & FOPS ,. name = "misc01 ",. nodename = "misc01_node"}; int setup_misc0 1 (void) {return misc_register (& Dev );} //////////////////////////////////////// /// // static int _ init misc01_init (void) {printk ("misc01 Init! \ N "); Return setup_misc01 ();} static void _ exit misc01_exit (void) {printk (" misc01 exit! \ N "); misc_deregister (& Dev );} //////////////////////////////////////// /////// module_init (misc01_init ); module_exit (misc01_exit );

Makefile

 
# Makefileobj-M: = misc01.opwd: = $ (shell PWD) k_dir: =/lib/modules/$ (shell uname-R)/buildall: $ (make) -C $ (k_dir) M = $ (PWD) modulesclean: $ (make)-C $ (k_dir) M = $ (PWD) cleantest: misc01_test.ogcc-o $ @ {1} lt;

Misc01_test.c File

 
# Include <stdio. h> # include <sys/STAT. h> # include <sys/IOCTL. h> # include <fcntl. h> # include <errno. h> ////////////////////////////////////// //// // int main (INT argc, char ** argv) {int FD; FD = open ("/dev/misc01_node", o_rdonly); If (FD <0) {printf ("Open/dev/misc01_node failed! \ N "); printf (" % s \ n ", strerror (errno); Return-1;} printf (" Open/dev/misc01_node OK! \ N "); If (IOCTL (FD, 6 )! = 0) {printf ("IOCTL failed! \ N "); printf (" % s \ n ", strerror (errno);} else printf (" IOCTL OK! \ N "); close (FD); Return 0 ;}

Related Article

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.