paul@3 | 1 | /* |
paul@3 | 2 | * Provide access to keypad events on the configured device. |
paul@3 | 3 | * |
paul@3 | 4 | * (c) 2018 Paul Boddie <paul@boddie.org.uk> |
paul@3 | 5 | * |
paul@3 | 6 | * This program is free software; you can redistribute it and/or |
paul@3 | 7 | * modify it under the terms of the GNU General Public License as |
paul@3 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@3 | 9 | * the License, or (at your option) any later version. |
paul@3 | 10 | * |
paul@3 | 11 | * This program is distributed in the hope that it will be useful, |
paul@3 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@3 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@3 | 14 | * GNU General Public License for more details. |
paul@3 | 15 | * |
paul@3 | 16 | * You should have received a copy of the GNU General Public License |
paul@3 | 17 | * along with this program; if not, write to the Free Software |
paul@3 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@3 | 19 | * Boston, MA 02110-1301, USA |
paul@3 | 20 | */ |
paul@3 | 21 | |
paul@3 | 22 | #include <l4/devices/dataspace.h> |
paul@3 | 23 | #include "input-event-client.h" |
paul@3 | 24 | #include "input-event-server.h" |
paul@3 | 25 | #include "input-event-ops.h" |
paul@3 | 26 | #include "input-keypad-client.h" |
paul@3 | 27 | |
paul@3 | 28 | #include <l4/cxx/ipc_stream> |
paul@3 | 29 | #include <l4/re/dataspace> |
paul@3 | 30 | #include <l4/re/env> |
paul@3 | 31 | #include <l4/re/event> |
paul@17 | 32 | #include <l4/re/util/br_manager> |
paul@3 | 33 | #include <l4/re/util/cap_alloc> |
paul@3 | 34 | #include <l4/re/util/object_registry> |
paul@3 | 35 | #include <l4/sys/capability> |
paul@3 | 36 | #include <l4/sys/icu> |
paul@3 | 37 | #include <l4/sys/kip.h> |
paul@3 | 38 | #include <l4/sys/types.h> |
paul@3 | 39 | #include <l4/util/util.h> |
paul@3 | 40 | |
paul@17 | 41 | /* Extract a fpage from the message buffers. Provide a Snd_fpage reference for |
paul@17 | 42 | modification. */ |
paul@17 | 43 | |
paul@17 | 44 | static |
paul@17 | 45 | void get_fpage(L4::Ipc::Iostream &ios, L4::Ipc::Snd_item &fpage) |
paul@17 | 46 | { |
paul@17 | 47 | l4_msgtag_t tag = ios.tag(); |
paul@17 | 48 | |
paul@17 | 49 | if (!tag.items()) |
paul@17 | 50 | return; |
paul@17 | 51 | |
paul@17 | 52 | /* Extract the fpage details directly since the stream operator doesn't seem |
paul@17 | 53 | to work. */ |
paul@17 | 54 | |
paul@17 | 55 | l4_msg_regs_t *m = l4_utcb_mr_u(l4_utcb()); |
paul@17 | 56 | l4_umword_t b = m->mr[tag.words()], d = m->mr[tag.words() + 1]; |
paul@17 | 57 | fpage = L4::Ipc::Snd_item(b, d); |
paul@17 | 58 | } |
paul@17 | 59 | |
paul@3 | 60 | /* Handle invocations. */ |
paul@3 | 61 | |
paul@3 | 62 | int |
paul@3 | 63 | Input_event_server::dispatch(l4_umword_t obj, L4::Ipc::Iostream &ios) |
paul@3 | 64 | { |
paul@3 | 65 | l4_msgtag_t tag; |
paul@15 | 66 | l4_umword_t op; |
paul@17 | 67 | unsigned irqnum; |
paul@17 | 68 | L4::Ipc::Snd_fpage fpage; |
paul@3 | 69 | |
paul@3 | 70 | (void) obj; |
paul@3 | 71 | ios >> tag; |
paul@3 | 72 | |
paul@3 | 73 | switch (tag.label()) |
paul@3 | 74 | { |
paul@3 | 75 | case L4::Meta::Protocol: |
paul@3 | 76 | return L4::Util::handle_meta_request<Input_event_interface>(ios); |
paul@3 | 77 | |
paul@15 | 78 | case L4RE_PROTO_EVENT: |
paul@15 | 79 | ios >> op; |
paul@15 | 80 | switch (op) |
paul@15 | 81 | { |
paul@15 | 82 | /* Reset the buffer state and return the buffer memory capability. */ |
paul@15 | 83 | |
paul@15 | 84 | case Input_event_op_get_buffer: |
paul@15 | 85 | _events.reset(); |
paul@15 | 86 | ios << _mem; |
paul@15 | 87 | return L4_EOK; |
paul@3 | 88 | |
paul@15 | 89 | default: |
paul@17 | 90 | return -L4_EINVAL; |
paul@15 | 91 | } |
paul@3 | 92 | |
paul@15 | 93 | case L4_PROTO_IRQ: |
paul@15 | 94 | ios >> op; |
paul@15 | 95 | switch (op) |
paul@15 | 96 | { |
paul@15 | 97 | /* Just return the interrupt capability. */ |
paul@3 | 98 | |
paul@15 | 99 | case Input_event_op_bind: |
paul@17 | 100 | ios >> irqnum; |
paul@17 | 101 | get_fpage(ios, fpage); |
paul@17 | 102 | |
paul@17 | 103 | if (!fpage.cap_received()) |
paul@17 | 104 | return -L4_EINVAL; |
paul@17 | 105 | |
paul@17 | 106 | _irq = server_iface()->rcv_cap<L4::Irq>(0); |
paul@17 | 107 | return server_iface()->realloc_rcv_cap(0); |
paul@15 | 108 | |
paul@15 | 109 | default: |
paul@17 | 110 | return -L4_EINVAL; |
paul@15 | 111 | } |
paul@3 | 112 | |
paul@3 | 113 | default: |
paul@3 | 114 | return -L4_EBADPROTO; |
paul@3 | 115 | } |
paul@3 | 116 | } |
paul@3 | 117 | |
paul@3 | 118 | |
paul@3 | 119 | |
paul@3 | 120 | /* Event sending methods. */ |
paul@3 | 121 | |
paul@3 | 122 | void |
paul@3 | 123 | Input_event_server::send_event(int type, int code, int value) |
paul@3 | 124 | { |
paul@3 | 125 | L4Re::Event_buffer::Event event; |
paul@3 | 126 | |
paul@3 | 127 | event.time = l4_kip_clock(l4re_kip()); |
paul@3 | 128 | event.payload.stream_id = 0; |
paul@3 | 129 | event.payload.type = type; |
paul@3 | 130 | event.payload.code = code; |
paul@3 | 131 | event.payload.value = value; |
paul@3 | 132 | |
paul@3 | 133 | /* Queue the event and trigger the interrupt. */ |
paul@3 | 134 | |
paul@3 | 135 | _events.put(event); |
paul@3 | 136 | _irq->trigger(); |
paul@3 | 137 | } |
paul@3 | 138 | |
paul@3 | 139 | |
paul@3 | 140 | |
paul@3 | 141 | static void handler(Input_event event, void *priv) |
paul@3 | 142 | { |
paul@3 | 143 | Input_event_server *server = (Input_event_server *) priv; |
paul@3 | 144 | |
paul@3 | 145 | server->send_event(event.type, event.code, event.value); |
paul@3 | 146 | } |
paul@3 | 147 | |
paul@3 | 148 | |
paul@3 | 149 | |
paul@17 | 150 | static L4Re::Util::Registry_server<L4Re::Util::Br_manager_hooks> server; |
paul@3 | 151 | |
paul@3 | 152 | |
paul@3 | 153 | |
paul@3 | 154 | int run(void) |
paul@3 | 155 | { |
paul@3 | 156 | Keypad *keypad = Keypad::get_keypad(); |
paul@3 | 157 | Input_keypad_client client(keypad); |
paul@3 | 158 | void *buffer; |
paul@3 | 159 | |
paul@3 | 160 | // Memory allocation capability for the event data. |
paul@3 | 161 | |
paul@3 | 162 | L4::Cap<L4Re::Dataspace> mem = allocate_data(L4_PAGESIZE, &buffer); |
paul@3 | 163 | if (!mem.is_valid()) return 1; |
paul@3 | 164 | |
paul@3 | 165 | /* Obtain a capability for the interrupt. */ |
paul@3 | 166 | |
paul@3 | 167 | L4::Cap<L4::Irq> irq = L4Re::Util::cap_alloc.alloc<L4::Irq>(); |
paul@3 | 168 | if (!irq.is_valid()) return 1; |
paul@3 | 169 | |
paul@3 | 170 | // Event buffer for the data. |
paul@3 | 171 | |
paul@3 | 172 | L4Re::Event_buffer events(buffer, L4_PAGESIZE); |
paul@3 | 173 | |
paul@3 | 174 | // Initialise and register a server object. |
paul@3 | 175 | |
paul@17 | 176 | Input_event_server server_obj(mem, events); |
paul@3 | 177 | server.registry()->register_obj(&server_obj, "ev"); |
paul@3 | 178 | |
paul@3 | 179 | // Attach the event handler and wait for events. |
paul@3 | 180 | |
paul@3 | 181 | client.attach(handler, (void *) &server_obj); |
paul@3 | 182 | |
paul@3 | 183 | // Enter the IPC server loop. |
paul@3 | 184 | |
paul@3 | 185 | server.loop(); |
paul@3 | 186 | return 0; |
paul@3 | 187 | } |