summaryrefslogtreecommitdiffstats
path: root/grub-core/bus/usb/usb.c
blob: 7cb3cc230b2364108dd10fea31ceef3f5cab7fb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* usb.c - Generic USB interfaces.  */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2008  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <grub/dl.h>
#include <grub/mm.h>
#include <grub/usb.h>
#include <grub/misc.h>
#include <grub/list.h>
#include <grub/term.h>

GRUB_MOD_LICENSE ("GPLv3+");

static struct grub_usb_attach_desc *attach_hooks;

#if 0
/* Context for grub_usb_controller_iterate.  */
struct grub_usb_controller_iterate_ctx
{
  grub_usb_controller_iterate_hook_t hook;
  void *hook_data;
  grub_usb_controller_dev_t p;
};

/* Helper for grub_usb_controller_iterate.  */
static int
grub_usb_controller_iterate_iter (grub_usb_controller_t dev, void *data)
{
  struct grub_usb_controller_iterate_ctx *ctx = data;

  dev->dev = ctx->p;
  if (ctx->hook (dev, ctx->hook_data))
    return 1;
  return 0;
}

int
grub_usb_controller_iterate (grub_usb_controller_iterate_hook_t hook,
			     void *hook_data)
{
  struct grub_usb_controller_iterate_ctx ctx = {
    .hook = hook,
    .hook_data = hook_data
  };

  /* Iterate over all controller drivers.  */
  for (ctx.p = grub_usb_list; ctx.p; ctx.p = ctx.p->next)
    {
      /* Iterate over the busses of the controllers.  XXX: Actually, a
	 hub driver should do this.  */
      if (ctx.p->iterate (grub_usb_controller_iterate_iter, &ctx))
	return 1;
    }

  return 0;
}
#endif


grub_usb_err_t
grub_usb_clear_halt (grub_usb_device_t dev, int endpoint)
{
  if (endpoint >= GRUB_USB_MAX_TOGGLE)
    return GRUB_USB_ERR_BADDEVICE;

  dev->toggle[endpoint] = 0;
  return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
				     | GRUB_USB_REQTYPE_STANDARD
				     | GRUB_USB_REQTYPE_TARGET_ENDP),
			       GRUB_USB_REQ_CLEAR_FEATURE,
			       GRUB_USB_FEATURE_ENDP_HALT,
			       endpoint, 0, 0);
}

grub_usb_err_t
grub_usb_set_configuration (grub_usb_device_t dev, int configuration)
{
  grub_memset (dev->toggle, 0, sizeof (dev->toggle));

  return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_OUT
				     | GRUB_USB_REQTYPE_STANDARD
				     | GRUB_USB_REQTYPE_TARGET_DEV),
			       GRUB_USB_REQ_SET_CONFIGURATION, configuration,
			       0, 0, NULL);
}

grub_usb_err_t
grub_usb_get_descriptor (grub_usb_device_t dev,
			 grub_uint8_t type, grub_uint8_t index,
			 grub_size_t size, char *data)
{
  return grub_usb_control_msg (dev, (GRUB_USB_REQTYPE_IN
				     | GRUB_USB_REQTYPE_STANDARD
				     | GRUB_USB_REQTYPE_TARGET_DEV),
			       GRUB_USB_REQ_GET_DESCRIPTOR,
			       (type << 8) | index,
			       0, size, data);
}

grub_usb_err_t
grub_usb_device_initialize (grub_usb_device_t dev)
{
  struct grub_usb_desc_device *descdev;
  struct grub_usb_desc_config config;
  grub_usb_err_t err;
  int i;

  /* First we have to read first 8 bytes only and determine
   * max. size of packet */
  dev->descdev.maxsize0 = 0; /* invalidating, for safety only, can be removed if it is sure it is zero here */
  err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_DEVICE,
                                 0, 8, (char *) &dev->descdev);
  if (err)
    return err;

  /* Now we have valid value in dev->descdev.maxsize0,
   * so we can read whole device descriptor */
  err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_DEVICE,
				 0, sizeof (struct grub_usb_desc_device),
				 (char *) &dev->descdev);
  if (err)
    return err;
  descdev = &dev->descdev;

  for (i = 0; i < GRUB_USB_MAX_CONF; i++)
    dev->config[i].descconf = NULL;

  if (descdev->configcnt == 0 || descdev->configcnt > GRUB_USB_MAX_CONF)
    {
      err = GRUB_USB_ERR_BADDEVICE;
      goto fail;
    }    

  for (i = 0; i < descdev->configcnt; i++)
    {
      int pos;
      int currif;
      char *data;
      struct grub_usb_desc *desc;

      /* First just read the first 4 bytes of the configuration
	 descriptor, after that it is known how many bytes really have
	 to be read.  */
      err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_CONFIG, i, 4,
				     (char *) &config);

      data = grub_malloc (config.totallen);
      if (! data)
	{
	  err = GRUB_USB_ERR_INTERNAL;
	  goto fail;
	}

      dev->config[i].descconf = (struct grub_usb_desc_config *) data;
      err = grub_usb_get_descriptor (dev, GRUB_USB_DESCRIPTOR_CONFIG, i,
				     config.totallen, data);
      if (err)
	goto fail;

      /* Skip the configuration descriptor.  */
      pos = dev->config[i].descconf->length;

      if (dev->config[i].descconf->numif > GRUB_USB_MAX_IF)
        {
          err = GRUB_USB_ERR_BADDEVICE;
          goto fail;
        }

      /* Read all interfaces.  */
      for (currif = 0; currif < dev->config[i].descconf->numif; currif++)
	{
	  while (pos < config.totallen)
            {
              desc = (struct grub_usb_desc *)&data[pos];
              if (desc->type == GRUB_USB_DESCRIPTOR_INTERFACE)
                break;
              if (!desc->length)
                {
                  err = GRUB_USB_ERR_BADDEVICE;
                  goto fail;
                }
              pos += desc->length;
            }

	  dev->config[i].interf[currif].descif
	    = (struct grub_usb_desc_if *) &data[pos];
	  pos += dev->config[i].interf[currif].descif->length;

	  while (pos < config.totallen)
            {
              desc = (struct grub_usb_desc *)&data[pos];
              if (desc->type == GRUB_USB_DESCRIPTOR_ENDPOINT)
                break;
              if (!desc->length)
                {
                  err = GRUB_USB_ERR_BADDEVICE;
                  goto fail;
                }
              pos += desc->length;
            }

	  /* Point to the first endpoint.  */
	  dev->config[i].interf[currif].descendp
	    = (struct grub_usb_desc_endp *) &data[pos];
	  pos += (sizeof (struct grub_usb_desc_endp)
		  * dev->config[i].interf[currif].descif->endpointcnt);
	}
    }

  return GRUB_USB_ERR_NONE;

 fail:

  for (i = 0; i < GRUB_USB_MAX_CONF; i++)
    grub_free (dev->config[i].descconf);

  return err;
}

void grub_usb_device_attach (grub_usb_device_t dev)
{
  int i;
  
  /* XXX: Just check configuration 0 for now.  */
  for (i = 0; i < dev->config[0].descconf->numif; i++)
    {
      struct grub_usb_desc_if *interf;
      struct grub_usb_attach_desc *desc;

      interf = dev->config[0].interf[i].descif;

      grub_dprintf ("usb", "iterate: interf=%d, class=%d, subclass=%d, protocol=%d\n",
		    i, interf->class, interf->subclass, interf->protocol);

      if (dev->config[0].interf[i].attached)
	continue;

      for (desc = attach_hooks; desc; desc = desc->next)
	if (interf->class == desc->class)
	  {
	    grub_boot_time ("Probing USB device driver class %x", desc->class);
	    if (desc->hook (dev, 0, i))
	      dev->config[0].interf[i].attached = 1;
	    grub_boot_time ("Probed USB device driver class %x", desc->class);
	  }

      if (dev->config[0].interf[i].attached)
	continue;

      switch (interf->class)
	{
	case GRUB_USB_CLASS_MASS_STORAGE:
	  grub_dl_load ("usbms");
	  grub_print_error ();
	  break;
	case GRUB_USB_CLASS_HID:
	  grub_dl_load ("usb_keyboard");
	  grub_print_error ();
	  break;
	case 0xff:
	  /* FIXME: don't load useless modules.  */
	  grub_dl_load ("usbserial_ftdi");
	  grub_print_error ();
	  grub_dl_load ("usbserial_pl2303");
	  grub_print_error ();
	  grub_dl_load ("usbserial_usbdebug");
	  grub_print_error ();
	  break;
	}
    }
}

/* Helper for grub_usb_register_attach_hook_class.  */
static int
grub_usb_register_attach_hook_class_iter (grub_usb_device_t usbdev, void *data)
{
  struct grub_usb_attach_desc *desc = data;
  struct grub_usb_desc_device *descdev = &usbdev->descdev;
  int i;

  if (descdev->class != 0 || descdev->subclass || descdev->protocol != 0
      || descdev->configcnt == 0)
    return 0;

  /* XXX: Just check configuration 0 for now.  */
  for (i = 0; i < usbdev->config[0].descconf->numif; i++)
    {
      struct grub_usb_desc_if *interf;

      interf = usbdev->config[0].interf[i].descif;

      grub_dprintf ("usb", "iterate: interf=%d, class=%d, subclass=%d, protocol=%d\n",
		    i, interf->class, interf->subclass, interf->protocol);

      if (usbdev->config[0].interf[i].attached)
	continue;

      if (interf->class != desc->class)
	continue;
      if (desc->hook (usbdev, 0, i))
	usbdev->config[0].interf[i].attached = 1;
    }

  return 0;
}

void
grub_usb_register_attach_hook_class (struct grub_usb_attach_desc *desc)
{
  desc->next = attach_hooks;
  attach_hooks = desc;

  grub_usb_iterate (grub_usb_register_attach_hook_class_iter, desc);
}

void
grub_usb_unregister_attach_hook_class (struct grub_usb_attach_desc *desc)
{
  grub_list_remove (GRUB_AS_LIST (desc));  
}


GRUB_MOD_INIT(usb)
{
  grub_term_poll_usb = grub_usb_poll_devices;
}

GRUB_MOD_FINI(usb)
{
  grub_term_poll_usb = NULL;
}