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
;
58 dev
= grub_malloc (sizeof (*dev
));
64 /* Fill in all descriptors. */
65 grub_usb_device_initialize (dev
);
67 /* Register the device. */
68 grub_usb_devs
[last
++] = dev
;
72 return GRUB_USB_ERR_NONE
;
76 grub_libusb_init (void)
82 if (grub_libusb_devices ())
85 grub_usb_controller_dev_register (&usb_controller
);
91 grub_libusb_fini (void)
98 grub_usb_iterate (int (*hook
) (grub_usb_device_t dev
))
102 for (i
= 0; i
< 128; i
++)
104 if (grub_usb_devs
[i
])
106 if (hook (grub_usb_devs
[i
]))
115 grub_usb_root_hub (grub_usb_controller_t controller
__attribute__((unused
)))
117 return GRUB_USB_ERR_NONE
;
121 grub_usb_control_msg (grub_usb_device_t dev
, grub_uint8_t reqtype
,
122 grub_uint8_t request
, grub_uint16_t value
,
123 grub_uint16_t index
, grub_size_t size
, char *data
)
125 usb_dev_handle
*devh
;
126 struct usb_device
*d
= dev
->data
;
129 if (usb_control_msg (devh
, reqtype
, request
,
130 value
, index
, data
, size
, 20) < 0)
133 return GRUB_USB_ERR_STALL
;
138 return GRUB_USB_ERR_NONE
;
142 grub_usb_bulk_read (grub_usb_device_t dev
,
143 int endpoint
, grub_size_t size
, char *data
)
145 usb_dev_handle
*devh
;
146 struct usb_device
*d
= dev
->data
;
149 if (usb_claim_interface (devh
, 0) < 1)
152 return GRUB_USB_ERR_STALL
;
155 if (usb_bulk_read (devh
, endpoint
, data
, size
, 20) < 1)
158 return GRUB_USB_ERR_STALL
;
161 usb_release_interface (devh
, 0);
164 return GRUB_USB_ERR_NONE
;
168 grub_usb_bulk_write (grub_usb_device_t dev
,
169 int endpoint
, grub_size_t size
, char *data
)
171 usb_dev_handle
*devh
;
172 struct usb_device
*d
= dev
->data
;
175 if (usb_claim_interface (devh
, 0) < 0)
178 if (usb_bulk_write (devh
, endpoint
, data
, size
, 20) < 0)
181 if (usb_release_interface (devh
, 0) < 0)
186 return GRUB_USB_ERR_NONE
;
190 return GRUB_USB_ERR_STALL
;