serial.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/serial.h>
  19. #include <grub/types.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/time.h>
  24. #include <grub/i18n.h>
  25. #include <grub/arc/arc.h>
  26. static void
  27. do_real_config (struct grub_serial_port *port)
  28. {
  29. char *name;
  30. if (port->configured)
  31. return;
  32. name = grub_arc_alt_name_to_norm (port->name, "");
  33. if (GRUB_ARC_FIRMWARE_VECTOR->open (name,GRUB_ARC_FILE_ACCESS_OPEN_RW,
  34. &port->handle))
  35. port->handle_valid = 0;
  36. else
  37. port->handle_valid = 1;
  38. port->configured = 1;
  39. }
  40. /* Fetch a key. */
  41. static int
  42. serial_hw_fetch (struct grub_serial_port *port)
  43. {
  44. unsigned long actual;
  45. char c;
  46. do_real_config (port);
  47. if (!port->handle_valid)
  48. return -1;
  49. if (GRUB_ARC_FIRMWARE_VECTOR->read (port->handle, &c,
  50. 1, &actual) || actual <= 0)
  51. return -1;
  52. return c;
  53. }
  54. /* Put a character. */
  55. static void
  56. serial_hw_put (struct grub_serial_port *port, const int c)
  57. {
  58. unsigned long actual;
  59. char c0 = c;
  60. do_real_config (port);
  61. if (!port->handle_valid)
  62. return;
  63. GRUB_ARC_FIRMWARE_VECTOR->write (port->handle, &c0,
  64. 1, &actual);
  65. }
  66. /* Initialize a serial device. PORT is the port number for a serial device.
  67. SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
  68. 19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
  69. for the device. Likewise, PARITY is the type of the parity and
  70. STOP_BIT_LEN is the length of the stop bit. The possible values for
  71. WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
  72. macros. */
  73. static grub_err_t
  74. serial_hw_configure (struct grub_serial_port *port __attribute__ ((unused)),
  75. struct grub_serial_config *config __attribute__ ((unused)))
  76. {
  77. /* FIXME: no ARC serial config available. */
  78. return GRUB_ERR_NONE;
  79. }
  80. struct grub_serial_driver grub_arcserial_driver =
  81. {
  82. .configure = serial_hw_configure,
  83. .fetch = serial_hw_fetch,
  84. .put = serial_hw_put
  85. };
  86. struct grub_serial_port *
  87. grub_arcserial_add_port (const char *path)
  88. {
  89. struct grub_serial_port *port;
  90. grub_err_t err;
  91. FOR_SERIAL_PORTS (port)
  92. if (grub_strcmp(path, port->name) == 0)
  93. return port;
  94. port = grub_zalloc (sizeof (*port));
  95. if (!port)
  96. return NULL;
  97. port->name = grub_strdup (path);
  98. if (!port->name)
  99. return NULL;
  100. port->driver = &grub_arcserial_driver;
  101. err = grub_serial_config_defaults (port);
  102. if (err)
  103. grub_print_error ();
  104. grub_serial_register (port);
  105. return port;
  106. }
  107. static int
  108. dev_iterate (const char *name,
  109. const struct grub_arc_component *comp __attribute__ ((unused)),
  110. void *data __attribute__ ((unused)))
  111. {
  112. /* We should check consolein/consoleout flags as
  113. well but some implementations are buggy. */
  114. if ((comp->flags & (GRUB_ARC_COMPONENT_FLAG_IN | GRUB_ARC_COMPONENT_FLAG_OUT))
  115. != (GRUB_ARC_COMPONENT_FLAG_IN | GRUB_ARC_COMPONENT_FLAG_OUT))
  116. return 0;
  117. if (!grub_arc_is_device_serial (name, 1))
  118. return 0;
  119. grub_arcserial_add_port (name);
  120. return 0;
  121. }
  122. void
  123. grub_arcserial_init (void)
  124. {
  125. grub_arc_iterate_devs (dev_iterate, 0, 1);
  126. }