usb: gadget: Provide a default implementation of default manufacturer string
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / usb / gadget / cdc2.c
CommitLineData
19e20680
DB
1/*
2 * cdc2.c -- CDC Composite driver, with ECM and ACM support
3 *
4 * Copyright (C) 2008 David Brownell
5 * Copyright (C) 2008 Nokia Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
19e20680
DB
11 */
12
13#include <linux/kernel.h>
6eb0de82 14#include <linux/module.h>
19e20680
DB
15
16#include "u_ether.h"
17#include "u_serial.h"
18
19
20#define DRIVER_DESC "CDC Composite Gadget"
21#define DRIVER_VERSION "King Kamehameha Day 2008"
22
23/*-------------------------------------------------------------------------*/
24
25/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
26 * Instead: allocate your own, using normal USB-IF procedures.
27 */
28
29/* Thanks to NetChip Technologies for donating this product ID.
30 * It's for devices with only this composite CDC configuration.
31 */
32#define CDC_VENDOR_NUM 0x0525 /* NetChip */
33#define CDC_PRODUCT_NUM 0xa4aa /* CDC Composite: ECM + ACM */
34
35/*-------------------------------------------------------------------------*/
7d16e8d3 36USB_GADGET_COMPOSITE_OPTIONS();
19e20680 37
8a1ce2c0
DB
38/*
39 * Kbuild is not very cooperative with respect to linking separately
40 * compiled library objects into one module. So for now we won't use
41 * separate compilation ... ensuring init/exit sections work to shrink
42 * the runtime footprint, and giving us at least some parts of what
43 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
44 */
45
46#include "composite.c"
8a1ce2c0
DB
47#include "u_serial.c"
48#include "f_acm.c"
49#include "f_ecm.c"
50#include "u_ether.c"
51
52/*-------------------------------------------------------------------------*/
53
19e20680
DB
54static struct usb_device_descriptor device_desc = {
55 .bLength = sizeof device_desc,
56 .bDescriptorType = USB_DT_DEVICE,
57
551509d2 58 .bcdUSB = cpu_to_le16(0x0200),
19e20680
DB
59
60 .bDeviceClass = USB_CLASS_COMM,
61 .bDeviceSubClass = 0,
62 .bDeviceProtocol = 0,
63 /* .bMaxPacketSize0 = f(hardware) */
64
65 /* Vendor and product id can be overridden by module parameters. */
551509d2
HH
66 .idVendor = cpu_to_le16(CDC_VENDOR_NUM),
67 .idProduct = cpu_to_le16(CDC_PRODUCT_NUM),
19e20680
DB
68 /* .bcdDevice = f(hardware) */
69 /* .iManufacturer = DYNAMIC */
70 /* .iProduct = DYNAMIC */
71 /* NO SERIAL NUMBER */
72 .bNumConfigurations = 1,
73};
74
75static struct usb_otg_descriptor otg_descriptor = {
76 .bLength = sizeof otg_descriptor,
77 .bDescriptorType = USB_DT_OTG,
78
79 /* REVISIT SRP-only hardware is possible, although
80 * it would not be called "OTG" ...
81 */
82 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
83};
84
85static const struct usb_descriptor_header *otg_desc[] = {
86 (struct usb_descriptor_header *) &otg_descriptor,
87 NULL,
88};
89
90
91/* string IDs are assigned dynamically */
19e20680 92static struct usb_string strings_dev[] = {
cc2683c3 93 [USB_GADGET_MANUFACTURER_IDX].s = "",
276e2e4f
SAS
94 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
95 [USB_GADGET_SERIAL_IDX].s = "",
19e20680
DB
96 { } /* end of list */
97};
98
99static struct usb_gadget_strings stringtab_dev = {
100 .language = 0x0409, /* en-us */
101 .strings = strings_dev,
102};
103
104static struct usb_gadget_strings *dev_strings[] = {
105 &stringtab_dev,
106 NULL,
107};
108
109static u8 hostaddr[ETH_ALEN];
110
111/*-------------------------------------------------------------------------*/
112
113/*
114 * We _always_ have both CDC ECM and CDC ACM functions.
115 */
e12995ec 116static int __init cdc_do_config(struct usb_configuration *c)
19e20680
DB
117{
118 int status;
119
120 if (gadget_is_otg(c->cdev->gadget)) {
121 c->descriptors = otg_desc;
122 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
123 }
124
125 status = ecm_bind_config(c, hostaddr);
126 if (status < 0)
127 return status;
128
129 status = acm_bind_config(c, 0);
ac90e365 130 if (status < 0)
19e20680
DB
131 return status;
132
133 return 0;
134}
135
136static struct usb_configuration cdc_config_driver = {
137 .label = "CDC Composite (ECM + ACM)",
19e20680
DB
138 .bConfigurationValue = 1,
139 /* .iConfiguration = DYNAMIC */
140 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
19e20680
DB
141};
142
143/*-------------------------------------------------------------------------*/
144
e12995ec 145static int __init cdc_bind(struct usb_composite_dev *cdev)
19e20680
DB
146{
147 int gcnum;
148 struct usb_gadget *gadget = cdev->gadget;
149 int status;
150
151 if (!can_support_ecm(cdev->gadget)) {
8a1ce2c0
DB
152 dev_err(&gadget->dev, "controller '%s' not usable\n",
153 gadget->name);
19e20680
DB
154 return -EINVAL;
155 }
156
157 /* set up network link layer */
158 status = gether_setup(cdev->gadget, hostaddr);
159 if (status < 0)
160 return status;
161
162 /* set up serial link layer */
163 status = gserial_setup(cdev->gadget, 1);
164 if (status < 0)
165 goto fail0;
166
167 gcnum = usb_gadget_controller_number(gadget);
168 if (gcnum >= 0)
169 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
170 else {
171 /* We assume that can_support_ecm() tells the truth;
172 * but if the controller isn't recognized at all then
173 * that assumption is a bit more likely to be wrong.
174 */
b6c63937 175 WARNING(cdev, "controller '%s' not recognized; trying %s\n",
19e20680
DB
176 gadget->name,
177 cdc_config_driver.label);
178 device_desc.bcdDevice =
551509d2 179 cpu_to_le16(0x0300 | 0x0099);
19e20680
DB
180 }
181
19e20680
DB
182 /* Allocate string descriptor numbers ... note that string
183 * contents can be overridden by the composite_dev glue.
184 */
185
e1f15ccb 186 status = usb_string_ids_tab(cdev, strings_dev);
19e20680
DB
187 if (status < 0)
188 goto fail1;
276e2e4f
SAS
189 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
190 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
19e20680
DB
191
192 /* register our configuration */
c9bfff9c 193 status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
19e20680
DB
194 if (status < 0)
195 goto fail1;
196
7d16e8d3 197 usb_composite_overwrite_options(cdev, &coverwrite);
8a1ce2c0
DB
198 dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
199 DRIVER_DESC);
19e20680
DB
200
201 return 0;
202
203fail1:
204 gserial_cleanup();
205fail0:
206 gether_cleanup();
207 return status;
208}
209
210static int __exit cdc_unbind(struct usb_composite_dev *cdev)
211{
212 gserial_cleanup();
213 gether_cleanup();
214 return 0;
215}
216
c2ec75c2 217static __refdata struct usb_composite_driver cdc_driver = {
19e20680
DB
218 .name = "g_cdc",
219 .dev = &device_desc,
220 .strings = dev_strings,
35a0e0bf 221 .max_speed = USB_SPEED_HIGH,
03e42bd5 222 .bind = cdc_bind,
19e20680
DB
223 .unbind = __exit_p(cdc_unbind),
224};
225
226MODULE_DESCRIPTION(DRIVER_DESC);
227MODULE_AUTHOR("David Brownell");
228MODULE_LICENSE("GPL");
229
230static int __init init(void)
231{
03e42bd5 232 return usb_composite_probe(&cdc_driver);
19e20680
DB
233}
234module_init(init);
235
236static void __exit cleanup(void)
237{
238 usb_composite_unregister(&cdc_driver);
239}
240module_exit(cleanup);