[25a7e11d] | 1 | /*
|
---|
[7a252ec8] | 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
[25a7e11d] | 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 |
|
---|
[41b56084] | 38 | #include "ops/char_dev.h"
|
---|
[af6b5157] | 39 | #include "ddf/driver.h"
|
---|
[25a7e11d] | 40 |
|
---|
| 41 | #define MAX_CHAR_RW_COUNT 256
|
---|
| 42 |
|
---|
[83a2f43] | 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 *);
|
---|
[25a7e11d] | 45 |
|
---|
[7a252ec8] | 46 | /** Remote character interface operations. */
|
---|
[50c57df] | 47 | static remote_iface_func_ptr_t remote_char_dev_iface_ops[] = {
|
---|
[25a7e11d] | 48 | &remote_char_read,
|
---|
[7a252ec8] | 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.
|
---|
[ca97cad] | 56 | */
|
---|
[50c57df] | 57 | remote_iface_t remote_char_dev_iface = {
|
---|
| 58 | .method_count = sizeof(remote_char_dev_iface_ops) /
|
---|
[7a252ec8] | 59 | sizeof(remote_iface_func_ptr_t),
|
---|
[50c57df] | 60 | .methods = remote_char_dev_iface_ops
|
---|
[25a7e11d] | 61 | };
|
---|
| 62 |
|
---|
[ca97cad] | 63 | /** Process the read request from the remote client.
|
---|
[7a252ec8] | 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 | *
|
---|
[8b1e15ac] | 69 | * @param fun The function from which the data are read.
|
---|
[d35ac1d] | 70 | * @param ops The local ops structure.
|
---|
[ca97cad] | 71 | */
|
---|
[7a252ec8] | 72 | static void
|
---|
[83a2f43] | 73 | remote_char_read(ddf_fun_t *fun, void *ops, ipc_callid_t callid,
|
---|
[7a252ec8] | 74 | ipc_call_t *call)
|
---|
[ce79069b] | 75 | {
|
---|
[d35ac1d] | 76 | char_dev_ops_t *char_dev_ops = (char_dev_ops_t *) ops;
|
---|
[ba95e8f] | 77 | ipc_callid_t cid;
|
---|
[25a7e11d] | 78 |
|
---|
| 79 | size_t len;
|
---|
[ba95e8f] | 80 | if (!async_data_read_receive(&cid, &len)) {
|
---|
[7a252ec8] | 81 | /* TODO handle protocol error. */
|
---|
[ffa2c8ef] | 82 | async_answer_0(callid, EINVAL);
|
---|
[f658458] | 83 | return;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[d35ac1d] | 86 | if (!char_dev_ops->read) {
|
---|
[ba95e8f] | 87 | async_data_read_finalize(cid, NULL, 0);
|
---|
[ffa2c8ef] | 88 | async_answer_0(callid, ENOTSUP);
|
---|
[25a7e11d] | 89 | return;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[7a252ec8] | 92 | if (len > MAX_CHAR_RW_COUNT)
|
---|
[25a7e11d] | 93 | len = MAX_CHAR_RW_COUNT;
|
---|
| 94 |
|
---|
| 95 | char buf[MAX_CHAR_RW_COUNT];
|
---|
[8b1e15ac] | 96 | int ret = (*char_dev_ops->read)(fun, buf, len);
|
---|
[25a7e11d] | 97 |
|
---|
[7a252ec8] | 98 | if (ret < 0) {
|
---|
| 99 | /* Some error occured. */
|
---|
[ba95e8f] | 100 | async_data_read_finalize(cid, buf, 0);
|
---|
[ffa2c8ef] | 101 | async_answer_0(callid, ret);
|
---|
[25a7e11d] | 102 | return;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[7a252ec8] | 105 | /* The operation was successful, return the number of data read. */
|
---|
[ba95e8f] | 106 | async_data_read_finalize(cid, buf, ret);
|
---|
[ffa2c8ef] | 107 | async_answer_1(callid, EOK, ret);
|
---|
[25a7e11d] | 108 | }
|
---|
| 109 |
|
---|
[ca97cad] | 110 | /** Process the write request from the remote client.
|
---|
[7a252ec8] | 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 | *
|
---|
[8b1e15ac] | 116 | * @param fun The function to which the data are written.
|
---|
[d35ac1d] | 117 | * @param ops The local ops structure.
|
---|
[ca97cad] | 118 | */
|
---|
[7a252ec8] | 119 | static void
|
---|
[83a2f43] | 120 | remote_char_write(ddf_fun_t *fun, void *ops, ipc_callid_t callid,
|
---|
[7a252ec8] | 121 | ipc_call_t *call)
|
---|
[25a7e11d] | 122 | {
|
---|
[d35ac1d] | 123 | char_dev_ops_t *char_dev_ops = (char_dev_ops_t *) ops;
|
---|
[ca97cad] | 124 | ipc_callid_t cid;
|
---|
[25a7e11d] | 125 | size_t len;
|
---|
[ca97cad] | 126 |
|
---|
| 127 | if (!async_data_write_receive(&cid, &len)) {
|
---|
[7a252ec8] | 128 | /* TODO handle protocol error. */
|
---|
[ffa2c8ef] | 129 | async_answer_0(callid, EINVAL);
|
---|
[25a7e11d] | 130 | return;
|
---|
[7a252ec8] | 131 | }
|
---|
[25a7e11d] | 132 |
|
---|
[d35ac1d] | 133 | if (!char_dev_ops->write) {
|
---|
[ca97cad] | 134 | async_data_write_finalize(cid, NULL, 0);
|
---|
[ffa2c8ef] | 135 | async_answer_0(callid, ENOTSUP);
|
---|
[ca97cad] | 136 | return;
|
---|
[ce79069b] | 137 | }
|
---|
[ca97cad] | 138 |
|
---|
[7a252ec8] | 139 | if (len > MAX_CHAR_RW_COUNT)
|
---|
[25a7e11d] | 140 | len = MAX_CHAR_RW_COUNT;
|
---|
| 141 |
|
---|
| 142 | char buf[MAX_CHAR_RW_COUNT];
|
---|
| 143 |
|
---|
[ca97cad] | 144 | async_data_write_finalize(cid, buf, len);
|
---|
[25a7e11d] | 145 |
|
---|
[8b1e15ac] | 146 | int ret = (*char_dev_ops->write)(fun, buf, len);
|
---|
[7a252ec8] | 147 | if (ret < 0) {
|
---|
| 148 | /* Some error occured. */
|
---|
[ffa2c8ef] | 149 | async_answer_0(callid, ret);
|
---|
[25a7e11d] | 150 | } else {
|
---|
[7a252ec8] | 151 | /*
|
---|
| 152 | * The operation was successful, return the number of data
|
---|
| 153 | * written.
|
---|
| 154 | */
|
---|
[ffa2c8ef] | 155 | async_answer_1(callid, EOK, ret);
|
---|
[25a7e11d] | 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[7a252ec8] | 159 | /**
|
---|
[25a7e11d] | 160 | * @}
|
---|
[7a252ec8] | 161 | */
|
---|