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/kernel.h>
22 #include <linux/kthread.h>
23 #include <linux/module.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 #include "usbip_common.h"
30 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
31 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
35 * - update root hub emulation
36 * - move the emulation code to userland ?
37 * porting to other operating systems
38 * minimize kernel code
39 * - add suspend/resume code
40 * - clean up everything
43 /* See usb gadget dummy hcd */
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,
47 u16 wIndex, char *buff, u16 wLength);
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);
55 static const char driver_name[] = "vhci_hcd";
56 static const char driver_desc[] = "USB/IP Virtual Host Controller";
58 struct vhci_hcd *the_controller;
60 static const char * const bit_desc[] = {
77 "C_CONNECTION", /*16*/
80 "C_OVER_CURRENT", /*19*/
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);
102 u32 prev = prev_status & bit;
103 u32 new = new_status & bit;
108 else if (prev && !new)
114 pr_debug(" %c%s\n", change, bit_desc[i]);
121 void rh_port_connect(int rhport, enum usb_device_speed speed)
125 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
127 spin_lock_irqsave(&the_controller->lock, flags);
129 the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
130 | (1 << USB_PORT_FEAT_C_CONNECTION);
134 the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
137 the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
143 /* spin_lock(&the_controller->vdev[rhport].ud.lock);
144 * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
145 * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
147 spin_unlock_irqrestore(&the_controller->lock, flags);
149 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
152 static void rh_port_disconnect(int rhport)
156 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
158 spin_lock_irqsave(&the_controller->lock, flags);
159 /* stop_activity(dum, driver); */
160 the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
161 the_controller->port_status[rhport] |=
162 (1 << USB_PORT_FEAT_C_CONNECTION);
164 /* not yet complete the disconnection
165 * spin_lock(&vdev->ud.lock);
166 * vdev->ud.status = VHC_ST_DISCONNECT;
167 * spin_unlock(&vdev->ud.lock); */
169 spin_unlock_irqrestore(&the_controller->lock, flags);
170 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
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)
181 * Returns 0 if the status hasn't changed, or the number of bytes in buf.
182 * Ports are 0-indexed from the HCD point of view,
183 * and 1-indexed from the USB core pointer of view.
185 * @buf: a bitmap to show which port status has been changed.
187 * bit 1: the status of port 0 has been changed.
188 * bit 2: the status of port 1 has been changed.
191 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
193 struct vhci_hcd *vhci;
199 retval = DIV_ROUND_UP(VHCI_NPORTS + 1, 8);
200 memset(buf, 0, retval);
202 vhci = hcd_to_vhci(hcd);
204 spin_lock_irqsave(&vhci->lock, flags);
205 if (!HCD_HW_ACCESSIBLE(hcd)) {
206 usbip_dbg_vhci_rh("hw accessible flag not on?\n");
210 /* check pseudo status register for each port */
211 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
212 if ((vhci->port_status[rhport] & PORT_C_MASK)) {
213 /* The status of a port has been changed, */
214 usbip_dbg_vhci_rh("port %d status changed\n", rhport);
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;
231 /* See hub_configure in hub.c */
232 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
234 memset(desc, 0, sizeof(*desc));
235 desc->bDescriptorType = 0x29;
236 desc->bDescLength = 9;
237 desc->wHubCharacteristics = (__force __u16)
238 (__constant_cpu_to_le16(0x0001));
239 desc->bNbrPorts = VHCI_NPORTS;
240 desc->u.hs.DeviceRemovable[0] = 0xff;
241 desc->u.hs.DeviceRemovable[1] = 0xff;
244 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
245 u16 wIndex, char *buf, u16 wLength)
247 struct vhci_hcd *dum;
252 u32 prev_port_status[VHCI_NPORTS];
254 if (!HCD_HW_ACCESSIBLE(hcd))
259 * wIndex shows the port number and begins from 1.
261 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
263 if (wIndex > VHCI_NPORTS)
264 pr_err("invalid port number %d\n", wIndex);
265 rhport = ((__u8)(wIndex & 0x00ff)) - 1;
267 dum = hcd_to_vhci(hcd);
269 spin_lock_irqsave(&dum->lock, flags);
271 /* store old status and compare now and old later */
272 if (usbip_dbg_flag_vhci_rh) {
273 memcpy(prev_port_status, dum->port_status,
274 sizeof(prev_port_status));
278 case ClearHubFeature:
279 usbip_dbg_vhci_rh(" ClearHubFeature\n");
281 case ClearPortFeature:
283 case USB_PORT_FEAT_SUSPEND:
284 if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
285 /* 20msec signaling */
288 jiffies + msecs_to_jiffies(20);
291 case USB_PORT_FEAT_POWER:
292 usbip_dbg_vhci_rh(" ClearPortFeature: "
293 "USB_PORT_FEAT_POWER\n");
294 dum->port_status[rhport] = 0;
295 /* dum->address = 0; */
299 case USB_PORT_FEAT_C_RESET:
300 usbip_dbg_vhci_rh(" ClearPortFeature: "
301 "USB_PORT_FEAT_C_RESET\n");
302 switch (dum->vdev[rhport].speed) {
304 dum->port_status[rhport] |=
305 USB_PORT_STAT_HIGH_SPEED;
308 dum->port_status[rhport] |=
309 USB_PORT_STAT_LOW_SPEED;
315 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
317 dum->port_status[rhport] &= ~(1 << wValue);
321 case GetHubDescriptor:
322 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
323 hub_descriptor((struct usb_hub_descriptor *) buf);
326 usbip_dbg_vhci_rh(" GetHubStatus\n");
327 *(__le32 *) buf = __constant_cpu_to_le32(0);
330 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
331 if (wIndex > VHCI_NPORTS || wIndex < 1) {
332 pr_err("invalid port number %d\n", wIndex);
336 /* we do no care of resume. */
338 /* whoever resets or resumes must GetPortStatus to
341 if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
342 dum->port_status[rhport] |=
343 (1 << USB_PORT_FEAT_C_SUSPEND);
344 dum->port_status[rhport] &=
345 ~(1 << USB_PORT_FEAT_SUSPEND);
348 /* if (dum->driver && dum->driver->resume) {
349 * spin_unlock (&dum->lock);
350 * dum->driver->resume (&dum->gadget);
351 * spin_lock (&dum->lock);
355 if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
356 0 && time_after(jiffies, dum->re_timeout)) {
357 dum->port_status[rhport] |=
358 (1 << USB_PORT_FEAT_C_RESET);
359 dum->port_status[rhport] &=
360 ~(1 << USB_PORT_FEAT_RESET);
363 if (dum->vdev[rhport].ud.status ==
364 VDEV_ST_NOTASSIGNED) {
365 usbip_dbg_vhci_rh(" enable rhport %d "
368 dum->vdev[rhport].ud.status);
369 dum->port_status[rhport] |=
370 USB_PORT_STAT_ENABLE;
373 ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
374 ((u16 *) buf)[1] = cpu_to_le16(dum->port_status[rhport] >> 16);
376 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
380 usbip_dbg_vhci_rh(" SetHubFeature\n");
385 case USB_PORT_FEAT_SUSPEND:
386 usbip_dbg_vhci_rh(" SetPortFeature: "
387 "USB_PORT_FEAT_SUSPEND\n");
389 case USB_PORT_FEAT_RESET:
390 usbip_dbg_vhci_rh(" SetPortFeature: "
391 "USB_PORT_FEAT_RESET\n");
392 /* if it's already running, disconnect first */
393 if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
394 dum->port_status[rhport] &=
395 ~(USB_PORT_STAT_ENABLE |
396 USB_PORT_STAT_LOW_SPEED |
397 USB_PORT_STAT_HIGH_SPEED);
398 /* FIXME test that code path! */
400 /* 50msec reset signaling */
401 dum->re_timeout = jiffies + msecs_to_jiffies(50);
405 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
407 dum->port_status[rhport] |= (1 << wValue);
413 pr_err("default: no such request\n");
414 /* dev_dbg (hardware,
415 * "hub control req%04x v%04x i%04x l%d\n",
416 * typeReq, wValue, wIndex, wLength); */
418 /* "protocol stall" on error */
422 if (usbip_dbg_flag_vhci_rh) {
423 pr_debug("port %d\n", rhport);
424 /* Only dump valid port status */
426 dump_port_status_diff(prev_port_status[rhport],
427 dum->port_status[rhport]);
430 usbip_dbg_vhci_rh(" bye\n");
432 spin_unlock_irqrestore(&dum->lock, flags);
437 static struct vhci_device *get_vdev(struct usb_device *udev)
444 for (i = 0; i < VHCI_NPORTS; i++)
445 if (the_controller->vdev[i].udev == udev)
446 return port_to_vdev(i);
451 static void vhci_tx_urb(struct urb *urb)
453 struct vhci_device *vdev = get_vdev(urb->dev);
454 struct vhci_priv *priv;
458 pr_err("could not get virtual device");
463 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
465 spin_lock_irqsave(&vdev->priv_lock, flag);
468 dev_err(&urb->dev->dev, "malloc vhci_priv\n");
469 spin_unlock_irqrestore(&vdev->priv_lock, flag);
470 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
474 priv->seqnum = atomic_inc_return(&the_controller->seqnum);
475 if (priv->seqnum == 0xffff)
476 dev_info(&urb->dev->dev, "seqnum max\n");
481 urb->hcpriv = (void *) priv;
483 list_add_tail(&priv->list, &vdev->priv_tx);
485 wake_up(&vdev->waitq_tx);
486 spin_unlock_irqrestore(&vdev->priv_lock, flag);
489 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
492 struct device *dev = &urb->dev->dev;
495 struct vhci_device *vdev;
497 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
498 hcd, urb, mem_flags);
500 /* patch to usb_sg_init() is in 2.5.60 */
501 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
503 spin_lock_irqsave(&the_controller->lock, flags);
505 if (urb->status != -EINPROGRESS) {
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);
513 /* refuse enqueue for dead connection */
514 spin_lock(&vdev->ud.lock);
515 if (vdev->ud.status == VDEV_ST_NULL ||
516 vdev->ud.status == VDEV_ST_ERROR) {
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);
524 ret = usb_hcd_link_urb_to_ep(hcd, urb);
529 * The enumeration process is as follows;
531 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
532 * to get max packet length of default pipe
534 * 2. Set_Address request to DevAddr(0) EndPoint(0)
537 if (usb_pipedevice(urb->pipe) == 0) {
538 __u8 type = usb_pipetype(urb->pipe);
539 struct usb_ctrlrequest *ctrlreq =
540 (struct usb_ctrlrequest *) urb->setup_packet;
542 if (type != PIPE_CONTROL || !ctrlreq) {
543 dev_err(dev, "invalid request to devnum 0\n");
548 switch (ctrlreq->bRequest) {
549 case USB_REQ_SET_ADDRESS:
550 /* set_address may come when a device is reset */
551 dev_info(dev, "SetAddress Request (%d) to port %d\n",
552 ctrlreq->wValue, vdev->rhport);
555 usb_put_dev(vdev->udev);
556 vdev->udev = usb_get_dev(urb->dev);
558 spin_lock(&vdev->ud.lock);
559 vdev->ud.status = VDEV_ST_USED;
560 spin_unlock(&vdev->ud.lock);
562 if (urb->status == -EINPROGRESS) {
563 /* This request is successfully completed. */
564 /* If not -EINPROGRESS, possibly unlinked. */
570 case USB_REQ_GET_DESCRIPTOR:
571 if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
572 usbip_dbg_vhci_hc("Not yet?: "
573 "Get_Descriptor to device 0 "
574 "(get max pipe size)\n");
577 usb_put_dev(vdev->udev);
578 vdev->udev = usb_get_dev(urb->dev);
583 dev_err(dev, "invalid request to devnum 0 bRequest %u, "
584 "wValue %u\n", ctrlreq->bRequest,
594 spin_unlock_irqrestore(&the_controller->lock, flags);
599 usb_hcd_unlink_urb_from_ep(hcd, urb);
601 spin_unlock_irqrestore(&the_controller->lock, flags);
602 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
607 * vhci_rx gives back the urb after receiving the reply of the urb. If an
608 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
609 * back its urb. For the driver unlinking the urb, the content of the urb is
610 * not important, but the calling to its completion handler is important; the
611 * completion of unlinking is notified by the completion handler.
616 * - When vhci_hcd receives RET_SUBMIT,
618 * - case 1a). the urb of the pdu is not unlinking.
620 * => just give back the urb
622 * - case 1b). the urb of the pdu is unlinking.
623 * - usbip.ko will return a reply of the unlinking request.
624 * => give back the urb now and go to case 2b).
626 * - When vhci_hcd receives RET_UNLINK,
628 * - case 2a). a submit request is still pending in vhci_hcd.
629 * - urb was really pending in usbip.ko and urb_unlink_urb() was
631 * => free a pending submit request
632 * => notify unlink completeness by giving back the urb
634 * - case 2b). a submit request is *not* pending in vhci_hcd.
635 * - urb was already given back to the core driver.
636 * => do not give back the urb
641 * - When usbip receives CMD_UNLINK,
643 * - case 3a). the urb of the unlink request is now in submission.
644 * => do usb_unlink_urb().
645 * => after the unlink is completed, send RET_UNLINK.
647 * - case 3b). the urb of the unlink request is not in submission.
648 * - may be already completed or never be received
652 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
655 struct vhci_priv *priv;
656 struct vhci_device *vdev;
658 pr_info("dequeue a urb %p\n", urb);
660 spin_lock_irqsave(&the_controller->lock, flags);
664 /* URB was never linked! or will be soon given back by
666 spin_unlock_irqrestore(&the_controller->lock, flags);
672 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
674 spin_unlock_irqrestore(&the_controller->lock, flags);
679 /* send unlink request here? */
682 if (!vdev->ud.tcp_socket) {
683 /* tcp connection is closed */
684 unsigned long flags2;
686 spin_lock_irqsave(&vdev->priv_lock, flags2);
688 pr_info("device %p seems to be disconnected\n", vdev);
689 list_del(&priv->list);
693 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
696 * If tcp connection is alive, we have sent CMD_UNLINK.
697 * vhci_rx will receive RET_UNLINK and give back the URB.
698 * Otherwise, we give back it here.
700 pr_info("gives back urb %p\n", urb);
702 usb_hcd_unlink_urb_from_ep(hcd, urb);
704 spin_unlock_irqrestore(&the_controller->lock, flags);
705 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
707 spin_lock_irqsave(&the_controller->lock, flags);
710 /* tcp connection is alive */
711 unsigned long flags2;
712 struct vhci_unlink *unlink;
714 spin_lock_irqsave(&vdev->priv_lock, flags2);
716 /* setup CMD_UNLINK pdu */
717 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
719 pr_err("malloc vhci_unlink\n");
720 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
721 spin_unlock_irqrestore(&the_controller->lock, flags);
722 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
726 unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
727 if (unlink->seqnum == 0xffff)
728 pr_info("seqnum max\n");
730 unlink->unlink_seqnum = priv->seqnum;
732 pr_info("device %p seems to be still connected\n", vdev);
734 /* send cmd_unlink and try to cancel the pending URB in the
736 list_add_tail(&unlink->list, &vdev->unlink_tx);
737 wake_up(&vdev->waitq_tx);
739 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
742 spin_unlock_irqrestore(&the_controller->lock, flags);
744 usbip_dbg_vhci_hc("leave\n");
748 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
750 struct vhci_unlink *unlink, *tmp;
752 spin_lock(&the_controller->lock);
753 spin_lock(&vdev->priv_lock);
755 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
756 pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
757 list_del(&unlink->list);
761 while (!list_empty(&vdev->unlink_rx)) {
764 unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
767 /* give back URB of unanswered unlink request */
768 pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
770 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
772 pr_info("the urb (seqnum %lu) was already given back\n",
773 unlink->unlink_seqnum);
774 list_del(&unlink->list);
779 urb->status = -ENODEV;
781 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
783 list_del(&unlink->list);
785 spin_unlock(&vdev->priv_lock);
786 spin_unlock(&the_controller->lock);
788 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
791 spin_lock(&the_controller->lock);
792 spin_lock(&vdev->priv_lock);
797 spin_unlock(&vdev->priv_lock);
798 spin_unlock(&the_controller->lock);
802 * The important thing is that only one context begins cleanup.
803 * This is why error handling and cleanup become simple.
804 * We do not want to consider race condition as possible.
806 static void vhci_shutdown_connection(struct usbip_device *ud)
808 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
810 /* need this? see stub_dev.c */
811 if (ud->tcp_socket) {
812 pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket);
813 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
816 /* kill threads related to this sdev, if v.c. exists */
817 if (vdev->ud.tcp_rx) {
818 kthread_stop_put(vdev->ud.tcp_rx);
819 vdev->ud.tcp_rx = NULL;
821 if (vdev->ud.tcp_tx) {
822 kthread_stop_put(vdev->ud.tcp_tx);
823 vdev->ud.tcp_tx = NULL;
825 pr_info("stop threads\n");
827 /* active connection is closed */
828 if (vdev->ud.tcp_socket != NULL) {
829 sock_release(vdev->ud.tcp_socket);
830 vdev->ud.tcp_socket = NULL;
832 pr_info("release socket\n");
834 vhci_device_unlink_cleanup(vdev);
837 * rh_port_disconnect() is a trigger of ...
838 * usb_disable_device():
839 * disable all the endpoints for a USB device.
840 * usb_disable_endpoint():
841 * disable endpoints. pending urbs are unlinked(dequeued).
843 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
844 * detached device should release used urbs in a cleanup function (i.e.
845 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
846 * pushed urbs and their private data in this function.
848 * NOTE: vhci_dequeue() must be considered carefully. When shutting down
849 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
850 * gives back pushed urbs and frees their private data by request of
851 * the cleanup function of a USB driver. When unlinking a urb with an
852 * active connection, vhci_dequeue() does not give back the urb which
853 * is actually given back by vhci_rx after receiving its return pdu.
856 rh_port_disconnect(vdev->rhport);
858 pr_info("disconnect device\n");
862 static void vhci_device_reset(struct usbip_device *ud)
864 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
866 spin_lock(&ud->lock);
872 usb_put_dev(vdev->udev);
875 ud->tcp_socket = NULL;
876 ud->status = VDEV_ST_NULL;
878 spin_unlock(&ud->lock);
881 static void vhci_device_unusable(struct usbip_device *ud)
883 spin_lock(&ud->lock);
884 ud->status = VDEV_ST_ERROR;
885 spin_unlock(&ud->lock);
888 static void vhci_device_init(struct vhci_device *vdev)
890 memset(vdev, 0, sizeof(*vdev));
892 vdev->ud.side = USBIP_VHCI;
893 vdev->ud.status = VDEV_ST_NULL;
894 spin_lock_init(&vdev->ud.lock);
896 INIT_LIST_HEAD(&vdev->priv_rx);
897 INIT_LIST_HEAD(&vdev->priv_tx);
898 INIT_LIST_HEAD(&vdev->unlink_tx);
899 INIT_LIST_HEAD(&vdev->unlink_rx);
900 spin_lock_init(&vdev->priv_lock);
902 init_waitqueue_head(&vdev->waitq_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;
908 usbip_start_eh(&vdev->ud);
911 static int vhci_start(struct usb_hcd *hcd)
913 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
917 usbip_dbg_vhci_hc("enter vhci_start\n");
919 /* initialize private data of usb_hcd */
921 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
922 struct vhci_device *vdev = &vhci->vdev[rhport];
923 vhci_device_init(vdev);
924 vdev->rhport = rhport;
927 atomic_set(&vhci->seqnum, 0);
928 spin_lock_init(&vhci->lock);
930 hcd->power_budget = 0; /* no limit */
931 hcd->state = HC_STATE_RUNNING;
932 hcd->uses_new_polling = 1;
934 /* vhci_hcd is now ready to be controlled through sysfs */
935 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
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);
949 usbip_dbg_vhci_hc("stop VHCI controller\n");
951 /* 1. remove the userland interface of vhci_hcd */
952 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
954 /* 2. shutdown all the ports of vhci_hcd */
955 for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
956 struct vhci_device *vdev = &vhci->vdev[rhport];
958 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
959 usbip_stop_eh(&vdev->ud);
963 static int vhci_get_frame_number(struct usb_hcd *hcd)
965 pr_err("Not yet implemented\n");
971 /* FIXME: suspend/resume */
972 static int vhci_bus_suspend(struct usb_hcd *hcd)
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);
979 /* vhci->rh_state = DUMMY_RH_SUSPENDED;
980 * set_link_state(vhci); */
981 hcd->state = HC_STATE_SUSPENDED;
982 spin_unlock_irq(&vhci->lock);
987 static int vhci_bus_resume(struct usb_hcd *hcd)
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)) {
998 /* vhci->rh_state = DUMMY_RH_RUNNING;
999 * set_link_state(vhci);
1000 * if (!list_empty(&vhci->urbp_list))
1001 * mod_timer(&vhci->timer, jiffies); */
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 = {
1016 .description = driver_name,
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,
1032 .bus_suspend = vhci_bus_suspend,
1033 .bus_resume = vhci_bus_resume,
1036 static int vhci_hcd_probe(struct platform_device *pdev)
1038 struct usb_hcd *hcd;
1041 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1043 /* will be removed */
1044 if (pdev->dev.dma_mask) {
1045 dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1050 * Allocate and initialize hcd.
1051 * Our private data is also allocated automatically.
1053 hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1055 pr_err("create hcd failed\n");
1060 /* this is private data for vhci_hcd */
1061 the_controller = hcd_to_vhci(hcd);
1064 * Finish generic HCD structure initialization and register.
1065 * Call the driver's reset() and start() routines.
1067 ret = usb_add_hcd(hcd, 0, 0);
1069 pr_err("usb_add_hcd failed %d\n", ret);
1071 the_controller = NULL;
1075 usbip_dbg_vhci_hc("bye\n");
1079 static int vhci_hcd_remove(struct platform_device *pdev)
1081 struct usb_hcd *hcd;
1083 hcd = platform_get_drvdata(pdev);
1088 * Disconnects the root hub,
1089 * then reverses the effects of usb_add_hcd(),
1090 * invoking the HCD's stop() methods.
1092 usb_remove_hcd(hcd);
1094 the_controller = NULL;
1101 /* what should happen for USB/IP under suspend/resume? */
1102 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1104 struct usb_hcd *hcd;
1109 hcd = platform_get_drvdata(pdev);
1111 spin_lock(&the_controller->lock);
1113 for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1114 if (the_controller->port_status[rhport] &
1115 USB_PORT_STAT_CONNECTION)
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"));
1125 dev_info(&pdev->dev, "suspend vhci_hcd");
1126 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1132 static int vhci_hcd_resume(struct platform_device *pdev)
1134 struct usb_hcd *hcd;
1136 dev_dbg(&pdev->dev, "%s\n", __func__);
1138 hcd = platform_get_drvdata(pdev);
1139 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1140 usb_hcd_poll_rh_status(hcd);
1147 #define vhci_hcd_suspend NULL
1148 #define vhci_hcd_resume NULL
1152 static struct platform_driver vhci_driver = {
1153 .probe = vhci_hcd_probe,
1154 .remove = __devexit_p(vhci_hcd_remove),
1155 .suspend = vhci_hcd_suspend,
1156 .resume = vhci_hcd_resume,
1158 .name = (char *) driver_name,
1159 .owner = THIS_MODULE,
1164 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1165 * We need to add this virtual device as a platform device arbitrarily:
1166 * 1. platform_device_register()
1168 static void the_pdev_release(struct device *dev)
1173 static struct platform_device the_pdev = {
1174 /* should be the same name as driver_name */
1175 .name = (char *) driver_name,
1178 /* .driver = &vhci_driver, */
1179 .release = the_pdev_release,
1183 static int __init vhci_hcd_init(void)
1190 ret = platform_driver_register(&vhci_driver);
1192 goto err_driver_register;
1194 ret = platform_device_register(&the_pdev);
1196 goto err_platform_device_register;
1198 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
1201 err_platform_device_register:
1202 platform_driver_unregister(&vhci_driver);
1203 err_driver_register:
1207 static void __exit vhci_hcd_exit(void)
1209 platform_device_unregister(&the_pdev);
1210 platform_driver_unregister(&vhci_driver);
1213 module_init(vhci_hcd_init);
1214 module_exit(vhci_hcd_exit);
1216 MODULE_AUTHOR(DRIVER_AUTHOR);
1217 MODULE_DESCRIPTION(DRIVER_DESC);
1218 MODULE_LICENSE("GPL");
1219 MODULE_VERSION(USBIP_VERSION);