本节是对上处链接的进一步研究。
一、Linux2.4之前手动mknod
如下使用内核接口在Linux2.6内核保留;试验也可选择Linux2.6内核。 1.驱动编写
static int __init test_drv_init(void)
{
int rc;
printk("test_driver dev\n");
//注册设备
rc = register_chrdev(TEST_MAJOR,"test_dev",&led_fops);
if (rc <0){
printk ("register %s char dev error\n","led");
return -1;
}
printk ("ok!\n");
return 0;
out_chrdev:
unregister_chrdev(TEST_MAJOR, "mymodule");
out:
return -1;
}
2.用户空间
mknod /dev/tankai_dev 240
二、Linux2.6.17之前内核devfs
linux-2.6.14/fs/Kconfig
menu "File systems"
#add by tank
config DEVFS_FS
bool "Devfs support"
depends on EXPERIMENTAL
config DEVFS_MOUNT
bool "devfs mount"
depends on DEVFS_FS
config DEVFS_DEBUG
bool "Debug devfs"
depends on DEVFS_FS
#end tank
linux-2.6.14/fs/Makefile
obj-$(CONFIG_DEVFS_FS) += devfs/
2.驱动编写
#include
static int __init test_drv_init(void)
{
int rc;
printk("test_driver dev\n");
//注册设备
rc = register_chrdev(TEST_MAJOR,"test_dev",&led_fops);
if (rc <0){
printk ("register %s char dev error\n","led");
return -1;
}
//devfs关键
devfs_mk_cdev( MKDEV(TEST_MAJOR, 0),S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, "tankai_dev");
//devfs
printk ("ok!\n");
return 0;
out_chrdev:
unregister_chrdev(TEST_MAJOR, "mymodule");
out:
return -1;
}
3.结果
# ls -l /dev/tankai_dev
crw-r----- 1 0 0 240, 0 Jan 1 00:00 /dev/tankai_dev
# mount
rootfs on / type rootfs (rw)
/dev/root on / type ext2 (rw,nogrpid)
none on /dev type devfs (rw)
none on /proc type proc (rw,nodiratime)
# cat /proc/version
Linux version 2.6.14 (root@tankai-G41T-R3) (gcc version 3.4.1) #75 Fri Jan 3 15:20:03 CST 2014
#
三、Linux2.6.29内核udev
static int __init test_drv_init(void)
{
int rc;
printk("test_driver dev\n");
//注册设备
rc = register_chrdev(TEST_MAJOR,"test_dev",&led_fops);
if (rc <0){
printk ("register %s char dev error\n","led");
return -1;
}
//实现动态创建
mymodule_class = class_create(THIS_MODULE, "test_dev");
device_create(mymodule_class, NULL, MKDEV(TEST_MAJOR, 0), NULL, "tankai_dev");
//结束
printk ("ok!\n");
return 0;
out_chrdev:
unregister_chrdev(TEST_MAJOR, "mymodule");
out:
return -1;
}2.用户空间
udev守护进程完成mknod功能
3.结果
root@android:/ # ll /dev/tankai_dev
crw------- root root 240, 0 2014-01-03 05:22 tankai_dev
root@android:/ # mount
rootfs / rootfs ro 0 0
tmpfs /dev tmpfs rw,mode=755 0 0
devpts /dev/pts devpts rw,mode=600 0 0
proc /proc proc rw 0 0
sysfs /sys sysfs rw 0 0
none /acct cgroup rw,cpuacct 0 0
tmpfs /mnt/asec tmpfs rw,mode=755,gid=1000 0 0
tmpfs /mnt/obb tmpfs rw,mode=755,gid=1000 0 0
none /dev/cpuctl cgroup rw,cpu 0 0
/dev/block/mtdblock0 /system yaffs2 ro 0 0
/dev/block/mtdblock1 /data yaffs2 rw,nosuid,nodev 0 0
/dev/block/mtdblock2 /cache yaffs2 rw,nosuid,nodev 0 0
/dev/block/vold/179:0 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
/dev/block/vold/179:0 /mnt/secure/asec vfat rw,dirsync,nosuid,nodev,noexec,uid=1000,gid=1015,fmask=0702,dmask=0702,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
tmpfs /mnt/sdcard/.android_secure tmpfs ro,size=0k,mode=000 0 0
root@android:/ # cat /proc/version
Linux version 2.6.29-gb0d93fb-dirty (root@tankai-G41T-R3) (gcc version 4.4.3 (GCC) ) #103 PREEMPT Fri Nov 22 13:15:05 CST 2013
root@android:/ #