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;
82
grub_usb_poll_devices (void)
84
/* TODO: recheck grub_usb_devs */
89
grub_usb_iterate (int (*hook) (grub_usb_device_t dev))
93
for (i = 0; i < 128; i++)
97
if (hook (grub_usb_devs[i]))
106
grub_usb_root_hub (grub_usb_controller_t controller __attribute__((unused)))
108
return GRUB_USB_ERR_NONE;
112
grub_usb_control_msg (grub_usb_device_t dev, grub_uint8_t reqtype,
113
grub_uint8_t request, grub_uint16_t value,
114
grub_uint16_t idx, grub_size_t size, char *data)
116
usb_dev_handle *devh;
117
struct usb_device *d = dev->data;
120
if (usb_control_msg (devh, reqtype, request,
121
value, idx, data, size, 20) < 0)
124
return GRUB_USB_ERR_STALL;
129
return GRUB_USB_ERR_NONE;
133
grub_usb_bulk_read (grub_usb_device_t dev,
134
int endpoint, grub_size_t size, char *data)
136
usb_dev_handle *devh;
137
struct usb_device *d = dev->data;
140
if (usb_claim_interface (devh, 0) < 1)
143
return GRUB_USB_ERR_STALL;
146
if (usb_bulk_read (devh, endpoint, data, size, 20) < 1)
149
return GRUB_USB_ERR_STALL;
152
usb_release_interface (devh, 0);
155
return GRUB_USB_ERR_NONE;
159
grub_usb_bulk_write (grub_usb_device_t dev,
160
int endpoint, grub_size_t size, char *data)
162
usb_dev_handle *devh;
163
struct usb_device *d = dev->data;
166
if (usb_claim_interface (devh, 0) < 0)
169
if (usb_bulk_write (devh, endpoint, data, size, 20) < 0)
172
if (usb_release_interface (devh, 0) < 0)
177
return GRUB_USB_ERR_NONE;
181
return GRUB_USB_ERR_STALL;
184
GRUB_MOD_INIT (libusb)
190
if (grub_libusb_devices ())
193
grub_usb_controller_dev_register (&usb_controller);
198
GRUB_MOD_FINI (libusb)