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 | /** @addtogroup kbd_port
|
---|
30 | * @ingroup kbd
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | * @brief ADB keyboard port driver.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <ipc/adb.h>
|
---|
38 | #include <async.h>
|
---|
39 | #include <vfs/vfs.h>
|
---|
40 | #include <fcntl.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <loc.h>
|
---|
43 | #include "../input.h"
|
---|
44 | #include "../kbd_port.h"
|
---|
45 | #include "../kbd.h"
|
---|
46 |
|
---|
47 | static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
48 | static void adb_kbd_reg0_data(uint16_t data);
|
---|
49 |
|
---|
50 | static int adb_port_init(kbd_dev_t *);
|
---|
51 | static void adb_port_yield(void);
|
---|
52 | static void adb_port_reclaim(void);
|
---|
53 | static void adb_port_write(uint8_t data);
|
---|
54 |
|
---|
55 | kbd_port_ops_t adb_port = {
|
---|
56 | .init = adb_port_init,
|
---|
57 | .yield = adb_port_yield,
|
---|
58 | .reclaim = adb_port_reclaim,
|
---|
59 | .write = adb_port_write
|
---|
60 | };
|
---|
61 |
|
---|
62 | static kbd_dev_t *kbd_dev;
|
---|
63 | static async_sess_t *dev_sess;
|
---|
64 |
|
---|
65 | static int adb_port_init(kbd_dev_t *kdev)
|
---|
66 | {
|
---|
67 | kbd_dev = kdev;
|
---|
68 |
|
---|
69 | const char *dev = "adb/kbd";
|
---|
70 | service_id_t service_id;
|
---|
71 | int rc = loc_service_get_id(dev, &service_id, 0);
|
---|
72 | if (rc != EOK)
|
---|
73 | return rc;
|
---|
74 |
|
---|
75 | dev_sess = loc_service_connect(EXCHANGE_ATOMIC, service_id, 0);
|
---|
76 | if (dev_sess == NULL) {
|
---|
77 | printf("%s: Failed to connect to device\n", NAME);
|
---|
78 | return ENOENT;
|
---|
79 | }
|
---|
80 |
|
---|
81 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
---|
82 | if (exch == NULL) {
|
---|
83 | printf("%s: Failed starting exchange with device\n", NAME);
|
---|
84 | async_hangup(dev_sess);
|
---|
85 | return ENOMEM;
|
---|
86 | }
|
---|
87 |
|
---|
88 | rc = async_connect_to_me(exch, 0, 0, 0, kbd_port_events, NULL);
|
---|
89 | async_exchange_end(exch);
|
---|
90 | if (rc != EOK) {
|
---|
91 | printf("%s: Failed to create callback from device\n", NAME);
|
---|
92 | async_hangup(dev_sess);
|
---|
93 | return rc;
|
---|
94 | }
|
---|
95 |
|
---|
96 | return EOK;
|
---|
97 | }
|
---|
98 |
|
---|
99 | static void adb_port_yield(void)
|
---|
100 | {
|
---|
101 | }
|
---|
102 |
|
---|
103 | static void adb_port_reclaim(void)
|
---|
104 | {
|
---|
105 | }
|
---|
106 |
|
---|
107 | static void adb_port_write(uint8_t data)
|
---|
108 | {
|
---|
109 | /*async_msg_1(dev_phone, CHAR_WRITE_BYTE, data);*/
|
---|
110 | }
|
---|
111 |
|
---|
112 | static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
113 | {
|
---|
114 | /* Ignore parameters, the connection is already opened */
|
---|
115 | while (true) {
|
---|
116 |
|
---|
117 | ipc_call_t call;
|
---|
118 | ipc_callid_t callid = async_get_call(&call);
|
---|
119 |
|
---|
120 | int retval;
|
---|
121 |
|
---|
122 | if (!IPC_GET_IMETHOD(call)) {
|
---|
123 | /* TODO: Handle hangup */
|
---|
124 | return;
|
---|
125 | }
|
---|
126 |
|
---|
127 | switch (IPC_GET_IMETHOD(call)) {
|
---|
128 | case ADB_REG_NOTIF:
|
---|
129 | adb_kbd_reg0_data(IPC_GET_ARG1(call));
|
---|
130 | break;
|
---|
131 | default:
|
---|
132 | retval = ENOENT;
|
---|
133 | }
|
---|
134 | async_answer_0(callid, retval);
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | static void adb_kbd_reg0_data(uint16_t data)
|
---|
139 | {
|
---|
140 | uint8_t b0 = (data >> 8) & 0xff;
|
---|
141 | uint8_t b1 = data & 0xff;
|
---|
142 |
|
---|
143 | if (b0 != 0xff)
|
---|
144 | kbd_push_data(kbd_dev, b0);
|
---|
145 |
|
---|
146 | if (b1 != 0xff)
|
---|
147 | kbd_push_data(kbd_dev, b1);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * @}
|
---|
152 | */
|
---|