usb: gadget: use usb_string_ids_tab instead multiple usb_string_id()
[deliverable/linux.git] / drivers / usb / gadget / ncm.c
CommitLineData
6c34d288
YK
1/*
2 * ncm.c -- NCM gadget driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
5 * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
6 *
7 * The driver borrows from ether.c which is:
8 *
9 * Copyright (C) 2003-2005,2008 David Brownell
10 * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
11 * Copyright (C) 2008 Nokia Corporation
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
6c34d288
YK
17 */
18
19/* #define DEBUG */
20/* #define VERBOSE_DEBUG */
21
22#include <linux/kernel.h>
23#include <linux/utsname.h>
24
25
26#include "u_ether.h"
27
28#define DRIVER_DESC "NCM Gadget"
29
30/*-------------------------------------------------------------------------*/
31
32/*
33 * Kbuild is not very cooperative with respect to linking separately
34 * compiled library objects into one module. So for now we won't use
35 * separate compilation ... ensuring init/exit sections work to shrink
36 * the runtime footprint, and giving us at least some parts of what
37 * a "gcc --combine ... part1.c part2.c part3.c ... " build would.
38 */
39#include "composite.c"
6c34d288
YK
40
41#include "f_ncm.c"
42#include "u_ether.c"
43
44/*-------------------------------------------------------------------------*/
45
46/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
47 * Instead: allocate your own, using normal USB-IF procedures.
48 */
49
50/* Thanks to NetChip Technologies for donating this product ID.
51 * It's for devices with only CDC Ethernet configurations.
52 */
53#define CDC_VENDOR_NUM 0x0525 /* NetChip */
54#define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
55
56/*-------------------------------------------------------------------------*/
57
58static struct usb_device_descriptor device_desc = {
59 .bLength = sizeof device_desc,
60 .bDescriptorType = USB_DT_DEVICE,
61
62 .bcdUSB = cpu_to_le16 (0x0200),
63
64 .bDeviceClass = USB_CLASS_COMM,
65 .bDeviceSubClass = 0,
66 .bDeviceProtocol = 0,
67 /* .bMaxPacketSize0 = f(hardware) */
68
69 /* Vendor and product id defaults change according to what configs
70 * we support. (As does bNumConfigurations.) These values can
71 * also be overridden by module parameters.
72 */
73 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
74 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
75 /* .bcdDevice = f(hardware) */
76 /* .iManufacturer = DYNAMIC */
77 /* .iProduct = DYNAMIC */
78 /* NO SERIAL NUMBER */
79 .bNumConfigurations = 1,
80};
81
82static struct usb_otg_descriptor otg_descriptor = {
83 .bLength = sizeof otg_descriptor,
84 .bDescriptorType = USB_DT_OTG,
85
86 /* REVISIT SRP-only hardware is possible, although
87 * it would not be called "OTG" ...
88 */
89 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
90};
91
92static const struct usb_descriptor_header *otg_desc[] = {
93 (struct usb_descriptor_header *) &otg_descriptor,
94 NULL,
95};
96
97
98/* string IDs are assigned dynamically */
99
100#define STRING_MANUFACTURER_IDX 0
101#define STRING_PRODUCT_IDX 1
102
103static char manufacturer[50];
104
105static struct usb_string strings_dev[] = {
106 [STRING_MANUFACTURER_IDX].s = manufacturer,
107 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
108 { } /* end of list */
109};
110
111static struct usb_gadget_strings stringtab_dev = {
112 .language = 0x0409, /* en-us */
113 .strings = strings_dev,
114};
115
116static struct usb_gadget_strings *dev_strings[] = {
117 &stringtab_dev,
118 NULL,
119};
120
121static u8 hostaddr[ETH_ALEN];
122
123/*-------------------------------------------------------------------------*/
124
125static int __init ncm_do_config(struct usb_configuration *c)
126{
127 /* FIXME alloc iConfiguration string, set it in c->strings */
128
129 if (gadget_is_otg(c->cdev->gadget)) {
130 c->descriptors = otg_desc;
131 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
132 }
133
134 return ncm_bind_config(c, hostaddr);
135}
136
137static struct usb_configuration ncm_config_driver = {
138 /* .label = f(hardware) */
139 .label = "CDC Ethernet (NCM)",
140 .bConfigurationValue = 1,
141 /* .iConfiguration = DYNAMIC */
142 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
143};
144
145/*-------------------------------------------------------------------------*/
146
147static int __init gncm_bind(struct usb_composite_dev *cdev)
148{
149 int gcnum;
150 struct usb_gadget *gadget = cdev->gadget;
151 int status;
152
153 /* set up network link layer */
154 status = gether_setup(cdev->gadget, hostaddr);
155 if (status < 0)
156 return status;
157
158 gcnum = usb_gadget_controller_number(gadget);
159 if (gcnum >= 0)
160 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
161 else {
162 /* We assume that can_support_ecm() tells the truth;
163 * but if the controller isn't recognized at all then
164 * that assumption is a bit more likely to be wrong.
165 */
166 dev_warn(&gadget->dev,
167 "controller '%s' not recognized; trying %s\n",
168 gadget->name,
169 ncm_config_driver.label);
170 device_desc.bcdDevice =
171 cpu_to_le16(0x0300 | 0x0099);
172 }
173
174
175 /* Allocate string descriptor numbers ... note that string
176 * contents can be overridden by the composite_dev glue.
177 */
178
179 /* device descriptor strings: manufacturer, product */
180 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
181 init_utsname()->sysname, init_utsname()->release,
182 gadget->name);
e1f15ccb 183 status = usb_string_ids_tab(cdev, strings_dev);
6c34d288
YK
184 if (status < 0)
185 goto fail;
e1f15ccb
SAS
186 device_desc.iManufacturer = strings_dev[STRING_MANUFACTURER_IDX].id;
187 device_desc.iProduct = strings_dev[STRING_PRODUCT_IDX].id;
6c34d288
YK
188
189 status = usb_add_config(cdev, &ncm_config_driver,
190 ncm_do_config);
191 if (status < 0)
192 goto fail;
193
194 dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
195
196 return 0;
197
198fail:
199 gether_cleanup();
200 return status;
201}
202
203static int __exit gncm_unbind(struct usb_composite_dev *cdev)
204{
205 gether_cleanup();
206 return 0;
207}
208
c2ec75c2 209static __refdata struct usb_composite_driver ncm_driver = {
6c34d288
YK
210 .name = "g_ncm",
211 .dev = &device_desc,
212 .strings = dev_strings,
35a0e0bf 213 .max_speed = USB_SPEED_HIGH,
03e42bd5 214 .bind = gncm_bind,
6c34d288
YK
215 .unbind = __exit_p(gncm_unbind),
216};
217
218MODULE_DESCRIPTION(DRIVER_DESC);
219MODULE_AUTHOR("Yauheni Kaliuta");
220MODULE_LICENSE("GPL");
221
222static int __init init(void)
223{
03e42bd5 224 return usb_composite_probe(&ncm_driver);
6c34d288
YK
225}
226module_init(init);
227
228static void __exit cleanup(void)
229{
230 usb_composite_unregister(&ncm_driver);
231}
232module_exit(cleanup);
This page took 0.138731 seconds and 5 git commands to generate.