source: mainline/uspace/lib/drv/generic/remote_char.c@ c47e1a8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c47e1a8 was c47e1a8, checked in by Lenka Trochtova <trochtova.lenka@…>, 15 years ago

merge mainline changes (rev. 451)

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