Based on PS3_BlueUSB with reference to http://blog.goo.ne.jp/roboz80/e/10e7bf38d3a63b996ca2894e9fb5e3b6

Dependencies:   TextLCD mbed

Committer:
kenbumono
Date:
Tue Jul 05 08:25:40 2011 +0000
Revision:
0:44619612f575

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenbumono 0:44619612f575 1 /*
kenbumono 0:44619612f575 2 Copyright (c) 2010 Peter Barrett
kenbumono 0:44619612f575 3
kenbumono 0:44619612f575 4 Permission is hereby granted, free of charge, to any person obtaining a copy
kenbumono 0:44619612f575 5 of this software and associated documentation files (the "Software"), to deal
kenbumono 0:44619612f575 6 in the Software without restriction, including without limitation the rights
kenbumono 0:44619612f575 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
kenbumono 0:44619612f575 8 copies of the Software, and to permit persons to whom the Software is
kenbumono 0:44619612f575 9 furnished to do so, subject to the following conditions:
kenbumono 0:44619612f575 10
kenbumono 0:44619612f575 11 The above copyright notice and this permission notice shall be included in
kenbumono 0:44619612f575 12 all copies or substantial portions of the Software.
kenbumono 0:44619612f575 13
kenbumono 0:44619612f575 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
kenbumono 0:44619612f575 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
kenbumono 0:44619612f575 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
kenbumono 0:44619612f575 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
kenbumono 0:44619612f575 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenbumono 0:44619612f575 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
kenbumono 0:44619612f575 20 THE SOFTWARE.
kenbumono 0:44619612f575 21 */
kenbumono 0:44619612f575 22
kenbumono 0:44619612f575 23 /*
kenbumono 0:44619612f575 24 Tue Apr 26 2011 Bart Janssens: added PS3 USB support
kenbumono 0:44619612f575 25 */
kenbumono 0:44619612f575 26
kenbumono 0:44619612f575 27 #include <stdio.h>
kenbumono 0:44619612f575 28 #include <stdlib.h>
kenbumono 0:44619612f575 29 #include <stdio.h>
kenbumono 0:44619612f575 30 #include <string.h>
kenbumono 0:44619612f575 31
kenbumono 0:44619612f575 32 #include "USBHost.h"
kenbumono 0:44619612f575 33 #include "Utils.h"
kenbumono 0:44619612f575 34 #include "ps3.h"
kenbumono 0:44619612f575 35
kenbumono 0:44619612f575 36 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
kenbumono 0:44619612f575 37 #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1)
kenbumono 0:44619612f575 38 #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2)
kenbumono 0:44619612f575 39 //#define AUTO_PS3 AUTOEVT(CLASS_HID,0,0)
kenbumono 0:44619612f575 40
kenbumono 0:44619612f575 41 u8 auto_mouse[4]; // buttons,dx,dy,scroll
kenbumono 0:44619612f575 42 u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6
kenbumono 0:44619612f575 43 u8 auto_joystick[4]; // x,y,buttons,throttle
kenbumono 0:44619612f575 44 //u8 auto_ps3[48];
kenbumono 0:44619612f575 45
kenbumono 0:44619612f575 46
kenbumono 0:44619612f575 47
kenbumono 0:44619612f575 48
kenbumono 0:44619612f575 49 void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
kenbumono 0:44619612f575 50 {
kenbumono 0:44619612f575 51 int evt = (int)userData;
kenbumono 0:44619612f575 52 switch (evt)
kenbumono 0:44619612f575 53 {
kenbumono 0:44619612f575 54 case AUTO_KEYBOARD:
kenbumono 0:44619612f575 55 printf("AUTO_KEYBOARD ");
kenbumono 0:44619612f575 56 break;
kenbumono 0:44619612f575 57 case AUTO_MOUSE:
kenbumono 0:44619612f575 58 printf("AUTO_MOUSE ");
kenbumono 0:44619612f575 59 break;
kenbumono 0:44619612f575 60 // case AUTO_PS3:
kenbumono 0:44619612f575 61 // printf("AUTO_PS3 ");
kenbumono 0:44619612f575 62 // ParsePs3Report(data,len);
kenbumono 0:44619612f575 63 // break;
kenbumono 0:44619612f575 64 default:
kenbumono 0:44619612f575 65 printf("HUH ");
kenbumono 0:44619612f575 66 }
kenbumono 0:44619612f575 67 //printfBytes("data",data,len);
kenbumono 0:44619612f575 68 USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData);
kenbumono 0:44619612f575 69 }
kenbumono 0:44619612f575 70
kenbumono 0:44619612f575 71 // Establish transfers for interrupt events
kenbumono 0:44619612f575 72 void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed)
kenbumono 0:44619612f575 73 {
kenbumono 0:44619612f575 74 printf("message from endpoint %02X\r\n",ed->bEndpointAddress);
kenbumono 0:44619612f575 75 printf("Class Sub Proto: %02X %02X %02X\r\n",id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
kenbumono 0:44619612f575 76 //if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80))
kenbumono 0:44619612f575 77 // return;
kenbumono 0:44619612f575 78
kenbumono 0:44619612f575 79 // Make automatic interrupt enpoints for known devices
kenbumono 0:44619612f575 80 u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
kenbumono 0:44619612f575 81 printf("Evt: %08X \r\n",evt);
kenbumono 0:44619612f575 82 u8* dst = 0;
kenbumono 0:44619612f575 83 int len;
kenbumono 0:44619612f575 84 switch (evt)
kenbumono 0:44619612f575 85 {
kenbumono 0:44619612f575 86 case AUTO_MOUSE:
kenbumono 0:44619612f575 87 dst = auto_mouse;
kenbumono 0:44619612f575 88 len = sizeof(auto_mouse);
kenbumono 0:44619612f575 89 break;
kenbumono 0:44619612f575 90 case AUTO_KEYBOARD:
kenbumono 0:44619612f575 91 dst = auto_keyboard;
kenbumono 0:44619612f575 92 len = sizeof(auto_keyboard);
kenbumono 0:44619612f575 93 break;
kenbumono 0:44619612f575 94 // case AUTO_PS3:
kenbumono 0:44619612f575 95 // printf("PS3 event ? \r\n");
kenbumono 0:44619612f575 96 // dst = auto_ps3;
kenbumono 0:44619612f575 97 // len = sizeof(auto_ps3);
kenbumono 0:44619612f575 98 default:
kenbumono 0:44619612f575 99 printf("Interrupt endpoint %02X %08X\r\n",ed->bEndpointAddress,evt);
kenbumono 0:44619612f575 100 break;
kenbumono 0:44619612f575 101 }
kenbumono 0:44619612f575 102 if (dst)
kenbumono 0:44619612f575 103 {
kenbumono 0:44619612f575 104 printf("Auto Event for %02X %08X\r\n",ed->bEndpointAddress,evt);
kenbumono 0:44619612f575 105 USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt);
kenbumono 0:44619612f575 106 }
kenbumono 0:44619612f575 107 }
kenbumono 0:44619612f575 108
kenbumono 0:44619612f575 109 void PrintString(int device, int i)
kenbumono 0:44619612f575 110 {
kenbumono 0:44619612f575 111 u8 buffer[256];
kenbumono 0:44619612f575 112 int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255);
kenbumono 0:44619612f575 113 if (le < 0)
kenbumono 0:44619612f575 114 return;
kenbumono 0:44619612f575 115 char* dst = (char*)buffer;
kenbumono 0:44619612f575 116 for (int j = 2; j < le; j += 2)
kenbumono 0:44619612f575 117 *dst++ = buffer[j];
kenbumono 0:44619612f575 118 *dst = 0;
kenbumono 0:44619612f575 119 printf("%d:%s\r\n",i,(const char*)buffer);
kenbumono 0:44619612f575 120 }
kenbumono 0:44619612f575 121
kenbumono 0:44619612f575 122 // Walk descriptors and create endpoints for a given device
kenbumono 0:44619612f575 123 int StartAutoEvent(int device, int configuration, int interfaceNumber)
kenbumono 0:44619612f575 124 {
kenbumono 0:44619612f575 125
kenbumono 0:44619612f575 126 printf("StartAutoEvent \r\n");
kenbumono 0:44619612f575 127
kenbumono 0:44619612f575 128 u8 buffer[255];
kenbumono 0:44619612f575 129 int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
kenbumono 0:44619612f575 130 if (err < 0)
kenbumono 0:44619612f575 131 return err;
kenbumono 0:44619612f575 132
kenbumono 0:44619612f575 133 int len = buffer[2] | (buffer[3] << 8);
kenbumono 0:44619612f575 134 u8* d = buffer;
kenbumono 0:44619612f575 135 u8* end = d + len;
kenbumono 0:44619612f575 136 while (d < end)
kenbumono 0:44619612f575 137 {
kenbumono 0:44619612f575 138 if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
kenbumono 0:44619612f575 139 {
kenbumono 0:44619612f575 140 InterfaceDescriptor* id = (InterfaceDescriptor*)d;
kenbumono 0:44619612f575 141 if (id->bInterfaceNumber == interfaceNumber)
kenbumono 0:44619612f575 142 {
kenbumono 0:44619612f575 143 d += d[0];
kenbumono 0:44619612f575 144 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
kenbumono 0:44619612f575 145 {
kenbumono 0:44619612f575 146 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
kenbumono 0:44619612f575 147 AddAutoEvent(device,id,(EndpointDescriptor*)d);
kenbumono 0:44619612f575 148 d += d[0];
kenbumono 0:44619612f575 149 }
kenbumono 0:44619612f575 150 }
kenbumono 0:44619612f575 151 }
kenbumono 0:44619612f575 152 d += d[0];
kenbumono 0:44619612f575 153 }
kenbumono 0:44619612f575 154 return 0;
kenbumono 0:44619612f575 155 }
kenbumono 0:44619612f575 156
kenbumono 0:44619612f575 157 /*
kenbumono 0:44619612f575 158 int StartPS3Event(int device, int configuration, int interfaceNumber)
kenbumono 0:44619612f575 159 {
kenbumono 0:44619612f575 160
kenbumono 0:44619612f575 161 printf("StartPS3Event \r\n");
kenbumono 0:44619612f575 162
kenbumono 0:44619612f575 163 EndpointDescriptor* ep;
kenbumono 0:44619612f575 164
kenbumono 0:44619612f575 165 u8 buf[4];
kenbumono 0:44619612f575 166 buf[0] = 0x42;
kenbumono 0:44619612f575 167 buf[1] = 0x0c;
kenbumono 0:44619612f575 168 buf[2] = 0x00;
kenbumono 0:44619612f575 169 buf[3] = 0x00;
kenbumono 0:44619612f575 170
kenbumono 0:44619612f575 171 u8 buf2[8];
kenbumono 0:44619612f575 172 u8 buf3[8];
kenbumono 0:44619612f575 173
kenbumono 0:44619612f575 174 buf2[0] = 0x01;
kenbumono 0:44619612f575 175 buf2[1] = 0x00;
kenbumono 0:44619612f575 176 buf2[2] = 0x00;
kenbumono 0:44619612f575 177 buf2[3] = 0x02;
kenbumono 0:44619612f575 178 buf2[4] = 0x72;
kenbumono 0:44619612f575 179 buf2[5] = 0xAD;
kenbumono 0:44619612f575 180 buf2[6] = 0xF3;
kenbumono 0:44619612f575 181 buf2[7] = 0x5B;
kenbumono 0:44619612f575 182
kenbumono 0:44619612f575 183
kenbumono 0:44619612f575 184
kenbumono 0:44619612f575 185
kenbumono 0:44619612f575 186 int result;
kenbumono 0:44619612f575 187 int err;
kenbumono 0:44619612f575 188
kenbumono 0:44619612f575 189 u8 buffer[255];
kenbumono 0:44619612f575 190 err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
kenbumono 0:44619612f575 191 if (err < 0)
kenbumono 0:44619612f575 192 return err;
kenbumono 0:44619612f575 193
kenbumono 0:44619612f575 194
kenbumono 0:44619612f575 195
kenbumono 0:44619612f575 196 //configure the device
kenbumono 0:44619612f575 197 //err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_STANDARD|RECIPIENT_DEVICE, SET_CONFIGURATION, 1, 0, 0, 0, 0, 0 );
kenbumono 0:44619612f575 198 err = SetConfiguration(device,1);
kenbumono 0:44619612f575 199 printf("set config result = %d\r\n", err);
kenbumono 0:44619612f575 200
kenbumono 0:44619612f575 201 // get Mac address
kenbumono 0:44619612f575 202 //err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_DEVICE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf3, sizeof(buf3), 0, 0 );
kenbumono 0:44619612f575 203 //printf("get Mac to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n", buf3[2], buf3[3], buf3[4], buf3[5], buf3[6], buf3[7], err);
kenbumono 0:44619612f575 204
kenbumono 0:44619612f575 205 // set Mac address
kenbumono 0:44619612f575 206 err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f5, 0, buf2, sizeof(buf2), 0, 0 );
kenbumono 0:44619612f575 207 printf("set Mac to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n", buf2[2], buf2[3], buf2[4], buf2[5], buf2[6], buf2[7], err);
kenbumono 0:44619612f575 208
kenbumono 0:44619612f575 209 // get Mac address
kenbumono 0:44619612f575 210 //err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_DEVICE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf3, sizeof(buf3), 0, 0 );
kenbumono 0:44619612f575 211 //printf("get Mac to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n", buf3[2], buf3[3], buf3[4], buf3[5], buf3[6], buf3[7], err);
kenbumono 0:44619612f575 212
kenbumono 0:44619612f575 213 err = USBControlTransfer(device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f4,0, buf, sizeof(buf), 0, 0 );
kenbumono 0:44619612f575 214 printf("set report result = %d\r\n", err);
kenbumono 0:44619612f575 215 //USBTransfer(device,0,DEVICE_TO_HOST,buf,sizeof(buf),0,0);
kenbumono 0:44619612f575 216
kenbumono 0:44619612f575 217 int len = buffer[2] | (buffer[3] << 8);
kenbumono 0:44619612f575 218 u8* d = buffer;
kenbumono 0:44619612f575 219 u8* end = d + len;
kenbumono 0:44619612f575 220 while (d < end)
kenbumono 0:44619612f575 221 {
kenbumono 0:44619612f575 222 if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
kenbumono 0:44619612f575 223 {
kenbumono 0:44619612f575 224 InterfaceDescriptor* id = (InterfaceDescriptor*)d;
kenbumono 0:44619612f575 225 if (id->bInterfaceNumber == interfaceNumber)
kenbumono 0:44619612f575 226 {
kenbumono 0:44619612f575 227 d += d[0];
kenbumono 0:44619612f575 228 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
kenbumono 0:44619612f575 229 {
kenbumono 0:44619612f575 230 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
kenbumono 0:44619612f575 231 ep = (EndpointDescriptor*)d;
kenbumono 0:44619612f575 232
kenbumono 0:44619612f575 233 if (ep->bEndpointAddress == 0x02) {
kenbumono 0:44619612f575 234 printf("PS3 input endpoint (0x02) found\r\n");
kenbumono 0:44619612f575 235
kenbumono 0:44619612f575 236 }
kenbumono 0:44619612f575 237 if (ep->bEndpointAddress == 0x81) {
kenbumono 0:44619612f575 238 printf("PS3 output endpoint (0x81) found\r\n");
kenbumono 0:44619612f575 239 AddAutoEvent(device,id,(EndpointDescriptor*)d);
kenbumono 0:44619612f575 240 }
kenbumono 0:44619612f575 241 d += d[0];
kenbumono 0:44619612f575 242 }
kenbumono 0:44619612f575 243 }
kenbumono 0:44619612f575 244 }
kenbumono 0:44619612f575 245 d += d[0];
kenbumono 0:44619612f575 246 }
kenbumono 0:44619612f575 247 return 0;
kenbumono 0:44619612f575 248 }
kenbumono 0:44619612f575 249 */
kenbumono 0:44619612f575 250
kenbumono 0:44619612f575 251 // Implemented in main.cpp
kenbumono 0:44619612f575 252 int OnDiskInsert(int device);
kenbumono 0:44619612f575 253
kenbumono 0:44619612f575 254 // Implemented in TestShell.cpp
kenbumono 0:44619612f575 255 int OnBluetoothInsert(int device);
kenbumono 0:44619612f575 256
kenbumono 0:44619612f575 257 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc)
kenbumono 0:44619612f575 258 {
kenbumono 0:44619612f575 259 printf("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
kenbumono 0:44619612f575 260 char s[128];
kenbumono 0:44619612f575 261 u8 my_mac[6] = {0x00, 0x1B, 0xDC, 0x04, 0xDA, 0xF0}; // mac address of my Bluetooth device
kenbumono 0:44619612f575 262
kenbumono 0:44619612f575 263 u8 buf2[6];
kenbumono 0:44619612f575 264
kenbumono 0:44619612f575 265 buf2[0] = 0x00;
kenbumono 0:44619612f575 266 buf2[1] = 0x1E;
kenbumono 0:44619612f575 267 buf2[2] = 0x35;
kenbumono 0:44619612f575 268 buf2[3] = 0x2E;
kenbumono 0:44619612f575 269 buf2[4] = 0xD5;
kenbumono 0:44619612f575 270 buf2[5] = 0x1D;
kenbumono 0:44619612f575 271
kenbumono 0:44619612f575 272
kenbumono 0:44619612f575 273 for (int i = 1; i < 3; i++)
kenbumono 0:44619612f575 274 {
kenbumono 0:44619612f575 275 if (GetString(device,i,s,sizeof(s)) < 0)
kenbumono 0:44619612f575 276 break;
kenbumono 0:44619612f575 277 printf("%d: %s\r\n",i,s);
kenbumono 0:44619612f575 278 }
kenbumono 0:44619612f575 279
kenbumono 0:44619612f575 280 switch (interfaceDesc->bInterfaceClass)
kenbumono 0:44619612f575 281 {
kenbumono 0:44619612f575 282 case CLASS_MASS_STORAGE:
kenbumono 0:44619612f575 283 if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50)
kenbumono 0:44619612f575 284 OnDiskInsert(device); // it's SCSI!
kenbumono 0:44619612f575 285 break;
kenbumono 0:44619612f575 286 case CLASS_WIRELESS_CONTROLLER:
kenbumono 0:44619612f575 287 if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01)
kenbumono 0:44619612f575 288 OnBluetoothInsert(device); // it's bluetooth!
kenbumono 0:44619612f575 289 break;
kenbumono 0:44619612f575 290 case CLASS_HID:
kenbumono 0:44619612f575 291 printf("idVendor = %04X idProduct = %04X \r\n",deviceDesc->idVendor,deviceDesc->idProduct);
kenbumono 0:44619612f575 292 //printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber);
kenbumono 0:44619612f575 293 //if (deviceDesc->idVendor == 0x054C && deviceDesc->idProduct == 0x0268) StartPS3Event(device,1,0);
kenbumono 0:44619612f575 294 if (deviceDesc->idVendor == 0x054C && deviceDesc->idProduct == 0x0268) {
kenbumono 0:44619612f575 295 Ps3USB _Ps3USB(device,1,0);
kenbumono 0:44619612f575 296
kenbumono 0:44619612f575 297 _Ps3USB.SetPair(my_mac);
kenbumono 0:44619612f575 298 _Ps3USB.Enable();
kenbumono 0:44619612f575 299 _Ps3USB.Led(1);
kenbumono 0:44619612f575 300 _Ps3USB.Rumble(0x20,0xff,0x20,0xff);
kenbumono 0:44619612f575 301 _Ps3USB.ShowPair();
kenbumono 0:44619612f575 302
kenbumono 0:44619612f575 303 }
kenbumono 0:44619612f575 304 else StartAutoEvent(device,1,0);
kenbumono 0:44619612f575 305 break;
kenbumono 0:44619612f575 306 default:
kenbumono 0:44619612f575 307 printf("Not yet supported \r\n");
kenbumono 0:44619612f575 308 //StartAutoEvent(device,1,0);
kenbumono 0:44619612f575 309 break;
kenbumono 0:44619612f575 310 }
kenbumono 0:44619612f575 311 }