File: i2cuser.c

package info (click to toggle)
lirc 0.10.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 8,364 kB
  • sloc: ansic: 26,717; cpp: 9,066; sh: 5,521; python: 4,362; makefile: 1,034; xml: 63
file content (308 lines) | stat: -rw-r--r-- 7,665 bytes parent folder | download | duplicates (2)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Remote control driver for I2C-attached devices from userspace
 *
 * Copyright 2006, 2007 Adam Sampson <ats@offog.org>
 *
 * This does the same job as the lirc_i2c kernel driver and hw_default, but
 * entirely in userspace -- which avoids the need to build the kernel module,
 * and should make this easier to port to other OSs in the future.
 *
 * At the moment, this only supports plain Hauppauge cards, since that's what
 * I've got. To add support for more types of devices, look at lirc_i2c.
 *
 * Based on:
 * Remote control driver for the Creative iNFRA CDrom
 *   by Leonid Froenchenko <lfroen@il.marvell.com>
 * i2c IR lirc plugin for Hauppauge and Pixelview cards
 *   Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
 * Userspace (libusb) driver for ATI/NVidia/X10 RF Remote.
 *   Copyright (C) 2004 Michael Gold <mgold@scs.carleton.ca>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
#include <signal.h>
#include <linux/i2c-dev.h>
#ifndef I2C_SLAVE               /* hack */
#include <linux/i2c.h>
#endif

#include "lirc_driver.h"

/* The number of bits and bytes in a code. */
#define CODE_SIZE_BITS 13
#define CODE_SIZE 2

/* The I2C address of the device to use. */
#define IR_ADDR 0x1a

/* The time that a key must be held down before it repeats (in seconds). */
#define REPEAT_TIME 0.3

static const logchannel_t logchannel = LOG_DRIVER;

static int i2cuser_init(void);
static int i2cuser_deinit(void);
static void i2cuser_read_loop(int fd);
static char* i2cuser_rec(struct ir_remote* remotes);

const struct driver hw_i2cuser = {
	.name		= "i2cuser",
	.device		= "auto",
	.features	= LIRC_CAN_REC_LIRCCODE,
	.send_mode	= 0,
	.rec_mode	= LIRC_MODE_LIRCCODE,
	.code_length	= CODE_SIZE_BITS,
	.init_func	= i2cuser_init,
	.deinit_func	= i2cuser_deinit,
	.open_func	= default_open,
	.close_func	= default_close,
	.send_func	= NULL,
	.rec_func	= i2cuser_rec,
	.decode_func	= receive_decode,
	.drvctl_func	= NULL,
	.readdata	= NULL,
	.api_version	= 3,
	.driver_version = "0.10.0",
	.info		= "No info available",
	.device_hint    = "/dev/i2c-*",
};


const struct driver* hardwares[] = { &hw_i2cuser, (const struct driver*)NULL };


/* FD of the i2c device. Since it's not selectable, we give the lircd core a
 * pipe and poll it ourself in a separate process. */
static int i2c_fd = -1;
/* The device name. */
char device_name[256];
/* PID of the child process. */
static pid_t child = -1;

/* Hunt for the appropriate i2c device and open it. */
static int open_i2c_device(void)
{
	const char* adapter_dir = "/sys/class/i2c-adapter";
	DIR* dir;
	int found;

	dir = opendir(adapter_dir);
	if (dir == NULL) {
		log_error("Cannot list i2c-adapter dir %s", adapter_dir);
		return -1;
	}
	found = -1;

	while (1) {
		char s[512];
		FILE* f;
		struct dirent* de;

		de = readdir(dir);
		if (de == NULL)
			break;
		if (de->d_name[0] == '.')
			continue;

		/* Kernels 2.6.22 and later had the name here: */
		snprintf(s, sizeof(s), "%s/%s/name", adapter_dir, de->d_name);

		f = fopen(s, "r");
		if (f == NULL) {
			/* ... and kernels prior to 2.6.22 have it here: */
			snprintf(s, sizeof(s), "%s/%s/device/name", adapter_dir, de->d_name);

			f = fopen(s, "r");
		}
		if (f == NULL) {
			log_error("Cannot open i2c name file %s", s);
			return -1;
		}
		memset(s, 0, sizeof(s));
		if (fread(s, 1, sizeof(s), f) != sizeof(s)) {
			if (ferror(f))
				log_warn(
					  "Error reading i2c device");
		}
		fclose(f);

		if (strncmp(s, "bt878", 5) == 0) {
			if (strncmp(de->d_name, "i2c-", 4) != 0) {
				log_error("i2c adapter dir %s has unexpected name", de->d_name);
				return -1;
			}
			found = atoi(de->d_name + 4);
			break;
		}
	}

	closedir(dir);
	if (found == -1) {
		log_error("Cannot find i2c adapter");
		return -1;
	}

	snprintf(device_name, sizeof(device_name), "/dev/i2c-%d", found);
	log_info("Using i2c device %s", device_name);
	drv.device = device_name;
	return open(device_name, O_RDWR);
}

static int i2cuser_init(void)
{
	int pipe_fd[2] = { -1, -1 };

	if (pipe(pipe_fd) != 0) {
		log_error("Couldn't open pipe: %s", strerror(errno));
		return 0;
	}
	drv.fd = pipe_fd[0];

	i2c_fd = open_i2c_device();
	if (i2c_fd == -1) {
		log_error("i2c device cannot be opened");
		goto fail;
	}

	if (ioctl(i2c_fd, I2C_SLAVE, IR_ADDR) < 0) {
		log_error("Cannot set i2c address %02x", IR_ADDR);
		goto fail;
	}

	child = fork();
	if (child == -1) {
		log_error("Cannot fork child process: %s", strerror(errno));
		goto fail;
	} else if (child == 0) {
		close(pipe_fd[0]);
		i2cuser_read_loop(pipe_fd[1]);
	}
	close(pipe_fd[1]);

	log_info("i2cuser driver: i2c device found and ready to go");
	return 1;

fail:
	if (i2c_fd != -1)
		close(i2c_fd);
	if (pipe_fd[0] != -1)
		close(pipe_fd[0]);
	if (pipe_fd[1] != -1)
		close(pipe_fd[1]);
	return 0;
}

static int i2cuser_deinit(void)
{
	if (child != -1) {
		if (kill(child, SIGTERM) == -1)
			return 0;
		if (waitpid(child, NULL, 0) == 0)
			return 0;
	}
	if (i2c_fd != -1)
		close(i2c_fd);
	if (drv.fd != -1)
		close(drv.fd);
	return 1;
}

static void i2cuser_read_loop(int out_fd)
{
	ir_code last_code = 0;
	double last_time = 0.0;

	alarm(0);
	signal(SIGTERM, SIG_DFL);
	signal(SIGPIPE, SIG_DFL);
	signal(SIGINT, SIG_DFL);
	signal(SIGHUP, SIG_IGN);
	signal(SIGALRM, SIG_IGN);

	for (;; ) {
		unsigned char buf[3], code_buf[CODE_SIZE];
		int rc, i;
		ir_code new_code;
		struct timeval tv;
		double new_time;

		do {
			/* Poll 20 times per second. */
			struct timespec ts = { 0, 50000000 };

			nanosleep(&ts, NULL);
			rc = read(i2c_fd, &buf, sizeof(buf));
			if (rc < 0 && errno != EREMOTEIO) {
				log_error("Error reading from i2c device: %s", strerror(errno));
				goto fail;
			} else if (rc != sizeof(buf)) {
				continue;
			}
		} while ((buf[0] & 0x80) == 0);

		gettimeofday(&tv, NULL);
		new_time = tv.tv_sec + (0.000001L * tv.tv_usec);

		new_code = ((buf[0] & 0x7f) << 6) | (buf[1] >> 2);
		if (new_code == last_code) {
			/* Same code as last time -- so we need to see whether
			 * it should be repeated or not. (The Hauppauge remote
			 * control will send a different code if you release
			 * and press the same key.) */
			if (new_time - last_time < REPEAT_TIME)
				continue;
		}

		log_info("Read input code: %08x", new_code);
		last_time = new_time;
		last_code = new_code;

		for (i = 0; i < CODE_SIZE; i++) {
			code_buf[(CODE_SIZE - 1) - i] = new_code & 0xFF;
			new_code >>= 8;
		}
		if (write(out_fd, code_buf, CODE_SIZE) != CODE_SIZE) {
			log_error("Write to i2cuser pipe failed: %s", strerror(errno));
			goto fail;
		}
	}

fail:
	_exit(1);
}

static char* i2cuser_rec(struct ir_remote* remotes)
{
	if (!rec_buffer_clear())
		return NULL;
	return decode_all(remotes);
}