Dependents:   Test_Wiimote

Committer:
bediyap
Date:
Sat Dec 17 06:59:19 2011 +0000
Revision:
3:37e5ebd509ea
Parent:
2:5c2bfbd63297
disable serial
todo:
auto detect wii disconnection due to battery failure ...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bediyap 0:f6f434d9a03a 1
bediyap 0:f6f434d9a03a 2 /*
bediyap 0:f6f434d9a03a 3 Copyright (c) 2010 Peter Barrett
bediyap 0:f6f434d9a03a 4
bediyap 0:f6f434d9a03a 5 Permission is hereby granted, free of charge, to any person obtaining a copy
bediyap 0:f6f434d9a03a 6 of this software and associated documentation files (the "Software"), to deal
bediyap 0:f6f434d9a03a 7 in the Software without restriction, including without limitation the rights
bediyap 0:f6f434d9a03a 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
bediyap 0:f6f434d9a03a 9 copies of the Software, and to permit persons to whom the Software is
bediyap 0:f6f434d9a03a 10 furnished to do so, subject to the following conditions:
bediyap 0:f6f434d9a03a 11
bediyap 0:f6f434d9a03a 12 The above copyright notice and this permission notice shall be included in
bediyap 0:f6f434d9a03a 13 all copies or substantial portions of the Software.
bediyap 0:f6f434d9a03a 14
bediyap 0:f6f434d9a03a 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bediyap 0:f6f434d9a03a 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bediyap 0:f6f434d9a03a 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
bediyap 0:f6f434d9a03a 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bediyap 0:f6f434d9a03a 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bediyap 0:f6f434d9a03a 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
bediyap 0:f6f434d9a03a 21 THE SOFTWARE.
bediyap 0:f6f434d9a03a 22 */
bediyap 0:f6f434d9a03a 23
bediyap 0:f6f434d9a03a 24 #include "mbed.h"
bediyap 0:f6f434d9a03a 25 #include "USBHost.h"
bediyap 0:f6f434d9a03a 26 #include "Utils.h"
bediyap 0:f6f434d9a03a 27
bediyap 0:f6f434d9a03a 28 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
bediyap 0:f6f434d9a03a 29 #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1)
bediyap 0:f6f434d9a03a 30 #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2)
bediyap 0:f6f434d9a03a 31
bediyap 0:f6f434d9a03a 32 u8 auto_mouse[4]; // buttons,dx,dy,scroll
bediyap 0:f6f434d9a03a 33 u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6
bediyap 0:f6f434d9a03a 34 u8 auto_joystick[4]; // x,y,buttons,throttle
bediyap 0:f6f434d9a03a 35
bediyap 0:f6f434d9a03a 36 void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
bediyap 0:f6f434d9a03a 37 {
bediyap 0:f6f434d9a03a 38 int evt = (int)userData;
bediyap 0:f6f434d9a03a 39 switch (evt)
bediyap 0:f6f434d9a03a 40 {
bediyap 0:f6f434d9a03a 41 case AUTO_KEYBOARD:
bediyap 2:5c2bfbd63297 42 #ifdef DEBUG_WII
bediyap 0:f6f434d9a03a 43 printf("AUTO_KEYBOARD ");
bediyap 2:5c2bfbd63297 44 #endif
bediyap 0:f6f434d9a03a 45 break;
bediyap 0:f6f434d9a03a 46 case AUTO_MOUSE:
bediyap 2:5c2bfbd63297 47 #ifdef DEBUG_WII
bediyap 0:f6f434d9a03a 48 printf("AUTO_MOUSE ");
bediyap 2:5c2bfbd63297 49 #endif
bediyap 0:f6f434d9a03a 50 break;
bediyap 0:f6f434d9a03a 51 default:
bediyap 0:f6f434d9a03a 52 printf("HUH ");
bediyap 0:f6f434d9a03a 53 }
bediyap 2:5c2bfbd63297 54 #ifdef DEBUG_WII
bediyap 0:f6f434d9a03a 55 printfBytes("data",data,len);
bediyap 2:5c2bfbd63297 56 #endif
bediyap 0:f6f434d9a03a 57 USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData);
bediyap 0:f6f434d9a03a 58 }
bediyap 0:f6f434d9a03a 59
bediyap 0:f6f434d9a03a 60 // Establish transfers for interrupt events
bediyap 0:f6f434d9a03a 61 void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed)
bediyap 0:f6f434d9a03a 62 {
bediyap 0:f6f434d9a03a 63 if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80))
bediyap 0:f6f434d9a03a 64 return;
bediyap 0:f6f434d9a03a 65
bediyap 0:f6f434d9a03a 66 // Make automatic interrupt enpoints for known devices
bediyap 0:f6f434d9a03a 67 u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
bediyap 0:f6f434d9a03a 68 u8* dst = 0;
bediyap 0:f6f434d9a03a 69 int len;
bediyap 0:f6f434d9a03a 70 switch (evt)
bediyap 0:f6f434d9a03a 71 {
bediyap 0:f6f434d9a03a 72 case AUTO_MOUSE:
bediyap 0:f6f434d9a03a 73 dst = auto_mouse;
bediyap 0:f6f434d9a03a 74 len = sizeof(auto_mouse);
bediyap 0:f6f434d9a03a 75 break;
bediyap 0:f6f434d9a03a 76 case AUTO_KEYBOARD:
bediyap 0:f6f434d9a03a 77 dst = auto_keyboard;
bediyap 0:f6f434d9a03a 78 len = sizeof(auto_keyboard);
bediyap 0:f6f434d9a03a 79 break;
bediyap 0:f6f434d9a03a 80 default:
bediyap 2:5c2bfbd63297 81 #ifdef DEBUG_WII
bediyap 0:f6f434d9a03a 82 printf("Interrupt endpoint %02X %08X\n",ed->bEndpointAddress,evt);
bediyap 2:5c2bfbd63297 83 #endif
bediyap 0:f6f434d9a03a 84 break;
bediyap 0:f6f434d9a03a 85 }
bediyap 0:f6f434d9a03a 86 if (dst)
bediyap 2:5c2bfbd63297 87 {
bediyap 2:5c2bfbd63297 88 #ifdef DEBUG_WII
bediyap 0:f6f434d9a03a 89 printf("Auto Event for %02X %08X\n",ed->bEndpointAddress,evt);
bediyap 2:5c2bfbd63297 90 #endif
bediyap 0:f6f434d9a03a 91 USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt);
bediyap 0:f6f434d9a03a 92 }
bediyap 0:f6f434d9a03a 93 }
bediyap 0:f6f434d9a03a 94
bediyap 0:f6f434d9a03a 95 void PrintString(int device, int i)
bediyap 0:f6f434d9a03a 96 {
bediyap 0:f6f434d9a03a 97 u8 buffer[256];
bediyap 0:f6f434d9a03a 98 int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255);
bediyap 0:f6f434d9a03a 99 if (le < 0)
bediyap 0:f6f434d9a03a 100 return;
bediyap 0:f6f434d9a03a 101 char* dst = (char*)buffer;
bediyap 0:f6f434d9a03a 102 for (int j = 2; j < le; j += 2)
bediyap 0:f6f434d9a03a 103 *dst++ = buffer[j];
bediyap 0:f6f434d9a03a 104 *dst = 0;
bediyap 2:5c2bfbd63297 105 #ifdef DEBUG_WII
bediyap 0:f6f434d9a03a 106 printf("%d:%s\n",i,(const char*)buffer);
bediyap 2:5c2bfbd63297 107 #endif
bediyap 0:f6f434d9a03a 108 }
bediyap 0:f6f434d9a03a 109
bediyap 0:f6f434d9a03a 110 // Walk descriptors and create endpoints for a given device
bediyap 0:f6f434d9a03a 111 int StartAutoEvent(int device, int configuration, int interfaceNumber)
bediyap 0:f6f434d9a03a 112 {
bediyap 0:f6f434d9a03a 113 u8 buffer[255];
bediyap 0:f6f434d9a03a 114 int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
bediyap 0:f6f434d9a03a 115 if (err < 0)
bediyap 0:f6f434d9a03a 116 return err;
bediyap 0:f6f434d9a03a 117
bediyap 0:f6f434d9a03a 118 int len = buffer[2] | (buffer[3] << 8);
bediyap 0:f6f434d9a03a 119 u8* d = buffer;
bediyap 0:f6f434d9a03a 120 u8* end = d + len;
bediyap 0:f6f434d9a03a 121 while (d < end)
bediyap 0:f6f434d9a03a 122 {
bediyap 0:f6f434d9a03a 123 if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
bediyap 0:f6f434d9a03a 124 {
bediyap 0:f6f434d9a03a 125 InterfaceDescriptor* id = (InterfaceDescriptor*)d;
bediyap 0:f6f434d9a03a 126 if (id->bInterfaceNumber == interfaceNumber)
bediyap 0:f6f434d9a03a 127 {
bediyap 0:f6f434d9a03a 128 d += d[0];
bediyap 0:f6f434d9a03a 129 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
bediyap 0:f6f434d9a03a 130 {
bediyap 0:f6f434d9a03a 131 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
bediyap 0:f6f434d9a03a 132 AddAutoEvent(device,id,(EndpointDescriptor*)d);
bediyap 0:f6f434d9a03a 133 d += d[0];
bediyap 0:f6f434d9a03a 134 }
bediyap 0:f6f434d9a03a 135 }
bediyap 0:f6f434d9a03a 136 }
bediyap 0:f6f434d9a03a 137 d += d[0];
bediyap 0:f6f434d9a03a 138 }
bediyap 0:f6f434d9a03a 139 return 0;
bediyap 0:f6f434d9a03a 140 }
bediyap 0:f6f434d9a03a 141
bediyap 0:f6f434d9a03a 142 // Implemented in main.cpp
bediyap 1:b9acea6cfa66 143 //int OnDiskInsert(int device);
bediyap 0:f6f434d9a03a 144
bediyap 0:f6f434d9a03a 145 // Implemented in TestShell.cpp
bediyap 0:f6f434d9a03a 146 int OnBluetoothInsert(int device);
bediyap 0:f6f434d9a03a 147
bediyap 0:f6f434d9a03a 148 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc)
bediyap 0:f6f434d9a03a 149 {
bediyap 2:5c2bfbd63297 150 #ifdef DEBUG_WII
bediyap 0:f6f434d9a03a 151 printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
bediyap 2:5c2bfbd63297 152 #endif
bediyap 0:f6f434d9a03a 153 char s[128];
bediyap 0:f6f434d9a03a 154 for (int i = 1; i < 3; i++)
bediyap 0:f6f434d9a03a 155 {
bediyap 0:f6f434d9a03a 156 if (GetString(device,i,s,sizeof(s)) < 0)
bediyap 0:f6f434d9a03a 157 break;
bediyap 2:5c2bfbd63297 158
bediyap 2:5c2bfbd63297 159 #ifdef DEBUG_WII
bediyap 0:f6f434d9a03a 160 printf("%d: %s\n",i,s);
bediyap 2:5c2bfbd63297 161 #endif
bediyap 0:f6f434d9a03a 162 }
bediyap 0:f6f434d9a03a 163
bediyap 0:f6f434d9a03a 164 switch (interfaceDesc->bInterfaceClass)
bediyap 0:f6f434d9a03a 165 {
bediyap 1:b9acea6cfa66 166 // case CLASS_MASS_STORAGE:
bediyap 1:b9acea6cfa66 167 // if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50)
bediyap 1:b9acea6cfa66 168 // OnDiskInsert(device); // it's SCSI!
bediyap 1:b9acea6cfa66 169 // break;
bediyap 0:f6f434d9a03a 170 case CLASS_WIRELESS_CONTROLLER:
bediyap 0:f6f434d9a03a 171 if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01)
bediyap 0:f6f434d9a03a 172 OnBluetoothInsert(device); // it's bluetooth!
bediyap 0:f6f434d9a03a 173 break;
bediyap 0:f6f434d9a03a 174 default:
bediyap 0:f6f434d9a03a 175 StartAutoEvent(device,1,0);
bediyap 0:f6f434d9a03a 176 break;
bediyap 0:f6f434d9a03a 177 }
bediyap 0:f6f434d9a03a 178 }