2010-07-17 23:37:19 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2010-07-18 10:53:35 +00:00
|
|
|
* Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
|
2010-07-17 23:37:19 +00:00
|
|
|
*
|
|
|
|
* GRUB is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GRUB is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <grub/serial.h>
|
|
|
|
#include <grub/types.h>
|
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/misc.h>
|
2010-07-18 10:53:35 +00:00
|
|
|
#include <grub/mm.h>
|
|
|
|
#include <grub/usb.h>
|
2010-07-18 17:10:57 +00:00
|
|
|
#include <grub/usbserial.h>
|
2010-07-17 23:37:19 +00:00
|
|
|
|
2011-04-11 21:01:51 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2010-07-18 12:43:23 +00:00
|
|
|
enum
|
|
|
|
{
|
2010-07-18 15:40:42 +00:00
|
|
|
GRUB_FTDI_MODEM_CTRL = 0x01,
|
|
|
|
GRUB_FTDI_FLOW_CTRL = 0x02,
|
|
|
|
GRUB_FTDI_SPEED_CTRL = 0x03,
|
|
|
|
GRUB_FTDI_DATA_CTRL = 0x04
|
2010-07-18 12:43:23 +00:00
|
|
|
};
|
|
|
|
|
2010-07-18 15:40:42 +00:00
|
|
|
#define GRUB_FTDI_MODEM_CTRL_DTRRTS 3
|
|
|
|
#define GRUB_FTDI_FLOW_CTRL_DTRRTS 3
|
2010-07-18 12:43:23 +00:00
|
|
|
|
|
|
|
/* Convert speed to divisor. */
|
|
|
|
static grub_uint32_t
|
|
|
|
get_divisor (unsigned int speed)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* The structure for speed vs. divisor. */
|
|
|
|
struct divisor
|
|
|
|
{
|
|
|
|
unsigned int speed;
|
|
|
|
grub_uint32_t div;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The table which lists common configurations. */
|
2010-07-18 13:00:50 +00:00
|
|
|
/* Computed with a division formula with 3MHz as base frequency. */
|
2010-07-18 12:43:23 +00:00
|
|
|
static struct divisor divisor_tab[] =
|
|
|
|
{
|
2010-07-18 13:00:50 +00:00
|
|
|
{ 2400, 0x04e2 },
|
|
|
|
{ 4800, 0x0271 },
|
2010-07-18 12:43:23 +00:00
|
|
|
{ 9600, 0x4138 },
|
2010-07-18 13:00:50 +00:00
|
|
|
{ 19200, 0x809c },
|
|
|
|
{ 38400, 0xc04e },
|
|
|
|
{ 57600, 0xc034 },
|
|
|
|
{ 115200, 0x001a }
|
2010-07-18 12:43:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Set the baud rate. */
|
2010-07-18 13:00:50 +00:00
|
|
|
for (i = 0; i < ARRAY_SIZE (divisor_tab); i++)
|
2010-07-18 12:43:23 +00:00
|
|
|
if (divisor_tab[i].speed == speed)
|
|
|
|
return divisor_tab[i].div;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-18 11:40:48 +00:00
|
|
|
static void
|
|
|
|
real_config (struct grub_serial_port *port)
|
|
|
|
{
|
2010-07-18 12:43:23 +00:00
|
|
|
grub_uint32_t divisor;
|
|
|
|
const grub_uint16_t parities[] = {
|
|
|
|
[GRUB_SERIAL_PARITY_NONE] = 0x0000,
|
|
|
|
[GRUB_SERIAL_PARITY_ODD] = 0x0100,
|
|
|
|
[GRUB_SERIAL_PARITY_EVEN] = 0x0200
|
|
|
|
};
|
|
|
|
const grub_uint16_t stop_bits[] = {
|
|
|
|
[GRUB_SERIAL_STOP_BITS_1] = 0x0000,
|
|
|
|
[GRUB_SERIAL_STOP_BITS_2] = 0x1000,
|
|
|
|
};
|
|
|
|
|
2010-07-18 11:40:48 +00:00
|
|
|
if (port->configured)
|
|
|
|
return;
|
|
|
|
|
2010-07-18 12:43:23 +00:00
|
|
|
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
|
2010-07-18 15:40:42 +00:00
|
|
|
GRUB_FTDI_MODEM_CTRL,
|
|
|
|
GRUB_FTDI_MODEM_CTRL_DTRRTS, 0, 0, 0);
|
2010-07-18 12:43:23 +00:00
|
|
|
|
|
|
|
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
|
2010-07-18 15:40:42 +00:00
|
|
|
GRUB_FTDI_FLOW_CTRL,
|
|
|
|
GRUB_FTDI_FLOW_CTRL_DTRRTS, 0, 0, 0);
|
2010-07-18 12:43:23 +00:00
|
|
|
|
|
|
|
divisor = get_divisor (port->config.speed);
|
|
|
|
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
|
2010-07-18 15:40:42 +00:00
|
|
|
GRUB_FTDI_SPEED_CTRL,
|
2010-07-18 12:43:23 +00:00
|
|
|
divisor & 0xffff, divisor >> 16, 0, 0);
|
|
|
|
|
|
|
|
grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
|
2010-07-18 15:40:42 +00:00
|
|
|
GRUB_FTDI_DATA_CTRL,
|
2010-07-18 12:43:23 +00:00
|
|
|
parities[port->config.parity]
|
2010-07-18 13:07:59 +00:00
|
|
|
| stop_bits[port->config.stop_bits]
|
|
|
|
| port->config.word_len, 0, 0, 0);
|
2010-07-18 12:43:23 +00:00
|
|
|
|
2010-07-18 11:40:48 +00:00
|
|
|
port->configured = 1;
|
|
|
|
}
|
|
|
|
|
2010-07-17 23:37:19 +00:00
|
|
|
/* Fetch a key. */
|
|
|
|
static int
|
2010-07-18 15:40:42 +00:00
|
|
|
ftdi_hw_fetch (struct grub_serial_port *port)
|
2010-07-17 23:37:19 +00:00
|
|
|
{
|
2010-07-18 11:40:48 +00:00
|
|
|
real_config (port);
|
|
|
|
|
2010-07-19 06:43:01 +00:00
|
|
|
return grub_usbserial_fetch (port, 2);
|
2010-07-17 23:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Put a character. */
|
|
|
|
static void
|
2010-07-18 15:40:42 +00:00
|
|
|
ftdi_hw_put (struct grub_serial_port *port, const int c)
|
2010-07-17 23:37:19 +00:00
|
|
|
{
|
|
|
|
char cc = c;
|
2010-07-18 11:40:48 +00:00
|
|
|
|
|
|
|
real_config (port);
|
|
|
|
|
2010-07-18 10:53:35 +00:00
|
|
|
grub_usb_bulk_write (port->usbdev, port->out_endp->endp_addr, 1, &cc);
|
2010-07-17 23:37:19 +00:00
|
|
|
}
|
|
|
|
|
2010-07-18 10:53:35 +00:00
|
|
|
static grub_err_t
|
2010-07-18 15:40:42 +00:00
|
|
|
ftdi_hw_configure (struct grub_serial_port *port,
|
2010-07-18 11:40:48 +00:00
|
|
|
struct grub_serial_config *config)
|
2010-07-17 23:37:19 +00:00
|
|
|
{
|
2010-07-18 12:43:23 +00:00
|
|
|
grub_uint16_t divisor;
|
|
|
|
|
|
|
|
divisor = get_divisor (config->speed);
|
|
|
|
if (divisor == 0)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad speed");
|
|
|
|
|
|
|
|
if (config->parity != GRUB_SERIAL_PARITY_NONE
|
|
|
|
&& config->parity != GRUB_SERIAL_PARITY_ODD
|
|
|
|
&& config->parity != GRUB_SERIAL_PARITY_EVEN)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported parity");
|
|
|
|
|
|
|
|
if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
|
|
|
|
&& config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported stop bits");
|
|
|
|
|
2010-07-18 13:07:59 +00:00
|
|
|
if (config->word_len < 5 || config->word_len > 8)
|
|
|
|
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported word length");
|
|
|
|
|
2010-07-18 11:40:48 +00:00
|
|
|
port->config = *config;
|
2010-07-18 10:53:35 +00:00
|
|
|
port->configured = 0;
|
2010-07-17 23:37:19 +00:00
|
|
|
|
2010-07-18 10:53:35 +00:00
|
|
|
return GRUB_ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2010-07-18 15:40:42 +00:00
|
|
|
static struct grub_serial_driver grub_ftdi_driver =
|
2010-07-17 23:37:19 +00:00
|
|
|
{
|
2010-07-18 15:40:42 +00:00
|
|
|
.configure = ftdi_hw_configure,
|
|
|
|
.fetch = ftdi_hw_fetch,
|
|
|
|
.put = ftdi_hw_put,
|
2010-07-18 17:10:57 +00:00
|
|
|
.fini = grub_usbserial_fini
|
2010-07-17 23:37:19 +00:00
|
|
|
};
|
|
|
|
|
2010-07-18 14:31:42 +00:00
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
grub_uint16_t vendor, product;
|
|
|
|
} products[] =
|
|
|
|
{
|
|
|
|
{0x0403, 0x6001} /* QEMU virtual USBserial. */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2010-07-18 15:40:42 +00:00
|
|
|
grub_ftdi_attach (grub_usb_device_t usbdev, int configno, int interfno)
|
2010-07-18 14:31:42 +00:00
|
|
|
{
|
|
|
|
unsigned j;
|
|
|
|
|
|
|
|
for (j = 0; j < ARRAY_SIZE (products); j++)
|
|
|
|
if (usbdev->descdev.vendorid == products[j].vendor
|
|
|
|
&& usbdev->descdev.prodid == products[j].product)
|
|
|
|
break;
|
|
|
|
if (j == ARRAY_SIZE (products))
|
|
|
|
return 0;
|
|
|
|
|
2010-07-18 17:10:57 +00:00
|
|
|
return grub_usbserial_attach (usbdev, configno, interfno,
|
|
|
|
&grub_ftdi_driver);
|
2010-07-18 14:31:42 +00:00
|
|
|
}
|
|
|
|
|
2010-07-18 17:10:57 +00:00
|
|
|
static struct grub_usb_attach_desc attach_hook =
|
2010-07-17 23:37:19 +00:00
|
|
|
{
|
2010-07-18 10:53:35 +00:00
|
|
|
.class = 0xff,
|
2010-07-18 15:40:42 +00:00
|
|
|
.hook = grub_ftdi_attach
|
2010-07-18 10:53:35 +00:00
|
|
|
};
|
|
|
|
|
2010-07-18 14:31:42 +00:00
|
|
|
GRUB_MOD_INIT(usbserial_ftdi)
|
2010-07-18 10:53:35 +00:00
|
|
|
{
|
|
|
|
grub_usb_register_attach_hook_class (&attach_hook);
|
2010-07-17 23:37:19 +00:00
|
|
|
}
|
2010-07-18 15:40:42 +00:00
|
|
|
|
|
|
|
GRUB_MOD_FINI(usbserial_ftdi)
|
|
|
|
{
|
|
|
|
grub_serial_unregister_driver (&grub_ftdi_driver);
|
|
|
|
grub_usb_unregister_attach_hook_class (&attach_hook);
|
|
|
|
}
|