My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UHS_host_INLINE.h 53KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /* Copyright (C) 2015-2016 Andrew J. Kroll
  2. and
  3. Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. Contact information
  16. -------------------
  17. Circuits At Home, LTD
  18. Web : https://www.circuitsathome.com
  19. e-mail : support@circuitsathome.com
  20. */
  21. #if defined(LOAD_USB_HOST_SYSTEM) && !defined(USB_HOST_SYSTEM_LOADED)
  22. #define USB_HOST_SYSTEM_LOADED
  23. #ifndef DEBUG_PRINTF_EXTRA_HUGE_UHS_HOST
  24. #define DEBUG_PRINTF_EXTRA_HUGE_UHS_HOST 0
  25. #endif
  26. #if DEBUG_PRINTF_EXTRA_HUGE
  27. #if DEBUG_PRINTF_EXTRA_HUGE_UHS_HOST
  28. #define HOST_DEBUG(...) printf(__VA_ARGS__)
  29. #else
  30. #define HOST_DEBUG(...) VOID0
  31. #endif
  32. #else
  33. #define HOST_DEBUG(...) VOID0
  34. #endif
  35. UHS_EpInfo* UHS_USB_HOST_BASE::getEpInfoEntry(uint8_t addr, uint8_t ep) {
  36. UHS_Device *p = addrPool.GetUsbDevicePtr(addr);
  37. if(!p || !p->epinfo)
  38. return NULL;
  39. UHS_EpInfo *pep;
  40. for(uint8_t j = 0; j < UHS_HOST_MAX_INTERFACE_DRIVERS; j++) {
  41. pep = (UHS_EpInfo *)(p->epinfo[j]);
  42. for(uint8_t i = 0; i < p->epcount; i++) {
  43. if((pep)->epAddr == ep) {
  44. HOST_DEBUG("ep entry for interface %d ep %d max packet size = %d\r\n", pep->bIface, ep, pep->maxPktSize);
  45. return pep;
  46. }
  47. pep++;
  48. }
  49. }
  50. return NULL;
  51. }
  52. /**
  53. * Sets a device table entry for a device.
  54. * Each device is different and has different number of endpoints.
  55. * This function plugs endpoint record structure, defined in application, to devtable
  56. *
  57. * @param addr device address
  58. * @param epcount how many endpoints
  59. * @param eprecord pointer to the endpoint structure
  60. * @return Zero for success, or error code
  61. */
  62. uint8_t UHS_USB_HOST_BASE::setEpInfoEntry(uint8_t addr, uint8_t iface, uint8_t epcount, volatile UHS_EpInfo* eprecord) {
  63. if(!eprecord)
  64. return UHS_HOST_ERROR_BAD_ARGUMENT;
  65. UHS_Device *p = addrPool.GetUsbDevicePtr(addr);
  66. if(!p)
  67. return UHS_HOST_ERROR_NO_ADDRESS_IN_POOL;
  68. p->address.devAddress = addr;
  69. p->epinfo[iface] = eprecord;
  70. p->epcount = epcount;
  71. return 0;
  72. }
  73. /**
  74. * sets all endpoint addresses to zero.
  75. * Sets all max packet sizes to defaults
  76. * Clears all endpoint attributes
  77. * Sets bmNakPower to USB_NAK_DEFAULT
  78. * Sets binterface to zero.
  79. * Sets bNumEP to zero.
  80. * Sets bAddress to zero.
  81. * Clears qNextPollTime and sets bPollEnable to false.
  82. *
  83. * @param maxep How many endpoints to initialize
  84. * @param device pointer to the device driver instance (this)
  85. */
  86. void UHS_USB_HOST_BASE::DeviceDefaults(uint8_t maxep, UHS_USBInterface *interface) {
  87. for(uint8_t i = 0; i < maxep; i++) {
  88. interface->epInfo[i].epAddr = 0;
  89. interface->epInfo[i].maxPktSize = (i) ? 0 : 8;
  90. interface->epInfo[i].epAttribs = 0;
  91. interface->epInfo[i].bmNakPower = UHS_USB_NAK_DEFAULT;
  92. }
  93. interface->pUsb->GetAddressPool()->FreeAddress(interface->bAddress);
  94. interface->bIface = 0;
  95. interface->bNumEP = 1;
  96. interface->bAddress = 0;
  97. interface->qNextPollTime = 0;
  98. interface->bPollEnable = false;
  99. }
  100. /**
  101. * Perform a bus reset to the port of the connected device
  102. *
  103. * @param parent index to Parent
  104. * @param port what port on the parent
  105. * @param address address of the device
  106. * @return Zero for success, or error code
  107. */
  108. uint8_t UHS_USB_HOST_BASE::doSoftReset(uint8_t parent, uint8_t port, uint8_t address) {
  109. uint8_t rcode = 0;
  110. if(parent == 0) {
  111. // Send a bus reset on the root interface.
  112. doHostReset();
  113. } else {
  114. // reset parent port
  115. devConfig[parent]->ResetHubPort(port);
  116. }
  117. //
  118. // Many devices require a delay before setting the address here...
  119. // We loop upon fails for up to 2 seconds instead.
  120. // Most devices will be happy without a retry.
  121. //
  122. uint8_t retries = 0;
  123. if(address) {
  124. do {
  125. rcode = setAddr(0, address);
  126. if(!rcode) break;
  127. retries++;
  128. sof_delay(10);
  129. } while(retries < 200);
  130. HOST_DEBUG("%i retries.\r\n", retries);
  131. } else {
  132. #if DEBUG_PRINTF_EXTRA_HUGE
  133. printf("\r\ndoSoftReset called with address == 0.\r\n");
  134. #endif
  135. }
  136. return rcode;
  137. }
  138. /*
  139. * Pseudo code so you may understand the code flow.
  140. *
  141. * reset; (happens at the lower level)
  142. * GetDevDescr();
  143. * reset;
  144. * If there are no configuration descriptors {
  145. * //
  146. * // Note: I know of no device that does this.
  147. * // I suppose there could be one though.
  148. * //
  149. * try to enumerate.
  150. * } else {
  151. * last success count = 0
  152. * best config = 0
  153. * for each configuration descriptor {
  154. * for each interface descriptor {
  155. * get the endpoint descriptors for this interface.
  156. * Check to see if a driver can handle this interface.
  157. * If it can, add 1 to the success count.
  158. * }
  159. * if success count > last success count {
  160. * best config = current config
  161. * last success count = success count
  162. * }
  163. * }
  164. * set the device config to the best config
  165. * for each best config interface descriptor {
  166. * initialize driver that can handle this interface config
  167. * }
  168. * }
  169. *
  170. * NOTES:
  171. * 1: We do not need to save toggle states anymore and have not
  172. * needed to for some time, because the lower level driver
  173. * actually corrects wrong toggles on-the-fly for us.
  174. *
  175. * 2: We always do a second reset, since this stupid bug is
  176. * actually part of the specification documents that I
  177. * have found all over the net. Even Linux does it, and
  178. * many devices actually EXPECT this behavior. Some devices
  179. * will not enumerate without it. For devices that do not
  180. * need it, the additional reset is harmless. Here is an
  181. * example of one of these documents, see page Five:
  182. * https://www.ftdichip.com/Support/Documents/TechnicalNotes/TN_113_Simplified%20Description%20of%20USB%20Device%20Enumeration.pdf
  183. *
  184. */
  185. /**
  186. * Enumerates interfaces on devices
  187. *
  188. * @param parent index to Parent
  189. * @param port what port on the parent
  190. * @param speed the speed of the device
  191. * @return Zero for success, or error code
  192. */
  193. uint8_t UHS_USB_HOST_BASE::Configuring(uint8_t parent, uint8_t port, uint8_t speed) {
  194. //uint8_t bAddress = 0;
  195. HOST_DEBUG("\r\n\r\n\r\nConfiguring: parent = %i, port = %i, speed = %i\r\n", parent, port, speed);
  196. uint8_t rcode = 0;
  197. uint8_t retries = 0;
  198. uint8_t numinf = 0;
  199. uint8_t configs;
  200. UHS_Device *p = NULL;
  201. //EpInfo epInfo; // cap at 16, this should be fairly reasonable.
  202. ENUMERATION_INFO ei;
  203. uint8_t bestconf = 0;
  204. uint8_t bestsuccess = 0;
  205. uint8_t devConfigIndex;
  206. #if UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE
  207. const uint8_t biggest = 0x40;
  208. // wrap in {} to throw away the 64 byte buffer when we are done with it
  209. {
  210. uint8_t buf[biggest];
  211. USB_FD_DEVICE_DESCRIPTOR *udd = reinterpret_cast<USB_FD_DEVICE_DESCRIPTOR *>(buf);
  212. #else
  213. const uint8_t biggest = 18;
  214. uint8_t buf[biggest];
  215. USB_FD_DEVICE_DESCRIPTOR *udd = reinterpret_cast<USB_FD_DEVICE_DESCRIPTOR *>(buf);
  216. USB_FD_CONFIGURATION_DESCRIPTOR *ucd = reinterpret_cast<USB_FD_CONFIGURATION_DESCRIPTOR *>(buf);
  217. #endif
  218. //for(devConfigIndex = 0; devConfigIndex < UHS_HOST_MAX_INTERFACE_DRIVERS; devConfigIndex++) {
  219. // if((devConfig[devConfigIndex]->bAddress) && (!devConfig[devConfigIndex]->bPollEnable)) {
  220. // devConfig[devConfigIndex]->bAddress = 0;
  221. // }
  222. //}
  223. // Serial.print("HOST USB Host @ 0x");
  224. // Serial.println((uint32_t)this, HEX);
  225. // Serial.print("HOST USB Host Address Pool @ 0x");
  226. // Serial.println((uint32_t)GetAddressPool(), HEX);
  227. sof_delay(200);
  228. p = addrPool.GetUsbDevicePtr(0);
  229. if(!p) {
  230. HOST_DEBUG("Configuring error: USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL\r\n");
  231. return UHS_HOST_ERROR_NO_ADDRESS_IN_POOL;
  232. }
  233. p->speed = speed;
  234. #if UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE
  235. p->epinfo[0][0].maxPktSize = 0x40; // Windows bug is expected.
  236. // poison data
  237. // udd->bMaxPacketSize0 = 0U;
  238. #else
  239. p->epinfo[0][0].maxPktSize = 0x08; // USB Spec, start small, work your way up.
  240. #endif
  241. again:
  242. memset((void *)buf, 0, biggest);
  243. HOST_DEBUG("\r\n\r\nConfiguring PktSize 0x%2.2x, rcode: 0x%2.2x, retries %i,\r\n", p->epinfo[0][0].maxPktSize, rcode, retries);
  244. rcode = getDevDescr(0, biggest, (uint8_t*)buf);
  245. #if UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE
  246. if(rcode || udd->bMaxPacketSize0 < 8)
  247. #else
  248. if(rcode)
  249. #endif
  250. {
  251. if(rcode == UHS_HOST_ERROR_JERR && retries < 4) {
  252. //
  253. // Some devices return JERR when plugged in.
  254. // Attempts to reinitialize the device usually works.
  255. //
  256. // I have a hub that will refuse to work and acts like
  257. // this unless external power is supplied.
  258. // So this may not always work, and you may be fooled.
  259. //
  260. sof_delay(100);
  261. retries++;
  262. goto again;
  263. #if UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE
  264. } else if(((rcode == UHS_HOST_ERROR_DMA || rcode == UHS_HOST_ERROR_MEM_LAT) && retries < 4) || (udd->bMaxPacketSize0 < 8 && !rcode)) {
  265. if(p->epinfo[0][0].maxPktSize > 8 && rcode == UHS_HOST_ERROR_DMA) p->epinfo[0][0].maxPktSize = p->epinfo[0][0].maxPktSize >> 1;
  266. #else
  267. } else if((rcode == UHS_HOST_ERROR_DMA || rcode == UHS_HOST_ERROR_MEM_LAT) && retries < 4) {
  268. if(p->epinfo[0][0].maxPktSize < 32) p->epinfo[0][0].maxPktSize = p->epinfo[0][0].maxPktSize << 1;
  269. #endif
  270. HOST_DEBUG("Configuring error: 0x%2.2x UHS_HOST_ERROR_DMA. Retry with maxPktSize: %i\r\n", rcode, p->epinfo[0][0].maxPktSize);
  271. doSoftReset(parent, port, 0);
  272. retries++;
  273. sof_delay(200);
  274. goto again;
  275. }
  276. HOST_DEBUG("Configuring error: 0x%2.2x Can't get USB_FD_DEVICE_DESCRIPTOR\r\n", rcode);
  277. return rcode;
  278. }
  279. #if UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE
  280. ei.address = addrPool.AllocAddress(parent, false, port);
  281. if(!ei.address) {
  282. return UHS_HOST_ERROR_ADDRESS_POOL_FULL;
  283. }
  284. p = addrPool.GetUsbDevicePtr(ei.address);
  285. // set to 1 if you suspect address table corruption.
  286. #if 0
  287. if(!p) {
  288. return UHS_HOST_ERROR_NO_ADDRESS_IN_POOL;
  289. }
  290. #endif
  291. p->speed = speed;
  292. rcode = doSoftReset(parent, port, ei.address);
  293. if(rcode) {
  294. addrPool.FreeAddress(ei.address);
  295. HOST_DEBUG("Configuring error: %2.2x Can't set USB INTERFACE ADDRESS\r\n", rcode);
  296. return rcode;
  297. }
  298. { // the { } wrapper saves on stack.
  299. HOST_DEBUG("DevDescr 2nd poll, bMaxPacketSize0:%u\r\n", udd->bMaxPacketSize0);
  300. UHS_EpInfo dev1ep;
  301. dev1ep.maxPktSize = udd->bMaxPacketSize0;
  302. dev1ep.epAddr = 0;
  303. dev1ep.epAttribs = 0;
  304. dev1ep.bmNakPower = UHS_USB_NAK_MAX_POWER;
  305. p->address.devAddress = ei.address;
  306. p->epcount = 1;
  307. p->epinfo[0] = &dev1ep;
  308. sof_delay(10);
  309. memset((void *)buf, 0, biggest);
  310. rcode = getDevDescr(ei.address, 18, (uint8_t*)buf);
  311. if(rcode) HOST_DEBUG("getDevDescr err: 0x%x \r\n", rcode);
  312. addrPool.FreeAddress(ei.address);
  313. if(rcode && rcode != UHS_HOST_ERROR_DMA) {
  314. return rcode;
  315. }
  316. sof_delay(10);
  317. }
  318. #endif
  319. ei.vid = udd->idVendor;
  320. ei.pid = udd->idProduct;
  321. ei.bcdDevice = udd->bcdDevice;
  322. ei.klass = udd->bDeviceClass;
  323. ei.subklass = udd->bDeviceSubClass;
  324. ei.protocol = udd->bDeviceProtocol;
  325. ei.bMaxPacketSize0 = udd->bMaxPacketSize0;
  326. ei.currentconfig = 0;
  327. ei.parent = parent;
  328. ei.port = port;
  329. configs = udd->bNumConfigurations;
  330. #if UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE
  331. } // unwrapped, old large buf now invalid and discarded.
  332. uint8_t buf[18];
  333. USB_FD_CONFIGURATION_DESCRIPTOR *ucd = reinterpret_cast<USB_FD_CONFIGURATION_DESCRIPTOR *>(buf);
  334. #endif
  335. ei.address = addrPool.AllocAddress(parent, IsHub(ei.klass), port);
  336. if(!ei.address) {
  337. return UHS_HOST_ERROR_ADDRESS_POOL_FULL;
  338. }
  339. p = addrPool.GetUsbDevicePtr(ei.address);
  340. // set to 1 if you suspect address table corruption.
  341. #if 0
  342. if(!p) {
  343. return UHS_HOST_ERROR_NO_ADDRESS_IN_POOL;
  344. }
  345. #endif
  346. p->speed = speed;
  347. rcode = doSoftReset(parent, port, ei.address);
  348. if(rcode) {
  349. addrPool.FreeAddress(ei.address);
  350. HOST_DEBUG("Configuring error: %2.2x Can't set USB INTERFACE ADDRESS\r\n", rcode);
  351. return rcode;
  352. }
  353. if(configs < 1) {
  354. HOST_DEBUG("No interfaces?!\r\n");
  355. addrPool.FreeAddress(ei.address);
  356. // rcode = TestInterface(&ei);
  357. // Not implemented (yet)
  358. rcode = UHS_HOST_ERROR_DEVICE_NOT_SUPPORTED;
  359. } else {
  360. HOST_DEBUG("configs: %i\r\n", configs);
  361. for(uint8_t conf = 0; (!rcode) && (conf < configs); conf++) {
  362. // read the config descriptor into a buffer.
  363. rcode = getConfDescr(ei.address, sizeof (USB_FD_CONFIGURATION_DESCRIPTOR), conf, buf);
  364. if(rcode) {
  365. HOST_DEBUG("Configuring error: %2.2x Can't get USB_FD_INTERFACE_DESCRIPTOR\r\n", rcode);
  366. rcode = UHS_HOST_ERROR_FailGetConfDescr;
  367. continue;
  368. }
  369. ei.currentconfig = conf;
  370. numinf = ucd->bNumInterfaces; // Does _not_ include alternates!
  371. HOST_DEBUG("CONFIGURATION: %i, bNumInterfaces %i, wTotalLength %i\r\n", conf, numinf, ucd->wTotalLength);
  372. uint8_t success = 0;
  373. uint16_t inf = 0;
  374. uint8_t data[ei.bMaxPacketSize0];
  375. UHS_EpInfo *pep;
  376. pep = ctrlReqOpen(ei.address, mkSETUP_PKT8(UHS_bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, ei.currentconfig, USB_DESCRIPTOR_CONFIGURATION, 0x0000U, ucd->wTotalLength), data);
  377. if(!pep) {
  378. rcode = UHS_HOST_ERROR_NULL_EPINFO;
  379. continue;
  380. }
  381. uint16_t left;
  382. uint16_t read;
  383. uint8_t offset;
  384. rcode = initDescrStream(&ei, ucd, pep, data, &left, &read, &offset);
  385. if(rcode) {
  386. HOST_DEBUG("Configuring error: %2.2x Can't get USB_FD_INTERFACE_DESCRIPTOR stream.\r\n", rcode);
  387. break;
  388. }
  389. for(; (numinf) && (!rcode); inf++) {
  390. // iterate for each interface on this config
  391. rcode = getNextInterface(&ei, pep, data, &left, &read, &offset);
  392. if(rcode == UHS_HOST_ERROR_END_OF_STREAM) {
  393. HOST_DEBUG("USB_INTERFACE END OF STREAM\r\n");
  394. ctrlReqClose(pep, UHS_bmREQ_GET_DESCR, left, ei.bMaxPacketSize0, data);
  395. rcode = 0;
  396. break;
  397. }
  398. if(rcode) {
  399. HOST_DEBUG("Configuring error: %2.2x Can't close USB_FD_INTERFACE_DESCRIPTOR stream.\r\n", rcode);
  400. continue;
  401. }
  402. rcode = TestInterface(&ei);
  403. if(!rcode) success++;
  404. rcode = 0;
  405. }
  406. if(!inf) {
  407. rcode = TestInterface(&ei);
  408. if(!rcode) success++;
  409. rcode = 0;
  410. }
  411. if(success > bestsuccess) {
  412. bestconf = conf;
  413. bestsuccess = success;
  414. }
  415. }
  416. if(!bestsuccess) rcode = UHS_HOST_ERROR_DEVICE_NOT_SUPPORTED;
  417. }
  418. if(!rcode) {
  419. rcode = getConfDescr(ei.address, sizeof (USB_FD_CONFIGURATION_DESCRIPTOR), bestconf, buf);
  420. if(rcode) {
  421. HOST_DEBUG("Configuring error: %2.2x Can't get USB_FD_INTERFACE_DESCRIPTOR\r\n", rcode);
  422. rcode = UHS_HOST_ERROR_FailGetConfDescr;
  423. }
  424. }
  425. if(!rcode) {
  426. bestconf++;
  427. ei.currentconfig = bestconf;
  428. numinf = ucd->bNumInterfaces; // Does _not_ include alternates!
  429. HOST_DEBUG("CONFIGURATION: %i, bNumInterfaces %i, wTotalLength %i\r\n", bestconf, numinf, ucd->wTotalLength);
  430. if(!rcode) {
  431. HOST_DEBUG("Best configuration is %i, enumerating interfaces.\r\n", bestconf);
  432. uint16_t inf = 0;
  433. uint8_t data[ei.bMaxPacketSize0];
  434. UHS_EpInfo *pep;
  435. pep = ctrlReqOpen(ei.address, mkSETUP_PKT8(UHS_bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, ei.currentconfig - 1, USB_DESCRIPTOR_CONFIGURATION, 0x0000U, ucd->wTotalLength), data);
  436. if(!pep) {
  437. rcode = UHS_HOST_ERROR_NULL_EPINFO;
  438. } else {
  439. uint16_t left;
  440. uint16_t read;
  441. uint8_t offset;
  442. rcode = initDescrStream(&ei, ucd, pep, data, &left, &read, &offset);
  443. if(rcode) {
  444. HOST_DEBUG("Configuring error: %2.2x Can't get USB_FD_INTERFACE_DESCRIPTOR stream.\r\n", rcode);
  445. } else {
  446. for(; (numinf) && (!rcode); inf++) {
  447. // iterate for each interface on this config
  448. rcode = getNextInterface(&ei, pep, data, &left, &read, &offset);
  449. if(rcode == UHS_HOST_ERROR_END_OF_STREAM) {
  450. ctrlReqClose(pep, UHS_bmREQ_GET_DESCR, left, ei.bMaxPacketSize0, data);
  451. rcode = 0;
  452. break;
  453. }
  454. if(rcode) {
  455. HOST_DEBUG("Configuring error: %2.2x Can't close USB_FD_INTERFACE_DESCRIPTOR stream.\r\n", rcode);
  456. continue;
  457. }
  458. if(enumerateInterface(&ei) == UHS_HOST_MAX_INTERFACE_DRIVERS) {
  459. HOST_DEBUG("No interface driver for this interface.");
  460. } else {
  461. HOST_DEBUG("Interface Configured\r\n");
  462. }
  463. }
  464. }
  465. }
  466. } else {
  467. HOST_DEBUG("Configuring error: %2.2x Can't set USB_INTERFACE_CONFIG stream.\r\n", rcode);
  468. }
  469. }
  470. if(!rcode) {
  471. rcode = setConf(ei.address, bestconf);
  472. if(rcode) {
  473. HOST_DEBUG("Configuring error: %2.2x Can't set Configuration.\r\n", rcode);
  474. addrPool.FreeAddress(ei.address);
  475. } else {
  476. for(devConfigIndex = 0; devConfigIndex < UHS_HOST_MAX_INTERFACE_DRIVERS; devConfigIndex++) {
  477. HOST_DEBUG("Driver %i ", devConfigIndex);
  478. if(!devConfig[devConfigIndex]) {
  479. HOST_DEBUG("no driver at this index.\r\n");
  480. continue; // no driver
  481. }
  482. HOST_DEBUG("@ %2.2x ", devConfig[devConfigIndex]->bAddress);
  483. if(devConfig[devConfigIndex]->bAddress) {
  484. if(!devConfig[devConfigIndex]->bPollEnable) {
  485. HOST_DEBUG("Initialize\r\n");
  486. rcode = devConfig[devConfigIndex]->Finalize();
  487. rcode = devConfig[devConfigIndex]->Start();
  488. if(!rcode) {
  489. HOST_DEBUG("Total endpoints = (%i)%i\r\n", p->epcount, devConfig[devConfigIndex]->bNumEP);
  490. } else {
  491. break;
  492. }
  493. } else {
  494. HOST_DEBUG("Already initialized.\r\n");
  495. continue; // consumed
  496. }
  497. } else {
  498. HOST_DEBUG("Skipped\r\n");
  499. }
  500. }
  501. #if 0 // defined(UHS_HID_LOADED)
  502. // Now do HID
  503. #endif
  504. }
  505. } else {
  506. addrPool.FreeAddress(ei.address);
  507. }
  508. return rcode;
  509. }
  510. /**
  511. * Removes a device from the tables
  512. *
  513. * @param addr address of the device
  514. * @return nothing
  515. */
  516. void UHS_USB_HOST_BASE::ReleaseDevice(uint8_t addr) {
  517. if(addr) {
  518. #if 0 // defined(UHS_HID_LOADED)
  519. // Release any HID children
  520. UHS_HID_Release(this, addr);
  521. #endif
  522. for(uint8_t i = 0; i < UHS_HOST_MAX_INTERFACE_DRIVERS; i++) {
  523. if(!devConfig[i]) continue;
  524. if(devConfig[i]->bAddress == addr) {
  525. devConfig[i]->Release();
  526. break;
  527. }
  528. }
  529. }
  530. }
  531. /**
  532. * Gets the device descriptor, or part of it from endpoint Zero.
  533. *
  534. * @param addr Address of the device
  535. * @param nbytes how many bytes to return
  536. * @param dataptr pointer to the data to return
  537. * @return status of the request, zero is success.
  538. */
  539. uint8_t UHS_USB_HOST_BASE::getDevDescr(uint8_t addr, uint16_t nbytes, uint8_t *dataptr) {
  540. return ( ctrlReq(addr, mkSETUP_PKT8(UHS_bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, 0x00, USB_DESCRIPTOR_DEVICE, 0x0000, nbytes), nbytes, dataptr));
  541. }
  542. /**
  543. * Gets the config descriptor, or part of it from endpoint Zero.
  544. *
  545. * @param addr Address of the device
  546. * @param nbytes how many bytes to return
  547. * @param conf index to descriptor to return
  548. * @param dataptr ointer to the data to return
  549. * @return status of the request, zero is success.
  550. */
  551. uint8_t UHS_USB_HOST_BASE::getConfDescr(uint8_t addr, uint16_t nbytes, uint8_t conf, uint8_t *dataptr) {
  552. return ( ctrlReq(addr, mkSETUP_PKT8(UHS_bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, conf, USB_DESCRIPTOR_CONFIGURATION, 0x0000, nbytes), nbytes, dataptr));
  553. }
  554. /**
  555. * Get the string descriptor from a device
  556. *
  557. * @param addr Address of the device
  558. * @param ns
  559. * @param index
  560. * @param langid language ID
  561. * @param dataptr pointer to the data to return
  562. * @return status of the request, zero is success.
  563. */
  564. uint8_t UHS_USB_HOST_BASE::getStrDescr(uint8_t addr, uint16_t ns, uint8_t index, uint16_t langid, uint8_t *dataptr) {
  565. return ( ctrlReq(addr, mkSETUP_PKT8(UHS_bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, index, USB_DESCRIPTOR_STRING, langid, ns), ns, dataptr));
  566. }
  567. //
  568. //set address
  569. //
  570. /**
  571. * Set the address of a device to a new address via endpoint Zero.
  572. *
  573. * @param oldaddr current address
  574. * @param newaddr new address
  575. * @return status of the request, zero is success.
  576. */
  577. uint8_t UHS_USB_HOST_BASE::setAddr(uint8_t oldaddr, uint8_t newaddr) {
  578. uint8_t rcode = ctrlReq(oldaddr, mkSETUP_PKT8(UHS_bmREQ_SET, USB_REQUEST_SET_ADDRESS, newaddr, 0x00, 0x0000, 0x0000), 0x0000, NULL);
  579. sof_delay(300); // Older spec says you should wait at least 200ms
  580. return rcode;
  581. }
  582. //
  583. //set configuration
  584. //
  585. /**
  586. * Set the configuration for the device to use via endpoint Zero.
  587. *
  588. * @param addr Address of the device
  589. * @param conf_value configuration index value
  590. * @return status of the request, zero is success.
  591. */
  592. uint8_t UHS_USB_HOST_BASE::setConf(uint8_t addr, uint8_t conf_value) {
  593. return ( ctrlReq(addr, mkSETUP_PKT8(UHS_bmREQ_SET, USB_REQUEST_SET_CONFIGURATION, conf_value, 0x00, 0x0000, 0x0000), 0x0000, NULL));
  594. }
  595. /* rcode 0 if no errors. rcode 01-0f is relayed from HRSL */
  596. /**
  597. * Writes data to an interface pipe
  598. *
  599. * @param addr Address of the device
  600. * @param ep Endpoint of the pipe
  601. * @param nbytes number of bytes to transfer
  602. * @param data pointer to buffer to hold transfer
  603. * @return zero for success or error code
  604. */
  605. uint8_t UHS_USB_HOST_BASE::outTransfer(uint8_t addr, uint8_t ep, uint16_t nbytes, uint8_t *data) {
  606. UHS_EpInfo *pep = NULL;
  607. uint16_t nak_limit = 0;
  608. HOST_DEBUG("outTransfer: addr: 0x%2.2x ep: 0x%2.2x nbytes: 0x%4.4x data: 0x%p\r\n", addr, ep, nbytes, data);
  609. uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
  610. HOST_DEBUG("outTransfer: SetAddress 0x%2.2x\r\n", rcode);
  611. if(!rcode)
  612. rcode = OutTransfer(pep, nak_limit, nbytes, data);
  613. return rcode;
  614. };
  615. /**
  616. * Reads data from an interface pipe
  617. *
  618. * @param addr Address of the device
  619. * @param ep Endpoint of the pipe
  620. * @param nbytesptr number of bytes to transfer
  621. * @param data pointer to buffer to hold transfer
  622. * @return zero for success or error code
  623. */
  624. uint8_t UHS_USB_HOST_BASE::inTransfer(uint8_t addr, uint8_t ep, uint16_t *nbytesptr, uint8_t *data) {
  625. UHS_EpInfo *pep = NULL;
  626. uint16_t nak_limit = 0;
  627. uint8_t rcode = SetAddress(addr, ep, &pep, nak_limit);
  628. // if(rcode) {
  629. // USBTRACE3("(USB::InTransfer) SetAddress Failed ", rcode, 0x81);
  630. // USBTRACE3("(USB::InTransfer) addr requested ", addr, 0x81);
  631. // USBTRACE3("(USB::InTransfer) ep requested ", ep, 0x81);
  632. // return rcode;
  633. // }
  634. if(!rcode)
  635. rcode = InTransfer(pep, nak_limit, nbytesptr, data);
  636. return rcode;
  637. }
  638. /**
  639. * Initialize the descriptor stream, works much like opening a file.
  640. *
  641. * @param ei
  642. * @param ucd
  643. * @param pep
  644. * @param data
  645. * @param left
  646. * @param read
  647. * @param offset
  648. * @return zero for success or error code
  649. */
  650. uint8_t UHS_USB_HOST_BASE::initDescrStream(ENUMERATION_INFO *ei, USB_FD_CONFIGURATION_DESCRIPTOR *ucd, UHS_EpInfo *pep, uint8_t *data, uint16_t *left, uint16_t *read, uint8_t *offset) {
  651. if(!ei || !ucd) return UHS_HOST_ERROR_BAD_ARGUMENT;
  652. if(!pep) return UHS_HOST_ERROR_NULL_EPINFO;
  653. *left = ucd->wTotalLength;
  654. *read = 0;
  655. *offset = 1;
  656. uint8_t rcode;
  657. pep->maxPktSize = ei->bMaxPacketSize0;
  658. rcode = getone(pep, left, read, data, offset);
  659. return rcode;
  660. }
  661. uint8_t UHS_USB_HOST_BASE::getNextInterface(ENUMERATION_INFO *ei, UHS_EpInfo *pep, uint8_t data[], uint16_t *left, uint16_t *read, uint8_t *offset) {
  662. uint16_t remain;
  663. uint8_t ty;
  664. uint8_t rcode = UHS_HOST_ERROR_END_OF_STREAM;
  665. uint8_t *ptr;
  666. uint8_t epc = 0;
  667. ei->interface.numep = 0;
  668. ei->interface.klass = 0;
  669. ei->interface.subklass = 0;
  670. ei->interface.protocol = 0;
  671. while(*left + *read) {
  672. remain = data[*offset]; // bLength
  673. while(remain < 2) {
  674. rcode = getone(pep, left, read, data, offset);
  675. if(rcode)
  676. return rcode;
  677. remain = data[*offset];
  678. }
  679. rcode = getone(pep, left, read, data, offset);
  680. if(rcode)
  681. return rcode;
  682. ty = data[*offset]; // bDescriptorType
  683. HOST_DEBUG("bLength: %i ", remain);
  684. HOST_DEBUG("bDescriptorType: %2.2x\r\n", ty);
  685. remain--;
  686. if(ty == USB_DESCRIPTOR_INTERFACE) {
  687. HOST_DEBUG("INTERFACE DESCRIPTOR FOUND\r\n");
  688. ptr = (uint8_t *)(&(ei->interface.bInterfaceNumber));
  689. for(int i = 0; i < 6; i++) {
  690. rcode = getone(pep, left, read, data, offset);
  691. if(rcode)
  692. return rcode;
  693. *ptr = data[*offset];
  694. ptr++;
  695. }
  696. rcode = getone(pep, left, read, data, offset);
  697. if(rcode)
  698. return rcode;
  699. // Now at iInterface
  700. // Get endpoints.
  701. HOST_DEBUG("Getting %i endpoints\r\n", ei->interface.numep);
  702. while(epc < ei->interface.numep) {
  703. rcode = getone(pep, left, read, data, offset);
  704. if(rcode) {
  705. HOST_DEBUG("ENDPOINT DESCRIPTOR DIED WAY EARLY\r\n");
  706. return rcode;
  707. }
  708. remain = data[*offset]; // bLength
  709. while(remain < 2) {
  710. rcode = getone(pep, left, read, data, offset);
  711. if(rcode)
  712. return rcode;
  713. remain = data[*offset];
  714. }
  715. rcode = getone(pep, left, read, data, offset);
  716. if(rcode) {
  717. HOST_DEBUG("ENDPOINT DESCRIPTOR DIED EARLY\r\n");
  718. return rcode;
  719. }
  720. ty = data[*offset]; // bDescriptorType
  721. HOST_DEBUG("bLength: %i ", remain);
  722. HOST_DEBUG("bDescriptorType: %2.2x\r\n", ty);
  723. remain -= 2;
  724. if(ty == USB_DESCRIPTOR_ENDPOINT) {
  725. HOST_DEBUG("ENDPOINT DESCRIPTOR: %i\r\n", epc);
  726. ptr = (uint8_t *)(&(ei->interface.epInfo[epc].bEndpointAddress));
  727. for(unsigned int i = 0; i< sizeof (ENDPOINT_INFO); i++) {
  728. rcode = getone(pep, left, read, data, offset);
  729. if(rcode) {
  730. HOST_DEBUG("ENDPOINT DESCRIPTOR DIED LATE\r\n");
  731. return rcode;
  732. }
  733. *ptr = data[*offset];
  734. ptr++;
  735. remain--;
  736. }
  737. epc++;
  738. HOST_DEBUG("ENDPOINT DESCRIPTOR OK\r\n");
  739. }
  740. rcode = eat(pep, left, read, data, offset, &remain);
  741. if(rcode) {
  742. HOST_DEBUG("ENDPOINT DESCRIPTOR DIED EATING\r\n");
  743. return rcode;
  744. }
  745. remain = 0;
  746. }
  747. remain = 1;
  748. // queue ahead, but do not report if error.
  749. rcode = eat(pep, left, read, data, offset, &remain);
  750. if(!ei->interface.numep && rcode) {
  751. return rcode;
  752. }
  753. HOST_DEBUG("ENDPOINT DESCRIPTORS FILLED\r\n");
  754. return 0;
  755. } else {
  756. rcode = eat(pep, left, read, data, offset, &remain);
  757. if(rcode)
  758. return rcode;
  759. }
  760. rcode = UHS_HOST_ERROR_END_OF_STREAM;
  761. }
  762. return rcode;
  763. }
  764. uint8_t UHS_USB_HOST_BASE::seekInterface(ENUMERATION_INFO *ei, uint16_t inf, USB_FD_CONFIGURATION_DESCRIPTOR *ucd) {
  765. if(!ei || !ucd) return UHS_HOST_ERROR_BAD_ARGUMENT;
  766. uint8_t data[ei->bMaxPacketSize0];
  767. UHS_EpInfo *pep;
  768. pep = ctrlReqOpen(ei->address, mkSETUP_PKT8(UHS_bmREQ_GET_DESCR, USB_REQUEST_GET_DESCRIPTOR, ei->currentconfig,
  769. USB_DESCRIPTOR_CONFIGURATION, 0x0000U, ucd->wTotalLength), data);
  770. if(!pep) return UHS_HOST_ERROR_NULL_EPINFO;
  771. uint16_t left = ucd->wTotalLength;
  772. uint8_t cinf = 0;
  773. uint8_t ty;
  774. uint8_t epc = 0;
  775. uint16_t remain = ucd->bLength;
  776. uint16_t read = 0;
  777. uint8_t offset = remain;
  778. uint8_t *ptr;
  779. uint8_t rcode;
  780. ei->interface.numep = 0;
  781. ei->interface.klass = 0;
  782. ei->interface.subklass = 0;
  783. ei->interface.protocol = 0;
  784. pep->maxPktSize = ei->bMaxPacketSize0;
  785. rcode = getone(pep, &left, &read, data, &offset);
  786. if(rcode)
  787. return rcode;
  788. HOST_DEBUG("\r\nGetting interface: %i\r\n", inf);
  789. inf++;
  790. while(cinf != inf && (left + read)) {
  791. //HOST_DEBUG("getInterface: cinf: %i inf: %i left: %i read: %i offset: %i remain %i\r\n", cinf, inf, left, read, offset, remain);
  792. // Go past current descriptor
  793. HOST_DEBUG("Skip: %i\r\n", remain);
  794. rcode = eat(pep, &left, &read, data, &offset, &remain);
  795. if(rcode)
  796. return rcode;
  797. remain = data[offset]; // bLength
  798. while(remain < 2) {
  799. rcode = getone(pep, &left, &read, data, &offset);
  800. if(rcode)
  801. return rcode;
  802. remain = data[offset];
  803. }
  804. rcode = getone(pep, &left, &read, data, &offset);
  805. if(rcode)
  806. return rcode;
  807. ty = data[offset]; // bDescriptorType
  808. HOST_DEBUG("bLength: %i ", remain);
  809. HOST_DEBUG("bDescriptorType: %2.2x\r\n", ty);
  810. remain--;
  811. if(ty == USB_DESCRIPTOR_INTERFACE) {
  812. HOST_DEBUG("INTERFACE DESCRIPTOR: %i\r\n", cinf);
  813. cinf++;
  814. if(cinf == inf) {
  815. // Get the interface descriptor information.
  816. ptr = (uint8_t *)(&(ei->interface.bInterfaceNumber));
  817. for(int i = 0; i < 6; i++) {
  818. rcode = getone(pep, &left, &read, data, &offset);
  819. if(rcode)
  820. return rcode;
  821. *ptr = data[offset];
  822. ptr++;
  823. }
  824. rcode = getone(pep, &left, &read, data, &offset);
  825. if(rcode)
  826. return rcode;
  827. // Now at iInterface
  828. remain = 0;
  829. // Get endpoints.
  830. HOST_DEBUG("Getting %i endpoints\r\n", ei->interface.numep);
  831. while(epc < ei->interface.numep) {
  832. rcode = getone(pep, &left, &read, data, &offset);
  833. if(rcode)
  834. return rcode;
  835. remain = data[offset]; // bLength
  836. while(remain < 2) {
  837. rcode = getone(pep, &left, &read, data, &offset);
  838. if(rcode)
  839. return rcode;
  840. remain = data[offset];
  841. }
  842. rcode = getone(pep, &left, &read, data, &offset);
  843. if(rcode)
  844. return rcode;
  845. ty = data[offset]; // bDescriptorType
  846. HOST_DEBUG("bLength: %i ", remain);
  847. HOST_DEBUG("bDescriptorType: %2.2x\r\n", ty);
  848. remain--;
  849. if(ty == USB_DESCRIPTOR_ENDPOINT) {
  850. HOST_DEBUG("ENDPOINT DESCRIPTOR: %i\r\n", epc);
  851. ptr = (uint8_t *)(&(ei->interface.epInfo[epc].bEndpointAddress));
  852. for(unsigned int i = 0; i< sizeof (ENDPOINT_INFO); i++) {
  853. rcode = getone(pep, &left, &read, data, &offset);
  854. if(rcode)
  855. return rcode;
  856. *ptr = data[offset];
  857. ptr++;
  858. }
  859. epc++;
  860. remain = 0;
  861. } else {
  862. rcode = eat(pep, &left, &read, data, &offset, &remain);
  863. if(rcode)
  864. return rcode;
  865. remain = 0;
  866. }
  867. }
  868. }
  869. }
  870. }
  871. return ctrlReqClose(pep, UHS_bmREQ_GET_DESCR, left, ei->bMaxPacketSize0, data);
  872. }
  873. uint8_t UHS_USB_HOST_BASE::getone(UHS_EpInfo *pep, uint16_t *left, uint16_t *read, uint8_t *dataptr, uint8_t *offset) {
  874. uint8_t rcode = 0;
  875. *offset += 1;
  876. if(*offset < *read) {
  877. return 0;
  878. } else if(*left > 0) {
  879. // uint16_t num = *left;
  880. uint16_t num = pep->maxPktSize;
  881. if(num > *left) num = *left;
  882. *offset = 0;
  883. rcode = ctrlReqRead(pep, left, read, num, dataptr);
  884. if(rcode == 0) {
  885. if(*read == 0) {
  886. rcode = UHS_HOST_ERROR_END_OF_STREAM;
  887. } else if(*read < num) *left = 0;
  888. }
  889. } else {
  890. rcode = UHS_HOST_ERROR_END_OF_STREAM;
  891. }
  892. return rcode;
  893. }
  894. uint8_t UHS_USB_HOST_BASE::eat(UHS_EpInfo *pep, uint16_t *left, uint16_t *read, uint8_t *dataptr, uint8_t *offset, uint16_t *yum) {
  895. uint8_t rcode = 0;
  896. HOST_DEBUG("eating %i\r\n", *yum);
  897. while(*yum) {
  898. *yum -= 1;
  899. rcode = getone(pep, left, read, dataptr, offset);
  900. if(rcode) break;
  901. }
  902. return rcode;
  903. }
  904. uint8_t UHS_USB_HOST_BASE::ctrlReq(uint8_t addr, uint64_t Request, uint16_t nbytes, uint8_t *dataptr) {
  905. //bool direction = bmReqType & 0x80; //request direction, IN or OUT
  906. uint8_t rcode = 0;
  907. //Serial.println();
  908. UHS_EpInfo *pep = ctrlReqOpen(addr, Request, dataptr);
  909. if(!pep) {
  910. HOST_DEBUG("ctrlReq1: ERROR_NULL_EPINFO addr: %d\r\n", addr);
  911. return UHS_HOST_ERROR_NULL_EPINFO;
  912. }
  913. uint8_t rt = (uint8_t)(Request & 0xFFU);
  914. // Serial.println("Opened");
  915. uint16_t left = (uint16_t)(Request >> 48) /*total*/;
  916. if(dataptr != NULL) {
  917. //data stage
  918. if((rt & 0x80) == 0x80) {
  919. //IN transfer
  920. while(left) {
  921. // Bytes read into buffer
  922. uint16_t read = nbytes;
  923. HOST_DEBUG("ctrlReq2: left: %i, read:%i, nbytes %i\r\n", left, read, nbytes);
  924. rcode = ctrlReqRead(pep, &left, &read, nbytes, dataptr);
  925. #if UHS_DEVICE_WINDOWS_USB_SPEC_VIOLATION_DESCRIPTOR_DEVICE
  926. HOST_DEBUG("RESULT: 0x%2.2x 0x%2.2x 0x%2.2x 0x%8.8lx%8.8lx\r\n", rcode, addr, read, (uint32_t)((Request>>32)&0xFFFFFFFFLU), (uint32_t)(Request&0xFFFFFFFFLU));
  927. // Should only be used for GET_DESCRIPTOR USB_DESCRIPTOR_DEVICE
  928. constexpr uint32_t req_match = ((uint32_t)USB_DESCRIPTOR_DEVICE << 24) |
  929. ((uint32_t)USB_REQUEST_GET_DESCRIPTOR << 8);
  930. const uint32_t req_found = Request & 0xFF00FF00ul;
  931. if(!addr && read && (req_found == req_match)) {
  932. HOST_DEBUG("ctrlReq3: acceptBuffer sz %i nbytes %i left %i\n\r", read, nbytes, left);
  933. left = 0;
  934. rcode = UHS_HOST_ERROR_NONE;
  935. break;
  936. }
  937. #endif
  938. if(rcode) {
  939. return rcode;
  940. }
  941. }
  942. } else {
  943. // OUT transfer
  944. rcode = OutTransfer(pep, 0, nbytes, dataptr);
  945. }
  946. if(rcode) {
  947. //return error
  948. return ( rcode);
  949. }
  950. }
  951. // Serial.println("Close Phase");
  952. // Serial.flush();
  953. // Status stage
  954. rcode = ctrlReqClose(pep, rt, left, nbytes, dataptr);
  955. // Serial.println("Closed");
  956. return rcode;
  957. }
  958. uint8_t UHS_USB_HOST_BASE::EPClearHalt(uint8_t addr, uint8_t ep) {
  959. return ctrlReq(addr, mkSETUP_PKT8(USB_SETUP_HOST_TO_DEVICE | USB_SETUP_TYPE_STANDARD | USB_SETUP_RECIPIENT_ENDPOINT, USB_REQUEST_CLEAR_FEATURE, USB_FEATURE_ENDPOINT_HALT, 0, ep, 0), 0, NULL);
  960. }
  961. uint8_t UHS_USB_HOST_BASE::TestInterface(ENUMERATION_INFO *ei) {
  962. uint8_t devConfigIndex;
  963. uint8_t rcode = 0;
  964. HOST_DEBUG("TestInterface VID:%4.4x PID:%4.4x Class:%2.2x Subclass:%2.2x Protocol %2.2x\r\n", ei->vid, ei->pid, ei->klass, ei->subklass, ei->protocol);
  965. HOST_DEBUG("Interface data: Class:%2.2x Subclass:%2.2x Protocol %2.2x, number of endpoints %i\r\n", ei->interface.klass, ei->interface.subklass, ei->interface.subklass, ei->interface.numep);
  966. HOST_DEBUG("Parent: %2.2x, bAddress: %2.2x\r\n", ei->parent, ei->address);
  967. for(devConfigIndex = 0; devConfigIndex < UHS_HOST_MAX_INTERFACE_DRIVERS; devConfigIndex++) {
  968. if(!devConfig[devConfigIndex]) {
  969. HOST_DEBUG("No driver at index %i\r\n", devConfigIndex);
  970. continue; // no driver
  971. }
  972. if(devConfig[devConfigIndex]->bAddress) {
  973. HOST_DEBUG("Driver %i is already consumed @ %2.2x\r\n", devConfigIndex, devConfig[devConfigIndex]->bAddress);
  974. continue; // consumed
  975. }
  976. if(devConfig[devConfigIndex]->OKtoEnumerate(ei)) {
  977. HOST_DEBUG("Driver %i supports this interface\r\n", devConfigIndex);
  978. break;
  979. }
  980. }
  981. if(devConfigIndex == UHS_HOST_MAX_INTERFACE_DRIVERS) {
  982. rcode = UHS_HOST_ERROR_DEVICE_NOT_SUPPORTED;
  983. #if 0 // defined(UHS_HID_LOADED)
  984. // Check HID here, if it is, then lie
  985. if(ei->klass == UHS_USB_CLASS_HID) {
  986. devConfigIndex = UHS_HID_INDEX; // for debugging, otherwise this has no use.
  987. rcode = 0;
  988. }
  989. #endif
  990. }
  991. if(!rcode) HOST_DEBUG("Driver %i can be used for this interface\r\n", devConfigIndex);
  992. else HOST_DEBUG("No driver for this interface.\r\n");
  993. return rcode;
  994. };
  995. uint8_t UHS_USB_HOST_BASE::enumerateInterface(ENUMERATION_INFO *ei) {
  996. uint8_t devConfigIndex;
  997. HOST_DEBUG("AttemptConfig: parent = %i, port = %i\r\n", ei->parent, ei->port);
  998. #if 0 // defined(UHS_HID_LOADED)
  999. // Check HID here, if it is, then lie
  1000. if(ei->klass == UHS_USB_CLASS_HID || ei->interface.klass == UHS_USB_CLASS_HID) {
  1001. UHS_HID_SetUSBInterface(this, ENUMERATION_INFO * ei);
  1002. devConfigIndex = UHS_HID_INDEX;
  1003. } else
  1004. #endif
  1005. for(devConfigIndex = 0; devConfigIndex < UHS_HOST_MAX_INTERFACE_DRIVERS; devConfigIndex++) {
  1006. if(!devConfig[devConfigIndex]) {
  1007. HOST_DEBUG("No driver at index %i\r\n", devConfigIndex);
  1008. continue; // no driver
  1009. }
  1010. if(devConfig[devConfigIndex]->bAddress) {
  1011. HOST_DEBUG("Driver %i is already consumed @ %2.2x\r\n", devConfigIndex, devConfig[devConfigIndex]->bAddress);
  1012. continue; // consumed
  1013. }
  1014. if(devConfig[devConfigIndex]->OKtoEnumerate(ei)) {
  1015. HOST_DEBUG("Driver %i supports this interface\r\n", devConfigIndex);
  1016. if(!devConfig[devConfigIndex]->SetInterface(ei)) break;
  1017. else devConfigIndex = UHS_HOST_MAX_INTERFACE_DRIVERS;
  1018. }
  1019. }
  1020. return devConfigIndex;
  1021. };
  1022. ////////////////////////////////////////////////////////////////////////////////
  1023. // Vendor Specific Interface Class
  1024. ////////////////////////////////////////////////////////////////////////////////
  1025. #if 0
  1026. /**
  1027. * Might go away, depends on if it is useful, or not.
  1028. *
  1029. * @param ei Enumeration information
  1030. * @return true if this interface driver can handle this interface description
  1031. */
  1032. bool UHS_NI UHS_VSI::OKtoEnumerate(ENUMERATION_INFO *ei) {
  1033. return (
  1034. (ei->subklass == UHS_USB_CLASS_VENDOR_SPECIFIC) ||
  1035. (ei->interface.subklass == UHS_USB_CLASS_VENDOR_SPECIFIC)
  1036. );
  1037. }
  1038. /**
  1039. * Copy the entire ENUMERATION_INFO structure
  1040. * @param ei Enumeration information
  1041. * @return 0
  1042. */
  1043. uint8_t UHS_NI UHS_VSI::SetInterface(ENUMERATION_INFO *ei) {
  1044. bNumEP = 1;
  1045. bAddress = ei->address;
  1046. eInfo.address = ei->address;
  1047. eInfo.bMaxPacketSize0 = ei->bMaxPacketSize0;
  1048. eInfo.currentconfig = ei->currentconfig;
  1049. eInfo.interface.bAlternateSetting = ei->interface.bAlternateSetting;
  1050. eInfo.interface.bInterfaceNumber = ei->interface.bInterfaceNumber;
  1051. eInfo.interface.numep = ei->interface.numep;
  1052. eInfo.interface.protocol = ei->interface.protocol;
  1053. eInfo.interface.subklass = ei->interface.subklass;
  1054. eInfo.klass = ei->klass;
  1055. eInfo.parent = ei->parent;
  1056. eInfo.pid = ei->pid;
  1057. eInfo.port = ei->port;
  1058. eInfo.protocol = ei->protocol;
  1059. eInfo.subklass = ei->subklass;
  1060. eInfo.vid = ei->vid;
  1061. for(uint8_t i = 0; i < eInfo.interface.numep; i++) {
  1062. eInfo.interface.epInfo[i].bEndpointAddress = ei->interface.epInfo[i].bEndpointAddress;
  1063. eInfo.interface.epInfo[i].bInterval = ei->interface.epInfo[i].bInterval;
  1064. eInfo.interface.epInfo[i].bmAttributes = ei->interface.epInfo[i].bmAttributes;
  1065. eInfo.interface.epInfo[i].wMaxPacketSize = ei->interface.epInfo[i].wMaxPacketSize;
  1066. }
  1067. return 0;
  1068. }
  1069. #endif
  1070. ////////////////////////////////////////////////////////////////////////////////
  1071. ////////////////////////////////////////////////////////////////////////////////
  1072. ////////////////////////////////////////////////////////////////////////////////
  1073. #if 0
  1074. /* TO-DO: Move this silliness to a NONE driver.
  1075. * When we have a generic NONE driver we can:
  1076. * o Extract ALL device information to help users with a new device.
  1077. * o Use an unknown device from a sketch, kind of like usblib does.
  1078. * This will aid in making more drivers in a faster way.
  1079. */
  1080. uint8_t UHS_USB_HOST_BASE::DefaultAddressing(uint8_t parent, uint8_t port, uint8_t speed) {
  1081. uint8_t rcode;
  1082. UHS_Device *p0 = NULL, *p = NULL;
  1083. // Get pointer to pseudo device with address 0 assigned
  1084. p0 = addrPool.GetUsbDevicePtr(0);
  1085. if(!p0)
  1086. return UHS_HOST_ERROR_NO_ADDRESS_IN_POOL;
  1087. if(!p0->epinfo)
  1088. return UHS_HOST_ERROR_NULL_EPINFO;
  1089. p0->speed = speed;
  1090. // Allocate new address according to device class
  1091. uint8_t bAddress = addrPool.AllocAddress(parent, false, port);
  1092. if(!bAddress)
  1093. return UHS_HOST_ERROR_ADDRESS_POOL_FULL;
  1094. p = addrPool.GetUsbDevicePtr(bAddress);
  1095. if(!p)
  1096. return UHS_HOST_ERROR_NO_ADDRESS_IN_POOL;
  1097. p->speed = speed;
  1098. // Assign new address to the device
  1099. rcode = setAddr(0, bAddress);
  1100. if(rcode) {
  1101. addrPool.FreeAddress(bAddress);
  1102. bAddress = 0;
  1103. return rcode;
  1104. }
  1105. return 0;
  1106. }
  1107. #endif
  1108. #else
  1109. #error "Never include UHS_host_INLINE.h, include UHS_host.h instead"
  1110. #endif