1 | /*
|
---|
2 | * Copyright (c) 2011 Vojtech Horky
|
---|
3 | * Copyright (c) 2018 Petr Manek, Ondrej Hlavaty
|
---|
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>
|
---|
41 | #include <usb/dev/request.h>
|
---|
42 | #include <usb/descriptor.h>
|
---|
43 | #include <usb/dev/pipes.h>
|
---|
44 |
|
---|
45 | #include "usbmid.h"
|
---|
46 |
|
---|
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 | */
|
---|
52 | static errno_t usbmid_device_add(usb_device_t *dev)
|
---|
53 | {
|
---|
54 | usb_log_info("Taking care of new MID `%s'.", usb_device_get_name(dev));
|
---|
55 |
|
---|
56 | return usbmid_explore_device(dev);
|
---|
57 | }
|
---|
58 |
|
---|
59 | static errno_t destroy_interfaces(usb_mid_t *usb_mid)
|
---|
60 | {
|
---|
61 | errno_t ret = EOK;
|
---|
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 |
|
---|
69 | const errno_t pret = usbmid_interface_destroy(iface);
|
---|
70 | if (pret != EOK) {
|
---|
71 | usb_log_error("Failed to remove child `%s': %s",
|
---|
72 | ddf_fun_get_name(iface->fun), str_error(pret));
|
---|
73 | ret = pret;
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | return ret;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /** Callback when a MID device is about to be removed from the host.
|
---|
81 | *
|
---|
82 | * @param dev USB device representing the removed device.
|
---|
83 | * @return Error code.
|
---|
84 | */
|
---|
85 | static errno_t usbmid_device_remove(usb_device_t *dev)
|
---|
86 | {
|
---|
87 | assert(dev);
|
---|
88 | usb_mid_t *usb_mid = usb_device_data_get(dev);
|
---|
89 | assert(usb_mid);
|
---|
90 |
|
---|
91 | /* Remove ctl function */
|
---|
92 | errno_t ret = ddf_fun_unbind(usb_mid->ctl_fun);
|
---|
93 | if (ret != EOK) {
|
---|
94 | usb_log_error("Failed to unbind USB MID ctl function: %s.",
|
---|
95 | str_error(ret));
|
---|
96 | return ret;
|
---|
97 | }
|
---|
98 | ddf_fun_destroy(usb_mid->ctl_fun);
|
---|
99 |
|
---|
100 | /* Remove all children */
|
---|
101 | list_foreach(usb_mid->interface_list, link, usbmid_interface_t, iface) {
|
---|
102 | usb_log_info("Removing child `%s'.",
|
---|
103 | ddf_fun_get_name(iface->fun));
|
---|
104 |
|
---|
105 | /* Tell the child to go offline. */
|
---|
106 | errno_t pret = ddf_fun_offline(iface->fun);
|
---|
107 | if (pret != EOK) {
|
---|
108 | usb_log_warning("Failed to turn off child `%s': %s",
|
---|
109 | ddf_fun_get_name(iface->fun), str_error(pret));
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | return destroy_interfaces(usb_mid);
|
---|
114 | }
|
---|
115 |
|
---|
116 | /** Callback when a MID device was removed from the host.
|
---|
117 | *
|
---|
118 | * @param dev USB device representing the removed device.
|
---|
119 | * @return Error code.
|
---|
120 | */
|
---|
121 | static errno_t usbmid_device_gone(usb_device_t *dev)
|
---|
122 | {
|
---|
123 | assert(dev);
|
---|
124 | usb_mid_t *usb_mid = usb_device_data_get(dev);
|
---|
125 | assert(usb_mid);
|
---|
126 |
|
---|
127 | usb_log_info("USB MID gone: `%s'.", usb_device_get_name(dev));
|
---|
128 |
|
---|
129 | /* Remove ctl function */
|
---|
130 | errno_t ret = ddf_fun_unbind(usb_mid->ctl_fun);
|
---|
131 | if (ret != EOK) {
|
---|
132 | usb_log_error("Failed to unbind USB MID ctl function: %s.",
|
---|
133 | str_error(ret));
|
---|
134 | return ret;
|
---|
135 | }
|
---|
136 | ddf_fun_destroy(usb_mid->ctl_fun);
|
---|
137 |
|
---|
138 | /* Destroy children and tell their drivers they are gone. */
|
---|
139 | return destroy_interfaces(usb_mid);
|
---|
140 | }
|
---|
141 |
|
---|
142 | static errno_t usbmid_function_online(ddf_fun_t *fun)
|
---|
143 | {
|
---|
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 |
|
---|
149 | return ddf_fun_online(fun);
|
---|
150 | }
|
---|
151 |
|
---|
152 | static errno_t usbmid_function_offline(ddf_fun_t *fun)
|
---|
153 | {
|
---|
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 |
|
---|
159 | return ddf_fun_offline(fun);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /** USB MID driver ops. */
|
---|
163 | static const usb_driver_ops_t mid_driver_ops = {
|
---|
164 | .device_add = usbmid_device_add,
|
---|
165 | .device_remove = usbmid_device_remove,
|
---|
166 | .device_gone = usbmid_device_gone,
|
---|
167 | .function_online = usbmid_function_online,
|
---|
168 | .function_offline = usbmid_function_offline
|
---|
169 | };
|
---|
170 |
|
---|
171 | /** USB MID driver. */
|
---|
172 | static const usb_driver_t mid_driver = {
|
---|
173 | .name = NAME,
|
---|
174 | .ops = &mid_driver_ops,
|
---|
175 | .endpoints = NULL
|
---|
176 | };
|
---|
177 |
|
---|
178 | int main(int argc, char *argv[])
|
---|
179 | {
|
---|
180 | printf(NAME ": USB multi interface device driver.\n");
|
---|
181 |
|
---|
182 | log_init(NAME);
|
---|
183 |
|
---|
184 | return usb_driver_main(&mid_driver);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * @}
|
---|
189 | */
|
---|