Changeset 8a64320e in mainline for uspace/drv/nic/ar9271/wmi.c


Ignore:
Timestamp:
2015-04-23T23:40:14Z (10 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
dcba819
Parents:
09044cb
Message:

pre-merge coding style cleanup and code review

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/nic/ar9271/wmi.c

    r09044cb r8a64320e  
    3434
    3535#include <usb/debug.h>
    36 
    3736#include <malloc.h>
    3837#include <mem.h>
    3938#include <byteorder.h>
    40 
    4139#include "wmi.h"
    4240
    43 /**
    44  * WMI registry read.
    45  *
     41/** WMI registry read.
     42 *
    4643 * @param htc_device HTC device structure.
    4744 * @param reg_offset Registry offset (address) to be read.
    48  * @param res Stored result.
    49  *
    50  * @return EOK if succeed, negative error code otherwise.
     45 * @param res        Stored result.
     46 *
     47 * @return EOK if succeed, negative error code otherwise.
     48 *
    5149 */
    5250int wmi_reg_read(htc_device_t *htc_device, uint32_t reg_offset, uint32_t *res)
     
    5452        uint32_t cmd_value = host2uint32_t_be(reg_offset);
    5553       
    56         void *resp_buffer = 
    57                 malloc(htc_device->ath_device->ctrl_response_length);
    58        
    59         int rc = wmi_send_command(htc_device, WMI_REG_READ, 
    60                 (uint8_t *) &cmd_value, sizeof(cmd_value), resp_buffer);
    61        
    62         if(rc != EOK) {
     54        void *resp_buffer =
     55            malloc(htc_device->ath_device->ctrl_response_length);
     56       
     57        int rc = wmi_send_command(htc_device, WMI_REG_READ,
     58            (uint8_t *) &cmd_value, sizeof(cmd_value), resp_buffer);
     59       
     60        if (rc != EOK) {
    6361                usb_log_error("Failed to read registry value.\n");
    6462                return rc;
    6563        }
    6664       
    67         uint32_t *resp_value = (uint32_t *) ((void*) resp_buffer +
    68                 sizeof(htc_frame_header_t) +
    69                 sizeof(wmi_command_header_t));
     65        uint32_t *resp_value = (uint32_t *) ((void *) resp_buffer +
     66            sizeof(htc_frame_header_t) + sizeof(wmi_command_header_t));
    7067       
    7168        *res = uint32_t_be2host(*resp_value);
     
    7471}
    7572
    76 /**
    77  * WMI registry write.
    78  *
    79  * @param htc_device HTC device structure.
    80  * @param reg_offset Registry offset (address) to be written.
    81  * @param val Value to be written
    82  *
    83  * @return EOK if succeed, negative error code otherwise.
     73/** WMI registry write.
     74 *
     75 * @param htc_device HTC device structure.
     76 * @param reg_offset Registry offset (address) to be written.
     77 * @param val        Value to be written
     78 *
     79 * @return EOK if succeed, negative error code otherwise.
     80 *
    8481 */
    8582int wmi_reg_write(htc_device_t *htc_device, uint32_t reg_offset, uint32_t val)
    8683{
    8784        uint32_t cmd_buffer[] = {
    88             host2uint32_t_be(reg_offset),
    89             host2uint32_t_be(val)
     85                host2uint32_t_be(reg_offset),
     86                host2uint32_t_be(val)
    9087        };
    9188       
    92         void *resp_buffer = 
    93                 malloc(htc_device->ath_device->ctrl_response_length);
    94        
    95         int rc = wmi_send_command(htc_device, WMI_REG_WRITE, 
    96                 (uint8_t *) &cmd_buffer, sizeof(cmd_buffer), resp_buffer);
     89        void *resp_buffer =
     90            malloc(htc_device->ath_device->ctrl_response_length);
     91       
     92        int rc = wmi_send_command(htc_device, WMI_REG_WRITE,
     93            (uint8_t *) &cmd_buffer, sizeof(cmd_buffer), resp_buffer);
    9794       
    9895        free(resp_buffer);
    9996       
    100         if(rc != EOK) {
     97        if (rc != EOK) {
    10198                usb_log_error("Failed to write registry value.\n");
    10299                return rc;
     
    106103}
    107104
    108 /**
    109  * WMI registry set or clear specified bits.
    110  *
    111  * @param htc_device HTC device structure.
    112  * @param reg_offset Registry offset (address) to be written.
    113  * @param set_bit Bit to be set.
    114  * @param clear_bit Bit to be cleared.
    115  *
    116  * @return EOK if succeed, negative error code otherwise.
    117  */
    118 int wmi_reg_set_clear_bit(htc_device_t *htc_device, uint32_t reg_offset, 
    119         uint32_t set_bit, uint32_t clear_bit)
     105/** WMI registry set or clear specified bits.
     106 *
     107 * @param htc_device HTC device structure.
     108 * @param reg_offset Registry offset (address) to be written.
     109 * @param set_bit    Bit to be set.
     110 * @param clear_bit  Bit to be cleared.
     111 *
     112 * @return EOK if succeed, negative error code otherwise.
     113 *
     114 */
     115int wmi_reg_set_clear_bit(htc_device_t *htc_device, uint32_t reg_offset,
     116    uint32_t set_bit, uint32_t clear_bit)
    120117{
    121118        uint32_t value;
    122119       
    123120        int rc = wmi_reg_read(htc_device, reg_offset, &value);
    124         if(rc != EOK) {
     121        if (rc != EOK) {
    125122                usb_log_error("Failed to read registry value in RMW "
    126                         "function.\n");
     123                    "function.\n");
    127124                return rc;
    128125        }
     
    132129       
    133130        rc = wmi_reg_write(htc_device, reg_offset, value);
    134         if(rc != EOK) {
     131        if (rc != EOK) {
    135132                usb_log_error("Failed to write registry value in RMW "
    136                         "function.\n");
    137                 return rc;
    138         }
    139        
    140         return rc;
    141 }
    142 
    143 /**
    144  * WMI registry set specified bit.
    145  *
    146  * @param htc_device HTC device structure.
    147  * @param reg_offset Registry offset (address) to be written.
    148  * @param set_bit Bit to be set.
    149  *
    150  * @return EOK if succeed, negative error code otherwise.
    151  */
    152 int wmi_reg_set_bit(htc_device_t *htc_device, uint32_t reg_offset, 
    153         uint32_t set_bit)
     133                    "function.\n");
     134                return rc;
     135        }
     136       
     137        return rc;
     138}
     139
     140/** WMI registry set specified bit.
     141 *
     142 * @param htc_device HTC device structure.
     143 * @param reg_offset Registry offset (address) to be written.
     144 * @param set_bit    Bit to be set.
     145 *
     146 * @return EOK if succeed, negative error code otherwise.
     147 *
     148 */
     149int wmi_reg_set_bit(htc_device_t *htc_device, uint32_t reg_offset,
     150    uint32_t set_bit)
    154151{
    155152        return wmi_reg_set_clear_bit(htc_device, reg_offset, set_bit, 0);
    156153}
    157154
    158 /**
    159  * WMI registry clear specified bit.
    160  *
    161  * @param htc_device HTC device structure.
    162  * @param reg_offset Registry offset (address) to be written.
    163  * @param clear_bit Bit to be cleared.
    164  *
    165  * @return EOK if succeed, negative error code otherwise.
    166  */
    167 int wmi_reg_clear_bit(htc_device_t *htc_device, uint32_t reg_offset, 
    168         uint32_t clear_bit)
     155/** WMI registry clear specified bit.
     156 *
     157 * @param htc_device HTC device structure.
     158 * @param reg_offset Registry offset (address) to be written.
     159 * @param clear_bit  Bit to be cleared.
     160 *
     161 * @return EOK if succeed, negative error code otherwise.
     162 *
     163 */
     164int wmi_reg_clear_bit(htc_device_t *htc_device, uint32_t reg_offset,
     165    uint32_t clear_bit)
    169166{
    170167        return wmi_reg_set_clear_bit(htc_device, reg_offset, 0, clear_bit);
    171168}
    172169
    173 /**
    174  * WMI multi registry write.
    175  *
     170/** WMI multi registry write.
     171 *
    176172 * @param htc_device HTC device structure.
    177173 * @param reg_buffer Array of registry values to be written.
    178  * @param elements Number of elements in array.
    179  *
    180  * @return EOK if succeed, negative error code otherwise.
     174 * @param elements   Number of elements in array.
     175 *
     176 * @return EOK if succeed, negative error code otherwise.
     177 *
    181178 */
    182179int wmi_reg_buffer_write(htc_device_t *htc_device, wmi_reg_t *reg_buffer,
    183         size_t elements)
     180    size_t elements)
    184181{
    185182        size_t buffer_size = sizeof(wmi_reg_t) * elements;
    186183        void *buffer = malloc(buffer_size);
    187         void *resp_buffer = 
    188                 malloc(htc_device->ath_device->ctrl_response_length);
     184        void *resp_buffer =
     185            malloc(htc_device->ath_device->ctrl_response_length);
    189186       
    190187        /* Convert values to correct endianness. */
    191         for(size_t i = 0; i < elements; i++) {
     188        for (size_t i = 0; i < elements; i++) {
    192189                wmi_reg_t *buffer_element = &reg_buffer[i];
    193190                wmi_reg_t *buffer_it = (wmi_reg_t *)
    194                         ((void *) buffer + i*sizeof(wmi_reg_t));
    195                 buffer_it->offset = 
    196                         host2uint32_t_be(buffer_element->offset);
     191                    ((void *) buffer + i * sizeof(wmi_reg_t));
     192                buffer_it->offset =
     193                    host2uint32_t_be(buffer_element->offset);
    197194                buffer_it->value =
    198                         host2uint32_t_be(buffer_element->value);
    199         }
    200        
    201         int rc = wmi_send_command(htc_device, WMI_REG_WRITE, 
    202                 (uint8_t *) buffer, buffer_size, resp_buffer);
     195                    host2uint32_t_be(buffer_element->value);
     196        }
     197       
     198        int rc = wmi_send_command(htc_device, WMI_REG_WRITE,
     199            (uint8_t *) buffer, buffer_size, resp_buffer);
    203200       
    204201        free(buffer);
    205202        free(resp_buffer);
    206203       
    207         if(rc != EOK) {
     204        if (rc != EOK) {
    208205                usb_log_error("Failed to write multi registry value.\n");
    209206                return rc;
     
    213210}
    214211
    215 /**
    216  * Send WMI message to HTC device.
    217  *
    218  * @param htc_device HTC device structure.
    219  * @param command_id Command identification.
    220  * @param command_buffer Buffer with command data.
    221  * @param command_length Length of command data.
     212/** Send WMI message to HTC device.
     213 *
     214 * @param htc_device      HTC device structure.
     215 * @param command_id      Command identification.
     216 * @param command_buffer  Buffer with command data.
     217 * @param command_length  Length of command data.
    222218 * @param response_buffer Buffer with response data.
    223  *
    224  * @return EOK if succeed, negative error code otherwise.
    225  */
    226 int wmi_send_command(htc_device_t *htc_device, wmi_command_t command_id,
    227     uint8_t *command_buffer, uint32_t command_length, void *response_buffer)
    228 {
    229         size_t header_size = sizeof(wmi_command_header_t) +
    230                 sizeof(htc_frame_header_t);
     219 *
     220 * @return EOK if succeed, negative error code otherwise.
     221 *
     222 */
     223int wmi_send_command(htc_device_t *htc_device, wmi_command_t command_id,
     224    uint8_t *command_buffer, uint32_t command_length, void *response_buffer)
     225{
     226        size_t header_size = sizeof(wmi_command_header_t) +
     227            sizeof(htc_frame_header_t);
    231228        size_t buffer_size = header_size + command_length;
    232229        void *buffer = malloc(buffer_size);
    233230       
    234         if(command_buffer != NULL) {
     231        if (command_buffer != NULL)
    235232                memcpy(buffer+header_size, command_buffer, command_length);
    236         }
    237233       
    238234        /* Set up WMI header */
    239235        wmi_command_header_t *wmi_header = (wmi_command_header_t *)
    240                 ((void *) buffer + sizeof(htc_frame_header_t));
    241         wmi_header->command_id =
    242                 host2uint16_t_be(command_id);
    243         wmi_header->sequence_number =
    244                 host2uint16_t_be(++htc_device->sequence_number);
     236            ((void *) buffer + sizeof(htc_frame_header_t));
     237        wmi_header->command_id = host2uint16_t_be(command_id);
     238        wmi_header->sequence_number =
     239            host2uint16_t_be(++htc_device->sequence_number);
    245240       
    246241        /* Send message. */
    247242        int rc = htc_send_control_message(htc_device, buffer, buffer_size,
    248                 htc_device->endpoints.wmi_endpoint);
    249         if(rc != EOK) {
     243            htc_device->endpoints.wmi_endpoint);
     244        if (rc != EOK) {
    250245                free(buffer);
    251246                usb_log_error("Failed to send WMI message. Error: %d\n", rc);
     
    256251       
    257252        bool clean_resp_buffer = false;
    258         size_t response_buffer_size = 
    259                 htc_device->ath_device->ctrl_response_length;
    260         if(response_buffer == NULL) {
     253        size_t response_buffer_size =
     254            htc_device->ath_device->ctrl_response_length;
     255        if (response_buffer == NULL) {
    261256                response_buffer = malloc(response_buffer_size);
    262257                clean_resp_buffer = true;
     
    267262        uint16_t cmd_id;
    268263        do {
    269                 rc = htc_read_control_message(htc_device, response_buffer, 
    270                         response_buffer_size, NULL);
    271                 if(rc != EOK) {
     264                rc = htc_read_control_message(htc_device, response_buffer,
     265                    response_buffer_size, NULL);
     266                if (rc != EOK) {
    272267                        free(buffer);
    273268                        usb_log_error("Failed to receive WMI message response. "
     
    276271                }
    277272               
    278                 if(response_buffer_size < sizeof(htc_frame_header_t) +
    279                         sizeof(wmi_command_header_t)) {
     273                if (response_buffer_size < sizeof(htc_frame_header_t) +
     274                    sizeof(wmi_command_header_t)) {
    280275                        free(buffer);
    281276                        usb_log_error("Corrupted response received.\n");
     
    283278                }
    284279               
    285                 wmi_command_header_t *wmi_hdr = (wmi_command_header_t *) 
    286                         ((void*) response_buffer + sizeof(htc_frame_header_t));
     280                wmi_command_header_t *wmi_hdr = (wmi_command_header_t *)
     281                    ((void *) response_buffer + sizeof(htc_frame_header_t));
    287282                cmd_id = uint16_t_be2host(wmi_hdr->command_id);
    288283        } while(cmd_id & WMI_MGMT_CMD_MASK);
    289284       
    290         if(clean_resp_buffer) {
     285        if (clean_resp_buffer)
    291286                free(response_buffer);
    292         }
    293        
    294         return rc;
    295 }
     287       
     288        return rc;
     289}
Note: See TracChangeset for help on using the changeset viewer.