merge with mainline
This commit is contained in:
commit
d84666e6bb
37 changed files with 2533 additions and 641 deletions
253
grub-core/term/ns8250.c
Normal file
253
grub-core/term/ns8250.c
Normal file
|
@ -0,0 +1,253 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
|
||||
*
|
||||
* 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/machine/memory.h>
|
||||
#include <grub/serial.h>
|
||||
#include <grub/ns8250.h>
|
||||
#include <grub/types.h>
|
||||
#include <grub/dl.h>
|
||||
#include <grub/misc.h>
|
||||
#include <grub/cpu/io.h>
|
||||
#include <grub/mm.h>
|
||||
|
||||
#ifdef GRUB_MACHINE_PCBIOS
|
||||
static const unsigned short *serial_hw_io_addr = (const unsigned short *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR;
|
||||
#define GRUB_SERIAL_PORT_NUM 4
|
||||
#else
|
||||
#include <grub/machine/serial.h>
|
||||
static const grub_port_t serial_hw_io_addr[] = GRUB_MACHINE_SERIAL_PORTS;
|
||||
#define GRUB_SERIAL_PORT_NUM (ARRAY_SIZE(serial_hw_io_addr))
|
||||
#endif
|
||||
|
||||
/* Convert speed to divisor. */
|
||||
static unsigned short
|
||||
serial_get_divisor (unsigned int speed)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
/* The structure for speed vs. divisor. */
|
||||
struct divisor
|
||||
{
|
||||
unsigned int speed;
|
||||
unsigned short div;
|
||||
};
|
||||
|
||||
/* The table which lists common configurations. */
|
||||
/* 1843200 / (speed * 16) */
|
||||
static struct divisor divisor_tab[] =
|
||||
{
|
||||
{ 2400, 0x0030 },
|
||||
{ 4800, 0x0018 },
|
||||
{ 9600, 0x000C },
|
||||
{ 19200, 0x0006 },
|
||||
{ 38400, 0x0003 },
|
||||
{ 57600, 0x0002 },
|
||||
{ 115200, 0x0001 }
|
||||
};
|
||||
|
||||
/* Set the baud rate. */
|
||||
for (i = 0; i < ARRAY_SIZE (divisor_tab); i++)
|
||||
if (divisor_tab[i].speed == speed)
|
||||
/* UART in Yeeloong runs twice the usual rate. */
|
||||
#ifdef GRUB_MACHINE_MIPS_YEELOONG
|
||||
return 2 * divisor_tab[i].div;
|
||||
#else
|
||||
return divisor_tab[i].div;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
do_real_config (struct grub_serial_port *port)
|
||||
{
|
||||
int divisor;
|
||||
unsigned char status = 0;
|
||||
const unsigned char parities[] = {
|
||||
[GRUB_SERIAL_PARITY_NONE] = UART_NO_PARITY,
|
||||
[GRUB_SERIAL_PARITY_ODD] = UART_ODD_PARITY,
|
||||
[GRUB_SERIAL_PARITY_EVEN] = UART_EVEN_PARITY
|
||||
};
|
||||
const unsigned char stop_bits[] = {
|
||||
[GRUB_SERIAL_STOP_BITS_1] = UART_1_STOP_BIT,
|
||||
[GRUB_SERIAL_STOP_BITS_2] = UART_2_STOP_BITS,
|
||||
};
|
||||
|
||||
if (port->configured)
|
||||
return;
|
||||
|
||||
divisor = serial_get_divisor (port->config.speed);
|
||||
|
||||
/* Turn off the interrupt. */
|
||||
grub_outb (0, port->port + UART_IER);
|
||||
|
||||
/* Set DLAB. */
|
||||
grub_outb (UART_DLAB, port->port + UART_LCR);
|
||||
|
||||
/* Set the baud rate. */
|
||||
grub_outb (divisor & 0xFF, port->port + UART_DLL);
|
||||
grub_outb (divisor >> 8, port->port + UART_DLH);
|
||||
|
||||
/* Set the line status. */
|
||||
status |= (parities[port->config.parity]
|
||||
| (port->config.word_len - 5)
|
||||
| stop_bits[port->config.stop_bits]);
|
||||
grub_outb (status, port->port + UART_LCR);
|
||||
|
||||
/* In Yeeloong serial port has only 3 wires. */
|
||||
#ifndef GRUB_MACHINE_MIPS_YEELOONG
|
||||
/* Enable the FIFO. */
|
||||
grub_outb (UART_ENABLE_FIFO_TRIGGER1, port->port + UART_FCR);
|
||||
|
||||
/* Turn on DTR and RTS. */
|
||||
grub_outb (UART_ENABLE_DTRRTS, port->port + UART_MCR);
|
||||
#else
|
||||
/* Enable the FIFO. */
|
||||
grub_outb (UART_ENABLE_FIFO_TRIGGER14, port->port + UART_FCR);
|
||||
|
||||
/* Turn on DTR, RTS, and OUT2. */
|
||||
grub_outb (UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, port->port + UART_MCR);
|
||||
#endif
|
||||
|
||||
/* Drain the input buffer. */
|
||||
while (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
|
||||
grub_inb (port->port + UART_RX);
|
||||
|
||||
port->configured = 1;
|
||||
}
|
||||
|
||||
/* Fetch a key. */
|
||||
static int
|
||||
serial_hw_fetch (struct grub_serial_port *port)
|
||||
{
|
||||
do_real_config (port);
|
||||
if (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
|
||||
return grub_inb (port->port + UART_RX);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Put a character. */
|
||||
static void
|
||||
serial_hw_put (struct grub_serial_port *port, const int c)
|
||||
{
|
||||
unsigned int timeout = 100000;
|
||||
|
||||
do_real_config (port);
|
||||
|
||||
/* Wait until the transmitter holding register is empty. */
|
||||
while ((grub_inb (port->port + UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
|
||||
{
|
||||
if (--timeout == 0)
|
||||
/* There is something wrong. But what can I do? */
|
||||
return;
|
||||
}
|
||||
|
||||
grub_outb (c, port->port + UART_TX);
|
||||
}
|
||||
|
||||
/* Initialize a serial device. PORT is the port number for a serial device.
|
||||
SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
|
||||
19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
|
||||
for the device. Likewise, PARITY is the type of the parity and
|
||||
STOP_BIT_LEN is the length of the stop bit. The possible values for
|
||||
WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
|
||||
macros. */
|
||||
static grub_err_t
|
||||
serial_hw_configure (struct grub_serial_port *port,
|
||||
struct grub_serial_config *config)
|
||||
{
|
||||
unsigned short divisor;
|
||||
|
||||
divisor = serial_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");
|
||||
|
||||
if (config->word_len < 5 || config->word_len > 8)
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported word length");
|
||||
|
||||
port->config = *config;
|
||||
port->configured = 0;
|
||||
|
||||
/* FIXME: should check if the serial terminal was found. */
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
struct grub_serial_driver grub_ns8250_driver =
|
||||
{
|
||||
.configure = serial_hw_configure,
|
||||
.fetch = serial_hw_fetch,
|
||||
.put = serial_hw_put
|
||||
};
|
||||
|
||||
static char com_names[GRUB_SERIAL_PORT_NUM][20];
|
||||
static struct grub_serial_port com_ports[GRUB_SERIAL_PORT_NUM];
|
||||
|
||||
void
|
||||
grub_ns8250_init (void)
|
||||
{
|
||||
unsigned i;
|
||||
for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
|
||||
if (serial_hw_io_addr[i])
|
||||
{
|
||||
grub_err_t err;
|
||||
grub_snprintf (com_names[i], sizeof (com_names[i]), "com%d", i);
|
||||
com_ports[i].name = com_names[i];
|
||||
com_ports[i].driver = &grub_ns8250_driver;
|
||||
com_ports[i].port = serial_hw_io_addr[i];
|
||||
err = grub_serial_config_defaults (&com_ports[i]);
|
||||
if (err)
|
||||
grub_print_error ();
|
||||
|
||||
grub_serial_register (&com_ports[i]);
|
||||
}
|
||||
}
|
||||
|
||||
char *
|
||||
grub_serial_ns8250_add_port (grub_port_t port)
|
||||
{
|
||||
struct grub_serial_port *p;
|
||||
unsigned i;
|
||||
for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
|
||||
if (com_ports[i].port == port)
|
||||
return com_names[i];
|
||||
p = grub_malloc (sizeof (*p));
|
||||
if (!p)
|
||||
return NULL;
|
||||
p->name = grub_xasprintf ("port%lx", (unsigned long) port);
|
||||
if (!p->name)
|
||||
{
|
||||
grub_free (p);
|
||||
return NULL;
|
||||
}
|
||||
p->driver = &grub_ns8250_driver;
|
||||
grub_serial_config_defaults (p);
|
||||
p->port = port;
|
||||
grub_serial_register (p);
|
||||
|
||||
return p->name;
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* GRUB -- GRand Unified Bootloader
|
||||
* Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
|
||||
* Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
|
||||
*
|
||||
* GRUB is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
@ -16,7 +16,6 @@
|
|||
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <grub/machine/memory.h>
|
||||
#include <grub/serial.h>
|
||||
#include <grub/term.h>
|
||||
#include <grub/types.h>
|
||||
|
@ -26,8 +25,9 @@
|
|||
#include <grub/cpu/io.h>
|
||||
#include <grub/extcmd.h>
|
||||
#include <grub/i18n.h>
|
||||
#include <grub/list.h>
|
||||
|
||||
static unsigned int registered = 0;
|
||||
#define FOR_SERIAL_PORTS(var) FOR_LIST_ELEMENTS((var), (grub_serial_ports))
|
||||
|
||||
/* Argument options. */
|
||||
static const struct grub_arg_option options[] =
|
||||
|
@ -41,154 +41,19 @@ static const struct grub_arg_option options[] =
|
|||
{0, 0, 0, 0, 0, 0}
|
||||
};
|
||||
|
||||
/* Serial port settings. */
|
||||
struct serial_port
|
||||
struct grub_serial_port *grub_serial_ports;
|
||||
|
||||
struct grub_serial_output_state
|
||||
{
|
||||
grub_port_t port;
|
||||
unsigned short divisor;
|
||||
unsigned short word_len;
|
||||
unsigned int parity;
|
||||
unsigned short stop_bits;
|
||||
struct grub_terminfo_output_state tinfo;
|
||||
struct grub_serial_port *port;
|
||||
};
|
||||
|
||||
/* Serial port settings. */
|
||||
static struct serial_port serial_settings;
|
||||
|
||||
#ifdef GRUB_MACHINE_PCBIOS
|
||||
static const unsigned short *serial_hw_io_addr = (const unsigned short *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR;
|
||||
#define GRUB_SERIAL_PORT_NUM 4
|
||||
#else
|
||||
#include <grub/machine/serial.h>
|
||||
static const grub_port_t serial_hw_io_addr[] = GRUB_MACHINE_SERIAL_PORTS;
|
||||
#define GRUB_SERIAL_PORT_NUM (ARRAY_SIZE(serial_hw_io_addr))
|
||||
#endif
|
||||
|
||||
/* Return the port number for the UNITth serial device. */
|
||||
static inline grub_port_t
|
||||
serial_hw_get_port (const unsigned int unit)
|
||||
struct grub_serial_input_state
|
||||
{
|
||||
if (unit < GRUB_SERIAL_PORT_NUM)
|
||||
return serial_hw_io_addr[unit];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Fetch a key. */
|
||||
static int
|
||||
serial_hw_fetch (void)
|
||||
{
|
||||
if (grub_inb (serial_settings.port + UART_LSR) & UART_DATA_READY)
|
||||
return grub_inb (serial_settings.port + UART_RX);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Put a character. */
|
||||
static void
|
||||
serial_hw_put (const int c)
|
||||
{
|
||||
unsigned int timeout = 100000;
|
||||
|
||||
/* Wait until the transmitter holding register is empty. */
|
||||
while ((grub_inb (serial_settings.port + UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
|
||||
{
|
||||
if (--timeout == 0)
|
||||
/* There is something wrong. But what can I do? */
|
||||
return;
|
||||
}
|
||||
|
||||
grub_outb (c, serial_settings.port + UART_TX);
|
||||
}
|
||||
|
||||
/* Convert speed to divisor. */
|
||||
static unsigned short
|
||||
serial_get_divisor (unsigned int speed)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
/* The structure for speed vs. divisor. */
|
||||
struct divisor
|
||||
{
|
||||
unsigned int speed;
|
||||
unsigned short div;
|
||||
};
|
||||
|
||||
/* The table which lists common configurations. */
|
||||
/* 1843200 / (speed * 16) */
|
||||
static struct divisor divisor_tab[] =
|
||||
{
|
||||
{ 2400, 0x0030 },
|
||||
{ 4800, 0x0018 },
|
||||
{ 9600, 0x000C },
|
||||
{ 19200, 0x0006 },
|
||||
{ 38400, 0x0003 },
|
||||
{ 57600, 0x0002 },
|
||||
{ 115200, 0x0001 }
|
||||
};
|
||||
|
||||
/* Set the baud rate. */
|
||||
for (i = 0; i < sizeof (divisor_tab) / sizeof (divisor_tab[0]); i++)
|
||||
if (divisor_tab[i].speed == speed)
|
||||
/* UART in Yeeloong runs twice the usual rate. */
|
||||
#ifdef GRUB_MACHINE_MIPS_YEELOONG
|
||||
return 2 * divisor_tab[i].div;
|
||||
#else
|
||||
return divisor_tab[i].div;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Initialize a serial device. PORT is the port number for a serial device.
|
||||
SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
|
||||
19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
|
||||
for the device. Likewise, PARITY is the type of the parity and
|
||||
STOP_BIT_LEN is the length of the stop bit. The possible values for
|
||||
WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
|
||||
macros. */
|
||||
static grub_err_t
|
||||
serial_hw_init (void)
|
||||
{
|
||||
unsigned char status = 0;
|
||||
|
||||
/* Turn off the interrupt. */
|
||||
grub_outb (0, serial_settings.port + UART_IER);
|
||||
|
||||
/* Set DLAB. */
|
||||
grub_outb (UART_DLAB, serial_settings.port + UART_LCR);
|
||||
|
||||
/* Set the baud rate. */
|
||||
grub_outb (serial_settings.divisor & 0xFF, serial_settings.port + UART_DLL);
|
||||
grub_outb (serial_settings.divisor >> 8, serial_settings.port + UART_DLH);
|
||||
|
||||
/* Set the line status. */
|
||||
status |= (serial_settings.parity
|
||||
| serial_settings.word_len
|
||||
| serial_settings.stop_bits);
|
||||
grub_outb (status, serial_settings.port + UART_LCR);
|
||||
|
||||
/* In Yeeloong serial port has only 3 wires. */
|
||||
#ifndef GRUB_MACHINE_MIPS_YEELOONG
|
||||
/* Enable the FIFO. */
|
||||
grub_outb (UART_ENABLE_FIFO_TRIGGER1, serial_settings.port + UART_FCR);
|
||||
|
||||
/* Turn on DTR and RTS. */
|
||||
grub_outb (UART_ENABLE_DTRRTS, serial_settings.port + UART_MCR);
|
||||
#else
|
||||
/* Enable the FIFO. */
|
||||
grub_outb (UART_ENABLE_FIFO_TRIGGER14, serial_settings.port + UART_FCR);
|
||||
|
||||
/* Turn on DTR, RTS, and OUT2. */
|
||||
grub_outb (UART_ENABLE_DTRRTS | UART_ENABLE_OUT2,
|
||||
serial_settings.port + UART_MCR);
|
||||
#endif
|
||||
|
||||
/* Drain the input buffer. */
|
||||
while (serial_hw_fetch () != -1);
|
||||
|
||||
/* FIXME: should check if the serial terminal was found. */
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
struct grub_terminfo_input_state tinfo;
|
||||
struct grub_serial_port *port;
|
||||
};
|
||||
|
||||
static grub_uint16_t
|
||||
grub_serial_getwh (struct grub_term_output *term __attribute__ ((unused)))
|
||||
|
@ -198,16 +63,38 @@ grub_serial_getwh (struct grub_term_output *term __attribute__ ((unused)))
|
|||
return (TEXT_WIDTH << 8) | TEXT_HEIGHT;
|
||||
}
|
||||
|
||||
struct grub_terminfo_input_state grub_serial_terminfo_input =
|
||||
static void
|
||||
serial_put (grub_term_output_t term, const int c)
|
||||
{
|
||||
struct grub_serial_output_state *data = term->data;
|
||||
data->port->driver->put (data->port, c);
|
||||
}
|
||||
|
||||
static int
|
||||
serial_fetch (grub_term_input_t term)
|
||||
{
|
||||
struct grub_serial_input_state *data = term->data;
|
||||
return data->port->driver->fetch (data->port);
|
||||
}
|
||||
|
||||
struct grub_serial_input_state grub_serial_terminfo_input =
|
||||
{
|
||||
.readkey = serial_hw_fetch
|
||||
.tinfo =
|
||||
{
|
||||
.readkey = serial_fetch
|
||||
}
|
||||
};
|
||||
|
||||
struct grub_terminfo_output_state grub_serial_terminfo_output =
|
||||
struct grub_serial_output_state grub_serial_terminfo_output =
|
||||
{
|
||||
.put = serial_hw_put
|
||||
.tinfo =
|
||||
{
|
||||
.put = serial_put
|
||||
}
|
||||
};
|
||||
|
||||
int registered = 0;
|
||||
|
||||
static struct grub_term_input grub_serial_term_input =
|
||||
{
|
||||
.name = "serial",
|
||||
|
@ -235,120 +122,219 @@ static struct grub_term_output grub_serial_term_output =
|
|||
|
||||
|
||||
|
||||
static struct grub_serial_port *
|
||||
grub_serial_find (char *name)
|
||||
{
|
||||
struct grub_serial_port *port;
|
||||
|
||||
FOR_SERIAL_PORTS (port)
|
||||
if (grub_strcmp (port->name, name) == 0)
|
||||
break;
|
||||
|
||||
#ifndef GRUB_MACHINE_EMU
|
||||
if (!port && grub_memcmp (name, "port", sizeof ("port") - 1) == 0
|
||||
&& grub_isdigit (name [sizeof ("port") - 1]))
|
||||
{
|
||||
name = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
|
||||
0, 16));
|
||||
if (!name)
|
||||
return NULL;
|
||||
|
||||
FOR_SERIAL_PORTS (port)
|
||||
if (grub_strcmp (port->name, name) == 0)
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
return port;
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_cmd_serial (grub_extcmd_t cmd,
|
||||
int argc __attribute__ ((unused)),
|
||||
char **args __attribute__ ((unused)))
|
||||
grub_cmd_serial (grub_extcmd_t cmd, int argc, char **args)
|
||||
{
|
||||
struct grub_arg_list *state = cmd->state;
|
||||
struct serial_port backup_settings = serial_settings;
|
||||
grub_err_t hwiniterr;
|
||||
char pname[40];
|
||||
char *name = NULL;
|
||||
struct grub_serial_port *port;
|
||||
struct grub_serial_config config;
|
||||
grub_err_t err;
|
||||
|
||||
if (state[0].set)
|
||||
{
|
||||
unsigned int unit;
|
||||
|
||||
unit = grub_strtoul (state[0].arg, 0, 0);
|
||||
serial_settings.port = serial_hw_get_port (unit);
|
||||
if (!serial_settings.port)
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad unit number");
|
||||
grub_snprintf (pname, sizeof (pname), "com%ld",
|
||||
grub_strtoul (state[0].arg, 0, 0));
|
||||
name = pname;
|
||||
}
|
||||
|
||||
if (state[1].set)
|
||||
serial_settings.port = (grub_port_t) grub_strtoul (state[1].arg, 0, 0);
|
||||
{
|
||||
grub_snprintf (pname, sizeof (pname), "port%lx",
|
||||
grub_strtoul (state[1].arg, 0, 0));
|
||||
name = pname;
|
||||
}
|
||||
|
||||
if (argc >= 1)
|
||||
name = args[0];
|
||||
|
||||
if (!name)
|
||||
name = "com0";
|
||||
|
||||
port = grub_serial_find (name);
|
||||
if (!port)
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown serial port");
|
||||
|
||||
config = port->config;
|
||||
|
||||
if (state[2].set)
|
||||
{
|
||||
unsigned long speed;
|
||||
|
||||
speed = grub_strtoul (state[2].arg, 0, 0);
|
||||
serial_settings.divisor = serial_get_divisor ((unsigned int) speed);
|
||||
if (serial_settings.divisor == 0)
|
||||
{
|
||||
serial_settings = backup_settings;
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad speed");
|
||||
}
|
||||
}
|
||||
config.speed = grub_strtoul (state[2].arg, 0, 0);
|
||||
|
||||
if (state[3].set)
|
||||
{
|
||||
if (! grub_strcmp (state[3].arg, "5"))
|
||||
serial_settings.word_len = UART_5BITS_WORD;
|
||||
else if (! grub_strcmp (state[3].arg, "6"))
|
||||
serial_settings.word_len = UART_6BITS_WORD;
|
||||
else if (! grub_strcmp (state[3].arg, "7"))
|
||||
serial_settings.word_len = UART_7BITS_WORD;
|
||||
else if (! grub_strcmp (state[3].arg, "8"))
|
||||
serial_settings.word_len = UART_8BITS_WORD;
|
||||
else
|
||||
{
|
||||
serial_settings = backup_settings;
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad word length");
|
||||
}
|
||||
}
|
||||
config.word_len = grub_strtoul (state[3].arg, 0, 0);
|
||||
|
||||
if (state[4].set)
|
||||
{
|
||||
if (! grub_strcmp (state[4].arg, "no"))
|
||||
serial_settings.parity = UART_NO_PARITY;
|
||||
config.parity = GRUB_SERIAL_PARITY_NONE;
|
||||
else if (! grub_strcmp (state[4].arg, "odd"))
|
||||
serial_settings.parity = UART_ODD_PARITY;
|
||||
config.parity = GRUB_SERIAL_PARITY_ODD;
|
||||
else if (! grub_strcmp (state[4].arg, "even"))
|
||||
serial_settings.parity = UART_EVEN_PARITY;
|
||||
config.parity = GRUB_SERIAL_PARITY_EVEN;
|
||||
else
|
||||
{
|
||||
serial_settings = backup_settings;
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad parity");
|
||||
}
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad parity");
|
||||
}
|
||||
|
||||
if (state[5].set)
|
||||
{
|
||||
if (! grub_strcmp (state[5].arg, "1"))
|
||||
serial_settings.stop_bits = UART_1_STOP_BIT;
|
||||
config.stop_bits = GRUB_SERIAL_STOP_BITS_1;
|
||||
else if (! grub_strcmp (state[5].arg, "2"))
|
||||
serial_settings.stop_bits = UART_2_STOP_BITS;
|
||||
config.stop_bits = GRUB_SERIAL_STOP_BITS_2;
|
||||
else
|
||||
{
|
||||
serial_settings = backup_settings;
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad number of stop bits");
|
||||
}
|
||||
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad number of stop bits");
|
||||
}
|
||||
|
||||
/* Initialize with new settings. */
|
||||
hwiniterr = serial_hw_init ();
|
||||
|
||||
if (hwiniterr == GRUB_ERR_NONE)
|
||||
err = port->driver->configure (port, &config);
|
||||
if (err)
|
||||
return err;
|
||||
#ifndef GRUB_MACHINE_EMU
|
||||
/* Compatibility kludge. */
|
||||
if (port->driver == &grub_ns8250_driver)
|
||||
{
|
||||
/* Register terminal if not yet registered. */
|
||||
if (registered == 0)
|
||||
if (!registered)
|
||||
{
|
||||
grub_term_register_input ("serial", &grub_serial_term_input);
|
||||
grub_term_register_output ("serial", &grub_serial_term_output);
|
||||
grub_terminfo_output_register (&grub_serial_term_output, "vt100");
|
||||
registered = 1;
|
||||
}
|
||||
grub_serial_terminfo_output.port = port;
|
||||
grub_serial_terminfo_input.port = port;
|
||||
registered = 1;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
grub_err_t
|
||||
grub_serial_register (struct grub_serial_port *port)
|
||||
{
|
||||
struct grub_term_input *in;
|
||||
struct grub_term_output *out;
|
||||
struct grub_serial_input_state *indata;
|
||||
struct grub_serial_output_state *outdata;
|
||||
|
||||
in = grub_malloc (sizeof (*in));
|
||||
if (!in)
|
||||
return grub_errno;
|
||||
|
||||
indata = grub_malloc (sizeof (*indata));
|
||||
if (!indata)
|
||||
{
|
||||
/* Initialization with new settings failed. */
|
||||
if (registered == 1)
|
||||
{
|
||||
/* If the terminal is registered, attempt to restore previous
|
||||
settings. */
|
||||
serial_settings = backup_settings;
|
||||
if (serial_hw_init () != GRUB_ERR_NONE)
|
||||
{
|
||||
/* If unable to restore settings, unregister terminal. */
|
||||
grub_term_unregister_input (&grub_serial_term_input);
|
||||
grub_term_unregister_output (&grub_serial_term_output);
|
||||
grub_terminfo_output_unregister (&grub_serial_term_output);
|
||||
registered = 0;
|
||||
}
|
||||
}
|
||||
grub_free (in);
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
return hwiniterr;
|
||||
grub_memcpy (in, &grub_serial_term_input, sizeof (*in));
|
||||
in->data = indata;
|
||||
in->name = grub_xasprintf ("serial_%s", port->name);
|
||||
grub_memcpy (indata, &grub_serial_terminfo_input, sizeof (*indata));
|
||||
|
||||
if (!in->name)
|
||||
{
|
||||
grub_free (in);
|
||||
grub_free (indata);
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
out = grub_malloc (sizeof (*out));
|
||||
if (!out)
|
||||
{
|
||||
grub_free (in);
|
||||
grub_free (indata);
|
||||
grub_free ((char *) in->name);
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
outdata = grub_malloc (sizeof (*outdata));
|
||||
if (!outdata)
|
||||
{
|
||||
grub_free (in);
|
||||
grub_free (indata);
|
||||
grub_free ((char *) in->name);
|
||||
grub_free (out);
|
||||
return grub_errno;
|
||||
}
|
||||
|
||||
grub_memcpy (out, &grub_serial_term_output, sizeof (*out));
|
||||
out->data = outdata;
|
||||
out->name = in->name;
|
||||
grub_memcpy (outdata, &grub_serial_terminfo_output, sizeof (*outdata));
|
||||
|
||||
grub_list_push (GRUB_AS_LIST_P (&grub_serial_ports), GRUB_AS_LIST (port));
|
||||
((struct grub_serial_input_state *) in->data)->port = port;
|
||||
((struct grub_serial_output_state *) out->data)->port = port;
|
||||
port->term_in = in;
|
||||
port->term_out = out;
|
||||
grub_terminfo_output_register (out, "vt100");
|
||||
#ifdef GRUB_MACHINE_MIPS_YEELOONG
|
||||
if (grub_strcmp (port->name, "com0") == 0)
|
||||
{
|
||||
grub_term_register_input_active ("serial_*", in);
|
||||
grub_term_register_output_active ("serial_*", out);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
grub_term_register_input ("serial_*", in);
|
||||
grub_term_register_output ("serial_*", out);
|
||||
}
|
||||
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
void
|
||||
grub_serial_unregister (struct grub_serial_port *port)
|
||||
{
|
||||
if (port->driver->fini)
|
||||
port->driver->fini (port);
|
||||
|
||||
if (port->term_in)
|
||||
grub_term_unregister_input (port->term_in);
|
||||
if (port->term_out)
|
||||
grub_term_unregister_output (port->term_out);
|
||||
|
||||
grub_list_remove (GRUB_AS_LIST_P (&grub_serial_ports), GRUB_AS_LIST (port));
|
||||
}
|
||||
|
||||
void
|
||||
grub_serial_unregister_driver (struct grub_serial_driver *driver)
|
||||
{
|
||||
struct grub_serial_port *port, *next;
|
||||
for (port = grub_serial_ports; port; port = next)
|
||||
{
|
||||
next = port->next;
|
||||
if (port->driver == driver)
|
||||
grub_serial_unregister (port);
|
||||
}
|
||||
}
|
||||
|
||||
static grub_extcmd_t cmd;
|
||||
|
@ -359,41 +345,19 @@ GRUB_MOD_INIT(serial)
|
|||
GRUB_COMMAND_FLAG_BOTH,
|
||||
N_("[OPTIONS...]"),
|
||||
N_("Configure serial port."), options);
|
||||
|
||||
/* Set default settings. */
|
||||
serial_settings.port = serial_hw_get_port (0);
|
||||
#ifdef GRUB_MACHINE_MIPS_YEELOONG
|
||||
serial_settings.divisor = serial_get_divisor (115200);
|
||||
#else
|
||||
serial_settings.divisor = serial_get_divisor (9600);
|
||||
#endif
|
||||
serial_settings.word_len = UART_8BITS_WORD;
|
||||
serial_settings.parity = UART_NO_PARITY;
|
||||
serial_settings.stop_bits = UART_1_STOP_BIT;
|
||||
|
||||
#ifdef GRUB_MACHINE_MIPS_YEELOONG
|
||||
{
|
||||
grub_err_t hwiniterr;
|
||||
hwiniterr = serial_hw_init ();
|
||||
|
||||
if (hwiniterr == GRUB_ERR_NONE)
|
||||
{
|
||||
grub_term_register_input_active ("serial", &grub_serial_term_input);
|
||||
grub_term_register_output_active ("serial", &grub_serial_term_output);
|
||||
|
||||
registered = 1;
|
||||
}
|
||||
}
|
||||
#ifndef GRUB_MACHINE_EMU
|
||||
grub_ns8250_init ();
|
||||
#endif
|
||||
}
|
||||
|
||||
GRUB_MOD_FINI(serial)
|
||||
{
|
||||
grub_unregister_extcmd (cmd);
|
||||
if (registered == 1) /* Unregister terminal only if registered. */
|
||||
while (grub_serial_ports)
|
||||
grub_serial_unregister (grub_serial_ports);
|
||||
if (registered)
|
||||
{
|
||||
grub_term_unregister_input (&grub_serial_term_input);
|
||||
grub_term_unregister_output (&grub_serial_term_output);
|
||||
grub_terminfo_output_unregister (&grub_serial_term_output);
|
||||
}
|
||||
grub_unregister_extcmd (cmd);
|
||||
}
|
||||
|
|
|
@ -195,7 +195,7 @@ putstr (struct grub_term_output *term, const char *str)
|
|||
struct grub_terminfo_output_state *data
|
||||
= (struct grub_terminfo_output_state *) term->data;
|
||||
while (*str)
|
||||
data->put (*str++);
|
||||
data->put (term, *str++);
|
||||
}
|
||||
|
||||
grub_uint16_t
|
||||
|
@ -225,7 +225,7 @@ grub_terminfo_gotoxy (struct grub_term_output *term,
|
|||
else
|
||||
{
|
||||
if ((y == data->ypos) && (x == data->xpos - 1))
|
||||
data->put ('\b');
|
||||
data->put (term, '\b');
|
||||
}
|
||||
|
||||
data->xpos = x;
|
||||
|
@ -348,20 +348,21 @@ grub_terminfo_putchar (struct grub_term_output *term,
|
|||
data->xpos = 0;
|
||||
if (data->ypos < grub_term_height (term) - 1)
|
||||
data->ypos++;
|
||||
data->put ('\r');
|
||||
data->put ('\n');
|
||||
data->put (term, '\r');
|
||||
data->put (term, '\n');
|
||||
}
|
||||
data->xpos += c->estimated_width;
|
||||
break;
|
||||
}
|
||||
|
||||
data->put (c->base);
|
||||
data->put (term, c->base);
|
||||
}
|
||||
|
||||
#define ANSI_C0 0x9b
|
||||
|
||||
static void
|
||||
grub_terminfo_readkey (int *keys, int *len, int (*readkey) (void))
|
||||
grub_terminfo_readkey (struct grub_term_input *term, int *keys, int *len,
|
||||
int (*readkey) (struct grub_term_input *term))
|
||||
{
|
||||
int c;
|
||||
|
||||
|
@ -371,7 +372,7 @@ grub_terminfo_readkey (int *keys, int *len, int (*readkey) (void))
|
|||
/* On 9600 we have to wait up to 12 milliseconds. */ \
|
||||
start = grub_get_time_ms (); \
|
||||
do \
|
||||
c = readkey (); \
|
||||
c = readkey (term); \
|
||||
while (c == -1 && grub_get_time_ms () - start < 12); \
|
||||
if (c == -1) \
|
||||
return; \
|
||||
|
@ -380,7 +381,7 @@ grub_terminfo_readkey (int *keys, int *len, int (*readkey) (void))
|
|||
(*len)++; \
|
||||
}
|
||||
|
||||
c = readkey ();
|
||||
c = readkey (term);
|
||||
if (c < 0)
|
||||
{
|
||||
*len = 0;
|
||||
|
@ -475,7 +476,8 @@ grub_terminfo_checkkey (struct grub_term_input *termi)
|
|||
if (data->npending)
|
||||
return data->input_buf[0];
|
||||
|
||||
grub_terminfo_readkey (data->input_buf, &data->npending, data->readkey);
|
||||
grub_terminfo_readkey (termi, data->input_buf,
|
||||
&data->npending, data->readkey);
|
||||
|
||||
if (data->npending)
|
||||
return data->input_buf[0];
|
||||
|
@ -491,7 +493,8 @@ grub_terminfo_getkey (struct grub_term_input *termi)
|
|||
= (struct grub_terminfo_input_state *) (termi->data);
|
||||
int ret;
|
||||
while (! data->npending)
|
||||
grub_terminfo_readkey (data->input_buf, &data->npending, data->readkey);
|
||||
grub_terminfo_readkey (termi, data->input_buf, &data->npending,
|
||||
data->readkey);
|
||||
|
||||
ret = data->input_buf[0];
|
||||
data->npending--;
|
||||
|
|
|
@ -54,12 +54,6 @@ static char keyboard_map_shift[128] =
|
|||
'?'
|
||||
};
|
||||
|
||||
static grub_usb_device_t usbdev;
|
||||
|
||||
/* Valid values for bmRequestType. See HID definition version 1.11 section
|
||||
7.2. */
|
||||
#define USB_HID_HOST_TO_DEVICE 0x21
|
||||
#define USB_HID_DEVICE_TO_HOST 0xA1
|
||||
|
||||
/* Valid values for bRequest. See HID definition version 1.11 section 7.2. */
|
||||
#define USB_HID_GET_REPORT 0x01
|
||||
|
@ -69,78 +63,176 @@ static grub_usb_device_t usbdev;
|
|||
#define USB_HID_SET_IDLE 0x0A
|
||||
#define USB_HID_SET_PROTOCOL 0x0B
|
||||
|
||||
static void
|
||||
grub_usb_hid (void)
|
||||
#define USB_HID_BOOT_SUBCLASS 0x01
|
||||
#define USB_HID_KBD_PROTOCOL 0x01
|
||||
|
||||
static int grub_usb_keyboard_checkkey (struct grub_term_input *term);
|
||||
static int grub_usb_keyboard_getkey (struct grub_term_input *term);
|
||||
static int grub_usb_keyboard_getkeystatus (struct grub_term_input *term);
|
||||
|
||||
static struct grub_term_input grub_usb_keyboard_term =
|
||||
{
|
||||
.checkkey = grub_usb_keyboard_checkkey,
|
||||
.getkey = grub_usb_keyboard_getkey,
|
||||
.getkeystatus = grub_usb_keyboard_getkeystatus,
|
||||
.next = 0
|
||||
};
|
||||
|
||||
struct grub_usb_keyboard_data
|
||||
{
|
||||
struct grub_usb_desc_device *descdev;
|
||||
grub_usb_device_t usbdev;
|
||||
grub_uint8_t status;
|
||||
int key;
|
||||
struct grub_usb_desc_endp *endp;
|
||||
};
|
||||
|
||||
auto int usb_iterate (grub_usb_device_t dev);
|
||||
int usb_iterate (grub_usb_device_t dev)
|
||||
static struct grub_term_input grub_usb_keyboards[16];
|
||||
|
||||
static void
|
||||
grub_usb_keyboard_detach (grub_usb_device_t usbdev,
|
||||
int config __attribute__ ((unused)),
|
||||
int interface __attribute__ ((unused)))
|
||||
{
|
||||
unsigned i;
|
||||
for (i = 0; i < ARRAY_SIZE (grub_usb_keyboards); i++)
|
||||
{
|
||||
descdev = &dev->descdev;
|
||||
struct grub_usb_keyboard_data *data = grub_usb_keyboards[i].data;
|
||||
|
||||
grub_dprintf ("usb_keyboard", "%x %x %x\n",
|
||||
descdev->class, descdev->subclass, descdev->protocol);
|
||||
if (!data)
|
||||
continue;
|
||||
|
||||
#if 0
|
||||
if (descdev->class != 0x09
|
||||
|| descdev->subclass == 0x01
|
||||
|| descdev->protocol != 0x02)
|
||||
return 0;
|
||||
#endif
|
||||
if (data->usbdev != usbdev)
|
||||
continue;
|
||||
|
||||
if (descdev->class != 0 || descdev->subclass != 0 || descdev->protocol != 0)
|
||||
return 0;
|
||||
|
||||
grub_printf ("HID found!\n");
|
||||
|
||||
usbdev = dev;
|
||||
|
||||
return 1;
|
||||
grub_term_unregister_input (&grub_usb_keyboards[i]);
|
||||
grub_free ((char *) grub_usb_keyboards[i].name);
|
||||
grub_usb_keyboards[i].name = NULL;
|
||||
grub_free (grub_usb_keyboards[i].data);
|
||||
grub_usb_keyboards[i].data = 0;
|
||||
}
|
||||
grub_usb_iterate (usb_iterate);
|
||||
|
||||
/* Place the device in boot mode. */
|
||||
grub_usb_control_msg (usbdev, USB_HID_HOST_TO_DEVICE, USB_HID_SET_PROTOCOL,
|
||||
0, 0, 0, 0);
|
||||
|
||||
/* Reports every time an event occurs and not more often than that. */
|
||||
grub_usb_control_msg (usbdev, USB_HID_HOST_TO_DEVICE, USB_HID_SET_IDLE,
|
||||
0<<8, 0, 0, 0);
|
||||
}
|
||||
|
||||
static grub_err_t
|
||||
grub_usb_keyboard_getreport (grub_usb_device_t dev, grub_uint8_t *report)
|
||||
static int
|
||||
grub_usb_keyboard_attach (grub_usb_device_t usbdev, int configno, int interfno)
|
||||
{
|
||||
return grub_usb_control_msg (dev, USB_HID_DEVICE_TO_HOST, USB_HID_GET_REPORT,
|
||||
0, 0, 8, (char *) report);
|
||||
unsigned curnum;
|
||||
struct grub_usb_keyboard_data *data;
|
||||
struct grub_usb_desc_endp *endp = NULL;
|
||||
int j;
|
||||
|
||||
grub_dprintf ("usb_keyboard", "%x %x %x %d %d\n",
|
||||
usbdev->descdev.class, usbdev->descdev.subclass,
|
||||
usbdev->descdev.protocol, configno, interfno);
|
||||
|
||||
for (curnum = 0; curnum < ARRAY_SIZE (grub_usb_keyboards); curnum++)
|
||||
if (!grub_usb_keyboards[curnum].data)
|
||||
break;
|
||||
|
||||
if (curnum == ARRAY_SIZE (grub_usb_keyboards))
|
||||
return 0;
|
||||
|
||||
if (usbdev->descdev.class != 0
|
||||
|| usbdev->descdev.subclass != 0 || usbdev->descdev.protocol != 0)
|
||||
return 0;
|
||||
|
||||
if (usbdev->config[configno].interf[interfno].descif->subclass
|
||||
!= USB_HID_BOOT_SUBCLASS
|
||||
|| usbdev->config[configno].interf[interfno].descif->protocol
|
||||
!= USB_HID_KBD_PROTOCOL)
|
||||
return 0;
|
||||
|
||||
for (j = 0; j < usbdev->config[configno].interf[interfno].descif->endpointcnt;
|
||||
j++)
|
||||
{
|
||||
endp = &usbdev->config[configno].interf[interfno].descendp[j];
|
||||
|
||||
if ((endp->endp_addr & 128) && grub_usb_get_ep_type(endp)
|
||||
== GRUB_USB_EP_INTERRUPT)
|
||||
break;
|
||||
}
|
||||
if (j == usbdev->config[configno].interf[interfno].descif->endpointcnt)
|
||||
return 0;
|
||||
|
||||
grub_dprintf ("usb_keyboard", "HID found!\n");
|
||||
|
||||
data = grub_malloc (sizeof (*data));
|
||||
if (!data)
|
||||
{
|
||||
grub_print_error ();
|
||||
return 0;
|
||||
}
|
||||
|
||||
data->usbdev = usbdev;
|
||||
data->endp = endp;
|
||||
|
||||
/* Place the device in boot mode. */
|
||||
grub_usb_control_msg (usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
|
||||
USB_HID_SET_PROTOCOL, 0, 0, 0, 0);
|
||||
|
||||
/* Reports every time an event occurs and not more often than that. */
|
||||
grub_usb_control_msg (usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
|
||||
USB_HID_SET_IDLE, 0<<8, 0, 0, 0);
|
||||
|
||||
grub_memcpy (&grub_usb_keyboards[curnum], &grub_usb_keyboard_term,
|
||||
sizeof (grub_usb_keyboards[curnum]));
|
||||
grub_usb_keyboards[curnum].data = data;
|
||||
usbdev->config[configno].interf[interfno].detach_hook
|
||||
= grub_usb_keyboard_detach;
|
||||
grub_usb_keyboards[curnum].name = grub_xasprintf ("usb_keyboard%d", curnum);
|
||||
if (!grub_usb_keyboards[curnum].name)
|
||||
{
|
||||
grub_print_error ();
|
||||
return 0;
|
||||
}
|
||||
|
||||
{
|
||||
grub_uint8_t report[8];
|
||||
grub_usb_err_t err;
|
||||
grub_memset (report, 0, sizeof (report));
|
||||
err = grub_usb_control_msg (usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_IN,
|
||||
USB_HID_GET_REPORT, 0x0000, interfno,
|
||||
sizeof (report), (char *) report);
|
||||
if (err)
|
||||
{
|
||||
data->status = 0;
|
||||
data->key = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
data->status = report[0];
|
||||
data->key = report[2] ? : -1;
|
||||
}
|
||||
}
|
||||
|
||||
grub_term_register_input_active ("usb_keyboard", &grub_usb_keyboards[curnum]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int
|
||||
grub_usb_keyboard_checkkey (struct grub_term_input *term __attribute__ ((unused)))
|
||||
grub_usb_keyboard_checkkey (struct grub_term_input *term)
|
||||
{
|
||||
grub_uint8_t data[8];
|
||||
int key;
|
||||
grub_err_t err;
|
||||
grub_uint64_t currtime;
|
||||
int timeout = 50;
|
||||
grub_usb_err_t err;
|
||||
struct grub_usb_keyboard_data *termdata = term->data;
|
||||
grub_size_t actual;
|
||||
|
||||
if (termdata->key != -1)
|
||||
return termdata->key;
|
||||
|
||||
data[2] = 0;
|
||||
currtime = grub_get_time_ms ();
|
||||
do
|
||||
{
|
||||
/* Get_Report. */
|
||||
err = grub_usb_keyboard_getreport (usbdev, data);
|
||||
/* Poll interrupt pipe. */
|
||||
err = grub_usb_bulk_read_extended (termdata->usbdev,
|
||||
termdata->endp->endp_addr, sizeof (data),
|
||||
(char *) data, 10, &actual);
|
||||
if (err || actual < 1)
|
||||
return -1;
|
||||
|
||||
/* Implement a timeout. */
|
||||
if (grub_get_time_ms () > currtime + timeout)
|
||||
break;
|
||||
}
|
||||
while (err || !data[2]);
|
||||
termdata->status = data[0];
|
||||
|
||||
if (err || !data[2])
|
||||
if (actual < 3 || !data[2])
|
||||
return -1;
|
||||
|
||||
grub_dprintf ("usb_keyboard",
|
||||
|
@ -151,178 +243,74 @@ grub_usb_keyboard_checkkey (struct grub_term_input *term __attribute__ ((unused)
|
|||
|
||||
/* Check if the Control or Shift key was pressed. */
|
||||
if (data[0] & 0x01 || data[0] & 0x10)
|
||||
key = keyboard_map[data[2]] - 'a' + 1;
|
||||
termdata->key = keyboard_map[data[2]] - 'a' + 1;
|
||||
else if (data[0] & 0x02 || data[0] & 0x20)
|
||||
key = keyboard_map_shift[data[2]];
|
||||
termdata->key = keyboard_map_shift[data[2]];
|
||||
else
|
||||
key = keyboard_map[data[2]];
|
||||
termdata->key = keyboard_map[data[2]];
|
||||
|
||||
if (key == 0)
|
||||
if (termdata->key == 0)
|
||||
grub_printf ("Unknown key 0x%x detected\n", data[2]);
|
||||
|
||||
#if 0
|
||||
/* Wait until the key is released. */
|
||||
while (!err && data[2])
|
||||
{
|
||||
err = grub_usb_control_msg (usbdev, USB_HID_DEVICE_TO_HOST,
|
||||
USB_HID_GET_REPORT, 0, 0,
|
||||
sizeof (data), (char *) data);
|
||||
grub_dprintf ("usb_keyboard",
|
||||
"report2: 0x%02x 0x%02x 0x%02x 0x%02x"
|
||||
" 0x%02x 0x%02x 0x%02x 0x%02x\n",
|
||||
data[0], data[1], data[2], data[3],
|
||||
data[4], data[5], data[6], data[7]);
|
||||
}
|
||||
#endif
|
||||
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
|
||||
return key;
|
||||
return termdata->key;
|
||||
}
|
||||
|
||||
typedef enum
|
||||
{
|
||||
GRUB_HIDBOOT_REPEAT_NONE,
|
||||
GRUB_HIDBOOT_REPEAT_FIRST,
|
||||
GRUB_HIDBOOT_REPEAT
|
||||
} grub_usb_keyboard_repeat_t;
|
||||
|
||||
static int
|
||||
grub_usb_keyboard_getkey (struct grub_term_input *term)
|
||||
{
|
||||
int key;
|
||||
grub_err_t err;
|
||||
grub_uint8_t data[8];
|
||||
grub_uint64_t currtime;
|
||||
int timeout;
|
||||
static grub_usb_keyboard_repeat_t repeat = GRUB_HIDBOOT_REPEAT_NONE;
|
||||
int ret;
|
||||
struct grub_usb_keyboard_data *termdata = term->data;
|
||||
|
||||
again:
|
||||
while (termdata->key == -1)
|
||||
grub_usb_keyboard_checkkey (term);
|
||||
|
||||
do
|
||||
{
|
||||
key = grub_usb_keyboard_checkkey (term);
|
||||
} while (key == -1);
|
||||
ret = termdata->key;
|
||||
|
||||
data[2] = !0; /* Or whatever. */
|
||||
err = 0;
|
||||
termdata->key = -1;
|
||||
|
||||
switch (repeat)
|
||||
{
|
||||
case GRUB_HIDBOOT_REPEAT_FIRST:
|
||||
timeout = 500;
|
||||
break;
|
||||
case GRUB_HIDBOOT_REPEAT:
|
||||
timeout = 50;
|
||||
break;
|
||||
default:
|
||||
timeout = 100;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Wait until the key is released. */
|
||||
currtime = grub_get_time_ms ();
|
||||
while (!err && data[2])
|
||||
{
|
||||
/* Implement a timeout. */
|
||||
if (grub_get_time_ms () > currtime + timeout)
|
||||
{
|
||||
if (repeat == 0)
|
||||
repeat = 1;
|
||||
else
|
||||
repeat = 2;
|
||||
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
return key;
|
||||
}
|
||||
|
||||
err = grub_usb_keyboard_getreport (usbdev, data);
|
||||
}
|
||||
|
||||
if (repeat)
|
||||
{
|
||||
repeat = 0;
|
||||
goto again;
|
||||
}
|
||||
|
||||
repeat = 0;
|
||||
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
|
||||
return key;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int
|
||||
grub_usb_keyboard_getkeystatus (struct grub_term_input *term __attribute__ ((unused)))
|
||||
grub_usb_keyboard_getkeystatus (struct grub_term_input *term)
|
||||
{
|
||||
grub_uint8_t data[8];
|
||||
struct grub_usb_keyboard_data *termdata = term->data;
|
||||
int mods = 0;
|
||||
grub_err_t err;
|
||||
grub_uint64_t currtime;
|
||||
int timeout = 50;
|
||||
|
||||
/* Set idle time to the minimum offered by the spec (4 milliseconds) so
|
||||
that we can find out the current state. */
|
||||
grub_usb_control_msg (usbdev, USB_HID_HOST_TO_DEVICE, USB_HID_SET_IDLE,
|
||||
0<<8, 0, 0, 0);
|
||||
|
||||
currtime = grub_get_time_ms ();
|
||||
do
|
||||
{
|
||||
/* Get_Report. */
|
||||
err = grub_usb_keyboard_getreport (usbdev, data);
|
||||
|
||||
/* Implement a timeout. */
|
||||
if (grub_get_time_ms () > currtime + timeout)
|
||||
break;
|
||||
}
|
||||
while (err || !data[0]);
|
||||
|
||||
/* Go back to reporting every time an event occurs and not more often than
|
||||
that. */
|
||||
grub_usb_control_msg (usbdev, USB_HID_HOST_TO_DEVICE, USB_HID_SET_IDLE,
|
||||
0<<8, 0, 0, 0);
|
||||
|
||||
/* We allowed a while for modifiers to show up in the report, but it is
|
||||
not an error if they never did. */
|
||||
if (err)
|
||||
return -1;
|
||||
|
||||
grub_dprintf ("usb_keyboard",
|
||||
"report: 0x%02x 0x%02x 0x%02x 0x%02x"
|
||||
" 0x%02x 0x%02x 0x%02x 0x%02x\n",
|
||||
data[0], data[1], data[2], data[3],
|
||||
data[4], data[5], data[6], data[7]);
|
||||
|
||||
/* Check Shift, Control, and Alt status. */
|
||||
if (data[0] & 0x02 || data[0] & 0x20)
|
||||
if (termdata->status & 0x02 || termdata->status & 0x20)
|
||||
mods |= GRUB_TERM_STATUS_SHIFT;
|
||||
if (data[0] & 0x01 || data[0] & 0x10)
|
||||
if (termdata->status & 0x01 || termdata->status & 0x10)
|
||||
mods |= GRUB_TERM_STATUS_CTRL;
|
||||
if (data[0] & 0x04 || data[0] & 0x40)
|
||||
if (termdata->status & 0x04 || termdata->status & 0x40)
|
||||
mods |= GRUB_TERM_STATUS_ALT;
|
||||
|
||||
grub_errno = GRUB_ERR_NONE;
|
||||
|
||||
return mods;
|
||||
}
|
||||
|
||||
static struct grub_term_input grub_usb_keyboard_term =
|
||||
{
|
||||
.name = "usb_keyboard",
|
||||
.checkkey = grub_usb_keyboard_checkkey,
|
||||
.getkey = grub_usb_keyboard_getkey,
|
||||
.getkeystatus = grub_usb_keyboard_getkeystatus,
|
||||
.next = 0
|
||||
};
|
||||
struct grub_usb_attach_desc attach_hook =
|
||||
{
|
||||
.class = GRUB_USB_CLASS_HID,
|
||||
.hook = grub_usb_keyboard_attach
|
||||
};
|
||||
|
||||
GRUB_MOD_INIT(usb_keyboard)
|
||||
{
|
||||
grub_usb_hid ();
|
||||
grub_term_register_input ("usb_keyboard", &grub_usb_keyboard_term);
|
||||
grub_usb_register_attach_hook_class (&attach_hook);
|
||||
}
|
||||
|
||||
GRUB_MOD_FINI(usb_keyboard)
|
||||
{
|
||||
grub_term_unregister_input (&grub_usb_keyboard_term);
|
||||
unsigned i;
|
||||
for (i = 0; i < ARRAY_SIZE (grub_usb_keyboards); i++)
|
||||
if (grub_usb_keyboards[i].data)
|
||||
{
|
||||
grub_term_unregister_input (&grub_usb_keyboards[i]);
|
||||
grub_free ((char *) grub_usb_keyboards[i].name);
|
||||
grub_usb_keyboards[i].name = NULL;
|
||||
grub_usb_keyboards[i].data = 0;
|
||||
}
|
||||
grub_usb_unregister_attach_hook_class (&attach_hook);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue