pl2303.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. /* Convert speed to divisor. */
  28. static grub_uint32_t
  29. is_speed_supported (unsigned int speed)
  30. {
  31. unsigned int i;
  32. unsigned int supported[] = { 2400, 4800, 9600, 19200, 38400, 57600, 115200};
  33. for (i = 0; i < ARRAY_SIZE (supported); i++)
  34. if (supported[i] == speed)
  35. return 1;
  36. return 0;
  37. }
  38. #define GRUB_PL2303_REQUEST_SET_CONFIG 0x20
  39. #define GRUB_PL2303_STOP_BITS_1 0x0
  40. #define GRUB_PL2303_STOP_BITS_1_5 0x1
  41. #define GRUB_PL2303_STOP_BITS_2 0x2
  42. #define GRUB_PL2303_PARITY_NONE 0
  43. #define GRUB_PL2303_PARITY_ODD 1
  44. #define GRUB_PL2303_PARITY_EVEN 2
  45. struct grub_pl2303_config
  46. {
  47. grub_uint32_t speed;
  48. grub_uint8_t stop_bits;
  49. grub_uint8_t parity;
  50. grub_uint8_t word_len;
  51. } GRUB_PACKED;
  52. static void
  53. real_config (struct grub_serial_port *port)
  54. {
  55. struct grub_pl2303_config config_pl2303;
  56. char xx;
  57. if (port->configured)
  58. return;
  59. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
  60. 1, 0x8484, 0, 1, &xx);
  61. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  62. 1, 0x0404, 0, 0, 0);
  63. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
  64. 1, 0x8484, 0, 1, &xx);
  65. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
  66. 1, 0x8383, 0, 1, &xx);
  67. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
  68. 1, 0x8484, 0, 1, &xx);
  69. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  70. 1, 0x0404, 1, 0, 0);
  71. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
  72. 1, 0x8484, 0, 1, &xx);
  73. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
  74. 1, 0x8383, 0, 1, &xx);
  75. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  76. 1, 0, 1, 0, 0);
  77. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  78. 1, 1, 0, 0, 0);
  79. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  80. 1, 2, 0x44, 0, 0);
  81. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  82. 1, 8, 0, 0, 0);
  83. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  84. 1, 9, 0, 0, 0);
  85. if (port->config.stop_bits == GRUB_SERIAL_STOP_BITS_2)
  86. config_pl2303.stop_bits = GRUB_PL2303_STOP_BITS_2;
  87. else if (port->config.stop_bits == GRUB_SERIAL_STOP_BITS_1_5)
  88. config_pl2303.stop_bits = GRUB_PL2303_STOP_BITS_1_5;
  89. else
  90. config_pl2303.stop_bits = GRUB_PL2303_STOP_BITS_1;
  91. switch (port->config.parity)
  92. {
  93. case GRUB_SERIAL_PARITY_NONE:
  94. config_pl2303.parity = GRUB_PL2303_PARITY_NONE;
  95. break;
  96. case GRUB_SERIAL_PARITY_ODD:
  97. config_pl2303.parity = GRUB_PL2303_PARITY_ODD;
  98. break;
  99. case GRUB_SERIAL_PARITY_EVEN:
  100. config_pl2303.parity = GRUB_PL2303_PARITY_EVEN;
  101. break;
  102. }
  103. config_pl2303.word_len = port->config.word_len;
  104. config_pl2303.speed = port->config.speed;
  105. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
  106. GRUB_PL2303_REQUEST_SET_CONFIG, 0, 0,
  107. sizeof (config_pl2303), (char *) &config_pl2303);
  108. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
  109. 0x22, 3, 0, 0, 0);
  110. grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
  111. 1, 0, port->config.rtscts ? 0x61 : 0, 0, 0);
  112. port->configured = 1;
  113. }
  114. /* Fetch a key. */
  115. static int
  116. pl2303_hw_fetch (struct grub_serial_port *port)
  117. {
  118. real_config (port);
  119. return grub_usbserial_fetch (port, 0);
  120. }
  121. /* Put a character. */
  122. static void
  123. pl2303_hw_put (struct grub_serial_port *port, const int c)
  124. {
  125. char cc = c;
  126. real_config (port);
  127. grub_usb_bulk_write (port->usbdev, port->out_endp, 1, &cc);
  128. }
  129. static grub_err_t
  130. pl2303_hw_configure (struct grub_serial_port *port,
  131. struct grub_serial_config *config)
  132. {
  133. if (!is_speed_supported (config->speed))
  134. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  135. N_("unsupported serial port speed"));
  136. if (config->parity != GRUB_SERIAL_PARITY_NONE
  137. && config->parity != GRUB_SERIAL_PARITY_ODD
  138. && config->parity != GRUB_SERIAL_PARITY_EVEN)
  139. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  140. N_("unsupported serial port parity"));
  141. if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
  142. && config->stop_bits != GRUB_SERIAL_STOP_BITS_1_5
  143. && config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
  144. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  145. N_("unsupported serial port stop bits number"));
  146. if (config->word_len < 5 || config->word_len > 8)
  147. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  148. N_("unsupported serial port word length"));
  149. port->config = *config;
  150. port->configured = 0;
  151. return GRUB_ERR_NONE;
  152. }
  153. static struct grub_serial_driver grub_pl2303_driver =
  154. {
  155. .configure = pl2303_hw_configure,
  156. .fetch = pl2303_hw_fetch,
  157. .put = pl2303_hw_put,
  158. .fini = grub_usbserial_fini
  159. };
  160. static const struct
  161. {
  162. grub_uint16_t vendor, product;
  163. } products[] =
  164. {
  165. {0x067b, 0x2303}
  166. };
  167. static int
  168. grub_pl2303_attach (grub_usb_device_t usbdev, int configno, int interfno)
  169. {
  170. unsigned j;
  171. for (j = 0; j < ARRAY_SIZE (products); j++)
  172. if (usbdev->descdev.vendorid == products[j].vendor
  173. && usbdev->descdev.prodid == products[j].product)
  174. break;
  175. if (j == ARRAY_SIZE (products))
  176. return 0;
  177. return grub_usbserial_attach (usbdev, configno, interfno,
  178. &grub_pl2303_driver,
  179. GRUB_USB_SERIAL_ENDPOINT_LAST_MATCHING,
  180. GRUB_USB_SERIAL_ENDPOINT_LAST_MATCHING);
  181. }
  182. static struct grub_usb_attach_desc attach_hook =
  183. {
  184. .class = 0xff,
  185. .hook = grub_pl2303_attach
  186. };
  187. GRUB_MOD_INIT(usbserial_pl2303)
  188. {
  189. grub_usb_register_attach_hook_class (&attach_hook);
  190. }
  191. GRUB_MOD_FINI(usbserial_pl2303)
  192. {
  193. grub_serial_unregister_driver (&grub_pl2303_driver);
  194. grub_usb_unregister_attach_hook_class (&attach_hook);
  195. }