source: mainline/uspace/srv/hid/input/port/niagara.c@ bf9cb2f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bf9cb2f was bf9cb2f, checked in by Martin Decky <martin@…>, 11 years ago

complete the desired API semantics of physmem_map() and dmamem_map_anonymous() to be compatible with as_area_create()
(the user is allowed to request a specific virtual memory base address, the kernel uses an available base address if AS_AREA_ANY is used)

  • Property mode set to 100644
File size: 4.1 KB
Line 
1/*
2 * Copyright (c) 2008 Pavel Rimsky
3 * Copyright (c) 2011 Jiri Svoboda
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 * @{
33 */
34/** @file
35 * @brief Niagara console keyboard port driver.
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>
44#include <stdbool.h>
45#include <errno.h>
46#include "../kbd_port.h"
47#include "../kbd.h"
48
49static int niagara_port_init(kbd_dev_t *);
50static void niagara_port_yield(void);
51static void niagara_port_reclaim(void);
52static void niagara_port_write(uint8_t data);
53
54kbd_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
61static kbd_dev_t *kbd_dev;
62
63#define POLL_INTERVAL 10000
64
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 */
70#define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
71
72typedef volatile struct {
73 uint64_t write_ptr;
74 uint64_t read_ptr;
75 char data[INPUT_BUFFER_SIZE];
76} __attribute__((packed)) __attribute__((aligned(PAGE_SIZE))) *input_buffer_t;
77
78/* virtual address of the shared buffer */
79static input_buffer_t input_buffer = (input_buffer_t) AS_AREA_ANY;
80
81static volatile bool polling_disabled = false;
82static void niagara_thread_impl(void *arg);
83
84/**
85 * Initializes the Niagara driver.
86 * Maps the shared buffer and creates the polling thread.
87 */
88static int niagara_port_init(kbd_dev_t *kdev)
89{
90 kbd_dev = kdev;
91
92 sysarg_t paddr;
93 if (sysinfo_get_value("niagara.inbuf.address", &paddr) != EOK)
94 return -1;
95
96 int rc = physmem_map(paddr, 1, AS_AREA_READ | AS_AREA_WRITE,
97 (void *) &input_buffer);
98 if (rc != 0) {
99 printf("Niagara: uspace driver couldn't map physical memory: %d\n",
100 rc);
101 return rc;
102 }
103
104 thread_id_t tid;
105 rc = thread_create(niagara_thread_impl, NULL, "kbd_poll", &tid);
106 if (rc != 0)
107 return rc;
108
109 return 0;
110}
111
112static void niagara_port_yield(void)
113{
114 polling_disabled = true;
115}
116
117static void niagara_port_reclaim(void)
118{
119 polling_disabled = false;
120}
121
122static void niagara_port_write(uint8_t data)
123{
124 (void) data;
125}
126
127/**
128 * Called regularly by the polling thread. Reads codes of all the
129 * pressed keys from the buffer.
130 */
131static void niagara_key_pressed(void)
132{
133 char c;
134
135 while (input_buffer->read_ptr != input_buffer->write_ptr) {
136 c = input_buffer->data[input_buffer->read_ptr];
137 input_buffer->read_ptr =
138 ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
139 kbd_push_data(kbd_dev, c);
140 }
141}
142
143/**
144 * Thread to poll Niagara console for keypresses.
145 */
146static void niagara_thread_impl(void *arg)
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 */
Note: See TracBrowser for help on using the repository browser.