source: mainline/uspace/lib/drv/generic/remote_char_dev.c@ 9359aae

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

libdrv: Make interface structures constant.

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