DMPAPI(void) uart_Close(void *vport) { unsigned char lcr; SerialPort *port = (SerialPort *)vport; if (port == NULL) { err_print((char*)"%s: port is null.\n", __FUNCTION__); return; } if (port->InUse != 0) { // restore old IER & MCR uart_IntDisable(vport); io_DisableINT(); { io_outpb(port->MCR, port->old_mcr); } io_RestoreINT(); irq_Close(); // restore old LSB & MSB #ifdef _VORTEX86EXC_UART_WORKAROUND /* To avoid Vortex86EX(D) write sync. issue. */ io_DisableINT(); { lcr = io_inpb(port->LCR); io_outpb(port->LCR, 0x80); do { io_outpb(port->DLLSB, port->old_lsb); } while (io_inpb(port->DLLSB) != port->old_lsb); do { io_outpb(port->DLMSB, port->old_msb); } while (io_inpb(port->DLMSB) != port->old_msb); io_inpb(0x80); // do IO delay io_outpb(port->LCR, lcr); } io_RestoreINT(); #else _16550_DLAB_Out(port, port->DLLSB, port->old_lsb); _16550_DLAB_Out(port, port->DLMSB, port->old_msb); #endif // restore old LCR & timeout uart_SetFormat(vport, port->old_lcr); uart_SetTimeOut(vport, port->old_TimeOut); vx86_uart_Close(port->com); if (io_Close() == false) err_print((char*)"Close IO lib error!!\n"); } uart_Free(port); }
DMPAPI(void) usb_Close(void *vusb) { USB_Device *usb = (USB_Device *)vusb; if (usb == NULL) { err_print((char*)"%s: USB device is null.\n", __FUNCTION__); return; } if (usb->InUse != 0) { io_DisableINT(); { io_outpb(usb->CFR, io_inpb(usb->CFR) & 0xFE); irq_UninstallISR(usb->nIRQ, (void *)usb); } io_RestoreINT(); irq_Close(); io_outpb(usb->CFR, 0x02); // Soft reset while (io_inpb(usb->CFR) & 0x02); #if defined DMP_DOS_DJGPP if (dma_Free(dma_handle) == false) err_print((char*)"%s: Free DMA buffer fail!!\n", __FUNCTION__); #endif ker_Mfree(usb->EP[0].SetupBuf); ker_Mfree(usb->EP[0].InBuf); ker_Mfree(usb->EP[0].OutBuf); ker_Mfree(usb->EP[1].InBuf); ker_Mfree(usb->EP[2].InBuf); ker_Mfree(usb->EP[2].OutBuf); if (io_Close() == false) err_print((char*)"%s: Close IO lib error!!\n", __FUNCTION__); usb->state = USB_DEV_POWERED; usb->InUse = 0; USB_Disconnect(); } DestoryQueue(usb->rcvd); DestoryQueue(usb->xmit); ker_Mfree((void *)usb); }
DMPAPI(bool) usb_Init(void *vusb) { #if defined DMP_DOS_DJGPP static bool locked = false; int size_temp; #endif USB_Device *usb = (USB_Device *)vusb; if (usb->InUse == 1) return true; // if (USB_IsAttached() == false) return false; USB_Connect(); if (irq_Init() == false) { err_print((char*)"%s: IRQ init fail.\n", __FUNCTION__); io_Close(); return false; } #if defined DMP_DOS_DJGPP if (locked == false) { int i, str_size; DPMI_LOCK_FUNC(SetEPnDLR); DPMI_LOCK_FUNC(Set_Address); DPMI_LOCK_FUNC(Get_Descriptor); DPMI_LOCK_FUNC(Set_Descriptor); DPMI_LOCK_FUNC(Get_Configuration); DPMI_LOCK_FUNC(Set_Configuration); DPMI_LOCK_FUNC(Get_Interface); DPMI_LOCK_FUNC(Set_Interface); DPMI_LOCK_FUNC(Synch_Frame); DPMI_LOCK_FUNC(USB_Standard_Request); DPMI_LOCK_FUNC(Set_Line_Coding); DPMI_LOCK_FUNC(Get_Line_Coding); DPMI_LOCK_FUNC(Set_Control_Line_State); DPMI_LOCK_FUNC(Send_Break); DPMI_LOCK_FUNC(USB_CDC_Request); DPMI_LOCK_FUNC(EP0_SetupHandler); DPMI_LOCK_FUNC(EP0_InHandler); DPMI_LOCK_FUNC(EP0_OutHandler); DPMI_LOCK_FUNC(EP1_InHandler); DPMI_LOCK_FUNC(EP2_InHandler); DPMI_LOCK_FUNC(EP2_OutHandler); DPMI_LOCK_FUNC(usb_Reset); DPMI_LOCK_FUNC(USB_ISR); DPMI_LOCK_VAR(desc_Device); DPMI_LOCK_VAR(desc_Config_Set); DPMI_LOCK_VAR(StringDescTable[0]); DPMI_LOCK_VAR(StringDescTable[1]); DPMI_LOCK_VAR(StringDescTable[2]); DPMI_LOCK_VAR(StringDescTable[3]); DPMI_LOCK_VAR(StringDescTable[4]); DPMI_LOCK_VAR(StringDescTable[5]); DPMI_LOCK_VAR(StringDescTable[6]); locked = true; } #endif io_outpb(usb->CFR, 0x02); // Soft reset while (io_inpb(usb->CFR) & 0x02); if ((usb->EP[0].SetupBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP0_MAX_PACKET_SIZE)) == NULL) goto EP0_SETUP_FAIL; if ((usb->EP[0].InBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP0_MAX_PACKET_SIZE)) == NULL) goto EP0_IN_FAIL; if ((usb->EP[0].OutBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP0_MAX_PACKET_SIZE)) == NULL) goto EP0_OUT_FAIL; if ((usb->EP[1].InBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP1_MAX_PACKET_SIZE_IN)) == NULL) goto EP1_IN_FAIL; if ((usb->EP[2].InBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP2_MAX_PACKET_SIZE_IN)) == NULL) goto EP2_IN_FAIL; if ((usb->EP[2].OutBuf = (BYTE *)ker_Malloc(sizeof(BYTE)*EP2_MAX_PACKET_SIZE_OUT)) == NULL) goto EP2_OUT_FAIL; #if defined DMP_DOS_DJGPP if ((dma_handle = dma_Alloc(EP0_MAX_PACKET_SIZE + EP0_MAX_PACKET_SIZE + EP0_MAX_PACKET_SIZE + EP1_MAX_PACKET_SIZE_IN + EP2_MAX_PACKET_SIZE_IN + EP2_MAX_PACKET_SIZE_OUT, &dma_addr)) == DMA_FAIL) goto EP2_OUT_FAIL; size_temp = 0; usb->EP[0].SetupPhysical = dma_addr; usb->EP[0].InPhysical = dma_addr + (size_temp += EP0_MAX_PACKET_SIZE); usb->EP[0].OutPhysical = dma_addr + (size_temp += EP0_MAX_PACKET_SIZE); usb->EP[1].InPhysical = dma_addr + (size_temp += EP0_MAX_PACKET_SIZE); usb->EP[2].InPhysical = dma_addr + (size_temp += EP1_MAX_PACKET_SIZE_IN); usb->EP[2].OutPhysical = dma_addr + (size_temp += EP2_MAX_PACKET_SIZE_IN); #else usb->EP[0].SetupPhysical = GrabPhysicalMEM((void *)usb->EP[0].SetupBuf); usb->EP[0].InPhysical = GrabPhysicalMEM((void *)usb->EP[0].InBuf); usb->EP[0].OutPhysical = GrabPhysicalMEM((void *)usb->EP[0].OutBuf); usb->EP[1].InPhysical = GrabPhysicalMEM((void *)usb->EP[1].InBuf); usb->EP[2].InPhysical = GrabPhysicalMEM((void *)usb->EP[2].InBuf); usb->EP[2].OutPhysical = GrabPhysicalMEM((void *)usb->EP[2].OutBuf); #endif // usb->DevAddr = 0x00; // usb->ReadySetAddr = false; // io_outpb(usb->DAR, 0x00); // enable USB device io_outpdw(usb->EP[0].SetupDSR, usb->EP[0].SetupPhysical); io_outpdw(usb->EP[0].InDSR , usb->EP[0].InPhysical); io_outpdw(usb->EP[0].OutDSR , usb->EP[0].OutPhysical); io_outpdw(usb->EP[1].InDSR , usb->EP[1].InPhysical); io_outpdw(usb->EP[2].InDSR , usb->EP[2].InPhysical); io_outpdw(usb->EP[2].OutDSR , usb->EP[2].OutPhysical); io_outpw(usb->EP[0].CtrlTR , 0x2000 | EP0_MAX_PACKET_SIZE); io_outpw(usb->EP[1].InTR , 0x3800 | EP1_MAX_PACKET_SIZE_IN); io_outpw(usb->EP[2].InTR , 0x3000 | EP2_MAX_PACKET_SIZE_IN); io_outpw(usb->EP[2].OutTR , 0x3000 | EP2_MAX_PACKET_SIZE_OUT); SetEPnDLR(usb, EP0, SETUP, ENABLE); // io_outpb(usb->DAR, 0x80); // enable USB device // while (!(io_inpb(usb->DAR) & 0x80)); // ClearQueue(usb->rcvd); // ClearQueue(usb->xmit); // io_DisableINT(); // { // io_outpb(usb->CFR, io_inpb(usb->CFR) & 0xFE); // io_outpdw(usb->IER, ISOF + IBRST + ISUSP + IRESM + SYSERR + // IEP0SETUP + IEP0RX + IEP0TX + IEP1TX + IEP2RX + IEP2TX); // io_outpb(usb->CFR, io_inpb(usb->CFR) | 0x01); // } // io_RestoreINT(); // usb->state = USB_DEV_DEFAULT; io_outpb(usb->DAR, 0x80); // enable USB device while (!(io_inpb(usb->DAR) & 0x80)); io_outpdw(usb->ISR, 0xFFFFFFFFL); io_DisableINT(); { io_outpb(usb->CFR, io_inpb(usb->CFR) & 0xFE); irq_Setting(usb->nIRQ, IRQ_LEVEL_TRIGGER); irq_InstallISR(usb->nIRQ, USB_ISR, (void *)usb); io_outpdw(usb->IER, ISOF + IBRST + ISUSP + IRESM + SYSERR + IEP0SETUP + IEP0RX + IEP0TX + IEP1TX + IEP2RX + IEP2TX); io_outpb(usb->CFR, io_inpb(usb->CFR) | 0x01); } io_RestoreINT(); usb->state = USB_DEV_POWERED; usb->InUse = 1; return true; EP2_OUT_FAIL: ker_Mfree(usb->EP[2].InBuf); EP2_IN_FAIL: ker_Mfree(usb->EP[1].InBuf); EP1_IN_FAIL: ker_Mfree(usb->EP[0].OutBuf); EP0_OUT_FAIL: ker_Mfree(usb->EP[0].InBuf); EP0_IN_FAIL: ker_Mfree(usb->EP[0].SetupBuf); EP0_SETUP_FAIL: err_print((char*)"%s: Alloc endpoint buffers error!!\n", __FUNCTION__); irq_Close(); io_Close(); return false; }