source: mainline/uspace/lib/c/generic/io/chardev.c@ f2d88f3

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

Add forgotten changes to enable non-blocking chardev read

  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33/**
34 * @file
35 * @brief Character device client interface
36 */
37
38#include <errno.h>
39#include <mem.h>
40#include <io/chardev.h>
41#include <ipc/chardev.h>
42#include <stddef.h>
43#include <stdlib.h>
44
45/** Open character device.
46 *
47 * @param sess Session with the character device
48 * @param rchardev Place to store pointer to the new character device structure
49 *
50 * @return EOK on success, ENOMEM if out of memory, EIO on I/O error
51 */
52errno_t chardev_open(async_sess_t *sess, chardev_t **rchardev)
53{
54 chardev_t *chardev;
55
56 chardev = calloc(1, sizeof(chardev_t));
57 if (chardev == NULL)
58 return ENOMEM;
59
60 chardev->sess = sess;
61 *rchardev = chardev;
62
63 /* EIO might be used in a future implementation */
64 return EOK;
65}
66
67/** Close character device.
68 *
69 * Frees the character device structure. The underlying session is
70 * not affected.
71 *
72 * @param chardev Character device or @c NULL
73 */
74void chardev_close(chardev_t *chardev)
75{
76 free(chardev);
77}
78
79/** Read from character device.
80 *
81 * Read as much data as is available from character device up to @a size
82 * bytes into @a buf. On success EOK is returned and at least one byte
83 * is read (if no byte is available the function blocks). The number
84 * of bytes read is stored in @a *nread.
85 *
86 * On error a non-zero error code is returned and @a *nread is filled with
87 * the number of bytes that were successfully transferred.
88 *
89 * @param chardev Character device
90 * @param buf Destination buffer
91 * @param size Maximum number of bytes to read
92 * @param nread Place to store actual number of bytes read
93 * @param flags @c chardev_f_nonblock to return immediately even if no
94 * bytes are available
95 *
96 * @return EOK on success or non-zero error code
97 */
98errno_t chardev_read(chardev_t *chardev, void *buf, size_t size, size_t *nread,
99 chardev_flags_t flags)
100{
101 async_exch_t *exch = async_exchange_begin(chardev->sess);
102
103 if (size > DATA_XFER_LIMIT) {
104 /* This should not hurt anything. */
105 size = DATA_XFER_LIMIT;
106 }
107
108 ipc_call_t answer;
109 aid_t req = async_send_1(exch, CHARDEV_READ, flags, &answer);
110 errno_t rc = async_data_read_start(exch, buf, size);
111 async_exchange_end(exch);
112
113 if (rc != EOK) {
114 async_forget(req);
115 *nread = 0;
116 return rc;
117 }
118
119 errno_t retval;
120 async_wait_for(req, &retval);
121
122 if (retval != EOK) {
123 *nread = 0;
124 return retval;
125 }
126
127 *nread = IPC_GET_ARG2(answer);
128 /* In case of partial success, ARG1 contains the error code */
129 return (errno_t) IPC_GET_ARG1(answer);
130
131}
132
133/** Write up to DATA_XFER_LIMIT bytes to character device.
134 *
135 * Write up to @a size or DATA_XFER_LIMIT bytes from @a data to character
136 * device. On success EOK is returned, bytes were written and @a *nwritten
137 * is set to min(@a size, DATA_XFER_LIMIT)
138 *
139 * On error a non-zero error code is returned and @a *nwritten is filled with
140 * the number of bytes that were successfully transferred.
141 *
142 * @param chardev Character device
143 * @param buf Destination buffer
144 * @param size Maximum number of bytes to read
145 * @param nwritten Place to store actual number of bytes written
146 *
147 * @return EOK on success or non-zero error code
148 */
149static errno_t chardev_write_once(chardev_t *chardev, const void *data,
150 size_t size, size_t *nwritten)
151{
152 async_exch_t *exch = async_exchange_begin(chardev->sess);
153 ipc_call_t answer;
154 aid_t req;
155 errno_t rc;
156
157 /* Break down large transfers */
158 if (size > DATA_XFER_LIMIT)
159 size = DATA_XFER_LIMIT;
160
161 req = async_send_0(exch, CHARDEV_WRITE, &answer);
162 rc = async_data_write_start(exch, data, size);
163 async_exchange_end(exch);
164
165 if (rc != EOK) {
166 async_forget(req);
167 *nwritten = 0;
168 return rc;
169 }
170
171 errno_t retval;
172 async_wait_for(req, &retval);
173 if (retval != EOK) {
174 *nwritten = 0;
175 return retval;
176 }
177
178 *nwritten = IPC_GET_ARG2(answer);
179 /* In case of partial success, ARG1 contains the error code */
180 return (errno_t) IPC_GET_ARG1(answer);
181}
182
183/** Write to character device.
184 *
185 * Write @a size bytes from @a data to character device. On success EOK
186 * is returned, all bytes were written and @a *nwritten is set to @a size.
187 *
188 * On error a non-zero error code is returned and @a *nwritten is filled with
189 * the number of bytes that were successfully transferred.
190 *
191 * @param chardev Character device
192 * @param buf Destination buffer
193 * @param size Maximum number of bytes to read
194 * @param nwritten Place to store actual number of bytes written
195 *
196 * @return EOK on success or non-zero error code
197 */
198errno_t chardev_write(chardev_t *chardev, const void *data, size_t size,
199 size_t *nwritten)
200{
201 size_t nw;
202 size_t p;
203 errno_t rc;
204
205 p = 0;
206 while (p < size) {
207 rc = chardev_write_once(chardev, data + p, size - p, &nw);
208 /* nw is always valid, we can have partial success */
209 p += nw;
210
211 if (rc != EOK) {
212 /* We can return partial success */
213 *nwritten = p;
214 return rc;
215 }
216 }
217
218 *nwritten = p;
219 return EOK;
220}
221
222/** @}
223 */
Note: See TracBrowser for help on using the repository browser.