21 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include <linux/slab.h>
30 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
31 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
45 static int vhci_hub_status(
struct usb_hcd *hcd,
char *buff);
46 static int vhci_hub_control(
struct usb_hcd *hcd,
u16 typeReq,
u16 wValue,
48 static int vhci_urb_enqueue(
struct usb_hcd *hcd,
struct urb *
urb,
50 static int vhci_urb_dequeue(
struct usb_hcd *hcd,
struct urb *
urb,
int status);
51 static int vhci_start(
struct usb_hcd *
vhci_hcd);
52 static void vhci_stop(
struct usb_hcd *hcd);
53 static int vhci_get_frame_number(
struct usb_hcd *hcd);
56 static const char driver_desc[] =
"USB/IP Virtual Host Controller";
60 static const char *
const bit_desc[] = {
95 static void dump_port_status_diff(
u32 prev_status,
u32 new_status)
100 pr_debug(
"status prev -> new: %08x -> %08x\n", prev_status, new_status);
103 u32 new = new_status &
bit;
108 else if (prev && !
new)
114 pr_debug(
" %c%s\n", change, bit_desc[i]);
147 spin_unlock_irqrestore(&the_controller->
lock, flags);
169 spin_unlock_irqrestore(&the_controller->
lock, flags);
173 #define PORT_C_MASK \
174 ((USB_PORT_STAT_C_CONNECTION \
175 | USB_PORT_STAT_C_ENABLE \
176 | USB_PORT_STAT_C_SUSPEND \
177 | USB_PORT_STAT_C_OVERCURRENT \
178 | USB_PORT_STAT_C_RESET) << 16)
191 static int vhci_hub_status(
struct usb_hcd *hcd,
char *
buf)
202 vhci = hcd_to_vhci(hcd);
205 if (!HCD_HW_ACCESSIBLE(hcd)) {
216 buf[(rhport + 1) / 8] |= 1 << (rhport + 1) % 8;
221 pr_info(
"changed %d\n", changed);
223 if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
224 usb_hcd_resume_root_hub(hcd);
227 spin_unlock_irqrestore(&vhci->
lock, flags);
228 return changed ? retval : 0;
234 memset(desc, 0,
sizeof(*desc));
244 static int vhci_hub_control(
struct usb_hcd *hcd,
u16 typeReq,
u16 wValue,
254 if (!HCD_HW_ACCESSIBLE(hcd))
264 pr_err(
"invalid port number %d\n", wIndex);
265 rhport = ((
__u8)(wIndex & 0x00ff)) - 1;
267 dum = hcd_to_vhci(hcd);
274 sizeof(prev_port_status));
278 case ClearHubFeature:
281 case ClearPortFeature:
293 "USB_PORT_FEAT_POWER\n");
301 "USB_PORT_FEAT_C_RESET\n");
302 switch (dum->
vdev[rhport].speed) {
321 case GetHubDescriptor:
332 pr_err(
"invalid port number %d\n", wIndex);
363 if (dum->
vdev[rhport].ud.status ==
368 dum->
vdev[rhport].ud.status);
387 "USB_PORT_FEAT_SUSPEND\n");
391 "USB_PORT_FEAT_RESET\n");
395 ~(USB_PORT_STAT_ENABLE |
413 pr_err(
"default: no such request\n");
426 dump_port_status_diff(prev_port_status[rhport],
432 spin_unlock_irqrestore(&dum->
lock, flags);
445 if (the_controller->
vdev[i].udev == udev)
446 return port_to_vdev(i);
451 static void vhci_tx_urb(
struct urb *
urb)
458 pr_err(
"could not get virtual device");
468 dev_err(&urb->dev->dev,
"malloc vhci_priv\n");
469 spin_unlock_irqrestore(&vdev->
priv_lock, flag);
475 if (priv->
seqnum == 0xffff)
476 dev_info(&urb->dev->dev,
"seqnum max\n");
481 urb->hcpriv = (
void *) priv;
486 spin_unlock_irqrestore(&vdev->
priv_lock, flag);
489 static int vhci_urb_enqueue(
struct usb_hcd *hcd,
struct urb *urb,
498 hcd, urb, mem_flags);
501 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
506 dev_err(dev,
"URB already unlinked!, status %d\n", urb->status);
507 spin_unlock_irqrestore(&the_controller->
lock, flags);
511 vdev = port_to_vdev(urb->dev->portnum-1);
514 spin_lock(&vdev->
ud.lock);
517 dev_err(dev,
"enqueue for inactive port %d\n", vdev->
rhport);
518 spin_unlock(&vdev->
ud.lock);
519 spin_unlock_irqrestore(&the_controller->
lock, flags);
522 spin_unlock(&vdev->
ud.lock);
537 if (usb_pipedevice(urb->pipe) == 0) {
538 __u8 type = usb_pipetype(urb->pipe);
542 if (type != PIPE_CONTROL || !ctrlreq) {
543 dev_err(dev,
"invalid request to devnum 0\n");
551 dev_info(dev,
"SetAddress Request (%d) to port %d\n",
558 spin_lock(&vdev->
ud.lock);
560 spin_unlock(&vdev->
ud.lock);
573 "Get_Descriptor to device 0 "
574 "(get max pipe size)\n");
583 dev_err(dev,
"invalid request to devnum 0 bRequest %u, "
594 spin_unlock_irqrestore(&the_controller->
lock, flags);
601 spin_unlock_irqrestore(&the_controller->
lock, flags);
652 static int vhci_urb_dequeue(
struct usb_hcd *hcd,
struct urb *urb,
int status)
658 pr_info(
"dequeue a urb %p\n", urb);
666 spin_unlock_irqrestore(&the_controller->
lock, flags);
674 spin_unlock_irqrestore(&the_controller->
lock, flags);
682 if (!vdev->
ud.tcp_socket) {
688 pr_info(
"device %p seems to be disconnected\n", vdev);
693 spin_unlock_irqrestore(&vdev->
priv_lock, flags2);
700 pr_info(
"gives back urb %p\n", urb);
704 spin_unlock_irqrestore(&the_controller->
lock, flags);
719 pr_err(
"malloc vhci_unlink\n");
720 spin_unlock_irqrestore(&vdev->
priv_lock, flags2);
721 spin_unlock_irqrestore(&the_controller->
lock, flags);
727 if (unlink->
seqnum == 0xffff)
732 pr_info(
"device %p seems to be still connected\n", vdev);
739 spin_unlock_irqrestore(&vdev->
priv_lock, flags2);
742 spin_unlock_irqrestore(&the_controller->
lock, flags);
748 static void vhci_device_unlink_cleanup(
struct vhci_device *vdev)
752 spin_lock(&the_controller->
lock);
772 pr_info(
"the urb (seqnum %lu) was already given back\n",
786 spin_unlock(&the_controller->
lock);
791 spin_lock(&the_controller->
lock);
798 spin_unlock(&the_controller->
lock);
817 if (vdev->
ud.tcp_rx) {
821 if (vdev->
ud.tcp_tx) {
828 if (vdev->
ud.tcp_socket !=
NULL) {
830 vdev->
ud.tcp_socket =
NULL;
834 vhci_device_unlink_cleanup(vdev);
858 pr_info(
"disconnect device\n");
866 spin_lock(&ud->
lock);
878 spin_unlock(&ud->
lock);
881 static void vhci_device_unusable(
struct usbip_device *ud)
883 spin_lock(&ud->
lock);
885 spin_unlock(&ud->
lock);
888 static void vhci_device_init(
struct vhci_device *vdev)
890 memset(vdev, 0,
sizeof(*vdev));
896 INIT_LIST_HEAD(&vdev->
priv_rx);
897 INIT_LIST_HEAD(&vdev->
priv_tx);
904 vdev->
ud.eh_ops.shutdown = vhci_shutdown_connection;
905 vdev->
ud.eh_ops.reset = vhci_device_reset;
906 vdev->
ud.eh_ops.unusable = vhci_device_unusable;
911 static int vhci_start(
struct usb_hcd *hcd)
913 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
923 vhci_device_init(vdev);
930 hcd->power_budget = 0;
931 hcd->state = HC_STATE_RUNNING;
932 hcd->uses_new_polling = 1;
937 pr_err(
"create sysfs files\n");
944 static void vhci_stop(
struct usb_hcd *hcd)
946 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
955 for (rhport = 0 ; rhport <
VHCI_NPORTS; rhport++) {
963 static int vhci_get_frame_number(
struct usb_hcd *hcd)
965 pr_err(
"Not yet implemented\n");
974 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
976 dev_dbg(&hcd->self.root_hub->dev,
"%s\n", __func__);
978 spin_lock_irq(&vhci->
lock);
981 hcd->state = HC_STATE_SUSPENDED;
982 spin_unlock_irq(&vhci->
lock);
989 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
992 dev_dbg(&hcd->self.root_hub->dev,
"%s\n", __func__);
994 spin_lock_irq(&vhci->
lock);
995 if (!HCD_HW_ACCESSIBLE(hcd)) {
1002 hcd->state = HC_STATE_RUNNING;
1004 spin_unlock_irq(&vhci->
lock);
1011 #define vhci_bus_suspend NULL
1012 #define vhci_bus_resume NULL
1015 static struct hc_driver vhci_hc_driver = {
1017 .product_desc = driver_desc,
1018 .hcd_priv_size =
sizeof(
struct vhci_hcd),
1022 .start = vhci_start,
1025 .urb_enqueue = vhci_urb_enqueue,
1026 .urb_dequeue = vhci_urb_dequeue,
1028 .get_frame_number = vhci_get_frame_number,
1030 .hub_status_data = vhci_hub_status,
1031 .hub_control = vhci_hub_control,
1038 struct usb_hcd *hcd;
1044 if (pdev->
dev.dma_mask) {
1045 dev_info(&pdev->
dev,
"vhci_hcd DMA not supported\n");
1055 pr_err(
"create hcd failed\n");
1061 the_controller = hcd_to_vhci(hcd);
1069 pr_err(
"usb_add_hcd failed %d\n", ret);
1071 the_controller =
NULL;
1081 struct usb_hcd *hcd;
1083 hcd = platform_get_drvdata(pdev);
1094 the_controller =
NULL;
1104 struct usb_hcd *hcd;
1109 hcd = platform_get_drvdata(pdev);
1111 spin_lock(&the_controller->
lock);
1118 spin_unlock(&the_controller->
lock);
1120 if (connected > 0) {
1121 dev_info(&pdev->
dev,
"We have %d active connection%s. Do not "
1122 "suspend.\n", connected, (connected == 1 ?
"" :
"s"));
1126 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1134 struct usb_hcd *hcd;
1138 hcd = platform_get_drvdata(pdev);
1139 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1147 #define vhci_hcd_suspend NULL
1148 #define vhci_hcd_resume NULL
1153 .probe = vhci_hcd_probe,
1168 static void the_pdev_release(
struct device *dev)
1183 static int __init vhci_hcd_init(
void)
1192 goto err_driver_register;
1196 goto err_platform_device_register;
1201 err_platform_device_register:
1203 err_driver_register:
1207 static void __exit vhci_hcd_exit(
void)