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