aboutsummaryrefslogtreecommitdiff
path: root/ddc-ci.c
blob: 7701152904870a16e2971a7656ab93d0022a2e20 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * ddc-ci.c: DDC-CI utilities, Linux-only
 *
 * Copyright (c) 2015 - 2017, Přemysl Eric Janouch <p@janouch.name>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

// Sources: ddcciv1r1.pdf, i2c-dev.c in Linux, ddccontrol source code,
//   http://www.boichat.ch/nicolas/ddcci/specs.html was also helpful

#include <sys/ioctl.h>

#include <linux/i2c-dev.h>
#ifndef I2C_FUNC_I2C
// Fuck you, openSUSE, for fucking up the previous file, see e.g.
// https://github.com/solettaproject/soletta/commit/427f47f
#include <linux/i2c.h>
#endif

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

static void
log_message_custom (void *user_data, const char *quote, const char *fmt,
	va_list ap)
{
	(void) user_data;
	FILE *stream = stdout;

	fprintf (stream, PROGRAM_NAME ": ");
	fputs (quote, stream);
	vfprintf (stream, fmt, ap);
	fputs ("\n", stream);
}

static void
wait_ms (long ms)
{
	struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
	nanosleep (&ts, NULL);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#define DDC_LENGTH_XOR 0x80

enum
{
	DDC_ADDRESS_HOST          = 0x50,   ///< Bus master's base address
	DDC_ADDRESS_DISPLAY       = 0x6E    ///< The display's base address
};

enum { I2C_WRITE, I2C_READ };

enum
{
	DDC_GET_VCP_FEATURE       = 0x01,   ///< Request info about a feature
	DDC_GET_VCP_FEATURE_REPLY = 0x02,   ///< Feature info response
	DDC_SET_VCP_FEATURE       = 0x03    ///< Set or activate a feature
};

enum
{
	VCP_BRIGHTNESS            = 0x10,   ///< Standard VCP opcode for brightness
	VCP_CONTRAST              = 0x12,   ///< Standard VCP opcode for contrast
	VCP_INPUT_SOURCE          = 0x60    ///< Standard VCP opcode for input
};

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

static bool
check_edid (int fd, struct error **e)
{
	uint8_t edid_req[] = { 0x00 };
	uint8_t buf[8] = "";
	struct i2c_msg bufs[] =
	{
		{ .addr = 0x50, .flags = 0,
		  .len = 1, .buf = edid_req },
		{ .addr = 0x50, .flags = I2C_M_RD,
		  .len = sizeof buf, .buf = buf },
	};

	struct i2c_rdwr_ioctl_data data;
	data.msgs = bufs;
	data.nmsgs = 2;

	if (ioctl (fd, I2C_RDWR, &data) < 0)
		return error_set (e, "%s: %s", "ioctl", strerror (errno));
	if (memcmp ("\x00\xFF\xFF\xFF\xFF\xFF\xFF\x00", buf, sizeof buf))
		return error_set (e, "invalid EDID");
	return true;
}

static bool
is_a_display (int fd, struct error **e)
{
	struct stat st;
	if (fstat (fd, &st) < 0)
		return error_set (e, "%s: %s", "fstat", strerror (errno));

	unsigned long funcs;
	if (!(st.st_mode & S_IFCHR)
	 || ioctl (fd, I2C_FUNCS, &funcs) < 0
	 || !(funcs & I2C_FUNC_I2C))
		return error_set (e, "not an I2C device");

	return check_edid (fd, e);
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

static bool
ddc_send (int fd, unsigned command, void *args, size_t args_len,
	struct error **e)
{
	struct str buf = str_make ();
	str_pack_u8 (&buf, DDC_ADDRESS_HOST | I2C_READ);
	str_pack_u8 (&buf, DDC_LENGTH_XOR | (args_len + 1));
	str_pack_u8 (&buf, command);
	str_append_data (&buf, args, args_len);

	unsigned xor = DDC_ADDRESS_DISPLAY;
	for (size_t i = 0; i < buf.len; i++)
		xor ^= buf.str[i];
	str_pack_u8 (&buf, xor);

	struct i2c_msg msg =
	{
		// The driver unshifts it back
		.addr = DDC_ADDRESS_DISPLAY >> 1, .flags = 0,
		.len = buf.len, .buf = (uint8_t *) buf.str,
	};

	struct i2c_rdwr_ioctl_data data;
	data.msgs = &msg;
	data.nmsgs = 1;

	bool failed = ioctl (fd, I2C_RDWR, &data) < 0;
	str_free (&buf);
	if (failed)
		return error_set (e, "%s: %s", "ioctl", strerror (errno));
	return true;
}

static bool
ddc_read (int fd, unsigned *command, void *out_buf, size_t *n_read,
	struct error **e)
{
	uint8_t buf[128] = "";
	struct i2c_msg msg =
	{
		// The driver unshifts it back
		.addr = DDC_ADDRESS_DISPLAY >> 1, .flags = I2C_M_RD,
		.len = sizeof buf, .buf = buf,
	};

	struct i2c_rdwr_ioctl_data data;
	data.msgs = &msg;
	data.nmsgs = 1;

	if (ioctl (fd, I2C_RDWR, &data) < 0)
		return error_set (e, "%s: %s", "ioctl", strerror (errno));

	struct msg_unpacker unpacker = msg_unpacker_make (buf, sizeof buf);

	uint8_t sender, length, cmd;
	(void) msg_unpacker_u8 (&unpacker, &sender);
	(void) msg_unpacker_u8 (&unpacker, &length);
	(void) msg_unpacker_u8 (&unpacker, &cmd);

	if (sender != (DDC_ADDRESS_DISPLAY | I2C_WRITE) || !(length & 0x80))
		return error_set (e, "invalid response");
	if (!(length ^= 0x80))
		return error_set (e, "NULL response");

	// TODO: also check the checksum

	*command = cmd;
	memcpy (out_buf, unpacker.data + unpacker.offset, (*n_read = length - 1));
	return true;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

struct vcp_feature_readout
{
	uint8_t type;                       ///< Type of the value
	int16_t max;                        ///< Maximum value
	int16_t cur;                        ///< Current value
};

static bool
vcp_get_feature (int fd, uint8_t feature, struct vcp_feature_readout *out,
	struct error **e)
{
	uint8_t get_req[] = { feature };
	if (!ddc_send (fd, DDC_GET_VCP_FEATURE, get_req, sizeof get_req, e))
		return false;

	wait_ms (40);

	unsigned command = 0;
	uint8_t buf[128] = "";
	size_t len = 0;
	if (!ddc_read (fd, &command, buf, &len, e))
		return false;

	if (command != DDC_GET_VCP_FEATURE_REPLY || len != 7)
		return error_set (e, "invalid response");

	struct msg_unpacker unpacker = msg_unpacker_make (buf, len);

	uint8_t result;     msg_unpacker_u8  (&unpacker, &result);
	uint8_t vcp_opcode; msg_unpacker_u8  (&unpacker, &vcp_opcode);
	msg_unpacker_u8  (&unpacker, &out->type);
	msg_unpacker_i16 (&unpacker, &out->max);
	msg_unpacker_i16 (&unpacker, &out->cur);

	if (result == 0x01)
		return error_set (e, "error reported by monitor");

	if (result != 0x00
	 || vcp_opcode != feature)
		return error_set (e, "invalid response");

	// These are unsigned but usually just one byte long
	if (out->max < 0 || out->cur < 0)
		return error_set (e, "capability range overflow");
	return true;
}