source: mainline/uspace/drv/char/i8042/main.c@ e882e3a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e882e3a was 2df6f6fe, checked in by Martin Decky <martin@…>, 14 years ago

cstyle and cleanup
(no change in functionality)

  • Property mode set to 100644
File size: 4.5 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
29/** @addtogroup drvi8042
30 * @{
31 */
32
33/** @file
34 * @brief i8042 driver DDF bits.
35 */
36
37#include <libarch/inttypes.h>
38#include <ddf/driver.h>
39#include <devman.h>
40#include <device/hw_res_parsed.h>
41#include <errno.h>
42#include <str_error.h>
43#include <ddf/log.h>
44#include <stdio.h>
45#include "i8042.h"
46
47#define CHECK_RET_RETURN(ret, message...) \
48 do { \
49 if (ret != EOK) { \
50 ddf_msg(LVL_ERROR, message); \
51 return ret; \
52 } \
53 } while (0)
54
55/** Get address of I/O registers.
56 *
57 * @param[in] dev Device asking for the addresses.
58 * @param[out] io_reg_address Base address of the memory range.
59 * @param[out] io_reg_size Size of the memory range.
60 * @param[out] kbd_irq Primary port IRQ.
61 * @param[out] mouse_irq Auxiliary port IRQ.
62 *
63 * @return Error code.
64 *
65 */
66static int get_my_registers(const ddf_dev_t *dev, uintptr_t *io_reg_address,
67 size_t *io_reg_size, int *kbd_irq, int *mouse_irq)
68{
69 assert(dev);
70
71 async_sess_t *parent_sess =
72 devman_parent_device_connect(EXCHANGE_SERIALIZE, dev->handle,
73 IPC_FLAG_BLOCKING);
74 if (!parent_sess)
75 return ENOMEM;
76
77 hw_res_list_parsed_t hw_resources;
78 hw_res_list_parsed_init(&hw_resources);
79 const int ret = hw_res_get_list_parsed(parent_sess, &hw_resources, 0);
80 async_hangup(parent_sess);
81 if (ret != EOK)
82 return ret;
83
84 if ((hw_resources.irqs.count != 2) ||
85 (hw_resources.io_ranges.count != 1)) {
86 hw_res_list_parsed_clean(&hw_resources);
87 return EINVAL;
88 }
89
90 if (io_reg_address)
91 *io_reg_address = hw_resources.io_ranges.ranges[0].address;
92
93 if (io_reg_size)
94 *io_reg_size = hw_resources.io_ranges.ranges[0].size;
95
96 if (kbd_irq)
97 *kbd_irq = hw_resources.irqs.irqs[0];
98
99 if (mouse_irq)
100 *mouse_irq = hw_resources.irqs.irqs[1];
101
102 hw_res_list_parsed_clean(&hw_resources);
103 return EOK;
104}
105
106/** Initialize a new ddf driver instance of i8042 driver
107 *
108 * @param[in] device DDF instance of the device to initialize.
109 *
110 * @return Error code.
111 *
112 */
113static int i8042_dev_add(ddf_dev_t *device)
114{
115 if (!device)
116 return EINVAL;
117
118 uintptr_t io_regs = 0;
119 size_t io_size = 0;
120 int kbd = 0;
121 int mouse = 0;
122
123 int ret = get_my_registers(device, &io_regs, &io_size, &kbd, &mouse);
124 CHECK_RET_RETURN(ret, "Failed to get registers: %s.",
125 str_error(ret));
126 ddf_msg(LVL_DEBUG, "I/O regs at %p (size %zuB), IRQ kbd %d, IRQ mouse %d.",
127 (void *) io_regs, io_size, kbd, mouse);
128
129 i8042_t *i8042 = ddf_dev_data_alloc(device, sizeof(i8042_t));
130 ret = (i8042 == NULL) ? ENOMEM : EOK;
131 CHECK_RET_RETURN(ret, "Failed to allocate i8042 driver instance.");
132
133 ret = i8042_init(i8042, (void *) io_regs, io_size, kbd, mouse, device);
134 CHECK_RET_RETURN(ret, "Failed to initialize i8042 driver: %s.",
135 str_error(ret));
136
137 ddf_msg(LVL_NOTE, "Controlling '%s' (%" PRIun ").",
138 device->name, device->handle);
139 return EOK;
140}
141
142/** DDF driver operations. */
143static driver_ops_t i8042_driver_ops = {
144 .dev_add = i8042_dev_add,
145};
146
147/** DDF driver. */
148static driver_t i8042_driver = {
149 .name = NAME,
150 .driver_ops = &i8042_driver_ops
151};
152
153int main(int argc, char *argv[])
154{
155 printf("%s: HelenOS PS/2 driver.\n", NAME);
156 ddf_log_init(NAME, LVL_NOTE);
157 return ddf_driver_main(&i8042_driver);
158}
159
160/**
161 * @}
162 */
Note: See TracBrowser for help on using the repository browser.