| 1 |
package gioctl |
| 2 |
|
| 3 |
import ( |
| 4 |
"syscall" |
| 5 |
) |
| 6 |
|
| 7 |
const ( |
| 8 |
numberBits = 8 |
| 9 |
typeBits = 8 |
| 10 |
|
| 11 |
numberShift = 0 |
| 12 |
typeShift = numberShift + numberBits |
| 13 |
sizeShift = typeShift + typeBits |
| 14 |
directionShift = sizeShift + sizeBits |
| 15 |
) |
| 16 |
|
| 17 |
func ioc(dir, t, nr, size uintptr) uintptr { |
| 18 |
return (dir << directionShift) | (t << typeShift) | (nr << numberShift) | (size << sizeShift) |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
func Io(t, nr uintptr) uintptr { |
| 23 |
return ioc(directionNone, t, nr, 0) |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
func IoR(t, nr, size uintptr) uintptr { |
| 28 |
return ioc(directionRead, t, nr, size) |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func IoW(t, nr, size uintptr) uintptr { |
| 33 |
return ioc(directionWrite, t, nr, size) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
func IoRW(t, nr, size uintptr) uintptr { |
| 38 |
return ioc(directionRead|directionWrite, t, nr, size) |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
func Ioctl(fd, op, arg uintptr) error { |
| 43 |
_, _, ep := syscall.Syscall(syscall.SYS_IOCTL, fd, op, arg) |
| 44 |
if ep != 0 { |
| 45 |
return syscall.Errno(ep) |
| 46 |
} |
| 47 |
return nil |
| 48 |
} |