📄 hub.c
字号:
/* Reset the device, and detect its speed */
if (hub_port_reset(hub, port, dev, delay)) {
usb_put_dev(dev);
break;
}
/* Find a new address for it */
if (DevcoosenAdress==0) {
usb_choose_address(dev);
DevcoosenAdress = dev->devnum;
}
dev->devnum = DevcoosenAdress ;
/* Set up TT records, if needed */
if (hub->tt) {
dev->tt = hub->tt;
dev->ttport = hub->ttport;
} else if (dev->speed != USB_SPEED_HIGH
&& hub->speed == USB_SPEED_HIGH) {
dev->tt = &hubstate->tt;
dev->ttport = port + 1;
}
/* Save readable and stable topology id, distinguishing
* devices by location for diagnostics, tools, etc. The
* string is a path along hub ports, from the root. Each
* device's id will be stable until USB is re-cabled, and
* hubs are often labeled with these port numbers.
*
* Initial size: ".NN" times five hubs + NUL = 16 bytes max
* (quite rare, since most hubs have 4-6 ports).
*/
pdev = dev->parent;
if (pdev->devpath [0] != '0') /* parent not root? */
len = snprintf (dev->devpath, sizeof dev->devpath,
"%s.%d", pdev->devpath, port + 1);
/* root == "0", root port 2 == "2", port 3 that hub "2.3" */
else
len = snprintf (dev->devpath, sizeof dev->devpath,
"%d", port + 1);
if (len == sizeof dev->devpath)
dev_err (&hubstate->intf->dev,
"devpath size! usb/%03d/%03d path %s\n",
dev->bus->busnum, dev->devnum, dev->devpath);
dev_info (&hubstate->intf->dev,
"new USB device on port %d, assigned address %d\n",
port + 1, dev->devnum);
/* put the device in the global device tree. the hub port
* is the "bus_id"; hubs show in hierarchy like bridges
*/
dev->dev.parent = dev->parent->dev.parent->parent;
/* Run it through the hoops (find a driver, etc) */
if (!usb_new_device(dev, &hub->dev)) {
hub->children[port] = dev;
usbfs_add_device(dev);
goto done;
}
/* Free the configuration if there was an error */
usb_put_dev(dev);
/* Switch to a long reset time */
delay = HUB_LONG_RESET_TIME;
}
hub_port_disable(hub, port);
done:
up(&usb_address0_sem);
}
static void hub_events(void)
{
unsigned long flags;
struct list_head *tmp;
struct usb_device *dev;
struct usb_hub *hub;
u16 hubstatus;
u16 hubchange;
u16 portstatus;
u16 portchange;
int i, ret;
int m=0;
/*
* We restart the list every time to avoid a deadlock with
* deleting hubs downstream from this one. This should be
* safe since we delete the hub from the event list.
* Not the most efficient, but avoids deadlocks.
*/
//DPRINT1("hub_events() called\n");
while (m<5) {
m++;
spin_lock_irqsave(&hub_event_lock, flags);
if (list_empty(&hub_event_list))
break;
/* Grab the next entry from the beginning of the list */
tmp = hub_event_list.next;
hub = list_entry(tmp, struct usb_hub, event_list);
dev = interface_to_usbdev(hub->intf);
list_del_init(tmp);
if (unlikely(down_trylock(&hub->khubd_sem)))
BUG(); /* never blocks, we were on list */
spin_unlock_irqrestore(&hub_event_lock, flags);
if (hub->error) {
dev_dbg (&hub->intf->dev, "resetting for error %d\n",
hub->error);
if (hub_reset(hub)) {
dev_dbg (&hub->intf->dev,
"can't reset; disconnecting\n");
up(&hub->khubd_sem);
hub_start_disconnect(dev);
continue;
}
hub->nerrors = 0;
hub->error = 0;
}
for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
ret = hub_port_status(dev, i, &portstatus, &portchange);
if (ret < 0) {
continue;
}
if (portchange & USB_PORT_STAT_C_CONNECTION) {
hub_port_connect_change(hub, i, portstatus, portchange);
} else if (portchange & USB_PORT_STAT_C_ENABLE) {
dev_dbg (hubdev (dev),
"port %d enable change, status %x\n",
i + 1, portstatus);
clear_port_feature(dev,
i + 1, USB_PORT_FEAT_C_ENABLE);
/*
* EM interference sometimes causes badly
* shielded USB devices to be shutdown by
* the hub, this hack enables them again.
* Works at least with mouse driver.
*/
if (!(portstatus & USB_PORT_STAT_ENABLE)
&& (portstatus & USB_PORT_STAT_CONNECTION)
&& (dev->children[i])) {
dev_err (&hub->intf->dev,
"port %i "
"disabled by hub (EMI?), "
"re-enabling...",
i + 1);
hub_port_connect_change(hub,
i, portstatus, portchange);
}
}
if (portchange & USB_PORT_STAT_C_SUSPEND) {
dev_dbg (&hub->intf->dev,
"suspend change on port %d\n",
i + 1);
clear_port_feature(dev,
i + 1, USB_PORT_FEAT_C_SUSPEND);
}
if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
dev_err (&hub->intf->dev,
"over-current change on port %d\n",
i + 1);
clear_port_feature(dev,
i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
hub_power_on(hub);
}
if (portchange & USB_PORT_STAT_C_RESET) {
dev_dbg (&hub->intf->dev,
"reset change on port %d\n",
i + 1);
clear_port_feature(dev,
i + 1, USB_PORT_FEAT_C_RESET);
}
} /* end for i */
/* deal with hub status changes */
if (hub_hub_status(hub, &hubstatus, &hubchange) < 0) {
dev_err (&hub->intf->dev, "get_hub_status failed\n");
}
else {
if (hubchange & HUB_CHANGE_LOCAL_POWER) {
dev_dbg (&hub->intf->dev, "power change\n");
clear_hub_feature(dev, C_HUB_LOCAL_POWER);
}
if (hubchange & HUB_CHANGE_OVERCURRENT) {
dev_dbg (&hub->intf->dev, "overcurrent change\n");
wait_ms(500); /* Cool down */
clear_hub_feature(dev, C_HUB_OVER_CURRENT);
hub_power_on(hub);
}
}
up(&hub->khubd_sem);
} /* end while (1) */
spin_unlock_irqrestore(&hub_event_lock, flags);
}
// ReactOS: STDCALL is needed here
static int STDCALL hub_thread(void *__hub)
{
//LARGE_INTEGER delay;
/*
* This thread doesn't need any user-level access,
* so get rid of all our resources
*/
daemonize("khubd");
allow_signal(SIGKILL);
// Initialize khubd spinlock
KeInitializeSpinLock((PKSPIN_LOCK)&hub_event_lock);
//delay.QuadPart = -10000000*5; // wait 5 seconds before powering up
//KeDelayExecutionThread(KernelMode, FALSE, &delay); //wait_us(1);
printk("hub_thread starting");
/* Send me a signal to get me die (for debugging) */
do {
LARGE_INTEGER delay;
/* The following is just for debug */
inc_jiffies(1);
do_all_timers();
//handle_irqs(-1);
/* End of debug hack*/
hub_events();
/* The following is just for debug */
handle_irqs(-1);
/* End of debug hack*/
//FIXME: Correct this
//wait_event_interruptible(khubd_wait, !list_empty(&hub_event_list)); // interruptable_sleep_on analog - below
//while (!list_empty(&hub_event_list)) {
// interruptible_sleep_on(&khubd_wait);
//}
delay.QuadPart = -10000*100; // convert to 100ns units
KeDelayExecutionThread(KernelMode, FALSE, &delay); //wait_us(1);
if (current->flags & PF_FREEZE)
refrigerator(PF_IOTHREAD);
} while (!signal_pending(current));
dbg("hub_thread exiting");
complete_and_exit(&khubd_exited, 0);
}
static struct usb_device_id hub_id_table [] = {
{ .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
.bDeviceClass = USB_CLASS_HUB},
{ .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
.bInterfaceClass = USB_CLASS_HUB},
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, hub_id_table);
static struct usb_driver hub_driver = {
.owner = THIS_MODULE,
.name = "hub",
.probe = hub_probe,
.disconnect = hub_disconnect,
.ioctl = hub_ioctl,
.id_table = hub_id_table,
};
/*
* This should be a separate module.
*/
int usb_hub_init(void)
{
pid_t pid;
// ReactOS-specific
// Create Event object, initialize other sync events
KeInitializeEvent(&khubd_wait, NotificationEvent, TRUE); // signalled state
if (usb_register(&hub_driver) < 0) {
err("Unable to register USB hub driver");
return -1;
}
pid = kernel_thread((void*)hub_thread, NULL,
(CLONE_FS | CLONE_FILES | CLONE_SIGHAND));
if (pid >= 0) {
khubd_pid = pid;
return 0;
}
/* Fall through if kernel_thread failed */
usb_deregister(&hub_driver);
err("failed to start hub_thread");
return -1;
}
void usb_hub_cleanup(void)
{
int ret;
/* Kill the thread */
ret = kill_proc(khubd_pid, SIGKILL, 1);
wait_for_completion(&khubd_exited);
/*
* Hub resources are freed for us by usb_deregister. It calls
* usb_driver_purge on every device which in turn calls that
* devices disconnect function if it is using this driver.
* The hub_disconnect function takes care of releasing the
* individual hub resources. -greg
*/
usb_deregister(&hub_driver);
} /* usb_hub_cleanup() */
/*
* WARNING - If a driver calls usb_reset_device, you should simulate a
* disconnect() and probe() for other interfaces you doesn't claim. This
* is left up to the driver writer right now. This insures other drivers
* have a chance to re-setup their interface.
*
* Take a look at proc_resetdevice in devio.c for some sample code to
* do this.
* Use this only from within your probe function, otherwise use
* usb_reset_device() below, which ensure proper locking
*/
int usb_physical_reset_device(struct usb_device *dev)
{
struct usb_device *parent = dev->parent;
struct usb_device_descriptor *descriptor;
int i, ret, port = -1;
if (!parent) {
err("attempting to reset root hub!");
return -EINVAL;
}
for (i = 0; i < parent->maxchild; i++)
if (parent->children[i] == dev) {
port = i;
break;
}
if (port < 0)
return -ENOENT;
descriptor = kmalloc(sizeof *descriptor, GFP_NOIO);
if (!descriptor) {
return -ENOMEM;
}
down(&usb_address0_sem);
/* Send a reset to the device */
if (hub_port_reset(parent, port, dev, HUB_SHORT_RESET_TIME)) {
hub_port_disable(parent, port);
up(&usb_address0_sem);
kfree(descriptor);
return(-ENODEV);
}
/* Reprogram the Address */
ret = usb_set_address(dev);
if (ret < 0) {
err("USB device not accepting new address (error=%d)", ret);
hub_port_disable(parent, port);
up(&usb_address0_sem);
kfree(descriptor);
return ret;
}
/* Let the SET_ADDRESS settle */
wait_ms(10);
up(&usb_address0_sem);
/*
* Now we fetch the configuration descriptors for the device and
* see if anything has changed. If it has, we dump the current
* parsed descriptors and reparse from scratch. Then we leave
* the device alone for the caller to finish setting up.
*
* If nothing changed, we reprogram the configuration and then
* the alternate settings.
*/
ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, descriptor,
sizeof(*descriptor));
if (ret < 0) {
kfree(descriptor);
return ret;
}
le16_to_cpus(&descriptor->bcdUSB);
le16_to_cpus(&descriptor->idVendor);
le16_to_cpus(&descriptor->idProduct);
le16_to_cpus(&descriptor->bcdDevice);
if (RtlCompareMemory(&dev->descriptor, descriptor, sizeof(*descriptor)) != sizeof(*descriptor)) {
kfree(descriptor);
usb_destroy_configuration(dev);
ret = usb_get_device_descriptor(dev);
if (ret < sizeof(dev->descriptor)) {
if (ret < 0) {
err("unable to get device %s descriptor "
"(error=%d)", dev->devpath, ret);
}
else {
err("USB device %s descriptor short read "
"(expected %Zi, got %i)",
dev->devpath,
sizeof(dev->descriptor), ret);
}
clear_bit(dev->devnum, dev->bus->devmap.devicemap);
dev->devnum = -1;
return -EIO;
}
ret = usb_get_configuration(dev);
if (ret < 0) {
err("unable to get configuration (error=%d)", ret);
usb_destroy_configuration(dev);
clear_bit(dev->devnum, dev->bus->devmap.devicemap);
dev->devnum = -1;
return 1;
}
usb_set_configuration(dev, dev->config[0].desc.bConfigurationValue);
return 1;
}
kfree(descriptor);
ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_SET_CONFIGURATION, 0,
dev->actconfig->desc.bConfigurationValue, 0,
NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
if (ret < 0) {
err("failed to set dev %s active configuration (error=%d)",
dev->devpath, ret);
return ret;
}
dev->state = USB_STATE_CONFIGURED;
for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
struct usb_interface *intf = &dev->actconfig->interface[i];
struct usb_interface_descriptor *as;
as = &intf->altsetting[intf->act_altsetting].desc;
ret = usb_set_interface(dev, as->bInterfaceNumber,
as->bAlternateSetting);
if (ret < 0) {
err("failed to set active alternate setting "
"for dev %s interface %d (error=%d)",
dev->devpath, i, ret);
return ret;
}
}
return 0;
}
int usb_reset_device(struct usb_device *udev)
{
//struct device *gdev = &udev->dev;
int r;
down_read(&gdev->bus->subsys.rwsem);
r = usb_physical_reset_device(udev);
up_read(&gdev->bus->subsys.rwsem);
return r;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -