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