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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b415cc was 8d2dd7f2, checked in by Jakub Jermar <jakub@…>, 8 years ago

Reduce the number of files that include <sys/types.h>

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