How to compile the Ubuntu character driver /////////////////////////////////// /// // 1. In any directory, create a directory named chardev, and create three new files named Makefilechardev under the directory. cmain. c2. Write the following content to three files respectively. 3. Open the terminal and use the su command to switch to the superuser
How to compile Ubuntu character driver
//////////////////////////////////////// //////////////////////
1. Create a new directory named chardev under any directory,
Create three new files in the directory named Makefile chardev. c main. c.
2. Write the following content into the three files respectively.
3. Open the terminal and run the su command, switch to the super user identity, and write the bit to the chardev directory.
4. Enter the make command to compile the driver chardev. c.
5. Enter the gcc main. c command to compile the test program.
6. Input insmod chardev. ko to load the driver module.
7. Enter gedit/proc/devices to view the master device number of the chardev driver module.
8. Enter mknod/dev/chardev c 250 0 in the/dev/directory to create the chardev device.
Among them, the c-code character drive, 250 is the driver's main device number, 0 is the next device number. Generally, the number of sub-devices is 0.
9. Enter./a. out to run the test program.
10. After the test program exits, enter rmmod chardev to uninstall the driver module.
11. Enter rm-rf/dev/chardev to delete the chardev device in the/dev/directory.
//////////////////////////////////////// //////////////////////
# Makefile
# Make-C $ (KDIR) M = $ (SRCPWD) modules a Tab key is required before this statement.
# Rm-rf chardev. o a Tab key is required before this statement.
#/////////////////////////////////////// ///////////////////////
Obj-m: = chardev. o
KDIR: =/lib/modules/$ (shell uname-r)/build
SRCPWD: = $ (shell pwd)
All:
Make-C $ (KDIR) M = $ (SRCPWD) modules
Clean:
Rm-rf chardev. o
#/////////////////////////////////////// ///////////////////////
// Chardev. c
//////////////////////////////////////// //////////////////////
# Include
# Include
# Include
# Include
# Include
//////////////////////////////////////// //////////////////////
Static int char_read (struct file * filp, char _ user * buffer, size_t, loff_t *);
Static int char_open (struct inode *, struct file *);
Static int char_write (struct file * filp, const char _ user * buffer, size_t, loff_t *);
Static int char_release (struct inode *, struct file *);
//////////////////////////////////////// //////////////////////
// Static int device_open_count;
Static int major;
//////////////////////////////////////// //////////////////////
Static const struct file_operations file_ops = {
. Read = char_read,
. Write = char_write,
. Open = char_open,
. Release = char_release,
};
Static int _ init char_init (void)
{
Int value;
Major = 0;
Value = register_chrdev (major, "chardev", & file_ops );
If (value <0 ){
Printk ("register dev failed! \ N ");
Return value;
}
If (major = 0)
Major = value;
Return 0;
}
Static int char_open (struct inode * inode, struct file * file)
{
// If (device_open_count = 0)
// Device_open_count ++;
// Else {
// Printk ("the device has been turned on \ n ");
// Return-1;
//}
Try_module_get (THIS_MODULE );
Return 0;
}
Static int char_release (struct inode * inode, struct file * file)
{
// Device_open_count --;
Module_put (THIS_MODULE );
Return 0;
}
Static int char_read (struct file * filp, char _ user * buffer, size_t length, loff_t * offset)
{
If (length <0)
Return-1;
Else if (length> 12)
Length = 12;
If (0 = copy_to_user (buffer, "Hello Linux! ", Length ))
Return length;
Return-1;
}
Static int char_write (struct file * filp, const char _ user * buffer, size_t length, loff_t * offset)
{
Return 0;
}
Static void _ exit module_close (void)
{
Unregister_chrdev (major, "chardev ");
}
//////////////////////////////////////// //////////////////////
Module_init (char_init );
Module_exit (module_close );
//////////////////////////////////////// //////////////////////
// Main. c
//////////////////////////////////////// ///////////////////
# Include
# Include
# Include
# Include
# Include
# Include
# Include
//////////////////////////////////////// ///////////////////
Int main (void)
{
Char ch = 0;
Int testdev;
Int I, rf = 0;
Char buf [15];
Memset (buf, 0, sizeof (buf ));
Testdev = open ("/dev/chardev", O_RDWR );
If (testdev =-1)
{
Perror ("open error! \ N ");
Exit (0 );
}
While ('q '! = Ch)
{
Rf = read (testdev, buf, 11 );
If (rf <0)
{
Printf ("read error [% d]! \ N ", rf );
}
Else
{
Printf ("R: % s \ n", buf );
}
Scanf ("% c", & ch );
}
Close (testdev );
Return 0;
}
//////////////////////////////////////// ///////////////////