Linux????????豸??д
???????????? ???????[ 2013/1/28 10:22:02 ] ????????
??????module_init?е????????????????????????????ж????????????module_exit?С??????豸????????????????豸??????????????????豸??????????????У?????????????????豸???alloc_chrdev_region??????????????????cdev_add??????????豸????????????????????????????????д??????????????????д???Makefile?????
ifneq ($(KERNELRELEASE)??)
obj-m := char.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions modules.* Module.*
endif
???????Makefile?????????????д??makefile????????????????????????????????char.o??????????????д??飬???????????make???ɡ??????char.ko???????????????????????????????????У?insmod char.ko?????????????????????????????????????????????У???????????????lsmod | grep char???в????????????????豸??????????????豸??????????major??minor?????????????dmesg | tail ????????????????hp??????????????????????249??0????????????????????????????豸??????????mknod /dev/chr_dev c 249 0????????????????ls /dev/chr_dev??????????????????????????????????????????豸????????????д??μ???????????£?
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#define CHAR_DEV_NAME "/dev/chr_dev"
int main()
{
int ret;
int fd;
char buf[32];
fd = open(CHAR_DEV_NAME?? O_RDONLY | O_NDELAY);
if(fd < 0)
{
printf("open failed!
");
return -1;
}
read(fd?? buf?? 32);
close(fd);
return 0;
}
?????????????????????????CHAR_DEV_NAME?????豸????д?豸????????????????????????????б?????????test.c??????gcc test.c -o test??????????????????????????./test???ɡ???????????????????????????????ok????????????????п????κ??????????????????????dmesg?????????????????????????????????????dmesg | tail???ɡ????????????????????????????????chr_open??chr_read?????μ?printk???????????????????????ok???
??????????????????С???????????????????????????????????????????????????????????????????????μ???????????????鲹????£???1???????????????????????豸??????rm -rf /dev/chr_dev???????2????????δ??????????????????Linux?豸??????????????????飬????????????????????????????????????ο???????????
??????

???·???
App??С????H5?????????????????Щ??
2024/9/11 15:34:34?????????????????????????
2024/9/10 11:13:49P-One ???????????????????????????????????????
2024/9/10 10:14:12???????????????????????????
2024/9/9 18:04:26??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44