[a455321] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
---|
[15c5418] | 3 | * Copyright (c) 2017 Jiri Svoboda
|
---|
[a455321] | 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 |
|
---|
[15c5418] | 30 | /** @addtogroup libc
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * @brief Character device client interface
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[a455321] | 38 | #include <errno.h>
|
---|
| 39 | #include <mem.h>
|
---|
[75751db6] | 40 | #include <io/chardev.h>
|
---|
| 41 | #include <ipc/chardev.h>
|
---|
[5d50c419] | 42 | #include <stddef.h>
|
---|
[15c5418] | 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 | */
|
---|
| 52 | int 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 | }
|
---|
[a455321] | 66 |
|
---|
[15c5418] | 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 | */
|
---|
| 74 | void chardev_close(chardev_t *chardev)
|
---|
| 75 | {
|
---|
| 76 | free(chardev);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[c657bd7] | 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 | *
|
---|
| 94 | * @return EOK on success or non-zero error code
|
---|
| 95 | */
|
---|
| 96 | int chardev_read(chardev_t *chardev, void *buf, size_t size, size_t *nread)
|
---|
[a455321] | 97 | {
|
---|
[15c5418] | 98 | async_exch_t *exch = async_exchange_begin(chardev->sess);
|
---|
[19ea61d] | 99 |
|
---|
| 100 | if (size > DATA_XFER_LIMIT) {
|
---|
| 101 | /* This should not hurt anything. */
|
---|
| 102 | size = DATA_XFER_LIMIT;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | ipc_call_t answer;
|
---|
| 106 | aid_t req = async_send_0(exch, CHARDEV_READ, &answer);
|
---|
| 107 | int rc = async_data_read_start(exch, buf, size);
|
---|
[15c5418] | 108 | async_exchange_end(exch);
|
---|
[5d50c419] | 109 |
|
---|
[19ea61d] | 110 | if (rc != EOK) {
|
---|
| 111 | async_forget(req);
|
---|
[5d50c419] | 112 | *nread = 0;
|
---|
[19ea61d] | 113 | return rc;
|
---|
[5d50c419] | 114 | }
|
---|
| 115 |
|
---|
[25a179e] | 116 | int retval;
|
---|
[19ea61d] | 117 | async_wait_for(req, &retval);
|
---|
| 118 |
|
---|
| 119 | if (retval != EOK) {
|
---|
| 120 | *nread = 0;
|
---|
| 121 | return retval;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | *nread = IPC_GET_ARG2(answer);
|
---|
| 125 | /* In case of partial success, ARG1 contains the error code */
|
---|
[d5c1051] | 126 | return (int) IPC_GET_ARG1(answer);
|
---|
[19ea61d] | 127 |
|
---|
[a455321] | 128 | }
|
---|
| 129 |
|
---|
[19ea61d] | 130 | /** Write up to DATA_XFER_LIMIT bytes to character device.
|
---|
[c657bd7] | 131 | *
|
---|
[19ea61d] | 132 | * Write up to @a size or DATA_XFER_LIMIT bytes from @a data to character
|
---|
| 133 | * device. On success EOK is returned, bytes were written and @a *nwritten
|
---|
| 134 | * is set to min(@a size, DATA_XFER_LIMIT)
|
---|
[c657bd7] | 135 | *
|
---|
| 136 | * On error a non-zero error code is returned and @a *nwritten is filled with
|
---|
| 137 | * the number of bytes that were successfully transferred.
|
---|
| 138 | *
|
---|
| 139 | * @param chardev Character device
|
---|
| 140 | * @param buf Destination buffer
|
---|
| 141 | * @param size Maximum number of bytes to read
|
---|
| 142 | * @param nwritten Place to store actual number of bytes written
|
---|
| 143 | *
|
---|
| 144 | * @return EOK on success or non-zero error code
|
---|
| 145 | */
|
---|
[19ea61d] | 146 | static int chardev_write_once(chardev_t *chardev, const void *data,
|
---|
| 147 | size_t size, size_t *nwritten)
|
---|
[a455321] | 148 | {
|
---|
[19ea61d] | 149 | async_exch_t *exch = async_exchange_begin(chardev->sess);
|
---|
| 150 | ipc_call_t answer;
|
---|
| 151 | aid_t req;
|
---|
| 152 | int rc;
|
---|
[15c5418] | 153 |
|
---|
[19ea61d] | 154 | /* Break down large transfers */
|
---|
| 155 | if (size > DATA_XFER_LIMIT)
|
---|
| 156 | size = DATA_XFER_LIMIT;
|
---|
[a455321] | 157 |
|
---|
[19ea61d] | 158 | req = async_send_0(exch, CHARDEV_WRITE, &answer);
|
---|
| 159 | rc = async_data_write_start(exch, data, size);
|
---|
[15c5418] | 160 | async_exchange_end(exch);
|
---|
[5d50c419] | 161 |
|
---|
[19ea61d] | 162 | if (rc != EOK) {
|
---|
| 163 | async_forget(req);
|
---|
[5d50c419] | 164 | *nwritten = 0;
|
---|
[19ea61d] | 165 | return rc;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[25a179e] | 168 | int retval;
|
---|
[19ea61d] | 169 | async_wait_for(req, &retval);
|
---|
| 170 | if (retval != EOK) {
|
---|
| 171 | *nwritten = 0;
|
---|
| 172 | return retval;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | *nwritten = IPC_GET_ARG2(answer);
|
---|
| 176 | /* In case of partial success, ARG1 contains the error code */
|
---|
[d5c1051] | 177 | return (int) IPC_GET_ARG1(answer);
|
---|
[19ea61d] | 178 | }
|
---|
| 179 |
|
---|
| 180 | /** Write to character device.
|
---|
| 181 | *
|
---|
| 182 | * Write @a size bytes from @a data to character device. On success EOK
|
---|
| 183 | * is returned, all bytes were written and @a *nwritten is set to @a size.
|
---|
| 184 | *
|
---|
| 185 | * On error a non-zero error code is returned and @a *nwritten is filled with
|
---|
| 186 | * the number of bytes that were successfully transferred.
|
---|
| 187 | *
|
---|
| 188 | * @param chardev Character device
|
---|
| 189 | * @param buf Destination buffer
|
---|
| 190 | * @param size Maximum number of bytes to read
|
---|
| 191 | * @param nwritten Place to store actual number of bytes written
|
---|
| 192 | *
|
---|
| 193 | * @return EOK on success or non-zero error code
|
---|
| 194 | */
|
---|
| 195 | int chardev_write(chardev_t *chardev, const void *data, size_t size,
|
---|
| 196 | size_t *nwritten)
|
---|
| 197 | {
|
---|
| 198 | size_t nw;
|
---|
| 199 | size_t p;
|
---|
| 200 | int rc;
|
---|
| 201 |
|
---|
| 202 | p = 0;
|
---|
| 203 | while (p < size) {
|
---|
| 204 | rc = chardev_write_once(chardev, data + p, size - p, &nw);
|
---|
| 205 | /* nw is always valid, we can have partial success */
|
---|
| 206 | p += nw;
|
---|
| 207 |
|
---|
| 208 | if (rc != EOK) {
|
---|
| 209 | /* We can return partial success */
|
---|
| 210 | *nwritten = p;
|
---|
| 211 | return rc;
|
---|
| 212 | }
|
---|
[5d50c419] | 213 | }
|
---|
| 214 |
|
---|
[19ea61d] | 215 | *nwritten = p;
|
---|
[5d50c419] | 216 | return EOK;
|
---|
[a455321] | 217 | }
|
---|
[15c5418] | 218 |
|
---|
| 219 | /** @}
|
---|
| 220 | */
|
---|