source: mainline/uspace/lib/c/generic/device/char.c@ 96b02eb9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 96b02eb9 was 96b02eb9, checked in by Martin Decky <martin@…>, 15 years ago

more unification of basic types

  • use sysarg_t and native_t (unsigned and signed variant) in both kernel and uspace
  • remove ipcarg_t in favour of sysarg_t

(no change in functionality)

  • Property mode set to 100644
File size: 4.0 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 libc
30 * @{
31 */
32/** @file
33 */
34
35#include <ipc/dev_iface.h>
36#include <device/char.h>
37#include <errno.h>
38#include <async.h>
39#include <malloc.h>
40#include <stdio.h>
41
42/** Read to or write from device.
43 *
44 * Helper function to read to or write from a device
45 * using its character interface.
46 *
47 * @param dev_phone Phone to the device.
48 * @param buf Buffer for the data read
49 * from or written to the device.
50 * @param len Maximum length of the data to be
51 * read or written.
52 * @param read Read from the device if true,
53 * write to it otherwise.
54 *
55 * @return Non-negative number of bytes actually read
56 * from or written to the device on success,
57 * negative error number otherwise.
58 *
59 */
60static ssize_t rw_dev(int dev_phone, void *buf, size_t len, bool read)
61{
62 async_serialize_start();
63
64 ipc_call_t answer;
65 aid_t req;
66 int ret;
67
68 if (read) {
69 req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE),
70 CHAR_READ_DEV, &answer);
71 ret = async_data_read_start(dev_phone, buf, len);
72 } else {
73 req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE),
74 CHAR_WRITE_DEV, &answer);
75 ret = async_data_write_start(dev_phone, buf, len);
76 }
77
78 sysarg_t rc;
79 if (ret != EOK) {
80 async_wait_for(req, &rc);
81 async_serialize_end();
82 if (rc == EOK)
83 return (ssize_t) ret;
84
85 return (ssize_t) rc;
86 }
87
88 async_wait_for(req, &rc);
89 async_serialize_end();
90
91 ret = (int) rc;
92 if (ret != EOK)
93 return (ssize_t) ret;
94
95 return (ssize_t) IPC_GET_ARG1(answer);
96}
97
98/** Read from device using its character interface.
99 *
100 * @param dev_phone Phone to the device.
101 * @param buf Output buffer for the data
102 * read from the device.
103 * @param len Maximum length of the data to be read.
104 *
105 * @return Non-negative number of bytes actually read
106 * from the device on success, negative error
107 * number otherwise.
108 *
109 */
110ssize_t read_dev(int dev_phone, void *buf, size_t len)
111{
112 return rw_dev(dev_phone, buf, len, true);
113}
114
115/** Write to device using its character interface.
116 *
117 * @param dev_phone Phone to the device.
118 * @param buf Input buffer containg the data
119 * to be written to the device.
120 * @param len Maximum length of the data to be written.
121 *
122 * @return Non-negative number of bytes actually written
123 * to the device on success, negative error number
124 * otherwise.
125 *
126 */
127ssize_t write_dev(int dev_phone, void *buf, size_t len)
128{
129 return rw_dev(dev_phone, buf, len, false);
130}
131
132/** @}
133 */
Note: See TracBrowser for help on using the repository browser.