| 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 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 | };
|
|---|
| 65 | static 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 |
|
|---|
| 74 | static const 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 a device is removed from the system.
|
|---|
| 85 | *
|
|---|
| 86 | * @param dev Representation of USB device.
|
|---|
| 87 | * @return Error code.
|
|---|
| 88 | */
|
|---|
| 89 | static 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 |
|
|---|
| 108 | /** Callback when a device is about to be removed.
|
|---|
| 109 | *
|
|---|
| 110 | * @param dev Representation of USB device.
|
|---|
| 111 | * @return Error code.
|
|---|
| 112 | */
|
|---|
| 113 | static int usbmast_device_remove(usb_device_t *dev)
|
|---|
| 114 | {
|
|---|
| 115 | //TODO: flush buffers, or whatever.
|
|---|
| 116 | //TODO: remove device
|
|---|
| 117 | return ENOTSUP;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /** Callback when new device is attached and recognized as a mass storage.
|
|---|
| 121 | *
|
|---|
| 122 | * @param dev Representation of USB device.
|
|---|
| 123 | * @return Error code.
|
|---|
| 124 | */
|
|---|
| 125 | static int usbmast_device_add(usb_device_t *dev)
|
|---|
| 126 | {
|
|---|
| 127 | int rc;
|
|---|
| 128 | usbmast_dev_t *mdev = NULL;
|
|---|
| 129 | unsigned i;
|
|---|
| 130 |
|
|---|
| 131 | /* Allocate softstate */
|
|---|
| 132 | mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
|
|---|
| 133 | if (mdev == NULL) {
|
|---|
| 134 | usb_log_error("Failed allocating softstate.\n");
|
|---|
| 135 | return ENOMEM;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | mdev->ddf_dev = dev->ddf_dev;
|
|---|
| 139 | mdev->usb_dev = dev;
|
|---|
| 140 |
|
|---|
| 141 | usb_log_info("Initializing mass storage `%s'.\n", dev->ddf_dev->name);
|
|---|
| 142 | usb_log_debug("Bulk in endpoint: %d [%zuB].\n",
|
|---|
| 143 | dev->pipes[BULK_IN_EP].pipe.endpoint_no,
|
|---|
| 144 | dev->pipes[BULK_IN_EP].pipe.max_packet_size);
|
|---|
| 145 | usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
|
|---|
| 146 | dev->pipes[BULK_OUT_EP].pipe.endpoint_no,
|
|---|
| 147 | dev->pipes[BULK_OUT_EP].pipe.max_packet_size);
|
|---|
| 148 |
|
|---|
| 149 | usb_log_debug("Get LUN count...\n");
|
|---|
| 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 | }
|
|---|
| 157 |
|
|---|
| 158 | for (i = 0; i < mdev->lun_count; i++) {
|
|---|
| 159 | rc = usbmast_fun_create(mdev, i);
|
|---|
| 160 | if (rc != EOK)
|
|---|
| 161 | goto error;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | return EOK;
|
|---|
| 165 | error:
|
|---|
| 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);
|
|---|
| 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 | */
|
|---|
| 189 | static 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 |
|
|---|
| 209 | /* Allocate soft state */
|
|---|
| 210 | mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
|
|---|
| 211 | if (mfun == NULL) {
|
|---|
| 212 | usb_log_error("Failed allocating softstate.\n");
|
|---|
| 213 | rc = ENOMEM;
|
|---|
| 214 | goto error;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | mfun->ddf_fun = fun;
|
|---|
| 218 | mfun->mdev = mdev;
|
|---|
| 219 | mfun->lun = lun;
|
|---|
| 220 |
|
|---|
| 221 | /* Set up a connection handler. */
|
|---|
| 222 | fun->conn_handler = usbmast_bd_connection;
|
|---|
| 223 |
|
|---|
| 224 | usb_log_debug("Inquire...\n");
|
|---|
| 225 | usbmast_inquiry_data_t inquiry;
|
|---|
| 226 | rc = usbmast_inquiry(mfun, &inquiry);
|
|---|
| 227 | if (rc != EOK) {
|
|---|
| 228 | usb_log_warning("Failed to inquire device `%s': %s.\n",
|
|---|
| 229 | mdev->ddf_dev->name, str_error(rc));
|
|---|
| 230 | rc = EIO;
|
|---|
| 231 | goto error;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 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,
|
|---|
| 238 | inquiry.product,
|
|---|
| 239 | inquiry.vendor,
|
|---|
| 240 | inquiry.revision,
|
|---|
| 241 | usbmast_scsi_dev_type_str(inquiry.device_type),
|
|---|
| 242 | inquiry.removable ? "removable" : "non-removable");
|
|---|
| 243 |
|
|---|
| 244 | uint32_t nblocks, block_size;
|
|---|
| 245 |
|
|---|
| 246 | rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
|
|---|
| 247 | if (rc != EOK) {
|
|---|
| 248 | usb_log_warning("Failed to read capacity, device `%s': %s.\n",
|
|---|
| 249 | mdev->ddf_dev->name, str_error(rc));
|
|---|
| 250 | rc = EIO;
|
|---|
| 251 | goto error;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
|
|---|
| 255 | "block_size=%" PRIu32 "\n", nblocks, block_size);
|
|---|
| 256 |
|
|---|
| 257 | mfun->nblocks = nblocks;
|
|---|
| 258 | mfun->block_size = block_size;
|
|---|
| 259 |
|
|---|
| 260 | rc = ddf_fun_bind(fun);
|
|---|
| 261 | if (rc != EOK) {
|
|---|
| 262 | usb_log_error("Failed to bind DDF function %s: %s.\n",
|
|---|
| 263 | fun_name, str_error(rc));
|
|---|
| 264 | goto error;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | free(fun_name);
|
|---|
| 268 | mdev->luns[lun] = fun;
|
|---|
| 269 |
|
|---|
| 270 | return EOK;
|
|---|
| 271 |
|
|---|
| 272 | /* Error cleanup */
|
|---|
| 273 | error:
|
|---|
| 274 | if (fun != NULL)
|
|---|
| 275 | ddf_fun_destroy(fun);
|
|---|
| 276 | if (fun_name != NULL)
|
|---|
| 277 | free(fun_name);
|
|---|
| 278 | return rc;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /** Blockdev client connection handler. */
|
|---|
| 282 | static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
|
|---|
| 283 | void *arg)
|
|---|
| 284 | {
|
|---|
| 285 | usbmast_fun_t *mfun;
|
|---|
| 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;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | (void) async_share_out_finalize(callid, &comm_buf);
|
|---|
| 304 | if (comm_buf == (void *) -1) {
|
|---|
| 305 | async_answer_0(callid, EHANGUP);
|
|---|
| 306 | return;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | mfun = (usbmast_fun_t *) ((ddf_fun_t *)arg)->driver_data;
|
|---|
| 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:
|
|---|
| 323 | async_answer_1(callid, EOK, mfun->block_size);
|
|---|
| 324 | break;
|
|---|
| 325 | case BD_GET_NUM_BLOCKS:
|
|---|
| 326 | async_answer_2(callid, EOK, LOWER32(mfun->nblocks),
|
|---|
| 327 | UPPER32(mfun->nblocks));
|
|---|
| 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);
|
|---|
| 332 | retval = usbmast_read(mfun, ba, cnt, comm_buf);
|
|---|
| 333 | async_answer_0(callid, retval);
|
|---|
| 334 | break;
|
|---|
| 335 | case BD_WRITE_BLOCKS:
|
|---|
| 336 | ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
|
|---|
| 337 | cnt = IPC_GET_ARG3(call);
|
|---|
| 338 | retval = usbmast_write(mfun, ba, cnt, comm_buf);
|
|---|
| 339 | async_answer_0(callid, retval);
|
|---|
| 340 | break;
|
|---|
| 341 | default:
|
|---|
| 342 | async_answer_0(callid, EINVAL);
|
|---|
| 343 | }
|
|---|
| 344 | }
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /** USB mass storage driver ops. */
|
|---|
| 348 | static const usb_driver_ops_t usbmast_driver_ops = {
|
|---|
| 349 | .device_add = usbmast_device_add,
|
|---|
| 350 | .device_rem = usbmast_device_remove,
|
|---|
| 351 | .device_gone = usbmast_device_gone,
|
|---|
| 352 | };
|
|---|
| 353 |
|
|---|
| 354 | /** USB mass storage driver. */
|
|---|
| 355 | static const usb_driver_t usbmast_driver = {
|
|---|
| 356 | .name = NAME,
|
|---|
| 357 | .ops = &usbmast_driver_ops,
|
|---|
| 358 | .endpoints = mast_endpoints
|
|---|
| 359 | };
|
|---|
| 360 |
|
|---|
| 361 | int 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 | */
|
|---|