1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/usb/composite.h>
21
22#include "u_ether.h"
23#include "u_ncm.h"
24
25#define DRIVER_DESC "NCM Gadget"
26
27
28
29
30
31
32
33
34
35
36#define CDC_VENDOR_NUM 0x0525
37#define CDC_PRODUCT_NUM 0xa4a1
38
39
40USB_GADGET_COMPOSITE_OPTIONS();
41
42USB_ETHERNET_MODULE_PARAMETERS();
43
44static struct usb_device_descriptor device_desc = {
45 .bLength = sizeof device_desc,
46 .bDescriptorType = USB_DT_DEVICE,
47
48
49
50 .bDeviceClass = USB_CLASS_COMM,
51 .bDeviceSubClass = 0,
52 .bDeviceProtocol = 0,
53
54
55
56
57
58
59 .idVendor = cpu_to_le16 (CDC_VENDOR_NUM),
60 .idProduct = cpu_to_le16 (CDC_PRODUCT_NUM),
61
62
63
64
65 .bNumConfigurations = 1,
66};
67
68static const struct usb_descriptor_header *otg_desc[2];
69
70
71static struct usb_string strings_dev[] = {
72 [USB_GADGET_MANUFACTURER_IDX].s = "",
73 [USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
74 [USB_GADGET_SERIAL_IDX].s = "",
75 { }
76};
77
78static struct usb_gadget_strings stringtab_dev = {
79 .language = 0x0409,
80 .strings = strings_dev,
81};
82
83static struct usb_gadget_strings *dev_strings[] = {
84 &stringtab_dev,
85 NULL,
86};
87
88static struct usb_function_instance *f_ncm_inst;
89static struct usb_function *f_ncm;
90
91
92
93static int ncm_do_config(struct usb_configuration *c)
94{
95 int status;
96
97
98
99 if (gadget_is_otg(c->cdev->gadget)) {
100 c->descriptors = otg_desc;
101 c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
102 }
103
104 f_ncm = usb_get_function(f_ncm_inst);
105 if (IS_ERR(f_ncm)) {
106 status = PTR_ERR(f_ncm);
107 return status;
108 }
109
110 status = usb_add_function(c, f_ncm);
111 if (status < 0) {
112 usb_put_function(f_ncm);
113 return status;
114 }
115
116 return 0;
117}
118
119static struct usb_configuration ncm_config_driver = {
120
121 .label = "CDC Ethernet (NCM)",
122 .bConfigurationValue = 1,
123
124 .bmAttributes = USB_CONFIG_ATT_SELFPOWER,
125};
126
127
128
129static int gncm_bind(struct usb_composite_dev *cdev)
130{
131 struct usb_gadget *gadget = cdev->gadget;
132 struct f_ncm_opts *ncm_opts;
133 int status;
134
135 f_ncm_inst = usb_get_function_instance("ncm");
136 if (IS_ERR(f_ncm_inst))
137 return PTR_ERR(f_ncm_inst);
138
139 ncm_opts = container_of(f_ncm_inst, struct f_ncm_opts, func_inst);
140
141 gether_set_qmult(ncm_opts->net, qmult);
142 if (!gether_set_host_addr(ncm_opts->net, host_addr))
143 pr_info("using host ethernet address: %s", host_addr);
144 if (!gether_set_dev_addr(ncm_opts->net, dev_addr))
145 pr_info("using self ethernet address: %s", dev_addr);
146
147
148
149
150
151 status = usb_string_ids_tab(cdev, strings_dev);
152 if (status < 0)
153 goto fail;
154 device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
155 device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
156
157 if (gadget_is_otg(gadget) && !otg_desc[0]) {
158 struct usb_descriptor_header *usb_desc;
159
160 usb_desc = usb_otg_descriptor_alloc(gadget);
161 if (!usb_desc)
162 goto fail;
163 usb_otg_descriptor_init(gadget, usb_desc);
164 otg_desc[0] = usb_desc;
165 otg_desc[1] = NULL;
166 }
167
168 status = usb_add_config(cdev, &ncm_config_driver,
169 ncm_do_config);
170 if (status < 0)
171 goto fail1;
172
173 usb_composite_overwrite_options(cdev, &coverwrite);
174 dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
175
176 return 0;
177
178fail1:
179 kfree(otg_desc[0]);
180 otg_desc[0] = NULL;
181fail:
182 usb_put_function_instance(f_ncm_inst);
183 return status;
184}
185
186static int gncm_unbind(struct usb_composite_dev *cdev)
187{
188 if (!IS_ERR_OR_NULL(f_ncm))
189 usb_put_function(f_ncm);
190 if (!IS_ERR_OR_NULL(f_ncm_inst))
191 usb_put_function_instance(f_ncm_inst);
192 kfree(otg_desc[0]);
193 otg_desc[0] = NULL;
194
195 return 0;
196}
197
198static struct usb_composite_driver ncm_driver = {
199 .name = "g_ncm",
200 .dev = &device_desc,
201 .strings = dev_strings,
202 .max_speed = USB_SPEED_HIGH,
203 .bind = gncm_bind,
204 .unbind = gncm_unbind,
205};
206
207module_usb_composite_driver(ncm_driver);
208
209MODULE_DESCRIPTION(DRIVER_DESC);
210MODULE_AUTHOR("Yauheni Kaliuta");
211MODULE_LICENSE("GPL");
212