source: mainline/uspace/drv/char/i8042/i8042.c@ f991b6b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f991b6b was bd87ae0, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

i8042, xtkbd: Switch keyboard away from DDF provided interface.

  • Property mode set to 100644
File size: 9.7 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2006 Josef Cejka
4 * Copyright (c) 2009 Jiri Svoboda
5 * Copyright (c) 2011 Jan Vesely
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * - The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/** @addtogroup kbd_port
32 * @ingroup kbd
33 * @{
34 */
35/** @file
36 * @brief i8042 PS/2 port driver.
37 */
38
39#include <devman.h>
40#include <device/hw_res.h>
41#include <ddi.h>
42#include <libarch/ddi.h>
43#include <errno.h>
44#include <str_error.h>
45#include <inttypes.h>
46
47#include <ddf/log.h>
48#include <ddf/interrupt.h>
49
50#include "i8042.h"
51
52#define NAME "i8042"
53
54void default_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
55
56/** Port function operations. */
57static ddf_dev_ops_t ops = {
58 .default_handler = default_handler,
59};
60
61/* Interesting bits for status register */
62#define i8042_OUTPUT_FULL 0x01
63#define i8042_INPUT_FULL 0x02
64#define i8042_AUX_DATA 0x20
65
66/* Command constants */
67#define i8042_CMD_WRITE_CMDB 0x60 /**< write command byte */
68#define i8042_CMD_WRITE_AUX 0xd4 /**< write aux device */
69
70/* Command byte fields */
71#define i8042_KBD_IE 0x01
72#define i8042_AUX_IE 0x02
73#define i8042_KBD_DISABLE 0x10
74#define i8042_AUX_DISABLE 0x20
75#define i8042_KBD_TRANSLATE 0x40 /* Use this to switch to XT scancodes */
76
77/** i8042 Interrupt pseudo-code. */
78static const irq_cmd_t i8042_cmds[] = {
79 {
80 .cmd = CMD_PIO_READ_8,
81 .addr = NULL, /* will be patched in run-time */
82 .dstarg = 1
83 },
84 {
85 .cmd = CMD_BTEST,
86 .value = i8042_OUTPUT_FULL,
87 .srcarg = 1,
88 .dstarg = 3
89 },
90 {
91 .cmd = CMD_PREDICATE,
92 .value = 2,
93 .srcarg = 3
94 },
95 {
96 .cmd = CMD_PIO_READ_8,
97 .addr = NULL, /* will be patched in run-time */
98 .dstarg = 2
99 },
100 {
101 .cmd = CMD_ACCEPT
102 }
103};
104
105/** Wait until it is safe to write to the device. */
106static void wait_ready(i8042_t *dev)
107{
108 assert(dev);
109 while (pio_read_8(&dev->regs->status) & i8042_INPUT_FULL);
110}
111
112/** Interrupt handler routine.
113 * Writes new data to the corresponding buffer.
114 * @param dev Device that caued the interrupt.
115 * @param iid Call id.
116 * @param call pointerr to call data.
117 */
118static void i8042_irq_handler(
119 ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
120{
121 if (!dev || !dev->driver_data)
122 return;
123 i8042_t *controller = dev->driver_data;
124
125 const uint8_t status = IPC_GET_ARG1(*call);
126 const uint8_t data = IPC_GET_ARG2(*call);
127 buffer_t *buffer = (status & i8042_AUX_DATA) ?
128 &controller->aux_buffer : &controller->kbd_buffer;
129 buffer_write(buffer, data);
130}
131
132/** Initialize i8042 driver structure.
133 * @param dev Driver structure to initialize.
134 * @param regs I/O address of registers.
135 * @param reg_size size of the reserved I/O address space.
136 * @param irq_kbd IRQ for primary port.
137 * @param irq_mouse IRQ for aux port.
138 * @param ddf_dev DDF device structure of the device.
139 * @return Error code.
140 */
141int i8042_init(i8042_t *dev, void *regs, size_t reg_size, int irq_kbd,
142 int irq_mouse, ddf_dev_t *ddf_dev)
143{
144 assert(ddf_dev);
145 assert(dev);
146
147 if (reg_size < sizeof(i8042_regs_t))
148 return EINVAL;
149
150 if (pio_enable(regs, sizeof(i8042_regs_t), (void**)&dev->regs) != 0)
151 return -1;
152
153 dev->kbd_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2a");
154 if (!dev->kbd_fun)
155 return ENOMEM;
156 int ret = ddf_fun_add_match_id(dev->kbd_fun, "char/xtkbd", 90);
157 if (ret != EOK) {
158 ddf_fun_destroy(dev->kbd_fun);
159 return ret;
160 }
161
162 dev->aux_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2b");
163 if (!dev->aux_fun) {
164 ddf_fun_destroy(dev->kbd_fun);
165 return ENOMEM;
166 }
167
168 ret = ddf_fun_add_match_id(dev->aux_fun, "char/ps2mouse", 90);
169 if (ret != EOK) {
170 ddf_fun_destroy(dev->kbd_fun);
171 ddf_fun_destroy(dev->aux_fun);
172 return ret;
173 }
174
175 dev->kbd_fun->ops = &ops;
176 dev->aux_fun->ops = &ops;
177 dev->kbd_fun->driver_data = dev;
178 dev->aux_fun->driver_data = dev;
179
180 buffer_init(&dev->kbd_buffer, dev->kbd_data, BUFFER_SIZE);
181 buffer_init(&dev->aux_buffer, dev->aux_data, BUFFER_SIZE);
182 fibril_mutex_initialize(&dev->write_guard);
183
184#define CHECK_RET_DESTROY(ret, msg...) \
185if (ret != EOK) { \
186 ddf_msg(LVL_ERROR, msg); \
187 if (dev->kbd_fun) { \
188 dev->kbd_fun->driver_data = NULL; \
189 ddf_fun_destroy(dev->kbd_fun); \
190 } \
191 if (dev->aux_fun) { \
192 dev->aux_fun->driver_data = NULL; \
193 ddf_fun_destroy(dev->aux_fun); \
194 } \
195} else (void)0
196
197 ret = ddf_fun_bind(dev->kbd_fun);
198 CHECK_RET_DESTROY(ret,
199 "Failed to bind keyboard function: %s.", str_error(ret));
200
201 ret = ddf_fun_bind(dev->aux_fun);
202 CHECK_RET_DESTROY(ret,
203 "Failed to bind mouse function: %s.", str_error(ret));
204
205 /* Disable kbd and aux */
206 wait_ready(dev);
207 pio_write_8(&dev->regs->status, i8042_CMD_WRITE_CMDB);
208 wait_ready(dev);
209 pio_write_8(&dev->regs->data, i8042_KBD_DISABLE | i8042_AUX_DISABLE);
210
211 /* Flush all current IO */
212 while (pio_read_8(&dev->regs->status) & i8042_OUTPUT_FULL)
213 (void) pio_read_8(&dev->regs->data);
214
215#define CHECK_RET_UNBIND_DESTROY(ret, msg...) \
216if (ret != EOK) { \
217 ddf_msg(LVL_ERROR, msg); \
218 if (dev->kbd_fun) { \
219 ddf_fun_unbind(dev->kbd_fun); \
220 dev->kbd_fun->driver_data = NULL; \
221 ddf_fun_destroy(dev->kbd_fun); \
222 } \
223 if (dev->aux_fun) { \
224 ddf_fun_unbind(dev->aux_fun); \
225 dev->aux_fun->driver_data = NULL; \
226 ddf_fun_destroy(dev->aux_fun); \
227 } \
228} else (void)0
229
230 const size_t cmd_count = sizeof(i8042_cmds) / sizeof(irq_cmd_t);
231 irq_cmd_t cmds[cmd_count];
232 memcpy(cmds, i8042_cmds, sizeof(i8042_cmds));
233 cmds[0].addr = (void *) &dev->regs->status;
234 cmds[3].addr = (void *) &dev->regs->data;
235
236 irq_code_t irq_code = { .cmdcount = cmd_count, .cmds = cmds };
237 ret = register_interrupt_handler(ddf_dev, irq_kbd, i8042_irq_handler,
238 &irq_code);
239 CHECK_RET_UNBIND_DESTROY(ret,
240 "Failed set handler for kbd: %s.", str_error(ret));
241
242 ret = register_interrupt_handler(ddf_dev, irq_mouse, i8042_irq_handler,
243 &irq_code);
244 CHECK_RET_UNBIND_DESTROY(ret,
245 "Failed set handler for mouse: %s.", str_error(ret));
246
247 /* Enable interrupts */
248 async_sess_t *parent_sess =
249 devman_parent_device_connect(EXCHANGE_SERIALIZE, ddf_dev->handle,
250 IPC_FLAG_BLOCKING);
251 ret = parent_sess ? EOK : ENOMEM;
252 CHECK_RET_UNBIND_DESTROY(ret, "Failed to create parent connection.");
253
254 const bool enabled = hw_res_enable_interrupt(parent_sess);
255 async_hangup(parent_sess);
256 ret = enabled ? EOK : EIO;
257 CHECK_RET_UNBIND_DESTROY(ret, "Failed to enable interrupts: %s.");
258
259 /* Enable port interrupts. */
260 wait_ready(dev);
261 pio_write_8(&dev->regs->status, i8042_CMD_WRITE_CMDB);
262 wait_ready(dev);
263 pio_write_8(&dev->regs->data, i8042_KBD_IE | i8042_KBD_TRANSLATE |
264 i8042_AUX_IE);
265
266 return EOK;
267}
268
269// TODO use shared instead this
270enum {
271 IPC_CHAR_READ = DEV_FIRST_CUSTOM_METHOD,
272 IPC_CHAR_WRITE,
273};
274
275/** Write data to i8042 port.
276 * @param fun DDF function.
277 * @param buffer Data source.
278 * @param size Data size.
279 * @return Bytes written.
280 */
281static int i8042_write(ddf_fun_t *fun, char *buffer, size_t size)
282{
283 assert(fun);
284 assert(fun->driver_data);
285 i8042_t *controller = fun->driver_data;
286 fibril_mutex_lock(&controller->write_guard);
287 for (size_t i = 0; i < size; ++i) {
288 wait_ready(controller);
289 if (controller->aux_fun == fun)
290 pio_write_8(
291 &controller->regs->status, i8042_CMD_WRITE_AUX);
292 pio_write_8(&controller->regs->data, buffer[i]);
293 }
294 fibril_mutex_unlock(&controller->write_guard);
295 return size;
296}
297
298/** Read data from i8042 port.
299 * @param fun DDF function.
300 * @param buffer Data place.
301 * @param size Data place size.
302 * @return Bytes read.
303 */
304static int i8042_read(ddf_fun_t *fun, char *data, size_t size)
305{
306 assert(fun);
307 assert(fun->driver_data);
308
309 i8042_t *controller = fun->driver_data;
310 buffer_t *buffer = (fun == controller->aux_fun) ?
311 &controller->aux_buffer : &controller->kbd_buffer;
312 for (size_t i = 0; i < size; ++i) {
313 *data++ = buffer_read(buffer);
314 }
315 return size;
316}
317
318/** Handle data requests.
319 * @param fun ddf_fun_t function.
320 * @param id callid
321 * @param call IPC request.
322 */
323void default_handler(ddf_fun_t *fun, ipc_callid_t id, ipc_call_t *call)
324{
325 const sysarg_t method = IPC_GET_IMETHOD(*call);
326 const size_t size = IPC_GET_ARG1(*call);
327 switch (method) {
328 case IPC_CHAR_READ:
329 if (size <= 4 * sizeof(sysarg_t)) {
330 sysarg_t message[4] = {};
331 i8042_read(fun, (char*)message, size);
332 async_answer_4(id, size, message[0], message[1],
333 message[2], message[3]);
334 } else {
335 async_answer_0(id, ELIMIT);
336 }
337 break;
338
339 case IPC_CHAR_WRITE:
340 if (size <= 3 * sizeof(sysarg_t)) {
341 const sysarg_t message[3] = {
342 IPC_GET_ARG2(*call), IPC_GET_ARG3(*call),
343 IPC_GET_ARG4(*call) };
344 i8042_write(fun, (char*)message, size);
345 async_answer_0(id, size);
346 } else {
347 async_answer_0(id, ELIMIT);
348 }
349
350 default:
351 async_answer_0(id, EINVAL);
352 }
353}
354/**
355 * @}
356 */
Note: See TracBrowser for help on using the repository browser.