在这里,不要忘记了前面的操作,在调这个函数之前, usb_disable_endpoint()已经将这个endpoint禁用了,也就是说该endpoint不会产生新的urb.因为,在后一个循环中,只需要等待那些被unlink的urb调度完即可.在usb_kill_urb()中,会一直等待,直到这个urb被调度完成为止.
可能有人又会有这样的疑问:
Usb_kill_urb()中也有unlink urb的操作,为什么这里要分做两个循环呢?
另外的一个函数是usb_hcd_disable_endpoint().代码如下:
void usb_hcd_disable_endpoint(struct usb_device *udev,
struct usb_host_endpoint *ep)
{
struct usb_hcd *hcd;
might_sleep();
hcd = bus_to_hcd(udev->bus);
if (hcd->driver->endpoint_disable)
hcd->driver->endpoint_disable(hcd, ep);
}
从上面的代码可以看到,操作转向了hcd->driver的endpoint_disable()接口.
以UHCI为例.在UHCI中,对应的接口为:
static void uhci_hcd_endpoint_disable(struct usb_hcd *hcd,
struct usb_host_endpoint *hep)
{
struct uhci_hcd *uhci = hcd_to_uhci(hcd);
struct uhci_qh *qh;
spin_lock_irq(&uhci->lock);
qh = (struct uhci_qh *) hep->hcpriv;
if (qh == NULL)
goto done;
while (qh->state != QH_STATE_IDLE) {
++uhci->num_waiting;
spin_unlock_irq(&uhci->lock);
wait_event_interruptible(uhci->waitqh,
qh->state == QH_STATE_IDLE);
spin_lock_irq(&uhci->lock);
--uhci->num_waiting;
}
uhci_free_qh(uhci, qh);
done:
spin_unlock_irq(&uhci->lock);
}
这个函数没啥好说的,就是在uhci->waitqh上等待队列状态变为QH_STATE_IDLE.来回忆一下,qh在什么情况下才会变为QH_STATE_IDLE呢? 是在qh没有待传输的urb的时候.
然后,将qh释放.
现在我们来接着看usb_disable_device()的第二个部份.
第二部份主要是针对dev->actconfig进行的操作, dev->actconfig存放的是设备当前的配置,现在要将设备设回Address状态.就些东西当然是用不了上的了.释放dev->actconfig->interface[]中的各元素,注意不要将dev->actconfig->interface[]所指向的信息释放了,它都是指向dev->config[]-> intf_cache[]中的东西,这些东西一释放,usb device在Get_ Configure所获得的信息就会部丢失了.
就这样, usb_disable_device()函数也走到了尾声.
2: usb_cache_string()函数
这个函数我们在分析UHCI的时候已经接触过,但末做详细的分析.
首先了解一下这个函数的作用,有时候,为了形象的说明,会提供一个字符串形式的说明.例如,对于配置描述符来说,它的iConfiguration就表示一个字符串索引,然后用Get_String就可以取得这个索引所对应的字串了.不过,事情并不是这么简单.因为字符串对应不同的编码,所以这里还会对应有编码的处理.来看具体的代码:
char *usb_cache_string(struct usb_device *udev, int index)
{
char *buf;
char *smallbuf = NULL;
int len;
if (index <= 0)
return NULL;
//不知道字符到底有多长,就按最长256字节处理
buf = kmalloc(256, GFP_KERNEL);
if (buf) {
len = usb_string(udev, index, buf, 256);
//取到字符了,分配合适的长度
if (len > 0) {
smallbuf = kmalloc(++len, GFP_KERNEL);
if (!smallbuf)
return buf;
//将字符copy过去
memcpy(smallbuf, buf, len);
}
//释放旧空间
kfree(buf);
}
return smallbuf;
}
这个函数没啥好说的,流程转入到usb_string中.代码如下:
int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
{
unsigned char *tbuf;
int err;
unsigned int u, idx;
if (dev->state == USB_STATE_SUSPENDED)
return -EHOSTUNREACH;
if (size <= 0 || !buf || !index)
return -EINVAL;
buf[0] = 0;
tbuf = kmalloc(256, GFP_KERNEL);
if (!tbuf)
return -ENOMEM;
/* get langid for strings if it's not yet known */
//先取得设备支持的编码ID
if (!dev->have_langid) {
//以0号序号和编码0,Get_String就可得到设备所支持的编码列表
err = usb_string_sub(dev, 0, 0, tbuf);
//如果发生了错误,或者是取得的数据超短(最短为4字节)
if (err < 0) {
dev_err(&dev->dev,
"string descriptor 0 read error: %d\n",
err);
goto errout;
} else if (err < 4) {
dev_err(&dev->dev, "string descriptor 0 too short\n");
err = -EINVAL;
goto errout;
}
//取设备支持的第一个编码
else {
dev->have_langid = 1;
dev->string_langid = tbuf[2] | (tbuf[3] << 8);
/* always use the first langid listed */
dev_dbg(&dev->dev, "default language 0x%04x\n",
dev->string_langid);
}
}
//以编码ID和序号Index作为参数Get_String取得序号对应的字串
err = usb_string_sub(dev, dev->string_langid, index, tbuf);
if (err < 0)
goto errout;
//空一个字符来用来存放结束符
size--; /* leave room for trailing NULL char in output buffer */
//两字节一组,(Unicode编码的)
for (idx = 0, u = 2; u < err; u += 2) {
if (idx >= size)
break;
//如果高字节有值,说明它不是ISO-8859-1编码的,将它置为?
//否则,就将低位的值存放到buf中
if (tbuf[u+1]) /* high byte */
buf[idx++] = '?'; /* non ISO-8859-1 character */
else
buf[idx++] = tbuf[u];
}
//在最后一位赋0,字串结尾
buf[idx] = 0;
//返回字串的长度,(算上了最后的结尾字符)
err = idx;
//如果该描述符不是STRING描述符,打印出错误提示
if (tbuf[1] != USB_DT_STRING)
dev_dbg(&dev->dev,
"wrong descriptor type %02x for string %d (\"%s\")\n",
tbuf[1], index, buf);
errout:
//释放空间,返回长度
kfree(tbuf);
return err;
}
结合代码中的注释,就很容易理解这一函数了,在此不对这一函数做详细分析.
跟踪进usb_string_sub().代码如下:
static int usb_string_sub(struct usb_device *dev, unsigned int langid,
unsigned int index, unsigned char *buf)
{
int rc;
/* Try to read the string descriptor by asking for the maximum
* possible number of bytes */
//如果设备不需要Fixup 就发出Get_String
if (dev->quirks & USB_QUIRK_STRING_FETCH_255)
rc = -EIO;
else
rc = usb_get_string(dev, langid, index, buf, 255);
/* If that failed try to read the descriptor length, then
* ask for just that many bytes */
//如果Get_String失败或者取得长度有问题.就先取字符描述符的头部
//再以实际的长度和参数,再次Get_String
if (rc < 2) {
rc = usb_get_string(dev, langid, index, buf, 2);
if (rc == 2)
rc = usb_get_string(dev, langid, index, buf, buf[0]);
}
//如果成功
if (rc >= 2) {
//如果前两个字节为空.则需要找到数据的有效起始位置
if (!buf[0] && !buf[1])
usb_try_string_workarounds(buf, &rc);
/* There might be extra junk at the end of the descriptor */
//整调一下描述符的长度
if (buf[0] < rc)
rc = buf[0];
//将rc置为了一个偶数.
rc = rc - (rc & 1); /* force a multiple of two */
}
//长度最终小于2.返回错误值
if (rc < 2)
rc = (rc < 0 ? rc : -EINVAL);
return rc;
}
在这个地方,有个错误处理,可能有的设备你一次用255的长度去取它对字符串会返回一个错误,所以,在用255长度返回错误的时候,先以2为长度取它的描述符头度,再以描述符的实际长度去取字符串描述符串.
另外,在描述符的前两个字节都为空的情况下,就需要计算它的有效长度.在代码中,这一工作是由usb_try_string_workarounds()完成的.
static void usb_try_string_workarounds(unsigned char *buf, int *length)
{
int newlength, oldlength = *length;
//前两个字节是描述符头部,所以从2开始循环.
//Unicode编码用两个字节来表示一个字符. 所以每次循环完了之后要+2
for (newlength = 2; newlength + 1 < oldlength; newlength += 2)
//低字节是不可打印字符,或者高字节不为空(不是ISO-8859-1), 就退出循环.
if (!isprint(buf[newlength]) || buf[newlength + 1])
break;
//修正字符串描述符的实际长度.
//如果newlength 等于2.说明字符中描述符没有带字串
if (newlength > 2) {
buf[0] = newlength;
*length = newlength;
}
}
这个函数涉及到编码方面的东东,建议参阅fudan_abc的<< Linux那些事儿之我是USB core >>,上面的较详细的描述.
至此, usb_cache_string()完分析完了.
到这里,usb device driver的probe过程也就完成了.
