Changeset 8a64320e in mainline


Ignore:
Timestamp:
2015-04-23T23:40:14Z (9 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

Files:
35 edited

Legend:

Unmodified
Added
Removed
  • boot/Makefile

    r09044cb r8a64320e  
    115115                cp "$(USPACE_PATH)/$(DRVS_PATH)/$$file_dir/$$file_name/$$file_name.dev" "$(DIST_PATH)/$(DRVS_PATH)/$$file_name/" ; \
    116116        done
    117         for file in $(RD_DRV_FW) ; do \
     117        for file in $(RD_DRVS_FW) ; do \
    118118                file_dir="`dirname "$$file"`" ; \
    119119                file_name="`basename "$$file"`" ; \
  • boot/Makefile.common

    r09044cb r8a64320e  
    139139        nic/rtl8139 \
    140140        nic/rtl8169 \
    141         nic/ar9271 \
     141        nic/ar9271 \
    142142        block/ahci
    143143
    144144RD_DRV_CFG =
    145145
    146 RD_DRV_FW = \
    147         nic/ar9271
     146RD_DRVS_FW_NON_ESSENTIAL = \
     147        nic/ar9271
    148148
    149149RD_LIBS =
     
    228228        $(USPACE_PATH)/app/df/df \
    229229        $(USPACE_PATH)/app/fontviewer/fontviewer \
    230         $(USPACE_PATH)/app/wifi_supplicant/wifi_supplicant
     230        $(USPACE_PATH)/app/wifi_supplicant/wifi_supplicant
    231231
    232232RD_TESTS = \
     
    252252RD_APPS = $(RD_APPS_ESSENTIAL)
    253253RD_DRVS = $(RD_DRVS_ESSENTIAL)
     254RD_DRVS_FW = $(RD_DRVS_FW_ESSENTIAL)
    254255else
    255256RD_SRVS = $(RD_SRVS_ESSENTIAL) $(RD_SRVS_NON_ESSENTIAL)
    256257RD_APPS = $(RD_APPS_ESSENTIAL) $(RD_APPS_NON_ESSENTIAL)
    257258RD_DRVS = $(RD_DRVS_ESSENTIAL) $(RD_DRVS_NON_ESSENTIAL)
     259RD_DRVS_FW = $(RD_DRVS_FW_ESSENTIAL) $(RD_DRVS_FW_NON_ESSENTIAL)
    258260endif
    259261
  • boot/arch/amd64/Makefile.inc

    r09044cb r8a64320e  
    6161RD_DRV_CFG += \
    6262        bus/isa
    63        
     63
    6464RD_APPS_ESSENTIAL += \
    6565        $(USPACE_PATH)/app/edit/edit \
  • uspace/Makefile

    r09044cb r8a64320e  
    252252        lib/mbr \
    253253        lib/gpt \
    254         lib/ieee80211 \
     254        lib/ieee80211
    255255
    256256LIBC_BUILD = $(addsuffix .build,$(LIBC))
  • uspace/app/wifi_supplicant/Makefile

    r09044cb r8a64320e  
    3535        wifi_supplicant.c
    3636
    37 include $(USPACE_PREFIX)/Makefile.common 
     37include $(USPACE_PREFIX)/Makefile.common
  • uspace/app/wifi_supplicant/wifi_supplicant.c

    r09044cb r8a64320e  
    4242#include <loc.h>
    4343
    44 #define NAME "wifi_supplicant"
    45 
    46 #define enum_name(name_arr, i) ((i < 0) ? "NA" : name_arr[i])
     44#define NAME  "wifi_supplicant"
     45
     46#define enum_name(name_arr, i) \
     47        ((i < 0) ? "NA" : name_arr[i])
    4748
    4849static const char* ieee80211_security_type_strs[] = {
     
    6566        printf("\tlist - list wifi devices in <index>: <name> format\n");
    6667        printf("\tscan <index> [-n] - output scan results (force scan "
    67                 "immediately)\n");
     68            "immediately)\n");
    6869        printf("\tconnect <index> <ssid_prefix> [<password>] - connect to "
    69                 "network\n");
     70            "network\n");
    7071        printf("\tdisconnect <index> - disconnect from network\n");
    7172}
    7273
    73 static char *nic_addr_format(nic_address_t *a)
    74 {
    75         int rc;
    76         char *s;
    77 
    78         rc = asprintf(&s, "%02x:%02x:%02x:%02x:%02x:%02x",
    79             a->address[0], a->address[1], a->address[2],
    80             a->address[3], a->address[4], a->address[5]);
    81 
     74static char *nic_addr_format(nic_address_t *addr)
     75{
     76        char *str;
     77        int rc = asprintf(&str, "%02x:%02x:%02x:%02x:%02x:%02x",
     78            addr->address[0], addr->address[1], addr->address[2],
     79            addr->address[3], addr->address[4], addr->address[5]);
     80       
    8281        if (rc < 0)
    8382                return NULL;
    84 
    85         return s;
     83       
     84        return str;
    8685}
    8786
     
    8988{
    9089        category_id_t wifi_cat;
    91 
    9290        int rc = loc_category_get_id("ieee80211", &wifi_cat, 0);
    9391        if (rc != EOK) {
     
    9593                return rc;
    9694        }
    97 
     95       
    9896        rc = loc_category_get_svcs(wifi_cat, wifis, count);
    9997        if (rc != EOK) {
     
    107105static async_sess_t *get_wifi_by_index(size_t i)
    108106{
    109         int rc;
     107        service_id_t *wifis = NULL;
    110108        size_t count;
    111         service_id_t *wifis = NULL;
    112 
    113         rc = get_wifi_list(&wifis, &count);
     109       
     110        int rc = get_wifi_list(&wifis, &count);
    114111        if (rc != EOK) {
    115112                printf("Error fetching wifi list.\n");
     
    117114        }
    118115       
    119         if(i >= count) {
     116        if (i >= count) {
    120117                printf("Invalid wifi index.\n");
    121118                free(wifis);
    122119                return NULL;
    123120        }
    124 
    125         async_sess_t *sess = 
    126                 loc_service_connect(EXCHANGE_SERIALIZE, wifis[i], 0);
     121       
     122        async_sess_t *sess =
     123            loc_service_connect(EXCHANGE_SERIALIZE, wifis[i], 0);
    127124        if (sess == NULL) {
    128125                printf("Error connecting to service.\n");
     
    130127                return NULL;
    131128        }
    132 
     129       
    133130        return sess;
    134131}
     
    138135        service_id_t *wifis = NULL;
    139136        size_t count;
    140         char *svc_name;
    141         int rc;
    142 
    143         rc = get_wifi_list(&wifis, &count);
     137       
     138        int rc = get_wifi_list(&wifis, &count);
    144139        if (rc != EOK) {
    145140                printf("Error fetching wifi list.\n");
    146141                return EINVAL;
    147142        }
    148 
     143       
    149144        printf("[Index]: [Service Name]\n");
    150145        for (size_t i = 0; i < count; i++) {
     146                char *svc_name;
    151147                rc = loc_service_get_name(wifis[i], &svc_name);
    152148                if (rc != EOK) {
     
    157153               
    158154                printf("%zu: %s\n", i, svc_name);
    159 
     155               
    160156                free(svc_name);
    161157        }
    162 
     158       
    163159        return EOK;
    164160}
     
    171167        if (sess == NULL) {
    172168                printf("Specified WIFI doesn't exist or cannot connect to "
    173                         "it.\n");
     169                    "it.\n");
    174170                return EINVAL;
    175171        }
     
    177173        int rc = ieee80211_disconnect(sess);
    178174        if(rc != EOK) {
    179                 if(rc == EREFUSED) {
    180                         printf("Device is not ready yet.\n");                   
    181                 } else {
     175                if (rc == EREFUSED)
     176                        printf("Device is not ready yet.\n");
     177                else
    182178                        printf("Error when disconnecting device. "
    183                                 "Error: %d\n", rc);
    184                 }
     179                            "Error: %d\n", rc);
    185180               
    186181                return rc;
     
    189184        rc = ieee80211_connect(sess, ssid_start, password);
    190185        if(rc != EOK) {
    191                 if(rc == EREFUSED) {
    192                         printf("Device is not ready yet.\n");                   
    193                 } else if(rc == ETIMEOUT) {
     186                if (rc == EREFUSED)
     187                        printf("Device is not ready yet.\n");
     188                else if (rc == ETIMEOUT)
    194189                        printf("Timeout when authenticating to network.\n");
    195                 } else if(rc == ENOENT) {
     190                else if (rc == ENOENT)
    196191                        printf("Given SSID not in scan results.\n");
    197                 } else {
     192                else
    198193                        printf("Error when connecting to network. "
    199                                 "Error: %d\n", rc);
    200                 }
    201                
    202                 return rc;
    203         }
    204        
    205         // TODO: Wait for DHCP address ?
     194                            "Error: %d\n", rc);
     195               
     196                return rc;
     197        }
     198       
     199        // TODO: Wait for DHCP address?
    206200       
    207201        printf("Successfully connected to network!\n");
     
    215209        if (sess == NULL) {
    216210                printf("Specified WIFI doesn't exist or cannot connect to "
    217                         "it.\n");
     211                    "it.\n");
    218212                return EINVAL;
    219213        }
    220214       
    221215        int rc = ieee80211_disconnect(sess);
    222         if(rc != EOK) {
    223                 if(rc == EREFUSED) {
     216        if (rc != EOK) {
     217                if (rc == EREFUSED)
    224218                        printf("Device is not ready yet.\n");
    225                 } else if(rc == EINVAL) {
     219                else if (rc == EINVAL)
    226220                        printf("Not connected to any WiFi network.\n");
    227                 } else {
     221                else
    228222                        printf("Error when disconnecting from network. "
    229                                 "Error: %d\n", rc);
    230                 }
     223                            "Error: %d\n", rc);
     224               
    231225                return rc;
    232226        }
     
    239233static int wifi_scan(uint32_t index, bool now)
    240234{
    241         ieee80211_scan_results_t scan_results;
    242        
    243235        async_sess_t *sess = get_wifi_by_index(index);
    244236        if (sess == NULL) {
    245237                printf("Specified WIFI doesn't exist or cannot connect to "
    246                         "it.\n");
     238                    "it.\n");
    247239                return EINVAL;
    248240        }
    249241       
     242        ieee80211_scan_results_t scan_results;
    250243        int rc = ieee80211_get_scan_results(sess, &scan_results, now);
    251         if(rc != EOK) {
    252                 if(rc == EREFUSED) {
     244        if (rc != EOK) {
     245                if (rc == EREFUSED)
    253246                        printf("Device is not ready yet.\n");
    254                 } else {
     247                else
    255248                        printf("Failed to fetch scan results. Error: %d\n", rc);
    256                 }
    257                
    258                 return rc;
    259         }
    260        
    261         if(scan_results.length == 0)
     249               
     250                return rc;
     251        }
     252       
     253        if (scan_results.length == 0)
    262254                return EOK;
    263255       
    264         printf("%16.16s %17s %4s %5s %5s %7s %7s\n", 
    265                 "SSID", "MAC", "CHAN", "TYPE", "AUTH", "UNI-ALG", "GRP-ALG");
    266        
    267         for(int i = 0; i < scan_results.length; i++) {
     256        printf("%16.16s %17s %4s %5s %5s %7s %7s\n",
     257            "SSID", "MAC", "CHAN", "TYPE", "AUTH", "UNI-ALG", "GRP-ALG");
     258       
     259        for (uint8_t i = 0; i < scan_results.length; i++) {
    268260                ieee80211_scan_result_t result = scan_results.results[i];
    269261               
    270                 printf("%16.16s %17s %4d %5s %5s %7s %7s\n",
    271                         result.ssid,
    272                         nic_addr_format(&result.bssid),
    273                         result.channel,
    274                         enum_name(ieee80211_security_type_strs,
    275                                 result.security.type),
    276                         enum_name(ieee80211_security_auth_strs,
    277                                 result.security.auth),
    278                         enum_name(ieee80211_security_alg_strs,
    279                                 result.security.pair_alg),
    280                         enum_name(ieee80211_security_alg_strs,
    281                                 result.security.group_alg)
    282                 );
     262                printf("%16.16s %17s %4d %5s %5s %7s %7s\n",
     263                    result.ssid, nic_addr_format(&result.bssid),
     264                    result.channel,
     265                    enum_name(ieee80211_security_type_strs, result.security.type),
     266                    enum_name(ieee80211_security_auth_strs, result.security.auth),
     267                    enum_name(ieee80211_security_alg_strs, result.security.pair_alg),
     268                    enum_name(ieee80211_security_alg_strs, result.security.group_alg));
    283269        }
    284270       
     
    288274int main(int argc, char *argv[])
    289275{
    290         int rc;
    291         uint32_t index;
    292        
    293         rc = inetcfg_init();
    294         if (rc != EOK) {
    295                 printf(NAME ": Failed connecting to inetcfg service (%d).\n",
    296                     rc);
     276        int rc = inetcfg_init();
     277        if (rc != EOK) {
     278                printf("%s: Failed connecting to inetcfg service (%d).\n",
     279                    NAME, rc);
    297280                return 1;
    298281        }
     
    300283        rc = dhcp_init();
    301284        if (rc != EOK) {
    302                 printf(NAME ": Failed connecting to dhcp service (%d).\n", rc);
     285                printf("%s: Failed connecting to dhcp service (%d).\n",
     286                    NAME, rc);
    303287                return 1;
    304288        }
    305289       
    306         if(argc == 2) {
    307                 if(!str_cmp(argv[1], "list")) {
     290        if (argc == 2) {
     291                if (!str_cmp(argv[1], "list"))
    308292                        return wifi_list();
    309                 }
    310         } else if(argc > 2) {
     293        } else if (argc > 2) {
     294                uint32_t index;
    311295                rc = str_uint32_t(argv[2], NULL, 10, false, &index);
    312                 if(rc != EOK) {
    313                         printf(NAME ": Invalid argument.\n");
     296                if (rc != EOK) {
     297                        printf("%s: Invalid argument.\n", NAME);
    314298                        print_syntax();
    315299                        return EINVAL;
    316300                }
    317                 if(!str_cmp(argv[1], "scan")) {
     301               
     302                if (!str_cmp(argv[1], "scan")) {
    318303                        bool now = false;
    319                         if(argc > 3)
    320                                 if(!str_cmp(argv[3], "-n"))
     304                        if (argc > 3)
     305                                if (!str_cmp(argv[3], "-n"))
    321306                                        now = true;
     307                       
    322308                        return wifi_scan(index, now);
    323                 } else if(!str_cmp(argv[1], "connect")) {
     309                } else if (!str_cmp(argv[1], "connect")) {
    324310                        char *pass = NULL;
    325                         if(argc > 3) {
    326                                 if(argc > 4)
     311                        if (argc > 3) {
     312                                if (argc > 4)
    327313                                        pass = argv[4];
     314                               
    328315                                return wifi_connect(index, argv[3], pass);
    329                         } 
    330                 } else if(!str_cmp(argv[1], "disconnect")) {
     316                        }
     317                } else if (!str_cmp(argv[1], "disconnect"))
    331318                        return wifi_disconnect(index);
    332                 }
    333319        }
    334320       
  • uspace/drv/nic/ar9271/Makefile

    r09044cb r8a64320e  
    3636        $(LIBIEEE80211_PREFIX)/libieee80211.a \
    3737        $(LIBCRYPTO_PREFIX)/libcrypto.a
    38        
     38
    3939EXTRA_CFLAGS += \
    4040        -I. \
     
    4545        -I$(LIBIEEE80211_PREFIX)/include \
    4646        -I$(LIBCRYPTO_PREFIX)
    47        
     47
    4848BINARY = ar9271
    4949
  • uspace/drv/nic/ar9271/ar9271.c

    r09044cb r8a64320e  
    4242#include <nic.h>
    4343#include <macros.h>
    44 
    4544#include "ath_usb.h"
    4645#include "wmi.h"
     
    4847#include "ar9271.h"
    4948
    50 #define NAME "ar9271"
    51 #define FIRMWARE_FILENAME "/drv/ar9271/ar9271.fw"
     49#define NAME  "ar9271"
     50#define FIRMWARE_FILENAME  "/drv/ar9271/ar9271.fw"
    5251
    5352const usb_endpoint_description_t usb_ar9271_out_bulk_endpoint_description = {
     
    9190        &usb_ar9271_out_bulk_endpoint_description,
    9291        &usb_ar9271_in_bulk_endpoint_description,
    93         &usb_ar9271_in_int_endpoint_description,
    94         &usb_ar9271_out_int_endpoint_description,
     92        &usb_ar9271_in_int_endpoint_description,
     93        &usb_ar9271_out_int_endpoint_description,
    9594        NULL
    9695};
     
    10099
    101100/* IEEE 802.11 callbacks */
    102 static int ar9271_ieee80211_start(ieee80211_dev_t *ieee80211_dev);
    103 static int ar9271_ieee80211_tx_handler(ieee80211_dev_t *ieee80211_dev,
    104         void *buffer, size_t buffer_size);
    105 static int ar9271_ieee80211_set_freq(ieee80211_dev_t *ieee80211_dev,
    106         uint16_t freq);
    107 static int ar9271_ieee80211_bssid_change(ieee80211_dev_t *ieee80211_dev,
    108         bool connected);
    109 static int ar9271_ieee80211_key_config(ieee80211_dev_t *ieee80211_dev,
    110         ieee80211_key_config_t *key_conf, bool insert);
     101static int ar9271_ieee80211_start(ieee80211_dev_t *);
     102static int ar9271_ieee80211_tx_handler(ieee80211_dev_t *, void *, size_t);
     103static int ar9271_ieee80211_set_freq(ieee80211_dev_t *, uint16_t);
     104static int ar9271_ieee80211_bssid_change(ieee80211_dev_t *, bool);
     105static int ar9271_ieee80211_key_config(ieee80211_dev_t *, ieee80211_key_config_t *,
     106    bool);
    111107
    112108static driver_ops_t ar9271_driver_ops = {
     
    129125static ieee80211_iface_t ar9271_ieee80211_iface;
    130126
    131 static int ar9271_get_device_info(ddf_fun_t *dev, nic_device_info_t *info);
     127static int ar9271_get_device_info(ddf_fun_t *, nic_device_info_t *);
    132128static int ar9271_get_cable_state(ddf_fun_t *, nic_cable_state_t *);
    133 static int ar9271_get_operation_mode(ddf_fun_t *, int *,
    134         nic_channel_mode_t *, nic_role_t *);
     129static int ar9271_get_operation_mode(ddf_fun_t *, int *, nic_channel_mode_t *,
     130    nic_role_t *);
    135131
    136132static nic_iface_t ar9271_ieee80211_nic_iface = {
     
    142138static ddf_dev_ops_t ar9271_ieee80211_dev_ops;
    143139
    144 /**
    145  * Get device information.
     140/** Get device information.
    146141 *
    147142 */
     
    153148        memset(info, 0, sizeof(nic_device_info_t));
    154149       
    155         info->vendor_id = 0x0CF3;
     150        info->vendor_id = 0x0cf3;
    156151        info->device_id = 0x9271;
    157152        str_cpy(info->vendor_name, NIC_VENDOR_MAX_LENGTH,
     
    163158}
    164159
    165 /**
    166  * Get cable state.
     160/** Get cable state.
    167161 *
    168162 */
     
    174168}
    175169
    176 /**
    177  * Get operation mode of the device.
     170/** Get operation mode of the device.
    178171 *
    179172 */
     
    188181}
    189182
    190 /**
    191  * Set multicast frames acceptance mode.
    192  *
    193  */
    194 static int ar9271_on_multicast_mode_change(nic_t *nic,
    195         nic_multicast_mode_t mode, const nic_address_t *addr, size_t addr_cnt)
    196 {
    197         /*
    198         ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *)
    199                 nic_get_specific(nic);
    200         ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev);
    201          */
    202        
     183/** Set multicast frames acceptance mode.
     184 *
     185 */
     186static int ar9271_on_multicast_mode_change(nic_t *nic,
     187    nic_multicast_mode_t mode, const nic_address_t *addr, size_t addr_cnt)
     188{
    203189        switch (mode) {
    204                 case NIC_MULTICAST_BLOCKED:
    205                         /* TODO */
    206                         break;
    207                 case NIC_MULTICAST_LIST:
    208                         /* TODO */
    209                         break;
    210                 case NIC_MULTICAST_PROMISC:
    211                         /* TODO */
    212                         break;
    213                 default:
    214                         return ENOTSUP;
    215         }
    216        
    217         return EOK;
    218 }
    219 
    220 /**
    221  * Set unicast frames acceptance mode.
     190        case NIC_MULTICAST_BLOCKED:
     191                /* TODO */
     192                break;
     193        case NIC_MULTICAST_LIST:
     194                /* TODO */
     195                break;
     196        case NIC_MULTICAST_PROMISC:
     197                /* TODO */
     198                break;
     199        default:
     200                return ENOTSUP;
     201        }
     202       
     203        return EOK;
     204}
     205
     206/** Set unicast frames acceptance mode.
    222207 *
    223208 */
     
    225210    const nic_address_t *addr, size_t addr_cnt)
    226211{
    227         /*
    228         ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *)
    229                 nic_get_specific(nic);
    230         ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev);
    231          */
    232        
    233212        switch (mode) {
    234                 case NIC_UNICAST_BLOCKED:
    235                         /* TODO */
    236                         break;
    237                 case NIC_UNICAST_DEFAULT:
    238                         /* TODO */
    239                         break;
    240                 case NIC_UNICAST_LIST:
    241                         /* TODO */
    242                         break;
    243                 case NIC_UNICAST_PROMISC:
    244                         /* TODO */
    245                         break;
    246                 default:
    247                         return ENOTSUP;
    248         }
    249        
    250         return EOK;
    251 }
    252 
    253 /**
    254  * Set broadcast frames acceptance mode.
    255  *
    256  */
    257 static int ar9271_on_broadcast_mode_change(nic_t *nic,
    258         nic_broadcast_mode_t mode)
    259 {
    260         /*
    261         ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *)
    262                 nic_get_specific(nic);
    263         ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev);
    264          */
    265        
     213        case NIC_UNICAST_BLOCKED:
     214                /* TODO */
     215                break;
     216        case NIC_UNICAST_DEFAULT:
     217                /* TODO */
     218                break;
     219        case NIC_UNICAST_LIST:
     220                /* TODO */
     221                break;
     222        case NIC_UNICAST_PROMISC:
     223                /* TODO */
     224                break;
     225        default:
     226                return ENOTSUP;
     227        }
     228       
     229        return EOK;
     230}
     231
     232/** Set broadcast frames acceptance mode.
     233 *
     234 */
     235static int ar9271_on_broadcast_mode_change(nic_t *nic,
     236    nic_broadcast_mode_t mode)
     237{
    266238        switch (mode) {
    267                 case NIC_BROADCAST_BLOCKED:
    268                         /* TODO */
    269                         break;
    270                 case NIC_BROADCAST_ACCEPTED:
    271                         /* TODO */
    272                         break;
    273                 default:
    274                         return ENOTSUP;
     239        case NIC_BROADCAST_BLOCKED:
     240                /* TODO */
     241                break;
     242        case NIC_BROADCAST_ACCEPTED:
     243                /* TODO */
     244                break;
     245        default:
     246                return ENOTSUP;
    275247        }
    276248       
     
    292264        void *buffer = malloc(buffer_size);
    293265       
    294         while(true) {
     266        while (true) {
    295267                size_t transferred_size;
    296                 if(htc_read_data_message(ar9271->htc_device,
    297                         buffer, buffer_size, &transferred_size) == EOK) {
    298                         size_t strip_length = 
    299                                 sizeof(ath_usb_data_header_t) +
    300                                 sizeof(htc_frame_header_t) +
    301                                 sizeof(htc_rx_status_t);
    302                        
    303                         if(transferred_size < strip_length)
     268                if (htc_read_data_message(ar9271->htc_device,
     269                    buffer, buffer_size, &transferred_size) == EOK) {
     270                        size_t strip_length =
     271                            sizeof(ath_usb_data_header_t) +
     272                            sizeof(htc_frame_header_t) +
     273                            sizeof(htc_rx_status_t);
     274                       
     275                        if (transferred_size < strip_length)
    304276                                continue;
    305 
    306                         ath_usb_data_header_t *data_header = 
    307                                 (ath_usb_data_header_t *) buffer;
    308 
     277                       
     278                        ath_usb_data_header_t *data_header =
     279                            (ath_usb_data_header_t *) buffer;
     280                       
    309281                        /* Invalid packet. */
    310                         if(data_header->tag != uint16_t_le2host(RX_TAG))
     282                        if (data_header->tag != uint16_t_le2host(RX_TAG))
    311283                                continue;
    312284                       
    313285                        htc_rx_status_t *rx_status =
    314                                 (htc_rx_status_t *) ((void *) buffer +
    315                                 sizeof(ath_usb_data_header_t) +
    316                                 sizeof(htc_frame_header_t));
     286                            (htc_rx_status_t *) ((void *) buffer +
     287                            sizeof(ath_usb_data_header_t) +
     288                            sizeof(htc_frame_header_t));
    317289                       
    318290                        uint16_t data_length =
    319                                 uint16_t_be2host(rx_status->data_length);
    320                        
    321                         int16_t payload_length = 
    322                                 transferred_size - strip_length;
    323                        
    324                         if(payload_length - data_length < 0)
     291                            uint16_t_be2host(rx_status->data_length);
     292                       
     293                        int16_t payload_length =
     294                            transferred_size - strip_length;
     295                       
     296                        if (payload_length - data_length < 0)
    325297                                continue;
    326298                       
    327                         if(ar9271_rx_status_error(rx_status->status))
     299                        if (ar9271_rx_status_error(rx_status->status))
    328300                                continue;
    329301                       
    330302                        void *strip_buffer = buffer + strip_length;
    331 
     303                       
    332304                        ieee80211_rx_handler(ar9271->ieee80211_dev,
    333                                 strip_buffer,
    334                                 payload_length);
     305                            strip_buffer,
     306                            payload_length);
    335307                }
    336308        }
     
    341313}
    342314
    343 /**
    344  * IEEE 802.11 handlers.
    345  */
    346 
     315/** IEEE 802.11 handlers.
     316 *
     317 */
    347318static int ar9271_ieee80211_set_freq(ieee80211_dev_t *ieee80211_dev,
    348         uint16_t freq)
     319    uint16_t freq)
    349320{
    350321        assert(ieee80211_dev);
     
    357328       
    358329        int rc = hw_freq_switch(ar9271, freq);
    359         if(rc != EOK) {
     330        if (rc != EOK) {
    360331                usb_log_error("Failed to HW switch frequency.\n");
    361332                return rc;
     
    365336       
    366337        rc = hw_rx_init(ar9271);
    367         if(rc != EOK) {
     338        if (rc != EOK) {
    368339                usb_log_error("Failed to initialize RX.\n");
    369340                return rc;
     
    371342       
    372343        uint16_t htc_mode = host2uint16_t_be(1);
    373         wmi_send_command(ar9271->htc_device, WMI_SET_MODE, 
    374                 (uint8_t *) &htc_mode, sizeof(htc_mode), NULL);
     344        wmi_send_command(ar9271->htc_device, WMI_SET_MODE,
     345            (uint8_t *) &htc_mode, sizeof(htc_mode), NULL);
    375346        wmi_send_command(ar9271->htc_device, WMI_ENABLE_INTR, NULL, 0, NULL);
    376347       
     
    379350
    380351static int ar9271_ieee80211_bssid_change(ieee80211_dev_t *ieee80211_dev,
    381         bool connected)
     352    bool connected)
    382353{
    383354        assert(ieee80211_dev);
    384355       
    385356        ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev);
    386 
    387         if(connected) {
     357       
     358        if (connected) {
    388359                nic_address_t bssid;
    389360                ieee80211_query_bssid(ieee80211_dev, &bssid);
    390 
     361               
    391362                htc_sta_msg_t sta_msg;
    392363                memset(&sta_msg, 0, sizeof(htc_sta_msg_t));
    393364                sta_msg.is_vif_sta = 0;
    394                 sta_msg.max_ampdu = 
    395                         host2uint16_t_be(1 << IEEE80211_MAX_AMPDU_FACTOR);
     365                sta_msg.max_ampdu =
     366                    host2uint16_t_be(1 << IEEE80211_MAX_AMPDU_FACTOR);
    396367                sta_msg.sta_index = 1;
    397368                sta_msg.vif_index = 0;
    398369                memcpy(&sta_msg.addr, bssid.address, ETH_ADDR);
    399 
    400                 wmi_send_command(ar9271->htc_device, WMI_NODE_CREATE, 
    401                         (uint8_t *) &sta_msg, sizeof(sta_msg), NULL);
    402 
     370               
     371                wmi_send_command(ar9271->htc_device, WMI_NODE_CREATE,
     372                    (uint8_t *) &sta_msg, sizeof(sta_msg), NULL);
     373               
    403374                htc_rate_msg_t rate_msg;
    404375                memset(&rate_msg, 0, sizeof(htc_rate_msg_t));
    405376                rate_msg.sta_index = 1;
    406377                rate_msg.is_new = 1;
    407                 rate_msg.legacy_rates_count =
    408                         ARRAY_SIZE(ieee80211bg_data_rates);
    409                 memcpy(&rate_msg.legacy_rates,
    410                         ieee80211bg_data_rates,
    411                         ARRAY_SIZE(ieee80211bg_data_rates));
    412 
    413                 wmi_send_command(ar9271->htc_device, WMI_RC_RATE_UPDATE,
    414                         (uint8_t *) &rate_msg, sizeof(rate_msg), NULL);
    415 
     378                rate_msg.legacy_rates_count = ARRAY_SIZE(ieee80211bg_data_rates);
     379                memcpy(&rate_msg.legacy_rates,
     380                    ieee80211bg_data_rates,
     381                    ARRAY_SIZE(ieee80211bg_data_rates));
     382               
     383                wmi_send_command(ar9271->htc_device, WMI_RC_RATE_UPDATE,
     384                    (uint8_t *) &rate_msg, sizeof(rate_msg), NULL);
     385               
    416386                hw_set_rx_filter(ar9271, true);
    417387        } else {
    418388                uint8_t station_id = 1;
    419389                wmi_send_command(ar9271->htc_device, WMI_NODE_REMOVE,
    420                         &station_id, sizeof(station_id), NULL);
     390                    &station_id, sizeof(station_id), NULL);
    421391               
    422392                hw_set_rx_filter(ar9271, false);
     
    429399
    430400static int ar9271_ieee80211_key_config(ieee80211_dev_t *ieee80211_dev,
    431         ieee80211_key_config_t *key_conf, bool insert)
     401    ieee80211_key_config_t *key_conf, bool insert)
    432402{
    433403        assert(ieee80211_dev);
     
    446416                ieee80211_query_bssid(ieee80211_dev, &bssid);
    447417               
    448                 switch(key_conf->suite) {
    449                         case IEEE80211_SECURITY_SUITE_WEP40:
    450                                 key_type = AR9271_KEY_TABLE_TYPE_WEP40;
    451                                 break;
    452                         case IEEE80211_SECURITY_SUITE_WEP104:
    453                                 key_type = AR9271_KEY_TABLE_TYPE_WEP104;
    454                                 break;
    455                         case IEEE80211_SECURITY_SUITE_TKIP:
    456                                 key_type = AR9271_KEY_TABLE_TYPE_TKIP;
    457                                 break;
    458                         case IEEE80211_SECURITY_SUITE_CCMP:
    459                                 key_type = AR9271_KEY_TABLE_TYPE_CCMP;
    460                                 break;
    461                         default:
    462                                 key_type = -1;
     418                switch (key_conf->suite) {
     419                case IEEE80211_SECURITY_SUITE_WEP40:
     420                        key_type = AR9271_KEY_TABLE_TYPE_WEP40;
     421                        break;
     422                case IEEE80211_SECURITY_SUITE_WEP104:
     423                        key_type = AR9271_KEY_TABLE_TYPE_WEP104;
     424                        break;
     425                case IEEE80211_SECURITY_SUITE_TKIP:
     426                        key_type = AR9271_KEY_TABLE_TYPE_TKIP;
     427                        break;
     428                case IEEE80211_SECURITY_SUITE_CCMP:
     429                        key_type = AR9271_KEY_TABLE_TYPE_CCMP;
     430                        break;
     431                default:
     432                        key_type = -1;
    463433                }
    464434               
    465                 uint8_t key_id = 
    466                         (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_PAIRWISE) ?
    467                                 AR9271_STA_KEY_INDEX : key_conf->id;
     435                uint8_t key_id =
     436                    (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_PAIRWISE) ?
     437                    AR9271_STA_KEY_INDEX : key_conf->id;
    468438               
    469439                reg_ptr = AR9271_KEY_TABLE(key_id);
     
    471441                data_start = (void *) key_conf->data;
    472442               
    473                 key[0] = uint32_t_le2host(
    474                         *((uint32_t *) data_start));
    475                 key[1] = uint16_t_le2host(
    476                         *((uint16_t *) (data_start + 4)));
    477                 key[2] = uint32_t_le2host(
    478                         *((uint32_t *) (data_start + 6)));
    479                 key[3] = uint16_t_le2host(
    480                         *((uint16_t *) (data_start + 10)));
    481                 key[4] = uint32_t_le2host(
    482                         *((uint32_t *) (data_start + 12)));
    483 
    484                 if(key_conf->suite == IEEE80211_SECURITY_SUITE_WEP40 ||
    485                    key_conf->suite == IEEE80211_SECURITY_SUITE_WEP104) {
     443                key[0] = uint32_t_le2host(*((uint32_t *) data_start));
     444                key[1] = uint16_t_le2host(*((uint16_t *) (data_start + 4)));
     445                key[2] = uint32_t_le2host(*((uint32_t *) (data_start + 6)));
     446                key[3] = uint16_t_le2host(*((uint16_t *) (data_start + 10)));
     447                key[4] = uint32_t_le2host(*((uint32_t *) (data_start + 12)));
     448               
     449                if ((key_conf->suite == IEEE80211_SECURITY_SUITE_WEP40) ||
     450                    (key_conf->suite == IEEE80211_SECURITY_SUITE_WEP104))
    486451                        key[4] &= 0xFF;
    487                 }
    488452               
    489453                wmi_reg_write(ar9271->htc_device, reg_ptr + 0, key[0]);
     
    494458                wmi_reg_write(ar9271->htc_device, reg_ptr + 20, key_type);
    495459               
    496                 uint32_t macL, macH;
    497                 if(key_conf->flags & IEEE80211_KEY_FLAG_TYPE_PAIRWISE) {
     460                uint32_t macl;
     461                uint32_t mach;
     462                if (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_PAIRWISE) {
    498463                        data_start = (void *) bssid.address;
    499                         macL = uint32_t_le2host(*((uint32_t *) data_start));
    500                         macH = uint16_t_le2host(*((uint16_t *)
    501                                 (data_start + 4)));
     464                        macl = uint32_t_le2host(*((uint32_t *) data_start));
     465                        mach = uint16_t_le2host(*((uint16_t *) (data_start + 4)));
    502466                } else {
    503                         macL = macH = 0;
     467                        macl = 0;
     468                        mach = 0;
    504469                }
    505470               
    506                 macL >>= 1;
    507                 macL |= (macH & 1) << 31;
    508                 macH >>= 1;
    509                 macH |= 0x8000;
    510                
    511                 wmi_reg_write(ar9271->htc_device, reg_ptr + 24, macL);
    512                 wmi_reg_write(ar9271->htc_device, reg_ptr + 28, macH);
     471                macl >>= 1;
     472                macl |= (mach & 1) << 31;
     473                mach >>= 1;
     474                mach |= 0x8000;
     475               
     476                wmi_reg_write(ar9271->htc_device, reg_ptr + 24, macl);
     477                wmi_reg_write(ar9271->htc_device, reg_ptr + 28, mach);
    513478               
    514479                /* Setup MIC keys for TKIP. */
    515                 if(key_conf->suite == IEEE80211_SECURITY_SUITE_TKIP) {
     480                if (key_conf->suite == IEEE80211_SECURITY_SUITE_TKIP) {
    516481                        uint32_t mic[5];
    517                         uint8_t *gen_mic =
    518                                 data_start + IEEE80211_TKIP_RX_MIC_OFFSET;
     482                        uint8_t *gen_mic = data_start + IEEE80211_TKIP_RX_MIC_OFFSET;
    519483                        uint8_t *tx_mic;
    520                         if(key_conf->flags & IEEE80211_KEY_FLAG_TYPE_GROUP) {
     484                       
     485                        if (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_GROUP)
    521486                                tx_mic = gen_mic;
    522                         } else {
    523                                 tx_mic = data_start +
    524                                         IEEE80211_TKIP_TX_MIC_OFFSET;
    525                         }
    526                        
    527                         mic[0] = uint32_t_le2host(
    528                                 *((uint32_t *) gen_mic));
    529                         mic[1] = uint16_t_le2host(
    530                                 *((uint16_t *) (tx_mic + 2))) & 0xFFFF;
    531                         mic[2] = uint32_t_le2host(
    532                                 *((uint32_t *) (gen_mic + 4)));
    533                         mic[3] = uint16_t_le2host(
    534                                 *((uint16_t *) tx_mic)) & 0xFFFF;
    535                         mic[4] = uint32_t_le2host(
    536                                 *((uint32_t *) (tx_mic + 4)));
    537                        
    538                         wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 0,
    539                                 mic[0]);
    540                         wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 4,
    541                                 mic[1]);
    542                         wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 8,
    543                                 mic[2]);
    544                         wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 12,
    545                                 mic[3]);
    546                         wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 16,
    547                                 mic[4]);
    548                         wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 20,
    549                                 AR9271_KEY_TABLE_TYPE_CLR);
     487                        else
     488                                tx_mic = data_start + IEEE80211_TKIP_TX_MIC_OFFSET;
     489                       
     490                        mic[0] = uint32_t_le2host(*((uint32_t *) gen_mic));
     491                        mic[1] = uint16_t_le2host(*((uint16_t *) (tx_mic + 2))) & 0xFFFF;
     492                        mic[2] = uint32_t_le2host(*((uint32_t *) (gen_mic + 4)));
     493                        mic[3] = uint16_t_le2host(*((uint16_t *) tx_mic)) & 0xFFFF;
     494                        mic[4] = uint32_t_le2host(*((uint32_t *) (tx_mic + 4)));
     495                       
     496                        wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 0, mic[0]);
     497                        wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 4, mic[1]);
     498                        wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 8, mic[2]);
     499                        wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 12, mic[3]);
     500                        wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 16, mic[4]);
     501                        wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 20,
     502                            AR9271_KEY_TABLE_TYPE_CLR);
    550503                       
    551504                        wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 24, 0);
     
    553506                }
    554507               
    555                 if(key_conf->flags & IEEE80211_KEY_FLAG_TYPE_GROUP)
     508                if (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_GROUP)
    556509                        ieee80211_setup_key_confirm(ieee80211_dev, true);
    557510        } else {
     
    564517
    565518static int ar9271_ieee80211_tx_handler(ieee80211_dev_t *ieee80211_dev,
    566         void *buffer, size_t buffer_size)
     519    void *buffer, size_t buffer_size)
    567520{
    568521        assert(ieee80211_dev);
    569522       
    570         size_t complete_size, offset;
     523        size_t complete_size;
     524        size_t offset;
    571525        void *complete_buffer;
    572526        int endpoint;
     
    575529       
    576530        uint16_t frame_ctrl = *((uint16_t *) buffer);
    577         if(ieee80211_is_data_frame(frame_ctrl)) {
     531        if (ieee80211_is_data_frame(frame_ctrl)) {
    578532                offset = sizeof(htc_tx_data_header_t) +
    579                         sizeof(htc_frame_header_t);
     533                    sizeof(htc_frame_header_t);
    580534                complete_size = buffer_size + offset;
    581535                complete_buffer = malloc(complete_size);
    582536                memset(complete_buffer, 0, complete_size);
    583537               
    584                 /* 
     538                /*
    585539                 * Because we handle just station mode yet, node ID and VIF ID
    586540                 * are fixed.
    587541                 */
    588542                htc_tx_data_header_t *data_header =
    589                         (htc_tx_data_header_t *)
    590                         (complete_buffer + sizeof(htc_frame_header_t));
     543                    (htc_tx_data_header_t *)
     544                    (complete_buffer + sizeof(htc_frame_header_t));
    591545                data_header->data_type = HTC_DATA_NORMAL;
    592546                data_header->node_idx = 1;
     
    594548                data_header->cookie = 0;
    595549               
    596                 if(ieee80211_query_using_key(ieee80211_dev)) {
     550                if (ieee80211_query_using_key(ieee80211_dev)) {
    597551                        data_header->keyix = AR9271_STA_KEY_INDEX;
    598                         int sec_suite =
    599                                 ieee80211_get_pairwise_security(ieee80211_dev);
    600                         switch(sec_suite) {
    601                                 case IEEE80211_SECURITY_SUITE_WEP40:
    602                                 case IEEE80211_SECURITY_SUITE_WEP104:
    603                                         data_header->key_type =
    604                                                 AR9271_KEY_TYPE_WEP;
    605                                         break;
    606                                 case IEEE80211_SECURITY_SUITE_TKIP:
    607                                         data_header->key_type =
    608                                                 AR9271_KEY_TYPE_TKIP;
    609                                         break;
    610                                 case IEEE80211_SECURITY_SUITE_CCMP:
    611                                         data_header->key_type =
    612                                                 AR9271_KEY_TYPE_AES;
    613                                         break;
     552                       
     553                        int sec_suite =
     554                            ieee80211_get_pairwise_security(ieee80211_dev);
     555                       
     556                        switch (sec_suite) {
     557                        case IEEE80211_SECURITY_SUITE_WEP40:
     558                        case IEEE80211_SECURITY_SUITE_WEP104:
     559                                data_header->key_type = AR9271_KEY_TYPE_WEP;
     560                                break;
     561                        case IEEE80211_SECURITY_SUITE_TKIP:
     562                                data_header->key_type = AR9271_KEY_TYPE_TKIP;
     563                                break;
     564                        case IEEE80211_SECURITY_SUITE_CCMP:
     565                                data_header->key_type = AR9271_KEY_TYPE_AES;
     566                                break;
    614567                        }
    615568                } else {
     
    621574        } else {
    622575                offset = sizeof(htc_tx_management_header_t) +
    623                         sizeof(htc_frame_header_t);
     576                    sizeof(htc_frame_header_t);
    624577                complete_size = buffer_size + offset;
    625578                complete_buffer = malloc(complete_size);
    626579                memset(complete_buffer, 0, complete_size);
    627580               
    628                 /* 
     581                /*
    629582                 * Because we handle just station mode yet, node ID and VIF ID
    630583                 * are fixed.
    631584                 */
    632585                htc_tx_management_header_t *mgmt_header =
    633                         (htc_tx_management_header_t *)
    634                         (complete_buffer + sizeof(htc_frame_header_t));
     586                    (htc_tx_management_header_t *)
     587                    (complete_buffer + sizeof(htc_frame_header_t));
    635588                mgmt_header->node_idx = 0;
    636589                mgmt_header->vif_idx = 0;
     
    645598       
    646599        htc_send_data_message(ar9271->htc_device, complete_buffer,
    647                 complete_size, endpoint);
     600            complete_size, endpoint);
    648601       
    649602        free(complete_buffer);
     
    661614       
    662615        int rc = hw_reset(ar9271);
    663         if(rc != EOK) {
     616        if (rc != EOK) {
    664617                usb_log_error("Failed to do HW reset.\n");
    665618                return rc;
     
    667620       
    668621        uint16_t htc_mode = host2uint16_t_be(1);
    669         wmi_send_command(ar9271->htc_device, WMI_SET_MODE, 
    670                 (uint8_t *) &htc_mode, sizeof(htc_mode), NULL);
     622        wmi_send_command(ar9271->htc_device, WMI_SET_MODE,
     623            (uint8_t *) &htc_mode, sizeof(htc_mode), NULL);
    671624        wmi_send_command(ar9271->htc_device, WMI_ATH_INIT, NULL, 0, NULL);
    672625        wmi_send_command(ar9271->htc_device, WMI_START_RECV, NULL, 0, NULL);
     
    674627       
    675628        rc = hw_rx_init(ar9271);
    676         if(rc != EOK) {
     629        if (rc != EOK) {
    677630                usb_log_error("Failed to initialize RX.\n");
    678631                return rc;
     
    681634        /* Send capability message to target. */
    682635        htc_cap_msg_t cap_msg;
    683         cap_msg.ampdu_limit = host2uint32_t_be(0xFFFF);
    684         cap_msg.ampdu_subframes = 0xFF;
     636        cap_msg.ampdu_limit = host2uint32_t_be(0xffff);
     637        cap_msg.ampdu_subframes = 0xff;
    685638        cap_msg.enable_coex = 0;
    686639        cap_msg.tx_chainmask = 0x1;
    687640       
    688641        wmi_send_command(ar9271->htc_device, WMI_TARGET_IC_UPDATE,
    689                 (uint8_t *) &cap_msg, sizeof(cap_msg), NULL);
     642            (uint8_t *) &cap_msg, sizeof(cap_msg), NULL);
    690643       
    691644        rc = htc_init_new_vif(ar9271->htc_device);
    692         if(rc != EOK) {
     645        if (rc != EOK) {
    693646                usb_log_error("Failed to initialize new VIF.\n");
    694647                return rc;
     
    697650        /* Add data polling fibril. */
    698651        fid_t fibril = fibril_create(ar9271_data_polling, ar9271);
    699         if (fibril == 0) {
     652        if (fibril == 0)
    700653                return ENOMEM;
    701         }
     654       
    702655        fibril_add_ready(fibril);
    703656       
     
    725678       
    726679        int rc = ath_usb_init(ar9271->ath_device, usb_device);
    727         if(rc != EOK) {
     680        if (rc != EOK) {
    728681                free(ar9271->ath_device);
    729682                usb_log_error("Failed to initialize ath device.\n");
     
    741694       
    742695        rc = ieee80211_device_init(ar9271->ieee80211_dev, ar9271->ddf_dev);
    743         if(rc != EOK) {
     696        if (rc != EOK) {
    744697                free(ar9271->ieee80211_dev);
    745698                free(ar9271->ath_device);
    746699                usb_log_error("Failed to initialize IEEE80211 device structure."
    747                         "\n");
     700                    "\n");
    748701                return rc;
    749702        }
    750703       
    751704        ieee80211_set_specific(ar9271->ieee80211_dev, ar9271);
    752                
     705       
    753706        /* HTC device structure initialization. */
    754707        ar9271->htc_device = calloc(1, sizeof(htc_device_t));
    755         if(!ar9271->htc_device) {
     708        if (!ar9271->htc_device) {
    756709                free(ar9271->ieee80211_dev);
    757710                free(ar9271->ath_device);
     
    761714        }
    762715       
    763         rc = htc_device_init(ar9271->ath_device, ar9271->ieee80211_dev, 
    764                 ar9271->htc_device);
    765         if(rc != EOK) {
     716        rc = htc_device_init(ar9271->ath_device, ar9271->ieee80211_dev,
     717            ar9271->htc_device);
     718        if (rc != EOK) {
    766719                free(ar9271->htc_device);
    767720                free(ar9271->ieee80211_dev);
     
    774727}
    775728
    776 /**
    777  * Upload firmware to WiFi device.
    778  *
     729/** Upload firmware to WiFi device.
     730 *
    779731 * @param ar9271 AR9271 device structure
    780  * 
     732 *
    781733 * @return EOK if succeed, negative error code otherwise
     734 *
    782735 */
    783736static int ar9271_upload_fw(ar9271_t *ar9271)
    784737{
    785         int rc;
    786        
    787738        usb_device_t *usb_device = ar9271->usb_device;
    788        
    789         /* TODO: Set by maximum packet size in pipe. */
     739       
     740        /* TODO: Set by maximum packet size in pipe. */
    790741        static const size_t MAX_TRANSFER_SIZE = 512;
    791742       
     
    802753       
    803754        void *fw_data = malloc(file_size);
    804         if(fw_data == NULL) {
     755        if (fw_data == NULL) {
    805756                fclose(fw_file);
    806757                usb_log_error("Failed allocating memory for firmware.\n");
    807758                return ENOMEM;
    808759        }
    809 
     760       
    810761        fread(fw_data, file_size, 1, fw_file);
    811762        fclose(fw_file);
     
    816767        uint8_t *current_data = fw_data;
    817768        uint8_t *buffer = malloc(MAX_TRANSFER_SIZE);
    818         while(remain_size > 0) {
     769       
     770        while (remain_size > 0) {
    819771                size_t chunk_size = min(remain_size, MAX_TRANSFER_SIZE);
    820772                memcpy(buffer, current_data, chunk_size);
    821                 rc = usb_control_request_set(&usb_device->ctrl_pipe,
     773                int rc = usb_control_request_set(&usb_device->ctrl_pipe,
    822774                    USB_REQUEST_TYPE_VENDOR,
    823775                    USB_REQUEST_RECIPIENT_DEVICE,
     
    825777                    uint16_host2usb(current_addr >> 8),
    826778                    0, buffer, chunk_size);
    827                 if(rc != EOK) {
     779                if (rc != EOK) {
    828780                        free(fw_data);
    829781                        free(buffer);
     
    841793        free(buffer);
    842794       
    843         /* Send command that firmware is successfully uploaded.
    844          * This should initiate creating confirmation message in
    845          * device side buffer which we will check in htc_check_ready function.
    846          */
    847         rc = usb_control_request_set(&usb_device->ctrl_pipe,
     795        /*
     796         * Send command that firmware is successfully uploaded.
     797         * This should initiate creating confirmation message in
     798         * device side buffer which we will check in htc_check_ready function.
     799        */
     800        int rc = usb_control_request_set(&usb_device->ctrl_pipe,
    848801            USB_REQUEST_TYPE_VENDOR,
    849802            USB_REQUEST_RECIPIENT_DEVICE,
     
    852805            0, NULL, 0);
    853806       
    854         if(rc != EOK) {
     807        if (rc != EOK) {
    855808                usb_log_error("IO error when sending fw upload confirmation "
    856                         "message.\n");
     809                    "message.\n");
    857810                return rc;
    858811        }
     
    867820
    868821/** Create driver data structure.
    869  * 
     822 *
    870823 *  @param dev The device structure
    871  * 
     824 *
    872825 *  @return Intialized device data structure or NULL if error occured
    873826 */
     
    883836        const char *err_msg = NULL;
    884837        int rc = usb_device_init(usb_device, dev, endpoints, &err_msg);
    885         if(rc != EOK) {
     838        if (rc != EOK) {
    886839                free(usb_device);
    887840                usb_log_error("Failed to create USB device: %s, "
     
    902855       
    903856        rc = ar9271_init(ar9271, usb_device);
    904         if(rc != EOK) {
     857        if (rc != EOK) {
    905858                free(ar9271);
    906859                free(usb_device);
    907                 usb_log_error("Failed to initialize AR9271 structure: %d\n", 
    908                         rc);
     860                usb_log_error("Failed to initialize AR9271 structure: %d\n",
     861                    rc);
    909862                return NULL;
    910863        }
     
    948901        /* Initialize AR9271 HTC services. */
    949902        int rc = htc_init(ar9271->htc_device);
    950         if(rc != EOK) {
     903        if (rc != EOK) {
    951904                ar9271_delete_dev_data(ar9271);
    952905                usb_log_error("HTC initialization failed.\n");
     
    956909        /* Initialize AR9271 HW. */
    957910        rc = hw_init(ar9271);
    958         if(rc != EOK) {
     911        if (rc != EOK) {
    959912                ar9271_delete_dev_data(ar9271);
    960913                usb_log_error("HW initialization failed.\n");
     
    964917        /* Initialize AR9271 IEEE 802.11 framework. */
    965918        rc = ieee80211_init(ar9271->ieee80211_dev, &ar9271_ieee80211_ops,
    966                 &ar9271_ieee80211_iface, &ar9271_ieee80211_nic_iface,
    967                 &ar9271_ieee80211_dev_ops);
    968         if(rc != EOK) {
     919            &ar9271_ieee80211_iface, &ar9271_ieee80211_nic_iface,
     920            &ar9271_ieee80211_dev_ops);
     921        if (rc != EOK) {
    969922                ar9271_delete_dev_data(ar9271);
    970923                usb_log_error("Failed to initialize IEEE80211 framework.\n");
     
    973926       
    974927        nic_set_filtering_change_handlers(nic_get_from_ddf_dev(dev),
    975                 ar9271_on_unicast_mode_change, ar9271_on_multicast_mode_change,
    976                 ar9271_on_broadcast_mode_change, NULL, NULL);
     928            ar9271_on_unicast_mode_change, ar9271_on_multicast_mode_change,
     929            ar9271_on_broadcast_mode_change, NULL, NULL);
    977930       
    978931        usb_log_info("HelenOS AR9271 added device.\n");
     
    989942       
    990943        usb_log_info("HelenOS AR9271 driver started.\n");
    991 
     944       
    992945        return ddf_driver_main(&ar9271_driver);
    993946}
  • uspace/drv/nic/ar9271/ar9271.h

    r09044cb r8a64320e  
    3737
    3838#include <usb/dev/driver.h>
    39 
    4039#include "htc.h"
    4140
    4241/** Number of transmission queues */
    43 #define AR9271_QUEUES_COUNT 10
     42#define AR9271_QUEUES_COUNT  10
    4443
    4544/** Number of GPIO pin used for handling led light */
    46 #define AR9271_LED_PIN 15
     45#define AR9271_LED_PIN  15
    4746
    4847/** Nominal value for AR9271 noise floor calibration. */
    49 #define AR9271_CALIB_NOMINAL_VALUE_2GHZ -118
     48#define AR9271_CALIB_NOMINAL_VALUE_2GHZ  -118
    5049
    5150/** RX errors values. */
    52 #define AR9271_RX_ERROR_CRC 0x01
    53 #define AR9271_RX_ERROR_PHY 0x02
     51#define AR9271_RX_ERROR_CRC  0x01
     52#define AR9271_RX_ERROR_PHY  0x02
    5453
    5554/** Key index used for device in station mode. */
    56 #define AR9271_STA_KEY_INDEX 4
     55#define AR9271_STA_KEY_INDEX  4
    5756
    5857/* HW encryption key indicator. */
     
    8382        AR9271_RC = 0x4000,
    8483        AR9271_RC_AHB = 0x00000001,
    85                
     84       
    8685        /* GPIO registers */
    87         AR9271_GPIO_IN_OUT = 0x4048,            /**< GPIO value read/set  */
    88         AR9271_GPIO_OE_OUT = 0x404C,            /**< GPIO set to output  */
    89         AR9271_GPIO_OE_OUT_ALWAYS = 0x3,        /**< GPIO always drive output */
     86        AR9271_GPIO_IN_OUT = 0x4048,       /**< GPIO value read/set  */
     87        AR9271_GPIO_OE_OUT = 0x404C,       /**< GPIO set to output  */
     88        AR9271_GPIO_OE_OUT_ALWAYS = 0x3,   /**< GPIO always drive output */
    9089        AR9271_GPIO_OUT_MUX1 = 0x4060,
    9190        AR9271_GPIO_OUT_MUX2 = 0x4064,
    9291        AR9271_GPIO_OUT_MUX3 = 0x4068,
    93         AR9271_GPIO_OUT_MUX_AS_OUT = 0x0,       /**< GPIO set mux as output */
    94    
     92        AR9271_GPIO_OUT_MUX_AS_OUT = 0x0,  /**< GPIO set mux as output */
     93       
    9594        /* RTC related registers */
    9695        AR9271_RTC_RC = 0x7000,
     
    109108        AR9271_RTC_FORCE_WAKE_ENABLE = 0x00000001,
    110109        AR9271_RTC_FORCE_WAKE_ON_INT = 0x00000002,
    111                
     110       
    112111        /* MAC Registers */
    113         AR9271_STATION_ID0 = 0x8000,    /**< STA Address Lower 32 Bits */
    114         AR9271_STATION_ID1 = 0x8004,    /**< STA Address Upper 16 Bits */
    115         AR9271_BSSID0 = 0x8008,         /**< BSSID Lower 32 Bits */
    116         AR9271_BSSID1 = 0x800C,         /**< BSSID Upper 16 Bits */
    117         AR9271_BSSID_MASK0 = 0x80E0,    /**< BSSID Mask Lower 32 Bits */
    118         AR9271_BSSID_MASK1 = 0x80E4,    /**< BSSID Mask Upper 16 Bits */
     112        AR9271_STATION_ID0 = 0x8000,  /**< STA Address Lower 32 Bits */
     113        AR9271_STATION_ID1 = 0x8004,  /**< STA Address Upper 16 Bits */
     114        AR9271_BSSID0 = 0x8008,       /**< BSSID Lower 32 Bits */
     115        AR9271_BSSID1 = 0x800C,       /**< BSSID Upper 16 Bits */
     116        AR9271_BSSID_MASK0 = 0x80E0,  /**< BSSID Mask Lower 32 Bits */
     117        AR9271_BSSID_MASK1 = 0x80E4,  /**< BSSID Mask Upper 16 Bits */
    119118        AR9271_STATION_ID1_MASK = 0x0000FFFF,
    120119        AR9271_STATION_ID1_POWER_SAVING = 0x00040000,
    121120        AR9271_MULTICAST_FILTER1 = 0x8040,
    122         AR9271_MULTICAST_FILTER2 = 0x8044,     
     121        AR9271_MULTICAST_FILTER2 = 0x8044,
    123122        AR9271_DIAG = 0x8048,
    124                
     123       
    125124        /* RX filtering register */
    126125        AR9271_RX_FILTER = 0x803C,
     
    134133        AR9271_RX_FILTER_MYBEACON = 0x00000200,
    135134        AR9271_RX_FILTER_MCAST_BCAST_ALL = 0x00008000,
    136                
     135       
    137136        /* Key related registers */
    138137        AR9271_KEY_TABLE_BASE = 0x8800,
     
    142141        AR9271_KEY_TABLE_TYPE_CCMP = 0x6,
    143142        AR9271_KEY_TABLE_TYPE_CLR = 0x7,
    144                
     143       
    145144        /* Physical layer registers */
    146145        AR9271_PHY_ACTIVE = 0x981C,
     
    168167        AR9271_PHY_TPCRG1_PD_CALIB = 0x00400000,
    169168        AR9271_CARRIER_LEAK_CALIB = 0x00000002,
    170                
     169       
    171170        AR9271_OPMODE_STATION_AP_MASK = 0x00010000,
    172171        AR9271_OPMODE_ADHOC_MASK = 0x00020000,
    173                
     172       
    174173        AR9271_CLOCK_CONTROL = 0x50040,
    175174        AR9271_MAX_CPU_CLOCK = 0x304,
    176                
     175       
    177176        AR9271_RESET_POWER_DOWN_CONTROL = 0x50044,
    178177        AR9271_RADIO_RF_RESET = 0x20,
    179178        AR9271_GATE_MAC_CONTROL = 0x4000,
    180    
     179       
    181180        /* FW Addresses */
    182181        AR9271_FW_ADDRESS = 0x501000,
     
    185184
    186185/** Compute key table base position for key by its id. */
    187 #define AR9271_KEY_TABLE(id) (AR9271_KEY_TABLE_BASE + (id)*32)
     186#define AR9271_KEY_TABLE(id)  (AR9271_KEY_TABLE_BASE + (id) * 32)
    188187
    189188/** AR9271 Requests */
     
    217216} ar9271_t;
    218217
    219 /**
    220  * AR9271 init values for 2GHz mode operation.
    221  *
     218/** AR9271 init values for 2GHz mode operation.
     219 *
    222220 * Including settings of noise floor limits.
    223  *
    224  * Taken from Linux sources.
     221 *
     222 * Taken from the Linux driver (drivers/net/wireless/ath/ath9k/)
     223 * Copyright (c) 2008-2011 Atheros Communications Inc.
     224 * Licensed under the terms of ISC
     225 *
    225226 */
    226227static const uint32_t ar9271_2g_mode_array[][2] = {
     
    232233        {0x0000801c, 0x12e0002b},
    233234        {0x00008318, 0x00003440},
    234         {0x00009804, 0x000003c0},/*< note: overridden */
     235        {0x00009804, 0x000003c0},  /*< Note: overridden */
    235236        {0x00009820, 0x02020200},
    236237        {0x00009824, 0x01000e0e},
    237         {0x00009828, 0x0a020001},/*< note: overridden */
     238        {0x00009828, 0x0a020001},  /*< Note: overridden */
    238239        {0x00009834, 0x00000e0e},
    239240        {0x00009838, 0x00000007},
     
    530531};
    531532
    532 /**
    533  * AR9271 TX init values for 2GHz mode operation.
    534  *
    535  * Taken from Linux sources.
     533/** AR9271 TX init values for 2GHz mode operation.
     534 *
     535 * Taken from the Linux driver (drivers/net/wireless/ath/ath9k/)
     536 * Copyright (c) 2008-2011 Atheros Communications Inc.
     537 * Licensed under the terms of ISC
     538 *
    536539 */
    537540static const uint32_t ar9271_2g_tx_array[][2] = {
     
    571574};
    572575
    573 /**
    574  * AR9271 hardware init values.
    575  *
    576  * Taken from Linux sources, some values omitted.
     576/** AR9271 hardware init values.
     577 *
     578 * Taken from the Linux driver (drivers/net/wireless/ath/ath9k/)
     579 * Copyright (c) 2008-2011 Atheros Communications Inc.
     580 * Licensed under the terms of ISC
     581 *
    577582 */
    578583static const uint32_t ar9271_init_array[][2] = {
     
    766771        {0x0000833c, 0x00000000},
    767772        {0x00008340, 0x00010380},
    768         {0x00008344, 0x00481083},/*< note: disabled ADHOC_MCAST_KEYID feature */
     773        {0x00008344, 0x00481083},  /**< Note: disabled ADHOC_MCAST_KEYID feature */
    769774        {0x00007010, 0x00000030},
    770775        {0x00007034, 0x00000002},
  • uspace/drv/nic/ar9271/ath.h

    r09044cb r8a64320e  
    3434
    3535#ifndef ATHEROS_ATH_H
    36 #define ATHEROS_ATH_H
     36#define ATHEROS_ATH_H
    3737
    3838struct ath;
     
    6161} ath_t;
    6262
    63 #endif  /* ATHEROS_ATH_H */
     63#endif  /* ATHEROS_ATH_H */
  • uspace/drv/nic/ar9271/ath_usb.c

    r09044cb r8a64320e  
    3636#include <usb/debug.h>
    3737#include <malloc.h>
    38 
    3938#include "ath_usb.h"
    4039
    41 static int ath_usb_send_ctrl_message(ath_t *ath, void *buffer,
    42         size_t buffer_size);
    43 
    44 static int ath_usb_read_ctrl_message(ath_t *ath, void *buffer,
    45         size_t buffer_size, size_t *transferred_size);
    46 
    47 static int ath_usb_send_data_message(ath_t *ath, void *buffer,
    48         size_t buffer_size);
    49 
    50 static int ath_usb_read_data_message(ath_t *ath, void *buffer,
    51         size_t buffer_size, size_t *transferred_size);
     40static int ath_usb_send_ctrl_message(ath_t *, void *, size_t);
     41static int ath_usb_read_ctrl_message(ath_t *, void *, size_t, size_t *);
     42static int ath_usb_send_data_message(ath_t *, void *, size_t);
     43static int ath_usb_read_data_message(ath_t *, void *, size_t, size_t *);
    5244
    5345static ath_ops_t ath_usb_ops = {
     
    5850};
    5951
    60 /**
    61  * Initialize Atheros WiFi USB device.
    62  *
     52/** Initialize Atheros WiFi USB device.
     53 *
    6354 * @param ath Generic Atheros WiFi device structure.
    64  * @param usb_device Connected USB device.
    65  * 
     55 * @param usb_device  Connected USB device.
     56 *
    6657 * @return EOK if succeed, negative error code otherwise.
     58 *
    6759 */
    6860int ath_usb_init(ath_t *ath, usb_device_t *usb_device)
     
    9284}
    9385
    94 /**
    95  * Send control message.
    96  *
    97  * @param ath Generic Atheros WiFi device structure.
    98  * @param buffer Buffer with data to send.
     86/** Send control message.
     87 *
     88 * @param ath         Generic Atheros WiFi device structure.
     89 * @param buffer      Buffer with data to send.
    9990 * @param buffer_size Buffer size.
    100  * 
     91 *
    10192 * @return EOK if succeed, negative error code otherwise.
     93 *
    10294 */
    103 static int ath_usb_send_ctrl_message(ath_t *ath, void *buffer, 
    104         size_t buffer_size)
     95static int ath_usb_send_ctrl_message(ath_t *ath, void *buffer,
     96    size_t buffer_size)
    10597{
    10698        ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
    107         usb_pipe_t *pipe =
    108                 &ath_usb->usb_device->pipes[ath_usb->output_ctrl_pipe_number].
    109                 pipe;
     99        usb_pipe_t *pipe =
     100            &ath_usb->usb_device->pipes[ath_usb->output_ctrl_pipe_number].pipe;
    110101       
    111102        return usb_pipe_write(pipe, buffer, buffer_size);
    112103}
    113104
    114 /**
    115  * Read control message.
    116  *
    117  * @param ath Generic Atheros WiFi device structure.
    118  * @param buffer Buffer with data to send.
    119  * @param buffer_size Buffer size.
     105/** Read control message.
     106 *
     107 * @param ath              Generic Atheros WiFi device structure.
     108 * @param buffer           Buffer with data to send.
     109 * @param buffer_size      Buffer size.
    120110 * @param transferred_size Real size of read data.
    121  * 
     111 *
    122112 * @return EOK if succeed, negative error code otherwise.
     113 *
    123114 */
    124 static int ath_usb_read_ctrl_message(ath_t *ath, void *buffer, 
    125         size_t buffer_size, size_t *transferred_size)
     115static int ath_usb_read_ctrl_message(ath_t *ath, void *buffer,
     116    size_t buffer_size, size_t *transferred_size)
    126117{
    127118        ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
    128         usb_pipe_t *pipe =
    129                 &ath_usb->usb_device->pipes[ath_usb->input_ctrl_pipe_number].
    130                 pipe;
     119        usb_pipe_t *pipe =
     120            &ath_usb->usb_device->pipes[ath_usb->input_ctrl_pipe_number].pipe;
    131121       
    132122        return usb_pipe_read(pipe, buffer, buffer_size, transferred_size);
    133123}
    134124
    135 /**
    136  * Send data message.
    137  *
    138  * @param ath Generic Atheros WiFi device structure.
    139  * @param buffer Buffer with data to send.
     125/** Send data message.
     126 *
     127 * @param ath         Generic Atheros WiFi device structure.
     128 * @param buffer      Buffer with data to send.
    140129 * @param buffer_size Buffer size.
    141  * 
     130 *
    142131 * @return EOK if succeed, negative error code otherwise.
     132 *
    143133 */
    144 static int ath_usb_send_data_message(ath_t *ath, void *buffer, 
    145         size_t buffer_size)
     134static int ath_usb_send_data_message(ath_t *ath, void *buffer,
     135    size_t buffer_size)
    146136{
    147         size_t complete_buffer_size = buffer_size + 
    148                 sizeof(ath_usb_data_header_t);
     137        size_t complete_buffer_size = buffer_size +
     138            sizeof(ath_usb_data_header_t);
    149139        void *complete_buffer = malloc(complete_buffer_size);
    150         memcpy(complete_buffer + sizeof(ath_usb_data_header_t), 
    151                 buffer, buffer_size);
     140        memcpy(complete_buffer + sizeof(ath_usb_data_header_t),
     141            buffer, buffer_size);
    152142       
    153         ath_usb_data_header_t *data_header = 
    154                 (ath_usb_data_header_t *) complete_buffer;
     143        ath_usb_data_header_t *data_header =
     144            (ath_usb_data_header_t *) complete_buffer;
    155145        data_header->length = host2uint16_t_le(buffer_size);
    156146        data_header->tag = host2uint16_t_le(TX_TAG);
    157147       
    158148        ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
    159         usb_pipe_t *pipe =
    160                 &ath_usb->usb_device->pipes[ath_usb->output_data_pipe_number].
    161                 pipe;
     149        usb_pipe_t *pipe =
     150            &ath_usb->usb_device->pipes[ath_usb->output_data_pipe_number].pipe;
    162151       
    163         int ret_val = usb_pipe_write(pipe, complete_buffer, 
    164                 complete_buffer_size);
     152        int ret_val = usb_pipe_write(pipe, complete_buffer,
     153            complete_buffer_size);
    165154       
    166155        free(complete_buffer);
     
    169158}
    170159
    171 /**
    172  * Read data message.
    173  *
    174  * @param ath Generic Atheros WiFi device structure.
    175  * @param buffer Buffer with data to send.
    176  * @param buffer_size Buffer size.
     160/** Read data message.
     161 *
     162 * @param ath              Generic Atheros WiFi device structure.
     163 * @param buffer           Buffer with data to send.
     164 * @param buffer_size      Buffer size.
    177165 * @param transferred_size Real size of read data.
    178  * 
     166 *
    179167 * @return EOK if succeed, negative error code otherwise.
     168 *
    180169 */
    181 static int ath_usb_read_data_message(ath_t *ath, void *buffer, 
    182         size_t buffer_size, size_t *transferred_size)
     170static int ath_usb_read_data_message(ath_t *ath, void *buffer,
     171    size_t buffer_size, size_t *transferred_size)
    183172{
    184173        ath_usb_t *ath_usb = (ath_usb_t *) ath->specific_data;
    185         usb_pipe_t *pipe =
    186                 &ath_usb->usb_device->pipes[ath_usb->input_data_pipe_number].
    187                 pipe;
     174        usb_pipe_t *pipe =
     175            &ath_usb->usb_device->pipes[ath_usb->input_data_pipe_number].pipe;
    188176       
    189         return usb_pipe_read(pipe, buffer, buffer_size, 
    190                 transferred_size);
     177        return usb_pipe_read(pipe, buffer, buffer_size,
     178            transferred_size);
    191179}
  • uspace/drv/nic/ar9271/ath_usb.h

    r09044cb r8a64320e  
    3434
    3535#ifndef ATHEROS_ATH_USB_H
    36 #define ATHEROS_ATH_USB_H
     36#define ATHEROS_ATH_USB_H
    3737
    3838#include <usb/dev/driver.h>
    39 
    4039#include "ath.h"
    4140
    42 #define RX_TAG 0x4e00
    43 #define TX_TAG 0x697e
     41#define RX_TAG  0x4e00
     42#define TX_TAG  0x697e
    4443
    4544/** Atheros USB wifi device structure */
     
    5655
    5756typedef struct {
    58         uint16_t length;                /**< Little Endian value! */
    59         uint16_t tag;                   /**< Little Endian value! */
     57        uint16_t length;  /**< Little Endian value! */
     58        uint16_t tag;     /**< Little Endian value! */
    6059} ath_usb_data_header_t;
    6160
    62 extern int ath_usb_init(ath_t *ath, usb_device_t *usb_device);
     61extern int ath_usb_init(ath_t *, usb_device_t *);
    6362
    64 #endif  /* ATHEROS_ATH_USB_H */
     63#endif  /* ATHEROS_ATH_USB_H */
  • uspace/drv/nic/ar9271/htc.c

    r09044cb r8a64320e  
    3636#include <byteorder.h>
    3737#include <errno.h>
    38 
    3938#include "wmi.h"
    4039#include "htc.h"
     
    4241#include "ar9271.h"
    4342
    44 /**
    45  * HTC download pipes mapping.
    46  *
     43/** HTC download pipes mapping.
     44 *
    4745 * @param service_id Identification of WMI service.
    48  * 
     46 *
    4947 * @return Number of pipe used for this service.
     48 *
    5049 */
    5150static inline uint8_t wmi_service_to_download_pipe(wmi_services_t service_id)
     
    5453}
    5554
    56 /**
    57  * HTC upload pipes mapping.
    58  *
     55/** HTC upload pipes mapping.
     56 *
    5957 * @param service_id Identification of WMI service.
    60  * 
     58 *
    6159 * @return Number of pipe used for this service.
     60 *
    6261 */
    6362static inline uint8_t wmi_service_to_upload_pipe(wmi_services_t service_id)
     
    7574       
    7675        nic_address_t addr;
    77         nic_t *nic =
    78                 nic_get_from_ddf_dev(
    79                         ieee80211_get_ddf_dev(htc_device->ieee80211_dev)
    80                 );
     76        nic_t *nic =
     77            nic_get_from_ddf_dev(ieee80211_get_ddf_dev(htc_device->ieee80211_dev));
    8178        nic_query_address(nic, &addr);
    8279       
     
    8481        memcpy(&sta_msg.addr, &addr.address, ETH_ADDR);
    8582       
    86         ieee80211_operating_mode_t op_mode = 
    87                 ieee80211_query_current_op_mode(htc_device->ieee80211_dev);
    88        
    89         switch(op_mode) {
    90                 case IEEE80211_OPMODE_ADHOC:
    91                         vif_msg.op_mode = HTC_OPMODE_ADHOC;
    92                         break;
    93                 case IEEE80211_OPMODE_AP:
    94                         vif_msg.op_mode = HTC_OPMODE_AP;
    95                         break;
    96                 case IEEE80211_OPMODE_MESH:
    97                         vif_msg.op_mode = HTC_OPMODE_MESH;
    98                         break;
    99                 case IEEE80211_OPMODE_STATION:
    100                         vif_msg.op_mode = HTC_OPMODE_STATION;
    101                         break;
     83        ieee80211_operating_mode_t op_mode =
     84            ieee80211_query_current_op_mode(htc_device->ieee80211_dev);
     85       
     86        switch (op_mode) {
     87        case IEEE80211_OPMODE_ADHOC:
     88                vif_msg.op_mode = HTC_OPMODE_ADHOC;
     89                break;
     90        case IEEE80211_OPMODE_AP:
     91                vif_msg.op_mode = HTC_OPMODE_AP;
     92                break;
     93        case IEEE80211_OPMODE_MESH:
     94                vif_msg.op_mode = HTC_OPMODE_MESH;
     95                break;
     96        case IEEE80211_OPMODE_STATION:
     97                vif_msg.op_mode = HTC_OPMODE_STATION;
     98                break;
    10299        }
    103100       
     
    105102        vif_msg.rts_thres = host2uint16_t_be(HTC_RTS_THRESHOLD);
    106103       
    107         wmi_send_command(htc_device, WMI_VAP_CREATE, (uint8_t *) &vif_msg, 
    108                 sizeof(vif_msg), NULL);
     104        wmi_send_command(htc_device, WMI_VAP_CREATE, (uint8_t *) &vif_msg,
     105            sizeof(vif_msg), NULL);
    109106       
    110107        sta_msg.is_vif_sta = 1;
     
    113110        sta_msg.vif_index = 0;
    114111       
    115         wmi_send_command(htc_device, WMI_NODE_CREATE, (uint8_t *) &sta_msg, 
    116                 sizeof(sta_msg), NULL);
     112        wmi_send_command(htc_device, WMI_NODE_CREATE, (uint8_t *) &sta_msg,
     113            sizeof(sta_msg), NULL);
    117114       
    118115        /* Write first 4 bytes of MAC address. */
     
    121118        id0 = host2uint32_t_le(id0);
    122119        wmi_reg_write(htc_device, AR9271_STATION_ID0, id0);
    123 
     120       
    124121        /* Write last 2 bytes of MAC address (and preserve existing data). */
    125122        uint32_t id1;
    126123        wmi_reg_read(htc_device, AR9271_STATION_ID1, &id1);
    127 
     124       
    128125        uint16_t id1_addr;
    129126        memcpy(&id1_addr, &addr.address[4], 2);
     
    134131}
    135132
    136 static void htc_config_frame_header(htc_frame_header_t *header, 
    137         size_t buffer_size, uint8_t endpoint_id)
     133static void htc_config_frame_header(htc_frame_header_t *header,
     134    size_t buffer_size, uint8_t endpoint_id)
    138135{
    139136        memset(header, 0, sizeof(htc_frame_header_t));
    140137       
    141138        header->endpoint_id = endpoint_id;
    142         header->payload_length =
    143                 host2uint16_t_be(buffer_size - sizeof(htc_frame_header_t));
    144 }
    145 
    146 /**
    147  * Send control HTC message to USB device.
    148  *
    149  * @param htc_device HTC device structure.
    150  * @param buffer Buffer with data to be sent to USB device (without HTC frame
    151  *              header).
     139        header->payload_length =
     140            host2uint16_t_be(buffer_size - sizeof(htc_frame_header_t));
     141}
     142
     143/** Send control HTC message to USB device.
     144 *
     145 * @param htc_device  HTC device structure.
     146 * @param buffer      Buffer with data to be sent to USB device
     147 *                    (without HTC frame header).
    152148 * @param buffer_size Size of buffer (including HTC frame header).
    153149 * @param endpoint_id Destination endpoint.
    154  *
    155  * @return EOK if succeed, negative error code otherwise.
    156  */
    157 int htc_send_control_message(htc_device_t *htc_device, void *buffer,
    158         size_t buffer_size, uint8_t endpoint_id)
     150 *
     151 * @return EOK if succeed, negative error code otherwise.
     152 *
     153 */
     154int htc_send_control_message(htc_device_t *htc_device, void *buffer,
     155    size_t buffer_size, uint8_t endpoint_id)
    159156{
    160157        htc_config_frame_header((htc_frame_header_t *) buffer, buffer_size,
    161                 endpoint_id);
     158            endpoint_id);
    162159       
    163160        ath_t *ath_device = htc_device->ath_device;
    164161       
    165         return ath_device->ops->send_ctrl_message(ath_device, buffer,
    166                 buffer_size);
    167 }
    168 
    169 /**
    170  * Send data HTC message to USB device.
    171  *
    172  * @param htc_device HTC device structure.
    173  * @param buffer Buffer with data to be sent to USB device (without HTC frame
    174  *              header).
     162        return ath_device->ops->send_ctrl_message(ath_device, buffer,
     163            buffer_size);
     164}
     165
     166/** Send data HTC message to USB device.
     167 *
     168 * @param htc_device  HTC device structure.
     169 * @param buffer      Buffer with data to be sent to USB device
     170 *                    (without HTC frame header).
    175171 * @param buffer_size Size of buffer (including HTC frame header).
    176172 * @param endpoint_id Destination endpoint.
    177  *
    178  * @return EOK if succeed, negative error code otherwise.
    179  */
    180 int htc_send_data_message(htc_device_t *htc_device, void *buffer,
    181         size_t buffer_size, uint8_t endpoint_id)
     173 *
     174 * @return EOK if succeed, negative error code otherwise.
     175 *
     176 */
     177int htc_send_data_message(htc_device_t *htc_device, void *buffer,
     178    size_t buffer_size, uint8_t endpoint_id)
    182179{
    183180        htc_config_frame_header((htc_frame_header_t *) buffer, buffer_size,
    184                 endpoint_id);
     181            endpoint_id);
    185182       
    186183        ath_t *ath_device = htc_device->ath_device;
    187184       
    188         return ath_device->ops->send_data_message(ath_device, buffer, 
    189                 buffer_size);
    190 }
    191 
    192 /**
    193  * Read HTC data message from USB device.
    194  *
    195  * @param htc_device HTC device structure.
    196  * @param buffer Buffer where data from USB device will be stored.
    197  * @param buffer_size Size of buffer.
     185        return ath_device->ops->send_data_message(ath_device, buffer,
     186            buffer_size);
     187}
     188
     189/** Read HTC data message from USB device.
     190 *
     191 * @param htc_device       HTC device structure.
     192 * @param buffer           Buffer where data from USB device
     193 *                         will be stored.
     194 * @param buffer_size      Size of buffer.
    198195 * @param transferred_size Real size of read data.
    199  *
    200  * @return EOK if succeed, negative error code otherwise.
    201  */
    202 int htc_read_data_message(htc_device_t *htc_device, void *buffer,
    203         size_t buffer_size, size_t *transferred_size)
     196 *
     197 * @return EOK if succeed, negative error code otherwise.
     198 *
     199 */
     200int htc_read_data_message(htc_device_t *htc_device, void *buffer,
     201    size_t buffer_size, size_t *transferred_size)
    204202{
    205203        ath_t *ath_device = htc_device->ath_device;
    206204       
    207         return ath_device->ops->read_data_message(ath_device, buffer, 
    208                 buffer_size, transferred_size);
    209 }
    210 
    211 /**
    212  * Read HTC control message from USB device.
    213  *
    214  * @param htc_device HTC device structure.
    215  * @param buffer Buffer where data from USB device will be stored.
    216  * @param buffer_size Size of buffer.
     205        return ath_device->ops->read_data_message(ath_device, buffer,
     206            buffer_size, transferred_size);
     207}
     208
     209/** Read HTC control message from USB device.
     210 *
     211 * @param htc_device       HTC device structure.
     212 * @param buffer           Buffer where data from USB device
     213 *                         will be stored.
     214 * @param buffer_size      Size of buffer.
    217215 * @param transferred_size Real size of read data.
    218  *
    219  * @return EOK if succeed, negative error code otherwise.
    220  */
    221 int htc_read_control_message(htc_device_t *htc_device, void *buffer,
    222         size_t buffer_size, size_t *transferred_size)
     216 *
     217 * @return EOK if succeed, negative error code otherwise.
     218 *
     219 */
     220int htc_read_control_message(htc_device_t *htc_device, void *buffer,
     221    size_t buffer_size, size_t *transferred_size)
    223222{
    224223        ath_t *ath_device = htc_device->ath_device;
    225224       
    226         return ath_device->ops->read_ctrl_message(ath_device, buffer,
    227                 buffer_size, transferred_size);
    228 }
    229 
    230 /**
    231  * Initialize HTC service.
    232  *
    233  * @param htc_device HTC device structure.
    234  * @param service_id Identification of WMI service.
    235  * @param response_endpoint_no HTC endpoint to be used for this service.
    236  *
    237  * @return EOK if succeed, EINVAL when failed to connect service,
    238  * negative error code otherwise.
    239  */
    240 static int htc_connect_service(htc_device_t *htc_device,
     225        return ath_device->ops->read_ctrl_message(ath_device, buffer,
     226            buffer_size, transferred_size);
     227}
     228
     229/** Initialize HTC service.
     230 *
     231 * @param htc_device           HTC device structure.
     232 * @param service_id           Identification of WMI service.
     233 * @param response_endpoint_no HTC endpoint to be used for
     234 *                             this service.
     235 *
     236 * @return EOK if succeed, EINVAL when failed to connect service,
     237 *         negative error code otherwise.
     238 *
     239 */
     240static int htc_connect_service(htc_device_t *htc_device,
    241241    wmi_services_t service_id, int *response_endpoint_no)
    242242{
    243         size_t buffer_size = sizeof(htc_frame_header_t) + 
    244                 sizeof(htc_service_msg_t);
     243        size_t buffer_size = sizeof(htc_frame_header_t) +
     244            sizeof(htc_service_msg_t);
    245245        void *buffer = malloc(buffer_size);
    246246        memset(buffer, 0, buffer_size);
     
    248248        /* Fill service message structure. */
    249249        htc_service_msg_t *service_message = (htc_service_msg_t *)
    250                 ((void *) buffer + sizeof(htc_frame_header_t));
    251         service_message->service_id = 
    252                 host2uint16_t_be(service_id);
    253         service_message->message_id = 
    254                 host2uint16_t_be(HTC_MESSAGE_CONNECT_SERVICE);
    255         service_message->download_pipe_id = 
    256                 wmi_service_to_download_pipe(service_id);
    257         service_message->upload_pipe_id = 
    258                 wmi_service_to_upload_pipe(service_id);
     250            ((void *) buffer + sizeof(htc_frame_header_t));
     251        service_message->service_id =
     252            host2uint16_t_be(service_id);
     253        service_message->message_id =
     254            host2uint16_t_be(HTC_MESSAGE_CONNECT_SERVICE);
     255        service_message->download_pipe_id =
     256            wmi_service_to_download_pipe(service_id);
     257        service_message->upload_pipe_id =
     258            wmi_service_to_upload_pipe(service_id);
    259259        service_message->connection_flags = 0;
    260260       
    261261        /* Send HTC message. */
    262262        int rc = htc_send_control_message(htc_device, buffer, buffer_size,
    263                 htc_device->endpoints.ctrl_endpoint);
    264         if(rc != EOK) {
     263            htc_device->endpoints.ctrl_endpoint);
     264        if (rc != EOK) {
    265265                free(buffer);
    266266                usb_log_error("Failed to send HTC message. Error: %d\n", rc);
     
    275275        /* Read response from device. */
    276276        rc = htc_read_control_message(htc_device, buffer, buffer_size, NULL);
    277         if(rc != EOK) {
     277        if (rc != EOK) {
    278278                free(buffer);
    279279                usb_log_error("Failed to receive HTC service connect response. "
    280                         "Error: %d\n", rc);
     280                    "Error: %d\n", rc);
    281281                return rc;
    282282        }
    283283       
    284284        htc_service_resp_msg_t *response_message = (htc_service_resp_msg_t *)
    285                 ((void *) buffer + sizeof(htc_frame_header_t));
    286 
    287         /* If service was successfully connected, write down HTC endpoint number
    288          * that will be used for communication. */
    289         if(response_message->status == HTC_SERVICE_SUCCESS) {
     285            ((void *) buffer + sizeof(htc_frame_header_t));
     286       
     287        /*
     288         * If service was successfully connected,
     289         * write down HTC endpoint number that will
     290         * be used for communication.
     291         */
     292        if (response_message->status == HTC_SERVICE_SUCCESS) {
    290293                *response_endpoint_no = response_message->endpoint_id;
    291294                rc = EOK;
    292295        } else {
    293296                usb_log_error("Failed to connect HTC service. "
    294                         "Message status: %d\n", response_message->status);
     297                    "Message status: %d\n", response_message->status);
    295298                rc = EINVAL;
    296         }
     299        }
    297300       
    298301        free(buffer);
     
    301304}
    302305
    303 /**
    304  * HTC credits initialization message.
    305  *
     306/** HTC credits initialization message.
     307 *
    306308 * @param htc_device HTC device structure.
    307  *
    308  * @return EOK if succeed, negative error code otherwise.
     309 *
     310 * @return EOK if succeed, negative error code otherwise.
     311 *
    309312 */
    310313static int htc_config_credits(htc_device_t *htc_device)
    311314{
    312         size_t buffer_size = sizeof(htc_frame_header_t) + 
    313                 sizeof(htc_config_msg_t);
     315        size_t buffer_size = sizeof(htc_frame_header_t) +
     316            sizeof(htc_config_msg_t);
    314317        void *buffer = malloc(buffer_size);
    315318        htc_config_msg_t *config_message = (htc_config_msg_t *)
    316                 ((void *) buffer + sizeof(htc_frame_header_t));
     319            ((void *) buffer + sizeof(htc_frame_header_t));
    317320       
    318321        config_message->message_id =
    319                 host2uint16_t_be(HTC_MESSAGE_CONFIG);
     322            host2uint16_t_be(HTC_MESSAGE_CONFIG);
    320323        /* Magic number to initialize device. */
    321324        config_message->credits = 33;
    322325        config_message->pipe_id = 1;
    323 
     326       
    324327        /* Send HTC message. */
    325328        int rc = htc_send_control_message(htc_device, buffer, buffer_size,
    326                 htc_device->endpoints.ctrl_endpoint);
    327         if(rc != EOK) {
     329            htc_device->endpoints.ctrl_endpoint);
     330        if (rc != EOK) {
    328331                free(buffer);
    329332                usb_log_error("Failed to send HTC config message. "
    330                         "Error: %d\n", rc);
     333                    "Error: %d\n", rc);
    331334                return rc;
    332335        }
     
    336339        buffer_size = htc_device->ath_device->ctrl_response_length;
    337340        buffer = malloc(buffer_size);
    338 
     341       
    339342        /* Check response from device. */
    340343        rc = htc_read_control_message(htc_device, buffer, buffer_size, NULL);
    341         if(rc != EOK) {
     344        if (rc != EOK) {
    342345                usb_log_error("Failed to receive HTC config response message. "
    343                         "Error: %d\n", rc);
     346                    "Error: %d\n", rc);
    344347        }
    345348       
    346349        free(buffer);
    347 
     350       
    348351        return rc;
    349352}
    350353
    351 /**
    352  * HTC setup complete confirmation message.
    353  *
     354/** HTC setup complete confirmation message.
     355 *
    354356 * @param htc_device HTC device structure.
    355  *
    356  * @return EOK if succeed, negative error code otherwise.
     357 *
     358 * @return EOK if succeed, negative error code otherwise.
     359 *
    357360 */
    358361static int htc_complete_setup(htc_device_t *htc_device)
    359362{
    360         size_t buffer_size = sizeof(htc_frame_header_t) + 
    361                 sizeof(htc_setup_complete_msg_t);
     363        size_t buffer_size = sizeof(htc_frame_header_t) +
     364            sizeof(htc_setup_complete_msg_t);
    362365        void *buffer = malloc(buffer_size);
    363         htc_setup_complete_msg_t *complete_message = 
    364                 (htc_setup_complete_msg_t *)
    365                 ((void *) buffer + sizeof(htc_frame_header_t));
    366        
    367         complete_message->message_id = 
    368                 host2uint16_t_be(HTC_MESSAGE_SETUP_COMPLETE);
    369 
     366        htc_setup_complete_msg_t *complete_message =
     367            (htc_setup_complete_msg_t *)
     368            ((void *) buffer + sizeof(htc_frame_header_t));
     369       
     370        complete_message->message_id =
     371            host2uint16_t_be(HTC_MESSAGE_SETUP_COMPLETE);
     372       
    370373        /* Send HTC message. */
    371         int rc = htc_send_control_message(htc_device, buffer, buffer_size, 
    372                 htc_device->endpoints.ctrl_endpoint);
    373         if(rc != EOK) {
     374        int rc = htc_send_control_message(htc_device, buffer, buffer_size,
     375            htc_device->endpoints.ctrl_endpoint);
     376        if (rc != EOK)
    374377                usb_log_error("Failed to send HTC setup complete message. "
    375                         "Error: %d\n", rc);
    376         }
     378                    "Error: %d\n", rc);
    377379       
    378380        free(buffer);
    379 
     381       
    380382        return rc;
    381383}
    382384
    383 /**
    384  * Try to fetch ready message from device.
     385/** Try to fetch ready message from device.
     386 *
    385387 * Checks that firmware was successfully loaded on device side.
    386  * 
     388 *
    387389 * @param htc_device HTC device structure.
    388  *
    389  * @return EOK if succeed, EINVAL if response error, negative error code
    390  * otherwise.
     390 *
     391 * @return EOK if succeed, EINVAL if response error,
     392 *         negative error code otherwise.
     393 *
    391394 */
    392395static int htc_check_ready(htc_device_t *htc_device)
     
    394397        size_t buffer_size = htc_device->ath_device->ctrl_response_length;
    395398        void *buffer = malloc(buffer_size);
    396 
     399       
    397400        /* Read response from device. */
    398         int rc = htc_read_control_message(htc_device, buffer, buffer_size, 
    399                 NULL);
    400         if(rc != EOK) {
     401        int rc = htc_read_control_message(htc_device, buffer, buffer_size,
     402            NULL);
     403        if (rc != EOK) {
    401404                free(buffer);
    402405                usb_log_error("Failed to receive HTC check ready message. "
    403                         "Error: %d\n", rc);
    404                 return rc;
    405         }
    406 
    407         uint16_t *message_id = (uint16_t *) ((void *) buffer + 
    408                 sizeof(htc_frame_header_t));
    409         if(uint16_t_be2host(*message_id) == HTC_MESSAGE_READY) {
     406                    "Error: %d\n", rc);
     407                return rc;
     408        }
     409       
     410        uint16_t *message_id = (uint16_t *) ((void *) buffer +
     411            sizeof(htc_frame_header_t));
     412        if (uint16_t_be2host(*message_id) == HTC_MESSAGE_READY)
    410413                rc = EOK;
    411         } else {
     414        else
    412415                rc = EINVAL;
    413         }
    414416       
    415417        free(buffer);
     
    418420}
    419421
    420 /**
    421  * Initialize HTC device structure.
    422  *
    423  * @param ath_device Atheros WiFi device connected with this HTC device.
     422/** Initialize HTC device structure.
     423 *
     424 * @param ath_device Atheros WiFi device connected
     425 *                   with this HTC device.
    424426 * @param htc_device HTC device structure to be initialized.
    425  *
    426  * @return EOK if succeed, negative error code otherwise.
    427  */
    428 int htc_device_init(ath_t *ath_device, ieee80211_dev_t *ieee80211_dev,
    429         htc_device_t *htc_device)
     427 *
     428 * @return EOK if succeed, negative error code otherwise.
     429 *
     430 */
     431int htc_device_init(ath_t *ath_device, ieee80211_dev_t *ieee80211_dev,
     432    htc_device_t *htc_device)
    430433{
    431434        fibril_mutex_initialize(&htc_device->rx_lock);
     
    440443}
    441444
    442 /**
    443  * HTC communication initalization.
    444  *
     445/** HTC communication initalization.
     446 *
    445447 * @param htc_device HTC device structure.
    446  *
    447  * @return EOK if succeed, negative error code otherwise.
     448 *
     449 * @return EOK if succeed, negative error code otherwise.
     450 *
    448451 */
    449452int htc_init(htc_device_t *htc_device)
     
    451454        /* First check ready message in device. */
    452455        int rc = htc_check_ready(htc_device);
    453         if(rc != EOK) {
     456        if (rc != EOK) {
    454457                usb_log_error("Device is not in ready state after loading "
    455                         "firmware.\n");
    456                 return rc;
    457         }
    458 
    459         /* 
     458                    "firmware.\n");
     459                return rc;
     460        }
     461       
     462        /*
    460463         * HTC services initialization start.
    461          *
    462464         */
    463        
    464465        rc = htc_connect_service(htc_device, WMI_CONTROL_SERVICE,
    465466            &htc_device->endpoints.wmi_endpoint);
    466         if(rc != EOK) {
     467        if (rc != EOK) {
    467468                usb_log_error("Error while initalizing WMI service.\n");
    468469                return rc;
    469470        }
    470 
     471       
    471472        rc = htc_connect_service(htc_device, WMI_BEACON_SERVICE,
    472473            &htc_device->endpoints.beacon_endpoint);
    473         if(rc != EOK) {
     474        if (rc != EOK) {
    474475                usb_log_error("Error while initalizing beacon service.\n");
    475476                return rc;
    476477        }
    477 
     478       
    478479        rc = htc_connect_service(htc_device, WMI_CAB_SERVICE,
    479480            &htc_device->endpoints.cab_endpoint);
    480         if(rc != EOK) {
     481        if (rc != EOK) {
    481482                usb_log_error("Error while initalizing CAB service.\n");
    482483                return rc;
    483484        }
    484 
     485       
    485486        rc = htc_connect_service(htc_device, WMI_UAPSD_SERVICE,
    486487            &htc_device->endpoints.uapsd_endpoint);
    487         if(rc != EOK) {
     488        if (rc != EOK) {
    488489                usb_log_error("Error while initalizing UAPSD service.\n");
    489490                return rc;
    490491        }
    491 
     492       
    492493        rc = htc_connect_service(htc_device, WMI_MGMT_SERVICE,
    493494            &htc_device->endpoints.mgmt_endpoint);
    494         if(rc != EOK) {
     495        if (rc != EOK) {
    495496                usb_log_error("Error while initalizing MGMT service.\n");
    496497                return rc;
     
    499500        rc = htc_connect_service(htc_device, WMI_DATA_BE_SERVICE,
    500501            &htc_device->endpoints.data_be_endpoint);
    501         if(rc != EOK) {
     502        if (rc != EOK) {
    502503                usb_log_error("Error while initalizing data best effort "
    503504                    "service.\n");
    504505                return rc;
    505506        }
    506 
     507       
    507508        rc = htc_connect_service(htc_device, WMI_DATA_BK_SERVICE,
    508509            &htc_device->endpoints.data_bk_endpoint);
    509         if(rc != EOK) {
     510        if (rc != EOK) {
    510511                usb_log_error("Error while initalizing data background "
    511512                    "service.\n");
    512513                return rc;
    513514        }
    514 
     515       
    515516        rc = htc_connect_service(htc_device, WMI_DATA_VIDEO_SERVICE,
    516517            &htc_device->endpoints.data_video_endpoint);
    517         if(rc != EOK) {
     518        if (rc != EOK) {
    518519                usb_log_error("Error while initalizing data video service.\n");
    519520                return rc;
     
    522523        rc = htc_connect_service(htc_device, WMI_DATA_VOICE_SERVICE,
    523524            &htc_device->endpoints.data_voice_endpoint);
    524         if(rc != EOK) {
     525        if (rc != EOK) {
    525526                usb_log_error("Error while initalizing data voice service.\n");
    526527                return rc;
    527528        }
    528 
    529         /* 
     529       
     530        /*
    530531         * HTC services initialization end.
    531          *
    532532         */
    533 
     533       
    534534        /* Credits initialization message. */
    535535        rc = htc_config_credits(htc_device);
    536         if(rc != EOK) {
     536        if (rc != EOK) {
    537537                usb_log_error("Failed to send HTC config message.\n");
    538538                return rc;
    539539        }
    540 
     540       
    541541        /* HTC setup complete confirmation message. */
    542542        rc = htc_complete_setup(htc_device);
    543         if(rc != EOK) {
     543        if (rc != EOK) {
    544544                usb_log_error("Failed to send HTC complete setup message.\n");
    545545                return rc;
    546546        }
    547 
     547       
    548548        usb_log_info("HTC services initialization finished successfully.\n");
    549549       
  • uspace/drv/nic/ar9271/htc.h

    r09044cb r8a64320e  
    2929/** @file htc.h
    3030 *
    31  * Definitions of the Atheros HTC (Host Target Communication) technology 
     31 * Definitions of the Atheros HTC (Host Target Communication) technology
    3232 * for communication between host (PC) and target (device firmware).
    3333 *
     
    3535
    3636#ifndef ATHEROS_HTC_H
    37 #define ATHEROS_HTC_H
     37#define ATHEROS_HTC_H
    3838
    3939#include <ieee80211.h>
     
    4141#include <sys/types.h>
    4242#include <nic.h>
    43 
    4443#include "ath.h"
    4544
    46 #define HTC_RTS_THRESHOLD 2304
    47 #define HTC_RATES_MAX_LENGTH 30
    48 
    49 /**
    50  * HTC message IDs.
     45#define HTC_RTS_THRESHOLD     2304
     46#define HTC_RATES_MAX_LENGTH  30
     47
     48/** HTC message IDs.
     49 *
    5150 */
    5251typedef enum {
    5352        HTC_MESSAGE_READY = 1,
    5453        HTC_MESSAGE_CONNECT_SERVICE,
    55         HTC_MESSAGE_CONNECT_SERVICE_RESPONSE,
    56         HTC_MESSAGE_SETUP_COMPLETE,
    57         HTC_MESSAGE_CONFIG
     54        HTC_MESSAGE_CONNECT_SERVICE_RESPONSE,
     55        HTC_MESSAGE_SETUP_COMPLETE,
     56        HTC_MESSAGE_CONFIG
    5857} htc_message_id_t;
    5958
    60 /**
    61  * HTC response message status codes.
     59/** HTC response message status codes.
     60 *
    6261 */
    6362typedef enum {
    64         HTC_SERVICE_SUCCESS = 0,
    65         HTC_SERVICE_NOT_FOUND,
    66         HTC_SERVICE_FAILED,
    67         HTC_SERVICE_NO_RESOURCES,
    68         HTC_SERVICE_NO_MORE_EP
     63        HTC_SERVICE_SUCCESS = 0,
     64        HTC_SERVICE_NOT_FOUND,
     65        HTC_SERVICE_FAILED,
     66        HTC_SERVICE_NO_RESOURCES,
     67        HTC_SERVICE_NO_MORE_EP
    6968} htc_response_status_code_t;
    7069
    71 /**
    72  * HTC operating mode definition.
     70/** HTC operating mode definition.
     71 *
    7372 */
    7473typedef enum {
     
    7978} htc_operating_mode_t;
    8079
    81 /**
    82  * HTC data type indicator.
     80/** HTC data type indicator.
     81 *
    8382 */
    8483typedef enum {
     
    8988} htc_data_type_t;
    9089
    91 /**
    92  * HTC endpoint numbers.
    93  */
    94 typedef struct {
    95         int ctrl_endpoint;
     90/** HTC endpoint numbers.
     91 *
     92 */
     93typedef struct {
     94        int ctrl_endpoint;
    9695        int wmi_endpoint;
    9796        int beacon_endpoint;
     
    105104} htc_pipes_t;
    106105
    107 /**
    108  * HTC device data.
    109  */
    110 typedef struct {
    111         /** WMI message sequence number */
    112         uint16_t sequence_number;
    113    
     106/** HTC device data.
     107 *
     108 */
     109typedef struct {
     110        /** WMI message sequence number */
     111        uint16_t sequence_number;
     112       
    114113        /** HTC endpoints numbers */
    115114        htc_pipes_t endpoints;
     
    128127} htc_device_t;
    129128
    130 /**
    131  * HTC frame header structure.
    132  */
    133 typedef struct {
    134         uint8_t   endpoint_id;
    135         uint8_t   flags;
    136         uint16_t  payload_length;       /**< Big Endian value! */
    137         uint8_t   control_bytes[4];
     129/** HTC frame header structure.
     130 *
     131 */
     132typedef struct {
     133        uint8_t endpoint_id;
     134        uint8_t flags;
     135        uint16_t payload_length;   /**< Big Endian value! */
     136        uint8_t control_bytes[4];
    138137} __attribute__((packed)) htc_frame_header_t;
    139138
    140 /**
    141  * HTC management TX frame header structure.
     139/** HTC management TX frame header structure.
     140 *
    142141 */
    143142typedef struct {
     
    152151} __attribute__((packed)) htc_tx_management_header_t;
    153152
    154 /**
    155  * HTC data TX frame header structure.
     153/** HTC data TX frame header structure.
     154 *
    156155 */
    157156typedef struct {
     
    160159        uint8_t vif_idx;
    161160        uint8_t tidno;
    162         uint32_t flags;                 /**< Big Endian value! */
     161        uint32_t flags;    /**< Big Endian value! */
    163162        uint8_t key_type;
    164163        uint8_t keyix;
     
    167166} __attribute__((packed)) htc_tx_data_header_t;
    168167
    169 /**
    170  * HTC ready message structure.
    171  */
    172 typedef struct {
    173         uint16_t message_id;            /**< Big Endian value! */
    174         uint16_t credits;               /**< Big Endian value! */
    175         uint16_t credit_size;           /**< Big Endian value! */
    176        
    177         uint8_t max_endpoints;
     168/** HTC ready message structure.
     169 *
     170 */
     171typedef struct {
     172        uint16_t message_id;   /**< Big Endian value! */
     173        uint16_t credits;      /**< Big Endian value! */
     174        uint16_t credit_size;  /**< Big Endian value! */
     175       
     176        uint8_t max_endpoints;
    178177        uint8_t pad;
    179178} __attribute__((packed)) htc_ready_msg_t;
    180179
    181 /**
    182  * HTC service message structure.
    183  */
    184 typedef struct {
    185         uint16_t message_id;            /**< Big Endian value! */
    186         uint16_t service_id;            /**< Big Endian value! */
    187         uint16_t connection_flags;      /**< Big Endian value! */
     180/** HTC service message structure.
     181 *
     182 */
     183typedef struct {
     184        uint16_t message_id;        /**< Big Endian value! */
     185        uint16_t service_id;        /**< Big Endian value! */
     186        uint16_t connection_flags;  /**< Big Endian value! */
    188187       
    189188        uint8_t download_pipe_id;
     
    193192} __attribute__((packed)) htc_service_msg_t;
    194193
    195 /**
    196  * HTC service response message structure.
    197  */
    198 typedef struct {
    199         uint16_t message_id;            /**< Big Endian value! */
    200         uint16_t service_id;            /**< Big Endian value! */
    201         uint8_t status;
    202         uint8_t endpoint_id;
    203         uint16_t max_message_length;    /**< Big Endian value! */
    204         uint8_t service_meta_length;
    205         uint8_t pad;
     194/** HTC service response message structure.
     195 *
     196 */
     197typedef struct {
     198        uint16_t message_id;          /**< Big Endian value! */
     199        uint16_t service_id;          /**< Big Endian value! */
     200        uint8_t status;
     201        uint8_t endpoint_id;
     202        uint16_t max_message_length;  /**< Big Endian value! */
     203        uint8_t service_meta_length;
     204        uint8_t pad;
    206205} __attribute__((packed)) htc_service_resp_msg_t;
    207206
    208 /**
    209  * HTC credits config message structure.
    210  */
    211 typedef struct {
    212         uint16_t message_id;            /**< Big Endian value! */
    213         uint8_t pipe_id;
    214         uint8_t credits;
     207/** HTC credits config message structure.
     208 *
     209 */
     210typedef struct {
     211        uint16_t message_id;  /**< Big Endian value! */
     212        uint8_t pipe_id;
     213        uint8_t credits;
    215214} __attribute__((packed)) htc_config_msg_t;
    216215
    217 /**
    218  * HTC new virtual interface message.
    219  */
    220 typedef struct {
    221         uint8_t index;
     216/** HTC new virtual interface message.
     217 *
     218 */
     219typedef struct {
     220        uint8_t index;
    222221        uint8_t op_mode;
    223222        uint8_t addr[ETH_ADDR];
    224223        uint8_t ath_cap;
    225         uint16_t rts_thres;             /**< Big Endian value! */
     224        uint16_t rts_thres;      /**< Big Endian value! */
    226225        uint8_t pad;
    227226} __attribute__((packed)) htc_vif_msg_t;
    228227
    229 /**
    230  * HTC new station message.
     228/** HTC new station message.
     229 *
    231230 */
    232231typedef struct {
    233232        uint8_t addr[ETH_ADDR];
    234233        uint8_t bssid[ETH_ADDR];
    235         uint8_t sta_index;
     234        uint8_t sta_index;
    236235        uint8_t vif_index;
    237236        uint8_t is_vif_sta;
    238                
    239         uint16_t flags;         /**< Big Endian value! */
    240         uint16_t ht_cap;        /**< Big Endian value! */
    241         uint16_t max_ampdu;     /**< Big Endian value! */
     237       
     238        uint16_t flags;      /**< Big Endian value! */
     239        uint16_t ht_cap;     /**< Big Endian value! */
     240        uint16_t max_ampdu;  /**< Big Endian value! */
    242241       
    243242        uint8_t pad;
    244243} __attribute__((packed)) htc_sta_msg_t;
    245244
    246 /**
    247  * HTC message to inform target about available capabilities.
    248  */
    249 typedef struct {
    250         uint32_t ampdu_limit;   /**< Big Endian value! */
     245/** HTC message to inform target about available capabilities.
     246 *
     247 */
     248typedef struct {
     249        uint32_t ampdu_limit;     /**< Big Endian value! */
    251250        uint8_t ampdu_subframes;
    252251        uint8_t enable_coex;
     
    258257        uint8_t sta_index;
    259258        uint8_t is_new;
    260         uint32_t cap_flags;     /**< Big Endian value! */
     259        uint32_t cap_flags;  /**< Big Endian value! */
    261260        uint8_t legacy_rates_count;
    262261        uint8_t legacy_rates[HTC_RATES_MAX_LENGTH];
     
    264263} htc_rate_msg_t;
    265264
    266 /**
    267  * HTC RX status structure used in incoming HTC data messages.
    268  */
    269 typedef struct {
    270         uint64_t timestamp;     /**< Big Endian value! */
    271         uint16_t data_length;   /**< Big Endian value! */
     265/** HTC RX status structure used in incoming HTC data messages.
     266 *
     267 */
     268typedef struct {
     269        uint64_t timestamp;    /**< Big Endian value! */
     270        uint16_t data_length;  /**< Big Endian value! */
    272271        uint8_t status;
    273272        uint8_t phy_err;
     
    284283        uint8_t flags;
    285284        uint8_t dummy;
    286         uint32_t evm0;          /**< Big Endian value! */
    287         uint32_t evm1;          /**< Big Endian value! */
    288         uint32_t evm2;          /**< Big Endian value! */
     285        uint32_t evm0;         /**< Big Endian value! */
     286        uint32_t evm1;         /**< Big Endian value! */
     287        uint32_t evm2;         /**< Big Endian value! */
    289288} htc_rx_status_t;
    290289
    291 /**
    292  * HTC setup complete message structure
    293  */
    294 typedef struct {
    295         uint16_t message_id;            /**< Big Endian value! */
     290/** HTC setup complete message structure
     291 *
     292 */
     293typedef struct {
     294        uint16_t message_id;  /**< Big Endian value! */
    296295} __attribute__((packed)) htc_setup_complete_msg_t;
    297296
    298 extern int htc_device_init(ath_t *ath_device, ieee80211_dev_t *ieee80211_dev,
    299         htc_device_t *htc_device);
    300 extern int htc_init(htc_device_t *htc_device);
    301 extern int htc_init_new_vif(htc_device_t *htc_device);
    302 extern int htc_read_control_message(htc_device_t *htc_device, void *buffer,
    303         size_t buffer_size, size_t *transferred_size);
    304 extern int htc_read_data_message(htc_device_t *htc_device, void *buffer,
    305         size_t buffer_size, size_t *transferred_size);
    306 extern int htc_send_control_message(htc_device_t *htc_device, void *buffer,
    307         size_t buffer_size, uint8_t endpoint_id);
    308 extern int htc_send_data_message(htc_device_t *htc_device, void *buffer,
    309         size_t buffer_size, uint8_t endpoint_id);
    310 
    311 #endif  /* ATHEROS_HTC_H */
    312 
     297extern int htc_device_init(ath_t *, ieee80211_dev_t *, htc_device_t *);
     298extern int htc_init(htc_device_t *);
     299extern int htc_init_new_vif(htc_device_t *);
     300extern int htc_read_control_message(htc_device_t *, void *, size_t, size_t *);
     301extern int htc_read_data_message(htc_device_t *, void *, size_t, size_t *);
     302extern int htc_send_control_message(htc_device_t *, void *, size_t, uint8_t);
     303extern int htc_send_data_message(htc_device_t *, void *, size_t, uint8_t);
     304
     305#endif  /* ATHEROS_HTC_H */
  • uspace/drv/nic/ar9271/hw.c

    r09044cb r8a64320e  
    3838#include <nic.h>
    3939#include <ieee80211.h>
    40 
    4140#include "hw.h"
    4241#include "wmi.h"
    4342
    44 /**
    45  * Try to wait for register value repeatedly until timeout is reached.
    46  *
     43/** Try to wait for register value repeatedly until timeout is reached.
     44 *
    4745 * @param ar9271 Device structure.
    4846 * @param offset Registry offset (address) to be read.
    49  * @param mask Mask to apply on read result.
    50  * @param value Required value we are waiting for.
    51  *
    52  * @return EOK if succeed, ETIMEOUT on timeout, negative error code otherwise.
     47 * @param mask   Mask to apply on read result.
     48 * @param value  Required value we are waiting for.
     49 *
     50 * @return EOK if succeed, ETIMEOUT on timeout,
     51 *         negative error code otherwise.
     52 *
    5353 */
    5454static int hw_read_wait(ar9271_t *ar9271, uint32_t offset, uint32_t mask,
    55         uint32_t value)
    56 {
    57         uint32_t result;
    58        
    59         for(size_t i = 0; i < HW_WAIT_LOOPS; i++) {
     55    uint32_t value)
     56{
     57        for (size_t i = 0; i < HW_WAIT_LOOPS; i++) {
    6058                udelay(HW_WAIT_TIME_US);
    61 
     59               
     60                uint32_t result;
    6261                wmi_reg_read(ar9271->htc_device, offset, &result);
    63                 if((result & mask) == value) {
     62                if ((result & mask) == value)
    6463                        return EOK;
    65                 }
    6664        }
    6765       
     
    7472                {
    7573                        .offset = AR9271_RTC_FORCE_WAKE,
    76                         .value = AR9271_RTC_FORCE_WAKE_ENABLE | 
    77                                 AR9271_RTC_FORCE_WAKE_ON_INT
     74                        .value = AR9271_RTC_FORCE_WAKE_ENABLE |
     75                            AR9271_RTC_FORCE_WAKE_ON_INT
    7876                },
    7977                {
     
    8886       
    8987        wmi_reg_buffer_write(ar9271->htc_device, buffer,
    90                 sizeof(buffer) / sizeof(wmi_reg_t));
     88            sizeof(buffer) / sizeof(wmi_reg_t));
    9189       
    9290        udelay(2);
     
    9593        wmi_reg_write(ar9271->htc_device, AR9271_RTC_RESET, 1);
    9694       
    97         int rc = hw_read_wait(ar9271, 
    98                 AR9271_RTC_STATUS,
    99                 AR9271_RTC_STATUS_MASK,
    100                 AR9271_RTC_STATUS_ON);
    101         if(rc != EOK) {
     95        int rc = hw_read_wait(ar9271,
     96            AR9271_RTC_STATUS,
     97            AR9271_RTC_STATUS_MASK,
     98            AR9271_RTC_STATUS_ON);
     99        if (rc != EOK) {
    102100                usb_log_error("Failed to wait for RTC wake up register.\n");
    103101                return rc;
     
    111109        uint32_t reset_value = AR9271_RTC_RC_MAC_WARM;
    112110       
    113         if(cold) {
     111        if (cold)
    114112                reset_value |= AR9271_RTC_RC_MAC_COLD;
    115         }
    116113       
    117114        wmi_reg_t buffer[] = {
    118115                {
    119116                        .offset = AR9271_RTC_FORCE_WAKE,
    120                         .value = AR9271_RTC_FORCE_WAKE_ENABLE | 
    121                                 AR9271_RTC_FORCE_WAKE_ON_INT
     117                        .value = AR9271_RTC_FORCE_WAKE_ENABLE |
     118                            AR9271_RTC_FORCE_WAKE_ON_INT
    122119                },
    123120                {
     
    131128        };
    132129       
    133         wmi_reg_buffer_write(ar9271->htc_device, buffer, 
    134                 sizeof(buffer) / sizeof(wmi_reg_t));
     130        wmi_reg_buffer_write(ar9271->htc_device, buffer,
     131            sizeof(buffer) / sizeof(wmi_reg_t));
    135132       
    136133        udelay(100);
     
    139136       
    140137        int rc = hw_read_wait(ar9271, AR9271_RTC_RC, AR9271_RTC_RC_MASK, 0);
    141         if(rc != EOK) {
     138        if (rc != EOK) {
    142139                usb_log_error("Failed to wait for RTC RC register.\n");
    143140                return rc;
     
    145142       
    146143        wmi_reg_write(ar9271->htc_device, AR9271_RC, 0);
    147         wmi_reg_clear_bit(ar9271->htc_device, AR9271_STATION_ID1, 
    148                 AR9271_STATION_ID1_POWER_SAVING);
     144        wmi_reg_clear_bit(ar9271->htc_device, AR9271_STATION_ID1,
     145            AR9271_STATION_ID1_POWER_SAVING);
    149146       
    150147        return EOK;
     
    156153        nic_address_t ar9271_address;
    157154       
    158         for(int i = 0; i < 3; i++) {
    159                 wmi_reg_read(ar9271->htc_device,
    160                         AR9271_EEPROM_MAC_ADDR_START + i*4,
    161                         &value);
     155        for (unsigned int i = 0; i < 3; i++) {
     156                wmi_reg_read(ar9271->htc_device,
     157                    AR9271_EEPROM_MAC_ADDR_START + i * 4, &value);
    162158               
    163159                uint16_t two_bytes = uint16_t_be2host(value);
     
    169165       
    170166        int rc = nic_report_address(nic, &ar9271_address);
    171         if(rc != EOK) {
     167        if (rc != EOK) {
    172168                usb_log_error("Failed to report NIC HW address.\n");
    173169                return rc;
     
    179175static int hw_gpio_set_output(ar9271_t *ar9271, uint32_t gpio, uint32_t type)
    180176{
    181         uint32_t address, gpio_shift, temp;
    182        
    183         if(gpio > 11) {
     177        uint32_t address;
     178       
     179        if (gpio > 11)
    184180                address = AR9271_GPIO_OUT_MUX3;
    185         } else if(gpio > 5) {
     181        else if (gpio > 5)
    186182                address = AR9271_GPIO_OUT_MUX2;
    187         } else {
     183        else
    188184                address = AR9271_GPIO_OUT_MUX1;
    189         }
    190        
    191         gpio_shift = (gpio % 6) * 5;
    192        
     185       
     186        uint32_t gpio_shift = (gpio % 6) * 5;
     187       
     188        uint32_t temp;
    193189        wmi_reg_read(ar9271->htc_device, address, &temp);
    194 
    195         temp = ((temp & 0x1F0) << 1) | (temp & ~0x1F0);
     190       
     191        temp = ((temp & 0x1f0) << 1) | (temp & ~0x1f0);
    196192        temp &= ~(0x1f << gpio_shift);
    197193        temp |= (type << gpio_shift);
    198 
     194       
    199195        wmi_reg_write(ar9271->htc_device, address, temp);
    200196       
     
    202198       
    203199        wmi_reg_set_clear_bit(ar9271->htc_device, AR9271_GPIO_OE_OUT,
    204                 AR9271_GPIO_OE_OUT_ALWAYS << gpio_shift,
    205                 AR9271_GPIO_OE_OUT_ALWAYS << gpio_shift);
     200            AR9271_GPIO_OE_OUT_ALWAYS << gpio_shift,
     201            AR9271_GPIO_OE_OUT_ALWAYS << gpio_shift);
    206202       
    207203        return EOK;
     
    211207{
    212208        wmi_reg_set_clear_bit(ar9271->htc_device, AR9271_GPIO_IN_OUT,
    213                 (~value & 1) << gpio, 1 << gpio);
    214         return EOK;
    215 }
    216 
    217 /**
    218  * Hardware init procedure of AR9271 device.
    219  *
     209            (~value & 1) << gpio, 1 << gpio);
     210        return EOK;
     211}
     212
     213/**Hardware init procedure of AR9271 device.
     214 *
    220215 * @param ar9271 Device structure.
    221  * 
     216 *
    222217 * @return EOK if succeed, negative error code otherwise.
     218 *
    223219 */
    224220static int hw_init_proc(ar9271_t *ar9271)
    225221{
    226222        int rc = hw_reset_power_on(ar9271);
    227         if(rc != EOK) {
     223        if (rc != EOK) {
    228224                usb_log_error("Failed to HW reset power on.\n");
    229225                return rc;
     
    231227       
    232228        rc = hw_set_reset(ar9271, false);
    233         if(rc != EOK) {
     229        if (rc != EOK) {
    234230                usb_log_error("Failed to HW warm reset.\n");
    235231                return rc;
     
    237233       
    238234        rc = hw_addr_init(ar9271);
    239         if(rc != EOK) {
     235        if (rc != EOK) {
    240236                usb_log_error("Failed to init HW addr.\n");
    241237                return rc;
     
    247243static int hw_init_led(ar9271_t *ar9271)
    248244{
    249         int rc = hw_gpio_set_output(ar9271, AR9271_LED_PIN, 
    250                 AR9271_GPIO_OUT_MUX_AS_OUT);
    251         if(rc != EOK) {
     245        int rc = hw_gpio_set_output(ar9271, AR9271_LED_PIN,
     246            AR9271_GPIO_OUT_MUX_AS_OUT);
     247        if (rc != EOK) {
    252248                usb_log_error("Failed to set led GPIO to output.\n");
    253249                return rc;
     
    255251       
    256252        rc = hw_gpio_set_value(ar9271, AR9271_LED_PIN, 0);
    257         if(rc != EOK) {
     253        if (rc != EOK) {
    258254                usb_log_error("Failed to init bring up GPIO led.\n");
    259255                return rc;
     
    271267}
    272268
    273 static int hw_set_operating_mode(ar9271_t *ar9271, 
    274         ieee80211_operating_mode_t op_mode)
     269static int hw_set_operating_mode(ar9271_t *ar9271,
     270    ieee80211_operating_mode_t op_mode)
    275271{
    276272        uint32_t set_bit = 0x10000000;
    277273       
    278274        switch(op_mode) {
    279                 case IEEE80211_OPMODE_ADHOC:
    280                         set_bit |= AR9271_OPMODE_ADHOC_MASK;
    281                         wmi_reg_set_bit(ar9271->htc_device, AR9271_CONFIG,
    282                                 AR9271_CONFIG_ADHOC);
    283                         break;
    284                 case IEEE80211_OPMODE_MESH:
    285                 case IEEE80211_OPMODE_AP:
    286                         set_bit |= AR9271_OPMODE_STATION_AP_MASK;
    287                 case IEEE80211_OPMODE_STATION:
    288                         wmi_reg_clear_bit(ar9271->htc_device, AR9271_CONFIG,
    289                                 AR9271_CONFIG_ADHOC);
     275        case IEEE80211_OPMODE_ADHOC:
     276                set_bit |= AR9271_OPMODE_ADHOC_MASK;
     277                wmi_reg_set_bit(ar9271->htc_device, AR9271_CONFIG,
     278                    AR9271_CONFIG_ADHOC);
     279                break;
     280        case IEEE80211_OPMODE_MESH:
     281        case IEEE80211_OPMODE_AP:
     282                set_bit |= AR9271_OPMODE_STATION_AP_MASK;
     283        case IEEE80211_OPMODE_STATION:
     284                wmi_reg_clear_bit(ar9271->htc_device, AR9271_CONFIG,
     285                    AR9271_CONFIG_ADHOC);
    290286        }
    291287       
    292288        wmi_reg_set_clear_bit(ar9271->htc_device, AR9271_STATION_ID1,
    293                 set_bit,
    294                 AR9271_OPMODE_STATION_AP_MASK | AR9271_OPMODE_ADHOC_MASK);
     289            set_bit, AR9271_OPMODE_STATION_AP_MASK | AR9271_OPMODE_ADHOC_MASK);
    295290       
    296291        ieee80211_report_current_op_mode(ar9271->ieee80211_dev, op_mode);
     
    302297{
    303298        int rc = hw_set_operating_mode(ar9271, IEEE80211_OPMODE_STATION);
    304         if(rc != EOK) {
     299        if (rc != EOK) {
    305300                usb_log_error("Failed to set opmode to station.\n");
    306301                return rc;
     
    314309        uint32_t value;
    315310        wmi_reg_read(ar9271->htc_device, AR9271_PHY_CAL, &value);
    316         value &= 0xFFFFFE00;
    317         value |= (((uint32_t) AR9271_CALIB_NOMINAL_VALUE_2GHZ << 1) & 0x1FF);
     311       
     312        value &= 0xfffffe00;
     313        value |= (((uint32_t) AR9271_CALIB_NOMINAL_VALUE_2GHZ << 1) & 0x1ff);
     314       
    318315        wmi_reg_write(ar9271->htc_device, AR9271_PHY_CAL, value);
    319316       
    320         wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL, 
    321                 AR9271_AGC_CONTROL_NF_CALIB_EN);
    322        
    323         wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL, 
    324                 AR9271_AGC_CONTROL_NF_NOT_UPDATE);
    325        
    326         wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL, 
    327                 AR9271_AGC_CONTROL_NF_CALIB);
    328        
    329         int rc = hw_read_wait(ar9271, AR9271_AGC_CONTROL, 
    330                 AR9271_AGC_CONTROL_NF_CALIB, 0);
    331         if(rc != EOK) {
     317        wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
     318            AR9271_AGC_CONTROL_NF_CALIB_EN);
     319       
     320        wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
     321            AR9271_AGC_CONTROL_NF_NOT_UPDATE);
     322       
     323        wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
     324            AR9271_AGC_CONTROL_NF_CALIB);
     325       
     326        int rc = hw_read_wait(ar9271, AR9271_AGC_CONTROL,
     327            AR9271_AGC_CONTROL_NF_CALIB, 0);
     328        if (rc != EOK) {
    332329                usb_log_error("Failed to wait for NF calibration.\n");
    333330                return rc;
    334331        }
    335332       
    336         wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL, 
    337                 AR9271_AGC_CONTROL_NF_CALIB_EN);
    338        
    339         wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL, 
    340                 AR9271_AGC_CONTROL_NF_NOT_UPDATE);
    341        
    342         wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL, 
    343                 AR9271_AGC_CONTROL_NF_CALIB);
     333        wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
     334            AR9271_AGC_CONTROL_NF_CALIB_EN);
     335       
     336        wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
     337            AR9271_AGC_CONTROL_NF_NOT_UPDATE);
     338       
     339        wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
     340            AR9271_AGC_CONTROL_NF_CALIB);
    344341       
    345342        return EOK;
     
    349346{
    350347        /* Not supported channel frequency. */
    351         if(freq < IEEE80211_FIRST_FREQ || freq > IEEE80211_MAX_FREQ) {
     348        if ((freq < IEEE80211_FIRST_FREQ) || (freq > IEEE80211_MAX_FREQ))
    352349                return EINVAL;
    353         }
    354350       
    355351        /* Not supported channel frequency. */
    356         if((freq - IEEE80211_FIRST_FREQ) % IEEE80211_CHANNEL_GAP != 0) {
     352        if ((freq - IEEE80211_FIRST_FREQ) % IEEE80211_CHANNEL_GAP != 0)
    357353                return EINVAL;
    358         }
    359354       
    360355        uint32_t tx_control;
    361356        wmi_reg_read(ar9271->htc_device, AR9271_PHY_CCK_TX_CTRL, &tx_control);
    362357        wmi_reg_write(ar9271->htc_device, AR9271_PHY_CCK_TX_CTRL,
    363                 tx_control & ~AR9271_PHY_CCK_TX_CTRL_JAPAN);
     358            tx_control & ~AR9271_PHY_CCK_TX_CTRL_JAPAN);
    364359       
    365360        /* Some magic here. */
    366361        uint32_t synth_ctl;
    367362        wmi_reg_read(ar9271->htc_device, AR9271_PHY_SYNTH_CONTROL, &synth_ctl);
    368         synth_ctl &= 0xC0000000;
     363        synth_ctl &= 0xc0000000;
    369364        uint32_t channel_select = (freq * 0x10000) / 15;
    370365        synth_ctl = synth_ctl | (1 << 29) | (1 << 28) | channel_select;
     
    382377       
    383378        int rc = hw_read_wait(ar9271, AR9271_PHY_RFBUS_GRANT, 0x1, 0x1);
    384         if(rc != EOK) {
     379        if (rc != EOK) {
    385380                usb_log_error("Failed to kill RF bus.\n");
    386381                return rc;
     
    388383       
    389384        rc = hw_set_freq(ar9271, freq);
    390         if(rc != EOK) {
     385        if (rc != EOK) {
    391386                usb_log_error("Failed to HW set frequency.\n");
    392387                return rc;
     
    394389       
    395390        rc = hw_activate_phy(ar9271);
    396         if(rc != EOK) {
     391        if (rc != EOK) {
    397392                usb_log_error("Failed to activate physical layer.\n");
    398393                return rc;
     
    403398       
    404399        rc = hw_noise_floor_calibration(ar9271);
    405         if(rc != EOK) {
     400        if (rc != EOK) {
    406401                usb_log_error("Failed to do NF calibration.\n");
    407402                return rc;
     
    413408int hw_set_rx_filter(ar9271_t *ar9271, bool assoc)
    414409{
    415         uint32_t filter_bits;
    416        
    417410        uint32_t additional_bits = 0;
    418411       
    419         if(assoc) {
     412        if (assoc)
    420413                additional_bits |= AR9271_RX_FILTER_MYBEACON;
    421         } else {
     414        else
    422415                additional_bits |= AR9271_RX_FILTER_BEACON;
    423         }
    424        
    425         filter_bits = AR9271_RX_FILTER_UNI | AR9271_RX_FILTER_MULTI |
    426                 AR9271_RX_FILTER_BROAD | additional_bits;
     416       
     417        uint32_t filter_bits = AR9271_RX_FILTER_UNI |
     418            AR9271_RX_FILTER_MULTI | AR9271_RX_FILTER_BROAD |
     419            additional_bits;
    427420       
    428421        wmi_reg_write(ar9271->htc_device, AR9271_RX_FILTER, filter_bits);
     
    441434        uint16_t *last_2bytes = (uint16_t *) &bssid.address[4];
    442435       
    443         wmi_reg_write(ar9271->htc_device, AR9271_BSSID0, 
    444                 uint32_t_le2host(*first_4bytes));
    445        
    446         wmi_reg_write(ar9271->htc_device, AR9271_BSSID1, 
    447                 uint16_t_le2host(*last_2bytes) |
    448                 ((ieee80211_get_aid(ieee80211_dev) & 0x3FFF) << 16));
     436        wmi_reg_write(ar9271->htc_device, AR9271_BSSID0,
     437            uint32_t_le2host(*first_4bytes));
     438       
     439        wmi_reg_write(ar9271->htc_device, AR9271_BSSID1,
     440            uint16_t_le2host(*last_2bytes) |
     441            ((ieee80211_get_aid(ieee80211_dev) & 0x3fff) << 16));
    449442       
    450443        return EOK;
     
    453446int hw_rx_init(ar9271_t *ar9271)
    454447{
    455         wmi_reg_write(ar9271->htc_device, AR9271_COMMAND, 
    456                 AR9271_COMMAND_RX_ENABLE);
     448        wmi_reg_write(ar9271->htc_device, AR9271_COMMAND,
     449            AR9271_COMMAND_RX_ENABLE);
    457450       
    458451        int rc = hw_set_rx_filter(ar9271, false);
    459         if(rc != EOK) {
     452        if (rc != EOK) {
    460453                usb_log_error("Failed to set RX filtering.\n");
    461454                return rc;
     
    473466static int hw_init_pll(ar9271_t *ar9271)
    474467{
    475         uint32_t pll;
    476        
    477468        /* Some magic here (set for 2GHz channels). But VERY important :-) */
    478         pll = (0x5 << 10) | 0x2C;
     469        uint32_t pll = (0x5 << 10) | 0x2c;
    479470       
    480471        wmi_reg_write(ar9271->htc_device, AR9271_RTC_PLL_CONTROL, pll);
    481472       
    482473        wmi_reg_write(ar9271->htc_device, AR9271_RTC_SLEEP_CLOCK,
    483                 AR9271_RTC_SLEEP_CLOCK_FORCE_DERIVED);
     474            AR9271_RTC_SLEEP_CLOCK_FORCE_DERIVED);
    484475        wmi_reg_set_bit(ar9271->htc_device, AR9271_RTC_FORCE_WAKE,
    485                 AR9271_RTC_FORCE_WAKE_ENABLE);
     476            AR9271_RTC_FORCE_WAKE_ENABLE);
    486477       
    487478        return EOK;
     
    490481static void hw_set_init_values(ar9271_t *ar9271)
    491482{
    492         uint32_t reg_offset, reg_value;
    493        
    494         int size = ARRAY_SIZE(ar9271_2g_mode_array);
    495        
    496         for(int i = 0; i < size; i++) {
     483        uint32_t reg_offset;
     484        uint32_t reg_value;
     485       
     486        size_t size = ARRAY_SIZE(ar9271_2g_mode_array);
     487       
     488        for (size_t i = 0; i < size; i++) {
    497489                reg_offset = ar9271_2g_mode_array[i][0];
    498490                reg_value = ar9271_2g_mode_array[i][1];
     
    502494        size = ARRAY_SIZE(ar9271_2g_tx_array);
    503495       
    504         for(int i = 0; i < size; i++) {
     496        for (size_t i = 0; i < size; i++) {
    505497                reg_offset = ar9271_2g_tx_array[i][0];
    506498                reg_value = ar9271_2g_tx_array[i][1];
     
    510502        size = ARRAY_SIZE(ar9271_init_array);
    511503       
    512         for(int i = 0; i < size; i++) {
     504        for (size_t i = 0; i < size; i++) {
    513505                reg_offset = ar9271_init_array[i][0];
    514506                reg_value = ar9271_init_array[i][1];
     
    520512{
    521513        wmi_reg_set_bit(ar9271->htc_device, AR9271_CARRIER_LEAK_CONTROL,
    522                 AR9271_CARRIER_LEAK_CALIB);
     514            AR9271_CARRIER_LEAK_CALIB);
    523515        wmi_reg_clear_bit(ar9271->htc_device, AR9271_ADC_CONTROL,
    524                 AR9271_ADC_CONTROL_OFF_PWDADC);
     516            AR9271_ADC_CONTROL_OFF_PWDADC);
    525517        wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
    526                 AR9271_AGC_CONTROL_TX_CALIB);
     518            AR9271_AGC_CONTROL_TX_CALIB);
    527519        wmi_reg_set_bit(ar9271->htc_device, AR9271_PHY_TPCRG1,
    528                 AR9271_PHY_TPCRG1_PD_CALIB);
     520            AR9271_PHY_TPCRG1_PD_CALIB);
    529521        wmi_reg_set_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
    530                 AR9271_AGC_CONTROL_CALIB);
    531        
    532         int rc = hw_read_wait(ar9271, AR9271_AGC_CONTROL, 
    533                 AR9271_AGC_CONTROL_CALIB, 0);
    534         if(rc != EOK) {
     522            AR9271_AGC_CONTROL_CALIB);
     523       
     524        int rc = hw_read_wait(ar9271, AR9271_AGC_CONTROL,
     525            AR9271_AGC_CONTROL_CALIB, 0);
     526        if (rc != EOK) {
    535527                usb_log_error("Failed to wait on calibrate completion.\n");
    536528                return rc;
     
    538530       
    539531        wmi_reg_set_bit(ar9271->htc_device, AR9271_ADC_CONTROL,
    540                 AR9271_ADC_CONTROL_OFF_PWDADC);
     532            AR9271_ADC_CONTROL_OFF_PWDADC);
    541533        wmi_reg_clear_bit(ar9271->htc_device, AR9271_CARRIER_LEAK_CONTROL,
    542                 AR9271_CARRIER_LEAK_CALIB);
     534            AR9271_CARRIER_LEAK_CALIB);
    543535        wmi_reg_clear_bit(ar9271->htc_device, AR9271_AGC_CONTROL,
    544                 AR9271_AGC_CONTROL_TX_CALIB);
    545        
    546         return EOK;
    547 }
    548 
    549 int hw_reset(ar9271_t *ar9271) 
     536            AR9271_AGC_CONTROL_TX_CALIB);
     537       
     538        return EOK;
     539}
     540
     541int hw_reset(ar9271_t *ar9271)
    550542{
    551543        /* Set physical layer as deactivated. */
     
    553545       
    554546        if(ar9271->starting_up) {
    555                 wmi_reg_write(ar9271->htc_device, 
    556                         AR9271_RESET_POWER_DOWN_CONTROL,
    557                         AR9271_RADIO_RF_RESET);
    558 
     547                wmi_reg_write(ar9271->htc_device,
     548                    AR9271_RESET_POWER_DOWN_CONTROL,
     549                    AR9271_RADIO_RF_RESET);
     550               
    559551                udelay(50);
    560552        }
     
    563555        uint32_t config_reg;
    564556        wmi_reg_read(ar9271->htc_device, AR9271_COMMAND, &config_reg);
    565         if(config_reg & AR9271_COMMAND_RX_ENABLE) {
     557        if (config_reg & AR9271_COMMAND_RX_ENABLE)
    566558                hw_set_reset(ar9271, true);
    567         }
    568559       
    569560        int rc = hw_init_pll(ar9271);
    570         if(rc != EOK) {
     561        if (rc != EOK) {
    571562                usb_log_error("Failed to init PLL.\n");
    572563                return rc;
     
    575566        udelay(500);
    576567       
    577         wmi_reg_write(ar9271->htc_device, AR9271_CLOCK_CONTROL, 
    578                 AR9271_MAX_CPU_CLOCK);
     568        wmi_reg_write(ar9271->htc_device, AR9271_CLOCK_CONTROL,
     569            AR9271_MAX_CPU_CLOCK);
    579570       
    580571        udelay(100);
    581572       
    582         if(ar9271->starting_up) {
    583                 wmi_reg_write(ar9271->htc_device, 
    584                         AR9271_RESET_POWER_DOWN_CONTROL,
    585                         AR9271_GATE_MAC_CONTROL);
    586 
     573        if (ar9271->starting_up) {
     574                wmi_reg_write(ar9271->htc_device,
     575                    AR9271_RESET_POWER_DOWN_CONTROL,
     576                    AR9271_GATE_MAC_CONTROL);
     577               
    587578                udelay(50);
    588579        }
     
    591582       
    592583        /* Set physical layer mode. */
    593         wmi_reg_write(ar9271->htc_device, AR9271_PHY_MODE, 
    594                 AR9271_PHY_MODE_DYNAMIC);
     584        wmi_reg_write(ar9271->htc_device, AR9271_PHY_MODE,
     585            AR9271_PHY_MODE_DYNAMIC);
    595586       
    596587        /* Reset device operating mode. */
    597588        rc = hw_reset_operating_mode(ar9271);
    598         if(rc != EOK) {
     589        if (rc != EOK) {
    599590                usb_log_error("Failed to reset operating mode.\n");
    600591                return rc;
     
    603594        /* Set initial channel frequency. */
    604595        rc = hw_set_freq(ar9271, IEEE80211_FIRST_FREQ);
    605         if(rc != EOK) {
     596        if (rc != EOK) {
    606597                usb_log_error("Failed to set channel.\n");
    607598                return rc;
     
    609600       
    610601        /* Initialize transmission queues. */
    611         for(int i = 0; i < AR9271_QUEUES_COUNT; i++) {
    612                 wmi_reg_write(ar9271->htc_device,
    613                         AR9271_QUEUE_BASE_MASK + (i << 2),
    614                         1 << i);
     602        for (unsigned int i = 0; i < AR9271_QUEUES_COUNT; i++) {
     603                wmi_reg_write(ar9271->htc_device,
     604                    AR9271_QUEUE_BASE_MASK + (i << 2), 1 << i);
    615605        }
    616606       
    617607        /* Activate physical layer. */
    618608        rc = hw_activate_phy(ar9271);
    619         if(rc != EOK) {
     609        if (rc != EOK) {
    620610                usb_log_error("Failed to activate physical layer.\n");
    621611                return rc;
     
    624614        /* Calibration. */
    625615        rc = hw_calibration(ar9271);
    626         if(rc != EOK) {
     616        if (rc != EOK) {
    627617                usb_log_error("Failed to calibrate device.\n");
    628618                return rc;
     
    630620       
    631621        rc = hw_noise_floor_calibration(ar9271);
    632         if(rc != EOK) {
     622        if (rc != EOK) {
    633623                usb_log_error("Failed to calibrate noise floor.\n");
    634624                return rc;
     
    641631}
    642632
    643 /**
    644  * Initialize hardware of AR9271 device.
    645  *
     633/** Initialize hardware of AR9271 device.
     634 *
    646635 * @param ar9271 Device structure.
    647  * 
     636 *
    648637 * @return EOK if succeed, negative error code otherwise.
    649638 */
     
    651640{
    652641        int rc = hw_init_proc(ar9271);
    653         if(rc != EOK) {
     642        if (rc != EOK) {
    654643                usb_log_error("Failed to HW reset device.\n");
    655644                return rc;
     
    657646       
    658647        rc = hw_init_led(ar9271);
    659         if(rc != EOK) {
     648        if (rc != EOK) {
    660649                usb_log_error("Failed to HW init led.\n");
    661650                return rc;
  • uspace/drv/nic/ar9271/hw.h

    r09044cb r8a64320e  
    3434
    3535#ifndef ATHEROS_HW_H
    36 #define ATHEROS_HW_H
     36#define ATHEROS_HW_H
    3737
    3838#include "ar9271.h"
    3939
    40 #define HW_WAIT_LOOPS 1000
    41 #define HW_WAIT_TIME_US 10
     40#define HW_WAIT_LOOPS    1000
     41#define HW_WAIT_TIME_US  10
    4242
    43 extern int hw_init(ar9271_t *ar9271);
    44 extern int hw_freq_switch(ar9271_t *, uint16_t freq);
    45 extern int hw_rx_init(ar9271_t *ar9271);
    46 extern int hw_reset(ar9271_t *ar9271);
    47 extern int hw_set_bssid(ar9271_t *ar9271);
    48 extern int hw_set_rx_filter(ar9271_t *ar9271, bool assoc);
     43extern int hw_init(ar9271_t *);
     44extern int hw_freq_switch(ar9271_t *, uint16_t);
     45extern int hw_rx_init(ar9271_t *);
     46extern int hw_reset(ar9271_t *);
     47extern int hw_set_bssid(ar9271_t *);
     48extern int hw_set_rx_filter(ar9271_t *, bool);
    4949
    50 #endif  /* ATHEROS_HW_H */
     50#endif  /* ATHEROS_HW_H */
  • 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}
  • uspace/drv/nic/ar9271/wmi.h

    r09044cb r8a64320e  
    3535
    3636#ifndef ATHEROS_WMI_H
    37 #define ATHEROS_WMI_H
     37#define ATHEROS_WMI_H
    3838
    3939#include "htc.h"
    4040
    4141/* Macros for creating service identificators. */
    42 #define WMI_SERVICE_GROUP 1
    43 #define CREATE_SERVICE_ID(group, i) (int) (((int) group << 8) | (int) (i))
     42#define WMI_SERVICE_GROUP  1
    4443
    45 #define WMI_MGMT_CMD_MASK 0x1000
     44#define CREATE_SERVICE_ID(group, i) \
     45        (unsigned int) (((unsigned int) group << 8) | (unsigned int) (i))
    4646
    47 /**
    48  * WMI header structure.
     47#define WMI_MGMT_CMD_MASK  0x1000
     48
     49/** WMI header structure.
     50 *
    4951 */
    5052typedef struct {
    51     uint16_t command_id;                /**< Big Endian value! */
    52     uint16_t sequence_number;           /**< Big Endian value! */
     53        uint16_t command_id;       /**< Big Endian value! */
     54        uint16_t sequence_number;  /**< Big Endian value! */
    5355} __attribute__((packed)) wmi_command_header_t;
    5456
    55 /**
    56  * WMI services IDs
     57/** WMI services IDs
     58 *
    5759 */
    5860typedef enum {
     
    6870} wmi_services_t;
    6971
    70 /**
    71  * List of WMI commands
     72/** List of WMI commands
     73 *
    7274 */
    7375typedef enum {
    7476        WMI_ECHO = 0x0001,
    7577        WMI_ACCESS_MEMORY,
    76 
     78       
    7779        /* Commands used for HOST -> DEVICE communication */
    7880        WMI_GET_FW_VERSION,
     
    107109} wmi_command_t;
    108110
    109 /**
    110  * Structure used when sending registry buffer
     111/**Structure used when sending registry buffer
     112 *
    111113 */
    112114typedef struct {
    113         uint32_t offset;                /**< Big Endian value! */
    114         uint32_t value;                 /**< Big Endian value! */
     115        uint32_t offset;  /**< Big Endian value! */
     116        uint32_t value;   /**< Big Endian value! */
    115117} wmi_reg_t;
    116118
    117 extern int wmi_reg_read(htc_device_t *htc_device, uint32_t reg_offset,
    118         uint32_t *res);
    119 extern int wmi_reg_write(htc_device_t *htc_device, uint32_t reg_offset,
    120         uint32_t val);
    121 extern int wmi_reg_set_clear_bit(htc_device_t *htc_device,
    122         uint32_t reg_offset, uint32_t set_bit, uint32_t clear_bit);
    123 extern int wmi_reg_set_bit(htc_device_t *htc_device, uint32_t reg_offset,
    124         uint32_t set_bit);
    125 extern int wmi_reg_clear_bit(htc_device_t *htc_device, uint32_t reg_offset,
    126         uint32_t clear_bit);
    127 extern int wmi_reg_buffer_write(htc_device_t *htc_device,
    128         wmi_reg_t *reg_buffer, size_t elements);
    129 extern int wmi_send_command(htc_device_t *htc_device,
    130         wmi_command_t command_id,
    131         uint8_t *command_buffer, uint32_t command_length,
    132         void *response_buffer);
     119extern int wmi_reg_read(htc_device_t *, uint32_t, uint32_t *);
     120extern int wmi_reg_write(htc_device_t *, uint32_t, uint32_t);
     121extern int wmi_reg_set_clear_bit(htc_device_t *, uint32_t, uint32_t, uint32_t);
     122extern int wmi_reg_set_bit(htc_device_t *, uint32_t, uint32_t);
     123extern int wmi_reg_clear_bit(htc_device_t *, uint32_t, uint32_t);
     124extern int wmi_reg_buffer_write(htc_device_t *, wmi_reg_t *, size_t);
     125extern int wmi_send_command(htc_device_t *, wmi_command_t, uint8_t *, uint32_t,
     126    void *);
    133127
    134 #endif  /* ATHEROS_WMI_H */
    135 
     128#endif  /* ATHEROS_WMI_H */
  • uspace/lib/c/include/ieee80211/ieee80211.h

    r09044cb r8a64320e  
    4444
    4545/** Max length of scan results array. */
    46 #define IEEE80211_MAX_RESULTS_LENGTH 32
     46#define IEEE80211_MAX_RESULTS_LENGTH  32
    4747
    4848/** Max SSID length including null character. */
    49 #define IEEE80211_MAX_SSID_LENGTH 35
     49#define IEEE80211_MAX_SSID_LENGTH  35
    5050
    5151/** WiFi security authentication method indicator. */
     
    9797/** @}
    9898 */
    99 
  • uspace/lib/crypto/aes.c

    r09044cb r8a64320e  
    2828
    2929/** @file aes.c
    30  * 
     30 *
    3131 * Implementation of AES-128 symmetric cipher cryptographic algorithm.
    32  * 
     32 *
    3333 * Based on FIPS 197.
    3434 */
     
    3737#include <errno.h>
    3838#include <mem.h>
    39 
    4039#include "crypto.h"
    4140
    4241/* Number of elements in rows/columns in AES arrays. */
    43 #define ELEMS 4
     42#define ELEMS  4
    4443
    4544/* Number of elements (words) in cipher. */
    46 #define CIPHER_ELEMS 4
     45#define CIPHER_ELEMS  4
    4746
    4847/* Length of AES block. */
    49 #define BLOCK_LEN 16
     48#define BLOCK_LEN  16
    5049
    5150/* Number of iterations in AES algorithm. */
    52 #define ROUNDS 10
    53 
    54 /*
    55  * Irreducible polynomial used in AES algorithm.
    56  *
     51#define ROUNDS  10
     52
     53/** Irreducible polynomial used in AES algorithm.
     54 *
    5755 * NOTE: x^8 + x^4 + x^3 + x + 1.
    58  */
    59 #define AES_IP 0x1B
    60 
    61 /* Precomputed values for AES sub_byte transformation. */
     56 *
     57 */
     58#define AES_IP  0x1b
     59
     60/** Precomputed values for AES sub_byte transformation. */
    6261static const uint8_t sbox[BLOCK_LEN][BLOCK_LEN] = {
    6362        {
    64                 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 
     63                0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5,
    6564                0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76
    6665        },
    6766        {
    68                 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 
     67                0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
    6968                0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0
    7069        },
     
    7473        },
    7574        {
    76                 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 
     75                0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a,
    7776                0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75
    7877        },
    7978        {
    80                 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 
     79                0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0,
    8180                0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84
    8281        },
    8382        {
    84                 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 
     83                0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b,
    8584                0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf
    8685        },
    8786        {
    88                 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 
     87                0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85,
    8988                0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8
    9089        },
    9190        {
    92                 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 
     91                0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5,
    9392                0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2
    9493        },
    9594        {
    96                 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 
     95                0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17,
    9796                0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73
    9897        },
    9998        {
    100                 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 
     99                0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88,
    101100                0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb
    102101        },
    103102        {
    104                 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 
     103                0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c,
    105104                0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79
    106105        },
    107106        {
    108                 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 
     107                0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9,
    109108                0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08
    110109        },
    111110        {
    112                 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 
     111                0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6,
    113112                0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a
    114113        },
    115114        {
    116                 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 
     115                0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e,
    117116                0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e
    118117        },
    119118        {
    120                 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 
     119                0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94,
    121120                0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf
    122121        },
    123122        {
    124                 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 
     123                0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68,
    125124                0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
    126125        }
    127126};
    128127
    129 /* Precomputed values for AES inv_sub_byte transformation. */
     128/** Precomputed values for AES inv_sub_byte transformation. */
    130129static uint8_t inv_sbox[BLOCK_LEN][BLOCK_LEN] = {
    131130        {
    132                 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 
     131                0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38,
    133132                0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb
    134133        },
    135134        {
    136                 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 
     135                0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87,
    137136                0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb
    138137        },
    139138        {
    140                 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 
     139                0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d,
    141140                0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e
    142141        },
    143142        {
    144                 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 
     143                0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2,
    145144                0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25
    146145        },
    147146        {
    148                 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 
     147                0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16,
    149148                0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92
    150149        },
    151150        {
    152                 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 
     151                0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda,
    153152                0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84
    154153        },
    155154        {
    156                 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 
     155                0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a,
    157156                0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06
    158157        },
    159158        {
    160                 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 
     159                0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02,
    161160                0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b
    162161        },
    163162        {
    164                 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 
     163                0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea,
    165164                0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73
    166165        },
    167166        {
    168                 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 
     167                0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85,
    169168                0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e
    170169        },
    171170        {
    172                 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 
     171                0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89,
    173172                0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b
    174173        },
    175174        {
    176                 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 
     175                0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20,
    177176                0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4
    178177        },
    179178        {
    180                 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 
     179                0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31,
    181180                0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f
    182181        },
    183182        {
    184                 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 
     183                0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d,
    185184                0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef
    186185        },
    187186        {
    188                 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 
     187                0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0,
    189188                0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61
    190189        },
    191190        {
    192                 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 
     191                0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26,
    193192                0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
    194193        }
    195194};
    196195
    197 /* Precomputed values of powers of 2 in GF(2^8) left shifted by 24b. */
     196/** Precomputed values of powers of 2 in GF(2^8) left shifted by 24b. */
    198197static const uint32_t r_con_array[] = {
    199         0x01000000, 0x02000000, 0x04000000, 0x08000000,
     198        0x01000000, 0x02000000, 0x04000000, 0x08000000,
    200199        0x10000000, 0x20000000, 0x40000000, 0x80000000,
    201         0x1B000000, 0x36000000
     200        0x1b000000, 0x36000000
    202201};
    203202
    204 /**
    205  * Perform substitution transformation on given byte.
    206  *
     203/** Perform substitution transformation on given byte.
     204 *
    207205 * @param byte Input byte.
    208  * @param inv Flag indicating whether to use inverse table.
    209  * 
     206 * @param inv  Flag indicating whether to use inverse table.
     207 *
    210208 * @return Substituted value.
     209 *
    211210 */
    212211static uint8_t sub_byte(uint8_t byte, bool inv)
     
    215214        uint8_t j = byte & 0xF;
    216215       
    217         if(!inv) {
     216        if (!inv)
    218217                return sbox[i][j];
    219         } else {
    220                 return inv_sbox[i][j];
    221         }
    222 }
    223 
    224 /**
    225  * Perform substitution transformation on state table.
    226  *
     218       
     219        return inv_sbox[i][j];
     220}
     221
     222/** Perform substitution transformation on state table.
     223 *
    227224 * @param state State table to be modified.
    228  * @param inv Flag indicating whether to use inverse table.
     225 * @param inv   Flag indicating whether to use inverse table.
     226 *
    229227 */
    230228static void sub_bytes(uint8_t state[ELEMS][ELEMS], bool inv)
     
    232230        uint8_t val;
    233231       
    234         for(size_t i = 0; i < ELEMS; i++) {
    235                 for(size_t j = 0; j < ELEMS; j++) {
     232        for (size_t i = 0; i < ELEMS; i++) {
     233                for (size_t j = 0; j < ELEMS; j++) {
    236234                        val = state[i][j];
    237235                        state[i][j] = sub_byte(val, inv);
     
    240238}
    241239
    242 /**
    243  * Perform shift rows transformation on state table.
    244  *
     240/** Perform shift rows transformation on state table.
     241 *
    245242 * @param state State table to be modified.
     243 *
    246244 */
    247245static void shift_rows(uint8_t state[ELEMS][ELEMS])
     
    249247        uint8_t temp[ELEMS];
    250248       
    251         for(size_t i = 1; i < ELEMS; i++) {
     249        for (size_t i = 1; i < ELEMS; i++) {
    252250                memcpy(temp, state[i], i);
    253251                memcpy(state[i], state[i] + i, ELEMS - i);
     
    256254}
    257255
    258 /**
    259  * Perform inverted shift rows transformation on state table.
    260  *
     256/** Perform inverted shift rows transformation on state table.
     257 *
    261258 * @param state State table to be modified.
     259 *
    262260 */
    263261static void inv_shift_rows(uint8_t state[ELEMS][ELEMS])
     
    265263        uint8_t temp[ELEMS];
    266264       
    267         for(size_t i = 1; i < ELEMS; i++) {
     265        for (size_t i = 1; i < ELEMS; i++) {
    268266                memcpy(temp, state[i], ELEMS - i);
    269267                memcpy(state[i], state[i] + ELEMS - i, i);
     
    272270}
    273271
    274 /**
    275  * Multiplication in GF(2^8).
    276  *
     272/** Multiplication in GF(2^8).
     273 *
    277274 * @param x First factor.
    278275 * @param y Second factor.
    279  * 
     276 *
    280277 * @return Multiplication of given factors in GF(2^8).
    281  */
    282 static uint8_t galois_mult(uint8_t x, uint8_t y) {
    283         uint8_t result = 0;
    284         uint8_t F_bitH;
    285        
    286         for(size_t i = 0; i < 8; i++) {
    287                 if (y & 1)
     278 *
     279 */
     280static uint8_t galois_mult(uint8_t x, uint8_t y)
     281{
     282        uint8_t result = 0;
     283        uint8_t f_bith;
     284       
     285        for (size_t i = 0; i < 8; i++) {
     286                if (y & 1)
    288287                        result ^= x;
    289                 F_bitH = (x & 0x80);
    290                 x <<= 1;
    291                 if (F_bitH)
    292                         x ^= AES_IP;
    293                 y >>= 1;
    294         }
    295        
    296         return result;
    297 }
    298 
    299 /**
    300  * Perform mix columns transformation on state table.
    301  *
     288               
     289                f_bith = (x & 0x80);
     290                x <<= 1;
     291               
     292                if (f_bith)
     293                        x ^= AES_IP;
     294               
     295                y >>= 1;
     296        }
     297       
     298        return result;
     299}
     300
     301/** Perform mix columns transformation on state table.
     302 *
    302303 * @param state State table to be modified.
     304 *
    303305 */
    304306static void mix_columns(uint8_t state[ELEMS][ELEMS])
     
    307309        memcpy(orig_state, state, BLOCK_LEN);
    308310       
    309         for(size_t j = 0; j < ELEMS; j++) {
    310                 state[0][j] =
    311                         galois_mult(0x2, orig_state[0][j]) ^
    312                         galois_mult(0x3, orig_state[1][j]) ^
    313                         orig_state[2][j] ^
    314                         orig_state[3][j];
    315                 state[1][j] =
    316                         orig_state[0][j] ^
    317                         galois_mult(0x2, orig_state[1][j]) ^
    318                         galois_mult(0x3, orig_state[2][j]) ^
    319                         orig_state[3][j];
    320                 state[2][j] =
    321                         orig_state[0][j] ^
    322                         orig_state[1][j] ^
    323                         galois_mult(0x2, orig_state[2][j]) ^
    324                         galois_mult(0x3, orig_state[3][j]);
    325                 state[3][j] =
    326                         galois_mult(0x3, orig_state[0][j]) ^
    327                         orig_state[1][j] ^
    328                         orig_state[2][j] ^
    329                         galois_mult(0x2, orig_state[3][j]);
    330         }
    331 }
    332 
    333 /**
    334  * Perform inverted mix columns transformation on state table.
    335  *
     311        for (size_t j = 0; j < ELEMS; j++) {
     312                state[0][j] =
     313                    galois_mult(0x2, orig_state[0][j]) ^
     314                    galois_mult(0x3, orig_state[1][j]) ^
     315                    orig_state[2][j] ^
     316                    orig_state[3][j];
     317                state[1][j] =
     318                    orig_state[0][j] ^
     319                    galois_mult(0x2, orig_state[1][j]) ^
     320                    galois_mult(0x3, orig_state[2][j]) ^
     321                    orig_state[3][j];
     322                state[2][j] =
     323                    orig_state[0][j] ^
     324                    orig_state[1][j] ^
     325                    galois_mult(0x2, orig_state[2][j]) ^
     326                    galois_mult(0x3, orig_state[3][j]);
     327                state[3][j] =
     328                    galois_mult(0x3, orig_state[0][j]) ^
     329                    orig_state[1][j] ^
     330                    orig_state[2][j] ^
     331                    galois_mult(0x2, orig_state[3][j]);
     332        }
     333}
     334
     335/** Perform inverted mix columns transformation on state table.
     336 *
    336337 * @param state State table to be modified.
     338 *
    337339 */
    338340static void inv_mix_columns(uint8_t state[ELEMS][ELEMS])
     
    341343        memcpy(orig_state, state, BLOCK_LEN);
    342344       
    343         for(size_t j = 0; j < ELEMS; j++) {
    344                 state[0][j] =
    345                         galois_mult(0x0E, orig_state[0][j]) ^
    346                         galois_mult(0x0B, orig_state[1][j]) ^
    347                         galois_mult(0x0D, orig_state[2][j]) ^
    348                         galois_mult(0x09, orig_state[3][j]);
    349                 state[1][j] =
    350                         galois_mult(0x09, orig_state[0][j]) ^
    351                         galois_mult(0x0E, orig_state[1][j]) ^
    352                         galois_mult(0x0B, orig_state[2][j]) ^
    353                         galois_mult(0x0D, orig_state[3][j]);
    354                 state[2][j] =
    355                         galois_mult(0x0D, orig_state[0][j]) ^
    356                         galois_mult(0x09, orig_state[1][j]) ^
    357                         galois_mult(0x0E, orig_state[2][j]) ^
    358                         galois_mult(0x0B, orig_state[3][j]);
    359                 state[3][j] =
    360                         galois_mult(0x0B, orig_state[0][j]) ^
    361                         galois_mult(0x0D, orig_state[1][j]) ^
    362                         galois_mult(0x09, orig_state[2][j]) ^
    363                         galois_mult(0x0E, orig_state[3][j]);
    364         }
    365 }
    366 
    367 /**
    368  * Perform round key transformation on state table.
    369  *
    370  * @param state State table to be modified.
     345        for (size_t j = 0; j < ELEMS; j++) {
     346                state[0][j] =
     347                    galois_mult(0x0e, orig_state[0][j]) ^
     348                    galois_mult(0x0b, orig_state[1][j]) ^
     349                    galois_mult(0x0d, orig_state[2][j]) ^
     350                    galois_mult(0x09, orig_state[3][j]);
     351                state[1][j] =
     352                    galois_mult(0x09, orig_state[0][j]) ^
     353                    galois_mult(0x0e, orig_state[1][j]) ^
     354                    galois_mult(0x0b, orig_state[2][j]) ^
     355                    galois_mult(0x0d, orig_state[3][j]);
     356                state[2][j] =
     357                    galois_mult(0x0d, orig_state[0][j]) ^
     358                    galois_mult(0x09, orig_state[1][j]) ^
     359                    galois_mult(0x0e, orig_state[2][j]) ^
     360                    galois_mult(0x0b, orig_state[3][j]);
     361                state[3][j] =
     362                    galois_mult(0x0b, orig_state[0][j]) ^
     363                    galois_mult(0x0d, orig_state[1][j]) ^
     364                    galois_mult(0x09, orig_state[2][j]) ^
     365                    galois_mult(0x0e, orig_state[3][j]);
     366        }
     367}
     368
     369/** Perform round key transformation on state table.
     370 *
     371 * @param state     State table to be modified.
    371372 * @param round_key Round key to be applied on state table.
     373 *
    372374 */
    373375static void add_round_key(uint8_t state[ELEMS][ELEMS], uint32_t *round_key)
     
    375377        uint8_t byte_round;
    376378        uint8_t shift;
    377         uint32_t mask = 0xFF;
    378        
    379         for(size_t j = 0; j < ELEMS; j++) {
    380                 for(size_t i = 0; i < ELEMS; i++) {
    381                         shift = 24 - 8*i;
     379        uint32_t mask = 0xff;
     380       
     381        for (size_t j = 0; j < ELEMS; j++) {
     382                for (size_t i = 0; i < ELEMS; i++) {
     383                        shift = 24 - 8 * i;
    382384                        byte_round = (round_key[j] & (mask << shift)) >> shift;
    383385                        state[i][j] = state[i][j] ^ byte_round;
     
    386388}
    387389
    388 /**
    389  * Perform substitution transformation on given word.
    390  *
     390/** Perform substitution transformation on given word.
     391 *
    391392 * @param byte Input word.
    392  * 
     393 *
    393394 * @return Substituted word.
     395 *
    394396 */
    395397static uint32_t sub_word(uint32_t word)
     
    398400        uint8_t *start = (uint8_t *) &temp;
    399401       
    400         for(size_t i = 0; i < 4; i++) {
    401                 *(start+i) = sub_byte(*(start+i), false);
    402         }
     402        for (size_t i = 0; i < 4; i++)
     403                *(start + i) = sub_byte(*(start + i), false);
    403404       
    404405        return temp;
    405406}
    406407
    407 /**
    408  * Perform left rotation by one byte on given word.
    409  *
     408/** Perform left rotation by one byte on given word.
     409 *
    410410 * @param byte Input word.
    411  * 
     411 *
    412412 * @return Rotated word.
     413 *
    413414 */
    414415static uint32_t rot_word(uint32_t word)
     
    417418}
    418419
    419 /**
    420  * Key expansion procedure for AES algorithm.
    421  *
    422  * @param key Input key.
     420/** Key expansion procedure for AES algorithm.
     421 *
     422 * @param key     Input key.
    423423 * @param key_exp Result key expansion.
     424 *
    424425 */
    425426static void key_expansion(uint8_t *key, uint32_t *key_exp)
     
    427428        uint32_t temp;
    428429       
    429         for(size_t i = 0; i < CIPHER_ELEMS; i++) {
    430                 key_exp[i] = 
    431                         (key[4*i] << 24) +
    432                         (key[4*i+1] << 16) +
    433                         (key[4*i+2] << 8) +
    434                         (key[4*i+3]);
    435         }
    436        
    437         for(size_t i = CIPHER_ELEMS; i < ELEMS * (ROUNDS + 1); i++) {
    438                 temp = key_exp[i-1];
    439                
    440                 if((i % CIPHER_ELEMS) == 0) {
    441                         temp = sub_word(rot_word(temp)) ^ 
    442                                 r_con_array[i/CIPHER_ELEMS - 1];
     430        for (size_t i = 0; i < CIPHER_ELEMS; i++) {
     431                key_exp[i] =
     432                    (key[4 * i] << 24) +
     433                    (key[4 * i + 1] << 16) +
     434                    (key[4 * i + 2] << 8) +
     435                    (key[4 * i + 3]);
     436        }
     437       
     438        for (size_t i = CIPHER_ELEMS; i < ELEMS * (ROUNDS + 1); i++) {
     439                temp = key_exp[i - 1];
     440               
     441                if ((i % CIPHER_ELEMS) == 0) {
     442                        temp = sub_word(rot_word(temp)) ^
     443                            r_con_array[i / CIPHER_ELEMS - 1];
    443444                }
    444445               
     
    447448}
    448449
    449 /**
    450  * AES-128 encryption algorithm.
    451  *
    452  * @param key Input key.
    453  * @param input Input data sequence to be encrypted.
     450/** AES-128 encryption algorithm.
     451 *
     452 * @param key    Input key.
     453 * @param input  Input data sequence to be encrypted.
    454454 * @param output Encrypted data sequence.
    455  *
    456  * @return EINVAL when input or key not specified, ENOMEM when pointer for
    457  * output is not allocated, otherwise EOK. 
     455 *
     456 * @return EINVAL when input or key not specified,
     457 *         ENOMEM when pointer for output is not allocated,
     458 *         otherwise EOK.
     459 *
    458460 */
    459461int aes_encrypt(uint8_t *key, uint8_t *input, uint8_t *output)
    460462{
    461         if(!key || !input)
     463        if ((!key) || (!input))
    462464                return EINVAL;
    463465       
    464         if(!output)
     466        if (!output)
    465467                return ENOMEM;
    466468       
    467469        /* Create key expansion. */
    468         uint32_t key_exp[ELEMS * (ROUNDS+1)];
     470        uint32_t key_exp[ELEMS * (ROUNDS + 1)];
    469471        key_expansion(key, key_exp);
    470472       
    471473        /* Copy input into state array. */
    472474        uint8_t state[ELEMS][ELEMS];
    473         for(size_t i = 0; i < ELEMS; i++) {
    474                 for(size_t j = 0; j < ELEMS; j++) {
    475                         state[i][j] = input[i + ELEMS*j];
    476                 }
     475        for (size_t i = 0; i < ELEMS; i++) {
     476                for (size_t j = 0; j < ELEMS; j++)
     477                        state[i][j] = input[i + ELEMS * j];
    477478        }
    478479       
     
    480481        add_round_key(state, key_exp);
    481482       
    482         for(size_t k = 1; k <= ROUNDS; k++) {
     483        for (size_t k = 1; k <= ROUNDS; k++) {
    483484                sub_bytes(state, false);
    484485                shift_rows(state);
    485                 if(k < ROUNDS)
     486               
     487                if (k < ROUNDS)
    486488                        mix_columns(state);
    487                 add_round_key(state, key_exp + k*ELEMS);
     489               
     490                add_round_key(state, key_exp + k * ELEMS);
    488491        }
    489492       
    490493        /* Copy state array into output. */
    491         for(size_t i = 0; i < ELEMS; i++) {
    492                 for(size_t j = 0; j < ELEMS; j++) {
    493                         output[i + j*ELEMS] = state[i][j];
    494                 }
     494        for (size_t i = 0; i < ELEMS; i++) {
     495                for (size_t j = 0; j < ELEMS; j++)
     496                        output[i + j * ELEMS] = state[i][j];
    495497        }
    496498       
     
    498500}
    499501
    500 /**
    501  * AES-128 decryption algorithm.
    502  *
    503  * @param key Input key.
    504  * @param input Input data sequence to be decrypted.
     502/** AES-128 decryption algorithm.
     503 *
     504 * @param key    Input key.
     505 * @param input  Input data sequence to be decrypted.
    505506 * @param output Decrypted data sequence.
    506  *
    507  * @return EINVAL when input or key not specified, ENOMEM when pointer for
    508  * output is not allocated, otherwise EOK. 
     507 *
     508 * @return EINVAL when input or key not specified,
     509 *         ENOMEM when pointer for output is not allocated,
     510 *         otherwise EOK.
     511 *
    509512 */
    510513int aes_decrypt(uint8_t *key, uint8_t *input, uint8_t *output)
    511514{
    512         if(!key || !input)
     515        if ((!key) || (!input))
    513516                return EINVAL;
    514517       
    515         if(!output)
     518        if (!output)
    516519                return ENOMEM;
    517520       
    518521        /* Create key expansion. */
    519         uint32_t key_exp[ELEMS * (ROUNDS+1)];
     522        uint32_t key_exp[ELEMS * (ROUNDS + 1)];
    520523        key_expansion(key, key_exp);
    521524       
    522525        /* Copy input into state array. */
    523526        uint8_t state[ELEMS][ELEMS];
    524         for(size_t i = 0; i < ELEMS; i++) {
    525                 for(size_t j = 0; j < ELEMS; j++) {
    526                         state[i][j] = input[i + ELEMS*j];
    527                 }
     527        for (size_t i = 0; i < ELEMS; i++) {
     528                for (size_t j = 0; j < ELEMS; j++)
     529                        state[i][j] = input[i + ELEMS * j];
    528530        }
    529531       
    530532        /* Processing loop. */
    531         add_round_key(state, key_exp + ROUNDS*ELEMS);
    532        
    533         for(int k = ROUNDS - 1; k >= 0; k--) {
     533        add_round_key(state, key_exp + ROUNDS * ELEMS);
     534       
     535        for (int k = ROUNDS - 1; k >= 0; k--) {
    534536                inv_shift_rows(state);
    535537                sub_bytes(state, true);
    536                 add_round_key(state, key_exp + k*ELEMS);
    537                 if(k > 0)
     538                add_round_key(state, key_exp + k * ELEMS);
     539               
     540                if (k > 0)
    538541                        inv_mix_columns(state);
    539542        }
    540543       
    541544        /* Copy state array into output. */
    542         for(size_t i = 0; i < ELEMS; i++) {
    543                 for(size_t j = 0; j < ELEMS; j++) {
    544                         output[i + j*ELEMS] = state[i][j];
    545                 }
     545        for (size_t i = 0; i < ELEMS; i++) {
     546                for (size_t j = 0; j < ELEMS; j++)
     547                        output[i + j * ELEMS] = state[i][j];
    546548        }
    547549       
  • uspace/lib/crypto/crypto.c

    r09044cb r8a64320e  
    2828
    2929/** @file crypto.c
    30  * 
     30 *
    3131 * Cryptographic functions library.
    3232 */
     
    3737#include <errno.h>
    3838#include <byteorder.h>
    39 
    4039#include "crypto.h"
    4140
    42 /* Hash function procedure definition. */
    43 typedef void (*HASH_FUNC)(uint32_t*, uint32_t*);
    44 
    45 /* Length of HMAC block. */
    46 #define HMAC_BLOCK_LENGTH 64
    47 
    48 /* Ceiling for UINT32. */
    49 #define ceil_uint32(val) (((val) - (uint32_t)(val)) > 0 ? \
    50         (uint32_t)((val) + 1) : (uint32_t)(val))
    51 
    52 /* Floor for UINT32. */
    53 #define floor_uint32(val) (((val) - (uint32_t)(val)) < 0 ? \
    54         (uint32_t)((val) - 1) : (uint32_t)(val))
    55 
    56 /* Pick value at specified index from array or zero if out of bounds. */
    57 #define get_at(input, size, i) (i < size ? input[i] : 0)
    58 
    59 /* Init values used in SHA1 and MD5 functions. */
     41/** Hash function procedure definition. */
     42typedef void (*hash_fnc_t)(uint32_t *, uint32_t *);
     43
     44/** Length of HMAC block. */
     45#define HMAC_BLOCK_LENGTH  64
     46
     47/** Ceiling for uint32_t. */
     48#define ceil_uint32(val) \
     49        (((val) - (uint32_t) (val)) > 0 ? \
     50        (uint32_t) ((val) + 1) : (uint32_t) (val))
     51
     52/** Floor for uint32_t. */
     53#define floor_uint32(val) \
     54        (((val) - (uint32_t) (val)) < 0 ? \
     55        (uint32_t) ((val) - 1) : (uint32_t) (val))
     56
     57/** Pick value at specified index from array or zero if out of bounds. */
     58#define get_at(input, size, i) \
     59        ((i) < (size) ? (input[i]) : 0)
     60
     61/** Init values used in SHA1 and MD5 functions. */
    6062static const uint32_t hash_init[] = {
    61         0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0
     63        0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0
    6264};
    6365
    64 /* Shift amount array for MD5 algorithm. */
     66/** Shift amount array for MD5 algorithm. */
    6567static const uint32_t md5_shift[] = {
    6668        7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,  7, 12, 17, 22,
     
    7072};
    7173
    72 /* Substitution box for MD5 algorithm. */
     74/** Substitution box for MD5 algorithm. */
    7375static const uint32_t md5_sbox[] = {
    7476        0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
     
    9092};
    9193
    92 /**
    93  * Working procedure of MD5 cryptographic hash function.
    94  *
    95  * @param h Working array with interim hash parts values.
     94/** Working procedure of MD5 cryptographic hash function.
     95 *
     96 * @param h         Working array with interim hash parts values.
    9697 * @param sched_arr Input array with scheduled values from input string.
     98 *
    9799 */
    98100static void md5_proc(uint32_t *h, uint32_t *sched_arr)
    99101{
    100102        uint32_t f, g, temp;
    101         uint32_t w[HASH_MD5/4];
    102        
    103         memcpy(w, h, (HASH_MD5/4) * sizeof(uint32_t));
    104        
    105         for(size_t k = 0; k < 64; k++) {
    106                 if(k < 16) {
     103        uint32_t w[HASH_MD5 / 4];
     104       
     105        memcpy(w, h, (HASH_MD5 / 4) * sizeof(uint32_t));
     106       
     107        for (size_t k = 0; k < 64; k++) {
     108                if (k < 16) {
    107109                        f = (w[1] & w[2]) | (~w[1] & w[3]);
    108110                        g = k;
    109                 } else if(k >= 16 && k < 32) {
     111                } else if ((k >= 16) && (k < 32)) {
    110112                        f = (w[1] & w[3]) | (w[2] & ~w[3]);
    111                         g = (5*k + 1) % 16;
    112                 } else if(k >= 32 && k < 48) {
     113                        g = (5 * k + 1) % 16;
     114                } else if ((k >= 32) && (k < 48)) {
    113115                        f = w[1] ^ w[2] ^ w[3];
    114                         g = (3*k + 5) % 16;
     116                        g = (3 * k + 5) % 16;
    115117                } else {
    116118                        f = w[2] ^ (w[1] | ~w[3]);
    117                         g = 7*k % 16;
     119                        g = 7 * k % 16;
    118120                }
     121               
    119122                temp = w[3];
    120123                w[3] = w[2];
    121124                w[2] = w[1];
    122                 w[1] += rotl_uint32(w[0] + f + md5_sbox[k] + 
    123                         uint32_t_byteorder_swap(sched_arr[g]), 
    124                         md5_shift[k]);
     125                w[1] += rotl_uint32(w[0] + f + md5_sbox[k] +
     126                    uint32_t_byteorder_swap(sched_arr[g]),
     127                    md5_shift[k]);
    125128                w[0] = temp;
    126129        }
    127130       
    128         for(uint8_t k = 0; k < HASH_MD5/4; k++)
     131        for (uint8_t k = 0; k < HASH_MD5 / 4; k++)
    129132                h[k] += w[k];
    130133}
    131134
    132 /**
    133  * Working procedure of SHA-1 cryptographic hash function.
    134  *
    135  * @param h Working array with interim hash parts values.
     135/** Working procedure of SHA-1 cryptographic hash function.
     136 *
     137 * @param h         Working array with interim hash parts values.
    136138 * @param sched_arr Input array with scheduled values from input string.
     139 *
    137140 */
    138141static void sha1_proc(uint32_t *h, uint32_t *sched_arr)
    139142{
    140143        uint32_t f, cf, temp;
    141         uint32_t w[HASH_SHA1/4];
    142        
    143         for(size_t k = 16; k < 80; k++) {
     144        uint32_t w[HASH_SHA1 / 4];
     145       
     146        for (size_t k = 16; k < 80; k++) {
    144147                sched_arr[k] = rotl_uint32(
    145                         sched_arr[k-3] ^
    146                         sched_arr[k-8] ^
    147                         sched_arr[k-14] ^
    148                         sched_arr[k-16],
    149                         1);
    150         }
    151 
    152         memcpy(w, h, (HASH_SHA1/4) * sizeof(uint32_t));
    153        
    154         for(size_t k = 0; k < 80; k++) {
    155                 if(k < 20) {
     148                    sched_arr[k-3] ^
     149                    sched_arr[k-8] ^
     150                    sched_arr[k-14] ^
     151                    sched_arr[k-16],
     152                    1);
     153        }
     154       
     155        memcpy(w, h, (HASH_SHA1 / 4) * sizeof(uint32_t));
     156       
     157        for (size_t k = 0; k < 80; k++) {
     158                if (k < 20) {
    156159                        f = (w[1] & w[2]) | (~w[1] & w[3]);
    157160                        cf = 0x5A827999;
    158                 } else if(k >= 20 && k < 40) {
     161                } else if ((k >= 20) && (k < 40)) {
    159162                        f = w[1] ^ w[2] ^ w[3];
    160                         cf = 0x6ED9EBA1;
    161                 } else if(k >= 40 && k < 60) {
     163                        cf = 0x6ed9eba1;
     164                } else if ((k >= 40) && (k < 60)) {
    162165                        f = (w[1] & w[2]) | (w[1] & w[3]) | (w[2] & w[3]);
    163                         cf = 0x8F1BBCDC;
     166                        cf = 0x8f1bbcdc;
    164167                } else {
    165168                        f = w[1] ^ w[2] ^ w[3];
    166                         cf = 0xCA62C1D6;
     169                        cf = 0xca62c1d6;
    167170                }
    168 
     171               
    169172                temp = rotl_uint32(w[0], 5) + f + w[4] + cf + sched_arr[k];
    170 
     173               
    171174                w[4] = w[3];
    172175                w[3] = w[2];
     
    175178                w[0] = temp;
    176179        }
    177 
    178         for(uint8_t k = 0; k < HASH_SHA1/4; k++)
     180       
     181        for (uint8_t k = 0; k < HASH_SHA1 / 4; k++)
    179182                h[k] += w[k];
    180183}
    181184
    182 /**
    183  * Create hash based on selected algorithm.
    184  *
    185  * @param input Input message byte sequence.
     185/** Create hash based on selected algorithm.
     186 *
     187 * @param input      Input message byte sequence.
    186188 * @param input_size Size of message sequence.
    187  * @param output Result hash byte sequence.
    188  * @param hash_sel Hash function selector.
    189  *
    190  * @return EINVAL when input not specified, ENOMEM when pointer for
    191  * output hash result is not allocated, otherwise EOK.
     189 * @param output     Result hash byte sequence.
     190 * @param hash_sel   Hash function selector.
     191 *
     192 * @return EINVAL when input not specified,
     193 *         ENOMEM when pointer for output hash result
     194 *         is not allocated, otherwise EOK.
     195 *
    192196 */
    193197int create_hash(uint8_t *input, size_t input_size, uint8_t *output,
    194         hash_func_t hash_sel)
    195 {
    196         if(!input)
     198    hash_func_t hash_sel)
     199{
     200        if (!input)
    197201                return EINVAL;
    198202       
    199         if(!output)
     203        if (!output)
    200204                return ENOMEM;
    201205       
    202         HASH_FUNC hash_func = (hash_sel == HASH_MD5) ? md5_proc : sha1_proc;
     206        hash_fnc_t hash_func = (hash_sel == HASH_MD5) ? md5_proc : sha1_proc;
    203207       
    204208        /* Prepare scheduled input. */
     
    207211        work_input[input_size] = 0x80;
    208212       
    209         size_t blocks = ceil_uint32((((double)input_size + 1) / 4 + 2) / 16);
     213        // FIXME: double?
     214        size_t blocks = ceil_uint32((((double) input_size + 1) / 4 + 2) / 16);
    210215        uint32_t work_arr[blocks * 16];
    211         for(size_t i = 0; i < blocks; i++) {
    212                 for(size_t j = 0; j < 16; j++) {
    213                         work_arr[i*16 + j] = 
    214                         (get_at(work_input, input_size+1, i*64+j*4) << 24) |
    215                         (get_at(work_input, input_size+1, i*64+j*4+1) << 16) |
    216                         (get_at(work_input, input_size+1, i*64+j*4+2) << 8) |
    217                         get_at(work_input, input_size+1, i*64+j*4+3);
     216        for (size_t i = 0; i < blocks; i++) {
     217                for (size_t j = 0; j < 16; j++) {
     218                        work_arr[i*16 + j] =
     219                            (get_at(work_input, input_size + 1, i * 64 + j * 4) << 24) |
     220                            (get_at(work_input, input_size + 1, i * 64 + j * 4 + 1) << 16) |
     221                            (get_at(work_input, input_size + 1, i * 64 + j * 4 + 2) << 8) |
     222                            get_at(work_input, input_size + 1, i * 64 + j * 4 + 3);
    218223                }
    219224        }
    220225       
    221         uint64_t bits_size = (uint64_t)(input_size * 8);
    222         if(hash_sel == HASH_MD5)
     226        uint64_t bits_size = (uint64_t) (input_size * 8);
     227        if (hash_sel == HASH_MD5)
    223228                bits_size = uint64_t_byteorder_swap(bits_size);
    224229       
    225230        work_arr[(blocks - 1) * 16 + 14] = bits_size >> 32;
    226         work_arr[(blocks - 1) * 16 + 15] = bits_size & 0xFFFFFFFF;
     231        work_arr[(blocks - 1) * 16 + 15] = bits_size & 0xffffffff;
    227232       
    228233        /* Hash computation. */
    229         uint32_t h[hash_sel/4];
    230         memcpy(h, hash_init, (hash_sel/4) * sizeof(uint32_t));
     234        uint32_t h[hash_sel / 4];
     235        memcpy(h, hash_init, (hash_sel / 4) * sizeof(uint32_t));
    231236        uint32_t sched_arr[80];
    232         for(size_t i = 0; i < blocks; i++) {
    233                 for(size_t k = 0; k < 16; k++) {
    234                         sched_arr[k] = work_arr[i*16 + k];
    235                 }
     237        for (size_t i = 0; i < blocks; i++) {
     238                for (size_t k = 0; k < 16; k++)
     239                        sched_arr[k] = work_arr[i * 16 + k];
    236240               
    237241                hash_func(h, sched_arr);
     
    239243       
    240244        /* Copy hash parts into final result. */
    241         for(size_t i = 0; i < hash_sel/4; i++) {
    242                 if(hash_sel == HASH_SHA1)
     245        for (size_t i = 0; i < hash_sel / 4; i++) {
     246                if (hash_sel == HASH_SHA1)
    243247                        h[i] = uint32_t_byteorder_swap(h[i]);
    244                 memcpy(output + i*sizeof(uint32_t), &h[i], sizeof(uint32_t));
     248               
     249                memcpy(output + i * sizeof(uint32_t), &h[i], sizeof(uint32_t));
    245250        }
    246251       
     
    248253}
    249254
    250 /**
    251  * Hash-based message authentication code.
    252  *
    253  * @param key Cryptographic key sequence.
     255/** Hash-based message authentication code.
     256 *
     257 * @param key      Cryptographic key sequence.
    254258 * @param key_size Size of key sequence.
    255  * @param msg Message sequence.
     259 * @param msg      Message sequence.
    256260 * @param msg_size Size of message sequence.
    257  * @param hash Output parameter for result hash.
     261 * @param hash     Output parameter for result hash.
    258262 * @param hash_sel Hash function selector.
    259  *
    260  * @return EINVAL when key or message not specified, ENOMEM when pointer for
    261  * output hash result is not allocated, otherwise EOK.
     263 *
     264 * @return EINVAL when key or message not specified,
     265 *         ENOMEM when pointer for output hash result
     266 *         is not allocated, otherwise EOK.
     267 *
    262268 */
    263269int hmac(uint8_t *key, size_t key_size, uint8_t *msg, size_t msg_size,
    264         uint8_t *hash, hash_func_t hash_sel)
    265 {
    266         if(!key || !msg)
     270    uint8_t *hash, hash_func_t hash_sel)
     271{
     272        if ((!key) || (!msg))
    267273                return EINVAL;
    268274       
    269         if(!hash)
     275        if (!hash)
    270276                return ENOMEM;
    271277       
     
    276282        memset(work_key, 0, HMAC_BLOCK_LENGTH);
    277283       
    278         if(key_size > HMAC_BLOCK_LENGTH) {
     284        if(key_size > HMAC_BLOCK_LENGTH)
    279285                create_hash(key, key_size, work_key, hash_sel);
    280         } else {
     286        else
    281287                memcpy(work_key, key, key_size);
    282         }
    283        
    284         for(size_t i = 0; i < HMAC_BLOCK_LENGTH; i++) {
    285                 o_key_pad[i] = work_key[i] ^ 0x5C;
     288       
     289        for (size_t i = 0; i < HMAC_BLOCK_LENGTH; i++) {
     290                o_key_pad[i] = work_key[i] ^ 0x5c;
    286291                i_key_pad[i] = work_key[i] ^ 0x36;
    287292        }
     
    291296        memcpy(temp_work + HMAC_BLOCK_LENGTH, msg, msg_size);
    292297       
    293         create_hash(temp_work, HMAC_BLOCK_LENGTH + msg_size, temp_hash, 
    294                 hash_sel);
     298        create_hash(temp_work, HMAC_BLOCK_LENGTH + msg_size, temp_hash,
     299            hash_sel);
    295300       
    296301        memcpy(temp_work, o_key_pad, HMAC_BLOCK_LENGTH);
     
    302307}
    303308
    304 /**
    305  * Password-Based Key Derivation Function 2 as defined in RFC 2898,
    306  * using HMAC-SHA1 with 4096 iterations and 32 bytes key result used
    307  * for WPA/WPA2.
    308  * 
    309  * @param pass Password sequence.
     309/** Password-Based Key Derivation Function 2.
     310 *
     311 * As defined in RFC 2898, using HMAC-SHA1 with 4096 iterations
     312 * and 32 bytes key result used for WPA/WPA2.
     313 *
     314 * @param pass      Password sequence.
    310315 * @param pass_size Password sequence length.
    311  * @param salt Salt sequence to be used with password.
     316 * @param salt      Salt sequence to be used with password.
    312317 * @param salt_size Salt sequence length.
    313  * @param hash Output parameter for result hash (32 byte value).
    314  *
    315  * @return EINVAL when pass or salt not specified, ENOMEM when pointer for
    316  * output hash result is not allocated, otherwise EOK.
    317  */
    318 int pbkdf2(uint8_t *pass, size_t pass_size, uint8_t *salt, size_t salt_size,
    319         uint8_t *hash)
    320 {
    321         if(!pass || !salt)
     318 * @param hash      Output parameter for result hash (32 byte value).
     319 *
     320 * @return EINVAL when pass or salt not specified,
     321 *         ENOMEM when pointer for output hash result
     322 *         is not allocated, otherwise EOK.
     323 *
     324 */
     325int pbkdf2(uint8_t *pass, size_t pass_size, uint8_t *salt, size_t salt_size,
     326    uint8_t *hash)
     327{
     328        if ((!pass) || (!salt))
    322329                return EINVAL;
    323330       
    324         if(!hash)
     331        if (!hash)
    325332                return ENOMEM;
    326333       
     
    330337        uint8_t temp_hmac[HASH_SHA1];
    331338        uint8_t xor_hmac[HASH_SHA1];
    332         uint8_t temp_hash[HASH_SHA1*2];
    333        
    334         for(size_t i = 0; i < 2; i++) {
    335                 uint32_t be_i = host2uint32_t_be(i+1);
     339        uint8_t temp_hash[HASH_SHA1 * 2];
     340       
     341        for (size_t i = 0; i < 2; i++) {
     342                uint32_t be_i = host2uint32_t_be(i + 1);
     343               
    336344                memcpy(work_salt + salt_size, &be_i, 4);
    337345                hmac(pass, pass_size, work_salt, salt_size + 4,
    338                         work_hmac, HASH_SHA1);
     346                    work_hmac, HASH_SHA1);
    339347                memcpy(xor_hmac, work_hmac, HASH_SHA1);
    340348               
    341                 for(size_t k = 1; k < 4096; k++) {
     349                for (size_t k = 1; k < 4096; k++) {
    342350                        memcpy(temp_hmac, work_hmac, HASH_SHA1);
    343                         hmac(pass, pass_size, temp_hmac, HASH_SHA1,
    344                                 work_hmac, HASH_SHA1);
    345                         for(size_t t = 0; t < HASH_SHA1; t++) {
     351                        hmac(pass, pass_size, temp_hmac, HASH_SHA1,
     352                            work_hmac, HASH_SHA1);
     353                       
     354                        for (size_t t = 0; t < HASH_SHA1; t++)
    346355                                xor_hmac[t] ^= work_hmac[t];
    347                         }
    348356                }
    349                 memcpy(temp_hash + i*HASH_SHA1, xor_hmac, HASH_SHA1);
     357               
     358                memcpy(temp_hash + i * HASH_SHA1, xor_hmac, HASH_SHA1);
    350359        }
    351360       
  • uspace/lib/crypto/crypto.h

    r09044cb r8a64320e  
    3232#include <sys/types.h>
    3333
    34 #define AES_CIPHER_LENGTH 16
    35 #define PBKDF2_KEY_LENGTH 32
     34#define AES_CIPHER_LENGTH  16
     35#define PBKDF2_KEY_LENGTH  32
    3636
    37 /* Left rotation for UINT32. */
    38 #define rotl_uint32(val, shift) (((val) << shift) | ((val) >> (32 - shift)))
     37/* Left rotation for uint32_t. */
     38#define rotl_uint32(val, shift) \
     39        (((val) << shift) | ((val) >> (32 - shift)))
    3940
    40 /* Right rotation for UINT32. */
    41 #define rotr_uint32(val, shift) (((val) >> shift) | ((val) << (32 - shift)))
     41/* Right rotation for uint32_t. */
     42#define rotr_uint32(val, shift) \
     43        (((val) >> shift) | ((val) << (32 - shift)))
    4244
    4345/** Hash function selector and also result hash length indicator. */
    4446typedef enum {
    45         HASH_MD5 =      16,
    46         HASH_SHA1 =     20
     47        HASH_MD5 =  16,
     48        HASH_SHA1 = 20
    4749} hash_func_t;
    4850
    49 extern int rc4(uint8_t *key, size_t key_size, uint8_t *input, size_t input_size,
    50         size_t skip, uint8_t *output);
    51 extern int aes_encrypt(uint8_t *key, uint8_t *input, uint8_t *output);
    52 extern int aes_decrypt(uint8_t *key, uint8_t *input, uint8_t *output);
    53 extern int create_hash(uint8_t *input, size_t input_size, uint8_t *output,
    54         hash_func_t hash_sel);
    55 extern int hmac(uint8_t *key, size_t key_size, uint8_t *msg, size_t msg_size,
    56         uint8_t *hash, hash_func_t hash_sel);
    57 extern int pbkdf2(uint8_t *pass, size_t pass_size, uint8_t *salt,
    58         size_t salt_size, uint8_t *hash);
     51extern int rc4(uint8_t *, size_t, uint8_t *, size_t, size_t, uint8_t *);
     52extern int aes_encrypt(uint8_t *, uint8_t *, uint8_t *);
     53extern int aes_decrypt(uint8_t *, uint8_t *, uint8_t *);
     54extern int create_hash(uint8_t *, size_t, uint8_t *, hash_func_t);
     55extern int hmac(uint8_t *, size_t, uint8_t *, size_t, uint8_t *, hash_func_t);
     56extern int pbkdf2(uint8_t *, size_t, uint8_t *, size_t, uint8_t *);
    5957
    6058#endif
  • uspace/lib/crypto/rc4.c

    r09044cb r8a64320e  
    2828
    2929/** @file rc4.c
    30  * 
     30 *
    3131 * Implementation of ARC4 symmetric cipher cryptographic algorithm.
    32  * 
     32 *
    3333 */
    3434
    3535#include <errno.h>
    3636#include <mem.h>
    37 
    3837#include "crypto.h"
    3938
    4039/* Sbox table size. */
    41 #define SBOX_SIZE 256
     40#define SBOX_SIZE  256
    4241
    43 /**
    44  * Swap two values in sbox.
    45  *
    46  * @param i First index of value in sbox to be swapped.
    47  * @param j Second index of value in sbox to be swapped.
     42/** Swap two values in sbox.
     43 *
     44 * @param i    First index of value in sbox to be swapped.
     45 * @param j    Second index of value in sbox to be swapped.
    4846 * @param sbox Sbox to be modified.
     47 *
    4948 */
    5049static void swap(size_t i, size_t j, uint8_t *sbox)
     
    5554}
    5655
    57 /**
    58  * Sbox initialization procedure.
    59  *
    60  * @param key Input key.
     56/** Sbox initialization procedure.
     57 *
     58 * @param key      Input key.
    6159 * @param key_size Size of key sequence.
    62  * @param sbox Place for result sbox.
     60 * @param sbox     Place for result sbox.
     61 *
    6362 */
    6463static void create_sbox(uint8_t *key, size_t key_size, uint8_t *sbox)
    6564{
    66         for(size_t i = 0; i < SBOX_SIZE; i++) {
     65        for (size_t i = 0; i < SBOX_SIZE; i++)
    6766                sbox[i] = i;
    68         }
    6967       
    7068        uint8_t j = 0;
    71         for(size_t i = 0; i < SBOX_SIZE; i++) {
     69        for (size_t i = 0; i < SBOX_SIZE; i++) {
    7270                j = j + sbox[i] + key[i % key_size];
    7371                swap(i, j, sbox);
     
    7573}
    7674
    77 /**
    78  * ARC4 encryption/decryption algorithm.
    79  *
    80  * @param key Input key.
    81  * @param key_size Size of key sequence.
    82  * @param input Input data sequence to be processed.
     75/** ARC4 encryption/decryption algorithm.
     76 *
     77 * @param key        Input key.
     78 * @param key_size   Size of key sequence.
     79 * @param input      Input data sequence to be processed.
    8380 * @param input_size Size of input data sequence.
    84  * @param skip Number of bytes to be skipped from the beginning of key stream.
    85  * @param output Result data sequence.
    86  *
    87  * @return EINVAL when input or key not specified, ENOMEM when pointer for
    88  * output is not allocated, otherwise EOK. 
     81 * @param skip       Number of bytes to be skipped from
     82 *                   the beginning of key stream.
     83 * @param output     Result data sequence.
     84 *
     85 * @return EINVAL when input or key not specified,
     86 *         ENOMEM when pointer for output is not allocated,
     87 *         otherwise EOK.
     88 *
    8989 */
    90 int rc4(uint8_t *key, size_t key_size, uint8_t *input, size_t input_size, 
    91         size_t skip, uint8_t *output)
     90int rc4(uint8_t *key, size_t key_size, uint8_t *input, size_t input_size,
     91    size_t skip, uint8_t *output)
    9292{
    93         if(!key || !input)
     93        if ((!key) || (!input))
    9494                return EINVAL;
    9595       
    96         if(!output)
     96        if (!output)
    9797                return ENOMEM;
    9898       
     
    102102       
    103103        /* Skip first x bytes. */
    104         uint8_t i = 0, j = 0;
    105         for(size_t k = 0; k < skip; k++) {
     104        uint8_t i = 0;
     105        uint8_t j = 0;
     106        for (size_t k = 0; k < skip; k++) {
    106107                i = i + 1;
    107108                j = j + sbox[i];
     
    111112        /* Processing loop. */
    112113        uint8_t val;
    113         for(size_t k = 0; k < input_size; k++) {
     114        for (size_t k = 0; k < input_size; k++) {
    114115                i = i + 1;
    115116                j = j + sbox[i];
  • uspace/lib/drv/generic/remote_ieee80211.c

    r09044cb r8a64320e  
    4040#include <inet/dhcp.h>
    4141#include <inet/inetcfg.h>
    42 
    4342#include "ops/ieee80211.h"
    4443#include "ieee80211_iface.h"
    4544#include "nic_iface.h"
    4645
    47 #define MAX_STRING_SIZE 32
     46#define MAX_STRING_SIZE  32
    4847
    4948/** IEEE 802.11 RPC functions IDs. */
     
    5655/** Get scan results from IEEE 802.11 device
    5756 *
    58  * @param[in] dev_sess Device session.
    59  * @param[out] results Structure where to put scan results.
     57 * @param[in]  dev_sess Device session.
     58 * @param[out] results  Structure where to put scan results.
    6059 *
    6160 * @return EOK If the operation was successfully completed,
    62  * negative error code otherwise.
    63  */
    64 int ieee80211_get_scan_results(async_sess_t *dev_sess,
    65         ieee80211_scan_results_t *results, bool now)
     61 *         negative error code otherwise.
     62 *
     63 */
     64int ieee80211_get_scan_results(async_sess_t *dev_sess,
     65    ieee80211_scan_results_t *results, bool now)
    6666{
    6767        assert(results);
     
    7171        aid_t aid = async_send_2(exch, DEV_IFACE_ID(IEEE80211_DEV_IFACE),
    7272            IEEE80211_GET_SCAN_RESULTS, now, NULL);
    73         int rc = async_data_read_start(exch, results, sizeof(ieee80211_scan_results_t));
     73        int rc = async_data_read_start(exch, results,
     74            sizeof(ieee80211_scan_results_t));
    7475        async_exchange_end(exch);
    75 
     76       
    7677        sysarg_t res;
    7778        async_wait_for(aid, &res);
     
    7980        if(res != EOK)
    8081                return (int) res;
    81         else
    82                 return rc;
     82       
     83        return rc;
    8384}
    8485
    8586static bool mac_matches(uint8_t *mac1, uint8_t *mac2)
    8687{
    87         for(size_t i = 0; i < ETH_ADDR; i++) {
    88                 if(mac1[i] != mac2[i])
     88        for (size_t i = 0; i < ETH_ADDR; i++) {
     89                if (mac1[i] != mac2[i])
    8990                        return false;
    9091        }
     
    108109                        return -1;
    109110               
    110                 if(mac_matches(mac, link_info.mac_addr)) {
     111                if (mac_matches(mac, link_info.mac_addr))
    111112                        return link_list[i];
    112                 }
    113113        }
    114114       
     
    118118/** Connect to specified network.
    119119 *
    120  * @param[in] dev_sess Device session.
     120 * @param[in] dev_sess   Device session.
    121121 * @param[in] ssid_start Network SSID prefix.
    122  * @param[in] password Network password (pass empty string if not needed).
     122 * @param[in] password   Network password (pass empty string if not needed).
    123123 *
    124124 * @return EOK If the operation was successfully completed,
    125  * negative error code otherwise.
     125 *         negative error code otherwise.
     126 *
    126127 */
    127128int ieee80211_connect(async_sess_t *dev_sess, char *ssid_start, char *password)
     
    136137            IEEE80211_CONNECT, NULL);
    137138       
    138         sysarg_t rc = async_data_write_start(exch, ssid_start, 
    139                 str_size(ssid_start) + 1);
     139        sysarg_t rc = async_data_write_start(exch, ssid_start,
     140            str_size(ssid_start) + 1);
    140141        if (rc != EOK) {
    141142                async_exchange_end(exch);
     
    144145                if (rc_orig == EOK)
    145146                        return (int) rc;
    146                 else
    147                         return (int) rc_orig;
    148         }
    149        
    150         if(password == NULL) {
     147               
     148                return (int) rc_orig;
     149        }
     150       
     151        // FIXME: Typecasting string literal
     152        if (password == NULL)
    151153                password = (char *) "";
    152         }
    153154       
    154155        rc = async_data_write_start(exch, password, str_size(password) + 1);
     
    159160                if (rc_orig == EOK)
    160161                        return (int) rc;
    161                 else
    162                         return (int) rc_orig;
     162               
     163                return (int) rc_orig;
    163164        }
    164165       
    165166        async_exchange_end(exch);
    166 
     167       
    167168        async_wait_for(aid, &rc);
    168169        if (rc != EOK)
     
    176177       
    177178        sysarg_t link_id = get_link_id(wifi_mac.address);
    178         if(link_id == ((sysarg_t) -1))
     179        if (link_id == ((sysarg_t) -1))
    179180                return EINVAL;
    180181       
     
    189190 *
    190191 * @return EOK If the operation was successfully completed,
    191  * negative error code otherwise.
     192 *         negative error code otherwise.
     193 *
    192194 */
    193195int ieee80211_disconnect(async_sess_t *dev_sess)
     
    198200        async_exchange_end(exch);
    199201       
    200         if(rc != EOK)
     202        if (rc != EOK)
    201203                return rc;
    202204       
     
    227229                        return rc;
    228230               
    229                 if(mac_matches(wifi_mac.address, link_info.mac_addr)) {
    230                         if(str_test_prefix(addr_info.name, "dhcp")) {
     231                if (mac_matches(wifi_mac.address, link_info.mac_addr)) {
     232                        if (str_test_prefix(addr_info.name, "dhcp")) {
    231233                                rc = inetcfg_addr_delete(addr_list[i]);
    232                                 if(rc != EOK)
     234                                if (rc != EOK)
    233235                                        return rc;
     236                               
    234237                                break;
    235238                        }
     
    237240        }
    238241       
    239         /* 
     242        /*
    240243         * TODO: At this moment there can be only one DHCP route,
    241244         * so it must be reimplemented after this limitation will be
     
    252255                        return rc;
    253256               
    254                 if(str_test_prefix(route_info.name, "dhcp")) {
     257                if (str_test_prefix(route_info.name, "dhcp")) {
    255258                        rc = inetcfg_sroute_delete(route_list[i]);
    256                         if(rc != EOK)
     259                        if (rc != EOK)
    257260                                return rc;
     261                       
    258262                        break;
    259263                }
  • uspace/lib/drv/include/ieee80211_iface.h

    r09044cb r8a64320e  
    2727 */
    2828
    29  /** @addtogroup libc
     29/** @addtogroup libc
    3030 * @{
    3131 */
     
    3939#include <async.h>
    4040
    41 extern int ieee80211_get_scan_results(async_sess_t *, 
    42         ieee80211_scan_results_t *, bool);
     41extern int ieee80211_get_scan_results(async_sess_t *,
     42    ieee80211_scan_results_t *, bool);
    4343extern int ieee80211_connect(async_sess_t *, char *, char *);
    4444extern int ieee80211_disconnect(async_sess_t *);
  • uspace/lib/drv/include/ops/ieee80211.h

    r09044cb r8a64320e  
    4242/** IEEE 802.11 interface functions definition. */
    4343typedef struct ieee80211_iface {
    44         /**
    45          * Fetch scan results from IEEE 802.11 device.
    46          *
    47          * @param fun IEEE 802.11 function.
     44        /** Fetch scan results from IEEE 802.11 device.
     45         *
     46         * @param fun     IEEE 802.11 function.
    4847         * @param results Structure where to put scan results.
    49          * @param now Whether to initiate scan immediately.
    50          * 
     48         * @param now     Whether to initiate scan immediately.
     49         *
    5150         * @return EOK if succeed, negative error code otherwise.
     51         *
    5252         */
    5353        int (*get_scan_results)(ddf_fun_t *, ieee80211_scan_results_t *, bool);
    5454       
    55         /**
    56          * Connect IEEE 802.11 device to specified network.
     55        /** Connect IEEE 802.11 device to specified network.
    5756         *
    58          * @param fun IEEE 802.11 function.
    59          * @param ssid Network SSID.
     57         * @param fun      IEEE 802.11 function.
     58         * @param ssid     Network SSID.
    6059         * @param password Network password (empty string if not needed).
    61          * 
     60         *
    6261         * @return EOK if succeed, negative error code otherwise.
     62         *
    6363         */
    6464        int (*connect)(ddf_fun_t *, char *, char *);
    6565       
    66         /**
    67          * Disconnect IEEE 802.11 device from network.
     66        /** Disconnect IEEE 802.11 device from network.
    6867         *
    6968         * @param fun IEEE 802.11 function.
    70          * 
     69         *
    7170         * @return EOK if succeed, negative error code otherwise.
     71         *
    7272         */
    7373        int (*disconnect)(ddf_fun_t *);
  • uspace/lib/ieee80211/Makefile

    r09044cb r8a64320e  
    4545        src/ieee80211_impl.c \
    4646        src/ieee80211_iface_impl.c \
    47        
    4847
    4948include $(USPACE_PREFIX)/Makefile.common
  • uspace/lib/ieee80211/include/ieee80211.h

    r09044cb r8a64320e  
    2727 */
    2828
    29 /** 
     29/**
    3030 * @addtogroup libieee80211
    3131 * @{
     
    4343#include <ops/ieee80211.h>
    4444
    45 #define DEVICE_CATEGORY_IEEE80211 "ieee80211"
     45#define DEVICE_CATEGORY_IEEE80211  "ieee80211"
    4646
    4747struct ieee80211_dev;
     
    4949
    5050/** Initial channel frequency. */
    51 #define IEEE80211_FIRST_FREQ 2412
     51#define IEEE80211_FIRST_FREQ  2412
    5252
    5353/** Max supported channel frequency. */
    54 #define IEEE80211_MAX_FREQ 2472
    55 
    56 /* Gap between IEEE80211 channels in MHz. */
    57 #define IEEE80211_CHANNEL_GAP 5
    58 
    59 /* Max AMPDU factor. */
    60 #define IEEE80211_MAX_AMPDU_FACTOR 13
    61 
    62 /* Max authentication password length. */
    63 #define IEEE80211_MAX_PASSW_LEN 64
     54#define IEEE80211_MAX_FREQ  2472
     55
     56/** Gap between IEEE80211 channels in MHz. */
     57#define IEEE80211_CHANNEL_GAP  5
     58
     59/** Max AMPDU factor. */
     60#define IEEE80211_MAX_AMPDU_FACTOR  13
     61
     62/** Max authentication password length. */
     63#define IEEE80211_MAX_PASSW_LEN  64
    6464
    6565/** IEEE 802.11 b/g supported data rates in units of 500 kb/s. */
     
    9797/** IEEE 802.11 callback functions. */
    9898typedef struct {
    99         /**
    100          * Function that is called at device initalization. This should
    101          * get device into running state.
    102          *
    103          * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    104          *
    105          * @return EOK if succeed, negative error code otherwise.
     99        /** unction that is called at device initalization.
     100         *
     101         * This should get device into running state.
     102         *
     103         * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
     104         *
     105         * @return EOK if succeed, negative error code otherwise.
     106         *
    106107         */
    107108        int (*start)(struct ieee80211_dev *);
    108109       
    109         /**
    110          * Scan neighborhood for networks. There should be implemented
    111          * scanning of whole bandwidth. Incoming results are processed by
    112          * IEEE 802.11 framework itself.
    113          *
    114          * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    115          *
    116          * @return EOK if succeed, negative error code otherwise.
     110        /** Scan neighborhood for networks.
     111         *
     112         * There should be implemented scanning of whole bandwidth.
     113         * Incoming results are processed by IEEE 802.11 framework itself.
     114         *
     115         * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
     116         *
     117         * @return EOK if succeed, negative error code otherwise.
     118         *
    117119         */
    118120        int (*scan)(struct ieee80211_dev *);
    119121       
    120         /**
    121          * Handler for TX frames to be send from device. This should be
    122          * called for every frame that has to be send from IEEE 802.11 device.
    123          *
    124          * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    125          * @param buffer Buffer with data to be send.
    126          * @param buffer_size Size of buffer.
    127          *
    128          * @return EOK if succeed, negative error code otherwise.
     122        /** Handler for TX frames to be send from device.
     123         *
     124         * This should be called for every frame that has to be send
     125         * from IEEE 802.11 device.
     126         *
     127         * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
     128         * @param buffer        Buffer with data to be send.
     129         * @param buffer_size   Size of buffer.
     130         *
     131         * @return EOK if succeed, negative error code otherwise.
     132         *
    129133         */
    130134        int (*tx_handler)(struct ieee80211_dev *, void *, size_t);
    131135       
    132         /**
    133          * Set device operating frequency to given value.
    134          *
    135          * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    136          * @param freq New device operating frequency.
    137          *
    138          * @return EOK if succeed, negative error code otherwise.
     136        /** Set device operating frequency to given value.
     137         *
     138         * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
     139         * @param freq          New device operating frequency.
     140         *
     141         * @return EOK if succeed, negative error code otherwise.
     142         *
    139143         */
    140144        int (*set_freq)(struct ieee80211_dev *, uint16_t);
    141145       
    142         /**
    143          * Callback to inform device about BSSID change.
    144          *
    145          * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    146          * @param connected True if connected to new BSSID, otherwise false.
    147          *
    148          * @return EOK if succeed, negative error code otherwise.
     146        /** Callback to inform device about BSSID change.
     147         *
     148         * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
     149         * @param connected     True if connected to new BSSID, otherwise false.
     150         *
     151         * @return EOK if succeed, negative error code otherwise.
     152         *
    149153         */
    150154        int (*bssid_change)(struct ieee80211_dev *, bool);
    151155       
    152         /**
    153          * Callback to setup encryption key in IEEE 802.11 device.
    154          *
    155          * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    156          * @param key_conf Key config structure.
    157          * @param insert True to insert this key to device, false to remove it.
    158          *
    159          * @return EOK if succeed, negative error code otherwise.
    160          */
    161         int (*key_config)(struct ieee80211_dev *,
    162                 ieee80211_key_config_t *key_conf, bool);
     156        /** Callback to setup encryption key in IEEE 802.11 device.
     157         *
     158         * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
     159         * @param key_conf      Key config structure.
     160         * @param insert        True to insert this key to device,
     161         *                      false to remove it.
     162         *
     163         * @return EOK if succeed, negative error code otherwise.
     164         *
     165         */
     166        int (*key_config)(struct ieee80211_dev *,
     167            ieee80211_key_config_t *key_conf, bool);
    163168} ieee80211_ops_t;
    164169
    165170/* Initialization functions. */
    166171extern ieee80211_dev_t *ieee80211_device_create(void);
    167 extern int ieee80211_device_init(ieee80211_dev_t *ieee80211_dev,
    168         ddf_dev_t *ddf_dev);
    169 extern int ieee80211_init(ieee80211_dev_t *ieee80211_dev,
    170         ieee80211_ops_t *ieee80211_ops, ieee80211_iface_t *ieee80211_iface,
    171         nic_iface_t *ieee80211_nic_iface, ddf_dev_ops_t *ieee80211_dev_ops);
     172extern int ieee80211_device_init(ieee80211_dev_t *, ddf_dev_t *);
     173extern int ieee80211_init(ieee80211_dev_t *, ieee80211_ops_t *,
     174    ieee80211_iface_t *, nic_iface_t *, ddf_dev_ops_t *);
    172175
    173176/* Getters & setters, queries & reports. */
    174 extern void *ieee80211_get_specific(ieee80211_dev_t *ieee80211_dev);
    175 extern void ieee80211_set_specific(ieee80211_dev_t *ieee80211_dev,
    176         void *specific);
    177 extern ddf_dev_t *ieee80211_get_ddf_dev(ieee80211_dev_t* ieee80211_dev);
    178 extern ieee80211_operating_mode_t
    179         ieee80211_query_current_op_mode(ieee80211_dev_t *ieee80211_dev);
    180 extern uint16_t ieee80211_query_current_freq(ieee80211_dev_t *ieee80211_dev);
    181 extern void ieee80211_query_bssid(ieee80211_dev_t* ieee80211_dev,
    182         nic_address_t *bssid);
    183 extern bool ieee80211_is_connected(ieee80211_dev_t* ieee80211_dev);
    184 extern void ieee80211_report_current_op_mode(ieee80211_dev_t *ieee80211_dev,
    185         ieee80211_operating_mode_t op_mode);
    186 extern void ieee80211_report_current_freq(ieee80211_dev_t *ieee80211_dev,
    187         uint16_t freq);
    188 extern uint16_t ieee80211_get_aid(ieee80211_dev_t* ieee80211_dev);
    189 extern int ieee80211_get_pairwise_security(ieee80211_dev_t* ieee80211_dev);
    190 extern bool ieee80211_is_ready(ieee80211_dev_t* ieee80211_dev);
    191 extern void ieee80211_set_ready(ieee80211_dev_t* ieee80211_dev, bool ready);
    192 extern bool ieee80211_query_using_key(ieee80211_dev_t* ieee80211_dev);
    193 extern void ieee80211_setup_key_confirm(ieee80211_dev_t* ieee80211_dev,
    194         bool using_key);
    195 
    196 extern bool ieee80211_is_data_frame(uint16_t frame_ctrl);
    197 extern bool ieee80211_is_mgmt_frame(uint16_t frame_ctrl);
    198 extern bool ieee80211_is_beacon_frame(uint16_t frame_ctrl);
    199 extern bool ieee80211_is_probe_response_frame(uint16_t frame_ctrl);
    200 extern bool ieee80211_is_auth_frame(uint16_t frame_ctrl);
    201 extern bool ieee80211_is_assoc_response_frame(uint16_t frame_ctrl);
     177extern void *ieee80211_get_specific(ieee80211_dev_t *);
     178extern void ieee80211_set_specific(ieee80211_dev_t *, void *);
     179extern ddf_dev_t *ieee80211_get_ddf_dev(ieee80211_dev_t *);
     180extern ieee80211_operating_mode_t
     181    ieee80211_query_current_op_mode(ieee80211_dev_t *);
     182extern uint16_t ieee80211_query_current_freq(ieee80211_dev_t *);
     183extern void ieee80211_query_bssid(ieee80211_dev_t *, nic_address_t *);
     184extern bool ieee80211_is_connected(ieee80211_dev_t *);
     185extern void ieee80211_report_current_op_mode(ieee80211_dev_t *,
     186    ieee80211_operating_mode_t);
     187extern void ieee80211_report_current_freq(ieee80211_dev_t *, uint16_t);
     188extern uint16_t ieee80211_get_aid(ieee80211_dev_t *);
     189extern int ieee80211_get_pairwise_security(ieee80211_dev_t *);
     190extern bool ieee80211_is_ready(ieee80211_dev_t *);
     191extern void ieee80211_set_ready(ieee80211_dev_t *, bool);
     192extern bool ieee80211_query_using_key(ieee80211_dev_t *);
     193extern void ieee80211_setup_key_confirm(ieee80211_dev_t *, bool);
     194
     195extern bool ieee80211_is_data_frame(uint16_t);
     196extern bool ieee80211_is_mgmt_frame(uint16_t);
     197extern bool ieee80211_is_beacon_frame(uint16_t);
     198extern bool ieee80211_is_probe_response_frame(uint16_t);
     199extern bool ieee80211_is_auth_frame(uint16_t);
     200extern bool ieee80211_is_assoc_response_frame(uint16_t);
    202201
    203202/* Worker functions. */
    204 extern int ieee80211_rx_handler(ieee80211_dev_t *ieee80211_dev, void *buffer,
    205         size_t buffer_size);
    206 
    207 #endif // LIB_IEEE80211_H
     203extern int ieee80211_rx_handler(ieee80211_dev_t *, void *, size_t);
     204
     205#endif
    208206
    209207/** @}
  • uspace/lib/ieee80211/include/ieee80211_iface_impl.h

    r09044cb r8a64320e  
    2727 */
    2828
    29 /** 
     29/**
    3030 * @addtogroup libieee80211
    3131 * @{
     
    3333
    3434/** @file ieee80211_iface_impl.h
    35  * 
     35 *
    3636 * IEEE 802.11 default interface functions definition.
    3737 */
    3838
    3939#ifndef LIB_IEEE80211_IFACE_IMPL_H
    40 #define LIB_IEEE80211_IFACE_IMPL_H
     40#define LIB_IEEE80211_IFACE_IMPL_H
    4141
    4242#include <ddf/driver.h>
    43 
    4443#include "ieee80211.h"
    4544
    46 extern int ieee80211_get_scan_results_impl(ddf_fun_t *fun,
    47         ieee80211_scan_results_t *results, bool now);
    48 extern int ieee80211_connect_impl(ddf_fun_t *fun, char *ssid_start,
    49         char *password);
    50 extern int ieee80211_disconnect_impl(ddf_fun_t *fun);
     45extern int ieee80211_get_scan_results_impl(ddf_fun_t *,
     46    ieee80211_scan_results_t *, bool);
     47extern int ieee80211_connect_impl(ddf_fun_t *, char *, char *);
     48extern int ieee80211_disconnect_impl(ddf_fun_t *);
    5149
    52 #endif  /* LIB_IEEE80211_IFACE_IMPL_H */
     50#endif  /* LIB_IEEE80211_IFACE_IMPL_H */
    5351
    5452/** @}
  • uspace/lib/ieee80211/include/ieee80211_impl.h

    r09044cb r8a64320e  
    2727 */
    2828
    29 /** 
     29/**
    3030 * @addtogroup libieee80211
    3131 * @{
     
    3333
    3434/** @file ieee80211_impl.h
    35  * 
     35 *
    3636 * IEEE 802.11 default device functions definition.
    3737 */
    3838
    3939#ifndef LIB_IEEE80211_IMPL_H
    40 #define LIB_IEEE80211_IMPL_H
     40#define LIB_IEEE80211_IMPL_H
    4141
    4242#include "ieee80211_private.h"
    4343
    44 extern int ieee80211_start_impl(ieee80211_dev_t *ieee80211_dev);
    45 extern int ieee80211_tx_handler_impl(ieee80211_dev_t *ieee80211_dev,
    46         void *buffer, size_t buffer_size);
    47 extern int ieee80211_set_freq_impl(ieee80211_dev_t *ieee80211_dev,
    48         uint16_t freq);
    49 extern int ieee80211_bssid_change_impl(ieee80211_dev_t *ieee80211_dev,
    50         bool connected);
    51 extern int ieee80211_key_config_impl(ieee80211_dev_t *ieee80211_dev,
    52         ieee80211_key_config_t *key_conf, bool insert);
    53 extern int ieee80211_scan_impl(ieee80211_dev_t *ieee80211_dev);
    54 extern int ieee80211_prf(uint8_t *key, uint8_t *data, uint8_t *hash,
    55         size_t output_size);
    56 extern int ieee80211_rc4_key_unwrap(uint8_t *key, uint8_t *data,
    57         size_t data_size, uint8_t *output);
    58 extern int ieee80211_aes_key_unwrap(uint8_t *kek, uint8_t *data,
    59         size_t data_size, uint8_t *output);
    60 extern int ieee80211_michael_mic(uint8_t *key, uint8_t *data, size_t data_size,
    61         uint8_t *mic);
    62 extern uint16_t uint16le_from_seq(void *seq);
    63 extern uint32_t uint32le_from_seq(void *seq);
    64 extern uint16_t uint16be_from_seq(void *seq);
    65 extern uint32_t uint32be_from_seq(void *seq);
    66 extern int rnd_sequence(uint8_t *sequence, size_t length);
    67 extern uint8_t *min_sequence(uint8_t *seq1, uint8_t *seq2, size_t size);
    68 extern uint8_t *max_sequence(uint8_t *seq1, uint8_t *seq2, size_t size);
     44extern int ieee80211_start_impl(ieee80211_dev_t *);
     45extern int ieee80211_tx_handler_impl(ieee80211_dev_t *, void *, size_t);
     46extern int ieee80211_set_freq_impl(ieee80211_dev_t *, uint16_t);
     47extern int ieee80211_bssid_change_impl(ieee80211_dev_t *, bool);
     48extern int ieee80211_key_config_impl(ieee80211_dev_t *,
     49    ieee80211_key_config_t *, bool);
     50extern int ieee80211_scan_impl(ieee80211_dev_t *);
     51extern int ieee80211_prf(uint8_t *, uint8_t *, uint8_t *, size_t);
     52extern int ieee80211_rc4_key_unwrap(uint8_t *, uint8_t *, size_t, uint8_t *);
     53extern int ieee80211_aes_key_unwrap(uint8_t *, uint8_t *, size_t, uint8_t *);
     54extern int ieee80211_michael_mic(uint8_t *, uint8_t *, size_t, uint8_t *);
     55extern uint16_t uint16le_from_seq(void *);
     56extern uint32_t uint32le_from_seq(void *);
     57extern uint16_t uint16be_from_seq(void *);
     58extern uint32_t uint32be_from_seq(void *);
     59extern int rnd_sequence(uint8_t *, size_t);
     60extern uint8_t *min_sequence(uint8_t *, uint8_t *, size_t);
     61extern uint8_t *max_sequence(uint8_t *, uint8_t *, size_t);
    6962
    70 #endif  /* LIB_IEEE80211_IMPL_H */
     63#endif  /* LIB_IEEE80211_IMPL_H */
    7164
    7265/** @}
  • uspace/lib/ieee80211/include/ieee80211_private.h

    r09044cb r8a64320e  
    2727 */
    2828
    29 /** 
     29/**
    3030 * @addtogroup libieee80211
    3131 * @{
     
    3333
    3434/** @file ieee80211.h
    35  * 
     35 *
    3636 * Internal IEEE 802.11 header that should not be included.
    3737 */
    3838
    39 #ifndef LIBNET_IEEE80211_PRIVATE_H
    40 #define LIBNET_IEEE80211_PRIVATE_H
     39#ifndef LIB_IEEE80211_PRIVATE_H
     40#define LIB_IEEE80211_PRIVATE_H
    4141
    4242#include <fibril_synch.h>
     
    4444#include <ddf/driver.h>
    4545#include <sys/types.h>
    46 
    4746#include <ieee80211/ieee80211.h>
    4847#include "ieee80211.h"
    4948
    50 /* Timeout in us for waiting to authentication/association response. */
    51 #define AUTH_TIMEOUT 200000
    52 
    53 /* Timeout in us for waiting to finish 4-way handshake process. */
    54 #define HANDSHAKE_TIMEOUT 5000000
    55 
    56 /* Scanning period. */
    57 #define SCAN_PERIOD_USEC 35000000
    58 
    59 /* Time to wait for beacons on channel. */
    60 #define SCAN_CHANNEL_WAIT_USEC 200000
    61 
    62 /* Max time to keep scan result. */
    63 #define MAX_KEEP_SCAN_SPAN_SEC 120
    64 
    65 /* Security bit in capability info field. */
    66 #define CAP_SECURITY 0x10
    67 
    68 /* Protocol type used in EAPOL frames. */
    69 #define ETH_TYPE_PAE 0x888E
    70 
    71 /* WPA OUI used in vendor specific IE. */
    72 #define WPA_OUI 0x0050F201
    73 
    74 /* GTK OUI used in vendor specific IE. */
    75 #define GTK_OUI 0x000FAC01
    76 
    77 /* Max PTK key length. */
    78 #define MAX_PTK_LENGTH 64
    79 
    80 /* Max GTK key length. */
    81 #define MAX_GTK_LENGTH 64
    82 
    83 /* KEK offset inside PTK. */
    84 #define KEK_OFFSET 16
    85 
    86 /* TK offset inside PTK. */
    87 #define TK_OFFSET 32
    88 
    89 /* Length of Michael MIC code used in TKIP security suite. */
    90 #define MIC_LENGTH 8
    91 
    92 /*
    93  * Length of data to be encrypted by PRF function:
    94  * NONCE + SNONCE (2 * 32) + DEST_MAC + SOURCE_MAC (2 * ETH_ADDR)
     49/** Timeout in us for waiting to authentication/association response. */
     50#define AUTH_TIMEOUT  200000
     51
     52/** Timeout in us for waiting to finish 4-way handshake process. */
     53#define HANDSHAKE_TIMEOUT  5000000
     54
     55/** Scanning period. */
     56#define SCAN_PERIOD_USEC  35000000
     57
     58/** Time to wait for beacons on channel. */
     59#define SCAN_CHANNEL_WAIT_USEC  200000
     60
     61/** Max time to keep scan result. */
     62#define MAX_KEEP_SCAN_SPAN_SEC  120
     63
     64/** Security bit in capability info field. */
     65#define CAP_SECURITY  0x10
     66
     67/** Protocol type used in EAPOL frames. */
     68#define ETH_TYPE_PAE  0x888e
     69
     70/** WPA OUI used in vendor specific IE. */
     71#define WPA_OUI  0x0050f201
     72
     73/** GTK OUI used in vendor specific IE. */
     74#define GTK_OUI  0x000fac01
     75
     76/** Max PTK key length. */
     77#define MAX_PTK_LENGTH  64
     78
     79/** Max GTK key length. */
     80#define MAX_GTK_LENGTH  64
     81
     82/** KEK offset inside PTK. */
     83#define KEK_OFFSET  16
     84
     85/** TK offset inside PTK. */
     86#define TK_OFFSET  32
     87
     88/** Length of Michael MIC code used in TKIP security suite. */
     89#define MIC_LENGTH  8
     90
     91/** Length of data to be encrypted by PRF function.
     92 *
     93 * NONCE + SNONCE (2 * 32) + DEST_MAC + SOURCE_MAC (2 * ETH_ADDR)
     94 *
    9595 */
    96 #define PRF_CRYPT_DATA_LENGTH 2*32 + 2*ETH_ADDR
    97 
    98 /* Special room in header reserved for encryption. */
     96#define PRF_CRYPT_DATA_LENGTH  (2 * 32 + 2 * ETH_ADDR)
     97
     98/** Special room in header reserved for encryption. */
    9999typedef enum {
    100100        IEEE80211_TKIP_HEADER_LENGTH = 8,
     
    184184/** IEEE 802.11 information element types. */
    185185typedef enum {
    186         IEEE80211_SSID_IE = 0,          /**< Target SSID. */
    187         IEEE80211_RATES_IE = 1,         /**< Supported data rates. */
    188         IEEE80211_CHANNEL_IE = 3,       /**< Current channel number. */
    189         IEEE80211_CHALLENGE_IE = 16,    /**< Challenge text. */
    190         IEEE80211_RSN_IE = 48,          /**< RSN. */
    191         IEEE80211_EXT_RATES_IE = 50,    /**< Extended data rates. */
    192         IEEE80211_VENDOR_IE = 221       /**< Vendor specific IE. */
     186        IEEE80211_SSID_IE = 0,        /**< Target SSID. */
     187        IEEE80211_RATES_IE = 1,       /**< Supported data rates. */
     188        IEEE80211_CHANNEL_IE = 3,     /**< Current channel number. */
     189        IEEE80211_CHALLENGE_IE = 16,  /**< Challenge text. */
     190        IEEE80211_RSN_IE = 48,        /**< RSN. */
     191        IEEE80211_EXT_RATES_IE = 50,  /**< Extended data rates. */
     192        IEEE80211_VENDOR_IE = 221     /**< Vendor specific IE. */
    193193} ieee80211_ie_type_t;
    194194
     
    249249        ieee80211_bssid_info_t bssid_info;
    250250       
    251         /** 
     251        /**
    252252         * Flag indicating that data traffic is encrypted by HW key
    253          * that is set up in device. 
     253         * that is set up in device.
    254254         */
    255255        bool using_hw_key;
     
    290290        uint8_t dest_addr[ETH_ADDR];
    291291        uint8_t src_addr[ETH_ADDR];
    292         uint16_t proto;                 /**< Big Endian value! */
    293 } __attribute__((packed)) __attribute__ ((aligned(2)))
    294         eth_header_t;
     292        uint16_t proto;  /**< Big Endian value! */
     293} __attribute__((packed)) __attribute__((aligned(2)))
     294    eth_header_t;
    295295
    296296/** IEEE 802.11 management header structure. */
    297297typedef struct {
    298         uint16_t frame_ctrl;            /**< Little Endian value! */
    299         uint16_t duration_id;           /**< Little Endian value! */
     298        uint16_t frame_ctrl;          /**< Little Endian value! */
     299        uint16_t duration_id;         /**< Little Endian value! */
    300300        uint8_t dest_addr[ETH_ADDR];
    301301        uint8_t src_addr[ETH_ADDR];
    302302        uint8_t bssid[ETH_ADDR];
    303         uint16_t seq_ctrl;              /**< Little Endian value! */
    304 } __attribute__((packed)) __attribute__ ((aligned(2)))
    305         ieee80211_mgmt_header_t;
     303        uint16_t seq_ctrl;            /**< Little Endian value! */
     304} __attribute__((packed)) __attribute__((aligned(2)))
     305    ieee80211_mgmt_header_t;
    306306
    307307/** IEEE 802.11 data header structure. */
    308308typedef struct {
    309         uint16_t frame_ctrl;            /**< Little Endian value! */
    310         uint16_t duration_id;           /**< Little Endian value! */
     309        uint16_t frame_ctrl;         /**< Little Endian value! */
     310        uint16_t duration_id;        /**< Little Endian value! */
    311311        uint8_t address1[ETH_ADDR];
    312312        uint8_t address2[ETH_ADDR];
    313313        uint8_t address3[ETH_ADDR];
    314         uint16_t seq_ctrl;              /**< Little Endian value! */
    315 } __attribute__((packed)) __attribute__ ((aligned(2)))
    316         ieee80211_data_header_t;
     314        uint16_t seq_ctrl;           /**< Little Endian value! */
     315} __attribute__((packed)) __attribute__((aligned(2)))
     316    ieee80211_data_header_t;
    317317
    318318/** IEEE 802.11 information element header. */
     
    320320        uint8_t element_id;
    321321        uint8_t length;
    322 } __attribute__((packed)) __attribute__ ((aligned(2)))
    323         ieee80211_ie_header_t;
     322} __attribute__((packed)) __attribute__((aligned(2)))
     323    ieee80211_ie_header_t;
    324324
    325325/** IEEE 802.11 authentication frame body. */
    326326typedef struct {
    327         uint16_t auth_alg;              /**< Little Endian value! */
    328         uint16_t auth_trans_no;         /**< Little Endian value! */
    329         uint16_t status;                /**< Little Endian value! */
    330 } __attribute__((packed)) __attribute__ ((aligned(2)))
    331         ieee80211_auth_body_t;
     327        uint16_t auth_alg;       /**< Little Endian value! */
     328        uint16_t auth_trans_no;  /**< Little Endian value! */
     329        uint16_t status;         /**< Little Endian value! */
     330} __attribute__((packed)) __attribute__((aligned(2)))
     331    ieee80211_auth_body_t;
    332332
    333333/** IEEE 802.11 deauthentication frame body. */
    334334typedef struct {
    335         uint16_t reason;                /**< Little Endian value! */
    336 } __attribute__((packed)) __attribute__ ((aligned(2)))
    337         ieee80211_deauth_body_t;
     335        uint16_t reason;    /**< Little Endian value! */
     336} __attribute__((packed)) __attribute__((aligned(2)))
     337    ieee80211_deauth_body_t;
    338338
    339339/** IEEE 802.11 association request frame body. */
    340340typedef struct {
    341         uint16_t capability;            /**< Little Endian value! */
    342         uint16_t listen_interval;       /**< Little Endian value! */
    343 } __attribute__((packed)) __attribute__ ((aligned(2)))
    344         ieee80211_assoc_req_body_t;
     341        uint16_t capability;       /**< Little Endian value! */
     342        uint16_t listen_interval;  /**< Little Endian value! */
     343} __attribute__((packed)) __attribute__((aligned(2)))
     344    ieee80211_assoc_req_body_t;
    345345
    346346/** IEEE 802.11 association response frame body. */
    347347typedef struct {
    348         uint16_t capability;            /**< Little Endian value! */
    349         uint16_t status;                /**< Little Endian value! */
    350         uint16_t aid;                   /**< Little Endian value! */
    351 } __attribute__((packed)) __attribute__ ((aligned(2)))
    352         ieee80211_assoc_resp_body_t;
     348        uint16_t capability;  /**< Little Endian value! */
     349        uint16_t status;      /**< Little Endian value! */
     350        uint16_t aid;         /**< Little Endian value! */
     351} __attribute__((packed)) __attribute__((aligned(2)))
     352    ieee80211_assoc_resp_body_t;
    353353
    354354/** IEEE 802.11 beacon frame body start. */
    355355typedef struct {
    356356        uint8_t timestamp[8];
    357         uint16_t beacon_interval;       /**< Little Endian value! */
    358         uint16_t capability;            /**< Little Endian value! */
    359 } __attribute__((packed)) __attribute__ ((aligned(2)))
    360         ieee80211_beacon_start_t;
     357        uint16_t beacon_interval;  /**< Little Endian value! */
     358        uint16_t capability;       /**< Little Endian value! */
     359} __attribute__((packed)) __attribute__((aligned(2)))
     360    ieee80211_beacon_start_t;
    361361
    362362/** IEEE 802.11i EAPOL-Key frame format. */
     
    364364        uint8_t proto_version;
    365365        uint8_t packet_type;
    366         uint16_t body_length;           /**< Big Endian value! */
     366        uint16_t body_length;      /**< Big Endian value! */
    367367        uint8_t descriptor_type;
    368         uint16_t key_info;              /**< Big Endian value! */
    369         uint16_t key_length;            /**< Big Endian value! */
     368        uint16_t key_info;         /**< Big Endian value! */
     369        uint16_t key_length;       /**< Big Endian value! */
    370370        uint8_t key_replay_counter[8];
    371371        uint8_t key_nonce[32];
     
    374374        uint8_t reserved[8];
    375375        uint8_t key_mic[16];
    376         uint16_t key_data_length;       /**< Big Endian value! */
     376        uint16_t key_data_length;  /**< Big Endian value! */
    377377} __attribute__((packed)) ieee80211_eapol_key_frame_t;
    378378
    379379#define ieee80211_scan_result_list_foreach(results, iter) \
    380     list_foreach((results).list, link, ieee80211_scan_result_link_t, (iter))
    381 
    382 static inline void ieee80211_scan_result_list_init(
    383         ieee80211_scan_result_list_t *results)
     380        list_foreach((results).list, link, ieee80211_scan_result_link_t, (iter))
     381
     382static inline void
     383    ieee80211_scan_result_list_init(ieee80211_scan_result_list_t *results)
    384384{
    385385        list_initialize(&results->list);
     
    387387}
    388388
    389 static inline void ieee80211_scan_result_list_remove(
    390         ieee80211_scan_result_list_t *results,
    391         ieee80211_scan_result_link_t *result)
     389static inline void
     390    ieee80211_scan_result_list_remove(ieee80211_scan_result_list_t *results,
     391    ieee80211_scan_result_link_t *result)
    392392{
    393393        list_remove(&result->link);
     
    395395}
    396396
    397 static inline void ieee80211_scan_result_list_append(
    398         ieee80211_scan_result_list_t *results,
    399         ieee80211_scan_result_link_t *result)
     397static inline void
     398    ieee80211_scan_result_list_append(ieee80211_scan_result_list_t *results,
     399    ieee80211_scan_result_link_t *result)
    400400{
    401401        list_append(&result->link, &results->list);
     
    403403}
    404404
    405 extern bool ieee80211_is_fromds_frame(uint16_t frame_ctrl);
    406 extern bool ieee80211_is_tods_frame(uint16_t frame_ctrl);
    407 extern void ieee80211_set_connect_request(ieee80211_dev_t *ieee80211_dev);
    408 extern bool ieee80211_pending_connect_request(ieee80211_dev_t *ieee80211_dev);
    409 extern ieee80211_auth_phase_t ieee80211_get_auth_phase(ieee80211_dev_t
    410         *ieee80211_dev);
    411 extern void ieee80211_set_auth_phase(ieee80211_dev_t *ieee80211_dev,
    412         ieee80211_auth_phase_t auth_phase);
    413 extern int ieee80211_probe_request(ieee80211_dev_t *ieee80211_dev,
    414         char *ssid);
    415 extern int ieee80211_authenticate(ieee80211_dev_t *ieee80211_dev);
    416 extern int ieee80211_associate(ieee80211_dev_t *ieee80211_dev,
    417         char *password);
    418 extern int ieee80211_deauthenticate(ieee80211_dev_t *ieee80211_dev);
    419 
    420 #endif /* LIBN_IEEE80211_H */
     405extern bool ieee80211_is_fromds_frame(uint16_t);
     406extern bool ieee80211_is_tods_frame(uint16_t);
     407extern void ieee80211_set_connect_request(ieee80211_dev_t *);
     408extern bool ieee80211_pending_connect_request(ieee80211_dev_t *);
     409extern ieee80211_auth_phase_t ieee80211_get_auth_phase(ieee80211_dev_t *);
     410extern void ieee80211_set_auth_phase(ieee80211_dev_t *, ieee80211_auth_phase_t);
     411extern int ieee80211_probe_request(ieee80211_dev_t *, char *);
     412extern int ieee80211_authenticate(ieee80211_dev_t *);
     413extern int ieee80211_associate(ieee80211_dev_t *, char *);
     414extern int ieee80211_deauthenticate(ieee80211_dev_t *);
     415
     416#endif
    421417
    422418/** @}
  • uspace/lib/ieee80211/src/ieee80211.c

    r09044cb r8a64320e  
    3232
    3333/** @file ieee80211.c
    34  * 
     34 *
    3535 * IEEE 802.11 interface implementation.
    3636 */
     
    4141#include <macros.h>
    4242#include <errno.h>
    43 
    4443#include <ieee80211.h>
    4544#include <ieee80211_impl.h>
     
    4847#include <ops/ieee80211.h>
    4948
    50 #define IEEE80211_DATA_RATES_SIZE 8
    51 #define IEEE80211_EXT_DATA_RATES_SIZE 4
     49#define IEEE80211_DATA_RATES_SIZE      8
     50#define IEEE80211_EXT_DATA_RATES_SIZE  4
    5251
    5352#define ATOMIC_GET(state)
    5453
    5554/** Frame encapsulation used in IEEE 802.11. */
    56 static const uint8_t rfc1042_header[] = { 
    57         0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00
     55static const uint8_t rfc1042_header[] = {
     56        0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00
    5857};
    5958
    6059/** Broadcast MAC address. */
    6160static const uint8_t ieee80211_broadcast_mac_addr[] = {
    62         0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
     61        0xff, 0xff, 0xff, 0xff, 0xff, 0xff
    6362};
    6463
    65 /**
    66  * Check data frame.
    67  *
     64/** Check data frame.
     65 *
    6866 * @param frame_ctrl Frame control field in little endian (!).
    69  * 
     67 *
    7068 * @return True if it is data frame, otherwise false.
     69 *
    7170 */
    7271inline bool ieee80211_is_data_frame(uint16_t frame_ctrl)
     
    7473        frame_ctrl = uint16_t_le2host(frame_ctrl);
    7574       
    76         return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_TYPE)
    77                 == IEEE80211_DATA_FRAME;
    78 }
    79 
    80 /**
    81  * Check management frame.
    82  *
     75        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_TYPE) ==
     76            IEEE80211_DATA_FRAME;
     77}
     78
     79/** Check management frame.
     80 *
    8381 * @param frame_ctrl Frame control field in little endian (!).
    84  * 
     82 *
    8583 * @return True if it is management frame, otherwise false.
     84 *
    8685 */
    8786inline bool ieee80211_is_mgmt_frame(uint16_t frame_ctrl)
     
    8988        frame_ctrl = uint16_t_le2host(frame_ctrl);
    9089       
    91         return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_TYPE)
    92                 == IEEE80211_MGMT_FRAME;
    93 }
    94 
    95 /**
    96  * Check management beacon frame.
    97  *
     90        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_TYPE) ==
     91            IEEE80211_MGMT_FRAME;
     92}
     93
     94/** Check management beacon frame.
     95 *
    9896 * @param frame_ctrl Frame control field in little endian (!).
    99  * 
     97 *
    10098 * @return True if it is beacon frame, otherwise false.
     99 *
    101100 */
    102101inline bool ieee80211_is_beacon_frame(uint16_t frame_ctrl)
     
    104103        frame_ctrl = uint16_t_le2host(frame_ctrl);
    105104       
    106         return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE)
    107                 == IEEE80211_MGMT_BEACON_FRAME;
    108 }
    109 
    110 /**
    111  * Check management probe response frame.
    112  *
     105        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
     106            IEEE80211_MGMT_BEACON_FRAME;
     107}
     108
     109/** Check management probe response frame.
     110 *
    113111 * @param frame_ctrl Frame control field in little endian (!).
    114  * 
     112 *
    115113 * @return True if it is probe resp frame, otherwise false.
     114 *
    116115 */
    117116inline bool ieee80211_is_probe_response_frame(uint16_t frame_ctrl)
     
    119118        frame_ctrl = uint16_t_le2host(frame_ctrl);
    120119       
    121         return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE)
    122                 == IEEE80211_MGMT_PROBE_RESP_FRAME;
    123 }
    124 
    125 /**
    126  * Check management authentication frame.
    127  *
     120        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
     121            IEEE80211_MGMT_PROBE_RESP_FRAME;
     122}
     123
     124/** Check management authentication frame.
     125 *
    128126 * @param frame_ctrl Frame control field in little endian (!).
    129  * 
     127 *
    130128 * @return True if it is auth frame, otherwise false.
     129 *
    131130 */
    132131inline bool ieee80211_is_auth_frame(uint16_t frame_ctrl)
     
    134133        frame_ctrl = uint16_t_le2host(frame_ctrl);
    135134       
    136         return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE)
    137                 == IEEE80211_MGMT_AUTH_FRAME;
    138 }
    139 
    140 /**
    141  * Check management association response frame.
    142  *
     135        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
     136            IEEE80211_MGMT_AUTH_FRAME;
     137}
     138
     139/** Check management association response frame.
     140 *
    143141 * @param frame_ctrl Frame control field in little endian (!).
    144  * 
     142 *
    145143 * @return True if it is assoc resp frame, otherwise false.
     144 *
    146145 */
    147146inline bool ieee80211_is_assoc_response_frame(uint16_t frame_ctrl)
     
    149148        frame_ctrl = uint16_t_le2host(frame_ctrl);
    150149       
    151         return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE)
    152                 == IEEE80211_MGMT_ASSOC_RESP_FRAME;
    153 }
    154 
    155 /**
    156  * Check data frame "to distribution system" direction.
    157  *
     150        return (frame_ctrl & IEEE80211_FRAME_CTRL_FRAME_SUBTYPE) ==
     151            IEEE80211_MGMT_ASSOC_RESP_FRAME;
     152}
     153
     154/** Check data frame "to distribution system" direction.
     155 *
    158156 * @param frame_ctrl Frame control field in little endian (!).
    159  * 
     157 *
    160158 * @return True if it is TODS frame, otherwise false.
     159 *
    161160 */
    162161inline bool ieee80211_is_tods_frame(uint16_t frame_ctrl)
     
    167166}
    168167
    169 /**
    170  * Check data frame "from distribution system" direction.
    171  *
     168/** Check data frame "from distribution system" direction.
     169 *
    172170 * @param frame_ctrl Frame control field in little endian (!).
    173  * 
     171 *
    174172 * @return True if it is FROMDS frame, otherwise false.
     173 *
    175174 */
    176175inline bool ieee80211_is_fromds_frame(uint16_t frame_ctrl)
     
    181180}
    182181
    183 /**
    184  * Check if it is data frame containing payload data.
    185  *
     182/** Check if it is data frame containing payload data.
     183 *
    186184 * @param frame_ctrl Frame control field in little endian (!).
    187  * 
     185 *
    188186 * @return True if it has payload data, otherwise false.
     187 *
    189188 */
    190189static inline bool ieee80211_has_data_frame(uint16_t frame_ctrl)
     
    192191        frame_ctrl = uint16_t_le2host(frame_ctrl);
    193192       
    194         return (frame_ctrl & (IEEE80211_FRAME_CTRL_FRAME_TYPE | 0x40))
    195                 == IEEE80211_DATA_FRAME;
    196 }
    197 
    198 /**
    199  * Check if it is encrypted frame.
    200  *
     193        return (frame_ctrl & (IEEE80211_FRAME_CTRL_FRAME_TYPE | 0x40)) ==
     194            IEEE80211_DATA_FRAME;
     195}
     196
     197/** Check if it is encrypted frame.
     198 *
    201199 * @param frame_ctrl Frame control field in little endian (!).
    202  * 
     200 *
    203201 * @return True if the frame is encrypted, otherwise false.
     202 *
    204203 */
    205204static inline bool ieee80211_is_encrypted_frame(uint16_t frame_ctrl)
     
    210209}
    211210
    212 /**
    213  * Check if PAE packet is EAPOL-Key frame.
    214  *
     211/** Check if PAE packet is EAPOL-Key frame.
     212 *
    215213 * @param key_frame Pointer to start of EAPOL frame.
    216  * 
     214 *
    217215 * @return True if it is EAPOL-Key frame, otherwise false.
    218  */
    219 static inline bool ieee80211_is_eapol_key_frame(ieee80211_eapol_key_frame_t
    220         *key_frame)
     216 *
     217 */
     218static inline bool
     219    ieee80211_is_eapol_key_frame(ieee80211_eapol_key_frame_t *key_frame)
    221220{
    222221        return (key_frame->packet_type == IEEE80211_EAPOL_KEY);
    223222}
    224223
    225 
    226 /**
    227  * Generate packet sequence number.
    228  *
     224/** Generate packet sequence number.
     225 *
    229226 * @param ieee80211_dev IEEE 802.11 device.
    230  * 
     227 *
    231228 * @return True if it has payload data, otherwise false.
     229 *
    232230 */
    233231static uint16_t ieee80211_get_sequence_number(ieee80211_dev_t *ieee80211_dev)
     
    235233        uint16_t ret_val = ieee80211_dev->sequence_number;
    236234        ieee80211_dev->sequence_number += (1 << 4);
     235       
    237236        return ret_val;
    238237}
    239238
    240 /**
    241  * Get driver-specific structure for IEEE 802.11 device.
    242  *
     239/** Get driver-specific structure for IEEE 802.11 device.
     240 *
    243241 * @param ieee80211_dev IEEE 802.11 device.
    244  * 
     242 *
    245243 * @return Driver-specific structure.
    246  */
    247 void *ieee80211_get_specific(ieee80211_dev_t* ieee80211_dev)
     244 *
     245 */
     246void *ieee80211_get_specific(ieee80211_dev_t *ieee80211_dev)
    248247{
    249248        return ieee80211_dev->specific;
    250249}
    251250
    252 /**
    253  * Set driver-specific structure for IEEE 802.11 device.
    254  *
     251/** Set driver-specific structure for IEEE 802.11 device.
     252 *
    255253 * @param ieee80211_dev IEEE 802.11 device.
    256  * @param specific Driver-specific structure.
    257  */
    258 void ieee80211_set_specific(ieee80211_dev_t* ieee80211_dev,
    259         void *specific)
     254 * @param specific      Driver-specific structure.
     255 *
     256 */
     257void ieee80211_set_specific(ieee80211_dev_t *ieee80211_dev,
     258    void *specific)
    260259{
    261260        ieee80211_dev->specific = specific;
    262261}
    263262
    264 /**
    265  * Get related DDF device.
    266  *
     263/** Get related DDF device.
     264 *
    267265 * @param ieee80211_dev IEEE 802.11 device.
    268  * 
     266 *
    269267 * @return DDF device.
    270  */
    271 ddf_dev_t *ieee80211_get_ddf_dev(ieee80211_dev_t* ieee80211_dev)
     268 *
     269 */
     270ddf_dev_t *ieee80211_get_ddf_dev(ieee80211_dev_t *ieee80211_dev)
    272271{
    273272        return ieee80211_dev->ddf_dev;
    274273}
    275274
    276 /**
    277  * Query current operating mode of IEEE 802.11 device.
    278  *
     275/** Query current operating mode of IEEE 802.11 device.
     276 *
    279277 * @param ieee80211_dev IEEE 802.11 device.
    280  * 
     278 *
    281279 * @return Current IEEE 802.11 operating mode.
    282  */
    283 ieee80211_operating_mode_t ieee80211_query_current_op_mode(ieee80211_dev_t*
    284         ieee80211_dev)
     280 *
     281 */
     282ieee80211_operating_mode_t
     283    ieee80211_query_current_op_mode(ieee80211_dev_t *ieee80211_dev)
    285284{
    286285        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    291290}
    292291
    293 /**
    294  * Query current frequency of IEEE 802.11 device.
    295  *
     292/** Query current frequency of IEEE 802.11 device.
     293 *
    296294 * @param ieee80211_dev IEEE 802.11 device.
    297  * 
     295 *
    298296 * @return Current device operating frequency.
    299  */
    300 uint16_t ieee80211_query_current_freq(ieee80211_dev_t* ieee80211_dev)
     297 *
     298 */
     299uint16_t ieee80211_query_current_freq(ieee80211_dev_t *ieee80211_dev)
    301300{
    302301        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    307306}
    308307
    309 /**
    310  * Query BSSID the device is connected to.
    311  *
     308/** Query BSSID the device is connected to.
     309 *
    312310 * Note: Expecting locked results_mutex.
    313  * 
     311 *
    314312 * @param ieee80211_dev IEEE 802.11 device.
    315  * @param bssid Pointer to structure where should be stored BSSID.
    316  */
    317 void ieee80211_query_bssid(ieee80211_dev_t* ieee80211_dev,
    318         nic_address_t *bssid)
     313 * @param bssid         Pointer to structure where should be stored BSSID.
     314 *
     315 */
     316void ieee80211_query_bssid(ieee80211_dev_t *ieee80211_dev,
     317    nic_address_t *bssid)
    319318{
    320319        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    321320       
    322         if(bssid) {
    323                 ieee80211_scan_result_link_t *res_link = 
    324                         ieee80211_dev->bssid_info.res_link;
    325                
    326                 if(res_link) {
    327                         memcpy(bssid, &res_link->scan_result.bssid, 
    328                                 sizeof(nic_address_t));
     321        if (bssid) {
     322                ieee80211_scan_result_link_t *res_link =
     323                    ieee80211_dev->bssid_info.res_link;
     324               
     325                if (res_link) {
     326                        memcpy(bssid, &res_link->scan_result.bssid,
     327                            sizeof(nic_address_t));
    329328                } else {
    330329                        nic_address_t broadcast_addr;
    331330                        memcpy(broadcast_addr.address,
    332                                 ieee80211_broadcast_mac_addr,
    333                                 ETH_ADDR);
     331                            ieee80211_broadcast_mac_addr, ETH_ADDR);
    334332                        memcpy(bssid, &broadcast_addr, sizeof(nic_address_t));
    335333                }
     
    339337}
    340338
    341 /**
    342  * Get AID of network we are connected to.
    343  *
     339/** Get AID of network we are connected to.
     340 *
    344341 * @param ieee80211_dev IEEE 802.11 device.
    345  * 
     342 *
    346343 * @return AID.
    347  */
    348 uint16_t ieee80211_get_aid(ieee80211_dev_t* ieee80211_dev)
     344 *
     345 */
     346uint16_t ieee80211_get_aid(ieee80211_dev_t *ieee80211_dev)
    349347{
    350348        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    355353}
    356354
    357 /**
    358  * Get pairwise security suite used for HW encryption.
    359  *
     355/** Get pairwise security suite used for HW encryption.
     356 *
    360357 * @param ieee80211_dev IEEE 802.11 device.
    361  * 
     358 *
    362359 * @return Security suite indicator.
    363  */
    364 int ieee80211_get_pairwise_security(ieee80211_dev_t* ieee80211_dev)
     360 *
     361 */
     362int ieee80211_get_pairwise_security(ieee80211_dev_t *ieee80211_dev)
    365363{
    366364        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    367         ieee80211_scan_result_link_t *auth_link = 
    368                 ieee80211_dev->bssid_info.res_link;
     365        ieee80211_scan_result_link_t *auth_link =
     366            ieee80211_dev->bssid_info.res_link;
    369367        int suite = auth_link->scan_result.security.pair_alg;
    370368        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
     
    373371}
    374372
    375 /**
    376  * Check if IEEE 802.11 device is connected to network.
    377  *
     373/** Check if IEEE 802.11 device is connected to network.
     374 *
    378375 * @param ieee80211_dev IEEE 802.11 device.
    379  * 
     376 *
    380377 * @return True if device is connected to network, otherwise false.
    381  */
    382 bool ieee80211_is_connected(ieee80211_dev_t* ieee80211_dev)
     378 *
     379 */
     380bool ieee80211_is_connected(ieee80211_dev_t *ieee80211_dev)
    383381{
    384382        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    385         bool conn_state = 
    386                 ieee80211_dev->current_auth_phase == IEEE80211_AUTH_CONNECTED;
     383        bool conn_state =
     384            ieee80211_dev->current_auth_phase == IEEE80211_AUTH_CONNECTED;
    387385        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
     386       
    388387        return conn_state;
    389388}
    390389
    391390void ieee80211_set_auth_phase(ieee80211_dev_t *ieee80211_dev,
    392         ieee80211_auth_phase_t auth_phase)
     391    ieee80211_auth_phase_t auth_phase)
    393392{
    394393        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    397396}
    398397
    399 ieee80211_auth_phase_t ieee80211_get_auth_phase(ieee80211_dev_t
    400         *ieee80211_dev)
     398ieee80211_auth_phase_t ieee80211_get_auth_phase(ieee80211_dev_t *ieee80211_dev)
    401399{
    402400        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    403401        ieee80211_auth_phase_t conn_state = ieee80211_dev->current_auth_phase;
    404402        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
     403       
    405404        return conn_state;
    406405}
     
    419418        ieee80211_dev->pending_conn_req = false;
    420419        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
     420       
    421421        return conn_request;
    422422}
    423423
    424 /**
    425  * Report current operating mode for IEEE 802.11 device.
    426  *
     424/** Report current operating mode for IEEE 802.11 device.
     425 *
    427426 * @param ieee80211_dev IEEE 802.11 device.
    428  * @param op_mode Current IEEE 802.11 operating mode.
    429  */
    430 void ieee80211_report_current_op_mode(ieee80211_dev_t* ieee80211_dev,
    431         ieee80211_operating_mode_t op_mode)
     427 * @param op_mode       Current IEEE 802.11 operating mode.
     428 *
     429 */
     430void ieee80211_report_current_op_mode(ieee80211_dev_t *ieee80211_dev,
     431    ieee80211_operating_mode_t op_mode)
    432432{
    433433        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    436436}
    437437
    438 /**
    439  * Report current frequency for IEEE 802.11 device.
    440  *
     438/** Report current frequency for IEEE 802.11 device.
     439 *
    441440 * @param ieee80211_dev IEEE 802.11 device.
    442  * @param freq Current device operating frequency.
    443  */
    444 void ieee80211_report_current_freq(ieee80211_dev_t* ieee80211_dev,
    445         uint16_t freq)
     441 * @param freq          Current device operating frequency.
     442 *
     443 */
     444void ieee80211_report_current_freq(ieee80211_dev_t *ieee80211_dev,
     445    uint16_t freq)
    446446{
    447447        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    450450}
    451451
    452 /**
    453  * Check if IEEE 802.11 device is ready (fully initialized).
    454  *
     452/** Check if IEEE 802.11 device is ready (fully initialized).
     453 *
    455454 * @param ieee80211_dev IEEE 802.11 device.
    456  * 
     455 *
    457456 * @return True if device is ready to work, otherwise false.
    458  */
    459 bool ieee80211_is_ready(ieee80211_dev_t* ieee80211_dev)
     457 *
     458 */
     459bool ieee80211_is_ready(ieee80211_dev_t *ieee80211_dev)
    460460{
    461461        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    466466}
    467467
    468 /**
    469  * Set IEEE 802.11 device to ready state.
    470  *
     468/** Set IEEE 802.11 device to ready state.
     469 *
    471470 * @param ieee80211_dev IEEE 802.11 device.
    472  * @param ready Ready state to be set.
    473  */
    474 void ieee80211_set_ready(ieee80211_dev_t* ieee80211_dev, bool ready)
     471 * @param ready         Ready state to be set.
     472 *
     473 */
     474void ieee80211_set_ready(ieee80211_dev_t *ieee80211_dev, bool ready)
    475475{
    476476        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    479479}
    480480
    481 extern bool ieee80211_query_using_key(ieee80211_dev_t* ieee80211_dev)
     481extern bool ieee80211_query_using_key(ieee80211_dev_t *ieee80211_dev)
    482482{
    483483        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    488488}
    489489
    490 void ieee80211_setup_key_confirm(ieee80211_dev_t* ieee80211_dev,
    491         bool using_key)
     490void ieee80211_setup_key_confirm(ieee80211_dev_t *ieee80211_dev,
     491    bool using_key)
    492492{
    493493        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    502502        ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *) arg;
    503503       
    504         while(true) {
     504        while (true) {
    505505                ieee80211_dev->ops->scan(ieee80211_dev);
    506506                async_usleep(SCAN_PERIOD_USEC);
     
    510510}
    511511
    512 /**
    513  * Implementation of NIC open callback for IEEE 802.11 devices.
    514  *
     512/** Implementation of NIC open callback for IEEE 802.11 devices.
     513 *
    515514 * @param fun NIC function.
    516  * 
     515 *
    517516 * @return EOK if succeed, negative error code otherwise.
     517 *
    518518 */
    519519static int ieee80211_open(ddf_fun_t *fun)
     
    522522        ieee80211_dev_t *ieee80211_dev = nic_get_specific(nic_data);
    523523       
    524         if(ieee80211_dev->started) {
     524        if (ieee80211_dev->started)
    525525                return EOK;
    526         } else {
    527                 ieee80211_dev->started = true;
    528         }
     526       
     527        ieee80211_dev->started = true;
    529528       
    530529        int rc = ieee80211_dev->ops->start(ieee80211_dev);
    531         if(rc != EOK)
     530        if (rc != EOK)
    532531                return rc;
    533532       
    534533        /* Add scanning fibril. */
    535534        fid_t fibril = fibril_create(ieee80211_scan, ieee80211_dev);
    536         if (fibril == 0) {
     535        if (fibril == 0)
    537536                return ENOMEM;
    538         }
     537       
    539538        fibril_add_ready(fibril);
    540539       
     
    542541}
    543542
    544 /**
    545  * Send frame handler.
    546  *
    547  * @param nic Pointer to NIC device.
     543/** Send frame handler.
     544 *
     545 * @param nic  Pointer to NIC device.
    548546 * @param data Data buffer.
    549547 * @param size Data buffer size.
     548 *
    550549 */
    551550static void ieee80211_send_frame(nic_t *nic, void *data, size_t size)
    552551{
    553         ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *) 
    554                 nic_get_specific(nic);
    555        
    556         ieee80211_auth_phase_t auth_phase = 
    557                 ieee80211_get_auth_phase(ieee80211_dev);
    558         if(auth_phase != IEEE80211_AUTH_ASSOCIATED &&
    559                 auth_phase != IEEE80211_AUTH_CONNECTED) {
     552        ieee80211_dev_t *ieee80211_dev = (ieee80211_dev_t *)
     553            nic_get_specific(nic);
     554       
     555        ieee80211_auth_phase_t auth_phase =
     556            ieee80211_get_auth_phase(ieee80211_dev);
     557        if ((auth_phase != IEEE80211_AUTH_ASSOCIATED) &&
     558            (auth_phase != IEEE80211_AUTH_CONNECTED))
    560559                return;
    561         }
    562560       
    563561        ieee80211_scan_result_t *auth_data =
    564                         &ieee80211_dev->bssid_info.res_link->scan_result;
     562            &ieee80211_dev->bssid_info.res_link->scan_result;
    565563       
    566564        /* We drop part of IEEE 802.3 ethernet header. */
    567565        size_t drop_bytes = sizeof(eth_header_t) - 2;
    568566       
    569         size_t complete_size = (size - drop_bytes) + 
    570                 sizeof(ieee80211_data_header_t) +
    571                 ARRAY_SIZE(rfc1042_header);
     567        size_t complete_size = (size - drop_bytes) +
     568            sizeof(ieee80211_data_header_t) +
     569            ARRAY_SIZE(rfc1042_header);
    572570       
    573571        /* Init crypto data. */
     
    579577       
    580578        // TODO: Distinguish used key (pair/group) by dest address ?
    581         if(ieee80211_query_using_key(ieee80211_dev)) {
     579        if (ieee80211_query_using_key(ieee80211_dev)) {
    582580                int sec_suite = auth_data->security.pair_alg;
    583                 switch(sec_suite) {
    584                         case IEEE80211_SECURITY_SUITE_TKIP:
    585                                 head_space = IEEE80211_TKIP_HEADER_LENGTH;
    586                                 mic_space = MIC_LENGTH;
    587                                 add_mic = true;
    588                                 break;
    589                         case IEEE80211_SECURITY_SUITE_CCMP:
    590                                 head_space = IEEE80211_CCMP_HEADER_LENGTH;
    591                                 head_data[3] = 0x20;
    592                                 break;
    593                         default:
    594                                 break;
     581                switch (sec_suite) {
     582                case IEEE80211_SECURITY_SUITE_TKIP:
     583                        head_space = IEEE80211_TKIP_HEADER_LENGTH;
     584                        mic_space = MIC_LENGTH;
     585                        add_mic = true;
     586                        break;
     587                case IEEE80211_SECURITY_SUITE_CCMP:
     588                        head_space = IEEE80211_CCMP_HEADER_LENGTH;
     589                        head_data[3] = 0x20;
     590                        break;
     591                default:
     592                        break;
    595593                }
    596 
     594               
    597595                crypto = uint16_t_le2host(IEEE80211_FRAME_CTRL_PROTECTED);
    598596        }
     
    601599       
    602600        void *complete_buffer = malloc(complete_size);
     601        if (!complete_buffer)
     602                return;
     603       
    603604        memset(complete_buffer, 0, complete_size);
    604605       
    605         if(head_space) {
     606        if (head_space)
    606607                memcpy(complete_buffer + sizeof(ieee80211_data_header_t),
    607                         head_data, head_space);
    608         }
     608                    head_data, head_space);
    609609       
    610610        memcpy(complete_buffer + sizeof(ieee80211_data_header_t) + head_space,
    611                 rfc1042_header,
    612                 ARRAY_SIZE(rfc1042_header));
    613        
    614         memcpy(complete_buffer +
    615                 sizeof(ieee80211_data_header_t) +
    616                 ARRAY_SIZE(rfc1042_header) + head_space,
    617                 data + drop_bytes, size - drop_bytes);
     611            rfc1042_header, ARRAY_SIZE(rfc1042_header));
     612       
     613        memcpy(complete_buffer + sizeof(ieee80211_data_header_t) +
     614            ARRAY_SIZE(rfc1042_header) + head_space,
     615            data + drop_bytes, size - drop_bytes);
    618616       
    619617        ieee80211_data_header_t *data_header =
    620                 (ieee80211_data_header_t *) complete_buffer;
    621         data_header->frame_ctrl = 
    622                 uint16_t_le2host(IEEE80211_DATA_FRAME) |
    623                 uint16_t_le2host(IEEE80211_DATA_DATA_FRAME) |
    624                 uint16_t_le2host(IEEE80211_FRAME_CTRL_TODS) |
    625                 crypto;
     618            (ieee80211_data_header_t *) complete_buffer;
     619        data_header->frame_ctrl =
     620            uint16_t_le2host(IEEE80211_DATA_FRAME) |
     621            uint16_t_le2host(IEEE80211_DATA_DATA_FRAME) |
     622            uint16_t_le2host(IEEE80211_FRAME_CTRL_TODS) |
     623            crypto;
    626624        data_header->seq_ctrl = ieee80211_get_sequence_number(ieee80211_dev);
    627625       
    628626        /* BSSID, SA, DA. */
    629         memcpy(data_header->address1, auth_data->bssid.address, ETH_ADDR);
     627        memcpy(data_header->address1, auth_data->bssid.address, ETH_ADDR);
    630628        memcpy(data_header->address2, data + ETH_ADDR, ETH_ADDR);
    631629        memcpy(data_header->address3, data, ETH_ADDR);
    632630       
    633         if(add_mic) {
     631        if (add_mic) {
    634632                size_t size_wo_mic = complete_size - MIC_LENGTH;
    635633                uint8_t *tx_mic = ieee80211_dev->bssid_info.ptk +
    636                         TK_OFFSET +
    637                         IEEE80211_TKIP_TX_MIC_OFFSET;
     634                    TK_OFFSET + IEEE80211_TKIP_TX_MIC_OFFSET;
    638635                ieee80211_michael_mic(tx_mic, complete_buffer, size_wo_mic,
    639                         complete_buffer + size_wo_mic);
    640         }
    641        
    642         ieee80211_dev->ops->tx_handler(ieee80211_dev,
    643                 complete_buffer,
    644                 complete_size);
     636                    complete_buffer + size_wo_mic);
     637        }
     638       
     639        ieee80211_dev->ops->tx_handler(ieee80211_dev,
     640            complete_buffer, complete_size);
    645641       
    646642        free(complete_buffer);
    647643}
    648644
    649 /**
    650  * Fill out IEEE 802.11 device functions implementations.
    651  *
    652  * @param ieee80211_dev IEEE 802.11 device.
    653  * @param ieee80211_ops Callbacks implementation.
     645/** Fill out IEEE 802.11 device functions implementations.
     646 *
     647 * @param ieee80211_dev   IEEE 802.11 device.
     648 * @param ieee80211_ops   Callbacks implementation.
    654649 * @param ieee80211_iface Interface functions implementation.
    655  * @param nic_iface NIC interface functions implementation.
    656  * @param nic_dev_ops NIC device functions implementation.
    657  *
    658  * @return EINVAL when missing pointer to ieee80211_ops or ieee80211_iface,
    659  * otherwise EOK.
    660  */
    661 static int ieee80211_implement(ieee80211_dev_t *ieee80211_dev,
    662         ieee80211_ops_t *ieee80211_ops, ieee80211_iface_t *ieee80211_iface,
    663         nic_iface_t *nic_iface, ddf_dev_ops_t *nic_dev_ops)
    664 {
    665         if(ieee80211_ops) {
    666                 if(!ieee80211_ops->start)
     650 * @param nic_iface       NIC interface functions implementation.
     651 * @param nic_dev_ops     NIC device functions implementation.
     652 *
     653 * @return EINVAL when missing pointer to ieee80211_ops
     654 *         or ieee80211_iface, otherwise EOK.
     655 *
     656 */
     657static int ieee80211_implement(ieee80211_dev_t *ieee80211_dev,
     658    ieee80211_ops_t *ieee80211_ops, ieee80211_iface_t *ieee80211_iface,
     659    nic_iface_t *nic_iface, ddf_dev_ops_t *nic_dev_ops)
     660{
     661        if (ieee80211_ops) {
     662                if (!ieee80211_ops->start)
    667663                        ieee80211_ops->start = ieee80211_start_impl;
    668 
    669                 if(!ieee80211_ops->tx_handler)
     664               
     665                if (!ieee80211_ops->tx_handler)
    670666                        ieee80211_ops->tx_handler = ieee80211_tx_handler_impl;
    671 
    672                 if(!ieee80211_ops->set_freq)
     667               
     668                if (!ieee80211_ops->set_freq)
    673669                        ieee80211_ops->set_freq = ieee80211_set_freq_impl;
    674670               
    675                 if(!ieee80211_ops->bssid_change)
    676                         ieee80211_ops->bssid_change =
    677                                 ieee80211_bssid_change_impl;
    678                
    679                 if(!ieee80211_ops->key_config)
     671                if (!ieee80211_ops->bssid_change)
     672                        ieee80211_ops->bssid_change = ieee80211_bssid_change_impl;
     673               
     674                if (!ieee80211_ops->key_config)
    680675                        ieee80211_ops->key_config = ieee80211_key_config_impl;
    681676               
    682                 if(!ieee80211_ops->scan)
     677                if (!ieee80211_ops->scan)
    683678                        ieee80211_ops->scan = ieee80211_scan_impl;
    684         } else {
     679        } else
    685680                return EINVAL;
    686         }
    687681       
    688682        ieee80211_dev->ops = ieee80211_ops;
    689683       
    690         if(ieee80211_iface) {
    691                 if(nic_dev_ops)
     684        if (ieee80211_iface) {
     685                if (nic_dev_ops)
    692686                        if (!nic_dev_ops->interfaces[IEEE80211_DEV_IFACE])
    693                                 nic_dev_ops->interfaces[IEEE80211_DEV_IFACE] = 
    694                                         ieee80211_iface;
    695                
    696                 if(!ieee80211_iface->get_scan_results)
    697                         ieee80211_iface->get_scan_results = 
    698                                 ieee80211_get_scan_results_impl;
    699                
    700                 if(!ieee80211_iface->connect)
     687                                nic_dev_ops->interfaces[IEEE80211_DEV_IFACE] =
     688                                    ieee80211_iface;
     689               
     690                if (!ieee80211_iface->get_scan_results)
     691                        ieee80211_iface->get_scan_results =
     692                            ieee80211_get_scan_results_impl;
     693               
     694                if (!ieee80211_iface->connect)
    701695                        ieee80211_iface->connect = ieee80211_connect_impl;
    702696               
    703                 if(!ieee80211_iface->disconnect)
     697                if (!ieee80211_iface->disconnect)
    704698                        ieee80211_iface->disconnect = ieee80211_disconnect_impl;
    705         } else {
     699        } else
    706700                return EINVAL;
    707         }
    708        
    709         if(nic_dev_ops) {
    710                 if(!nic_dev_ops->open)
     701       
     702        if (nic_dev_ops) {
     703                if (!nic_dev_ops->open)
    711704                        nic_dev_ops->open = ieee80211_open;
    712         } else {
     705        } else
    713706                return EINVAL;
    714         }
    715707       
    716708        ieee80211_dev->iface = ieee80211_iface;
     
    721713}
    722714
    723 /**
    724  * Allocate IEEE802.11 device structure.
    725  *
     715/** Allocate IEEE802.11 device structure.
     716 *
    726717 * @return Pointer to allocated IEEE802.11 device structure.
     718 *
    727719 */
    728720ieee80211_dev_t *ieee80211_device_create()
     
    731723}
    732724
    733 /**
    734  * Initialize an IEEE802.11 framework structure.
    735  *
     725/** Initialize an IEEE802.11 framework structure.
     726 *
    736727 * @param ieee80211_dev Device structure to initialize.
    737  * @param ddf_dev Pointer to backing DDF device structure.
    738  * 
     728 * @param ddf_dev       Pointer to backing DDF device structure.
     729 *
    739730 * @return EOK if succeed, negative error code otherwise.
     731 *
    740732 */
    741733int ieee80211_device_init(ieee80211_dev_t *ieee80211_dev, ddf_dev_t *ddf_dev)
     
    748740        ieee80211_dev->current_op_mode = IEEE80211_OPMODE_STATION;
    749741        ieee80211_dev->current_auth_phase = IEEE80211_AUTH_DISCONNECTED;
    750         memcpy(ieee80211_dev->bssid_mask.address, ieee80211_broadcast_mac_addr,
    751                 ETH_ADDR);
     742       
     743        memcpy(ieee80211_dev->bssid_mask.address, ieee80211_broadcast_mac_addr,
     744            ETH_ADDR);
    752745       
    753746        ieee80211_scan_result_list_init(&ieee80211_dev->ap_list);
     
    759752        /* Bind NIC to device */
    760753        nic_t *nic = nic_create_and_bind(ddf_dev);
    761         if (!nic) {
     754        if (!nic)
    762755                return ENOMEM;
    763         }
    764756       
    765757        nic_set_specific(nic, ieee80211_dev);
     
    768760}
    769761
    770 /**
    771  * IEEE802.11 WiFi framework initialization.
    772  *
    773  * @param ieee80211_dev Device structure to initialize.
    774  * @param ieee80211_ops Structure with implemented IEEE802.11 device operations.
    775  * @param ieee80211_iface Structure with implemented IEEE802.11 interface
    776  * operations.
    777  * 
     762/** IEEE802.11 WiFi framework initialization.
     763 *
     764 * @param ieee80211_dev   Device structure to initialize.
     765 * @param ieee80211_ops   Structure with implemented IEEE802.11
     766 *                       device operations.
     767 * @param ieee80211_iface Structure with implemented IEEE802.11
     768 *                        interface operations.
     769 *
    778770 * @return EOK if succeed, negative error code otherwise.
    779  */
    780 int ieee80211_init(ieee80211_dev_t *ieee80211_dev,
    781         ieee80211_ops_t *ieee80211_ops, ieee80211_iface_t *ieee80211_iface,
    782         nic_iface_t *ieee80211_nic_iface, ddf_dev_ops_t *ieee80211_nic_dev_ops)
    783 {
    784         int rc = ieee80211_implement(ieee80211_dev,
    785                 ieee80211_ops, ieee80211_iface,
    786                 ieee80211_nic_iface, ieee80211_nic_dev_ops);
    787         if(rc != EOK) {
     771 *
     772 */
     773int ieee80211_init(ieee80211_dev_t *ieee80211_dev,
     774    ieee80211_ops_t *ieee80211_ops, ieee80211_iface_t *ieee80211_iface,
     775    nic_iface_t *ieee80211_nic_iface, ddf_dev_ops_t *ieee80211_nic_dev_ops)
     776{
     777        int rc = ieee80211_implement(ieee80211_dev,
     778            ieee80211_ops, ieee80211_iface,
     779            ieee80211_nic_iface, ieee80211_nic_dev_ops);
     780        if (rc != EOK)
    788781                return rc;
    789         }
    790782       
    791783        nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
    792784       
    793         /** TODO: Set NIC handlers here. */
     785        /* TODO: Set NIC handlers here. */
    794786        nic_set_send_frame_handler(nic, ieee80211_send_frame);
    795787       
    796         ddf_fun_t *fun = ddf_fun_create(ieee80211_dev->ddf_dev, fun_exposed, 
    797                 "port0");
    798         if (fun == NULL) {
     788        ddf_fun_t *fun = ddf_fun_create(ieee80211_dev->ddf_dev, fun_exposed,
     789            "port0");
     790        if (fun == NULL)
    799791                return EINVAL;
    800         }
    801792       
    802793        nic_set_ddf_fun(nic, fun);
     
    808799                return rc;
    809800        }
     801       
    810802        rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_NIC);
    811803        if (rc != EOK) {
     
    813805                return rc;
    814806        }
     807       
    815808        rc = ddf_fun_add_to_category(fun, DEVICE_CATEGORY_IEEE80211);
    816809        if (rc != EOK) {
     
    822815}
    823816
    824 /**
    825  * Convert frequency value to channel number.
    826  *
     817/** Convert frequency value to channel number.
     818 *
    827819 * @param freq IEEE 802.11 operating frequency.
    828  * 
     820 *
    829821 * @return Operating channel number.
     822 *
    830823 */
    831824static uint8_t ieee80211_freq_to_channel(uint16_t freq)
     
    835828
    836829static void ieee80211_prepare_ie_header(void **ie_header,
    837         uint8_t id, uint8_t length, void *data)
     830    uint8_t id, uint8_t length, void *data)
    838831{
    839832        ieee80211_ie_header_t *header =
    840                 (ieee80211_ie_header_t *) *ie_header;
     833            (ieee80211_ie_header_t *) *ie_header;
    841834       
    842835        header->element_id = id;
     
    845838        memcpy(*ie_header + sizeof(ieee80211_ie_header_t), data, length);
    846839       
    847         *ie_header = (void *) ((void *) header +
    848                 sizeof(ieee80211_ie_header_t) + length);
    849 }
    850 
    851 /**
    852  * Probe request implementation.
    853  *
     840        *ie_header = (void *) ((void *) header +
     841            sizeof(ieee80211_ie_header_t) + length);
     842}
     843
     844/** Probe request implementation.
     845 *
    854846 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    855  * @param ssid Probing SSID or NULL if broadcast.
    856  * 
     847 * @param ssid          Probing SSID or NULL if broadcast.
     848 *
    857849 * @return EOK if succeed, negative error code otherwise.
     850 *
    858851 */
    859852int ieee80211_probe_request(ieee80211_dev_t *ieee80211_dev, char *ssid)
     
    866859        size_t channel_data_size = 1;
    867860       
    868         uint8_t channel =
    869                 ieee80211_freq_to_channel(ieee80211_dev->current_freq);
    870        
    871         /* 4 headers - (ssid, rates, ext rates, current channel) and their data
    872          * lengths.
     861        uint8_t channel =
     862            ieee80211_freq_to_channel(ieee80211_dev->current_freq);
     863       
     864        /*
     865         * 4 headers - (ssid, rates, ext rates, current channel)
     866         * and their data lengths.
    873867         */
    874         size_t payload_size = 
    875                 sizeof(ieee80211_ie_header_t) * 4 +
    876                 ssid_data_size +
    877                 IEEE80211_DATA_RATES_SIZE + IEEE80211_EXT_DATA_RATES_SIZE +
    878                 channel_data_size;
     868        size_t payload_size =
     869            sizeof(ieee80211_ie_header_t) * 4 +
     870            ssid_data_size +
     871            IEEE80211_DATA_RATES_SIZE + IEEE80211_EXT_DATA_RATES_SIZE +
     872            channel_data_size;
    879873       
    880874        size_t buffer_size = sizeof(ieee80211_mgmt_header_t) + payload_size;
    881875        void *buffer = malloc(buffer_size);
     876        if (!buffer)
     877                return ENOMEM;
     878       
    882879        memset(buffer, 0, buffer_size);
    883880       
    884         ieee80211_mgmt_header_t *mgmt_header =
    885                 (ieee80211_mgmt_header_t *) buffer;
    886        
    887         mgmt_header->frame_ctrl = host2uint16_t_le(
    888                 IEEE80211_MGMT_FRAME |
    889                 IEEE80211_MGMT_PROBE_REQ_FRAME
    890                 );
     881        ieee80211_mgmt_header_t *mgmt_header =
     882            (ieee80211_mgmt_header_t *) buffer;
     883       
     884        mgmt_header->frame_ctrl =
     885            host2uint16_t_le(IEEE80211_MGMT_FRAME |
     886            IEEE80211_MGMT_PROBE_REQ_FRAME);
    891887        memcpy(mgmt_header->dest_addr, ieee80211_broadcast_mac_addr, ETH_ADDR);
    892888        memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
    893889        memcpy(mgmt_header->bssid, ieee80211_broadcast_mac_addr, ETH_ADDR);
    894         mgmt_header->seq_ctrl = 
    895                 host2uint16_t_le(ieee80211_get_sequence_number(ieee80211_dev));
     890        mgmt_header->seq_ctrl =
     891            host2uint16_t_le(ieee80211_get_sequence_number(ieee80211_dev));
    896892       
    897893        /* Jump to payload. */
    898894        void *it = (void *) buffer + sizeof(ieee80211_mgmt_header_t);
    899         ieee80211_prepare_ie_header(&it, IEEE80211_SSID_IE, ssid_data_size,
    900                 (void *) ssid);
    901         ieee80211_prepare_ie_header(&it, IEEE80211_RATES_IE,
    902                 IEEE80211_DATA_RATES_SIZE,
    903                 (void *) &ieee80211bg_data_rates);
    904         ieee80211_prepare_ie_header(&it, IEEE80211_EXT_RATES_IE,
    905                 IEEE80211_EXT_DATA_RATES_SIZE,
    906                 (void *) &ieee80211bg_data_rates[IEEE80211_DATA_RATES_SIZE]);
    907         ieee80211_prepare_ie_header(&it, IEEE80211_CHANNEL_IE,
    908                 channel_data_size, (void *) &channel);
     895        ieee80211_prepare_ie_header(&it, IEEE80211_SSID_IE, ssid_data_size,
     896            (void *) ssid);
     897        ieee80211_prepare_ie_header(&it, IEEE80211_RATES_IE,
     898            IEEE80211_DATA_RATES_SIZE, (void *) &ieee80211bg_data_rates);
     899        ieee80211_prepare_ie_header(&it, IEEE80211_EXT_RATES_IE,
     900            IEEE80211_EXT_DATA_RATES_SIZE,
     901            (void *) &ieee80211bg_data_rates[IEEE80211_DATA_RATES_SIZE]);
     902        ieee80211_prepare_ie_header(&it, IEEE80211_CHANNEL_IE,
     903            channel_data_size, (void *) &channel);
    909904       
    910905        ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
     
    915910}
    916911
    917 /**
    918  * IEEE 802.11 authentication implementation.
    919  *
     912/** IEEE 802.11 authentication implementation.
     913 *
    920914 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    921  * 
     915 *
    922916 * @return EOK if succeed, negative error code otherwise.
     917 *
    923918 */
    924919int ieee80211_authenticate(ieee80211_dev_t *ieee80211_dev)
     
    929924       
    930925        ieee80211_scan_result_t *auth_data =
    931                 &ieee80211_dev->bssid_info.res_link->scan_result;
    932        
    933         size_t buffer_size = sizeof(ieee80211_mgmt_header_t) + 
    934                 sizeof(ieee80211_auth_body_t);
     926            &ieee80211_dev->bssid_info.res_link->scan_result;
     927       
     928        size_t buffer_size = sizeof(ieee80211_mgmt_header_t) +
     929            sizeof(ieee80211_auth_body_t);
    935930       
    936931        void *buffer = malloc(buffer_size);
     932        if (!buffer)
     933                return ENOMEM;
     934       
    937935        memset(buffer, 0, buffer_size);
    938936       
    939         ieee80211_mgmt_header_t *mgmt_header =
    940                 (ieee80211_mgmt_header_t *) buffer;
    941        
    942         mgmt_header->frame_ctrl = host2uint16_t_le(
    943                 IEEE80211_MGMT_FRAME |
    944                 IEEE80211_MGMT_AUTH_FRAME
    945                 );
     937        ieee80211_mgmt_header_t *mgmt_header =
     938            (ieee80211_mgmt_header_t *) buffer;
     939       
     940        mgmt_header->frame_ctrl =
     941            host2uint16_t_le(IEEE80211_MGMT_FRAME |
     942            IEEE80211_MGMT_AUTH_FRAME);
    946943        memcpy(mgmt_header->dest_addr, auth_data->bssid.address, ETH_ADDR);
    947944        memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
     
    949946       
    950947        ieee80211_auth_body_t *auth_body =
    951                 (ieee80211_auth_body_t *)
    952                 (buffer + sizeof(ieee80211_mgmt_header_t));
     948            (ieee80211_auth_body_t *)
     949            (buffer + sizeof(ieee80211_mgmt_header_t));
    953950        auth_body->auth_alg = host2uint16_t_le(0);
    954951        auth_body->auth_trans_no = host2uint16_t_le(1);
     
    961958}
    962959
    963 /**
    964  * IEEE 802.11 association implementation.
    965  *
     960/** IEEE 802.11 association implementation.
     961 *
    966962 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    967  * @param password Passphrase to be used in encrypted communication or NULL
    968  * for open networks.
    969  * 
     963 * @param password      Passphrase to be used in encrypted communication
     964 *                      or NULL for open networks.
     965 *
    970966 * @return EOK if succeed, negative error code otherwise.
    971  */
    972 int ieee80211_associate(ieee80211_dev_t *ieee80211_dev, char *password)
     967 *
     968 */
     969int ieee80211_associate(ieee80211_dev_t *ieee80211_dev, char *password)
    973970{
    974971        nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
     
    977974       
    978975        ieee80211_scan_result_link_t *auth_link =
    979                 ieee80211_dev->bssid_info.res_link;
     976            ieee80211_dev->bssid_info.res_link;
    980977       
    981978        ieee80211_scan_result_t *auth_data = &auth_link->scan_result;
     
    983980        size_t ssid_data_size = str_size(auth_data->ssid);
    984981       
    985         size_t payload_size = 
    986                 sizeof(ieee80211_ie_header_t) * 3 +
    987                 ssid_data_size +
    988                 IEEE80211_DATA_RATES_SIZE +
    989                 IEEE80211_EXT_DATA_RATES_SIZE;
    990        
    991         size_t buffer_size = 
    992                 sizeof(ieee80211_mgmt_header_t) +
    993                 sizeof(ieee80211_assoc_req_body_t) +
    994                 payload_size;
    995        
    996         if(auth_data->security.type == IEEE80211_SECURITY_WPA ||
    997                 auth_data->security.type == IEEE80211_SECURITY_WPA2) {
     982        size_t payload_size =
     983            sizeof(ieee80211_ie_header_t) * 3 +
     984            ssid_data_size +
     985            IEEE80211_DATA_RATES_SIZE +
     986            IEEE80211_EXT_DATA_RATES_SIZE;
     987       
     988        size_t buffer_size =
     989            sizeof(ieee80211_mgmt_header_t) +
     990            sizeof(ieee80211_assoc_req_body_t) +
     991            payload_size;
     992       
     993        if ((auth_data->security.type == IEEE80211_SECURITY_WPA) ||
     994            (auth_data->security.type == IEEE80211_SECURITY_WPA2))
    998995                buffer_size += auth_link->auth_ie_len;
    999         }
    1000996       
    1001997        void *buffer = malloc(buffer_size);
     998        if (!buffer)
     999                return ENOMEM;
     1000       
    10021001        memset(buffer, 0, buffer_size);
    10031002       
    1004         ieee80211_mgmt_header_t *mgmt_header =
    1005                 (ieee80211_mgmt_header_t *) buffer;
    1006        
    1007         mgmt_header->frame_ctrl = host2uint16_t_le(
    1008                 IEEE80211_MGMT_FRAME |
    1009                 IEEE80211_MGMT_ASSOC_REQ_FRAME
    1010                 );
     1003        ieee80211_mgmt_header_t *mgmt_header =
     1004            (ieee80211_mgmt_header_t *) buffer;
     1005       
     1006        mgmt_header->frame_ctrl =
     1007            host2uint16_t_le(IEEE80211_MGMT_FRAME |
     1008            IEEE80211_MGMT_ASSOC_REQ_FRAME);
    10111009        memcpy(mgmt_header->dest_addr, auth_data->bssid.address, ETH_ADDR);
    10121010        memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
     
    10141012       
    10151013        ieee80211_assoc_req_body_t *assoc_body =
    1016                 (ieee80211_assoc_req_body_t *)
    1017                 (buffer + sizeof(ieee80211_mgmt_header_t));
     1014            (ieee80211_assoc_req_body_t *)
     1015            (buffer + sizeof(ieee80211_mgmt_header_t));
    10181016        assoc_body->listen_interval = host2uint16_t_le(1);
    10191017       
    10201018        /* Jump to payload. */
    10211019        void *it = buffer + sizeof(ieee80211_mgmt_header_t) +
    1022                 sizeof(ieee80211_assoc_req_body_t);
    1023         ieee80211_prepare_ie_header(&it, IEEE80211_SSID_IE,
    1024                 ssid_data_size, (void *) auth_data->ssid);
    1025         ieee80211_prepare_ie_header(&it, IEEE80211_RATES_IE,
    1026                 IEEE80211_DATA_RATES_SIZE,
    1027                 (void *) &ieee80211bg_data_rates);
    1028         ieee80211_prepare_ie_header(&it, IEEE80211_EXT_RATES_IE,
    1029                 IEEE80211_EXT_DATA_RATES_SIZE,
    1030                 (void *) &ieee80211bg_data_rates[IEEE80211_DATA_RATES_SIZE]);
    1031        
    1032         if(auth_data->security.type != IEEE80211_SECURITY_OPEN) {
     1020            sizeof(ieee80211_assoc_req_body_t);
     1021        ieee80211_prepare_ie_header(&it, IEEE80211_SSID_IE,
     1022            ssid_data_size, (void *) auth_data->ssid);
     1023        ieee80211_prepare_ie_header(&it, IEEE80211_RATES_IE,
     1024            IEEE80211_DATA_RATES_SIZE, (void *) &ieee80211bg_data_rates);
     1025        ieee80211_prepare_ie_header(&it, IEEE80211_EXT_RATES_IE,
     1026            IEEE80211_EXT_DATA_RATES_SIZE,
     1027            (void *) &ieee80211bg_data_rates[IEEE80211_DATA_RATES_SIZE]);
     1028       
     1029        if (auth_data->security.type != IEEE80211_SECURITY_OPEN)
    10331030                assoc_body->capability |= host2uint16_t_le(CAP_SECURITY);
    1034         }
    1035        
    1036         if(auth_data->security.type == IEEE80211_SECURITY_WPA ||
    1037                 auth_data->security.type == IEEE80211_SECURITY_WPA2) {
    1038                 memcpy(it, auth_link->auth_ie,  auth_link->auth_ie_len);
    1039         }
     1031       
     1032        if ((auth_data->security.type == IEEE80211_SECURITY_WPA) ||
     1033            (auth_data->security.type == IEEE80211_SECURITY_WPA2))
     1034                memcpy(it, auth_link->auth_ie, auth_link->auth_ie_len);
    10401035       
    10411036        ieee80211_dev->ops->tx_handler(ieee80211_dev, buffer, buffer_size);
    10421037       
    1043         /* 
    1044          * Save password to be used in eventual authentication handshake. 
     1038        /*
     1039         * Save password to be used in eventual authentication handshake.
    10451040         */
    10461041        memset(ieee80211_dev->bssid_info.password, 0, IEEE80211_MAX_PASSW_LEN);
    1047         memcpy(ieee80211_dev->bssid_info.password, password, 
    1048                 str_size(password));
     1042        memcpy(ieee80211_dev->bssid_info.password, password,
     1043            str_size(password));
    10491044       
    10501045        free(buffer);
     
    10531048}
    10541049
    1055 /**
    1056  * IEEE 802.11 deauthentication implementation.
    1057  *
     1050/** IEEE 802.11 deauthentication implementation.
     1051 *
    10581052 * Note: Expecting locked results_mutex or scan_mutex.
    1059  * 
     1053 *
    10601054 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    1061  * 
     1055 *
    10621056 * @return EOK if succeed, negative error code otherwise.
     1057 *
    10631058 */
    10641059int ieee80211_deauthenticate(ieee80211_dev_t *ieee80211_dev)
    10651060{
    10661061        ieee80211_scan_result_t *auth_data =
    1067                 &ieee80211_dev->bssid_info.res_link->scan_result;
     1062            &ieee80211_dev->bssid_info.res_link->scan_result;
    10681063       
    10691064        nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
     
    10711066        nic_query_address(nic, &nic_address);
    10721067       
    1073         size_t buffer_size = sizeof(ieee80211_mgmt_header_t) +
    1074                 sizeof(ieee80211_deauth_body_t);
     1068        size_t buffer_size = sizeof(ieee80211_mgmt_header_t) +
     1069            sizeof(ieee80211_deauth_body_t);
     1070       
    10751071        void *buffer = malloc(buffer_size);
     1072        if (!buffer)
     1073                return ENOMEM;
     1074       
    10761075        memset(buffer, 0, buffer_size);
    10771076       
    1078         ieee80211_mgmt_header_t *mgmt_header =
    1079                 (ieee80211_mgmt_header_t *) buffer;
    1080        
    1081         mgmt_header->frame_ctrl = host2uint16_t_le(
    1082                 IEEE80211_MGMT_FRAME |
    1083                 IEEE80211_MGMT_DEAUTH_FRAME
    1084                 );
     1077        ieee80211_mgmt_header_t *mgmt_header =
     1078            (ieee80211_mgmt_header_t *) buffer;
     1079       
     1080        mgmt_header->frame_ctrl =
     1081            host2uint16_t_le(IEEE80211_MGMT_FRAME |
     1082            IEEE80211_MGMT_DEAUTH_FRAME);
    10851083        memcpy(mgmt_header->dest_addr, auth_data->bssid.address, ETH_ADDR);
    10861084        memcpy(mgmt_header->src_addr, nic_address.address, ETH_ADDR);
     
    10941092        ieee80211_dev->ops->bssid_change(ieee80211_dev, false);
    10951093       
    1096         if(ieee80211_query_using_key(ieee80211_dev))
     1094        if (ieee80211_query_using_key(ieee80211_dev))
    10971095                ieee80211_dev->ops->key_config(ieee80211_dev, NULL, false);
    10981096       
     
    11031101
    11041102static void ieee80211_process_auth_info(ieee80211_scan_result_link_t *ap_data,
    1105         void *buffer)
     1103    void *buffer)
    11061104{
    11071105        uint8_t *it = (uint8_t *) buffer;
    11081106       
    11091107        uint16_t *version = (uint16_t *) it;
    1110         if(uint16_t_le2host(*version) != 0x1) {
     1108        if (uint16_t_le2host(*version) != 0x1) {
    11111109                ap_data->scan_result.security.type = -1;
    11121110                return;
     
    11151113        it += sizeof(uint16_t);
    11161114       
    1117         uint32_t group_cipher = *(it+3);
    1118         switch(group_cipher) {
    1119                 case IEEE80211_AUTH_CIPHER_TKIP:
    1120                         ap_data->scan_result.security.group_alg =
    1121                                 IEEE80211_SECURITY_SUITE_TKIP;
    1122                         break;
    1123                 case IEEE80211_AUTH_CIPHER_CCMP:
    1124                         ap_data->scan_result.security.group_alg =
    1125                                 IEEE80211_SECURITY_SUITE_CCMP;
    1126                         break;
    1127                 default:
    1128                         ap_data->scan_result.security.group_alg = -1;
    1129         }
    1130        
    1131         it += 4*sizeof(uint8_t);
     1115        uint32_t group_cipher = *(it + 3);
     1116        switch (group_cipher) {
     1117        case IEEE80211_AUTH_CIPHER_TKIP:
     1118                ap_data->scan_result.security.group_alg =
     1119                    IEEE80211_SECURITY_SUITE_TKIP;
     1120                break;
     1121        case IEEE80211_AUTH_CIPHER_CCMP:
     1122                ap_data->scan_result.security.group_alg =
     1123                    IEEE80211_SECURITY_SUITE_CCMP;
     1124                break;
     1125        default:
     1126                ap_data->scan_result.security.group_alg = -1;
     1127        }
     1128       
     1129        it += 4 * sizeof(uint8_t);
    11321130       
    11331131        uint16_t *pairwise_count = (uint16_t *) it;
    1134         uint32_t pairwise_cipher = *(it+sizeof(uint16_t)+3);
    1135         switch(pairwise_cipher) {
    1136                 case IEEE80211_AUTH_CIPHER_TKIP:
    1137                         ap_data->scan_result.security.pair_alg =
    1138                                 IEEE80211_SECURITY_SUITE_TKIP;
    1139                         break;
    1140                 case IEEE80211_AUTH_CIPHER_CCMP:
    1141                         ap_data->scan_result.security.pair_alg =
    1142                                 IEEE80211_SECURITY_SUITE_CCMP;
    1143                         break;
    1144                 default:
    1145                         ap_data->scan_result.security.pair_alg = -1;
    1146         }
    1147        
    1148         it += 2*sizeof(uint16_t) +
    1149                 uint16_t_le2host(*pairwise_count)*sizeof(uint32_t);
    1150        
    1151         uint32_t auth_suite = *(it+3);
    1152         switch(auth_suite) {
    1153                 case IEEE80211_AUTH_AKM_PSK:
    1154                         ap_data->scan_result.security.auth =
    1155                                 IEEE80211_SECURITY_AUTH_PSK;
    1156                         break;
    1157                 case IEEE80211_AUTH_AKM_8021X:
    1158                         ap_data->scan_result.security.auth =
    1159                                 IEEE80211_SECURITY_AUTH_8021X;
    1160                         break;
    1161                 default:
    1162                         ap_data->scan_result.security.auth = -1;
     1132        uint32_t pairwise_cipher = *(it + sizeof(uint16_t) + 3);
     1133        switch (pairwise_cipher) {
     1134        case IEEE80211_AUTH_CIPHER_TKIP:
     1135                ap_data->scan_result.security.pair_alg =
     1136                    IEEE80211_SECURITY_SUITE_TKIP;
     1137                break;
     1138        case IEEE80211_AUTH_CIPHER_CCMP:
     1139                ap_data->scan_result.security.pair_alg =
     1140                    IEEE80211_SECURITY_SUITE_CCMP;
     1141                break;
     1142        default:
     1143                ap_data->scan_result.security.pair_alg = -1;
     1144        }
     1145       
     1146        it += 2 * sizeof(uint16_t) +
     1147            uint16_t_le2host(*pairwise_count) * sizeof(uint32_t);
     1148       
     1149        uint32_t auth_suite = *(it + 3);
     1150        switch (auth_suite) {
     1151        case IEEE80211_AUTH_AKM_PSK:
     1152                ap_data->scan_result.security.auth =
     1153                    IEEE80211_SECURITY_AUTH_PSK;
     1154                break;
     1155        case IEEE80211_AUTH_AKM_8021X:
     1156                ap_data->scan_result.security.auth =
     1157                    IEEE80211_SECURITY_AUTH_8021X;
     1158                break;
     1159        default:
     1160                ap_data->scan_result.security.auth = -1;
    11631161        }
    11641162}
    11651163
    11661164static void copy_auth_ie(ieee80211_ie_header_t *ie_header,
    1167         ieee80211_scan_result_link_t *ap_data, void *it)
    1168 {
    1169         ap_data->auth_ie_len = ie_header->length + 
    1170                 sizeof(ieee80211_ie_header_t);
     1165    ieee80211_scan_result_link_t *ap_data, void *it)
     1166{
     1167        ap_data->auth_ie_len = ie_header->length +
     1168            sizeof(ieee80211_ie_header_t);
    11711169       
    11721170        memcpy(ap_data->auth_ie, it, ap_data->auth_ie_len);
     
    11741172
    11751173static uint8_t *ieee80211_process_ies(ieee80211_dev_t *ieee80211_dev,
    1176         ieee80211_scan_result_link_t *ap_data, void *buffer, size_t buffer_size)
     1174    ieee80211_scan_result_link_t *ap_data, void *buffer, size_t buffer_size)
    11771175{
    11781176        void *it = buffer;
    1179         while((it + sizeof(ieee80211_ie_header_t)) < buffer + buffer_size) {
    1180                 ieee80211_ie_header_t *ie_header = 
    1181                         (ieee80211_ie_header_t *) it;
     1177        while ((it + sizeof(ieee80211_ie_header_t)) < buffer + buffer_size) {
     1178                ieee80211_ie_header_t *ie_header =
     1179                    (ieee80211_ie_header_t *) it;
    11821180                uint8_t *channel;
    11831181                uint32_t oui;
    1184                 switch(ie_header->element_id) {
    1185                         case IEEE80211_CHANNEL_IE:
    1186                                 if(!ap_data)
     1182               
     1183                switch (ie_header->element_id) {
     1184                case IEEE80211_CHANNEL_IE:
     1185                        if (!ap_data)
     1186                                break;
     1187                       
     1188                        channel = (uint8_t *)
     1189                            (it + sizeof(ieee80211_ie_header_t));
     1190                        ap_data->scan_result.channel = *channel;
     1191                        break;
     1192                case IEEE80211_RSN_IE:
     1193                        if (!ap_data)
     1194                                break;
     1195                       
     1196                        ap_data->scan_result.security.type =
     1197                            IEEE80211_SECURITY_WPA2;
     1198                        ieee80211_process_auth_info(ap_data,
     1199                            it + sizeof(ieee80211_ie_header_t));
     1200                        copy_auth_ie(ie_header, ap_data, it);
     1201                        break;
     1202                case IEEE80211_VENDOR_IE:
     1203                        oui = uint32be_from_seq(it +
     1204                            sizeof(ieee80211_ie_header_t));
     1205                       
     1206                        if (oui == WPA_OUI) {
     1207                                if (!ap_data)
    11871208                                        break;
    1188                                 channel = (uint8_t *)
    1189                                         (it + sizeof(ieee80211_ie_header_t));
    1190                                 ap_data->scan_result.channel = *channel;
    1191                                 break;
    1192                         case IEEE80211_RSN_IE:
    1193                                 if(!ap_data)
     1209                               
     1210                                /* Prefering WPA2. */
     1211                                if (ap_data->scan_result.security.type ==
     1212                                    IEEE80211_SECURITY_WPA2)
    11941213                                        break;
     1214                               
    11951215                                ap_data->scan_result.security.type =
    1196                                         IEEE80211_SECURITY_WPA2;
    1197                                 ieee80211_process_auth_info(ap_data,
    1198                                         it + sizeof(ieee80211_ie_header_t));
     1216                                    IEEE80211_SECURITY_WPA;
     1217                               
     1218                                ieee80211_process_auth_info(ap_data,
     1219                                    it + sizeof(ieee80211_ie_header_t) +
     1220                                    sizeof(uint32_t));
    11991221                                copy_auth_ie(ie_header, ap_data, it);
    1200                                 break;
    1201                         case IEEE80211_VENDOR_IE:
    1202                                 oui = uint32be_from_seq(it +
    1203                                         sizeof(ieee80211_ie_header_t));
    1204                                 if(oui == WPA_OUI) {
    1205                                         if(!ap_data)
    1206                                                 break;
    1207                                         /* Prefering WPA2. */
    1208                                         if(ap_data->scan_result.security.type ==
    1209                                                 IEEE80211_SECURITY_WPA2) {
    1210                                                 break;
    1211                                         }
    1212                                         ap_data->scan_result.security.type =
    1213                                                 IEEE80211_SECURITY_WPA;
    1214                                         ieee80211_process_auth_info(ap_data,
    1215                                                 it +
    1216                                                 sizeof(ieee80211_ie_header_t) +
    1217                                                 sizeof(uint32_t));
    1218                                         copy_auth_ie(ie_header, ap_data, it);
    1219                                 } else if(oui == GTK_OUI) {
    1220                                         return it +
    1221                                                 sizeof(ieee80211_ie_header_t) +
    1222                                                 sizeof(uint32_t);
    1223                                 }
     1222                        } else if (oui == GTK_OUI) {
     1223                                return it +
     1224                                    sizeof(ieee80211_ie_header_t) +
     1225                                    sizeof(uint32_t);
     1226                        }
    12241227                }
     1228               
    12251229                it += sizeof(ieee80211_ie_header_t) + ie_header->length;
    12261230        }
     
    12291233}
    12301234
    1231 /**
    1232  * Process probe response and store results.
    1233  *
     1235/** Process probe response and store results.
     1236 *
    12341237 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    1235  * @param mgmt_header Pointer to start of management frame header.
    1236  * 
     1238 * @param mgmt_header   Pointer to start of management frame header.
     1239 *
    12371240 * @return EOK if succeed, negative error code otherwise.
    1238  */
    1239 static int ieee80211_process_probe_response(ieee80211_dev_t *ieee80211_dev,
    1240         ieee80211_mgmt_header_t *mgmt_header, size_t buffer_size)
    1241 {
    1242         ieee80211_beacon_start_t *beacon_body = (ieee80211_beacon_start_t *)
    1243                 ((void *)mgmt_header + sizeof(ieee80211_mgmt_header_t));
    1244        
    1245         ieee80211_ie_header_t *ssid_ie_header = (ieee80211_ie_header_t *)
    1246                 ((void *)beacon_body + sizeof(ieee80211_beacon_start_t));
     1241 *
     1242 */
     1243static int ieee80211_process_probe_response(ieee80211_dev_t *ieee80211_dev,
     1244    ieee80211_mgmt_header_t *mgmt_header, size_t buffer_size)
     1245{
     1246        ieee80211_beacon_start_t *beacon_body = (ieee80211_beacon_start_t *)
     1247            ((void *) mgmt_header + sizeof(ieee80211_mgmt_header_t));
     1248       
     1249        ieee80211_ie_header_t *ssid_ie_header = (ieee80211_ie_header_t *)
     1250            ((void *) beacon_body + sizeof(ieee80211_beacon_start_t));
    12471251       
    12481252        /* Not empty SSID. */
    1249         if(ssid_ie_header->length > 0) {
     1253        if (ssid_ie_header->length > 0) {
    12501254                ieee80211_scan_result_list_t *result_list =
    1251                         &ieee80211_dev->ap_list;
    1252                
    1253                 uint8_t *ssid_start = (uint8_t *) ((void *)ssid_ie_header +
    1254                         sizeof(ieee80211_ie_header_t));
     1255                    &ieee80211_dev->ap_list;
     1256               
     1257                uint8_t *ssid_start = (uint8_t *) ((void *) ssid_ie_header +
     1258                    sizeof(ieee80211_ie_header_t));
    12551259                char ssid[IEEE80211_MAX_SSID_LENGTH];
     1260               
    12561261                memcpy(ssid, ssid_start, ssid_ie_header->length);
    12571262                ssid[ssid_ie_header->length] = '\0';
     
    12591264                /* Check whether SSID is already in results. */
    12601265                ieee80211_scan_result_list_foreach(*result_list, result) {
    1261                         if(!str_cmp(ssid, result->scan_result.ssid)) {
     1266                        if (!str_cmp(ssid, result->scan_result.ssid)) {
    12621267                                result->last_beacon = time(NULL);
    12631268                                return EOK;
     
    12661271               
    12671272                /* Results are full. */
    1268                 if(result_list->size == IEEE80211_MAX_RESULTS_LENGTH - 1) {
     1273                if (result_list->size == IEEE80211_MAX_RESULTS_LENGTH - 1)
    12691274                        return EOK;
    1270                 }
    1271                
    1272                 ieee80211_scan_result_link_t *ap_data =
    1273                         malloc(sizeof(ieee80211_scan_result_link_t));
     1275               
     1276                ieee80211_scan_result_link_t *ap_data =
     1277                    malloc(sizeof(ieee80211_scan_result_link_t));
     1278                if (!ap_data)
     1279                        return ENOMEM;
     1280               
    12741281                memset(ap_data, 0, sizeof(ieee80211_scan_result_link_t));
    12751282                link_initialize(&ap_data->link);
    12761283               
    1277                 memcpy(ap_data->scan_result.bssid.address, 
    1278                         mgmt_header->bssid, ETH_ADDR);
    1279                 memcpy(ap_data->scan_result.ssid, ssid, 
    1280                         ssid_ie_header->length + 1);
    1281                
    1282                 if(uint16_t_le2host(beacon_body->capability) & CAP_SECURITY) {
    1283                         ap_data->scan_result.security.type = 
    1284                                 IEEE80211_SECURITY_WEP;
     1284                memcpy(ap_data->scan_result.bssid.address,
     1285                    mgmt_header->bssid, ETH_ADDR);
     1286                memcpy(ap_data->scan_result.ssid, ssid,
     1287                    ssid_ie_header->length + 1);
     1288               
     1289                if (uint16_t_le2host(beacon_body->capability) & CAP_SECURITY) {
     1290                        ap_data->scan_result.security.type =
     1291                            IEEE80211_SECURITY_WEP;
    12851292                } else {
    1286                         ap_data->scan_result.security.type = 
    1287                                 IEEE80211_SECURITY_OPEN;
     1293                        ap_data->scan_result.security.type =
     1294                            IEEE80211_SECURITY_OPEN;
    12881295                        ap_data->scan_result.security.auth = -1;
    12891296                        ap_data->scan_result.security.pair_alg = -1;
     
    12921299               
    12931300                void *rest_ies_start = ssid_start + ssid_ie_header->length;
    1294                 size_t rest_buffer_size = 
    1295                         buffer_size -
    1296                         sizeof(ieee80211_mgmt_header_t) -
    1297                         sizeof(ieee80211_beacon_start_t) -
    1298                         sizeof(ieee80211_ie_header_t) -
    1299                         ssid_ie_header->length;
    1300                
    1301                 ieee80211_process_ies(ieee80211_dev, ap_data, rest_ies_start, 
    1302                         rest_buffer_size);
     1301                size_t rest_buffer_size =
     1302                    buffer_size -
     1303                    sizeof(ieee80211_mgmt_header_t) -
     1304                    sizeof(ieee80211_beacon_start_t) -
     1305                    sizeof(ieee80211_ie_header_t) -
     1306                    ssid_ie_header->length;
     1307               
     1308                ieee80211_process_ies(ieee80211_dev, ap_data, rest_ies_start,
     1309                    rest_buffer_size);
    13031310               
    13041311                ap_data->last_beacon = time(NULL);
     
    13121319}
    13131320
    1314 /**
    1315  * Process authentication response.
    1316  *
     1321/** Process authentication response.
     1322 *
    13171323 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    1318  * @param mgmt_header Pointer to start of management frame header.
    1319  * 
     1324 * @param mgmt_header   Pointer to start of management frame header.
     1325 *
    13201326 * @return EOK if succeed, negative error code otherwise.
     1327 *
    13211328 */
    13221329static int ieee80211_process_auth_response(ieee80211_dev_t *ieee80211_dev,
    1323         ieee80211_mgmt_header_t *mgmt_header)
     1330    ieee80211_mgmt_header_t *mgmt_header)
    13241331{
    13251332        ieee80211_auth_body_t *auth_body =
    1326                 (ieee80211_auth_body_t *)
    1327                 ((void *)mgmt_header + sizeof(ieee80211_mgmt_header_t));
    1328        
    1329         if(auth_body->status != 0) {
    1330                 ieee80211_set_auth_phase(ieee80211_dev,
    1331                         IEEE80211_AUTH_DISCONNECTED);
    1332         } else {
    1333                 ieee80211_set_auth_phase(ieee80211_dev,
    1334                         IEEE80211_AUTH_AUTHENTICATED);
    1335         }
     1333            (ieee80211_auth_body_t *)
     1334            ((void *) mgmt_header + sizeof(ieee80211_mgmt_header_t));
     1335       
     1336        if (auth_body->status != 0)
     1337                ieee80211_set_auth_phase(ieee80211_dev,
     1338                    IEEE80211_AUTH_DISCONNECTED);
     1339        else
     1340                ieee80211_set_auth_phase(ieee80211_dev,
     1341                    IEEE80211_AUTH_AUTHENTICATED);
    13361342       
    13371343        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
     
    13421348}
    13431349
    1344 /**
    1345  * Process association response.
    1346  *
     1350/** Process association response.
     1351 *
    13471352 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    1348  * @param mgmt_header Pointer to start of management frame header.
    1349  * 
     1353 * @param mgmt_header   Pointer to start of management frame header.
     1354 *
    13501355 * @return EOK if succeed, negative error code otherwise.
     1356 *
    13511357 */
    13521358static int ieee80211_process_assoc_response(ieee80211_dev_t *ieee80211_dev,
    1353         ieee80211_mgmt_header_t *mgmt_header)
     1359    ieee80211_mgmt_header_t *mgmt_header)
    13541360{
    13551361        ieee80211_assoc_resp_body_t *assoc_resp =
    1356                 (ieee80211_assoc_resp_body_t *) ((void *)mgmt_header +
    1357                 sizeof(ieee80211_mgmt_header_t));
    1358        
    1359         if(assoc_resp->status != 0) {
    1360                 ieee80211_set_auth_phase(ieee80211_dev, 
    1361                         IEEE80211_AUTH_DISCONNECTED);
    1362         } else {
    1363                 ieee80211_dev->bssid_info.aid = 
    1364                         uint16_t_le2host(assoc_resp->aid);
    1365                 ieee80211_set_auth_phase(ieee80211_dev, 
    1366                         IEEE80211_AUTH_ASSOCIATED);
     1362            (ieee80211_assoc_resp_body_t *) ((void *) mgmt_header +
     1363            sizeof(ieee80211_mgmt_header_t));
     1364       
     1365        if (assoc_resp->status != 0)
     1366                ieee80211_set_auth_phase(ieee80211_dev,
     1367                    IEEE80211_AUTH_DISCONNECTED);
     1368        else {
     1369                ieee80211_dev->bssid_info.aid =
     1370                    uint16_t_le2host(assoc_resp->aid);
     1371                ieee80211_set_auth_phase(ieee80211_dev,
     1372                    IEEE80211_AUTH_ASSOCIATED);
    13671373                ieee80211_dev->ops->bssid_change(ieee80211_dev, true);
    13681374        }
     
    13761382
    13771383static int ieee80211_process_4way_handshake(ieee80211_dev_t *ieee80211_dev,
    1378         void *buffer, size_t buffer_size)
    1379 {
    1380         ieee80211_eapol_key_frame_t *key_frame = 
    1381                 (ieee80211_eapol_key_frame_t *) buffer;
    1382        
    1383         ieee80211_scan_result_link_t *auth_link = 
    1384                 ieee80211_dev->bssid_info.res_link;
    1385 
     1384    void *buffer, size_t buffer_size)
     1385{
     1386        ieee80211_eapol_key_frame_t *key_frame =
     1387            (ieee80211_eapol_key_frame_t *) buffer;
     1388       
     1389        ieee80211_scan_result_link_t *auth_link =
     1390            ieee80211_dev->bssid_info.res_link;
     1391       
    13861392        ieee80211_scan_result_t *auth_data = &auth_link->scan_result;
    13871393       
    13881394        /* We don't support 802.1X authentication yet. */
    1389         if(auth_data->security.auth == IEEE80211_AUTH_AKM_8021X) {
     1395        if (auth_data->security.auth == IEEE80211_AUTH_AKM_8021X)
    13901396                return ENOTSUP;
    1391         }
    13921397       
    13931398        uint8_t *ptk = ieee80211_dev->bssid_info.ptk;
    13941399        uint8_t *gtk = ieee80211_dev->bssid_info.gtk;
    13951400        uint8_t gtk_id = 1;
    1396 
     1401       
    13971402        bool handshake_done = false;
    13981403       
    1399         bool old_wpa = 
    1400                 auth_data->security.type == IEEE80211_SECURITY_WPA;
     1404        bool old_wpa =
     1405            auth_data->security.type == IEEE80211_SECURITY_WPA;
    14011406       
    14021407        bool key_phase =
    1403                 uint16_t_be2host(key_frame->key_info) &
    1404                 IEEE80211_EAPOL_KEY_KEYINFO_MIC;
    1405        
    1406         bool final_phase = 
    1407                 uint16_t_be2host(key_frame->key_info) &
    1408                 IEEE80211_EAPOL_KEY_KEYINFO_SECURE;
    1409        
    1410         bool ccmp_used = 
    1411                 auth_data->security.pair_alg == IEEE80211_SECURITY_SUITE_CCMP ||
    1412                 auth_data->security.group_alg == IEEE80211_SECURITY_SUITE_CCMP;
     1408            uint16_t_be2host(key_frame->key_info) &
     1409            IEEE80211_EAPOL_KEY_KEYINFO_MIC;
     1410       
     1411        bool final_phase =
     1412            uint16_t_be2host(key_frame->key_info) &
     1413            IEEE80211_EAPOL_KEY_KEYINFO_SECURE;
     1414       
     1415        bool ccmp_used =
     1416            (auth_data->security.pair_alg == IEEE80211_SECURITY_SUITE_CCMP) ||
     1417            (auth_data->security.group_alg == IEEE80211_SECURITY_SUITE_CCMP);
    14131418       
    14141419        size_t ptk_key_length, gtk_key_length;
    14151420        hash_func_t mic_hash;
    1416         if(ccmp_used) {
     1421        if (ccmp_used)
    14171422                mic_hash = HASH_SHA1;
    1418         } else {
     1423        else
    14191424                mic_hash = HASH_MD5;
    1420         }
    1421        
    1422         if(auth_data->security.pair_alg == IEEE80211_SECURITY_SUITE_CCMP) {
     1425       
     1426        if (auth_data->security.pair_alg == IEEE80211_SECURITY_SUITE_CCMP)
    14231427                ptk_key_length = IEEE80211_PTK_CCMP_LENGTH;
    1424         } else {
     1428        else
    14251429                ptk_key_length = IEEE80211_PTK_TKIP_LENGTH;
    1426         }
    1427        
    1428         if(auth_data->security.group_alg == IEEE80211_SECURITY_SUITE_CCMP) {
     1430       
     1431        if (auth_data->security.group_alg == IEEE80211_SECURITY_SUITE_CCMP)
    14291432                gtk_key_length = IEEE80211_GTK_CCMP_LENGTH;
    1430         } else {
     1433        else
    14311434                gtk_key_length = IEEE80211_GTK_TKIP_LENGTH;
    1432         }
    1433        
    1434         size_t output_size =
    1435                 sizeof(eth_header_t) +
    1436                 sizeof(ieee80211_eapol_key_frame_t);
    1437 
    1438         if(!(uint16_t_be2host(key_frame->key_info) &
    1439                 IEEE80211_EAPOL_KEY_KEYINFO_MIC)) {
     1435       
     1436        size_t output_size =
     1437            sizeof(eth_header_t) +
     1438            sizeof(ieee80211_eapol_key_frame_t);
     1439       
     1440        if (!(uint16_t_be2host(key_frame->key_info) &
     1441            IEEE80211_EAPOL_KEY_KEYINFO_MIC))
    14401442                output_size += auth_link->auth_ie_len;
    1441         }
    1442 
     1443       
    14431444        nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
    14441445        nic_address_t nic_address;
    14451446        nic_query_address(nic, &nic_address);
    1446 
     1447       
    14471448        void *output_buffer = malloc(output_size);
     1449        if (!output_buffer)
     1450                return ENOMEM;
     1451       
    14481452        memset(output_buffer, 0, output_size);
    1449 
     1453       
    14501454        /* Setup ethernet header. */
    14511455        eth_header_t *eth_header = (eth_header_t *) output_buffer;
    1452         memcpy(eth_header->dest_addr, auth_data->bssid.address, ETH_ADDR);
     1456        memcpy(eth_header->dest_addr, auth_data->bssid.address, ETH_ADDR);
    14531457        memcpy(eth_header->src_addr, nic_address.address, ETH_ADDR);
    14541458        eth_header->proto = host2uint16_t_be(ETH_TYPE_PAE);
    1455 
     1459       
    14561460        ieee80211_eapol_key_frame_t *output_key_frame =
    1457                 (ieee80211_eapol_key_frame_t *)
    1458                 (output_buffer + sizeof(eth_header_t));
    1459 
     1461            (ieee80211_eapol_key_frame_t *)
     1462            (output_buffer + sizeof(eth_header_t));
     1463       
    14601464        /* Copy content of incoming EAPOL-Key frame. */
    1461         memcpy((void *) output_key_frame, buffer, 
    1462                 sizeof(ieee80211_eapol_key_frame_t));
    1463 
     1465        memcpy((void *) output_key_frame, buffer,
     1466            sizeof(ieee80211_eapol_key_frame_t));
     1467       
    14641468        output_key_frame->proto_version = 0x1;
    14651469        output_key_frame->body_length =
    1466                 host2uint16_t_be(output_size - sizeof(eth_header_t) - 4);
    1467         output_key_frame->key_info &=
    1468                 ~host2uint16_t_be(
    1469                         IEEE80211_EAPOL_KEY_KEYINFO_ACK
    1470                 );
    1471 
    1472         if(key_phase) {
    1473                 output_key_frame->key_info &=
    1474                         ~host2uint16_t_be(
    1475                                 IEEE80211_EAPOL_KEY_KEYINFO_ENCDATA
    1476                         );
    1477                 output_key_frame->key_info &=
    1478                         ~host2uint16_t_be(
    1479                                 IEEE80211_EAPOL_KEY_KEYINFO_INSTALL
    1480                         );
     1470            host2uint16_t_be(output_size - sizeof(eth_header_t) - 4);
     1471        output_key_frame->key_info &=
     1472            ~host2uint16_t_be(IEEE80211_EAPOL_KEY_KEYINFO_ACK);
     1473       
     1474        if (key_phase) {
     1475                output_key_frame->key_info &=
     1476                    ~host2uint16_t_be(IEEE80211_EAPOL_KEY_KEYINFO_ENCDATA);
     1477                output_key_frame->key_info &=
     1478                    ~host2uint16_t_be(IEEE80211_EAPOL_KEY_KEYINFO_INSTALL);
    14811479                output_key_frame->key_data_length = 0;
    14821480                memset(output_key_frame->key_nonce, 0, 32);
     
    14841482                memset(output_key_frame->key_rsc, 0, 8);
    14851483                memset(output_key_frame->eapol_key_iv, 0, 16);
    1486 
     1484               
    14871485                /* Derive GTK and save it. */
    1488                 if(final_phase) {
    1489                         uint16_t key_data_length = 
    1490                                 uint16_t_be2host(key_frame->key_data_length);
     1486                if (final_phase) {
     1487                        uint16_t key_data_length =
     1488                            uint16_t_be2host(key_frame->key_data_length);
    14911489                        uint8_t key_data[key_data_length];
    1492                         uint8_t *data_ptr = (uint8_t *) (buffer +
    1493                                 sizeof(ieee80211_eapol_key_frame_t));
    1494 
     1490                        uint8_t *data_ptr = (uint8_t *)
     1491                            (buffer + sizeof(ieee80211_eapol_key_frame_t));
     1492                       
    14951493                        int rc;
    14961494                        uint8_t work_key[32];
    1497                
    1498                         if(ccmp_used) {
    1499                                 rc = ieee80211_aes_key_unwrap(ptk + KEK_OFFSET, 
    1500                                         data_ptr, key_data_length, key_data);
     1495                       
     1496                        if (ccmp_used) {
     1497                                rc = ieee80211_aes_key_unwrap(ptk + KEK_OFFSET,
     1498                                    data_ptr, key_data_length, key_data);
    15011499                        } else {
    15021500                                memcpy(work_key, key_frame->eapol_key_iv, 16);
    15031501                                memcpy(work_key + 16, ptk + KEK_OFFSET, 16);
    1504                                 rc = ieee80211_rc4_key_unwrap(work_key, 
    1505                                         data_ptr, key_data_length, key_data);
     1502                                rc = ieee80211_rc4_key_unwrap(work_key,
     1503                                    data_ptr, key_data_length, key_data);
    15061504                        }
    15071505                       
    1508                         if(rc == EOK) {
     1506                        if (rc == EOK) {
    15091507                                uint8_t *key_data_ptr = old_wpa ? key_data :
    1510                                         ieee80211_process_ies(ieee80211_dev,
    1511                                         NULL, key_data, key_data_length);
    1512 
    1513                                 if(key_data_ptr) {
     1508                                    ieee80211_process_ies(ieee80211_dev,
     1509                                    NULL, key_data, key_data_length);
     1510                               
     1511                                if (key_data_ptr) {
    15141512                                        uint8_t *key_ptr;
    1515                                         if(old_wpa) {
     1513                                       
     1514                                        if (old_wpa)
    15161515                                                key_ptr = key_data_ptr;
    1517                                         } else {
     1516                                        else {
    15181517                                                gtk_id = *key_data_ptr & 0x3;
    15191518                                                key_ptr = key_data_ptr + 2;
    15201519                                        }
    1521                                                
     1520                                       
    15221521                                        memcpy(gtk, key_ptr, gtk_key_length);
    15231522                                        handshake_done = true;
     
    15261525                }
    15271526        } else {
    1528                 output_key_frame->key_info |=
    1529                         host2uint16_t_be(
    1530                                 IEEE80211_EAPOL_KEY_KEYINFO_MIC
    1531                         );
     1527                output_key_frame->key_info |=
     1528                    host2uint16_t_be(IEEE80211_EAPOL_KEY_KEYINFO_MIC);
    15321529                output_key_frame->key_data_length =
    1533                         host2uint16_t_be(auth_link->auth_ie_len);
    1534                 memcpy((void *)output_key_frame +
    1535                         sizeof(ieee80211_eapol_key_frame_t),
    1536                         auth_link->auth_ie,
    1537                         auth_link->auth_ie_len);
    1538 
     1530                    host2uint16_t_be(auth_link->auth_ie_len);
     1531                memcpy((void *) output_key_frame +
     1532                    sizeof(ieee80211_eapol_key_frame_t),
     1533                    auth_link->auth_ie, auth_link->auth_ie_len);
     1534               
    15391535                /* Compute PMK. */
    15401536                uint8_t pmk[PBKDF2_KEY_LENGTH];
    15411537                pbkdf2((uint8_t *) ieee80211_dev->bssid_info.password,
    1542                         str_size(ieee80211_dev->bssid_info.password),
    1543                         (uint8_t *) auth_data->ssid,
    1544                         str_size(auth_data->ssid), pmk);
    1545 
     1538                    str_size(ieee80211_dev->bssid_info.password),
     1539                    (uint8_t *) auth_data->ssid,
     1540                    str_size(auth_data->ssid), pmk);
     1541               
    15461542                uint8_t *anonce = key_frame->key_nonce;
    1547 
     1543               
    15481544                /* Generate SNONCE. */
    15491545                uint8_t snonce[32];
    15501546                rnd_sequence(snonce, 32);
    1551 
     1547               
    15521548                memcpy(output_key_frame->key_nonce, snonce, 32);
    1553 
     1549               
    15541550                uint8_t *dest_addr = eth_header->dest_addr;
    15551551                uint8_t *src_addr = eth_header->src_addr;
    1556 
     1552               
    15571553                /* Derive PTK and save it. */
    15581554                uint8_t crypt_data[PRF_CRYPT_DATA_LENGTH];
    1559                 memcpy(crypt_data,
    1560                         min_sequence(dest_addr, src_addr, ETH_ADDR),
    1561                         ETH_ADDR);
    1562                 memcpy(crypt_data + ETH_ADDR,
    1563                         max_sequence(dest_addr, src_addr, ETH_ADDR),
    1564                         ETH_ADDR);
    1565                 memcpy(crypt_data + 2*ETH_ADDR,
    1566                         min_sequence(anonce, snonce, 32),
    1567                         32);
    1568                 memcpy(crypt_data + 2*ETH_ADDR + 32,
    1569                         max_sequence(anonce, snonce, 32),
    1570                         32);
     1555                memcpy(crypt_data,
     1556                    min_sequence(dest_addr, src_addr, ETH_ADDR), ETH_ADDR);
     1557                memcpy(crypt_data + ETH_ADDR,
     1558                    max_sequence(dest_addr, src_addr, ETH_ADDR), ETH_ADDR);
     1559                memcpy(crypt_data + 2*ETH_ADDR,
     1560                    min_sequence(anonce, snonce, 32), 32);
     1561                memcpy(crypt_data + 2*ETH_ADDR + 32,
     1562                    max_sequence(anonce, snonce, 32), 32);
    15711563                ieee80211_prf(pmk, crypt_data, ptk, ptk_key_length);
    15721564        }
    1573 
     1565       
    15741566        /* Compute MIC of key frame data from KCK part of PTK. */
    15751567        uint8_t mic[mic_hash];
    1576         hmac(ptk, 16, (uint8_t *) output_key_frame, 
    1577                 output_size - sizeof(eth_header_t), mic, mic_hash);
    1578 
     1568        hmac(ptk, 16, (uint8_t *) output_key_frame,
     1569            output_size - sizeof(eth_header_t), mic, mic_hash);
     1570       
    15791571        memcpy(output_key_frame->key_mic, mic, 16);
    1580 
     1572       
    15811573        ieee80211_send_frame(nic, output_buffer, output_size);
    1582 
     1574       
    15831575        free(output_buffer);
    1584 
     1576       
    15851577        ieee80211_key_config_t key_config;
    15861578       
    15871579        /* Insert Pairwise key. */
    1588         if((key_phase && old_wpa) || (final_phase && !old_wpa)) {
     1580        if ((key_phase && old_wpa) || (final_phase && !old_wpa)) {
    15891581                key_config.suite = auth_data->security.pair_alg;
    15901582                key_config.flags =
    1591                         IEEE80211_KEY_FLAG_TYPE_PAIRWISE;
    1592                 memcpy(key_config.data,
    1593                         ptk + TK_OFFSET,
    1594                         ptk_key_length - TK_OFFSET);
    1595 
     1583                    IEEE80211_KEY_FLAG_TYPE_PAIRWISE;
     1584                memcpy(key_config.data,
     1585                    ptk + TK_OFFSET, ptk_key_length - TK_OFFSET);
     1586               
    15961587                ieee80211_dev->ops->key_config(ieee80211_dev,
    1597                         &key_config, true);
     1588                    &key_config, true);
    15981589        }
    15991590       
    16001591        /* Insert Group key. */
    1601         if(final_phase) {
     1592        if (final_phase) {
    16021593                key_config.id = gtk_id;
    16031594                key_config.suite = auth_data->security.group_alg;
    1604                 key_config.flags =
    1605                         IEEE80211_KEY_FLAG_TYPE_GROUP;
     1595                key_config.flags = IEEE80211_KEY_FLAG_TYPE_GROUP;
    16061596                memcpy(key_config.data, gtk, gtk_key_length);
    1607 
     1597               
    16081598                ieee80211_dev->ops->key_config(ieee80211_dev,
    1609                         &key_config, true);
    1610         }
    1611 
     1599                    &key_config, true);
     1600        }
     1601       
    16121602        /* Signal successful handshake completion. */
    1613         if(handshake_done) {
     1603        if (handshake_done) {
    16141604                fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    16151605                fibril_condvar_signal(&ieee80211_dev->gen_cond);
     
    16211611
    16221612static int ieee80211_process_eapol_frame(ieee80211_dev_t *ieee80211_dev,
    1623         void *buffer, size_t buffer_size)
    1624 {
    1625         ieee80211_eapol_key_frame_t *key_frame =
    1626                 (ieee80211_eapol_key_frame_t *) buffer;
    1627         if(ieee80211_is_eapol_key_frame(key_frame)) {
     1613    void *buffer, size_t buffer_size)
     1614{
     1615        ieee80211_eapol_key_frame_t *key_frame =
     1616            (ieee80211_eapol_key_frame_t *) buffer;
     1617       
     1618        if (ieee80211_is_eapol_key_frame(key_frame))
    16281619                return ieee80211_process_4way_handshake(ieee80211_dev, buffer,
    1629                         buffer_size);
    1630         }
     1620                    buffer_size);
    16311621       
    16321622        return EOK;
    16331623}
    16341624
    1635 /**
    1636  * Process data frame.
    1637  *
     1625/** Process data frame.
     1626 *
    16381627 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    1639  * @param buffer Data buffer starting with IEEE 802.11 data header.
    1640  * @param buffer_size Size of buffer.
    1641  * 
     1628 * @param buffer        Data buffer starting with IEEE 802.11 data header.
     1629 * @param buffer_size   Size of buffer.
     1630 *
    16421631 * @return EOK if succeed, negative error code otherwise.
    1643  */
    1644 static int ieee80211_process_data(ieee80211_dev_t *ieee80211_dev,
    1645         void *buffer, size_t buffer_size)
    1646 {
    1647         ieee80211_data_header_t *data_header =
    1648                 (ieee80211_data_header_t *) buffer;
    1649        
    1650         if(ieee80211_has_data_frame(data_header->frame_ctrl)) {
     1632 *
     1633 */
     1634static int ieee80211_process_data(ieee80211_dev_t *ieee80211_dev,
     1635    void *buffer, size_t buffer_size)
     1636{
     1637        ieee80211_data_header_t *data_header =
     1638            (ieee80211_data_header_t *) buffer;
     1639       
     1640        if (ieee80211_has_data_frame(data_header->frame_ctrl)) {
    16511641                nic_t *nic = nic_get_from_ddf_dev(ieee80211_dev->ddf_dev);
    1652                 size_t strip_length = sizeof(ieee80211_data_header_t) + 
    1653                         ARRAY_SIZE(rfc1042_header);
     1642                size_t strip_length = sizeof(ieee80211_data_header_t) +
     1643                    ARRAY_SIZE(rfc1042_header);
    16541644               
    16551645                /* TODO: Different by used security alg. */
    16561646                /* TODO: Trim frame by used security alg. */
    16571647                // TODO: Distinguish used key (pair/group) by dest address ?
    1658                 if(ieee80211_is_encrypted_frame(data_header->frame_ctrl)) {
     1648                if (ieee80211_is_encrypted_frame(data_header->frame_ctrl))
    16591649                        strip_length += 8;
    1660                 }
    16611650               
    16621651                /* Process 4-way authentication handshake. */
    16631652                uint16_t *proto = (uint16_t *) (buffer + strip_length);
    1664                 if(uint16_t_be2host(*proto) == ETH_TYPE_PAE) {
     1653                if (uint16_t_be2host(*proto) == ETH_TYPE_PAE)
    16651654                        return ieee80211_process_eapol_frame(ieee80211_dev,
    1666                                 buffer + strip_length + sizeof(uint16_t),
    1667                                 buffer_size - strip_length - sizeof(uint16_t));
    1668                 }
    1669                
    1670                 /* Note: ETH protocol ID is already there, so we don't create
    1671                  * whole ETH header. */
    1672                 size_t frame_size =
    1673                         buffer_size - strip_length + sizeof(eth_header_t)-2;
     1655                            buffer + strip_length + sizeof(uint16_t),
     1656                            buffer_size - strip_length - sizeof(uint16_t));
     1657               
     1658                /*
     1659                 * Note: ETH protocol ID is already there, so we don't create
     1660                 * whole ETH header.
     1661                 */
     1662                size_t frame_size =
     1663                    buffer_size - strip_length + sizeof(eth_header_t) - 2;
    16741664                nic_frame_t *frame = nic_alloc_frame(nic, frame_size);
    1675 
    1676                 if(frame == NULL) {
     1665               
     1666                if(frame == NULL)
    16771667                        return ENOMEM;
    1678                 }
    1679 
    1680                 uint8_t *src_addr =
    1681                         ieee80211_is_fromds_frame(data_header->frame_ctrl) ?
    1682                                 data_header->address3 : data_header->address2;
    1683                 uint8_t *dest_addr =
    1684                         ieee80211_is_tods_frame(data_header->frame_ctrl) ?
    1685                                 data_header->address3 : data_header->address1;
    1686 
    1687                 eth_header_t *eth_header =
    1688                         (eth_header_t *) frame->data;
     1668               
     1669                uint8_t *src_addr =
     1670                    ieee80211_is_fromds_frame(data_header->frame_ctrl) ?
     1671                    data_header->address3 : data_header->address2;
     1672                uint8_t *dest_addr =
     1673                    ieee80211_is_tods_frame(data_header->frame_ctrl) ?
     1674                    data_header->address3 : data_header->address1;
     1675               
     1676                eth_header_t *eth_header = (eth_header_t *) frame->data;
    16891677                memcpy(eth_header->src_addr, src_addr, ETH_ADDR);
    16901678                memcpy(eth_header->dest_addr, dest_addr, ETH_ADDR);
    1691 
    1692                 memcpy(frame->data + sizeof(eth_header_t)-2,
    1693                         buffer + strip_length,
    1694                         buffer_size - strip_length);
    1695 
     1679               
     1680                memcpy(frame->data + sizeof(eth_header_t) - 2,
     1681                    buffer + strip_length, buffer_size - strip_length);
     1682               
    16961683                nic_received_frame(nic, frame);
    16971684        }
     
    17001687}
    17011688
    1702 /**
    1703  * IEEE 802.11 RX frames handler.
    1704  *
     1689/** IEEE 802.11 RX frames handler.
     1690 *
    17051691 * @param ieee80211_dev Pointer to IEEE 802.11 device structure.
    1706  * @param buffer Buffer with data.
    1707  * @param buffer_size Size of buffer.
    1708  * 
     1692 * @param buffer        Buffer with data.
     1693 * @param buffer_size   Size of buffer.
     1694 *
    17091695 * @return EOK if succeed, negative error code otherwise.
     1696 *
    17101697 */
    17111698int ieee80211_rx_handler(ieee80211_dev_t *ieee80211_dev, void *buffer,
    1712         size_t buffer_size)
     1699    size_t buffer_size)
    17131700{
    17141701        uint16_t frame_ctrl = *((uint16_t *) buffer);
    1715         if(ieee80211_is_mgmt_frame(frame_ctrl)) {
     1702       
     1703        if (ieee80211_is_mgmt_frame(frame_ctrl)) {
    17161704                ieee80211_mgmt_header_t *mgmt_header =
    1717                         (ieee80211_mgmt_header_t *) buffer;
    1718                
    1719                 if(ieee80211_is_probe_response_frame(mgmt_header->frame_ctrl) ||
    1720                         ieee80211_is_beacon_frame(mgmt_header->frame_ctrl)) {
     1705                    (ieee80211_mgmt_header_t *) buffer;
     1706               
     1707                if ((ieee80211_is_probe_response_frame(mgmt_header->frame_ctrl)) ||
     1708                    (ieee80211_is_beacon_frame(mgmt_header->frame_ctrl)))
    17211709                        return ieee80211_process_probe_response(ieee80211_dev,
    1722                                 mgmt_header, buffer_size);
    1723                 }
    1724                
    1725                 if(ieee80211_is_auth_frame(mgmt_header->frame_ctrl)) {
     1710                            mgmt_header, buffer_size);
     1711               
     1712                if (ieee80211_is_auth_frame(mgmt_header->frame_ctrl))
    17261713                        return ieee80211_process_auth_response(ieee80211_dev,
    1727                                 mgmt_header);
    1728                 }
    1729                
    1730                 if(ieee80211_is_assoc_response_frame(mgmt_header->frame_ctrl)) {
     1714                            mgmt_header);
     1715               
     1716                if (ieee80211_is_assoc_response_frame(mgmt_header->frame_ctrl))
    17311717                        return ieee80211_process_assoc_response(ieee80211_dev,
    1732                                 mgmt_header);
    1733                 }
    1734         } else if(ieee80211_is_data_frame(frame_ctrl)) {
    1735                 return ieee80211_process_data(ieee80211_dev, buffer,
    1736                         buffer_size);
    1737         }
     1718                            mgmt_header);
     1719        } else if (ieee80211_is_data_frame(frame_ctrl))
     1720                return ieee80211_process_data(ieee80211_dev, buffer,
     1721                    buffer_size);
    17381722       
    17391723        return EOK;
  • uspace/lib/ieee80211/src/ieee80211_iface_impl.c

    r09044cb r8a64320e  
    2727 */
    2828
     29/** @addtogroup libieee80211
     30 * @{
     31 */
     32
     33/** @file ieee80211_iface_impl.c
     34 *
     35 * IEEE 802.11 default interface functions implementation.
     36 */
     37
    2938#include <str.h>
    3039#include <errno.h>
    31 
    3240#include <ieee80211_private.h>
    3341#include <ieee80211_iface_impl.h>
    3442
    35 /** @addtogroup libieee80211
    36  * @{
    37  */
    38 
    39 /** @file ieee80211_iface_impl.c
    40  *
    41  * IEEE 802.11 default interface functions implementation.
    42  */
    43 
    44 /**
    45  * Implementation of fetching scan results.
    46  *
    47  * @param fun Device function.
     43/** Implementation of fetching scan results.
     44 *
     45 * @param fun     Device function.
    4846 * @param results Structure where should be stored scan results.
    49  *
    50  * @return EOK if everything went OK, EREFUSED when device is not ready yet.
    51  */
    52 int ieee80211_get_scan_results_impl(ddf_fun_t *fun,
    53         ieee80211_scan_results_t *results, bool now)
     47 *
     48 * @return EOK if everything went OK,
     49 *         EREFUSED when device is not ready yet.
     50 *
     51 */
     52int ieee80211_get_scan_results_impl(ddf_fun_t *fun,
     53    ieee80211_scan_results_t *results, bool now)
    5454{
    5555        nic_t *nic_data = nic_get_from_ddf_fun(fun);
    5656        ieee80211_dev_t *ieee80211_dev = nic_get_specific(nic_data);
    5757       
    58         if(!ieee80211_is_ready(ieee80211_dev))
     58        if (!ieee80211_is_ready(ieee80211_dev))
    5959                return EREFUSED;
    6060       
    61         if(now) {
     61        if (now)
    6262                ieee80211_dev->ops->scan(ieee80211_dev);
    63         }
    6463       
    6564        fibril_mutex_lock(&ieee80211_dev->ap_list.results_mutex);
    66         if(results) {
     65       
     66        if (results) {
    6767                ieee80211_scan_result_list_t *result_list =
    68                         &ieee80211_dev->ap_list;
    69                
    70                 int i = 0;
     68                    &ieee80211_dev->ap_list;
     69               
     70                unsigned int i = 0;
    7171                ieee80211_scan_result_list_foreach(*result_list, result) {
    72                         memcpy(&results->results[i], 
    73                                 &result->scan_result,
    74                                 sizeof(ieee80211_scan_result_t));
     72                        memcpy(&results->results[i],
     73                            &result->scan_result,
     74                            sizeof(ieee80211_scan_result_t));
    7575                        i++;
    7676                }
     
    7878                results->length = i;
    7979        }
     80       
    8081        fibril_mutex_unlock(&ieee80211_dev->ap_list.results_mutex);
    8182       
     
    8889}
    8990
    90 /**
    91  * Working procedure of connect function.
    92  *
     91/** Working procedure of connect function.
     92 *
    9393 * @param ieee80211_dev Pointer to IEEE 802.11 device.
    94  * @param auth_data Selected AP data we want to connect to.
    95  *
    96  * @return EOK if everything OK, ETIMEOUT when timeout during authenticating.
     94 * @param auth_data     Selected AP data we want to connect to.
     95 *
     96 * @return EOK if everything OK,
     97 *         ETIMEOUT when timeout during authenticating.
     98 *
    9799 */
    98100static int ieee80211_connect_proc(ieee80211_dev_t *ieee80211_dev,
    99         ieee80211_scan_result_link_t *auth_data, char *password)
     101    ieee80211_scan_result_link_t *auth_data, char *password)
    100102{
    101103        ieee80211_dev->bssid_info.res_link = auth_data;
    102104       
    103105        /* Set channel. */
    104         int rc = ieee80211_dev->ops->set_freq(ieee80211_dev, 
    105                 ieee80211_channel_to_freq(auth_data->scan_result.channel));
    106         if(rc != EOK)
     106        int rc = ieee80211_dev->ops->set_freq(ieee80211_dev,
     107            ieee80211_channel_to_freq(auth_data->scan_result.channel));
     108        if (rc != EOK)
    107109                return rc;
    108110       
    109111        /* Try to authenticate. */
    110112        ieee80211_authenticate(ieee80211_dev);
     113       
    111114        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    112115        rc = fibril_condvar_wait_timeout(&ieee80211_dev->gen_cond,
    113                         &ieee80211_dev->gen_mutex,
    114                         AUTH_TIMEOUT);
     116            &ieee80211_dev->gen_mutex, AUTH_TIMEOUT);
    115117        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    116         if(rc != EOK)
     118       
     119        if (rc != EOK)
    117120                return rc;
    118         if(ieee80211_get_auth_phase(ieee80211_dev) !=
    119                 IEEE80211_AUTH_AUTHENTICATED) {
    120                 ieee80211_set_auth_phase(ieee80211_dev,
    121                         IEEE80211_AUTH_DISCONNECTED);
     121       
     122        if (ieee80211_get_auth_phase(ieee80211_dev) !=
     123            IEEE80211_AUTH_AUTHENTICATED) {
     124                ieee80211_set_auth_phase(ieee80211_dev,
     125                    IEEE80211_AUTH_DISCONNECTED);
    122126                return EINVAL;
    123127        }
     
    125129        /* Try to associate. */
    126130        ieee80211_associate(ieee80211_dev, password);
     131       
    127132        fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    128133        rc = fibril_condvar_wait_timeout(&ieee80211_dev->gen_cond,
    129                         &ieee80211_dev->gen_mutex,
    130                         AUTH_TIMEOUT);
     134            &ieee80211_dev->gen_mutex, AUTH_TIMEOUT);
    131135        fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    132         if(rc != EOK)
     136       
     137        if (rc != EOK)
    133138                return rc;
    134         if(ieee80211_get_auth_phase(ieee80211_dev) !=
    135                 IEEE80211_AUTH_ASSOCIATED) {
    136                 ieee80211_set_auth_phase(ieee80211_dev,
    137                         IEEE80211_AUTH_DISCONNECTED);
     139       
     140        if (ieee80211_get_auth_phase(ieee80211_dev) !=
     141            IEEE80211_AUTH_ASSOCIATED) {
     142                ieee80211_set_auth_phase(ieee80211_dev,
     143                    IEEE80211_AUTH_DISCONNECTED);
    138144                return EINVAL;
    139145        }
    140146       
    141147        /* On open network, we are finished. */
    142         if(auth_data->scan_result.security.type != IEEE80211_SECURITY_OPEN) {
     148        if (auth_data->scan_result.security.type !=
     149            IEEE80211_SECURITY_OPEN) {
    143150                /* Otherwise wait for 4-way handshake to complete. */
     151               
    144152                fibril_mutex_lock(&ieee80211_dev->gen_mutex);
    145153                rc = fibril_condvar_wait_timeout(&ieee80211_dev->gen_cond,
    146                                 &ieee80211_dev->gen_mutex,
    147                                 HANDSHAKE_TIMEOUT);
     154                    &ieee80211_dev->gen_mutex, HANDSHAKE_TIMEOUT);
    148155                fibril_mutex_unlock(&ieee80211_dev->gen_mutex);
    149                 if(rc != EOK) {
     156               
     157                if (rc != EOK) {
    150158                        ieee80211_deauthenticate(ieee80211_dev);
    151159                        return rc;
     
    158166}
    159167
    160 /**
    161  * Implementation of connecting to specified SSID.
    162  *
    163  * @param fun Device function.
     168/** Implementation of connecting to specified SSID.
     169 *
     170 * @param fun        Device function.
    164171 * @param ssid_start SSID prefix of access point we want to connect to.
    165  *
    166  * @return EOK if everything OK, ETIMEOUT when timeout during authenticating,
    167  * EINVAL when SSID not in scan results list, EPERM when incorrect password
    168  * passed, EREFUSED when device is not ready yet.
     172 *
     173 * @return EOK if everything OK,
     174 *         ETIMEOUT when timeout during authenticating,
     175 *         EINVAL when SSID not in scan results list,
     176 *         EPERM when incorrect password passed,
     177 *         EREFUSED when device is not ready yet.
     178 *
    169179 */
    170180int ieee80211_connect_impl(ddf_fun_t *fun, char *ssid_start, char *password)
     
    173183        assert(password);
    174184       
    175         int rc;
    176        
    177185        nic_t *nic_data = nic_get_from_ddf_fun(fun);
    178186        ieee80211_dev_t *ieee80211_dev = nic_get_specific(nic_data);
    179187       
    180         if(!ieee80211_is_ready(ieee80211_dev))
     188        if (!ieee80211_is_ready(ieee80211_dev))
    181189                return EREFUSED;
    182190       
    183         if(ieee80211_is_connected(ieee80211_dev)) {
    184                 rc = ieee80211_dev->iface->disconnect(fun);
    185                 if(rc != EOK)
     191        if (ieee80211_is_connected(ieee80211_dev)) {
     192                int rc = ieee80211_dev->iface->disconnect(fun);
     193                if (rc != EOK)
    186194                        return rc;
    187195        }
     
    189197        ieee80211_set_connect_request(ieee80211_dev);
    190198       
    191         rc = ENOENT;
     199        int rc = ENOENT;
    192200        fibril_mutex_lock(&ieee80211_dev->scan_mutex);
    193201       
    194202        ieee80211_dev->pending_conn_req = false;
    195 
     203       
    196204        ieee80211_scan_result_list_foreach(ieee80211_dev->ap_list, result) {
    197                 if(!str_lcmp(ssid_start,
    198                         result->scan_result.ssid,
    199                         str_size(ssid_start))) {
     205                if (!str_lcmp(ssid_start, result->scan_result.ssid,
     206                    str_size(ssid_start))) {
    200207                        rc = ieee80211_connect_proc(ieee80211_dev, result,
    201                                 password);
     208                            password);
    202209                        break;
    203210                }
     
    209216}
    210217
    211 /**
    212  * Implementation of disconnecting device from network.
    213  *
     218/** Implementation of disconnecting device from network.
     219 *
    214220 * @param fun Device function.
    215  *
    216  * @return EOK if everything OK, EREFUSED if device is not ready yet.
     221 *
     222 * @return EOK if everything OK,
     223 *         EREFUSED if device is not ready yet.
     224 *
    217225 */
    218226int ieee80211_disconnect_impl(ddf_fun_t *fun)
     
    221229        ieee80211_dev_t *ieee80211_dev = nic_get_specific(nic_data);
    222230       
    223         if(!ieee80211_is_ready(ieee80211_dev))
     231        if (!ieee80211_is_ready(ieee80211_dev))
    224232                return EREFUSED;
    225233       
    226         if(!ieee80211_is_connected(ieee80211_dev)) {
     234        if (!ieee80211_is_connected(ieee80211_dev))
    227235                return EOK;
    228         } else {
    229                 fibril_mutex_lock(&ieee80211_dev->ap_list.results_mutex);
    230                 int rc = ieee80211_deauthenticate(ieee80211_dev);
    231                 fibril_mutex_unlock(&ieee80211_dev->ap_list.results_mutex);
    232                 return rc;
    233         }
     236       
     237        fibril_mutex_lock(&ieee80211_dev->ap_list.results_mutex);
     238        int rc = ieee80211_deauthenticate(ieee80211_dev);
     239        fibril_mutex_unlock(&ieee80211_dev->ap_list.results_mutex);
     240       
     241        return rc;
    234242}
    235243
  • uspace/lib/ieee80211/src/ieee80211_impl.c

    r09044cb r8a64320e  
    3232
    3333/** @file ieee80211_impl.c
    34  * 
     34 *
    3535 * IEEE 802.11 default device functions implementation.
    3636 */
     
    4040#include <stdlib.h>
    4141#include <errno.h>
    42 
    4342#include <ieee80211_impl.h>
    4443
    45 /**
    46  * Default implementation of IEEE802.11 start function.
    47  *
    48  * @param ieee80211_dev Structure of IEEE802.11 device.
    49  *
    50  * @return EOK.
     44/** Default implementation of IEEE802.11 start function.
     45 *
     46 * @param ieee80211_dev Structure of IEEE802.11 device.
     47 *
     48 * @return EOK.
     49 *
    5150 */
    5251int ieee80211_start_impl(ieee80211_dev_t *ieee80211_dev)
     
    5554}
    5655
    57 /**
    58  * Default implementation of IEEE802.11 TX handler function.
    59  *
    60  * @param ieee80211_dev Structure of IEEE802.11 device.
    61  * @param buffer Buffer with data to send.
    62  * @param buffer_size Size of buffer.
    63  *
    64  * @return EOK.
    65  */
    66 int ieee80211_tx_handler_impl(ieee80211_dev_t *ieee80211_dev, void *buffer, 
    67         size_t buffer_size)
    68 {
    69         return EOK;
    70 }
    71 
    72 /**
    73  * Default implementation of IEEE802.11 set frequency function.
    74  *
    75  * @param ieee80211_dev Structure of IEEE802.11 device.
    76  * @param freq Value of frequency to be switched on.
    77  *
    78  * @return EOK.
     56/** Default implementation of IEEE802.11 TX handler function.
     57 *
     58 * @param ieee80211_dev Structure of IEEE802.11 device.
     59 * @param buffer        Buffer with data to send.
     60 * @param buffer_size   Size of buffer.
     61 *
     62 * @return EOK.
     63 *
     64 */
     65int ieee80211_tx_handler_impl(ieee80211_dev_t *ieee80211_dev, void *buffer,
     66    size_t buffer_size)
     67{
     68        return EOK;
     69}
     70
     71/** Default implementation of IEEE802.11 set frequency function.
     72 *
     73 * @param ieee80211_dev Structure of IEEE802.11 device.
     74 * @param freq          Value of frequency to be switched on.
     75 *
     76 * @return EOK.
     77 *
    7978 */
    8079int ieee80211_set_freq_impl(ieee80211_dev_t *ieee80211_dev, uint16_t freq)
     
    8382}
    8483
    85 /**
    86  * Default implementation of IEEE802.11 BSSID change function.
    87  *
    88  * @param ieee80211_dev Structure of IEEE802.11 device.
    89  *
    90  * @return EOK.
    91  */
    92 int ieee80211_bssid_change_impl(ieee80211_dev_t *ieee80211_dev, 
    93         bool connected)
    94 {
    95         return EOK;
    96 }
    97 
    98 /**
    99  * Default implementation of IEEE802.11 key config function.
    100  *
    101  * @param ieee80211_dev Structure of IEEE802.11 device.
    102  *
    103  * @return EOK.
     84/** Default implementation of IEEE802.11 BSSID change function.
     85 *
     86 * @param ieee80211_dev Structure of IEEE802.11 device.
     87 *
     88 * @return EOK.
     89 *
     90 */
     91int ieee80211_bssid_change_impl(ieee80211_dev_t *ieee80211_dev,
     92    bool connected)
     93{
     94        return EOK;
     95}
     96
     97/** Default implementation of IEEE802.11 key config function.
     98 *
     99 * @param ieee80211_dev Structure of IEEE802.11 device.
     100 *
     101 * @return EOK.
     102 *
    104103 */
    105104int ieee80211_key_config_impl(ieee80211_dev_t *ieee80211_dev,
    106         ieee80211_key_config_t *key_conf, bool insert)
    107 {
    108         return EOK;
    109 }
    110 
    111 /**
    112  * Default implementation of IEEE802.11 scan function.
    113  *
    114  * @param ieee80211_dev Structure of IEEE802.11 device.
    115  * @param clear Whether to clear current scan results.
    116  *
    117  * @return EOK if succeed, negative error code otherwise.
     105    ieee80211_key_config_t *key_conf, bool insert)
     106{
     107        return EOK;
     108}
     109
     110/** Default implementation of IEEE802.11 scan function.
     111 *
     112 * @param ieee80211_dev Structure of IEEE802.11 device.
     113 * @param clear         Whether to clear current scan results.
     114 *
     115 * @return EOK if succeed, negative error code otherwise.
     116 *
    118117 */
    119118int ieee80211_scan_impl(ieee80211_dev_t *ieee80211_dev)
     
    121120        fibril_mutex_lock(&ieee80211_dev->scan_mutex);
    122121       
    123         if(ieee80211_get_auth_phase(ieee80211_dev) ==
    124                 IEEE80211_AUTH_DISCONNECTED) {
     122        if (ieee80211_get_auth_phase(ieee80211_dev) ==
     123            IEEE80211_AUTH_DISCONNECTED) {
    125124                fibril_mutex_lock(&ieee80211_dev->ap_list.results_mutex);
     125               
    126126                /* Remove old entries we don't receive beacons from. */
    127                 ieee80211_scan_result_list_t *result_list =
    128                         &ieee80211_dev->ap_list;
     127                ieee80211_scan_result_list_t *result_list =
     128                    &ieee80211_dev->ap_list;
     129               
    129130                list_foreach_safe(result_list->list, cur_link, next_link) {
    130                         ieee80211_scan_result_link_t *cur_result =
    131                                 list_get_instance(cur_link,
    132                                 ieee80211_scan_result_link_t,
    133                                 link);
    134                         if((time(NULL) - cur_result->last_beacon) >
    135                                 MAX_KEEP_SCAN_SPAN_SEC) {
    136                                 ieee80211_scan_result_list_remove(result_list,
    137                                         cur_result);
    138                         }
     131                        ieee80211_scan_result_link_t *cur_result =
     132                            list_get_instance(cur_link,
     133                            ieee80211_scan_result_link_t, link);
     134                       
     135                        if ((time(NULL) - cur_result->last_beacon) >
     136                            MAX_KEEP_SCAN_SPAN_SEC)
     137                                ieee80211_scan_result_list_remove(result_list,
     138                                    cur_result);
    139139                }
     140               
    140141                fibril_mutex_unlock(&ieee80211_dev->ap_list.results_mutex);
    141 
     142               
    142143                uint16_t orig_freq = ieee80211_dev->current_freq;
    143 
    144                 for(uint16_t freq = IEEE80211_FIRST_FREQ;
    145                         freq <= IEEE80211_MAX_FREQ;
    146                         freq += IEEE80211_CHANNEL_GAP) {
    147                         if(ieee80211_pending_connect_request(ieee80211_dev)) {
     144               
     145                for (uint16_t freq = IEEE80211_FIRST_FREQ;
     146                    freq <= IEEE80211_MAX_FREQ; freq += IEEE80211_CHANNEL_GAP) {
     147                        if (ieee80211_pending_connect_request(ieee80211_dev))
    148148                                break;
    149                         }
    150149                       
    151150                        ieee80211_dev->ops->set_freq(ieee80211_dev, freq);
    152151                        ieee80211_probe_request(ieee80211_dev, NULL);
    153 
     152                       
    154153                        /* Wait for probe responses. */
    155154                        async_usleep(SCAN_CHANNEL_WAIT_USEC);
    156155                }
    157 
     156               
    158157                ieee80211_dev->ops->set_freq(ieee80211_dev, orig_freq);
    159158        }
     
    164163}
    165164
    166 /**
    167  * Pseudorandom function used for IEEE 802.11 pairwise key computation
    168  * using SHA1 hash algorithm.
    169  *
    170  * @param key Key with PBKDF2 encrypted passphrase.
    171  * @param data Concatenated sequence of both mac addresses and nonces.
    172  * @param hash Output parameter for result hash.
     165/** Pseudorandom function used for IEEE 802.11 pairwise key computation.
     166 *
     167 * Using SHA1 hash algorithm.
     168 *
     169 * @param key         Key with PBKDF2 encrypted passphrase.
     170 * @param data        Concatenated sequence of both MAC
     171 *                    addresses and nonces.
     172 * @param hash        Output parameter for result hash.
    173173 * @param output_size Length of output sequence to be generated.
    174  *
    175  * @return EINVAL when key or data not specified, ENOMEM when pointer for
    176  * output hash result is not allocated, otherwise EOK.
    177  */
    178 int ieee80211_prf(uint8_t *key, uint8_t *data, uint8_t *hash,
    179         size_t output_size)
    180 {
    181         if(!key || !data)
     174 *
     175 * @return EINVAL when key or data not specified,
     176 *         ENOMEM when pointer for output hash result
     177 *         is not allocated, otherwise EOK.
     178 *
     179 */
     180int ieee80211_prf(uint8_t *key, uint8_t *data, uint8_t *hash,
     181    size_t output_size)
     182{
     183        if ((!key) || (!data))
    182184                return EINVAL;
    183185       
    184         if(!hash)
     186        if (!hash)
    185187                return ENOMEM;
    186188       
     
    188190       
    189191        const char *a = "Pairwise key expansion";
    190         uint8_t result[HASH_SHA1*iters];
     192        uint8_t result[HASH_SHA1 * iters];
    191193        uint8_t temp[HASH_SHA1];
     194       
    192195        size_t data_size = PRF_CRYPT_DATA_LENGTH + str_size(a) + 2;
    193196        uint8_t work_arr[data_size];
     
    196199        memcpy(work_arr, a, str_size(a));
    197200        memcpy(work_arr + str_size(a) + 1, data, PRF_CRYPT_DATA_LENGTH);
    198 
    199         for(uint8_t i = 0; i < iters; i++) {
     201       
     202        for (uint8_t i = 0; i < iters; i++) {
    200203                memcpy(work_arr + data_size - 1, &i, 1);
    201                 hmac(key, PBKDF2_KEY_LENGTH, work_arr, data_size, temp, 
    202                         HASH_SHA1);
     204                hmac(key, PBKDF2_KEY_LENGTH, work_arr, data_size, temp,
     205                    HASH_SHA1);
    203206                memcpy(result + i*HASH_SHA1, temp, HASH_SHA1);
    204207        }
     
    210213
    211214int ieee80211_rc4_key_unwrap(uint8_t *key, uint8_t *data, size_t data_size,
    212         uint8_t *output)
     215    uint8_t *output)
    213216{
    214217        return rc4(key, 32, data, data_size, 256, output);
     
    216219
    217220int ieee80211_aes_key_unwrap(uint8_t *kek, uint8_t *data, size_t data_size,
    218         uint8_t *output)
    219 {
    220         if(!kek || !data)
     221    uint8_t *output)
     222{
     223        if ((!kek) || (!data))
    221224                return EINVAL;
    222225       
    223         if(!output)
     226        if (!output)
    224227                return ENOMEM;
    225 
    226         uint32_t n = data_size/8 - 1;
    227         uint8_t work_data[n*8];
     228       
     229        uint32_t n = data_size / 8 - 1;
     230        uint8_t work_data[n * 8];
    228231        uint8_t work_input[AES_CIPHER_LENGTH];
    229232        uint8_t work_output[AES_CIPHER_LENGTH];
    230233        uint8_t *work_block;
    231234        uint8_t a[8];
     235       
    232236        memcpy(a, data, 8);
    233         uint64_t mask = 0xFF;
     237       
     238        uint64_t mask = 0xff;
    234239        uint8_t shift, shb;
    235240       
    236         memcpy(work_data, data + 8, n*8);
    237         for(int j = 5; j >= 0; j--) {
    238                 for(int i = n; i > 0; i--) {
    239                         for(size_t k = 0; k < 8; k++) {
    240                                 shift = 56 - 8*k;
    241                                 shb = ((n*j+i) & (mask << shift)) >> shift;
     241        memcpy(work_data, data + 8, n * 8);
     242        for (int j = 5; j >= 0; j--) {
     243                for (int i = n; i > 0; i--) {
     244                        for (size_t k = 0; k < 8; k++) {
     245                                shift = 56 - 8 * k;
     246                                shb = ((n * j + i) & (mask << shift)) >> shift;
    242247                                a[k] ^= shb;
    243248                        }
    244                         work_block = work_data + (i-1)*8;
     249                       
     250                        work_block = work_data + (i - 1) * 8;
    245251                        memcpy(work_input, a, 8);
    246252                        memcpy(work_input + 8, work_block, 8);
    247253                        aes_decrypt(kek, work_input, work_output);
    248254                        memcpy(a, work_output, 8);
    249                         memcpy(work_data + (i-1)*8, work_output + 8, 8);
     255                        memcpy(work_data + (i - 1) * 8, work_output + 8, 8);
    250256                }
    251257        }
    252258       
    253259        size_t it;
    254         for(it = 0; it < 8; it++) {
    255                 if(a[it] != 0xA6)
     260        for (it = 0; it < 8; it++) {
     261                if (a[it] != 0xa6)
    256262                        break;
    257263        }
    258264       
    259         if(it == 8) {
    260                 memcpy(output, work_data, n*8);
     265        if (it == 8) {
     266                memcpy(output, work_data, n * 8);
    261267                return EOK;
    262         } else {
    263                 return EINVAL;
    264         }
    265 }
    266 
    267 static void ieee80211_michael_mic_block(uint32_t *l, uint32_t *r, 
    268         uint32_t value)
     268        }
     269       
     270        return EINVAL;
     271}
     272
     273static void ieee80211_michael_mic_block(uint32_t *l, uint32_t *r,
     274    uint32_t value)
    269275{
    270276        *l ^= value;
    271277        *r ^= rotl_uint32(*l, 17);
    272278        *l += *r;
    273         *r ^= ((*l & 0x00FF00FF) << 8) | ((*l & 0xFF00FF00) >> 8);
     279        *r ^= ((*l & 0x00ff00ff) << 8) | ((*l & 0xff00ff00) >> 8);
    274280        *l += *r;
    275281        *r ^= rotl_uint32(*l, 3);
     
    279285}
    280286
    281 int ieee80211_michael_mic(uint8_t *key, uint8_t *buffer, size_t size, 
    282         uint8_t *mic)
    283 {
    284         if(!key || !buffer)
     287int ieee80211_michael_mic(uint8_t *key, uint8_t *buffer, size_t size,
     288    uint8_t *mic)
     289{
     290        if ((!key) || (!buffer))
    285291                return EINVAL;
    286292       
    287         if(!mic)
     293        if (!mic)
    288294                return ENOMEM;
    289295       
     
    292298       
    293299        ieee80211_data_header_t *data_header =
    294                 (ieee80211_data_header_t *) buffer;
    295        
    296         uint8_t *data = buffer + sizeof(ieee80211_data_header_t) + 
    297                 IEEE80211_TKIP_HEADER_LENGTH;
     300            (ieee80211_data_header_t *) buffer;
     301       
     302        uint8_t *data = buffer + sizeof(ieee80211_data_header_t) +
     303            IEEE80211_TKIP_HEADER_LENGTH;
    298304        size_t data_size = size - sizeof(ieee80211_data_header_t) -
    299                 IEEE80211_TKIP_HEADER_LENGTH;
     305            IEEE80211_TKIP_HEADER_LENGTH;
    300306       
    301307        /* Process header. */
    302         uint8_t *src_addr = 
    303                 ieee80211_is_fromds_frame(data_header->frame_ctrl) ?
    304                         data_header->address3 : data_header->address2;
    305         uint8_t *dest_addr = 
    306                 ieee80211_is_tods_frame(data_header->frame_ctrl) ?
    307                         data_header->address3 : data_header->address1;
     308        uint8_t *src_addr =
     309            ieee80211_is_fromds_frame(data_header->frame_ctrl) ?
     310            data_header->address3 : data_header->address2;
     311        uint8_t *dest_addr =
     312            ieee80211_is_tods_frame(data_header->frame_ctrl) ?
     313            data_header->address3 : data_header->address1;
    308314       
    309315        ieee80211_michael_mic_block(&l, &r, uint32le_from_seq(dest_addr));
    310         ieee80211_michael_mic_block(&l, &r, 
    311                 uint16le_from_seq(dest_addr + 4) |
    312                 (uint16le_from_seq(src_addr) << 16));
     316        ieee80211_michael_mic_block(&l, &r,
     317            uint16le_from_seq(dest_addr + 4) |
     318            (uint16le_from_seq(src_addr) << 16));
    313319        ieee80211_michael_mic_block(&l, &r, uint32le_from_seq(src_addr + 2));
    314320        ieee80211_michael_mic_block(&l, &r, 0);
     
    318324        size_t pad = data_size % 4;
    319325       
    320         for(size_t k = 0; k < blocks; k++) {
    321                 ieee80211_michael_mic_block(&l, &r, 
    322                         uint32le_from_seq(&data[k*4]));
     326        for (size_t k = 0; k < blocks; k++) {
     327                ieee80211_michael_mic_block(&l, &r,
     328                    uint32le_from_seq(&data[k * 4]));
    323329        }
    324330       
    325331        /* Add padding. */
    326         uint32_t value = 0x5A;
    327         for(size_t i = pad; i > 0; i--) {
     332        uint32_t value = 0x5a;
     333        for (size_t i = pad; i > 0; i--) {
    328334                value <<= 8;
    329                 value |= data[blocks*4 + (i-1)];
     335                value |= data[blocks * 4 + (i - 1)];
    330336        }
    331337       
     
    345351{
    346352        uint16_t *u16 = (uint16_t *) seq;
    347         return uint16_t_le2host(*u16); 
     353        return uint16_t_le2host(*u16);
    348354}
    349355
     
    351357{
    352358        uint32_t *u32 = (uint32_t *) seq;
    353         return uint32_t_le2host(*u32); 
     359        return uint32_t_le2host(*u32);
    354360}
    355361
     
    357363{
    358364        uint16_t *u16 = (uint16_t *) seq;
    359         return uint16_t_be2host(*u16); 
     365        return uint16_t_be2host(*u16);
    360366}
    361367
     
    363369{
    364370        uint32_t *u32 = (uint32_t *) seq;
    365         return uint32_t_be2host(*u32); 
     371        return uint32_t_be2host(*u32);
    366372}
    367373
    368374int rnd_sequence(uint8_t *sequence, size_t length)
    369375{
    370         if(!sequence)
     376        if (!sequence)
    371377                return ENOMEM;
    372378       
    373         for(size_t i = 0; i < length; i++) {
     379        for (size_t i = 0; i < length; i++)
    374380                sequence[i] = (uint8_t) rand();
    375         }
    376381       
    377382        return EOK;
     
    380385uint8_t *min_sequence(uint8_t *seq1, uint8_t *seq2, size_t size)
    381386{
    382         if(!seq1 || !seq2)
     387        if ((!seq1) || (!seq2))
    383388                return NULL;
    384389       
    385         for(size_t i = 0; i < size; i++) {
    386                 if(seq1[i] < seq2[i]) {
     390        for (size_t i = 0; i < size; i++) {
     391                if (seq1[i] < seq2[i])
    387392                        return seq1;
    388                 } else if(seq1[i] > seq2[i]) {
     393                else if (seq1[i] > seq2[i])
    389394                        return seq2;
    390                 }
    391395        }
    392396       
     
    397401{
    398402        uint8_t *min = min_sequence(seq1, seq2, size);
    399         if(min == seq1) {
     403        if (min == seq1)
    400404                return seq2;
    401         } else {
    402                 return seq1;
    403         }
     405       
     406        return seq1;
    404407}
    405408
  • uspace/srv/net/dhcp/dhcp.c

    r09044cb r8a64320e  
    428428        dlink->retries_left = dhcp_discover_retries;
    429429       
    430         if(dlink->timeout->state == fts_not_set ||
    431                 dlink->timeout->state == fts_fired) {
     430        if ((dlink->timeout->state == fts_not_set) ||
     431            (dlink->timeout->state == fts_fired))
    432432                fibril_timer_set(dlink->timeout, dhcp_discover_timeout_val,
    433                         dhcpsrv_discover_timeout, dlink);
    434         }
     433                    dhcpsrv_discover_timeout, dlink);
    435434       
    436435        return rc;
     
    507506       
    508507        dhcp_link_t *dlink = dhcpsrv_link_find(link_id);
    509 
     508       
    510509        if (dlink == NULL) {
    511510                log_msg(LOG_DEFAULT, LVL_NOTE, "Link %zu doesn't exist",
Note: See TracChangeset for help on using the changeset viewer.