| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup drvusbmast
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Main routines of USB mass storage driver.
|
|---|
| 35 | */
|
|---|
| 36 | #include <usb/dev/driver.h>
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 | #include <usb/classes/classes.h>
|
|---|
| 39 | #include <usb/classes/massstor.h>
|
|---|
| 40 | #include <errno.h>
|
|---|
| 41 | #include <str_error.h>
|
|---|
| 42 | #include "cmds.h"
|
|---|
| 43 | #include "scsi.h"
|
|---|
| 44 | #include "mast.h"
|
|---|
| 45 |
|
|---|
| 46 | #define NAME "usbmast"
|
|---|
| 47 |
|
|---|
| 48 | #define BULK_IN_EP 0
|
|---|
| 49 | #define BULK_OUT_EP 1
|
|---|
| 50 |
|
|---|
| 51 | #define GET_BULK_IN(dev) ((dev)->pipes[BULK_IN_EP].pipe)
|
|---|
| 52 | #define GET_BULK_OUT(dev) ((dev)->pipes[BULK_OUT_EP].pipe)
|
|---|
| 53 |
|
|---|
| 54 | static usb_endpoint_description_t bulk_in_ep = {
|
|---|
| 55 | .transfer_type = USB_TRANSFER_BULK,
|
|---|
| 56 | .direction = USB_DIRECTION_IN,
|
|---|
| 57 | .interface_class = USB_CLASS_MASS_STORAGE,
|
|---|
| 58 | .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
|
|---|
| 59 | .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
|
|---|
| 60 | .flags = 0
|
|---|
| 61 | };
|
|---|
| 62 | static usb_endpoint_description_t bulk_out_ep = {
|
|---|
| 63 | .transfer_type = USB_TRANSFER_BULK,
|
|---|
| 64 | .direction = USB_DIRECTION_OUT,
|
|---|
| 65 | .interface_class = USB_CLASS_MASS_STORAGE,
|
|---|
| 66 | .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
|
|---|
| 67 | .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
|
|---|
| 68 | .flags = 0
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | usb_endpoint_description_t *mast_endpoints[] = {
|
|---|
| 72 | &bulk_in_ep,
|
|---|
| 73 | &bulk_out_ep,
|
|---|
| 74 | NULL
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | /** Callback when new device is attached and recognized as a mass storage.
|
|---|
| 78 | *
|
|---|
| 79 | * @param dev Representation of a the USB device.
|
|---|
| 80 | * @return Error code.
|
|---|
| 81 | */
|
|---|
| 82 | static int usbmast_add_device(usb_device_t *dev)
|
|---|
| 83 | {
|
|---|
| 84 | int rc;
|
|---|
| 85 | const char *fun_name = "ctl";
|
|---|
| 86 |
|
|---|
| 87 | ddf_fun_t *ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed,
|
|---|
| 88 | fun_name);
|
|---|
| 89 | if (ctl_fun == NULL) {
|
|---|
| 90 | usb_log_error("Failed to create control function.\n");
|
|---|
| 91 | return ENOMEM;
|
|---|
| 92 | }
|
|---|
| 93 | rc = ddf_fun_bind(ctl_fun);
|
|---|
| 94 | if (rc != EOK) {
|
|---|
| 95 | usb_log_error("Failed to bind control function: %s.\n",
|
|---|
| 96 | str_error(rc));
|
|---|
| 97 | return rc;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | usb_log_info("Pretending to control mass storage `%s'.\n",
|
|---|
| 101 | dev->ddf_dev->name);
|
|---|
| 102 | usb_log_debug(" Bulk in endpoint: %d [%zuB].\n",
|
|---|
| 103 | dev->pipes[BULK_IN_EP].pipe->endpoint_no,
|
|---|
| 104 | (size_t) dev->pipes[BULK_IN_EP].descriptor->max_packet_size);
|
|---|
| 105 | usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
|
|---|
| 106 | dev->pipes[BULK_OUT_EP].pipe->endpoint_no,
|
|---|
| 107 | (size_t) dev->pipes[BULK_OUT_EP].descriptor->max_packet_size);
|
|---|
| 108 |
|
|---|
| 109 | size_t lun_count = usb_masstor_get_lun_count(dev);
|
|---|
| 110 |
|
|---|
| 111 | usb_massstor_inquiry_result_t inquiry;
|
|---|
| 112 | rc = usb_massstor_inquiry(dev, BULK_IN_EP, BULK_OUT_EP, &inquiry);
|
|---|
| 113 | if (rc != EOK) {
|
|---|
| 114 | usb_log_warning("Failed to inquiry device `%s': %s.\n",
|
|---|
| 115 | dev->ddf_dev->name, str_error(rc));
|
|---|
| 116 | return EOK;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | usb_log_info("Mass storage `%s': " \
|
|---|
| 120 | "`%s' by `%s' is %s (%s), %zu LUN(s).\n",
|
|---|
| 121 | dev->ddf_dev->name,
|
|---|
| 122 | inquiry.product_and_revision, inquiry.vendor_id,
|
|---|
| 123 | usb_str_masstor_scsi_peripheral_device_type(inquiry.peripheral_device_type),
|
|---|
| 124 | inquiry.removable ? "removable" : "non-removable",
|
|---|
| 125 | lun_count);
|
|---|
| 126 |
|
|---|
| 127 | return EOK;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /** USB mass storage driver ops. */
|
|---|
| 131 | static usb_driver_ops_t usbmast_driver_ops = {
|
|---|
| 132 | .add_device = usbmast_add_device,
|
|---|
| 133 | };
|
|---|
| 134 |
|
|---|
| 135 | /** USB mass storage driver. */
|
|---|
| 136 | static usb_driver_t usbmast_driver = {
|
|---|
| 137 | .name = NAME,
|
|---|
| 138 | .ops = &usbmast_driver_ops,
|
|---|
| 139 | .endpoints = mast_endpoints
|
|---|
| 140 | };
|
|---|
| 141 |
|
|---|
| 142 | int main(int argc, char *argv[])
|
|---|
| 143 | {
|
|---|
| 144 | usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
|
|---|
| 145 |
|
|---|
| 146 | return usb_driver_main(&usbmast_driver);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /**
|
|---|
| 150 | * @}
|
|---|
| 151 | */
|
|---|