1 | /*
|
---|
2 | * Copyright (c) 2013 Jan Vesely
|
---|
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 libusbhost
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | *
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <usb_iface.h>
|
---|
37 | #include <usb/classes/classes.h>
|
---|
38 | #include <usb/debug.h>
|
---|
39 | #include <usb/descriptor.h>
|
---|
40 | #include <usb/request.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <str_error.h>
|
---|
43 |
|
---|
44 | #include "ddf_helpers.h"
|
---|
45 |
|
---|
46 | #define CTRL_PIPE_MIN_PACKET_SIZE 8
|
---|
47 |
|
---|
48 | typedef struct usb_dev {
|
---|
49 | link_t link;
|
---|
50 | list_t devices;
|
---|
51 | fibril_mutex_t guard;
|
---|
52 | ddf_fun_t *fun;
|
---|
53 | usb_address_t address;
|
---|
54 | usb_speed_t speed;
|
---|
55 | usb_address_t tt_address;
|
---|
56 | unsigned port;
|
---|
57 | } usb_dev_t;
|
---|
58 |
|
---|
59 | typedef struct hc_dev {
|
---|
60 | ddf_fun_t *ctl_fun;
|
---|
61 | hcd_t hcd;
|
---|
62 | usb_dev_t *root_hub;
|
---|
63 | } hc_dev_t;
|
---|
64 |
|
---|
65 | static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
|
---|
66 | {
|
---|
67 | return ddf_dev_data_get(dev);
|
---|
68 | }
|
---|
69 |
|
---|
70 | hcd_t *dev_to_hcd(ddf_dev_t *dev)
|
---|
71 | {
|
---|
72 | hc_dev_t *hc_dev = dev_to_hc_dev(dev);
|
---|
73 | if (!hc_dev) {
|
---|
74 | usb_log_error("Invalid HCD device.\n");
|
---|
75 | return NULL;
|
---|
76 | }
|
---|
77 | return &hc_dev->hcd;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | static int hcd_ddf_new_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port);
|
---|
82 | static int hcd_ddf_remove_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port);
|
---|
83 |
|
---|
84 |
|
---|
85 | /* DDF INTERFACE */
|
---|
86 |
|
---|
87 | /** Register endpoint interface function.
|
---|
88 | * @param fun DDF function.
|
---|
89 | * @param address USB address of the device.
|
---|
90 | * @param endpoint USB endpoint number to be registered.
|
---|
91 | * @param transfer_type Endpoint's transfer type.
|
---|
92 | * @param direction USB communication direction the endpoint is capable of.
|
---|
93 | * @param max_packet_size Maximu size of packets the endpoint accepts.
|
---|
94 | * @param interval Preferred timeout between communication.
|
---|
95 | * @return Error code.
|
---|
96 | */
|
---|
97 | static int register_endpoint(
|
---|
98 | ddf_fun_t *fun, usb_endpoint_t endpoint,
|
---|
99 | usb_transfer_type_t transfer_type, usb_direction_t direction,
|
---|
100 | size_t max_packet_size, unsigned interval)
|
---|
101 | {
|
---|
102 | assert(fun);
|
---|
103 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
104 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
---|
105 | assert(hcd);
|
---|
106 | assert(dev);
|
---|
107 | const size_t size = max_packet_size;
|
---|
108 | const usb_target_t target =
|
---|
109 | {{.address = dev->address, .endpoint = endpoint}};
|
---|
110 |
|
---|
111 | usb_log_debug("Register endpoint %d:%d %s-%s %zuB %ums.\n",
|
---|
112 | dev->address, endpoint, usb_str_transfer_type(transfer_type),
|
---|
113 | usb_str_direction(direction), max_packet_size, interval);
|
---|
114 |
|
---|
115 | return hcd_add_ep(hcd, target, direction, transfer_type,
|
---|
116 | max_packet_size, size, dev->tt_address, dev->port);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /** Unregister endpoint interface function.
|
---|
120 | * @param fun DDF function.
|
---|
121 | * @param address USB address of the endpoint.
|
---|
122 | * @param endpoint USB endpoint number.
|
---|
123 | * @param direction Communication direction of the enpdoint to unregister.
|
---|
124 | * @return Error code.
|
---|
125 | */
|
---|
126 | static int unregister_endpoint(
|
---|
127 | ddf_fun_t *fun, usb_endpoint_t endpoint, usb_direction_t direction)
|
---|
128 | {
|
---|
129 | assert(fun);
|
---|
130 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
131 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
---|
132 | assert(hcd);
|
---|
133 | assert(dev);
|
---|
134 | const usb_target_t target =
|
---|
135 | {{.address = dev->address, .endpoint = endpoint}};
|
---|
136 | usb_log_debug("Unregister endpoint %d:%d %s.\n",
|
---|
137 | dev->address, endpoint, usb_str_direction(direction));
|
---|
138 | return hcd_remove_ep(hcd, target, direction);
|
---|
139 | }
|
---|
140 |
|
---|
141 | static int reserve_default_address(ddf_fun_t *fun, usb_speed_t speed)
|
---|
142 | {
|
---|
143 | assert(fun);
|
---|
144 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
145 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
---|
146 | assert(hcd);
|
---|
147 | assert(dev);
|
---|
148 |
|
---|
149 | usb_log_debug("Device %d requested default address at %s speed\n",
|
---|
150 | dev->address, usb_str_speed(speed));
|
---|
151 | return hcd_reserve_default_address(hcd, speed);
|
---|
152 | }
|
---|
153 |
|
---|
154 | static int release_default_address(ddf_fun_t *fun)
|
---|
155 | {
|
---|
156 | assert(fun);
|
---|
157 | hcd_t *hcd = dev_to_hcd(ddf_fun_get_dev(fun));
|
---|
158 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
---|
159 | assert(hcd);
|
---|
160 | assert(dev);
|
---|
161 |
|
---|
162 | usb_log_debug("Device %d released default address\n", dev->address);
|
---|
163 | return hcd_release_default_address(hcd);
|
---|
164 | }
|
---|
165 |
|
---|
166 | static int device_enumerate(ddf_fun_t *fun, unsigned port)
|
---|
167 | {
|
---|
168 | assert(fun);
|
---|
169 | ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
|
---|
170 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
---|
171 | assert(ddf_dev);
|
---|
172 | assert(dev);
|
---|
173 | usb_log_debug("Hub %d reported a new USB device on port: %u\n",
|
---|
174 | dev->address, port);
|
---|
175 | return hcd_ddf_new_device(ddf_dev, dev, port);
|
---|
176 | }
|
---|
177 |
|
---|
178 | static int device_remove(ddf_fun_t *fun, unsigned port)
|
---|
179 | {
|
---|
180 | assert(fun);
|
---|
181 | ddf_dev_t *ddf_dev = ddf_fun_get_dev(fun);
|
---|
182 | usb_dev_t *dev = ddf_fun_data_get(fun);
|
---|
183 | assert(ddf_dev);
|
---|
184 | assert(dev);
|
---|
185 | usb_log_debug("Hub `%s' reported removal of device on port %u\n",
|
---|
186 | ddf_fun_get_name(fun), port);
|
---|
187 | return hcd_ddf_remove_device(ddf_dev, dev, port);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** Gets handle of the respective device.
|
---|
191 | *
|
---|
192 | * @param[in] fun Device function.
|
---|
193 | * @param[out] handle Place to write the handle.
|
---|
194 | * @return Error code.
|
---|
195 | */
|
---|
196 | static int get_my_device_handle(ddf_fun_t *fun, devman_handle_t *handle)
|
---|
197 | {
|
---|
198 | assert(fun);
|
---|
199 | if (handle)
|
---|
200 | *handle = ddf_fun_get_handle(fun);
|
---|
201 | return EOK;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /** Inbound communication interface function.
|
---|
205 | * @param fun DDF function.
|
---|
206 | * @param target Communication target.
|
---|
207 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
208 | * @param data Pointer to data buffer.
|
---|
209 | * @param size Size of the data buffer.
|
---|
210 | * @param callback Function to call on communication end.
|
---|
211 | * @param arg Argument passed to the callback function.
|
---|
212 | * @return Error code.
|
---|
213 | */
|
---|
214 | static int dev_read(ddf_fun_t *fun, usb_endpoint_t endpoint,
|
---|
215 | uint64_t setup_data, uint8_t *data, size_t size,
|
---|
216 | usbhc_iface_transfer_in_callback_t callback, void *arg)
|
---|
217 | {
|
---|
218 | assert(fun);
|
---|
219 | usb_dev_t *usb_dev = ddf_fun_data_get(fun);
|
---|
220 | assert(usb_dev);
|
---|
221 | const usb_target_t target = {{
|
---|
222 | .address = usb_dev->address,
|
---|
223 | .endpoint = endpoint,
|
---|
224 | }};
|
---|
225 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)), target,
|
---|
226 | USB_DIRECTION_IN, data, size, setup_data, callback, NULL, arg,
|
---|
227 | "READ");
|
---|
228 | }
|
---|
229 |
|
---|
230 | /** Outbound communication interface function.
|
---|
231 | * @param fun DDF function.
|
---|
232 | * @param target Communication target.
|
---|
233 | * @param setup_data Data to use in setup stage (control transfers).
|
---|
234 | * @param data Pointer to data buffer.
|
---|
235 | * @param size Size of the data buffer.
|
---|
236 | * @param callback Function to call on communication end.
|
---|
237 | * @param arg Argument passed to the callback function.
|
---|
238 | * @return Error code.
|
---|
239 | */
|
---|
240 | static int dev_write(ddf_fun_t *fun, usb_endpoint_t endpoint,
|
---|
241 | uint64_t setup_data, const uint8_t *data, size_t size,
|
---|
242 | usbhc_iface_transfer_out_callback_t callback, void *arg)
|
---|
243 | {
|
---|
244 | assert(fun);
|
---|
245 | usb_dev_t *usb_dev = ddf_fun_data_get(fun);
|
---|
246 | assert(usb_dev);
|
---|
247 | const usb_target_t target = {{
|
---|
248 | .address = usb_dev->address,
|
---|
249 | .endpoint = endpoint,
|
---|
250 | }};
|
---|
251 | return hcd_send_batch(dev_to_hcd(ddf_fun_get_dev(fun)),
|
---|
252 | target, USB_DIRECTION_OUT, (uint8_t*)data, size, setup_data, NULL,
|
---|
253 | callback, arg, "WRITE");
|
---|
254 | }
|
---|
255 |
|
---|
256 | /** USB device interface */
|
---|
257 | static usb_iface_t usb_iface = {
|
---|
258 | .get_my_device_handle = get_my_device_handle,
|
---|
259 |
|
---|
260 | .reserve_default_address = reserve_default_address,
|
---|
261 | .release_default_address = release_default_address,
|
---|
262 |
|
---|
263 | .device_enumerate = device_enumerate,
|
---|
264 | .device_remove = device_remove,
|
---|
265 |
|
---|
266 | .register_endpoint = register_endpoint,
|
---|
267 | .unregister_endpoint = unregister_endpoint,
|
---|
268 |
|
---|
269 | .read = dev_read,
|
---|
270 | .write = dev_write,
|
---|
271 | };
|
---|
272 |
|
---|
273 | /** Standard USB device interface) */
|
---|
274 | static ddf_dev_ops_t usb_ops = {
|
---|
275 | .interfaces[USB_DEV_IFACE] = &usb_iface,
|
---|
276 | };
|
---|
277 |
|
---|
278 |
|
---|
279 | /* DDF HELPERS */
|
---|
280 |
|
---|
281 | #define GET_DEVICE_DESC(size) \
|
---|
282 | { \
|
---|
283 | .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST \
|
---|
284 | | (USB_REQUEST_TYPE_STANDARD << 5) \
|
---|
285 | | USB_REQUEST_RECIPIENT_DEVICE, \
|
---|
286 | .request = USB_DEVREQ_GET_DESCRIPTOR, \
|
---|
287 | .value = uint16_host2usb(USB_DESCTYPE_DEVICE << 8), \
|
---|
288 | .index = uint16_host2usb(0), \
|
---|
289 | .length = uint16_host2usb(size), \
|
---|
290 | };
|
---|
291 |
|
---|
292 | #define SET_ADDRESS(address) \
|
---|
293 | { \
|
---|
294 | .request_type = SETUP_REQUEST_TYPE_HOST_TO_DEVICE \
|
---|
295 | | (USB_REQUEST_TYPE_STANDARD << 5) \
|
---|
296 | | USB_REQUEST_RECIPIENT_DEVICE, \
|
---|
297 | .request = USB_DEVREQ_SET_ADDRESS, \
|
---|
298 | .value = uint16_host2usb(address), \
|
---|
299 | .index = uint16_host2usb(0), \
|
---|
300 | .length = uint16_host2usb(0), \
|
---|
301 | };
|
---|
302 |
|
---|
303 | static int hcd_ddf_add_device(ddf_dev_t *parent, usb_dev_t *hub_dev,
|
---|
304 | unsigned port, usb_address_t address, usb_speed_t speed, const char *name,
|
---|
305 | const match_id_list_t *mids)
|
---|
306 | {
|
---|
307 | assert(parent);
|
---|
308 |
|
---|
309 | char default_name[10] = { 0 }; /* usbxyz-ss */
|
---|
310 | if (!name) {
|
---|
311 | snprintf(default_name, sizeof(default_name) - 1,
|
---|
312 | "usb%u-%cs", address, usb_str_speed(speed)[0]);
|
---|
313 | name = default_name;
|
---|
314 | }
|
---|
315 |
|
---|
316 | ddf_fun_t *fun = ddf_fun_create(parent, fun_inner, name);
|
---|
317 | if (!fun)
|
---|
318 | return ENOMEM;
|
---|
319 | usb_dev_t *info = ddf_fun_data_alloc(fun, sizeof(usb_dev_t));
|
---|
320 | if (!info) {
|
---|
321 | ddf_fun_destroy(fun);
|
---|
322 | return ENOMEM;
|
---|
323 | }
|
---|
324 | info->address = address;
|
---|
325 | info->speed = speed;
|
---|
326 | info->fun = fun;
|
---|
327 | info->port = port;
|
---|
328 | info->tt_address = hub_dev ? hub_dev->tt_address : -1;
|
---|
329 | link_initialize(&info->link);
|
---|
330 | list_initialize(&info->devices);
|
---|
331 | fibril_mutex_initialize(&info->guard);
|
---|
332 |
|
---|
333 | if (hub_dev && hub_dev->speed == USB_SPEED_HIGH && usb_speed_is_11(speed))
|
---|
334 | info->tt_address = hub_dev->address;
|
---|
335 |
|
---|
336 | ddf_fun_set_ops(fun, &usb_ops);
|
---|
337 | list_foreach(mids->ids, link, const match_id_t, mid) {
|
---|
338 | ddf_fun_add_match_id(fun, mid->id, mid->score);
|
---|
339 | }
|
---|
340 |
|
---|
341 | int ret = ddf_fun_bind(fun);
|
---|
342 | if (ret != EOK) {
|
---|
343 | ddf_fun_destroy(fun);
|
---|
344 | return ret;
|
---|
345 | }
|
---|
346 |
|
---|
347 | if (hub_dev) {
|
---|
348 | fibril_mutex_lock(&hub_dev->guard);
|
---|
349 | list_append(&info->link, &hub_dev->devices);
|
---|
350 | fibril_mutex_unlock(&hub_dev->guard);
|
---|
351 | } else {
|
---|
352 | hc_dev_t *hc_dev = dev_to_hc_dev(parent);
|
---|
353 | assert(hc_dev->root_hub == NULL);
|
---|
354 | hc_dev->root_hub = info;
|
---|
355 | }
|
---|
356 | return EOK;
|
---|
357 | }
|
---|
358 |
|
---|
359 | #define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
|
---|
360 | do { \
|
---|
361 | match_id_t *mid = malloc(sizeof(match_id_t)); \
|
---|
362 | if (!mid) { \
|
---|
363 | clean_match_ids(list); \
|
---|
364 | return ENOMEM; \
|
---|
365 | } \
|
---|
366 | char *id = NULL; \
|
---|
367 | int ret = asprintf(&id, str, ##__VA_ARGS__); \
|
---|
368 | if (ret < 0) { \
|
---|
369 | clean_match_ids(list); \
|
---|
370 | free(mid); \
|
---|
371 | return ENOMEM; \
|
---|
372 | } \
|
---|
373 | mid->score = sc; \
|
---|
374 | mid->id = id; \
|
---|
375 | add_match_id(list, mid); \
|
---|
376 | } while (0)
|
---|
377 |
|
---|
378 | /* This is a copy of lib/usbdev/src/recognise.c */
|
---|
379 | static int create_match_ids(match_id_list_t *l,
|
---|
380 | usb_standard_device_descriptor_t *d)
|
---|
381 | {
|
---|
382 | assert(l);
|
---|
383 | assert(d);
|
---|
384 |
|
---|
385 | if (d->vendor_id != 0) {
|
---|
386 | /* First, with release number. */
|
---|
387 | ADD_MATCHID_OR_RETURN(l, 100,
|
---|
388 | "usb&vendor=%#04x&product=%#04x&release=%x.%x",
|
---|
389 | d->vendor_id, d->product_id, (d->device_version >> 8),
|
---|
390 | (d->device_version & 0xff));
|
---|
391 |
|
---|
392 | /* Next, without release number. */
|
---|
393 | ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
|
---|
394 | d->vendor_id, d->product_id);
|
---|
395 | }
|
---|
396 |
|
---|
397 | /* Class match id */
|
---|
398 | ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
|
---|
399 | usb_str_class(d->device_class));
|
---|
400 |
|
---|
401 | /* As a last resort, try fallback driver. */
|
---|
402 | ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
|
---|
403 |
|
---|
404 | return EOK;
|
---|
405 |
|
---|
406 | }
|
---|
407 |
|
---|
408 | static int hcd_ddf_remove_device(ddf_dev_t *device, usb_dev_t *hub,
|
---|
409 | unsigned port)
|
---|
410 | {
|
---|
411 | assert(device);
|
---|
412 |
|
---|
413 | hcd_t *hcd = dev_to_hcd(device);
|
---|
414 | assert(hcd);
|
---|
415 |
|
---|
416 | hc_dev_t *hc_dev = dev_to_hc_dev(device);
|
---|
417 | assert(hc_dev);
|
---|
418 |
|
---|
419 | fibril_mutex_lock(&hub->guard);
|
---|
420 |
|
---|
421 | usb_dev_t *victim = NULL;
|
---|
422 |
|
---|
423 | list_foreach(hub->devices, link, usb_dev_t, it) {
|
---|
424 | if (it->port == port) {
|
---|
425 | victim = it;
|
---|
426 | break;
|
---|
427 | }
|
---|
428 | }
|
---|
429 | if (victim && victim->port == port) {
|
---|
430 | list_remove(&victim->link);
|
---|
431 | fibril_mutex_unlock(&hub->guard);
|
---|
432 | const int ret = ddf_fun_unbind(victim->fun);
|
---|
433 | if (ret == EOK) {
|
---|
434 | ddf_fun_destroy(victim->fun);
|
---|
435 | hcd_release_address(hcd, victim->address);
|
---|
436 | } else {
|
---|
437 | usb_log_warning("Failed to unbind device `%s': %s\n",
|
---|
438 | ddf_fun_get_name(victim->fun), str_error(ret));
|
---|
439 | }
|
---|
440 | return EOK;
|
---|
441 | }
|
---|
442 | return ENOENT;
|
---|
443 | }
|
---|
444 |
|
---|
445 | static int hcd_ddf_new_device(ddf_dev_t *device, usb_dev_t *hub, unsigned port)
|
---|
446 | {
|
---|
447 | assert(device);
|
---|
448 |
|
---|
449 | hcd_t *hcd = dev_to_hcd(device);
|
---|
450 | assert(hcd);
|
---|
451 |
|
---|
452 | usb_speed_t speed = USB_SPEED_MAX;
|
---|
453 |
|
---|
454 | /* This checks whether the default address is reserved and gets speed */
|
---|
455 | int ret = usb_endpoint_manager_get_speed(&hcd->ep_manager,
|
---|
456 | USB_ADDRESS_DEFAULT, &speed);
|
---|
457 | if (ret != EOK) {
|
---|
458 | return ret;
|
---|
459 | }
|
---|
460 |
|
---|
461 | static const usb_target_t default_target = {{
|
---|
462 | .address = USB_ADDRESS_DEFAULT,
|
---|
463 | .endpoint = 0,
|
---|
464 | }};
|
---|
465 |
|
---|
466 | const usb_address_t address = hcd_request_address(hcd, speed);
|
---|
467 | if (address < 0)
|
---|
468 | return address;
|
---|
469 |
|
---|
470 | const usb_target_t target = {{
|
---|
471 | .address = address,
|
---|
472 | .endpoint = 0,
|
---|
473 | }};
|
---|
474 |
|
---|
475 | const usb_address_t tt_address = hub ? hub->tt_address : -1;
|
---|
476 |
|
---|
477 | /* Add default pipe on default address */
|
---|
478 | ret = hcd_add_ep(hcd,
|
---|
479 | default_target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
|
---|
480 | CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE,
|
---|
481 | tt_address, port);
|
---|
482 |
|
---|
483 | if (ret != EOK) {
|
---|
484 | hcd_release_address(hcd, address);
|
---|
485 | return ret;
|
---|
486 | }
|
---|
487 |
|
---|
488 | /* Get max packet size for default pipe */
|
---|
489 | usb_standard_device_descriptor_t desc = { 0 };
|
---|
490 | static const usb_device_request_setup_packet_t get_device_desc_8 =
|
---|
491 | GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
|
---|
492 |
|
---|
493 | // TODO CALLBACKS
|
---|
494 | ssize_t got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_IN,
|
---|
495 | &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
|
---|
496 | "read first 8 bytes of dev descriptor");
|
---|
497 |
|
---|
498 | if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
|
---|
499 | hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
|
---|
500 | hcd_release_address(hcd, address);
|
---|
501 | return got < 0 ? got : EOVERFLOW;
|
---|
502 | }
|
---|
503 |
|
---|
504 | /* Register EP on the new address */
|
---|
505 | ret = hcd_add_ep(hcd, target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
|
---|
506 | desc.max_packet_size, desc.max_packet_size, tt_address, port);
|
---|
507 | if (ret != EOK) {
|
---|
508 | hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
|
---|
509 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
510 | hcd_release_address(hcd, address);
|
---|
511 | return ret;
|
---|
512 | }
|
---|
513 |
|
---|
514 | /* Set new address */
|
---|
515 | const usb_device_request_setup_packet_t set_address =
|
---|
516 | SET_ADDRESS(target.address);
|
---|
517 |
|
---|
518 | got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
|
---|
519 | NULL, 0, *(uint64_t *)&set_address, "set address");
|
---|
520 |
|
---|
521 | hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
|
---|
522 |
|
---|
523 | if (got != 0) {
|
---|
524 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
525 | hcd_release_address(hcd, address);
|
---|
526 | return got;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /* Get std device descriptor */
|
---|
530 | static const usb_device_request_setup_packet_t get_device_desc =
|
---|
531 | GET_DEVICE_DESC(sizeof(desc));
|
---|
532 |
|
---|
533 | got = hcd_send_batch_sync(hcd, target, USB_DIRECTION_IN,
|
---|
534 | &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
|
---|
535 | "read device descriptor");
|
---|
536 | if (ret != EOK) {
|
---|
537 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
538 | hcd_release_address(hcd, target.address);
|
---|
539 | return got < 0 ? got : EOVERFLOW;
|
---|
540 | }
|
---|
541 |
|
---|
542 | /* Create match ids from the device descriptor */
|
---|
543 | match_id_list_t mids;
|
---|
544 | init_match_ids(&mids);
|
---|
545 |
|
---|
546 | ret = create_match_ids(&mids, &desc);
|
---|
547 | if (ret != EOK) {
|
---|
548 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
549 | hcd_release_address(hcd, target.address);
|
---|
550 | return ret;
|
---|
551 | }
|
---|
552 |
|
---|
553 | /* Register device */
|
---|
554 | ret = hcd_ddf_add_device(device, hub, port, address, speed, NULL, &mids);
|
---|
555 | clean_match_ids(&mids);
|
---|
556 | if (ret != EOK) {
|
---|
557 | hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
|
---|
558 | hcd_release_address(hcd, target.address);
|
---|
559 | }
|
---|
560 |
|
---|
561 | return ret;
|
---|
562 | }
|
---|
563 |
|
---|
564 | /** Announce root hub to the DDF
|
---|
565 | *
|
---|
566 | * @param[in] device Host controller ddf device
|
---|
567 | * @return Error code
|
---|
568 | */
|
---|
569 | int hcd_ddf_setup_root_hub(ddf_dev_t *device)
|
---|
570 | {
|
---|
571 | assert(device);
|
---|
572 | hcd_t *hcd = dev_to_hcd(device);
|
---|
573 | assert(hcd);
|
---|
574 |
|
---|
575 | hcd_reserve_default_address(hcd, hcd->ep_manager.max_speed);
|
---|
576 | const int ret = hcd_ddf_new_device(device, NULL, 0);
|
---|
577 | hcd_release_default_address(hcd);
|
---|
578 | return ret;
|
---|
579 | }
|
---|
580 |
|
---|
581 | /** Initialize hc structures.
|
---|
582 | *
|
---|
583 | * @param[in] device DDF instance of the device to use.
|
---|
584 | *
|
---|
585 | * This function does all the ddf work for hc driver.
|
---|
586 | */
|
---|
587 | int hcd_ddf_setup_hc(ddf_dev_t *device, usb_speed_t max_speed,
|
---|
588 | size_t bw, bw_count_func_t bw_count)
|
---|
589 | {
|
---|
590 | assert(device);
|
---|
591 |
|
---|
592 | hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
|
---|
593 | if (instance == NULL) {
|
---|
594 | usb_log_error("Failed to allocate HCD ddf structure.\n");
|
---|
595 | return ENOMEM;
|
---|
596 | }
|
---|
597 | instance->root_hub = NULL;
|
---|
598 | hcd_init(&instance->hcd, max_speed, bw, bw_count);
|
---|
599 |
|
---|
600 | int ret = ENOMEM;
|
---|
601 | instance->ctl_fun = ddf_fun_create(device, fun_exposed, "ctl");
|
---|
602 | if (!instance->ctl_fun) {
|
---|
603 | usb_log_error("Failed to create HCD ddf fun.\n");
|
---|
604 | goto err_destroy_fun;
|
---|
605 | }
|
---|
606 |
|
---|
607 | ret = ddf_fun_bind(instance->ctl_fun);
|
---|
608 | if (ret != EOK) {
|
---|
609 | usb_log_error("Failed to bind ctl_fun: %s.\n", str_error(ret));
|
---|
610 | goto err_destroy_fun;
|
---|
611 | }
|
---|
612 |
|
---|
613 | ret = ddf_fun_add_to_category(instance->ctl_fun, USB_HC_CATEGORY);
|
---|
614 | if (ret != EOK) {
|
---|
615 | usb_log_error("Failed to add fun to category: %s.\n",
|
---|
616 | str_error(ret));
|
---|
617 | ddf_fun_unbind(instance->ctl_fun);
|
---|
618 | goto err_destroy_fun;
|
---|
619 | }
|
---|
620 |
|
---|
621 | /* HC should be ok at this point (except it can't do anything) */
|
---|
622 | return EOK;
|
---|
623 |
|
---|
624 | err_destroy_fun:
|
---|
625 | ddf_fun_destroy(instance->ctl_fun);
|
---|
626 | instance->ctl_fun = NULL;
|
---|
627 | return ret;
|
---|
628 | }
|
---|
629 |
|
---|
630 | void hcd_ddf_clean_hc(ddf_dev_t *device)
|
---|
631 | {
|
---|
632 | assert(device);
|
---|
633 | hc_dev_t *hc = dev_to_hc_dev(device);
|
---|
634 | assert(hc);
|
---|
635 | const int ret = ddf_fun_unbind(hc->ctl_fun);
|
---|
636 | if (ret == EOK)
|
---|
637 | ddf_fun_destroy(hc->ctl_fun);
|
---|
638 | }
|
---|
639 | /**
|
---|
640 | * @}
|
---|
641 | */
|
---|