Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ieee802154: atusb: Driver for Busware HUL dongle

Busware manufactured an USB dongle that is quite similar to
the atben and rzusb USB dongles. that are already supported.
This patch aims to support the Busware HUL dongle (called
hulusb) alongside atusb and rzusb. hulusb is using the
at86rf212 transceiver which is specifically designed to
support the 700/800/900 MHz wave band.

The source code is heavily inspired by the existing atusb
and at86rf2xx drivers.

Signed-off-by: Josef Filzmaier <j.filzmaier@gmx.at>
Signed-off-by: Stefan Schmidt <stefan@osg.samsung.com>

authored by

Josef Filzmaier and committed by
Stefan Schmidt
d5dd29e4 3e496266

+285 -40
+277 -40
drivers/net/ieee802154/atusb.c
··· 21 21 * 22 22 * USB initialization is 23 23 * Copyright (c) 2013 Alexander Aring <alex.aring@gmail.com> 24 + * 25 + * Busware HUL support is 26 + * Copyright (c) 2017 Josef Filzmaier <j.filzmaier@gmx.at> 24 27 */ 25 28 26 29 #include <linux/kernel.h> ··· 45 42 #define ATUSB_ALLOC_DELAY_MS 100 /* delay after failed allocation */ 46 43 #define ATUSB_TX_TIMEOUT_MS 200 /* on the air timeout */ 47 44 45 + struct atusb_chip_data; 46 + 48 47 struct atusb { 49 48 struct ieee802154_hw *hw; 50 49 struct usb_device *usb_dev; 50 + struct atusb_chip_data *data; 51 51 int shutdown; /* non-zero if shutting down */ 52 52 int err; /* set by first error */ 53 53 ··· 69 63 unsigned char fw_ver_maj; /* Firmware major version number */ 70 64 unsigned char fw_ver_min; /* Firmware minor version number */ 71 65 unsigned char fw_hw_type; /* Firmware hardware type */ 66 + }; 67 + 68 + struct atusb_chip_data { 69 + u16 t_channel_switch; 70 + int rssi_base_val; 71 + 72 + int (*set_channel)(struct ieee802154_hw*, u8, u8); 73 + int (*set_txpower)(struct ieee802154_hw*, s32); 72 74 }; 73 75 74 76 /* ----- USB commands without data ----------------------------------------- */ ··· 175 161 ret = atusb_write_reg(atusb, reg, tmp); 176 162 177 163 return ret; 164 + } 165 + 166 + static int atusb_read_subreg(struct atusb *lp, 167 + unsigned int addr, unsigned int mask, 168 + unsigned int shift) 169 + { 170 + int rc; 171 + 172 + rc = atusb_read_reg(lp, addr); 173 + rc = (rc & mask) >> shift; 174 + 175 + return rc; 178 176 } 179 177 180 178 static int atusb_get_and_clear_error(struct atusb *atusb) ··· 405 379 return ret; 406 380 } 407 381 408 - static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel) 409 - { 410 - struct atusb *atusb = hw->priv; 411 - int ret; 412 - 413 - ret = atusb_write_subreg(atusb, SR_CHANNEL, channel); 414 - if (ret < 0) 415 - return ret; 416 - msleep(1); /* @@@ ugly synchronization */ 417 - return 0; 418 - } 419 - 420 382 static int atusb_ed(struct ieee802154_hw *hw, u8 *level) 421 383 { 422 384 BUG_ON(!level); ··· 489 475 }; 490 476 491 477 static int 478 + atusb_txpower(struct ieee802154_hw *hw, s32 mbm) 479 + { 480 + struct atusb *atusb = hw->priv; 481 + 482 + if (atusb->data) 483 + return atusb->data->set_txpower(hw, mbm); 484 + else 485 + return -ENOTSUPP; 486 + } 487 + 488 + static int 492 489 atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm) 493 490 { 494 491 struct atusb *atusb = hw->priv; ··· 513 488 return -EINVAL; 514 489 } 515 490 491 + static int 492 + hulusb_set_txpower(struct ieee802154_hw *hw, s32 mbm) 493 + { 494 + u32 i; 495 + 496 + for (i = 0; i < hw->phy->supported.tx_powers_size; i++) { 497 + if (hw->phy->supported.tx_powers[i] == mbm) 498 + return atusb_write_subreg(hw->priv, SR_TX_PWR_212, i); 499 + } 500 + 501 + return -EINVAL; 502 + } 503 + 516 504 #define ATUSB_MAX_ED_LEVELS 0xF 517 505 static const s32 atusb_ed_levels[ATUSB_MAX_ED_LEVELS + 1] = { 518 506 -9100, -8900, -8700, -8500, -8300, -8100, -7900, -7700, -7500, -7300, 519 507 -7100, -6900, -6700, -6500, -6300, -6100, 508 + }; 509 + 510 + #define AT86RF212_MAX_TX_POWERS 0x1F 511 + static const s32 at86rf212_powers[AT86RF212_MAX_TX_POWERS + 1] = { 512 + 500, 400, 300, 200, 100, 0, -100, -200, -300, -400, -500, -600, -700, 513 + -800, -900, -1000, -1100, -1200, -1300, -1400, -1500, -1600, -1700, 514 + -1800, -1900, -2000, -2100, -2200, -2300, -2400, -2500, -2600, 515 + }; 516 + 517 + #define AT86RF2XX_MAX_ED_LEVELS 0xF 518 + static const s32 at86rf212_ed_levels_100[AT86RF2XX_MAX_ED_LEVELS + 1] = { 519 + -10000, -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, 520 + -8000, -7800, -7600, -7400, -7200, -7000, 521 + }; 522 + 523 + static const s32 at86rf212_ed_levels_98[AT86RF2XX_MAX_ED_LEVELS + 1] = { 524 + -9800, -9600, -9400, -9200, -9000, -8800, -8600, -8400, -8200, -8000, 525 + -7800, -7600, -7400, -7200, -7000, -6800, 520 526 }; 521 527 522 528 static int ··· 583 527 return atusb_write_subreg(atusb, SR_CCA_MODE, val); 584 528 } 585 529 530 + static int hulusb_set_cca_ed_level(struct atusb *lp, int rssi_base_val) 531 + { 532 + unsigned int cca_ed_thres; 533 + 534 + cca_ed_thres = atusb_read_subreg(lp, SR_CCA_ED_THRES); 535 + 536 + switch (rssi_base_val) { 537 + case -98: 538 + lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_98; 539 + lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_98); 540 + lp->hw->phy->cca_ed_level = at86rf212_ed_levels_98[cca_ed_thres]; 541 + break; 542 + case -100: 543 + lp->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100; 544 + lp->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100); 545 + lp->hw->phy->cca_ed_level = at86rf212_ed_levels_100[cca_ed_thres]; 546 + break; 547 + default: 548 + WARN_ON(1); 549 + } 550 + 551 + return 0; 552 + } 553 + 586 554 static int 587 555 atusb_set_cca_ed_level(struct ieee802154_hw *hw, s32 mbm) 588 556 { ··· 619 539 } 620 540 621 541 return -EINVAL; 542 + } 543 + 544 + static int atusb_channel(struct ieee802154_hw *hw, u8 page, u8 channel) 545 + { 546 + struct atusb *atusb = hw->priv; 547 + int ret = -ENOTSUPP; 548 + 549 + if (atusb->data) { 550 + ret = atusb->data->set_channel(hw, page, channel); 551 + /* @@@ ugly synchronization */ 552 + msleep(atusb->data->t_channel_switch); 553 + } 554 + 555 + return ret; 556 + } 557 + 558 + static int atusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel) 559 + { 560 + struct atusb *atusb = hw->priv; 561 + int ret; 562 + 563 + ret = atusb_write_subreg(atusb, SR_CHANNEL, channel); 564 + if (ret < 0) 565 + return ret; 566 + return 0; 567 + } 568 + 569 + static int hulusb_set_channel(struct ieee802154_hw *hw, u8 page, u8 channel) 570 + { 571 + int rc; 572 + int rssi_base_val; 573 + 574 + struct atusb *lp = hw->priv; 575 + 576 + if (channel == 0) 577 + rc = atusb_write_subreg(lp, SR_SUB_MODE, 0); 578 + else 579 + rc = atusb_write_subreg(lp, SR_SUB_MODE, 1); 580 + if (rc < 0) 581 + return rc; 582 + 583 + if (page == 0) { 584 + rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 0); 585 + rssi_base_val = -100; 586 + } else { 587 + rc = atusb_write_subreg(lp, SR_BPSK_QPSK, 1); 588 + rssi_base_val = -98; 589 + } 590 + if (rc < 0) 591 + return rc; 592 + 593 + rc = hulusb_set_cca_ed_level(lp, rssi_base_val); 594 + if (rc < 0) 595 + return rc; 596 + 597 + /* This sets the symbol_duration according frequency on the 212. 598 + * TODO move this handling while set channel and page in cfg802154. 599 + * We can do that, this timings are according 802.15.4 standard. 600 + * If we do that in cfg802154, this is a more generic calculation. 601 + * 602 + * This should also protected from ifs_timer. Means cancel timer and 603 + * init with a new value. For now, this is okay. 604 + */ 605 + if (channel == 0) { 606 + if (page == 0) { 607 + /* SUB:0 and BPSK:0 -> BPSK-20 */ 608 + lp->hw->phy->symbol_duration = 50; 609 + } else { 610 + /* SUB:1 and BPSK:0 -> BPSK-40 */ 611 + lp->hw->phy->symbol_duration = 25; 612 + } 613 + } else { 614 + if (page == 0) 615 + /* SUB:0 and BPSK:1 -> OQPSK-100/200/400 */ 616 + lp->hw->phy->symbol_duration = 40; 617 + else 618 + /* SUB:1 and BPSK:1 -> OQPSK-250/500/1000 */ 619 + lp->hw->phy->symbol_duration = 16; 620 + } 621 + 622 + lp->hw->phy->lifs_period = IEEE802154_LIFS_PERIOD * 623 + lp->hw->phy->symbol_duration; 624 + lp->hw->phy->sifs_period = IEEE802154_SIFS_PERIOD * 625 + lp->hw->phy->symbol_duration; 626 + 627 + return atusb_write_subreg(lp, SR_CHANNEL, channel); 622 628 } 623 629 624 630 static int ··· 722 556 return ret; 723 557 724 558 return atusb_write_subreg(atusb, SR_MAX_CSMA_RETRIES, retries); 559 + } 560 + 561 + static int 562 + hulusb_set_lbt(struct ieee802154_hw *hw, bool on) 563 + { 564 + struct atusb *atusb = hw->priv; 565 + 566 + return atusb_write_subreg(atusb, SR_CSMA_LBT_MODE, on); 725 567 } 726 568 727 569 static int ··· 767 593 return 0; 768 594 } 769 595 596 + struct atusb_chip_data atusb_chip_data = { 597 + .t_channel_switch = 1, 598 + .rssi_base_val = -91, 599 + .set_txpower = atusb_set_txpower, 600 + .set_channel = atusb_set_channel, 601 + }; 602 + 603 + struct atusb_chip_data hulusb_chip_data = { 604 + .t_channel_switch = 11, 605 + .rssi_base_val = -100, 606 + .set_txpower = hulusb_set_txpower, 607 + .set_channel = hulusb_set_channel, 608 + }; 609 + 770 610 static const struct ieee802154_ops atusb_ops = { 771 611 .owner = THIS_MODULE, 772 612 .xmit_async = atusb_xmit, ··· 789 601 .start = atusb_start, 790 602 .stop = atusb_stop, 791 603 .set_hw_addr_filt = atusb_set_hw_addr_filt, 792 - .set_txpower = atusb_set_txpower, 604 + .set_txpower = atusb_txpower, 605 + .set_lbt = hulusb_set_lbt, 793 606 .set_cca_mode = atusb_set_cca_mode, 794 607 .set_cca_ed_level = atusb_set_cca_ed_level, 795 608 .set_csma_params = atusb_set_csma_params, ··· 803 614 static int atusb_get_and_show_revision(struct atusb *atusb) 804 615 { 805 616 struct usb_device *usb_dev = atusb->usb_dev; 617 + char *hw_name; 806 618 unsigned char *buffer; 807 619 int ret; 808 620 ··· 820 630 atusb->fw_ver_min = buffer[1]; 821 631 atusb->fw_hw_type = buffer[2]; 822 632 633 + switch (atusb->fw_hw_type) { 634 + case ATUSB_HW_TYPE_100813: 635 + case ATUSB_HW_TYPE_101216: 636 + case ATUSB_HW_TYPE_110131: 637 + hw_name = "ATUSB"; 638 + atusb->data = &atusb_chip_data; 639 + break; 640 + case ATUSB_HW_TYPE_RZUSB: 641 + hw_name = "RZUSB"; 642 + atusb->data = &atusb_chip_data; 643 + break; 644 + case ATUSB_HW_TYPE_HULUSB: 645 + hw_name = "HULUSB"; 646 + atusb->data = &hulusb_chip_data; 647 + break; 648 + default: 649 + hw_name = "UNKNOWN"; 650 + atusb->err = -ENOTSUPP; 651 + ret = -ENOTSUPP; 652 + break; 653 + } 654 + 823 655 dev_info(&usb_dev->dev, 824 - "Firmware: major: %u, minor: %u, hardware type: %u\n", 825 - atusb->fw_ver_maj, atusb->fw_ver_min, atusb->fw_hw_type); 656 + "Firmware: major: %u, minor: %u, hardware type: %s (%d)\n", 657 + atusb->fw_ver_maj, atusb->fw_ver_min, hw_name, atusb->fw_hw_type); 826 658 } 827 659 if (atusb->fw_ver_maj == 0 && atusb->fw_ver_min < 2) { 828 660 dev_info(&usb_dev->dev, ··· 879 667 return ret; 880 668 } 881 669 882 - static int atusb_get_and_show_chip(struct atusb *atusb) 670 + static int atusb_get_and_conf_chip(struct atusb *atusb) 883 671 { 884 672 struct usb_device *usb_dev = atusb->usb_dev; 885 673 uint8_t man_id_0, man_id_1, part_num, version_num; 886 674 const char *chip; 675 + struct ieee802154_hw *hw = atusb->hw; 887 676 888 677 man_id_0 = atusb_read_reg(atusb, RG_MAN_ID_0); 889 678 man_id_1 = atusb_read_reg(atusb, RG_MAN_ID_1); ··· 893 680 894 681 if (atusb->err) 895 682 return atusb->err; 683 + 684 + hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT | 685 + IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS; 686 + 687 + hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL | 688 + WPAN_PHY_FLAG_CCA_MODE; 689 + 690 + hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) | 691 + BIT(NL802154_CCA_CARRIER) | 692 + BIT(NL802154_CCA_ENERGY_CARRIER); 693 + hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) | 694 + BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR); 695 + 696 + hw->phy->cca.mode = NL802154_CCA_ENERGY; 697 + 698 + hw->phy->current_page = 0; 896 699 897 700 if ((man_id_1 << 8 | man_id_0) != ATUSB_JEDEC_ATMEL) { 898 701 dev_err(&usb_dev->dev, ··· 920 691 switch (part_num) { 921 692 case 2: 922 693 chip = "AT86RF230"; 694 + atusb->hw->phy->supported.channels[0] = 0x7FFF800; 695 + atusb->hw->phy->current_channel = 11; /* reset default */ 696 + atusb->hw->phy->symbol_duration = 16; 697 + atusb->hw->phy->supported.tx_powers = atusb_powers; 698 + atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers); 699 + hw->phy->supported.cca_ed_levels = atusb_ed_levels; 700 + hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels); 923 701 break; 924 702 case 3: 925 703 chip = "AT86RF231"; 704 + atusb->hw->phy->supported.channels[0] = 0x7FFF800; 705 + atusb->hw->phy->current_channel = 11; /* reset default */ 706 + atusb->hw->phy->symbol_duration = 16; 707 + atusb->hw->phy->supported.tx_powers = atusb_powers; 708 + atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers); 709 + hw->phy->supported.cca_ed_levels = atusb_ed_levels; 710 + hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels); 711 + break; 712 + case 7: 713 + chip = "AT86RF212"; 714 + atusb->hw->flags |= IEEE802154_HW_LBT; 715 + atusb->hw->phy->supported.channels[0] = 0x00007FF; 716 + atusb->hw->phy->supported.channels[2] = 0x00007FF; 717 + atusb->hw->phy->current_channel = 5; 718 + atusb->hw->phy->symbol_duration = 25; 719 + atusb->hw->phy->supported.lbt = NL802154_SUPPORTED_BOOL_BOTH; 720 + atusb->hw->phy->supported.tx_powers = at86rf212_powers; 721 + atusb->hw->phy->supported.tx_powers_size = ARRAY_SIZE(at86rf212_powers); 722 + atusb->hw->phy->supported.cca_ed_levels = at86rf212_ed_levels_100; 723 + atusb->hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(at86rf212_ed_levels_100); 926 724 break; 927 725 default: 928 726 dev_err(&usb_dev->dev, ··· 957 701 part_num, version_num); 958 702 goto fail; 959 703 } 704 + 705 + hw->phy->transmit_power = hw->phy->supported.tx_powers[0]; 706 + hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7]; 960 707 961 708 dev_info(&usb_dev->dev, "ATUSB: %s version %d\n", chip, version_num); 962 709 ··· 1053 794 goto fail; 1054 795 1055 796 hw->parent = &usb_dev->dev; 1056 - hw->flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT | 1057 - IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS; 1058 - 1059 - hw->phy->flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL | 1060 - WPAN_PHY_FLAG_CCA_MODE; 1061 - 1062 - hw->phy->supported.cca_modes = BIT(NL802154_CCA_ENERGY) | 1063 - BIT(NL802154_CCA_CARRIER) | BIT(NL802154_CCA_ENERGY_CARRIER); 1064 - hw->phy->supported.cca_opts = BIT(NL802154_CCA_OPT_ENERGY_CARRIER_AND) | 1065 - BIT(NL802154_CCA_OPT_ENERGY_CARRIER_OR); 1066 - 1067 - hw->phy->supported.cca_ed_levels = atusb_ed_levels; 1068 - hw->phy->supported.cca_ed_levels_size = ARRAY_SIZE(atusb_ed_levels); 1069 - 1070 - hw->phy->cca.mode = NL802154_CCA_ENERGY; 1071 - 1072 - hw->phy->current_page = 0; 1073 - hw->phy->current_channel = 11; /* reset default */ 1074 - hw->phy->supported.channels[0] = 0x7FFF800; 1075 - hw->phy->supported.tx_powers = atusb_powers; 1076 - hw->phy->supported.tx_powers_size = ARRAY_SIZE(atusb_powers); 1077 - hw->phy->transmit_power = hw->phy->supported.tx_powers[0]; 1078 - hw->phy->cca_ed_level = hw->phy->supported.cca_ed_levels[7]; 1079 797 1080 798 atusb_command(atusb, ATUSB_RF_RESET, 0); 1081 - atusb_get_and_show_chip(atusb); 799 + atusb_get_and_conf_chip(atusb); 1082 800 atusb_get_and_show_revision(atusb); 1083 801 atusb_get_and_show_build(atusb); 1084 802 atusb_set_extended_addr(atusb); ··· 1177 941 MODULE_AUTHOR("Richard Sharpe <realrichardsharpe@gmail.com>"); 1178 942 MODULE_AUTHOR("Stefan Schmidt <stefan@datenfreihafen.org>"); 1179 943 MODULE_AUTHOR("Werner Almesberger <werner@almesberger.net>"); 944 + MODULE_AUTHOR("Josef Filzmaier <j.filzmaier@gmx.at>"); 1180 945 MODULE_DESCRIPTION("ATUSB IEEE 802.15.4 Driver"); 1181 946 MODULE_LICENSE("GPL");
+8
drivers/net/ieee802154/atusb.h
··· 50 50 ATUSB_EUI64_READ, 51 51 }; 52 52 53 + enum { 54 + ATUSB_HW_TYPE_100813, /* 2010-08-13 */ 55 + ATUSB_HW_TYPE_101216, /* 2010-12-16 */ 56 + ATUSB_HW_TYPE_110131, /* 2011-01-31, ATmega32U2-based */ 57 + ATUSB_HW_TYPE_RZUSB, /* Atmel Raven USB dongle with at86rf230 */ 58 + ATUSB_HW_TYPE_HULUSB, /* Busware HUL USB dongle with at86rf212 */ 59 + }; 60 + 53 61 /* 54 62 * Direction bRequest wValue wIndex wLength 55 63 *