| 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 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | * Helper functions.
|
|---|
| 36 | */
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <str_error.h>
|
|---|
| 39 | #include <stdlib.h>
|
|---|
| 40 | #include <usb_iface.h>
|
|---|
| 41 | #include <usb/ddfiface.h>
|
|---|
| 42 | #include <usb/dev/pipes.h>
|
|---|
| 43 | #include <usb/classes/classes.h>
|
|---|
| 44 | #include <usb/dev/recognise.h>
|
|---|
| 45 | #include "usbmid.h"
|
|---|
| 46 |
|
|---|
| 47 | /** Callback for DDF USB interface. */
|
|---|
| 48 | static int usb_iface_get_interface_impl(ddf_fun_t *fun, int *iface_no)
|
|---|
| 49 | {
|
|---|
| 50 | usbmid_interface_t *iface = ddf_fun_data_get(fun);
|
|---|
| 51 | assert(iface);
|
|---|
| 52 |
|
|---|
| 53 | if (iface_no != NULL) {
|
|---|
| 54 | *iface_no = iface->interface_no;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | return EOK;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /** DDF interface of the child - interface function. */
|
|---|
| 61 | static usb_iface_t child_usb_iface = {
|
|---|
| 62 | .get_hc_handle = usb_iface_get_hc_handle_device_impl,
|
|---|
| 63 | .get_my_address = usb_iface_get_my_address_forward_impl,
|
|---|
| 64 | .get_my_interface = usb_iface_get_interface_impl,
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | /** Operations for children - interface functions. */
|
|---|
| 68 | static ddf_dev_ops_t child_device_ops = {
|
|---|
| 69 | .interfaces[USB_DEV_IFACE] = &child_usb_iface
|
|---|
| 70 | };
|
|---|
| 71 |
|
|---|
| 72 | int usbmid_interface_destroy(usbmid_interface_t *mid_iface)
|
|---|
| 73 | {
|
|---|
| 74 | assert(mid_iface);
|
|---|
| 75 | assert_link_not_used(&mid_iface->link);
|
|---|
| 76 | const int ret = ddf_fun_unbind(mid_iface->fun);
|
|---|
| 77 | if (ret != EOK) {
|
|---|
| 78 | return ret;
|
|---|
| 79 | }
|
|---|
| 80 | ddf_fun_destroy(mid_iface->fun);
|
|---|
| 81 | return EOK;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /** Spawn new child device from one interface.
|
|---|
| 85 | *
|
|---|
| 86 | * @param parent Parent MID device.
|
|---|
| 87 | * @param iface Interface information.
|
|---|
| 88 | * @param device_descriptor Device descriptor.
|
|---|
| 89 | * @param interface_descriptor Interface descriptor.
|
|---|
| 90 | * @return Error code.
|
|---|
| 91 | */
|
|---|
| 92 | int usbmid_spawn_interface_child(usb_device_t *parent,
|
|---|
| 93 | usbmid_interface_t **iface_ret,
|
|---|
| 94 | const usb_standard_device_descriptor_t *device_descriptor,
|
|---|
| 95 | const usb_standard_interface_descriptor_t *interface_descriptor)
|
|---|
| 96 | {
|
|---|
| 97 | ddf_fun_t *child = NULL;
|
|---|
| 98 | char *child_name = NULL;
|
|---|
| 99 | int rc;
|
|---|
| 100 |
|
|---|
| 101 | /*
|
|---|
| 102 | * Name is class name followed by interface number.
|
|---|
| 103 | * The interface number shall provide uniqueness while the
|
|---|
| 104 | * class name something humanly understandable.
|
|---|
| 105 | */
|
|---|
| 106 | rc = asprintf(&child_name, "%s%hhu",
|
|---|
| 107 | usb_str_class(interface_descriptor->interface_class),
|
|---|
| 108 | interface_descriptor->interface_number);
|
|---|
| 109 | if (rc < 0) {
|
|---|
| 110 | return ENOMEM;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | /* Create the device. */
|
|---|
| 114 | child = ddf_fun_create(parent->ddf_dev, fun_inner, child_name);
|
|---|
| 115 | free(child_name);
|
|---|
| 116 | if (child == NULL) {
|
|---|
| 117 | return ENOMEM;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | match_id_list_t match_ids;
|
|---|
| 121 | init_match_ids(&match_ids);
|
|---|
| 122 |
|
|---|
| 123 | rc = usb_device_create_match_ids_from_interface(device_descriptor,
|
|---|
| 124 | interface_descriptor, &match_ids);
|
|---|
| 125 | if (rc != EOK) {
|
|---|
| 126 | ddf_fun_destroy(child);
|
|---|
| 127 | return rc;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | list_foreach(match_ids.ids, link) {
|
|---|
| 131 | match_id_t *match_id = list_get_instance(link, match_id_t, link);
|
|---|
| 132 | rc = ddf_fun_add_match_id(child, match_id->id, match_id->score);
|
|---|
| 133 | if (rc != EOK) {
|
|---|
| 134 | clean_match_ids(&match_ids);
|
|---|
| 135 | ddf_fun_destroy(child);
|
|---|
| 136 | return rc;
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | clean_match_ids(&match_ids);
|
|---|
| 140 | ddf_fun_set_ops(child, &child_device_ops);
|
|---|
| 141 |
|
|---|
| 142 | usbmid_interface_t *iface = ddf_fun_data_alloc(child, sizeof(*iface));
|
|---|
| 143 |
|
|---|
| 144 | iface->fun = child;
|
|---|
| 145 | iface->interface_no = interface_descriptor->interface_number;
|
|---|
| 146 | link_initialize(&iface->link);
|
|---|
| 147 |
|
|---|
| 148 | rc = ddf_fun_bind(child);
|
|---|
| 149 | if (rc != EOK) {
|
|---|
| 150 | /* This takes care of match_id deallocation as well. */
|
|---|
| 151 | ddf_fun_destroy(child);
|
|---|
| 152 | return rc;
|
|---|
| 153 | }
|
|---|
| 154 | *iface_ret = iface;
|
|---|
| 155 |
|
|---|
| 156 | return EOK;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * @}
|
|---|
| 161 | */
|
|---|