2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 00:19:16 2018 +0000
Revision:
43:78f328f311dc
2018.07.26

Who changed what in which revision?

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