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