| 1 | /* | 
|---|
| 2 | * Copyright (c) 2008 Pavel Rimsky | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @addtogroup kbd_port | 
|---|
| 30 | * @ingroup  kbd | 
|---|
| 31 | * @{ | 
|---|
| 32 | */ | 
|---|
| 33 | /** @file | 
|---|
| 34 | * @brief       Niagara console keyboard port driver. | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | #include <as.h> | 
|---|
| 38 | #include <ddi.h> | 
|---|
| 39 | #include <async.h> | 
|---|
| 40 | #include <kbd.h> | 
|---|
| 41 | #include <kbd_port.h> | 
|---|
| 42 | #include <sysinfo.h> | 
|---|
| 43 | #include <stdio.h> | 
|---|
| 44 | #include <thread.h> | 
|---|
| 45 | #include <bool.h> | 
|---|
| 46 | #include <errno.h> | 
|---|
| 47 |  | 
|---|
| 48 | #define POLL_INTERVAL  10000 | 
|---|
| 49 |  | 
|---|
| 50 | /** | 
|---|
| 51 | * Virtual address mapped to the buffer shared with the kernel counterpart. | 
|---|
| 52 | */ | 
|---|
| 53 | static uintptr_t input_buffer_addr; | 
|---|
| 54 |  | 
|---|
| 55 | /* | 
|---|
| 56 | * Kernel counterpart of the driver pushes characters (it has read) here. | 
|---|
| 57 | * Keep in sync with the definition from | 
|---|
| 58 | * kernel/arch/sparc64/src/drivers/niagara.c. | 
|---|
| 59 | */ | 
|---|
| 60 | #define INPUT_BUFFER_SIZE  ((PAGE_SIZE) - 2 * 8) | 
|---|
| 61 |  | 
|---|
| 62 | typedef volatile struct { | 
|---|
| 63 | uint64_t write_ptr; | 
|---|
| 64 | uint64_t read_ptr; | 
|---|
| 65 | char data[INPUT_BUFFER_SIZE]; | 
|---|
| 66 | } | 
|---|
| 67 | __attribute__ ((packed)) | 
|---|
| 68 | __attribute__ ((aligned(PAGE_SIZE))) | 
|---|
| 69 | *input_buffer_t; | 
|---|
| 70 |  | 
|---|
| 71 | /* virtual address of the shared buffer */ | 
|---|
| 72 | input_buffer_t input_buffer; | 
|---|
| 73 |  | 
|---|
| 74 | static volatile bool polling_disabled = false; | 
|---|
| 75 | static void niagara_thread_impl(void *arg); | 
|---|
| 76 |  | 
|---|
| 77 | /** | 
|---|
| 78 | * Initializes the Niagara driver. | 
|---|
| 79 | * Maps the shared buffer and creates the polling thread. | 
|---|
| 80 | */ | 
|---|
| 81 | int kbd_port_init(void) | 
|---|
| 82 | { | 
|---|
| 83 | sysarg_t paddr; | 
|---|
| 84 | if (sysinfo_get_value("niagara.inbuf.address", &paddr) != EOK) | 
|---|
| 85 | return -1; | 
|---|
| 86 |  | 
|---|
| 87 | input_buffer_addr = (uintptr_t) as_get_mappable_page(PAGE_SIZE); | 
|---|
| 88 | int rc = physmem_map((void *) paddr, (void *) input_buffer_addr, | 
|---|
| 89 | 1, AS_AREA_READ | AS_AREA_WRITE); | 
|---|
| 90 |  | 
|---|
| 91 | if (rc != 0) { | 
|---|
| 92 | printf("Niagara: uspace driver couldn't map physical memory: %d\n", | 
|---|
| 93 | rc); | 
|---|
| 94 | return rc; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | input_buffer = (input_buffer_t) input_buffer_addr; | 
|---|
| 98 |  | 
|---|
| 99 | thread_id_t tid; | 
|---|
| 100 | rc = thread_create(niagara_thread_impl, NULL, "kbd_poll", &tid); | 
|---|
| 101 | if (rc != 0) | 
|---|
| 102 | return rc; | 
|---|
| 103 |  | 
|---|
| 104 | return 0; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | void kbd_port_yield(void) | 
|---|
| 108 | { | 
|---|
| 109 | polling_disabled = true; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | void kbd_port_reclaim(void) | 
|---|
| 113 | { | 
|---|
| 114 | polling_disabled = false; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | void kbd_port_write(uint8_t data) | 
|---|
| 118 | { | 
|---|
| 119 | (void) data; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | /** | 
|---|
| 123 | * Called regularly by the polling thread. Reads codes of all the | 
|---|
| 124 | * pressed keys from the buffer. | 
|---|
| 125 | */ | 
|---|
| 126 | static void niagara_key_pressed(void) | 
|---|
| 127 | { | 
|---|
| 128 | char c; | 
|---|
| 129 |  | 
|---|
| 130 | while (input_buffer->read_ptr != input_buffer->write_ptr) { | 
|---|
| 131 | c = input_buffer->data[input_buffer->read_ptr]; | 
|---|
| 132 | input_buffer->read_ptr = | 
|---|
| 133 | ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE; | 
|---|
| 134 | kbd_push_scancode(c); | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | /** | 
|---|
| 139 | * Thread to poll SGCN for keypresses. | 
|---|
| 140 | */ | 
|---|
| 141 | static void niagara_thread_impl(void *arg) | 
|---|
| 142 | { | 
|---|
| 143 | (void) arg; | 
|---|
| 144 |  | 
|---|
| 145 | while (1) { | 
|---|
| 146 | if (polling_disabled == false) | 
|---|
| 147 | niagara_key_pressed(); | 
|---|
| 148 | usleep(POLL_INTERVAL); | 
|---|
| 149 | } | 
|---|
| 150 | } | 
|---|
| 151 | /** @} | 
|---|
| 152 | */ | 
|---|