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>
29
GRUB_FTDI_MODEM_CTRL = 0x01,
30
GRUB_FTDI_FLOW_CTRL = 0x02,
31
GRUB_FTDI_SPEED_CTRL = 0x03,
32
GRUB_FTDI_DATA_CTRL = 0x04
35
#define GRUB_FTDI_MODEM_CTRL_DTRRTS 3
36
#define GRUB_FTDI_FLOW_CTRL_DTRRTS 3
38
/* Convert speed to divisor. */
40
get_divisor (unsigned int speed)
44
/* The structure for speed vs. divisor. */
51
/* The table which lists common configurations. */
52
/* Computed with a division formula with 3MHz as base frequency. */
53
static struct divisor divisor_tab[] =
64
/* Set the baud rate. */
65
for (i = 0; i < ARRAY_SIZE (divisor_tab); i++)
66
if (divisor_tab[i].speed == speed)
67
return divisor_tab[i].div;
72
real_config (struct grub_serial_port *port)
74
grub_uint32_t divisor;
75
const grub_uint16_t parities[] = {
76
[GRUB_SERIAL_PARITY_NONE] = 0x0000,
77
[GRUB_SERIAL_PARITY_ODD] = 0x0100,
78
[GRUB_SERIAL_PARITY_EVEN] = 0x0200
80
const grub_uint16_t stop_bits[] = {
81
[GRUB_SERIAL_STOP_BITS_1] = 0x0000,
82
[GRUB_SERIAL_STOP_BITS_2] = 0x1000,
88
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
90
GRUB_FTDI_MODEM_CTRL_DTRRTS, 0, 0, 0);
92
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
94
GRUB_FTDI_FLOW_CTRL_DTRRTS, 0, 0, 0);
96
divisor = get_divisor (port->config.speed);
97
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
99
divisor & 0xffff, divisor >> 16, 0, 0);
101
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
103
parities[port->config.parity]
104
| stop_bits[port->config.stop_bits]
105
| port->config.word_len, 0, 0, 0);
107
port->configured = 1;
112
ftdi_hw_fetch (struct grub_serial_port *port)
116
return grub_usbserial_fetch (port, 2);
119
/* Put a character. */
121
ftdi_hw_put (struct grub_serial_port *port, const int c)
127
grub_usb_bulk_write (port->usbdev, port->out_endp->endp_addr, 1, &cc);
131
ftdi_hw_configure (struct grub_serial_port *port,
132
struct grub_serial_config *config)
134
grub_uint16_t divisor;
136
divisor = get_divisor (config->speed);
138
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad speed");
140
if (config->parity != GRUB_SERIAL_PARITY_NONE
141
&& config->parity != GRUB_SERIAL_PARITY_ODD
142
&& config->parity != GRUB_SERIAL_PARITY_EVEN)
143
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported parity");
145
if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
146
&& config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
147
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported stop bits");
149
if (config->word_len < 5 || config->word_len > 8)
150
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported word length");
152
port->config = *config;
153
port->configured = 0;
155
return GRUB_ERR_NONE;
158
static struct grub_serial_driver grub_ftdi_driver =
160
.configure = ftdi_hw_configure,
161
.fetch = ftdi_hw_fetch,
163
.fini = grub_usbserial_fini
168
grub_uint16_t vendor, product;
171
{0x0403, 0x6001} /* QEMU virtual USBserial. */
175
grub_ftdi_attach (grub_usb_device_t usbdev, int configno, int interfno)
179
for (j = 0; j < ARRAY_SIZE (products); j++)
180
if (usbdev->descdev.vendorid == products[j].vendor
181
&& usbdev->descdev.prodid == products[j].product)
183
if (j == ARRAY_SIZE (products))
186
return grub_usbserial_attach (usbdev, configno, interfno,
190
static struct grub_usb_attach_desc attach_hook =
193
.hook = grub_ftdi_attach
196
GRUB_MOD_INIT(usbserial_ftdi)
198
grub_usb_register_attach_hook_class (&attach_hook);
201
GRUB_MOD_FINI(usbserial_ftdi)
203
grub_serial_unregister_driver (&grub_ftdi_driver);
204
grub_usb_unregister_attach_hook_class (&attach_hook);