1/*- 2 * Copyright (c) 1996-1999 3 * Kazutaka YOKOTA (yokota@zodiac.mech.utsunomiya-u.ac.jp) 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 13 unchanged lines hidden (view full) --- 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * |
30 * $FreeBSD: head/sys/dev/atkbdc/atkbdc.c 58271 2000-03-19 03:25:13Z yokota $ |
31 * from kbdio.c,v 1.13 1998/09/25 11:55:46 yokota Exp 32 */ 33 34#include "atkbdc.h" 35#include "opt_kbd.h" 36 37#include <sys/param.h> 38#include <sys/systm.h> |
39#include <sys/bus.h> |
40#include <sys/malloc.h> 41#include <sys/syslog.h> |
42#include <machine/bus_pio.h> 43#include <machine/bus.h> 44#include <machine/resource.h> 45#include <sys/rman.h> |
46 47#include <machine/clock.h> 48 49#include <dev/kbd/atkbdcreg.h> 50 51#include <isa/isareg.h> 52 53/* constants */ --- 10 unchanged lines hidden (view full) --- 64#define nextq(i) (((i) + 1) % KBDQ_BUFSIZE) 65#define availq(q) ((q)->head != (q)->tail) 66#if KBDIO_DEBUG >= 2 67#define emptyq(q) ((q)->tail = (q)->head = (q)->qcount = 0) 68#else 69#define emptyq(q) ((q)->tail = (q)->head = 0) 70#endif 71 |
72#define read_data(k) (bus_space_read_1((k)->iot, (k)->ioh0, 0)) 73#define read_status(k) (bus_space_read_1((k)->iot, (k)->ioh1, 0)) 74#define write_data(k, d) \ 75 (bus_space_write_1((k)->iot, (k)->ioh0, 0, (d))) 76#define write_command(k, d) \ 77 (bus_space_write_1((k)->iot, (k)->ioh1, 0, (d))) 78 |
79/* local variables */ 80 81/* 82 * We always need at least one copy of the kbdc_softc struct for the 83 * low-level console. As the low-level console accesses the keyboard 84 * controller before kbdc, and all other devices, is probed, we 85 * statically allocate one entry. XXX 86 */ 87static atkbdc_softc_t default_kbdc; 88static atkbdc_softc_t *atkbdc_softc[MAXKBDC] = { &default_kbdc }; 89 90static int verbose = KBDIO_DEBUG; 91 92/* function prototypes */ 93 |
94static int atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, 95 bus_space_handle_t h0, bus_space_handle_t h1); |
96static int addq(kqueue *q, int c); 97static int removeq(kqueue *q); 98static int wait_while_controller_busy(atkbdc_softc_t *kbdc); 99static int wait_for_data(atkbdc_softc_t *kbdc); 100static int wait_for_kbd_data(atkbdc_softc_t *kbdc); 101static int wait_for_kbd_ack(atkbdc_softc_t *kbdc); 102static int wait_for_aux_data(atkbdc_softc_t *kbdc); 103static int wait_for_aux_ack(atkbdc_softc_t *kbdc); --- 7 unchanged lines hidden (view full) --- 111 return NULL; 112 sc = atkbdc_softc[unit]; 113 if (sc == NULL) { 114 sc = atkbdc_softc[unit] 115 = malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT); 116 if (sc == NULL) 117 return NULL; 118 bzero(sc, sizeof(*sc)); |
119 } 120 return sc; 121} 122 123int |
124atkbdc_probe_unit(int unit, struct resource *port0, struct resource *port1) |
125{ |
126 if (rman_get_start(port0) <= 0) |
127 return ENXIO; |
128 if (rman_get_start(port1) <= 0) 129 return ENXIO; |
130 return 0; 131} 132 133int |
134atkbdc_attach_unit(int unit, atkbdc_softc_t *sc, struct resource *port0, 135 struct resource *port1) |
136{ |
137 return atkbdc_setup(sc, rman_get_bustag(port0), 138 rman_get_bushandle(port0), 139 rman_get_bushandle(port1)); |
140} 141 142/* the backdoor to the keyboard controller! XXX */ 143int 144atkbdc_configure(void) 145{ |
146 bus_space_tag_t tag; 147 bus_space_handle_t h0; 148 bus_space_handle_t h1; 149 int port0; 150 int port1; 151 152 port0 = IO_KBD; 153 resource_int_value("atkbdc", 0, "port", &port0); 154 port1 = IO_KBD + KBD_STATUS_PORT; 155#if 0 156 resource_int_value("atkbdc", 0, "port", &port0); 157#endif 158 159 /* XXX: tag should be passed from the caller */ 160#if defined(__i386__) 161 tag = I386_BUS_SPACE_IO; 162#elif defined(__alpha__) 163 tag = ALPHA_BUS_SPACE_IO; 164#endif 165 166#if notyet 167 bus_space_map(tag, port0, IO_KBDSIZE, 0, &h0); 168 bus_space_map(tag, port1, IO_KBDSIZE, 0, &h1); 169#else 170 h0 = (bus_space_handle_t)port0; 171 h1 = (bus_space_handle_t)port1; 172#endif 173 return atkbdc_setup(atkbdc_softc[0], tag, h0, h1); |
174} 175 176static int |
177atkbdc_setup(atkbdc_softc_t *sc, bus_space_tag_t tag, bus_space_handle_t h0, 178 bus_space_handle_t h1) |
179{ |
180 if (sc->ioh0 == 0) { /* XXX */ |
181 sc->command_byte = -1; 182 sc->command_mask = 0; 183 sc->lock = FALSE; 184 sc->kbd.head = sc->kbd.tail = 0; 185 sc->aux.head = sc->aux.tail = 0; 186#if KBDIO_DEBUG >= 2 187 sc->kbd.call_count = 0; 188 sc->kbd.qcount = sc->kbd.max_qcount = 0; 189 sc->aux.call_count = 0; 190 sc->aux.qcount = sc->aux.max_qcount = 0; 191#endif 192 } |
193 sc->iot = tag; 194 sc->ioh0 = h0; 195 sc->ioh1 = h1; |
196 return 0; 197} 198 |
199/* open a keyboard controller */ |
200KBDC |
201atkbdc_open(int unit) |
202{ |
203 if (unit <= 0) 204 unit = 0; 205 if (unit >= MAXKBDC) 206 return NULL; 207 if ((atkbdc_softc[unit]->port0 != NULL) 208 || (atkbdc_softc[unit]->ioh0 != 0)) /* XXX */ 209 return (KBDC)atkbdc_softc[unit]; |
210 return NULL; 211} 212 213/* 214 * I/O access arbitration in `kbdio' 215 * 216 * The `kbdio' module uses a simplistic convention to arbitrate 217 * I/O access to the controller/keyboard/mouse. The convention requires --- 42 unchanged lines hidden (view full) --- 260 return (prevlock != lock); 261} 262 263/* check if any data is waiting to be processed */ 264int 265kbdc_data_ready(KBDC p) 266{ 267 return (availq(&kbdcp(p)->kbd) || availq(&kbdcp(p)->aux) |
268 || (read_status(kbdcp(p)) & KBDS_ANY_BUFFER_FULL)); |
269} 270 271/* queuing functions */ 272 273static int 274addq(kqueue *q, int c) 275{ 276 if (nextq(q->tail) != q->head) { --- 29 unchanged lines hidden (view full) --- 306/* 307 * device I/O routines 308 */ 309static int 310wait_while_controller_busy(struct atkbdc_softc *kbdc) 311{ 312 /* CPU will stay inside the loop for 100msec at most */ 313 int retry = 5000; |
314 int f; 315 |
316 while ((f = read_status(kbdc)) & KBDS_INPUT_BUFFER_FULL) { |
317 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) { 318 DELAY(KBDD_DELAYTIME); |
319 addq(&kbdc->kbd, read_data(kbdc)); |
320 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) { 321 DELAY(KBDD_DELAYTIME); |
322 addq(&kbdc->aux, read_data(kbdc)); |
323 } 324 DELAY(KBDC_DELAYTIME); 325 if (--retry < 0) 326 return FALSE; 327 } 328 return TRUE; 329} 330 331/* 332 * wait for any data; whether it's from the controller, 333 * the keyboard, or the aux device. 334 */ 335static int 336wait_for_data(struct atkbdc_softc *kbdc) 337{ 338 /* CPU will stay inside the loop for 200msec at most */ 339 int retry = 10000; |
340 int f; 341 |
342 while ((f = read_status(kbdc) & KBDS_ANY_BUFFER_FULL) == 0) { |
343 DELAY(KBDC_DELAYTIME); 344 if (--retry < 0) 345 return 0; 346 } 347 DELAY(KBDD_DELAYTIME); 348 return f; 349} 350 351/* wait for data from the keyboard */ 352static int 353wait_for_kbd_data(struct atkbdc_softc *kbdc) 354{ 355 /* CPU will stay inside the loop for 200msec at most */ 356 int retry = 10000; |
357 int f; 358 |
359 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL) |
360 != KBDS_KBD_BUFFER_FULL) { 361 if (f == KBDS_AUX_BUFFER_FULL) { 362 DELAY(KBDD_DELAYTIME); |
363 addq(&kbdc->aux, read_data(kbdc)); |
364 } 365 DELAY(KBDC_DELAYTIME); 366 if (--retry < 0) 367 return 0; 368 } 369 DELAY(KBDD_DELAYTIME); 370 return f; 371} 372 373/* 374 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the keyboard. 375 * queue anything else. 376 */ 377static int 378wait_for_kbd_ack(struct atkbdc_softc *kbdc) 379{ 380 /* CPU will stay inside the loop for 200msec at most */ 381 int retry = 10000; |
382 int f; 383 int b; 384 385 while (retry-- > 0) { |
386 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) { |
387 DELAY(KBDD_DELAYTIME); |
388 b = read_data(kbdc); |
389 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) { 390 if ((b == KBD_ACK) || (b == KBD_RESEND) 391 || (b == KBD_RESET_FAIL)) 392 return b; 393 addq(&kbdc->kbd, b); 394 } else if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) { 395 addq(&kbdc->aux, b); 396 } --- 4 unchanged lines hidden (view full) --- 401} 402 403/* wait for data from the aux device */ 404static int 405wait_for_aux_data(struct atkbdc_softc *kbdc) 406{ 407 /* CPU will stay inside the loop for 200msec at most */ 408 int retry = 10000; |
409 int f; 410 |
411 while ((f = read_status(kbdc) & KBDS_BUFFER_FULL) |
412 != KBDS_AUX_BUFFER_FULL) { 413 if (f == KBDS_KBD_BUFFER_FULL) { 414 DELAY(KBDD_DELAYTIME); |
415 addq(&kbdc->kbd, read_data(kbdc)); |
416 } 417 DELAY(KBDC_DELAYTIME); 418 if (--retry < 0) 419 return 0; 420 } 421 DELAY(KBDD_DELAYTIME); 422 return f; 423} 424 425/* 426 * wait for an ACK(FAh), RESEND(FEh), or RESET_FAIL(FCh) from the aux device. 427 * queue anything else. 428 */ 429static int 430wait_for_aux_ack(struct atkbdc_softc *kbdc) 431{ 432 /* CPU will stay inside the loop for 200msec at most */ 433 int retry = 10000; |
434 int f; 435 int b; 436 437 while (retry-- > 0) { |
438 if ((f = read_status(kbdc)) & KBDS_ANY_BUFFER_FULL) { |
439 DELAY(KBDD_DELAYTIME); |
440 b = read_data(kbdc); |
441 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) { 442 if ((b == PSM_ACK) || (b == PSM_RESEND) 443 || (b == PSM_RESET_FAIL)) 444 return b; 445 addq(&kbdc->aux, b); 446 } else if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) { 447 addq(&kbdc->kbd, b); 448 } --- 4 unchanged lines hidden (view full) --- 453} 454 455/* write a one byte command to the controller */ 456int 457write_controller_command(KBDC p, int c) 458{ 459 if (!wait_while_controller_busy(kbdcp(p))) 460 return FALSE; |
461 write_command(kbdcp(p), c); |
462 return TRUE; 463} 464 465/* write a one byte data to the controller */ 466int 467write_controller_data(KBDC p, int c) 468{ 469 if (!wait_while_controller_busy(kbdcp(p))) 470 return FALSE; |
471 write_data(kbdcp(p), c); |
472 return TRUE; 473} 474 475/* write a one byte keyboard command */ 476int 477write_kbd_command(KBDC p, int c) 478{ 479 if (!wait_while_controller_busy(kbdcp(p))) 480 return FALSE; |
481 write_data(kbdcp(p), c); |
482 return TRUE; 483} 484 485/* write a one byte auxiliary device command */ 486int 487write_aux_command(KBDC p, int c) 488{ 489 if (!write_controller_command(p, KBDC_WRITE_TO_AUX)) --- 112 unchanged lines hidden (view full) --- 602read_controller_data(KBDC p) 603{ 604 if (availq(&kbdcp(p)->kbd)) 605 return removeq(&kbdcp(p)->kbd); 606 if (availq(&kbdcp(p)->aux)) 607 return removeq(&kbdcp(p)->aux); 608 if (!wait_for_data(kbdcp(p))) 609 return -1; /* timeout */ |
610 return read_data(kbdcp(p)); |
611} 612 613#if KBDIO_DEBUG >= 2 614static int call = 0; 615#endif 616 617/* read one byte from the keyboard */ 618int --- 8 unchanged lines hidden (view full) --- 627 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount); 628 } 629#endif 630 631 if (availq(&kbdcp(p)->kbd)) 632 return removeq(&kbdcp(p)->kbd); 633 if (!wait_for_kbd_data(kbdcp(p))) 634 return -1; /* timeout */ |
635 return read_data(kbdcp(p)); |
636} 637 638/* read one byte from the keyboard, but return immediately if 639 * no data is waiting 640 */ 641int 642read_kbd_data_no_wait(KBDC p) 643{ --- 6 unchanged lines hidden (view full) --- 650 "aux q: %d calls, max %d chars\n", 651 kbdcp(p)->kbd.call_count, kbdcp(p)->kbd.max_qcount, 652 kbdcp(p)->aux.call_count, kbdcp(p)->aux.max_qcount); 653 } 654#endif 655 656 if (availq(&kbdcp(p)->kbd)) 657 return removeq(&kbdcp(p)->kbd); |
658 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL; |
659 if (f == KBDS_AUX_BUFFER_FULL) { 660 DELAY(KBDD_DELAYTIME); |
661 addq(&kbdcp(p)->aux, read_data(kbdcp(p))); 662 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL; |
663 } 664 if (f == KBDS_KBD_BUFFER_FULL) { 665 DELAY(KBDD_DELAYTIME); |
666 return read_data(kbdcp(p)); |
667 } 668 return -1; /* no data */ 669} 670 671/* read one byte from the aux device */ 672int 673read_aux_data(KBDC p) 674{ 675 if (availq(&kbdcp(p)->aux)) 676 return removeq(&kbdcp(p)->aux); 677 if (!wait_for_aux_data(kbdcp(p))) 678 return -1; /* timeout */ |
679 return read_data(kbdcp(p)); |
680} 681 682/* read one byte from the aux device, but return immediately if 683 * no data is waiting 684 */ 685int 686read_aux_data_no_wait(KBDC p) 687{ 688 int f; 689 690 if (availq(&kbdcp(p)->aux)) 691 return removeq(&kbdcp(p)->aux); |
692 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL; |
693 if (f == KBDS_KBD_BUFFER_FULL) { 694 DELAY(KBDD_DELAYTIME); |
695 addq(&kbdcp(p)->kbd, read_data(kbdcp(p))); 696 f = read_status(kbdcp(p)) & KBDS_BUFFER_FULL; |
697 } 698 if (f == KBDS_AUX_BUFFER_FULL) { 699 DELAY(KBDD_DELAYTIME); |
700 return read_data(kbdcp(p)); |
701 } 702 return -1; /* no data */ 703} 704 705/* discard data from the keyboard */ 706void 707empty_kbd_buffer(KBDC p, int wait) 708{ 709 int t; 710 int b; 711 int f; 712#if KBDIO_DEBUG >= 2 713 int c1 = 0; 714 int c2 = 0; 715#endif 716 int delta = 2; 717 718 for (t = wait; t > 0; ) { |
719 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) { |
720 DELAY(KBDD_DELAYTIME); |
721 b = read_data(kbdcp(p)); |
722 if ((f & KBDS_BUFFER_FULL) == KBDS_AUX_BUFFER_FULL) { 723 addq(&kbdcp(p)->aux, b); 724#if KBDIO_DEBUG >= 2 725 ++c2; 726 } else { 727 ++c1; 728#endif 729 } --- 20 unchanged lines hidden (view full) --- 750 int f; 751#if KBDIO_DEBUG >= 2 752 int c1 = 0; 753 int c2 = 0; 754#endif 755 int delta = 2; 756 757 for (t = wait; t > 0; ) { |
758 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) { |
759 DELAY(KBDD_DELAYTIME); |
760 b = read_data(kbdcp(p)); |
761 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) { 762 addq(&kbdcp(p)->kbd, b); 763#if KBDIO_DEBUG >= 2 764 ++c1; 765 } else { 766 ++c2; 767#endif 768 } --- 19 unchanged lines hidden (view full) --- 788 int f; 789#if KBDIO_DEBUG >= 2 790 int c1 = 0; 791 int c2 = 0; 792#endif 793 int delta = 2; 794 795 for (t = wait; t > 0; ) { |
796 if ((f = read_status(kbdcp(p))) & KBDS_ANY_BUFFER_FULL) { |
797 DELAY(KBDD_DELAYTIME); |
798 (void)read_data(kbdcp(p)); |
799#if KBDIO_DEBUG >= 2 800 if ((f & KBDS_BUFFER_FULL) == KBDS_KBD_BUFFER_FULL) 801 ++c1; 802 else 803 ++c2; 804#endif 805 t = wait; 806 } else { --- 232 unchanged lines hidden --- |