serial.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /* serial.h - serial device interface */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef GRUB_SERIAL_HEADER
  20. #define GRUB_SERIAL_HEADER 1
  21. #include <grub/types.h>
  22. #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
  23. #include <grub/cpu/io.h>
  24. #endif
  25. #include <grub/usb.h>
  26. #include <grub/list.h>
  27. #include <grub/term.h>
  28. #ifdef GRUB_MACHINE_IEEE1275
  29. #include <grub/ieee1275/ieee1275.h>
  30. #endif
  31. #ifdef GRUB_MACHINE_ARC
  32. #include <grub/arc/arc.h>
  33. #endif
  34. struct grub_serial_port;
  35. struct grub_serial_config;
  36. struct grub_serial_driver
  37. {
  38. grub_err_t (*configure) (struct grub_serial_port *port,
  39. struct grub_serial_config *config);
  40. int (*fetch) (struct grub_serial_port *port);
  41. void (*put) (struct grub_serial_port *port, const int c);
  42. void (*fini) (struct grub_serial_port *port);
  43. };
  44. /* The type of parity. */
  45. typedef enum
  46. {
  47. GRUB_SERIAL_PARITY_NONE,
  48. GRUB_SERIAL_PARITY_ODD,
  49. GRUB_SERIAL_PARITY_EVEN,
  50. } grub_serial_parity_t;
  51. typedef enum
  52. {
  53. GRUB_SERIAL_STOP_BITS_1,
  54. GRUB_SERIAL_STOP_BITS_1_5,
  55. GRUB_SERIAL_STOP_BITS_2,
  56. } grub_serial_stop_bits_t;
  57. struct grub_serial_config
  58. {
  59. unsigned speed;
  60. int word_len;
  61. grub_serial_parity_t parity;
  62. grub_serial_stop_bits_t stop_bits;
  63. grub_uint64_t base_clock;
  64. int rtscts;
  65. };
  66. struct grub_serial_port
  67. {
  68. struct grub_serial_port *next;
  69. struct grub_serial_port **prev;
  70. char *name;
  71. struct grub_serial_driver *driver;
  72. struct grub_serial_config config;
  73. int configured;
  74. int broken;
  75. /* This should be void *data but since serial is useful as an early console
  76. when malloc isn't available it's a union.
  77. */
  78. union
  79. {
  80. struct
  81. {
  82. bool use_mmio;
  83. union
  84. {
  85. #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
  86. grub_port_t port;
  87. #endif
  88. struct
  89. {
  90. grub_addr_t base;
  91. /* Access size uses ACPI definition */
  92. grub_uint8_t access_size;
  93. } mmio;
  94. };
  95. };
  96. struct
  97. {
  98. grub_usb_device_t usbdev;
  99. int configno;
  100. int interfno;
  101. char buf[64];
  102. int bufstart, bufend;
  103. struct grub_usb_desc_endp *in_endp;
  104. struct grub_usb_desc_endp *out_endp;
  105. };
  106. struct grub_escc_descriptor *escc_desc;
  107. #ifdef GRUB_MACHINE_IEEE1275
  108. struct
  109. {
  110. grub_ieee1275_ihandle_t handle;
  111. struct ofserial_hash_ent *elem;
  112. };
  113. #endif
  114. #ifdef GRUB_MACHINE_EFI
  115. struct grub_efi_serial_io_interface *interface;
  116. #endif
  117. #ifdef GRUB_MACHINE_ARC
  118. struct
  119. {
  120. grub_arc_fileno_t handle;
  121. int handle_valid;
  122. };
  123. #endif
  124. };
  125. grub_term_output_t term_out;
  126. grub_term_input_t term_in;
  127. };
  128. grub_err_t EXPORT_FUNC(grub_serial_register) (struct grub_serial_port *port);
  129. void EXPORT_FUNC(grub_serial_unregister) (struct grub_serial_port *port);
  130. /* Convenience functions to perform primitive operations on a port. */
  131. static inline grub_err_t
  132. grub_serial_port_configure (struct grub_serial_port *port,
  133. struct grub_serial_config *config)
  134. {
  135. return port->driver->configure (port, config);
  136. }
  137. static inline int
  138. grub_serial_port_fetch (struct grub_serial_port *port)
  139. {
  140. return port->driver->fetch (port);
  141. }
  142. static inline void
  143. grub_serial_port_put (struct grub_serial_port *port, const int c)
  144. {
  145. port->driver->put (port, c);
  146. }
  147. static inline void
  148. grub_serial_port_fini (struct grub_serial_port *port)
  149. {
  150. port->driver->fini (port);
  151. }
  152. /* Set default settings. */
  153. static inline grub_err_t
  154. grub_serial_config_defaults (struct grub_serial_port *port)
  155. {
  156. struct grub_serial_config config =
  157. {
  158. #ifdef GRUB_MACHINE_MIPS_LOONGSON
  159. .speed = 115200,
  160. /* On Loongson machines serial port has only 3 wires. */
  161. .rtscts = 0,
  162. #else
  163. .speed = 9600,
  164. .rtscts = 1,
  165. #endif
  166. .word_len = 8,
  167. .parity = GRUB_SERIAL_PARITY_NONE,
  168. .stop_bits = GRUB_SERIAL_STOP_BITS_1,
  169. .base_clock = 0
  170. };
  171. return port->driver->configure (port, &config);
  172. }
  173. #if defined(__mips__) || defined (__i386__) || defined (__x86_64__)
  174. void grub_ns8250_init (void);
  175. struct grub_serial_port *grub_ns8250_spcr_init (void);
  176. struct grub_serial_port *grub_serial_ns8250_add_port (grub_port_t port,
  177. struct grub_serial_config *config);
  178. struct grub_serial_port *grub_serial_ns8250_add_mmio (grub_addr_t addr,
  179. unsigned int acc_size,
  180. struct grub_serial_config *config);
  181. #endif
  182. #ifdef GRUB_MACHINE_IEEE1275
  183. void grub_ofserial_init (void);
  184. #endif
  185. #ifdef GRUB_MACHINE_EFI
  186. void
  187. grub_efiserial_init (void);
  188. #endif
  189. #ifdef GRUB_MACHINE_ARC
  190. void
  191. grub_arcserial_init (void);
  192. struct grub_serial_port *grub_arcserial_add_port (const char *path);
  193. #endif
  194. #if defined(__i386__) || defined(__x86_64__)
  195. extern void grub_pciserial_init (void);
  196. #endif
  197. struct grub_serial_port *grub_serial_find (const char *name);
  198. extern struct grub_serial_driver grub_ns8250_driver;
  199. void EXPORT_FUNC(grub_serial_unregister_driver) (struct grub_serial_driver *driver);
  200. #ifndef GRUB_MACHINE_EMU
  201. extern void grub_serial_init (void);
  202. extern void grub_serial_fini (void);
  203. #endif
  204. extern struct grub_serial_port *grub_serial_ports;
  205. #define FOR_SERIAL_PORTS(var) FOR_LIST_ELEMENTS((var), (grub_serial_ports))
  206. #endif