source: mainline/uspace/drv/bus/usb/ehci/ehci_rh.c@ 95d5dca

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

ehci, rh: Handle status change bits

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