2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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,
20 #include <linux/slab.h>
21 #include <linux/kthread.h>
23 #include "usbip_common.h"
26 #define DRIVER_VERSION "1.0"
27 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
28 #define DRIVER_DESC "Virtual Host Controller Interface Driver for USB/IP"
29 #define DRIVER_LICENCE "GPL"
33 * - update root hub emulation
34 * - move the emulation code to userland ?
35 * porting to other operating systems
36 * minimize kernel code
37 * - add suspend/resume code
38 * - clean up everything
41 /* See usb gadget dummy hcd */
43 static int vhci_hub_status(struct usb_hcd
*hcd
, char *buff
);
44 static int vhci_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
45 u16 wIndex
, char *buff
, u16 wLength
);
46 static int vhci_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
48 static int vhci_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
);
49 static int vhci_start(struct usb_hcd
*vhci_hcd
);
50 static void vhci_stop(struct usb_hcd
*hcd
);
51 static int vhci_get_frame_number(struct usb_hcd
*hcd
);
53 static const char driver_name
[] = "vhci_hcd";
54 static const char driver_desc
[] = "USB/IP Virtual Host Controller";
56 struct vhci_hcd
*the_controller
;
58 static const char * const bit_desc
[] = {
75 "C_CONNECTION", /*16*/
78 "C_OVER_CURRENT", /*19*/
93 static void dump_port_status(u32 status
)
97 printk(KERN_DEBUG
"status %08x:", status
);
98 for (i
= 0; i
< 32; i
++) {
99 if (status
& (1 << i
))
100 printk(KERN_DEBUG
" %s", bit_desc
[i
]);
102 printk(KERN_DEBUG
"\n");
105 void rh_port_connect(int rhport
, enum usb_device_speed speed
)
109 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport
);
111 spin_lock_irqsave(&the_controller
->lock
, flags
);
113 the_controller
->port_status
[rhport
] |= USB_PORT_STAT_CONNECTION
114 | (1 << USB_PORT_FEAT_C_CONNECTION
);
118 the_controller
->port_status
[rhport
] |= USB_PORT_STAT_HIGH_SPEED
;
121 the_controller
->port_status
[rhport
] |= USB_PORT_STAT_LOW_SPEED
;
127 /* spin_lock(&the_controller->vdev[rhport].ud.lock);
128 * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
129 * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
131 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
133 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller
));
136 void rh_port_disconnect(int rhport
)
140 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport
);
142 spin_lock_irqsave(&the_controller
->lock
, flags
);
143 /* stop_activity(dum, driver); */
144 the_controller
->port_status
[rhport
] &= ~USB_PORT_STAT_CONNECTION
;
145 the_controller
->port_status
[rhport
] |=
146 (1 << USB_PORT_FEAT_C_CONNECTION
);
148 /* not yet complete the disconnection
149 * spin_lock(&vdev->ud.lock);
150 * vdev->ud.status = VHC_ST_DISCONNECT;
151 * spin_unlock(&vdev->ud.lock); */
153 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
154 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller
));
157 #define PORT_C_MASK \
158 ((USB_PORT_STAT_C_CONNECTION \
159 | USB_PORT_STAT_C_ENABLE \
160 | USB_PORT_STAT_C_SUSPEND \
161 | USB_PORT_STAT_C_OVERCURRENT \
162 | USB_PORT_STAT_C_RESET) << 16)
165 * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without
166 * suspend/resume support. But, it is modified to provide multiple ports.
168 * @buf: a bitmap to show which port status has been changed.
169 * bit 0: reserved or used for another purpose?
170 * bit 1: the status of port 0 has been changed.
171 * bit 2: the status of port 1 has been changed.
173 * bit 7: the status of port 6 has been changed.
174 * bit 8: the status of port 7 has been changed.
176 * bit 15: the status of port 14 has been changed.
178 * So, the maximum number of ports is 31 ( port 0 to port 30) ?
180 * The return value is the actual transferred length in byte. If nothing has
181 * been changed, return 0. In the case that the number of ports is less than or
182 * equal to 6 (VHCI_NPORTS==7), return 1.
185 static int vhci_hub_status(struct usb_hcd
*hcd
, char *buf
)
187 struct vhci_hcd
*vhci
;
191 /* the enough buffer is allocated according to USB_MAXCHILDREN */
192 unsigned long *event_bits
= (unsigned long *) buf
;
198 vhci
= hcd_to_vhci(hcd
);
200 spin_lock_irqsave(&vhci
->lock
, flags
);
201 if (!HCD_HW_ACCESSIBLE(hcd
)) {
202 usbip_dbg_vhci_rh("hw accessible flag in on?\n");
206 /* check pseudo status register for each port */
207 for (rhport
= 0; rhport
< VHCI_NPORTS
; rhport
++) {
208 if ((vhci
->port_status
[rhport
] & PORT_C_MASK
)) {
209 /* The status of a port has been changed, */
210 usbip_dbg_vhci_rh("port %d is changed\n", rhport
);
212 *event_bits
|= 1 << (rhport
+ 1);
217 usbip_uinfo("changed %d\n", changed
);
219 if (hcd
->state
== HC_STATE_SUSPENDED
)
220 usb_hcd_resume_root_hub(hcd
);
223 retval
= 1 + (VHCI_NPORTS
/ 8);
228 spin_unlock_irqrestore(&vhci
->lock
, flags
);
232 /* See hub_configure in hub.c */
233 static inline void hub_descriptor(struct usb_hub_descriptor
*desc
)
235 memset(desc
, 0, sizeof(*desc
));
236 desc
->bDescriptorType
= 0x29;
237 desc
->bDescLength
= 9;
238 desc
->wHubCharacteristics
= (__force __u16
)
239 (__constant_cpu_to_le16(0x0001));
240 desc
->bNbrPorts
= VHCI_NPORTS
;
241 desc
->u
.hs
.DeviceRemovable
[0] = 0xff;
242 desc
->u
.hs
.DeviceRemovable
[1] = 0xff;
245 static int vhci_hub_control(struct usb_hcd
*hcd
, u16 typeReq
, u16 wValue
,
246 u16 wIndex
, char *buf
, u16 wLength
)
248 struct vhci_hcd
*dum
;
253 u32 prev_port_status
[VHCI_NPORTS
];
255 if (!HCD_HW_ACCESSIBLE(hcd
))
260 * wIndex shows the port number and begins from 1.
262 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq
, wValue
,
264 if (wIndex
> VHCI_NPORTS
)
265 printk(KERN_ERR
"%s: invalid port number %d\n", __func__
,
267 rhport
= ((__u8
)(wIndex
& 0x00ff)) - 1;
269 dum
= hcd_to_vhci(hcd
);
271 spin_lock_irqsave(&dum
->lock
, flags
);
273 /* store old status and compare now and old later */
274 if (usbip_dbg_flag_vhci_rh
) {
276 for (i
= 0; i
< VHCI_NPORTS
; i
++)
277 prev_port_status
[i
] = dum
->port_status
[i
];
281 case ClearHubFeature
:
282 usbip_dbg_vhci_rh(" ClearHubFeature\n");
284 case ClearPortFeature
:
286 case USB_PORT_FEAT_SUSPEND
:
287 if (dum
->port_status
[rhport
] & USB_PORT_STAT_SUSPEND
) {
288 /* 20msec signaling */
291 jiffies
+ msecs_to_jiffies(20);
294 case USB_PORT_FEAT_POWER
:
295 usbip_dbg_vhci_rh(" ClearPortFeature: "
296 "USB_PORT_FEAT_POWER\n");
297 dum
->port_status
[rhport
] = 0;
298 /* dum->address = 0; */
302 case USB_PORT_FEAT_C_RESET
:
303 usbip_dbg_vhci_rh(" ClearPortFeature: "
304 "USB_PORT_FEAT_C_RESET\n");
305 switch (dum
->vdev
[rhport
].speed
) {
307 dum
->port_status
[rhport
] |=
308 USB_PORT_STAT_HIGH_SPEED
;
311 dum
->port_status
[rhport
] |=
312 USB_PORT_STAT_LOW_SPEED
;
318 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
320 dum
->port_status
[rhport
] &= ~(1 << wValue
);
324 case GetHubDescriptor
:
325 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
326 hub_descriptor((struct usb_hub_descriptor
*) buf
);
329 usbip_dbg_vhci_rh(" GetHubStatus\n");
330 *(__le32
*) buf
= __constant_cpu_to_le32(0);
333 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex
);
334 if (wIndex
> VHCI_NPORTS
|| wIndex
< 1) {
335 printk(KERN_ERR
"%s: invalid port number %d\n",
340 /* we do no care of resume. */
342 /* whoever resets or resumes must GetPortStatus to
345 if (dum
->resuming
&& time_after(jiffies
, dum
->re_timeout
)) {
346 printk(KERN_ERR
"%s: not yet\n", __func__
);
347 dum
->port_status
[rhport
] |=
348 (1 << USB_PORT_FEAT_C_SUSPEND
);
349 dum
->port_status
[rhport
] &=
350 ~(1 << USB_PORT_FEAT_SUSPEND
);
353 /* if (dum->driver && dum->driver->resume) {
354 * spin_unlock (&dum->lock);
355 * dum->driver->resume (&dum->gadget);
356 * spin_lock (&dum->lock);
360 if ((dum
->port_status
[rhport
] & (1 << USB_PORT_FEAT_RESET
)) !=
361 0 && time_after(jiffies
, dum
->re_timeout
)) {
362 dum
->port_status
[rhport
] |=
363 (1 << USB_PORT_FEAT_C_RESET
);
364 dum
->port_status
[rhport
] &=
365 ~(1 << USB_PORT_FEAT_RESET
);
368 if (dum
->vdev
[rhport
].ud
.status
==
369 VDEV_ST_NOTASSIGNED
) {
370 usbip_dbg_vhci_rh(" enable rhport %d "
373 dum
->vdev
[rhport
].ud
.status
);
374 dum
->port_status
[rhport
] |=
375 USB_PORT_STAT_ENABLE
;
379 dum
->port_status
[rhport
] |=
380 USB_PORT_STAT_ENABLE
;
381 /* give it the best speed we agree on */
382 dum
->gadget
.speed
= dum
->driver
->speed
;
383 dum
->gadget
.ep0
->maxpacket
= 64;
384 switch (dum
->gadget
.speed
) {
386 dum
->port_status
[rhport
] |=
387 USB_PORT_STAT_HIGH_SPEED
;
390 dum
->gadget
.ep0
->maxpacket
= 8;
391 dum
->port_status
[rhport
] |=
392 USB_PORT_STAT_LOW_SPEED
;
395 dum
->gadget
.speed
= USB_SPEED_FULL
;
401 ((u16
*) buf
)[0] = cpu_to_le16(dum
->port_status
[rhport
]);
402 ((u16
*) buf
)[1] = cpu_to_le16(dum
->port_status
[rhport
] >> 16);
404 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16
*)buf
)[0],
408 usbip_dbg_vhci_rh(" SetHubFeature\n");
413 case USB_PORT_FEAT_SUSPEND
:
414 usbip_dbg_vhci_rh(" SetPortFeature: "
415 "USB_PORT_FEAT_SUSPEND\n");
416 printk(KERN_ERR
"%s: not yet\n", __func__
);
418 dum
->port_status
[rhport
] |=
419 (1 << USB_PORT_FEAT_SUSPEND
);
420 if (dum
->driver
->suspend
) {
421 spin_unlock(&dum
->lock
);
422 dum
->driver
->suspend(&dum
->gadget
);
423 spin_lock(&dum
->lock
);
427 case USB_PORT_FEAT_RESET
:
428 usbip_dbg_vhci_rh(" SetPortFeature: "
429 "USB_PORT_FEAT_RESET\n");
430 /* if it's already running, disconnect first */
431 if (dum
->port_status
[rhport
] & USB_PORT_STAT_ENABLE
) {
432 dum
->port_status
[rhport
] &=
433 ~(USB_PORT_STAT_ENABLE
|
434 USB_PORT_STAT_LOW_SPEED
|
435 USB_PORT_STAT_HIGH_SPEED
);
438 dev_dbg(hardware
, "disconnect\n");
439 stop_activity(dum
, dum
->driver
);
443 /* FIXME test that code path! */
445 /* 50msec reset signaling */
446 dum
->re_timeout
= jiffies
+ msecs_to_jiffies(50);
450 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
452 dum
->port_status
[rhport
] |= (1 << wValue
);
458 printk(KERN_ERR
"%s: default: no such request\n", __func__
);
459 /* dev_dbg (hardware,
460 * "hub control req%04x v%04x i%04x l%d\n",
461 * typeReq, wValue, wIndex, wLength); */
463 /* "protocol stall" on error */
467 if (usbip_dbg_flag_vhci_rh
) {
468 printk(KERN_DEBUG
"port %d\n", rhport
);
469 dump_port_status(prev_port_status
[rhport
]);
470 dump_port_status(dum
->port_status
[rhport
]);
472 usbip_dbg_vhci_rh(" bye\n");
474 spin_unlock_irqrestore(&dum
->lock
, flags
);
479 static struct vhci_device
*get_vdev(struct usb_device
*udev
)
486 for (i
= 0; i
< VHCI_NPORTS
; i
++)
487 if (the_controller
->vdev
[i
].udev
== udev
)
488 return port_to_vdev(i
);
493 static void vhci_tx_urb(struct urb
*urb
)
495 struct vhci_device
*vdev
= get_vdev(urb
->dev
);
496 struct vhci_priv
*priv
;
500 err("could not get virtual device");
505 priv
= kzalloc(sizeof(struct vhci_priv
), GFP_ATOMIC
);
507 spin_lock_irqsave(&vdev
->priv_lock
, flag
);
510 dev_err(&urb
->dev
->dev
, "malloc vhci_priv\n");
511 spin_unlock_irqrestore(&vdev
->priv_lock
, flag
);
512 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_MALLOC
);
516 priv
->seqnum
= atomic_inc_return(&the_controller
->seqnum
);
517 if (priv
->seqnum
== 0xffff)
518 usbip_uinfo("seqnum max\n");
523 urb
->hcpriv
= (void *) priv
;
525 list_add_tail(&priv
->list
, &vdev
->priv_tx
);
527 wake_up(&vdev
->waitq_tx
);
528 spin_unlock_irqrestore(&vdev
->priv_lock
, flag
);
531 static int vhci_urb_enqueue(struct usb_hcd
*hcd
, struct urb
*urb
,
534 struct device
*dev
= &urb
->dev
->dev
;
537 struct vhci_device
*vdev
;
539 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
540 hcd
, urb
, mem_flags
);
542 /* patch to usb_sg_init() is in 2.5.60 */
543 BUG_ON(!urb
->transfer_buffer
&& urb
->transfer_buffer_length
);
545 spin_lock_irqsave(&the_controller
->lock
, flags
);
547 if (urb
->status
!= -EINPROGRESS
) {
548 dev_err(dev
, "URB already unlinked!, status %d\n", urb
->status
);
549 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
553 vdev
= port_to_vdev(urb
->dev
->portnum
-1);
555 /* refuse enqueue for dead connection */
556 spin_lock(&vdev
->ud
.lock
);
557 if (vdev
->ud
.status
== VDEV_ST_NULL
||
558 vdev
->ud
.status
== VDEV_ST_ERROR
) {
559 usbip_uerr("enqueue for inactive port %d\n", vdev
->rhport
);
560 spin_unlock(&vdev
->ud
.lock
);
561 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
564 spin_unlock(&vdev
->ud
.lock
);
566 ret
= usb_hcd_link_urb_to_ep(hcd
, urb
);
571 * The enumeration process is as follows;
573 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
574 * to get max packet length of default pipe
576 * 2. Set_Address request to DevAddr(0) EndPoint(0)
579 if (usb_pipedevice(urb
->pipe
) == 0) {
580 __u8 type
= usb_pipetype(urb
->pipe
);
581 struct usb_ctrlrequest
*ctrlreq
=
582 (struct usb_ctrlrequest
*) urb
->setup_packet
;
584 if (type
!= PIPE_CONTROL
|| !ctrlreq
) {
585 dev_err(dev
, "invalid request to devnum 0\n");
590 switch (ctrlreq
->bRequest
) {
591 case USB_REQ_SET_ADDRESS
:
592 /* set_address may come when a device is reset */
593 dev_info(dev
, "SetAddress Request (%d) to port %d\n",
594 ctrlreq
->wValue
, vdev
->rhport
);
597 usb_put_dev(vdev
->udev
);
598 vdev
->udev
= usb_get_dev(urb
->dev
);
600 spin_lock(&vdev
->ud
.lock
);
601 vdev
->ud
.status
= VDEV_ST_USED
;
602 spin_unlock(&vdev
->ud
.lock
);
604 if (urb
->status
== -EINPROGRESS
) {
605 /* This request is successfully completed. */
606 /* If not -EINPROGRESS, possibly unlinked. */
612 case USB_REQ_GET_DESCRIPTOR
:
613 if (ctrlreq
->wValue
== (USB_DT_DEVICE
<< 8))
614 usbip_dbg_vhci_hc("Not yet?: "
615 "Get_Descriptor to device 0 "
616 "(get max pipe size)\n");
619 usb_put_dev(vdev
->udev
);
620 vdev
->udev
= usb_get_dev(urb
->dev
);
625 dev_err(dev
, "invalid request to devnum 0 bRequest %u, "
626 "wValue %u\n", ctrlreq
->bRequest
,
636 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
641 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
643 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
645 usb_hcd_giveback_urb(vhci_to_hcd(the_controller
), urb
, urb
->status
);
651 * vhci_rx gives back the urb after receiving the reply of the urb. If an
652 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
653 * back its urb. For the driver unlinking the urb, the content of the urb is
654 * not important, but the calling to its completion handler is important; the
655 * completion of unlinking is notified by the completion handler.
660 * - When vhci_hcd receives RET_SUBMIT,
662 * - case 1a). the urb of the pdu is not unlinking.
664 * => just give back the urb
666 * - case 1b). the urb of the pdu is unlinking.
667 * - usbip.ko will return a reply of the unlinking request.
668 * => give back the urb now and go to case 2b).
670 * - When vhci_hcd receives RET_UNLINK,
672 * - case 2a). a submit request is still pending in vhci_hcd.
673 * - urb was really pending in usbip.ko and urb_unlink_urb() was
675 * => free a pending submit request
676 * => notify unlink completeness by giving back the urb
678 * - case 2b). a submit request is *not* pending in vhci_hcd.
679 * - urb was already given back to the core driver.
680 * => do not give back the urb
685 * - When usbip receives CMD_UNLINK,
687 * - case 3a). the urb of the unlink request is now in submission.
688 * => do usb_unlink_urb().
689 * => after the unlink is completed, send RET_UNLINK.
691 * - case 3b). the urb of the unlink request is not in submission.
692 * - may be already completed or never be received
696 static int vhci_urb_dequeue(struct usb_hcd
*hcd
, struct urb
*urb
, int status
)
699 struct vhci_priv
*priv
;
700 struct vhci_device
*vdev
;
702 usbip_uinfo("vhci_hcd: dequeue a urb %p\n", urb
);
704 spin_lock_irqsave(&the_controller
->lock
, flags
);
708 /* URB was never linked! or will be soon given back by
710 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
716 ret
= usb_hcd_check_unlink_urb(hcd
, urb
, status
);
718 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
723 /* send unlink request here? */
726 if (!vdev
->ud
.tcp_socket
) {
727 /* tcp connection is closed */
728 unsigned long flags2
;
730 spin_lock_irqsave(&vdev
->priv_lock
, flags2
);
732 usbip_uinfo("vhci_hcd: device %p seems to be disconnected\n",
734 list_del(&priv
->list
);
738 spin_unlock_irqrestore(&vdev
->priv_lock
, flags2
);
741 * If tcp connection is alive, we have sent CMD_UNLINK.
742 * vhci_rx will receive RET_UNLINK and give back the URB.
743 * Otherwise, we give back it here.
745 usbip_uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n",
748 usb_hcd_unlink_urb_from_ep(hcd
, urb
);
750 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
751 usb_hcd_giveback_urb(vhci_to_hcd(the_controller
), urb
,
753 spin_lock_irqsave(&the_controller
->lock
, flags
);
756 /* tcp connection is alive */
757 unsigned long flags2
;
758 struct vhci_unlink
*unlink
;
760 spin_lock_irqsave(&vdev
->priv_lock
, flags2
);
762 /* setup CMD_UNLINK pdu */
763 unlink
= kzalloc(sizeof(struct vhci_unlink
), GFP_ATOMIC
);
765 usbip_uerr("malloc vhci_unlink\n");
766 spin_unlock_irqrestore(&vdev
->priv_lock
, flags2
);
767 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
768 usbip_event_add(&vdev
->ud
, VDEV_EVENT_ERROR_MALLOC
);
772 unlink
->seqnum
= atomic_inc_return(&the_controller
->seqnum
);
773 if (unlink
->seqnum
== 0xffff)
774 usbip_uinfo("seqnum max\n");
776 unlink
->unlink_seqnum
= priv
->seqnum
;
778 usbip_uinfo("vhci_hcd: device %p seems to be still connected\n",
781 /* send cmd_unlink and try to cancel the pending URB in the
783 list_add_tail(&unlink
->list
, &vdev
->unlink_tx
);
784 wake_up(&vdev
->waitq_tx
);
786 spin_unlock_irqrestore(&vdev
->priv_lock
, flags2
);
789 spin_unlock_irqrestore(&the_controller
->lock
, flags
);
791 usbip_dbg_vhci_hc("leave\n");
795 static void vhci_device_unlink_cleanup(struct vhci_device
*vdev
)
797 struct vhci_unlink
*unlink
, *tmp
;
799 spin_lock(&vdev
->priv_lock
);
801 list_for_each_entry_safe(unlink
, tmp
, &vdev
->unlink_tx
, list
) {
802 usbip_uinfo("unlink cleanup tx %lu\n", unlink
->unlink_seqnum
);
803 list_del(&unlink
->list
);
807 list_for_each_entry_safe(unlink
, tmp
, &vdev
->unlink_rx
, list
) {
810 /* give back URB of unanswered unlink request */
811 usbip_uinfo("unlink cleanup rx %lu\n", unlink
->unlink_seqnum
);
813 urb
= pickup_urb_and_free_priv(vdev
, unlink
->unlink_seqnum
);
815 usbip_uinfo("the urb (seqnum %lu) was already given "
816 "back\n", unlink
->unlink_seqnum
);
817 list_del(&unlink
->list
);
822 urb
->status
= -ENODEV
;
824 spin_lock(&the_controller
->lock
);
825 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller
), urb
);
826 spin_unlock(&the_controller
->lock
);
828 usb_hcd_giveback_urb(vhci_to_hcd(the_controller
), urb
,
831 list_del(&unlink
->list
);
835 spin_unlock(&vdev
->priv_lock
);
839 * The important thing is that only one context begins cleanup.
840 * This is why error handling and cleanup become simple.
841 * We do not want to consider race condition as possible.
843 static void vhci_shutdown_connection(struct usbip_device
*ud
)
845 struct vhci_device
*vdev
= container_of(ud
, struct vhci_device
, ud
);
847 /* need this? see stub_dev.c */
848 if (ud
->tcp_socket
) {
849 usbip_udbg("shutdown tcp_socket %p\n", ud
->tcp_socket
);
850 kernel_sock_shutdown(ud
->tcp_socket
, SHUT_RDWR
);
853 /* kill threads related to this sdev, if v.c. exists */
854 kthread_stop(vdev
->ud
.tcp_rx
);
855 kthread_stop(vdev
->ud
.tcp_tx
);
857 usbip_uinfo("stop threads\n");
859 /* active connection is closed */
860 if (vdev
->ud
.tcp_socket
!= NULL
) {
861 sock_release(vdev
->ud
.tcp_socket
);
862 vdev
->ud
.tcp_socket
= NULL
;
864 usbip_uinfo("release socket\n");
866 vhci_device_unlink_cleanup(vdev
);
869 * rh_port_disconnect() is a trigger of ...
870 * usb_disable_device():
871 * disable all the endpoints for a USB device.
872 * usb_disable_endpoint():
873 * disable endpoints. pending urbs are unlinked(dequeued).
875 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
876 * deteched device should release used urbs in a cleanup function(i.e.
877 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
878 * pushed urbs and their private data in this function.
880 * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
881 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
882 * gives back pushed urbs and frees their private data by request of
883 * the cleanup function of a USB driver. When unlinking a urb with an
884 * active connection, vhci_dequeue() does not give back the urb which
885 * is actually given back by vhci_rx after receiving its return pdu.
888 rh_port_disconnect(vdev
->rhport
);
890 usbip_uinfo("disconnect device\n");
894 static void vhci_device_reset(struct usbip_device
*ud
)
896 struct vhci_device
*vdev
= container_of(ud
, struct vhci_device
, ud
);
898 spin_lock(&ud
->lock
);
904 usb_put_dev(vdev
->udev
);
907 ud
->tcp_socket
= NULL
;
908 ud
->status
= VDEV_ST_NULL
;
910 spin_unlock(&ud
->lock
);
913 static void vhci_device_unusable(struct usbip_device
*ud
)
915 spin_lock(&ud
->lock
);
916 ud
->status
= VDEV_ST_ERROR
;
917 spin_unlock(&ud
->lock
);
920 static void vhci_device_init(struct vhci_device
*vdev
)
922 memset(vdev
, 0, sizeof(*vdev
));
924 vdev
->ud
.tcp_rx
= kthread_create(vhci_rx_loop
, &vdev
->ud
, "vhci_rx");
925 vdev
->ud
.tcp_tx
= kthread_create(vhci_tx_loop
, &vdev
->ud
, "vhci_tx");
927 vdev
->ud
.side
= USBIP_VHCI
;
928 vdev
->ud
.status
= VDEV_ST_NULL
;
929 /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */
930 spin_lock_init(&vdev
->ud
.lock
);
932 INIT_LIST_HEAD(&vdev
->priv_rx
);
933 INIT_LIST_HEAD(&vdev
->priv_tx
);
934 INIT_LIST_HEAD(&vdev
->unlink_tx
);
935 INIT_LIST_HEAD(&vdev
->unlink_rx
);
936 /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
937 spin_lock_init(&vdev
->priv_lock
);
939 init_waitqueue_head(&vdev
->waitq_tx
);
941 vdev
->ud
.eh_ops
.shutdown
= vhci_shutdown_connection
;
942 vdev
->ud
.eh_ops
.reset
= vhci_device_reset
;
943 vdev
->ud
.eh_ops
.unusable
= vhci_device_unusable
;
945 usbip_start_eh(&vdev
->ud
);
948 static int vhci_start(struct usb_hcd
*hcd
)
950 struct vhci_hcd
*vhci
= hcd_to_vhci(hcd
);
954 usbip_dbg_vhci_hc("enter vhci_start\n");
956 /* initialize private data of usb_hcd */
958 for (rhport
= 0; rhport
< VHCI_NPORTS
; rhport
++) {
959 struct vhci_device
*vdev
= &vhci
->vdev
[rhport
];
960 vhci_device_init(vdev
);
961 vdev
->rhport
= rhport
;
964 atomic_set(&vhci
->seqnum
, 0);
965 spin_lock_init(&vhci
->lock
);
967 hcd
->power_budget
= 0; /* no limit */
968 hcd
->state
= HC_STATE_RUNNING
;
969 hcd
->uses_new_polling
= 1;
971 /* vhci_hcd is now ready to be controlled through sysfs */
972 err
= sysfs_create_group(&vhci_dev(vhci
)->kobj
, &dev_attr_group
);
974 usbip_uerr("create sysfs files\n");
981 static void vhci_stop(struct usb_hcd
*hcd
)
983 struct vhci_hcd
*vhci
= hcd_to_vhci(hcd
);
986 usbip_dbg_vhci_hc("stop VHCI controller\n");
988 /* 1. remove the userland interface of vhci_hcd */
989 sysfs_remove_group(&vhci_dev(vhci
)->kobj
, &dev_attr_group
);
991 /* 2. shutdown all the ports of vhci_hcd */
992 for (rhport
= 0 ; rhport
< VHCI_NPORTS
; rhport
++) {
993 struct vhci_device
*vdev
= &vhci
->vdev
[rhport
];
995 usbip_event_add(&vdev
->ud
, VDEV_EVENT_REMOVED
);
996 usbip_stop_eh(&vdev
->ud
);
999 usbip_uinfo("vhci_stop done\n");
1002 static int vhci_get_frame_number(struct usb_hcd
*hcd
)
1004 usbip_uerr("Not yet implemented\n");
1010 /* FIXME: suspend/resume */
1011 static int vhci_bus_suspend(struct usb_hcd
*hcd
)
1013 struct vhci_hcd
*vhci
= hcd_to_vhci(hcd
);
1015 dev_dbg(&hcd
->self
.root_hub
->dev
, "%s\n", __func__
);
1017 spin_lock_irq(&vhci
->lock
);
1018 /* vhci->rh_state = DUMMY_RH_SUSPENDED;
1019 * set_link_state(vhci); */
1020 hcd
->state
= HC_STATE_SUSPENDED
;
1021 spin_unlock_irq(&vhci
->lock
);
1026 static int vhci_bus_resume(struct usb_hcd
*hcd
)
1028 struct vhci_hcd
*vhci
= hcd_to_vhci(hcd
);
1031 dev_dbg(&hcd
->self
.root_hub
->dev
, "%s\n", __func__
);
1033 spin_lock_irq(&vhci
->lock
);
1034 if (!HCD_HW_ACCESSIBLE(hcd
)) {
1037 /* vhci->rh_state = DUMMY_RH_RUNNING;
1038 * set_link_state(vhci);
1039 * if (!list_empty(&vhci->urbp_list))
1040 * mod_timer(&vhci->timer, jiffies); */
1041 hcd
->state
= HC_STATE_RUNNING
;
1043 spin_unlock_irq(&vhci
->lock
);
1051 #define vhci_bus_suspend NULL
1052 #define vhci_bus_resume NULL
1055 static struct hc_driver vhci_hc_driver
= {
1056 .description
= driver_name
,
1057 .product_desc
= driver_desc
,
1058 .hcd_priv_size
= sizeof(struct vhci_hcd
),
1062 .start
= vhci_start
,
1065 .urb_enqueue
= vhci_urb_enqueue
,
1066 .urb_dequeue
= vhci_urb_dequeue
,
1068 .get_frame_number
= vhci_get_frame_number
,
1070 .hub_status_data
= vhci_hub_status
,
1071 .hub_control
= vhci_hub_control
,
1072 .bus_suspend
= vhci_bus_suspend
,
1073 .bus_resume
= vhci_bus_resume
,
1076 static int vhci_hcd_probe(struct platform_device
*pdev
)
1078 struct usb_hcd
*hcd
;
1081 usbip_uinfo("proving...\n");
1083 usbip_dbg_vhci_hc("name %s id %d\n", pdev
->name
, pdev
->id
);
1085 /* will be removed */
1086 if (pdev
->dev
.dma_mask
) {
1087 dev_info(&pdev
->dev
, "vhci_hcd DMA not supported\n");
1092 * Allocate and initialize hcd.
1093 * Our private data is also allocated automatically.
1095 hcd
= usb_create_hcd(&vhci_hc_driver
, &pdev
->dev
, dev_name(&pdev
->dev
));
1097 usbip_uerr("create hcd failed\n");
1102 /* this is private data for vhci_hcd */
1103 the_controller
= hcd_to_vhci(hcd
);
1106 * Finish generic HCD structure initialization and register.
1107 * Call the driver's reset() and start() routines.
1109 ret
= usb_add_hcd(hcd
, 0, 0);
1111 usbip_uerr("usb_add_hcd failed %d\n", ret
);
1113 the_controller
= NULL
;
1117 usbip_dbg_vhci_hc("bye\n");
1121 static int vhci_hcd_remove(struct platform_device
*pdev
)
1123 struct usb_hcd
*hcd
;
1125 hcd
= platform_get_drvdata(pdev
);
1130 * Disconnects the root hub,
1131 * then reverses the effects of usb_add_hcd(),
1132 * invoking the HCD's stop() methods.
1134 usb_remove_hcd(hcd
);
1136 the_controller
= NULL
;
1143 /* what should happen for USB/IP under suspend/resume? */
1144 static int vhci_hcd_suspend(struct platform_device
*pdev
, pm_message_t state
)
1146 struct usb_hcd
*hcd
;
1151 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
1153 hcd
= platform_get_drvdata(pdev
);
1155 spin_lock(&the_controller
->lock
);
1157 for (rhport
= 0; rhport
< VHCI_NPORTS
; rhport
++)
1158 if (the_controller
->port_status
[rhport
] &
1159 USB_PORT_STAT_CONNECTION
)
1162 spin_unlock(&the_controller
->lock
);
1164 if (connected
> 0) {
1165 usbip_uinfo("We have %d active connection%s. Do not suspend.\n",
1166 connected
, (connected
== 1 ? "" : "s"));
1169 usbip_uinfo("suspend vhci_hcd");
1170 clear_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
1176 static int vhci_hcd_resume(struct platform_device
*pdev
)
1178 struct usb_hcd
*hcd
;
1180 dev_dbg(&pdev
->dev
, "%s\n", __func__
);
1182 hcd
= platform_get_drvdata(pdev
);
1183 set_bit(HCD_FLAG_HW_ACCESSIBLE
, &hcd
->flags
);
1184 usb_hcd_poll_rh_status(hcd
);
1191 #define vhci_hcd_suspend NULL
1192 #define vhci_hcd_resume NULL
1196 static struct platform_driver vhci_driver
= {
1197 .probe
= vhci_hcd_probe
,
1198 .remove
= __devexit_p(vhci_hcd_remove
),
1199 .suspend
= vhci_hcd_suspend
,
1200 .resume
= vhci_hcd_resume
,
1202 .name
= (char *) driver_name
,
1203 .owner
= THIS_MODULE
,
1208 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1209 * We need to add this virtual device as a platform device arbitrarily:
1210 * 1. platform_device_register()
1212 static void the_pdev_release(struct device
*dev
)
1217 static struct platform_device the_pdev
= {
1218 /* should be the same name as driver_name */
1219 .name
= (char *) driver_name
,
1222 /* .driver = &vhci_driver, */
1223 .release
= the_pdev_release
,
1227 static int __init
vhci_init(void)
1231 usbip_dbg_vhci_hc("enter\n");
1235 printk(KERN_INFO KBUILD_MODNAME
": %s, %s\n", driver_name
,
1238 ret
= platform_driver_register(&vhci_driver
);
1240 goto err_driver_register
;
1242 ret
= platform_device_register(&the_pdev
);
1244 goto err_platform_device_register
;
1246 usbip_dbg_vhci_hc("bye\n");
1249 /* error occurred */
1250 err_platform_device_register
:
1251 platform_driver_unregister(&vhci_driver
);
1252 err_driver_register
:
1253 usbip_dbg_vhci_hc("bye\n");
1257 static void __exit
vhci_cleanup(void)
1259 usbip_dbg_vhci_hc("enter\n");
1261 platform_device_unregister(&the_pdev
);
1262 platform_driver_unregister(&vhci_driver
);
1264 usbip_dbg_vhci_hc("bye\n");
1267 module_init(vhci_init
);
1268 module_exit(vhci_cleanup
);
1270 MODULE_AUTHOR(DRIVER_AUTHOR
);
1271 MODULE_DESCRIPTION(DRIVER_DESC
);
1272 MODULE_LICENSE(DRIVER_LICENCE
);