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

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

usbhost refactoring: let transfer_batch be initialized by bus

Currently makes older HCs fail, work in progress.

  • Property mode set to 100644
File size: 17.0 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 batch->dir, &batch->setup.packet,
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);
192 }
193 return EOK;
194}
195
196/** Handle OHCI RHSC interrupt.
197 * @param instance OHCI root hub isntance.
198 * @return Always EOK.
199 *
200 * Interrupt means there is a change of status to report. It may trigger
201 * processing of a postponed request.
202 */
203int ohci_rh_interrupt(ohci_rh_t *instance)
204{
205 //TODO atomic swap needed
206 usb_transfer_batch_t *batch = instance->unfinished_interrupt_transfer;
207 instance->unfinished_interrupt_transfer = NULL;
208 if (batch) {
209 const usb_target_t target = batch->ep->target;
210 batch->error = virthub_base_request(&instance->base, target,
211 batch->dir, &batch->setup.packet,
212 batch->buffer, batch->buffer_size, &batch->transfered_size);
213 usb_transfer_batch_finish(batch);
214 }
215 return EOK;
216}
217
218/* HUB ROUTINES IMPLEMENTATION */
219#define TEST_SIZE_INIT(size, port, hub) \
220do { \
221 hub = virthub_get_data(device); \
222 assert(hub);\
223 if (uint16_usb2host(setup_packet->length) != size) \
224 return ESTALL; \
225 port = uint16_usb2host(setup_packet->index) - 1; \
226 if (port > hub->port_count) \
227 return EINVAL; \
228} while (0)
229
230/** Hub status request handler.
231 * @param device Virtual hub device
232 * @param setup_packet USB setup stage data.
233 * @param[out] data destination data buffer, size must be at least
234 * setup_packet->length bytes
235 * @param[out] act_size Sized of the valid response part of the buffer.
236 * @return Error code.
237 */
238static int req_get_status(usbvirt_device_t *device,
239 const usb_device_request_setup_packet_t *setup_packet,
240 uint8_t *data, size_t *act_size)
241{
242 ohci_rh_t *hub = virthub_get_data(device);
243 assert(hub);
244 if (uint16_usb2host(setup_packet->length) != 4)
245 return ESTALL;
246 const uint32_t val = OHCI_RD(hub->registers->rh_status) &
247 (RHS_LPS_FLAG | RHS_LPSC_FLAG | RHS_OCI_FLAG | RHS_OCIC_FLAG);
248 memcpy(data, &val, sizeof(val));
249 *act_size = sizeof(val);
250 return EOK;
251}
252
253/** Hub set feature request handler.
254 * @param device Virtual hub device
255 * @param setup_packet USB setup stage data.
256 * @param[out] data destination data buffer, size must be at least
257 * setup_packet->length bytes
258 * @param[out] act_size Sized of the valid response part of the buffer.
259 * @return Error code.
260 */
261static int req_clear_hub_feature(usbvirt_device_t *device,
262 const usb_device_request_setup_packet_t *setup_packet,
263 uint8_t *data, size_t *act_size)
264{
265 ohci_rh_t *hub = virthub_get_data(device);
266 assert(hub);
267
268 /*
269 * Chapter 11.16.2 specifies that only C_HUB_LOCAL_POWER and
270 * C_HUB_OVER_CURRENT are supported.
271 * C_HUB_LOCAL_POWER is not supported
272 * because root hubs do not support local power status feature.
273 * C_HUB_OVER_CURRENT is represented by OHCI RHS_OCIC_FLAG.
274 * (OHCI pg. 127)
275 */
276 const unsigned feature = uint16_usb2host(setup_packet->value);
277 if (feature == USB_HUB_FEATURE_C_HUB_OVER_CURRENT) {
278 OHCI_WR(hub->registers->rh_status, RHS_OCIC_FLAG);
279 }
280 return EOK;
281}
282
283/** Port status request handler.
284 * @param device Virtual hub device
285 * @param setup_packet USB setup stage data.
286 * @param[out] data destination data buffer, size must be at least
287 * setup_packet->length bytes
288 * @param[out] act_size Sized of the valid response part of the buffer.
289 * @return Error code.
290 */
291static int req_get_port_status(usbvirt_device_t *device,
292 const usb_device_request_setup_packet_t *setup_packet,
293 uint8_t *data, size_t *act_size)
294{
295 ohci_rh_t *hub;
296 unsigned port;
297 TEST_SIZE_INIT(4, port, hub);
298 if (setup_packet->value != 0)
299 return EINVAL;
300
301 const uint32_t status = OHCI_RD(hub->registers->rh_port_status[port]);
302 memcpy(data, &status, sizeof(status));
303 *act_size = sizeof(status);
304 return EOK;
305}
306
307/** Port clear feature request handler.
308 * @param device Virtual hub device
309 * @param setup_packet USB setup stage data.
310 * @param[out] data destination data buffer, size must be at least
311 * setup_packet->length bytes
312 * @param[out] act_size Sized of the valid response part of the buffer.
313 * @return Error code.
314 */
315static int req_clear_port_feature(usbvirt_device_t *device,
316 const usb_device_request_setup_packet_t *setup_packet,
317 uint8_t *data, size_t *act_size)
318{
319 ohci_rh_t *hub;
320 unsigned port;
321 TEST_SIZE_INIT(0, port, hub);
322 const unsigned feature = uint16_usb2host(setup_packet->value);
323 /* Enabled features to clear: see page 269 of USB specs */
324 switch (feature)
325 {
326 case USB_HUB_FEATURE_PORT_POWER: /*8*/
327 {
328 const uint32_t rhda =
329 OHCI_RD(hub->registers->rh_desc_a);
330 /* No power switching */
331 if (rhda & RHDA_NPS_FLAG)
332 return ENOTSUP;
333 /* Ganged power switching, one port powers all */
334 if (!(rhda & RHDA_PSM_FLAG)) {
335 OHCI_WR(hub->registers->rh_status,
336 RHS_CLEAR_GLOBAL_POWER);
337 return EOK;
338 }
339 OHCI_WR(hub->registers->rh_port_status[port],
340 RHPS_CLEAR_PORT_POWER);
341 return EOK;
342 }
343
344 case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
345 OHCI_WR(hub->registers->rh_port_status[port],
346 RHPS_CLEAR_PORT_ENABLE);
347 return EOK;
348
349 case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
350 OHCI_WR(hub->registers->rh_port_status[port],
351 RHPS_CLEAR_PORT_SUSPEND);
352 return EOK;
353
354 case USB_HUB_FEATURE_C_PORT_CONNECTION: /*16*/
355 case USB_HUB_FEATURE_C_PORT_ENABLE: /*17*/
356 case USB_HUB_FEATURE_C_PORT_SUSPEND: /*18*/
357 case USB_HUB_FEATURE_C_PORT_OVER_CURRENT: /*19*/
358 case USB_HUB_FEATURE_C_PORT_RESET: /*20*/
359 usb_log_debug2("Clearing port C_CONNECTION, C_ENABLE, "
360 "C_SUSPEND, C_OC or C_RESET on port %u.\n", port);
361 /* Bit offsets correspond to the feature number */
362 OHCI_WR(hub->registers->rh_port_status[port],
363 1 << feature);
364 return EOK;
365
366 default:
367 return ENOTSUP;
368 }
369}
370
371/** Port set feature request handler.
372 * @param device Virtual hub device
373 * @param setup_packet USB setup stage data.
374 * @param[out] data destination data buffer, size must be at least
375 * setup_packet->length bytes
376 * @param[out] act_size Sized of the valid response part of the buffer.
377 * @return Error code.
378 */
379static int req_set_port_feature(usbvirt_device_t *device,
380 const usb_device_request_setup_packet_t *setup_packet,
381 uint8_t *data, size_t *act_size)
382{
383 ohci_rh_t *hub;
384 unsigned port;
385 TEST_SIZE_INIT(0, port, hub);
386 const unsigned feature = uint16_usb2host(setup_packet->value);
387
388 switch (feature) {
389 case USB_HUB_FEATURE_PORT_POWER: /*8*/
390 {
391 const uint32_t rhda = OHCI_RD(hub->registers->rh_desc_a);
392
393 /* No power switching */
394 if (rhda & RHDA_NPS_FLAG)
395 return EOK;
396
397 /* Ganged power switching, one port powers all */
398 if (!(rhda & RHDA_PSM_FLAG)) {
399 OHCI_WR(hub->registers->rh_status,RHS_SET_GLOBAL_POWER);
400 return EOK;
401 }
402 }
403 /* Fall through, for per port power */
404 /* Fallthrough */
405 case USB_HUB_FEATURE_PORT_ENABLE: /*1*/
406 case USB_HUB_FEATURE_PORT_SUSPEND: /*2*/
407 case USB_HUB_FEATURE_PORT_RESET: /*4*/
408 usb_log_debug2("Setting port POWER, ENABLE, SUSPEND or RESET "
409 "on port %u.\n", port);
410 /* Bit offsets correspond to the feature number */
411 OHCI_WR(hub->registers->rh_port_status[port], 1 << feature);
412 return EOK;
413 default:
414 return ENOTSUP;
415 }
416}
417
418/** Status change handler.
419 * @param device Virtual hub device
420 * @param endpoint Endpoint number
421 * @param tr_type Transfer type
422 * @param buffer Response destination
423 * @param buffer_size Bytes available in buffer
424 * @param actual_size Size us the used part of the dest buffer.
425 *
426 * Produces status mask. Bit 0 indicates hub status change the other bits
427 * represent port status change. Endian does not matter as UHCI root hubs
428 * only need 1 byte.
429 */
430static int req_status_change_handler(usbvirt_device_t *device,
431 usb_endpoint_t endpoint, usb_transfer_type_t tr_type,
432 void *buffer, size_t buffer_size, size_t *actual_size)
433{
434 ohci_rh_t *hub = virthub_get_data(device);
435 assert(hub);
436
437 if (buffer_size < STATUS_BYTES(hub->port_count))
438 return ESTALL;
439
440 uint16_t mask = 0;
441
442 /* Only local power source change and over-current change can happen */
443 if (OHCI_RD(hub->registers->rh_status) & (RHS_LPSC_FLAG | RHS_OCIC_FLAG)) {
444 mask |= 1;
445 }
446
447 for (unsigned port = 1; port <= hub->port_count; ++port) {
448 /* Write-clean bits are those that indicate change */
449 if (OHCI_RD(hub->registers->rh_port_status[port - 1])
450 & RHPS_CHANGE_WC_MASK) {
451 mask |= (1 << port);
452 }
453 }
454
455 usb_log_debug2("OHCI root hub interrupt mask: %hx.\n", mask);
456
457 if (mask == 0)
458 return ENAK;
459 mask = uint16_host2usb(mask);
460 memcpy(buffer, &mask, STATUS_BYTES(hub->port_count));
461 *actual_size = STATUS_BYTES(hub->port_count);
462 return EOK;
463}
464
465/** OHCI root hub request handlers */
466static const usbvirt_control_request_handler_t control_transfer_handlers[] = {
467 {
468 STD_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
469 .name = "GetDescriptor",
470 .callback = virthub_base_get_hub_descriptor,
471 },
472 {
473 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_DEVREQ_GET_DESCRIPTOR),
474 .name = "GetDescriptor",
475 .callback = virthub_base_get_hub_descriptor,
476 },
477 {
478 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_DESCRIPTOR),
479 .name = "GetHubDescriptor",
480 .callback = virthub_base_get_hub_descriptor,
481 },
482 {
483 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
484 .name = "GetPortStatus",
485 .callback = req_get_port_status,
486 },
487 {
488 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_CLEAR_FEATURE),
489 .name = "ClearHubFeature",
490 .callback = req_clear_hub_feature,
491 },
492 {
493 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_CLEAR_FEATURE),
494 .name = "ClearPortFeature",
495 .callback = req_clear_port_feature,
496 },
497 {
498 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_GET_STATUS),
499 .name = "GetHubStatus",
500 .callback = req_get_status,
501 },
502 {
503 CLASS_REQ_IN(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_GET_STATUS),
504 .name = "GetPortStatus",
505 .callback = req_get_port_status,
506 },
507 {
508 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_DEVICE, USB_HUB_REQUEST_SET_FEATURE),
509 .name = "SetHubFeature",
510 .callback = req_nop,
511 },
512 {
513 CLASS_REQ_OUT(USB_REQUEST_RECIPIENT_OTHER, USB_HUB_REQUEST_SET_FEATURE),
514 .name = "SetPortFeature",
515 .callback = req_set_port_feature,
516 },
517 {
518 .callback = NULL
519 }
520};
521
522/** Virtual OHCI root hub ops */
523static usbvirt_device_ops_t ops = {
524 .control = control_transfer_handlers,
525 .data_in[HUB_STATUS_CHANGE_PIPE] = req_status_change_handler,
526};
Note: See TracBrowser for help on using the repository browser.