1
/* usb.c - Generic USB interfaces. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2008 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
23
#include <grub/misc.h>
24
#include <grub/list.h>
25
#include <grub/term.h>
27
static grub_usb_controller_dev_t grub_usb_list;
28
struct grub_usb_attach_desc *attach_hooks;
31
grub_usb_controller_dev_register (grub_usb_controller_dev_t usb)
33
auto int iterate_hook (grub_usb_controller_t dev);
35
/* Iterate over all controllers found by the driver. */
36
int iterate_hook (grub_usb_controller_t dev)
40
/* Enable the ports of the USB Root Hub. */
41
grub_usb_root_hub (dev);
46
usb->next = grub_usb_list;
50
usb->iterate (iterate_hook);
54
grub_usb_controller_dev_unregister (grub_usb_controller_dev_t usb)
56
grub_usb_controller_dev_t *p, q;
58
for (p = &grub_usb_list, q = *p; q; p = &(q->next), q = q->next)
68
grub_usb_controller_iterate (int (*hook) (grub_usb_controller_t dev))
70
grub_usb_controller_dev_t p;
72
auto int iterate_hook (grub_usb_controller_t dev);
74
int iterate_hook (grub_usb_controller_t dev)
82
/* Iterate over all controller drivers. */
83
for (p = grub_usb_list; p; p = p->next)
85
/* Iterate over the busses of the controllers. XXX: Actually, a
86
hub driver should do this. */
87
if (p->iterate (iterate_hook))
97
grub_usb_clear_halt (grub_usb_device_t dev, int endpoint)
99
dev->toggle[endpoint] = 0;
100
return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
101
| GRUB_USB_REQTYPE_STANDARD
102
| GRUB_USB_REQTYPE_TARGET_ENDP),
103
GRUB_USB_REQ_CLEAR_FEATURE,
104
GRUB_USB_FEATURE_ENDP_HALT,
109
grub_usb_set_configuration (grub_usb_device_t dev, int configuration)
111
grub_memset (dev->toggle, 0, sizeof (dev->toggle));
113
return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
114
| GRUB_USB_REQTYPE_STANDARD
115
| GRUB_USB_REQTYPE_TARGET_DEV),
116
GRUB_USB_REQ_SET_CONFIGURATION, configuration,
121
grub_usb_get_descriptor (grub_usb_device_t dev,
122
grub_uint8_t type, grub_uint8_t index,
123
grub_size_t size, char *data)
125
return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_IN
126
| GRUB_USB_REQTYPE_STANDARD
127
| GRUB_USB_REQTYPE_TARGET_DEV),
128
GRUB_USB_REQ_GET_DESCRIPTOR,
133
struct grub_usb_desc_endp *
134
grub_usb_get_endpdescriptor (grub_usb_device_t usbdev, int addr)
138
for (i = 0; i < usbdev->config[0].descconf->numif; i++)
140
struct grub_usb_desc_if *interf;
143
interf = usbdev->config[0].interf[i].descif;
145
for (j = 0; j < interf->endpointcnt; j++)
147
struct grub_usb_desc_endp *endp;
148
endp = &usbdev->config[0].interf[i].descendp[j];
150
if (endp->endp_addr == addr)
159
grub_usb_device_initialize (grub_usb_device_t dev)
161
struct grub_usb_desc_device *descdev;
162
struct grub_usb_desc_config config;
166
/* First we have to read first 8 bytes only and determine
167
* max. size of packet */
168
dev->descdev.maxsize0 = 0; /* invalidating, for safety only, can be removed if it is sure it is zero here */
169
err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_DEVICE,
170
0, 8, (char *) &dev->descdev);
174
/* Now we have valid value in dev->descdev.maxsize0,
175
* so we can read whole device descriptor */
176
err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_DEVICE,
177
0, sizeof (struct grub_usb_desc_device),
178
(char *) &dev->descdev);
181
descdev = &dev->descdev;
183
for (i = 0; i < 8; i++)
184
dev->config[i].descconf = NULL;
186
for (i = 0; i < descdev->configcnt; i++)
192
/* First just read the first 4 bytes of the configuration
193
descriptor, after that it is known how many bytes really have
195
err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_CONFIG, i, 4,
198
data = grub_malloc (config.totallen);
201
err = GRUB_USB_ERR_INTERNAL;
205
dev->config[i].descconf = (struct grub_usb_desc_config *) data;
206
err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_CONFIG, i,
207
config.totallen, data);
211
/* Skip the configuration descriptor. */
212
pos = dev->config[i].descconf->length;
214
/* Read all interfaces. */
215
for (currif = 0; currif < dev->config[i].descconf->numif; currif++)
217
while (pos < config.totallen
218
&& ((struct grub_usb_desc *)&data[pos])->type
219
!= GRUB_USB_DESCRIPTOR_INTERFACE)
220
pos += ((struct grub_usb_desc *)&data[pos])->length;
221
dev->config[i].interf[currif].descif
222
= (struct grub_usb_desc_if *) &data[pos];
223
pos += dev->config[i].interf[currif].descif->length;
225
while (pos < config.totallen
226
&& ((struct grub_usb_desc *)&data[pos])->type
227
!= GRUB_USB_DESCRIPTOR_ENDPOINT)
228
pos += ((struct grub_usb_desc *)&data[pos])->length;
230
/* Point to the first endpoint. */
231
dev->config[i].interf[currif].descendp
232
= (struct grub_usb_desc_endp *) &data[pos];
233
pos += (sizeof (struct grub_usb_desc_endp)
234
* dev->config[i].interf[currif].descif->endpointcnt);
238
return GRUB_USB_ERR_NONE;
242
for (i = 0; i < 8; i++)
243
grub_free (dev->config[i].descconf);
248
void grub_usb_device_attach (grub_usb_device_t dev)
252
/* XXX: Just check configuration 0 for now. */
253
for (i = 0; i < dev->config[0].descconf->numif; i++)
255
struct grub_usb_desc_if *interf;
256
struct grub_usb_attach_desc *desc;
258
interf = dev->config[0].interf[i].descif;
260
grub_dprintf ("usb", "iterate: interf=%d, class=%d, subclass=%d, protocol=%d\n",
261
i, interf->class, interf->subclass, interf->protocol);
263
if (dev->config[0].interf[i].attached)
266
for (desc = attach_hooks; desc; desc = desc->next)
267
if (interf->class == desc->class && desc->hook (dev, 0, i))
268
dev->config[0].interf[i].attached = 1;
270
if (dev->config[0].interf[i].attached)
273
switch (interf->class)
275
case GRUB_USB_CLASS_MASS_STORAGE:
276
grub_dl_load ("usbms");
278
case GRUB_USB_CLASS_HID:
279
grub_dl_load ("usb_keyboard");
282
/* FIXME: don't load useless modules. */
283
grub_dl_load ("usbserial_ftdi");
284
grub_dl_load ("usbserial_pl2303");
291
grub_usb_register_attach_hook_class (struct grub_usb_attach_desc *desc)
293
auto int usb_iterate (grub_usb_device_t dev);
295
int usb_iterate (grub_usb_device_t usbdev)
297
struct grub_usb_desc_device *descdev = &usbdev->descdev;
300
if (descdev->class != 0 || descdev->subclass || descdev->protocol != 0
301
|| descdev->configcnt == 0)
304
/* XXX: Just check configuration 0 for now. */
305
for (i = 0; i < usbdev->config[0].descconf->numif; i++)
307
struct grub_usb_desc_if *interf;
309
interf = usbdev->config[0].interf[i].descif;
311
grub_dprintf ("usb", "iterate: interf=%d, class=%d, subclass=%d, protocol=%d\n",
312
i, interf->class, interf->subclass, interf->protocol);
314
if (usbdev->config[0].interf[i].attached)
317
if (interf->class != desc->class)
319
if (desc->hook (usbdev, 0, i))
320
usbdev->config[0].interf[i].attached = 1;
326
desc->next = attach_hooks;
329
grub_usb_iterate (usb_iterate);
333
grub_usb_unregister_attach_hook_class (struct grub_usb_attach_desc *desc)
335
grub_list_remove (GRUB_AS_LIST_P (&attach_hooks), GRUB_AS_LIST (desc));
341
grub_term_poll_usb = grub_usb_poll_devices;
346
grub_term_poll_usb = NULL;