source: mainline/uspace/drv/block/usbmast/main.c@ bb1e968

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bb1e968 was bb1e968, checked in by Petr Manek <petr.manek@…>, 8 years ago

usbmast: add device_removed callback

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