【驱动】第11课、热拔插—hotplug_uevent机制
主 机:VMWare--Ubuntu-16.04.2-x64-100ask
开发板:Mini2440--256M NandFlash, 2M NorFlash, 64M SDRAM, LCD-TD35;
bootlorder:u-boot1.16, Kernel:2.6.22.6;
编译器:arm-linux-gcc-3.4.5
1、问题
问题1.进程的产生是由执行应用程序而产生的,是吗?
答1.应该是的。
2、注意事项
2.1vi .config
CONFIG_FB_S3C2410=m //设为模块;
2.2下面的命令 if 之后必须有一个空格!否则失败!
leds?[123]? 0:0 777 * if [ $ACTION = "add" ]; then echo create /dev/$MDEV > /dev/console; else echo remove /dev/$MDEV > /dev/console; fi
课堂笔记
1、
class_device_create
class_device_register
class_device_add
kobject_uevent(&class_dev->kobj, KOBJ_ADD);
kobject_uevent_env(kobj, action, NULL);
// action_string = "add";
action_string = action_to_string(action);
/* 分配保存环境变量的内存 */
/* environment values */
buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
/* 设置环境变量 */
envp [i++] = scratch;
scratch += sprintf(scratch, "ACTION=%s", action_string) + 1;
envp [i++] = scratch;
scratch += sprintf (scratch, "DEVPATH=%s", devpath) + 1;
envp [i++] = scratch;
scratch += sprintf(scratch, "SUBSYSTEM=%s", subsystem) + 1;
/* 调用应用程序: 比如mdev */
/* 启动脚本 echo /sbin/mdev > /proc/sys/kernel/hotplug
* 设置了uevent_helper为“/sbin/mdev“
*/
argv [0] = uevent_helper; // = "/sbin/mdev"
argv [1] = (char *)subsystem;
argv [2] = NULL;
call_usermodehelper (argv[0], argv, envp, 0);
2、分析: busybox mdev.c
100ask: uevent_helper = /sbin/mdev
envp[0] = HOME=/
envp[1] = PATH=/sbin:/bin:/usr/sbin:/usr/bin
envp[2] = ACTION=add
envp[3] = DEVPATH=/class/sixth_drv/buttons
envp[4] = SUBSYSTEM=sixth_drv
envp[5] = SEQNUM=720
envp[6] = MAJOR=252
envp[7] = MINOR=0
mdev_main
temp = /sys/class/sixth_drv/buttons
make_device(temp, 0);
/* 确定设备文件名,类型,主次设备号 */
device_name = bb_basename(path); = "buttons"
'c' == > 字符设备节点
根据"/sys/class/sixth_drv/buttons/dev"的内容确定主次设备号
mknod(device_name, mode | type, makedev(major, minor)
问题:我接上U盘,想自动挂载,怎么办?
mdev.conf的格式:
<device regex> <uid>:<gid> <octal permissions> [<@|$|*> <command>]
device regex:正则表达式,表示哪一个设备
uid: owner
gid: 组ID
octal permissions:以八进制表示的属性
@:创建设备节点之后执行命令
$:删除设备节点之前执行命令
*: 创建设备节点之后 和 删除设备节点之前 执行命令
command:要执行的命令
写mdev.conf
1.
leds 0:0 777
led1 0:0 777
led2 0:0 777
led3 0:0 777
2.
leds?[123]? 0:0 777
3.
leds?[123]? 0:0 777 @ echo create /dev/$MDEV > /dev/console
4.
leds?[123]? 0:0 777 * if [ $ACTION = "add" ]; then echo create /dev/$MDEV > /dev/console; else echo remove /dev/$MDEV > /dev/console; fi
5.
leds?[123]? 0:0 777 * /bin/add_remove_led.sh
把命令写入一个脚本:
add_remove_led.sh
#!/bin/sh
if [ $ACTION = "add" ];
then
echo create /dev/$MDEV > /dev/console;
else
echo remove /dev/$MDEV > /dev/console;
fi
6. U盘自动挂载到 /mnt 目录
sda[1-9]+ 0:0 777 * if [ $ACTION = "add" ]; then mount /dev/$MDEV /mnt; else umount /mnt; fi
7.# vi /etc/mdev.conf //修改添加如下:
sda[1-9]+ 0:0 777 * /bin/add_remove_udisk.sh
# vi /bin/add_remove_udisk.sh //如下:
#!/bin/sh
if [ $ACTION = "add" ];
then
mount /dev/$MDEV /mnt;
else
umount /mnt;
fi