hv_drv_UsbBitWise.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * @file hv_drv_UsbBitWise.h
  3. * @brief Header file of usb module.
  4. *
  5. * @author HiView SoC Software Team
  6. * @version 1.0.0
  7. * @date 2022-06-15
  8. */
  9. #ifndef __HV_DRV_USB_BITWISE_H
  10. #define __HV_DRV_USB_BITWISE_H
  11. #include "hv_drv_UsbPortingTypes.h"
  12. #define __BITS_PER_LONG 32
  13. #define BITOP_WORD(nr) (nr / __BITS_PER_LONG)
  14. INLINE void set_bit(int nr, volatile unsigned long *addr) {
  15. addr[nr / __BITS_PER_LONG] |= 1UL << (nr % __BITS_PER_LONG);
  16. }
  17. INLINE void clear_bit(int nr, volatile unsigned long *addr) {
  18. addr[nr / __BITS_PER_LONG] &= ~(1UL << (nr % __BITS_PER_LONG));
  19. }
  20. INLINE int test_bit(int nr, const unsigned long *addr) {
  21. return ((1UL << (nr % __BITS_PER_LONG)) & (addr[nr / __BITS_PER_LONG])) != 0;
  22. }
  23. INLINE int test_and_clear_bit(int nr, volatile unsigned long *addr) {
  24. const unsigned b = ((1UL << (nr % __BITS_PER_LONG)) & (addr[nr / __BITS_PER_LONG]));
  25. clear_bit(nr, addr);
  26. return b != 0;
  27. }
  28. /* awkward implement */
  29. INLINE long find_next_zero_bit(const unsigned long *addr,
  30. unsigned long size, unsigned long offset) {
  31. int max_of = BITOP_WORD(size);
  32. int addr_of = BITOP_WORD(offset);
  33. const unsigned long *temp = addr + addr_of;
  34. int ofc = offset - (addr_of * __BITS_PER_LONG);
  35. int i, of;
  36. for (of = addr_of; of < max_of; of++) {
  37. for (i = ofc; i < __BITS_PER_LONG; i++) {
  38. if ((1 << i) & *temp)
  39. continue;
  40. else
  41. break;
  42. }
  43. if (i == __BITS_PER_LONG) {
  44. temp += 1;
  45. ofc = 0;
  46. } else
  47. break;
  48. }
  49. return (((of) * __BITS_PER_LONG) + i);
  50. }
  51. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  52. #define BITS_PER_BYTE 8
  53. #define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
  54. /* common */
  55. #define DECLARE_BITMAP(name, bits) \
  56. unsigned long name[BITS_TO_LONGS(bits)]
  57. #define small_const_nbits(nbits) \
  58. (__builtin_constant_p(nbits) && (nbits) <= __BITS_PER_LONG)
  59. static inline void bitmap_zero(unsigned long *dst, int nbits)
  60. {
  61. if (small_const_nbits(nbits))
  62. *dst = 0UL;
  63. else {
  64. int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
  65. HV_MEMSET(dst, 0, len);
  66. }
  67. }
  68. #endif