source: mainline/uspace/lib/libdrv/generic/remote_char.c@ bb864a0

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

backup

  • Property mode set to 100644
File size: 3.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 "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
47static remote_iface_func_ptr_t remote_char_iface_ops [] = {
48 &remote_char_read,
49 &remote_char_write
50};
51
52remote_iface_t remote_char_iface = {
53 .method_count = sizeof(remote_char_iface_ops) / sizeof(remote_iface_func_ptr_t),
54 .methods = remote_char_iface_ops
55};
56
57static void remote_char_read(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call)
58{
59 char_iface_t *char_iface = (char_iface_t *)iface;
60
61 size_t len;
62 if (!async_data_read_receive(&callid, &len)) {
63 // TODO handle protocol error
64 ipc_answer_0(callid, EINVAL);
65 return;
66 }
67
68 if (!char_iface->read) {
69 async_data_read_finalize(callid, NULL, 0);
70 ipc_answer_0(callid, ENOTSUP);
71 return;
72 }
73
74 if (len > MAX_CHAR_RW_COUNT) {
75 len = MAX_CHAR_RW_COUNT;
76 }
77
78 char buf[MAX_CHAR_RW_COUNT];
79 int ret = (*char_iface->read)(dev, buf, len);
80
81 if (ret < 0) { // some error occured
82 async_data_read_finalize(callid, buf, 0);
83 ipc_answer_0(callid, ret);
84 return;
85 }
86
87 printf("remote_char_read - async_data_read_finalize\n");
88 async_data_read_finalize(callid, buf, ret);
89 printf("remote_char_read - ipc_answer_0(callid, EOK);\n");
90 ipc_answer_0(callid, EOK);
91}
92
93static void remote_char_write(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call)
94{
95 char_iface_t *char_iface = (char_iface_t *)iface;
96 if (!char_iface->write) {
97 ipc_answer_0(callid, ENOTSUP);
98 return;
99 }
100
101 size_t len;
102 if (!async_data_write_receive(&callid, &len)) {
103 // TODO handle protocol error
104 return;
105 }
106
107 if (len > MAX_CHAR_RW_COUNT) {
108 len = MAX_CHAR_RW_COUNT;
109 }
110
111 char buf[MAX_CHAR_RW_COUNT];
112
113 async_data_write_finalize(callid, buf, len);
114
115 int ret = (*char_iface->write)(dev, buf, len);
116 if (ret < 0) { // some error occured
117 ipc_answer_0(callid, ret);
118 } else {
119 ipc_answer_0(callid, EOK);
120 }
121}
122
123 /**
124 * @}
125 */
Note: See TracBrowser for help on using the repository browser.