ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / gadget / config.c
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  * This program 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 this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <linux/errno.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/string.h>
25 #include <linux/device.h>
26
27 #include <linux/usb_ch9.h>
28
29
30 /**
31  * usb_descriptor_fillbuf - fill buffer with descriptors
32  * @buf: Buffer to be filled
33  * @buflen: Size of buf
34  * @src: Array of descriptor pointers, terminated by null pointer.
35  *
36  * Copies descriptors into the buffer, returning the length or a
37  * negative error code if they can't all be copied.  Useful when
38  * assembling descriptors for an associated set of interfaces used
39  * as part of configuring a composite device; or in other cases where
40  * sets of descriptors need to be marshaled.
41  */
42 int
43 usb_descriptor_fillbuf(void *buf, unsigned buflen,
44                 const struct usb_descriptor_header **src)
45 {
46         u8      *dest = buf;
47
48         if (!src)
49                 return -EINVAL;
50
51         /* fill buffer from src[] until null descriptor ptr */
52         for (; 0 != *src; src++) {
53                 unsigned                len = (*src)->bLength;
54
55                 if (len > buflen)
56                         return -EINVAL;
57                 memcpy(dest, *src, len);
58                 buflen -= len;
59                 dest += len;
60         }
61         return dest - (u8 *)buf;
62 }
63
64
65 /**
66  * usb_gadget_config_buf - builts a complete configuration descriptor
67  * @config: Header for the descriptor, including characteristics such
68  *      as power requirements and number of interfaces.
69  * @desc: Null-terminated vector of pointers to the descriptors (interface,
70  *      endpoint, etc) defining all functions in this device configuration.
71  * @buf: Buffer for the resulting configuration descriptor.
72  * @length: Length of buffer.  If this is not big enough to hold the
73  *      entire configuration descriptor, an error code will be returned.
74  *
75  * This copies descriptors into the response buffer, building a descriptor
76  * for that configuration.  It returns the buffer length or a negative
77  * status code.  The config.wTotalLength field is set to match the length
78  * of the result, but other descriptor fields (including power usage and
79  * interface count) must be set by the caller.
80  *
81  * Gadget drivers could use this when constructing a config descriptor
82  * in response to USB_REQ_GET_DESCRIPTOR.  They will need to patch the
83  * resulting bDescriptorType value if USB_DT_OTHER_SPEED_CONFIG is needed.
84  */
85 int usb_gadget_config_buf(
86         const struct usb_config_descriptor      *config,
87         void                                    *buf,
88         unsigned                                length,
89         const struct usb_descriptor_header      **desc
90 )
91 {
92         struct usb_config_descriptor            *cp = buf;
93         int                                     len;
94
95         /* config descriptor first */
96         if (length < USB_DT_CONFIG_SIZE || !desc)
97                 return -EINVAL;
98         *cp = *config; 
99
100         /* then interface/endpoint/class/vendor/... */
101         len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (u8*)buf,
102                         length - USB_DT_CONFIG_SIZE, desc);
103         if (len < 0)
104                 return len;
105         len += USB_DT_CONFIG_SIZE;
106         if (len > 0xffff)
107                 return -EINVAL;
108
109         /* patch up the config descriptor */
110         cp->bLength = USB_DT_CONFIG_SIZE;
111         cp->bDescriptorType = USB_DT_CONFIG;
112         cp->wTotalLength = cpu_to_le16(len);
113         cp->bmAttributes |= USB_CONFIG_ATT_ONE;
114         return len;
115 }
116