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

Last change on this file was 797ab95, checked in by Jiri Svoboda <jiri@…>, 4 months ago

Implement quiesce in i8042.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
3 * Copyright (c) 2011 Jan Vesely
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup i8042
31 * @{
32 */
33
34/** @file
35 * @brief i8042 driver DDF bits.
36 */
37
38#include <inttypes.h>
39#include <libarch/config.h>
40#include <ddf/driver.h>
41#include <device/hw_res_parsed.h>
42#include <errno.h>
43#include <str_error.h>
44#include <ddf/log.h>
45#include <stdio.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] p_io_reg Pointer to register range.
52 * @param[out] kbd_irq Primary port IRQ.
53 * @param[out] mouse_irq Auxiliary port IRQ.
54 *
55 * @return Error code.
56 *
57 */
58static errno_t get_my_registers(ddf_dev_t *dev, addr_range_t *p_io_reg,
59 int *kbd_irq, int *mouse_irq)
60{
61 assert(dev);
62
63 async_sess_t *parent_sess = ddf_dev_parent_sess_get(dev);
64 if (parent_sess == NULL)
65 return ENOMEM;
66
67 hw_res_list_parsed_t hw_resources;
68 hw_res_list_parsed_init(&hw_resources);
69 const errno_t ret = hw_res_get_list_parsed(parent_sess, &hw_resources, 0);
70 if (ret != EOK)
71 return ret;
72
73 if ((hw_resources.irqs.count != 2) ||
74 (hw_resources.io_ranges.count != 1)) {
75 hw_res_list_parsed_clean(&hw_resources);
76 return EINVAL;
77 }
78
79 if (p_io_reg)
80 *p_io_reg = hw_resources.io_ranges.ranges[0];
81
82 if (kbd_irq)
83 *kbd_irq = hw_resources.irqs.irqs[0];
84
85 if (mouse_irq)
86 *mouse_irq = hw_resources.irqs.irqs[1];
87
88 hw_res_list_parsed_clean(&hw_resources);
89 return EOK;
90}
91
92/** Initialize a new ddf driver instance of i8042 driver
93 *
94 * @param[in] device DDF instance of the device to initialize.
95 *
96 * @return Error code.
97 *
98 */
99static errno_t i8042_dev_add(ddf_dev_t *device)
100{
101 addr_range_t io_regs;
102 int kbd = 0;
103 int mouse = 0;
104 errno_t rc;
105
106 if (!device)
107 return EINVAL;
108
109 rc = get_my_registers(device, &io_regs, &kbd, &mouse);
110 if (rc != EOK) {
111 ddf_msg(LVL_ERROR, "Failed to get registers: %s.",
112 str_error(rc));
113 return rc;
114 }
115
116 ddf_msg(LVL_DEBUG,
117 "I/O regs at %p (size %zuB), IRQ kbd %d, IRQ mouse %d.",
118 RNGABSPTR(io_regs), RNGSZ(io_regs), kbd, mouse);
119
120 i8042_t *i8042 = ddf_dev_data_alloc(device, sizeof(i8042_t));
121 if (i8042 == NULL) {
122 ddf_msg(LVL_ERROR, "Out of memory.");
123 return ENOMEM;
124 }
125
126 rc = i8042_init(i8042, &io_regs, kbd, mouse, device);
127 if (rc != EOK) {
128 ddf_msg(LVL_ERROR, "Failed to initialize i8042 driver: %s.",
129 str_error(rc));
130 return rc;
131 }
132
133 ddf_msg(LVL_NOTE, "Controlling '%s' (%" PRIun ").",
134 ddf_dev_get_name(device), ddf_dev_get_handle(device));
135 return EOK;
136}
137
138/** Initialize a new ddf driver instance of i8042 driver
139 *
140 * @param[in] device DDF instance of the device to initialize.
141 *
142 * @return Error code.
143 *
144 */
145static errno_t i8042_dev_quiesce(ddf_dev_t *device)
146{
147 i8042_t *i8042;
148
149 ddf_msg(LVL_DEBUG, "i8042_dev_quiesce()");
150
151 i8042 = (i8042_t *)ddf_dev_data_get(device);
152 i8042_quiesce(i8042);
153 return EOK;
154}
155
156/** DDF driver operations. */
157static driver_ops_t i8042_driver_ops = {
158 .dev_add = i8042_dev_add,
159 .dev_quiesce = i8042_dev_quiesce
160};
161
162/** DDF driver. */
163static driver_t i8042_driver = {
164 .name = NAME,
165 .driver_ops = &i8042_driver_ops
166};
167
168int main(int argc, char *argv[])
169{
170 printf("%s: HelenOS PS/2 driver.\n", NAME);
171 ddf_log_init(NAME);
172
173 return ddf_driver_main(&i8042_driver);
174}
175
176/**
177 * @}
178 */
Note: See TracBrowser for help on using the repository browser.