source: mainline/uspace/drv/char/i8042/i8042.c@ 78aa0ab

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

i8042: Use one device structure instead of multiple separate variables.

  • Property mode set to 100644
File size: 7.5 KB
Line 
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
70static 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
97static irq_code_t i8042_kbd = {
98 sizeof(i8042_cmds) / sizeof(irq_cmd_t),
99 i8042_cmds
100};
101
102static i8042_dev_t device;
103
104static void wait_ready(void)
105{
106 while (pio_read_8(&device.regs->status) & i8042_INPUT_FULL);
107}
108
109static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call);
110static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg);
111static int i8042_init(i8042_dev_t *dev);
112static void i8042_port_write(int devid, uint8_t data);
113
114
115int main(int argc, char *argv[])
116{
117 char name[16];
118 int i, rc;
119 const char dchar[MAX_DEVS] = { 'a', 'b' };
120
121 printf(NAME ": i8042 PS/2 port driver\n");
122
123 rc = loc_server_register(NAME, i8042_connection);
124 if (rc < 0) {
125 printf(NAME ": Unable to register server.\n");
126 return rc;
127 }
128
129 if (i8042_init(&device) != EOK)
130 return -1;
131
132 for (i = 0; i < MAX_DEVS; i++) {
133 device.port[i].client_sess = NULL;
134
135 snprintf(name, 16, "%s/ps2%c", NAMESPACE, dchar[i]);
136 rc = loc_service_register(name, &device.port[i].service_id);
137 if (rc != EOK) {
138 printf(NAME ": Unable to register device %s.\n", name);
139 return rc;
140 }
141 printf(NAME ": Registered device %s\n", name);
142 }
143
144 printf(NAME ": Accepting connections\n");
145 task_retval(0);
146 async_manager();
147
148 /* Not reached */
149 return 0;
150}
151
152static int i8042_init(i8042_dev_t *dev)
153{
154 static uintptr_t i8042_physical;
155 static uintptr_t i8042_kernel;
156 assert(dev);
157 if (sysinfo_get_value("i8042.address.physical", &i8042_physical) != EOK)
158 return -1;
159
160 if (sysinfo_get_value("i8042.address.kernel", &i8042_kernel) != EOK)
161 return -1;
162
163 void *vaddr;
164 if (pio_enable((void *) i8042_physical, sizeof(i8042_regs_t), &vaddr) != 0)
165 return -1;
166
167 dev->regs = vaddr;
168
169 sysarg_t inr_a;
170 sysarg_t inr_b;
171
172 if (sysinfo_get_value("i8042.inr_a", &inr_a) != EOK)
173 return -1;
174
175 if (sysinfo_get_value("i8042.inr_b", &inr_b) != EOK)
176 return -1;
177
178 async_set_interrupt_received(i8042_irq_handler);
179
180 /* Disable kbd and aux */
181 wait_ready();
182 pio_write_8(&dev->regs->status, i8042_CMD_WRITE_CMDB);
183 wait_ready();
184 pio_write_8(&dev->regs->data, i8042_KBD_DISABLE | i8042_AUX_DISABLE);
185
186 /* Flush all current IO */
187 while (pio_read_8(&dev->regs->status) & i8042_OUTPUT_FULL)
188 (void) pio_read_8(&dev->regs->data);
189
190 i8042_kbd.cmds[0].addr = (void *) &((i8042_regs_t *) i8042_kernel)->status;
191 i8042_kbd.cmds[3].addr = (void *) &((i8042_regs_t *) i8042_kernel)->data;
192 register_irq(inr_a, device_assign_devno(), 0, &i8042_kbd);
193 register_irq(inr_b, device_assign_devno(), 0, &i8042_kbd);
194 printf("%s: registered for interrupts %" PRIun " and %" PRIun "\n",
195 NAME, inr_a, inr_b);
196
197 wait_ready();
198 pio_write_8(&dev->regs->status, i8042_CMD_WRITE_CMDB);
199 wait_ready();
200 pio_write_8(&dev->regs->data, i8042_KBD_IE | i8042_KBD_TRANSLATE |
201 i8042_AUX_IE);
202
203 return 0;
204}
205
206/** Character device connection handler */
207static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
208{
209 ipc_callid_t callid;
210 ipc_call_t call;
211 sysarg_t method;
212 service_id_t dsid;
213 int retval;
214 int dev_id, i;
215
216 printf(NAME ": connection handler\n");
217
218 /* Get the device handle. */
219 dsid = IPC_GET_ARG1(*icall);
220
221 /* Determine which disk device is the client connecting to. */
222 dev_id = -1;
223 for (i = 0; i < MAX_DEVS; i++) {
224 if (device.port[i].service_id == dsid)
225 dev_id = i;
226 }
227
228 if (dev_id < 0) {
229 async_answer_0(iid, EINVAL);
230 return;
231 }
232
233 /* Answer the IPC_M_CONNECT_ME_TO call. */
234 async_answer_0(iid, EOK);
235
236 printf(NAME ": accepted connection\n");
237
238 while (1) {
239 callid = async_get_call(&call);
240 method = IPC_GET_IMETHOD(call);
241
242 if (!method) {
243 /* The other side has hung up. */
244 async_answer_0(callid, EOK);
245 return;
246 }
247
248 async_sess_t *sess =
249 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
250 if (sess != NULL) {
251 if (device.port[dev_id].client_sess == NULL) {
252 device.port[dev_id].client_sess = sess;
253 retval = EOK;
254 } else
255 retval = ELIMIT;
256 } else {
257 switch (method) {
258 case IPC_FIRST_USER_METHOD:
259 printf(NAME ": write %" PRIun " to devid %d\n",
260 IPC_GET_ARG1(call), dev_id);
261 i8042_port_write(dev_id, IPC_GET_ARG1(call));
262 retval = 0;
263 break;
264 default:
265 retval = EINVAL;
266 break;
267 }
268 }
269
270 async_answer_0(callid, retval);
271 }
272}
273
274void i8042_port_write(int devid, uint8_t data)
275{
276 if (devid == DEVID_AUX) {
277 wait_ready();
278 pio_write_8(&device.regs->status, i8042_CMD_WRITE_AUX);
279 }
280 wait_ready();
281 pio_write_8(&device.regs->data, data);
282}
283
284static 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 */
Note: See TracBrowser for help on using the repository browser.