Changeset a35b458 in mainline for uspace/drv/nic/ar9271/ar9271.c
- Timestamp:
- 2018-03-02T20:10:49Z (6 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/drv/nic/ar9271/ar9271.c
r3061bc1 ra35b458 148 148 assert(dev); 149 149 assert(info); 150 150 151 151 memset(info, 0, sizeof(nic_device_info_t)); 152 152 153 153 info->vendor_id = 0x0cf3; 154 154 info->device_id = 0x9271; … … 157 157 str_cpy(info->model_name, NIC_MODEL_MAX_LENGTH, 158 158 "AR9271"); 159 159 160 160 return EOK; 161 161 } … … 167 167 { 168 168 *state = NIC_CS_PLUGGED; 169 169 170 170 return EOK; 171 171 } … … 180 180 *speed = 10; 181 181 *role = NIC_ROLE_UNKNOWN; 182 182 183 183 return EOK; 184 184 } … … 203 203 return ENOTSUP; 204 204 } 205 205 206 206 return EOK; 207 207 } … … 229 229 return ENOTSUP; 230 230 } 231 231 232 232 return EOK; 233 233 } … … 249 249 return ENOTSUP; 250 250 } 251 251 252 252 return EOK; 253 253 } … … 261 261 { 262 262 assert(arg); 263 263 264 264 ar9271_t *ar9271 = (ar9271_t *) arg; 265 265 266 266 size_t buffer_size = ar9271->ath_device->data_response_length; 267 267 void *buffer = malloc(buffer_size); 268 268 269 269 while (true) { 270 270 size_t transferred_size; … … 275 275 sizeof(htc_frame_header_t) + 276 276 sizeof(htc_rx_status_t); 277 277 278 278 if (transferred_size < strip_length) 279 279 continue; 280 280 281 281 ath_usb_data_header_t *data_header = 282 282 (ath_usb_data_header_t *) buffer; 283 283 284 284 /* Invalid packet. */ 285 285 if (data_header->tag != uint16_t_le2host(RX_TAG)) 286 286 continue; 287 287 288 288 htc_rx_status_t *rx_status = 289 289 (htc_rx_status_t *) ((void *) buffer + 290 290 sizeof(ath_usb_data_header_t) + 291 291 sizeof(htc_frame_header_t)); 292 292 293 293 uint16_t data_length = 294 294 uint16_t_be2host(rx_status->data_length); 295 295 296 296 int16_t payload_length = 297 297 transferred_size - strip_length; 298 298 299 299 if (payload_length - data_length < 0) 300 300 continue; 301 301 302 302 if (ar9271_rx_status_error(rx_status->status)) 303 303 continue; 304 304 305 305 void *strip_buffer = buffer + strip_length; 306 306 307 307 ieee80211_rx_handler(ar9271->ieee80211_dev, 308 308 strip_buffer, … … 310 310 } 311 311 } 312 312 313 313 free(buffer); 314 314 315 315 return EOK; 316 316 } … … 323 323 { 324 324 assert(ieee80211_dev); 325 325 326 326 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev); 327 327 328 328 wmi_send_command(ar9271->htc_device, WMI_DISABLE_INTR, NULL, 0, NULL); 329 329 wmi_send_command(ar9271->htc_device, WMI_DRAIN_TXQ_ALL, NULL, 0, NULL); 330 330 wmi_send_command(ar9271->htc_device, WMI_STOP_RECV, NULL, 0, NULL); 331 331 332 332 errno_t rc = hw_freq_switch(ar9271, freq); 333 333 if (rc != EOK) { … … 335 335 return rc; 336 336 } 337 337 338 338 wmi_send_command(ar9271->htc_device, WMI_START_RECV, NULL, 0, NULL); 339 339 340 340 rc = hw_rx_init(ar9271); 341 341 if (rc != EOK) { … … 343 343 return rc; 344 344 } 345 345 346 346 uint16_t htc_mode = host2uint16_t_be(1); 347 347 wmi_send_command(ar9271->htc_device, WMI_SET_MODE, 348 348 (uint8_t *) &htc_mode, sizeof(htc_mode), NULL); 349 349 wmi_send_command(ar9271->htc_device, WMI_ENABLE_INTR, NULL, 0, NULL); 350 350 351 351 return EOK; 352 352 } … … 356 356 { 357 357 assert(ieee80211_dev); 358 358 359 359 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev); 360 360 361 361 if (connected) { 362 362 nic_address_t bssid; 363 363 ieee80211_query_bssid(ieee80211_dev, &bssid); 364 364 365 365 htc_sta_msg_t sta_msg; 366 366 memset(&sta_msg, 0, sizeof(htc_sta_msg_t)); … … 371 371 sta_msg.vif_index = 0; 372 372 memcpy(&sta_msg.addr, bssid.address, ETH_ADDR); 373 373 374 374 wmi_send_command(ar9271->htc_device, WMI_NODE_CREATE, 375 375 (uint8_t *) &sta_msg, sizeof(sta_msg), NULL); 376 376 377 377 htc_rate_msg_t rate_msg; 378 378 memset(&rate_msg, 0, sizeof(htc_rate_msg_t)); … … 383 383 ieee80211bg_data_rates, 384 384 ARRAY_SIZE(ieee80211bg_data_rates)); 385 385 386 386 wmi_send_command(ar9271->htc_device, WMI_RC_RATE_UPDATE, 387 387 (uint8_t *) &rate_msg, sizeof(rate_msg), NULL); 388 388 389 389 hw_set_rx_filter(ar9271, true); 390 390 } else { … … 392 392 wmi_send_command(ar9271->htc_device, WMI_NODE_REMOVE, 393 393 &station_id, sizeof(station_id), NULL); 394 394 395 395 hw_set_rx_filter(ar9271, false); 396 396 } 397 397 398 398 hw_set_bssid(ar9271); 399 399 400 400 return EOK; 401 401 } … … 405 405 { 406 406 assert(ieee80211_dev); 407 407 408 408 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev); 409 409 410 410 if(insert) { 411 411 assert(key_conf); 412 412 413 413 uint32_t key[5]; 414 414 uint32_t key_type; 415 415 uint32_t reg_ptr, mic_reg_ptr; 416 416 void *data_start; 417 417 418 418 nic_address_t bssid; 419 419 ieee80211_query_bssid(ieee80211_dev, &bssid); 420 420 421 421 switch (key_conf->suite) { 422 422 case IEEE80211_SECURITY_SUITE_WEP40: … … 435 435 key_type = -1; 436 436 } 437 437 438 438 uint8_t key_id = 439 439 (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_PAIRWISE) ? 440 440 AR9271_STA_KEY_INDEX : key_conf->id; 441 441 442 442 reg_ptr = AR9271_KEY_TABLE(key_id); 443 443 mic_reg_ptr = AR9271_KEY_TABLE(key_id + 64); 444 444 data_start = (void *) key_conf->data; 445 445 446 446 key[0] = uint32_t_le2host(*((uint32_t *) data_start)); 447 447 key[1] = uint16_t_le2host(*((uint16_t *) (data_start + 4))); … … 449 449 key[3] = uint16_t_le2host(*((uint16_t *) (data_start + 10))); 450 450 key[4] = uint32_t_le2host(*((uint32_t *) (data_start + 12))); 451 451 452 452 if ((key_conf->suite == IEEE80211_SECURITY_SUITE_WEP40) || 453 453 (key_conf->suite == IEEE80211_SECURITY_SUITE_WEP104)) 454 454 key[4] &= 0xFF; 455 455 456 456 wmi_reg_write(ar9271->htc_device, reg_ptr + 0, key[0]); 457 457 wmi_reg_write(ar9271->htc_device, reg_ptr + 4, key[1]); … … 460 460 wmi_reg_write(ar9271->htc_device, reg_ptr + 16, key[4]); 461 461 wmi_reg_write(ar9271->htc_device, reg_ptr + 20, key_type); 462 462 463 463 uint32_t macl; 464 464 uint32_t mach; … … 471 471 mach = 0; 472 472 } 473 473 474 474 macl >>= 1; 475 475 macl |= (mach & 1) << 31; 476 476 mach >>= 1; 477 477 mach |= 0x8000; 478 478 479 479 wmi_reg_write(ar9271->htc_device, reg_ptr + 24, macl); 480 480 wmi_reg_write(ar9271->htc_device, reg_ptr + 28, mach); 481 481 482 482 /* Setup MIC keys for TKIP. */ 483 483 if (key_conf->suite == IEEE80211_SECURITY_SUITE_TKIP) { … … 485 485 uint8_t *gen_mic = data_start + IEEE80211_TKIP_RX_MIC_OFFSET; 486 486 uint8_t *tx_mic; 487 487 488 488 if (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_GROUP) 489 489 tx_mic = gen_mic; 490 490 else 491 491 tx_mic = data_start + IEEE80211_TKIP_TX_MIC_OFFSET; 492 492 493 493 mic[0] = uint32_t_le2host(*((uint32_t *) gen_mic)); 494 494 mic[1] = uint16_t_le2host(*((uint16_t *) (tx_mic + 2))) & 0xFFFF; … … 496 496 mic[3] = uint16_t_le2host(*((uint16_t *) tx_mic)) & 0xFFFF; 497 497 mic[4] = uint32_t_le2host(*((uint32_t *) (tx_mic + 4))); 498 498 499 499 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 0, mic[0]); 500 500 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 4, mic[1]); … … 504 504 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 20, 505 505 AR9271_KEY_TABLE_TYPE_CLR); 506 506 507 507 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 24, 0); 508 508 wmi_reg_write(ar9271->htc_device, mic_reg_ptr + 28, 0); 509 509 } 510 510 511 511 if (key_conf->flags & IEEE80211_KEY_FLAG_TYPE_GROUP) 512 512 ieee80211_setup_key_confirm(ieee80211_dev, true); … … 515 515 ieee80211_setup_key_confirm(ieee80211_dev, false); 516 516 } 517 517 518 518 return EOK; 519 519 } … … 523 523 { 524 524 assert(ieee80211_dev); 525 525 526 526 size_t complete_size; 527 527 size_t offset; 528 528 void *complete_buffer; 529 529 int endpoint; 530 530 531 531 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev); 532 532 533 533 uint16_t frame_ctrl = *((uint16_t *) buffer); 534 534 if (ieee80211_is_data_frame(frame_ctrl)) { … … 538 538 complete_buffer = malloc(complete_size); 539 539 memset(complete_buffer, 0, complete_size); 540 540 541 541 /* 542 542 * Because we handle just station mode yet, node ID and VIF ID … … 550 550 data_header->vif_idx = 0; 551 551 data_header->cookie = 0; 552 552 553 553 if (ieee80211_query_using_key(ieee80211_dev)) { 554 554 data_header->keyix = AR9271_STA_KEY_INDEX; 555 555 556 556 int sec_suite = 557 557 ieee80211_get_pairwise_security(ieee80211_dev); 558 558 559 559 switch (sec_suite) { 560 560 case IEEE80211_SECURITY_SUITE_WEP40: … … 573 573 data_header->keyix = 0xFF; 574 574 } 575 575 576 576 endpoint = ar9271->htc_device->endpoints.data_be_endpoint; 577 577 } else { … … 581 581 complete_buffer = malloc(complete_size); 582 582 memset(complete_buffer, 0, complete_size); 583 583 584 584 /* 585 585 * Because we handle just station mode yet, node ID and VIF ID … … 593 593 mgmt_header->cookie = 0; 594 594 mgmt_header->keyix = 0xFF; 595 595 596 596 endpoint = ar9271->htc_device->endpoints.mgmt_endpoint; 597 597 } 598 598 599 599 /* Copy IEEE802.11 data to new allocated buffer with HTC headers. */ 600 600 memcpy(complete_buffer + offset, buffer, buffer_size); 601 601 602 602 htc_send_data_message(ar9271->htc_device, complete_buffer, 603 603 complete_size, endpoint); 604 604 605 605 free(complete_buffer); 606 606 607 607 return EOK; 608 608 } … … 611 611 { 612 612 assert(ieee80211_dev); 613 613 614 614 ar9271_t *ar9271 = (ar9271_t *) ieee80211_get_specific(ieee80211_dev); 615 615 616 616 wmi_send_command(ar9271->htc_device, WMI_FLUSH_RECV, NULL, 0, NULL); 617 617 618 618 errno_t rc = hw_reset(ar9271); 619 619 if (rc != EOK) { … … 621 621 return rc; 622 622 } 623 623 624 624 uint16_t htc_mode = host2uint16_t_be(1); 625 625 wmi_send_command(ar9271->htc_device, WMI_SET_MODE, … … 628 628 wmi_send_command(ar9271->htc_device, WMI_START_RECV, NULL, 0, NULL); 629 629 wmi_send_command(ar9271->htc_device, WMI_ENABLE_INTR, NULL, 0, NULL); 630 630 631 631 rc = hw_rx_init(ar9271); 632 632 if (rc != EOK) { … … 634 634 return rc; 635 635 } 636 636 637 637 /* Send capability message to target. */ 638 638 htc_cap_msg_t cap_msg; … … 641 641 cap_msg.enable_coex = 0; 642 642 cap_msg.tx_chainmask = 0x1; 643 643 644 644 wmi_send_command(ar9271->htc_device, WMI_TARGET_IC_UPDATE, 645 645 (uint8_t *) &cap_msg, sizeof(cap_msg), NULL); 646 646 647 647 rc = htc_init_new_vif(ar9271->htc_device); 648 648 if (rc != EOK) { … … 650 650 return rc; 651 651 } 652 652 653 653 /* Add data polling fibril. */ 654 654 fid_t fibril = fibril_create(ar9271_data_polling, ar9271); 655 655 if (fibril == 0) 656 656 return ENOMEM; 657 657 658 658 fibril_add_ready(fibril); 659 659 660 660 ar9271->starting_up = false; 661 661 ieee80211_set_ready(ieee80211_dev, true); 662 662 663 663 usb_log_info("Device fully initialized.\n"); 664 664 665 665 return EOK; 666 666 } … … 670 670 ar9271->starting_up = true; 671 671 ar9271->usb_device = usb_device; 672 672 673 673 fibril_mutex_initialize(&ar9271->ar9271_lock); 674 674 675 675 ar9271->ath_device = calloc(1, sizeof(ath_t)); 676 676 if (!ar9271->ath_device) { … … 679 679 return ENOMEM; 680 680 } 681 681 682 682 errno_t rc = ath_usb_init(ar9271->ath_device, usb_device, endpoints); 683 683 if (rc != EOK) { … … 686 686 return rc; 687 687 } 688 688 689 689 /* IEEE 802.11 framework structure initialization. */ 690 690 ar9271->ieee80211_dev = ieee80211_device_create(); … … 695 695 return ENOMEM; 696 696 } 697 697 698 698 rc = ieee80211_device_init(ar9271->ieee80211_dev, ar9271->ddf_dev); 699 699 if (rc != EOK) { … … 704 704 return rc; 705 705 } 706 706 707 707 ieee80211_set_specific(ar9271->ieee80211_dev, ar9271); 708 708 709 709 /* HTC device structure initialization. */ 710 710 ar9271->htc_device = calloc(1, sizeof(htc_device_t)); … … 716 716 return ENOMEM; 717 717 } 718 718 719 719 rc = htc_device_init(ar9271->ath_device, ar9271->ieee80211_dev, 720 720 ar9271->htc_device); … … 726 726 return rc; 727 727 } 728 728 729 729 return EOK; 730 730 } … … 740 740 { 741 741 usb_device_t *usb_device = ar9271->usb_device; 742 742 743 743 /* TODO: Set by maximum packet size in pipe. */ 744 744 static const size_t MAX_TRANSFER_SIZE = 512; 745 745 746 746 /* Load FW from file. */ 747 747 FILE *fw_file = fopen(FIRMWARE_FILENAME, "rb"); … … 750 750 return ENOENT; 751 751 } 752 752 753 753 fseek(fw_file, 0, SEEK_END); 754 754 uint64_t file_size = ftell(fw_file); 755 755 fseek(fw_file, 0, SEEK_SET); 756 756 757 757 void *fw_data = malloc(file_size); 758 758 if (fw_data == NULL) { … … 761 761 return ENOMEM; 762 762 } 763 763 764 764 fread(fw_data, file_size, 1, fw_file); 765 765 fclose(fw_file); 766 766 767 767 /* Upload FW to device. */ 768 768 uint64_t remain_size = file_size; … … 770 770 uint8_t *current_data = fw_data; 771 771 uint8_t *buffer = malloc(MAX_TRANSFER_SIZE); 772 772 773 773 while (remain_size > 0) { 774 774 size_t chunk_size = min(remain_size, MAX_TRANSFER_SIZE); … … 788 788 return rc; 789 789 } 790 790 791 791 remain_size -= chunk_size; 792 792 current_addr += chunk_size; 793 793 current_data += chunk_size; 794 794 } 795 795 796 796 free(fw_data); 797 797 free(buffer); 798 798 799 799 /* 800 800 * Send command that firmware is successfully uploaded. … … 809 809 uint16_host2usb(AR9271_FW_OFFSET >> 8), 810 810 0, NULL, 0); 811 811 812 812 if (rc != EOK) { 813 813 usb_log_error("IO error when sending fw upload confirmation " … … 815 815 return rc; 816 816 } 817 817 818 818 usb_log_info("Firmware uploaded successfully.\n"); 819 819 820 820 /* Wait until firmware is ready - wait for 1 second to be sure. */ 821 821 async_sleep(1); 822 822 823 823 return rc; 824 824 } … … 840 840 return NULL; 841 841 } 842 842 843 843 /* AR9271 structure initialization. */ 844 844 ar9271_t *ar9271 = calloc(1, sizeof(ar9271_t)); … … 848 848 return NULL; 849 849 } 850 850 851 851 ar9271->ddf_dev = dev; 852 852 853 853 rc = ar9271_init(ar9271, usb_device_get(dev), endpoints); 854 854 if (rc != EOK) { … … 858 858 return NULL; 859 859 } 860 860 861 861 return ar9271; 862 862 } … … 869 869 { 870 870 assert(ar9271); 871 871 872 872 // TODO 873 873 } … … 882 882 { 883 883 assert(dev); 884 884 885 885 /* Allocate driver data for the device. */ 886 886 ar9271_t *ar9271 = ar9271_create_dev_data(dev); … … 889 889 return ENOMEM; 890 890 } 891 891 892 892 usb_log_info("HelenOS AR9271 device initialized.\n"); 893 893 894 894 /* Upload AR9271 firmware. */ 895 895 ar9271_upload_fw(ar9271); 896 896 897 897 /* Initialize AR9271 HTC services. */ 898 898 errno_t rc = htc_init(ar9271->htc_device); … … 902 902 return rc; 903 903 } 904 904 905 905 /* Initialize AR9271 HW. */ 906 906 rc = hw_init(ar9271); … … 910 910 return rc; 911 911 } 912 912 913 913 /* Initialize AR9271 IEEE 802.11 framework. */ 914 914 rc = ieee80211_init(ar9271->ieee80211_dev, &ar9271_ieee80211_ops, … … 920 920 return rc; 921 921 } 922 922 923 923 nic_set_filtering_change_handlers(nic_get_from_ddf_dev(dev), 924 924 ar9271_on_unicast_mode_change, ar9271_on_multicast_mode_change, 925 925 ar9271_on_broadcast_mode_change, NULL, NULL); 926 926 927 927 usb_log_info("HelenOS AR9271 added device.\n"); 928 928 929 929 return EOK; 930 930 } … … 933 933 { 934 934 log_init(NAME); 935 935 936 936 if (nic_driver_init(NAME) != EOK) 937 937 return 1; 938 938 939 939 usb_log_info("HelenOS AR9271 driver started.\n"); 940 940 941 941 return ddf_driver_main(&ar9271_driver); 942 942 }
Note:
See TracChangeset
for help on using the changeset viewer.