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/init.h>
21 #include <linux/file.h>
22 #include <linux/kernel.h>
23 #include <linux/kthread.h>
24 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
28 #include "usbip_common.h"
31 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
32 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
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
44 /* See usb gadget dummy hcd */
46 static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
47 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
48 u16 wIndex, char *buff, u16 wLength);
49 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
51 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
52 static int vhci_start(struct usb_hcd *vhci_hcd);
53 static void vhci_stop(struct usb_hcd *hcd);
54 static int vhci_get_frame_number(struct usb_hcd *hcd);
56 static const char driver_name[] = "vhci_hcd";
57 static const char driver_desc[] = "USB/IP Virtual Host Controller";
59 struct vhci_hcd *the_controller;
61 static const char * const bit_desc[] = {
78 "C_CONNECTION", /*16*/
81 "C_OVER_CURRENT", /*19*/
96 static void dump_port_status_diff(u32 prev_status, u32 new_status)
101 pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status);
103 u32 prev = prev_status & bit;
104 u32 new = new_status & bit;
109 else if (prev && !new)
115 pr_debug(" %c%s\n", change, bit_desc[i]);
122 void rh_port_connect(int rhport, enum usb_device_speed speed)
126 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
128 spin_lock_irqsave(&the_controller->lock, flags);
130 the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
131 | (1 << USB_PORT_FEAT_C_CONNECTION);
135 the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
138 the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
144 spin_unlock_irqrestore(&the_controller->lock, flags);
146 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
149 static void rh_port_disconnect(int rhport)
153 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
155 spin_lock_irqsave(&the_controller->lock, flags);
157 the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
158 the_controller->port_status[rhport] |=
159 (1 << USB_PORT_FEAT_C_CONNECTION);
161 spin_unlock_irqrestore(&the_controller->lock, flags);
162 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
165 #define PORT_C_MASK \
166 ((USB_PORT_STAT_C_CONNECTION \
167 | USB_PORT_STAT_C_ENABLE \
168 | USB_PORT_STAT_C_SUSPEND \
169 | USB_PORT_STAT_C_OVERCURRENT \
170 | USB_PORT_STAT_C_RESET) << 16)
173 * Returns 0 if the status hasn't changed, or the number of bytes in buf.
174 * Ports are 0-indexed from the HCD point of view,
175 * and 1-indexed from the USB core pointer of view.
177 * @buf: a bitmap to show which port status has been changed.
179 * bit 1: the status of port 0 has been changed.
180 * bit 2: the status of port 1 has been changed.
183 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
185 struct vhci_hcd *vhci;
191 retval = DIV_ROUND_UP(VHCI_NPORTS + 1, 8);
192 memset(buf, 0, retval);
194 vhci = hcd_to_vhci(hcd);
196 spin_lock_irqsave(&vhci->lock, flags);
197 if (!HCD_HW_ACCESSIBLE(hcd)) {
198 usbip_dbg_vhci_rh("hw accessible flag not on?\n");
202 /* check pseudo status register for each port */
203 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
204 if ((vhci->port_status[rhport] & PORT_C_MASK)) {
205 /* The status of a port has been changed, */
206 usbip_dbg_vhci_rh("port %d status changed\n", rhport);
208 buf[(rhport + 1) / 8] |= 1 << (rhport + 1) % 8;
213 pr_info("changed %d\n", changed);
215 if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
216 usb_hcd_resume_root_hub(hcd);
219 spin_unlock_irqrestore(&vhci->lock, flags);
220 return changed ? retval : 0;
223 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
225 memset(desc, 0, sizeof(*desc));
226 desc->bDescriptorType = 0x29;
227 desc->bDescLength = 9;
228 desc->wHubCharacteristics = (__force __u16)
229 (__constant_cpu_to_le16(0x0001));
230 desc->bNbrPorts = VHCI_NPORTS;
231 desc->u.hs.DeviceRemovable[0] = 0xff;
232 desc->u.hs.DeviceRemovable[1] = 0xff;
235 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
236 u16 wIndex, char *buf, u16 wLength)
238 struct vhci_hcd *dum;
243 u32 prev_port_status[VHCI_NPORTS];
245 if (!HCD_HW_ACCESSIBLE(hcd))
250 * wIndex shows the port number and begins from 1.
252 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
254 if (wIndex > VHCI_NPORTS)
255 pr_err("invalid port number %d\n", wIndex);
256 rhport = ((__u8)(wIndex & 0x00ff)) - 1;
258 dum = hcd_to_vhci(hcd);
260 spin_lock_irqsave(&dum->lock, flags);
262 /* store old status and compare now and old later */
263 if (usbip_dbg_flag_vhci_rh) {
264 memcpy(prev_port_status, dum->port_status,
265 sizeof(prev_port_status));
269 case ClearHubFeature:
270 usbip_dbg_vhci_rh(" ClearHubFeature\n");
272 case ClearPortFeature:
274 case USB_PORT_FEAT_SUSPEND:
275 if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
276 /* 20msec signaling */
279 jiffies + msecs_to_jiffies(20);
282 case USB_PORT_FEAT_POWER:
283 usbip_dbg_vhci_rh(" ClearPortFeature: "
284 "USB_PORT_FEAT_POWER\n");
285 dum->port_status[rhport] = 0;
288 case USB_PORT_FEAT_C_RESET:
289 usbip_dbg_vhci_rh(" ClearPortFeature: "
290 "USB_PORT_FEAT_C_RESET\n");
291 switch (dum->vdev[rhport].speed) {
293 dum->port_status[rhport] |=
294 USB_PORT_STAT_HIGH_SPEED;
297 dum->port_status[rhport] |=
298 USB_PORT_STAT_LOW_SPEED;
304 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
306 dum->port_status[rhport] &= ~(1 << wValue);
310 case GetHubDescriptor:
311 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
312 hub_descriptor((struct usb_hub_descriptor *) buf);
315 usbip_dbg_vhci_rh(" GetHubStatus\n");
316 *(__le32 *) buf = __constant_cpu_to_le32(0);
319 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
320 if (wIndex > VHCI_NPORTS || wIndex < 1) {
321 pr_err("invalid port number %d\n", wIndex);
325 /* we do not care about resume. */
327 /* whoever resets or resumes must GetPortStatus to
330 if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
331 dum->port_status[rhport] |=
332 (1 << USB_PORT_FEAT_C_SUSPEND);
333 dum->port_status[rhport] &=
334 ~(1 << USB_PORT_FEAT_SUSPEND);
339 if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
340 0 && time_after(jiffies, dum->re_timeout)) {
341 dum->port_status[rhport] |=
342 (1 << USB_PORT_FEAT_C_RESET);
343 dum->port_status[rhport] &=
344 ~(1 << USB_PORT_FEAT_RESET);
347 if (dum->vdev[rhport].ud.status ==
348 VDEV_ST_NOTASSIGNED) {
349 usbip_dbg_vhci_rh(" enable rhport %d "
352 dum->vdev[rhport].ud.status);
353 dum->port_status[rhport] |=
354 USB_PORT_STAT_ENABLE;
357 ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
358 ((u16 *) buf)[1] = cpu_to_le16(dum->port_status[rhport] >> 16);
360 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
364 usbip_dbg_vhci_rh(" SetHubFeature\n");
369 case USB_PORT_FEAT_SUSPEND:
370 usbip_dbg_vhci_rh(" SetPortFeature: "
371 "USB_PORT_FEAT_SUSPEND\n");
373 case USB_PORT_FEAT_RESET:
374 usbip_dbg_vhci_rh(" SetPortFeature: "
375 "USB_PORT_FEAT_RESET\n");
376 /* if it's already running, disconnect first */
377 if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
378 dum->port_status[rhport] &=
379 ~(USB_PORT_STAT_ENABLE |
380 USB_PORT_STAT_LOW_SPEED |
381 USB_PORT_STAT_HIGH_SPEED);
382 /* FIXME test that code path! */
384 /* 50msec reset signaling */
385 dum->re_timeout = jiffies + msecs_to_jiffies(50);
389 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
391 dum->port_status[rhport] |= (1 << wValue);
397 pr_err("default: no such request\n");
399 /* "protocol stall" on error */
403 if (usbip_dbg_flag_vhci_rh) {
404 pr_debug("port %d\n", rhport);
405 /* Only dump valid port status */
407 dump_port_status_diff(prev_port_status[rhport],
408 dum->port_status[rhport]);
411 usbip_dbg_vhci_rh(" bye\n");
413 spin_unlock_irqrestore(&dum->lock, flags);
418 static struct vhci_device *get_vdev(struct usb_device *udev)
425 for (i = 0; i < VHCI_NPORTS; i++)
426 if (the_controller->vdev[i].udev == udev)
427 return port_to_vdev(i);
432 static void vhci_tx_urb(struct urb *urb)
434 struct vhci_device *vdev = get_vdev(urb->dev);
435 struct vhci_priv *priv;
439 pr_err("could not get virtual device");
443 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
445 spin_lock_irqsave(&vdev->priv_lock, flag);
448 dev_err(&urb->dev->dev, "malloc vhci_priv\n");
449 spin_unlock_irqrestore(&vdev->priv_lock, flag);
450 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
454 priv->seqnum = atomic_inc_return(&the_controller->seqnum);
455 if (priv->seqnum == 0xffff)
456 dev_info(&urb->dev->dev, "seqnum max\n");
461 urb->hcpriv = (void *) priv;
463 list_add_tail(&priv->list, &vdev->priv_tx);
465 wake_up(&vdev->waitq_tx);
466 spin_unlock_irqrestore(&vdev->priv_lock, flag);
469 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
472 struct device *dev = &urb->dev->dev;
475 struct vhci_device *vdev;
477 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
478 hcd, urb, mem_flags);
480 /* patch to usb_sg_init() is in 2.5.60 */
481 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
483 spin_lock_irqsave(&the_controller->lock, flags);
485 if (urb->status != -EINPROGRESS) {
486 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
487 spin_unlock_irqrestore(&the_controller->lock, flags);
491 vdev = port_to_vdev(urb->dev->portnum-1);
493 /* refuse enqueue for dead connection */
494 spin_lock(&vdev->ud.lock);
495 if (vdev->ud.status == VDEV_ST_NULL ||
496 vdev->ud.status == VDEV_ST_ERROR) {
497 dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
498 spin_unlock(&vdev->ud.lock);
499 spin_unlock_irqrestore(&the_controller->lock, flags);
502 spin_unlock(&vdev->ud.lock);
504 ret = usb_hcd_link_urb_to_ep(hcd, urb);
509 * The enumeration process is as follows;
511 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
512 * to get max packet length of default pipe
514 * 2. Set_Address request to DevAddr(0) EndPoint(0)
517 if (usb_pipedevice(urb->pipe) == 0) {
518 __u8 type = usb_pipetype(urb->pipe);
519 struct usb_ctrlrequest *ctrlreq =
520 (struct usb_ctrlrequest *) urb->setup_packet;
522 if (type != PIPE_CONTROL || !ctrlreq) {
523 dev_err(dev, "invalid request to devnum 0\n");
528 switch (ctrlreq->bRequest) {
529 case USB_REQ_SET_ADDRESS:
530 /* set_address may come when a device is reset */
531 dev_info(dev, "SetAddress Request (%d) to port %d\n",
532 ctrlreq->wValue, vdev->rhport);
535 usb_put_dev(vdev->udev);
536 vdev->udev = usb_get_dev(urb->dev);
538 spin_lock(&vdev->ud.lock);
539 vdev->ud.status = VDEV_ST_USED;
540 spin_unlock(&vdev->ud.lock);
542 if (urb->status == -EINPROGRESS) {
543 /* This request is successfully completed. */
544 /* If not -EINPROGRESS, possibly unlinked. */
550 case USB_REQ_GET_DESCRIPTOR:
551 if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
552 usbip_dbg_vhci_hc("Not yet?: "
553 "Get_Descriptor to device 0 "
554 "(get max pipe size)\n");
557 usb_put_dev(vdev->udev);
558 vdev->udev = usb_get_dev(urb->dev);
563 dev_err(dev, "invalid request to devnum 0 bRequest %u, "
564 "wValue %u\n", ctrlreq->bRequest,
574 spin_unlock_irqrestore(&the_controller->lock, flags);
579 usb_hcd_unlink_urb_from_ep(hcd, urb);
581 spin_unlock_irqrestore(&the_controller->lock, flags);
582 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
587 * vhci_rx gives back the urb after receiving the reply of the urb. If an
588 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
589 * back its urb. For the driver unlinking the urb, the content of the urb is
590 * not important, but the calling to its completion handler is important; the
591 * completion of unlinking is notified by the completion handler.
596 * - When vhci_hcd receives RET_SUBMIT,
598 * - case 1a). the urb of the pdu is not unlinking.
600 * => just give back the urb
602 * - case 1b). the urb of the pdu is unlinking.
603 * - usbip.ko will return a reply of the unlinking request.
604 * => give back the urb now and go to case 2b).
606 * - When vhci_hcd receives RET_UNLINK,
608 * - case 2a). a submit request is still pending in vhci_hcd.
609 * - urb was really pending in usbip.ko and urb_unlink_urb() was
611 * => free a pending submit request
612 * => notify unlink completeness by giving back the urb
614 * - case 2b). a submit request is *not* pending in vhci_hcd.
615 * - urb was already given back to the core driver.
616 * => do not give back the urb
621 * - When usbip receives CMD_UNLINK,
623 * - case 3a). the urb of the unlink request is now in submission.
624 * => do usb_unlink_urb().
625 * => after the unlink is completed, send RET_UNLINK.
627 * - case 3b). the urb of the unlink request is not in submission.
628 * - may be already completed or never be received
632 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
635 struct vhci_priv *priv;
636 struct vhci_device *vdev;
638 pr_info("dequeue a urb %p\n", urb);
640 spin_lock_irqsave(&the_controller->lock, flags);
644 /* URB was never linked! or will be soon given back by
646 spin_unlock_irqrestore(&the_controller->lock, flags);
652 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
654 spin_unlock_irqrestore(&the_controller->lock, flags);
659 /* send unlink request here? */
662 if (!vdev->ud.tcp_socket) {
663 /* tcp connection is closed */
664 unsigned long flags2;
666 spin_lock_irqsave(&vdev->priv_lock, flags2);
668 pr_info("device %p seems to be disconnected\n", vdev);
669 list_del(&priv->list);
673 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
676 * If tcp connection is alive, we have sent CMD_UNLINK.
677 * vhci_rx will receive RET_UNLINK and give back the URB.
678 * Otherwise, we give back it here.
680 pr_info("gives back urb %p\n", urb);
682 usb_hcd_unlink_urb_from_ep(hcd, urb);
684 spin_unlock_irqrestore(&the_controller->lock, flags);
685 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
687 spin_lock_irqsave(&the_controller->lock, flags);
690 /* tcp connection is alive */
691 unsigned long flags2;
692 struct vhci_unlink *unlink;
694 spin_lock_irqsave(&vdev->priv_lock, flags2);
696 /* setup CMD_UNLINK pdu */
697 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
699 pr_err("malloc vhci_unlink\n");
700 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
701 spin_unlock_irqrestore(&the_controller->lock, flags);
702 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
706 unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
707 if (unlink->seqnum == 0xffff)
708 pr_info("seqnum max\n");
710 unlink->unlink_seqnum = priv->seqnum;
712 pr_info("device %p seems to be still connected\n", vdev);
714 /* send cmd_unlink and try to cancel the pending URB in the
716 list_add_tail(&unlink->list, &vdev->unlink_tx);
717 wake_up(&vdev->waitq_tx);
719 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
722 spin_unlock_irqrestore(&the_controller->lock, flags);
724 usbip_dbg_vhci_hc("leave\n");
728 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
730 struct vhci_unlink *unlink, *tmp;
732 spin_lock(&the_controller->lock);
733 spin_lock(&vdev->priv_lock);
735 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
736 pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
737 list_del(&unlink->list);
741 while (!list_empty(&vdev->unlink_rx)) {
744 unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
747 /* give back URB of unanswered unlink request */
748 pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
750 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
752 pr_info("the urb (seqnum %lu) was already given back\n",
753 unlink->unlink_seqnum);
754 list_del(&unlink->list);
759 urb->status = -ENODEV;
761 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
763 list_del(&unlink->list);
765 spin_unlock(&vdev->priv_lock);
766 spin_unlock(&the_controller->lock);
768 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
771 spin_lock(&the_controller->lock);
772 spin_lock(&vdev->priv_lock);
777 spin_unlock(&vdev->priv_lock);
778 spin_unlock(&the_controller->lock);
782 * The important thing is that only one context begins cleanup.
783 * This is why error handling and cleanup become simple.
784 * We do not want to consider race condition as possible.
786 static void vhci_shutdown_connection(struct usbip_device *ud)
788 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
790 /* need this? see stub_dev.c */
791 if (ud->tcp_socket) {
792 pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket);
793 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
796 /* kill threads related to this sdev */
797 if (vdev->ud.tcp_rx) {
798 kthread_stop_put(vdev->ud.tcp_rx);
799 vdev->ud.tcp_rx = NULL;
801 if (vdev->ud.tcp_tx) {
802 kthread_stop_put(vdev->ud.tcp_tx);
803 vdev->ud.tcp_tx = NULL;
805 pr_info("stop threads\n");
807 /* active connection is closed */
808 if (vdev->ud.tcp_socket) {
809 fput(vdev->ud.tcp_socket->file);
810 vdev->ud.tcp_socket = NULL;
812 pr_info("release socket\n");
814 vhci_device_unlink_cleanup(vdev);
817 * rh_port_disconnect() is a trigger of ...
818 * usb_disable_device():
819 * disable all the endpoints for a USB device.
820 * usb_disable_endpoint():
821 * disable endpoints. pending urbs are unlinked(dequeued).
823 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
824 * detached device should release used urbs in a cleanup function (i.e.
825 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
826 * pushed urbs and their private data in this function.
828 * NOTE: vhci_dequeue() must be considered carefully. When shutting down
829 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
830 * gives back pushed urbs and frees their private data by request of
831 * the cleanup function of a USB driver. When unlinking a urb with an
832 * active connection, vhci_dequeue() does not give back the urb which
833 * is actually given back by vhci_rx after receiving its return pdu.
836 rh_port_disconnect(vdev->rhport);
838 pr_info("disconnect device\n");
842 static void vhci_device_reset(struct usbip_device *ud)
844 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
846 spin_lock(&ud->lock);
852 usb_put_dev(vdev->udev);
855 if (ud->tcp_socket) {
856 fput(ud->tcp_socket->file);
857 ud->tcp_socket = NULL;
859 ud->status = VDEV_ST_NULL;
861 spin_unlock(&ud->lock);
864 static void vhci_device_unusable(struct usbip_device *ud)
866 spin_lock(&ud->lock);
867 ud->status = VDEV_ST_ERROR;
868 spin_unlock(&ud->lock);
871 static void vhci_device_init(struct vhci_device *vdev)
873 memset(vdev, 0, sizeof(*vdev));
875 vdev->ud.side = USBIP_VHCI;
876 vdev->ud.status = VDEV_ST_NULL;
877 spin_lock_init(&vdev->ud.lock);
879 INIT_LIST_HEAD(&vdev->priv_rx);
880 INIT_LIST_HEAD(&vdev->priv_tx);
881 INIT_LIST_HEAD(&vdev->unlink_tx);
882 INIT_LIST_HEAD(&vdev->unlink_rx);
883 spin_lock_init(&vdev->priv_lock);
885 init_waitqueue_head(&vdev->waitq_tx);
887 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
888 vdev->ud.eh_ops.reset = vhci_device_reset;
889 vdev->ud.eh_ops.unusable = vhci_device_unusable;
891 usbip_start_eh(&vdev->ud);
894 static int vhci_start(struct usb_hcd *hcd)
896 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
900 usbip_dbg_vhci_hc("enter vhci_start\n");
902 /* initialize private data of usb_hcd */
904 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
905 struct vhci_device *vdev = &vhci->vdev[rhport];
906 vhci_device_init(vdev);
907 vdev->rhport = rhport;
910 atomic_set(&vhci->seqnum, 0);
911 spin_lock_init(&vhci->lock);
913 hcd->power_budget = 0; /* no limit */
914 hcd->uses_new_polling = 1;
916 /* vhci_hcd is now ready to be controlled through sysfs */
917 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
919 pr_err("create sysfs files\n");
926 static void vhci_stop(struct usb_hcd *hcd)
928 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
931 usbip_dbg_vhci_hc("stop VHCI controller\n");
933 /* 1. remove the userland interface of vhci_hcd */
934 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
936 /* 2. shutdown all the ports of vhci_hcd */
937 for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
938 struct vhci_device *vdev = &vhci->vdev[rhport];
940 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
941 usbip_stop_eh(&vdev->ud);
945 static int vhci_get_frame_number(struct usb_hcd *hcd)
947 pr_err("Not yet implemented\n");
953 /* FIXME: suspend/resume */
954 static int vhci_bus_suspend(struct usb_hcd *hcd)
956 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
958 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
960 spin_lock_irq(&vhci->lock);
961 hcd->state = HC_STATE_SUSPENDED;
962 spin_unlock_irq(&vhci->lock);
967 static int vhci_bus_resume(struct usb_hcd *hcd)
969 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
972 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
974 spin_lock_irq(&vhci->lock);
975 if (!HCD_HW_ACCESSIBLE(hcd)) {
978 hcd->state = HC_STATE_RUNNING;
980 spin_unlock_irq(&vhci->lock);
987 #define vhci_bus_suspend NULL
988 #define vhci_bus_resume NULL
991 static struct hc_driver vhci_hc_driver = {
992 .description = driver_name,
993 .product_desc = driver_desc,
994 .hcd_priv_size = sizeof(struct vhci_hcd),
1001 .urb_enqueue = vhci_urb_enqueue,
1002 .urb_dequeue = vhci_urb_dequeue,
1004 .get_frame_number = vhci_get_frame_number,
1006 .hub_status_data = vhci_hub_status,
1007 .hub_control = vhci_hub_control,
1008 .bus_suspend = vhci_bus_suspend,
1009 .bus_resume = vhci_bus_resume,
1012 static int vhci_hcd_probe(struct platform_device *pdev)
1014 struct usb_hcd *hcd;
1017 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1019 /* will be removed */
1020 if (pdev->dev.dma_mask) {
1021 dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1026 * Allocate and initialize hcd.
1027 * Our private data is also allocated automatically.
1029 hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1031 pr_err("create hcd failed\n");
1036 /* this is private data for vhci_hcd */
1037 the_controller = hcd_to_vhci(hcd);
1040 * Finish generic HCD structure initialization and register.
1041 * Call the driver's reset() and start() routines.
1043 ret = usb_add_hcd(hcd, 0, 0);
1045 pr_err("usb_add_hcd failed %d\n", ret);
1047 the_controller = NULL;
1051 usbip_dbg_vhci_hc("bye\n");
1055 static int vhci_hcd_remove(struct platform_device *pdev)
1057 struct usb_hcd *hcd;
1059 hcd = platform_get_drvdata(pdev);
1064 * Disconnects the root hub,
1065 * then reverses the effects of usb_add_hcd(),
1066 * invoking the HCD's stop() methods.
1068 usb_remove_hcd(hcd);
1070 the_controller = NULL;
1077 /* what should happen for USB/IP under suspend/resume? */
1078 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1080 struct usb_hcd *hcd;
1085 hcd = platform_get_drvdata(pdev);
1087 spin_lock(&the_controller->lock);
1089 for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1090 if (the_controller->port_status[rhport] &
1091 USB_PORT_STAT_CONNECTION)
1094 spin_unlock(&the_controller->lock);
1096 if (connected > 0) {
1097 dev_info(&pdev->dev, "We have %d active connection%s. Do not "
1098 "suspend.\n", connected, (connected == 1 ? "" : "s"));
1101 dev_info(&pdev->dev, "suspend vhci_hcd");
1102 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1108 static int vhci_hcd_resume(struct platform_device *pdev)
1110 struct usb_hcd *hcd;
1112 dev_dbg(&pdev->dev, "%s\n", __func__);
1114 hcd = platform_get_drvdata(pdev);
1115 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1116 usb_hcd_poll_rh_status(hcd);
1123 #define vhci_hcd_suspend NULL
1124 #define vhci_hcd_resume NULL
1128 static struct platform_driver vhci_driver = {
1129 .probe = vhci_hcd_probe,
1130 .remove = vhci_hcd_remove,
1131 .suspend = vhci_hcd_suspend,
1132 .resume = vhci_hcd_resume,
1134 .name = (char *) driver_name,
1135 .owner = THIS_MODULE,
1140 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1141 * We need to add this virtual device as a platform device arbitrarily:
1142 * 1. platform_device_register()
1144 static void the_pdev_release(struct device *dev)
1149 static struct platform_device the_pdev = {
1150 /* should be the same name as driver_name */
1151 .name = (char *) driver_name,
1154 .release = the_pdev_release,
1158 static int __init vhci_hcd_init(void)
1165 ret = platform_driver_register(&vhci_driver);
1167 goto err_driver_register;
1169 ret = platform_device_register(&the_pdev);
1171 goto err_platform_device_register;
1173 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
1176 err_platform_device_register:
1177 platform_driver_unregister(&vhci_driver);
1178 err_driver_register:
1182 static void __exit vhci_hcd_exit(void)
1184 platform_device_unregister(&the_pdev);
1185 platform_driver_unregister(&vhci_driver);
1188 module_init(vhci_hcd_init);
1189 module_exit(vhci_hcd_exit);
1191 MODULE_AUTHOR(DRIVER_AUTHOR);
1192 MODULE_DESCRIPTION(DRIVER_DESC);
1193 MODULE_LICENSE("GPL");
1194 MODULE_VERSION(USBIP_VERSION);