| 892 |
theseven |
1 |
#include "global.h"
|
|
|
2 |
#include "usbglue.h"
|
|
|
3 |
#include "usb.h"
|
|
|
4 |
#include "usbdebug.h"
|
|
|
5 |
#include "util.h"
|
|
|
6 |
#include "thread.h"
|
|
|
7 |
#include "malloc.h"
|
|
|
8 |
#include USB_DRIVER_HEADER
|
|
|
9 |
|
|
|
10 |
static const struct usb_devicedescriptor usb_devicedescriptor =
|
|
|
11 |
{
|
|
|
12 |
.bLength = sizeof(struct usb_devicedescriptor),
|
|
|
13 |
.bDescriptorType = USB_DESCRIPTOR_TYPE_DEVICE,
|
|
|
14 |
.bcdUSB = 0x0200,
|
|
|
15 |
.bDeviceClass = 0,
|
|
|
16 |
.bDeviceSubClass = 0,
|
|
|
17 |
.bDeviceProtocol = 0,
|
|
|
18 |
.bMaxPacketSize0 = 64,
|
|
|
19 |
.idVendor = 0xffff,
|
|
|
20 |
.idProduct = 0xe000,
|
|
|
21 |
.bcdDevice = 2,
|
|
|
22 |
.iManufacturer = 1,
|
|
|
23 |
.iProduct = 2,
|
|
|
24 |
.iSerialNumber = 0,
|
|
|
25 |
.bNumConfigurations = 1,
|
|
|
26 |
};
|
|
|
27 |
|
|
|
28 |
static const struct __attribute__((packed)) _usb_config1_descriptors
|
|
|
29 |
{
|
|
|
30 |
struct usb_configurationdescriptor c1;
|
|
|
31 |
struct usb_interfacedescriptor c1_i0_a0;
|
|
|
32 |
} usb_config1_descriptors =
|
|
|
33 |
{
|
|
|
34 |
.c1 =
|
|
|
35 |
{
|
|
|
36 |
.bLength = sizeof(struct usb_configurationdescriptor),
|
|
|
37 |
.bDescriptorType = USB_DESCRIPTOR_TYPE_CONFIGURATION,
|
|
|
38 |
.wTotalLength = sizeof(struct _usb_config1_descriptors),
|
|
|
39 |
.bNumInterfaces = 1,
|
|
|
40 |
.bConfigurationValue = 1,
|
|
|
41 |
.iConfiguration = 0,
|
|
|
42 |
.bmAttributes = { .buspowered = 1, .selfpowered = 1 },
|
|
|
43 |
.bMaxPower = USB_MAXCURRENT / 2,
|
|
|
44 |
},
|
|
|
45 |
.c1_i0_a0 =
|
|
|
46 |
{
|
|
|
47 |
.bLength = sizeof(struct usb_interfacedescriptor),
|
|
|
48 |
.bDescriptorType = USB_DESCRIPTOR_TYPE_INTERFACE,
|
|
|
49 |
.bInterfaceNumber = 0,
|
|
|
50 |
.bAlternateSetting = 0,
|
|
|
51 |
.bNumEndpoints = 0,
|
|
|
52 |
.bInterfaceClass = 0xff,
|
|
|
53 |
.bInterfaceSubClass = 0x00,
|
|
|
54 |
.bInterfaceProtocol = 0x00,
|
|
|
55 |
.iInterface = 0,
|
|
|
56 |
},
|
|
|
57 |
};
|
|
|
58 |
|
|
|
59 |
static const struct usb_stringdescriptor usb_string_language =
|
|
|
60 |
{
|
|
|
61 |
.bLength = sizeof(usb_string_language) + sizeof(*usb_string_language.wString),
|
|
|
62 |
.bDescriptorType = USB_DESCRIPTOR_TYPE_STRING,
|
|
|
63 |
.wString = { 0x0409 },
|
|
|
64 |
};
|
|
|
65 |
|
|
|
66 |
static const struct usb_stringdescriptor usb_string_vendor =
|
|
|
67 |
{
|
|
|
68 |
.bLength = sizeof(usb_string_vendor) + sizeof(*usb_string_vendor.wString) * 14,
|
|
|
69 |
.bDescriptorType = USB_DESCRIPTOR_TYPE_STRING,
|
|
|
70 |
.wString = { 'f', 'r', 'e', 'e', 'm', 'y', 'i', 'p', 'o', 'd', '.', 'o', 'r', 'g' },
|
|
|
71 |
};
|
|
|
72 |
|
|
|
73 |
static const struct usb_stringdescriptor usb_string_product =
|
|
|
74 |
{
|
|
|
75 |
.bLength = sizeof(usb_string_product) + sizeof(*usb_string_product.wString) * 15,
|
|
|
76 |
.bDescriptorType = USB_DESCRIPTOR_TYPE_STRING,
|
|
|
77 |
.wString = { 'e', 'm', 'C', 'O', 'R', 'E', ' ', 'd', 'e', 'b', 'u', 'g', 'g', 'e', 'r' },
|
|
|
78 |
};
|
|
|
79 |
|
|
|
80 |
static const struct usb_stringdescriptor* usb_stringdescriptors[] =
|
|
|
81 |
{
|
|
|
82 |
&usb_string_language,
|
|
|
83 |
&usb_string_vendor,
|
|
|
84 |
&usb_string_product,
|
|
|
85 |
};
|
|
|
86 |
|
|
|
87 |
static const struct usb_altsetting usb_c1_i0_a0 =
|
|
|
88 |
{
|
|
|
89 |
.set_altsetting = usbdebug_enable,
|
|
|
90 |
.unset_altsetting = usbdebug_disable,
|
|
|
91 |
.endpoint_count = 0,
|
|
|
92 |
.endpoints =
|
|
|
93 |
{
|
|
|
94 |
},
|
|
|
95 |
};
|
|
|
96 |
|
|
|
97 |
static struct usb_interface usb_c1_i0 =
|
|
|
98 |
{
|
|
|
99 |
.bus_reset = NULL,
|
|
|
100 |
.ctrl_request = usbdebug_handle_setup,
|
|
|
101 |
.altsetting_count = 1,
|
|
|
102 |
.altsettings =
|
|
|
103 |
{
|
|
|
104 |
&usb_c1_i0_a0,
|
|
|
105 |
},
|
|
|
106 |
};
|
|
|
107 |
|
|
|
108 |
static const struct usb_configuration usb_c1 =
|
|
|
109 |
{
|
|
|
110 |
.descriptor = &usb_config1_descriptors.c1,
|
|
|
111 |
.set_configuration = NULL,
|
|
|
112 |
.unset_configuration = NULL,
|
|
|
113 |
.interface_count = 1,
|
|
|
114 |
.interfaces =
|
|
|
115 |
{
|
|
|
116 |
&usb_c1_i0,
|
|
|
117 |
},
|
|
|
118 |
};
|
|
|
119 |
|
|
|
120 |
|
|
|
121 |
static USB_DRIVER_CONFIG_TYPE usb_driver_config = USB_DRIVER_CONFIG;
|
|
|
122 |
|
|
|
123 |
static USB_DRIVER_STATE_TYPE usb_driver_state = USB_DRIVER_STATE;
|
|
|
124 |
|
|
|
125 |
static struct usb_state usb_state =
|
|
|
126 |
{
|
|
|
127 |
.interface_altsetting = { 0 },
|
|
|
128 |
};
|
|
|
129 |
|
|
|
130 |
static union usb_ep0_buffer usb_buffer CACHEALIGN_ATTR;
|
|
|
131 |
|
|
|
132 |
struct usb_instance usb_default_data =
|
|
|
133 |
{
|
|
|
134 |
.driver = &USB_DRIVER,
|
|
|
135 |
.driver_config = &usb_driver_config,
|
|
|
136 |
.driver_state = &usb_driver_state,
|
|
|
137 |
.state = &usb_state,
|
|
|
138 |
.buffer = &usb_buffer,
|
|
|
139 |
.bus_reset = NULL,
|
|
|
140 |
.ep0_setup_hook = NULL,
|
|
|
141 |
.configuration_count = 1,
|
|
|
142 |
.stringdescriptor_count = 3,
|
|
|
143 |
.devicedescriptor = &usb_devicedescriptor,
|
|
|
144 |
.stringdescriptors = usb_stringdescriptors,
|
|
|
145 |
.configurations =
|
|
|
146 |
{
|
|
|
147 |
&usb_c1,
|
|
|
148 |
},
|
|
|
149 |
};
|
|
|
150 |
|
|
|
151 |
struct usb_instance* usb_data = &usb_default_data;
|
|
|
152 |
|
|
|
153 |
static struct scheduler_thread usbmanager_thread_handle;
|
|
|
154 |
static uint32_t usbmanager_stack[0x40] STACK_ATTR;
|
|
|
155 |
static bool usb_connected;
|
|
|
156 |
static struct mutex usbmanager_mutex;
|
|
|
157 |
|
|
|
158 |
void usbmanager_thread(void* arg0, void* arg1, void* arg2, void* arg3)
|
|
|
159 |
{
|
|
|
160 |
while (true)
|
|
|
161 |
{
|
|
|
162 |
sleep(200000);
|
|
|
163 |
mutex_lock(&usbmanager_mutex, TIMEOUT_BLOCK);
|
|
|
164 |
bool newstate = vbus_state();
|
|
|
165 |
if (usb_connected != newstate)
|
|
|
166 |
{
|
|
|
167 |
if (newstate) usb_init(usb_data);
|
|
|
168 |
else usb_exit(usb_data);
|
|
|
169 |
usb_connected = newstate;
|
|
|
170 |
}
|
|
|
171 |
mutex_unlock(&usbmanager_mutex);
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
void usbmanager_init()
|
|
|
176 |
{
|
|
|
177 |
mutex_init(&usbmanager_mutex);
|
|
|
178 |
usb_connected = false;
|
|
|
179 |
usbdebug_init();
|
|
|
180 |
|
|
|
181 |
thread_create(&usbmanager_thread_handle, "synopsysotg", usbmanager_thread,
|
|
|
182 |
usbmanager_stack, sizeof(usbmanager_stack), OS_THREAD, 63, true,
|
|
|
183 |
NULL, NULL, NULL, NULL);
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
void usbmanager_exit()
|
|
|
187 |
{
|
|
|
188 |
if (usb_connected) usb_exit(usb_data);
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
int usbmanager_install_custom(const struct usb_devicedescriptor* devicedescriptor,
|
|
|
192 |
uint8_t config_count, const struct usb_configuration** configurations,
|
|
|
193 |
uint8_t string_count, const struct usb_stringdescriptor** stringdescriptors,
|
|
|
194 |
bool enable_debug)
|
|
|
195 |
{
|
|
|
196 |
int i, j, k, l;
|
|
|
197 |
int size = sizeof(struct usb_instance);
|
|
|
198 |
int descsize = sizeof(struct usb_devicedescriptor);
|
|
|
199 |
int max_interfaces = 0;
|
|
|
200 |
for (i = 0; i < config_count; i++)
|
|
|
201 |
{
|
|
|
202 |
const struct usb_configuration* configuration = configurations[i];
|
|
|
203 |
size += 4 + sizeof(struct usb_configuration);
|
|
|
204 |
descsize += configuration->descriptor->wTotalLength;
|
|
|
205 |
if (configuration->interface_count > max_interfaces)
|
|
|
206 |
max_interfaces = configuration->interface_count;
|
|
|
207 |
for (j = 0; j < configuration->interface_count; j++)
|
|
|
208 |
{
|
|
|
209 |
const struct usb_interface* interface = configuration->interfaces[j];
|
|
|
210 |
size += 4 + sizeof(struct usb_interface);
|
|
|
211 |
for (k = 0; k < interface->altsetting_count; k++)
|
|
|
212 |
{
|
|
|
213 |
const struct usb_altsetting* altsetting = interface->altsettings[k];
|
|
|
214 |
size += 4 + sizeof(struct usb_altsetting)
|
|
|
215 |
+ (4 + sizeof(struct usb_endpoint)) * altsetting->endpoint_count;
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
}
|
|
|
219 |
for (i = 0; i < string_count; i++) descsize += stringdescriptors[i]->bLength;
|
|
|
220 |
if (enable_debug)
|
|
|
221 |
{
|
|
|
222 |
size += 4;
|
|
|
223 |
descsize += sizeof(struct usb_interfacedescriptor);
|
|
|
224 |
if (configurations[0]->interface_count == max_interfaces) max_interfaces++;
|
|
|
225 |
}
|
|
|
226 |
size += 4 * string_count + sizeof(struct usb_state) + max_interfaces;
|
|
|
227 |
void* buf = malloc(size + descsize);
|
|
|
228 |
if (!buf) RET_ERR(1);
|
|
|
229 |
reownalloc(buf, KERNEL_OWNER(KERNEL_OWNER_CUSTOM_USB));
|
|
|
230 |
void* descbuf = buf + size;
|
|
|
231 |
struct usb_instance* instance = (struct usb_instance*)buf;
|
|
|
232 |
buf += sizeof(struct usb_instance) + 4 * config_count;
|
|
|
233 |
memcpy(instance, &usb_default_data, sizeof(struct usb_instance));
|
|
|
234 |
struct usb_devicedescriptor* devdescriptor = (struct usb_devicedescriptor*)descbuf;
|
|
|
235 |
descbuf += sizeof(struct usb_devicedescriptor);
|
|
|
236 |
memcpy(devdescriptor, devicedescriptor, sizeof(struct usb_devicedescriptor));
|
|
|
237 |
instance->devicedescriptor = devdescriptor;
|
|
|
238 |
for (i = 0; i < config_count; i++)
|
|
|
239 |
{
|
|
|
240 |
const struct usb_configuration* configuration = configurations[i];
|
|
|
241 |
struct usb_configuration* config = (struct usb_configuration*)buf;
|
|
|
242 |
buf += sizeof(struct usb_configuration) + 4 * configuration->interface_count;
|
|
|
243 |
memcpy(config, configuration, sizeof(struct usb_configuration));
|
|
|
244 |
instance->configurations[i] = config;
|
|
|
245 |
struct usb_configurationdescriptor* cfgdescriptor = (struct usb_configurationdescriptor*)descbuf;
|
|
|
246 |
descbuf += configuration->descriptor->wTotalLength;
|
|
|
247 |
memcpy(cfgdescriptor, configuration->descriptor, configuration->descriptor->wTotalLength);
|
|
|
248 |
config->descriptor = cfgdescriptor;
|
|
|
249 |
if (!i && enable_debug) buf += 4;
|
|
|
250 |
for (j = 0; j < configuration->interface_count; j++)
|
|
|
251 |
{
|
|
|
252 |
const struct usb_interface* interface = configuration->interfaces[j];
|
|
|
253 |
struct usb_interface* intf = (struct usb_interface*)buf;
|
|
|
254 |
buf += sizeof(struct usb_interface) + 4 * interface->altsetting_count;
|
|
|
255 |
memcpy(intf, interface, sizeof(struct usb_interface));
|
|
|
256 |
config->interfaces[j] = intf;
|
|
|
257 |
for (k = 0; k < interface->altsetting_count; k++)
|
|
|
258 |
{
|
|
|
259 |
const struct usb_altsetting* altsetting = interface->altsettings[k];
|
|
|
260 |
struct usb_altsetting* as = (struct usb_altsetting*)buf;
|
|
|
261 |
buf += sizeof(struct usb_altsetting) + 4 * altsetting->endpoint_count;
|
|
|
262 |
memcpy(as, altsetting, sizeof(struct usb_altsetting));
|
|
|
263 |
intf->altsettings[k] = as;
|
|
|
264 |
for (l = 0; l < altsetting->endpoint_count; l++)
|
|
|
265 |
{
|
|
|
266 |
const struct usb_endpoint* endpoint = altsetting->endpoints[l];
|
|
|
267 |
struct usb_endpoint* ep = (struct usb_endpoint*)buf;
|
|
|
268 |
buf += sizeof(struct usb_endpoint);
|
|
|
269 |
memcpy(ep, endpoint, sizeof(struct usb_endpoint));
|
|
|
270 |
as->endpoints[l] = ep;
|
|
|
271 |
}
|
|
|
272 |
}
|
|
|
273 |
}
|
|
|
274 |
if (!i && enable_debug)
|
|
|
275 |
{
|
|
|
276 |
struct usb_interfacedescriptor* intfdescriptor = (struct usb_interfacedescriptor*)descbuf;
|
|
|
277 |
descbuf += sizeof(struct usb_interfacedescriptor);
|
|
|
278 |
memcpy(intfdescriptor, &usb_config1_descriptors.c1_i0_a0, sizeof(struct usb_interfacedescriptor));
|
|
|
279 |
intfdescriptor->bInterfaceNumber = j;
|
|
|
280 |
config->interfaces[j] = &usb_c1_i0;
|
|
|
281 |
config->interface_count++;
|
|
|
282 |
cfgdescriptor->wTotalLength += sizeof(struct usb_interfacedescriptor);
|
|
|
283 |
cfgdescriptor->bNumInterfaces++;
|
|
|
284 |
}
|
|
|
285 |
}
|
|
|
286 |
const struct usb_stringdescriptor** stringdescs = (const struct usb_stringdescriptor**)buf;
|
|
|
287 |
buf += 4 * string_count;
|
|
|
288 |
instance->stringdescriptors = stringdescs;
|
|
|
289 |
for (i = 0; i < string_count; i++)
|
|
|
290 |
{
|
|
|
291 |
const struct usb_stringdescriptor* stringdescriptor = stringdescriptors[i];
|
|
|
292 |
struct usb_stringdescriptor* stringdesc = (struct usb_stringdescriptor*)descbuf;
|
|
|
293 |
descbuf += stringdescriptor->bLength;
|
|
|
294 |
memcpy(stringdesc, stringdescriptor, stringdescriptor->bLength);
|
|
|
295 |
stringdescs[i] = stringdesc;
|
|
|
296 |
}
|
|
|
297 |
struct usb_state* state = (struct usb_state*)buf;
|
|
|
298 |
buf += sizeof(struct usb_state) + max_interfaces;
|
|
|
299 |
instance->state = state;
|
|
|
300 |
mutex_lock(&usbmanager_mutex, TIMEOUT_BLOCK);
|
|
|
301 |
if (usb_connected) usb_exit(usb_data);
|
|
|
302 |
if (usb_data != &usb_default_data) free(usb_data);
|
|
|
303 |
usb_data = instance;
|
|
|
304 |
if (usb_connected) usb_init(usb_data);
|
|
|
305 |
mutex_unlock(&usbmanager_mutex);
|
|
|
306 |
return 0;
|
|
|
307 |
}
|
|
|
308 |
|
|
|
309 |
void usbmanager_uninstall_custom()
|
|
|
310 |
{
|
|
|
311 |
mutex_lock(&usbmanager_mutex, TIMEOUT_BLOCK);
|
|
|
312 |
if (usb_data != &usb_default_data)
|
|
|
313 |
{
|
|
|
314 |
if (usb_connected) usb_exit(usb_data);
|
|
|
315 |
free(usb_data);
|
|
|
316 |
usb_data = &usb_default_data;
|
|
|
317 |
if (usb_connected) usb_init(usb_data);
|
|
|
318 |
}
|
|
|
319 |
mutex_unlock(&usbmanager_mutex);
|
|
|
320 |
}
|
|
|
321 |
|
|
|
322 |
uint32_t usbmanager_get_available_endpoints()
|
|
|
323 |
{
|
|
|
324 |
return USB_ENDPOINTS;
|
|
|
325 |
}
|
|
|
326 |
|
|
|
327 |
bool usbmanager_get_connected()
|
|
|
328 |
{
|
|
|
329 |
return usb_connected;
|
|
|
330 |
}
|
|
|
331 |
|