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