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.

358 lines
12KB

  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. * information about the BlueTooth HCI comes from logic analyzer captures
  24. * plus... http://affon.narod.ru/BT/bluetooth_app_c10.pdf
  25. */
  26. #include <Arduino.h>
  27. #include "USBHost_t36.h" // Read this header first for key info
  28. #define print USBHost::print_
  29. #define println USBHost::println_
  30. /************************************************************/
  31. // Define mapping VID/PID - to Serial Device type.
  32. /************************************************************/
  33. /************************************************************/
  34. // Initialization and claiming of devices & interfaces
  35. /************************************************************/
  36. void BluetoothController::init()
  37. {
  38. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  39. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  40. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  41. driver_ready_for_device(this);
  42. }
  43. bool BluetoothController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  44. {
  45. // only claim at device level
  46. println("BluetoothController claim this=", (uint32_t)this, HEX);
  47. // Lets try to support the main USB Bluetooth class...
  48. // http://www.usb.org/developers/defined_class/#BaseClassE0h
  49. if (dev->bDeviceClass != 0xe0) return false; // not base class wireless controller
  50. if ((dev->bDeviceSubClass != 1) || (dev->bDeviceProtocol != 1)) return false; // Bluetooth Programming Interface
  51. if (type != 0) return false;
  52. Serial.printf("BluetoothController claim this=%x vid:pid=%x:%x\n ", (uint32_t)this, dev->idVendor, dev->idProduct);
  53. for (uint8_t i=0; i < len; i++) Serial.printf("%x ", descriptors[i]);
  54. Serial.printf("\n");
  55. // Lets try to process the first Interface and get the end points...
  56. // Some common stuff for both XBoxs
  57. uint32_t count_end_points = descriptors[4];
  58. if (count_end_points < 2) return false;
  59. uint32_t rxep = 0;
  60. uint32_t rx2ep = 0;
  61. uint32_t txep = 0;
  62. uint8_t rx_interval = 0;
  63. uint8_t rx2_interval = 0;
  64. uint8_t tx_interval = 0;
  65. rx_size_ = 0;
  66. rx2_size_ = 0;
  67. tx_size_ = 0;
  68. uint32_t descriptor_index = 9;
  69. while (count_end_points-- && ((rxep == 0) || txep == 0)) {
  70. if (descriptors[descriptor_index] != 7) return false; // length 7
  71. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  72. if ((descriptors[descriptor_index+4] <= 64)
  73. && (descriptors[descriptor_index+5] == 0)) {
  74. // have a bulk EP size
  75. if (descriptors[descriptor_index+2] & 0x80 ) {
  76. if (descriptors[descriptor_index+3] == 3) { // Interrupt
  77. rxep = descriptors[descriptor_index+2];
  78. rx_size_ = descriptors[descriptor_index+4];
  79. rx_interval = descriptors[descriptor_index+6];
  80. } else if (descriptors[descriptor_index+3] == 2) { // bulk
  81. rx2ep = descriptors[descriptor_index+2];
  82. rx2_size_ = descriptors[descriptor_index+4];
  83. rx2_interval = descriptors[descriptor_index+6];
  84. }
  85. } else {
  86. txep = descriptors[descriptor_index+2];
  87. tx_size_ = descriptors[descriptor_index+4];
  88. tx_interval = descriptors[descriptor_index+6];
  89. }
  90. }
  91. descriptor_index += 7; // setup to look at next one...
  92. }
  93. if ((rxep == 0) || (txep == 0)) {
  94. Serial.printf("Bluetooth end points not found: %d %d\n", rxep, txep);
  95. return false; // did not find two end points.
  96. }
  97. Serial.printf(" rxep=%d(%d) txep=%d(%d)\n", rxep&15, rx_size_, txep, tx_size_);
  98. print("BluetoothController, rxep=", rxep & 15);
  99. print("(", rx_size_);
  100. print("), txep=", txep);
  101. print("(", tx_size_);
  102. println(")");
  103. rxpipe_ = new_Pipe(dev, 3, rxep & 15, 1, rx_size_, rx_interval);
  104. if (!rxpipe_) return false;
  105. txpipe_ = new_Pipe(dev, 3, txep, 0, tx_size_, tx_interval);
  106. if (!txpipe_) {
  107. //free_Pipe(rxpipe_);
  108. return false;
  109. }
  110. rxpipe_->callback_function = rx_callback;
  111. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  112. txpipe_->callback_function = tx_callback;
  113. // Send out the reset
  114. device = dev; // yes this is normally done on return from this but should not hurt if we do it here.
  115. sendResetHCI();
  116. pending_control_ = 1; // not sure yet on what we need...
  117. return true;
  118. }
  119. void BluetoothController::disconnect()
  120. {
  121. }
  122. void BluetoothController::control(const Transfer_t *transfer)
  123. {
  124. println("control callback (bluetooth) ", pending_control_, HEX);
  125. Serial.printf("control callback (bluetooth): %d : ", pending_control_);
  126. uint8_t *buffer = (uint8_t*)transfer->buffer;
  127. for (uint8_t i=0; i < transfer->length; i++) Serial.printf("%x ", buffer[i]);
  128. Serial.printf("\n");
  129. }
  130. /************************************************************/
  131. // Interrupt-based Data Movement
  132. /************************************************************/
  133. void BluetoothController::rx_callback(const Transfer_t *transfer)
  134. {
  135. if (!transfer->driver) return;
  136. ((BluetoothController *)(transfer->driver))->rx_data(transfer);
  137. }
  138. void BluetoothController::tx_callback(const Transfer_t *transfer)
  139. {
  140. if (!transfer->driver) return;
  141. ((BluetoothController *)(transfer->driver))->tx_data(transfer);
  142. }
  143. void BluetoothController::rx_data(const Transfer_t *transfer)
  144. {
  145. uint32_t len = transfer->length - ((transfer->qtd.token >> 16) & 0x7FFF);
  146. print_hexbytes((uint8_t*)transfer->buffer, len);
  147. Serial.printf("BT rx_data(%d): ", len);
  148. uint8_t *buffer = (uint8_t*)transfer->buffer;
  149. for (uint8_t i=0; i < len; i++) Serial.printf("%x ", buffer[i]);
  150. Serial.printf("\n");
  151. switch(buffer[0]) { // Switch on event type
  152. case EV_COMMAND_COMPLETE:
  153. if(!buffer[5]) { // Check if command succeeded
  154. Serial.printf(" Command Completed! \n");
  155. break;
  156. }
  157. }
  158. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  159. if (rx_packet_data_remaining == 0) {
  160. rx_packet_data_remaining = buffer[1] + 2; // length of data plus the two bytes at start...
  161. }
  162. rx_packet_data_remaining -= len; // remove the length of this packet from length
  163. switch (pending_control_) {
  164. case 1:
  165. {
  166. static uint8_t HCI_CMD3_10[] = {3, 0x10, 0}; // OCF=3, OGF=4<<2, Parameters=0 ???
  167. sendHCICommand(HCI_CMD3_10, sizeof(HCI_CMD3_10)); // Read local supported features. ?? look up
  168. pending_control_++;
  169. }
  170. break;
  171. case 2:
  172. sendHCIReadLocalVersionInfo();
  173. pending_control_++;
  174. break;
  175. case 3:
  176. hciVersion = buffer[6]; // Should do error checking above...
  177. Serial.printf(" Local Version: %x\n", hciVersion);
  178. sendHCIReadBDAddr();
  179. pending_control_++;
  180. break;
  181. case 4:
  182. Serial.printf(" BD Addr");
  183. for(uint8_t i = 0; i < 6; i++) {
  184. my_bdaddr[i] = buffer[6 + i];
  185. Serial.printf(":%x", my_bdaddr[i]);
  186. }
  187. Serial.printf("\n");
  188. sendHCIReadBufferSize();
  189. pending_control_++;
  190. break;
  191. case 5:
  192. sendHCIReadClassOfDevice();
  193. pending_control_++;
  194. break;
  195. case 6:
  196. sendHCIReadLocalName();
  197. pending_control_++;
  198. break;
  199. case 7:
  200. // received name back... Not sure yet if we received
  201. // full name or just start of it...
  202. Serial.printf(" Local name: ");
  203. for (uint8_t i=6; i < len; i++) {
  204. if (buffer[i] == 0) {
  205. break;
  206. }
  207. Serial.printf("%c", buffer[i]);
  208. }
  209. Serial.printf("\n");
  210. if (rx_packet_data_remaining) {
  211. pending_control_++; // go to next state
  212. } else {
  213. pending_control_ += 2;
  214. sendHCIReadVoiceSettings();
  215. }
  216. break;
  217. case 8:
  218. Serial.printf(" Local name continue: ");
  219. for (uint8_t i=0; i < len; i++) {
  220. if (buffer[i] == 0) {
  221. Serial.printf("\n");
  222. break;
  223. }
  224. Serial.printf("%c", buffer[i]);
  225. }
  226. if (rx_packet_data_remaining == 0) {
  227. sendHCIReadVoiceSettings();
  228. pending_control_++;
  229. }
  230. break;
  231. case 9:
  232. SendHCICommandReadNumberSupportedIAC();
  233. pending_control_++;
  234. break;
  235. case 10:
  236. SendHCICommandReadCurrentIACLAP();
  237. pending_control_++;
  238. break;
  239. case 11:
  240. sendHCIClearAllEventFilters();
  241. pending_control_++;
  242. break;
  243. case 12:
  244. sendHCIWriteConnectionAcceptTimeout();
  245. pending_control_++;
  246. break;
  247. }
  248. }
  249. void BluetoothController::tx_data(const Transfer_t *transfer)
  250. {
  251. }
  252. void inline BluetoothController::sendHCICommand(uint8_t* data, uint16_t nbytes)
  253. {
  254. mk_setup(setup, 0x20, 0x0, 0, 0, nbytes);
  255. queue_Control_Transfer(device, &setup, data, this);
  256. }
  257. void BluetoothController::sendResetHCI() {
  258. Serial.printf("HCI_RESET called\n");
  259. static uint8_t HCI_RESET[] = {3, 0xc, 0}; // OCF=3, OGF=3<<2, Parameters=0
  260. sendHCICommand(HCI_RESET, sizeof(HCI_RESET));
  261. }
  262. void BluetoothController::sendHCIClearAllEventFilters() {
  263. Serial.printf("HCI_Set_Event_Filter_Clear called\n");
  264. static uint8_t HCI_Set_Event_Filter_Clear[] = {5, 0xc, 1, 0}; // OCF=5, OGF=3<<2, Parameters=1, 0
  265. sendHCICommand(HCI_Set_Event_Filter_Clear, sizeof(HCI_Set_Event_Filter_Clear));
  266. }
  267. void BluetoothController::sendHCIReadLocalName() {
  268. Serial.printf("HCI_Read_Local_Name called\n");
  269. static uint8_t HCI_Read_Local_Name[] = {0x14, 0xc, 0}; // OCF=14, OGF=3<<2, Parameters=0
  270. sendHCICommand(HCI_Read_Local_Name, sizeof(HCI_Read_Local_Name));
  271. }
  272. void BluetoothController::sendHCIWriteConnectionAcceptTimeout() {
  273. // BUGBUG: Hard coded timeout here...
  274. Serial.printf("Write_Connection_Accept_Timeout called\n");
  275. static uint8_t Write_Connection_Accept_Timeout[] = {0x16, 0xc, 2, 0, 0x7d}; // OCF=16, OGF=3<<2, Parameters=2, 0, 7d
  276. sendHCICommand(Write_Connection_Accept_Timeout, sizeof(Write_Connection_Accept_Timeout));
  277. }
  278. void BluetoothController::sendHCIReadClassOfDevice() {
  279. Serial.printf("HCI_READ_CLASS_OF_DEVICE called\n");
  280. static uint8_t HCI_READ_CLASS_OF_DEVICE[] = {0x23, 0xc, 0}; // OCF=23, OGF=3<<2, Parameters=0
  281. sendHCICommand(HCI_READ_CLASS_OF_DEVICE, sizeof(HCI_READ_CLASS_OF_DEVICE));
  282. }
  283. void BluetoothController::sendHCIReadVoiceSettings() {
  284. Serial.printf("HCI_Read_Voice_Setting called\n");
  285. static uint8_t HCI_Read_Voice_Setting[] = {0x25, 0xc, 0}; // OCF=25, OGF=3<<2, Parameters=0
  286. sendHCICommand(HCI_Read_Voice_Setting, sizeof(HCI_Read_Voice_Setting));
  287. }
  288. void BluetoothController::SendHCICommandReadNumberSupportedIAC() {
  289. Serial.printf("HCI_Read_Number_Of_Supported_IAC\n");
  290. static uint8_t HCI_Read_Number_Of_Supported_IAC[] = {0x38, 0xc, 0}; // OCF=38, OGF=3<<2, Parameters=0
  291. sendHCICommand(HCI_Read_Number_Of_Supported_IAC, sizeof(HCI_Read_Number_Of_Supported_IAC));
  292. }
  293. void BluetoothController::SendHCICommandReadCurrentIACLAP() {
  294. Serial.printf("HCI_Read_Current_IAC_LAP called\n");
  295. static uint8_t HCI_Read_Current_IAC_LAP[] = {0x39, 0xc, 0}; // OCF=39, OGF=3<<2, Parameters=0
  296. sendHCICommand(HCI_Read_Current_IAC_LAP, sizeof(HCI_Read_Current_IAC_LAP));
  297. }
  298. void BluetoothController::sendHCIReadBufferSize() {
  299. Serial.printf("HCI_Read_Buffer_Size called\n");
  300. static uint8_t HCI_Read_Buffer_Size[] = {5, 0x10, 0}; // OCF=5, OGF=4<<2, Parameters=0 ???
  301. sendHCICommand(HCI_Read_Buffer_Size, sizeof(HCI_Read_Buffer_Size));
  302. }
  303. void BluetoothController::sendHCIReadBDAddr() {
  304. Serial.printf("HCI_Read_BD_ADDR called\n");
  305. static uint8_t HCI_Read_BD_ADDR[] = {9, 0x10, 0}; // OCF=9, OGF=4<<2, Parameters=0 ???
  306. sendHCICommand(HCI_Read_BD_ADDR, sizeof(HCI_Read_BD_ADDR));
  307. }
  308. void BluetoothController::sendHCIReadLocalVersionInfo() {
  309. Serial.printf("HCI_Read_Local_Version_Information called\n");
  310. static uint8_t HCI_Read_Local_Version_Information[] = {1, 0x10, 0}; // OCF=1, OGF=4<<2, Parameters=0 ???
  311. sendHCICommand(HCI_Read_Local_Version_Information, sizeof(HCI_Read_Local_Version_Information));
  312. }