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

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

improve read_dev() and write_dev() interface

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[25a7e11d]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 */
[df6b760]28
[25a7e11d]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>
[f658458]40#include <stdio.h>
[25a7e11d]41
[df6b760]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 *
[ca97cad]59 */
[df6b760]60static ssize_t rw_dev(int dev_phone, void *buf, size_t len, bool read)
[ca97cad]61{
[25a7e11d]62 async_serialize_start();
63
[df6b760]64 ipc_call_t answer;
[ca97cad]65 aid_t req;
[c47e1a8]66 int ret;
[25a7e11d]67
[ca97cad]68 if (read) {
[df6b760]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);
[ca97cad]72 } else {
[df6b760]73 req = async_send_1(dev_phone, DEV_IFACE_ID(CHAR_DEV_IFACE),
74 CHAR_WRITE_DEV, &answer);
[c47e1a8]75 ret = async_data_write_start(dev_phone, buf, len);
[ca97cad]76 }
[bb864a0]77
[c47e1a8]78 ipcarg_t rc;
[df6b760]79 if (ret != EOK) {
[c47e1a8]80 async_wait_for(req, &rc);
[25a7e11d]81 async_serialize_end();
[df6b760]82 if (rc == EOK)
83 return (ssize_t) ret;
84
85 return (ssize_t) rc;
[25a7e11d]86 }
87
88 async_wait_for(req, &rc);
89 async_serialize_end();
90
[df6b760]91 ret = (int) rc;
92 if (ret != EOK)
93 return (ssize_t) ret;
[25a7e11d]94
[df6b760]95 return (ssize_t) IPC_GET_ARG1(answer);
[25a7e11d]96}
97
[df6b760]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 *
[ca97cad]109 */
[df6b760]110ssize_t read_dev(int dev_phone, void *buf, size_t len)
111{
[ca97cad]112 return rw_dev(dev_phone, buf, len, true);
113}
114
[df6b760]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 *
[ca97cad]126 */
[df6b760]127ssize_t write_dev(int dev_phone, void *buf, size_t len)
[25a7e11d]128{
[ca97cad]129 return rw_dev(dev_phone, buf, len, false);
[25a7e11d]130}
131
[df6b760]132/** @}
133 */
Note: See TracBrowser for help on using the repository browser.