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