1 | /*
|
---|
2 | * Copyright (c) 2013 Jan Vesely
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 | /** @addtogroup drvusbehci
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /** @file
|
---|
32 | * @brief EHCI driver
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <assert.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <mem.h>
|
---|
38 | #include <sys/types.h>
|
---|
39 |
|
---|
40 | #include <usb/classes/hub.h>
|
---|
41 | #include <usb/debug.h>
|
---|
42 | #include <usb/descriptor.h>
|
---|
43 | #include <usb/request.h>
|
---|
44 | #include <usb/usb.h>
|
---|
45 |
|
---|
46 | #include <usb/host/endpoint.h>
|
---|
47 | #include <usbvirt/device.h>
|
---|
48 |
|
---|
49 | #include "ehci_rh.h"
|
---|
50 |
|
---|
51 | enum {
|
---|
52 | HUB_STATUS_CHANGE_PIPE = 1,
|
---|
53 | };
|
---|
54 |
|
---|
55 | static usbvirt_device_ops_t ops;
|
---|
56 |
|
---|
57 | /** Initialize internal USB HUB class descriptor.
|
---|
58 | * @param instance EHCI root hub.
|
---|
59 | * Use register based info to create accurate descriptor.
|
---|
60 | */
|
---|
61 | static void ehci_rh_hub_desc_init(ehci_rh_t *instance, unsigned hcs)
|
---|
62 | {
|
---|
63 | assert(instance);
|
---|
64 | const unsigned dsize = sizeof(usb_hub_descriptor_header_t) +
|
---|
65 | STATUS_BYTES(instance->port_count) * 2;
|
---|
66 | assert(dsize <= sizeof(instance->hub_descriptor));
|
---|
67 |
|
---|
68 | instance->hub_descriptor.header.length = dsize;
|
---|
69 | instance->hub_descriptor.header.descriptor_type = USB_DESCTYPE_HUB;
|
---|
70 | instance->hub_descriptor.header.port_count = instance->port_count;
|
---|
71 | /* Bits 0,1 indicate power switching mode
|
---|
72 | * Bit 2 indicates device type (compound device)
|
---|
73 | * Bits 3,4 indicate over-current protection mode */
|
---|
74 | instance->hub_descriptor.header.characteristics = 0 |
|
---|
75 | ((hcs & EHCI_CAPS_HCS_PPC_FLAG) ? 0x09 : 0x12) |
|
---|
76 | ((hcs & EHCI_CAPS_HCS_INDICATORS_FLAG) ? 0x80 : 0) |
|
---|
77 | (0x3 << 5); /* Need 32 FS bit times */ // TODO Implement
|
---|
78 | instance->hub_descriptor.header.characteristics_reserved = 0;
|
---|
79 | instance->hub_descriptor.header.power_good_time = 50;
|
---|
80 | /* bHubContrCurrent, root hubs don't need no power. */
|
---|
81 | instance->hub_descriptor.header.max_current = 0;
|
---|
82 |
|
---|
83 | /* Device removable and some legacy 1.0 stuff*/
|
---|
84 | instance->hub_descriptor.rempow[0] = 0xff;
|
---|
85 | instance->hub_descriptor.rempow[1] = 0xff;
|
---|
86 | instance->hub_descriptor.rempow[2] = 0xff;
|
---|
87 | instance->hub_descriptor.rempow[3] = 0xff;
|
---|
88 |
|
---|
89 | }
|
---|
90 | /** Initialize EHCI root hub.
|
---|
91 | * @param instance Place to initialize.
|
---|
92 | * @param regs EHCI device registers.
|
---|
93 | * @param name Device name.
|
---|
94 | * return Error code, EOK on success.
|
---|
95 | *
|
---|
96 | * Selects preconfigured port powering mode, sets up descriptor, and
|
---|
97 | * initializes internal virtual hub.
|
---|
98 | */
|
---|
99 | int ehci_rh_init(ehci_rh_t *instance, ehci_caps_regs_t *caps, ehci_regs_t *regs,
|
---|
100 | const char *name)
|
---|
101 | {
|
---|
102 | assert(instance);
|
---|
103 | instance->registers = regs;
|
---|
104 | instance->port_count =
|
---|
105 | (EHCI_RD(caps->hcsparams) >> EHCI_CAPS_HCS_N_PORTS_SHIFT) &
|
---|
106 | EHCI_CAPS_HCS_N_PORTS_MASK;
|
---|
107 | usb_log_debug2("hcsparams: %x.\n", EHCI_RD(caps->hcsparams));
|
---|
108 | usb_log_info("%s: Found %u ports.\n", name, instance->port_count);
|
---|
109 |
|
---|
110 | if (EHCI_RD(caps->hcsparams) & EHCI_CAPS_HCS_PPC_FLAG) {
|
---|
111 | usb_log_info("%s: Per-port power switching.\n", name);
|
---|
112 | } else {
|
---|
113 | usb_log_info("%s: No power switching.\n", name);
|
---|
114 | }
|
---|
115 |
|
---|
116 | for (unsigned i = 0; i < EHCI_MAX_PORTS; ++i) {
|
---|
117 | instance->reset_flag[i] = false;
|
---|
118 | instance->resume_flag[i] = false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | ehci_rh_hub_desc_init(instance, EHCI_RD(caps->hcsparams));
|
---|
122 | instance->unfinished_interrupt_transfer = NULL;
|
---|
123 |
|
---|
124 | return virthub_base_init(&instance->base, name, &ops, instance,
|
---|
125 | NULL, &instance->hub_descriptor.header, HUB_STATUS_CHANGE_PIPE);
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Schedule USB request.
|
---|
129 | * @param instance OCHI root hub instance.
|
---|
130 | * @param batch USB requst batch to schedule.
|
---|
131 | * @return Always EOK.
|
---|
132 | * Most requests complete even before this function returns,
|
---|
133 | * status change requests might be postponed until there is something to report.
|
---|
134 | */
|
---|
135 | int ehci_rh_schedule(ehci_rh_t *instance, usb_transfer_batch_t *batch)
|
---|
136 | {
|
---|
137 | assert(instance);
|
---|
138 | assert(batch);
|
---|
139 | const usb_target_t target = {{
|
---|
140 | .address = batch->ep->address,
|
---|
141 | .endpoint = batch->ep->endpoint,
|
---|
142 | }};
|
---|
143 | batch->error = virthub_base_request(&instance->base, target,
|
---|
144 | usb_transfer_batch_direction(batch), (void*)batch->setup_buffer,
|
---|
145 | batch->buffer, batch->buffer_size, &batch->transfered_size);
|
---|
146 | if (batch->error == ENAK) {
|
---|
147 | /* This is safe because only status change interrupt transfers
|
---|
148 | * return NAK. The assertion holds true because the batch
|
---|
149 | * existence prevents communication with that ep */
|
---|
150 | assert(instance->unfinished_interrupt_transfer == NULL);
|
---|
151 | instance->unfinished_interrupt_transfer = batch;
|
---|
152 | } else {
|
---|
153 | usb_transfer_batch_finish(batch, NULL);
|
---|
154 | usb_transfer_batch_destroy(batch);
|
---|
155 | }
|
---|
156 | return EOK;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /** Handle EHCI RHSC interrupt.
|
---|
160 | * @param instance EHCI root hub isntance.
|
---|
161 | * @return Always EOK.
|
---|
162 | *
|
---|
163 | * Interrupt means there is a change of status to report. It may trigger
|
---|
164 | * processing of a postponed request.
|
---|
165 | */
|
---|
166 | int ehci_rh_interrupt(ehci_rh_t *instance)
|
---|
167 | {
|
---|
168 | //TODO atomic swap needed
|
---|
169 | usb_transfer_batch_t *batch = instance->unfinished_interrupt_transfer;
|
---|
170 | instance->unfinished_interrupt_transfer = NULL;
|
---|
171 | if (batch) {
|
---|
172 | const usb_target_t target = {{
|
---|
173 | .address = batch->ep->address,
|
---|
174 | .endpoint = batch->ep->endpoint,
|
---|
175 | }};
|
---|
176 | batch->error = virthub_base_request(&instance->base, target,
|
---|
177 | usb_transfer_batch_direction(batch),
|
---|
178 | (void*)batch->setup_buffer,
|
---|
179 | batch->buffer, batch->buffer_size, &batch->transfered_size);
|
---|
180 | usb_transfer_batch_finish(batch, NULL);
|
---|
181 | usb_transfer_batch_destroy(batch);
|
---|
182 | }
|
---|
183 | return EOK;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /* HUB ROUTINES IMPLEMENTATION */
|
---|
187 | #define TEST_SIZE_INIT(size, port, hub) \
|
---|
188 | do { \
|
---|
189 | hub = virthub_get_data(device); \
|
---|
190 | assert(hub);\
|
---|
191 | if (uint16_usb2host(setup_packet->length) != size) \
|
---|
192 | return ESTALL; \
|
---|
193 | port = uint16_usb2host(setup_packet->index) - 1; \
|
---|
194 | if (port > hub->port_count) \
|
---|
195 | return EINVAL; \
|
---|
196 | } while (0)
|
---|
197 |
|
---|
198 | /** Hub status request handler.
|
---|
199 | * @param device Virtual hub device
|
---|
200 | * @param setup_packet USB setup stage data.
|
---|
201 | * @param[out] data destination data buffer, size must be at least
|
---|
202 | * setup_packet->length bytes
|
---|
203 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
204 | * @return Error code.
|
---|
205 | */
|
---|
206 | static int req_get_status(usbvirt_device_t *device,
|
---|
207 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
208 | uint8_t *data, size_t *act_size)
|
---|
209 | {
|
---|
210 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
211 | assert(hub);
|
---|
212 | if (uint16_usb2host(setup_packet->length) != 4)
|
---|
213 | return ESTALL;
|
---|
214 | /* ECHI RH does not report global OC, and local power is always good */
|
---|
215 | const uint32_t val = 0;
|
---|
216 | memcpy(data, &val, sizeof(val));
|
---|
217 | *act_size = sizeof(val);
|
---|
218 | return EOK;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** Hub set feature request handler.
|
---|
222 | * @param device Virtual hub device
|
---|
223 | * @param setup_packet USB setup stage data.
|
---|
224 | * @param[out] data destination data buffer, size must be at least
|
---|
225 | * setup_packet->length bytes
|
---|
226 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
227 | * @return Error code.
|
---|
228 | */
|
---|
229 | static int req_clear_hub_feature(usbvirt_device_t *device,
|
---|
230 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
231 | uint8_t *data, size_t *act_size)
|
---|
232 | {
|
---|
233 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
234 | assert(hub);
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
---|
238 | * C_HUB_OVER_CURRENT are supported.
|
---|
239 | * C_HUB_LOCAL_POWER is not supported because root hubs do not support
|
---|
240 | * local power status feature.
|
---|
241 | * EHCI RH does not report global OC condition either
|
---|
242 | */
|
---|
243 | return ESTALL;
|
---|
244 | }
|
---|
245 |
|
---|
246 | #define BIT_VAL(val, bit) ((val & bit) ? 1 : 0)
|
---|
247 | #define EHCI2USB(val, bit, feat) (BIT_VAL(val, bit) << feat)
|
---|
248 |
|
---|
249 | /** Port status request handler.
|
---|
250 | * @param device Virtual hub device
|
---|
251 | * @param setup_packet USB setup stage data.
|
---|
252 | * @param[out] data destination data buffer, size must be at least
|
---|
253 | * setup_packet->length bytes
|
---|
254 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
255 | * @return Error code.
|
---|
256 | */
|
---|
257 | static int req_get_port_status(usbvirt_device_t *device,
|
---|
258 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
259 | uint8_t *data, size_t *act_size)
|
---|
260 | {
|
---|
261 | ehci_rh_t *hub;
|
---|
262 | unsigned port;
|
---|
263 | TEST_SIZE_INIT(4, port, hub);
|
---|
264 | if (setup_packet->value != 0)
|
---|
265 | return EINVAL;
|
---|
266 |
|
---|
267 | const uint32_t reg = EHCI_RD(hub->registers->portsc[port]);
|
---|
268 | const uint32_t status = uint32_host2usb(
|
---|
269 | EHCI2USB(reg, USB_PORTSC_CONNECT_FLAG, USB_HUB_FEATURE_PORT_CONNECTION) |
|
---|
270 | EHCI2USB(reg, USB_PORTSC_ENABLED_FLAG, USB_HUB_FEATURE_PORT_ENABLE) |
|
---|
271 | EHCI2USB(reg, USB_PORTSC_SUSPEND_FLAG, USB_HUB_FEATURE_PORT_SUSPEND) |
|
---|
272 | EHCI2USB(reg, USB_PORTSC_OC_ACTIVE_FLAG, USB_HUB_FEATURE_PORT_OVER_CURRENT) |
|
---|
273 | EHCI2USB(reg, USB_PORTSC_PORT_RESET_FLAG, USB_HUB_FEATURE_PORT_RESET) |
|
---|
274 | EHCI2USB(reg, USB_PORTSC_PORT_POWER_FLAG, USB_HUB_FEATURE_PORT_POWER) |
|
---|
275 | (((reg & USB_PORTSC_LINE_STATUS_MASK) == USB_PORTSC_LINE_STATUS_K) ?
|
---|
276 | (1 << USB_HUB_FEATURE_PORT_LOW_SPEED) : 0) |
|
---|
277 | ((reg & USB_PORTSC_PORT_OWNER_FLAG) ? 0 : (1 << USB_HUB_FEATURE_PORT_HIGH_SPEED)) |
|
---|
278 | EHCI2USB(reg, USB_PORTSC_PORT_TEST_MASK, 11) |
|
---|
279 | EHCI2USB(reg, USB_PORTSC_INDICATOR_MASK, 12) |
|
---|
280 | EHCI2USB(reg, USB_PORTSC_CONNECT_CH_FLAG, USB_HUB_FEATURE_C_PORT_CONNECTION) |
|
---|
281 | EHCI2USB(reg, USB_PORTSC_EN_CHANGE_FLAG, USB_HUB_FEATURE_C_PORT_ENABLE) |
|
---|
282 | (hub->resume_flag[port] ? (1 << USB_HUB_FEATURE_C_PORT_SUSPEND) : 0) |
|
---|
283 | EHCI2USB(reg, USB_PORTSC_OC_CHANGE_FLAG, USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
|
---|
284 | (hub->reset_flag[port] ? (1 << USB_HUB_FEATURE_C_PORT_RESET): 0)
|
---|
285 | );
|
---|
286 | /* Note feature numbers for test and indicator feature do not
|
---|
287 | * corespond to the port status bit locations */
|
---|
288 | memcpy(data, &status, sizeof(status));
|
---|
289 | *act_size = sizeof(status);
|
---|
290 | return EOK;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** Port clear feature request handler.
|
---|
294 | * @param device Virtual hub device
|
---|
295 | * @param setup_packet USB setup stage data.
|
---|
296 | * @param[out] data destination data buffer, size must be at least
|
---|
297 | * setup_packet->length bytes
|
---|
298 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
299 | * @return Error code.
|
---|
300 | */
|
---|
301 | static int req_clear_port_feature(usbvirt_device_t *device,
|
---|
302 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
303 | uint8_t *data, size_t *act_size)
|
---|
304 | {
|
---|
305 | ehci_rh_t *hub;
|
---|
306 | unsigned port;
|
---|
307 | TEST_SIZE_INIT(0, port, hub);
|
---|
308 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
309 | /* Enabled features to clear: see page 269 of USB specs */
|
---|
310 | switch (feature)
|
---|
311 | {
|
---|
312 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
313 | EHCI_CLR(hub->registers->portsc[port],
|
---|
314 | USB_PORTSC_PORT_POWER_FLAG);
|
---|
315 | return EOK;
|
---|
316 |
|
---|
317 | case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
318 | EHCI_CLR(hub->registers->portsc[port],
|
---|
319 | USB_PORTSC_ENABLED_FLAG);
|
---|
320 | return EOK;
|
---|
321 |
|
---|
322 | case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
323 | /* If not in suspend it's noop */
|
---|
324 | if ((EHCI_RD(hub->registers->portsc[port]) &
|
---|
325 | USB_PORTSC_SUSPEND_FLAG) == 0)
|
---|
326 | return EOK;
|
---|
327 | /* Host driven resume */
|
---|
328 | EHCI_SET(hub->registers->portsc[port],
|
---|
329 | USB_PORTSC_RESUME_FLAG);
|
---|
330 | async_usleep(20000);
|
---|
331 | EHCI_CLR(hub->registers->portsc[port],
|
---|
332 | USB_PORTSC_RESUME_FLAG);
|
---|
333 | hub->resume_flag[port] = true;
|
---|
334 | return EOK;
|
---|
335 |
|
---|
336 | case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
|
---|
337 | EHCI_SET(hub->registers->portsc[port],
|
---|
338 | USB_PORTSC_CONNECT_CH_FLAG);
|
---|
339 | return EOK;
|
---|
340 | case USB_HUB_FEATURE_C_PORT_ENABLE: /*17*/
|
---|
341 | EHCI_SET(hub->registers->portsc[port],
|
---|
342 | USB_PORTSC_CONNECT_CH_FLAG);
|
---|
343 | return EOK;
|
---|
344 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
|
---|
345 | EHCI_SET(hub->registers->portsc[port],
|
---|
346 | USB_PORTSC_OC_CHANGE_FLAG);
|
---|
347 | return EOK;
|
---|
348 | case USB_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
|
---|
349 | hub->resume_flag[port] = false;
|
---|
350 | return EOK;
|
---|
351 | case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
|
---|
352 | hub->reset_flag[port] = false;
|
---|
353 | return EOK;
|
---|
354 |
|
---|
355 | default:
|
---|
356 | return ENOTSUP;
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | /** Port set feature request handler.
|
---|
361 | * @param device Virtual hub device
|
---|
362 | * @param setup_packet USB setup stage data.
|
---|
363 | * @param[out] data destination data buffer, size must be at least
|
---|
364 | * setup_packet->length bytes
|
---|
365 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
366 | * @return Error code.
|
---|
367 | */
|
---|
368 | static int req_set_port_feature(usbvirt_device_t *device,
|
---|
369 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
370 | uint8_t *data, size_t *act_size)
|
---|
371 | {
|
---|
372 | ehci_rh_t *hub;
|
---|
373 | unsigned port;
|
---|
374 | TEST_SIZE_INIT(0, port, hub);
|
---|
375 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
376 | switch (feature) {
|
---|
377 | case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
378 | EHCI_SET(hub->registers->portsc[port],
|
---|
379 | USB_PORTSC_ENABLED_FLAG);
|
---|
380 | return EOK;
|
---|
381 | case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
382 | EHCI_SET(hub->registers->portsc[port],
|
---|
383 | USB_PORTSC_SUSPEND_FLAG);
|
---|
384 | return EOK;
|
---|
385 | case USB_HUB_FEATURE_PORT_RESET: /*4*/
|
---|
386 | EHCI_SET(hub->registers->portsc[port],
|
---|
387 | USB_PORTSC_PORT_RESET_FLAG);
|
---|
388 | async_usleep(50000);
|
---|
389 | EHCI_CLR(hub->registers->portsc[port],
|
---|
390 | USB_PORTSC_PORT_RESET_FLAG);
|
---|
391 | /* wait for reset to complete */
|
---|
392 | while (EHCI_RD(hub->registers->portsc[port]) &
|
---|
393 | USB_PORTSC_PORT_RESET_FLAG);
|
---|
394 | /* Handle port ownership, if the port is not enabled
|
---|
395 | * after reset it's a full speed device */
|
---|
396 | if (!(EHCI_RD(hub->registers->portsc[port]) &
|
---|
397 | USB_PORTSC_ENABLED_FLAG)) {
|
---|
398 | EHCI_SET(hub->registers->portsc[port],
|
---|
399 | USB_PORTSC_PORT_OWNER_FLAG);
|
---|
400 | } else {
|
---|
401 | hub->reset_flag[port] = true;
|
---|
402 | }
|
---|
403 |
|
---|
404 | return EOK;
|
---|
405 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
406 | EHCI_SET(hub->registers->portsc[port],
|
---|
407 | USB_PORTSC_PORT_POWER_FLAG);
|
---|
408 | return EOK;
|
---|
409 | default:
|
---|
410 | return ENOTSUP;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | /** Status change handler.
|
---|
415 | * @param device Virtual hub device
|
---|
416 | * @param endpoint Endpoint number
|
---|
417 | * @param tr_type Transfer type
|
---|
418 | * @param buffer Response destination
|
---|
419 | * @param buffer_size Bytes available in buffer
|
---|
420 | * @param actual_size Size us the used part of the dest buffer.
|
---|
421 | *
|
---|
422 | * Produces status mask. Bit 0 indicates hub status change the other bits
|
---|
423 | * represent port status change. Endian does not matter as UHCI root hubs
|
---|
424 | * only need 1 byte.
|
---|
425 | */
|
---|
426 | static int req_status_change_handler(usbvirt_device_t *device,
|
---|
427 | usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
|
---|
428 | void *buffer, size_t buffer_size, size_t *actual_size)
|
---|
429 | {
|
---|
430 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
431 | assert(hub);
|
---|
432 |
|
---|
433 | if (buffer_size < STATUS_BYTES(hub->port_count))
|
---|
434 | return ESTALL;
|
---|
435 |
|
---|
436 | uint16_t mask = 0;
|
---|
437 | for (unsigned port = 0; port < hub->port_count; ++port) {
|
---|
438 | /* Write-clean bits are those that indicate change */
|
---|
439 | uint32_t status = EHCI_RD(hub->registers->portsc[port]);
|
---|
440 | if ((status & USB_PORTSC_WC_MASK)
|
---|
441 | || hub->reset_flag[port] || hub->reset_flag[port]) {
|
---|
442 | /* Ignore new LS device */
|
---|
443 | if ((status & USB_PORTSC_CONNECT_CH_FLAG) &&
|
---|
444 | (status & USB_PORTSC_LINE_STATUS_MASK) ==
|
---|
445 | USB_PORTSC_LINE_STATUS_K)
|
---|
446 | EHCI_SET(hub->registers->portsc[port],
|
---|
447 | USB_PORTSC_PORT_OWNER_FLAG);
|
---|
448 | else
|
---|
449 | mask |= (2 << port);
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | usb_log_debug2("EHCI root hub interrupt mask: %hx.\n", mask);
|
---|
454 |
|
---|
455 | if (mask == 0)
|
---|
456 | return ENAK;
|
---|
457 | mask = uint16_host2usb(mask);
|
---|
458 | memcpy(buffer, &mask, STATUS_BYTES(hub->port_count));
|
---|
459 | *actual_size = STATUS_BYTES(hub->port_count);
|
---|
460 | return EOK;
|
---|
461 | }
|
---|
462 |
|
---|
463 | /** EHCI root hub request handlers */
|
---|
464 | static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
|
---|
465 | {
|
---|
466 | STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
467 | .name = "GetDescriptor",
|
---|
468 | .callback = virthub_base_get_hub_descriptor,
|
---|
469 | },
|
---|
470 | {
|
---|
471 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
472 | .name = "GetDescriptor",
|
---|
473 | .callback = virthub_base_get_hub_descriptor,
|
---|
474 | },
|
---|
475 | {
|
---|
476 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
|
---|
477 | .name = "GetHubDescriptor",
|
---|
478 | .callback = virthub_base_get_hub_descriptor,
|
---|
479 | },
|
---|
480 | {
|
---|
481 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
|
---|
482 | .name = "GetPortStatus",
|
---|
483 | .callback = req_get_port_status,
|
---|
484 | },
|
---|
485 | {
|
---|
486 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
487 | .name = "ClearHubFeature",
|
---|
488 | .callback = req_clear_hub_feature,
|
---|
489 | },
|
---|
490 | {
|
---|
491 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
492 | .name = "ClearPortFeature",
|
---|
493 | .callback = req_clear_port_feature,
|
---|
494 | },
|
---|
495 | {
|
---|
496 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
|
---|
497 | .name = "GetHubStatus",
|
---|
498 | .callback = req_get_status,
|
---|
499 | },
|
---|
500 | {
|
---|
501 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
|
---|
502 | .name = "GetPortStatus",
|
---|
503 | .callback = req_get_port_status,
|
---|
504 | },
|
---|
505 | {
|
---|
506 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
|
---|
507 | .name = "SetHubFeature",
|
---|
508 | .callback = req_nop,
|
---|
509 | },
|
---|
510 | {
|
---|
511 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
|
---|
512 | .name = "SetPortFeature",
|
---|
513 | .callback = req_set_port_feature,
|
---|
514 | },
|
---|
515 | {
|
---|
516 | .callback = NULL
|
---|
517 | }
|
---|
518 | };
|
---|
519 |
|
---|
520 | /** Virtual EHCI root hub ops */
|
---|
521 | static usbvirt_device_ops_t ops = {
|
---|
522 | .control = control_transfer_handlers,
|
---|
523 | .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
|
---|
524 | };
|
---|