USB总线-Linux内核USB3.0设备控制器复合设备之legacy方式分析(八)

1.概述

在usb gadget configfs引入到内核之前,内核都使用硬编码的方式实现复合设备,无法在用户空间动态修改和绑定不同的function驱动,若要修改,则需要修改内核代码,重新编码,非常不方便。目前这部分代码在被放到drivers/usb/gadget/legacy/目录下。被编译成内核模块时,名称以g开头,如音频设备g_audio.ko、串口设备g_serial.ko、CDC设备及大容量存储设备g_multi.ko。USB gadget configfs和legacy相比只是实现复合设备的形式不同而已,设备的功能最终还是要通过function驱动实现。下面以音频复合设备为例,分析g_audio驱动的工作过程。

2.音频复合设备驱动

从前面的分析中可以看出,复合设备驱动围绕usb_composite_driverusb_composite_dev两个数据结构展开,legacy方式的复合设备驱动也不例外。

2.1.定义

音频复合设备的实现在drivers/usb/gadget/legacy/audio.c文件中,其usb_composite_driver数据结构定义如下。最重要的是audio_bindaudio_unbind两个回调函数,在g_audio驱动绑定时和解除绑定时调用。g_audio驱动使用module_usb_composite_driver宏注册到内核中,初始化函数为usb_composite_probe,卸载函数为usb_composite_unregister

[drivers/usb/gadget/legacy/audio.c]
static struct usb_composite_driver audio_driver = {
	.name		= "g_audio",       // 驱动名称
	.dev		= &device_desc,    // USB设备描述符,USB复合设备只有一个设备描述符
	.strings	= audio_strings,   // 字符串
	.max_speed	= USB_SPEED_HIGH,  // USB设备最大速度,USB2.0
	.bind		= audio_bind,      // audio composite驱动bind回调函数
	.unbind		= audio_unbind,    // audio composite驱动unbind回调函数
};
module_usb_composite_driver(audio_driver);  // 注册audio_driver

[include/linux/usb/composite.h]
#define module_usb_composite_driver(__usb_composite_driver) \
	module_driver(__usb_composite_driver, usb_composite_probe, \
		       usb_composite_unregister)

module_driver宏定义如下,最终还是通过module_init初始化,module_exit宏卸载。

[include/linux/device.h]
#define module_driver(__driver, __register, __unregister, ...) \
static int __init __driver##_init(void) \
{ \
	return __register(&(__driver) , ##__VA_ARGS__); \
} \
module_init(__driver##_init); \
static void __exit __driver##_exit(void) \
{ \
	__unregister(&(__driver) , ##__VA_ARGS__); \
} \
module_exit(__driver##_exit);

2.2.参数

g_audio驱动将音频参数定义为模块参数,如下所示,加载模块的时候可以指定选项修改这些参数的默认值。

[drivers/usb/gadget/legacy/audio.c]
/* Playback(USB-IN) Default Stereo - Fl/Fr */
static int p_chmask = UAC2_DEF_PCHMASK;
module_param(p_chmask, uint, S_IRUGO);
MODULE_PARM_DESC(p_chmask, "Playback Channel Mask");

/* Playback Default 48 KHz */
static int p_srate = UAC2_DEF_PSRATE;
module_param(p_srate, uint, S_IRUGO);
MODULE_PARM_DESC(p_srate, "Playback Sampling Rate");

/* Playback Default 16bits/sample */
static int p_ssize = UAC2_DEF_PSSIZE;
module_param(p_ssize, uint, S_IRUGO);
MODULE_PARM_DESC(p_ssize, "Playback Sample Size(bytes)");

/* Capture(USB-OUT) Default Stereo - Fl/Fr */
static int c_chmask = UAC2_DEF_CCHMASK;
module_param(c_chmask, uint, S_IRUGO);
MODULE_PARM_DESC(c_chmask, "Capture Channel Mask");

/* Capture Default 64 KHz */
static int c_srate = UAC2_DEF_CSRATE;
module_param(c_srate, uint, S_IRUGO);
MODULE_PARM_DESC(c_srate, "Capture Sampling Rate");

/* Capture Default 16bits/sample */
static int c_ssize = UAC2_DEF_CSSIZE;
module_param(c_ssize, uint, S_IRUGO);
MODULE_PARM_DESC(c_ssize, "Capture Sample Size(bytes)");

驱动模块加载成功后,这些参数会被导出到用户空间,目录为/sys/module/g_audio/parameters。

2.3.初始化

g_audio驱动调用usb_composite_probe函数初始化,执行流程如下图所示。和USB gadget configfs定义的复合设备驱动初始化流程相似,只是设置的usb_gadget_driver不同。USB gadget configfs定义的usb_gadget_driverconfigfs_driver_template,而legacy方式定义的usb_gadget_drivercomposite_driver_templatecomposite_driver_template是function驱动和UDC驱动沟通的桥梁,在适当的时机会被UDC驱动回调。

[drivers/usb/gadget/composite.c]
static const struct usb_gadget_driver composite_driver_template = {
	.bind		= composite_bind,
	.unbind		= composite_unbind,

	.setup		= composite_setup,
	.reset		= composite_disconnect,
	.disconnect	= composite_disconnect,

	.suspend	= composite_suspend,
	.resume		= composite_resume,

	.driver	= {
		.owner		= THIS_MODULE,
	},
};

usb_composite_probe的工作流程如下:

  1. 设置usb_gadget_drivercomposite_driver_template
  2. audio_driver设置的最大速度设置到composite_driver_template,表明该复合设备支持的最大速度。
  3. 调用UDC驱动接口,找到合适的USB设备控制器绑定composite_driver_template
    1. 遍历udc_list链表,查找第一个USB控制器。USB gadget configfs根据名称查找USB设备控制器,而legacy方式只匹配第一个USB设备控制器,无法匹配指定的USB设备控制器。
    2. 找到USB设备控制器后,UDC数据结构保存composite_driver_template,此时就完成了UDC绑定composite_driver_template
    3. 回调composite_driver_template定义的composite_bind函数,将复合设备和function驱动绑定。
    4. 分配端点0的usb_request、分配USB请求的缓冲区、设置usb_request的回调函数、复位所有端点,并将gadget的端点数量清零。
    5. 回调复合设备驱动g_audio定义的audio_bind函数。复合设备和function驱动绑定在这里完成,主要是添加配置、调用f_uac2驱动的afunc_alloc_instafunc_alloc函数创建usb_function_instanceusb_function、调用f_uac2驱动的 afunc_bind 函数。
    6. 如果使用os_string,则需要分配os_string requset、分配USB请求的缓冲区、设置USB请求的回调函数。
    7. audio_driver中定义的设备描述符更新到usb_composite_dev中。
    8. usb_composite_driver中定义的设备描述符更新到usb_composite_dev中。

usb_composite_dev

3.总结

从上面可以看出,legacy方式定义的复合设备很不灵活,使用者无法在用户空间动态配置复合设备和绑定的function驱动。若要使用音频设备,则只能通过g_audio驱动构造复合设备,若使用USB虚拟网卡,则只能通过g_ether驱动构造复合设备,若需要多个功能的USB设备,则需要重新构造复合设备,编码定义usb_composite_driver。USB gadget configfs不需要在内核中预先定义好复合设备,使用者在用户空间配置,内核会自动生成所需的复合设备,并和对应的function驱动绑定。

参考资料

  1. Rockchip RK3399TRM V1.3 Part1
  2. Rockchip RK3399TRM V1.3 Part2
  3. Linux内核4.4.179版本源码
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Universal Serial Bus Type-C Cable and Connector Specification Release 1.3 July 14, 2017 CONTENTS Specification Work Group Chairs / Specification Editors ................................................... 11 Specification Work Group Contributors .................................................................................. 11 Pre-Release Draft Industry Reviewing Companies That Provided Feedback ................ 14 Revision History ............................................................................................................................. 14 1 Introduction ............................................................................................................................ 15 1.1 Purpose ......................................................................................................................... 15 1.2 Scope ............................................................................................................................. 15 1.3 Related Documents .................................................................................................... 16 1.4 Conventions ................................................................................................................. 16 1.4.1 Precedence .................................................................................................... 16 1.4.2 Keywords ...................................................................................................... 16 1.4.3 Numbering .................................................................................................... 17 1.5 Terms and Abbreviations ......................................................................................... 17 2 Overview ................................................................................................................................. 21 2.1 Introduction ................................................................................................................ 21 2.2 USB Type-C Receptacles, Plugs and Cables ......................................................... 22 2.3 Configuration Process ............................................................................................... 23 2.3.1 Source-to-Sink Attach/Detach Detection .............................................. 24 2.3.2 Plug Orientation/Cable Twist Detection ............................................... 24 2.3.3 Initial Power (Source-to-Sink) Detection and Establishing the Data (Host-to-Device) Relationship ................................................................. 24 2.3.4 USB Type-C VBUS Current Detection and Usage .................................. 25 2.3.5 USB PD Communication ............................................................................. 25 2.3.6 Functional Extensions ................................................................................ 26 2.4 VBUS ............................................................................................................................... 26 2.5 VCONN ............................................................................................................................. 27 2.6 Hubs ............................................................................................................................... 27 3 Mechanical ............................................................................................................................... 28 3.1 Overview ...................................................................................................................... 28 3.1.1 Compliant Connectors................................................................................ 28 3.1.2 Compliant Cable Assemblies .................................................................... 28 3.1.3 Compliant USB Type-C to Legacy Cable Assemblies .......................... 28 3.1.4 Compliant USB Type-C to Legacy Adapter Assemblies ..................... 29 3.2 USB Type-C Connector Mating Interfaces ............................................................ 29 3.2.1 Interface Definition .................................................................................... 29 3.2.2 Reference Designs ....................................................................................... 52 3.2.3 Pin Assignments and Descriptions ......................................................... 59 3.3 Cable Construction and Wire Assignments ......................................................... 60 3.3.1 Cable Construction (Informative) ........................................................... 60 3.3.2 Wire Assignments ....................................................................................... 62 3.3.3 Wire Gauges and Cable Diameters (Informative) ............................... 63 3.4 Standard USB Type-C Cable Assemblies .............................................................. 65 3.4.1 USB Full-Featured Type-C Cable Assembly .......................................... 65 3.4.2 USB 2.0 Type-C Cable Assembly .............................................................. 66 3.4.3 USB Type-C Captive Cable Assemblies .................................................. 67 3.5 Legacy Cable Assemblies .......................................................................................... 67 3.5.1 USB Type-C to USB 3.1 Standard-A Cable Assembly .......................... 68 3.5.2 USB Type-C to USB 2.0 Standard-A Cable Assembly .......................... 69 3.5.3 USB Type-C to USB 3.1 Standard-B Cable Assembly .......................... 70 3.5.4 USB Type-C to USB 2.0 Standard-B Cable Assembly .......................... 71 3.5.5 USB Type-C to USB 2.0 Mini-B Cable Assembly ................................... 72 3.5.6 USB Type-C to USB 3.1 Micro-B Cable Assembly ................................. 73 3.5.7 USB Type-C to USB 2.0 Micro-B Cable Assembly ................................. 75 3.6 Legacy Adapter Assemblies ..................................................................................... 76 3.6.1 USB Type-C to USB 3.1 Standard-A Receptacle Adapter Assembly 76 3.6.2 USB Type-C to USB 2.0 Micro-B Receptacle Adapter Assembly ...... 78 3.7 Electrical Characteristics ......................................................................................... 79 3.7.1 Raw Cable (Informative) ........................................................................... 79 3.7.2 USB Type-C to Type-C Passive Cable Assemblies (Normative) ....... 80 3.7.3 Mated Connector (Informative) .............................................................. 93 3.7.4 USB Type-C to Legacy Cable Assemblies (Normative) ...................... 97 3.7.5 USB Type-C to USB Legacy Adapter Assemblies (Normative) ....... 101 3.7.6 Shielding Effectiveness Requirements (Normative) ........................ 103 3.7.7 DC Electrical Requirements (Normative) ........................................... 106 3.8 Mechanical and Environmental Requirements (Normative) ........................ 109 3.8.1 Mechanical Requirements ....................................................................... 109 3.8.2 Environmental Requirements ................................................................ 115 3.9 Docking Applications (Informative) ................................................................... 116 3.10 Implementation Notes and Design Guides ........................................................ 117 3.10.1 EMC Management (Informative) ........................................................... 117 3.10.2 Stacked and Side-by-Side Connector Physical Spacing (Informative) .............................................................................................. 120 3.10.3 Cable Mating Considerations (Informative) ...................................... 120 4 Functional .............................................................................................................................. 122 4.1 Signal Summary ........................................................................................................ 122 4.2 Signal Pin Descriptions ........................................................................................... 122 4.2.1 SuperSpeed USB Pins ............................................................................... 122 4.2.2 USB 2.0 Pins ................................................................................................ 123 4.2.3 Auxiliary Signal Pins ................................................................................ 123 4.2.4 Power and Ground Pins ........................................................................... 123 4.2.5 Configuration Pins .................................................................................... 123 4.3 Sideband Use (SBU) ................................................................................................. 123 4.4 Power and Ground ................................................................................................... 123 4.4.1 IR Drop ......................................................................................................... 123 4.4.2 VBUS ............................................................................................................... 124 ... ...

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值