由于对sysfs的概念不是太清楚,只是知道sysfs是一种类似于/proc的虚拟文件系统,可以在用户空间读写sysfs中的相应文件访问或控制内核空间数据,后拜读各类文章后终于对sysfs及平台设备有了更深层次的理解,但读的再多不如亲手一试。恰巧公司需要为android系统提供一个马达驱动,并且android又恰巧很多驱动的读写方式都是通过sysfs操作的,于是开始行动……
#define VIBRA_MINOR 88
#define STARTUP_VIBRA_DELAY 150
static unsigned int onkey_stat;
static struct timer_list vibra_timer;
static void vibra_stop_vibration(unsigned long data)
{
struct micco_vibra *vibra = (void *)data;
struct vibra_control *control = vibra->vibra_ctl;
control->level = 0x0;
micco_write(MICCO_VIBRA_CONTROL, control->level);
}
static int raw_vibrate(int level) //驱动马达,level控制转速
{
u8 val;
val = level & 0x00fe;
DMSG("+/-vibra_ioctl/n");
micco_write(MICCO_VIBRA_CONTROL, val);
return 0;
}
static int adv_vibrate(struct vibra_control *ctl) //驱动马达,level控制转速
{
u8 val;
val = ctl->level & 0x00fe;
micco_write(MICCO_VIBRA_CONTROL, val);
mod_timer(&vibra_timer, jiffies + msecs_to_jiffies(ctl->m_time));
return 0;
}
static int vibra_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg) //用户空间通过ioctl操作马达
{
int ret;
unsigned int level;
u8 val;
unsigned int onkey_val;
struct vibra_control ctl;
switch(cmd) {
case VIBRA_RAW_VIBTATION:
ret = copy_from_user(&level, (void *)arg, sizeof(unsigned int));
ret = raw_vibrate(level);
break;
case VIBRA_ADV_VIBTATION:
ret = copy_from_user(&