source: mainline/uspace/drv/bus/usb/ohci/ohci_rh.c@ c10daa8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c10daa8 was e6b9182, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

WIP usbhost refactoring: ohci completed

Along with that we noticed hcd_t in bus_t is superfluous and it complicates initialization (and breaks isolation), so we removed it. Also, type of the toggle has changed to bool in the functions.

  • Property mode set to 100644
File size: 17.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 drvusbohci
29 * @{
30 */
31/** @file
32 * @brief OHCI driver
33 */
34
35#include <assert.h>
36#include <errno.h>
37#include <mem.h>
38#include <stddef.h>
39#include <stdint.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 "ohci_rh.h"
51
52enum {
53 HUB_STATUS_CHANGE_PIPE = 1,
54};
55
56static usbvirt_device_ops_t ops;
57
58/** Initialize internal USB HUB class descriptor.
59 * @param instance OHCI root hub.
60 * Use register based info to create accurate descriptor.
61 */
62static void ohci_rh_hub_desc_init(ohci_rh_t *instance)
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 const uint32_t hub_desc = OHCI_RD(instance->registers->rh_desc_a);
69 const uint32_t port_desc = OHCI_RD(instance->registers->rh_desc_b);
70
71 instance->hub_descriptor.header.length = dsize;
72 instance->hub_descriptor.header.descriptor_type = USB_DESCTYPE_HUB;
73 instance->hub_descriptor.header.port_count = instance->port_count;
74 instance->hub_descriptor.header.characteristics = 0 |
75 /* Bits 0,1 indicate power switching mode */
76 ((hub_desc & RHDA_PSM_FLAG) ? 0x01 : 0) |
77 ((hub_desc & RHDA_NPS_FLAG) ? 0x02 : 0) |
78 /* Bit 2 indicates device type (compound device) */
79 ((hub_desc & RHDA_DT_FLAG) ? 0x04 : 0) |
80 /* Bits 3,4 indicate over-current protection mode */
81 ((hub_desc & RHDA_OCPM_FLAG) ? 0x08 : 0) |
82 ((hub_desc & RHDA_NOCP_FLAG) ? 0x10 : 0);
83 instance->hub_descriptor.header.power_good_time =
84 hub_desc >> RHDA_POTPGT_SHIFT;
85 /* bHubContrCurrent, root hubs don't need no power. */
86 instance->hub_descriptor.header.max_current = 0;
87
88 /* Device Removable and some legacy 1.0 stuff*/
89 instance->hub_descriptor.rempow[0] =
90 (port_desc >> RHDB_DR_SHIFT) & 0xff;
91 if (STATUS_BYTES(instance->port_count) == 1) {
92 instance->hub_descriptor.rempow[1] = 0xff;
93 } else {
94 instance->hub_descriptor.rempow[1] =
95 ((port_desc >> RHDB_DR_SHIFT) >> 8) & 0xff;
96 }
97
98 instance->hub_descriptor.rempow[2] = 0xff;
99 instance->hub_descriptor.rempow[3] = 0xff;
100
101}
102/** Initialize OHCI root hub.
103 * @param instance Place to initialize.
104 * @param regs OHCI device registers.
105 * @param name Device name.
106 * return Error code, EOK on success.
107 *
108 * Selects preconfigured port powering mode, sets up descriptor, and
109 * initializes internal virtual hub.
110 */
111int ohci_rh_init(ohci_rh_t *instance, ohci_regs_t *regs, const char *name)
112{
113 assert(instance);
114 instance->registers = regs;
115 instance->port_count = OHCI_RD(regs->rh_desc_a) & RHDA_NDS_MASK;
116 usb_log_debug2("rh_desc_a: %x.\n", OHCI_RD(regs->rh_desc_a));
117 if (instance->port_count > OHCI_MAX_PORTS) {
118 usb_log_warning("OHCI specification does not allow %d ports. "
119 "Max %d ports will be used.\n", instance->port_count,
120 OHCI_MAX_PORTS);
121 instance->port_count = OHCI_MAX_PORTS;
122 }
123 usb_log_info("%s: Found %u ports.\n", name, instance->port_count);
124
125#if defined OHCI_POWER_SWITCH_no
126 usb_log_info("%s: Set power mode to no power switching.\n", name);
127 /* Set port power mode to no power-switching. (always on) */
128 OHCI_SET(regs->rh_desc_a, RHDA_NPS_FLAG);
129
130 /* Set to no over-current reporting */
131 OHCI_SET(regs->rh_desc_a, RHDA_NOCP_FLAG);
132
133#elif defined OHCI_POWER_SWITCH_ganged
134 usb_log_info("%s: Set power mode to ganged power switching.\n", name);
135 /* Set port power mode to ganged power-switching. */
136 OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
137 OHCI_CLR(regs->rh_desc_a, RHDA_PSM_FLAG);
138
139 /* Turn off power (hub driver will turn this back on)*/
140 OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
141
142 /* Set to global over-current */
143 OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
144 OHCI_CLR(regs->rh_desc_a, RHDA_OCPM_FLAG);
145#else
146 usb_log_info("%s: Set power mode to per-port power switching.\n", name);
147 /* Set port power mode to per port power-switching. */
148 OHCI_CLR(regs->rh_desc_a, RHDA_NPS_FLAG);
149 OHCI_SET(regs->rh_desc_a, RHDA_PSM_FLAG);
150
151 /* Control all ports by global switch and turn them off */
152 OHCI_CLR(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
153 OHCI_WR(regs->rh_status, RHS_CLEAR_GLOBAL_POWER);
154
155 /* Return control to per port state */
156 OHCI_SET(regs->rh_desc_b, RHDB_PCC_MASK << RHDB_PCC_SHIFT);
157
158 /* Set per port over-current */
159 OHCI_CLR(regs->rh_desc_a, RHDA_NOCP_FLAG);
160 OHCI_SET(regs->rh_desc_a, RHDA_OCPM_FLAG);
161#endif
162
163 ohci_rh_hub_desc_init(instance);
164 instance->unfinished_interrupt_transfer = NULL;
165 return virthub_base_init(&instance->base, name, &ops, instance,
166 NULL, &instance->hub_descriptor.header, HUB_STATUS_CHANGE_PIPE);
167}
168
169/** Schedule USB request.
170 * @param instance OCHI root hub instance.
171 * @param batch USB requst batch to schedule.
172 * @return Always EOK.
173 * Most requests complete even before this function returns,
174 * status change requests might be postponed until there is something to report.
175 */
176int ohci_rh_schedule(ohci_rh_t *instance, usb_transfer_batch_t *batch)
177{
178 assert(instance);
179 assert(batch);
180 const usb_target_t target = batch->ep->target;
181 batch->error = virthub_base_request(&instance->base, target,
182 usb_transfer_batch_direction(batch), (void*)batch->setup_buffer,
183 batch->buffer, batch->buffer_size, &batch->transfered_size);
184 if (batch->error == ENAK) {
185 /* This is safe because only status change interrupt transfers
186 * return NAK. The assertion holds true because the batch
187 * existence prevents communication with that ep */
188 assert(instance->unfinished_interrupt_transfer == NULL);
189 instance->unfinished_interrupt_transfer = batch;
190 } else {
191 usb_transfer_batch_finish(batch, NULL);
192 usb_transfer_batch_destroy(batch);
193 }
194 return EOK;
195}
196
197/** Handle OHCI RHSC interrupt.
198 * @param instance OHCI root hub isntance.
199 * @return Always EOK.
200 *
201 * Interrupt means there is a change of status to report. It may trigger
202 * processing of a postponed request.
203 */
204int ohci_rh_interrupt(ohci_rh_t *instance)
205{
206 //TODO atomic swap needed
207 usb_transfer_batch_t *batch = instance->unfinished_interrupt_transfer;
208 instance->unfinished_interrupt_transfer = NULL;
209 if (batch) {
210 const usb_target_t target = batch->ep->target;
211 batch->error = virthub_base_request(&instance->base, target,
212 usb_transfer_batch_direction(batch),
213 (void*)batch->setup_buffer,
214 batch->buffer, batch->buffer_size, &batch->transfered_size);
215 usb_transfer_batch_finish(batch, NULL);
216 usb_transfer_batch_destroy(batch);
217 }
218 return EOK;
219}
220
221/* HUB ROUTINES IMPLEMENTATION */
222#define TEST_SIZE_INIT(size, port, hub) \
223do { \
224 hub = virthub_get_data(device); \
225 assert(hub);\
226 if (uint16_usb2host(setup_packet->length) != size) \
227 return ESTALL; \
228 port = uint16_usb2host(setup_packet->index) - 1; \
229 if (port > hub->port_count) \
230 return EINVAL; \
231} while (0)
232
233/** Hub status request handler.
234 * @param device Virtual hub device
235 * @param setup_packet USB setup stage data.
236 * @param[out] data destination data buffer, size must be at least
237 * setup_packet->length bytes
238 * @param[out] act_size Sized of the valid response part of the buffer.
239 * @return Error code.
240 */
241static int req_get_status(usbvirt_device_t *device,
242 const usb_device_request_setup_packet_t *setup_packet,
243 uint8_t *data, size_t *act_size)
244{
245 ohci_rh_t *hub = virthub_get_data(device);
246 assert(hub);
247 if (uint16_usb2host(setup_packet->length) != 4)
248 return ESTALL;
249 const uint32_t val = OHCI_RD(hub->registers->rh_status) &
250 (RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
251 memcpy(data, &val, sizeof(val));
252 *act_size = sizeof(val);
253 return EOK;
254}
255
256/** Hub set feature 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 */
264static int req_clear_hub_feature(usbvirt_device_t *device,
265 const usb_device_request_setup_packet_t *setup_packet,
266 uint8_t *data, size_t *act_size)
267{
268 ohci_rh_t *hub = virthub_get_data(device);
269 assert(hub);
270
271 /*
272 * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
273 * C_HUB_OVER_CURRENT are supported.
274 * C_HUB_LOCAL_POWER is not supported
275 * because root hubs do not support local power status feature.
276 * C_HUB_OVER_CURRENT is represented by OHCI RHS_OCIC_FLAG.
277 * (OHCI pg. 127)
278 */
279 const unsigned feature = uint16_usb2host(setup_packet->value);
280 if (feature == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
281 OHCI_WR(hub->registers->rh_status, RHS_OCIC_FLAG);
282 }
283 return EOK;
284}
285
286/** Port status request handler.
287 * @param device Virtual hub device
288 * @param setup_packet USB setup stage data.
289 * @param[out] data destination data buffer, size must be at least
290 * setup_packet->length bytes
291 * @param[out] act_size Sized of the valid response part of the buffer.
292 * @return Error code.
293 */
294static int req_get_port_status(usbvirt_device_t *device,
295 const usb_device_request_setup_packet_t *setup_packet,
296 uint8_t *data, size_t *act_size)
297{
298 ohci_rh_t *hub;
299 unsigned port;
300 TEST_SIZE_INIT(4, port, hub);
301 if (setup_packet->value != 0)
302 return EINVAL;
303
304 const uint32_t status = OHCI_RD(hub->registers->rh_port_status[port]);
305 memcpy(data, &status, sizeof(status));
306 *act_size = sizeof(status);
307 return EOK;
308}
309
310/** Port clear feature request handler.
311 * @param device Virtual hub device
312 * @param setup_packet USB setup stage data.
313 * @param[out] data destination data buffer, size must be at least
314 * setup_packet->length bytes
315 * @param[out] act_size Sized of the valid response part of the buffer.
316 * @return Error code.
317 */
318static int req_clear_port_feature(usbvirt_device_t *device,
319 const usb_device_request_setup_packet_t *setup_packet,
320 uint8_t *data, size_t *act_size)
321{
322 ohci_rh_t *hub;
323 unsigned port;
324 TEST_SIZE_INIT(0, port, hub);
325 const unsigned feature = uint16_usb2host(setup_packet->value);
326 /* Enabled features to clear: see page 269 of USB specs */
327 switch (feature)
328 {
329 case USB_HUB_FEATURE_PORT_POWER: /*8*/
330 {
331 const uint32_t rhda =
332 OHCI_RD(hub->registers->rh_desc_a);
333 /* No power switching */
334 if (rhda & RHDA_NPS_FLAG)
335 return ENOTSUP;
336 /* Ganged power switching, one port powers all */
337 if (!(rhda & RHDA_PSM_FLAG)) {
338 OHCI_WR(hub->registers->rh_status,
339 RHS_CLEAR_GLOBAL_POWER);
340 return EOK;
341 }
342 OHCI_WR(hub->registers->rh_port_status[port],
343 RHPS_CLEAR_PORT_POWER);
344 return EOK;
345 }
346
347 case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
348 OHCI_WR(hub->registers->rh_port_status[port],
349 RHPS_CLEAR_PORT_ENABLE);
350 return EOK;
351
352 case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
353 OHCI_WR(hub->registers->rh_port_status[port],
354 RHPS_CLEAR_PORT_SUSPEND);
355 return EOK;
356
357 case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
358 case USB_HUB_FEATURE_C_PORT_ENABLE: /*17*/
359 case USB_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
360 case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
361 case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
362 usb_log_debug2("Clearing port C_CONNECTION, C_ENABLE, "
363 "C_SUSPEND, C_OC or C_RESET on port %"PRIu16".\n", port);
364 /* Bit offsets correspond to the feature number */
365 OHCI_WR(hub->registers->rh_port_status[port],
366 1 << feature);
367 return EOK;
368
369 default:
370 return ENOTSUP;
371 }
372}
373
374/** Port set feature request handler.
375 * @param device Virtual hub device
376 * @param setup_packet USB setup stage data.
377 * @param[out] data destination data buffer, size must be at least
378 * setup_packet->length bytes
379 * @param[out] act_size Sized of the valid response part of the buffer.
380 * @return Error code.
381 */
382static int req_set_port_feature(usbvirt_device_t *device,
383 const usb_device_request_setup_packet_t *setup_packet,
384 uint8_t *data, size_t *act_size)
385{
386 ohci_rh_t *hub;
387 unsigned port;
388 TEST_SIZE_INIT(0, port, hub);
389 const unsigned feature = uint16_usb2host(setup_packet->value);
390
391 switch (feature) {
392 case USB_HUB_FEATURE_PORT_POWER: /*8*/
393 {
394 const uint32_t rhda = OHCI_RD(hub->registers->rh_desc_a);
395
396 /* No power switching */
397 if (rhda & RHDA_NPS_FLAG)
398 return EOK;
399
400 /* Ganged power switching, one port powers all */
401 if (!(rhda & RHDA_PSM_FLAG)) {
402 OHCI_WR(hub->registers->rh_status,RHS_SET_GLOBAL_POWER);
403 return EOK;
404 }
405 }
406 /* Fall through, for per port power */
407 /* Fallthrough */
408 case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
409 case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
410 case USB_HUB_FEATURE_PORT_RESET: /*4*/
411 usb_log_debug2("Setting port POWER, ENABLE, SUSPEND or RESET "
412 "on port %"PRIu16".\n", port);
413 /* Bit offsets correspond to the feature number */
414 OHCI_WR(hub->registers->rh_port_status[port], 1 << feature);
415 return EOK;
416 default:
417 return ENOTSUP;
418 }
419}
420
421/** Status change handler.
422 * @param device Virtual hub device
423 * @param endpoint Endpoint number
424 * @param tr_type Transfer type
425 * @param buffer Response destination
426 * @param buffer_size Bytes available in buffer
427 * @param actual_size Size us the used part of the dest buffer.
428 *
429 * Produces status mask. Bit 0 indicates hub status change the other bits
430 * represent port status change. Endian does not matter as UHCI root hubs
431 * only need 1 byte.
432 */
433static int req_status_change_handler(usbvirt_device_t *device,
434 usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
435 void *buffer, size_t buffer_size, size_t *actual_size)
436{
437 ohci_rh_t *hub = virthub_get_data(device);
438 assert(hub);
439
440 if (buffer_size < STATUS_BYTES(hub->port_count))
441 return ESTALL;
442
443 uint16_t mask = 0;
444
445 /* Only local power source change and over-current change can happen */
446 if (OHCI_RD(hub->registers->rh_status) & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
447 mask |= 1;
448 }
449
450 for (unsigned port = 1; port <= hub->port_count; ++port) {
451 /* Write-clean bits are those that indicate change */
452 if (OHCI_RD(hub->registers->rh_port_status[port - 1])
453 & RHPS_CHANGE_WC_MASK) {
454 mask |= (1 << port);
455 }
456 }
457
458 usb_log_debug2("OHCI root hub interrupt mask: %hx.\n", mask);
459
460 if (mask == 0)
461 return ENAK;
462 mask = uint16_host2usb(mask);
463 memcpy(buffer, &mask, STATUS_BYTES(hub->port_count));
464 *actual_size = STATUS_BYTES(hub->port_count);
465 return EOK;
466}
467
468/** OHCI root hub request handlers */
469static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
470 {
471 STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
472 .name = "GetDescriptor",
473 .callback = virthub_base_get_hub_descriptor,
474 },
475 {
476 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
477 .name = "GetDescriptor",
478 .callback = virthub_base_get_hub_descriptor,
479 },
480 {
481 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
482 .name = "GetHubDescriptor",
483 .callback = virthub_base_get_hub_descriptor,
484 },
485 {
486 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
487 .name = "GetPortStatus",
488 .callback = req_get_port_status,
489 },
490 {
491 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
492 .name = "ClearHubFeature",
493 .callback = req_clear_hub_feature,
494 },
495 {
496 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
497 .name = "ClearPortFeature",
498 .callback = req_clear_port_feature,
499 },
500 {
501 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
502 .name = "GetHubStatus",
503 .callback = req_get_status,
504 },
505 {
506 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
507 .name = "GetPortStatus",
508 .callback = req_get_port_status,
509 },
510 {
511 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
512 .name = "SetHubFeature",
513 .callback = req_nop,
514 },
515 {
516 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
517 .name = "SetPortFeature",
518 .callback = req_set_port_feature,
519 },
520 {
521 .callback = NULL
522 }
523};
524
525/** Virtual OHCI root hub ops */
526static usbvirt_device_ops_t ops = {
527 .control = control_transfer_handlers,
528 .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
529};
Note: See TracBrowser for help on using the repository browser.