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)  * Copyright (C) 2003-2008 Takahiro Hirofuchi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/usb/hcd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "usbip_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "stub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) static int is_clear_halt_cmd(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 	struct usb_ctrlrequest *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	req = (struct usb_ctrlrequest *) urb->setup_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	       (req->bRequestType == USB_RECIP_ENDPOINT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	       (req->wValue == USB_ENDPOINT_HALT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static int is_set_interface_cmd(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct usb_ctrlrequest *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	req = (struct usb_ctrlrequest *) urb->setup_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	return (req->bRequest == USB_REQ_SET_INTERFACE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		(req->bRequestType == USB_RECIP_INTERFACE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static int is_set_configuration_cmd(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct usb_ctrlrequest *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	req = (struct usb_ctrlrequest *) urb->setup_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		(req->bRequestType == USB_RECIP_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static int is_reset_device_cmd(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct usb_ctrlrequest *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	__u16 value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	__u16 index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	req = (struct usb_ctrlrequest *) urb->setup_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	value = le16_to_cpu(req->wValue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	index = le16_to_cpu(req->wIndex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if ((req->bRequest == USB_REQ_SET_FEATURE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	    (req->bRequestType == USB_RT_PORT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	    (value == USB_PORT_FEAT_RESET)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		usbip_dbg_stub_rx("reset_device_cmd, port %u\n", index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static int tweak_clear_halt_cmd(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct usb_ctrlrequest *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int target_endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	int target_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int target_pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	req = (struct usb_ctrlrequest *) urb->setup_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	 * The stalled endpoint is specified in the wIndex value. The endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	 * of the urb is the target of this clear_halt request (i.e., control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	 * endpoint).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	target_endp = le16_to_cpu(req->wIndex) & 0x000f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	/* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80.  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	target_dir = le16_to_cpu(req->wIndex) & 0x0080;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (target_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	ret = usb_clear_halt(urb->dev, target_pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		dev_err(&urb->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 			"usb_clear_halt error: devnum %d endp %d ret %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			urb->dev->devnum, target_endp, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		dev_info(&urb->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			 "usb_clear_halt done: devnum %d endp %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			 urb->dev->devnum, target_endp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int tweak_set_interface_cmd(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct usb_ctrlrequest *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	__u16 alternate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	__u16 interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	req = (struct usb_ctrlrequest *) urb->setup_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	alternate = le16_to_cpu(req->wValue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	interface = le16_to_cpu(req->wIndex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	usbip_dbg_stub_rx("set_interface: inf %u alt %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			  interface, alternate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ret = usb_set_interface(urb->dev, interface, alternate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		dev_err(&urb->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 			"usb_set_interface error: inf %u alt %u ret %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 			interface, alternate, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		dev_info(&urb->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			"usb_set_interface done: inf %u alt %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			interface, alternate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static int tweak_set_configuration_cmd(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	struct stub_priv *priv = (struct stub_priv *) urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	struct stub_device *sdev = priv->sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct usb_ctrlrequest *req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	__u16 config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	req = (struct usb_ctrlrequest *) urb->setup_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	config = le16_to_cpu(req->wValue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	err = usb_set_configuration(sdev->udev, config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (err && err != -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		dev_err(&sdev->udev->dev, "can't set config #%d, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			config, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) static int tweak_reset_device_cmd(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	struct stub_priv *priv = (struct stub_priv *) urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	struct stub_device *sdev = priv->sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	dev_info(&urb->dev->dev, "usb_queue_reset_device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if (usb_lock_device_for_reset(sdev->udev, NULL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		dev_err(&urb->dev->dev, "could not obtain lock to reset device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	usb_reset_device(sdev->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	usb_unlock_device(sdev->udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * clear_halt, set_interface, and set_configuration require special tricks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void tweak_special_requests(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (!urb || !urb->setup_packet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (is_clear_halt_cmd(urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		/* tweak clear_halt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		 tweak_clear_halt_cmd(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	else if (is_set_interface_cmd(urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		/* tweak set_interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		tweak_set_interface_cmd(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	else if (is_set_configuration_cmd(urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		/* tweak set_configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		tweak_set_configuration_cmd(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	else if (is_reset_device_cmd(urb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		tweak_reset_device_cmd(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		usbip_dbg_stub_rx("no need to tweak\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * stub_recv_unlink() unlinks the URB by a call to usb_unlink_urb().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * By unlinking the urb asynchronously, stub_rx can continuously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  * process coming urbs.  Even if the urb is unlinked, its completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  * handler will be called and stub_tx will send a return pdu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  * See also comments about unlinking strategy in vhci_hcd.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int stub_recv_cmd_unlink(struct stub_device *sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 				struct usbip_header *pdu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	struct stub_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	spin_lock_irqsave(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	list_for_each_entry(priv, &sdev->priv_init, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		if (priv->seqnum != pdu->u.cmd_unlink.seqnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		 * This matched urb is not completed yet (i.e., be in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		 * flight in usb hcd hardware/driver). Now we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		 * cancelling it. The unlinking flag means that we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		 * now not going to return the normal result pdu of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		 * submission request, but going to return a result pdu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		 * of the unlink request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		priv->unlinking = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		 * In the case that unlinking flag is on, prev->seqnum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		 * is changed from the seqnum of the cancelling urb to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		 * the seqnum of the unlink request. This will be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		 * to make the result pdu of the unlink request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		priv->seqnum = pdu->base.seqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		spin_unlock_irqrestore(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		 * usb_unlink_urb() is now out of spinlocking to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		 * spinlock recursion since stub_complete() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		 * sometimes called in this context but not in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		 * interrupt context.  If stub_complete() is executed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		 * before we call usb_unlink_urb(), usb_unlink_urb()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		 * will return an error value. In this case, stub_tx
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 		 * will return the result pdu of this unlink request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		 * though submission is completed and actual unlinking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		 * is not executed. OK?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		/* In the above case, urb->status is not -ECONNRESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		 * so a driver in a client host will know the failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		 * of the unlink request ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		for (i = priv->completed_urbs; i < priv->num_urbs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 			ret = usb_unlink_urb(priv->urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			if (ret != -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 				dev_err(&priv->urbs[i]->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 					"failed to unlink %d/%d urb of seqnum %lu, ret %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 					i + 1, priv->num_urbs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 					priv->seqnum, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	usbip_dbg_stub_rx("seqnum %d is not pending\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			  pdu->u.cmd_unlink.seqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	 * The urb of the unlink target is not found in priv_init queue. It was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	 * already completed and its results is/was going to be sent by a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	 * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 * return the completeness of this unlink request to vhci_hcd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	spin_unlock_irqrestore(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	struct usbip_device *ud = &sdev->ud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	int valid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (pdu->base.devid == sdev->devid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		spin_lock_irq(&ud->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if (ud->status == SDEV_ST_USED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			/* A request is valid. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 			valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		spin_unlock_irq(&ud->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	return valid;
^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) static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 					 struct usbip_header *pdu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	struct stub_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct usbip_device *ud = &sdev->ud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	spin_lock_irqsave(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	priv = kmem_cache_zalloc(stub_priv_cache, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	if (!priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		dev_err(&sdev->udev->dev, "alloc stub_priv\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		spin_unlock_irqrestore(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	priv->seqnum = pdu->base.seqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	priv->sdev = sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	 * After a stub_priv is linked to a list_head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	 * our error handler can free allocated data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	list_add_tail(&priv->list, &sdev->priv_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	spin_unlock_irqrestore(&sdev->priv_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	return priv;
^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) static int get_pipe(struct stub_device *sdev, struct usbip_header *pdu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct usb_device *udev = sdev->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	struct usb_host_endpoint *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	struct usb_endpoint_descriptor *epd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	int epnum = pdu->base.ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	int dir = pdu->base.direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (epnum < 0 || epnum > 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		goto err_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (dir == USBIP_DIR_IN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		ep = udev->ep_in[epnum & 0x7f];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		ep = udev->ep_out[epnum & 0x7f];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		goto err_ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	epd = &ep->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	if (usb_endpoint_xfer_control(epd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		if (dir == USBIP_DIR_OUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 			return usb_sndctrlpipe(udev, epnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			return usb_rcvctrlpipe(udev, epnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (usb_endpoint_xfer_bulk(epd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		if (dir == USBIP_DIR_OUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			return usb_sndbulkpipe(udev, epnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			return usb_rcvbulkpipe(udev, epnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (usb_endpoint_xfer_int(epd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		if (dir == USBIP_DIR_OUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 			return usb_sndintpipe(udev, epnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			return usb_rcvintpipe(udev, epnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (usb_endpoint_xfer_isoc(epd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		/* validate number of packets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 		if (pdu->u.cmd_submit.number_of_packets < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		    pdu->u.cmd_submit.number_of_packets >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		    USBIP_MAX_ISO_PACKETS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 			dev_err(&sdev->udev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 				"CMD_SUBMIT: isoc invalid num packets %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 				pdu->u.cmd_submit.number_of_packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		if (dir == USBIP_DIR_OUT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 			return usb_sndisocpipe(udev, epnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 			return usb_rcvisocpipe(udev, epnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) err_ret:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	/* NOT REACHED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	dev_err(&sdev->udev->dev, "CMD_SUBMIT: invalid epnum %d\n", epnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static void masking_bogus_flags(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	int				xfertype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	struct usb_device		*dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	struct usb_host_endpoint	*ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	int				is_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	unsigned int	allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (!urb || urb->hcpriv || !urb->complete)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	dev = urb->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if ((!dev) || (dev->state < USB_STATE_UNAUTHENTICATED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	ep = (usb_pipein(urb->pipe) ? dev->ep_in : dev->ep_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		[usb_pipeendpoint(urb->pipe)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	xfertype = usb_endpoint_type(&ep->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	if (xfertype == USB_ENDPOINT_XFER_CONTROL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		struct usb_ctrlrequest *setup =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 			(struct usb_ctrlrequest *) urb->setup_packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		if (!setup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		is_out = !(setup->bRequestType & USB_DIR_IN) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 			!setup->wLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		is_out = usb_endpoint_dir_out(&ep->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	/* enforce simple/standard policy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	allowed = (URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		   URB_DIR_MASK | URB_FREE_BUFFER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	switch (xfertype) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	case USB_ENDPOINT_XFER_BULK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		if (is_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			allowed |= URB_ZERO_PACKET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	default:			/* all non-iso endpoints */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		if (!is_out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			allowed |= URB_SHORT_NOT_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	case USB_ENDPOINT_XFER_ISOC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		allowed |= URB_ISO_ASAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	urb->transfer_flags &= allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static int stub_recv_xbuff(struct usbip_device *ud, struct stub_priv *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	for (i = 0; i < priv->num_urbs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		ret = usbip_recv_xbuff(ud, priv->urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			break;
^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) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static void stub_recv_cmd_submit(struct stub_device *sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 				 struct usbip_header *pdu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	struct stub_priv *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	struct usbip_device *ud = &sdev->ud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	struct usb_device *udev = sdev->udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct scatterlist *sgl = NULL, *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	void *buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	unsigned long long buf_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	int nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	int num_urbs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	int pipe = get_pipe(sdev, pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	int use_sg = pdu->u.cmd_submit.transfer_flags & URB_DMA_MAP_SG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	int support_sg = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	int np = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (pipe == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	 * Smatch reported the error case where use_sg is true and buf_len is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	 * In this case, It adds SDEV_EVENT_ERROR_MALLOC and stub_priv will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	 * released by stub event handler and connection will be shut down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	priv = stub_priv_alloc(sdev, pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	buf_len = (unsigned long long)pdu->u.cmd_submit.transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (use_sg && !buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		dev_err(&udev->dev, "sg buffer with zero length\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		goto err_malloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	/* allocate urb transfer buffer, if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	if (buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		if (use_sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			sgl = sgl_alloc(buf_len, GFP_KERNEL, &nents);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 			if (!sgl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 				goto err_malloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			/* Check if the server's HCD supports SG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 			if (!udev->bus->sg_tablesize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 				 * If the server's HCD doesn't support SG, break
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 				 * a single SG request into several URBs and map
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 				 * each SG list entry to corresponding URB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 				 * buffer. The previously allocated SG list is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 				 * stored in priv->sgl (If the server's HCD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 				 * support SG, SG list is stored only in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 				 * urb->sg) and it is used as an indicator that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 				 * the server split single SG request into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 				 * several URBs. Later, priv->sgl is used by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 				 * stub_complete() and stub_send_ret_submit() to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 				 * reassemble the divied URBs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 				support_sg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 				num_urbs = nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 				priv->completed_urbs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 				pdu->u.cmd_submit.transfer_flags &=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 								~URB_DMA_MAP_SG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 			buffer = kzalloc(buf_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 				goto err_malloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	/* allocate urb array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	priv->num_urbs = num_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	priv->urbs = kmalloc_array(num_urbs, sizeof(*priv->urbs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	if (!priv->urbs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		goto err_urbs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	/* setup a urb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	if (support_sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 		if (usb_pipeisoc(pipe))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 			np = pdu->u.cmd_submit.number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		priv->urbs[0] = usb_alloc_urb(np, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 		if (!priv->urbs[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 			goto err_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		if (buf_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 			if (use_sg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 				priv->urbs[0]->sg = sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 				priv->urbs[0]->num_sgs = nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 				priv->urbs[0]->transfer_buffer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 				priv->urbs[0]->transfer_buffer = buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		/* copy urb setup packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		priv->urbs[0]->setup_packet = kmemdup(&pdu->u.cmd_submit.setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 					8, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		if (!priv->urbs[0]->setup_packet) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 			usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		usbip_pack_pdu(pdu, priv->urbs[0], USBIP_CMD_SUBMIT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		for_each_sg(sgl, sg, nents, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			priv->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 			/* The URBs which is previously allocated will be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 			 * in stub_device_cleanup_urbs() if error occurs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 			if (!priv->urbs[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 				goto err_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			usbip_pack_pdu(pdu, priv->urbs[i], USBIP_CMD_SUBMIT, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 			priv->urbs[i]->transfer_buffer = sg_virt(sg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			priv->urbs[i]->transfer_buffer_length = sg->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		priv->sgl = sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	for (i = 0; i < num_urbs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		/* set other members from the base header of pdu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		priv->urbs[i]->context = (void *) priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		priv->urbs[i]->dev = udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		priv->urbs[i]->pipe = pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		priv->urbs[i]->complete = stub_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		/* no need to submit an intercepted request, but harmless? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		tweak_special_requests(priv->urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		masking_bogus_flags(priv->urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	if (stub_recv_xbuff(ud, priv) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	if (usbip_recv_iso(ud, priv->urbs[0]) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	/* urb is now ready to submit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	for (i = 0; i < priv->num_urbs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 		ret = usb_submit_urb(priv->urbs[i], GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 			usbip_dbg_stub_rx("submit urb ok, seqnum %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 					pdu->base.seqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 			dev_err(&udev->dev, "submit_urb error, %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 			usbip_dump_header(pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			usbip_dump_urb(priv->urbs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			 * Pessimistic.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			 * This connection will be discarded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	usbip_dbg_stub_rx("Leave\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) err_urb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	kfree(priv->urbs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) err_urbs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	sgl_free(sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) err_malloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) /* recv a pdu */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) static void stub_rx_pdu(struct usbip_device *ud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	struct usbip_header pdu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	struct stub_device *sdev = container_of(ud, struct stub_device, ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	struct device *dev = &sdev->udev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	usbip_dbg_stub_rx("Enter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	memset(&pdu, 0, sizeof(pdu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	/* receive a pdu header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	if (ret != sizeof(pdu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 		dev_err(dev, "recv a header, %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 		return;
^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) 	usbip_header_correct_endian(&pdu, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	if (usbip_dbg_flag_stub_rx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 		usbip_dump_header(&pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	if (!valid_request(sdev, &pdu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		dev_err(dev, "recv invalid request\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	switch (pdu.base.command) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	case USBIP_CMD_UNLINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		stub_recv_cmd_unlink(sdev, &pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	case USBIP_CMD_SUBMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		stub_recv_cmd_submit(sdev, &pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		/* NOTREACHED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		dev_err(dev, "unknown pdu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) int stub_rx_loop(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	struct usbip_device *ud = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	while (!kthread_should_stop()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 		if (usbip_event_happened(ud))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		stub_rx_pdu(ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) }