source: mainline/uspace/lib/c/generic/bd.c@ 889cdb1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 889cdb1 was 889cdb1, checked in by Jakub Jermar <jakub@…>, 7 years ago

Always answer the IPC_M_PHONE_HUNGUP message

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[4802dd7]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
[b2f542a]34 * @brief Block device client interface
[4802dd7]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>
[23c8acd9]46#include <offset.h>
[4802dd7]47
[984a9ba]48static void bd_cb_conn(ipc_call_t *icall, void *arg);
[4802dd7]49
[b7fd2a0]50errno_t bd_open(async_sess_t *sess, bd_t **rbd)
[4802dd7]51{
52 bd_t *bd = calloc(1, sizeof(bd_t));
53 if (bd == NULL)
54 return ENOMEM;
[a35b458]55
[4802dd7]56 bd->sess = sess;
[a35b458]57
[4802dd7]58 async_exch_t *exch = async_exchange_begin(sess);
[a35b458]59
[f9b2cb4c]60 port_id_t port;
[b7fd2a0]61 errno_t rc = async_create_callback_port(exch, INTERFACE_BLOCK_CB, 0, 0,
[f9b2cb4c]62 bd_cb_conn, bd, &port);
[a35b458]63
[4802dd7]64 async_exchange_end(exch);
[a35b458]65
[4802dd7]66 if (rc != EOK)
67 goto error;
[a35b458]68
[4802dd7]69 *rbd = bd;
70 return EOK;
[a35b458]71
[4802dd7]72error:
73 if (bd != NULL)
74 free(bd);
[a35b458]75
[4802dd7]76 return rc;
77}
78
79void bd_close(bd_t *bd)
80{
81 /* XXX Synchronize with bd_cb_conn */
82 free(bd);
83}
84
[b7fd2a0]85errno_t bd_read_blocks(bd_t *bd, aoff64_t ba, size_t cnt, void *data, size_t size)
[4802dd7]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);
[b7fd2a0]92 errno_t rc = async_data_read_start(exch, data, size);
[4802dd7]93 async_exchange_end(exch);
94
95 if (rc != EOK) {
96 async_forget(req);
97 return rc;
98 }
99
[b7fd2a0]100 errno_t retval;
[4802dd7]101 async_wait_for(req, &retval);
102
103 if (retval != EOK)
104 return retval;
105
106 return EOK;
107}
108
[b7fd2a0]109errno_t bd_read_toc(bd_t *bd, uint8_t session, void *buf, size_t size)
[4802dd7]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);
[b7fd2a0]115 errno_t rc = async_data_read_start(exch, buf, size);
[4802dd7]116 async_exchange_end(exch);
117
118 if (rc != EOK) {
119 async_forget(req);
120 return rc;
121 }
122
[b7fd2a0]123 errno_t retval;
[4802dd7]124 async_wait_for(req, &retval);
125
126 if (retval != EOK)
127 return retval;
128
129 return EOK;
130}
131
[b7fd2a0]132errno_t bd_write_blocks(bd_t *bd, aoff64_t ba, size_t cnt, const void *data,
[4802dd7]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);
[b7fd2a0]140 errno_t rc = async_data_write_start(exch, data, size);
[4802dd7]141 async_exchange_end(exch);
142
143 if (rc != EOK) {
144 async_forget(req);
145 return rc;
146 }
147
[b7fd2a0]148 errno_t retval;
[4802dd7]149 async_wait_for(req, &retval);
150 if (retval != EOK)
151 return retval;
152
153 return EOK;
154}
155
[b7fd2a0]156errno_t bd_sync_cache(bd_t *bd, aoff64_t ba, size_t cnt)
[dd8b6a8]157{
158 async_exch_t *exch = async_exchange_begin(bd->sess);
159
[b7fd2a0]160 errno_t rc = async_req_3_0(exch, BD_SYNC_CACHE, LOWER32(ba),
[dd8b6a8]161 UPPER32(ba), cnt);
162 async_exchange_end(exch);
163
164 return rc;
165}
166
[b7fd2a0]167errno_t bd_get_block_size(bd_t *bd, size_t *rbsize)
[4802dd7]168{
169 sysarg_t bsize;
170 async_exch_t *exch = async_exchange_begin(bd->sess);
171
[b7fd2a0]172 errno_t rc = async_req_0_1(exch, BD_GET_BLOCK_SIZE, &bsize);
[4802dd7]173 async_exchange_end(exch);
174
175 if (rc != EOK)
176 return rc;
177
178 *rbsize = bsize;
179 return EOK;
180}
181
[b7fd2a0]182errno_t bd_get_num_blocks(bd_t *bd, aoff64_t *rnb)
[4802dd7]183{
184 sysarg_t nb_l;
185 sysarg_t nb_h;
186 async_exch_t *exch = async_exchange_begin(bd->sess);
187
[b7fd2a0]188 errno_t rc = async_req_0_2(exch, BD_GET_NUM_BLOCKS, &nb_l, &nb_h);
[4802dd7]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
[984a9ba]198static void bd_cb_conn(ipc_call_t *icall, void *arg)
[4802dd7]199{
200 bd_t *bd = (bd_t *)arg;
201
202 (void)bd;
203
204 while (true) {
205 ipc_call_t call;
[984a9ba]206 async_get_call(&call);
[4802dd7]207
208 if (!IPC_GET_IMETHOD(call)) {
[889cdb1]209 async_answer_0(&call, EOK);
[4802dd7]210 return;
211 }
212
213 switch (IPC_GET_IMETHOD(call)) {
214 default:
[984a9ba]215 async_answer_0(&call, ENOTSUP);
[4802dd7]216 }
217 }
218}
219
220/** @}
221 */
Note: See TracBrowser for help on using the repository browser.