2018.07.26

Dependencies:   FATFileSystem2 mbed-rtos

Fork of USBHost by mbed official

Committer:
samux
Date:
Tue Mar 12 17:23:37 2013 +0000
Revision:
4:b320d68e98e7
Parent:
0:a554658735bf
Child:
8:93da8ea2708b
bug fixes - support composite device

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:a554658735bf 1 /* Copyright (c) 2010-2012 mbed.org, MIT License
mbed_official 0:a554658735bf 2 *
mbed_official 0:a554658735bf 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mbed_official 0:a554658735bf 4 * and associated documentation files (the "Software"), to deal in the Software without
mbed_official 0:a554658735bf 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
mbed_official 0:a554658735bf 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
mbed_official 0:a554658735bf 7 * Software is furnished to do so, subject to the following conditions:
mbed_official 0:a554658735bf 8 *
mbed_official 0:a554658735bf 9 * The above copyright notice and this permission notice shall be included in all copies or
mbed_official 0:a554658735bf 10 * substantial portions of the Software.
mbed_official 0:a554658735bf 11 *
mbed_official 0:a554658735bf 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mbed_official 0:a554658735bf 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mbed_official 0:a554658735bf 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mbed_official 0:a554658735bf 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mbed_official 0:a554658735bf 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mbed_official 0:a554658735bf 17 */
mbed_official 0:a554658735bf 18
mbed_official 0:a554658735bf 19 #include "USBDeviceConnected.h"
mbed_official 0:a554658735bf 20 #include "dbg.h"
mbed_official 0:a554658735bf 21
mbed_official 0:a554658735bf 22 USBDeviceConnected::USBDeviceConnected() {
mbed_official 0:a554658735bf 23 init();
mbed_official 0:a554658735bf 24 }
mbed_official 0:a554658735bf 25
mbed_official 0:a554658735bf 26 void USBDeviceConnected::init() {
mbed_official 0:a554658735bf 27 hub_nb = 0;
mbed_official 0:a554658735bf 28 port = 0;
mbed_official 0:a554658735bf 29 vid = 0;
mbed_official 0:a554658735bf 30 pid = 0;
mbed_official 0:a554658735bf 31 nb_interf = 0;
mbed_official 0:a554658735bf 32 enumerated = false;
mbed_official 0:a554658735bf 33 activeAddr = false;
mbed_official 0:a554658735bf 34 sizeControlEndpoint = 8;
mbed_official 0:a554658735bf 35 device_class = 0;
mbed_official 0:a554658735bf 36 device_subclass = 0;
mbed_official 0:a554658735bf 37 proto = 0;
mbed_official 0:a554658735bf 38 speed = false;
mbed_official 0:a554658735bf 39 for (int i = 0; i < MAX_INTF; i++) {
mbed_official 0:a554658735bf 40 memset((void *)&intf[i], 0, sizeof(INTERFACE));
mbed_official 0:a554658735bf 41 intf[i].in_use = false;
mbed_official 0:a554658735bf 42 for (int j = 0; j < MAX_ENDPOINT_PER_INTERFACE; j++) {
mbed_official 0:a554658735bf 43 intf[i].ep[j] = NULL;
samux 4:b320d68e98e7 44 strcpy(intf[i].name, "Unknown");
mbed_official 0:a554658735bf 45 }
mbed_official 0:a554658735bf 46 }
mbed_official 0:a554658735bf 47 hub_parent = NULL;
mbed_official 0:a554658735bf 48 hub = NULL;
samux 4:b320d68e98e7 49 nb_interf = 0;
mbed_official 0:a554658735bf 50 }
mbed_official 0:a554658735bf 51
mbed_official 0:a554658735bf 52 INTERFACE * USBDeviceConnected::getInterface(uint8_t index) {
samux 4:b320d68e98e7 53 if (index >= MAX_INTF)
mbed_official 0:a554658735bf 54 return NULL;
samux 4:b320d68e98e7 55
samux 4:b320d68e98e7 56 if (intf[index].in_use)
samux 4:b320d68e98e7 57 return &intf[index];
samux 4:b320d68e98e7 58
samux 4:b320d68e98e7 59 return NULL;
mbed_official 0:a554658735bf 60 }
mbed_official 0:a554658735bf 61
mbed_official 0:a554658735bf 62 bool USBDeviceConnected::addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) {
mbed_official 0:a554658735bf 63 if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use)) {
mbed_official 0:a554658735bf 64 return false;
mbed_official 0:a554658735bf 65 }
mbed_official 0:a554658735bf 66 intf[intf_nb].in_use = true;
mbed_official 0:a554658735bf 67 intf[intf_nb].intf_class = intf_class;
mbed_official 0:a554658735bf 68 intf[intf_nb].intf_subclass = intf_subclass;
mbed_official 0:a554658735bf 69 intf[intf_nb].intf_protocol = intf_protocol;
mbed_official 0:a554658735bf 70 intf[intf_nb].nb_endpoint = 0;
mbed_official 0:a554658735bf 71 return true;
mbed_official 0:a554658735bf 72 }
mbed_official 0:a554658735bf 73
mbed_official 0:a554658735bf 74 bool USBDeviceConnected::addEndpoint(uint8_t intf_nb, USBEndpoint * ept) {
mbed_official 0:a554658735bf 75 if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use == false) || (intf[intf_nb].nb_endpoint >= MAX_ENDPOINT_PER_INTERFACE)) {
mbed_official 0:a554658735bf 76 return false;
mbed_official 0:a554658735bf 77 }
mbed_official 0:a554658735bf 78 intf[intf_nb].nb_endpoint++;
mbed_official 0:a554658735bf 79
mbed_official 0:a554658735bf 80 for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
mbed_official 0:a554658735bf 81 if (intf[intf_nb].ep[i] == NULL) {
mbed_official 0:a554658735bf 82 intf[intf_nb].ep[i] = ept;
mbed_official 0:a554658735bf 83 return true;
mbed_official 0:a554658735bf 84 }
mbed_official 0:a554658735bf 85 }
mbed_official 0:a554658735bf 86 return false;
mbed_official 0:a554658735bf 87 }
mbed_official 0:a554658735bf 88
mbed_official 0:a554658735bf 89 void USBDeviceConnected::init(uint8_t hub_, uint8_t port_, bool lowSpeed_) {
mbed_official 0:a554658735bf 90 USB_DBG("init dev: %p", this);
mbed_official 0:a554658735bf 91 init();
mbed_official 0:a554658735bf 92 hub_nb = hub_;
mbed_official 0:a554658735bf 93 port = port_;
mbed_official 0:a554658735bf 94 speed = lowSpeed_;
mbed_official 0:a554658735bf 95 }
mbed_official 0:a554658735bf 96
mbed_official 0:a554658735bf 97 void USBDeviceConnected::disconnect() {
samux 4:b320d68e98e7 98 for(int i = 0; i < MAX_INTF; i++) {
mbed_official 0:a554658735bf 99 intf[i].detach.call();
mbed_official 0:a554658735bf 100 }
mbed_official 0:a554658735bf 101 init();
mbed_official 0:a554658735bf 102 }
mbed_official 0:a554658735bf 103
mbed_official 0:a554658735bf 104
mbed_official 0:a554658735bf 105 USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index) {
mbed_official 0:a554658735bf 106 if (intf_nb >= MAX_INTF) {
mbed_official 0:a554658735bf 107 return NULL;
mbed_official 0:a554658735bf 108 }
mbed_official 0:a554658735bf 109 for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
mbed_official 0:a554658735bf 110 if ((intf[intf_nb].ep[i]->getType() == type) && (intf[intf_nb].ep[i]->getDir() == dir)) {
mbed_official 0:a554658735bf 111 if(index) {
mbed_official 0:a554658735bf 112 index--;
mbed_official 0:a554658735bf 113 } else {
mbed_official 0:a554658735bf 114 return intf[intf_nb].ep[i];
mbed_official 0:a554658735bf 115 }
mbed_official 0:a554658735bf 116 }
mbed_official 0:a554658735bf 117 }
mbed_official 0:a554658735bf 118 return NULL;
mbed_official 0:a554658735bf 119 }
mbed_official 0:a554658735bf 120
mbed_official 0:a554658735bf 121 USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, uint8_t index) {
mbed_official 0:a554658735bf 122 if ((intf_nb >= MAX_INTF) || (index >= MAX_ENDPOINT_PER_INTERFACE)) {
mbed_official 0:a554658735bf 123 return NULL;
mbed_official 0:a554658735bf 124 }
mbed_official 0:a554658735bf 125 return intf[intf_nb].ep[index];
mbed_official 0:a554658735bf 126 }