source: grub-pc/trunk/fuentes/grub-core/bus/usb/emu/usb.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 4.0 KB
Line 
1/*  usb.c -- libusb USB support for GRUB.  */
2/*
3 *  GRUB  --  GRand Unified Bootloader
4 *  Copyright (C) 2008  Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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>
26
27GRUB_MOD_LICENSE ("GPLv3+");
28
29
30static struct grub_usb_controller_dev usb_controller =
31{
32  .name = "libusb"
33};
34
35static struct grub_usb_device *grub_usb_devs[128];
36
37struct usb_bus *busses;
38
39static grub_err_t
40grub_libusb_devices (void)
41
42{
43  struct usb_bus *bus;
44  int last = 0;
45
46  busses = usb_get_busses();
47
48  for (bus = busses; bus; bus = bus->next)
49    {
50      struct usb_device *usbdev;
51      struct grub_usb_device *dev;
52
53      for (usbdev = bus->devices; usbdev; usbdev = usbdev->next)
54        {
55          struct usb_device_descriptor *desc = &usbdev->descriptor;
56          grub_err_t err;
57
58          if (! desc->bcdUSB)
59            continue;
60
61          dev = grub_malloc (sizeof (*dev));
62          if (! dev)
63            return grub_errno;
64
65          dev->data = usbdev;
66
67          /* Fill in all descriptors.  */
68          err = grub_usb_device_initialize (dev);
69          if (err)
70            {
71              grub_errno = GRUB_ERR_NONE;
72              continue;
73            }
74
75          /* Register the device.  */
76          grub_usb_devs[last++] = dev;
77        }
78    }
79
80  return GRUB_USB_ERR_NONE;
81}
82
83void
84grub_usb_poll_devices (void)
85{
86  /* TODO: recheck grub_usb_devs */
87}
88
89
90int
91grub_usb_iterate (grub_usb_iterate_hook_t hook, void *hook_data)
92{
93  int i;
94
95  for (i = 0; i < 128; i++)
96    {
97      if (grub_usb_devs[i])
98        {
99          if (hook (grub_usb_devs[i], hook_data))
100              return 1;
101        }
102    }
103
104  return 0;
105}
106
107grub_usb_err_t
108grub_usb_root_hub (grub_usb_controller_t controller __attribute__((unused)))
109{
110  return GRUB_USB_ERR_NONE;
111}
112
113grub_usb_err_t
114grub_usb_control_msg (grub_usb_device_t dev, grub_uint8_t reqtype,
115                      grub_uint8_t request, grub_uint16_t value,
116                      grub_uint16_t idx, grub_size_t size, char *data)
117{
118  usb_dev_handle *devh;
119  struct usb_device *d = dev->data;
120
121  devh = usb_open (d);
122  if (usb_control_msg (devh, reqtype, request,
123                       value, idx, data, size, 20) < 0)
124    {
125      usb_close (devh);
126      return GRUB_USB_ERR_STALL;
127    }
128
129  usb_close (devh);
130
131  return GRUB_USB_ERR_NONE;
132}
133
134grub_usb_err_t
135grub_usb_bulk_read (grub_usb_device_t dev,
136                    int endpoint, grub_size_t size, char *data)
137{
138  usb_dev_handle *devh;
139  struct usb_device *d = dev->data;
140
141  devh = usb_open (d);
142  if (usb_claim_interface (devh, 0) < 1)
143    {
144      usb_close (devh);
145      return GRUB_USB_ERR_STALL;
146    }
147
148  if (usb_bulk_read (devh, endpoint, data, size, 20) < 1)
149    {
150      usb_close (devh);
151      return GRUB_USB_ERR_STALL;
152    }
153
154  usb_release_interface (devh, 0);
155  usb_close (devh);
156
157  return GRUB_USB_ERR_NONE;
158}
159
160grub_usb_err_t
161grub_usb_bulk_write (grub_usb_device_t dev,
162                     int endpoint, grub_size_t size, char *data)
163{
164  usb_dev_handle *devh;
165  struct usb_device *d = dev->data;
166
167  devh = usb_open (d);
168  if (usb_claim_interface (devh, 0) < 0)
169    goto fail;
170
171  if (usb_bulk_write (devh, endpoint, data, size, 20) < 0)
172    goto fail;
173
174  if (usb_release_interface (devh, 0) < 0)
175    goto fail;
176
177  usb_close (devh);
178
179  return GRUB_USB_ERR_NONE;
180
181 fail:
182  usb_close (devh);
183  return GRUB_USB_ERR_STALL;
184}
185
186GRUB_MOD_INIT (libusb)
187{
188  usb_init();
189  usb_find_busses();
190  usb_find_devices();
191
192  if (grub_libusb_devices ())
193    return;
194
195  grub_usb_controller_dev_register (&usb_controller);
196
197  return;
198}
199
200GRUB_MOD_FINI (libusb)
201{
202  return;
203}
Note: See TracBrowser for help on using the repository browser.