選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

497 行
17KB

  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. { 0x054C, 0x0268, PS3, true},
  33. { 0x054C, 0x05C4, PS4, true}, {0x054C, 0x09CC, PS4, true }
  34. };
  35. //-----------------------------------------------------------------------------
  36. void JoystickController::init()
  37. {
  38. contribute_Pipes(mypipes, sizeof(mypipes)/sizeof(Pipe_t));
  39. contribute_Transfers(mytransfers, sizeof(mytransfers)/sizeof(Transfer_t));
  40. contribute_String_Buffers(mystring_bufs, sizeof(mystring_bufs)/sizeof(strbuf_t));
  41. driver_ready_for_device(this);
  42. USBHIDParser::driver_ready_for_hid_collection(this);
  43. }
  44. //-----------------------------------------------------------------------------
  45. JoystickController::joytype_t JoystickController::mapVIDPIDtoJoystickType(uint16_t idVendor, uint16_t idProduct, bool exclude_hid_devices)
  46. {
  47. for (uint8_t i = 0; i < (sizeof(pid_vid_mapping)/sizeof(pid_vid_mapping[0])); i++) {
  48. if ((idVendor == pid_vid_mapping[i].idVendor) && (idProduct == pid_vid_mapping[i].idProduct)) {
  49. println("Match PID/VID: ", i, DEC);
  50. if (exclude_hid_devices && pid_vid_mapping[i].hidDevice) return UNKNOWN;
  51. return pid_vid_mapping[i].joyType;
  52. }
  53. }
  54. return UNKNOWN; // Not in our list
  55. }
  56. //*****************************************************************************
  57. // Some simple query functions depend on which interface we are using...
  58. //*****************************************************************************
  59. uint16_t JoystickController::idVendor()
  60. {
  61. if (device != nullptr) return device->idVendor;
  62. if (mydevice != nullptr) return mydevice->idVendor;
  63. return 0;
  64. }
  65. uint16_t JoystickController::idProduct()
  66. {
  67. if (device != nullptr) return device->idProduct;
  68. if (mydevice != nullptr) return mydevice->idProduct;
  69. return 0;
  70. }
  71. const uint8_t *JoystickController::manufacturer()
  72. {
  73. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  74. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_MAN]];
  75. return nullptr;
  76. }
  77. const uint8_t *JoystickController::product()
  78. {
  79. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  80. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_PROD]];
  81. return nullptr;
  82. }
  83. const uint8_t *JoystickController::serialNumber()
  84. {
  85. if ((device != nullptr) && (device->strbuf != nullptr)) return &device->strbuf->buffer[device->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  86. if ((mydevice != nullptr) && (mydevice->strbuf != nullptr)) return &mydevice->strbuf->buffer[mydevice->strbuf->iStrings[strbuf_t::STR_ID_SERIAL]];
  87. return nullptr;
  88. }
  89. bool JoystickController::setRumble(uint8_t lValue, uint8_t rValue, uint8_t timeout)
  90. {
  91. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  92. // joystick types.
  93. rumble_lValue_ = lValue;
  94. rumble_rValue_ = rValue;
  95. rumble_timeout_ = timeout;
  96. switch (joystickType) {
  97. default:
  98. break;
  99. case PS3:
  100. return transmitPS3UserFeedbackMsg();
  101. case PS4:
  102. return transmitPS4UserFeedbackMsg();
  103. case XBOXONE:
  104. // Lets try sending a request to the XBox 1.
  105. txbuf_[0] = 0x9;
  106. txbuf_[1] = 0x8;
  107. txbuf_[2] = 0x0;
  108. txbuf_[3] = 0x08; // Substructure (what substructure rest of this packet has)
  109. txbuf_[4] = 0x00; // Mode
  110. txbuf_[5] = 0x0f; // Rumble mask (what motors are activated) (0000 lT rT L R)
  111. txbuf_[6] = 0x0; // lT force
  112. txbuf_[7] = 0x0; // rT force
  113. txbuf_[8] = lValue; // L force
  114. txbuf_[9] = rValue; // R force
  115. txbuf_[10] = 0x80; // Length of pulse
  116. txbuf_[11] = 0x00; // Period between pulses
  117. if (!queue_Data_Transfer(txpipe_, txbuf_, 12, this)) {
  118. println("XBoxOne rumble transfer fail");
  119. }
  120. return true; //
  121. }
  122. return false;
  123. }
  124. bool JoystickController::setLEDs(uint8_t lr, uint8_t lg, uint8_t lb)
  125. {
  126. // Need to know which joystick we are on. Start off with XBox support - maybe need to add some enum value for the known
  127. // joystick types.
  128. if ((leds_[0] != lr) || (leds_[1] != lg) || (leds_[2] != lb)) {
  129. leds_[0] = lr;
  130. leds_[1] = lg;
  131. leds_[2] = lb;
  132. switch (joystickType) {
  133. case PS3:
  134. return transmitPS3UserFeedbackMsg();
  135. case PS4:
  136. return transmitPS4UserFeedbackMsg();
  137. default:
  138. return false;
  139. }
  140. }
  141. return false;
  142. }
  143. bool JoystickController::transmitPS4UserFeedbackMsg() {
  144. if (!driver_) return false;
  145. uint8_t packet[32];
  146. memset(packet, 0, sizeof(packet));
  147. packet[0] = 0x05; // Report ID
  148. packet[1]= 0xFF;
  149. packet[4] = rumble_lValue_; // Small Rumble
  150. packet[5] = rumble_rValue_; // Big rumble
  151. packet[6] = leds_[0]; // RGB value
  152. packet[7] = leds_[1];
  153. packet[8] = leds_[2];
  154. // 9, 10 flash ON, OFF times in 100ths of sedond? 2.5 seconds = 255
  155. Serial.printf("Joystick update Rumble/LEDs");
  156. return driver_->sendPacket(packet, 32);
  157. }
  158. static const uint8_t PS3_USER_FEEDBACK_INIT[] = {
  159. 0x00, 0x00, 0x00, 0x00, 0x00,
  160. 0x00, 0x00, 0x00, 0x00, 0x00,
  161. 0xff, 0x27, 0x10, 0x00, 0x32,
  162. 0xff, 0x27, 0x10, 0x00, 0x32,
  163. 0xff, 0x27, 0x10, 0x00, 0x32,
  164. 0xff, 0x27, 0x10, 0x00, 0x32,
  165. 0x00, 0x00, 0x00, 0x00, 0x00,
  166. 0x00, 0x00, 0x00, 0x00, 0x00,
  167. 0x00, 0x00, 0x00, 0x00, 0x00,
  168. 0x00, 0x00, 0x00 };
  169. bool JoystickController::transmitPS3UserFeedbackMsg() {
  170. if (!driver_) return false;
  171. memcpy(txbuf_, PS3_USER_FEEDBACK_INIT, 48);
  172. txbuf_[1] = rumble_lValue_? rumble_timeout_ : 0;
  173. txbuf_[2] = rumble_lValue_; // Small Rumble
  174. txbuf_[3] = rumble_rValue_? rumble_timeout_ : 0;
  175. txbuf_[4] = rumble_rValue_; // Big rumble
  176. txbuf_[9] = leds_[0] << 1; // RGB value
  177. //Serial.printf("\nJoystick update Rumble/LEDs %d %d %d %d %d\n", txbuf_[1], txbuf_[2], txbuf_[3], txbuf_[4], txbuf_[9]);
  178. return driver_->sendControlPacket(0x21, 9, 0x201, 0, 48, txbuf_);
  179. }
  180. //*****************************************************************************
  181. // Support for Joysticks that Use HID data.
  182. //*****************************************************************************
  183. hidclaim_t JoystickController::claim_collection(USBHIDParser *driver, Device_t *dev, uint32_t topusage)
  184. {
  185. // only claim Desktop/Joystick and Desktop/Gamepad
  186. if (topusage != 0x10004 && topusage != 0x10005) return CLAIM_NO;
  187. // only claim from one physical device
  188. if (mydevice != NULL && dev != mydevice) return CLAIM_NO;
  189. mydevice = dev;
  190. collections_claimed++;
  191. anychange = true; // always report values on first read
  192. driver_ = driver; // remember the driver.
  193. driver_->setTXBuffers(txbuf_, nullptr, sizeof(txbuf_));
  194. // Lets see if we know what type of joystick this is. That is, is it a PS3 or PS4 or ...
  195. joystickType = mapVIDPIDtoJoystickType(mydevice->idVendor, mydevice->idProduct, false);
  196. switch (joystickType) {
  197. case PS3:
  198. additional_axis_usage_page_ = 0x1;
  199. additional_axis_usage_start_ = 0x100;
  200. additional_axis_usage_count_ = 39;
  201. axis_change_notify_mask_ = (uint64_t)-1; // Start off assume all bits
  202. break;
  203. case PS4:
  204. additional_axis_usage_page_ = 0xFF00;
  205. additional_axis_usage_start_ = 0x21;
  206. additional_axis_usage_count_ = 54;
  207. axis_change_notify_mask_ = (uint64_t)0xfffffffffffff3ffl; // Start off assume all bits - 10 and 11
  208. break;
  209. default:
  210. additional_axis_usage_page_ = 0;
  211. additional_axis_usage_start_ = 0;
  212. additional_axis_usage_count_ = 0;
  213. axis_change_notify_mask_ = 0x3ff; // Start off assume only the 10 bits...
  214. }
  215. Serial.printf("Claim Additional axis: %x %x %d\n", additional_axis_usage_page_, additional_axis_usage_start_, additional_axis_usage_count_);
  216. return CLAIM_REPORT;
  217. }
  218. void JoystickController::disconnect_collection(Device_t *dev)
  219. {
  220. if (--collections_claimed == 0) {
  221. mydevice = NULL;
  222. driver_ = nullptr;
  223. axis_mask_ = 0;
  224. axis_changed_mask_ = 0;
  225. }
  226. }
  227. void JoystickController::hid_input_begin(uint32_t topusage, uint32_t type, int lgmin, int lgmax)
  228. {
  229. // TODO: set up translation from logical min/max to consistent 16 bit scale
  230. }
  231. void JoystickController::hid_input_data(uint32_t usage, int32_t value)
  232. {
  233. //Serial.printf("Joystick: usage=%X, value=%d\n", usage, value);
  234. uint32_t usage_page = usage >> 16;
  235. usage &= 0xFFFF;
  236. if (usage_page == 9 && usage >= 1 && usage <= 32) {
  237. uint32_t bit = 1 << (usage -1);
  238. if (value == 0) {
  239. if (buttons & bit) {
  240. buttons &= ~bit;
  241. anychange = true;
  242. }
  243. } else {
  244. if (!(buttons & bit)) {
  245. buttons |= bit;
  246. anychange = true;
  247. }
  248. }
  249. } else if (usage_page == 1 && usage >= 0x30 && usage <= 0x39) {
  250. // TODO: need scaling of value to consistent API, 16 bit signed?
  251. // TODO: many joysticks repeat slider usage. Detect & map to axis?
  252. uint32_t i = usage - 0x30;
  253. axis_mask_ |= (1 << i); // Keep record of which axis we have data on.
  254. if (axis[i] != value) {
  255. axis[i] = value;
  256. axis_changed_mask_ |= (1 << i);
  257. if (axis_changed_mask_ & axis_change_notify_mask_)
  258. anychange = true;
  259. }
  260. } else if (usage_page == additional_axis_usage_page_) {
  261. // see if the usage is witin range.
  262. //Serial.printf("UP: usage_page=%x usage=%x User: %x %d\n", usage_page, usage, user_buttons_usage_start, user_buttons_count_);
  263. if ((usage >= additional_axis_usage_start_) && (usage < (additional_axis_usage_start_ + additional_axis_usage_count_))) {
  264. // We are in the user range.
  265. uint16_t usage_index = usage - additional_axis_usage_start_ + STANDARD_AXIS_COUNT;
  266. if (usage_index < (sizeof(axis)/sizeof(axis[0]))) {
  267. if (axis[usage_index] != value) {
  268. axis[usage_index] = value;
  269. if (usage_index > 63) usage_index = 63; // don't overflow our mask
  270. axis_changed_mask_ |= ((uint64_t)1 << usage_index); // Keep track of which ones changed.
  271. if (axis_changed_mask_ & axis_change_notify_mask_)
  272. anychange = true; // We have changes...
  273. }
  274. axis_mask_ |= ((uint64_t)1 << usage_index); // Keep record of which axis we have data on.
  275. }
  276. //Serial.printf("UB: index=%x value=%x\n", usage_index, value);
  277. }
  278. } else {
  279. Serial.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_);
  280. }
  281. // TODO: hat switch?
  282. }
  283. void JoystickController::hid_input_end()
  284. {
  285. if (anychange) {
  286. joystickEvent = true;
  287. }
  288. }
  289. bool JoystickController::hid_process_out_data(const Transfer_t *transfer)
  290. {
  291. Serial.printf("JoystickController::hid_process_out_data\n");
  292. return true;
  293. }
  294. void JoystickController::joystickDataClear() {
  295. joystickEvent = false;
  296. anychange = false;
  297. axis_changed_mask_ = 0;
  298. axis_mask_ = 0;
  299. }
  300. //*****************************************************************************
  301. // Support for Joysticks that are class specific and do not use HID
  302. // Example: XBox One controller.
  303. //*****************************************************************************
  304. static uint8_t start_input[] = {0x05, 0x20, 0x00, 0x01, 0x00};
  305. bool JoystickController::claim(Device_t *dev, int type, const uint8_t *descriptors, uint32_t len)
  306. {
  307. println("JoystickController claim this=", (uint32_t)this, HEX);
  308. // only claim at device level
  309. if (type != 0) return false;
  310. print_hexbytes(descriptors, len);
  311. JoystickController::joytype_t jtype = mapVIDPIDtoJoystickType(dev->idVendor, dev->idProduct, true);
  312. println("Jtype=", (uint8_t)jtype, DEC);
  313. if (jtype == UNKNOWN)
  314. return false;
  315. // 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...
  316. // 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
  317. // Lets do some verifications to make sure.
  318. if (len < 9+7+7) return false;
  319. uint32_t count_end_points = descriptors[4];
  320. if (count_end_points < 2) return false;
  321. if (descriptors[5] != 0xff) return false; // bInterfaceClass, 3 = HID
  322. uint32_t rxep = 0;
  323. uint32_t txep = 0;
  324. rx_size_ = 0;
  325. tx_size_ = 0;
  326. uint32_t descriptor_index = 9;
  327. while (count_end_points-- && ((rxep == 0) || txep == 0)) {
  328. if (descriptors[descriptor_index] != 7) return false; // length 7
  329. if (descriptors[descriptor_index+1] != 5) return false; // ep desc
  330. if ((descriptors[descriptor_index+3] == 3) // Type 3...
  331. && (descriptors[descriptor_index+4] <= 64)
  332. && (descriptors[descriptor_index+5] == 0)) {
  333. // have a bulk EP size
  334. if (descriptors[descriptor_index+2] & 0x80 ) {
  335. rxep = descriptors[descriptor_index+2];
  336. rx_size_ = descriptors[descriptor_index+4];
  337. } else {
  338. txep = descriptors[descriptor_index+2];
  339. tx_size_ = descriptors[descriptor_index+4];
  340. }
  341. }
  342. descriptor_index += 7; // setup to look at next one...
  343. }
  344. if ((rxep == 0) || (txep == 0)) return false; // did not find two end points.
  345. print("JoystickController, rxep=", rxep & 15);
  346. print("(", rx_size_);
  347. print("), txep=", txep);
  348. print("(", tx_size_);
  349. println(")");
  350. rxpipe_ = new_Pipe(dev, 2, rxep & 15, 1, rx_size_);
  351. if (!rxpipe_) return false;
  352. txpipe_ = new_Pipe(dev, 2, txep, 0, tx_size_);
  353. if (!txpipe_) {
  354. //free_Pipe(rxpipe_);
  355. return false;
  356. }
  357. rxpipe_->callback_function = rx_callback;
  358. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  359. txpipe_->callback_function = tx_callback;
  360. queue_Data_Transfer(txpipe_, start_input, sizeof(start_input), this);
  361. memset(axis, 0, sizeof(axis)); // clear out any data.
  362. joystickType = jtype; // remember we are an XBox One.
  363. return true;
  364. }
  365. void JoystickController::control(const Transfer_t *transfer)
  366. {
  367. }
  368. /************************************************************/
  369. // Interrupt-based Data Movement
  370. /************************************************************/
  371. void JoystickController::rx_callback(const Transfer_t *transfer)
  372. {
  373. if (!transfer->driver) return;
  374. ((JoystickController *)(transfer->driver))->rx_data(transfer);
  375. }
  376. void JoystickController::tx_callback(const Transfer_t *transfer)
  377. {
  378. if (!transfer->driver) return;
  379. ((JoystickController *)(transfer->driver))->tx_data(transfer);
  380. }
  381. /************************************************************/
  382. // Interrupt-based Data Movement
  383. // XBox one input data when type == 0x20
  384. // Information came from several places on the web including:
  385. // https://github.com/quantus/xbox-one-controller-protocol
  386. /************************************************************/
  387. typedef struct {
  388. uint8_t type;
  389. uint8_t const_0;
  390. uint16_t id;
  391. // From online references button order:
  392. // sync, dummy, start, back, a, b, x, y
  393. // dpad up, down left, right
  394. // lb, rb, left stick, right stick
  395. // Axis:
  396. // lt, rt, lx, xy, rx, ry
  397. //
  398. uint16_t buttons;
  399. int16_t axis[6];
  400. } xbox1data20_t;
  401. static const uint8_t xbox_axis_order_mapping[] = {4, 5, 0, 1, 2, 3};
  402. void JoystickController::rx_data(const Transfer_t *transfer)
  403. {
  404. // print("JoystickController::rx_data: ");
  405. // print_hexbytes((uint8_t*)transfer->buffer, transfer->length);
  406. axis_mask_ = 0x3f;
  407. axis_changed_mask_ = 0; // assume none for now
  408. xbox1data20_t *xb1d = (xbox1data20_t *)transfer->buffer;
  409. if ((xb1d->type == 0x20) && (transfer->length >= sizeof (xbox1data20_t))) {
  410. // We have a data transfer. Lets see what is new...
  411. if (xb1d->buttons != buttons) {
  412. buttons = xb1d->buttons;
  413. anychange = true;
  414. }
  415. for (uint8_t i = 0; i < sizeof (xbox_axis_order_mapping); i++) {
  416. // The first two values were unsigned.
  417. int axis_value = (i < 2)? (int)(uint16_t)xb1d->axis[i] : xb1d->axis[i];
  418. if (axis_value != axis[xbox_axis_order_mapping[i]]) {
  419. axis[xbox_axis_order_mapping[i]] = axis_value;
  420. anychange = true;
  421. }
  422. }
  423. joystickEvent = true;
  424. }
  425. queue_Data_Transfer(rxpipe_, rxbuf_, rx_size_, this);
  426. }
  427. void JoystickController::tx_data(const Transfer_t *transfer)
  428. {
  429. }
  430. void JoystickController::disconnect()
  431. {
  432. axis_mask_ = 0;
  433. axis_changed_mask_ = 0;
  434. // TODO: free resources
  435. }