github.com/icexin/eggos@v0.4.2-0.20220216025428-78b167e4f349/drivers/ps2/ps2.go (about)

     1  package ps2
     2  
     3  import "github.com/icexin/eggos/kernel/sys"
     4  
     5  const (
     6  	_CMD_PORT  = 0x64
     7  	_DATA_PORT = 0x60
     8  )
     9  
    10  func waitCanWrite() {
    11  	timeout := 1000
    12  	for timeout > 0 {
    13  		timeout--
    14  		x := sys.Inb(_CMD_PORT)
    15  		// input buffer empty means we can write to controller
    16  		if x&0x02 == 0 {
    17  			return
    18  		}
    19  	}
    20  }
    21  
    22  func waitCanRead() {
    23  	timeout := 1000
    24  	for timeout > 0 {
    25  		timeout--
    26  		x := sys.Inb(_CMD_PORT)
    27  		// output buffer full means we can read from controller
    28  		if x&0x01 != 0 {
    29  			return
    30  		}
    31  	}
    32  }
    33  
    34  func ReadDataNoWait() byte {
    35  	return sys.Inb(_DATA_PORT)
    36  }
    37  
    38  func ReadData() byte {
    39  	waitCanRead()
    40  	return sys.Inb(_DATA_PORT)
    41  }
    42  
    43  func WriteData(x byte, needAck bool) {
    44  	waitCanWrite()
    45  	sys.Outb(_DATA_PORT, x)
    46  	if needAck {
    47  		ReadAck()
    48  	}
    49  }
    50  
    51  func ReadAck() {
    52  	x := ReadData()
    53  	if x != 0xFA {
    54  		panic("not a ps2 ack packet")
    55  	}
    56  }
    57  
    58  func ReadCmd() byte {
    59  	return sys.Inb(_CMD_PORT)
    60  }
    61  
    62  func WriteCmd(x byte) {
    63  	waitCanWrite()
    64  	sys.Outb(_CMD_PORT, x)
    65  }
    66  
    67  func WriteMouseData(x byte) {
    68  	WriteCmd(0xD4)
    69  	WriteData(x, true)
    70  }
    71  
    72  func ReadMouseData(x byte) byte {
    73  	WriteCmd(0xD4)
    74  	WriteData(x, true)
    75  	return ReadData()
    76  }