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