Loading...
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * f_subset.c -- "CDC Subset" Ethernet link function driver
4 *
5 * Copyright (C) 2003-2005,2008 David Brownell
6 * Copyright (C) 2008 Nokia Corporation
7 */
8
9#include <linux/slab.h>
10#include <linux/kernel.h>
11#include <linux/module.h>
12#include <linux/device.h>
13#include <linux/etherdevice.h>
14
15#include "u_ether.h"
16#include "u_ether_configfs.h"
17#include "u_gether.h"
18
19/*
20 * This function packages a simple "CDC Subset" Ethernet port with no real
21 * control mechanisms; just raw data transfer over two bulk endpoints.
22 * The data transfer model is exactly that of CDC Ethernet, which is
23 * why we call it the "CDC Subset".
24 *
25 * Because it's not standardized, this has some interoperability issues.
26 * They mostly relate to driver binding, since the data transfer model is
27 * so simple (CDC Ethernet). The original versions of this protocol used
28 * specific product/vendor IDs: byteswapped IDs for Digital Equipment's
29 * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported
30 * daughtercards with USB peripheral connectors. (It was used more often
31 * with other boards, using the Itsy identifiers.) Linux hosts recognized
32 * this with CONFIG_USB_ARMLINUX; these devices have only one configuration
33 * and one interface.
34 *
35 * At some point, MCCI defined a (nonconformant) CDC MDLM variant called
36 * "SAFE", which happens to have a mode which is identical to the "CDC
37 * Subset" in terms of data transfer and lack of control model. This was
38 * adopted by later Sharp Zaurus models, and by some other software which
39 * Linux hosts recognize with CONFIG_USB_NET_ZAURUS.
40 *
41 * Because Microsoft's RNDIS drivers are far from robust, we added a few
42 * descriptors to the CDC Subset code, making this code look like a SAFE
43 * implementation. This lets you use MCCI's host side MS-Windows drivers
44 * if you get fed up with RNDIS. It also makes it easier for composite
45 * drivers to work, since they can use class based binding instead of
46 * caring about specific product and vendor IDs.
47 */
48
49struct f_gether {
50 struct gether port;
51
52 char ethaddr[14];
53};
54
55static inline struct f_gether *func_to_geth(struct usb_function *f)
56{
57 return container_of(f, struct f_gether, port.func);
58}
59
60/*-------------------------------------------------------------------------*/
61
62/*
63 * "Simple" CDC-subset option is a simple vendor-neutral model that most
64 * full speed controllers can handle: one interface, two bulk endpoints.
65 * To assist host side drivers, we fancy it up a bit, and add descriptors so
66 * some host side drivers will understand it as a "SAFE" variant.
67 *
68 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways.
69 * Data endpoints live in the control interface, there's no data interface.
70 * And it's not used to talk to a cell phone radio.
71 */
72
73/* interface descriptor: */
74
75static struct usb_interface_descriptor subset_data_intf = {
76 .bLength = sizeof subset_data_intf,
77 .bDescriptorType = USB_DT_INTERFACE,
78
79 /* .bInterfaceNumber = DYNAMIC */
80 .bAlternateSetting = 0,
81 .bNumEndpoints = 2,
82 .bInterfaceClass = USB_CLASS_COMM,
83 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
84 .bInterfaceProtocol = 0,
85 /* .iInterface = DYNAMIC */
86};
87
88static struct usb_cdc_header_desc mdlm_header_desc = {
89 .bLength = sizeof mdlm_header_desc,
90 .bDescriptorType = USB_DT_CS_INTERFACE,
91 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
92
93 .bcdCDC = cpu_to_le16(0x0110),
94};
95
96static struct usb_cdc_mdlm_desc mdlm_desc = {
97 .bLength = sizeof mdlm_desc,
98 .bDescriptorType = USB_DT_CS_INTERFACE,
99 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
100
101 .bcdVersion = cpu_to_le16(0x0100),
102 .bGUID = {
103 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
104 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
105 },
106};
107
108/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
109 * can't really use its struct. All we do here is say that we're using
110 * the submode of "SAFE" which directly matches the CDC Subset.
111 */
112static u8 mdlm_detail_desc[] = {
113 6,
114 USB_DT_CS_INTERFACE,
115 USB_CDC_MDLM_DETAIL_TYPE,
116
117 0, /* "SAFE" */
118 0, /* network control capabilities (none) */
119 0, /* network data capabilities ("raw" encapsulation) */
120};
121
122static struct usb_cdc_ether_desc ether_desc = {
123 .bLength = sizeof ether_desc,
124 .bDescriptorType = USB_DT_CS_INTERFACE,
125 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
126
127 /* this descriptor actually adds value, surprise! */
128 /* .iMACAddress = DYNAMIC */
129 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
130 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
131 .wNumberMCFilters = cpu_to_le16(0),
132 .bNumberPowerFilters = 0,
133};
134
135/* full speed support: */
136
137static struct usb_endpoint_descriptor fs_subset_in_desc = {
138 .bLength = USB_DT_ENDPOINT_SIZE,
139 .bDescriptorType = USB_DT_ENDPOINT,
140
141 .bEndpointAddress = USB_DIR_IN,
142 .bmAttributes = USB_ENDPOINT_XFER_BULK,
143};
144
145static struct usb_endpoint_descriptor fs_subset_out_desc = {
146 .bLength = USB_DT_ENDPOINT_SIZE,
147 .bDescriptorType = USB_DT_ENDPOINT,
148
149 .bEndpointAddress = USB_DIR_OUT,
150 .bmAttributes = USB_ENDPOINT_XFER_BULK,
151};
152
153static struct usb_descriptor_header *fs_eth_function[] = {
154 (struct usb_descriptor_header *) &subset_data_intf,
155 (struct usb_descriptor_header *) &mdlm_header_desc,
156 (struct usb_descriptor_header *) &mdlm_desc,
157 (struct usb_descriptor_header *) &mdlm_detail_desc,
158 (struct usb_descriptor_header *) ðer_desc,
159 (struct usb_descriptor_header *) &fs_subset_in_desc,
160 (struct usb_descriptor_header *) &fs_subset_out_desc,
161 NULL,
162};
163
164/* high speed support: */
165
166static struct usb_endpoint_descriptor hs_subset_in_desc = {
167 .bLength = USB_DT_ENDPOINT_SIZE,
168 .bDescriptorType = USB_DT_ENDPOINT,
169
170 .bmAttributes = USB_ENDPOINT_XFER_BULK,
171 .wMaxPacketSize = cpu_to_le16(512),
172};
173
174static struct usb_endpoint_descriptor hs_subset_out_desc = {
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177
178 .bmAttributes = USB_ENDPOINT_XFER_BULK,
179 .wMaxPacketSize = cpu_to_le16(512),
180};
181
182static struct usb_descriptor_header *hs_eth_function[] = {
183 (struct usb_descriptor_header *) &subset_data_intf,
184 (struct usb_descriptor_header *) &mdlm_header_desc,
185 (struct usb_descriptor_header *) &mdlm_desc,
186 (struct usb_descriptor_header *) &mdlm_detail_desc,
187 (struct usb_descriptor_header *) ðer_desc,
188 (struct usb_descriptor_header *) &hs_subset_in_desc,
189 (struct usb_descriptor_header *) &hs_subset_out_desc,
190 NULL,
191};
192
193/* super speed support: */
194
195static struct usb_endpoint_descriptor ss_subset_in_desc = {
196 .bLength = USB_DT_ENDPOINT_SIZE,
197 .bDescriptorType = USB_DT_ENDPOINT,
198
199 .bmAttributes = USB_ENDPOINT_XFER_BULK,
200 .wMaxPacketSize = cpu_to_le16(1024),
201};
202
203static struct usb_endpoint_descriptor ss_subset_out_desc = {
204 .bLength = USB_DT_ENDPOINT_SIZE,
205 .bDescriptorType = USB_DT_ENDPOINT,
206
207 .bmAttributes = USB_ENDPOINT_XFER_BULK,
208 .wMaxPacketSize = cpu_to_le16(1024),
209};
210
211static struct usb_ss_ep_comp_descriptor ss_subset_bulk_comp_desc = {
212 .bLength = sizeof ss_subset_bulk_comp_desc,
213 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
214
215 /* the following 2 values can be tweaked if necessary */
216 /* .bMaxBurst = 0, */
217 /* .bmAttributes = 0, */
218};
219
220static struct usb_descriptor_header *ss_eth_function[] = {
221 (struct usb_descriptor_header *) &subset_data_intf,
222 (struct usb_descriptor_header *) &mdlm_header_desc,
223 (struct usb_descriptor_header *) &mdlm_desc,
224 (struct usb_descriptor_header *) &mdlm_detail_desc,
225 (struct usb_descriptor_header *) ðer_desc,
226 (struct usb_descriptor_header *) &ss_subset_in_desc,
227 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
228 (struct usb_descriptor_header *) &ss_subset_out_desc,
229 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
230 NULL,
231};
232
233/* string descriptors: */
234
235static struct usb_string geth_string_defs[] = {
236 [0].s = "CDC Ethernet Subset/SAFE",
237 [1].s = "",
238 { } /* end of list */
239};
240
241static struct usb_gadget_strings geth_string_table = {
242 .language = 0x0409, /* en-us */
243 .strings = geth_string_defs,
244};
245
246static struct usb_gadget_strings *geth_strings[] = {
247 &geth_string_table,
248 NULL,
249};
250
251/*-------------------------------------------------------------------------*/
252
253static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
254{
255 struct f_gether *geth = func_to_geth(f);
256 struct usb_composite_dev *cdev = f->config->cdev;
257 struct net_device *net;
258
259 /* we know alt == 0, so this is an activation or a reset */
260
261 if (geth->port.in_ep->enabled) {
262 DBG(cdev, "reset cdc subset\n");
263 gether_disconnect(&geth->port);
264 }
265
266 DBG(cdev, "init + activate cdc subset\n");
267 if (config_ep_by_speed(cdev->gadget, f, geth->port.in_ep) ||
268 config_ep_by_speed(cdev->gadget, f, geth->port.out_ep)) {
269 geth->port.in_ep->desc = NULL;
270 geth->port.out_ep->desc = NULL;
271 return -EINVAL;
272 }
273
274 net = gether_connect(&geth->port);
275 return PTR_ERR_OR_ZERO(net);
276}
277
278static void geth_disable(struct usb_function *f)
279{
280 struct f_gether *geth = func_to_geth(f);
281 struct usb_composite_dev *cdev = f->config->cdev;
282
283 DBG(cdev, "net deactivated\n");
284 gether_disconnect(&geth->port);
285}
286
287/*-------------------------------------------------------------------------*/
288
289/* serial function driver setup/binding */
290
291static int
292geth_bind(struct usb_configuration *c, struct usb_function *f)
293{
294 struct usb_composite_dev *cdev = c->cdev;
295 struct f_gether *geth = func_to_geth(f);
296 struct usb_string *us;
297 int status;
298 struct usb_ep *ep;
299
300 struct f_gether_opts *gether_opts;
301
302 gether_opts = container_of(f->fi, struct f_gether_opts, func_inst);
303
304 /*
305 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
306 * configurations are bound in sequence with list_for_each_entry,
307 * in each configuration its functions are bound in sequence
308 * with list_for_each_entry, so we assume no race condition
309 * with regard to gether_opts->bound access
310 */
311 if (!gether_opts->bound) {
312 mutex_lock(&gether_opts->lock);
313 gether_set_gadget(gether_opts->net, cdev->gadget);
314 status = gether_register_netdev(gether_opts->net);
315 mutex_unlock(&gether_opts->lock);
316 if (status)
317 return status;
318 gether_opts->bound = true;
319 }
320
321 us = usb_gstrings_attach(cdev, geth_strings,
322 ARRAY_SIZE(geth_string_defs));
323 if (IS_ERR(us))
324 return PTR_ERR(us);
325
326 subset_data_intf.iInterface = us[0].id;
327 ether_desc.iMACAddress = us[1].id;
328
329 /* allocate instance-specific interface IDs */
330 status = usb_interface_id(c, f);
331 if (status < 0)
332 goto fail;
333 subset_data_intf.bInterfaceNumber = status;
334
335 status = -ENODEV;
336
337 /* allocate instance-specific endpoints */
338 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc);
339 if (!ep)
340 goto fail;
341 geth->port.in_ep = ep;
342
343 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc);
344 if (!ep)
345 goto fail;
346 geth->port.out_ep = ep;
347
348 /* support all relevant hardware speeds... we expect that when
349 * hardware is dual speed, all bulk-capable endpoints work at
350 * both speeds
351 */
352 hs_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
353 hs_subset_out_desc.bEndpointAddress =
354 fs_subset_out_desc.bEndpointAddress;
355
356 ss_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
357 ss_subset_out_desc.bEndpointAddress =
358 fs_subset_out_desc.bEndpointAddress;
359
360 status = usb_assign_descriptors(f, fs_eth_function, hs_eth_function,
361 ss_eth_function, ss_eth_function);
362 if (status)
363 goto fail;
364
365 /* NOTE: all that is done without knowing or caring about
366 * the network link ... which is unavailable to this code
367 * until we're activated via set_alt().
368 */
369
370 DBG(cdev, "CDC Subset: IN/%s OUT/%s\n",
371 geth->port.in_ep->name, geth->port.out_ep->name);
372 return 0;
373
374fail:
375 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
376
377 return status;
378}
379
380static inline struct f_gether_opts *to_f_gether_opts(struct config_item *item)
381{
382 return container_of(to_config_group(item), struct f_gether_opts,
383 func_inst.group);
384}
385
386/* f_gether_item_ops */
387USB_ETHERNET_CONFIGFS_ITEM(gether);
388
389/* f_gether_opts_dev_addr */
390USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(gether);
391
392/* f_gether_opts_host_addr */
393USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(gether);
394
395/* f_gether_opts_qmult */
396USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(gether);
397
398/* f_gether_opts_ifname */
399USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(gether);
400
401static struct configfs_attribute *gether_attrs[] = {
402 &gether_opts_attr_dev_addr,
403 &gether_opts_attr_host_addr,
404 &gether_opts_attr_qmult,
405 &gether_opts_attr_ifname,
406 NULL,
407};
408
409static const struct config_item_type gether_func_type = {
410 .ct_item_ops = &gether_item_ops,
411 .ct_attrs = gether_attrs,
412 .ct_owner = THIS_MODULE,
413};
414
415static void geth_free_inst(struct usb_function_instance *f)
416{
417 struct f_gether_opts *opts;
418
419 opts = container_of(f, struct f_gether_opts, func_inst);
420 if (opts->bound)
421 gether_cleanup(netdev_priv(opts->net));
422 else
423 free_netdev(opts->net);
424 kfree(opts);
425}
426
427static struct usb_function_instance *geth_alloc_inst(void)
428{
429 struct f_gether_opts *opts;
430
431 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
432 if (!opts)
433 return ERR_PTR(-ENOMEM);
434 mutex_init(&opts->lock);
435 opts->func_inst.free_func_inst = geth_free_inst;
436 opts->net = gether_setup_default();
437 if (IS_ERR(opts->net)) {
438 struct net_device *net = opts->net;
439 kfree(opts);
440 return ERR_CAST(net);
441 }
442
443 config_group_init_type_name(&opts->func_inst.group, "",
444 &gether_func_type);
445
446 return &opts->func_inst;
447}
448
449static void geth_free(struct usb_function *f)
450{
451 struct f_gether *eth;
452
453 eth = func_to_geth(f);
454 kfree(eth);
455}
456
457static void geth_unbind(struct usb_configuration *c, struct usb_function *f)
458{
459 geth_string_defs[0].id = 0;
460 usb_free_all_descriptors(f);
461}
462
463static struct usb_function *geth_alloc(struct usb_function_instance *fi)
464{
465 struct f_gether *geth;
466 struct f_gether_opts *opts;
467 int status;
468
469 /* allocate and initialize one new instance */
470 geth = kzalloc(sizeof(*geth), GFP_KERNEL);
471 if (!geth)
472 return ERR_PTR(-ENOMEM);
473
474 opts = container_of(fi, struct f_gether_opts, func_inst);
475
476 mutex_lock(&opts->lock);
477 opts->refcnt++;
478 /* export host's Ethernet address in CDC format */
479 status = gether_get_host_addr_cdc(opts->net, geth->ethaddr,
480 sizeof(geth->ethaddr));
481 if (status < 12) {
482 kfree(geth);
483 mutex_unlock(&opts->lock);
484 return ERR_PTR(-EINVAL);
485 }
486 geth_string_defs[1].s = geth->ethaddr;
487
488 geth->port.ioport = netdev_priv(opts->net);
489 mutex_unlock(&opts->lock);
490 geth->port.cdc_filter = DEFAULT_FILTER;
491
492 geth->port.func.name = "cdc_subset";
493 geth->port.func.bind = geth_bind;
494 geth->port.func.unbind = geth_unbind;
495 geth->port.func.set_alt = geth_set_alt;
496 geth->port.func.disable = geth_disable;
497 geth->port.func.free_func = geth_free;
498
499 return &geth->port.func;
500}
501
502DECLARE_USB_FUNCTION_INIT(geth, geth_alloc_inst, geth_alloc);
503MODULE_LICENSE("GPL");
504MODULE_AUTHOR("David Brownell");
1/*
2 * f_subset.c -- "CDC Subset" Ethernet link function driver
3 *
4 * Copyright (C) 2003-2005,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.
11 */
12
13#include <linux/slab.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/device.h>
17#include <linux/etherdevice.h>
18
19#include "u_ether.h"
20#include "u_ether_configfs.h"
21#include "u_gether.h"
22
23/*
24 * This function packages a simple "CDC Subset" Ethernet port with no real
25 * control mechanisms; just raw data transfer over two bulk endpoints.
26 * The data transfer model is exactly that of CDC Ethernet, which is
27 * why we call it the "CDC Subset".
28 *
29 * Because it's not standardized, this has some interoperability issues.
30 * They mostly relate to driver binding, since the data transfer model is
31 * so simple (CDC Ethernet). The original versions of this protocol used
32 * specific product/vendor IDs: byteswapped IDs for Digital Equipment's
33 * SA-1100 "Itsy" board, which could run Linux 2.4 kernels and supported
34 * daughtercards with USB peripheral connectors. (It was used more often
35 * with other boards, using the Itsy identifiers.) Linux hosts recognized
36 * this with CONFIG_USB_ARMLINUX; these devices have only one configuration
37 * and one interface.
38 *
39 * At some point, MCCI defined a (nonconformant) CDC MDLM variant called
40 * "SAFE", which happens to have a mode which is identical to the "CDC
41 * Subset" in terms of data transfer and lack of control model. This was
42 * adopted by later Sharp Zaurus models, and by some other software which
43 * Linux hosts recognize with CONFIG_USB_NET_ZAURUS.
44 *
45 * Because Microsoft's RNDIS drivers are far from robust, we added a few
46 * descriptors to the CDC Subset code, making this code look like a SAFE
47 * implementation. This lets you use MCCI's host side MS-Windows drivers
48 * if you get fed up with RNDIS. It also makes it easier for composite
49 * drivers to work, since they can use class based binding instead of
50 * caring about specific product and vendor IDs.
51 */
52
53struct f_gether {
54 struct gether port;
55
56 char ethaddr[14];
57};
58
59static inline struct f_gether *func_to_geth(struct usb_function *f)
60{
61 return container_of(f, struct f_gether, port.func);
62}
63
64/*-------------------------------------------------------------------------*/
65
66/*
67 * "Simple" CDC-subset option is a simple vendor-neutral model that most
68 * full speed controllers can handle: one interface, two bulk endpoints.
69 * To assist host side drivers, we fancy it up a bit, and add descriptors so
70 * some host side drivers will understand it as a "SAFE" variant.
71 *
72 * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various ways.
73 * Data endpoints live in the control interface, there's no data interface.
74 * And it's not used to talk to a cell phone radio.
75 */
76
77/* interface descriptor: */
78
79static struct usb_interface_descriptor subset_data_intf = {
80 .bLength = sizeof subset_data_intf,
81 .bDescriptorType = USB_DT_INTERFACE,
82
83 /* .bInterfaceNumber = DYNAMIC */
84 .bAlternateSetting = 0,
85 .bNumEndpoints = 2,
86 .bInterfaceClass = USB_CLASS_COMM,
87 .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
88 .bInterfaceProtocol = 0,
89 /* .iInterface = DYNAMIC */
90};
91
92static struct usb_cdc_header_desc mdlm_header_desc = {
93 .bLength = sizeof mdlm_header_desc,
94 .bDescriptorType = USB_DT_CS_INTERFACE,
95 .bDescriptorSubType = USB_CDC_HEADER_TYPE,
96
97 .bcdCDC = cpu_to_le16(0x0110),
98};
99
100static struct usb_cdc_mdlm_desc mdlm_desc = {
101 .bLength = sizeof mdlm_desc,
102 .bDescriptorType = USB_DT_CS_INTERFACE,
103 .bDescriptorSubType = USB_CDC_MDLM_TYPE,
104
105 .bcdVersion = cpu_to_le16(0x0100),
106 .bGUID = {
107 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
108 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
109 },
110};
111
112/* since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
113 * can't really use its struct. All we do here is say that we're using
114 * the submode of "SAFE" which directly matches the CDC Subset.
115 */
116static u8 mdlm_detail_desc[] = {
117 6,
118 USB_DT_CS_INTERFACE,
119 USB_CDC_MDLM_DETAIL_TYPE,
120
121 0, /* "SAFE" */
122 0, /* network control capabilities (none) */
123 0, /* network data capabilities ("raw" encapsulation) */
124};
125
126static struct usb_cdc_ether_desc ether_desc = {
127 .bLength = sizeof ether_desc,
128 .bDescriptorType = USB_DT_CS_INTERFACE,
129 .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
130
131 /* this descriptor actually adds value, surprise! */
132 /* .iMACAddress = DYNAMIC */
133 .bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
134 .wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
135 .wNumberMCFilters = cpu_to_le16(0),
136 .bNumberPowerFilters = 0,
137};
138
139/* full speed support: */
140
141static struct usb_endpoint_descriptor fs_subset_in_desc = {
142 .bLength = USB_DT_ENDPOINT_SIZE,
143 .bDescriptorType = USB_DT_ENDPOINT,
144
145 .bEndpointAddress = USB_DIR_IN,
146 .bmAttributes = USB_ENDPOINT_XFER_BULK,
147};
148
149static struct usb_endpoint_descriptor fs_subset_out_desc = {
150 .bLength = USB_DT_ENDPOINT_SIZE,
151 .bDescriptorType = USB_DT_ENDPOINT,
152
153 .bEndpointAddress = USB_DIR_OUT,
154 .bmAttributes = USB_ENDPOINT_XFER_BULK,
155};
156
157static struct usb_descriptor_header *fs_eth_function[] = {
158 (struct usb_descriptor_header *) &subset_data_intf,
159 (struct usb_descriptor_header *) &mdlm_header_desc,
160 (struct usb_descriptor_header *) &mdlm_desc,
161 (struct usb_descriptor_header *) &mdlm_detail_desc,
162 (struct usb_descriptor_header *) ðer_desc,
163 (struct usb_descriptor_header *) &fs_subset_in_desc,
164 (struct usb_descriptor_header *) &fs_subset_out_desc,
165 NULL,
166};
167
168/* high speed support: */
169
170static struct usb_endpoint_descriptor hs_subset_in_desc = {
171 .bLength = USB_DT_ENDPOINT_SIZE,
172 .bDescriptorType = USB_DT_ENDPOINT,
173
174 .bmAttributes = USB_ENDPOINT_XFER_BULK,
175 .wMaxPacketSize = cpu_to_le16(512),
176};
177
178static struct usb_endpoint_descriptor hs_subset_out_desc = {
179 .bLength = USB_DT_ENDPOINT_SIZE,
180 .bDescriptorType = USB_DT_ENDPOINT,
181
182 .bmAttributes = USB_ENDPOINT_XFER_BULK,
183 .wMaxPacketSize = cpu_to_le16(512),
184};
185
186static struct usb_descriptor_header *hs_eth_function[] = {
187 (struct usb_descriptor_header *) &subset_data_intf,
188 (struct usb_descriptor_header *) &mdlm_header_desc,
189 (struct usb_descriptor_header *) &mdlm_desc,
190 (struct usb_descriptor_header *) &mdlm_detail_desc,
191 (struct usb_descriptor_header *) ðer_desc,
192 (struct usb_descriptor_header *) &hs_subset_in_desc,
193 (struct usb_descriptor_header *) &hs_subset_out_desc,
194 NULL,
195};
196
197/* super speed support: */
198
199static struct usb_endpoint_descriptor ss_subset_in_desc = {
200 .bLength = USB_DT_ENDPOINT_SIZE,
201 .bDescriptorType = USB_DT_ENDPOINT,
202
203 .bmAttributes = USB_ENDPOINT_XFER_BULK,
204 .wMaxPacketSize = cpu_to_le16(1024),
205};
206
207static struct usb_endpoint_descriptor ss_subset_out_desc = {
208 .bLength = USB_DT_ENDPOINT_SIZE,
209 .bDescriptorType = USB_DT_ENDPOINT,
210
211 .bmAttributes = USB_ENDPOINT_XFER_BULK,
212 .wMaxPacketSize = cpu_to_le16(1024),
213};
214
215static struct usb_ss_ep_comp_descriptor ss_subset_bulk_comp_desc = {
216 .bLength = sizeof ss_subset_bulk_comp_desc,
217 .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
218
219 /* the following 2 values can be tweaked if necessary */
220 /* .bMaxBurst = 0, */
221 /* .bmAttributes = 0, */
222};
223
224static struct usb_descriptor_header *ss_eth_function[] = {
225 (struct usb_descriptor_header *) &subset_data_intf,
226 (struct usb_descriptor_header *) &mdlm_header_desc,
227 (struct usb_descriptor_header *) &mdlm_desc,
228 (struct usb_descriptor_header *) &mdlm_detail_desc,
229 (struct usb_descriptor_header *) ðer_desc,
230 (struct usb_descriptor_header *) &ss_subset_in_desc,
231 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
232 (struct usb_descriptor_header *) &ss_subset_out_desc,
233 (struct usb_descriptor_header *) &ss_subset_bulk_comp_desc,
234 NULL,
235};
236
237/* string descriptors: */
238
239static struct usb_string geth_string_defs[] = {
240 [0].s = "CDC Ethernet Subset/SAFE",
241 [1].s = "",
242 { } /* end of list */
243};
244
245static struct usb_gadget_strings geth_string_table = {
246 .language = 0x0409, /* en-us */
247 .strings = geth_string_defs,
248};
249
250static struct usb_gadget_strings *geth_strings[] = {
251 &geth_string_table,
252 NULL,
253};
254
255/*-------------------------------------------------------------------------*/
256
257static int geth_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
258{
259 struct f_gether *geth = func_to_geth(f);
260 struct usb_composite_dev *cdev = f->config->cdev;
261 struct net_device *net;
262
263 /* we know alt == 0, so this is an activation or a reset */
264
265 if (geth->port.in_ep->enabled) {
266 DBG(cdev, "reset cdc subset\n");
267 gether_disconnect(&geth->port);
268 }
269
270 DBG(cdev, "init + activate cdc subset\n");
271 if (config_ep_by_speed(cdev->gadget, f, geth->port.in_ep) ||
272 config_ep_by_speed(cdev->gadget, f, geth->port.out_ep)) {
273 geth->port.in_ep->desc = NULL;
274 geth->port.out_ep->desc = NULL;
275 return -EINVAL;
276 }
277
278 net = gether_connect(&geth->port);
279 return PTR_ERR_OR_ZERO(net);
280}
281
282static void geth_disable(struct usb_function *f)
283{
284 struct f_gether *geth = func_to_geth(f);
285 struct usb_composite_dev *cdev = f->config->cdev;
286
287 DBG(cdev, "net deactivated\n");
288 gether_disconnect(&geth->port);
289}
290
291/*-------------------------------------------------------------------------*/
292
293/* serial function driver setup/binding */
294
295static int
296geth_bind(struct usb_configuration *c, struct usb_function *f)
297{
298 struct usb_composite_dev *cdev = c->cdev;
299 struct f_gether *geth = func_to_geth(f);
300 struct usb_string *us;
301 int status;
302 struct usb_ep *ep;
303
304 struct f_gether_opts *gether_opts;
305
306 gether_opts = container_of(f->fi, struct f_gether_opts, func_inst);
307
308 /*
309 * in drivers/usb/gadget/configfs.c:configfs_composite_bind()
310 * configurations are bound in sequence with list_for_each_entry,
311 * in each configuration its functions are bound in sequence
312 * with list_for_each_entry, so we assume no race condition
313 * with regard to gether_opts->bound access
314 */
315 if (!gether_opts->bound) {
316 mutex_lock(&gether_opts->lock);
317 gether_set_gadget(gether_opts->net, cdev->gadget);
318 status = gether_register_netdev(gether_opts->net);
319 mutex_unlock(&gether_opts->lock);
320 if (status)
321 return status;
322 gether_opts->bound = true;
323 }
324
325 us = usb_gstrings_attach(cdev, geth_strings,
326 ARRAY_SIZE(geth_string_defs));
327 if (IS_ERR(us))
328 return PTR_ERR(us);
329
330 subset_data_intf.iInterface = us[0].id;
331 ether_desc.iMACAddress = us[1].id;
332
333 /* allocate instance-specific interface IDs */
334 status = usb_interface_id(c, f);
335 if (status < 0)
336 goto fail;
337 subset_data_intf.bInterfaceNumber = status;
338
339 status = -ENODEV;
340
341 /* allocate instance-specific endpoints */
342 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_in_desc);
343 if (!ep)
344 goto fail;
345 geth->port.in_ep = ep;
346
347 ep = usb_ep_autoconfig(cdev->gadget, &fs_subset_out_desc);
348 if (!ep)
349 goto fail;
350 geth->port.out_ep = ep;
351
352 /* support all relevant hardware speeds... we expect that when
353 * hardware is dual speed, all bulk-capable endpoints work at
354 * both speeds
355 */
356 hs_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
357 hs_subset_out_desc.bEndpointAddress =
358 fs_subset_out_desc.bEndpointAddress;
359
360 ss_subset_in_desc.bEndpointAddress = fs_subset_in_desc.bEndpointAddress;
361 ss_subset_out_desc.bEndpointAddress =
362 fs_subset_out_desc.bEndpointAddress;
363
364 status = usb_assign_descriptors(f, fs_eth_function, hs_eth_function,
365 ss_eth_function, NULL);
366 if (status)
367 goto fail;
368
369 /* NOTE: all that is done without knowing or caring about
370 * the network link ... which is unavailable to this code
371 * until we're activated via set_alt().
372 */
373
374 DBG(cdev, "CDC Subset: %s speed IN/%s OUT/%s\n",
375 gadget_is_superspeed(c->cdev->gadget) ? "super" :
376 gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
377 geth->port.in_ep->name, geth->port.out_ep->name);
378 return 0;
379
380fail:
381 ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
382
383 return status;
384}
385
386static inline struct f_gether_opts *to_f_gether_opts(struct config_item *item)
387{
388 return container_of(to_config_group(item), struct f_gether_opts,
389 func_inst.group);
390}
391
392/* f_gether_item_ops */
393USB_ETHERNET_CONFIGFS_ITEM(gether);
394
395/* f_gether_opts_dev_addr */
396USB_ETHERNET_CONFIGFS_ITEM_ATTR_DEV_ADDR(gether);
397
398/* f_gether_opts_host_addr */
399USB_ETHERNET_CONFIGFS_ITEM_ATTR_HOST_ADDR(gether);
400
401/* f_gether_opts_qmult */
402USB_ETHERNET_CONFIGFS_ITEM_ATTR_QMULT(gether);
403
404/* f_gether_opts_ifname */
405USB_ETHERNET_CONFIGFS_ITEM_ATTR_IFNAME(gether);
406
407static struct configfs_attribute *gether_attrs[] = {
408 &gether_opts_attr_dev_addr,
409 &gether_opts_attr_host_addr,
410 &gether_opts_attr_qmult,
411 &gether_opts_attr_ifname,
412 NULL,
413};
414
415static struct config_item_type gether_func_type = {
416 .ct_item_ops = &gether_item_ops,
417 .ct_attrs = gether_attrs,
418 .ct_owner = THIS_MODULE,
419};
420
421static void geth_free_inst(struct usb_function_instance *f)
422{
423 struct f_gether_opts *opts;
424
425 opts = container_of(f, struct f_gether_opts, func_inst);
426 if (opts->bound)
427 gether_cleanup(netdev_priv(opts->net));
428 else
429 free_netdev(opts->net);
430 kfree(opts);
431}
432
433static struct usb_function_instance *geth_alloc_inst(void)
434{
435 struct f_gether_opts *opts;
436
437 opts = kzalloc(sizeof(*opts), GFP_KERNEL);
438 if (!opts)
439 return ERR_PTR(-ENOMEM);
440 mutex_init(&opts->lock);
441 opts->func_inst.free_func_inst = geth_free_inst;
442 opts->net = gether_setup_default();
443 if (IS_ERR(opts->net)) {
444 struct net_device *net = opts->net;
445 kfree(opts);
446 return ERR_CAST(net);
447 }
448
449 config_group_init_type_name(&opts->func_inst.group, "",
450 &gether_func_type);
451
452 return &opts->func_inst;
453}
454
455static void geth_free(struct usb_function *f)
456{
457 struct f_gether *eth;
458
459 eth = func_to_geth(f);
460 kfree(eth);
461}
462
463static void geth_unbind(struct usb_configuration *c, struct usb_function *f)
464{
465 geth_string_defs[0].id = 0;
466 usb_free_all_descriptors(f);
467}
468
469static struct usb_function *geth_alloc(struct usb_function_instance *fi)
470{
471 struct f_gether *geth;
472 struct f_gether_opts *opts;
473 int status;
474
475 /* allocate and initialize one new instance */
476 geth = kzalloc(sizeof(*geth), GFP_KERNEL);
477 if (!geth)
478 return ERR_PTR(-ENOMEM);
479
480 opts = container_of(fi, struct f_gether_opts, func_inst);
481
482 mutex_lock(&opts->lock);
483 opts->refcnt++;
484 /* export host's Ethernet address in CDC format */
485 status = gether_get_host_addr_cdc(opts->net, geth->ethaddr,
486 sizeof(geth->ethaddr));
487 if (status < 12) {
488 kfree(geth);
489 mutex_unlock(&opts->lock);
490 return ERR_PTR(-EINVAL);
491 }
492 geth_string_defs[1].s = geth->ethaddr;
493
494 geth->port.ioport = netdev_priv(opts->net);
495 mutex_unlock(&opts->lock);
496 geth->port.cdc_filter = DEFAULT_FILTER;
497
498 geth->port.func.name = "cdc_subset";
499 geth->port.func.bind = geth_bind;
500 geth->port.func.unbind = geth_unbind;
501 geth->port.func.set_alt = geth_set_alt;
502 geth->port.func.disable = geth_disable;
503 geth->port.func.free_func = geth_free;
504
505 return &geth->port.func;
506}
507
508DECLARE_USB_FUNCTION_INIT(geth, geth_alloc_inst, geth_alloc);
509MODULE_LICENSE("GPL");
510MODULE_AUTHOR("David Brownell");