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