@@ -322,6 +322,9 @@ static void pci_xhci_dev_destroy(struct pci_xhci_dev_emu *de);
322
322
static int pci_xhci_port_chg (struct pci_xhci_vdev * xdev , int port , int conn );
323
323
static void pci_xhci_set_evtrb (struct xhci_trb * evtrb , uint64_t port ,
324
324
uint32_t errcode , uint32_t evtype );
325
+ static int pci_xhci_xfer_complete (struct pci_xhci_vdev * xdev ,
326
+ struct usb_data_xfer * xfer , uint32_t slot , uint32_t epid ,
327
+ int * do_intr );
325
328
326
329
static int
327
330
pci_xhci_native_usb_dev_conn_cb (void * hci_data , void * dev_data )
@@ -417,6 +420,56 @@ pci_xhci_native_usb_dev_disconn_cb(void *hci_data, void *dev_data)
417
420
return 0 ;
418
421
}
419
422
423
+ /*
424
+ * return value:
425
+ * = 0: succeed without interrupt
426
+ * > 0: succeed with interrupt
427
+ * < 0: failure
428
+ */
429
+ static int
430
+ pci_xhci_usb_dev_notify_cb (void * hci_data , void * udev_data )
431
+ {
432
+ int slot , epid , intr , rc ;
433
+ struct usb_data_xfer * xfer ;
434
+ struct pci_xhci_dev_emu * edev ;
435
+ struct pci_xhci_vdev * xdev ;
436
+
437
+ xfer = udev_data ;
438
+ if (!xfer )
439
+ return -1 ;
440
+
441
+ epid = xfer -> epid ;
442
+ edev = xfer -> dev ;
443
+ if (!edev )
444
+ return -1 ;
445
+
446
+ xdev = edev -> xdev ;
447
+ if (!xdev )
448
+ return -1 ;
449
+
450
+ slot = edev -> hci .hci_address ;
451
+ rc = pci_xhci_xfer_complete (xdev , xfer , slot , epid , & intr );
452
+
453
+ if (rc )
454
+ return -1 ;
455
+ else if (intr )
456
+ return 1 ;
457
+ else
458
+ return 0 ;
459
+ }
460
+
461
+ static int
462
+ pci_xhci_usb_dev_intr_cb (void * hci_data , void * udev_data )
463
+ {
464
+ struct pci_xhci_dev_emu * edev ;
465
+
466
+ edev = hci_data ;
467
+ if (edev && edev -> xdev )
468
+ pci_xhci_assert_interrupt (edev -> xdev );
469
+
470
+ return 0 ;
471
+ }
472
+
420
473
static struct pci_xhci_dev_emu *
421
474
pci_xhci_dev_create (struct pci_xhci_vdev * xdev , void * dev_data )
422
475
{
@@ -432,15 +485,22 @@ pci_xhci_dev_create(struct pci_xhci_vdev *xdev, void *dev_data)
432
485
if (!ue )
433
486
return NULL ;
434
487
435
- /* TODO: following function pointers will be populated in future */
488
+ /*
489
+ * TODO: at present, the following functions are
490
+ * enough. But for the purpose to be compatible with
491
+ * usb_mouse.c, the high level design including the
492
+ * function interface should be changed and refined
493
+ * in future.
494
+ */
436
495
ue -> ue_init = usb_dev_init ;
437
496
ue -> ue_request = usb_dev_request ;
438
- ue -> ue_data = NULL ;
497
+ ue -> ue_data = usb_dev_data ;
439
498
ue -> ue_info = usb_dev_info ;
440
499
ue -> ue_reset = usb_dev_reset ;
441
500
ue -> ue_remove = NULL ;
442
501
ue -> ue_stop = NULL ;
443
502
ue -> ue_deinit = usb_dev_deinit ;
503
+ ue -> ue_devtype = USB_DEV_PORT_MAPPER ;
444
504
445
505
ud = ue -> ue_init (dev_data , NULL );
446
506
if (!ud )
@@ -489,10 +549,16 @@ pci_xhci_dev_destroy(struct pci_xhci_dev_emu *de)
489
549
ue = de -> dev_ue ;
490
550
ud = de -> dev_instance ;
491
551
if (ue ) {
492
- assert (ue -> ue_deinit );
493
- ue -> ue_deinit (ud );
552
+ if (ue -> ue_devtype == USB_DEV_PORT_MAPPER ) {
553
+ assert (ue -> ue_deinit );
554
+ if (ue -> ue_deinit )
555
+ ue -> ue_deinit (ud );
556
+ }
494
557
}
495
- free (ue );
558
+
559
+ if (ue -> ue_devtype == USB_DEV_PORT_MAPPER )
560
+ free (ue );
561
+
496
562
free (de );
497
563
}
498
564
}
@@ -879,9 +945,11 @@ pci_xhci_init_ep(struct pci_xhci_dev_emu *dev, int epid)
879
945
880
946
if (devep -> ep_xfer == NULL ) {
881
947
devep -> ep_xfer = malloc (sizeof (struct usb_data_xfer ));
882
- if (devep -> ep_xfer )
948
+ if (devep -> ep_xfer ) {
883
949
USB_DATA_XFER_INIT (devep -> ep_xfer );
884
- else
950
+ devep -> ep_xfer -> dev = (void * )dev ;
951
+ devep -> ep_xfer -> epid = epid ;
952
+ } else
885
953
return -1 ;
886
954
}
887
955
return 0 ;
@@ -1821,6 +1889,7 @@ pci_xhci_xfer_complete(struct pci_xhci_vdev *xdev,
1821
1889
}
1822
1890
1823
1891
xfer -> ndata -- ;
1892
+ xfer -> head = (xfer -> head + 1 ) % USB_MAX_XFER_BLOCKS ;
1824
1893
edtla += xfer -> data [i ].bdone ;
1825
1894
1826
1895
trb -> dwTrb3 = (trb -> dwTrb3 & ~0x1 ) | (xfer -> data [i ].ccs );
@@ -1929,7 +1998,12 @@ pci_xhci_try_usb_xfer(struct pci_xhci_vdev *xdev,
1929
1998
if (USB_DATA_GET_ERRCODE (& xfer -> data [xfer -> head ]) ==
1930
1999
USB_NAK )
1931
2000
err = XHCI_TRB_ERROR_SUCCESS ;
1932
- } else {
2001
+ }
2002
+ /*
2003
+ * Only for usb_mouse.c, emulation with port mapping will do it
2004
+ * by the libusb callback function.
2005
+ */
2006
+ else if (dev -> dev_ue -> ue_devtype == USB_DEV_STATIC ) {
1933
2007
err = pci_xhci_xfer_complete (xdev , xfer , slot , epid ,
1934
2008
& do_intr );
1935
2009
if (err == XHCI_TRB_ERROR_SUCCESS && do_intr )
@@ -2050,13 +2124,12 @@ pci_xhci_handle_transfer(struct pci_xhci_vdev *xdev,
2050
2124
/* fall through */
2051
2125
2052
2126
case XHCI_TRB_TYPE_DATA_STAGE :
2053
- xfer_block =
2054
- usb_data_xfer_append (xfer , (void * )(trbflags &
2055
- XHCI_TRB_3_IDT_BIT ?
2056
- & trb -> qwTrb0 :
2057
- XHCI_GADDR (xdev ,
2058
- trb -> qwTrb0 )),
2059
- trb -> dwTrb2 & 0x1FFFF , (void * )addr , ccs );
2127
+ xfer_block = usb_data_xfer_append (xfer ,
2128
+ (void * )(trbflags & XHCI_TRB_3_IDT_BIT ?
2129
+ & trb -> qwTrb0 :
2130
+ XHCI_GADDR (xdev , trb -> qwTrb0 )),
2131
+ trb -> dwTrb2 & 0x1FFFF , (void * )addr ,
2132
+ ccs );
2060
2133
break ;
2061
2134
2062
2135
case XHCI_TRB_TYPE_STATUS_STAGE :
@@ -2200,8 +2273,14 @@ pci_xhci_device_doorbell(struct pci_xhci_vdev *xdev,
2200
2273
if (ep_ctx -> qwEpCtx2 == 0 )
2201
2274
return ;
2202
2275
2276
+ /*
2277
+ * In USB emulation with port mapping, the following transfer should
2278
+ * NOT be called, or else the interrupt transfer will result
2279
+ * of invalid and infinite loop. It is used by usb_mouse.c only.
2280
+ */
2203
2281
/* handle pending transfers */
2204
- if (devep -> ep_xfer -> ndata > 0 ) {
2282
+ if (dev -> dev_ue && dev -> dev_ue -> ue_devtype == USB_DEV_STATIC &&
2283
+ devep -> ep_xfer -> ndata > 0 ) {
2205
2284
pci_xhci_try_usb_xfer (xdev , dev , devep , ep_ctx , slot , epid );
2206
2285
return ;
2207
2286
}
@@ -3090,10 +3169,10 @@ pci_xhci_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts)
3090
3169
if (xdev -> ndevices == 0 )
3091
3170
if (usb_dev_sys_init (pci_xhci_native_usb_dev_conn_cb ,
3092
3171
pci_xhci_native_usb_dev_disconn_cb ,
3093
- NULL , NULL , xdev ,
3094
- usb_get_log_level ()) < 0 ) {
3172
+ pci_xhci_usb_dev_notify_cb ,
3173
+ pci_xhci_usb_dev_intr_cb ,
3174
+ xdev , usb_get_log_level ()) < 0 )
3095
3175
goto done ;
3096
- }
3097
3176
3098
3177
xdev -> caplength = XHCI_SET_CAPLEN (XHCI_CAPLEN ) |
3099
3178
XHCI_SET_HCIVERSION (0x0100 );
0 commit comments