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