~ubuntu-branches/ubuntu/trusty/grub2/trusty-updates

« back to all changes in this revision

Viewing changes to util/usb.c

Tags: upstream-1.99~20101122
ImportĀ upstreamĀ versionĀ 1.99~20101122

Show diffs side-by-side

added added

removed removed

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