source: mainline/uspace/drv/bus/usb/ehci/ehci_rh.c@ 3e200736

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

ehci: Add skeleton driver.

Mostly copied from ohci. Reports root hub correctly, does nothing else.

  • Property mode set to 100644
File size: 15.2 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 return virthub_base_init(&instance->base, name, &ops, instance,
119 NULL, &instance->hub_descriptor.header, HUB_STATUS_CHANGE_PIPE);
120}
121
122/** Schedule USB request.
123 * @param instance OCHI root hub instance.
124 * @param batch USB requst batch to schedule.
125 * @return Always EOK.
126 * Most requests complete even before this function returns,
127 * status change requests might be postponed until there is something to report.
128 */
129int ehci_rh_schedule(ehci_rh_t *instance, usb_transfer_batch_t *batch)
130{
131 assert(instance);
132 assert(batch);
133 const usb_target_t target = {{
134 .address = batch->ep->address,
135 .endpoint = batch->ep->endpoint,
136 }};
137 batch->error = virthub_base_request(&instance->base, target,
138 usb_transfer_batch_direction(batch), (void*)batch->setup_buffer,
139 batch->buffer, batch->buffer_size, &batch->transfered_size);
140 if (batch->error == ENAK) {
141 /* This is safe because only status change interrupt transfers
142 * return NAK. The assertion holds true because the batch
143 * existence prevents communication with that ep */
144 assert(instance->unfinished_interrupt_transfer == NULL);
145 instance->unfinished_interrupt_transfer = batch;
146 } else {
147 usb_transfer_batch_finish(batch, NULL);
148 usb_transfer_batch_destroy(batch);
149 }
150 return EOK;
151}
152
153/** Handle EHCI RHSC interrupt.
154 * @param instance EHCI root hub isntance.
155 * @return Always EOK.
156 *
157 * Interrupt means there is a change of status to report. It may trigger
158 * processing of a postponed request.
159 */
160int ehci_rh_interrupt(ehci_rh_t *instance)
161{
162 //TODO atomic swap needed
163 usb_transfer_batch_t *batch = instance->unfinished_interrupt_transfer;
164 instance->unfinished_interrupt_transfer = NULL;
165 if (batch) {
166 const usb_target_t target = {{
167 .address = batch->ep->address,
168 .endpoint = batch->ep->endpoint,
169 }};
170 batch->error = virthub_base_request(&instance->base, target,
171 usb_transfer_batch_direction(batch),
172 (void*)batch->setup_buffer,
173 batch->buffer, batch->buffer_size, &batch->transfered_size);
174 usb_transfer_batch_finish(batch, NULL);
175 usb_transfer_batch_destroy(batch);
176 }
177 return EOK;
178}
179
180/* HUB ROUTINES IMPLEMENTATION */
181#define TEST_SIZE_INIT(size, port, hub) \
182do { \
183 hub = virthub_get_data(device); \
184 assert(hub);\
185 if (uint16_usb2host(setup_packet->length) != size) \
186 return ESTALL; \
187 port = uint16_usb2host(setup_packet->index) - 1; \
188 if (port > hub->port_count) \
189 return EINVAL; \
190} while (0)
191
192/** Hub status request handler.
193 * @param device Virtual hub device
194 * @param setup_packet USB setup stage data.
195 * @param[out] data destination data buffer, size must be at least
196 * setup_packet->length bytes
197 * @param[out] act_size Sized of the valid response part of the buffer.
198 * @return Error code.
199 */
200static int req_get_status(usbvirt_device_t *device,
201 const usb_device_request_setup_packet_t *setup_packet,
202 uint8_t *data, size_t *act_size)
203{
204 ehci_rh_t *hub = virthub_get_data(device);
205 assert(hub);
206 if (uint16_usb2host(setup_packet->length) != 4)
207 return ESTALL;
208 const uint32_t val = 0;
209 //TODO: implement
210 memcpy(data, &val, sizeof(val));
211 *act_size = sizeof(val);
212 return EOK;
213}
214
215/** Hub set feature request handler.
216 * @param device Virtual hub device
217 * @param setup_packet USB setup stage data.
218 * @param[out] data destination data buffer, size must be at least
219 * setup_packet->length bytes
220 * @param[out] act_size Sized of the valid response part of the buffer.
221 * @return Error code.
222 */
223static int req_clear_hub_feature(usbvirt_device_t *device,
224 const usb_device_request_setup_packet_t *setup_packet,
225 uint8_t *data, size_t *act_size)
226{
227 ehci_rh_t *hub = virthub_get_data(device);
228 assert(hub);
229
230 /*
231 * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
232 * C_HUB_OVER_CURRENT are supported.
233 * C_HUB_LOCAL_POWER is not supported
234 * because root hubs do not support local power status feature.
235 * C_HUB_OVER_CURRENT is represented by EHCI RHS_OCIC_FLAG.
236 * (EHCI pg. 127)
237 */
238 const unsigned feature = uint16_usb2host(setup_packet->value);
239 if (feature == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
240 //TODO: Implement
241// EHCI_WR(hub->registers->rh_status, RHS_OCIC_FLAG);
242 }
243 return EOK;
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 status = 0;
265 // TODO: Implement
266 //EHCI_RD(hub->registers->rh_port_status[port]);
267 memcpy(data, &status, sizeof(status));
268 *act_size = sizeof(status);
269 return EOK;
270}
271
272/** Port clear feature request handler.
273 * @param device Virtual hub device
274 * @param setup_packet USB setup stage data.
275 * @param[out] data destination data buffer, size must be at least
276 * setup_packet->length bytes
277 * @param[out] act_size Sized of the valid response part of the buffer.
278 * @return Error code.
279 */
280static int req_clear_port_feature(usbvirt_device_t *device,
281 const usb_device_request_setup_packet_t *setup_packet,
282 uint8_t *data, size_t *act_size)
283{
284 ehci_rh_t *hub;
285 unsigned port;
286 TEST_SIZE_INIT(0, port, hub);
287 const unsigned feature = uint16_usb2host(setup_packet->value);
288 /* Enabled features to clear: see page 269 of USB specs */
289 switch (feature)
290 {
291 case USB_HUB_FEATURE_PORT_POWER: /*8*/
292 {
293// EHCI_WR(hub->registers->rh_port_status[port],
294// RHPS_CLEAR_PORT_POWER);
295// TODO: Implement
296 return EOK;
297 }
298
299 case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
300// EHCI_WR(hub->registers->rh_port_status[port],
301// RHPS_CLEAR_PORT_ENABLE);
302 return EOK;
303
304 case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
305// EHCI_WR(hub->registers->rh_port_status[port],
306// RHPS_CLEAR_PORT_SUSPEND);
307 return EOK;
308
309 case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
310 case USB_HUB_FEATURE_C_PORT_ENABLE: /*17*/
311 case USB_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
312 case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
313 case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
314 usb_log_debug2("Clearing port C_CONNECTION, C_ENABLE, "
315 "C_SUSPEND, C_OC or C_RESET on port %"PRIu16".\n", port);
316 /* Bit offsets correspond to the feature number */
317// EHCI_WR(hub->registers->rh_port_status[port],
318// 1 << feature);
319 return EOK;
320
321 default:
322 return ENOTSUP;
323 }
324}
325
326/** Port set feature request handler.
327 * @param device Virtual hub device
328 * @param setup_packet USB setup stage data.
329 * @param[out] data destination data buffer, size must be at least
330 * setup_packet->length bytes
331 * @param[out] act_size Sized of the valid response part of the buffer.
332 * @return Error code.
333 */
334static int req_set_port_feature(usbvirt_device_t *device,
335 const usb_device_request_setup_packet_t *setup_packet,
336 uint8_t *data, size_t *act_size)
337{
338 ehci_rh_t *hub;
339 unsigned port;
340 TEST_SIZE_INIT(0, port, hub);
341 const unsigned feature = uint16_usb2host(setup_packet->value);
342 switch (feature) {
343 case USB_HUB_FEATURE_PORT_POWER: /*8*/
344 {
345 /* No power switching */
346// if (rhda & RHDA_NPS_FLAG)
347 return EOK;
348 /* Ganged power switching, one port powers all */
349// if (!(rhda & RHDA_PSM_FLAG)) {
350// EHCI_WR(hub->registers->rh_status,RHS_SET_GLOBAL_POWER);
351// return EOK;
352// }
353 }
354 /* Fall through, for per port power */
355 case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
356 case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
357 case USB_HUB_FEATURE_PORT_RESET: /*4*/
358 usb_log_debug2("Setting port POWER, ENABLE, SUSPEND or RESET "
359 "on port %"PRIu16".\n", port);
360 /* Bit offsets correspond to the feature number */
361// EHCI_WR(hub->registers->rh_port_status[port], 1 << feature);
362 return EOK;
363 default:
364 return ENOTSUP;
365 }
366}
367
368/** Status change handler.
369 * @param device Virtual hub device
370 * @param endpoint Endpoint number
371 * @param tr_type Transfer type
372 * @param buffer Response destination
373 * @param buffer_size Bytes available in buffer
374 * @param actual_size Size us the used part of the dest buffer.
375 *
376 * Produces status mask. Bit 0 indicates hub status change the other bits
377 * represent port status change. Endian does not matter as UHCI root hubs
378 * only need 1 byte.
379 */
380static int req_status_change_handler(usbvirt_device_t *device,
381 usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
382 void *buffer, size_t buffer_size, size_t *actual_size)
383{
384 ehci_rh_t *hub = virthub_get_data(device);
385 assert(hub);
386
387 if (buffer_size < STATUS_BYTES(hub->port_count))
388 return ESTALL;
389
390 uint16_t mask = 0;
391
392 /* Only local power source change and over-current change can happen */
393// if (EHCI_RD(hub->registers->rh_status) & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
394// mask |= 1;
395// }
396
397 for (unsigned port = 1; port <= hub->port_count; ++port) {
398 /* Write-clean bits are those that indicate change */
399// if (EHCI_RD(hub->registers->rh_port_status[port - 1])
400// & RHPS_CHANGE_WC_MASK) {
401// mask |= (1 << port);
402// }
403 }
404
405 usb_log_debug2("EHCI root hub interrupt mask: %hx.\n", mask);
406
407 if (mask == 0)
408 return ENAK;
409 mask = uint16_host2usb(mask);
410 memcpy(buffer, &mask, STATUS_BYTES(hub->port_count));
411 *actual_size = STATUS_BYTES(hub->port_count);
412 return EOK;
413}
414
415/** EHCI root hub request handlers */
416static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
417 {
418 STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
419 .name = "GetDescriptor",
420 .callback = virthub_base_get_hub_descriptor,
421 },
422 {
423 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
424 .name = "GetDescriptor",
425 .callback = virthub_base_get_hub_descriptor,
426 },
427 {
428 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
429 .name = "GetHubDescriptor",
430 .callback = virthub_base_get_hub_descriptor,
431 },
432 {
433 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
434 .name = "GetPortStatus",
435 .callback = req_get_port_status,
436 },
437 {
438 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
439 .name = "ClearHubFeature",
440 .callback = req_clear_hub_feature,
441 },
442 {
443 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
444 .name = "ClearPortFeature",
445 .callback = req_clear_port_feature,
446 },
447 {
448 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
449 .name = "GetHubStatus",
450 .callback = req_get_status,
451 },
452 {
453 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
454 .name = "GetPortStatus",
455 .callback = req_get_port_status,
456 },
457 {
458 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
459 .name = "SetHubFeature",
460 .callback = req_nop,
461 },
462 {
463 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
464 .name = "SetPortFeature",
465 .callback = req_set_port_feature,
466 },
467 {
468 .callback = NULL
469 }
470};
471
472/** Virtual EHCI root hub ops */
473static usbvirt_device_ops_t ops = {
474 .control = control_transfer_handlers,
475 .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
476};
Note: See TracBrowser for help on using the repository browser.