第一种:fileoparetion里的open close read write ioctl........:
第二种:属性操作中对文件进行读写,这种是重要的,也是常见的:
static ssize_t isa1200_mode_show(struct device *dev,struct device_attribute *attr, char *buf)
{
int enable;
struct isa1200_chip *haptic =container_of(dev,struct isa1200_chip,dev );
return sprintf(buf, "%d/n", haptic->enable);
}
static ssize_t isa1200_mode_store(struct device *dev, struct device_attribute *attr,const char *buf, size_t count)
{
int enable;
struct isa1200_chip *haptic =container_of(dev,struct isa1200_chip,dev );
if (sscanf(buf, "%d", &enable) != 1) return -EINVAL;
if(enable){
haptic->enable=1;
isa1200_chip_set_pwm_cycle(haptic);
isa1200_chip_power_on(haptic);
}else{
haptic->enable=0;
isa1200_chip_power_off(haptic);
}
return count;
}
static DEVICE_ATTR(isa1200_mode, 0644, isa1200_mode_show, isa1200_mode_store);
这就完成了声明,然后再在probe里加上这么一句注册就行了:
ret = device_create_file(&pdev->dev, &dev_attr_isa1200_mode);
printk(KERN_INFO "dev_attr