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