尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

linux USB --- 监听 USB 角色

linux USB --- 监听 USB 角色
📅 发布时间:2026/6/19 13:33:44

 

#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");

 

相关新闻

  • 温州工友自动包装设备有限公司:专注螺丝五金智能包装,助力企业降本增效
  • 25.11.09
  • [豪の学习笔记] Spring框架学习碎碎念#5

最新新闻

  • 电脑日常维护与故障处理,《保姆级教程》
  • 术语俗话 --- 防火墙/IDS/IPS/WAF
  • 2026 沈阳翡翠回收横评!跑遍沈河和平,靠谱商家盘点 - 逸程
  • 鸣潮智能管家:重新定义你的游戏时间管理艺术
  • drand核心概念解析:阈值签名与BLS12-381密码学原理
  • 2026聊城放心贵金属回收,CCIC 中检授权收黄金回收铂金回收白银回收持证实体门店 - 诚金汇钻回收公司

日新闻

  • 5分钟掌握Python进化算法:Geatpy高性能优化工具完全指南
  • Microchip 24AA044 EEPROM选型与应用全指南:从参数解析到实战编程
  • 华为的鸿蒙到底有多牛?为什么称作遥遥领先?

周新闻

  • 3步解锁iOS设备:applera1n激活锁绕过完全指南
  • 39 2026 人工智能证书终极盘点,普通人选 AI 证书可以从这些方向入手
  • Redis 暴露公网有多危险?从端口检查到补救步骤

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号