| 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 <ipc/bd.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 |
|
|---|
| 57 | static 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 | };
|
|---|
| 65 | static 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 |
|
|---|
| 74 | usb_endpoint_description_t *mast_endpoints[] = {
|
|---|
| 75 | &bulk_in_ep,
|
|---|
| 76 | &bulk_out_ep,
|
|---|
| 77 | NULL
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun);
|
|---|
| 81 | static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 82 | void *arg);
|
|---|
| 83 |
|
|---|
| 84 | /** Callback when new device is attached and recognized as a mass storage.
|
|---|
| 85 | *
|
|---|
| 86 | * @param dev Representation of a the USB device.
|
|---|
| 87 | * @return Error code.
|
|---|
| 88 | */
|
|---|
| 89 | static int usbmast_device_add(usb_device_t *dev)
|
|---|
| 90 | {
|
|---|
| 91 | int rc;
|
|---|
| 92 | usbmast_dev_t *mdev = NULL;
|
|---|
| 93 | unsigned i;
|
|---|
| 94 |
|
|---|
| 95 | /* Allocate softstate */
|
|---|
| 96 | dev->driver_data = mdev = malloc(sizeof(usbmast_dev_t));
|
|---|
| 97 | if (mdev == NULL) {
|
|---|
| 98 | usb_log_error("Failed allocating softstate.\n");
|
|---|
| 99 | return ENOMEM;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | mdev->ddf_dev = dev->ddf_dev;
|
|---|
| 103 | mdev->usb_dev = dev;
|
|---|
| 104 |
|
|---|
| 105 | usb_log_info("Initializing mass storage `%s'.\n", dev->ddf_dev->name);
|
|---|
| 106 | usb_log_debug(" Bulk in endpoint: %d [%zuB].\n",
|
|---|
| 107 | dev->pipes[BULK_IN_EP].pipe->endpoint_no,
|
|---|
| 108 | (size_t) dev->pipes[BULK_IN_EP].descriptor->max_packet_size);
|
|---|
| 109 | usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
|
|---|
| 110 | dev->pipes[BULK_OUT_EP].pipe->endpoint_no,
|
|---|
| 111 | (size_t) dev->pipes[BULK_OUT_EP].descriptor->max_packet_size);
|
|---|
| 112 |
|
|---|
| 113 | usb_log_debug("Get LUN count...\n");
|
|---|
| 114 | mdev->luns = usb_masstor_get_lun_count(mdev);
|
|---|
| 115 |
|
|---|
| 116 | for (i = 0; i < mdev->luns; i++) {
|
|---|
| 117 | rc = usbmast_fun_create(mdev, i);
|
|---|
| 118 | if (rc != EOK)
|
|---|
| 119 | goto error;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | return EOK;
|
|---|
| 123 | error:
|
|---|
| 124 | /* XXX Destroy functions */
|
|---|
| 125 | return rc;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /** Create mass storage function.
|
|---|
| 129 | *
|
|---|
| 130 | * Called once for each LUN.
|
|---|
| 131 | *
|
|---|
| 132 | * @param mdev Mass storage device
|
|---|
| 133 | * @param lun LUN
|
|---|
| 134 | * @return EOK on success or negative error code.
|
|---|
| 135 | */
|
|---|
| 136 | static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun)
|
|---|
| 137 | {
|
|---|
| 138 | int rc;
|
|---|
| 139 | char *fun_name = NULL;
|
|---|
| 140 | ddf_fun_t *fun = NULL;
|
|---|
| 141 | usbmast_fun_t *mfun = NULL;
|
|---|
| 142 |
|
|---|
| 143 | if (asprintf(&fun_name, "l%u", lun) < 0) {
|
|---|
| 144 | usb_log_error("Out of memory.\n");
|
|---|
| 145 | rc = ENOMEM;
|
|---|
| 146 | goto error;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | fun = ddf_fun_create(mdev->ddf_dev, fun_exposed, fun_name);
|
|---|
| 150 | if (fun == NULL) {
|
|---|
| 151 | usb_log_error("Failed to create DDF function %s.\n", fun_name);
|
|---|
| 152 | rc = ENOMEM;
|
|---|
| 153 | goto error;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /* Allocate soft state */
|
|---|
| 157 | mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
|
|---|
| 158 | if (mfun == NULL) {
|
|---|
| 159 | usb_log_error("Failed allocating softstate.\n");
|
|---|
| 160 | rc = ENOMEM;
|
|---|
| 161 | goto error;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | mfun->mdev = mdev;
|
|---|
| 165 | mfun->lun = lun;
|
|---|
| 166 |
|
|---|
| 167 | /* Set up a connection handler. */
|
|---|
| 168 | fun->conn_handler = usbmast_bd_connection;
|
|---|
| 169 |
|
|---|
| 170 | usb_log_debug("Inquire...\n");
|
|---|
| 171 | usbmast_inquiry_data_t inquiry;
|
|---|
| 172 | rc = usbmast_inquiry(mfun, &inquiry);
|
|---|
| 173 | if (rc != EOK) {
|
|---|
| 174 | usb_log_warning("Failed to inquire device `%s': %s.\n",
|
|---|
| 175 | mdev->ddf_dev->name, str_error(rc));
|
|---|
| 176 | rc = EIO;
|
|---|
| 177 | goto error;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | usb_log_info("Mass storage `%s' LUN %u: " \
|
|---|
| 181 | "%s by %s rev. %s is %s (%s).\n",
|
|---|
| 182 | mdev->ddf_dev->name,
|
|---|
| 183 | lun,
|
|---|
| 184 | inquiry.product,
|
|---|
| 185 | inquiry.vendor,
|
|---|
| 186 | inquiry.revision,
|
|---|
| 187 | usbmast_scsi_dev_type_str(inquiry.device_type),
|
|---|
| 188 | inquiry.removable ? "removable" : "non-removable");
|
|---|
| 189 |
|
|---|
| 190 | uint32_t nblocks, block_size;
|
|---|
| 191 |
|
|---|
| 192 | rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
|
|---|
| 193 | if (rc != EOK) {
|
|---|
| 194 | usb_log_warning("Failed to read capacity, device `%s': %s.\n",
|
|---|
| 195 | mdev->ddf_dev->name, str_error(rc));
|
|---|
| 196 | rc = EIO;
|
|---|
| 197 | goto error;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
|
|---|
| 201 | "block_size=%" PRIu32 "\n", nblocks, block_size);
|
|---|
| 202 |
|
|---|
| 203 | mfun->nblocks = nblocks;
|
|---|
| 204 | mfun->block_size = block_size;
|
|---|
| 205 |
|
|---|
| 206 | rc = ddf_fun_bind(fun);
|
|---|
| 207 | if (rc != EOK) {
|
|---|
| 208 | usb_log_error("Failed to bind DDF function %s: %s.\n",
|
|---|
| 209 | fun_name, str_error(rc));
|
|---|
| 210 | goto error;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | free(fun_name);
|
|---|
| 214 |
|
|---|
| 215 | return EOK;
|
|---|
| 216 |
|
|---|
| 217 | /* Error cleanup */
|
|---|
| 218 | error:
|
|---|
| 219 | if (fun != NULL)
|
|---|
| 220 | ddf_fun_destroy(fun);
|
|---|
| 221 | if (fun_name != NULL)
|
|---|
| 222 | free(fun_name);
|
|---|
| 223 | return rc;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /** Blockdev client connection handler. */
|
|---|
| 227 | static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 228 | void *arg)
|
|---|
| 229 | {
|
|---|
| 230 | usbmast_fun_t *mfun;
|
|---|
| 231 | void *comm_buf = NULL;
|
|---|
| 232 | size_t comm_size;
|
|---|
| 233 | ipc_callid_t callid;
|
|---|
| 234 | ipc_call_t call;
|
|---|
| 235 | unsigned int flags;
|
|---|
| 236 | sysarg_t method;
|
|---|
| 237 | uint64_t ba;
|
|---|
| 238 | size_t cnt;
|
|---|
| 239 | int retval;
|
|---|
| 240 |
|
|---|
| 241 | async_answer_0(iid, EOK);
|
|---|
| 242 |
|
|---|
| 243 | if (!async_share_out_receive(&callid, &comm_size, &flags)) {
|
|---|
| 244 | async_answer_0(callid, EHANGUP);
|
|---|
| 245 | return;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | comm_buf = as_get_mappable_page(comm_size);
|
|---|
| 249 | if (comm_buf == NULL) {
|
|---|
| 250 | async_answer_0(callid, EHANGUP);
|
|---|
| 251 | return;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | (void) async_share_out_finalize(callid, comm_buf);
|
|---|
| 255 |
|
|---|
| 256 | mfun = (usbmast_fun_t *) ((ddf_fun_t *)arg)->driver_data;
|
|---|
| 257 |
|
|---|
| 258 | while (true) {
|
|---|
| 259 | callid = async_get_call(&call);
|
|---|
| 260 | method = IPC_GET_IMETHOD(call);
|
|---|
| 261 |
|
|---|
| 262 | if (!method) {
|
|---|
| 263 | /* The other side hung up. */
|
|---|
| 264 | async_answer_0(callid, EOK);
|
|---|
| 265 | return;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | switch (method) {
|
|---|
| 269 | case BD_GET_BLOCK_SIZE:
|
|---|
| 270 | async_answer_1(callid, EOK, mfun->block_size);
|
|---|
| 271 | break;
|
|---|
| 272 | case BD_GET_NUM_BLOCKS:
|
|---|
| 273 | async_answer_2(callid, EOK, LOWER32(mfun->nblocks),
|
|---|
| 274 | UPPER32(mfun->nblocks));
|
|---|
| 275 | break;
|
|---|
| 276 | case BD_READ_BLOCKS:
|
|---|
| 277 | ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
|
|---|
| 278 | cnt = IPC_GET_ARG3(call);
|
|---|
| 279 | retval = usbmast_read(mfun, ba, cnt, comm_buf);
|
|---|
| 280 | async_answer_0(callid, retval);
|
|---|
| 281 | break;
|
|---|
| 282 | case BD_WRITE_BLOCKS:
|
|---|
| 283 | ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
|
|---|
| 284 | cnt = IPC_GET_ARG3(call);
|
|---|
| 285 | retval = usbmast_write(mfun, ba, cnt, comm_buf);
|
|---|
| 286 | async_answer_0(callid, retval);
|
|---|
| 287 | break;
|
|---|
| 288 | default:
|
|---|
| 289 | async_answer_0(callid, EINVAL);
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | /** USB mass storage driver ops. */
|
|---|
| 295 | static usb_driver_ops_t usbmast_driver_ops = {
|
|---|
| 296 | .device_add = usbmast_device_add,
|
|---|
| 297 | };
|
|---|
| 298 |
|
|---|
| 299 | /** USB mass storage driver. */
|
|---|
| 300 | static usb_driver_t usbmast_driver = {
|
|---|
| 301 | .name = NAME,
|
|---|
| 302 | .ops = &usbmast_driver_ops,
|
|---|
| 303 | .endpoints = mast_endpoints
|
|---|
| 304 | };
|
|---|
| 305 |
|
|---|
| 306 | int main(int argc, char *argv[])
|
|---|
| 307 | {
|
|---|
| 308 | usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
|
|---|
| 309 |
|
|---|
| 310 | return usb_driver_main(&usbmast_driver);
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | /**
|
|---|
| 314 | * @}
|
|---|
| 315 | */
|
|---|