1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "usbip_common.h"
22#include "vhci.h"
23
24#define DRIVER_VERSION "1.0"
25#define DRIVER_AUTHOR "Takahiro Hirofuchi"
26#define DRIVER_DESC "Virtual Host Controller Interface Driver for USB/IP"
27#define DRIVER_LICENCE "GPL"
28MODULE_AUTHOR(DRIVER_AUTHOR);
29MODULE_DESCRIPTION(DRIVER_DESC);
30MODULE_LICENSE(DRIVER_LICENCE);
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
49static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
50 u16 wIndex, char *buff, u16 wLength);
51static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
52 gfp_t mem_flags);
53static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
54static int vhci_start(struct usb_hcd *vhci_hcd);
55static void vhci_stop(struct usb_hcd *hcd);
56static int vhci_get_frame_number(struct usb_hcd *hcd);
57
58static const char driver_name[] = "vhci_hcd";
59static const char driver_desc[] = "USB/IP Virtual Host Contoroller";
60
61struct vhci_hcd *the_controller;
62
63static const char *bit_desc[] = {
64 "CONNECTION",
65 "ENABLE",
66 "SUSPEND",
67 "OVER_CURRENT",
68 "RESET",
69 "R5",
70 "R6",
71 "R7",
72 "POWER",
73 "LOWSPEED",
74 "HIGHSPEED",
75 "PORT_TEST",
76 "INDICATOR",
77 "R13",
78 "R14",
79 "R15",
80 "C_CONNECTION",
81 "C_ENABLE",
82 "C_SUSPEND",
83 "C_OVER_CURRENT",
84 "C_RESET",
85 "R21",
86 "R22",
87 "R23",
88 "R24",
89 "R25",
90 "R26",
91 "R27",
92 "R28",
93 "R29",
94 "R30",
95 "R31",
96};
97
98
99static void dump_port_status(u32 status)
100{
101 int i = 0;
102
103 printk(KERN_DEBUG "status %08x:", status);
104 for (i = 0; i < 32; i++) {
105 if (status & (1 << i))
106 printk(" %s", bit_desc[i]);
107 }
108
109 printk("\n");
110}
111
112
113
114void rh_port_connect(int rhport, enum usb_device_speed speed)
115{
116 unsigned long flags;
117
118 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
119
120 spin_lock_irqsave(&the_controller->lock, flags);
121
122 the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
123 | (1 << USB_PORT_FEAT_C_CONNECTION);
124
125 switch (speed) {
126 case USB_SPEED_HIGH:
127 the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
128 break;
129 case USB_SPEED_LOW:
130 the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
131 break;
132 default:
133 break;
134 }
135
136
137
138
139
140 the_controller->pending_port = rhport;
141
142 spin_unlock_irqrestore(&the_controller->lock, flags);
143
144 usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
145}
146
147void rh_port_disconnect(int rhport)
148{
149 unsigned long flags;
150
151 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
152
153 spin_lock_irqsave(&the_controller->lock, flags);
154
155 the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
156 the_controller->port_status[rhport] |=
157 (1 << USB_PORT_FEAT_C_CONNECTION);
158
159
160
161
162
163
164
165 spin_unlock_irqrestore(&the_controller->lock, flags);
166}
167
168
169
170
171
172#define PORT_C_MASK \
173 ((USB_PORT_STAT_C_CONNECTION \
174 | USB_PORT_STAT_C_ENABLE \
175 | USB_PORT_STAT_C_SUSPEND \
176 | USB_PORT_STAT_C_OVERCURRENT \
177 | USB_PORT_STAT_C_RESET) << 16)
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
201{
202 struct vhci_hcd *vhci;
203 unsigned long flags;
204 int retval = 0;
205
206
207 unsigned long *event_bits = (unsigned long *) buf;
208 int rhport;
209 int changed = 0;
210
211
212 *event_bits = 0;
213
214 vhci = hcd_to_vhci(hcd);
215
216 spin_lock_irqsave(&vhci->lock, flags);
217 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
218 usbip_dbg_vhci_rh("hw accessible flag in on?\n");
219 goto done;
220 }
221
222
223 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
224 if ((vhci->port_status[rhport] & PORT_C_MASK)) {
225
226 usbip_dbg_vhci_rh("port %d is changed\n", rhport);
227
228 *event_bits |= 1 << (rhport + 1);
229 changed = 1;
230 }
231 }
232
233 usbip_uinfo("changed %d\n", changed);
234
235 if (hcd->state == HC_STATE_SUSPENDED)
236 usb_hcd_resume_root_hub(hcd);
237
238 if (changed)
239 retval = 1 + (VHCI_NPORTS / 8);
240 else
241 retval = 0;
242
243done:
244 spin_unlock_irqrestore(&vhci->lock, flags);
245 return retval;
246}
247
248
249static inline void hub_descriptor(struct usb_hub_descriptor *desc)
250{
251 memset(desc, 0, sizeof(*desc));
252 desc->bDescriptorType = 0x29;
253 desc->bDescLength = 9;
254 desc->wHubCharacteristics = (__force __u16)
255 (__constant_cpu_to_le16(0x0001));
256 desc->bNbrPorts = VHCI_NPORTS;
257 desc->bitmap[0] = 0xff;
258 desc->bitmap[1] = 0xff;
259}
260
261static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
262 u16 wIndex, char *buf, u16 wLength)
263{
264 struct vhci_hcd *dum;
265 int retval = 0;
266 unsigned long flags;
267 int rhport;
268
269 u32 prev_port_status[VHCI_NPORTS];
270
271 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
272 return -ETIMEDOUT;
273
274
275
276
277
278 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
279 wIndex);
280 if (wIndex > VHCI_NPORTS)
281 printk(KERN_ERR "%s: invalid port number %d\n", __func__,
282 wIndex);
283 rhport = ((__u8)(wIndex & 0x00ff)) - 1;
284
285 dum = hcd_to_vhci(hcd);
286
287 spin_lock_irqsave(&dum->lock, flags);
288
289
290 if (usbip_dbg_flag_vhci_rh) {
291 int i = 0;
292 for (i = 0; i < VHCI_NPORTS; i++)
293 prev_port_status[i] = dum->port_status[i];
294 }
295
296 switch (typeReq) {
297 case ClearHubFeature:
298 usbip_dbg_vhci_rh(" ClearHubFeature\n");
299 break;
300 case ClearPortFeature:
301 switch (wValue) {
302 case USB_PORT_FEAT_SUSPEND:
303 if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
304
305 dum->resuming = 1;
306 dum->re_timeout =
307 jiffies + msecs_to_jiffies(20);
308 }
309 break;
310 case USB_PORT_FEAT_POWER:
311 usbip_dbg_vhci_rh(" ClearPortFeature: "
312 "USB_PORT_FEAT_POWER\n");
313 dum->port_status[rhport] = 0;
314
315
316 dum->resuming = 0;
317 break;
318 case USB_PORT_FEAT_C_RESET:
319 usbip_dbg_vhci_rh(" ClearPortFeature: "
320 "USB_PORT_FEAT_C_RESET\n");
321 switch (dum->vdev[rhport].speed) {
322 case USB_SPEED_HIGH:
323 dum->port_status[rhport] |=
324 USB_PORT_STAT_HIGH_SPEED;
325 break;
326 case USB_SPEED_LOW:
327 dum->port_status[rhport] |=
328 USB_PORT_STAT_LOW_SPEED;
329 break;
330 default:
331 break;
332 }
333 default:
334 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
335 wValue);
336 dum->port_status[rhport] &= ~(1 << wValue);
337 }
338 break;
339 case GetHubDescriptor:
340 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
341 hub_descriptor((struct usb_hub_descriptor *) buf);
342 break;
343 case GetHubStatus:
344 usbip_dbg_vhci_rh(" GetHubStatus\n");
345 *(__le32 *) buf = __constant_cpu_to_le32(0);
346 break;
347 case GetPortStatus:
348 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
349 if (wIndex > VHCI_NPORTS || wIndex < 1) {
350 printk(KERN_ERR "%s: invalid port number %d\n",
351 __func__, wIndex);
352 retval = -EPIPE;
353 }
354
355
356
357
358
359
360 if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
361 printk(KERN_ERR "%s: not yet\n", __func__);
362 dum->port_status[rhport] |=
363 (1 << USB_PORT_FEAT_C_SUSPEND);
364 dum->port_status[rhport] &=
365 ~(1 << USB_PORT_FEAT_SUSPEND);
366 dum->resuming = 0;
367 dum->re_timeout = 0;
368
369
370
371
372
373 }
374
375 if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
376 0 && time_after(jiffies, dum->re_timeout)) {
377 dum->port_status[rhport] |=
378 (1 << USB_PORT_FEAT_C_RESET);
379 dum->port_status[rhport] &=
380 ~(1 << USB_PORT_FEAT_RESET);
381 dum->re_timeout = 0;
382
383 if (dum->vdev[rhport].ud.status ==
384 VDEV_ST_NOTASSIGNED) {
385 usbip_dbg_vhci_rh(" enable rhport %d "
386 "(status %u)\n",
387 rhport,
388 dum->vdev[rhport].ud.status);
389 dum->port_status[rhport] |=
390 USB_PORT_STAT_ENABLE;
391 }
392#if 0
393 if (dum->driver) {
394
395 dum->port_status[rhport] |=
396 USB_PORT_STAT_ENABLE;
397
398 dum->gadget.speed = dum->driver->speed;
399 dum->gadget.ep0->maxpacket = 64;
400 switch (dum->gadget.speed) {
401 case USB_SPEED_HIGH:
402 dum->port_status[rhport] |=
403 USB_PORT_STAT_HIGH_SPEED;
404 break;
405 case USB_SPEED_LOW:
406 dum->gadget.ep0->maxpacket = 8;
407 dum->port_status[rhport] |=
408 USB_PORT_STAT_LOW_SPEED;
409 break;
410 default:
411 dum->gadget.speed = USB_SPEED_FULL;
412 break;
413 }
414 }
415#endif
416
417 }
418 ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
419 ((u16 *) buf)[1] =
420 cpu_to_le16(dum->port_status[rhport] >> 16);
421
422 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
423 ((u16 *)buf)[1]);
424 break;
425 case SetHubFeature:
426 usbip_dbg_vhci_rh(" SetHubFeature\n");
427 retval = -EPIPE;
428 break;
429 case SetPortFeature:
430 switch (wValue) {
431 case USB_PORT_FEAT_SUSPEND:
432 usbip_dbg_vhci_rh(" SetPortFeature: "
433 "USB_PORT_FEAT_SUSPEND\n");
434 printk(KERN_ERR "%s: not yet\n", __func__);
435#if 0
436 dum->port_status[rhport] |=
437 (1 << USB_PORT_FEAT_SUSPEND);
438 if (dum->driver->suspend) {
439 spin_unlock(&dum->lock);
440 dum->driver->suspend(&dum->gadget);
441 spin_lock(&dum->lock);
442 }
443#endif
444 break;
445 case USB_PORT_FEAT_RESET:
446 usbip_dbg_vhci_rh(" SetPortFeature: "
447 "USB_PORT_FEAT_RESET\n");
448
449 if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
450 dum->port_status[rhport] &=
451 ~(USB_PORT_STAT_ENABLE |
452 USB_PORT_STAT_LOW_SPEED |
453 USB_PORT_STAT_HIGH_SPEED);
454#if 0
455 if (dum->driver) {
456 dev_dbg(hardware, "disconnect\n");
457 stop_activity(dum, dum->driver);
458 }
459#endif
460
461
462 }
463
464 dum->re_timeout = jiffies + msecs_to_jiffies(50);
465
466
467 default:
468 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
469 wValue);
470 dum->port_status[rhport] |= (1 << wValue);
471 }
472 break;
473
474 default:
475 printk(KERN_ERR "%s: default: no such request\n", __func__);
476
477
478
479
480
481 retval = -EPIPE;
482 }
483
484 if (usbip_dbg_flag_vhci_rh) {
485 printk(KERN_DEBUG "port %d\n", rhport);
486 dump_port_status(prev_port_status[rhport]);
487 dump_port_status(dum->port_status[rhport]);
488 }
489 usbip_dbg_vhci_rh(" bye\n");
490
491 spin_unlock_irqrestore(&dum->lock, flags);
492
493 return retval;
494}
495
496
497
498
499
500static struct vhci_device *get_vdev(struct usb_device *udev)
501{
502 int i;
503
504 if (!udev)
505 return NULL;
506
507 for (i = 0; i < VHCI_NPORTS; i++)
508 if (the_controller->vdev[i].udev == udev)
509 return port_to_vdev(i);
510
511 return NULL;
512}
513
514static void vhci_tx_urb(struct urb *urb)
515{
516 struct vhci_device *vdev = get_vdev(urb->dev);
517 struct vhci_priv *priv;
518 unsigned long flag;
519
520 if (!vdev) {
521 err("could not get virtual device");
522
523 return;
524 }
525
526 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
527
528 spin_lock_irqsave(&vdev->priv_lock, flag);
529
530 if (!priv) {
531 dev_err(&urb->dev->dev, "malloc vhci_priv\n");
532 spin_unlock_irqrestore(&vdev->priv_lock, flag);
533 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
534 return;
535 }
536
537 priv->seqnum = atomic_inc_return(&the_controller->seqnum);
538 if (priv->seqnum == 0xffff)
539 usbip_uinfo("seqnum max\n");
540
541 priv->vdev = vdev;
542 priv->urb = urb;
543
544 urb->hcpriv = (void *) priv;
545
546
547 list_add_tail(&priv->list, &vdev->priv_tx);
548
549 wake_up(&vdev->waitq_tx);
550 spin_unlock_irqrestore(&vdev->priv_lock, flag);
551}
552
553static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
554 gfp_t mem_flags)
555{
556 struct device *dev = &urb->dev->dev;
557 int ret = 0;
558 unsigned long flags;
559
560 usbip_dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
561 hcd, urb, mem_flags);
562
563
564 BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
565
566 spin_lock_irqsave(&the_controller->lock, flags);
567
568 if (urb->status != -EINPROGRESS) {
569 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
570 spin_unlock_irqrestore(&the_controller->lock, flags);
571 return urb->status;
572 }
573
574 ret = usb_hcd_link_urb_to_ep(hcd, urb);
575 if (ret)
576 goto no_need_unlink;
577
578
579
580
581
582
583
584
585
586
587
588 if (usb_pipedevice(urb->pipe) == 0) {
589 __u8 type = usb_pipetype(urb->pipe);
590 struct usb_ctrlrequest *ctrlreq =
591 (struct usb_ctrlrequest *) urb->setup_packet;
592 struct vhci_device *vdev =
593 port_to_vdev(the_controller->pending_port);
594
595 if (type != PIPE_CONTROL || !ctrlreq) {
596 dev_err(dev, "invalid request to devnum 0\n");
597 ret = -EINVAL;
598 goto no_need_xmit;
599 }
600
601 switch (ctrlreq->bRequest) {
602 case USB_REQ_SET_ADDRESS:
603
604 dev_info(dev, "SetAddress Request (%d) to port %d\n",
605 ctrlreq->wValue, vdev->rhport);
606
607 vdev->udev = urb->dev;
608
609 spin_lock(&vdev->ud.lock);
610 vdev->ud.status = VDEV_ST_USED;
611 spin_unlock(&vdev->ud.lock);
612
613 if (urb->status == -EINPROGRESS) {
614
615
616 urb->status = 0;
617 }
618
619 goto no_need_xmit;
620
621 case USB_REQ_GET_DESCRIPTOR:
622 if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
623 usbip_dbg_vhci_hc("Not yet?: "
624 "Get_Descriptor to device 0 "
625 "(get max pipe size)\n");
626
627
628 vdev->udev = urb->dev;
629 goto out;
630
631 default:
632
633 dev_err(dev, "invalid request to devnum 0 bRequest %u, "
634 "wValue %u\n", ctrlreq->bRequest,
635 ctrlreq->wValue);
636 ret = -EINVAL;
637 goto no_need_xmit;
638 }
639
640 }
641
642out:
643 vhci_tx_urb(urb);
644
645 spin_unlock_irqrestore(&the_controller->lock, flags);
646
647 return 0;
648
649no_need_xmit:
650 usb_hcd_unlink_urb_from_ep(hcd, urb);
651no_need_unlink:
652 spin_unlock_irqrestore(&the_controller->lock, flags);
653
654 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
655
656 return ret;
657}
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
706{
707 unsigned long flags;
708 struct vhci_priv *priv;
709 struct vhci_device *vdev;
710
711 usbip_uinfo("vhci_hcd: dequeue a urb %p\n", urb);
712
713
714 spin_lock_irqsave(&the_controller->lock, flags);
715
716 priv = urb->hcpriv;
717 if (!priv) {
718
719
720 spin_unlock_irqrestore(&the_controller->lock, flags);
721 return 0;
722 }
723
724 {
725 int ret = 0;
726 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
727 if (ret) {
728 spin_unlock_irqrestore(&the_controller->lock, flags);
729 return ret;
730 }
731 }
732
733
734 vdev = priv->vdev;
735
736 if (!vdev->ud.tcp_socket) {
737
738 unsigned long flags2;
739
740 spin_lock_irqsave(&vdev->priv_lock, flags2);
741
742 usbip_uinfo("vhci_hcd: device %p seems to be disconnected\n",
743 vdev);
744 list_del(&priv->list);
745 kfree(priv);
746 urb->hcpriv = NULL;
747
748 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
749
750
751
752
753
754
755 usbip_uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n",
756 urb);
757
758 usb_hcd_unlink_urb_from_ep(hcd, urb);
759
760 spin_unlock_irqrestore(&the_controller->lock, flags);
761 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
762 urb->status);
763 spin_lock_irqsave(&the_controller->lock, flags);
764
765 } else {
766
767 unsigned long flags2;
768 struct vhci_unlink *unlink;
769
770 spin_lock_irqsave(&vdev->priv_lock, flags2);
771
772
773 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
774 if (!unlink) {
775 usbip_uerr("malloc vhci_unlink\n");
776 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
777 spin_unlock_irqrestore(&the_controller->lock, flags);
778 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
779 return -ENOMEM;
780 }
781
782 unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
783 if (unlink->seqnum == 0xffff)
784 usbip_uinfo("seqnum max\n");
785
786 unlink->unlink_seqnum = priv->seqnum;
787
788 usbip_uinfo("vhci_hcd: device %p seems to be still connected\n",
789 vdev);
790
791
792
793 list_add_tail(&unlink->list, &vdev->unlink_tx);
794 wake_up(&vdev->waitq_tx);
795
796 spin_unlock_irqrestore(&vdev->priv_lock, flags2);
797 }
798
799
800 if (!vdev->ud.tcp_socket) {
801
802 usbip_uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n",
803 urb);
804
805 usb_hcd_unlink_urb_from_ep(hcd, urb);
806
807 spin_unlock_irqrestore(&the_controller->lock, flags);
808 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
809 urb->status);
810 spin_lock_irqsave(&the_controller->lock, flags);
811 }
812
813 spin_unlock_irqrestore(&the_controller->lock, flags);
814
815 usbip_dbg_vhci_hc("leave\n");
816 return 0;
817}
818
819
820static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
821{
822 struct vhci_unlink *unlink, *tmp;
823
824 spin_lock(&vdev->priv_lock);
825
826 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
827 list_del(&unlink->list);
828 kfree(unlink);
829 }
830
831 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
832 list_del(&unlink->list);
833 kfree(unlink);
834 }
835
836 spin_unlock(&vdev->priv_lock);
837}
838
839
840
841
842
843
844static void vhci_shutdown_connection(struct usbip_device *ud)
845{
846 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
847
848
849 if (ud->tcp_socket) {
850 usbip_udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
851 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
852 }
853
854 usbip_stop_threads(&vdev->ud);
855 usbip_uinfo("stop threads\n");
856
857
858 if (vdev->ud.tcp_socket != NULL) {
859 sock_release(vdev->ud.tcp_socket);
860 vdev->ud.tcp_socket = NULL;
861 }
862 usbip_uinfo("release socket\n");
863
864 vhci_device_unlink_cleanup(vdev);
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886 rh_port_disconnect(vdev->rhport);
887
888 usbip_uinfo("disconnect device\n");
889}
890
891
892static void vhci_device_reset(struct usbip_device *ud)
893{
894 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
895
896 spin_lock(&ud->lock);
897
898 vdev->speed = 0;
899 vdev->devid = 0;
900
901 ud->tcp_socket = NULL;
902
903 ud->status = VDEV_ST_NULL;
904
905 spin_unlock(&ud->lock);
906}
907
908static void vhci_device_unusable(struct usbip_device *ud)
909{
910 spin_lock(&ud->lock);
911
912 ud->status = VDEV_ST_ERROR;
913
914 spin_unlock(&ud->lock);
915}
916
917static void vhci_device_init(struct vhci_device *vdev)
918{
919 memset(vdev, 0, sizeof(*vdev));
920
921 usbip_task_init(&vdev->ud.tcp_rx, "vhci_rx", vhci_rx_loop);
922 usbip_task_init(&vdev->ud.tcp_tx, "vhci_tx", vhci_tx_loop);
923
924 vdev->ud.side = USBIP_VHCI;
925 vdev->ud.status = VDEV_ST_NULL;
926
927 spin_lock_init(&vdev->ud.lock);
928
929 INIT_LIST_HEAD(&vdev->priv_rx);
930 INIT_LIST_HEAD(&vdev->priv_tx);
931 INIT_LIST_HEAD(&vdev->unlink_tx);
932 INIT_LIST_HEAD(&vdev->unlink_rx);
933
934 spin_lock_init(&vdev->priv_lock);
935
936 init_waitqueue_head(&vdev->waitq_tx);
937
938 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
939 vdev->ud.eh_ops.reset = vhci_device_reset;
940 vdev->ud.eh_ops.unusable = vhci_device_unusable;
941
942 usbip_start_eh(&vdev->ud);
943}
944
945
946
947
948static int vhci_start(struct usb_hcd *hcd)
949{
950 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
951 int rhport;
952 int err = 0;
953
954 usbip_dbg_vhci_hc("enter vhci_start\n");
955
956
957
958
959 for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
960 struct vhci_device *vdev = &vhci->vdev[rhport];
961 vhci_device_init(vdev);
962 vdev->rhport = rhport;
963 }
964
965 atomic_set(&vhci->seqnum, 0);
966 spin_lock_init(&vhci->lock);
967
968
969
970 hcd->power_budget = 0;
971 hcd->state = HC_STATE_RUNNING;
972 hcd->uses_new_polling = 1;
973
974
975
976 err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
977 if (err) {
978 usbip_uerr("create sysfs files\n");
979 return err;
980 }
981
982 return 0;
983}
984
985static void vhci_stop(struct usb_hcd *hcd)
986{
987 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
988 int rhport = 0;
989
990 usbip_dbg_vhci_hc("stop VHCI controller\n");
991
992
993
994 sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
995
996
997 for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
998 struct vhci_device *vdev = &vhci->vdev[rhport];
999
1000 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1001 usbip_stop_eh(&vdev->ud);
1002 }
1003
1004
1005 usbip_uinfo("vhci_stop done\n");
1006}
1007
1008
1009
1010static int vhci_get_frame_number(struct usb_hcd *hcd)
1011{
1012 usbip_uerr("Not yet implemented\n");
1013 return 0;
1014}
1015
1016
1017#ifdef CONFIG_PM
1018
1019
1020static int vhci_bus_suspend(struct usb_hcd *hcd)
1021{
1022 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1023
1024 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1025
1026 spin_lock_irq(&vhci->lock);
1027
1028
1029 hcd->state = HC_STATE_SUSPENDED;
1030 spin_unlock_irq(&vhci->lock);
1031
1032 return 0;
1033}
1034
1035static int vhci_bus_resume(struct usb_hcd *hcd)
1036{
1037 struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1038 int rc = 0;
1039
1040 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1041
1042 spin_lock_irq(&vhci->lock);
1043 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1044 rc = -ESHUTDOWN;
1045 } else {
1046
1047
1048
1049
1050 hcd->state = HC_STATE_RUNNING;
1051 }
1052 spin_unlock_irq(&vhci->lock);
1053 return rc;
1054
1055 return 0;
1056}
1057
1058#else
1059
1060#define vhci_bus_suspend NULL
1061#define vhci_bus_resume NULL
1062#endif
1063
1064
1065
1066static struct hc_driver vhci_hc_driver = {
1067 .description = driver_name,
1068 .product_desc = driver_desc,
1069 .hcd_priv_size = sizeof(struct vhci_hcd),
1070
1071 .flags = HCD_USB2,
1072
1073 .start = vhci_start,
1074 .stop = vhci_stop,
1075
1076 .urb_enqueue = vhci_urb_enqueue,
1077 .urb_dequeue = vhci_urb_dequeue,
1078
1079 .get_frame_number = vhci_get_frame_number,
1080
1081 .hub_status_data = vhci_hub_status,
1082 .hub_control = vhci_hub_control,
1083 .bus_suspend = vhci_bus_suspend,
1084 .bus_resume = vhci_bus_resume,
1085};
1086
1087static int vhci_hcd_probe(struct platform_device *pdev)
1088{
1089 struct usb_hcd *hcd;
1090 int ret;
1091
1092 usbip_uinfo("proving...\n");
1093
1094 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1095
1096
1097 if (pdev->dev.dma_mask) {
1098 dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1099 return -EINVAL;
1100 }
1101
1102
1103
1104
1105
1106 hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1107 if (!hcd) {
1108 usbip_uerr("create hcd failed\n");
1109 return -ENOMEM;
1110 }
1111
1112
1113
1114 the_controller = hcd_to_vhci(hcd);
1115
1116
1117
1118
1119
1120 ret = usb_add_hcd(hcd, 0, 0);
1121 if (ret != 0) {
1122 usbip_uerr("usb_add_hcd failed %d\n", ret);
1123 usb_put_hcd(hcd);
1124 the_controller = NULL;
1125 return ret;
1126 }
1127
1128
1129 usbip_dbg_vhci_hc("bye\n");
1130 return 0;
1131}
1132
1133
1134static int vhci_hcd_remove(struct platform_device *pdev)
1135{
1136 struct usb_hcd *hcd;
1137
1138 hcd = platform_get_drvdata(pdev);
1139 if (!hcd)
1140 return 0;
1141
1142
1143
1144
1145
1146
1147 usb_remove_hcd(hcd);
1148 usb_put_hcd(hcd);
1149 the_controller = NULL;
1150
1151
1152 return 0;
1153}
1154
1155
1156
1157#ifdef CONFIG_PM
1158
1159
1160static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1161{
1162 struct usb_hcd *hcd;
1163 int rhport = 0;
1164 int connected = 0;
1165 int ret = 0;
1166
1167 dev_dbg(&pdev->dev, "%s\n", __func__);
1168
1169 hcd = platform_get_drvdata(pdev);
1170
1171 spin_lock(&the_controller->lock);
1172
1173 for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1174 if (the_controller->port_status[rhport] &
1175 USB_PORT_STAT_CONNECTION)
1176 connected += 1;
1177
1178 spin_unlock(&the_controller->lock);
1179
1180 if (connected > 0) {
1181 usbip_uinfo("We have %d active connection%s. Do not suspend.\n",
1182 connected, (connected == 1 ? "" : "s"));
1183 ret = -EBUSY;
1184 } else {
1185 usbip_uinfo("suspend vhci_hcd");
1186 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1187 }
1188
1189 return ret;
1190}
1191
1192static int vhci_hcd_resume(struct platform_device *pdev)
1193{
1194 struct usb_hcd *hcd;
1195
1196 dev_dbg(&pdev->dev, "%s\n", __func__);
1197
1198 hcd = platform_get_drvdata(pdev);
1199 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1200 usb_hcd_poll_rh_status(hcd);
1201
1202 return 0;
1203}
1204
1205#else
1206
1207#define vhci_hcd_suspend NULL
1208#define vhci_hcd_resume NULL
1209
1210#endif
1211
1212
1213static struct platform_driver vhci_driver = {
1214 .probe = vhci_hcd_probe,
1215 .remove = __devexit_p(vhci_hcd_remove),
1216 .suspend = vhci_hcd_suspend,
1217 .resume = vhci_hcd_resume,
1218 .driver = {
1219 .name = (char *) driver_name,
1220 .owner = THIS_MODULE,
1221 },
1222};
1223
1224
1225
1226
1227
1228
1229
1230
1231static void the_pdev_release(struct device *dev)
1232{
1233 return;
1234}
1235
1236static struct platform_device the_pdev = {
1237
1238 .name = (char *) driver_name,
1239 .id = -1,
1240 .dev = {
1241
1242 .release = the_pdev_release,
1243 },
1244};
1245
1246static int __init vhci_init(void)
1247{
1248 int ret;
1249
1250 usbip_dbg_vhci_hc("enter\n");
1251 if (usb_disabled())
1252 return -ENODEV;
1253
1254 printk(KERN_INFO KBUILD_MODNAME ": %s, %s\n", driver_name,
1255 DRIVER_VERSION);
1256
1257 ret = platform_driver_register(&vhci_driver);
1258 if (ret < 0)
1259 goto err_driver_register;
1260
1261 ret = platform_device_register(&the_pdev);
1262 if (ret < 0)
1263 goto err_platform_device_register;
1264
1265 usbip_dbg_vhci_hc("bye\n");
1266 return ret;
1267
1268
1269err_platform_device_register:
1270 platform_driver_unregister(&vhci_driver);
1271
1272err_driver_register:
1273 usbip_dbg_vhci_hc("bye\n");
1274 return ret;
1275}
1276module_init(vhci_init);
1277
1278static void __exit vhci_cleanup(void)
1279{
1280 usbip_dbg_vhci_hc("enter\n");
1281
1282 platform_device_unregister(&the_pdev);
1283 platform_driver_unregister(&vhci_driver);
1284
1285 usbip_dbg_vhci_hc("bye\n");
1286}
1287module_exit(vhci_cleanup);
1288