~emersion/go-drm

go-drm/device_linux.go -rw-r--r-- 1.8 KiB
7088ebfeSimon Ser Add support for Amlogic format modifiers 12 days ago
                                                                                
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
73
74
75
76
77
78
79
80
81
82
83
84
85
package drm

import (
	"fmt"
	"os"
	"strings"
)

const drmMajor = 226

func major(dev uint64) uint32 {
	return uint32((dev & 0xfff00) >> 8)
}

func minor(dev uint64) uint32 {
	return uint32((dev & 0xff) | ((dev >> 12) & 0xfff00))
}

func getMinorType(min uint32) (NodeType, error) {
	t := NodeType(min >> 6)
	switch t {
	case NodePrimary, NodeControl, NodeRender:
		return t, nil
	default:
		return 0, fmt.Errorf("drm: unknown node type 0x%X", t)
	}
}

func getPCIPath(dev uint64) string {
	return fmt.Sprintf("/sys/dev/char/%d:%d/device", major(dev), minor(dev))
}

func getSubsystemType(dev uint64) (BusType, error) {
	subsystemPath := getPCIPath(dev) + "/subsystem"
	subsystemTarget, err := os.Readlink(subsystemPath)
	if err != nil {
		return 0, err
	}

	switch {
	case strings.HasSuffix(subsystemTarget, "/pci"):
		return BusPCI, nil
	case strings.HasSuffix(subsystemTarget, "/usb"):
		return BusUSB, nil
	case strings.HasSuffix(subsystemTarget, "/platform"):
		return BusPlatform, nil
	case strings.HasSuffix(subsystemTarget, "/spi"):
		return BusPlatform, nil
	case strings.HasSuffix(subsystemTarget, "/host1x"):
		return BusHost1x, nil
	case strings.HasSuffix(subsystemTarget, "/virtio"):
		return BusType(0x10), nil
	default:
		return 0, fmt.Errorf("drm: failed to get subsystem type from path: %v", subsystemTarget)
	}
}

func getPCIDevice(devID uint64) (*PCIDevice, error) {
	p := getPCIPath(devID)

	var dev PCIDevice
	props := []struct {
		name string
		dst  *uint32
	}{
		{"vendor", &dev.Vendor},
		{"device", &dev.Device},
		{"subsystem_vendor", &dev.SubVendor},
		{"subsystem_device", &dev.SubDevice},
	}
	for _, prop := range props {
		f, err := os.Open(p + "/" + prop.name)
		if err != nil {
			return nil, err
		}

		_, err = fmt.Fscanf(f, "0x%x", prop.dst)
		f.Close()
		if err != nil {
			return nil, err
		}
	}

	return &dev, nil
}