1 | /*
|
---|
2 | * Copyright (c) 2010 Matus Dekanek
|
---|
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 drvusbhub
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief various utilities
|
---|
34 | */
|
---|
35 | #include <ddf/driver.h>
|
---|
36 | #include <errno.h>
|
---|
37 |
|
---|
38 | #include <usbhc_iface.h>
|
---|
39 | #include <usb/descriptor.h>
|
---|
40 | #include <usb/classes/hub.h>
|
---|
41 | #include <usb/debug.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | #include "usbhub.h"
|
---|
45 | #include "utils.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Deserialize descriptor into given pointer
|
---|
50 | *
|
---|
51 | * @param serialized_descriptor
|
---|
52 | * @param descriptor
|
---|
53 | * @return
|
---|
54 | */
|
---|
55 | int usb_deserialize_hub_desriptor(
|
---|
56 | void *serialized_descriptor, size_t size, usb_hub_descriptor_t *descriptor)
|
---|
57 | {
|
---|
58 | uint8_t *sdescriptor = serialized_descriptor;
|
---|
59 |
|
---|
60 | if (sdescriptor[1] != USB_DESCTYPE_HUB) {
|
---|
61 | usb_log_error("Trying to deserialize wrong descriptor %x\n",
|
---|
62 | sdescriptor[1]);
|
---|
63 | return EINVAL;
|
---|
64 | }
|
---|
65 | if (size < 7) {
|
---|
66 | usb_log_error("Serialized descriptor too small.\n");
|
---|
67 | return EOVERFLOW;
|
---|
68 | }
|
---|
69 |
|
---|
70 | descriptor->ports_count = sdescriptor[2];
|
---|
71 | descriptor->hub_characteristics = sdescriptor[3] + 256 * sdescriptor[4];
|
---|
72 | descriptor->pwr_on_2_good_time = sdescriptor[5];
|
---|
73 | descriptor->current_requirement = sdescriptor[6];
|
---|
74 | const size_t var_size = (descriptor->ports_count + 7) / 8;
|
---|
75 |
|
---|
76 | if (size < (7 + var_size)) {
|
---|
77 | usb_log_error("Serialized descriptor too small.\n");
|
---|
78 | return EOVERFLOW;
|
---|
79 | }
|
---|
80 | size_t i = 0;
|
---|
81 | for (; i < var_size; ++i) {
|
---|
82 | descriptor->devices_removable[i] = sdescriptor[7 + i];
|
---|
83 | }
|
---|
84 | return EOK;
|
---|
85 | }
|
---|
86 | /*----------------------------------------------------------------------------*/
|
---|
87 | /**
|
---|
88 | * @}
|
---|
89 | */
|
---|