Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Staging: USB/IP: add client driver

This adds the USB IP client driver

Brian Merrell cleaned up a lot of this code and submitted it for
inclusion. Greg also did a lot of cleanup.

Signed-off-by: Brian G. Merrell <bgmerrell@novell.com>
Cc: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Takahiro Hirofuchi and committed by
Greg Kroah-Hartman
04679b34 05a1f28e

+2171
+11
drivers/staging/usbip/Kconfig
··· 12 12 module will be called usbip_common_mod. 13 13 14 14 If unsure, say N. 15 + 16 + config USB_IP_VHCI_HCD 17 + tristate "USB IP client driver" 18 + depends on USB_IP_COMMON 19 + default N 20 + ---help--- 21 + This enables the USB IP host controller driver which will 22 + run on the client machine. 23 + 24 + To compile this driver as a module, choose M here: the 25 + module will be called vhci_hcd.
+3
drivers/staging/usbip/Makefile
··· 1 1 obj-$(CONFIG_USB_IP_COMMON) += usbip_common_mod.o 2 2 usbip_common_mod-objs := usbip_common.o usbip_event.o 3 3 4 + obj-$(CONFIG_USB_IP_VHCI_HCD) += vhci-hcd.o 5 + vhci-hcd-objs := vhci_sysfs.o vhci_tx.o vhci_rx.o vhci_hcd.o 6 + 4 7 ifeq ($(CONFIG_USB_DEBUG),y) 5 8 EXTRA_CFLAGS += -DDEBUG 6 9 endif
+142
drivers/staging/usbip/vhci.h
··· 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 + 20 + #include <linux/platform_device.h> 21 + #include "../../usb/core/hcd.h" 22 + 23 + 24 + struct vhci_device { 25 + struct usb_device *udev; 26 + 27 + /* 28 + * devid specifies a remote usb device uniquely instead 29 + * of combination of busnum and devnum. 30 + */ 31 + __u32 devid; 32 + 33 + /* speed of a remote device */ 34 + enum usb_device_speed speed; 35 + 36 + /* vhci root-hub port to which this device is attached */ 37 + __u32 rhport; 38 + 39 + struct usbip_device ud; 40 + 41 + 42 + /* lock for the below link lists */ 43 + spinlock_t priv_lock; 44 + 45 + /* vhci_priv is linked to one of them. */ 46 + struct list_head priv_tx; 47 + struct list_head priv_rx; 48 + 49 + /* vhci_unlink is linked to one of them */ 50 + struct list_head unlink_tx; 51 + struct list_head unlink_rx; 52 + 53 + /* vhci_tx thread sleeps for this queue */ 54 + wait_queue_head_t waitq_tx; 55 + }; 56 + 57 + 58 + /* urb->hcpriv, use container_of() */ 59 + struct vhci_priv { 60 + unsigned long seqnum; 61 + struct list_head list; 62 + 63 + struct vhci_device *vdev; 64 + struct urb *urb; 65 + }; 66 + 67 + 68 + struct vhci_unlink { 69 + /* seqnum of this request */ 70 + unsigned long seqnum; 71 + 72 + struct list_head list; 73 + 74 + /* seqnum of the unlink target */ 75 + unsigned long unlink_seqnum; 76 + }; 77 + 78 + /* 79 + * The number of ports is less than 16 ? 80 + * USB_MAXCHILDREN is statically defined to 16 in usb.h. Its maximum value 81 + * would be 31 because the event_bits[1] of struct usb_hub is defined as 82 + * unsigned long in hub.h 83 + */ 84 + #define VHCI_NPORTS 8 85 + 86 + /* for usb_bus.hcpriv */ 87 + struct vhci_hcd { 88 + spinlock_t lock; 89 + 90 + u32 port_status[VHCI_NPORTS]; 91 + 92 + unsigned resuming:1; 93 + unsigned long re_timeout; 94 + 95 + atomic_t seqnum; 96 + 97 + /* 98 + * NOTE: 99 + * wIndex shows the port number and begins from 1. 100 + * But, the index of this array begins from 0. 101 + */ 102 + struct vhci_device vdev[VHCI_NPORTS]; 103 + 104 + /* vhci_device which has not been assiged its address yet */ 105 + int pending_port; 106 + }; 107 + 108 + 109 + extern struct vhci_hcd *the_controller; 110 + extern struct attribute_group dev_attr_group; 111 + 112 + 113 + /*-------------------------------------------------------------------------*/ 114 + /* prototype declaration */ 115 + 116 + /* vhci_hcd.c */ 117 + void rh_port_connect(int rhport, enum usb_device_speed speed); 118 + void rh_port_disconnect(int rhport); 119 + void vhci_rx_loop(struct usbip_task *ut); 120 + void vhci_tx_loop(struct usbip_task *ut); 121 + 122 + #define hardware (&the_controller->pdev.dev) 123 + 124 + static inline struct vhci_device *port_to_vdev(__u32 port) 125 + { 126 + return &the_controller->vdev[port]; 127 + } 128 + 129 + static inline struct vhci_hcd *hcd_to_vhci(struct usb_hcd *hcd) 130 + { 131 + return (struct vhci_hcd *) (hcd->hcd_priv); 132 + } 133 + 134 + static inline struct usb_hcd *vhci_to_hcd(struct vhci_hcd *vhci) 135 + { 136 + return container_of((void *) vhci, struct usb_hcd, hcd_priv); 137 + } 138 + 139 + static inline struct device *vhci_dev(struct vhci_hcd *vhci) 140 + { 141 + return vhci_to_hcd(vhci)->self.controller; 142 + }
+1275
drivers/staging/usbip/vhci_hcd.c
··· 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 + 20 + 21 + #include "usbip_common.h" 22 + #include "vhci.h" 23 + 24 + #define DRIVER_VERSION "1.0" 25 + #define DRIVER_AUTHOR "Takahiro Hirofuchi" 26 + #define DRIVER_DESC "Virtual Host Controller Interface Driver for USB/IP" 27 + #define DRIVER_LICENCE "GPL" 28 + MODULE_AUTHOR(DRIVER_AUTHOR); 29 + MODULE_DESCRIPTION(DRIVER_DESC); 30 + MODULE_LICENSE(DRIVER_LICENCE); 31 + 32 + 33 + 34 + /* 35 + * TODO 36 + * - update root hub emulation 37 + * - move the emulation code to userland ? 38 + * porting to other operating systems 39 + * minimize kernel code 40 + * - add suspend/resume code 41 + * - clean up everything 42 + */ 43 + 44 + 45 + /* See usb gadget dummy hcd */ 46 + 47 + 48 + static int vhci_hub_status(struct usb_hcd *hcd, char *buff); 49 + static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, 50 + u16 wIndex, char *buff, u16 wLength); 51 + static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, 52 + gfp_t mem_flags); 53 + static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status); 54 + static int vhci_start(struct usb_hcd *vhci_hcd); 55 + static void vhci_stop(struct usb_hcd *hcd); 56 + static int vhci_get_frame_number(struct usb_hcd *hcd); 57 + 58 + static const char driver_name[] = "vhci_hcd"; 59 + static const char driver_desc[] = "USB/IP Virtual Host Contoroller"; 60 + 61 + struct vhci_hcd *the_controller; 62 + 63 + static const char *bit_desc[] = { 64 + "CONNECTION", /*0*/ 65 + "ENABLE", /*1*/ 66 + "SUSPEND", /*2*/ 67 + "OVER_CURRENT", /*3*/ 68 + "RESET", /*4*/ 69 + "R5", /*5*/ 70 + "R6", /*6*/ 71 + "R7", /*7*/ 72 + "POWER", /*8*/ 73 + "LOWSPEED", /*9*/ 74 + "HIGHSPEED", /*10*/ 75 + "PORT_TEST", /*11*/ 76 + "INDICATOR", /*12*/ 77 + "R13", /*13*/ 78 + "R14", /*14*/ 79 + "R15", /*15*/ 80 + "C_CONNECTION", /*16*/ 81 + "C_ENABLE", /*17*/ 82 + "C_SUSPEND", /*18*/ 83 + "C_OVER_CURRENT", /*19*/ 84 + "C_RESET", /*20*/ 85 + "R21", /*21*/ 86 + "R22", /*22*/ 87 + "R23", /*23*/ 88 + "R24", /*24*/ 89 + "R25", /*25*/ 90 + "R26", /*26*/ 91 + "R27", /*27*/ 92 + "R28", /*28*/ 93 + "R29", /*29*/ 94 + "R30", /*30*/ 95 + "R31", /*31*/ 96 + }; 97 + 98 + 99 + static void dump_port_status(u32 status) 100 + { 101 + int i = 0; 102 + 103 + printk(KERN_DEBUG "status %08x:", status); 104 + for (i = 0; i < 32; i++) { 105 + if (status & (1 << i)) 106 + printk(" %s", bit_desc[i]); 107 + } 108 + 109 + printk("\n"); 110 + } 111 + 112 + 113 + 114 + void rh_port_connect(int rhport, enum usb_device_speed speed) 115 + { 116 + unsigned long flags; 117 + 118 + dbg_vhci_rh("rh_port_connect %d\n", rhport); 119 + 120 + spin_lock_irqsave(&the_controller->lock, flags); 121 + 122 + the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION 123 + | (1 << USB_PORT_FEAT_C_CONNECTION); 124 + 125 + switch (speed) { 126 + case USB_SPEED_HIGH: 127 + the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED; 128 + break; 129 + case USB_SPEED_LOW: 130 + the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED; 131 + break; 132 + default: 133 + break; 134 + } 135 + 136 + /* spin_lock(&the_controller->vdev[rhport].ud.lock); 137 + * the_controller->vdev[rhport].ud.status = VDEV_CONNECT; 138 + * spin_unlock(&the_controller->vdev[rhport].ud.lock); */ 139 + 140 + the_controller->pending_port = rhport; 141 + 142 + spin_unlock_irqrestore(&the_controller->lock, flags); 143 + 144 + usb_hcd_poll_rh_status(vhci_to_hcd(the_controller)); 145 + } 146 + 147 + void rh_port_disconnect(int rhport) 148 + { 149 + unsigned long flags; 150 + 151 + dbg_vhci_rh("rh_port_disconnect %d\n", rhport); 152 + 153 + spin_lock_irqsave(&the_controller->lock, flags); 154 + /* stop_activity(dum, driver); */ 155 + the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION; 156 + the_controller->port_status[rhport] |= 157 + (1 << USB_PORT_FEAT_C_CONNECTION); 158 + 159 + 160 + /* not yet complete the disconnection 161 + * spin_lock(&vdev->ud.lock); 162 + * vdev->ud.status = VHC_ST_DISCONNECT; 163 + * spin_unlock(&vdev->ud.lock); */ 164 + 165 + spin_unlock_irqrestore(&the_controller->lock, flags); 166 + } 167 + 168 + 169 + 170 + /*----------------------------------------------------------------------*/ 171 + 172 + #define PORT_C_MASK \ 173 + ((USB_PORT_STAT_C_CONNECTION \ 174 + | USB_PORT_STAT_C_ENABLE \ 175 + | USB_PORT_STAT_C_SUSPEND \ 176 + | USB_PORT_STAT_C_OVERCURRENT \ 177 + | USB_PORT_STAT_C_RESET) << 16) 178 + 179 + /* 180 + * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without 181 + * suspend/resume support. But, it is modified to provide multiple ports. 182 + * 183 + * @buf: a bitmap to show which port status has been changed. 184 + * bit 0: reserved or used for another purpose? 185 + * bit 1: the status of port 0 has been changed. 186 + * bit 2: the status of port 1 has been changed. 187 + * ... 188 + * bit 7: the status of port 6 has been changed. 189 + * bit 8: the status of port 7 has been changed. 190 + * ... 191 + * bit 15: the status of port 14 has been changed. 192 + * 193 + * So, the maximum number of ports is 31 ( port 0 to port 30) ? 194 + * 195 + * The return value is the actual transfered length in byte. If nothing has 196 + * been changed, return 0. In the case that the number of ports is less than or 197 + * equal to 6 (VHCI_NPORTS==7), return 1. 198 + * 199 + */ 200 + static int vhci_hub_status(struct usb_hcd *hcd, char *buf) 201 + { 202 + struct vhci_hcd *vhci; 203 + unsigned long flags; 204 + int retval = 0; 205 + 206 + /* the enough buffer is allocated according to USB_MAXCHILDREN */ 207 + unsigned long *event_bits = (unsigned long *) buf; 208 + int rhport; 209 + int changed = 0; 210 + 211 + 212 + *event_bits = 0; 213 + 214 + vhci = hcd_to_vhci(hcd); 215 + 216 + spin_lock_irqsave(&vhci->lock, flags); 217 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 218 + dbg_vhci_rh("hw accessible flag in on?\n"); 219 + goto done; 220 + } 221 + 222 + /* check pseudo status register for each port */ 223 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { 224 + if ((vhci->port_status[rhport] & PORT_C_MASK)) { 225 + /* The status of a port has been changed, */ 226 + dbg_vhci_rh("port %d is changed\n", rhport); 227 + 228 + *event_bits |= 1 << (rhport + 1); 229 + changed = 1; 230 + } 231 + } 232 + 233 + uinfo("changed %d\n", changed); 234 + 235 + if (hcd->state == HC_STATE_SUSPENDED) 236 + usb_hcd_resume_root_hub(hcd); 237 + 238 + if (changed) 239 + retval = 1 + (VHCI_NPORTS / 8); 240 + else 241 + retval = 0; 242 + 243 + done: 244 + spin_unlock_irqrestore(&vhci->lock, flags); 245 + return retval; 246 + } 247 + 248 + /* See hub_configure in hub.c */ 249 + static inline void hub_descriptor(struct usb_hub_descriptor *desc) 250 + { 251 + memset(desc, 0, sizeof(*desc)); 252 + desc->bDescriptorType = 0x29; 253 + desc->bDescLength = 9; 254 + desc->wHubCharacteristics = (__force __u16) 255 + (__constant_cpu_to_le16(0x0001)); 256 + desc->bNbrPorts = VHCI_NPORTS; 257 + desc->bitmap[0] = 0xff; 258 + desc->bitmap[1] = 0xff; 259 + } 260 + 261 + static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue, 262 + u16 wIndex, char *buf, u16 wLength) 263 + { 264 + struct vhci_hcd *dum; 265 + int retval = 0; 266 + unsigned long flags; 267 + int rhport; 268 + 269 + u32 prev_port_status[VHCI_NPORTS]; 270 + 271 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) 272 + return -ETIMEDOUT; 273 + 274 + /* 275 + * NOTE: 276 + * wIndex shows the port number and begins from 1. 277 + */ 278 + dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue, 279 + wIndex); 280 + if (wIndex > VHCI_NPORTS) 281 + printk(KERN_ERR "%s: invalid port number %d\n", __func__, wIndex); 282 + rhport = ((__u8)(wIndex & 0x00ff)) - 1; 283 + 284 + dum = hcd_to_vhci(hcd); 285 + 286 + spin_lock_irqsave(&dum->lock, flags); 287 + 288 + /* store old status and compare now and old later */ 289 + if (dbg_flag_vhci_rh) { 290 + int i = 0; 291 + for (i = 0; i < VHCI_NPORTS; i++) 292 + prev_port_status[i] = dum->port_status[i]; 293 + } 294 + 295 + switch (typeReq) { 296 + case ClearHubFeature: 297 + dbg_vhci_rh(" ClearHubFeature\n"); 298 + break; 299 + case ClearPortFeature: 300 + switch (wValue) { 301 + case USB_PORT_FEAT_SUSPEND: 302 + if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) { 303 + /* 20msec signaling */ 304 + dum->resuming = 1; 305 + dum->re_timeout = 306 + jiffies + msecs_to_jiffies(20); 307 + } 308 + break; 309 + case USB_PORT_FEAT_POWER: 310 + dbg_vhci_rh(" ClearPortFeature: USB_PORT_FEAT_POWER\n"); 311 + dum->port_status[rhport] = 0; 312 + /* dum->address = 0; */ 313 + /* dum->hdev = 0; */ 314 + dum->resuming = 0; 315 + break; 316 + case USB_PORT_FEAT_C_RESET: 317 + dbg_vhci_rh(" ClearPortFeature: " 318 + "USB_PORT_FEAT_C_RESET\n"); 319 + switch (dum->vdev[rhport].speed) { 320 + case USB_SPEED_HIGH: 321 + dum->port_status[rhport] |= 322 + USB_PORT_STAT_HIGH_SPEED; 323 + break; 324 + case USB_SPEED_LOW: 325 + dum->port_status[rhport] |= 326 + USB_PORT_STAT_LOW_SPEED; 327 + break; 328 + default: 329 + break; 330 + } 331 + default: 332 + dbg_vhci_rh(" ClearPortFeature: default %x\n", wValue); 333 + dum->port_status[rhport] &= ~(1 << wValue); 334 + } 335 + break; 336 + case GetHubDescriptor: 337 + dbg_vhci_rh(" GetHubDescriptor\n"); 338 + hub_descriptor((struct usb_hub_descriptor *) buf); 339 + break; 340 + case GetHubStatus: 341 + dbg_vhci_rh(" GetHubStatus\n"); 342 + *(__le32 *) buf = __constant_cpu_to_le32(0); 343 + break; 344 + case GetPortStatus: 345 + dbg_vhci_rh(" GetPortStatus port %x\n", wIndex); 346 + if (wIndex > VHCI_NPORTS || wIndex < 1) { 347 + printk(KERN_ERR "%s: invalid port number %d\n", 348 + __func__, wIndex); 349 + retval = -EPIPE; 350 + } 351 + 352 + /* we do no care of resume. */ 353 + 354 + /* whoever resets or resumes must GetPortStatus to 355 + * complete it!! 356 + * */ 357 + if (dum->resuming && time_after(jiffies, dum->re_timeout)) { 358 + printk(KERN_ERR "%s: not yet\n", __func__); 359 + dum->port_status[rhport] |= 360 + (1 << USB_PORT_FEAT_C_SUSPEND); 361 + dum->port_status[rhport] &= 362 + ~(1 << USB_PORT_FEAT_SUSPEND); 363 + dum->resuming = 0; 364 + dum->re_timeout = 0; 365 + /* if (dum->driver && dum->driver->resume) { 366 + * spin_unlock (&dum->lock); 367 + * dum->driver->resume (&dum->gadget); 368 + * spin_lock (&dum->lock); 369 + * } */ 370 + } 371 + 372 + if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) != 373 + 0 && time_after(jiffies, dum->re_timeout)) { 374 + dum->port_status[rhport] |= 375 + (1 << USB_PORT_FEAT_C_RESET); 376 + dum->port_status[rhport] &= 377 + ~(1 << USB_PORT_FEAT_RESET); 378 + dum->re_timeout = 0; 379 + 380 + if (dum->vdev[rhport].ud.status == 381 + VDEV_ST_NOTASSIGNED) { 382 + dbg_vhci_rh(" enable rhport %d (status %u)\n", 383 + rhport, 384 + dum->vdev[rhport].ud.status); 385 + dum->port_status[rhport] |= 386 + USB_PORT_STAT_ENABLE; 387 + } 388 + #if 0 389 + if (dum->driver) { 390 + 391 + dum->port_status[rhport] |= 392 + USB_PORT_STAT_ENABLE; 393 + /* give it the best speed we agree on */ 394 + dum->gadget.speed = dum->driver->speed; 395 + dum->gadget.ep0->maxpacket = 64; 396 + switch (dum->gadget.speed) { 397 + case USB_SPEED_HIGH: 398 + dum->port_status[rhport] |= 399 + USB_PORT_STAT_HIGH_SPEED; 400 + break; 401 + case USB_SPEED_LOW: 402 + dum->gadget.ep0->maxpacket = 8; 403 + dum->port_status[rhport] |= 404 + USB_PORT_STAT_LOW_SPEED; 405 + break; 406 + default: 407 + dum->gadget.speed = USB_SPEED_FULL; 408 + break; 409 + } 410 + } 411 + #endif 412 + 413 + } 414 + ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]); 415 + ((u16 *) buf)[1] = 416 + cpu_to_le16(dum->port_status[rhport] >> 16); 417 + 418 + dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0], 419 + ((u16 *)buf)[1]); 420 + break; 421 + case SetHubFeature: 422 + dbg_vhci_rh(" SetHubFeature\n"); 423 + retval = -EPIPE; 424 + break; 425 + case SetPortFeature: 426 + switch (wValue) { 427 + case USB_PORT_FEAT_SUSPEND: 428 + dbg_vhci_rh(" SetPortFeature: " 429 + "USB_PORT_FEAT_SUSPEND\n"); 430 + printk(KERN_ERR "%s: not yet\n", __func__); 431 + #if 0 432 + dum->port_status[rhport] |= 433 + (1 << USB_PORT_FEAT_SUSPEND); 434 + if (dum->driver->suspend) { 435 + spin_unlock(&dum->lock); 436 + dum->driver->suspend(&dum->gadget); 437 + spin_lock(&dum->lock); 438 + } 439 + #endif 440 + break; 441 + case USB_PORT_FEAT_RESET: 442 + dbg_vhci_rh(" SetPortFeature: USB_PORT_FEAT_RESET\n"); 443 + /* if it's already running, disconnect first */ 444 + if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) { 445 + dum->port_status[rhport] &= 446 + ~(USB_PORT_STAT_ENABLE | 447 + USB_PORT_STAT_LOW_SPEED | 448 + USB_PORT_STAT_HIGH_SPEED); 449 + #if 0 450 + if (dum->driver) { 451 + dev_dbg(hardware, "disconnect\n"); 452 + stop_activity(dum, dum->driver); 453 + } 454 + #endif 455 + 456 + /* FIXME test that code path! */ 457 + } 458 + /* 50msec reset signaling */ 459 + dum->re_timeout = jiffies + msecs_to_jiffies(50); 460 + 461 + /* FALLTHROUGH */ 462 + default: 463 + dbg_vhci_rh(" SetPortFeature: default %d\n", wValue); 464 + dum->port_status[rhport] |= (1 << wValue); 465 + } 466 + break; 467 + 468 + default: 469 + printk(KERN_ERR "%s: default: no such request\n", __func__); 470 + /* dev_dbg (hardware, 471 + * "hub control req%04x v%04x i%04x l%d\n", 472 + * typeReq, wValue, wIndex, wLength); */ 473 + 474 + /* "protocol stall" on error */ 475 + retval = -EPIPE; 476 + } 477 + 478 + if (dbg_flag_vhci_rh) { 479 + printk(KERN_DEBUG "port %d\n", rhport); 480 + dump_port_status(prev_port_status[rhport]); 481 + dump_port_status(dum->port_status[rhport]); 482 + } 483 + dbg_vhci_rh(" bye\n"); 484 + 485 + spin_unlock_irqrestore(&dum->lock, flags); 486 + 487 + return retval; 488 + } 489 + 490 + 491 + 492 + /*----------------------------------------------------------------------*/ 493 + 494 + static struct vhci_device *get_vdev(struct usb_device *udev) 495 + { 496 + int i; 497 + 498 + if (!udev) 499 + return NULL; 500 + 501 + for (i = 0; i < VHCI_NPORTS; i++) 502 + if (the_controller->vdev[i].udev == udev) 503 + return port_to_vdev(i); 504 + 505 + return NULL; 506 + } 507 + 508 + static void vhci_tx_urb(struct urb *urb) 509 + { 510 + struct vhci_device *vdev = get_vdev(urb->dev); 511 + struct vhci_priv *priv; 512 + unsigned long flag; 513 + 514 + if (!vdev) { 515 + err("could not get virtual device"); 516 + /* BUG(); */ 517 + return; 518 + } 519 + 520 + spin_lock_irqsave(&vdev->priv_lock, flag); 521 + 522 + priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC); 523 + if (!priv) { 524 + dev_err(&urb->dev->dev, "malloc vhci_priv\n"); 525 + spin_unlock_irqrestore(&vdev->priv_lock, flag); 526 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC); 527 + return; 528 + } 529 + 530 + priv->seqnum = atomic_inc_return(&the_controller->seqnum); 531 + if (priv->seqnum == 0xffff) 532 + uinfo("seqnum max\n"); 533 + 534 + priv->vdev = vdev; 535 + priv->urb = urb; 536 + 537 + urb->hcpriv = (void *) priv; 538 + 539 + 540 + list_add_tail(&priv->list, &vdev->priv_tx); 541 + 542 + wake_up(&vdev->waitq_tx); 543 + spin_unlock_irqrestore(&vdev->priv_lock, flag); 544 + } 545 + 546 + static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, 547 + gfp_t mem_flags) 548 + { 549 + struct device *dev = &urb->dev->dev; 550 + int ret = 0; 551 + unsigned long flags; 552 + 553 + dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n", 554 + hcd, urb, mem_flags); 555 + 556 + /* patch to usb_sg_init() is in 2.5.60 */ 557 + BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length); 558 + 559 + spin_lock_irqsave(&the_controller->lock, flags); 560 + 561 + /* check HC is active or not */ 562 + if (!HC_IS_RUNNING(hcd->state)) { 563 + dev_err(dev, "HC is not running\n"); 564 + spin_unlock_irqrestore(&the_controller->lock, flags); 565 + return -ENODEV; 566 + } 567 + 568 + if (urb->status != -EINPROGRESS) { 569 + dev_err(dev, "URB already unlinked!, status %d\n", urb->status); 570 + spin_unlock_irqrestore(&the_controller->lock, flags); 571 + return urb->status; 572 + } 573 + 574 + ret = usb_hcd_link_urb_to_ep(hcd, urb); 575 + if (ret) 576 + goto no_need_unlink; 577 + 578 + /* 579 + * The enumelation process is as follows; 580 + * 581 + * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0) 582 + * to get max packet length of default pipe 583 + * 584 + * 2. Set_Address request to DevAddr(0) EndPoint(0) 585 + * 586 + */ 587 + 588 + if (usb_pipedevice(urb->pipe) == 0) { 589 + __u8 type = usb_pipetype(urb->pipe); 590 + struct usb_ctrlrequest *ctrlreq = 591 + (struct usb_ctrlrequest *) urb->setup_packet; 592 + struct vhci_device *vdev = 593 + port_to_vdev(the_controller->pending_port); 594 + 595 + if (type != PIPE_CONTROL || !ctrlreq) { 596 + dev_err(dev, "invalid request to devnum 0\n"); 597 + ret = EINVAL; 598 + goto no_need_xmit; 599 + } 600 + 601 + switch (ctrlreq->bRequest) { 602 + case USB_REQ_SET_ADDRESS: 603 + /* set_address may come when a device is reset */ 604 + dev_info(dev, "SetAddress Request (%d) to port %d\n", 605 + ctrlreq->wValue, vdev->rhport); 606 + 607 + vdev->udev = urb->dev; 608 + 609 + spin_lock(&vdev->ud.lock); 610 + vdev->ud.status = VDEV_ST_USED; 611 + spin_unlock(&vdev->ud.lock); 612 + 613 + if (urb->status == -EINPROGRESS) { 614 + /* This request is successfully completed. */ 615 + /* If not -EINPROGRESS, possibly unlinked. */ 616 + urb->status = 0; 617 + } 618 + 619 + goto no_need_xmit; 620 + 621 + case USB_REQ_GET_DESCRIPTOR: 622 + if (ctrlreq->wValue == (USB_DT_DEVICE << 8)) 623 + dbg_vhci_hc("Not yet?: " 624 + "Get_Descriptor to device 0 " 625 + "(get max pipe size)\n"); 626 + 627 + /* FIXME: reference count? (usb_get_dev()) */ 628 + vdev->udev = urb->dev; 629 + goto out; 630 + 631 + default: 632 + /* NOT REACHED */ 633 + dev_err(dev, "invalid request to devnum 0 bRequest %u, " 634 + "wValue %u\n", ctrlreq->bRequest, 635 + ctrlreq->wValue); 636 + ret = -EINVAL; 637 + goto no_need_xmit; 638 + } 639 + 640 + } 641 + 642 + out: 643 + vhci_tx_urb(urb); 644 + 645 + spin_unlock_irqrestore(&the_controller->lock, flags); 646 + 647 + return 0; 648 + 649 + no_need_xmit: 650 + usb_hcd_unlink_urb_from_ep(hcd, urb); 651 + no_need_unlink: 652 + spin_unlock_irqrestore(&the_controller->lock, flags); 653 + 654 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status); 655 + 656 + return 0; 657 + } 658 + 659 + /* 660 + * vhci_rx gives back the urb after receiving the reply of the urb. If an 661 + * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives 662 + * back its urb. For the driver unlinking the urb, the content of the urb is 663 + * not important, but the calling to its completion handler is important; the 664 + * completion of unlinking is notified by the completion handler. 665 + * 666 + * 667 + * CLIENT SIDE 668 + * 669 + * - When vhci_hcd receives RET_SUBMIT, 670 + * 671 + * - case 1a). the urb of the pdu is not unlinking. 672 + * - normal case 673 + * => just give back the urb 674 + * 675 + * - case 1b). the urb of the pdu is unlinking. 676 + * - usbip.ko will return a reply of the unlinking request. 677 + * => give back the urb now and go to case 2b). 678 + * 679 + * - When vhci_hcd receives RET_UNLINK, 680 + * 681 + * - case 2a). a submit request is still pending in vhci_hcd. 682 + * - urb was really pending in usbip.ko and urb_unlink_urb() was 683 + * completed there. 684 + * => free a pending submit request 685 + * => notify unlink completeness by giving back the urb 686 + * 687 + * - case 2b). a submit request is *not* pending in vhci_hcd. 688 + * - urb was already given back to the core driver. 689 + * => do not give back the urb 690 + * 691 + * 692 + * SERVER SIDE 693 + * 694 + * - When usbip receives CMD_UNLINK, 695 + * 696 + * - case 3a). the urb of the unlink request is now in submission. 697 + * => do usb_unlink_urb(). 698 + * => after the unlink is completed, send RET_UNLINK. 699 + * 700 + * - case 3b). the urb of the unlink request is not in submission. 701 + * - may be already completed or never be received 702 + * => send RET_UNLINK 703 + * 704 + */ 705 + static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) 706 + { 707 + unsigned long flags; 708 + struct vhci_priv *priv; 709 + struct vhci_device *vdev; 710 + 711 + uinfo("vhci_hcd: dequeue a urb %p\n", urb); 712 + 713 + 714 + spin_lock_irqsave(&the_controller->lock, flags); 715 + 716 + priv = urb->hcpriv; 717 + if (!priv) { 718 + /* URB was never linked! or will be soon given back by 719 + * vhci_rx. */ 720 + spin_unlock_irqrestore(&the_controller->lock, flags); 721 + return 0; 722 + } 723 + 724 + { 725 + int ret = 0; 726 + ret = usb_hcd_check_unlink_urb(hcd, urb, status); 727 + if (ret) { 728 + spin_unlock_irqrestore(&the_controller->lock, flags); 729 + return 0; 730 + } 731 + } 732 + 733 + /* send unlink request here? */ 734 + vdev = priv->vdev; 735 + 736 + if (!vdev->ud.tcp_socket) { 737 + /* tcp connection is closed */ 738 + unsigned long flags2; 739 + 740 + spin_lock_irqsave(&vdev->priv_lock, flags2); 741 + 742 + uinfo("vhci_hcd: device %p seems to be disconnected\n", vdev); 743 + list_del(&priv->list); 744 + kfree(priv); 745 + urb->hcpriv = NULL; 746 + 747 + spin_unlock_irqrestore(&vdev->priv_lock, flags2); 748 + 749 + } else { 750 + /* tcp connection is alive */ 751 + unsigned long flags2; 752 + struct vhci_unlink *unlink; 753 + 754 + spin_lock_irqsave(&vdev->priv_lock, flags2); 755 + 756 + /* setup CMD_UNLINK pdu */ 757 + unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC); 758 + if (!unlink) { 759 + uerr("malloc vhci_unlink\n"); 760 + spin_unlock_irqrestore(&vdev->priv_lock, flags2); 761 + spin_unlock_irqrestore(&the_controller->lock, flags); 762 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC); 763 + return -ENOMEM; 764 + } 765 + 766 + unlink->seqnum = atomic_inc_return(&the_controller->seqnum); 767 + if (unlink->seqnum == 0xffff) 768 + uinfo("seqnum max\n"); 769 + 770 + unlink->unlink_seqnum = priv->seqnum; 771 + 772 + uinfo("vhci_hcd: device %p seems to be still connected\n", 773 + vdev); 774 + 775 + /* send cmd_unlink and try to cancel the pending URB in the 776 + * peer */ 777 + list_add_tail(&unlink->list, &vdev->unlink_tx); 778 + wake_up(&vdev->waitq_tx); 779 + 780 + spin_unlock_irqrestore(&vdev->priv_lock, flags2); 781 + } 782 + 783 + 784 + /* 785 + * If tcp connection is alive, we have sent CMD_UNLINK. 786 + * vhci_rx will receive RET_UNLINK and give back the URB. 787 + * Otherwise, we give back it here. 788 + */ 789 + if (!vdev->ud.tcp_socket) { 790 + /* tcp connection is closed */ 791 + uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n", urb); 792 + 793 + usb_hcd_unlink_urb_from_ep(hcd, urb); 794 + 795 + spin_unlock_irqrestore(&the_controller->lock, flags); 796 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, 797 + urb->status); 798 + spin_lock_irqsave(&the_controller->lock, flags); 799 + } 800 + 801 + spin_unlock_irqrestore(&the_controller->lock, flags); 802 + 803 + dbg_vhci_hc("leave\n"); 804 + return 0; 805 + } 806 + 807 + 808 + static void vhci_device_unlink_cleanup(struct vhci_device *vdev) 809 + { 810 + struct vhci_unlink *unlink, *tmp; 811 + 812 + spin_lock(&vdev->priv_lock); 813 + 814 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) { 815 + list_del(&unlink->list); 816 + kfree(unlink); 817 + } 818 + 819 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) { 820 + list_del(&unlink->list); 821 + kfree(unlink); 822 + } 823 + 824 + spin_unlock(&vdev->priv_lock); 825 + } 826 + 827 + /* 828 + * The important thing is that only one context begins cleanup. 829 + * This is why error handling and cleanup become simple. 830 + * We do not want to consider race condition as possible. 831 + */ 832 + static void vhci_shutdown_connection(struct usbip_device *ud) 833 + { 834 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); 835 + 836 + /* need this? see stub_dev.c */ 837 + if (ud->tcp_socket) { 838 + udbg("shutdown tcp_socket %p\n", ud->tcp_socket); 839 + kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR); 840 + } 841 + 842 + usbip_stop_threads(&vdev->ud); 843 + uinfo("stop threads\n"); 844 + 845 + /* active connection is closed */ 846 + if (vdev->ud.tcp_socket != NULL) { 847 + sock_release(vdev->ud.tcp_socket); 848 + vdev->ud.tcp_socket = NULL; 849 + } 850 + uinfo("release socket\n"); 851 + 852 + vhci_device_unlink_cleanup(vdev); 853 + 854 + /* 855 + * rh_port_disconnect() is a trigger of ... 856 + * usb_disable_device(): 857 + * disable all the endpoints for a USB device. 858 + * usb_disable_endpoint(): 859 + * disable endpoints. pending urbs are unlinked(dequeued). 860 + * 861 + * NOTE: After calling rh_port_disconnect(), the USB device drivers of a 862 + * deteched device should release used urbs in a cleanup function(i.e. 863 + * xxx_disconnect()). Therefore, vhci_hcd does not need to release 864 + * pushed urbs and their private data in this function. 865 + * 866 + * NOTE: vhci_dequeue() must be considered carefully. When shutdowning 867 + * a connection, vhci_shutdown_connection() expects vhci_dequeue() 868 + * gives back pushed urbs and frees their private data by request of 869 + * the cleanup function of a USB driver. When unlinking a urb with an 870 + * active connection, vhci_dequeue() does not give back the urb which 871 + * is actually given back by vhci_rx after receiving its return pdu. 872 + * 873 + */ 874 + rh_port_disconnect(vdev->rhport); 875 + 876 + uinfo("disconnect device\n"); 877 + } 878 + 879 + 880 + static void vhci_device_reset(struct usbip_device *ud) 881 + { 882 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); 883 + 884 + spin_lock(&ud->lock); 885 + 886 + vdev->speed = 0; 887 + vdev->devid = 0; 888 + 889 + ud->tcp_socket = NULL; 890 + 891 + ud->status = VDEV_ST_NULL; 892 + 893 + spin_unlock(&ud->lock); 894 + } 895 + 896 + static void vhci_device_unusable(struct usbip_device *ud) 897 + { 898 + spin_lock(&ud->lock); 899 + 900 + ud->status = VDEV_ST_ERROR; 901 + 902 + spin_unlock(&ud->lock); 903 + } 904 + 905 + static void vhci_device_init(struct vhci_device *vdev) 906 + { 907 + memset(vdev, 0, sizeof(*vdev)); 908 + 909 + usbip_task_init(&vdev->ud.tcp_rx, "vhci_rx", vhci_rx_loop); 910 + usbip_task_init(&vdev->ud.tcp_tx, "vhci_tx", vhci_tx_loop); 911 + 912 + vdev->ud.side = USBIP_VHCI; 913 + vdev->ud.status = VDEV_ST_NULL; 914 + /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */ 915 + spin_lock_init(&vdev->ud.lock); 916 + 917 + INIT_LIST_HEAD(&vdev->priv_rx); 918 + INIT_LIST_HEAD(&vdev->priv_tx); 919 + INIT_LIST_HEAD(&vdev->unlink_tx); 920 + INIT_LIST_HEAD(&vdev->unlink_rx); 921 + /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */ 922 + spin_lock_init(&vdev->priv_lock); 923 + 924 + init_waitqueue_head(&vdev->waitq_tx); 925 + 926 + vdev->ud.eh_ops.shutdown = vhci_shutdown_connection; 927 + vdev->ud.eh_ops.reset = vhci_device_reset; 928 + vdev->ud.eh_ops.unusable = vhci_device_unusable; 929 + 930 + usbip_start_eh(&vdev->ud); 931 + } 932 + 933 + 934 + /*----------------------------------------------------------------------*/ 935 + 936 + static int vhci_start(struct usb_hcd *hcd) 937 + { 938 + struct vhci_hcd *vhci = hcd_to_vhci(hcd); 939 + int rhport; 940 + int err = 0; 941 + 942 + dbg_vhci_hc("enter vhci_start\n"); 943 + 944 + 945 + /* initialize private data of usb_hcd */ 946 + 947 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++) { 948 + struct vhci_device *vdev = &vhci->vdev[rhport]; 949 + vhci_device_init(vdev); 950 + vdev->rhport = rhport; 951 + } 952 + 953 + atomic_set(&vhci->seqnum, 0); 954 + spin_lock_init(&vhci->lock); 955 + 956 + 957 + 958 + hcd->power_budget = 0; /* no limit */ 959 + hcd->state = HC_STATE_RUNNING; 960 + hcd->uses_new_polling = 1; 961 + 962 + 963 + /* vhci_hcd is now ready to be controlled through sysfs */ 964 + err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group); 965 + if (err) { 966 + uerr("create sysfs files\n"); 967 + return err; 968 + } 969 + 970 + return 0; 971 + } 972 + 973 + static void vhci_stop(struct usb_hcd *hcd) 974 + { 975 + struct vhci_hcd *vhci = hcd_to_vhci(hcd); 976 + int rhport = 0; 977 + 978 + dbg_vhci_hc("stop VHCI controller\n"); 979 + 980 + 981 + /* 1. remove the userland interface of vhci_hcd */ 982 + sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group); 983 + 984 + /* 2. shutdown all the ports of vhci_hcd */ 985 + for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) { 986 + struct vhci_device *vdev = &vhci->vdev[rhport]; 987 + 988 + usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED); 989 + usbip_stop_eh(&vdev->ud); 990 + } 991 + 992 + 993 + uinfo("vhci_stop done\n"); 994 + } 995 + 996 + /*----------------------------------------------------------------------*/ 997 + 998 + static int vhci_get_frame_number(struct usb_hcd *hcd) 999 + { 1000 + uerr("Not yet implemented\n"); 1001 + return 0; 1002 + } 1003 + 1004 + 1005 + #ifdef CONFIG_PM 1006 + 1007 + /* FIXME: suspend/resume */ 1008 + static int vhci_bus_suspend(struct usb_hcd *hcd) 1009 + { 1010 + struct vhci_hcd *vhci = hcd_to_vhci(hcd); 1011 + 1012 + dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); 1013 + 1014 + spin_lock_irq(&vhci->lock); 1015 + /* vhci->rh_state = DUMMY_RH_SUSPENDED; 1016 + * set_link_state(vhci); */ 1017 + hcd->state = HC_STATE_SUSPENDED; 1018 + spin_unlock_irq(&vhci->lock); 1019 + 1020 + return 0; 1021 + } 1022 + 1023 + static int vhci_bus_resume(struct usb_hcd *hcd) 1024 + { 1025 + struct vhci_hcd *vhci = hcd_to_vhci(hcd); 1026 + int rc = 0; 1027 + 1028 + dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__); 1029 + 1030 + spin_lock_irq(&vhci->lock); 1031 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { 1032 + rc = -ESHUTDOWN; 1033 + } else { 1034 + /* vhci->rh_state = DUMMY_RH_RUNNING; 1035 + * set_link_state(vhci); 1036 + * if (!list_empty(&vhci->urbp_list)) 1037 + * mod_timer(&vhci->timer, jiffies); */ 1038 + hcd->state = HC_STATE_RUNNING; 1039 + } 1040 + spin_unlock_irq(&vhci->lock); 1041 + return rc; 1042 + 1043 + return 0; 1044 + } 1045 + 1046 + #else 1047 + 1048 + #define vhci_bus_suspend NULL 1049 + #define vhci_bus_resume NULL 1050 + #endif 1051 + 1052 + 1053 + 1054 + static struct hc_driver vhci_hc_driver = { 1055 + .description = driver_name, 1056 + .product_desc = driver_desc, 1057 + .hcd_priv_size = sizeof(struct vhci_hcd), 1058 + 1059 + .flags = HCD_USB2, 1060 + 1061 + .start = vhci_start, 1062 + .stop = vhci_stop, 1063 + 1064 + .urb_enqueue = vhci_urb_enqueue, 1065 + .urb_dequeue = vhci_urb_dequeue, 1066 + 1067 + .get_frame_number = vhci_get_frame_number, 1068 + 1069 + .hub_status_data = vhci_hub_status, 1070 + .hub_control = vhci_hub_control, 1071 + .bus_suspend = vhci_bus_suspend, 1072 + .bus_resume = vhci_bus_resume, 1073 + }; 1074 + 1075 + static int vhci_hcd_probe(struct platform_device *pdev) 1076 + { 1077 + struct usb_hcd *hcd; 1078 + int ret; 1079 + 1080 + uinfo("proving...\n"); 1081 + 1082 + dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id); 1083 + 1084 + /* will be removed */ 1085 + if (pdev->dev.dma_mask) { 1086 + dev_info(&pdev->dev, "vhci_hcd DMA not supported\n"); 1087 + return -EINVAL; 1088 + } 1089 + 1090 + /* 1091 + * Allocate and initialize hcd. 1092 + * Our private data is also allocated automatically. 1093 + */ 1094 + hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, pdev->dev.bus_id); 1095 + if (!hcd) { 1096 + uerr("create hcd failed\n"); 1097 + return -ENOMEM; 1098 + } 1099 + 1100 + 1101 + /* this is private data for vhci_hcd */ 1102 + the_controller = hcd_to_vhci(hcd); 1103 + 1104 + /* 1105 + * Finish generic HCD structure initialization and register. 1106 + * Call the driver's reset() and start() routines. 1107 + */ 1108 + ret = usb_add_hcd(hcd, 0, 0); 1109 + if (ret != 0) { 1110 + uerr("usb_add_hcd failed %d\n", ret); 1111 + usb_put_hcd(hcd); 1112 + the_controller = NULL; 1113 + return ret; 1114 + } 1115 + 1116 + 1117 + dbg_vhci_hc("bye\n"); 1118 + return 0; 1119 + } 1120 + 1121 + 1122 + static int vhci_hcd_remove(struct platform_device *pdev) 1123 + { 1124 + struct usb_hcd *hcd; 1125 + 1126 + hcd = platform_get_drvdata(pdev); 1127 + if (!hcd) 1128 + return 0; 1129 + 1130 + /* 1131 + * Disconnects the root hub, 1132 + * then reverses the effects of usb_add_hcd(), 1133 + * invoking the HCD's stop() methods. 1134 + */ 1135 + usb_remove_hcd(hcd); 1136 + usb_put_hcd(hcd); 1137 + the_controller = NULL; 1138 + 1139 + 1140 + return 0; 1141 + } 1142 + 1143 + 1144 + 1145 + #ifdef CONFIG_PM 1146 + 1147 + /* what should happen for USB/IP under suspend/resume? */ 1148 + static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state) 1149 + { 1150 + struct usb_hcd *hcd; 1151 + int rhport = 0; 1152 + int connected = 0; 1153 + int ret = 0; 1154 + 1155 + dev_dbg(&pdev->dev, "%s\n", __func__); 1156 + 1157 + hcd = platform_get_drvdata(pdev); 1158 + 1159 + spin_lock(&the_controller->lock); 1160 + 1161 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++) 1162 + if (the_controller->port_status[rhport] & 1163 + USB_PORT_STAT_CONNECTION) 1164 + connected += 1; 1165 + 1166 + spin_unlock(&the_controller->lock); 1167 + 1168 + if (connected > 0) { 1169 + uinfo("We have %d active connection%s. Do not suspend.\n", 1170 + connected, (connected == 1 ? "" : "s")); 1171 + ret = -EBUSY; 1172 + } else { 1173 + uinfo("suspend vhci_hcd"); 1174 + clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 1175 + } 1176 + 1177 + return ret; 1178 + } 1179 + 1180 + static int vhci_hcd_resume(struct platform_device *pdev) 1181 + { 1182 + struct usb_hcd *hcd; 1183 + 1184 + dev_dbg(&pdev->dev, "%s\n", __func__); 1185 + 1186 + hcd = platform_get_drvdata(pdev); 1187 + set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); 1188 + usb_hcd_poll_rh_status(hcd); 1189 + 1190 + return 0; 1191 + } 1192 + 1193 + #else 1194 + 1195 + #define vhci_hcd_suspend NULL 1196 + #define vhci_hcd_resume NULL 1197 + 1198 + #endif 1199 + 1200 + 1201 + static struct platform_driver vhci_driver = { 1202 + .probe = vhci_hcd_probe, 1203 + .remove = __devexit_p(vhci_hcd_remove), 1204 + .suspend = vhci_hcd_suspend, 1205 + .resume = vhci_hcd_resume, 1206 + .driver = { 1207 + .name = (char *) driver_name, 1208 + .owner = THIS_MODULE, 1209 + }, 1210 + }; 1211 + 1212 + /*----------------------------------------------------------------------*/ 1213 + 1214 + /* 1215 + * The VHCI 'device' is 'virtual'; not a real plug&play hardware. 1216 + * We need to add this virtual device as a platform device arbitrarily: 1217 + * 1. platform_device_register() 1218 + */ 1219 + static void the_pdev_release(struct device *dev) 1220 + { 1221 + return; 1222 + } 1223 + 1224 + static struct platform_device the_pdev = { 1225 + /* should be the same name as driver_name */ 1226 + .name = (char *) driver_name, 1227 + .id = -1, 1228 + .dev = { 1229 + /* .driver = &vhci_driver, */ 1230 + .release = the_pdev_release, 1231 + }, 1232 + }; 1233 + 1234 + static int __init vhci_init(void) 1235 + { 1236 + int ret; 1237 + 1238 + dbg_vhci_hc("enter\n"); 1239 + if (usb_disabled()) 1240 + return -ENODEV; 1241 + 1242 + printk(KERN_INFO KBUILD_MODNAME ": %s, %s\n", driver_name, 1243 + DRIVER_VERSION); 1244 + 1245 + ret = platform_driver_register(&vhci_driver); 1246 + if (ret < 0) 1247 + goto err_driver_register; 1248 + 1249 + ret = platform_device_register(&the_pdev); 1250 + if (ret < 0) 1251 + goto err_platform_device_register; 1252 + 1253 + dbg_vhci_hc("bye\n"); 1254 + return ret; 1255 + 1256 + /* error occurred */ 1257 + err_platform_device_register: 1258 + platform_driver_unregister(&vhci_driver); 1259 + 1260 + err_driver_register: 1261 + dbg_vhci_hc("bye\n"); 1262 + return ret; 1263 + } 1264 + module_init(vhci_init); 1265 + 1266 + static void __exit vhci_cleanup(void) 1267 + { 1268 + dbg_vhci_hc("enter\n"); 1269 + 1270 + platform_device_unregister(&the_pdev); 1271 + platform_driver_unregister(&vhci_driver); 1272 + 1273 + dbg_vhci_hc("bye\n"); 1274 + } 1275 + module_exit(vhci_cleanup);
+251
drivers/staging/usbip/vhci_rx.c
··· 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 + 20 + #include "usbip_common.h" 21 + #include "vhci.h" 22 + 23 + 24 + /* get URB from transmitted urb queue */ 25 + static struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, 26 + __u32 seqnum) 27 + { 28 + struct vhci_priv *priv, *tmp; 29 + struct urb *urb = NULL; 30 + int status; 31 + 32 + spin_lock(&vdev->priv_lock); 33 + 34 + list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) { 35 + if (priv->seqnum == seqnum) { 36 + urb = priv->urb; 37 + status = urb->status; 38 + 39 + dbg_vhci_rx("find urb %p vurb %p seqnum %u\n", 40 + urb, priv, seqnum); 41 + 42 + /* TODO: fix logic here to improve indent situtation */ 43 + if (status != -EINPROGRESS) { 44 + if (status == -ENOENT || 45 + status == -ECONNRESET) 46 + dev_info(&urb->dev->dev, 47 + "urb %p was unlinked " 48 + "%ssynchronuously.\n", urb, 49 + status == -ENOENT ? "" : "a"); 50 + else 51 + dev_info(&urb->dev->dev, 52 + "urb %p may be in a error, " 53 + "status %d\n", urb, status); 54 + } 55 + 56 + list_del(&priv->list); 57 + kfree(priv); 58 + urb->hcpriv = NULL; 59 + 60 + break; 61 + } 62 + } 63 + 64 + spin_unlock(&vdev->priv_lock); 65 + 66 + return urb; 67 + } 68 + 69 + static void vhci_recv_ret_submit(struct vhci_device *vdev, 70 + struct usbip_header *pdu) 71 + { 72 + struct usbip_device *ud = &vdev->ud; 73 + struct urb *urb; 74 + 75 + 76 + urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum); 77 + 78 + 79 + if (!urb) { 80 + uerr("cannot find a urb of seqnum %u\n", pdu->base.seqnum); 81 + uinfo("max seqnum %d\n", atomic_read(&the_controller->seqnum)); 82 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); 83 + return; 84 + } 85 + 86 + 87 + /* unpack the pdu to a urb */ 88 + usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0); 89 + 90 + 91 + /* recv transfer buffer */ 92 + if (usbip_recv_xbuff(ud, urb) < 0) 93 + return; 94 + 95 + 96 + /* recv iso_packet_descriptor */ 97 + if (usbip_recv_iso(ud, urb) < 0) 98 + return; 99 + 100 + 101 + if (dbg_flag_vhci_rx) 102 + usbip_dump_urb(urb); 103 + 104 + 105 + dbg_vhci_rx("now giveback urb %p\n", urb); 106 + 107 + spin_lock(&the_controller->lock); 108 + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); 109 + spin_unlock(&the_controller->lock); 110 + 111 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status); 112 + 113 + 114 + dbg_vhci_rx("Leave\n"); 115 + 116 + return; 117 + } 118 + 119 + 120 + static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev, 121 + struct usbip_header *pdu) 122 + { 123 + struct vhci_unlink *unlink, *tmp; 124 + 125 + spin_lock(&vdev->priv_lock); 126 + 127 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) { 128 + uinfo("unlink->seqnum %lu\n", unlink->seqnum); 129 + if (unlink->seqnum == pdu->base.seqnum) { 130 + dbg_vhci_rx("found pending unlink, %lu\n", 131 + unlink->seqnum); 132 + list_del(&unlink->list); 133 + 134 + spin_unlock(&vdev->priv_lock); 135 + return unlink; 136 + } 137 + } 138 + 139 + spin_unlock(&vdev->priv_lock); 140 + 141 + return NULL; 142 + } 143 + 144 + 145 + static void vhci_recv_ret_unlink(struct vhci_device *vdev, 146 + struct usbip_header *pdu) 147 + { 148 + struct vhci_unlink *unlink; 149 + struct urb *urb; 150 + 151 + usbip_dump_header(pdu); 152 + 153 + unlink = dequeue_pending_unlink(vdev, pdu); 154 + if (!unlink) { 155 + uinfo("cannot find the pending unlink %u\n", pdu->base.seqnum); 156 + return; 157 + } 158 + 159 + urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum); 160 + if (!urb) { 161 + /* 162 + * I get the result of a unlink request. But, it seems that I 163 + * already received the result of its submit result and gave 164 + * back the URB. 165 + */ 166 + uinfo("the urb (seqnum %d) was already given backed\n", 167 + pdu->base.seqnum); 168 + } else { 169 + dbg_vhci_rx("now giveback urb %p\n", urb); 170 + 171 + /* If unlink is succeed, status is -ECONNRESET */ 172 + urb->status = pdu->u.ret_unlink.status; 173 + uinfo("%d\n", urb->status); 174 + 175 + spin_lock(&the_controller->lock); 176 + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); 177 + spin_unlock(&the_controller->lock); 178 + 179 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, 180 + urb->status); 181 + } 182 + 183 + kfree(unlink); 184 + 185 + return; 186 + } 187 + 188 + /* recv a pdu */ 189 + static void vhci_rx_pdu(struct usbip_device *ud) 190 + { 191 + int ret; 192 + struct usbip_header pdu; 193 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); 194 + 195 + 196 + dbg_vhci_rx("Enter\n"); 197 + 198 + memset(&pdu, 0, sizeof(pdu)); 199 + 200 + 201 + /* 1. receive a pdu header */ 202 + ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0); 203 + if (ret != sizeof(pdu)) { 204 + uerr("receiving pdu failed! size is %d, should be %d\n", 205 + ret, sizeof(pdu)); 206 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); 207 + return; 208 + } 209 + 210 + usbip_header_correct_endian(&pdu, 0); 211 + 212 + if (dbg_flag_vhci_rx) 213 + usbip_dump_header(&pdu); 214 + 215 + switch (pdu.base.command) { 216 + case USBIP_RET_SUBMIT: 217 + vhci_recv_ret_submit(vdev, &pdu); 218 + break; 219 + case USBIP_RET_UNLINK: 220 + vhci_recv_ret_unlink(vdev, &pdu); 221 + break; 222 + default: 223 + /* NOTREACHED */ 224 + uerr("unknown pdu %u\n", pdu.base.command); 225 + usbip_dump_header(&pdu); 226 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); 227 + } 228 + } 229 + 230 + 231 + /*-------------------------------------------------------------------------*/ 232 + 233 + void vhci_rx_loop(struct usbip_task *ut) 234 + { 235 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx); 236 + 237 + 238 + while (1) { 239 + if (signal_pending(current)) { 240 + dbg_vhci_rx("signal catched!\n"); 241 + break; 242 + } 243 + 244 + 245 + if (usbip_event_happend(ud)) 246 + break; 247 + 248 + vhci_rx_pdu(ud); 249 + } 250 + } 251 +
+250
drivers/staging/usbip/vhci_sysfs.c
··· 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 + 20 + #include "usbip_common.h" 21 + #include "vhci.h" 22 + 23 + #include <linux/in.h> 24 + 25 + /* TODO: refine locking ?*/ 26 + 27 + /* Sysfs entry to show port status */ 28 + static ssize_t show_status(struct device *dev, struct device_attribute *attr, 29 + char *out) 30 + { 31 + char *s = out; 32 + int i = 0; 33 + 34 + if (!the_controller || !out) 35 + BUG(); 36 + 37 + spin_lock(&the_controller->lock); 38 + 39 + /* 40 + * output example: 41 + * prt sta spd dev socket local_busid 42 + * 000 004 000 000 c5a7bb80 1-2.3 43 + * 001 004 000 000 d8cee980 2-3.4 44 + * 45 + * IP address can be retrieved from a socket pointer address by looking 46 + * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a 47 + * port number and its peer IP address. 48 + */ 49 + out += sprintf(out, "prt sta spd bus dev socket " 50 + "local_busid\n"); 51 + 52 + for (i = 0; i < VHCI_NPORTS; i++) { 53 + struct vhci_device *vdev = port_to_vdev(i); 54 + 55 + spin_lock(&vdev->ud.lock); 56 + 57 + out += sprintf(out, "%03u %03u ", i, vdev->ud.status); 58 + 59 + if (vdev->ud.status == VDEV_ST_USED) { 60 + out += sprintf(out, "%03u %08x ", 61 + vdev->speed, vdev->devid); 62 + out += sprintf(out, "%16p ", vdev->ud.tcp_socket); 63 + out += sprintf(out, "%s", vdev->udev->dev.bus_id); 64 + 65 + } else 66 + out += sprintf(out, "000 000 000 0000000000000000 0-0"); 67 + 68 + out += sprintf(out, "\n"); 69 + 70 + spin_unlock(&vdev->ud.lock); 71 + } 72 + 73 + spin_unlock(&the_controller->lock); 74 + 75 + return out - s; 76 + } 77 + static DEVICE_ATTR(status, S_IRUGO, show_status, NULL); 78 + 79 + /* Sysfs entry to shutdown a virtual connection */ 80 + static int vhci_port_disconnect(__u32 rhport) 81 + { 82 + struct vhci_device *vdev; 83 + 84 + dbg_vhci_sysfs("enter\n"); 85 + 86 + /* lock */ 87 + spin_lock(&the_controller->lock); 88 + 89 + vdev = port_to_vdev(rhport); 90 + 91 + spin_lock(&vdev->ud.lock); 92 + if (vdev->ud.status == VDEV_ST_NULL) { 93 + uerr("not connected %d\n", vdev->ud.status); 94 + 95 + /* unlock */ 96 + spin_unlock(&vdev->ud.lock); 97 + spin_unlock(&the_controller->lock); 98 + 99 + return -EINVAL; 100 + } 101 + 102 + /* unlock */ 103 + spin_unlock(&vdev->ud.lock); 104 + spin_unlock(&the_controller->lock); 105 + 106 + usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN); 107 + 108 + return 0; 109 + } 110 + 111 + static ssize_t store_detach(struct device *dev, struct device_attribute *attr, 112 + const char *buf, size_t count) 113 + { 114 + int err; 115 + __u32 rhport = 0; 116 + 117 + sscanf(buf, "%u", &rhport); 118 + 119 + /* check rhport */ 120 + if (rhport >= VHCI_NPORTS) { 121 + uerr("invalid port %u\n", rhport); 122 + return -EINVAL; 123 + } 124 + 125 + err = vhci_port_disconnect(rhport); 126 + if (err < 0) 127 + return -EINVAL; 128 + 129 + dbg_vhci_sysfs("Leave\n"); 130 + return count; 131 + } 132 + static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach); 133 + 134 + /* Sysfs entry to establish a virtual connection */ 135 + static int valid_args(__u32 rhport, enum usb_device_speed speed) 136 + { 137 + /* check rhport */ 138 + if ((rhport < 0) || (rhport >= VHCI_NPORTS)) { 139 + uerr("port %u\n", rhport); 140 + return -EINVAL; 141 + } 142 + 143 + /* check speed */ 144 + switch (speed) { 145 + case USB_SPEED_LOW: 146 + case USB_SPEED_FULL: 147 + case USB_SPEED_HIGH: 148 + case USB_SPEED_VARIABLE: 149 + break; 150 + default: 151 + uerr("speed %d\n", speed); 152 + return -EINVAL; 153 + } 154 + 155 + return 0; 156 + } 157 + 158 + /* 159 + * To start a new USB/IP attachment, a userland program needs to setup a TCP 160 + * connection and then write its socket descriptor with remote device 161 + * information into this sysfs file. 162 + * 163 + * A remote device is virtually attached to the root-hub port of @rhport with 164 + * @speed. @devid is embedded into a request to specify the remote device in a 165 + * server host. 166 + * 167 + * write() returns 0 on success, else negative errno. 168 + */ 169 + static ssize_t store_attach(struct device *dev, struct device_attribute *attr, 170 + const char *buf, size_t count) 171 + { 172 + struct vhci_device *vdev; 173 + struct socket *socket; 174 + int sockfd = 0; 175 + __u32 rhport = 0, devid = 0, speed = 0; 176 + 177 + /* 178 + * @rhport: port number of vhci_hcd 179 + * @sockfd: socket descriptor of an established TCP connection 180 + * @devid: unique device identifier in a remote host 181 + * @speed: usb device speed in a remote host 182 + */ 183 + sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed); 184 + 185 + dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n", 186 + rhport, sockfd, devid, speed); 187 + 188 + 189 + /* check received parameters */ 190 + if (valid_args(rhport, speed) < 0) 191 + return -EINVAL; 192 + 193 + /* check sockfd */ 194 + socket = sockfd_to_socket(sockfd); 195 + if (!socket) 196 + return -EINVAL; 197 + 198 + /* now need lock until setting vdev status as used */ 199 + 200 + /* begin a lock */ 201 + spin_lock(&the_controller->lock); 202 + 203 + vdev = port_to_vdev(rhport); 204 + 205 + spin_lock(&vdev->ud.lock); 206 + 207 + if (vdev->ud.status != VDEV_ST_NULL) { 208 + /* end of the lock */ 209 + spin_unlock(&vdev->ud.lock); 210 + spin_unlock(&the_controller->lock); 211 + 212 + uerr("port %d already used\n", rhport); 213 + return -EINVAL; 214 + } 215 + 216 + uinfo("rhport(%u) sockfd(%d) devid(%u) speed(%u)\n", 217 + rhport, sockfd, devid, speed); 218 + 219 + vdev->devid = devid; 220 + vdev->speed = speed; 221 + vdev->ud.tcp_socket = socket; 222 + vdev->ud.status = VDEV_ST_NOTASSIGNED; 223 + 224 + spin_unlock(&vdev->ud.lock); 225 + spin_unlock(&the_controller->lock); 226 + /* end the lock */ 227 + 228 + /* 229 + * this function will sleep, so should be out of the lock. but, it's ok 230 + * because we already marked vdev as being used. really? 231 + */ 232 + usbip_start_threads(&vdev->ud); 233 + 234 + rh_port_connect(rhport, speed); 235 + 236 + return count; 237 + } 238 + static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach); 239 + 240 + static struct attribute *dev_attrs[] = { 241 + &dev_attr_status.attr, 242 + &dev_attr_detach.attr, 243 + &dev_attr_attach.attr, 244 + &dev_attr_usbip_debug.attr, 245 + NULL, 246 + }; 247 + 248 + struct attribute_group dev_attr_group = { 249 + .attrs = dev_attrs, 250 + };
+239
drivers/staging/usbip/vhci_tx.c
··· 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 + 20 + #include "usbip_common.h" 21 + #include "vhci.h" 22 + 23 + 24 + static void setup_cmd_submit_pdu(struct usbip_header *pdup, struct urb *urb) 25 + { 26 + struct vhci_priv *priv = ((struct vhci_priv *)urb->hcpriv); 27 + struct vhci_device *vdev = priv->vdev; 28 + 29 + dbg_vhci_tx("URB, local devnum %u, remote devid %u\n", 30 + usb_pipedevice(urb->pipe), vdev->devid); 31 + 32 + pdup->base.command = USBIP_CMD_SUBMIT; 33 + pdup->base.seqnum = priv->seqnum; 34 + pdup->base.devid = vdev->devid; 35 + if (usb_pipein(urb->pipe)) 36 + pdup->base.direction = USBIP_DIR_IN; 37 + else 38 + pdup->base.direction = USBIP_DIR_OUT; 39 + pdup->base.ep = usb_pipeendpoint(urb->pipe); 40 + 41 + usbip_pack_pdu(pdup, urb, USBIP_CMD_SUBMIT, 1); 42 + 43 + if (urb->setup_packet) 44 + memcpy(pdup->u.cmd_submit.setup, urb->setup_packet, 8); 45 + } 46 + 47 + static struct vhci_priv *dequeue_from_priv_tx(struct vhci_device *vdev) 48 + { 49 + unsigned long flags; 50 + struct vhci_priv *priv, *tmp; 51 + 52 + spin_lock_irqsave(&vdev->priv_lock, flags); 53 + 54 + list_for_each_entry_safe(priv, tmp, &vdev->priv_tx, list) { 55 + list_move_tail(&priv->list, &vdev->priv_rx); 56 + spin_unlock_irqrestore(&vdev->priv_lock, flags); 57 + return priv; 58 + } 59 + 60 + spin_unlock_irqrestore(&vdev->priv_lock, flags); 61 + 62 + return NULL; 63 + } 64 + 65 + 66 + 67 + static int vhci_send_cmd_submit(struct vhci_device *vdev) 68 + { 69 + struct vhci_priv *priv = NULL; 70 + 71 + struct msghdr msg; 72 + struct kvec iov[3]; 73 + size_t txsize; 74 + 75 + size_t total_size = 0; 76 + 77 + while ((priv = dequeue_from_priv_tx(vdev)) != NULL) { 78 + int ret; 79 + struct urb *urb = priv->urb; 80 + struct usbip_header pdu_header; 81 + void *iso_buffer = NULL; 82 + 83 + txsize = 0; 84 + memset(&pdu_header, 0, sizeof(pdu_header)); 85 + memset(&msg, 0, sizeof(msg)); 86 + memset(&iov, 0, sizeof(iov)); 87 + 88 + dbg_vhci_tx("setup txdata urb %p\n", urb); 89 + 90 + 91 + /* 1. setup usbip_header */ 92 + setup_cmd_submit_pdu(&pdu_header, urb); 93 + usbip_header_correct_endian(&pdu_header, 1); 94 + 95 + iov[0].iov_base = &pdu_header; 96 + iov[0].iov_len = sizeof(pdu_header); 97 + txsize += sizeof(pdu_header); 98 + 99 + /* 2. setup transfer buffer */ 100 + if (!usb_pipein(urb->pipe) && urb->transfer_buffer_length > 0) { 101 + iov[1].iov_base = urb->transfer_buffer; 102 + iov[1].iov_len = urb->transfer_buffer_length; 103 + txsize += urb->transfer_buffer_length; 104 + } 105 + 106 + /* 3. setup iso_packet_descriptor */ 107 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { 108 + ssize_t len = 0; 109 + 110 + iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len); 111 + if (!iso_buffer) { 112 + usbip_event_add(&vdev->ud, 113 + SDEV_EVENT_ERROR_MALLOC); 114 + return -1; 115 + } 116 + 117 + iov[2].iov_base = iso_buffer; 118 + iov[2].iov_len = len; 119 + txsize += len; 120 + } 121 + 122 + ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 3, txsize); 123 + if (ret != txsize) { 124 + uerr("sendmsg failed!, retval %d for %zd\n", ret, 125 + txsize); 126 + kfree(iso_buffer); 127 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP); 128 + return -1; 129 + } 130 + 131 + kfree(iso_buffer); 132 + dbg_vhci_tx("send txdata\n"); 133 + 134 + total_size += txsize; 135 + } 136 + 137 + return total_size; 138 + } 139 + 140 + 141 + /*-------------------------------------------------------------------------*/ 142 + 143 + static struct vhci_unlink *dequeue_from_unlink_tx(struct vhci_device *vdev) 144 + { 145 + unsigned long flags; 146 + struct vhci_unlink *unlink, *tmp; 147 + 148 + spin_lock_irqsave(&vdev->priv_lock, flags); 149 + 150 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) { 151 + list_move_tail(&unlink->list, &vdev->unlink_rx); 152 + spin_unlock_irqrestore(&vdev->priv_lock, flags); 153 + return unlink; 154 + } 155 + 156 + spin_unlock_irqrestore(&vdev->priv_lock, flags); 157 + 158 + return NULL; 159 + } 160 + 161 + static int vhci_send_cmd_unlink(struct vhci_device *vdev) 162 + { 163 + struct vhci_unlink *unlink = NULL; 164 + 165 + struct msghdr msg; 166 + struct kvec iov[3]; 167 + size_t txsize; 168 + 169 + size_t total_size = 0; 170 + 171 + while ((unlink = dequeue_from_unlink_tx(vdev)) != NULL) { 172 + int ret; 173 + struct usbip_header pdu_header; 174 + 175 + txsize = 0; 176 + memset(&pdu_header, 0, sizeof(pdu_header)); 177 + memset(&msg, 0, sizeof(msg)); 178 + memset(&iov, 0, sizeof(iov)); 179 + 180 + dbg_vhci_tx("setup cmd unlink, %lu \n", unlink->seqnum); 181 + 182 + 183 + /* 1. setup usbip_header */ 184 + pdu_header.base.command = USBIP_CMD_UNLINK; 185 + pdu_header.base.seqnum = unlink->seqnum; 186 + pdu_header.base.devid = vdev->devid; 187 + pdu_header.base.ep = 0; 188 + pdu_header.u.cmd_unlink.seqnum = unlink->unlink_seqnum; 189 + 190 + usbip_header_correct_endian(&pdu_header, 1); 191 + 192 + iov[0].iov_base = &pdu_header; 193 + iov[0].iov_len = sizeof(pdu_header); 194 + txsize += sizeof(pdu_header); 195 + 196 + ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 1, txsize); 197 + if (ret != txsize) { 198 + uerr("sendmsg failed!, retval %d for %zd\n", ret, 199 + txsize); 200 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP); 201 + return -1; 202 + } 203 + 204 + 205 + dbg_vhci_tx("send txdata\n"); 206 + 207 + total_size += txsize; 208 + } 209 + 210 + return total_size; 211 + } 212 + 213 + 214 + /*-------------------------------------------------------------------------*/ 215 + 216 + void vhci_tx_loop(struct usbip_task *ut) 217 + { 218 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx); 219 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); 220 + 221 + while (1) { 222 + if (signal_pending(current)) { 223 + uinfo("vhci_tx signal catched\n"); 224 + break; 225 + } 226 + 227 + if (vhci_send_cmd_submit(vdev) < 0) 228 + break; 229 + 230 + if (vhci_send_cmd_unlink(vdev) < 0) 231 + break; 232 + 233 + wait_event_interruptible(vdev->waitq_tx, 234 + (!list_empty(&vdev->priv_tx) || 235 + !list_empty(&vdev->unlink_tx))); 236 + 237 + dbg_vhci_tx("pending urbs ?, now wake up\n"); 238 + } 239 + }