1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
34
35
36
37
38
39#include "composite.c"
40#include "usbstring.c"
41#include "config.c"
42#include "epautoconf.c"
43
44#include "f_ncm.c"
45#include "u_ether.c"
46
47
48
49
50
51
52
53
54
55
56#define CDC_VENDOR_NUM 0x0525
57#define CDC_PRODUCT_NUM 0xa4a1
58
59
60
61static struct usb_device_descriptor device_desc = {
62 .bLength = sizeof device_desc,
63 .bDescriptorType = USB_DT_DEVICE,
64
65 .bcdUSB = cpu_to_le16 (0x0200),
66
67 .bDeviceClass = USB_CLASS_COMM,
68 .bDeviceSubClass = 0,
69 .bDeviceProtocol = 0,
70
71
72
73
74
75
76 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
77 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
78
79
80
81
82 .bNumConfigurations = 1,
83};
84
85static struct usb_otg_descriptor otg_descriptor = {
86 .bLength = sizeof otg_descriptor,
87 .bDescriptorType = USB_DT_OTG,
88
89
90
91
92 .bmAttributes = USB_OTG_SRP | USB_OTG_HNP,
93};
94
95static const struct usb_descriptor_header *otg_desc[] = {
96 (struct usb_descriptor_header *) &otg_descriptor,
97 NULL,
98};
99
100
101
102
103#define STRING_MANUFACTURER_IDX 0
104#define STRING_PRODUCT_IDX 1
105
106static char manufacturer[50];
107
108static struct usb_string strings_dev[] = {
109 [STRING_MANUFACTURER_IDX].s = manufacturer,
110 [STRING_PRODUCT_IDX].s = DRIVER_DESC,
111 { }
112};
113
114static struct usb_gadget_strings stringtab_dev = {
115 .language = 0x0409,
116 .strings = strings_dev,
117};
118
119static struct usb_gadget_strings *dev_strings[] = {
120 &stringtab_dev,
121 NULL,
122};
123
124static u8 hostaddr[ETH_ALEN];
125
126
127
128static int __init ncm_do_config(struct usb_configuration *c)
129{
130
131
132 if (gadget_is_otg(c->cdev->gadget)) {
133 c->descriptors = otg_desc;
134 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
135 }
136
137 return ncm_bind_config(c, hostaddr);
138}
139
140static struct usb_configuration ncm_config_driver = {
141
142 .label = "CDC Ethernet (NCM)",
143 .bConfigurationValue = 1,
144
145 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
146};
147
148
149
150static int __init gncm_bind(struct usb_composite_dev *cdev)
151{
152 int gcnum;
153 struct usb_gadget *gadget = cdev->gadget;
154 int status;
155
156
157 status = gether_setup(cdev->gadget, hostaddr);
158 if (status < 0)
159 return status;
160
161 gcnum = usb_gadget_controller_number(gadget);
162 if (gcnum >= 0)
163 device_desc.bcdDevice = cpu_to_le16(0x0300 | gcnum);
164 else {
165
166
167
168
169 dev_warn(&gadget->dev,
170 "controller '%s' not recognized; trying %s\n",
171 gadget->name,
172 ncm_config_driver.label);
173 device_desc.bcdDevice =
174 cpu_to_le16(0x0300 | 0x0099);
175 }
176
177
178
179
180
181
182
183 snprintf(manufacturer, sizeof manufacturer, "%s %s with %s",
184 init_utsname()->sysname, init_utsname()->release,
185 gadget->name);
186 status = usb_string_id(cdev);
187 if (status < 0)
188 goto fail;
189 strings_dev[STRING_MANUFACTURER_IDX].id = status;
190 device_desc.iManufacturer = status;
191
192 status = usb_string_id(cdev);
193 if (status < 0)
194 goto fail;
195 strings_dev[STRING_PRODUCT_IDX].id = status;
196 device_desc.iProduct = status;
197
198 status = usb_add_config(cdev, &ncm_config_driver,
199 ncm_do_config);
200 if (status < 0)
201 goto fail;
202
203 dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
204
205 return 0;
206
207fail:
208 gether_cleanup();
209 return status;
210}
211
212static int __exit gncm_unbind(struct usb_composite_dev *cdev)
213{
214 gether_cleanup();
215 return 0;
216}
217
218static struct usb_composite_driver ncm_driver = {
219 .name = "g_ncm",
220 .dev = &device_desc,
221 .strings = dev_strings,
222 .max_speed = USB_SPEED_HIGH,
223 .unbind = __exit_p(gncm_unbind),
224};
225
226MODULE_DESCRIPTION(DRIVER_DESC);
227MODULE_AUTHOR("Yauheni Kaliuta");
228MODULE_LICENSE("GPL");
229
230static int __init init(void)
231{
232 return usb_composite_probe(&ncm_driver, gncm_bind);
233}
234module_init(init);
235
236static void __exit cleanup(void)
237{
238 usb_composite_unregister(&ncm_driver);
239}
240module_exit(cleanup);
241