[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 |
|
---|
[b68b279] | 69 | static usb_iface_t child_usb_iface = {
|
---|
| 70 | .get_hc_handle = usb_iface_get_hc_handle_hub_child_impl,
|
---|
[8ba18c6] | 71 | .get_address = usb_iface_get_address_impl,
|
---|
| 72 | .get_interface = usb_iface_get_interface_impl
|
---|
[b68b279] | 73 | };
|
---|
| 74 |
|
---|
| 75 |
|
---|
[eb1a2f4] | 76 | static ddf_dev_ops_t child_device_ops = {
|
---|
[b68b279] | 77 | .interfaces[USB_DEV_IFACE] = &child_usb_iface
|
---|
[3ae93a8] | 78 | };
|
---|
| 79 |
|
---|
[eb1a2f4] | 80 | static ddf_dev_ops_t mid_device_ops = {
|
---|
[b68b279] | 81 | .interfaces[USB_DEV_IFACE] = &usb_iface_hub_impl
|
---|
[3ae93a8] | 82 | };
|
---|
| 83 |
|
---|
| 84 | /** Create new USB multi interface device.
|
---|
| 85 | *
|
---|
| 86 | * @param dev Backing generic DDF device.
|
---|
| 87 | * @return New USB MID device.
|
---|
| 88 | * @retval NULL Error occured.
|
---|
| 89 | */
|
---|
[eb1a2f4] | 90 | usbmid_device_t *usbmid_device_create(ddf_dev_t *dev)
|
---|
[3ae93a8] | 91 | {
|
---|
| 92 | usbmid_device_t *mid = malloc(sizeof(usbmid_device_t));
|
---|
| 93 | if (mid == NULL) {
|
---|
[b68b279] | 94 | usb_log_error("Out of memory (wanted %zu bytes).\n",
|
---|
| 95 | sizeof(usbmid_device_t));
|
---|
[3ae93a8] | 96 | return NULL;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | int rc;
|
---|
| 100 | rc = usb_device_connection_initialize_from_device(&mid->wire, dev);
|
---|
| 101 | if (rc != EOK) {
|
---|
[b68b279] | 102 | usb_log_error("Failed to initialize `USB wire': %s.\n",
|
---|
| 103 | str_error(rc));
|
---|
[3ae93a8] | 104 | free(mid);
|
---|
| 105 | return NULL;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | rc = usb_endpoint_pipe_initialize_default_control(&mid->ctrl_pipe,
|
---|
| 109 | &mid->wire);
|
---|
| 110 | if (rc != EOK) {
|
---|
[b68b279] | 111 | usb_log_error("Failed to initialize control pipe: %s.\n",
|
---|
| 112 | str_error(rc));
|
---|
[3ae93a8] | 113 | free(mid);
|
---|
| 114 | return NULL;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | mid->dev = dev;
|
---|
[eb1a2f4] | 118 | (void) &mid_device_ops;
|
---|
[3ae93a8] | 119 |
|
---|
| 120 | return mid;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[8ba18c6] | 123 | /** Create new interface for USB MID device.
|
---|
| 124 | *
|
---|
| 125 | * @param dev Backing generic DDF child device (representing interface).
|
---|
| 126 | * @param iface_no Interface number.
|
---|
| 127 | * @return New interface.
|
---|
| 128 | * @retval NULL Error occured.
|
---|
| 129 | */
|
---|
[eb1a2f4] | 130 | usbmid_interface_t *usbmid_interface_create(ddf_fun_t *fun, int iface_no)
|
---|
[8ba18c6] | 131 | {
|
---|
| 132 | usbmid_interface_t *iface = malloc(sizeof(usbmid_interface_t));
|
---|
| 133 | if (iface == NULL) {
|
---|
| 134 | usb_log_error("Out of memory (wanted %zuB).\n",
|
---|
| 135 | sizeof(usbmid_interface_t));
|
---|
| 136 | return NULL;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[eb1a2f4] | 139 | iface->fun = fun;
|
---|
[8ba18c6] | 140 | iface->interface_no = iface_no;
|
---|
| 141 |
|
---|
| 142 | return iface;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[3ae93a8] | 145 |
|
---|
| 146 | /** Spawn new child device from one interface.
|
---|
| 147 | *
|
---|
| 148 | * @param parent Parent MID device.
|
---|
| 149 | * @param device_descriptor Device descriptor.
|
---|
| 150 | * @param interface_descriptor Interface descriptor.
|
---|
| 151 | * @return Error code.
|
---|
| 152 | */
|
---|
| 153 | int usbmid_spawn_interface_child(usbmid_device_t *parent,
|
---|
| 154 | const usb_standard_device_descriptor_t *device_descriptor,
|
---|
| 155 | const usb_standard_interface_descriptor_t *interface_descriptor)
|
---|
| 156 | {
|
---|
[eb1a2f4] | 157 | ddf_fun_t *child = NULL;
|
---|
[3ae93a8] | 158 | char *child_name = NULL;
|
---|
[8ba18c6] | 159 | usbmid_interface_t *child_as_interface = NULL;
|
---|
[3ae93a8] | 160 | int rc;
|
---|
| 161 |
|
---|
| 162 | /*
|
---|
| 163 | * Name is class name followed by interface number.
|
---|
| 164 | * The interface number shall provide uniqueness while the
|
---|
| 165 | * class name something humanly understandable.
|
---|
| 166 | */
|
---|
| 167 | rc = asprintf(&child_name, "%s%d",
|
---|
| 168 | usb_str_class(interface_descriptor->interface_class),
|
---|
| 169 | (int) interface_descriptor->interface_number);
|
---|
| 170 | if (rc < 0) {
|
---|
| 171 | goto error_leave;
|
---|
| 172 | }
|
---|
[8ba18c6] | 173 |
|
---|
[eb1a2f4] | 174 | /* Create the device. */
|
---|
| 175 | child = ddf_fun_create(parent->dev, fun_inner, child_name);
|
---|
| 176 | if (child == NULL) {
|
---|
| 177 | rc = ENOMEM;
|
---|
| 178 | goto error_leave;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 |
|
---|
[8ba18c6] | 183 | child_as_interface = usbmid_interface_create(child,
|
---|
| 184 | (int) interface_descriptor->interface_number);
|
---|
| 185 | if (child_as_interface == NULL) {
|
---|
| 186 | rc = ENOMEM;
|
---|
| 187 | goto error_leave;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | child->driver_data = child_as_interface;
|
---|
[b68b279] | 191 | child->ops = &child_device_ops;
|
---|
[3ae93a8] | 192 |
|
---|
[51f0e410] | 193 | rc = usb_device_create_match_ids_from_interface(device_descriptor,
|
---|
| 194 | interface_descriptor,
|
---|
[3ae93a8] | 195 | &child->match_ids);
|
---|
| 196 | if (rc != EOK) {
|
---|
| 197 | goto error_leave;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[eb1a2f4] | 200 | rc = ddf_fun_bind(child);
|
---|
[3ae93a8] | 201 | if (rc != EOK) {
|
---|
| 202 | goto error_leave;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | return EOK;
|
---|
| 206 |
|
---|
| 207 | error_leave:
|
---|
| 208 | if (child != NULL) {
|
---|
| 209 | child->name = NULL;
|
---|
| 210 | /* This takes care of match_id deallocation as well. */
|
---|
[eb1a2f4] | 211 | ddf_fun_destroy(child);
|
---|
[3ae93a8] | 212 | }
|
---|
| 213 | if (child_name != NULL) {
|
---|
| 214 | free(child_name);
|
---|
| 215 | }
|
---|
[8ba18c6] | 216 | if (child_as_interface != NULL) {
|
---|
| 217 | free(child_as_interface);
|
---|
| 218 | }
|
---|
[3ae93a8] | 219 |
|
---|
| 220 | return rc;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
| 224 | * @}
|
---|
| 225 | */
|
---|