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