summaryrefslogtreecommitdiff
path: root/fw/src/usbh_driver_hub_private.h
blob: 8c9bc44b538f55c9c31ad7de92c10c134d00fb1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * This file is part of the libusbhost library
 * hosted at http://github.com/libusbhost/libusbhost
 *
 * Copyright (C) 2015 Amir Hammad <amir.hammad@hotmail.com>
 *
 *
 * libusbhost is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef USBH_DRIVER_HUB_PRIVATE_
#define USBH_DRIVER_HUB_PRIVATE_

#include "usbh_config.h"
#include "driver/usbh_device_driver.h"
#include "usbh_driver_hub.h"

#include <stdint.h>
#include <stdbool.h>
#include <libopencm3/usb/usbstd.h>


// # HUB DEFINITIONS
#define HUB_FEATURE_PORT_CONNECTION 0
#define HUB_FEATURE_PORT_ENABLE 1
#define HUB_FEATURE_PORT_SUSPEND 2
#define HUB_FEATURE_PORT_OVERCURRENT 3
#define HUB_FEATURE_PORT_RESET 4
#define HUB_FEATURE_PORT_POWER 8
#define HUB_FEATURE_PORT_LOWSPEED 9
#define HUB_FEATURE_PORT_HIGHSPEED 10

#define HUB_FEATURE_C_PORT_CONNECTION 16
#define HUB_FEATURE_C_PORT_ENABLE 17
#define HUB_FEATURE_C_PORT_SUSPEND 18
#define HUB_FEATURE_C_PORT_OVERCURRENT 19
#define HUB_FEATURE_C_PORT_RESET 20

#define HUB_REQ_GET_STATUS 		0
#define HUB_REQ_CLEAR_FEATURE 	1
#define HUB_REQ_SET_FEATURE 	3
#define HUB_REQ_GET_DESCRIPTOR 	6

#define USB_DT_HUB 		(41)
#define USB_DT_HUB_SIZE	(9)
// Hub buffer: must be larger than hub descriptor
#define USBH_HUB_BUFFER_SIZE	(USB_DT_HUB_SIZE)


#define CURRENT_PORT_NONE -1

enum EVENT_STATE {
	EVENT_STATE_NONE,
	EVENT_STATE_INITIAL,
	EVENT_STATE_POLL_REQ,
	EVENT_STATE_POLL,
	EVENT_STATE_READ_HUB_DESCRIPTOR_COMPLETE,
	EVENT_STATE_ENABLE_PORTS,
	EVENT_STATE_GET_PORT_STATUS,
	EVENT_STATE_PORT_RESET_REQ,
	EVENT_STATE_PORT_RESET_COMPLETE,
	EVENT_STATE_SLEEP_500_MS,
	EVENT_STATE_GET_STATUS_COMPLETE,
};

struct _hub_device {
	usbh_device_t *device[USBH_HUB_MAX_DEVICES + 1];
	uint8_t buffer[USBH_HUB_BUFFER_SIZE];
	uint16_t endpoint_in_maxpacketsize;
	uint8_t endpoint_in_address;
	uint8_t endpoint_in_toggle;
	enum EVENT_STATE state;

	uint8_t desc_len;
	uint16_t ports_num;
	int8_t index;
	int8_t current_port;

	struct {
		uint16_t sts;
		uint16_t stc;
	} hub_and_port_status[USBH_HUB_MAX_DEVICES + 1];

	bool busy;

	uint32_t time_curr_us;
	uint32_t timestamp_us;
};

typedef struct _hub_device hub_device_t;

struct usb_hub_descriptor_head {
	uint8_t bDescLength;
	uint8_t bDescriptorType;
	uint8_t bNbrPorts;
	uint16_t wHubCharacteristics;
	uint8_t bPwrOn2PwrGood;
	uint8_t bHubContrCurrent;
} __attribute__((packed));
struct usb_hub_descriptor_body {
	uint8_t bDeviceRemovable;
	uint8_t PortPwrCtrlMask;
} __attribute__((packed));

// for hubs with up to 7 ports on hub
struct usb_hub_descriptor {
	struct usb_hub_descriptor_head head;
	struct usb_hub_descriptor_body body[1];
} __attribute__((packed));

#endif