usb_device.c (188985) | usb_device.c (188986) |
---|---|
1/* $FreeBSD: head/sys/dev/usb/usb_device.c 188985 2009-02-24 03:40:48Z thompsa $ */ | 1/* $FreeBSD: head/sys/dev/usb/usb_device.c 188986 2009-02-24 03:41:52Z thompsa $ */ |
2/*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. --- 536 unchanged lines hidden (view full) --- 546 usb2_free_iface_data(udev); 547 548 if (index == USB_UNCONFIG_INDEX) { 549 /* 550 * Leave unallocated when unconfiguring the 551 * device. "usb2_free_iface_data()" will also reset 552 * the current config number and index. 553 */ | 2/*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. --- 536 unchanged lines hidden (view full) --- 546 usb2_free_iface_data(udev); 547 548 if (index == USB_UNCONFIG_INDEX) { 549 /* 550 * Leave unallocated when unconfiguring the 551 * device. "usb2_free_iface_data()" will also reset 552 * the current config number and index. 553 */ |
554 err = usb2_req_set_config(udev, &Giant, USB_UNCONFIG_NO); | 554 err = usb2_req_set_config(udev, NULL, USB_UNCONFIG_NO); |
555 goto done; 556 } 557 /* get the full config descriptor */ 558 err = usb2_req_get_config_desc_full(udev, | 555 goto done; 556 } 557 /* get the full config descriptor */ 558 err = usb2_req_get_config_desc_full(udev, |
559 &Giant, &cdp, M_USB, index); | 559 NULL, &cdp, M_USB, index); |
560 if (err) { 561 goto done; 562 } 563 /* set the new config descriptor */ 564 565 udev->cdesc = cdp; 566 567 if (cdp->bNumInterface > USB_IFACE_MAX) { --- 10 unchanged lines hidden (view full) --- 578 /* Must ask device. */ 579 if (udev->flags.uq_power_claim) { 580 /* 581 * HUB claims to be self powered, but isn't. 582 * It seems that the power status can be 583 * determined by the HUB characteristics. 584 */ 585 err = usb2_req_get_hub_descriptor | 560 if (err) { 561 goto done; 562 } 563 /* set the new config descriptor */ 564 565 udev->cdesc = cdp; 566 567 if (cdp->bNumInterface > USB_IFACE_MAX) { --- 10 unchanged lines hidden (view full) --- 578 /* Must ask device. */ 579 if (udev->flags.uq_power_claim) { 580 /* 581 * HUB claims to be self powered, but isn't. 582 * It seems that the power status can be 583 * determined by the HUB characteristics. 584 */ 585 err = usb2_req_get_hub_descriptor |
586 (udev, &Giant, &hd, 1); | 586 (udev, NULL, &hd, 1); |
587 if (err) { 588 DPRINTFN(0, "could not read " 589 "HUB descriptor: %s\n", 590 usb2_errstr(err)); 591 592 } else if (UGETW(hd.wHubCharacteristics) & 593 UHD_PWR_INDIVIDUAL) { 594 selfpowered = 1; 595 } 596 DPRINTF("characteristics=0x%04x\n", 597 UGETW(hd.wHubCharacteristics)); 598 } else { 599 err = usb2_req_get_device_status | 587 if (err) { 588 DPRINTFN(0, "could not read " 589 "HUB descriptor: %s\n", 590 usb2_errstr(err)); 591 592 } else if (UGETW(hd.wHubCharacteristics) & 593 UHD_PWR_INDIVIDUAL) { 594 selfpowered = 1; 595 } 596 DPRINTF("characteristics=0x%04x\n", 597 UGETW(hd.wHubCharacteristics)); 598 } else { 599 err = usb2_req_get_device_status |
600 (udev, &Giant, &ds); | 600 (udev, NULL, &ds); |
601 if (err) { 602 DPRINTFN(0, "could not read " 603 "device status: %s\n", 604 usb2_errstr(err)); 605 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 606 selfpowered = 1; 607 } 608 DPRINTF("status=0x%04x \n", --- 26 unchanged lines hidden (view full) --- 635 if (udev->flags.usb2_mode == USB_MODE_HOST) { 636 udev->flags.self_powered = selfpowered; 637 } 638 udev->power = power; 639 udev->curr_config_no = cdp->bConfigurationValue; 640 udev->curr_config_index = index; 641 642 /* Set the actual configuration value. */ | 601 if (err) { 602 DPRINTFN(0, "could not read " 603 "device status: %s\n", 604 usb2_errstr(err)); 605 } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 606 selfpowered = 1; 607 } 608 DPRINTF("status=0x%04x \n", --- 26 unchanged lines hidden (view full) --- 635 if (udev->flags.usb2_mode == USB_MODE_HOST) { 636 udev->flags.self_powered = selfpowered; 637 } 638 udev->power = power; 639 udev->curr_config_no = cdp->bConfigurationValue; 640 udev->curr_config_index = index; 641 642 /* Set the actual configuration value. */ |
643 err = usb2_req_set_config(udev, &Giant, cdp->bConfigurationValue); | 643 err = usb2_req_set_config(udev, NULL, cdp->bConfigurationValue); |
644 if (err) { 645 goto done; 646 } 647 /* Allocate and fill interface data. */ 648 nifc = cdp->bNumInterface; 649 while (nifc--) { 650 err = usb2_fill_iface_data(udev, nifc, 0); 651 if (err) { --- 12 unchanged lines hidden (view full) --- 664 return (err); 665} 666 667/*------------------------------------------------------------------------* 668 * usb2_set_alt_interface_index 669 * 670 * This function will select an alternate interface index for the 671 * given interface index. The interface should not be in use when this | 644 if (err) { 645 goto done; 646 } 647 /* Allocate and fill interface data. */ 648 nifc = cdp->bNumInterface; 649 while (nifc--) { 650 err = usb2_fill_iface_data(udev, nifc, 0); 651 if (err) { --- 12 unchanged lines hidden (view full) --- 664 return (err); 665} 666 667/*------------------------------------------------------------------------* 668 * usb2_set_alt_interface_index 669 * 670 * This function will select an alternate interface index for the 671 * given interface index. The interface should not be in use when this |
672 * function is called. That means there should be no open USB 673 * transfers. Else an error is returned. | 672 * function is called. That means there should not be any open USB 673 * transfers. Else an error is returned. If the alternate setting is 674 * already set this function will simply return success. This function 675 * is called in Host mode and Device mode! |
674 * 675 * Returns: 676 * 0: Success 677 * Else: Failure 678 *------------------------------------------------------------------------*/ 679usb2_error_t 680usb2_set_alt_interface_index(struct usb2_device *udev, 681 uint8_t iface_index, uint8_t alt_index) --- 10 unchanged lines hidden (view full) --- 692 sx_xlock(udev->default_sx + 1); 693 } 694 if (iface == NULL) { 695 err = USB_ERR_INVAL; 696 goto done; 697 } 698 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 699 usb2_detach_device(udev, iface_index, 1); | 676 * 677 * Returns: 678 * 0: Success 679 * Else: Failure 680 *------------------------------------------------------------------------*/ 681usb2_error_t 682usb2_set_alt_interface_index(struct usb2_device *udev, 683 uint8_t iface_index, uint8_t alt_index) --- 10 unchanged lines hidden (view full) --- 694 sx_xlock(udev->default_sx + 1); 695 } 696 if (iface == NULL) { 697 err = USB_ERR_INVAL; 698 goto done; 699 } 700 if (udev->flags.usb2_mode == USB_MODE_DEVICE) { 701 usb2_detach_device(udev, iface_index, 1); |
702 } else { 703 if (iface->alt_index == alt_index) { 704 /* 705 * Optimise away duplicate setting of 706 * alternate setting in USB Host Mode! 707 */ 708 err = 0; 709 goto done; 710 } |
|
700 } 701 /* 702 * Free all generic FIFOs for this interface, except control 703 * endpoint FIFOs: 704 */ 705 usb2_fifo_free_wrap(udev, iface_index, 0); 706 707 err = usb2_fill_iface_data(udev, iface_index, alt_index); 708 if (err) { 709 goto done; 710 } | 711 } 712 /* 713 * Free all generic FIFOs for this interface, except control 714 * endpoint FIFOs: 715 */ 716 usb2_fifo_free_wrap(udev, iface_index, 0); 717 718 err = usb2_fill_iface_data(udev, iface_index, alt_index); 719 if (err) { 720 goto done; 721 } |
711 err = usb2_req_set_alt_interface_no 712 (udev, &Giant, iface_index, | 722 err = usb2_req_set_alt_interface_no(udev, NULL, iface_index, |
713 iface->idesc->bAlternateSetting); 714 715done: 716 if (do_unlock) { 717 sx_unlock(udev->default_sx + 1); 718 } 719 return (err); 720} --- 689 unchanged lines hidden (view full) --- 1410 &udev->default_ep_desc, 1411 &udev->default_pipe); 1412 1413 /* set device index */ 1414 udev->device_index = device_index; 1415 1416 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1417 | 723 iface->idesc->bAlternateSetting); 724 725done: 726 if (do_unlock) { 727 sx_unlock(udev->default_sx + 1); 728 } 729 return (err); 730} --- 689 unchanged lines hidden (view full) --- 1420 &udev->default_ep_desc, 1421 &udev->default_pipe); 1422 1423 /* set device index */ 1424 udev->device_index = device_index; 1425 1426 if (udev->flags.usb2_mode == USB_MODE_HOST) { 1427 |
1418 err = usb2_req_set_address(udev, &Giant, device_index); | 1428 err = usb2_req_set_address(udev, NULL, device_index); |
1419 1420 /* This is the new USB device address from now on */ 1421 1422 udev->address = device_index; 1423 1424 /* 1425 * We ignore any set-address errors, hence there are 1426 * buggy USB devices out there that actually receive 1427 * the SETUP PID, but manage to set the address before 1428 * the STATUS stage is ACK'ed. If the device responds 1429 * to the subsequent get-descriptor at the new 1430 * address, then we know that the set-address command 1431 * was successful. 1432 */ 1433 if (err) { 1434 DPRINTFN(0, "set address %d failed " 1435 "(ignored)\n", udev->address); 1436 } 1437 /* allow device time to set new address */ | 1429 1430 /* This is the new USB device address from now on */ 1431 1432 udev->address = device_index; 1433 1434 /* 1435 * We ignore any set-address errors, hence there are 1436 * buggy USB devices out there that actually receive 1437 * the SETUP PID, but manage to set the address before 1438 * the STATUS stage is ACK'ed. If the device responds 1439 * to the subsequent get-descriptor at the new 1440 * address, then we know that the set-address command 1441 * was successful. 1442 */ 1443 if (err) { 1444 DPRINTFN(0, "set address %d failed " 1445 "(ignored)\n", udev->address); 1446 } 1447 /* allow device time to set new address */ |
1438 usb2_pause_mtx(&Giant, | 1448 usb2_pause_mtx(NULL, |
1439 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE)); 1440 } else { 1441 /* We are not self powered */ 1442 udev->flags.self_powered = 0; 1443 1444 /* Set unconfigured state */ 1445 udev->curr_config_no = USB_UNCONFIG_NO; 1446 udev->curr_config_index = USB_UNCONFIG_INDEX; --- 12 unchanged lines hidden (view full) --- 1459 * 1460 * NOTE: "usb2_do_request" will check the device descriptor 1461 * next time we do a request to see if the maximum packet size 1462 * changed! The 8 first bytes of the device descriptor 1463 * contains the maximum packet size to use on control endpoint 1464 * 0. If this value is different from "USB_MAX_IPACKET" a new 1465 * USB control request will be setup! 1466 */ | 1449 USB_MS_TO_TICKS(USB_SET_ADDRESS_SETTLE)); 1450 } else { 1451 /* We are not self powered */ 1452 udev->flags.self_powered = 0; 1453 1454 /* Set unconfigured state */ 1455 udev->curr_config_no = USB_UNCONFIG_NO; 1456 udev->curr_config_index = USB_UNCONFIG_INDEX; --- 12 unchanged lines hidden (view full) --- 1469 * 1470 * NOTE: "usb2_do_request" will check the device descriptor 1471 * next time we do a request to see if the maximum packet size 1472 * changed! The 8 first bytes of the device descriptor 1473 * contains the maximum packet size to use on control endpoint 1474 * 0. If this value is different from "USB_MAX_IPACKET" a new 1475 * USB control request will be setup! 1476 */ |
1467 err = usb2_req_get_desc(udev, &Giant, NULL, &udev->ddesc, | 1477 err = usb2_req_get_desc(udev, NULL, NULL, &udev->ddesc, |
1468 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1469 if (err) { 1470 DPRINTFN(0, "getting device descriptor " 1471 "at addr %d failed!\n", udev->address); 1472 /* XXX try to re-enumerate the device */ | 1478 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1479 if (err) { 1480 DPRINTFN(0, "getting device descriptor " 1481 "at addr %d failed!\n", udev->address); 1482 /* XXX try to re-enumerate the device */ |
1473 err = usb2_req_re_enumerate(udev, &Giant); | 1483 err = usb2_req_re_enumerate(udev, NULL); |
1474 if (err) { 1475 goto done; 1476 } 1477 } 1478 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, " 1479 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1480 udev->address, UGETW(udev->ddesc.bcdUSB), 1481 udev->ddesc.bDeviceClass, 1482 udev->ddesc.bDeviceSubClass, 1483 udev->ddesc.bDeviceProtocol, 1484 udev->ddesc.bMaxPacketSize, 1485 udev->ddesc.bLength, 1486 udev->speed); 1487 1488 /* get the full device descriptor */ | 1484 if (err) { 1485 goto done; 1486 } 1487 } 1488 DPRINTF("adding unit addr=%d, rev=%02x, class=%d, " 1489 "subclass=%d, protocol=%d, maxpacket=%d, len=%d, speed=%d\n", 1490 udev->address, UGETW(udev->ddesc.bcdUSB), 1491 udev->ddesc.bDeviceClass, 1492 udev->ddesc.bDeviceSubClass, 1493 udev->ddesc.bDeviceProtocol, 1494 udev->ddesc.bMaxPacketSize, 1495 udev->ddesc.bLength, 1496 udev->speed); 1497 1498 /* get the full device descriptor */ |
1489 err = usb2_req_get_device_desc(udev, &Giant, &udev->ddesc); | 1499 err = usb2_req_get_device_desc(udev, NULL, &udev->ddesc); |
1490 if (err) { 1491 DPRINTF("addr=%d, getting full desc failed\n", 1492 udev->address); 1493 goto done; 1494 } 1495 /* 1496 * Setup temporary USB attach args so that we can figure out some 1497 * basic quirks for this device. --- 22 unchanged lines hidden (view full) --- 1520 */ 1521 scratch_ptr = udev->bus->scratch[0].data; 1522 scratch_size = sizeof(udev->bus->scratch[0].data); 1523 1524 if (udev->ddesc.iManufacturer || 1525 udev->ddesc.iProduct || 1526 udev->ddesc.iSerialNumber) { 1527 /* read out the language ID string */ | 1500 if (err) { 1501 DPRINTF("addr=%d, getting full desc failed\n", 1502 udev->address); 1503 goto done; 1504 } 1505 /* 1506 * Setup temporary USB attach args so that we can figure out some 1507 * basic quirks for this device. --- 22 unchanged lines hidden (view full) --- 1530 */ 1531 scratch_ptr = udev->bus->scratch[0].data; 1532 scratch_size = sizeof(udev->bus->scratch[0].data); 1533 1534 if (udev->ddesc.iManufacturer || 1535 udev->ddesc.iProduct || 1536 udev->ddesc.iSerialNumber) { 1537 /* read out the language ID string */ |
1528 err = usb2_req_get_string_desc(udev, &Giant, | 1538 err = usb2_req_get_string_desc(udev, NULL, |
1529 (char *)scratch_ptr, 4, scratch_size, 1530 USB_LANGUAGE_TABLE); 1531 } else { 1532 err = USB_ERR_INVAL; 1533 } 1534 1535 if (err || (scratch_ptr[0] < 4)) { 1536 udev->flags.no_strings = 1; 1537 } else { 1538 /* pick the first language as the default */ 1539 udev->langid = UGETW(scratch_ptr + 2); 1540 } 1541 1542 /* assume 100mA bus powered for now. Changed when configured. */ 1543 udev->power = USB_MIN_POWER; 1544 1545 /* get serial number string */ 1546 err = usb2_req_get_string_any | 1539 (char *)scratch_ptr, 4, scratch_size, 1540 USB_LANGUAGE_TABLE); 1541 } else { 1542 err = USB_ERR_INVAL; 1543 } 1544 1545 if (err || (scratch_ptr[0] < 4)) { 1546 udev->flags.no_strings = 1; 1547 } else { 1548 /* pick the first language as the default */ 1549 udev->langid = UGETW(scratch_ptr + 2); 1550 } 1551 1552 /* assume 100mA bus powered for now. Changed when configured. */ 1553 udev->power = USB_MIN_POWER; 1554 1555 /* get serial number string */ 1556 err = usb2_req_get_string_any |
1547 (udev, &Giant, (char *)scratch_ptr, | 1557 (udev, NULL, (char *)scratch_ptr, |
1548 scratch_size, udev->ddesc.iSerialNumber); 1549 1550 strlcpy(udev->serial, (char *)scratch_ptr, sizeof(udev->serial)); 1551 1552 /* get manufacturer string */ 1553 err = usb2_req_get_string_any | 1558 scratch_size, udev->ddesc.iSerialNumber); 1559 1560 strlcpy(udev->serial, (char *)scratch_ptr, sizeof(udev->serial)); 1561 1562 /* get manufacturer string */ 1563 err = usb2_req_get_string_any |
1554 (udev, &Giant, (char *)scratch_ptr, | 1564 (udev, NULL, (char *)scratch_ptr, |
1555 scratch_size, udev->ddesc.iManufacturer); 1556 1557 strlcpy(udev->manufacturer, (char *)scratch_ptr, sizeof(udev->manufacturer)); 1558 1559 /* get product string */ 1560 err = usb2_req_get_string_any | 1565 scratch_size, udev->ddesc.iManufacturer); 1566 1567 strlcpy(udev->manufacturer, (char *)scratch_ptr, sizeof(udev->manufacturer)); 1568 1569 /* get product string */ 1570 err = usb2_req_get_string_any |
1561 (udev, &Giant, (char *)scratch_ptr, | 1571 (udev, NULL, (char *)scratch_ptr, |
1562 scratch_size, udev->ddesc.iProduct); 1563 1564 strlcpy(udev->product, (char *)scratch_ptr, sizeof(udev->product)); 1565 1566 /* finish up all the strings */ 1567 usb2_check_strings(udev); 1568 1569 if (udev->flags.usb2_mode == USB_MODE_HOST) { --- 34 unchanged lines hidden (view full) --- 1604 err = usb2_set_config_index(udev, config_index); 1605 sx_unlock(udev->default_sx + 1); 1606 if (err) { 1607 if (udev->ddesc.bNumConfigurations != 0) { 1608 if (!set_config_failed) { 1609 set_config_failed = 1; 1610 /* XXX try to re-enumerate the device */ 1611 err = usb2_req_re_enumerate( | 1572 scratch_size, udev->ddesc.iProduct); 1573 1574 strlcpy(udev->product, (char *)scratch_ptr, sizeof(udev->product)); 1575 1576 /* finish up all the strings */ 1577 usb2_check_strings(udev); 1578 1579 if (udev->flags.usb2_mode == USB_MODE_HOST) { --- 34 unchanged lines hidden (view full) --- 1614 err = usb2_set_config_index(udev, config_index); 1615 sx_unlock(udev->default_sx + 1); 1616 if (err) { 1617 if (udev->ddesc.bNumConfigurations != 0) { 1618 if (!set_config_failed) { 1619 set_config_failed = 1; 1620 /* XXX try to re-enumerate the device */ 1621 err = usb2_req_re_enumerate( |
1612 udev, &Giant); | 1622 udev, NULL); |
1613 if (err == 0) 1614 goto repeat_set_config; 1615 } 1616 DPRINTFN(0, "Failure selecting " 1617 "configuration index %u: %s, port %u, " 1618 "addr %u (ignored)\n", 1619 config_index, usb2_errstr(err), udev->port_no, 1620 udev->address); --- 606 unchanged lines hidden --- | 1623 if (err == 0) 1624 goto repeat_set_config; 1625 } 1626 DPRINTFN(0, "Failure selecting " 1627 "configuration index %u: %s, port %u, " 1628 "addr %u (ignored)\n", 1629 config_index, usb2_errstr(err), udev->port_no, 1630 udev->address); --- 606 unchanged lines hidden --- |