1
10
13
14
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
165
166
167
168
169
170
171
172
174
175
176
177
178
179
180
181
182
183
184
185
186
187
191
192
193
194
195
196
197
198
199
200
201
202
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
377
378
379
380
381
382
383
384
385
390
391
392
393
394
395
396
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
...
...
...
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_device_stack.h"
UX_COMPILE_TIME_ASSERT(!UX_OVERFLOW_CHECK_MULC_ULONG(sizeof(UX_SLAVE_CLASS), UX_MAX_SLAVE_CLASS_DRIVER), UX_MAX_SLAVE_CLASS_DRIVER_mul_ovf)
UCHAR _ux_system_slave_class_storage_name[] = "ux_slave_class_storage";
UCHAR _ux_system_slave_class_cdc_acm_name[] = "ux_slave_class_cdc_acm";
UCHAR _ux_system_slave_class_dpump_name[] = "ux_slave_class_dpump";
UCHAR _ux_system_slave_class_pima_name[] = "ux_slave_class_pima";
UCHAR _ux_system_slave_class_hid_name[] = "ux_slave_class_hid";
UCHAR _ux_system_slave_class_rndis_name[] = "ux_slave_class_rndis";
UCHAR _ux_system_slave_class_cdc_ecm_name[] = "ux_slave_class_cdc_ecm";
UCHAR _ux_system_slave_class_dfu_name[] = "ux_slave_class_dfu";
UCHAR _ux_system_slave_class_audio_name[] = "ux_slave_class_audio";
UCHAR _ux_system_device_class_printer_name[] = "ux_device_class_printer";
UX_SYSTEM_SLAVE *_ux_system_slave;
...
...
UINT _ux_device_stack_initialize(UCHAR * device_framework_high_speed, ULONG device_framework_length_high_speed,
UCHAR * device_framework_full_speed, ULONG device_framework_length_full_speed,
UCHAR * string_framework, ULONG string_framework_length,
UCHAR * language_id_framework, ULONG language_id_framework_length,
UINT (*ux_system_slave_change_function)(ULONG))
{
UX_SLAVE_DEVICE *device;
UX_SLAVE_ENDPOINT *endpoints_pool;
UX_SLAVE_INTERFACE *interfaces_pool;
UX_SLAVE_TRANSFER *transfer_request;
UINT status;
ULONG interfaces_found;
ULONG endpoints_found;
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE)
ULONG max_interface_number;
ULONG local_interfaces_found;
ULONG local_endpoints_found;
ULONG endpoints_in_interface_found;
UCHAR *device_framework;
ULONG device_framework_length;
UCHAR descriptor_type;
ULONG descriptor_length;/* ... */
#endif
UCHAR *memory;
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_INITIALIZE, 0, 0, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
device = &_ux_system_slave -> ux_system_slave_device;
_ux_system_slave -> ux_system_slave_device_framework_high_speed = device_framework_high_speed;
_ux_system_slave -> ux_system_slave_device_framework_length_high_speed = device_framework_length_high_speed;
_ux_system_slave -> ux_system_slave_device_framework_full_speed = device_framework_full_speed;
_ux_system_slave -> ux_system_slave_device_framework_length_full_speed = device_framework_length_full_speed;
_ux_system_slave -> ux_system_slave_string_framework = string_framework;
_ux_system_slave -> ux_system_slave_string_framework_length = string_framework_length;
_ux_system_slave -> ux_system_slave_language_id_framework = language_id_framework;
_ux_system_slave -> ux_system_slave_language_id_framework_length = language_id_framework_length;
UX_SYSTEM_DEVICE_MAX_CLASS_SET(UX_MAX_SLAVE_CLASS_DRIVER);
_ux_system_slave -> ux_system_slave_change_function = ux_system_slave_change_function;
/* ... */
memory = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_SLAVE_CLASS) * UX_MAX_SLAVE_CLASS_DRIVER);
if (memory == UX_NULL)
return(UX_MEMORY_INSUFFICIENT);
_ux_system_slave -> ux_system_slave_class_array = (UX_SLAVE_CLASS *) ((void *) memory);
/* ... */
transfer_request = &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
transfer_request -> ux_slave_transfer_request_data_pointer =
_ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH);
if (transfer_request -> ux_slave_transfer_request_data_pointer == UX_NULL)
status = UX_MEMORY_INSUFFICIENT;
else
status = UX_SUCCESS;
#if defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE)
interfaces_found = UX_MAX_SLAVE_INTERFACES;
endpoints_found = UX_MAX_DEVICE_ENDPOINTS;/* ... */
#else
interfaces_found = 0;
endpoints_found = 0;
max_interface_number = 0;
if (status == UX_SUCCESS)
{
/* ... */
device_framework = _ux_system_slave -> ux_system_slave_device_framework_full_speed;
device_framework_length = _ux_system_slave -> ux_system_slave_device_framework_length_full_speed;
local_interfaces_found = 0;
local_endpoints_found = 0;
endpoints_in_interface_found = 0;
while (device_framework_length != 0)
{
descriptor_length = (ULONG) *device_framework;
descriptor_type = *(device_framework + 1);
switch(descriptor_type)
{
case UX_INTERFACE_DESCRIPTOR_ITEM:
/* ... */
if (*(device_framework + 3) == 0)
{
local_endpoints_found += endpoints_in_interface_found;
endpoints_in_interface_found = (ULONG) *(device_framework + 4);
local_interfaces_found++;
}if (*(device_framework + 3) == 0) { ... }
else
{
if (endpoints_in_interface_found < (ULONG) *(device_framework + 4))
endpoints_in_interface_found = (ULONG) *(device_framework + 4);
}else { ... }
if (*(device_framework + 2) > max_interface_number)
max_interface_number = *(device_framework + 2);
break;
case UX_INTERFACE_DESCRIPTOR_ITEM:
case UX_CONFIGURATION_DESCRIPTOR_ITEM:
if (local_interfaces_found > interfaces_found)
interfaces_found = local_interfaces_found;
local_interfaces_found = 0;
local_endpoints_found += endpoints_in_interface_found;
if (local_endpoints_found > endpoints_found)
endpoints_found = local_endpoints_found;
local_endpoints_found = 0;
endpoints_in_interface_found = 0;
break;
case UX_CONFIGURATION_DESCRIPTOR_ITEM:
default:
break;default
}switch (descriptor_type) { ... }
device_framework_length -= descriptor_length;
device_framework += descriptor_length;
}while (device_framework_length != 0) { ... }
local_endpoints_found += endpoints_in_interface_found;
if (local_endpoints_found > endpoints_found)
endpoints_found = local_endpoints_found;
if (local_interfaces_found > interfaces_found)
interfaces_found = local_interfaces_found;
/* ... */
if (interfaces_found == 0)
{
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_INIT, UX_DESCRIPTOR_CORRUPTED);
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, device_framework, 0, 0, UX_TRACE_ERRORS, 0, 0)
status = UX_DESCRIPTOR_CORRUPTED;
}if (interfaces_found == 0) { ... }
if (status == UX_SUCCESS &&
max_interface_number >= UX_MAX_SLAVE_INTERFACES)
{
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_INIT, UX_MEMORY_INSUFFICIENT);
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_INSUFFICIENT, device_framework, 0, 0, UX_TRACE_ERRORS, 0, 0)
status = UX_MEMORY_INSUFFICIENT;
}if (status == UX_SUCCESS && max_interface_number >= UX_MAX_SLAVE_INTERFACES) { ... }
}if (status == UX_SUCCESS) { ... }
/* ... */#endif
if (status == UX_SUCCESS)
{
device -> ux_slave_device_interfaces_pool_number = interfaces_found;
device -> ux_slave_device_endpoints_pool_number = endpoints_found;
interfaces_pool = _ux_utility_memory_allocate_mulc_safe(UX_NO_ALIGN, UX_REGULAR_MEMORY, interfaces_found, sizeof(UX_SLAVE_INTERFACE));
if (interfaces_pool == UX_NULL)
status = UX_MEMORY_INSUFFICIENT;
else
device -> ux_slave_device_interfaces_pool = interfaces_pool;
}if (status == UX_SUCCESS) { ... }
if (endpoints_found != 0 && status == UX_SUCCESS)
{
endpoints_pool = _ux_utility_memory_allocate_mulc_safe(UX_NO_ALIGN, UX_REGULAR_MEMORY, endpoints_found, sizeof(UX_SLAVE_ENDPOINT));
if (endpoints_pool == UX_NULL)
status = UX_MEMORY_INSUFFICIENT;
else
{
device -> ux_slave_device_endpoints_pool = endpoints_pool;
/* ... */
while (endpoints_pool < (device -> ux_slave_device_endpoints_pool + endpoints_found))
{
endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer =
_ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, UX_SLAVE_REQUEST_DATA_MAX_LENGTH);
if (endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer == UX_NULL)
{
status = UX_MEMORY_INSUFFICIENT;
break;
}if (endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer == UX_NULL) { ... }
status = _ux_device_semaphore_create(&endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_semaphore,
"ux_transfer_request_semaphore", 0);
if (status != UX_SUCCESS)
{
status = UX_SEMAPHORE_ERROR;
break;
}if (status != UX_SUCCESS) { ... }
endpoints_pool++;
}while (endpoints_pool < (device -> ux_slave_device_endpoints_pool + endpoints_found)) { ... }
}else { ... }
}if (endpoints_found != 0 && status == UX_SUCCESS) { ... }
else
endpoints_pool = UX_NULL;
if (status == UX_SUCCESS)
return(UX_SUCCESS);
if (endpoints_pool)
{
/* ... */
while(endpoints_pool >= device -> ux_slave_device_endpoints_pool)
{
if (_ux_device_semaphore_created(&endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_semaphore))
_ux_device_semaphore_delete(&endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_semaphore);
if (endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer)
_ux_utility_memory_free(endpoints_pool -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer);
endpoints_pool --;
}while (endpoints_pool >= device -> ux_slave_device_endpoints_pool) { ... }
_ux_utility_memory_free(device -> ux_slave_device_endpoints_pool);
}if (endpoints_pool) { ... }
if (device -> ux_slave_device_interfaces_pool)
_ux_utility_memory_free(device -> ux_slave_device_interfaces_pool);
if (device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer)
_ux_utility_memory_free(device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer);
_ux_utility_memory_free(_ux_system_slave -> ux_system_slave_class_array);
return(status);
}{ ... }