Advertisement
zmatt

uio-irq.h

May 13th, 2020 (edited)
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #pragma once
  2. #include <stdint.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5.  
  6. //-------------- UIO interrupt handling --------------------------------------//
  7. //
  8. // All functions here return 0 on success or negated errno on failure.
  9. //
  10. // The uio device has an event-counter, initialized to zero at device creation.
  11. // Whenever an irq is recognized the counter is incremented and the fd becomes
  12. // readable.  You read the event counter from the fd to rearm the mechanism.
  13. //
  14. inline int uio_wfi( int fd, uint32_t *counter_out = nullptr )
  15. {
  16.     uint32_t tmp;  // in case caller doesn't care about the counter
  17.     ssize_t n = read( fd, counter_out ?: &tmp, sizeof *counter_out );
  18.     if( n < 0 )
  19.         return -errno;
  20.     assert( (size_t)n == sizeof *counter_out );
  21.     return 0;
  22. }
  23.  
  24.  
  25. // Driver-specific irq control.
  26. //
  27. inline int uio_irqcontrol( int fd, uint32_t value )
  28. {
  29.     ssize_t n = write( fd, &value, sizeof value );
  30.     if( n < 0 )
  31.         return -errno;
  32.     assert( (size_t)n == sizeof value );
  33.     return 0;
  34. }
  35.  
  36.  
  37. // Generic IRQ control used by some uio drivers, notably uio_pdrv_genirq.
  38. //
  39. // Note that uio_pdrv_genirq automatically disables the irq when it triggers,
  40. // and you'll need to reenable it yourself each time.
  41. //
  42. // See https://pastebin.com/x0sUKmKa for more details.
  43. //
  44. inline int uio_generic_irqenable( int fd )
  45. {
  46.     return uio_irqcontrol( fd, 1 );
  47. }
  48. inline int uio_generic_irqdisable( int fd )
  49. {
  50.     return uio_irqcontrol( fd, 0 );
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement