1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
230
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
286
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
313
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
|
//==========================================================================
//
// usbs.c
//
// Generic USB slave-side support
//
//==========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2010 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later
// version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with eCos; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// As a special exception, if other files instantiate templates or use
// macros or inline functions from this file, or you compile this file
// and link it with other works to produce a work based on this file,
// this file does not by itself cause the resulting work to be covered by
// the GNU General Public License. However the source code for this file
// must still be made available in accordance with section (3) of the GNU
// General Public License v2.
//
// This exception does not invalidate any other reasons why a work based
// on this file might be covered by the GNU General Public License.
// -------------------------------------------
// ####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): bartv
// Contributors: bartv
// Date: 2000-10-04
//
//####DESCRIPTIONEND####
//
//==========================================================================
#include <pkgconf/system.h>
#include <cyg/infra/cyg_type.h>
#include <cyg/infra/cyg_ass.h>
#include <cyg/infra/cyg_trac.h>
#include <cyg/infra/diag.h>
#include <cyg/io/usb/usbs.h>
#include <cyg/hal/drv_api.h>
// ----------------------------------------------------------------------------
// Devtab entry support. This code can be compiled with no overheads as
// long as the necessary support package is in place.
#ifdef CYGPKG_IO
# include <cyg/io/io.h>
# include <cyg/io/devtab.h>
// ----------------------------------------------------------------------------
// read()/write() functions applied to USB endpoints. These just
// indirect via the usbs_endpoint structures and wait for the
// callback to happen.
typedef struct usbs_callback_data {
bool completed;
int result;
cyg_drv_mutex_t lock;
cyg_drv_cond_t signal;
} usbs_callback_data;
static void
usbs_devtab_callback(void* arg, int result)
{
usbs_callback_data* callback_data = (usbs_callback_data*) arg;
callback_data->result = result;
callback_data->completed = true;
cyg_drv_cond_signal(&(callback_data->signal));
}
Cyg_ErrNo
usbs_devtab_cwrite(cyg_io_handle_t handle, const void* buf, cyg_uint32* size)
{
usbs_callback_data wait;
cyg_devtab_entry_t* devtab_entry;
usbs_tx_endpoint* endpoint;
int result = ENOERR;
CYG_REPORT_FUNCTION();
wait.completed = 0;
cyg_drv_mutex_init(&wait.lock);
cyg_drv_cond_init(&wait.signal, &wait.lock);
devtab_entry = (cyg_devtab_entry_t*) handle;
CYG_CHECK_DATA_PTR( devtab_entry, "A valid endpoint must be supplied");
endpoint = (usbs_tx_endpoint*) devtab_entry->priv;
CYG_CHECK_DATA_PTR( endpoint, "The handle must correspond to a USB endpoint");
CYG_CHECK_FUNC_PTR( endpoint->start_tx_fn, "The endpoint must have a start_tx function");
endpoint->buffer = (unsigned char*) buf;
endpoint->buffer_size = (int) *size;
endpoint->complete_fn = &usbs_devtab_callback;
endpoint->complete_data = (void*) &wait;
(*endpoint->start_tx_fn)(endpoint);
cyg_drv_mutex_lock(&wait.lock);
cyg_drv_dsr_lock();
while (!wait.completed) {
cyg_drv_cond_wait(&wait.signal);
}
cyg_drv_dsr_unlock();
cyg_drv_mutex_unlock(&wait.lock);
if (wait.result < 0) {
result = wait.result;
} else {
*size = wait.result;
}
cyg_drv_cond_destroy(&wait.signal);
cyg_drv_mutex_destroy(&wait.lock);
CYG_REPORT_RETURN();
return result;
}
Cyg_ErrNo
usbs_devtab_cread(cyg_io_handle_t handle, void* buf, cyg_uint32* size)
{
usbs_callback_data wait;
cyg_devtab_entry_t* devtab_entry;
usbs_rx_endpoint* endpoint;
int result = ENOERR;
CYG_REPORT_FUNCTION();
wait.completed = 0;
cyg_drv_mutex_init(&wait.lock);
cyg_drv_cond_init(&wait.signal, &wait.lock);
devtab_entry = (cyg_devtab_entry_t*) handle;
CYG_CHECK_DATA_PTR( devtab_entry, "A valid endpoint must be supplied");
endpoint = (usbs_rx_endpoint*) devtab_entry->priv;
CYG_CHECK_DATA_PTR( endpoint, "The handle must correspond to a USB endpoint");
CYG_CHECK_FUNC_PTR( endpoint->start_rx_fn, "The endpoint must have a start_rx function");
endpoint->buffer = (unsigned char*) buf;
endpoint->buffer_size = (int) *size;
endpoint->complete_fn = &usbs_devtab_callback;
endpoint->complete_data = (void*) &wait;
(*endpoint->start_rx_fn)(endpoint);
cyg_drv_mutex_lock(&wait.lock);
cyg_drv_dsr_lock();
while (!wait.completed) {
cyg_drv_cond_wait(&wait.signal);
}
cyg_drv_dsr_unlock();
cyg_drv_mutex_unlock(&wait.lock);
if (wait.result < 0) {
result = wait.result;
} else {
*size = wait.result;
}
cyg_drv_cond_destroy(&wait.signal);
cyg_drv_mutex_destroy(&wait.lock);
CYG_REPORT_RETURN();
return result;
}
// ----------------------------------------------------------------------------
// More devtab functions, this time related to ioctl() style operations.
Cyg_ErrNo
usbs_devtab_get_config(cyg_io_handle_t handle, cyg_uint32 code, void* buf, cyg_uint32* size)
{
return -EINVAL;
}
Cyg_ErrNo
usbs_devtab_set_config(cyg_io_handle_t handle, cyg_uint32 code, const void* buf, cyg_uint32* size)
{
return -EINVAL;
}
#endif // CYGPKG_IO
// ----------------------------------------------------------------------------
// USB-specific functions that are useful for applications/packages which
// do not want to use the devtab interface. These may get called in DSR
// context.
usbs_rx_endpoint*
usbs_get_rx_endpoint(usbs_control_endpoint* control_endpoint,
cyg_uint8 endpoint_id)
{
CYG_CHECK_DATA_PTR(control_endpoint, "A valid control endpoint must be supplied");
CYG_CHECK_FUNC_PTR(control_endpoint->get_rxep_fn, "Dynamic endpoint access not supported");
if (control_endpoint->state != USBS_STATE_CONFIGURED)
return NULL;
return (*control_endpoint->get_rxep_fn)(control_endpoint, endpoint_id);
}
usbs_tx_endpoint*
usbs_get_tx_endpoint(usbs_control_endpoint* control_endpoint,
cyg_uint8 endpoint_id)
{
CYG_CHECK_DATA_PTR(control_endpoint, "A valid control endpoint must be supplied");
CYG_CHECK_FUNC_PTR(control_endpoint->get_txep_fn, "Dynamic endpoint access not supported");
if (control_endpoint->state != USBS_STATE_CONFIGURED)
return NULL;
return (*control_endpoint->get_txep_fn)(control_endpoint, endpoint_id);
}
void
usbs_start_rx(usbs_rx_endpoint* endpoint)
{
CYG_CHECK_DATA_PTR( endpoint, "A valid USB endpoint must be supplied");
CYG_CHECK_FUNC_PTR( endpoint->start_rx_fn, "The USB endpoint must support receive operations");
(*endpoint->start_rx_fn)(endpoint);
}
void
usbs_start_rx_buffer(usbs_rx_endpoint* endpoint,
unsigned char* buf, int size,
void (*callback_fn)(void *, int), void* callback_arg)
{
CYG_CHECK_DATA_PTR( endpoint, "A valid USB endpoint must be supplied");
CYG_CHECK_FUNC_PTR( endpoint->start_rx_fn, "The USB endpoint must support receive operations");
endpoint->buffer = buf;
endpoint->buffer_size = size;
endpoint->complete_fn = callback_fn;
endpoint->complete_data = callback_arg;
(*endpoint->start_rx_fn)(endpoint);
}
void
usbs_start_tx(usbs_tx_endpoint* endpoint)
{
CYG_CHECK_DATA_PTR( endpoint, "A valid USB endpoint must be supplied");
CYG_CHECK_FUNC_PTR( endpoint->start_tx_fn, "The USB endpoint must support receive operations");
(*endpoint->start_tx_fn)(endpoint);
}
void
usbs_start_tx_buffer(usbs_tx_endpoint* endpoint,
const unsigned char* buf, int size,
void (*callback_fn)(void*, int), void *callback_arg)
{
CYG_CHECK_DATA_PTR( endpoint, "A valid USB endpoint must be supplied");
CYG_CHECK_FUNC_PTR( endpoint->start_tx_fn, "The USB endpoint must support receive operations");
endpoint->buffer = buf;
endpoint->buffer_size = size;
endpoint->complete_fn = callback_fn;
endpoint->complete_data = callback_arg;
(*endpoint->start_tx_fn)(endpoint);
}
void
usbs_start(usbs_control_endpoint* endpoint)
{
CYG_CHECK_DATA_PTR( endpoint, "A valid USB endpoint must be supplied");
CYG_CHECK_FUNC_PTR( endpoint->start_fn, "The USB endpoint should have a start function");
(*endpoint->start_fn)(endpoint);
}
cyg_bool
usbs_rx_endpoint_halted(usbs_rx_endpoint* endpoint)
{
CYG_CHECK_DATA_PTR( endpoint, "A valid USB endpoint must be supplied");
return endpoint->halted;
}
cyg_bool
usbs_tx_endpoint_halted(usbs_tx_endpoint* endpoint)
{
CYG_CHECK_DATA_PTR( endpoint, "A valid USB endpoint must be supplied");
return endpoint->halted;
}
void
usbs_set_rx_endpoint_halted(usbs_rx_endpoint* endpoint, cyg_bool halted)
{
CYG_CHECK_DATA_PTR( endpoint, "A valid USB endpoint must be supplied");
CYG_CHECK_FUNC_PTR( endpoint->set_halted_fn, "The USB endpoint should have a set-halted function");
(*endpoint->set_halted_fn)(endpoint, halted);
}
void
usbs_set_tx_endpoint_halted(usbs_tx_endpoint* endpoint, cyg_bool halted)
{
CYG_CHECK_DATA_PTR( endpoint, "A valid USB endpoint must be supplied");
CYG_CHECK_FUNC_PTR( endpoint->set_halted_fn, "The USB endpoint should have a set-halted function");
(*endpoint->set_halted_fn)(endpoint, halted);
}
void
usbs_start_rx_endpoint_wait(usbs_rx_endpoint* endpoint, void (*callback_fn)(void*, int), void* callback_data)
{
endpoint->buffer = (unsigned char*) 0;
endpoint->buffer_size = 0;
endpoint->complete_fn = callback_fn;
endpoint->complete_data = callback_data;
(*endpoint->start_rx_fn)(endpoint);
}
void
usbs_start_tx_endpoint_wait(usbs_tx_endpoint* endpoint, void (*callback_fn)(void*, int), void* callback_data)
{
endpoint->buffer = (unsigned char*) 0;
endpoint->buffer_size = 0;
endpoint->complete_fn = callback_fn;
endpoint->complete_data = callback_data;
(*endpoint->start_tx_fn)(endpoint);
}
// ----------------------------------------------------------------------------
// Handling of standard control messages. This will be invoked by
// a USB device driver, usually at DSR level, to process any control
// messages that cannot be handled by the hardware itself and that
// have also not been handled by the application-specific handler
// (if any).
//
// Because this function can run at DSR level performance is important.
//
// The various standard control messages are affected as follows:
//
// clear-feature: nothing can be done here about device requests to
// disable remote-wakeup or about endpoint halt requests. It appears
// to be legal to clear no features on an interface.
//
// get-configuration: if the device is not configured a single byte 0
// should be returned. Otherwise this code assumes only one configuration
// is supported and its id can be extracted from the enumeration data.
// For more complicated devices get-configuration has to be handled
// at a higher-level.
//
// get-descriptor: this is the big one. It is used to obtain
// the enumeration data. An auxiliary refill function is needed.
//
// get-interface: this can be used to identify the current variant
// for a given interface within a configuration. For simple devices
// there will be only interface, 0. For anything more complicated
// higher level code will have to take care of the request.
//
// get-status. Much the same as clear-feature.
//
// set-address. Must be handled at the device driver level.
//
// set-configuration: a value of 0 is used to deconfigure the device,
// which can be handled here. Otherwise this code assumes that only
// a single configuration is supported and enables that. For anything
// more complicated higher-level code has to handle this request.
//
// set-descriptor: used to update the enumeration data. This is not
// supported here, although higher-level code can choose to do so.
//
// set-feature. See clear-feature and get-status.
//
// set-interface. Variant interfaces are not supported by the
// base code so this request has to be handled at a higher level.
//
// synch-frame. This is only relevant for isochronous transfers
// which are not yet supported, and anyway it is not clear what
// could be done about these requests here.
// This refill function handles GET_DESCRIPTOR requests for a
// configuration. For details of the control_buffer usage see
// the relevant code in the main callback.
static void
usbs_configuration_descriptor_refill(usbs_control_endpoint* endpoint)
{
usb_devreq* req = (usb_devreq*) endpoint->control_buffer;
int length = (req->length_hi << 8) | req->length_lo;
int sent = (req->index_hi << 8) | req->index_lo;
int current_interface = req->type;
int last_interface = req->request;
int current_endpoint = req->value_lo;
int last_endpoint = req->value_hi;
cyg_bool done = false;
if (current_endpoint == last_endpoint) {
// The next transfer should be a single interface - unless we have already finished.
if (current_interface == last_interface) {
done = true;
} else {
endpoint->buffer = (unsigned char*) &(endpoint->enumeration_data->interfaces[current_interface]);
if (USB_INTERFACE_DESCRIPTOR_LENGTH >= (length - sent)) {
endpoint->buffer_size = length - sent;
done = true;
} else {
endpoint->buffer_size = USB_INTERFACE_DESCRIPTOR_LENGTH;
sent += USB_INTERFACE_DESCRIPTOR_LENGTH;
// Note that an interface with zero endpoints is ok. We'll just move
// to the next interface in the next call.
last_endpoint = current_endpoint +
endpoint->enumeration_data->interfaces[current_interface].number_endpoints;
current_interface++;
}
}
} else {
// The next transfer should be a single endpoint. The
// endpoints are actually contiguous array elements
// but may not be packed, so they have to be transferred
// one at a time.
endpoint->buffer = (unsigned char*) &(endpoint->enumeration_data->endpoints[current_endpoint]);
if ((sent + USB_ENDPOINT_DESCRIPTOR_LENGTH) >= length) {
endpoint->buffer_size = length - sent;
done = true;
} else {
endpoint->buffer_size = USB_ENDPOINT_DESCRIPTOR_LENGTH;
current_endpoint ++;
}
}
if (done) {
endpoint->fill_buffer_fn = (void (*)(usbs_control_endpoint*)) 0;
} else {
req->type = (unsigned char) current_interface;
req->value_lo = (unsigned char) current_endpoint;
req->value_hi = (unsigned char) last_endpoint;
req->index_hi = (unsigned char) (sent >> 8);
req->index_lo = (unsigned char) (sent & 0x00FF);
}
}
usbs_control_return
usbs_handle_standard_control(usbs_control_endpoint* endpoint)
{
usbs_control_return result = USBS_CONTROL_RETURN_UNKNOWN;
usb_devreq* req = (usb_devreq*) endpoint->control_buffer;
int length;
int direction;
int recipient;
length = (req->length_hi << 8) | req->length_lo;
direction = req->type & USB_DEVREQ_DIRECTION_MASK;
recipient = req->type & USB_DEVREQ_RECIPIENT_MASK;
if (USB_DEVREQ_CLEAR_FEATURE == req->request) {
if (USB_DEVREQ_RECIPIENT_INTERFACE == recipient) {
// The host should expect no data back, the device must
// be configured, and there are no defined features to clear.
if ((0 == length) &&
(USBS_STATE_CONFIGURED == (endpoint->state & USBS_STATE_MASK)) &&
(0 == req->value_lo)) {
int interface_id = req->index_lo;
CYG_ASSERT( 1 == endpoint->enumeration_data->total_number_interfaces, \
"Higher level code should have handled this request");
if (interface_id == endpoint->enumeration_data->interfaces[0].interface_id) {
result = USBS_CONTROL_RETURN_HANDLED;
} else {
result = USBS_CONTROL_RETURN_STALL;
}
} else {
result = USBS_CONTROL_RETURN_STALL;
}
}
} else if (USB_DEVREQ_GET_CONFIGURATION == req->request) {
// Return a single byte 0 if the device is not currently
// configured. Otherwise assume a single configuration
// in the enumeration data and return its id.
if ((1 == length) && (USB_DEVREQ_DIRECTION_IN == direction)) {
if (USBS_STATE_CONFIGURED == (endpoint->state & USBS_STATE_MASK)) {
CYG_ASSERT( 1 == endpoint->enumeration_data->device.number_configurations, \
"Higher level code should have handled this request");
endpoint->control_buffer[0] = endpoint->enumeration_data->configurations[0].configuration_id;
} else {
endpoint->control_buffer[0] = 0;
}
endpoint->buffer = endpoint->control_buffer;
endpoint->buffer_size = 1;
endpoint->fill_buffer_fn = (void (*)(usbs_control_endpoint*)) 0;
endpoint->complete_fn = (usbs_control_return (*)(usbs_control_endpoint*, cyg_bool)) 0;
result = USBS_CONTROL_RETURN_HANDLED;
} else {
result = USBS_CONTROL_RETURN_STALL;
}
} else if (USB_DEVREQ_GET_DESCRIPTOR == req->request) {
// The descriptor type is in value_hi. The descriptor index
// is in value_lo.
// The hsot must expect at least one byte of data.
if ((0 == length) || (USB_DEVREQ_DIRECTION_IN != direction)) {
result = USBS_CONTROL_RETURN_STALL;
} else if (USB_DEVREQ_DESCRIPTOR_TYPE_DEVICE == req->value_hi) {
// The device descriptor is easy, it is a single field in the
// enumeration data.
endpoint->buffer = (unsigned char*) &(endpoint->enumeration_data->device);
endpoint->fill_buffer_fn = (void (*)(usbs_control_endpoint*)) 0;
endpoint->complete_fn = (usbs_control_return (*)(usbs_control_endpoint*, cyg_bool)) 0;
if (length < USB_DEVICE_DESCRIPTOR_LENGTH) {
endpoint->buffer_size = length;
} else {
endpoint->buffer_size = USB_DEVICE_DESCRIPTOR_LENGTH;
}
result = USBS_CONTROL_RETURN_HANDLED;
} else if (USB_DEVREQ_DESCRIPTOR_TYPE_CONFIGURATION == req->value_hi) {
// This is where things get messy. We need to supply the
// specified configuration data, followed by some number of
// interfaces and endpoints. Plus there are length limits
// to consider. First check that the specified index is valid.
if (req->value_lo >= endpoint->enumeration_data->device.number_configurations) {
result = USBS_CONTROL_RETURN_STALL;
} else {
// No such luck. OK, supplying the initial block is easy.
endpoint->buffer = (unsigned char*) &(endpoint->enumeration_data->configurations[req->value_lo]);
endpoint->complete_fn = (usbs_control_return (*)(usbs_control_endpoint*, cyg_bool)) 0;
// How much data was actually requested. If only the
// configuration itself is of interest then there is
// no need to worry about the rest.
if (length <= USB_CONFIGURATION_DESCRIPTOR_LENGTH) {
endpoint->buffer_size = length;
endpoint->fill_buffer_fn = (void (*)(usbs_control_endpoint*)) 0;
} else {
int i, j;
int start_interface;
int start_endpoint;
endpoint->buffer_size = USB_CONFIGURATION_DESCRIPTOR_LENGTH;
endpoint->fill_buffer_fn = &usbs_configuration_descriptor_refill;
// The descriptor refill_fn needs to know what next to transfer.
// The desired interfaces and endpoints will be contiguous so
// we need to keep track of the following:
// 1) the current interface index being transferred.
// 2) the last interface that should be transferred.
// 3) the current endpoint index that should be transferred.
// 4) the last endpoint index. This marks interface/endpoint transitions.
// 5) how much has been transferred to date.
// This information can be held in the control_buffer,
// with the length field being preserved.
start_interface = 0;
start_endpoint = 0;
// For all configurations up to the desired one.
for (i = 0; i < req->value_lo; i++) {
int config_interfaces = endpoint->enumeration_data->configurations[i].number_interfaces;
// For all interfaces in this configuration.
for (j = 0; j < config_interfaces; j++) {
// Add the number of endpoints in this interface to the current count.
CYG_ASSERT( (j + start_interface) < endpoint->enumeration_data->total_number_interfaces, \
"Valid interface count in enumeration data");
start_endpoint += endpoint->enumeration_data->interfaces[j + start_interface].number_endpoints;
}
// And update the index for the starting interface.
start_interface += config_interfaces;
}
CYG_ASSERT( start_interface < endpoint->enumeration_data->total_number_interfaces, \
"Valid interface count in enumeration data");
CYG_ASSERT( ((0 == endpoint->enumeration_data->total_number_endpoints) && (0 == start_endpoint)) || \
(start_endpoint < endpoint->enumeration_data->total_number_endpoints), \
"Valid endpoint count in enumeration data");
req->type = (unsigned char) start_interface;
req->request = (unsigned char) (start_interface +
endpoint->enumeration_data->configurations[req->value_lo].number_interfaces
);
req->value_lo = (unsigned char) start_endpoint;
req->value_hi = (unsigned char) start_endpoint;
req->index_lo = USB_CONFIGURATION_DESCRIPTOR_LENGTH;
req->index_hi = 0;
}
result = USBS_CONTROL_RETURN_HANDLED;
}
} else if (USB_DEVREQ_DESCRIPTOR_TYPE_STRING == req->value_hi) {
// As long as the index is valid, the rest is easy since
// the strings are just held in a simple array.
// NOTE: if multiple languages have to be supported
// then things get more difficult.
if (req->value_lo >= endpoint->enumeration_data->total_number_strings) {
result = USBS_CONTROL_RETURN_STALL;
} else {
endpoint->buffer = (unsigned char*) endpoint->enumeration_data->strings[req->value_lo];
endpoint->fill_buffer_fn = (void (*)(usbs_control_endpoint*)) 0;
endpoint->complete_fn = (usbs_control_return (*)(usbs_control_endpoint*, cyg_bool)) 0;
if (length < endpoint->buffer[0]) {
endpoint->buffer_size = length;
} else {
endpoint->buffer_size = endpoint->buffer[0];
}
result = USBS_CONTROL_RETURN_HANDLED;
}
} else {
result = USBS_CONTROL_RETURN_STALL;
}
} else if (USB_DEVREQ_GET_INTERFACE == req->request) {
if ((1 != length) ||
(USB_DEVREQ_DIRECTION_IN != direction) ||
(USBS_STATE_CONFIGURED != (endpoint->state & USBS_STATE_MASK))) {
result = USBS_CONTROL_RETURN_STALL;
} else {
int interface_id;
CYG_ASSERT( (1 == endpoint->enumeration_data->device.number_configurations) && \
(1 == endpoint->enumeration_data->total_number_interfaces), \
"Higher level code should have handled this request");
interface_id = (req->index_hi << 8) | req->index_lo;
if (interface_id != endpoint->enumeration_data->interfaces[0].interface_id) {
result = USBS_CONTROL_RETURN_STALL;
} else {
endpoint->control_buffer[0] = endpoint->enumeration_data->interfaces[0].alternate_setting;
endpoint->buffer = endpoint->control_buffer;
endpoint->buffer_size = 1;
endpoint->fill_buffer_fn = (void (*)(usbs_control_endpoint*)) 0;
endpoint->complete_fn = (usbs_control_return (*)(usbs_control_endpoint*, cyg_bool)) 0;
result = USBS_CONTROL_RETURN_HANDLED;
}
}
} else if (USB_DEVREQ_GET_STATUS == req->request) {
if (USB_DEVREQ_RECIPIENT_INTERFACE == recipient) {
// The host should expect two bytes back, the device must
// be configured, the interface number must be valid.
// The host should expect no data back, the device must
// be configured, and there are no defined features to clear.
if ((2 == length) &&
(USB_DEVREQ_DIRECTION_IN == direction) &&
(USBS_STATE_CONFIGURED == (endpoint->state & USBS_STATE_MASK))) {
int interface_id = req->index_lo;
CYG_ASSERT( 1 == endpoint->enumeration_data->total_number_interfaces, \
"Higher level code should have handled this request");
if (interface_id == endpoint->enumeration_data->interfaces[0].interface_id) {
// The request is legit, but there are no defined features for an interface...
endpoint->control_buffer[0] = 0;
endpoint->control_buffer[1] = 0;
endpoint->buffer = endpoint->control_buffer;
endpoint->buffer_size = 2;
endpoint->fill_buffer_fn = (void (*)(usbs_control_endpoint*)) 0;
endpoint->complete_fn = (usbs_control_return (*)(usbs_control_endpoint*, cyg_bool)) 0;
result = USBS_CONTROL_RETURN_HANDLED;
} else {
result = USBS_CONTROL_RETURN_STALL;
}
} else {
result = USBS_CONTROL_RETURN_STALL;
}
}
} else if (USB_DEVREQ_SET_CONFIGURATION == req->request) {
// Changing to configuration 0 means a state change from
// configured to addressed. Changing to anything else means a
// state change to configured. Both involve invoking the
// state change callback. If there are multiple configurations
// to choose from then this request has to be handled at
// a higher level.
int old_state = endpoint->state;
if (0 == req->value_lo) {
endpoint->state = USBS_STATE_ADDRESSED;
if ((void (*)(usbs_control_endpoint*, void*, usbs_state_change, int))0 != endpoint->state_change_fn) {
(*endpoint->state_change_fn)(endpoint, endpoint->state_change_data,
USBS_STATE_CHANGE_DECONFIGURED, old_state);
}
result = USBS_CONTROL_RETURN_HANDLED;
} else {
CYG_ASSERT(1 == endpoint->enumeration_data->device.number_configurations, \
"Higher level code should have handled this request");
if (req->value_lo == endpoint->enumeration_data->configurations[0].configuration_id) {
endpoint->state = USBS_STATE_CONFIGURED;
if ((void (*)(usbs_control_endpoint*, void*, usbs_state_change, int))0 != endpoint->state_change_fn) {
(*endpoint->state_change_fn)(endpoint, endpoint->state_change_data,
USBS_STATE_CHANGE_CONFIGURED, old_state);
}
result = USBS_CONTROL_RETURN_HANDLED;
} else {
result = USBS_CONTROL_RETURN_STALL;
}
}
} else if (USB_DEVREQ_SET_FEATURE == req->request) {
if (USB_DEVREQ_RECIPIENT_INTERFACE == recipient) {
// The host should expect no data back, the device must
// be configured, and there are no defined features to clear.
if ((0 == length) &&
(USBS_STATE_CONFIGURED == (endpoint->state & USBS_STATE_MASK)) &&
(0 == req->value_lo)) {
int interface_id = req->index_lo;
CYG_ASSERT( 1 == endpoint->enumeration_data->total_number_interfaces, \
"Higher level code should have handled this request");
if (interface_id == endpoint->enumeration_data->interfaces[0].interface_id) {
result = USBS_CONTROL_RETURN_HANDLED;
} else {
result = USBS_CONTROL_RETURN_STALL;
}
} else {
result = USBS_CONTROL_RETURN_STALL;
}
}
}
return result;
}
|