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