linux里的驱动接口

时间:2022-01-07 04:45:25

大致来说分为两种(也许还有许多,等见到了再总结吧):

第一种:fileoparetion里的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_isa1200_mode/n");    

       if (ret != 0)

              dev_err(&pdev->dev,"No source control for dev_attr_isa1200_mode: %d/n", ret);

这个就是常常能见到的驱动文件给用户的接口了。怎么用呢?这个很是困惑我一段时间,网上类似的文章也不多,大概是大家觉得这么简单的东西没必要写吧,那作为菜鸟的我来写:

 

 

如这个device_create_file后会生成的一个文件名叫isa1200_mode的文件,如生成了目录:

/sys/class/……/isa1200/device/isa1200_mode

那么对它进行读写就是这样的:sudo echo 1 >/sys/class/……/isa1200/device/isa1200_mode ,也就是把1写进了文件isa1200_mode里,那么属性机制就会分析这个操作,并调用isa1200设备驱动的isa1200_mode_store函数,并会把写入的1当作参数*buf来处理。通过一句

if (sscanf(buf, "%d", &enable) != 1)就把1传给了enable,一直进行接下来的工作。这下面的工作当然就好理解了,isa1200_mode_store里也是调用驱动中写好的函数来完成操作工作。至于读操作:……………………

 

上面说是操作只是通常的用户接口,对于不同的设备还是会有其它更好的接口:

1.       led_classdev,led类的一个常用接口:brightness

 

static void w380_iconled_set(struct led_classdev *led_dev,

                            enum led_brightness value)

{

       switch (value) {

       case 255:

              s3c_gpio_setpin(S5PV210_GPJ3(2), 1);

              break;

       default:

       case 0:

              s3c_gpio_setpin(S5PV210_GPJ3(2), 0);

              break;

       }

}

static struct led_classdev w380_iconled = {

       .name                    = "w380:icon",

       .brightness_set       = w380_iconled_set,

 

};

这就完成了声明,然后再在probe里加上这么一句注册就行了:

       ret = led_classdev_register(&pdev->dev, &w380_iconled);

       if (ret)

              goto err_icon;

 

通过led_classdev_register就会生成目录:/sys/class/leds/ w380:icon

及其下面的/sys/class/leds/ w380:icon/brightness文件。对了,接口就是brightness文件

 

那么对它进行读写就是这样的:sudo echo 255 >/sys/class/leds/w380:icon/brightness 这样就打了这个LED灯。关闭LED灯的操作就是sudo echo 0>/sys/class/leds/w380:icon/brightness

 

2.       timed_output_dev时间输出类的一个常用接口:enable

static void isa1200_chip_enable(struct timed_output_dev *dev, int value)

{

       struct isa1200_chip *haptic = container_of(dev, struct isa1200_chip,dev);

       unsigned long flags;

       int ret;

       if (value) {

              haptic->enable=1;  

              ret = isa1200_chip_set_pwm_cycle(haptic);

              if (ret) {

                     dev_dbg(haptic->dev.dev, "set_pwm_cycle failed/n");

                     return;

              }

              isa1200_chip_power_on(haptic);

              mdelay(value);

              haptic->enable=0;  

              isa1200_chip_power_off(haptic);        

             

       } else {

              haptic->enable=0;  

              isa1200_chip_power_off(haptic);

       }

}

这就完成了声明,然后再在probe里加上这么一句注册就行了:

       ret = timed_output_dev_register(&haptic->dev);

       if (ret < 0)

              goto timed_reg_fail;

 

通过timed_output_dev_register就会生成目录:/sys/class/timed_output/isa1200

及其下面的/sys/class/timed_output/isa1200/enable文件。对了,接口就是enable文件

 

那么对它进行读写就是这样的:sudo echo 1 >/sys/class/timed_output/isa1200/enable这样就打了这个motor。关闭motor的操作就是sudo echo 0>/sys/class/timed_output/isa1200/enable

 

3.       switch_devandroid中常用于耳机设备:switch_class接口是namestate

 

通过switch_dev_register就会生成目录:/sys/class/switch/h2w

及其下面的"/sys/class/switch/h2w/name"; "/sys/class/switch/h2w/state";文件。对了,接口就是这两个文件

由驱动通过中断将判断得出的耳机名称和状态,写入name state

应用程序对耳机进行名称和状态的判断就靠读出这两个文件得到。

 

 

4.待续……