Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/errno.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/device.h> |
| 18 | |
David Brownell | 5f84813 | 2006-12-16 15:34:53 -0800 | [diff] [blame] | 19 | #include <linux/usb/ch9.h> |
David Brownell | 9454a57 | 2007-10-04 18:05:17 -0700 | [diff] [blame] | 20 | #include <linux/usb/gadget.h> |
Arve Hjønnevåg | 6ef3aa4 | 2013-04-18 18:58:11 -0700 | [diff] [blame] | 21 | #include <linux/usb/composite.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Vijayavardhan Vennapusa | 95b650a | 2012-01-18 12:54:01 +0530 | [diff] [blame] | 23 | /** |
| 24 | * usb_find_descriptor_fillbuf - fill buffer with the requested descriptor |
| 25 | * @buf: Buffer to be filled |
| 26 | * @buflen: Size of buf |
| 27 | * @src: Array of descriptor pointers, terminated by null pointer. |
| 28 | * @desc_type: bDescriptorType field of the requested descriptor. |
| 29 | * |
| 30 | * Copies the requested descriptor into the buffer, returning the length |
| 31 | * or a negative error code if it is not found or can't be copied. Useful |
| 32 | * when DT_OTG descriptor is requested. |
| 33 | */ |
| 34 | int |
| 35 | usb_find_descriptor_fillbuf(void *buf, unsigned buflen, |
| 36 | const struct usb_descriptor_header **src, u8 desc_type) |
| 37 | { |
| 38 | if (!src) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | for (; NULL != *src; src++) { |
| 42 | unsigned len; |
| 43 | |
| 44 | if ((*src)->bDescriptorType != desc_type) |
| 45 | continue; |
| 46 | |
| 47 | len = (*src)->bLength; |
| 48 | if (len > buflen) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | memcpy(buf, *src, len); |
| 52 | return len; |
| 53 | } |
| 54 | |
| 55 | return -ENOENT; |
| 56 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * usb_descriptor_fillbuf - fill buffer with descriptors |
| 60 | * @buf: Buffer to be filled |
| 61 | * @buflen: Size of buf |
| 62 | * @src: Array of descriptor pointers, terminated by null pointer. |
| 63 | * |
| 64 | * Copies descriptors into the buffer, returning the length or a |
| 65 | * negative error code if they can't all be copied. Useful when |
| 66 | * assembling descriptors for an associated set of interfaces used |
| 67 | * as part of configuring a composite device; or in other cases where |
| 68 | * sets of descriptors need to be marshaled. |
| 69 | */ |
| 70 | int |
| 71 | usb_descriptor_fillbuf(void *buf, unsigned buflen, |
| 72 | const struct usb_descriptor_header **src) |
| 73 | { |
| 74 | u8 *dest = buf; |
| 75 | |
| 76 | if (!src) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | /* fill buffer from src[] until null descriptor ptr */ |
David Brownell | a947522 | 2007-07-30 12:31:07 -0700 | [diff] [blame] | 80 | for (; NULL != *src; src++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | unsigned len = (*src)->bLength; |
| 82 | |
| 83 | if (len > buflen) |
| 84 | return -EINVAL; |
| 85 | memcpy(dest, *src, len); |
| 86 | buflen -= len; |
| 87 | dest += len; |
| 88 | } |
| 89 | return dest - (u8 *)buf; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * usb_gadget_config_buf - builts a complete configuration descriptor |
| 95 | * @config: Header for the descriptor, including characteristics such |
| 96 | * as power requirements and number of interfaces. |
| 97 | * @desc: Null-terminated vector of pointers to the descriptors (interface, |
| 98 | * endpoint, etc) defining all functions in this device configuration. |
| 99 | * @buf: Buffer for the resulting configuration descriptor. |
| 100 | * @length: Length of buffer. If this is not big enough to hold the |
| 101 | * entire configuration descriptor, an error code will be returned. |
| 102 | * |
| 103 | * This copies descriptors into the response buffer, building a descriptor |
| 104 | * for that configuration. It returns the buffer length or a negative |
| 105 | * status code. The config.wTotalLength field is set to match the length |
| 106 | * of the result, but other descriptor fields (including power usage and |
| 107 | * interface count) must be set by the caller. |
| 108 | * |
| 109 | * Gadget drivers could use this when constructing a config descriptor |
| 110 | * in response to USB_REQ_GET_DESCRIPTOR. They will need to patch the |
| 111 | * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed. |
| 112 | */ |
| 113 | int usb_gadget_config_buf( |
| 114 | const struct usb_config_descriptor *config, |
| 115 | void *buf, |
| 116 | unsigned length, |
| 117 | const struct usb_descriptor_header **desc |
| 118 | ) |
| 119 | { |
| 120 | struct usb_config_descriptor *cp = buf; |
| 121 | int len; |
| 122 | |
| 123 | /* config descriptor first */ |
| 124 | if (length < USB_DT_CONFIG_SIZE || !desc) |
| 125 | return -EINVAL; |
David Brownell | a4c39c4 | 2008-06-19 17:52:25 -0700 | [diff] [blame] | 126 | *cp = *config; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
| 128 | /* then interface/endpoint/class/vendor/... */ |
| 129 | len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf, |
| 130 | length - USB_DT_CONFIG_SIZE, desc); |
| 131 | if (len < 0) |
| 132 | return len; |
| 133 | len += USB_DT_CONFIG_SIZE; |
| 134 | if (len > 0xffff) |
| 135 | return -EINVAL; |
| 136 | |
| 137 | /* patch up the config descriptor */ |
| 138 | cp->bLength = USB_DT_CONFIG_SIZE; |
| 139 | cp->bDescriptorType = USB_DT_CONFIG; |
| 140 | cp->wTotalLength = cpu_to_le16(len); |
| 141 | cp->bmAttributes |= USB_CONFIG_ATT_ONE; |
| 142 | return len; |
| 143 | } |
| 144 | |
David Brownell | a4c39c4 | 2008-06-19 17:52:25 -0700 | [diff] [blame] | 145 | /** |
| 146 | * usb_copy_descriptors - copy a vector of USB descriptors |
| 147 | * @src: null-terminated vector to copy |
| 148 | * Context: initialization code, which may sleep |
| 149 | * |
| 150 | * This makes a copy of a vector of USB descriptors. Its primary use |
| 151 | * is to support usb_function objects which can have multiple copies, |
| 152 | * each needing different descriptors. Functions may have static |
| 153 | * tables of descriptors, which are used as templates and customized |
| 154 | * with identifiers (for interfaces, strings, endpoints, and more) |
| 155 | * as needed by a given function instance. |
| 156 | */ |
Michal Nazarewicz | 28824b1 | 2010-05-05 12:53:13 +0200 | [diff] [blame] | 157 | struct usb_descriptor_header ** |
David Brownell | a4c39c4 | 2008-06-19 17:52:25 -0700 | [diff] [blame] | 158 | usb_copy_descriptors(struct usb_descriptor_header **src) |
| 159 | { |
| 160 | struct usb_descriptor_header **tmp; |
| 161 | unsigned bytes; |
| 162 | unsigned n_desc; |
| 163 | void *mem; |
| 164 | struct usb_descriptor_header **ret; |
| 165 | |
| 166 | /* count descriptors and their sizes; then add vector size */ |
| 167 | for (bytes = 0, n_desc = 0, tmp = src; *tmp; tmp++, n_desc++) |
| 168 | bytes += (*tmp)->bLength; |
| 169 | bytes += (n_desc + 1) * sizeof(*tmp); |
| 170 | |
| 171 | mem = kmalloc(bytes, GFP_KERNEL); |
| 172 | if (!mem) |
| 173 | return NULL; |
| 174 | |
| 175 | /* fill in pointers starting at "tmp", |
| 176 | * to descriptors copied starting at "mem"; |
| 177 | * and return "ret" |
| 178 | */ |
| 179 | tmp = mem; |
| 180 | ret = mem; |
| 181 | mem += (n_desc + 1) * sizeof(*tmp); |
| 182 | while (*src) { |
| 183 | memcpy(mem, *src, (*src)->bLength); |
| 184 | *tmp = mem; |
| 185 | tmp++; |
| 186 | mem += (*src)->bLength; |
| 187 | src++; |
| 188 | } |
| 189 | *tmp = NULL; |
| 190 | |
| 191 | return ret; |
| 192 | } |
Arve Hjønnevåg | 6ef3aa4 | 2013-04-18 18:58:11 -0700 | [diff] [blame] | 193 | EXPORT_SYMBOL_GPL(usb_copy_descriptors); |
David Brownell | a4c39c4 | 2008-06-19 17:52:25 -0700 | [diff] [blame] | 194 | |
Arve Hjønnevåg | 6ef3aa4 | 2013-04-18 18:58:11 -0700 | [diff] [blame] | 195 | int usb_assign_descriptors(struct usb_function *f, |
| 196 | struct usb_descriptor_header **fs, |
| 197 | struct usb_descriptor_header **hs, |
| 198 | struct usb_descriptor_header **ss) |
| 199 | { |
| 200 | struct usb_gadget *g = f->config->cdev->gadget; |
| 201 | |
| 202 | if (fs) { |
| 203 | f->fs_descriptors = usb_copy_descriptors(fs); |
| 204 | if (!f->fs_descriptors) |
| 205 | goto err; |
| 206 | } |
| 207 | if (hs && gadget_is_dualspeed(g)) { |
| 208 | f->hs_descriptors = usb_copy_descriptors(hs); |
| 209 | if (!f->hs_descriptors) |
| 210 | goto err; |
| 211 | } |
| 212 | if (ss && gadget_is_superspeed(g)) { |
| 213 | f->ss_descriptors = usb_copy_descriptors(ss); |
| 214 | if (!f->ss_descriptors) |
| 215 | goto err; |
| 216 | } |
| 217 | return 0; |
| 218 | err: |
| 219 | usb_free_all_descriptors(f); |
| 220 | return -ENOMEM; |
| 221 | } |
| 222 | EXPORT_SYMBOL_GPL(usb_assign_descriptors); |
| 223 | |
| 224 | void usb_free_all_descriptors(struct usb_function *f) |
| 225 | { |
| 226 | usb_free_descriptors(f->fs_descriptors); |
| 227 | usb_free_descriptors(f->hs_descriptors); |
| 228 | usb_free_descriptors(f->ss_descriptors); |
| 229 | } |
| 230 | EXPORT_SYMBOL_GPL(usb_free_all_descriptors); |