Fork to support Mouse boot protocol and steam controllers.

Fork of USBHOST by ST

Committer:
Louise Newberry
Date:
Thu Aug 31 21:36:56 2017 +0100
Revision:
11:ba29414a455e
Parent:
8:e57ccb876952
Add control write to select boot mode as mice require this to
guarentee boot reports are selected.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 1 /* mbed USBHost Library
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 2 * Copyright (c) 2006-2013 ARM Limited
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 3 *
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 4 * Licensed under the Apache License, Version 2.0 (the "License");
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 5 * you may not use this file except in compliance with the License.
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 6 * You may obtain a copy of the License at
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 7 *
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 8 * http://www.apache.org/licenses/LICENSE-2.0
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 9 *
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 10 * Unless required by applicable law or agreed to in writing, software
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 11 * distributed under the License is distributed on an "AS IS" BASIS,
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 13 * See the License for the specific language governing permissions and
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 14 * limitations under the License.
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 15 */
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 16
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 17 #include "USBHostMouse.h"
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 18
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 19 #if USBHOST_MOUSE
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 20
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 21 USBHostMouse::USBHostMouse() {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 22 host = USBHost::getHostInst();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 23 init();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 24 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 25
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 26 void USBHostMouse::init() {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 27 dev = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 28 int_in = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 29 onUpdate = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 30 onButtonUpdate = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 31 onXUpdate = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 32 onYUpdate = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 33 onZUpdate = NULL;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 34 report_id = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 35 dev_connected = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 36 mouse_device_found = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 37 mouse_intf = -1;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 38
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 39 buttons = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 40 x = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 41 y = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 42 z = 0;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 43 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 44
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 45 bool USBHostMouse::connected() {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 46 return dev_connected;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 47 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 48
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 49 bool USBHostMouse::connect()
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 50 {
Louise Newberry 11:ba29414a455e 51 int len_listen;
Louise Newberry 11:ba29414a455e 52 uint8_t rep_buf[] =
Louise Newberry 11:ba29414a455e 53 {
Louise Newberry 11:ba29414a455e 54 0,
Louise Newberry 11:ba29414a455e 55 };
Louise Newberry 11:ba29414a455e 56
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 57 if (dev_connected) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 58 return true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 59 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 60
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 61 for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 62 if ((dev = host->getDevice(i)) != NULL) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 63
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 64 if(host->enumerate(dev, this))
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 65 break;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 66 if (mouse_device_found) {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 67 {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 68 /* As this is done in a specific thread
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 69 * this lock is taken to avoid to process the device
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 70 * disconnect in usb process during the device registering */
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 71 USBHost::Lock Lock(host);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 72 int_in = dev->getEndpoint(mouse_intf, INTERRUPT_ENDPOINT, IN);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 73 if (!int_in)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 74 break;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 75
Louise Newberry 11:ba29414a455e 76 USB_INFO("New Mouse device: VID:%04x PID:%04x [dev: %p - intf: %d %d]", dev->getVid(), dev->getPid(), dev, mouse_intf,int_in);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 77 dev->setName("Mouse", mouse_intf);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 78 host->registerDriver(dev, mouse_intf, this, &USBHostMouse::init);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 79
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 80 int_in->attach(this, &USBHostMouse::rxHandler);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 81 len_listen = int_in->getSize();
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 82 if (len_listen > sizeof(report)) {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 83 len_listen = sizeof(report);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 84 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 85 }
Louise Newberry 11:ba29414a455e 86
Louise Newberry 11:ba29414a455e 87 /* Call set protocol to select boot protocol, otherwise the device may use report mode! */
Louise Newberry 11:ba29414a455e 88 printf("ret = %d\r\n",host->controlWrite( dev, USB_HOST_TO_DEVICE | USB_REQUEST_TYPE_CLASS | USB_RECIPIENT_INTERFACE,
Louise Newberry 11:ba29414a455e 89 0x0b, 0x00, mouse_intf, rep_buf, 0));
Louise Newberry 11:ba29414a455e 90
Louise Newberry 11:ba29414a455e 91
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 92 int ret=host->interruptRead(dev, int_in, report, len_listen, false);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 93 MBED_ASSERT((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING) || (ret == USB_TYPE_FREE));
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 94 if ((ret==USB_TYPE_OK) || (ret ==USB_TYPE_PROCESSING))
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 95 dev_connected = true;
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 96 if (ret == USB_TYPE_FREE)
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 97 dev_connected = false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 98 return true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 99 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 100 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 101 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 102 init();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 103 return false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 104 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 105
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 106 void USBHostMouse::rxHandler() {
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 107 int len_listen = int_in->getLengthTransferred();
geekylou 7:cc595e523032 108 int buttons_t, x_t, y_t, z_t;
Louise Newberry 11:ba29414a455e 109 {
geekylou 7:cc595e523032 110 int index;
geekylou 7:cc595e523032 111 for (index=0;index < len_listen; index++)
geekylou 7:cc595e523032 112 {
geekylou 7:cc595e523032 113 printf("%2x ",report[index]);
geekylou 7:cc595e523032 114 }
geekylou 7:cc595e523032 115
geekylou 7:cc595e523032 116 if (index>0) printf(" %x:%x \r\n",dev->getVid(), dev->getPid());
Louise Newberry 11:ba29414a455e 117 }
geekylou 7:cc595e523032 118
geekylou 7:cc595e523032 119 // We really should be parsing the pid report but this is a good workaround
geekylou 7:cc595e523032 120 // for my wireless mouse for now!
Louise Newberry 11:ba29414a455e 121 // Or NOT! Turns out that you need to use Set Protocol to select boot protocol or
Louise Newberry 11:ba29414a455e 122 // the device may use report mode instead.
Louise Newberry 11:ba29414a455e 123 /*if (dev->getVid() == 0x62a && dev->getPid() == 0x4101)
geekylou 7:cc595e523032 124 {
geekylou 7:cc595e523032 125 buttons_t = report[1] & 0x07;
geekylou 8:e57ccb876952 126 x_t = (((int)report[3] & 0xf) << 8) + (report[2]);
geekylou 8:e57ccb876952 127 y_t = (report[3] >> 4) + ((int)report[4] << 4);
geekylou 8:e57ccb876952 128
geekylou 8:e57ccb876952 129 if (y_t & 0x800) y_t |= 0xf000; // Fix 4 highest bits of negative nos.
geekylou 8:e57ccb876952 130 if (x_t & 0x800) x_t |= 0xf000;
geekylou 8:e57ccb876952 131
geekylou 8:e57ccb876952 132 z_t = report[5];
geekylou 7:cc595e523032 133 }
Louise Newberry 11:ba29414a455e 134 else*/
geekylou 7:cc595e523032 135 {
geekylou 7:cc595e523032 136 buttons_t = report[0] & 0x07;
Louise Newberry 11:ba29414a455e 137 x_t = (int8_t)report[1];
Louise Newberry 11:ba29414a455e 138 y_t = (int8_t)report[2];
Louise Newberry 11:ba29414a455e 139 z_t = (int8_t)report[3];
geekylou 7:cc595e523032 140 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 141 if (len_listen !=0) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 142
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 143 if (onUpdate) {
geekylou 8:e57ccb876952 144 (*onUpdate)(buttons_t, x_t, y_t, z_t);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 145 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 146
geekylou 8:e57ccb876952 147 if (onButtonUpdate && (buttons != (buttons_t))) {
geekylou 8:e57ccb876952 148 (*onButtonUpdate)(buttons_t);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 149 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 150
geekylou 8:e57ccb876952 151 if (onXUpdate && (x != x_t)) {
geekylou 7:cc595e523032 152 (*onXUpdate)(x_t);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 153 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 154
geekylou 8:e57ccb876952 155 if (onYUpdate && (y != y_t)) {
geekylou 7:cc595e523032 156 (*onYUpdate)(y_t);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 157 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 158
geekylou 8:e57ccb876952 159 if (onZUpdate && (z != z_t)) {
geekylou 7:cc595e523032 160 (*onZUpdate)(z_t);
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 161 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 162
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 163 // update mouse state
geekylou 7:cc595e523032 164 buttons = buttons_t;
geekylou 7:cc595e523032 165 x = x_t;
geekylou 7:cc595e523032 166 y = y_t;
geekylou 7:cc595e523032 167 z = z_t;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 168 }
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 169 /* set again the maximum value */
frq08711@LMECWL0871.LME.ST.COM 5:fc157e6bd5a5 170 len_listen = int_in->getSize();
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 171
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 172 if (len_listen > sizeof(report)) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 173 len_listen = sizeof(report);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 174 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 175
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 176 if (dev)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 177 host->interruptRead(dev, int_in, report, len_listen, false);
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 178 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 179
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 180 /*virtual*/ void USBHostMouse::setVidPid(uint16_t vid, uint16_t pid)
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 181 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 182 // we don't check VID/PID for mouse driver
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 183 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 184
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 185 /*virtual*/ bool USBHostMouse::parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) //Must return true if the interface should be parsed
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 186 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 187 if ((mouse_intf == -1) &&
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 188 (intf_class == HID_CLASS) &&
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 189 (intf_subclass == 0x01) &&
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 190 (intf_protocol == 0x02)) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 191 mouse_intf = intf_nb;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 192 return true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 193 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 194 return false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 195 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 196
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 197 /*virtual*/ bool USBHostMouse::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 198 {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 199 if (intf_nb == mouse_intf) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 200 if (type == INTERRUPT_ENDPOINT && dir == IN) {
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 201 mouse_device_found = true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 202 return true;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 203 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 204 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 205 return false;
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 206 }
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 207
frq08711@LMECWL0871.LME.ST.COM 1:ab240722d7ef 208 #endif