util.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
  2. * Copyright(c) 2019-2020 Realtek Corporation
  3. */
  4. #ifndef __RTW89_UTIL_H__
  5. #define __RTW89_UTIL_H__
  6. #include "core.h"
  7. #define rtw89_iterate_vifs_bh(rtwdev, iterator, data) \
  8. ieee80211_iterate_active_interfaces_atomic((rtwdev)->hw, \
  9. IEEE80211_IFACE_ITER_NORMAL, iterator, data)
  10. /* call this function with rtwdev->mutex is held */
  11. #define rtw89_for_each_rtwvif(rtwdev, rtwvif) \
  12. list_for_each_entry(rtwvif, &(rtwdev)->rtwvifs_list, list)
  13. /* The result of negative dividend and positive divisor is undefined, but it
  14. * should be one case of round-down or round-up. So, make it round-down if the
  15. * result is round-up.
  16. * Note: the maximum value of divisor is 0x7FFF_FFFF, because we cast it to
  17. * signed value to make compiler to use signed divide instruction.
  18. */
  19. static inline s32 s32_div_u32_round_down(s32 dividend, u32 divisor, s32 *remainder)
  20. {
  21. s32 i_divisor = (s32)divisor;
  22. s32 i_remainder;
  23. s32 quotient;
  24. quotient = dividend / i_divisor;
  25. i_remainder = dividend % i_divisor;
  26. if (i_remainder < 0) {
  27. quotient--;
  28. i_remainder += i_divisor;
  29. }
  30. if (remainder)
  31. *remainder = i_remainder;
  32. return quotient;
  33. }
  34. static inline s32 s32_div_u32_round_closest(s32 dividend, u32 divisor)
  35. {
  36. return s32_div_u32_round_down(dividend + divisor / 2, divisor, NULL);
  37. }
  38. static inline void ether_addr_copy_mask(u8 *dst, const u8 *src, u8 mask)
  39. {
  40. int i;
  41. eth_zero_addr(dst);
  42. for (i = 0; i < ETH_ALEN; i++) {
  43. if (mask & BIT(i))
  44. dst[i] = src[i];
  45. }
  46. }
  47. #endif