[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>
|
---|
[7d521e24] | 41 | #include <usb/dev/pipes.h>
|
---|
[3ae93a8] | 42 | #include <usb/classes/classes.h>
|
---|
[7d521e24] | 43 | #include <usb/dev/recognise.h>
|
---|
[3ae93a8] | 44 | #include "usbmid.h"
|
---|
| 45 |
|
---|
[8ba18c6] | 46 | /** Callback for DDF USB interface. */
|
---|
[eb1a2f4] | 47 | static int usb_iface_get_interface_impl(ddf_fun_t *fun, devman_handle_t handle,
|
---|
[8ba18c6] | 48 | int *iface_no)
|
---|
| 49 | {
|
---|
[eb1a2f4] | 50 | assert(fun);
|
---|
[8ba18c6] | 51 |
|
---|
[eb1a2f4] | 52 | usbmid_interface_t *iface = fun->driver_data;
|
---|
[8ba18c6] | 53 | assert(iface);
|
---|
| 54 |
|
---|
| 55 | if (iface_no != NULL) {
|
---|
| 56 | *iface_no = iface->interface_no;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | return EOK;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[a6add7a] | 62 | /** DDF interface of the child - interface function. */
|
---|
[b68b279] | 63 | static usb_iface_t child_usb_iface = {
|
---|
| 64 | .get_hc_handle = usb_iface_get_hc_handle_hub_child_impl,
|
---|
[d8e698e7] | 65 | .get_address = usb_iface_get_address_hub_impl,
|
---|
[8ba18c6] | 66 | .get_interface = usb_iface_get_interface_impl
|
---|
[b68b279] | 67 | };
|
---|
| 68 |
|
---|
[a6add7a] | 69 | /** Operations for children - interface functions. */
|
---|
[eb1a2f4] | 70 | static ddf_dev_ops_t child_device_ops = {
|
---|
[b68b279] | 71 | .interfaces[USB_DEV_IFACE] = &child_usb_iface
|
---|
[3ae93a8] | 72 | };
|
---|
| 73 |
|
---|
[38e68ab] | 74 | int usbmid_interface_destroy(usbmid_interface_t *mid_iface)
|
---|
| 75 | {
|
---|
| 76 | assert(mid_iface);
|
---|
| 77 | assert_link_not_used(&mid_iface->link);
|
---|
| 78 | const int ret = ddf_fun_unbind(mid_iface->fun);
|
---|
| 79 | if (ret != EOK) {
|
---|
| 80 | return ret;
|
---|
| 81 | }
|
---|
[a92ce4ef] | 82 | /* NOTE: usbmid->interface points somewhere, but we did not
|
---|
| 83 | * allocate that space, so don't touch */
|
---|
[38e68ab] | 84 | ddf_fun_destroy(mid_iface->fun);
|
---|
[a92ce4ef] | 85 | /* NOTE: mid_iface is invalid at this point, it was assigned to
|
---|
| 86 | * mid_iface->fun->driver_data and freed in ddf_fun_destroy */
|
---|
[38e68ab] | 87 | return EOK;
|
---|
| 88 | }
|
---|
[3ae93a8] | 89 |
|
---|
| 90 | /** Spawn new child device from one interface.
|
---|
| 91 | *
|
---|
| 92 | * @param parent Parent MID device.
|
---|
[ecb107b] | 93 | * @param iface Interface information.
|
---|
[3ae93a8] | 94 | * @param device_descriptor Device descriptor.
|
---|
| 95 | * @param interface_descriptor Interface descriptor.
|
---|
| 96 | * @return Error code.
|
---|
| 97 | */
|
---|
[fcafa04] | 98 | int usbmid_spawn_interface_child(usb_device_t *parent,
|
---|
[ecb107b] | 99 | usbmid_interface_t *iface,
|
---|
[3ae93a8] | 100 | const usb_standard_device_descriptor_t *device_descriptor,
|
---|
| 101 | const usb_standard_interface_descriptor_t *interface_descriptor)
|
---|
| 102 | {
|
---|
[eb1a2f4] | 103 | ddf_fun_t *child = NULL;
|
---|
[3ae93a8] | 104 | char *child_name = NULL;
|
---|
| 105 | int rc;
|
---|
| 106 |
|
---|
| 107 | /*
|
---|
| 108 | * Name is class name followed by interface number.
|
---|
| 109 | * The interface number shall provide uniqueness while the
|
---|
| 110 | * class name something humanly understandable.
|
---|
| 111 | */
|
---|
| 112 | rc = asprintf(&child_name, "%s%d",
|
---|
| 113 | usb_str_class(interface_descriptor->interface_class),
|
---|
| 114 | (int) interface_descriptor->interface_number);
|
---|
| 115 | if (rc < 0) {
|
---|
[359d96f] | 116 | return ENOMEM;
|
---|
[3ae93a8] | 117 | }
|
---|
[8ba18c6] | 118 |
|
---|
[eb1a2f4] | 119 | /* Create the device. */
|
---|
[fcafa04] | 120 | child = ddf_fun_create(parent->ddf_dev, fun_inner, child_name);
|
---|
[359d96f] | 121 | free(child_name);
|
---|
[eb1a2f4] | 122 | if (child == NULL) {
|
---|
[359d96f] | 123 | return ENOMEM;
|
---|
[eb1a2f4] | 124 | }
|
---|
| 125 |
|
---|
[ecb107b] | 126 | iface->fun = child;
|
---|
[eb1a2f4] | 127 |
|
---|
[ecb107b] | 128 | child->driver_data = iface;
|
---|
[b68b279] | 129 | child->ops = &child_device_ops;
|
---|
[3ae93a8] | 130 |
|
---|
[51f0e410] | 131 | rc = usb_device_create_match_ids_from_interface(device_descriptor,
|
---|
[359d96f] | 132 | interface_descriptor, &child->match_ids);
|
---|
[3ae93a8] | 133 | if (rc != EOK) {
|
---|
[359d96f] | 134 | ddf_fun_destroy(child);
|
---|
| 135 | return rc;
|
---|
[3ae93a8] | 136 | }
|
---|
| 137 |
|
---|
[eb1a2f4] | 138 | rc = ddf_fun_bind(child);
|
---|
[3ae93a8] | 139 | if (rc != EOK) {
|
---|
| 140 | /* This takes care of match_id deallocation as well. */
|
---|
[eb1a2f4] | 141 | ddf_fun_destroy(child);
|
---|
[359d96f] | 142 | return rc;
|
---|
[3ae93a8] | 143 | }
|
---|
| 144 |
|
---|
[359d96f] | 145 | return EOK;
|
---|
[3ae93a8] | 146 | }
|
---|
| 147 |
|
---|
| 148 | /**
|
---|
| 149 | * @}
|
---|
| 150 | */
|
---|