summaryrefslogtreecommitdiff
path: root/drivers/base/physical_location.c
blob: 87af641cfe1a3daad857eeec88ad98ad04b8df7b (plain) (blame)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: GPL-2.0
/*
 * Device physical location support
 *
 * Author: Won Chung <wonchung@google.com>
 */

#include <linux/acpi.h>
#include <linux/sysfs.h>

#include "physical_location.h"

bool dev_add_physical_location(struct device *dev)
{
	struct acpi_pld_info *pld;
	acpi_status status;

	if (!has_acpi_companion(dev))
		return false;

	status = acpi_get_physical_device_location(ACPI_HANDLE(dev), &pld);
	if (ACPI_FAILURE(status))
		return false;

	dev->physical_location =
		kzalloc(sizeof(*dev->physical_location), GFP_KERNEL);
	if (!dev->physical_location)
		return false;
	dev->physical_location->panel = pld->panel;
	dev->physical_location->vertical_position = pld->vertical_position;
	dev->physical_location->horizontal_position = pld->horizontal_position;
	dev->physical_location->dock = pld->dock;
	dev->physical_location->lid = pld->lid;

	ACPI_FREE(pld);
	return true;
}

static ssize_t panel_show(struct device *dev, struct device_attribute *attr,
	char *buf)
{
	const char *panel;

	switch (dev->physical_location->panel) {
	case DEVICE_PANEL_TOP:
		panel = "top";
		break;
	case DEVICE_PANEL_BOTTOM:
		panel = "bottom";
		break;
	case DEVICE_PANEL_LEFT:
		panel = "left";
		break;
	case DEVICE_PANEL_RIGHT:
		panel = "right";
		break;
	case DEVICE_PANEL_FRONT:
		panel = "front";
		break;
	case DEVICE_PANEL_BACK:
		panel = "back";
		break;
	default:
		panel = "unknown";
	}
	return sysfs_emit(buf, "%s\n", panel);
}
static DEVICE_ATTR_RO(panel);

static ssize_t vertical_position_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	const char *vertical_position;

	switch (dev->physical_location->vertical_position) {
	case DEVICE_VERT_POS_UPPER:
		vertical_position = "upper";
		break;
	case DEVICE_VERT_POS_CENTER:
		vertical_position = "center";
		break;
	case DEVICE_VERT_POS_LOWER:
		vertical_position = "lower";
		break;
	default:
		vertical_position = "unknown";
	}
	return sysfs_emit(buf, "%s\n", vertical_position);
}
static DEVICE_ATTR_RO(vertical_position);

static ssize_t horizontal_position_show(struct device *dev,
	struct device_attribute *attr, char *buf)
{
	const char *horizontal_position;

	switch (dev->physical_location->horizontal_position) {
	case DEVICE_HORI_POS_LEFT:
		horizontal_position = "left";
		break;
	case DEVICE_HORI_POS_CENTER:
		horizontal_position = "center";
		break;
	case DEVICE_HORI_POS_RIGHT:
		horizontal_position = "right";
		break;
	default:
		horizontal_position = "unknown";
	}
	return sysfs_emit(buf, "%s\n", horizontal_position);
}
static DEVICE_ATTR_RO(horizontal_position);

static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
	char *buf)
{
	return sysfs_emit(buf, "%s\n",
		dev->physical_location->dock ? "yes" : "no");
}
static DEVICE_ATTR_RO(dock);

static ssize_t lid_show(struct device *dev, struct device_attribute *attr,
	char *buf)
{
	return sysfs_emit(buf, "%s\n",
		dev->physical_location->lid ? "yes" : "no");
}
static DEVICE_ATTR_RO(lid);

static struct attribute *dev_attr_physical_location[] = {
	&dev_attr_panel.attr,
	&dev_attr_vertical_position.attr,
	&dev_attr_horizontal_position.attr,
	&dev_attr_dock.attr,
	&dev_attr_lid.attr,
	NULL,
};

const struct attribute_group dev_attr_physical_location_group = {
	.name = "physical_location",
	.attrs = dev_attr_physical_location,
};