[9f51afc] | 1 | /*
|
---|
[9be360ee] | 2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
[9f51afc] | 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 | * @{
|
---|
[79ae36dd] | 32 | */
|
---|
[9f51afc] | 33 | /** @file
|
---|
| 34 | * @brief Chardev keyboard port driver.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <ipc/char.h>
|
---|
| 38 | #include <async.h>
|
---|
[15f3c3f] | 39 | #include <loc.h>
|
---|
[9f51afc] | 40 | #include <errno.h>
|
---|
[79ae36dd] | 41 | #include <stdio.h>
|
---|
[b6a088f] | 42 | #include "../input.h"
|
---|
| 43 | #include "../kbd_port.h"
|
---|
| 44 | #include "../kbd.h"
|
---|
[79ae36dd] | 45 |
|
---|
[9934f7d] | 46 | static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall, void *arg);
|
---|
[9f51afc] | 47 |
|
---|
[9be360ee] | 48 | static int chardev_port_init(kbd_dev_t *);
|
---|
[b1bdc7a4] | 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 |
|
---|
[9be360ee] | 56 | static kbd_dev_t *kbd_dev;
|
---|
[a40dea3] | 57 | static async_sess_t *dev_sess;
|
---|
[9f51afc] | 58 |
|
---|
[a9b5b5f] | 59 | /** List of devices to try connecting to. */
|
---|
| 60 | static const char *in_devs[] = {
|
---|
[6d15572] | 61 | /** S3C24xx UART - Openmoko debug console */
|
---|
| 62 | "char/s3c24xx_uart",
|
---|
| 63 | /** Ski console */
|
---|
| 64 | "devices/\\hw\\console\\a"
|
---|
[a9b5b5f] | 65 | };
|
---|
| 66 |
|
---|
[79ae36dd] | 67 | static const unsigned int num_devs = sizeof(in_devs) / sizeof(in_devs[0]);
|
---|
[a9b5b5f] | 68 |
|
---|
[9be360ee] | 69 | static int chardev_port_init(kbd_dev_t *kdev)
|
---|
[9f51afc] | 70 | {
|
---|
[15f3c3f] | 71 | service_id_t service_id;
|
---|
[a40dea3] | 72 | async_exch_t *exch;
|
---|
[79ae36dd] | 73 | unsigned int i;
|
---|
| 74 | int rc;
|
---|
| 75 |
|
---|
[9be360ee] | 76 | kbd_dev = kdev;
|
---|
| 77 |
|
---|
[a9b5b5f] | 78 | for (i = 0; i < num_devs; i++) {
|
---|
[15f3c3f] | 79 | rc = loc_service_get_id(in_devs[i], &service_id, 0);
|
---|
[f5b6fb5] | 80 | if (rc == EOK)
|
---|
[a9b5b5f] | 81 | break;
|
---|
| 82 | }
|
---|
[79ae36dd] | 83 |
|
---|
[a9b5b5f] | 84 | if (i >= num_devs) {
|
---|
[79ae36dd] | 85 | printf("%s: Could not find any suitable input device\n", NAME);
|
---|
[a9b5b5f] | 86 | return -1;
|
---|
[9f51afc] | 87 | }
|
---|
[79ae36dd] | 88 |
|
---|
[f9b2cb4c] | 89 | dev_sess = loc_service_connect(service_id, INTERFACE_DDF,
|
---|
[a40dea3] | 90 | IPC_FLAG_BLOCKING);
|
---|
| 91 | if (dev_sess == NULL) {
|
---|
[79ae36dd] | 92 | printf("%s: Failed connecting to device\n", NAME);
|
---|
| 93 | return ENOENT;
|
---|
[9f51afc] | 94 | }
|
---|
[79ae36dd] | 95 |
|
---|
[a40dea3] | 96 | exch = async_exchange_begin(dev_sess);
|
---|
| 97 | if (exch == NULL) {
|
---|
| 98 | printf("%s: Failed starting exchange with device\n", NAME);
|
---|
| 99 | async_hangup(dev_sess);
|
---|
| 100 | return ENOMEM;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[f9b2cb4c] | 103 | port_id_t port;
|
---|
| 104 | rc = async_create_callback_port(exch, INTERFACE_CHAR_CB, 0, 0,
|
---|
| 105 | kbd_port_events, NULL, &port);
|
---|
| 106 |
|
---|
[a40dea3] | 107 | async_exchange_end(exch);
|
---|
| 108 |
|
---|
| 109 | if (rc != 0) {
|
---|
[1875a0c] | 110 | printf("%s: Failed to create callback from device\n", NAME);
|
---|
[a40dea3] | 111 | async_hangup(dev_sess);
|
---|
[a9b5b5f] | 112 | return -1;
|
---|
[9f51afc] | 113 | }
|
---|
[a40dea3] | 114 |
|
---|
[9f51afc] | 115 | return 0;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[b1bdc7a4] | 118 | static void chardev_port_write(uint8_t data)
|
---|
[9f51afc] | 119 | {
|
---|
[a40dea3] | 120 | async_exch_t *exch = async_exchange_begin(dev_sess);
|
---|
| 121 | if (exch == NULL) {
|
---|
| 122 | printf("%s: Failed starting exchange with device\n", NAME);
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | async_msg_1(exch, CHAR_WRITE_BYTE, data);
|
---|
| 127 | async_exchange_end(exch);
|
---|
[9f51afc] | 128 | }
|
---|
| 129 |
|
---|
[9934f7d] | 130 | static void kbd_port_events(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[9f51afc] | 131 | {
|
---|
| 132 | /* Ignore parameters, the connection is already opened */
|
---|
| 133 | while (true) {
|
---|
| 134 |
|
---|
| 135 | ipc_call_t call;
|
---|
| 136 | ipc_callid_t callid = async_get_call(&call);
|
---|
[79ae36dd] | 137 |
|
---|
| 138 | if (!IPC_GET_IMETHOD(call)) {
|
---|
| 139 | /* TODO: Handle hangup */
|
---|
| 140 | return;
|
---|
| 141 | }
|
---|
[9f51afc] | 142 |
|
---|
[f81498d] | 143 | int retval = EOK;
|
---|
[9f51afc] | 144 |
|
---|
[228e490] | 145 | switch (IPC_GET_IMETHOD(call)) {
|
---|
[9f51afc] | 146 | case CHAR_NOTIF_BYTE:
|
---|
[1875a0c] | 147 | kbd_push_data(kbd_dev, IPC_GET_ARG1(call));
|
---|
[9f51afc] | 148 | break;
|
---|
| 149 | default:
|
---|
| 150 | retval = ENOENT;
|
---|
| 151 | }
|
---|
[ffa2c8ef] | 152 | async_answer_0(callid, retval);
|
---|
[9f51afc] | 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * @}
|
---|
| 159 | */
|
---|