1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * Copyright (c) 2011 Jan Vesely
|
---|
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 | /** @addtogroup libusbdev
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * USB device driver framework.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <usb_iface.h>
|
---|
37 | #include <usb/dev/alternate_ifaces.h>
|
---|
38 | #include <usb/dev/device.h>
|
---|
39 | #include <usb/dev/pipes.h>
|
---|
40 | #include <usb/dev/request.h>
|
---|
41 | #include <usb/debug.h>
|
---|
42 | #include <usb/descriptor.h>
|
---|
43 | #include <usb/usb.h>
|
---|
44 |
|
---|
45 | #include <assert.h>
|
---|
46 | #include <async.h>
|
---|
47 | #include <devman.h>
|
---|
48 | #include <errno.h>
|
---|
49 | #include <stdlib.h>
|
---|
50 |
|
---|
51 | #include <ddf/driver.h>
|
---|
52 |
|
---|
53 | /** USB device structure. */
|
---|
54 | typedef struct usb_device {
|
---|
55 | /** Connection to device on USB bus */
|
---|
56 | usb_dev_session_t *bus_session;
|
---|
57 | /** devman handle */
|
---|
58 | devman_handle_t handle;
|
---|
59 | /** The default control pipe. */
|
---|
60 | usb_pipe_t ctrl_pipe;
|
---|
61 |
|
---|
62 | /** Other endpoint pipes.
|
---|
63 | * This is an array of other endpoint pipes in the same order as
|
---|
64 | * in usb_driver_t.
|
---|
65 | */
|
---|
66 | usb_endpoint_mapping_t *pipes;
|
---|
67 | /** Number of other endpoint pipes. */
|
---|
68 | size_t pipes_count;
|
---|
69 | /** Current interface.
|
---|
70 | * Usually, drivers operate on single interface only.
|
---|
71 | * This item contains the value of the interface or -1 for any.
|
---|
72 | */
|
---|
73 | int interface_no;
|
---|
74 | /** Alternative interfaces. */
|
---|
75 | usb_alternate_interfaces_t alternate_interfaces;
|
---|
76 | /** Some useful descriptors for USB device. */
|
---|
77 | usb_device_descriptors_t descriptors;
|
---|
78 | /** Generic DDF device backing this one. DO NOT TOUCH! */
|
---|
79 | ddf_dev_t *ddf_dev;
|
---|
80 | /** Custom driver data.
|
---|
81 | * Do not use the entry in generic device, that is already used
|
---|
82 | * by the framework.
|
---|
83 | */
|
---|
84 | void *driver_data;
|
---|
85 |
|
---|
86 | } usb_device_t;
|
---|
87 |
|
---|
88 | /** Count number of pipes the driver expects.
|
---|
89 | *
|
---|
90 | * @param drv USB driver.
|
---|
91 | * @return Number of pipes (excluding default control pipe).
|
---|
92 | */
|
---|
93 | static inline size_t count_pipes(const usb_endpoint_description_t **endpoints)
|
---|
94 | {
|
---|
95 | size_t count;
|
---|
96 | for (count = 0; endpoints != NULL && endpoints[count] != NULL; ++count);
|
---|
97 | return count;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /** Change interface setting of a device.
|
---|
101 | * This function selects new alternate setting of an interface by issuing
|
---|
102 | * proper USB command to the device and also creates new USB pipes
|
---|
103 | * under @c dev->pipes.
|
---|
104 | *
|
---|
105 | * @warning This function is intended for drivers working at interface level.
|
---|
106 | * For drivers controlling the whole device, you need to change interface
|
---|
107 | * manually using usb_request_set_interface() and creating new pipes
|
---|
108 | * with usb_pipe_initialize_from_configuration().
|
---|
109 | *
|
---|
110 | * @warning This is a wrapper function that does several operations that
|
---|
111 | * can fail and that cannot be rollbacked easily. That means that a failure
|
---|
112 | * during the SET_INTERFACE request would result in having a device with
|
---|
113 | * no pipes at all (except the default control one). That is because the old
|
---|
114 | * pipes needs to be unregistered at HC first and the new ones could not
|
---|
115 | * be created.
|
---|
116 | *
|
---|
117 | * @param dev USB device.
|
---|
118 | * @param alternate_setting Alternate setting to choose.
|
---|
119 | * @param endpoints New endpoint descriptions.
|
---|
120 | * @return Error code.
|
---|
121 | */
|
---|
122 | int usb_device_select_interface(usb_device_t *usb_dev,
|
---|
123 | uint8_t alternate_setting, const usb_endpoint_description_t **endpoints)
|
---|
124 | {
|
---|
125 | assert(usb_dev);
|
---|
126 |
|
---|
127 | if (usb_dev->interface_no < 0) {
|
---|
128 | return EINVAL;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /* Change the interface itself. */
|
---|
132 | int rc = usb_request_set_interface(&usb_dev->ctrl_pipe,
|
---|
133 | usb_dev->interface_no, alternate_setting);
|
---|
134 | if (rc != EOK) {
|
---|
135 | return rc;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /* Change current alternative */
|
---|
139 | usb_dev->alternate_interfaces.current = alternate_setting;
|
---|
140 |
|
---|
141 | /* Destroy existing pipes. */
|
---|
142 | usb_device_destroy_pipes(usb_dev);
|
---|
143 |
|
---|
144 | /* Create new pipes. */
|
---|
145 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
146 |
|
---|
147 | return rc;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /** Retrieve basic descriptors from the device.
|
---|
151 | *
|
---|
152 | * @param[in] ctrl_pipe Control endpoint pipe.
|
---|
153 | * @param[out] descriptors Where to store the descriptors.
|
---|
154 | * @return Error code.
|
---|
155 | */
|
---|
156 | static int usb_device_retrieve_descriptors(usb_device_t *usb_dev)
|
---|
157 | {
|
---|
158 | assert(usb_dev);
|
---|
159 | assert(usb_dev->descriptors.full_config == NULL);
|
---|
160 |
|
---|
161 | /* Get the device descriptor. */
|
---|
162 | int rc = usb_request_get_device_descriptor(&usb_dev->ctrl_pipe,
|
---|
163 | &usb_dev->descriptors.device);
|
---|
164 | if (rc != EOK) {
|
---|
165 | return rc;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /* Get the full configuration descriptor. */
|
---|
169 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
170 | &usb_dev->ctrl_pipe, 0,
|
---|
171 | &usb_dev->descriptors.full_config,
|
---|
172 | &usb_dev->descriptors.full_config_size);
|
---|
173 |
|
---|
174 |
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /** Cleanup structure initialized via usb_device_retrieve_descriptors.
|
---|
179 | *
|
---|
180 | * @param[in] descriptors Where to store the descriptors.
|
---|
181 | */
|
---|
182 | static void usb_device_release_descriptors(usb_device_t *usb_dev)
|
---|
183 | {
|
---|
184 | assert(usb_dev);
|
---|
185 | free(usb_dev->descriptors.full_config);
|
---|
186 | usb_dev->descriptors.full_config = NULL;
|
---|
187 | usb_dev->descriptors.full_config_size = 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** Create pipes for a device.
|
---|
191 | *
|
---|
192 | * This is more or less a wrapper that does following actions:
|
---|
193 | * - allocate and initialize pipes
|
---|
194 | * - map endpoints to the pipes based on the descriptions
|
---|
195 | * - registers endpoints with the host controller
|
---|
196 | *
|
---|
197 | * @param[in] endpoints Endpoints description, NULL terminated.
|
---|
198 | * @param[in] config_descr Configuration descriptor of active configuration.
|
---|
199 | * @param[in] config_descr_size Size of @p config_descr in bytes.
|
---|
200 | * @param[in] interface_no Interface to map from.
|
---|
201 | * @param[in] interface_setting Interface setting (default is usually 0).
|
---|
202 | * @param[out] pipes_ptr Where to store array of created pipes
|
---|
203 | * (not NULL terminated).
|
---|
204 | * @param[out] pipes_count_ptr Where to store number of pipes
|
---|
205 | * (set to NULL if you wish to ignore the count).
|
---|
206 | * @return Error code.
|
---|
207 | */
|
---|
208 | int usb_device_create_pipes(usb_device_t *usb_dev,
|
---|
209 | const usb_endpoint_description_t **endpoints)
|
---|
210 | {
|
---|
211 | assert(usb_dev);
|
---|
212 | assert(usb_dev->descriptors.full_config);
|
---|
213 | assert(usb_dev->pipes == NULL);
|
---|
214 | assert(usb_dev->pipes_count == 0);
|
---|
215 |
|
---|
216 | size_t pipe_count = count_pipes(endpoints);
|
---|
217 | if (pipe_count == 0) {
|
---|
218 | return EOK;
|
---|
219 | }
|
---|
220 |
|
---|
221 | usb_endpoint_mapping_t *pipes =
|
---|
222 | calloc(pipe_count, sizeof(usb_endpoint_mapping_t));
|
---|
223 | if (pipes == NULL) {
|
---|
224 | return ENOMEM;
|
---|
225 | }
|
---|
226 |
|
---|
227 | /* Now initialize. */
|
---|
228 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
229 | pipes[i].description = endpoints[i];
|
---|
230 | pipes[i].interface_no = usb_dev->interface_no;
|
---|
231 | pipes[i].interface_setting =
|
---|
232 | usb_dev->alternate_interfaces.current;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /* Find the mapping from configuration descriptor. */
|
---|
236 | int rc = usb_pipe_initialize_from_configuration(pipes, pipe_count,
|
---|
237 | usb_dev->descriptors.full_config,
|
---|
238 | usb_dev->descriptors.full_config_size,
|
---|
239 | usb_dev->bus_session);
|
---|
240 | if (rc != EOK) {
|
---|
241 | free(pipes);
|
---|
242 | return rc;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /* Register created pipes. */
|
---|
246 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
247 | if (pipes[i].present) {
|
---|
248 | rc = usb_pipe_register(&pipes[i].pipe,
|
---|
249 | pipes[i].descriptor->poll_interval);
|
---|
250 | if (rc != EOK) {
|
---|
251 | goto rollback_unregister_endpoints;
|
---|
252 | }
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | usb_dev->pipes = pipes;
|
---|
257 | usb_dev->pipes_count = pipe_count;
|
---|
258 |
|
---|
259 | return EOK;
|
---|
260 |
|
---|
261 | /*
|
---|
262 | * Jump here if something went wrong after endpoints have
|
---|
263 | * been registered.
|
---|
264 | * This is also the target when the registration of
|
---|
265 | * endpoints fails.
|
---|
266 | */
|
---|
267 | rollback_unregister_endpoints:
|
---|
268 | for (size_t i = 0; i < pipe_count; i++) {
|
---|
269 | if (pipes[i].present) {
|
---|
270 | usb_pipe_unregister(&pipes[i].pipe);
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | free(pipes);
|
---|
275 | return rc;
|
---|
276 | }
|
---|
277 |
|
---|
278 | /** Destroy pipes previously created by usb_device_create_pipes.
|
---|
279 | *
|
---|
280 | * @param[in] usb_dev USB device.
|
---|
281 | */
|
---|
282 | void usb_device_destroy_pipes(usb_device_t *usb_dev)
|
---|
283 | {
|
---|
284 | assert(usb_dev);
|
---|
285 | assert(usb_dev->pipes || usb_dev->pipes_count == 0);
|
---|
286 | /* Destroy the pipes. */
|
---|
287 | for (size_t i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
288 | usb_log_debug2("Unregistering pipe %zu: %spresent.\n",
|
---|
289 | i, usb_dev->pipes[i].present ? "" : "not ");
|
---|
290 | if (usb_dev->pipes[i].present)
|
---|
291 | usb_pipe_unregister(&usb_dev->pipes[i].pipe);
|
---|
292 | }
|
---|
293 | free(usb_dev->pipes);
|
---|
294 | usb_dev->pipes = NULL;
|
---|
295 | usb_dev->pipes_count = 0;
|
---|
296 | }
|
---|
297 |
|
---|
298 | usb_pipe_t *usb_device_get_default_pipe(usb_device_t *usb_dev)
|
---|
299 | {
|
---|
300 | assert(usb_dev);
|
---|
301 | return &usb_dev->ctrl_pipe;
|
---|
302 | }
|
---|
303 |
|
---|
304 | usb_endpoint_mapping_t *usb_device_get_mapped_ep_desc(usb_device_t *usb_dev,
|
---|
305 | const usb_endpoint_description_t *desc)
|
---|
306 | {
|
---|
307 | assert(usb_dev);
|
---|
308 | for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
309 | if (usb_dev->pipes[i].description == desc)
|
---|
310 | return &usb_dev->pipes[i];
|
---|
311 | }
|
---|
312 | return NULL;
|
---|
313 | }
|
---|
314 |
|
---|
315 | usb_endpoint_mapping_t * usb_device_get_mapped_ep(
|
---|
316 | usb_device_t *usb_dev, usb_endpoint_t ep)
|
---|
317 | {
|
---|
318 | assert(usb_dev);
|
---|
319 | for (unsigned i = 0; i < usb_dev->pipes_count; ++i) {
|
---|
320 | if (usb_dev->pipes[i].pipe.endpoint_no == ep)
|
---|
321 | return &usb_dev->pipes[i];
|
---|
322 | }
|
---|
323 | return NULL;
|
---|
324 | }
|
---|
325 |
|
---|
326 | int usb_device_get_iface_number(usb_device_t *usb_dev)
|
---|
327 | {
|
---|
328 | assert(usb_dev);
|
---|
329 | return usb_dev->interface_no;
|
---|
330 | }
|
---|
331 |
|
---|
332 | devman_handle_t usb_device_get_devman_handle(usb_device_t *usb_dev)
|
---|
333 | {
|
---|
334 | assert(usb_dev);
|
---|
335 | return usb_dev->handle;
|
---|
336 | }
|
---|
337 |
|
---|
338 | const usb_device_descriptors_t *usb_device_descriptors(usb_device_t *usb_dev)
|
---|
339 | {
|
---|
340 | assert(usb_dev);
|
---|
341 | return &usb_dev->descriptors;
|
---|
342 | }
|
---|
343 |
|
---|
344 | const usb_alternate_interfaces_t * usb_device_get_alternative_ifaces(
|
---|
345 | usb_device_t *usb_dev)
|
---|
346 | {
|
---|
347 | assert(usb_dev);
|
---|
348 | return &usb_dev->alternate_interfaces;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /** Clean instance of a USB device.
|
---|
352 | *
|
---|
353 | * @param dev Device to be de-initialized.
|
---|
354 | *
|
---|
355 | * Does not free/destroy supplied pointer.
|
---|
356 | */
|
---|
357 | static void usb_device_fini(usb_device_t *usb_dev)
|
---|
358 | {
|
---|
359 | if (usb_dev) {
|
---|
360 | /* Destroy existing pipes. */
|
---|
361 | usb_device_destroy_pipes(usb_dev);
|
---|
362 | /* Ignore errors and hope for the best. */
|
---|
363 | usb_alternate_interfaces_deinit(&usb_dev->alternate_interfaces);
|
---|
364 | usb_device_release_descriptors(usb_dev);
|
---|
365 | free(usb_dev->driver_data);
|
---|
366 | usb_dev->driver_data = NULL;
|
---|
367 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
368 | usb_dev->bus_session = NULL;
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | /** Initialize new instance of USB device.
|
---|
373 | *
|
---|
374 | * @param[in] usb_dev Pointer to the new device.
|
---|
375 | * @param[in] ddf_dev Generic DDF device backing the USB one.
|
---|
376 | * @param[in] endpoints NULL terminated array of endpoints (NULL for none).
|
---|
377 | * @param[out] errstr_ptr Where to store description of context
|
---|
378 | * (in case error occurs).
|
---|
379 | * @return Error code.
|
---|
380 | */
|
---|
381 | static int usb_device_init(usb_device_t *usb_dev, ddf_dev_t *ddf_dev,
|
---|
382 | const usb_endpoint_description_t **endpoints, const char **errstr_ptr,
|
---|
383 | devman_handle_t handle, int interface_no)
|
---|
384 | {
|
---|
385 | assert(usb_dev != NULL);
|
---|
386 | assert(errstr_ptr);
|
---|
387 |
|
---|
388 | *errstr_ptr = NULL;
|
---|
389 |
|
---|
390 | usb_dev->ddf_dev = ddf_dev;
|
---|
391 | usb_dev->handle = handle;
|
---|
392 | usb_dev->interface_no = interface_no;
|
---|
393 | usb_dev->driver_data = NULL;
|
---|
394 | usb_dev->descriptors.full_config = NULL;
|
---|
395 | usb_dev->descriptors.full_config_size = 0;
|
---|
396 | usb_dev->pipes_count = 0;
|
---|
397 | usb_dev->pipes = NULL;
|
---|
398 |
|
---|
399 | usb_dev->bus_session = usb_dev_connect(handle);
|
---|
400 |
|
---|
401 | if (!usb_dev->bus_session) {
|
---|
402 | *errstr_ptr = "device bus session create";
|
---|
403 | return ENOMEM;
|
---|
404 | }
|
---|
405 |
|
---|
406 | /* This pipe was registered by the hub driver,
|
---|
407 | * during device initialization. */
|
---|
408 | int rc = usb_pipe_initialize_default_control(
|
---|
409 | &usb_dev->ctrl_pipe, usb_dev->bus_session);
|
---|
410 | if (rc != EOK) {
|
---|
411 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
412 | *errstr_ptr = "default control pipe initialization";
|
---|
413 | return rc;
|
---|
414 | }
|
---|
415 |
|
---|
416 | /* Retrieve standard descriptors. */
|
---|
417 | rc = usb_device_retrieve_descriptors(usb_dev);
|
---|
418 | if (rc != EOK) {
|
---|
419 | *errstr_ptr = "descriptor retrieval";
|
---|
420 | usb_dev_disconnect(usb_dev->bus_session);
|
---|
421 | return rc;
|
---|
422 | }
|
---|
423 |
|
---|
424 | /* Create alternate interfaces. We will silently ignore failure.
|
---|
425 | * We might either control one interface or an entire device,
|
---|
426 | * it makes no sense to speak about alternate interfaces when
|
---|
427 | * controlling a device. */
|
---|
428 | rc = usb_alternate_interfaces_init(&usb_dev->alternate_interfaces,
|
---|
429 | usb_dev->descriptors.full_config,
|
---|
430 | usb_dev->descriptors.full_config_size, usb_dev->interface_no);
|
---|
431 |
|
---|
432 | if (endpoints) {
|
---|
433 | /* Create and register other pipes than default control (EP 0)*/
|
---|
434 | rc = usb_device_create_pipes(usb_dev, endpoints);
|
---|
435 | if (rc != EOK) {
|
---|
436 | usb_device_fini(usb_dev);
|
---|
437 | *errstr_ptr = "pipes initialization";
|
---|
438 | return rc;
|
---|
439 | }
|
---|
440 | }
|
---|
441 |
|
---|
442 | return EOK;
|
---|
443 | }
|
---|
444 |
|
---|
445 | static int usb_device_get_info(async_sess_t *sess, devman_handle_t *handle,
|
---|
446 | int *iface_no)
|
---|
447 | {
|
---|
448 | assert(handle);
|
---|
449 | assert(iface_no);
|
---|
450 | async_exch_t *exch = async_exchange_begin(sess);
|
---|
451 | if (!exch)
|
---|
452 | return EPARTY;
|
---|
453 | int ret = usb_get_my_device_handle(exch, handle);
|
---|
454 | if (ret == EOK) {
|
---|
455 | ret = usb_get_my_interface(exch, iface_no);
|
---|
456 | if (ret == ENOTSUP) {
|
---|
457 | *iface_no = -1;
|
---|
458 | ret = EOK;
|
---|
459 | }
|
---|
460 | }
|
---|
461 | async_exchange_end(exch);
|
---|
462 | return ret;
|
---|
463 | }
|
---|
464 |
|
---|
465 | int usb_device_create_ddf(ddf_dev_t *ddf_dev,
|
---|
466 | const usb_endpoint_description_t **desc, const char **err)
|
---|
467 | {
|
---|
468 | assert(ddf_dev);
|
---|
469 | assert(err);
|
---|
470 |
|
---|
471 | devman_handle_t h = 0;
|
---|
472 | int iface_no = -1;
|
---|
473 |
|
---|
474 | async_sess_t *sess = devman_parent_device_connect(
|
---|
475 | ddf_dev_get_handle(ddf_dev), IPC_FLAG_BLOCKING);
|
---|
476 | const int ret = usb_device_get_info(sess, &h, &iface_no);
|
---|
477 | async_hangup(sess);
|
---|
478 | if (ret != EOK)
|
---|
479 | return ret;
|
---|
480 |
|
---|
481 | usb_device_t *usb_dev =
|
---|
482 | ddf_dev_data_alloc(ddf_dev, sizeof(usb_device_t));
|
---|
483 | if (usb_dev == NULL) {
|
---|
484 | *err = "DDF data alloc";
|
---|
485 | return ENOMEM;
|
---|
486 | }
|
---|
487 |
|
---|
488 | return usb_device_init(usb_dev, ddf_dev, desc, err, h, iface_no);
|
---|
489 | }
|
---|
490 |
|
---|
491 | void usb_device_destroy_ddf(ddf_dev_t *ddf_dev)
|
---|
492 | {
|
---|
493 | assert(ddf_dev);
|
---|
494 | usb_device_t *usb_dev = ddf_dev_data_get(ddf_dev);
|
---|
495 | assert(usb_dev);
|
---|
496 | usb_device_fini(usb_dev);
|
---|
497 | return;
|
---|
498 | }
|
---|
499 |
|
---|
500 | usb_device_t * usb_device_create(devman_handle_t handle)
|
---|
501 | {
|
---|
502 | devman_handle_t h = 0;
|
---|
503 | int iface_no = -1;
|
---|
504 |
|
---|
505 | async_sess_t *sess = devman_device_connect(handle, IPC_FLAG_BLOCKING);
|
---|
506 | int ret = usb_device_get_info(sess, &h, &iface_no);
|
---|
507 | if (sess)
|
---|
508 | async_hangup(sess);
|
---|
509 | if (ret != EOK)
|
---|
510 | return NULL;
|
---|
511 |
|
---|
512 | usb_device_t *usb_dev = malloc(sizeof(usb_device_t));
|
---|
513 | if (!usb_dev)
|
---|
514 | return NULL;
|
---|
515 |
|
---|
516 | const char* dummy = NULL;
|
---|
517 | ret = usb_device_init(usb_dev, NULL, NULL, &dummy, handle, iface_no);
|
---|
518 | if (ret != EOK) {
|
---|
519 | free(usb_dev);
|
---|
520 | usb_dev = NULL;
|
---|
521 | }
|
---|
522 | return usb_dev;
|
---|
523 | }
|
---|
524 |
|
---|
525 | void usb_device_destroy(usb_device_t *usb_dev)
|
---|
526 | {
|
---|
527 | if (usb_dev) {
|
---|
528 | usb_device_fini(usb_dev);
|
---|
529 | free(usb_dev);
|
---|
530 | }
|
---|
531 | }
|
---|
532 |
|
---|
533 | const char *usb_device_get_name(usb_device_t *usb_dev)
|
---|
534 | {
|
---|
535 | assert(usb_dev);
|
---|
536 | if (usb_dev->ddf_dev)
|
---|
537 | return ddf_dev_get_name(usb_dev->ddf_dev);
|
---|
538 | return NULL;
|
---|
539 | }
|
---|
540 |
|
---|
541 | ddf_fun_t *usb_device_ddf_fun_create(usb_device_t *usb_dev, fun_type_t ftype,
|
---|
542 | const char* name)
|
---|
543 | {
|
---|
544 | assert(usb_dev);
|
---|
545 | if (usb_dev->ddf_dev)
|
---|
546 | return ddf_fun_create(usb_dev->ddf_dev, ftype, name);
|
---|
547 | return NULL;
|
---|
548 | }
|
---|
549 |
|
---|
550 | async_exch_t * usb_device_bus_exchange_begin(usb_device_t *usb_dev)
|
---|
551 | {
|
---|
552 | assert(usb_dev);
|
---|
553 | return async_exchange_begin(usb_dev->bus_session);
|
---|
554 | }
|
---|
555 |
|
---|
556 | void usb_device_bus_exchange_end(async_exch_t *exch)
|
---|
557 | {
|
---|
558 | async_exchange_end(exch);
|
---|
559 | }
|
---|
560 |
|
---|
561 | /** Allocate driver specific data.
|
---|
562 | * @param usb_dev usb_device structure.
|
---|
563 | * @param size requested data size.
|
---|
564 | * @return Pointer to the newly allocated space, NULL on failure.
|
---|
565 | */
|
---|
566 | void * usb_device_data_alloc(usb_device_t *usb_dev, size_t size)
|
---|
567 | {
|
---|
568 | assert(usb_dev);
|
---|
569 | assert(usb_dev->driver_data == NULL);
|
---|
570 | return usb_dev->driver_data = calloc(1, size);
|
---|
571 |
|
---|
572 | }
|
---|
573 |
|
---|
574 | void * usb_device_data_get(usb_device_t *usb_dev)
|
---|
575 | {
|
---|
576 | assert(usb_dev);
|
---|
577 | return usb_dev->driver_data;
|
---|
578 | }
|
---|
579 |
|
---|
580 | /**
|
---|
581 | * @}
|
---|
582 | */
|
---|