| 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 | /** @addtogroup mouse_port
|
|---|
| 30 | * @ingroup mouse
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /** @file
|
|---|
| 34 | * @brief Chardev mouse port driver.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <ipc/char.h>
|
|---|
| 38 | #include <stdio.h>
|
|---|
| 39 | #include <async.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <loc.h>
|
|---|
| 42 | #include <input.h>
|
|---|
| 43 | #include <mouse_port.h>
|
|---|
| 44 | #include <mouse.h>
|
|---|
| 45 |
|
|---|
| 46 | static mouse_dev_t *mouse_dev;
|
|---|
| 47 | static async_sess_t *dev_sess;
|
|---|
| 48 |
|
|---|
| 49 | /** List of devices to try connecting to. */
|
|---|
| 50 | static const char *in_devs[] = {
|
|---|
| 51 | "char/ps2b",
|
|---|
| 52 | };
|
|---|
| 53 |
|
|---|
| 54 | static const unsigned int num_devs = sizeof(in_devs) / sizeof(in_devs[0]);
|
|---|
| 55 |
|
|---|
| 56 | static void mouse_port_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
|---|
| 57 | {
|
|---|
| 58 | /* Ignore parameters, the connection is already opened */
|
|---|
| 59 | while (true) {
|
|---|
| 60 | ipc_call_t call;
|
|---|
| 61 | ipc_callid_t callid = async_get_call(&call);
|
|---|
| 62 |
|
|---|
| 63 | if (!IPC_GET_IMETHOD(call)) {
|
|---|
| 64 | /* TODO: Handle hangup */
|
|---|
| 65 | return;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | int retval;
|
|---|
| 69 |
|
|---|
| 70 | switch (IPC_GET_IMETHOD(call)) {
|
|---|
| 71 | case CHAR_NOTIF_BYTE:
|
|---|
| 72 | mouse_push_data(mouse_dev, IPC_GET_ARG1(call));
|
|---|
| 73 | break;
|
|---|
| 74 | default:
|
|---|
| 75 | retval = ENOENT;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | async_answer_0(callid, retval);
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | static int chardev_port_init(mouse_dev_t *mdev)
|
|---|
| 83 | {
|
|---|
| 84 | service_id_t service_id;
|
|---|
| 85 | unsigned int i;
|
|---|
| 86 | int rc;
|
|---|
| 87 |
|
|---|
| 88 | mouse_dev = mdev;
|
|---|
| 89 |
|
|---|
| 90 | for (i = 0; i < num_devs; i++) {
|
|---|
| 91 | rc = loc_service_get_id(in_devs[i], &service_id, 0);
|
|---|
| 92 | if (rc == EOK)
|
|---|
| 93 | break;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | if (i >= num_devs) {
|
|---|
| 97 | printf("%s: Could not find any suitable input device\n", NAME);
|
|---|
| 98 | return -1;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | dev_sess = loc_service_connect(EXCHANGE_ATOMIC, service_id,
|
|---|
| 102 | IPC_FLAG_BLOCKING);
|
|---|
| 103 | if (dev_sess == NULL) {
|
|---|
| 104 | printf("%s: Failed connecting to device\n", NAME);
|
|---|
| 105 | return ENOENT;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
|---|
| 109 | if (exch == NULL) {
|
|---|
| 110 | printf("%s: Failed starting exchange with device\n", NAME);
|
|---|
| 111 | async_hangup(dev_sess);
|
|---|
| 112 | return ENOMEM;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | /* NB: The callback connection is slotted for removal */
|
|---|
| 116 | rc = async_connect_to_me(exch, 0, 0, 0, mouse_port_events, NULL);
|
|---|
| 117 | async_exchange_end(exch);
|
|---|
| 118 |
|
|---|
| 119 | if (rc != 0) {
|
|---|
| 120 | printf("%s: Failed to create callback from device\n", NAME);
|
|---|
| 121 | async_hangup(dev_sess);
|
|---|
| 122 | return -1;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | return 0;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | static void chardev_port_yield(void)
|
|---|
| 129 | {
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | static void chardev_port_reclaim(void)
|
|---|
| 133 | {
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | static void chardev_port_write(uint8_t data)
|
|---|
| 137 | {
|
|---|
| 138 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
|---|
| 139 | if (exch == NULL) {
|
|---|
| 140 | printf("%s: Failed starting exchange with device\n", NAME);
|
|---|
| 141 | return;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | async_msg_1(exch, CHAR_WRITE_BYTE, data);
|
|---|
| 145 | async_exchange_end(exch);
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | mouse_port_ops_t chardev_mouse_port = {
|
|---|
| 149 | .init = chardev_port_init,
|
|---|
| 150 | .yield = chardev_port_yield,
|
|---|
| 151 | .reclaim = chardev_port_reclaim,
|
|---|
| 152 | .write = chardev_port_write
|
|---|
| 153 | };
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * @}
|
|---|
| 157 | */ |
|---|