^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) * Copyright (C) 2015-2016 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Krzysztof Opasiak <k.opasiak@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "usbip_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define DRIVER_DESC "USB/IP Core"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #ifdef CONFIG_USBIP_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned long usbip_debug_flag = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned long usbip_debug_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) EXPORT_SYMBOL_GPL(usbip_debug_flag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) module_param(usbip_debug_flag, ulong, S_IRUGO|S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) MODULE_PARM_DESC(usbip_debug_flag, "debug flags (defined in usbip_common.h)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /* FIXME */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct device_attribute dev_attr_usbip_debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static ssize_t usbip_debug_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return sprintf(buf, "%lx\n", usbip_debug_flag);
^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) static ssize_t usbip_debug_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct device_attribute *attr, const char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (sscanf(buf, "%lx", &usbip_debug_flag) != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) DEVICE_ATTR_RW(usbip_debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static void usbip_dump_buffer(char *buff, int bufflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) print_hex_dump(KERN_DEBUG, "usbip-core", DUMP_PREFIX_OFFSET, 16, 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) buff, bufflen, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static void usbip_dump_pipe(unsigned int p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned char type = usb_pipetype(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) unsigned char ep = usb_pipeendpoint(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned char dev = usb_pipedevice(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) unsigned char dir = usb_pipein(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pr_debug("dev(%d) ep(%d) [%s] ", dev, ep, dir ? "IN" : "OUT");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) case PIPE_ISOCHRONOUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) pr_debug("ISO\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) case PIPE_INTERRUPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) pr_debug("INT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) case PIPE_CONTROL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) pr_debug("CTRL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) case PIPE_BULK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pr_debug("BULK\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pr_debug("ERR\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static void usbip_dump_usb_device(struct usb_device *udev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct device *dev = &udev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) dev_dbg(dev, " devnum(%d) devpath(%s) usb speed(%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) udev->devnum, udev->devpath, usb_speed_string(udev->speed));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pr_debug("tt hub ttport %d\n", udev->ttport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) dev_dbg(dev, " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) for (i = 0; i < 16; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) pr_debug(" %2u", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) dev_dbg(dev, " toggle0(IN) :");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) for (i = 0; i < 16; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pr_debug(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) dev_dbg(dev, " toggle1(OUT):");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) for (i = 0; i < 16; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) pr_debug(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) dev_dbg(dev, " epmaxp_in :");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) for (i = 0; i < 16; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (udev->ep_in[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pr_debug(" %2u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) dev_dbg(dev, " epmaxp_out :");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) for (i = 0; i < 16; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (udev->ep_out[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) pr_debug(" %2u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) pr_debug("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) dev_dbg(dev, "parent %s, bus %s\n", dev_name(&udev->parent->dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) udev->bus->bus_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) dev_dbg(dev, "have_langid %d, string_langid %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) udev->have_langid, udev->string_langid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) dev_dbg(dev, "maxchild %d\n", udev->maxchild);
^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 void usbip_dump_request_type(__u8 rt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) switch (rt & USB_RECIP_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) case USB_RECIP_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pr_debug("DEVICE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) case USB_RECIP_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) pr_debug("INTERF");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) case USB_RECIP_ENDPOINT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) pr_debug("ENDPOI");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) case USB_RECIP_OTHER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) pr_debug("OTHER ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) pr_debug("------");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (!cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) pr_debug(" : null pointer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) pr_debug(" ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) pr_debug("bRequestType(%02X) bRequest(%02X) wValue(%04X) wIndex(%04X) wLength(%04X) ",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) cmd->bRequestType, cmd->bRequest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) cmd->wValue, cmd->wIndex, cmd->wLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) pr_debug("\n ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) pr_debug("STANDARD ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) switch (cmd->bRequest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) case USB_REQ_GET_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) pr_debug("GET_STATUS\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) case USB_REQ_CLEAR_FEATURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) pr_debug("CLEAR_FEAT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) case USB_REQ_SET_FEATURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) pr_debug("SET_FEAT\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) case USB_REQ_SET_ADDRESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) pr_debug("SET_ADDRRS\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) case USB_REQ_GET_DESCRIPTOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) pr_debug("GET_DESCRI\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) case USB_REQ_SET_DESCRIPTOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) pr_debug("SET_DESCRI\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) case USB_REQ_GET_CONFIGURATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) pr_debug("GET_CONFIG\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) case USB_REQ_SET_CONFIGURATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pr_debug("SET_CONFIG\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) case USB_REQ_GET_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) pr_debug("GET_INTERF\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) case USB_REQ_SET_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) pr_debug("SET_INTERF\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) case USB_REQ_SYNCH_FRAME:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) pr_debug("SYNC_FRAME\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) pr_debug("REQ(%02X)\n", cmd->bRequest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) usbip_dump_request_type(cmd->bRequestType);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) pr_debug("CLASS\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) pr_debug("VENDOR\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) pr_debug("RESERVED\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) void usbip_dump_urb(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (!urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) pr_debug("urb: null pointer!!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!urb->dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) pr_debug("urb->dev: null pointer!!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) dev = &urb->dev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) usbip_dump_usb_device(urb->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) dev_dbg(dev, " pipe :%08x ", urb->pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) usbip_dump_pipe(urb->pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) dev_dbg(dev, " status :%d\n", urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) dev_dbg(dev, " transfer_buffer_length:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) urb->transfer_buffer_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) usbip_dump_usb_ctrlrequest(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) (struct usb_ctrlrequest *)urb->setup_packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) dev_dbg(dev, " interval :%d\n", urb->interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) dev_dbg(dev, " error_count :%d\n", urb->error_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) EXPORT_SYMBOL_GPL(usbip_dump_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) void usbip_dump_header(struct usbip_header *pdu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) pr_debug("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) pdu->base.command,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) pdu->base.seqnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) pdu->base.devid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) pdu->base.direction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) pdu->base.ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) switch (pdu->base.command) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case USBIP_CMD_SUBMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) pr_debug("USBIP_CMD_SUBMIT: x_flags %u x_len %u sf %u #p %d iv %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) pdu->u.cmd_submit.transfer_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) pdu->u.cmd_submit.transfer_buffer_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) pdu->u.cmd_submit.start_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) pdu->u.cmd_submit.number_of_packets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) pdu->u.cmd_submit.interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) case USBIP_CMD_UNLINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) pr_debug("USBIP_CMD_UNLINK: seq %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) pdu->u.cmd_unlink.seqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) case USBIP_RET_SUBMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) pr_debug("USBIP_RET_SUBMIT: st %d al %u sf %d #p %d ec %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) pdu->u.ret_submit.status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) pdu->u.ret_submit.actual_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) pdu->u.ret_submit.start_frame,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) pdu->u.ret_submit.number_of_packets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) pdu->u.ret_submit.error_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) case USBIP_RET_UNLINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) pr_debug("USBIP_RET_UNLINK: status %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) pdu->u.ret_unlink.status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /* NOT REACHED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) pr_err("unknown command\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) EXPORT_SYMBOL_GPL(usbip_dump_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* Receive data over TCP/IP. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) int usbip_recv(struct socket *sock, void *buf, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct kvec iov = {.iov_base = buf, .iov_len = size};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct msghdr msg = {.msg_flags = MSG_NOSIGNAL};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int total = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (!sock || !buf || !size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) iov_iter_kvec(&msg.msg_iter, READ, &iov, 1, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) usbip_dbg_xmit("enter\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) sock->sk->sk_allocation = GFP_NOIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) result = sock_recvmsg(sock, &msg, MSG_WAITALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (result <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) total += result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) } while (msg_data_left(&msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (usbip_dbg_flag_xmit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!in_interrupt())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) pr_debug("%-10s:", current->comm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) pr_debug("interrupt :");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) pr_debug("receiving....\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) usbip_dump_buffer(buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) pr_debug("received, osize %d ret %d size %zd total %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) size, result, msg_data_left(&msg), total);
^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) return total;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) EXPORT_SYMBOL_GPL(usbip_recv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /* there may be more cases to tweak the flags. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static unsigned int tweak_transfer_flags(unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) flags &= ~URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return flags;
^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) static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int pack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * Some members are not still implemented in usbip. I hope this issue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * will be discussed when usbip is ported to other operating systems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (pack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) spdu->transfer_flags =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) tweak_transfer_flags(urb->transfer_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) spdu->transfer_buffer_length = urb->transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) spdu->start_frame = urb->start_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) spdu->number_of_packets = urb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) spdu->interval = urb->interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) urb->transfer_flags = spdu->transfer_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) urb->transfer_buffer_length = spdu->transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) urb->start_frame = spdu->start_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) urb->number_of_packets = spdu->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) urb->interval = spdu->interval;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) int pack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) if (pack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) rpdu->status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) rpdu->actual_length = urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) rpdu->start_frame = urb->start_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) rpdu->number_of_packets = urb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) rpdu->error_count = urb->error_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) urb->status = rpdu->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) urb->actual_length = rpdu->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) urb->start_frame = rpdu->start_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) urb->number_of_packets = rpdu->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) urb->error_count = rpdu->error_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) int pack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) case USBIP_CMD_SUBMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) usbip_pack_cmd_submit(pdu, urb, pack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) case USBIP_RET_SUBMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) usbip_pack_ret_submit(pdu, urb, pack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /* NOT REACHED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) pr_err("unknown command\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) EXPORT_SYMBOL_GPL(usbip_pack_pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static void correct_endian_basic(struct usbip_header_basic *base, int send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (send) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) base->command = cpu_to_be32(base->command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) base->seqnum = cpu_to_be32(base->seqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) base->devid = cpu_to_be32(base->devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) base->direction = cpu_to_be32(base->direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) base->ep = cpu_to_be32(base->ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) base->command = be32_to_cpu(base->command);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) base->seqnum = be32_to_cpu(base->seqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) base->devid = be32_to_cpu(base->devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) base->direction = be32_to_cpu(base->direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) base->ep = be32_to_cpu(base->ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (send) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) cpu_to_be32s(&pdu->transfer_buffer_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) cpu_to_be32s(&pdu->start_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) cpu_to_be32s(&pdu->number_of_packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) cpu_to_be32s(&pdu->interval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) be32_to_cpus(&pdu->transfer_buffer_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) be32_to_cpus(&pdu->start_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) be32_to_cpus(&pdu->number_of_packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) be32_to_cpus(&pdu->interval);
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) int send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (send) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) cpu_to_be32s(&pdu->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) cpu_to_be32s(&pdu->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) cpu_to_be32s(&pdu->start_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) cpu_to_be32s(&pdu->number_of_packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) cpu_to_be32s(&pdu->error_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) be32_to_cpus(&pdu->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) be32_to_cpus(&pdu->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) be32_to_cpus(&pdu->start_frame);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) be32_to_cpus(&pdu->number_of_packets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) be32_to_cpus(&pdu->error_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) int send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) pdu->seqnum = cpu_to_be32(pdu->seqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) pdu->seqnum = be32_to_cpu(pdu->seqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) cpu_to_be32s(&pdu->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) be32_to_cpus(&pdu->status);
^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) void usbip_header_correct_endian(struct usbip_header *pdu, int send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) __u32 cmd = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) cmd = pdu->base.command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) correct_endian_basic(&pdu->base, send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (!send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) cmd = pdu->base.command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) case USBIP_CMD_SUBMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) case USBIP_RET_SUBMIT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) correct_endian_ret_submit(&pdu->u.ret_submit, send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) case USBIP_CMD_UNLINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) case USBIP_RET_UNLINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) /* NOT REACHED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) pr_err("unknown command\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) static void usbip_iso_packet_correct_endian(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) struct usbip_iso_packet_descriptor *iso, int send)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) /* does not need all members. but copy all simply. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (send) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) iso->offset = cpu_to_be32(iso->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) iso->length = cpu_to_be32(iso->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) iso->status = cpu_to_be32(iso->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) iso->actual_length = cpu_to_be32(iso->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) iso->offset = be32_to_cpu(iso->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) iso->length = be32_to_cpu(iso->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) iso->status = be32_to_cpu(iso->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) iso->actual_length = be32_to_cpu(iso->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^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) static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) struct usb_iso_packet_descriptor *uiso, int pack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (pack) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) iso->offset = uiso->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) iso->length = uiso->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) iso->status = uiso->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) iso->actual_length = uiso->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) uiso->offset = iso->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) uiso->length = iso->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) uiso->status = iso->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) uiso->actual_length = iso->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) /* must free buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) struct usbip_iso_packet_descriptor*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) struct usbip_iso_packet_descriptor *iso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) int np = urb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ssize_t size = np * sizeof(*iso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) iso = kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (!iso)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) for (i = 0; i < np; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) usbip_iso_packet_correct_endian(&iso[i], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) *bufflen = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) return iso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /* some members of urb must be substituted before. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) void *buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) struct usbip_iso_packet_descriptor *iso;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) int np = urb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) int size = np * sizeof(*iso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) int total_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (!usb_pipeisoc(urb->pipe))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) /* my Bluetooth dongle gets ISO URBs which are np = 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) if (np == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) buff = kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (!buff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) ret = usbip_recv(ud->tcp_socket, buff, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (ret != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) kfree(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) return -EPIPE;
^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) iso = (struct usbip_iso_packet_descriptor *) buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) for (i = 0; i < np; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) usbip_iso_packet_correct_endian(&iso[i], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) total_length += urb->iso_frame_desc[i].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) kfree(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) if (total_length != urb->actual_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) dev_err(&urb->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) "total length of iso packets %d not equal to actual length of buffer %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) total_length, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) EXPORT_SYMBOL_GPL(usbip_recv_iso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * This functions restores the padding which was removed for optimizing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * the bandwidth during transfer over tcp/ip
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) * buffer and iso packets need to be stored and be in propeper endian in urb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) * before calling this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) void usbip_pad_iso(struct usbip_device *ud, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) int np = urb->number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) int actualoffset = urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (!usb_pipeisoc(urb->pipe))
^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) /* if no packets or length of data is 0, then nothing to unpack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) if (np == 0 || urb->actual_length == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * if actual_length is transfer_buffer_length then no padding is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * present.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (urb->actual_length == urb->transfer_buffer_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * loop over all packets from last to first (to prevent overwriting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * memory when padding) and move them into the proper place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) for (i = np-1; i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) actualoffset -= urb->iso_frame_desc[i].actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) memmove(urb->transfer_buffer + urb->iso_frame_desc[i].offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) urb->transfer_buffer + actualoffset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) urb->iso_frame_desc[i].actual_length);
^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) EXPORT_SYMBOL_GPL(usbip_pad_iso);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) /* some members of urb must be substituted before. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) int recv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) int copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) /* the direction of urb must be OUT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (usb_pipein(urb->pipe))
^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) size = urb->transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) /* the direction of urb must be IN. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) if (usb_pipeout(urb->pipe))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) size = urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) /* no need to recv xbuff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) if (!(size > 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (size > urb->transfer_buffer_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) /* should not happen, probably malicious packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) if (urb->num_sgs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) copy = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) for_each_sg(urb->sg, sg, urb->num_sgs, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) int recv_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) if (copy < sg->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) recv_size = copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) recv_size = sg->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) recv = usbip_recv(ud->tcp_socket, sg_virt(sg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) recv_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) if (recv != recv_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) copy -= recv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) ret += recv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) if (!copy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) if (ret != size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (ret != size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) if (ud->side == USBIP_STUB || ud->side == USBIP_VUDC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) return -EPIPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) static int __init usbip_core_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) return usbip_init_eh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) static void __exit usbip_core_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) usbip_finish_eh();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) module_init(usbip_core_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) module_exit(usbip_core_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) MODULE_AUTHOR(DRIVER_AUTHOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) MODULE_LICENSE("GPL");