关系是跑出来的,感情是喝出来的,朋友是处出来的,事业是干出来的,但设备是配置出来的,绝非吹出来的。core配置设备使用的是message.c里的usb_set_configuration函数
1388 /*
1389 * usb_set_configuration - Makes a particular device setting be current
1390 * @dev: the device whose configuration is being updated
1391 * @configuration: the configuration being chosen.
1392 * Context: !in_interrupt(), caller owns the device lock
1393 *
1394 * This is used to enable non-default device modes. Not all devices
1395 * use this kind of configurability; many devices only have one
1396 * configuration.
1397 *
1398 * @configuration is the value of the configuration to be installed.
1399 * According to the USB spec (e.g. section 9.1.1.5), configuration values
1400 * must be non-zero; a value of zero indicates that the device in
1401 * unconfigured. However some devices erroneously use 0 as one of their
1402 * configuration values. To help manage such devices, this routine will
1403 * accept @configuration = -1 as indicating the device should be put in
1404 * an unconfigured state.
1405 *
1406 * USB device configurations may affect Linux interoperability,
1407 * power consumption and the functionality available. For example,
1408 * the default configuration is limited to using 100mA of bus power,
1409 * so that when certain device functionality requires more power,
1410 * and the device is bus powered, that functionality should be in some
1411 * non-default device configuration. Other device modes may also be
1412 * reflected as configuration options, such as whether two ISDN
1413 * channels are available independently; and choosing between open
1414 * standard device protocols (like CDC) or proprietary ones.
1415 *
1416 * Note that USB has an additional level of device configurability,
1417 * associated with interfaces. That configurability is accessed using
1418 * usb_set_interface().
1419 *
1420 * This call is synchronous. The calling context must be able to sleep,
1421 * must own the device lock, and must not hold the driver model's USB
1422 * bus mutex; usb device driver probe() methods cannot use this routine.
1423
*
1424
* Returns zero on success, or else the status code returned by the
1425
* underlying call that failed. On successful completion, each interface
1426
* in the original device configuration has been destroyed, and each one
1427
* in the new configuration has been probed by all relevant usb device
1428
* drivers currently known to the kernel.
1429
*/
1430
int usb_set_configuration(struct usb_device *dev, int configuration)
1431
{
1432 int i, ret;
1433 struct usb_host_config *cp = NULL;
1434 struct usb_interface **new_interfaces = NULL;
1435 int n, nintf;
1436
1437 if (configuration == -1)
1438 configuration = 0;
1439 else {
1440 for (i = 0; i < dev->descriptor.bNumConfigurations; i++) {
1441 if (dev->config[i].desc.bConfigurationValue ==
1442 configuration) {
1443 cp = &dev->config[i];
1444 break;
1445 }
1446 }
1447 }
1448 if ((!cp && configuration != 0))
1449 return -EINVAL;
1450
1451 /* The USB spec says configuration 0 means unconfigured.
1452 * But if a device includes a configuration numbered 0,
1453 * we will accept it as a correctly configured state.
1454 * Use -1 if you really want to unconfigure the device.
1455
*/
1456
if (cp && configuration == 0)
1457
dev_warn(&dev->dev, "config 0 descriptor??/n");
1458
1459 /* Allocate memory for new interfaces before doing anything else,
1460 * so that if we run out then nothing will have changed. */
1461 n = nintf = 0;
1462 if (cp) {
1463 nintf = cp->desc.bNumInterfaces;
1464 new_interfaces = kmalloc(nintf * sizeof(*new_interfaces),
1465 GFP_KERNEL);
1466 if (!new_interfaces) {
1467 dev_err(&dev->dev, "Out of memory");
1468 return -ENOMEM;
1469 }
1470
1471 for (; n < nintf; ++n) {
1472 new_interfaces[n] = kzalloc(
1473 sizeof(struct usb_interface),
1474 GFP_KERNEL);
1475 if (!new_interfaces[n]) {
1476 dev_err(&dev->dev, "Out of memory");
1477
ret = -ENOMEM;
1478
free_interfaces:
1479
while (--n >= 0)
1480
kfree(new_interfaces[n]);
1481
kfree(new_interfaces);
1482
return ret;
1483
}
1484
}
1485
1486
i = dev->bus_mA - cp->desc.bMaxPower * 2;
1487
if (i < 0)
1488
dev_warn(&dev->dev, "new config #%d exceeds power "
1489
"limit by %dmA/n",
1490
configuration, -i);
1491
}
1492
1493
/* Wake up the device so we can send it the Set-Config request */
1494
ret = usb_autoresume_device(dev);
1495
if (ret)
1496
goto free_interfaces;
1497
1498
/* if it's already configured, clear out old state first.
1499
* getting rid of old interfaces means unbinding their drivers.
1500
*/
1501
if (dev->state != USB_STATE_ADDRESS)
1502
usb_disable_device (dev, 1); // Skip ep0
1503
1504
if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1505
USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1506
NULL, 0, USB_CTRL_SET_TIMEOUT)) < 0) {
1507
1508
/* All the old state is gone, so what else can we do?
1509
* The device is probably useless now anyway.
1510
*/
1511
cp = NULL;
1512
}
1513
1514
dev->actconfig = cp;
1515
if (!cp) {
1516
usb_set_device_state(dev, USB_STATE_ADDRESS);
1517
usb_autosuspend_device(dev);
1518
goto free_interfaces;
1519
}
1520
usb_set_device_state(dev, USB_STATE_CONFIGURED);
1521
1522
/* Initialize the new interface structures and the
1523 * hc/hcd/usbcore interface/endpoint state.
1524 */
1525 for (i = 0; i < nintf; ++i) {
1526 struct usb_interface_cache *intfc;
1527 struct usb_interface *intf;
1528 struct usb_host_interface *alt;
1529
1530 cp->interface[i] = intf = new_interfaces[i];
1531 intfc = cp->intf_cache[i];
1532 intf->altsetting = intfc->altsetting;
1533 intf->num_altsetting = intfc->num_altsetting;
1534 kref_get(&intfc->ref);
1535
1536 alt = usb_altnum_to_altsetting(intf, 0);
1537
1538 /* No altsetting 0? We'll assume the first altsetting.
1539 * We could use a GetInterface call, but if a device is
1540 * so non-compliant that it doesn't have altsetting 0
1541 * then I wouldn't trust its reply anyway.
1542 */
1543 if (!alt)
1544 alt = &intf->altsetting[0];
1545
1546 intf->cur_altsetting = alt;
1547
usb_enable_interface(dev, intf);
1548
intf->dev.parent = &dev->dev;
1549
intf->dev.driver = NULL;
1550
intf->dev.bus = &usb_bus_type;
1551
intf->dev.type = &usb_if_device_type;
1552
intf->dev.dma_mask = dev->dev.dma_mask;
1553
device_initialize (&intf->dev);
1554
mark_quiesced(intf);
1555
sprintf (&intf->dev.bus_id[0], "%d-%s:%d.%d",
1556
dev->bus->busnum, dev->devpath,
1557
configuration, alt->desc.bInterfaceNumber);
1558
}
1559
kfree(new_interfaces);
1560
1561
if (cp->string == NULL)
1562
cp->string = usb_cache_string(dev, cp->desc.iConfiguration);
1563
1564
/* Now that all the interfaces are set up, register them
1565
* to trigger binding of drivers to interfaces. probe()
1566
* routines may install different altsettings and may
1567
* claim() any interfaces not yet bound. Many class drivers
1568
* need that: CDC, audio, video, etc.
1569
*/
1570
for (i = 0; i < nintf; ++i) {
1571
struct usb_interface *intf = cp->interface[i];
1572
1573
dev_dbg (&dev->dev,
1574
"adding %s (config #%d, interface %d)/n",
1575
intf->dev.bus_id, configuration,
1576
intf->cur_altsetting->desc.bInterfaceNumber);
1577
ret = device_add (&intf->dev);
1578
if (ret != 0) {
1579
dev_err(&dev->dev, "device_add(%s) --> %d/n",
1580 intf->dev.bus_id, ret);
1581 continue;
1582 }
1583 usb_create_sysfs_intf_files (intf);
1584 }
1585
1586 usb_autosuspend_device(dev);
1587 return 0;
1588
}
这个函数很迷信,从1388行开始,到1588行结束,怎么着它都要发发,希望咱们看的时候也能沾上点财气运气。那就先对着它许个愿吧,并不是真的相信它,但是反正也是免费的,而且也没有证据证明它不灵。
说代码前咱们再聊点这个函数背后的人生哲学,你设备不是和usb_generic_driver这个大美女配对成功了么,可是这并不是就说你可以高枕无忧了,要想保持和她的这种亲密关系,你得想办法让她得到满足,你就得抱着一颗勇敢的心,准备着让她去配置,准备着她想让你什么样你就什么样。你要明白,吸引住男人的办法就是让他一直得不到,吸引住女人的办法正好相反,就是让她一直满足。从这个角度看,这个函数就可以泾渭分明的分成三个部分三个阶段,从1432到1491的这几十行是准备阶段,做做常规检查啊,申请申请内存啊,搞点前戏什么的。1498到1520这部分可是重头戏,就是在这里设备从Address发展到了Configured,可算是高潮阶段,别看它短,这是事物发展的规律,也是每个男人女人的规律。1522到1584这阶段也挺重要的,主要就是充实充实设备的每个接口并提交给设备模型,为它们寻找命中注定的接口驱动,最后温存温存,过了这个后戏阶段,usb_generic_driver也就彻底从你设备那儿得到满足了,generic_probe的历史使命也就完成了。事物的发展大体上就脱离不了这三个阶段,再比如股票,买、卖、回味。
先看第一阶段,要做一个好男人,按顺序一步一步来。1437行,configuration是前边儿choose_configuration()那里返回回来的,找到合意的配置的话,就返回那个配置的bConfigurationValue值,没有找到称心的配置的话,就返回-1,所以这里的configuration值就可能有两种情况,或者为-1,或者为配置的bConfigurationValue值。当configuration为-1时这里为啥又要把它改为0捏?要知道configuration这个值是要在后面的高潮阶段里发送SET_CONFIGURATION请求时用的,关于SET_CONFIGURATION请求,spec里说,这个值必须为0或者与配置描述符的bConfigurationValue一致,如果为0,则设备收到SET_CONFIGURATION请求后,仍然会待在Address状态。这里当configuration为-1也就是没有发现满意的配置时,设备不能进入Configured,所以要把configuration的值改为0,以便满足SET_CONFIGURATION请求的要求。
那接下来的问题就出来了,在没有找到合适配置的时候直接给configuration这个参数传个0,也就是让choose_configuration()返回个0不就得了,干吗还这么麻烦先返回个-1再把它改成0,不是脱裤子放屁多此一举么?你别愤怒,这归根结底还是那句话,林子大了什么鸟都有,有些设备就是有拿0当配置bConfigurationValue值的毛病,你又不能不让它用,不是有句俗话么:既然说人生是本书,那出现几个错别字就没什么大不了的。人生都能出现错字,那usb世界里出现几个有毛病的设备也没啥大不了的,这里妥协一下就是了,想让设备回到Address状态时,usb_set_configuration()就别传递0了,传递个-1,里边儿去处理一下。如果configuration值为0或大于0的值,就从设备struct usb_device结构体的config数组里将相应配置的描述信息,也就是struct usb_host_config结构体给取出来。
1448行,如果没有拿到配置的内容,configuration值就必须为0了,让设备待在Address那儿别动。这也很好理解,配置的内容都找不到了,还配置个什么劲儿。当然,如果拿到了配置的内容,但同时configuration为0,这就是对应了上面说的那种有毛病的设备的情况,就提出一下警告,告诉你不正常现象出现了。
1461行,过了这个if,第一阶段就告结束了。如果配置是实实在在存在的,就为它使用的那些接口都准备一个struct usb_interface结构体。new_interfaces是开头儿就定义好的一个struct usb_interface结构体指针数组,数组的每一项都指向了一个struct usb_interface结构体,所以这里申请内存也要分两步走,先申请指针数组的,再申请每一项的。