staging: usbip: fix header includes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / usbip / vhci_hcd.c
blobfacd58ece53de02259a3a87f9a5d8529fc78235a
1 /*
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,
17 * USA.
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"
28 #include "vhci.h"
30 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
31 #define DRIVER_DESC "Virtual Host Controller Interface Driver for USB/IP"
34 * TODO
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,
49 gfp_t mem_flags);
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[] = {
61 "CONNECTION", /*0*/
62 "ENABLE", /*1*/
63 "SUSPEND", /*2*/
64 "OVER_CURRENT", /*3*/
65 "RESET", /*4*/
66 "R5", /*5*/
67 "R6", /*6*/
68 "R7", /*7*/
69 "POWER", /*8*/
70 "LOWSPEED", /*9*/
71 "HIGHSPEED", /*10*/
72 "PORT_TEST", /*11*/
73 "INDICATOR", /*12*/
74 "R13", /*13*/
75 "R14", /*14*/
76 "R15", /*15*/
77 "C_CONNECTION", /*16*/
78 "C_ENABLE", /*17*/
79 "C_SUSPEND", /*18*/
80 "C_OVER_CURRENT", /*19*/
81 "C_RESET", /*20*/
82 "R21", /*21*/
83 "R22", /*22*/
84 "R23", /*23*/
85 "R24", /*24*/
86 "R25", /*25*/
87 "R26", /*26*/
88 "R27", /*27*/
89 "R28", /*28*/
90 "R29", /*29*/
91 "R30", /*30*/
92 "R31", /*31*/
95 static void dump_port_status(u32 status)
97 int i = 0;
99 printk(KERN_DEBUG "status %08x:", status);
100 for (i = 0; i < 32; i++) {
101 if (status & (1 << i))
102 printk(KERN_DEBUG " %s", bit_desc[i]);
104 printk(KERN_DEBUG "\n");
107 void rh_port_connect(int rhport, enum usb_device_speed speed)
109 unsigned long flags;
111 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
113 spin_lock_irqsave(&the_controller->lock, flags);
115 the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
116 | (1 << USB_PORT_FEAT_C_CONNECTION);
118 switch (speed) {
119 case USB_SPEED_HIGH:
120 the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
121 break;
122 case USB_SPEED_LOW:
123 the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
124 break;
125 default:
126 break;
129 /* spin_lock(&the_controller->vdev[rhport].ud.lock);
130 * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
131 * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
133 spin_unlock_irqrestore(&the_controller->lock, flags);
135 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
138 void rh_port_disconnect(int rhport)
140 unsigned long flags;
142 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
144 spin_lock_irqsave(&the_controller->lock, flags);
145 /* stop_activity(dum, driver); */
146 the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
147 the_controller->port_status[rhport] |=
148 (1 << USB_PORT_FEAT_C_CONNECTION);
150 /* not yet complete the disconnection
151 * spin_lock(&vdev->ud.lock);
152 * vdev->ud.status = VHC_ST_DISCONNECT;
153 * spin_unlock(&vdev->ud.lock); */
155 spin_unlock_irqrestore(&the_controller->lock, flags);
156 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
159 #define PORT_C_MASK \
160 ((USB_PORT_STAT_C_CONNECTION \
161 | USB_PORT_STAT_C_ENABLE \
162 | USB_PORT_STAT_C_SUSPEND \
163 | USB_PORT_STAT_C_OVERCURRENT \
164 | USB_PORT_STAT_C_RESET) << 16)
167 * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without
168 * suspend/resume support. But, it is modified to provide multiple ports.
170 * @buf: a bitmap to show which port status has been changed.
171 * bit 0: reserved or used for another purpose?
172 * bit 1: the status of port 0 has been changed.
173 * bit 2: the status of port 1 has been changed.
174 * ...
175 * bit 7: the status of port 6 has been changed.
176 * bit 8: the status of port 7 has been changed.
177 * ...
178 * bit 15: the status of port 14 has been changed.
180 * So, the maximum number of ports is 31 ( port 0 to port 30) ?
182 * The return value is the actual transferred length in byte. If nothing has
183 * been changed, return 0. In the case that the number of ports is less than or
184 * equal to 6 (VHCI_NPORTS==7), return 1.
187 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
189 struct vhci_hcd *vhci;
190 unsigned long flags;
191 int retval = 0;
193 /* the enough buffer is allocated according to USB_MAXCHILDREN */
194 unsigned long *event_bits = (unsigned long *) buf;
195 int rhport;
196 int changed = 0;
198 *event_bits = 0;
200 vhci = hcd_to_vhci(hcd);
202 spin_lock_irqsave(&vhci->lock, flags);
203 if (!HCD_HW_ACCESSIBLE(hcd)) {
204 usbip_dbg_vhci_rh("hw accessible flag in on?\n");
205 goto done;
208 /* check pseudo status register for each port */
209 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
210 if ((vhci->port_status[rhport] & PORT_C_MASK)) {
211 /* The status of a port has been changed, */
212 usbip_dbg_vhci_rh("port %d is changed\n", rhport);
214 *event_bits |= 1 << (rhport + 1);
215 changed = 1;
219 usbip_uinfo("changed %d\n", changed);
221 if (hcd->state == HC_STATE_SUSPENDED)
222 usb_hcd_resume_root_hub(hcd);
224 if (changed)
225 retval = 1 + (VHCI_NPORTS / 8);
226 else
227 retval = 0;
229 done:
230 spin_unlock_irqrestore(&vhci->lock, flags);
231 return retval;
234 /* See hub_configure in hub.c */
235 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
237 memset(desc, 0, sizeof(*desc));
238 desc->bDescriptorType = 0x29;
239 desc->bDescLength = 9;
240 desc->wHubCharacteristics = (__force __u16)
241 (__constant_cpu_to_le16(0x0001));
242 desc->bNbrPorts = VHCI_NPORTS;
243 desc->u.hs.DeviceRemovable[0] = 0xff;
244 desc->u.hs.DeviceRemovable[1] = 0xff;
247 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
248 u16 wIndex, char *buf, u16 wLength)
250 struct vhci_hcd *dum;
251 int retval = 0;
252 unsigned long flags;
253 int rhport;
255 u32 prev_port_status[VHCI_NPORTS];
257 if (!HCD_HW_ACCESSIBLE(hcd))
258 return -ETIMEDOUT;
261 * NOTE:
262 * wIndex shows the port number and begins from 1.
264 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
265 wIndex);
266 if (wIndex > VHCI_NPORTS)
267 printk(KERN_ERR "%s: invalid port number %d\n", __func__,
268 wIndex);
269 rhport = ((__u8)(wIndex & 0x00ff)) - 1;
271 dum = hcd_to_vhci(hcd);
273 spin_lock_irqsave(&dum->lock, flags);
275 /* store old status and compare now and old later */
276 if (usbip_dbg_flag_vhci_rh) {
277 int i = 0;
278 for (i = 0; i < VHCI_NPORTS; i++)
279 prev_port_status[i] = dum->port_status[i];
282 switch (typeReq) {
283 case ClearHubFeature:
284 usbip_dbg_vhci_rh(" ClearHubFeature\n");
285 break;
286 case ClearPortFeature:
287 switch (wValue) {
288 case USB_PORT_FEAT_SUSPEND:
289 if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
290 /* 20msec signaling */
291 dum->resuming = 1;
292 dum->re_timeout =
293 jiffies + msecs_to_jiffies(20);
295 break;
296 case USB_PORT_FEAT_POWER:
297 usbip_dbg_vhci_rh(" ClearPortFeature: "
298 "USB_PORT_FEAT_POWER\n");
299 dum->port_status[rhport] = 0;
300 /* dum->address = 0; */
301 /* dum->hdev = 0; */
302 dum->resuming = 0;
303 break;
304 case USB_PORT_FEAT_C_RESET:
305 usbip_dbg_vhci_rh(" ClearPortFeature: "
306 "USB_PORT_FEAT_C_RESET\n");
307 switch (dum->vdev[rhport].speed) {
308 case USB_SPEED_HIGH:
309 dum->port_status[rhport] |=
310 USB_PORT_STAT_HIGH_SPEED;
311 break;
312 case USB_SPEED_LOW:
313 dum->port_status[rhport] |=
314 USB_PORT_STAT_LOW_SPEED;
315 break;
316 default:
317 break;
319 default:
320 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
321 wValue);
322 dum->port_status[rhport] &= ~(1 << wValue);
323 break;
325 break;
326 case GetHubDescriptor:
327 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
328 hub_descriptor((struct usb_hub_descriptor *) buf);
329 break;
330 case GetHubStatus:
331 usbip_dbg_vhci_rh(" GetHubStatus\n");
332 *(__le32 *) buf = __constant_cpu_to_le32(0);
333 break;
334 case GetPortStatus:
335 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
336 if (wIndex > VHCI_NPORTS || wIndex < 1) {
337 printk(KERN_ERR "%s: invalid port number %d\n",
338 __func__, wIndex);
339 retval = -EPIPE;
342 /* we do no care of resume. */
344 /* whoever resets or resumes must GetPortStatus to
345 * complete it!!
346 * */
347 if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
348 printk(KERN_ERR "%s: not yet\n", __func__);
349 dum->port_status[rhport] |=
350 (1 << USB_PORT_FEAT_C_SUSPEND);
351 dum->port_status[rhport] &=
352 ~(1 << USB_PORT_FEAT_SUSPEND);
353 dum->resuming = 0;
354 dum->re_timeout = 0;
355 /* if (dum->driver && dum->driver->resume) {
356 * spin_unlock (&dum->lock);
357 * dum->driver->resume (&dum->gadget);
358 * spin_lock (&dum->lock);
359 * } */
362 if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
363 0 && time_after(jiffies, dum->re_timeout)) {
364 dum->port_status[rhport] |=
365 (1 << USB_PORT_FEAT_C_RESET);
366 dum->port_status[rhport] &=
367 ~(1 << USB_PORT_FEAT_RESET);
368 dum->re_timeout = 0;
370 if (dum->vdev[rhport].ud.status ==
371 VDEV_ST_NOTASSIGNED) {
372 usbip_dbg_vhci_rh(" enable rhport %d "
373 "(status %u)\n",
374 rhport,
375 dum->vdev[rhport].ud.status);
376 dum->port_status[rhport] |=
377 USB_PORT_STAT_ENABLE;
379 #if 0
380 if (dum->driver) {
381 dum->port_status[rhport] |=
382 USB_PORT_STAT_ENABLE;
383 /* give it the best speed we agree on */
384 dum->gadget.speed = dum->driver->speed;
385 dum->gadget.ep0->maxpacket = 64;
386 switch (dum->gadget.speed) {
387 case USB_SPEED_HIGH:
388 dum->port_status[rhport] |=
389 USB_PORT_STAT_HIGH_SPEED;
390 break;
391 case USB_SPEED_LOW:
392 dum->gadget.ep0->maxpacket = 8;
393 dum->port_status[rhport] |=
394 USB_PORT_STAT_LOW_SPEED;
395 break;
396 default:
397 dum->gadget.speed = USB_SPEED_FULL;
398 break;
401 #endif
403 ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
404 ((u16 *) buf)[1] = cpu_to_le16(dum->port_status[rhport] >> 16);
406 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
407 ((u16 *)buf)[1]);
408 break;
409 case SetHubFeature:
410 usbip_dbg_vhci_rh(" SetHubFeature\n");
411 retval = -EPIPE;
412 break;
413 case SetPortFeature:
414 switch (wValue) {
415 case USB_PORT_FEAT_SUSPEND:
416 usbip_dbg_vhci_rh(" SetPortFeature: "
417 "USB_PORT_FEAT_SUSPEND\n");
418 printk(KERN_ERR "%s: not yet\n", __func__);
419 #if 0
420 dum->port_status[rhport] |=
421 (1 << USB_PORT_FEAT_SUSPEND);
422 if (dum->driver->suspend) {
423 spin_unlock(&dum->lock);
424 dum->driver->suspend(&dum->gadget);
425 spin_lock(&dum->lock);
427 #endif
428 break;
429 case USB_PORT_FEAT_RESET:
430 usbip_dbg_vhci_rh(" SetPortFeature: "
431 "USB_PORT_FEAT_RESET\n");
432 /* if it's already running, disconnect first */
433 if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
434 dum->port_status[rhport] &=
435 ~(USB_PORT_STAT_ENABLE |
436 USB_PORT_STAT_LOW_SPEED |
437 USB_PORT_STAT_HIGH_SPEED);
438 #if 0
439 if (dum->driver) {
440 dev_dbg(hardware, "disconnect\n");
441 stop_activity(dum, dum->driver);
443 #endif
445 /* FIXME test that code path! */
447 /* 50msec reset signaling */
448 dum->re_timeout = jiffies + msecs_to_jiffies(50);
450 /* FALLTHROUGH */
451 default:
452 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
453 wValue);
454 dum->port_status[rhport] |= (1 << wValue);
455 break;
457 break;
459 default:
460 printk(KERN_ERR "%s: default: no such request\n", __func__);
461 /* dev_dbg (hardware,
462 * "hub control req%04x v%04x i%04x l%d\n",
463 * typeReq, wValue, wIndex, wLength); */
465 /* "protocol stall" on error */
466 retval = -EPIPE;
469 if (usbip_dbg_flag_vhci_rh) {
470 printk(KERN_DEBUG "port %d\n", rhport);
471 dump_port_status(prev_port_status[rhport]);
472 dump_port_status(dum->port_status[rhport]);
474 usbip_dbg_vhci_rh(" bye\n");
476 spin_unlock_irqrestore(&dum->lock, flags);
478 return retval;
481 static struct vhci_device *get_vdev(struct usb_device *udev)
483 int i;
485 if (!udev)
486 return NULL;
488 for (i = 0; i < VHCI_NPORTS; i++)
489 if (the_controller->vdev[i].udev == udev)
490 return port_to_vdev(i);
492 return NULL;
495 static void vhci_tx_urb(struct urb *urb)
497 struct vhci_device *vdev = get_vdev(urb->dev);
498 struct vhci_priv *priv;
499 unsigned long flag;
501 if (!vdev) {
502 err("could not get virtual device");
503 /* BUG(); */
504 return;
507 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
509 spin_lock_irqsave(&vdev->priv_lock, flag);
511 if (!priv) {
512 dev_err(&urb->dev->dev, "malloc vhci_priv\n");
513 spin_unlock_irqrestore(&vdev->priv_lock, flag);
514 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
515 return;
518 priv->seqnum = atomic_inc_return(&the_controller->seqnum);
519 if (priv->seqnum == 0xffff)
520 usbip_uinfo("seqnum max\n");
522 priv->vdev = vdev;
523 priv->urb = urb;
525 urb->hcpriv = (void *) priv;
527 list_add_tail(&priv->list, &vdev->priv_tx);
529 wake_up(&vdev->waitq_tx);
530 spin_unlock_irqrestore(&vdev->priv_lock, flag);
533 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
534 gfp_t mem_flags)
536 struct device *dev = &urb->dev->dev;
537 int ret = 0;
538 unsigned long flags;
539 struct vhci_device *vdev;
541 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
542 hcd, urb, mem_flags);
544 /* patch to usb_sg_init() is in 2.5.60 */
545 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
547 spin_lock_irqsave(&the_controller->lock, flags);
549 if (urb->status != -EINPROGRESS) {
550 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
551 spin_unlock_irqrestore(&the_controller->lock, flags);
552 return urb->status;
555 vdev = port_to_vdev(urb->dev->portnum-1);
557 /* refuse enqueue for dead connection */
558 spin_lock(&vdev->ud.lock);
559 if (vdev->ud.status == VDEV_ST_NULL ||
560 vdev->ud.status == VDEV_ST_ERROR) {
561 usbip_uerr("enqueue for inactive port %d\n", vdev->rhport);
562 spin_unlock(&vdev->ud.lock);
563 spin_unlock_irqrestore(&the_controller->lock, flags);
564 return -ENODEV;
566 spin_unlock(&vdev->ud.lock);
568 ret = usb_hcd_link_urb_to_ep(hcd, urb);
569 if (ret)
570 goto no_need_unlink;
573 * The enumeration process is as follows;
575 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
576 * to get max packet length of default pipe
578 * 2. Set_Address request to DevAddr(0) EndPoint(0)
581 if (usb_pipedevice(urb->pipe) == 0) {
582 __u8 type = usb_pipetype(urb->pipe);
583 struct usb_ctrlrequest *ctrlreq =
584 (struct usb_ctrlrequest *) urb->setup_packet;
586 if (type != PIPE_CONTROL || !ctrlreq) {
587 dev_err(dev, "invalid request to devnum 0\n");
588 ret = -EINVAL;
589 goto no_need_xmit;
592 switch (ctrlreq->bRequest) {
593 case USB_REQ_SET_ADDRESS:
594 /* set_address may come when a device is reset */
595 dev_info(dev, "SetAddress Request (%d) to port %d\n",
596 ctrlreq->wValue, vdev->rhport);
598 if (vdev->udev)
599 usb_put_dev(vdev->udev);
600 vdev->udev = usb_get_dev(urb->dev);
602 spin_lock(&vdev->ud.lock);
603 vdev->ud.status = VDEV_ST_USED;
604 spin_unlock(&vdev->ud.lock);
606 if (urb->status == -EINPROGRESS) {
607 /* This request is successfully completed. */
608 /* If not -EINPROGRESS, possibly unlinked. */
609 urb->status = 0;
612 goto no_need_xmit;
614 case USB_REQ_GET_DESCRIPTOR:
615 if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
616 usbip_dbg_vhci_hc("Not yet?: "
617 "Get_Descriptor to device 0 "
618 "(get max pipe size)\n");
620 if (vdev->udev)
621 usb_put_dev(vdev->udev);
622 vdev->udev = usb_get_dev(urb->dev);
623 goto out;
625 default:
626 /* NOT REACHED */
627 dev_err(dev, "invalid request to devnum 0 bRequest %u, "
628 "wValue %u\n", ctrlreq->bRequest,
629 ctrlreq->wValue);
630 ret = -EINVAL;
631 goto no_need_xmit;
636 out:
637 vhci_tx_urb(urb);
638 spin_unlock_irqrestore(&the_controller->lock, flags);
640 return 0;
642 no_need_xmit:
643 usb_hcd_unlink_urb_from_ep(hcd, urb);
644 no_need_unlink:
645 spin_unlock_irqrestore(&the_controller->lock, flags);
647 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
649 return ret;
653 * vhci_rx gives back the urb after receiving the reply of the urb. If an
654 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
655 * back its urb. For the driver unlinking the urb, the content of the urb is
656 * not important, but the calling to its completion handler is important; the
657 * completion of unlinking is notified by the completion handler.
660 * CLIENT SIDE
662 * - When vhci_hcd receives RET_SUBMIT,
664 * - case 1a). the urb of the pdu is not unlinking.
665 * - normal case
666 * => just give back the urb
668 * - case 1b). the urb of the pdu is unlinking.
669 * - usbip.ko will return a reply of the unlinking request.
670 * => give back the urb now and go to case 2b).
672 * - When vhci_hcd receives RET_UNLINK,
674 * - case 2a). a submit request is still pending in vhci_hcd.
675 * - urb was really pending in usbip.ko and urb_unlink_urb() was
676 * completed there.
677 * => free a pending submit request
678 * => notify unlink completeness by giving back the urb
680 * - case 2b). a submit request is *not* pending in vhci_hcd.
681 * - urb was already given back to the core driver.
682 * => do not give back the urb
685 * SERVER SIDE
687 * - When usbip receives CMD_UNLINK,
689 * - case 3a). the urb of the unlink request is now in submission.
690 * => do usb_unlink_urb().
691 * => after the unlink is completed, send RET_UNLINK.
693 * - case 3b). the urb of the unlink request is not in submission.
694 * - may be already completed or never be received
695 * => send RET_UNLINK
698 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
700 unsigned long flags;
701 struct vhci_priv *priv;
702 struct vhci_device *vdev;
704 usbip_uinfo("vhci_hcd: dequeue a urb %p\n", urb);
706 spin_lock_irqsave(&the_controller->lock, flags);
708 priv = urb->hcpriv;
709 if (!priv) {
710 /* URB was never linked! or will be soon given back by
711 * vhci_rx. */
712 spin_unlock_irqrestore(&the_controller->lock, flags);
713 return 0;
717 int ret = 0;
718 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
719 if (ret) {
720 spin_unlock_irqrestore(&the_controller->lock, flags);
721 return ret;
725 /* send unlink request here? */
726 vdev = priv->vdev;
728 if (!vdev->ud.tcp_socket) {
729 /* tcp connection is closed */
730 unsigned long flags2;
732 spin_lock_irqsave(&vdev->priv_lock, flags2);
734 usbip_uinfo("vhci_hcd: device %p seems to be disconnected\n",
735 vdev);
736 list_del(&priv->list);
737 kfree(priv);
738 urb->hcpriv = NULL;
740 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
743 * If tcp connection is alive, we have sent CMD_UNLINK.
744 * vhci_rx will receive RET_UNLINK and give back the URB.
745 * Otherwise, we give back it here.
747 usbip_uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n",
748 urb);
750 usb_hcd_unlink_urb_from_ep(hcd, urb);
752 spin_unlock_irqrestore(&the_controller->lock, flags);
753 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
754 urb->status);
755 spin_lock_irqsave(&the_controller->lock, flags);
757 } else {
758 /* tcp connection is alive */
759 unsigned long flags2;
760 struct vhci_unlink *unlink;
762 spin_lock_irqsave(&vdev->priv_lock, flags2);
764 /* setup CMD_UNLINK pdu */
765 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
766 if (!unlink) {
767 usbip_uerr("malloc vhci_unlink\n");
768 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
769 spin_unlock_irqrestore(&the_controller->lock, flags);
770 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
771 return -ENOMEM;
774 unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
775 if (unlink->seqnum == 0xffff)
776 usbip_uinfo("seqnum max\n");
778 unlink->unlink_seqnum = priv->seqnum;
780 usbip_uinfo("vhci_hcd: device %p seems to be still connected\n",
781 vdev);
783 /* send cmd_unlink and try to cancel the pending URB in the
784 * peer */
785 list_add_tail(&unlink->list, &vdev->unlink_tx);
786 wake_up(&vdev->waitq_tx);
788 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
791 spin_unlock_irqrestore(&the_controller->lock, flags);
793 usbip_dbg_vhci_hc("leave\n");
794 return 0;
797 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
799 struct vhci_unlink *unlink, *tmp;
801 spin_lock(&vdev->priv_lock);
803 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
804 usbip_uinfo("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
805 list_del(&unlink->list);
806 kfree(unlink);
809 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
810 struct urb *urb;
812 /* give back URB of unanswered unlink request */
813 usbip_uinfo("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
815 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
816 if (!urb) {
817 usbip_uinfo("the urb (seqnum %lu) was already given "
818 "back\n", unlink->unlink_seqnum);
819 list_del(&unlink->list);
820 kfree(unlink);
821 continue;
824 urb->status = -ENODEV;
826 spin_lock(&the_controller->lock);
827 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
828 spin_unlock(&the_controller->lock);
830 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
831 urb->status);
833 list_del(&unlink->list);
834 kfree(unlink);
837 spin_unlock(&vdev->priv_lock);
841 * The important thing is that only one context begins cleanup.
842 * This is why error handling and cleanup become simple.
843 * We do not want to consider race condition as possible.
845 static void vhci_shutdown_connection(struct usbip_device *ud)
847 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
849 /* need this? see stub_dev.c */
850 if (ud->tcp_socket) {
851 usbip_udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
852 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
855 /* kill threads related to this sdev, if v.c. exists */
856 kthread_stop(vdev->ud.tcp_rx);
857 kthread_stop(vdev->ud.tcp_tx);
859 usbip_uinfo("stop threads\n");
861 /* active connection is closed */
862 if (vdev->ud.tcp_socket != NULL) {
863 sock_release(vdev->ud.tcp_socket);
864 vdev->ud.tcp_socket = NULL;
866 usbip_uinfo("release socket\n");
868 vhci_device_unlink_cleanup(vdev);
871 * rh_port_disconnect() is a trigger of ...
872 * usb_disable_device():
873 * disable all the endpoints for a USB device.
874 * usb_disable_endpoint():
875 * disable endpoints. pending urbs are unlinked(dequeued).
877 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
878 * deteched device should release used urbs in a cleanup function(i.e.
879 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
880 * pushed urbs and their private data in this function.
882 * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
883 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
884 * gives back pushed urbs and frees their private data by request of
885 * the cleanup function of a USB driver. When unlinking a urb with an
886 * active connection, vhci_dequeue() does not give back the urb which
887 * is actually given back by vhci_rx after receiving its return pdu.
890 rh_port_disconnect(vdev->rhport);
892 usbip_uinfo("disconnect device\n");
896 static void vhci_device_reset(struct usbip_device *ud)
898 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
900 spin_lock(&ud->lock);
902 vdev->speed = 0;
903 vdev->devid = 0;
905 if (vdev->udev)
906 usb_put_dev(vdev->udev);
907 vdev->udev = NULL;
909 ud->tcp_socket = NULL;
910 ud->status = VDEV_ST_NULL;
912 spin_unlock(&ud->lock);
915 static void vhci_device_unusable(struct usbip_device *ud)
917 spin_lock(&ud->lock);
918 ud->status = VDEV_ST_ERROR;
919 spin_unlock(&ud->lock);
922 static void vhci_device_init(struct vhci_device *vdev)
924 memset(vdev, 0, sizeof(*vdev));
926 vdev->ud.tcp_rx = kthread_create(vhci_rx_loop, &vdev->ud, "vhci_rx");
927 vdev->ud.tcp_tx = kthread_create(vhci_tx_loop, &vdev->ud, "vhci_tx");
929 vdev->ud.side = USBIP_VHCI;
930 vdev->ud.status = VDEV_ST_NULL;
931 /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */
932 spin_lock_init(&vdev->ud.lock);
934 INIT_LIST_HEAD(&vdev->priv_rx);
935 INIT_LIST_HEAD(&vdev->priv_tx);
936 INIT_LIST_HEAD(&vdev->unlink_tx);
937 INIT_LIST_HEAD(&vdev->unlink_rx);
938 /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
939 spin_lock_init(&vdev->priv_lock);
941 init_waitqueue_head(&vdev->waitq_tx);
943 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
944 vdev->ud.eh_ops.reset = vhci_device_reset;
945 vdev->ud.eh_ops.unusable = vhci_device_unusable;
947 usbip_start_eh(&vdev->ud);
950 static int vhci_start(struct usb_hcd *hcd)
952 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
953 int rhport;
954 int err = 0;
956 usbip_dbg_vhci_hc("enter vhci_start\n");
958 /* initialize private data of usb_hcd */
960 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
961 struct vhci_device *vdev = &vhci->vdev[rhport];
962 vhci_device_init(vdev);
963 vdev->rhport = rhport;
966 atomic_set(&vhci->seqnum, 0);
967 spin_lock_init(&vhci->lock);
969 hcd->power_budget = 0; /* no limit */
970 hcd->state = HC_STATE_RUNNING;
971 hcd->uses_new_polling = 1;
973 /* vhci_hcd is now ready to be controlled through sysfs */
974 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
975 if (err) {
976 usbip_uerr("create sysfs files\n");
977 return err;
980 return 0;
983 static void vhci_stop(struct usb_hcd *hcd)
985 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
986 int rhport = 0;
988 usbip_dbg_vhci_hc("stop VHCI controller\n");
990 /* 1. remove the userland interface of vhci_hcd */
991 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
993 /* 2. shutdown all the ports of vhci_hcd */
994 for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
995 struct vhci_device *vdev = &vhci->vdev[rhport];
997 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
998 usbip_stop_eh(&vdev->ud);
1001 usbip_uinfo("vhci_stop done\n");
1004 static int vhci_get_frame_number(struct usb_hcd *hcd)
1006 usbip_uerr("Not yet implemented\n");
1007 return 0;
1010 #ifdef CONFIG_PM
1012 /* FIXME: suspend/resume */
1013 static int vhci_bus_suspend(struct usb_hcd *hcd)
1015 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1017 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1019 spin_lock_irq(&vhci->lock);
1020 /* vhci->rh_state = DUMMY_RH_SUSPENDED;
1021 * set_link_state(vhci); */
1022 hcd->state = HC_STATE_SUSPENDED;
1023 spin_unlock_irq(&vhci->lock);
1025 return 0;
1028 static int vhci_bus_resume(struct usb_hcd *hcd)
1030 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1031 int rc = 0;
1033 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1035 spin_lock_irq(&vhci->lock);
1036 if (!HCD_HW_ACCESSIBLE(hcd)) {
1037 rc = -ESHUTDOWN;
1038 } else {
1039 /* vhci->rh_state = DUMMY_RH_RUNNING;
1040 * set_link_state(vhci);
1041 * if (!list_empty(&vhci->urbp_list))
1042 * mod_timer(&vhci->timer, jiffies); */
1043 hcd->state = HC_STATE_RUNNING;
1045 spin_unlock_irq(&vhci->lock);
1046 return rc;
1048 return 0;
1051 #else
1053 #define vhci_bus_suspend NULL
1054 #define vhci_bus_resume NULL
1055 #endif
1057 static struct hc_driver vhci_hc_driver = {
1058 .description = driver_name,
1059 .product_desc = driver_desc,
1060 .hcd_priv_size = sizeof(struct vhci_hcd),
1062 .flags = HCD_USB2,
1064 .start = vhci_start,
1065 .stop = vhci_stop,
1067 .urb_enqueue = vhci_urb_enqueue,
1068 .urb_dequeue = vhci_urb_dequeue,
1070 .get_frame_number = vhci_get_frame_number,
1072 .hub_status_data = vhci_hub_status,
1073 .hub_control = vhci_hub_control,
1074 .bus_suspend = vhci_bus_suspend,
1075 .bus_resume = vhci_bus_resume,
1078 static int vhci_hcd_probe(struct platform_device *pdev)
1080 struct usb_hcd *hcd;
1081 int ret;
1083 usbip_uinfo("proving...\n");
1085 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1087 /* will be removed */
1088 if (pdev->dev.dma_mask) {
1089 dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1090 return -EINVAL;
1094 * Allocate and initialize hcd.
1095 * Our private data is also allocated automatically.
1097 hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1098 if (!hcd) {
1099 usbip_uerr("create hcd failed\n");
1100 return -ENOMEM;
1104 /* this is private data for vhci_hcd */
1105 the_controller = hcd_to_vhci(hcd);
1108 * Finish generic HCD structure initialization and register.
1109 * Call the driver's reset() and start() routines.
1111 ret = usb_add_hcd(hcd, 0, 0);
1112 if (ret != 0) {
1113 usbip_uerr("usb_add_hcd failed %d\n", ret);
1114 usb_put_hcd(hcd);
1115 the_controller = NULL;
1116 return ret;
1119 usbip_dbg_vhci_hc("bye\n");
1120 return 0;
1123 static int vhci_hcd_remove(struct platform_device *pdev)
1125 struct usb_hcd *hcd;
1127 hcd = platform_get_drvdata(pdev);
1128 if (!hcd)
1129 return 0;
1132 * Disconnects the root hub,
1133 * then reverses the effects of usb_add_hcd(),
1134 * invoking the HCD's stop() methods.
1136 usb_remove_hcd(hcd);
1137 usb_put_hcd(hcd);
1138 the_controller = NULL;
1140 return 0;
1143 #ifdef CONFIG_PM
1145 /* what should happen for USB/IP under suspend/resume? */
1146 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1148 struct usb_hcd *hcd;
1149 int rhport = 0;
1150 int connected = 0;
1151 int ret = 0;
1153 dev_dbg(&pdev->dev, "%s\n", __func__);
1155 hcd = platform_get_drvdata(pdev);
1157 spin_lock(&the_controller->lock);
1159 for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1160 if (the_controller->port_status[rhport] &
1161 USB_PORT_STAT_CONNECTION)
1162 connected += 1;
1164 spin_unlock(&the_controller->lock);
1166 if (connected > 0) {
1167 usbip_uinfo("We have %d active connection%s. Do not suspend.\n",
1168 connected, (connected == 1 ? "" : "s"));
1169 ret = -EBUSY;
1170 } else {
1171 usbip_uinfo("suspend vhci_hcd");
1172 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1175 return ret;
1178 static int vhci_hcd_resume(struct platform_device *pdev)
1180 struct usb_hcd *hcd;
1182 dev_dbg(&pdev->dev, "%s\n", __func__);
1184 hcd = platform_get_drvdata(pdev);
1185 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1186 usb_hcd_poll_rh_status(hcd);
1188 return 0;
1191 #else
1193 #define vhci_hcd_suspend NULL
1194 #define vhci_hcd_resume NULL
1196 #endif
1198 static struct platform_driver vhci_driver = {
1199 .probe = vhci_hcd_probe,
1200 .remove = __devexit_p(vhci_hcd_remove),
1201 .suspend = vhci_hcd_suspend,
1202 .resume = vhci_hcd_resume,
1203 .driver = {
1204 .name = (char *) driver_name,
1205 .owner = THIS_MODULE,
1210 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1211 * We need to add this virtual device as a platform device arbitrarily:
1212 * 1. platform_device_register()
1214 static void the_pdev_release(struct device *dev)
1216 return;
1219 static struct platform_device the_pdev = {
1220 /* should be the same name as driver_name */
1221 .name = (char *) driver_name,
1222 .id = -1,
1223 .dev = {
1224 /* .driver = &vhci_driver, */
1225 .release = the_pdev_release,
1229 static int __init vhci_init(void)
1231 int ret;
1233 usbip_dbg_vhci_hc("enter\n");
1234 if (usb_disabled())
1235 return -ENODEV;
1237 printk(KERN_INFO KBUILD_MODNAME ": %s, %s\n", driver_name,
1238 USBIP_VERSION);
1240 ret = platform_driver_register(&vhci_driver);
1241 if (ret < 0)
1242 goto err_driver_register;
1244 ret = platform_device_register(&the_pdev);
1245 if (ret < 0)
1246 goto err_platform_device_register;
1248 usbip_dbg_vhci_hc("bye\n");
1249 return ret;
1251 /* error occurred */
1252 err_platform_device_register:
1253 platform_driver_unregister(&vhci_driver);
1254 err_driver_register:
1255 usbip_dbg_vhci_hc("bye\n");
1256 return ret;
1259 static void __exit vhci_cleanup(void)
1261 usbip_dbg_vhci_hc("enter\n");
1263 platform_device_unregister(&the_pdev);
1264 platform_driver_unregister(&vhci_driver);
1266 usbip_dbg_vhci_hc("bye\n");
1269 module_init(vhci_init);
1270 module_exit(vhci_cleanup);
1272 MODULE_AUTHOR(DRIVER_AUTHOR);
1273 MODULE_DESCRIPTION(DRIVER_DESC);
1274 MODULE_LICENSE("GPL");
1275 MODULE_VERSION(USBIP_VERSION);