Rev 3925 | Rev 4346 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3925 | Rev 4343 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Pavel Rimsky |
2 | * Copyright (c) 2008 Pavel Rimsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup kbd_port |
29 | /** @addtogroup kbd_port |
30 | * @ingroup kbd |
30 | * @ingroup kbd |
31 | * @{ |
31 | * @{ |
32 | */ |
32 | */ |
33 | /** @file |
33 | /** @file |
34 | * @brief SGCN (Serengeti Console) keyboard port driver. |
34 | * @brief SGCN (Serengeti Console) keyboard port driver. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <as.h> |
37 | #include <as.h> |
38 | #include <ddi.h> |
38 | #include <ddi.h> |
39 | #include <ipc/ipc.h> |
39 | #include <ipc/ipc.h> |
40 | #include <async.h> |
40 | #include <async.h> |
41 | #include <kbd.h> |
41 | #include <kbd.h> |
42 | #include <kbd_port.h> |
42 | #include <kbd_port.h> |
43 | #include <sysinfo.h> |
43 | #include <sysinfo.h> |
44 | #include <stdio.h> |
44 | #include <stdio.h> |
45 | 45 | ||
46 | /** |
46 | /** |
47 | * SGCN buffer header. It is placed at the very beginning of the SGCN |
47 | * SGCN buffer header. It is placed at the very beginning of the SGCN |
48 | * buffer. |
48 | * buffer. |
49 | */ |
49 | */ |
50 | typedef struct { |
50 | typedef struct { |
51 | /** hard-wired to "CON" */ |
51 | /** hard-wired to "CON" */ |
52 | char magic[4]; |
52 | char magic[4]; |
53 | 53 | ||
54 | /** we don't need this */ |
54 | /** we don't need this */ |
55 | char unused[8]; |
55 | char unused[8]; |
56 | 56 | ||
57 | /** offset within the SGCN buffer of the input buffer start */ |
57 | /** offset within the SGCN buffer of the input buffer start */ |
58 | uint32_t in_begin; |
58 | uint32_t in_begin; |
59 | 59 | ||
60 | /** offset within the SGCN buffer of the input buffer end */ |
60 | /** offset within the SGCN buffer of the input buffer end */ |
61 | uint32_t in_end; |
61 | uint32_t in_end; |
62 | 62 | ||
63 | /** offset within the SGCN buffer of the input buffer read pointer */ |
63 | /** offset within the SGCN buffer of the input buffer read pointer */ |
64 | uint32_t in_rdptr; |
64 | uint32_t in_rdptr; |
65 | 65 | ||
66 | /** offset within the SGCN buffer of the input buffer write pointer */ |
66 | /** offset within the SGCN buffer of the input buffer write pointer */ |
67 | uint32_t in_wrptr; |
67 | uint32_t in_wrptr; |
68 | } __attribute__ ((packed)) sgcn_buffer_header_t; |
68 | } __attribute__ ((packed)) sgcn_buffer_header_t; |
69 | 69 | ||
70 | /* |
70 | /* |
71 | * Returns a pointer to the object of a given type which is placed at the given |
71 | * Returns a pointer to the object of a given type which is placed at the given |
72 | * offset from the console buffer beginning. |
72 | * offset from the console buffer beginning. |
73 | */ |
73 | */ |
74 | #define SGCN_BUFFER(type, offset) \ |
74 | #define SGCN_BUFFER(type, offset) \ |
75 | ((type *) (sram_virt_addr + sram_buffer_offset + (offset))) |
75 | ((type *) (sram_virt_addr + sram_buffer_offset + (offset))) |
76 | 76 | ||
77 | /** Returns a pointer to the console buffer header. */ |
77 | /** Returns a pointer to the console buffer header. */ |
78 | #define SGCN_BUFFER_HEADER (SGCN_BUFFER(sgcn_buffer_header_t, 0)) |
78 | #define SGCN_BUFFER_HEADER (SGCN_BUFFER(sgcn_buffer_header_t, 0)) |
79 | 79 | ||
80 | /** |
80 | /** |
81 | * Virtual address mapped to SRAM. |
81 | * Virtual address mapped to SRAM. |
82 | */ |
82 | */ |
83 | static uintptr_t sram_virt_addr; |
83 | static uintptr_t sram_virt_addr; |
84 | 84 | ||
85 | /** |
85 | /** |
86 | * SGCN buffer offset within SGCN. |
86 | * SGCN buffer offset within SGCN. |
87 | */ |
87 | */ |
88 | static uintptr_t sram_buffer_offset; |
88 | static uintptr_t sram_buffer_offset; |
89 | 89 | ||
90 | static void sgcn_irq_handler(ipc_callid_t iid, ipc_call_t *call); |
90 | static void sgcn_irq_handler(ipc_callid_t iid, ipc_call_t *call); |
91 | 91 | ||
92 | 92 | ||
93 | /** |
93 | /** |
94 | * Initializes the SGCN driver. |
94 | * Initializes the SGCN driver. |
95 | * Maps the physical memory (SRAM) and registers the interrupt. |
95 | * Maps the physical memory (SRAM) and registers the interrupt. |
96 | */ |
96 | */ |
97 | int kbd_port_init(void) |
97 | int kbd_port_init(void) |
98 | { |
98 | { |
99 | async_set_interrupt_received(sgcn_irq_handler); |
99 | async_set_interrupt_received(sgcn_irq_handler); |
100 | sram_virt_addr = (uintptr_t) as_get_mappable_page(sysinfo_value("sram.area.size")); |
100 | sram_virt_addr = (uintptr_t) as_get_mappable_page(sysinfo_value("sram.area.size")); |
101 | if (physmem_map((void *) sysinfo_value("sram.address.physical"), |
101 | if (physmem_map((void *) sysinfo_value("sram.address.physical"), |
102 | (void *) sram_virt_addr, sysinfo_value("sram.area.size") / PAGE_SIZE, |
102 | (void *) sram_virt_addr, sysinfo_value("sram.area.size") / PAGE_SIZE, |
103 | AS_AREA_READ | AS_AREA_WRITE) != 0) { |
103 | AS_AREA_READ | AS_AREA_WRITE) != 0) { |
104 | printf("SGCN: uspace driver could not map physical memory."); |
104 | printf("SGCN: uspace driver could not map physical memory."); |
105 | return -1; |
105 | return -1; |
106 | } |
106 | } |
107 | 107 | ||
108 | sram_buffer_offset = sysinfo_value("sram.buffer.offset"); |
108 | sram_buffer_offset = sysinfo_value("sram.buffer.offset"); |
109 | ipc_register_irq(sysinfo_value("kbd.inr"), sysinfo_value("kbd.devno"), |
109 | ipc_register_irq(sysinfo_value("kbd.inr"), sysinfo_value("kbd.devno"), |
110 | 0, (void *) 0); |
110 | 0, (void *) 0); |
111 | return 0; |
111 | return 0; |
112 | } |
112 | } |
113 | 113 | ||
114 | /** |
114 | /** |
115 | * Handler of the "key pressed" event. Reads codes of all the pressed keys from |
115 | * Handler of the "key pressed" event. Reads codes of all the pressed keys from |
116 | * the buffer. |
116 | * the buffer. |
117 | */ |
117 | */ |
118 | static void sgcn_irq_handler(ipc_callid_t iid, ipc_call_t *call) |
118 | static void sgcn_irq_handler(ipc_callid_t iid, ipc_call_t *call) |
119 | { |
119 | { |
120 | char c; |
120 | char c; |
121 | 121 | ||
122 | uint32_t begin = SGCN_BUFFER_HEADER->in_begin; |
122 | uint32_t begin = SGCN_BUFFER_HEADER->in_begin; |
123 | uint32_t end = SGCN_BUFFER_HEADER->in_end; |
123 | uint32_t end = SGCN_BUFFER_HEADER->in_end; |
124 | uint32_t size = end - begin; |
124 | uint32_t size = end - begin; |
125 | 125 | ||
126 | volatile char *buf_ptr = (volatile char *) |
126 | volatile char *buf_ptr = (volatile char *) |
127 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr); |
127 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr); |
128 | volatile uint32_t *in_wrptr_ptr = &(SGCN_BUFFER_HEADER->in_wrptr); |
128 | volatile uint32_t *in_wrptr_ptr = &(SGCN_BUFFER_HEADER->in_wrptr); |
129 | volatile uint32_t *in_rdptr_ptr = &(SGCN_BUFFER_HEADER->in_rdptr); |
129 | volatile uint32_t *in_rdptr_ptr = &(SGCN_BUFFER_HEADER->in_rdptr); |
130 | 130 | ||
131 | while (*in_rdptr_ptr != *in_wrptr_ptr) { |
131 | while (*in_rdptr_ptr != *in_wrptr_ptr) { |
132 | c = *buf_ptr; |
132 | c = *buf_ptr; |
133 | *in_rdptr_ptr = (((*in_rdptr_ptr) - begin + 1) % size) + begin; |
133 | *in_rdptr_ptr = (((*in_rdptr_ptr) - begin + 1) % size) + begin; |
134 | buf_ptr = (volatile char *) |
134 | buf_ptr = (volatile char *) |
135 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr); |
135 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr); |
136 | kbd_push_scancode(c); |
136 | kbd_push_scancode(c); |
137 | } |
137 | } |
138 | } |
138 | } |
139 | 139 | ||
140 | /** @} |
140 | /** @} |
141 | */ |
141 | */ |
142 | 142 |