Driver file:
- /*
- * Simplechrdriver. c
- *
- * Copyright (c) 2008 breathomn <breathomn@sohu.com>
- *
- */
- # Include <Linux/module. h>
- # Include <Linux/kernel. h>
- # Include <Linux/fs. h>
- # Include <Linux/init. h>
- # Include <ASM/uaccess. h>/* Copy _ * _ User */
- StaticUnsigned int major_num = 0;
- StaticInt global_var = 0;
- StaticSsize_t simplechr_read (struct file * filp, char * Buf, ssize_t size, loff_t * Off)
- {
- If (copy_to_user (buf, & global_var, sizeof (int )))
- Return-EFAULT;
- Return sizeof (int );
- }
- StaticSsize_t simplechr_write (struct file * filp, const char * buf, ssize_t size, loff_t * off)
- {
- If (copy_from_user (& global_var, buf, sizeof (int )))
- Return-EFAULT;
- Return sizeof (int );
- }
- StaticStruct file_operations simplechrdev_ops =
- {
- . Owner = THIS_MODULE,
- . Read = simplechr_read,
- . Write = simplechr_write,
- };
- StaticInt _ init simplechr_init (void)
- {
- Int ret;
- If (ret = register_chrdev (major_num, "simplechrdev", & simplechrdev_ops) <0)
- Printk (KERN_ALERT "simplechrdev register failed! /N ");
- Else
- {
- Major_num = ret;
- Printk (KERN_ALERT "simplechrdev register success! /N ");
- }
- Return ret;
- }
- StaticVoid _ exit simplechr_exit (void)
- {
- Int ret;
- If (ret = unregister_chrdev (major_num, "simplechrdev ")))
- Printk (kern_alert "simplechrdev unregister failed! /N ");
- Else
- Printk (kern_alert "simplechrdev unregister success! /N ");
- }
- Module_init (simplechr_init );
- Module_exit (simplechr_exit );
User File:
- /*
- * Simplechrsrc. c
- *
- * Copyright (c) 2008 breathomn <breathomn@sohu.com>
- *
- */
- # Include <sys/types. h>
- # Include <sys/stat. h>
- # Include <stdio. h>
- # Include <fcntl. h>
- IntMain ()
- {
- Int fd, num;
- If (fd = open ("/dev/simplechrdev", O_RDWR, S_IRUSR | S_IWUSR ))! =-1)
- {
- Read (fd, & num, sizeof (int ));
- Printf ("Read: % d/n", num );
- Printf ("Write a num :");
- Scanf ("% d", & num );
- Write (FD, & num, sizeof (INT ));
- Read (FD, & num, sizeof (INT ));
- Printf ("RAED again: % d/N", num );
- Close (FD );
- }
- Else
- Printf ("open device failed! /N ");
- }
MAKEFILE file:
OBJ-M: = simplechrdriver. o
Kerneldir? =/Lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
ALL:
$ (Make)-C $ (kerneldir) M = $ (PWD)
Clean:
Rm-RF *. O *~ Core. depend. *. CMD *. Ko *. Mod. C. tmp_versions
Compile our driver into kernel:
(1) create a new directory named simplechrdev in "$ (kernelsource)/Drivers/char" directory.
(2) copy the driver file "simplechrdriver. c" to the new directory.
(3) create a kconfig file in "simplechrdev" directory with contents below:
Config simplechrdev
Tristate "simplechrdev"
(4) modify the kconfig file of its parent directory "char", add contents:
Source "Drivers/Char/simplechrdev/kconfig"
Then we can find a item named "simplechrdev" in "Device Driver-> character devices" when we use "make menuconfig/xconfig ".
(5) create a MAKEFILE file in "simplechrdev" directory with contents below:
OBJ-$ (config_simplechrdev) + = simplechrdriver. o
(6) modify the MAKEFILE file of its parent directory "char", add contents:
OBJ-$ (config_simplechrdev) + = simplechrdev/
All things done! Remake kernel and reboot computer, the driver simplechrdriver works probably. input the command "cat/proc/devices", our device simplechrdev is in the character devices list.
(7) To load our module automatically
We can choose to compile our driver as a module, but sometimes the kernel can not load our module automatically. we can find a solution that add some contents to "/etc/rc. d/RC. sysinit "file to the problem.
# Simplechrdev
If [-D/lib/modules/$ unamer/kernel/Drivers/Char/simplechrdev]; then
For module in/lib/modules/$ unamer/kernel/Drivers/Char/simplechrdev/*; do
Module =$ {module ##*/}
Module =$ {module %. Ko}
Modprobe $ module>/dev/null 2> & 1
Done
Fi