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