summaryrefslogtreecommitdiff
path: root/usb/generic.h
blob: d5c12ffa038425a1ffe1da0f588ff2ab11c54aee (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef GENERIC_H
#define GENERIC_H

#include <stdint.h>

#include <util/rblog.h>

RBLog<256, 2> usb_rblog;

struct desc_t {
	uint32_t size;
	void* data;
};

enum SetupStatus {Unhandled, Ok, Stall};
enum EPType {Control, Bulk, Interrupt, Isochronous};

class USB_class_driver {
	friend class USB_generic;
	
	protected:
		virtual SetupStatus handle_setup(uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint16_t wLength) {
			return SetupStatus::Unhandled;
		}
		
		virtual void handle_out(uint8_t ep, uint32_t len) {}
};

class USB_generic {
	private:
		desc_t dev_desc;
		desc_t conf_desc;
		
		USB_class_driver* control_handlers[4];
		USB_class_driver* out_handlers[6];
	
	public:
		USB_generic(desc_t dev, desc_t conf) : dev_desc(dev), conf_desc(conf) {}
		
		virtual bool ep_ready(uint32_t ep) = 0;
		virtual void write(uint32_t ep, uint32_t* bufp, uint32_t len) = 0;
		virtual uint32_t read(uint32_t ep, uint32_t* bufp, uint32_t len) = 0;
		virtual void hw_set_address(uint8_t addr) = 0;
		virtual void hw_conf_ep(uint8_t ep, EPType type, uint32_t size) = 0;
		virtual void hw_set_stall(uint8_t ep) = 0;
		
		bool register_control_handler(USB_class_driver* control_handler) {
			for(USB_class_driver*& handler : control_handlers) {
				if(!handler || handler == control_handler) {
					handler = control_handler;
					return true;
				}
			}
			
			// Handler table is full.
			return false;
		}
		
		bool register_out_handler(USB_class_driver* out_handler, uint8_t ep) {
			if(ep >= sizeof(out_handlers) / sizeof(*out_handlers)) {
				return false;
			}
			
			out_handlers[ep] = out_handler;
			
			return true;
		}
	
	protected:
		bool get_descriptor(uint16_t wValue, uint16_t wIndex, uint16_t wLength) {
			desc_t* descp;
			
			switch(wValue) {
				case 0x100:
					descp = &dev_desc;
					break;
				case 0x200:
					descp = &conf_desc;
					break;
				default:
					return false;
			}
			
			uint32_t* dp = (uint32_t*)descp->data;
			uint32_t length = wLength > descp->size ? descp->size : wLength;
			
			while(length > 64) {
				write(0, dp, 64);
				dp += 16;
				length -= 64;
				
				while(!ep_ready(0));
			}
			
			write(0, dp, length);
			
			return true;
		}
		
		bool set_address(uint16_t wValue, uint16_t wIndex, uint16_t wLength) {
			hw_set_address(wValue);
			write(0, 0, 0);
			return true;
		}
		
		bool set_configuration(uint16_t wValue, uint16_t wIndex, uint16_t wLength) {
			switch(wValue) {
				case 0:
					break;
				
				case 1:
					hw_conf_ep(0x01, Bulk, 64);
					hw_conf_ep(0x81, Bulk, 64);
					break;
				
				default:
					return false;
			}
			
			write(0, 0, 0);
			return true;
		}
		
		void handle_reset() {
			hw_conf_ep(0, Control, 64);
		}
		
		void handle_setup(const uint32_t* bufp) {
			uint8_t bmRequestType = bufp[0] & 0xff;
			uint8_t bRequest = (bufp[0] >> 8) & 0xff;
			uint16_t wValue = (bufp[0] >> 16) & 0xffff;
			uint16_t wIndex = bufp[1] & 0xffff;
			uint16_t wLength = (bufp[1] >> 16) & 0xffff;
			
			usb_rblog.log("handle_setup, bmRequestType=%02x, bRequest=%02x", bmRequestType, bRequest);
			
			out_handlers[0] = nullptr;
			
			// GET_DESCRIPTOR
			if(bmRequestType == 0x80 && bRequest == 0x06) {
				if(get_descriptor(wValue, wIndex, wLength)) {
					return;
				}
			}
			
			// SET_ADDRESS
			if(bmRequestType == 0x00 && bRequest == 0x05) {
				if(set_address(wValue, wIndex, wLength)) {
					return;
				}
			}
			
			// SET_CONFIGURATION
			if(bmRequestType == 0x00 && bRequest == 0x09) {
				if(set_configuration(wValue, wIndex, wLength)) {
					return;
				}
			}
			
			SetupStatus res = SetupStatus::Unhandled;
			
			for(USB_class_driver*& handler : control_handlers) {
				if(handler) {
					res = handler->handle_setup(bmRequestType, bRequest, wValue, wIndex, wLength);
					
					if(res == SetupStatus::Ok) {
						out_handlers[0] = handler;
					}
					
					if(res != SetupStatus::Unhandled) {
						break;
					}
				}
			}
			
			if(res == SetupStatus::Ok) {
				return;
			}
			
			hw_set_stall(0);
		}
		
		void handle_out(uint8_t ep, uint32_t len) {
			usb_rblog.log("handle_out, ep=%02x, len=%d", ep, len);
			
			if(out_handlers[ep]) {
				out_handlers[ep]->handle_out(ep, len);
			}
		}
};

#endif