[ad97131] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
---|
| 3 | * Copyright (c) 2013 Jan Vesely
|
---|
| 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 | /** @addtogroup libusbdev
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * USB device driver framework.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <usb/dev/driver.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <str_error.h>
|
---|
| 39 | #include <usb/debug.h>
|
---|
| 40 |
|
---|
| 41 | static const usb_driver_t *driver = NULL;
|
---|
| 42 |
|
---|
| 43 | /** Callback when a new device is supposed to be controlled by this driver.
|
---|
| 44 | *
|
---|
| 45 | * This callback is a wrapper for USB specific version of @c device_add.
|
---|
| 46 | *
|
---|
| 47 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 48 | * @return Error code.
|
---|
| 49 | */
|
---|
| 50 | static int generic_device_add(ddf_dev_t *gen_dev)
|
---|
| 51 | {
|
---|
| 52 | assert(driver);
|
---|
| 53 | assert(driver->ops);
|
---|
| 54 | assert(driver->ops->device_add);
|
---|
| 55 |
|
---|
| 56 | /* Get place for driver data. */
|
---|
| 57 | usb_device_t *dev = ddf_dev_data_alloc(gen_dev, sizeof(usb_device_t));
|
---|
| 58 | if (dev == NULL) {
|
---|
| 59 | usb_log_error("USB device `%s' structure allocation failed.\n",
|
---|
| 60 | ddf_dev_get_name(gen_dev));
|
---|
| 61 | return ENOMEM;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | /* Initialize generic USB driver data. */
|
---|
| 65 | const char *err_msg = NULL;
|
---|
| 66 | int rc = usb_device_init(dev, gen_dev, driver->endpoints, &err_msg);
|
---|
| 67 | if (rc != EOK) {
|
---|
| 68 | usb_log_error("USB device `%s' init failed (%s): %s.\n",
|
---|
| 69 | ddf_dev_get_name(gen_dev), err_msg, str_error(rc));
|
---|
| 70 | return rc;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /* Start USB driver specific initialization. */
|
---|
| 74 | rc = driver->ops->device_add(dev);
|
---|
| 75 | if (rc != EOK)
|
---|
| 76 | usb_device_deinit(dev);
|
---|
| 77 | return rc;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | /** Callback when a device is supposed to be removed from the system.
|
---|
| 81 | *
|
---|
| 82 | * This callback is a wrapper for USB specific version of @c device_remove.
|
---|
| 83 | *
|
---|
| 84 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 85 | * @return Error code.
|
---|
| 86 | */
|
---|
| 87 | static int generic_device_remove(ddf_dev_t *gen_dev)
|
---|
| 88 | {
|
---|
| 89 | assert(driver);
|
---|
| 90 | assert(driver->ops);
|
---|
| 91 | if (driver->ops->device_rem == NULL)
|
---|
| 92 | return ENOTSUP;
|
---|
| 93 | /* Just tell the driver to stop whatever it is doing */
|
---|
| 94 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
---|
| 95 | const int ret = driver->ops->device_rem(usb_dev);
|
---|
| 96 | if (ret != EOK)
|
---|
| 97 | return ret;
|
---|
| 98 | usb_device_deinit(usb_dev);
|
---|
| 99 | return EOK;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /** Callback when a device was removed from the system.
|
---|
| 103 | *
|
---|
| 104 | * This callback is a wrapper for USB specific version of @c device_gone.
|
---|
| 105 | *
|
---|
| 106 | * @param gen_dev Device structure as prepared by DDF.
|
---|
| 107 | * @return Error code.
|
---|
| 108 | */
|
---|
| 109 | static int generic_device_gone(ddf_dev_t *gen_dev)
|
---|
| 110 | {
|
---|
| 111 | assert(driver);
|
---|
| 112 | assert(driver->ops);
|
---|
| 113 | if (driver->ops->device_gone == NULL)
|
---|
| 114 | return ENOTSUP;
|
---|
| 115 | usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
|
---|
| 116 | const int ret = driver->ops->device_gone(usb_dev);
|
---|
| 117 | if (ret == EOK)
|
---|
| 118 | usb_device_deinit(usb_dev);
|
---|
| 119 |
|
---|
| 120 | return ret;
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | static driver_ops_t generic_driver_ops = {
|
---|
| 124 | .dev_add = generic_device_add,
|
---|
| 125 | .dev_remove = generic_device_remove,
|
---|
| 126 | .dev_gone = generic_device_gone,
|
---|
| 127 | };
|
---|
| 128 | static driver_t generic_driver = {
|
---|
| 129 | .driver_ops = &generic_driver_ops
|
---|
| 130 | };
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | /** Main routine of USB device driver.
|
---|
| 134 | *
|
---|
| 135 | * Under normal conditions, this function never returns.
|
---|
| 136 | *
|
---|
| 137 | * @param drv USB device driver structure.
|
---|
| 138 | * @return Task exit status.
|
---|
| 139 | */
|
---|
| 140 | int usb_driver_main(const usb_driver_t *drv)
|
---|
| 141 | {
|
---|
| 142 | assert(drv != NULL);
|
---|
| 143 |
|
---|
| 144 | /* Prepare the generic driver. */
|
---|
| 145 | generic_driver.name = drv->name;
|
---|
| 146 |
|
---|
| 147 | driver = drv;
|
---|
| 148 |
|
---|
| 149 | return ddf_driver_main(&generic_driver);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
| 153 | * @}
|
---|
| 154 | */
|
---|