source: mainline/uspace/srv/hid/kbd/port/sgcn.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@…>, 14 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: 5.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 SGCN (Serengeti 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 sgcn_port_init(void);
49static void sgcn_port_yield(void);
50static void sgcn_port_reclaim(void);
51static void sgcn_port_write(uint8_t data);
52
53kbd_port_ops_t sgcn_port = {
54 .init = sgcn_port_init,
55 .yield = sgcn_port_yield,
56 .reclaim = sgcn_port_reclaim,
57 .write = sgcn_port_write
58};
59
60#define POLL_INTERVAL 10000
61
62/**
63 * SGCN buffer header. It is placed at the very beginning of the SGCN
64 * buffer.
65 */
66typedef struct {
67 /** hard-wired to "CON" */
68 char magic[4];
69
70 /** we don't need this */
71 char unused[8];
72
73 /** offset within the SGCN buffer of the input buffer start */
74 uint32_t in_begin;
75
76 /** offset within the SGCN buffer of the input buffer end */
77 uint32_t in_end;
78
79 /** offset within the SGCN buffer of the input buffer read pointer */
80 uint32_t in_rdptr;
81
82 /** offset within the SGCN buffer of the input buffer write pointer */
83 uint32_t in_wrptr;
84} __attribute__ ((packed)) sgcn_buffer_header_t;
85
86/*
87 * Returns a pointer to the object of a given type which is placed at the given
88 * offset from the console buffer beginning.
89 */
90#define SGCN_BUFFER(type, offset) \
91 ((type *) (sram_virt_addr + sram_buffer_offset + (offset)))
92
93/** Returns a pointer to the console buffer header. */
94#define SGCN_BUFFER_HEADER (SGCN_BUFFER(sgcn_buffer_header_t, 0))
95
96/**
97 * Virtual address mapped to SRAM.
98 */
99static uintptr_t sram_virt_addr;
100
101/**
102 * SGCN buffer offset within SGCN.
103 */
104static uintptr_t sram_buffer_offset;
105
106/* polling thread */
107static void sgcn_thread_impl(void *arg);
108
109static volatile bool polling_disabled = false;
110
111/**
112 * Initializes the SGCN driver.
113 * Maps the physical memory (SRAM) and creates the polling thread.
114 */
115static int sgcn_port_init(void)
116{
117 sysarg_t sram_paddr;
118 if (sysinfo_get_value("sram.address.physical", &sram_paddr) != EOK)
119 return -1;
120
121 sysarg_t sram_size;
122 if (sysinfo_get_value("sram.area.size", &sram_size) != EOK)
123 return -1;
124
125 if (sysinfo_get_value("sram.buffer.offset", &sram_buffer_offset) != EOK)
126 sram_buffer_offset = 0;
127
128 sram_virt_addr = (uintptr_t) as_get_mappable_page(sram_size);
129
130 if (physmem_map((void *) sram_paddr, (void *) sram_virt_addr,
131 sram_size / PAGE_SIZE, AS_AREA_READ | AS_AREA_WRITE) != 0) {
132 printf("SGCN: uspace driver could not map physical memory.");
133 return -1;
134 }
135
136 thread_id_t tid;
137 int rc = thread_create(sgcn_thread_impl, NULL, "kbd_poll", &tid);
138 if (rc != 0)
139 return rc;
140
141 return 0;
142}
143
144static void sgcn_port_yield(void)
145{
146 polling_disabled = true;
147}
148
149static void sgcn_port_reclaim(void)
150{
151 polling_disabled = false;
152}
153
154static void sgcn_port_write(uint8_t data)
155{
156 (void) data;
157}
158
159/**
160 * Handler of the "key pressed" event. Reads codes of all the pressed keys from
161 * the buffer.
162 */
163static void sgcn_key_pressed(void)
164{
165 char c;
166
167 uint32_t begin = SGCN_BUFFER_HEADER->in_begin;
168 uint32_t end = SGCN_BUFFER_HEADER->in_end;
169 uint32_t size = end - begin;
170
171 volatile char *buf_ptr = (volatile char *)
172 SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr);
173 volatile uint32_t *in_wrptr_ptr = &(SGCN_BUFFER_HEADER->in_wrptr);
174 volatile uint32_t *in_rdptr_ptr = &(SGCN_BUFFER_HEADER->in_rdptr);
175
176 while (*in_rdptr_ptr != *in_wrptr_ptr) {
177 c = *buf_ptr;
178 *in_rdptr_ptr = (((*in_rdptr_ptr) - begin + 1) % size) + begin;
179 buf_ptr = (volatile char *)
180 SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr);
181 kbd_push_scancode(c);
182 }
183}
184
185/**
186 * Thread to poll SGCN for keypresses.
187 */
188static void sgcn_thread_impl(void *arg)
189{
190 (void) arg;
191
192 while (1) {
193 if (polling_disabled == false)
194 sgcn_key_pressed();
195 usleep(POLL_INTERVAL);
196 }
197}
198
199/** @}
200 */
Note: See TracBrowser for help on using the repository browser.