Takao Aratani / uvchost

Dependents:   WebCamera_SD

Fork of uvchost by Norimasa Okamoto

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UsbDevice2.cpp Source File

UsbDevice2.cpp

00001 #include "UsbDevice.h"
00002 //#define __DEBUG
00003 #include "mydbg.h"
00004 #include "Utils.h"
00005 
00006 #define PORT_RESET         4
00007 #define PORT_POWER         8
00008 #define C_PORT_CONNECTION 16
00009 #define C_PORT_RESET      20
00010 
00011 UsbErr UsbDevice::hub_init()
00012 {
00013     UsbErr rc;
00014     uint8_t buf[9];
00015     rc = controlReceive(
00016           USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_DEVICE, // 0xa0 
00017           GET_DESCRIPTOR, 
00018           (USB_DESCRIPTOR_TYPE_HUB << 8), 0, buf, sizeof(buf));
00019     DBG_ASSERT(rc == USBERR_OK);
00020     DBG_ASSERT(buf[0] == 9);
00021     DBG_ASSERT(buf[1] == 0x29);
00022     DBG_BYTES("HUB DESCRIPTOR", buf, sizeof(buf));
00023 
00024     m_hub_ports = buf[2];
00025     VERBOSE("NbrPorts: %d\n", m_hub_ports);
00026     int PwrOn2PwrGood = buf[5];
00027     VERBOSE("PwrOn2PwrGood: %d %d ms\n", PwrOn2PwrGood, PwrOn2PwrGood*2);
00028     VERBOSE("HubContrCurrent: %d\n", buf[6]);
00029 
00030     rc = setConfiguration(1);    
00031     DBG_ASSERT(rc == USBERR_OK);
00032     
00033     uint8_t status[4];
00034     rc = controlReceive(
00035           USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_DEVICE, // 0xa0 
00036           GET_STATUS, 
00037           0, 0, status, sizeof(status));
00038     DBG_ASSERT(rc == USBERR_OK);
00039     DBG_BYTES("HUB STATUS", status, sizeof(status));
00040 
00041     for(int i = 1; i <= m_hub_ports; i++) {
00042         rc = SetPortFeature(PORT_POWER, i);
00043         DBG("PORT_POWER port=%d rc=%d\n", i, rc);
00044         DBG_ASSERT(rc == USBERR_OK);
00045         if (rc != USBERR_OK) {
00046             return rc;
00047         }
00048     }
00049     wait_ms(PwrOn2PwrGood*2);
00050     
00051     m_enumerated = true;
00052     return USBERR_OK;
00053 }
00054 
00055 UsbErr UsbDevice::hub_poll()
00056 {
00057     DBG("%p m_hub=%d m_port=%d m_addr=%d\n", this, m_hub, m_port, m_addr);
00058     // check status
00059     for(int port = 1; port <= m_hub_ports; port++) {
00060         uint32_t status;
00061         UsbErr rc = GetPortStatus(port, &status);
00062         DBG_ASSERT(rc == USBERR_OK);
00063         DBG("port: %d status: %08X\n", port, status);
00064         if (status & 0x010000) { // Connect Status Change, has changed
00065             DBG_ASSERT(status & 0x000001);
00066             ClearPortFeature(C_PORT_CONNECTION, port);
00067             bool low_speed = false;
00068             if (status & 0x0200) {
00069                 low_speed = true;
00070             }
00071             DBG_ASSERT(m_pMgr);
00072             m_pMgr->onUsbDeviceConnected(m_addr, port, low_speed);
00073             return USBERR_PROCESSING;
00074         }
00075     }
00076     return USBERR_OK;
00077 }
00078 
00079 UsbErr UsbDevice::hub_PortReset(int port)
00080 {
00081     DBG("%p port=%d\n", this, port);
00082     DBG_ASSERT(port >= 1);
00083     SetPortReset(port);
00084     // wait reset
00085     for(int i = 0; i < 100; i++) {
00086         uint32_t status;    
00087         UsbErr rc = GetPortStatus(port, &status);
00088         DBG_ASSERT(rc == USBERR_OK);
00089         DBG("RESET port: %d status: %08X\n", port, status);
00090         if (status & 0x100000) { // Reset change , Reset complete
00091             return USBERR_OK;
00092         }
00093         wait_ms(5);
00094      }
00095      return USBERR_ERROR;
00096 }
00097 
00098 UsbErr UsbDevice::SetPortFeature(int feature, int index)
00099 {
00100     //DBG("feature=%d index=%d\n", feature, index);
00101     UsbErr rc;
00102     rc = controlSend(
00103           USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_OTHER, 
00104           SET_FEATURE, feature, index, 0, 0);
00105     return rc;
00106 }
00107 
00108 UsbErr UsbDevice::ClearPortFeature(int feature, int index)
00109 {
00110     UsbErr rc;
00111     rc = controlSend(
00112           USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_OTHER, 
00113           CLEAR_FEATURE, feature, index, 0, 0);
00114     return rc;
00115 }
00116 
00117 UsbErr UsbDevice::SetPortReset(int port)
00118 {
00119     //DBG("port=%d\n", port);
00120     UsbErr rc = SetPortFeature(PORT_RESET, port);
00121     DBG_ASSERT(rc == USBERR_OK);
00122     return rc;
00123 }
00124 
00125 UsbErr UsbDevice::GetPortStatus(int port, uint8_t* buf, int size)
00126 {
00127     DBG_ASSERT(size == 4);
00128     UsbErr rc;
00129     rc = controlReceive(
00130           USB_DEVICE_TO_HOST | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_OTHER, 
00131           GET_STATUS, 0, port, buf, sizeof(buf));
00132     return rc;
00133 }
00134 
00135 UsbErr UsbDevice::GetPortStatus(int port, uint32_t* status)
00136 {
00137     uint8_t buf[4];
00138     UsbErr rc = GetPortStatus(port, buf, sizeof(buf));
00139     *status = LE32(buf);
00140     return rc;
00141 }