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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bfc5c9dd was fbcdeb8, checked in by Martin Decky <martin@…>, 14 years ago

Remove the two-phase way of creating virtual memory areas (first asking for a mappable address and then mapping it) which was prone to race conditions when two or more calls to as_get_mappable_page() and as_area_create() were interleaved. This for example caused the e1k driver to randomly fail.

The memory area related syscalls and IPC calls have all been altered to accept a special value (void *) -1, representing a demand to atomically search for a mappable address space "hole" and map to it.

Individual changes:

  • IPC_M_SHARE_OUT: the destination address space area is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the callee via a pointer in an IPC reply argument)

  • IPC_M_SHARE_IN: the destination address space ares is supplied by the kernel, the callee only specifies the lower bound

(the address is returned to the caller as usual via an IPC argument)

  • SYS_AS_GET_UNMAPPED_AREA was removed
  • dummy implementations of SYS_PHYSMEM_UNMAP and SYS_IOSPACE_DISABLE were added for the sake of symmetry (they do nothing yet)
  • SYS_PHYSMEM_MAP and SYS_DMAMEM_MAP were altered to accept (void *) -1 as address space area base and a lower bound
  • kernel as_area_create() and as_area_share() were altered to accept (void *) -1 as address space area base and a lower bound
  • uspace libraries and programs were altered to reflect the new API
  • Property mode set to 100644
File size: 9.4 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>
39#include <ipc/bd.h>
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
[7a5c8b8f]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
[9871bca]57static const usb_endpoint_description_t bulk_in_ep = {
[2715978]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};
[9871bca]65static const usb_endpoint_description_t bulk_out_ep = {
[2715978]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
[b803845]74static const usb_endpoint_description_t *mast_endpoints[] = {
[2715978]75 &bulk_in_ep,
76 &bulk_out_ep,
77 NULL
78};
79
[2aceec5]80static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun);
[ff65e91]81static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
82 void *arg);
83
[3002434]84/** Callback when a device is removed from the system.
85 *
86 * @param dev Representation of USB device.
87 * @return Error code.
88 */
89static int usbmast_device_gone(usb_device_t *dev)
90{
91 usbmast_dev_t *mdev = dev->driver_data;
92 assert(mdev);
93
94 for (size_t i = 0; i < mdev->lun_count; ++i) {
95 const int rc = ddf_fun_unbind(mdev->luns[i]);
96 if (rc != EOK) {
97 usb_log_error("Failed to unbind LUN function %zu: "
98 "%s\n", i, str_error(rc));
99 return rc;
100 }
101 ddf_fun_destroy(mdev->luns[i]);
102 mdev->luns[i] = NULL;
103 }
104 free(mdev->luns);
105 return EOK;
106}
107
[9871bca]108/** Callback when a device is about to be removed.
109 *
110 * @param dev Representation of USB device.
111 * @return Error code.
112 */
113static int usbmast_device_remove(usb_device_t *dev)
114{
115 //TODO: flush buffers, or whatever.
[dcc10b8d]116 //TODO: remove device
[9871bca]117 return ENOTSUP;
118}
119
[2715978]120/** Callback when new device is attached and recognized as a mass storage.
121 *
[9871bca]122 * @param dev Representation of USB device.
[2715978]123 * @return Error code.
124 */
[1a4ea01d]125static int usbmast_device_add(usb_device_t *dev)
[2715978]126{
127 int rc;
[2aceec5]128 usbmast_dev_t *mdev = NULL;
129 unsigned i;
[2715978]130
[ff65e91]131 /* Allocate softstate */
[065064e6]132 mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
[2aceec5]133 if (mdev == NULL) {
[ff65e91]134 usb_log_error("Failed allocating softstate.\n");
[d6c953e9]135 return ENOMEM;
[2715978]136 }
[ff65e91]137
[2aceec5]138 mdev->ddf_dev = dev->ddf_dev;
139 mdev->usb_dev = dev;
[ff65e91]140
[96fde65]141 usb_log_info("Initializing mass storage `%s'.\n", dev->ddf_dev->name);
[b77931d]142 usb_log_debug("Bulk in endpoint: %d [%zuB].\n",
143 dev->pipes[BULK_IN_EP].pipe.endpoint_no,
[d176b1d]144 dev->pipes[BULK_IN_EP].pipe.max_packet_size);
[2715978]145 usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
[b77931d]146 dev->pipes[BULK_OUT_EP].pipe.endpoint_no,
[d176b1d]147 dev->pipes[BULK_OUT_EP].pipe.max_packet_size);
[2715978]148
[239e7e10]149 usb_log_debug("Get LUN count...\n");
[87037cc9]150 mdev->lun_count = usb_masstor_get_lun_count(mdev);
151 mdev->luns = calloc(mdev->lun_count, sizeof(ddf_fun_t*));
152 if (mdev->luns == NULL) {
153 rc = ENOMEM;
154 usb_log_error("Failed allocating luns table.\n");
155 goto error;
156 }
[239e7e10]157
[87037cc9]158 for (i = 0; i < mdev->lun_count; i++) {
[2aceec5]159 rc = usbmast_fun_create(mdev, i);
160 if (rc != EOK)
161 goto error;
[ff65e91]162 }
163
[2aceec5]164 return EOK;
165error:
[3002434]166 /* Destroy functions */
167 for (size_t i = 0; i < mdev->lun_count; ++i) {
168 if (mdev->luns[i] == NULL)
169 continue;
170 const int rc = ddf_fun_unbind(mdev->luns[i]);
171 if (rc != EOK) {
172 usb_log_warning("Failed to unbind LUN function %zu: "
173 "%s.\n", i, str_error(rc));
174 }
175 ddf_fun_destroy(mdev->luns[i]);
176 }
177 free(mdev->luns);
[2aceec5]178 return rc;
179}
180
181/** Create mass storage function.
182 *
183 * Called once for each LUN.
184 *
185 * @param mdev Mass storage device
186 * @param lun LUN
187 * @return EOK on success or negative error code.
188 */
189static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun)
190{
191 int rc;
192 char *fun_name = NULL;
193 ddf_fun_t *fun = NULL;
194 usbmast_fun_t *mfun = NULL;
195
196 if (asprintf(&fun_name, "l%u", lun) < 0) {
197 usb_log_error("Out of memory.\n");
198 rc = ENOMEM;
199 goto error;
200 }
201
202 fun = ddf_fun_create(mdev->ddf_dev, fun_exposed, fun_name);
203 if (fun == NULL) {
204 usb_log_error("Failed to create DDF function %s.\n", fun_name);
205 rc = ENOMEM;
206 goto error;
207 }
208
[5f6e25e]209 /* Allocate soft state */
[d95f02ff]210 mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
[5f6e25e]211 if (mfun == NULL) {
212 usb_log_error("Failed allocating softstate.\n");
213 rc = ENOMEM;
214 goto error;
215 }
216
[87037cc9]217 mfun->ddf_fun = fun;
[5f6e25e]218 mfun->mdev = mdev;
219 mfun->lun = lun;
220
[2aceec5]221 /* Set up a connection handler. */
222 fun->conn_handler = usbmast_bd_connection;
223
[0feaae4]224 usb_log_debug("Inquire...\n");
[6430ac6]225 usbmast_inquiry_data_t inquiry;
[2aceec5]226 rc = usbmast_inquiry(mfun, &inquiry);
[70c12d6]227 if (rc != EOK) {
[0feaae4]228 usb_log_warning("Failed to inquire device `%s': %s.\n",
[2aceec5]229 mdev->ddf_dev->name, str_error(rc));
[ff65e91]230 rc = EIO;
231 goto error;
[70c12d6]232 }
233
[2aceec5]234 usb_log_info("Mass storage `%s' LUN %u: " \
235 "%s by %s rev. %s is %s (%s).\n",
236 mdev->ddf_dev->name,
237 lun,
[7b2c17c]238 inquiry.product,
239 inquiry.vendor,
240 inquiry.revision,
[6430ac6]241 usbmast_scsi_dev_type_str(inquiry.device_type),
[2aceec5]242 inquiry.removable ? "removable" : "non-removable");
[7a5c8b8f]243
[71fa44c]244 uint32_t nblocks, block_size;
245
[2aceec5]246 rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
[71fa44c]247 if (rc != EOK) {
248 usb_log_warning("Failed to read capacity, device `%s': %s.\n",
[2aceec5]249 mdev->ddf_dev->name, str_error(rc));
[ff65e91]250 rc = EIO;
251 goto error;
[71fa44c]252 }
253
254 usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
255 "block_size=%" PRIu32 "\n", nblocks, block_size);
256
[2aceec5]257 mfun->nblocks = nblocks;
258 mfun->block_size = block_size;
[f7a55f9]259
[ff65e91]260 rc = ddf_fun_bind(fun);
[f7a55f9]261 if (rc != EOK) {
[ff65e91]262 usb_log_error("Failed to bind DDF function %s: %s.\n",
263 fun_name, str_error(rc));
264 goto error;
[f7a55f9]265 }
266
[60c6fe0]267 free(fun_name);
[87037cc9]268 mdev->luns[lun] = fun;
[60c6fe0]269
[ff65e91]270 return EOK;
[f7a55f9]271
[ff65e91]272 /* Error cleanup */
273error:
274 if (fun != NULL)
275 ddf_fun_destroy(fun);
[2aceec5]276 if (fun_name != NULL)
277 free(fun_name);
[ff65e91]278 return rc;
279}
280
281/** Blockdev client connection handler. */
282static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
283 void *arg)
284{
[2aceec5]285 usbmast_fun_t *mfun;
[ff65e91]286 void *comm_buf = NULL;
287 size_t comm_size;
288 ipc_callid_t callid;
289 ipc_call_t call;
290 unsigned int flags;
291 sysarg_t method;
292 uint64_t ba;
293 size_t cnt;
294 int retval;
295
296 async_answer_0(iid, EOK);
297
298 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
299 async_answer_0(callid, EHANGUP);
300 return;
[f7a55f9]301 }
[fbcdeb8]302
303 (void) async_share_out_finalize(callid, &comm_buf);
304 if (comm_buf == (void *) -1) {
[ff65e91]305 async_answer_0(callid, EHANGUP);
306 return;
307 }
[fbcdeb8]308
[2aceec5]309 mfun = (usbmast_fun_t *) ((ddf_fun_t *)arg)->driver_data;
[ff65e91]310
311 while (true) {
312 callid = async_get_call(&call);
313 method = IPC_GET_IMETHOD(call);
314
315 if (!method) {
316 /* The other side hung up. */
317 async_answer_0(callid, EOK);
318 return;
319 }
320
321 switch (method) {
322 case BD_GET_BLOCK_SIZE:
[2aceec5]323 async_answer_1(callid, EOK, mfun->block_size);
[ff65e91]324 break;
325 case BD_GET_NUM_BLOCKS:
[2aceec5]326 async_answer_2(callid, EOK, LOWER32(mfun->nblocks),
327 UPPER32(mfun->nblocks));
[ff65e91]328 break;
329 case BD_READ_BLOCKS:
330 ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
331 cnt = IPC_GET_ARG3(call);
[2aceec5]332 retval = usbmast_read(mfun, ba, cnt, comm_buf);
[ff65e91]333 async_answer_0(callid, retval);
334 break;
335 case BD_WRITE_BLOCKS:
[38c9505]336 ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
[ff65e91]337 cnt = IPC_GET_ARG3(call);
[2aceec5]338 retval = usbmast_write(mfun, ba, cnt, comm_buf);
[ff65e91]339 async_answer_0(callid, retval);
[38c9505]340 break;
[ff65e91]341 default:
342 async_answer_0(callid, EINVAL);
343 }
344 }
[2715978]345}
346
347/** USB mass storage driver ops. */
[9871bca]348static const usb_driver_ops_t usbmast_driver_ops = {
[1a4ea01d]349 .device_add = usbmast_device_add,
[9871bca]350 .device_rem = usbmast_device_remove,
[3002434]351 .device_gone = usbmast_device_gone,
[2715978]352};
353
354/** USB mass storage driver. */
[9871bca]355static const usb_driver_t usbmast_driver = {
[2715978]356 .name = NAME,
357 .ops = &usbmast_driver_ops,
358 .endpoints = mast_endpoints
359};
360
361int main(int argc, char *argv[])
362{
363 usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
364
365 return usb_driver_main(&usbmast_driver);
366}
367
368/**
369 * @}
370 */
Note: See TracBrowser for help on using the repository browser.