source: mainline/uspace/drv/bus/usb/usbmast/main.c@ dd8b6a8

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

Add synchronize cache operation to block layer and usbmast.

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2011 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup drvusbmast
31 * @{
32 */
33/**
34 * @file
35 * Main routines of USB mass storage driver.
36 */
37#include <as.h>
38#include <async.h>
39#include <bd_srv.h>
40#include <macros.h>
41#include <usb/dev/driver.h>
42#include <usb/debug.h>
43#include <usb/classes/classes.h>
44#include <usb/classes/massstor.h>
45#include <errno.h>
46#include <str_error.h>
47#include "cmdw.h"
48#include "bo_trans.h"
49#include "scsi_ms.h"
50#include "usbmast.h"
51
52#define NAME "usbmast"
53
54#define GET_BULK_IN(dev) ((dev)->pipes[BULK_IN_EP].pipe)
55#define GET_BULK_OUT(dev) ((dev)->pipes[BULK_OUT_EP].pipe)
56
57static const usb_endpoint_description_t bulk_in_ep = {
58 .transfer_type = USB_TRANSFER_BULK,
59 .direction = USB_DIRECTION_IN,
60 .interface_class = USB_CLASS_MASS_STORAGE,
61 .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
62 .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
63 .flags = 0
64};
65static const usb_endpoint_description_t bulk_out_ep = {
66 .transfer_type = USB_TRANSFER_BULK,
67 .direction = USB_DIRECTION_OUT,
68 .interface_class = USB_CLASS_MASS_STORAGE,
69 .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
70 .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
71 .flags = 0
72};
73
74static const usb_endpoint_description_t *mast_endpoints[] = {
75 &bulk_in_ep,
76 &bulk_out_ep,
77 NULL
78};
79
80static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun);
81static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
82 void *arg);
83
84static int usbmast_bd_open(bd_srvs_t *, bd_srv_t *);
85static int usbmast_bd_close(bd_srv_t *);
86static int usbmast_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
87static int usbmast_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
88static int usbmast_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
89static int usbmast_bd_get_block_size(bd_srv_t *, size_t *);
90static int usbmast_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
91
92static bd_ops_t usbmast_bd_ops = {
93 .open = usbmast_bd_open,
94 .close = usbmast_bd_close,
95 .read_blocks = usbmast_bd_read_blocks,
96 .sync_cache = usbmast_bd_sync_cache,
97 .write_blocks = usbmast_bd_write_blocks,
98 .get_block_size = usbmast_bd_get_block_size,
99 .get_num_blocks = usbmast_bd_get_num_blocks
100};
101
102static usbmast_fun_t *bd_srv_usbmast(bd_srv_t *bd)
103{
104 return (usbmast_fun_t *) bd->srvs->sarg;
105}
106
107/** Callback when a device is removed from the system.
108 *
109 * @param dev Representation of USB device.
110 * @return Error code.
111 */
112static int usbmast_device_gone(usb_device_t *dev)
113{
114 usbmast_dev_t *mdev = dev->driver_data;
115 assert(mdev);
116
117 for (size_t i = 0; i < mdev->lun_count; ++i) {
118 const int rc = ddf_fun_unbind(mdev->luns[i]);
119 if (rc != EOK) {
120 usb_log_error("Failed to unbind LUN function %zu: "
121 "%s\n", i, str_error(rc));
122 return rc;
123 }
124 ddf_fun_destroy(mdev->luns[i]);
125 mdev->luns[i] = NULL;
126 }
127 free(mdev->luns);
128 return EOK;
129}
130
131/** Callback when a device is about to be removed.
132 *
133 * @param dev Representation of USB device.
134 * @return Error code.
135 */
136static int usbmast_device_remove(usb_device_t *dev)
137{
138 //TODO: flush buffers, or whatever.
139 //TODO: remove device
140 return ENOTSUP;
141}
142
143/** Callback when new device is attached and recognized as a mass storage.
144 *
145 * @param dev Representation of USB device.
146 * @return Error code.
147 */
148static int usbmast_device_add(usb_device_t *dev)
149{
150 int rc;
151 usbmast_dev_t *mdev = NULL;
152 unsigned i;
153
154 /* Allocate softstate */
155 mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
156 if (mdev == NULL) {
157 usb_log_error("Failed allocating softstate.\n");
158 return ENOMEM;
159 }
160
161 mdev->ddf_dev = dev->ddf_dev;
162 mdev->usb_dev = dev;
163
164 usb_log_info("Initializing mass storage `%s'.\n", ddf_dev_get_name(dev->ddf_dev));
165 usb_log_debug("Bulk in endpoint: %d [%zuB].\n",
166 dev->pipes[BULK_IN_EP].pipe.endpoint_no,
167 dev->pipes[BULK_IN_EP].pipe.max_packet_size);
168 usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
169 dev->pipes[BULK_OUT_EP].pipe.endpoint_no,
170 dev->pipes[BULK_OUT_EP].pipe.max_packet_size);
171
172 usb_log_debug("Get LUN count...\n");
173 mdev->lun_count = usb_masstor_get_lun_count(mdev);
174 mdev->luns = calloc(mdev->lun_count, sizeof(ddf_fun_t*));
175 if (mdev->luns == NULL) {
176 rc = ENOMEM;
177 usb_log_error("Failed allocating luns table.\n");
178 goto error;
179 }
180
181 for (i = 0; i < mdev->lun_count; i++) {
182 rc = usbmast_fun_create(mdev, i);
183 if (rc != EOK)
184 goto error;
185 }
186
187 return EOK;
188error:
189 /* Destroy functions */
190 for (size_t i = 0; i < mdev->lun_count; ++i) {
191 if (mdev->luns[i] == NULL)
192 continue;
193 const int rc = ddf_fun_unbind(mdev->luns[i]);
194 if (rc != EOK) {
195 usb_log_warning("Failed to unbind LUN function %zu: "
196 "%s.\n", i, str_error(rc));
197 }
198 ddf_fun_destroy(mdev->luns[i]);
199 }
200 free(mdev->luns);
201 return rc;
202}
203
204/** Create mass storage function.
205 *
206 * Called once for each LUN.
207 *
208 * @param mdev Mass storage device
209 * @param lun LUN
210 * @return EOK on success or negative error code.
211 */
212static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun)
213{
214 int rc;
215 char *fun_name = NULL;
216 ddf_fun_t *fun = NULL;
217 usbmast_fun_t *mfun = NULL;
218
219 if (asprintf(&fun_name, "l%u", lun) < 0) {
220 usb_log_error("Out of memory.\n");
221 rc = ENOMEM;
222 goto error;
223 }
224
225 fun = ddf_fun_create(mdev->ddf_dev, fun_exposed, fun_name);
226 if (fun == NULL) {
227 usb_log_error("Failed to create DDF function %s.\n", fun_name);
228 rc = ENOMEM;
229 goto error;
230 }
231
232 /* Allocate soft state */
233 mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
234 if (mfun == NULL) {
235 usb_log_error("Failed allocating softstate.\n");
236 rc = ENOMEM;
237 goto error;
238 }
239
240 mfun->ddf_fun = fun;
241 mfun->mdev = mdev;
242 mfun->lun = lun;
243
244 bd_srvs_init(&mfun->bds);
245 mfun->bds.ops = &usbmast_bd_ops;
246 mfun->bds.sarg = mfun;
247
248 /* Set up a connection handler. */
249 ddf_fun_set_conn_handler(fun, usbmast_bd_connection);
250
251 usb_log_debug("Inquire...\n");
252 usbmast_inquiry_data_t inquiry;
253 rc = usbmast_inquiry(mfun, &inquiry);
254 if (rc != EOK) {
255 usb_log_warning("Failed to inquire device `%s': %s.\n",
256 ddf_dev_get_name(mdev->ddf_dev), str_error(rc));
257 rc = EIO;
258 goto error;
259 }
260
261 usb_log_info("Mass storage `%s' LUN %u: " \
262 "%s by %s rev. %s is %s (%s).\n",
263 ddf_dev_get_name(mdev->ddf_dev),
264 lun,
265 inquiry.product,
266 inquiry.vendor,
267 inquiry.revision,
268 usbmast_scsi_dev_type_str(inquiry.device_type),
269 inquiry.removable ? "removable" : "non-removable");
270
271 uint32_t nblocks, block_size;
272
273 rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
274 if (rc != EOK) {
275 usb_log_warning("Failed to read capacity, device `%s': %s.\n",
276 ddf_dev_get_name(mdev->ddf_dev), str_error(rc));
277 rc = EIO;
278 goto error;
279 }
280
281 usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
282 "block_size=%" PRIu32 "\n", nblocks, block_size);
283
284 mfun->nblocks = nblocks;
285 mfun->block_size = block_size;
286
287 rc = ddf_fun_bind(fun);
288 if (rc != EOK) {
289 usb_log_error("Failed to bind DDF function %s: %s.\n",
290 fun_name, str_error(rc));
291 goto error;
292 }
293
294 free(fun_name);
295 mdev->luns[lun] = fun;
296
297 return EOK;
298
299 /* Error cleanup */
300error:
301 if (fun != NULL)
302 ddf_fun_destroy(fun);
303 if (fun_name != NULL)
304 free(fun_name);
305 return rc;
306}
307
308/** Blockdev client connection handler. */
309static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
310 void *arg)
311{
312 usbmast_fun_t *mfun;
313
314 mfun = (usbmast_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
315 bd_conn(iid, icall, &mfun->bds);
316}
317
318/** Open device. */
319static int usbmast_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
320{
321 return EOK;
322}
323
324/** Close device. */
325static int usbmast_bd_close(bd_srv_t *bd)
326{
327 return EOK;
328}
329
330/** Read blocks from the device. */
331static int usbmast_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
332 size_t size)
333{
334 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
335
336 if (size < cnt * mfun->block_size)
337 return EINVAL;
338
339 return usbmast_read(mfun, ba, cnt, buf);
340}
341
342/** Synchronize blocks to nonvolatile storage. */
343static int usbmast_bd_sync_cache(bd_srv_t *bd, uint64_t ba, size_t cnt)
344{
345 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
346
347 return usbmast_sync_cache(mfun, ba, cnt);
348}
349
350/** Write blocks to the device. */
351static int usbmast_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
352 const void *buf, size_t size)
353{
354 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
355
356 if (size < cnt * mfun->block_size)
357 return EINVAL;
358
359 return usbmast_write(mfun, ba, cnt, buf);
360}
361
362/** Get device block size. */
363static int usbmast_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
364{
365 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
366 *rsize = mfun->block_size;
367 return EOK;
368}
369
370/** Get number of blocks on device. */
371static int usbmast_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
372{
373 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
374 *rnb = mfun->nblocks;
375 return EOK;
376}
377
378
379/** USB mass storage driver ops. */
380static const usb_driver_ops_t usbmast_driver_ops = {
381 .device_add = usbmast_device_add,
382 .device_rem = usbmast_device_remove,
383 .device_gone = usbmast_device_gone,
384};
385
386/** USB mass storage driver. */
387static const usb_driver_t usbmast_driver = {
388 .name = NAME,
389 .ops = &usbmast_driver_ops,
390 .endpoints = mast_endpoints
391};
392
393int main(int argc, char *argv[])
394{
395 log_init(NAME);
396
397 return usb_driver_main(&usbmast_driver);
398}
399
400/**
401 * @}
402 */
Note: See TracBrowser for help on using the repository browser.