字符设备驱动实例:自动挂载SD卡
时间:2010-10-28 09:26来源: 作者: 点击:129次
内核驱动文件: l200_mac.c #include l200.h
extern char l200_sddev_name[10]; //define and export in
drivers/usb/storage/usb.c extern int sd_insert_status; //define and
export in drivers/scsi/w90p710_sdio.c extern int macPlugStatus; //define
an
内核驱动文件: l200_mac.c
#include "l200.h"
extern char l200_sddev_name[10]; //define and export in drivers/usb/storage/usb.c
extern int sd_insert_status; //define and export in drivers/scsi/w90p710_sdio.c
extern int macPlugStatus; //define and export in drivers/net/w90p710_mac.c
static int mac_open(struct inode *inode, struct file *file)
{
return 0;
}
static int mac_release(struct inode *inode, struct file *file)
{
return 0;
}
static int mac_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
int status = 0;
char buffer[10];
switch(cmd)
{
case GET_ETH_STATUS: // cmd=1, whether network wire is plugin or not
return macPlugStatus;
case GET_SD_STATUS: // cmd=2, check whether SD card insert or not
if(copy_from_user(buffer, (const char*)arg, sizeof(buffer)) != 0)
return -EFAULT;
printk("Receive argument: %s\n", buffer);
status = sd_insert_status;
if(1 == sd_insert_status)
{
if(copy_to_user((unsigned char*)arg, &l200_sddev_name, 10) != 0)
return -EFAULT;
printk("SD Card insert with device name %s1\n", l200_sddev_name);
}
return status;
default:
return -1;
}
return 0;
}
static struct file_operations mac_fops=
{
.ioctl = mac_ioctl,
.open = mac_open,
.release = mac_release,
};
static void mac_module_exit(void)
{
unregister_chrdev(MAC_MAJOR, MAC_NAME);
}
static int __init mac_module_init(void)
{
if((register_chrdev(MAC_MAJOR, MAC_NAME, &mac_fops)) < 0)
{
mac_module_exit();
printk("Regist L200 MAC device failure...\n");
return -ENODEV;
}
printk("Regist L200 MAC device success...\n");
return 0;
}
module_init(mac_module_init);
module_exit(mac_module_exit);
主动
挂载SD卡措施:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#define GET_SD_STATUS 2
int daemon_init(void)
{
pid_t pid, cpid;
int status;
setsid(); /* become session leader */
chdir("/"); /* change working directory */
umask(0); /* clear file mode creation mask */
close(0); /* close stdin */
close(1); /* close stdout */
close(2); /* close stderr */
switch(pid = vfork()) //parent sleeps, child_proc_1 comes
{
case 0:
waitpid(pid, &status, WUNTRACED);
//vfork succ , child_proc_1 sleeps, child_proc_2 comes
if(cpid = vfork())
{
waitpid(cpid, &status, WUNTRACED);
_exit(0);
}
/* child_proc_2 continues */
return 1;
case -1:
return(-1);//vfork err
default:
exit(0); /* parent only, exit */
}
return(0);
}
int main(void)
{
int fd = -1;
int ret = -1;
int sd_mnt_flag = 0;
char sd_name[10];
char mnt_cmd[40];
if( -1 == daemon_init() )
{
printf("can't fork self\n");
exit(0);
}
if(-1 == (fd=open("/dev/eth", O_RDWR, 755)))
{
perror("open sd device failure\n");
return 0;
}
while(1)
{
sleep(3);//sleep 3 seconds
if(-1 == (ret=ioctl(fd, GET_SD_STATUS, sd_name )) )
{
perror("ioctl on sd failure.\n");
return 0;
}
if( 1==ret && 1!=sd_mnt_flag) //SD card insert, and not mount
{
snprintf(mnt_cmd, sizeof(mnt_cmd), "mount -t vfat /dev/%s1 /media/sdc", sd_name);
system(mnt_cmd);
sd_mnt_flag = 1;
}
else if ( 0==ret && 0!=sd_mnt_flag) // SD card pull out and not umount
{
system("umount /media/sdc");
sd_mnt_flag = 0;
}
}
close(fd);
return 0;
} |
|