No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

848 líneas
28KB

  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. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. #define print USBHost::print_
  26. #define println USBHost::println_
  27. #define DEBUG_JOYSTICK
  28. #ifdef DEBUG_JOYSTICK
  29. #define DBGPrintf USBHDBGSerial.printf
  30. #else
  31. #define DBGPrintf(...)
  32. #endif
  33. // PID/VID to joystick mapping - Only the XBOXOne is used to claim the USB interface directly,
  34. // The others are used after claim-hid code to know which one we have and to use it for
  35. // doing other features.
  36. JoystickController::product_vendor_mapping_t JoystickController::pid_vid_mapping[] = {
  37. { 0x045e, 0x02ea, XBOXONE, false },{ 0x045e, 0x02dd, XBOXONE, false },
  38. { 0x045e, 0x0719, XBOX360, false},
  39. { 0x054C, 0x0268, PS3, true},
  40. { 0x054C, 0x05C4, PS4, true}, {0x054C, 0x09CC, PS4, true }
  41. };
  42. //-----------------------------------------------------------------------------
  43. void JoystickController::init()
  44. {
  45. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  46. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  47. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  48. driver_ready_for_device(this);
  49. USBHIDParser::driver_ready_for_hid_collection(this);
  50. BluetoothController::driver_ready_for_bluetooth(this);
  51. }
  52. //-----------------------------------------------------------------------------
  53. JoystickController::joytype_t JoystickController::mapVIDPIDtoJoystickType(uint16_t idVendor, uint16_t idProduct, bool exclude_hid_devices)
  54. {
  55. for (uint8_t i = 0; i < (sizeof(pid_vid_mapping)/sizeof(pid_vid_mapping[0])); i++) {
  56. if ((idVendor == pid_vid_mapping[i].idVendor) && (idProduct == pid_vid_mapping[i].idProduct)) {
  57. println("Match PID/VID: ", i, DEC);
  58. if (exclude_hid_devices && pid_vid_mapping[i].hidDevice) return UNKNOWN;
  59. return pid_vid_mapping[i].joyType;
  60. }
  61. }
  62. return UNKNOWN; // Not in our list
  63. }
  64. //*****************************************************************************
  65. // Some simple query functions depend on which interface we are using...
  66. //*****************************************************************************
  67. uint16_t JoystickController::idVendor()
  68. {
  69. if (device != nullptr) return device->idVendor;
  70. if (mydevice != nullptr) return mydevice->idVendor;
  71. return 0;
  72. }
  73. uint16_t JoystickController::idProduct()
  74. {
  75. if (device != nullptr) return device->idProduct;
  76. if (mydevice != nullptr) return mydevice->idProduct;
  77. return 0;
  78. }
  79. const uint8_t *JoystickController::manufacturer()
  80. {
  81. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  82. //if ((btdevice != nullptr) && (btdevice->strbuf != nullptr)) return &btdevice->strbuf->buffer[btdevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  83. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  84. return nullptr;
  85. }
  86. const uint8_t *JoystickController::product()
  87. {
  88. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  89. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  90. return nullptr;
  91. }
  92. const uint8_t *JoystickController::serialNumber()
  93. {
  94. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  95. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  96. return nullptr;
  97. }
  98. bool JoystickController::setRumble(uint8_t lValue, uint8_t rValue, uint8_t timeout)
  99. {
  100. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  101. // joystick types.
  102. rumble_lValue_ = lValue;
  103. rumble_rValue_ = rValue;
  104. rumble_timeout_ = timeout;
  105. switch (joystickType) {
  106. default:
  107. break;
  108. case PS3:
  109. return transmitPS3UserFeedbackMsg();
  110. case PS4:
  111. return transmitPS4UserFeedbackMsg();
  112. case XBOXONE:
  113. // Lets try sending a request to the XBox 1.
  114. txbuf_[0] = 0x9;
  115. txbuf_[1] = 0x0;
  116. txbuf_[2] = 0x0;
  117. txbuf_[3] = 0x09; // Substructure (what substructure rest of this packet has)
  118. txbuf_[4] = 0x00; // Mode
  119. txbuf_[5] = 0x0f; // Rumble mask (what motors are activated) (0000 lT rT L R)
  120. txbuf_[6] = 0x0; // lT force
  121. txbuf_[7] = 0x0; // rT force
  122. txbuf_[8] = lValue; // L force
  123. txbuf_[9] = rValue; // R force
  124. txbuf_[10] = 0xff; // Length of pulse
  125. txbuf_[11] = 0x00; // Period between pulses
  126. txbuf_[12] = 0x00; // Repeat
  127. if (!queue_Data_Transfer(txpipe_, txbuf_, 13, this)) {
  128. println("XBoxOne rumble transfer fail");
  129. }
  130. return true; //
  131. case XBOX360:
  132. txbuf_[0] = 0x00;
  133. txbuf_[1] = 0x01;
  134. txbuf_[2] = 0x0F;
  135. txbuf_[3] = 0xC0;
  136. txbuf_[4] = 0x00;
  137. txbuf_[5] = lValue;
  138. txbuf_[6] = rValue;
  139. txbuf_[7] = 0x00;
  140. txbuf_[8] = 0x00;
  141. txbuf_[9] = 0x00;
  142. txbuf_[10] = 0x00;
  143. txbuf_[11] = 0x00;
  144. if (!queue_Data_Transfer(txpipe_, txbuf_, 12, this)) {
  145. println("XBox360 rumble transfer fail");
  146. }
  147. return true;
  148. }
  149. return false;
  150. }
  151. bool JoystickController::setLEDs(uint8_t lr, uint8_t lg, uint8_t lb)
  152. {
  153. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  154. // joystick types.
  155. if ((leds_[0] != lr) || (leds_[1] != lg) || (leds_[2] != lb)) {
  156. leds_[0] = lr;
  157. leds_[1] = lg;
  158. leds_[2] = lb;
  159. switch (joystickType) {
  160. case PS3:
  161. return transmitPS3UserFeedbackMsg();
  162. case PS4:
  163. return transmitPS4UserFeedbackMsg();
  164. case XBOX360:
  165. // 0: off, 1: all blink then return to before
  166. // 2-5(TL, TR, BL, BR) - blink on then stay on
  167. // 6-9() - On
  168. // ...
  169. txbuf_[1] = 0x00;
  170. txbuf_[2] = 0x08;
  171. txbuf_[3] = 0x40 + lr;
  172. txbuf_[4] = 0x00;
  173. txbuf_[5] = 0x00;
  174. txbuf_[6] = 0x00;
  175. txbuf_[7] = 0x00;
  176. txbuf_[8] = 0x00;
  177. txbuf_[9] = 0x00;
  178. txbuf_[10] = 0x00;
  179. txbuf_[11] = 0x00;
  180. if (!queue_Data_Transfer(txpipe_, txbuf_, 12, this)) {
  181. println("XBox360 set leds fail");
  182. }
  183. return true;
  184. case XBOXONE:
  185. default:
  186. return false;
  187. }
  188. }
  189. return false;
  190. }
  191. bool JoystickController::transmitPS4UserFeedbackMsg() {
  192. if (driver_) {
  193. uint8_t packet[32];
  194. memset(packet, 0, sizeof(packet));
  195. packet[0] = 0x05; // Report ID
  196. packet[1]= 0xFF;
  197. packet[4] = rumble_lValue_; // Small Rumble
  198. packet[5] = rumble_rValue_; // Big rumble
  199. packet[6] = leds_[0]; // RGB value
  200. packet[7] = leds_[1];
  201. packet[8] = leds_[2];
  202. // 9, 10 flash ON, OFF times in 100ths of second? 2.5 seconds = 255
  203. DBGPrintf("Joystick update Rumble/LEDs\n");
  204. return driver_->sendPacket(packet, 32);
  205. } else if (btdriver_) {
  206. uint8_t packet[79];
  207. memset(packet, 0, sizeof(packet));
  208. //0xa2, 0x11, 0xc0, 0x20, 0xf0, 0x04, 0x00
  209. packet[0] = 0x52;
  210. packet[1] = 0x11; // Report ID
  211. packet[2] = 0x80;
  212. //packet[3] = 0x20;
  213. packet[4] = 0xFF;
  214. packet[7] = rumble_lValue_; // Small Rumble
  215. packet[8] = rumble_rValue_; // Big rumble
  216. packet[9] = leds_[0]; // RGB value
  217. packet[10] = leds_[1];
  218. packet[11] = leds_[2];
  219. // 12, 13 flash ON, OFF times in 100ths of sedond? 2.5 seconds = 255
  220. DBGPrintf("Joystick update Rumble/LEDs\n");
  221. btdriver_->sendL2CapCommand(packet, sizeof(packet), 0x40);
  222. return true;
  223. }
  224. return false;
  225. }
  226. static const uint8_t PS3_USER_FEEDBACK_INIT[] = {
  227. 0x00, 0x00, 0x00, 0x00, 0x00,
  228. 0x00, 0x00, 0x00, 0x00, 0x00,
  229. 0xff, 0x27, 0x10, 0x00, 0x32,
  230. 0xff, 0x27, 0x10, 0x00, 0x32,
  231. 0xff, 0x27, 0x10, 0x00, 0x32,
  232. 0xff, 0x27, 0x10, 0x00, 0x32,
  233. 0x00, 0x00, 0x00, 0x00, 0x00,
  234. 0x00, 0x00, 0x00, 0x00, 0x00,
  235. 0x00, 0x00, 0x00, 0x00, 0x00,
  236. 0x00, 0x00, 0x00 };
  237. bool JoystickController::transmitPS3UserFeedbackMsg() {
  238. if (driver_) {
  239. memcpy(txbuf_, PS3_USER_FEEDBACK_INIT, 48);
  240. txbuf_[1] = rumble_lValue_? rumble_timeout_ : 0;
  241. txbuf_[2] = rumble_lValue_; // Small Rumble
  242. txbuf_[3] = rumble_rValue_? rumble_timeout_ : 0;
  243. txbuf_[4] = rumble_rValue_; // Big rumble
  244. txbuf_[9] = leds_[0] << 1; // RGB value
  245. //DBGPrintf("\nJoystick update Rumble/LEDs %d %d %d %d %d\n", txbuf_[1], txbuf_[2], txbuf_[3], txbuf_[4], txbuf_[9]);
  246. return driver_->sendControlPacket(0x21, 9, 0x201, 0, 48, txbuf_);
  247. } else if (btdriver_) {
  248. txbuf_[0] = 0x52;
  249. txbuf_[1] = 0x1;
  250. memcpy(&txbuf_[2], PS3_USER_FEEDBACK_INIT, 48);
  251. txbuf_[3] = rumble_lValue_? rumble_timeout_ : 0;
  252. txbuf_[4] = rumble_lValue_; // Small Rumble
  253. txbuf_[5] = rumble_rValue_? rumble_timeout_ : 0;
  254. txbuf_[6] = rumble_rValue_; // Big rumble
  255. txbuf_[11] = leds_[0] << 1; // RGB value
  256. DBGPrintf("\nJoystick update Rumble/LEDs %d %d %d %d %d\n", txbuf_[3], txbuf_[4], txbuf_[5], txbuf_[6], txbuf_[11]);
  257. btdriver_->sendL2CapCommand(txbuf_, 50, BluetoothController::CONTROL_SCID);
  258. return true;
  259. }
  260. return false;
  261. }
  262. //*****************************************************************************
  263. // Support for Joysticks that Use HID data.
  264. //*****************************************************************************
  265. hidclaim_t JoystickController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  266. {
  267. // only claim Desktop/Joystick and Desktop/Gamepad
  268. if (topusage != 0x10004 && topusage != 0x10005) return CLAIM_NO;
  269. // only claim from one physical device
  270. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  271. // Also don't allow us to claim if it is used as a standard usb object (XBox...)
  272. if (device != nullptr) return CLAIM_NO;
  273. mydevice = dev;
  274. collections_claimed++;
  275. anychange = true; // always report values on first read
  276. driver_ = driver; // remember the driver.
  277. driver_->setTXBuffers(txbuf_, nullptr, sizeof(txbuf_));
  278. connected_ = true; // remember that hardware is actually connected...
  279. // Lets see if we know what type of joystick this is. That is, is it a PS3 or PS4 or ...
  280. joystickType = mapVIDPIDtoJoystickType(mydevice->idVendor, mydevice->idProduct, false);
  281. switch (joystickType) {
  282. case PS3:
  283. additional_axis_usage_page_ = 0x1;
  284. additional_axis_usage_start_ = 0x100;
  285. additional_axis_usage_count_ = 39;
  286. axis_change_notify_mask_ = (uint64_t)-1; // Start off assume all bits
  287. break;
  288. case PS4:
  289. additional_axis_usage_page_ = 0xFF00;
  290. additional_axis_usage_start_ = 0x21;
  291. additional_axis_usage_count_ = 54;
  292. axis_change_notify_mask_ = (uint64_t)0xfffffffffffff3ffl; // Start off assume all bits - 10 and 11
  293. break;
  294. default:
  295. additional_axis_usage_page_ = 0;
  296. additional_axis_usage_start_ = 0;
  297. additional_axis_usage_count_ = 0;
  298. axis_change_notify_mask_ = 0x3ff; // Start off assume only the 10 bits...
  299. }
  300. DBGPrintf("Claim Additional axis: %x %x %d\n", additional_axis_usage_page_, additional_axis_usage_start_, additional_axis_usage_count_);
  301. return CLAIM_REPORT;
  302. }
  303. void JoystickController::disconnect_collection(Device_t *dev)
  304. {
  305. if (--collections_claimed == 0) {
  306. mydevice = NULL;
  307. driver_ = nullptr;
  308. axis_mask_ = 0;
  309. axis_changed_mask_ = 0;
  310. }
  311. }
  312. void JoystickController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  313. {
  314. // TODO: set up translation from logical min/max to consistent 16 bit scale
  315. }
  316. void JoystickController::hid_input_data(uint32_t usage, int32_t value)
  317. {
  318. //DBGPrintf("Joystick: usage=%X, value=%d\n", usage, value);
  319. uint32_t usage_page = usage >> 16;
  320. usage &= 0xFFFF;
  321. if (usage_page == 9 && usage >= 1 && usage <= 32) {
  322. uint32_t bit = 1 << (usage -1);
  323. if (value == 0) {
  324. if (buttons & bit) {
  325. buttons &= ~bit;
  326. anychange = true;
  327. }
  328. } else {
  329. if (!(buttons & bit)) {
  330. buttons |= bit;
  331. anychange = true;
  332. }
  333. }
  334. } else if (usage_page == 1 && usage >= 0x30 && usage <= 0x39) {
  335. // TODO: need scaling of value to consistent API, 16 bit signed?
  336. // TODO: many joysticks repeat slider usage. Detect & map to axis?
  337. uint32_t i = usage - 0x30;
  338. axis_mask_ |= (1 << i); // Keep record of which axis we have data on.
  339. if (axis[i] != value) {
  340. axis[i] = value;
  341. axis_changed_mask_ |= (1 << i);
  342. if (axis_changed_mask_ & axis_change_notify_mask_)
  343. anychange = true;
  344. }
  345. } else if (usage_page == additional_axis_usage_page_) {
  346. // see if the usage is witin range.
  347. //DBGPrintf("UP: usage_page=%x usage=%x User: %x %d\n", usage_page, usage, user_buttons_usage_start, user_buttons_count_);
  348. if ((usage >= additional_axis_usage_start_) && (usage < (additional_axis_usage_start_ + additional_axis_usage_count_))) {
  349. // We are in the user range.
  350. uint16_t usage_index = usage - additional_axis_usage_start_ + STANDARD_AXIS_COUNT;
  351. if (usage_index < (sizeof(axis)/sizeof(axis[0]))) {
  352. if (axis[usage_index] != value) {
  353. axis[usage_index] = value;
  354. if (usage_index > 63) usage_index = 63; // don't overflow our mask
  355. axis_changed_mask_ |= ((uint64_t)1 << usage_index); // Keep track of which ones changed.
  356. if (axis_changed_mask_ & axis_change_notify_mask_)
  357. anychange = true; // We have changes...
  358. }
  359. axis_mask_ |= ((uint64_t)1 << usage_index); // Keep record of which axis we have data on.
  360. }
  361. //DBGPrintf("UB: index=%x value=%x\n", usage_index, value);
  362. }
  363. } else {
  364. DBGPrintf("UP: usage_page=%x usage=%x add: %x %x %d\n", usage_page, usage, additional_axis_usage_page_, additional_axis_usage_start_, additional_axis_usage_count_);
  365. }
  366. // TODO: hat switch?
  367. }
  368. void JoystickController::hid_input_end()
  369. {
  370. if (anychange) {
  371. joystickEvent = true;
  372. }
  373. }
  374. bool JoystickController::hid_process_out_data(const Transfer_t *transfer)
  375. {
  376. //DBGPrintf("JoystickController::hid_process_out_data\n");
  377. return true;
  378. }
  379. void JoystickController::joystickDataClear() {
  380. joystickEvent = false;
  381. anychange = false;
  382. axis_changed_mask_ = 0;
  383. axis_mask_ = 0;
  384. }
  385. //*****************************************************************************
  386. // Support for Joysticks that are class specific and do not use HID
  387. // Example: XBox One controller.
  388. //*****************************************************************************
  389. static uint8_t xboxone_start_input[] = {0x05, 0x20, 0x00, 0x01, 0x00};
  390. static uint8_t xbox360w_inquire_present[] = {0x08, 0x00, 0x0F, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  391. bool JoystickController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  392. {
  393. println("JoystickController claim this=", (uint32_t)this, HEX);
  394. // Don't try to claim if it is used as USB device or HID device
  395. if (mydevice != NULL) return false;
  396. if (device != nullptr) return false;
  397. // Try claiming at the interface level.
  398. if (type != 1) return false;
  399. print_hexbytes(descriptors, len);
  400. JoystickController::joytype_t jtype = mapVIDPIDtoJoystickType(dev->idVendor, dev->idProduct, true);
  401. println("Jtype=", (uint8_t)jtype, DEC);
  402. if (jtype == UNKNOWN)
  403. return false;
  404. // XBOX One
  405. // 0 1 2 3 4 5 6 7 8 *9 10 1 2 3 4 5 *6 7 8 9 20 1 2 3 4 5 6 7 8 9 30 1...
  406. // 09 04 00 00 02 FF 47 D0 00 07 05 02 03 40 00 04 07 05 82 03 40 00 04 09 04 01 00 00 FF 47 D0 00
  407. // Lets do some verifications to make sure.
  408. // XBOX 360 wireless... Has 8 interfaces. 4 joysticks (1, 3, 5, 7) and 4 headphones assume 2,4,6, 8...
  409. // Shows data for #1 only...
  410. // Also they have some unknown data type we need to ignore between interface and end points.
  411. // 0 1 2 3 4 5 6 7 8 *9 10 1 2 3 4 5 *6 7 8 9 20 1 2 3 4 5 6 7 8
  412. // 09 04 00 00 02 FF 5D 81 00 14 22 00 01 13 81 1D 00 17 01 02 08 13 01 0C 00 0C 01 02 08
  413. // 29 30 1 2 3 4 5 6 7 8 9 40 41 42
  414. // 07 05 81 03 20 00 01 07 05 01 03 20 00 08
  415. if (len < 9+7+7) return false;
  416. // Some common stuff for both XBoxs
  417. uint32_t count_end_points = descriptors[4];
  418. if (count_end_points < 2) return false;
  419. if (descriptors[5] != 0xff) return false; // bInterfaceClass, 3 = HID
  420. rx_ep_ = 0;
  421. uint32_t txep = 0;
  422. uint8_t rx_interval = 0;
  423. uint8_t tx_interval = 0;
  424. rx_size_ = 0;
  425. tx_size_ = 0;
  426. uint32_t descriptor_index = 9;
  427. if (descriptors[descriptor_index+1] == 0x22) {
  428. if (descriptors[descriptor_index] != 0x14) return false; // only support specific versions...
  429. descriptor_index += descriptors[descriptor_index]; // XBox360w ignore this unknown setup...
  430. }
  431. while (count_end_points-- && ((rx_ep_ == 0) || txep == 0)) {
  432. if (descriptors[descriptor_index] != 7) return false; // length 7
  433. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  434. if ((descriptors[descriptor_index+3] == 3) // Type 3...
  435. && (descriptors[descriptor_index+4] <= 64)
  436. && (descriptors[descriptor_index+5] == 0)) {
  437. // have a bulk EP size
  438. if (descriptors[descriptor_index+2] & 0x80 ) {
  439. rx_ep_ = descriptors[descriptor_index+2];
  440. rx_size_ = descriptors[descriptor_index+4];
  441. rx_interval = descriptors[descriptor_index+6];
  442. } else {
  443. txep = descriptors[descriptor_index+2];
  444. tx_size_ = descriptors[descriptor_index+4];
  445. tx_interval = descriptors[descriptor_index+6];
  446. }
  447. }
  448. descriptor_index += 7; // setup to look at next one...
  449. }
  450. if ((rx_ep_ == 0) || (txep == 0)) return false; // did not find two end points.
  451. print("JoystickController, rx_ep_=", rx_ep_ & 15);
  452. print("(", rx_size_);
  453. print("), txep=", txep);
  454. print("(", tx_size_);
  455. println(")");
  456. rxpipe_ = new_Pipe(dev, 3, rx_ep_ & 15, 1, rx_size_, rx_interval);
  457. if (!rxpipe_) return false;
  458. txpipe_ = new_Pipe(dev, 3, txep, 0, tx_size_, tx_interval);
  459. if (!txpipe_) {
  460. //free_Pipe(rxpipe_);
  461. return false;
  462. }
  463. rxpipe_->callback_function = rx_callback;
  464. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  465. txpipe_->callback_function = tx_callback;
  466. if (jtype == XBOXONE) {
  467. queue_Data_Transfer(txpipe_, xboxone_start_input, sizeof(xboxone_start_input), this);
  468. connected_ = true; // remember that hardware is actually connected...
  469. } else if (jtype == XBOX360) {
  470. queue_Data_Transfer(txpipe_, xbox360w_inquire_present, sizeof(xbox360w_inquire_present), this);
  471. connected_ = 0; // remember that hardware is actually connected...
  472. }
  473. memset(axis, 0, sizeof(axis)); // clear out any data.
  474. joystickType = jtype; // remember we are an XBox One.
  475. return true;
  476. }
  477. void JoystickController::control(const Transfer_t *transfer)
  478. {
  479. }
  480. /************************************************************/
  481. // Interrupt-based Data Movement
  482. /************************************************************/
  483. void JoystickController::rx_callback(const Transfer_t *transfer)
  484. {
  485. if (!transfer->driver) return;
  486. ((JoystickController *)(transfer->driver))->rx_data(transfer);
  487. }
  488. void JoystickController::tx_callback(const Transfer_t *transfer)
  489. {
  490. if (!transfer->driver) return;
  491. ((JoystickController *)(transfer->driver))->tx_data(transfer);
  492. }
  493. /************************************************************/
  494. // Interrupt-based Data Movement
  495. // XBox one input data when type == 0x20
  496. // Information came from several places on the web including:
  497. // https://github.com/quantus/xbox-one-controller-protocol
  498. /************************************************************/
  499. typedef struct {
  500. uint8_t type;
  501. uint8_t const_0;
  502. uint16_t id;
  503. // From online references button order:
  504. // sync, dummy, start, back, a, b, x, y
  505. // dpad up, down left, right
  506. // lb, rb, left stick, right stick
  507. // Axis:
  508. // lt, rt, lx, ly, rx, ry
  509. //
  510. uint16_t buttons;
  511. int16_t axis[6];
  512. } xbox1data20_t;
  513. typedef struct {
  514. uint8_t state;
  515. uint8_t id_or_type;
  516. uint16_t controller_status;
  517. uint16_t unknown;
  518. // From online references button order:
  519. // sync, dummy, start, back, a, b, x, y
  520. // dpad up, down left, right
  521. // lb, rb, left stick, right stick
  522. // Axis:
  523. // lt, rt, lx, ly, rx, ry
  524. //
  525. uint16_t buttons;
  526. uint8_t lt;
  527. uint8_t rt;
  528. int16_t axis[4];
  529. } xbox360data_t;
  530. static const uint8_t xbox_axis_order_mapping[] = {4, 5, 0, 1, 2, 3};
  531. void JoystickController::rx_data(const Transfer_t *transfer)
  532. {
  533. print("JoystickController::rx_data: ");
  534. print_hexbytes((uint8_t*)transfer->buffer, transfer->length);
  535. if (joystickType == XBOXONE) {
  536. // Process XBOX One data
  537. axis_mask_ = 0x3f;
  538. axis_changed_mask_ = 0; // assume none for now
  539. xbox1data20_t *xb1d = (xbox1data20_t *)transfer->buffer;
  540. if ((xb1d->type == 0x20) && (transfer->length >= sizeof (xbox1data20_t))) {
  541. // We have a data transfer. Lets see what is new...
  542. if (xb1d->buttons != buttons) {
  543. buttons = xb1d->buttons;
  544. anychange = true;
  545. }
  546. for (uint8_t i = 0; i < sizeof (xbox_axis_order_mapping); i++) {
  547. // The first two values were unsigned.
  548. int axis_value = (i < 2)? (int)(uint16_t)xb1d->axis[i] : xb1d->axis[i];
  549. if (axis_value != axis[xbox_axis_order_mapping[i]]) {
  550. axis[xbox_axis_order_mapping[i]] = axis_value;
  551. axis_changed_mask_ |= (1 << xbox_axis_order_mapping[i]);
  552. anychange = true;
  553. }
  554. }
  555. joystickEvent = true;
  556. }
  557. } else if (joystickType == XBOX360) {
  558. // First byte appears to status - if the byte is 0x8 it is a connect or disconnect of the controller.
  559. xbox360data_t *xb360d = (xbox360data_t *)transfer->buffer;
  560. if (xb360d->state == 0x08) {
  561. if (xb360d->id_or_type != connected_) {
  562. connected_ = xb360d->id_or_type; // remember it...
  563. if (connected_) {
  564. println("XBox360w - Connected type:", connected_, HEX);
  565. // rx_ep_ should be 1, 3, 5, 7 for the wireless convert to 2-5 on led
  566. setLEDs(2+rx_ep_/2); // Right now hard coded to first joystick...
  567. } else {
  568. println("XBox360w - disconnected");
  569. }
  570. }
  571. } else if((xb360d->id_or_type == 0x00) && (xb360d->controller_status & 0x1300)) {
  572. // Controller status report - Maybe we should save away and allow the user access?
  573. println("XBox360w - controllerStatus: ", xb360d->controller_status, HEX);
  574. } else if(xb360d->id_or_type == 0x01) { // Lets only process report 1.
  575. //const uint8_t *pbuffer = (uint8_t*)transfer->buffer;
  576. //for (uint8_t i = 0; i < transfer->length; i++) DBGPrintf("%02x ", pbuffer[i]);
  577. //DBGPrintf("\n");
  578. if (buttons != xb360d->buttons) {
  579. buttons = xb360d->buttons;
  580. anychange = true;
  581. }
  582. axis_mask_ = 0x3f;
  583. axis_changed_mask_ = 0; // assume none for now
  584. for (uint8_t i = 0; i < 4; i++) {
  585. if (axis[i] != xb360d->axis[i]) {
  586. axis[i] = xb360d->axis[i];
  587. axis_changed_mask_ |= (1 << i);
  588. anychange = true;
  589. }
  590. }
  591. // the two triggers show up as 4 and 5
  592. if (axis[4] != xb360d->lt) {
  593. axis[4] = xb360d->lt;
  594. axis_changed_mask_ |= (1 << 4);
  595. anychange = true;
  596. }
  597. if (axis[5] != xb360d->rt) {
  598. axis[5] = xb360d->rt;
  599. axis_changed_mask_ |= (1 << 5);
  600. anychange = true;
  601. }
  602. if (anychange) joystickEvent = true;
  603. }
  604. }
  605. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  606. }
  607. void JoystickController::tx_data(const Transfer_t *transfer)
  608. {
  609. }
  610. void JoystickController::disconnect()
  611. {
  612. axis_mask_ = 0;
  613. axis_changed_mask_ = 0;
  614. // TODO: free resources
  615. }
  616. bool JoystickController::claim_bluetooth(BluetoothController *driver, uint32_t bluetooth_class, uint8_t *remoteName)
  617. {
  618. if ((((bluetooth_class & 0xff00) == 0x2500) || (((bluetooth_class & 0xff00) == 0x500))) && ((bluetooth_class & 0x3C) == 0x08)) {
  619. DBGPrintf("JoystickController::claim_bluetooth TRUE\n");
  620. btdriver_ = driver;
  621. btdevice = (Device_t*)driver; // remember this way
  622. return true;
  623. }
  624. if (remoteName && (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0)) {
  625. DBGPrintf("JoystickController::claim_bluetooth TRUE PS3 hack...\n");
  626. btdriver_ = driver;
  627. btdevice = (Device_t*)driver; // remember this way
  628. special_process_required = SP_PS3_IDS; // PS3 maybe needs different IDS.
  629. joystickType = PS3;
  630. return true;
  631. }
  632. return false;
  633. }
  634. bool JoystickController::process_bluetooth_HID_data(const uint8_t *data, uint16_t length)
  635. {
  636. // Example data from PS4 controller
  637. //01 7e 7f 82 84 08 00 00 00 00
  638. // LX LY RX RY BT BT PS LT RT
  639. //DBGPrintf("JoystickController::process_bluetooth_HID_data\n");
  640. // May have to look at this one with other controllers...
  641. if (data[0] == 1) {
  642. //print(" Joystick Data: ");
  643. //print_hexbytes(data, length);
  644. // DBGPrintf(" Joystick Data: ");
  645. uint64_t mask = 0x1;
  646. axis_mask_ = 0;
  647. axis_changed_mask_ = 0;
  648. if (length > TOTAL_AXIS_COUNT) length = TOTAL_AXIS_COUNT; // don't overflow arrays...
  649. for (uint16_t i = 0; i < length; i++ ) {
  650. axis_mask_ |= mask;
  651. if(data[i] != axis[i]) {
  652. axis_changed_mask_ |= mask;
  653. axis[i] = data[i];
  654. }
  655. mask <<= 1; // shift down the mask.
  656. // DBGPrintf("%02x ", axis[i]);
  657. }
  658. // DBGPrintf("\n");
  659. joystickEvent = true;
  660. connected_ = true;
  661. return true;
  662. } else if(data[0] == 0x11){
  663. DBGPrintf(" Joystick Data: ");
  664. uint64_t mask = 0x1;
  665. axis_mask_ = 0;
  666. axis_changed_mask_ = 0;
  667. //This moves data to be equivalent to what we see for
  668. //data[0] = 0x01
  669. uint8_t tmp_data[length-2];
  670. for (uint16_t i = 0; i < (length-2); i++ ) {
  671. tmp_data[i] = data[i+2];
  672. }
  673. /*
  674. * [1] LX, [2] = LY, [3] = RX, [4] = RY
  675. * [5] combo, tri, cir, x, sqr, D-PAD (4bits, 0-3
  676. * [6] R3,L3, opt, share, R2, L2, R1, L1
  677. * [7] Counter (bit7-2), T-PAD, PS
  678. * [8] Left Trigger, [9] Right Trigger
  679. * [10-11] Timestamp
  680. * [12] Battery (0 to 0xff)
  681. * [13-14] acceleration x
  682. * [15-16] acceleration y
  683. * [17-18] acceleration z
  684. * [19-20] gyro x
  685. * [21-22] gyro y
  686. * [23-24] gyro z
  687. * [25-29] unknown
  688. * [30] 0x00,phone,mic, usb, battery level (4bits)
  689. * rest is trackpad? to do implement?
  690. */
  691. //PS Bit
  692. tmp_data[7] = (tmp_data[7] >> 0) & 1;
  693. //set arrow buttons to axis[0]
  694. tmp_data[0] = tmp_data[5] & ((1 << 4) - 1);
  695. //set buttons for last 4bits in the axis[5]
  696. tmp_data[5] = tmp_data[5] >> 4;
  697. for (uint16_t i = 0; i < (length-2); i++ ) {
  698. if(tmp_data[i] != axis[i]) {
  699. axis_changed_mask_ |= mask;
  700. axis[i] = tmp_data[i];
  701. }
  702. mask <<= 1; // shift down the mask.
  703. DBGPrintf("%02x ", axis[i]);
  704. }
  705. DBGPrintf("\n");
  706. joystickEvent = true;
  707. connected_ = true;
  708. }
  709. return false;
  710. }
  711. bool JoystickController::remoteNameComplete(const uint8_t *remoteName)
  712. {
  713. // Sort of a hack, but try to map the name given from remote to a type...
  714. if (strncmp((const char *)remoteName, "Wireless Controller", 19) == 0) {
  715. DBGPrintf(" JoystickController::remoteNameComplete %s - set to PS4\n", remoteName);
  716. special_process_required = SP_NEED_CONNECT; // We need to force this.
  717. joystickType = PS4;
  718. } else if (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0) {
  719. DBGPrintf(" JoystickController::remoteNameComplete %s - set to PS3\n", remoteName);
  720. special_process_required = SP_PS3_IDS; // PS3 maybe needs different IDS.
  721. joystickType = PS3;
  722. } else {
  723. DBGPrintf(" JoystickController::remoteNameComplete %s - Unknown\n", remoteName);
  724. }
  725. return true;
  726. }
  727. void JoystickController::connectionComplete()
  728. {
  729. DBGPrintf(" JoystickController::connectionComplete joystick type %d\n", joystickType);
  730. if (joystickType == PS4) {
  731. uint8_t packet[2];
  732. packet[0] = 0x43; // HID BT Get_report (0x40) | Report Type (Feature 0x03)
  733. packet[1] = 0x02; // Report ID
  734. DBGPrintf("Set PS4 report\n");
  735. delay(1);
  736. btdriver_->sendL2CapCommand(packet, sizeof(packet), 0x40);
  737. } else if (joystickType == PS3) {
  738. uint8_t packet[6];
  739. packet[0] = 0x53; // HID BT Set_report (0x50) | Report Type (Feature 0x03)
  740. packet[1] = 0xF4; // Report ID
  741. packet[2] = 0x42; // Special PS3 Controller enable commands
  742. packet[3] = 0x03;
  743. packet[4] = 0x00;
  744. packet[5] = 0x00;
  745. DBGPrintf("enable six axis\n");
  746. delay(1);
  747. btdriver_->sendL2CapCommand(packet, sizeof(packet), BluetoothController::CONTROL_SCID);
  748. }
  749. }
  750. void JoystickController::release_bluetooth()
  751. {
  752. btdevice = nullptr; // remember this way
  753. btdriver_ = nullptr;
  754. connected_ = false;
  755. special_process_required = false;
  756. }