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 | int ehci_rh_init(ehci_rh_t *instance, ehci_caps_regs_t *caps, ehci_regs_t *regs,
|
---|
102 | 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.\n", instance,
|
---|
110 | EHCI_RD(caps->hcsparams));
|
---|
111 | usb_log_info("RH(%p): Found %u ports.\n", 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->unfinished_interrupt_transfer = NULL;
|
---|
130 |
|
---|
131 | return virthub_base_init(&instance->base, name, &ops, instance,
|
---|
132 | NULL, &instance->hub_descriptor.header, HUB_STATUS_CHANGE_PIPE);
|
---|
133 | }
|
---|
134 |
|
---|
135 | /** Schedule USB request.
|
---|
136 | * @param instance OCHI root hub instance.
|
---|
137 | * @param batch USB requst batch to schedule.
|
---|
138 | * @return Always EOK.
|
---|
139 | * Most requests complete even before this function returns,
|
---|
140 | * status change requests might be postponed until there is something to report.
|
---|
141 | */
|
---|
142 | int ehci_rh_schedule(ehci_rh_t *instance, usb_transfer_batch_t *batch)
|
---|
143 | {
|
---|
144 | assert(instance);
|
---|
145 | assert(batch);
|
---|
146 | const usb_target_t target = batch->ep->target;
|
---|
147 | batch->error = virthub_base_request(&instance->base, target,
|
---|
148 | usb_transfer_batch_direction(batch), (void*)batch->setup_buffer,
|
---|
149 | batch->buffer, batch->buffer_size, &batch->transfered_size);
|
---|
150 | if (batch->error == ENAK) {
|
---|
151 | usb_log_debug("RH(%p): BATCH(%p) adding as unfinished",
|
---|
152 | instance, batch);
|
---|
153 | /* This is safe because only status change interrupt transfers
|
---|
154 | * return NAK. The assertion holds true because the batch
|
---|
155 | * existence prevents communication with that ep */
|
---|
156 | assert(instance->unfinished_interrupt_transfer == NULL);
|
---|
157 | instance->unfinished_interrupt_transfer = batch;
|
---|
158 | } else {
|
---|
159 | usb_transfer_batch_finish(batch, NULL);
|
---|
160 | usb_transfer_batch_destroy(batch);
|
---|
161 | usb_log_debug("RH(%p): BATCH(%p) virtual request complete: %s",
|
---|
162 | instance, batch, str_error(batch->error));
|
---|
163 | }
|
---|
164 | return EOK;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /** Handle EHCI RHSC interrupt.
|
---|
168 | * @param instance EHCI root hub isntance.
|
---|
169 | * @return Always EOK.
|
---|
170 | *
|
---|
171 | * Interrupt means there is a change of status to report. It may trigger
|
---|
172 | * processing of a postponed request.
|
---|
173 | */
|
---|
174 | int ehci_rh_interrupt(ehci_rh_t *instance)
|
---|
175 | {
|
---|
176 | //TODO atomic swap needed
|
---|
177 | usb_transfer_batch_t *batch = instance->unfinished_interrupt_transfer;
|
---|
178 | instance->unfinished_interrupt_transfer = NULL;
|
---|
179 | usb_log_debug2("RH(%p): Interrupt. Processing batch: %p",
|
---|
180 | instance, batch);
|
---|
181 | if (batch) {
|
---|
182 | const usb_target_t target = batch->ep->target;
|
---|
183 | batch->error = virthub_base_request(&instance->base, target,
|
---|
184 | usb_transfer_batch_direction(batch),
|
---|
185 | (void*)batch->setup_buffer,
|
---|
186 | batch->buffer, batch->buffer_size, &batch->transfered_size);
|
---|
187 | usb_transfer_batch_finish(batch, NULL);
|
---|
188 | usb_transfer_batch_destroy(batch);
|
---|
189 | }
|
---|
190 | return EOK;
|
---|
191 | }
|
---|
192 |
|
---|
193 | /* HUB ROUTINES IMPLEMENTATION */
|
---|
194 | #define TEST_SIZE_INIT(size, port, hub) \
|
---|
195 | do { \
|
---|
196 | hub = virthub_get_data(device); \
|
---|
197 | assert(hub);\
|
---|
198 | if (uint16_usb2host(setup_packet->length) != size) \
|
---|
199 | return ESTALL; \
|
---|
200 | port = uint16_usb2host(setup_packet->index) - 1; \
|
---|
201 | if (port > hub->port_count) \
|
---|
202 | return EINVAL; \
|
---|
203 | } while (0)
|
---|
204 |
|
---|
205 | /** Hub status request handler.
|
---|
206 | * @param device Virtual hub device
|
---|
207 | * @param setup_packet USB setup stage data.
|
---|
208 | * @param[out] data destination data buffer, size must be at least
|
---|
209 | * setup_packet->length bytes
|
---|
210 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
211 | * @return Error code.
|
---|
212 | */
|
---|
213 | static int req_get_status(usbvirt_device_t *device,
|
---|
214 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
215 | uint8_t *data, size_t *act_size)
|
---|
216 | {
|
---|
217 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
218 | assert(hub);
|
---|
219 | if (uint16_usb2host(setup_packet->length) != 4)
|
---|
220 | return ESTALL;
|
---|
221 | /* ECHI RH does not report global OC, and local power is always good */
|
---|
222 | const uint32_t val = 0;
|
---|
223 | memcpy(data, &val, sizeof(val));
|
---|
224 | *act_size = sizeof(val);
|
---|
225 | return EOK;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /** Hub set feature request handler.
|
---|
229 | * @param device Virtual hub device
|
---|
230 | * @param setup_packet USB setup stage data.
|
---|
231 | * @param[out] data destination data buffer, size must be at least
|
---|
232 | * setup_packet->length bytes
|
---|
233 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
234 | * @return Error code.
|
---|
235 | */
|
---|
236 | static int req_clear_hub_feature(usbvirt_device_t *device,
|
---|
237 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
238 | uint8_t *data, size_t *act_size)
|
---|
239 | {
|
---|
240 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
241 | assert(hub);
|
---|
242 |
|
---|
243 | /*
|
---|
244 | * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
|
---|
245 | * C_HUB_OVER_CURRENT are supported.
|
---|
246 | * C_HUB_LOCAL_POWER is not supported because root hubs do not support
|
---|
247 | * local power status feature.
|
---|
248 | * EHCI RH does not report global OC condition either
|
---|
249 | */
|
---|
250 | return ESTALL;
|
---|
251 | }
|
---|
252 |
|
---|
253 | #define BIT_VAL(val, bit) ((val & bit) ? 1 : 0)
|
---|
254 | #define EHCI2USB(val, bit, feat) (BIT_VAL(val, bit) << feat)
|
---|
255 |
|
---|
256 | /** Port status request handler.
|
---|
257 | * @param device Virtual hub device
|
---|
258 | * @param setup_packet USB setup stage data.
|
---|
259 | * @param[out] data destination data buffer, size must be at least
|
---|
260 | * setup_packet->length bytes
|
---|
261 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
262 | * @return Error code.
|
---|
263 | */
|
---|
264 | static int req_get_port_status(usbvirt_device_t *device,
|
---|
265 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
266 | uint8_t *data, size_t *act_size)
|
---|
267 | {
|
---|
268 | ehci_rh_t *hub;
|
---|
269 | unsigned port;
|
---|
270 | TEST_SIZE_INIT(4, port, hub);
|
---|
271 | if (setup_packet->value != 0)
|
---|
272 | return EINVAL;
|
---|
273 |
|
---|
274 | const uint32_t reg = EHCI_RD(hub->registers->portsc[port]);
|
---|
275 | const uint32_t status = uint32_host2usb(
|
---|
276 | EHCI2USB(reg, USB_PORTSC_CONNECT_FLAG, USB_HUB_FEATURE_PORT_CONNECTION) |
|
---|
277 | EHCI2USB(reg, USB_PORTSC_ENABLED_FLAG, USB_HUB_FEATURE_PORT_ENABLE) |
|
---|
278 | EHCI2USB(reg, USB_PORTSC_SUSPEND_FLAG, USB_HUB_FEATURE_PORT_SUSPEND) |
|
---|
279 | EHCI2USB(reg, USB_PORTSC_OC_ACTIVE_FLAG, USB_HUB_FEATURE_PORT_OVER_CURRENT) |
|
---|
280 | EHCI2USB(reg, USB_PORTSC_PORT_RESET_FLAG, USB_HUB_FEATURE_PORT_RESET) |
|
---|
281 | EHCI2USB(reg, USB_PORTSC_PORT_POWER_FLAG, USB_HUB_FEATURE_PORT_POWER) |
|
---|
282 | (((reg & USB_PORTSC_LINE_STATUS_MASK) == USB_PORTSC_LINE_STATUS_K) ?
|
---|
283 | (1 << USB_HUB_FEATURE_PORT_LOW_SPEED) : 0) |
|
---|
284 | ((reg & USB_PORTSC_PORT_OWNER_FLAG) ? 0 : (1 << USB_HUB_FEATURE_PORT_HIGH_SPEED)) |
|
---|
285 | EHCI2USB(reg, USB_PORTSC_PORT_TEST_MASK, 11) |
|
---|
286 | EHCI2USB(reg, USB_PORTSC_INDICATOR_MASK, 12) |
|
---|
287 | EHCI2USB(reg, USB_PORTSC_CONNECT_CH_FLAG, USB_HUB_FEATURE_C_PORT_CONNECTION) |
|
---|
288 | EHCI2USB(reg, USB_PORTSC_EN_CHANGE_FLAG, USB_HUB_FEATURE_C_PORT_ENABLE) |
|
---|
289 | (hub->resume_flag[port] ? (1 << USB_HUB_FEATURE_C_PORT_SUSPEND) : 0) |
|
---|
290 | EHCI2USB(reg, USB_PORTSC_OC_CHANGE_FLAG, USB_HUB_FEATURE_C_PORT_OVER_CURRENT) |
|
---|
291 | (hub->reset_flag[port] ? (1 << USB_HUB_FEATURE_C_PORT_RESET): 0)
|
---|
292 | );
|
---|
293 | /* Note feature numbers for test and indicator feature do not
|
---|
294 | * correspond to the port status bit locations */
|
---|
295 | usb_log_debug2("RH(%p-%u) port status: %"PRIx32"(%"PRIx32")", hub, port,
|
---|
296 | status, reg);
|
---|
297 | memcpy(data, &status, sizeof(status));
|
---|
298 | *act_size = sizeof(status);
|
---|
299 | return EOK;
|
---|
300 | }
|
---|
301 |
|
---|
302 | typedef struct {
|
---|
303 | ehci_rh_t *hub;
|
---|
304 | unsigned port;
|
---|
305 | } ehci_rh_job_t;
|
---|
306 |
|
---|
307 | static int stop_reset(void *arg)
|
---|
308 | {
|
---|
309 | ehci_rh_job_t *job = arg;
|
---|
310 | async_usleep(50000);
|
---|
311 | usb_log_debug("RH(%p-%u): Clearing reset", job->hub, job->port);
|
---|
312 | EHCI_CLR(job->hub->registers->portsc[job->port],
|
---|
313 | USB_PORTSC_PORT_RESET_FLAG);
|
---|
314 | /* wait for reset to complete */
|
---|
315 | while (EHCI_RD(job->hub->registers->portsc[job->port]) &
|
---|
316 | USB_PORTSC_PORT_RESET_FLAG) {
|
---|
317 | async_usleep(1);
|
---|
318 | };
|
---|
319 | usb_log_debug("RH(%p-%u): Reset complete", job->hub, job->port);
|
---|
320 | /* Handle port ownership, if the port is not enabled
|
---|
321 | * after reset it's a full speed device */
|
---|
322 | if (!(EHCI_RD(job->hub->registers->portsc[job->port]) &
|
---|
323 | USB_PORTSC_ENABLED_FLAG)) {
|
---|
324 | usb_log_info("RH(%p-%u): Port not enabled after reset (%"PRIX32
|
---|
325 | "), giving up ownership", job->hub, job->port,
|
---|
326 | EHCI_RD(job->hub->registers->portsc[job->port]));
|
---|
327 | EHCI_SET(job->hub->registers->portsc[job->port],
|
---|
328 | USB_PORTSC_PORT_OWNER_FLAG);
|
---|
329 | }
|
---|
330 | job->hub->reset_flag[job->port] = true;
|
---|
331 | ehci_rh_interrupt(job->hub);
|
---|
332 | free(job);
|
---|
333 | return 0;
|
---|
334 | }
|
---|
335 |
|
---|
336 | static int stop_resume(void *arg)
|
---|
337 | {
|
---|
338 | ehci_rh_job_t *job = arg;
|
---|
339 | async_usleep(20000);
|
---|
340 | usb_log_debug("RH(%p-%u): Stopping resume", job->hub, job->port);
|
---|
341 | EHCI_CLR(job->hub->registers->portsc[job->port],
|
---|
342 | USB_PORTSC_RESUME_FLAG);
|
---|
343 | job->hub->resume_flag[job->port] = true;
|
---|
344 | ehci_rh_interrupt(job->hub);
|
---|
345 | free(job);
|
---|
346 | return 0;
|
---|
347 | }
|
---|
348 |
|
---|
349 | static int delayed_job(int (*func)(void*), ehci_rh_t *rh, unsigned port)
|
---|
350 | {
|
---|
351 | ehci_rh_job_t *job = malloc(sizeof(*job));
|
---|
352 | if (!job)
|
---|
353 | return ENOMEM;
|
---|
354 | job->hub = rh;
|
---|
355 | job->port = port;
|
---|
356 | fid_t fib = fibril_create(func, job);
|
---|
357 | if (!fib) {
|
---|
358 | free(job);
|
---|
359 | return ENOMEM;
|
---|
360 | }
|
---|
361 | fibril_add_ready(fib);
|
---|
362 | usb_log_debug2("RH(%p-%u): Scheduled delayed stop job.", rh, port);
|
---|
363 | return EOK;
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | /** Port clear feature request handler.
|
---|
368 | * @param device Virtual hub device
|
---|
369 | * @param setup_packet USB setup stage data.
|
---|
370 | * @param[out] data destination data buffer, size must be at least
|
---|
371 | * setup_packet->length bytes
|
---|
372 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
373 | * @return Error code.
|
---|
374 | */
|
---|
375 | static int req_clear_port_feature(usbvirt_device_t *device,
|
---|
376 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
377 | uint8_t *data, size_t *act_size)
|
---|
378 | {
|
---|
379 | ehci_rh_t *hub;
|
---|
380 | unsigned port;
|
---|
381 | TEST_SIZE_INIT(0, port, hub);
|
---|
382 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
383 | /* Enabled features to clear: see page 269 of USB specs */
|
---|
384 | switch (feature)
|
---|
385 | {
|
---|
386 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
387 | usb_log_debug2("RH(%p-%u): Clear port power.", hub, port);
|
---|
388 | EHCI_CLR(hub->registers->portsc[port],
|
---|
389 | USB_PORTSC_PORT_POWER_FLAG);
|
---|
390 | return EOK;
|
---|
391 |
|
---|
392 | case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
393 | usb_log_debug2("RH(%p-%u): Clear port enable.", hub, port);
|
---|
394 | EHCI_CLR(hub->registers->portsc[port],
|
---|
395 | USB_PORTSC_ENABLED_FLAG);
|
---|
396 | return EOK;
|
---|
397 |
|
---|
398 | case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
399 | usb_log_debug2("RH(%p-%u): Clear port suspend.", hub, port);
|
---|
400 | /* If not in suspend it's noop */
|
---|
401 | if ((EHCI_RD(hub->registers->portsc[port]) &
|
---|
402 | USB_PORTSC_SUSPEND_FLAG) == 0)
|
---|
403 | return EOK;
|
---|
404 | /* Host driven resume */
|
---|
405 | EHCI_SET(hub->registers->portsc[port],
|
---|
406 | USB_PORTSC_RESUME_FLAG);
|
---|
407 | //TODO: What if creating the delayed job fails?
|
---|
408 | return delayed_job(stop_resume, hub, port);
|
---|
409 |
|
---|
410 | case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
|
---|
411 | usb_log_debug2("RH(%p-%u): Clear port connection change.",
|
---|
412 | hub, port);
|
---|
413 | EHCI_SET(hub->registers->portsc[port],
|
---|
414 | USB_PORTSC_CONNECT_CH_FLAG);
|
---|
415 | return EOK;
|
---|
416 | case USB_HUB_FEATURE_C_PORT_ENABLE: /*17*/
|
---|
417 | usb_log_debug2("RH(%p-%u): Clear port enable change.",
|
---|
418 | hub, port);
|
---|
419 | EHCI_SET(hub->registers->portsc[port],
|
---|
420 | USB_PORTSC_CONNECT_CH_FLAG);
|
---|
421 | return EOK;
|
---|
422 | case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
|
---|
423 | usb_log_debug2("RH(%p-%u): Clear port OC change.",
|
---|
424 | hub, port);
|
---|
425 | EHCI_SET(hub->registers->portsc[port],
|
---|
426 | USB_PORTSC_OC_CHANGE_FLAG);
|
---|
427 | return EOK;
|
---|
428 | case USB_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
|
---|
429 | usb_log_debug2("RH(%p-%u): Clear port suspend change.",
|
---|
430 | hub, port);
|
---|
431 | hub->resume_flag[port] = false;
|
---|
432 | return EOK;
|
---|
433 | case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
|
---|
434 | usb_log_debug2("RH(%p-%u): Clear port reset change.",
|
---|
435 | hub, port);
|
---|
436 | hub->reset_flag[port] = false;
|
---|
437 | return EOK;
|
---|
438 |
|
---|
439 | default:
|
---|
440 | usb_log_warning("RH(%p-%u): Clear unknown feature: %u",
|
---|
441 | hub, port, feature);
|
---|
442 | return ENOTSUP;
|
---|
443 | }
|
---|
444 | }
|
---|
445 |
|
---|
446 | /** Port set feature request handler.
|
---|
447 | * @param device Virtual hub device
|
---|
448 | * @param setup_packet USB setup stage data.
|
---|
449 | * @param[out] data destination data buffer, size must be at least
|
---|
450 | * setup_packet->length bytes
|
---|
451 | * @param[out] act_size Sized of the valid response part of the buffer.
|
---|
452 | * @return Error code.
|
---|
453 | */
|
---|
454 | static int req_set_port_feature(usbvirt_device_t *device,
|
---|
455 | const usb_device_request_setup_packet_t *setup_packet,
|
---|
456 | uint8_t *data, size_t *act_size)
|
---|
457 | {
|
---|
458 | ehci_rh_t *hub;
|
---|
459 | unsigned port;
|
---|
460 | TEST_SIZE_INIT(0, port, hub);
|
---|
461 | const unsigned feature = uint16_usb2host(setup_packet->value);
|
---|
462 | switch (feature) {
|
---|
463 | case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
|
---|
464 | usb_log_debug2("RH(%p-%u): Set port enable.", hub, port);
|
---|
465 | EHCI_SET(hub->registers->portsc[port],
|
---|
466 | USB_PORTSC_ENABLED_FLAG);
|
---|
467 | return EOK;
|
---|
468 | case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
|
---|
469 | usb_log_debug2("RH(%p-%u): Set port suspend.", hub, port);
|
---|
470 | EHCI_SET(hub->registers->portsc[port],
|
---|
471 | USB_PORTSC_SUSPEND_FLAG);
|
---|
472 | return EOK;
|
---|
473 | case USB_HUB_FEATURE_PORT_RESET: /*4*/
|
---|
474 | usb_log_debug2("RH(%p-%u): Set port reset.", hub, port);
|
---|
475 | EHCI_SET(hub->registers->portsc[port],
|
---|
476 | USB_PORTSC_PORT_RESET_FLAG);
|
---|
477 | //TODO: What if creating the delayed job fails?
|
---|
478 | return delayed_job(stop_reset, hub, port);
|
---|
479 | case USB_HUB_FEATURE_PORT_POWER: /*8*/
|
---|
480 | usb_log_debug2("RH(%p-%u): Set port power.", hub, port);
|
---|
481 | EHCI_SET(hub->registers->portsc[port],
|
---|
482 | USB_PORTSC_PORT_POWER_FLAG);
|
---|
483 | return EOK;
|
---|
484 | default:
|
---|
485 | usb_log_warning("RH(%p-%u): Set unknown feature: %u",
|
---|
486 | hub, port, feature);
|
---|
487 | return ENOTSUP;
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | /** Status change handler.
|
---|
492 | * @param device Virtual hub device
|
---|
493 | * @param endpoint Endpoint number
|
---|
494 | * @param tr_type Transfer type
|
---|
495 | * @param buffer Response destination
|
---|
496 | * @param buffer_size Bytes available in buffer
|
---|
497 | * @param actual_size Size us the used part of the dest buffer.
|
---|
498 | *
|
---|
499 | * Produces status mask. Bit 0 indicates hub status change the other bits
|
---|
500 | * represent port status change. Endian does not matter as UHCI root hubs
|
---|
501 | * only need 1 byte.
|
---|
502 | */
|
---|
503 | static int req_status_change_handler(usbvirt_device_t *device,
|
---|
504 | usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
|
---|
505 | void *buffer, size_t buffer_size, size_t *actual_size)
|
---|
506 | {
|
---|
507 | ehci_rh_t *hub = virthub_get_data(device);
|
---|
508 | assert(hub);
|
---|
509 |
|
---|
510 | if (buffer_size < STATUS_BYTES(hub->port_count))
|
---|
511 | return ESTALL;
|
---|
512 |
|
---|
513 | uint16_t mask = 0;
|
---|
514 | for (unsigned port = 0; port < hub->port_count; ++port) {
|
---|
515 | /* Write-clean bits are those that indicate change */
|
---|
516 | uint32_t status = EHCI_RD(hub->registers->portsc[port]);
|
---|
517 | if ((status & USB_PORTSC_WC_MASK) || hub->reset_flag[port]) {
|
---|
518 | /* Ignore new LS device */
|
---|
519 | if ((status & USB_PORTSC_CONNECT_CH_FLAG) &&
|
---|
520 | (status & USB_PORTSC_LINE_STATUS_MASK) ==
|
---|
521 | USB_PORTSC_LINE_STATUS_K)
|
---|
522 | EHCI_SET(hub->registers->portsc[port],
|
---|
523 | USB_PORTSC_PORT_OWNER_FLAG);
|
---|
524 | else
|
---|
525 | mask |= (2 << port);
|
---|
526 | }
|
---|
527 | }
|
---|
528 |
|
---|
529 | usb_log_debug2("RH(%p): root hub interrupt mask: %"PRIx16, hub, mask);
|
---|
530 |
|
---|
531 | if (mask == 0)
|
---|
532 | return ENAK;
|
---|
533 | mask = uint16_host2usb(mask);
|
---|
534 | memcpy(buffer, &mask, STATUS_BYTES(hub->port_count));
|
---|
535 | *actual_size = STATUS_BYTES(hub->port_count);
|
---|
536 | return EOK;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /** EHCI root hub request handlers */
|
---|
540 | static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
|
---|
541 | {
|
---|
542 | STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
543 | .name = "GetDescriptor",
|
---|
544 | .callback = virthub_base_get_hub_descriptor,
|
---|
545 | },
|
---|
546 | {
|
---|
547 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
|
---|
548 | .name = "GetDescriptor",
|
---|
549 | .callback = virthub_base_get_hub_descriptor,
|
---|
550 | },
|
---|
551 | {
|
---|
552 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
|
---|
553 | .name = "GetHubDescriptor",
|
---|
554 | .callback = virthub_base_get_hub_descriptor,
|
---|
555 | },
|
---|
556 | {
|
---|
557 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
|
---|
558 | .name = "GetPortStatus",
|
---|
559 | .callback = req_get_port_status,
|
---|
560 | },
|
---|
561 | {
|
---|
562 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
563 | .name = "ClearHubFeature",
|
---|
564 | .callback = req_clear_hub_feature,
|
---|
565 | },
|
---|
566 | {
|
---|
567 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
|
---|
568 | .name = "ClearPortFeature",
|
---|
569 | .callback = req_clear_port_feature,
|
---|
570 | },
|
---|
571 | {
|
---|
572 | CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
|
---|
573 | .name = "GetHubStatus",
|
---|
574 | .callback = req_get_status,
|
---|
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_SET_FEATURE),
|
---|
583 | .name = "SetHubFeature",
|
---|
584 | .callback = req_nop,
|
---|
585 | },
|
---|
586 | {
|
---|
587 | CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
|
---|
588 | .name = "SetPortFeature",
|
---|
589 | .callback = req_set_port_feature,
|
---|
590 | },
|
---|
591 | {
|
---|
592 | .callback = NULL
|
---|
593 | }
|
---|
594 | };
|
---|
595 |
|
---|
596 | /** Virtual EHCI root hub ops */
|
---|
597 | static usbvirt_device_ops_t ops = {
|
---|
598 | .control = control_transfer_handlers,
|
---|
599 | .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
|
---|
600 | };
|
---|