source: mainline/uspace/drv/bus/usb/usbmast/main.c@ 71fa44c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 71fa44c was 71fa44c, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Implement Read Capacity command for USB mass storage.

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