mirror of
https://github.com/gatieme/LDD-LinuxDeviceDrivers.git
synced 2026-11-14 02:02:39 +08:00
使用cdev_add注册字符设备...
This commit is contained in:
1 parent
d3526eace7
commit
151377c60c
16 files changed
+278
-819
No files matched your search
@@ -2,7 +2,7 @@ all:github
|
||||
|
||||
|
||||
|
||||
COMMIT="完善了thread_info的学习..."
|
||||
COMMIT="使用cdev_add注册字符设备..."
|
||||
|
||||
.PHONY : github
|
||||
github:
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
ROOT=.
|
||||
|
||||
|
||||
DIRS= demo test
|
||||
|
||||
all:
|
||||
for i in $(DIRS); do \
|
||||
(cd $$i && echo "making $$i" && $(MAKE) ) || exit 1; \
|
||||
done
|
||||
|
||||
|
||||
clean:
|
||||
for i in $(DIRS); do \
|
||||
(cd $$i && echo "cleaning $$i" && $(MAKE) clean) || exit 1; \
|
||||
done
|
||||
|
||||
|
||||
insmod:
|
||||
cd demo && make insmod
|
||||
|
||||
rmmod:
|
||||
cd demo && make rmmod
|
||||
|
||||
reinsmod:
|
||||
cd demo && make reinsmod
|
||||
|
||||
run :
|
||||
sudo ./test/test
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
cmd_/mnt/hgfs/windows/src/1drivermodel/1-3register_chrdev/demo/demo.ko := /usr/local/arm/4.2.2-eabi/usr/bin/arm-linux-ld -EL -r -o /mnt/hgfs/windows/src/1drivermodel/1-3register_chrdev/demo/demo.ko /mnt/hgfs/windows/src/1drivermodel/1-3register_chrdev/demo/demo.o /mnt/hgfs/windows/src/1drivermodel/1-3register_chrdev/demo/demo.mod.o
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -1,2 +0,0 @@
|
||||
/mnt/hgfs/windows/src/1drivermodel/1-3register_chrdev/demo/demo.ko
|
||||
/mnt/hgfs/windows/src/1drivermodel/1-3register_chrdev/demo/demo.o
|
||||
@@ -0,0 +1,80 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
#
|
||||
# Makefile for the LDD-LinuxDeviceDrivers.
|
||||
#
|
||||
# Author: gatieme
|
||||
# Create: 2016-07-29 15:50:46
|
||||
# Last modified: 2016-07-29 16:10:29
|
||||
# Description:
|
||||
# This program is loaded as a kernel(v2.6.18 or later) module.
|
||||
# Use "make install" to load it into kernel.
|
||||
# Use "make remove" to remove the module out of kernel.
|
||||
#
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
ROOT=../../../../../
|
||||
#PLATFORM=$(shell $(ROOT)/systype.sh)
|
||||
#include $(ROOT)/Make.defines.$(PLATFORM)
|
||||
|
||||
# my driver description
|
||||
DRIVER_VERSION := "1.0.0"
|
||||
DRIVER_AUTHOR := "Gatieme @ AderStep Inc..."
|
||||
DRIVER_DESC := "Demo module for LDD-LinuxDeviceDrivers devices"
|
||||
DRIVER_LICENSE := "Dual BSD/GPL"
|
||||
|
||||
|
||||
MODULE_NAME := demo
|
||||
|
||||
ifneq ($(KERNELRELEASE),)
|
||||
|
||||
#CFG_FLAGS += -O2 -I./
|
||||
#EXTRA_CFLAGS += $(C_FLAGS) $(CFG_INC) $(CFG_INC)
|
||||
|
||||
|
||||
|
||||
obj-m := $(MODULE_NAME).o #smodule.o
|
||||
|
||||
else
|
||||
|
||||
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
|
||||
|
||||
PWD := $(shell pwd)
|
||||
|
||||
modules:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules
|
||||
|
||||
modules_install:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules_install
|
||||
|
||||
|
||||
|
||||
insmod:
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
sudo mknod /dev/fgj c 224 0
|
||||
|
||||
reinsmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
rmmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
sudo /bin/rm /dev/fgj
|
||||
|
||||
clean:
|
||||
make -C $(KERNELDIR) M=$(PWD) clean
|
||||
rm -f modules.order Module.symvers Module.markers
|
||||
|
||||
|
||||
COMMIT="[1.1.4 带参数的可加载模块](http://book.51cto.com/art/201205/337663.htm)"
|
||||
github:
|
||||
cd $(ROOT) && make github
|
||||
|
||||
|
||||
.PHNOY:
|
||||
modules modules_install clean
|
||||
|
||||
|
||||
|
||||
endif
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
# ------------------------------------------------------------------------------
|
||||
#
|
||||
# Makefile for the LDD-LinuxDeviceDrivers.
|
||||
#
|
||||
# Author: gatieme
|
||||
# Create: 2016-07-29 15:50:46
|
||||
# Last modified: 2016-07-29 16:10:29
|
||||
# Description:
|
||||
# This program is loaded as a kernel(v2.6.18 or later) module.
|
||||
# Use "make install" to load it into kernel.
|
||||
# Use "make remove" to remove the module out of kernel.
|
||||
#
|
||||
# ------------------------------------------------------------------------------
|
||||
|
||||
|
||||
ROOT=../../../../../
|
||||
#PLATFORM=$(shell $(ROOT)/systype.sh)
|
||||
#include $(ROOT)/Make.defines.$(PLATFORM)
|
||||
|
||||
# my driver description
|
||||
DRIVER_VERSION := "1.0.0"
|
||||
DRIVER_AUTHOR := "Gatieme @ AderStep Inc..."
|
||||
DRIVER_DESC := "Demo module for LDD-LinuxDeviceDrivers devices"
|
||||
DRIVER_LICENSE := "Dual BSD/GPL"
|
||||
|
||||
|
||||
MODULE_NAME := demo
|
||||
|
||||
ifneq ($(KERNELRELEASE),)
|
||||
|
||||
#CFG_FLAGS += -O2 -I./
|
||||
#EXTRA_CFLAGS += $(C_FLAGS) $(CFG_INC) $(CFG_INC)
|
||||
|
||||
|
||||
|
||||
obj-m := $(MODULE_NAME).o #smodule.o
|
||||
|
||||
else
|
||||
|
||||
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
|
||||
|
||||
PWD := $(shell pwd)
|
||||
|
||||
modules:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules
|
||||
|
||||
modules_install:
|
||||
make -C $(KERNELDIR) M=$(PWD) modules_install
|
||||
|
||||
|
||||
|
||||
insmod:
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
sudo mknod /dev/fgj c 224 0
|
||||
|
||||
reinsmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
sudo insmod $(MODULE_NAME).ko
|
||||
|
||||
rmmod:
|
||||
sudo rmmod $(MODULE_NAME)
|
||||
sudo /bin/rm /dev/fgj
|
||||
|
||||
clean:
|
||||
make -C $(KERNELDIR) M=$(PWD) clean
|
||||
rm -f modules.order Module.symvers Module.markers
|
||||
|
||||
|
||||
COMMIT="[1.1.4 带参数的可加载模块](http://book.51cto.com/art/201205/337663.htm)"
|
||||
github:
|
||||
cd $(ROOT) && make github
|
||||
|
||||
|
||||
.PHNOY:
|
||||
modules modules_install clean
|
||||
|
||||
|
||||
|
||||
endif
|
||||
|
||||
Whitespace-only changes.
@@ -37,7 +37,7 @@ ssize_t simple_read(struct file *filp, char __user *buf, size_t count,loff_t *f_
|
||||
/* 把数据拷贝到应用程序空间 */
|
||||
if (copy_to_user(buf,demoBuffer,count))
|
||||
{
|
||||
count=-EFAULT;
|
||||
count=-EFAULT;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -65,14 +65,14 @@ struct file_operations simple_fops = {
|
||||
*******************************************************/
|
||||
void simple_cleanup_module(void)
|
||||
{
|
||||
unregister_chrdev(simple_MAJOR, "simple");
|
||||
unregister_chrdev(simple_MAJOR, "simple");
|
||||
printk("simple_cleanup_module!\n");
|
||||
}
|
||||
|
||||
int simple_init_module(void)
|
||||
{
|
||||
int ret;
|
||||
ret = register_chrdev(simple_MAJOR, "simple", &simple_fops);//注册设备号231,设备名
|
||||
ret = register_chrdev(simple_MAJOR, "simple", &simple_fops);//注册设备号231,设备名
|
||||
if (ret < 0)
|
||||
{
|
||||
printk("Unable to register character device %d!\n",simple_MAJOR);
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
#ifndef _simple_H_
|
||||
#define _simple_H_
|
||||
|
||||
@@ -21,11 +20,15 @@
|
||||
#undef PDEBUGG
|
||||
#define PDEBUGG(fmt, args...) /* nothing: it's a placeholder */
|
||||
|
||||
//设备号
|
||||
// 设备号
|
||||
// error inserting 'globalmem.ko': -1 Device or resource busy
|
||||
// 出现这个原因是定义的设备号有冲突,
|
||||
// 可以通过cat /proc/devices查看有哪些设备号还没被使用
|
||||
// 或者直接由系统动态分配主设备号
|
||||
#define simple_MAJOR 224
|
||||
|
||||
//设备结构
|
||||
struct simple_dev
|
||||
struct simple_dev
|
||||
{
|
||||
struct cdev cdev; /* Char device structure */
|
||||
};
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
#include <linux/module.h>
|
||||
#include <linux/vermagic.h>
|
||||
#include <linux/compiler.h>
|
||||
|
||||
MODULE_INFO(vermagic, VERMAGIC_STRING);
|
||||
|
||||
struct module __this_module
|
||||
__attribute__((section(".gnu.linkonce.this_module"))) = {
|
||||
.name = KBUILD_MODNAME,
|
||||
.init = init_module,
|
||||
#ifdef CONFIG_MODULE_UNLOAD
|
||||
.exit = cleanup_module,
|
||||
#endif
|
||||
.arch = MODULE_ARCH_INIT,
|
||||
};
|
||||
|
||||
static const char __module_depends[]
|
||||
__used
|
||||
__attribute__((section(".modinfo"))) =
|
||||
"depends=";
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
kernel//mnt/hgfs/windows/src/1drivermodel/1-3register_chrdev/demo/demo.ko
|
||||
@@ -0,0 +1,25 @@
|
||||
ROOT=../../../
|
||||
|
||||
target=test
|
||||
|
||||
|
||||
|
||||
all:$(target)
|
||||
|
||||
|
||||
memorylayout : test.o
|
||||
$(CC) $^ -o $@ $(LDFLAGS)
|
||||
@echo "测试使用register_chrdev注册的字符设备..."
|
||||
|
||||
%.o : %.c
|
||||
$(CC) -c $^ -o $@ $(CFLAGS) $(DEFINES)
|
||||
|
||||
clean :
|
||||
rm -rf *.o
|
||||
rm -rf $(target)
|
||||
|
||||
|
||||
run :
|
||||
sudo ./test
|
||||
|
||||
.PHONY : clean run
|
||||
Binary file not shown.
@@ -1,40 +1,54 @@
|
||||
#include<sys/types.h>
|
||||
#include<unistd.h>
|
||||
#include<fcntl.h>
|
||||
#include<linux/rtc.h>
|
||||
#include<linux/ioctl.h>
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
|
||||
void main(void)
|
||||
{
|
||||
int fd;
|
||||
int i;
|
||||
char data[256];
|
||||
|
||||
int retval;
|
||||
fd=open("/dev/fgj",O_RDWR);
|
||||
if(fd==-1)
|
||||
{
|
||||
perror("error open\n");
|
||||
exit(-1);
|
||||
}
|
||||
printf("open /dev/fgj successfully\n");
|
||||
|
||||
retval=write(fd,"fgj",3);
|
||||
if(retval==-1)
|
||||
{
|
||||
perror("write error\n");
|
||||
exit(-1);
|
||||
}
|
||||
retval=read(fd,data,3);
|
||||
if(retval==-1)
|
||||
{
|
||||
perror("read error\n");
|
||||
exit(-1);
|
||||
}
|
||||
data[retval]=0;
|
||||
printf("read successfully:%s\n",data);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <linux/rtc.h>
|
||||
#include <linux/ioctl.h>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#define MAX_LINE 256
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int fd;
|
||||
int i;
|
||||
char data[MAX_LINE] = "Hello World!";
|
||||
int retval;
|
||||
|
||||
fd = open("/dev/fgj",O_RDWR);
|
||||
if(fd == -1)
|
||||
{
|
||||
perror("error open");
|
||||
exit(-1);
|
||||
}
|
||||
printf("open /dev/fgj successfully\n");
|
||||
|
||||
retval = write(fd, data, strlen(data));
|
||||
if(retval == -1)
|
||||
{
|
||||
perror("write error");
|
||||
exit(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("write data successfully\n");
|
||||
}
|
||||
retval = read(fd, data, MAX_LINE);
|
||||
if(retval == -1)
|
||||
{
|
||||
perror("read error");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
data[retval] = '\0';
|
||||
printf("read successfully:%s\n", data);
|
||||
|
||||
close(fd);
|
||||
}
|
||||
Reference in new issue
Block a user