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#include <malloc.h>
33#include "usbdcore.h"
34
35#define MAX_INTERFACES 2
36
37
38int maxstrings = 20;
39
40
41
42struct usb_string_descriptor **usb_strings;
43
44int usb_devices;
45
46extern struct usb_function_driver ep0_driver;
47
48int registered_functions;
49int registered_devices;
50
51char *usbd_device_events[] = {
52 "DEVICE_UNKNOWN",
53 "DEVICE_INIT",
54 "DEVICE_CREATE",
55 "DEVICE_HUB_CONFIGURED",
56 "DEVICE_RESET",
57 "DEVICE_ADDRESS_ASSIGNED",
58 "DEVICE_CONFIGURED",
59 "DEVICE_SET_INTERFACE",
60 "DEVICE_SET_FEATURE",
61 "DEVICE_CLEAR_FEATURE",
62 "DEVICE_DE_CONFIGURED",
63 "DEVICE_BUS_INACTIVE",
64 "DEVICE_BUS_ACTIVITY",
65 "DEVICE_POWER_INTERRUPTION",
66 "DEVICE_HUB_RESET",
67 "DEVICE_DESTROY",
68 "DEVICE_FUNCTION_PRIVATE",
69};
70
71char *usbd_device_states[] = {
72 "STATE_INIT",
73 "STATE_CREATED",
74 "STATE_ATTACHED",
75 "STATE_POWERED",
76 "STATE_DEFAULT",
77 "STATE_ADDRESSED",
78 "STATE_CONFIGURED",
79 "STATE_UNKNOWN",
80};
81
82char *usbd_device_requests[] = {
83 "GET STATUS",
84 "CLEAR FEATURE",
85 "RESERVED",
86 "SET FEATURE",
87 "RESERVED",
88 "SET ADDRESS",
89 "GET DESCRIPTOR",
90 "SET DESCRIPTOR",
91 "GET CONFIGURATION",
92 "SET CONFIGURATION",
93 "GET INTERFACE",
94 "SET INTERFACE",
95 "SYNC FRAME",
96};
97
98char *usbd_device_descriptors[] = {
99 "UNKNOWN",
100 "DEVICE",
101 "CONFIG",
102 "STRING",
103 "INTERFACE",
104 "ENDPOINT",
105 "DEVICE QUALIFIER",
106 "OTHER SPEED",
107 "INTERFACE POWER",
108};
109
110char *usbd_device_status[] = {
111 "USBD_OPENING",
112 "USBD_OK",
113 "USBD_SUSPENDED",
114 "USBD_CLOSING",
115};
116
117
118
119
120
121
122
123
124
125
126
127struct usb_string_descriptor *usbd_get_string (__u8 index)
128{
129 if (index >= maxstrings) {
130 return NULL;
131 }
132 return usb_strings[index];
133}
134
135
136
137
138
139
140
141
142
143
144
145
146static struct usb_configuration_instance *usbd_device_configuration_instance (struct usb_device_instance *device,
147 unsigned int port, unsigned int configuration)
148{
149 if (configuration >= device->configurations)
150 return NULL;
151
152 return device->configuration_instance_array + configuration;
153}
154
155
156
157
158
159
160
161
162
163
164struct usb_interface_instance *usbd_device_interface_instance (struct usb_device_instance *device, int port, int configuration, int interface)
165{
166 struct usb_configuration_instance *configuration_instance;
167
168 if ((configuration_instance = usbd_device_configuration_instance (device, port, configuration)) == NULL) {
169 return NULL;
170 }
171 if (interface >= configuration_instance->interfaces) {
172 return NULL;
173 }
174 return configuration_instance->interface_instance_array + interface;
175}
176
177
178
179
180
181
182
183
184
185
186struct usb_alternate_instance *usbd_device_alternate_instance (struct usb_device_instance *device, int port, int configuration, int interface, int alternate)
187{
188 struct usb_interface_instance *interface_instance;
189
190 if ((interface_instance = usbd_device_interface_instance (device, port, configuration, interface)) == NULL) {
191 return NULL;
192 }
193
194 if (alternate >= interface_instance->alternates) {
195 return NULL;
196 }
197
198 return interface_instance->alternates_instance_array + alternate;
199}
200
201
202
203
204
205
206
207
208
209
210struct usb_device_descriptor *usbd_device_device_descriptor (struct usb_device_instance *device, int port)
211{
212 return (device->device_descriptor);
213}
214
215
216
217
218
219
220
221
222
223
224struct usb_configuration_descriptor *usbd_device_configuration_descriptor (struct
225 usb_device_instance
226 *device, int port, int configuration)
227{
228 struct usb_configuration_instance *configuration_instance;
229 if (!(configuration_instance = usbd_device_configuration_instance (device, port, configuration))) {
230 return NULL;
231 }
232 return (configuration_instance->configuration_descriptor);
233}
234
235
236
237
238
239
240
241
242
243
244
245
246struct usb_interface_descriptor *usbd_device_interface_descriptor (struct usb_device_instance
247 *device, int port, int configuration, int interface, int alternate)
248{
249 struct usb_interface_instance *interface_instance;
250 if (!(interface_instance = usbd_device_interface_instance (device, port, configuration, interface))) {
251 return NULL;
252 }
253 if ((alternate < 0) || (alternate >= interface_instance->alternates)) {
254 return NULL;
255 }
256 return (interface_instance->alternates_instance_array[alternate].interface_descriptor);
257}
258
259
260
261
262
263
264
265
266
267
268
269
270struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor_index (struct usb_device_instance
271 *device, int port, int configuration, int interface, int alternate, int index)
272{
273 struct usb_alternate_instance *alternate_instance;
274
275 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
276 return NULL;
277 }
278 if (index >= alternate_instance->endpoints) {
279 return NULL;
280 }
281 return *(alternate_instance->endpoints_descriptor_array + index);
282}
283
284
285
286
287
288
289
290
291
292
293
294
295int usbd_device_endpoint_transfersize (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int index)
296{
297 struct usb_alternate_instance *alternate_instance;
298
299 if (!(alternate_instance = usbd_device_alternate_instance (device, port, configuration, interface, alternate))) {
300 return 0;
301 }
302 if (index >= alternate_instance->endpoints) {
303 return 0;
304 }
305 return *(alternate_instance->endpoint_transfersize_array + index);
306}
307
308
309
310
311
312
313
314
315
316
317
318
319
320struct usb_endpoint_descriptor *usbd_device_endpoint_descriptor (struct usb_device_instance *device, int port, int configuration, int interface, int alternate, int endpoint)
321{
322 struct usb_endpoint_descriptor *endpoint_descriptor;
323 int i;
324
325 for (i = 0; !(endpoint_descriptor = usbd_device_endpoint_descriptor_index (device, port, configuration, interface, alternate, i)); i++) {
326 if (endpoint_descriptor->bEndpointAddress == endpoint) {
327 return endpoint_descriptor;
328 }
329 }
330 return NULL;
331}
332
333
334
335
336
337
338
339
340int usbd_endpoint_halted (struct usb_device_instance *device, int endpoint)
341{
342 return (device->status == USB_STATUS_HALT);
343}
344
345
346
347
348
349
350
351
352
353
354void usbd_rcv_complete(struct usb_endpoint_instance *endpoint, int len, int urb_bad)
355{
356 if (endpoint) {
357 struct urb *rcv_urb;
358
359
360
361
362 if ((rcv_urb = endpoint->rcv_urb)) {
363
364
365
366
367
368 if (!urb_bad && (len <= endpoint->rcv_packetSize)) {
369
370
371
372 rcv_urb->actual_length += len;
373
374 } else {
375 usberr(" RECV_ERROR actual: %d buffer: %d urb_bad: %d\n",
376 rcv_urb->actual_length, rcv_urb->buffer_length, urb_bad);
377
378 rcv_urb->actual_length = 0;
379 rcv_urb->status = RECV_ERROR;
380 }
381 } else {
382 usberr("no rcv_urb!");
383 }
384 } else {
385 usberr("no endpoint!");
386 }
387
388}
389
390
391
392
393
394
395
396
397void usbd_tx_complete (struct usb_endpoint_instance *endpoint)
398{
399 if (endpoint) {
400 struct urb *tx_urb;
401
402
403 if ((tx_urb = endpoint->tx_urb)) {
404 int sent = endpoint->last;
405 endpoint->sent += sent;
406 endpoint->last -= sent;
407
408 if( (endpoint->tx_urb->actual_length - endpoint->sent) <= 0 ) {
409 tx_urb->actual_length = 0;
410 endpoint->sent = 0;
411 endpoint->last = 0;
412
413
414 urb_detach(tx_urb);
415 urb_append(&endpoint->done, tx_urb);
416
417
418
419 endpoint->tx_urb = first_urb_detached(&endpoint->tx);
420 if( endpoint->tx_urb ) {
421 endpoint->tx_queue--;
422 usbdbg("got urb from tx list");
423 }
424 if( !endpoint->tx_urb ) {
425
426 endpoint->tx_urb = first_urb_detached(&endpoint->done);
427 }
428 if( !endpoint->tx_urb ) {
429 usbdbg("allocating new urb for tx_urb");
430 endpoint->tx_urb = usbd_alloc_urb(tx_urb->device, endpoint);
431 }
432 }
433 }
434 }
435}
436
437
438
439
440
441
442
443
444void urb_link_init (urb_link * ul)
445{
446 if (ul) {
447 ul->prev = ul->next = ul;
448 }
449}
450
451
452
453
454
455
456
457void urb_detach (struct urb *urb)
458{
459 if (urb) {
460 urb_link *ul = &urb->link;
461 ul->next->prev = ul->prev;
462 ul->prev->next = ul->next;
463 urb_link_init (ul);
464 }
465}
466
467
468
469
470
471
472
473urb_link *first_urb_link (urb_link * hd)
474{
475 urb_link *nx;
476 if (NULL != hd && NULL != (nx = hd->next) && nx != hd) {
477
478
479 return (nx);
480 }
481
482 return (NULL);
483}
484
485
486
487
488
489struct urb *first_urb (urb_link * hd)
490{
491 urb_link *nx;
492 if (NULL == (nx = first_urb_link (hd))) {
493
494 return (NULL);
495 }
496 return (p2surround (struct urb, link, nx));
497}
498
499
500
501
502
503
504struct urb *first_urb_detached (urb_link * hd)
505{
506 struct urb *urb;
507 if ((urb = first_urb (hd))) {
508 urb_detach (urb);
509 }
510 return urb;
511}
512
513
514
515
516
517
518
519void urb_append (urb_link * hd, struct urb *urb)
520{
521 if (hd && urb) {
522 urb_link *new = &urb->link;
523
524
525
526
527
528 urb_link *pul = hd->prev;
529 new->prev->next = hd;
530 hd->prev = new->prev;
531 new->prev = pul;
532 pul->next = new;
533 }
534}
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549struct urb *usbd_alloc_urb (struct usb_device_instance *device,
550 struct usb_endpoint_instance *endpoint)
551{
552 struct urb *urb;
553
554 if (!(urb = (struct urb *) malloc (sizeof (struct urb)))) {
555 usberr (" F A T A L: malloc(%zu) FAILED!!!!",
556 sizeof (struct urb));
557 return NULL;
558 }
559
560
561 memset (urb, 0, sizeof (struct urb));
562 urb->endpoint = endpoint;
563 urb->device = device;
564 urb->buffer = (u8 *) urb->buffer_data;
565 urb->buffer_length = sizeof (urb->buffer_data);
566
567 urb_link_init (&urb->link);
568
569 return urb;
570}
571
572
573
574
575
576
577
578void usbd_dealloc_urb (struct urb *urb)
579{
580 if (urb) {
581 free (urb);
582 }
583}
584
585
586
587
588
589
590
591
592
593
594void usbd_device_event_irq (struct usb_device_instance *device, usb_device_event_t event, int data)
595{
596 usb_device_state_t state;
597
598 if (!device || !device->bus) {
599 usberr("(%p,%d) NULL device or device->bus", device, event);
600 return;
601 }
602
603 state = device->device_state;
604
605 usbinfo("%s", usbd_device_events[event]);
606
607 switch (event) {
608 case DEVICE_UNKNOWN:
609 break;
610 case DEVICE_INIT:
611 device->device_state = STATE_INIT;
612 break;
613
614 case DEVICE_CREATE:
615 device->device_state = STATE_ATTACHED;
616 break;
617
618 case DEVICE_HUB_CONFIGURED:
619 device->device_state = STATE_POWERED;
620 break;
621
622 case DEVICE_RESET:
623 device->device_state = STATE_DEFAULT;
624 device->address = 0;
625 break;
626
627 case DEVICE_ADDRESS_ASSIGNED:
628 device->device_state = STATE_ADDRESSED;
629 break;
630
631 case DEVICE_CONFIGURED:
632 device->device_state = STATE_CONFIGURED;
633 break;
634
635 case DEVICE_DE_CONFIGURED:
636 device->device_state = STATE_ADDRESSED;
637 break;
638
639 case DEVICE_BUS_INACTIVE:
640 if (device->status != USBD_CLOSING) {
641 device->status = USBD_SUSPENDED;
642 }
643 break;
644 case DEVICE_BUS_ACTIVITY:
645 if (device->status != USBD_CLOSING) {
646 device->status = USBD_OK;
647 }
648 break;
649
650 case DEVICE_SET_INTERFACE:
651 break;
652 case DEVICE_SET_FEATURE:
653 break;
654 case DEVICE_CLEAR_FEATURE:
655 break;
656
657 case DEVICE_POWER_INTERRUPTION:
658 device->device_state = STATE_POWERED;
659 break;
660 case DEVICE_HUB_RESET:
661 device->device_state = STATE_ATTACHED;
662 break;
663 case DEVICE_DESTROY:
664 device->device_state = STATE_UNKNOWN;
665 break;
666
667 case DEVICE_FUNCTION_PRIVATE:
668 break;
669
670 default:
671 usbdbg("event %d - not handled",event);
672 break;
673 }
674
675
676
677
678
679 if( device->event ) {
680
681 device->event(device, event, data);
682 }
683}
684