[3ae93a8] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Ondrej Hlavaty
|
---|
[3ae93a8] | 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 drvusbmid
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
[56fd7cf] | 33 |
|
---|
[3ae93a8] | 34 | /**
|
---|
| 35 | * @file
|
---|
| 36 | * Helper functions.
|
---|
| 37 | */
|
---|
[58563585] | 38 |
|
---|
[3ae93a8] | 39 | #include <errno.h>
|
---|
| 40 | #include <str_error.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 | #include <usb_iface.h>
|
---|
[7d521e24] | 43 | #include <usb/dev/pipes.h>
|
---|
[3ae93a8] | 44 | #include <usb/classes/classes.h>
|
---|
[7d521e24] | 45 | #include <usb/dev/recognise.h>
|
---|
[3ae93a8] | 46 | #include "usbmid.h"
|
---|
[6fe7683] | 47 |
|
---|
[c280d7e] | 48 | /**
|
---|
| 49 | * Get USB device description by calling HC and altering the interface field.
|
---|
[4ca778b] | 50 | *
|
---|
| 51 | * @param[in] fun Device function the operation is running on.
|
---|
[c280d7e] | 52 | * @param[out] desc Device descriptor.
|
---|
[4ca778b] | 53 | * @return Error code.
|
---|
| 54 | */
|
---|
[5a6cc679] | 55 | static errno_t usb_iface_description(ddf_fun_t *fun, usb_device_desc_t *desc)
|
---|
[8ba18c6] | 56 | {
|
---|
[56fd7cf] | 57 | usbmid_interface_t *iface = ddf_fun_data_get(fun);
|
---|
[8ba18c6] | 58 | assert(iface);
|
---|
[c280d7e] | 59 | usb_device_t *usb_dev = ddf_dev_data_get(ddf_fun_get_dev(fun));
|
---|
| 60 | assert(usb_dev);
|
---|
| 61 |
|
---|
| 62 | async_exch_t *exch = usb_device_bus_exchange_begin(usb_dev);
|
---|
| 63 | if (!exch)
|
---|
| 64 | return EPARTY;
|
---|
| 65 |
|
---|
| 66 | usb_device_desc_t tmp_desc;
|
---|
[5a6cc679] | 67 | const errno_t ret = usb_get_my_description(exch, &tmp_desc);
|
---|
[8ba18c6] | 68 |
|
---|
[c280d7e] | 69 | if (ret == EOK && desc) {
|
---|
| 70 | *desc = tmp_desc;
|
---|
| 71 | desc->iface = iface->interface_no;
|
---|
| 72 | }
|
---|
[8ba18c6] | 73 |
|
---|
[4c3fef4] | 74 | usb_device_bus_exchange_end(exch);
|
---|
| 75 |
|
---|
[8ba18c6] | 76 | return EOK;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[6fe7683] | 79 | /** DDF interface of the child - USB functions. */
|
---|
[b68b279] | 80 | static usb_iface_t child_usb_iface = {
|
---|
[c280d7e] | 81 | .get_my_description = usb_iface_description,
|
---|
[b68b279] | 82 | };
|
---|
| 83 |
|
---|
[a6add7a] | 84 | /** Operations for children - interface functions. */
|
---|
[eb1a2f4] | 85 | static ddf_dev_ops_t child_device_ops = {
|
---|
[b68b279] | 86 | .interfaces[USB_DEV_IFACE] = &child_usb_iface
|
---|
[3ae93a8] | 87 | };
|
---|
| 88 |
|
---|
[5a6cc679] | 89 | errno_t usbmid_interface_destroy(usbmid_interface_t *mid_iface)
|
---|
[38e68ab] | 90 | {
|
---|
| 91 | assert(mid_iface);
|
---|
| 92 | assert_link_not_used(&mid_iface->link);
|
---|
[5a6cc679] | 93 | const errno_t ret = ddf_fun_unbind(mid_iface->fun);
|
---|
[38e68ab] | 94 | if (ret != EOK) {
|
---|
| 95 | return ret;
|
---|
| 96 | }
|
---|
| 97 | ddf_fun_destroy(mid_iface->fun);
|
---|
| 98 | return EOK;
|
---|
| 99 | }
|
---|
[3ae93a8] | 100 |
|
---|
| 101 | /** Spawn new child device from one interface.
|
---|
| 102 | *
|
---|
| 103 | * @param parent Parent MID device.
|
---|
[ecb107b] | 104 | * @param iface Interface information.
|
---|
[3ae93a8] | 105 | * @param device_descriptor Device descriptor.
|
---|
| 106 | * @param interface_descriptor Interface descriptor.
|
---|
| 107 | * @return Error code.
|
---|
| 108 | */
|
---|
[5a6cc679] | 109 | errno_t usbmid_spawn_interface_child(usb_device_t *parent,
|
---|
[9e2132a] | 110 | usbmid_interface_t **iface_ret,
|
---|
[3ae93a8] | 111 | const usb_standard_device_descriptor_t *device_descriptor,
|
---|
| 112 | const usb_standard_interface_descriptor_t *interface_descriptor)
|
---|
| 113 | {
|
---|
[eb1a2f4] | 114 | ddf_fun_t *child = NULL;
|
---|
[3ae93a8] | 115 | char *child_name = NULL;
|
---|
[5a6cc679] | 116 | errno_t rc;
|
---|
[3ae93a8] | 117 |
|
---|
| 118 | /*
|
---|
| 119 | * Name is class name followed by interface number.
|
---|
| 120 | * The interface number shall provide uniqueness while the
|
---|
| 121 | * class name something humanly understandable.
|
---|
| 122 | */
|
---|
[a821f05] | 123 | errno_t ret = asprintf(&child_name, "%s%hhu",
|
---|
[3ae93a8] | 124 | usb_str_class(interface_descriptor->interface_class),
|
---|
[8be7819] | 125 | interface_descriptor->interface_number);
|
---|
[d5c1051] | 126 | if (ret < 0) {
|
---|
[359d96f] | 127 | return ENOMEM;
|
---|
[3ae93a8] | 128 | }
|
---|
[8ba18c6] | 129 |
|
---|
[eb1a2f4] | 130 | /* Create the device. */
|
---|
[6785b538] | 131 | child = usb_device_ddf_fun_create(parent, fun_inner, child_name);
|
---|
[359d96f] | 132 | free(child_name);
|
---|
[eb1a2f4] | 133 | if (child == NULL) {
|
---|
[359d96f] | 134 | return ENOMEM;
|
---|
[eb1a2f4] | 135 | }
|
---|
| 136 |
|
---|
[56fd7cf] | 137 | match_id_list_t match_ids;
|
---|
| 138 | init_match_ids(&match_ids);
|
---|
| 139 |
|
---|
[51f0e410] | 140 | rc = usb_device_create_match_ids_from_interface(device_descriptor,
|
---|
[56fd7cf] | 141 | interface_descriptor, &match_ids);
|
---|
[3ae93a8] | 142 | if (rc != EOK) {
|
---|
[359d96f] | 143 | ddf_fun_destroy(child);
|
---|
| 144 | return rc;
|
---|
[3ae93a8] | 145 | }
|
---|
| 146 |
|
---|
[feeac0d] | 147 | list_foreach(match_ids.ids, link, match_id_t, match_id) {
|
---|
[56fd7cf] | 148 | rc = ddf_fun_add_match_id(child, match_id->id, match_id->score);
|
---|
| 149 | if (rc != EOK) {
|
---|
| 150 | clean_match_ids(&match_ids);
|
---|
| 151 | ddf_fun_destroy(child);
|
---|
| 152 | return rc;
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 | clean_match_ids(&match_ids);
|
---|
[9e2132a] | 156 | ddf_fun_set_ops(child, &child_device_ops);
|
---|
| 157 |
|
---|
| 158 | usbmid_interface_t *iface = ddf_fun_data_alloc(child, sizeof(*iface));
|
---|
| 159 |
|
---|
| 160 | iface->fun = child;
|
---|
| 161 | iface->interface_no = interface_descriptor->interface_number;
|
---|
| 162 | link_initialize(&iface->link);
|
---|
[56fd7cf] | 163 |
|
---|
[eb1a2f4] | 164 | rc = ddf_fun_bind(child);
|
---|
[3ae93a8] | 165 | if (rc != EOK) {
|
---|
| 166 | /* This takes care of match_id deallocation as well. */
|
---|
[eb1a2f4] | 167 | ddf_fun_destroy(child);
|
---|
[359d96f] | 168 | return rc;
|
---|
[3ae93a8] | 169 | }
|
---|
[9e2132a] | 170 | *iface_ret = iface;
|
---|
[5153b58] | 171 |
|
---|
[359d96f] | 172 | return EOK;
|
---|
[3ae93a8] | 173 | }
|
---|
| 174 |
|
---|
| 175 | /**
|
---|
| 176 | * @}
|
---|
| 177 | */
|
---|