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.

999 líneas
33KB

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