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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f401312 was d35ac1d, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Rename iface to ops in driver.c

  • Property mode set to 100644
File size: 4.6 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 <ipc/ipc.h>
36#include <async.h>
37#include <errno.h>
38
39#include "ops/char_dev.h"
40#include "driver.h"
41
42#define MAX_CHAR_RW_COUNT 256
43
44static void remote_char_read(device_t *, void *, ipc_callid_t, ipc_call_t *);
45static void remote_char_write(device_t *, void *, ipc_callid_t, ipc_call_t *);
46
47/** Remote character interface operations. */
48static remote_iface_func_ptr_t remote_char_dev_iface_ops[] = {
49 &remote_char_read,
50 &remote_char_write
51};
52
53/** Remote character interface structure.
54 *
55 * Interface for processing request from remote clients addressed to the
56 * character interface.
57 */
58remote_iface_t remote_char_dev_iface = {
59 .method_count = sizeof(remote_char_dev_iface_ops) /
60 sizeof(remote_iface_func_ptr_t),
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 dev The device from which the data are read.
71 * @param ops The local ops structure.
72 */
73static void
74remote_char_read(device_t *dev, 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 ipc_answer_0(callid, EINVAL);
84 return;
85 }
86
87 if (!char_dev_ops->read) {
88 async_data_read_finalize(cid, NULL, 0);
89 ipc_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)(dev, buf, len);
98
99 if (ret < 0) {
100 /* Some error occured. */
101 async_data_read_finalize(cid, buf, 0);
102 ipc_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 ipc_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 dev The device to which the data are written.
118 * @param ops The local ops structure.
119 */
120static void
121remote_char_write(device_t *dev, 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 ipc_answer_0(callid, EINVAL);
131 return;
132 }
133
134 if (!char_dev_ops->write) {
135 async_data_write_finalize(cid, NULL, 0);
136 ipc_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)(dev, buf, len);
148 if (ret < 0) {
149 /* Some error occured. */
150 ipc_answer_0(callid, ret);
151 } else {
152 /*
153 * The operation was successful, return the number of data
154 * written.
155 */
156 ipc_answer_1(callid, EOK, ret);
157 }
158}
159
160/**
161 * @}
162 */
Note: See TracBrowser for help on using the repository browser.