[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 |
|
---|
[5d50c419] | 79 | int chardev_read(chardev_t *chardev, void *data, size_t size, size_t *nread)
|
---|
[a455321] | 80 | {
|
---|
| 81 | if (size > 4 * sizeof(sysarg_t))
|
---|
| 82 | return ELIMIT;
|
---|
| 83 |
|
---|
[15c5418] | 84 | async_exch_t *exch = async_exchange_begin(chardev->sess);
|
---|
[a455321] | 85 | sysarg_t message[4] = { 0 };
|
---|
[75751db6] | 86 | const ssize_t ret = async_req_1_4(exch, CHARDEV_READ, size,
|
---|
[a455321] | 87 | &message[0], &message[1], &message[2], &message[3]);
|
---|
[15c5418] | 88 | async_exchange_end(exch);
|
---|
[a455321] | 89 | if (ret > 0 && (size_t)ret <= size)
|
---|
| 90 | memcpy(data, message, size);
|
---|
[5d50c419] | 91 |
|
---|
| 92 | if (ret < 0) {
|
---|
| 93 | *nread = 0;
|
---|
| 94 | return ret;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | *nread = ret;
|
---|
| 98 | return EOK;
|
---|
[a455321] | 99 | }
|
---|
| 100 |
|
---|
[5d50c419] | 101 | int chardev_write(chardev_t *chardev, const void *data, size_t size,
|
---|
| 102 | size_t *nwritten)
|
---|
[a455321] | 103 | {
|
---|
[15c5418] | 104 | int ret;
|
---|
| 105 |
|
---|
[a455321] | 106 | if (size > 3 * sizeof(sysarg_t))
|
---|
| 107 | return ELIMIT;
|
---|
| 108 |
|
---|
[15c5418] | 109 | async_exch_t *exch = async_exchange_begin(chardev->sess);
|
---|
[a455321] | 110 | sysarg_t message[3] = { 0 };
|
---|
| 111 | memcpy(message, data, size);
|
---|
[15c5418] | 112 | ret = async_req_4_0(exch, CHARDEV_WRITE, size,
|
---|
[a455321] | 113 | message[0], message[1], message[2]);
|
---|
[15c5418] | 114 | async_exchange_end(exch);
|
---|
[5d50c419] | 115 |
|
---|
| 116 | if (ret < 0) {
|
---|
| 117 | *nwritten = 0;
|
---|
| 118 | return ret;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | *nwritten = ret;
|
---|
| 122 | return EOK;
|
---|
[a455321] | 123 | }
|
---|
[15c5418] | 124 |
|
---|
| 125 | /** @}
|
---|
| 126 | */
|
---|