1 | /*
|
---|
2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
3 | * Copyright (c) 2006 Josef Cejka
|
---|
4 | * Copyright (c) 2009 Jiri Svoboda
|
---|
5 | * All rights reserved.
|
---|
6 | *
|
---|
7 | * Redistribution and use in source and binary forms, with or without
|
---|
8 | * modification, are permitted provided that the following conditions
|
---|
9 | * are met:
|
---|
10 | *
|
---|
11 | * - Redistributions of source code must retain the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer.
|
---|
13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
14 | * notice, this list of conditions and the following disclaimer in the
|
---|
15 | * documentation and/or other materials provided with the distribution.
|
---|
16 | * - The name of the author may not be used to endorse or promote products
|
---|
17 | * derived from this software without specific prior written permission.
|
---|
18 | *
|
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /** @addtogroup kbd_port
|
---|
32 | * @ingroup kbd
|
---|
33 | * @{
|
---|
34 | */
|
---|
35 | /** @file
|
---|
36 | * @brief i8042 PS/2 port driver.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <ddi.h>
|
---|
40 | #include <libarch/ddi.h>
|
---|
41 | #include <loc.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <unistd.h>
|
---|
44 | #include <sysinfo.h>
|
---|
45 | #include <stdio.h>
|
---|
46 | #include <errno.h>
|
---|
47 | #include <inttypes.h>
|
---|
48 | #include "i8042.h"
|
---|
49 |
|
---|
50 | #define NAME "i8042"
|
---|
51 | #define NAMESPACE "char"
|
---|
52 |
|
---|
53 | /* Interesting bits for status register */
|
---|
54 | #define i8042_OUTPUT_FULL 0x01
|
---|
55 | #define i8042_INPUT_FULL 0x02
|
---|
56 | #define i8042_AUX_DATA 0x20
|
---|
57 |
|
---|
58 | /* Command constants */
|
---|
59 | #define i8042_CMD_WRITE_CMDB 0x60 /**< write command byte */
|
---|
60 | #define i8042_CMD_WRITE_AUX 0xd4 /**< write aux device */
|
---|
61 |
|
---|
62 | /* Command byte fields */
|
---|
63 | #define i8042_KBD_IE 0x01
|
---|
64 | #define i8042_AUX_IE 0x02
|
---|
65 | #define i8042_KBD_DISABLE 0x10
|
---|
66 | #define i8042_AUX_DISABLE 0x20
|
---|
67 | #define i8042_KBD_TRANSLATE 0x40
|
---|
68 |
|
---|
69 |
|
---|
70 | static irq_cmd_t i8042_cmds[] = {
|
---|
71 | {
|
---|
72 | .cmd = CMD_PIO_READ_8,
|
---|
73 | .addr = NULL, /* will be patched in run-time */
|
---|
74 | .dstarg = 1
|
---|
75 | },
|
---|
76 | {
|
---|
77 | .cmd = CMD_BTEST,
|
---|
78 | .value = i8042_OUTPUT_FULL,
|
---|
79 | .srcarg = 1,
|
---|
80 | .dstarg = 3
|
---|
81 | },
|
---|
82 | {
|
---|
83 | .cmd = CMD_PREDICATE,
|
---|
84 | .value = 2,
|
---|
85 | .srcarg = 3
|
---|
86 | },
|
---|
87 | {
|
---|
88 | .cmd = CMD_PIO_READ_8,
|
---|
89 | .addr = NULL, /* will be patched in run-time */
|
---|
90 | .dstarg = 2
|
---|
91 | },
|
---|
92 | {
|
---|
93 | .cmd = CMD_ACCEPT
|
---|
94 | }
|
---|
95 | };
|
---|
96 |
|
---|
97 | static irq_code_t i8042_kbd = {
|
---|
98 | sizeof(i8042_cmds) / sizeof(irq_cmd_t),
|
---|
99 | i8042_cmds
|
---|
100 | };
|
---|
101 |
|
---|
102 | static i8042_dev_t device;
|
---|
103 |
|
---|
104 | static void wait_ready(i8042_dev_t *dev)
|
---|
105 | {
|
---|
106 | assert(dev);
|
---|
107 | while (pio_read_8(&dev->regs->status) & i8042_INPUT_FULL);
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call);
|
---|
111 | static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
112 | static int i8042_init(i8042_dev_t *dev);
|
---|
113 | static void i8042_port_write(i8042_dev_t *dev, int devid, uint8_t data);
|
---|
114 |
|
---|
115 |
|
---|
116 | int main(int argc, char *argv[])
|
---|
117 | {
|
---|
118 | printf(NAME ": i8042 PS/2 port driver\n");
|
---|
119 |
|
---|
120 | int rc = loc_server_register(NAME, i8042_connection);
|
---|
121 | if (rc < 0) {
|
---|
122 | printf(NAME ": Unable to register server.\n");
|
---|
123 | return rc;
|
---|
124 | }
|
---|
125 |
|
---|
126 | if (i8042_init(&device) != EOK)
|
---|
127 | return -1;
|
---|
128 |
|
---|
129 | for (int i = 0; i < MAX_DEVS; i++) {
|
---|
130 | device.port[i].client_sess = NULL;
|
---|
131 |
|
---|
132 | static const char *names[MAX_DEVS] = {
|
---|
133 | NAMESPACE "/ps2a", NAMESPACE "/ps2b"};
|
---|
134 | rc = loc_service_register(names[i], &device.port[i].service_id);
|
---|
135 | if (rc != EOK) {
|
---|
136 | printf(NAME ": Unable to register device %s.\n",
|
---|
137 | names[i]);
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 | printf(NAME ": Registered device %s\n", names[i]);
|
---|
141 | }
|
---|
142 |
|
---|
143 | printf(NAME ": Accepting connections\n");
|
---|
144 | task_retval(0);
|
---|
145 | async_manager();
|
---|
146 |
|
---|
147 | /* Not reached */
|
---|
148 | return 0;
|
---|
149 | }
|
---|
150 |
|
---|
151 | static int i8042_init(i8042_dev_t *dev)
|
---|
152 | {
|
---|
153 | static uintptr_t i8042_physical;
|
---|
154 | static uintptr_t i8042_kernel;
|
---|
155 | assert(dev);
|
---|
156 | if (sysinfo_get_value("i8042.address.physical", &i8042_physical) != EOK)
|
---|
157 | return -1;
|
---|
158 |
|
---|
159 | if (sysinfo_get_value("i8042.address.kernel", &i8042_kernel) != EOK)
|
---|
160 | return -1;
|
---|
161 |
|
---|
162 | void *vaddr;
|
---|
163 | if (pio_enable((void *) i8042_physical, sizeof(i8042_regs_t), &vaddr) != 0)
|
---|
164 | return -1;
|
---|
165 |
|
---|
166 | dev->regs = vaddr;
|
---|
167 |
|
---|
168 | sysarg_t inr_a;
|
---|
169 | sysarg_t inr_b;
|
---|
170 |
|
---|
171 | if (sysinfo_get_value("i8042.inr_a", &inr_a) != EOK)
|
---|
172 | return -1;
|
---|
173 |
|
---|
174 | if (sysinfo_get_value("i8042.inr_b", &inr_b) != EOK)
|
---|
175 | return -1;
|
---|
176 |
|
---|
177 | async_set_interrupt_received(i8042_irq_handler);
|
---|
178 |
|
---|
179 | /* Disable kbd and aux */
|
---|
180 | wait_ready(dev);
|
---|
181 | pio_write_8(&dev->regs->status, i8042_CMD_WRITE_CMDB);
|
---|
182 | wait_ready(dev);
|
---|
183 | pio_write_8(&dev->regs->data, i8042_KBD_DISABLE | i8042_AUX_DISABLE);
|
---|
184 |
|
---|
185 | /* Flush all current IO */
|
---|
186 | while (pio_read_8(&dev->regs->status) & i8042_OUTPUT_FULL)
|
---|
187 | (void) pio_read_8(&dev->regs->data);
|
---|
188 |
|
---|
189 | i8042_kbd.cmds[0].addr = (void *) &((i8042_regs_t *) i8042_kernel)->status;
|
---|
190 | i8042_kbd.cmds[3].addr = (void *) &((i8042_regs_t *) i8042_kernel)->data;
|
---|
191 | register_irq(inr_a, device_assign_devno(), 0, &i8042_kbd);
|
---|
192 | register_irq(inr_b, device_assign_devno(), 0, &i8042_kbd);
|
---|
193 | printf("%s: registered for interrupts %" PRIun " and %" PRIun "\n",
|
---|
194 | NAME, inr_a, inr_b);
|
---|
195 |
|
---|
196 | wait_ready(dev);
|
---|
197 | pio_write_8(&dev->regs->status, i8042_CMD_WRITE_CMDB);
|
---|
198 | wait_ready(dev);
|
---|
199 | pio_write_8(&dev->regs->data, i8042_KBD_IE | i8042_KBD_TRANSLATE |
|
---|
200 | i8042_AUX_IE);
|
---|
201 |
|
---|
202 | return 0;
|
---|
203 | }
|
---|
204 |
|
---|
205 | /** Character device connection handler */
|
---|
206 | static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
207 | {
|
---|
208 | ipc_callid_t callid;
|
---|
209 | ipc_call_t call;
|
---|
210 | sysarg_t method;
|
---|
211 | service_id_t dsid;
|
---|
212 | int retval;
|
---|
213 | int dev_id, i;
|
---|
214 |
|
---|
215 | printf(NAME ": connection handler\n");
|
---|
216 |
|
---|
217 | /* Get the device handle. */
|
---|
218 | dsid = IPC_GET_ARG1(*icall);
|
---|
219 |
|
---|
220 | /* Determine which disk device is the client connecting to. */
|
---|
221 | dev_id = -1;
|
---|
222 | for (i = 0; i < MAX_DEVS; i++) {
|
---|
223 | if (device.port[i].service_id == dsid)
|
---|
224 | dev_id = i;
|
---|
225 | }
|
---|
226 |
|
---|
227 | if (dev_id < 0) {
|
---|
228 | async_answer_0(iid, EINVAL);
|
---|
229 | return;
|
---|
230 | }
|
---|
231 |
|
---|
232 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
---|
233 | async_answer_0(iid, EOK);
|
---|
234 |
|
---|
235 | printf(NAME ": accepted connection\n");
|
---|
236 |
|
---|
237 | while (1) {
|
---|
238 | callid = async_get_call(&call);
|
---|
239 | method = IPC_GET_IMETHOD(call);
|
---|
240 |
|
---|
241 | if (!method) {
|
---|
242 | /* The other side has hung up. */
|
---|
243 | async_answer_0(callid, EOK);
|
---|
244 | return;
|
---|
245 | }
|
---|
246 |
|
---|
247 | async_sess_t *sess =
|
---|
248 | async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
|
---|
249 | if (sess != NULL) {
|
---|
250 | if (device.port[dev_id].client_sess == NULL) {
|
---|
251 | device.port[dev_id].client_sess = sess;
|
---|
252 | retval = EOK;
|
---|
253 | } else
|
---|
254 | retval = ELIMIT;
|
---|
255 | } else {
|
---|
256 | switch (method) {
|
---|
257 | case IPC_FIRST_USER_METHOD:
|
---|
258 | printf(NAME ": write %" PRIun " to devid %d\n",
|
---|
259 | IPC_GET_ARG1(call), dev_id);
|
---|
260 | i8042_port_write(&device, dev_id, IPC_GET_ARG1(call));
|
---|
261 | retval = 0;
|
---|
262 | break;
|
---|
263 | default:
|
---|
264 | retval = EINVAL;
|
---|
265 | break;
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | async_answer_0(callid, retval);
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | void i8042_port_write(i8042_dev_t *dev, int devid, uint8_t data)
|
---|
274 | {
|
---|
275 | assert(dev);
|
---|
276 | if (devid == DEVID_AUX) {
|
---|
277 | wait_ready(dev);
|
---|
278 | pio_write_8(&dev->regs->status, i8042_CMD_WRITE_AUX);
|
---|
279 | }
|
---|
280 | wait_ready(dev);
|
---|
281 | pio_write_8(&dev->regs->data, data);
|
---|
282 | }
|
---|
283 |
|
---|
284 | static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call)
|
---|
285 | {
|
---|
286 | int status, data;
|
---|
287 | int devid;
|
---|
288 |
|
---|
289 | status = IPC_GET_ARG1(*call);
|
---|
290 | data = IPC_GET_ARG2(*call);
|
---|
291 |
|
---|
292 | if ((status & i8042_AUX_DATA)) {
|
---|
293 | devid = DEVID_AUX;
|
---|
294 | } else {
|
---|
295 | devid = DEVID_PRI;
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (device.port[devid].client_sess != NULL) {
|
---|
299 | async_exch_t *exch =
|
---|
300 | async_exchange_begin(device.port[devid].client_sess);
|
---|
301 | async_msg_1(exch, IPC_FIRST_USER_METHOD, data);
|
---|
302 | async_exchange_end(exch);
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * @}
|
---|
308 | */
|
---|