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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since af0a2c7 was 75751db6, checked in by Jiri Svoboda <jiri@…>, 11 years ago

Factor out chardev IPC from pl050, i8042, xtkbd and ps2mouse.

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