staging: usbip: replace usbip_u{dbg,err,info} and printk with dev_ and pr_
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / usbip / vhci_hcd.c
blob598f31180e96e2ce2fa888c75ddc121f6051c5d5
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 "USB/IP 'Virtual' Host Controller (VHCI) Driver"
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 pr_debug("status %08x:", status);
100 for (i = 0; i < 32; i++) {
101 if (status & (1 << i))
102 pr_debug(" %s", bit_desc[i]);
104 pr_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 pr_info("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 pr_err("invalid port number %d\n", wIndex);
268 rhport = ((__u8)(wIndex & 0x00ff)) - 1;
270 dum = hcd_to_vhci(hcd);
272 spin_lock_irqsave(&dum->lock, flags);
274 /* store old status and compare now and old later */
275 if (usbip_dbg_flag_vhci_rh) {
276 int i = 0;
277 for (i = 0; i < VHCI_NPORTS; i++)
278 prev_port_status[i] = dum->port_status[i];
281 switch (typeReq) {
282 case ClearHubFeature:
283 usbip_dbg_vhci_rh(" ClearHubFeature\n");
284 break;
285 case ClearPortFeature:
286 switch (wValue) {
287 case USB_PORT_FEAT_SUSPEND:
288 if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
289 /* 20msec signaling */
290 dum->resuming = 1;
291 dum->re_timeout =
292 jiffies + msecs_to_jiffies(20);
294 break;
295 case USB_PORT_FEAT_POWER:
296 usbip_dbg_vhci_rh(" ClearPortFeature: "
297 "USB_PORT_FEAT_POWER\n");
298 dum->port_status[rhport] = 0;
299 /* dum->address = 0; */
300 /* dum->hdev = 0; */
301 dum->resuming = 0;
302 break;
303 case USB_PORT_FEAT_C_RESET:
304 usbip_dbg_vhci_rh(" ClearPortFeature: "
305 "USB_PORT_FEAT_C_RESET\n");
306 switch (dum->vdev[rhport].speed) {
307 case USB_SPEED_HIGH:
308 dum->port_status[rhport] |=
309 USB_PORT_STAT_HIGH_SPEED;
310 break;
311 case USB_SPEED_LOW:
312 dum->port_status[rhport] |=
313 USB_PORT_STAT_LOW_SPEED;
314 break;
315 default:
316 break;
318 default:
319 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
320 wValue);
321 dum->port_status[rhport] &= ~(1 << wValue);
322 break;
324 break;
325 case GetHubDescriptor:
326 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
327 hub_descriptor((struct usb_hub_descriptor *) buf);
328 break;
329 case GetHubStatus:
330 usbip_dbg_vhci_rh(" GetHubStatus\n");
331 *(__le32 *) buf = __constant_cpu_to_le32(0);
332 break;
333 case GetPortStatus:
334 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
335 if (wIndex > VHCI_NPORTS || wIndex < 1) {
336 pr_err("invalid port number %d\n", wIndex);
337 retval = -EPIPE;
340 /* we do no care of resume. */
342 /* whoever resets or resumes must GetPortStatus to
343 * complete it!!
344 * */
345 if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
346 dum->port_status[rhport] |=
347 (1 << USB_PORT_FEAT_C_SUSPEND);
348 dum->port_status[rhport] &=
349 ~(1 << USB_PORT_FEAT_SUSPEND);
350 dum->resuming = 0;
351 dum->re_timeout = 0;
352 /* if (dum->driver && dum->driver->resume) {
353 * spin_unlock (&dum->lock);
354 * dum->driver->resume (&dum->gadget);
355 * spin_lock (&dum->lock);
356 * } */
359 if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
360 0 && time_after(jiffies, dum->re_timeout)) {
361 dum->port_status[rhport] |=
362 (1 << USB_PORT_FEAT_C_RESET);
363 dum->port_status[rhport] &=
364 ~(1 << USB_PORT_FEAT_RESET);
365 dum->re_timeout = 0;
367 if (dum->vdev[rhport].ud.status ==
368 VDEV_ST_NOTASSIGNED) {
369 usbip_dbg_vhci_rh(" enable rhport %d "
370 "(status %u)\n",
371 rhport,
372 dum->vdev[rhport].ud.status);
373 dum->port_status[rhport] |=
374 USB_PORT_STAT_ENABLE;
376 #if 0
377 if (dum->driver) {
378 dum->port_status[rhport] |=
379 USB_PORT_STAT_ENABLE;
380 /* give it the best speed we agree on */
381 dum->gadget.speed = dum->driver->speed;
382 dum->gadget.ep0->maxpacket = 64;
383 switch (dum->gadget.speed) {
384 case USB_SPEED_HIGH:
385 dum->port_status[rhport] |=
386 USB_PORT_STAT_HIGH_SPEED;
387 break;
388 case USB_SPEED_LOW:
389 dum->gadget.ep0->maxpacket = 8;
390 dum->port_status[rhport] |=
391 USB_PORT_STAT_LOW_SPEED;
392 break;
393 default:
394 dum->gadget.speed = USB_SPEED_FULL;
395 break;
398 #endif
400 ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
401 ((u16 *) buf)[1] = cpu_to_le16(dum->port_status[rhport] >> 16);
403 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
404 ((u16 *)buf)[1]);
405 break;
406 case SetHubFeature:
407 usbip_dbg_vhci_rh(" SetHubFeature\n");
408 retval = -EPIPE;
409 break;
410 case SetPortFeature:
411 switch (wValue) {
412 case USB_PORT_FEAT_SUSPEND:
413 usbip_dbg_vhci_rh(" SetPortFeature: "
414 "USB_PORT_FEAT_SUSPEND\n");
415 #if 0
416 dum->port_status[rhport] |=
417 (1 << USB_PORT_FEAT_SUSPEND);
418 if (dum->driver->suspend) {
419 spin_unlock(&dum->lock);
420 dum->driver->suspend(&dum->gadget);
421 spin_lock(&dum->lock);
423 #endif
424 break;
425 case USB_PORT_FEAT_RESET:
426 usbip_dbg_vhci_rh(" SetPortFeature: "
427 "USB_PORT_FEAT_RESET\n");
428 /* if it's already running, disconnect first */
429 if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
430 dum->port_status[rhport] &=
431 ~(USB_PORT_STAT_ENABLE |
432 USB_PORT_STAT_LOW_SPEED |
433 USB_PORT_STAT_HIGH_SPEED);
434 #if 0
435 if (dum->driver) {
436 dev_dbg(hardware, "disconnect\n");
437 stop_activity(dum, dum->driver);
439 #endif
441 /* FIXME test that code path! */
443 /* 50msec reset signaling */
444 dum->re_timeout = jiffies + msecs_to_jiffies(50);
446 /* FALLTHROUGH */
447 default:
448 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
449 wValue);
450 dum->port_status[rhport] |= (1 << wValue);
451 break;
453 break;
455 default:
456 pr_err("default: no such request\n");
457 /* dev_dbg (hardware,
458 * "hub control req%04x v%04x i%04x l%d\n",
459 * typeReq, wValue, wIndex, wLength); */
461 /* "protocol stall" on error */
462 retval = -EPIPE;
465 if (usbip_dbg_flag_vhci_rh) {
466 pr_debug("port %d\n", rhport);
467 dump_port_status(prev_port_status[rhport]);
468 dump_port_status(dum->port_status[rhport]);
470 usbip_dbg_vhci_rh(" bye\n");
472 spin_unlock_irqrestore(&dum->lock, flags);
474 return retval;
477 static struct vhci_device *get_vdev(struct usb_device *udev)
479 int i;
481 if (!udev)
482 return NULL;
484 for (i = 0; i < VHCI_NPORTS; i++)
485 if (the_controller->vdev[i].udev == udev)
486 return port_to_vdev(i);
488 return NULL;
491 static void vhci_tx_urb(struct urb *urb)
493 struct vhci_device *vdev = get_vdev(urb->dev);
494 struct vhci_priv *priv;
495 unsigned long flag;
497 if (!vdev) {
498 pr_err("could not get virtual device");
499 /* BUG(); */
500 return;
503 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
505 spin_lock_irqsave(&vdev->priv_lock, flag);
507 if (!priv) {
508 dev_err(&urb->dev->dev, "malloc vhci_priv\n");
509 spin_unlock_irqrestore(&vdev->priv_lock, flag);
510 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
511 return;
514 priv->seqnum = atomic_inc_return(&the_controller->seqnum);
515 if (priv->seqnum == 0xffff)
516 dev_info(&urb->dev->dev, "seqnum max\n");
518 priv->vdev = vdev;
519 priv->urb = urb;
521 urb->hcpriv = (void *) priv;
523 list_add_tail(&priv->list, &vdev->priv_tx);
525 wake_up(&vdev->waitq_tx);
526 spin_unlock_irqrestore(&vdev->priv_lock, flag);
529 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
530 gfp_t mem_flags)
532 struct device *dev = &urb->dev->dev;
533 int ret = 0;
534 unsigned long flags;
535 struct vhci_device *vdev;
537 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
538 hcd, urb, mem_flags);
540 /* patch to usb_sg_init() is in 2.5.60 */
541 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
543 spin_lock_irqsave(&the_controller->lock, flags);
545 if (urb->status != -EINPROGRESS) {
546 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
547 spin_unlock_irqrestore(&the_controller->lock, flags);
548 return urb->status;
551 vdev = port_to_vdev(urb->dev->portnum-1);
553 /* refuse enqueue for dead connection */
554 spin_lock(&vdev->ud.lock);
555 if (vdev->ud.status == VDEV_ST_NULL ||
556 vdev->ud.status == VDEV_ST_ERROR) {
557 dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
558 spin_unlock(&vdev->ud.lock);
559 spin_unlock_irqrestore(&the_controller->lock, flags);
560 return -ENODEV;
562 spin_unlock(&vdev->ud.lock);
564 ret = usb_hcd_link_urb_to_ep(hcd, urb);
565 if (ret)
566 goto no_need_unlink;
569 * The enumeration process is as follows;
571 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
572 * to get max packet length of default pipe
574 * 2. Set_Address request to DevAddr(0) EndPoint(0)
577 if (usb_pipedevice(urb->pipe) == 0) {
578 __u8 type = usb_pipetype(urb->pipe);
579 struct usb_ctrlrequest *ctrlreq =
580 (struct usb_ctrlrequest *) urb->setup_packet;
582 if (type != PIPE_CONTROL || !ctrlreq) {
583 dev_err(dev, "invalid request to devnum 0\n");
584 ret = -EINVAL;
585 goto no_need_xmit;
588 switch (ctrlreq->bRequest) {
589 case USB_REQ_SET_ADDRESS:
590 /* set_address may come when a device is reset */
591 dev_info(dev, "SetAddress Request (%d) to port %d\n",
592 ctrlreq->wValue, vdev->rhport);
594 if (vdev->udev)
595 usb_put_dev(vdev->udev);
596 vdev->udev = usb_get_dev(urb->dev);
598 spin_lock(&vdev->ud.lock);
599 vdev->ud.status = VDEV_ST_USED;
600 spin_unlock(&vdev->ud.lock);
602 if (urb->status == -EINPROGRESS) {
603 /* This request is successfully completed. */
604 /* If not -EINPROGRESS, possibly unlinked. */
605 urb->status = 0;
608 goto no_need_xmit;
610 case USB_REQ_GET_DESCRIPTOR:
611 if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
612 usbip_dbg_vhci_hc("Not yet?: "
613 "Get_Descriptor to device 0 "
614 "(get max pipe size)\n");
616 if (vdev->udev)
617 usb_put_dev(vdev->udev);
618 vdev->udev = usb_get_dev(urb->dev);
619 goto out;
621 default:
622 /* NOT REACHED */
623 dev_err(dev, "invalid request to devnum 0 bRequest %u, "
624 "wValue %u\n", ctrlreq->bRequest,
625 ctrlreq->wValue);
626 ret = -EINVAL;
627 goto no_need_xmit;
632 out:
633 vhci_tx_urb(urb);
634 spin_unlock_irqrestore(&the_controller->lock, flags);
636 return 0;
638 no_need_xmit:
639 usb_hcd_unlink_urb_from_ep(hcd, urb);
640 no_need_unlink:
641 spin_unlock_irqrestore(&the_controller->lock, flags);
643 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
645 return ret;
649 * vhci_rx gives back the urb after receiving the reply of the urb. If an
650 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
651 * back its urb. For the driver unlinking the urb, the content of the urb is
652 * not important, but the calling to its completion handler is important; the
653 * completion of unlinking is notified by the completion handler.
656 * CLIENT SIDE
658 * - When vhci_hcd receives RET_SUBMIT,
660 * - case 1a). the urb of the pdu is not unlinking.
661 * - normal case
662 * => just give back the urb
664 * - case 1b). the urb of the pdu is unlinking.
665 * - usbip.ko will return a reply of the unlinking request.
666 * => give back the urb now and go to case 2b).
668 * - When vhci_hcd receives RET_UNLINK,
670 * - case 2a). a submit request is still pending in vhci_hcd.
671 * - urb was really pending in usbip.ko and urb_unlink_urb() was
672 * completed there.
673 * => free a pending submit request
674 * => notify unlink completeness by giving back the urb
676 * - case 2b). a submit request is *not* pending in vhci_hcd.
677 * - urb was already given back to the core driver.
678 * => do not give back the urb
681 * SERVER SIDE
683 * - When usbip receives CMD_UNLINK,
685 * - case 3a). the urb of the unlink request is now in submission.
686 * => do usb_unlink_urb().
687 * => after the unlink is completed, send RET_UNLINK.
689 * - case 3b). the urb of the unlink request is not in submission.
690 * - may be already completed or never be received
691 * => send RET_UNLINK
694 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
696 unsigned long flags;
697 struct vhci_priv *priv;
698 struct vhci_device *vdev;
700 pr_info("dequeue a urb %p\n", urb);
702 spin_lock_irqsave(&the_controller->lock, flags);
704 priv = urb->hcpriv;
705 if (!priv) {
706 /* URB was never linked! or will be soon given back by
707 * vhci_rx. */
708 spin_unlock_irqrestore(&the_controller->lock, flags);
709 return 0;
713 int ret = 0;
714 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
715 if (ret) {
716 spin_unlock_irqrestore(&the_controller->lock, flags);
717 return ret;
721 /* send unlink request here? */
722 vdev = priv->vdev;
724 if (!vdev->ud.tcp_socket) {
725 /* tcp connection is closed */
726 unsigned long flags2;
728 spin_lock_irqsave(&vdev->priv_lock, flags2);
730 pr_info("device %p seems to be disconnected\n", vdev);
731 list_del(&priv->list);
732 kfree(priv);
733 urb->hcpriv = NULL;
735 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
738 * If tcp connection is alive, we have sent CMD_UNLINK.
739 * vhci_rx will receive RET_UNLINK and give back the URB.
740 * Otherwise, we give back it here.
742 pr_info("gives back urb %p\n", urb);
744 usb_hcd_unlink_urb_from_ep(hcd, urb);
746 spin_unlock_irqrestore(&the_controller->lock, flags);
747 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
748 urb->status);
749 spin_lock_irqsave(&the_controller->lock, flags);
751 } else {
752 /* tcp connection is alive */
753 unsigned long flags2;
754 struct vhci_unlink *unlink;
756 spin_lock_irqsave(&vdev->priv_lock, flags2);
758 /* setup CMD_UNLINK pdu */
759 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
760 if (!unlink) {
761 pr_err("malloc vhci_unlink\n");
762 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
763 spin_unlock_irqrestore(&the_controller->lock, flags);
764 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
765 return -ENOMEM;
768 unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
769 if (unlink->seqnum == 0xffff)
770 pr_info("seqnum max\n");
772 unlink->unlink_seqnum = priv->seqnum;
774 pr_info("device %p seems to be still connected\n", vdev);
776 /* send cmd_unlink and try to cancel the pending URB in the
777 * peer */
778 list_add_tail(&unlink->list, &vdev->unlink_tx);
779 wake_up(&vdev->waitq_tx);
781 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
784 spin_unlock_irqrestore(&the_controller->lock, flags);
786 usbip_dbg_vhci_hc("leave\n");
787 return 0;
790 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
792 struct vhci_unlink *unlink, *tmp;
794 spin_lock(&vdev->priv_lock);
796 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
797 pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
798 list_del(&unlink->list);
799 kfree(unlink);
802 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
803 struct urb *urb;
805 /* give back URB of unanswered unlink request */
806 pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
808 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
809 if (!urb) {
810 pr_info("the urb (seqnum %lu) was already given back\n",
811 unlink->unlink_seqnum);
812 list_del(&unlink->list);
813 kfree(unlink);
814 continue;
817 urb->status = -ENODEV;
819 spin_lock(&the_controller->lock);
820 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
821 spin_unlock(&the_controller->lock);
823 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
824 urb->status);
826 list_del(&unlink->list);
827 kfree(unlink);
830 spin_unlock(&vdev->priv_lock);
834 * The important thing is that only one context begins cleanup.
835 * This is why error handling and cleanup become simple.
836 * We do not want to consider race condition as possible.
838 static void vhci_shutdown_connection(struct usbip_device *ud)
840 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
842 /* need this? see stub_dev.c */
843 if (ud->tcp_socket) {
844 pr_debug("shutdown tcp_socket %p\n", ud->tcp_socket);
845 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
848 /* kill threads related to this sdev, if v.c. exists */
849 kthread_stop(vdev->ud.tcp_rx);
850 kthread_stop(vdev->ud.tcp_tx);
852 pr_info("stop threads\n");
854 /* active connection is closed */
855 if (vdev->ud.tcp_socket != NULL) {
856 sock_release(vdev->ud.tcp_socket);
857 vdev->ud.tcp_socket = NULL;
859 pr_info("release socket\n");
861 vhci_device_unlink_cleanup(vdev);
864 * rh_port_disconnect() is a trigger of ...
865 * usb_disable_device():
866 * disable all the endpoints for a USB device.
867 * usb_disable_endpoint():
868 * disable endpoints. pending urbs are unlinked(dequeued).
870 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
871 * deteched device should release used urbs in a cleanup function(i.e.
872 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
873 * pushed urbs and their private data in this function.
875 * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
876 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
877 * gives back pushed urbs and frees their private data by request of
878 * the cleanup function of a USB driver. When unlinking a urb with an
879 * active connection, vhci_dequeue() does not give back the urb which
880 * is actually given back by vhci_rx after receiving its return pdu.
883 rh_port_disconnect(vdev->rhport);
885 pr_info("disconnect device\n");
889 static void vhci_device_reset(struct usbip_device *ud)
891 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
893 spin_lock(&ud->lock);
895 vdev->speed = 0;
896 vdev->devid = 0;
898 if (vdev->udev)
899 usb_put_dev(vdev->udev);
900 vdev->udev = NULL;
902 ud->tcp_socket = NULL;
903 ud->status = VDEV_ST_NULL;
905 spin_unlock(&ud->lock);
908 static void vhci_device_unusable(struct usbip_device *ud)
910 spin_lock(&ud->lock);
911 ud->status = VDEV_ST_ERROR;
912 spin_unlock(&ud->lock);
915 static void vhci_device_init(struct vhci_device *vdev)
917 memset(vdev, 0, sizeof(*vdev));
919 vdev->ud.tcp_rx = kthread_create(vhci_rx_loop, &vdev->ud, "vhci_rx");
920 vdev->ud.tcp_tx = kthread_create(vhci_tx_loop, &vdev->ud, "vhci_tx");
922 vdev->ud.side = USBIP_VHCI;
923 vdev->ud.status = VDEV_ST_NULL;
924 /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */
925 spin_lock_init(&vdev->ud.lock);
927 INIT_LIST_HEAD(&vdev->priv_rx);
928 INIT_LIST_HEAD(&vdev->priv_tx);
929 INIT_LIST_HEAD(&vdev->unlink_tx);
930 INIT_LIST_HEAD(&vdev->unlink_rx);
931 /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
932 spin_lock_init(&vdev->priv_lock);
934 init_waitqueue_head(&vdev->waitq_tx);
936 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
937 vdev->ud.eh_ops.reset = vhci_device_reset;
938 vdev->ud.eh_ops.unusable = vhci_device_unusable;
940 usbip_start_eh(&vdev->ud);
943 static int vhci_start(struct usb_hcd *hcd)
945 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
946 int rhport;
947 int err = 0;
949 usbip_dbg_vhci_hc("enter vhci_start\n");
951 /* initialize private data of usb_hcd */
953 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
954 struct vhci_device *vdev = &vhci->vdev[rhport];
955 vhci_device_init(vdev);
956 vdev->rhport = rhport;
959 atomic_set(&vhci->seqnum, 0);
960 spin_lock_init(&vhci->lock);
962 hcd->power_budget = 0; /* no limit */
963 hcd->state = HC_STATE_RUNNING;
964 hcd->uses_new_polling = 1;
966 /* vhci_hcd is now ready to be controlled through sysfs */
967 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
968 if (err) {
969 pr_err("create sysfs files\n");
970 return err;
973 return 0;
976 static void vhci_stop(struct usb_hcd *hcd)
978 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
979 int rhport = 0;
981 usbip_dbg_vhci_hc("stop VHCI controller\n");
983 /* 1. remove the userland interface of vhci_hcd */
984 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
986 /* 2. shutdown all the ports of vhci_hcd */
987 for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
988 struct vhci_device *vdev = &vhci->vdev[rhport];
990 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
991 usbip_stop_eh(&vdev->ud);
995 static int vhci_get_frame_number(struct usb_hcd *hcd)
997 pr_err("Not yet implemented\n");
998 return 0;
1001 #ifdef CONFIG_PM
1003 /* FIXME: suspend/resume */
1004 static int vhci_bus_suspend(struct usb_hcd *hcd)
1006 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1008 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1010 spin_lock_irq(&vhci->lock);
1011 /* vhci->rh_state = DUMMY_RH_SUSPENDED;
1012 * set_link_state(vhci); */
1013 hcd->state = HC_STATE_SUSPENDED;
1014 spin_unlock_irq(&vhci->lock);
1016 return 0;
1019 static int vhci_bus_resume(struct usb_hcd *hcd)
1021 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1022 int rc = 0;
1024 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1026 spin_lock_irq(&vhci->lock);
1027 if (!HCD_HW_ACCESSIBLE(hcd)) {
1028 rc = -ESHUTDOWN;
1029 } else {
1030 /* vhci->rh_state = DUMMY_RH_RUNNING;
1031 * set_link_state(vhci);
1032 * if (!list_empty(&vhci->urbp_list))
1033 * mod_timer(&vhci->timer, jiffies); */
1034 hcd->state = HC_STATE_RUNNING;
1036 spin_unlock_irq(&vhci->lock);
1037 return rc;
1039 return 0;
1042 #else
1044 #define vhci_bus_suspend NULL
1045 #define vhci_bus_resume NULL
1046 #endif
1048 static struct hc_driver vhci_hc_driver = {
1049 .description = driver_name,
1050 .product_desc = driver_desc,
1051 .hcd_priv_size = sizeof(struct vhci_hcd),
1053 .flags = HCD_USB2,
1055 .start = vhci_start,
1056 .stop = vhci_stop,
1058 .urb_enqueue = vhci_urb_enqueue,
1059 .urb_dequeue = vhci_urb_dequeue,
1061 .get_frame_number = vhci_get_frame_number,
1063 .hub_status_data = vhci_hub_status,
1064 .hub_control = vhci_hub_control,
1065 .bus_suspend = vhci_bus_suspend,
1066 .bus_resume = vhci_bus_resume,
1069 static int vhci_hcd_probe(struct platform_device *pdev)
1071 struct usb_hcd *hcd;
1072 int ret;
1074 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1076 /* will be removed */
1077 if (pdev->dev.dma_mask) {
1078 dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1079 return -EINVAL;
1083 * Allocate and initialize hcd.
1084 * Our private data is also allocated automatically.
1086 hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1087 if (!hcd) {
1088 pr_err("create hcd failed\n");
1089 return -ENOMEM;
1093 /* this is private data for vhci_hcd */
1094 the_controller = hcd_to_vhci(hcd);
1097 * Finish generic HCD structure initialization and register.
1098 * Call the driver's reset() and start() routines.
1100 ret = usb_add_hcd(hcd, 0, 0);
1101 if (ret != 0) {
1102 pr_err("usb_add_hcd failed %d\n", ret);
1103 usb_put_hcd(hcd);
1104 the_controller = NULL;
1105 return ret;
1108 usbip_dbg_vhci_hc("bye\n");
1109 return 0;
1112 static int vhci_hcd_remove(struct platform_device *pdev)
1114 struct usb_hcd *hcd;
1116 hcd = platform_get_drvdata(pdev);
1117 if (!hcd)
1118 return 0;
1121 * Disconnects the root hub,
1122 * then reverses the effects of usb_add_hcd(),
1123 * invoking the HCD's stop() methods.
1125 usb_remove_hcd(hcd);
1126 usb_put_hcd(hcd);
1127 the_controller = NULL;
1129 return 0;
1132 #ifdef CONFIG_PM
1134 /* what should happen for USB/IP under suspend/resume? */
1135 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1137 struct usb_hcd *hcd;
1138 int rhport = 0;
1139 int connected = 0;
1140 int ret = 0;
1142 hcd = platform_get_drvdata(pdev);
1144 spin_lock(&the_controller->lock);
1146 for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1147 if (the_controller->port_status[rhport] &
1148 USB_PORT_STAT_CONNECTION)
1149 connected += 1;
1151 spin_unlock(&the_controller->lock);
1153 if (connected > 0) {
1154 dev_info(&pdev->dev, "We have %d active connection%s. Do not "
1155 "suspend.\n", connected, (connected == 1 ? "" : "s"));
1156 ret = -EBUSY;
1157 } else {
1158 dev_info(&pdev->dev, "suspend vhci_hcd");
1159 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1162 return ret;
1165 static int vhci_hcd_resume(struct platform_device *pdev)
1167 struct usb_hcd *hcd;
1169 dev_dbg(&pdev->dev, "%s\n", __func__);
1171 hcd = platform_get_drvdata(pdev);
1172 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1173 usb_hcd_poll_rh_status(hcd);
1175 return 0;
1178 #else
1180 #define vhci_hcd_suspend NULL
1181 #define vhci_hcd_resume NULL
1183 #endif
1185 static struct platform_driver vhci_driver = {
1186 .probe = vhci_hcd_probe,
1187 .remove = __devexit_p(vhci_hcd_remove),
1188 .suspend = vhci_hcd_suspend,
1189 .resume = vhci_hcd_resume,
1190 .driver = {
1191 .name = (char *) driver_name,
1192 .owner = THIS_MODULE,
1197 * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1198 * We need to add this virtual device as a platform device arbitrarily:
1199 * 1. platform_device_register()
1201 static void the_pdev_release(struct device *dev)
1203 return;
1206 static struct platform_device the_pdev = {
1207 /* should be the same name as driver_name */
1208 .name = (char *) driver_name,
1209 .id = -1,
1210 .dev = {
1211 /* .driver = &vhci_driver, */
1212 .release = the_pdev_release,
1216 static int __init vhci_init(void)
1218 int ret;
1220 if (usb_disabled())
1221 return -ENODEV;
1223 ret = platform_driver_register(&vhci_driver);
1224 if (ret < 0)
1225 goto err_driver_register;
1227 ret = platform_device_register(&the_pdev);
1228 if (ret < 0)
1229 goto err_platform_device_register;
1231 pr_info(DRIVER_DESC " v" USBIP_VERSION "\n");
1232 return ret;
1234 err_platform_device_register:
1235 platform_driver_unregister(&vhci_driver);
1236 err_driver_register:
1237 return ret;
1240 static void __exit vhci_cleanup(void)
1242 platform_device_unregister(&the_pdev);
1243 platform_driver_unregister(&vhci_driver);
1246 module_init(vhci_init);
1247 module_exit(vhci_cleanup);
1249 MODULE_AUTHOR(DRIVER_AUTHOR);
1250 MODULE_DESCRIPTION(DRIVER_DESC);
1251 MODULE_LICENSE("GPL");
1252 MODULE_VERSION(USBIP_VERSION);