1 | /*
|
---|
2 | * Copyright (c) 2010 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libusb usb
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief HC driver and hub driver (implementation).
|
---|
34 | */
|
---|
35 | #include <usb/hcdhubd.h>
|
---|
36 | #include <usbhc_iface.h>
|
---|
37 | #include <driver.h>
|
---|
38 | #include <bool.h>
|
---|
39 | #include <errno.h>
|
---|
40 |
|
---|
41 | #define USB_HUB_DEVICE_NAME "usbhub"
|
---|
42 |
|
---|
43 | /** List of handled host controllers. */
|
---|
44 | static LIST_INITIALIZE(hc_list);
|
---|
45 |
|
---|
46 | /** Our HC driver. */
|
---|
47 | static usb_hc_driver_t *hc_driver = NULL;
|
---|
48 |
|
---|
49 | static usbhc_iface_t usb_interface = {
|
---|
50 | .interrupt_out = NULL,
|
---|
51 | .interrupt_in = NULL
|
---|
52 | };
|
---|
53 |
|
---|
54 | static device_ops_t usb_device_ops = {
|
---|
55 | .interfaces[USBHC_DEV_IFACE] = &usb_interface
|
---|
56 | };
|
---|
57 |
|
---|
58 | /** Callback when new device is detected and must be handled by this driver.
|
---|
59 | *
|
---|
60 | * @param dev New device.
|
---|
61 | * @return Error code.
|
---|
62 | */
|
---|
63 | static int add_device(device_t *dev)
|
---|
64 | {
|
---|
65 | /*
|
---|
66 | * FIXME: use some magic to determine whether hub or another HC
|
---|
67 | * was connected.
|
---|
68 | */
|
---|
69 | bool is_hc = str_cmp(dev->name, USB_HUB_DEVICE_NAME) != 0;
|
---|
70 | printf("%s: add_device(name=\"%s\")\n", hc_driver->name, dev->name);
|
---|
71 |
|
---|
72 | if (is_hc) {
|
---|
73 | /*
|
---|
74 | * We are the HC itself.
|
---|
75 | */
|
---|
76 | usb_hc_device_t *hc_dev = malloc(sizeof(usb_hc_device_t));
|
---|
77 | list_initialize(&hc_dev->link);
|
---|
78 | hc_dev->transfer_ops = NULL;
|
---|
79 |
|
---|
80 | hc_dev->generic = dev;
|
---|
81 | dev->ops = &usb_device_ops;
|
---|
82 | hc_dev->generic->driver_data = hc_dev;
|
---|
83 |
|
---|
84 | int rc = hc_driver->add_hc(hc_dev);
|
---|
85 | if (rc != EOK) {
|
---|
86 | free(hc_dev);
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * FIXME: The following line causes devman to hang.
|
---|
92 | * Will investigate later why.
|
---|
93 | */
|
---|
94 | // add_device_to_class(dev, "usbhc");
|
---|
95 |
|
---|
96 | list_append(&hc_dev->link, &hc_list);
|
---|
97 |
|
---|
98 | return EOK;
|
---|
99 | } else {
|
---|
100 | printf("%s: hub added, hurrah!\n", hc_driver->name);
|
---|
101 | /*
|
---|
102 | * We are some (probably deeply nested) hub.
|
---|
103 | * Thus, assign our own operations and explore already
|
---|
104 | * connected devices.
|
---|
105 | */
|
---|
106 | return ENOTSUP;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Check changes on all known hubs.
|
---|
111 | */
|
---|
112 | static void check_hub_changes(void)
|
---|
113 | {
|
---|
114 | /*
|
---|
115 | * Iterate through all HCs.
|
---|
116 | */
|
---|
117 | link_t *link_hc;
|
---|
118 | for (link_hc = hc_list.next;
|
---|
119 | link_hc != &hc_list;
|
---|
120 | link_hc = link_hc->next) {
|
---|
121 | usb_hc_device_t *hc = list_get_instance(link_hc,
|
---|
122 | usb_hc_device_t, link);
|
---|
123 | /*
|
---|
124 | * Iterate through all their hubs.
|
---|
125 | */
|
---|
126 | link_t *link_hub;
|
---|
127 | for (link_hub = hc->hubs.next;
|
---|
128 | link_hub != &hc->hubs;
|
---|
129 | link_hub = link_hub->next) {
|
---|
130 | usb_hcd_hub_info_t *hub = list_get_instance(link_hub,
|
---|
131 | usb_hcd_hub_info_t, link);
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * Check status change pipe of this hub.
|
---|
135 | */
|
---|
136 | usb_target_t target = {
|
---|
137 | .address = hub->device->address,
|
---|
138 | .endpoint = 1
|
---|
139 | };
|
---|
140 |
|
---|
141 | // FIXME: count properly
|
---|
142 | size_t byte_length = (hub->port_count / 8) + 1;
|
---|
143 |
|
---|
144 | void *change_bitmap = malloc(byte_length);
|
---|
145 | size_t actual_size;
|
---|
146 | usb_handle_t handle;
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Send the request.
|
---|
150 | * FIXME: check returned value for possible errors
|
---|
151 | */
|
---|
152 | usb_hc_async_interrupt_in(hc, target,
|
---|
153 | change_bitmap, byte_length, &actual_size,
|
---|
154 | &handle);
|
---|
155 |
|
---|
156 | usb_hc_async_wait_for(handle);
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * TODO: handle the changes.
|
---|
160 | */
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 |
|
---|
165 | /** Operations for combined HC and HUB driver. */
|
---|
166 | static driver_ops_t hc_driver_generic_ops = {
|
---|
167 | .add_device = add_device
|
---|
168 | };
|
---|
169 |
|
---|
170 | /** Combined HC and HUB driver. */
|
---|
171 | static driver_t hc_driver_generic = {
|
---|
172 | .driver_ops = &hc_driver_generic_ops
|
---|
173 | };
|
---|
174 |
|
---|
175 | /** Main USB host controller driver routine.
|
---|
176 | *
|
---|
177 | * @see driver_main
|
---|
178 | *
|
---|
179 | * @param hc Host controller driver.
|
---|
180 | * @return Error code.
|
---|
181 | */
|
---|
182 | int usb_hcd_main(usb_hc_driver_t *hc)
|
---|
183 | {
|
---|
184 | hc_driver = hc;
|
---|
185 | hc_driver_generic.name = hc->name;
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Launch here fibril that will periodically check all
|
---|
189 | * attached hubs for status change.
|
---|
190 | * WARN: This call will effectively do nothing.
|
---|
191 | */
|
---|
192 | check_hub_changes();
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * Run the device driver framework.
|
---|
196 | */
|
---|
197 | return driver_main(&hc_driver_generic);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /** Add a root hub for given host controller.
|
---|
201 | * This function shall be called only once for each host controller driven
|
---|
202 | * by this driver.
|
---|
203 | * It takes care of creating child device - hub - that will be driven by
|
---|
204 | * this task.
|
---|
205 | *
|
---|
206 | * @param dev Host controller device.
|
---|
207 | * @return Error code.
|
---|
208 | */
|
---|
209 | int usb_hcd_add_root_hub(usb_hc_device_t *dev)
|
---|
210 | {
|
---|
211 | int rc;
|
---|
212 |
|
---|
213 | /*
|
---|
214 | * For testing/debugging purposes only.
|
---|
215 | * Try to send some data to default USB address.
|
---|
216 | */
|
---|
217 | usb_target_t target = {0, 0};
|
---|
218 | usb_handle_t handle = 0;
|
---|
219 | char *data = (char *) "Hello, World!";
|
---|
220 |
|
---|
221 |
|
---|
222 | (void)usb_hc_async_interrupt_out(dev, target, data, str_length(data), &handle);
|
---|
223 | (void)usb_hc_async_wait_for(handle);
|
---|
224 |
|
---|
225 | /*
|
---|
226 | * Announce presence of child device.
|
---|
227 | */
|
---|
228 | device_t *hub = NULL;
|
---|
229 | match_id_t *match_id = NULL;
|
---|
230 |
|
---|
231 | hub = create_device();
|
---|
232 | if (hub == NULL) {
|
---|
233 | rc = ENOMEM;
|
---|
234 | goto failure;
|
---|
235 | }
|
---|
236 | hub->name = USB_HUB_DEVICE_NAME;
|
---|
237 |
|
---|
238 | match_id = create_match_id();
|
---|
239 | if (match_id == NULL) {
|
---|
240 | rc = ENOMEM;
|
---|
241 | goto failure;
|
---|
242 | }
|
---|
243 |
|
---|
244 | char *id;
|
---|
245 | rc = asprintf(&id, "usb&hc=%s&hub", dev->generic->name);
|
---|
246 | if (rc <= 0) {
|
---|
247 | rc = ENOMEM;
|
---|
248 | goto failure;
|
---|
249 | }
|
---|
250 |
|
---|
251 | match_id->id = id;
|
---|
252 | match_id->score = 30;
|
---|
253 |
|
---|
254 | add_match_id(&hub->match_ids, match_id);
|
---|
255 |
|
---|
256 | rc = child_device_register(hub, dev->generic);
|
---|
257 | if (rc != EOK) {
|
---|
258 | goto failure;
|
---|
259 | }
|
---|
260 |
|
---|
261 | printf("%s: registered root hub\n", dev->generic->name);
|
---|
262 | return EOK;
|
---|
263 |
|
---|
264 | failure:
|
---|
265 | if (hub != NULL) {
|
---|
266 | hub->name = NULL;
|
---|
267 | delete_device(hub);
|
---|
268 | }
|
---|
269 | delete_match_id(match_id);
|
---|
270 |
|
---|
271 | return rc;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /** Issue interrupt OUT transfer to HC driven by current task.
|
---|
275 | *
|
---|
276 | * @param hc Host controller to handle the transfer.
|
---|
277 | * @param target Target device address.
|
---|
278 | * @param buffer Data buffer.
|
---|
279 | * @param size Buffer size.
|
---|
280 | * @param handle Transfer handle.
|
---|
281 | * @return Error code.
|
---|
282 | */
|
---|
283 | int usb_hc_async_interrupt_out(usb_hc_device_t *hc, usb_target_t target,
|
---|
284 | void *buffer, size_t size,
|
---|
285 | usb_handle_t *handle)
|
---|
286 | {
|
---|
287 | if ((hc->transfer_ops == NULL)
|
---|
288 | || (hc->transfer_ops->transfer_out == NULL)) {
|
---|
289 | return ENOTSUP;
|
---|
290 | }
|
---|
291 |
|
---|
292 | /*
|
---|
293 | * For debugging purposes only.
|
---|
294 | * We need to find appropriate device in list of managed device
|
---|
295 | * and pass it to the transfer callback function.
|
---|
296 | */
|
---|
297 | usb_hcd_attached_device_info_t dev = {
|
---|
298 | .address = target.address,
|
---|
299 | .endpoint_count = 0,
|
---|
300 | .endpoints = NULL,
|
---|
301 | };
|
---|
302 | usb_hc_endpoint_info_t endpoint = {
|
---|
303 | .endpoint = target.endpoint,
|
---|
304 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
305 | .direction = USB_DIRECTION_OUT,
|
---|
306 | .data_toggle = 0
|
---|
307 | };
|
---|
308 |
|
---|
309 | hc->transfer_ops->transfer_out(hc, &dev, &endpoint, buffer, size, NULL, NULL);
|
---|
310 |
|
---|
311 | *handle = (usb_handle_t)NULL;
|
---|
312 |
|
---|
313 | return EOK;
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | /** Issue interrupt IN transfer to HC driven by current task.
|
---|
318 | *
|
---|
319 | * @warning The @p buffer and @p actual_size parameters shall not be
|
---|
320 | * touched until this transfer is waited for by usb_hc_async_wait_for().
|
---|
321 | *
|
---|
322 | * @param hc Host controller to handle the transfer.
|
---|
323 | * @param target Target device address.
|
---|
324 | * @param buffer Data buffer.
|
---|
325 | * @param size Buffer size.
|
---|
326 | * @param actual_size Size of actually transferred data.
|
---|
327 | * @param handle Transfer handle.
|
---|
328 | * @return Error code.
|
---|
329 | */
|
---|
330 | int usb_hc_async_interrupt_in(usb_hc_device_t *hc, usb_target_t target,
|
---|
331 | void *buffer, size_t size, size_t *actual_size,
|
---|
332 | usb_handle_t *handle)
|
---|
333 | {
|
---|
334 | /*
|
---|
335 | * TODO: verify that given endpoint is of interrupt type and
|
---|
336 | * call hc->transfer_ops->transfer_in()
|
---|
337 | */
|
---|
338 | return ENOTSUP;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /** Wait for transfer to complete.
|
---|
342 | *
|
---|
343 | * @param handle Transfer handle.
|
---|
344 | * @return Error code.
|
---|
345 | */
|
---|
346 | int usb_hc_async_wait_for(usb_handle_t handle)
|
---|
347 | {
|
---|
348 | return ENOTSUP;
|
---|
349 | }
|
---|
350 |
|
---|
351 | /**
|
---|
352 | * @}
|
---|
353 | */
|
---|