当前位置: 首页 > news >正文

linux USB --- 监听 USB 角色

 

#include <linux/device.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>
#include <linux/usb/typec_dp.h>
#include <linux/usb/typec_mux.h>
#include <linux/usb/role.h>
#include <linux/kernel.h>
#include <linux/of_device.h>
#include <linux/of_gpio.h>struct usb_switch {struct device *dev;struct usb_role_switch *usb_role;struct mutex lock;char role;
};static int usb_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
{struct usb_switch *usb_switch = usb_role_switch_get_drvdata(sw);struct device *dev = usb_switch->dev;// struct platform_device *pdev = container_of(sw, struct platform_device, usb_role);// struct usb_switch *usb_switch = platform_get_drvdata(pdev);
    printk("%s role=%d\n", __func__, role);usb_switch->role = role;kobject_uevent(&dev->kobj, KOBJ_CHANGE);return 0;
}static enum usb_role usb_switch_get_role(struct usb_role_switch *sw)
{// struct platform_device *pdev = container_of(dev, struct platform_device, dev);// struct usb_switch *usb_switch = platform_get_drvdata(pdev);struct usb_switch *usb_switch = usb_role_switch_get_drvdata(sw);// struct device *dev = usb_switch->dev;
    printk("%s role=%d\n", __func__, usb_switch->role);return usb_switch->role;
}static ssize_t role_show(struct device *dev, struct device_attribute *attr, char *buf)
{struct platform_device *pdev = container_of(dev, struct platform_device, dev);struct usb_switch *usb_switch = platform_get_drvdata(pdev);return sprintf(buf, "%d", usb_switch->role);
}DEVICE_ATTR(role, S_IRUGO, role_show, NULL);static int usb_switch_probe(struct platform_device *pdev)
{int ret;struct usb_role_switch_desc role_desc = {};struct device *dev = &pdev->dev;struct usb_switch *usb_switch;printk("%s start\n", __func__);usb_switch = devm_kzalloc(dev, sizeof(*usb_switch), GFP_KERNEL);if (!usb_switch)return -ENOMEM;usb_switch->dev = dev;platform_set_drvdata(pdev, usb_switch);mutex_init(&usb_switch->lock);role_desc.set = usb_switch_set_role;role_desc.get = usb_switch_get_role;role_desc.allow_userspace_control = true;role_desc.fwnode = dev_fwnode(dev);role_desc.driver_data = usb_switch;usb_switch->usb_role = usb_role_switch_register(dev, &role_desc);if (IS_ERR(usb_switch->usb_role)){dev_err(dev, "failed to register typec switch\n");return ret;}device_create_file(dev, &dev_attr_role);// platform_set_drvdata(pdev, usb_switch);return 0;
}static int usb_switch_remove(struct platform_device *pdev)
{struct device *dev = &pdev->dev;struct usb_switch *usb_switch = platform_get_drvdata(pdev);device_remove_file(dev, &dev_attr_role);usb_role_switch_unregister(usb_switch->usb_role);return 0;
}static const struct of_device_id usb_switch_match[] = {{.compatible = "usb-switch",},{}
};
MODULE_DEVICE_TABLE(of, usb_switch_match);static struct platform_driver usb_switch_driver = {.probe = usb_switch_probe,.remove = usb_switch_remove,.driver = {.name = "usb_switch",.of_match_table = usb_switch_match,},
};
module_platform_driver(usb_switch_driver);MODULE_AUTHOR("XXX");
MODULE_DESCRIPTION("usb-switch driver");
MODULE_LICENSE("GPL");

 

http://www.rkmt.cn/news/48826.html

相关文章:

  • 温州工友自动包装设备有限公司:专注螺丝五金智能包装,助力企业降本增效
  • 25.11.09
  • [豪の学习笔记] Spring框架学习碎碎念#5
  • LucaOne模型的词汇表系统
  • 2025 年终端数据安全软件公司推荐数篷科技(深圳)有限公司,数据安全领域的坚实力量
  • 网络协议工程 - eNSP及相关软件安装 - [eNSP, VirtualBox, WinPcap, Wireshark, Win7] - 教程
  • 20232314 2025-2026-1 《网络与系统攻防技术》实验五实验报告
  • dify插件开发
  • 其他游戏攻略
  • 11.13 模拟赛 T3
  • 动态路由协议
  • 2025-11-13 PQ v.Next日志记录
  • vscode集成MCP Server
  • 框架架构设计师备考第41天——软件可靠性建模、管理与设计​
  • 奇怪的问题(们)
  • 基于多模态AI技术的传统行业智能化升级路径研究——以开源AI大模型、AI智能名片与S2B2C商城小程序为例 - 实践
  • 2025智慧康养/智慧养老标杆机构推荐榜:教之道五星领跑 实训室建设与虚拟仿真领域 3 家公司凭实力上榜
  • coze 搭建能写文案导出word pdf
  • Siemens PLCSIM V18
  • 《密码系统设计》第十二周预习
  • 1 移动端开发概念与环境准备
  • 八大排序总结
  • (八大排序)快速排序(递归)
  • (八大排序)冒泡排序
  • (八大排序)堆排序
  • #20232329 2025-2026-1 《网络与系统攻防技术》 实验六实验报告
  • (八大排序)希尔排序
  • 壅土(拼音:yōng tǔ)
  • 2025-11-13 早报新闻
  • P14463 【MX-S10-T4】『FeOI-4』呼吸之野