Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 45100 | Differences between
and this patch

Collapse All | Expand All

(-)linux-2.6.5.orig/Documentation/lirc/lirc_it87 (+54 lines)
Line 0 Link Here
1
This is the README using the ITE IT8705 and IT8712 CIR port for LIRC.
2
3
The IT8705 for example can be found on the ECS K7S5A.
4
5
The driver supports receiving (MODE2) and sending (PULSE). It seems
6
sending 'LIRC_CAN_SEND_PULSE' isn't optimal for this type of hardware.
7
But because I don't know how to implement 'LIRC_CAN_SEND_CODE', I did
8
it this way.
9
10
Attention:
11
Because of missing hardware, the following hasn't been tested:
12
a) receiving with demodulator enabled,
13
b) sending (debugging output looks good) and
14
c) using IT8712
15
16
Any help and/or additions etc. is welcome.
17
18
lirc_it87 knows about the following module-parameters:
19
MODULE_DESCRIPTION("LIRC driver for ITE IT8712/IT8705 CIR port");
20
MODULE_PARM(io, "i");
21
MODULE_PARM_DESC(io, "I/O base address (default: 0x310)");
22
MODULE_PARM(irq, "i");
23
MODULE_PARM_DESC(irq, "Interrupt (1,3-12) (default: 7)");
24
MODULE_PARM(it87_enable_demodulator, "i");
25
MODULE_PARM_DESC(it87_enable_demodulator, "Receiver demodulator
26
	enable/disable (1/0), default: 0");
27
28
29
Usage:
30
31
a) io and irq:
32
33
If the driver finds the IT8705/12-CIR port initialized, io and irq of
34
the preinitialized hardware is used by the driver. If both values are
35
read 0x0 from the hardware, the default or given value is used.
36
Note: I experienced using irq=3. The driver initialized without any
37
problems, but no irqs are recognized by the system. I had to switch
38
back to default, irq 7.
39
40
b) it87_enable_demodulator:
41
42
The demodulator for the receiver can be switched off (default within
43
the driver). If you need the demodulator simple enable it by the
44
following way: it87_enable_demodulator=1.
45
46
Hans-Günter Lütke Uphues
47
48
49
TODO
50
51
This is my todo-list for lirc_it87:
52
53
1. enabling/using shared IRQ
54
2. init/drop IRQ-usage in lirc_open/lirc_close
(-)linux-2.6.5.orig/drivers/char/Kconfig (+2 lines)
Lines 632-637 Link Here
632
comment "metalab.unc.edu or ftp://titus.cfw.com/pub/Linux/util/"
632
comment "metalab.unc.edu or ftp://titus.cfw.com/pub/Linux/util/"
633
	depends on QIC02_TAPE && QIC02_DYNCONF
633
	depends on QIC02_TAPE && QIC02_DYNCONF
634
634
635
source "drivers/char/lirc/Kconfig"
636
635
source "drivers/char/ipmi/Kconfig"
637
source "drivers/char/ipmi/Kconfig"
636
638
637
source "drivers/char/watchdog/Kconfig"
639
source "drivers/char/watchdog/Kconfig"
(-)linux-2.6.5.orig/drivers/char/Makefile (-1 / +1 lines)
Lines 7-13 Link Here
7
#
7
#
8
FONTMAPFILE = cp437.uni
8
FONTMAPFILE = cp437.uni
9
9
10
obj-y	 += mem.o random.o tty_io.o n_tty.o tty_ioctl.o pty.o misc.o
10
obj-y	 += mem.o random.o tty_io.o n_tty.o tty_ioctl.o pty.o misc.o lirc/
11
11
12
obj-$(CONFIG_VT)		+= vt_ioctl.o vc_screen.o consolemap.o \
12
obj-$(CONFIG_VT)		+= vt_ioctl.o vc_screen.o consolemap.o \
13
				   consolemap_deftbl.o selection.o keyboard.o
13
				   consolemap_deftbl.o selection.o keyboard.o
(-)linux-2.6.5.orig/drivers/char/lirc/Kconfig (+208 lines)
Line 0 Link Here
1
# LIRC http://lirc.sf.net/
2
# Kernel patch by Flameeyes <dgp85@users.sf.net>
3
# Check for new patch at http://flameeyes.web.ctonet.it
4
#
5
# Thanks to Koos Vriezen <koos.vriezen@xs4all.nl> for the Homebrew support.
6
# Thanks to Jeff Clark <jeff@tmtrading.com> for support when I wasn't able
7
# to update it and for his patch (found at http://www.clarkmania.com/~jclark/
8
# Thanks to Bernhard Rosenkraenzer <bero@arklinux.org> for SMP patch.
9
# Thanks to Vince <fuzzy77@free.fr> for the temporary lirc_atiusb driver.
10
# Thanks to Paul Miller <pmiller9@users.sourceforge.net> for the new working
11
# lirc_atiusb driver.
12
13
menu "Linux InfraRed Controller"
14
15
config LIRC_SUPPORT
16
	tristate "Linux InfraRed Controller"
17
18
	config LIRC_MAX_DEV
19
		int "Maximum LIRC devices"
20
		default "2"
21
		depends on LIRC_SUPPORT
22
23
	config LIRC_I2C
24
		tristate "I2C Driver"
25
		depends on LIRC_SUPPORT && VIDEO_BT848 && I2C && I2C_ALGOBIT
26
		help
27
		  Say Y here if you need support for the following cards:
28
29
		  Pixelview IR
30
		  Hauppauage IR
31
		  PV951 IR
32
		  TV-Box IR
33
		  KNC ONE IR
34
35
		  If these dont make sense to you, then dont use the module.
36
37
	config LIRC_GPIO
38
		tristate "GPIO Driver"
39
		depends on LIRC_SUPPORT && VIDEO_BT848
40
41
	config LIRC_BT829
42
		tristate "BT829 Driver"
43
		depends on LIRC_SUPPORT
44
45
	config LIRC_IT87
46
		tristate "IT87 Driver"
47
		depends on LIRC_SUPPORT
48
49
	config LIRC_ATIUSB
50
		tristate "ATI USB Driver"
51
		depends on LIRC_SUPPORT && USB
52
53
	config LIRC_MCEUSB
54
		tristate "MCE USB Driver"
55
		depends on LIRC_SUPPORT && USB
56
57
	config LIRC_PARALLEL
58
		tristate "Parallel Driver"
59
		depends on LIRC_SUPPORT && !SMP && PARPORT
60
61
		choice
62
			prompt "Parallel Port"
63
			depends on LIRC_PARALLEL
64
			config LIRC_PARALLEL_LPT1
65
				bool "LPT1 (0x378, 7)"
66
			config LIRC_PARALLEL_LPT2
67
				bool "LPT2 (0x278, 5)"
68
			config LIRC_PARALLEL_LPT3
69
				bool "COM3 (0x3bc, none)"
70
			config LIRC_PARALLEL_OTHER
71
				bool "Other (custom values)"
72
		endchoice
73
74
		config LIRC_PORT_PARALLEL
75
			hex "I/O Port"
76
				default "0x378" if LIRC_PARALLEL_LPT1
77
				default "0x278" if LIRC_PARALLEL_LPT2
78
				default "0x3bc" if LIRC_PARALLEL_LPT3
79
			depends on LIRC_PARALLEL
80
81
		config LIRC_IRQ_PARALLEL
82
			hex "IRQ"
83
				default "7" if LIRC_PARALLEL_LPT1
84
				default "5" if LIRC_PARALLEL_LPT2
85
			depends on LIRC_PARALLEL
86
87
		config LIRC_TIMER
88
			int "Timer"
89
			default "65535"
90
			depends on LIRC_PARALLEL
91
92
	config LIRC_SERIAL
93
		tristate "Serial Driver"
94
		depends on LIRC_SUPPORT && SERIAL_8250
95
96
		choice
97
			prompt "Serial Receiver Type"
98
			depends on LIRC_SERIAL
99
100
			config LIRC_HOMEBREW
101
				bool "Homebrew"
102
103
			config LIRC_SERIAL_ANIMAX
104
				bool "Animax"
105
106
			config LIRC_SERIAL_IRDEO
107
				bool "IRdeo"
108
109
			config LIRC_SERIAL_IRDEO_REMOTE
110
				bool "IRdeo Remote"
111
112
		endchoice
113
114
		config LIRC_SERIAL_TRANSMITTER
115
			bool "With transmitter diode"
116
			depends on LIRC_SERIAL && !LIRC_SERIAL_ANIMAX
117
118
		config LIRC_SERIAL_SOFTCARRIER
119
			bool "With software carrier"
120
			depends on LIRC_SERIAL_TRANSMITTER
121
122
		config LIRC_SERIAL_IGOR
123
			bool "Igor Ceska's variation"
124
			depends on LIRC_SERIAL
125
126
		choice
127
			prompt "Serial Port"
128
			depends on LIRC_SERIAL
129
			config LIRC_SERIAL_COM1
130
				bool "COM1 (0x3f8, 4)"
131
			config LIRC_SERIAL_COM2
132
				bool "COM2 (0x2f8, 3)"
133
			config LIRC_SERIAL_COM3
134
				bool "COM3 (0x3e8, 4)"
135
			config LIRC_SERIAL_COM4
136
				bool "COM4 (0x2e8, 3)"
137
			config LIRC_SERIAL_OTHER
138
				bool "Other (custom values)"
139
		endchoice
140
141
		config LIRC_PORT_SERIAL
142
			hex "I/O Port"
143
				default "0x3f8" if LIRC_SERIAL_COM1
144
				default "0x2f8" if LIRC_SERIAL_COM2
145
				default "0x3e8" if LIRC_SERIAL_COM3
146
				default "0x2e8" if LIRC_SERIAL_COM4
147
			depends on LIRC_SERIAL
148
149
		config LIRC_IRQ_SERIAL
150
			hex "IRQ"
151
				default "4" if LIRC_SERIAL_COM1 || LIRC_SERIAL_COM3
152
				default "3" if LIRC_SERIAL_COM2 || LIRC_SERIAL_COM4
153
			depends on LIRC_SERIAL
154
155
	config LIRC_SIR
156
		tristate "SIR Driver"
157
		depends on LIRC_SUPPORT
158
159
		config LIRC_ON_SA1100
160
			bool "LIRC driver for StrongARM SA1100 embedded microprocessor"
161
			depends on LIRC_SIR
162
163
		choice
164
			prompt "SIR Type"
165
			depends on LIRC_SIR && !LIRC_ON_SA1100
166
167
			config LIRC_SIR_IRDA
168
				bool "SIR IrDA (built-in IR ports)"
169
170
			config LIRC_SIR_TEKRAM
171
				bool "Tekram Irmate 210 (16x50 UART compatible serial port)"
172
173
			config LIRC_SIR_ACTISYS_ACT200L
174
				bool "Actisys Act200L SIR driver support"
175
176
		endchoice
177
178
		choice
179
			prompt "Serial Port"
180
			depends on LIRC_SIR
181
			config LIRC_SIR_COM1
182
				bool "COM1 (0x3f8, 4)"
183
			config LIRC_SIR_COM2
184
				bool "COM2 (0x2f8, 3)"
185
			config LIRC_SIR_COM3
186
				bool "COM3 (0x3e8, 4)"
187
			config LIRC_SIR_COM4
188
				bool "COM4 (0x2e8, 3)"
189
			config LIRC_SIR_OTHER
190
				bool "Other (custom values)"
191
		endchoice
192
193
		config LIRC_PORT_SIR
194
			hex "I/O Port"
195
				default "0x3f8" if LIRC_SIR_COM1
196
				default "0x2f8" if LIRC_SIR_COM2
197
				default "0x3e8" if LIRC_SIR_COM3
198
				default "0x2e8" if LIRC_SIR_COM4
199
			depends on LIRC_SIR
200
201
		config LIRC_IRQ_SIR
202
			hex "IRQ"
203
				default "4" if LIRC_SIR_COM1 || LIRC_SIR_COM3
204
				default "3" if LIRC_SIR_COM2 || LIRC_SIR_COM4
205
			depends on LIRC_SIR
206
207
endmenu
208
(-)linux-2.6.5.orig/drivers/char/lirc/Makefile (+14 lines)
Line 0 Link Here
1
#
2
# Makefile for the lirc drivers
3
#
4
5
obj-$(CONFIG_LIRC_SUPPORT) += lirc_dev.o
6
obj-$(CONFIG_LIRC_GPIO) += lirc_gpio.o
7
obj-$(CONFIG_LIRC_BT829) += lirc_bt829.o
8
obj-$(CONFIG_LIRC_IT87) += lirc_it87.o
9
obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
10
obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
11
obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
12
obj-$(CONFIG_LIRC_ATIUSB) += lirc_atiusb.o
13
obj-$(CONFIG_LIRC_MCEUSB) += lirc_mceusb.o
14
obj-$(CONFIG_LIRC_I2C) += lirc_i2c.o
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_atiusb.c (+629 lines)
Line 0 Link Here
1
/* lirc_atiusb - USB remote support for LIRC
2
 * (currently only supports X10 USB remotes)
3
 * Version 0.3  [beta status]
4
 *
5
 * Copyright (C) 2003-2004 Paul Miller <pmiller9@users.sourceforge.net>
6
 *
7
 * This driver was derived from:
8
 *   Vladimir Dergachev <volodya@minspring.com>'s 2002
9
 *      "USB ATI Remote support" (input device)
10
 *   Adrian Dewhurst <sailor-lk@sailorfrag.net>'s 2002
11
 *      "USB StreamZap remote driver" (LIRC)
12
 *   Artur Lipowski <alipowski@kki.net.pl>'s 2002
13
 *      "lirc_dev" and "lirc_gpio" LIRC modules
14
 *
15
 * $Id: lirc_atiusb.c,v 1.21 2004/01/31 03:38:58 pmiller9 Exp $
16
 */
17
18
/*
19
 * This program is free software; you can redistribute it and/or modify
20
 * it under the terms of the GNU General Public License as published by
21
 * the Free Software Foundation; either version 2 of the License, or
22
 * (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU General Public License
30
 * along with this program; if not, write to the Free Software
31
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32
 *
33
 */
34
35
#include <linux/version.h>
36
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 4)
37
#error "*******************************************************"
38
#error "Sorry, this driver needs kernel version 2.2.4 or higher"
39
#error "*******************************************************"
40
#endif
41
42
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
43
#define KERNEL26		1
44
#else
45
#define KERNEL26		0
46
#endif
47
48
#include <linux/config.h>
49
50
#include <linux/kernel.h>
51
#include <linux/errno.h>
52
#include <linux/init.h>
53
#include <linux/slab.h>
54
#include <linux/module.h>
55
#include <linux/kmod.h>
56
#include <linux/smp_lock.h>
57
#include <linux/completion.h>
58
#include <asm/uaccess.h>
59
#include <linux/usb.h>
60
#include <linux/poll.h>
61
#include <linux/wait.h>
62
63
#if KERNEL26
64
#include <linux/lirc.h>
65
#include "lirc_dev.h"
66
#else
67
#include "drivers/lirc.h"
68
#include "drivers/lirc_dev/lirc_dev.h"
69
#endif
70
71
#define DRIVER_VERSION		"0.3"
72
#define DRIVER_AUTHOR		"Paul Miller <pmiller9@users.sourceforge.net>"
73
#define DRIVER_DESC		"USB remote driver for LIRC"
74
#define DRIVER_NAME		"lirc_atiusb"
75
76
#define CODE_LENGTH		5
77
#define CODE_MIN_LENGTH		4
78
#define USB_BUFLEN		(CODE_LENGTH*4)
79
80
#ifdef CONFIG_USB_DEBUG
81
	static int debug = 1;
82
#else
83
	static int debug = 0;
84
#endif
85
#define dprintk			if (debug) printk
86
87
/* get hi and low bytes of a 16-bits int */
88
#define HI(a)			((unsigned char)((a) >> 8))
89
#define LO(a)			((unsigned char)((a) & 0xff))
90
91
/* lock irctl structure */
92
#define IRLOCK			down_interruptible(&ir->lock)
93
#define IRUNLOCK		up(&ir->lock)
94
95
/* general constants */
96
#define SUCCESS			0
97
#define SEND_FLAG_IN_PROGRESS	1
98
#define SEND_FLAG_COMPLETE	2
99
100
101
/* data structure for each usb remote */
102
struct irctl {
103
104
	/* usb */
105
	struct usb_device *usbdev;
106
	struct urb *urb_in;
107
	struct urb *urb_out;
108
	int devnum;
109
110
	/* buffers and dma */
111
	unsigned char *buf_in;
112
	unsigned char *buf_out;
113
	unsigned int len_in;
114
#if KERNEL26
115
	dma_addr_t dma_in;
116
	dma_addr_t dma_out;
117
#endif
118
119
	/* lirc */
120
	struct lirc_plugin *p;
121
	int connected;
122
123
	/* handle sending (init strings) */
124
	int send_flags;
125
	wait_queue_head_t wait_out;
126
127
	struct semaphore lock;
128
};
129
130
/* init strings */
131
static char init1[] = {0x01, 0x00, 0x20, 0x14};
132
static char init2[] = {0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20};
133
134
/* send packet - used to initialize remote */
135
static void send_packet(struct irctl *ir, u16 cmd, unsigned char *data)
136
{
137
	DECLARE_WAITQUEUE(wait, current);
138
	int timeout = HZ; /* 1 second */
139
	unsigned char buf[USB_BUFLEN];
140
141
	dprintk(DRIVER_NAME "[%d]: send called (%#x)\n", ir->devnum, cmd);
142
143
	IRLOCK;
144
	ir->urb_out->transfer_buffer_length = LO(cmd) + 1;
145
	ir->urb_out->dev = ir->usbdev;
146
	ir->send_flags = SEND_FLAG_IN_PROGRESS;
147
148
	memcpy(buf+1, data, LO(cmd));
149
	buf[0] = HI(cmd);
150
	memcpy(ir->buf_out, buf, LO(cmd)+1);
151
152
	set_current_state(TASK_INTERRUPTIBLE);
153
	add_wait_queue(&ir->wait_out, &wait);
154
155
#if KERNEL26
156
	if (usb_submit_urb(ir->urb_out, SLAB_ATOMIC)) {
157
#else
158
	if (usb_submit_urb(ir->urb_out)) {
159
#endif
160
		set_current_state(TASK_RUNNING);
161
		remove_wait_queue(&ir->wait_out, &wait);
162
		IRUNLOCK;
163
		return;
164
	}
165
	IRUNLOCK;
166
167
	while (timeout && (ir->urb_out->status == -EINPROGRESS)
168
		&& !(ir->send_flags & SEND_FLAG_COMPLETE)) {
169
		timeout = schedule_timeout(timeout);
170
		rmb();
171
	}
172
173
	dprintk(DRIVER_NAME "[%d]: send complete (%#x)\n", ir->devnum, cmd);
174
175
	set_current_state(TASK_RUNNING);
176
	remove_wait_queue(&ir->wait_out, &wait);
177
	usb_unlink_urb(ir->urb_out);
178
}
179
180
static int unregister_from_lirc(struct irctl *ir)
181
{
182
	struct lirc_plugin *p = ir->p;
183
	int devnum;
184
	int rtn;
185
186
	devnum = ir->devnum;
187
	dprintk(DRIVER_NAME "[%d]: unregister from lirc called\n", devnum);
188
189
	if ((rtn = lirc_unregister_plugin(p->minor)) > 0) {
190
		printk(DRIVER_NAME "[%d]: error in lirc_unregister minor: %d\n"
191
			"Trying again...\n", devnum, p->minor);
192
		if (rtn == -EBUSY) {
193
			printk(DRIVER_NAME
194
				"[%d]: device is opened, will unregister"
195
				" on close\n", devnum);
196
			return -EAGAIN;
197
		}
198
		set_current_state(TASK_INTERRUPTIBLE);
199
		schedule_timeout(HZ);
200
201
		if ((rtn = lirc_unregister_plugin(p->minor)) > 0) {
202
			printk(DRIVER_NAME "[%d]: lirc_unregister failed\n",
203
			devnum);
204
		}
205
	}
206
207
	if (rtn != SUCCESS) {
208
		printk(DRIVER_NAME "[%d]: didn't free resources\n", devnum);
209
		return -EAGAIN;
210
	}
211
212
	printk(DRIVER_NAME "[%d]: usb remote disconnected\n", devnum);
213
214
	lirc_buffer_free(p->rbuf);
215
	kfree(p->rbuf);
216
	kfree(p);
217
	kfree(ir);
218
	return SUCCESS;
219
}
220
221
static int set_use_inc(void *data)
222
{
223
	struct irctl *ir = data;
224
225
	if (!ir) {
226
		printk(DRIVER_NAME "[?]: set_use_inc called with no context\n");
227
		return -EIO;
228
	}
229
	dprintk(DRIVER_NAME "[%d]: set use inc\n", ir->devnum);
230
231
	if (!ir->connected) {
232
		if (!ir->usbdev)
233
			return -ENOENT;
234
		ir->urb_in->dev = ir->usbdev;
235
#if KERNEL26
236
		if (usb_submit_urb(ir->urb_in, SLAB_ATOMIC)) {
237
#else
238
		if (usb_submit_urb(ir->urb_in)) {
239
#endif
240
			printk(DRIVER_NAME "[%d]: open result = -EIO error "
241
				"submitting urb\n", ir->devnum);
242
			return -EIO;
243
		}
244
		ir->connected = 1;
245
	}
246
247
	return SUCCESS;
248
}
249
250
static void set_use_dec(void *data)
251
{
252
	struct irctl *ir = data;
253
254
	if (!ir) {
255
		printk(DRIVER_NAME "[?]: set_use_dec called with no context\n");
256
		return;
257
	}
258
	dprintk(DRIVER_NAME "[%d]: set use dec\n", ir->devnum);
259
260
	if (ir->connected) {
261
		IRLOCK;
262
		usb_unlink_urb(ir->urb_in);
263
		ir->connected = 0;
264
		IRUNLOCK;
265
	}
266
}
267
268
269
#if KERNEL26
270
static void usb_remote_recv(struct urb *urb, struct pt_regs *regs)
271
#else
272
static void usb_remote_recv(struct urb *urb)
273
#endif
274
{
275
	struct irctl *ir;
276
	char buf[CODE_LENGTH];
277
	int i, len;
278
279
	if (!urb)
280
		return;
281
282
	if (!(ir = urb->context)) {
283
		usb_unlink_urb(urb);
284
		return;
285
	}
286
287
	dprintk(DRIVER_NAME "[%d]: data received (length %d)\n",
288
		ir->devnum, urb->actual_length);
289
290
	switch (urb->status) {
291
292
	/* success */
293
	case SUCCESS:
294
		/* some remotes emit both 4 and 5 byte length codes. */
295
		len = urb->actual_length;
296
		if (len < CODE_MIN_LENGTH || len > CODE_LENGTH) return;
297
298
		memcpy(buf,urb->transfer_buffer,len);
299
		for (i = len; i < CODE_LENGTH; i++) buf[i] = 0;
300
301
		lirc_buffer_write_1(ir->p->rbuf, buf);
302
		wake_up(&ir->p->rbuf->wait_poll);
303
		break;
304
305
	/* unlink */
306
	case -ECONNRESET:
307
	case -ENOENT:
308
	case -ESHUTDOWN:
309
		usb_unlink_urb(urb);
310
		return;
311
	}
312
313
	/* resubmit urb */
314
#if KERNEL26
315
	usb_submit_urb(urb, SLAB_ATOMIC);
316
#else
317
	usb_submit_urb(urb);
318
#endif
319
}
320
321
#if KERNEL26
322
static void usb_remote_send(struct urb *urb, struct pt_regs *regs)
323
#else
324
static void usb_remote_send(struct urb *urb)
325
#endif
326
{
327
	struct irctl *ir;
328
329
	if (!urb)
330
		return;
331
332
	if (!(ir = urb->context)) {
333
		usb_unlink_urb(urb);
334
		return;
335
	}
336
337
	dprintk(DRIVER_NAME "[%d]: usb out called\n", ir->devnum);
338
339
	if (urb->status)
340
		return;
341
342
	ir->send_flags |= SEND_FLAG_COMPLETE;
343
	wmb();
344
	if (waitqueue_active(&ir->wait_out))
345
		wake_up(&ir->wait_out);
346
}
347
348
#if KERNEL26
349
static int usb_remote_probe(struct usb_interface *intf,
350
				const struct usb_device_id *id)
351
{
352
	struct usb_device *dev = NULL;
353
	struct usb_host_interface *idesc = NULL;
354
#else
355
static void *usb_remote_probe(struct usb_device *dev, unsigned int ifnum,
356
				const struct usb_device_id *id)
357
{
358
	struct usb_interface *intf;
359
	struct usb_interface_descriptor *idesc;
360
#endif
361
	struct usb_endpoint_descriptor *ep_in, *ep_out;
362
	struct irctl *ir = NULL;
363
	struct lirc_plugin *plugin = NULL;
364
	struct lirc_buffer *rbuf = NULL;
365
	int devnum, pipe, maxp, len, buf_len, bytes_in_key;
366
	int minor = 0;
367
	char buf[63], name[128]="";
368
	int mem_failure = 0;
369
370
	dprintk(DRIVER_NAME ": usb probe called\n");
371
372
#if KERNEL26
373
	dev = interface_to_usbdev(intf);
374
	idesc = &intf->altsetting[intf->act_altsetting];
375
	if (idesc->desc.bNumEndpoints != 2)
376
		return -ENODEV;
377
	ep_in = &idesc->endpoint[0].desc;
378
	ep_out = &idesc->endpoint[1].desc;
379
	if (((ep_in->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN)
380
		|| (ep_in->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
381
		!= USB_ENDPOINT_XFER_INT)
382
		return -ENODEV;
383
#else
384
	intf = &dev->actconfig->interface[ifnum];
385
	idesc = &intf->altsetting[intf->act_altsetting];
386
	if (idesc->bNumEndpoints != 2)
387
		return NULL;
388
	ep_in = idesc->endpoint + 0;
389
	ep_out = idesc->endpoint + 1;
390
	if (((ep_in->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN)
391
		|| (ep_in->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
392
		!= USB_ENDPOINT_XFER_INT)
393
		return NULL;
394
#endif
395
	devnum = dev->devnum;
396
	pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
397
	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
398
399
	bytes_in_key = CODE_LENGTH;
400
	len = (maxp > USB_BUFLEN) ? USB_BUFLEN : maxp;
401
	buf_len = len - (len % bytes_in_key);
402
403
	dprintk(DRIVER_NAME "[%d]: bytes_in_key=%d len=%d maxp=%d buf_len=%d\n",
404
		devnum, bytes_in_key, len, maxp, buf_len);
405
406
407
	/* allocate kernel memory */
408
	mem_failure = 0;
409
	if (!(ir = kmalloc(sizeof(struct irctl), GFP_KERNEL))) {
410
		mem_failure = 1;
411
	} else {
412
		memset(ir, 0, sizeof(struct irctl));
413
414
		if (!(plugin = kmalloc(sizeof(struct lirc_plugin), GFP_KERNEL))) {
415
			mem_failure = 2;
416
		} else if (!(rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL))) {
417
			mem_failure = 3;
418
		} else if (lirc_buffer_init(rbuf, bytes_in_key, USB_BUFLEN/bytes_in_key)) {
419
			mem_failure = 4;
420
#if KERNEL26
421
		} else if (!(ir->buf_in = usb_buffer_alloc(dev, buf_len, SLAB_ATOMIC, &ir->dma_in))) {
422
			mem_failure = 5;
423
		} else if (!(ir->buf_out = usb_buffer_alloc(dev, USB_BUFLEN, SLAB_ATOMIC, &ir->dma_out))) {
424
			mem_failure = 6;
425
		} else if (!(ir->urb_in = usb_alloc_urb(0, GFP_KERNEL))) {
426
			mem_failure = 7;
427
		} else if (!(ir->urb_out = usb_alloc_urb(0, GFP_KERNEL))) {
428
			mem_failure = 8;
429
#else
430
		} else if (!(ir->buf_in = kmalloc(buf_len, GFP_KERNEL))) {
431
			mem_failure = 5;
432
		} else if (!(ir->buf_out = kmalloc(USB_BUFLEN, GFP_KERNEL))) {
433
			mem_failure = 6;
434
		} else if (!(ir->urb_in = usb_alloc_urb(0))) {
435
			mem_failure = 7;
436
		} else if (!(ir->urb_out = usb_alloc_urb(0))) {
437
			mem_failure = 8;
438
#endif
439
		} else {
440
441
			memset(plugin, 0, sizeof(struct lirc_plugin));
442
443
			strcpy(plugin->name, DRIVER_NAME " ");
444
			plugin->minor = -1;
445
			plugin->code_length = bytes_in_key*8;
446
			plugin->features = LIRC_CAN_REC_LIRCCODE;
447
			plugin->data = ir;
448
			plugin->rbuf = rbuf;
449
			plugin->set_use_inc = &set_use_inc;
450
			plugin->set_use_dec = &set_use_dec;
451
452
			init_MUTEX(&ir->lock);
453
			init_waitqueue_head(&ir->wait_out);
454
455
			if ((minor = lirc_register_plugin(plugin)) < 0) {
456
				mem_failure = 9;
457
			}
458
		}
459
	}
460
461
	/* free allocated memory incase of failure */
462
	switch (mem_failure) {
463
	case 9:
464
		lirc_buffer_free(rbuf);
465
	case 8:
466
		usb_free_urb(ir->urb_out);
467
	case 7:
468
		usb_free_urb(ir->urb_in);
469
#if KERNEL26
470
	case 6:
471
		usb_buffer_free(dev, USB_BUFLEN, ir->buf_out, ir->dma_out);
472
	case 5:
473
		usb_buffer_free(dev, buf_len, ir->buf_in, ir->dma_in);
474
#else
475
	case 6:
476
		kfree(ir->buf_out);
477
	case 5:
478
		kfree(ir->buf_in);
479
#endif
480
	case 4:
481
		kfree(rbuf);
482
	case 3:
483
		kfree(plugin);
484
	case 2:
485
		kfree(ir);
486
	case 1:
487
		printk(DRIVER_NAME "[%d]: out of memory (code=%d)\n",
488
			devnum, mem_failure);
489
#if KERNEL26
490
		return -ENOMEM;
491
#else
492
		return NULL;
493
#endif
494
	}
495
496
	plugin->minor = minor;
497
	ir->p = plugin;
498
	ir->devnum = devnum;
499
	ir->usbdev = dev;
500
	ir->len_in = buf_len;
501
	ir->connected = 0;
502
503
	usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
504
		buf_len, usb_remote_recv, ir, ep_in->bInterval);
505
	usb_fill_int_urb(ir->urb_out, dev,
506
		usb_sndintpipe(dev, ep_out->bEndpointAddress), ir->buf_out,
507
		USB_BUFLEN, usb_remote_send, ir, ep_out->bInterval);
508
509
	if (dev->descriptor.iManufacturer
510
		&& usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0)
511
		strncpy(name, buf, 128);
512
	if (dev->descriptor.iProduct
513
		&& usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
514
		snprintf(name, 128, "%s %s", name, buf);
515
	printk(DRIVER_NAME "[%d]: %s on usb%d:%d\n", devnum, name,
516
	       dev->bus->busnum, devnum);
517
518
	send_packet(ir, 0x8004, init1);
519
	send_packet(ir, 0x8007, init2);
520
521
#if KERNEL26
522
	usb_set_intfdata(intf, ir);
523
	return SUCCESS;
524
#else
525
	return ir;
526
#endif
527
}
528
529
530
#if KERNEL26
531
static void usb_remote_disconnect(struct usb_interface *intf)
532
{
533
	struct usb_device *dev = interface_to_usbdev(intf);
534
	struct irctl *ir = usb_get_intfdata(intf);
535
	usb_set_intfdata(intf, NULL);
536
#else
537
static void usb_remote_disconnect(struct usb_device *dev, void *ptr)
538
{
539
	struct irctl *ir = ptr;
540
#endif
541
542
	if (!ir || !ir->p)
543
		return;
544
545
	ir->usbdev = NULL;
546
	wake_up_all(&ir->wait_out);
547
548
	IRLOCK;
549
	usb_unlink_urb(ir->urb_in);
550
	usb_unlink_urb(ir->urb_out);
551
	usb_free_urb(ir->urb_in);
552
	usb_free_urb(ir->urb_out);
553
#if KERNEL26
554
	usb_buffer_free(dev, ir->len_in, ir->buf_in, ir->dma_in);
555
	usb_buffer_free(dev, USB_BUFLEN, ir->buf_out, ir->dma_out);
556
#else
557
	kfree(ir->buf_in);
558
	kfree(ir->buf_out);
559
#endif
560
	IRUNLOCK;
561
562
	unregister_from_lirc(ir);
563
}
564
565
static struct usb_device_id usb_remote_id_table [] = {
566
	{ USB_DEVICE(0x0bc7, 0x0002) },		/* X10 USB Firecracker Interface */
567
	{ USB_DEVICE(0x0bc7, 0x0003) },		/* X10 VGA Video Sender */
568
	{ USB_DEVICE(0x0bc7, 0x0004) },		/* ATI Wireless Remote Receiver */
569
	{ USB_DEVICE(0x0bc7, 0x0005) },		/* NVIDIA Wireless Remote Receiver */
570
	{ USB_DEVICE(0x0bc7, 0x0006) },		/* ATI Wireless Remote Receiver */
571
	{ USB_DEVICE(0x0bc7, 0x0007) },		/* X10 USB Wireless Transceiver */
572
	{ USB_DEVICE(0x0bc7, 0x0008) },		/* X10 USB Wireless Transceiver */
573
	{ USB_DEVICE(0x0bc7, 0x0009) },		/* X10 USB Wireless Transceiver */
574
	{ USB_DEVICE(0x0bc7, 0x000A) },		/* X10 USB Wireless Transceiver */
575
	{ USB_DEVICE(0x0bc7, 0x000B) },		/* X10 USB Transceiver */
576
	{ USB_DEVICE(0x0bc7, 0x000C) },		/* X10 USB Transceiver */
577
	{ USB_DEVICE(0x0bc7, 0x000D) },		/* X10 USB Transceiver */
578
	{ USB_DEVICE(0x0bc7, 0x000E) },		/* X10 USB Transceiver */
579
	{ USB_DEVICE(0x0bc7, 0x000F) },		/* X10 USB Transceiver */
580
581
	{ }					/* Terminating entry */
582
};
583
584
static struct usb_driver usb_remote_driver = {
585
	.owner =	THIS_MODULE,
586
	.name =		DRIVER_NAME,
587
	.probe =	usb_remote_probe,
588
	.disconnect =	usb_remote_disconnect,
589
	.id_table =	usb_remote_id_table
590
};
591
592
static int __init usb_remote_init(void)
593
{
594
	int i;
595
596
	printk("\n" DRIVER_NAME ": " DRIVER_DESC " v" DRIVER_VERSION "\n");
597
	printk(DRIVER_NAME ": " DRIVER_AUTHOR "\n");
598
	dprintk(DRIVER_NAME ": debug mode enabled\n");
599
600
	request_module("lirc_dev");
601
602
	if ((i = usb_register(&usb_remote_driver)) < 0) {
603
		printk(DRIVER_NAME ": usb register failed, result = %d\n", i);
604
		return -ENODEV;
605
	}
606
607
	return SUCCESS;
608
}
609
610
static void __exit usb_remote_exit(void)
611
{
612
	usb_deregister(&usb_remote_driver);
613
}
614
615
module_init(usb_remote_init);
616
module_exit(usb_remote_exit);
617
618
MODULE_AUTHOR (DRIVER_AUTHOR);
619
MODULE_DESCRIPTION (DRIVER_DESC);
620
MODULE_LICENSE ("GPL");
621
MODULE_DEVICE_TABLE (usb, usb_remote_id_table);
622
623
MODULE_PARM(debug, "i");
624
MODULE_PARM_DESC(debug, "enable driver debug mode");
625
626
#if !KERNEL26
627
EXPORT_NO_SYMBOLS;
628
#endif
629
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_bt829.c (+368 lines)
Line 0 Link Here
1
/*
2
 * Remote control driver for the TV-card based on bt829
3
 *
4
 *  by Leonid Froenchenko <lfroen@galileo.co.il>
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
*/
20
21
#include <linux/version.h>
22
#include <linux/config.h>
23
#include <linux/kernel.h>
24
#include <linux/module.h>
25
#include <linux/threads.h>
26
#include <linux/sched.h>
27
#include <linux/ioport.h>
28
#include <linux/pci.h>
29
#include <linux/delay.h>
30
#include <linux/init.h>
31
32
#include "lirc_dev.h"
33
34
int poll_main(void);
35
int atir_init_start(void);
36
37
void write_index(unsigned char index,unsigned int value);
38
unsigned int read_index(unsigned char index);
39
40
void do_i2c_start(void);
41
void do_i2c_stop(void);
42
43
void seems_wr_byte(unsigned char al);
44
unsigned char seems_rd_byte(void);
45
46
unsigned int read_index(unsigned char al);
47
void write_index(unsigned char ah,unsigned int edx);
48
49
void cycle_delay(int cycle);
50
51
void do_set_bits(unsigned char bl);
52
unsigned char do_get_bits(void);
53
54
#define DATA_PCI_OFF 0x7FFC00
55
#define WAIT_CYCLE   20
56
57
58
int atir_minor;
59
unsigned long pci_addr_phys, pci_addr_lin;
60
61
struct lirc_plugin atir_plugin;
62
63
int do_pci_probe(void)
64
{
65
	struct pci_dev *my_dev;
66
	my_dev = (struct pci_dev *)pci_find_device(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_264VT,NULL);
67
	if ( my_dev ) {
68
		printk(KERN_ERR "ATIR: Using device: %s\n",my_dev->slot_name);
69
		pci_addr_phys = 0;
70
		if ( my_dev->resource[0].flags & IORESOURCE_MEM ) {
71
			pci_addr_phys = my_dev->resource[0].start;
72
			printk(KERN_INFO "ATIR memory at 0x%08X \n",(unsigned int)pci_addr_phys);
73
		}
74
		if ( pci_addr_phys == 0 ) {
75
			printk(KERN_ERR "ATIR no memory resource ?\n");
76
			return 0;
77
		}
78
	} else {
79
		printk(KERN_ERR "ATIR: pci_prob failed\n");
80
		return 0;
81
	}
82
	return 1;
83
}
84
85
86
int atir_add_to_buf (void* data, struct lirc_buffer* buf)
87
{
88
	unsigned char key;
89
	int status;
90
	status = poll_main();
91
	key = (status >> 8) & 0xFF;
92
	if( status & 0xFF )
93
	{
94
	//    printk(KERN_INFO "ATIR reading key %02X\n",*key);
95
		lirc_buffer_write_1( buf, &key );
96
		return 0;
97
	}
98
	return -ENODATA;
99
}
100
101
int atir_set_use_inc(void* data)
102
{
103
	printk(KERN_DEBUG "ATIR driver is opened\n");
104
	return 0;
105
}
106
107
void atir_set_use_dec(void* data)
108
{
109
	printk(KERN_DEBUG "ATIR driver is closed\n");
110
}
111
112
static int __init lirc_bt829_init(void)
113
{
114
	if ( !do_pci_probe() ) {
115
		return 1;
116
	}
117
118
	if ( !atir_init_start() ) {
119
		return 1;
120
	}
121
122
	strcpy(atir_plugin.name,"ATIR");
123
	atir_plugin.minor       = -1;
124
	atir_plugin.code_length = 8;
125
	atir_plugin.sample_rate = 10;
126
	atir_plugin.data        = 0;
127
	atir_plugin.add_to_buf  = atir_add_to_buf;
128
	atir_plugin.set_use_inc = atir_set_use_inc;
129
	atir_plugin.set_use_dec = atir_set_use_dec;
130
131
	atir_minor = lirc_register_plugin(&atir_plugin);
132
	printk(KERN_DEBUG "ATIR driver is registered on minor %d\n",atir_minor);
133
134
	return 0;
135
}
136
137
138
static void __exit lirc_bt829_exit(void)
139
{
140
	lirc_unregister_plugin(atir_minor);
141
}
142
143
144
int atir_init_start(void)
145
{
146
	pci_addr_lin = (unsigned long)ioremap(pci_addr_phys + DATA_PCI_OFF,0x400);
147
	if ( pci_addr_lin == 0 ) {
148
		printk(KERN_INFO "atir: pci mem must be mapped\n");
149
		return 0;
150
	}
151
	return 1;
152
}
153
154
void cycle_delay(int cycle)
155
{
156
	udelay(WAIT_CYCLE*cycle);
157
}
158
159
160
int poll_main()
161
{
162
	unsigned char status_high, status_low;
163
164
	do_i2c_start();
165
166
	seems_wr_byte(0xAA);
167
	seems_wr_byte(0x01);
168
169
	do_i2c_start();
170
171
	seems_wr_byte(0xAB);
172
173
	status_low = seems_rd_byte();
174
	status_high = seems_rd_byte();
175
176
	do_i2c_stop();
177
178
	return (status_high << 8) | status_low;
179
}
180
181
void do_i2c_start(void)
182
{
183
	do_set_bits(3);
184
	cycle_delay(4);
185
186
	do_set_bits(1);
187
	cycle_delay(7);
188
189
	do_set_bits(0);
190
	cycle_delay(2);
191
}
192
193
void do_i2c_stop(void)
194
{
195
	unsigned char bits;
196
	bits =  do_get_bits() & 0xFD;
197
	do_set_bits(bits);
198
	cycle_delay(1);
199
200
	bits |= 1;
201
	do_set_bits(bits);
202
	cycle_delay(2);
203
204
	bits |= 2;
205
	do_set_bits(bits);
206
	bits = 3;
207
	do_set_bits(bits);
208
	cycle_delay(2);
209
}
210
211
212
void seems_wr_byte(unsigned char value)
213
{
214
	int i;
215
	unsigned char reg;
216
217
	reg = do_get_bits();
218
	for(i = 0;i < 8;i++) {
219
		if ( value & 0x80 ) {
220
			reg |= 0x02;
221
		} else {
222
			reg &= 0xFD;
223
		}
224
		do_set_bits(reg);
225
		cycle_delay(1);
226
227
		reg |= 1;
228
		do_set_bits(reg);
229
		cycle_delay(1);
230
231
		reg &= 0xFE;
232
		do_set_bits(reg);
233
		cycle_delay(1);
234
		value <<= 1;
235
	}
236
	cycle_delay(2);
237
238
	reg |= 2;
239
	do_set_bits(reg);
240
241
	reg |= 1;
242
	do_set_bits(reg);
243
244
	cycle_delay(1);
245
	do_get_bits();
246
247
	reg &= 0xFE;
248
	do_set_bits(reg);
249
	cycle_delay(3);
250
}
251
252
unsigned char seems_rd_byte(void)
253
{
254
	int i;
255
	int rd_byte;
256
	unsigned char bits_2, bits_1;
257
258
	bits_1 = do_get_bits() | 2;
259
	do_set_bits(bits_1);
260
261
	rd_byte = 0;
262
	for(i = 0;i < 8;i++) {
263
		bits_1 &= 0xFE;
264
		do_set_bits(bits_1);
265
		cycle_delay(2);
266
267
		bits_1 |= 1;
268
		do_set_bits(bits_1);
269
		cycle_delay(1);
270
271
		if ( (bits_2 = do_get_bits()) & 2 ) {
272
			rd_byte |= 1;
273
		}
274
		rd_byte <<= 1;
275
	}
276
277
	bits_1 = 0;
278
	if ( bits_2 == 0 ) {
279
		bits_1 |= 2;
280
	}
281
	do_set_bits(bits_1);
282
	cycle_delay(2);
283
284
	bits_1 |= 1;
285
	do_set_bits(bits_1);
286
	cycle_delay(3);
287
288
	bits_1 &= 0xFE;
289
	do_set_bits(bits_1);
290
	cycle_delay(2);
291
292
	rd_byte >>= 1;
293
	rd_byte &= 0xFF;
294
	return rd_byte;
295
}
296
297
void do_set_bits(unsigned char new_bits)
298
{
299
	int reg_val;
300
	reg_val = read_index(0x34);
301
	if ( new_bits & 2 ) {
302
		reg_val &= 0xFFFFFFDF;
303
		reg_val |= 1;
304
	} else {
305
		reg_val &= 0xFFFFFFFE;
306
		reg_val |= 0x20;
307
	}
308
	reg_val |= 0x10;
309
	write_index(0x34,reg_val);
310
311
	reg_val = read_index(0x31);
312
	if ( new_bits & 1 ) {
313
		reg_val |= 0x1000000;
314
	} else {
315
		reg_val &= 0xFEFFFFFF;
316
	}
317
	reg_val |= 0x8000000;
318
	write_index(0x31,reg_val);
319
}
320
321
unsigned char do_get_bits(void)
322
{
323
	unsigned char bits;
324
	int reg_val;
325
326
	reg_val = read_index(0x34);
327
	reg_val |= 0x10;
328
	reg_val &= 0xFFFFFFDF;
329
	write_index(0x34,reg_val);
330
331
	reg_val = read_index(0x34);
332
	bits = 0;
333
	if ( reg_val & 8 ) {
334
		bits |= 2;
335
	} else {
336
		bits &= 0xFD;
337
	}
338
	reg_val = read_index(0x31);
339
	if ( reg_val & 0x1000000 ) {
340
		bits |= 1;
341
	} else {
342
		bits &= 0xFE;
343
	}
344
	return bits;
345
}
346
347
unsigned int read_index(unsigned char index)
348
{
349
	unsigned int addr, value;
350
	//  addr = pci_addr_lin + DATA_PCI_OFF + ((index & 0xFF) << 2);
351
	addr = pci_addr_lin + ((index & 0xFF) << 2);
352
	value = readl(addr);
353
	return value;
354
}
355
356
void write_index(unsigned char index,unsigned int reg_val)
357
{
358
	unsigned int addr;
359
	addr = pci_addr_lin + ((index & 0xFF) << 2);
360
	writel(reg_val,addr);
361
}
362
363
MODULE_AUTHOR("Froenchenko Leonid");
364
MODULE_DESCRIPTION("IR remote driver for bt829 based TV cards");
365
MODULE_LICENSE("GPL");
366
367
module_init(lirc_bt829_init);
368
module_exit(lirc_bt829_exit);
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_dev.c (+727 lines)
Line 0 Link Here
1
/*
2
 * LIRC base driver
3
 *
4
 * (L) by Artur Lipowski <alipowski@interia.pl>
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
 *
20
 * $Id: lirc_dev.c,v 1.27 2004/01/13 13:59:48 lirc Exp $
21
 *
22
 */
23
24
#include <linux/version.h>
25
26
#include <linux/config.h>
27
#include <linux/module.h>
28
#include <linux/kernel.h>
29
#include <linux/sched.h>
30
#include <linux/ioctl.h>
31
#include <linux/fs.h>
32
#include <linux/poll.h>
33
#include <linux/smp_lock.h>
34
#include <asm/uaccess.h>
35
#include <asm/semaphore.h>
36
#include <asm/errno.h>
37
#define __KERNEL_SYSCALLS__
38
#include <linux/unistd.h>
39
#include <linux/init.h>
40
#include <linux/devfs_fs_kernel.h>
41
42
#include <linux/lirc.h>
43
44
#include "lirc_dev.h"
45
46
static int debug = 0;
47
48
MODULE_PARM(debug,"i");
49
50
#define IRCTL_DEV_NAME    "BaseRemoteCtl"
51
#define SUCCESS           0
52
#define NOPLUG            -1
53
#define dprintk           if (debug) printk
54
55
#define LOGHEAD           "lirc_dev (%s[%d]): "
56
57
struct irctl
58
{
59
	struct lirc_plugin p;
60
	int open;
61
62
	struct lirc_buffer *buf;
63
64
	int t_pid;
65
66
	struct semaphore *t_notify;
67
	struct semaphore *t_notify2;
68
	int shutdown;
69
	long jiffies_to_wait;
70
};
71
72
DECLARE_MUTEX(plugin_lock);
73
74
static struct irctl irctls[CONFIG_LIRC_MAX_DEV];
75
static struct file_operations fops;
76
77
78
/*  helper function
79
 *  initializes the irctl structure
80
 */
81
static inline void init_irctl(struct irctl *ir)
82
{
83
	memset(&ir->p, 0, sizeof(struct lirc_plugin));
84
	ir->p.minor = NOPLUG;
85
86
	ir->t_pid = -1;
87
	ir->t_notify = NULL;
88
	ir->t_notify2 = NULL;
89
	ir->shutdown = 0;
90
91
	ir->jiffies_to_wait = 0;
92
93
	ir->open = 0;
94
}
95
96
97
/*  helper function
98
 *  reads key codes from plugin and puts them into buffer
99
 *  buffer free space is checked and locking performed
100
 *  returns 0 on success
101
 */
102
103
inline static int add_to_buf(struct irctl *ir)
104
{
105
	if (lirc_buffer_full(ir->buf)) {
106
		dprintk(LOGHEAD "buffer overflow\n",
107
			ir->p.name, ir->p.minor);
108
		return -EOVERFLOW;
109
	}
110
111
    if(ir->p.add_to_buf) {
112
        int res = -ENODATA;
113
        int got_data = 0;
114
        
115
        /* Service the device as long as it is returning
116
         * data and we have space
117
         */
118
        while( !lirc_buffer_full(ir->buf) )
119
        {
120
            res = ir->p.add_to_buf( ir->p.data, ir->buf );
121
            if( res == SUCCESS )
122
                got_data++;
123
            else
124
                break;
125
        }
126
127
        if( res == -ENODEV )
128
        {
129
            ir->shutdown = 1;
130
        }
131
        return (got_data ? SUCCESS : res);
132
    }
133
134
	return SUCCESS;
135
}
136
137
/* main function of the polling thread
138
 */
139
static int lirc_thread(void *irctl)
140
{
141
	struct irctl *ir = irctl;
142
143
	daemonize("lirc_dev");
144
145
	if (ir->t_notify != NULL) {
146
		up(ir->t_notify);
147
	}
148
149
	dprintk(LOGHEAD "poll thread started\n", ir->p.name, ir->p.minor);
150
151
	do {
152
		if (ir->open) {
153
			if (ir->jiffies_to_wait) {
154
				current->state = TASK_INTERRUPTIBLE;
155
				schedule_timeout(ir->jiffies_to_wait);
156
			} else {
157
				interruptible_sleep_on(ir->p.get_queue(ir->p.data));
158
			}
159
			if (ir->shutdown || !ir->open) {
160
				break;
161
			}
162
			if (!add_to_buf(ir)) {
163
				wake_up_interruptible(&ir->buf->wait_poll);
164
			}
165
		} else {
166
			/* if device not opened so we can sleep half a second */
167
			current->state = TASK_INTERRUPTIBLE;
168
			schedule_timeout(HZ/2);
169
		}
170
	} while (!ir->shutdown);
171
172
	dprintk(LOGHEAD "poll thread ended\n", ir->p.name, ir->p.minor);
173
174
	if (ir->t_notify2 != NULL) {
175
		down(ir->t_notify2);
176
	}
177
178
	ir->t_pid = -1;
179
180
	if (ir->t_notify != NULL) {
181
		up(ir->t_notify);
182
	}
183
184
	return 0;
185
}
186
187
/*
188
 *
189
 */
190
int lirc_register_plugin(struct lirc_plugin *p)
191
{
192
	struct irctl *ir;
193
	int minor;
194
	int bytes_in_key;
195
	DECLARE_MUTEX_LOCKED(tn);
196
197
	if (!p) {
198
		printk("lirc_dev: lirc_register_plugin:"
199
		       "plugin pointer must be not NULL!\n");
200
		return -EBADRQC;
201
	}
202
203
	if (CONFIG_LIRC_MAX_DEV <= p->minor) {
204
		printk("lirc_dev: lirc_register_plugin:"
205
		       "\" minor\" must be beetween 0 and %d (%d)!\n",
206
		       CONFIG_LIRC_MAX_DEV-1, p->minor);
207
		return -EBADRQC;
208
	}
209
210
	if (1 > p->code_length || (BUFLEN*8) < p->code_length) {
211
		printk("lirc_dev: lirc_register_plugin:"
212
		       "code length in bits for minor (%d) "
213
		       "must be less than %d!\n",
214
		       p->minor, BUFLEN*8);
215
		return -EBADRQC;
216
	}
217
218
	printk("lirc_dev: lirc_register_plugin:"
219
	       "sample_rate: %d\n",p->sample_rate);
220
	if (p->sample_rate) {
221
		if (2 > p->sample_rate || 100 < p->sample_rate) {
222
			printk("lirc_dev: lirc_register_plugin:"
223
			       "sample_rate must be beetween 2 and 100!\n");
224
			return -EBADRQC;
225
		}
226
        if (!p->add_to_buf) {
227
            printk("lirc_dev: lirc_register_plugin:"
228
                   "add_to_buf cannot be NULL when sample_rate is set\n");
229
            return -EBADRQC;
230
        }
231
	} else if (!(p->fops && p->fops->read)
232
			&& !p->get_queue && !p->rbuf) {
233
		printk("lirc_dev: lirc_register_plugin:"
234
		       "fops->read, get_queue and rbuf cannot all be NULL!\n");
235
		return -EBADRQC;
236
	} else if (!p->get_queue && !p->rbuf) {
237
		if (!(p->fops && p->fops->read && p->fops->poll)
238
				|| (!p->fops->ioctl && !p->ioctl)) {
239
			printk("lirc_dev: lirc_register_plugin:"
240
			       "neither read, poll nor ioctl can be NULL!\n");
241
			return -EBADRQC;
242
		}
243
	}
244
245
	down_interruptible(&plugin_lock);
246
247
	minor = p->minor;
248
249
	if (0 > minor) {
250
		/* find first free slot for plugin */
251
		for (minor=0; minor<CONFIG_LIRC_MAX_DEV; minor++)
252
			if (irctls[minor].p.minor == NOPLUG)
253
				break;
254
		if (CONFIG_LIRC_MAX_DEV == minor) {
255
			printk("lirc_dev: lirc_register_plugin: "
256
			       "no free slots for plugins!\n");
257
			up(&plugin_lock);
258
			return -ENOMEM;
259
		}
260
	} else if (irctls[minor].p.minor != NOPLUG) {
261
		printk("lirc_dev: lirc_register_plugin:"
262
		       "minor (%d) just registerd!\n", minor);
263
		up(&plugin_lock);
264
		return -EBUSY;
265
	}
266
267
	ir = &irctls[minor];
268
269
	if (p->sample_rate) {
270
		ir->jiffies_to_wait = HZ / p->sample_rate;
271
	} else {
272
                /* it means - wait for externeal event in task queue */
273
		ir->jiffies_to_wait = 0;
274
	}
275
276
	/* some safety check 8-) */
277
	p->name[sizeof(p->name)-1] = '\0';
278
279
	bytes_in_key = p->code_length/8 + (p->code_length%8 ? 1 : 0);
280
281
	if (p->rbuf) {
282
		ir->buf = p->rbuf;
283
	} else {
284
		ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
285
		lirc_buffer_init(ir->buf, bytes_in_key, BUFLEN/bytes_in_key);
286
	}
287
288
	if (p->features==0)
289
		p->features = (p->code_length > 8) ?
290
			LIRC_CAN_REC_LIRCCODE : LIRC_CAN_REC_CODE;
291
292
	ir->p = *p;
293
	ir->p.minor = minor;
294
295
#ifdef CONFIG_DEVFS_FS
296
	devfs_mk_cdev(MKDEV(IRCTL_DEV_MAJOR, ir->p.minor), S_IFCHR | S_IRUSR | S_IWUSR, "lirc/lirc%d", ir->p.minor);
297
#endif
298
299
	if(p->sample_rate || p->get_queue) {
300
		/* try to fire up polling thread */
301
		ir->t_notify = &tn;
302
		ir->t_pid = kernel_thread(lirc_thread, (void*)ir, 0);
303
		if (ir->t_pid < 0) {
304
			up(&plugin_lock);
305
			printk("lirc_dev: lirc_register_plugin:"
306
			       "cannot run poll thread for minor = %d\n",
307
			       p->minor);
308
			return -ECHILD;
309
		}
310
		down(&tn);
311
		ir->t_notify = NULL;
312
	}
313
	up(&plugin_lock);
314
315
	try_module_get(THIS_MODULE);
316
317
	dprintk("lirc_dev: plugin %s registered at minor number = %d\n",
318
		ir->p.name, ir->p.minor);
319
320
	return minor;
321
}
322
323
/*
324
 *
325
 */
326
int lirc_unregister_plugin(int minor)
327
{
328
	struct irctl *ir;
329
	DECLARE_MUTEX_LOCKED(tn);
330
	DECLARE_MUTEX_LOCKED(tn2);
331
332
	if (minor < 0 || minor >= CONFIG_LIRC_MAX_DEV) {
333
		printk("lirc_dev: lirc_unregister_plugin:"
334
		       "\" minor\" must be beetween 0 and %d!\n",
335
		       CONFIG_LIRC_MAX_DEV-1);
336
		return -EBADRQC;
337
	}
338
339
	ir = &irctls[minor];
340
341
	down_interruptible(&plugin_lock);
342
343
	if (ir->p.minor != minor) {
344
		printk("lirc_dev: lirc_unregister_plugin:"
345
		       "minor (%d) device not registered!", minor);
346
		up(&plugin_lock);
347
		return -ENOENT;
348
	}
349
350
	if (ir->open) {
351
		printk("lirc_dev: lirc_unregister_plugin:"
352
		       "plugin %s[%d] in use!", ir->p.name, ir->p.minor);
353
		up(&plugin_lock);
354
		return -EBUSY;
355
	}
356
357
	/* end up polling thread */
358
	if (ir->t_pid >= 0) {
359
		ir->t_notify = &tn;
360
		ir->t_notify2 = &tn2;
361
		ir->shutdown = 1;
362
		{
363
			struct task_struct *p;
364
365
			p = find_task_by_pid(ir->t_pid);
366
			wake_up_process(p);
367
		}
368
		up(&tn2);
369
		down(&tn);
370
		ir->t_notify = NULL;
371
		ir->t_notify2 = NULL;
372
	}
373
374
	dprintk("lirc_dev: plugin %s unregistered from minor number = %d\n",
375
		ir->p.name, ir->p.minor);
376
377
#ifdef CONFIG_DEVFS_FS
378
	devfs_remove("lirc/lirc%d", ir->p.minor);
379
#endif
380
381
	if (ir->buf != ir->p.rbuf){
382
		lirc_buffer_free(ir->buf);
383
		kfree(ir->buf);
384
	}
385
	ir->buf = NULL;
386
	init_irctl(ir);
387
	up(&plugin_lock);
388
389
	module_put(THIS_MODULE);
390
391
	return SUCCESS;
392
}
393
394
/*
395
 *
396
 */
397
static int irctl_open(struct inode *inode, struct file *file)
398
{
399
	struct irctl *ir;
400
	int retval;
401
402
	if (MINOR(inode->i_rdev) >= CONFIG_LIRC_MAX_DEV) {
403
		dprintk("lirc_dev [%d]: open result = -ENODEV\n",
404
			MINOR(inode->i_rdev));
405
		return -ENODEV;
406
	}
407
408
	ir = &irctls[MINOR(inode->i_rdev)];
409
410
	dprintk(LOGHEAD "open called\n", ir->p.name, ir->p.minor);
411
412
	/* if the plugin has an open function use it instead */
413
	if(ir->p.fops && ir->p.fops->open)
414
		return ir->p.fops->open(inode, file);
415
416
	down_interruptible(&plugin_lock);
417
418
	if (ir->p.minor == NOPLUG) {
419
		up(&plugin_lock);
420
		dprintk(LOGHEAD "open result = -ENODEV\n",
421
			ir->p.name, ir->p.minor);
422
		return -ENODEV;
423
	}
424
425
	if (ir->open) {
426
		up(&plugin_lock);
427
		dprintk(LOGHEAD "open result = -EBUSY\n",
428
			ir->p.name, ir->p.minor);
429
		return -EBUSY;
430
	}
431
432
	/* there is no need for locking here because ir->open is 0
433
         * and lirc_thread isn't using buffer
434
	 * plugins which use irq's should allocate them on set_use_inc,
435
	 * so there should be no problem with those either.
436
         */
437
	ir->buf->head = ir->buf->tail;
438
	ir->buf->fill = 0;
439
440
	++ir->open;
441
	retval = ir->p.set_use_inc(ir->p.data);
442
443
	up(&plugin_lock);
444
445
	if (retval != SUCCESS) {
446
		--ir->open;
447
		return retval;
448
	}
449
450
	dprintk(LOGHEAD "open result = %d\n", ir->p.name, ir->p.minor, SUCCESS);
451
452
	return SUCCESS;
453
}
454
455
/*
456
 *
457
 */
458
static int irctl_close(struct inode *inode, struct file *file)
459
{
460
	struct irctl *ir = &irctls[MINOR(inode->i_rdev)];
461
462
	dprintk(LOGHEAD "close called\n", ir->p.name, ir->p.minor);
463
464
	/* if the plugin has a close function use it instead */
465
	if(ir->p.fops && ir->p.fops->release)
466
		return ir->p.fops->release(inode, file);
467
468
	down_interruptible(&plugin_lock);
469
470
	--ir->open;
471
	ir->p.set_use_dec(ir->p.data);
472
473
	up(&plugin_lock);
474
475
	return SUCCESS;
476
}
477
478
/*
479
 *
480
 */
481
static unsigned int irctl_poll(struct file *file, poll_table *wait)
482
{
483
	struct irctl *ir = &irctls[MINOR(file->f_dentry->d_inode->i_rdev)];
484
485
	dprintk(LOGHEAD "poll called\n", ir->p.name, ir->p.minor);
486
487
	/* if the plugin has a poll function use it instead */
488
	if(ir->p.fops && ir->p.fops->poll)
489
		return ir->p.fops->poll(file, wait);
490
491
	poll_wait(file, &ir->buf->wait_poll, wait);
492
493
	dprintk(LOGHEAD "poll result = %s\n",
494
		ir->p.name, ir->p.minor,
495
		lirc_buffer_empty(ir->buf) ? "0" : "POLLIN|POLLRDNORM");
496
497
	return lirc_buffer_empty(ir->buf) ? 0 : (POLLIN|POLLRDNORM);
498
}
499
500
/*
501
 *
502
 */
503
static int irctl_ioctl(struct inode *inode, struct file *file,
504
                       unsigned int cmd, unsigned long arg)
505
{
506
	unsigned long mode;
507
	int result;
508
	struct irctl *ir = &irctls[MINOR(inode->i_rdev)];
509
510
	dprintk(LOGHEAD "ioctl called (%u)\n",
511
		ir->p.name, ir->p.minor, cmd);
512
513
	/* if the plugin has a ioctl function use it instead */
514
	if(ir->p.fops && ir->p.fops->ioctl)
515
		return ir->p.fops->ioctl(inode, file, cmd, arg);
516
517
	if (ir->p.minor == NOPLUG) {
518
		dprintk(LOGHEAD "ioctl result = -ENODEV\n",
519
			ir->p.name, ir->p.minor);
520
		return -ENODEV;
521
	}
522
523
	/* Give the plugin a chance to handle the ioctl */
524
	if(ir->p.ioctl){
525
		result = ir->p.ioctl(inode, file, cmd, arg);
526
		if (result != -ENOIOCTLCMD)
527
			return result;
528
	}
529
	/* The plugin can't handle cmd */
530
	result = SUCCESS;
531
532
	switch(cmd)
533
	{
534
	case LIRC_GET_FEATURES:
535
		result = put_user(ir->p.features, (unsigned long*)arg);
536
		break;
537
	case LIRC_GET_REC_MODE:
538
		if(!(ir->p.features&LIRC_CAN_REC_MASK))
539
			return(-ENOSYS);
540
541
		result = put_user(LIRC_REC2MODE
542
				  (ir->p.features&LIRC_CAN_REC_MASK),
543
				  (unsigned long*)arg);
544
		break;
545
	case LIRC_SET_REC_MODE:
546
		if(!(ir->p.features&LIRC_CAN_REC_MASK))
547
			return(-ENOSYS);
548
549
		result = get_user(mode, (unsigned long*)arg);
550
		if(!result && !(LIRC_MODE2REC(mode) & ir->p.features)) {
551
			result = -EINVAL;
552
		}
553
		/* FIXME: We should actually set the mode somehow
554
		 * but for now, lirc_serial doesn't support mode changin
555
		 * eighter */
556
		break;
557
	case LIRC_GET_LENGTH:
558
		result = put_user((unsigned long)ir->p.code_length,
559
				  (unsigned long *)arg);
560
		break;
561
	default:
562
		result = -ENOIOCTLCMD;
563
	}
564
565
	dprintk(LOGHEAD "ioctl result = %d\n",
566
		ir->p.name, ir->p.minor, result);
567
568
	return result;
569
}
570
571
/*
572
 *
573
 */
574
static ssize_t irctl_read(struct file *file,
575
			  char *buffer,
576
			  size_t length,
577
			  loff_t *ppos)
578
{
579
	struct irctl *ir = &irctls[MINOR(file->f_dentry->d_inode->i_rdev)];
580
	unsigned char buf[ir->buf->chunk_size];
581
	int ret=0, written=0;
582
	DECLARE_WAITQUEUE(wait, current);
583
584
	dprintk(LOGHEAD "read called\n", ir->p.name, ir->p.minor);
585
586
	/* if the plugin has a specific read function use it instead */
587
	if(ir->p.fops && ir->p.fops->read)
588
		return ir->p.fops->read(file, buffer, length, ppos);
589
590
	if (length % ir->buf->chunk_size) {
591
		dprintk(LOGHEAD "read result = -EINVAL\n",
592
			ir->p.name, ir->p.minor);
593
		return -EINVAL;
594
	}
595
596
	/* we add ourselves to the task queue before buffer check
597
         * to avoid losing scan code (in case when queue is awaken somewhere
598
	 * beetwen while condition checking and scheduling)
599
	 */
600
	add_wait_queue(&ir->buf->wait_poll, &wait);
601
	current->state = TASK_INTERRUPTIBLE;
602
603
	/* while we did't provide 'length' bytes, device is opened in blocking
604
	 * mode and 'copy_to_user' is happy, wait for data.
605
	 */
606
	while (written < length && ret == 0) {
607
		if (lirc_buffer_empty(ir->buf)) {
608
			/* According to the read(2) man page, 'written' can be
609
			 * returned as less than 'length', instead of blocking
610
			 * again, returning -EWOULDBLOCK, or returning
611
			 * -ERESTARTSYS */
612
			if (written) break;
613
			if (file->f_flags & O_NONBLOCK) {
614
				dprintk(LOGHEAD "read result = -EWOULDBLOCK\n",
615
						ir->p.name, ir->p.minor);
616
				remove_wait_queue(&ir->buf->wait_poll, &wait);
617
				current->state = TASK_RUNNING;
618
				return -EWOULDBLOCK;
619
			}
620
			if (signal_pending(current)) {
621
				dprintk(LOGHEAD "read result = -ERESTARTSYS\n",
622
						ir->p.name, ir->p.minor);
623
				remove_wait_queue(&ir->buf->wait_poll, &wait);
624
				current->state = TASK_RUNNING;
625
				return -ERESTARTSYS;
626
			}
627
			schedule();
628
			current->state = TASK_INTERRUPTIBLE;
629
		} else {
630
			lirc_buffer_read_1(ir->buf, buf);
631
			ret = copy_to_user((void *)buffer+written, buf,
632
					   ir->buf->chunk_size);
633
			written += ir->buf->chunk_size;
634
		}
635
	}
636
637
	remove_wait_queue(&ir->buf->wait_poll, &wait);
638
	current->state = TASK_RUNNING;
639
640
	dprintk(LOGHEAD "read result = %s (%d)\n",
641
		ir->p.name, ir->p.minor, ret ? "-EFAULT" : "OK", ret);
642
643
	return ret ? -EFAULT : written;
644
}
645
646
static ssize_t irctl_write(struct file *file, const char *buffer,
647
			   size_t length, loff_t * ppos)
648
{
649
	struct irctl *ir = &irctls[MINOR(file->f_dentry->d_inode->i_rdev)];
650
651
	dprintk(LOGHEAD "write called\n", ir->p.name, ir->p.minor);
652
653
	/* if the plugin has a specific read function use it instead */
654
	if(ir->p.fops && ir->p.fops->write)
655
		return ir->p.fops->write(file, buffer, length, ppos);
656
657
	return -EINVAL;
658
}
659
660
661
static struct file_operations fops = {
662
	read:    irctl_read,
663
	write:   irctl_write,
664
	poll:    irctl_poll,
665
	ioctl:   irctl_ioctl,
666
	open:    irctl_open,
667
	release: irctl_close
668
};
669
670
static int __init lirc_dev_init(void)
671
{
672
	int i;
673
674
	for (i=0; i < CONFIG_LIRC_MAX_DEV; ++i) {
675
		init_irctl(&irctls[i]);
676
	}
677
678
	i = register_chrdev(IRCTL_DEV_MAJOR,
679
				   IRCTL_DEV_NAME,
680
				   &fops);
681
682
	if (i < 0) {
683
		printk ("lirc_dev: device registration failed with %d\n", i);
684
		return i;
685
	}
686
687
	printk("lirc_dev: IR Remote Control driver registered, at major %d \n",
688
	       IRCTL_DEV_MAJOR);
689
690
	return SUCCESS;
691
}
692
693
static void __exit lirc_dev_exit(void)
694
{
695
	int ret;
696
697
	ret = unregister_chrdev(IRCTL_DEV_MAJOR, IRCTL_DEV_NAME);
698
699
	if (0 > ret){
700
		printk("lirc_dev: error in module_unregister_chrdev: %d\n",
701
		       ret);
702
	} else {
703
		dprintk("lirc_dev: module successfully unloaded\n");
704
	}
705
}
706
707
/* ---------------------------------------------------------------------- */
708
709
/* For now dont try to use it as a static version !  */
710
711
MODULE_DESCRIPTION("LIRC base driver module");
712
MODULE_AUTHOR("Artur Lipowski");
713
MODULE_LICENSE("GPL");
714
715
EXPORT_SYMBOL(lirc_register_plugin);
716
EXPORT_SYMBOL(lirc_unregister_plugin);
717
718
module_init(lirc_dev_init);
719
module_exit(lirc_dev_exit);
720
721
/*
722
 * Overrides for Emacs so that we follow Linus's tabbing style.
723
 * ---------------------------------------------------------------------------
724
 * Local variables:
725
 * c-basic-offset: 4
726
 * End:
727
 */
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_dev.h (+237 lines)
Line 0 Link Here
1
/*
2
 * LIRC base driver
3
 *
4
 * (L) by Artur Lipowski <alipowski@interia.pl>
5
 *        This code is licensed under GNU GPL
6
 *
7
 * $Id: lirc_dev.h,v 1.11 2003/05/02 18:56:44 ranty Exp $
8
 *
9
 */
10
11
#ifndef _LINUX_LIRC_DEV_H
12
#define _LINUX_LIRC_DEV_H
13
14
#define BUFLEN            16
15
16
//#define LIRC_BUFF_POWER_OF_2
17
#ifdef LIRC_BUFF_POWER_OF_2
18
#define mod(n, div) ((n) & ((div) -1))
19
#else
20
#define mod(n, div) ((n) % (div))
21
#endif
22
#include <linux/slab.h>
23
#include <linux/fs.h>
24
struct lirc_buffer
25
{
26
        wait_queue_head_t wait_poll;
27
	spinlock_t lock;
28
29
	unsigned char *data;
30
	unsigned int chunk_size;
31
	unsigned int size; /* in chunks */
32
	unsigned int fill; /* in chunks */
33
	int head, tail;    /* in chunks */
34
	/* Using chunks instead of bytes pretends to simplify boundary checking
35
	 * And should allow for some performance fine tunning later */
36
};
37
static inline int lirc_buffer_init(struct lirc_buffer *buf,
38
				    unsigned int chunk_size,
39
				    unsigned int size)
40
{
41
	/* Adjusting size to the next power of 2 would allow for
42
	 * inconditional LIRC_BUFF_POWER_OF_2 optimization */
43
	init_waitqueue_head(&buf->wait_poll);
44
	spin_lock_init(&buf->lock);
45
	buf->head = buf->tail = buf->fill = 0;
46
	buf->chunk_size = chunk_size;
47
	buf->size = size;
48
	buf->data = kmalloc(size*chunk_size, GFP_KERNEL);
49
	if (buf->data == NULL)
50
		return -1;
51
	memset(buf->data, 0, size*chunk_size);
52
	return 0;
53
}
54
static inline void lirc_buffer_free(struct lirc_buffer *buf)
55
{
56
	kfree(buf->data);
57
	buf->data = NULL;
58
	buf->head = buf->tail = buf->fill = 0;
59
	buf->chunk_size = 0;
60
	buf->size = 0;
61
}
62
static inline int  lirc_buffer_full(struct lirc_buffer *buf)
63
{
64
	return (buf->fill >= buf->size);
65
}
66
static inline int  lirc_buffer_empty(struct lirc_buffer *buf)
67
{
68
	return !(buf->fill);
69
}
70
static inline int  lirc_buffer_available(struct lirc_buffer *buf)
71
{
72
    return (buf->size - buf->fill);
73
}
74
extern inline void lirc_buffer_lock(struct lirc_buffer *buf, unsigned long *flags)
75
{
76
	spin_lock_irqsave(&buf->lock, *flags);
77
}
78
extern inline void lirc_buffer_unlock(struct lirc_buffer *buf, unsigned long *flags)
79
{
80
	spin_unlock_irqrestore(&buf->lock, *flags);
81
}
82
static inline void _lirc_buffer_remove_1(struct lirc_buffer *buf)
83
{
84
	buf->head = mod(buf->head+1, buf->size);
85
	buf->fill -= 1;
86
}
87
static inline void lirc_buffer_remove_1(struct lirc_buffer *buf)
88
{
89
	unsigned long flags;
90
	lirc_buffer_lock(buf, &flags);
91
	_lirc_buffer_remove_1(buf);
92
	lirc_buffer_unlock(buf, &flags);
93
}
94
static inline void _lirc_buffer_read_1(struct lirc_buffer *buf,
95
				     unsigned char *dest)
96
{
97
	memcpy(dest, &buf->data[buf->head*buf->chunk_size], buf->chunk_size);
98
	buf->head = mod(buf->head+1, buf->size);
99
	buf->fill -= 1;
100
}
101
static inline void lirc_buffer_read_1(struct lirc_buffer *buf,
102
				      unsigned char *dest)
103
{
104
	unsigned long flags;
105
	lirc_buffer_lock(buf, &flags);
106
	_lirc_buffer_read_1(buf, dest);
107
	lirc_buffer_unlock(buf, &flags);
108
}
109
static inline void _lirc_buffer_write_1(struct lirc_buffer *buf,
110
				      unsigned char *orig)
111
{
112
	memcpy(&buf->data[buf->tail*buf->chunk_size], orig, buf->chunk_size);
113
	buf->tail = mod(buf->tail+1, buf->size);
114
	buf->fill++;
115
}
116
static inline void lirc_buffer_write_1(struct lirc_buffer *buf,
117
				       unsigned char *orig)
118
{
119
	unsigned long flags;
120
	lirc_buffer_lock(buf, &flags);
121
	_lirc_buffer_write_1(buf, orig);
122
	lirc_buffer_unlock(buf, &flags);
123
}
124
static inline void _lirc_buffer_write_n(struct lirc_buffer *buf,
125
                       unsigned char* orig, int count)
126
{
127
    memcpy(&buf->data[buf->tail*buf->chunk_size], orig, count*buf->chunk_size);
128
    buf->tail = mod(buf->tail+count, buf->size);
129
    buf->fill += count;
130
}
131
static inline void lirc_buffer_write_n(struct lirc_buffer *buf,
132
                       unsigned char* orig, int count)
133
{
134
    unsigned long flags;
135
    int space1;
136
    lirc_buffer_lock(buf,&flags);
137
    if( buf->head > buf->tail ) space1 = buf->head - buf->tail;
138
    else space1 = buf->size - buf->tail;
139
    
140
    if( count > space1 )
141
    {
142
        _lirc_buffer_write_n(buf, orig, space1);
143
        _lirc_buffer_write_n(buf, orig+(space1*buf->chunk_size), count-space1);
144
    }
145
    else
146
        _lirc_buffer_write_n(buf, orig, count);
147
    lirc_buffer_unlock(buf, &flags);
148
}
149
150
struct lirc_plugin
151
{
152
     char name[40];
153
     int minor;
154
     int code_length;
155
     int sample_rate;
156
     unsigned long features;
157
     void* data;
158
     int (*add_to_buf) (void* data, struct lirc_buffer* buf);
159
     wait_queue_head_t* (*get_queue) (void* data);
160
     struct lirc_buffer *rbuf;
161
     int (*set_use_inc) (void* data);
162
     void (*set_use_dec) (void* data);
163
     int (*ioctl) (struct inode *,struct file *,unsigned int, unsigned long);
164
     struct file_operations *fops;
165
};
166
/* name:
167
 * this string will be used for logs
168
 *
169
 * minor:
170
 * indicates minor device (/dev/lircd) number for registered plugin
171
 * if caller fills it with negative value, then the first free minor
172
 * number will be used (if available)
173
 *
174
 * code_length:
175
 * length of the remote control key code expressed in bits
176
 * if code_length > 8 then many bytes are returned through the device read
177
 * in such situation get_key should return key code values starting
178
 * from most significant byte (device read will preseve this order)
179
 * in addition if code_length > 8 then get_key will be called
180
 * several (ceil(code_length/8)) times in one pool pass (or after task queue
181
 * awake) key_no parameter denotes number of the requested byte (0 means first
182
 * byte)
183
 *
184
 * sample_rate:
185
 * sample_rate equal to 0 means that no polling will be performed and add_to_buf
186
 * will be triggered by external events (through task queue returned by
187
 * get_queue)
188
 *
189
 * data:
190
 * it may point to any plugin data and this pointer will be passed to all
191
 * callback functions
192
 *
193
 * add_to_buf:
194
 * add_to_buf will be called after specified period of the time or triggered
195
 * by the external event, this behavior depends on value of the sample_rate
196
 * this function will be called in user context. this routine should return 0
197
 * if data was added to the buffer and -ENODATA if none was available. this should
198
 * add some number of bits evenly divisible by code_length to the buffer
199
 *
200
 * get_queue:
201
 * this callback should return a pointer to the task queue which will be used
202
 * for external event waiting
203
 *
204
 * rbuf:
205
 * if not NULL, it will be used as a read buffer, you will have to write to
206
 * the buffer by other means, like irq's (see also lirc_serial.c).
207
 *
208
 * set_use_inc:
209
 * set_use_inc will be called after device is opened
210
 *
211
 * set_use_dec:
212
 * set_use_dec will be called after device is closed
213
 *
214
 * ioctl:
215
 * Some ioctl's can be directly handled by lirc_dev but will be forwared here
216
 * if not NULL and only handled if it returns -ENOIOCTLCMD (see also
217
 * lirc_serial.c).
218
 *
219
 * fops:
220
 * file_operations for drivers which don't fit the current plugin model.
221
 */
222
223
224
/* following functions can be called ONLY from user context
225
 *
226
 * returns negative value on error or minor number
227
 * of the registered device if success
228
 * contens of the structure pointed by p is copied
229
 */
230
extern int lirc_register_plugin(struct lirc_plugin *p);
231
232
/* returns negative value on error or 0 if success
233
*/
234
extern int lirc_unregister_plugin(int minor);
235
236
237
#endif
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_gpio.c (+546 lines)
Line 0 Link Here
1
/*
2
 * Remote control driver for the TV-card
3
 * key codes are obtained from GPIO port
4
 *
5
 * (L) by Artur Lipowski <alipowski@interia.pl>
6
 *     patch for the AverMedia by Santiago Garcia Mantinan <manty@i.am>
7
 *                            and Christoph Bartelmus <lirc@bartelmus.de>
8
 *     patch for the BestBuy by Miguel Angel Alvarez <maacruz@navegalia.com>
9
 *     patch for the Winfast TV2000 by Juan Toledo
10
 *     <toledo@users.sourceforge.net>
11
 *     patch for the I-O Data GV-BCTV5/PCI by Jens C. Rasmussen
12
 *     <jens.rasmussen@ieee.org>
13
 *
14
 *  This program is free software; you can redistribute it and/or modify
15
 *  it under the terms of the GNU General Public License as published by
16
 *  the Free Software Foundation; either version 2 of the License, or
17
 *  (at your option) any later version.
18
 *
19
 *  This program is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  You should have received a copy of the GNU General Public License
25
 *  along with this program; if not, write to the Free Software
26
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27
 *
28
 * $Id: lirc_gpio.c,v 1.34 2003/11/02 15:38:34 lirc Exp $
29
 *
30
 */
31
32
#include <linux/version.h>
33
34
#include <linux/module.h>
35
#include <linux/kmod.h>
36
#include <linux/sched.h>
37
#include <linux/errno.h>
38
#include <linux/init.h>
39
#include <linux/moduleparam.h>
40
41
#include "../../media/video/bttv.h"
42
#include "../../media/video/bttvp.h"
43
44
#include "lirc_dev.h"
45
46
static int debug = 0;
47
static int card = 0;
48
static int minor = -1;
49
static int bttv_id = BTTV_UNKNOWN;
50
static unsigned long gpio_mask = 0;
51
static unsigned long gpio_enable = 0;
52
static unsigned long gpio_lock_mask = 0;
53
static unsigned long gpio_xor_mask = 0;
54
static unsigned int soft_gap = 0;
55
static unsigned char sample_rate = 10;
56
57
MODULE_PARM(debug,"i");
58
MODULE_PARM(card,"i");
59
MODULE_PARM(minor,"i");
60
MODULE_PARM(gpio_mask,"l");
61
MODULE_PARM(gpio_lock_mask,"l");
62
MODULE_PARM(gpio_xor_mask,"l");
63
MODULE_PARM(soft_gap,"i");
64
MODULE_PARM(sample_rate,"b");
65
MODULE_PARM(bttv_id,"i");
66
67
#undef dprintk
68
#define dprintk  if (debug) printk
69
70
struct rcv_info {
71
	int bttv_id;
72
	int card_id;
73
	unsigned long gpio_mask;
74
	unsigned long gpio_enable;
75
	unsigned long gpio_lock_mask;
76
	unsigned long gpio_xor_mask;
77
	unsigned int soft_gap;
78
	unsigned char sample_rate;
79
	unsigned char code_length;
80
};
81
82
static struct rcv_info rcv_infos[] = {
83
	{BTTV_UNKNOWN,                0,          0,          0,         0,          0,   0,  1,  0},
84
	{BTTV_PXELVWPLTVPAK,          0, 0x00003e00,          0, 0x0010000,          0,   0, 15, 32},
85
	{BTTV_PXELVWPLTVPRO,          0, 0x00001f00,          0, 0x0008000,          0, 500, 12, 32},
86
	{BTTV_PV_BT878P_9B,           0, 0x00001f00,          0, 0x0008000,          0, 500, 12, 32},
87
	{BTTV_PV_BT878P_PLUS,         0, 0x00001f00,          0, 0x0008000,          0, 500, 12, 32},
88
	{BTTV_AVERMEDIA,              0, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32},
89
	{BTTV_AVPHONE98,     0x00011461, 0x003b8000, 0x00004000, 0x0800000, 0x00800000,   0, 10,  0}, /*mapped to Capture98*/
90
	{BTTV_AVERMEDIA98,   0x00021461, 0x003b8000, 0x00004000, 0x0800000, 0x00800000,   0, 10,  0}, /*mapped to Capture98*/
91
	{BTTV_AVPHONE98,     0x00031461, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32}, /*mapped to Phone98*/
92
	/* is this one correct? */
93
	{BTTV_AVERMEDIA98,   0x00041461, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32}, /*mapped to Phone98*/
94
	/* work-around for VDOMATE */
95
	{BTTV_AVERMEDIA98,   0x03001461, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32}, /*mapped to Phone98*/
96
	/* reported by Danijel Korzinek, AVerTV GOw/FM */
97
	{BTTV_AVERMEDIA98,   0x00000000, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32}, /*mapped to Phone98*/
98
	{BTTV_CHRONOS_VS2,            0, 0x000000f8,          0, 0x0000100,          0,   0, 20,  0},
99
	/* CPH031 and CPH033 cards (?) */
100
	/* MIRO was just a work-around */
101
	{BTTV_MIRO,                   0, 0x00001f00,          0, 0x0004000,          0,   0, 10, 32},
102
	{BTTV_DYNALINK,               0, 0x00001f00,          0, 0x0004000,          0,   0, 10, 32},
103
	{BTTV_WINVIEW_601,            0, 0x00001f00,          0, 0x0004000,          0,   0,  0, 32},
104
#ifdef BTTV_KWORLD
105
	{BTTV_KWORLD,                 0, 0x00007f00,          0, 0x0004000,          0,   0, 12, 32},
106
#endif
107
	/* just a guess */
108
	{BTTV_MAGICTVIEW061,          0, 0x0028e000,          0, 0x0020000,          0,   0, 20, 32},
109
 	{BTTV_MAGICTVIEW063,          0, 0x0028e000,          0, 0x0020000,          0,   0, 20, 32},
110
 	{BTTV_PHOEBE_TVMAS,           0, 0x0028e000,          0, 0x0020000,          0,   0, 20, 32},
111
#ifdef BTTV_BESTBUY_EASYTV2
112
        {BTTV_BESTBUY_EASYTV,         0, 0x00007F00,          0, 0x0004000,          0,   0, 10,  8},
113
        {BTTV_BESTBUY_EASYTV2,        0, 0x00007F00,          0, 0x0008000,          0,   0, 10,  8},
114
#endif
115
	/* lock_mask probably also 0x100, or maybe it is 0x0 for all others !?! */
116
	{BTTV_FLYVIDEO,               0, 0x000000f8,          0,         0,          0,   0,  0, 42},
117
 	{BTTV_FLYVIDEO_98,            0, 0x000000f8,          0, 0x0000100,          0,   0,  0, 42},
118
 	{BTTV_TYPHOON_TVIEW,          0, 0x000000f8,          0, 0x0000100,          0,   0,  0, 42},
119
#ifdef BTTV_FLYVIDEO_98FM
120
	/* smorar@alfonzo.smuts.uct.ac.za */
121
	{BTTV_FLYVIDEO_98FM,          0, 0x000000f8,          0, 0x0000100,          0,   0,  0, 42},
122
#endif
123
	/* The Leadtek WinFast TV 2000 XP card (id 0x6606107d) uses an
124
	 * extra gpio bit compared to the original TV 2000 card (id
125
	 * 0x217d6606); as the bttv-0.7.100 driver does not
126
	 * distinguish between the two cards, we enable the extra bit
127
	 * based on the card id: */
128
	{BTTV_WINFAST2000,   0x6606107d, 0x000008f8,          0, 0x0000100,          0,   0,  0, 32},
129
	/* default: */
130
	{BTTV_WINFAST2000,            0, 0x000000f8,          0, 0x0000100,          0,   0,  0, 32},
131
#ifdef BTTV_GVBCTV5PCI
132
	{BTTV_GVBCTV5PCI,             0, 0x00f0b000,          0,         0,          0,   0, 20,  8},
133
#endif
134
};
135
136
static unsigned char code_length = 0;
137
static unsigned char code_bytes = 1;
138
139
#define MAX_BYTES 8
140
141
#define SUCCESS 0
142
#define LOGHEAD "lirc_gpio (%d): "
143
144
/* how many bits GPIO value can be shifted right before processing
145
 * it is computed from the value of gpio_mask_parameter
146
 */
147
static unsigned char gpio_pre_shift = 0;
148
149
150
static inline int reverse(int data, int bits)
151
{
152
	int i;
153
	int c;
154
155
	for (c=0,i=0; i<bits; i++) {
156
		c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
157
	}
158
159
	return c;
160
}
161
162
static int build_key(unsigned long gpio_val, unsigned char codes[MAX_BYTES])
163
{
164
	unsigned long mask = gpio_mask;
165
	unsigned char shift = 0;
166
167
	dprintk(LOGHEAD "gpio_val is %lx\n",card,(unsigned long) gpio_val);
168
169
	gpio_val ^= gpio_xor_mask;
170
171
	if (gpio_lock_mask && (gpio_val & gpio_lock_mask)) {
172
		return -EBUSY;
173
	}
174
175
	switch (bttv_id)
176
	{
177
	case BTTV_AVERMEDIA98:
178
		if (bttv_write_gpio(card, gpio_enable, gpio_enable)) {
179
			dprintk(LOGHEAD "cannot write to GPIO\n", card);
180
			return -EIO;
181
		}
182
		if (bttv_read_gpio(card, &gpio_val)) {
183
			dprintk(LOGHEAD "cannot read GPIO\n", card);
184
			return -EIO;
185
		}
186
		if (bttv_write_gpio(card, gpio_enable, 0)) {
187
			dprintk(LOGHEAD "cannot write to GPIO\n", card);
188
			return -EIO;
189
		}
190
		break;
191
	default:
192
		break;
193
	}
194
195
	/* extract bits from "raw" GPIO value using gpio_mask */
196
	codes[0] = 0;
197
	gpio_val >>= gpio_pre_shift;
198
	while (mask) {
199
		if (mask & 1u) {
200
			codes[0] |= (gpio_val & 1u) << shift++;
201
		}
202
		mask >>= 1;
203
		gpio_val >>= 1;
204
	}
205
206
	dprintk(LOGHEAD "code is %lx\n",card,(unsigned long) codes[0]);
207
	switch (bttv_id)
208
	{
209
	case BTTV_AVERMEDIA:
210
		codes[2] = (codes[0]<<2)&0xff;
211
		codes[3] = (~codes[2])&0xff;
212
		codes[0] = 0x02;
213
		codes[1] = 0xFD;
214
		break;
215
	case BTTV_AVPHONE98:
216
		codes[2] = ((codes[0]&(~0x1))<<2)&0xff;
217
		codes[3] = (~codes[2])&0xff;
218
		if (codes[0]&0x1) {
219
			codes[0] = 0xc0;
220
			codes[1] = 0x3f;
221
		} else {
222
			codes[0] = 0x40;
223
			codes[1] = 0xbf;
224
		}
225
		break;
226
	case BTTV_AVERMEDIA98:
227
		break;
228
	case BTTV_FLYVIDEO:
229
	case BTTV_FLYVIDEO_98:
230
	case BTTV_TYPHOON_TVIEW:
231
#ifdef BTTV_FLYVIDEO_98FM
232
	case BTTV_FLYVIDEO_98FM:
233
#endif
234
		codes[4]=codes[0]<<3;
235
		codes[5]=((~codes[4])&0xff);
236
237
		codes[0]=0x00;
238
		codes[1]=0x1A;
239
		codes[2]=0x1F;
240
		codes[3]=0x2F;
241
		break;
242
        case BTTV_MAGICTVIEW061:
243
        case BTTV_MAGICTVIEW063:
244
	case BTTV_PHOEBE_TVMAS:
245
		codes[0] = (codes[0]&0x01)
246
			|((codes[0]&0x02)<<1)
247
			|((codes[0]&0x04)<<2)
248
			|((codes[0]&0x08)>>2)
249
			|((codes[0]&0x10)>>1);
250
		/* FALLTHROUGH */
251
	case BTTV_MIRO:
252
	case BTTV_DYNALINK:
253
	case BTTV_PXELVWPLTVPAK:
254
	case BTTV_PXELVWPLTVPRO:
255
	case BTTV_PV_BT878P_9B:
256
	case BTTV_PV_BT878P_PLUS:
257
#ifdef BTTV_KWORLD
258
	case BTTV_KWORLD:
259
#endif
260
		codes[2] = reverse(codes[0],8);
261
		codes[3] = (~codes[2])&0xff;
262
		codes[0] = 0x61;
263
		codes[1] = 0xD6;
264
		break;
265
#if 0
266
		/* derived from e-tech config file */
267
		/* 26 + 16 bits */
268
		/* won't apply it until it's confirmed with a fly98 */
269
 	case BTTV_FLYVIDEO_98:
270
	case BTTV_FLYVIDEO_98FM:
271
		codes[4]=codes[0]<<3;
272
		codes[5]=(~codes[4])&0xff;
273
274
		codes[0]=0x00;
275
		codes[1]=0x1A;
276
		codes[2]=0x1F;
277
		codes[3]=0x2F;
278
		break;
279
#endif
280
	case BTTV_WINFAST2000:
281
		/* shift extra bit */
282
		codes[0] = (codes[0]&0x1f) | ((codes[0]&0x20) << 1);
283
	case BTTV_WINVIEW_601:
284
		codes[2] = reverse(codes[0],8);
285
		codes[3] = (~codes[2])&0xff;
286
		codes[0] = 0xC0;
287
		codes[1] = 0x3F;
288
		break;
289
	default:
290
		break;
291
	}
292
293
	return SUCCESS;
294
}
295
296
/* add_to_buf - copy a code to the buffer */
297
static int add_to_buf(void* data, struct lirc_buffer* buf)
298
{
299
	static unsigned long next_time = 0;
300
	static unsigned char prev_codes[MAX_BYTES];
301
	unsigned long code = 0;
302
	unsigned char cur_codes[MAX_BYTES];
303
304
	if (bttv_read_gpio(card, &code)) {
305
		dprintk(LOGHEAD "cannot read GPIO\n", card);
306
		return -EIO;
307
	}
308
309
	if (build_key(code, cur_codes)) {
310
		return -EFAULT;
311
	}
312
313
		
314
	/* XXX this should be double checked; i think the soft_gap
315
	 * here is supposed to track repeats, which is why i save off
316
	 * the prev_codes, but i'm not certain this is correct
317
	 */
318
	if (soft_gap) {
319
		if (!memcmp(prev_codes, cur_codes, code_bytes) &&
320
			jiffies < next_time) {
321
			return -EAGAIN;
322
		}
323
		next_time = jiffies + soft_gap;
324
	}
325
	memcpy( prev_codes, cur_codes, code_bytes );
326
327
	lirc_buffer_write_1( buf, cur_codes );
328
329
	return SUCCESS;
330
}
331
332
static int set_use_inc(void* data)
333
{
334
	try_module_get(THIS_MODULE);
335
	return 0;
336
}
337
338
static void set_use_dec(void* data)
339
{
340
	module_put(THIS_MODULE);
341
}
342
343
static wait_queue_head_t* get_queue(void* data)
344
{
345
	return bttv_get_gpio_queue(card);
346
}
347
348
static struct lirc_plugin plugin = {
349
	.name		= "lirc_gpio  ",
350
	.add_to_buf	= add_to_buf,
351
	.get_queue	= get_queue,
352
	.set_use_inc	= set_use_inc,
353
	.set_use_dec	= set_use_dec,
354
};
355
356
/*
357
 *
358
 */
359
int gpio_remote_init(void)
360
{
361
	int ret;
362
	unsigned int mask;
363
364
	/* "normalize" gpio_mask
365
	 * this means shift it right until first bit is set
366
	 */
367
	while (!(gpio_mask & 1u)) {
368
		gpio_pre_shift++;
369
		gpio_mask >>= 1;
370
	}
371
372
	if (code_length) {
373
		plugin.code_length = code_length;
374
	} else {
375
		/* calculate scan code length in bits if needed */
376
		plugin.code_length = 1;
377
		mask = gpio_mask >> 1;
378
		while (mask) {
379
			if (mask & 1u) {
380
				plugin.code_length++;
381
			}
382
			mask >>= 1;
383
		}
384
	}
385
386
	code_bytes = (plugin.code_length/8) + (plugin.code_length%8 ? 1 : 0);
387
	if (MAX_BYTES < code_bytes) {
388
		printk (LOGHEAD "scan code too long (%d bytes)\n",
389
			minor, code_bytes);
390
		return -EBADRQC;
391
	}
392
393
	if (gpio_enable) {
394
		if(bttv_gpio_enable(card, gpio_enable, gpio_enable)) {
395
			printk(LOGHEAD "gpio_enable failure\n", minor);
396
			return -EIO;
397
		}
398
	}
399
400
401
	/* translate ms to jiffies */
402
	soft_gap = (soft_gap*HZ) / 1000;
403
404
	plugin.minor = minor;
405
	plugin.sample_rate = sample_rate;
406
407
	ret = lirc_register_plugin(&plugin);
408
409
	if (0 > ret) {
410
		printk (LOGHEAD "device registration failed with %d\n",
411
			minor, ret);
412
		return ret;
413
	}
414
415
	minor = ret;
416
	printk(LOGHEAD "driver registered\n", minor);
417
418
	return SUCCESS;
419
}
420
421
static int __init lirc_gpio_init(void)
422
{
423
	int type,cardid,card_type;
424
425
	if (CONFIG_LIRC_MAX_DEV < minor) {
426
		printk("lirc_gpio: parameter minor (%d) must be less than %d!\n",
427
		       minor, CONFIG_LIRC_MAX_DEV-1);
428
		return -EBADRQC;
429
	}
430
431
	request_module("bttv");
432
433
	/* if gpio_mask not zero then use module parameters
434
	 * instead of autodetecting TV card
435
	 */
436
	if (gpio_mask) {
437
		if (sample_rate!=0 && (2 > sample_rate || 50 < sample_rate)) {
438
			printk(LOGHEAD "parameter sample_rate "
439
			       "must be beetween 2 and 50!\n", minor);
440
			return -EBADRQC;
441
		}
442
443
		if (sample_rate!=0 && soft_gap &&
444
		    ((2000/sample_rate) > soft_gap || 1000 < soft_gap)) {
445
			printk(LOGHEAD "parameter soft_gap "
446
			       "must be beetween %d and 1000!\n",
447
			       minor, 2000/sample_rate);
448
			return -EBADRQC;
449
		}
450
	} else {
451
		if(bttv_get_cardinfo(card,&type,&cardid)==-1) {
452
			printk(LOGHEAD "could not get card type\n", minor);
453
		}
454
		printk(LOGHEAD "card type 0x%x, id 0x%x\n",minor,
455
		       type,cardid);
456
457
		if (type == BTTV_UNKNOWN) {
458
			printk(LOGHEAD "cannot detect TV card nr %d!\n",
459
			       minor, card);
460
			return -EBADRQC;
461
		}
462
		for (card_type = 1;
463
		     card_type < sizeof(rcv_infos)/sizeof(struct rcv_info);
464
		     card_type++) {
465
			if (rcv_infos[card_type].bttv_id == type &&
466
			    (rcv_infos[card_type].card_id == 0 ||
467
			     rcv_infos[card_type].card_id == cardid)) {
468
				bttv_id = rcv_infos[card_type].bttv_id;
469
				gpio_mask = rcv_infos[card_type].gpio_mask;
470
				gpio_enable = rcv_infos[card_type].gpio_enable;
471
				gpio_lock_mask = rcv_infos[card_type].gpio_lock_mask;
472
				gpio_xor_mask = rcv_infos[card_type].gpio_xor_mask;
473
				soft_gap = rcv_infos[card_type].soft_gap;
474
				sample_rate = rcv_infos[card_type].sample_rate;
475
				code_length = rcv_infos[card_type].code_length;
476
				break;
477
			}
478
		}
479
		if (type==BTTV_AVPHONE98 && cardid==0x00011461)	{
480
			bttv_id = BTTV_AVERMEDIA98;
481
		}
482
		if (type==BTTV_AVERMEDIA98 && cardid==0x00041461) {
483
			bttv_id = BTTV_AVPHONE98;
484
		}
485
		if (type==BTTV_AVERMEDIA98 && cardid==0x03001461) {
486
			bttv_id = BTTV_AVPHONE98;
487
		}
488
		if (type==BTTV_AVERMEDIA98 && cardid==0x00000000) {
489
			bttv_id = BTTV_AVPHONE98;
490
		}
491
		if (card_type == sizeof(rcv_infos)/sizeof(struct rcv_info)) {
492
			printk(LOGHEAD "TV card type 0x%x not supported!\n",
493
			       minor, type);
494
			return -EBADRQC;
495
		}
496
	}
497
498
	request_module("lirc_dev");
499
500
	return gpio_remote_init();
501
}
502
503
void __exit lirc_gpio_exit(void)
504
{
505
	int ret;
506
507
	ret = lirc_unregister_plugin(minor);
508
509
	if (0 > ret) {
510
		printk(LOGHEAD "error in lirc_unregister_minor: %d\n"
511
		       "Trying again...\n",
512
		       minor, ret);
513
514
		current->state = TASK_INTERRUPTIBLE;
515
		schedule_timeout(HZ);
516
517
		ret = lirc_unregister_plugin(minor);
518
519
		if (0 > ret) {
520
			printk(LOGHEAD "error in lirc_unregister_minor: %d!!!\n",
521
			       minor, ret);
522
			return;
523
		}
524
	}
525
526
	dprintk(LOGHEAD "module successfully unloaded\n", minor);
527
}
528
529
MODULE_DESCRIPTION("Driver module for remote control (data from bt848 GPIO port)");
530
MODULE_AUTHOR("Artur Lipowski");
531
MODULE_LICENSE("GPL");
532
533
#ifdef MODULE
534
	module_init(lirc_gpio_init);
535
#else
536
	late_initcall(lirc_gpio_init);
537
#endif
538
module_exit(lirc_gpio_exit);
539
540
/*
541
 * Overrides for Emacs so that we follow Linus's tabbing style.
542
 * ---------------------------------------------------------------------------
543
 * Local variables:
544
 * c-basic-offset: 8
545
 * End:
546
 */
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_i2c.c (+440 lines)
Line 0 Link Here
1
/*      $Id: lirc_i2c.c,v 1.22 2004/03/02 19:34:21 lirc Exp $      */
2
3
/*
4
 * i2c IR lirc plugin for Hauppauge and Pixelview cards - new 2.8.x i2c stack
5
 *
6
 * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
7
 * modified for PixelView (BT878P+W/FM) by
8
 *      Michal Kochanowicz <mkochano@pld.org.pl>
9
 *      Christoph Bartelmus <lirc@bartelmus.de>
10
 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
11
 *      Ulrich Mueller <ulrich.mueller42@web.de>
12
 * modified for Asus TV-Box and Creative/VisionTek BreakOut-Box by
13
 *      Stefan Jahn <stefan@lkcc.org>
14
 * modified for Linux 2.6 by
15
 * 	Jeffrey Clark <jeff@clarkmania.com>
16
 *
17
 * parts are cut&pasted from the old lirc_haup.c driver
18
 *
19
 *  This program is free software; you can redistribute it and/or modify
20
 *  it under the terms of the GNU General Public License as published by
21
 *  the Free Software Foundation; either version 2 of the License, or
22
 *  (at your option) any later version.
23
 *
24
 *  This program is distributed in the hope that it will be useful,
25
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 *  GNU General Public License for more details.
28
 *
29
 *  You should have received a copy of the GNU General Public License
30
 *  along with this program; if not, write to the Free Software
31
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
32
 *
33
 */
34
35
#include <linux/module.h>
36
#include <linux/kmod.h>
37
#include <linux/kernel.h>
38
#include <linux/sched.h>
39
#include <linux/string.h>
40
#include <linux/timer.h>
41
#include <linux/delay.h>
42
#include <linux/errno.h>
43
#include <linux/slab.h>
44
#include <linux/init.h>
45
#include <linux/moduleparam.h>
46
47
#include <linux/i2c.h>
48
#include <linux/i2c-algo-bit.h>
49
#include <asm/semaphore.h>
50
51
#include "../../media/video/bttv.h"
52
53
#include "lirc_dev.h"
54
55
static unsigned short normal_i2c[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, 0x21, 0x23, I2C_CLIENT_END };
56
static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
57
static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
58
static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
59
static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
60
static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
61
static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
62
63
static struct i2c_client_address_data addr_data = {
64
	.normal_i2c             = normal_i2c,
65
	.normal_i2c_range       = normal_i2c_range,
66
	.probe                  = probe,
67
	.probe_range            = probe_range,
68
	.ignore                 = ignore,
69
	.ignore_range           = ignore_range,
70
	.force                  = force
71
};
72
73
struct i2c_ir {
74
	struct lirc_plugin lirc;
75
	struct i2c_client client;
76
	int nextkey;
77
	unsigned char b[3];
78
	unsigned char bits;
79
	unsigned char flag;
80
};
81
82
/* ----------------------------------------------------------------------- */
83
/* insmod parameters                                                       */
84
85
static int debug   = 0;    /* debug output */
86
static int minor   = -1;   /* minor number */
87
88
MODULE_PARM(debug,"i");
89
MODULE_PARM(minor,"i");
90
91
MODULE_DESCRIPTION("Infrared receiver driver for Hauppauge and Pixelview cards (i2c stack)");
92
MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller, Stefan Jahn, Jeffrey Clark");
93
MODULE_LICENSE("GPL");
94
95
#define dprintk if (debug) printk
96
97
/* ----------------------------------------------------------------------- */
98
99
#define DRIVER_NAME "lirc_i2c"
100
#define DEVICE_NAME DRIVER_NAME
101
102
/* ----------------------------------------------------------------------- */
103
104
static inline int reverse(int data, int bits)
105
{
106
	int i;
107
	int c;
108
	
109
	for (c=0,i=0; i<bits; i++) {
110
		c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
111
	}
112
113
	return c;
114
}
115
116
static int add_to_buf_pcf8574(void* data, struct lirc_buffer* buf)
117
{
118
	struct i2c_ir *ir = data;
119
	int rc;
120
	unsigned char all, mask;
121
	unsigned char key;
122
123
	/* compute all valid bits (key code + pressed/release flag) */
124
	all = ir->bits | ir->flag;
125
126
	/* save IR writable mask bits */
127
	mask = i2c_smbus_read_byte(&ir->client) & ~all;
128
129
	/* send bit mask */
130
	rc = i2c_smbus_write_byte(&ir->client, (0xff & all) | mask);
131
132
	/* receive scan code */
133
	rc = i2c_smbus_read_byte(&ir->client);
134
135
	if (rc == -1) {
136
		/* should this return -EIO? */	
137
		dprintk(DEVICE_NAME ": %s read error\n", ir->client.name);
138
		return -ENODATA;
139
	}
140
141
	/* drop duplicate polls */
142
	if (ir->b[0] == (rc & all)) {
143
		return -ENODATA;
144
	}
145
	ir->b[0] = rc & all;
146
147
	dprintk(DRIVER_NAME ": %s key 0x%02X %s\n",
148
		ir->client.name, rc & ir->bits,
149
		(rc & ir->flag) ? "released" : "pressed");
150
151
	if (rc & ir->flag) {
152
		/* ignore released buttons */
153
		return -ENODATA;
154
	}
155
156
	/* set valid key code */
157
	key  = rc & ir->bits;
158
	lirc_buffer_write_1( buf, &key );
159
	return 0;
160
}
161
162
static int add_to_buf_haup(void* data, struct lirc_buffer* buf)
163
{
164
	struct i2c_ir *ir = data;
165
        unsigned char keybuf[3];
166
	__u16 code;
167
	unsigned char codes[2];
168
169
	/* poll IR chip */
170
	if (3 == i2c_master_recv(&ir->client,keybuf,3)) {
171
		ir->b[0] = keybuf[0];
172
		ir->b[1] = keybuf[1];
173
		ir->b[2] = keybuf[2];
174
	} else {
175
		dprintk(DRIVER_NAME ": read error\n");
176
		/* keep last successfull read buffer */
177
	}
178
179
	/* key pressed ? */
180
	if ((ir->b[0] & 0x80) == 0)
181
		return -ENODATA;
182
	
183
	dprintk(DRIVER_NAME ": key (0x%02x/0x%02x)\n",
184
	ir->b[0], ir->b[1]);
185
186
	/* look what we have */
187
	code = (((__u16)ir->b[0]&0x7f)<<6) | (ir->b[1]>>2);
188
189
	codes[0] = (code >> 8) & 0xff;
190
	codes[1] = code & 0xff;
191
192
	/* return it */
193
	lirc_buffer_write_1( buf, codes );
194
	return 0;
195
}
196
197
static int add_to_buf_pixelview(void* data, struct lirc_buffer* buf)
198
{
199
	struct i2c_ir *ir = data;
200
	unsigned char key;
201
	
202
	/* poll IR chip */
203
	if (1 != i2c_master_recv(&ir->client,&key,1)) {
204
		dprintk(DRIVER_NAME ": read error\n");
205
		return -1;
206
	}
207
208
	dprintk(KERN_DEBUG DEVICE_NAME ": key %02x\n", key);
209
210
	/* return it */
211
	lirc_buffer_write_1( buf, &key );
212
/* not sure where b came from
213
 	*key = b;
214
*/
215
	return 0;
216
}
217
218
static int add_to_buf_pv951(void* data, struct lirc_buffer* buf)
219
{
220
	struct i2c_ir *ir = data;
221
	unsigned char key;
222
	unsigned char codes[4];
223
	
224
	/* poll IR chip */
225
	if (1 != i2c_master_recv(&ir->client,&key,1)) {
226
		dprintk(DRIVER_NAME ": read error\n");
227
		return -ENODATA;
228
	}
229
	/* ignore 0xaa */
230
	if (key==0xaa)
231
		return -ENODATA;
232
	dprintk(KERN_DEBUG DEVICE_NAME ": key %02x\n", key);
233
234
	codes[3] = 0x61;
235
	codes[2] = 0xD6;
236
	codes[1] = reverse(key,8);
237
	codes[0] = (~codes[2])&0xff;
238
	
239
	lirc_buffer_write_1( buf, codes );
240
	return 0;
241
}
242
243
static int add_to_buf_knc1(void *data, struct lirc_buffer* buf)
244
{
245
	static unsigned char last_key = 0xFF;
246
	struct i2c_ir *ir = data;
247
	unsigned char key;
248
	
249
	/* poll IR chip */
250
	if (1 != i2c_master_recv(&ir->client,&key,1)) {
251
		dprintk(DRIVER_NAME ": read error\n");
252
		return -ENODATA;
253
	}
254
	
255
	/* it seems that 0xFE indicates that a button is still hold
256
	   down, while 0xFF indicates that no button is hold
257
	   down. 0xFE sequences are sometimes interrupted by 0xFF */
258
	
259
	dprintk(KERN_DEBUG DEVICE_NAME ": key %02x\n", key);
260
	
261
	if( key == 0xFF )
262
		return -ENODATA;
263
264
	if ( key == 0xFE )
265
		key = last_key;
266
267
	last_key = key;
268
	lirc_buffer_write_1( buf, &key );
269
270
	return 0;
271
}
272
273
static int set_use_inc(void* data)
274
{
275
	try_module_get(THIS_MODULE);
276
	return 0;
277
}
278
279
static void set_use_dec(void* data)
280
{
281
	module_put(THIS_MODULE);
282
}
283
284
static struct lirc_plugin lirc_template = {
285
	.name =        "lirc_i2c",
286
	.set_use_inc = set_use_inc,
287
	.set_use_dec = set_use_dec
288
};
289
290
/* ----------------------------------------------------------------------- */
291
292
static int lirc_i2c_attach(struct i2c_adapter *adap, int addr, int kind);
293
static int lirc_i2c_detach(struct i2c_client *client);
294
static int lirc_i2c_probe(struct i2c_adapter *adap);
295
296
static struct i2c_driver driver = {
297
	.owner          = THIS_MODULE,
298
        .name           = DRIVER_NAME,
299
        .id             = I2C_DRIVERID_EXP3, /* FIXME */
300
        .flags          = I2C_DF_NOTIFY,
301
        .attach_adapter = lirc_i2c_probe,
302
        .detach_client  = lirc_i2c_detach,
303
};
304
305
static struct i2c_client client_template = 
306
{
307
        I2C_DEVNAME("(unset)"),
308
	.flags  = I2C_CLIENT_ALLOW_USE,
309
        .driver = &driver
310
};
311
312
static int lirc_i2c_attach(struct i2c_adapter *adap, int addr, int kind)
313
{
314
        struct i2c_ir *ir;
315
	int ret;
316
	
317
        client_template.adapter = adap;
318
        client_template.addr = addr;
319
	
320
	if (NULL == (ir = kmalloc(sizeof(struct i2c_ir),GFP_KERNEL)))
321
                return -ENOMEM;
322
	memset(ir,0,sizeof(struct i2c_ir));
323
	memcpy(&ir->client,&client_template,sizeof(struct i2c_client));
324
        memcpy(&ir->lirc,&lirc_template,sizeof(struct lirc_plugin));
325
	
326
	ir->lirc.data		= ir;
327
	ir->lirc.minor		= minor;
328
	ir->nextkey		= -1;
329
330
	i2c_set_clientdata(&ir->client,ir);
331
332
	switch(addr)
333
	{
334
	case 0x64:
335
		strncpy(ir->client.name, "Pixelview IR", I2C_NAME_SIZE);
336
		ir->lirc.code_length = 8;
337
		ir->lirc.sample_rate = 10;
338
		ir->lirc.add_to_buf = add_to_buf_pixelview;
339
		break;
340
	case 0x4b:
341
		strncpy(ir->client.name,"PV951 IR", I2C_NAME_SIZE);
342
		ir->lirc.code_length = 32;
343
		ir->lirc.sample_rate = 10;
344
		ir->lirc.add_to_buf = add_to_buf_pv951;
345
		break;
346
	case 0x18:
347
	case 0x1a:
348
		strncpy(ir->client.name,"Hauppauge IR", I2C_NAME_SIZE);
349
		ir->lirc.code_length = 13;
350
		ir->lirc.sample_rate = 6;
351
		ir->lirc.add_to_buf = add_to_buf_haup;
352
		break;
353
	case 0x30:
354
		strncpy(ir->client.name,"KNC ONE IR", I2C_NAME_SIZE);
355
		ir->lirc.code_length = 8;
356
		ir->lirc.sample_rate = 10;
357
		ir->lirc.add_to_buf = add_to_buf_knc1;
358
		break;
359
	case 0x21:
360
	case 0x23:
361
		strncpy(ir->client.name,"TV-Box IR", I2C_NAME_SIZE);
362
		ir->lirc.code_length = 8;
363
		ir->lirc.sample_rate = 10;
364
		ir->lirc.add_to_buf = add_to_buf_pcf8574;
365
		ir->bits = ir->client.flags & 0xff;
366
		ir->flag = (ir->client.flags >> 8) & 0xff;
367
		break;
368
		
369
	default:
370
		/* shouldn't happen */
371
		dprintk(DRIVER_NAME ": unknown i2c address (0x%02x)?\n",addr);
372
		kfree(ir);
373
		return -1;
374
	}
375
	dprintk(DRIVER_NAME ": chip found @ 0x%02x (%s)\n",addr,
376
			ir->client.name);
377
	
378
	/* register device */
379
	i2c_attach_client(&ir->client);
380
	
381
	if((ret = lirc_register_plugin(&ir->lirc))) {
382
		dprintk(DRIVER_NAME ": device registration failed with %d\n",
383
				ret);
384
		kfree(ir);
385
		return -1;
386
	}
387
388
	ir->lirc.minor = ret;
389
	dprintk(DRIVER_NAME ": driver registered\n");
390
391
	return 0;
392
}
393
394
static int lirc_i2c_detach(struct i2c_client *client)
395
{
396
        struct i2c_ir *ir = i2c_get_clientdata(client);
397
	int err;
398
	
399
	/* unregister device */
400
	if ((err = lirc_unregister_plugin(ir->lirc.minor))) {
401
		dprintk(DRIVER_NAME ": lirc unregister failed\n");
402
		return err;
403
	} else {
404
		dprintk(DRIVER_NAME ": lirc unregister successful\n");
405
	}
406
407
	if ((err = i2c_detach_client(&ir->client))) {
408
		dprintk(DRIVER_NAME ": i2c detach failed\n");
409
		return err;
410
	} else {
411
		dprintk(DRIVER_NAME ": i2c detach successful\n");
412
	}
413
414
	/* free memory */
415
	kfree(ir);
416
	return 0;
417
}
418
419
static int lirc_i2c_probe(struct i2c_adapter *adap) {
420
	dprintk(DRIVER_NAME ": starting probe for adapter %s (0x%x)\n",
421
			adap->name, adap->id);
422
	return i2c_probe(adap, &addr_data, lirc_i2c_attach);
423
}
424
425
static int __init lirc_i2c_init(void)
426
{
427
	dprintk(DRIVER_NAME ": init\n");
428
	request_module("bttv");
429
	request_module("lirc_dev");
430
	return i2c_add_driver(&driver);
431
}
432
433
static void __exit lirc_i2c_exit(void)
434
{
435
	dprintk(DRIVER_NAME ": exit\n");
436
	i2c_del_driver(&driver);
437
}
438
439
module_init(lirc_i2c_init);
440
module_exit(lirc_i2c_exit);
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_it87.c (+957 lines)
Line 0 Link Here
1
/*
2
 * LIRC driver for ITE IT8712/IT8705 CIR port
3
 *
4
 * Copyright (C) 2001 Hans-Günter Lütke Uphues <hg_lu@web.de>
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License as
8
 * published by the Free Software Foundation; either version 2 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * General Public License for more details.
15
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19
 * USA
20
 *
21
 * ITE IT8705 and IT8712(not tested) CIR-port support for lirc based
22
 * via cut and paste from lirc_sir.c (C) 2000 Milan Pikula
23
 *
24
 * Attention: Sendmode only tested with debugging logs
25
 *
26
 * 2001/02/27 Christoph Bartelmus <lirc@bartelmus.de> :
27
 *   reimplemented read function
28
 */
29
30
31
#include <linux/version.h>
32
#include <linux/module.h>
33
34
#include <linux/config.h>
35
36
37
#include <linux/sched.h>
38
#include <linux/errno.h>
39
#include <linux/signal.h>
40
#include <linux/fs.h>
41
#include <linux/interrupt.h>
42
#include <linux/ioport.h>
43
#include <linux/kernel.h>
44
#include <linux/major.h>
45
#include <linux/serial_reg.h>
46
#include <linux/time.h>
47
#include <linux/string.h>
48
#include <linux/types.h>
49
#include <linux/wait.h>
50
#include <linux/mm.h>
51
#include <linux/delay.h>
52
#include <linux/poll.h>
53
#include <asm/system.h>
54
#include <asm/segment.h>
55
#include <asm/io.h>
56
#include <asm/irq.h>
57
#include <asm/fcntl.h>
58
59
#include <linux/timer.h>
60
61
#include <linux/lirc.h>
62
#include "lirc_dev.h"
63
64
#include "lirc_it87.h"
65
66
static unsigned long it87_bits_in_byte_out = 0;
67
static unsigned long it87_send_counter = 0;
68
static unsigned char it87_RXEN_mask = IT87_CIR_RCR_RXEN;
69
70
#define RBUF_LEN 1024
71
#define WBUF_LEN 1024
72
73
#define LIRC_DRIVER_NAME "lirc_it87"
74
75
/* timeout for sequences in jiffies (=5/100s) */
76
/* must be longer than TIME_CONST */
77
#define IT87_TIMEOUT	(HZ*5/100)
78
79
static int io = IT87_CIR_DEFAULT_IOBASE;
80
static int irq = IT87_CIR_DEFAULT_IRQ;
81
static unsigned char it87_freq = 38; /* kHz */
82
/* receiver demodulator default: off */
83
static unsigned char it87_enable_demodulator = 0;
84
85
static spinlock_t timer_lock = SPIN_LOCK_UNLOCKED;
86
static struct timer_list timerlist;
87
/* time of last signal change detected */
88
static struct timeval last_tv = {0, 0};
89
/* time of last UART data ready interrupt */
90
static struct timeval last_intr_tv = {0, 0};
91
static int last_value = 0;
92
93
static DECLARE_WAIT_QUEUE_HEAD(lirc_read_queue);
94
95
static spinlock_t hardware_lock = SPIN_LOCK_UNLOCKED;
96
static spinlock_t dev_lock = SPIN_LOCK_UNLOCKED;
97
98
static lirc_t rx_buf[RBUF_LEN]; unsigned int rx_tail = 0, rx_head = 0;
99
static lirc_t tx_buf[WBUF_LEN];
100
101
/* SECTION: Prototypes */
102
103
/* Communication with user-space */
104
static int lirc_open(struct inode * inode,
105
		     struct file * file);
106
static int lirc_close(struct inode * inode,
107
		      struct file *file);
108
static unsigned int lirc_poll(struct file * file,
109
			      poll_table * wait);
110
static ssize_t lirc_read(struct file * file,
111
			 char * buf,
112
			 size_t count,
113
			 loff_t * ppos);
114
static ssize_t lirc_write(struct file * file,
115
			  const char * buf,
116
			  size_t n,
117
			  loff_t * pos);
118
static int lirc_ioctl(struct inode *node,
119
		      struct file *filep,
120
		      unsigned int cmd,
121
		      unsigned long arg);
122
static void add_read_queue(int flag,
123
			   unsigned long val);
124
static int init_chrdev(void);
125
static void drop_chrdev(void);
126
	/* Hardware */
127
static irqreturn_t it87_interrupt(int irq,
128
			   void * dev_id,
129
			   struct pt_regs * regs);
130
static void send_space(unsigned long len);
131
static void send_pulse(unsigned long len);
132
static void init_send(void);
133
static void terminate_send(unsigned long len);
134
static int init_hardware(void);
135
static void drop_hardware(void);
136
	/* Initialisation */
137
static int init_port(void);
138
static void drop_port(void);
139
int init_module(void);
140
void cleanup_module(void);
141
142
143
/* SECTION: Communication with user-space */
144
145
static int lirc_open(struct inode * inode,
146
		     struct file * file)
147
{
148
	spin_lock(&dev_lock);
149
#ifdef CONFIG_MODULE_UNLOAD
150
	if (module_refcount(THIS_MODULE))
151
	{
152
		spin_unlock(&dev_lock);
153
		return -EBUSY;
154
	}
155
#endif
156
	try_module_get(THIS_MODULE);
157
	spin_unlock(&dev_lock);
158
	return 0;
159
}
160
161
162
static int lirc_close(struct inode * inode,
163
		      struct file *file)
164
{
165
	module_put(THIS_MODULE);
166
	return 0;
167
}
168
169
170
static unsigned int lirc_poll(struct file * file,
171
			      poll_table * wait)
172
{
173
	poll_wait(file, &lirc_read_queue, wait);
174
	if (rx_head != rx_tail)
175
		return POLLIN | POLLRDNORM;
176
	return 0;
177
}
178
179
180
static ssize_t lirc_read(struct file * file,
181
			 char * buf,
182
			 size_t count,
183
			 loff_t * ppos)
184
{
185
	int n=0;
186
	int retval=0;
187
188
	while(n<count)
189
	{
190
		if(file->f_flags & O_NONBLOCK &&
191
		   rx_head==rx_tail)
192
		{
193
			retval = -EAGAIN;
194
			break;
195
		}
196
		retval=wait_event_interruptible(lirc_read_queue,
197
						rx_head!=rx_tail);
198
		if(retval)
199
		{
200
			break;
201
		}
202
203
		retval=verify_area(VERIFY_WRITE,(void *) buf+n,
204
				   sizeof(lirc_t));
205
		if (retval)
206
		{
207
			return retval;
208
		}
209
		copy_to_user((void *) buf+n,(void *) (rx_buf+rx_head),
210
			     sizeof(lirc_t));
211
		rx_head=(rx_head+1)&(RBUF_LEN-1);
212
		n+=sizeof(lirc_t);
213
	}
214
	if(n)
215
	{
216
		return n;
217
	}
218
	return retval;
219
}
220
221
222
static ssize_t lirc_write(struct file * file,
223
			  const char * buf,
224
			  size_t n,
225
			  loff_t * pos)
226
{
227
	int i;
228
	int retval;
229
230
        if(n%sizeof(lirc_t) || (n/sizeof(lirc_t)) > WBUF_LEN)
231
		return(-EINVAL);
232
	retval = verify_area(VERIFY_READ, buf, n);
233
	if (retval)
234
		return retval;
235
	copy_from_user(tx_buf, buf, n);
236
	i = 0;
237
	n/=sizeof(lirc_t);
238
	init_send();
239
	while (1) {
240
		if (i >= n)
241
			break;
242
		if (tx_buf[i])
243
			send_pulse(tx_buf[i]);
244
		i++;
245
		if (i >= n)
246
			break;
247
		if (tx_buf[i])
248
			send_space(tx_buf[i]);
249
		i++;
250
	}
251
	terminate_send(tx_buf[i-1]);
252
	return n;
253
}
254
255
256
static int lirc_ioctl(struct inode *node,
257
		      struct file *filep,
258
		      unsigned int cmd,
259
		      unsigned long arg)
260
{
261
	int retval = 0;
262
	unsigned long value = 0;
263
	unsigned int ivalue;
264
265
	if (cmd == LIRC_GET_FEATURES)
266
		value = LIRC_CAN_SEND_PULSE |
267
			LIRC_CAN_SET_SEND_CARRIER |
268
			LIRC_CAN_REC_MODE2;
269
	else if (cmd == LIRC_GET_SEND_MODE)
270
		value = LIRC_MODE_PULSE;
271
	else if (cmd == LIRC_GET_REC_MODE)
272
		value = LIRC_MODE_MODE2;
273
274
	switch (cmd) {
275
	case LIRC_GET_FEATURES:
276
	case LIRC_GET_SEND_MODE:
277
	case LIRC_GET_REC_MODE:
278
		retval = put_user(value, (unsigned long *) arg);
279
		break;
280
281
	case LIRC_SET_SEND_MODE:
282
	case LIRC_SET_REC_MODE:
283
		retval = get_user(value, (unsigned long *) arg);
284
		break;
285
286
	case LIRC_SET_SEND_CARRIER:
287
		retval=get_user(ivalue,(unsigned int *) arg);
288
		if(retval) return(retval);
289
		ivalue /= 1000;
290
		if (ivalue > IT87_CIR_FREQ_MAX ||
291
		    ivalue < IT87_CIR_FREQ_MIN) return(-EINVAL);
292
293
		it87_freq = ivalue;
294
		{
295
			unsigned long hw_flags;
296
297
			spin_lock_irqsave(&hardware_lock, hw_flags);
298
			outb(((inb(io + IT87_CIR_TCR2) & IT87_CIR_TCR2_TXMPW) |
299
			      (it87_freq - IT87_CIR_FREQ_MIN) << 3),
300
			     io + IT87_CIR_TCR2);
301
			spin_unlock_irqrestore(&hardware_lock, hw_flags);
302
#ifdef DEBUG
303
			printk(KERN_DEBUG LIRC_DRIVER_NAME
304
			       " demodulation frequency: %d kHz\n", it87_freq);
305
#endif
306
		}
307
308
		break;
309
310
	default:
311
		retval = -ENOIOCTLCMD;
312
	}
313
314
	if (retval)
315
		return retval;
316
317
	if (cmd == LIRC_SET_REC_MODE) {
318
		if (value != LIRC_MODE_MODE2)
319
			retval = -ENOSYS;
320
	} else if (cmd == LIRC_SET_SEND_MODE) {
321
		if (value != LIRC_MODE_PULSE)
322
			retval = -ENOSYS;
323
	}
324
	return retval;
325
}
326
327
static void add_read_queue(int flag,
328
			   unsigned long val)
329
{
330
	unsigned int new_rx_tail;
331
	lirc_t newval;
332
333
#ifdef DEBUG_SIGNAL
334
	printk(KERN_DEBUG LIRC_DRIVER_NAME
335
	       ": add flag %d with val %lu\n",
336
	       flag,val);
337
#endif
338
339
	newval = val & PULSE_MASK;
340
341
	/* statistically pulses are ~TIME_CONST/2 too long: we could
342
	   maybe make this more exactly but this is good enough */
343
	if(flag) /* pulse */ {
344
		if(newval>TIME_CONST/2) {
345
			newval-=TIME_CONST/2;
346
		}
347
		else /* should not ever happen */ {
348
			newval=1;
349
		}
350
		newval|=PULSE_BIT;
351
	}
352
	else {
353
		newval+=TIME_CONST/2;
354
	}
355
	new_rx_tail = (rx_tail + 1) & (RBUF_LEN - 1);
356
	if (new_rx_tail == rx_head) {
357
#ifdef DEBUG
358
		printk(KERN_WARNING LIRC_DRIVER_NAME ": Buffer overrun.\n");
359
#endif
360
		return;
361
	}
362
	rx_buf[rx_tail] = newval;
363
	rx_tail = new_rx_tail;
364
	wake_up_interruptible(&lirc_read_queue);
365
}
366
367
368
static struct file_operations lirc_fops = {
369
	read:    lirc_read,
370
	write:   lirc_write,
371
	poll:    lirc_poll,
372
	ioctl:   lirc_ioctl,
373
	open:    lirc_open,
374
	release: lirc_close,
375
};
376
377
static int set_use_inc(void* data)
378
{
379
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
380
       try_module_get(THIS_MODULE);
381
#endif
382
       return 0;
383
}
384
385
static void set_use_dec(void* data)
386
{
387
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
388
       module_put(THIS_MODULE);
389
#endif
390
}
391
static struct lirc_plugin plugin = {
392
       name:           LIRC_DRIVER_NAME,
393
       minor:          -1,
394
       code_length:    1,
395
       sample_rate:    0,
396
       data:           NULL,
397
       add_to_buf:     NULL,
398
       get_queue:      NULL,
399
       set_use_inc:    set_use_inc,
400
       set_use_dec:    set_use_dec,
401
       fops:           &lirc_fops,
402
};
403
404
405
int init_chrdev(void)
406
{
407
	plugin.minor = lirc_register_plugin(&plugin);
408
409
	if (plugin.minor < 0) {
410
		printk(KERN_ERR LIRC_DRIVER_NAME ": init_chrdev() failed.\n");
411
		return -EIO;
412
	}
413
	return 0;
414
}
415
416
417
static void drop_chrdev(void)
418
{
419
	lirc_unregister_plugin(plugin.minor);
420
}
421
422
/* SECTION: Hardware */
423
static long delta(struct timeval * tv1,
424
		  struct timeval * tv2)
425
{
426
	unsigned long deltv;
427
428
	deltv = tv2->tv_sec - tv1->tv_sec;
429
	if (deltv > 15)
430
		deltv = 0xFFFFFF;
431
	else
432
		deltv = deltv*1000000 +
433
			tv2->tv_usec -
434
			tv1->tv_usec;
435
	return deltv;
436
}
437
438
439
static void it87_timeout(unsigned long data)
440
{
441
	/* if last received signal was a pulse, but receiving stopped
442
	   within the 9 bit frame, we need to finish this pulse and
443
	   simulate a signal change to from pulse to space. Otherwise
444
	   upper layers will receive two sequences next time. */
445
446
	unsigned long flags;
447
	unsigned long pulse_end;
448
449
	/* avoid interference with interrupt */
450
 	spin_lock_irqsave(&timer_lock, flags);
451
	if (last_value) {
452
		/* determine 'virtual' pulse end: */
453
	 	pulse_end = delta(&last_tv, &last_intr_tv);
454
#ifdef DEBUG_SIGNAL
455
		printk(KERN_DEBUG LIRC_DRIVER_NAME
456
		       ": timeout add %d for %lu usec\n",
457
		       last_value,
458
		       pulse_end);
459
#endif
460
		add_read_queue(last_value,
461
			       pulse_end);
462
		last_value = 0;
463
		last_tv=last_intr_tv;
464
	}
465
	spin_unlock_irqrestore(&timer_lock, flags);
466
}
467
468
469
static irqreturn_t it87_interrupt(int irq,
470
			   void * dev_id,
471
			   struct pt_regs * regs)
472
{
473
	unsigned char data;
474
	struct timeval curr_tv;
475
	static unsigned long deltv;
476
	unsigned long deltintrtv;
477
	unsigned long flags, hw_flags;
478
	int iir, lsr;
479
	int fifo = 0;
480
481
	iir = inb(io + IT87_CIR_IIR);
482
483
	switch (iir & IT87_CIR_IIR_IID) {
484
	case 0x4:
485
	case 0x6:
486
		lsr = inb(io + IT87_CIR_RSR) & (IT87_CIR_RSR_RXFTO |
487
						    IT87_CIR_RSR_RXFBC);
488
		fifo = lsr & IT87_CIR_RSR_RXFBC;
489
#ifdef DEBUG_SIGNAL
490
		printk(KERN_DEBUG LIRC_DRIVER_NAME
491
		       "iir: 0x%x fifo: 0x%x\n", iir, lsr);
492
#endif
493
494
		/* avoid interference with timer */
495
		spin_lock_irqsave(&timer_lock, flags);
496
		spin_lock_irqsave(&hardware_lock, hw_flags);
497
		do {
498
			del_timer(&timerlist);
499
			data = inb(io + IT87_CIR_DR);
500
#ifdef DEBUG_SIGNAL
501
			printk(KERN_DEBUG LIRC_DRIVER_NAME
502
			       ": data=%.2x\n",
503
			       data);
504
#endif
505
			do_gettimeofday(&curr_tv);
506
			deltv = delta(&last_tv, &curr_tv);
507
			deltintrtv = delta(&last_intr_tv, &curr_tv);
508
#ifdef DEBUG_SIGNAL
509
			printk(KERN_DEBUG LIRC_DRIVER_NAME
510
			       ": t %lu , d %d\n",
511
			       deltintrtv,
512
			       (int)data);
513
#endif
514
			/* if nothing came in last 2 cycles,
515
			   it was gap */
516
			if (deltintrtv > TIME_CONST * 2) {
517
				if (last_value) {
518
#ifdef DEBUG_SIGNAL
519
					printk(KERN_DEBUG LIRC_DRIVER_NAME ": GAP\n");
520
#endif
521
					/* simulate signal change */
522
					add_read_queue(last_value,
523
						       deltv-
524
						       deltintrtv);
525
					last_value = 0;
526
					last_tv.tv_sec = last_intr_tv.tv_sec;
527
					last_tv.tv_usec = last_intr_tv.tv_usec;
528
					deltv = deltintrtv;
529
				}
530
			}
531
			data = 1;
532
			if (data ^ last_value) {
533
				/* deltintrtv > 2*TIME_CONST,
534
				   remember ? */
535
				/* the other case is timeout */
536
				add_read_queue(last_value,
537
					       deltv-TIME_CONST);
538
				last_value = data;
539
				last_tv = curr_tv;
540
				if(last_tv.tv_usec>=TIME_CONST) {
541
					last_tv.tv_usec-=TIME_CONST;
542
				}
543
				else {
544
					last_tv.tv_sec--;
545
					last_tv.tv_usec+=1000000-
546
						TIME_CONST;
547
				}
548
			}
549
			last_intr_tv = curr_tv;
550
			if (data) {
551
				/* start timer for end of sequence detection */
552
				timerlist.expires = jiffies + IT87_TIMEOUT;
553
				add_timer(&timerlist);
554
			}
555
			outb((inb(io + IT87_CIR_RCR) & ~IT87_CIR_RCR_RXEN) |
556
			     IT87_CIR_RCR_RXACT,
557
			     io + IT87_CIR_RCR);
558
			if (it87_RXEN_mask) {
559
				outb(inb(io + IT87_CIR_RCR) | IT87_CIR_RCR_RXEN,
560
				     io + IT87_CIR_RCR);
561
			}
562
			fifo--;
563
		}
564
		while (fifo != 0);
565
		spin_unlock_irqrestore(&hardware_lock, hw_flags);
566
		spin_unlock_irqrestore(&timer_lock, flags);
567
		break;
568
569
	default:
570
		/* not our irq */
571
#ifdef DEBUG_SIGNAL
572
		printk(KERN_DEBUG LIRC_DRIVER_NAME
573
		       "unknown IRQ (shouldn't happen) !!\n");
574
#endif
575
		break;
576
	}
577
	return IRQ_HANDLED; //FIXME true status should be returned (include/linux/interrupt.h)
578
}
579
580
581
static void send_it87(unsigned long len,
582
		      unsigned long stime,
583
		      unsigned char send_byte,
584
		      unsigned int count_bits)
585
{
586
        long count = len / stime;
587
	long time_left = 0;
588
	static unsigned char byte_out = 0;
589
590
#ifdef DEBUG_SIGNAL
591
	printk(KERN_DEBUG LIRC_DRIVER_NAME
592
	       "send_it87: len=%ld, sb=%d\n",
593
	       len,
594
	       send_byte);
595
#endif
596
	time_left = (long)len - (long)count * (long)stime;
597
	count += ((2 * time_left) / stime);
598
	while (count) {
599
		long i=0;
600
		for (i=0; i<count_bits; i++) {
601
			byte_out = (byte_out << 1) | (send_byte & 1);
602
			it87_bits_in_byte_out++;
603
		}
604
		if (it87_bits_in_byte_out == 8) {
605
#ifdef DEBUG_SIGNAL
606
			printk(KERN_DEBUG LIRC_DRIVER_NAME
607
			       "out=0x%x, tsr_txfbc: 0x%x\n",
608
			       byte_out,
609
			       inb(io + IT87_CIR_TSR) &
610
			       IT87_CIR_TSR_TXFBC);
611
#endif
612
			while ((inb(io + IT87_CIR_TSR) &
613
				IT87_CIR_TSR_TXFBC) >= IT87_CIR_FIFO_SIZE);
614
			{
615
				unsigned long hw_flags;
616
617
				spin_lock_irqsave(&hardware_lock, hw_flags);
618
				outb(byte_out, io + IT87_CIR_DR);
619
				spin_unlock_irqrestore(&hardware_lock, hw_flags);
620
			}
621
			it87_bits_in_byte_out = 0;
622
			it87_send_counter++;
623
			byte_out = 0;
624
		}
625
		count--;
626
	}
627
}
628
629
630
/*
631
maybe: exchange space and pulse because
632
it8705 only modulates 0-bits
633
*/
634
635
636
static void send_space(unsigned long len)
637
{
638
	send_it87(len,
639
		  TIME_CONST,
640
		  IT87_CIR_SPACE,
641
		  IT87_CIR_BAUDRATE_DIVISOR);
642
}
643
644
static void send_pulse(unsigned long len)
645
{
646
	send_it87(len,
647
		  TIME_CONST,
648
		  IT87_CIR_PULSE,
649
		  IT87_CIR_BAUDRATE_DIVISOR);
650
}
651
652
653
static void init_send()
654
{
655
	unsigned long flags;
656
657
	spin_lock_irqsave(&hardware_lock, flags);
658
	/* RXEN=0: receiver disable */
659
	it87_RXEN_mask = 0;
660
	outb(inb(io + IT87_CIR_RCR) & ~IT87_CIR_RCR_RXEN,
661
	     io + IT87_CIR_RCR);
662
	spin_unlock_irqrestore(&hardware_lock, flags);
663
	it87_bits_in_byte_out = 0;
664
	it87_send_counter = 0;
665
}
666
667
668
static void terminate_send(unsigned long len)
669
{
670
	unsigned long flags;
671
	unsigned long last = 0;
672
673
	last = it87_send_counter;
674
	/* make sure all necessary data has been sent */
675
	while (last == it87_send_counter)
676
		send_space(len);
677
	/* wait until all data sent */
678
	while ((inb(io + IT87_CIR_TSR) & IT87_CIR_TSR_TXFBC) != 0);
679
	/* then reenable receiver */
680
	spin_lock_irqsave(&hardware_lock, flags);
681
	it87_RXEN_mask = IT87_CIR_RCR_RXEN;
682
	outb(inb(io + IT87_CIR_RCR) | IT87_CIR_RCR_RXEN,
683
	     io + IT87_CIR_RCR);
684
	spin_unlock_irqrestore(&hardware_lock, flags);
685
}
686
687
688
static int init_hardware(void)
689
{
690
	unsigned long flags;
691
	unsigned char it87_rcr = 0;
692
693
	spin_lock_irqsave(&hardware_lock, flags);
694
	/* init cir-port */
695
	/* enable r/w-access to Baudrate-Register */
696
	outb(IT87_CIR_IER_BR, io + IT87_CIR_IER);
697
	outb(IT87_CIR_BAUDRATE_DIVISOR % 0x100, io+IT87_CIR_BDLR);
698
	outb(IT87_CIR_BAUDRATE_DIVISOR / 0x100, io+IT87_CIR_BDHR);
699
	/* Baudrate Register off, define IRQs: Input only */
700
	outb(IT87_CIR_IER_IEC | IT87_CIR_IER_RDAIE, io + IT87_CIR_IER);
701
	/* RX: HCFS=0, RXDCR = 001b (35,6..40,3 kHz), RXEN=1 */
702
	it87_rcr = (IT87_CIR_RCR_RXEN & it87_RXEN_mask) | 0x1;
703
	if (it87_enable_demodulator)
704
		it87_rcr |= IT87_CIR_RCR_RXEND;
705
	outb(it87_rcr, io + IT87_CIR_RCR);
706
	/* TX: 38kHz, 13,3us (pulse-width */
707
	outb(((it87_freq - IT87_CIR_FREQ_MIN) << 3) | 0x06,
708
	     io + IT87_CIR_TCR2);
709
	spin_unlock_irqrestore(&hardware_lock, flags);
710
	return 0;
711
}
712
713
714
static void drop_hardware(void)
715
{
716
	unsigned long flags;
717
718
	spin_lock_irqsave(&hardware_lock, flags);
719
	disable_irq(irq);
720
	/* receiver disable */
721
	it87_RXEN_mask = 0;
722
	outb(0x1, io + IT87_CIR_RCR);
723
	/* turn off irqs */
724
	outb(0, io + IT87_CIR_IER);
725
	/* fifo clear */
726
        outb(IT87_CIR_TCR1_FIFOCLR, io+IT87_CIR_TCR1);
727
        /* reset */
728
        outb(IT87_CIR_IER_RESET, io+IT87_CIR_IER);
729
	enable_irq(irq);
730
	spin_unlock_irqrestore(&hardware_lock, flags);
731
}
732
733
734
static unsigned char it87_read(unsigned char port)
735
{
736
	outb(port, IT87_ADRPORT);
737
	return inb(IT87_DATAPORT);
738
}
739
740
741
static void it87_write(unsigned char port,
742
		       unsigned char data)
743
{
744
	outb(port, IT87_ADRPORT);
745
	outb(data, IT87_DATAPORT);
746
}
747
748
749
/* SECTION: Initialisation */
750
751
static int init_port(void)
752
{
753
	int retval = 0;
754
755
	unsigned char init_bytes[4] = {IT87_INIT};
756
	unsigned char it87_chipid = 0;
757
	unsigned char ldn = 0;
758
	unsigned int  it87_io = 0;
759
	unsigned int  it87_irq = 0;
760
761
	/* Enter MB PnP Mode */
762
	outb(init_bytes[0], IT87_ADRPORT);
763
	outb(init_bytes[1], IT87_ADRPORT);
764
	outb(init_bytes[2], IT87_ADRPORT);
765
	outb(init_bytes[3], IT87_ADRPORT);
766
767
	/* 8712 or 8705 ? */
768
	it87_chipid = it87_read(IT87_CHIP_ID1);
769
	if (it87_chipid != 0x87) {
770
		retval = -ENXIO;
771
		return retval;
772
	}
773
	it87_chipid = it87_read(IT87_CHIP_ID2);
774
	if ((it87_chipid != 0x12) && (it87_chipid != 0x05)) {
775
		printk(KERN_INFO LIRC_DRIVER_NAME
776
		       ": no IT8705/12 found, exiting..\n");
777
		retval = -ENXIO;
778
		return retval;
779
	}
780
	printk(KERN_INFO LIRC_DRIVER_NAME
781
	       ": found IT87%.2x.\n",
782
	       it87_chipid);
783
784
	/* get I/O-Port and IRQ */
785
	if (it87_chipid == 0x12)
786
		ldn = IT8712_CIR_LDN;
787
	else
788
		ldn = IT8705_CIR_LDN;
789
	it87_write(IT87_LDN, ldn);
790
791
	it87_io = it87_read(IT87_CIR_BASE_MSB) * 256 +
792
		it87_read(IT87_CIR_BASE_LSB);
793
	if (it87_io == 0) {
794
		if (io == 0)
795
			io = IT87_CIR_DEFAULT_IOBASE;
796
		printk(KERN_INFO LIRC_DRIVER_NAME
797
		       ": set default io 0x%x\n",
798
		       io);
799
		it87_write(IT87_CIR_BASE_MSB, io / 0x100);
800
		it87_write(IT87_CIR_BASE_LSB, io % 0x100);
801
	}
802
	else
803
		io = it87_io;
804
805
	it87_irq = it87_read(IT87_CIR_IRQ);
806
	if (it87_irq == 0) {
807
		if (irq == 0)
808
			irq = IT87_CIR_DEFAULT_IRQ;
809
		printk(KERN_INFO LIRC_DRIVER_NAME
810
		       ": set default irq 0x%x\n",
811
		       irq);
812
		it87_write(IT87_CIR_IRQ, irq);
813
	}
814
	else
815
		irq = it87_irq;
816
817
	{
818
		unsigned long hw_flags;
819
820
		spin_lock_irqsave(&hardware_lock, hw_flags);
821
		/* reset */
822
		outb(IT87_CIR_IER_RESET, io+IT87_CIR_IER);
823
		/* fifo clear */
824
		outb(IT87_CIR_TCR1_FIFOCLR |
825
		     /*	     IT87_CIR_TCR1_ILE | */
826
		     IT87_CIR_TCR1_TXRLE |
827
		     IT87_CIR_TCR1_TXENDF, io+IT87_CIR_TCR1);
828
		spin_unlock_irqrestore(&hardware_lock, hw_flags);
829
	}
830
831
	/* get I/O port access and IRQ line */
832
	retval = request_region(io, 8, LIRC_DRIVER_NAME);
833
834
	if (!retval) {
835
		printk(KERN_ERR LIRC_DRIVER_NAME
836
		       ": Unable to reserve IO region for LIRC IT87. Port 0x%.4x already in use.\n",
837
		       io);
838
		/* Leaving MB PnP Mode */
839
		it87_write(IT87_CFGCTRL, 0x2);
840
		return retval;
841
	}
842
843
	/* activate CIR-Device */
844
	it87_write(IT87_CIR_ACT, 0x1);
845
846
	/* Leaving MB PnP Mode */
847
	it87_write(IT87_CFGCTRL, 0x2);
848
849
	retval = request_irq(irq, it87_interrupt, 0 /*SA_INTERRUPT*/,
850
			     LIRC_DRIVER_NAME, NULL);
851
	if (retval < 0) {
852
		printk(KERN_ERR LIRC_DRIVER_NAME
853
		       ": IRQ %d already in use.\n",
854
		       irq);
855
		return retval;
856
	}
857
858
	printk(KERN_INFO LIRC_DRIVER_NAME
859
	       ": I/O port 0x%.4x, IRQ %d.\n",
860
	       io,
861
	       irq);
862
863
	request_region(io, 8, LIRC_DRIVER_NAME);
864
	init_timer(&timerlist);
865
	timerlist.function = it87_timeout;
866
	timerlist.data = 0xabadcafe;
867
868
	return 0;
869
}
870
871
872
static void drop_port(void)
873
{
874
/*
875
        unsigned char init_bytes[4] = {IT87_INIT};
876
877
        / * Enter MB PnP Mode * /
878
        outb(init_bytes[0], IT87_ADRPORT);
879
        outb(init_bytes[1], IT87_ADRPORT);
880
        outb(init_bytes[2], IT87_ADRPORT);
881
        outb(init_bytes[3], IT87_ADRPORT);
882
883
        / * deactivate CIR-Device * /
884
        it87_write(IT87_CIR_ACT, 0x0);
885
886
        / * Leaving MB PnP Mode * /
887
        it87_write(IT87_CFGCTRL, 0x2);
888
*/
889
890
	del_timer_sync(&timerlist);
891
	free_irq(irq, NULL);
892
	release_region(io, 8);
893
}
894
895
896
int init_lirc_it87(void)
897
{
898
	int retval;
899
900
	init_waitqueue_head(&lirc_read_queue);
901
	retval = init_port();
902
	if (retval < 0)
903
		return retval;
904
	init_hardware();
905
	printk(KERN_INFO LIRC_DRIVER_NAME
906
	       ": Installed.\n");
907
	return 0;
908
}
909
910
911
MODULE_AUTHOR("Hans-Günter Lütke Uphues");
912
MODULE_DESCRIPTION("LIRC driver for ITE IT8712/IT8705 CIR port");
913
MODULE_PARM(io, "i");
914
MODULE_PARM_DESC(io,
915
		 "I/O base address (default: 0x310)");
916
MODULE_PARM(irq, "i");
917
MODULE_PARM_DESC(irq,
918
		 "Interrupt (1,3-12) (default: 7)");
919
MODULE_PARM(it87_enable_demodulator, "i");
920
MODULE_PARM_DESC(it87_enable_demodulator,
921
		 "Receiver demodulator enable/disable (1/0), default: 0");
922
MODULE_LICENSE("GPL");
923
924
static int __init lirc_it87_init(void)
925
{
926
	int retval;
927
928
	retval=init_chrdev();
929
	if(retval < 0)
930
		return retval;
931
	retval = init_lirc_it87();
932
	if (retval) {
933
		drop_chrdev();
934
		return retval;
935
	}
936
	return 0;
937
}
938
939
940
static void __exit lirc_it87_exit(void)
941
{
942
	drop_hardware();
943
	drop_chrdev();
944
	drop_port();
945
	printk(KERN_INFO LIRC_DRIVER_NAME ": Uninstalled.\n");
946
}
947
948
module_init(lirc_it87_init);
949
module_exit(lirc_it87_exit);
950
951
/*
952
 * Overrides for Emacs so that we follow Linus's tabbing style.
953
 * ---------------------------------------------------------------------------
954
 * Local variables:
955
 * c-basic-offset: 8
956
 * End:
957
 */
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_it87.h (+116 lines)
Line 0 Link Here
1
/* lirc_it87.h */
2
/* SECTION: Definitions */
3
4
/********************************* ITE IT87xx ************************/
5
6
/* based on the following documentation from ITE:
7
   a) IT8712F Preliminary CIR Programming Guide V0.1
8
   b) IT8705F Simple LPC I/O Preliminary Specifiction V0.3
9
   c) IT8712F EC-LPC I/O Preliminary Specification V0.5
10
*/
11
12
/* IT8712/05 Ports: */
13
#define IT87_ADRPORT      0x2e
14
#define IT87_DATAPORT     0x2f
15
#define IT87_INIT         0x87, 0x01, 0x55, 0x55
16
17
/* alternate Ports: */
18
/*
19
#define IT87_ADRPORT      0x4e
20
#define IT87_DATAPORT     0x4f
21
#define IT87_INIT         0x87, 0x01, 0x55, 0xaa
22
 */
23
24
/* IT8712/05 Registers */
25
#define IT87_CFGCTRL      0x2
26
#define IT87_LDN          0x7
27
#define IT87_CHIP_ID1     0x20
28
#define IT87_CHIP_ID2     0x21
29
#define IT87_CFG_VERSION  0x22
30
#define IT87_SWSUSPEND    0x23
31
32
#define IT8712_CIR_LDN    0xa
33
#define IT8705_CIR_LDN    0x7
34
35
/* CIR Configuration Registers: */
36
#define IT87_CIR_ACT      0x30
37
#define IT87_CIR_BASE_MSB 0x60
38
#define IT87_CIR_BASE_LSB 0x61
39
#define IT87_CIR_IRQ      0x70
40
#define IT87_CIR_CONFIG   0xf0
41
42
/* List of IT87_CIR registers: offset to BaseAddr */
43
#define IT87_CIR_DR   0
44
#define IT87_CIR_IER  1
45
#define IT87_CIR_RCR  2
46
#define IT87_CIR_TCR1 3
47
#define IT87_CIR_TCR2 4
48
#define IT87_CIR_TSR  5
49
#define IT87_CIR_RSR  6
50
#define IT87_CIR_BDLR 5
51
#define IT87_CIR_BDHR 6
52
#define IT87_CIR_IIR  7
53
54
/* Bit Definitionen */
55
/* IER: */
56
#define IT87_CIR_IER_TM_EN   0x80
57
#define IT87_CIR_IER_RESEVED 0x40
58
#define IT87_CIR_IER_RESET   0x20
59
#define IT87_CIR_IER_BR      0x10
60
#define IT87_CIR_IER_IEC     0x8
61
#define IT87_CIR_IER_RFOIE   0x4
62
#define IT87_CIR_IER_RDAIE   0x2
63
#define IT87_CIR_IER_TLDLIE  0x1
64
65
/* RCR: */
66
#define IT87_CIR_RCR_RDWOS  0x80
67
#define IT87_CIR_RCR_HCFS   0x40
68
#define IT87_CIR_RCR_RXEN   0x20
69
#define IT87_CIR_RCR_RXEND  0x10
70
#define IT87_CIR_RCR_RXACT  0x8
71
#define IT87_CIR_RCR_RXDCR  0x7
72
73
/* TCR1: */
74
#define IT87_CIR_TCR1_FIFOCLR 0x80
75
#define IT87_CIR_TCR1_ILE     0x40
76
#define IT87_CIR_TCR1_FIFOTL  0x30
77
#define IT87_CIR_TCR1_TXRLE   0x8
78
#define IT87_CIR_TCR1_TXENDF  0x4
79
#define IT87_CIR_TCR1_TXMPM   0x3
80
81
/* TCR2: */
82
#define IT87_CIR_TCR2_CFQ   0xf8
83
#define IT87_CIR_TCR2_TXMPW 0x7
84
85
/* TSR: */
86
#define IT87_CIR_TSR_RESERVED 0xc0
87
#define IT87_CIR_TSR_TXFBC    0x3f
88
89
/* RSR: */
90
#define IT87_CIR_RSR_RXFTO    0x80
91
#define IT87_CIR_RSR_RESERVED 0x40
92
#define IT87_CIR_RSR_RXFBC    0x3f
93
94
/* IIR: */
95
#define IT87_CIR_IIR_RESERVED 0xf8
96
#define IT87_CIR_IIR_IID      0x6
97
#define IT87_CIR_IIR_IIP      0x1
98
99
/* TM: */
100
#define IT87_CIR_TM_IL_SEL    0x80
101
#define IT87_CIR_TM_RESERVED  0x40
102
#define IT87_CIR_TM_TM_REG    0x3f
103
104
#define IT87_CIR_FIFO_SIZE 32
105
106
/* Baudratedivisor for IT87: power of 2: only 1,2,4 or 8) */
107
#define IT87_CIR_BAUDRATE_DIVISOR 0x1
108
#define IT87_CIR_DEFAULT_IOBASE 0x310
109
#define IT87_CIR_DEFAULT_IRQ    0x7
110
#define IT87_CIR_SPACE 0x00
111
#define IT87_CIR_PULSE 0xff
112
#define IT87_CIR_FREQ_MIN 27
113
#define IT87_CIR_FREQ_MAX 58
114
#define TIME_CONST (IT87_CIR_BAUDRATE_DIVISOR * 8000000ul / 115200ul)
115
116
/********************************* ITE IT87xx ************************/
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_mceusb.c (+1530 lines)
Line 0 Link Here
1
/*
2
 * USB Microsoft IR Transceiver driver - 0.1
3
 *
4
 * Copyright (c) 2003-2004 Dan Conti (dconti@acm.wwu.edu)
5
 *
6
 * This driver is based on the USB skeleton driver packaged with the
7
 * kernel, and the notice from that package has been retained below.
8
 *
9
 * The Microsoft IR Transceiver is a neat little IR receiver with two
10
 * emitters on it designed for Windows Media Center. This driver might
11
 * work for all media center remotes, but I have only tested it with
12
 * the philips model. The first revision of this driver only supports
13
 * the receive function - the transmit function will be much more
14
 * tricky due to the nature of the hardware. Microsoft chose to build
15
 * this device inexpensively, therefore making it extra dumb.  There
16
 * is no interrupt endpoint on this device; all usb traffic happens
17
 * over two bulk endpoints. As a result of this, poll() for this
18
 * device is an actual hardware poll (instead of a receive queue
19
 * check) and is rather expensive.
20
 *
21
 * This driver is structured in three basic layers
22
 *  - lower  - interface with the usb device and manage usb data
23
 *  - middle - api to convert usb data into mode2 and provide this in
24
 *    _read calls
25
 *  - mceusb_* - linux driver interface
26
 *
27
 * The key routines are as follows:
28
 *  msir_fetch_more_data - this reads incoming data, strips off the
29
 *                         start codes the ir receiver places on them,
30
 *                         and dumps it in an * internal buffer
31
 *  msir_generate_mode2  - this takes the above data, depacketizes it,
32
 *                         and generates mode2 data to feed out
33
 *                         through read calls
34
 *
35
 *
36
 * All trademarks property of their respective owners.
37
 *
38
 * 2003_11_11 - Restructured to minimalize code interpretation in the
39
 *              driver. The normal use case will be with lirc.
40
 *
41
 * 2004_01_01 - Removed all code interpretation. Generate mode2 data
42
 *              for passing off to lirc. Cleanup
43
 *
44
 * 2004_01_04 - Removed devfs handle. Put in a temporary workaround
45
 *              for a known issue where repeats generate two
46
 *              sequential spaces * (last_was_repeat_gap)
47
 *
48
 * TODO
49
 *   - Fix up minor number, registration of major/minor with usb subsystem
50
 *   - Fix up random EINTR being sent
51
 *   - Fix problem where third key in a repeat sequence is randomly truncated
52
 *
53
 */
54
/*
55
 * USB Skeleton driver - 0.6
56
 *
57
 * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
58
 *
59
 *	This program is free software; you can redistribute it and/or
60
 *	modify it under the terms of the GNU General Public License as
61
 *	published by the Free Software Foundation; either version 2 of
62
 *	the License, or (at your option) any later version.
63
 *
64
 *
65
 * This driver is to be used as a skeleton driver to be able to create a
66
 * USB driver quickly.  The design of it is based on the usb-serial and
67
 * dc2xx drivers.
68
 *
69
 * Thanks to Oliver Neukum and David Brownell for their help in debugging
70
 * this driver.
71
 *
72
 * TODO:
73
 *	- fix urb->status race condition in write sequence
74
 *	- move minor_table to a dynamic list.
75
 *
76
 * History:
77
 *
78
 * 2001_11_05 - 0.6 - fix minor locking problem in skel_disconnect.
79
 *			Thanks to Pete Zaitcev for the fix.
80
 * 2001_09_04 - 0.5 - fix devfs bug in skel_disconnect. Thanks to wim delvaux
81
 * 2001_08_21 - 0.4 - more small bug fixes.
82
 * 2001_05_29 - 0.3 - more bug fixes based on review from linux-usb-devel
83
 * 2001_05_24 - 0.2 - bug fixes based on review from linux-usb-devel people
84
 * 2001_05_01 - 0.1 - first version
85
 *
86
 */
87
88
#include <linux/config.h>
89
#include <linux/kernel.h>
90
#include <linux/sched.h>
91
#include <linux/wait.h>
92
#include <linux/signal.h>
93
#include <linux/errno.h>
94
#include <linux/poll.h>
95
#include <linux/init.h>
96
#include <linux/slab.h>
97
#include <linux/fcntl.h>
98
#include <linux/module.h>
99
#include <linux/spinlock.h>
100
#include <linux/list.h>
101
#include <linux/smp_lock.h>
102
#include <linux/usb.h>
103
104
#ifdef CONFIG_USB_DEBUG
105
	static int debug = 1;
106
#else
107
	static int debug = 1;
108
#endif
109
110
#include <linux/lirc.h>
111
#include "drivers/lirc_dev/lirc_dev.h"
112
113
/* Version Information */
114
#define DRIVER_VERSION "v0.1"
115
#define DRIVER_AUTHOR "Dan Conti, dconti@acm.wwu.edu"
116
#define DRIVER_DESC "USB Microsoft IR Transceiver Driver"
117
118
/* Module paramaters */
119
MODULE_PARM(debug, "i");
120
MODULE_PARM_DESC(debug, "Debug enabled or not");
121
122
/* Define these values to match your device */
123
#define USB_MCEUSB_VENDOR_ID	0x045e
124
#define USB_MCEUSB_PRODUCT_ID	0x006d
125
126
/* table of devices that work with this driver */
127
static struct usb_device_id mceusb_table [] = {
128
	{ USB_DEVICE(USB_MCEUSB_VENDOR_ID, USB_MCEUSB_PRODUCT_ID) },
129
	{ }					/* Terminating entry */
130
};
131
132
MODULE_DEVICE_TABLE (usb, mceusb_table);
133
134
/* XXX TODO, 244 is likely unused but not reserved */
135
/* Get a minor range for your devices from the usb maintainer */
136
#define USB_MCEUSB_MINOR_BASE	244
137
138
139
/* we can have up to this number of device plugged in at once */
140
#define MAX_DEVICES		16
141
142
/* Structure to hold all of our device specific stuff */
143
struct usb_skel	     {
144
	/* save off the usb device pointer */
145
	struct usb_device *	    udev;
146
	/* the interface for this device */
147
	struct usb_interface *	interface;
148
	/* the starting minor number for this device */
149
	unsigned char   minor;
150
	/* the number of ports this device has */
151
	unsigned char   num_ports;
152
	/* number of interrupt in endpoints we have */
153
	char            num_interrupt_in;
154
	/* number of bulk in endpoints we have */
155
	char            num_bulk_in;
156
	/* number of bulk out endpoints we have */
157
	char            num_bulk_out;
158
159
	/* the buffer to receive data */
160
	unsigned char *    bulk_in_buffer;
161
	/* the size of the receive buffer */
162
	int                bulk_in_size;
163
	/* the address of the bulk in endpoint */
164
	__u8               bulk_in_endpointAddr;
165
166
	/* the buffer to send data */
167
	unsigned char *    bulk_out_buffer;
168
	/* the size of the send buffer */
169
	int	           bulk_out_size;
170
	/* the urb used to send data */
171
	struct urb *       write_urb;
172
	/* the address of the bulk out endpoint */
173
	__u8               bulk_out_endpointAddr;
174
175
	wait_queue_head_t  wait_q;      /* for timeouts */
176
	int                open_count;	/* number of times this port
177
					 * has been opened */
178
	struct semaphore   sem;		/* locks this structure */
179
180
	struct lirc_plugin* plugin;
181
182
	/* Used in converting to mode2 and storing */
183
        /* buffer for the mode2 data, since lirc reads 4bytes */
184
	int    mode2_data[256];
185
	int    mode2_idx;               /* read index */
186
	int    mode2_count;             /* words available (i.e. write
187
					 * index) */
188
	int    mode2_partial_pkt_size;
189
	int    mode2_once;
190
191
	/* Used for storing preprocessed usb data before converting to mode2*/
192
	char   usb_dbuffer[1024];
193
	int    usb_dstart;
194
	int    usb_dcount;
195
	int    usb_valid_bytes_in_bulk_buffer;
196
197
	/* Set to 1 if the last value we adjusted was a repeat gap; we
198
	 * need to hold this value around until we process a lead
199
	 * space on the repeat code, otherwise we pass off two
200
	 * sequential spaces */
201
	int    last_was_repeat_gap;
202
};
203
204
/* driver api */
205
static ssize_t mceusb_read	(struct file *file, char *buffer,
206
				 size_t count, loff_t *ppos);
207
static ssize_t mceusb_write	(struct file *file, const char *buffer,
208
				 size_t count, loff_t *ppos);
209
static unsigned int mceusb_poll (struct file* file, poll_table* wait);
210
211
static int mceusb_open		(struct inode *inode, struct file *file);
212
static int mceusb_release	(struct inode *inode, struct file *file);
213
214
static void * mceusb_probe	(struct usb_device *dev, unsigned int ifnum,
215
				 const struct usb_device_id *id);
216
static void *mceusb_disconnect	(struct usb_interface *intf);
217
218
static void mceusb_write_bulk_callback	(struct urb *urb);
219
220
/* lower level api */
221
static int msir_fetch_more_data( struct usb_skel* dev, int dont_block );
222
static int msir_read_from_buffer( struct usb_skel* dev, char* buffer, int len );
223
static int msir_mark_as_read( struct usb_skel* dev, int count );
224
static int msir_available_data( struct usb_skel* dev );
225
226
/* middle */
227
static int msir_generate_mode2( struct usb_skel* dev, signed char* usb_data,
228
				int bytecount );
229
static int msir_copy_mode2( struct usb_skel* dev, int* mode2_data, int count );
230
static int msir_available_mode2( struct usb_skel* dev );
231
232
/* helper functions */
233
static void msir_cleanup( struct usb_skel* dev );
234
static int set_use_inc(void* data);
235
static void set_use_dec(void* data);
236
237
/* array of pointers to our devices that are currently connected */
238
static struct usb_skel		*minor_table[MAX_DEVICES];
239
240
/* lock to protect the minor_table structure */
241
static DECLARE_MUTEX (minor_table_mutex);
242
243
/*
244
 * File operations needed when we register this driver.
245
 * This assumes that this driver NEEDS file operations,
246
 * of course, which means that the driver is expected
247
 * to have a node in the /dev directory. If the USB
248
 * device were for a network interface then the driver
249
 * would use "struct net_driver" instead, and a serial
250
 * device would use "struct tty_driver".
251
 */
252
static struct file_operations mceusb_fops = {
253
	/*
254
	 * The owner field is part of the module-locking
255
	 * mechanism. The idea is that the kernel knows
256
	 * which module to increment the use-counter of
257
	 * BEFORE it calls the device's open() function.
258
	 * This also means that the kernel can decrement
259
	 * the use-counter again before calling release()
260
	 * or should the open() function fail.
261
	 *
262
	 * Not all device structures have an "owner" field
263
	 * yet. "struct file_operations" and "struct net_device"
264
	 * do, while "struct tty_driver" does not. If the struct
265
	 * has an "owner" field, then initialize it to the value
266
	 * THIS_MODULE and the kernel will handle all module
267
	 * locking for you automatically. Otherwise, you must
268
	 * increment the use-counter in the open() function
269
	 * and decrement it again in the release() function
270
	 * yourself.
271
	 */
272
	owner:		THIS_MODULE,
273
274
	read:		mceusb_read,
275
	write:		mceusb_write,
276
	poll:           mceusb_poll,
277
	ioctl:          NULL,
278
	open:		mceusb_open,
279
	release:	mceusb_release,
280
};
281
282
283
/* usb specific object needed to register this driver with the usb subsystem */
284
static struct usb_driver mceusb_driver = {
285
	name:		"ir_transceiver",
286
	probe:		mceusb_probe,
287
	disconnect:	mceusb_disconnect,
288
	fops:		&mceusb_fops,
289
	minor:		USB_MCEUSB_MINOR_BASE,
290
	id_table:	mceusb_table,
291
};
292
293
294
/**
295
 *	usb_mceusb_debug_data
296
 */
297
static inline void usb_mceusb_debug_data (const char *function, int size,
298
					  const unsigned char *data)
299
{
300
	int i;
301
302
	if (!debug)
303
		return;
304
305
	printk (KERN_DEBUG __FILE__": %s - length = %d, data = ",
306
		function, size);
307
	for (i = 0; i < size; ++i) {
308
		printk ("%.2x ", data[i]);
309
	}
310
	printk ("\n");
311
}
312
313
314
/**
315
 *	mceusb_delete
316
 */
317
static inline void mceusb_delete (struct usb_skel *dev)
318
{
319
	minor_table[dev->minor] = NULL;
320
	if (dev->bulk_in_buffer != NULL)
321
		kfree (dev->bulk_in_buffer);
322
	if (dev->bulk_out_buffer != NULL)
323
		kfree (dev->bulk_out_buffer);
324
	if (dev->write_urb != NULL)
325
		usb_free_urb (dev->write_urb);
326
	kfree (dev);
327
}
328
329
static void mceusb_setup( struct usb_device *udev )
330
{
331
	char data[8];
332
	int res;
333
	memset( data, 0, 8 );
334
335
	/* Get Status */
336
	res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
337
			      USB_REQ_GET_STATUS, USB_DIR_IN,
338
			      0, 0, data, 2, HZ * 3);
339
340
	/*    res = usb_get_status( udev, 0, 0, data ); */
341
	dbg(__FUNCTION__ " res = %d status = 0x%x 0x%x",
342
	    res, data[0], data[1] );
343
344
	/* This is a strange one. They issue a set address to the
345
	 * device on the receive control pipe and expect a certain
346
	 * value pair back
347
	 */
348
	memset( data, 0, 8 );
349
350
	res = usb_control_msg( udev, usb_rcvctrlpipe(udev, 0),
351
			       5, USB_TYPE_VENDOR, 0, 0,
352
			       data, 2, HZ * 3 );
353
	dbg(__FUNCTION__ " res = %d, devnum = %d", res, udev->devnum);
354
	dbg(__FUNCTION__ " data[0] = %d, data[1] = %d", data[0], data[1] );
355
356
	/* set feature */
357
	res = usb_control_msg( udev, usb_sndctrlpipe(udev, 0),
358
			       USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
359
			       0xc04e, 0x0000, NULL, 0, HZ * 3 );
360
361
	dbg(__FUNCTION__ " res = %d", res);
362
363
	/* These two are sent by the windows driver, but stall for
364
	 * me. I dont have an analyzer on the linux side so i can't
365
	 * see what is actually different and why * the device takes
366
	 * issue with them
367
	 */
368
#if 0
369
	/* this is some custom control message they send */
370
	res = usb_control_msg( udev, usb_sndctrlpipe(udev, 0),
371
			       0x04, USB_TYPE_VENDOR,
372
			       0x0808, 0x0000, NULL, 0, HZ * 3 );
373
374
	dbg(__FUNCTION__ " res = %d", res);
375
376
	/* this is another custom control message they send */
377
	res = usb_control_msg( udev, usb_sndctrlpipe(udev, 0),
378
			       0x02, USB_TYPE_VENDOR,
379
			       0x0000, 0x0100, NULL, 0, HZ * 3 );
380
381
	dbg(__FUNCTION__ " res = %d", res);
382
#endif
383
}
384
385
/**
386
 *	mceusb_open
387
 */
388
static int mceusb_open (struct inode *inode, struct file *file)
389
{
390
	struct usb_skel *dev = NULL;
391
	struct usb_device* udev = NULL;
392
	int subminor;
393
	int retval = 0;
394
395
	dbg(__FUNCTION__);
396
397
	/* This is a very sucky point. On lirc, we get passed the
398
	 * minor number of the lirc device, which is totally
399
	 * retarded. We want to support people opening /dev/usb/msir0
400
	 * directly though, so try and determine who the hell is
401
	 * calling us here
402
	 */
403
	if( MAJOR( inode->i_rdev ) != USB_MAJOR )
404
	{
405
		/* This is the lirc device just passing on the
406
		 * request. We probably mismatch minor numbers here,
407
		 * but the lucky fact is that nobody will ever use two
408
		 * of the exact same remotes with two recievers on one
409
		 * machine
410
		 */
411
		subminor = 0;
412
	} else {
413
		subminor = MINOR (inode->i_rdev) - USB_MCEUSB_MINOR_BASE;
414
	}
415
	if ((subminor < 0) ||
416
	    (subminor >= MAX_DEVICES)) {
417
		dbg("subminor %d", subminor);
418
		return -ENODEV;
419
	}
420
421
	/* Increment our usage count for the module.
422
	 * This is redundant here, because "struct file_operations"
423
	 * has an "owner" field. This line is included here soley as
424
	 * a reference for drivers using lesser structures... ;-)
425
	 */
426
	try_module_get(THIS_MODULE);
427
428
	/* lock our minor table and get our local data for this minor */
429
	down (&minor_table_mutex);
430
	dev = minor_table[subminor];
431
	if (dev == NULL) {
432
		dbg("dev == NULL");
433
		up (&minor_table_mutex);
434
		module_put(THIS_MODULE);
435
		return -ENODEV;
436
	}
437
	udev = dev->udev;
438
439
	/* lock this device */
440
	down (&dev->sem);
441
442
	/* unlock the minor table */
443
	up (&minor_table_mutex);
444
445
	/* increment our usage count for the driver */
446
	++dev->open_count;
447
448
	/* save our object in the file's private structure */
449
	file->private_data = dev;
450
451
	/* init the waitq */
452
	init_waitqueue_head( &dev->wait_q );
453
454
	/* clear off the first few messages. these look like
455
	 * calibration or test data, i can't really tell
456
	 * this also flushes in case we have random ir data queued up
457
	 */
458
	{
459
		char junk[64];
460
		int partial = 0, retval, i;
461
		for( i = 0; i < 40; i++ )
462
		{
463
			retval = usb_bulk_msg (udev,
464
					       usb_rcvbulkpipe
465
					       (udev,
466
						dev->bulk_in_endpointAddr),
467
					       junk, 64,
468
					       &partial, HZ*10);
469
		}
470
	}
471
472
	msir_cleanup( dev );
473
474
	/* unlock this device */
475
	up (&dev->sem);
476
477
	return retval;
478
}
479
480
481
/**
482
 *	mceusb_release
483
 */
484
static int mceusb_release (struct inode *inode, struct file *file)
485
{
486
	struct usb_skel *dev;
487
	int retval = 0;
488
489
	dev = (struct usb_skel *)file->private_data;
490
	if (dev == NULL) {
491
		dbg (__FUNCTION__ " - object is NULL");
492
		return -ENODEV;
493
	}
494
495
	dbg(__FUNCTION__ " - minor %d", dev->minor);
496
497
	/* lock our minor table */
498
	down (&minor_table_mutex);
499
500
	/* lock our device */
501
	down (&dev->sem);
502
503
	if (dev->open_count <= 0) {
504
		dbg (__FUNCTION__ " - device not opened");
505
		retval = -ENODEV;
506
		goto exit_not_opened;
507
	}
508
509
	if (dev->udev == NULL) {
510
		/* the device was unplugged before the file was released */
511
		up (&dev->sem);
512
		mceusb_delete (dev);
513
		up (&minor_table_mutex);
514
		module_put(THIS_MODULE);
515
		return 0;
516
	}
517
518
	/* decrement our usage count for the device */
519
	--dev->open_count;
520
	if (dev->open_count <= 0) {
521
		/* shutdown any bulk writes that might be going on */
522
		usb_unlink_urb (dev->write_urb);
523
		dev->open_count = 0;
524
	}
525
526
	/* decrement our usage count for the module */
527
	module_put(THIS_MODULE);
528
529
  exit_not_opened:
530
	up (&dev->sem);
531
	up (&minor_table_mutex);
532
533
	return retval;
534
}
535
536
static void msir_cleanup( struct usb_skel* dev )
537
{
538
	memset( dev->bulk_in_buffer, 0, dev->bulk_in_size );
539
540
	memset( dev->usb_dbuffer, 0, sizeof(dev->usb_dbuffer) );
541
	dev->usb_dstart = 0;
542
	dev->usb_dcount = 0;
543
	dev->usb_valid_bytes_in_bulk_buffer = 0;
544
545
	memset( dev->mode2_data, 0, sizeof(dev->mode2_data) );
546
	dev->mode2_partial_pkt_size = 0;
547
	dev->mode2_count = 0;
548
	dev->mode2_idx = 0;
549
	dev->mode2_once = 0;
550
	dev->last_was_repeat_gap = 0;
551
}
552
553
static int set_use_inc(void* data)
554
{
555
	/*    struct usb_skel* skel = (struct usb_skel*)data; */
556
557
	try_module_get(THIS_MODULE);
558
	return 0;
559
}
560
561
static void set_use_dec(void* data)
562
{
563
	/* check for unplug here */
564
	struct usb_skel* dev = (struct usb_skel*) data;
565
	if( !dev->udev )
566
	{
567
		lirc_unregister_plugin( dev->minor );
568
		lirc_buffer_free( dev->plugin->rbuf );
569
		kfree( dev->plugin->rbuf );
570
		kfree( dev->plugin );
571
	}
572
573
	module_put(THIS_MODULE);
574
}
575
576
static int msir_available_mode2( struct usb_skel* dev )
577
{
578
	return dev->mode2_count - dev->last_was_repeat_gap;
579
}
580
581
static int msir_available_data( struct usb_skel* dev )
582
{
583
	return dev->usb_dcount;
584
}
585
586
static int msir_copy_mode2( struct usb_skel* dev, int* mode2_data, int count )
587
{
588
	int words_to_read = count;
589
	//    int words_avail   = dev->mode2_count;
590
	int words_avail = msir_available_mode2( dev );
591
592
	if( !dev->mode2_once && words_avail )
593
	{
594
		int space = PULSE_MASK;
595
		count--;
596
		copy_to_user( mode2_data, &space, 4 );
597
		dev->mode2_once = 1;
598
599
		if( count )
600
		{
601
			mode2_data++;
602
		}
603
		else
604
		{
605
			return 1;
606
		}
607
	}
608
609
	if( !words_avail )
610
	{
611
		return 0;
612
	}
613
614
	if( words_to_read > words_avail )
615
	{
616
		words_to_read = words_avail;
617
	}
618
619
	dbg(__FUNCTION__ " dev->mode2_count %d, dev->mode2_idx %d",
620
	    dev->mode2_count, dev->mode2_idx);
621
	dbg(__FUNCTION__ " words_avail %d words_to_read %d",
622
	    words_avail, words_to_read);
623
	copy_to_user( mode2_data, &( dev->mode2_data[dev->mode2_idx] ),
624
		      words_to_read<<2 );
625
	dbg(__FUNCTION__ " would copy_to_user() %d w", words_to_read);
626
627
	dev->mode2_idx += words_to_read;
628
	dev->mode2_count -= words_to_read;
629
630
	if( dev->mode2_count == 0 )
631
	{
632
		dev->mode2_idx = 0;
633
	}
634
	else if( dev->mode2_count == 1 && dev->last_was_repeat_gap )
635
	{
636
		// shift down the repeat gap and map it up to a
637
		// lirc-acceptable value
638
		dev->mode2_data[0] = dev->mode2_data[dev->mode2_idx];
639
		if( dev->mode2_data[0] >= 60000 &&
640
		    dev->mode2_data[0] <= 70000 )
641
			dev->mode2_data[0] = 95000;
642
		//printk(__FUNCTION__ " shifting value %d down from %d prev %d\n", dev->mode2_data[0], dev->mode2_idx,
643
		//    dev->mode2_data[dev->mode2_idx-1]);
644
		dev->mode2_idx = 0;
645
	}
646
647
	return words_to_read;
648
}
649
650
static int msir_read_from_buffer( struct usb_skel* dev, char* buffer, int len )
651
{
652
	if( len > dev->usb_dcount )
653
	{
654
		len = dev->usb_dcount;
655
	}
656
	memcpy( buffer, dev->usb_dbuffer + dev->usb_dstart, len );
657
	return len;
658
}
659
660
static int msir_mark_as_read( struct usb_skel* dev, int count )
661
{
662
	//    if( count != dev->usb_dcount )
663
	//        printk(KERN_INFO __FUNCTION__ " count %d dev->usb_dcount %d dev->usb_dstart %d", count, dev->usb_dcount, dev->usb_dstart );
664
	if( count > dev->usb_dcount )
665
		count = dev->usb_dcount;
666
	dev->usb_dcount -= count;
667
	dev->usb_dstart += count;
668
669
	if( !dev->usb_dcount )
670
		dev->usb_dstart = 0;
671
672
	return 0;
673
}
674
675
676
/*
677
 * msir_fetch_more_data
678
 *
679
 * The goal here is to read in more remote codes from the remote. In
680
 * the event that the remote isn't sending us anything, the caller
681
 * will block until a key is pressed (i.e. this performs phys read,
682
 * filtering, and queueing of data) unless dont_block is set to 1; in
683
 * this situation, it will perform a few reads and will exit out if it
684
 * does not see any appropriate data
685
 *
686
 * dev->sem should be locked when this function is called - fine grain
687
 * locking isn't really important here anyways
688
 *
689
 * TODO change this to do partials based on term codes, or not always fill
690
 */
691
692
static int msir_fetch_more_data( struct usb_skel* dev, int dont_block )
693
{
694
	int retries = 0;
695
	int count, this_read, partial;
696
	int retval;
697
	int writeindex, terminators = 0;
698
	int bytes_to_read = sizeof(dev->usb_dbuffer) - dev->usb_dcount;
699
	signed char* ibuf;
700
	int sequential_empty_reads = 0;
701
702
	/* special case where we are already full */
703
	if( bytes_to_read == 0 )
704
		return dev->usb_dcount;
705
706
	/* shift down */
707
	if( dev->usb_dcount && dev->usb_dstart != 0 )
708
	{
709
		printk( __FUNCTION__ " shifting %d bytes from %d\n",
710
			dev->usb_dcount, dev->usb_dstart );
711
		memcpy( dev->usb_dbuffer, dev->usb_dbuffer + dev->usb_dstart,
712
			dev->usb_dcount );
713
	}
714
715
	dev->usb_dstart = 0;
716
717
	writeindex = dev->usb_dcount;
718
719
	count = bytes_to_read;
720
721
	ibuf = (signed char*)dev->bulk_in_buffer;
722
	if( !dev->usb_valid_bytes_in_bulk_buffer )
723
	{
724
		memset( ibuf, 0, dev->bulk_in_size );
725
	}
726
727
#if 0
728
	printk( __FUNCTION__ " going to read, dev->usb_dcount %d, bytes_to_read %d vbb %d\n", dev->usb_dcount, bytes_to_read,
729
		dev->usb_valid_bytes_in_bulk_buffer );
730
#endif
731
	/* 8 is the minimum read size */
732
	while( count > 8 )
733
	{
734
		int i, goodbytes = 0;
735
736
		/* break out if we were interrupted */
737
		if( signal_pending(current) )
738
		{
739
			printk( __FUNCTION__ " got signal %ld\n",
740
				current->pending.signal.sig[0]);
741
			return dev->usb_dcount ? dev->usb_dcount : -EINTR;
742
		}
743
744
		/* or if we were unplugged */
745
		if( !dev->udev )
746
		{
747
			return -ENODEV;
748
		}
749
750
		/* or on data issues */
751
		if( writeindex == sizeof(dev->usb_dbuffer) )
752
		{
753
			printk( __FUNCTION__ " buffer full, returning\n");
754
			return dev->usb_dcount;
755
		}
756
757
		// always read the maximum
758
		this_read = dev->bulk_in_size;
759
760
		partial = 0;
761
762
		if( dev->usb_valid_bytes_in_bulk_buffer ) {
763
			retval = 0;
764
			this_read = partial = dev->usb_valid_bytes_in_bulk_buffer;
765
			dev->usb_valid_bytes_in_bulk_buffer = 0;
766
		} else {
767
			// This call always returns almost immediately
768
			// with data, since this device will always
769
			// provide a 2 byte response on a bulk
770
			// read. Not exactly friendly to the usb bus
771
			// or our load avg. We attempt to compensate
772
			// for this on 2 byte reads below
773
774
			memset( ibuf, 0, dev->bulk_in_size );
775
			retval = usb_bulk_msg (dev->udev,
776
					       usb_rcvbulkpipe
777
					       (dev->udev,
778
						dev->bulk_in_endpointAddr),
779
					       (unsigned char*)ibuf, this_read,
780
					       &partial, HZ*10);
781
		}
782
783
		if( retval )
784
		{
785
			/* break out on errors */
786
			printk(__FUNCTION__ " got retval %d %d %d",
787
			       retval, this_read, partial );
788
			if( retval == USB_ST_DATAOVERRUN && retries < 5 )
789
			{
790
				retries++;
791
				interruptible_sleep_on_timeout
792
					( &dev->wait_q, HZ );
793
				continue;
794
			}
795
			else
796
			{
797
				return -EIO;
798
			}
799
		} else {
800
			retries = 0;
801
		}
802
803
		if( partial )
804
		{
805
			this_read = partial;
806
		}
807
808
		/* All packets i've seen start with b1 60. If no data
809
		 * was actually available, the transceiver still gives
810
		 * this byte pair back. We only care about actual
811
		 * codes, so we can safely ignore these 2 byte reads
812
		 */
813
		if( this_read > 2 )
814
		{
815
#if 0
816
			printk( __FUNCTION__ " read %d bytes partial %d goodbytes %d writeidx %d\n",
817
				this_read, partial, goodbytes, writeindex );
818
#endif
819
			sequential_empty_reads = 0;
820
			/* copy from the input buffer to the capture buffer */
821
			for( i = 0; i < this_read; i++ )
822
			{
823
				if( (((unsigned char*)ibuf)[i] == 0xb1) ||
824
				    (ibuf[i] == 0x60) )
825
					;
826
				else
827
				{
828
					if( writeindex == sizeof(dev->usb_dbuffer) )
829
					{
830
						/* this can happen in
831
						 * repeats, where
832
						 * basically the bulk
833
						 * buffer is getting
834
						 * spammed and we
835
						 * aren't processing
836
						 * data fast enough
837
						 */
838
#if 1
839
						dev->usb_valid_bytes_in_bulk_buffer = this_read - i;
840
						memcpy( ibuf, &( ibuf[i] ),
841
							dev->usb_valid_bytes_in_bulk_buffer );
842
#endif
843
						break;
844
					}
845
					dev->usb_dbuffer[writeindex++] = ibuf[i];
846
					goodbytes++;
847
848
					if( ibuf[i] == 0x7f )
849
					{
850
						terminators++;
851
852
						/* This is a bug - we should either get 10 or 15 */
853
						if( terminators > 15 )
854
						{
855
							dbg("bugbug - terminators %d at %d gb %d", terminators, i, goodbytes );
856
						} else
857
							dbg("terminator %d at %d gb %d", terminators, i, goodbytes );
858
						dbg("writeindex %d", writeindex);
859
					}
860
					else if( terminators )
861
					{
862
						if( ((unsigned char*)ibuf)[i] == 128 )
863
						{
864
							/* copy back any remainder and break out */
865
							dev->usb_valid_bytes_in_bulk_buffer = this_read - (i + 1);
866
							if( dev->usb_valid_bytes_in_bulk_buffer )
867
							{
868
								memcpy( ibuf, &( ibuf[i+1] ), dev->usb_valid_bytes_in_bulk_buffer );
869
							}
870
871
							count = 0;
872
							break;
873
						}
874
						if( terminators == 10 ||
875
						    terminators == 15 )
876
							dbg("post-termination data %d idx %d %d", ibuf[i], dev->usb_dcount, i);
877
					}
878
				}
879
			}
880
			dev->usb_dcount += goodbytes;
881
			count -= goodbytes;
882
		} else {
883
			sequential_empty_reads++;
884
885
			// assume no data
886
			if( dont_block && sequential_empty_reads == 5 )
887
				break;
888
889
			// Try to be nice to the usb bus by sleeping
890
			// for a bit here before going in to the next
891
			// read
892
			interruptible_sleep_on_timeout( &dev->wait_q, 1 );
893
		}
894
895
	}
896
	/* return the number of bytes available now */
897
	return dev->usb_dcount;
898
}
899
900
// layout of data, per Christoph Bartelmus
901
// The protocol is:
902
// 1 byte: -length of following packet
903
// the following bytes of the packet are:
904
// negative value:
905
//   -(number of time units) of pulse
906
// positive value:
907
//   (number of time units) of space
908
// one time unit is 50us
909
910
#define MCE_TIME_UNIT 50
911
912
// returns the number of bytes processed from the 'usb_data' array
913
static int msir_generate_mode2( struct usb_skel* dev, signed char* usb_data,
914
				int bytecount )
915
{
916
	int bytes_left_in_packet = 0;
917
	int pos = 0;
918
	int mode2count = 0;
919
	int last_was_pulse = 1;
920
	int last_pkt = 0;
921
	int split_pkt_size = 0;
922
	// XXX no bounds checking here
923
	int* mode2_data;
924
	int mode2_limit = sizeof( dev->mode2_data ) - dev->mode2_count;
925
926
	// If data exists in the buffer, we have to point to the last
927
	// item there so we can append consecutive pulse/space
928
	// ops. Otherwise, set last_was_pulse 1 (since the first byte
929
	// is a pulse, and we want to store in the first array
930
	// location
931
	if( dev->mode2_count == 0 )
932
	{
933
		mode2_data = &( dev->mode2_data[0] );
934
		last_was_pulse = (dev->mode2_once ? 1 : 0);
935
		mode2_data[0] = 0;
936
	}
937
	else
938
	{
939
		mode2_data = &( dev->mode2_data[dev->mode2_idx +
940
						dev->mode2_count - 1] );
941
		last_was_pulse = (mode2_data[0] & PULSE_BIT) ? 1 : 0;
942
	}
943
944
	while( pos < bytecount && !last_pkt &&
945
	       (mode2_limit > (dev->mode2_count + mode2count)) )
946
	{
947
		if( dev->mode2_partial_pkt_size )
948
		{
949
			bytes_left_in_packet = dev->mode2_partial_pkt_size;
950
			dev->mode2_partial_pkt_size = 0;
951
		}
952
		else {
953
			bytes_left_in_packet = 128 + usb_data[pos];
954
955
			// XXX out of sync? find the next packet
956
			// header, establish a distance, and fix the
957
			// packet size
958
			if( bytes_left_in_packet > 4 )
959
			{
960
				int i;
961
				for( i = pos + 1; i < pos + 4; i++ )
962
				{
963
					if( (int)(128 + usb_data[i]) <= 4 )
964
					{
965
						bytes_left_in_packet = i - pos;
966
						break;
967
					}
968
				}
969
			}
970
			else
971
			{
972
				// otherwise, increment past the header
973
				pos++;
974
			}
975
		}
976
977
		// special case where we have a terminator at the
978
		// start but not at the end of this packet, indicating
979
		// potential repeat, or the packet is less than 4
980
		// bytes, indicating end also special case a split
981
		// starting packet
982
		if( pos > 1 && bytes_left_in_packet < 4 )
983
		{
984
			// end
985
			last_pkt = 1;
986
		}
987
		else if( usb_data[pos] == 127 &&
988
			 usb_data[pos+bytes_left_in_packet-1] != 127 )
989
		{
990
			// the genius ir transciever is blending data
991
			// from the repeat events into a single
992
			// packet. how we handle this is by splitting
993
			// the packet (and truncating the packet size
994
			// value we read), then rewriting a new packet
995
			// header onto the outbound data.  it's
996
			// ultraghetto.
997
			while( usb_data[pos+bytes_left_in_packet-1] != 127 )
998
			{
999
				bytes_left_in_packet--;
1000
				split_pkt_size++;
1001
			}
1002
			// repeat code
1003
			last_pkt = 2;
1004
		}
1005
		while( bytes_left_in_packet && pos < bytecount )
1006
		{
1007
			int keycode = usb_data[pos];
1008
			int pulse = 0;
1009
1010
			pos++;
1011
			if( keycode < 0 )
1012
			{
1013
				pulse = 1;
1014
				keycode += 128;
1015
			}
1016
			keycode *= MCE_TIME_UNIT;
1017
1018
			// on a state change, increment the position
1019
			// for the output buffer and initialize the
1020
			// current spot to 0; otherwise we need to
1021
			// concatenate pulse/gap values for lirc to be
1022
			// happy
1023
			if( pulse != last_was_pulse &&
1024
			    (mode2count || mode2_data[mode2count]))
1025
			{
1026
				if( dev->last_was_repeat_gap )
1027
				{
1028
					//printk( __FUNCTION__ " transition with lwrg set lastval %d idx1 %d idx2 %d\n",
1029
					//  mode2_data[mode2count],mode2count, dev->mode2_count+dev->mode2_idx-1 );
1030
				}
1031
				mode2count++;
1032
				mode2_data[mode2count] = 0;
1033
			}
1034
1035
			mode2_data[mode2count] += keycode;
1036
1037
			// Or in the pulse bit, and map all gap
1038
			// lengths to a fixed value; this makes lirc
1039
			// happy, sort of.
1040
			if( pulse ) {
1041
				mode2_data[mode2count] |= PULSE_BIT;
1042
				dev->last_was_repeat_gap = 0;
1043
			}
1044
1045
			last_was_pulse = pulse;
1046
			bytes_left_in_packet--;
1047
		}
1048
	}
1049
1050
	// If the last value in the data array is a repeat gap, set
1051
	// the last_was_repeat_gap flag
1052
	if( mode2_data[mode2count] > 20000 && mode2_data[mode2count] < 70000 )
1053
	{
1054
		// printk(__FUNCTION__ " setting lwrg for val %d idx1 %d idx2 %d\n",
1055
		//    mode2_data[mode2count], mode2count, dev->mode2_count+dev->mode2_idx-1 );
1056
		dev->last_was_repeat_gap = 1;
1057
	} else {
1058
		dev->last_was_repeat_gap = 0;
1059
	}
1060
1061
	// this is a bit tricky; we need to change to a counter, but
1062
	// if we already had data in dev->mode2_data, then byte 0
1063
	// actually was pre-existing data and shouldn't be counted
1064
	if( mode2count && !dev->mode2_count )
1065
	{
1066
		mode2count++;
1067
		//        printk(__FUNCTION__ " mode2count++ to %d\n", mode2count);
1068
	}
1069
1070
	// never lie about how much output we have
1071
	dev->mode2_count += mode2count;
1072
1073
	if( last_pkt == 1 )
1074
	{
1075
		return bytecount;
1076
	}
1077
	else
1078
	{
1079
		//  note the partial pkt size, and make sure we only claim
1080
		//  the bytes we processed
1081
		if( last_pkt == 2 )
1082
		{
1083
			dev->mode2_partial_pkt_size = split_pkt_size;
1084
		}
1085
#if 1
1086
		// XXX this i am not sure about; it seems like this should be required, but it
1087
		// isn't, and seems to cause problems
1088
		else
1089
		{
1090
			dev->mode2_partial_pkt_size = bytes_left_in_packet;
1091
		}
1092
#endif
1093
		return pos;
1094
	}
1095
}
1096
1097
static ssize_t mceusb_read( struct file* file, char* buffer,
1098
			    size_t count, loff_t* ppos)
1099
{
1100
	char _data_buffer[128];
1101
	struct usb_skel* dev;
1102
	int read_count;
1103
	int bytes_copied = 0;
1104
1105
	dev = (struct usb_skel*) file->private_data;
1106
1107
	if( (count % 4) != 0 )
1108
	{
1109
		return -EINVAL;
1110
	}
1111
1112
	down( &dev->sem );
1113
1114
	/* verify that the device wasn't unplugged */
1115
	if (dev->udev == NULL) {
1116
		up( &dev->sem );
1117
		return -ENODEV;
1118
	}
1119
1120
	dbg(__FUNCTION__ " (1) calling msir_copy_mode2 with %d", count);
1121
	bytes_copied = 4 * msir_copy_mode2( dev, (int*)buffer, count >> 2 );
1122
	if( bytes_copied == count )
1123
	{
1124
		up( &dev->sem );
1125
		return count;
1126
	}
1127
1128
	/* we didn't get enough mode2 data. the process now is a bit complex
1129
	 * 1. see if we have data read from the usb device that hasn't
1130
	 *    been converted to mode2; if so, convert that, and try to
1131
	 *    copy that out
1132
	 * 2. otherwise, go ahead and read more, then convert that, then copy
1133
	 */
1134
1135
	if( dev->usb_dcount )
1136
	{
1137
		read_count = msir_read_from_buffer( dev, _data_buffer, 128 );
1138
		read_count = msir_generate_mode2
1139
			( dev, (signed char*)_data_buffer, read_count );
1140
		msir_mark_as_read( dev, read_count );
1141
		bytes_copied += (4 * msir_copy_mode2
1142
				 ( dev, (int*)(buffer + bytes_copied),
1143
				   (count-bytes_copied) >> 2 ));
1144
	}
1145
1146
	if( bytes_copied == count )
1147
	{
1148
		up( &dev->sem );
1149
		return count;
1150
	}
1151
1152
	/* read more data in a loop until we get enough */
1153
	while( bytes_copied < count )
1154
	{
1155
		read_count = msir_fetch_more_data
1156
			( dev, (file->f_flags & O_NONBLOCK ? 1 : 0) );
1157
1158
		if( read_count <= 0 )
1159
		{
1160
			up( &dev->sem );
1161
			return (read_count ? read_count : -EWOULDBLOCK);
1162
		}
1163
1164
		read_count = msir_read_from_buffer( dev, _data_buffer, 128 );
1165
		read_count = msir_generate_mode2
1166
			( dev, (signed char*)_data_buffer, read_count );
1167
		msir_mark_as_read( dev, read_count );
1168
1169
		bytes_copied += (4 * msir_copy_mode2
1170
				 ( dev, (int*)(buffer + bytes_copied),
1171
				   (count-bytes_copied) >> 2 ));
1172
	}
1173
1174
	up( &dev->sem );
1175
	return bytes_copied;
1176
}
1177
1178
/**
1179
 * mceusb_poll
1180
 */
1181
static unsigned int mceusb_poll(struct file* file, poll_table* wait)
1182
{
1183
	struct usb_skel* dev;
1184
	int data;
1185
	dev = (struct usb_skel*)file->private_data;
1186
1187
	// So this is a crummy poll. Unfortunately all the lirc tools
1188
	// assume your hardware is interrupt driven. Instead, we have
1189
	// to actually read here to see whether or not there is data
1190
	// (unless we have a key saved up - unlikely )
1191
1192
	//    if( dev->usb_dcount || dev->mode2_count )
1193
	if( msir_available_data( dev ) || msir_available_mode2( dev ) )
1194
	{
1195
		return POLLIN | POLLRDNORM;
1196
	}
1197
	else {
1198
		down( &dev->sem );
1199
		data = msir_fetch_more_data( dev, 1 );
1200
		up( &dev->sem );
1201
1202
		if( data )
1203
			return POLLIN | POLLRDNORM;
1204
	}
1205
1206
	return 0;
1207
}
1208
1209
/**
1210
 *	mceusb_write
1211
 */
1212
static ssize_t mceusb_write (struct file *file, const char *buffer, size_t count, loff_t *ppos)
1213
{
1214
	struct usb_skel *dev;
1215
	ssize_t bytes_written = 0;
1216
	int retval = 0;
1217
1218
	dev = (struct usb_skel *)file->private_data;
1219
1220
	dbg(__FUNCTION__ " - minor %d, count = %d", dev->minor, count);
1221
1222
	/* lock this object */
1223
	down (&dev->sem);
1224
1225
	/* verify that the device wasn't unplugged */
1226
	if (dev->udev == NULL) {
1227
		retval = -ENODEV;
1228
		goto exit;
1229
	}
1230
1231
	/* verify that we actually have some data to write */
1232
	if (count == 0) {
1233
		dbg(__FUNCTION__ " - write request of 0 bytes");
1234
		goto exit;
1235
	}
1236
1237
	/* see if we are already in the middle of a write */
1238
	if (dev->write_urb->status == -EINPROGRESS) {
1239
		dbg (__FUNCTION__ " - already writing");
1240
		goto exit;
1241
	}
1242
1243
	/* we can only write as much as 1 urb will hold */
1244
	bytes_written = (count > dev->bulk_out_size) ?
1245
		dev->bulk_out_size : count;
1246
1247
	/* copy the data from userspace into our urb */
1248
	if (copy_from_user(dev->write_urb->transfer_buffer, buffer,
1249
			   bytes_written)) {
1250
		retval = -EFAULT;
1251
		goto exit;
1252
	}
1253
1254
	usb_mceusb_debug_data (__FUNCTION__, bytes_written,
1255
			       dev->write_urb->transfer_buffer);
1256
1257
	/* set up our urb */
1258
	FILL_BULK_URB(dev->write_urb, dev->udev,
1259
		      usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
1260
		      dev->write_urb->transfer_buffer, bytes_written,
1261
		      mceusb_write_bulk_callback, dev);
1262
1263
	/* send the data out the bulk port */
1264
	retval = usb_submit_urb(dev->write_urb);
1265
	if (retval) {
1266
		err(__FUNCTION__ " - failed submitting write urb, error %d",
1267
		    retval);
1268
	} else {
1269
		retval = bytes_written;
1270
	}
1271
1272
  exit:
1273
	/* unlock the device */
1274
	up (&dev->sem);
1275
1276
	return retval;
1277
}
1278
1279
/*
1280
 *	mceusb_write_bulk_callback
1281
 */
1282
1283
static void mceusb_write_bulk_callback (struct urb *urb)
1284
{
1285
	struct usb_skel *dev = (struct usb_skel *)urb->context;
1286
1287
	dbg(__FUNCTION__ " - minor %d", dev->minor);
1288
1289
	if ((urb->status != -ENOENT) &&
1290
	    (urb->status != -ECONNRESET)) {
1291
		dbg(__FUNCTION__ " - nonzero write bulk status received: %d",
1292
		    urb->status);
1293
		return;
1294
	}
1295
1296
	return;
1297
}
1298
1299
/**
1300
 *	mceusb_probe
1301
 *
1302
 *	Called by the usb core when a new device is connected that it
1303
 *	thinks this driver might be interested in.
1304
 */
1305
static void * mceusb_probe(struct usb_device *udev, unsigned int ifnum,
1306
			   const struct usb_device_id *id)
1307
{
1308
	struct usb_skel *dev = NULL;
1309
	struct usb_interface *interface;
1310
	struct usb_interface_descriptor *iface_desc;
1311
	struct usb_endpoint_descriptor *endpoint;
1312
	struct lirc_plugin* plugin;
1313
	struct lirc_buffer* rbuf;
1314
1315
	int minor;
1316
	int buffer_size;
1317
	int i;
1318
	char name[10];
1319
1320
1321
	/* See if the device offered us matches what we can accept */
1322
	if ((udev->descriptor.idVendor != USB_MCEUSB_VENDOR_ID) ||
1323
	    (udev->descriptor.idProduct != USB_MCEUSB_PRODUCT_ID)) {
1324
		return NULL;
1325
	}
1326
1327
	/* select a "subminor" number (part of a minor number) */
1328
	down (&minor_table_mutex);
1329
	for (minor = 0; minor < MAX_DEVICES; ++minor) {
1330
		if (minor_table[minor] == NULL)
1331
			break;
1332
	}
1333
	if (minor >= MAX_DEVICES) {
1334
		info ("Too many devices plugged in, can not handle this device.");
1335
		goto exit;
1336
	}
1337
1338
	/* allocate memory for our device state and intialize it */
1339
	dev = kmalloc (sizeof(struct usb_skel), GFP_KERNEL);
1340
	if (dev == NULL) {
1341
		err ("Out of memory");
1342
		goto exit;
1343
	}
1344
	minor_table[minor] = dev;
1345
1346
	interface = &udev->actconfig->interface[ifnum];
1347
1348
	init_MUTEX (&dev->sem);
1349
	dev->udev = udev;
1350
	dev->interface = interface;
1351
	dev->minor = minor;
1352
1353
	/* set up the endpoint information */
1354
	/* check out the endpoints */
1355
	iface_desc = &interface->altsetting[0];
1356
	for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
1357
		endpoint = &iface_desc->endpoint[i];
1358
1359
		if ((endpoint->bEndpointAddress & 0x80) &&
1360
		    ((endpoint->bmAttributes & 3) == 0x02)) {
1361
			/* we found a bulk in endpoint */
1362
			buffer_size = endpoint->wMaxPacketSize;
1363
			dev->bulk_in_size = buffer_size;
1364
			dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
1365
			dev->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
1366
			if (!dev->bulk_in_buffer) {
1367
				err("Couldn't allocate bulk_in_buffer");
1368
				goto error;
1369
			}
1370
		}
1371
1372
		if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
1373
		    ((endpoint->bmAttributes & 3) == 0x02)) {
1374
			/* we found a bulk out endpoint */
1375
			dev->write_urb = usb_alloc_urb(0);
1376
			if (!dev->write_urb) {
1377
				err("No free urbs available");
1378
				goto error;
1379
			}
1380
			buffer_size = endpoint->wMaxPacketSize;
1381
			dev->bulk_out_size = buffer_size;
1382
			dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
1383
			dev->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
1384
			if (!dev->bulk_out_buffer) {
1385
				err("Couldn't allocate bulk_out_buffer");
1386
				goto error;
1387
			}
1388
			FILL_BULK_URB(dev->write_urb, udev,
1389
				      usb_sndbulkpipe
1390
				      (udev, endpoint->bEndpointAddress),
1391
				      dev->bulk_out_buffer, buffer_size,
1392
				      mceusb_write_bulk_callback, dev);
1393
		}
1394
	}
1395
1396
	memset( dev->mode2_data, 0, sizeof( dev->mode2_data ) );
1397
	dev->mode2_idx = 0;
1398
	dev->mode2_count = 0;
1399
	dev->mode2_partial_pkt_size = 0;
1400
	dev->mode2_once = 0;
1401
	dev->last_was_repeat_gap = 0;
1402
1403
	/* Set up our lirc plugin */
1404
	if(!(plugin = kmalloc(sizeof(struct lirc_plugin), GFP_KERNEL))) {
1405
		err("out of memory");
1406
		goto error;
1407
	}
1408
	memset( plugin, 0, sizeof(struct lirc_plugin) );
1409
1410
	if(!(rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL))) {
1411
		err("out of memory");
1412
		kfree( plugin );
1413
		goto error;
1414
	}
1415
	/* the lirc_atiusb module doesn't memset rbuf here ... ? */
1416
	if( lirc_buffer_init( rbuf, sizeof(lirc_t),
1417
			      sizeof(struct lirc_buffer))) {
1418
		err("out of memory");
1419
		kfree( plugin );
1420
		kfree( rbuf );
1421
		goto error;
1422
	}
1423
	strcpy(plugin->name, "lirc_mce ");
1424
	plugin->minor       = minor;
1425
	plugin->code_length = sizeof(lirc_t);
1426
	plugin->features    = LIRC_CAN_REC_MODE2; // | LIRC_CAN_SEND_MODE2;
1427
	plugin->data        = dev;
1428
	plugin->rbuf        = rbuf;
1429
	plugin->ioctl       = NULL;
1430
	plugin->set_use_inc = &set_use_inc;
1431
	plugin->set_use_dec = &set_use_dec;
1432
	plugin->fops        = &mceusb_fops;
1433
	if( lirc_register_plugin( plugin ) < 0 )
1434
	{
1435
		kfree( plugin );
1436
		lirc_buffer_free( rbuf );
1437
		kfree( rbuf );
1438
		goto error;
1439
	}
1440
	dev->plugin = plugin;
1441
1442
	mceusb_setup( udev );
1443
1444
	/* let the user know what node this device is now attached to */
1445
	info ("USB Microsoft IR Transceiver device now attached to msir%d",
1446
	      dev->minor);
1447
	goto exit;
1448
1449
  error:
1450
	mceusb_delete (dev);
1451
	dev = NULL;
1452
1453
  exit:
1454
	up (&minor_table_mutex);
1455
	return dev;
1456
}
1457
1458
/**
1459
 *	mceusb_disconnect
1460
 *
1461
 *	Called by the usb core when the device is removed from the system.
1462
 */
1463
static void mceusb_disconnect(struct usb_device *udev, void *ptr)
1464
{
1465
	int minor;
1466
1467
	down (&minor_table_mutex);
1468
	down (&dev->sem);
1469
1470
	minor = dev->minor;
1471
1472
	/* unhook lirc things */
1473
	lirc_unregister_plugin( dev->minor );
1474
	lirc_buffer_free( dev->plugin->rbuf );
1475
	kfree( dev->plugin->rbuf );
1476
	kfree( dev->plugin );
1477
1478
	/* if the device is not opened, then we clean up right now */
1479
	if (!dev->open_count) {
1480
		up (&dev->sem);
1481
		mceusb_delete (dev);
1482
	} else {
1483
		dev->udev = NULL;
1484
		up (&dev->sem);
1485
	}
1486
1487
	info("USB Skeleton #%d now disconnected", minor);
1488
	up (&minor_table_mutex);
1489
	return NULL;
1490
}
1491
1492
1493
1494
/**
1495
 *	usb_mceusb_init
1496
 */
1497
static int __init usb_mceusb_init(void)
1498
{
1499
	int result;
1500
1501
	/* register this driver with the USB subsystem */
1502
	result = usb_register(&mceusb_driver);
1503
	if (result < 0) {
1504
		err("usb_register failed for the "__FILE__" driver. Error number %d",
1505
		    result);
1506
		return -1;
1507
	}
1508
1509
	info(DRIVER_DESC " " DRIVER_VERSION);
1510
	return 0;
1511
}
1512
1513
1514
/**
1515
 *	usb_mceusb_exit
1516
 */
1517
static void __exit usb_mceusb_exit(void)
1518
{
1519
	/* deregister this driver with the USB subsystem */
1520
	usb_deregister(&mceusb_driver);
1521
}
1522
1523
1524
module_init (usb_mceusb_init);
1525
module_exit (usb_mceusb_exit);
1526
1527
MODULE_AUTHOR(DRIVER_AUTHOR);
1528
MODULE_DESCRIPTION(DRIVER_DESC);
1529
MODULE_LICENSE("GPL");
1530
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_parallel.c (+743 lines)
Line 0 Link Here
1
/*      $Id: lirc_parallel.c,v 5.18 2002/11/19 20:22:07 ranty Exp $      */
2
3
/****************************************************************************
4
 ** lirc_parallel.c *********************************************************
5
 ****************************************************************************
6
 *
7
 * lirc_parallel - device driver for infra-red signal receiving and
8
 *                 transmitting unit built by the author
9
 *
10
 * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
11
 *
12
 *  This program is free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  This program is distributed in the hope that it will be useful,
18
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 *  GNU General Public License for more details.
21
 *
22
 *  You should have received a copy of the GNU General Public License
23
 *  along with this program; if not, write to the Free Software
24
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 *
26
 */
27
28
/***********************************************************************
29
 *************************       Includes        ***********************
30
 ***********************************************************************/
31
32
#include <linux/version.h>
33
34
#include <linux/config.h>
35
36
#include <linux/module.h>
37
#include <linux/sched.h>
38
#include <linux/errno.h>
39
#include <linux/signal.h>
40
#include <linux/config.h>
41
#include <linux/fs.h>
42
#include <linux/kernel.h>
43
#include <linux/ioport.h>
44
#include <linux/time.h>
45
#include <linux/mm.h>
46
#include <linux/delay.h>
47
#include <linux/init.h>
48
49
#include <asm/io.h>
50
#include <asm/signal.h>
51
#include <asm/irq.h>
52
#include <asm/system.h>
53
54
#include <asm/uaccess.h>
55
#include <linux/poll.h>
56
#include <linux/parport.h>
57
58
#include <linux/lirc.h>
59
#include "lirc_dev.h"
60
61
#include "lirc_parallel.h"
62
63
#define LIRC_DRIVER_NAME "lirc_parallel"
64
65
/***********************************************************************
66
 *************************   Globale Variablen   ***********************
67
 ***********************************************************************/
68
69
unsigned int irq = CONFIG_LIRC_IRQ_PARALLEL;
70
unsigned int io = CONFIG_LIRC_PORT_PARALLEL;
71
#ifdef CONFIG_LIRC_TIMER
72
unsigned int timer = 0;
73
unsigned int default_timer = CONFIG_LIRC_TIMER;
74
#endif
75
76
#define WBUF_SIZE (256)
77
#define RBUF_SIZE (256) /* this must be a power of 2 larger than 1 */
78
79
static lirc_t wbuf[WBUF_SIZE];
80
static lirc_t rbuf[RBUF_SIZE];
81
82
DECLARE_WAIT_QUEUE_HEAD(lirc_wait);
83
84
unsigned int rptr=0,wptr=0;
85
unsigned int lost_irqs=0;
86
int is_open=0;
87
88
struct parport *pport;
89
struct pardevice *ppdevice;
90
int is_claimed=0;
91
92
int pf(void *handle);
93
void kf(void *handle);
94
95
/***********************************************************************
96
 *************************   Interne Funktionen  ***********************
97
 ***********************************************************************/
98
99
unsigned int __inline__ in(int offset)
100
{
101
	switch(offset)
102
	{
103
	case LIRC_LP_BASE:
104
		return(parport_read_data(pport));
105
	case LIRC_LP_STATUS:
106
		return(parport_read_status(pport));
107
	case LIRC_LP_CONTROL:
108
		return(parport_read_control(pport));
109
	}
110
	return(0); /* make compiler happy */
111
}
112
113
void __inline__ out(int offset, int value)
114
{
115
	switch(offset)
116
	{
117
	case LIRC_LP_BASE:
118
		parport_write_data(pport,value);
119
		break;
120
	case LIRC_LP_CONTROL:
121
		parport_write_control(pport,value);
122
		break;
123
	case LIRC_LP_STATUS:
124
		printk(KERN_INFO "%s: attempt to write to status register\n",
125
		       LIRC_DRIVER_NAME);
126
		break;
127
	}
128
}
129
130
unsigned int __inline__ lirc_get_timer(void)
131
{
132
	return(in(LIRC_PORT_TIMER)&LIRC_PORT_TIMER_BIT);
133
}
134
135
unsigned int __inline__  lirc_get_signal(void)
136
{
137
	return(in(LIRC_PORT_SIGNAL)&LIRC_PORT_SIGNAL_BIT);
138
}
139
140
void __inline__ lirc_on(void)
141
{
142
	out(LIRC_PORT_DATA,LIRC_PORT_DATA_BIT);
143
}
144
145
void __inline__ lirc_off(void)
146
{
147
	out(LIRC_PORT_DATA,0);
148
}
149
150
unsigned int init_CONFIG_LIRC_TIMER(void)
151
{
152
	struct timeval tv,now;
153
	unsigned int level,newlevel,timeelapsed,newtimer;
154
	int count=0;
155
156
	do_gettimeofday(&tv);
157
	tv.tv_sec++;                     /* wait max. 1 sec. */
158
	level=lirc_get_timer();
159
	do
160
	{
161
		newlevel=lirc_get_timer();
162
		if(level==0 && newlevel!=0) count++;
163
		level=newlevel;
164
		do_gettimeofday(&now);
165
	}
166
	while(count<1000 && (now.tv_sec<tv.tv_sec
167
			     || (now.tv_sec==tv.tv_sec
168
				 && now.tv_usec<tv.tv_usec)));
169
170
	timeelapsed=((now.tv_sec+1-tv.tv_sec)*1000000
171
		     +(now.tv_usec-tv.tv_usec));
172
	if(count>=1000 && timeelapsed>0)
173
	{
174
		if(default_timer==0)                    /* autodetect timer */
175
		{
176
			newtimer=(1000000*count)/timeelapsed;
177
			printk(KERN_INFO "%s: %u Hz timer detected\n",
178
			       LIRC_DRIVER_NAME,newtimer);
179
			return(newtimer);
180
		}
181
		else
182
		{
183
			newtimer=(1000000*count)/timeelapsed;
184
			if(abs(newtimer-default_timer)>
185
			   default_timer/10) /* bad timer */
186
			{
187
				printk(KERN_NOTICE "%s: bad timer: %u Hz\n",
188
				       LIRC_DRIVER_NAME,newtimer);
189
				printk(KERN_NOTICE "%s: using default timer: "
190
				       "%u Hz\n",
191
				       LIRC_DRIVER_NAME,default_timer);
192
				return(default_timer);
193
			}
194
			else
195
			{
196
				printk(KERN_INFO "%s: %u Hz timer detected\n",
197
				       LIRC_DRIVER_NAME,newtimer);
198
				return(newtimer); /* use detected value */
199
			}
200
		}
201
	}
202
	else
203
	{
204
		printk(KERN_NOTICE "%s: no timer detected\n",LIRC_DRIVER_NAME);
205
		return(0);
206
	}
207
}
208
209
int lirc_claim(void)
210
{
211
	if(parport_claim(ppdevice)!=0)
212
	{
213
		printk(KERN_WARNING "%s: could not claim port\n",
214
		       LIRC_DRIVER_NAME);
215
		printk(KERN_WARNING "%s: waiting for port becoming available"
216
		       "\n",LIRC_DRIVER_NAME);
217
		if(parport_claim_or_block(ppdevice)<0)
218
		{
219
			printk(KERN_NOTICE "%s: could not claim port, giving"
220
			       " up\n",LIRC_DRIVER_NAME);
221
			return(0);
222
		}
223
	}
224
	out(LIRC_LP_CONTROL,LP_PSELECP|LP_PINITP);
225
	is_claimed=1;
226
	return(1);
227
}
228
229
/***********************************************************************
230
 *************************   interrupt handler  ************************
231
 ***********************************************************************/
232
233
static inline void rbuf_write(lirc_t signal)
234
{
235
	unsigned int nwptr;
236
237
	nwptr=(wptr+1) & (RBUF_SIZE-1);
238
	if(nwptr==rptr) /* no new signals will be accepted */
239
	{
240
		lost_irqs++;
241
		printk(KERN_NOTICE "%s: buffer overrun\n",LIRC_DRIVER_NAME);
242
		return;
243
	}
244
	rbuf[wptr]=signal;
245
	wptr=nwptr;
246
}
247
248
void irq_handler(int i,void *blah,struct pt_regs * regs)
249
{
250
	struct timeval tv;
251
	static struct timeval lasttv;
252
	static int init=0;
253
	long signal;
254
	lirc_t data;
255
	unsigned int level,newlevel;
256
	unsigned int timeout;
257
258
#ifdef CONFIG_MODULE_UNLOAD
259
	if(!module_refcount(THIS_MODULE))
260
		return;
261
#endif
262
263
	if(!is_claimed)
264
	{
265
		return;
266
	}
267
268
	/* disable interrupt */
269
	/*
270
	  disable_irq(irq);
271
	  out(LIRC_PORT_IRQ,in(LIRC_PORT_IRQ)&(~LP_PINTEN));
272
	*/
273
	if(in(1)&LP_PSELECD)
274
	{
275
		return;
276
	}
277
278
#ifdef CONFIG_LIRC_TIMER
279
	if(init)
280
	{
281
		do_gettimeofday(&tv);
282
283
	        signal=tv.tv_sec-lasttv.tv_sec;
284
		if(signal>15)
285
		{
286
			data=PULSE_MASK;  /* really long time */
287
		}
288
		else
289
		{
290
			data=(lirc_t) (signal*1000000+
291
				       tv.tv_usec-lasttv.tv_usec+
292
				       LIRC_SFH506_DELAY);
293
		};
294
295
		rbuf_write(data); /* space */
296
	}
297
	else
298
	{
299
		if(timer==0) /* wake up; we'll lose this signal
300
				but it will be garbage if the device
301
				is turned on anyway
302
			      */
303
		{
304
			timer=init_CONFIG_LIRC_TIMER();
305
			/* enable_irq(irq); */
306
			return;
307
		}
308
		init=1;
309
	}
310
311
	timeout=timer/10;           /* timeout after 1/10 sec. */
312
	signal=1;
313
	level=lirc_get_timer();
314
	do{
315
		newlevel=lirc_get_timer();
316
		if(level==0 && newlevel!=0) signal++;
317
		level=newlevel;
318
319
		/* giving up */
320
		if(signal>timeout || (in(1)&LP_PSELECD))
321
		{
322
			signal=0;
323
			printk(KERN_NOTICE "%s: timeout\n",LIRC_DRIVER_NAME);
324
			break;
325
		}
326
	}
327
	while(lirc_get_signal());
328
	if(signal!=0)
329
	{
330
		/* ajust value to usecs */
331
		signal=(long) (((unsigned long long) signal)*1000000)/timer;
332
333
		if(signal>LIRC_SFH506_DELAY)
334
		{
335
			data=signal-LIRC_SFH506_DELAY;
336
		}
337
		else
338
		{
339
			data=1;
340
		}
341
		rbuf_write(PULSE_BIT|data); /* pulse */
342
	}
343
	do_gettimeofday(&lasttv);
344
#else
345
	/* add your code here */
346
#endif
347
348
	wake_up_interruptible(&lirc_wait);
349
350
	/* enable interrupt */
351
	/*
352
	  enable_irq(irq);
353
	  out(LIRC_PORT_IRQ,in(LIRC_PORT_IRQ)|LP_PINTEN);
354
	*/
355
}
356
357
/***********************************************************************
358
 **************************   file_operations   ************************
359
 ***********************************************************************/
360
361
static loff_t lirc_lseek(struct file *filep,loff_t offset,int orig)
362
{
363
	return(-ESPIPE);
364
}
365
366
static ssize_t lirc_read(struct file *filep,char *buf,size_t n,loff_t *ppos)
367
{
368
	int result;
369
	int count=0;
370
	DECLARE_WAITQUEUE(wait, current);
371
372
	if(n%sizeof(lirc_t)) return(-EINVAL);
373
374
	result=verify_area(VERIFY_WRITE,buf,n);
375
	if(result) return(result);
376
377
	add_wait_queue(&lirc_wait,&wait);
378
	current->state=TASK_INTERRUPTIBLE;
379
	while(count<n)
380
	{
381
		if(rptr!=wptr)
382
		{
383
			copy_to_user(buf+count,(char *) &rbuf[rptr],
384
				     sizeof(lirc_t));
385
			rptr=(rptr+1)&(RBUF_SIZE-1);
386
			count+=sizeof(lirc_t);
387
		}
388
		else
389
		{
390
			if(filep->f_flags & O_NONBLOCK)
391
			{
392
				result=-EAGAIN;
393
				break;
394
			}
395
			if (signal_pending(current))
396
			{
397
				result=-ERESTARTSYS;
398
				break;
399
			}
400
			schedule();
401
			current->state=TASK_INTERRUPTIBLE;
402
		}
403
	}
404
	remove_wait_queue(&lirc_wait,&wait);
405
	current->state=TASK_RUNNING;
406
	return(count ? count:result);
407
}
408
409
static ssize_t lirc_write(struct file *filep,const char *buf,size_t n,
410
			  loff_t *ppos)
411
{
412
	int result,count;
413
	unsigned int i;
414
	unsigned int level,newlevel;
415
	unsigned long flags;
416
	lirc_t counttimer;
417
418
	if(!is_claimed)
419
	{
420
		return(-EBUSY);
421
	}
422
	if(n%sizeof(lirc_t)) return(-EINVAL);
423
	result=verify_area(VERIFY_READ,buf,n);
424
	if(result) return(result);
425
426
	count=n/sizeof(lirc_t);
427
428
	if(count>WBUF_SIZE || count%2==0) return(-EINVAL);
429
430
	copy_from_user(wbuf,buf,n);
431
432
#ifdef CONFIG_LIRC_TIMER
433
	if(timer==0) /* try again if device is ready */
434
	{
435
		timer=init_CONFIG_LIRC_TIMER();
436
		if(timer==0) return(-EIO);
437
	}
438
439
	/* ajust values from usecs */
440
	for(i=0;i<count;i++)
441
	{
442
		wbuf[i]=(lirc_t) (((double) wbuf[i])*timer/1000000);
443
	}
444
445
	local_save_flags(flags);local_irq_disable();
446
	i=0;
447
	while(i<count)
448
	{
449
		level=lirc_get_timer();
450
		counttimer=0;
451
		lirc_on();
452
		do
453
		{
454
			newlevel=lirc_get_timer();
455
			if(level==0 && newlevel!=0) counttimer++;
456
			level=newlevel;
457
			if(in(1)&LP_PSELECD)
458
			{
459
				lirc_off();
460
				local_irq_restore(flags); /* sti(); */
461
				return(-EIO);
462
			}
463
		}
464
		while(counttimer<wbuf[i]);i++;
465
466
		lirc_off();
467
		if(i==count) break;
468
		counttimer=0;
469
		do
470
		{
471
			newlevel=lirc_get_timer();
472
			if(level==0 && newlevel!=0) counttimer++;
473
			level=newlevel;
474
			if(in(1)&LP_PSELECD)
475
			{
476
				local_irq_restore(flags); /* sti(); */
477
				return(-EIO);
478
			}
479
		}
480
		while(counttimer<wbuf[i]);i++;
481
	}
482
	local_irq_restore(flags); /* sti(); */
483
#else
484
	/*
485
	   place code that handles write
486
	   without extarnal timer here
487
	*/
488
#endif
489
	return(n);
490
}
491
492
static unsigned int lirc_poll(struct file *file, poll_table * wait)
493
{
494
	poll_wait(file, &lirc_wait,wait);
495
	if (rptr!=wptr)
496
		return(POLLIN|POLLRDNORM);
497
	return(0);
498
}
499
500
static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
501
		      unsigned long arg)
502
{
503
        int result;
504
	unsigned long features=LIRC_CAN_SEND_PULSE|LIRC_CAN_REC_MODE2,mode;
505
506
	switch(cmd)
507
	{
508
	case LIRC_GET_FEATURES:
509
		result=put_user(features,(unsigned long *) arg);
510
		if(result) return(result);
511
		break;
512
	case LIRC_GET_SEND_MODE:
513
		result=put_user(LIRC_MODE_PULSE,(unsigned long *) arg);
514
		if(result) return(result);
515
		break;
516
	case LIRC_GET_REC_MODE:
517
		result=put_user(LIRC_MODE_MODE2,(unsigned long *) arg);
518
		if(result) return(result);
519
		break;
520
	case LIRC_SET_SEND_MODE:
521
		result=get_user(mode,(unsigned long *) arg);
522
		if(result) return(result);
523
		if(mode!=LIRC_MODE_PULSE) return(-EINVAL);
524
		break;
525
	case LIRC_SET_REC_MODE:
526
		result=get_user(mode,(unsigned long *) arg);
527
		if(result) return(result);
528
		if(mode!=LIRC_MODE_MODE2) return(-ENOSYS);
529
		break;
530
	default:
531
		return(-ENOIOCTLCMD);
532
	}
533
	return(0);
534
}
535
536
static int lirc_open(struct inode* node,struct file* filep)
537
{
538
#ifdef CONFIG_MODULE_UNLOAD
539
	if(module_refcount(THIS_MODULE))
540
	{
541
		return(-EBUSY);
542
	}
543
#endif
544
	if(!lirc_claim())
545
	{
546
		return(-EBUSY);
547
	}
548
	pport->ops->enable_irq(pport);
549
550
	/* init read ptr */
551
	rptr=wptr=0;
552
	lost_irqs=0;
553
554
	try_module_get(THIS_MODULE);
555
	is_open=1;
556
	return(0);
557
}
558
559
static int lirc_close(struct inode* node,struct file* filep)
560
{
561
	if(is_claimed)
562
	{
563
		is_claimed=0;
564
		parport_release(ppdevice);
565
	}
566
	is_open=0;
567
	module_put(THIS_MODULE);
568
	return(0);
569
}
570
571
static struct file_operations lirc_fops =
572
{
573
	llseek:  lirc_lseek,
574
	read:    lirc_read,
575
	write:   lirc_write,
576
	poll:    lirc_poll,
577
	ioctl:   lirc_ioctl,
578
	open:    lirc_open,
579
	release: lirc_close
580
};
581
582
static int set_use_inc(void* data)
583
{
584
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
585
       try_module_get(THIS_MODULE);
586
#endif
587
       return 0;
588
}
589
590
static void set_use_dec(void* data)
591
{
592
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
593
       module_put(THIS_MODULE);
594
#endif
595
}
596
static struct lirc_plugin plugin = {
597
       name:           LIRC_DRIVER_NAME,
598
       minor:          -1,
599
       code_length:    1,
600
       sample_rate:    0,
601
       data:           NULL,
602
       add_to_buf:     NULL,
603
       get_queue:      NULL,
604
       set_use_inc:    set_use_inc,
605
       set_use_dec:    set_use_dec,
606
       fops:           &lirc_fops,
607
};
608
609
static int __init lirc_parallel_init (void)
610
{
611
#ifndef LIRC26
612
	pport=parport_enumerate();
613
	while(pport!=NULL)
614
	{
615
		if(pport->base==io)
616
		{
617
			break;
618
		}
619
		pport=pport->next;
620
	}
621
#else
622
	pport=parport_find_base(io);
623
#endif
624
	if(pport==NULL)
625
	{
626
		printk(KERN_NOTICE "%s: no port at %x found\n",
627
		       LIRC_DRIVER_NAME,io);
628
		return(-ENXIO);
629
	}
630
	ppdevice=parport_register_device(pport,LIRC_DRIVER_NAME,
631
					 pf,kf,irq_handler,0,NULL);
632
	if(ppdevice==NULL)
633
	{
634
		printk(KERN_NOTICE "%s: parport_register_device() failed\n",
635
		       LIRC_DRIVER_NAME);
636
		return(-ENXIO);
637
	}
638
	if(parport_claim(ppdevice)!=0)
639
		goto skip_init;
640
	is_claimed=1;
641
	out(LIRC_LP_CONTROL,LP_PSELECP|LP_PINITP);
642
643
#ifdef CONFIG_LIRC_TIMER
644
#       ifdef DEBUG
645
	out(LIRC_PORT_DATA,LIRC_PORT_DATA_BIT);
646
#       endif
647
648
	timer=init_CONFIG_LIRC_TIMER();
649
650
#       if 0 	/* continue even if device is offline */
651
	if(timer==0)
652
	{
653
		is_claimed=0;
654
		parport_release(pport);
655
		parport_unregister_device(ppdevice);
656
		return(-EIO);
657
	}
658
659
#       endif
660
#       ifdef DEBUG
661
	out(LIRC_PORT_DATA,0);
662
#       endif
663
#endif
664
665
	is_claimed=0;
666
	parport_release(ppdevice);
667
 skip_init:
668
	if ((plugin.minor = lirc_register_plugin(&plugin)) < 0)
669
	{
670
		printk(KERN_NOTICE "%s: register_chrdev() failed\n",LIRC_DRIVER_NAME);
671
		parport_unregister_device(ppdevice);
672
		return(-EIO);
673
	}
674
	printk(KERN_INFO "%s: installed using port 0x%04x irq %d\n",LIRC_DRIVER_NAME,io,irq);
675
	return(0);
676
}
677
678
static void __exit lirc_parallel_exit(void)
679
{
680
#ifdef CONFIG_MODULE_UNLOAD
681
	if(module_refcount(THIS_MODULE)) return;
682
#endif
683
	parport_unregister_device(ppdevice);
684
	lirc_unregister_plugin(plugin.minor);
685
}
686
687
static struct timer_list poll_timer;
688
static void poll_state(unsigned long ignored);
689
690
static void poll_state(unsigned long ignored)
691
{
692
	printk(KERN_NOTICE "%s: time\n",
693
	       LIRC_DRIVER_NAME);
694
	del_timer(&poll_timer);
695
	if(is_claimed)
696
		return;
697
	kf(NULL);
698
	if(!is_claimed)
699
	{
700
		printk(KERN_NOTICE "%s: could not claim port, giving up\n",
701
		       LIRC_DRIVER_NAME);
702
		init_timer(&poll_timer);
703
		poll_timer.expires=jiffies+HZ;
704
		poll_timer.data=(unsigned long) current;
705
		poll_timer.function=poll_state;
706
		add_timer(&poll_timer);
707
	}
708
}
709
710
int pf(void *handle)
711
{
712
	pport->ops->disable_irq(pport);
713
	is_claimed=0;
714
	return(0);
715
}
716
717
718
void kf(void *handle)
719
{
720
	if(!is_open)
721
		return;
722
	if(!lirc_claim())
723
		return;
724
	pport->ops->enable_irq(pport);
725
	/* this is a bit annoying when you actually print...*/
726
	/*
727
	printk(KERN_INFO "%s: reclaimed port\n",LIRC_DRIVER_NAME);
728
	*/
729
}
730
731
MODULE_AUTHOR("Christoph Bartelmus");
732
MODULE_DESCRIPTION("Infrared receiver driver for parallel ports.");
733
MODULE_LICENSE("GPL");
734
735
MODULE_PARM(io, "i");
736
MODULE_PARM_DESC(io, "I/O address base (0x3bc, 0x378 or 0x278)");
737
738
MODULE_PARM(irq, "i");
739
MODULE_PARM_DESC(irq, "Interrupt (7 or 5)");
740
741
module_init(lirc_parallel_init);
742
module_exit(lirc_parallel_exit);
743
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_parallel.h (+24 lines)
Line 0 Link Here
1
/*      $Id: lirc_parallel.h,v 5.1 1999/07/21 18:23:37 columbus Exp $      */
2
3
#ifndef _LIRC_PARALLEL_H
4
#define _LIRC_PARALLEL_H
5
6
#include <linux/lp.h>
7
8
#define LIRC_PORT_LEN 3
9
10
#define LIRC_LP_BASE    0
11
#define LIRC_LP_STATUS  1
12
#define LIRC_LP_CONTROL 2
13
14
#define LIRC_PORT_DATA           LIRC_LP_BASE    /* base */
15
#define LIRC_PORT_DATA_BIT               0x01    /* 1st bit */
16
#define LIRC_PORT_TIMER        LIRC_LP_STATUS    /* status port */
17
#define LIRC_PORT_TIMER_BIT          LP_PBUSY    /* busy signal */
18
#define LIRC_PORT_SIGNAL       LIRC_LP_STATUS    /* status port */
19
#define LIRC_PORT_SIGNAL_BIT          LP_PACK    /* ack signal */
20
#define LIRC_PORT_IRQ         LIRC_LP_CONTROL    /* control port */
21
22
#define LIRC_SFH506_DELAY 0             /* delay t_phl in usecs */
23
24
#endif
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_serial.c (+1056 lines)
Line 0 Link Here
1
/*      $Id: lirc_serial.c,v 5.49 2004/01/12 10:21:12 lirc Exp $      */
2
3
/****************************************************************************
4
 ** lirc_serial.c ***********************************************************
5
 ****************************************************************************
6
 *
7
 * lirc_serial - Device driver that records pulse- and pause-lengths
8
 *               (space-lengths) between DDCD event on a serial port.
9
 *
10
 * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
11
 * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
12
 * Copyright (C) 1998 Ben Pfaff <blp@gnu.org>
13
 * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
14
 *
15
 *  This program is free software; you can redistribute it and/or modify
16
 *  it under the terms of the GNU General Public License as published by
17
 *  the Free Software Foundation; either version 2 of the License, or
18
 *  (at your option) any later version.
19
 *
20
 *  This program is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  You should have received a copy of the GNU General Public License
26
 *  along with this program; if not, write to the Free Software
27
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 *
29
 */
30
31
/* Steve's changes to improve transmission fidelity:
32
     - for systems with the rdtsc instruction and the clock counter, a
33
       send_pule that times the pulses directly using the counter.
34
       This means that the CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY fudge is
35
       not needed. Measurement shows very stable waveform, even where
36
       PCI activity slows the access to the UART, which trips up other
37
       versions.
38
     - For other system, non-integer-microsecond pulse/space lengths,
39
       done using fixed point binary. So, much more accurate carrier
40
       frequency.
41
     - fine tuned transmitter latency, taking advantage of fractional
42
       microseconds in previous change
43
     - Fixed bug in the way transmitter latency was accounted for by
44
       tuning the pulse lengths down - the send_pulse routine ignored
45
       this overhead as it timed the overall pulse length - so the
46
       pulse frequency was right but overall pulse length was too
47
       long. Fixed by accounting for latency on each pulse/space
48
       iteration.
49
50
   Steve Davies <steve@daviesfam.org>  July 2001
51
52
   Flameeyes Patches Contribution
53
    - Ronald Wahl <ronald.wahl@informatik.tu-chemnitz.de> sent a patch to
54
      eliminate a deadlock on SMP systems.
55
    - Florian Steinel <Florian.Steinel@t-online.de> sent a patch to fix irq
56
      disabling by kernel.
57
    - Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz> sent a patch fixing
58
      one-shot use of lirc_serial.
59
*/
60
61
#include <linux/config.h>
62
#include <linux/module.h>
63
#include <linux/errno.h>
64
#include <linux/signal.h>
65
#include <linux/sched.h>
66
#include <linux/fs.h>
67
#include <linux/interrupt.h>
68
#include <linux/ioport.h>
69
#include <linux/kernel.h>
70
#include <linux/major.h>
71
#include <linux/serial_reg.h>
72
#include <linux/time.h>
73
#include <linux/string.h>
74
#include <linux/types.h>
75
#include <linux/wait.h>
76
#include <linux/mm.h>
77
#include <linux/delay.h>
78
#include <linux/poll.h>
79
#include <linux/smp_lock.h>
80
81
#include <asm/system.h>
82
#include <asm/segment.h>
83
#include <asm/io.h>
84
#include <asm/irq.h>
85
#include <asm/fcntl.h>
86
87
#include <linux/lirc.h>
88
#include "lirc_dev.h"
89
90
#if defined(rdtsc)
91
92
#define USE_RDTSC
93
#warning "Note: using rdtsc instruction"
94
#endif
95
96
struct lirc_serial
97
{
98
	int type;
99
	int signal_pin;
100
	int signal_pin_change;
101
	int on;
102
	int off;
103
	long (*send_pulse)(unsigned long length);
104
	void (*send_space)(long length);
105
	int features;
106
};
107
108
#define LIRC_HOMEBREW        0
109
#define LIRC_IRDEO           1
110
#define LIRC_IRDEO_REMOTE    2
111
#define LIRC_ANIMAX          3
112
#define LIRC_IGOR            4
113
114
#ifdef CONFIG_LIRC_SERIAL_IRDEO
115
int type=LIRC_IRDEO;
116
#elif defined(CONFIG_LIRC_SERIAL_IRDEO_REMOTE)
117
int type=LIRC_IRDEO_REMOTE;
118
#elif defined(CONFIG_LIRC_SERIAL_ANIMAX)
119
int type=LIRC_ANIMAX;
120
#elif defined(CONFIG_LIRC_SERIAL_IGOR)
121
int type=LIRC_IGOR;
122
#else
123
int type=LIRC_HOMEBREW;
124
#endif
125
126
#ifdef CONFIG_LIRC_SERIAL_SOFTCARRIER
127
int softcarrier=1;
128
#else
129
int softcarrier=0;
130
#endif
131
132
static int sense = -1;   /* -1 = auto, 0 = active high, 1 = active low */
133
134
static int io = CONFIG_LIRC_PORT_SERIAL;
135
136
static int irq = CONFIG_LIRC_IRQ_SERIAL;
137
138
static int debug = 0;
139
140
MODULE_PARM(type, "i");
141
MODULE_PARM_DESC(type, "Hardware type (0 = home-brew, 1 = IRdeo,"
142
		 " 2 = IRdeo Remote, 3 = AnimaX");
143
144
MODULE_PARM(io, "i");
145
MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
146
147
MODULE_PARM(irq, "i");
148
MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
149
150
MODULE_PARM(sense, "i");
151
MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
152
		 " (0 = active high, 1 = active low )");
153
154
MODULE_PARM(softcarrier, "i");
155
MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on)");
156
157
MODULE_PARM(debug,"i");
158
159
#define dprintk           if (debug) printk
160
161
#define LOGHEAD           "lirc_serial: "
162
163
/* forward declarations */
164
long send_pulse_irdeo(unsigned long length);
165
long send_pulse_homebrew(unsigned long length);
166
void send_space_irdeo(long length);
167
void send_space_homebrew(long length);
168
169
struct lirc_serial hardware[]=
170
{
171
	/* home-brew receiver/transmitter */
172
	{
173
		LIRC_HOMEBREW,
174
		UART_MSR_DCD,
175
		UART_MSR_DDCD,
176
		UART_MCR_RTS|UART_MCR_OUT2|UART_MCR_DTR,
177
		UART_MCR_RTS|UART_MCR_OUT2,
178
		send_pulse_homebrew,
179
		send_space_homebrew,
180
		(
181
#ifdef CONFIG_LIRC_SERIAL_TRANSMITTER
182
		 LIRC_CAN_SET_SEND_DUTY_CYCLE|
183
		 LIRC_CAN_SET_SEND_CARRIER|
184
		 LIRC_CAN_SEND_PULSE|
185
#endif
186
		 LIRC_CAN_REC_MODE2)
187
	},
188
189
	/* IRdeo classic */
190
	{
191
		LIRC_IRDEO,
192
		UART_MSR_DSR,
193
		UART_MSR_DDSR,
194
		UART_MCR_OUT2,
195
		UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
196
		send_pulse_irdeo,
197
		send_space_irdeo,
198
		(LIRC_CAN_SET_SEND_DUTY_CYCLE|
199
		 LIRC_CAN_SEND_PULSE|
200
		 LIRC_CAN_REC_MODE2)
201
	},
202
203
	/* IRdeo remote */
204
	{
205
		LIRC_IRDEO_REMOTE,
206
		UART_MSR_DSR,
207
		UART_MSR_DDSR,
208
		UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
209
		UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
210
		send_pulse_irdeo,
211
		send_space_irdeo,
212
		(LIRC_CAN_SET_SEND_DUTY_CYCLE|
213
		 LIRC_CAN_SEND_PULSE|
214
		 LIRC_CAN_REC_MODE2)
215
	},
216
217
	/* AnimaX */
218
	{
219
		LIRC_ANIMAX,
220
		UART_MSR_DCD,
221
		UART_MSR_DDCD,
222
		0,
223
		UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
224
		NULL,
225
		NULL,
226
		LIRC_CAN_REC_MODE2
227
	},
228
229
	/* home-brew receiver/transmitter (Igor Cesko's variation) */
230
	{
231
		LIRC_HOMEBREW,
232
		UART_MSR_DSR,
233
		UART_MSR_DDSR,
234
		UART_MCR_RTS|UART_MCR_OUT2|UART_MCR_DTR,
235
		UART_MCR_RTS|UART_MCR_OUT2,
236
		send_pulse_homebrew,
237
		send_space_homebrew,
238
		(
239
#ifdef CONFIG_LIRC_SERIAL_TRANSMITTER
240
		 LIRC_CAN_SET_SEND_DUTY_CYCLE|
241
		 LIRC_CAN_SET_SEND_CARRIER|
242
		 LIRC_CAN_SEND_PULSE|
243
#endif
244
		 LIRC_CAN_REC_MODE2)
245
	}
246
247
};
248
249
#define LIRC_DRIVER_NAME "lirc_serial"
250
251
#define RS_ISR_PASS_LIMIT 256
252
253
/* A long pulse code from a remote might take upto 300 bytes.  The
254
   daemon should read the bytes as soon as they are generated, so take
255
   the number of keys you think you can push before the daemon runs
256
   and multiply by 300.  The driver will warn you if you overrun this
257
   buffer.  If you have a slow computer or non-busmastering IDE disks,
258
   maybe you will need to increase this.  */
259
260
/* This MUST be a power of two!  It has to be larger than 1 as well. */
261
262
#define RBUF_LEN 256
263
#define WBUF_LEN 256
264
265
static struct timeval lasttv = {0, 0};
266
267
static spinlock_t lirc_lock = SPIN_LOCK_UNLOCKED;
268
269
static struct lirc_buffer rbuf;
270
271
static lirc_t wbuf[WBUF_LEN];
272
273
unsigned int freq = 38000;
274
unsigned int duty_cycle = 50;
275
276
/* Initialized in init_timing_params() */
277
unsigned long period = 0;
278
unsigned long pulse_width = 0;
279
unsigned long space_width = 0;
280
281
#if defined(__i386__)
282
/*
283
  From:
284
  Linux I/O port programming mini-HOWTO
285
  Author: Riku Saikkonen <Riku.Saikkonen@hut.fi>
286
  v, 28 December 1997
287
288
  [...]
289
  Actually, a port I/O instruction on most ports in the 0-0x3ff range
290
  takes almost exactly 1 microsecond, so if you're, for example, using
291
  the parallel port directly, just do additional inb()s from that port
292
  to delay.
293
  [...]
294
*/
295
/* transmitter latency 1.5625us 0x1.90 - this figure arrived at from
296
 * comment above plus trimming to match actual measured frequency.
297
 * This will be sensitive to cpu speed, though hopefully most of the 1.5us
298
 * is spent in the uart access.  Still - for reference test machine was a
299
 * 1.13GHz Athlon system - Steve
300
 */
301
302
/* changed from 400 to 450 as this works better on slower machines;
303
   faster machines will use the rdtsc code anyway */
304
305
#define CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY 450
306
307
#else
308
309
/* does anybody have information on other platforms ? */
310
/* 256 = 1<<8 */
311
#define CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY 256
312
313
#endif  /* __i386__ */
314
315
static inline unsigned int sinp(int offset)
316
{
317
	return inb(io + offset);
318
}
319
320
static inline void soutp(int offset, int value)
321
{
322
	outb(value, io + offset);
323
}
324
325
static inline void on(void)
326
{
327
	soutp(UART_MCR,hardware[type].on);
328
}
329
330
static inline void off(void)
331
{
332
	soutp(UART_MCR,hardware[type].off);
333
}
334
335
#ifndef MAX_UDELAY_MS
336
#define MAX_UDELAY_US 5000
337
#else
338
#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
339
#endif
340
341
static inline void safe_udelay(unsigned long usecs)
342
{
343
	while(usecs>MAX_UDELAY_US)
344
	{
345
		udelay(MAX_UDELAY_US);
346
		usecs-=MAX_UDELAY_US;
347
	}
348
	udelay(usecs);
349
}
350
351
#ifdef USE_RDTSC
352
/* This is an overflow/precision juggle, complicated in that we can't
353
   do long long divide in the kernel */
354
355
/* When we use the rdtsc instruction to measure clocks, we keep the
356
 * pulse and space widths as clock cycles.  As this is CPU speed
357
 * dependent, the widths must be calculated in init_port and ioctl
358
 * time
359
 */
360
361
/* So send_pulse can quickly convert microseconds to clocks */
362
unsigned long conv_us_to_clocks = 0;
363
364
static inline int init_timing_params(unsigned int new_duty_cycle,
365
		unsigned int new_freq)
366
{
367
	unsigned long long loops_per_sec,work;
368
369
	duty_cycle=new_duty_cycle;
370
	freq=new_freq;
371
372
	loops_per_sec=current_cpu_data.loops_per_jiffy;
373
	loops_per_sec*=HZ;
374
375
	/* How many clocks in a microsecond?, avoiding long long divide */
376
	work=loops_per_sec;
377
	work*=4295;  /* 4295 = 2^32 / 1e6 */
378
	conv_us_to_clocks=(work>>32);
379
380
	/* Carrier period in clocks, approach good up to 32GHz clock,
381
           gets carrier frequency within 8Hz */
382
	period=loops_per_sec>>3;
383
	period/=(freq>>3);
384
385
	/* Derive pulse and space from the period */
386
387
	pulse_width = period*duty_cycle/100;
388
	space_width = period - pulse_width;
389
	dprintk(LOGHEAD
390
	       ": in init_timing_params, freq=%d, duty_cycle=%d, "
391
	       "clk/jiffy=%ld, pulse=%ld, space=%ld, conv_us_to_clocks=%ld\n",
392
	       freq, duty_cycle, current_cpu_data.loops_per_jiffy,
393
	       pulse_width, space_width, conv_us_to_clocks);
394
	return 0;
395
}
396
#else /* ! USE_RDTSC */
397
static inline int init_timing_params(unsigned int new_duty_cycle,
398
		unsigned int new_freq)
399
{
400
/* period, pulse/space width are kept with 8 binary places -
401
 * IE multiplied by 256. */
402
	if(256*1000000L/new_freq*new_duty_cycle/100<=
403
	   CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY) return(-EINVAL);
404
	if(256*1000000L/new_freq*(100-new_duty_cycle)/100<=
405
	   CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY) return(-EINVAL);
406
	duty_cycle=new_duty_cycle;
407
	freq=new_freq;
408
	period=256*1000000L/freq;
409
	pulse_width=period*duty_cycle/100;
410
	space_width=period-pulse_width;
411
	dprintk(LOGHEAD
412
	       ": in init_timing_params, freq=%d pulse=%ld, "
413
	       "space=%ld\n", freq, pulse_width, space_width);
414
	return 0;
415
}
416
#endif /* USE_RDTSC */
417
418
419
/* return value: space length delta */
420
421
long send_pulse_irdeo(unsigned long length)
422
{
423
	long rawbits;
424
	int i;
425
	unsigned char output;
426
	unsigned char chunk,shifted;
427
428
	/* how many bits have to be sent ? */
429
	rawbits=length*1152/10000;
430
	if(duty_cycle>50) chunk=3;
431
	else chunk=1;
432
	for(i=0,output=0x7f;rawbits>0;rawbits-=3)
433
	{
434
		shifted=chunk<<(i*3);
435
		shifted>>=1;
436
		output&=(~shifted);
437
		i++;
438
		if(i==3)
439
		{
440
			soutp(UART_TX,output);
441
			while(!(sinp(UART_LSR) & UART_LSR_THRE));
442
			output=0x7f;
443
			i=0;
444
		}
445
	}
446
	if(i!=0)
447
	{
448
		soutp(UART_TX,output);
449
		while(!(sinp(UART_LSR) & UART_LSR_TEMT));
450
	}
451
452
	if(i==0)
453
	{
454
		return((-rawbits)*10000/1152);
455
	}
456
	else
457
	{
458
		return((3-i)*3*10000/1152+(-rawbits)*10000/1152);
459
	}
460
}
461
462
#ifdef USE_RDTSC
463
/* Version that uses Pentium rdtsc instruction to measure clocks */
464
465
/* This version does sub-microsecond timing using rdtsc instruction,
466
 * and does away with the fudged CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY
467
 * Implicitly i586 architecture...  - Steve
468
 */
469
470
static inline long send_pulse_homebrew_softcarrier(unsigned long length)
471
{
472
	int flag;
473
	unsigned long target, start, now;
474
475
	/* Get going quick as we can */
476
	rdtscl(start);on();
477
	/* Convert length from microseconds to clocks */
478
	length*=conv_us_to_clocks;
479
	/* And loop till time is up - flipping at right intervals */
480
	now=start;
481
	target=pulse_width;
482
	flag=1;
483
	while((now-start)<length)
484
	{
485
		/* Delay till flip time */
486
		do
487
		{
488
			rdtscl(now);
489
		}
490
		while ((now-start)<target);
491
		/* flip */
492
		if(flag)
493
		{
494
			rdtscl(now);off();
495
			target+=space_width;
496
		}
497
		else
498
		{
499
			rdtscl(now);on();
500
			target+=pulse_width;
501
		}
502
		flag=!flag;
503
	}
504
	rdtscl(now);
505
	return(((now-start)-length)/conv_us_to_clocks);
506
}
507
#else /* ! USE_RDTSC */
508
/* Version using udelay() */
509
510
/* here we use fixed point arithmetic, with 8
511
   fractional bits.  that gets us within 0.1% or so of the right average
512
   frequency, albeit with some jitter in pulse length - Steve */
513
514
/* To match 8 fractional bits used for pulse/space length */
515
516
static inline long send_pulse_homebrew_softcarrier(unsigned long length)
517
{
518
	int flag;
519
	unsigned long actual, target, d;
520
	length<<=8;
521
522
	actual=target=0; flag=0;
523
	while(actual<length)
524
	{
525
		if(flag)
526
		{
527
			off();
528
			target+=space_width;
529
		}
530
		else
531
		{
532
			on();
533
			target+=pulse_width;
534
		}
535
		d=(target-actual-CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY+128)>>8;
536
		/* Note - we've checked in ioctl that the pulse/space
537
		   widths are big enough so that d is > 0 */
538
		udelay(d);
539
		actual+=(d<<8)+CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY;
540
		flag=!flag;
541
	}
542
	return((actual-length)>>8);
543
}
544
#endif /* USE_RDTSC */
545
546
long send_pulse_homebrew(unsigned long length)
547
{
548
	if(length<=0) return 0;
549
	if(softcarrier)
550
	{
551
		return send_pulse_homebrew_softcarrier(length);
552
	}
553
	else
554
	{
555
		on();
556
		safe_udelay(length);
557
		return(0);
558
	}
559
}
560
561
void send_space_irdeo(long length)
562
{
563
	if(length<=0) return;
564
	safe_udelay(length);
565
}
566
567
void send_space_homebrew(long length)
568
{
569
        off();
570
	if(length<=0) return;
571
	safe_udelay(length);
572
}
573
574
static void inline rbwrite(lirc_t l)
575
{
576
	if(lirc_buffer_full(&rbuf))    /* no new signals will be accepted */
577
	{
578
		dprintk(LOGHEAD ": Buffer overrun\n");
579
		return;
580
	}
581
	_lirc_buffer_write_1(&rbuf, (void *)&l);
582
}
583
584
static void inline frbwrite(lirc_t l)
585
{
586
	/* simple noise filter */
587
	static lirc_t pulse=0L,space=0L;
588
	static unsigned int ptr=0;
589
590
	if(ptr>0 && (l&PULSE_BIT))
591
	{
592
		pulse+=l&PULSE_MASK;
593
		if(pulse>250)
594
		{
595
			rbwrite(space);
596
			rbwrite(pulse|PULSE_BIT);
597
			ptr=0;
598
			pulse=0;
599
		}
600
		return;
601
	}
602
	if(!(l&PULSE_BIT))
603
	{
604
		if(ptr==0)
605
		{
606
			if(l>20000)
607
			{
608
				space=l;
609
				ptr++;
610
				return;
611
			}
612
		}
613
		else
614
		{
615
			if(l>20000)
616
			{
617
				space+=pulse;
618
				if(space>PULSE_MASK) space=PULSE_MASK;
619
				space+=l;
620
				if(space>PULSE_MASK) space=PULSE_MASK;
621
				pulse=0;
622
				return;
623
			}
624
			rbwrite(space);
625
			rbwrite(pulse|PULSE_BIT);
626
			ptr=0;
627
			pulse=0;
628
		}
629
	}
630
	rbwrite(l);
631
}
632
633
irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
634
{
635
	struct timeval tv;
636
	int status,counter,dcd;
637
	long deltv;
638
	lirc_t data;
639
640
	counter=0;
641
	do{
642
		counter++;
643
		status=sinp(UART_MSR);
644
		if(counter>RS_ISR_PASS_LIMIT)
645
		{
646
			printk(KERN_WARNING LIRC_DRIVER_NAME ": AIEEEE: "
647
			       "We're caught!\n");
648
			break;
649
		}
650
		if((status&hardware[type].signal_pin_change) && sense!=-1)
651
		{
652
			/* get current time */
653
			do_gettimeofday(&tv);
654
655
			/* New mode, written by Trent Piepho
656
			   <xyzzy@u.washington.edu>. */
657
658
			/* The old format was not very portable.
659
			   We now use the type lirc_t to pass pulses
660
			   and spaces to user space.
661
662
			   If PULSE_BIT is set a pulse has been
663
			   received, otherwise a space has been
664
			   received.  The driver needs to know if your
665
			   receiver is active high or active low, or
666
			   the space/pulse sense could be
667
			   inverted. The bits denoted by PULSE_MASK are
668
			   the length in microseconds. Lengths greater
669
			   than or equal to 16 seconds are clamped to
670
			   PULSE_MASK.  All other bits are unused.
671
			   This is a much simpler interface for user
672
			   programs, as well as eliminating "out of
673
			   phase" errors with space/pulse
674
			   autodetection. */
675
676
			/* calculate time since last interrupt in
677
			   microseconds */
678
			dcd=(status & hardware[type].signal_pin) ? 1:0;
679
680
			deltv=tv.tv_sec-lasttv.tv_sec;
681
			if(deltv>15)
682
			{
683
				dprintk(LOGHEAD
684
				       ": AIEEEE: %d %d %lx %lx %lx %lx\n",
685
				       dcd,sense,
686
				       tv.tv_sec,lasttv.tv_sec,
687
				       tv.tv_usec,lasttv.tv_usec);
688
				data=PULSE_MASK; /* really long time */
689
				if(!(dcd^sense)) /* sanity check */
690
				{
691
				        /* detecting pulse while this
692
					   MUST be a space! */
693
				        sense=sense ? 0:1;
694
				}
695
			}
696
			else
697
			{
698
				data=(lirc_t) (deltv*1000000+
699
					       tv.tv_usec-
700
					       lasttv.tv_usec);
701
			};
702
			if(tv.tv_sec<lasttv.tv_sec ||
703
			   (tv.tv_sec==lasttv.tv_sec &&
704
			    tv.tv_usec<lasttv.tv_usec))
705
			{
706
				printk(KERN_WARNING LIRC_DRIVER_NAME
707
				       ": AIEEEE: your clock just jumped "
708
				       "backwards\n");
709
				printk(KERN_WARNING LIRC_DRIVER_NAME
710
				       ": %d %d %lx %lx %lx %lx\n",
711
				       dcd,sense,
712
				       tv.tv_sec,lasttv.tv_sec,
713
				       tv.tv_usec,lasttv.tv_usec);
714
				data=PULSE_MASK;
715
			}
716
			frbwrite(dcd^sense ? data : (data|PULSE_BIT));
717
			lasttv=tv;
718
			wake_up_interruptible(&rbuf.wait_poll);
719
		}
720
	} while(!(sinp(UART_IIR) & UART_IIR_NO_INT)); /* still pending ? */
721
722
	return IRQ_HANDLED;
723
}
724
725
static DECLARE_WAIT_QUEUE_HEAD(power_supply_queue);
726
static spinlock_t lirc_lock;
727
728
static int init_port(void)
729
{
730
	unsigned long flags;
731
732
	/* Reserve io region. */
733
	if(!request_region(io, 8, LIRC_DRIVER_NAME))
734
	{
735
		printk(KERN_ERR  LIRC_DRIVER_NAME
736
		       ": port %04x already in use\n", io);
737
		printk(KERN_WARNING LIRC_DRIVER_NAME
738
		       ": use 'setserial /dev/ttySX uart none'\n");
739
		printk(KERN_WARNING LIRC_DRIVER_NAME
740
		       ": or compile the serial port driver as module and\n");
741
		printk(KERN_WARNING LIRC_DRIVER_NAME
742
		       ": make sure this module is loaded first\n");
743
		return(-EBUSY);
744
	}
745
746
	lock_kernel();
747
748
	spin_lock_irqsave(&lirc_lock, flags);
749
750
	/* Set DLAB 0. */
751
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
752
753
	/* First of all, disable all interrupts */
754
	soutp(UART_IER, sinp(UART_IER)&
755
	      (~(UART_IER_MSI|UART_IER_RLSI|UART_IER_THRI|UART_IER_RDI)));
756
757
	/* Clear registers. */
758
	sinp(UART_LSR);
759
	sinp(UART_RX);
760
	sinp(UART_IIR);
761
	sinp(UART_MSR);
762
763
	/* Set line for power source */
764
	soutp(UART_MCR, hardware[type].off);
765
766
	/* Clear registers again to be sure. */
767
	sinp(UART_LSR);
768
	sinp(UART_RX);
769
	sinp(UART_IIR);
770
	sinp(UART_MSR);
771
772
	switch(hardware[type].type)
773
	{
774
	case LIRC_IRDEO:
775
	case LIRC_IRDEO_REMOTE:
776
		/* setup port to 7N1 @ 115200 Baud */
777
		/* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
778
779
		/* Set DLAB 1. */
780
		soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
781
		/* Set divisor to 1 => 115200 Baud */
782
		soutp(UART_DLM,0);
783
		soutp(UART_DLL,1);
784
		/* Set DLAB 0 +  7N1 */
785
		soutp(UART_LCR,UART_LCR_WLEN7);
786
		/* THR interrupt already disabled at this point */
787
		break;
788
	default:
789
		break;
790
	}
791
792
	spin_unlock_irqrestore(&lirc_lock, flags);
793
794
	/* Initialize pulse/space widths */
795
	init_timing_params(duty_cycle, freq);
796
797
	/* If pin is high, then this must be an active low receiver. */
798
	if(sense==-1)
799
	{
800
		/* wait 1 sec for the power supply */
801
802
		sleep_on_timeout(&power_supply_queue,HZ);
803
804
		sense=(sinp(UART_MSR) & hardware[type].signal_pin) ? 1:0;
805
		printk(KERN_INFO  LIRC_DRIVER_NAME  ": auto-detected active "
806
		       "%s receiver\n",sense ? "low":"high");
807
	}
808
	else
809
	{
810
		printk(KERN_INFO  LIRC_DRIVER_NAME  ": Manually using active "
811
		       "%s receiver\n",sense ? "low":"high");
812
	};
813
814
	unlock_kernel();
815
816
	return 0;
817
}
818
819
static int set_use_inc(void* data)
820
{
821
	int result;
822
	unsigned long flags;
823
824
	spin_lock(&lirc_lock);
825
#ifdef CONFIG_MODULE_UNLOAD
826
	if(module_refcount(THIS_MODULE))
827
	{
828
		spin_unlock(&lirc_lock);
829
		return -EBUSY;
830
	}
831
#endif
832
833
	/* initialize timestamp */
834
	do_gettimeofday(&lasttv);
835
836
	result=request_irq(irq,irq_handler,SA_INTERRUPT,LIRC_DRIVER_NAME,NULL);
837
	switch(result)
838
	{
839
	case -EBUSY:
840
		printk(KERN_ERR LIRC_DRIVER_NAME ": IRQ %d busy\n", irq);
841
		spin_unlock(&lirc_lock);
842
		return -EBUSY;
843
	case -EINVAL:
844
		printk(KERN_ERR LIRC_DRIVER_NAME
845
		       ": Bad irq number or handler\n");
846
		spin_unlock(&lirc_lock);
847
		return -EINVAL;
848
	default:
849
		dprintk(LOGHEAD
850
		       ": Interrupt %d, port %04x obtained\n", irq, io);
851
		break;
852
	};
853
854
	local_irq_save(flags);
855
856
	/* Set DLAB 0. */
857
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
858
859
	soutp(UART_IER, sinp(UART_IER)|UART_IER_MSI);
860
861
	local_irq_restore(flags);
862
863
	try_module_get(THIS_MODULE);
864
	spin_unlock(&lirc_lock);
865
	return 0;
866
}
867
868
static void set_use_dec(void* data)
869
{	unsigned long flags;
870
871
	spin_lock_irqsave(&lirc_lock, flags);
872
873
	/* Set DLAB 0. */
874
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
875
876
	/* First of all, disable all interrupts */
877
	soutp(UART_IER, sinp(UART_IER)&
878
	      (~(UART_IER_MSI|UART_IER_RLSI|UART_IER_THRI|UART_IER_RDI)));
879
	spin_unlock_irqrestore(&lirc_lock, flags);
880
881
	free_irq(irq, NULL);
882
	dprintk(LOGHEAD ": freed IRQ %d\n", irq);
883
884
	module_put(THIS_MODULE);
885
}
886
887
static ssize_t lirc_write(struct file *file, const char *buf,
888
			 size_t n, loff_t * ppos)
889
{
890
	int retval,i,count;
891
	unsigned long flags;
892
	long delta=0;
893
894
	if(!(hardware[type].features&LIRC_CAN_SEND_PULSE))
895
	{
896
		return(-EBADF);
897
	}
898
899
	if(n%sizeof(lirc_t)) return(-EINVAL);
900
	retval=verify_area(VERIFY_READ,buf,n);
901
	if(retval) return(retval);
902
	count=n/sizeof(lirc_t);
903
	if(count>WBUF_LEN || count%2==0) return(-EINVAL);
904
	copy_from_user(wbuf,buf,n);
905
	spin_lock_irqsave(&lirc_lock, flags);
906
	if(hardware[type].type==LIRC_IRDEO)
907
	{
908
		/* DTR, RTS down */
909
		on();
910
	}
911
	for(i=0;i<count;i++)
912
	{
913
		if(i%2) hardware[type].send_space(wbuf[i]-delta);
914
		else delta=hardware[type].send_pulse(wbuf[i]);
915
	}
916
	off();
917
	spin_unlock_irqrestore(&lirc_lock, flags);
918
	return(n);
919
}
920
921
static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
922
		      unsigned long arg)
923
{
924
        int result;
925
	unsigned long value;
926
	unsigned int ivalue;
927
928
	switch(cmd)
929
	{
930
	case LIRC_GET_SEND_MODE:
931
		if(!(hardware[type].features&LIRC_CAN_SEND_MASK))
932
		{
933
			return(-ENOIOCTLCMD);
934
		}
935
936
		result=put_user(LIRC_SEND2MODE
937
				(hardware[type].features&LIRC_CAN_SEND_MASK),
938
				(unsigned long *) arg);
939
		if(result) return(result);
940
		break;
941
942
	case LIRC_SET_SEND_MODE:
943
		if(!(hardware[type].features&LIRC_CAN_SEND_MASK))
944
		{
945
			return(-ENOIOCTLCMD);
946
		}
947
948
		result=get_user(value,(unsigned long *) arg);
949
		if(result) return(result);
950
		/* only LIRC_MODE_PULSE supported */
951
		if(value!=LIRC_MODE_PULSE) return(-ENOSYS);
952
		break;
953
954
	case LIRC_GET_LENGTH:
955
		return(-ENOSYS);
956
		break;
957
958
	case LIRC_SET_SEND_DUTY_CYCLE:
959
		dprintk(LOGHEAD ": SET_SEND_DUTY_CYCLE\n");
960
		if(!(hardware[type].features&LIRC_CAN_SET_SEND_DUTY_CYCLE))
961
		{
962
			return(-ENOIOCTLCMD);
963
		}
964
965
		result=get_user(ivalue,(unsigned int *) arg);
966
		if(result) return(result);
967
		if(ivalue<=0 || ivalue>100) return(-EINVAL);
968
		return init_timing_params(ivalue, freq);
969
		break;
970
971
	case LIRC_SET_SEND_CARRIER:
972
		dprintk(LOGHEAD ": SET_SEND_CARRIER\n");
973
		if(!(hardware[type].features&LIRC_CAN_SET_SEND_CARRIER))
974
		{
975
			return(-ENOIOCTLCMD);
976
		}
977
978
		result=get_user(ivalue,(unsigned int *) arg);
979
		if(result) return(result);
980
		if(ivalue>500000 || ivalue<20000) return(-EINVAL);
981
		return init_timing_params(duty_cycle, ivalue);
982
		break;
983
984
	default:
985
		return(-ENOIOCTLCMD);
986
	}
987
	return(0);
988
}
989
990
static struct file_operations lirc_fops =
991
{
992
	write:   lirc_write,
993
};
994
995
static struct lirc_plugin plugin = {
996
	name:		LIRC_DRIVER_NAME,
997
	minor:		-1,
998
	code_length:	1,
999
	sample_rate:	0,
1000
	data:		NULL,
1001
	add_to_buf:	NULL,
1002
	get_queue:	NULL,
1003
	rbuf:		&rbuf,
1004
	set_use_inc:	set_use_inc,
1005
	set_use_dec:	set_use_dec,
1006
	ioctl:		lirc_ioctl,
1007
	fops:		&lirc_fops,
1008
};
1009
1010
MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus");
1011
MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
1012
MODULE_LICENSE("GPL");
1013
1014
static int __init lirc_serial_init(void)
1015
{
1016
	int result;
1017
1018
	switch(type)
1019
	{
1020
	case LIRC_HOMEBREW:
1021
	case LIRC_IRDEO:
1022
	case LIRC_IRDEO_REMOTE:
1023
	case LIRC_ANIMAX:
1024
	case LIRC_IGOR:
1025
		break;
1026
	default:
1027
		return(-EINVAL);
1028
	}
1029
	if(!softcarrier && hardware[type].type==LIRC_HOMEBREW)
1030
	{
1031
		hardware[type].features&=~(LIRC_CAN_SET_SEND_DUTY_CYCLE|
1032
					   LIRC_CAN_SET_SEND_CARRIER);
1033
	}
1034
	if ((result = init_port()) < 0)
1035
		return result;
1036
	plugin.features = hardware[type].features;
1037
	lirc_buffer_init(&rbuf, sizeof(lirc_t), RBUF_LEN);
1038
	if ((plugin.minor = lirc_register_plugin(&plugin)) < 0) {
1039
		printk(KERN_ERR  LIRC_DRIVER_NAME
1040
		       ": register_chrdev failed!\n");
1041
		release_region(io, 8);
1042
		return -EIO;
1043
	}
1044
	return 0;
1045
}
1046
1047
static void __exit lirc_serial_exit(void)
1048
{
1049
	release_region(io, 8);
1050
	lirc_buffer_free(&rbuf);
1051
	lirc_unregister_plugin(plugin.minor);
1052
	dprintk(LOGHEAD ": cleaned up module\n");
1053
}
1054
1055
module_init(lirc_serial_init);
1056
module_exit(lirc_serial_exit);
(-)linux-2.6.5.orig/drivers/char/lirc/lirc_sir.c (+1309 lines)
Line 0 Link Here
1
/*
2
 * LIRC SIR driver, (C) 2000 Milan Pikula <www@fornax.sk>
3
 *
4
 * lirc_sir - Device driver for use with SIR (serial infra red)
5
 * mode of IrDA on many notebooks.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 *
21
 *
22
 * 2000/09/16 Frank Przybylski <mail@frankprzybylski.de> :
23
 *  added timeout and relaxed pulse detection, removed gap bug
24
 *
25
 * 2000/12/15 Christoph Bartelmus <lirc@bartelmus.de> :
26
 *   added support for Tekram Irmate 210 (sending does not work yet,
27
 *   kind of disappointing that nobody was able to implement that
28
 *   before),
29
 *   major clean-up
30
 *
31
 * 2001/02/27 Christoph Bartelmus <lirc@bartelmus.de> :
32
 *   added support for StrongARM SA1100 embedded microprocessor
33
 *   parts cut'n'pasted from sa1100_ir.c (C) 2000 Russell King
34
 */
35
36
37
#include <linux/version.h>
38
#include <linux/module.h>
39
#include <linux/config.h>
40
41
#if !defined(CONFIG_LIRC_ON_SA1100) && !defined(CONFIG_SERIAL_MODULE)
42
#warning "******************************************"
43
#warning " Your serial port driver is compiled into "
44
#warning " the kernel. You will have to release the "
45
#warning " port you want to use for LIRC with:      "
46
#warning "    setserial /dev/ttySx uart none        "
47
#warning "******************************************"
48
#endif
49
50
#include <linux/sched.h>
51
#include <linux/errno.h>
52
#include <linux/signal.h>
53
#include <linux/fs.h>
54
#include <linux/interrupt.h>
55
#include <linux/ioport.h>
56
#include <linux/kernel.h>
57
#include <linux/major.h>
58
#include <linux/serial_reg.h>
59
#include <linux/time.h>
60
#include <linux/string.h>
61
#include <linux/types.h>
62
#include <linux/wait.h>
63
#include <linux/mm.h>
64
#include <linux/delay.h>
65
#include <linux/poll.h>
66
#include <asm/system.h>
67
#include <asm/segment.h>
68
#include <asm/io.h>
69
#include <asm/irq.h>
70
#include <asm/fcntl.h>
71
#ifdef CONFIG_LIRC_ON_SA1100
72
#include <asm/hardware.h>
73
#ifdef CONFIG_SA1100_COLLIE
74
#include <asm/arch/tc35143.h>
75
#include <asm/ucb1200.h>
76
#endif
77
#endif
78
79
#include <linux/timer.h>
80
81
#include <linux/lirc.h>
82
#include "lirc_dev.h"
83
84
/* SECTION: Definitions */
85
86
/**************************** Tekram dongle ***************************/
87
#ifdef CONFIG_LIRC_SIR_TEKRAM
88
/* stolen from kernel source */
89
/* definitions for Tekram dongle */
90
#define TEKRAM_115200 0x00
91
#define TEKRAM_57600  0x01
92
#define TEKRAM_38400  0x02
93
#define TEKRAM_19200  0x03
94
#define TEKRAM_9600   0x04
95
#define TEKRAM_2400   0x08
96
97
#define TEKRAM_PW 0x10 /* Pulse select bit */
98
99
/* 10bit * 1s/115200bit in milli seconds = 87ms*/
100
#define TIME_CONST (10000000ul/115200ul)
101
102
#endif
103
104
#ifdef CONFIG_LIRC_SIR_ACTISYS_ACT200L
105
static void init_act200(void);
106
#endif
107
108
/******************************* SA1100 ********************************/
109
#ifdef CONFIG_LIRC_ON_SA1100
110
struct sa1100_ser2_registers
111
{
112
	/* HSSP control register */
113
	unsigned char hscr0;
114
	/* UART registers */
115
	unsigned char utcr0;
116
	unsigned char utcr1;
117
	unsigned char utcr2;
118
	unsigned char utcr3;
119
	unsigned char utcr4;
120
	unsigned char utdr;
121
	unsigned char utsr0;
122
	unsigned char utsr1;
123
} sr;
124
125
static int irq=IRQ_Ser2ICP;
126
127
#define LIRC_ON_SA1100_TRANSMITTER_LATENCY 0
128
129
/* pulse/space ratio of 50/50 */
130
unsigned long pulse_width = (13-LIRC_ON_SA1100_TRANSMITTER_LATENCY);
131
/* 1000000/freq-pulse_width */
132
unsigned long space_width = (13-LIRC_ON_SA1100_TRANSMITTER_LATENCY);
133
unsigned int freq = 38000;      /* modulation frequency */
134
unsigned int duty_cycle = 50;   /* duty cycle of 50% */
135
136
#endif
137
138
#define RBUF_LEN 1024
139
#define WBUF_LEN 1024
140
141
#define LIRC_DRIVER_NAME "lirc_sir"
142
143
#ifndef CONFIG_LIRC_SIR_TEKRAM
144
#define PULSE '['
145
146
/* 9bit * 1s/115200bit in milli seconds = 78.125ms*/
147
#define TIME_CONST (9000000ul/115200ul)
148
#endif
149
150
151
/* timeout for sequences in jiffies (=5/100s) */
152
/* must be longer than TIME_CONST */
153
#define SIR_TIMEOUT	(HZ*5/100)
154
155
#ifndef CONFIG_LIRC_ON_SA1100
156
static int io = CONFIG_LIRC_PORT_SIR;
157
static int irq = CONFIG_LIRC_IRQ_SIR;
158
static int threshold = 3;
159
#endif
160
161
static spinlock_t timer_lock = SPIN_LOCK_UNLOCKED;
162
static struct timer_list timerlist;
163
/* time of last signal change detected */
164
static struct timeval last_tv = {0, 0};
165
/* time of last UART data ready interrupt */
166
static struct timeval last_intr_tv = {0, 0};
167
static int last_value = 0;
168
static spinlock_t lirc_lock;
169
170
static DECLARE_WAIT_QUEUE_HEAD(lirc_read_queue);
171
172
static spinlock_t hardware_lock = SPIN_LOCK_UNLOCKED;
173
static spinlock_t dev_lock = SPIN_LOCK_UNLOCKED;
174
175
static lirc_t rx_buf[RBUF_LEN]; unsigned int rx_tail = 0, rx_head = 0;
176
#ifndef CONFIG_LIRC_SIR_TEKRAM
177
static lirc_t tx_buf[WBUF_LEN];
178
#endif
179
180
/* SECTION: Prototypes */
181
182
/* Communication with user-space */
183
static int lirc_open(struct inode * inode, struct file * file);
184
static int lirc_close(struct inode * inode, struct file *file);
185
static unsigned int lirc_poll(struct file * file, poll_table * wait);
186
static ssize_t lirc_read(struct file * file, char * buf, size_t count,
187
		loff_t * ppos);
188
static ssize_t lirc_write(struct file * file, const char * buf, size_t n, loff_t * pos);
189
static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
190
		unsigned long arg);
191
static void add_read_queue(int flag, unsigned long val);
192
#ifdef MODULE
193
static int init_chrdev(void);
194
static void drop_chrdev(void);
195
#endif
196
	/* Hardware */
197
static irqreturn_t sir_interrupt(int irq, void * dev_id, struct pt_regs * regs);
198
#ifndef CONFIG_LIRC_SIR_TEKRAM
199
static void send_space(unsigned long len);
200
static void send_pulse(unsigned long len);
201
#endif
202
static int init_hardware(void);
203
static void drop_hardware(void);
204
	/* Initialisation */
205
static int init_port(void);
206
static void drop_port(void);
207
int init_module(void);
208
void cleanup_module(void);
209
210
#ifdef CONFIG_LIRC_ON_SA1100
211
void inline on(void)
212
{
213
	PPSR|=PPC_TXD2;
214
}
215
216
void inline off(void)
217
{
218
	PPSR&=~PPC_TXD2;
219
}
220
#else
221
static inline unsigned int sinp(int offset)
222
{
223
	return inb(io + offset);
224
}
225
226
static inline void soutp(int offset, int value)
227
{
228
	outb(value, io + offset);
229
}
230
#endif
231
232
#ifndef MAX_UDELAY_MS
233
#define MAX_UDELAY_US 5000
234
#else
235
#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
236
#endif
237
238
static inline void safe_udelay(unsigned long usecs)
239
{
240
	while(usecs>MAX_UDELAY_US)
241
	{
242
		udelay(MAX_UDELAY_US);
243
		usecs-=MAX_UDELAY_US;
244
	}
245
	udelay(usecs);
246
}
247
248
/* SECTION: Communication with user-space */
249
250
static int lirc_open(struct inode * inode, struct file * file)
251
{
252
	spin_lock(&dev_lock);
253
#ifdef CONFIG_MODULE_UNLOAD
254
	if (module_refcount(THIS_MODULE))
255
	{
256
		spin_unlock(&dev_lock);
257
		return -EBUSY;
258
	}
259
#endif
260
	if (!try_module_get(THIS_MODULE))
261
	{
262
		spin_unlock(&dev_lock);
263
		return -EINVAL;
264
	}
265
	spin_unlock(&dev_lock);
266
	return 0;
267
}
268
269
static int lirc_close(struct inode * inode, struct file *file)
270
{
271
	module_put(THIS_MODULE);
272
	return 0;
273
}
274
275
static unsigned int lirc_poll(struct file * file, poll_table * wait)
276
{
277
	poll_wait(file, &lirc_read_queue, wait);
278
	if (rx_head != rx_tail)
279
		return POLLIN | POLLRDNORM;
280
	return 0;
281
}
282
283
static ssize_t lirc_read(struct file * file, char * buf, size_t count,
284
		loff_t * ppos)
285
{
286
	int n=0;
287
	int retval = 0;
288
	DECLARE_WAITQUEUE(wait,current);
289
290
	if(n%sizeof(lirc_t)) return(-EINVAL);
291
292
	add_wait_queue(&lirc_read_queue,&wait);
293
	current->state=TASK_INTERRUPTIBLE;
294
	while(n<count)
295
	{
296
		if(rx_head!=rx_tail)
297
		{
298
			retval=verify_area(VERIFY_WRITE,
299
					   (void *) buf+n,sizeof(lirc_t));
300
			if (retval)
301
			{
302
				return retval;
303
			}
304
			copy_to_user((void *) buf+n,(void *) (rx_buf+rx_head),
305
				     sizeof(lirc_t));
306
			rx_head=(rx_head+1)&(RBUF_LEN-1);
307
			n+=sizeof(lirc_t);
308
		}
309
		else
310
		{
311
			if(file->f_flags & O_NONBLOCK)
312
			{
313
				retval=-EAGAIN;
314
				break;
315
			}
316
			if(signal_pending(current))
317
			{
318
				retval=-ERESTARTSYS;
319
				break;
320
			}
321
			schedule();
322
			current->state=TASK_INTERRUPTIBLE;
323
		}
324
	}
325
	remove_wait_queue(&lirc_read_queue,&wait);
326
	current->state=TASK_RUNNING;
327
	return (n ? n : retval);
328
}
329
static ssize_t lirc_write(struct file * file, const char * buf, size_t n, loff_t * pos)
330
{
331
	unsigned long flags;
332
#ifdef CONFIG_LIRC_SIR_TEKRAM
333
	return(-EBADF);
334
#else
335
	int i;
336
	int retval;
337
338
        if(n%sizeof(lirc_t) || (n/sizeof(lirc_t)) > WBUF_LEN)
339
		return(-EINVAL);
340
	retval = verify_area(VERIFY_READ, buf, n);
341
	if (retval)
342
		return retval;
343
	copy_from_user(tx_buf, buf, n);
344
	i = 0;
345
	n/=sizeof(lirc_t);
346
#ifdef CONFIG_LIRC_ON_SA1100
347
	/* disable receiver */
348
	Ser2UTCR3=0;
349
#endif
350
	spin_lock_irqsave(&lirc_lock, flags);
351
	while (1) {
352
		if (i >= n)
353
			break;
354
		if (tx_buf[i])
355
			send_pulse(tx_buf[i]);
356
		i++;
357
		if (i >= n)
358
			break;
359
		if (tx_buf[i])
360
			send_space(tx_buf[i]);
361
		i++;
362
	}
363
	spin_unlock_irqrestore(&lirc_lock, flags);
364
#ifdef CONFIG_LIRC_ON_SA1100
365
	off();
366
	udelay(1000); /* wait 1ms for IR diode to recover */
367
	Ser2UTCR3=0;
368
	/* clear status register to prevent unwanted interrupts */
369
	Ser2UTSR0 &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
370
	/* enable receiver */
371
	Ser2UTCR3=UTCR3_RXE|UTCR3_RIE;
372
#endif
373
	return n;
374
#endif
375
}
376
377
static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
378
		unsigned long arg)
379
{
380
	int retval = 0;
381
	unsigned long value = 0;
382
#ifdef CONFIG_LIRC_ON_SA1100
383
	unsigned int ivalue;
384
#endif
385
386
#ifdef CONFIG_LIRC_SIR_TEKRAM
387
	if (cmd == LIRC_GET_FEATURES)
388
		value = LIRC_CAN_REC_MODE2;
389
	else if (cmd == LIRC_GET_SEND_MODE)
390
		value = 0;
391
	else if (cmd == LIRC_GET_REC_MODE)
392
		value = LIRC_MODE_MODE2;
393
#elif defined(CONFIG_LIRC_ON_SA1100)
394
	if (cmd == LIRC_GET_FEATURES)
395
		value = LIRC_CAN_SEND_PULSE |
396
			LIRC_CAN_SET_SEND_DUTY_CYCLE |
397
			LIRC_CAN_SET_SEND_CARRIER |
398
			LIRC_CAN_REC_MODE2;
399
	else if (cmd == LIRC_GET_SEND_MODE)
400
		value = LIRC_MODE_PULSE;
401
	else if (cmd == LIRC_GET_REC_MODE)
402
		value = LIRC_MODE_MODE2;
403
#else
404
	if (cmd == LIRC_GET_FEATURES)
405
		value = LIRC_CAN_SEND_PULSE | LIRC_CAN_REC_MODE2;
406
	else if (cmd == LIRC_GET_SEND_MODE)
407
		value = LIRC_MODE_PULSE;
408
	else if (cmd == LIRC_GET_REC_MODE)
409
		value = LIRC_MODE_MODE2;
410
#endif
411
412
	switch (cmd) {
413
	case LIRC_GET_FEATURES:
414
	case LIRC_GET_SEND_MODE:
415
	case LIRC_GET_REC_MODE:
416
		retval = put_user(value, (unsigned long *) arg);
417
		break;
418
419
	case LIRC_SET_SEND_MODE:
420
	case LIRC_SET_REC_MODE:
421
		retval = get_user(value, (unsigned long *) arg);
422
		break;
423
#ifdef CONFIG_LIRC_ON_SA1100
424
	case LIRC_SET_SEND_DUTY_CYCLE:
425
		retval=get_user(ivalue,(unsigned int *) arg);
426
		if(retval) return(retval);
427
		if(ivalue<=0 || ivalue>100) return(-EINVAL);
428
		/* (ivalue/100)*(1000000/freq) */
429
		duty_cycle=ivalue;
430
		pulse_width=(unsigned long) duty_cycle*10000/freq;
431
		space_width=(unsigned long) 1000000L/freq-pulse_width;
432
		if(pulse_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
433
			pulse_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
434
		if(space_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
435
			space_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
436
		break;
437
	case LIRC_SET_SEND_CARRIER:
438
		retval=get_user(ivalue,(unsigned int *) arg);
439
		if(retval) return(retval);
440
		if(ivalue>500000 || ivalue<20000) return(-EINVAL);
441
		freq=ivalue;
442
		pulse_width=(unsigned long) duty_cycle*10000/freq;
443
		space_width=(unsigned long) 1000000L/freq-pulse_width;
444
		if(pulse_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
445
			pulse_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
446
		if(space_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
447
			space_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
448
		break;
449
#endif
450
	default:
451
		retval = -ENOIOCTLCMD;
452
453
	}
454
455
	if (retval)
456
		return retval;
457
458
#ifdef CONFIG_LIRC_SIR_TEKRAM
459
	if (cmd == LIRC_SET_REC_MODE) {
460
		if (value != LIRC_MODE_MODE2)
461
			retval = -ENOSYS;
462
	} else if (cmd == LIRC_SET_SEND_MODE) {
463
		retval = -ENOSYS;
464
	}
465
#else
466
	if (cmd == LIRC_SET_REC_MODE) {
467
		if (value != LIRC_MODE_MODE2)
468
			retval = -ENOSYS;
469
	} else if (cmd == LIRC_SET_SEND_MODE) {
470
		if (value != LIRC_MODE_PULSE)
471
			retval = -ENOSYS;
472
	}
473
#endif
474
	return retval;
475
}
476
477
static void add_read_queue(int flag, unsigned long val)
478
{
479
	unsigned int new_rx_tail;
480
	lirc_t newval;
481
482
#ifdef DEBUG_SIGNAL
483
	printk(KERN_DEBUG LIRC_DRIVER_NAME
484
		": add flag %d with val %lu\n",
485
		flag,val);
486
#endif
487
488
	newval = val & PULSE_MASK;
489
490
	/* statistically pulses are ~TIME_CONST/2 too long: we could
491
	   maybe make this more exactly but this is good enough */
492
	if(flag) /* pulse */
493
	{
494
		if(newval>TIME_CONST/2)
495
		{
496
			newval-=TIME_CONST/2;
497
		}
498
		else /* should not ever happen */
499
		{
500
			newval=1;
501
		}
502
		newval|=PULSE_BIT;
503
	}
504
	else
505
	{
506
		newval+=TIME_CONST/2;
507
	}
508
	new_rx_tail = (rx_tail + 1) & (RBUF_LEN - 1);
509
	if (new_rx_tail == rx_head) {
510
#               ifdef DEBUG
511
		printk(KERN_WARNING LIRC_DRIVER_NAME ": Buffer overrun.\n");
512
#               endif
513
		return;
514
	}
515
	rx_buf[rx_tail] = newval;
516
	rx_tail = new_rx_tail;
517
	wake_up_interruptible(&lirc_read_queue);
518
}
519
520
static struct file_operations lirc_fops =
521
{
522
	.read    = lirc_read,
523
	.write   = lirc_write,
524
	.poll    = lirc_poll,
525
	.ioctl   = lirc_ioctl,
526
	.open    = lirc_open,
527
	.release = lirc_close,
528
};
529
530
static int set_use_inc(void* data)
531
{
532
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
533
       if (!try_module_get(THIS_MODULE)) {
534
                return -EINVAL;
535
       }
536
#endif
537
       return 0;
538
}
539
540
static void set_use_dec(void* data)
541
{
542
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
543
       module_put(THIS_MODULE);
544
#endif
545
}
546
static struct lirc_plugin plugin = {
547
       .name          = LIRC_DRIVER_NAME,
548
       .minor         = -1,
549
       .code_length   = 1,
550
       .sample_rate   = 0,
551
       .data          = NULL,
552
       .add_to_buf    = NULL,
553
       .get_queue     = NULL,
554
       .set_use_inc   = set_use_inc,
555
       .set_use_dec   = set_use_dec,
556
       .fops          = &lirc_fops,
557
};
558
559
int init_chrdev(void)
560
{
561
	plugin.minor = lirc_register_plugin(&plugin);
562
	if (plugin.minor < 0) {
563
		printk(KERN_ERR LIRC_DRIVER_NAME ": init_chrdev() failed.\n");
564
		return -EIO;
565
	}
566
	return 0;
567
}
568
569
static void drop_chrdev(void)
570
{
571
	lirc_unregister_plugin(plugin.minor);
572
}
573
574
/* SECTION: Hardware */
575
static long delta(struct timeval * tv1, struct timeval * tv2)
576
{
577
	unsigned long deltv;
578
579
	deltv = tv2->tv_sec - tv1->tv_sec;
580
	if (deltv > 15)
581
		deltv = 0xFFFFFF;
582
	else
583
		deltv = deltv*1000000 +
584
			tv2->tv_usec -
585
			tv1->tv_usec;
586
	return deltv;
587
}
588
589
static void sir_timeout(unsigned long data)
590
{
591
	/* if last received signal was a pulse, but receiving stopped
592
	   within the 9 bit frame, we need to finish this pulse and
593
	   simulate a signal change to from pulse to space. Otherwise
594
	   upper layers will receive two sequences next time. */
595
596
	unsigned long flags;
597
	unsigned long pulse_end;
598
599
	/* avoid interference with interrupt */
600
 	spin_lock_irqsave(&timer_lock, flags);
601
	if (last_value)
602
	{
603
#ifndef CONFIG_LIRC_ON_SA1100
604
		/* clear unread bits in UART and restart */
605
		outb(UART_FCR_CLEAR_RCVR, io + UART_FCR);
606
#endif
607
		/* determine 'virtual' pulse end: */
608
	 	pulse_end = delta(&last_tv, &last_intr_tv);
609
#ifdef DEBUG_SIGNAL
610
		printk(KERN_DEBUG LIRC_DRIVER_NAME
611
			": timeout add %d for %lu usec\n",last_value,pulse_end);
612
#endif
613
		add_read_queue(last_value,pulse_end);
614
		last_value = 0;
615
		last_tv=last_intr_tv;
616
	}
617
	spin_unlock_irqrestore(&timer_lock, flags);
618
}
619
620
static irqreturn_t sir_interrupt(int irq, void *dev_id, struct pt_regs *regs)
621
{
622
	unsigned char data;
623
	struct timeval curr_tv;
624
	static unsigned long deltv;
625
#ifdef CONFIG_LIRC_ON_SA1100
626
	int status;
627
	static int n=0;
628
629
	//printk("interrupt\n");
630
	status = Ser2UTSR0;
631
	/*
632
	 * Deal with any receive errors first.  The bytes in error may be
633
	 * the only bytes in the receive FIFO, so we do this first.
634
	 */
635
	while (status & UTSR0_EIF)
636
	{
637
		int bstat;
638
639
#ifdef DEBUG
640
		printk("EIF\n");
641
		bstat = Ser2UTSR1;
642
643
		if (bstat & UTSR1_FRE)
644
			printk("frame error\n");
645
		if (bstat & UTSR1_ROR)
646
			printk("receive fifo overrun\n");
647
		if(bstat&UTSR1_PRE)
648
			printk("parity error\n");
649
#endif
650
651
		bstat = Ser2UTDR;
652
		n++;
653
		status = Ser2UTSR0;
654
	}
655
656
	if (status & (UTSR0_RFS | UTSR0_RID))
657
	{
658
		do_gettimeofday(&curr_tv);
659
		deltv = delta(&last_tv, &curr_tv);
660
		do
661
		{
662
#ifdef DEBUG_SIGNAL
663
			printk(KERN_DEBUG LIRC_DRIVER_NAME": t %lu , d %d\n",
664
			       deltintrtv,(int)data);
665
#endif
666
			data=Ser2UTDR;
667
			//printk("data: %d\n",data);
668
			n++;
669
		}
670
		while(status&UTSR0_RID && /* do not empty fifo in
671
                                             order to get UTSR0_RID in
672
                                             any case */
673
		      Ser2UTSR1 & UTSR1_RNE); /* data ready */
674
675
		if(status&UTSR0_RID)
676
		{
677
			//printk("add\n");
678
			add_read_queue(0,deltv-n*TIME_CONST); /*space*/
679
			add_read_queue(1,n*TIME_CONST); /*pulse*/
680
			n=0;
681
			last_tv=curr_tv;
682
		}
683
	}
684
685
	if (status & UTSR0_TFS) {
686
687
		printk("transmit fifo not full, shouldn't ever happen\n");
688
	}
689
690
	/*
691
	 * We must clear certain bits.
692
	 */
693
	status &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
694
	if (status)
695
		Ser2UTSR0 = status;
696
#else
697
	unsigned long deltintrtv;
698
	unsigned long flags;
699
	int iir, lsr;
700
701
	while ((iir = inb(io + UART_IIR) & UART_IIR_ID)) {
702
		switch (iir&UART_IIR_ID) { /* FIXME toto treba preriedit */
703
		case UART_IIR_MSI:
704
			(void) inb(io + UART_MSR);
705
			break;
706
		case UART_IIR_RLSI:
707
			(void) inb(io + UART_LSR);
708
			break;
709
		case UART_IIR_THRI:
710
#if 0
711
			if (lsr & UART_LSR_THRE) /* FIFO is empty */
712
				outb(data, io + UART_TX)
713
#endif
714
			break;
715
		case UART_IIR_RDI:
716
			/* avoid interference with timer */
717
		 	spin_lock_irqsave(&timer_lock, flags);
718
			do
719
			{
720
				del_timer(&timerlist);
721
				data = inb(io + UART_RX);
722
				do_gettimeofday(&curr_tv);
723
				deltv = delta(&last_tv, &curr_tv);
724
				deltintrtv = delta(&last_intr_tv, &curr_tv);
725
#ifdef DEBUG_SIGNAL
726
				printk(KERN_DEBUG LIRC_DRIVER_NAME": t %lu , d %d\n",deltintrtv,(int)data);
727
#endif
728
				/* if nothing came in last X cycles,
729
				   it was gap */
730
				if (deltintrtv > TIME_CONST * threshold) {
731
					if (last_value) {
732
#ifdef DEBUG_SIGNAL
733
						printk(KERN_DEBUG LIRC_DRIVER_NAME ": GAP\n");
734
#endif
735
						/* simulate signal change */
736
						add_read_queue(last_value,
737
							       deltv-
738
							       deltintrtv);
739
						last_value = 0;
740
						last_tv.tv_sec = last_intr_tv.tv_sec;
741
						last_tv.tv_usec = last_intr_tv.tv_usec;
742
						deltv = deltintrtv;
743
					}
744
				}
745
				data = 1;
746
				if (data ^ last_value) {
747
					/* deltintrtv > 2*TIME_CONST,
748
                                           remember ? */
749
					/* the other case is timeout */
750
					add_read_queue(last_value,
751
						       deltv-TIME_CONST);
752
					last_value = data;
753
					last_tv = curr_tv;
754
					if(last_tv.tv_usec>=TIME_CONST)
755
					{
756
						last_tv.tv_usec-=TIME_CONST;
757
					}
758
					else
759
					{
760
						last_tv.tv_sec--;
761
						last_tv.tv_usec+=1000000-
762
							TIME_CONST;
763
					}
764
				}
765
				last_intr_tv = curr_tv;
766
				if (data)
767
				{
768
					/* start timer for end of sequence detection */
769
					timerlist.expires = jiffies + SIR_TIMEOUT;
770
					add_timer(&timerlist);
771
				}
772
			}
773
			while ((lsr = inb(io + UART_LSR))
774
				& UART_LSR_DR); /* data ready */
775
			spin_unlock_irqrestore(&timer_lock, flags);
776
			break;
777
		default:
778
			break;
779
		}
780
	}
781
#endif
782
	return IRQ_HANDLED;
783
}
784
785
#ifdef CONFIG_LIRC_ON_SA1100
786
void send_pulse(unsigned long length)
787
{
788
	unsigned long k,delay;
789
	int flag;
790
791
	if(length==0) return;
792
	/* this won't give us the carrier frequency we really want
793
	   due to integer arithmetic, but we can accept this inaccuracy */
794
795
	for(k=flag=0;k<length;k+=delay,flag=!flag)
796
	{
797
		if(flag)
798
		{
799
			off();
800
			delay=space_width;
801
		}
802
		else
803
		{
804
			on();
805
			delay=pulse_width;
806
		}
807
		safe_udelay(delay);
808
	}
809
	off();
810
}
811
812
void send_space(unsigned long length)
813
{
814
	if(length==0) return;
815
	off();
816
	safe_udelay(length);
817
}
818
#elif defined(CONFIG_LIRC_SIR_TEKRAM)
819
#else
820
static void send_space(unsigned long len)
821
{
822
	safe_udelay(len);
823
}
824
825
static void send_pulse(unsigned long len)
826
{
827
	long bytes_out = len / TIME_CONST;
828
	long time_left;
829
830
	if (!bytes_out)
831
		bytes_out++;
832
	time_left = (long)len - (long)bytes_out * (long)TIME_CONST;
833
	while (--bytes_out) {
834
		outb(PULSE, io + UART_TX);
835
		/* FIXME treba seriozne cakanie z drivers/char/serial.c */
836
		while (!(inb(io + UART_LSR) & UART_LSR_THRE));
837
	}
838
#if 0
839
	if (time_left > 0)
840
		safe_udelay(time_left);
841
#endif
842
}
843
#endif
844
845
#ifdef CONFIG_SA1100_COLLIE
846
static inline int sa1100_irda_set_power_collie(int state)
847
{
848
	if (state) {
849
		/*
850
		 *  0 - off
851
		 *  1 - short range, lowest power
852
		 *  2 - medium range, medium power
853
		 *  3 - maximum range, high power
854
		 */
855
		ucb1200_set_io_direction(TC35143_GPIO_IR_ON,
856
					 TC35143_IODIR_OUTPUT);
857
		ucb1200_set_io(TC35143_GPIO_IR_ON, TC35143_IODAT_LOW);
858
		udelay(100);
859
	}
860
	else {
861
		/* OFF */
862
		ucb1200_set_io_direction(TC35143_GPIO_IR_ON,
863
					 TC35143_IODIR_OUTPUT);
864
		ucb1200_set_io(TC35143_GPIO_IR_ON, TC35143_IODAT_HIGH);
865
	}
866
	return 0;
867
}
868
#endif
869
870
static int init_hardware(void)
871
{
872
	int flags;
873
874
	spin_lock_irqsave(&hardware_lock, flags);
875
	/* reset UART */
876
#ifdef CONFIG_LIRC_ON_SA1100
877
#ifdef CONFIG_SA1100_BITSY
878
	if (machine_is_bitsy()) {
879
		printk("Power on IR module\n");
880
		set_bitsy_egpio(EGPIO_BITSY_IR_ON);
881
	}
882
#endif
883
#ifdef CONFIG_SA1100_COLLIE
884
	sa1100_irda_set_power_collie(3);	/* power on */
885
#endif
886
	sr.hscr0=Ser2HSCR0;
887
888
	sr.utcr0=Ser2UTCR0;
889
	sr.utcr1=Ser2UTCR1;
890
	sr.utcr2=Ser2UTCR2;
891
	sr.utcr3=Ser2UTCR3;
892
	sr.utcr4=Ser2UTCR4;
893
894
	sr.utdr=Ser2UTDR;
895
	sr.utsr0=Ser2UTSR0;
896
	sr.utsr1=Ser2UTSR1;
897
898
	/* configure GPIO */
899
	/* output */
900
	PPDR|=PPC_TXD2;
901
	PSDR|=PPC_TXD2;
902
	/* set output to 0 */
903
	off();
904
905
	/*
906
	 * Enable HP-SIR modulation, and ensure that the port is disabled.
907
	 */
908
	Ser2UTCR3=0;
909
	Ser2HSCR0=sr.hscr0 & (~HSCR0_HSSP);
910
911
	/* clear status register to prevent unwanted interrupts */
912
	Ser2UTSR0 &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
913
914
	/* 7N1 */
915
	Ser2UTCR0=UTCR0_1StpBit|UTCR0_7BitData;
916
	/* 115200 */
917
	Ser2UTCR1=0;
918
	Ser2UTCR2=1;
919
	/* use HPSIR, 1.6 usec pulses */
920
	Ser2UTCR4=UTCR4_HPSIR|UTCR4_Z1_6us;
921
922
	/* enable receiver, receive fifo interrupt */
923
	Ser2UTCR3=UTCR3_RXE|UTCR3_RIE;
924
925
	/* clear status register to prevent unwanted interrupts */
926
	Ser2UTSR0 &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
927
928
#elif defined(CONFIG_LIRC_SIR_TEKRAM)
929
	/* disable FIFO */
930
	soutp(UART_FCR,
931
	      UART_FCR_CLEAR_RCVR|
932
	      UART_FCR_CLEAR_XMIT|
933
	      UART_FCR_TRIGGER_1);
934
935
	/* Set DLAB 0. */
936
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
937
938
	/* First of all, disable all interrupts */
939
	soutp(UART_IER, sinp(UART_IER)&
940
	      (~(UART_IER_MSI|UART_IER_RLSI|UART_IER_THRI|UART_IER_RDI)));
941
942
	/* Set DLAB 1. */
943
	soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
944
945
	/* Set divisor to 12 => 9600 Baud */
946
	soutp(UART_DLM,0);
947
	soutp(UART_DLL,12);
948
949
	/* Set DLAB 0. */
950
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
951
952
	/* power supply */
953
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
954
	safe_udelay(50*1000);
955
956
	/* -DTR low -> reset PIC */
957
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_OUT2);
958
	udelay(1*1000);
959
960
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
961
	udelay(100);
962
963
964
        /* -RTS low -> send control byte */
965
	soutp(UART_MCR, UART_MCR_DTR|UART_MCR_OUT2);
966
	udelay(7);
967
	soutp(UART_TX, TEKRAM_115200|TEKRAM_PW);
968
969
	/* one byte takes ~1042 usec to transmit at 9600,8N1 */
970
	udelay(1500);
971
972
	/* back to normal operation */
973
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
974
	udelay(50);
975
976
	udelay(1500);
977
978
	/* read previous control byte */
979
	printk(KERN_INFO LIRC_DRIVER_NAME
980
	       ": 0x%02x\n",sinp(UART_RX));
981
982
	/* Set DLAB 1. */
983
	soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
984
985
	/* Set divisor to 1 => 115200 Baud */
986
	soutp(UART_DLM,0);
987
	soutp(UART_DLL,1);
988
989
	/* Set DLAB 0, 8 Bit */
990
	soutp(UART_LCR, UART_LCR_WLEN8);
991
	/* enable interrupts */
992
	soutp(UART_IER, sinp(UART_IER)|UART_IER_RDI);
993
#else
994
	outb(0, io + UART_MCR);
995
	outb(0, io + UART_IER);
996
	/* init UART */
997
		/* set DLAB, speed = 115200 */
998
	outb(UART_LCR_DLAB | UART_LCR_WLEN7, io + UART_LCR);
999
	outb(1, io + UART_DLL); outb(0, io + UART_DLM);
1000
		/* 7N1+start = 9 bits at 115200 ~ 3 bits at 44000 */
1001
	outb(UART_LCR_WLEN7, io + UART_LCR);
1002
		/* FIFO operation */
1003
	outb(UART_FCR_ENABLE_FIFO, io + UART_FCR);
1004
		/* interrupts */
1005
	// outb(UART_IER_RLSI|UART_IER_RDI|UART_IER_THRI, io + UART_IER);
1006
	outb(UART_IER_RDI, io + UART_IER);
1007
	/* turn on UART */
1008
	outb(UART_MCR_DTR|UART_MCR_RTS|UART_MCR_OUT2, io + UART_MCR);
1009
#ifdef CONFIG_LIRC_SIR_ACTISYS_ACT200L
1010
	init_act200();
1011
#endif
1012
#endif
1013
	spin_unlock_irqrestore(&hardware_lock, flags);
1014
	return 0;
1015
}
1016
1017
static void drop_hardware(void)
1018
{
1019
	int flags;
1020
1021
	spin_lock_irqsave(&hardware_lock, flags);
1022
1023
#ifdef CONFIG_LIRC_ON_SA1100
1024
	Ser2UTCR3=0;
1025
1026
	Ser2UTCR0=sr.utcr0;
1027
	Ser2UTCR1=sr.utcr1;
1028
	Ser2UTCR2=sr.utcr2;
1029
	Ser2UTCR4=sr.utcr4;
1030
	Ser2UTCR3=sr.utcr3;
1031
1032
	Ser2HSCR0=sr.hscr0;
1033
#ifdef CONFIG_SA1100_BITSY
1034
	if (machine_is_bitsy()) {
1035
		clr_bitsy_egpio(EGPIO_BITSY_IR_ON);
1036
	}
1037
#endif
1038
#ifdef CONFIG_SA1100_COLLIE
1039
	sa1100_irda_set_power_collie(0);	/* power off */
1040
#endif
1041
#else
1042
	/* turn off interrupts */
1043
	outb(0, io + UART_IER);
1044
#endif
1045
	spin_unlock_irqrestore(&hardware_lock, flags);
1046
}
1047
1048
/* SECTION: Initialisation */
1049
1050
static int init_port(void)
1051
{
1052
	int retval;
1053
1054
#ifndef CONFIG_LIRC_ON_SA1100
1055
	/* get I/O port access and IRQ line */
1056
	retval = request_region(io, 8, LIRC_DRIVER_NAME);
1057
	if (!retval) {
1058
		printk(KERN_ERR LIRC_DRIVER_NAME
1059
			": i/o port 0x%.4x already in use.\n",
1060
			io);
1061
		return retval;
1062
	}
1063
	printk(KERN_INFO LIRC_DRIVER_NAME
1064
		": I/O port 0x%.4x, IRQ %d.\n",
1065
		io, irq);
1066
#endif
1067
	retval = request_irq(irq, sir_interrupt, SA_INTERRUPT,
1068
			     LIRC_DRIVER_NAME, NULL);
1069
	if (retval < 0) {
1070
		printk(KERN_ERR LIRC_DRIVER_NAME
1071
			": IRQ %d already in use.\n",
1072
			irq);
1073
		return retval;
1074
	}
1075
1076
	init_timer(&timerlist);
1077
	timerlist.function = sir_timeout;
1078
	timerlist.data = 0xabadcafe;
1079
1080
	return 0;
1081
}
1082
1083
static void drop_port(void)
1084
{
1085
	disable_irq(irq);
1086
	free_irq(irq, NULL);
1087
	del_timer_sync(&timerlist);
1088
#ifndef CONFIG_LIRC_ON_SA1100
1089
	release_region(io, 8);
1090
#endif
1091
}
1092
1093
#ifdef CONFIG_LIRC_SIR_ACTISYS_ACT200L
1094
/******************************************************/
1095
/* Crystal/Cirrus CS8130 IR transceiver, used in Actisys Act200L dongle */
1096
/* some code borrowed from Linux IRDA driver */
1097
1098
/* Regsiter 0: Control register #1 */
1099
#define ACT200L_REG0    0x00
1100
#define ACT200L_TXEN    0x01 /* Enable transmitter */
1101
#define ACT200L_RXEN    0x02 /* Enable receiver */
1102
#define ACT200L_ECHO    0x08 /* Echo control chars */
1103
1104
/* Register 1: Control register #2 */
1105
#define ACT200L_REG1    0x10
1106
#define ACT200L_LODB    0x01 /* Load new baud rate count value */
1107
#define ACT200L_WIDE    0x04 /* Expand the maximum allowable pulse */
1108
1109
/* Register 3: Transmit mode register #2 */
1110
#define ACT200L_REG3    0x30
1111
#define ACT200L_B0      0x01 /* DataBits, 0=6, 1=7, 2=8, 3=9(8P)  */
1112
#define ACT200L_B1      0x02 /* DataBits, 0=6, 1=7, 2=8, 3=9(8P)  */
1113
#define ACT200L_CHSY    0x04 /* StartBit Synced 0=bittime, 1=startbit */
1114
1115
/* Register 4: Output Power register */
1116
#define ACT200L_REG4    0x40
1117
#define ACT200L_OP0     0x01 /* Enable LED1C output */
1118
#define ACT200L_OP1     0x02 /* Enable LED2C output */
1119
#define ACT200L_BLKR    0x04
1120
1121
/* Register 5: Receive Mode register */
1122
#define ACT200L_REG5    0x50
1123
#define ACT200L_RWIDL   0x01 /* fixed 1.6us pulse mode */
1124
    /*.. other various IRDA bit modes, and TV remote modes..*/
1125
1126
/* Register 6: Receive Sensitivity register #1 */
1127
#define ACT200L_REG6    0x60
1128
#define ACT200L_RS0     0x01 /* receive threshold bit 0 */
1129
#define ACT200L_RS1     0x02 /* receive threshold bit 1 */
1130
1131
/* Register 7: Receive Sensitivity register #2 */
1132
#define ACT200L_REG7    0x70
1133
#define ACT200L_ENPOS   0x04 /* Ignore the falling edge */
1134
1135
/* Register 8,9: Baud Rate Dvider register #1,#2 */
1136
#define ACT200L_REG8    0x80
1137
#define ACT200L_REG9    0x90
1138
1139
#define ACT200L_2400    0x5f
1140
#define ACT200L_9600    0x17
1141
#define ACT200L_19200   0x0b
1142
#define ACT200L_38400   0x05
1143
#define ACT200L_57600   0x03
1144
#define ACT200L_115200  0x01
1145
1146
/* Register 13: Control register #3 */
1147
#define ACT200L_REG13   0xd0
1148
#define ACT200L_SHDW    0x01 /* Enable access to shadow registers */
1149
1150
/* Register 15: Status register */
1151
#define ACT200L_REG15   0xf0
1152
1153
/* Register 21: Control register #4 */
1154
#define ACT200L_REG21   0x50
1155
#define ACT200L_EXCK    0x02 /* Disable clock output driver */
1156
#define ACT200L_OSCL    0x04 /* oscillator in low power, medium accuracy mode */
1157
1158
static void init_act200(void)
1159
{
1160
  int i;
1161
	__u8 control[] = {
1162
		ACT200L_REG15,
1163
		ACT200L_REG13 | ACT200L_SHDW,
1164
		ACT200L_REG21 | ACT200L_EXCK | ACT200L_OSCL,
1165
		ACT200L_REG13,
1166
		ACT200L_REG7  | ACT200L_ENPOS,
1167
		ACT200L_REG6  | ACT200L_RS0  | ACT200L_RS1,
1168
		ACT200L_REG5  | ACT200L_RWIDL,
1169
		ACT200L_REG4  | ACT200L_OP0  | ACT200L_OP1 | ACT200L_BLKR,
1170
		ACT200L_REG3  | ACT200L_B0,
1171
		ACT200L_REG0  | ACT200L_TXEN | ACT200L_RXEN,
1172
		ACT200L_REG8 |  (ACT200L_115200       & 0x0f),
1173
		ACT200L_REG9 | ((ACT200L_115200 >> 4) & 0x0f),
1174
		ACT200L_REG1 | ACT200L_LODB | ACT200L_WIDE
1175
	};
1176
1177
	/* Set DLAB 1. */
1178
	soutp(UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN8);
1179
1180
	/* Set divisor to 12 => 9600 Baud */
1181
	soutp(UART_DLM,0);
1182
	soutp(UART_DLL,12);
1183
1184
	/* Set DLAB 0. */
1185
	soutp(UART_LCR, UART_LCR_WLEN8);
1186
	/* Set divisor to 12 => 9600 Baud */
1187
1188
	/* power supply */
1189
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
1190
	for (i=0; i<50; i++) {
1191
		safe_udelay(1000);
1192
	}
1193
1194
		/* Reset the dongle : set RTS low for 25 ms */
1195
	soutp(UART_MCR, UART_MCR_DTR|UART_MCR_OUT2);
1196
	for (i=0; i<25; i++) {
1197
		udelay(1000);
1198
	}
1199
1200
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
1201
	udelay(100);
1202
1203
	/* Clear DTR and set RTS to enter command mode */
1204
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_OUT2);
1205
	udelay(7);
1206
1207
/* send out the control register settings for 115K 7N1 SIR operation */
1208
	for (i=0; i<sizeof(control); i++) {
1209
		soutp(UART_TX, control[i]);
1210
		/* one byte takes ~1042 usec to transmit at 9600,8N1 */
1211
		udelay(1500);
1212
	}
1213
1214
	/* back to normal operation */
1215
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
1216
	udelay(50);
1217
1218
	udelay(1500);
1219
	soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
1220
1221
	/* Set DLAB 1. */
1222
	soutp(UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN7);
1223
1224
	/* Set divisor to 1 => 115200 Baud */
1225
	soutp(UART_DLM,0);
1226
	soutp(UART_DLL,1);
1227
1228
	/* Set DLAB 0. */
1229
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
1230
1231
	/* Set DLAB 0, 7 Bit */
1232
	soutp(UART_LCR, UART_LCR_WLEN7);
1233
1234
	/* enable interrupts */
1235
	soutp(UART_IER, sinp(UART_IER)|UART_IER_RDI);
1236
}
1237
#endif
1238
1239
int init_lirc_sir(void)
1240
{
1241
	int retval;
1242
1243
	init_waitqueue_head(&lirc_read_queue);
1244
	retval = init_port();
1245
	if (retval < 0)
1246
		return retval;
1247
	init_hardware();
1248
	enable_irq(irq);
1249
	printk(KERN_INFO LIRC_DRIVER_NAME
1250
		": Installed.\n");
1251
	return 0;
1252
}
1253
1254
#ifdef CONFIG_LIRC_SIR_TEKRAM
1255
MODULE_AUTHOR("Christoph Bartelmus");
1256
MODULE_DESCRIPTION("Infrared receiver driver for Tekram Irmate 210");
1257
#elif defined(CONFIG_LIRC_ON_SA1100)
1258
MODULE_AUTHOR("Christoph Bartelmus");
1259
MODULE_DESCRIPTION("LIRC driver for StrongARM SA1100 embedded microprocessor");
1260
#elif defined(CONFIG_LIRC_SIR_ACTISYS_ACT200L)
1261
MODULE_AUTHOR("Karl Bongers");
1262
MODULE_DESCRIPTION("LIRC driver for Actisys Act200L");
1263
#else
1264
MODULE_AUTHOR("Milan Pikula");
1265
MODULE_DESCRIPTION("Infrared receiver driver for SIR type serial ports");
1266
#endif
1267
1268
#ifdef CONFIG_LIRC_ON_SA1100
1269
MODULE_PARM(irq, "i");
1270
MODULE_PARM_DESC(irq, "Interrupt (16)");
1271
#else
1272
MODULE_PARM(io, "i");
1273
MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
1274
MODULE_PARM(irq, "i");
1275
MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
1276
MODULE_PARM(threshold, "i");
1277
MODULE_PARM_DESC(threshold, "space detection threshold (3)");
1278
#endif
1279
1280
MODULE_LICENSE("GPL");
1281
1282
static int __init lirc_sir_init(void)
1283
{
1284
	int retval;
1285
1286
	retval=init_chrdev();
1287
	if(retval < 0)
1288
		return retval;
1289
	retval = init_lirc_sir();
1290
	if (retval) {
1291
		drop_chrdev();
1292
		return retval;
1293
	}
1294
	return 0;
1295
}
1296
1297
static void __exit lirc_sir_exit(void)
1298
{
1299
#ifdef CONFIG_MODULE_UNLOAD
1300
	if(module_refcount(THIS_MODULE)) return;
1301
#endif
1302
	drop_hardware();
1303
	drop_chrdev();
1304
	drop_port();
1305
	printk(KERN_INFO LIRC_DRIVER_NAME ": Uninstalled.\n");
1306
}
1307
1308
module_init(lirc_sir_init);
1309
module_exit(lirc_sir_exit);
(-)linux-2.6.5.orig/include/linux/lirc.h (+103 lines)
Line 0 Link Here
1
/*      $Id: lirc.h,v 5.8 2003/01/26 12:57:59 lirc Exp $      */
2
3
#ifndef _LINUX_LIRC_H
4
#define _LINUX_LIRC_H
5
6
#if defined (__linux__)
7
#include <asm/types.h>
8
#include <linux/ioctl.h>
9
#else
10
#include <sys/types.h>
11
typedef u_int32_t __u32;
12
#endif
13
14
#define PULSE_BIT  0x01000000
15
#define PULSE_MASK 0x00FFFFFF
16
17
typedef int lirc_t;
18
19
/*
20
 * lirc compatible hardware features
21
 */
22
23
24
#define LIRC_MODE2SEND(x) (x)
25
#define LIRC_SEND2MODE(x) (x)
26
#define LIRC_MODE2REC(x) ((x) << 16)
27
#define LIRC_REC2MODE(x) ((x) >> 16)
28
29
#define LIRC_MODE_RAW                  0x00000001
30
#define LIRC_MODE_PULSE                0x00000002
31
#define LIRC_MODE_MODE2                0x00000004
32
#define LIRC_MODE_CODE                 0x00000008
33
#define LIRC_MODE_LIRCCODE             0x00000010
34
#define LIRC_MODE_STRING               0x00000020
35
36
37
#define LIRC_CAN_SEND_RAW              LIRC_MODE2SEND(LIRC_MODE_RAW)
38
#define LIRC_CAN_SEND_PULSE            LIRC_MODE2SEND(LIRC_MODE_PULSE)
39
#define LIRC_CAN_SEND_MODE2            LIRC_MODE2SEND(LIRC_MODE_MODE2)
40
#define LIRC_CAN_SEND_CODE             LIRC_MODE2SEND(LIRC_MODE_CODE)
41
#define LIRC_CAN_SEND_LIRCCODE         LIRC_MODE2SEND(LIRC_MODE_LIRCCODE)
42
#define LIRC_CAN_SEND_STRING           LIRC_MODE2SEND(LIRC_MODE_STRING)
43
44
#define LIRC_CAN_SEND_MASK             0x0000003f
45
46
#define LIRC_CAN_SET_SEND_CARRIER      0x00000100
47
#define LIRC_CAN_SET_SEND_DUTY_CYCLE   0x00000200
48
49
#define LIRC_CAN_REC_RAW               LIRC_MODE2REC(LIRC_MODE_RAW)
50
#define LIRC_CAN_REC_PULSE             LIRC_MODE2REC(LIRC_MODE_PULSE)
51
#define LIRC_CAN_REC_MODE2             LIRC_MODE2REC(LIRC_MODE_MODE2)
52
#define LIRC_CAN_REC_CODE              LIRC_MODE2REC(LIRC_MODE_CODE)
53
#define LIRC_CAN_REC_LIRCCODE          LIRC_MODE2REC(LIRC_MODE_LIRCCODE)
54
#define LIRC_CAN_REC_STRING            LIRC_MODE2REC(LIRC_MODE_STRING)
55
56
#define LIRC_CAN_REC_MASK              LIRC_MODE2REC(LIRC_CAN_SEND_MASK)
57
58
#define LIRC_CAN_SET_REC_CARRIER       (LIRC_CAN_SET_SEND_CARRIER << 16)
59
#define LIRC_CAN_SET_REC_DUTY_CYCLE    (LIRC_CAN_SET_SEND_DUTY_CYCLE << 16)
60
61
#define LIRC_CAN_SET_REC_DUTY_CYCLE_RANGE 0x40000000
62
#define LIRC_CAN_SET_REC_CARRIER_RANGE    0x80000000
63
64
65
#define LIRC_CAN_SEND(x) ((x)&LIRC_CAN_SEND_MASK)
66
#define LIRC_CAN_REC(x) ((x)&LIRC_CAN_REC_MASK)
67
68
/*
69
 * IOCTL commands for lirc driver
70
 */
71
72
#define LIRC_GET_FEATURES              _IOR('i', 0x00000000, __u32)
73
74
#define LIRC_GET_SEND_MODE             _IOR('i', 0x00000001, __u32)
75
#define LIRC_GET_REC_MODE              _IOR('i', 0x00000002, __u32)
76
#define LIRC_GET_SEND_CARRIER          _IOR('i', 0x00000003, __u32)
77
#define LIRC_GET_REC_CARRIER           _IOR('i', 0x00000004, __u32)
78
#define LIRC_GET_SEND_DUTY_CYCLE       _IOR('i', 0x00000005, __u32)
79
#define LIRC_GET_REC_DUTY_CYCLE        _IOR('i', 0x00000006, __u32)
80
81
/* code length in bits, currently only for LIRC_MODE_LIRCCODE */
82
#define LIRC_GET_LENGTH                _IOR('i', 0x0000000f, __u32)
83
84
#define LIRC_SET_SEND_MODE             _IOW('i', 0x00000011, __u32)
85
#define LIRC_SET_REC_MODE              _IOW('i', 0x00000012, __u32)
86
/* Note: these can reset the according pulse_width */
87
#define LIRC_SET_SEND_CARRIER          _IOW('i', 0x00000013, __u32)
88
#define LIRC_SET_REC_CARRIER           _IOW('i', 0x00000014, __u32)
89
#define LIRC_SET_SEND_DUTY_CYCLE       _IOW('i', 0x00000015, __u32)
90
#define LIRC_SET_REC_DUTY_CYCLE        _IOW('i', 0x00000016, __u32)
91
92
/* to set a range use
93
   LIRC_SET_REC_DUTY_CYCLE_RANGE/LIRC_SET_REC_CARRIER_RANGE with the
94
   lower bound first and later
95
   LIRC_SET_REC_DUTY_CYCLE/LIRC_SET_REC_CARRIER with the upper bound */
96
97
#define LIRC_SET_REC_DUTY_CYCLE_RANGE  _IOW('i', 0x0000001e, __u32)
98
#define LIRC_SET_REC_CARRIER_RANGE     _IOW('i', 0x0000001f, __u32)
99
100
#define DEV_LIRC       "lirc"
101
#define IRCTL_DEV_MAJOR 61
102
103
#endif

Return to bug 45100