source: mainline/uspace/drv/uhci-rhd/main.c@ bc02b83

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bc02b83 was 4125b7d, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

usb_log_printf() checks for printf correctness

It is surprising how many printf warnings simple check could
produce ;-).

Next time, it won't compile. Bad, huh?

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * Copyright (c) 2011 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 drvusbuhcirh
29 * @{
30 */
31/** @file
32 * @brief UHCI root hub initialization routines
33 */
34#include <ddf/driver.h>
35#include <devman.h>
36#include <device/hw_res.h>
37#include <errno.h>
38#include <str_error.h>
39#include <usb_iface.h>
40#include <usb/ddfiface.h>
41#include <usb/debug.h>
42
43#include "root_hub.h"
44
45#define NAME "uhci-rhd"
46
47static int hc_get_my_registers(ddf_dev_t *dev,
48 uintptr_t *io_reg_address, size_t *io_reg_size);
49/*----------------------------------------------------------------------------*/
50static int uhci_rh_add_device(ddf_dev_t *device);
51/*----------------------------------------------------------------------------*/
52static driver_ops_t uhci_rh_driver_ops = {
53 .add_device = uhci_rh_add_device,
54};
55/*----------------------------------------------------------------------------*/
56static driver_t uhci_rh_driver = {
57 .name = NAME,
58 .driver_ops = &uhci_rh_driver_ops
59};
60/*----------------------------------------------------------------------------*/
61/** Initialize global driver structures (NONE).
62 *
63 * @param[in] argc Nmber of arguments in argv vector (ignored).
64 * @param[in] argv Cmdline argument vector (ignored).
65 * @return Error code.
66 *
67 * Driver debug level is set here.
68 */
69int main(int argc, char *argv[])
70{
71 printf(NAME ": HelenOS UHCI root hub driver.\n");
72 usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
73 return ddf_driver_main(&uhci_rh_driver);
74}
75/*----------------------------------------------------------------------------*/
76/** Initialize a new ddf driver instance of UHCI root hub.
77 *
78 * @param[in] device DDF instance of the device to initialize.
79 * @return Error code.
80 */
81static int uhci_rh_add_device(ddf_dev_t *device)
82{
83 if (!device)
84 return EINVAL;
85
86 usb_log_debug2("uhci_rh_add_device(handle=%" PRIun ")\n",
87 device->handle);
88
89 uintptr_t io_regs = 0;
90 size_t io_size = 0;
91 uhci_root_hub_t *rh = NULL;
92 int ret = EOK;
93
94#define CHECK_RET_FREE_RH_RETURN(ret, message...) \
95if (ret != EOK) { \
96 usb_log_error(message); \
97 if (rh) \
98 free(rh); \
99 return ret; \
100} else (void)0
101
102 ret = hc_get_my_registers(device, &io_regs, &io_size);
103 CHECK_RET_FREE_RH_RETURN(ret,
104 "Failed to get registers from HC: %s.\n", str_error(ret));
105 usb_log_debug("I/O regs at %p (size %zuB).\n",
106 (void *) io_regs, io_size);
107
108 rh = malloc(sizeof(uhci_root_hub_t));
109 ret = (rh == NULL) ? ENOMEM : EOK;
110 CHECK_RET_FREE_RH_RETURN(ret,
111 "Failed to allocate rh driver instance.\n");
112
113 ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device);
114 CHECK_RET_FREE_RH_RETURN(ret,
115 "Failed(%d) to initialize rh driver instance: %s.\n",
116 ret, str_error(ret));
117
118 device->driver_data = rh;
119 usb_log_info("Controlling root hub '%s' (%" PRIun ").\n",
120 device->name, device->handle);
121 return EOK;
122}
123/*----------------------------------------------------------------------------*/
124/** Get address of I/O registers.
125 *
126 * @param[in] dev Device asking for the addresses.
127 * @param[out] io_reg_address Base address of the memory range.
128 * @param[out] io_reg_size Size of the memory range.
129 * @return Error code.
130 */
131int hc_get_my_registers(
132 ddf_dev_t *dev, uintptr_t *io_reg_address, size_t *io_reg_size)
133{
134 assert(dev != NULL);
135
136 int parent_phone = devman_parent_device_connect(dev->handle,
137 IPC_FLAG_BLOCKING);
138 if (parent_phone < 0) {
139 return parent_phone;
140 }
141
142 hw_resource_list_t hw_resources;
143 int ret = hw_res_get_resource_list(parent_phone, &hw_resources);
144 if (ret != EOK) {
145 async_hangup(parent_phone);
146 return ret;
147 }
148
149 uintptr_t io_address = 0;
150 size_t io_size = 0;
151 bool io_found = false;
152
153 size_t i = 0;
154 for (; i < hw_resources.count; i++) {
155 hw_resource_t *res = &hw_resources.resources[i];
156 if (res->type == IO_RANGE) {
157 io_address = res->res.io_range.address;
158 io_size = res->res.io_range.size;
159 io_found = true;
160 }
161 }
162 async_hangup(parent_phone);
163
164 if (!io_found) {
165 return ENOENT;
166 }
167 if (io_reg_address != NULL) {
168 *io_reg_address = io_address;
169 }
170 if (io_reg_size != NULL) {
171 *io_reg_size = io_size;
172 }
173 return EOK;
174}
175/**
176 * @}
177 */
Note: See TracBrowser for help on using the repository browser.