File: creative_infracd.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 (297 lines) | stat: -rw-r--r-- 7,624 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
/*
 * Remote control driver for the Creative iNFRA CDrom
 *
 *  by Leonid Froenchenko <lfroen@il.marvell.com>
 *    thnx Kira Langerman for donated hardware
 *
 *  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 <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>

#include <scsi/sg.h>
#include <scsi/scsi.h>

#include "lirc_driver.h"



#define MAX_SCSI_REPLY_LEN     96
#define SCSI_INQ_CMD_LEN       6
#define SCSI_TUR_CMD_LEN       6
#define SCSI_SEN_CMD_LEN       10


static const logchannel_t logchannel = LOG_DRIVER;

static int creative_infracd_init(void);
static int creative_infracd_deinit(void);
static int creative_infracd_decode(struct ir_remote* remote, struct decode_ctx_t* ctx);
static char* creative_infracd_rec(struct ir_remote* remotes);

/* private stuff */
#define MASK_COMMAND_PRESENT 0x00f00000

static int test_device_command(int fd);


const struct driver hw_creative_infracd = {
	.name		= "creative_infracd",
	.device		= 0,
	.features	= LIRC_CAN_REC_LIRCCODE,
	.send_mode	= 0,
	.rec_mode	= LIRC_MODE_LIRCCODE,
	.code_length	= 8,
	.init_func	= creative_infracd_init,
	.deinit_func	= creative_infracd_deinit,
	.open_func	= default_open,
	.close_func	= default_close,
	.send_func	= NULL,
	.rec_func	= creative_infracd_rec,
	.decode_func	= creative_infracd_decode,
	.drvctl_func	= NULL,
	.readdata	= NULL,
	.api_version	= 3,
	.driver_version = "0.10.0",
	.info		= "No info available",
	.device_hint    = "/dev/sg*",
};

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


/*
 * opened /dev/sg<x>. I'm not using drv.fd for reasons of lirc design
 */
static int int_fd = 0;

/* last code seen from remote */
static ir_code code;

static char dev_name[32];

int is_my_device(int fd, const char* name)
{
	sg_io_hdr_t io_hdr;
	int k;
	unsigned char inqCmdBlk[SCSI_INQ_CMD_LEN] = { INQUIRY, 0, 0, 0, MAX_SCSI_REPLY_LEN, 0 };
	char Buff[MAX_SCSI_REPLY_LEN];
	unsigned char sense_buffer[32];

	/* Just to be safe, check we have a sg device wigh version > 3 */
	if ((ioctl(fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
		log_trace("%s isn't sg device version > 3", name);
		return 0;
	}
	usleep(10);
	log_trace("%s is valid sg device - checking what it is", name);

	/* Prepare INQUIRY command */
	memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
	io_hdr.interface_id = 'S';
	io_hdr.cmd_len = sizeof(inqCmdBlk);
	io_hdr.mx_sb_len = sizeof(sense_buffer);
	io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
	io_hdr.dxfer_len = MAX_SCSI_REPLY_LEN;
	io_hdr.dxferp = Buff;
	io_hdr.cmdp = inqCmdBlk;
	io_hdr.sbp = sense_buffer;
	io_hdr.timeout = 2000;

	if (ioctl(fd, SG_IO, &io_hdr) < 0) {
		log_error("INQUIRY SG_IO ioctl error");
		return 0;
	}
	usleep(10);
	if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
		log_error("INQUIRY: SCSI status=0x%x host_status=0x%x driver_status=0x%x", io_hdr.status,
			  io_hdr.host_status, io_hdr.driver_status);
		return 0;
	}
	/* check INQUIRY returned string */
	if (strncmp(Buff + 8, "CREATIVE", 8) > 0)
		log_error("%s is %s (vendor isn't Creative)", name, Buff + 8);

	/* now run sense_mode_10 for page 0 to see if this is really it */
	if (test_device_command(fd) < 0)
		return 0;
	return 1;
}

/* actually polling function */
int test_device_command(int fd)
{
	sg_io_hdr_t io_hdr;
	unsigned char senCmdBlk[SCSI_SEN_CMD_LEN] = { MODE_SENSE_10, 0, 0, 0, 0, 0, 0, 0, MAX_SCSI_REPLY_LEN, 0 };

	unsigned char sense_buffer[255];
	unsigned char Buff[MAX_SCSI_REPLY_LEN];
	unsigned int* i_Buff = (unsigned int*)Buff;

	memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
	io_hdr.interface_id = 'S';
	io_hdr.cmd_len = sizeof(senCmdBlk);
	io_hdr.mx_sb_len = sizeof(sense_buffer);
	io_hdr.dxfer_direction = SG_DXFER_TO_FROM_DEV;
	io_hdr.dxfer_len = MAX_SCSI_REPLY_LEN;
	io_hdr.dxferp = Buff;
	io_hdr.cmdp = senCmdBlk;
	io_hdr.sbp = sense_buffer;
	io_hdr.timeout = 2000;

	memset(Buff, 0, MAX_SCSI_REPLY_LEN);

	if (ioctl(fd, SG_IO, &io_hdr) < 0) {
		log_trace("MODE_SENSE_10 SG_IO ioctl error");
		return -1;
	}

	if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
		log_trace("MODE_SENSE_10: status=0x%x host=0x%x driver=0x%x", io_hdr.status,
			  io_hdr.host_status, io_hdr.driver_status);
		return -1;
	}
	if (i_Buff[2] & MASK_COMMAND_PRESENT)
		// when command present - opcode is found on bits [15:8]
		return (i_Buff[3] >> 8) & 0xff;
	// device ok, but no command
	return 0;
}

char* creative_infracd_rec(struct ir_remote* remotes)
{
	int cmd;

	while ((cmd = test_device_command(int_fd)) == 0)
		usleep(40);
	;
	if (cmd == -1)
		return 0;

	code = (reverse(cmd, 8) << 8) | (~reverse(cmd, 8) & 0xff);
	return decode_all(remotes);
}

int creative_infracd_decode(struct ir_remote* remote, struct decode_ctx_t* ctx)
{
	if (!map_code(remote, ctx, 16, 0x8435, 16, code, 0, 0))
		return 0;

	return 1;
}

int init_device(void)
{
	char c;
	int fd;

	/* user overriding autoprobing */
	if (drv.device) {
		fd = open(drv.device, O_RDWR);
		if (fd < 0) {
			log_trace("Init: open of %s failed", drv.device);
			return 0;
		}
		/* open ok, test device */
		if (is_my_device(fd, drv.device))
			return fd;
		return 0;
	}
	for (c = 'a'; c < 'z'; c++) {
		sprintf(dev_name, "/dev/sg%c", c);
		fd = open(dev_name, O_RDWR);
		if (fd < 0) {
			log_trace("Probing: open of %s failed", dev_name);
			continue;
		}
		/* open ok, test device */
		if (is_my_device(fd, dev_name)) {
			drv.device = dev_name;
			return fd;
		}
	}
	return 0;
}

int creative_infracd_init(void)
{
	int fd;

	log_trace("Creative iNFRA driver: begin search for device");

	fd = init_device();
	if (fd) {
		/*
		 * lircd making "select" for device we open. However,
		 * /dev/sg<x> does not behave like /dev/ttyS<x>, i.e. it
		 * never asserted untill we explicitly send some scsi
		 * command. So, make lircd think that device always
		 * has data, and make polling loops myself
		 */
		drv.fd = open("/dev/null", O_RDONLY);
		if (drv.fd == -1) {
			close(fd);
			return 0;
		}
		int_fd = fd;
		log_trace("Probing: %s is my device", drv.device);
		return 1;
	}

	/* probing failed - simple sanity check why */
	fd = open("/proc/scsi/scsi", O_RDONLY);
	if (fd < 0) {
		log_trace("Probing: unable to open /proc/scsi/scsi");
	} else {
		close(fd);
		fd = open("/proc/scsi/ide-scsi/0", O_RDONLY);
		if (fd < 0) {
			log_trace("Probing: scsi support present but ide-scsi is not loaded");
		} else {
			close(fd);
			log_trace(
				  "Probing: scsi in kernel, ide-scsi is loaded. Bad configuration or "
				  "device not present");
		}
	}
	return 0;
}

int creative_infracd_deinit(void)
{
	close(drv.fd);
	close(int_fd);
	return 1;
}

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-basic-offset: 8
 * End:
 */