source: mainline/uspace/lib/c/generic/bd.c@ 38db6288

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

Factor out client and server IPC stubs for block devices.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
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/**
33 * @file
34 * @brief IP link client stub
35 */
36
37#include <async.h>
38#include <assert.h>
39#include <bd.h>
40#include <errno.h>
41#include <ipc/bd.h>
42#include <ipc/services.h>
43#include <loc.h>
44#include <macros.h>
45#include <stdlib.h>
46
47static void bd_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
48
49int bd_open(async_sess_t *sess, bd_t **rbd)
50{
51 bd_t *bd = calloc(1, sizeof(bd_t));
52 if (bd == NULL)
53 return ENOMEM;
54
55 bd->sess = sess;
56
57 async_exch_t *exch = async_exchange_begin(sess);
58
59 int rc = async_connect_to_me(exch, 0, 0, 0, bd_cb_conn, bd);
60 async_exchange_end(exch);
61
62 if (rc != EOK)
63 goto error;
64
65 *rbd = bd;
66 return EOK;
67
68error:
69 if (bd != NULL)
70 free(bd);
71
72 return rc;
73}
74
75void bd_close(bd_t *bd)
76{
77 /* XXX Synchronize with bd_cb_conn */
78 free(bd);
79}
80
81int bd_read_blocks(bd_t *bd, aoff64_t ba, size_t cnt, void *data, size_t size)
82{
83 async_exch_t *exch = async_exchange_begin(bd->sess);
84
85 ipc_call_t answer;
86 aid_t req = async_send_3(exch, BD_READ_BLOCKS, LOWER32(ba),
87 UPPER32(ba), cnt, &answer);
88 int rc = async_data_read_start(exch, data, size);
89 async_exchange_end(exch);
90
91 if (rc != EOK) {
92 async_forget(req);
93 return rc;
94 }
95
96 sysarg_t retval;
97 async_wait_for(req, &retval);
98
99 if (retval != EOK)
100 return retval;
101
102 return EOK;
103}
104
105int bd_read_toc(bd_t *bd, uint8_t session, void *buf, size_t size)
106{
107 async_exch_t *exch = async_exchange_begin(bd->sess);
108
109 ipc_call_t answer;
110 aid_t req = async_send_1(exch, BD_READ_TOC, session, &answer);
111 int rc = async_data_read_start(exch, buf, size);
112 async_exchange_end(exch);
113
114 if (rc != EOK) {
115 async_forget(req);
116 return rc;
117 }
118
119 sysarg_t retval;
120 async_wait_for(req, &retval);
121
122 if (retval != EOK)
123 return retval;
124
125 return EOK;
126}
127
128int bd_write_blocks(bd_t *bd, aoff64_t ba, size_t cnt, const void *data,
129 size_t size)
130{
131 async_exch_t *exch = async_exchange_begin(bd->sess);
132
133 ipc_call_t answer;
134 aid_t req = async_send_3(exch, BD_WRITE_BLOCKS, LOWER32(ba),
135 UPPER32(ba), cnt, &answer);
136 int rc = async_data_write_start(exch, data, size);
137 async_exchange_end(exch);
138
139 if (rc != EOK) {
140 async_forget(req);
141 return rc;
142 }
143
144 sysarg_t retval;
145 async_wait_for(req, &retval);
146 if (retval != EOK)
147 return retval;
148
149 return EOK;
150}
151
152int bd_get_block_size(bd_t *bd, size_t *rbsize)
153{
154 sysarg_t bsize;
155 async_exch_t *exch = async_exchange_begin(bd->sess);
156
157 int rc = async_req_0_1(exch, BD_GET_BLOCK_SIZE, &bsize);
158 async_exchange_end(exch);
159
160 if (rc != EOK)
161 return rc;
162
163 *rbsize = bsize;
164 return EOK;
165}
166
167int bd_get_num_blocks(bd_t *bd, aoff64_t *rnb)
168{
169 sysarg_t nb_l;
170 sysarg_t nb_h;
171 async_exch_t *exch = async_exchange_begin(bd->sess);
172
173 int rc = async_req_0_2(exch, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
174 async_exchange_end(exch);
175
176 if (rc != EOK)
177 return rc;
178
179 *rnb = (aoff64_t) MERGE_LOUP32(nb_l, nb_h);
180 return EOK;
181}
182
183static void bd_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
184{
185 bd_t *bd = (bd_t *)arg;
186
187 (void)bd;
188
189 while (true) {
190 ipc_call_t call;
191 ipc_callid_t callid = async_get_call(&call);
192
193 if (!IPC_GET_IMETHOD(call)) {
194 /* TODO: Handle hangup */
195 return;
196 }
197
198 switch (IPC_GET_IMETHOD(call)) {
199 default:
200 async_answer_0(callid, ENOTSUP);
201 }
202 }
203}
204
205/** @}
206 */
Note: See TracBrowser for help on using the repository browser.