📄 hub.c
字号:
/*
* USB hub driver.
*
* (C) Copyright 1999 Linus Torvalds
* (C) Copyright 1999 Johannes Erdfelt
* (C) Copyright 1999 Gregory P. Smith
* (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
*
*/
#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/module.h>
#include <linux/completion.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/smp_lock.h>
#include <linux/ioctl.h>
#ifdef CONFIG_USB_DEBUG
#define DEBUG
#else
#undef DEBUG
#endif
#include <linux/usb.h>
#include <linux/usbdevice_fs.h>
#include <linux/suspend.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>
#include <asm/byteorder.h>
#include "hcd.h"
#include "hub.h"
/* Wakes up khubd */
static spinlock_t hub_event_lock = SPIN_LOCK_UNLOCKED;
static DECLARE_MUTEX(usb_address0_sem);
static LIST_HEAD(hub_event_list); /* List of hubs needing servicing */
static LIST_HEAD(hub_list); /* List of all hubs (for cleanup) */
static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
static int khubd_pid = 0; /* PID of khubd */
static DECLARE_COMPLETION(khubd_exited);
#ifdef DEBUG
static inline char *portspeed (int portstatus)
{
if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
return "480 Mb/s";
else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
return "1.5 Mb/s";
else
return "12 Mb/s";
}
#endif
/* USB 2.0 spec Section 11.24.4.5 */
static int usb_get_hub_descriptor(struct usb_device *dev, void *data, int size)
{
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
USB_DT_HUB << 8, 0, data, size, HZ);
}
/*
* USB 2.0 spec Section 11.24.2.1
*/
static int usb_clear_hub_feature(struct usb_device *dev, int feature)
{
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, HZ);
}
/*
* USB 2.0 spec Section 11.24.2.2
* BUG: doesn't handle port indicator selector in high byte of wIndex
*/
static int usb_clear_port_feature(struct usb_device *dev, int port, int feature)
{
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
}
/*
* USB 2.0 spec Section 11.24.2.13
* BUG: doesn't handle port indicator selector in high byte of wIndex
*/
static int usb_set_port_feature(struct usb_device *dev, int port, int feature)
{
return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
}
/*
* USB 2.0 spec Section 11.24.2.6
*/
static int usb_get_hub_status(struct usb_device *dev, void *data)
{
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
data, sizeof(struct usb_hub_status), HZ);
}
/*
* USB 2.0 spec Section 11.24.2.7
*/
static int usb_get_port_status(struct usb_device *dev, int port, void *data)
{
return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
data, sizeof(struct usb_hub_status), HZ);
}
/* completion function, fires on port status changes and various faults */
static void hub_irq(struct urb *urb)
{
struct usb_hub *hub = (struct usb_hub *)urb->context;
unsigned long flags;
switch (urb->status) {
case -ENOENT: /* synchronous unlink */
case -ECONNRESET: /* async unlink */
case -ESHUTDOWN: /* hardware going away */
return;
default: /* presumably an error */
/* Cause a hub reset after 10 consecutive errors */
dbg("hub '%s' status %d for interrupt transfer",
urb->dev->devpath, urb->status);
if ((++hub->nerrors < 10) || hub->error)
return;
hub->error = urb->status;
/* FALL THROUGH */
/* let khubd handle things */
case 0: /* we got data: port status changed */
break;
}
hub->nerrors = 0;
/* Something happened, let khubd figure it out */
spin_lock_irqsave(&hub_event_lock, flags);
if (list_empty(&hub->event_list)) {
list_add(&hub->event_list, &hub_event_list);
wake_up(&khubd_wait);
}
spin_unlock_irqrestore(&hub_event_lock, flags);
}
/* USB 2.0 spec Section 11.24.2.3 */
static inline int
hub_clear_tt_buffer (struct usb_device *hub, u16 devinfo, u16 tt)
{
return usb_control_msg (hub, usb_rcvctrlpipe (hub, 0),
HUB_CLEAR_TT_BUFFER, USB_DIR_IN | USB_RECIP_OTHER,
devinfo, tt, 0, 0, HZ);
}
/*
* enumeration blocks khubd for a long time. we use keventd instead, since
* long blocking there is the exception, not the rule. accordingly, HCDs
* talking to TTs must queue control transfers (not just bulk and iso), so
* both can talk to the same hub concurrently.
*/
static void hub_tt_kevent (void *arg)
{
struct usb_hub *hub = arg;
unsigned long flags;
spin_lock_irqsave (&hub->tt.lock, flags);
while (!list_empty (&hub->tt.clear_list)) {
struct list_head *temp;
struct usb_tt_clear *clear;
int status;
temp = hub->tt.clear_list.next;
clear = list_entry (temp, struct usb_tt_clear, clear_list);
list_del (&clear->clear_list);
/* drop lock so HCD can concurrently report other TT errors */
spin_unlock_irqrestore (&hub->tt.lock, flags);
status = hub_clear_tt_buffer (hub->dev,
clear->devinfo, clear->tt);
spin_lock_irqsave (&hub->tt.lock, flags);
if (status)
err ("usb-%s-%s clear tt %d (%04x) error %d",
hub->dev->bus->bus_name, hub->dev->devpath,
clear->tt, clear->devinfo, status);
kfree (clear);
}
spin_unlock_irqrestore (&hub->tt.lock, flags);
}
/**
* usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub
* @dev: the device whose split transaction failed
* @pipe: identifies the endpoint of the failed transaction
*
* High speed HCDs use this to tell the hub driver that some split control or
* bulk transaction failed in a way that requires clearing internal state of
* a transaction translator. This is normally detected (and reported) from
* interrupt context.
*
* It may not be possible for that hub to handle additional full (or low)
* speed transactions until that state is fully cleared out.
*/
void usb_hub_tt_clear_buffer (struct usb_device *dev, int pipe)
{
struct usb_tt *tt = dev->tt;
unsigned long flags;
struct usb_tt_clear *clear;
/* we've got to cope with an arbitrary number of pending TT clears,
* since each TT has "at least two" buffers that can need it (and
* there can be many TTs per hub). even if they're uncommon.
*/
if ((clear = kmalloc (sizeof *clear, SLAB_ATOMIC)) == 0) {
err ("can't save CLEAR_TT_BUFFER state for hub at usb-%s-%s",
dev->bus->bus_name, tt->hub->devpath);
/* FIXME recover somehow ... RESET_TT? */
return;
}
/* info that CLEAR_TT_BUFFER needs */
clear->tt = tt->multi ? dev->ttport : 1;
clear->devinfo = usb_pipeendpoint (pipe);
clear->devinfo |= dev->devnum << 4;
clear->devinfo |= usb_pipecontrol (pipe)
? (USB_ENDPOINT_XFER_CONTROL << 11)
: (USB_ENDPOINT_XFER_BULK << 11);
if (usb_pipein (pipe))
clear->devinfo |= 1 << 15;
/* tell keventd to clear state for this TT */
spin_lock_irqsave (&tt->lock, flags);
list_add_tail (&clear->clear_list, &tt->clear_list);
schedule_task (&tt->kevent);
spin_unlock_irqrestore (&tt->lock, flags);
}
static void usb_hub_power_on(struct usb_hub *hub)
{
int i;
/* Enable power to the ports */
dbg("enabling power on all ports");
for (i = 0; i < hub->descriptor->bNbrPorts; i++)
usb_set_port_feature(hub->dev, i + 1, USB_PORT_FEAT_POWER);
/* Wait for power to be enabled */
wait_ms(hub->descriptor->bPwrOn2PwrGood * 2);
}
static int usb_hub_configure(struct usb_hub *hub,
struct usb_endpoint_descriptor *endpoint)
{
struct usb_device *dev = hub->dev;
struct usb_hub_status hubstatus;
char portstr[USB_MAXCHILDREN + 1];
unsigned int pipe;
int i, maxp, ret;
hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
if (!hub->descriptor) {
err("Unable to kmalloc %Zd bytes for hub descriptor",
sizeof(*hub->descriptor));
return -1;
}
/* Request the entire hub descriptor.
* hub->descriptor can handle USB_MAXCHILDREN ports,
* but the hub can/will return fewer bytes here.
*/
ret = usb_get_hub_descriptor(dev, hub->descriptor,
sizeof(*hub->descriptor));
if (ret < 0) {
err("Unable to get hub descriptor (err = %d)", ret);
kfree(hub->descriptor);
return -1;
} else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
err("Hub is too big! %d children", hub->descriptor->bNbrPorts);
kfree(hub->descriptor);
return -1;
}
dev->maxchild = hub->descriptor->bNbrPorts;
info("%d port%s detected", dev->maxchild,
(dev->maxchild == 1) ? "" : "s");
le16_to_cpus(&hub->descriptor->wHubCharacteristics);
if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND)
dbg("part of a compound device");
else
dbg("standalone hub");
switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
case 0x00:
dbg("ganged power switching");
break;
case 0x01:
dbg("individual port power switching");
break;
case 0x02:
case 0x03:
dbg("unknown reserved power switching mode");
break;
}
switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
case 0x00:
dbg("global over-current protection");
break;
case 0x08:
dbg("individual port over-current protection");
break;
case 0x10:
case 0x18:
dbg("no over-current protection");
break;
}
spin_lock_init (&hub->tt.lock);
INIT_LIST_HEAD (&hub->tt.clear_list);
INIT_TQUEUE (&hub->tt.kevent, hub_tt_kevent, hub);
switch (dev->descriptor.bDeviceProtocol) {
case 0:
break;
case 1:
dbg("Single TT");
hub->tt.hub = dev;
break;
case 2:
dbg("TT per port");
hub->tt.hub = dev;
hub->tt.multi = 1;
break;
default:
dbg("Unrecognized hub protocol %d",
dev->descriptor.bDeviceProtocol);
break;
}
switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) {
case 0x00:
if (dev->descriptor.bDeviceProtocol != 0)
dbg("TT requires at most 8 FS bit times");
break;
case 0x20:
dbg("TT requires at most 16 FS bit times");
break;
case 0x40:
dbg("TT requires at most 24 FS bit times");
break;
case 0x60:
dbg("TT requires at most 32 FS bit times");
break;
}
dbg("Port indicators are %s supported",
(hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND)
? "" : "not");
dbg("power on to power good time: %dms",
hub->descriptor->bPwrOn2PwrGood * 2);
dbg("hub controller current requirement: %dmA",
hub->descriptor->bHubContrCurrent);
for (i = 0; i < dev->maxchild; i++)
portstr[i] = hub->descriptor->DeviceRemovable
[((i + 1) / 8)] & (1 << ((i + 1) % 8))
? 'F' : 'R';
portstr[dev->maxchild] = 0;
dbg("port removable status: %s", portstr);
ret = usb_get_hub_status(dev, &hubstatus);
if (ret < 0) {
err("Unable to get hub status (err = %d)", ret);
kfree(hub->descriptor);
return -1;
}
le16_to_cpus(&hubstatus.wHubStatus);
dbg("local power source is %s",
(hubstatus.wHubStatus & HUB_STATUS_LOCAL_POWER)
? "lost (inactive)" : "good");
dbg("%sover-current condition exists",
(hubstatus.wHubStatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
/* Start the interrupt endpoint */
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
if (maxp > sizeof(hub->buffer))
maxp = sizeof(hub->buffer);
hub->urb = usb_alloc_urb(0, GFP_KERNEL);
if (!hub->urb) {
err("couldn't allocate interrupt urb");
kfree(hub->descriptor);
return -1;
}
FILL_INT_URB(hub->urb, dev, pipe, hub->buffer, maxp, hub_irq,
hub, endpoint->bInterval);
ret = usb_submit_urb(hub->urb, GFP_KERNEL);
if (ret) {
err("usb_submit_urb failed (%d)", ret);
kfree(hub->descriptor);
return -1;
}
/* Wake up khubd */
wake_up(&khubd_wait);
usb_hub_power_on(hub);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -