You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

733 lines
25KB

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