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 Block device client interface
|
---|
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 | #include <offset.h>
|
---|
47 |
|
---|
48 | static void bd_cb_conn(ipc_call_t *icall, void *arg);
|
---|
49 |
|
---|
50 | errno_t bd_open(async_sess_t *sess, bd_t **rbd)
|
---|
51 | {
|
---|
52 | bd_t *bd = calloc(1, sizeof(bd_t));
|
---|
53 | if (bd == NULL)
|
---|
54 | return ENOMEM;
|
---|
55 |
|
---|
56 | bd->sess = sess;
|
---|
57 |
|
---|
58 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
59 |
|
---|
60 | port_id_t port;
|
---|
61 | errno_t rc = async_create_callback_port(exch, INTERFACE_BLOCK_CB, 0, 0,
|
---|
62 | bd_cb_conn, bd, &port);
|
---|
63 |
|
---|
64 | async_exchange_end(exch);
|
---|
65 |
|
---|
66 | if (rc != EOK)
|
---|
67 | goto error;
|
---|
68 |
|
---|
69 | *rbd = bd;
|
---|
70 | return EOK;
|
---|
71 |
|
---|
72 | error:
|
---|
73 | if (bd != NULL)
|
---|
74 | free(bd);
|
---|
75 |
|
---|
76 | return rc;
|
---|
77 | }
|
---|
78 |
|
---|
79 | void bd_close(bd_t *bd)
|
---|
80 | {
|
---|
81 | /* XXX Synchronize with bd_cb_conn */
|
---|
82 | free(bd);
|
---|
83 | }
|
---|
84 |
|
---|
85 | errno_t bd_read_blocks(bd_t *bd, aoff64_t ba, size_t cnt, void *data, size_t size)
|
---|
86 | {
|
---|
87 | async_exch_t *exch = async_exchange_begin(bd->sess);
|
---|
88 |
|
---|
89 | ipc_call_t answer;
|
---|
90 | aid_t req = async_send_3(exch, BD_READ_BLOCKS, LOWER32(ba),
|
---|
91 | UPPER32(ba), cnt, &answer);
|
---|
92 | errno_t rc = async_data_read_start(exch, data, size);
|
---|
93 | async_exchange_end(exch);
|
---|
94 |
|
---|
95 | if (rc != EOK) {
|
---|
96 | async_forget(req);
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | errno_t retval;
|
---|
101 | async_wait_for(req, &retval);
|
---|
102 |
|
---|
103 | if (retval != EOK)
|
---|
104 | return retval;
|
---|
105 |
|
---|
106 | return EOK;
|
---|
107 | }
|
---|
108 |
|
---|
109 | errno_t bd_read_toc(bd_t *bd, uint8_t session, void *buf, size_t size)
|
---|
110 | {
|
---|
111 | async_exch_t *exch = async_exchange_begin(bd->sess);
|
---|
112 |
|
---|
113 | ipc_call_t answer;
|
---|
114 | aid_t req = async_send_1(exch, BD_READ_TOC, session, &answer);
|
---|
115 | errno_t rc = async_data_read_start(exch, buf, size);
|
---|
116 | async_exchange_end(exch);
|
---|
117 |
|
---|
118 | if (rc != EOK) {
|
---|
119 | async_forget(req);
|
---|
120 | return rc;
|
---|
121 | }
|
---|
122 |
|
---|
123 | errno_t retval;
|
---|
124 | async_wait_for(req, &retval);
|
---|
125 |
|
---|
126 | if (retval != EOK)
|
---|
127 | return retval;
|
---|
128 |
|
---|
129 | return EOK;
|
---|
130 | }
|
---|
131 |
|
---|
132 | errno_t bd_write_blocks(bd_t *bd, aoff64_t ba, size_t cnt, const void *data,
|
---|
133 | size_t size)
|
---|
134 | {
|
---|
135 | async_exch_t *exch = async_exchange_begin(bd->sess);
|
---|
136 |
|
---|
137 | ipc_call_t answer;
|
---|
138 | aid_t req = async_send_3(exch, BD_WRITE_BLOCKS, LOWER32(ba),
|
---|
139 | UPPER32(ba), cnt, &answer);
|
---|
140 | errno_t rc = async_data_write_start(exch, data, size);
|
---|
141 | async_exchange_end(exch);
|
---|
142 |
|
---|
143 | if (rc != EOK) {
|
---|
144 | async_forget(req);
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 | errno_t retval;
|
---|
149 | async_wait_for(req, &retval);
|
---|
150 | if (retval != EOK)
|
---|
151 | return retval;
|
---|
152 |
|
---|
153 | return EOK;
|
---|
154 | }
|
---|
155 |
|
---|
156 | errno_t bd_sync_cache(bd_t *bd, aoff64_t ba, size_t cnt)
|
---|
157 | {
|
---|
158 | async_exch_t *exch = async_exchange_begin(bd->sess);
|
---|
159 |
|
---|
160 | errno_t rc = async_req_3_0(exch, BD_SYNC_CACHE, LOWER32(ba),
|
---|
161 | UPPER32(ba), cnt);
|
---|
162 | async_exchange_end(exch);
|
---|
163 |
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | errno_t bd_get_block_size(bd_t *bd, size_t *rbsize)
|
---|
168 | {
|
---|
169 | sysarg_t bsize;
|
---|
170 | async_exch_t *exch = async_exchange_begin(bd->sess);
|
---|
171 |
|
---|
172 | errno_t rc = async_req_0_1(exch, BD_GET_BLOCK_SIZE, &bsize);
|
---|
173 | async_exchange_end(exch);
|
---|
174 |
|
---|
175 | if (rc != EOK)
|
---|
176 | return rc;
|
---|
177 |
|
---|
178 | *rbsize = bsize;
|
---|
179 | return EOK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | errno_t bd_get_num_blocks(bd_t *bd, aoff64_t *rnb)
|
---|
183 | {
|
---|
184 | sysarg_t nb_l;
|
---|
185 | sysarg_t nb_h;
|
---|
186 | async_exch_t *exch = async_exchange_begin(bd->sess);
|
---|
187 |
|
---|
188 | errno_t rc = async_req_0_2(exch, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
|
---|
189 | async_exchange_end(exch);
|
---|
190 |
|
---|
191 | if (rc != EOK)
|
---|
192 | return rc;
|
---|
193 |
|
---|
194 | *rnb = (aoff64_t) MERGE_LOUP32(nb_l, nb_h);
|
---|
195 | return EOK;
|
---|
196 | }
|
---|
197 |
|
---|
198 | static void bd_cb_conn(ipc_call_t *icall, void *arg)
|
---|
199 | {
|
---|
200 | bd_t *bd = (bd_t *)arg;
|
---|
201 |
|
---|
202 | (void)bd;
|
---|
203 |
|
---|
204 | while (true) {
|
---|
205 | ipc_call_t call;
|
---|
206 | async_get_call(&call);
|
---|
207 |
|
---|
208 | if (!IPC_GET_IMETHOD(call)) {
|
---|
209 | async_answer_0(&call, EOK);
|
---|
210 | return;
|
---|
211 | }
|
---|
212 |
|
---|
213 | switch (IPC_GET_IMETHOD(call)) {
|
---|
214 | default:
|
---|
215 | async_answer_0(&call, ENOTSUP);
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | /** @}
|
---|
221 | */
|
---|