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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ca4730a5 was ca4730a5, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Remove evil macros.

  • Property mode set to 100644
File size: 4.6 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 <libarch/config.h>
39#include <ddf/driver.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 <async.h>
46#include "i8042.h"
47
48/** Get address of I/O registers.
49 *
50 * @param[in] dev Device asking for the addresses.
51 * @param[out] io_reg_address Base address of the memory range.
52 * @param[out] io_reg_size Size of the memory range.
53 * @param[out] kbd_irq Primary port IRQ.
54 * @param[out] mouse_irq Auxiliary port IRQ.
55 *
56 * @return Error code.
57 *
58 */
59static int get_my_registers(ddf_dev_t *dev, uintptr_t *io_reg_address,
60 size_t *io_reg_size, int *kbd_irq, int *mouse_irq)
61{
62 assert(dev);
63
64 async_sess_t *parent_sess = ddf_dev_parent_sess_create(
65 dev, EXCHANGE_SERIALIZE);
66 if (parent_sess == NULL)
67 return ENOMEM;
68
69 hw_res_list_parsed_t hw_resources;
70 hw_res_list_parsed_init(&hw_resources);
71 const int ret = hw_res_get_list_parsed(parent_sess, &hw_resources, 0);
72 if (ret != EOK)
73 return ret;
74
75 if ((hw_resources.irqs.count != 2) ||
76 (hw_resources.io_ranges.count != 1)) {
77 hw_res_list_parsed_clean(&hw_resources);
78 return EINVAL;
79 }
80
81 if (io_reg_address)
82 *io_reg_address = hw_resources.io_ranges.ranges[0].address;
83
84 if (io_reg_size)
85 *io_reg_size = hw_resources.io_ranges.ranges[0].size;
86
87 if (kbd_irq)
88 *kbd_irq = hw_resources.irqs.irqs[0];
89
90 if (mouse_irq)
91 *mouse_irq = hw_resources.irqs.irqs[1];
92
93 hw_res_list_parsed_clean(&hw_resources);
94 return EOK;
95}
96
97/** Initialize a new ddf driver instance of i8042 driver
98 *
99 * @param[in] device DDF instance of the device to initialize.
100 *
101 * @return Error code.
102 *
103 */
104static int i8042_dev_add(ddf_dev_t *device)
105{
106 uintptr_t io_regs = 0;
107 size_t io_size = 0;
108 int kbd = 0;
109 int mouse = 0;
110 int rc;
111
112 if (!device)
113 return EINVAL;
114
115 rc = get_my_registers(device, &io_regs, &io_size, &kbd, &mouse);
116 if (rc != EOK) {
117 ddf_msg(LVL_ERROR, "Failed to get registers: %s.",
118 str_error(rc));
119 return rc;
120 }
121
122 ddf_msg(LVL_DEBUG, "I/O regs at %p (size %zuB), IRQ kbd %d, IRQ mouse %d.",
123 (void *) io_regs, io_size, kbd, mouse);
124
125 i8042_t *i8042 = ddf_dev_data_alloc(device, sizeof(i8042_t));
126 if (i8042 == NULL) {
127 ddf_msg(LVL_ERROR, "Out of memory.");
128 return ENOMEM;
129 }
130
131 rc = i8042_init(i8042, (void *) io_regs, io_size, kbd, mouse, device);
132 if (rc != EOK) {
133 ddf_msg(LVL_ERROR, "Failed to initialize i8042 driver: %s.",
134 str_error(rc));
135 return rc;
136 }
137
138 ddf_msg(LVL_NOTE, "Controlling '%s' (%" PRIun ").",
139 ddf_dev_get_name(device), ddf_dev_get_handle(device));
140 return EOK;
141}
142
143/** DDF driver operations. */
144static driver_ops_t i8042_driver_ops = {
145 .dev_add = i8042_dev_add,
146};
147
148/** DDF driver. */
149static driver_t i8042_driver = {
150 .name = NAME,
151 .driver_ops = &i8042_driver_ops
152};
153
154int main(int argc, char *argv[])
155{
156 printf("%s: HelenOS PS/2 driver.\n", NAME);
157 ddf_log_init(NAME);
158
159 /*
160 * Alleviate the virtual memory / page table pressure caused by
161 * interrupt storms when the default large stacks are used.
162 */
163 async_set_interrupt_handler_stack_size(PAGE_SIZE);
164
165 return ddf_driver_main(&i8042_driver);
166}
167
168/**
169 * @}
170 */
Note: See TracBrowser for help on using the repository browser.