source: mainline/uspace/drv/bus/usb/ehci/ehci_rh.c@ 2be477d5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2be477d5 was 2be477d5, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

ehci: Implement EHCI RH routines.

untested. reste signalling requires running hc anyway.

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