source: mainline/uspace/drv/usbhub/utils.c@ 8dd039a

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

usb hub code refactoring

  • Property mode set to 100644
File size: 7.1 KB
RevLine 
[dac43be]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 libusb usb
30 * @{
31 */
32/** @file
[e080332]33 * @brief various utilities
[dac43be]34 */
[4317827]35#include <driver.h>
[94c19b8]36#include <bool.h>
37#include <errno.h>
38
[dac43be]39#include <usbhc_iface.h>
[4317827]40#include <usb/usbdrv.h>
[dac43be]41#include <usb/descriptor.h>
[94c19b8]42#include <usb/devreq.h>
[dac43be]43#include <usb/classes/hub.h>
[94c19b8]44
[4317827]45#include "usbhub.h"
[b5ec347]46#include "usbhub_private.h"
47#include "port_status.h"
[94c19b8]48
[dac43be]49
50size_t USB_HUB_MAX_DESCRIPTOR_SIZE = 71;
51
52//*********************************************
53//
54// various utils
55//
56//*********************************************
57
[b5ec347]58//hub descriptor utils
[98d06b8]59
[dac43be]60void * 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
88usb_hub_descriptor_t * usb_deserialize_hub_desriptor(void * serialized_descriptor) {
89 uint8_t * sdescriptor = (uint8_t*) serialized_descriptor;
[10096231]90
91 if (sdescriptor[1] != USB_DESCTYPE_HUB) {
[e080332]92 dprintf(1,"[usb_hub] wrong descriptor %x\n",sdescriptor[1]);
[10096231]93 return NULL;
94 }
95
[b5ec347]96 usb_hub_descriptor_t * result = usb_new(usb_hub_descriptor_t);
[10096231]97
98
[dac43be]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];
[e080332]104 size_t var_size = result->ports_count / 8 + ((result->ports_count % 8 > 0)
105 ? 1 : 0);
[dac43be]106 result->devices_removable = (uint8_t*) malloc(var_size);
[10096231]107 //printf("[usb_hub] getting removable devices data \n");
[dac43be]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
[b5ec347]115//control transactions
[98d06b8]116
[b5ec347]117int usb_drv_sync_control_read(
[98d06b8]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 ) {
[b5ec347]122 usb_handle_t handle;
123 int opResult;
124 //setup
125 opResult = usb_drv_async_control_read_setup(phone, target,
[98d06b8]126 request, sizeof (usb_device_request_setup_packet_t),
127 &handle);
128 if (opResult != EOK) {
[b5ec347]129 return opResult;
130 }
131 opResult = usb_drv_async_wait_for(handle);
[98d06b8]132 if (opResult != EOK) {
[b5ec347]133 return opResult;
134 }
135 //read
136 opResult = usb_drv_async_control_read_data(phone, target,
[98d06b8]137 rcvd_buffer, rcvd_size, actual_size,
138 &handle);
139 if (opResult != EOK) {
[b5ec347]140 return opResult;
141 }
142 opResult = usb_drv_async_wait_for(handle);
[98d06b8]143 if (opResult != EOK) {
[b5ec347]144 return opResult;
145 }
146 //finalize
147 opResult = usb_drv_async_control_read_status(phone, target,
[98d06b8]148 &handle);
149 if (opResult != EOK) {
[b5ec347]150 return opResult;
151 }
152 opResult = usb_drv_async_wait_for(handle);
[98d06b8]153 if (opResult != EOK) {
[b5ec347]154 return opResult;
155 }
156 return EOK;
157}
158
159int usb_drv_sync_control_write(
[98d06b8]160 int phone, usb_target_t target,
161 usb_device_request_setup_packet_t * request,
162 void * sent_buffer, size_t sent_size
163 ) {
[b5ec347]164 usb_handle_t handle;
165 int opResult;
166 //setup
167 opResult = usb_drv_async_control_write_setup(phone, target,
[98d06b8]168 request, sizeof (usb_device_request_setup_packet_t),
169 &handle);
170 if (opResult != EOK) {
[b5ec347]171 return opResult;
172 }
173 opResult = usb_drv_async_wait_for(handle);
[98d06b8]174 if (opResult != EOK) {
[b5ec347]175 return opResult;
176 }
177 //write
178 opResult = usb_drv_async_control_write_data(phone, target,
[98d06b8]179 sent_buffer, sent_size,
180 &handle);
181 if (opResult != EOK) {
[b5ec347]182 return opResult;
183 }
184 opResult = usb_drv_async_wait_for(handle);
[98d06b8]185 if (opResult != EOK) {
[b5ec347]186 return opResult;
187 }
188 //finalize
189 opResult = usb_drv_async_control_write_status(phone, target,
[98d06b8]190 &handle);
191 if (opResult != EOK) {
[b5ec347]192 return opResult;
193 }
194 opResult = usb_drv_async_wait_for(handle);
[98d06b8]195 if (opResult != EOK) {
[b5ec347]196 return opResult;
197 }
198 return EOK;
199}
200
201
[e080332]202/*
203 * method for testing port status bitmap
204
[98d06b8]205static void usb_hub_test_port_status(void) {
206 printf("[usb_hub] -------------port status test---------\n");
207 usb_port_status_t status = 0;
208
209 //printf("original status %d (should be 0)\n",(uint32_t)status);
210 usb_port_set_bit(&status, 1, 1);
211 //printf("%d =?= 2\n",(uint32_t)status);
212 if (status != 2) {
213 printf("[usb_port_status] test failed: wrong set of bit 1\n");
214 return;
215 }
216 usb_port_set_bit(&status, 3, 1);
217 if (status != 10) {
218 printf("[usb_port_status] test failed: wrong set of bit 3\n");
219 return;
220 }
221
222 usb_port_set_bit(&status, 15, 1);
223 if (status != 10 + (1 << 15)) {
224 printf("[usb_port_status] test failed: wrong set of bit 15\n");
225 return;
226 }
227 usb_port_set_bit(&status, 1, 0);
228 if (status != 8 + (1 << 15)) {
229 printf("[usb_port_status] test failed: wrong unset of bit 1\n");
230 return;
231 }
232 int i;
233 for (i = 0; i < 32; ++i) {
234 if (i == 3 || i == 15) {
235 if (!usb_port_get_bit(&status, i)) {
236 printf("[usb_port_status] test failed: wrong bit at %d\n", i);
237 }
238 } else {
239 if (usb_port_get_bit(&status, i)) {
240 printf("[usb_port_status] test failed: wrong bit at %d\n", i);
241 }
242 }
243 }
244
245 printf("test ok\n");
246
247
248 //printf("%d =?= 10\n",(uint32_t)status);
249
250 //printf("this should be 0: %d \n",usb_port_get_bit(&status,0));
251 //printf("this should be 1: %d \n",usb_port_get_bit(&status,1));
252 //printf("this should be 0: %d \n",usb_port_get_bit(&status,2));
253 //printf("this should be 1: %d \n",usb_port_get_bit(&status,3));
254 //printf("this should be 0: %d \n",usb_port_get_bit(&status,4));
255
256}
[e080332]257*/
[98d06b8]258
[dac43be]259
260/**
261 * @}
262 */
Note: See TracBrowser for help on using the repository browser.