2
* GRUB -- GRand Unified Bootloader
3
* Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
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.
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.
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/>.
19
#include <grub/serial.h>
20
#include <grub/types.h>
22
#include <grub/misc.h>
25
#include <grub/usbserial.h>
27
/* Convert speed to divisor. */
29
is_speed_supported (unsigned int speed)
32
unsigned int supported[] = { 2400, 4800, 9600, 19200, 38400, 57600, 115200};
34
for (i = 0; i < ARRAY_SIZE (supported); i++)
35
if (supported[i] == speed)
40
#define GRUB_PL2303_REQUEST_SET_CONFIG 0x20
41
#define GRUB_PL2303_STOP_BITS_1 0x0
42
#define GRUB_PL2303_STOP_BITS_2 0x2
44
#define GRUB_PL2303_PARITY_NONE 0
45
#define GRUB_PL2303_PARITY_ODD 1
46
#define GRUB_PL2303_PARITY_EVEN 2
48
struct grub_pl2303_config
51
grub_uint8_t stop_bits;
53
grub_uint8_t word_len;
54
} __attribute__ ((packed));
57
real_config (struct grub_serial_port *port)
59
struct grub_pl2303_config config_pl2303;
65
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
66
1, 0x8484, 0, 1, &xx);
67
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
70
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
71
1, 0x8484, 0, 1, &xx);
72
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
73
1, 0x8383, 0, 1, &xx);
74
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
75
1, 0x8484, 0, 1, &xx);
76
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
79
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
80
1, 0x8484, 0, 1, &xx);
81
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_IN,
82
1, 0x8383, 0, 1, &xx);
84
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
86
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
88
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
90
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
92
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
95
if (port->config.stop_bits == GRUB_SERIAL_STOP_BITS_2)
96
config_pl2303.stop_bits = GRUB_PL2303_STOP_BITS_2;
98
config_pl2303.stop_bits = GRUB_PL2303_STOP_BITS_1;
100
switch (port->config.parity)
102
case GRUB_SERIAL_PARITY_NONE:
103
config_pl2303.parity = GRUB_PL2303_PARITY_NONE;
105
case GRUB_SERIAL_PARITY_ODD:
106
config_pl2303.parity = GRUB_PL2303_PARITY_ODD;
108
case GRUB_SERIAL_PARITY_EVEN:
109
config_pl2303.parity = GRUB_PL2303_PARITY_EVEN;
113
config_pl2303.word_len = port->config.word_len;
114
config_pl2303.speed = port->config.speed;
115
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
116
GRUB_PL2303_REQUEST_SET_CONFIG, 0, 0,
117
sizeof (config_pl2303), (char *) &config_pl2303);
118
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
121
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
123
port->configured = 1;
128
pl2303_hw_fetch (struct grub_serial_port *port)
132
return grub_usbserial_fetch (port, 0);
135
/* Put a character. */
137
pl2303_hw_put (struct grub_serial_port *port, const int c)
143
grub_usb_bulk_write (port->usbdev, port->out_endp->endp_addr, 1, &cc);
147
pl2303_hw_configure (struct grub_serial_port *port,
148
struct grub_serial_config *config)
150
if (!is_speed_supported (config->speed))
151
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad speed");
153
if (config->parity != GRUB_SERIAL_PARITY_NONE
154
&& config->parity != GRUB_SERIAL_PARITY_ODD
155
&& config->parity != GRUB_SERIAL_PARITY_EVEN)
156
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported parity");
158
if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
159
&& config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
160
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported stop bits");
162
if (config->word_len < 5 || config->word_len > 8)
163
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported word length");
165
port->config = *config;
166
port->configured = 0;
168
return GRUB_ERR_NONE;
171
static struct grub_serial_driver grub_pl2303_driver =
173
.configure = pl2303_hw_configure,
174
.fetch = pl2303_hw_fetch,
175
.put = pl2303_hw_put,
176
.fini = grub_usbserial_fini
181
grub_uint16_t vendor, product;
188
grub_pl2303_attach (grub_usb_device_t usbdev, int configno, int interfno)
192
for (j = 0; j < ARRAY_SIZE (products); j++)
193
if (usbdev->descdev.vendorid == products[j].vendor
194
&& usbdev->descdev.prodid == products[j].product)
196
if (j == ARRAY_SIZE (products))
199
return grub_usbserial_attach (usbdev, configno, interfno,
200
&grub_pl2303_driver);
203
static struct grub_usb_attach_desc attach_hook =
206
.hook = grub_pl2303_attach
209
GRUB_MOD_INIT(usbserial_pl2303)
211
grub_usb_register_attach_hook_class (&attach_hook);
214
GRUB_MOD_FINI(usbserial_pl2303)
216
grub_serial_unregister_driver (&grub_pl2303_driver);
217
grub_usb_unregister_attach_hook_class (&attach_hook);