[3ae93a8] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup drvusbmid
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * Helper functions.
|
---|
| 35 | */
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <str_error.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <usb_iface.h>
|
---|
[b68b279] | 40 | #include <usb/ddfiface.h>
|
---|
[3ae93a8] | 41 | #include <usb/pipes.h>
|
---|
| 42 | #include <usb/classes/classes.h>
|
---|
| 43 | #include <usb/recognise.h>
|
---|
| 44 | #include "usbmid.h"
|
---|
| 45 |
|
---|
| 46 | /** Callback for DDF USB interface. */
|
---|
[eb1a2f4] | 47 | static int usb_iface_get_address_impl(ddf_fun_t *fun, devman_handle_t handle,
|
---|
[b68b279] | 48 | usb_address_t *address)
|
---|
[3ae93a8] | 49 | {
|
---|
[eb1a2f4] | 50 | return usb_iface_get_address_hub_impl(fun, handle, address);
|
---|
[3ae93a8] | 51 | }
|
---|
| 52 |
|
---|
[8ba18c6] | 53 | /** Callback for DDF USB interface. */
|
---|
[eb1a2f4] | 54 | static int usb_iface_get_interface_impl(ddf_fun_t *fun, devman_handle_t handle,
|
---|
[8ba18c6] | 55 | int *iface_no)
|
---|
| 56 | {
|
---|
[eb1a2f4] | 57 | assert(fun);
|
---|
[8ba18c6] | 58 |
|
---|
[eb1a2f4] | 59 | usbmid_interface_t *iface = fun->driver_data;
|
---|
[8ba18c6] | 60 | assert(iface);
|
---|
| 61 |
|
---|
| 62 | if (iface_no != NULL) {
|
---|
| 63 | *iface_no = iface->interface_no;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | return EOK;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[a6add7a] | 69 | /** DDF interface of the child - interface function. */
|
---|
[b68b279] | 70 | static usb_iface_t child_usb_iface = {
|
---|
| 71 | .get_hc_handle = usb_iface_get_hc_handle_hub_child_impl,
|
---|
[8ba18c6] | 72 | .get_address = usb_iface_get_address_impl,
|
---|
| 73 | .get_interface = usb_iface_get_interface_impl
|
---|
[b68b279] | 74 | };
|
---|
| 75 |
|
---|
[a6add7a] | 76 | /** Operations for children - interface functions. */
|
---|
[eb1a2f4] | 77 | static ddf_dev_ops_t child_device_ops = {
|
---|
[b68b279] | 78 | .interfaces[USB_DEV_IFACE] = &child_usb_iface
|
---|
[3ae93a8] | 79 | };
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | /** Spawn new child device from one interface.
|
---|
| 83 | *
|
---|
| 84 | * @param parent Parent MID device.
|
---|
[ecb107b] | 85 | * @param iface Interface information.
|
---|
[3ae93a8] | 86 | * @param device_descriptor Device descriptor.
|
---|
| 87 | * @param interface_descriptor Interface descriptor.
|
---|
| 88 | * @return Error code.
|
---|
| 89 | */
|
---|
[fcafa04] | 90 | int usbmid_spawn_interface_child(usb_device_t *parent,
|
---|
[ecb107b] | 91 | usbmid_interface_t *iface,
|
---|
[3ae93a8] | 92 | const usb_standard_device_descriptor_t *device_descriptor,
|
---|
| 93 | const usb_standard_interface_descriptor_t *interface_descriptor)
|
---|
| 94 | {
|
---|
[eb1a2f4] | 95 | ddf_fun_t *child = NULL;
|
---|
[3ae93a8] | 96 | char *child_name = NULL;
|
---|
| 97 | int rc;
|
---|
| 98 |
|
---|
| 99 | /*
|
---|
| 100 | * Name is class name followed by interface number.
|
---|
| 101 | * The interface number shall provide uniqueness while the
|
---|
| 102 | * class name something humanly understandable.
|
---|
| 103 | */
|
---|
| 104 | rc = asprintf(&child_name, "%s%d",
|
---|
| 105 | usb_str_class(interface_descriptor->interface_class),
|
---|
| 106 | (int) interface_descriptor->interface_number);
|
---|
| 107 | if (rc < 0) {
|
---|
| 108 | goto error_leave;
|
---|
| 109 | }
|
---|
[8ba18c6] | 110 |
|
---|
[eb1a2f4] | 111 | /* Create the device. */
|
---|
[fcafa04] | 112 | child = ddf_fun_create(parent->ddf_dev, fun_inner, child_name);
|
---|
[eb1a2f4] | 113 | if (child == NULL) {
|
---|
| 114 | rc = ENOMEM;
|
---|
| 115 | goto error_leave;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[ecb107b] | 118 | iface->fun = child;
|
---|
[eb1a2f4] | 119 |
|
---|
[ecb107b] | 120 | child->driver_data = iface;
|
---|
[b68b279] | 121 | child->ops = &child_device_ops;
|
---|
[3ae93a8] | 122 |
|
---|
[51f0e410] | 123 | rc = usb_device_create_match_ids_from_interface(device_descriptor,
|
---|
| 124 | interface_descriptor,
|
---|
[3ae93a8] | 125 | &child->match_ids);
|
---|
| 126 | if (rc != EOK) {
|
---|
| 127 | goto error_leave;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[eb1a2f4] | 130 | rc = ddf_fun_bind(child);
|
---|
[3ae93a8] | 131 | if (rc != EOK) {
|
---|
| 132 | goto error_leave;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | return EOK;
|
---|
| 136 |
|
---|
| 137 | error_leave:
|
---|
| 138 | if (child != NULL) {
|
---|
| 139 | child->name = NULL;
|
---|
| 140 | /* This takes care of match_id deallocation as well. */
|
---|
[eb1a2f4] | 141 | ddf_fun_destroy(child);
|
---|
[3ae93a8] | 142 | }
|
---|
| 143 | if (child_name != NULL) {
|
---|
| 144 | free(child_name);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | return rc;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | * @}
|
---|
| 152 | */
|
---|