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