ioctl_linux.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package linux
  2. // ioctl for linux
  3. /* ioctl command encoding: 32 bits total, command in lower 16 bits,
  4. * size of the parameter structure in the lower 14 bits of the
  5. * upper 16 bits.
  6. * Encoding the size of the parameter structure in the ioctl request
  7. * is useful for catching programs compiled with old versions
  8. * and to avoid overwriting user space outside the user buffer area.
  9. * The highest 2 bits are reserved for indicating the ``access mode''.
  10. * NOTE: This limits the max parameter size to 16kB -1 !
  11. */
  12. /*
  13. * The following is for compatibility across the various Linux
  14. * platforms. The generic ioctl numbering scheme doesn't really enforce
  15. * a type_ field. De facto, however, the top 8 bits of the lower 16
  16. * bits are indeed used as a type_ field, so we might just as well make
  17. * this explicit here. Please be sure to use the decoding macros
  18. * below from now on.
  19. */
  20. const IOC_NRBITS = 8
  21. // XXX might be different on some platforms
  22. const IOC_TYPEBITS = 8
  23. // XXX might be 13 on some platforms
  24. const IOC_SIZEBITS = 14
  25. // XXX might be 3 on some platforms
  26. const IOC_DIRBITS = 2
  27. const IOC_NRMASK = ((1 << IOC_NRBITS)-1)
  28. const IOC_TYPEMASK = ((1 << IOC_TYPEBITS)-1)
  29. const IOC_SIZEMASK = ((1 << IOC_SIZEBITS)-1)
  30. const IOC_DIRMASK = ((1 << IOC_DIRBITS)-1)
  31. const IOC_NRSHIFT = 0
  32. const IOC_TYPESHIFT = (IOC_NRSHIFT+IOC_NRBITS)
  33. const IOC_SIZESHIFT = (IOC_TYPESHIFT+IOC_TYPEBITS)
  34. const IOC_DIRSHIFT = (IOC_SIZESHIFT+IOC_SIZEBITS)
  35. /*
  36. * Direction bits, which any architecture can choose to override
  37. * before including this file.
  38. */
  39. const IOC_NONE = 0
  40. //XXX is 4 on some platforms
  41. const IOC_WRITE = 1
  42. const IOC_READ = 2
  43. func IOC(dir uint32, type_ rune, nr uint32, size uintptr) uint32 {
  44. return (((dir) << IOC_DIRSHIFT) |
  45. ((uint32(type_)) << IOC_TYPESHIFT) |
  46. (uint32(nr) << IOC_NRSHIFT) |
  47. (uint32(size) << IOC_SIZESHIFT))
  48. }
  49. /* used to create numbers */
  50. func IO(type_ rune,nr uint32) uint32 {
  51. return IOC(IOC_NONE,(type_),(nr),0)
  52. }
  53. func IOR(type_ rune, nr uint32, size uintptr) uint32 {
  54. return IOC(IOC_READ,type_,nr, size)
  55. }
  56. func IOW(type_ rune ,nr uint32,size uintptr) uint32 {
  57. return IOC(IOC_WRITE,(type_),(nr),((size)))
  58. }
  59. func IOWR(type_ rune,nr uint32,size uintptr) uint32 {
  60. return IOC(IOC_READ|IOC_WRITE,(type_),(nr),((size)))
  61. }
  62. /* used to decode ioctl numbers.. */
  63. func IOC_DIR(nr uint32) uint32 {
  64. return (((nr) >> IOC_DIRSHIFT) & IOC_DIRMASK)
  65. }
  66. func IOC_TYPE(nr uint32) uint32 {
  67. return (((nr) >> IOC_TYPESHIFT) & IOC_TYPEMASK)
  68. }
  69. func IOC_NR(nr uint32) uint32 {
  70. return (((nr) >> IOC_NRSHIFT) & IOC_NRMASK)
  71. }
  72. func IOC_SIZE(nr uint32) uint32 {
  73. return (((nr) >> IOC_SIZESHIFT) & IOC_SIZEMASK)
  74. }
  75. /* ...and for the drivers/sound files... */
  76. const IOC_IN = (IOC_WRITE << IOC_DIRSHIFT)
  77. const IOC_OUT = (IOC_READ << IOC_DIRSHIFT)
  78. const IOCINOUT = ((IOC_WRITE|IOC_READ) << IOC_DIRSHIFT)
  79. const IOCSIZE_MASK = (IOC_SIZEMASK << IOC_SIZESHIFT)
  80. const IOCSIZE_SHIFT = (IOC_SIZESHIFT)