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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 296fcce was 3cc55b47, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

usbmast: Set bulk pipes before probing the device

Fixes usbmast segfault

  • Property mode set to 100644
File size: 10.2 KB
RevLine 
[2715978]1/*
2 * Copyright (c) 2011 Vojtech Horky
[ff65e91]3 * Copyright (c) 2011 Jiri Svoboda
[2715978]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 */
[ff65e91]37#include <as.h>
38#include <async.h>
[4802dd7]39#include <bd_srv.h>
[ff65e91]40#include <macros.h>
[7d521e24]41#include <usb/dev/driver.h>
[2715978]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>
[89d3f3c7]47#include "cmdw.h"
48#include "bo_trans.h"
[6430ac6]49#include "scsi_ms.h"
[7190bbc]50#include "usbmast.h"
[2715978]51
52#define NAME "usbmast"
53
[9871bca]54static const usb_endpoint_description_t bulk_in_ep = {
[2715978]55 .transfer_type = USB_TRANSFER_BULK,
56 .direction = USB_DIRECTION_IN,
57 .interface_class = USB_CLASS_MASS_STORAGE,
58 .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
59 .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
60 .flags = 0
61};
[9871bca]62static const usb_endpoint_description_t bulk_out_ep = {
[2715978]63 .transfer_type = USB_TRANSFER_BULK,
64 .direction = USB_DIRECTION_OUT,
65 .interface_class = USB_CLASS_MASS_STORAGE,
66 .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
67 .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
68 .flags = 0
69};
70
[b803845]71static const usb_endpoint_description_t *mast_endpoints[] = {
[2715978]72 &bulk_in_ep,
73 &bulk_out_ep,
74 NULL
75};
76
[2aceec5]77static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun);
[ff65e91]78static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
79 void *arg);
80
[135486d]81static int usbmast_bd_open(bd_srvs_t *, bd_srv_t *);
[4802dd7]82static int usbmast_bd_close(bd_srv_t *);
83static int usbmast_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
84static int usbmast_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
85static int usbmast_bd_get_block_size(bd_srv_t *, size_t *);
86static int usbmast_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
87
88static bd_ops_t usbmast_bd_ops = {
89 .open = usbmast_bd_open,
90 .close = usbmast_bd_close,
91 .read_blocks = usbmast_bd_read_blocks,
92 .write_blocks = usbmast_bd_write_blocks,
93 .get_block_size = usbmast_bd_get_block_size,
94 .get_num_blocks = usbmast_bd_get_num_blocks
95};
96
97static usbmast_fun_t *bd_srv_usbmast(bd_srv_t *bd)
98{
[f73b291]99 return (usbmast_fun_t *) bd->srvs->sarg;
[4802dd7]100}
101
[3002434]102/** Callback when a device is removed from the system.
103 *
104 * @param dev Representation of USB device.
105 * @return Error code.
106 */
107static int usbmast_device_gone(usb_device_t *dev)
108{
[c39e9fb]109 usbmast_dev_t *mdev = usb_device_data_get(dev);
[3002434]110 assert(mdev);
111
112 for (size_t i = 0; i < mdev->lun_count; ++i) {
113 const int rc = ddf_fun_unbind(mdev->luns[i]);
114 if (rc != EOK) {
115 usb_log_error("Failed to unbind LUN function %zu: "
116 "%s\n", i, str_error(rc));
117 return rc;
118 }
119 ddf_fun_destroy(mdev->luns[i]);
120 mdev->luns[i] = NULL;
121 }
122 free(mdev->luns);
123 return EOK;
124}
125
[9871bca]126/** Callback when a device is about to be removed.
127 *
128 * @param dev Representation of USB device.
129 * @return Error code.
130 */
131static int usbmast_device_remove(usb_device_t *dev)
132{
133 //TODO: flush buffers, or whatever.
[dcc10b8d]134 //TODO: remove device
[9871bca]135 return ENOTSUP;
136}
137
[2715978]138/** Callback when new device is attached and recognized as a mass storage.
139 *
[9871bca]140 * @param dev Representation of USB device.
[2715978]141 * @return Error code.
142 */
[1a4ea01d]143static int usbmast_device_add(usb_device_t *dev)
[2715978]144{
145 int rc;
[2aceec5]146 usbmast_dev_t *mdev = NULL;
147 unsigned i;
[2715978]148
[3e23316]149 usb_endpoint_mapping_t *epm_in =
150 usb_device_get_mapped_ep_desc(dev, &bulk_in_ep);
151 usb_endpoint_mapping_t *epm_out =
152 usb_device_get_mapped_ep_desc(dev, &bulk_out_ep);
153 if (!epm_in || !epm_out || !epm_in->present || !epm_out->present) {
154 usb_log_error("Required EPs were not mapped.\n");
155 return ENOENT;
156 }
157
[ff65e91]158 /* Allocate softstate */
[065064e6]159 mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
[2aceec5]160 if (mdev == NULL) {
[ff65e91]161 usb_log_error("Failed allocating softstate.\n");
[d6c953e9]162 return ENOMEM;
[2715978]163 }
[ff65e91]164
[2aceec5]165 mdev->usb_dev = dev;
[ff65e91]166
[7c69861]167 usb_log_info("Initializing mass storage `%s'.\n",
168 usb_device_get_name(dev));
[b77931d]169 usb_log_debug("Bulk in endpoint: %d [%zuB].\n",
[3e23316]170 epm_in->pipe.endpoint_no, epm_in->pipe.max_packet_size);
[2715978]171 usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
[3e23316]172 epm_out->pipe.endpoint_no, epm_out->pipe.max_packet_size);
[2715978]173
[239e7e10]174 usb_log_debug("Get LUN count...\n");
[87037cc9]175 mdev->lun_count = usb_masstor_get_lun_count(mdev);
176 mdev->luns = calloc(mdev->lun_count, sizeof(ddf_fun_t*));
177 if (mdev->luns == NULL) {
178 rc = ENOMEM;
179 usb_log_error("Failed allocating luns table.\n");
180 goto error;
181 }
[239e7e10]182
[3cc55b47]183 mdev->bulk_in_pipe = &epm_in->pipe;
184 mdev->bulk_out_pipe = &epm_out->pipe;
[87037cc9]185 for (i = 0; i < mdev->lun_count; i++) {
[2aceec5]186 rc = usbmast_fun_create(mdev, i);
187 if (rc != EOK)
188 goto error;
[ff65e91]189 }
190
[2aceec5]191 return EOK;
192error:
[3002434]193 /* Destroy functions */
194 for (size_t i = 0; i < mdev->lun_count; ++i) {
195 if (mdev->luns[i] == NULL)
196 continue;
197 const int rc = ddf_fun_unbind(mdev->luns[i]);
198 if (rc != EOK) {
199 usb_log_warning("Failed to unbind LUN function %zu: "
200 "%s.\n", i, str_error(rc));
201 }
202 ddf_fun_destroy(mdev->luns[i]);
203 }
204 free(mdev->luns);
[2aceec5]205 return rc;
206}
207
208/** Create mass storage function.
209 *
210 * Called once for each LUN.
211 *
212 * @param mdev Mass storage device
213 * @param lun LUN
214 * @return EOK on success or negative error code.
215 */
216static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun)
217{
218 int rc;
219 char *fun_name = NULL;
220 ddf_fun_t *fun = NULL;
221 usbmast_fun_t *mfun = NULL;
222
223 if (asprintf(&fun_name, "l%u", lun) < 0) {
224 usb_log_error("Out of memory.\n");
225 rc = ENOMEM;
226 goto error;
227 }
228
[7c69861]229 fun = usb_device_ddf_fun_create(mdev->usb_dev, fun_exposed, fun_name);
[2aceec5]230 if (fun == NULL) {
231 usb_log_error("Failed to create DDF function %s.\n", fun_name);
232 rc = ENOMEM;
233 goto error;
234 }
235
[5f6e25e]236 /* Allocate soft state */
[d95f02ff]237 mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
[5f6e25e]238 if (mfun == NULL) {
239 usb_log_error("Failed allocating softstate.\n");
240 rc = ENOMEM;
241 goto error;
242 }
243
[87037cc9]244 mfun->ddf_fun = fun;
[5f6e25e]245 mfun->mdev = mdev;
246 mfun->lun = lun;
247
[135486d]248 bd_srvs_init(&mfun->bds);
249 mfun->bds.ops = &usbmast_bd_ops;
250 mfun->bds.sarg = mfun;
[4802dd7]251
[2aceec5]252 /* Set up a connection handler. */
[56fd7cf]253 ddf_fun_set_conn_handler(fun, usbmast_bd_connection);
[2aceec5]254
[0feaae4]255 usb_log_debug("Inquire...\n");
[6430ac6]256 usbmast_inquiry_data_t inquiry;
[2aceec5]257 rc = usbmast_inquiry(mfun, &inquiry);
[70c12d6]258 if (rc != EOK) {
[0feaae4]259 usb_log_warning("Failed to inquire device `%s': %s.\n",
[7c69861]260 usb_device_get_name(mdev->usb_dev), str_error(rc));
[ff65e91]261 rc = EIO;
262 goto error;
[70c12d6]263 }
264
[2aceec5]265 usb_log_info("Mass storage `%s' LUN %u: " \
266 "%s by %s rev. %s is %s (%s).\n",
[7c69861]267 usb_device_get_name(mdev->usb_dev),
[2aceec5]268 lun,
[7b2c17c]269 inquiry.product,
270 inquiry.vendor,
271 inquiry.revision,
[6430ac6]272 usbmast_scsi_dev_type_str(inquiry.device_type),
[2aceec5]273 inquiry.removable ? "removable" : "non-removable");
[7a5c8b8f]274
[71fa44c]275 uint32_t nblocks, block_size;
276
[2aceec5]277 rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
[71fa44c]278 if (rc != EOK) {
279 usb_log_warning("Failed to read capacity, device `%s': %s.\n",
[7c69861]280 usb_device_get_name(mdev->usb_dev), str_error(rc));
[ff65e91]281 rc = EIO;
282 goto error;
[71fa44c]283 }
284
285 usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
286 "block_size=%" PRIu32 "\n", nblocks, block_size);
287
[2aceec5]288 mfun->nblocks = nblocks;
289 mfun->block_size = block_size;
[f7a55f9]290
[ff65e91]291 rc = ddf_fun_bind(fun);
[f7a55f9]292 if (rc != EOK) {
[ff65e91]293 usb_log_error("Failed to bind DDF function %s: %s.\n",
294 fun_name, str_error(rc));
295 goto error;
[f7a55f9]296 }
297
[60c6fe0]298 free(fun_name);
[87037cc9]299 mdev->luns[lun] = fun;
[60c6fe0]300
[ff65e91]301 return EOK;
[f7a55f9]302
[ff65e91]303 /* Error cleanup */
304error:
305 if (fun != NULL)
306 ddf_fun_destroy(fun);
[2aceec5]307 if (fun_name != NULL)
308 free(fun_name);
[ff65e91]309 return rc;
310}
311
312/** Blockdev client connection handler. */
313static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
314 void *arg)
315{
[2aceec5]316 usbmast_fun_t *mfun;
[4802dd7]317
[56fd7cf]318 mfun = (usbmast_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
[135486d]319 bd_conn(iid, icall, &mfun->bds);
[4802dd7]320}
[ff65e91]321
[4802dd7]322/** Open device. */
[135486d]323static int usbmast_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
[4802dd7]324{
325 return EOK;
326}
[ff65e91]327
[4802dd7]328/** Close device. */
329static int usbmast_bd_close(bd_srv_t *bd)
330{
331 return EOK;
332}
[ff65e91]333
[4802dd7]334/** Read blocks from the device. */
335static int usbmast_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
336 size_t size)
337{
338 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
339
340 if (size < cnt * mfun->block_size)
341 return EINVAL;
342
343 return usbmast_read(mfun, ba, cnt, buf);
344}
345
346/** Write blocks to the device. */
347static int usbmast_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
348 const void *buf, size_t size)
349{
350 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
351
352 if (size < cnt * mfun->block_size)
353 return EINVAL;
354
355 return usbmast_write(mfun, ba, cnt, buf);
[2715978]356}
357
[4802dd7]358/** Get device block size. */
359static int usbmast_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
360{
361 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
362 *rsize = mfun->block_size;
363 return EOK;
364}
365
366/** Get number of blocks on device. */
367static int usbmast_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
368{
369 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
370 *rnb = mfun->nblocks;
371 return EOK;
372}
373
374
[2715978]375/** USB mass storage driver ops. */
[9871bca]376static const usb_driver_ops_t usbmast_driver_ops = {
[1a4ea01d]377 .device_add = usbmast_device_add,
[9871bca]378 .device_rem = usbmast_device_remove,
[3002434]379 .device_gone = usbmast_device_gone,
[2715978]380};
381
382/** USB mass storage driver. */
[9871bca]383static const usb_driver_t usbmast_driver = {
[2715978]384 .name = NAME,
385 .ops = &usbmast_driver_ops,
386 .endpoints = mast_endpoints
387};
388
389int main(int argc, char *argv[])
390{
[920d0fc]391 log_init(NAME);
[2715978]392
393 return usb_driver_main(&usbmast_driver);
394}
395
396/**
397 * @}
398 */
Note: See TracBrowser for help on using the repository browser.