Revert "usb: thor: fix possible alignment issues"
[profile/mobile/platform/kernel/u-boot-tm1.git] / property / usb / cdc_descriptor.c
1 #include <asm/arch/sci_types.h>
2 #include <linux/usb/ch9.h>
3 #include <linux/usb/cdc.h>
4 #include <linux/usb/gadget.h>
5 #include <linux/byteorder/little_endian.h>
6 #include <linux/string.h>
7
8 #define THOR_VENDOR_NUM                 0x04E8
9 #define THOR_PRODUCT_NUM                0x685D
10
11 static struct usb_device_descriptor thor_device_desc_high __align (32)= {
12         .bLength                        = sizeof(thor_device_desc_high),
13         .bDescriptorType        = USB_DT_DEVICE,
14
15         .bcdUSB                         = __constant_cpu_to_le16(0x0200),
16         .bDeviceClass           = USB_CLASS_COMM,
17
18         .bDeviceSubClass        = 0x02,
19         .bDeviceProtocol        = 0x00,
20
21         .bMaxPacketSize0        = 64,
22
23         .idVendor                       = __constant_cpu_to_le16(THOR_VENDOR_NUM),
24         .idProduct                      = __constant_cpu_to_le16(THOR_PRODUCT_NUM),
25         .bcdDevice                      = cpu_to_le16(0x021B),
26
27         .iManufacturer          = 0x01,
28         .iProduct                       = 0x02,
29
30         .iSerialNumber          = 0x00,
31         .bNumConfigurations     = 0x01,
32 };
33
34 static struct usb_device_descriptor thor_device_desc_full __align (32) = {
35         .bLength                        = sizeof(thor_device_desc_full),
36         .bDescriptorType        = USB_DT_DEVICE,
37
38         .bcdUSB                         = __constant_cpu_to_le16(0x0101),
39         .bDeviceClass           = USB_CLASS_COMM,
40
41         .bDeviceSubClass        = 0x02,
42         .bDeviceProtocol        = 0x00,
43
44         .bMaxPacketSize0        = 64,
45
46         .idVendor                       = __constant_cpu_to_le16(THOR_VENDOR_NUM),
47         .idProduct                      = __constant_cpu_to_le16(THOR_PRODUCT_NUM),
48         .bcdDevice                      = cpu_to_le16(0x021B),
49
50         .iManufacturer          = 0x01,
51         .iProduct                       = 0x02,
52
53         .iSerialNumber          = 0x00,
54         .bNumConfigurations     = 0x01,
55 };
56
57 /* function descriptor */
58 static const struct usb_config_descriptor thor_config_desc __align (32) = {
59         .bLength                                = sizeof(thor_config_desc),
60         .bDescriptorType                = USB_DT_CONFIG,
61
62         .wTotalLength                   = __constant_cpu_to_le16(67),
63
64         .bNumInterfaces                 = 0x02,
65         .bConfigurationValue    = 0x01,
66         .iConfiguration                 = 0x00,
67
68         .bmAttributes                   = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
69         .bMaxPower                              = 0x19,
70 };
71
72 static struct usb_interface_descriptor thor_downloader_intf_init __align (32) = {
73         .bLength                                = sizeof(thor_downloader_intf_init),
74         .bDescriptorType                = USB_DT_INTERFACE,
75
76         .bInterfaceNumber               = 0,
77         .bAlternateSetting              = 0,
78         .bNumEndpoints                  = 1,
79
80         .bInterfaceClass                = USB_CLASS_COMM,
81
82         .bInterfaceSubClass             = USB_CDC_SUBCLASS_ACM,
83         .bInterfaceProtocol             = USB_CDC_ACM_PROTO_AT_V25TER,
84         .iInterface                             = 3,
85 };
86
87 static const struct usb_cdc_header_desc thor_downloader_func_desc __align (32) = {
88         .bLength                                = sizeof(thor_downloader_func_desc),
89         .bDescriptorType                = USB_DT_CS_INTERFACE,
90         .bDescriptorSubType             = USB_CDC_HEADER_TYPE,
91         .bcdCDC                                 = __constant_cpu_to_le16(0x0110),
92 };
93
94 static struct usb_cdc_call_mgmt_descriptor thor_downloader_func_desc_call __align (32) = {
95         .bLength                                = sizeof(thor_downloader_func_desc_call),
96         .bDescriptorType                = USB_DT_CS_INTERFACE,
97         .bDescriptorSubType             = USB_CDC_CALL_MANAGEMENT_TYPE,
98         .bmCapabilities                 = 0x00,
99         .bDataInterface                 = 0x01,
100 };
101
102 static struct usb_cdc_acm_descriptor thor_downloader_func_desc_abstract __align (32) = {
103         .bLength                                = 0x04,
104         .bDescriptorType                = USB_DT_CS_INTERFACE,
105         .bDescriptorSubType             = USB_CDC_ACM_TYPE,
106         .bmCapabilities                 = 0x0F,
107 };
108
109 static struct usb_cdc_union_desc thor_downloader_cdc_union __align (32) = {
110         .bLength                                = 0x05,
111         .bDescriptorType                = USB_DT_CS_INTERFACE,
112         .bDescriptorSubType             = USB_CDC_UNION_TYPE,
113         .bMasterInterface0              = 0x00,
114         .bSlaveInterface0               = 0x01,
115 };
116
117 static struct usb_endpoint_descriptor thor_downloader_ep3_in __align (32) = {
118         .bLength                                = USB_DT_ENDPOINT_SIZE,
119         .bDescriptorType                = USB_DT_ENDPOINT,
120         .bEndpointAddress               = 0x83,
121         .bmAttributes                   = USB_ENDPOINT_XFER_INT,
122         .wMaxPacketSize                 = __constant_cpu_to_le16(16),
123         .bInterval = 0x9,
124 };
125
126 static const struct usb_interface_descriptor thor_downloader_intf_data __align (32) = {
127         .bLength                                = USB_DT_INTERFACE_SIZE,
128         .bDescriptorType                = USB_DT_INTERFACE,
129
130         .bInterfaceNumber               = 1,
131         .bAlternateSetting              = 0,
132
133         .bNumEndpoints                  = 2,
134         .bInterfaceClass                = USB_CLASS_CDC_DATA,
135
136         .bInterfaceSubClass             = 0,
137         .bInterfaceProtocol             = 0,
138         .iInterface                             = 4,
139 };
140
141 static struct usb_endpoint_descriptor thor_downloader_ep1_in __align (32) = {
142         .bLength                        = USB_DT_ENDPOINT_SIZE,
143         .bDescriptorType        = USB_DT_ENDPOINT,
144         .bEndpointAddress       = 0x81,
145         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
146         .wMaxPacketSize         = __constant_cpu_to_le16(512),
147         .bInterval                      = 0,
148 };
149
150 static struct usb_endpoint_descriptor thor_downloader_ep2_out __align (32) = {
151         .bLength                        = USB_DT_ENDPOINT_SIZE,
152         .bDescriptorType        = USB_DT_ENDPOINT,
153         .bEndpointAddress       = 0x02,
154         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
155         .wMaxPacketSize         = __constant_cpu_to_le16(512),
156         .bInterval                      = 0,
157 };
158
159 static const struct usb_descriptor_header *thor_function_high[] __align (32) = {
160         (struct usb_descriptor_header *) &thor_downloader_intf_init,
161         (struct usb_descriptor_header *) &thor_downloader_func_desc,
162         (struct usb_descriptor_header *) &thor_downloader_func_desc_call,
163         (struct usb_descriptor_header *) &thor_downloader_func_desc_abstract,
164         (struct usb_descriptor_header *) &thor_downloader_cdc_union,
165         (struct usb_descriptor_header *) &thor_downloader_ep3_in,
166         (struct usb_descriptor_header *) &thor_downloader_intf_data,
167         (struct usb_descriptor_header *) &thor_downloader_ep1_in,
168         (struct usb_descriptor_header *) &thor_downloader_ep2_out,
169 };
170
171 static struct usb_cdc_acm_descriptor thor_downloader_func_desc_abstract_full __align (32) = {
172         .bLength                                = 0x04,
173         .bDescriptorType                = USB_DT_CS_INTERFACE,
174         .bDescriptorSubType             = USB_CDC_ACM_TYPE,
175         .bmCapabilities                 = 0x00,
176 };
177
178 static struct usb_endpoint_descriptor thor_downloader_ep1_in_full __align (32) = {
179         .bLength                        = USB_DT_ENDPOINT_SIZE,
180         .bDescriptorType        = USB_DT_ENDPOINT,
181         .bEndpointAddress       = 0x81,
182         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
183         .wMaxPacketSize         = __constant_cpu_to_le16(64),
184         .bInterval                      = 0,
185 };
186
187 static struct usb_endpoint_descriptor thor_downloader_ep2_out_full __align (32) = {
188         .bLength                        = USB_DT_ENDPOINT_SIZE,
189         .bDescriptorType        = USB_DT_ENDPOINT,
190         .bEndpointAddress       = 0x02,
191         .bmAttributes           = USB_ENDPOINT_XFER_BULK,
192         .wMaxPacketSize         = __constant_cpu_to_le16(64),
193         .bInterval                      = 0,
194 };
195
196 static const struct usb_descriptor_header *thor_function_full[] __align (32) = {
197         (struct usb_descriptor_header *) &thor_downloader_intf_init,
198         (struct usb_descriptor_header *) &thor_downloader_func_desc,
199         (struct usb_descriptor_header *) &thor_downloader_func_desc_call,
200         (struct usb_descriptor_header *) &thor_downloader_func_desc_abstract_full,
201         (struct usb_descriptor_header *) &thor_downloader_cdc_union,
202         (struct usb_descriptor_header *) &thor_downloader_ep3_in,
203         (struct usb_descriptor_header *) &thor_downloader_intf_data,
204         (struct usb_descriptor_header *) &thor_downloader_ep1_in_full,
205         (struct usb_descriptor_header *) &thor_downloader_ep2_out_full,
206 };
207
208 static char function_desc_buf[67] __align(32);
209
210 #define CONFIG_THOR_STRING_PRODUCT                      "SAMSUNG USB DRIVER"
211 static char string_product[2 + 2 * (sizeof(CONFIG_THOR_STRING_PRODUCT) - 1)] __align(32);
212
213 #define CONFIG_THOR_STRING_MANUFACTURER         "SAMSUNG"
214 static char string_manufacturer[2 + 2 * (sizeof(CONFIG_THOR_STRING_MANUFACTURER) - 1)] __align(32);
215
216 static char string_lang_ids[4] __align (32) = {4, USB_DT_STRING, 0x9, 0x4};
217
218 #define CONFIG_THOR_STRING_INTERFACE            "CDC Abstract Control Model"
219 static char string_interface[2 + 2 * (sizeof(CONFIG_THOR_STRING_INTERFACE) - 1)] __align(32);
220
221 #define CONFIG_THOR_STRING_CONTROL                      "SAMSUNG SERIAL CONTROL"
222 static char string_control[2 + 2 * (sizeof(CONFIG_THOR_STRING_CONTROL) - 1)] __align(32);
223
224 static struct usb_qualifier_descriptor thor_dev_qualifier_desc __align(32) = {
225         .bLength                        = sizeof(thor_dev_qualifier_desc),
226         .bDescriptorType        = USB_DT_DEVICE_QUALIFIER,
227         .bcdUSB                         = __constant_cpu_to_le16(0x0200),
228         .bDeviceClass           = USB_CLASS_VENDOR_SPEC,
229
230         .bDeviceSubClass        = 0x00,
231         .bDeviceProtocol        = 0x00,
232         .bMaxPacketSize0        = 64,
233
234         .bNumConfigurations     = 0x01,
235         .bRESERVED                      = 0x00,
236 };
237
238 unsigned char g_USB_Other_Speed_ConfigDescr[] __align(32) =
239 {
240         0x09,0x07,0x43,0x00,0x02,0x01,0x00,0xC0,
241         0x19,
242         0x09,0x04,0x00,0x00,0x01,0x02,0x02,0x01,
243         0x03,
244         0x05,0x24,0x00,0x10,0x01,
245         0x05,0x24,0x01,0x00,0x01,
246         0x04,0x24,0x02,0x0f,
247         0x05,0x24,0x06,0x00,0x01,
248         0x07,0x05,0x83,0x03,0x10,0x00,0x9,      //Ep 3 In       64
249         0x09,0x04,0x01,0x00,0x02,0x0A,0x00,0x00,
250         0x04,
251         0x07,0x05,0x81,0x02,0x40,0x00,0x00,  //Ep 1 In  64    
252         0x07,0x05,0x02,0x02,0x40,0x00,0x00,  //Ep 2 OUT  64                     
253 };
254
255 unsigned char *thor_get_device_desc(unsigned int speed)
256 {
257         if (speed == 0x00)              /* USB HIGH */
258                 return (unsigned char *) &thor_device_desc_high;
259         else
260                 return (unsigned char *) &thor_device_desc_full;
261 }
262
263 unsigned char *thor_get_config_desc(unsigned int speed)
264 {
265         usb_gadget_config_buf(&thor_config_desc, &function_desc_buf, 256, thor_function_high);
266
267         if (speed == 0x00)              /* USB_HIGH */
268                 usb_gadget_config_buf(&thor_config_desc, &function_desc_buf, 256, thor_function_high);
269         else
270                 usb_gadget_config_buf(&thor_config_desc, &function_desc_buf, 256, thor_function_full);
271
272         return (unsigned char *) &function_desc_buf;
273 }
274
275 static void str2wide (char *str, u16 * wide)
276 {
277         int i;
278         for (i = 0; i < strlen (str) && str[i]; i++){
279                 #if defined(__LITTLE_ENDIAN)
280                         wide[i] = (u16) str[i];
281                 #elif defined(__BIG_ENDIAN)
282                         wide[i] = ((u16)(str[i])<<8);
283                 #else
284                         #error "__LITTLE_ENDIAN or __BIG_ENDIAN undefined"
285                 #endif
286         }
287 }
288
289 unsigned char *thor_get_string_desc(unsigned char index)
290 {
291         struct usb_string_descriptor *string;
292
293         switch (index) {
294                 case 0:
295                         string = (struct usb_string_descriptor *) string_lang_ids;
296
297                         return (unsigned char *) string_lang_ids;
298                 case 1:
299                         string = (struct usb_string_descriptor *) string_manufacturer;
300                         string->bLength = sizeof(string_manufacturer);
301                         string->bDescriptorType = USB_DT_STRING;
302                         str2wide(CONFIG_THOR_STRING_MANUFACTURER, string->wData);
303
304                         return (unsigned char *) string;
305                 case 3:
306                         string = (struct usb_string_descriptor *) string_interface;
307                         string->bLength = sizeof(string_interface);
308                         string->bDescriptorType = USB_DT_STRING;
309                         str2wide(CONFIG_THOR_STRING_INTERFACE, string->wData);
310
311                         return (unsigned char *) string;
312                 case 6:
313                         string = (struct usb_string_descriptor *) string_control;
314                         string->bLength = sizeof(string_control);
315                         string->bDescriptorType = USB_DT_STRING;
316                         str2wide(CONFIG_THOR_STRING_CONTROL, string->wData);
317
318                         return (unsigned char *) string;
319                 case 2:
320                 default:
321                         string = (struct usb_string_descriptor *) string_product;
322                         string->bLength = sizeof(string_product);
323                         string->bDescriptorType = USB_DT_STRING;
324                         str2wide(CONFIG_THOR_STRING_PRODUCT, string->wData);
325
326                         return (unsigned char *) string;
327         }
328
329     return NULL;
330 }
331
332 unsigned char *thor_get_qualifer_desc(void)
333 {
334     return (unsigned char *) &thor_dev_qualifier_desc;
335 }
336
337 unsigned char *thor_get_other_speed_config_desc(void)
338 {
339     return (unsigned char *) g_USB_Other_Speed_ConfigDescr;
340 }