summaryrefslogtreecommitdiffstats
path: root/grub-core/term/arc/serial.c
blob: 87d1ce8218db807045bdd64fb06c5af9d5746416 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2013  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/serial.h>
#include <grub/types.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/time.h>
#include <grub/i18n.h>
#include <grub/arc/arc.h>


static void
do_real_config (struct grub_serial_port *port)
{
  char *name;
  if (port->configured)
    return;

  name = grub_arc_alt_name_to_norm (port->name, "");

  if (GRUB_ARC_FIRMWARE_VECTOR->open (name,GRUB_ARC_FILE_ACCESS_OPEN_RW,
				      &port->handle))
    port->handle_valid = 0;
  else
    port->handle_valid = 1;

  port->configured = 1;
}

/* Fetch a key.  */
static int
serial_hw_fetch (struct grub_serial_port *port)
{
  unsigned long actual;
  char c;

  do_real_config (port);

  if (!port->handle_valid)
    return -1;
  if (GRUB_ARC_FIRMWARE_VECTOR->read (port->handle, &c,
				      1, &actual) || actual <= 0)
    return -1;
  return c;
}

/* Put a character.  */
static void
serial_hw_put (struct grub_serial_port *port, const int c)
{
  unsigned long actual;
  char c0 = c;

  do_real_config (port);

  if (!port->handle_valid)
    return;

  GRUB_ARC_FIRMWARE_VECTOR->write (port->handle, &c0,
				   1, &actual);
}

/* 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 __attribute__ ((unused)),
		     struct grub_serial_config *config __attribute__ ((unused)))
{
  /*  FIXME: no ARC serial config available.  */

  return GRUB_ERR_NONE;
}

struct grub_serial_driver grub_arcserial_driver =
  {
    .configure = serial_hw_configure,
    .fetch = serial_hw_fetch,
    .put = serial_hw_put
  };

const char *
grub_arcserial_add_port (const char *path)
{
  struct grub_serial_port *port;
  grub_err_t err;

  port = grub_zalloc (sizeof (*port));
  if (!port)
    return NULL;
  port->name = grub_strdup (path);
  if (!port->name)
    return NULL;

  port->driver = &grub_arcserial_driver;
  err = grub_serial_config_defaults (port);
  if (err)
    grub_print_error ();

  grub_serial_register (port);

  return port->name;
}

static int
dev_iterate (const char *name,
	     const struct grub_arc_component *comp __attribute__ ((unused)),
	     void *data __attribute__ ((unused)))
{
  /* We should check consolein/consoleout flags as
     well but some implementations are buggy.  */
  if ((comp->flags & (GRUB_ARC_COMPONENT_FLAG_IN | GRUB_ARC_COMPONENT_FLAG_OUT))
      != (GRUB_ARC_COMPONENT_FLAG_IN | GRUB_ARC_COMPONENT_FLAG_OUT))
    return 0;
  if (!grub_arc_is_device_serial (name, 1))
    return 0;
  grub_arcserial_add_port (name);
  return 0;
}

void
grub_arcserial_init (void)
{
  grub_arc_iterate_devs (dev_iterate, 0, 1);
}