| 1 | /*
|
|---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
|---|
| 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 libdrv
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <async.h>
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 |
|
|---|
| 38 | #include "ops/char_dev.h"
|
|---|
| 39 | #include "ddf/driver.h"
|
|---|
| 40 |
|
|---|
| 41 | #define MAX_CHAR_RW_COUNT 256
|
|---|
| 42 |
|
|---|
| 43 | static void remote_char_read(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 44 | static void remote_char_write(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
|
|---|
| 45 |
|
|---|
| 46 | /** Remote character interface operations. */
|
|---|
| 47 | static remote_iface_func_ptr_t remote_char_dev_iface_ops[] = {
|
|---|
| 48 | &remote_char_read,
|
|---|
| 49 | &remote_char_write
|
|---|
| 50 | };
|
|---|
| 51 |
|
|---|
| 52 | /** Remote character interface structure.
|
|---|
| 53 | *
|
|---|
| 54 | * Interface for processing request from remote clients addressed to the
|
|---|
| 55 | * character interface.
|
|---|
| 56 | */
|
|---|
| 57 | remote_iface_t remote_char_dev_iface = {
|
|---|
| 58 | .method_count = sizeof(remote_char_dev_iface_ops) /
|
|---|
| 59 | sizeof(remote_iface_func_ptr_t),
|
|---|
| 60 | .methods = remote_char_dev_iface_ops
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | /** Process the read request from the remote client.
|
|---|
| 64 | *
|
|---|
| 65 | * Receive the read request's parameters from the remote client and pass them
|
|---|
| 66 | * to the local interface. Return the result of the operation processed by the
|
|---|
| 67 | * local interface to the remote client.
|
|---|
| 68 | *
|
|---|
| 69 | * @param fun The function from which the data are read.
|
|---|
| 70 | * @param ops The local ops structure.
|
|---|
| 71 | */
|
|---|
| 72 | static void
|
|---|
| 73 | remote_char_read(ddf_fun_t *fun, void *ops, ipc_callid_t callid,
|
|---|
| 74 | ipc_call_t *call)
|
|---|
| 75 | {
|
|---|
| 76 | char_dev_ops_t *char_dev_ops = (char_dev_ops_t *) ops;
|
|---|
| 77 | ipc_callid_t cid;
|
|---|
| 78 |
|
|---|
| 79 | size_t len;
|
|---|
| 80 | if (!async_data_read_receive(&cid, &len)) {
|
|---|
| 81 | /* TODO handle protocol error. */
|
|---|
| 82 | async_answer_0(callid, EINVAL);
|
|---|
| 83 | return;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | if (!char_dev_ops->read) {
|
|---|
| 87 | async_data_read_finalize(cid, NULL, 0);
|
|---|
| 88 | async_answer_0(callid, ENOTSUP);
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | if (len > MAX_CHAR_RW_COUNT)
|
|---|
| 93 | len = MAX_CHAR_RW_COUNT;
|
|---|
| 94 |
|
|---|
| 95 | char buf[MAX_CHAR_RW_COUNT];
|
|---|
| 96 | int ret = (*char_dev_ops->read)(fun, buf, len);
|
|---|
| 97 |
|
|---|
| 98 | if (ret < 0) {
|
|---|
| 99 | /* Some error occured. */
|
|---|
| 100 | async_data_read_finalize(cid, buf, 0);
|
|---|
| 101 | async_answer_0(callid, ret);
|
|---|
| 102 | return;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /* The operation was successful, return the number of data read. */
|
|---|
| 106 | async_data_read_finalize(cid, buf, ret);
|
|---|
| 107 | async_answer_1(callid, EOK, ret);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | /** Process the write request from the remote client.
|
|---|
| 111 | *
|
|---|
| 112 | * Receive the write request's parameters from the remote client and pass them
|
|---|
| 113 | * to the local interface. Return the result of the operation processed by the
|
|---|
| 114 | * local interface to the remote client.
|
|---|
| 115 | *
|
|---|
| 116 | * @param fun The function to which the data are written.
|
|---|
| 117 | * @param ops The local ops structure.
|
|---|
| 118 | */
|
|---|
| 119 | static void
|
|---|
| 120 | remote_char_write(ddf_fun_t *fun, void *ops, ipc_callid_t callid,
|
|---|
| 121 | ipc_call_t *call)
|
|---|
| 122 | {
|
|---|
| 123 | char_dev_ops_t *char_dev_ops = (char_dev_ops_t *) ops;
|
|---|
| 124 | ipc_callid_t cid;
|
|---|
| 125 | size_t len;
|
|---|
| 126 |
|
|---|
| 127 | if (!async_data_write_receive(&cid, &len)) {
|
|---|
| 128 | /* TODO handle protocol error. */
|
|---|
| 129 | async_answer_0(callid, EINVAL);
|
|---|
| 130 | return;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | if (!char_dev_ops->write) {
|
|---|
| 134 | async_data_write_finalize(cid, NULL, 0);
|
|---|
| 135 | async_answer_0(callid, ENOTSUP);
|
|---|
| 136 | return;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | if (len > MAX_CHAR_RW_COUNT)
|
|---|
| 140 | len = MAX_CHAR_RW_COUNT;
|
|---|
| 141 |
|
|---|
| 142 | char buf[MAX_CHAR_RW_COUNT];
|
|---|
| 143 |
|
|---|
| 144 | async_data_write_finalize(cid, buf, len);
|
|---|
| 145 |
|
|---|
| 146 | int ret = (*char_dev_ops->write)(fun, buf, len);
|
|---|
| 147 | if (ret < 0) {
|
|---|
| 148 | /* Some error occured. */
|
|---|
| 149 | async_answer_0(callid, ret);
|
|---|
| 150 | } else {
|
|---|
| 151 | /*
|
|---|
| 152 | * The operation was successful, return the number of data
|
|---|
| 153 | * written.
|
|---|
| 154 | */
|
|---|
| 155 | async_answer_1(callid, EOK, ret);
|
|---|
| 156 | }
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * @}
|
|---|
| 161 | */
|
|---|