This example is for Android up-Reading Exploration (vol. 1): the example program that accompanies the HAL and Driver Development book. are now pasted out for inspection.
Terminal operations, commands that may be used:
Insmond Word_count.kolsmod | grep Word_count See if the driver is installed successfully Rmmod WORD_COUNTDMESG | grep word_cout | Tail-n 2 View log information with Linux driver output Cat/var/log/syslong | grep Word_count | Tail-n 2modinfo Word_count.ko View driver information
Drive source Code
#include <linux/module.h> #include <linux/init.h> #include <linux/kernel.h> #include <linux/fs.h > #include <linux/miscdevice.h> #include <asm/uaccess.h>//define the device file name # device_name "WordCount" static unsigned char mem[10000]; Saves data written to the device file static int word_count = 0, #define TRUE 255#define FALSE 0//Determines whether the specified character is a space (including spaces, tabs, carriage returns, and line breaks) static unsigned Char Is_spacewhite (char c) {if (c = = + | | c = = 9 | | c = = | | c = TEN) return True;elsereturn FALSE; static int Get_word_count (const char *buf) {int n = 1;int i = 0;char c = ' '; char flag = 0;//handle multiple space-delimited cases, 0: normal, 1: a space has been encountered if (*buf = = ') return 0;//the 1th character is a space, starting from 0 counts if (is_spacewhite (*buf) = = TRUE) n--;//scans each character in a string for (; (c = * (buf + i))! = ' + '; i++) {//Only one space separates words if (flag = = = 1 && is_spacewhite (c) = = FALSE) {flag = 0;} When a word is separated by more than one space, the extra space is ignored, else if (flag = = 1 && is_spacewhite (c) = = TRUE) {continue;} The current character is a space is the number of words plus 1if (is_spacewhite (c) = = TRUE) {N++;flag = 1;}} If the string ends with one or more spaces, no count (number of words minus 1) iF (Is_spacewhite (* (buf + i-1)) = = TRUE) n--;return N;} Static ssize_t word_count_read (struct file *file, char __user *buf, size_t count, loff_t *ppos) {unsigned char temp[ 4];temp[0] = Word_count >> 24;temp[1] = word_count >> 16; TEMP[2] = Word_count >> 8;temp[3] = word_count;if (Copy_to_user (buf, (void*) temp, 4)) {Return-einval;} PRINTK ("Read:word count:%d", (int) count); return count;} Static ssize_t word_count_write (struct file *file, const char __user *buf, size_t count, loff_t *ppos) {ssize_t writ Ten = Count;if (Copy_from_user (Mem, buf, Count)) {Return-einval;} Mem[count] = ' + '; word_count = Get_word_count (MEM);p rintk ("Write:word count:%d\n", (int) word_count); return written;} Describes the callback function pointer that corresponds to an event triggered by a device file static struct file_operations dev_fops ={. Owner = This_module,. Read = Word_count_read,. Write = Word_count_write};//describes the information for a device file static struct Miscdevice misc ={. minor = Misc_dynamic_minor,. Name = Device_name,. fo PS = &dev_fops};//initialize Linux driver static int __init word_count_init (void) {int ret;//Establish device file RET = Misc_register (&MISC);//output log information printk ("Word_count_init_ Success\n "); return ret;} Uninstall Linux driver static void __exit word_count_exit (void) {//delete device file Misc_deregister (&MISC);//output log information printk ("Word_count _init_exit_success\n ");} Register initialize the Linux driver function module_init (word_count_init);//Register to uninstall the Linux-powered function Module_exit (word_count_exit); Module_author ("lining"); Module_description ("Statistics of Word count."); Module_alias ("Word Count module."); Module_license ("GPL");
Test code
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> #include < String.h>int Main (int argc, char *argv[]) {int testdev;unsigned char Buf[4];testdev = open ("/dev/wordcount", O_RDWR); if (Testdev = =-1) {printf ("Cann ' t open file \ n"); return 0;} if (argc > 1) {write (Testdev, argv[1], strlen (argv[1]));p rintf ("string:%s\n", Argv[1]);} Read (Testdev, buf, 4); int n = 0;// restores 4 bytes to a value of type int = ((int) buf[0]) << 24 | ((int) buf[1]) << 16 | ((int) buf[2]) << 8 | ((int) buf[3]);p rintf ("Word byte display:%d,%d,%d,%d\n", Buf[0], buf[1], buf[2], buf[3]);p rintf ("Word count:%d\n", n); Close (Testdev); return 0;}