18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ncm.c -- NCM gadget driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2010 Nokia Corporation
68c2ecf20Sopenharmony_ci * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * The driver borrows from ether.c which is:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Copyright (C) 2003-2005,2008 David Brownell
118c2ecf20Sopenharmony_ci * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
128c2ecf20Sopenharmony_ci * Copyright (C) 2008 Nokia Corporation
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/* #define DEBUG */
168c2ecf20Sopenharmony_ci/* #define VERBOSE_DEBUG */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/kernel.h>
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/usb/composite.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "u_ether.h"
238c2ecf20Sopenharmony_ci#include "u_ncm.h"
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define DRIVER_DESC		"NCM Gadget"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
308c2ecf20Sopenharmony_ci * Instead:  allocate your own, using normal USB-IF procedures.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Thanks to NetChip Technologies for donating this product ID.
348c2ecf20Sopenharmony_ci * It's for devices with only CDC Ethernet configurations.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci#define CDC_VENDOR_NUM		0x0525	/* NetChip */
378c2ecf20Sopenharmony_ci#define CDC_PRODUCT_NUM		0xa4a1	/* Linux-USB Ethernet Gadget */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
408c2ecf20Sopenharmony_ciUSB_GADGET_COMPOSITE_OPTIONS();
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ciUSB_ETHERNET_MODULE_PARAMETERS();
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic struct usb_device_descriptor device_desc = {
458c2ecf20Sopenharmony_ci	.bLength =		sizeof device_desc,
468c2ecf20Sopenharmony_ci	.bDescriptorType =	USB_DT_DEVICE,
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	/* .bcdUSB = DYNAMIC */
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	.bDeviceClass =		USB_CLASS_COMM,
518c2ecf20Sopenharmony_ci	.bDeviceSubClass =	0,
528c2ecf20Sopenharmony_ci	.bDeviceProtocol =	0,
538c2ecf20Sopenharmony_ci	/* .bMaxPacketSize0 = f(hardware) */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* Vendor and product id defaults change according to what configs
568c2ecf20Sopenharmony_ci	 * we support.  (As does bNumConfigurations.)  These values can
578c2ecf20Sopenharmony_ci	 * also be overridden by module parameters.
588c2ecf20Sopenharmony_ci	 */
598c2ecf20Sopenharmony_ci	.idVendor =		cpu_to_le16 (CDC_VENDOR_NUM),
608c2ecf20Sopenharmony_ci	.idProduct =		cpu_to_le16 (CDC_PRODUCT_NUM),
618c2ecf20Sopenharmony_ci	/* .bcdDevice = f(hardware) */
628c2ecf20Sopenharmony_ci	/* .iManufacturer = DYNAMIC */
638c2ecf20Sopenharmony_ci	/* .iProduct = DYNAMIC */
648c2ecf20Sopenharmony_ci	/* NO SERIAL NUMBER */
658c2ecf20Sopenharmony_ci	.bNumConfigurations =	1,
668c2ecf20Sopenharmony_ci};
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic const struct usb_descriptor_header *otg_desc[2];
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/* string IDs are assigned dynamically */
718c2ecf20Sopenharmony_cistatic struct usb_string strings_dev[] = {
728c2ecf20Sopenharmony_ci	[USB_GADGET_MANUFACTURER_IDX].s = "",
738c2ecf20Sopenharmony_ci	[USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
748c2ecf20Sopenharmony_ci	[USB_GADGET_SERIAL_IDX].s = "",
758c2ecf20Sopenharmony_ci	{  } /* end of list */
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cistatic struct usb_gadget_strings stringtab_dev = {
798c2ecf20Sopenharmony_ci	.language	= 0x0409,	/* en-us */
808c2ecf20Sopenharmony_ci	.strings	= strings_dev,
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic struct usb_gadget_strings *dev_strings[] = {
848c2ecf20Sopenharmony_ci	&stringtab_dev,
858c2ecf20Sopenharmony_ci	NULL,
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic struct usb_function_instance *f_ncm_inst;
898c2ecf20Sopenharmony_cistatic struct usb_function *f_ncm;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic int ncm_do_config(struct usb_configuration *c)
948c2ecf20Sopenharmony_ci{
958c2ecf20Sopenharmony_ci	int status;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* FIXME alloc iConfiguration string, set it in c->strings */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	if (gadget_is_otg(c->cdev->gadget)) {
1008c2ecf20Sopenharmony_ci		c->descriptors = otg_desc;
1018c2ecf20Sopenharmony_ci		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	f_ncm = usb_get_function(f_ncm_inst);
1058c2ecf20Sopenharmony_ci	if (IS_ERR(f_ncm))
1068c2ecf20Sopenharmony_ci		return PTR_ERR(f_ncm);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	status = usb_add_function(c, f_ncm);
1098c2ecf20Sopenharmony_ci	if (status < 0) {
1108c2ecf20Sopenharmony_ci		usb_put_function(f_ncm);
1118c2ecf20Sopenharmony_ci		return status;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return 0;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic struct usb_configuration ncm_config_driver = {
1188c2ecf20Sopenharmony_ci	/* .label = f(hardware) */
1198c2ecf20Sopenharmony_ci	.label			= "CDC Ethernet (NCM)",
1208c2ecf20Sopenharmony_ci	.bConfigurationValue	= 1,
1218c2ecf20Sopenharmony_ci	/* .iConfiguration = DYNAMIC */
1228c2ecf20Sopenharmony_ci	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic int gncm_bind(struct usb_composite_dev *cdev)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct usb_gadget	*gadget = cdev->gadget;
1308c2ecf20Sopenharmony_ci	struct f_ncm_opts	*ncm_opts;
1318c2ecf20Sopenharmony_ci	int			status;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	f_ncm_inst = usb_get_function_instance("ncm");
1348c2ecf20Sopenharmony_ci	if (IS_ERR(f_ncm_inst))
1358c2ecf20Sopenharmony_ci		return PTR_ERR(f_ncm_inst);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	ncm_opts = container_of(f_ncm_inst, struct f_ncm_opts, func_inst);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	gether_set_qmult(ncm_opts->net, qmult);
1408c2ecf20Sopenharmony_ci	if (!gether_set_host_addr(ncm_opts->net, host_addr))
1418c2ecf20Sopenharmony_ci		pr_info("using host ethernet address: %s", host_addr);
1428c2ecf20Sopenharmony_ci	if (!gether_set_dev_addr(ncm_opts->net, dev_addr))
1438c2ecf20Sopenharmony_ci		pr_info("using self ethernet address: %s", dev_addr);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* Allocate string descriptor numbers ... note that string
1468c2ecf20Sopenharmony_ci	 * contents can be overridden by the composite_dev glue.
1478c2ecf20Sopenharmony_ci	 */
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	status = usb_string_ids_tab(cdev, strings_dev);
1508c2ecf20Sopenharmony_ci	if (status < 0)
1518c2ecf20Sopenharmony_ci		goto fail;
1528c2ecf20Sopenharmony_ci	device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
1538c2ecf20Sopenharmony_ci	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	if (gadget_is_otg(gadget) && !otg_desc[0]) {
1568c2ecf20Sopenharmony_ci		struct usb_descriptor_header *usb_desc;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci		usb_desc = usb_otg_descriptor_alloc(gadget);
1598c2ecf20Sopenharmony_ci		if (!usb_desc) {
1608c2ecf20Sopenharmony_ci			status = -ENOMEM;
1618c2ecf20Sopenharmony_ci			goto fail;
1628c2ecf20Sopenharmony_ci		}
1638c2ecf20Sopenharmony_ci		usb_otg_descriptor_init(gadget, usb_desc);
1648c2ecf20Sopenharmony_ci		otg_desc[0] = usb_desc;
1658c2ecf20Sopenharmony_ci		otg_desc[1] = NULL;
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	status = usb_add_config(cdev, &ncm_config_driver,
1698c2ecf20Sopenharmony_ci				ncm_do_config);
1708c2ecf20Sopenharmony_ci	if (status < 0)
1718c2ecf20Sopenharmony_ci		goto fail1;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	usb_composite_overwrite_options(cdev, &coverwrite);
1748c2ecf20Sopenharmony_ci	dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	return 0;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cifail1:
1798c2ecf20Sopenharmony_ci	kfree(otg_desc[0]);
1808c2ecf20Sopenharmony_ci	otg_desc[0] = NULL;
1818c2ecf20Sopenharmony_cifail:
1828c2ecf20Sopenharmony_ci	usb_put_function_instance(f_ncm_inst);
1838c2ecf20Sopenharmony_ci	return status;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int gncm_unbind(struct usb_composite_dev *cdev)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(f_ncm))
1898c2ecf20Sopenharmony_ci		usb_put_function(f_ncm);
1908c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(f_ncm_inst))
1918c2ecf20Sopenharmony_ci		usb_put_function_instance(f_ncm_inst);
1928c2ecf20Sopenharmony_ci	kfree(otg_desc[0]);
1938c2ecf20Sopenharmony_ci	otg_desc[0] = NULL;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	return 0;
1968c2ecf20Sopenharmony_ci}
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_cistatic struct usb_composite_driver ncm_driver = {
1998c2ecf20Sopenharmony_ci	.name		= "g_ncm",
2008c2ecf20Sopenharmony_ci	.dev		= &device_desc,
2018c2ecf20Sopenharmony_ci	.strings	= dev_strings,
2028c2ecf20Sopenharmony_ci	.max_speed	= USB_SPEED_SUPER,
2038c2ecf20Sopenharmony_ci	.bind		= gncm_bind,
2048c2ecf20Sopenharmony_ci	.unbind		= gncm_unbind,
2058c2ecf20Sopenharmony_ci};
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cimodule_usb_composite_driver(ncm_driver);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
2108c2ecf20Sopenharmony_ciMODULE_AUTHOR("Yauheni Kaliuta");
2118c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
212