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