[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 |
|
---|
[cd5b878] | 58 | /**
|
---|
| 59 | * create uint8_t array with serialized descriptor
|
---|
| 60 | *
|
---|
| 61 | * @param descriptor
|
---|
| 62 | * @return newly created serializd descriptor pointer
|
---|
| 63 | */
|
---|
| 64 | void * usb_create_serialized_hub_descriptor(usb_hub_descriptor_t * descriptor) {
|
---|
[dac43be] | 65 | //base size
|
---|
| 66 | size_t size = 7;
|
---|
| 67 | //variable size according to port count
|
---|
[f35b294] | 68 | size_t var_size = (descriptor->ports_count+7)/8;
|
---|
[dac43be] | 69 | size += 2 * var_size;
|
---|
[f35b294] | 70 | uint8_t * result = malloc(size);
|
---|
[dac43be] | 71 | //size
|
---|
[cd5b878] | 72 | usb_serialize_hub_descriptor(descriptor,result);
|
---|
| 73 | return result;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * serialize descriptor into given buffer
|
---|
| 78 | *
|
---|
| 79 | * The buffer size is not checked.
|
---|
| 80 | * @param descriptor
|
---|
| 81 | * @param serialized_descriptor
|
---|
| 82 | */
|
---|
| 83 | void usb_serialize_hub_descriptor(usb_hub_descriptor_t * descriptor,
|
---|
| 84 | void * serialized_descriptor) {
|
---|
| 85 | //base size
|
---|
| 86 | uint8_t * sdescriptor = serialized_descriptor;
|
---|
| 87 | size_t size = 7;
|
---|
| 88 | //variable size according to port count
|
---|
| 89 | size_t var_size = (descriptor->ports_count+7)/8;
|
---|
| 90 | size += 2 * var_size;
|
---|
| 91 | //size
|
---|
| 92 | sdescriptor[0] = size;
|
---|
[dac43be] | 93 | //descriptor type
|
---|
[cd5b878] | 94 | sdescriptor[1] = USB_DESCTYPE_HUB;
|
---|
| 95 | sdescriptor[2] = descriptor->ports_count;
|
---|
[dac43be] | 96 | /// @fixme handling of endianness??
|
---|
[cd5b878] | 97 | sdescriptor[3] = descriptor->hub_characteristics / 256;
|
---|
| 98 | sdescriptor[4] = descriptor->hub_characteristics % 256;
|
---|
| 99 | sdescriptor[5] = descriptor->pwr_on_2_good_time;
|
---|
| 100 | sdescriptor[6] = descriptor->current_requirement;
|
---|
[dac43be] | 101 |
|
---|
| 102 | size_t i;
|
---|
| 103 | for (i = 0; i < var_size; ++i) {
|
---|
[cd5b878] | 104 | sdescriptor[7 + i] = descriptor->devices_removable[i];
|
---|
[dac43be] | 105 | }
|
---|
| 106 | for (i = 0; i < var_size; ++i) {
|
---|
[cd5b878] | 107 | sdescriptor[7 + var_size + i] = 255;
|
---|
[dac43be] | 108 | }
|
---|
| 109 | }
|
---|
| 110 |
|
---|
[cd5b878] | 111 |
|
---|
| 112 | /**
|
---|
| 113 | * create deserialized desriptor structure out of serialized descriptor
|
---|
| 114 | *
|
---|
| 115 | * The serialized descriptor must be proper usb hub descriptor,
|
---|
| 116 | * otherwise an eerror might occur.
|
---|
| 117 | *
|
---|
| 118 | * @param sdescriptor serialized descriptor
|
---|
| 119 | * @return newly created deserialized descriptor pointer
|
---|
| 120 | */
|
---|
| 121 | usb_hub_descriptor_t * usb_create_deserialized_hub_desriptor(
|
---|
[f35b294] | 122 | void * serialized_descriptor) {
|
---|
| 123 | uint8_t * sdescriptor = serialized_descriptor;
|
---|
[10096231] | 124 |
|
---|
| 125 | if (sdescriptor[1] != USB_DESCTYPE_HUB) {
|
---|
[f35b294] | 126 | usb_log_warning("trying to deserialize wrong descriptor %x\n",
|
---|
| 127 | sdescriptor[1]);
|
---|
[10096231] | 128 | return NULL;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[f35b294] | 131 | usb_hub_descriptor_t * result = malloc(sizeof(usb_hub_descriptor_t));
|
---|
[cd5b878] | 132 | if(result)
|
---|
| 133 | usb_deserialize_hub_desriptor(serialized_descriptor,result);
|
---|
| 134 | return result;
|
---|
| 135 | }
|
---|
[10096231] | 136 |
|
---|
[cd5b878] | 137 | /**
|
---|
| 138 | * deserialize descriptor into given pointer
|
---|
| 139 | *
|
---|
| 140 | * @param serialized_descriptor
|
---|
| 141 | * @param descriptor
|
---|
| 142 | * @return
|
---|
| 143 | */
|
---|
| 144 | void usb_deserialize_hub_desriptor(
|
---|
| 145 | void * serialized_descriptor, usb_hub_descriptor_t * descriptor) {
|
---|
| 146 | uint8_t * sdescriptor = serialized_descriptor;
|
---|
| 147 | descriptor->ports_count = sdescriptor[2];
|
---|
[dac43be] | 148 | /// @fixme handling of endianness??
|
---|
[cd5b878] | 149 | descriptor->hub_characteristics = sdescriptor[4] + 256 * sdescriptor[3];
|
---|
| 150 | descriptor->pwr_on_2_good_time = sdescriptor[5];
|
---|
| 151 | descriptor->current_requirement = sdescriptor[6];
|
---|
| 152 | size_t var_size = (descriptor->ports_count+7) / 8;
|
---|
| 153 | //descriptor->devices_removable = (uint8_t*) malloc(var_size);
|
---|
[09daa8b] | 154 |
|
---|
[dac43be] | 155 | size_t i;
|
---|
| 156 | for (i = 0; i < var_size; ++i) {
|
---|
[cd5b878] | 157 | descriptor->devices_removable[i] = sdescriptor[7 + i];
|
---|
[dac43be] | 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /**
|
---|
| 162 | * @}
|
---|
| 163 | */
|
---|