33 *
44 * Written 2013 by Werner Almesberger <[email protected] > 55 *
6+ * Copyright (c) 2015 - 2016 Stefan Schmidt <[email protected] > 7+ *
68 * This program is free software; you can redistribute it and/or
79 * modify it under the terms of the GNU General Public License as
810 * published by the Free Software Foundation, version 2
@@ -472,6 +474,76 @@ atusb_set_txpower(struct ieee802154_hw *hw, s32 mbm)
472474 return - EINVAL ;
473475}
474476
477+ #define ATUSB_MAX_ED_LEVELS 0xF
478+ static const s32 atusb_ed_levels [ATUSB_MAX_ED_LEVELS + 1 ] = {
479+ -9100 , -8900 , -8700 , -8500 , -8300 , -8100 , -7900 , -7700 , -7500 , -7300 ,
480+ -7100 , -6900 , -6700 , -6500 , -6300 , -6100 ,
481+ };
482+
483+ static int
484+ atusb_set_cca_mode (struct ieee802154_hw * hw , const struct wpan_phy_cca * cca )
485+ {
486+ struct atusb * atusb = hw -> priv ;
487+ u8 val ;
488+
489+ /* mapping 802.15.4 to driver spec */
490+ switch (cca -> mode ) {
491+ case NL802154_CCA_ENERGY :
492+ val = 1 ;
493+ break ;
494+ case NL802154_CCA_CARRIER :
495+ val = 2 ;
496+ break ;
497+ case NL802154_CCA_ENERGY_CARRIER :
498+ switch (cca -> opt ) {
499+ case NL802154_CCA_OPT_ENERGY_CARRIER_AND :
500+ val = 3 ;
501+ break ;
502+ case NL802154_CCA_OPT_ENERGY_CARRIER_OR :
503+ val = 0 ;
504+ break ;
505+ default :
506+ return - EINVAL ;
507+ }
508+ break ;
509+ default :
510+ return - EINVAL ;
511+ }
512+
513+ return atusb_write_subreg (atusb , SR_CCA_MODE , val );
514+ }
515+
516+ static int
517+ atusb_set_cca_ed_level (struct ieee802154_hw * hw , s32 mbm )
518+ {
519+ struct atusb * atusb = hw -> priv ;
520+ u32 i ;
521+
522+ for (i = 0 ; i < hw -> phy -> supported .cca_ed_levels_size ; i ++ ) {
523+ if (hw -> phy -> supported .cca_ed_levels [i ] == mbm )
524+ return atusb_write_subreg (atusb , SR_CCA_ED_THRES , i );
525+ }
526+
527+ return - EINVAL ;
528+ }
529+
530+ static int
531+ atusb_set_csma_params (struct ieee802154_hw * hw , u8 min_be , u8 max_be , u8 retries )
532+ {
533+ struct atusb * atusb = hw -> priv ;
534+ int ret ;
535+
536+ ret = atusb_write_subreg (atusb , SR_MIN_BE , min_be );
537+ if (ret )
538+ return ret ;
539+
540+ ret = atusb_write_subreg (atusb , SR_MAX_BE , max_be );
541+ if (ret )
542+ return ret ;
543+
544+ return atusb_write_subreg (atusb , SR_MAX_CSMA_RETRIES , retries );
545+ }
546+
475547static int
476548atusb_set_promiscuous_mode (struct ieee802154_hw * hw , const bool on )
477549{
@@ -508,6 +580,9 @@ static struct ieee802154_ops atusb_ops = {
508580 .stop = atusb_stop ,
509581 .set_hw_addr_filt = atusb_set_hw_addr_filt ,
510582 .set_txpower = atusb_set_txpower ,
583+ .set_cca_mode = atusb_set_cca_mode ,
584+ .set_cca_ed_level = atusb_set_cca_ed_level ,
585+ .set_csma_params = atusb_set_csma_params ,
511586 .set_promiscuous_mode = atusb_set_promiscuous_mode ,
512587};
513588
@@ -636,9 +711,20 @@ static int atusb_probe(struct usb_interface *interface,
636711
637712 hw -> parent = & usb_dev -> dev ;
638713 hw -> flags = IEEE802154_HW_TX_OMIT_CKSUM | IEEE802154_HW_AFILT |
639- IEEE802154_HW_PROMISCUOUS ;
714+ IEEE802154_HW_PROMISCUOUS | IEEE802154_HW_CSMA_PARAMS ;
715+
716+ hw -> phy -> flags = WPAN_PHY_FLAG_TXPOWER | WPAN_PHY_FLAG_CCA_ED_LEVEL |
717+ WPAN_PHY_FLAG_CCA_MODE ;
718+
719+ hw -> phy -> supported .cca_modes = BIT (NL802154_CCA_ENERGY ) |
720+ BIT (NL802154_CCA_CARRIER ) | BIT (NL802154_CCA_ENERGY_CARRIER );
721+ hw -> phy -> supported .cca_opts = BIT (NL802154_CCA_OPT_ENERGY_CARRIER_AND ) |
722+ BIT (NL802154_CCA_OPT_ENERGY_CARRIER_OR );
723+
724+ hw -> phy -> supported .cca_ed_levels = atusb_ed_levels ;
725+ hw -> phy -> supported .cca_ed_levels_size = ARRAY_SIZE (atusb_ed_levels );
640726
641- hw -> phy -> flags = WPAN_PHY_FLAG_TXPOWER ;
727+ hw -> phy -> cca . mode = NL802154_CCA_ENERGY ;
642728
643729 hw -> phy -> current_page = 0 ;
644730 hw -> phy -> current_channel = 11 ; /* reset default */
@@ -647,6 +733,7 @@ static int atusb_probe(struct usb_interface *interface,
647733 hw -> phy -> supported .tx_powers_size = ARRAY_SIZE (atusb_powers );
648734 hw -> phy -> transmit_power = hw -> phy -> supported .tx_powers [0 ];
649735 ieee802154_random_extended_addr (& hw -> phy -> perm_extended_addr );
736+ hw -> phy -> cca_ed_level = hw -> phy -> supported .cca_ed_levels [7 ];
650737
651738 atusb_command (atusb , ATUSB_RF_RESET , 0 );
652739 atusb_get_and_show_chip (atusb );
0 commit comments