//现在看看第二阶段的重头戏,看看设备是怎么从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);
}