2009-07-16 Vladimir Serbinenko <phcoder@gmail.com>
[grub2/phcoder.git] / util / usb.c
bloba687eea9b0581e7cc7d88e2347315052d2153a7c
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;
54 grub_err_t err;
56 if (! desc->bcdUSB)
57 continue;
59 dev = grub_malloc (sizeof (*dev));
60 if (! dev)
61 return grub_errno;
63 dev->data = usbdev;
65 /* Fill in all descriptors. */
66 err = grub_usb_device_initialize (dev);
67 if (err)
69 grub_errno = GRUB_ERR_NONE;
70 continue;
73 /* Register the device. */
74 grub_usb_devs[last++] = dev;
78 return GRUB_USB_ERR_NONE;
82 int
83 grub_usb_iterate (int (*hook) (grub_usb_device_t dev))
85 int i;
87 for (i = 0; i < 128; i++)
89 if (grub_usb_devs[i])
91 if (hook (grub_usb_devs[i]))
92 return 1;
96 return 0;
99 grub_usb_err_t
100 grub_usb_root_hub (grub_usb_controller_t controller __attribute__((unused)))
102 return GRUB_USB_ERR_NONE;
105 grub_usb_err_t
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;
113 devh = usb_open (d);
114 if (usb_control_msg (devh, reqtype, request,
115 value, index, data, size, 20) < 0)
117 usb_close (devh);
118 return GRUB_USB_ERR_STALL;
121 usb_close (devh);
123 return GRUB_USB_ERR_NONE;
126 grub_usb_err_t
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;
133 devh = usb_open (d);
134 if (usb_claim_interface (devh, 0) < 1)
136 usb_close (devh);
137 return GRUB_USB_ERR_STALL;
140 if (usb_bulk_read (devh, endpoint, data, size, 20) < 1)
142 usb_close (devh);
143 return GRUB_USB_ERR_STALL;
146 usb_release_interface (devh, 0);
147 usb_close (devh);
149 return GRUB_USB_ERR_NONE;
152 grub_usb_err_t
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;
159 devh = usb_open (d);
160 if (usb_claim_interface (devh, 0) < 0)
161 goto fail;
163 if (usb_bulk_write (devh, endpoint, data, size, 20) < 0)
164 goto fail;
166 if (usb_release_interface (devh, 0) < 0)
167 goto fail;
169 usb_close (devh);
171 return GRUB_USB_ERR_NONE;
173 fail:
174 usb_close (devh);
175 return GRUB_USB_ERR_STALL;
178 GRUB_MOD_INIT (libusb)
180 usb_init();
181 usb_find_busses();
182 usb_find_devices();
184 if (grub_libusb_devices ())
185 return;
187 grub_usb_controller_dev_register (&usb_controller);
189 return;
192 GRUB_MOD_FINI (libusb)
194 return;