source: mainline/uspace/srv/hid/kbd/port/niagara.c@ b1bdc7a4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b1bdc7a4 was b1bdc7a4, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Control keyboard port modules through ops structures. Allows compiling in
all modules at the same time.

  • Property mode set to 100644
File size: 4.2 KB
Line 
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#include <errno.h>
47
48static int niagara_port_init(void);
49static void niagara_port_yield(void);
50static void niagara_port_reclaim(void);
51static void niagara_port_write(uint8_t data);
52
53kbd_port_ops_t niagara_port = {
54 .init = niagara_port_init,
55 .yield = niagara_port_yield,
56 .reclaim = niagara_port_reclaim,
57 .write = niagara_port_write
58};
59
60#define POLL_INTERVAL 10000
61
62/**
63 * Virtual address mapped to the buffer shared with the kernel counterpart.
64 */
65static uintptr_t input_buffer_addr;
66
67/*
68 * Kernel counterpart of the driver pushes characters (it has read) here.
69 * Keep in sync with the definition from
70 * kernel/arch/sparc64/src/drivers/niagara.c.
71 */
72#define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
73
74typedef volatile struct {
75 uint64_t write_ptr;
76 uint64_t read_ptr;
77 char data[INPUT_BUFFER_SIZE];
78}
79 __attribute__ ((packed))
80 __attribute__ ((aligned(PAGE_SIZE)))
81 *input_buffer_t;
82
83/* virtual address of the shared buffer */
84static input_buffer_t input_buffer;
85
86static volatile bool polling_disabled = false;
87static void niagara_thread_impl(void *arg);
88
89/**
90 * Initializes the Niagara driver.
91 * Maps the shared buffer and creates the polling thread.
92 */
93static int niagara_port_init(void)
94{
95 sysarg_t paddr;
96 if (sysinfo_get_value("niagara.inbuf.address", &paddr) != EOK)
97 return -1;
98
99 input_buffer_addr = (uintptr_t) as_get_mappable_page(PAGE_SIZE);
100 int rc = physmem_map((void *) paddr, (void *) input_buffer_addr,
101 1, AS_AREA_READ | AS_AREA_WRITE);
102
103 if (rc != 0) {
104 printf("Niagara: uspace driver couldn't map physical memory: %d\n",
105 rc);
106 return rc;
107 }
108
109 input_buffer = (input_buffer_t) input_buffer_addr;
110
111 thread_id_t tid;
112 rc = thread_create(niagara_thread_impl, NULL, "kbd_poll", &tid);
113 if (rc != 0)
114 return rc;
115
116 return 0;
117}
118
119static void niagara_port_yield(void)
120{
121 polling_disabled = true;
122}
123
124static void niagara_port_reclaim(void)
125{
126 polling_disabled = false;
127}
128
129static void niagara_port_write(uint8_t data)
130{
131 (void) data;
132}
133
134/**
135 * Called regularly by the polling thread. Reads codes of all the
136 * pressed keys from the buffer.
137 */
138static void niagara_key_pressed(void)
139{
140 char c;
141
142 while (input_buffer->read_ptr != input_buffer->write_ptr) {
143 c = input_buffer->data[input_buffer->read_ptr];
144 input_buffer->read_ptr =
145 ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
146 kbd_push_scancode(c);
147 }
148}
149
150/**
151 * Thread to poll SGCN for keypresses.
152 */
153static void niagara_thread_impl(void *arg)
154{
155 (void) arg;
156
157 while (1) {
158 if (polling_disabled == false)
159 niagara_key_pressed();
160 usleep(POLL_INTERVAL);
161 }
162}
163/** @}
164 */
Note: See TracBrowser for help on using the repository browser.