Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * ncm.c -- NCM gadget driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2010 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Contact: Yauheni Kaliuta <yauheni.kaliuta@nokia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * The driver borrows from ether.c which is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * Copyright (C) 2003-2005,2008 David Brownell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * Copyright (C) 2008 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /* #define DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) /* #define VERBOSE_DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/usb/composite.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "u_ether.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "u_ncm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define DRIVER_DESC		"NCM Gadget"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * Instead:  allocate your own, using normal USB-IF procedures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /* Thanks to NetChip Technologies for donating this product ID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * It's for devices with only CDC Ethernet configurations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define CDC_VENDOR_NUM		0x0525	/* NetChip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define CDC_PRODUCT_NUM		0xa4a1	/* Linux-USB Ethernet Gadget */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) USB_GADGET_COMPOSITE_OPTIONS();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) USB_ETHERNET_MODULE_PARAMETERS();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static struct usb_device_descriptor device_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	.bLength =		sizeof device_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	.bDescriptorType =	USB_DT_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	/* .bcdUSB = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	.bDeviceClass =		USB_CLASS_COMM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	.bDeviceSubClass =	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	.bDeviceProtocol =	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	/* .bMaxPacketSize0 = f(hardware) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	/* Vendor and product id defaults change according to what configs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	 * we support.  (As does bNumConfigurations.)  These values can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 * also be overridden by module parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	.idVendor =		cpu_to_le16 (CDC_VENDOR_NUM),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	.idProduct =		cpu_to_le16 (CDC_PRODUCT_NUM),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	/* .bcdDevice = f(hardware) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	/* .iManufacturer = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	/* .iProduct = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	/* NO SERIAL NUMBER */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	.bNumConfigurations =	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static const struct usb_descriptor_header *otg_desc[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) /* string IDs are assigned dynamically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static struct usb_string strings_dev[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	[USB_GADGET_MANUFACTURER_IDX].s = "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	[USB_GADGET_PRODUCT_IDX].s = DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	[USB_GADGET_SERIAL_IDX].s = "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	{  } /* end of list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static struct usb_gadget_strings stringtab_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	.language	= 0x0409,	/* en-us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	.strings	= strings_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static struct usb_gadget_strings *dev_strings[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	&stringtab_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static struct usb_function_instance *f_ncm_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static struct usb_function *f_ncm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static int ncm_do_config(struct usb_configuration *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/* FIXME alloc iConfiguration string, set it in c->strings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (gadget_is_otg(c->cdev->gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		c->descriptors = otg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		c->bmAttributes |= USB_CONFIG_ATT_WAKEUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	f_ncm = usb_get_function(f_ncm_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (IS_ERR(f_ncm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		return PTR_ERR(f_ncm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	status = usb_add_function(c, f_ncm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		usb_put_function(f_ncm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static struct usb_configuration ncm_config_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/* .label = f(hardware) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	.label			= "CDC Ethernet (NCM)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	.bConfigurationValue	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	/* .iConfiguration = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	.bmAttributes		= USB_CONFIG_ATT_SELFPOWER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int gncm_bind(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct usb_gadget	*gadget = cdev->gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	struct f_ncm_opts	*ncm_opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	int			status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	f_ncm_inst = usb_get_function_instance("ncm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (IS_ERR(f_ncm_inst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		return PTR_ERR(f_ncm_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	ncm_opts = container_of(f_ncm_inst, struct f_ncm_opts, func_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	gether_set_qmult(ncm_opts->net, qmult);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	if (!gether_set_host_addr(ncm_opts->net, host_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		pr_info("using host ethernet address: %s", host_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (!gether_set_dev_addr(ncm_opts->net, dev_addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		pr_info("using self ethernet address: %s", dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	/* Allocate string descriptor numbers ... note that string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	 * contents can be overridden by the composite_dev glue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	status = usb_string_ids_tab(cdev, strings_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (gadget_is_otg(gadget) && !otg_desc[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		struct usb_descriptor_header *usb_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		usb_desc = usb_otg_descriptor_alloc(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		if (!usb_desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		usb_otg_descriptor_init(gadget, usb_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		otg_desc[0] = usb_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		otg_desc[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	status = usb_add_config(cdev, &ncm_config_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 				ncm_do_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	usb_composite_overwrite_options(cdev, &coverwrite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	dev_info(&gadget->dev, "%s\n", DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) fail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	kfree(otg_desc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	otg_desc[0] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	usb_put_function_instance(f_ncm_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int gncm_unbind(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (!IS_ERR_OR_NULL(f_ncm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		usb_put_function(f_ncm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	if (!IS_ERR_OR_NULL(f_ncm_inst))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		usb_put_function_instance(f_ncm_inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	kfree(otg_desc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	otg_desc[0] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static struct usb_composite_driver ncm_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.name		= "g_ncm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.dev		= &device_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.strings	= dev_strings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	.max_speed	= USB_SPEED_SUPER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	.bind		= gncm_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	.unbind		= gncm_unbind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) module_usb_composite_driver(ncm_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) MODULE_AUTHOR("Yauheni Kaliuta");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) MODULE_LICENSE("GPL");