USB Core 十一

本文详细解析了USB设备从Address状态到Configured状态的转换过程,重点介绍了usb_disable_device函数的作用,包括禁用端点和解除接口注册,以及usb_disable_endpoint函数如何处理端点禁用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//现在看看第二阶段的重头戏,看看设备是怎么从Address 进入Configured 的
//usb_disable_device 函数的清理工作主要有两部分,一是将设备
里所有端点给disable 掉,一是将设备当前配置使用的每个接口都从系统里给unregister
掉,也就是将接口和它对应的驱动给分开.
void usb_disable_device(struct usb_device *dev, int skip_ep0)
 {
 int i;

 dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__,
 skip_ep0 ? "non-ep0" : "all");
 for (i = skip_ep0; i < 16; ++i) {
 usb_disable_endpoint(dev, i);
 usb_disable_endpoint(dev, i + USB_DIR_IN);
 }
 dev->toggle[0] = dev->toggle[1] = 0;


 /* getting rid of interfaces will disconnect
 * any drivers bound to them (a key side effect)
 */
//这个for 循环就是将这个配置的每个接口从设备模型的体系中删除掉,将它们和对应的接口驱动分开,没有驱动了,这些接口也就丧失了能力
 if (dev->actconfig)//actconfig 表示的是设备当前激活的配置,只有它不为空时才有接下来清理的必要。
 {
 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
 struct usb_interface *interface;

 /* remove this interface if it has been registered */
 interface = dev->actconfig->interface[i];
 if (!device_is_registered(&interface->dev))
 continue;
 dev_dbg (&dev->dev, "unregistering interface %s\n",
 interface->dev.bus_id);
 usb_remove_sysfs_intf_files(interface);
 device_del (&interface->dev);
 }

 /* Now that the interfaces are unbound, nobody should
 * try to access them.
 */
/*
将actconfig 的interface 数组置为空,然后再将actconfig 置为空,为什么只是置为空,既然要清理actconfig,为什么不直接将它占用的内存给释放掉?你这里只是不想让设备当前激活任何一个配置而已,没必要将actconfig 指向的那个配置给释放掉吧,
*/
 for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
 put_device (&dev->actconfig->interface[i]->dev);
 dev->actconfig->interface[i] = NULL;
 }
 dev->actconfig = NULL;
 if (dev->state == USB_STATE_CONFIGURED)//如果这个设备此时确实是在Configured 状态,就让它回到Address
 usb_set_device_state(dev, USB_STATE_ADDRESS);
 }
 }
/*
这个函数先获得端点号和端点的方向,然后从ep_in 或ep_out 两个数组里取出端点的
struct usb_host_endpoint 结构体,并将数组里的对应项置为空
*/
void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
{
 unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
 struct usb_host_endpoint *ep;

 if (!dev)
 return;

 if (usb_endpoint_out(epaddr)) {
 ep = dev->ep_out[epnum];
 dev->ep_out[epnum] = NULL;

 } else {
 ep = dev->ep_in[epnum];
 dev->ep_in[epnum] = NULL;
 }
 if (ep && dev->bus)
 usb_hcd_endpoint_disable(dev, ep);
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值