2009-05-16 Pavel Roskin <proski@gnu.org>
[grub2/bean.git] / util / usb.c
blob744ad86c9485b96d8ed7759c1f3353f23ad5e399
1 /* usb.c -- libusb USB support for GRUB. */
2 /*
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/>.
20 #include <config.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <usb.h>
24 #include <grub/usb.h>
25 #include <grub/dl.h>
28 static struct grub_usb_controller_dev usb_controller =
30 .name = "libusb"
33 static struct grub_usb_device *grub_usb_devs[128];
35 struct usb_bus *busses;
37 static grub_err_t
38 grub_libusb_devices (void)
41 struct usb_bus *bus;
42 int last = 0;
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;
55 if (! desc->bcdUSB)
56 continue;
58 dev = grub_malloc (sizeof (*dev));
59 if (! dev)
60 return grub_errno;
62 dev->data = usbdev;
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;
75 grub_err_t
76 grub_libusb_init (void)
78 usb_init();
79 usb_find_busses();
80 usb_find_devices();
82 if (grub_libusb_devices ())
83 return grub_errno;
85 grub_usb_controller_dev_register (&usb_controller);
87 return 0;
90 grub_err_t
91 grub_libusb_fini (void)
93 return 0;
97 int
98 grub_usb_iterate (int (*hook) (grub_usb_device_t dev))
100 int i;
102 for (i = 0; i < 128; i++)
104 if (grub_usb_devs[i])
106 if (hook (grub_usb_devs[i]))
107 return 1;
111 return 0;
114 grub_usb_err_t
115 grub_usb_root_hub (grub_usb_controller_t controller __attribute__((unused)))
117 return GRUB_USB_ERR_NONE;
120 grub_usb_err_t
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;
128 devh = usb_open (d);
129 if (usb_control_msg (devh, reqtype, request,
130 value, index, data, size, 20) < 0)
132 usb_close (devh);
133 return GRUB_USB_ERR_STALL;
136 usb_close (devh);
138 return GRUB_USB_ERR_NONE;
141 grub_usb_err_t
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;
148 devh = usb_open (d);
149 if (usb_claim_interface (devh, 0) < 1)
151 usb_close (devh);
152 return GRUB_USB_ERR_STALL;
155 if (usb_bulk_read (devh, endpoint, data, size, 20) < 1)
157 usb_close (devh);
158 return GRUB_USB_ERR_STALL;
161 usb_release_interface (devh, 0);
162 usb_close (devh);
164 return GRUB_USB_ERR_NONE;
167 grub_usb_err_t
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;
174 devh = usb_open (d);
175 if (usb_claim_interface (devh, 0) < 0)
176 goto fail;
178 if (usb_bulk_write (devh, endpoint, data, size, 20) < 0)
179 goto fail;
181 if (usb_release_interface (devh, 0) < 0)
182 goto fail;
184 usb_close (devh);
186 return GRUB_USB_ERR_NONE;
188 fail:
189 usb_close (devh);
190 return GRUB_USB_ERR_STALL;