~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

Viewing changes to grub-core/bus/usb/serial/pl2303.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  GRUB  --  GRand Unified Bootloader
 
3
 *  Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010  Free Software Foundation, Inc.
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#include <grub/serial.h>
 
20
#include <grub/types.h>
 
21
#include <grub/dl.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/mm.h>
 
24
#include <grub/usb.h>
 
25
#include <grub/usbserial.h>
 
26
 
 
27
/* Convert speed to divisor.  */
 
28
static grub_uint32_t
 
29
is_speed_supported (unsigned int speed)
 
30
{
 
31
  unsigned int i;
 
32
  unsigned int supported[] = { 2400, 4800, 9600, 19200, 38400, 57600, 115200};
 
33
 
 
34
  for (i = 0; i < ARRAY_SIZE (supported); i++)
 
35
    if (supported[i] == speed)
 
36
      return 1;
 
37
  return 0;
 
38
}
 
39
 
 
40
#define GRUB_PL2303_REQUEST_SET_CONFIG 0x20
 
41
#define GRUB_PL2303_STOP_BITS_1 0x0
 
42
#define GRUB_PL2303_STOP_BITS_2 0x2
 
43
 
 
44
#define GRUB_PL2303_PARITY_NONE 0
 
45
#define GRUB_PL2303_PARITY_ODD  1
 
46
#define GRUB_PL2303_PARITY_EVEN 2
 
47
 
 
48
struct grub_pl2303_config
 
49
{
 
50
  grub_uint32_t speed;
 
51
  grub_uint8_t stop_bits;
 
52
  grub_uint8_t parity;
 
53
  grub_uint8_t word_len;
 
54
} __attribute__ ((packed));
 
55
 
 
56
static void
 
57
real_config (struct grub_serial_port *port)
 
58
{
 
59
  struct grub_pl2303_config config_pl2303;
 
60
  char xx;
 
61
 
 
62
  if (port->configured)
 
63
    return;
 
64
 
 
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,
 
68
                        1, 0x0404, 0, 0, 0);
 
69
 
 
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,
 
77
                        1, 0x0404, 1, 0, 0);
 
78
 
 
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);
 
83
 
 
84
  grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
 
85
                        1, 0, 1, 0, 0);
 
86
  grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
 
87
                        1, 1, 0, 0, 0);
 
88
  grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
 
89
                        1, 2, 0x44, 0, 0);
 
90
  grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
 
91
                        1, 8, 0, 0, 0);
 
92
  grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
 
93
                        1, 9, 0, 0, 0);
 
94
 
 
95
  if (port->config.stop_bits == GRUB_SERIAL_STOP_BITS_2)
 
96
    config_pl2303.stop_bits = GRUB_PL2303_STOP_BITS_2;
 
97
  else
 
98
    config_pl2303.stop_bits = GRUB_PL2303_STOP_BITS_1;
 
99
 
 
100
  switch (port->config.parity)
 
101
    {
 
102
    case GRUB_SERIAL_PARITY_NONE:
 
103
      config_pl2303.parity = GRUB_PL2303_PARITY_NONE;
 
104
      break;
 
105
    case GRUB_SERIAL_PARITY_ODD:
 
106
      config_pl2303.parity = GRUB_PL2303_PARITY_ODD;
 
107
      break;
 
108
    case GRUB_SERIAL_PARITY_EVEN:
 
109
      config_pl2303.parity = GRUB_PL2303_PARITY_EVEN;
 
110
      break;
 
111
    }
 
112
 
 
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,
 
119
                        0x22, 3, 0, 0, 0);
 
120
 
 
121
  grub_usb_control_msg (port->usbdev, GRUB_USB_REQTYPE_VENDOR_OUT,
 
122
                        1, 0, 0x61, 0, 0);
 
123
  port->configured = 1;
 
124
}
 
125
 
 
126
/* Fetch a key.  */
 
127
static int
 
128
pl2303_hw_fetch (struct grub_serial_port *port)
 
129
{
 
130
  real_config (port);
 
131
 
 
132
  return grub_usbserial_fetch (port, 0);
 
133
}
 
134
 
 
135
/* Put a character.  */
 
136
static void
 
137
pl2303_hw_put (struct grub_serial_port *port, const int c)
 
138
{
 
139
  char cc = c;
 
140
 
 
141
  real_config (port);
 
142
 
 
143
  grub_usb_bulk_write (port->usbdev, port->out_endp->endp_addr, 1, &cc);
 
144
}
 
145
 
 
146
static grub_err_t
 
147
pl2303_hw_configure (struct grub_serial_port *port,
 
148
                        struct grub_serial_config *config)
 
149
{
 
150
  if (!is_speed_supported (config->speed))
 
151
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad speed");
 
152
 
 
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");
 
157
 
 
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");
 
161
 
 
162
  if (config->word_len < 5 || config->word_len > 8)
 
163
    return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported word length");
 
164
 
 
165
  port->config = *config;
 
166
  port->configured = 0;
 
167
 
 
168
  return GRUB_ERR_NONE;
 
169
}
 
170
 
 
171
static struct grub_serial_driver grub_pl2303_driver =
 
172
  {
 
173
    .configure = pl2303_hw_configure,
 
174
    .fetch = pl2303_hw_fetch,
 
175
    .put = pl2303_hw_put,
 
176
    .fini = grub_usbserial_fini
 
177
  };
 
178
 
 
179
static const struct 
 
180
{
 
181
  grub_uint16_t vendor, product;
 
182
} products[] =
 
183
  {
 
184
    {0x067b, 0x2303}
 
185
  };
 
186
 
 
187
static int
 
188
grub_pl2303_attach (grub_usb_device_t usbdev, int configno, int interfno)
 
189
{
 
190
  unsigned j;
 
191
 
 
192
  for (j = 0; j < ARRAY_SIZE (products); j++)
 
193
    if (usbdev->descdev.vendorid == products[j].vendor
 
194
        && usbdev->descdev.prodid == products[j].product)
 
195
      break;
 
196
  if (j == ARRAY_SIZE (products))
 
197
    return 0;
 
198
 
 
199
  return grub_usbserial_attach (usbdev, configno, interfno,
 
200
                                &grub_pl2303_driver);
 
201
}
 
202
 
 
203
static struct grub_usb_attach_desc attach_hook =
 
204
{
 
205
  .class = 0xff,
 
206
  .hook = grub_pl2303_attach
 
207
};
 
208
 
 
209
GRUB_MOD_INIT(usbserial_pl2303)
 
210
{
 
211
  grub_usb_register_attach_hook_class (&attach_hook);
 
212
}
 
213
 
 
214
GRUB_MOD_FINI(usbserial_pl2303)
 
215
{
 
216
  grub_serial_unregister_driver (&grub_pl2303_driver);
 
217
  grub_usb_unregister_attach_hook_class (&attach_hook);
 
218
}