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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e27e36e was 582a0b8, checked in by Jakub Jermar <jakub@…>, 8 years ago

Remove unistd.h

  • Rename usleep() and sleep() to thread_usleep() and thread_sleep() and move to thread.[hc].
  • Include stddef.h in order to provide NULL.
  • Move getpagesize() to libposix.
  • Sync uspace/dist/src/c/demos with originals.
  • Property mode set to 100644
File size: 3.8 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_write(uint8_t data);
51
52kbd_port_ops_t niagara_port = {
53 .init = niagara_port_init,
54 .write = niagara_port_write
55};
56
57static kbd_dev_t *kbd_dev;
58
59#define POLL_INTERVAL 10000
60
61/*
62 * Kernel counterpart of the driver pushes characters (it has read) here.
63 * Keep in sync with the definition from
64 * kernel/arch/sparc64/src/drivers/niagara.c.
65 */
66#define INPUT_BUFFER_SIZE ((PAGE_SIZE) - 2 * 8)
67
68typedef volatile struct {
69 uint64_t write_ptr;
70 uint64_t read_ptr;
71 char data[INPUT_BUFFER_SIZE];
72} __attribute__((packed)) __attribute__((aligned(PAGE_SIZE))) *input_buffer_t;
73
74/* virtual address of the shared buffer */
75static input_buffer_t input_buffer = (input_buffer_t) AS_AREA_ANY;
76
77static void niagara_thread_impl(void *arg);
78
79/**
80 * Initializes the Niagara driver.
81 * Maps the shared buffer and creates the polling thread.
82 */
83static int niagara_port_init(kbd_dev_t *kdev)
84{
85 kbd_dev = kdev;
86
87 sysarg_t paddr;
88 if (sysinfo_get_value("niagara.inbuf.address", &paddr) != EOK)
89 return -1;
90
91 int rc = physmem_map(paddr, 1, AS_AREA_READ | AS_AREA_WRITE,
92 (void *) &input_buffer);
93 if (rc != 0) {
94 printf("Niagara: uspace driver couldn't map physical memory: %d\n",
95 rc);
96 return rc;
97 }
98
99 thread_id_t tid;
100 rc = thread_create(niagara_thread_impl, NULL, "kbd_poll", &tid);
101 if (rc != 0)
102 return rc;
103
104 return 0;
105}
106
107static void niagara_port_write(uint8_t data)
108{
109 (void) data;
110}
111
112/**
113 * Called regularly by the polling thread. Reads codes of all the
114 * pressed keys from the buffer.
115 */
116static void niagara_key_pressed(void)
117{
118 char c;
119
120 while (input_buffer->read_ptr != input_buffer->write_ptr) {
121 c = input_buffer->data[input_buffer->read_ptr];
122 input_buffer->read_ptr =
123 ((input_buffer->read_ptr) + 1) % INPUT_BUFFER_SIZE;
124 kbd_push_data(kbd_dev, c);
125 }
126}
127
128/**
129 * Thread to poll Niagara console for keypresses.
130 */
131static void niagara_thread_impl(void *arg)
132{
133 (void) arg;
134
135 while (1) {
136 niagara_key_pressed();
137 thread_usleep(POLL_INTERVAL);
138 }
139}
140/** @}
141 */
Note: See TracBrowser for help on using the repository browser.