| 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 <bitops.h>
|
|---|
| 37 | #include <usb/dev/driver.h>
|
|---|
| 38 | #include <usb/debug.h>
|
|---|
| 39 | #include <usb/classes/classes.h>
|
|---|
| 40 | #include <usb/classes/massstor.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <str_error.h>
|
|---|
| 43 | #include <str.h>
|
|---|
| 44 | #include <ctype.h>
|
|---|
| 45 | #include <scsi/spc.h>
|
|---|
| 46 | #include "cmds.h"
|
|---|
| 47 | #include "mast.h"
|
|---|
| 48 |
|
|---|
| 49 | /** Get string representation for SCSI peripheral device type.
|
|---|
| 50 | *
|
|---|
| 51 | * @param type SCSI peripheral device type code.
|
|---|
| 52 | * @return String representation.
|
|---|
| 53 | */
|
|---|
| 54 | const char *usb_str_masstor_scsi_peripheral_device_type(unsigned type)
|
|---|
| 55 | {
|
|---|
| 56 | return scsi_get_dev_type_str(type);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /** Perform SCSI INQUIRY command on USB mass storage device.
|
|---|
| 60 | *
|
|---|
| 61 | * @param dev USB device.
|
|---|
| 62 | * @param bulk_in_idx Index (in dev->pipes) of bulk in pipe.
|
|---|
| 63 | * @param bulk_out_idx Index of bulk out pipe.
|
|---|
| 64 | * @param inquiry_result Where to store parsed inquiry result.
|
|---|
| 65 | * @return Error code.
|
|---|
| 66 | */
|
|---|
| 67 | int usb_massstor_inquiry(usb_device_t *dev,
|
|---|
| 68 | usb_massstor_inquiry_result_t *inquiry_result)
|
|---|
| 69 | {
|
|---|
| 70 | scsi_std_inquiry_data_t inq_data;
|
|---|
| 71 | size_t response_len;
|
|---|
| 72 | scsi_cdb_inquiry_t inquiry = {
|
|---|
| 73 | .op_code = SCSI_CMD_INQUIRY,
|
|---|
| 74 | .evpd = 0,
|
|---|
| 75 | .page_code = 0,
|
|---|
| 76 | .alloc_len = host2uint16_t_be(sizeof(inq_data)),
|
|---|
| 77 | .control = 0
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | int rc;
|
|---|
| 81 |
|
|---|
| 82 | rc = usb_massstor_data_in(dev, 0xDEADBEEF, 0, (uint8_t *) &inquiry,
|
|---|
| 83 | sizeof(inquiry), &inq_data, sizeof(inq_data), &response_len);
|
|---|
| 84 |
|
|---|
| 85 | if (rc != EOK) {
|
|---|
| 86 | usb_log_error("Failed to probe device %s using %s: %s.\n",
|
|---|
| 87 | dev->ddf_dev->name, "SCSI:INQUIRY", str_error(rc));
|
|---|
| 88 | return rc;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | if (response_len < SCSI_STD_INQUIRY_DATA_MIN_SIZE) {
|
|---|
| 92 | usb_log_error("The SCSI inquiry response is too short.\n");
|
|---|
| 93 | return EIO;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /*
|
|---|
| 97 | * Parse inquiry data and fill in the result structure.
|
|---|
| 98 | */
|
|---|
| 99 |
|
|---|
| 100 | bzero(inquiry_result, sizeof(*inquiry_result));
|
|---|
| 101 |
|
|---|
| 102 | inquiry_result->device_type = BIT_RANGE_EXTRACT(uint8_t,
|
|---|
| 103 | inq_data.pqual_devtype, SCSI_PQDT_DEV_TYPE_h, SCSI_PQDT_DEV_TYPE_l);
|
|---|
| 104 |
|
|---|
| 105 | inquiry_result->removable = BIT_RANGE_EXTRACT(uint8_t,
|
|---|
| 106 | inq_data.rmb, SCSI_RMB_RMB, SCSI_RMB_RMB);
|
|---|
| 107 |
|
|---|
| 108 | spascii_to_str(inquiry_result->vendor, SCSI_INQ_VENDOR_STR_BUFSIZE,
|
|---|
| 109 | inq_data.vendor, sizeof(inq_data.vendor));
|
|---|
| 110 |
|
|---|
| 111 | spascii_to_str(inquiry_result->product, SCSI_INQ_PRODUCT_STR_BUFSIZE,
|
|---|
| 112 | inq_data.product, sizeof(inq_data.product));
|
|---|
| 113 |
|
|---|
| 114 | spascii_to_str(inquiry_result->revision, SCSI_INQ_REVISION_STR_BUFSIZE,
|
|---|
| 115 | inq_data.revision, sizeof(inq_data.revision));
|
|---|
| 116 |
|
|---|
| 117 | return EOK;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * @}
|
|---|
| 122 | */
|
|---|