Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/kb8042/kb8042.c
+++ new/usr/src/uts/common/io/kb8042/kb8042.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /* Copyright (c) 1990, 1991 UNIX System Laboratories, Inc. */
22 22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989, 1990 AT&T */
23 23 /* All Rights Reserved */
24 24
25 25 /*
26 26 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 30 #define KEYMAP_SIZE_VARIABLE
31 31
32 32 #include <sys/types.h>
33 33 #include <sys/errno.h>
34 34 #include <sys/inline.h>
35 35 #include <sys/termio.h>
36 36 #include <sys/stropts.h>
37 37 #include <sys/termios.h>
38 38 #include <sys/stream.h>
39 39 #include <sys/strtty.h>
40 40 #include <sys/strsubr.h>
41 41 #include <sys/strsun.h>
42 42 #include <sys/ddi.h>
43 43 #include <sys/sunddi.h>
44 44 #include <sys/note.h>
45 45 #include "sys/consdev.h"
46 46 #include <sys/kbd.h>
47 47 #include <sys/kbtrans.h>
48 48 #include "kb8042.h"
49 49
50 50 #include <sys/i8042.h>
51 51
52 52 #include "sys/kbio.h" /* for KIOCSLAYOUT */
53 53 #include "sys/stat.h"
54 54 #include "sys/reboot.h"
55 55 #include <sys/promif.h>
56 56 #include <sys/beep.h>
57 57 #include <sys/inttypes.h>
58 58 #include <sys/policy.h>
59 59
60 60 /*
61 61 * For any keyboard, there is a unique code describing the position
62 62 * of the key on a keyboard. We refer to the code as "station number".
63 63 * The following table is used to map the station numbers from ps2
64 64 * AT/XT keyboards to that of a USB one.
65 65 *
66 66 * A mapping was added for entry K8042_STOP, to map to USB key code 120 (which
67 67 * maps to the STOP key) when in KB_USB mode, and maps to a HOLE entry
68 68 * when in KB_PC mode. Note that this does not need to be made conditional
69 69 * on the architecture for which this module is complied because there are no
70 70 * keys that map to K8042_STOP on non-SPARC platforms.
71 71 */
72 72 static kbtrans_key_t keytab_pc2usb[KBTRANS_KEYNUMS_MAX] = {
73 73 /* 0 */ 0, 53, 30, 31, 32, 33, 34, 35,
74 74 /* 8 */ 36, 37, 38, 39, 45, 46, 137, 42,
75 75 /* 16 */ 43, 20, 26, 8, 21, 23, 28, 24,
76 76 /* 24 */ 12, 18, 19, 47, 48, 49, 57, 4,
77 77 /* 32 */ 22, 7, 9, 10, 11, 13, 14, 15,
78 78 /* 40 */ 51, 52, 50, 40, 225, 100, 29, 27,
79 79 /* 48 */ 6, 25, 5, 17, 16, 54, 55, 56,
80 80 /* 56 */ 135, 229, 224, 227, 226, 44, 230, 231,
81 81 /* 64 */ 228, 101, 0, 0, 0, 0, 0, 0,
82 82 /* 72 */ 0, 0, 0, 73, 76, 0, 0, 80,
83 83 /* 80 */ 74, 77, 0, 82, 81, 75, 78, 0,
84 84 /* 88 */ 0, 79, 83, 95, 92, 89, 0, 84,
85 85 /* 96 */ 96, 93, 90, 98, 85, 97, 94, 91,
86 86 /* 104 */ 99, 86, 87, 133, 88, 0, 41, 0,
87 87 /* 112 */ 58, 59, 60, 61, 62, 63, 64, 65,
88 88 /* 120 */ 66, 67, 68, 69, 70, 71, 72, 0,
89 89 /* 128 */ 0, 0, 0, 139, 138, 136, 0, 0,
90 90 /* 136 */ 0, 0, 0, 0, 0, 0, 0, 0,
91 91 /* 144 */ 0, 0, 0, 0, 0, 0, 0, 0,
92 92 /* 152 */ 0, 0, 0, 0, 0, 0, 0, 0,
93 93 /* 160 */ 120, 0, 0, 0, 0, 0, 0, 0,
94 94 /* 168 */ 0, 0, 0, 0, 0, 0, 0, 0,
95 95 /* 176 */ 0, 0, 0, 0, 0, 0, 0, 0,
96 96 /* 184 */ 0, 0, 0, 0, 0, 0, 0, 0,
97 97 /* 192 */ 0, 0, 0, 0, 0, 0, 0, 0,
98 98 /* 200 */ 0, 0, 0, 0, 0, 0, 0, 0,
99 99 /* 208 */ 0, 0, 0, 0, 0, 0, 0, 0,
100 100 /* 216 */ 0, 0, 0, 0, 0, 0, 0, 0,
101 101 /* 224 */ 0, 0, 0, 0, 0, 0, 0, 0,
102 102 /* 232 */ 0, 128, 129, 127, 0, 0, 0, 0,
103 103 /* 240 */ 0, 0, 0, 0, 0, 0, 0, 0,
104 104 /* 248 */ 0, 0, 0, 0
105 105 };
106 106
107 107 #ifdef __sparc
108 108 #define USECS_PER_WAIT 100
109 109 #define MAX_WAIT_USECS 100000 /* in usecs = 100 ms */
110 110 #define MIN_DELAY_USECS USECS_PER_WAIT
111 111
112 112 boolean_t kb8042_warn_unknown_scanset = B_TRUE;
113 113 int kb8042_default_scanset = 2;
114 114
115 115 #endif
116 116
117 117 #define MAX_KB8042_WAIT_MAX_MS 500 /* 500ms total */
118 118 #define MAX_KB8042_RETRIES 5
119 119
120 120 enum state_return { STATE_NORMAL, STATE_INTERNAL };
121 121
122 122 static void kb8042_init(struct kb8042 *kb8042, boolean_t from_resume);
123 123 static uint_t kb8042_intr(caddr_t arg);
124 124 static void kb8042_wait_poweron(struct kb8042 *kb8042);
125 125 static void kb8042_send_to_keyboard(struct kb8042 *, int, boolean_t);
126 126 static int kb8042_xlate_leds(int);
127 127 static void kb8042_setled(struct kb8042 *, int led_state, boolean_t polled);
128 128 static void kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state);
129 129 static void kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state);
130 130 static boolean_t kb8042_polled_keycheck(
131 131 struct kbtrans_hardware *hw, int *key,
132 132 enum keystate *state);
133 133 static void kb8042_get_initial_leds(struct kb8042 *, int *, int *);
134 134 static boolean_t kb8042_autorepeat_detect(struct kb8042 *kb8042, int key_pos,
135 135 enum keystate state);
136 136 static void kb8042_type4_cmd(struct kb8042 *kb8042, int cmd);
137 137 static void kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *, mblk_t *);
138 138 static void kb8042_iocdatamsg(queue_t *, mblk_t *);
139 139 static void kb8042_process_key(struct kb8042 *, kbtrans_key_t, enum keystate);
140 140 static int kb8042_polled_ischar(cons_polledio_arg_t arg);
141 141 static int kb8042_polled_getchar(cons_polledio_arg_t arg);
142 142 static void kb8042_cleanup(struct kb8042 *kb8042);
143 143
144 144 static struct kbtrans_callbacks kb8042_callbacks = {
145 145 kb8042_streams_setled,
146 146 kb8042_polled_setled,
147 147 kb8042_polled_keycheck,
148 148 };
149 149
150 150 extern struct keyboard keyindex_pc;
151 151
152 152 #define DRIVER_NAME(dip) ddi_driver_name(dip)
153 153
154 154 static char module_name[] = "kb8042";
155 155
156 156 static int kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag,
157 157 cred_t *credp);
158 158 static int kb8042_close(queue_t *qp, int flag, cred_t *credp);
159 159 static int kb8042_wsrv();
160 160
161 161 struct module_info kb8042_sinfo = {
162 162 42, /* Module ID */
163 163 module_name,
164 164 0, 32, /* Minimum and maximum packet sizes */
165 165 256, 128 /* High and low water marks */
166 166 };
167 167
168 168 static struct qinit kb8042_rinit = {
↓ open down ↓ |
168 lines elided |
↑ open up ↑ |
169 169 NULL, NULL, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
170 170 };
171 171
172 172 static struct qinit kb8042_winit = {
173 173 putq, kb8042_wsrv, kb8042_open, kb8042_close, NULL, &kb8042_sinfo, NULL
174 174 };
175 175
176 176 struct streamtab
177 177 kb8042_str_info = { &kb8042_rinit, &kb8042_winit, NULL, NULL };
178 178
179 -struct kb8042 Kdws = {0};
179 +struct kb8042 Kdws = {{{NULL}}};
180 180 static dev_info_t *kb8042_dip = NULL;
181 181
182 182 static int kb8042_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
183 183 void **result);
184 184 static int kb8042_attach(dev_info_t *, ddi_attach_cmd_t);
185 185 static int kb8042_detach(dev_info_t *, ddi_detach_cmd_t);
186 186
187 187 static struct cb_ops cb_kb8042_ops = {
188 188 nulldev, /* cb_open */
189 189 nulldev, /* cb_close */
190 190 nodev, /* cb_strategy */
191 191 nodev, /* cb_print */
192 192 nodev, /* cb_dump */
193 193 nodev, /* cb_read */
194 194 nodev, /* cb_write */
195 195 nodev, /* cb_ioctl */
196 196 nodev, /* cb_devmap */
197 197 nodev, /* cb_mmap */
198 198 nodev, /* cb_segmap */
199 199 nochpoll, /* cb_chpoll */
200 200 ddi_prop_op, /* cb_prop_op */
201 201 &kb8042_str_info, /* cb_stream */
202 202 D_MP
203 203 };
204 204
205 205 struct dev_ops kb8042_ops = {
206 206 DEVO_REV, /* devo_rev */
207 207 0, /* devo_refcnt */
208 208 kb8042_getinfo, /* devo_getinfo */
209 209 nulldev, /* devo_identify */
210 210 nulldev, /* devo_probe */
211 211 kb8042_attach, /* devo_attach */
212 212 kb8042_detach, /* devo_detach */
213 213 nodev, /* devo_reset */
214 214 &cb_kb8042_ops, /* devo_cb_ops */
215 215 (struct bus_ops *)NULL, /* devo_bus_ops */
216 216 NULL, /* devo_power */
217 217 ddi_quiesce_not_needed, /* devo_quiesce */
218 218 };
219 219
220 220
221 221 /*
222 222 * This is the loadable module wrapper.
223 223 */
224 224 #include <sys/modctl.h>
225 225
226 226 /*
↓ open down ↓ |
37 lines elided |
↑ open up ↑ |
227 227 * Module linkage information for the kernel.
228 228 */
229 229 static struct modldrv modldrv = {
230 230 &mod_driverops, /* Type of module. This one is a driver */
231 231 "PS/2 keyboard driver",
232 232 &kb8042_ops, /* driver ops */
233 233 };
234 234
235 235 static struct modlinkage modlinkage = {
236 236 MODREV_1,
237 - (void *) &modldrv,
238 - NULL
237 + { (void *) &modldrv, NULL }
239 238 };
240 239
241 240 int
242 241 _init(void)
243 242 {
244 243 int rv;
245 244
246 245 rv = mod_install(&modlinkage);
247 246 return (rv);
248 247 }
249 248
250 249
251 250 int
252 251 _fini(void)
253 252 {
254 253 return (mod_remove(&modlinkage));
255 254 }
256 255
257 256 int
258 257 _info(struct modinfo *modinfop)
259 258 {
260 259 return (mod_info(&modlinkage, modinfop));
261 260 }
262 261
263 262 #ifdef __sparc
264 263 static boolean_t
265 264 kb8042_is_input_avail(struct kb8042 *kb8042, int timeout_usec, boolean_t polled)
266 265 {
267 266 int i;
268 267 int port = (polled == B_TRUE) ? I8042_POLL_INPUT_AVAIL :
269 268 I8042_INT_INPUT_AVAIL;
270 269 int reps = timeout_usec / USECS_PER_WAIT;
271 270
272 271 for (i = 0; i < reps; i++) {
273 272 if (ddi_get8(kb8042->handle, kb8042->addr + port) != 0)
274 273 return (B_TRUE);
275 274
276 275 if (i < (reps - 1))
277 276 drv_usecwait(USECS_PER_WAIT);
278 277 }
279 278 return (B_FALSE);
280 279 }
281 280
282 281 static void
283 282 kb8042_clear_input_buffer(struct kb8042 *kb8042, boolean_t polled)
284 283 {
285 284 int port = (polled == B_TRUE) ? I8042_POLL_INPUT_DATA :
286 285 I8042_INT_INPUT_DATA;
287 286
288 287 while (kb8042_is_input_avail(kb8042, MIN_DELAY_USECS, polled)) {
289 288 (void) ddi_get8(kb8042->handle, kb8042->addr + port);
290 289 }
291 290 }
292 291
293 292 /*
294 293 * kb8042_send_and_expect does all its I/O via polling interfaces
295 294 */
296 295 static boolean_t
297 296 kb8042_send_and_expect(struct kb8042 *kb8042, uint8_t send, uint8_t expect,
298 297 int timeout, int *error, uint8_t *got)
299 298 {
300 299 uint8_t datab;
301 300 int err;
302 301 boolean_t rval;
303 302
304 303 ddi_put8(kb8042->handle,
305 304 kb8042->addr + I8042_POLL_OUTPUT_DATA, send);
306 305
307 306 if (kb8042_is_input_avail(kb8042, timeout, B_TRUE)) {
308 307 err = 0;
309 308 datab = ddi_get8(kb8042->handle,
310 309 kb8042->addr + I8042_POLL_INPUT_DATA);
311 310 rval = ((datab == expect) ? B_TRUE : B_FALSE);
312 311 } else {
313 312 err = EAGAIN;
314 313 rval = B_FALSE;
315 314 }
316 315
317 316 if (error != NULL)
318 317 *error = err;
319 318 if (got != NULL)
320 319 *got = datab;
321 320 return (rval);
322 321 }
323 322
324 323 static const char *
325 324 kb8042_error_string(int errcode)
326 325 {
327 326 switch (errcode) {
328 327 case EAGAIN:
329 328 return ("Timed out");
330 329 default:
331 330 return ("Unknown error");
332 331 }
333 332 }
334 333
335 334 /*
336 335 * kb8042_read_scanset works properly because it is called before ddi_add_intr
337 336 * (if it is called after ddi_add_intr, i8042_intr would call kb8042_intr
338 337 * instead of just storing the data that comes in from the keyboard, which
339 338 * would prevent the code here from getting it.)
340 339 */
341 340 static int
342 341 kb8042_read_scanset(struct kb8042 *kb8042)
343 342 {
344 343 int scanset = -1;
345 344 int err;
346 345 uint8_t got;
347 346
348 347 kb8042_clear_input_buffer(kb8042, B_TRUE);
349 348
350 349 /*
351 350 * Send a "change scan code set" command to the keyboard.
352 351 * It should respond with an ACK.
353 352 */
354 353 if (kb8042_send_and_expect(kb8042, KB_SET_SCAN, KB_ACK, MAX_WAIT_USECS,
355 354 &err, &got) != B_TRUE) {
356 355 goto fail_read_scanset;
357 356 }
358 357
359 358 /*
360 359 * Send a 0. The keyboard should ACK the 0, then it should send the
361 360 * scan code set in use.
362 361 */
363 362 if (kb8042_send_and_expect(kb8042, 0, KB_ACK, MAX_WAIT_USECS, &err,
364 363 &got) != B_TRUE) {
365 364 goto fail_read_scanset;
366 365 }
367 366
368 367 /*
369 368 * The next input byte from the keyboard should be the scan code
370 369 * set in use, though some keyboards like to send a few more acks
371 370 * just for fun, so blow past those to get the keyboard scan code.
372 371 */
373 372 while (kb8042_is_input_avail(kb8042, MAX_WAIT_USECS, B_TRUE) &&
374 373 (scanset = ddi_get8(kb8042->handle,
375 374 kb8042->addr + I8042_POLL_INPUT_DATA)) == KB_ACK)
376 375 ;
377 376
378 377 #ifdef DEBUG
379 378 cmn_err(CE_NOTE, "!Scan code set from keyboard is `%d'.",
380 379 scanset);
381 380 #endif
382 381
383 382 return (scanset);
384 383
385 384 fail_read_scanset:
386 385 #ifdef DEBUG
387 386 if (err == 0)
388 387 cmn_err(CE_NOTE, "Could not read current scan set from "
389 388 "keyboard: %s. (Expected 0x%x, but got 0x%x).",
390 389 kb8042_error_string(err), KB_ACK, got);
391 390 else
392 391 cmn_err(CE_NOTE, "Could not read current scan set from "
393 392 "keyboard: %s.", kb8042_error_string(err));
394 393 #endif
395 394 return (-1);
396 395 }
397 396 #endif
398 397
399 398 static int
400 399 kb8042_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
401 400 {
402 401 int rc;
403 402 int scanset;
404 403 int leds;
405 404
406 405 struct kb8042 *kb8042 = &Kdws;
407 406 static ddi_device_acc_attr_t attr = {
408 407 DDI_DEVICE_ATTR_V0,
409 408 DDI_NEVERSWAP_ACC,
410 409 DDI_STRICTORDER_ACC,
411 410 };
412 411
413 412 switch (cmd) {
414 413 case DDI_RESUME:
415 414 leds = kb8042->leds.commanded;
416 415 kb8042->w_init = 0;
417 416 kb8042_init(kb8042, B_TRUE);
418 417 kb8042_setled(kb8042, leds, B_FALSE);
419 418 mutex_enter(&kb8042->w_hw_mutex);
420 419 kb8042->suspended = B_FALSE;
421 420 if (kb8042->w_qp != NULL) {
422 421 enableok(WR(kb8042->w_qp));
423 422 qenable(WR(kb8042->w_qp));
424 423 }
425 424 cv_broadcast(&kb8042->suspend_cv);
426 425 mutex_exit(&kb8042->w_hw_mutex);
427 426 return (DDI_SUCCESS);
428 427
429 428 case DDI_ATTACH:
430 429 if (kb8042_dip != NULL)
431 430 return (DDI_FAILURE);
432 431 /* The rest of the function is for attach */
433 432 break;
434 433
435 434 default:
436 435 return (DDI_FAILURE);
437 436 }
438 437
439 438 kb8042->debugger.mod1 = 58; /* Left Ctrl */
440 439 kb8042->debugger.mod2 = 60; /* Left Alt */
441 440 kb8042->debugger.trigger = 33; /* D */
442 441 kb8042->debugger.mod1_down = B_FALSE;
443 442 kb8042->debugger.mod2_down = B_FALSE;
444 443 kb8042->debugger.enabled = B_FALSE;
445 444
446 445 kb8042_dip = devi;
447 446 kb8042->init_state = KB8042_UNINITIALIZED;
448 447
449 448 kb8042->polled_synthetic_release_pending = B_FALSE;
450 449
451 450 if (ddi_create_minor_node(devi, module_name, S_IFCHR, 0,
452 451 DDI_NT_KEYBOARD, 0) == DDI_FAILURE) {
453 452 goto failure;
454 453 }
455 454
456 455 kb8042->init_state |= KB8042_MINOR_NODE_CREATED;
457 456
458 457 rc = ddi_regs_map_setup(devi, 0, (caddr_t *)&kb8042->addr,
459 458 (offset_t)0, (offset_t)0, &attr, &kb8042->handle);
460 459 if (rc != DDI_SUCCESS) {
461 460 #ifdef DEBUG
462 461 cmn_err(CE_WARN, "kb8042_attach: can't map registers");
463 462 #endif
464 463 goto failure;
465 464 }
466 465
467 466 kb8042->init_state |= KB8042_REGS_MAPPED;
468 467
469 468 if (ddi_get_iblock_cookie(devi, 0, &kb8042->w_iblock) !=
470 469 DDI_SUCCESS) {
471 470 cmn_err(CE_WARN, "kb8042_attach: Can't get iblock cookie");
472 471 goto failure;
473 472 }
474 473
475 474 mutex_init(&kb8042->w_hw_mutex, NULL, MUTEX_DRIVER, kb8042->w_iblock);
476 475 cv_init(&kb8042->ops_cv, NULL, CV_DRIVER, NULL);
477 476 cv_init(&kb8042->suspend_cv, NULL, CV_DRIVER, NULL);
478 477 cv_init(&kb8042->cmd_cv, NULL, CV_DRIVER, NULL);
479 478 kb8042->init_state |= KB8042_HW_MUTEX_INITTED;
480 479
481 480 kb8042_init(kb8042, B_FALSE);
482 481
483 482 #ifdef __sparc
484 483 /* Detect the scan code set currently in use */
485 484 scanset = kb8042_read_scanset(kb8042);
486 485
487 486 if (scanset < 0 && kb8042_warn_unknown_scanset) {
488 487
489 488 cmn_err(CE_WARN, "Cannot determine keyboard scan code set ");
490 489 cmn_err(CE_CONT, "(is the keyboard plugged in?). ");
491 490 cmn_err(CE_CONT, "Defaulting to scan code set %d. If the "
492 491 "keyboard does not ", kb8042_default_scanset);
493 492 cmn_err(CE_CONT, "work properly, add "
494 493 "`set kb8042:kb8042_default_scanset=%d' to /etc/system ",
495 494 (kb8042_default_scanset == 1) ? 2 : 1);
496 495 cmn_err(CE_CONT, "(via network or with a USB keyboard) and "
497 496 "restart the system. If you ");
498 497 cmn_err(CE_CONT, "do not want to see this message in the "
499 498 "future, add ");
500 499 cmn_err(CE_CONT, "`set kb8042:kb8042_warn_unknown_scanset=0' "
501 500 "to /etc/system.\n");
502 501
503 502 /* Use the default scan code set. */
504 503 scanset = kb8042_default_scanset;
505 504 }
506 505 #else
507 506 /* x86 systems use scan code set 1 -- no detection required */
508 507 scanset = 1;
509 508 #endif
510 509 if (KeyboardConvertScan_init(kb8042, scanset) != DDI_SUCCESS) {
511 510 cmn_err(CE_WARN, "Cannot initialize keyboard scan converter: "
512 511 "Unknown scan code set `%d'.", scanset);
513 512 /* Scan code set is not supported */
514 513 goto failure;
515 514 }
516 515
517 516 /*
518 517 * Turn on interrupts...
519 518 */
520 519 if (ddi_add_intr(devi, 0,
521 520 &kb8042->w_iblock, (ddi_idevice_cookie_t *)NULL,
522 521 kb8042_intr, (caddr_t)kb8042) != DDI_SUCCESS) {
523 522 cmn_err(CE_WARN, "kb8042_attach: cannot add interrupt");
524 523 goto failure;
525 524 }
526 525
527 526 kb8042->init_state |= KB8042_INTR_ADDED;
528 527
529 528 ddi_report_dev(devi);
530 529
531 530 #ifdef DEBUG
532 531 cmn_err(CE_CONT, "?%s instance #%d READY\n",
533 532 DRIVER_NAME(devi), ddi_get_instance(devi));
534 533 #endif
535 534
536 535 return (DDI_SUCCESS);
537 536
538 537 failure:
539 538 kb8042_cleanup(kb8042);
540 539 return (DDI_FAILURE);
541 540 }
542 541
543 542 static int
544 543 kb8042_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
545 544 {
546 545 struct kb8042 *kb8042 = &Kdws;
547 546
548 547 switch (cmd) {
549 548 case DDI_SUSPEND:
550 549 mutex_enter(&kb8042->w_hw_mutex);
551 550 ASSERT(kb8042->ops >= 0);
552 551 while (kb8042->ops > 0)
553 552 cv_wait(&kb8042->ops_cv, &kb8042->w_hw_mutex);
554 553 kb8042->suspended = B_TRUE;
555 554 mutex_exit(&kb8042->w_hw_mutex);
556 555 return (DDI_SUCCESS);
557 556
558 557 case DDI_DETACH:
559 558 /* If someone has a stream open, fail to detach */
560 559 if (kb8042->w_qp != NULL)
561 560 return (DDI_FAILURE);
562 561
563 562 ASSERT(kb8042_dip == dip);
564 563
565 564 kb8042_cleanup(kb8042);
566 565
567 566 return (DDI_SUCCESS);
568 567
569 568 default:
570 569 return (DDI_FAILURE);
571 570 }
572 571 }
573 572
574 573 /*ARGSUSED*/
575 574 static int
576 575 kb8042_getinfo(
577 576 dev_info_t *dip,
578 577 ddi_info_cmd_t infocmd,
579 578 void *arg,
580 579 void **result)
581 580 {
582 581 register int error;
583 582
584 583 switch (infocmd) {
585 584 case DDI_INFO_DEVT2DEVINFO:
586 585 if (kb8042_dip == NULL) {
587 586 error = DDI_FAILURE;
588 587 } else {
589 588 *result = (void *) kb8042_dip;
590 589 error = DDI_SUCCESS;
591 590 }
592 591 break;
593 592 case DDI_INFO_DEVT2INSTANCE:
594 593 *result = (void *)0;
595 594 error = DDI_SUCCESS;
596 595 break;
597 596 default:
598 597 error = DDI_FAILURE;
599 598 break;
600 599 }
601 600 return (error);
602 601 }
603 602
604 603 static void
605 604 kb8042_cleanup(struct kb8042 *kb8042)
606 605 {
607 606 ASSERT(kb8042_dip != NULL);
608 607
609 608 if (kb8042->init_state & KB8042_INTR_ADDED)
610 609 ddi_remove_intr(kb8042_dip, 0, kb8042->w_iblock);
611 610
612 611 if (kb8042->init_state & KB8042_HW_MUTEX_INITTED) {
613 612 cv_destroy(&kb8042->cmd_cv);
614 613 cv_destroy(&kb8042->suspend_cv);
615 614 cv_destroy(&kb8042->ops_cv);
616 615 mutex_destroy(&kb8042->w_hw_mutex);
617 616 }
618 617
619 618 if (kb8042->init_state & KB8042_REGS_MAPPED)
620 619 ddi_regs_map_free(&kb8042->handle);
621 620
622 621 if (kb8042->init_state & KB8042_MINOR_NODE_CREATED)
623 622 ddi_remove_minor_node(kb8042_dip, NULL);
624 623
625 624 kb8042->init_state = KB8042_UNINITIALIZED;
626 625 kb8042_dip = NULL;
627 626 }
628 627
629 628 static void
630 629 kb8042_init(struct kb8042 *kb8042, boolean_t from_resume)
631 630 {
632 631 if (kb8042->w_init)
633 632 return;
634 633
635 634 if (!from_resume) {
636 635 kb8042->w_kblayout = 0; /* Default to US */
637 636 kb8042->w_qp = (queue_t *)NULL;
638 637 kb8042->simulated_kbd_type = KB_PC;
639 638 kb8042->leds.commanded = -1; /* Unknown initial state */
640 639 kb8042->leds.desired = -1; /* Unknown initial state */
641 640 }
642 641
643 642 kb8042_wait_poweron(kb8042);
644 643
645 644 kb8042->kb_old_key_pos = 0;
646 645
647 646 /*
648 647 * Explicitly grab and release the 8042 lock outside of
649 648 * kb8042_send_to_keyboard, because this is the only situation
650 649 * where a polling interface is used with locking required.
651 650 */
652 651 (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_LOCK);
653 652 /* Set up the command state machine and start it running. */
654 653 kb8042_send_to_keyboard(kb8042, KB_ENABLE, B_TRUE);
655 654 (void) ddi_get8(kb8042->handle, kb8042->addr + I8042_UNLOCK);
656 655
657 656 kb8042->w_init++;
658 657
659 658 (void) drv_setparm(SYSRINT, 1); /* reset keyboard interrupts */
660 659 }
661 660
662 661 /*ARGSUSED2*/
663 662 static int
664 663 kb8042_open(queue_t *qp, dev_t *devp, int flag, int sflag, cred_t *credp)
665 664 {
666 665 struct kb8042 *kb8042;
667 666 int err = 0;
668 667 int initial_leds;
669 668 int initial_led_mask;
670 669
671 670 kb8042 = &Kdws;
672 671
673 672 mutex_enter(&kb8042->w_hw_mutex);
674 673 if (qp->q_ptr) {
675 674 kb8042->w_dev = *devp;
676 675 mutex_exit(&kb8042->w_hw_mutex);
677 676 return (0);
678 677 }
679 678
680 679 if (secpolicy_console(credp) != 0) {
681 680 mutex_exit(&kb8042->w_hw_mutex);
682 681 return (EPERM);
683 682 }
684 683
685 684 while (kb8042->suspended) {
686 685 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
687 686 0) {
688 687 mutex_exit(&kb8042->w_hw_mutex);
689 688 return (EINTR);
690 689 }
691 690 }
692 691
693 692 kb8042->w_dev = *devp;
694 693 qp->q_ptr = (caddr_t)kb8042;
695 694 WR(qp)->q_ptr = qp->q_ptr;
696 695 if (!kb8042->w_qp)
697 696 kb8042->w_qp = qp;
698 697
699 698 ASSERT(kb8042->ops >= 0);
700 699 kb8042->ops++;
701 700 mutex_exit(&kb8042->w_hw_mutex);
702 701
703 702 kb8042_get_initial_leds(kb8042, &initial_leds, &initial_led_mask);
704 703 err = kbtrans_streams_init(qp, sflag,
705 704 (struct kbtrans_hardware *)kb8042, &kb8042_callbacks,
706 705 &kb8042->hw_kbtrans,
707 706 initial_leds, initial_led_mask);
708 707 if (err != 0)
709 708 goto out;
710 709
711 710 kbtrans_streams_set_keyboard(kb8042->hw_kbtrans, KB_PC, &keyindex_pc);
712 711
713 712 kb8042->polledio.cons_polledio_version = CONSPOLLEDIO_V1;
714 713 kb8042->polledio.cons_polledio_argument =
715 714 (cons_polledio_arg_t)kb8042;
716 715 kb8042->polledio.cons_polledio_putchar = NULL;
717 716 kb8042->polledio.cons_polledio_getchar =
718 717 (int (*)(cons_polledio_arg_t))kb8042_polled_getchar;
719 718 kb8042->polledio.cons_polledio_ischar =
720 719 (boolean_t (*)(cons_polledio_arg_t))kb8042_polled_ischar;
721 720 kb8042->polledio.cons_polledio_enter = NULL;
722 721 kb8042->polledio.cons_polledio_exit = NULL;
723 722 kb8042->polledio.cons_polledio_setled =
724 723 (void (*)(cons_polledio_arg_t, int))kb8042_polled_setled;
725 724 kb8042->polledio.cons_polledio_keycheck =
726 725 (boolean_t (*)(cons_polledio_arg_t, int *,
727 726 enum keystate *))kb8042_polled_keycheck;
728 727
729 728 qprocson(qp);
730 729
731 730 kbtrans_streams_enable(kb8042->hw_kbtrans);
732 731
733 732 out:
734 733 mutex_enter(&kb8042->w_hw_mutex);
735 734 ASSERT(kb8042->ops > 0);
736 735 kb8042->ops--;
737 736 if (kb8042->ops == 0)
738 737 cv_broadcast(&kb8042->ops_cv);
739 738 mutex_exit(&kb8042->w_hw_mutex);
740 739
741 740 return (err);
742 741 }
743 742
744 743 /*ARGSUSED1*/
745 744 static int
746 745 kb8042_close(queue_t *qp, int flag, cred_t *credp)
747 746 {
748 747 struct kb8042 *kb8042;
749 748
750 749 /* If a beep is in progress, stop that */
751 750 (void) beeper_off();
752 751
753 752 kb8042 = (struct kb8042 *)qp->q_ptr;
754 753
755 754 mutex_enter(&kb8042->w_hw_mutex);
756 755 while (kb8042->suspended) {
757 756 if (cv_wait_sig(&kb8042->suspend_cv, &kb8042->w_hw_mutex) ==
758 757 0) {
759 758 mutex_exit(&kb8042->w_hw_mutex);
760 759 return (EINTR);
761 760 }
762 761 }
763 762
764 763 ASSERT(kb8042->ops >= 0);
765 764 kb8042->ops++;
766 765 mutex_exit(&kb8042->w_hw_mutex);
767 766
768 767 (void) kbtrans_streams_fini(kb8042->hw_kbtrans);
769 768
770 769 kb8042->w_qp = (queue_t *)NULL;
771 770 qprocsoff(qp);
772 771
773 772 mutex_enter(&kb8042->w_hw_mutex);
774 773 ASSERT(kb8042->ops > 0);
775 774 kb8042->ops--;
776 775 if (kb8042->ops == 0)
777 776 cv_broadcast(&kb8042->ops_cv);
778 777 mutex_exit(&kb8042->w_hw_mutex);
779 778
780 779 return (0);
781 780 }
782 781
783 782 static int
784 783 kb8042_wsrv(queue_t *qp)
785 784 {
786 785 struct kb8042 *kb8042;
787 786
788 787 mblk_t *mp;
789 788 boolean_t suspended;
790 789
791 790 kb8042 = (struct kb8042 *)qp->q_ptr;
792 791
793 792 mutex_enter(&kb8042->w_hw_mutex);
794 793 suspended = kb8042->suspended;
795 794 ASSERT(kb8042->ops >= 0);
796 795 if (!suspended)
797 796 kb8042->ops++;
798 797 mutex_exit(&kb8042->w_hw_mutex);
799 798
800 799 #ifdef NO_KB_DEBUG
801 800 while (!suspended && (mp = getq(qp)) != NULL) {
802 801 #else
803 802 /*
804 803 * Not taking keyboard input while suspending can make debugging
805 804 * difficult. However, we still do the ops counting so that we
806 805 * don't suspend at a bad time.
807 806 */
808 807 while ((mp = getq(qp))) {
809 808 #endif
810 809 switch (kbtrans_streams_message(kb8042->hw_kbtrans, mp)) {
811 810 case KBTRANS_MESSAGE_HANDLED:
812 811 continue;
813 812 case KBTRANS_MESSAGE_NOT_HANDLED:
814 813 break;
815 814 }
816 815 switch (mp->b_datap->db_type) {
817 816 case M_IOCTL:
818 817 kb8042_ioctlmsg(kb8042, qp, mp);
819 818 continue;
820 819 case M_IOCDATA:
821 820 kb8042_iocdatamsg(qp, mp);
822 821 continue;
823 822 case M_DELAY:
824 823 case M_STARTI:
825 824 case M_STOPI:
826 825 case M_READ: /* ignore, no buffered data */
827 826 freemsg(mp);
828 827 continue;
829 828 case M_FLUSH:
830 829 *mp->b_rptr &= ~FLUSHW;
831 830 if (*mp->b_rptr & FLUSHR)
832 831 qreply(qp, mp);
833 832 else
834 833 freemsg(mp);
835 834 continue;
836 835 default:
837 836 cmn_err(CE_NOTE, "kb8042_wsrv: bad msg %x",
838 837 mp->b_datap->db_type);
839 838 freemsg(mp);
840 839 continue;
841 840 }
842 841 }
843 842
844 843 mutex_enter(&kb8042->w_hw_mutex);
845 844 if (!suspended) {
846 845 ASSERT(kb8042->ops > 0);
847 846 kb8042->ops--;
848 847 if (kb8042->ops == 0)
849 848 cv_broadcast(&kb8042->ops_cv);
850 849 }
851 850 mutex_exit(&kb8042->w_hw_mutex);
852 851
853 852 return (0);
854 853 }
855 854
856 855 static void
857 856 kb8042_ioctlmsg(struct kb8042 *kb8042, queue_t *qp, mblk_t *mp)
858 857 {
859 858 struct iocblk *iocp;
860 859 mblk_t *datap;
861 860 int error;
862 861 int tmp;
863 862 int cycles;
864 863 int frequency;
865 864 int msecs;
866 865
867 866 iocp = (struct iocblk *)mp->b_rptr;
868 867
869 868 switch (iocp->ioc_cmd) {
870 869
871 870 case CONSOPENPOLLEDIO:
872 871 error = miocpullup(mp, sizeof (struct cons_polledio *));
873 872 if (error != 0) {
874 873 miocnak(qp, mp, 0, error);
875 874 return;
876 875 }
877 876
878 877 /*
879 878 * We are given an appropriate-sized data block,
880 879 * and return a pointer to our structure in it.
881 880 */
882 881 *(struct cons_polledio **)mp->b_cont->b_rptr =
883 882 &kb8042->polledio;
884 883 mp->b_datap->db_type = M_IOCACK;
885 884 iocp->ioc_error = 0;
886 885 qreply(qp, mp);
887 886 break;
888 887
889 888 case CONSCLOSEPOLLEDIO:
890 889 miocack(qp, mp, 0, 0);
891 890 break;
892 891
893 892 case CONSSETABORTENABLE:
894 893 if (iocp->ioc_count != TRANSPARENT) {
895 894 miocnak(qp, mp, 0, EINVAL);
896 895 return;
897 896 }
898 897
899 898 kb8042->debugger.enabled = *(intptr_t *)mp->b_cont->b_rptr;
900 899 miocack(qp, mp, 0, 0);
901 900 break;
902 901
903 902 /*
904 903 * Valid only in TR_UNTRANS_MODE mode.
905 904 */
906 905 case CONSSETKBDTYPE:
907 906 error = miocpullup(mp, sizeof (int));
908 907 if (error != 0) {
909 908 miocnak(qp, mp, 0, error);
910 909 return;
911 910 }
912 911 tmp = *(int *)mp->b_cont->b_rptr;
913 912 if (tmp != KB_PC && tmp != KB_USB) {
914 913 miocnak(qp, mp, 0, EINVAL);
915 914 break;
916 915 }
917 916 kb8042->simulated_kbd_type = tmp;
918 917 miocack(qp, mp, 0, 0);
919 918 break;
920 919
921 920 case KIOCLAYOUT:
922 921 if (kb8042->w_kblayout == -1) {
923 922 miocnak(qp, mp, 0, EINVAL);
924 923 return;
925 924 }
926 925
927 926 if ((datap = allocb(sizeof (int), BPRI_HI)) == NULL) {
928 927 miocnak(qp, mp, 0, ENOMEM);
929 928 return;
930 929 }
931 930
932 931 if (kb8042->simulated_kbd_type == KB_USB)
933 932 *(int *)datap->b_wptr = KBTRANS_USBKB_DEFAULT_LAYOUT;
934 933 else
935 934 *(int *)datap->b_wptr = kb8042->w_kblayout;
936 935
937 936 datap->b_wptr += sizeof (int);
938 937 if (mp->b_cont)
939 938 freemsg(mp->b_cont);
940 939 mp->b_cont = datap;
941 940 iocp->ioc_count = sizeof (int);
942 941 mp->b_datap->db_type = M_IOCACK;
943 942 iocp->ioc_error = 0;
944 943 qreply(qp, mp);
945 944 break;
946 945
947 946 case KIOCSLAYOUT:
948 947 if (iocp->ioc_count != TRANSPARENT) {
949 948 miocnak(qp, mp, 0, EINVAL);
950 949 return;
951 950 }
952 951
953 952 kb8042->w_kblayout = *(intptr_t *)mp->b_cont->b_rptr;
954 953 miocack(qp, mp, 0, 0);
955 954 break;
956 955
957 956 case KIOCCMD:
958 957 error = miocpullup(mp, sizeof (int));
959 958 if (error != 0) {
960 959 miocnak(qp, mp, 0, error);
961 960 return;
962 961 }
963 962
964 963 kb8042_type4_cmd(kb8042, *(int *)mp->b_cont->b_rptr);
965 964 miocack(qp, mp, 0, 0);
966 965 break;
967 966
968 967 case KIOCMKTONE:
969 968 if (iocp->ioc_count != TRANSPARENT) {
970 969 miocnak(qp, mp, 0, EINVAL);
971 970 return;
972 971 }
973 972
974 973 tmp = (int)(*(intptr_t *)mp->b_cont->b_rptr);
975 974 cycles = tmp & 0xffff;
976 975 msecs = (tmp >> 16) & 0xffff;
977 976
978 977 if (cycles == 0)
979 978 frequency = UINT16_MAX;
980 979 else if (cycles == UINT16_MAX)
981 980 frequency = 0;
982 981 else {
983 982 frequency = (PIT_HZ + cycles / 2) / cycles;
984 983 if (frequency > UINT16_MAX)
985 984 frequency = UINT16_MAX;
986 985 }
987 986
988 987 error = beep_mktone(frequency, msecs);
989 988 if (error != 0)
990 989 miocnak(qp, mp, 0, error);
991 990 else
992 991 miocack(qp, mp, 0, 0);
993 992 break;
994 993
995 994 default:
996 995 #ifdef DEBUG1
997 996 cmn_err(CE_NOTE, "!kb8042_ioctlmsg %x", iocp->ioc_cmd);
998 997 #endif
999 998 miocnak(qp, mp, 0, EINVAL);
1000 999 return;
1001 1000 }
1002 1001 }
1003 1002
1004 1003 /*
1005 1004 * Process a byte received from the keyboard
1006 1005 */
1007 1006 static void
1008 1007 kb8042_received_byte(
1009 1008 struct kb8042 *kb8042,
1010 1009 int scancode) /* raw scan code */
1011 1010 {
1012 1011 boolean_t legit; /* is this a legit key pos'n? */
1013 1012 int key_pos = -1;
1014 1013 enum keystate state;
1015 1014 boolean_t synthetic_release_needed;
1016 1015
1017 1016 /*
1018 1017 * Intercept ACK and RESEND and signal the condition that
1019 1018 * kb8042_send_and_wait is waiting for.
1020 1019 */
1021 1020 if (scancode == KB_ACK) {
1022 1021 mutex_enter(&kb8042->w_hw_mutex);
1023 1022 kb8042->acked = 1;
1024 1023 cv_signal(&kb8042->cmd_cv);
1025 1024 mutex_exit(&kb8042->w_hw_mutex);
1026 1025 return;
1027 1026 } else if (scancode == KB_RESEND) {
1028 1027 mutex_enter(&kb8042->w_hw_mutex);
1029 1028 kb8042->need_retry = 1;
1030 1029 cv_signal(&kb8042->cmd_cv);
1031 1030 mutex_exit(&kb8042->w_hw_mutex);
1032 1031 return;
1033 1032 }
1034 1033
1035 1034 if (!kb8042->w_init) /* can't do anything anyway */
1036 1035 return;
1037 1036
1038 1037 legit = KeyboardConvertScan(kb8042, scancode, &key_pos, &state,
1039 1038 &synthetic_release_needed);
1040 1039
1041 1040 if (legit == 0) {
1042 1041 /* Eaten by translation */
1043 1042 return;
1044 1043 }
1045 1044
1046 1045 /*
1047 1046 * Don't know if we want this permanently, but it seems interesting
1048 1047 * for the moment.
1049 1048 */
1050 1049 if (key_pos == kb8042->debugger.mod1) {
1051 1050 kb8042->debugger.mod1_down = (state == KEY_PRESSED);
1052 1051 }
1053 1052 if (key_pos == kb8042->debugger.mod2) {
1054 1053 kb8042->debugger.mod2_down = (state == KEY_PRESSED);
1055 1054 }
1056 1055 if (kb8042->debugger.enabled &&
1057 1056 key_pos == kb8042->debugger.trigger &&
1058 1057 kb8042->debugger.mod1_down &&
1059 1058 kb8042->debugger.mod2_down) {
1060 1059 /*
1061 1060 * Require new presses of the modifiers.
1062 1061 */
1063 1062 kb8042->debugger.mod1_down = B_FALSE;
1064 1063 kb8042->debugger.mod2_down = B_FALSE;
1065 1064 abort_sequence_enter(NULL);
1066 1065 return;
1067 1066 }
1068 1067
1069 1068 /*
1070 1069 * If there's no queue above us - as can happen if we've been
1071 1070 * attached but not opened - drop the keystroke.
1072 1071 * Note that we do this here instead of above so that
1073 1072 * Ctrl-Alt-D still works.
1074 1073 */
1075 1074 if (kb8042->w_qp == NULL) {
1076 1075 return;
1077 1076 }
1078 1077
1079 1078 /*
1080 1079 * This is to filter out auto repeat since it can't be
1081 1080 * turned off at the hardware. (Yeah, yeah, PS/2 keyboards
1082 1081 * can. Don't know whether they've taken over the world.
1083 1082 * Don't think so.)
1084 1083 */
1085 1084 if (kb8042_autorepeat_detect(kb8042, key_pos, state)) {
1086 1085 return;
1087 1086 }
1088 1087
1089 1088
1090 1089 kb8042_process_key(kb8042, key_pos, state);
1091 1090
1092 1091 /*
1093 1092 * This is a total hack. For some stupid reason, the two additional
1094 1093 * keys on Korean keyboards (Hangul and Hangul/Hanja) report press
1095 1094 * only. We synthesize a release immediately.
1096 1095 */
1097 1096 if (synthetic_release_needed) {
1098 1097 (void) kb8042_autorepeat_detect(kb8042, key_pos, KEY_RELEASED);
1099 1098 kb8042_process_key(kb8042, key_pos, state);
1100 1099 }
1101 1100 }
1102 1101
1103 1102
1104 1103 static void
1105 1104 kb8042_process_key(struct kb8042 *kb8042, kbtrans_key_t key_pos,
1106 1105 enum keystate state)
1107 1106 {
1108 1107 kbtrans_key_t key;
1109 1108
1110 1109 ASSERT(key_pos >= 0 && key_pos <= 255);
1111 1110 if (kb8042->simulated_kbd_type == KB_PC) {
1112 1111 kbtrans_streams_key(kb8042->hw_kbtrans, key_pos, state);
1113 1112 } else if (kb8042->simulated_kbd_type == KB_USB) {
1114 1113 key = keytab_pc2usb[key_pos];
1115 1114 if (key != 0) {
1116 1115 kbtrans_streams_key(kb8042->hw_kbtrans, key, state);
1117 1116 }
1118 1117 }
1119 1118 }
1120 1119
1121 1120 /*
1122 1121 * Called from interrupt handler when keyboard interrupt occurs.
1123 1122 */
1124 1123 static uint_t
1125 1124 kb8042_intr(caddr_t arg)
1126 1125 {
1127 1126 uchar_t scancode; /* raw scan code */
1128 1127 int rc;
1129 1128 struct kb8042 *kb8042 = (struct kb8042 *)arg;
1130 1129
1131 1130 rc = DDI_INTR_UNCLAIMED;
1132 1131
1133 1132 if (kb8042->init_state == KB8042_UNINITIALIZED)
1134 1133 return (DDI_INTR_UNCLAIMED);
1135 1134
1136 1135 /* don't care if drv_setparm succeeds */
1137 1136 (void) drv_setparm(SYSRINT, 1);
1138 1137
1139 1138 while (ddi_get8(kb8042->handle, kb8042->addr + I8042_INT_INPUT_AVAIL)
1140 1139 != 0) {
1141 1140 rc = DDI_INTR_CLAIMED;
1142 1141
1143 1142 scancode = ddi_get8(kb8042->handle,
1144 1143 kb8042->addr + I8042_INT_INPUT_DATA);
1145 1144
1146 1145 kb8042_received_byte(kb8042, scancode);
1147 1146 }
1148 1147
1149 1148 return (rc);
1150 1149 }
1151 1150
1152 1151 static void
1153 1152 kb8042_iocdatamsg(queue_t *qp, mblk_t *mp)
1154 1153 {
1155 1154 struct copyresp *csp;
1156 1155
1157 1156 csp = (struct copyresp *)mp->b_rptr;
1158 1157 if (csp->cp_rval) {
1159 1158 freemsg(mp);
1160 1159 return;
1161 1160 }
1162 1161
1163 1162 switch (csp->cp_cmd) {
1164 1163 default:
1165 1164 miocack(qp, mp, 0, 0);
1166 1165 break;
1167 1166 }
1168 1167 }
1169 1168
1170 1169 static boolean_t
1171 1170 kb8042_polled_keycheck(
1172 1171 struct kbtrans_hardware *hw,
1173 1172 int *key,
1174 1173 enum keystate *state)
1175 1174 {
1176 1175 struct kb8042 *kb8042 = (struct kb8042 *)hw;
1177 1176 int scancode;
1178 1177 boolean_t legit;
1179 1178 boolean_t synthetic_release_needed;
1180 1179
1181 1180 if (kb8042->polled_synthetic_release_pending) {
1182 1181 *key = kb8042->polled_synthetic_release_key;
1183 1182 *state = KEY_RELEASED;
1184 1183 kb8042->polled_synthetic_release_pending = B_FALSE;
1185 1184 (void) kb8042_autorepeat_detect(kb8042, *key, *state);
1186 1185 return (B_TRUE);
1187 1186 }
1188 1187
1189 1188 for (;;) {
1190 1189 if (ddi_get8(kb8042->handle,
1191 1190 kb8042->addr + I8042_POLL_INPUT_AVAIL) == 0) {
1192 1191 return (B_FALSE);
1193 1192 }
1194 1193
1195 1194 scancode = ddi_get8(kb8042->handle,
1196 1195 kb8042->addr + I8042_POLL_INPUT_DATA);
1197 1196
1198 1197 legit = KeyboardConvertScan(kb8042, scancode, key, state,
1199 1198 &synthetic_release_needed);
1200 1199 if (!legit) {
1201 1200 continue;
1202 1201 }
1203 1202 /*
1204 1203 * For the moment at least, we rely on hardware autorepeat
1205 1204 * for polled I/O autorepeat. However, for coordination
1206 1205 * with the interrupt-driven code, maintain the last key
1207 1206 * pressed.
1208 1207 */
1209 1208 (void) kb8042_autorepeat_detect(kb8042, *key, *state);
1210 1209
1211 1210 /*
1212 1211 * This is a total hack to support two additional keys
1213 1212 * on Korean keyboards. They report only on press, and
1214 1213 * so we synthesize a release. Most likely this will
1215 1214 * never be important to polled I/O, but if I do it
1216 1215 * "right" the first time it _won't_ be an issue.
1217 1216 */
1218 1217 if (synthetic_release_needed) {
1219 1218 kb8042->polled_synthetic_release_pending = B_TRUE;
1220 1219 kb8042->polled_synthetic_release_key = *key;
1221 1220 }
1222 1221
1223 1222 if (kb8042->simulated_kbd_type == KB_USB) {
1224 1223 *key = keytab_pc2usb[*key];
1225 1224 }
1226 1225 return (B_TRUE);
1227 1226 }
1228 1227 }
1229 1228
1230 1229 static void
1231 1230 kb8042_setled(struct kb8042 *kb8042, int led_state, boolean_t polled)
1232 1231 {
1233 1232 kb8042->leds.desired = led_state;
1234 1233
1235 1234 if (!polled)
1236 1235 mutex_enter(&kb8042->w_hw_mutex);
1237 1236
1238 1237 if (kb8042->leds.desired != kb8042->leds.commanded) {
1239 1238 kb8042_send_to_keyboard(kb8042, KB_SET_LED, polled);
1240 1239 }
1241 1240
1242 1241 if (!polled)
1243 1242 mutex_exit(&kb8042->w_hw_mutex);
1244 1243 }
1245 1244
1246 1245 static void
1247 1246 kb8042_polled_setled(struct kbtrans_hardware *hw, int led_state)
1248 1247 {
1249 1248 struct kb8042 *kb8042 = (struct kb8042 *)hw;
1250 1249 kb8042_setled(kb8042, led_state, B_TRUE);
1251 1250 }
1252 1251
1253 1252 static void
1254 1253 kb8042_streams_setled(struct kbtrans_hardware *hw, int led_state)
1255 1254 {
1256 1255 struct kb8042 *kb8042 = (struct kb8042 *)hw;
1257 1256 kb8042_setled(kb8042, led_state, B_FALSE);
1258 1257 }
1259 1258
1260 1259
1261 1260 static int
1262 1261 kb8042_send_and_wait(struct kb8042 *kb8042, uint8_t u8, boolean_t polled)
1263 1262 {
1264 1263 uint8_t *outp = kb8042->addr +
1265 1264 (polled ? I8042_POLL_OUTPUT_DATA : I8042_INT_OUTPUT_DATA);
1266 1265 uint8_t *inavp = kb8042->addr +
1267 1266 (polled ? I8042_POLL_INPUT_AVAIL : I8042_INT_INPUT_AVAIL);
1268 1267 uint8_t *inp = kb8042->addr +
1269 1268 (polled ? I8042_POLL_INPUT_DATA : I8042_INT_INPUT_DATA);
1270 1269 uint8_t b;
1271 1270 int ms_waited;
1272 1271 int timedout;
1273 1272 int expire;
1274 1273 int retries = 0;
1275 1274
1276 1275 do {
1277 1276 kb8042->acked = 0;
1278 1277 kb8042->need_retry = 0;
1279 1278 ms_waited = 0; /* Zero it whether polled or not */
1280 1279 timedout = 0;
1281 1280
1282 1281 ddi_put8(kb8042->handle, outp, u8);
1283 1282
1284 1283 while (!kb8042->acked && !kb8042->need_retry && !timedout) {
1285 1284
1286 1285 if (polled) {
1287 1286 if (ddi_get8(kb8042->handle, inavp)) {
1288 1287 b = ddi_get8(kb8042->handle, inp);
1289 1288 switch (b) {
1290 1289 case KB_ACK:
1291 1290 kb8042->acked = 1;
1292 1291 break;
1293 1292 case KB_RESEND:
1294 1293 kb8042->need_retry = 1;
1295 1294 break;
1296 1295 default:
1297 1296 /*
1298 1297 * drop it: We should never
1299 1298 * get scancodes while
1300 1299 * we're in the middle of a
1301 1300 * command anyway.
1302 1301 */
1303 1302 #ifdef DEBUG
1304 1303 cmn_err(CE_WARN, "!Unexpected "
1305 1304 " byte 0x%x", b);
1306 1305 #endif
1307 1306 break;
1308 1307 }
1309 1308 }
1310 1309
1311 1310 /*
1312 1311 * Wait 1ms if an ACK wasn't received yet
1313 1312 */
1314 1313 if (!kb8042->acked) {
1315 1314 drv_usecwait(1000);
1316 1315 ms_waited++;
1317 1316 if (ms_waited >= MAX_KB8042_WAIT_MAX_MS)
1318 1317 timedout = B_TRUE;
1319 1318 }
1320 1319 } else {
1321 1320 /* Interrupt-driven */
1322 1321 expire = ddi_get_lbolt() +
1323 1322 drv_usectohz(MAX_KB8042_WAIT_MAX_MS * 1000);
1324 1323
1325 1324 /*
1326 1325 * If cv_timedwait returned -1 and we neither
1327 1326 * received an ACK nor a RETRY response, then
1328 1327 * we timed out.
1329 1328 */
1330 1329 if (cv_timedwait(&kb8042->cmd_cv,
1331 1330 &kb8042->w_hw_mutex, expire) == -1 &&
1332 1331 !kb8042->acked && !kb8042->need_retry) {
1333 1332 timedout = B_TRUE;
1334 1333 }
1335 1334 }
1336 1335
1337 1336 }
1338 1337 } while ((kb8042->need_retry || timedout) &&
1339 1338 ++retries < MAX_KB8042_RETRIES);
1340 1339
1341 1340 return (kb8042->acked);
1342 1341 }
1343 1342
1344 1343 /*
1345 1344 * kb8042_send_to_keyboard should be called with w_hw_mutex held if
1346 1345 * polled is FALSE.
1347 1346 */
1348 1347 static void
1349 1348 kb8042_send_to_keyboard(struct kb8042 *kb8042, int byte, boolean_t polled)
1350 1349 {
1351 1350
1352 1351 /*
1353 1352 * KB_SET_LED and KB_ENABLE are special commands which require blocking
1354 1353 * other 8042 consumers while executing.
1355 1354 *
1356 1355 * Other commands/data are sent using the single put8 I/O access
1357 1356 * function.
1358 1357 */
1359 1358 if (byte == KB_SET_LED) {
1360 1359
1361 1360 if (!polled) {
1362 1361 (void) ddi_get8(kb8042->handle, kb8042->addr +
1363 1362 I8042_LOCK);
1364 1363 }
1365 1364
1366 1365 if (kb8042_send_and_wait(kb8042, KB_SET_LED, polled)) {
1367 1366 /*
1368 1367 * Ignore return value, as there's nothing we can
1369 1368 * do about it if the SET LED command fails.
1370 1369 */
1371 1370 (void) kb8042_send_and_wait(kb8042,
1372 1371 kb8042_xlate_leds(kb8042->leds.desired), polled);
1373 1372 }
1374 1373
1375 1374 if (!polled) {
1376 1375 (void) ddi_get8(kb8042->handle, kb8042->addr +
1377 1376 I8042_UNLOCK);
1378 1377 }
1379 1378 kb8042->leds.commanded = kb8042->leds.desired;
1380 1379
1381 1380 } else if (byte == KB_ENABLE) {
1382 1381
1383 1382 if (!polled) {
1384 1383 (void) ddi_get8(kb8042->handle, kb8042->addr +
1385 1384 I8042_LOCK);
1386 1385 }
1387 1386
1388 1387 (void) kb8042_send_and_wait(kb8042, KB_ENABLE, polled);
1389 1388
1390 1389 if (!polled) {
1391 1390 (void) ddi_get8(kb8042->handle, kb8042->addr +
1392 1391 I8042_UNLOCK);
1393 1392 }
1394 1393
1395 1394 } else {
1396 1395 /* All other commands use the "normal" virtual output port */
1397 1396 if (polled) {
1398 1397 ddi_put8(kb8042->handle,
1399 1398 kb8042->addr + I8042_POLL_OUTPUT_DATA, byte);
1400 1399 } else {
1401 1400 ddi_put8(kb8042->handle,
1402 1401 kb8042->addr + I8042_INT_OUTPUT_DATA, byte);
1403 1402 }
1404 1403 }
1405 1404 }
1406 1405
1407 1406 /*
1408 1407 * Wait until the keyboard is fully up, maybe.
1409 1408 * We may be the first person to talk to the keyboard, in which case
1410 1409 * it's patiently waiting to say "AA" to us to tell us it's up.
1411 1410 * In theory it sends the AA in 300ms < n < 9s, but it's a pretty
1412 1411 * good bet that we've already spent that long getting to that point,
1413 1412 * so we'll only wait long enough for the communications electronics to
1414 1413 * run.
1415 1414 */
1416 1415 static void
1417 1416 kb8042_wait_poweron(struct kb8042 *kb8042)
1418 1417 {
1419 1418 int cnt;
1420 1419 int ready;
1421 1420
1422 1421 /* wait for up to 250 ms for a response */
1423 1422 for (cnt = 0; cnt < 250; cnt++) {
1424 1423 ready = ddi_get8(kb8042->handle,
1425 1424 kb8042->addr + I8042_INT_INPUT_AVAIL);
1426 1425 if (ready != 0)
1427 1426 break;
1428 1427 drv_usecwait(1000);
1429 1428 }
1430 1429
1431 1430 /*
1432 1431 * If there's something pending, read and discard it. If not,
1433 1432 * assume things are OK anyway - maybe somebody else ate it
1434 1433 * already. (On a PC, the BIOS almost certainly did.)
1435 1434 */
1436 1435 if (ready != 0) {
1437 1436 (void) ddi_get8(kb8042->handle,
1438 1437 kb8042->addr + I8042_INT_INPUT_DATA);
1439 1438 }
1440 1439 }
1441 1440
1442 1441 static int
1443 1442 kb8042_xlate_leds(int led)
1444 1443 {
1445 1444 int res;
1446 1445
1447 1446 res = 0;
1448 1447
1449 1448 if (led & LED_NUM_LOCK)
1450 1449 res |= LED_NUM;
1451 1450 if (led & LED_SCROLL_LOCK)
1452 1451 res |= LED_SCR;
1453 1452 if (led & LED_CAPS_LOCK)
1454 1453 res |= LED_CAP;
1455 1454
1456 1455 return (res);
1457 1456 }
1458 1457
1459 1458 /*ARGSUSED*/
1460 1459 static void
1461 1460 kb8042_get_initial_leds(
1462 1461 struct kb8042 *kb8042,
1463 1462 int *initial_leds,
1464 1463 int *initial_led_mask)
1465 1464 {
1466 1465 #if defined(__i386) || defined(__amd64)
1467 1466 extern caddr_t p0_va;
1468 1467 uint8_t bios_kb_flag;
1469 1468
1470 1469 bios_kb_flag = p0_va[BIOS_KB_FLAG];
1471 1470
1472 1471 *initial_led_mask = LED_CAPS_LOCK | LED_NUM_LOCK | LED_SCROLL_LOCK;
1473 1472 *initial_leds = 0;
1474 1473 if (bios_kb_flag & BIOS_CAPS_STATE)
1475 1474 *initial_leds |= LED_CAPS_LOCK;
1476 1475 if (bios_kb_flag & BIOS_NUM_STATE)
1477 1476 *initial_leds |= LED_NUM_LOCK;
1478 1477 if (bios_kb_flag & BIOS_SCROLL_STATE)
1479 1478 *initial_leds |= LED_SCROLL_LOCK;
1480 1479 #else
1481 1480 *initial_leds = 0;
1482 1481 *initial_led_mask = 0;
1483 1482 #endif
1484 1483 }
1485 1484
1486 1485 static boolean_t
1487 1486 kb8042_autorepeat_detect(
1488 1487 struct kb8042 *kb8042,
1489 1488 int key_pos,
1490 1489 enum keystate state)
1491 1490 {
1492 1491 if (state == KEY_RELEASED) {
1493 1492 if (kb8042->kb_old_key_pos == key_pos)
1494 1493 kb8042->kb_old_key_pos = 0;
1495 1494 } else {
1496 1495 if (kb8042->kb_old_key_pos == key_pos) {
1497 1496 return (B_TRUE);
1498 1497 }
1499 1498 kb8042->kb_old_key_pos = key_pos;
1500 1499 }
1501 1500 return (B_FALSE);
1502 1501 }
1503 1502
1504 1503 /* ARGSUSED */
1505 1504 static void
1506 1505 kb8042_type4_cmd(struct kb8042 *kb8042, int cmd)
1507 1506 {
1508 1507 switch (cmd) {
1509 1508 case KBD_CMD_BELL:
1510 1509 (void) beeper_on(BEEP_TYPE4);
1511 1510 break;
1512 1511 case KBD_CMD_NOBELL:
1513 1512 (void) beeper_off();
1514 1513 break;
1515 1514 }
1516 1515 }
1517 1516
1518 1517
1519 1518 /*
1520 1519 * This is a pass-thru routine to get a character at poll time.
1521 1520 */
1522 1521 static int
1523 1522 kb8042_polled_getchar(cons_polledio_arg_t arg)
1524 1523 {
1525 1524 struct kb8042 *kb8042;
1526 1525
1527 1526 kb8042 = (struct kb8042 *)arg;
1528 1527
1529 1528 return (kbtrans_getchar(kb8042->hw_kbtrans));
1530 1529 }
1531 1530
1532 1531 /*
1533 1532 * This is a pass-thru routine to get a character at poll time.
1534 1533 */
1535 1534 static int
1536 1535 kb8042_polled_ischar(cons_polledio_arg_t arg)
1537 1536 {
1538 1537 struct kb8042 *kb8042;
1539 1538
1540 1539 kb8042 = (struct kb8042 *)arg;
1541 1540
1542 1541 return (kbtrans_ischar(kb8042->hw_kbtrans));
1543 1542 }
↓ open down ↓ |
1295 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX