source: mainline/uspace/drv/bus/usb/usbmast/main.c@ 87037cc9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 87037cc9 was 87037cc9, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

usbmast: Keep track of pointers to created LUN functions.

  • Property mode set to 100644
File size: 8.2 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * Copyright (c) 2011 Jiri Svoboda
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 */
37#include <as.h>
38#include <async.h>
39#include <ipc/bd.h>
40#include <macros.h>
41#include <usb/dev/driver.h>
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>
47#include "cmdw.h"
48#include "bo_trans.h"
49#include "scsi_ms.h"
50#include "usbmast.h"
51
52#define NAME "usbmast"
53
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
57static 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};
65static 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
74usb_endpoint_description_t *mast_endpoints[] = {
75 &bulk_in_ep,
76 &bulk_out_ep,
77 NULL
78};
79
80static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun);
81static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
82 void *arg);
83
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 */
89static int usbmast_device_add(usb_device_t *dev)
90{
91 int rc;
92 usbmast_dev_t *mdev = NULL;
93 unsigned i;
94
95 /* Allocate softstate */
96 dev->driver_data = mdev = malloc(sizeof(usbmast_dev_t));
97 if (mdev == NULL) {
98 usb_log_error("Failed allocating softstate.\n");
99 return ENOMEM;
100 }
101
102 mdev->ddf_dev = dev->ddf_dev;
103 mdev->usb_dev = dev;
104
105 usb_log_info("Initializing mass storage `%s'.\n", dev->ddf_dev->name);
106 usb_log_debug(" Bulk in endpoint: %d [%zuB].\n",
107 dev->pipes[BULK_IN_EP].pipe->endpoint_no,
108 (size_t) dev->pipes[BULK_IN_EP].descriptor->max_packet_size);
109 usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
110 dev->pipes[BULK_OUT_EP].pipe->endpoint_no,
111 (size_t) dev->pipes[BULK_OUT_EP].descriptor->max_packet_size);
112
113 usb_log_debug("Get LUN count...\n");
114 mdev->lun_count = usb_masstor_get_lun_count(mdev);
115 mdev->luns = calloc(mdev->lun_count, sizeof(ddf_fun_t*));
116 if (mdev->luns == NULL) {
117 rc = ENOMEM;
118 usb_log_error("Failed allocating luns table.\n");
119 goto error;
120 }
121
122 for (i = 0; i < mdev->lun_count; i++) {
123 rc = usbmast_fun_create(mdev, i);
124 if (rc != EOK)
125 goto error;
126 }
127
128 return EOK;
129error:
130 /* XXX Destroy functions */
131 return rc;
132}
133
134/** Create mass storage function.
135 *
136 * Called once for each LUN.
137 *
138 * @param mdev Mass storage device
139 * @param lun LUN
140 * @return EOK on success or negative error code.
141 */
142static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun)
143{
144 int rc;
145 char *fun_name = NULL;
146 ddf_fun_t *fun = NULL;
147 usbmast_fun_t *mfun = NULL;
148
149 if (asprintf(&fun_name, "l%u", lun) < 0) {
150 usb_log_error("Out of memory.\n");
151 rc = ENOMEM;
152 goto error;
153 }
154
155 fun = ddf_fun_create(mdev->ddf_dev, fun_exposed, fun_name);
156 if (fun == NULL) {
157 usb_log_error("Failed to create DDF function %s.\n", fun_name);
158 rc = ENOMEM;
159 goto error;
160 }
161
162 /* Allocate soft state */
163 mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
164 if (mfun == NULL) {
165 usb_log_error("Failed allocating softstate.\n");
166 rc = ENOMEM;
167 goto error;
168 }
169
170 mfun->ddf_fun = fun;
171 mfun->mdev = mdev;
172 mfun->lun = lun;
173
174 /* Set up a connection handler. */
175 fun->conn_handler = usbmast_bd_connection;
176
177 usb_log_debug("Inquire...\n");
178 usbmast_inquiry_data_t inquiry;
179 rc = usbmast_inquiry(mfun, &inquiry);
180 if (rc != EOK) {
181 usb_log_warning("Failed to inquire device `%s': %s.\n",
182 mdev->ddf_dev->name, str_error(rc));
183 rc = EIO;
184 goto error;
185 }
186
187 usb_log_info("Mass storage `%s' LUN %u: " \
188 "%s by %s rev. %s is %s (%s).\n",
189 mdev->ddf_dev->name,
190 lun,
191 inquiry.product,
192 inquiry.vendor,
193 inquiry.revision,
194 usbmast_scsi_dev_type_str(inquiry.device_type),
195 inquiry.removable ? "removable" : "non-removable");
196
197 uint32_t nblocks, block_size;
198
199 rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
200 if (rc != EOK) {
201 usb_log_warning("Failed to read capacity, device `%s': %s.\n",
202 mdev->ddf_dev->name, str_error(rc));
203 rc = EIO;
204 goto error;
205 }
206
207 usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
208 "block_size=%" PRIu32 "\n", nblocks, block_size);
209
210 mfun->nblocks = nblocks;
211 mfun->block_size = block_size;
212
213 rc = ddf_fun_bind(fun);
214 if (rc != EOK) {
215 usb_log_error("Failed to bind DDF function %s: %s.\n",
216 fun_name, str_error(rc));
217 goto error;
218 }
219
220 free(fun_name);
221 mdev->luns[lun] = fun;
222
223 return EOK;
224
225 /* Error cleanup */
226error:
227 if (fun != NULL)
228 ddf_fun_destroy(fun);
229 if (fun_name != NULL)
230 free(fun_name);
231 return rc;
232}
233
234/** Blockdev client connection handler. */
235static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
236 void *arg)
237{
238 usbmast_fun_t *mfun;
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;
254 }
255
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
264 mfun = (usbmast_fun_t *) ((ddf_fun_t *)arg)->driver_data;
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:
278 async_answer_1(callid, EOK, mfun->block_size);
279 break;
280 case BD_GET_NUM_BLOCKS:
281 async_answer_2(callid, EOK, LOWER32(mfun->nblocks),
282 UPPER32(mfun->nblocks));
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);
287 retval = usbmast_read(mfun, ba, cnt, comm_buf);
288 async_answer_0(callid, retval);
289 break;
290 case BD_WRITE_BLOCKS:
291 ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
292 cnt = IPC_GET_ARG3(call);
293 retval = usbmast_write(mfun, ba, cnt, comm_buf);
294 async_answer_0(callid, retval);
295 break;
296 default:
297 async_answer_0(callid, EINVAL);
298 }
299 }
300}
301
302/** USB mass storage driver ops. */
303static usb_driver_ops_t usbmast_driver_ops = {
304 .device_add = usbmast_device_add,
305};
306
307/** USB mass storage driver. */
308static usb_driver_t usbmast_driver = {
309 .name = NAME,
310 .ops = &usbmast_driver_ops,
311 .endpoints = mast_endpoints
312};
313
314int 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 */
Note: See TracBrowser for help on using the repository browser.