Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
config.c
Go to the documentation of this file.
1 /*
2  * usb/gadget/config.c -- simplify building config descriptors
3  *
4  * Copyright (C) 2003 David Brownell
5  *
6  * This program 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 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/list.h>
17 #include <linux/string.h>
18 #include <linux/device.h>
19 
20 #include <linux/usb/ch9.h>
21 #include <linux/usb/gadget.h>
22 
23 
36 int
38  const struct usb_descriptor_header **src)
39 {
40  u8 *dest = buf;
41 
42  if (!src)
43  return -EINVAL;
44 
45  /* fill buffer from src[] until null descriptor ptr */
46  for (; NULL != *src; src++) {
47  unsigned len = (*src)->bLength;
48 
49  if (len > buflen)
50  return -EINVAL;
51  memcpy(dest, *src, len);
52  buflen -= len;
53  dest += len;
54  }
55  return dest - (u8 *)buf;
56 }
58 
80  const struct usb_config_descriptor *config,
81  void *buf,
82  unsigned length,
83  const struct usb_descriptor_header **desc
84 )
85 {
86  struct usb_config_descriptor *cp = buf;
87  int len;
88 
89  /* config descriptor first */
90  if (length < USB_DT_CONFIG_SIZE || !desc)
91  return -EINVAL;
92  *cp = *config;
93 
94  /* then interface/endpoint/class/vendor/... */
96  length - USB_DT_CONFIG_SIZE, desc);
97  if (len < 0)
98  return len;
99  len += USB_DT_CONFIG_SIZE;
100  if (len > 0xffff)
101  return -EINVAL;
102 
103  /* patch up the config descriptor */
106  cp->wTotalLength = cpu_to_le16(len);
108  return len;
109 }
111 
124 struct usb_descriptor_header **
126 {
127  struct usb_descriptor_header **tmp;
128  unsigned bytes;
129  unsigned n_desc;
130  void *mem;
131  struct usb_descriptor_header **ret;
132 
133  /* count descriptors and their sizes; then add vector size */
134  for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
135  bytes += (*tmp)->bLength;
136  bytes += (n_desc + 1) * sizeof(*tmp);
137 
138  mem = kmalloc(bytes, GFP_KERNEL);
139  if (!mem)
140  return NULL;
141 
142  /* fill in pointers starting at "tmp",
143  * to descriptors copied starting at "mem";
144  * and return "ret"
145  */
146  tmp = mem;
147  ret = mem;
148  mem += (n_desc + 1) * sizeof(*tmp);
149  while (*src) {
150  memcpy(mem, *src, (*src)->bLength);
151  *tmp = mem;
152  tmp++;
153  mem += (*src)->bLength;
154  src++;
155  }
156  *tmp = NULL;
157 
158  return ret;
159 }