/* some members of urb must be substituted before. */ int usbip_recv_iso(struct usbip_device *ud, struct urb *urb) { void *buff; struct usbip_iso_packet_descriptor *iso; int np = urb->number_of_packets; int size = np * sizeof(*iso); int i; int ret; int total_length = 0; if (!usb_pipeisoc(urb->pipe)) return 0; /* my Bluetooth dongle gets ISO URBs which are np = 0 */ if (np == 0) return 0; buff = kzalloc(size, GFP_KERNEL); if (!buff) return -ENOMEM; ret = usbip_recv(ud->tcp_socket, buff, size); if (ret != size) { dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n", ret); kfree(buff); if (ud->side == USBIP_STUB) usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); else usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); return -EPIPE; } iso = (struct usbip_iso_packet_descriptor *) buff; for (i = 0; i < np; i++) { usbip_iso_packet_correct_endian(&iso[i], 0); usbip_pack_iso(&iso[i], &urb->iso_frame_desc[i], 0); total_length += urb->iso_frame_desc[i].actual_length; } kfree(buff); if (total_length != urb->actual_length) { dev_err(&urb->dev->dev, "total length of iso packets %d not equal to actual " "length of buffer %d\n", total_length, urb->actual_length); if (ud->side == USBIP_STUB) usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); else usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); return -EPIPE; } return ret; }
/* recv a pdu */ static void stub_rx_pdu(struct usbip_device *ud) { int ret; struct usbip_header pdu; struct stub_device *sdev = container_of(ud, struct stub_device, ud); dbg_stub_rx("Enter\n"); memset(&pdu, 0, sizeof(pdu)); /* 1. receive a pdu header */ dbg_stub_rx("before usbip_xmit\n"); ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0); dbg_stub_rx("after usbip_xmit\n"); if (ret != sizeof(pdu)) { uerr("recv a header, %d\n", ret); usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); return; } usbip_header_correct_endian(&pdu, 0); if (dbg_flag_stub_rx) usbip_dump_header(&pdu); if (!valid_request(sdev, &pdu)) { uerr("recv invalid request\n"); usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); return; } #if CLIENT > 2 sdev->heartbeat_pending = 0; #endif switch (pdu.base.command) { case USBIP_CMD_UNLINK: stub_recv_cmd_unlink(sdev, &pdu); break; case USBIP_CMD_SUBMIT: stub_recv_cmd_submit(sdev, &pdu); break; #if CLIENT > 2 // added by tf, 110530 case USBIP_HEARTBEAT_REPLY: sdev->heartbeat_pending = 0; break; // end --- added #endif default: /* NOTREACHED */ uerr("unknown pdu\n"); usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); return; } }
/* recv a pdu */ static void stub_rx_pdu(struct usbip_device *ud) { int ret; struct usbip_header pdu; struct stub_device *sdev = container_of(ud, struct stub_device, ud); dbg_stub_rx("Enter\n"); memset(&pdu, 0, sizeof(pdu)); /* 1. receive a pdu header */ ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu),0); if (ret != sizeof(pdu)) { uerr("recv a header, %d\n", ret); usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); return; } usbip_header_correct_endian(&pdu, 0); if (dbg_flag_stub_rx) usbip_dump_header(&pdu); if (!valid_request(sdev, &pdu)) { uerr("recv invalid request\n"); usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); return; } switch (pdu.base.command) { case USBIP_CMD_UNLINK: stub_recv_cmd_unlink(sdev, &pdu); break; case USBIP_CMD_SUBMIT: stub_recv_cmd_submit(sdev, &pdu); break; default: /* NOTREACHED */ uerr("unknown pdu\n"); usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); return; } }
/* Sysfs entry to shutdown a virtual connection */ static int vhci_port_disconnect(__u32 rhport) { struct vhci_device *vdev; usbip_dbg_vhci_sysfs("enter\n"); /* lock */ spin_lock(&the_controller->lock); vdev = port_to_vdev(rhport); spin_lock(&vdev->ud.lock); if (vdev->ud.status == VDEV_ST_NULL) { pr_err("not connected %d\n", vdev->ud.status); /* unlock */ spin_unlock(&vdev->ud.lock); spin_unlock(&the_controller->lock); return -EINVAL; } /* unlock */ spin_unlock(&vdev->ud.lock); spin_unlock(&the_controller->lock); usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN); return 0; }
/* Sysfs entry to shutdown a virtual connection */ static int vhci_port_disconnect(struct vhci_hcd *vhci_hcd, __u32 rhport) { struct vhci_device *vdev = &vhci_hcd->vdev[rhport]; struct vhci *vhci = vhci_hcd->vhci; unsigned long flags; usbip_dbg_vhci_sysfs("enter\n"); /* lock */ spin_lock_irqsave(&vhci->lock, flags); spin_lock(&vdev->ud.lock); if (vdev->ud.status == VDEV_ST_NULL) { pr_err("not connected %d\n", vdev->ud.status); /* unlock */ spin_unlock(&vdev->ud.lock); spin_unlock_irqrestore(&vhci->lock, flags); return -EINVAL; } /* unlock */ spin_unlock(&vdev->ud.lock); spin_unlock_irqrestore(&vhci->lock, flags); usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN); return 0; }
static struct stub_priv *stub_priv_alloc(struct stub_device *sdev, struct usbip_header *pdu) { struct stub_priv *priv; struct usbip_device *ud = &sdev->ud; unsigned long flags; spin_lock_irqsave(&sdev->priv_lock, flags); priv = kmem_cache_alloc(stub_priv_cache, GFP_ATOMIC); if (!priv) { uerr("alloc stub_priv\n"); spin_unlock_irqrestore(&sdev->priv_lock, flags); usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC); return NULL; } memset(priv, 0, sizeof(struct stub_priv)); priv->seqnum = pdu->base.seqnum; priv->sdev = sdev; /* * After a stub_priv is linked to a list_head, * our error handler can free allocated data. */ list_add_tail(&priv->list, &sdev->priv_init); spin_unlock_irqrestore(&sdev->priv_lock, flags); return priv; }
/* some members of urb must be substituted before. */ int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb) { int ret; int size; if (ud->side == USBIP_STUB) { /* the direction of urb must be OUT. */ if (usb_pipein(urb->pipe)) return 0; size = urb->transfer_buffer_length; } else { /* the direction of urb must be IN. */ if (usb_pipeout(urb->pipe)) return 0; size = urb->actual_length; } /* no need to recv xbuff */ if (!(size > 0)) return 0; if (size > urb->transfer_buffer_length) { /* should not happen, probably malicious packet */ if (ud->side == USBIP_STUB) { usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); return 0; } else { usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); return -EPIPE; } } ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size); if (ret != size) { dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret); if (ud->side == USBIP_STUB) { usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); } else { usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); return -EPIPE; } } return ret; }
/* some members of urb must be substituted before. */ int usbip_recv_iso(struct usbip_device *ud, struct urb *urb) { void *buff; struct usbip_iso_packet_descriptor *iso; int np = urb->number_of_packets; int size = np * sizeof(*iso); int i; int ret; if (!usb_pipeisoc(urb->pipe)) return 0; buff = kzalloc(size, GFP_KERNEL); if (!buff) return -ENOMEM; ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0); if (ret != size) { uerr("recv iso_frame_descriptor, %d\n", ret); kfree(buff); if (ud->side == USBIP_STUB) usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); else usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); return -EPIPE; } for (i = 0; i < np; i++) { iso = buff + (i * sizeof(*iso)); usbip_iso_pakcet_correct_endian(iso, 0); usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0); } kfree(buff); return ret; }
/* some members of urb must be substituted before. */ int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb) { int ret; int size; if (ud->side == USBIP_STUB) { /* stub_rx.c */ /* the direction of urb must be OUT. */ if (usb_pipein(urb->pipe)) return 0; size = urb->transfer_buffer_length; } else { /* vhci_rx.c */ /* the direction of urb must be IN. */ if (usb_pipeout(urb->pipe)) return 0; size = urb->actual_length; } /* no need to recv xbuff */ if (!(size > 0)) return 0; ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer, size, 0); if (ret != size) { dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret); if (ud->side == USBIP_STUB) { usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); } else { usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); return -EPIPE; } } return ret; }
int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb) { int ret; int size; if (ud->side == USBIP_STUB) { if (usb_pipein(urb->pipe)) return 0; size = urb->transfer_buffer_length; } else { if (usb_pipeout(urb->pipe)) return 0; size = urb->actual_length; } if (!(size > 0)) return 0; ret = usbip_recv(ud->tcp_socket, urb->transfer_buffer, size); if (ret != size) { dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret); if (ud->side == USBIP_STUB) { usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); } else { usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); return -EPIPE; } } return ret; }
int usbip_recv_iso(struct usbip_device *ud, struct urb *urb) { int ret; char *iso_frame_desc = (char *) &urb->iso_frame_desc[0]; int np = urb->number_of_packets; int size = np * sizeof(struct usb_iso_packet_descriptor); if (!usb_pipeisoc(urb->pipe)) return 0; ret = usbip_xmit(0, ud->tcp_socket, iso_frame_desc, size, 0); if (ret != size ) { uerr("recv iso_frame_descriptor, %d\n", ret); if (ud->side == USBIP_STUB) { usbip_event_add(ud, SDEV_EVENT_ERROR_TCP); } else { usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); } return -EPIPE; } return ret; }
/* be in spin_lock_irqsave(&sdev->priv_lock, flags) */ void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum, __u32 status) { struct stub_unlink *unlink; unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC); if (!unlink) { dev_err(&sdev->interface->dev, "alloc stub_unlink\n"); usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC); return; } unlink->seqnum = seqnum; unlink->status = status; list_add_tail(&unlink->list, &sdev->unlink_tx); }
static void stub_recv_cmd_submit(struct stub_device *sdev, struct usbip_header *pdu) { int ret; struct stub_priv *priv; struct usbip_device *ud = &sdev->ud; struct usb_device *udev = interface_to_usbdev(sdev->interface); int pipe = get_pipe(sdev, pdu->base.ep, pdu->base.direction); priv = stub_priv_alloc(sdev, pdu); if (!priv) return; /* setup a urb */ if (usb_pipeisoc(pipe)) priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets, GFP_KERNEL); else priv->urb = usb_alloc_urb(0, GFP_KERNEL); if (!priv->urb) { uerr("malloc urb\n"); usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC); return; } /* set priv->urb->transfer_buffer */ if (pdu->u.cmd_submit.transfer_buffer_length > 0) { priv->urb->transfer_buffer = kzalloc(pdu->u.cmd_submit.transfer_buffer_length, GFP_KERNEL); if (!priv->urb->transfer_buffer) { uerr("malloc x_buff\n"); usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC); return; } } /* set priv->urb->setup_packet */ priv->urb->setup_packet = kzalloc(8, GFP_KERNEL); if (!priv->urb->setup_packet) { uerr("allocate setup_packet\n"); usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC); return; } memcpy(priv->urb->setup_packet, &pdu->u.cmd_submit.setup, 8); /* set other members from the base header of pdu */ priv->urb->context = (void *) priv; priv->urb->dev = udev; priv->urb->pipe = pipe; priv->urb->complete = stub_complete; usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0); if (usbip_recv_xbuff(ud, priv->urb) < 0) return; if (usbip_recv_iso(ud, priv->urb) < 0) return; /* no need to submit an intercepted request, but harmless? */ tweak_special_requests(priv->urb); /* urb is now ready to submit */ ret = usb_submit_urb(priv->urb, GFP_KERNEL); if (ret == 0) dbg_stub_rx("submit urb ok, seqnum %u\n", pdu->base.seqnum); else { uerr("submit_urb error, %d\n", ret); usbip_dump_header(pdu); usbip_dump_urb(priv->urb); /* * Pessimistic. * This connection will be discarded. */ usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT); } dbg_stub_rx("Leave\n"); return; }