17static const char *usbdVersionString =
"Version 1.6.0";
19void *doGetDeviceStaticDescriptor(
int devId,
void *data, u8 type)
24 dev = fetchDeviceById(devId);
31 for ( ; descBuf->bLength >= 2u && (u8 *)descBuf < (u8 *)dev->m_staticDeviceDescEndPtr;
34 if ( !type || descBuf->bDescriptorType == type )
47 deviceTreeRoot = getDeviceTreeRoot();
48 for (
count = 0;
count < 6 && dev != deviceTreeRoot;
count += 1, dev = dev->m_parent )
50 tmpPath[
count] = dev->m_attachedToPortNo;
52 if ( dev != deviceTreeRoot )
53 return USB_RC_BADHUBDEPTH;
55 return USB_RC_BADHUBDEPTH;
56 for ( cpCount = 0; cpCount < 7; cpCount += 1 )
58 path[cpCount] = (cpCount <
count) ? tmpPath[
count - cpCount - 1] : 0;
70 return dev->m_endpointListStart;
71 return openDeviceEndpoint(dev, endpDesc, alignFlag);
76 if ( ep->m_correspDevice->m_endpointListStart == ep )
78 return removeEndpointFromDevice(ep->m_correspDevice, ep);
83 if ( !ep->m_correspDevice )
87 if ( req->m_busyFlag )
92 req->m_correspEndpoint = ep;
93 req->m_destPtr = destdata;
94 req->m_length = length;
96 req->m_callbackProc = (InternCallback)callback;
97 req->m_prev = ep->m_ioReqListEnd;
98 if ( ep->m_ioReqListEnd )
99 ep->m_ioReqListEnd->m_next = req;
101 ep->m_ioReqListStart = req;
103 ep->m_ioReqListEnd = req;
108int doControlTransfer(
119 if ( req->m_busyFlag )
121 dbg_printf(
"ERROR: doControlTransfer: IoReq busy\n");
124 req->m_devReq.requesttype = requestType;
125 req->m_devReq.request =
request;
126 req->m_devReq.value = value;
127 req->m_devReq.index = index;
128 req->m_devReq.length = length;
129 return attachIoReqToEndpoint(ep, req, destdata, length, callback);
136 dbg_printf(
"searching driver for dev %d, FA %02X\n", dev->m_id, dev->m_functionAddress);
137 dev->m_deviceStatus = DEVICE_READY;
138 for ( drv = drvListStart; drv; drv = drv->next )
140 dev->m_privDataField = NULL;
141 if ( callUsbDriverFunc(drv->probe, dev->m_id, drv->gp) )
143 dev->m_devDriver = drv;
144 dbg_printf(
"Driver found (%s)\n", drv->
name);
145 callUsbDriverFunc(drv->connect, dev->m_id, drv->gp);
153 dev->m_privDataField = NULL;
154 if ( callUsbDriverFunc(drv->probe, dev->m_id, drv->gp) )
156 dev->m_devDriver = drv;
157 dbg_printf(
"(autoloader) Driver found (%s)\n", drv->
name);
158 callUsbDriverFunc(drv->connect, dev->m_id, drv->gp);
162 dbg_printf(
"no driver found\n");
167 req->m_correspEndpoint->m_correspDevice->m_reportDescriptorCurForFetch =
168 req->m_correspEndpoint->m_correspDevice->m_reportDescriptorCurForFetch->m_next;
169 if ( req->m_correspEndpoint->m_correspDevice->m_reportDescriptorCurForFetch )
170 fetchNextReportDescriptor(req);
172 connectNewDevice(req->m_correspEndpoint->m_correspDevice);
178 req->m_correspEndpoint,
179 &req->m_correspEndpoint->m_correspDevice->m_ioRequest,
180 USB_DIR_IN | USB_RECIP_INTERFACE,
183 req->m_correspEndpoint->m_correspDevice->m_reportDescriptorCurForFetch->m_ifNum,
184 req->m_correspEndpoint->m_correspDevice->m_reportDescriptorCurForFetch->m_length,
185 req->m_correspEndpoint->m_correspDevice->m_reportDescriptorCurForFetch->m_data,
186 fetchNextReportDescriptorCB);
191 removeEndpointFromDevice(dev, ep);
192 checkDelayedResets(dev);
200 int fetchDescriptorCounter;
207 dev = req->m_correspEndpoint->m_correspDevice;
209 if ( (
int)dev->m_fetchDescriptorCounter > 0 && req->m_resultCode !=
USB_RC_OK )
211 killDevice(req->m_correspEndpoint->m_correspDevice, req->m_correspEndpoint);
214 fetchDescriptorCounter = dev->m_fetchDescriptorCounter;
215 fetchDesc_1 = fetchDescriptorCounter + 1;
216 dev->m_fetchDescriptorCounter = fetchDesc_1;
217 fetchDesc_2 = fetchDescriptorCounter & 1;
218 curDescNum_1 = fetchDescriptorCounter >> 1;
220 if ( curDescNum_1 > 0 && !fetchDesc_2 )
221 dev->m_staticDeviceDescEndPtr = (u8 *)dev->m_staticDeviceDescEndPtr + READ_UINT16(&destdata->wTotalLength);
222 readLen = fetchDesc_2 ? READ_UINT16(&destdata->wTotalLength) : 4;
223 if ( (u8 *)dev->m_staticDeviceDescPtr + usbConfig.m_maxStaticDescSize < (u8 *)(&destdata->bLength + readLen) )
225 dbg_printf(
"USBD: UsbdDevice_t ignored, UsbdDevice_t descriptors too large\n");
228 if ( curDescNum_1 < staticDeviceDescPtr->bNumConfigurations )
231 req->m_correspEndpoint,
233 USB_DIR_IN | USB_RECIP_DEVICE,
234 USB_REQ_GET_DESCRIPTOR,
235 curDescNum_1 | (USB_DT_CONFIG << 8),
239 fetchConfigDescriptors);
242 dev->m_reportDescriptorStart = NULL;
243 if ( usbConfig.m_curDescNum )
245 handleStaticDeviceDescriptor(
247 if ( dev->m_reportDescriptorStart )
249 dev->m_reportDescriptorCurForFetch = dev->m_reportDescriptorStart;
250 fetchNextReportDescriptor(req);
254 connectNewDevice(dev);
262 dev = req->m_correspEndpoint->m_correspDevice;
265 dbg_printf(
"unable to read device descriptor, err %d\n", req->m_resultCode);
266 killDevice(req->m_correspEndpoint->m_correspDevice, req->m_correspEndpoint);
271 dev->m_fetchDescriptorCounter = 0;
272 dev->m_staticDeviceDescEndPtr = &((
UsbDeviceDescriptor *)dev->m_staticDeviceDescEndPtr)[1];
273 fetchConfigDescriptors(req);
276 desc = dev->m_staticDeviceDescPtr;
277 req->m_correspEndpoint->m_hcEd->m_hcArea.stru.m_maxPacketSize =
278 (req->m_correspEndpoint->m_hcEd->m_hcArea.stru.m_maxPacketSize & 0xF800) | desc->bMaxPacketSize0;
280 requestDeviceDescriptor(req);
287 dev = req->m_correspEndpoint->m_correspDevice;
288 dev->m_staticDeviceDescEndPtr = dev->m_staticDeviceDescPtr;
290 req->m_correspEndpoint,
292 USB_DIR_IN | USB_RECIP_DEVICE,
293 USB_REQ_GET_DESCRIPTOR,
297 dev->m_staticDeviceDescPtr,
298 requestDeviceDescrptorCB);
304 requestDeviceDescriptor(req);
307 checkDelayedResets(req->m_correspEndpoint->m_correspDevice);
314 void (*cb_func)(
void *);
317 dev = req->m_correspEndpoint->m_correspDevice;
320 dbg_printf(
"device not responding\n");
321 dev->m_functionDelay <<= 1;
322 if ( dev->m_functionDelay > 0x500 )
324 killDevice(dev, req->m_correspEndpoint);
327 cb_func = (void (*)(
void *))hubSetFuncAddress;
328 cb_arg = req->m_correspEndpoint;
329 cb_delay = dev->m_functionDelay | 1;
333 cb_func = (void (*)(
void *))hubPeekDeviceDescriptor;
336 req->m_correspEndpoint->m_hcEd->m_hcArea.stru.m_hcArea |= dev->m_functionAddress & 0x7F;
337 dev->m_deviceStatus = DEVICE_FETCHINGDESCRIPTOR;
339 addTimerCallback(&dev->m_timer, cb_func, cb_arg, cb_delay);
347 &ep->m_correspDevice->m_ioRequest,
348 USB_DIR_OUT | USB_RECIP_DEVICE,
350 ep->m_correspDevice->m_functionAddress,
354 hubSetFuncAddressCB);
359 dev->m_functionDelay = 20;
360 addTimerCallback(&dev->m_timer, (TimerCallback)hubSetFuncAddress, dev->m_endpointListStart, 21);
370 if ( dev->m_deviceStatus != DEVICE_NOTCONNECTED )
372 dev->m_deviceStatus = DEVICE_NOTCONNECTED;
373 if ( dev->m_devDriver )
374 callUsbDriverFunc(dev->m_devDriver->disconnect, dev->m_id, dev->m_devDriver->gp);
375 dev->m_devDriver = NULL;
376 if ( dev->m_timer.m_isActive )
377 cancelTimerCallback(&dev->m_timer);
378 while ( dev->m_endpointListStart )
380 removeEndpointFromDevice(dev, dev->m_endpointListStart);
382 for ( desc = dev->m_reportDescriptorStart; desc; desc = dev->m_reportDescriptorStart )
385 desc->m_next->m_prev = desc->m_prev;
387 dev->m_reportDescriptorEnd = desc->m_prev;
389 desc->m_prev->m_next = desc->m_next;
391 dev->m_reportDescriptorStart = desc->m_next;
396 while ( dev->m_childListStart )
398 child = dev->m_childListStart;
400 child->m_next->m_prev = child->m_prev;
402 dev->m_childListEnd = child->m_prev;
404 child->m_prev->m_next = child->m_next;
406 dev->m_childListStart = child->m_next;
410 dev->m_ioRequest.m_busyFlag = 0;
412 if ( dev->m_resetFlag )
413 checkDelayedResets(dev);
416int usbdInitInner(
void)
418 printf(
"USB Driver (%s)", usbdVersionString);
420 dbg_printf(
"HCD init...\n");
421 if ( initHcdStructs() < 0 )
423 dbg_printf(
"Hub driver...\n");
424 if ( initHubDriver() < 0 )
int CpuResumeIntr(int state)
int CpuSuspendIntr(int *state)
#define USB_RC_NORESPONSE
u32 count
start sector of fragmented bd/file