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

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

parts of generic char interface, fixed some bugs

  • Property mode set to 100644
File size: 3.4 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 async_data_read_finalize(callid, buf, ret);
88 ipc_answer_0(callid, EOK);
89}
90
91static void remote_char_write(device_t *dev, void *iface, ipc_callid_t callid, ipc_call_t *call)
92{
93 char_iface_t *char_iface = (char_iface_t *)iface;
94 if (!char_iface->write) {
95 ipc_answer_0(callid, ENOTSUP);
96 return;
97 }
98
99 size_t len;
100 if (!async_data_write_receive(&callid, &len)) {
101 // TODO handle protocol error
102 return;
103 }
104
105 if (len > MAX_CHAR_RW_COUNT) {
106 len = MAX_CHAR_RW_COUNT;
107 }
108
109 char buf[MAX_CHAR_RW_COUNT];
110
111 async_data_write_finalize(callid, buf, len);
112
113 int ret = (*char_iface->write)(dev, buf, len);
114 if (ret < 0) { // some error occured
115 ipc_answer_0(callid, ret);
116 } else {
117 ipc_answer_0(callid, EOK);
118 }
119}
120
121 /**
122 * @}
123 */
Note: See TracBrowser for help on using the repository browser.