📄 otg_hub.c
字号:
ret = usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_ENABLE);
if (ret)
err("cannot disable port %d of hub %d (err = %d)",
port + 1, hub->devnum, ret);
}
static void usb_hub_port_connect_change(struct usb_device *hub, int port,
struct usb_port_status *portsts)
{
struct usb_device *dev;
unsigned short portstatus, portchange;
unsigned int delay = HUB_SHORT_RESET_TIME;
int i;
char *portstr, *tempstr;
struct usb_otg_bus *hub_bus = NULL;
portstatus = le16_to_cpu(portsts->wPortStatus);
portchange = le16_to_cpu(portsts->wPortChange);
dbg("port %d, portstatus %x, change %x, %s",
port + 1, portstatus, portchange, portspeed (portstatus));
/* Clear the connection change status */
usb_clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_CONNECTION);
/* Disconnect any existing devices under this port */
if (hub->children[port])
usb_disconnect(&hub->children[port]);
/* Return now if nothing is connected */
if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
if (portstatus & USB_PORT_STAT_ENABLE)
usb_hub_port_disable(hub, port);
return;
}
/* Some low speed devices have problems with the quick delay, so */
/* be a bit pessimistic with those devices. RHbug #23670 */
if (portstatus & USB_PORT_STAT_LOW_SPEED) {
wait_ms(400);
delay = HUB_LONG_RESET_TIME;
}
down(&usb_address0_sem);
tempstr = kmalloc(1024, GFP_KERNEL);
portstr = kmalloc(1024, GFP_KERNEL);
for (i = 0; i < HUB_PROBE_TRIES; i++) {
struct usb_device *pdev, *cdev;
/* Allocate a new device struct */
dev = usb_alloc_dev(hub, hub->bus);
if (!dev) {
err("couldn't allocate usb_device");
break;
}
hub->children[port] = dev;
/* Reset the device */
if (usb_hub_port_reset(hub, port, dev, delay)) {
usb_free_dev(dev);
break;
}
/* Find a new device ID for it */
usb_connect(dev);
/* Create a readable topology string */
cdev = dev;
pdev = dev->parent;
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,20)
if (portstr && tempstr) {
portstr[0] = 0;
while (pdev) {
int port;
for (port = 0; port < pdev->maxchild; port++)
if (pdev->children[port] == cdev)
break;
strcpy(tempstr, portstr);
if (!strlen(tempstr))
sprintf(portstr, "%d", port + 1);
else
sprintf(portstr, "%d/%s", port + 1, tempstr);
cdev = pdev;
pdev = pdev->parent;
}
info("USB new device connect on bus%d/%s, assigned device number %d",
dev->bus->busnum, portstr, dev->devnum);
} else
info("USB new device connect on bus%d, assigned device number %d",
dev->bus->busnum, dev->devnum);
#else
{
int len = 0;
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)
warn ("devpath size! usb/%03d/%03d path %s",
dev->bus->busnum, dev->devnum, dev->devpath);
info("new USB device %s-%s, assigned address %d",
dev->bus->bus_name, dev->devpath, dev->devnum);
}
#endif
hub_bus = otg_hub_get_otg_bus(dev->bus);
if(hub_bus) {
int err = otg_hub_new_device(dev,port);
if(hub_bus->otg_enum_result) hub_bus->otg_enum_result(hub_bus->otg_priv, &err);
if(err == 0) goto done;
i = HUB_PROBE_TRIES;
} else {
/* Run it through the hoops (find a driver, etc) */
if (!usb_new_device(dev))
goto done;
}
/* Free the configuration if there was an error */
usb_free_dev(dev);
/* Switch to a long reset time */
delay = HUB_LONG_RESET_TIME;
}
hub->children[port] = NULL;
usb_hub_port_disable(hub, port);
done:
up(&usb_address0_sem);
if (portstr)
kfree(portstr);
if (tempstr)
kfree(tempstr);
}
static void usb_hub_events(void)
{
unsigned long flags;
struct list_head *tmp;
struct usb_device *dev;
struct usb_hub *hub;
struct usb_hub_status hubsts;
unsigned short hubstatus, hubchange;
int i, ret;
/*
* We restart the list everytime 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.
*/
while (1) {
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 = hub->dev;
list_del(tmp);
INIT_LIST_HEAD(tmp);
down(&hub->khubd_sem); /* never blocks, we were on list */
spin_unlock_irqrestore(&hub_event_lock, flags);
if (hub->error) {
dbg("resetting hub %d for error %d", dev->devnum, hub->error);
if (usb_hub_reset(hub)) {
err("error resetting hub %d - disconnecting", dev->devnum);
up(&hub->khubd_sem);
usb_hub_disconnect(dev);
continue;
}
hub->nerrors = 0;
hub->error = 0;
}
for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
struct usb_port_status portsts;
unsigned short portstatus, portchange;
ret = usb_get_port_status(dev, i + 1, &portsts);
if (ret < 0) {
err("get_port_status failed (err = %d)", ret);
continue;
}
portstatus = le16_to_cpu(portsts.wPortStatus);
portchange = le16_to_cpu(portsts.wPortChange);
if (portchange & USB_PORT_STAT_C_CONNECTION) {
dbg("port %d connection change", i + 1);
usb_hub_port_connect_change(dev, i, &portsts);
} else if (portchange & USB_PORT_STAT_C_ENABLE) {
dbg("port %d enable change, status %x", i + 1, portstatus);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_ENABLE);
/*
* EM interference sometimes causes bad 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])) {
err("already running port %i disabled by hub (EMI?), re-enabling...",
i + 1);
usb_hub_port_connect_change(dev, i, &portsts);
}
}
if (portchange & USB_PORT_STAT_C_SUSPEND) {
dbg("port %d suspend change", i + 1);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_SUSPEND);
}
if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
err("port %d over-current change", i + 1);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
usb_hub_power_on(hub);
}
if (portchange & USB_PORT_STAT_C_RESET) {
dbg("port %d reset change", i + 1);
usb_clear_port_feature(dev, i + 1, USB_PORT_FEAT_C_RESET);
}
} /* end for i */
/* deal with hub status changes */
if (usb_get_hub_status(dev, &hubsts) < 0)
err("get_hub_status failed");
else {
hubstatus = le16_to_cpup(&hubsts.wHubStatus);
hubchange = le16_to_cpup(&hubsts.wHubChange);
if (hubchange & HUB_CHANGE_LOCAL_POWER) {
dbg("hub power change");
usb_clear_hub_feature(dev, C_HUB_LOCAL_POWER);
}
if (hubchange & HUB_CHANGE_OVERCURRENT) {
dbg("hub overcurrent change");
wait_ms(500); /* Cool down */
usb_clear_hub_feature(dev, C_HUB_OVER_CURRENT);
usb_hub_power_on(hub);
}
}
up(&hub->khubd_sem);
} /* end while (1) */
spin_unlock_irqrestore(&hub_event_lock, flags);
}
static int usb_hub_thread(void *__hub)
{
lock_kernel();
/*
* This thread doesn't need any user-level access,
* so get rid of all our resources
*/
daemonize();
/* Setup a nice name */
strcpy(current->comm, "kotghubd");
/* Send me a signal to get me die (for debugging) */
do {
usb_hub_events();
interruptible_sleep_on(&khubd_wait);
} while (!signal_pending(current));
dbg("usb_hub_thread exiting");
unlock_kernel();
complete_and_exit(&khubd_exited, 0);
}
static struct usb_device_id hub_id_table [] = {
{ 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 = {
name: "otg_hub",
probe: hub_probe,
ioctl: hub_ioctl,
disconnect: hub_disconnect,
id_table: hub_id_table,
};
/*
* This should be a separate module.
*/
int otg_hub_init(void)
{
int pid;
const struct usb_device_id *id;
struct list_head * tmp = NULL;
struct usb_driver *driver = NULL;
struct list_head * drv_list = usb_driver_get_list();
/* Find the old USB CORE Hub driver that was already registerd and
* replace it with the OTG-HUB */
for (tmp = drv_list->next; tmp != drv_list;) {
driver = list_entry(tmp, struct usb_driver, driver_list);
tmp = tmp->next;
id = driver->id_table;
if (id && (id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
(id->bInterfaceClass == USB_CLASS_HUB)) {
old_hub_driver = driver;
usb_deregister(old_hub_driver);
break;
}
}
if (usb_register(&hub_driver) < 0) {
err("Unable to register USB hub driver");
return -1;
}
pid = kernel_thread(usb_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 usb_hub_thread");
return -1;
}
void otg_hub_cleanup(void)
{
int ret;
/* Kill the thread */
ret = kill_proc(khubd_pid, SIGTERM, 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);
/* Register the USB CORE HUB Driver */
if(old_hub_driver) usb_register(old_hub_driver);
old_hub_driver = NULL;
} /* usb_hub_cleanup() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -