source: mainline/uspace/drv/char/i8042/i8042.c@ 35b8bfe

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 35b8bfe was 8820544, checked in by Martin Decky <martin@…>, 11 years ago

support for kernel notification multiplexing in the async framework

  • rename SYS_EVENT_* and SYS_IRQ_* syscalls to unify the terminology
  • add SYS_IPC_EVENT_UNSUBSCRIBE
  • remove IRQ handler multiplexing from DDF, the generic mechanism replaces it (unfortunatelly the order of arguments used by interrupt_handler_t needs to be permutated to align with the async framework conventions)
  • Property mode set to 100644
File size: 9.8 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
32/** @addtogroup kbd_port
33 * @ingroup kbd
34 * @{
35 */
36
37/** @file
38 * @brief i8042 PS/2 port driver.
39 */
40
41#include <device/hw_res.h>
42#include <ddi.h>
43#include <errno.h>
44#include <str_error.h>
45#include <inttypes.h>
46#include <ddf/log.h>
47#include <ddf/interrupt.h>
48#include "i8042.h"
49
50/* Interesting bits for status register */
51#define i8042_OUTPUT_FULL 0x01
52#define i8042_INPUT_FULL 0x02
53#define i8042_AUX_DATA 0x20
54
55/* Command constants */
56#define i8042_CMD_WRITE_CMDB 0x60 /**< Write command byte */
57#define i8042_CMD_WRITE_AUX 0xd4 /**< Write aux device */
58
59/* Command byte fields */
60#define i8042_KBD_IE 0x01
61#define i8042_AUX_IE 0x02
62#define i8042_KBD_DISABLE 0x10
63#define i8042_AUX_DISABLE 0x20
64#define i8042_KBD_TRANSLATE 0x40 /* Use this to switch to XT scancodes */
65
66static void default_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
67
68/** Port function operations. */
69static ddf_dev_ops_t ops = {
70 .default_handler = default_handler
71};
72
73static const irq_pio_range_t i8042_ranges[] = {
74 {
75 .base = 0,
76 .size = sizeof(i8042_regs_t)
77 }
78};
79
80/** i8042 Interrupt pseudo-code. */
81static const irq_cmd_t i8042_cmds[] = {
82 {
83 .cmd = CMD_PIO_READ_8,
84 .addr = NULL, /* will be patched in run-time */
85 .dstarg = 1
86 },
87 {
88 .cmd = CMD_AND,
89 .value = i8042_OUTPUT_FULL,
90 .srcarg = 1,
91 .dstarg = 3
92 },
93 {
94 .cmd = CMD_PREDICATE,
95 .value = 2,
96 .srcarg = 3
97 },
98 {
99 .cmd = CMD_PIO_READ_8,
100 .addr = NULL, /* will be patched in run-time */
101 .dstarg = 2
102 },
103 {
104 .cmd = CMD_ACCEPT
105 }
106};
107
108/** Get i8042 soft state from device node. */
109static i8042_t *dev_i8042(ddf_dev_t *dev)
110{
111 return ddf_dev_data_get(dev);
112}
113
114/** Wait until it is safe to write to the device. */
115static void wait_ready(i8042_t *dev)
116{
117 assert(dev);
118 while (pio_read_8(&dev->regs->status) & i8042_INPUT_FULL);
119}
120
121/** Interrupt handler routine.
122 *
123 * Write new data to the corresponding buffer.
124 *
125 * @param iid Call id.
126 * @param call pointerr to call data.
127 * @param dev Device that caued the interrupt.
128 *
129 */
130static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call,
131 ddf_dev_t *dev)
132{
133 i8042_t *controller = dev_i8042(dev);
134
135 const uint8_t status = IPC_GET_ARG1(*call);
136 const uint8_t data = IPC_GET_ARG2(*call);
137
138 buffer_t *buffer = (status & i8042_AUX_DATA) ?
139 &controller->aux_buffer : &controller->kbd_buffer;
140
141 buffer_write(buffer, data);
142}
143
144/** Initialize i8042 driver structure.
145 *
146 * @param dev Driver structure to initialize.
147 * @param regs I/O range of registers.
148 * @param irq_kbd IRQ for primary port.
149 * @param irq_mouse IRQ for aux port.
150 * @param ddf_dev DDF device structure of the device.
151 *
152 * @return Error code.
153 *
154 */
155int i8042_init(i8042_t *dev, addr_range_t *regs, int irq_kbd,
156 int irq_mouse, ddf_dev_t *ddf_dev)
157{
158 const size_t range_count = sizeof(i8042_ranges) /
159 sizeof(irq_pio_range_t);
160 irq_pio_range_t ranges[range_count];
161 const size_t cmd_count = sizeof(i8042_cmds) / sizeof(irq_cmd_t);
162 irq_cmd_t cmds[cmd_count];
163 i8042_regs_t *ar;
164
165 int rc;
166 bool kbd_bound = false;
167 bool aux_bound = false;
168
169 dev->kbd_fun = NULL;
170 dev->aux_fun = NULL;
171
172 if (regs->size < sizeof(i8042_regs_t)) {
173 rc = EINVAL;
174 goto error;
175 }
176
177 if (pio_enable_range(regs, (void **) &dev->regs) != 0) {
178 rc = EIO;
179 goto error;
180 }
181
182 dev->kbd_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2a");
183 if (dev->kbd_fun == NULL) {
184 rc = ENOMEM;
185 goto error;
186 };
187
188 rc = ddf_fun_add_match_id(dev->kbd_fun, "char/xtkbd", 90);
189 if (rc != EOK)
190 goto error;
191
192 dev->aux_fun = ddf_fun_create(ddf_dev, fun_inner, "ps2b");
193 if (dev->aux_fun == NULL) {
194 rc = ENOMEM;
195 goto error;
196 }
197
198 rc = ddf_fun_add_match_id(dev->aux_fun, "char/ps2mouse", 90);
199 if (rc != EOK)
200 goto error;
201
202 ddf_fun_set_ops(dev->kbd_fun, &ops);
203 ddf_fun_set_ops(dev->aux_fun, &ops);
204
205 buffer_init(&dev->kbd_buffer, dev->kbd_data, BUFFER_SIZE);
206 buffer_init(&dev->aux_buffer, dev->aux_data, BUFFER_SIZE);
207 fibril_mutex_initialize(&dev->write_guard);
208
209 rc = ddf_fun_bind(dev->kbd_fun);
210 if (rc != EOK) {
211 ddf_msg(LVL_ERROR, "Failed to bind keyboard function: %s.",
212 ddf_fun_get_name(dev->kbd_fun));
213 goto error;
214 }
215 kbd_bound = true;
216
217 rc = ddf_fun_bind(dev->aux_fun);
218 if (rc != EOK) {
219 ddf_msg(LVL_ERROR, "Failed to bind aux function: %s.",
220 ddf_fun_get_name(dev->aux_fun));
221 goto error;
222 }
223 aux_bound = true;
224
225 /* Disable kbd and aux */
226 wait_ready(dev);
227 pio_write_8(&dev->regs->status, i8042_CMD_WRITE_CMDB);
228 wait_ready(dev);
229 pio_write_8(&dev->regs->data, i8042_KBD_DISABLE | i8042_AUX_DISABLE);
230
231 /* Flush all current IO */
232 while (pio_read_8(&dev->regs->status) & i8042_OUTPUT_FULL)
233 (void) pio_read_8(&dev->regs->data);
234
235 memcpy(ranges, i8042_ranges, sizeof(i8042_ranges));
236 ranges[0].base = RNGABS(*regs);
237
238
239 ar = RNGABSPTR(*regs);
240 memcpy(cmds, i8042_cmds, sizeof(i8042_cmds));
241 cmds[0].addr = (void *) &ar->status;
242 cmds[3].addr = (void *) &ar->data;
243
244 irq_code_t irq_code = {
245 .rangecount = range_count,
246 .ranges = ranges,
247 .cmdcount = cmd_count,
248 .cmds = cmds
249 };
250
251 rc = register_interrupt_handler(ddf_dev, irq_kbd, i8042_irq_handler,
252 &irq_code);
253 if (rc != EOK) {
254 ddf_msg(LVL_ERROR, "Failed set handler for kbd: %s.",
255 ddf_dev_get_name(ddf_dev));
256 goto error;
257 }
258
259 rc = register_interrupt_handler(ddf_dev, irq_mouse, i8042_irq_handler,
260 &irq_code);
261 if (rc != EOK) {
262 ddf_msg(LVL_ERROR, "Failed set handler for mouse: %s.",
263 ddf_dev_get_name(ddf_dev));
264 goto error;
265 }
266
267 /* Enable interrupts */
268 async_sess_t *parent_sess = ddf_dev_parent_sess_get(ddf_dev);
269 assert(parent_sess != NULL);
270
271 const bool enabled = hw_res_enable_interrupt(parent_sess);
272 if (!enabled) {
273 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed to enable interrupts: %s.",
274 ddf_dev_get_name(ddf_dev));
275 rc = EIO;
276 goto error;
277 }
278
279 /* Enable port interrupts. */
280 wait_ready(dev);
281 pio_write_8(&dev->regs->status, i8042_CMD_WRITE_CMDB);
282 wait_ready(dev);
283 pio_write_8(&dev->regs->data, i8042_KBD_IE | i8042_KBD_TRANSLATE |
284 i8042_AUX_IE);
285
286 return EOK;
287error:
288 if (kbd_bound)
289 ddf_fun_unbind(dev->kbd_fun);
290 if (aux_bound)
291 ddf_fun_unbind(dev->aux_fun);
292 if (dev->kbd_fun != NULL)
293 ddf_fun_destroy(dev->kbd_fun);
294 if (dev->aux_fun != NULL)
295 ddf_fun_destroy(dev->aux_fun);
296
297 return rc;
298}
299
300// FIXME TODO use shared instead this
301enum {
302 IPC_CHAR_READ = DEV_FIRST_CUSTOM_METHOD,
303 IPC_CHAR_WRITE,
304};
305
306/** Write data to i8042 port.
307 *
308 * @param fun DDF function.
309 * @param buffer Data source.
310 * @param size Data size.
311 *
312 * @return Bytes written.
313 *
314 */
315static int i8042_write(ddf_fun_t *fun, char *buffer, size_t size)
316{
317 i8042_t *controller = dev_i8042(ddf_fun_get_dev(fun));
318 fibril_mutex_lock(&controller->write_guard);
319
320 for (size_t i = 0; i < size; ++i) {
321 if (controller->aux_fun == fun) {
322 wait_ready(controller);
323 pio_write_8(&controller->regs->status,
324 i8042_CMD_WRITE_AUX);
325 }
326
327 wait_ready(controller);
328 pio_write_8(&controller->regs->data, buffer[i]);
329 }
330
331 fibril_mutex_unlock(&controller->write_guard);
332 return size;
333}
334
335/** Read data from i8042 port.
336 *
337 * @param fun DDF function.
338 * @param buffer Data place.
339 * @param size Data place size.
340 *
341 * @return Bytes read.
342 *
343 */
344static int i8042_read(ddf_fun_t *fun, char *data, size_t size)
345{
346 i8042_t *controller = dev_i8042(ddf_fun_get_dev(fun));
347 buffer_t *buffer = (fun == controller->aux_fun) ?
348 &controller->aux_buffer : &controller->kbd_buffer;
349
350 for (size_t i = 0; i < size; ++i)
351 *data++ = buffer_read(buffer);
352
353 return size;
354}
355
356/** Handle data requests.
357 *
358 * @param fun ddf_fun_t function.
359 * @param id callid
360 * @param call IPC request.
361 *
362 */
363static void default_handler(ddf_fun_t *fun, ipc_callid_t id, ipc_call_t *call)
364{
365 const sysarg_t method = IPC_GET_IMETHOD(*call);
366 const size_t size = IPC_GET_ARG1(*call);
367
368 switch (method) {
369 case IPC_CHAR_READ:
370 if (size <= 4 * sizeof(sysarg_t)) {
371 sysarg_t message[4] = {};
372
373 i8042_read(fun, (char *) message, size);
374 async_answer_4(id, size, message[0], message[1],
375 message[2], message[3]);
376 } else
377 async_answer_0(id, ELIMIT);
378 break;
379
380 case IPC_CHAR_WRITE:
381 if (size <= 3 * sizeof(sysarg_t)) {
382 const sysarg_t message[3] = {
383 IPC_GET_ARG2(*call),
384 IPC_GET_ARG3(*call),
385 IPC_GET_ARG4(*call)
386 };
387
388 i8042_write(fun, (char *) message, size);
389 async_answer_0(id, size);
390 } else
391 async_answer_0(id, ELIMIT);
392
393 default:
394 async_answer_0(id, EINVAL);
395 }
396}
397
398/**
399 * @}
400 */
Note: See TracBrowser for help on using the repository browser.