ftdi.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 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/usb.h>
  24. #include <grub/usbserial.h>
  25. #include <grub/i18n.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. enum
  28. {
  29. GRUB_FTDI_MODEM_CTRL = 0x01,
  30. GRUB_FTDI_FLOW_CTRL = 0x02,
  31. GRUB_FTDI_SPEED_CTRL = 0x03,
  32. GRUB_FTDI_DATA_CTRL = 0x04
  33. };
  34. #define GRUB_FTDI_MODEM_CTRL_DTRRTS 3
  35. #define GRUB_FTDI_FLOW_CTRL_DTRRTS 3
  36. /* Convert speed to divisor. */
  37. static grub_uint32_t
  38. get_divisor (unsigned int speed)
  39. {
  40. unsigned int i;
  41. /* The structure for speed vs. divisor. */
  42. struct divisor
  43. {
  44. unsigned int speed;
  45. grub_uint32_t div;
  46. };
  47. /* The table which lists common configurations. */
  48. /* Computed with a division formula with 3MHz as base frequency. */
  49. static struct divisor divisor_tab[] =
  50. {
  51. { 2400, 0x04e2 },
  52. { 4800, 0x0271 },
  53. { 9600, 0x4138 },
  54. { 19200, 0x809c },
  55. { 38400, 0xc04e },
  56. { 57600, 0xc034 },
  57. { 115200, 0x001a }
  58. };
  59. /* Set the baud rate. */
  60. for (i = 0; i < ARRAY_SIZE (divisor_tab); i++)
  61. if (divisor_tab[i].speed == speed)
  62. return divisor_tab[i].div;
  63. return 0;
  64. }
  65. static void
  66. real_config (struct grub_serial_port *port)
  67. {
  68. grub_uint32_t divisor;
  69. const grub_uint16_t parities[] = {
  70. [GRUB_SERIAL_PARITY_NONE] = 0x0000,
  71. [GRUB_SERIAL_PARITY_ODD] = 0x0100,
  72. [GRUB_SERIAL_PARITY_EVEN] = 0x0200
  73. };
  74. const grub_uint16_t stop_bits[] = {
  75. [GRUB_SERIAL_STOP_BITS_1] = 0x0000,
  76. [GRUB_SERIAL_STOP_BITS_1_5] = 0x0800,
  77. [GRUB_SERIAL_STOP_BITS_2] = 0x1000,
  78. };
  79. if (port->configured)
  80. return;
  81. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  82. GRUB_FTDI_MODEM_CTRL,
  83. port->config.rtscts ? GRUB_FTDI_MODEM_CTRL_DTRRTS : 0,
  84. 0, 0, 0);
  85. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  86. GRUB_FTDI_FLOW_CTRL,
  87. port->config.rtscts ? GRUB_FTDI_FLOW_CTRL_DTRRTS : 0,
  88. 0, 0, 0);
  89. divisor = get_divisor (port->config.speed);
  90. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  91. GRUB_FTDI_SPEED_CTRL,
  92. divisor & 0xffff, divisor >> 16, 0, 0);
  93. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  94. GRUB_FTDI_DATA_CTRL,
  95. parities[port->config.parity]
  96. | stop_bits[port->config.stop_bits]
  97. | port->config.word_len, 0, 0, 0);
  98. port->configured = 1;
  99. }
  100. /* Fetch a key. */
  101. static int
  102. ftdi_hw_fetch (struct grub_serial_port *port)
  103. {
  104. real_config (port);
  105. return grub_usbserial_fetch (port, 2);
  106. }
  107. /* Put a character. */
  108. static void
  109. ftdi_hw_put (struct grub_serial_port *port, const int c)
  110. {
  111. char cc = c;
  112. real_config (port);
  113. grub_usb_bulk_write (port->usbdev, port->out_endp, 1, &cc);
  114. }
  115. static grub_err_t
  116. ftdi_hw_configure (struct grub_serial_port *port,
  117. struct grub_serial_config *config)
  118. {
  119. grub_uint16_t divisor;
  120. divisor = get_divisor (config->speed);
  121. if (divisor == 0)
  122. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  123. N_("unsupported serial port speed"));
  124. if (config->parity != GRUB_SERIAL_PARITY_NONE
  125. && config->parity != GRUB_SERIAL_PARITY_ODD
  126. && config->parity != GRUB_SERIAL_PARITY_EVEN)
  127. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  128. N_("unsupported serial port parity"));
  129. if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
  130. && config->stop_bits != GRUB_SERIAL_STOP_BITS_1_5
  131. && config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
  132. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  133. N_("unsupported serial port stop bits number"));
  134. if (config->word_len < 5 || config->word_len > 8)
  135. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  136. N_("unsupported serial port word length"));
  137. port->config = *config;
  138. port->configured = 0;
  139. return GRUB_ERR_NONE;
  140. }
  141. static struct grub_serial_driver grub_ftdi_driver =
  142. {
  143. .configure = ftdi_hw_configure,
  144. .fetch = ftdi_hw_fetch,
  145. .put = ftdi_hw_put,
  146. .fini = grub_usbserial_fini
  147. };
  148. static const struct
  149. {
  150. grub_uint16_t vendor, product;
  151. } products[] =
  152. {
  153. {0x0403, 0x6001} /* QEMU virtual USBserial. */
  154. };
  155. static int
  156. grub_ftdi_attach (grub_usb_device_t usbdev, int configno, int interfno)
  157. {
  158. unsigned j;
  159. for (j = 0; j < ARRAY_SIZE (products); j++)
  160. if (usbdev->descdev.vendorid == products[j].vendor
  161. && usbdev->descdev.prodid == products[j].product)
  162. break;
  163. if (j == ARRAY_SIZE (products))
  164. return 0;
  165. return grub_usbserial_attach (usbdev, configno, interfno,
  166. &grub_ftdi_driver,
  167. GRUB_USB_SERIAL_ENDPOINT_LAST_MATCHING,
  168. GRUB_USB_SERIAL_ENDPOINT_LAST_MATCHING);
  169. }
  170. static struct grub_usb_attach_desc attach_hook =
  171. {
  172. .class = 0xff,
  173. .hook = grub_ftdi_attach
  174. };
  175. GRUB_MOD_INIT(usbserial_ftdi)
  176. {
  177. grub_usb_register_attach_hook_class (&attach_hook);
  178. }
  179. GRUB_MOD_FINI(usbserial_ftdi)
  180. {
  181. grub_serial_unregister_driver (&grub_ftdi_driver);
  182. grub_usb_unregister_attach_hook_class (&attach_hook);
  183. }