github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/console/i8042_linux.go (about)

     1  // Copyright 2012-2020 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file contains support functions for i8042 io for Linux.
     6  package main
     7  
     8  const (
     9  	i8042Data = 0x60 /* data port */
    10  
    11  	i8042Status   = 0x64 /* status port */
    12  	i8042Inready  = 0x01 /*  input character ready */
    13  	i8042Outbusy  = 0x02 /*  output busy */
    14  	i8042Sysflag  = 0x04 /*  system flag */
    15  	i8042Cmddata  = 0x08 /*  cmd==0 data==1 */
    16  	i8042Inhibit  = 0x10 /*  keyboard/mouse inhibited */
    17  	i8042Minready = 0x20 /*  mouse character ready */
    18  	i8042Rtimeout = 0x40 /*  general timeout */
    19  	i8042Parity   = 0x80
    20  
    21  	i8042Cmd   = 0x64 /* command port (write only) */
    22  	i8042Nscan = 128
    23  )
    24  
    25  type i8042 struct {
    26  	data, status int64
    27  }
    28  
    29  func openi8042() (*i8042, error) {
    30  	if err := openPort(); err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	return &i8042{data: i8042Data, status: i8042Status}, nil
    35  }
    36  
    37  func (u *i8042) OK(bit uint8) (bool, error) {
    38  	var rdy [1]byte
    39  	if _, err := portFile.ReadAt(rdy[:], u.status); err != nil {
    40  		return false, err
    41  	}
    42  	return rdy[0]&bit != 0, nil
    43  }
    44  
    45  func (u *i8042) io(b []byte, bit uint8, f func([]byte) error) (int, error) {
    46  	var (
    47  		err error
    48  		amt int
    49  	)
    50  
    51  	for amt = 0; amt < len(b); {
    52  		rdy, err := u.OK(bit)
    53  		if err != nil {
    54  			break
    55  		}
    56  		if !rdy {
    57  			continue
    58  		}
    59  		if err = f(b[amt:]); err != nil {
    60  			break
    61  		}
    62  		amt = amt + 1
    63  	}
    64  	return amt, err
    65  }
    66  
    67  func (u *i8042) Read(b []byte) (int, error) {
    68  	return u.io(b, i8042Inready, func(b []byte) error {
    69  		_, err := portFile.ReadAt(b[:1], u.data)
    70  		return err
    71  	})
    72  }