[965dc18] | 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 |
|
---|
[24ff4df] | 29 | /** @addtogroup kbd_port
|
---|
[965dc18] | 30 | * @ingroup kbd
|
---|
| 31 | * @{
|
---|
[d9fae235] | 32 | */
|
---|
[965dc18] | 33 | /** @file
|
---|
[d9fae235] | 34 | * @brief SGCN (Serengeti Console) keyboard port driver.
|
---|
[965dc18] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <as.h>
|
---|
| 38 | #include <ddi.h>
|
---|
[24ff4df] | 39 | #include <async.h>
|
---|
[965dc18] | 40 | #include <kbd.h>
|
---|
[24ff4df] | 41 | #include <kbd_port.h>
|
---|
[965dc18] | 42 | #include <sysinfo.h>
|
---|
| 43 | #include <stdio.h>
|
---|
[63b1537] | 44 | #include <thread.h>
|
---|
[9701d49] | 45 | #include <bool.h>
|
---|
[d9fae235] | 46 | #include <errno.h>
|
---|
[63b1537] | 47 |
|
---|
[d9fae235] | 48 | #define POLL_INTERVAL 10000
|
---|
[965dc18] | 49 |
|
---|
| 50 | /**
|
---|
| 51 | * SGCN buffer header. It is placed at the very beginning of the SGCN
|
---|
[d9fae235] | 52 | * buffer.
|
---|
[965dc18] | 53 | */
|
---|
| 54 | typedef struct {
|
---|
| 55 | /** hard-wired to "CON" */
|
---|
| 56 | char magic[4];
|
---|
| 57 |
|
---|
| 58 | /** we don't need this */
|
---|
| 59 | char unused[8];
|
---|
| 60 |
|
---|
| 61 | /** offset within the SGCN buffer of the input buffer start */
|
---|
| 62 | uint32_t in_begin;
|
---|
| 63 |
|
---|
| 64 | /** offset within the SGCN buffer of the input buffer end */
|
---|
| 65 | uint32_t in_end;
|
---|
| 66 |
|
---|
| 67 | /** offset within the SGCN buffer of the input buffer read pointer */
|
---|
| 68 | uint32_t in_rdptr;
|
---|
| 69 |
|
---|
| 70 | /** offset within the SGCN buffer of the input buffer write pointer */
|
---|
| 71 | uint32_t in_wrptr;
|
---|
| 72 | } __attribute__ ((packed)) sgcn_buffer_header_t;
|
---|
| 73 |
|
---|
| 74 | /*
|
---|
| 75 | * Returns a pointer to the object of a given type which is placed at the given
|
---|
| 76 | * offset from the console buffer beginning.
|
---|
| 77 | */
|
---|
| 78 | #define SGCN_BUFFER(type, offset) \
|
---|
| 79 | ((type *) (sram_virt_addr + sram_buffer_offset + (offset)))
|
---|
| 80 |
|
---|
| 81 | /** Returns a pointer to the console buffer header. */
|
---|
| 82 | #define SGCN_BUFFER_HEADER (SGCN_BUFFER(sgcn_buffer_header_t, 0))
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Virtual address mapped to SRAM.
|
---|
| 86 | */
|
---|
| 87 | static uintptr_t sram_virt_addr;
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
| 90 | * SGCN buffer offset within SGCN.
|
---|
| 91 | */
|
---|
| 92 | static uintptr_t sram_buffer_offset;
|
---|
| 93 |
|
---|
[63b1537] | 94 | /* polling thread */
|
---|
[36e9cd1] | 95 | static void sgcn_thread_impl(void *arg);
|
---|
[24ff4df] | 96 |
|
---|
[9701d49] | 97 | static volatile bool polling_disabled = false;
|
---|
[24ff4df] | 98 |
|
---|
[965dc18] | 99 | /**
|
---|
| 100 | * Initializes the SGCN driver.
|
---|
[63b1537] | 101 | * Maps the physical memory (SRAM) and creates the polling thread.
|
---|
[965dc18] | 102 | */
|
---|
[24ff4df] | 103 | int kbd_port_init(void)
|
---|
[965dc18] | 104 | {
|
---|
[d9fae235] | 105 | sysarg_t sram_paddr;
|
---|
| 106 | if (sysinfo_get_value("sram.address.physical", &sram_paddr) != EOK)
|
---|
| 107 | return -1;
|
---|
| 108 |
|
---|
| 109 | sysarg_t sram_size;
|
---|
| 110 | if (sysinfo_get_value("sram.area.size", &sram_size) != EOK)
|
---|
| 111 | return -1;
|
---|
| 112 |
|
---|
| 113 | if (sysinfo_get_value("sram.buffer.offset", &sram_buffer_offset) != EOK)
|
---|
| 114 | sram_buffer_offset = 0;
|
---|
| 115 |
|
---|
| 116 | sram_virt_addr = (uintptr_t) as_get_mappable_page(sram_size);
|
---|
| 117 |
|
---|
| 118 | if (physmem_map((void *) sram_paddr, (void *) sram_virt_addr,
|
---|
| 119 | sram_size / PAGE_SIZE, AS_AREA_READ | AS_AREA_WRITE) != 0) {
|
---|
[965dc18] | 120 | printf("SGCN: uspace driver could not map physical memory.");
|
---|
[24ff4df] | 121 | return -1;
|
---|
| 122 | }
|
---|
[965dc18] | 123 |
|
---|
[63b1537] | 124 | thread_id_t tid;
|
---|
[d9fae235] | 125 | int rc = thread_create(sgcn_thread_impl, NULL, "kbd_poll", &tid);
|
---|
| 126 | if (rc != 0)
|
---|
[63b1537] | 127 | return rc;
|
---|
[d9fae235] | 128 |
|
---|
[24ff4df] | 129 | return 0;
|
---|
[965dc18] | 130 | }
|
---|
| 131 |
|
---|
[ccd1a14] | 132 | void kbd_port_yield(void)
|
---|
| 133 | {
|
---|
[9701d49] | 134 | polling_disabled = true;
|
---|
[ccd1a14] | 135 | }
|
---|
| 136 |
|
---|
| 137 | void kbd_port_reclaim(void)
|
---|
| 138 | {
|
---|
[9701d49] | 139 | polling_disabled = false;
|
---|
[ccd1a14] | 140 | }
|
---|
| 141 |
|
---|
[c145bc2] | 142 | void kbd_port_write(uint8_t data)
|
---|
| 143 | {
|
---|
| 144 | (void) data;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[965dc18] | 147 | /**
|
---|
| 148 | * Handler of the "key pressed" event. Reads codes of all the pressed keys from
|
---|
| 149 | * the buffer.
|
---|
| 150 | */
|
---|
[63b1537] | 151 | static void sgcn_key_pressed(void)
|
---|
[965dc18] | 152 | {
|
---|
| 153 | char c;
|
---|
| 154 |
|
---|
| 155 | uint32_t begin = SGCN_BUFFER_HEADER->in_begin;
|
---|
| 156 | uint32_t end = SGCN_BUFFER_HEADER->in_end;
|
---|
| 157 | uint32_t size = end - begin;
|
---|
| 158 |
|
---|
| 159 | volatile char *buf_ptr = (volatile char *)
|
---|
| 160 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr);
|
---|
| 161 | volatile uint32_t *in_wrptr_ptr = &(SGCN_BUFFER_HEADER->in_wrptr);
|
---|
| 162 | volatile uint32_t *in_rdptr_ptr = &(SGCN_BUFFER_HEADER->in_rdptr);
|
---|
| 163 |
|
---|
| 164 | while (*in_rdptr_ptr != *in_wrptr_ptr) {
|
---|
| 165 | c = *buf_ptr;
|
---|
| 166 | *in_rdptr_ptr = (((*in_rdptr_ptr) - begin + 1) % size) + begin;
|
---|
| 167 | buf_ptr = (volatile char *)
|
---|
| 168 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr);
|
---|
[24ff4df] | 169 | kbd_push_scancode(c);
|
---|
[965dc18] | 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[63b1537] | 173 | /**
|
---|
| 174 | * Thread to poll SGCN for keypresses.
|
---|
| 175 | */
|
---|
[36e9cd1] | 176 | static void sgcn_thread_impl(void *arg)
|
---|
[63b1537] | 177 | {
|
---|
| 178 | (void) arg;
|
---|
| 179 |
|
---|
| 180 | while (1) {
|
---|
[9701d49] | 181 | if (polling_disabled == false)
|
---|
| 182 | sgcn_key_pressed();
|
---|
[63b1537] | 183 | usleep(POLL_INTERVAL);
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[965dc18] | 187 | /** @}
|
---|
| 188 | */
|
---|