1919
2020#include " USBAPI.h"
2121#include " USBDesc.h"
22+ #include " USBCore.h"
2223#include " PluggableUSB.h"
2324
25+ #if defined(USBCON)
2426#ifdef PLUGGABLE_USB_ENABLED
2527
26- #define MAX_MODULES 6
27-
28- static uint8_t lastIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT;
29- static uint8_t lastEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT;
30-
3128extern uint32_t EndPoints[];
3229
33- // PUSBCallbacks cbs[MAX_MODULES];
34- static uint8_t modules_count = 0 ;
35-
36- static PUSBListNode* rootNode = NULL ;
37-
38- int PUSB_GetInterface (uint8_t * interfaceNum)
30+ int PluggableUSB_::getInterface (uint8_t * interfaceCount)
3931{
40- int ret = 0 ;
41- PUSBListNode* node = rootNode;
42- for (uint8_t i=0 ; i<modules_count; i++) {
43- ret += node->cb ->getInterface (interfaceNum);
44- node = node->next ;
32+ int sent = 0 ;
33+ PluggableUSBModule* node;
34+ for (node = rootNode; node; node = node->next ) {
35+ int res = node->getInterface (interfaceCount);
36+ if (res < 0 )
37+ return -1 ;
38+ sent += res;
4539 }
46- return ret ;
40+ return sent ;
4741}
4842
49- int PUSB_GetDescriptor ( int8_t t )
43+ int PluggableUSB_::getDescriptor (USBSetup& setup )
5044{
51- int ret = 0 ;
52- PUSBListNode* node = rootNode;
53- for (uint8_t i=0 ; i<modules_count && ret == 0 ; i++) {
54- ret = node->cb ->getDescriptor (t);
55- node = node->next ;
45+ PluggableUSBModule* node;
46+ for (node = rootNode; node; node = node->next ) {
47+ int ret = node->getDescriptor (setup);
48+ // ret!=0 -> request has been processed
49+ if (ret)
50+ return ret;
5651 }
57- return ret ;
52+ return 0 ;
5853}
5954
60- bool PUSB_Setup (USBSetup& setup, uint8_t j )
55+ bool PluggableUSB_::setup (USBSetup& setup)
6156{
62- bool ret = false ;
63- PUSBListNode* node = rootNode;
64- for ( uint8_t i= 0 ; i<modules_count && ret == false ; i++ ) {
65- ret = node-> cb -> setup (setup, j) ;
66- node = node-> next ;
57+ PluggableUSBModule* node ;
58+ for ( node = rootNode; node; node = node-> next ) {
59+ if (node-> setup (setup) ) {
60+ return true ;
61+ }
6762 }
68- return ret ;
63+ return false ;
6964}
7065
71- int8_t PUSB_AddFunction (PUSBListNode *node, uint8_t * interface )
66+ bool PluggableUSB_::plug (PluggableUSBModule *node)
7267{
73- if (modules_count >= MAX_MODULES ) {
74- return 0 ;
68+ if ((lastEp + node-> numEndpoints ) > USB_ENDPOINTS ) {
69+ return false ;
7570 }
7671
77- if (modules_count == 0 ) {
72+ if (!rootNode ) {
7873 rootNode = node;
7974 } else {
80- PUSBListNode *current = rootNode;
81- while (current->next != NULL ) {
75+ PluggableUSBModule *current = rootNode;
76+ while (current->next ) {
8277 current = current->next ;
8378 }
8479 current->next = node;
8580 }
8681
87- *interface = lastIf;
88- lastIf += node->cb ->numInterfaces ;
89- for ( uint8_t i = 0 ; i< node->cb ->numEndpoints ; i++) {
90- EndPoints[lastEp] = node->cb ->endpointType [i];
82+ node->pluggedInterface = lastIf;
83+ node->pluggedEndpoint = lastEp;
84+ lastIf += node->numInterfaces ;
85+ for (uint8_t i = 0 ; i < node->numEndpoints ; i++) {
86+ EndPoints[lastEp] = node->endpointType [i];
9187 lastEp++;
9288 }
93- modules_count++;
94- return lastEp - node->cb ->numEndpoints ;
89+ return true ;
9590 // restart USB layer???
9691}
9792
93+ PluggableUSB_& PluggableUSB ()
94+ {
95+ static PluggableUSB_ obj;
96+ return obj;
97+ }
98+
99+ PluggableUSB_::PluggableUSB_ () : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT),
100+ lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT),
101+ rootNode(NULL )
102+ {
103+ // Empty
104+ }
105+
106+ #endif
98107#endif
0 commit comments