在/sys下面创建属性文件

以前一直有一个疑问:驱动到底在哪里创建文件节点才有意义,才能满足应用层的需求。后来一直得不到解答,慢慢的就不干觉得有困惑。因为android的开发

,从原厂拿到bsp大多数驱动都已经做好了,功能健全,通过HAL提供本地方法。其实事实还有其他的途径提供驱动接口,比如字符设备,创建一个文件节点,

然后JNI层包装一下节点的操作函数,向java层提供public class;输入设备,上报按键,触摸屏等输入设备的值;在/sys下创建属性文件,对设备/驱动/总线

提供直接操作的方法,在java层直接可以调用shell语句来操作文件,完成访问硬件驱动资源的任务。。。

正所谓条条大道通罗马!^_^

下面是重点,在驱动中使用/sys文件系统操作方法创建属性文件的过程如下面的两个例子,标红的为重点过程。

1,创建一个属性文件

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/fb.h>
#include <linux/backlight.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <mach/platform.h>
#include <linux/delay.h>


#define GPIO_OUTA ((PAD_GPIO_B + 28) | PAD_FUNC_ALT0)
#define GPIO_OUTB ((PAD_GPIO_D + 28) | PAD_FUNC_ALT0)

unsigned long isLOCK =0;
static ssize_t lock_show(struct device* cd,struct device_attribute *attr, char* buf)
{
ssize_t ret = 0;

sprintf(buf, "lock_show %d\n",isLOCK);

ret = strlen(buf) + 1;
return ret;
}
/*echo 1 >/sys/devices/platform/rk29-keypad/doorlock open
**echo 0 >/sys/devices/platform/rk29-keypad/doorlock close
**echo 2 >/sys/devices/platform/rk29-keypad/doorlock stop
*/
static ssize_t lock_store(struct device* cd, struct device_attribute *attr,
const char* buf, size_t len)
{
unsigned long on_off = simple_strtoul(buf, NULL, 10);
isLOCK = on_off;
printk("%s: %d\n",__func__, isLOCK);
if(isLOCK == 1){
gpio_direction_output(GPIO_OUTA, 1);
gpio_direction_output(GPIO_OUTB, 0);
mdelay(150);
}else if(isLOCK == 0){
gpio_direction_output(GPIO_OUTA, 0);
gpio_direction_output(GPIO_OUTB, 1);
mdelay(150);
}else{
gpio_direction_output(GPIO_OUTA, 1);
gpio_direction_output(GPIO_OUTB, 1);
}
gpio_direction_output(GPIO_OUTA, 1);
gpio_direction_output(GPIO_OUTB, 1);
return len;
}

static DEVICE_ATTR(doorLock, S_IRUGO | S_IWUSR, lock_show, lock_store);

/*
*GPIO_OUTA GPIO_OUTB STATE
* 0 0 wait
* 1 0 + run
* 0 1 - run
* 1 1 stop 
*/
static int lock_gpio_probe(struct platform_device *pdev)
{
int err=0;
printk("=====lock_gpio_probe start=======\n");
gpio_request(GPIO_OUTA,"gpio_outa");
gpio_direction_output(GPIO_OUTA, 0);
gpio_request(GPIO_OUTB,"gpio_outb");
gpio_direction_output(GPIO_OUTB, 0);
printk("=====lock_gpio_probe over=======\n");

if ((err = device_create_file(&pdev->dev, &dev_attr_doorLock)))
goto err_out;
return 0 ;
err_out:
return 1;

}

static int lock_gpio_remove(struct platform_device *pdev)
{
return 0 ;
}


static struct platform_driver lock_gpio_driver = {
.driver = {
.name = "rk29-keypad",//lock-gpio
.owner = THIS_MODULE,
},
.probe = lock_gpio_probe,
.remove = lock_gpio_remove,
};

module_platform_driver(lock_gpio_driver);

MODULE_DESCRIPTION("gpio based lock Driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Eliot_shao");


dev_attr_doorLock就是DEVICE_ATTR宏创建的变量!lock_show&lock_store是属性文件的读写方法,实现方法见历程。

2,创建一组属性文件

#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/pci.h>
#include <linux/input.h>
#include <linux/platform_device.h>


struct input_dev *vms_input_dev;        /* Representation of an input device */
static struct platform_device *vms_dev; /* Device structure */


                                        /* Sysfs method to input simulated
                                           coordinates to the virtual
                                           mouse driver */
static ssize_t
write_vms(struct device *dev,
          struct device_attribute *attr,
          const char *buffer, size_t count)
{
  int x,y;
  sscanf(buffer, "%d%d", &x, &y);
                                        /* Report relative coordinates via the
                                           event interface */
  input_report_rel(vms_input_dev, REL_X, x);
  input_report_rel(vms_input_dev, REL_Y, y);
  input_sync(vms_input_dev);
  return count;
}
/* Attach the sysfs write method */
DEVICE_ATTR(coordinates, 0644, NULL, write_vms);
/* Attribute Descriptor */
static struct attribute *vms_attrs[] = {
  &dev_attr_coordinates.attr,
  NULL
};
/* Attribute group */
static struct attribute_group vms_attr_group = {
  .attrs = vms_attrs,
};
/* Driver Initialization */
int __init
vms_init(void)
{
  /* Register a platform device */
  vms_dev = platform_device_register_simple("vms", -1, NULL, 0);
  if (IS_ERR(vms_dev)) {
    PTR_ERR(vms_dev);
    printk("vms_init: error\n");
  }
  /* Create a sysfs node to read simulated coordinates */
  sysfs_create_group(&vms_dev->dev.kobj, &vms_attr_group);
  /* Allocate an input device data structure */
  vms_input_dev = input_allocate_device();
  if (!vms_input_dev) {
    printk("Bad input_alloc_device()\n");
  }
  /* Announce that the virtual mouse will generate
     relative coordinates */
  set_bit(EV_REL, vms_input_dev->evbit);
  set_bit(REL_X, vms_input_dev->relbit);
  set_bit(REL_Y, vms_input_dev->relbit);


  /* Register with the input subsystem */
  input_register_device(vms_input_dev);
  printk("Virtual Mouse Driver Initialized.\n");
  return 0;
}
/* Driver Exit */
void
vms_cleanup(void)
{
  /* Unregister from the input subsystem */
  input_unregister_device(vms_input_dev);
  /* Cleanup sysfs node */
  sysfs_remove_group(&vms_dev->dev.kobj, &vms_attr_group);
  /* Unregister driver */
  platform_device_unregister(vms_dev);
  return;
}


module_init(vms_init);
module_exit(vms_cleanup);</span>

属性文件的操作方法很简单,如

echo 1  >/sys/devices/platform/lock-gpio/doorlock 

cat /sys/devices/platform/lock-gpio/doorlock 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值