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

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

usb: unified logging

Use logger instead of printf. Logger adds newlines automatically.

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