source: mainline/uspace/lib/drv/generic/remote_char_dev.c@ 5c65e61

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5c65e61 was 5c65e61, checked in by Jan Vesely <jano.vesely@…>, 11 years ago

libc,libdrv: Move char dev iface to libdrv

  • Property mode set to 100644
File size: 6.9 KB
RevLine 
[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>
[9be30cdf]37#include <macros.h>
[25a7e11d]38
[41b56084]39#include "ops/char_dev.h"
[5c65e61]40#include "char_dev_iface.h"
[af6b5157]41#include "ddf/driver.h"
[25a7e11d]42
43#define MAX_CHAR_RW_COUNT 256
44
[5c65e61]45/** Read to or write from device.
46 *
47 * Helper function to read to or write from a device
48 * using its character interface.
49 *
50 * @param sess Session to the device.
51 * @param buf Buffer for the data read from or written to the device.
52 * @param size Maximum size of data (in bytes) to be read or written.
53 * @param read Read from the device if true, write to it otherwise.
54 *
55 * @return Non-negative number of bytes actually read from or
56 * written to the device on success, negative error number
57 * otherwise.
58 *
59 */
60static ssize_t char_dev_rw(async_sess_t *sess, void *buf, size_t size, bool read)
61{
62 ipc_call_t answer;
63 aid_t req;
64 int ret;
65
66 async_exch_t *exch = async_exchange_begin(sess);
67
68 if (read) {
69 req = async_send_1(exch, DEV_IFACE_ID(CHAR_DEV_IFACE),
70 CHAR_DEV_READ, &answer);
71 ret = async_data_read_start(exch, buf, size);
72 } else {
73 req = async_send_1(exch, DEV_IFACE_ID(CHAR_DEV_IFACE),
74 CHAR_DEV_WRITE, &answer);
75 ret = async_data_write_start(exch, buf, size);
76 }
77
78 async_exchange_end(exch);
79
80 sysarg_t rc;
81 if (ret != EOK) {
82 async_wait_for(req, &rc);
83 if (rc == EOK)
84 return (ssize_t) ret;
85
86 return (ssize_t) rc;
87 }
88
89 async_wait_for(req, &rc);
90
91 ret = (int) rc;
92 if (ret != EOK)
93 return (ssize_t) ret;
94
95 return (ssize_t) IPC_GET_ARG1(answer);
96}
97
98/** Read from character device.
99 *
100 * @param sess Session to the device.
101 * @param buf Output buffer for the data read from the device.
102 * @param size Maximum size (in bytes) of the data to be read.
103 *
104 * @return Non-negative number of bytes actually read from the
105 * device on success, negative error number otherwise.
106 *
107 */
108ssize_t char_dev_read(async_sess_t *sess, void *buf, size_t size)
109{
110 return char_dev_rw(sess, buf, size, true);
111}
112
113/** Write to character device.
114 *
115 * @param sess Session to the device.
116 * @param buf Input buffer containg the data to be written to the
117 * device.
118 * @param size Maximum size (in bytes) of the data to be written.
119 *
120 * @return Non-negative number of bytes actually written to the
121 * device on success, negative error number otherwise.
122 *
123 */
124ssize_t char_dev_write(async_sess_t *sess, void *buf, size_t size)
125{
126 return char_dev_rw(sess, buf, size, false);
127}
128
[83a2f43]129static void remote_char_read(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
130static void remote_char_write(ddf_fun_t *, void *, ipc_callid_t, ipc_call_t *);
[25a7e11d]131
[7a252ec8]132/** Remote character interface operations. */
[9be30cdf]133static const remote_iface_func_ptr_t remote_char_dev_iface_ops[] = {
[20ec37c]134 [CHAR_DEV_READ] = remote_char_read,
135 [CHAR_DEV_WRITE] = remote_char_write
[7a252ec8]136};
137
138/** Remote character interface structure.
139 *
140 * Interface for processing request from remote clients addressed to the
141 * character interface.
[ca97cad]142 */
[7f80313]143const remote_iface_t remote_char_dev_iface = {
[9be30cdf]144 .method_count = ARRAY_SIZE(remote_char_dev_iface_ops),
[50c57df]145 .methods = remote_char_dev_iface_ops
[25a7e11d]146};
147
[ca97cad]148/** Process the read request from the remote client.
[7a252ec8]149 *
150 * Receive the read request's parameters from the remote client and pass them
151 * to the local interface. Return the result of the operation processed by the
152 * local interface to the remote client.
153 *
[8b1e15ac]154 * @param fun The function from which the data are read.
[d35ac1d]155 * @param ops The local ops structure.
[ca97cad]156 */
[7a252ec8]157static void
[83a2f43]158remote_char_read(ddf_fun_t *fun, void *ops, ipc_callid_t callid,
[7a252ec8]159 ipc_call_t *call)
[ce79069b]160{
[d35ac1d]161 char_dev_ops_t *char_dev_ops = (char_dev_ops_t *) ops;
[ba95e8f]162 ipc_callid_t cid;
[25a7e11d]163
164 size_t len;
[ba95e8f]165 if (!async_data_read_receive(&cid, &len)) {
[7a252ec8]166 /* TODO handle protocol error. */
[ffa2c8ef]167 async_answer_0(callid, EINVAL);
[f658458]168 return;
169 }
170
[d35ac1d]171 if (!char_dev_ops->read) {
[ba95e8f]172 async_data_read_finalize(cid, NULL, 0);
[ffa2c8ef]173 async_answer_0(callid, ENOTSUP);
[25a7e11d]174 return;
175 }
176
[7a252ec8]177 if (len > MAX_CHAR_RW_COUNT)
[25a7e11d]178 len = MAX_CHAR_RW_COUNT;
179
180 char buf[MAX_CHAR_RW_COUNT];
[8b1e15ac]181 int ret = (*char_dev_ops->read)(fun, buf, len);
[25a7e11d]182
[7a252ec8]183 if (ret < 0) {
184 /* Some error occured. */
[ba95e8f]185 async_data_read_finalize(cid, buf, 0);
[ffa2c8ef]186 async_answer_0(callid, ret);
[25a7e11d]187 return;
188 }
189
[7a252ec8]190 /* The operation was successful, return the number of data read. */
[ba95e8f]191 async_data_read_finalize(cid, buf, ret);
[ffa2c8ef]192 async_answer_1(callid, EOK, ret);
[25a7e11d]193}
194
[ca97cad]195/** Process the write request from the remote client.
[7a252ec8]196 *
197 * Receive the write request's parameters from the remote client and pass them
198 * to the local interface. Return the result of the operation processed by the
199 * local interface to the remote client.
200 *
[8b1e15ac]201 * @param fun The function to which the data are written.
[d35ac1d]202 * @param ops The local ops structure.
[ca97cad]203 */
[7a252ec8]204static void
[83a2f43]205remote_char_write(ddf_fun_t *fun, void *ops, ipc_callid_t callid,
[7a252ec8]206 ipc_call_t *call)
[25a7e11d]207{
[d35ac1d]208 char_dev_ops_t *char_dev_ops = (char_dev_ops_t *) ops;
[ca97cad]209 ipc_callid_t cid;
[25a7e11d]210 size_t len;
[ca97cad]211
212 if (!async_data_write_receive(&cid, &len)) {
[7a252ec8]213 /* TODO handle protocol error. */
[ffa2c8ef]214 async_answer_0(callid, EINVAL);
[25a7e11d]215 return;
[7a252ec8]216 }
[25a7e11d]217
[d35ac1d]218 if (!char_dev_ops->write) {
[ca97cad]219 async_data_write_finalize(cid, NULL, 0);
[ffa2c8ef]220 async_answer_0(callid, ENOTSUP);
[ca97cad]221 return;
[ce79069b]222 }
[ca97cad]223
[7a252ec8]224 if (len > MAX_CHAR_RW_COUNT)
[25a7e11d]225 len = MAX_CHAR_RW_COUNT;
226
227 char buf[MAX_CHAR_RW_COUNT];
228
[ca97cad]229 async_data_write_finalize(cid, buf, len);
[25a7e11d]230
[8b1e15ac]231 int ret = (*char_dev_ops->write)(fun, buf, len);
[7a252ec8]232 if (ret < 0) {
233 /* Some error occured. */
[ffa2c8ef]234 async_answer_0(callid, ret);
[25a7e11d]235 } else {
[7a252ec8]236 /*
237 * The operation was successful, return the number of data
238 * written.
239 */
[ffa2c8ef]240 async_answer_1(callid, EOK, ret);
[25a7e11d]241 }
242}
243
[7a252ec8]244/**
[25a7e11d]245 * @}
[7a252ec8]246 */
Note: See TracBrowser for help on using the repository browser.