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