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 <driver.h>
|
---|
36 | #include <bool.h>
|
---|
37 | #include <errno.h>
|
---|
38 |
|
---|
39 | #include <usbhc_iface.h>
|
---|
40 | #include <usb/usbdrv.h>
|
---|
41 | #include <usb/descriptor.h>
|
---|
42 | #include <usb/devreq.h>
|
---|
43 | #include <usb/classes/hub.h>
|
---|
44 |
|
---|
45 | #include "usbhub.h"
|
---|
46 | #include "usbhub_private.h"
|
---|
47 | #include "port_status.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | size_t USB_HUB_MAX_DESCRIPTOR_SIZE = 71;
|
---|
51 |
|
---|
52 | //*********************************************
|
---|
53 | //
|
---|
54 | // various utils
|
---|
55 | //
|
---|
56 | //*********************************************
|
---|
57 |
|
---|
58 | //hub descriptor utils
|
---|
59 |
|
---|
60 | void * usb_serialize_hub_descriptor(usb_hub_descriptor_t * descriptor) {
|
---|
61 | //base size
|
---|
62 | size_t size = 7;
|
---|
63 | //variable size according to port count
|
---|
64 | size_t var_size = descriptor->ports_count / 8 + ((descriptor->ports_count % 8 > 0) ? 1 : 0);
|
---|
65 | size += 2 * var_size;
|
---|
66 | uint8_t * result = (uint8_t*) malloc(size);
|
---|
67 | //size
|
---|
68 | result[0] = size;
|
---|
69 | //descriptor type
|
---|
70 | result[1] = USB_DESCTYPE_HUB;
|
---|
71 | result[2] = descriptor->ports_count;
|
---|
72 | /// @fixme handling of endianness??
|
---|
73 | result[3] = descriptor->hub_characteristics / 256;
|
---|
74 | result[4] = descriptor->hub_characteristics % 256;
|
---|
75 | result[5] = descriptor->pwr_on_2_good_time;
|
---|
76 | result[6] = descriptor->current_requirement;
|
---|
77 |
|
---|
78 | size_t i;
|
---|
79 | for (i = 0; i < var_size; ++i) {
|
---|
80 | result[7 + i] = descriptor->devices_removable[i];
|
---|
81 | }
|
---|
82 | for (i = 0; i < var_size; ++i) {
|
---|
83 | result[7 + var_size + i] = 255;
|
---|
84 | }
|
---|
85 | return result;
|
---|
86 | }
|
---|
87 |
|
---|
88 | usb_hub_descriptor_t * usb_deserialize_hub_desriptor(void * serialized_descriptor) {
|
---|
89 | uint8_t * sdescriptor = (uint8_t*) serialized_descriptor;
|
---|
90 |
|
---|
91 | if (sdescriptor[1] != USB_DESCTYPE_HUB) {
|
---|
92 | dprintf(1,"[usb_hub] wrong descriptor %x\n",sdescriptor[1]);
|
---|
93 | return NULL;
|
---|
94 | }
|
---|
95 |
|
---|
96 | usb_hub_descriptor_t * result = usb_new(usb_hub_descriptor_t);
|
---|
97 |
|
---|
98 |
|
---|
99 | result->ports_count = sdescriptor[2];
|
---|
100 | /// @fixme handling of endianness??
|
---|
101 | result->hub_characteristics = sdescriptor[4] + 256 * sdescriptor[3];
|
---|
102 | result->pwr_on_2_good_time = sdescriptor[5];
|
---|
103 | result->current_requirement = sdescriptor[6];
|
---|
104 | size_t var_size = result->ports_count / 8 + ((result->ports_count % 8 > 0)
|
---|
105 | ? 1 : 0);
|
---|
106 | result->devices_removable = (uint8_t*) malloc(var_size);
|
---|
107 | //printf("[usb_hub] getting removable devices data \n");
|
---|
108 | size_t i;
|
---|
109 | for (i = 0; i < var_size; ++i) {
|
---|
110 | result->devices_removable[i] = sdescriptor[7 + i];
|
---|
111 | }
|
---|
112 | return result;
|
---|
113 | }
|
---|
114 |
|
---|
115 | //control transactions
|
---|
116 |
|
---|
117 | int usb_drv_sync_control_read(
|
---|
118 | int phone, usb_target_t target,
|
---|
119 | usb_device_request_setup_packet_t * request,
|
---|
120 | void * rcvd_buffer, size_t rcvd_size, size_t * actual_size
|
---|
121 | ) {
|
---|
122 | usb_handle_t handle;
|
---|
123 | int opResult;
|
---|
124 | //setup
|
---|
125 | opResult = usb_drv_async_control_read_setup(phone, target,
|
---|
126 | request, sizeof (usb_device_request_setup_packet_t),
|
---|
127 | &handle);
|
---|
128 | if (opResult != EOK) {
|
---|
129 | return opResult;
|
---|
130 | }
|
---|
131 | opResult = usb_drv_async_wait_for(handle);
|
---|
132 | if (opResult != EOK) {
|
---|
133 | return opResult;
|
---|
134 | }
|
---|
135 | //read
|
---|
136 | opResult = usb_drv_async_control_read_data(phone, target,
|
---|
137 | rcvd_buffer, rcvd_size, actual_size,
|
---|
138 | &handle);
|
---|
139 | if (opResult != EOK) {
|
---|
140 | return opResult;
|
---|
141 | }
|
---|
142 | opResult = usb_drv_async_wait_for(handle);
|
---|
143 | if (opResult != EOK) {
|
---|
144 | return opResult;
|
---|
145 | }
|
---|
146 | //finalize
|
---|
147 | opResult = usb_drv_async_control_read_status(phone, target,
|
---|
148 | &handle);
|
---|
149 | if (opResult != EOK) {
|
---|
150 | return opResult;
|
---|
151 | }
|
---|
152 | opResult = usb_drv_async_wait_for(handle);
|
---|
153 | if (opResult != EOK) {
|
---|
154 | return opResult;
|
---|
155 | }
|
---|
156 | return EOK;
|
---|
157 | }
|
---|
158 |
|
---|
159 | int usb_drv_sync_control_write(
|
---|
160 | int phone, usb_target_t target,
|
---|
161 | usb_device_request_setup_packet_t * request,
|
---|
162 | void * sent_buffer, size_t sent_size
|
---|
163 | ) {
|
---|
164 | usb_handle_t handle;
|
---|
165 | int opResult;
|
---|
166 | //setup
|
---|
167 | opResult = usb_drv_async_control_write_setup(phone, target,
|
---|
168 | request, sizeof (usb_device_request_setup_packet_t),
|
---|
169 | &handle);
|
---|
170 | if (opResult != EOK) {
|
---|
171 | return opResult;
|
---|
172 | }
|
---|
173 | opResult = usb_drv_async_wait_for(handle);
|
---|
174 | if (opResult != EOK) {
|
---|
175 | return opResult;
|
---|
176 | }
|
---|
177 | //write
|
---|
178 | opResult = usb_drv_async_control_write_data(phone, target,
|
---|
179 | sent_buffer, sent_size,
|
---|
180 | &handle);
|
---|
181 | if (opResult != EOK) {
|
---|
182 | return opResult;
|
---|
183 | }
|
---|
184 | opResult = usb_drv_async_wait_for(handle);
|
---|
185 | if (opResult != EOK) {
|
---|
186 | return opResult;
|
---|
187 | }
|
---|
188 | //finalize
|
---|
189 | opResult = usb_drv_async_control_write_status(phone, target,
|
---|
190 | &handle);
|
---|
191 | if (opResult != EOK) {
|
---|
192 | return opResult;
|
---|
193 | }
|
---|
194 | opResult = usb_drv_async_wait_for(handle);
|
---|
195 | if (opResult != EOK) {
|
---|
196 | return opResult;
|
---|
197 | }
|
---|
198 | return EOK;
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 |
|
---|
203 |
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * @}
|
---|
207 | */
|
---|