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