linux簡單字元裝置驅動

2021-06-22 09:52:48 字數 1506 閱讀 4407

os:ubuntu 12.04

如果是用交叉編譯器,只需要修改makefile中的kernel所在路徑即可。

下面貼出乙個簡單的字元裝置驅動例子:

#include #include #include #include #include #include #include #define name "test"

static dev_t dev = 0;

static struct cdev *cdev;

static struct class *test_class;

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

static long test_ioctl(struct file *filp,unsigned int cmd, unsigned long argv)

return 0;

}static int test_release(struct inode* inode,struct file* filp)

static struct file_operations test_opt =

;static int __init test_init(void)

} cdev = cdev_alloc();

cdev->ops = &test_opt;

result = cdev_add(cdev,dev,1);

if(result < 0)

test_class = class_create(this_module,name);

if(is_err(test_class))

device_create(test_class,null,dev,null,name);

return 0;

class_err:

cdev_del(cdev);

cdev_err:

unregister_chrdev_region(dev,1);

reg_err:

return -1;

}static void __exit test_exit(void)

module_init(test_init);

module_exit(test_exit);

module_license("gpl");

上面是乙個簡單的字元裝置驅動的例子。接著貼出ubunut下編譯方法: 

obj-m += test.o

all:

make -c /lib/modules/3.2.0-23-generic/build m=$(shell pwd) modules

clean:

rm *.ko *.o

編譯完成後,直接執行

#insmod test.ko

#dmesg

如果看看是否有資訊輸出:hello test

簡單字元驅動程式

mycdev.c include include include include include include include include include include module license gpl define mycdev major 231 cat proc devices檢視...

簡單字元裝置驅動程式 以及 相應測試程式

include include include include include include include include include define device name embedsky leds 載入模式後,執行 cat proc devices 命令看到的裝置名稱 define le...

簡單字元驅動程式mycdev c

root bogon 桌面 gedit mycdev.c include include include include include include module author me module license gpl define mycdev major 231 給定的主裝置號 defin...