Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #include <Arduino.h>
  25. #include "USBHost_t36.h" // Read this header first for key info
  26. #define print USBHost::print_
  27. #define println USBHost::println_
  28. #define ADK_VID 0x18D1
  29. #define ADK_PID 0x2D00
  30. #define ADB_PID 0x2D01
  31. #define USB_SETUP_DEVICE_TO_HOST 0x80
  32. #define USB_SETUP_HOST_TO_DEVICE 0x00
  33. #define USB_SETUP_TYPE_VENDOR 0x40
  34. #define USB_SETUP_RECIPIENT_DEVICE 0x00
  35. #define UHS_ADK_bmREQ_GET USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_VENDOR|USB_SETUP_RECIPIENT_DEVICE
  36. #define UHS_ADK_bmREQ_SEND USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_VENDOR|USB_SETUP_RECIPIENT_DEVICE
  37. #define UHS_ADK_GETPROTO 51 // check USB accessory protocol version
  38. #define UHS_ADK_SENDSTR 52 // send identifying string
  39. #define UHS_ADK_ACCSTART 53 // start device in accessory mode
  40. #define UHS_ADK_ID_MANUFACTURER 0
  41. #define UHS_ADK_ID_MODEL 1
  42. #define UHS_ADK_ID_DESCRIPTION 2
  43. #define UHS_ADK_ID_VERSION 3
  44. #define UHS_ADK_ID_URI 4
  45. #define UHS_ADK_ID_SERIAL 5
  46. static uint8_t adkbuf[256] __attribute__ ((aligned(16)));
  47. static setup_t adksetup __attribute__ ((aligned(16)));
  48. /************************************************************/
  49. // Initialization and claiming of devices & interfaces
  50. /************************************************************/
  51. void ADK::init()
  52. {
  53. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  54. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  55. rx_head = 0;
  56. rx_tail = 0;
  57. driver_ready_for_device(this);
  58. state = 0;
  59. }
  60. bool ADK::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  61. {
  62. // only claim at interface level
  63. if (type != 1)
  64. return false;
  65. // Check for ADK or ADB PIDs
  66. if (dev->idVendor == ADK_VID && (dev->idProduct == ADK_PID || dev->idProduct == ADB_PID))
  67. {
  68. const uint8_t *p = descriptors;
  69. const uint8_t *end = p + len;
  70. // Interface descriptor
  71. if (p[0] != 9 || p[1] != 4)
  72. return false;
  73. // bInterfaceClass: 255 Vendor Specific
  74. if (p[5] != 255)
  75. {
  76. return false;
  77. }
  78. // bInterfaceSubClass: 255
  79. if (p[6] != 255)
  80. {
  81. return false;
  82. }
  83. println("ADK claim this=", (uint32_t)this, HEX);
  84. print("vid=", dev->idVendor, HEX);
  85. print(", pid=", dev->idProduct, HEX);
  86. print(", bDeviceClass = ", dev->bDeviceClass);
  87. print(", bDeviceSubClass = ", dev->bDeviceSubClass);
  88. println(", bDeviceProtocol = ", dev->bDeviceProtocol);
  89. println(" bInterfaceClass=", p[5]);
  90. println(" bInterfaceSubClass=", p[6]);
  91. print_hexbytes(descriptors, len);
  92. p += 9;
  93. rx_ep = 0;
  94. tx_ep = 0;
  95. while (p < end)
  96. {
  97. len = *p;
  98. if (len < 4)
  99. return false; // all desc are at least 4 bytes
  100. if (p + len > end)
  101. return false; // reject if beyond end of data
  102. uint32_t type = p[1];
  103. if (type == 5)
  104. {
  105. // endpoint descriptor
  106. if (p[0] < 7)
  107. return false; // at least 7 bytes
  108. if (p[3] != 2)
  109. return false; // must be bulk type
  110. println(" Endpoint: ", p[2], HEX);
  111. switch (p[2] & 0xF0)
  112. {
  113. case 0x80:
  114. // IN endpoint
  115. if (rx_ep == 0)
  116. {
  117. rx_ep = p[2] & 0x0F;
  118. rx_size = p[4] | (p[5] << 8);
  119. println(" rx_size = ", rx_size);
  120. println(" rx_ep = ", rx_ep);
  121. }
  122. break;
  123. case 0x00:
  124. // OUT endpoint
  125. if (tx_ep == 0)
  126. {
  127. tx_ep = p[2];
  128. tx_size = p[4] | (p[5] << 8);
  129. println(" tx_size = ", tx_size);
  130. println(" tx_ep = ", tx_ep);
  131. }
  132. break;
  133. default:
  134. return false;
  135. }
  136. }
  137. // ADK uses the first two endpoints for communication
  138. if (rx_ep && tx_ep)
  139. {
  140. println("Found both rx and tx EPs");
  141. break;
  142. }
  143. p += len;
  144. }
  145. // if an IN endpoint was found, create its pipe
  146. if (rx_ep && rx_size <= MAX_PACKET_SIZE)
  147. {
  148. rxpipe = new_Pipe(dev, 2, rx_ep, 1, rx_size);
  149. if (rxpipe)
  150. {
  151. rxpipe->callback_function = rx_callback;
  152. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  153. rx_packet_queued = true;
  154. println("Done creating RX pipe");
  155. }
  156. }
  157. else
  158. {
  159. rxpipe = NULL;
  160. }
  161. // if an OUT endpoint was found, create its pipe
  162. if (tx_ep && tx_size <= MAX_PACKET_SIZE)
  163. {
  164. txpipe = new_Pipe(dev, 2, tx_ep, 0, tx_size);
  165. if (txpipe)
  166. {
  167. txpipe->callback_function = tx_callback;
  168. println("Done creating TX pipe");
  169. }
  170. }
  171. else
  172. {
  173. txpipe = NULL;
  174. }
  175. rx_head = 0;
  176. rx_tail = 0;
  177. // claim if either pipe created
  178. bool created = (rxpipe || txpipe);
  179. if (created)
  180. {
  181. println("Done with init.");
  182. state = 8;
  183. }
  184. return created;
  185. }
  186. else
  187. {
  188. state = 0;
  189. println("Not in accessory mode.");
  190. // Kick off switch to Accessory Mode
  191. mk_setup(adksetup, UHS_ADK_bmREQ_GET, UHS_ADK_GETPROTO, 0, 0, 2);
  192. queue_Control_Transfer(dev, &adksetup, adkbuf, this);
  193. return true;
  194. }
  195. return false;
  196. }
  197. void ADK::sendStr(Device_t *dev, uint8_t index, char *str)
  198. {
  199. strcpy(adkbuf, (unsigned char*)str);
  200. mk_setup(adksetup, UHS_ADK_bmREQ_SEND, UHS_ADK_SENDSTR, 0, index, strlen(str));
  201. queue_Control_Transfer(dev, &adksetup, adkbuf, this);
  202. }
  203. void ADK::control(const Transfer_t *transfer)
  204. {
  205. println("Control callback state=",state);
  206. switch (state)
  207. {
  208. case 0:
  209. print_hexbytes(transfer->buffer, transfer->length);
  210. state++;
  211. sendStr(device, UHS_ADK_ID_MANUFACTURER, manufacturer);
  212. break;
  213. case 1:
  214. print_hexbytes(transfer->buffer, transfer->length);
  215. state++;
  216. sendStr(device, UHS_ADK_ID_MODEL, model);
  217. break;
  218. case 2:
  219. print_hexbytes(transfer->buffer, transfer->length);
  220. state++;
  221. sendStr(device, UHS_ADK_ID_DESCRIPTION, desc);
  222. break;
  223. case 3:
  224. print_hexbytes(transfer->buffer, transfer->length);
  225. state++;
  226. sendStr(device, UHS_ADK_ID_VERSION, version);
  227. break;
  228. case 4:
  229. print_hexbytes(transfer->buffer, transfer->length);
  230. state++;
  231. sendStr(device, UHS_ADK_ID_URI, uri);
  232. break;
  233. case 5:
  234. print_hexbytes(transfer->buffer, transfer->length);
  235. state++;
  236. sendStr(device, UHS_ADK_ID_SERIAL, serial);
  237. break;
  238. case 6:
  239. print_hexbytes(transfer->buffer, transfer->length);
  240. state++;
  241. // Send ADK switch command
  242. mk_setup(adksetup, UHS_ADK_bmREQ_SEND, UHS_ADK_ACCSTART, 0, 0, 0);
  243. queue_Control_Transfer(device, &adksetup, NULL, this);
  244. break;
  245. case 7:
  246. println("After ACC switch command. Device should re-enumerate.");
  247. print_hexbytes(transfer->buffer, transfer->length);
  248. state++;
  249. break;
  250. default:
  251. break;
  252. }
  253. }
  254. void ADK::rx_callback(const Transfer_t *transfer)
  255. {
  256. if (transfer->driver) {
  257. ((ADK *)(transfer->driver))->rx_data(transfer);
  258. }
  259. }
  260. void ADK::tx_callback(const Transfer_t *transfer)
  261. {
  262. if (transfer->driver) {
  263. ((ADK *)(transfer->driver))->tx_data(transfer);
  264. }
  265. }
  266. void ADK::rx_data(const Transfer_t *transfer)
  267. {
  268. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  269. println("ADK Receive rx_data");
  270. print("Len: ");
  271. print(len);
  272. print(" Data: ");
  273. print_hexbytes(transfer->buffer, len);
  274. uint32_t head = rx_head;
  275. uint8_t *p = (const uint8_t *)transfer->buffer;
  276. if (p != NULL && len != 0)
  277. {
  278. do
  279. {
  280. if (++head >= RX_QUEUE_SIZE)
  281. head = 0;
  282. rx_queue[head] = *p++;
  283. } while (--len);
  284. }
  285. rx_head = head;
  286. rx_packet_queued = false;
  287. rx_queue_packets(rx_head, rx_tail);
  288. }
  289. void ADK::rx_queue_packets(uint32_t head, uint32_t tail)
  290. {
  291. if (rx_packet_queued)
  292. return;
  293. uint32_t avail = (head < tail) ? tail - head - 1 : RX_QUEUE_SIZE - 1 - head + tail;
  294. println("rx_size = ", rx_size);
  295. println("avail = ", avail);
  296. if (avail >= rx_size)
  297. {
  298. // enough space to accept another full packet
  299. println("queue another receive packet");
  300. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  301. rx_packet_queued = true;
  302. } else {
  303. // queue can't accept another packet's data, so leave
  304. // the data waiting on the device until we can accept it
  305. println("wait to receive more packets");
  306. rx_packet_queued = false;
  307. }
  308. }
  309. void ADK::tx_data(const Transfer_t *transfer)
  310. {
  311. println("ADK tx_data transmit complete");
  312. print(" Data: ");
  313. print_hexbytes(transfer->buffer, tx_size);
  314. }
  315. void ADK::disconnect()
  316. {
  317. println("Disconnect");
  318. rxpipe = NULL;
  319. txpipe = NULL;
  320. state = 0;
  321. }
  322. void ADK::begin(char *adk_manufacturer, char *adk_model, char *adk_desc, char *adk_version, char *adk_uri, char *adk_serial)
  323. {
  324. manufacturer = adk_manufacturer;
  325. model = adk_model;
  326. desc = adk_desc;
  327. version = adk_version;
  328. uri = adk_uri;
  329. serial = adk_serial;
  330. }
  331. void ADK::end(void)
  332. {
  333. // TODO: add end code
  334. }
  335. int ADK::available(void)
  336. {
  337. if (!device)
  338. return 0;
  339. uint32_t head = rx_head;
  340. uint32_t tail = rx_tail;
  341. if (head >= tail)
  342. return head - tail;
  343. return RX_QUEUE_SIZE + head - tail;
  344. }
  345. int ADK::peek(void)
  346. {
  347. if (!device)
  348. return -1;
  349. uint32_t head = rx_head;
  350. uint32_t tail = rx_tail;
  351. if (head == tail)
  352. return -1;
  353. if (++tail >= RX_QUEUE_SIZE)
  354. tail = 0;
  355. return rx_queue[tail];
  356. }
  357. int ADK::read(void)
  358. {
  359. if (!device)
  360. return -1;
  361. uint32_t head = rx_head;
  362. uint32_t tail = rx_tail;
  363. if (head == tail)
  364. return -1;
  365. if (++tail >= RX_QUEUE_SIZE)
  366. tail = 0;
  367. int c = rx_queue[tail];
  368. rx_tail = tail;
  369. rx_queue_packets(head, tail);
  370. return c;
  371. }
  372. size_t ADK::write(size_t len, uint8_t *buf)
  373. {
  374. memcpy(tx_buffer, buf, len);
  375. __disable_irq();
  376. queue_Data_Transfer(txpipe, tx_buffer, len, this);
  377. __enable_irq();
  378. return len;
  379. }
  380. bool ADK::ready()
  381. {
  382. if (state > 7)
  383. return true;
  384. return false;
  385. }