]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBHost/USBHost/USBDeviceConnected.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / USBHost / USBHost / USBDeviceConnected.cpp
1 /* mbed USBHost Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "USBDeviceConnected.h"
18 #include "dbg.h"
19
20 USBDeviceConnected::USBDeviceConnected() {
21     init();
22 }
23
24 void USBDeviceConnected::init() {
25     hub_nb = 0;
26     port = 0;
27     vid = 0;
28     pid = 0;
29     nb_interf = 0;
30     enumerated = false;
31     activeAddr = false;
32     sizeControlEndpoint = 8;
33     device_class = 0;
34     device_subclass = 0;
35     proto = 0;
36     speed = false;
37     for (int i = 0; i < MAX_INTF; i++) {
38         memset((void *)&intf[i], 0, sizeof(INTERFACE));
39         intf[i].in_use = false;
40         for (int j = 0; j < MAX_ENDPOINT_PER_INTERFACE; j++) {
41             intf[i].ep[j] = NULL;
42             strcpy(intf[i].name, "Unknown");
43         }
44     }
45     hub_parent = NULL;
46     hub = NULL;
47     nb_interf = 0;
48 }
49
50 INTERFACE * USBDeviceConnected::getInterface(uint8_t index) {
51     if (index >= MAX_INTF)
52         return NULL;
53
54     if (intf[index].in_use)
55         return &intf[index];
56
57     return NULL;
58 }
59
60 bool USBDeviceConnected::addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) {
61     if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use)) {
62         return false;
63     }
64     intf[intf_nb].in_use = true;
65     intf[intf_nb].intf_class = intf_class;
66     intf[intf_nb].intf_subclass = intf_subclass;
67     intf[intf_nb].intf_protocol = intf_protocol;
68     intf[intf_nb].nb_endpoint = 0;
69     return true;
70 }
71
72 bool USBDeviceConnected::addEndpoint(uint8_t intf_nb, USBEndpoint * ept) {
73     if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use == false) || (intf[intf_nb].nb_endpoint >= MAX_ENDPOINT_PER_INTERFACE)) {
74         return false;
75     }
76     intf[intf_nb].nb_endpoint++;
77
78     for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
79         if (intf[intf_nb].ep[i] == NULL) {
80             intf[intf_nb].ep[i] = ept;
81             return true;
82         }
83     }
84     return false;
85 }
86
87 void USBDeviceConnected::init(uint8_t hub_, uint8_t port_, bool lowSpeed_) {
88     USB_DBG("init dev: %p", this);
89     init();
90     hub_nb = hub_;
91     port = port_;
92     speed = lowSpeed_;
93 }
94
95 void USBDeviceConnected::disconnect() {
96     for(int i = 0; i < MAX_INTF; i++) {
97         intf[i].detach.call();
98     }
99     init();
100 }
101
102
103 USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index) {
104     if (intf_nb >= MAX_INTF) {
105         return NULL;
106     }
107     for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
108         if ((intf[intf_nb].ep[i]->getType() == type) && (intf[intf_nb].ep[i]->getDir() == dir)) {
109             if(index) {
110                 index--;
111             } else {
112                 return intf[intf_nb].ep[i];
113             }
114         }
115     }
116     return NULL;
117 }
118
119 USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, uint8_t index) {
120     if ((intf_nb >= MAX_INTF) || (index >= MAX_ENDPOINT_PER_INTERFACE)) {
121         return NULL;
122     }
123     return intf[intf_nb].ep[index];
124 }