source: mainline/uspace/lib/usbhost/src/ddf_helpers.c@ b995183

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

libusbhost: Add support for device removal. Use address as id.

  • Property mode set to 100644
File size: 12.6 KB
Line 
1/*
2 * Copyright (c) 2012 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
48extern usbhc_iface_t hcd_iface;
49
50typedef struct hc_dev {
51 ddf_fun_t *hc_fun;
52 list_t devices;
53 fibril_mutex_t guard;
54} hc_dev_t;
55
56static hc_dev_t *dev_to_hc_dev(ddf_dev_t *dev)
57{
58 return ddf_dev_data_get(dev);
59}
60
61hcd_t *dev_to_hcd(ddf_dev_t *dev)
62{
63 hc_dev_t *hc_dev = dev_to_hc_dev(dev);
64 if (!hc_dev || !hc_dev->hc_fun) {
65 usb_log_error("Invalid HCD device.\n");
66 return NULL;
67 }
68 return ddf_fun_data_get(hc_dev->hc_fun);
69}
70
71typedef struct usb_dev {
72 link_t link;
73 ddf_fun_t *fun;
74 usb_address_t address;
75 usb_speed_t speed;
76 devman_handle_t hc_handle;
77} usb_dev_t;
78
79/** Get USB address assigned to root hub.
80 *
81 * @param[in] fun Root hub function.
82 * @param[out] address Store the address here.
83 * @return Error code.
84 */
85static int get_my_address(ddf_fun_t *fun, usb_address_t *address)
86{
87 assert(fun);
88 if (address != NULL) {
89 usb_dev_t *usb_dev = ddf_fun_data_get(fun);
90 *address = usb_dev->address;
91 }
92 return EOK;
93}
94
95/** Gets handle of the respective hc (this device, hc function).
96 *
97 * @param[in] root_hub_fun Root hub function seeking hc handle.
98 * @param[out] handle Place to write the handle.
99 * @return Error code.
100 */
101static int get_hc_handle(ddf_fun_t *fun, devman_handle_t *handle)
102{
103 assert(fun);
104
105 if (handle != NULL) {
106 usb_dev_t *usb_dev = ddf_fun_data_get(fun);
107 *handle = usb_dev->hc_handle;
108 }
109 return EOK;
110}
111
112/** Root hub USB interface */
113static usb_iface_t usb_iface = {
114 .get_hc_handle = get_hc_handle,
115 .get_my_address = get_my_address,
116};
117/** Standard USB RH options (RH interface) */
118static ddf_dev_ops_t usb_ops = {
119 .interfaces[USB_DEV_IFACE] = &usb_iface,
120};
121
122/** Standard USB HC options (HC interface) */
123static ddf_dev_ops_t hc_ops = {
124 .interfaces[USBHC_DEV_IFACE] = &hcd_iface,
125};
126
127#define GET_DEVICE_DESC(size) \
128{ \
129 .request_type = SETUP_REQUEST_TYPE_DEVICE_TO_HOST \
130 | (USB_REQUEST_TYPE_STANDARD << 5) \
131 | USB_REQUEST_RECIPIENT_DEVICE, \
132 .request = USB_DEVREQ_GET_DESCRIPTOR, \
133 .value = uint16_host2usb(USB_DESCTYPE_DEVICE << 8), \
134 .index = uint16_host2usb(0), \
135 .length = uint16_host2usb(size), \
136};
137
138#define SET_ADDRESS(address) \
139{ \
140 .request_type = SETUP_REQUEST_TYPE_HOST_TO_DEVICE \
141 | (USB_REQUEST_TYPE_STANDARD << 5) \
142 | USB_REQUEST_RECIPIENT_DEVICE, \
143 .request = USB_DEVREQ_SET_ADDRESS, \
144 .value = uint16_host2usb(address), \
145 .index = uint16_host2usb(0), \
146 .length = uint16_host2usb(0), \
147};
148
149int hcd_ddf_add_usb_device(ddf_dev_t *parent,
150 usb_address_t address, usb_speed_t speed, const char *name,
151 const match_id_list_t *mids)
152{
153 assert(parent);
154 hc_dev_t *hc_dev = dev_to_hc_dev(parent);
155 devman_handle_t hc_handle = ddf_fun_get_handle(hc_dev->hc_fun);
156
157 char default_name[10] = { 0 }; /* usbxyz-ss */
158 if (!name) {
159 snprintf(default_name, sizeof(default_name) - 1,
160 "usb%u-%cs", address, usb_str_speed(speed)[0]);
161 name = default_name;
162 }
163
164 //TODO more checks
165 ddf_fun_t *fun = ddf_fun_create(parent, fun_inner, name);
166 if (!fun)
167 return ENOMEM;
168 usb_dev_t *info = ddf_fun_data_alloc(fun, sizeof(usb_dev_t));
169 if (!info) {
170 ddf_fun_destroy(fun);
171 return ENOMEM;
172 }
173 info->address = address;
174 info->speed = speed;
175 info->hc_handle = hc_handle;
176 info->fun = fun;
177 link_initialize(&info->link);
178
179 ddf_fun_set_ops(fun, &usb_ops);
180 list_foreach(mids->ids, iter) {
181 match_id_t *mid = list_get_instance(iter, match_id_t, link);
182 ddf_fun_add_match_id(fun, mid->id, mid->score);
183 }
184
185 int ret = ddf_fun_bind(fun);
186 if (ret != EOK) {
187 ddf_fun_destroy(fun);
188 return ret;
189 }
190
191 ret = usb_device_manager_bind_address(&dev_to_hcd(parent)->dev_manager,
192 address, ddf_fun_get_handle(fun));
193 if (ret != EOK)
194 usb_log_warning("Failed to bind address: %s.\n",
195 str_error(ret));
196
197 list_append(&info->link, &hc_dev->devices);
198 return EOK;
199}
200
201#define ADD_MATCHID_OR_RETURN(list, sc, str, ...) \
202do { \
203 match_id_t *mid = malloc(sizeof(match_id_t)); \
204 if (!mid) { \
205 clean_match_ids(list); \
206 return ENOMEM; \
207 } \
208 char *id = NULL; \
209 int ret = asprintf(&id, str, ##__VA_ARGS__); \
210 if (ret < 0) { \
211 clean_match_ids(list); \
212 free(mid); \
213 return ENOMEM; \
214 } \
215 mid->score = sc; \
216 mid->id = id; \
217 add_match_id(list, mid); \
218} while (0)
219
220
221/* This is a copy of lib/usbdev/src/recognise.c */
222static int create_match_ids(match_id_list_t *l,
223 usb_standard_device_descriptor_t *d)
224{
225 assert(l);
226 assert(d);
227
228 if (d->vendor_id != 0) {
229 /* First, with release number. */
230 ADD_MATCHID_OR_RETURN(l, 100,
231 "usb&vendor=%#04x&product=%#04x&release=%x.%x",
232 d->vendor_id, d->product_id, (d->device_version >> 8),
233 (d->device_version & 0xff));
234
235 /* Next, without release number. */
236 ADD_MATCHID_OR_RETURN(l, 90, "usb&vendor=%#04x&product=%#04x",
237 d->vendor_id, d->product_id);
238 }
239
240 /* Class match id */
241 ADD_MATCHID_OR_RETURN(l, 50, "usb&class=%s",
242 usb_str_class(d->device_class));
243
244 /* As a last resort, try fallback driver. */
245 ADD_MATCHID_OR_RETURN(l, 10, "usb&fallback");
246
247 return EOK;
248
249}
250
251int hcd_ddf_remove_device(ddf_dev_t *device, usb_address_t id)
252{
253 assert(device);
254
255 hcd_t *hcd = dev_to_hcd(device);
256 assert(hcd);
257
258 hc_dev_t *hc_dev = dev_to_hc_dev(device);
259 assert(hc_dev);
260
261 fibril_mutex_lock(&hc_dev->guard);
262
263 usb_dev_t *victim = NULL;
264
265 list_foreach(hc_dev->devices, it) {
266 victim = list_get_instance(it, usb_dev_t, link);
267 if (victim->address == id)
268 break;
269 }
270 if (victim && victim->address == id) {
271 list_remove(&victim->link);
272 fibril_mutex_unlock(&hc_dev->guard);
273 ddf_fun_unbind(victim->fun);
274 ddf_fun_destroy(victim->fun);
275 return EOK;
276 }
277 return ENOENT;
278}
279
280int hcd_ddf_new_device(ddf_dev_t *device, usb_address_t *id)
281{
282 assert(device);
283
284 hcd_t *hcd = dev_to_hcd(device);
285 assert(hcd);
286
287 usb_speed_t speed = USB_SPEED_MAX;
288
289 /* This checks whether the default address is reserved and gets speed */
290 int ret = usb_device_manager_get_info_by_address(&hcd->dev_manager,
291 USB_ADDRESS_DEFAULT, NULL, &speed);
292 if (ret != EOK) {
293 return ret;
294 }
295
296 static const usb_target_t default_target = {{
297 .address = USB_ADDRESS_DEFAULT,
298 .endpoint = 0,
299 }};
300
301 const usb_address_t address = hcd_request_address(hcd, speed);
302 if (address < 0)
303 return address;
304
305 const usb_target_t target = {{
306 .address = address,
307 .endpoint = 0,
308 }};
309
310 /* Add default pipe on default address */
311 ret = hcd_add_ep(hcd,
312 default_target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
313 CTRL_PIPE_MIN_PACKET_SIZE, CTRL_PIPE_MIN_PACKET_SIZE);
314
315 if (ret != EOK) {
316 hcd_release_address(hcd, address);
317 return ret;
318 }
319
320 /* Get max packet size for default pipe */
321 usb_standard_device_descriptor_t desc = { 0 };
322 static const usb_device_request_setup_packet_t get_device_desc_8 =
323 GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
324
325 // TODO CALLBACKS
326 ssize_t got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_IN,
327 &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
328 "read first 8 bytes of dev descriptor");
329
330 if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
331 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
332 hcd_release_address(hcd, address);
333 return got < 0 ? got : EOVERFLOW;
334 }
335
336 /* Register EP on the new address */
337 ret = hcd_add_ep(hcd, target, USB_DIRECTION_BOTH, USB_TRANSFER_CONTROL,
338 desc.max_packet_size, desc.max_packet_size);
339 if (ret != EOK) {
340 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
341 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
342 hcd_release_address(hcd, address);
343 return ret;
344 }
345
346 /* Set new address */
347 const usb_device_request_setup_packet_t set_address =
348 SET_ADDRESS(target.address);
349
350 got = hcd_send_batch_sync(hcd, default_target, USB_DIRECTION_OUT,
351 NULL, 0, *(uint64_t *)&set_address, "set address");
352
353 hcd_remove_ep(hcd, default_target, USB_DIRECTION_BOTH);
354
355 if (got != 0) {
356 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
357 hcd_release_address(hcd, address);
358 return got;
359 }
360
361 /* Get std device descriptor */
362 static const usb_device_request_setup_packet_t get_device_desc =
363 GET_DEVICE_DESC(sizeof(desc));
364
365 got = hcd_send_batch_sync(hcd, target, USB_DIRECTION_IN,
366 &desc, sizeof(desc), *(uint64_t *)&get_device_desc,
367 "read device descriptor");
368 if (ret != EOK) {
369 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
370 hcd_release_address(hcd, target.address);
371 return got < 0 ? got : EOVERFLOW;
372 }
373
374 /* Create match ids from the device descriptor */
375 match_id_list_t mids;
376 init_match_ids(&mids);
377
378 ret = create_match_ids(&mids, &desc);
379 if (ret != EOK) {
380 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
381 hcd_release_address(hcd, target.address);
382 return ret;
383 }
384
385 /* Register device */
386 ret = hcd_ddf_add_usb_device(device, address, speed, NULL, &mids);
387 clean_match_ids(&mids);
388 if (ret != EOK) {
389 hcd_remove_ep(hcd, target, USB_DIRECTION_BOTH);
390 hcd_release_address(hcd, target.address);
391 return ret;
392 }
393 if (ret == EOK && id)
394 *id = target.address;
395
396 return ret;
397}
398
399/** Announce root hub to the DDF
400 *
401 * @param[in] device Host controller ddf device
402 * @param[in] speed roothub communication speed
403 * @return Error code
404 */
405int hcd_ddf_setup_root_hub(ddf_dev_t *device, usb_speed_t speed)
406{
407 assert(device);
408 hcd_t *hcd = dev_to_hcd(device);
409 assert(hcd);
410
411 hcd_reserve_default_address(hcd, speed);
412 const int ret = hcd_ddf_new_device(device, NULL);
413 hcd_release_default_address(hcd);
414 return ret;
415}
416
417/** Initialize hc structures.
418 *
419 * @param[in] device DDF instance of the device to use.
420 *
421 * This function does all the ddf work for hc driver.
422 */
423int hcd_ddf_setup_device(ddf_dev_t *device, ddf_fun_t **hc_fun,
424 usb_speed_t max_speed, size_t bw, bw_count_func_t bw_count)
425{
426 if (device == NULL)
427 return EBADMEM;
428
429 hc_dev_t *instance = ddf_dev_data_alloc(device, sizeof(hc_dev_t));
430 if (instance == NULL) {
431 usb_log_error("Failed to allocate HCD ddf structure.\n");
432 return ENOMEM;
433 }
434 list_initialize(&instance->devices);
435 fibril_mutex_initialize(&instance->guard);
436
437#define CHECK_RET_DEST_FREE_RETURN(ret, message...) \
438if (ret != EOK) { \
439 if (instance->hc_fun) { \
440 ddf_fun_destroy(instance->hc_fun); \
441 } \
442 usb_log_error(message); \
443 return ret; \
444} else (void)0
445
446 instance->hc_fun = ddf_fun_create(device, fun_exposed, "hc");
447 int ret = instance->hc_fun ? EOK : ENOMEM;
448 CHECK_RET_DEST_FREE_RETURN(ret,
449 "Failed to create HCD HC function: %s.\n", str_error(ret));
450 ddf_fun_set_ops(instance->hc_fun, &hc_ops);
451 hcd_t *hcd = ddf_fun_data_alloc(instance->hc_fun, sizeof(hcd_t));
452 ret = hcd ? EOK : ENOMEM;
453 CHECK_RET_DEST_FREE_RETURN(ret,
454 "Failed to allocate HCD structure: %s.\n", str_error(ret));
455
456 hcd_init(hcd, max_speed, bw, bw_count);
457
458 ret = ddf_fun_bind(instance->hc_fun);
459 CHECK_RET_DEST_FREE_RETURN(ret,
460 "Failed to bind HCD device function: %s.\n", str_error(ret));
461
462#define CHECK_RET_UNBIND_FREE_RETURN(ret, message...) \
463if (ret != EOK) { \
464 ddf_fun_unbind(instance->hc_fun); \
465 CHECK_RET_DEST_FREE_RETURN(ret, message); \
466} else (void)0
467 ret = ddf_fun_add_to_category(instance->hc_fun, USB_HC_CATEGORY);
468 CHECK_RET_UNBIND_FREE_RETURN(ret,
469 "Failed to add hc to category: %s\n", str_error(ret));
470
471 /* HC should be ok at this point (except it can't do anything) */
472 if (hc_fun)
473 *hc_fun = instance->hc_fun;
474
475 return EOK;
476}
477
478/**
479 * @}
480 */
Note: See TracBrowser for help on using the repository browser.