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 |
|
---|
47 | #define POLL_INTERVAL 10000
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Virtual address mapped to the buffer shared with the kernel counterpart.
|
---|
51 | */
|
---|
52 | static uintptr_t input_buffer_addr;
|
---|
53 |
|
---|
54 | /*
|
---|
55 | * Kernel counterpart of the driver pushes characters (it has read) here.
|
---|
56 | * Keep in sync with the definition from
|
---|
57 | * kernel/arch/sparc64/src/drivers/niagara.c.
|
---|
58 | */
|
---|
59 | #define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
|
---|
60 | typedef volatile struct {
|
---|
61 | uint64_t write_ptr;
|
---|
62 | uint64_t read_ptr;
|
---|
63 | char data[INPUT_BUFFER_SIZE];
|
---|
64 | }
|
---|
65 | __attribute__ ((packed))
|
---|
66 | __attribute__ ((aligned(PAGE_SIZE)))
|
---|
67 | *input_buffer_t;
|
---|
68 |
|
---|
69 | input_buffer_t input_buffer;
|
---|
70 |
|
---|
71 | static volatile bool polling_disabled = false;
|
---|
72 | static void *niagara_thread_impl(void *arg);
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Initializes the SGCN driver.
|
---|
76 | * Maps the physical memory (SRAM) and creates the polling thread.
|
---|
77 | */
|
---|
78 | int kbd_port_init(void)
|
---|
79 | {
|
---|
80 | input_buffer_addr = (uintptr_t) as_get_mappable_page(PAGE_SIZE);
|
---|
81 | int result = physmem_map(
|
---|
82 | (void *) sysinfo_value("niagara.inbuf.address"),
|
---|
83 | (void *) input_buffer_addr,
|
---|
84 | 1, AS_AREA_READ | AS_AREA_WRITE);
|
---|
85 |
|
---|
86 | if (result != 0) {
|
---|
87 | printf("Niagara: uspace driver couldn't map physical memory: %d\n",
|
---|
88 | result);
|
---|
89 | }
|
---|
90 |
|
---|
91 | input_buffer = (input_buffer_t) input_buffer_addr;
|
---|
92 |
|
---|
93 | thread_id_t tid;
|
---|
94 | int rc;
|
---|
95 |
|
---|
96 | rc = thread_create(niagara_thread_impl, NULL, "kbd_poll", &tid);
|
---|
97 | if (rc != 0) {
|
---|
98 | return rc;
|
---|
99 | }
|
---|
100 | return 0;
|
---|
101 | }
|
---|
102 |
|
---|
103 | void kbd_port_yield(void)
|
---|
104 | {
|
---|
105 | polling_disabled = true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | void kbd_port_reclaim(void)
|
---|
109 | {
|
---|
110 | polling_disabled = false;
|
---|
111 | }
|
---|
112 |
|
---|
113 | void kbd_port_write(uint8_t data)
|
---|
114 | {
|
---|
115 | (void) data;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Handler of the "key pressed" event. Reads codes of all the pressed keys from
|
---|
120 | * the buffer.
|
---|
121 | */
|
---|
122 | static void niagara_key_pressed(void)
|
---|
123 | {
|
---|
124 | char c;
|
---|
125 |
|
---|
126 | while (input_buffer->read_ptr != input_buffer->write_ptr) {
|
---|
127 | c = input_buffer->data[input_buffer->read_ptr];
|
---|
128 | input_buffer->read_ptr =
|
---|
129 | ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
|
---|
130 | kbd_push_scancode(c);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Thread to poll SGCN for keypresses.
|
---|
136 | */
|
---|
137 | static void *niagara_thread_impl(void *arg)
|
---|
138 | {
|
---|
139 | (void) arg;
|
---|
140 |
|
---|
141 | while (1) {
|
---|
142 | if (polling_disabled == false)
|
---|
143 | niagara_key_pressed();
|
---|
144 | usleep(POLL_INTERVAL);
|
---|
145 | }
|
---|
146 | return 0;
|
---|
147 | }
|
---|
148 | /** @}
|
---|
149 | */
|
---|