source: mainline/uspace/drv/hid/adb-kbd/adb-kbd.c@ 15c5418

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 15c5418 was 15c5418, checked in by Jiri Svoboda <jiri@…>, 8 years ago

chardev_open, chardev_close.

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Svoboda
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/** @file
30 * @brief ADB keyboard port driver.
31 */
32
33#include <async.h>
34#include <ddf/log.h>
35#include <errno.h>
36#include <io/chardev.h>
37#include <io/console.h>
38#include <ipc/adb.h>
39#include <ipc/kbdev.h>
40#include <loc.h>
41#include <vfs/vfs.h>
42
43#include "adb-kbd.h"
44#include "ctl.h"
45
46static void adb_kbd_events(ipc_callid_t, ipc_call_t *, void *);
47static void adb_kbd_reg0_data(adb_kbd_t *, uint16_t);
48static void adb_kbd_conn(ipc_callid_t, ipc_call_t *, void *);
49
50/** Add ADB keyboard device */
51int adb_kbd_add(adb_kbd_t *kbd)
52{
53 int rc;
54 bool bound = false;
55
56 kbd->fun = ddf_fun_create(kbd->dev, fun_exposed, "a");
57 if (kbd->fun == NULL) {
58 ddf_msg(LVL_ERROR, "Error creating function");
59 rc = ENOMEM;
60 goto error;
61 }
62
63 kbd->parent_sess = ddf_dev_parent_sess_get(kbd->dev);
64 if (kbd->parent_sess == NULL) {
65 ddf_msg(LVL_ERROR, "Error connecting parent driver");
66 rc = EIO;
67 goto error;
68 }
69
70 async_exch_t *exch = async_exchange_begin(kbd->parent_sess);
71 if (exch == NULL) {
72 ddf_msg(LVL_ERROR, "Error starting exchange with parent");
73 rc = ENOMEM;
74 goto error;
75 }
76
77 port_id_t port;
78 rc = async_create_callback_port(exch, INTERFACE_ADB_CB, 0, 0,
79 adb_kbd_events, kbd, &port);
80
81 async_exchange_end(exch);
82 if (rc != EOK) {
83 ddf_msg(LVL_ERROR, "Error creating callback from device");
84 goto error;
85 }
86
87 ddf_fun_set_conn_handler(kbd->fun, adb_kbd_conn);
88
89 rc = ddf_fun_bind(kbd->fun);
90 if (rc != EOK) {
91 ddf_msg(LVL_ERROR, "Error binding function");
92 goto error;
93 }
94
95 bound = true;
96
97 rc = ddf_fun_add_to_category(kbd->fun, "keyboard");
98 if (rc != EOK) {
99 ddf_msg(LVL_ERROR, "Error adding function to category");
100 goto error;
101 }
102
103 return EOK;
104error:
105 if (bound)
106 ddf_fun_unbind(kbd->fun);
107
108 if (kbd->parent_sess != NULL) {
109 async_hangup(kbd->parent_sess);
110 kbd->parent_sess = NULL;
111 }
112
113 if (kbd->fun != NULL) {
114 ddf_fun_destroy(kbd->fun);
115 kbd->fun = NULL;
116 }
117
118 return rc;
119}
120
121/** Remove ADB keyboard device */
122int adb_kbd_remove(adb_kbd_t *con)
123{
124 return ENOTSUP;
125}
126
127/** ADB keyboard device gone */
128int adb_kbd_gone(adb_kbd_t *con)
129{
130 return ENOTSUP;
131}
132
133static void adb_kbd_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)
134{
135 adb_kbd_t *kbd = (adb_kbd_t *) arg;
136
137 /* Ignore parameters, the connection is already opened */
138 while (true) {
139
140 ipc_call_t call;
141 ipc_callid_t callid = async_get_call(&call);
142
143 int retval = EOK;
144
145 if (!IPC_GET_IMETHOD(call)) {
146 /* TODO: Handle hangup */
147 return;
148 }
149
150 switch (IPC_GET_IMETHOD(call)) {
151 case ADB_REG_NOTIF:
152 adb_kbd_reg0_data(kbd, IPC_GET_ARG1(call));
153 break;
154 default:
155 retval = ENOENT;
156 }
157 async_answer_0(callid, retval);
158 }
159}
160
161static void adb_kbd_data(adb_kbd_t *kbd, uint8_t b)
162{
163 kbd_event_type_t etype;
164 unsigned int key;
165 int rc;
166
167 rc = adb_kbd_key_translate(b, &etype, &key);
168 if (rc != EOK)
169 return;
170
171 if (kbd->client_sess == NULL)
172 return;
173
174 async_exch_t *exch = async_exchange_begin(kbd->client_sess);
175 async_msg_4(exch, KBDEV_EVENT, etype, key, 0, 0);
176 async_exchange_end(exch);
177}
178
179static void adb_kbd_reg0_data(adb_kbd_t *kbd, uint16_t data)
180{
181 uint8_t b0 = (data >> 8) & 0xff;
182 uint8_t b1 = data & 0xff;
183
184 if (b0 != 0xff)
185 adb_kbd_data(kbd, b0);
186
187 if (b1 != 0xff)
188 adb_kbd_data(kbd, b1);
189 (void)b0; (void)b1;
190}
191
192/** Handle client connection */
193static void adb_kbd_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
194{
195 ipc_callid_t callid;
196 ipc_call_t call;
197 sysarg_t method;
198 adb_kbd_t *kbd;
199
200 /*
201 * Answer the first IPC_M_CONNECT_ME_TO call.
202 */
203 async_answer_0(iid, EOK);
204
205 kbd = (adb_kbd_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg));
206
207 while (true) {
208 callid = async_get_call(&call);
209 method = IPC_GET_IMETHOD(call);
210
211 if (!method) {
212 /* The other side has hung up. */
213 async_answer_0(callid, EOK);
214 return;
215 }
216
217 async_sess_t *sess =
218 async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
219 if (sess != NULL) {
220 kbd->client_sess = sess;
221 async_answer_0(callid, EOK);
222 } else {
223 async_answer_0(callid, EINVAL);
224 }
225 }
226}
227
228/**
229 * @}
230 */
Note: See TracBrowser for help on using the repository browser.