[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 | * @{
|
---|
[b6a088f] | 33 | */
|
---|
[eeb643d] | 34 | /** @file
|
---|
[b6a088f] | 35 | * @brief Niagara console keyboard port driver.
|
---|
[eeb643d] | 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <as.h>
|
---|
| 39 | #include <ddi.h>
|
---|
| 40 | #include <async.h>
|
---|
| 41 | #include <sysinfo.h>
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include <thread.h>
|
---|
[3e6a98c5] | 44 | #include <stdbool.h>
|
---|
[d9fae235] | 45 | #include <errno.h>
|
---|
[b6a088f] | 46 | #include "../kbd_port.h"
|
---|
| 47 | #include "../kbd.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 | * Kernel counterpart of the driver pushes characters (it has read) here.
|
---|
| 67 | * Keep in sync with the definition from
|
---|
| 68 | * kernel/arch/sparc64/src/drivers/niagara.c.
|
---|
| 69 | */
|
---|
[d9fae235] | 70 | #define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
|
---|
| 71 |
|
---|
[86018c1] | 72 | typedef volatile struct {
|
---|
| 73 | uint64_t write_ptr;
|
---|
| 74 | uint64_t read_ptr;
|
---|
| 75 | char data[INPUT_BUFFER_SIZE];
|
---|
[bf9cb2f] | 76 | } __attribute__((packed)) __attribute__((aligned(PAGE_SIZE))) *input_buffer_t;
|
---|
[86018c1] | 77 |
|
---|
[8e33e1d] | 78 | /* virtual address of the shared buffer */
|
---|
[bf9cb2f] | 79 | static input_buffer_t input_buffer = (input_buffer_t) AS_AREA_ANY;
|
---|
[86018c1] | 80 |
|
---|
[eeb643d] | 81 | static volatile bool polling_disabled = false;
|
---|
[b473611] | 82 | static void niagara_thread_impl(void *arg);
|
---|
[eeb643d] | 83 |
|
---|
| 84 | /**
|
---|
[8e33e1d] | 85 | * Initializes the Niagara driver.
|
---|
[bf9cb2f] | 86 | * Maps the shared buffer and creates the polling thread.
|
---|
[eeb643d] | 87 | */
|
---|
[9be360ee] | 88 | static int niagara_port_init(kbd_dev_t *kdev)
|
---|
[eeb643d] | 89 | {
|
---|
[9be360ee] | 90 | kbd_dev = kdev;
|
---|
| 91 |
|
---|
[d9fae235] | 92 | sysarg_t paddr;
|
---|
| 93 | if (sysinfo_get_value("niagara.inbuf.address", &paddr) != EOK)
|
---|
| 94 | return -1;
|
---|
| 95 |
|
---|
[8442d10] | 96 | int rc = physmem_map(paddr, 1, AS_AREA_READ | AS_AREA_WRITE,
|
---|
| 97 | (void *) &input_buffer);
|
---|
[d9fae235] | 98 | if (rc != 0) {
|
---|
[86018c1] | 99 | printf("Niagara: uspace driver couldn't map physical memory: %d\n",
|
---|
[d9fae235] | 100 | rc);
|
---|
| 101 | return rc;
|
---|
[86018c1] | 102 | }
|
---|
[d9fae235] | 103 |
|
---|
[eeb643d] | 104 | thread_id_t tid;
|
---|
| 105 | rc = thread_create(niagara_thread_impl, NULL, "kbd_poll", &tid);
|
---|
[d9fae235] | 106 | if (rc != 0)
|
---|
[eeb643d] | 107 | return rc;
|
---|
[d9fae235] | 108 |
|
---|
[eeb643d] | 109 | return 0;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[b1bdc7a4] | 112 | static void niagara_port_yield(void)
|
---|
[eeb643d] | 113 | {
|
---|
| 114 | polling_disabled = true;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[b1bdc7a4] | 117 | static void niagara_port_reclaim(void)
|
---|
[eeb643d] | 118 | {
|
---|
| 119 | polling_disabled = false;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[b1bdc7a4] | 122 | static void niagara_port_write(uint8_t data)
|
---|
[eeb643d] | 123 | {
|
---|
| 124 | (void) data;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
[8e33e1d] | 128 | * Called regularly by the polling thread. Reads codes of all the
|
---|
| 129 | * pressed keys from the buffer.
|
---|
[eeb643d] | 130 | */
|
---|
| 131 | static void niagara_key_pressed(void)
|
---|
| 132 | {
|
---|
| 133 | char c;
|
---|
| 134 |
|
---|
[86018c1] | 135 | while (input_buffer->read_ptr != input_buffer->write_ptr) {
|
---|
| 136 | c = input_buffer->data[input_buffer->read_ptr];
|
---|
| 137 | input_buffer->read_ptr =
|
---|
[1875a0c] | 138 | ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
|
---|
| 139 | kbd_push_data(kbd_dev, c);
|
---|
[eeb643d] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
[336d2f52] | 144 | * Thread to poll Niagara console for keypresses.
|
---|
[eeb643d] | 145 | */
|
---|
[b473611] | 146 | static void niagara_thread_impl(void *arg)
|
---|
[eeb643d] | 147 | {
|
---|
| 148 | (void) arg;
|
---|
| 149 |
|
---|
| 150 | while (1) {
|
---|
| 151 | if (polling_disabled == false)
|
---|
| 152 | niagara_key_pressed();
|
---|
| 153 | usleep(POLL_INTERVAL);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | /** @}
|
---|
| 157 | */
|
---|