2018.07.26

Dependencies:   FATFileSystem3 mbed-rtos

Fork of USBHost by mbed official

Committer:
sayzyas
Date:
Thu Jul 26 04:20:29 2018 +0000
Revision:
44:ef52682c423e
Parent:
43:1675750cca08
2018.07.26;

Who changed what in which revision?

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