Android drivers/switch驱动详解(用于通过GPIO状态检测耳机、HDMI等的插拔状态)


一、相关文件:

/drivers/switch/switch_gpio.c

/drivers/switch/switch_class.c


二、节点创建流程:

1、在/sys/class/目录下创建“switch”类,创建完成后出现/sys/class/switch,具体实现如下:

struct class *switch_class;

switch_class = class_create(THIS_MODULE, "switch");


2、在/sys/class/switch目录下创建某个具体设备(比如“h2w”),创建完成后出现/sys/class/switch/h2w,具体实现如下:

struct switch_dev *sdev;

sdev->dev = device_create(switch_class, NULL,
MKDEV(0, sdev->index), NULL, sdev->name); // 其中sdev->name赋值为“h2w”


3、在/sys/class/switch/h2w目录下创建设备的属性文件(比如“name”和“state”),创建完成后出现/sys/class/switch/h2w/name/sys/class/switch/h2w/state,具体实现如下:

static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, state_show, NULL); // state_show实现对state属性文件读操作
static DEVICE_ATTR(name, S_IRUGO | S_IWUSR, name_show, NULL);//name_show实现对name属性文件读操作

int ret;

ret = device_create_file(sdev->dev, &dev_attr_name);
ret = device_create_file(sdev->dev, &dev_attr_state);


三、节点创建关键函数:

1、class_create

该函数用于创建一个class;


2、device_create

/**
 * device_create - 创建一个设备并将它注册到sysfs文件系统里
 * @class: 指向class_create函数创建的class
 * @parent: pointer to the parent struct device of this new device, if any
 * @devt: the dev_t for the char device to be added
 * @drvdata: the data to be added to the device for callbacks
 * @fmt: 设备名称
 */
struct device *device_create(struct class *class, struct device *parent,
    dev_t devt, void *drvdata, const char *fmt, ...);


3、device_create_file

/**
 * device_create_file - 创建设备的属性文件
 * @dev: 指定具体为哪个设备创建属性文件
 * @attr: 该设备对应的属性描述
 */
int device_create_file(struct device *dev,
      const struct device_attribute *attr);













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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值