source: mainline/uspace/drv/bus/usb/usbhub/utils.c@ 75eb6735

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 75eb6735 was 193da9d6, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

usbhub: Rename usbhub_private.h ⇒ utils.h.

Add error checks, comments, and slight refactoring.

  • Property mode set to 100644
File size: 4.5 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
[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"
[193da9d6]44#include "utils.h"
[94c19b8]45
[dac43be]46
47size_t USB_HUB_MAX_DESCRIPTOR_SIZE = 71;
48
49//*********************************************
50//
51// various utils
52//
53//*********************************************
54
[b5ec347]55//hub descriptor utils
[98d06b8]56
[cd5b878]57/**
58 * create uint8_t array with serialized descriptor
59 *
60 * @param descriptor
61 * @return newly created serializd descriptor pointer
62 */
[6ab7f3e9]63void * usb_create_serialized_hub_descriptor(usb_hub_descriptor_t *descriptor) {
[dac43be]64 //base size
65 size_t size = 7;
66 //variable size according to port count
[6ab7f3e9]67 size_t var_size = (descriptor->ports_count + 7) / 8;
[dac43be]68 size += 2 * var_size;
[ee9ea16]69 uint8_t *result = malloc(size);
[dac43be]70 //size
[6ab7f3e9]71 if (result)
72 usb_serialize_hub_descriptor(descriptor, result);
[cd5b878]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 */
[6ab7f3e9]83void usb_serialize_hub_descriptor(usb_hub_descriptor_t *descriptor,
[ee9ea16]84 void *serialized_descriptor)
85{
[cd5b878]86 //base size
[ee9ea16]87 uint8_t *sdescriptor = serialized_descriptor;
[cd5b878]88 size_t size = 7;
89 //variable size according to port count
[6ab7f3e9]90 size_t var_size = (descriptor->ports_count + 7) / 8;
[cd5b878]91 size += 2 * var_size;
92 //size
93 sdescriptor[0] = size;
[dac43be]94 //descriptor type
[cd5b878]95 sdescriptor[1] = USB_DESCTYPE_HUB;
96 sdescriptor[2] = descriptor->ports_count;
[dac43be]97 /// @fixme handling of endianness??
[cd5b878]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;
[dac43be]102
103 size_t i;
104 for (i = 0; i < var_size; ++i) {
[cd5b878]105 sdescriptor[7 + i] = descriptor->devices_removable[i];
[dac43be]106 }
107 for (i = 0; i < var_size; ++i) {
[cd5b878]108 sdescriptor[7 + var_size + i] = 255;
[dac43be]109 }
110}
[5fd0dc23]111/*----------------------------------------------------------------------------*/
[cd5b878]112/**
[5fd0dc23]113 * Deserialize descriptor into given pointer
[cd5b878]114 *
[5fd0dc23]115 * @param serialized_descriptor
116 * @param descriptor
117 * @return
[cd5b878]118 */
[5fd0dc23]119int usb_deserialize_hub_desriptor(
120 void *serialized_descriptor, size_t size, usb_hub_descriptor_t *descriptor)
121{
[ee9ea16]122 uint8_t *sdescriptor = serialized_descriptor;
[10096231]123
124 if (sdescriptor[1] != USB_DESCTYPE_HUB) {
[5fd0dc23]125 usb_log_error("Trying to deserialize wrong descriptor %x\n",
[f35b294]126 sdescriptor[1]);
[5fd0dc23]127 return EINVAL;
128 }
129 if (size < 7) {
130 usb_log_error("Serialized descriptor too small.\n");
131 return EOVERFLOW;
[10096231]132 }
133
[cd5b878]134 descriptor->ports_count = sdescriptor[2];
[dbb9663]135 descriptor->hub_characteristics = sdescriptor[3] + 256 * sdescriptor[4];
[cd5b878]136 descriptor->pwr_on_2_good_time = sdescriptor[5];
137 descriptor->current_requirement = sdescriptor[6];
[5fd0dc23]138 const size_t var_size = (descriptor->ports_count + 7) / 8;
[09daa8b]139
[5fd0dc23]140 if (size < (7 + var_size)) {
141 usb_log_error("Serialized descriptor too small.\n");
142 return EOVERFLOW;
143 }
144 size_t i = 0;
145 for (; i < var_size; ++i) {
[cd5b878]146 descriptor->devices_removable[i] = sdescriptor[7 + i];
[dac43be]147 }
[5fd0dc23]148 return EOK;
[dac43be]149}
[5fd0dc23]150/*----------------------------------------------------------------------------*/
[dac43be]151/**
152 * @}
153 */
Note: See TracBrowser for help on using the repository browser.