Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

454 lines
10KB

  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=%i",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. uint32_t tail = rx_tail;
  276. for (uint32_t i=0; i < ceil(len/4); i++)
  277. {
  278. uint32_t msg = rx_buffer[i];
  279. if (msg) {
  280. if (++head >= RX_QUEUE_SIZE)
  281. head = 0;
  282. rx_queue[head] = msg;
  283. }
  284. }
  285. rx_head = head;
  286. rx_packet_queued = false;
  287. rx_queue_packets(head, 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 >= (uint32_t)(rx_size>>2)) {
  297. // enough space to accept another full packet
  298. println("queue another receive packet");
  299. queue_Data_Transfer(rxpipe, rx_buffer, rx_size, this);
  300. rx_packet_queued = true;
  301. } else {
  302. // queue can't accept another packet's data, so leave
  303. // the data waiting on the device until we can accept it
  304. println("wait to receive more packets");
  305. rx_packet_queued = false;
  306. }
  307. }
  308. void ADK::tx_data(const Transfer_t *transfer)
  309. {
  310. println("ADK tx_data transmit complete");
  311. print(" Data: ");
  312. print_hexbytes(transfer->buffer, tx_size);
  313. }
  314. void ADK::disconnect()
  315. {
  316. println("Disconnect");
  317. rxpipe = NULL;
  318. txpipe = NULL;
  319. state = 0;
  320. }
  321. void ADK::begin(char *adk_manufacturer, char *adk_model, char *adk_desc, char *adk_version, char *adk_uri, char *adk_serial)
  322. {
  323. manufacturer = adk_manufacturer;
  324. model = adk_model;
  325. desc = adk_desc;
  326. version = adk_version;
  327. uri = adk_uri;
  328. serial = adk_serial;
  329. }
  330. void ADK::end(void)
  331. {
  332. // TODO: add end code
  333. }
  334. int ADK::available(void)
  335. {
  336. if (!device)
  337. return 0;
  338. uint32_t head = rx_head;
  339. uint32_t tail = rx_tail;
  340. if (head >= tail)
  341. return head - tail;
  342. return RX_QUEUE_SIZE + head - tail;
  343. }
  344. int ADK::peek(void)
  345. {
  346. if (!device)
  347. return -1;
  348. uint32_t head = rx_head;
  349. uint32_t tail = rx_tail;
  350. if (head == tail)
  351. return -1;
  352. if (++tail >= RX_QUEUE_SIZE)
  353. tail = 0;
  354. return rx_queue[tail];
  355. }
  356. int ADK::read(void)
  357. {
  358. if (!device)
  359. return -1;
  360. uint32_t head = rx_head;
  361. uint32_t tail = rx_tail;
  362. if (head == tail)
  363. return -1;
  364. if (++tail >= RX_QUEUE_SIZE)
  365. tail = 0;
  366. int c = rx_queue[tail];
  367. rx_tail = tail;
  368. rx_queue_packets(head, tail);
  369. return c;
  370. }
  371. size_t ADK::write(size_t len, uint8_t *buf)
  372. {
  373. memcpy(tx_buffer, buf, len);
  374. __disable_irq();
  375. queue_Data_Transfer(txpipe, tx_buffer, len, this);
  376. __enable_irq();
  377. return len;
  378. }
  379. bool ADK::ready()
  380. {
  381. if (state > 7)
  382. return true;
  383. return false;
  384. }