2
* Copyright (c) 2008 Pavel Rimsky
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
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.
29
/** @addtogroup kbd_port
34
* @brief SGCN (Serengeti Console) keyboard port driver.
47
#define POLL_INTERVAL 10000
50
* SGCN buffer header. It is placed at the very beginning of the SGCN
54
/** hard-wired to "CON" */
57
/** we don't need this */
60
/** offset within the SGCN buffer of the input buffer start */
63
/** offset within the SGCN buffer of the input buffer end */
66
/** offset within the SGCN buffer of the input buffer read pointer */
69
/** offset within the SGCN buffer of the input buffer write pointer */
71
} __attribute__ ((packed)) sgcn_buffer_header_t;
74
* Returns a pointer to the object of a given type which is placed at the given
75
* offset from the console buffer beginning.
77
#define SGCN_BUFFER(type, offset) \
78
((type *) (sram_virt_addr + sram_buffer_offset + (offset)))
80
/** Returns a pointer to the console buffer header. */
81
#define SGCN_BUFFER_HEADER (SGCN_BUFFER(sgcn_buffer_header_t, 0))
84
* Virtual address mapped to SRAM.
86
static uintptr_t sram_virt_addr;
89
* SGCN buffer offset within SGCN.
91
static uintptr_t sram_buffer_offset;
94
static void *sgcn_thread_impl(void *arg);
96
static volatile bool polling_disabled = false;
99
* Initializes the SGCN driver.
100
* Maps the physical memory (SRAM) and creates the polling thread.
102
int kbd_port_init(void)
104
sram_virt_addr = (uintptr_t) as_get_mappable_page(sysinfo_value("sram.area.size"));
105
if (physmem_map((void *) sysinfo_value("sram.address.physical"),
106
(void *) sram_virt_addr, sysinfo_value("sram.area.size") / PAGE_SIZE,
107
AS_AREA_READ | AS_AREA_WRITE) != 0) {
108
printf("SGCN: uspace driver could not map physical memory.");
112
sram_buffer_offset = sysinfo_value("sram.buffer.offset");
117
rc = thread_create(sgcn_thread_impl, NULL, "kbd_poll", &tid);
125
void kbd_port_yield(void)
127
polling_disabled = true;
130
void kbd_port_reclaim(void)
132
polling_disabled = false;
136
* Handler of the "key pressed" event. Reads codes of all the pressed keys from
139
static void sgcn_key_pressed(void)
143
uint32_t begin = SGCN_BUFFER_HEADER->in_begin;
144
uint32_t end = SGCN_BUFFER_HEADER->in_end;
145
uint32_t size = end - begin;
147
volatile char *buf_ptr = (volatile char *)
148
SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr);
149
volatile uint32_t *in_wrptr_ptr = &(SGCN_BUFFER_HEADER->in_wrptr);
150
volatile uint32_t *in_rdptr_ptr = &(SGCN_BUFFER_HEADER->in_rdptr);
152
while (*in_rdptr_ptr != *in_wrptr_ptr) {
154
*in_rdptr_ptr = (((*in_rdptr_ptr) - begin + 1) % size) + begin;
155
buf_ptr = (volatile char *)
156
SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr);
157
kbd_push_scancode(c);
162
* Thread to poll SGCN for keypresses.
164
static void *sgcn_thread_impl(void *arg)
169
if (polling_disabled == false)
171
usleep(POLL_INTERVAL);