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

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

Make ddf_dev_t and ddf_fun_t opaque. This further tighthens the DDF interface.

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