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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8d517c3 was f9c03b5, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

Refactoring and dead code removal

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky, 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("%s called device %d\n", __FUNCTION__, device->handle);
87
88 uintptr_t io_regs = 0;
89 size_t io_size = 0;
90 uhci_root_hub_t *rh = NULL;
91 int ret = EOK;
92
93#define CHECK_RET_FREE_RH_RETURN(ret, message...) \
94if (ret != EOK) { \
95 usb_log_error(message); \
96 if (rh) \
97 free(rh); \
98 return ret; \
99} else (void)0
100
101 ret = hc_get_my_registers(device, &io_regs, &io_size);
102 CHECK_RET_FREE_RH_RETURN(ret,
103 "Failed(%d) to get registers from HC: %s.\n", ret, str_error(ret));
104 usb_log_debug("I/O regs at %#x (size %zu).\n", io_regs, io_size);
105
106 rh = malloc(sizeof(uhci_root_hub_t));
107 ret = (rh == NULL) ? ENOMEM : EOK;
108 CHECK_RET_FREE_RH_RETURN(ret,
109 "Failed to allocate rh driver instance.\n");
110
111 ret = uhci_root_hub_init(rh, (void*)io_regs, io_size, device);
112 CHECK_RET_FREE_RH_RETURN(ret,
113 "Failed(%d) to initialize rh driver instance: %s.\n",
114 ret, str_error(ret));
115
116 device->driver_data = rh;
117 usb_log_info("Controlling root hub '%s' (%llu).\n",
118 device->name, device->handle);
119 return EOK;
120}
121/*----------------------------------------------------------------------------*/
122/** Get address of I/O registers.
123 *
124 * @param[in] dev Device asking for the addresses.
125 * @param[out] io_reg_address Base address of the memory range.
126 * @param[out] io_reg_size Size of the memory range.
127 * @return Error code.
128 */
129int hc_get_my_registers(
130 ddf_dev_t *dev, uintptr_t *io_reg_address, size_t *io_reg_size)
131{
132 assert(dev != NULL);
133
134 int parent_phone = devman_parent_device_connect(dev->handle,
135 IPC_FLAG_BLOCKING);
136 if (parent_phone < 0) {
137 return parent_phone;
138 }
139
140 hw_resource_list_t hw_resources;
141 int ret = hw_res_get_resource_list(parent_phone, &hw_resources);
142 if (ret != EOK) {
143 async_hangup(parent_phone);
144 return ret;
145 }
146
147 uintptr_t io_address = 0;
148 size_t io_size = 0;
149 bool io_found = false;
150
151 size_t i = 0;
152 for (; i < hw_resources.count; i++) {
153 hw_resource_t *res = &hw_resources.resources[i];
154 if (res->type == IO_RANGE) {
155 io_address = res->res.io_range.address;
156 io_size = res->res.io_range.size;
157 io_found = true;
158 }
159 }
160 async_hangup(parent_phone);
161
162 if (!io_found) {
163 return ENOENT;
164 }
165 if (io_reg_address != NULL) {
166 *io_reg_address = io_address;
167 }
168 if (io_reg_size != NULL) {
169 *io_reg_size = io_size;
170 }
171 return EOK;
172}
173/**
174 * @}
175 */
Note: See TracBrowser for help on using the repository browser.