Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 47404
Collapse All | Expand All

(-)linux-2.6.2/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.5rc2.old/drivers/char/Kconfig (+2 lines)
Lines 599-604 Link Here
599
	bool "Support for console on line printer"
599
	bool "Support for console on line printer"
600
	depends on PC9800_OLDLP
600
	depends on PC9800_OLDLP
601
601
602
source "drivers/char/lirc/Kconfig"
603
602
config QIC02_TAPE
604
config QIC02_TAPE
603
	tristate "QIC-02 tape support"
605
	tristate "QIC-02 tape support"
604
	help
606
	help
(-)linux-2.6.2/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.2/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.2/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.2/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: 405_lirc_infrared-2.6.2-02092004.patch,v 1.1 2004/02/24 22:27:37 brad_mssw 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.2/drivers/char/lirc/lirc_bt829.c (+364 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_get_key (void* data, unsigned char* key, int key_no)
87
{
88
	int status;
89
	status = poll_main();
90
	*key = (status >> 8) & 0xFF;
91
	//  if ( status & 0xFF ) {
92
	//    printk(KERN_INFO "ATIR reading key %02X\n",*key);
93
	//  }
94
	return (status & 0xFF) ? 0 : -1;
95
}
96
97
int atir_set_use_inc(void* data)
98
{
99
	printk(KERN_DEBUG "ATIR driver is opened\n");
100
	return 0;
101
}
102
103
void atir_set_use_dec(void* data)
104
{
105
	printk(KERN_DEBUG "ATIR driver is closed\n");
106
}
107
108
static int __init lirc_bt829_init(void)
109
{
110
	if ( !do_pci_probe() ) {
111
		return 1;
112
	}
113
114
	if ( !atir_init_start() ) {
115
		return 1;
116
	}
117
118
	strcpy(atir_plugin.name,"ATIR");
119
	atir_plugin.minor       = -1;
120
	atir_plugin.code_length = 8;
121
	atir_plugin.sample_rate = 10;
122
	atir_plugin.data        = 0;
123
	atir_plugin.get_key     = atir_get_key;
124
	atir_plugin.set_use_inc = atir_set_use_inc;
125
	atir_plugin.set_use_dec = atir_set_use_dec;
126
127
	atir_minor = lirc_register_plugin(&atir_plugin);
128
	printk(KERN_DEBUG "ATIR driver is registered on minor %d\n",atir_minor);
129
130
	return 0;
131
}
132
133
134
static void __exit lirc_bt829_exit(void)
135
{
136
	lirc_unregister_plugin(atir_minor);
137
}
138
139
140
int atir_init_start(void)
141
{
142
	pci_addr_lin = (unsigned long)ioremap(pci_addr_phys + DATA_PCI_OFF,0x400);
143
	if ( pci_addr_lin == 0 ) {
144
		printk(KERN_INFO "atir: pci mem must be mapped\n");
145
		return 0;
146
	}
147
	return 1;
148
}
149
150
void cycle_delay(int cycle)
151
{
152
	udelay(WAIT_CYCLE*cycle);
153
}
154
155
156
int poll_main()
157
{
158
	unsigned char status_high, status_low;
159
160
	do_i2c_start();
161
162
	seems_wr_byte(0xAA);
163
	seems_wr_byte(0x01);
164
165
	do_i2c_start();
166
167
	seems_wr_byte(0xAB);
168
169
	status_low = seems_rd_byte();
170
	status_high = seems_rd_byte();
171
172
	do_i2c_stop();
173
174
	return (status_high << 8) | status_low;
175
}
176
177
void do_i2c_start(void)
178
{
179
	do_set_bits(3);
180
	cycle_delay(4);
181
182
	do_set_bits(1);
183
	cycle_delay(7);
184
185
	do_set_bits(0);
186
	cycle_delay(2);
187
}
188
189
void do_i2c_stop(void)
190
{
191
	unsigned char bits;
192
	bits =  do_get_bits() & 0xFD;
193
	do_set_bits(bits);
194
	cycle_delay(1);
195
196
	bits |= 1;
197
	do_set_bits(bits);
198
	cycle_delay(2);
199
200
	bits |= 2;
201
	do_set_bits(bits);
202
	bits = 3;
203
	do_set_bits(bits);
204
	cycle_delay(2);
205
}
206
207
208
void seems_wr_byte(unsigned char value)
209
{
210
	int i;
211
	unsigned char reg;
212
213
	reg = do_get_bits();
214
	for(i = 0;i < 8;i++) {
215
		if ( value & 0x80 ) {
216
			reg |= 0x02;
217
		} else {
218
			reg &= 0xFD;
219
		}
220
		do_set_bits(reg);
221
		cycle_delay(1);
222
223
		reg |= 1;
224
		do_set_bits(reg);
225
		cycle_delay(1);
226
227
		reg &= 0xFE;
228
		do_set_bits(reg);
229
		cycle_delay(1);
230
		value <<= 1;
231
	}
232
	cycle_delay(2);
233
234
	reg |= 2;
235
	do_set_bits(reg);
236
237
	reg |= 1;
238
	do_set_bits(reg);
239
240
	cycle_delay(1);
241
	do_get_bits();
242
243
	reg &= 0xFE;
244
	do_set_bits(reg);
245
	cycle_delay(3);
246
}
247
248
unsigned char seems_rd_byte(void)
249
{
250
	int i;
251
	int rd_byte;
252
	unsigned char bits_2, bits_1;
253
254
	bits_1 = do_get_bits() | 2;
255
	do_set_bits(bits_1);
256
257
	rd_byte = 0;
258
	for(i = 0;i < 8;i++) {
259
		bits_1 &= 0xFE;
260
		do_set_bits(bits_1);
261
		cycle_delay(2);
262
263
		bits_1 |= 1;
264
		do_set_bits(bits_1);
265
		cycle_delay(1);
266
267
		if ( (bits_2 = do_get_bits()) & 2 ) {
268
			rd_byte |= 1;
269
		}
270
		rd_byte <<= 1;
271
	}
272
273
	bits_1 = 0;
274
	if ( bits_2 == 0 ) {
275
		bits_1 |= 2;
276
	}
277
	do_set_bits(bits_1);
278
	cycle_delay(2);
279
280
	bits_1 |= 1;
281
	do_set_bits(bits_1);
282
	cycle_delay(3);
283
284
	bits_1 &= 0xFE;
285
	do_set_bits(bits_1);
286
	cycle_delay(2);
287
288
	rd_byte >>= 1;
289
	rd_byte &= 0xFF;
290
	return rd_byte;
291
}
292
293
void do_set_bits(unsigned char new_bits)
294
{
295
	int reg_val;
296
	reg_val = read_index(0x34);
297
	if ( new_bits & 2 ) {
298
		reg_val &= 0xFFFFFFDF;
299
		reg_val |= 1;
300
	} else {
301
		reg_val &= 0xFFFFFFFE;
302
		reg_val |= 0x20;
303
	}
304
	reg_val |= 0x10;
305
	write_index(0x34,reg_val);
306
307
	reg_val = read_index(0x31);
308
	if ( new_bits & 1 ) {
309
		reg_val |= 0x1000000;
310
	} else {
311
		reg_val &= 0xFEFFFFFF;
312
	}
313
	reg_val |= 0x8000000;
314
	write_index(0x31,reg_val);
315
}
316
317
unsigned char do_get_bits(void)
318
{
319
	unsigned char bits;
320
	int reg_val;
321
322
	reg_val = read_index(0x34);
323
	reg_val |= 0x10;
324
	reg_val &= 0xFFFFFFDF;
325
	write_index(0x34,reg_val);
326
327
	reg_val = read_index(0x34);
328
	bits = 0;
329
	if ( reg_val & 8 ) {
330
		bits |= 2;
331
	} else {
332
		bits &= 0xFD;
333
	}
334
	reg_val = read_index(0x31);
335
	if ( reg_val & 0x1000000 ) {
336
		bits |= 1;
337
	} else {
338
		bits &= 0xFE;
339
	}
340
	return bits;
341
}
342
343
unsigned int read_index(unsigned char index)
344
{
345
	unsigned int addr, value;
346
	//  addr = pci_addr_lin + DATA_PCI_OFF + ((index & 0xFF) << 2);
347
	addr = pci_addr_lin + ((index & 0xFF) << 2);
348
	value = readl(addr);
349
	return value;
350
}
351
352
void write_index(unsigned char index,unsigned int reg_val)
353
{
354
	unsigned int addr;
355
	addr = pci_addr_lin + ((index & 0xFF) << 2);
356
	writel(reg_val,addr);
357
}
358
359
MODULE_AUTHOR("Froenchenko Leonid");
360
MODULE_DESCRIPTION("IR remote driver for bt829 based TV cards");
361
MODULE_LICENSE("GPL");
362
363
module_init(lirc_bt829_init);
364
module_exit(lirc_bt829_exit);
(-)linux-2.6.2/drivers/char/lirc/lirc_dev.c (+713 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: 405_lirc_infrared-2.6.2-02092004.patch,v 1.1 2004/02/24 22:27:37 brad_mssw 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
	unsigned char buf[BUFLEN];
106
	unsigned int i;
107
108
	if (lirc_buffer_full(ir->buf)) {
109
		dprintk(LOGHEAD "buffer overflow\n",
110
			ir->p.name, ir->p.minor);
111
		return -EOVERFLOW;
112
	}
113
114
	for (i=0; i < ir->buf->chunk_size; i++) {
115
		if (ir->p.get_key(ir->p.data, &buf[i], i)) {
116
			return -ENODATA;
117
		}
118
		dprintk(LOGHEAD "remote code (0x%x) now in buffer\n",
119
			ir->p.name, ir->p.minor, buf[i]);
120
	}
121
122
	/* here is the only point at which we add key codes to the buffer */
123
	lirc_buffer_write_1(ir->buf, buf);
124
125
	return SUCCESS;
126
}
127
128
/* main function of the polling thread
129
 */
130
static int lirc_thread(void *irctl)
131
{
132
	struct irctl *ir = irctl;
133
134
	daemonize("lirc_dev");
135
136
	if (ir->t_notify != NULL) {
137
		up(ir->t_notify);
138
	}
139
140
	dprintk(LOGHEAD "poll thread started\n", ir->p.name, ir->p.minor);
141
142
	do {
143
		if (ir->open) {
144
			if (ir->jiffies_to_wait) {
145
				current->state = TASK_INTERRUPTIBLE;
146
				schedule_timeout(ir->jiffies_to_wait);
147
			} else {
148
				interruptible_sleep_on(ir->p.get_queue(ir->p.data));
149
			}
150
			if (ir->shutdown) {
151
				break;
152
			}
153
			if (!add_to_buf(ir)) {
154
				wake_up_interruptible(&ir->buf->wait_poll);
155
			}
156
		} else {
157
			/* if device not opened so we can sleep half a second */
158
			current->state = TASK_INTERRUPTIBLE;
159
			schedule_timeout(HZ/2);
160
		}
161
	} while (!ir->shutdown);
162
163
	dprintk(LOGHEAD "poll thread ended\n", ir->p.name, ir->p.minor);
164
165
	if (ir->t_notify2 != NULL) {
166
		down(ir->t_notify2);
167
	}
168
169
	ir->t_pid = -1;
170
171
	if (ir->t_notify != NULL) {
172
		up(ir->t_notify);
173
	}
174
175
	return 0;
176
}
177
178
/*
179
 *
180
 */
181
int lirc_register_plugin(struct lirc_plugin *p)
182
{
183
	struct irctl *ir;
184
	int minor;
185
	int bytes_in_key;
186
	DECLARE_MUTEX_LOCKED(tn);
187
188
	if (!p) {
189
		printk("lirc_dev: lirc_register_plugin:"
190
		       "plugin pointer must be not NULL!\n");
191
		return -EBADRQC;
192
	}
193
194
	if (CONFIG_LIRC_MAX_DEV <= p->minor) {
195
		printk("lirc_dev: lirc_register_plugin:"
196
		       "\" minor\" must be beetween 0 and %d (%d)!\n",
197
		       CONFIG_LIRC_MAX_DEV-1, p->minor);
198
		return -EBADRQC;
199
	}
200
201
	if (1 > p->code_length || (BUFLEN*8) < p->code_length) {
202
		printk("lirc_dev: lirc_register_plugin:"
203
		       "code length in bits for minor (%d) "
204
		       "must be less than %d!\n",
205
		       p->minor, BUFLEN*8);
206
		return -EBADRQC;
207
	}
208
209
	printk("lirc_dev: lirc_register_plugin:"
210
	       "sample_rate: %d\n",p->sample_rate);
211
	if (p->sample_rate) {
212
		if (2 > p->sample_rate || 50 < p->sample_rate) {
213
			printk("lirc_dev: lirc_register_plugin:"
214
			       "sample_rate must be beetween 2 and 50!\n");
215
			return -EBADRQC;
216
		}
217
	} else if (!(p->fops && p->fops->read)
218
			&& !p->get_queue && !p->rbuf) {
219
		printk("lirc_dev: lirc_register_plugin:"
220
		       "fops->read, get_queue and rbuf cannot all be NULL!\n");
221
		return -EBADRQC;
222
	} else if (!p->get_queue && !p->rbuf) {
223
		if (!(p->fops && p->fops->read && p->fops->poll)
224
				|| (!p->fops->ioctl && !p->ioctl)) {
225
			printk("lirc_dev: lirc_register_plugin:"
226
			       "neither read, poll nor ioctl can be NULL!\n");
227
			return -EBADRQC;
228
		}
229
	}
230
231
	down_interruptible(&plugin_lock);
232
233
	minor = p->minor;
234
235
	if (0 > minor) {
236
		/* find first free slot for plugin */
237
		for (minor=0; minor<CONFIG_LIRC_MAX_DEV; minor++)
238
			if (irctls[minor].p.minor == NOPLUG)
239
				break;
240
		if (CONFIG_LIRC_MAX_DEV == minor) {
241
			printk("lirc_dev: lirc_register_plugin: "
242
			       "no free slots for plugins!\n");
243
			up(&plugin_lock);
244
			return -ENOMEM;
245
		}
246
	} else if (irctls[minor].p.minor != NOPLUG) {
247
		printk("lirc_dev: lirc_register_plugin:"
248
		       "minor (%d) just registerd!\n", minor);
249
		up(&plugin_lock);
250
		return -EBUSY;
251
	}
252
253
	ir = &irctls[minor];
254
255
	if (p->sample_rate) {
256
		ir->jiffies_to_wait = HZ / p->sample_rate;
257
	} else {
258
                /* it means - wait for externeal event in task queue */
259
		ir->jiffies_to_wait = 0;
260
	}
261
262
	/* some safety check 8-) */
263
	p->name[sizeof(p->name)-1] = '\0';
264
265
	bytes_in_key = p->code_length/8 + (p->code_length%8 ? 1 : 0);
266
267
	if (p->rbuf) {
268
		ir->buf = p->rbuf;
269
	} else {
270
		ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
271
		lirc_buffer_init(ir->buf, bytes_in_key, BUFLEN/bytes_in_key);
272
	}
273
274
	if (p->features==0)
275
		p->features = (p->code_length > 8) ?
276
			LIRC_CAN_REC_LIRCCODE : LIRC_CAN_REC_CODE;
277
278
	ir->p = *p;
279
	ir->p.minor = minor;
280
281
#ifdef CONFIG_DEVFS_FS
282
	devfs_mk_cdev(MKDEV(IRCTL_DEV_MAJOR, ir->p.minor), S_IFCHR | S_IRUSR | S_IWUSR, "lirc/lirc%d", ir->p.minor);
283
#endif
284
285
	if(p->sample_rate || p->get_queue) {
286
		/* try to fire up polling thread */
287
		ir->t_notify = &tn;
288
		ir->t_pid = kernel_thread(lirc_thread, (void*)ir, 0);
289
		if (ir->t_pid < 0) {
290
			up(&plugin_lock);
291
			printk("lirc_dev: lirc_register_plugin:"
292
			       "cannot run poll thread for minor = %d\n",
293
			       p->minor);
294
			return -ECHILD;
295
		}
296
		down(&tn);
297
		ir->t_notify = NULL;
298
	}
299
	up(&plugin_lock);
300
301
	try_module_get(THIS_MODULE);
302
303
	dprintk("lirc_dev: plugin %s registered at minor number = %d\n",
304
		ir->p.name, ir->p.minor);
305
306
	return minor;
307
}
308
309
/*
310
 *
311
 */
312
int lirc_unregister_plugin(int minor)
313
{
314
	struct irctl *ir;
315
	DECLARE_MUTEX_LOCKED(tn);
316
	DECLARE_MUTEX_LOCKED(tn2);
317
318
	if (minor < 0 || minor >= CONFIG_LIRC_MAX_DEV) {
319
		printk("lirc_dev: lirc_unregister_plugin:"
320
		       "\" minor\" must be beetween 0 and %d!\n",
321
		       CONFIG_LIRC_MAX_DEV-1);
322
		return -EBADRQC;
323
	}
324
325
	ir = &irctls[minor];
326
327
	down_interruptible(&plugin_lock);
328
329
	if (ir->p.minor != minor) {
330
		printk("lirc_dev: lirc_unregister_plugin:"
331
		       "minor (%d) device not registered!", minor);
332
		up(&plugin_lock);
333
		return -ENOENT;
334
	}
335
336
	if (ir->open) {
337
		printk("lirc_dev: lirc_unregister_plugin:"
338
		       "plugin %s[%d] in use!", ir->p.name, ir->p.minor);
339
		up(&plugin_lock);
340
		return -EBUSY;
341
	}
342
343
	/* end up polling thread */
344
	if (ir->t_pid >= 0) {
345
		ir->t_notify = &tn;
346
		ir->t_notify2 = &tn2;
347
		ir->shutdown = 1;
348
		{
349
			struct task_struct *p;
350
351
			p = find_task_by_pid(ir->t_pid);
352
			wake_up_process(p);
353
		}
354
		up(&tn2);
355
		down(&tn);
356
		ir->t_notify = NULL;
357
		ir->t_notify2 = NULL;
358
	}
359
360
	dprintk("lirc_dev: plugin %s unregistered from minor number = %d\n",
361
		ir->p.name, ir->p.minor);
362
363
#ifdef CONFIG_DEVFS_FS
364
	devfs_remove("lirc/lirc%d", ir->p.minor);
365
#endif
366
367
	if (ir->buf != ir->p.rbuf){
368
		lirc_buffer_free(ir->buf);
369
		kfree(ir->buf);
370
	}
371
	ir->buf = NULL;
372
	init_irctl(ir);
373
	up(&plugin_lock);
374
375
	module_put(THIS_MODULE);
376
377
	return SUCCESS;
378
}
379
380
/*
381
 *
382
 */
383
static int irctl_open(struct inode *inode, struct file *file)
384
{
385
	struct irctl *ir;
386
	int retval;
387
388
	if (MINOR(inode->i_rdev) >= CONFIG_LIRC_MAX_DEV) {
389
		dprintk("lirc_dev [%d]: open result = -ENODEV\n",
390
			MINOR(inode->i_rdev));
391
		return -ENODEV;
392
	}
393
394
	ir = &irctls[MINOR(inode->i_rdev)];
395
396
	dprintk(LOGHEAD "open called\n", ir->p.name, ir->p.minor);
397
398
	/* if the plugin has an open function use it instead */
399
	if(ir->p.fops && ir->p.fops->open)
400
		return ir->p.fops->open(inode, file);
401
402
	down_interruptible(&plugin_lock);
403
404
	if (ir->p.minor == NOPLUG) {
405
		up(&plugin_lock);
406
		dprintk(LOGHEAD "open result = -ENODEV\n",
407
			ir->p.name, ir->p.minor);
408
		return -ENODEV;
409
	}
410
411
	if (ir->open) {
412
		up(&plugin_lock);
413
		dprintk(LOGHEAD "open result = -EBUSY\n",
414
			ir->p.name, ir->p.minor);
415
		return -EBUSY;
416
	}
417
418
	/* there is no need for locking here because ir->open is 0
419
         * and lirc_thread isn't using buffer
420
	 * plugins which use irq's should allocate them on set_use_inc,
421
	 * so there should be no problem with those either.
422
         */
423
	ir->buf->head = ir->buf->tail;
424
	ir->buf->fill = 0;
425
426
	++ir->open;
427
	retval = ir->p.set_use_inc(ir->p.data);
428
429
	up(&plugin_lock);
430
431
	if (retval != SUCCESS) {
432
		--ir->open;
433
		return retval;
434
	}
435
436
	dprintk(LOGHEAD "open result = %d\n", ir->p.name, ir->p.minor, SUCCESS);
437
438
	return SUCCESS;
439
}
440
441
/*
442
 *
443
 */
444
static int irctl_close(struct inode *inode, struct file *file)
445
{
446
	struct irctl *ir = &irctls[MINOR(inode->i_rdev)];
447
448
	dprintk(LOGHEAD "close called\n", ir->p.name, ir->p.minor);
449
450
	/* if the plugin has a close function use it instead */
451
	if(ir->p.fops && ir->p.fops->release)
452
		return ir->p.fops->release(inode, file);
453
454
	down_interruptible(&plugin_lock);
455
456
	--ir->open;
457
	ir->p.set_use_dec(ir->p.data);
458
459
	up(&plugin_lock);
460
461
	return SUCCESS;
462
}
463
464
/*
465
 *
466
 */
467
static unsigned int irctl_poll(struct file *file, poll_table *wait)
468
{
469
	struct irctl *ir = &irctls[MINOR(file->f_dentry->d_inode->i_rdev)];
470
471
	dprintk(LOGHEAD "poll called\n", ir->p.name, ir->p.minor);
472
473
	/* if the plugin has a poll function use it instead */
474
	if(ir->p.fops && ir->p.fops->poll)
475
		return ir->p.fops->poll(file, wait);
476
477
	poll_wait(file, &ir->buf->wait_poll, wait);
478
479
	dprintk(LOGHEAD "poll result = %s\n",
480
		ir->p.name, ir->p.minor,
481
		lirc_buffer_empty(ir->buf) ? "0" : "POLLIN|POLLRDNORM");
482
483
	return lirc_buffer_empty(ir->buf) ? 0 : (POLLIN|POLLRDNORM);
484
}
485
486
/*
487
 *
488
 */
489
static int irctl_ioctl(struct inode *inode, struct file *file,
490
                       unsigned int cmd, unsigned long arg)
491
{
492
	unsigned long mode;
493
	int result;
494
	struct irctl *ir = &irctls[MINOR(inode->i_rdev)];
495
496
	dprintk(LOGHEAD "ioctl called (%u)\n",
497
		ir->p.name, ir->p.minor, cmd);
498
499
	/* if the plugin has a ioctl function use it instead */
500
	if(ir->p.fops && ir->p.fops->ioctl)
501
		return ir->p.fops->ioctl(inode, file, cmd, arg);
502
503
	if (ir->p.minor == NOPLUG) {
504
		dprintk(LOGHEAD "ioctl result = -ENODEV\n",
505
			ir->p.name, ir->p.minor);
506
		return -ENODEV;
507
	}
508
509
	/* Give the plugin a chance to handle the ioctl */
510
	if(ir->p.ioctl){
511
		result = ir->p.ioctl(inode, file, cmd, arg);
512
		if (result != -ENOIOCTLCMD)
513
			return result;
514
	}
515
	/* The plugin can't handle cmd */
516
	result = SUCCESS;
517
518
	switch(cmd)
519
	{
520
	case LIRC_GET_FEATURES:
521
		result = put_user(ir->p.features, (unsigned long*)arg);
522
		break;
523
	case LIRC_GET_REC_MODE:
524
		if(!(ir->p.features&LIRC_CAN_REC_MASK))
525
			return(-ENOSYS);
526
527
		result = put_user(LIRC_REC2MODE
528
				  (ir->p.features&LIRC_CAN_REC_MASK),
529
				  (unsigned long*)arg);
530
		break;
531
	case LIRC_SET_REC_MODE:
532
		if(!(ir->p.features&LIRC_CAN_REC_MASK))
533
			return(-ENOSYS);
534
535
		result = get_user(mode, (unsigned long*)arg);
536
		if(!result && !(LIRC_MODE2REC(mode) & ir->p.features)) {
537
			result = -EINVAL;
538
		}
539
		/* FIXME: We should actually set the mode somehow
540
		 * but for now, lirc_serial doesn't support mode changin
541
		 * eighter */
542
		break;
543
	case LIRC_GET_LENGTH:
544
		result = put_user((unsigned long)ir->p.code_length,
545
				  (unsigned long *)arg);
546
		break;
547
	default:
548
		result = -ENOIOCTLCMD;
549
	}
550
551
	dprintk(LOGHEAD "ioctl result = %d\n",
552
		ir->p.name, ir->p.minor, result);
553
554
	return result;
555
}
556
557
/*
558
 *
559
 */
560
static ssize_t irctl_read(struct file *file,
561
			  char *buffer,
562
			  size_t length,
563
			  loff_t *ppos)
564
{
565
	struct irctl *ir = &irctls[MINOR(file->f_dentry->d_inode->i_rdev)];
566
	unsigned char buf[ir->buf->chunk_size];
567
	int ret=0, written=0;
568
	DECLARE_WAITQUEUE(wait, current);
569
570
	dprintk(LOGHEAD "read called\n", ir->p.name, ir->p.minor);
571
572
	/* if the plugin has a specific read function use it instead */
573
	if(ir->p.fops && ir->p.fops->read)
574
		return ir->p.fops->read(file, buffer, length, ppos);
575
576
	if (length % ir->buf->chunk_size) {
577
		dprintk(LOGHEAD "read result = -EINVAL\n",
578
			ir->p.name, ir->p.minor);
579
		return -EINVAL;
580
	}
581
582
	/* we add ourselves to the task queue before buffer check
583
         * to avoid losing scan code (in case when queue is awaken somewhere
584
	 * beetwen while condition checking and scheduling)
585
	 */
586
	add_wait_queue(&ir->buf->wait_poll, &wait);
587
	current->state = TASK_INTERRUPTIBLE;
588
589
	/* while we did't provide 'length' bytes, device is opened in blocking
590
	 * mode and 'copy_to_user' is happy, wait for data.
591
	 */
592
	while (written < length && ret == 0) {
593
		if (lirc_buffer_empty(ir->buf)) {
594
			/* According to the read(2) man page, 'written' can be
595
			 * returned as less than 'length', instead of blocking
596
			 * again, returning -EWOULDBLOCK, or returning
597
			 * -ERESTARTSYS */
598
			if (written) break;
599
			if (file->f_flags & O_NONBLOCK) {
600
				dprintk(LOGHEAD "read result = -EWOULDBLOCK\n",
601
						ir->p.name, ir->p.minor);
602
				remove_wait_queue(&ir->buf->wait_poll, &wait);
603
				current->state = TASK_RUNNING;
604
				return -EWOULDBLOCK;
605
			}
606
			if (signal_pending(current)) {
607
				dprintk(LOGHEAD "read result = -ERESTARTSYS\n",
608
						ir->p.name, ir->p.minor);
609
				remove_wait_queue(&ir->buf->wait_poll, &wait);
610
				current->state = TASK_RUNNING;
611
				return -ERESTARTSYS;
612
			}
613
			schedule();
614
			current->state = TASK_INTERRUPTIBLE;
615
		} else {
616
			lirc_buffer_read_1(ir->buf, buf);
617
			ret = copy_to_user((void *)buffer+written, buf,
618
					   ir->buf->chunk_size);
619
			written += ir->buf->chunk_size;
620
		}
621
	}
622
623
	remove_wait_queue(&ir->buf->wait_poll, &wait);
624
	current->state = TASK_RUNNING;
625
626
	dprintk(LOGHEAD "read result = %s (%d)\n",
627
		ir->p.name, ir->p.minor, ret ? "-EFAULT" : "OK", ret);
628
629
	return ret ? -EFAULT : written;
630
}
631
632
static ssize_t irctl_write(struct file *file, const char *buffer,
633
			   size_t length, loff_t * ppos)
634
{
635
	struct irctl *ir = &irctls[MINOR(file->f_dentry->d_inode->i_rdev)];
636
637
	dprintk(LOGHEAD "write called\n", ir->p.name, ir->p.minor);
638
639
	/* if the plugin has a specific read function use it instead */
640
	if(ir->p.fops && ir->p.fops->write)
641
		return ir->p.fops->write(file, buffer, length, ppos);
642
643
	return -EINVAL;
644
}
645
646
647
static struct file_operations fops = {
648
	read:    irctl_read,
649
	write:   irctl_write,
650
	poll:    irctl_poll,
651
	ioctl:   irctl_ioctl,
652
	open:    irctl_open,
653
	release: irctl_close
654
};
655
656
static int __init lirc_dev_init(void)
657
{
658
	int i;
659
660
	for (i=0; i < CONFIG_LIRC_MAX_DEV; ++i) {
661
		init_irctl(&irctls[i]);
662
	}
663
664
	i = register_chrdev(IRCTL_DEV_MAJOR,
665
				   IRCTL_DEV_NAME,
666
				   &fops);
667
668
	if (i < 0) {
669
		printk ("lirc_dev: device registration failed with %d\n", i);
670
		return i;
671
	}
672
673
	printk("lirc_dev: IR Remote Control driver registered, at major %d \n",
674
	       IRCTL_DEV_MAJOR);
675
676
	return SUCCESS;
677
}
678
679
static void __exit lirc_dev_exit(void)
680
{
681
	int ret;
682
683
	ret = unregister_chrdev(IRCTL_DEV_MAJOR, IRCTL_DEV_NAME);
684
685
	if (0 > ret){
686
		printk("lirc_dev: error in module_unregister_chrdev: %d\n",
687
		       ret);
688
	} else {
689
		dprintk("lirc_dev: module successfully unloaded\n");
690
	}
691
}
692
693
/* ---------------------------------------------------------------------- */
694
695
/* For now dont try to use it as a static version !  */
696
697
MODULE_DESCRIPTION("LIRC base driver module");
698
MODULE_AUTHOR("Artur Lipowski");
699
MODULE_LICENSE("GPL");
700
701
EXPORT_SYMBOL(lirc_register_plugin);
702
EXPORT_SYMBOL(lirc_unregister_plugin);
703
704
module_init(lirc_dev_init);
705
module_exit(lirc_dev_exit);
706
707
/*
708
 * Overrides for Emacs so that we follow Linus's tabbing style.
709
 * ---------------------------------------------------------------------------
710
 * Local variables:
711
 * c-basic-offset: 8
712
 * End:
713
 */
(-)linux-2.6.2/drivers/char/lirc/lirc_dev.h (+206 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: 405_lirc_infrared-2.6.2-02092004.patch,v 1.1 2004/02/24 22:27:37 brad_mssw 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
extern inline void lirc_buffer_lock(struct lirc_buffer *buf, unsigned long *flags)
71
{
72
	spin_lock_irqsave(&buf->lock, *flags);
73
}
74
extern inline void lirc_buffer_unlock(struct lirc_buffer *buf, unsigned long *flags)
75
{
76
	spin_unlock_irqrestore(&buf->lock, *flags);
77
}
78
static inline void _lirc_buffer_remove_1(struct lirc_buffer *buf)
79
{
80
	buf->head = mod(buf->head+1, buf->size);
81
	buf->fill -= 1;
82
}
83
static inline void lirc_buffer_remove_1(struct lirc_buffer *buf)
84
{
85
	unsigned long flags;
86
	lirc_buffer_lock(buf, &flags);
87
	_lirc_buffer_remove_1(buf);
88
	lirc_buffer_unlock(buf, &flags);
89
}
90
static inline void _lirc_buffer_read_1(struct lirc_buffer *buf,
91
				     unsigned char *dest)
92
{
93
	memcpy(dest, &buf->data[buf->head*buf->chunk_size], buf->chunk_size);
94
	buf->head = mod(buf->head+1, buf->size);
95
	buf->fill -= 1;
96
}
97
static inline void lirc_buffer_read_1(struct lirc_buffer *buf,
98
				      unsigned char *dest)
99
{
100
	unsigned long flags;
101
	lirc_buffer_lock(buf, &flags);
102
	_lirc_buffer_read_1(buf, dest);
103
	lirc_buffer_unlock(buf, &flags);
104
}
105
static inline void _lirc_buffer_write_1(struct lirc_buffer *buf,
106
				      unsigned char *orig)
107
{
108
	memcpy(&buf->data[buf->tail*buf->chunk_size], orig, buf->chunk_size);
109
	buf->tail = mod(buf->tail+1, buf->size);
110
	buf->fill++;
111
}
112
static inline void lirc_buffer_write_1(struct lirc_buffer *buf,
113
				       unsigned char *orig)
114
{
115
	unsigned long flags;
116
	lirc_buffer_lock(buf, &flags);
117
	_lirc_buffer_write_1(buf, orig);
118
	lirc_buffer_unlock(buf, &flags);
119
}
120
121
struct lirc_plugin
122
{
123
     char name[40];
124
     int minor;
125
     int code_length;
126
     int sample_rate;
127
     unsigned long features;
128
     void* data;
129
     int (*get_key) (void* data, unsigned char* key, int key_no);
130
     wait_queue_head_t* (*get_queue) (void* data);
131
     struct lirc_buffer *rbuf;
132
     int (*set_use_inc) (void* data);
133
     void (*set_use_dec) (void* data);
134
     int (*ioctl) (struct inode *,struct file *,unsigned int, unsigned long);
135
     struct file_operations *fops;
136
};
137
/* name:
138
 * this string will be used for logs
139
 *
140
 * minor:
141
 * indicates minor device (/dev/lircd) number for registered plugin
142
 * if caller fills it with negative value, then the first free minor
143
 * number will be used (if available)
144
 *
145
 * code_length:
146
 * length ofthe  remote control key code expressed in bits
147
 * if code_length > 8 then many bytes are returned through the device read
148
 * in such situation get_key should return key code values starting
149
 * from most significant byte (device read will preseve this order)
150
 * in addition if code_length > 8 then get_key will be called
151
 * several (ceil(code_length/8)) times in one pool pass (or after task queue
152
 * awake) key_no parameter denotes number of the requested byte (0 means first
153
 * byte)
154
 *
155
 * sample_rate:
156
 * sample_rate equal to 0 means that no pooling will be performed and get_key
157
 * will be triggered by external events (through task queue returned by
158
 * get_queue)
159
 *
160
 * data:
161
 * it may point to any plugin data and this pointer will be passed to all
162
 * callback functions
163
 *
164
 * get_key:
165
 * get_key will be called after specified period of the time or triggered by the
166
 * external event, this behavior depends on value of the sample_rate
167
 * this function will be called in user context
168
 *
169
 * get_queue:
170
 * this callback should return a pointer to the task queue which will be used
171
 * for external event waiting
172
 *
173
 * rbuf:
174
 * if not NULL, it will be used as a read buffer, you will have to write to
175
 * the buffer by other means, like irq's (see also lirc_serial.c).
176
 *
177
 * set_use_inc:
178
 * set_use_inc will be called after device is opened
179
 *
180
 * set_use_dec:
181
 * set_use_dec will be called after device is closed
182
 *
183
 * ioctl:
184
 * Some ioctl's can be directly handled by lirc_dev but will be forwared here
185
 * if not NULL and only handled if it returns -ENOIOCTLCMD (see also
186
 * lirc_serial.c).
187
 *
188
 * fops:
189
 * file_operations for drivers which don't fit the current plugin model.
190
 */
191
192
193
/* following functions can be called ONLY from user context
194
 *
195
 * returns negative value on error or minor number
196
 * of the registered device if success
197
 * contens of the structure pointed by p is copied
198
 */
199
extern int lirc_register_plugin(struct lirc_plugin *p);
200
201
/* returns negative value on error or 0 if success
202
*/
203
extern int lirc_unregister_plugin(int minor);
204
205
206
#endif
(-)linux-2.6.2/drivers/char/lirc/lirc_gpio.c (+550 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: 405_lirc_infrared-2.6.2-02092004.patch,v 1.1 2004/02/24 22:27:37 brad_mssw 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
static int get_key(void* data, unsigned char *key, int key_no)
297
{
298
	static unsigned long next_time = 0;
299
	static unsigned char codes[MAX_BYTES];
300
	unsigned long code = 0;
301
	unsigned char cur_codes[MAX_BYTES];
302
303
	if (key_no > 0)	{
304
		if (code_bytes < 2 || key_no >= code_bytes) {
305
			dprintk(LOGHEAD "something wrong in get_key\n", card);
306
			return -EBADRQC;
307
		}
308
		*key = codes[key_no];
309
		return SUCCESS;
310
	}
311
312
	if (bttv_read_gpio(card, &code)) {
313
		dprintk(LOGHEAD "cannot read GPIO\n", card);
314
		return -EIO;
315
	}
316
317
	if (build_key(code, cur_codes)) {
318
		return -EFAULT;
319
	}
320
321
	if (soft_gap) {
322
		if (!memcmp(codes, cur_codes, code_bytes) &&
323
		    jiffies < next_time) {
324
			return -EAGAIN;
325
		}
326
		next_time = jiffies + soft_gap;
327
	}
328
329
	memcpy(codes, cur_codes, code_bytes);
330
331
	*key = codes[0];
332
333
	return SUCCESS;
334
}
335
336
static int set_use_inc(void* data)
337
{
338
	try_module_get(THIS_MODULE);
339
	return 0;
340
}
341
342
static void set_use_dec(void* data)
343
{
344
	module_put(THIS_MODULE);
345
}
346
347
static wait_queue_head_t* get_queue(void* data)
348
{
349
	return bttv_get_gpio_queue(card);
350
}
351
352
static struct lirc_plugin plugin = {
353
	.name		= "lirc_gpio  ",
354
	.get_key	= get_key,
355
	.get_queue	= get_queue,
356
	.set_use_inc	= set_use_inc,
357
	.set_use_dec	= set_use_dec,
358
};
359
360
/*
361
 *
362
 */
363
int gpio_remote_init(void)
364
{
365
	int ret;
366
	unsigned int mask;
367
368
	/* "normalize" gpio_mask
369
	 * this means shift it right until first bit is set
370
	 */
371
	while (!(gpio_mask & 1u)) {
372
		gpio_pre_shift++;
373
		gpio_mask >>= 1;
374
	}
375
376
	if (code_length) {
377
		plugin.code_length = code_length;
378
	} else {
379
		/* calculate scan code length in bits if needed */
380
		plugin.code_length = 1;
381
		mask = gpio_mask >> 1;
382
		while (mask) {
383
			if (mask & 1u) {
384
				plugin.code_length++;
385
			}
386
			mask >>= 1;
387
		}
388
	}
389
390
	code_bytes = (plugin.code_length/8) + (plugin.code_length%8 ? 1 : 0);
391
	if (MAX_BYTES < code_bytes) {
392
		printk (LOGHEAD "scan code too long (%d bytes)\n",
393
			minor, code_bytes);
394
		return -EBADRQC;
395
	}
396
397
	if (gpio_enable) {
398
		if(bttv_gpio_enable(card, gpio_enable, gpio_enable)) {
399
			printk(LOGHEAD "gpio_enable failure\n", minor);
400
			return -EIO;
401
		}
402
	}
403
404
405
	/* translate ms to jiffies */
406
	soft_gap = (soft_gap*HZ) / 1000;
407
408
	plugin.minor = minor;
409
	plugin.sample_rate = sample_rate;
410
411
	ret = lirc_register_plugin(&plugin);
412
413
	if (0 > ret) {
414
		printk (LOGHEAD "device registration failed with %d\n",
415
			minor, ret);
416
		return ret;
417
	}
418
419
	minor = ret;
420
	printk(LOGHEAD "driver registered\n", minor);
421
422
	return SUCCESS;
423
}
424
425
static int __init lirc_gpio_init(void)
426
{
427
	int type,cardid,card_type;
428
429
	if (CONFIG_LIRC_MAX_DEV < minor) {
430
		printk("lirc_gpio: parameter minor (%d) must be less than %d!\n",
431
		       minor, CONFIG_LIRC_MAX_DEV-1);
432
		return -EBADRQC;
433
	}
434
435
	request_module("bttv");
436
437
	/* if gpio_mask not zero then use module parameters
438
	 * instead of autodetecting TV card
439
	 */
440
	if (gpio_mask) {
441
		if (sample_rate!=0 && (2 > sample_rate || 50 < sample_rate)) {
442
			printk(LOGHEAD "parameter sample_rate "
443
			       "must be beetween 2 and 50!\n", minor);
444
			return -EBADRQC;
445
		}
446
447
		if (sample_rate!=0 && soft_gap &&
448
		    ((2000/sample_rate) > soft_gap || 1000 < soft_gap)) {
449
			printk(LOGHEAD "parameter soft_gap "
450
			       "must be beetween %d and 1000!\n",
451
			       minor, 2000/sample_rate);
452
			return -EBADRQC;
453
		}
454
	} else {
455
		if(bttv_get_cardinfo(card,&type,&cardid)==-1) {
456
			printk(LOGHEAD "could not get card type\n", minor);
457
		}
458
		printk(LOGHEAD "card type 0x%x, id 0x%x\n",minor,
459
		       type,cardid);
460
461
		if (type == BTTV_UNKNOWN) {
462
			printk(LOGHEAD "cannot detect TV card nr %d!\n",
463
			       minor, card);
464
			return -EBADRQC;
465
		}
466
		for (card_type = 1;
467
		     card_type < sizeof(rcv_infos)/sizeof(struct rcv_info);
468
		     card_type++) {
469
			if (rcv_infos[card_type].bttv_id == type &&
470
			    (rcv_infos[card_type].card_id == 0 ||
471
			     rcv_infos[card_type].card_id == cardid)) {
472
				bttv_id = rcv_infos[card_type].bttv_id;
473
				gpio_mask = rcv_infos[card_type].gpio_mask;
474
				gpio_enable = rcv_infos[card_type].gpio_enable;
475
				gpio_lock_mask = rcv_infos[card_type].gpio_lock_mask;
476
				gpio_xor_mask = rcv_infos[card_type].gpio_xor_mask;
477
				soft_gap = rcv_infos[card_type].soft_gap;
478
				sample_rate = rcv_infos[card_type].sample_rate;
479
				code_length = rcv_infos[card_type].code_length;
480
				break;
481
			}
482
		}
483
		if (type==BTTV_AVPHONE98 && cardid==0x00011461)	{
484
			bttv_id = BTTV_AVERMEDIA98;
485
		}
486
		if (type==BTTV_AVERMEDIA98 && cardid==0x00041461) {
487
			bttv_id = BTTV_AVPHONE98;
488
		}
489
		if (type==BTTV_AVERMEDIA98 && cardid==0x03001461) {
490
			bttv_id = BTTV_AVPHONE98;
491
		}
492
		if (type==BTTV_AVERMEDIA98 && cardid==0x00000000) {
493
			bttv_id = BTTV_AVPHONE98;
494
		}
495
		if (card_type == sizeof(rcv_infos)/sizeof(struct rcv_info)) {
496
			printk(LOGHEAD "TV card type 0x%x not supported!\n",
497
			       minor, type);
498
			return -EBADRQC;
499
		}
500
	}
501
502
	request_module("lirc_dev");
503
504
	return gpio_remote_init();
505
}
506
507
void __exit lirc_gpio_exit(void)
508
{
509
	int ret;
510
511
	ret = lirc_unregister_plugin(minor);
512
513
	if (0 > ret) {
514
		printk(LOGHEAD "error in lirc_unregister_minor: %d\n"
515
		       "Trying again...\n",
516
		       minor, ret);
517
518
		current->state = TASK_INTERRUPTIBLE;
519
		schedule_timeout(HZ);
520
521
		ret = lirc_unregister_plugin(minor);
522
523
		if (0 > ret) {
524
			printk(LOGHEAD "error in lirc_unregister_minor: %d!!!\n",
525
			       minor, ret);
526
			return;
527
		}
528
	}
529
530
	dprintk(LOGHEAD "module successfully unloaded\n", minor);
531
}
532
533
MODULE_DESCRIPTION("Driver module for remote control (data from bt848 GPIO port)");
534
MODULE_AUTHOR("Artur Lipowski");
535
MODULE_LICENSE("GPL");
536
537
#ifdef MODULE
538
	module_init(lirc_gpio_init);
539
#else
540
	late_initcall(lirc_gpio_init);
541
#endif
542
module_exit(lirc_gpio_exit);
543
544
/*
545
 * Overrides for Emacs so that we follow Linus's tabbing style.
546
 * ---------------------------------------------------------------------------
547
 * Local variables:
548
 * c-basic-offset: 8
549
 * End:
550
 */
(-)linux-2.6.2/drivers/char/lirc/lirc_i2c.c (+444 lines)
Line 0 Link Here
1
/*      $Id: 405_lirc_infrared-2.6.2-02092004.patch,v 1.1 2004/02/24 22:27:37 brad_mssw 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
101
/* ----------------------------------------------------------------------- */
102
103
static inline int reverse(int data, int bits)
104
{
105
	int i;
106
	int c;
107
	
108
	for (c=0,i=0; i<bits; i++) {
109
		c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
110
	}
111
112
	return c;
113
}
114
115
static int get_key_pcf8574(void* data, unsigned char* key, int key_no)
116
{
117
	struct i2c_ir *ir = data;
118
	int rc;
119
	unsigned char all, mask;
120
121
	/* compute all valid bits (key code + pressed/release flag) */
122
	all = ir->bits | ir->flag;
123
124
	/* save IR writable mask bits */
125
	mask = i2c_smbus_read_byte(&ir->client) & ~all;
126
127
	/* send bit mask */
128
	rc = i2c_smbus_write_byte(&ir->client, (0xff & all) | mask);
129
130
	/* receive scan code */
131
	rc = i2c_smbus_read_byte(&ir->client);
132
133
	if (rc == -1) {
134
		dprintk(DRIVER_NAME ": %s read error\n", ir->client.name);
135
		return -1;
136
	}
137
138
	/* drop duplicate polls */
139
	if (ir->b[0] == (rc & all)) {
140
		return -1;
141
	}
142
	ir->b[0] = rc & all;
143
144
	dprintk(DRIVER_NAME ": %s key 0x%02X %s\n",
145
		ir->client.name, rc & ir->bits,
146
		(rc & ir->flag) ? "released" : "pressed");
147
148
	if (rc & ir->flag) {
149
		/* ignore released buttons */
150
		return -1;
151
	}
152
153
	/* return valid key code */
154
	*key  = rc & ir->bits;
155
	return 0;
156
}
157
158
static int get_key_haup(void* data, unsigned char* key, int key_no)
159
{
160
	struct i2c_ir *ir = data;
161
        unsigned char buf[3];
162
	__u16 code;
163
164
	if (ir->nextkey != -1) {
165
		/* pass second byte */
166
		*key = ir->nextkey;
167
		ir->nextkey = -1;
168
		return 0;
169
	}
170
171
	/* poll IR chip */
172
	if (3 == i2c_master_recv(&ir->client,buf,3)) {
173
		ir->b[0] = buf[0];
174
		ir->b[1] = buf[1];
175
		ir->b[2] = buf[2];
176
	} else {
177
		dprintk(DRIVER_NAME ": read error\n");
178
		/* keep last successfull read buffer */
179
	}
180
181
	/* key pressed ? */
182
	if ((ir->b[0] & 0x80) == 0)
183
		return -1;
184
	
185
	dprintk(DRIVER_NAME ": key (0x%02x/0x%02x)\n",
186
	ir->b[0], ir->b[1]);
187
188
	/* look what we have */
189
	code = (((__u16)ir->b[0]&0x7f)<<6) | (ir->b[1]>>2);
190
191
	/* return it */
192
	*key        = (code >> 8) & 0xff;
193
	ir->nextkey =  code       & 0xff;
194
	return 0;
195
}
196
197
static int get_key_pixelview(void* data, unsigned char* key, int key_no)
198
{
199
	struct i2c_ir *ir = data;
200
        unsigned char b;
201
	
202
	/* poll IR chip */
203
	if (1 != i2c_master_recv(&ir->client,&b,1)) {
204
		dprintk(DRIVER_NAME ": read error\n");
205
		return -1;
206
	}
207
	dprintk(DRIVER_NAME ": key %02x\n", b);
208
	*key = b;
209
	return 0;
210
}
211
212
static int get_key_pv951(void* data, unsigned char* key, int key_no)
213
{
214
	struct i2c_ir *ir = data;
215
        unsigned char b;
216
	static unsigned char codes[4];
217
	
218
	if(key_no>0)
219
	{
220
		if(key_no>=4) {
221
			dprintk(DRIVER_NAME
222
				": something wrong in get_key_pv951\n");
223
			return -EBADRQC;
224
		}
225
		*key = codes[key_no];
226
		return 0;
227
	}
228
	
229
	/* poll IR chip */
230
	if (1 != i2c_master_recv(&ir->client,&b,1)) {
231
		dprintk(DRIVER_NAME ": read error\n");
232
		return -1;
233
	}
234
	/* ignore 0xaa */
235
	if (b==0xaa)
236
		return -1;
237
	dprintk(DRIVER_NAME ": key %02x\n", b);
238
	
239
	codes[2] = reverse(b,8);
240
	codes[3] = (~codes[2])&0xff;
241
	codes[0] = 0x61;
242
	codes[1] = 0xD6;
243
	
244
	*key=codes[0];
245
	return 0;
246
}
247
248
static int get_key_knc1(void *data, unsigned char *key, int key_no)
249
{
250
	struct i2c_ir *ir = data;
251
	unsigned char b;
252
	static unsigned char last_button = 0xFF;
253
	
254
	/* poll IR chip */
255
	if (1 != i2c_master_recv(&ir->client,&b,1)) {
256
		dprintk(DRIVER_NAME ": read error\n");
257
		return -1;
258
	}
259
	
260
	/* it seems that 0xFE indicates that a button is still hold
261
	   down, while 0xFF indicates that no button is hold
262
	   down. 0xFE sequences are sometimes interrupted by 0xFF */
263
	
264
	if( b == 0xFF )
265
		return -1;
266
	
267
	dprintk(DRIVER_NAME ": key %02x\n", b);
268
	
269
	if ( b == 0xFE )
270
		b = last_button;
271
272
	*key = b;
273
	last_button = b;
274
	return 0;
275
}
276
277
static int set_use_inc(void* data)
278
{
279
	try_module_get(THIS_MODULE);
280
	return 0;
281
}
282
283
static void set_use_dec(void* data)
284
{
285
	module_put(THIS_MODULE);
286
}
287
288
static struct lirc_plugin lirc_template = {
289
	.name =        "lirc_i2c",
290
	.set_use_inc = set_use_inc,
291
	.set_use_dec = set_use_dec
292
};
293
294
/* ----------------------------------------------------------------------- */
295
296
static int lirc_i2c_attach(struct i2c_adapter *adap, int addr, int kind);
297
static int lirc_i2c_detach(struct i2c_client *client);
298
static int lirc_i2c_probe(struct i2c_adapter *adap);
299
300
static struct i2c_driver driver = {
301
	.owner          = THIS_MODULE,
302
        .name           = DRIVER_NAME,
303
        .id             = I2C_DRIVERID_EXP3, /* FIXME */
304
        .flags          = I2C_DF_NOTIFY,
305
        .attach_adapter = lirc_i2c_probe,
306
        .detach_client  = lirc_i2c_detach,
307
};
308
309
static struct i2c_client client_template = 
310
{
311
        I2C_DEVNAME("(unset)"),
312
	.flags  = I2C_CLIENT_ALLOW_USE,
313
        .driver = &driver
314
};
315
316
static int lirc_i2c_attach(struct i2c_adapter *adap, int addr, int kind)
317
{
318
        struct i2c_ir *ir;
319
	int ret;
320
	
321
        client_template.adapter = adap;
322
        client_template.addr = addr;
323
	
324
	if (NULL == (ir = kmalloc(sizeof(struct i2c_ir),GFP_KERNEL)))
325
                return -ENOMEM;
326
	memset(ir,0,sizeof(struct i2c_ir));
327
	memcpy(&ir->client,&client_template,sizeof(struct i2c_client));
328
        memcpy(&ir->lirc,&lirc_template,sizeof(struct lirc_plugin));
329
	
330
	ir->lirc.data		= ir;
331
	ir->lirc.minor		= minor;
332
	ir->nextkey		= -1;
333
334
	i2c_set_clientdata(&ir->client,ir);
335
336
	switch(addr)
337
	{
338
	case 0x64:
339
		strncpy(ir->client.name, "Pixelview IR", I2C_NAME_SIZE);
340
		ir->lirc.code_length = 8;
341
		ir->lirc.sample_rate = 10;
342
		ir->lirc.get_key = get_key_pixelview;
343
		break;
344
	case 0x4b:
345
		strncpy(ir->client.name,"PV951 IR", I2C_NAME_SIZE);
346
		ir->lirc.code_length = 32;
347
		ir->lirc.sample_rate = 10;
348
		ir->lirc.get_key = get_key_pv951;
349
		break;
350
	case 0x18:
351
	case 0x1a:
352
		strncpy(ir->client.name,"Hauppauge IR", I2C_NAME_SIZE);
353
		ir->lirc.code_length = 13;
354
		ir->lirc.sample_rate = 6;
355
		ir->lirc.get_key = get_key_haup;
356
		break;
357
	case 0x30:
358
		strncpy(ir->client.name,"KNC ONE IR", I2C_NAME_SIZE);
359
		ir->lirc.code_length = 8;
360
		ir->lirc.sample_rate = 10;
361
		ir->lirc.get_key = get_key_knc1;
362
		break;
363
	case 0x21:
364
	case 0x23:
365
		strncpy(ir->client.name,"TV-Box IR", I2C_NAME_SIZE);
366
		ir->lirc.code_length = 8;
367
		ir->lirc.sample_rate = 10;
368
		ir->lirc.get_key = get_key_pcf8574;
369
		ir->bits = ir->client.flags & 0xff;
370
		ir->flag = (ir->client.flags >> 8) & 0xff;
371
		break;
372
		
373
	default:
374
		/* shouldn't happen */
375
		dprintk(DRIVER_NAME ": unknown i2c address (0x%02x)?\n",addr);
376
		kfree(ir);
377
		return -1;
378
	}
379
	dprintk(DRIVER_NAME ": chip found @ 0x%02x (%s)\n",addr,
380
			ir->client.name);
381
	
382
	/* register device */
383
	i2c_attach_client(&ir->client);
384
	
385
	if((ret = lirc_register_plugin(&ir->lirc))) {
386
		dprintk(DRIVER_NAME ": device registration failed with %d\n",
387
				ret);
388
		kfree(ir);
389
		return -1;
390
	}
391
392
	ir->lirc.minor = ret;
393
	dprintk(DRIVER_NAME ": driver registered\n");
394
395
	return 0;
396
}
397
398
static int lirc_i2c_detach(struct i2c_client *client)
399
{
400
        struct i2c_ir *ir = i2c_get_clientdata(client);
401
	int err;
402
	
403
	/* unregister device */
404
	if ((err = lirc_unregister_plugin(ir->lirc.minor))) {
405
		dprintk(DRIVER_NAME ": lirc unregister failed\n");
406
		return err;
407
	} else {
408
		dprintk(DRIVER_NAME ": lirc unregister successful\n");
409
	}
410
411
	if ((err = i2c_detach_client(&ir->client))) {
412
		dprintk(DRIVER_NAME ": i2c detach failed\n");
413
		return err;
414
	} else {
415
		dprintk(DRIVER_NAME ": i2c detach successful\n");
416
	}
417
418
	/* free memory */
419
	kfree(ir);
420
	return 0;
421
}
422
423
static int lirc_i2c_probe(struct i2c_adapter *adap) {
424
	dprintk(DRIVER_NAME ": starting probe for adapter %s (0x%x)\n",
425
			adap->name, adap->id);
426
	return i2c_probe(adap, &addr_data, lirc_i2c_attach);
427
}
428
429
static int __init lirc_i2c_init(void)
430
{
431
	dprintk(DRIVER_NAME ": init\n");
432
	request_module("bttv");
433
	request_module("lirc_dev");
434
	return i2c_add_driver(&driver);
435
}
436
437
static void __exit lirc_i2c_exit(void)
438
{
439
	dprintk(DRIVER_NAME ": exit\n");
440
	i2c_del_driver(&driver);
441
}
442
443
module_init(lirc_i2c_init);
444
module_exit(lirc_i2c_exit);
(-)linux-2.6.2/drivers/char/lirc/lirc_it87.c (+953 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 void 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
	if (module_refcount(THIS_MODULE)) {
150
		spin_unlock(&dev_lock);
151
		return -EBUSY;
152
	}
153
	try_module_get(THIS_MODULE);
154
	spin_unlock(&dev_lock);
155
	return 0;
156
}
157
158
159
static int lirc_close(struct inode * inode,
160
		      struct file *file)
161
{
162
	module_put(THIS_MODULE);
163
	return 0;
164
}
165
166
167
static unsigned int lirc_poll(struct file * file,
168
			      poll_table * wait)
169
{
170
	poll_wait(file, &lirc_read_queue, wait);
171
	if (rx_head != rx_tail)
172
		return POLLIN | POLLRDNORM;
173
	return 0;
174
}
175
176
177
static ssize_t lirc_read(struct file * file,
178
			 char * buf,
179
			 size_t count,
180
			 loff_t * ppos)
181
{
182
	int n=0;
183
	int retval=0;
184
185
	while(n<count)
186
	{
187
		if(file->f_flags & O_NONBLOCK &&
188
		   rx_head==rx_tail)
189
		{
190
			retval = -EAGAIN;
191
			break;
192
		}
193
		retval=wait_event_interruptible(lirc_read_queue,
194
						rx_head!=rx_tail);
195
		if(retval)
196
		{
197
			break;
198
		}
199
200
		retval=verify_area(VERIFY_WRITE,(void *) buf+n,
201
				   sizeof(lirc_t));
202
		if (retval)
203
		{
204
			return retval;
205
		}
206
		copy_to_user((void *) buf+n,(void *) (rx_buf+rx_head),
207
			     sizeof(lirc_t));
208
		rx_head=(rx_head+1)&(RBUF_LEN-1);
209
		n+=sizeof(lirc_t);
210
	}
211
	if(n)
212
	{
213
		return n;
214
	}
215
	return retval;
216
}
217
218
219
static ssize_t lirc_write(struct file * file,
220
			  const char * buf,
221
			  size_t n,
222
			  loff_t * pos)
223
{
224
	int i;
225
	int retval;
226
227
        if(n%sizeof(lirc_t) || (n/sizeof(lirc_t)) > WBUF_LEN)
228
		return(-EINVAL);
229
	retval = verify_area(VERIFY_READ, buf, n);
230
	if (retval)
231
		return retval;
232
	copy_from_user(tx_buf, buf, n);
233
	i = 0;
234
	n/=sizeof(lirc_t);
235
	init_send();
236
	while (1) {
237
		if (i >= n)
238
			break;
239
		if (tx_buf[i])
240
			send_pulse(tx_buf[i]);
241
		i++;
242
		if (i >= n)
243
			break;
244
		if (tx_buf[i])
245
			send_space(tx_buf[i]);
246
		i++;
247
	}
248
	terminate_send(tx_buf[i-1]);
249
	return n;
250
}
251
252
253
static int lirc_ioctl(struct inode *node,
254
		      struct file *filep,
255
		      unsigned int cmd,
256
		      unsigned long arg)
257
{
258
	int retval = 0;
259
	unsigned long value = 0;
260
	unsigned int ivalue;
261
262
	if (cmd == LIRC_GET_FEATURES)
263
		value = LIRC_CAN_SEND_PULSE |
264
			LIRC_CAN_SET_SEND_CARRIER |
265
			LIRC_CAN_REC_MODE2;
266
	else if (cmd == LIRC_GET_SEND_MODE)
267
		value = LIRC_MODE_PULSE;
268
	else if (cmd == LIRC_GET_REC_MODE)
269
		value = LIRC_MODE_MODE2;
270
271
	switch (cmd) {
272
	case LIRC_GET_FEATURES:
273
	case LIRC_GET_SEND_MODE:
274
	case LIRC_GET_REC_MODE:
275
		retval = put_user(value, (unsigned long *) arg);
276
		break;
277
278
	case LIRC_SET_SEND_MODE:
279
	case LIRC_SET_REC_MODE:
280
		retval = get_user(value, (unsigned long *) arg);
281
		break;
282
283
	case LIRC_SET_SEND_CARRIER:
284
		retval=get_user(ivalue,(unsigned int *) arg);
285
		if(retval) return(retval);
286
		ivalue /= 1000;
287
		if (ivalue > IT87_CIR_FREQ_MAX ||
288
		    ivalue < IT87_CIR_FREQ_MIN) return(-EINVAL);
289
290
		it87_freq = ivalue;
291
		{
292
			unsigned long hw_flags;
293
294
			spin_lock_irqsave(&hardware_lock, hw_flags);
295
			outb(((inb(io + IT87_CIR_TCR2) & IT87_CIR_TCR2_TXMPW) |
296
			      (it87_freq - IT87_CIR_FREQ_MIN) << 3),
297
			     io + IT87_CIR_TCR2);
298
			spin_unlock_irqrestore(&hardware_lock, hw_flags);
299
#ifdef DEBUG
300
			printk(KERN_DEBUG LIRC_DRIVER_NAME
301
			       " demodulation frequency: %d kHz\n", it87_freq);
302
#endif
303
		}
304
305
		break;
306
307
	default:
308
		retval = -ENOIOCTLCMD;
309
	}
310
311
	if (retval)
312
		return retval;
313
314
	if (cmd == LIRC_SET_REC_MODE) {
315
		if (value != LIRC_MODE_MODE2)
316
			retval = -ENOSYS;
317
	} else if (cmd == LIRC_SET_SEND_MODE) {
318
		if (value != LIRC_MODE_PULSE)
319
			retval = -ENOSYS;
320
	}
321
	return retval;
322
}
323
324
static void add_read_queue(int flag,
325
			   unsigned long val)
326
{
327
	unsigned int new_rx_tail;
328
	lirc_t newval;
329
330
#ifdef DEBUG_SIGNAL
331
	printk(KERN_DEBUG LIRC_DRIVER_NAME
332
	       ": add flag %d with val %lu\n",
333
	       flag,val);
334
#endif
335
336
	newval = val & PULSE_MASK;
337
338
	/* statistically pulses are ~TIME_CONST/2 too long: we could
339
	   maybe make this more exactly but this is good enough */
340
	if(flag) /* pulse */ {
341
		if(newval>TIME_CONST/2) {
342
			newval-=TIME_CONST/2;
343
		}
344
		else /* should not ever happen */ {
345
			newval=1;
346
		}
347
		newval|=PULSE_BIT;
348
	}
349
	else {
350
		newval+=TIME_CONST/2;
351
	}
352
	new_rx_tail = (rx_tail + 1) & (RBUF_LEN - 1);
353
	if (new_rx_tail == rx_head) {
354
#ifdef DEBUG
355
		printk(KERN_WARNING LIRC_DRIVER_NAME ": Buffer overrun.\n");
356
#endif
357
		return;
358
	}
359
	rx_buf[rx_tail] = newval;
360
	rx_tail = new_rx_tail;
361
	wake_up_interruptible(&lirc_read_queue);
362
}
363
364
365
static struct file_operations lirc_fops = {
366
	read:    lirc_read,
367
	write:   lirc_write,
368
	poll:    lirc_poll,
369
	ioctl:   lirc_ioctl,
370
	open:    lirc_open,
371
	release: lirc_close,
372
};
373
374
static int set_use_inc(void* data)
375
{
376
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
377
       try_module_get(THIS_MODULE);
378
#endif
379
       return 0;
380
}
381
382
static void set_use_dec(void* data)
383
{
384
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
385
       module_put(THIS_MODULE);
386
#endif
387
}
388
static struct lirc_plugin plugin = {
389
       name:           LIRC_DRIVER_NAME,
390
       minor:          -1,
391
       code_length:    1,
392
       sample_rate:    0,
393
       data:           NULL,
394
       get_key:        NULL,
395
       get_queue:      NULL,
396
       set_use_inc:    set_use_inc,
397
       set_use_dec:    set_use_dec,
398
       fops:           &lirc_fops,
399
};
400
401
402
int init_chrdev(void)
403
{
404
	plugin.minor = lirc_register_plugin(&plugin);
405
406
	if (plugin.minor < 0) {
407
		printk(KERN_ERR LIRC_DRIVER_NAME ": init_chrdev() failed.\n");
408
		return -EIO;
409
	}
410
	return 0;
411
}
412
413
414
static void drop_chrdev(void)
415
{
416
	lirc_unregister_plugin(plugin.minor);
417
}
418
419
/* SECTION: Hardware */
420
static long delta(struct timeval * tv1,
421
		  struct timeval * tv2)
422
{
423
	unsigned long deltv;
424
425
	deltv = tv2->tv_sec - tv1->tv_sec;
426
	if (deltv > 15)
427
		deltv = 0xFFFFFF;
428
	else
429
		deltv = deltv*1000000 +
430
			tv2->tv_usec -
431
			tv1->tv_usec;
432
	return deltv;
433
}
434
435
436
static void it87_timeout(unsigned long data)
437
{
438
	/* if last received signal was a pulse, but receiving stopped
439
	   within the 9 bit frame, we need to finish this pulse and
440
	   simulate a signal change to from pulse to space. Otherwise
441
	   upper layers will receive two sequences next time. */
442
443
	unsigned long flags;
444
	unsigned long pulse_end;
445
446
	/* avoid interference with interrupt */
447
 	spin_lock_irqsave(&timer_lock, flags);
448
	if (last_value) {
449
		/* determine 'virtual' pulse end: */
450
	 	pulse_end = delta(&last_tv, &last_intr_tv);
451
#ifdef DEBUG_SIGNAL
452
		printk(KERN_DEBUG LIRC_DRIVER_NAME
453
		       ": timeout add %d for %lu usec\n",
454
		       last_value,
455
		       pulse_end);
456
#endif
457
		add_read_queue(last_value,
458
			       pulse_end);
459
		last_value = 0;
460
		last_tv=last_intr_tv;
461
	}
462
	spin_unlock_irqrestore(&timer_lock, flags);
463
}
464
465
466
static void it87_interrupt(int irq,
467
			   void * dev_id,
468
			   struct pt_regs * regs)
469
{
470
	unsigned char data;
471
	struct timeval curr_tv;
472
	static unsigned long deltv;
473
	unsigned long deltintrtv;
474
	unsigned long flags, hw_flags;
475
	int iir, lsr;
476
	int fifo = 0;
477
478
	iir = inb(io + IT87_CIR_IIR);
479
480
	switch (iir & IT87_CIR_IIR_IID) {
481
	case 0x4:
482
	case 0x6:
483
		lsr = inb(io + IT87_CIR_RSR) & (IT87_CIR_RSR_RXFTO |
484
						    IT87_CIR_RSR_RXFBC);
485
		fifo = lsr & IT87_CIR_RSR_RXFBC;
486
#ifdef DEBUG_SIGNAL
487
		printk(KERN_DEBUG LIRC_DRIVER_NAME
488
		       "iir: 0x%x fifo: 0x%x\n", iir, lsr);
489
#endif
490
491
		/* avoid interference with timer */
492
		spin_lock_irqsave(&timer_lock, flags);
493
		spin_lock_irqsave(&hardware_lock, hw_flags);
494
		do {
495
			del_timer(&timerlist);
496
			data = inb(io + IT87_CIR_DR);
497
#ifdef DEBUG_SIGNAL
498
			printk(KERN_DEBUG LIRC_DRIVER_NAME
499
			       ": data=%.2x\n",
500
			       data);
501
#endif
502
			do_gettimeofday(&curr_tv);
503
			deltv = delta(&last_tv, &curr_tv);
504
			deltintrtv = delta(&last_intr_tv, &curr_tv);
505
#ifdef DEBUG_SIGNAL
506
			printk(KERN_DEBUG LIRC_DRIVER_NAME
507
			       ": t %lu , d %d\n",
508
			       deltintrtv,
509
			       (int)data);
510
#endif
511
			/* if nothing came in last 2 cycles,
512
			   it was gap */
513
			if (deltintrtv > TIME_CONST * 2) {
514
				if (last_value) {
515
#ifdef DEBUG_SIGNAL
516
					printk(KERN_DEBUG LIRC_DRIVER_NAME ": GAP\n");
517
#endif
518
					/* simulate signal change */
519
					add_read_queue(last_value,
520
						       deltv-
521
						       deltintrtv);
522
					last_value = 0;
523
					last_tv.tv_sec = last_intr_tv.tv_sec;
524
					last_tv.tv_usec = last_intr_tv.tv_usec;
525
					deltv = deltintrtv;
526
				}
527
			}
528
			data = 1;
529
			if (data ^ last_value) {
530
				/* deltintrtv > 2*TIME_CONST,
531
				   remember ? */
532
				/* the other case is timeout */
533
				add_read_queue(last_value,
534
					       deltv-TIME_CONST);
535
				last_value = data;
536
				last_tv = curr_tv;
537
				if(last_tv.tv_usec>=TIME_CONST) {
538
					last_tv.tv_usec-=TIME_CONST;
539
				}
540
				else {
541
					last_tv.tv_sec--;
542
					last_tv.tv_usec+=1000000-
543
						TIME_CONST;
544
				}
545
			}
546
			last_intr_tv = curr_tv;
547
			if (data) {
548
				/* start timer for end of sequence detection */
549
				timerlist.expires = jiffies + IT87_TIMEOUT;
550
				add_timer(&timerlist);
551
			}
552
			outb((inb(io + IT87_CIR_RCR) & ~IT87_CIR_RCR_RXEN) |
553
			     IT87_CIR_RCR_RXACT,
554
			     io + IT87_CIR_RCR);
555
			if (it87_RXEN_mask) {
556
				outb(inb(io + IT87_CIR_RCR) | IT87_CIR_RCR_RXEN,
557
				     io + IT87_CIR_RCR);
558
			}
559
			fifo--;
560
		}
561
		while (fifo != 0);
562
		spin_unlock_irqrestore(&hardware_lock, hw_flags);
563
		spin_unlock_irqrestore(&timer_lock, flags);
564
		break;
565
566
	default:
567
		/* not our irq */
568
#ifdef DEBUG_SIGNAL
569
		printk(KERN_DEBUG LIRC_DRIVER_NAME
570
		       "unknown IRQ (shouldn't happen) !!\n");
571
#endif
572
		break;
573
	}
574
}
575
576
577
static void send_it87(unsigned long len,
578
		      unsigned long stime,
579
		      unsigned char send_byte,
580
		      unsigned int count_bits)
581
{
582
        long count = len / stime;
583
	long time_left = 0;
584
	static unsigned char byte_out = 0;
585
586
#ifdef DEBUG_SIGNAL
587
	printk(KERN_DEBUG LIRC_DRIVER_NAME
588
	       "send_it87: len=%ld, sb=%d\n",
589
	       len,
590
	       send_byte);
591
#endif
592
	time_left = (long)len - (long)count * (long)stime;
593
	count += ((2 * time_left) / stime);
594
	while (count) {
595
		long i=0;
596
		for (i=0; i<count_bits; i++) {
597
			byte_out = (byte_out << 1) | (send_byte & 1);
598
			it87_bits_in_byte_out++;
599
		}
600
		if (it87_bits_in_byte_out == 8) {
601
#ifdef DEBUG_SIGNAL
602
			printk(KERN_DEBUG LIRC_DRIVER_NAME
603
			       "out=0x%x, tsr_txfbc: 0x%x\n",
604
			       byte_out,
605
			       inb(io + IT87_CIR_TSR) &
606
			       IT87_CIR_TSR_TXFBC);
607
#endif
608
			while ((inb(io + IT87_CIR_TSR) &
609
				IT87_CIR_TSR_TXFBC) >= IT87_CIR_FIFO_SIZE);
610
			{
611
				unsigned long hw_flags;
612
613
				spin_lock_irqsave(&hardware_lock, hw_flags);
614
				outb(byte_out, io + IT87_CIR_DR);
615
				spin_unlock_irqrestore(&hardware_lock, hw_flags);
616
			}
617
			it87_bits_in_byte_out = 0;
618
			it87_send_counter++;
619
			byte_out = 0;
620
		}
621
		count--;
622
	}
623
}
624
625
626
/*
627
maybe: exchange space and pulse because
628
it8705 only modulates 0-bits
629
*/
630
631
632
static void send_space(unsigned long len)
633
{
634
	send_it87(len,
635
		  TIME_CONST,
636
		  IT87_CIR_SPACE,
637
		  IT87_CIR_BAUDRATE_DIVISOR);
638
}
639
640
static void send_pulse(unsigned long len)
641
{
642
	send_it87(len,
643
		  TIME_CONST,
644
		  IT87_CIR_PULSE,
645
		  IT87_CIR_BAUDRATE_DIVISOR);
646
}
647
648
649
static void init_send()
650
{
651
	unsigned long flags;
652
653
	spin_lock_irqsave(&hardware_lock, flags);
654
	/* RXEN=0: receiver disable */
655
	it87_RXEN_mask = 0;
656
	outb(inb(io + IT87_CIR_RCR) & ~IT87_CIR_RCR_RXEN,
657
	     io + IT87_CIR_RCR);
658
	spin_unlock_irqrestore(&hardware_lock, flags);
659
	it87_bits_in_byte_out = 0;
660
	it87_send_counter = 0;
661
}
662
663
664
static void terminate_send(unsigned long len)
665
{
666
	unsigned long flags;
667
	unsigned long last = 0;
668
669
	last = it87_send_counter;
670
	/* make sure all necessary data has been sent */
671
	while (last == it87_send_counter)
672
		send_space(len);
673
	/* wait until all data sent */
674
	while ((inb(io + IT87_CIR_TSR) & IT87_CIR_TSR_TXFBC) != 0);
675
	/* then reenable receiver */
676
	spin_lock_irqsave(&hardware_lock, flags);
677
	it87_RXEN_mask = IT87_CIR_RCR_RXEN;
678
	outb(inb(io + IT87_CIR_RCR) | IT87_CIR_RCR_RXEN,
679
	     io + IT87_CIR_RCR);
680
	spin_unlock_irqrestore(&hardware_lock, flags);
681
}
682
683
684
static int init_hardware(void)
685
{
686
	unsigned long flags;
687
	unsigned char it87_rcr = 0;
688
689
	spin_lock_irqsave(&hardware_lock, flags);
690
	/* init cir-port */
691
	/* enable r/w-access to Baudrate-Register */
692
	outb(IT87_CIR_IER_BR, io + IT87_CIR_IER);
693
	outb(IT87_CIR_BAUDRATE_DIVISOR % 0x100, io+IT87_CIR_BDLR);
694
	outb(IT87_CIR_BAUDRATE_DIVISOR / 0x100, io+IT87_CIR_BDHR);
695
	/* Baudrate Register off, define IRQs: Input only */
696
	outb(IT87_CIR_IER_IEC | IT87_CIR_IER_RDAIE, io + IT87_CIR_IER);
697
	/* RX: HCFS=0, RXDCR = 001b (35,6..40,3 kHz), RXEN=1 */
698
	it87_rcr = (IT87_CIR_RCR_RXEN & it87_RXEN_mask) | 0x1;
699
	if (it87_enable_demodulator)
700
		it87_rcr |= IT87_CIR_RCR_RXEND;
701
	outb(it87_rcr, io + IT87_CIR_RCR);
702
	/* TX: 38kHz, 13,3us (pulse-width */
703
	outb(((it87_freq - IT87_CIR_FREQ_MIN) << 3) | 0x06,
704
	     io + IT87_CIR_TCR2);
705
	spin_unlock_irqrestore(&hardware_lock, flags);
706
	return 0;
707
}
708
709
710
static void drop_hardware(void)
711
{
712
	unsigned long flags;
713
714
	spin_lock_irqsave(&hardware_lock, flags);
715
	disable_irq(irq);
716
	/* receiver disable */
717
	it87_RXEN_mask = 0;
718
	outb(0x1, io + IT87_CIR_RCR);
719
	/* turn off irqs */
720
	outb(0, io + IT87_CIR_IER);
721
	/* fifo clear */
722
        outb(IT87_CIR_TCR1_FIFOCLR, io+IT87_CIR_TCR1);
723
        /* reset */
724
        outb(IT87_CIR_IER_RESET, io+IT87_CIR_IER);
725
	enable_irq(irq);
726
	spin_unlock_irqrestore(&hardware_lock, flags);
727
}
728
729
730
static unsigned char it87_read(unsigned char port)
731
{
732
	outb(port, IT87_ADRPORT);
733
	return inb(IT87_DATAPORT);
734
}
735
736
737
static void it87_write(unsigned char port,
738
		       unsigned char data)
739
{
740
	outb(port, IT87_ADRPORT);
741
	outb(data, IT87_DATAPORT);
742
}
743
744
745
/* SECTION: Initialisation */
746
747
static int init_port(void)
748
{
749
	int retval = 0;
750
751
	unsigned char init_bytes[4] = {IT87_INIT};
752
	unsigned char it87_chipid = 0;
753
	unsigned char ldn = 0;
754
	unsigned int  it87_io = 0;
755
	unsigned int  it87_irq = 0;
756
757
	/* Enter MB PnP Mode */
758
	outb(init_bytes[0], IT87_ADRPORT);
759
	outb(init_bytes[1], IT87_ADRPORT);
760
	outb(init_bytes[2], IT87_ADRPORT);
761
	outb(init_bytes[3], IT87_ADRPORT);
762
763
	/* 8712 or 8705 ? */
764
	it87_chipid = it87_read(IT87_CHIP_ID1);
765
	if (it87_chipid != 0x87) {
766
		retval = -ENXIO;
767
		return retval;
768
	}
769
	it87_chipid = it87_read(IT87_CHIP_ID2);
770
	if ((it87_chipid != 0x12) && (it87_chipid != 0x05)) {
771
		printk(KERN_INFO LIRC_DRIVER_NAME
772
		       ": no IT8705/12 found, exiting..\n");
773
		retval = -ENXIO;
774
		return retval;
775
	}
776
	printk(KERN_INFO LIRC_DRIVER_NAME
777
	       ": found IT87%.2x.\n",
778
	       it87_chipid);
779
780
	/* get I/O-Port and IRQ */
781
	if (it87_chipid == 0x12)
782
		ldn = IT8712_CIR_LDN;
783
	else
784
		ldn = IT8705_CIR_LDN;
785
	it87_write(IT87_LDN, ldn);
786
787
	it87_io = it87_read(IT87_CIR_BASE_MSB) * 256 +
788
		it87_read(IT87_CIR_BASE_LSB);
789
	if (it87_io == 0) {
790
		if (io == 0)
791
			io = IT87_CIR_DEFAULT_IOBASE;
792
		printk(KERN_INFO LIRC_DRIVER_NAME
793
		       ": set default io 0x%x\n",
794
		       io);
795
		it87_write(IT87_CIR_BASE_MSB, io / 0x100);
796
		it87_write(IT87_CIR_BASE_LSB, io % 0x100);
797
	}
798
	else
799
		io = it87_io;
800
801
	it87_irq = it87_read(IT87_CIR_IRQ);
802
	if (it87_irq == 0) {
803
		if (irq == 0)
804
			irq = IT87_CIR_DEFAULT_IRQ;
805
		printk(KERN_INFO LIRC_DRIVER_NAME
806
		       ": set default irq 0x%x\n",
807
		       irq);
808
		it87_write(IT87_CIR_IRQ, irq);
809
	}
810
	else
811
		irq = it87_irq;
812
813
	{
814
		unsigned long hw_flags;
815
816
		spin_lock_irqsave(&hardware_lock, hw_flags);
817
		/* reset */
818
		outb(IT87_CIR_IER_RESET, io+IT87_CIR_IER);
819
		/* fifo clear */
820
		outb(IT87_CIR_TCR1_FIFOCLR |
821
		     /*	     IT87_CIR_TCR1_ILE | */
822
		     IT87_CIR_TCR1_TXRLE |
823
		     IT87_CIR_TCR1_TXENDF, io+IT87_CIR_TCR1);
824
		spin_unlock_irqrestore(&hardware_lock, hw_flags);
825
	}
826
827
	/* get I/O port access and IRQ line */
828
	retval = request_region(io, 8, LIRC_DRIVER_NAME);
829
830
	if (!retval) {
831
		printk(KERN_ERR LIRC_DRIVER_NAME
832
		       ": Unable to reserve IO region for LIRC IT87. Port 0x%.4x already in use.\n",
833
		       io);
834
		/* Leaving MB PnP Mode */
835
		it87_write(IT87_CFGCTRL, 0x2);
836
		return retval;
837
	}
838
839
	/* activate CIR-Device */
840
	it87_write(IT87_CIR_ACT, 0x1);
841
842
	/* Leaving MB PnP Mode */
843
	it87_write(IT87_CFGCTRL, 0x2);
844
845
	retval = request_irq(irq, it87_interrupt, 0 /*SA_INTERRUPT*/,
846
			     LIRC_DRIVER_NAME, NULL);
847
	if (retval < 0) {
848
		printk(KERN_ERR LIRC_DRIVER_NAME
849
		       ": IRQ %d already in use.\n",
850
		       irq);
851
		return retval;
852
	}
853
854
	printk(KERN_INFO LIRC_DRIVER_NAME
855
	       ": I/O port 0x%.4x, IRQ %d.\n",
856
	       io,
857
	       irq);
858
859
	request_region(io, 8, LIRC_DRIVER_NAME);
860
	init_timer(&timerlist);
861
	timerlist.function = it87_timeout;
862
	timerlist.data = 0xabadcafe;
863
864
	return 0;
865
}
866
867
868
static void drop_port(void)
869
{
870
/*
871
        unsigned char init_bytes[4] = {IT87_INIT};
872
873
        / * Enter MB PnP Mode * /
874
        outb(init_bytes[0], IT87_ADRPORT);
875
        outb(init_bytes[1], IT87_ADRPORT);
876
        outb(init_bytes[2], IT87_ADRPORT);
877
        outb(init_bytes[3], IT87_ADRPORT);
878
879
        / * deactivate CIR-Device * /
880
        it87_write(IT87_CIR_ACT, 0x0);
881
882
        / * Leaving MB PnP Mode * /
883
        it87_write(IT87_CFGCTRL, 0x2);
884
*/
885
886
	del_timer_sync(&timerlist);
887
	free_irq(irq, NULL);
888
	release_region(io, 8);
889
}
890
891
892
int init_lirc_it87(void)
893
{
894
	int retval;
895
896
	init_waitqueue_head(&lirc_read_queue);
897
	retval = init_port();
898
	if (retval < 0)
899
		return retval;
900
	init_hardware();
901
	printk(KERN_INFO LIRC_DRIVER_NAME
902
	       ": Installed.\n");
903
	return 0;
904
}
905
906
907
MODULE_AUTHOR("Hans-Günter Lütke Uphues");
908
MODULE_DESCRIPTION("LIRC driver for ITE IT8712/IT8705 CIR port");
909
MODULE_PARM(io, "i");
910
MODULE_PARM_DESC(io,
911
		 "I/O base address (default: 0x310)");
912
MODULE_PARM(irq, "i");
913
MODULE_PARM_DESC(irq,
914
		 "Interrupt (1,3-12) (default: 7)");
915
MODULE_PARM(it87_enable_demodulator, "i");
916
MODULE_PARM_DESC(it87_enable_demodulator,
917
		 "Receiver demodulator enable/disable (1/0), default: 0");
918
MODULE_LICENSE("GPL");
919
920
static int __init lirc_it87_init(void)
921
{
922
	int retval;
923
924
	retval=init_chrdev();
925
	if(retval < 0)
926
		return retval;
927
	retval = init_lirc_it87();
928
	if (retval) {
929
		drop_chrdev();
930
		return retval;
931
	}
932
	return 0;
933
}
934
935
936
static void __exit lirc_it87_exit(void)
937
{
938
	drop_hardware();
939
	drop_chrdev();
940
	drop_port();
941
	printk(KERN_INFO LIRC_DRIVER_NAME ": Uninstalled.\n");
942
}
943
944
module_init(lirc_it87_init);
945
module_exit(lirc_it87_exit);
946
947
/*
948
 * Overrides for Emacs so that we follow Linus's tabbing style.
949
 * ---------------------------------------------------------------------------
950
 * Local variables:
951
 * c-basic-offset: 8
952
 * End:
953
 */
(-)linux-2.6.2/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.2/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.2/drivers/char/lirc/lirc_parallel.c (+733 lines)
Line 0 Link Here
1
/*      $Id: 405_lirc_infrared-2.6.2-02092004.patch,v 1.1 2004/02/24 22:27:37 brad_mssw 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
	if(!module_refcount(THIS_MODULE))
259
		return;
260
261
	if(!is_claimed)
262
	{
263
		return;
264
	}
265
266
	/* disable interrupt */
267
	/*
268
	  disable_irq(irq);
269
	  out(LIRC_PORT_IRQ,in(LIRC_PORT_IRQ)&(~LP_PINTEN));
270
	*/
271
	if(in(1)&LP_PSELECD)
272
	{
273
		return;
274
	}
275
276
#ifdef CONFIG_LIRC_TIMER
277
	if(init)
278
	{
279
		do_gettimeofday(&tv);
280
281
	        signal=tv.tv_sec-lasttv.tv_sec;
282
		if(signal>15)
283
		{
284
			data=PULSE_MASK;  /* really long time */
285
		}
286
		else
287
		{
288
			data=(lirc_t) (signal*1000000+
289
				       tv.tv_usec-lasttv.tv_usec+
290
				       LIRC_SFH506_DELAY);
291
		};
292
293
		rbuf_write(data); /* space */
294
	}
295
	else
296
	{
297
		if(timer==0) /* wake up; we'll lose this signal
298
				but it will be garbage if the device
299
				is turned on anyway
300
			      */
301
		{
302
			timer=init_CONFIG_LIRC_TIMER();
303
			/* enable_irq(irq); */
304
			return;
305
		}
306
		init=1;
307
	}
308
309
	timeout=timer/10;           /* timeout after 1/10 sec. */
310
	signal=1;
311
	level=lirc_get_timer();
312
	do{
313
		newlevel=lirc_get_timer();
314
		if(level==0 && newlevel!=0) signal++;
315
		level=newlevel;
316
317
		/* giving up */
318
		if(signal>timeout || (in(1)&LP_PSELECD))
319
		{
320
			signal=0;
321
			printk(KERN_NOTICE "%s: timeout\n",LIRC_DRIVER_NAME);
322
			break;
323
		}
324
	}
325
	while(lirc_get_signal());
326
	if(signal!=0)
327
	{
328
		/* ajust value to usecs */
329
		signal=(long) (((unsigned long long) signal)*1000000)/timer;
330
331
		if(signal>LIRC_SFH506_DELAY)
332
		{
333
			data=signal-LIRC_SFH506_DELAY;
334
		}
335
		else
336
		{
337
			data=1;
338
		}
339
		rbuf_write(PULSE_BIT|data); /* pulse */
340
	}
341
	do_gettimeofday(&lasttv);
342
#else
343
	/* add your code here */
344
#endif
345
346
	wake_up_interruptible(&lirc_wait);
347
348
	/* enable interrupt */
349
	/*
350
	  enable_irq(irq);
351
	  out(LIRC_PORT_IRQ,in(LIRC_PORT_IRQ)|LP_PINTEN);
352
	*/
353
}
354
355
/***********************************************************************
356
 **************************   file_operations   ************************
357
 ***********************************************************************/
358
359
static loff_t lirc_lseek(struct file *filep,loff_t offset,int orig)
360
{
361
	return(-ESPIPE);
362
}
363
364
static ssize_t lirc_read(struct file *filep,char *buf,size_t n,loff_t *ppos)
365
{
366
	int result;
367
	int count=0;
368
	DECLARE_WAITQUEUE(wait, current);
369
370
	if(n%sizeof(lirc_t)) return(-EINVAL);
371
372
	result=verify_area(VERIFY_WRITE,buf,n);
373
	if(result) return(result);
374
375
	add_wait_queue(&lirc_wait,&wait);
376
	current->state=TASK_INTERRUPTIBLE;
377
	while(count<n)
378
	{
379
		if(rptr!=wptr)
380
		{
381
			copy_to_user(buf+count,(char *) &rbuf[rptr],
382
				     sizeof(lirc_t));
383
			rptr=(rptr+1)&(RBUF_SIZE-1);
384
			count+=sizeof(lirc_t);
385
		}
386
		else
387
		{
388
			if(filep->f_flags & O_NONBLOCK)
389
			{
390
				result=-EAGAIN;
391
				break;
392
			}
393
			if (signal_pending(current))
394
			{
395
				result=-ERESTARTSYS;
396
				break;
397
			}
398
			schedule();
399
			current->state=TASK_INTERRUPTIBLE;
400
		}
401
	}
402
	remove_wait_queue(&lirc_wait,&wait);
403
	current->state=TASK_RUNNING;
404
	return(count ? count:result);
405
}
406
407
static ssize_t lirc_write(struct file *filep,const char *buf,size_t n,
408
			  loff_t *ppos)
409
{
410
	int result,count;
411
	unsigned int i;
412
	unsigned int level,newlevel;
413
	unsigned long flags;
414
	lirc_t counttimer;
415
416
	if(!is_claimed)
417
	{
418
		return(-EBUSY);
419
	}
420
	if(n%sizeof(lirc_t)) return(-EINVAL);
421
	result=verify_area(VERIFY_READ,buf,n);
422
	if(result) return(result);
423
424
	count=n/sizeof(lirc_t);
425
426
	if(count>WBUF_SIZE || count%2==0) return(-EINVAL);
427
428
	copy_from_user(wbuf,buf,n);
429
430
#ifdef CONFIG_LIRC_TIMER
431
	if(timer==0) /* try again if device is ready */
432
	{
433
		timer=init_CONFIG_LIRC_TIMER();
434
		if(timer==0) return(-EIO);
435
	}
436
437
	/* ajust values from usecs */
438
	for(i=0;i<count;i++)
439
	{
440
		wbuf[i]=(lirc_t) (((double) wbuf[i])*timer/1000000);
441
	}
442
443
	local_save_flags(flags);local_irq_disable();
444
	i=0;
445
	while(i<count)
446
	{
447
		level=lirc_get_timer();
448
		counttimer=0;
449
		lirc_on();
450
		do
451
		{
452
			newlevel=lirc_get_timer();
453
			if(level==0 && newlevel!=0) counttimer++;
454
			level=newlevel;
455
			if(in(1)&LP_PSELECD)
456
			{
457
				lirc_off();
458
				local_irq_restore(flags); /* sti(); */
459
				return(-EIO);
460
			}
461
		}
462
		while(counttimer<wbuf[i]);i++;
463
464
		lirc_off();
465
		if(i==count) break;
466
		counttimer=0;
467
		do
468
		{
469
			newlevel=lirc_get_timer();
470
			if(level==0 && newlevel!=0) counttimer++;
471
			level=newlevel;
472
			if(in(1)&LP_PSELECD)
473
			{
474
				local_irq_restore(flags); /* sti(); */
475
				return(-EIO);
476
			}
477
		}
478
		while(counttimer<wbuf[i]);i++;
479
	}
480
	local_irq_restore(flags); /* sti(); */
481
#else
482
	/*
483
	   place code that handles write
484
	   without extarnal timer here
485
	*/
486
#endif
487
	return(n);
488
}
489
490
static unsigned int lirc_poll(struct file *file, poll_table * wait)
491
{
492
	poll_wait(file, &lirc_wait,wait);
493
	if (rptr!=wptr)
494
		return(POLLIN|POLLRDNORM);
495
	return(0);
496
}
497
498
static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
499
		      unsigned long arg)
500
{
501
        int result;
502
	unsigned long features=LIRC_CAN_SEND_PULSE|LIRC_CAN_REC_MODE2,mode;
503
504
	switch(cmd)
505
	{
506
	case LIRC_GET_FEATURES:
507
		result=put_user(features,(unsigned long *) arg);
508
		if(result) return(result);
509
		break;
510
	case LIRC_GET_SEND_MODE:
511
		result=put_user(LIRC_MODE_PULSE,(unsigned long *) arg);
512
		if(result) return(result);
513
		break;
514
	case LIRC_GET_REC_MODE:
515
		result=put_user(LIRC_MODE_MODE2,(unsigned long *) arg);
516
		if(result) return(result);
517
		break;
518
	case LIRC_SET_SEND_MODE:
519
		result=get_user(mode,(unsigned long *) arg);
520
		if(result) return(result);
521
		if(mode!=LIRC_MODE_PULSE) return(-EINVAL);
522
		break;
523
	case LIRC_SET_REC_MODE:
524
		result=get_user(mode,(unsigned long *) arg);
525
		if(result) return(result);
526
		if(mode!=LIRC_MODE_MODE2) return(-ENOSYS);
527
		break;
528
	default:
529
		return(-ENOIOCTLCMD);
530
	}
531
	return(0);
532
}
533
534
static int lirc_open(struct inode* node,struct file* filep)
535
{
536
	if(module_refcount(THIS_MODULE))
537
	{
538
		return(-EBUSY);
539
	}
540
	if(!lirc_claim())
541
	{
542
		return(-EBUSY);
543
	}
544
	pport->ops->enable_irq(pport);
545
546
	/* init read ptr */
547
	rptr=wptr=0;
548
	lost_irqs=0;
549
550
	try_module_get(THIS_MODULE);
551
	is_open=1;
552
	return(0);
553
}
554
555
static int lirc_close(struct inode* node,struct file* filep)
556
{
557
	if(is_claimed)
558
	{
559
		is_claimed=0;
560
		parport_release(ppdevice);
561
	}
562
	is_open=0;
563
	module_put(THIS_MODULE);
564
	return(0);
565
}
566
567
static struct file_operations lirc_fops =
568
{
569
	llseek:  lirc_lseek,
570
	read:    lirc_read,
571
	write:   lirc_write,
572
	poll:    lirc_poll,
573
	ioctl:   lirc_ioctl,
574
	open:    lirc_open,
575
	release: lirc_close
576
};
577
578
static int set_use_inc(void* data)
579
{
580
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
581
       try_module_get(THIS_MODULE);
582
#endif
583
       return 0;
584
}
585
586
static void set_use_dec(void* data)
587
{
588
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
589
       module_put(THIS_MODULE);
590
#endif
591
}
592
static struct lirc_plugin plugin = {
593
       name:           LIRC_DRIVER_NAME,
594
       minor:          -1,
595
       code_length:    1,
596
       sample_rate:    0,
597
       data:           NULL,
598
       get_key:        NULL,
599
       get_queue:      NULL,
600
       set_use_inc:    set_use_inc,
601
       set_use_dec:    set_use_dec,
602
       fops:           &lirc_fops,
603
};
604
605
static int __init lirc_parallel_init (void)
606
{
607
	pport=parport_enumerate();
608
	while(pport!=NULL)
609
	{
610
		if(pport->base==io)
611
		{
612
			break;
613
		}
614
		pport=pport->next;
615
	}
616
	if(pport==NULL)
617
	{
618
		printk(KERN_NOTICE "%s: no port at %x found\n",
619
		       LIRC_DRIVER_NAME,io);
620
		return(-ENXIO);
621
	}
622
	ppdevice=parport_register_device(pport,LIRC_DRIVER_NAME,
623
					 pf,kf,irq_handler,0,NULL);
624
	if(ppdevice==NULL)
625
	{
626
		printk(KERN_NOTICE "%s: parport_register_device() failed\n",
627
		       LIRC_DRIVER_NAME);
628
		return(-ENXIO);
629
	}
630
	if(parport_claim(ppdevice)!=0)
631
		goto skip_init;
632
	is_claimed=1;
633
	out(LIRC_LP_CONTROL,LP_PSELECP|LP_PINITP);
634
635
#ifdef CONFIG_LIRC_TIMER
636
#       ifdef DEBUG
637
	out(LIRC_PORT_DATA,LIRC_PORT_DATA_BIT);
638
#       endif
639
640
	timer=init_CONFIG_LIRC_TIMER();
641
642
#       if 0 	/* continue even if device is offline */
643
	if(timer==0)
644
	{
645
		is_claimed=0;
646
		parport_release(pport);
647
		parport_unregister_device(ppdevice);
648
		return(-EIO);
649
	}
650
651
#       endif
652
#       ifdef DEBUG
653
	out(LIRC_PORT_DATA,0);
654
#       endif
655
#endif
656
657
	is_claimed=0;
658
	parport_release(ppdevice);
659
 skip_init:
660
	if ((plugin.minor = lirc_register_plugin(&plugin)) < 0)
661
	{
662
		printk(KERN_NOTICE "%s: register_chrdev() failed\n",LIRC_DRIVER_NAME);
663
		parport_unregister_device(ppdevice);
664
		return(-EIO);
665
	}
666
	printk(KERN_INFO "%s: installed using port 0x%04x irq %d\n",LIRC_DRIVER_NAME,io,irq);
667
	return(0);
668
}
669
670
static void __exit lirc_parallel_exit(void)
671
{
672
	if(module_refcount(THIS_MODULE)) return;
673
	parport_unregister_device(ppdevice);
674
	lirc_unregister_plugin(plugin.minor);
675
}
676
677
static struct timer_list poll_timer;
678
static void poll_state(unsigned long ignored);
679
680
static void poll_state(unsigned long ignored)
681
{
682
	printk(KERN_NOTICE "%s: time\n",
683
	       LIRC_DRIVER_NAME);
684
	del_timer(&poll_timer);
685
	if(is_claimed)
686
		return;
687
	kf(NULL);
688
	if(!is_claimed)
689
	{
690
		printk(KERN_NOTICE "%s: could not claim port, giving up\n",
691
		       LIRC_DRIVER_NAME);
692
		init_timer(&poll_timer);
693
		poll_timer.expires=jiffies+HZ;
694
		poll_timer.data=(unsigned long) current;
695
		poll_timer.function=poll_state;
696
		add_timer(&poll_timer);
697
	}
698
}
699
700
int pf(void *handle)
701
{
702
	pport->ops->disable_irq(pport);
703
	is_claimed=0;
704
	return(0);
705
}
706
707
708
void kf(void *handle)
709
{
710
	if(!is_open)
711
		return;
712
	if(!lirc_claim())
713
		return;
714
	pport->ops->enable_irq(pport);
715
	/* this is a bit annoying when you actually print...*/
716
	/*
717
	printk(KERN_INFO "%s: reclaimed port\n",LIRC_DRIVER_NAME);
718
	*/
719
}
720
721
MODULE_AUTHOR("Christoph Bartelmus");
722
MODULE_DESCRIPTION("Infrared receiver driver for parallel ports.");
723
MODULE_LICENSE("GPL");
724
725
MODULE_PARM(io, "i");
726
MODULE_PARM_DESC(io, "I/O address base (0x3bc, 0x378 or 0x278)");
727
728
MODULE_PARM(irq, "i");
729
MODULE_PARM_DESC(irq, "Interrupt (7 or 5)");
730
731
module_init(lirc_parallel_init);
732
module_exit(lirc_parallel_exit);
733
(-)linux-2.6.2/drivers/char/lirc/lirc_parallel.h (+24 lines)
Line 0 Link Here
1
/*      $Id: 405_lirc_infrared-2.6.2-02092004.patch,v 1.1 2004/02/24 22:27:37 brad_mssw 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.2/drivers/char/lirc/lirc_serial.c (+1053 lines)
Line 0 Link Here
1
/*      $Id: 405_lirc_infrared-2.6.2-02092004.patch,v 1.1 2004/02/24 22:27:37 brad_mssw 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
80
#include <asm/system.h>
81
#include <asm/segment.h>
82
#include <asm/io.h>
83
#include <asm/irq.h>
84
#include <asm/fcntl.h>
85
86
#include <linux/lirc.h>
87
#include "lirc_dev.h"
88
89
#if defined(rdtsc)
90
91
#define USE_RDTSC
92
#warning "Note: using rdtsc instruction"
93
#endif
94
95
struct lirc_serial
96
{
97
	int type;
98
	int signal_pin;
99
	int signal_pin_change;
100
	int on;
101
	int off;
102
	long (*send_pulse)(unsigned long length);
103
	void (*send_space)(long length);
104
	int features;
105
};
106
107
#define LIRC_HOMEBREW        0
108
#define LIRC_IRDEO           1
109
#define LIRC_IRDEO_REMOTE    2
110
#define LIRC_ANIMAX          3
111
#define LIRC_IGOR            4
112
113
#ifdef CONFIG_LIRC_SERIAL_IRDEO
114
int type=LIRC_IRDEO;
115
#elif defined(CONFIG_LIRC_SERIAL_IRDEO_REMOTE)
116
int type=LIRC_IRDEO_REMOTE;
117
#elif defined(CONFIG_LIRC_SERIAL_ANIMAX)
118
int type=LIRC_ANIMAX;
119
#elif defined(CONFIG_LIRC_SERIAL_IGOR)
120
int type=LIRC_IGOR;
121
#else
122
int type=LIRC_HOMEBREW;
123
#endif
124
125
#ifdef CONFIG_LIRC_SERIAL_SOFTCARRIER
126
int softcarrier=1;
127
#else
128
int softcarrier=0;
129
#endif
130
131
static int sense = -1;   /* -1 = auto, 0 = active high, 1 = active low */
132
133
static int io = CONFIG_LIRC_PORT_SERIAL;
134
135
static int irq = CONFIG_LIRC_IRQ_SERIAL;
136
137
static int debug = 0;
138
139
MODULE_PARM(type, "i");
140
MODULE_PARM_DESC(type, "Hardware type (0 = home-brew, 1 = IRdeo,"
141
		 " 2 = IRdeo Remote, 3 = AnimaX");
142
143
MODULE_PARM(io, "i");
144
MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
145
146
MODULE_PARM(irq, "i");
147
MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
148
149
MODULE_PARM(sense, "i");
150
MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
151
		 " (0 = active high, 1 = active low )");
152
153
MODULE_PARM(softcarrier, "i");
154
MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on)");
155
156
MODULE_PARM(debug,"i");
157
158
#define dprintk           if (debug) printk
159
160
#define LOGHEAD           "lirc_serial: "
161
162
/* forward declarations */
163
long send_pulse_irdeo(unsigned long length);
164
long send_pulse_homebrew(unsigned long length);
165
void send_space_irdeo(long length);
166
void send_space_homebrew(long length);
167
168
struct lirc_serial hardware[]=
169
{
170
	/* home-brew receiver/transmitter */
171
	{
172
		LIRC_HOMEBREW,
173
		UART_MSR_DCD,
174
		UART_MSR_DDCD,
175
		UART_MCR_RTS|UART_MCR_OUT2|UART_MCR_DTR,
176
		UART_MCR_RTS|UART_MCR_OUT2,
177
		send_pulse_homebrew,
178
		send_space_homebrew,
179
		(
180
#ifdef CONFIG_LIRC_SERIAL_TRANSMITTER
181
		 LIRC_CAN_SET_SEND_DUTY_CYCLE|
182
		 LIRC_CAN_SET_SEND_CARRIER|
183
		 LIRC_CAN_SEND_PULSE|
184
#endif
185
		 LIRC_CAN_REC_MODE2)
186
	},
187
188
	/* IRdeo classic */
189
	{
190
		LIRC_IRDEO,
191
		UART_MSR_DSR,
192
		UART_MSR_DDSR,
193
		UART_MCR_OUT2,
194
		UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
195
		send_pulse_irdeo,
196
		send_space_irdeo,
197
		(LIRC_CAN_SET_SEND_DUTY_CYCLE|
198
		 LIRC_CAN_SEND_PULSE|
199
		 LIRC_CAN_REC_MODE2)
200
	},
201
202
	/* IRdeo remote */
203
	{
204
		LIRC_IRDEO_REMOTE,
205
		UART_MSR_DSR,
206
		UART_MSR_DDSR,
207
		UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
208
		UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
209
		send_pulse_irdeo,
210
		send_space_irdeo,
211
		(LIRC_CAN_SET_SEND_DUTY_CYCLE|
212
		 LIRC_CAN_SEND_PULSE|
213
		 LIRC_CAN_REC_MODE2)
214
	},
215
216
	/* AnimaX */
217
	{
218
		LIRC_ANIMAX,
219
		UART_MSR_DCD,
220
		UART_MSR_DDCD,
221
		0,
222
		UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
223
		NULL,
224
		NULL,
225
		LIRC_CAN_REC_MODE2
226
	},
227
228
	/* home-brew receiver/transmitter (Igor Cesko's variation) */
229
	{
230
		LIRC_HOMEBREW,
231
		UART_MSR_DSR,
232
		UART_MSR_DDSR,
233
		UART_MCR_RTS|UART_MCR_OUT2|UART_MCR_DTR,
234
		UART_MCR_RTS|UART_MCR_OUT2,
235
		send_pulse_homebrew,
236
		send_space_homebrew,
237
		(
238
#ifdef CONFIG_LIRC_SERIAL_TRANSMITTER
239
		 LIRC_CAN_SET_SEND_DUTY_CYCLE|
240
		 LIRC_CAN_SET_SEND_CARRIER|
241
		 LIRC_CAN_SEND_PULSE|
242
#endif
243
		 LIRC_CAN_REC_MODE2)
244
	}
245
246
};
247
248
#define LIRC_DRIVER_NAME "lirc_serial"
249
250
#define RS_ISR_PASS_LIMIT 256
251
252
/* A long pulse code from a remote might take upto 300 bytes.  The
253
   daemon should read the bytes as soon as they are generated, so take
254
   the number of keys you think you can push before the daemon runs
255
   and multiply by 300.  The driver will warn you if you overrun this
256
   buffer.  If you have a slow computer or non-busmastering IDE disks,
257
   maybe you will need to increase this.  */
258
259
/* This MUST be a power of two!  It has to be larger than 1 as well. */
260
261
#define RBUF_LEN 256
262
#define WBUF_LEN 256
263
264
static struct timeval lasttv = {0, 0};
265
266
static spinlock_t lirc_lock = SPIN_LOCK_UNLOCKED;
267
268
static struct lirc_buffer rbuf;
269
270
static lirc_t wbuf[WBUF_LEN];
271
272
unsigned int freq = 38000;
273
unsigned int duty_cycle = 50;
274
275
/* Initialized in init_timing_params() */
276
unsigned long period = 0;
277
unsigned long pulse_width = 0;
278
unsigned long space_width = 0;
279
280
#if defined(__i386__)
281
/*
282
  From:
283
  Linux I/O port programming mini-HOWTO
284
  Author: Riku Saikkonen <Riku.Saikkonen@hut.fi>
285
  v, 28 December 1997
286
287
  [...]
288
  Actually, a port I/O instruction on most ports in the 0-0x3ff range
289
  takes almost exactly 1 microsecond, so if you're, for example, using
290
  the parallel port directly, just do additional inb()s from that port
291
  to delay.
292
  [...]
293
*/
294
/* transmitter latency 1.5625us 0x1.90 - this figure arrived at from
295
 * comment above plus trimming to match actual measured frequency.
296
 * This will be sensitive to cpu speed, though hopefully most of the 1.5us
297
 * is spent in the uart access.  Still - for reference test machine was a
298
 * 1.13GHz Athlon system - Steve
299
 */
300
301
/* changed from 400 to 450 as this works better on slower machines;
302
   faster machines will use the rdtsc code anyway */
303
304
#define CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY 450
305
306
#else
307
308
/* does anybody have information on other platforms ? */
309
/* 256 = 1<<8 */
310
#define CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY 256
311
312
#endif  /* __i386__ */
313
314
static inline unsigned int sinp(int offset)
315
{
316
	return inb(io + offset);
317
}
318
319
static inline void soutp(int offset, int value)
320
{
321
	outb(value, io + offset);
322
}
323
324
static inline void on(void)
325
{
326
	soutp(UART_MCR,hardware[type].on);
327
}
328
329
static inline void off(void)
330
{
331
	soutp(UART_MCR,hardware[type].off);
332
}
333
334
#ifndef MAX_UDELAY_MS
335
#define MAX_UDELAY_US 5000
336
#else
337
#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
338
#endif
339
340
static inline void safe_udelay(unsigned long usecs)
341
{
342
	while(usecs>MAX_UDELAY_US)
343
	{
344
		udelay(MAX_UDELAY_US);
345
		usecs-=MAX_UDELAY_US;
346
	}
347
	udelay(usecs);
348
}
349
350
#ifdef USE_RDTSC
351
/* This is an overflow/precision juggle, complicated in that we can't
352
   do long long divide in the kernel */
353
354
/* When we use the rdtsc instruction to measure clocks, we keep the
355
 * pulse and space widths as clock cycles.  As this is CPU speed
356
 * dependent, the widths must be calculated in init_port and ioctl
357
 * time
358
 */
359
360
/* So send_pulse can quickly convert microseconds to clocks */
361
unsigned long conv_us_to_clocks = 0;
362
363
static inline int init_timing_params(unsigned int new_duty_cycle,
364
		unsigned int new_freq)
365
{
366
	unsigned long long loops_per_sec,work;
367
368
	duty_cycle=new_duty_cycle;
369
	freq=new_freq;
370
371
	loops_per_sec=current_cpu_data.loops_per_jiffy;
372
	loops_per_sec*=HZ;
373
374
	/* How many clocks in a microsecond?, avoiding long long divide */
375
	work=loops_per_sec;
376
	work*=4295;  /* 4295 = 2^32 / 1e6 */
377
	conv_us_to_clocks=(work>>32);
378
379
	/* Carrier period in clocks, approach good up to 32GHz clock,
380
           gets carrier frequency within 8Hz */
381
	period=loops_per_sec>>3;
382
	period/=(freq>>3);
383
384
	/* Derive pulse and space from the period */
385
386
	pulse_width = period*duty_cycle/100;
387
	space_width = period - pulse_width;
388
	dprintk(LOGHEAD
389
	       ": in init_timing_params, freq=%d, duty_cycle=%d, "
390
	       "clk/jiffy=%ld, pulse=%ld, space=%ld, conv_us_to_clocks=%ld\n",
391
	       freq, duty_cycle, current_cpu_data.loops_per_jiffy,
392
	       pulse_width, space_width, conv_us_to_clocks);
393
	return 0;
394
}
395
#else /* ! USE_RDTSC */
396
static inline int init_timing_params(unsigned int new_duty_cycle,
397
		unsigned int new_freq)
398
{
399
/* period, pulse/space width are kept with 8 binary places -
400
 * IE multiplied by 256. */
401
	if(256*1000000L/new_freq*new_duty_cycle/100<=
402
	   CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY) return(-EINVAL);
403
	if(256*1000000L/new_freq*(100-new_duty_cycle)/100<=
404
	   CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY) return(-EINVAL);
405
	duty_cycle=new_duty_cycle;
406
	freq=new_freq;
407
	period=256*1000000L/freq;
408
	pulse_width=period*duty_cycle/100;
409
	space_width=period-pulse_width;
410
	dprintk(LOGHEAD
411
	       ": in init_timing_params, freq=%d pulse=%ld, "
412
	       "space=%ld\n", freq, pulse_width, space_width);
413
	return 0;
414
}
415
#endif /* USE_RDTSC */
416
417
418
/* return value: space length delta */
419
420
long send_pulse_irdeo(unsigned long length)
421
{
422
	long rawbits;
423
	int i;
424
	unsigned char output;
425
	unsigned char chunk,shifted;
426
427
	/* how many bits have to be sent ? */
428
	rawbits=length*1152/10000;
429
	if(duty_cycle>50) chunk=3;
430
	else chunk=1;
431
	for(i=0,output=0x7f;rawbits>0;rawbits-=3)
432
	{
433
		shifted=chunk<<(i*3);
434
		shifted>>=1;
435
		output&=(~shifted);
436
		i++;
437
		if(i==3)
438
		{
439
			soutp(UART_TX,output);
440
			while(!(sinp(UART_LSR) & UART_LSR_THRE));
441
			output=0x7f;
442
			i=0;
443
		}
444
	}
445
	if(i!=0)
446
	{
447
		soutp(UART_TX,output);
448
		while(!(sinp(UART_LSR) & UART_LSR_TEMT));
449
	}
450
451
	if(i==0)
452
	{
453
		return((-rawbits)*10000/1152);
454
	}
455
	else
456
	{
457
		return((3-i)*3*10000/1152+(-rawbits)*10000/1152);
458
	}
459
}
460
461
#ifdef USE_RDTSC
462
/* Version that uses Pentium rdtsc instruction to measure clocks */
463
464
/* This version does sub-microsecond timing using rdtsc instruction,
465
 * and does away with the fudged CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY
466
 * Implicitly i586 architecture...  - Steve
467
 */
468
469
static inline long send_pulse_homebrew_softcarrier(unsigned long length)
470
{
471
	int flag;
472
	unsigned long target, start, now;
473
474
	/* Get going quick as we can */
475
	rdtscl(start);on();
476
	/* Convert length from microseconds to clocks */
477
	length*=conv_us_to_clocks;
478
	/* And loop till time is up - flipping at right intervals */
479
	now=start;
480
	target=pulse_width;
481
	flag=1;
482
	while((now-start)<length)
483
	{
484
		/* Delay till flip time */
485
		do
486
		{
487
			rdtscl(now);
488
		}
489
		while ((now-start)<target);
490
		/* flip */
491
		if(flag)
492
		{
493
			rdtscl(now);off();
494
			target+=space_width;
495
		}
496
		else
497
		{
498
			rdtscl(now);on();
499
			target+=pulse_width;
500
		}
501
		flag=!flag;
502
	}
503
	rdtscl(now);
504
	return(((now-start)-length)/conv_us_to_clocks);
505
}
506
#else /* ! USE_RDTSC */
507
/* Version using udelay() */
508
509
/* here we use fixed point arithmetic, with 8
510
   fractional bits.  that gets us within 0.1% or so of the right average
511
   frequency, albeit with some jitter in pulse length - Steve */
512
513
/* To match 8 fractional bits used for pulse/space length */
514
515
static inline long send_pulse_homebrew_softcarrier(unsigned long length)
516
{
517
	int flag;
518
	unsigned long actual, target, d;
519
	length<<=8;
520
521
	actual=target=0; flag=0;
522
	while(actual<length)
523
	{
524
		if(flag)
525
		{
526
			off();
527
			target+=space_width;
528
		}
529
		else
530
		{
531
			on();
532
			target+=pulse_width;
533
		}
534
		d=(target-actual-CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY+128)>>8;
535
		/* Note - we've checked in ioctl that the pulse/space
536
		   widths are big enough so that d is > 0 */
537
		udelay(d);
538
		actual+=(d<<8)+CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY;
539
		flag=!flag;
540
	}
541
	return((actual-length)>>8);
542
}
543
#endif /* USE_RDTSC */
544
545
long send_pulse_homebrew(unsigned long length)
546
{
547
	if(length<=0) return 0;
548
	if(softcarrier)
549
	{
550
		return send_pulse_homebrew_softcarrier(length);
551
	}
552
	else
553
	{
554
		on();
555
		safe_udelay(length);
556
		return(0);
557
	}
558
}
559
560
void send_space_irdeo(long length)
561
{
562
	if(length<=0) return;
563
	safe_udelay(length);
564
}
565
566
void send_space_homebrew(long length)
567
{
568
        off();
569
	if(length<=0) return;
570
	safe_udelay(length);
571
}
572
573
static void inline rbwrite(lirc_t l)
574
{
575
	if(lirc_buffer_full(&rbuf))    /* no new signals will be accepted */
576
	{
577
		dprintk(LOGHEAD ": Buffer overrun\n");
578
		return;
579
	}
580
	_lirc_buffer_write_1(&rbuf, (void *)&l);
581
}
582
583
static void inline frbwrite(lirc_t l)
584
{
585
	/* simple noise filter */
586
	static lirc_t pulse=0L,space=0L;
587
	static unsigned int ptr=0;
588
589
	if(ptr>0 && (l&PULSE_BIT))
590
	{
591
		pulse+=l&PULSE_MASK;
592
		if(pulse>250)
593
		{
594
			rbwrite(space);
595
			rbwrite(pulse|PULSE_BIT);
596
			ptr=0;
597
			pulse=0;
598
		}
599
		return;
600
	}
601
	if(!(l&PULSE_BIT))
602
	{
603
		if(ptr==0)
604
		{
605
			if(l>20000)
606
			{
607
				space=l;
608
				ptr++;
609
				return;
610
			}
611
		}
612
		else
613
		{
614
			if(l>20000)
615
			{
616
				space+=pulse;
617
				if(space>PULSE_MASK) space=PULSE_MASK;
618
				space+=l;
619
				if(space>PULSE_MASK) space=PULSE_MASK;
620
				pulse=0;
621
				return;
622
			}
623
			rbwrite(space);
624
			rbwrite(pulse|PULSE_BIT);
625
			ptr=0;
626
			pulse=0;
627
		}
628
	}
629
	rbwrite(l);
630
}
631
632
irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
633
{
634
	struct timeval tv;
635
	int status,counter,dcd;
636
	long deltv;
637
	lirc_t data;
638
639
	counter=0;
640
	do{
641
		counter++;
642
		status=sinp(UART_MSR);
643
		if(counter>RS_ISR_PASS_LIMIT)
644
		{
645
			printk(KERN_WARNING LIRC_DRIVER_NAME ": AIEEEE: "
646
			       "We're caught!\n");
647
			break;
648
		}
649
		if((status&hardware[type].signal_pin_change) && sense!=-1)
650
		{
651
			/* get current time */
652
			do_gettimeofday(&tv);
653
654
			/* New mode, written by Trent Piepho
655
			   <xyzzy@u.washington.edu>. */
656
657
			/* The old format was not very portable.
658
			   We now use the type lirc_t to pass pulses
659
			   and spaces to user space.
660
661
			   If PULSE_BIT is set a pulse has been
662
			   received, otherwise a space has been
663
			   received.  The driver needs to know if your
664
			   receiver is active high or active low, or
665
			   the space/pulse sense could be
666
			   inverted. The bits denoted by PULSE_MASK are
667
			   the length in microseconds. Lengths greater
668
			   than or equal to 16 seconds are clamped to
669
			   PULSE_MASK.  All other bits are unused.
670
			   This is a much simpler interface for user
671
			   programs, as well as eliminating "out of
672
			   phase" errors with space/pulse
673
			   autodetection. */
674
675
			/* calculate time since last interrupt in
676
			   microseconds */
677
			dcd=(status & hardware[type].signal_pin) ? 1:0;
678
679
			deltv=tv.tv_sec-lasttv.tv_sec;
680
			if(deltv>15)
681
			{
682
				dprintk(LOGHEAD
683
				       ": AIEEEE: %d %d %lx %lx %lx %lx\n",
684
				       dcd,sense,
685
				       tv.tv_sec,lasttv.tv_sec,
686
				       tv.tv_usec,lasttv.tv_usec);
687
				data=PULSE_MASK; /* really long time */
688
				if(!(dcd^sense)) /* sanity check */
689
				{
690
				        /* detecting pulse while this
691
					   MUST be a space! */
692
				        sense=sense ? 0:1;
693
				}
694
			}
695
			else
696
			{
697
				data=(lirc_t) (deltv*1000000+
698
					       tv.tv_usec-
699
					       lasttv.tv_usec);
700
			};
701
			if(tv.tv_sec<lasttv.tv_sec ||
702
			   (tv.tv_sec==lasttv.tv_sec &&
703
			    tv.tv_usec<lasttv.tv_usec))
704
			{
705
				printk(KERN_WARNING LIRC_DRIVER_NAME
706
				       ": AIEEEE: your clock just jumped "
707
				       "backwards\n");
708
				printk(KERN_WARNING LIRC_DRIVER_NAME
709
				       ": %d %d %lx %lx %lx %lx\n",
710
				       dcd,sense,
711
				       tv.tv_sec,lasttv.tv_sec,
712
				       tv.tv_usec,lasttv.tv_usec);
713
				data=PULSE_MASK;
714
			}
715
			frbwrite(dcd^sense ? data : (data|PULSE_BIT));
716
			lasttv=tv;
717
			wake_up_interruptible(&rbuf.wait_poll);
718
		}
719
	} while(!(sinp(UART_IIR) & UART_IIR_NO_INT)); /* still pending ? */
720
721
	return IRQ_HANDLED;
722
}
723
724
static DECLARE_WAIT_QUEUE_HEAD(power_supply_queue);
725
static spinlock_t lirc_lock;
726
727
static int init_port(void)
728
{
729
	unsigned long flags;
730
731
	/* Reserve io region. */
732
	if(!request_region(io, 8, LIRC_DRIVER_NAME))
733
	{
734
		printk(KERN_ERR  LIRC_DRIVER_NAME
735
		       ": port %04x already in use\n", io);
736
		printk(KERN_WARNING LIRC_DRIVER_NAME
737
		       ": use 'setserial /dev/ttySX uart none'\n");
738
		printk(KERN_WARNING LIRC_DRIVER_NAME
739
		       ": or compile the serial port driver as module and\n");
740
		printk(KERN_WARNING LIRC_DRIVER_NAME
741
		       ": make sure this module is loaded first\n");
742
		return(-EBUSY);
743
	}
744
745
	lock_kernel();
746
747
	spin_lock_irqsave(&lirc_lock, flags);
748
749
	/* Set DLAB 0. */
750
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
751
752
	/* First of all, disable all interrupts */
753
	soutp(UART_IER, sinp(UART_IER)&
754
	      (~(UART_IER_MSI|UART_IER_RLSI|UART_IER_THRI|UART_IER_RDI)));
755
756
	/* Clear registers. */
757
	sinp(UART_LSR);
758
	sinp(UART_RX);
759
	sinp(UART_IIR);
760
	sinp(UART_MSR);
761
762
	/* Set line for power source */
763
	soutp(UART_MCR, hardware[type].off);
764
765
	/* Clear registers again to be sure. */
766
	sinp(UART_LSR);
767
	sinp(UART_RX);
768
	sinp(UART_IIR);
769
	sinp(UART_MSR);
770
771
	switch(hardware[type].type)
772
	{
773
	case LIRC_IRDEO:
774
	case LIRC_IRDEO_REMOTE:
775
		/* setup port to 7N1 @ 115200 Baud */
776
		/* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
777
778
		/* Set DLAB 1. */
779
		soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
780
		/* Set divisor to 1 => 115200 Baud */
781
		soutp(UART_DLM,0);
782
		soutp(UART_DLL,1);
783
		/* Set DLAB 0 +  7N1 */
784
		soutp(UART_LCR,UART_LCR_WLEN7);
785
		/* THR interrupt already disabled at this point */
786
		break;
787
	default:
788
		break;
789
	}
790
791
	spin_unlock_irqrestore(&lirc_lock, flags);
792
793
	/* Initialize pulse/space widths */
794
	init_timing_params(duty_cycle, freq);
795
796
	/* If pin is high, then this must be an active low receiver. */
797
	if(sense==-1)
798
	{
799
		/* wait 1 sec for the power supply */
800
801
		sleep_on_timeout(&power_supply_queue,HZ);
802
803
		sense=(sinp(UART_MSR) & hardware[type].signal_pin) ? 1:0;
804
		printk(KERN_INFO  LIRC_DRIVER_NAME  ": auto-detected active "
805
		       "%s receiver\n",sense ? "low":"high");
806
	}
807
	else
808
	{
809
		printk(KERN_INFO  LIRC_DRIVER_NAME  ": Manually using active "
810
		       "%s receiver\n",sense ? "low":"high");
811
	};
812
813
	unlock_kernel();
814
815
	return 0;
816
}
817
818
static int set_use_inc(void* data)
819
{
820
	int result;
821
	unsigned long flags;
822
823
	spin_lock(&lirc_lock);
824
	if(module_refcount(THIS_MODULE))
825
	{
826
		spin_unlock(&lirc_lock);
827
		return -EBUSY;
828
	}
829
830
	/* initialize timestamp */
831
	do_gettimeofday(&lasttv);
832
833
	result=request_irq(irq,irq_handler,SA_INTERRUPT,LIRC_DRIVER_NAME,NULL);
834
	switch(result)
835
	{
836
	case -EBUSY:
837
		printk(KERN_ERR LIRC_DRIVER_NAME ": IRQ %d busy\n", irq);
838
		spin_unlock(&lirc_lock);
839
		return -EBUSY;
840
	case -EINVAL:
841
		printk(KERN_ERR LIRC_DRIVER_NAME
842
		       ": Bad irq number or handler\n");
843
		spin_unlock(&lirc_lock);
844
		return -EINVAL;
845
	default:
846
		dprintk(LOGHEAD
847
		       ": Interrupt %d, port %04x obtained\n", irq, io);
848
		break;
849
	};
850
851
	local_irq_save(flags);
852
853
	/* Set DLAB 0. */
854
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
855
856
	soutp(UART_IER, sinp(UART_IER)|UART_IER_MSI);
857
858
	local_irq_restore(flags);
859
860
	try_module_get(THIS_MODULE);
861
	spin_unlock(&lirc_lock);
862
	return 0;
863
}
864
865
static void set_use_dec(void* data)
866
{	unsigned long flags;
867
868
	spin_lock_irqsave(&lirc_lock, flags);
869
870
	/* Set DLAB 0. */
871
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
872
873
	/* First of all, disable all interrupts */
874
	soutp(UART_IER, sinp(UART_IER)&
875
	      (~(UART_IER_MSI|UART_IER_RLSI|UART_IER_THRI|UART_IER_RDI)));
876
	spin_unlock_irqrestore(&lirc_lock, flags);
877
878
	free_irq(irq, NULL);
879
	dprintk(LOGHEAD ": freed IRQ %d\n", irq);
880
881
	module_put(THIS_MODULE);
882
}
883
884
static ssize_t lirc_write(struct file *file, const char *buf,
885
			 size_t n, loff_t * ppos)
886
{
887
	int retval,i,count;
888
	unsigned long flags;
889
	long delta=0;
890
891
	if(!(hardware[type].features&LIRC_CAN_SEND_PULSE))
892
	{
893
		return(-EBADF);
894
	}
895
896
	if(n%sizeof(lirc_t)) return(-EINVAL);
897
	retval=verify_area(VERIFY_READ,buf,n);
898
	if(retval) return(retval);
899
	count=n/sizeof(lirc_t);
900
	if(count>WBUF_LEN || count%2==0) return(-EINVAL);
901
	copy_from_user(wbuf,buf,n);
902
	spin_lock_irqsave(&lirc_lock, flags);
903
	if(hardware[type].type==LIRC_IRDEO)
904
	{
905
		/* DTR, RTS down */
906
		on();
907
	}
908
	for(i=0;i<count;i++)
909
	{
910
		if(i%2) hardware[type].send_space(wbuf[i]-delta);
911
		else delta=hardware[type].send_pulse(wbuf[i]);
912
	}
913
	off();
914
	spin_unlock_irqrestore(&lirc_lock, flags);
915
	return(n);
916
}
917
918
static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
919
		      unsigned long arg)
920
{
921
        int result;
922
	unsigned long value;
923
	unsigned int ivalue;
924
925
	switch(cmd)
926
	{
927
	case LIRC_GET_SEND_MODE:
928
		if(!(hardware[type].features&LIRC_CAN_SEND_MASK))
929
		{
930
			return(-ENOIOCTLCMD);
931
		}
932
933
		result=put_user(LIRC_SEND2MODE
934
				(hardware[type].features&LIRC_CAN_SEND_MASK),
935
				(unsigned long *) arg);
936
		if(result) return(result);
937
		break;
938
939
	case LIRC_SET_SEND_MODE:
940
		if(!(hardware[type].features&LIRC_CAN_SEND_MASK))
941
		{
942
			return(-ENOIOCTLCMD);
943
		}
944
945
		result=get_user(value,(unsigned long *) arg);
946
		if(result) return(result);
947
		/* only LIRC_MODE_PULSE supported */
948
		if(value!=LIRC_MODE_PULSE) return(-ENOSYS);
949
		break;
950
951
	case LIRC_GET_LENGTH:
952
		return(-ENOSYS);
953
		break;
954
955
	case LIRC_SET_SEND_DUTY_CYCLE:
956
		dprintk(LOGHEAD ": SET_SEND_DUTY_CYCLE\n");
957
		if(!(hardware[type].features&LIRC_CAN_SET_SEND_DUTY_CYCLE))
958
		{
959
			return(-ENOIOCTLCMD);
960
		}
961
962
		result=get_user(ivalue,(unsigned int *) arg);
963
		if(result) return(result);
964
		if(ivalue<=0 || ivalue>100) return(-EINVAL);
965
		return init_timing_params(ivalue, freq);
966
		break;
967
968
	case LIRC_SET_SEND_CARRIER:
969
		dprintk(LOGHEAD ": SET_SEND_CARRIER\n");
970
		if(!(hardware[type].features&LIRC_CAN_SET_SEND_CARRIER))
971
		{
972
			return(-ENOIOCTLCMD);
973
		}
974
975
		result=get_user(ivalue,(unsigned int *) arg);
976
		if(result) return(result);
977
		if(ivalue>500000 || ivalue<20000) return(-EINVAL);
978
		return init_timing_params(duty_cycle, ivalue);
979
		break;
980
981
	default:
982
		return(-ENOIOCTLCMD);
983
	}
984
	return(0);
985
}
986
987
static struct file_operations lirc_fops =
988
{
989
	write:   lirc_write,
990
};
991
992
static struct lirc_plugin plugin = {
993
	name:		LIRC_DRIVER_NAME,
994
	minor:		-1,
995
	code_length:	1,
996
	sample_rate:	0,
997
	data:		NULL,
998
	get_key:	NULL,
999
	get_queue:	NULL,
1000
	rbuf:		&rbuf,
1001
	set_use_inc:	set_use_inc,
1002
	set_use_dec:	set_use_dec,
1003
	ioctl:		lirc_ioctl,
1004
	fops:		&lirc_fops,
1005
};
1006
1007
MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus");
1008
MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
1009
MODULE_LICENSE("GPL");
1010
1011
static int __init lirc_serial_init(void)
1012
{
1013
	int result;
1014
1015
	switch(type)
1016
	{
1017
	case LIRC_HOMEBREW:
1018
	case LIRC_IRDEO:
1019
	case LIRC_IRDEO_REMOTE:
1020
	case LIRC_ANIMAX:
1021
	case LIRC_IGOR:
1022
		break;
1023
	default:
1024
		return(-EINVAL);
1025
	}
1026
	if(!softcarrier && hardware[type].type==LIRC_HOMEBREW)
1027
	{
1028
		hardware[type].features&=~(LIRC_CAN_SET_SEND_DUTY_CYCLE|
1029
					   LIRC_CAN_SET_SEND_CARRIER);
1030
	}
1031
	if ((result = init_port()) < 0)
1032
		return result;
1033
	plugin.features = hardware[type].features;
1034
	lirc_buffer_init(&rbuf, sizeof(lirc_t), RBUF_LEN);
1035
	if ((plugin.minor = lirc_register_plugin(&plugin)) < 0) {
1036
		printk(KERN_ERR  LIRC_DRIVER_NAME
1037
		       ": register_chrdev failed!\n");
1038
		release_region(io, 8);
1039
		return -EIO;
1040
	}
1041
	return 0;
1042
}
1043
1044
static void __exit lirc_serial_exit(void)
1045
{
1046
	release_region(io, 8);
1047
	lirc_buffer_free(&rbuf);
1048
	lirc_unregister_plugin(plugin.minor);
1049
	dprintk(LOGHEAD ": cleaned up module\n");
1050
}
1051
1052
module_init(lirc_serial_init);
1053
module_exit(lirc_serial_exit);
(-)linux-2.6.2/drivers/char/lirc/lirc_sir.c (+1303 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
	if (module_refcount(THIS_MODULE)) {
254
		spin_unlock(&dev_lock);
255
		return -EBUSY;
256
	}
257
	if (!try_module_get(THIS_MODULE)) {
258
		spin_unlock(&dev_lock);
259
		return -EINVAL;
260
	}
261
	spin_unlock(&dev_lock);
262
	return 0;
263
}
264
265
static int lirc_close(struct inode * inode, struct file *file)
266
{
267
	module_put(THIS_MODULE);
268
	return 0;
269
}
270
271
static unsigned int lirc_poll(struct file * file, poll_table * wait)
272
{
273
	poll_wait(file, &lirc_read_queue, wait);
274
	if (rx_head != rx_tail)
275
		return POLLIN | POLLRDNORM;
276
	return 0;
277
}
278
279
static ssize_t lirc_read(struct file * file, char * buf, size_t count,
280
		loff_t * ppos)
281
{
282
	int n=0;
283
	int retval = 0;
284
	DECLARE_WAITQUEUE(wait,current);
285
286
	if(n%sizeof(lirc_t)) return(-EINVAL);
287
288
	add_wait_queue(&lirc_read_queue,&wait);
289
	current->state=TASK_INTERRUPTIBLE;
290
	while(n<count)
291
	{
292
		if(rx_head!=rx_tail)
293
		{
294
			retval=verify_area(VERIFY_WRITE,
295
					   (void *) buf+n,sizeof(lirc_t));
296
			if (retval)
297
			{
298
				return retval;
299
			}
300
			copy_to_user((void *) buf+n,(void *) (rx_buf+rx_head),
301
				     sizeof(lirc_t));
302
			rx_head=(rx_head+1)&(RBUF_LEN-1);
303
			n+=sizeof(lirc_t);
304
		}
305
		else
306
		{
307
			if(file->f_flags & O_NONBLOCK)
308
			{
309
				retval=-EAGAIN;
310
				break;
311
			}
312
			if(signal_pending(current))
313
			{
314
				retval=-ERESTARTSYS;
315
				break;
316
			}
317
			schedule();
318
			current->state=TASK_INTERRUPTIBLE;
319
		}
320
	}
321
	remove_wait_queue(&lirc_read_queue,&wait);
322
	current->state=TASK_RUNNING;
323
	return (n ? n : retval);
324
}
325
static ssize_t lirc_write(struct file * file, const char * buf, size_t n, loff_t * pos)
326
{
327
	unsigned long flags;
328
#ifdef CONFIG_LIRC_SIR_TEKRAM
329
	return(-EBADF);
330
#else
331
	int i;
332
	int retval;
333
334
        if(n%sizeof(lirc_t) || (n/sizeof(lirc_t)) > WBUF_LEN)
335
		return(-EINVAL);
336
	retval = verify_area(VERIFY_READ, buf, n);
337
	if (retval)
338
		return retval;
339
	copy_from_user(tx_buf, buf, n);
340
	i = 0;
341
	n/=sizeof(lirc_t);
342
#ifdef CONFIG_LIRC_ON_SA1100
343
	/* disable receiver */
344
	Ser2UTCR3=0;
345
#endif
346
	spin_lock_irqsave(&lirc_lock, flags);
347
	while (1) {
348
		if (i >= n)
349
			break;
350
		if (tx_buf[i])
351
			send_pulse(tx_buf[i]);
352
		i++;
353
		if (i >= n)
354
			break;
355
		if (tx_buf[i])
356
			send_space(tx_buf[i]);
357
		i++;
358
	}
359
	spin_unlock_irqrestore(&lirc_lock, flags);
360
#ifdef CONFIG_LIRC_ON_SA1100
361
	off();
362
	udelay(1000); /* wait 1ms for IR diode to recover */
363
	Ser2UTCR3=0;
364
	/* clear status register to prevent unwanted interrupts */
365
	Ser2UTSR0 &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
366
	/* enable receiver */
367
	Ser2UTCR3=UTCR3_RXE|UTCR3_RIE;
368
#endif
369
	return n;
370
#endif
371
}
372
373
static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
374
		unsigned long arg)
375
{
376
	int retval = 0;
377
	unsigned long value = 0;
378
#ifdef CONFIG_LIRC_ON_SA1100
379
	unsigned int ivalue;
380
#endif
381
382
#ifdef CONFIG_LIRC_SIR_TEKRAM
383
	if (cmd == LIRC_GET_FEATURES)
384
		value = LIRC_CAN_REC_MODE2;
385
	else if (cmd == LIRC_GET_SEND_MODE)
386
		value = 0;
387
	else if (cmd == LIRC_GET_REC_MODE)
388
		value = LIRC_MODE_MODE2;
389
#elif defined(CONFIG_LIRC_ON_SA1100)
390
	if (cmd == LIRC_GET_FEATURES)
391
		value = LIRC_CAN_SEND_PULSE |
392
			LIRC_CAN_SET_SEND_DUTY_CYCLE |
393
			LIRC_CAN_SET_SEND_CARRIER |
394
			LIRC_CAN_REC_MODE2;
395
	else if (cmd == LIRC_GET_SEND_MODE)
396
		value = LIRC_MODE_PULSE;
397
	else if (cmd == LIRC_GET_REC_MODE)
398
		value = LIRC_MODE_MODE2;
399
#else
400
	if (cmd == LIRC_GET_FEATURES)
401
		value = LIRC_CAN_SEND_PULSE | LIRC_CAN_REC_MODE2;
402
	else if (cmd == LIRC_GET_SEND_MODE)
403
		value = LIRC_MODE_PULSE;
404
	else if (cmd == LIRC_GET_REC_MODE)
405
		value = LIRC_MODE_MODE2;
406
#endif
407
408
	switch (cmd) {
409
	case LIRC_GET_FEATURES:
410
	case LIRC_GET_SEND_MODE:
411
	case LIRC_GET_REC_MODE:
412
		retval = put_user(value, (unsigned long *) arg);
413
		break;
414
415
	case LIRC_SET_SEND_MODE:
416
	case LIRC_SET_REC_MODE:
417
		retval = get_user(value, (unsigned long *) arg);
418
		break;
419
#ifdef CONFIG_LIRC_ON_SA1100
420
	case LIRC_SET_SEND_DUTY_CYCLE:
421
		retval=get_user(ivalue,(unsigned int *) arg);
422
		if(retval) return(retval);
423
		if(ivalue<=0 || ivalue>100) return(-EINVAL);
424
		/* (ivalue/100)*(1000000/freq) */
425
		duty_cycle=ivalue;
426
		pulse_width=(unsigned long) duty_cycle*10000/freq;
427
		space_width=(unsigned long) 1000000L/freq-pulse_width;
428
		if(pulse_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
429
			pulse_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
430
		if(space_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
431
			space_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
432
		break;
433
	case LIRC_SET_SEND_CARRIER:
434
		retval=get_user(ivalue,(unsigned int *) arg);
435
		if(retval) return(retval);
436
		if(ivalue>500000 || ivalue<20000) return(-EINVAL);
437
		freq=ivalue;
438
		pulse_width=(unsigned long) duty_cycle*10000/freq;
439
		space_width=(unsigned long) 1000000L/freq-pulse_width;
440
		if(pulse_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
441
			pulse_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
442
		if(space_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
443
			space_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
444
		break;
445
#endif
446
	default:
447
		retval = -ENOIOCTLCMD;
448
449
	}
450
451
	if (retval)
452
		return retval;
453
454
#ifdef CONFIG_LIRC_SIR_TEKRAM
455
	if (cmd == LIRC_SET_REC_MODE) {
456
		if (value != LIRC_MODE_MODE2)
457
			retval = -ENOSYS;
458
	} else if (cmd == LIRC_SET_SEND_MODE) {
459
		retval = -ENOSYS;
460
	}
461
#else
462
	if (cmd == LIRC_SET_REC_MODE) {
463
		if (value != LIRC_MODE_MODE2)
464
			retval = -ENOSYS;
465
	} else if (cmd == LIRC_SET_SEND_MODE) {
466
		if (value != LIRC_MODE_PULSE)
467
			retval = -ENOSYS;
468
	}
469
#endif
470
	return retval;
471
}
472
473
static void add_read_queue(int flag, unsigned long val)
474
{
475
	unsigned int new_rx_tail;
476
	lirc_t newval;
477
478
#ifdef DEBUG_SIGNAL
479
	printk(KERN_DEBUG LIRC_DRIVER_NAME
480
		": add flag %d with val %lu\n",
481
		flag,val);
482
#endif
483
484
	newval = val & PULSE_MASK;
485
486
	/* statistically pulses are ~TIME_CONST/2 too long: we could
487
	   maybe make this more exactly but this is good enough */
488
	if(flag) /* pulse */
489
	{
490
		if(newval>TIME_CONST/2)
491
		{
492
			newval-=TIME_CONST/2;
493
		}
494
		else /* should not ever happen */
495
		{
496
			newval=1;
497
		}
498
		newval|=PULSE_BIT;
499
	}
500
	else
501
	{
502
		newval+=TIME_CONST/2;
503
	}
504
	new_rx_tail = (rx_tail + 1) & (RBUF_LEN - 1);
505
	if (new_rx_tail == rx_head) {
506
#               ifdef DEBUG
507
		printk(KERN_WARNING LIRC_DRIVER_NAME ": Buffer overrun.\n");
508
#               endif
509
		return;
510
	}
511
	rx_buf[rx_tail] = newval;
512
	rx_tail = new_rx_tail;
513
	wake_up_interruptible(&lirc_read_queue);
514
}
515
516
static struct file_operations lirc_fops =
517
{
518
	.read    = lirc_read,
519
	.write   = lirc_write,
520
	.poll    = lirc_poll,
521
	.ioctl   = lirc_ioctl,
522
	.open    = lirc_open,
523
	.release = lirc_close,
524
};
525
526
static int set_use_inc(void* data)
527
{
528
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
529
       if (!try_module_get(THIS_MODULE)) {
530
                return -EINVAL;
531
       }
532
#endif
533
       return 0;
534
}
535
536
static void set_use_dec(void* data)
537
{
538
#if WE_DONT_USE_LOCAL_OPEN_CLOSE
539
       module_put(THIS_MODULE);
540
#endif
541
}
542
static struct lirc_plugin plugin = {
543
       .name          = LIRC_DRIVER_NAME,
544
       .minor         = -1,
545
       .code_length   = 1,
546
       .sample_rate   = 0,
547
       .data          = NULL,
548
       .get_key       = NULL,
549
       .get_queue     = NULL,
550
       .set_use_inc   = set_use_inc,
551
       .set_use_dec   = set_use_dec,
552
       .fops          = &lirc_fops,
553
};
554
555
int init_chrdev(void)
556
{
557
	plugin.minor = lirc_register_plugin(&plugin);
558
	if (plugin.minor < 0) {
559
		printk(KERN_ERR LIRC_DRIVER_NAME ": init_chrdev() failed.\n");
560
		return -EIO;
561
	}
562
	return 0;
563
}
564
565
static void drop_chrdev(void)
566
{
567
	lirc_unregister_plugin(plugin.minor);
568
}
569
570
/* SECTION: Hardware */
571
static long delta(struct timeval * tv1, struct timeval * tv2)
572
{
573
	unsigned long deltv;
574
575
	deltv = tv2->tv_sec - tv1->tv_sec;
576
	if (deltv > 15)
577
		deltv = 0xFFFFFF;
578
	else
579
		deltv = deltv*1000000 +
580
			tv2->tv_usec -
581
			tv1->tv_usec;
582
	return deltv;
583
}
584
585
static void sir_timeout(unsigned long data)
586
{
587
	/* if last received signal was a pulse, but receiving stopped
588
	   within the 9 bit frame, we need to finish this pulse and
589
	   simulate a signal change to from pulse to space. Otherwise
590
	   upper layers will receive two sequences next time. */
591
592
	unsigned long flags;
593
	unsigned long pulse_end;
594
595
	/* avoid interference with interrupt */
596
 	spin_lock_irqsave(&timer_lock, flags);
597
	if (last_value)
598
	{
599
#ifndef CONFIG_LIRC_ON_SA1100
600
		/* clear unread bits in UART and restart */
601
		outb(UART_FCR_CLEAR_RCVR, io + UART_FCR);
602
#endif
603
		/* determine 'virtual' pulse end: */
604
	 	pulse_end = delta(&last_tv, &last_intr_tv);
605
#ifdef DEBUG_SIGNAL
606
		printk(KERN_DEBUG LIRC_DRIVER_NAME
607
			": timeout add %d for %lu usec\n",last_value,pulse_end);
608
#endif
609
		add_read_queue(last_value,pulse_end);
610
		last_value = 0;
611
		last_tv=last_intr_tv;
612
	}
613
	spin_unlock_irqrestore(&timer_lock, flags);
614
}
615
616
static irqreturn_t sir_interrupt(int irq, void *dev_id, struct pt_regs *regs)
617
{
618
	unsigned char data;
619
	struct timeval curr_tv;
620
	static unsigned long deltv;
621
#ifdef CONFIG_LIRC_ON_SA1100
622
	int status;
623
	static int n=0;
624
625
	//printk("interrupt\n");
626
	status = Ser2UTSR0;
627
	/*
628
	 * Deal with any receive errors first.  The bytes in error may be
629
	 * the only bytes in the receive FIFO, so we do this first.
630
	 */
631
	while (status & UTSR0_EIF)
632
	{
633
		int bstat;
634
635
#ifdef DEBUG
636
		printk("EIF\n");
637
		bstat = Ser2UTSR1;
638
639
		if (bstat & UTSR1_FRE)
640
			printk("frame error\n");
641
		if (bstat & UTSR1_ROR)
642
			printk("receive fifo overrun\n");
643
		if(bstat&UTSR1_PRE)
644
			printk("parity error\n");
645
#endif
646
647
		bstat = Ser2UTDR;
648
		n++;
649
		status = Ser2UTSR0;
650
	}
651
652
	if (status & (UTSR0_RFS | UTSR0_RID))
653
	{
654
		do_gettimeofday(&curr_tv);
655
		deltv = delta(&last_tv, &curr_tv);
656
		do
657
		{
658
#ifdef DEBUG_SIGNAL
659
			printk(KERN_DEBUG LIRC_DRIVER_NAME": t %lu , d %d\n",
660
			       deltintrtv,(int)data);
661
#endif
662
			data=Ser2UTDR;
663
			//printk("data: %d\n",data);
664
			n++;
665
		}
666
		while(status&UTSR0_RID && /* do not empty fifo in
667
                                             order to get UTSR0_RID in
668
                                             any case */
669
		      Ser2UTSR1 & UTSR1_RNE); /* data ready */
670
671
		if(status&UTSR0_RID)
672
		{
673
			//printk("add\n");
674
			add_read_queue(0,deltv-n*TIME_CONST); /*space*/
675
			add_read_queue(1,n*TIME_CONST); /*pulse*/
676
			n=0;
677
			last_tv=curr_tv;
678
		}
679
	}
680
681
	if (status & UTSR0_TFS) {
682
683
		printk("transmit fifo not full, shouldn't ever happen\n");
684
	}
685
686
	/*
687
	 * We must clear certain bits.
688
	 */
689
	status &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
690
	if (status)
691
		Ser2UTSR0 = status;
692
#else
693
	unsigned long deltintrtv;
694
	unsigned long flags;
695
	int iir, lsr;
696
697
	while ((iir = inb(io + UART_IIR) & UART_IIR_ID)) {
698
		switch (iir&UART_IIR_ID) { /* FIXME toto treba preriedit */
699
		case UART_IIR_MSI:
700
			(void) inb(io + UART_MSR);
701
			break;
702
		case UART_IIR_RLSI:
703
			(void) inb(io + UART_LSR);
704
			break;
705
		case UART_IIR_THRI:
706
#if 0
707
			if (lsr & UART_LSR_THRE) /* FIFO is empty */
708
				outb(data, io + UART_TX)
709
#endif
710
			break;
711
		case UART_IIR_RDI:
712
			/* avoid interference with timer */
713
		 	spin_lock_irqsave(&timer_lock, flags);
714
			do
715
			{
716
				del_timer(&timerlist);
717
				data = inb(io + UART_RX);
718
				do_gettimeofday(&curr_tv);
719
				deltv = delta(&last_tv, &curr_tv);
720
				deltintrtv = delta(&last_intr_tv, &curr_tv);
721
#ifdef DEBUG_SIGNAL
722
				printk(KERN_DEBUG LIRC_DRIVER_NAME": t %lu , d %d\n",deltintrtv,(int)data);
723
#endif
724
				/* if nothing came in last X cycles,
725
				   it was gap */
726
				if (deltintrtv > TIME_CONST * threshold) {
727
					if (last_value) {
728
#ifdef DEBUG_SIGNAL
729
						printk(KERN_DEBUG LIRC_DRIVER_NAME ": GAP\n");
730
#endif
731
						/* simulate signal change */
732
						add_read_queue(last_value,
733
							       deltv-
734
							       deltintrtv);
735
						last_value = 0;
736
						last_tv.tv_sec = last_intr_tv.tv_sec;
737
						last_tv.tv_usec = last_intr_tv.tv_usec;
738
						deltv = deltintrtv;
739
					}
740
				}
741
				data = 1;
742
				if (data ^ last_value) {
743
					/* deltintrtv > 2*TIME_CONST,
744
                                           remember ? */
745
					/* the other case is timeout */
746
					add_read_queue(last_value,
747
						       deltv-TIME_CONST);
748
					last_value = data;
749
					last_tv = curr_tv;
750
					if(last_tv.tv_usec>=TIME_CONST)
751
					{
752
						last_tv.tv_usec-=TIME_CONST;
753
					}
754
					else
755
					{
756
						last_tv.tv_sec--;
757
						last_tv.tv_usec+=1000000-
758
							TIME_CONST;
759
					}
760
				}
761
				last_intr_tv = curr_tv;
762
				if (data)
763
				{
764
					/* start timer for end of sequence detection */
765
					timerlist.expires = jiffies + SIR_TIMEOUT;
766
					add_timer(&timerlist);
767
				}
768
			}
769
			while ((lsr = inb(io + UART_LSR))
770
				& UART_LSR_DR); /* data ready */
771
			spin_unlock_irqrestore(&timer_lock, flags);
772
			break;
773
		default:
774
			break;
775
		}
776
	}
777
#endif
778
	return IRQ_HANDLED;
779
}
780
781
#ifdef CONFIG_LIRC_ON_SA1100
782
void send_pulse(unsigned long length)
783
{
784
	unsigned long k,delay;
785
	int flag;
786
787
	if(length==0) return;
788
	/* this won't give us the carrier frequency we really want
789
	   due to integer arithmetic, but we can accept this inaccuracy */
790
791
	for(k=flag=0;k<length;k+=delay,flag=!flag)
792
	{
793
		if(flag)
794
		{
795
			off();
796
			delay=space_width;
797
		}
798
		else
799
		{
800
			on();
801
			delay=pulse_width;
802
		}
803
		safe_udelay(delay);
804
	}
805
	off();
806
}
807
808
void send_space(unsigned long length)
809
{
810
	if(length==0) return;
811
	off();
812
	safe_udelay(length);
813
}
814
#elif defined(CONFIG_LIRC_SIR_TEKRAM)
815
#else
816
static void send_space(unsigned long len)
817
{
818
	safe_udelay(len);
819
}
820
821
static void send_pulse(unsigned long len)
822
{
823
	long bytes_out = len / TIME_CONST;
824
	long time_left;
825
826
	if (!bytes_out)
827
		bytes_out++;
828
	time_left = (long)len - (long)bytes_out * (long)TIME_CONST;
829
	while (--bytes_out) {
830
		outb(PULSE, io + UART_TX);
831
		/* FIXME treba seriozne cakanie z drivers/char/serial.c */
832
		while (!(inb(io + UART_LSR) & UART_LSR_THRE));
833
	}
834
#if 0
835
	if (time_left > 0)
836
		safe_udelay(time_left);
837
#endif
838
}
839
#endif
840
841
#ifdef CONFIG_SA1100_COLLIE
842
static inline int sa1100_irda_set_power_collie(int state)
843
{
844
	if (state) {
845
		/*
846
		 *  0 - off
847
		 *  1 - short range, lowest power
848
		 *  2 - medium range, medium power
849
		 *  3 - maximum range, high power
850
		 */
851
		ucb1200_set_io_direction(TC35143_GPIO_IR_ON,
852
					 TC35143_IODIR_OUTPUT);
853
		ucb1200_set_io(TC35143_GPIO_IR_ON, TC35143_IODAT_LOW);
854
		udelay(100);
855
	}
856
	else {
857
		/* OFF */
858
		ucb1200_set_io_direction(TC35143_GPIO_IR_ON,
859
					 TC35143_IODIR_OUTPUT);
860
		ucb1200_set_io(TC35143_GPIO_IR_ON, TC35143_IODAT_HIGH);
861
	}
862
	return 0;
863
}
864
#endif
865
866
static int init_hardware(void)
867
{
868
	int flags;
869
870
	spin_lock_irqsave(&hardware_lock, flags);
871
	/* reset UART */
872
#ifdef CONFIG_LIRC_ON_SA1100
873
#ifdef CONFIG_SA1100_BITSY
874
	if (machine_is_bitsy()) {
875
		printk("Power on IR module\n");
876
		set_bitsy_egpio(EGPIO_BITSY_IR_ON);
877
	}
878
#endif
879
#ifdef CONFIG_SA1100_COLLIE
880
	sa1100_irda_set_power_collie(3);	/* power on */
881
#endif
882
	sr.hscr0=Ser2HSCR0;
883
884
	sr.utcr0=Ser2UTCR0;
885
	sr.utcr1=Ser2UTCR1;
886
	sr.utcr2=Ser2UTCR2;
887
	sr.utcr3=Ser2UTCR3;
888
	sr.utcr4=Ser2UTCR4;
889
890
	sr.utdr=Ser2UTDR;
891
	sr.utsr0=Ser2UTSR0;
892
	sr.utsr1=Ser2UTSR1;
893
894
	/* configure GPIO */
895
	/* output */
896
	PPDR|=PPC_TXD2;
897
	PSDR|=PPC_TXD2;
898
	/* set output to 0 */
899
	off();
900
901
	/*
902
	 * Enable HP-SIR modulation, and ensure that the port is disabled.
903
	 */
904
	Ser2UTCR3=0;
905
	Ser2HSCR0=sr.hscr0 & (~HSCR0_HSSP);
906
907
	/* clear status register to prevent unwanted interrupts */
908
	Ser2UTSR0 &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
909
910
	/* 7N1 */
911
	Ser2UTCR0=UTCR0_1StpBit|UTCR0_7BitData;
912
	/* 115200 */
913
	Ser2UTCR1=0;
914
	Ser2UTCR2=1;
915
	/* use HPSIR, 1.6 usec pulses */
916
	Ser2UTCR4=UTCR4_HPSIR|UTCR4_Z1_6us;
917
918
	/* enable receiver, receive fifo interrupt */
919
	Ser2UTCR3=UTCR3_RXE|UTCR3_RIE;
920
921
	/* clear status register to prevent unwanted interrupts */
922
	Ser2UTSR0 &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
923
924
#elif defined(CONFIG_LIRC_SIR_TEKRAM)
925
	/* disable FIFO */
926
	soutp(UART_FCR,
927
	      UART_FCR_CLEAR_RCVR|
928
	      UART_FCR_CLEAR_XMIT|
929
	      UART_FCR_TRIGGER_1);
930
931
	/* Set DLAB 0. */
932
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
933
934
	/* First of all, disable all interrupts */
935
	soutp(UART_IER, sinp(UART_IER)&
936
	      (~(UART_IER_MSI|UART_IER_RLSI|UART_IER_THRI|UART_IER_RDI)));
937
938
	/* Set DLAB 1. */
939
	soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
940
941
	/* Set divisor to 12 => 9600 Baud */
942
	soutp(UART_DLM,0);
943
	soutp(UART_DLL,12);
944
945
	/* Set DLAB 0. */
946
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
947
948
	/* power supply */
949
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
950
	safe_udelay(50*1000);
951
952
	/* -DTR low -> reset PIC */
953
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_OUT2);
954
	udelay(1*1000);
955
956
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
957
	udelay(100);
958
959
960
        /* -RTS low -> send control byte */
961
	soutp(UART_MCR, UART_MCR_DTR|UART_MCR_OUT2);
962
	udelay(7);
963
	soutp(UART_TX, TEKRAM_115200|TEKRAM_PW);
964
965
	/* one byte takes ~1042 usec to transmit at 9600,8N1 */
966
	udelay(1500);
967
968
	/* back to normal operation */
969
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
970
	udelay(50);
971
972
	udelay(1500);
973
974
	/* read previous control byte */
975
	printk(KERN_INFO LIRC_DRIVER_NAME
976
	       ": 0x%02x\n",sinp(UART_RX));
977
978
	/* Set DLAB 1. */
979
	soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
980
981
	/* Set divisor to 1 => 115200 Baud */
982
	soutp(UART_DLM,0);
983
	soutp(UART_DLL,1);
984
985
	/* Set DLAB 0, 8 Bit */
986
	soutp(UART_LCR, UART_LCR_WLEN8);
987
	/* enable interrupts */
988
	soutp(UART_IER, sinp(UART_IER)|UART_IER_RDI);
989
#else
990
	outb(0, io + UART_MCR);
991
	outb(0, io + UART_IER);
992
	/* init UART */
993
		/* set DLAB, speed = 115200 */
994
	outb(UART_LCR_DLAB | UART_LCR_WLEN7, io + UART_LCR);
995
	outb(1, io + UART_DLL); outb(0, io + UART_DLM);
996
		/* 7N1+start = 9 bits at 115200 ~ 3 bits at 44000 */
997
	outb(UART_LCR_WLEN7, io + UART_LCR);
998
		/* FIFO operation */
999
	outb(UART_FCR_ENABLE_FIFO, io + UART_FCR);
1000
		/* interrupts */
1001
	// outb(UART_IER_RLSI|UART_IER_RDI|UART_IER_THRI, io + UART_IER);
1002
	outb(UART_IER_RDI, io + UART_IER);
1003
	/* turn on UART */
1004
	outb(UART_MCR_DTR|UART_MCR_RTS|UART_MCR_OUT2, io + UART_MCR);
1005
#ifdef CONFIG_LIRC_SIR_ACTISYS_ACT200L
1006
	init_act200();
1007
#endif
1008
#endif
1009
	spin_unlock_irqrestore(&hardware_lock, flags);
1010
	return 0;
1011
}
1012
1013
static void drop_hardware(void)
1014
{
1015
	int flags;
1016
1017
	spin_lock_irqsave(&hardware_lock, flags);
1018
1019
#ifdef CONFIG_LIRC_ON_SA1100
1020
	Ser2UTCR3=0;
1021
1022
	Ser2UTCR0=sr.utcr0;
1023
	Ser2UTCR1=sr.utcr1;
1024
	Ser2UTCR2=sr.utcr2;
1025
	Ser2UTCR4=sr.utcr4;
1026
	Ser2UTCR3=sr.utcr3;
1027
1028
	Ser2HSCR0=sr.hscr0;
1029
#ifdef CONFIG_SA1100_BITSY
1030
	if (machine_is_bitsy()) {
1031
		clr_bitsy_egpio(EGPIO_BITSY_IR_ON);
1032
	}
1033
#endif
1034
#ifdef CONFIG_SA1100_COLLIE
1035
	sa1100_irda_set_power_collie(0);	/* power off */
1036
#endif
1037
#else
1038
	/* turn off interrupts */
1039
	outb(0, io + UART_IER);
1040
#endif
1041
	spin_unlock_irqrestore(&hardware_lock, flags);
1042
}
1043
1044
/* SECTION: Initialisation */
1045
1046
static int init_port(void)
1047
{
1048
	int retval;
1049
1050
#ifndef CONFIG_LIRC_ON_SA1100
1051
	/* get I/O port access and IRQ line */
1052
	retval = request_region(io, 8, LIRC_DRIVER_NAME);
1053
	if (!retval) {
1054
		printk(KERN_ERR LIRC_DRIVER_NAME
1055
			": i/o port 0x%.4x already in use.\n",
1056
			io);
1057
		return retval;
1058
	}
1059
	printk(KERN_INFO LIRC_DRIVER_NAME
1060
		": I/O port 0x%.4x, IRQ %d.\n",
1061
		io, irq);
1062
#endif
1063
	retval = request_irq(irq, sir_interrupt, SA_INTERRUPT,
1064
			     LIRC_DRIVER_NAME, NULL);
1065
	if (retval < 0) {
1066
		printk(KERN_ERR LIRC_DRIVER_NAME
1067
			": IRQ %d already in use.\n",
1068
			irq);
1069
		return retval;
1070
	}
1071
1072
	init_timer(&timerlist);
1073
	timerlist.function = sir_timeout;
1074
	timerlist.data = 0xabadcafe;
1075
1076
	return 0;
1077
}
1078
1079
static void drop_port(void)
1080
{
1081
	disable_irq(irq);
1082
	free_irq(irq, NULL);
1083
	del_timer_sync(&timerlist);
1084
#ifndef CONFIG_LIRC_ON_SA1100
1085
	release_region(io, 8);
1086
#endif
1087
}
1088
1089
#ifdef CONFIG_LIRC_SIR_ACTISYS_ACT200L
1090
/******************************************************/
1091
/* Crystal/Cirrus CS8130 IR transceiver, used in Actisys Act200L dongle */
1092
/* some code borrowed from Linux IRDA driver */
1093
1094
/* Regsiter 0: Control register #1 */
1095
#define ACT200L_REG0    0x00
1096
#define ACT200L_TXEN    0x01 /* Enable transmitter */
1097
#define ACT200L_RXEN    0x02 /* Enable receiver */
1098
#define ACT200L_ECHO    0x08 /* Echo control chars */
1099
1100
/* Register 1: Control register #2 */
1101
#define ACT200L_REG1    0x10
1102
#define ACT200L_LODB    0x01 /* Load new baud rate count value */
1103
#define ACT200L_WIDE    0x04 /* Expand the maximum allowable pulse */
1104
1105
/* Register 3: Transmit mode register #2 */
1106
#define ACT200L_REG3    0x30
1107
#define ACT200L_B0      0x01 /* DataBits, 0=6, 1=7, 2=8, 3=9(8P)  */
1108
#define ACT200L_B1      0x02 /* DataBits, 0=6, 1=7, 2=8, 3=9(8P)  */
1109
#define ACT200L_CHSY    0x04 /* StartBit Synced 0=bittime, 1=startbit */
1110
1111
/* Register 4: Output Power register */
1112
#define ACT200L_REG4    0x40
1113
#define ACT200L_OP0     0x01 /* Enable LED1C output */
1114
#define ACT200L_OP1     0x02 /* Enable LED2C output */
1115
#define ACT200L_BLKR    0x04
1116
1117
/* Register 5: Receive Mode register */
1118
#define ACT200L_REG5    0x50
1119
#define ACT200L_RWIDL   0x01 /* fixed 1.6us pulse mode */
1120
    /*.. other various IRDA bit modes, and TV remote modes..*/
1121
1122
/* Register 6: Receive Sensitivity register #1 */
1123
#define ACT200L_REG6    0x60
1124
#define ACT200L_RS0     0x01 /* receive threshold bit 0 */
1125
#define ACT200L_RS1     0x02 /* receive threshold bit 1 */
1126
1127
/* Register 7: Receive Sensitivity register #2 */
1128
#define ACT200L_REG7    0x70
1129
#define ACT200L_ENPOS   0x04 /* Ignore the falling edge */
1130
1131
/* Register 8,9: Baud Rate Dvider register #1,#2 */
1132
#define ACT200L_REG8    0x80
1133
#define ACT200L_REG9    0x90
1134
1135
#define ACT200L_2400    0x5f
1136
#define ACT200L_9600    0x17
1137
#define ACT200L_19200   0x0b
1138
#define ACT200L_38400   0x05
1139
#define ACT200L_57600   0x03
1140
#define ACT200L_115200  0x01
1141
1142
/* Register 13: Control register #3 */
1143
#define ACT200L_REG13   0xd0
1144
#define ACT200L_SHDW    0x01 /* Enable access to shadow registers */
1145
1146
/* Register 15: Status register */
1147
#define ACT200L_REG15   0xf0
1148
1149
/* Register 21: Control register #4 */
1150
#define ACT200L_REG21   0x50
1151
#define ACT200L_EXCK    0x02 /* Disable clock output driver */
1152
#define ACT200L_OSCL    0x04 /* oscillator in low power, medium accuracy mode */
1153
1154
static void init_act200(void)
1155
{
1156
  int i;
1157
	__u8 control[] = {
1158
		ACT200L_REG15,
1159
		ACT200L_REG13 | ACT200L_SHDW,
1160
		ACT200L_REG21 | ACT200L_EXCK | ACT200L_OSCL,
1161
		ACT200L_REG13,
1162
		ACT200L_REG7  | ACT200L_ENPOS,
1163
		ACT200L_REG6  | ACT200L_RS0  | ACT200L_RS1,
1164
		ACT200L_REG5  | ACT200L_RWIDL,
1165
		ACT200L_REG4  | ACT200L_OP0  | ACT200L_OP1 | ACT200L_BLKR,
1166
		ACT200L_REG3  | ACT200L_B0,
1167
		ACT200L_REG0  | ACT200L_TXEN | ACT200L_RXEN,
1168
		ACT200L_REG8 |  (ACT200L_115200       & 0x0f),
1169
		ACT200L_REG9 | ((ACT200L_115200 >> 4) & 0x0f),
1170
		ACT200L_REG1 | ACT200L_LODB | ACT200L_WIDE
1171
	};
1172
1173
	/* Set DLAB 1. */
1174
	soutp(UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN8);
1175
1176
	/* Set divisor to 12 => 9600 Baud */
1177
	soutp(UART_DLM,0);
1178
	soutp(UART_DLL,12);
1179
1180
	/* Set DLAB 0. */
1181
	soutp(UART_LCR, UART_LCR_WLEN8);
1182
	/* Set divisor to 12 => 9600 Baud */
1183
1184
	/* power supply */
1185
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
1186
	for (i=0; i<50; i++) {
1187
		safe_udelay(1000);
1188
	}
1189
1190
		/* Reset the dongle : set RTS low for 25 ms */
1191
	soutp(UART_MCR, UART_MCR_DTR|UART_MCR_OUT2);
1192
	for (i=0; i<25; i++) {
1193
		udelay(1000);
1194
	}
1195
1196
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
1197
	udelay(100);
1198
1199
	/* Clear DTR and set RTS to enter command mode */
1200
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_OUT2);
1201
	udelay(7);
1202
1203
/* send out the control register settings for 115K 7N1 SIR operation */
1204
	for (i=0; i<sizeof(control); i++) {
1205
		soutp(UART_TX, control[i]);
1206
		/* one byte takes ~1042 usec to transmit at 9600,8N1 */
1207
		udelay(1500);
1208
	}
1209
1210
	/* back to normal operation */
1211
	soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
1212
	udelay(50);
1213
1214
	udelay(1500);
1215
	soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
1216
1217
	/* Set DLAB 1. */
1218
	soutp(UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN7);
1219
1220
	/* Set divisor to 1 => 115200 Baud */
1221
	soutp(UART_DLM,0);
1222
	soutp(UART_DLL,1);
1223
1224
	/* Set DLAB 0. */
1225
	soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
1226
1227
	/* Set DLAB 0, 7 Bit */
1228
	soutp(UART_LCR, UART_LCR_WLEN7);
1229
1230
	/* enable interrupts */
1231
	soutp(UART_IER, sinp(UART_IER)|UART_IER_RDI);
1232
}
1233
#endif
1234
1235
int init_lirc_sir(void)
1236
{
1237
	int retval;
1238
1239
	init_waitqueue_head(&lirc_read_queue);
1240
	retval = init_port();
1241
	if (retval < 0)
1242
		return retval;
1243
	init_hardware();
1244
	enable_irq(irq);
1245
	printk(KERN_INFO LIRC_DRIVER_NAME
1246
		": Installed.\n");
1247
	return 0;
1248
}
1249
1250
#ifdef CONFIG_LIRC_SIR_TEKRAM
1251
MODULE_AUTHOR("Christoph Bartelmus");
1252
MODULE_DESCRIPTION("Infrared receiver driver for Tekram Irmate 210");
1253
#elif defined(CONFIG_LIRC_ON_SA1100)
1254
MODULE_AUTHOR("Christoph Bartelmus");
1255
MODULE_DESCRIPTION("LIRC driver for StrongARM SA1100 embedded microprocessor");
1256
#elif defined(CONFIG_LIRC_SIR_ACTISYS_ACT200L)
1257
MODULE_AUTHOR("Karl Bongers");
1258
MODULE_DESCRIPTION("LIRC driver for Actisys Act200L");
1259
#else
1260
MODULE_AUTHOR("Milan Pikula");
1261
MODULE_DESCRIPTION("Infrared receiver driver for SIR type serial ports");
1262
#endif
1263
1264
#ifdef CONFIG_LIRC_ON_SA1100
1265
MODULE_PARM(irq, "i");
1266
MODULE_PARM_DESC(irq, "Interrupt (16)");
1267
#else
1268
MODULE_PARM(io, "i");
1269
MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
1270
MODULE_PARM(irq, "i");
1271
MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
1272
MODULE_PARM(threshold, "i");
1273
MODULE_PARM_DESC(threshold, "space detection threshold (3)");
1274
#endif
1275
1276
MODULE_LICENSE("GPL");
1277
1278
static int __init lirc_sir_init(void)
1279
{
1280
	int retval;
1281
1282
	retval=init_chrdev();
1283
	if(retval < 0)
1284
		return retval;
1285
	retval = init_lirc_sir();
1286
	if (retval) {
1287
		drop_chrdev();
1288
		return retval;
1289
	}
1290
	return 0;
1291
}
1292
1293
static void __exit lirc_sir_exit(void)
1294
{
1295
	if(module_refcount(THIS_MODULE)) return;
1296
	drop_hardware();
1297
	drop_chrdev();
1298
	drop_port();
1299
	printk(KERN_INFO LIRC_DRIVER_NAME ": Uninstalled.\n");
1300
}
1301
1302
module_init(lirc_sir_init);
1303
module_exit(lirc_sir_exit);
(-)linux-2.6.2/include/linux/lirc.h (+103 lines)
Line 0 Link Here
1
/*      $Id: 405_lirc_infrared-2.6.2-02092004.patch,v 1.1 2004/02/24 22:27:37 brad_mssw 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 47404