Changeset e50cd7f in mainline for uspace/drv/usbhub/utils.c


Ignore:
Timestamp:
2011-04-17T19:17:55Z (14 years ago)
Author:
Matej Klonfar <maklf@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
63517c2, cfbbe1d3
Parents:
ef354b6 (diff), 8595577b (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

new report structure fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/usbhub/utils.c

    ref354b6 re50cd7f  
    5656//hub descriptor utils
    5757
    58 void * usb_serialize_hub_descriptor(usb_hub_descriptor_t * descriptor) {
     58/**
     59 * create uint8_t array with serialized descriptor
     60 *
     61 * @param descriptor
     62 * @return newly created serializd descriptor pointer
     63 */
     64void * usb_create_serialized_hub_descriptor(usb_hub_descriptor_t * descriptor) {
    5965        //base size
    6066        size_t size = 7;
    6167        //variable size according to port count
    62         size_t var_size = descriptor->ports_count / 8 + ((descriptor->ports_count % 8 > 0) ? 1 : 0);
     68        size_t var_size = (descriptor->ports_count+7)/8;
    6369        size += 2 * var_size;
    64         uint8_t * result = (uint8_t*) malloc(size);
     70        uint8_t * result = malloc(size);
    6571        //size
    66         result[0] = size;
     72        if(result)
     73                usb_serialize_hub_descriptor(descriptor,result);
     74        return result;
     75}
     76
     77/**
     78 * serialize descriptor into given buffer
     79 *
     80 * The buffer size is not checked.
     81 * @param descriptor
     82 * @param serialized_descriptor
     83 */
     84void usb_serialize_hub_descriptor(usb_hub_descriptor_t * descriptor,
     85    void * serialized_descriptor) {
     86        //base size
     87        uint8_t * sdescriptor = serialized_descriptor;
     88        size_t size = 7;
     89        //variable size according to port count
     90        size_t var_size = (descriptor->ports_count+7)/8;
     91        size += 2 * var_size;
     92        //size
     93        sdescriptor[0] = size;
    6794        //descriptor type
    68         result[1] = USB_DESCTYPE_HUB;
    69         result[2] = descriptor->ports_count;
     95        sdescriptor[1] = USB_DESCTYPE_HUB;
     96        sdescriptor[2] = descriptor->ports_count;
    7097        /// @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;
     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;
    75102
    76103        size_t i;
    77104        for (i = 0; i < var_size; ++i) {
    78                 result[7 + i] = descriptor->devices_removable[i];
     105                sdescriptor[7 + i] = descriptor->devices_removable[i];
    79106        }
    80107        for (i = 0; i < var_size; ++i) {
    81                 result[7 + var_size + i] = 255;
     108                sdescriptor[7 + var_size + i] = 255;
    82109        }
     110}
     111
     112
     113/**
     114 * create deserialized desriptor structure out of serialized descriptor
     115 *
     116 * The serialized descriptor must be proper usb hub descriptor,
     117 * otherwise an eerror might occur.
     118 *
     119 * @param sdescriptor serialized descriptor
     120 * @return newly created deserialized descriptor pointer
     121 */
     122usb_hub_descriptor_t * usb_create_deserialized_hub_desriptor(
     123void * serialized_descriptor) {
     124        uint8_t * sdescriptor = serialized_descriptor;
     125
     126        if (sdescriptor[1] != USB_DESCTYPE_HUB) {
     127                usb_log_warning("trying to deserialize wrong descriptor %x\n",
     128                    sdescriptor[1]);
     129                return NULL;
     130        }
     131
     132        usb_hub_descriptor_t * result = malloc(sizeof(usb_hub_descriptor_t));
     133        if(result)
     134                usb_deserialize_hub_desriptor(serialized_descriptor,result);
    83135        return result;
    84136}
    85137
    86 usb_hub_descriptor_t * usb_deserialize_hub_desriptor(void * serialized_descriptor) {
    87         uint8_t * sdescriptor = (uint8_t*) serialized_descriptor;
    88 
    89         if (sdescriptor[1] != USB_DESCTYPE_HUB) {
    90                 usb_log_warning("trying to deserialize wrong descriptor %x\n",sdescriptor[1]);
    91                 return NULL;
    92         }
    93 
    94         usb_hub_descriptor_t * result = usb_new(usb_hub_descriptor_t);
    95        
    96 
    97         result->ports_count = sdescriptor[2];
     138/**
     139 * deserialize descriptor into given pointer
     140 *
     141 * @param serialized_descriptor
     142 * @param descriptor
     143 * @return
     144 */
     145void usb_deserialize_hub_desriptor(
     146void * serialized_descriptor, usb_hub_descriptor_t * descriptor) {
     147        uint8_t * sdescriptor = serialized_descriptor;
     148        descriptor->ports_count = sdescriptor[2];
    98149        /// @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];
    102         size_t var_size = result->ports_count / 8 + ((result->ports_count % 8 > 0)
    103                         ? 1 : 0);
    104         result->devices_removable = (uint8_t*) malloc(var_size);
     150        descriptor->hub_characteristics = sdescriptor[4] + 256 * sdescriptor[3];
     151        descriptor->pwr_on_2_good_time = sdescriptor[5];
     152        descriptor->current_requirement = sdescriptor[6];
     153        size_t var_size = (descriptor->ports_count+7) / 8;
     154        //descriptor->devices_removable = (uint8_t*) malloc(var_size);
    105155
    106156        size_t i;
    107157        for (i = 0; i < var_size; ++i) {
    108                 result->devices_removable[i] = sdescriptor[7 + i];
     158                descriptor->devices_removable[i] = sdescriptor[7 + i];
    109159        }
    110         return result;
    111160}
    112 
    113 
    114161
    115162/**
Note: See TracChangeset for help on using the changeset viewer.