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/list.h>
16 #include <linux/string.h>
17 #include <linux/device.h>
18 
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 
22 
35 int
37  const struct usb_descriptor_header **src)
38 {
39  u8 *dest = buf;
40 
41  if (!src)
42  return -EINVAL;
43 
44  /* fill buffer from src[] until null descriptor ptr */
45  for (; NULL != *src; src++) {
46  unsigned len = (*src)->bLength;
47 
48  if (len > buflen)
49  return -EINVAL;
50  memcpy(dest, *src, len);
51  buflen -= len;
52  dest += len;
53  }
54  return dest - (u8 *)buf;
55 }
56 
57 
79  const struct usb_config_descriptor *config,
80  void *buf,
81  unsigned length,
82  const struct usb_descriptor_header **desc
83 )
84 {
85  struct usb_config_descriptor *cp = buf;
86  int len;
87 
88  /* config descriptor first */
89  if (length < USB_DT_CONFIG_SIZE || !desc)
90  return -EINVAL;
91  *cp = *config;
92 
93  /* then interface/endpoint/class/vendor/... */
95  length - USB_DT_CONFIG_SIZE, desc);
96  if (len < 0)
97  return len;
98  len += USB_DT_CONFIG_SIZE;
99  if (len > 0xffff)
100  return -EINVAL;
101 
102  /* patch up the config descriptor */
105  cp->wTotalLength = cpu_to_le16(len);
107  return len;
108 }
109 
122 struct usb_descriptor_header **
124 {
125  struct usb_descriptor_header **tmp;
126  unsigned bytes;
127  unsigned n_desc;
128  void *mem;
129  struct usb_descriptor_header **ret;
130 
131  /* count descriptors and their sizes; then add vector size */
132  for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++)
133  bytes += (*tmp)->bLength;
134  bytes += (n_desc + 1) * sizeof(*tmp);
135 
136  mem = kmalloc(bytes, GFP_KERNEL);
137  if (!mem)
138  return NULL;
139 
140  /* fill in pointers starting at "tmp",
141  * to descriptors copied starting at "mem";
142  * and return "ret"
143  */
144  tmp = mem;
145  ret = mem;
146  mem += (n_desc + 1) * sizeof(*tmp);
147  while (*src) {
148  memcpy(mem, *src, (*src)->bLength);
149  *tmp = mem;
150  tmp++;
151  mem += (*src)->bLength;
152  src++;
153  }
154  *tmp = NULL;
155 
156  return ret;
157 }
158