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

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

usbmast: downgrade cmd_fail to warning

set debug level explicitly

  • Property mode set to 100644
File size: 10.6 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 <bd_srv.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 <io/logctl.h>
47#include <str_error.h>
48#include "cmdw.h"
49#include "bo_trans.h"
50#include "scsi_ms.h"
51#include "usbmast.h"
52
53#define NAME "usbmast"
54
55static const usb_endpoint_description_t bulk_in_ep = {
56 .transfer_type = USB_TRANSFER_BULK,
57 .direction = USB_DIRECTION_IN,
58 .interface_class = USB_CLASS_MASS_STORAGE,
59 .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
60 .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
61 .flags = 0
62};
63static const usb_endpoint_description_t bulk_out_ep = {
64 .transfer_type = USB_TRANSFER_BULK,
65 .direction = USB_DIRECTION_OUT,
66 .interface_class = USB_CLASS_MASS_STORAGE,
67 .interface_subclass = USB_MASSSTOR_SUBCLASS_SCSI,
68 .interface_protocol = USB_MASSSTOR_PROTOCOL_BBB,
69 .flags = 0
70};
71
72static const usb_endpoint_description_t *mast_endpoints[] = {
73 &bulk_in_ep,
74 &bulk_out_ep,
75 NULL
76};
77
78static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun);
79static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
80 void *arg);
81
82static int usbmast_bd_open(bd_srvs_t *, bd_srv_t *);
83static int usbmast_bd_close(bd_srv_t *);
84static int usbmast_bd_read_blocks(bd_srv_t *, aoff64_t, size_t, void *, size_t);
85static int usbmast_bd_sync_cache(bd_srv_t *, aoff64_t, size_t);
86static int usbmast_bd_write_blocks(bd_srv_t *, aoff64_t, size_t, const void *, size_t);
87static int usbmast_bd_get_block_size(bd_srv_t *, size_t *);
88static int usbmast_bd_get_num_blocks(bd_srv_t *, aoff64_t *);
89
90static bd_ops_t usbmast_bd_ops = {
91 .open = usbmast_bd_open,
92 .close = usbmast_bd_close,
93 .read_blocks = usbmast_bd_read_blocks,
94 .sync_cache = usbmast_bd_sync_cache,
95 .write_blocks = usbmast_bd_write_blocks,
96 .get_block_size = usbmast_bd_get_block_size,
97 .get_num_blocks = usbmast_bd_get_num_blocks
98};
99
100static usbmast_fun_t *bd_srv_usbmast(bd_srv_t *bd)
101{
102 return (usbmast_fun_t *) bd->srvs->sarg;
103}
104
105/** Callback when a device is removed from the system.
106 *
107 * @param dev Representation of USB device.
108 * @return Error code.
109 */
110static int usbmast_device_gone(usb_device_t *dev)
111{
112 usbmast_dev_t *mdev = usb_device_data_get(dev);
113 assert(mdev);
114
115 for (size_t i = 0; i < mdev->lun_count; ++i) {
116 const int rc = ddf_fun_unbind(mdev->luns[i]);
117 if (rc != EOK) {
118 usb_log_error("Failed to unbind LUN function %zu: "
119 "%s\n", i, str_error(rc));
120 return rc;
121 }
122 ddf_fun_destroy(mdev->luns[i]);
123 mdev->luns[i] = NULL;
124 }
125 free(mdev->luns);
126 return EOK;
127}
128
129/** Callback when a device is about to be removed.
130 *
131 * @param dev Representation of USB device.
132 * @return Error code.
133 */
134static int usbmast_device_remove(usb_device_t *dev)
135{
136 //TODO: flush buffers, or whatever.
137 //TODO: remove device
138 return ENOTSUP;
139}
140
141/** Callback when new device is attached and recognized as a mass storage.
142 *
143 * @param dev Representation of USB device.
144 * @return Error code.
145 */
146static int usbmast_device_add(usb_device_t *dev)
147{
148 int rc;
149 usbmast_dev_t *mdev = NULL;
150 unsigned i;
151
152 usb_endpoint_mapping_t *epm_in =
153 usb_device_get_mapped_ep_desc(dev, &bulk_in_ep);
154 usb_endpoint_mapping_t *epm_out =
155 usb_device_get_mapped_ep_desc(dev, &bulk_out_ep);
156 if (!epm_in || !epm_out || !epm_in->present || !epm_out->present) {
157 usb_log_error("Required EPs were not mapped.\n");
158 return ENOENT;
159 }
160
161 /* Allocate softstate */
162 mdev = usb_device_data_alloc(dev, sizeof(usbmast_dev_t));
163 if (mdev == NULL) {
164 usb_log_error("Failed allocating softstate.\n");
165 return ENOMEM;
166 }
167
168 mdev->usb_dev = dev;
169
170 usb_log_info("Initializing mass storage `%s'.\n",
171 usb_device_get_name(dev));
172 usb_log_debug("Bulk in endpoint: %d [%zuB].\n",
173 epm_in->pipe.endpoint_no, epm_in->pipe.max_packet_size);
174 usb_log_debug("Bulk out endpoint: %d [%zuB].\n",
175 epm_out->pipe.endpoint_no, epm_out->pipe.max_packet_size);
176
177 usb_log_debug("Get LUN count...\n");
178 mdev->lun_count = usb_masstor_get_lun_count(mdev);
179 mdev->luns = calloc(mdev->lun_count, sizeof(ddf_fun_t*));
180 if (mdev->luns == NULL) {
181 rc = ENOMEM;
182 usb_log_error("Failed allocating luns table.\n");
183 goto error;
184 }
185
186 mdev->bulk_in_pipe = &epm_in->pipe;
187 mdev->bulk_out_pipe = &epm_out->pipe;
188 for (i = 0; i < mdev->lun_count; i++) {
189 rc = usbmast_fun_create(mdev, i);
190 if (rc != EOK)
191 goto error;
192 }
193
194 return EOK;
195error:
196 /* Destroy functions */
197 for (size_t i = 0; i < mdev->lun_count; ++i) {
198 if (mdev->luns[i] == NULL)
199 continue;
200 const int rc = ddf_fun_unbind(mdev->luns[i]);
201 if (rc != EOK) {
202 usb_log_warning("Failed to unbind LUN function %zu: "
203 "%s.\n", i, str_error(rc));
204 }
205 ddf_fun_destroy(mdev->luns[i]);
206 }
207 free(mdev->luns);
208 return rc;
209}
210
211/** Create mass storage function.
212 *
213 * Called once for each LUN.
214 *
215 * @param mdev Mass storage device
216 * @param lun LUN
217 * @return EOK on success or negative error code.
218 */
219static int usbmast_fun_create(usbmast_dev_t *mdev, unsigned lun)
220{
221 int rc;
222 char *fun_name = NULL;
223 ddf_fun_t *fun = NULL;
224 usbmast_fun_t *mfun = NULL;
225
226 if (asprintf(&fun_name, "l%u", lun) < 0) {
227 usb_log_error("Out of memory.\n");
228 rc = ENOMEM;
229 goto error;
230 }
231
232 fun = usb_device_ddf_fun_create(mdev->usb_dev, fun_exposed, fun_name);
233 if (fun == NULL) {
234 usb_log_error("Failed to create DDF function %s.\n", fun_name);
235 rc = ENOMEM;
236 goto error;
237 }
238
239 /* Allocate soft state */
240 mfun = ddf_fun_data_alloc(fun, sizeof(usbmast_fun_t));
241 if (mfun == NULL) {
242 usb_log_error("Failed allocating softstate.\n");
243 rc = ENOMEM;
244 goto error;
245 }
246
247 mfun->ddf_fun = fun;
248 mfun->mdev = mdev;
249 mfun->lun = lun;
250
251 bd_srvs_init(&mfun->bds);
252 mfun->bds.ops = &usbmast_bd_ops;
253 mfun->bds.sarg = mfun;
254
255 /* Set up a connection handler. */
256 ddf_fun_set_conn_handler(fun, usbmast_bd_connection);
257
258 usb_log_debug("Inquire...\n");
259 usbmast_inquiry_data_t inquiry;
260 rc = usbmast_inquiry(mfun, &inquiry);
261 if (rc != EOK) {
262 usb_log_warning("Failed to inquire device `%s': %s.\n",
263 usb_device_get_name(mdev->usb_dev), str_error(rc));
264 rc = EIO;
265 goto error;
266 }
267
268 usb_log_info("Mass storage `%s' LUN %u: " \
269 "%s by %s rev. %s is %s (%s).\n",
270 usb_device_get_name(mdev->usb_dev),
271 lun,
272 inquiry.product,
273 inquiry.vendor,
274 inquiry.revision,
275 usbmast_scsi_dev_type_str(inquiry.device_type),
276 inquiry.removable ? "removable" : "non-removable");
277
278 uint32_t nblocks, block_size;
279
280 rc = usbmast_read_capacity(mfun, &nblocks, &block_size);
281 if (rc != EOK) {
282 usb_log_warning("Failed to read capacity, device `%s': %s.\n",
283 usb_device_get_name(mdev->usb_dev), str_error(rc));
284 rc = EIO;
285 goto error;
286 }
287
288 usb_log_info("Read Capacity: nblocks=%" PRIu32 ", "
289 "block_size=%" PRIu32 "\n", nblocks, block_size);
290
291 mfun->nblocks = nblocks;
292 mfun->block_size = block_size;
293
294 rc = ddf_fun_bind(fun);
295 if (rc != EOK) {
296 usb_log_error("Failed to bind DDF function %s: %s.\n",
297 fun_name, str_error(rc));
298 goto error;
299 }
300
301 free(fun_name);
302 mdev->luns[lun] = fun;
303
304 return EOK;
305
306 /* Error cleanup */
307error:
308 if (fun != NULL)
309 ddf_fun_destroy(fun);
310 if (fun_name != NULL)
311 free(fun_name);
312 return rc;
313}
314
315/** Blockdev client connection handler. */
316static void usbmast_bd_connection(ipc_callid_t iid, ipc_call_t *icall,
317 void *arg)
318{
319 usbmast_fun_t *mfun;
320
321 mfun = (usbmast_fun_t *) ddf_fun_data_get((ddf_fun_t *)arg);
322 bd_conn(iid, icall, &mfun->bds);
323}
324
325/** Open device. */
326static int usbmast_bd_open(bd_srvs_t *bds, bd_srv_t *bd)
327{
328 return EOK;
329}
330
331/** Close device. */
332static int usbmast_bd_close(bd_srv_t *bd)
333{
334 return EOK;
335}
336
337/** Read blocks from the device. */
338static int usbmast_bd_read_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt, void *buf,
339 size_t size)
340{
341 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
342
343 if (size < cnt * mfun->block_size)
344 return EINVAL;
345
346 return usbmast_read(mfun, ba, cnt, buf);
347}
348
349/** Synchronize blocks to nonvolatile storage. */
350static int usbmast_bd_sync_cache(bd_srv_t *bd, uint64_t ba, size_t cnt)
351{
352 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
353
354 return usbmast_sync_cache(mfun, ba, cnt);
355}
356
357/** Write blocks to the device. */
358static int usbmast_bd_write_blocks(bd_srv_t *bd, uint64_t ba, size_t cnt,
359 const void *buf, size_t size)
360{
361 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
362
363 if (size < cnt * mfun->block_size)
364 return EINVAL;
365
366 return usbmast_write(mfun, ba, cnt, buf);
367}
368
369/** Get device block size. */
370static int usbmast_bd_get_block_size(bd_srv_t *bd, size_t *rsize)
371{
372 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
373 *rsize = mfun->block_size;
374 return EOK;
375}
376
377/** Get number of blocks on device. */
378static int usbmast_bd_get_num_blocks(bd_srv_t *bd, aoff64_t *rnb)
379{
380 usbmast_fun_t *mfun = bd_srv_usbmast(bd);
381 *rnb = mfun->nblocks;
382 return EOK;
383}
384
385
386/** USB mass storage driver ops. */
387static const usb_driver_ops_t usbmast_driver_ops = {
388 .device_add = usbmast_device_add,
389 .device_rem = usbmast_device_remove,
390 .device_gone = usbmast_device_gone,
391};
392
393/** USB mass storage driver. */
394static const usb_driver_t usbmast_driver = {
395 .name = NAME,
396 .ops = &usbmast_driver_ops,
397 .endpoints = mast_endpoints
398};
399
400int main(int argc, char *argv[])
401{
402 log_init(NAME);
403 logctl_set_log_level(NAME, LVL_NOTE);
404 return usb_driver_main(&usbmast_driver);
405}
406
407/**
408 * @}
409 */
Note: See TracBrowser for help on using the repository browser.