io.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Copyright 2026 Aarav Ravindra Kharade
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. /* Copyright (C) 1996-2026 Free Software Foundation, Inc.
  17. This file is part of the GNU C Library.
  18. The GNU C Library is free software; you can redistribute it and/or
  19. modify it under the terms of the GNU Lesser General Public
  20. License as published by the Free Software Foundation; either
  21. version 2.1 of the License, or (at your option) any later version.
  22. The GNU C Library is distributed in the hope that it will be useful,
  23. but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. Lesser General Public License for more details.
  26. You should have received a copy of the GNU Lesser General Public
  27. License along with the GNU C Library; if not, see
  28. <https://www.gnu.org/licenses/>. */
  29. #ifndef _SYS_IO_H
  30. #define _SYS_IO_H 1
  31. #include <features.h>
  32. __BEGIN_DECLS
  33. /* If TURN_ON is TRUE, request for permission to do direct i/o on the
  34. port numbers in the range [FROM,FROM+NUM-1]. Otherwise, turn I/O
  35. permission off for that range. This call requires root privileges.
  36. Portability note: not all Linux platforms support this call. Most
  37. platforms based on the PC I/O architecture probably will, however.
  38. E.g., Linux/Alpha for Alpha PCs supports this. */
  39. extern int ioperm (unsigned long int __from, unsigned long int __num,
  40. int __turn_on) __THROW;
  41. /* Set the I/O privilege level to LEVEL. If LEVEL>3, permission to
  42. access any I/O port is granted. This call requires root
  43. privileges. */
  44. extern int iopl (int __level) __THROW;
  45. #if defined __GNUC__ && __GNUC__ >= 2
  46. static __inline unsigned char
  47. inb (unsigned short int __port)
  48. {
  49. unsigned char _v;
  50. __asm__ __volatile__ ("inb %w1,%0":"=a" (_v):"Nd" (__port));
  51. return _v;
  52. }
  53. static __inline unsigned char
  54. inb_p (unsigned short int __port)
  55. {
  56. unsigned char _v;
  57. __asm__ __volatile__ ("inb %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port));
  58. return _v;
  59. }
  60. static __inline unsigned short int
  61. inw (unsigned short int __port)
  62. {
  63. unsigned short _v;
  64. __asm__ __volatile__ ("inw %w1,%0":"=a" (_v):"Nd" (__port));
  65. return _v;
  66. }
  67. static __inline unsigned short int
  68. inw_p (unsigned short int __port)
  69. {
  70. unsigned short int _v;
  71. __asm__ __volatile__ ("inw %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port));
  72. return _v;
  73. }
  74. static __inline unsigned int
  75. inl (unsigned short int __port)
  76. {
  77. unsigned int _v;
  78. __asm__ __volatile__ ("inl %w1,%0":"=a" (_v):"Nd" (__port));
  79. return _v;
  80. }
  81. static __inline unsigned int
  82. inl_p (unsigned short int __port)
  83. {
  84. unsigned int _v;
  85. __asm__ __volatile__ ("inl %w1,%0\noutb %%al,$0x80":"=a" (_v):"Nd" (__port));
  86. return _v;
  87. }
  88. static __inline void
  89. outb (unsigned char __value, unsigned short int __port)
  90. {
  91. __asm__ __volatile__ ("outb %b0,%w1": :"a" (__value), "Nd" (__port));
  92. }
  93. static __inline void
  94. outb_p (unsigned char __value, unsigned short int __port)
  95. {
  96. __asm__ __volatile__ ("outb %b0,%w1\noutb %%al,$0x80": :"a" (__value),
  97. "Nd" (__port));
  98. }
  99. static __inline void
  100. outw (unsigned short int __value, unsigned short int __port)
  101. {
  102. __asm__ __volatile__ ("outw %w0,%w1": :"a" (__value), "Nd" (__port));
  103. }
  104. static __inline void
  105. outw_p (unsigned short int __value, unsigned short int __port)
  106. {
  107. __asm__ __volatile__ ("outw %w0,%w1\noutb %%al,$0x80": :"a" (__value),
  108. "Nd" (__port));
  109. }
  110. static __inline void
  111. outl (unsigned int __value, unsigned short int __port)
  112. {
  113. __asm__ __volatile__ ("outl %0,%w1": :"a" (__value), "Nd" (__port));
  114. }
  115. static __inline void
  116. outl_p (unsigned int __value, unsigned short int __port)
  117. {
  118. __asm__ __volatile__ ("outl %0,%w1\noutb %%al,$0x80": :"a" (__value),
  119. "Nd" (__port));
  120. }
  121. static __inline void
  122. insb (unsigned short int __port, void *__addr, unsigned long int __count)
  123. {
  124. __asm__ __volatile__ ("cld ; rep ; insb":"=D" (__addr), "=c" (__count)
  125. :"d" (__port), "0" (__addr), "1" (__count));
  126. }
  127. static __inline void
  128. insw (unsigned short int __port, void *__addr, unsigned long int __count)
  129. {
  130. __asm__ __volatile__ ("cld ; rep ; insw":"=D" (__addr), "=c" (__count)
  131. :"d" (__port), "0" (__addr), "1" (__count));
  132. }
  133. static __inline void
  134. insl (unsigned short int __port, void *__addr, unsigned long int __count)
  135. {
  136. __asm__ __volatile__ ("cld ; rep ; insl":"=D" (__addr), "=c" (__count)
  137. :"d" (__port), "0" (__addr), "1" (__count));
  138. }
  139. static __inline void
  140. outsb (unsigned short int __port, const void *__addr,
  141. unsigned long int __count)
  142. {
  143. __asm__ __volatile__ ("cld ; rep ; outsb":"=S" (__addr), "=c" (__count)
  144. :"d" (__port), "0" (__addr), "1" (__count));
  145. }
  146. static __inline void
  147. outsw (unsigned short int __port, const void *__addr,
  148. unsigned long int __count)
  149. {
  150. __asm__ __volatile__ ("cld ; rep ; outsw":"=S" (__addr), "=c" (__count)
  151. :"d" (__port), "0" (__addr), "1" (__count));
  152. }
  153. static __inline void
  154. outsl (unsigned short int __port, const void *__addr,
  155. unsigned long int __count)
  156. {
  157. __asm__ __volatile__ ("cld ; rep ; outsl":"=S" (__addr), "=c" (__count)
  158. :"d" (__port), "0" (__addr), "1" (__count));
  159. }
  160. #endif /* GNU C */
  161. __END_DECLS
  162. #endif /* _SYS_IO_H */