18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * cdc2.c -- CDC Composite driver, with ECM and ACM support
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2008 David Brownell
68c2ecf20Sopenharmony_ci * Copyright (C) 2008 Nokia Corporation
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include "u_ether.h"
138c2ecf20Sopenharmony_ci#include "u_serial.h"
148c2ecf20Sopenharmony_ci#include "u_ecm.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define DRIVER_DESC		"CDC Composite Gadget"
188c2ecf20Sopenharmony_ci#define DRIVER_VERSION		"King Kamehameha Day 2008"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
238c2ecf20Sopenharmony_ci * Instead:  allocate your own, using normal USB-IF procedures.
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* Thanks to NetChip Technologies for donating this product ID.
278c2ecf20Sopenharmony_ci * It's for devices with only this composite CDC configuration.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci#define CDC_VENDOR_NUM		0x0525	/* NetChip */
308c2ecf20Sopenharmony_ci#define CDC_PRODUCT_NUM		0xa4aa	/* CDC Composite: ECM + ACM */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ciUSB_GADGET_COMPOSITE_OPTIONS();
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciUSB_ETHERNET_MODULE_PARAMETERS();
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic struct usb_device_descriptor device_desc = {
398c2ecf20Sopenharmony_ci	.bLength =		sizeof device_desc,
408c2ecf20Sopenharmony_ci	.bDescriptorType =	USB_DT_DEVICE,
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	/* .bcdUSB = DYNAMIC */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	.bDeviceClass =		USB_CLASS_COMM,
458c2ecf20Sopenharmony_ci	.bDeviceSubClass =	0,
468c2ecf20Sopenharmony_ci	.bDeviceProtocol =	0,
478c2ecf20Sopenharmony_ci	/* .bMaxPacketSize0 = f(hardware) */
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	/* Vendor and product id can be overridden by module parameters.  */
508c2ecf20Sopenharmony_ci	.idVendor =		cpu_to_le16(CDC_VENDOR_NUM),
518c2ecf20Sopenharmony_ci	.idProduct =		cpu_to_le16(CDC_PRODUCT_NUM),
528c2ecf20Sopenharmony_ci	/* .bcdDevice = f(hardware) */
538c2ecf20Sopenharmony_ci	/* .iManufacturer = DYNAMIC */
548c2ecf20Sopenharmony_ci	/* .iProduct = DYNAMIC */
558c2ecf20Sopenharmony_ci	/* NO SERIAL NUMBER */
568c2ecf20Sopenharmony_ci	.bNumConfigurations =	1,
578c2ecf20Sopenharmony_ci};
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_cistatic const struct usb_descriptor_header *otg_desc[2];
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/* string IDs are assigned dynamically */
628c2ecf20Sopenharmony_cistatic struct usb_string strings_dev[] = {
638c2ecf20Sopenharmony_ci	[USB_GADGET_MANUFACTURER_IDX].s = "",
648c2ecf20Sopenharmony_ci	[USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
658c2ecf20Sopenharmony_ci	[USB_GADGET_SERIAL_IDX].s = "",
668c2ecf20Sopenharmony_ci	{  } /* end of list */
678c2ecf20Sopenharmony_ci};
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic struct usb_gadget_strings stringtab_dev = {
708c2ecf20Sopenharmony_ci	.language	= 0x0409,	/* en-us */
718c2ecf20Sopenharmony_ci	.strings	= strings_dev,
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic struct usb_gadget_strings *dev_strings[] = {
758c2ecf20Sopenharmony_ci	&stringtab_dev,
768c2ecf20Sopenharmony_ci	NULL,
778c2ecf20Sopenharmony_ci};
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
808c2ecf20Sopenharmony_cistatic struct usb_function *f_acm;
818c2ecf20Sopenharmony_cistatic struct usb_function_instance *fi_serial;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cistatic struct usb_function *f_ecm;
848c2ecf20Sopenharmony_cistatic struct usb_function_instance *fi_ecm;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci/*
878c2ecf20Sopenharmony_ci * We _always_ have both CDC ECM and CDC ACM functions.
888c2ecf20Sopenharmony_ci */
898c2ecf20Sopenharmony_cistatic int cdc_do_config(struct usb_configuration *c)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	int	status;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	if (gadget_is_otg(c->cdev->gadget)) {
948c2ecf20Sopenharmony_ci		c->descriptors = otg_desc;
958c2ecf20Sopenharmony_ci		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	f_ecm = usb_get_function(fi_ecm);
998c2ecf20Sopenharmony_ci	if (IS_ERR(f_ecm)) {
1008c2ecf20Sopenharmony_ci		status = PTR_ERR(f_ecm);
1018c2ecf20Sopenharmony_ci		goto err_get_ecm;
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	status = usb_add_function(c, f_ecm);
1058c2ecf20Sopenharmony_ci	if (status)
1068c2ecf20Sopenharmony_ci		goto err_add_ecm;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	f_acm = usb_get_function(fi_serial);
1098c2ecf20Sopenharmony_ci	if (IS_ERR(f_acm)) {
1108c2ecf20Sopenharmony_ci		status = PTR_ERR(f_acm);
1118c2ecf20Sopenharmony_ci		goto err_get_acm;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	status = usb_add_function(c, f_acm);
1158c2ecf20Sopenharmony_ci	if (status)
1168c2ecf20Sopenharmony_ci		goto err_add_acm;
1178c2ecf20Sopenharmony_ci	return 0;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cierr_add_acm:
1208c2ecf20Sopenharmony_ci	usb_put_function(f_acm);
1218c2ecf20Sopenharmony_cierr_get_acm:
1228c2ecf20Sopenharmony_ci	usb_remove_function(c, f_ecm);
1238c2ecf20Sopenharmony_cierr_add_ecm:
1248c2ecf20Sopenharmony_ci	usb_put_function(f_ecm);
1258c2ecf20Sopenharmony_cierr_get_ecm:
1268c2ecf20Sopenharmony_ci	return status;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic struct usb_configuration cdc_config_driver = {
1308c2ecf20Sopenharmony_ci	.label			= "CDC Composite (ECM + ACM)",
1318c2ecf20Sopenharmony_ci	.bConfigurationValue	= 1,
1328c2ecf20Sopenharmony_ci	/* .iConfiguration = DYNAMIC */
1338c2ecf20Sopenharmony_ci	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/*-------------------------------------------------------------------------*/
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int cdc_bind(struct usb_composite_dev *cdev)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	struct usb_gadget	*gadget = cdev->gadget;
1418c2ecf20Sopenharmony_ci	struct f_ecm_opts	*ecm_opts;
1428c2ecf20Sopenharmony_ci	int			status;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	if (!can_support_ecm(cdev->gadget)) {
1458c2ecf20Sopenharmony_ci		dev_err(&gadget->dev, "controller '%s' not usable\n",
1468c2ecf20Sopenharmony_ci				gadget->name);
1478c2ecf20Sopenharmony_ci		return -EINVAL;
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	fi_ecm = usb_get_function_instance("ecm");
1518c2ecf20Sopenharmony_ci	if (IS_ERR(fi_ecm))
1528c2ecf20Sopenharmony_ci		return PTR_ERR(fi_ecm);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	ecm_opts = container_of(fi_ecm, struct f_ecm_opts, func_inst);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	gether_set_qmult(ecm_opts->net, qmult);
1578c2ecf20Sopenharmony_ci	if (!gether_set_host_addr(ecm_opts->net, host_addr))
1588c2ecf20Sopenharmony_ci		pr_info("using host ethernet address: %s", host_addr);
1598c2ecf20Sopenharmony_ci	if (!gether_set_dev_addr(ecm_opts->net, dev_addr))
1608c2ecf20Sopenharmony_ci		pr_info("using self ethernet address: %s", dev_addr);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	fi_serial = usb_get_function_instance("acm");
1638c2ecf20Sopenharmony_ci	if (IS_ERR(fi_serial)) {
1648c2ecf20Sopenharmony_ci		status = PTR_ERR(fi_serial);
1658c2ecf20Sopenharmony_ci		goto fail;
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/* Allocate string descriptor numbers ... note that string
1698c2ecf20Sopenharmony_ci	 * contents can be overridden by the composite_dev glue.
1708c2ecf20Sopenharmony_ci	 */
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	status = usb_string_ids_tab(cdev, strings_dev);
1738c2ecf20Sopenharmony_ci	if (status < 0)
1748c2ecf20Sopenharmony_ci		goto fail1;
1758c2ecf20Sopenharmony_ci	device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
1768c2ecf20Sopenharmony_ci	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (gadget_is_otg(gadget) && !otg_desc[0]) {
1798c2ecf20Sopenharmony_ci		struct usb_descriptor_header *usb_desc;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci		usb_desc = usb_otg_descriptor_alloc(gadget);
1828c2ecf20Sopenharmony_ci		if (!usb_desc) {
1838c2ecf20Sopenharmony_ci			status = -ENOMEM;
1848c2ecf20Sopenharmony_ci			goto fail1;
1858c2ecf20Sopenharmony_ci		}
1868c2ecf20Sopenharmony_ci		usb_otg_descriptor_init(gadget, usb_desc);
1878c2ecf20Sopenharmony_ci		otg_desc[0] = usb_desc;
1888c2ecf20Sopenharmony_ci		otg_desc[1] = NULL;
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/* register our configuration */
1928c2ecf20Sopenharmony_ci	status = usb_add_config(cdev, &cdc_config_driver, cdc_do_config);
1938c2ecf20Sopenharmony_ci	if (status < 0)
1948c2ecf20Sopenharmony_ci		goto fail2;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	usb_composite_overwrite_options(cdev, &coverwrite);
1978c2ecf20Sopenharmony_ci	dev_info(&gadget->dev, "%s, version: " DRIVER_VERSION "\n",
1988c2ecf20Sopenharmony_ci			DRIVER_DESC);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return 0;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cifail2:
2038c2ecf20Sopenharmony_ci	kfree(otg_desc[0]);
2048c2ecf20Sopenharmony_ci	otg_desc[0] = NULL;
2058c2ecf20Sopenharmony_cifail1:
2068c2ecf20Sopenharmony_ci	usb_put_function_instance(fi_serial);
2078c2ecf20Sopenharmony_cifail:
2088c2ecf20Sopenharmony_ci	usb_put_function_instance(fi_ecm);
2098c2ecf20Sopenharmony_ci	return status;
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic int cdc_unbind(struct usb_composite_dev *cdev)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	usb_put_function(f_acm);
2158c2ecf20Sopenharmony_ci	usb_put_function_instance(fi_serial);
2168c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(f_ecm))
2178c2ecf20Sopenharmony_ci		usb_put_function(f_ecm);
2188c2ecf20Sopenharmony_ci	if (!IS_ERR_OR_NULL(fi_ecm))
2198c2ecf20Sopenharmony_ci		usb_put_function_instance(fi_ecm);
2208c2ecf20Sopenharmony_ci	kfree(otg_desc[0]);
2218c2ecf20Sopenharmony_ci	otg_desc[0] = NULL;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	return 0;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic struct usb_composite_driver cdc_driver = {
2278c2ecf20Sopenharmony_ci	.name		= "g_cdc",
2288c2ecf20Sopenharmony_ci	.dev		= &device_desc,
2298c2ecf20Sopenharmony_ci	.strings	= dev_strings,
2308c2ecf20Sopenharmony_ci	.max_speed	= USB_SPEED_SUPER,
2318c2ecf20Sopenharmony_ci	.bind		= cdc_bind,
2328c2ecf20Sopenharmony_ci	.unbind		= cdc_unbind,
2338c2ecf20Sopenharmony_ci};
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cimodule_usb_composite_driver(cdc_driver);
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
2388c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Brownell");
2398c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
240