1
/* usb.c -- libusb USB support for GRUB. */
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/>.
21
#include <grub/misc.h>
28
static struct grub_usb_controller_dev usb_controller =
33
static struct grub_usb_device *grub_usb_devs[128];
35
struct usb_bus *busses;
38
grub_libusb_devices (void)
44
busses = usb_get_busses();
46
for (bus = busses; bus; bus = bus->next)
48
struct usb_device *usbdev;
49
struct grub_usb_device *dev;
51
for (usbdev = bus->devices; usbdev; usbdev = usbdev->next)
53
struct usb_device_descriptor *desc = &usbdev->descriptor;
59
dev = grub_malloc (sizeof (*dev));
65
/* Fill in all descriptors. */
66
err = grub_usb_device_initialize (dev);
69
grub_errno = GRUB_ERR_NONE;
73
/* Register the device. */
74
grub_usb_devs[last++] = dev;
78
return GRUB_USB_ERR_NONE;
83
grub_usb_iterate (int (*hook) (grub_usb_device_t dev))
87
for (i = 0; i < 128; i++)
91
if (hook (grub_usb_devs[i]))
100
grub_usb_root_hub (grub_usb_controller_t controller __attribute__((unused)))
102
return GRUB_USB_ERR_NONE;
106
grub_usb_control_msg (grub_usb_device_t dev, grub_uint8_t reqtype,
107
grub_uint8_t request, grub_uint16_t value,
108
grub_uint16_t index, grub_size_t size, char *data)
110
usb_dev_handle *devh;
111
struct usb_device *d = dev->data;
114
if (usb_control_msg (devh, reqtype, request,
115
value, index, data, size, 20) < 0)
118
return GRUB_USB_ERR_STALL;
123
return GRUB_USB_ERR_NONE;
127
grub_usb_bulk_read (grub_usb_device_t dev,
128
int endpoint, grub_size_t size, char *data)
130
usb_dev_handle *devh;
131
struct usb_device *d = dev->data;
134
if (usb_claim_interface (devh, 0) < 1)
137
return GRUB_USB_ERR_STALL;
140
if (usb_bulk_read (devh, endpoint, data, size, 20) < 1)
143
return GRUB_USB_ERR_STALL;
146
usb_release_interface (devh, 0);
149
return GRUB_USB_ERR_NONE;
153
grub_usb_bulk_write (grub_usb_device_t dev,
154
int endpoint, grub_size_t size, char *data)
156
usb_dev_handle *devh;
157
struct usb_device *d = dev->data;
160
if (usb_claim_interface (devh, 0) < 0)
163
if (usb_bulk_write (devh, endpoint, data, size, 20) < 0)
166
if (usb_release_interface (devh, 0) < 0)
171
return GRUB_USB_ERR_NONE;
175
return GRUB_USB_ERR_STALL;
178
GRUB_MOD_INIT (libusb)
184
if (grub_libusb_devices ())
187
grub_usb_controller_dev_register (&usb_controller);
192
GRUB_MOD_FINI (libusb)