[ad97131] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 3 | * Copyright (c) 2013 Jan Vesely
|
---|
[e0a5d4c] | 4 | * Copyright (c) 2018 Petr Manek
|
---|
[ad97131] | 5 | * All rights reserved.
|
---|
| 6 | *
|
---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
---|
| 8 | * modification, are permitted provided that the following conditions
|
---|
| 9 | * are met:
|
---|
| 10 | *
|
---|
| 11 | * - Redistributions of source code must retain the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 15 | * documentation and/or other materials provided with the distribution.
|
---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
---|
| 17 | * derived from this software without specific prior written permission.
|
---|
| 18 | *
|
---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 29 | */
|
---|
| 30 | /** @addtogroup libusbdev
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | * USB device driver framework.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <usb/dev/driver.h>
|
---|
[c01987c] | 38 | #include <usb/dev/device.h>
|
---|
| 39 | #include <usb/debug.h>
|
---|
| 40 |
|
---|
| 41 | #include <assert.h>
|
---|
[ad97131] | 42 | #include <errno.h>
|
---|
| 43 | #include <str_error.h>
|
---|
[c01987c] | 44 | #include <ddf/driver.h>
|
---|
[ad97131] | 45 |
|
---|
| 46 | static const usb_driver_t *driver = NULL;
|
---|
| 47 |
|
---|
| 48 | /** Callback when a new device is supposed to be controlled by this driver.
|
---|
| 49 | *
|
---|
| 50 | * This callback is a wrapper for USB specific version of @c device_add.
|
---|
| 51 | *
|
---|
| 52 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 53 | * @return Error code.
|
---|
| 54 | */
|
---|
[5a6cc679] | 55 | static errno_t generic_device_add(ddf_dev_t *gen_dev)
|
---|
[ad97131] | 56 | {
|
---|
| 57 | assert(driver);
|
---|
| 58 | assert(driver->ops);
|
---|
| 59 | assert(driver->ops->device_add);
|
---|
| 60 |
|
---|
| 61 | /* Initialize generic USB driver data. */
|
---|
| 62 | const char *err_msg = NULL;
|
---|
[5a6cc679] | 63 | errno_t rc = usb_device_create_ddf(gen_dev, driver->endpoints, &err_msg);
|
---|
[ad97131] | 64 | if (rc != EOK) {
|
---|
[a1732929] | 65 | usb_log_error("USB device `%s' init failed (%s): %s.",
|
---|
[ad97131] | 66 | ddf_dev_get_name(gen_dev), err_msg, str_error(rc));
|
---|
| 67 | return rc;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /* Start USB driver specific initialization. */
|
---|
[fd9b3a67] | 71 | rc = driver->ops->device_add(ddf_dev_data_get(gen_dev));
|
---|
[ad97131] | 72 | if (rc != EOK)
|
---|
[fd9b3a67] | 73 | usb_device_destroy_ddf(gen_dev);
|
---|
[ad97131] | 74 | return rc;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /** Callback when a device is supposed to be removed from the system.
|
---|
| 78 | *
|
---|
| 79 | * This callback is a wrapper for USB specific version of @c device_remove.
|
---|
| 80 | *
|
---|
| 81 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 82 | * @return Error code.
|
---|
| 83 | */
|
---|
[5a6cc679] | 84 | static errno_t generic_device_remove(ddf_dev_t *gen_dev)
|
---|
[ad97131] | 85 | {
|
---|
| 86 | assert(driver);
|
---|
| 87 | assert(driver->ops);
|
---|
[c54b898] | 88 | if (driver->ops->device_remove == NULL)
|
---|
[ad97131] | 89 | return ENOTSUP;
|
---|
[c54b898] | 90 |
|
---|
[ad97131] | 91 | /* Just tell the driver to stop whatever it is doing */
|
---|
| 92 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
---|
[5a6cc679] | 93 | const errno_t ret = driver->ops->device_remove(usb_dev);
|
---|
[ad97131] | 94 | if (ret != EOK)
|
---|
| 95 | return ret;
|
---|
[c54b898] | 96 |
|
---|
[fd9b3a67] | 97 | usb_device_destroy_ddf(gen_dev);
|
---|
[ad97131] | 98 | return EOK;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /** Callback when a device was removed from the system.
|
---|
| 102 | *
|
---|
| 103 | * This callback is a wrapper for USB specific version of @c device_gone.
|
---|
| 104 | *
|
---|
| 105 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 106 | * @return Error code.
|
---|
| 107 | */
|
---|
[5a6cc679] | 108 | static errno_t generic_device_gone(ddf_dev_t *gen_dev)
|
---|
[ad97131] | 109 | {
|
---|
| 110 | assert(driver);
|
---|
| 111 | assert(driver->ops);
|
---|
| 112 | if (driver->ops->device_gone == NULL)
|
---|
| 113 | return ENOTSUP;
|
---|
| 114 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
---|
[5a6cc679] | 115 | const errno_t ret = driver->ops->device_gone(usb_dev);
|
---|
[ad97131] | 116 | if (ret == EOK)
|
---|
[fd9b3a67] | 117 | usb_device_destroy_ddf(gen_dev);
|
---|
[ad97131] | 118 |
|
---|
| 119 | return ret;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[d46ceb2b] | 122 | /** Callback when the driver is asked to online a specific function.
|
---|
| 123 | *
|
---|
| 124 | * This callback is a wrapper for USB specific version of @c fun_online.
|
---|
| 125 | *
|
---|
| 126 | * @param gen_dev Device function structure as prepared by DDF.
|
---|
| 127 | * @return Error code.
|
---|
| 128 | */
|
---|
| 129 | static int generic_function_online(ddf_fun_t *fun)
|
---|
| 130 | {
|
---|
| 131 | assert(driver);
|
---|
| 132 | assert(driver->ops);
|
---|
| 133 | if (driver->ops->function_online == NULL)
|
---|
| 134 | return ENOTSUP;
|
---|
| 135 | return driver->ops->function_online(fun);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /** Callback when the driver is asked to offline a specific function.
|
---|
| 139 | *
|
---|
| 140 | * This callback is a wrapper for USB specific version of @c fun_offline.
|
---|
| 141 | *
|
---|
| 142 | * @param gen_dev Device function structure as prepared by DDF.
|
---|
| 143 | * @return Error code.
|
---|
| 144 | */
|
---|
| 145 | static int generic_function_offline(ddf_fun_t *fun)
|
---|
| 146 | {
|
---|
| 147 | assert(driver);
|
---|
| 148 | assert(driver->ops);
|
---|
| 149 | if (driver->ops->function_offline == NULL)
|
---|
| 150 | return ENOTSUP;
|
---|
| 151 | return driver->ops->function_offline(fun);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[ad97131] | 154 | static driver_ops_t generic_driver_ops = {
|
---|
| 155 | .dev_add = generic_device_add,
|
---|
| 156 | .dev_remove = generic_device_remove,
|
---|
| 157 | .dev_gone = generic_device_gone,
|
---|
[d46ceb2b] | 158 | .fun_online = generic_function_online,
|
---|
| 159 | .fun_offline = generic_function_offline,
|
---|
[ad97131] | 160 | };
|
---|
| 161 | static driver_t generic_driver = {
|
---|
| 162 | .driver_ops = &generic_driver_ops
|
---|
| 163 | };
|
---|
| 164 |
|
---|
| 165 | /** Main routine of USB device driver.
|
---|
| 166 | *
|
---|
| 167 | * Under normal conditions, this function never returns.
|
---|
| 168 | *
|
---|
| 169 | * @param drv USB device driver structure.
|
---|
| 170 | * @return Task exit status.
|
---|
| 171 | */
|
---|
| 172 | int usb_driver_main(const usb_driver_t *drv)
|
---|
| 173 | {
|
---|
| 174 | assert(drv != NULL);
|
---|
| 175 |
|
---|
| 176 | /* Prepare the generic driver. */
|
---|
| 177 | generic_driver.name = drv->name;
|
---|
| 178 |
|
---|
| 179 | driver = drv;
|
---|
| 180 |
|
---|
| 181 | return ddf_driver_main(&generic_driver);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * @}
|
---|
| 186 | */
|
---|