开始-例子_字符设备_手动创建设备
----------------
$>vi drivers/demo/demo.c
---------
/******************************************************************************
* 动态查看系统日志#tail -f /var/log/syslog
******************************************************************************/
#include <asm/unaligned.h>
#include <linux/ctype.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/ioctl.h>
#include <linux/cdev.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/clk.h>
#include <linux/uaccess.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
void simple_cleanup_module(void);
static unsigned char simple_inc=0;
static unsigned char demoBuffer[256];
//设备号
#define simple_MAJOR 55
int hex_dump_to_buffer_xe(const void *buf, size_t len, int rowsize, int groupsize,char *linebuf, size_t linebuflen, bool ascii)
{
const u8 *ptr = buf;
int ngroups;
u8 ch;
int j, lx = 0;
int ascii_column;
int ret;
if (rowsize != 16 && rowsize != 32)
rowsize = 16;
if (len > rowsize) /* limit to one line at a time */
len = rowsize;
if (!is_power_of_2(groupsize) || groupsize > 8)
groupsize = 1;
if ((len % groupsize) != 0) /* no mixed size output */
groupsize = 1;
ngroups = len / groupsize;
ascii_column = rowsize * 2 + rowsize / groupsize + 1;
if (!linebuflen)
goto overflow1;
if (!len)
goto nil;
if (groupsize == 8) {
const u64 *ptr8 = buf;
for (j = 0; j < ngroups; j++) {
ret = snprintf(linebuf + lx, linebuflen - lx,"%s%16.16llx", j ? " " : "",get_unaligned(ptr8 + j));
if (ret >= linebuflen - lx)
goto overflow1;
lx += ret;
}
} else if (groupsize == 4) {
const u32 *ptr4 = buf;
for (j = 0; j < ngroups; j++) {
ret = snprintf(linebuf + lx, linebuflen - lx,"%s%8.8x", j ? " " : "",get_unaligned(ptr4 + j));
if (ret >= linebuflen - lx)
goto overflow1;
lx += ret;
}
} else if (groupsize == 2) {
const u16 *ptr2 = buf;
for (j = 0; j < ngroups; j++) {
ret = snprintf(linebuf + lx, linebuflen - lx,"%s%4.4x", j ? " " : "",get_unaligned(ptr2 + j));
if (ret >= linebuflen - lx)
goto overflow1;
lx += ret;
}
} else {
for (j = 0; j < len; j++) {
if (linebuflen < lx + 2)
goto overflow2;
ch = ptr[j];
linebuf[lx++] = hex_asc_hi(ch);
if (linebuflen < lx + 2)
goto overflow2;
linebuf[lx++] = hex_asc_lo(ch);
if (linebuflen < lx + 2)
goto overflow2;
linebuf[lx++] = ' ';
}
if (j)
lx--;
}
if (!ascii)
goto nil;
while (lx < ascii_column) {
if (linebuflen < lx + 2)
goto overflow2;
linebuf[lx++] = ' ';
}
for (j = 0; j < len; j++) {
if (linebuflen < lx + 2)
goto overflow2;
ch = ptr[j];
linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
}
nil:
linebuf[lx] = '\0';
return lx;
overflow2:
linebuf[lx++] = '\0';
overflow1:
return ascii ? ascii_column + len : (groupsize * 2 + 1) * ngroups - 1;
}
void hex_dump_xe(const char *level, const char *prefix_str, int prefix_type,int rowsize, int groupsize,const void *buf, size_t len, bool ascii)
{
const u8 *ptr = buf;
int i, linelen, remaining = len;
unsigned char linebuf[32 * 3 + 2 + 32 + 1];
if (rowsize != 16 && rowsize != 32)
rowsize = 16;
printk("\0013#############HEXDUMP START###############\n");
for (i = 0; i < len; i += rowsize)
{
linelen = min(remaining, rowsize);
remaining -= rowsize;
hex_dump_to_buffer_xe(ptr + i, linelen, rowsize, groupsize,linebuf, sizeof(linebuf), ascii);
switch (prefix_type)
{
case DUMP_PREFIX_ADDRESS:
printk("%s%s%p: %s\n",level, prefix_str, ptr + i, linebuf);
break;
case DUMP_PREFIX_OFFSET:
printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
break;
default:
printk("%s%s%s\n", level, prefix_str, linebuf);
break;
}
}
printk("\0013##############HEXDUMP END################\n");
}
/********************************************************************************************************
*
********************************************************************************************************/
int simple_open(struct inode *inode, struct file *filp)
{
printk("simple_open!\n");
if(simple_inc > 0)
{
return -ERESTARTSYS;
}
simple_inc++;
return 0;
}
/********************************************************************************************************
*
********************************************************************************************************/
int simple_release(struct inode *inode, struct file *filp)
{
printk("simple_release!\n");
simple_inc--;
return 0;
}
/********************************************************************************************************
*
********************************************************************************************************/
ssize_t simple_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
/* 把数据拷贝到应用程序空间 */
printk("simple_read,count:%d %s-%d\n",count,__FILE__,__LINE__);
hex_dump_xe(KERN_ERR,"\t",0,16,1,demoBuffer,count,true);
if (copy_to_user(buf,demoBuffer,count))
{
count=-EFAULT;
}
return count;
}
/********************************************************************************************************
*
********************************************************************************************************/
ssize_t simple_write(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
/* 把数据拷贝到内核空间 */
printk("simple_write,count:%d %s-%d\n",count,__FILE__,__LINE__);
if (copy_from_user(demoBuffer+*f_pos, buf, count))
{
count = -EFAULT;
}
hex_dump_xe(KERN_ERR,"\t",0,16,1,demoBuffer+*f_pos,count,true);
return count;
}
/********************************************************************************************************
*
********************************************************************************************************/
struct file_operations simple_fops = {
.owner = THIS_MODULE,
.read = simple_read,
.write = simple_write,
.open = simple_open,
.release = simple_release,
};
/********************************************************************************************************
*
********************************************************************************************************/
int simple_init_module(void)
{
int ret;
printk("simple_init_module!\n");
ret = register_chrdev(simple_MAJOR, "simple", &simple_fops);
if (ret < 0)
{
printk("Unable to register character device %d!\n",simple_MAJOR);
return ret;
}
return 0;
}
/********************************************************************************************************
*
********************************************************************************************************/
void simple_cleanup_module(void)
{
unregister_chrdev(simple_MAJOR, "simple");
printk("simple_cleanup_module!\n");
}
/********************************************************************************************************
*
********************************************************************************************************/
module_init(simple_init_module);
module_exit(simple_cleanup_module);
MODULE_LICENSE("Dual BSD/GPL");
MODULE_LICENSE("Dual BSD/GPL");
$>vi drivers/demo/Makefile
---------
KBUILD_CFLAGS := $(filter-out -O2, $(KBUILD_CFLAGS))
KBUILD_HOSTCFLAGS := $(filter-out -O2, $(KBUILD_HOSTCFLAGS))
KBUILD_HOSTCXXFLAGS := $(filter-out -O2, $(KBUILD_HOSTCXXFLAGS))
KBUILD_HOSTCFLAGS := -v -g3 -O0 -Wall -Wmissing-prototypes -Wstrict-prototypes -fomit-frame-pointer -std=gnu89 $(HOST_LFS_CFLAGS) $(HOSTCFLAGS)
KBUILD_HOSTCXXFLAGS := -v -g3 -O0 $(HOST_LFS_CFLAGS) $(HOSTCXXFLAGS)
KBUILD_CFLAGS += -O0 -g3 -ggdb
#obj-$(CONFIG_XX) += demo.o
obj-m += demo.o
xx-y := demo.o
.PHONY:clean
clean:
rm -rf *.o *.order *.a .*.cmd *.o *.mod.c *.tmp_versions *.symvers
$>vi drivers/demo/Kconfig
---------
config XX
bool "bool xx"
#depends on KGDB
default y
$>vi drivers/Kconfig
---------
# SPDX-License-Identifier: GPL-2.0
menu "Device Drivers"
...
source "drivers/demo/Kconfig"
...
endmenu
$>vi drivers/Makefile
---------
obj-y += demo/
$>vi main.c
---------
/********************************************************************************************************************************
*
********************************************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
/********************************************************************************************************************************
* 需要手动生成设备节点 #mknod /dev/hello c 55 0
********************************************************************************************************************************/
int main(void)
{
int fd;
int i;
char data[256];
int retval;
fd=open("/dev/hello",O_RDWR);
if(fd==-1)
{
perror("error open\n");
exit(-1);
}
printf("open /dev/hello successfully\n");
retval=write(fd,"gaoyanli",8);
if(retval==-1)
{
perror("write error\n");
exit(-1);
}
retval=read(fd,data,8);
if(retval==-1)
{
perror("read error\n");
exit(-1);
}
data[retval]=0;
printf("read successfully: %s\n", data);
close(fd);
}
$>vi Makefile
---------
#############################################################
#
#############################################################
APP = gao_app
all:main.o
arm-linux-gnueabihf-gcc -g -O0 -o $(APP) main.o
main.o:main.c
arm-linux-gnueabihf-gcc -g -O0 -c main.c
.PHONY: clean
clean:
rm -rf *.o $(APP)
$>insmod demo.ko
$>mknod /dev/hello c 55 0
$>./gao_app
运行结果
---------
simple_open!
open /dev/hello successfully
simple_write,count:8 drivers/demo/demo.c-222
#############HEXDUMP START###############
67 61 6f 79 61 6e 6c 69 gaoyanli
##############HEXDUMP END################
simple_read,count:8 drivers/demo/demo.c-204
#############HEXDUMP START###############
67 61 6f 79 61 6e 6c 69 gaoyanli
##############HEXDUMP END################
read successfully: gaoyanli
simple_release!
---------
结束-例子_字符设备_手动创建设备
---------