2323#if defined(USBCON)
2424#ifdef PLUGGABLE_USB_ENABLED
2525
26- # define MAX_MODULES 6
26+ extern uint8_t _initEndpoints[];
2727
28- static u8 lastIf = CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT;
29- static u8 lastEp = CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT;
30-
31- extern u8 _initEndpoints[];
32-
33- // PUSBCallbacks cbs[MAX_MODULES];
34- static u8 modules_count = 0 ;
35-
36- static PUSBListNode* rootNode = NULL ;
37-
38- int PUSB_GetInterface (u8 * interfaceNum)
28+ int PluggableUSB_::getInterface (uint8_t * interfaceCount)
3929{
40- int ret = 0 ;
41- PUSBListNode* node = rootNode;
42- for (u8 i=0 ; i<modules_count; i++) {
43- ret = node->cb ->getInterface (interfaceNum);
44- node = node->next ;
30+ int sent = 0 ;
31+ PUSBListNode* node;
32+ for (node = rootNode; node; node = node->next ) {
33+ int res = node->getInterface (interfaceCount);
34+ if (res < 0 )
35+ return -1 ;
36+ sent += res;
4537 }
46- return ret ;
38+ return sent ;
4739}
4840
49- int PUSB_GetDescriptor (int8_t t )
41+ int PluggableUSB_::getDescriptor (int8_t type )
5042{
51- int ret = 0 ;
52- PUSBListNode* node = rootNode;
53- for (u8 i=0 ; i<modules_count && ret == 0 ; i++) {
54- ret = node->cb ->getDescriptor (t);
55- node = node->next ;
43+ PUSBListNode* node;
44+ for (node = rootNode; node; node = node->next ) {
45+ int ret = node->getDescriptor (type);
46+ // ret!=0 -> request has been processed
47+ if (ret)
48+ return ret;
5649 }
57- return ret ;
50+ return 0 ;
5851}
5952
60- bool PUSB_Setup (USBSetup& setup, u8 j )
53+ bool PluggableUSB_::setup (USBSetup& setup, uint8_t interfaceNum )
6154{
62- bool ret = false ;
63- PUSBListNode* node = rootNode;
64- for ( u8 i= 0 ; i<modules_count && ret == false ; i++ ) {
65- ret = node-> cb -> setup (setup, j) ;
66- node = node-> next ;
55+ PUSBListNode* node ;
56+ for ( node = rootNode; node; node = node-> next ) {
57+ if (node-> setup (setup, interfaceNum) ) {
58+ return true ;
59+ }
6760 }
68- return ret ;
61+ return false ;
6962}
7063
71- int8_t PUSB_AddFunction (PUSBListNode *node, u8 * interface )
64+ bool PluggableUSB_::plug (PUSBListNode *node)
7265{
73- if (modules_count >= MAX_MODULES ) {
74- return 0 ;
66+ if ((lastEp + node-> numEndpoints ) > USB_ENDPOINTS ) {
67+ return false ;
7568 }
7669
77- if (modules_count == 0 ) {
70+ if (!rootNode ) {
7871 rootNode = node;
7972 } else {
8073 PUSBListNode *current = rootNode;
81- while (current->next != NULL ) {
74+ while (current->next ) {
8275 current = current->next ;
8376 }
8477 current->next = node;
8578 }
8679
87- *interface = lastIf;
88- lastIf += node->cb ->numInterfaces ;
89- for ( u8 i = 0 ; i< node->cb ->numEndpoints ; i++) {
90- _initEndpoints[lastEp] = node->cb ->endpointType [i];
80+ node->pluggedInterface = lastIf;
81+ node->pluggedEndpoint = lastEp;
82+ lastIf += node->numInterfaces ;
83+ for (uint8_t i = 0 ; i < node->numEndpoints ; i++) {
84+ _initEndpoints[lastEp] = node->endpointType [i];
9185 lastEp++;
9286 }
93- modules_count++;
94- return lastEp - node->cb ->numEndpoints ;
87+ return true ;
9588 // restart USB layer???
9689}
9790
91+ PluggableUSB_& PluggableUSB ()
92+ {
93+ static PluggableUSB_ obj;
94+ return obj;
95+ }
96+
97+ PluggableUSB_::PluggableUSB_ () : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT),
98+ lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT),
99+ rootNode(NULL )
100+ {
101+ // Empty
102+ }
103+
98104#endif
99105
100- #endif /* if defined(USBCON) */
106+ #endif /* if defined(USBCON) */
0 commit comments