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)  * serial.c -- USB gadget serial driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2003 Al Borchers (alborchers@steinerpoint.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2008 by David Brownell
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2008 by Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "u_serial.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) /* Defines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #define GS_VERSION_STR			"v2.4"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define GS_VERSION_NUM			0x2400
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define GS_LONG_NAME			"Gadget Serial"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define GS_VERSION_NAME			GS_LONG_NAME " " GS_VERSION_STR
^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) USB_GADGET_COMPOSITE_OPTIONS();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /* Thanks to NetChip Technologies for donating this product ID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) * DO NOT REUSE THESE IDs with a protocol-incompatible driver!!  Ever!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) * Instead:  allocate your own, using normal USB-IF procedures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define GS_VENDOR_ID			0x0525	/* NetChip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define GS_PRODUCT_ID			0xa4a6	/* Linux-USB Serial Gadget */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define GS_CDC_PRODUCT_ID		0xa4a7	/* ... as CDC-ACM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define GS_CDC_OBEX_PRODUCT_ID		0xa4a9	/* ... as CDC-OBEX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /* string IDs are assigned dynamically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define STRING_DESCRIPTION_IDX		USB_GADGET_FIRST_AVAIL_IDX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static struct usb_string strings_dev[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	[USB_GADGET_MANUFACTURER_IDX].s = "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	[USB_GADGET_PRODUCT_IDX].s = GS_VERSION_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	[USB_GADGET_SERIAL_IDX].s = "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	[STRING_DESCRIPTION_IDX].s = NULL /* updated; f(use_acm) */,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	{  } /* end of list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static struct usb_gadget_strings stringtab_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	.language	= 0x0409,	/* en-us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	.strings	= strings_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static struct usb_gadget_strings *dev_strings[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	&stringtab_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static struct usb_device_descriptor device_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	.bLength =		USB_DT_DEVICE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	.bDescriptorType =	USB_DT_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	/* .bcdUSB = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	/* .bDeviceClass = f(use_acm) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	.bDeviceSubClass =	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	.bDeviceProtocol =	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	/* .bMaxPacketSize0 = f(hardware) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	.idVendor =		cpu_to_le16(GS_VENDOR_ID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	/* .idProduct =	f(use_acm) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	.bcdDevice = cpu_to_le16(GS_VERSION_NUM),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	/* .iManufacturer = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	/* .iProduct = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	.bNumConfigurations =	1,
^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 const struct usb_descriptor_header *otg_desc[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) /* Module */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) MODULE_DESCRIPTION(GS_VERSION_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) MODULE_AUTHOR("Al Borchers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) MODULE_AUTHOR("David Brownell");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static bool use_acm = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) module_param(use_acm, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) MODULE_PARM_DESC(use_acm, "Use CDC ACM, default=yes");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static bool use_obex = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) module_param(use_obex, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) MODULE_PARM_DESC(use_obex, "Use CDC OBEX, default=no");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static unsigned n_ports = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) module_param(n_ports, uint, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) MODULE_PARM_DESC(n_ports, "number of ports to create, default=1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static bool enable = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int switch_gserial_enable(bool do_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) static int enable_set(const char *s, const struct kernel_param *kp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	bool do_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (!s)	/* called for no-arg enable == default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	ret = strtobool(s, &do_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (ret || enable == do_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	ret = switch_gserial_enable(do_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		enable = do_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static const struct kernel_param_ops enable_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	.set = enable_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	.get = param_get_bool,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) module_param_cb(enable, &enable_ops, &enable, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static struct usb_configuration serial_config_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	/* .label = f(use_acm) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	/* .bConfigurationValue = f(use_acm) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	/* .iConfiguration = DYNAMIC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	.bmAttributes	= USB_CONFIG_ATT_SELFPOWER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static struct usb_function_instance *fi_serial[MAX_U_SERIAL_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static struct usb_function *f_serial[MAX_U_SERIAL_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) static int serial_register_ports(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		struct usb_configuration *c, const char *f_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	ret = usb_add_config_only(cdev, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	for (i = 0; i < n_ports; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		fi_serial[i] = usb_get_function_instance(f_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		if (IS_ERR(fi_serial[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			ret = PTR_ERR(fi_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		f_serial[i] = usb_get_function(fi_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		if (IS_ERR(f_serial[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			ret = PTR_ERR(f_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 			goto err_get_func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		ret = usb_add_function(c, f_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			goto err_add_func;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) err_add_func:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	usb_put_function(f_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) err_get_func:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	usb_put_function_instance(fi_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	while (i >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		usb_remove_function(c, f_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		usb_put_function(f_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		usb_put_function_instance(fi_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int gs_bind(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int			status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	/* Allocate string descriptor numbers ... note that string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	 * contents can be overridden by the composite_dev glue.
^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) 	status = usb_string_ids_tab(cdev, strings_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	device_desc.iManufacturer = strings_dev[USB_GADGET_MANUFACTURER_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	device_desc.iProduct = strings_dev[USB_GADGET_PRODUCT_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	status = strings_dev[STRING_DESCRIPTION_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	serial_config_driver.iConfiguration = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (gadget_is_otg(cdev->gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		if (!otg_desc[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 			struct usb_descriptor_header *usb_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 			usb_desc = usb_otg_descriptor_alloc(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			if (!usb_desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 				status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			usb_otg_descriptor_init(cdev->gadget, usb_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			otg_desc[0] = usb_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			otg_desc[1] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		serial_config_driver.descriptors = otg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		serial_config_driver.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	/* register our configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (use_acm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		status  = serial_register_ports(cdev, &serial_config_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 				"acm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		usb_ep_autoconfig_reset(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	} else if (use_obex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		status = serial_register_ports(cdev, &serial_config_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				"obex");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		status = serial_register_ports(cdev, &serial_config_driver,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 				"gser");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	usb_composite_overwrite_options(cdev, &coverwrite);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	INFO(cdev, "%s\n", GS_VERSION_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) fail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	kfree(otg_desc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	otg_desc[0] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int gs_unbind(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	for (i = 0; i < n_ports; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		usb_put_function(f_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		usb_put_function_instance(fi_serial[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	kfree(otg_desc[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	otg_desc[0] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static struct usb_composite_driver gserial_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	.name		= "g_serial",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	.dev		= &device_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	.strings	= dev_strings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	.max_speed	= USB_SPEED_SUPER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	.bind		= gs_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	.unbind		= gs_unbind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int switch_gserial_enable(bool do_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (!serial_config_driver.label)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		/* init() was not called, yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (do_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return usb_composite_probe(&gserial_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	usb_composite_unregister(&gserial_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) static int __init init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	/* We *could* export two configs; that'd be much cleaner...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * but neither of these product IDs was defined that way.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (use_acm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		serial_config_driver.label = "CDC ACM config";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		serial_config_driver.bConfigurationValue = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		device_desc.bDeviceClass = USB_CLASS_COMM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		device_desc.idProduct =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 				cpu_to_le16(GS_CDC_PRODUCT_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	} else if (use_obex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		serial_config_driver.label = "CDC OBEX config";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		serial_config_driver.bConfigurationValue = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		device_desc.bDeviceClass = USB_CLASS_COMM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		device_desc.idProduct =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			cpu_to_le16(GS_CDC_OBEX_PRODUCT_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		serial_config_driver.label = "Generic Serial config";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		serial_config_driver.bConfigurationValue = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		device_desc.idProduct =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 				cpu_to_le16(GS_PRODUCT_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	strings_dev[STRING_DESCRIPTION_IDX].s = serial_config_driver.label;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (!enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return usb_composite_probe(&gserial_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) module_init(init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static void __exit cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		usb_composite_unregister(&gserial_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) module_exit(cleanup);