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

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

usb: Make endpoint descriptions const.

  • Property mode set to 100644
File size: 9.3 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
74static const usb_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 a device is removed from the system.
85 *
86 * @param dev Representation of USB device.
87 * @return Error code.
88 */
89static int usbmast_device_gone(usb_device_t *dev)
90{
91 usbmast_dev_t *mdev = dev->driver_data;
92 assert(mdev);
93
94 for (size_t i = 0; i < mdev->lun_count; ++i) {
95 const int rc = ddf_fun_unbind(mdev->luns[i]);
96 if (rc != EOK) {
97 usb_log_error("Failed to unbind LUN function %zu: "
98 "%s\n", i, str_error(rc));
99 return rc;
100 }
101 ddf_fun_destroy(mdev->luns[i]);
102 mdev->luns[i] = NULL;
103 }
104 free(mdev->luns);
105 return EOK;
106}
107
108/** Callback when new device is attached and recognized as a mass storage.
109 *
110 * @param dev Representation of a the USB device.
111 * @return Error code.
112 */
113static int usbmast_device_add(usb_device_t *dev)
114{
115 int rc;
116 usbmast_dev_t *mdev = NULL;
117 unsigned i;
118
119 /* Allocate softstate */
120 mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
121 if (mdev == NULL) {
122 usb_log_error("Failed allocating softstate.\n");
123 return ENOMEM;
124 }
125
126 mdev->ddf_dev = dev->ddf_dev;
127 mdev->usb_dev = dev;
128
129 usb_log_info("Initializing mass storage `%s'.\n", dev->ddf_dev->name);
130 usb_log_debug(" Bulk in endpoint: %d [%zuB].\n",
131 dev->pipes[BULK_IN_EP].pipe->endpoint_no,
132 (size_t) dev->pipes[BULK_IN_EP].descriptor->max_packet_size);
133 usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
134 dev->pipes[BULK_OUT_EP].pipe->endpoint_no,
135 (size_t) dev->pipes[BULK_OUT_EP].descriptor->max_packet_size);
136
137 usb_log_debug("Get LUN count...\n");
138 mdev->lun_count = usb_masstor_get_lun_count(mdev);
139 mdev->luns = calloc(mdev->lun_count, sizeof(ddf_fun_t*));
140 if (mdev->luns == NULL) {
141 rc = ENOMEM;
142 usb_log_error("Failed allocating luns table.\n");
143 goto error;
144 }
145
146 for (i = 0; i < mdev->lun_count; i++) {
147 rc = usbmast_fun_create(mdev, i);
148 if (rc != EOK)
149 goto error;
150 }
151
152 return EOK;
153error:
154 /* Destroy functions */
155 for (size_t i = 0; i < mdev->lun_count; ++i) {
156 if (mdev->luns[i] == NULL)
157 continue;
158 const int rc = ddf_fun_unbind(mdev->luns[i]);
159 if (rc != EOK) {
160 usb_log_warning("Failed to unbind LUN function %zu: "
161 "%s.\n", i, str_error(rc));
162 }
163 ddf_fun_destroy(mdev->luns[i]);
164 }
165 free(mdev->luns);
166 /*
167 * Do not free mdev here as it was passed to DDF as device driver data.
168 * It will be deallocated by DDF when the device is destroyed.
169 */
170 return rc;
171}
172
173/** Create mass storage function.
174 *
175 * Called once for each LUN.
176 *
177 * @param mdev Mass storage device
178 * @param lun LUN
179 * @return EOK on success or negative error code.
180 */
181static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun)
182{
183 int rc;
184 char *fun_name = NULL;
185 ddf_fun_t *fun = NULL;
186 usbmast_fun_t *mfun = NULL;
187
188 if (asprintf(&fun_name, "l%u", lun) < 0) {
189 usb_log_error("Out of memory.\n");
190 rc = ENOMEM;
191 goto error;
192 }
193
194 fun = ddf_fun_create(mdev->ddf_dev, fun_exposed, fun_name);
195 if (fun == NULL) {
196 usb_log_error("Failed to create DDF function %s.\n", fun_name);
197 rc = ENOMEM;
198 goto error;
199 }
200
201 /* Allocate soft state */
202 mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
203 if (mfun == NULL) {
204 usb_log_error("Failed allocating softstate.\n");
205 rc = ENOMEM;
206 goto error;
207 }
208
209 mfun->ddf_fun = fun;
210 mfun->mdev = mdev;
211 mfun->lun = lun;
212
213 /* Set up a connection handler. */
214 fun->conn_handler = usbmast_bd_connection;
215
216 usb_log_debug("Inquire...\n");
217 usbmast_inquiry_data_t inquiry;
218 rc = usbmast_inquiry(mfun, &inquiry);
219 if (rc != EOK) {
220 usb_log_warning("Failed to inquire device `%s': %s.\n",
221 mdev->ddf_dev->name, str_error(rc));
222 rc = EIO;
223 goto error;
224 }
225
226 usb_log_info("Mass storage `%s' LUN %u: " \
227 "%s by %s rev. %s is %s (%s).\n",
228 mdev->ddf_dev->name,
229 lun,
230 inquiry.product,
231 inquiry.vendor,
232 inquiry.revision,
233 usbmast_scsi_dev_type_str(inquiry.device_type),
234 inquiry.removable ? "removable" : "non-removable");
235
236 uint32_t nblocks, block_size;
237
238 rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
239 if (rc != EOK) {
240 usb_log_warning("Failed to read capacity, device `%s': %s.\n",
241 mdev->ddf_dev->name, str_error(rc));
242 rc = EIO;
243 goto error;
244 }
245
246 usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
247 "block_size=%" PRIu32 "\n", nblocks, block_size);
248
249 mfun->nblocks = nblocks;
250 mfun->block_size = block_size;
251
252 rc = ddf_fun_bind(fun);
253 if (rc != EOK) {
254 usb_log_error("Failed to bind DDF function %s: %s.\n",
255 fun_name, str_error(rc));
256 goto error;
257 }
258
259 free(fun_name);
260 mdev->luns[lun] = fun;
261
262 return EOK;
263
264 /* Error cleanup */
265error:
266 if (fun != NULL)
267 ddf_fun_destroy(fun);
268 if (fun_name != NULL)
269 free(fun_name);
270 return rc;
271}
272
273/** Blockdev client connection handler. */
274static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
275 void *arg)
276{
277 usbmast_fun_t *mfun;
278 void *comm_buf = NULL;
279 size_t comm_size;
280 ipc_callid_t callid;
281 ipc_call_t call;
282 unsigned int flags;
283 sysarg_t method;
284 uint64_t ba;
285 size_t cnt;
286 int retval;
287
288 async_answer_0(iid, EOK);
289
290 if (!async_share_out_receive(&callid, &comm_size, &flags)) {
291 async_answer_0(callid, EHANGUP);
292 return;
293 }
294
295 comm_buf = as_get_mappable_page(comm_size);
296 if (comm_buf == NULL) {
297 async_answer_0(callid, EHANGUP);
298 return;
299 }
300
301 (void) async_share_out_finalize(callid, comm_buf);
302
303 mfun = (usbmast_fun_t *) ((ddf_fun_t *)arg)->driver_data;
304
305 while (true) {
306 callid = async_get_call(&call);
307 method = IPC_GET_IMETHOD(call);
308
309 if (!method) {
310 /* The other side hung up. */
311 async_answer_0(callid, EOK);
312 return;
313 }
314
315 switch (method) {
316 case BD_GET_BLOCK_SIZE:
317 async_answer_1(callid, EOK, mfun->block_size);
318 break;
319 case BD_GET_NUM_BLOCKS:
320 async_answer_2(callid, EOK, LOWER32(mfun->nblocks),
321 UPPER32(mfun->nblocks));
322 break;
323 case BD_READ_BLOCKS:
324 ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
325 cnt = IPC_GET_ARG3(call);
326 retval = usbmast_read(mfun, ba, cnt, comm_buf);
327 async_answer_0(callid, retval);
328 break;
329 case BD_WRITE_BLOCKS:
330 ba = MERGE_LOUP32(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
331 cnt = IPC_GET_ARG3(call);
332 retval = usbmast_write(mfun, ba, cnt, comm_buf);
333 async_answer_0(callid, retval);
334 break;
335 default:
336 async_answer_0(callid, EINVAL);
337 }
338 }
339}
340
341/** USB mass storage driver ops. */
342static usb_driver_ops_t usbmast_driver_ops = {
343 .device_add = usbmast_device_add,
344 .device_gone = usbmast_device_gone,
345};
346
347/** USB mass storage driver. */
348static usb_driver_t usbmast_driver = {
349 .name = NAME,
350 .ops = &usbmast_driver_ops,
351 .endpoints = mast_endpoints
352};
353
354int main(int argc, char *argv[])
355{
356 usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
357
358 return usb_driver_main(&usbmast_driver);
359}
360
361/**
362 * @}
363 */
Note: See TracBrowser for help on using the repository browser.