1) minimum framework
/* Example minimal character device driver */<br/> # include <Linux/module. h> </P> <p> static int _ init hello_init (void) <br/> {<br/> printk ("Hello example init/N "); <br/> return 0; <br/>}< br/> static void _ exit hello_exit (void) <br/>{< br/> printk ("Hello example exit/N"); <br/>}</P> <p> module_init (hello_init ); <br/> module_exit (hello_exit); </P> <p> module_author ("ooo"); <br/> module_description ("Hello example "); <br/> module_license ("GPL ");
2) added the call interface
/* Example minimal character device driver */<br/> # include <Linux/module. h> <br/> # include <Linux/Fs. h ></P> <p> # define hello_major 242 </P> <p> static int debug_enable = 0; <br/> module_param (debug_enable, Int, 0 ); <br/> module_parm_desc (debug_enable, "enable module debug mode. "); </P> <p> struct file_operations hello_fops; </P> <p> static int hello_open (struct inode * inode, struct file * file) <br/> {<br/> PRI Ntk ("hello_open: Successful/N"); <br/> return 0; <br/>}</P> <p> static int hello_release (struct inode * inode, struct file * file) <br/>{< br/> printk ("hello_release: Successful/N"); <br/> return 0; <br/>}</P> <p> static ssize_t hello_read (struct file * file, char * Buf, size_t count, loff_t * PTR) <br/>{< br/> printk ("hello_read: Returning zero Bytes/N"); <br/> return 0; <br/>}</P> <p> static ssize_t hello_writ E (struct file * file, const char * Buf, size_t count, loff_t * PPOs) <br/>{< br/> printk ("hello_write: accepting zero Bytes/N "); <br/> return 0; <br/>}</P> <p> static int hello_ioctl (struct inode * inode, struct file * file, <br/> unsigned int cmd, unsigned long Arg) <br/> {<br/> printk ("hello_ioctl: cmd = % d, arg = % LD/N ", CMD, ARG); <br/> return 0; <br/>}</P> <p> static int _ init hello_init (void) <br/>{< br/> in T ret; <br/> printk ("Hello example init-debug mode is % s/n", <br/> debug_enable? "Enabled": "disabled"); <br/> ret = register_chrdev (hello_major, "hello", & hello_fops); <br/> If (Ret <0) {<br/> printk ("error registering Hello device/N"); <br/> goto hello_fail; <br/>}< br/> printk ("Hello: registered module successfully! /N "); </P> <p>/* init processing here... */</P> <p> return 0; </P> <p> hello_fail: <br/> return ret; <br/>}</P> <p> static void _ exit hello_exit (void) <br/>{< br/> printk ("Hello exaple exit/N "); <br/>}</P> <p> struct file_operations hello_fops ={< br/> owner: this_module, <br/> Read: hello_read, <br/> write: hello_write, <br/> IOCTL: hello_ioctl, <br/> open: hello_open, <br/> release: hello_release, <br/> }; </P> <p> module_init (hello_init); <br/> module_exit (hello_exit); </P> <p> module_description ("Hello module example "); <br/> module_license ("GPL"); <br/> module_author ("ooo"); </P> <p>
Add the relevant configuration file in the Linux source code tree, generate hello. Ko, and install it in/lib/modules.
3) test this module
On the target platform,
# Insmod hello. Ko
Create a device node, such as/dev/Hello
# Mknod/dev/Hello C 242 0
Then, write the test program:
/* Test hello. ko <br/> * You shoshould insmod hello. ko and mknod/dev/hello before <br/> */<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # include <sys/types. h> <br/> # include <sys/STAT. h> <br/> # include <fcntl. h> <br/> # include <unistd. h> </P> <p> int main (INT argc, char ** argv) <br/>{< br/> int FD; <br/> int rc = 0; <br/> char * rd_buf [16]; </P> <p> printf ("% s: entered/N ", argv [0]); </P> <p> FD = open ("/dev/Hello", o_rdwr); <br/> If (FD =-1) {<br/> printf ("Open failed"); <br/> rc = FD; <br/> exit (-1 ); <br/>}< br/> printf ("% s: Open successful: % d/N", argv [0], FD ); <br/> close (FD); </P> <p>}
After cross-compilation, put the BIN file (such as hello-Test) in the rootfs/bin directory and run it on the target platform.
# Hello-test