linux 驅動建立多個裝置

2021-06-18 11:19:12 字數 4458 閱讀 2250

linux 驅動中建立乙個裝置的例子很多,以下例子是建立多個字元裝置的例子,包括驅動部分和測試部分

驅動部分包括 globalmem.c  makefile兩個檔案:

globalmem.c file:

#include #include #include #include #include #include #include #include #include #include #include #include #include #define globalmem_size 0x1000 //global memory size 4kb

#define mem_clear 0x1 //clear all the mem

#define globalmem_major 350 //globalmem major number

static struct class* globalmem_class0;

static struct class* globalmem_class1;

static int globalmem_major = globalmem_major;

struct globalmem_dev;

struct globalmem_dev* globalmem_devp;

void globalmem_setup_cdev(struct globalmem_dev * dev, int index);

int globalmem_init(void)else

if(result < 0)

globalmem_devp = kmalloc(2 * sizeof(struct globalmem_dev), gfp_kernel);

if(!globalmem_devp)

memset(globalmem_devp, 0, 2 * sizeof(struct globalmem_dev));

globalmem_setup_cdev(&globalmem_devp[0], 0);

globalmem_setup_cdev(&globalmem_devp[1], 1);

globalmem_class0 = class_create(this_module, "globalmem0"); //create file under /sys/class

device_create(globalmem_class0, null, mkdev(globalmem_major, 0), null, "globalmem_0"); //globalmem_0 is the name under /dev/

globalmem_class1 = class_create(this_module, "globalmem1"); //create file under /sys/class

device_create(globalmem_class1, null, mkdev(globalmem_major, 1), null, "globalmem_1"); //globalmem_1 is the name under /dev/

return 0;

fail_malloc:

unregister_chrdev_region(devno, 1);

return result;

}void globalmem_exit(void)

static int globalmem_open_0(struct inode* inode, struct file* filp)

static int globalmem_open_1(struct inode* inode, struct file* filp)

static ssize_t globalmem_read_0(struct file* filp, char __user * buf, size_t count, loff_t * ppos)

static ssize_t globalmem_read_1(struct file* filp, char __user * buf, size_t count, loff_t * ppos)

static ssize_t globalmem_write_0(struct file* filp, const char __user * buf, size_t count, loff_t * ppos)

static ssize_t globalmem_write_1(struct file* filp, const char __user * buf, size_t count, loff_t * ppos)

static loff_t globalmem_llseek_0(struct file* filp, loff_t offset, int orig)

static loff_t globalmem_llseek_1(struct file* filp, loff_t offset, int orig)

static long globalmem_ioctl_0(struct file * filp, unsigned int cmd, unsigned long arg)

static long globalmem_ioctl_1(struct file * filp, unsigned int cmd, unsigned long arg)

static const struct file_operations globalmem_fops = ,

[1] =

};void globalmem_setup_cdev(struct globalmem_dev* dev, int index)

module_init(globalmem_init);

module_exit(globalmem_exit);

module_license("dual bsd/gpl");

module_author("***");

module_description("global mem test");

1,1 top

makefile file:

mymodule-objs := globalmem.o

obj-m := globalmem.o

pwd:=$(shell pwd)

kernel_dir=/sziauto/android_src/m6_lch_1280x800/svn_m6_jb42_0403/branches/branches-lch/out/target/product/t37lchaudi/obj/kernel_obj/

cross_compile=/sziauto/android_src/m6_lch_1280x800/svn_m6_jb42_0403/branches/branches-lch/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin

pwd:=$(shell pwd)

all:

make arch=arm $(cross_compile) -c $(kernel_dir) m=$(pwd) modules

clean:

rm -rf *.o modules.order *.ko module.symvers *.mod.c

測試部分包括 android.mk  driver_test.c

driver_test.c :

#include #include #include #include #include #define buffer_len 100

int main(int argc, char* argv);

char content[buffer_len] = ;

const char* path0 = "/dev/globalmem_0";

const char* path1 = "/dev/globalmem_1";

while(1)

fd = open(path0, o_rdwr);

}else if(param[0] == '1')

fd = open(path1, o_rdwr);

}}else if(param[0] == 'r' || param[0] == 'r')else if(param[0] == 'w' || param[0] == 'w')else if(param[0] == 'i' || param[0] == 'i')else if(param[0] == 'q' || param[0] == 'q')

}return 0;

}

android.mk

include $(clear_vars)

local_src_files := \

driver_test.c

local_module_tags := optional

local_module := driver_test

include $(build_executable)

Linux驅動(三) 完善裝置驅動自動建立裝置號

include include include include include include include argc 應用程式引數個數,包括應用程式本身 ar 具體的引數內容,字串形式 int main int argc,char ar filename ar 1 獲取檔名稱 fd open f...

驅動 linux裝置驅動 字元裝置驅動開發

preface 前面對linux裝置驅動的相應知識點進行了總結,現在進入實踐階段!linux 裝置驅動入門篇 linux 裝置驅動掃盲篇 fedora下的字元裝置驅動開發 開發乙個基本的字元裝置驅動 在linux核心驅動中,字元裝置是最基本的裝置驅動。字元裝置包括了裝置最基本的操作,如開啟裝置 關閉...

建立字元裝置驅動

需要定義乙個字元裝置,裡面有字元裝置相關的方法 struct file operations cdev fops struct gyo struct gyostruct 申請裝置號 int alloc chrdev region dev t dev,unsigned baseminor,unsign...