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 "mast.h"
|
---|
44 |
|
---|
45 | #define NAME "usbmast"
|
---|
46 |
|
---|
47 | #define BULK_IN_EP 0
|
---|
48 | #define BULK_OUT_EP 1
|
---|
49 |
|
---|
50 | #define GET_BULK_IN(dev) ((dev)->pipes[BULK_IN_EP].pipe)
|
---|
51 | #define GET_BULK_OUT(dev) ((dev)->pipes[BULK_OUT_EP].pipe)
|
---|
52 |
|
---|
53 | static usb_endpoint_description_t bulk_in_ep = {
|
---|
54 | .transfer_type = USB_TRANSFER_BULK,
|
---|
55 | .direction = USB_DIRECTION_IN,
|
---|
56 | .interface_class = USB_CLASS_MASS_STORAGE,
|
---|
57 | .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
|
---|
58 | .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
|
---|
59 | .flags = 0
|
---|
60 | };
|
---|
61 | static usb_endpoint_description_t bulk_out_ep = {
|
---|
62 | .transfer_type = USB_TRANSFER_BULK,
|
---|
63 | .direction = USB_DIRECTION_OUT,
|
---|
64 | .interface_class = USB_CLASS_MASS_STORAGE,
|
---|
65 | .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
|
---|
66 | .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
|
---|
67 | .flags = 0
|
---|
68 | };
|
---|
69 |
|
---|
70 | usb_endpoint_description_t *mast_endpoints[] = {
|
---|
71 | &bulk_in_ep,
|
---|
72 | &bulk_out_ep,
|
---|
73 | NULL
|
---|
74 | };
|
---|
75 |
|
---|
76 | /** Callback when new device is attached and recognized as a mass storage.
|
---|
77 | *
|
---|
78 | * @param dev Representation of a the USB device.
|
---|
79 | * @return Error code.
|
---|
80 | */
|
---|
81 | static int usbmast_add_device(usb_device_t *dev)
|
---|
82 | {
|
---|
83 | int rc;
|
---|
84 | const char *fun_name = "ctl";
|
---|
85 |
|
---|
86 | ddf_fun_t *ctl_fun = ddf_fun_create(dev->ddf_dev, fun_exposed,
|
---|
87 | fun_name);
|
---|
88 | if (ctl_fun == NULL) {
|
---|
89 | usb_log_error("Failed to create control function.\n");
|
---|
90 | return ENOMEM;
|
---|
91 | }
|
---|
92 | rc = ddf_fun_bind(ctl_fun);
|
---|
93 | if (rc != EOK) {
|
---|
94 | usb_log_error("Failed to bind control function: %s.\n",
|
---|
95 | str_error(rc));
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | usb_log_info("Pretending to control mass storage `%s'.\n",
|
---|
100 | dev->ddf_dev->name);
|
---|
101 | usb_log_debug(" Bulk in endpoint: %d [%zuB].\n",
|
---|
102 | dev->pipes[BULK_IN_EP].pipe->endpoint_no,
|
---|
103 | (size_t) dev->pipes[BULK_IN_EP].descriptor->max_packet_size);
|
---|
104 | usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
|
---|
105 | dev->pipes[BULK_OUT_EP].pipe->endpoint_no,
|
---|
106 | (size_t) dev->pipes[BULK_OUT_EP].descriptor->max_packet_size);
|
---|
107 |
|
---|
108 | usb_log_debug("Get LUN count...\n");
|
---|
109 | size_t lun_count = usb_masstor_get_lun_count(dev);
|
---|
110 |
|
---|
111 | usb_log_debug("Inquire...\n");
|
---|
112 | usb_massstor_inquiry_result_t inquiry;
|
---|
113 | rc = usb_massstor_inquiry(dev, BULK_IN_EP, BULK_OUT_EP, &inquiry);
|
---|
114 | if (rc != EOK) {
|
---|
115 | usb_log_warning("Failed to inquire device `%s': %s.\n",
|
---|
116 | dev->ddf_dev->name, str_error(rc));
|
---|
117 | return EOK;
|
---|
118 | }
|
---|
119 |
|
---|
120 | usb_log_info("Mass storage `%s': " \
|
---|
121 | "`%s' by `%s' is %s (%s), %zu LUN(s).\n",
|
---|
122 | dev->ddf_dev->name,
|
---|
123 | inquiry.product_and_revision, inquiry.vendor_id,
|
---|
124 | usb_str_masstor_scsi_peripheral_device_type(inquiry.peripheral_device_type),
|
---|
125 | inquiry.removable ? "removable" : "non-removable",
|
---|
126 | lun_count);
|
---|
127 |
|
---|
128 | return EOK;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** USB mass storage driver ops. */
|
---|
132 | static usb_driver_ops_t usbmast_driver_ops = {
|
---|
133 | .add_device = usbmast_add_device,
|
---|
134 | };
|
---|
135 |
|
---|
136 | /** USB mass storage driver. */
|
---|
137 | static usb_driver_t usbmast_driver = {
|
---|
138 | .name = NAME,
|
---|
139 | .ops = &usbmast_driver_ops,
|
---|
140 | .endpoints = mast_endpoints
|
---|
141 | };
|
---|
142 |
|
---|
143 | int main(int argc, char *argv[])
|
---|
144 | {
|
---|
145 | usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
|
---|
146 |
|
---|
147 | return usb_driver_main(&usbmast_driver);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * @}
|
---|
152 | */
|
---|