source: mainline/uspace/lib/usbdev/src/driver.c@ 8582076

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8582076 was fd9b3a67, checked in by Jan Vesely <jano.vesely@…>, 12 years ago

libusbdev: Make usb_device_t opaque.

  • Property mode set to 100644
File size: 4.2 KB
Line 
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
41static 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 */
50static 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 /* Initialize generic USB driver data. */
57 const char *err_msg = NULL;
58 int rc = usb_device_create_ddf(gen_dev, driver->endpoints, &err_msg);
59 if (rc != EOK) {
60 usb_log_error("USB device `%s' init failed (%s): %s.\n",
61 ddf_dev_get_name(gen_dev), err_msg, str_error(rc));
62 return rc;
63 }
64
65 /* Start USB driver specific initialization. */
66 rc = driver->ops->device_add(ddf_dev_data_get(gen_dev));
67 if (rc != EOK)
68 usb_device_destroy_ddf(gen_dev);
69 return rc;
70}
71
72/** Callback when a device is supposed to be removed from the system.
73 *
74 * This callback is a wrapper for USB specific version of @c device_remove.
75 *
76 * @param gen_dev Device structure as prepared by DDF.
77 * @return Error code.
78 */
79static int generic_device_remove(ddf_dev_t *gen_dev)
80{
81 assert(driver);
82 assert(driver->ops);
83 if (driver->ops->device_rem == NULL)
84 return ENOTSUP;
85 /* Just tell the driver to stop whatever it is doing */
86 usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
87 const int ret = driver->ops->device_rem(usb_dev);
88 if (ret != EOK)
89 return ret;
90 usb_device_destroy_ddf(gen_dev);
91 return EOK;
92}
93
94/** Callback when a device was removed from the system.
95 *
96 * This callback is a wrapper for USB specific version of @c device_gone.
97 *
98 * @param gen_dev Device structure as prepared by DDF.
99 * @return Error code.
100 */
101static int generic_device_gone(ddf_dev_t *gen_dev)
102{
103 assert(driver);
104 assert(driver->ops);
105 if (driver->ops->device_gone == NULL)
106 return ENOTSUP;
107 usb_device_t *usb_dev = ddf_dev_data_get(gen_dev);
108 const int ret = driver->ops->device_gone(usb_dev);
109 if (ret == EOK)
110 usb_device_destroy_ddf(gen_dev);
111
112 return ret;
113}
114
115static driver_ops_t generic_driver_ops = {
116 .dev_add = generic_device_add,
117 .dev_remove = generic_device_remove,
118 .dev_gone = generic_device_gone,
119};
120static driver_t generic_driver = {
121 .driver_ops = &generic_driver_ops
122};
123
124
125/** Main routine of USB device driver.
126 *
127 * Under normal conditions, this function never returns.
128 *
129 * @param drv USB device driver structure.
130 * @return Task exit status.
131 */
132int usb_driver_main(const usb_driver_t *drv)
133{
134 assert(drv != NULL);
135
136 /* Prepare the generic driver. */
137 generic_driver.name = drv->name;
138
139 driver = drv;
140
141 return ddf_driver_main(&generic_driver);
142}
143
144/**
145 * @}
146 */
Note: See TracBrowser for help on using the repository browser.