[3ae93a8] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
[e0a5d4c] | 3 | * Copyright (c) 2018 Petr Manek, 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 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | * Main routines of USB multi interface device driver.
|
---|
| 36 | */
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <str_error.h>
|
---|
| 39 | #include <usb/debug.h>
|
---|
| 40 | #include <usb/classes/classes.h>
|
---|
[7d521e24] | 41 | #include <usb/dev/request.h>
|
---|
[3ae93a8] | 42 | #include <usb/descriptor.h>
|
---|
[7d521e24] | 43 | #include <usb/dev/pipes.h>
|
---|
[3ae93a8] | 44 |
|
---|
| 45 | #include "usbmid.h"
|
---|
| 46 |
|
---|
[a6add7a] | 47 | /** Callback when new MID device is attached to the host.
|
---|
| 48 | *
|
---|
| 49 | * @param gen_dev Generic DDF device representing the new device.
|
---|
| 50 | * @return Error code.
|
---|
| 51 | */
|
---|
[5a6cc679] | 52 | static errno_t usbmid_device_add(usb_device_t *dev)
|
---|
[3ae93a8] | 53 | {
|
---|
[a1732929] | 54 | usb_log_info("Taking care of new MID `%s'.", usb_device_get_name(dev));
|
---|
[3ae93a8] | 55 |
|
---|
[34289ea] | 56 | return usbmid_explore_device(dev);
|
---|
[3ae93a8] | 57 | }
|
---|
[76fbd9a] | 58 |
|
---|
[a821f05] | 59 | static errno_t destroy_interfaces(usb_mid_t *usb_mid)
|
---|
[91173333] | 60 | {
|
---|
[a821f05] | 61 | errno_t ret = EOK;
|
---|
[91173333] | 62 |
|
---|
| 63 | while (!list_empty(&usb_mid->interface_list)) {
|
---|
| 64 | link_t *item = list_first(&usb_mid->interface_list);
|
---|
| 65 | list_remove(item);
|
---|
| 66 |
|
---|
| 67 | usbmid_interface_t *iface = usbmid_interface_from_link(item);
|
---|
| 68 |
|
---|
[a821f05] | 69 | const errno_t pret = usbmid_interface_destroy(iface);
|
---|
[91173333] | 70 | if (pret != EOK) {
|
---|
[a1732929] | 71 | usb_log_error("Failed to remove child `%s': %s",
|
---|
[91173333] | 72 | ddf_fun_get_name(iface->fun), str_error(pret));
|
---|
| 73 | ret = pret;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return ret;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[aa05b29] | 80 | /** Callback when a MID device is about to be removed from the host.
|
---|
| 81 | *
|
---|
[5153b58] | 82 | * @param dev USB device representing the removed device.
|
---|
[aa05b29] | 83 | * @return Error code.
|
---|
| 84 | */
|
---|
[5a6cc679] | 85 | static errno_t usbmid_device_remove(usb_device_t *dev)
|
---|
[aa05b29] | 86 | {
|
---|
| 87 | assert(dev);
|
---|
[c39e9fb] | 88 | usb_mid_t *usb_mid = usb_device_data_get(dev);
|
---|
[5153b58] | 89 | assert(usb_mid);
|
---|
[8be7819] | 90 |
|
---|
| 91 | /* Remove ctl function */
|
---|
[5a6cc679] | 92 | errno_t ret = ddf_fun_unbind(usb_mid->ctl_fun);
|
---|
[8be7819] | 93 | if (ret != EOK) {
|
---|
[a1732929] | 94 | usb_log_error("Failed to unbind USB MID ctl function: %s.",
|
---|
[8be7819] | 95 | str_error(ret));
|
---|
| 96 | return ret;
|
---|
| 97 | }
|
---|
| 98 | ddf_fun_destroy(usb_mid->ctl_fun);
|
---|
| 99 |
|
---|
| 100 | /* Remove all children */
|
---|
[5823525e] | 101 | list_foreach(usb_mid->interface_list, link, usbmid_interface_t, iface) {
|
---|
[a1732929] | 102 | usb_log_info("Removing child `%s'.",
|
---|
[68e1023] | 103 | ddf_fun_get_name(iface->fun));
|
---|
[8be7819] | 104 |
|
---|
[5823525e] | 105 | /* Tell the child to go offline. */
|
---|
[5a6cc679] | 106 | errno_t pret = ddf_fun_offline(iface->fun);
|
---|
[8be7819] | 107 | if (pret != EOK) {
|
---|
[a1732929] | 108 | usb_log_warning("Failed to turn off child `%s': %s",
|
---|
[68e1023] | 109 | ddf_fun_get_name(iface->fun), str_error(pret));
|
---|
[8be7819] | 110 | }
|
---|
[5823525e] | 111 | }
|
---|
[8be7819] | 112 |
|
---|
[5823525e] | 113 | return destroy_interfaces(usb_mid);
|
---|
[f75725d] | 114 | }
|
---|
| 115 |
|
---|
[aa05b29] | 116 | /** Callback when a MID device was removed from the host.
|
---|
| 117 | *
|
---|
[5153b58] | 118 | * @param dev USB device representing the removed device.
|
---|
[aa05b29] | 119 | * @return Error code.
|
---|
| 120 | */
|
---|
[5a6cc679] | 121 | static errno_t usbmid_device_gone(usb_device_t *dev)
|
---|
[38e68ab] | 122 | {
|
---|
| 123 | assert(dev);
|
---|
[c39e9fb] | 124 | usb_mid_t *usb_mid = usb_device_data_get(dev);
|
---|
[5153b58] | 125 | assert(usb_mid);
|
---|
| 126 |
|
---|
[a1732929] | 127 | usb_log_info("USB MID gone: `%s'.", usb_device_get_name(dev));
|
---|
[38e68ab] | 128 |
|
---|
| 129 | /* Remove ctl function */
|
---|
[5a6cc679] | 130 | errno_t ret = ddf_fun_unbind(usb_mid->ctl_fun);
|
---|
[38e68ab] | 131 | if (ret != EOK) {
|
---|
[a1732929] | 132 | usb_log_error("Failed to unbind USB MID ctl function: %s.",
|
---|
[38e68ab] | 133 | str_error(ret));
|
---|
| 134 | return ret;
|
---|
| 135 | }
|
---|
| 136 | ddf_fun_destroy(usb_mid->ctl_fun);
|
---|
| 137 |
|
---|
[5823525e] | 138 | /* Destroy children and tell their drivers they are gone. */
|
---|
| 139 | return destroy_interfaces(usb_mid);
|
---|
[38e68ab] | 140 | }
|
---|
| 141 |
|
---|
[a821f05] | 142 | static errno_t usbmid_function_online(ddf_fun_t *fun)
|
---|
[d46ceb2b] | 143 | {
|
---|
[5ff5dc5] | 144 | usb_device_t *usb_dev = ddf_dev_data_get(ddf_fun_get_dev(fun));
|
---|
| 145 | usb_mid_t *usb_mid = usb_device_data_get(usb_dev);
|
---|
| 146 | if (fun == usb_mid->ctl_fun)
|
---|
| 147 | return ENOTSUP;
|
---|
| 148 |
|
---|
[d46ceb2b] | 149 | return ddf_fun_online(fun);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[5a6cc679] | 152 | static errno_t usbmid_function_offline(ddf_fun_t *fun)
|
---|
[d46ceb2b] | 153 | {
|
---|
[5ff5dc5] | 154 | usb_device_t *usb_dev = ddf_dev_data_get(ddf_fun_get_dev(fun));
|
---|
| 155 | usb_mid_t *usb_mid = usb_device_data_get(usb_dev);
|
---|
| 156 | if (fun == usb_mid->ctl_fun)
|
---|
| 157 | return ENOTSUP;
|
---|
| 158 |
|
---|
[d46ceb2b] | 159 | return ddf_fun_offline(fun);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[a6add7a] | 162 | /** USB MID driver ops. */
|
---|
[aa05b29] | 163 | static const usb_driver_ops_t mid_driver_ops = {
|
---|
[1a4ea01d] | 164 | .device_add = usbmid_device_add,
|
---|
[c54b898] | 165 | .device_remove = usbmid_device_remove,
|
---|
[38e68ab] | 166 | .device_gone = usbmid_device_gone,
|
---|
[d46ceb2b] | 167 | .function_online = usbmid_function_online,
|
---|
| 168 | .function_offline = usbmid_function_offline
|
---|
[3ae93a8] | 169 | };
|
---|
| 170 |
|
---|
[a6add7a] | 171 | /** USB MID driver. */
|
---|
[aa05b29] | 172 | static const usb_driver_t mid_driver = {
|
---|
[3ae93a8] | 173 | .name = NAME,
|
---|
[fcafa04] | 174 | .ops = &mid_driver_ops,
|
---|
| 175 | .endpoints = NULL
|
---|
[3ae93a8] | 176 | };
|
---|
| 177 |
|
---|
| 178 | int main(int argc, char *argv[])
|
---|
| 179 | {
|
---|
| 180 | printf(NAME ": USB multi interface device driver.\n");
|
---|
| 181 |
|
---|
[920d0fc] | 182 | log_init(NAME);
|
---|
[fcafa04] | 183 |
|
---|
| 184 | return usb_driver_main(&mid_driver);
|
---|
[3ae93a8] | 185 | }
|
---|
| 186 |
|
---|
| 187 | /**
|
---|
| 188 | * @}
|
---|
| 189 | */
|
---|