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

975 linhas
32KB

  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_ = 0;
  298. additional_axis_usage_start_ = 0;
  299. additional_axis_usage_count_ = 0;
  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. typedef struct {
  503. uint8_t type;
  504. uint8_t const_0;
  505. uint16_t id;
  506. // From online references button order:
  507. // sync, dummy, start, back, a, b, x, y
  508. // dpad up, down left, right
  509. // lb, rb, left stick, right stick
  510. // Axis:
  511. // lt, rt, lx, ly, rx, ry
  512. //
  513. uint16_t buttons;
  514. int16_t axis[6];
  515. } xbox1data20_t;
  516. typedef struct {
  517. uint8_t state;
  518. uint8_t id_or_type;
  519. uint16_t controller_status;
  520. uint16_t unknown;
  521. // From online references button order:
  522. // sync, dummy, start, back, a, b, x, y
  523. // dpad up, down left, right
  524. // lb, rb, left stick, right stick
  525. // Axis:
  526. // lt, rt, lx, ly, rx, ry
  527. //
  528. uint16_t buttons;
  529. uint8_t lt;
  530. uint8_t rt;
  531. int16_t axis[4];
  532. } xbox360data_t;
  533. static const uint8_t xbox_axis_order_mapping[] = {4, 5, 0, 1, 2, 3};
  534. void JoystickController::rx_data(const Transfer_t *transfer)
  535. {
  536. print("JoystickController::rx_data: ");
  537. print_hexbytes((uint8_t*)transfer->buffer, transfer->length);
  538. if (joystickType_ == XBOXONE) {
  539. // Process XBOX One data
  540. axis_mask_ = 0x3f;
  541. axis_changed_mask_ = 0; // assume none for now
  542. xbox1data20_t *xb1d = (xbox1data20_t *)transfer->buffer;
  543. if ((xb1d->type == 0x20) && (transfer->length >= sizeof (xbox1data20_t))) {
  544. // We have a data transfer. Lets see what is new...
  545. if (xb1d->buttons != buttons) {
  546. buttons = xb1d->buttons;
  547. anychange = true;
  548. }
  549. for (uint8_t i = 0; i < sizeof (xbox_axis_order_mapping); i++) {
  550. // The first two values were unsigned.
  551. int axis_value = (i < 2)? (int)(uint16_t)xb1d->axis[i] : xb1d->axis[i];
  552. if (axis_value != axis[xbox_axis_order_mapping[i]]) {
  553. axis[xbox_axis_order_mapping[i]] = axis_value;
  554. axis_changed_mask_ |= (1 << xbox_axis_order_mapping[i]);
  555. anychange = true;
  556. }
  557. }
  558. joystickEvent = true;
  559. }
  560. } else if (joystickType_ == XBOX360) {
  561. // First byte appears to status - if the byte is 0x8 it is a connect or disconnect of the controller.
  562. xbox360data_t *xb360d = (xbox360data_t *)transfer->buffer;
  563. if (xb360d->state == 0x08) {
  564. if (xb360d->id_or_type != connected_) {
  565. connected_ = xb360d->id_or_type; // remember it...
  566. if (connected_) {
  567. println("XBox360w - Connected type:", connected_, HEX);
  568. // rx_ep_ should be 1, 3, 5, 7 for the wireless convert to 2-5 on led
  569. setLEDs(2+rx_ep_/2); // Right now hard coded to first joystick...
  570. } else {
  571. println("XBox360w - disconnected");
  572. }
  573. }
  574. } else if((xb360d->id_or_type == 0x00) && (xb360d->controller_status & 0x1300)) {
  575. // Controller status report - Maybe we should save away and allow the user access?
  576. println("XBox360w - controllerStatus: ", xb360d->controller_status, HEX);
  577. } else if(xb360d->id_or_type == 0x01) { // Lets only process report 1.
  578. //const uint8_t *pbuffer = (uint8_t*)transfer->buffer;
  579. //for (uint8_t i = 0; i < transfer->length; i++) DBGPrintf("%02x ", pbuffer[i]);
  580. //DBGPrintf("\n");
  581. if (buttons != xb360d->buttons) {
  582. buttons = xb360d->buttons;
  583. anychange = true;
  584. }
  585. axis_mask_ = 0x3f;
  586. axis_changed_mask_ = 0; // assume none for now
  587. for (uint8_t i = 0; i < 4; i++) {
  588. if (axis[i] != xb360d->axis[i]) {
  589. axis[i] = xb360d->axis[i];
  590. axis_changed_mask_ |= (1 << i);
  591. anychange = true;
  592. }
  593. }
  594. // the two triggers show up as 4 and 5
  595. if (axis[4] != xb360d->lt) {
  596. axis[4] = xb360d->lt;
  597. axis_changed_mask_ |= (1 << 4);
  598. anychange = true;
  599. }
  600. if (axis[5] != xb360d->rt) {
  601. axis[5] = xb360d->rt;
  602. axis_changed_mask_ |= (1 << 5);
  603. anychange = true;
  604. }
  605. if (anychange) joystickEvent = true;
  606. }
  607. }
  608. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  609. }
  610. void JoystickController::tx_data(const Transfer_t *transfer)
  611. {
  612. }
  613. void JoystickController::disconnect()
  614. {
  615. axis_mask_ = 0;
  616. axis_changed_mask_ = 0;
  617. // TODO: free resources
  618. }
  619. bool JoystickController::claim_bluetooth(BluetoothController *driver, uint32_t bluetooth_class, uint8_t *remoteName)
  620. {
  621. if ((((bluetooth_class & 0xff00) == 0x2500) || (((bluetooth_class & 0xff00) == 0x500))) && ((bluetooth_class & 0x3C) == 0x08)) {
  622. DBGPrintf("JoystickController::claim_bluetooth TRUE\n");
  623. btdriver_ = driver;
  624. btdevice = (Device_t*)driver; // remember this way
  625. return true;
  626. }
  627. if (remoteName && (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0)) {
  628. DBGPrintf("JoystickController::claim_bluetooth TRUE PS3 hack...\n");
  629. btdriver_ = driver;
  630. btdevice = (Device_t*)driver; // remember this way
  631. special_process_required = SP_PS3_IDS; // PS3 maybe needs different IDS.
  632. joystickType_ = PS3;
  633. return true;
  634. }
  635. return false;
  636. }
  637. bool JoystickController::process_bluetooth_HID_data(const uint8_t *data, uint16_t length)
  638. {
  639. // Example data from PS4 controller
  640. //01 7e 7f 82 84 08 00 00 00 00
  641. // LX LY RX RY BT BT PS LT RT
  642. //DBGPrintf("JoystickController::process_bluetooth_HID_data\n");
  643. // May have to look at this one with other controllers...
  644. if (data[0] == 1) {
  645. //print(" Joystick Data: ");
  646. //print_hexbytes(data, length);
  647. // DBGPrintf(" Joystick Data: ");
  648. if (length > TOTAL_AXIS_COUNT) length = TOTAL_AXIS_COUNT; // don't overflow arrays...
  649. if (joystickType_ == PS3) {
  650. // Quick and dirty hack to match PS3 HID data
  651. uint32_t cur_buttons = data[2] | ((uint16_t)data[3] << 8) | ((uint32_t)data[4] << 16);
  652. if (cur_buttons != buttons) {
  653. buttons = cur_buttons;
  654. joystickEvent = true; // something changed.
  655. }
  656. uint64_t mask = 0x1;
  657. axis_mask_ = 0x27; // assume bits 0, 1, 2, 5
  658. for (uint16_t i = 0; i < 3; i++) {
  659. if (axis[i] != data[i+6]) {
  660. axis_changed_mask_ |= mask;
  661. axis[i] = data[i+6];
  662. }
  663. mask <<= 1; // shift down the mask.
  664. }
  665. if (axis[5] != data[9]) {
  666. axis_changed_mask_ |= (1<<5);
  667. axis[5] = data[9];
  668. }
  669. if (axis[3] != data[18]) {
  670. axis_changed_mask_ |= (1<<3);
  671. axis[3] = data[18];
  672. }
  673. if (axis[4] != data[19]) {
  674. axis_changed_mask_ |= (1<<4);
  675. axis[4] = data[19];
  676. }
  677. // Then rest of data
  678. mask = 0x1 << 10; // setup for other bits
  679. for (uint16_t i = 10; i < length; i++ ) {
  680. axis_mask_ |= mask;
  681. if(data[i] != axis[i]) {
  682. axis_changed_mask_ |= mask;
  683. axis[i] = data[i];
  684. }
  685. mask <<= 1; // shift down the mask.
  686. }
  687. } else {
  688. uint64_t mask = 0x1;
  689. axis_mask_ = 0;
  690. for (uint16_t i = 0; i < length; i++ ) {
  691. axis_mask_ |= mask;
  692. if(data[i] != axis[i]) {
  693. axis_changed_mask_ |= mask;
  694. axis[i] = data[i];
  695. }
  696. mask <<= 1; // shift down the mask.
  697. // DBGPrintf("%02x ", axis[i]);
  698. }
  699. }
  700. if (axis_changed_mask_ & axis_change_notify_mask_)
  701. joystickEvent = true;
  702. connected_ = true;
  703. return true;
  704. } else if(data[0] == 0x11){
  705. if (data[0] == 1) {
  706. //print(" Joystick Data: ");
  707. //print_hexbytes(data, length);
  708. // DBGPrintf(" Joystick Data: ");
  709. uint64_t mask = 0x1;
  710. axis_mask_ = 0;
  711. axis_changed_mask_ = 0;
  712. if (length > TOTAL_AXIS_COUNT) length = TOTAL_AXIS_COUNT; // don't overflow arrays...
  713. for (uint16_t i = 0; i < length; i++ ) {
  714. axis_mask_ |= mask;
  715. if(data[i] != axis[i]) {
  716. axis_changed_mask_ |= mask;
  717. axis[i] = data[i];
  718. }
  719. mask <<= 1; // shift down the mask.
  720. // DBGPrintf("%02x ", axis[i]);
  721. }
  722. // DBGPrintf("\n");
  723. joystickEvent = true;
  724. connected_ = true;
  725. return true;
  726. } else if(data[0] == 0x11){
  727. DBGPrintf("\n Joystick Data: ");
  728. uint64_t mask = 0x1;
  729. axis_mask_ = 0;
  730. axis_changed_mask_ = 0;
  731. //This moves data to be equivalent to what we see for
  732. //data[0] = 0x01
  733. uint8_t tmp_data[length-2];
  734. for (uint16_t i = 0; i < (length-2); i++ ) {
  735. tmp_data[i] = 0;
  736. tmp_data[i] = data[i+2];
  737. }
  738. /*
  739. * [1] LX, [2] = LY, [3] = RX, [4] = RY
  740. * [5] combo, tri, cir, x, sqr, D-PAD (4bits, 0-3
  741. * [6] R3,L3, opt, share, R2, L2, R1, L1
  742. * [7] Counter (bit7-2), T-PAD, PS
  743. * [8] Left Trigger, [9] Right Trigger
  744. * [10-11] Timestamp
  745. * [12] Battery (0 to 0xff)
  746. * [13-14] acceleration x
  747. * [15-16] acceleration y
  748. * [17-18] acceleration z
  749. * [19-20] gyro x
  750. * [21-22] gyro y
  751. * [23-24] gyro z
  752. * [25-29] unknown
  753. * [30] 0x00,phone,mic, usb, battery level (4bits)
  754. * rest is trackpad? to do implement?
  755. */
  756. //PS Bit
  757. tmp_data[7] = (tmp_data[7] >> 0) & 1;
  758. //set arrow buttons to axis[0]
  759. tmp_data[10] = tmp_data[5] & ((1 << 4) - 1);
  760. //set buttons for last 4bits in the axis[5]
  761. tmp_data[5] = tmp_data[5] >> 4;
  762. // Quick and dirty hack to match PS4 HID data
  763. uint32_t cur_buttons = tmp_data[7] | (tmp_data[10]) | ((tmp_data[6]*10)) | ((uint16_t)tmp_data[5] << 16) ;
  764. if (cur_buttons != buttons) {
  765. buttons = cur_buttons;
  766. joystickEvent = true; // something changed.
  767. }
  768. mask = 0x1;
  769. axis_mask_ = 0x27; // assume bits 0, 1, 2, 5
  770. for (uint16_t i = 0; i < 3; i++) {
  771. if (axis[i] != tmp_data[i+1]) {
  772. axis_changed_mask_ |= mask;
  773. axis[i] = tmp_data[i+1];
  774. }
  775. mask <<= 1; // shift down the mask.
  776. }
  777. if (axis[5] != tmp_data[4]) {
  778. axis_changed_mask_ |= (1<<5);
  779. axis[5] = tmp_data[4];
  780. }
  781. if (axis[3] != tmp_data[8]) {
  782. axis_changed_mask_ |= (1<<3);
  783. axis[3] = tmp_data[8];
  784. }
  785. if (axis[4] != tmp_data[9]) {
  786. axis_changed_mask_ |= (1<<4);
  787. axis[4] = tmp_data[9];
  788. }
  789. //limit for masking
  790. mask = 0x1;
  791. for (uint16_t i = 6; i < (64); i++ ) {
  792. axis_mask_ |= mask;
  793. if(tmp_data[i] != axis[i]) {
  794. axis_changed_mask_ |= mask;
  795. axis[i] = tmp_data[i];
  796. }
  797. mask <<= 1; // shift down the mask.
  798. DBGPrintf("%02x ", axis[i]);
  799. }
  800. DBGPrintf("\n");
  801. //DBGPrintf("Axis Mask (axis_mask_, axis_changed_mask_; %d, %d\n", axis_mask_,axis_changed_mask_);
  802. joystickEvent = true;
  803. connected_ = true;
  804. }
  805. }
  806. return false;
  807. }
  808. bool JoystickController::remoteNameComplete(const uint8_t *remoteName)
  809. {
  810. // Sort of a hack, but try to map the name given from remote to a type...
  811. if (strncmp((const char *)remoteName, "Wireless Controller", 19) == 0) {
  812. DBGPrintf(" JoystickController::remoteNameComplete %s - set to PS4\n", remoteName);
  813. special_process_required = SP_NEED_CONNECT; // We need to force this.
  814. joystickType_ = PS4;
  815. } else if (strncmp((const char *)remoteName, "PLAYSTATION(R)3", 15) == 0) {
  816. DBGPrintf(" JoystickController::remoteNameComplete %x %s - set to PS3\n", (uint32_t)this, remoteName);
  817. special_process_required = SP_PS3_IDS; // PS3 maybe needs different IDS.
  818. joystickType_ = PS3;
  819. } else {
  820. DBGPrintf(" JoystickController::remoteNameComplete %s - Unknown\n", remoteName);
  821. }
  822. DBGPrintf(" Joystick Type: %d\n", joystickType_);
  823. return true;
  824. }
  825. void JoystickController::connectionComplete()
  826. {
  827. DBGPrintf(" JoystickController::connectionComplete %x joystick type %d\n", (uint32_t)this, joystickType_);
  828. if (joystickType_ == PS4) {
  829. uint8_t packet[2];
  830. packet[0] = 0x43; // HID BT Get_report (0x40) | Report Type (Feature 0x03)
  831. packet[1] = 0x02; // Report ID
  832. DBGPrintf("Set PS4 report\n");
  833. delay(1);
  834. btdriver_->sendL2CapCommand(packet, sizeof(packet), 0x40);
  835. } else if (joystickType_ == PS3) {
  836. uint8_t packet[6];
  837. packet[0] = 0x53; // HID BT Set_report (0x50) | Report Type (Feature 0x03)
  838. packet[1] = 0xF4; // Report ID
  839. packet[2] = 0x42; // Special PS3 Controller enable commands
  840. packet[3] = 0x03;
  841. packet[4] = 0x00;
  842. packet[5] = 0x00;
  843. DBGPrintf("enable six axis\n");
  844. delay(1);
  845. btdriver_->sendL2CapCommand(packet, sizeof(packet), BluetoothController::CONTROL_SCID);
  846. }
  847. }
  848. void JoystickController::release_bluetooth()
  849. {
  850. btdevice = nullptr; // remember this way
  851. btdriver_ = nullptr;
  852. connected_ = false;
  853. special_process_required = false;
  854. }
  855. bool JoystickController::PS3Pair(uint8_t* bdaddr) {
  856. if ((joystickType_ != PS3) || !driver_) return false; // not a PS2 nor plugged into USB...
  857. /* Set the internal Bluetooth address */
  858. txbuf_[0] = 0x01;
  859. txbuf_[1] = 0x00;
  860. for(uint8_t i = 0; i < 6; i++)
  861. txbuf_[i + 2] = bdaddr[5 - i]; // Copy into buffer, has to be written reversed, so it is MSB first
  862. // 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
  863. return driver_->sendControlPacket(0x21, 9, 0x3f5, 0, 8, txbuf_);
  864. }