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

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

usbmast: Add device_remove stub.

Add const qualifiers.

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