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

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

chardev_open, chardev_close.

  • Property mode set to 100644
File size: 3.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 <stdlib.h>
43
44/** Open character device.
45 *
46 * @param sess Session with the character device
47 * @param rchardev Place to store pointer to the new character device structure
48 *
49 * @return EOK on success, ENOMEM if out of memory, EIO on I/O error
50 */
51int chardev_open(async_sess_t *sess, chardev_t **rchardev)
52{
53 chardev_t *chardev;
54
55 chardev = calloc(1, sizeof(chardev_t));
56 if (chardev == NULL)
57 return ENOMEM;
58
59 chardev->sess = sess;
60 *rchardev = chardev;
61
62 /* EIO might be used in a future implementation */
63 return EOK;
64}
65
66/** Close character device.
67 *
68 * Frees the character device structure. The underlying session is
69 * not affected.
70 *
71 * @param chardev Character device or @c NULL
72 */
73void chardev_close(chardev_t *chardev)
74{
75 free(chardev);
76}
77
78ssize_t chardev_read(chardev_t *chardev, void *data, size_t size)
79{
80 if (size > 4 * sizeof(sysarg_t))
81 return ELIMIT;
82
83 async_exch_t *exch = async_exchange_begin(chardev->sess);
84 sysarg_t message[4] = { 0 };
85 const ssize_t ret = async_req_1_4(exch, CHARDEV_READ, size,
86 &message[0], &message[1], &message[2], &message[3]);
87 async_exchange_end(exch);
88 if (ret > 0 && (size_t)ret <= size)
89 memcpy(data, message, size);
90 return ret;
91}
92
93ssize_t chardev_write(chardev_t *chardev, const void *data, size_t size)
94{
95 int ret;
96
97 if (size > 3 * sizeof(sysarg_t))
98 return ELIMIT;
99
100 async_exch_t *exch = async_exchange_begin(chardev->sess);
101 sysarg_t message[3] = { 0 };
102 memcpy(message, data, size);
103 ret = async_req_4_0(exch, CHARDEV_WRITE, size,
104 message[0], message[1], message[2]);
105 async_exchange_end(exch);
106 return ret;
107}
108
109/** @}
110 */
Note: See TracBrowser for help on using the repository browser.