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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 75eb6735 was 5f6e25e, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Leave it up to DDF to free driver-specific data. This makes it possible
to ensure soft state is not freed during calls to driver entry points.

This requires some driver changes:

  • minimum change is not to free() driver-data structures (ddf_fun_t.driver_data and ddf_dev_t.driver_data)
  • ideally allocate using ddf_dev_data_alloc(), ddf_fun_data_alloc()

I tried fixing existing drivers accordingly (mostly the minimalistic
change variant), but could have missed something.

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