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)  * Greybus "AP" USB driver for "ES2" controller chips
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright 2014-2015 Google Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright 2014-2015 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/kfifo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/greybus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include "arpc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "greybus_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) /* Default timeout for USB vendor requests. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define ES2_USB_CTRL_TIMEOUT	500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) /* Default timeout for ARPC CPort requests */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #define ES2_ARPC_CPORT_TIMEOUT	500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) /* Fixed CPort numbers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define ES2_CPORT_CDSI0		16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define ES2_CPORT_CDSI1		17
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) /* Memory sizes for the buffers sent to/from the ES2 controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #define ES2_GBUF_MSG_SIZE_MAX	2048
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) /* Memory sizes for the ARPC buffers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define ARPC_OUT_SIZE_MAX	U16_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define ARPC_IN_SIZE_MAX	128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) static const struct usb_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	{ USB_DEVICE(0x18d1, 0x1eaf) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	{ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) MODULE_DEVICE_TABLE(usb, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define APB1_LOG_SIZE		SZ_16K
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47)  * Number of CPort IN urbs in flight at any point in time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48)  * Adjust if we are having stalls in the USB buffer due to not enough urbs in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49)  * flight.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define NUM_CPORT_IN_URB	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) /* Number of CPort OUT urbs in flight at any point in time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54)  * Adjust if we get messages saying we are out of urbs in the system log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define NUM_CPORT_OUT_URB	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * Number of ARPC in urbs in flight at any point in time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define NUM_ARPC_IN_URB		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  * @endpoint: bulk in endpoint for CPort data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  * @urb: array of urbs for the CPort in messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * @buffer: array of buffers for the @cport_in_urb urbs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) struct es2_cport_in {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	__u8 endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	struct urb *urb[NUM_CPORT_IN_URB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	u8 *buffer[NUM_CPORT_IN_URB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * es2_ap_dev - ES2 USB Bridge to AP structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * @usb_dev: pointer to the USB device we are.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * @usb_intf: pointer to the USB interface we are bound to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * @hd: pointer to our gb_host_device structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80)  * @cport_in: endpoint, urbs and buffer for cport in messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81)  * @cport_out_endpoint: endpoint for for cport out messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * @cport_out_urb: array of urbs for the CPort out messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  *			not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * @cport_out_urb_cancelled: array of flags indicating whether the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  *			corresponding @cport_out_urb is being cancelled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  * @cport_out_urb_lock: locks the @cport_out_urb_busy "list"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89)  * @apb_log_task: task pointer for logging thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * @apb_log_dentry: file system entry for the log file interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * @apb_log_enable_dentry: file system entry for enabling logging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * @apb_log_fifo: kernel FIFO to carry logged data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * @arpc_urb: array of urbs for the ARPC in messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * @arpc_buffer: array of buffers for the @arpc_urb urbs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  * @arpc_id_cycle: gives an unique id to ARPC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97)  * @arpc_lock: locks ARPC list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * @arpcs: list of in progress ARPCs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) struct es2_ap_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	struct usb_device *usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	struct usb_interface *usb_intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	struct gb_host_device *hd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	struct es2_cport_in cport_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	__u8 cport_out_endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	struct urb *cport_out_urb[NUM_CPORT_OUT_URB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	bool cport_out_urb_busy[NUM_CPORT_OUT_URB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	spinlock_t cport_out_urb_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	bool cdsi1_in_use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	struct task_struct *apb_log_task;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	struct dentry *apb_log_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	struct dentry *apb_log_enable_dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 	DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	__u8 arpc_endpoint_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	struct urb *arpc_urb[NUM_ARPC_IN_URB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	u8 *arpc_buffer[NUM_ARPC_IN_URB];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 	int arpc_id_cycle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	spinlock_t arpc_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	struct list_head arpcs;
^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) struct arpc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	struct arpc_request_message *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	struct arpc_response_message *resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct completion response_received;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	bool active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	return (struct es2_ap_dev *)&hd->hd_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) static void cport_out_callback(struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) static void usb_log_enable(struct es2_ap_dev *es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) static void usb_log_disable(struct es2_ap_dev *es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		     size_t size, int *result, unsigned int timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	struct usb_device *udev = es2->usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	data = kmemdup(req, size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 				 cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 				 USB_DIR_OUT | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 				 USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 				 0, 0, data, size, ES2_USB_CTRL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 		dev_err(&udev->dev, "%s: return error %d\n", __func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	return retval;
^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) static void ap_urb_complete(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	struct usb_ctrlrequest *dr = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	kfree(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	struct usb_device *udev = es2->usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	struct usb_ctrlrequest *dr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	urb = usb_alloc_urb(0, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	if (!dr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		return -ENOMEM;
^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) 	buf = (u8 *)dr + sizeof(*dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	memcpy(buf, req, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	dr->bRequest = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	dr->wValue = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	dr->wIndex = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	dr->wLength = cpu_to_le16(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			     (unsigned char *)dr, buf, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 			     ap_urb_complete, dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	retval = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		kfree(dr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		  bool async)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	if (async)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		return output_async(es2, req, size, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	return output_sync(es2, req, size, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) static int es2_cport_in_enable(struct es2_ap_dev *es2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 			       struct es2_cport_in *cport_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		urb = cport_in->urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		ret = usb_submit_urb(urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 			dev_err(&es2->usb_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 				"failed to submit in-urb: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 			goto err_kill_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) err_kill_urbs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	for (--i; i >= 0; --i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		urb = cport_in->urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		usb_kill_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) static void es2_cport_in_disable(struct es2_ap_dev *es2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 				 struct es2_cport_in *cport_in)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 		urb = cport_in->urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		usb_kill_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) static int es2_arpc_in_enable(struct es2_ap_dev *es2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		urb = es2->arpc_urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		ret = usb_submit_urb(urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 			dev_err(&es2->usb_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 				"failed to submit arpc in-urb: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 			goto err_kill_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		}
^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) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) err_kill_urbs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	for (--i; i >= 0; --i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 		urb = es2->arpc_urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 		usb_kill_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) static void es2_arpc_in_disable(struct es2_ap_dev *es2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		urb = es2->arpc_urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		usb_kill_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	}
^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) static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	struct urb *urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	/* Look in our pool of allocated urbs first, as that's the "fastest" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		if (!es2->cport_out_urb_busy[i] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		    !es2->cport_out_urb_cancelled[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 			es2->cport_out_urb_busy[i] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			urb = es2->cport_out_urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	if (urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		return urb;
^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) 	 * Crap, pool is empty, complain to the syslog and go allocate one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	 * dynamically as we have to succeed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	dev_dbg(&es2->usb_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		"No free CPort OUT urbs, having to dynamically allocate one!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	return usb_alloc_urb(0, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) static void free_urb(struct es2_ap_dev *es2, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	 * See if this was an urb in our pool, if so mark it "free", otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	 * we need to free it ourselves.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		if (urb == es2->cport_out_urb[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 			es2->cport_out_urb_busy[i] = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 			urb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	/* If urb is not NULL, then we need to free this urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	usb_free_urb(urb);
^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)  * We (ab)use the operation-message header pad bytes to transfer the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362)  * cport id in order to minimise overhead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	header->pad[0] = cport_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) /* Clear the pad bytes used for the CPort id */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) static void gb_message_cport_clear(struct gb_operation_msg_hdr *header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	header->pad[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) /* Extract the CPort id packed into the header, and clear it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	u16 cport_id = header->pad[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	gb_message_cport_clear(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	return cport_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387)  * Returns zero if the message was successfully queued, or a negative errno
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388)  * otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) static int message_send(struct gb_host_device *hd, u16 cport_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 			struct gb_message *message, gfp_t gfp_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	struct usb_device *udev = es2->usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	size_t buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	 * The data actually transferred will include an indication
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	 * of where the data should be sent.  Do one last check of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	 * the target CPort id before filling it in.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	if (!cport_id_valid(hd, cport_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 		dev_err(&udev->dev, "invalid cport %u\n", cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	/* Find a free urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	urb = next_free_urb(es2, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	if (!urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	message->hcpriv = urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	/* Pack the cport id into the message header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	gb_message_cport_pack(message->header, cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	buffer_size = sizeof(*message->header) + message->payload_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 	usb_fill_bulk_urb(urb, udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			  usb_sndbulkpipe(udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 					  es2->cport_out_endpoint),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 			  message->buffer, buffer_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 			  cport_out_callback, message);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	urb->transfer_flags |= URB_ZERO_PACKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	trace_gb_message_submit(message);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	retval = usb_submit_urb(urb, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		message->hcpriv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		free_urb(es2, urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		gb_message_cport_clear(message->header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  * Can not be called in atomic context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) static void message_cancel(struct gb_message *message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	struct gb_host_device *hd = message->operation->connection->hd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	might_sleep();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	spin_lock_irq(&es2->cport_out_urb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	urb = message->hcpriv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	/* Prevent dynamically allocated urb from being deallocated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	usb_get_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	/* Prevent pre-allocated urb from being reused. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		if (urb == es2->cport_out_urb[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			es2->cport_out_urb_cancelled[i] = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	spin_unlock_irq(&es2->cport_out_urb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	usb_kill_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	if (i < NUM_CPORT_OUT_URB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		spin_lock_irq(&es2->cport_out_urb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		es2->cport_out_urb_cancelled[i] = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		spin_unlock_irq(&es2->cport_out_urb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) static int es2_cport_allocate(struct gb_host_device *hd, int cport_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 			      unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	struct ida *id_map = &hd->cport_id_map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	int ida_start, ida_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	switch (cport_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	case ES2_CPORT_CDSI0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	case ES2_CPORT_CDSI1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		dev_err(&hd->dev, "cport %d not available\n", cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	if (flags & GB_CONNECTION_FLAG_OFFLOADED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	    flags & GB_CONNECTION_FLAG_CDSI1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 		if (es2->cdsi1_in_use) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			dev_err(&hd->dev, "CDSI1 already in use\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 		es2->cdsi1_in_use = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 		return ES2_CPORT_CDSI1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (cport_id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		ida_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		ida_end = hd->num_cports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	} else if (cport_id < hd->num_cports) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		ida_start = cport_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		ida_end = cport_id + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 		dev_err(&hd->dev, "cport %d not available\n", cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) static void es2_cport_release(struct gb_host_device *hd, u16 cport_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	switch (cport_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	case ES2_CPORT_CDSI1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		es2->cdsi1_in_use = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	ida_simple_remove(&hd->cport_id_map, cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) static int cport_enable(struct gb_host_device *hd, u16 cport_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 			unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	struct usb_device *udev = es2->usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	struct gb_apb_request_cport_flags *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	u32 connection_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	req = kzalloc(sizeof(*req), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	if (!req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	connection_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (flags & GB_CONNECTION_FLAG_CONTROL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		connection_flags |= GB_APB_CPORT_FLAG_CONTROL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	if (flags & GB_CONNECTION_FLAG_HIGH_PRIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	req->flags = cpu_to_le32(connection_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		cport_id, connection_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 			      GB_APB_REQUEST_CPORT_FLAGS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			      USB_DIR_OUT | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 			      USB_RECIP_INTERFACE, cport_id, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 			      req, sizeof(*req), ES2_USB_CTRL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (ret != sizeof(*req)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		dev_err(&udev->dev, "failed to set cport flags for port %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	kfree(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) static int es2_cport_connected(struct gb_host_device *hd, u16 cport_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	struct device *dev = &es2->usb_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	struct arpc_cport_connected_req req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	req.cport_id = cpu_to_le16(cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	ret = arpc_sync(es2, ARPC_TYPE_CPORT_CONNECTED, &req, sizeof(req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 			NULL, ES2_ARPC_CPORT_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 		dev_err(dev, "failed to set connected state for cport %u: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 			cport_id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) static int es2_cport_flush(struct gb_host_device *hd, u16 cport_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	struct device *dev = &es2->usb_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	struct arpc_cport_flush_req req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	req.cport_id = cpu_to_le16(cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	ret = arpc_sync(es2, ARPC_TYPE_CPORT_FLUSH, &req, sizeof(req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 			NULL, ES2_ARPC_CPORT_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		dev_err(dev, "failed to flush cport %u: %d\n", cport_id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) static int es2_cport_shutdown(struct gb_host_device *hd, u16 cport_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			      u8 phase, unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	struct device *dev = &es2->usb_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	struct arpc_cport_shutdown_req req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	if (timeout > U16_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	req.cport_id = cpu_to_le16(cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	req.timeout = cpu_to_le16(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	req.phase = phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	ret = arpc_sync(es2, ARPC_TYPE_CPORT_SHUTDOWN, &req, sizeof(req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			&result, ES2_ARPC_CPORT_TIMEOUT + timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		dev_err(dev, "failed to send shutdown over cport %u: %d (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 			cport_id, ret, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) static int es2_cport_quiesce(struct gb_host_device *hd, u16 cport_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 			     size_t peer_space, unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	struct device *dev = &es2->usb_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	struct arpc_cport_quiesce_req req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	if (peer_space > U16_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 	if (timeout > U16_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	req.cport_id = cpu_to_le16(cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	req.peer_space = cpu_to_le16(peer_space);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	req.timeout = cpu_to_le16(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	ret = arpc_sync(es2, ARPC_TYPE_CPORT_QUIESCE, &req, sizeof(req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 			&result, ES2_ARPC_CPORT_TIMEOUT + timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		dev_err(dev, "failed to quiesce cport %u: %d (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 			cport_id, ret, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) static int es2_cport_clear(struct gb_host_device *hd, u16 cport_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	struct device *dev = &es2->usb_dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	struct arpc_cport_clear_req req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	req.cport_id = cpu_to_le16(cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	ret = arpc_sync(es2, ARPC_TYPE_CPORT_CLEAR, &req, sizeof(req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 			NULL, ES2_ARPC_CPORT_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 		dev_err(dev, "failed to clear cport %u: %d\n", cport_id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	struct usb_device *udev = es2->usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 				 GB_APB_REQUEST_LATENCY_TAG_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 				 USB_DIR_OUT | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 				 USB_RECIP_INTERFACE, cport_id, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 				 0, ES2_USB_CTRL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 			cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	struct usb_device *udev = es2->usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 				 GB_APB_REQUEST_LATENCY_TAG_DIS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 				 USB_DIR_OUT | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 				 USB_RECIP_INTERFACE, cport_id, 0, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 				 0, ES2_USB_CTRL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) static struct gb_hd_driver es2_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	.hd_priv_size			= sizeof(struct es2_ap_dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	.message_send			= message_send,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	.message_cancel			= message_cancel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	.cport_allocate			= es2_cport_allocate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	.cport_release			= es2_cport_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	.cport_enable			= cport_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	.cport_connected		= es2_cport_connected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	.cport_flush			= es2_cport_flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	.cport_shutdown			= es2_cport_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	.cport_quiesce			= es2_cport_quiesce,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	.cport_clear			= es2_cport_clear,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	.latency_tag_enable		= latency_tag_enable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	.latency_tag_disable		= latency_tag_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	.output				= output,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) /* Common function to report consistent warnings based on URB status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) static int check_urb_status(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	struct device *dev = &urb->dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	case -EOVERFLOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		dev_err(dev, "%s: overflow actual length is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 			__func__, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	case -EILSEQ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	case -EPROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		/* device is gone, stop sending */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	dev_err(dev, "%s: unknown status %d\n", __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) static void es2_destroy(struct es2_ap_dev *es2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	debugfs_remove(es2->apb_log_enable_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	usb_log_disable(es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	/* Tear down everything! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		urb = es2->cport_out_urb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		usb_kill_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		usb_free_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		es2->cport_out_urb[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		es2->cport_out_urb_busy[i] = false;	/* just to be anal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		usb_free_urb(es2->arpc_urb[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		kfree(es2->arpc_buffer[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		es2->arpc_buffer[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		usb_free_urb(es2->cport_in.urb[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		kfree(es2->cport_in.buffer[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		es2->cport_in.buffer[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	/* release reserved CDSI0 and CDSI1 cports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	udev = es2->usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	gb_hd_put(es2->hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	usb_put_dev(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) static void cport_in_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	struct gb_host_device *hd = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	struct device *dev = &urb->dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	struct gb_operation_msg_hdr *header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	int status = check_urb_status(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	u16 cport_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		if ((status == -EAGAIN) || (status == -EPROTO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		/* The urb is being unlinked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		if (status == -ENOENT || status == -ESHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		dev_err(dev, "urb cport in error %d (dropped)\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	if (urb->actual_length < sizeof(*header)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		dev_err(dev, "short message received\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	/* Extract the CPort id, which is packed in the message header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	header = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	cport_id = gb_message_cport_unpack(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	if (cport_id_valid(hd, cport_id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		greybus_data_rcvd(hd, cport_id, urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 				  urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		dev_err(dev, "invalid cport id %u received\n", cport_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	/* put our urb back in the request pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	retval = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		dev_err(dev, "failed to resubmit in-urb: %d\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) static void cport_out_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	struct gb_message *message = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	struct gb_host_device *hd = message->operation->connection->hd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 	struct es2_ap_dev *es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	int status = check_urb_status(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	gb_message_cport_clear(message->header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	spin_lock_irqsave(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	message->hcpriv = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	 * Tell the submitter that the message send (attempt) is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	 * complete, and report the status.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	greybus_message_sent(hd, message, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	free_urb(es2, urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) static struct arpc *arpc_alloc(void *payload, u16 size, u8 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	struct arpc *rpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	rpc = kzalloc(sizeof(*rpc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	if (!rpc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	INIT_LIST_HEAD(&rpc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	if (!rpc->req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		goto err_free_rpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	if (!rpc->resp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 		goto err_free_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	rpc->req->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	rpc->req->size = cpu_to_le16(sizeof(*rpc->req) + size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	memcpy(rpc->req->data, payload, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	init_completion(&rpc->response_received);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	return rpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) err_free_req:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	kfree(rpc->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) err_free_rpc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	kfree(rpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) static void arpc_free(struct arpc *rpc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	kfree(rpc->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	kfree(rpc->resp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	kfree(rpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	struct arpc *rpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	list_for_each_entry(rpc, &es2->arpcs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		if (rpc->req->id == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 			return rpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	rpc->active = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 	rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	list_add_tail(&rpc->list, &es2->arpcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	if (rpc->active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		rpc->active = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		list_del(&rpc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	struct usb_device *udev = es2->usb_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 				 GB_APB_REQUEST_ARPC_RUN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 				 USB_DIR_OUT | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 				 USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 				 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 				 rpc->req, le16_to_cpu(rpc->req->size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 				 ES2_USB_CTRL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	if (retval != le16_to_cpu(rpc->req->size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		dev_err(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 			"failed to send ARPC request %d: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			rpc->req->type, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		if (retval > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		     size_t size, int *result, unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	struct arpc *rpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		*result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	rpc = arpc_alloc(payload, size, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	if (!rpc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	spin_lock_irqsave(&es2->arpc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	arpc_add(es2, rpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	spin_unlock_irqrestore(&es2->arpc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	retval = arpc_send(es2, rpc, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		goto out_arpc_del;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	retval = wait_for_completion_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 						&rpc->response_received,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 						msecs_to_jiffies(timeout));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	if (retval <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		if (!retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 			retval = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 		goto out_arpc_del;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if (rpc->resp->result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		retval = -EREMOTEIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 			*result = rpc->resp->result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) out_arpc_del:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	spin_lock_irqsave(&es2->arpc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	arpc_del(es2, rpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	spin_unlock_irqrestore(&es2->arpc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	arpc_free(rpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	if (retval < 0 && retval != -EREMOTEIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		dev_err(&es2->usb_dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 			"failed to execute ARPC: %d\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) static void arpc_in_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	struct es2_ap_dev *es2 = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	struct device *dev = &urb->dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	int status = check_urb_status(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	struct arpc *rpc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	struct arpc_response_message *resp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	if (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		if ((status == -EAGAIN) || (status == -EPROTO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		/* The urb is being unlinked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 		if (status == -ENOENT || status == -ESHUTDOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		dev_err(dev, "arpc in-urb error %d (dropped)\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	if (urb->actual_length < sizeof(*resp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		dev_err(dev, "short aprc response received\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	resp = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	spin_lock_irqsave(&es2->arpc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	rpc = arpc_find(es2, resp->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	if (!rpc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 		dev_err(dev, "invalid arpc response id received: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 			le16_to_cpu(resp->id));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		spin_unlock_irqrestore(&es2->arpc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	arpc_del(es2, rpc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	memcpy(rpc->resp, resp, sizeof(*resp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 	complete(&rpc->response_received);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	spin_unlock_irqrestore(&es2->arpc_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	/* put our urb back in the request pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 	retval = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) #define APB1_LOG_MSG_SIZE	64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) static void apb_log_get(struct es2_ap_dev *es2, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		retval = usb_control_msg(es2->usb_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 					 usb_rcvctrlpipe(es2->usb_dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 					 GB_APB_REQUEST_LOG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 					 USB_DIR_IN | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 					 USB_RECIP_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 					 0x00, 0x00,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 					 buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 					 APB1_LOG_MSG_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 					 ES2_USB_CTRL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		if (retval > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			kfifo_in(&es2->apb_log_fifo, buf, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	} while (retval > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) static int apb_log_poll(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	struct es2_ap_dev *es2 = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		msleep(1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		apb_log_get(es2, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) static ssize_t apb_log_read(struct file *f, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 			    size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	struct es2_ap_dev *es2 = file_inode(f)->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	size_t copied;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	char *tmp_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	if (count > APB1_LOG_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 		count = APB1_LOG_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	tmp_buf = kmalloc(count, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	if (!tmp_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	kfree(tmp_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) static const struct file_operations apb_log_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	.read	= apb_log_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) static void usb_log_enable(struct es2_ap_dev *es2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (!IS_ERR_OR_NULL(es2->apb_log_task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	/* get log from APB1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	if (IS_ERR(es2->apb_log_task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	/* XXX We will need to rename this per APB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	es2->apb_log_dentry = debugfs_create_file("apb_log", 0444,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 						  gb_debugfs_get(), es2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 						  &apb_log_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) static void usb_log_disable(struct es2_ap_dev *es2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	if (IS_ERR_OR_NULL(es2->apb_log_task))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	debugfs_remove(es2->apb_log_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	es2->apb_log_dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	kthread_stop(es2->apb_log_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	es2->apb_log_task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) static ssize_t apb_log_enable_read(struct file *f, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 				   size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 	struct es2_ap_dev *es2 = file_inode(f)->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	int enable = !IS_ERR_OR_NULL(es2->apb_log_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 	char tmp_buf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 	sprintf(tmp_buf, "%d\n", enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) static ssize_t apb_log_enable_write(struct file *f, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 				    size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	int enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	struct es2_ap_dev *es2 = file_inode(f)->i_private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	retval = kstrtoint_from_user(buf, count, 10, &enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	if (enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		usb_log_enable(es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		usb_log_disable(es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) static const struct file_operations apb_log_enable_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	.read	= apb_log_enable_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	.write	= apb_log_enable_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) static int apb_get_cport_count(struct usb_device *udev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	__le16 *cport_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	if (!cport_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 				 GB_APB_REQUEST_CPORT_COUNT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 				 USB_DIR_IN | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 				 USB_RECIP_INTERFACE, 0, 0, cport_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 				 sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	if (retval != sizeof(*cport_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		if (retval >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 			retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	retval = le16_to_cpu(*cport_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	/* We need to fit a CPort ID in one byte of a message header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	if (retval > U8_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		retval = U8_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	kfree(cport_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)  * The ES2 USB Bridge device has 15 endpoints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)  * 1 Control - usual USB stuff + AP -> APBridgeA messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246)  * 7 Bulk IN - CPort data in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)  * 7 Bulk OUT - CPort data out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) static int ap_probe(struct usb_interface *interface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		    const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	struct es2_ap_dev *es2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	struct gb_host_device *hd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	struct usb_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	struct usb_host_interface *iface_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 	struct usb_endpoint_descriptor *endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	__u8 ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	int num_cports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	bool bulk_out_found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	bool bulk_in_found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	bool arpc_in_found = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	udev = usb_get_dev(interface_to_usbdev(interface));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	num_cports = apb_get_cport_count(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	if (num_cports < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 		usb_put_dev(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 			num_cports);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 		return num_cports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 			  num_cports);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	if (IS_ERR(hd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 		usb_put_dev(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 		return PTR_ERR(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	es2 = hd_to_es2(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	es2->hd = hd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	es2->usb_intf = interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	es2->usb_dev = udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	spin_lock_init(&es2->cport_out_urb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	INIT_KFIFO(es2->apb_log_fifo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	usb_set_intfdata(interface, es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	 * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	 * dynamically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	/* find all bulk endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	iface_desc = interface->cur_altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		endpoint = &iface_desc->endpoint[i].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		ep_addr = endpoint->bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 		if (usb_endpoint_is_bulk_in(endpoint)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 			if (!bulk_in_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 				es2->cport_in.endpoint = ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 				bulk_in_found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			} else if (!arpc_in_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 				es2->arpc_endpoint_in = ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 				arpc_in_found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 				dev_warn(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 					 "Unused bulk IN endpoint found: 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 					 ep_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		if (usb_endpoint_is_bulk_out(endpoint)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 			if (!bulk_out_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 				es2->cport_out_endpoint = ep_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 				bulk_out_found = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 				dev_warn(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 					 "Unused bulk OUT endpoint found: 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 					 ep_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		dev_warn(&udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			 "Unknown endpoint type found, address 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 			 ep_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	if (!bulk_in_found || !arpc_in_found || !bulk_out_found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	/* Allocate buffers for our cport in messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	for (i = 0; i < NUM_CPORT_IN_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		es2->cport_in.urb[i] = urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 		buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		if (!buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 			retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		usb_fill_bulk_urb(urb, udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 				  usb_rcvbulkpipe(udev, es2->cport_in.endpoint),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 				  buffer, ES2_GBUF_MSG_SIZE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 				  cport_in_callback, hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		es2->cport_in.buffer[i] = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	/* Allocate buffers for ARPC in messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	for (i = 0; i < NUM_ARPC_IN_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 		u8 *buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 			retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		es2->arpc_urb[i] = urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		if (!buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		usb_fill_bulk_urb(urb, udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 				  usb_rcvbulkpipe(udev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 						  es2->arpc_endpoint_in),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 				  buffer, ARPC_IN_SIZE_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 				  arpc_in_callback, es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 		es2->arpc_buffer[i] = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	/* Allocate urbs for our CPort OUT messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	for (i = 0; i < NUM_CPORT_OUT_URB; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		struct urb *urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 			retval = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		es2->cport_out_urb[i] = urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		es2->cport_out_urb_busy[i] = false;	/* just to be anal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 	/* XXX We will need to rename this per APB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 							 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 							 gb_debugfs_get(), es2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 							 &apb_log_enable_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	INIT_LIST_HEAD(&es2->arpcs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	spin_lock_init(&es2->arpc_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 	retval = es2_arpc_in_enable(es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	retval = gb_hd_add(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		goto err_disable_arpc_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	retval = es2_cport_in_enable(es2, &es2->cport_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 		goto err_hd_del;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) err_hd_del:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 	gb_hd_del(hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) err_disable_arpc_in:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	es2_arpc_in_disable(es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	es2_destroy(es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) static void ap_disconnect(struct usb_interface *interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 	struct es2_ap_dev *es2 = usb_get_intfdata(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	gb_hd_del(es2->hd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	es2_cport_in_disable(es2, &es2->cport_in);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	es2_arpc_in_disable(es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	es2_destroy(es2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) static struct usb_driver es2_ap_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	.name =		"es2_ap_driver",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	.probe =	ap_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	.disconnect =	ap_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	.id_table =	id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	.soft_unbind =	1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) module_usb_driver(es2_ap_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>");