source: mainline/uspace/drv/usbhub/utils.c@ c1693dae

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c1693dae was c1693dae, checked in by Matus Dekanek <smekideki@…>, 14 years ago

minor fixes and enhancements

  • Property mode set to 100644
File size: 4.8 KB
Line 
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 <bool.h>
37#include <errno.h>
38
39#include <usbhc_iface.h>
40#include <usb/descriptor.h>
41#include <usb/classes/hub.h>
42
43#include "usbhub.h"
44#include "usbhub_private.h"
45#include "port_status.h"
46
47
48size_t USB_HUB_MAX_DESCRIPTOR_SIZE = 71;
49
50//*********************************************
51//
52// various utils
53//
54//*********************************************
55
56//hub descriptor utils
57
58/**
59 * create uint8_t array with serialized descriptor
60 *
61 * @param descriptor
62 * @return newly created serializd descriptor pointer
63 */
64void * usb_create_serialized_hub_descriptor(usb_hub_descriptor_t * descriptor) {
65 //base size
66 size_t size = 7;
67 //variable size according to port count
68 size_t var_size = (descriptor->ports_count+7)/8;
69 size += 2 * var_size;
70 uint8_t * result = malloc(size);
71 //size
72 if(result)
73 usb_serialize_hub_descriptor(descriptor,result);
74 return result;
75}
76
77/**
78 * serialize descriptor into given buffer
79 *
80 * The buffer size is not checked.
81 * @param descriptor
82 * @param serialized_descriptor
83 */
84void usb_serialize_hub_descriptor(usb_hub_descriptor_t * descriptor,
85 void * serialized_descriptor) {
86 //base size
87 uint8_t * sdescriptor = serialized_descriptor;
88 size_t size = 7;
89 //variable size according to port count
90 size_t var_size = (descriptor->ports_count+7)/8;
91 size += 2 * var_size;
92 //size
93 sdescriptor[0] = size;
94 //descriptor type
95 sdescriptor[1] = USB_DESCTYPE_HUB;
96 sdescriptor[2] = descriptor->ports_count;
97 /// @fixme handling of endianness??
98 sdescriptor[3] = descriptor->hub_characteristics / 256;
99 sdescriptor[4] = descriptor->hub_characteristics % 256;
100 sdescriptor[5] = descriptor->pwr_on_2_good_time;
101 sdescriptor[6] = descriptor->current_requirement;
102
103 size_t i;
104 for (i = 0; i < var_size; ++i) {
105 sdescriptor[7 + i] = descriptor->devices_removable[i];
106 }
107 for (i = 0; i < var_size; ++i) {
108 sdescriptor[7 + var_size + i] = 255;
109 }
110}
111
112
113/**
114 * create deserialized desriptor structure out of serialized descriptor
115 *
116 * The serialized descriptor must be proper usb hub descriptor,
117 * otherwise an eerror might occur.
118 *
119 * @param sdescriptor serialized descriptor
120 * @return newly created deserialized descriptor pointer
121 */
122usb_hub_descriptor_t * usb_create_deserialized_hub_desriptor(
123void * serialized_descriptor) {
124 uint8_t * sdescriptor = serialized_descriptor;
125
126 if (sdescriptor[1] != USB_DESCTYPE_HUB) {
127 usb_log_warning("trying to deserialize wrong descriptor %x\n",
128 sdescriptor[1]);
129 return NULL;
130 }
131
132 usb_hub_descriptor_t * result = malloc(sizeof(usb_hub_descriptor_t));
133 if(result)
134 usb_deserialize_hub_desriptor(serialized_descriptor,result);
135 return result;
136}
137
138/**
139 * deserialize descriptor into given pointer
140 *
141 * @param serialized_descriptor
142 * @param descriptor
143 * @return
144 */
145void usb_deserialize_hub_desriptor(
146void * serialized_descriptor, usb_hub_descriptor_t * descriptor) {
147 uint8_t * sdescriptor = serialized_descriptor;
148 descriptor->ports_count = sdescriptor[2];
149 /// @fixme handling of endianness??
150 descriptor->hub_characteristics = sdescriptor[4] + 256 * sdescriptor[3];
151 descriptor->pwr_on_2_good_time = sdescriptor[5];
152 descriptor->current_requirement = sdescriptor[6];
153 size_t var_size = (descriptor->ports_count+7) / 8;
154 //descriptor->devices_removable = (uint8_t*) malloc(var_size);
155
156 size_t i;
157 for (i = 0; i < var_size; ++i) {
158 descriptor->devices_removable[i] = sdescriptor[7 + i];
159 }
160}
161
162/**
163 * @}
164 */
Note: See TracBrowser for help on using the repository browser.