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)  * dbgp.c -- EHCI Debug Port device gadget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2010 Stephane Duverger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Released under the GPLv2.
^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) /* verbose messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/usb/ch9.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/usb/gadget.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "u_serial.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define DRIVER_VENDOR_ID	0x0525 /* NetChip */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define DRIVER_PRODUCT_ID	0xc0de /* undefined */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define USB_DEBUG_MAX_PACKET_SIZE     8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define DBGP_REQ_EP0_LEN              128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define DBGP_REQ_LEN                  512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static struct dbgp {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct usb_gadget  *gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct usb_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct usb_ep      *i_ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct usb_ep      *o_ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #ifdef CONFIG_USB_G_DBGP_SERIAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	struct gserial     *serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) } dbgp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static struct usb_device_descriptor device_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	.bLength = sizeof device_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	.bDescriptorType = USB_DT_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	.bcdUSB = cpu_to_le16(0x0200),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	.bDeviceClass =	USB_CLASS_VENDOR_SPEC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	.idVendor = cpu_to_le16(DRIVER_VENDOR_ID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	.idProduct = cpu_to_le16(DRIVER_PRODUCT_ID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	.bNumConfigurations = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static struct usb_debug_descriptor dbg_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	.bLength = sizeof dbg_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	.bDescriptorType = USB_DT_DEBUG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static struct usb_endpoint_descriptor i_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	.bLength = USB_DT_ENDPOINT_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	.bDescriptorType = USB_DT_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	.bmAttributes = USB_ENDPOINT_XFER_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	.bEndpointAddress = USB_DIR_IN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static struct usb_endpoint_descriptor o_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	.bLength = USB_DT_ENDPOINT_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	.bDescriptorType = USB_DT_ENDPOINT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	.bmAttributes = USB_ENDPOINT_XFER_BULK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	.bEndpointAddress = USB_DIR_OUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #ifdef CONFIG_USB_G_DBGP_PRINTK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) static int dbgp_consume(char *buf, unsigned len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	c = buf[len-1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (c != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		buf[len-1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	printk(KERN_NOTICE "%s%c", buf, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	return 0;
^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) static void __disable_ep(struct usb_ep *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	usb_ep_disable(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) static void dbgp_disable_ep(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	__disable_ep(dbgp.i_ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	__disable_ep(dbgp.o_ep);
^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) static void dbgp_complete(struct usb_ep *ep, struct usb_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	int stp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	int status = req->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (ep == dbgp.i_ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		stp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (status != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		stp = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		goto release_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	dbgp_consume(req->buf, req->actual);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	req->length = DBGP_REQ_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	err = usb_ep_queue(ep, req, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		stp = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		goto release_req;
^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) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) release_req:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	kfree(req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	usb_ep_free_request(dbgp.o_ep, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	dbgp_disable_ep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	dev_dbg(&dbgp.gadget->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		"complete: failure (%d:%d) ==> %d\n", stp, err, status);
^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) static int dbgp_enable_ep_req(struct usb_ep *ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int err, stp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct usb_request *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	req = usb_ep_alloc_request(ep, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (!req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		stp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		goto fail_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	req->buf = kzalloc(DBGP_REQ_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (!req->buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		stp = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		goto fail_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	req->complete = dbgp_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	req->length = DBGP_REQ_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	err = usb_ep_queue(ep, req, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		stp = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		goto fail_3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) fail_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	kfree(req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) fail_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	usb_ep_free_request(dbgp.o_ep, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) fail_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	dev_dbg(&dbgp.gadget->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		"enable ep req: failure (%d:%d)\n", stp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static int __enable_ep(struct usb_ep *ep, struct usb_endpoint_descriptor *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	ep->desc = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	err = usb_ep_enable(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static int dbgp_enable_ep(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	int err, stp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	err = __enable_ep(dbgp.i_ep, &i_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		stp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		goto fail_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	err = __enable_ep(dbgp.o_ep, &o_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		stp = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		goto fail_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	err = dbgp_enable_ep_req(dbgp.o_ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		stp = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		goto fail_3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) fail_3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	__disable_ep(dbgp.o_ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) fail_2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	__disable_ep(dbgp.i_ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) fail_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	dev_dbg(&dbgp.gadget->dev, "enable ep: failure (%d:%d)\n", stp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static void dbgp_disconnect(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) #ifdef CONFIG_USB_G_DBGP_PRINTK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	dbgp_disable_ep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	gserial_disconnect(dbgp.serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void dbgp_unbind(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) #ifdef CONFIG_USB_G_DBGP_SERIAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	kfree(dbgp.serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	dbgp.serial = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (dbgp.req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		kfree(dbgp.req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		usb_ep_free_request(gadget->ep0, dbgp.req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		dbgp.req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) #ifdef CONFIG_USB_G_DBGP_SERIAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static unsigned char tty_line;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static int dbgp_configure_endpoints(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	int stp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	usb_ep_autoconfig_reset(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	dbgp.i_ep = usb_ep_autoconfig(gadget, &i_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (!dbgp.i_ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		stp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		goto fail_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	i_desc.wMaxPacketSize =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	dbgp.o_ep = usb_ep_autoconfig(gadget, &o_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (!dbgp.o_ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		stp = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		goto fail_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	o_desc.wMaxPacketSize =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		cpu_to_le16(USB_DEBUG_MAX_PACKET_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	dbg_desc.bDebugInEndpoint = i_desc.bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	dbg_desc.bDebugOutEndpoint = o_desc.bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) #ifdef CONFIG_USB_G_DBGP_SERIAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	dbgp.serial->in = dbgp.i_ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	dbgp.serial->out = dbgp.o_ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	dbgp.serial->in->desc = &i_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	dbgp.serial->out->desc = &o_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) fail_1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	dev_dbg(&dbgp.gadget->dev, "ep config: failure (%d)\n", stp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int dbgp_bind(struct usb_gadget *gadget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		struct usb_gadget_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	int err, stp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	dbgp.gadget = gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	dbgp.req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (!dbgp.req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		stp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	dbgp.req->buf = kmalloc(DBGP_REQ_EP0_LEN, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	if (!dbgp.req->buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		stp = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	dbgp.req->length = DBGP_REQ_EP0_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) #ifdef CONFIG_USB_G_DBGP_SERIAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	dbgp.serial = kzalloc(sizeof(struct gserial), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	if (!dbgp.serial) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		stp = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	if (gserial_alloc_line(&tty_line)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		stp = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	err = dbgp_configure_endpoints(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		stp = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	dev_dbg(&dbgp.gadget->dev, "bind: success\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	dev_dbg(&gadget->dev, "bind: failure (%d:%d)\n", stp, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	dbgp_unbind(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static void dbgp_setup_complete(struct usb_ep *ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 				struct usb_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	dev_dbg(&dbgp.gadget->dev, "setup complete: %d, %d/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		req->status, req->actual, req->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static int dbgp_setup(struct usb_gadget *gadget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		      const struct usb_ctrlrequest *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	struct usb_request *req = dbgp.req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	u8 request = ctrl->bRequest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	u16 value = le16_to_cpu(ctrl->wValue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	u16 length = le16_to_cpu(ctrl->wLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	int err = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	void *data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	u16 len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	if (length > DBGP_REQ_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		if (ctrl->bRequestType & USB_DIR_IN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 			/* Cast away the const, we are going to overwrite on purpose. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			__le16 *temp = (__le16 *)&ctrl->wLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			*temp = cpu_to_le16(DBGP_REQ_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			length = DBGP_REQ_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	if (request == USB_REQ_GET_DESCRIPTOR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		switch (value>>8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		case USB_DT_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			dev_dbg(&dbgp.gadget->dev, "setup: desc device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 			len = sizeof device_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			data = &device_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		case USB_DT_DEBUG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 			dev_dbg(&dbgp.gadget->dev, "setup: desc debug\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			len = sizeof dbg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 			data = &dbg_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	} else if (request == USB_REQ_SET_FEATURE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		   value == USB_DEVICE_DEBUG_MODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		dev_dbg(&dbgp.gadget->dev, "setup: feat debug\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) #ifdef CONFIG_USB_G_DBGP_PRINTK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		err = dbgp_enable_ep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		err = dbgp_configure_endpoints(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		err = gserial_connect(dbgp.serial, tty_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	req->length = min(length, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	req->zero = len < req->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (data && req->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		memcpy(req->buf, data, req->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	req->complete = dbgp_setup_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	return usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	dev_dbg(&dbgp.gadget->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		"setup: failure req %x v %x\n", request, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static struct usb_gadget_driver dbgp_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	.function = "dbgp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	.max_speed = USB_SPEED_HIGH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	.bind = dbgp_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	.unbind = dbgp_unbind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	.setup = dbgp_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	.reset = dbgp_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	.disconnect = dbgp_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	.driver	= {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 		.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		.name = "dbgp"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) static int __init dbgp_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return usb_gadget_probe_driver(&dbgp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static void __exit dbgp_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	usb_gadget_unregister_driver(&dbgp_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) #ifdef CONFIG_USB_G_DBGP_SERIAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	gserial_free_line(tty_line);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) MODULE_AUTHOR("Stephane Duverger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) module_init(dbgp_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) module_exit(dbgp_exit);