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