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