utils.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Matus Dekanek
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00035 #include <ddf/driver.h>
00036 #include <bool.h>
00037 #include <errno.h>
00038 
00039 #include <usbhc_iface.h>
00040 #include <usb/descriptor.h>
00041 #include <usb/classes/hub.h>
00042 
00043 #include "usbhub.h"
00044 #include "usbhub_private.h"
00045 #include "port_status.h"
00046 
00047 
00048 size_t USB_HUB_MAX_DESCRIPTOR_SIZE = 71;
00049 
00050 //*********************************************
00051 //
00052 //  various utils
00053 //
00054 //*********************************************
00055 
00056 //hub descriptor utils
00057 
00064 void * usb_create_serialized_hub_descriptor(usb_hub_descriptor_t *descriptor) {
00065         //base size
00066         size_t size = 7;
00067         //variable size according to port count
00068         size_t var_size = (descriptor->ports_count + 7) / 8;
00069         size += 2 * var_size;
00070         uint8_t * result = malloc(size);
00071         //size
00072         if (result)
00073                 usb_serialize_hub_descriptor(descriptor, result);
00074         return result;
00075 }
00076 
00084 void usb_serialize_hub_descriptor(usb_hub_descriptor_t *descriptor,
00085     void * serialized_descriptor) {
00086         //base size
00087         uint8_t * sdescriptor = serialized_descriptor;
00088         size_t size = 7;
00089         //variable size according to port count
00090         size_t var_size = (descriptor->ports_count + 7) / 8;
00091         size += 2 * var_size;
00092         //size
00093         sdescriptor[0] = size;
00094         //descriptor type
00095         sdescriptor[1] = USB_DESCTYPE_HUB;
00096         sdescriptor[2] = descriptor->ports_count;
00098         sdescriptor[3] = descriptor->hub_characteristics / 256;
00099         sdescriptor[4] = descriptor->hub_characteristics % 256;
00100         sdescriptor[5] = descriptor->pwr_on_2_good_time;
00101         sdescriptor[6] = descriptor->current_requirement;
00102 
00103         size_t i;
00104         for (i = 0; i < var_size; ++i) {
00105                 sdescriptor[7 + i] = descriptor->devices_removable[i];
00106         }
00107         for (i = 0; i < var_size; ++i) {
00108                 sdescriptor[7 + var_size + i] = 255;
00109         }
00110 }
00111 
00121 usb_hub_descriptor_t * usb_create_deserialized_hub_desriptor(
00122     void *serialized_descriptor) {
00123         uint8_t * sdescriptor = serialized_descriptor;
00124 
00125         if (sdescriptor[1] != USB_DESCTYPE_HUB) {
00126                 usb_log_warning("trying to deserialize wrong descriptor %x\n",
00127                     sdescriptor[1]);
00128                 return NULL;
00129         }
00130 
00131         usb_hub_descriptor_t * result = malloc(sizeof (usb_hub_descriptor_t));
00132         if (result)
00133                 usb_deserialize_hub_desriptor(serialized_descriptor, result);
00134         return result;
00135 }
00136 
00144 void usb_deserialize_hub_desriptor(
00145     void * serialized_descriptor, usb_hub_descriptor_t *descriptor) {
00146         uint8_t * sdescriptor = serialized_descriptor;
00147         descriptor->ports_count = sdescriptor[2];
00149         descriptor->hub_characteristics = sdescriptor[4] + 256 * sdescriptor[3];
00150         descriptor->pwr_on_2_good_time = sdescriptor[5];
00151         descriptor->current_requirement = sdescriptor[6];
00152         size_t var_size = (descriptor->ports_count + 7) / 8;
00153         //descriptor->devices_removable = (uint8_t*) malloc(var_size);
00154 
00155         size_t i;
00156         for (i = 0; i < var_size; ++i) {
00157                 descriptor->devices_removable[i] = sdescriptor[7 + i];
00158         }
00159 }
00160 

Generated on Thu Jun 2 07:45:44 2011 for HelenOS/USB by  doxygen 1.4.7