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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cb8ede1 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
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
54static const usb_endpoint_description_t bulk_in_ep = {
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};
62static const usb_endpoint_description_t bulk_out_ep = {
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
71static const usb_endpoint_description_t *mast_endpoints[] = {
72 &bulk_in_ep,
73 &bulk_out_ep,
74 NULL
75};
76
77static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun);
78static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
79 void *arg);
80
81static int usbmast_bd_open(bd_srvs_t *, bd_srv_t *);
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{
99 return (usbmast_fun_t *) bd->srvs->sarg;
100}
101
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{
109 usbmast_dev_t *mdev = usb_device_data_get(dev);
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
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.
134 //TODO: remove device
135 return ENOTSUP;
136}
137
138/** Callback when new device is attached and recognized as a mass storage.
139 *
140 * @param dev Representation of USB device.
141 * @return Error code.
142 */
143static int usbmast_device_add(usb_device_t *dev)
144{
145 int rc;
146 usbmast_dev_t *mdev = NULL;
147 unsigned i;
148
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
158 /* Allocate softstate */
159 mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
160 if (mdev == NULL) {
161 usb_log_error("Failed allocating softstate.\n");
162 return ENOMEM;
163 }
164
165 mdev->usb_dev = dev;
166
167 usb_log_info("Initializing mass storage `%s'.\n",
168 usb_device_get_name(dev));
169 usb_log_debug("Bulk in endpoint: %d [%zuB].\n",
170 epm_in->pipe.endpoint_no, epm_in->pipe.max_packet_size);
171 usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
172 epm_out->pipe.endpoint_no, epm_out->pipe.max_packet_size);
173
174 usb_log_debug("Get LUN count...\n");
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 }
182
183 mdev->bulk_in_pipe = &epm_in->pipe;
184 mdev->bulk_out_pipe = &epm_out->pipe;
185 for (i = 0; i < mdev->lun_count; i++) {
186 rc = usbmast_fun_create(mdev, i);
187 if (rc != EOK)
188 goto error;
189 }
190
191 return EOK;
192error:
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);
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
229 fun = usb_device_ddf_fun_create(mdev->usb_dev, fun_exposed, fun_name);
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
236 /* Allocate soft state */
237 mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
238 if (mfun == NULL) {
239 usb_log_error("Failed allocating softstate.\n");
240 rc = ENOMEM;
241 goto error;
242 }
243
244 mfun->ddf_fun = fun;
245 mfun->mdev = mdev;
246 mfun->lun = lun;
247
248 bd_srvs_init(&mfun->bds);
249 mfun->bds.ops = &usbmast_bd_ops;
250 mfun->bds.sarg = mfun;
251
252 /* Set up a connection handler. */
253 ddf_fun_set_conn_handler(fun, usbmast_bd_connection);
254
255 usb_log_debug("Inquire...\n");
256 usbmast_inquiry_data_t inquiry;
257 rc = usbmast_inquiry(mfun, &inquiry);
258 if (rc != EOK) {
259 usb_log_warning("Failed to inquire device `%s': %s.\n",
260 usb_device_get_name(mdev->usb_dev), str_error(rc));
261 rc = EIO;
262 goto error;
263 }
264
265 usb_log_info("Mass storage `%s' LUN %u: " \
266 "%s by %s rev. %s is %s (%s).\n",
267 usb_device_get_name(mdev->usb_dev),
268 lun,
269 inquiry.product,
270 inquiry.vendor,
271 inquiry.revision,
272 usbmast_scsi_dev_type_str(inquiry.device_type),
273 inquiry.removable ? "removable" : "non-removable");
274
275 uint32_t nblocks, block_size;
276
277 rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
278 if (rc != EOK) {
279 usb_log_warning("Failed to read capacity, device `%s': %s.\n",
280 usb_device_get_name(mdev->usb_dev), str_error(rc));
281 rc = EIO;
282 goto error;
283 }
284
285 usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
286 "block_size=%" PRIu32 "\n", nblocks, block_size);
287
288 mfun->nblocks = nblocks;
289 mfun->block_size = block_size;
290
291 rc = ddf_fun_bind(fun);
292 if (rc != EOK) {
293 usb_log_error("Failed to bind DDF function %s: %s.\n",
294 fun_name, str_error(rc));
295 goto error;
296 }
297
298 free(fun_name);
299 mdev->luns[lun] = fun;
300
301 return EOK;
302
303 /* Error cleanup */
304error:
305 if (fun != NULL)
306 ddf_fun_destroy(fun);
307 if (fun_name != NULL)
308 free(fun_name);
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{
316 usbmast_fun_t *mfun;
317
318 mfun = (usbmast_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
319 bd_conn(iid, icall, &mfun->bds);
320}
321
322/** Open device. */
323static int usbmast_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
324{
325 return EOK;
326}
327
328/** Close device. */
329static int usbmast_bd_close(bd_srv_t *bd)
330{
331 return EOK;
332}
333
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);
356}
357
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
375/** USB mass storage driver ops. */
376static const usb_driver_ops_t usbmast_driver_ops = {
377 .device_add = usbmast_device_add,
378 .device_rem = usbmast_device_remove,
379 .device_gone = usbmast_device_gone,
380};
381
382/** USB mass storage driver. */
383static const usb_driver_t usbmast_driver = {
384 .name = NAME,
385 .ops = &usbmast_driver_ops,
386 .endpoints = mast_endpoints
387};
388
389int main(int argc, char *argv[])
390{
391 log_init(NAME);
392
393 return usb_driver_main(&usbmast_driver);
394}
395
396/**
397 * @}
398 */
Note: See TracBrowser for help on using the repository browser.