[a2afd8f] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Martin Decky
|
---|
| 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 mouse driver
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #include <async.h>
|
---|
| 34 | #include <ddf/log.h>
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <ipc/adb.h>
|
---|
| 37 | #include <ipc/mouseev.h>
|
---|
| 38 | #include <loc.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 |
|
---|
| 41 | #include "adb-mouse.h"
|
---|
| 42 |
|
---|
[984a9ba] | 43 | static void adb_mouse_conn(ipc_call_t *, void *);
|
---|
[a2afd8f] | 44 |
|
---|
| 45 | static void adb_mouse_event_button(adb_mouse_t *mouse, int bnum, int bpress)
|
---|
| 46 | {
|
---|
| 47 | async_exch_t *exch = async_exchange_begin(mouse->client_sess);
|
---|
| 48 | async_msg_2(exch, MOUSEEV_BUTTON_EVENT, bnum, bpress);
|
---|
| 49 | async_exchange_end(exch);
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | static void adb_mouse_event_move(adb_mouse_t *mouse, int dx, int dy, int dz)
|
---|
| 53 | {
|
---|
| 54 | async_exch_t *exch = async_exchange_begin(mouse->client_sess);
|
---|
| 55 | async_msg_3(exch, MOUSEEV_MOVE_EVENT, dx, dy, dz);
|
---|
| 56 | async_exchange_end(exch);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /** Process mouse data */
|
---|
| 60 | static void adb_mouse_data(adb_mouse_t *mouse, sysarg_t data)
|
---|
| 61 | {
|
---|
| 62 | bool b1, b2;
|
---|
| 63 | uint16_t udx, udy;
|
---|
| 64 | int dx, dy;
|
---|
| 65 |
|
---|
| 66 | /* Extract fields. */
|
---|
| 67 | b1 = ((data >> 15) & 1) == 0;
|
---|
| 68 | udy = (data >> 8) & 0x7f;
|
---|
| 69 | b2 = ((data >> 7) & 1) == 0;
|
---|
| 70 | udx = data & 0x7f;
|
---|
| 71 |
|
---|
| 72 | /* Decode 7-bit two's complement signed values. */
|
---|
| 73 | dx = (udx & 0x40) ? (udx - 0x80) : udx;
|
---|
| 74 | dy = (udy & 0x40) ? (udy - 0x80) : udy;
|
---|
| 75 |
|
---|
| 76 | if (b1 != mouse->b1_pressed) {
|
---|
| 77 | adb_mouse_event_button(mouse, 1, b1);
|
---|
| 78 | mouse->b1_pressed = b1;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | if (b2 != mouse->b2_pressed) {
|
---|
| 82 | adb_mouse_event_button(mouse, 2, b2);
|
---|
| 83 | mouse->b2_pressed = b2;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | if (dx != 0 || dy != 0)
|
---|
| 87 | adb_mouse_event_move(mouse, dx, dy, 0);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[984a9ba] | 90 | static void adb_mouse_events(ipc_call_t *icall, void *arg)
|
---|
[a2afd8f] | 91 | {
|
---|
| 92 | adb_mouse_t *mouse = (adb_mouse_t *) arg;
|
---|
| 93 |
|
---|
| 94 | /* Ignore parameters, the connection is already opened */
|
---|
| 95 | while (true) {
|
---|
| 96 | ipc_call_t call;
|
---|
[984a9ba] | 97 | async_get_call(&call);
|
---|
[a2afd8f] | 98 |
|
---|
[b7fd2a0] | 99 | errno_t retval = EOK;
|
---|
[a2afd8f] | 100 |
|
---|
[fafb8e5] | 101 | if (!ipc_get_imethod(&call)) {
|
---|
[a2afd8f] | 102 | /* TODO: Handle hangup */
|
---|
| 103 | return;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[fafb8e5] | 106 | switch (ipc_get_imethod(&call)) {
|
---|
[a2afd8f] | 107 | case ADB_REG_NOTIF:
|
---|
[fafb8e5] | 108 | adb_mouse_data(mouse, ipc_get_arg1(&call));
|
---|
[a2afd8f] | 109 | break;
|
---|
| 110 | default:
|
---|
| 111 | retval = ENOENT;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[984a9ba] | 114 | async_answer_0(&call, retval);
|
---|
[a2afd8f] | 115 | }
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | /** Add ADB mouse device */
|
---|
[b7fd2a0] | 119 | errno_t adb_mouse_add(adb_mouse_t *mouse)
|
---|
[a2afd8f] | 120 | {
|
---|
[b7fd2a0] | 121 | errno_t rc;
|
---|
[a2afd8f] | 122 | bool bound = false;
|
---|
| 123 |
|
---|
| 124 | mouse->fun = ddf_fun_create(mouse->dev, fun_exposed, "a");
|
---|
| 125 | if (mouse->fun == NULL) {
|
---|
| 126 | ddf_msg(LVL_ERROR, "Error creating function");
|
---|
| 127 | rc = ENOMEM;
|
---|
| 128 | goto error;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | mouse->parent_sess = ddf_dev_parent_sess_get(mouse->dev);
|
---|
| 132 | if (mouse->parent_sess == NULL) {
|
---|
| 133 | ddf_msg(LVL_ERROR, "Error connecting parent driver");
|
---|
| 134 | rc = EIO;
|
---|
| 135 | goto error;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | async_exch_t *exch = async_exchange_begin(mouse->parent_sess);
|
---|
| 139 | if (exch == NULL) {
|
---|
| 140 | ddf_msg(LVL_ERROR, "Error starting exchange with parent");
|
---|
| 141 | rc = ENOMEM;
|
---|
| 142 | goto error;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | port_id_t port;
|
---|
| 146 | rc = async_create_callback_port(exch, INTERFACE_ADB_CB, 0, 0,
|
---|
| 147 | adb_mouse_events, mouse, &port);
|
---|
| 148 |
|
---|
| 149 | async_exchange_end(exch);
|
---|
| 150 | if (rc != EOK) {
|
---|
| 151 | ddf_msg(LVL_ERROR, "Error creating callback from device");
|
---|
| 152 | goto error;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | ddf_fun_set_conn_handler(mouse->fun, adb_mouse_conn);
|
---|
| 156 |
|
---|
| 157 | rc = ddf_fun_bind(mouse->fun);
|
---|
| 158 | if (rc != EOK) {
|
---|
| 159 | ddf_msg(LVL_ERROR, "Error binding function");
|
---|
| 160 | goto error;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | bound = true;
|
---|
| 164 |
|
---|
| 165 | rc = ddf_fun_add_to_category(mouse->fun, "mouse");
|
---|
| 166 | if (rc != EOK) {
|
---|
| 167 | ddf_msg(LVL_ERROR, "Error adding function to category");
|
---|
| 168 | goto error;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | return EOK;
|
---|
| 172 | error:
|
---|
| 173 | if (bound)
|
---|
| 174 | ddf_fun_unbind(mouse->fun);
|
---|
| 175 |
|
---|
| 176 | if (mouse->parent_sess != NULL) {
|
---|
| 177 | async_hangup(mouse->parent_sess);
|
---|
| 178 | mouse->parent_sess = NULL;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | if (mouse->fun != NULL) {
|
---|
| 182 | ddf_fun_destroy(mouse->fun);
|
---|
| 183 | mouse->fun = NULL;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | return rc;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /** Remove ADB mouse device */
|
---|
[b7fd2a0] | 190 | errno_t adb_mouse_remove(adb_mouse_t *con)
|
---|
[a2afd8f] | 191 | {
|
---|
| 192 | return ENOTSUP;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | /** ADB mouse device gone */
|
---|
[b7fd2a0] | 196 | errno_t adb_mouse_gone(adb_mouse_t *con)
|
---|
[a2afd8f] | 197 | {
|
---|
| 198 | return ENOTSUP;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | /** Handle client connection */
|
---|
[984a9ba] | 202 | static void adb_mouse_conn(ipc_call_t *icall, void *arg)
|
---|
[a2afd8f] | 203 | {
|
---|
| 204 | ipc_call_t call;
|
---|
| 205 | sysarg_t method;
|
---|
| 206 | adb_mouse_t *mouse;
|
---|
| 207 |
|
---|
| 208 | /*
|
---|
| 209 | * Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
| 210 | */
|
---|
[beb83c1] | 211 | async_accept_0(icall);
|
---|
[a2afd8f] | 212 |
|
---|
| 213 | mouse = (adb_mouse_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg));
|
---|
| 214 |
|
---|
| 215 | while (true) {
|
---|
[984a9ba] | 216 | async_get_call(&call);
|
---|
[fafb8e5] | 217 | method = ipc_get_imethod(&call);
|
---|
[a2afd8f] | 218 |
|
---|
| 219 | if (!method) {
|
---|
| 220 | /* The other side has hung up. */
|
---|
[984a9ba] | 221 | async_answer_0(&call, EOK);
|
---|
[a2afd8f] | 222 | return;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | async_sess_t *sess =
|
---|
| 226 | async_callback_receive_start(EXCHANGE_SERIALIZE, &call);
|
---|
| 227 | if (sess != NULL) {
|
---|
| 228 | mouse->client_sess = sess;
|
---|
[984a9ba] | 229 | async_answer_0(&call, EOK);
|
---|
[a2afd8f] | 230 | } else {
|
---|
[984a9ba] | 231 | async_answer_0(&call, EINVAL);
|
---|
[a2afd8f] | 232 | }
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
| 237 | * @}
|
---|
| 238 | */
|
---|