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