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