]> git.pld-linux.org Git - packages/kernel.git/blob - lirc-2.6.5-20040404
- CSZ scheduler removed from kernel tree.
[packages/kernel.git] / lirc-2.6.5-20040404
1 diff -uprN -X dontdiff linux-2.6.5.orig/Documentation/lirc/lirc_it87 linux-2.6.5/Documentation/lirc/lirc_it87
2 --- linux-2.6.5.orig/Documentation/lirc/lirc_it87       1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.5/Documentation/lirc/lirc_it87    2004-04-04 14:05:29.398630000 +0200
4 @@ -0,0 +1,54 @@
5 +This is the README using the ITE IT8705 and IT8712 CIR port for LIRC.
6 +
7 +The IT8705 for example can be found on the ECS K7S5A.
8 +
9 +The driver supports receiving (MODE2) and sending (PULSE). It seems
10 +sending 'LIRC_CAN_SEND_PULSE' isn't optimal for this type of hardware.
11 +But because I don't know how to implement 'LIRC_CAN_SEND_CODE', I did
12 +it this way.
13 +
14 +Attention:
15 +Because of missing hardware, the following hasn't been tested:
16 +a) receiving with demodulator enabled,
17 +b) sending (debugging output looks good) and
18 +c) using IT8712
19 +
20 +Any help and/or additions etc. is welcome.
21 +
22 +lirc_it87 knows about the following module-parameters:
23 +MODULE_DESCRIPTION("LIRC driver for ITE IT8712/IT8705 CIR port");
24 +MODULE_PARM(io, "i");
25 +MODULE_PARM_DESC(io, "I/O base address (default: 0x310)");
26 +MODULE_PARM(irq, "i");
27 +MODULE_PARM_DESC(irq, "Interrupt (1,3-12) (default: 7)");
28 +MODULE_PARM(it87_enable_demodulator, "i");
29 +MODULE_PARM_DESC(it87_enable_demodulator, "Receiver demodulator
30 +       enable/disable (1/0), default: 0");
31 +
32 +
33 +Usage:
34 +
35 +a) io and irq:
36 +
37 +If the driver finds the IT8705/12-CIR port initialized, io and irq of
38 +the preinitialized hardware is used by the driver. If both values are
39 +read 0x0 from the hardware, the default or given value is used.
40 +Note: I experienced using irq=3. The driver initialized without any
41 +problems, but no irqs are recognized by the system. I had to switch
42 +back to default, irq 7.
43 +
44 +b) it87_enable_demodulator:
45 +
46 +The demodulator for the receiver can be switched off (default within
47 +the driver). If you need the demodulator simple enable it by the
48 +following way: it87_enable_demodulator=1.
49 +
50 +Hans-Günter Lütke Uphues
51 +
52 +
53 +TODO
54 +
55 +This is my todo-list for lirc_it87:
56 +
57 +1. enabling/using shared IRQ
58 +2. init/drop IRQ-usage in lirc_open/lirc_close
59 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/Kconfig linux-2.6.5/drivers/char/Kconfig
60 --- linux-2.6.5.orig/drivers/char/Kconfig       2004-04-04 13:59:17.669141472 +0200
61 +++ linux-2.6.5/drivers/char/Kconfig    2004-04-04 14:05:29.400629696 +0200
62 @@ -444,6 +444,9 @@ config A2232
63  
64  source "drivers/serial/Kconfig"
65  
66 +
67 +source "drivers/char/lirc/Kconfig"
68 +
69  config UNIX98_PTYS
70         bool "Unix98 PTY support" if EMBEDDED
71         default y
72 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/Kconfig linux-2.6.5/drivers/char/lirc/Kconfig
73 --- linux-2.6.5.orig/drivers/char/lirc/Kconfig  1970-01-01 01:00:00.000000000 +0100
74 +++ linux-2.6.5/drivers/char/lirc/Kconfig       2004-04-04 14:05:29.401629544 +0200
75 @@ -0,0 +1,208 @@
76 +# LIRC http://lirc.sf.net/
77 +# Kernel patch by Flameeyes <dgp85@users.sf.net>
78 +# Check for new patch at http://flameeyes.web.ctonet.it
79 +#
80 +# Thanks to Koos Vriezen <koos.vriezen@xs4all.nl> for the Homebrew support.
81 +# Thanks to Jeff Clark <jeff@tmtrading.com> for support when I wasn't able
82 +# to update it and for his patch (found at http://www.clarkmania.com/~jclark/
83 +# Thanks to Bernhard Rosenkraenzer <bero@arklinux.org> for SMP patch.
84 +# Thanks to Vince <fuzzy77@free.fr> for the temporary lirc_atiusb driver.
85 +# Thanks to Paul Miller <pmiller9@users.sourceforge.net> for the new working
86 +# lirc_atiusb driver.
87 +
88 +menu "Linux InfraRed Controller"
89 +
90 +config LIRC_SUPPORT
91 +       tristate "Linux InfraRed Controller"
92 +
93 +       config LIRC_MAX_DEV
94 +               int "Maximum LIRC devices"
95 +               default "2"
96 +               depends on LIRC_SUPPORT
97 +
98 +       config LIRC_I2C
99 +               tristate "I2C Driver"
100 +               depends on LIRC_SUPPORT && VIDEO_BT848 && I2C && I2C_ALGOBIT
101 +               help
102 +                 Say Y here if you need support for the following cards:
103 +
104 +                 Pixelview IR
105 +                 Hauppauage IR
106 +                 PV951 IR
107 +                 TV-Box IR
108 +                 KNC ONE IR
109 +
110 +                 If these dont make sense to you, then dont use the module.
111 +
112 +       config LIRC_GPIO
113 +               tristate "GPIO Driver"
114 +               depends on LIRC_SUPPORT && VIDEO_BT848
115 +
116 +       config LIRC_BT829
117 +               tristate "BT829 Driver"
118 +               depends on LIRC_SUPPORT
119 +
120 +       config LIRC_IT87
121 +               tristate "IT87 Driver"
122 +               depends on LIRC_SUPPORT
123 +
124 +       config LIRC_ATIUSB
125 +               tristate "ATI USB Driver"
126 +               depends on LIRC_SUPPORT && USB
127 +
128 +       config LIRC_MCEUSB
129 +               tristate "MCE USB Driver"
130 +               depends on LIRC_SUPPORT && USB
131 +
132 +       config LIRC_PARALLEL
133 +               tristate "Parallel Driver"
134 +               depends on LIRC_SUPPORT && !SMP && PARPORT
135 +
136 +               choice
137 +                       prompt "Parallel Port"
138 +                       depends on LIRC_PARALLEL
139 +                       config LIRC_PARALLEL_LPT1
140 +                               bool "LPT1 (0x378, 7)"
141 +                       config LIRC_PARALLEL_LPT2
142 +                               bool "LPT2 (0x278, 5)"
143 +                       config LIRC_PARALLEL_LPT3
144 +                               bool "COM3 (0x3bc, none)"
145 +                       config LIRC_PARALLEL_OTHER
146 +                               bool "Other (custom values)"
147 +               endchoice
148 +
149 +               config LIRC_PORT_PARALLEL
150 +                       hex "I/O Port"
151 +                               default "0x378" if LIRC_PARALLEL_LPT1
152 +                               default "0x278" if LIRC_PARALLEL_LPT2
153 +                               default "0x3bc" if LIRC_PARALLEL_LPT3
154 +                       depends on LIRC_PARALLEL
155 +
156 +               config LIRC_IRQ_PARALLEL
157 +                       hex "IRQ"
158 +                               default "7" if LIRC_PARALLEL_LPT1
159 +                               default "5" if LIRC_PARALLEL_LPT2
160 +                       depends on LIRC_PARALLEL
161 +
162 +               config LIRC_TIMER
163 +                       int "Timer"
164 +                       default "65535"
165 +                       depends on LIRC_PARALLEL
166 +
167 +       config LIRC_SERIAL
168 +               tristate "Serial Driver"
169 +               depends on LIRC_SUPPORT && SERIAL_8250
170 +
171 +               choice
172 +                       prompt "Serial Receiver Type"
173 +                       depends on LIRC_SERIAL
174 +
175 +                       config LIRC_HOMEBREW
176 +                               bool "Homebrew"
177 +
178 +                       config LIRC_SERIAL_ANIMAX
179 +                               bool "Animax"
180 +
181 +                       config LIRC_SERIAL_IRDEO
182 +                               bool "IRdeo"
183 +
184 +                       config LIRC_SERIAL_IRDEO_REMOTE
185 +                               bool "IRdeo Remote"
186 +
187 +               endchoice
188 +
189 +               config LIRC_SERIAL_TRANSMITTER
190 +                       bool "With transmitter diode"
191 +                       depends on LIRC_SERIAL && !LIRC_SERIAL_ANIMAX
192 +
193 +               config LIRC_SERIAL_SOFTCARRIER
194 +                       bool "With software carrier"
195 +                       depends on LIRC_SERIAL_TRANSMITTER
196 +
197 +               config LIRC_SERIAL_IGOR
198 +                       bool "Igor Ceska's variation"
199 +                       depends on LIRC_SERIAL
200 +
201 +               choice
202 +                       prompt "Serial Port"
203 +                       depends on LIRC_SERIAL
204 +                       config LIRC_SERIAL_COM1
205 +                               bool "COM1 (0x3f8, 4)"
206 +                       config LIRC_SERIAL_COM2
207 +                               bool "COM2 (0x2f8, 3)"
208 +                       config LIRC_SERIAL_COM3
209 +                               bool "COM3 (0x3e8, 4)"
210 +                       config LIRC_SERIAL_COM4
211 +                               bool "COM4 (0x2e8, 3)"
212 +                       config LIRC_SERIAL_OTHER
213 +                               bool "Other (custom values)"
214 +               endchoice
215 +
216 +               config LIRC_PORT_SERIAL
217 +                       hex "I/O Port"
218 +                               default "0x3f8" if LIRC_SERIAL_COM1
219 +                               default "0x2f8" if LIRC_SERIAL_COM2
220 +                               default "0x3e8" if LIRC_SERIAL_COM3
221 +                               default "0x2e8" if LIRC_SERIAL_COM4
222 +                       depends on LIRC_SERIAL
223 +
224 +               config LIRC_IRQ_SERIAL
225 +                       hex "IRQ"
226 +                               default "4" if LIRC_SERIAL_COM1 || LIRC_SERIAL_COM3
227 +                               default "3" if LIRC_SERIAL_COM2 || LIRC_SERIAL_COM4
228 +                       depends on LIRC_SERIAL
229 +
230 +       config LIRC_SIR
231 +               tristate "SIR Driver"
232 +               depends on LIRC_SUPPORT
233 +
234 +               config LIRC_ON_SA1100
235 +                       bool "LIRC driver for StrongARM SA1100 embedded microprocessor"
236 +                       depends on LIRC_SIR
237 +
238 +               choice
239 +                       prompt "SIR Type"
240 +                       depends on LIRC_SIR && !LIRC_ON_SA1100
241 +
242 +                       config LIRC_SIR_IRDA
243 +                               bool "SIR IrDA (built-in IR ports)"
244 +
245 +                       config LIRC_SIR_TEKRAM
246 +                               bool "Tekram Irmate 210 (16x50 UART compatible serial port)"
247 +
248 +                       config LIRC_SIR_ACTISYS_ACT200L
249 +                               bool "Actisys Act200L SIR driver support"
250 +
251 +               endchoice
252 +
253 +               choice
254 +                       prompt "Serial Port"
255 +                       depends on LIRC_SIR
256 +                       config LIRC_SIR_COM1
257 +                               bool "COM1 (0x3f8, 4)"
258 +                       config LIRC_SIR_COM2
259 +                               bool "COM2 (0x2f8, 3)"
260 +                       config LIRC_SIR_COM3
261 +                               bool "COM3 (0x3e8, 4)"
262 +                       config LIRC_SIR_COM4
263 +                               bool "COM4 (0x2e8, 3)"
264 +                       config LIRC_SIR_OTHER
265 +                               bool "Other (custom values)"
266 +               endchoice
267 +
268 +               config LIRC_PORT_SIR
269 +                       hex "I/O Port"
270 +                               default "0x3f8" if LIRC_SIR_COM1
271 +                               default "0x2f8" if LIRC_SIR_COM2
272 +                               default "0x3e8" if LIRC_SIR_COM3
273 +                               default "0x2e8" if LIRC_SIR_COM4
274 +                       depends on LIRC_SIR
275 +
276 +               config LIRC_IRQ_SIR
277 +                       hex "IRQ"
278 +                               default "4" if LIRC_SIR_COM1 || LIRC_SIR_COM3
279 +                               default "3" if LIRC_SIR_COM2 || LIRC_SIR_COM4
280 +                       depends on LIRC_SIR
281 +
282 +endmenu
283 +
284 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_atiusb.c linux-2.6.5/drivers/char/lirc/lirc_atiusb.c
285 --- linux-2.6.5.orig/drivers/char/lirc/lirc_atiusb.c    1970-01-01 01:00:00.000000000 +0100
286 +++ linux-2.6.5/drivers/char/lirc/lirc_atiusb.c 2004-04-04 14:05:29.403629240 +0200
287 @@ -0,0 +1,629 @@
288 +/* lirc_atiusb - USB remote support for LIRC
289 + * (currently only supports X10 USB remotes)
290 + * Version 0.3  [beta status]
291 + *
292 + * Copyright (C) 2003-2004 Paul Miller <pmiller9@users.sourceforge.net>
293 + *
294 + * This driver was derived from:
295 + *   Vladimir Dergachev <volodya@minspring.com>'s 2002
296 + *      "USB ATI Remote support" (input device)
297 + *   Adrian Dewhurst <sailor-lk@sailorfrag.net>'s 2002
298 + *      "USB StreamZap remote driver" (LIRC)
299 + *   Artur Lipowski <alipowski@kki.net.pl>'s 2002
300 + *      "lirc_dev" and "lirc_gpio" LIRC modules
301 + *
302 + * $Id$
303 + */
304 +
305 +/*
306 + * This program is free software; you can redistribute it and/or modify
307 + * it under the terms of the GNU General Public License as published by
308 + * the Free Software Foundation; either version 2 of the License, or
309 + * (at your option) any later version.
310 + *
311 + * This program is distributed in the hope that it will be useful,
312 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
313 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
314 + * GNU General Public License for more details.
315 + *
316 + * You should have received a copy of the GNU General Public License
317 + * along with this program; if not, write to the Free Software
318 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
319 + *
320 + */
321 +
322 +#include <linux/version.h>
323 +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 4)
324 +#error "*******************************************************"
325 +#error "Sorry, this driver needs kernel version 2.2.4 or higher"
326 +#error "*******************************************************"
327 +#endif
328 +
329 +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
330 +#define KERNEL26               1
331 +#else
332 +#define KERNEL26               0
333 +#endif
334 +
335 +#include <linux/config.h>
336 +
337 +#include <linux/kernel.h>
338 +#include <linux/errno.h>
339 +#include <linux/init.h>
340 +#include <linux/slab.h>
341 +#include <linux/module.h>
342 +#include <linux/kmod.h>
343 +#include <linux/smp_lock.h>
344 +#include <linux/completion.h>
345 +#include <asm/uaccess.h>
346 +#include <linux/usb.h>
347 +#include <linux/poll.h>
348 +#include <linux/wait.h>
349 +
350 +#if KERNEL26
351 +#include <linux/lirc.h>
352 +#include "lirc_dev.h"
353 +#else
354 +#include "drivers/lirc.h"
355 +#include "drivers/lirc_dev/lirc_dev.h"
356 +#endif
357 +
358 +#define DRIVER_VERSION         "0.3"
359 +#define DRIVER_AUTHOR          "Paul Miller <pmiller9@users.sourceforge.net>"
360 +#define DRIVER_DESC            "USB remote driver for LIRC"
361 +#define DRIVER_NAME            "lirc_atiusb"
362 +
363 +#define CODE_LENGTH            5
364 +#define CODE_MIN_LENGTH                4
365 +#define USB_BUFLEN             (CODE_LENGTH*4)
366 +
367 +#ifdef CONFIG_USB_DEBUG
368 +       static int debug = 1;
369 +#else
370 +       static int debug = 0;
371 +#endif
372 +#define dprintk                        if (debug) printk
373 +
374 +/* get hi and low bytes of a 16-bits int */
375 +#define HI(a)                  ((unsigned char)((a) >> 8))
376 +#define LO(a)                  ((unsigned char)((a) & 0xff))
377 +
378 +/* lock irctl structure */
379 +#define IRLOCK                 down_interruptible(&ir->lock)
380 +#define IRUNLOCK               up(&ir->lock)
381 +
382 +/* general constants */
383 +#define SUCCESS                        0
384 +#define SEND_FLAG_IN_PROGRESS  1
385 +#define SEND_FLAG_COMPLETE     2
386 +
387 +
388 +/* data structure for each usb remote */
389 +struct irctl {
390 +
391 +       /* usb */
392 +       struct usb_device *usbdev;
393 +       struct urb *urb_in;
394 +       struct urb *urb_out;
395 +       int devnum;
396 +
397 +       /* buffers and dma */
398 +       unsigned char *buf_in;
399 +       unsigned char *buf_out;
400 +       unsigned int len_in;
401 +#if KERNEL26
402 +       dma_addr_t dma_in;
403 +       dma_addr_t dma_out;
404 +#endif
405 +
406 +       /* lirc */
407 +       struct lirc_plugin *p;
408 +       int connected;
409 +
410 +       /* handle sending (init strings) */
411 +       int send_flags;
412 +       wait_queue_head_t wait_out;
413 +
414 +       struct semaphore lock;
415 +};
416 +
417 +/* init strings */
418 +static char init1[] = {0x01, 0x00, 0x20, 0x14};
419 +static char init2[] = {0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20};
420 +
421 +/* send packet - used to initialize remote */
422 +static void send_packet(struct irctl *ir, u16 cmd, unsigned char *data)
423 +{
424 +       DECLARE_WAITQUEUE(wait, current);
425 +       int timeout = HZ; /* 1 second */
426 +       unsigned char buf[USB_BUFLEN];
427 +
428 +       dprintk(DRIVER_NAME "[%d]: send called (%#x)\n", ir->devnum, cmd);
429 +
430 +       IRLOCK;
431 +       ir->urb_out->transfer_buffer_length = LO(cmd) + 1;
432 +       ir->urb_out->dev = ir->usbdev;
433 +       ir->send_flags = SEND_FLAG_IN_PROGRESS;
434 +
435 +       memcpy(buf+1, data, LO(cmd));
436 +       buf[0] = HI(cmd);
437 +       memcpy(ir->buf_out, buf, LO(cmd)+1);
438 +
439 +       set_current_state(TASK_INTERRUPTIBLE);
440 +       add_wait_queue(&ir->wait_out, &wait);
441 +
442 +#if KERNEL26
443 +       if (usb_submit_urb(ir->urb_out, SLAB_ATOMIC)) {
444 +#else
445 +       if (usb_submit_urb(ir->urb_out)) {
446 +#endif
447 +               set_current_state(TASK_RUNNING);
448 +               remove_wait_queue(&ir->wait_out, &wait);
449 +               IRUNLOCK;
450 +               return;
451 +       }
452 +       IRUNLOCK;
453 +
454 +       while (timeout && (ir->urb_out->status == -EINPROGRESS)
455 +               && !(ir->send_flags & SEND_FLAG_COMPLETE)) {
456 +               timeout = schedule_timeout(timeout);
457 +               rmb();
458 +       }
459 +
460 +       dprintk(DRIVER_NAME "[%d]: send complete (%#x)\n", ir->devnum, cmd);
461 +
462 +       set_current_state(TASK_RUNNING);
463 +       remove_wait_queue(&ir->wait_out, &wait);
464 +       usb_unlink_urb(ir->urb_out);
465 +}
466 +
467 +static int unregister_from_lirc(struct irctl *ir)
468 +{
469 +       struct lirc_plugin *p = ir->p;
470 +       int devnum;
471 +       int rtn;
472 +
473 +       devnum = ir->devnum;
474 +       dprintk(DRIVER_NAME "[%d]: unregister from lirc called\n", devnum);
475 +
476 +       if ((rtn = lirc_unregister_plugin(p->minor)) > 0) {
477 +               printk(DRIVER_NAME "[%d]: error in lirc_unregister minor: %d\n"
478 +                       "Trying again...\n", devnum, p->minor);
479 +               if (rtn == -EBUSY) {
480 +                       printk(DRIVER_NAME
481 +                               "[%d]: device is opened, will unregister"
482 +                               " on close\n", devnum);
483 +                       return -EAGAIN;
484 +               }
485 +               set_current_state(TASK_INTERRUPTIBLE);
486 +               schedule_timeout(HZ);
487 +
488 +               if ((rtn = lirc_unregister_plugin(p->minor)) > 0) {
489 +                       printk(DRIVER_NAME "[%d]: lirc_unregister failed\n",
490 +                       devnum);
491 +               }
492 +       }
493 +
494 +       if (rtn != SUCCESS) {
495 +               printk(DRIVER_NAME "[%d]: didn't free resources\n", devnum);
496 +               return -EAGAIN;
497 +       }
498 +
499 +       printk(DRIVER_NAME "[%d]: usb remote disconnected\n", devnum);
500 +
501 +       lirc_buffer_free(p->rbuf);
502 +       kfree(p->rbuf);
503 +       kfree(p);
504 +       kfree(ir);
505 +       return SUCCESS;
506 +}
507 +
508 +static int set_use_inc(void *data)
509 +{
510 +       struct irctl *ir = data;
511 +
512 +       if (!ir) {
513 +               printk(DRIVER_NAME "[?]: set_use_inc called with no context\n");
514 +               return -EIO;
515 +       }
516 +       dprintk(DRIVER_NAME "[%d]: set use inc\n", ir->devnum);
517 +
518 +       if (!ir->connected) {
519 +               if (!ir->usbdev)
520 +                       return -ENOENT;
521 +               ir->urb_in->dev = ir->usbdev;
522 +#if KERNEL26
523 +               if (usb_submit_urb(ir->urb_in, SLAB_ATOMIC)) {
524 +#else
525 +               if (usb_submit_urb(ir->urb_in)) {
526 +#endif
527 +                       printk(DRIVER_NAME "[%d]: open result = -EIO error "
528 +                               "submitting urb\n", ir->devnum);
529 +                       return -EIO;
530 +               }
531 +               ir->connected = 1;
532 +       }
533 +
534 +       return SUCCESS;
535 +}
536 +
537 +static void set_use_dec(void *data)
538 +{
539 +       struct irctl *ir = data;
540 +
541 +       if (!ir) {
542 +               printk(DRIVER_NAME "[?]: set_use_dec called with no context\n");
543 +               return;
544 +       }
545 +       dprintk(DRIVER_NAME "[%d]: set use dec\n", ir->devnum);
546 +
547 +       if (ir->connected) {
548 +               IRLOCK;
549 +               usb_unlink_urb(ir->urb_in);
550 +               ir->connected = 0;
551 +               IRUNLOCK;
552 +       }
553 +}
554 +
555 +
556 +#if KERNEL26
557 +static void usb_remote_recv(struct urb *urb, struct pt_regs *regs)
558 +#else
559 +static void usb_remote_recv(struct urb *urb)
560 +#endif
561 +{
562 +       struct irctl *ir;
563 +       char buf[CODE_LENGTH];
564 +       int i, len;
565 +
566 +       if (!urb)
567 +               return;
568 +
569 +       if (!(ir = urb->context)) {
570 +               usb_unlink_urb(urb);
571 +               return;
572 +       }
573 +
574 +       dprintk(DRIVER_NAME "[%d]: data received (length %d)\n",
575 +               ir->devnum, urb->actual_length);
576 +
577 +       switch (urb->status) {
578 +
579 +       /* success */
580 +       case SUCCESS:
581 +               /* some remotes emit both 4 and 5 byte length codes. */
582 +               len = urb->actual_length;
583 +               if (len < CODE_MIN_LENGTH || len > CODE_LENGTH) return;
584 +
585 +               memcpy(buf,urb->transfer_buffer,len);
586 +               for (i = len; i < CODE_LENGTH; i++) buf[i] = 0;
587 +
588 +               lirc_buffer_write_1(ir->p->rbuf, buf);
589 +               wake_up(&ir->p->rbuf->wait_poll);
590 +               break;
591 +
592 +       /* unlink */
593 +       case -ECONNRESET:
594 +       case -ENOENT:
595 +       case -ESHUTDOWN:
596 +               usb_unlink_urb(urb);
597 +               return;
598 +       }
599 +
600 +       /* resubmit urb */
601 +#if KERNEL26
602 +       usb_submit_urb(urb, SLAB_ATOMIC);
603 +#else
604 +       usb_submit_urb(urb);
605 +#endif
606 +}
607 +
608 +#if KERNEL26
609 +static void usb_remote_send(struct urb *urb, struct pt_regs *regs)
610 +#else
611 +static void usb_remote_send(struct urb *urb)
612 +#endif
613 +{
614 +       struct irctl *ir;
615 +
616 +       if (!urb)
617 +               return;
618 +
619 +       if (!(ir = urb->context)) {
620 +               usb_unlink_urb(urb);
621 +               return;
622 +       }
623 +
624 +       dprintk(DRIVER_NAME "[%d]: usb out called\n", ir->devnum);
625 +
626 +       if (urb->status)
627 +               return;
628 +
629 +       ir->send_flags |= SEND_FLAG_COMPLETE;
630 +       wmb();
631 +       if (waitqueue_active(&ir->wait_out))
632 +               wake_up(&ir->wait_out);
633 +}
634 +
635 +#if KERNEL26
636 +static int usb_remote_probe(struct usb_interface *intf,
637 +                               const struct usb_device_id *id)
638 +{
639 +       struct usb_device *dev = NULL;
640 +       struct usb_host_interface *idesc = NULL;
641 +#else
642 +static void *usb_remote_probe(struct usb_device *dev, unsigned int ifnum,
643 +                               const struct usb_device_id *id)
644 +{
645 +       struct usb_interface *intf;
646 +       struct usb_interface_descriptor *idesc;
647 +#endif
648 +       struct usb_endpoint_descriptor *ep_in, *ep_out;
649 +       struct irctl *ir = NULL;
650 +       struct lirc_plugin *plugin = NULL;
651 +       struct lirc_buffer *rbuf = NULL;
652 +       int devnum, pipe, maxp, len, buf_len, bytes_in_key;
653 +       int minor = 0;
654 +       char buf[63], name[128]="";
655 +       int mem_failure = 0;
656 +
657 +       dprintk(DRIVER_NAME ": usb probe called\n");
658 +
659 +#if KERNEL26
660 +       dev = interface_to_usbdev(intf);
661 +       idesc = &intf->altsetting[intf->act_altsetting];
662 +       if (idesc->desc.bNumEndpoints != 2)
663 +               return -ENODEV;
664 +       ep_in = &idesc->endpoint[0].desc;
665 +       ep_out = &idesc->endpoint[1].desc;
666 +       if (((ep_in->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN)
667 +               || (ep_in->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
668 +               != USB_ENDPOINT_XFER_INT)
669 +               return -ENODEV;
670 +#else
671 +       intf = &dev->actconfig->interface[ifnum];
672 +       idesc = &intf->altsetting[intf->act_altsetting];
673 +       if (idesc->bNumEndpoints != 2)
674 +               return NULL;
675 +       ep_in = idesc->endpoint + 0;
676 +       ep_out = idesc->endpoint + 1;
677 +       if (((ep_in->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN)
678 +               || (ep_in->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
679 +               != USB_ENDPOINT_XFER_INT)
680 +               return NULL;
681 +#endif
682 +       devnum = dev->devnum;
683 +       pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress);
684 +       maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
685 +
686 +       bytes_in_key = CODE_LENGTH;
687 +       len = (maxp > USB_BUFLEN) ? USB_BUFLEN : maxp;
688 +       buf_len = len - (len % bytes_in_key);
689 +
690 +       dprintk(DRIVER_NAME "[%d]: bytes_in_key=%d len=%d maxp=%d buf_len=%d\n",
691 +               devnum, bytes_in_key, len, maxp, buf_len);
692 +
693 +
694 +       /* allocate kernel memory */
695 +       mem_failure = 0;
696 +       if (!(ir = kmalloc(sizeof(struct irctl), GFP_KERNEL))) {
697 +               mem_failure = 1;
698 +       } else {
699 +               memset(ir, 0, sizeof(struct irctl));
700 +
701 +               if (!(plugin = kmalloc(sizeof(struct lirc_plugin), GFP_KERNEL))) {
702 +                       mem_failure = 2;
703 +               } else if (!(rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL))) {
704 +                       mem_failure = 3;
705 +               } else if (lirc_buffer_init(rbuf, bytes_in_key, USB_BUFLEN/bytes_in_key)) {
706 +                       mem_failure = 4;
707 +#if KERNEL26
708 +               } else if (!(ir->buf_in = usb_buffer_alloc(dev, buf_len, SLAB_ATOMIC, &ir->dma_in))) {
709 +                       mem_failure = 5;
710 +               } else if (!(ir->buf_out = usb_buffer_alloc(dev, USB_BUFLEN, SLAB_ATOMIC, &ir->dma_out))) {
711 +                       mem_failure = 6;
712 +               } else if (!(ir->urb_in = usb_alloc_urb(0, GFP_KERNEL))) {
713 +                       mem_failure = 7;
714 +               } else if (!(ir->urb_out = usb_alloc_urb(0, GFP_KERNEL))) {
715 +                       mem_failure = 8;
716 +#else
717 +               } else if (!(ir->buf_in = kmalloc(buf_len, GFP_KERNEL))) {
718 +                       mem_failure = 5;
719 +               } else if (!(ir->buf_out = kmalloc(USB_BUFLEN, GFP_KERNEL))) {
720 +                       mem_failure = 6;
721 +               } else if (!(ir->urb_in = usb_alloc_urb(0))) {
722 +                       mem_failure = 7;
723 +               } else if (!(ir->urb_out = usb_alloc_urb(0))) {
724 +                       mem_failure = 8;
725 +#endif
726 +               } else {
727 +
728 +                       memset(plugin, 0, sizeof(struct lirc_plugin));
729 +
730 +                       strcpy(plugin->name, DRIVER_NAME " ");
731 +                       plugin->minor = -1;
732 +                       plugin->code_length = bytes_in_key*8;
733 +                       plugin->features = LIRC_CAN_REC_LIRCCODE;
734 +                       plugin->data = ir;
735 +                       plugin->rbuf = rbuf;
736 +                       plugin->set_use_inc = &set_use_inc;
737 +                       plugin->set_use_dec = &set_use_dec;
738 +
739 +                       init_MUTEX(&ir->lock);
740 +                       init_waitqueue_head(&ir->wait_out);
741 +
742 +                       if ((minor = lirc_register_plugin(plugin)) < 0) {
743 +                               mem_failure = 9;
744 +                       }
745 +               }
746 +       }
747 +
748 +       /* free allocated memory incase of failure */
749 +       switch (mem_failure) {
750 +       case 9:
751 +               lirc_buffer_free(rbuf);
752 +       case 8:
753 +               usb_free_urb(ir->urb_out);
754 +       case 7:
755 +               usb_free_urb(ir->urb_in);
756 +#if KERNEL26
757 +       case 6:
758 +               usb_buffer_free(dev, USB_BUFLEN, ir->buf_out, ir->dma_out);
759 +       case 5:
760 +               usb_buffer_free(dev, buf_len, ir->buf_in, ir->dma_in);
761 +#else
762 +       case 6:
763 +               kfree(ir->buf_out);
764 +       case 5:
765 +               kfree(ir->buf_in);
766 +#endif
767 +       case 4:
768 +               kfree(rbuf);
769 +       case 3:
770 +               kfree(plugin);
771 +       case 2:
772 +               kfree(ir);
773 +       case 1:
774 +               printk(DRIVER_NAME "[%d]: out of memory (code=%d)\n",
775 +                       devnum, mem_failure);
776 +#if KERNEL26
777 +               return -ENOMEM;
778 +#else
779 +               return NULL;
780 +#endif
781 +       }
782 +
783 +       plugin->minor = minor;
784 +       ir->p = plugin;
785 +       ir->devnum = devnum;
786 +       ir->usbdev = dev;
787 +       ir->len_in = buf_len;
788 +       ir->connected = 0;
789 +
790 +       usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in,
791 +               buf_len, usb_remote_recv, ir, ep_in->bInterval);
792 +       usb_fill_int_urb(ir->urb_out, dev,
793 +               usb_sndintpipe(dev, ep_out->bEndpointAddress), ir->buf_out,
794 +               USB_BUFLEN, usb_remote_send, ir, ep_out->bInterval);
795 +
796 +       if (dev->descriptor.iManufacturer
797 +               && usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0)
798 +               strncpy(name, buf, 128);
799 +       if (dev->descriptor.iProduct
800 +               && usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0)
801 +               snprintf(name, 128, "%s %s", name, buf);
802 +       printk(DRIVER_NAME "[%d]: %s on usb%d:%d\n", devnum, name,
803 +              dev->bus->busnum, devnum);
804 +
805 +       send_packet(ir, 0x8004, init1);
806 +       send_packet(ir, 0x8007, init2);
807 +
808 +#if KERNEL26
809 +       usb_set_intfdata(intf, ir);
810 +       return SUCCESS;
811 +#else
812 +       return ir;
813 +#endif
814 +}
815 +
816 +
817 +#if KERNEL26
818 +static void usb_remote_disconnect(struct usb_interface *intf)
819 +{
820 +       struct usb_device *dev = interface_to_usbdev(intf);
821 +       struct irctl *ir = usb_get_intfdata(intf);
822 +       usb_set_intfdata(intf, NULL);
823 +#else
824 +static void usb_remote_disconnect(struct usb_device *dev, void *ptr)
825 +{
826 +       struct irctl *ir = ptr;
827 +#endif
828 +
829 +       if (!ir || !ir->p)
830 +               return;
831 +
832 +       ir->usbdev = NULL;
833 +       wake_up_all(&ir->wait_out);
834 +
835 +       IRLOCK;
836 +       usb_unlink_urb(ir->urb_in);
837 +       usb_unlink_urb(ir->urb_out);
838 +       usb_free_urb(ir->urb_in);
839 +       usb_free_urb(ir->urb_out);
840 +#if KERNEL26
841 +       usb_buffer_free(dev, ir->len_in, ir->buf_in, ir->dma_in);
842 +       usb_buffer_free(dev, USB_BUFLEN, ir->buf_out, ir->dma_out);
843 +#else
844 +       kfree(ir->buf_in);
845 +       kfree(ir->buf_out);
846 +#endif
847 +       IRUNLOCK;
848 +
849 +       unregister_from_lirc(ir);
850 +}
851 +
852 +static struct usb_device_id usb_remote_id_table [] = {
853 +       { USB_DEVICE(0x0bc7, 0x0002) },         /* X10 USB Firecracker Interface */
854 +       { USB_DEVICE(0x0bc7, 0x0003) },         /* X10 VGA Video Sender */
855 +       { USB_DEVICE(0x0bc7, 0x0004) },         /* ATI Wireless Remote Receiver */
856 +       { USB_DEVICE(0x0bc7, 0x0005) },         /* NVIDIA Wireless Remote Receiver */
857 +       { USB_DEVICE(0x0bc7, 0x0006) },         /* ATI Wireless Remote Receiver */
858 +       { USB_DEVICE(0x0bc7, 0x0007) },         /* X10 USB Wireless Transceiver */
859 +       { USB_DEVICE(0x0bc7, 0x0008) },         /* X10 USB Wireless Transceiver */
860 +       { USB_DEVICE(0x0bc7, 0x0009) },         /* X10 USB Wireless Transceiver */
861 +       { USB_DEVICE(0x0bc7, 0x000A) },         /* X10 USB Wireless Transceiver */
862 +       { USB_DEVICE(0x0bc7, 0x000B) },         /* X10 USB Transceiver */
863 +       { USB_DEVICE(0x0bc7, 0x000C) },         /* X10 USB Transceiver */
864 +       { USB_DEVICE(0x0bc7, 0x000D) },         /* X10 USB Transceiver */
865 +       { USB_DEVICE(0x0bc7, 0x000E) },         /* X10 USB Transceiver */
866 +       { USB_DEVICE(0x0bc7, 0x000F) },         /* X10 USB Transceiver */
867 +
868 +       { }                                     /* Terminating entry */
869 +};
870 +
871 +static struct usb_driver usb_remote_driver = {
872 +       .owner =        THIS_MODULE,
873 +       .name =         DRIVER_NAME,
874 +       .probe =        usb_remote_probe,
875 +       .disconnect =   usb_remote_disconnect,
876 +       .id_table =     usb_remote_id_table
877 +};
878 +
879 +static int __init usb_remote_init(void)
880 +{
881 +       int i;
882 +
883 +       printk("\n" DRIVER_NAME ": " DRIVER_DESC " v" DRIVER_VERSION "\n");
884 +       printk(DRIVER_NAME ": " DRIVER_AUTHOR "\n");
885 +       dprintk(DRIVER_NAME ": debug mode enabled\n");
886 +
887 +       request_module("lirc_dev");
888 +
889 +       if ((i = usb_register(&usb_remote_driver)) < 0) {
890 +               printk(DRIVER_NAME ": usb register failed, result = %d\n", i);
891 +               return -ENODEV;
892 +       }
893 +
894 +       return SUCCESS;
895 +}
896 +
897 +static void __exit usb_remote_exit(void)
898 +{
899 +       usb_deregister(&usb_remote_driver);
900 +}
901 +
902 +module_init(usb_remote_init);
903 +module_exit(usb_remote_exit);
904 +
905 +MODULE_AUTHOR (DRIVER_AUTHOR);
906 +MODULE_DESCRIPTION (DRIVER_DESC);
907 +MODULE_LICENSE ("GPL");
908 +MODULE_DEVICE_TABLE (usb, usb_remote_id_table);
909 +
910 +MODULE_PARM(debug, "i");
911 +MODULE_PARM_DESC(debug, "enable driver debug mode");
912 +
913 +#if !KERNEL26
914 +EXPORT_NO_SYMBOLS;
915 +#endif
916 +
917 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_bt829.c linux-2.6.5/drivers/char/lirc/lirc_bt829.c
918 --- linux-2.6.5.orig/drivers/char/lirc/lirc_bt829.c     1970-01-01 01:00:00.000000000 +0100
919 +++ linux-2.6.5/drivers/char/lirc/lirc_bt829.c  2004-04-04 14:05:29.405628936 +0200
920 @@ -0,0 +1,368 @@
921 +/*
922 + * Remote control driver for the TV-card based on bt829
923 + *
924 + *  by Leonid Froenchenko <lfroen@galileo.co.il>
925 + *
926 + *  This program is free software; you can redistribute it and/or modify
927 + *  it under the terms of the GNU General Public License as published by
928 + *  the Free Software Foundation; either version 2 of the License, or
929 + *  (at your option) any later version.
930 + *
931 + *  This program is distributed in the hope that it will be useful,
932 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
933 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
934 + *  GNU General Public License for more details.
935 + *
936 + *  You should have received a copy of the GNU General Public License
937 + *  along with this program; if not, write to the Free Software
938 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
939 +*/
940 +
941 +#include <linux/version.h>
942 +#include <linux/config.h>
943 +#include <linux/kernel.h>
944 +#include <linux/module.h>
945 +#include <linux/threads.h>
946 +#include <linux/sched.h>
947 +#include <linux/ioport.h>
948 +#include <linux/pci.h>
949 +#include <linux/delay.h>
950 +#include <linux/init.h>
951 +
952 +#include "lirc_dev.h"
953 +
954 +int poll_main(void);
955 +int atir_init_start(void);
956 +
957 +void write_index(unsigned char index,unsigned int value);
958 +unsigned int read_index(unsigned char index);
959 +
960 +void do_i2c_start(void);
961 +void do_i2c_stop(void);
962 +
963 +void seems_wr_byte(unsigned char al);
964 +unsigned char seems_rd_byte(void);
965 +
966 +unsigned int read_index(unsigned char al);
967 +void write_index(unsigned char ah,unsigned int edx);
968 +
969 +void cycle_delay(int cycle);
970 +
971 +void do_set_bits(unsigned char bl);
972 +unsigned char do_get_bits(void);
973 +
974 +#define DATA_PCI_OFF 0x7FFC00
975 +#define WAIT_CYCLE   20
976 +
977 +
978 +int atir_minor;
979 +unsigned long pci_addr_phys, pci_addr_lin;
980 +
981 +struct lirc_plugin atir_plugin;
982 +
983 +int do_pci_probe(void)
984 +{
985 +       struct pci_dev *my_dev;
986 +       my_dev = (struct pci_dev *)pci_find_device(PCI_VENDOR_ID_ATI, PCI_DEVICE_ID_ATI_264VT,NULL);
987 +       if ( my_dev ) {
988 +               printk(KERN_ERR "ATIR: Using device: %s\n",my_dev->slot_name);
989 +               pci_addr_phys = 0;
990 +               if ( my_dev->resource[0].flags & IORESOURCE_MEM ) {
991 +                       pci_addr_phys = my_dev->resource[0].start;
992 +                       printk(KERN_INFO "ATIR memory at 0x%08X \n",(unsigned int)pci_addr_phys);
993 +               }
994 +               if ( pci_addr_phys == 0 ) {
995 +                       printk(KERN_ERR "ATIR no memory resource ?\n");
996 +                       return 0;
997 +               }
998 +       } else {
999 +               printk(KERN_ERR "ATIR: pci_prob failed\n");
1000 +               return 0;
1001 +       }
1002 +       return 1;
1003 +}
1004 +
1005 +
1006 +int atir_add_to_buf (void* data, struct lirc_buffer* buf)
1007 +{
1008 +       unsigned char key;
1009 +       int status;
1010 +       status = poll_main();
1011 +       key = (status >> 8) & 0xFF;
1012 +       if( status & 0xFF )
1013 +       {
1014 +       //    printk(KERN_INFO "ATIR reading key %02X\n",*key);
1015 +               lirc_buffer_write_1( buf, &key );
1016 +               return 0;
1017 +       }
1018 +       return -ENODATA;
1019 +}
1020 +
1021 +int atir_set_use_inc(void* data)
1022 +{
1023 +       printk(KERN_DEBUG "ATIR driver is opened\n");
1024 +       return 0;
1025 +}
1026 +
1027 +void atir_set_use_dec(void* data)
1028 +{
1029 +       printk(KERN_DEBUG "ATIR driver is closed\n");
1030 +}
1031 +
1032 +static int __init lirc_bt829_init(void)
1033 +{
1034 +       if ( !do_pci_probe() ) {
1035 +               return 1;
1036 +       }
1037 +
1038 +       if ( !atir_init_start() ) {
1039 +               return 1;
1040 +       }
1041 +
1042 +       strcpy(atir_plugin.name,"ATIR");
1043 +       atir_plugin.minor       = -1;
1044 +       atir_plugin.code_length = 8;
1045 +       atir_plugin.sample_rate = 10;
1046 +       atir_plugin.data        = 0;
1047 +       atir_plugin.add_to_buf  = atir_add_to_buf;
1048 +       atir_plugin.set_use_inc = atir_set_use_inc;
1049 +       atir_plugin.set_use_dec = atir_set_use_dec;
1050 +
1051 +       atir_minor = lirc_register_plugin(&atir_plugin);
1052 +       printk(KERN_DEBUG "ATIR driver is registered on minor %d\n",atir_minor);
1053 +
1054 +       return 0;
1055 +}
1056 +
1057 +
1058 +static void __exit lirc_bt829_exit(void)
1059 +{
1060 +       lirc_unregister_plugin(atir_minor);
1061 +}
1062 +
1063 +
1064 +int atir_init_start(void)
1065 +{
1066 +       pci_addr_lin = (unsigned long)ioremap(pci_addr_phys + DATA_PCI_OFF,0x400);
1067 +       if ( pci_addr_lin == 0 ) {
1068 +               printk(KERN_INFO "atir: pci mem must be mapped\n");
1069 +               return 0;
1070 +       }
1071 +       return 1;
1072 +}
1073 +
1074 +void cycle_delay(int cycle)
1075 +{
1076 +       udelay(WAIT_CYCLE*cycle);
1077 +}
1078 +
1079 +
1080 +int poll_main()
1081 +{
1082 +       unsigned char status_high, status_low;
1083 +
1084 +       do_i2c_start();
1085 +
1086 +       seems_wr_byte(0xAA);
1087 +       seems_wr_byte(0x01);
1088 +
1089 +       do_i2c_start();
1090 +
1091 +       seems_wr_byte(0xAB);
1092 +
1093 +       status_low = seems_rd_byte();
1094 +       status_high = seems_rd_byte();
1095 +
1096 +       do_i2c_stop();
1097 +
1098 +       return (status_high << 8) | status_low;
1099 +}
1100 +
1101 +void do_i2c_start(void)
1102 +{
1103 +       do_set_bits(3);
1104 +       cycle_delay(4);
1105 +
1106 +       do_set_bits(1);
1107 +       cycle_delay(7);
1108 +
1109 +       do_set_bits(0);
1110 +       cycle_delay(2);
1111 +}
1112 +
1113 +void do_i2c_stop(void)
1114 +{
1115 +       unsigned char bits;
1116 +       bits =  do_get_bits() & 0xFD;
1117 +       do_set_bits(bits);
1118 +       cycle_delay(1);
1119 +
1120 +       bits |= 1;
1121 +       do_set_bits(bits);
1122 +       cycle_delay(2);
1123 +
1124 +       bits |= 2;
1125 +       do_set_bits(bits);
1126 +       bits = 3;
1127 +       do_set_bits(bits);
1128 +       cycle_delay(2);
1129 +}
1130 +
1131 +
1132 +void seems_wr_byte(unsigned char value)
1133 +{
1134 +       int i;
1135 +       unsigned char reg;
1136 +
1137 +       reg = do_get_bits();
1138 +       for(i = 0;i < 8;i++) {
1139 +               if ( value & 0x80 ) {
1140 +                       reg |= 0x02;
1141 +               } else {
1142 +                       reg &= 0xFD;
1143 +               }
1144 +               do_set_bits(reg);
1145 +               cycle_delay(1);
1146 +
1147 +               reg |= 1;
1148 +               do_set_bits(reg);
1149 +               cycle_delay(1);
1150 +
1151 +               reg &= 0xFE;
1152 +               do_set_bits(reg);
1153 +               cycle_delay(1);
1154 +               value <<= 1;
1155 +       }
1156 +       cycle_delay(2);
1157 +
1158 +       reg |= 2;
1159 +       do_set_bits(reg);
1160 +
1161 +       reg |= 1;
1162 +       do_set_bits(reg);
1163 +
1164 +       cycle_delay(1);
1165 +       do_get_bits();
1166 +
1167 +       reg &= 0xFE;
1168 +       do_set_bits(reg);
1169 +       cycle_delay(3);
1170 +}
1171 +
1172 +unsigned char seems_rd_byte(void)
1173 +{
1174 +       int i;
1175 +       int rd_byte;
1176 +       unsigned char bits_2, bits_1;
1177 +
1178 +       bits_1 = do_get_bits() | 2;
1179 +       do_set_bits(bits_1);
1180 +
1181 +       rd_byte = 0;
1182 +       for(i = 0;i < 8;i++) {
1183 +               bits_1 &= 0xFE;
1184 +               do_set_bits(bits_1);
1185 +               cycle_delay(2);
1186 +
1187 +               bits_1 |= 1;
1188 +               do_set_bits(bits_1);
1189 +               cycle_delay(1);
1190 +
1191 +               if ( (bits_2 = do_get_bits()) & 2 ) {
1192 +                       rd_byte |= 1;
1193 +               }
1194 +               rd_byte <<= 1;
1195 +       }
1196 +
1197 +       bits_1 = 0;
1198 +       if ( bits_2 == 0 ) {
1199 +               bits_1 |= 2;
1200 +       }
1201 +       do_set_bits(bits_1);
1202 +       cycle_delay(2);
1203 +
1204 +       bits_1 |= 1;
1205 +       do_set_bits(bits_1);
1206 +       cycle_delay(3);
1207 +
1208 +       bits_1 &= 0xFE;
1209 +       do_set_bits(bits_1);
1210 +       cycle_delay(2);
1211 +
1212 +       rd_byte >>= 1;
1213 +       rd_byte &= 0xFF;
1214 +       return rd_byte;
1215 +}
1216 +
1217 +void do_set_bits(unsigned char new_bits)
1218 +{
1219 +       int reg_val;
1220 +       reg_val = read_index(0x34);
1221 +       if ( new_bits & 2 ) {
1222 +               reg_val &= 0xFFFFFFDF;
1223 +               reg_val |= 1;
1224 +       } else {
1225 +               reg_val &= 0xFFFFFFFE;
1226 +               reg_val |= 0x20;
1227 +       }
1228 +       reg_val |= 0x10;
1229 +       write_index(0x34,reg_val);
1230 +
1231 +       reg_val = read_index(0x31);
1232 +       if ( new_bits & 1 ) {
1233 +               reg_val |= 0x1000000;
1234 +       } else {
1235 +               reg_val &= 0xFEFFFFFF;
1236 +       }
1237 +       reg_val |= 0x8000000;
1238 +       write_index(0x31,reg_val);
1239 +}
1240 +
1241 +unsigned char do_get_bits(void)
1242 +{
1243 +       unsigned char bits;
1244 +       int reg_val;
1245 +
1246 +       reg_val = read_index(0x34);
1247 +       reg_val |= 0x10;
1248 +       reg_val &= 0xFFFFFFDF;
1249 +       write_index(0x34,reg_val);
1250 +
1251 +       reg_val = read_index(0x34);
1252 +       bits = 0;
1253 +       if ( reg_val & 8 ) {
1254 +               bits |= 2;
1255 +       } else {
1256 +               bits &= 0xFD;
1257 +       }
1258 +       reg_val = read_index(0x31);
1259 +       if ( reg_val & 0x1000000 ) {
1260 +               bits |= 1;
1261 +       } else {
1262 +               bits &= 0xFE;
1263 +       }
1264 +       return bits;
1265 +}
1266 +
1267 +unsigned int read_index(unsigned char index)
1268 +{
1269 +       unsigned int addr, value;
1270 +       //  addr = pci_addr_lin + DATA_PCI_OFF + ((index & 0xFF) << 2);
1271 +       addr = pci_addr_lin + ((index & 0xFF) << 2);
1272 +       value = readl(addr);
1273 +       return value;
1274 +}
1275 +
1276 +void write_index(unsigned char index,unsigned int reg_val)
1277 +{
1278 +       unsigned int addr;
1279 +       addr = pci_addr_lin + ((index & 0xFF) << 2);
1280 +       writel(reg_val,addr);
1281 +}
1282 +
1283 +MODULE_AUTHOR("Froenchenko Leonid");
1284 +MODULE_DESCRIPTION("IR remote driver for bt829 based TV cards");
1285 +MODULE_LICENSE("GPL");
1286 +
1287 +module_init(lirc_bt829_init);
1288 +module_exit(lirc_bt829_exit);
1289 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_dev.c linux-2.6.5/drivers/char/lirc/lirc_dev.c
1290 --- linux-2.6.5.orig/drivers/char/lirc/lirc_dev.c       1970-01-01 01:00:00.000000000 +0100
1291 +++ linux-2.6.5/drivers/char/lirc/lirc_dev.c    2004-04-04 14:05:29.407628632 +0200
1292 @@ -0,0 +1,727 @@
1293 +/*
1294 + * LIRC base driver
1295 + *
1296 + * (L) by Artur Lipowski <alipowski@interia.pl>
1297 + *
1298 + *  This program is free software; you can redistribute it and/or modify
1299 + *  it under the terms of the GNU General Public License as published by
1300 + *  the Free Software Foundation; either version 2 of the License, or
1301 + *  (at your option) any later version.
1302 + *
1303 + *  This program is distributed in the hope that it will be useful,
1304 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
1305 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1306 + *  GNU General Public License for more details.
1307 + *
1308 + *  You should have received a copy of the GNU General Public License
1309 + *  along with this program; if not, write to the Free Software
1310 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
1311 + *
1312 + * $Id$
1313 + *
1314 + */
1315 +
1316 +#include <linux/version.h>
1317 +
1318 +#include <linux/config.h>
1319 +#include <linux/module.h>
1320 +#include <linux/kernel.h>
1321 +#include <linux/sched.h>
1322 +#include <linux/ioctl.h>
1323 +#include <linux/fs.h>
1324 +#include <linux/poll.h>
1325 +#include <linux/smp_lock.h>
1326 +#include <asm/uaccess.h>
1327 +#include <asm/semaphore.h>
1328 +#include <asm/errno.h>
1329 +#define __KERNEL_SYSCALLS__
1330 +#include <linux/unistd.h>
1331 +#include <linux/init.h>
1332 +#include <linux/devfs_fs_kernel.h>
1333 +
1334 +#include <linux/lirc.h>
1335 +
1336 +#include "lirc_dev.h"
1337 +
1338 +static int debug = 0;
1339 +
1340 +MODULE_PARM(debug,"i");
1341 +
1342 +#define IRCTL_DEV_NAME    "BaseRemoteCtl"
1343 +#define SUCCESS           0
1344 +#define NOPLUG            -1
1345 +#define dprintk           if (debug) printk
1346 +
1347 +#define LOGHEAD           "lirc_dev (%s[%d]): "
1348 +
1349 +struct irctl
1350 +{
1351 +       struct lirc_plugin p;
1352 +       int open;
1353 +
1354 +       struct lirc_buffer *buf;
1355 +
1356 +       int t_pid;
1357 +
1358 +       struct semaphore *t_notify;
1359 +       struct semaphore *t_notify2;
1360 +       int shutdown;
1361 +       long jiffies_to_wait;
1362 +};
1363 +
1364 +DECLARE_MUTEX(plugin_lock);
1365 +
1366 +static struct irctl irctls[CONFIG_LIRC_MAX_DEV];
1367 +static struct file_operations fops;
1368 +
1369 +
1370 +/*  helper function
1371 + *  initializes the irctl structure
1372 + */
1373 +static inline void init_irctl(struct irctl *ir)
1374 +{
1375 +       memset(&ir->p, 0, sizeof(struct lirc_plugin));
1376 +       ir->p.minor = NOPLUG;
1377 +
1378 +       ir->t_pid = -1;
1379 +       ir->t_notify = NULL;
1380 +       ir->t_notify2 = NULL;
1381 +       ir->shutdown = 0;
1382 +
1383 +       ir->jiffies_to_wait = 0;
1384 +
1385 +       ir->open = 0;
1386 +}
1387 +
1388 +
1389 +/*  helper function
1390 + *  reads key codes from plugin and puts them into buffer
1391 + *  buffer free space is checked and locking performed
1392 + *  returns 0 on success
1393 + */
1394 +
1395 +inline static int add_to_buf(struct irctl *ir)
1396 +{
1397 +       if (lirc_buffer_full(ir->buf)) {
1398 +               dprintk(LOGHEAD "buffer overflow\n",
1399 +                       ir->p.name, ir->p.minor);
1400 +               return -EOVERFLOW;
1401 +       }
1402 +
1403 +    if(ir->p.add_to_buf) {
1404 +        int res = -ENODATA;
1405 +        int got_data = 0;
1406 +        
1407 +        /* Service the device as long as it is returning
1408 +         * data and we have space
1409 +         */
1410 +        while( !lirc_buffer_full(ir->buf) )
1411 +        {
1412 +            res = ir->p.add_to_buf( ir->p.data, ir->buf );
1413 +            if( res == SUCCESS )
1414 +                got_data++;
1415 +            else
1416 +                break;
1417 +        }
1418 +
1419 +        if( res == -ENODEV )
1420 +        {
1421 +            ir->shutdown = 1;
1422 +        }
1423 +        return (got_data ? SUCCESS : res);
1424 +    }
1425 +
1426 +       return SUCCESS;
1427 +}
1428 +
1429 +/* main function of the polling thread
1430 + */
1431 +static int lirc_thread(void *irctl)
1432 +{
1433 +       struct irctl *ir = irctl;
1434 +
1435 +       daemonize("lirc_dev");
1436 +
1437 +       if (ir->t_notify != NULL) {
1438 +               up(ir->t_notify);
1439 +       }
1440 +
1441 +       dprintk(LOGHEAD "poll thread started\n", ir->p.name, ir->p.minor);
1442 +
1443 +       do {
1444 +               if (ir->open) {
1445 +                       if (ir->jiffies_to_wait) {
1446 +                               current->state = TASK_INTERRUPTIBLE;
1447 +                               schedule_timeout(ir->jiffies_to_wait);
1448 +                       } else {
1449 +                               interruptible_sleep_on(ir->p.get_queue(ir->p.data));
1450 +                       }
1451 +                       if (ir->shutdown || !ir->open) {
1452 +                               break;
1453 +                       }
1454 +                       if (!add_to_buf(ir)) {
1455 +                               wake_up_interruptible(&ir->buf->wait_poll);
1456 +                       }
1457 +               } else {
1458 +                       /* if device not opened so we can sleep half a second */
1459 +                       current->state = TASK_INTERRUPTIBLE;
1460 +                       schedule_timeout(HZ/2);
1461 +               }
1462 +       } while (!ir->shutdown);
1463 +
1464 +       dprintk(LOGHEAD "poll thread ended\n", ir->p.name, ir->p.minor);
1465 +
1466 +       if (ir->t_notify2 != NULL) {
1467 +               down(ir->t_notify2);
1468 +       }
1469 +
1470 +       ir->t_pid = -1;
1471 +
1472 +       if (ir->t_notify != NULL) {
1473 +               up(ir->t_notify);
1474 +       }
1475 +
1476 +       return 0;
1477 +}
1478 +
1479 +/*
1480 + *
1481 + */
1482 +int lirc_register_plugin(struct lirc_plugin *p)
1483 +{
1484 +       struct irctl *ir;
1485 +       int minor;
1486 +       int bytes_in_key;
1487 +       DECLARE_MUTEX_LOCKED(tn);
1488 +
1489 +       if (!p) {
1490 +               printk("lirc_dev: lirc_register_plugin:"
1491 +                      "plugin pointer must be not NULL!\n");
1492 +               return -EBADRQC;
1493 +       }
1494 +
1495 +       if (CONFIG_LIRC_MAX_DEV <= p->minor) {
1496 +               printk("lirc_dev: lirc_register_plugin:"
1497 +                      "\" minor\" must be beetween 0 and %d (%d)!\n",
1498 +                      CONFIG_LIRC_MAX_DEV-1, p->minor);
1499 +               return -EBADRQC;
1500 +       }
1501 +
1502 +       if (1 > p->code_length || (BUFLEN*8) < p->code_length) {
1503 +               printk("lirc_dev: lirc_register_plugin:"
1504 +                      "code length in bits for minor (%d) "
1505 +                      "must be less than %d!\n",
1506 +                      p->minor, BUFLEN*8);
1507 +               return -EBADRQC;
1508 +       }
1509 +
1510 +       printk("lirc_dev: lirc_register_plugin:"
1511 +              "sample_rate: %d\n",p->sample_rate);
1512 +       if (p->sample_rate) {
1513 +               if (2 > p->sample_rate || 100 < p->sample_rate) {
1514 +                       printk("lirc_dev: lirc_register_plugin:"
1515 +                              "sample_rate must be beetween 2 and 100!\n");
1516 +                       return -EBADRQC;
1517 +               }
1518 +        if (!p->add_to_buf) {
1519 +            printk("lirc_dev: lirc_register_plugin:"
1520 +                   "add_to_buf cannot be NULL when sample_rate is set\n");
1521 +            return -EBADRQC;
1522 +        }
1523 +       } else if (!(p->fops && p->fops->read)
1524 +                       && !p->get_queue && !p->rbuf) {
1525 +               printk("lirc_dev: lirc_register_plugin:"
1526 +                      "fops->read, get_queue and rbuf cannot all be NULL!\n");
1527 +               return -EBADRQC;
1528 +       } else if (!p->get_queue && !p->rbuf) {
1529 +               if (!(p->fops && p->fops->read && p->fops->poll)
1530 +                               || (!p->fops->ioctl && !p->ioctl)) {
1531 +                       printk("lirc_dev: lirc_register_plugin:"
1532 +                              "neither read, poll nor ioctl can be NULL!\n");
1533 +                       return -EBADRQC;
1534 +               }
1535 +       }
1536 +
1537 +       down_interruptible(&plugin_lock);
1538 +
1539 +       minor = p->minor;
1540 +
1541 +       if (0 > minor) {
1542 +               /* find first free slot for plugin */
1543 +               for (minor=0; minor<CONFIG_LIRC_MAX_DEV; minor++)
1544 +                       if (irctls[minor].p.minor == NOPLUG)
1545 +                               break;
1546 +               if (CONFIG_LIRC_MAX_DEV == minor) {
1547 +                       printk("lirc_dev: lirc_register_plugin: "
1548 +                              "no free slots for plugins!\n");
1549 +                       up(&plugin_lock);
1550 +                       return -ENOMEM;
1551 +               }
1552 +       } else if (irctls[minor].p.minor != NOPLUG) {
1553 +               printk("lirc_dev: lirc_register_plugin:"
1554 +                      "minor (%d) just registerd!\n", minor);
1555 +               up(&plugin_lock);
1556 +               return -EBUSY;
1557 +       }
1558 +
1559 +       ir = &irctls[minor];
1560 +
1561 +       if (p->sample_rate) {
1562 +               ir->jiffies_to_wait = HZ / p->sample_rate;
1563 +       } else {
1564 +                /* it means - wait for externeal event in task queue */
1565 +               ir->jiffies_to_wait = 0;
1566 +       }
1567 +
1568 +       /* some safety check 8-) */
1569 +       p->name[sizeof(p->name)-1] = '\0';
1570 +
1571 +       bytes_in_key = p->code_length/8 + (p->code_length%8 ? 1 : 0);
1572 +
1573 +       if (p->rbuf) {
1574 +               ir->buf = p->rbuf;
1575 +       } else {
1576 +               ir->buf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
1577 +               lirc_buffer_init(ir->buf, bytes_in_key, BUFLEN/bytes_in_key);
1578 +       }
1579 +
1580 +       if (p->features==0)
1581 +               p->features = (p->code_length > 8) ?
1582 +                       LIRC_CAN_REC_LIRCCODE : LIRC_CAN_REC_CODE;
1583 +
1584 +       ir->p = *p;
1585 +       ir->p.minor = minor;
1586 +
1587 +#ifdef CONFIG_DEVFS_FS
1588 +       devfs_mk_cdev(MKDEV(IRCTL_DEV_MAJOR, ir->p.minor), S_IFCHR | S_IRUSR | S_IWUSR, "lirc/lirc%d", ir->p.minor);
1589 +#endif
1590 +
1591 +       if(p->sample_rate || p->get_queue) {
1592 +               /* try to fire up polling thread */
1593 +               ir->t_notify = &tn;
1594 +               ir->t_pid = kernel_thread(lirc_thread, (void*)ir, 0);
1595 +               if (ir->t_pid < 0) {
1596 +                       up(&plugin_lock);
1597 +                       printk("lirc_dev: lirc_register_plugin:"
1598 +                              "cannot run poll thread for minor = %d\n",
1599 +                              p->minor);
1600 +                       return -ECHILD;
1601 +               }
1602 +               down(&tn);
1603 +               ir->t_notify = NULL;
1604 +       }
1605 +       up(&plugin_lock);
1606 +
1607 +       try_module_get(THIS_MODULE);
1608 +
1609 +       dprintk("lirc_dev: plugin %s registered at minor number = %d\n",
1610 +               ir->p.name, ir->p.minor);
1611 +
1612 +       return minor;
1613 +}
1614 +
1615 +/*
1616 + *
1617 + */
1618 +int lirc_unregister_plugin(int minor)
1619 +{
1620 +       struct irctl *ir;
1621 +       DECLARE_MUTEX_LOCKED(tn);
1622 +       DECLARE_MUTEX_LOCKED(tn2);
1623 +
1624 +       if (minor < 0 || minor >= CONFIG_LIRC_MAX_DEV) {
1625 +               printk("lirc_dev: lirc_unregister_plugin:"
1626 +                      "\" minor\" must be beetween 0 and %d!\n",
1627 +                      CONFIG_LIRC_MAX_DEV-1);
1628 +               return -EBADRQC;
1629 +       }
1630 +
1631 +       ir = &irctls[minor];
1632 +
1633 +       down_interruptible(&plugin_lock);
1634 +
1635 +       if (ir->p.minor != minor) {
1636 +               printk("lirc_dev: lirc_unregister_plugin:"
1637 +                      "minor (%d) device not registered!", minor);
1638 +               up(&plugin_lock);
1639 +               return -ENOENT;
1640 +       }
1641 +
1642 +       if (ir->open) {
1643 +               printk("lirc_dev: lirc_unregister_plugin:"
1644 +                      "plugin %s[%d] in use!", ir->p.name, ir->p.minor);
1645 +               up(&plugin_lock);
1646 +               return -EBUSY;
1647 +       }
1648 +
1649 +       /* end up polling thread */
1650 +       if (ir->t_pid >= 0) {
1651 +               ir->t_notify = &tn;
1652 +               ir->t_notify2 = &tn2;
1653 +               ir->shutdown = 1;
1654 +               {
1655 +                       struct task_struct *p;
1656 +
1657 +                       p = find_task_by_pid(ir->t_pid);
1658 +                       wake_up_process(p);
1659 +               }
1660 +               up(&tn2);
1661 +               down(&tn);
1662 +               ir->t_notify = NULL;
1663 +               ir->t_notify2 = NULL;
1664 +       }
1665 +
1666 +       dprintk("lirc_dev: plugin %s unregistered from minor number = %d\n",
1667 +               ir->p.name, ir->p.minor);
1668 +
1669 +#ifdef CONFIG_DEVFS_FS
1670 +       devfs_remove("lirc/lirc%d", ir->p.minor);
1671 +#endif
1672 +
1673 +       if (ir->buf != ir->p.rbuf){
1674 +               lirc_buffer_free(ir->buf);
1675 +               kfree(ir->buf);
1676 +       }
1677 +       ir->buf = NULL;
1678 +       init_irctl(ir);
1679 +       up(&plugin_lock);
1680 +
1681 +       module_put(THIS_MODULE);
1682 +
1683 +       return SUCCESS;
1684 +}
1685 +
1686 +/*
1687 + *
1688 + */
1689 +static int irctl_open(struct inode *inode, struct file *file)
1690 +{
1691 +       struct irctl *ir;
1692 +       int retval;
1693 +
1694 +       if (MINOR(inode->i_rdev) >= CONFIG_LIRC_MAX_DEV) {
1695 +               dprintk("lirc_dev [%d]: open result = -ENODEV\n",
1696 +                       MINOR(inode->i_rdev));
1697 +               return -ENODEV;
1698 +       }
1699 +
1700 +       ir = &irctls[MINOR(inode->i_rdev)];
1701 +
1702 +       dprintk(LOGHEAD "open called\n", ir->p.name, ir->p.minor);
1703 +
1704 +       /* if the plugin has an open function use it instead */
1705 +       if(ir->p.fops && ir->p.fops->open)
1706 +               return ir->p.fops->open(inode, file);
1707 +
1708 +       down_interruptible(&plugin_lock);
1709 +
1710 +       if (ir->p.minor == NOPLUG) {
1711 +               up(&plugin_lock);
1712 +               dprintk(LOGHEAD "open result = -ENODEV\n",
1713 +                       ir->p.name, ir->p.minor);
1714 +               return -ENODEV;
1715 +       }
1716 +
1717 +       if (ir->open) {
1718 +               up(&plugin_lock);
1719 +               dprintk(LOGHEAD "open result = -EBUSY\n",
1720 +                       ir->p.name, ir->p.minor);
1721 +               return -EBUSY;
1722 +       }
1723 +
1724 +       /* there is no need for locking here because ir->open is 0
1725 +         * and lirc_thread isn't using buffer
1726 +        * plugins which use irq's should allocate them on set_use_inc,
1727 +        * so there should be no problem with those either.
1728 +         */
1729 +       ir->buf->head = ir->buf->tail;
1730 +       ir->buf->fill = 0;
1731 +
1732 +       ++ir->open;
1733 +       retval = ir->p.set_use_inc(ir->p.data);
1734 +
1735 +       up(&plugin_lock);
1736 +
1737 +       if (retval != SUCCESS) {
1738 +               --ir->open;
1739 +               return retval;
1740 +       }
1741 +
1742 +       dprintk(LOGHEAD "open result = %d\n", ir->p.name, ir->p.minor, SUCCESS);
1743 +
1744 +       return SUCCESS;
1745 +}
1746 +
1747 +/*
1748 + *
1749 + */
1750 +static int irctl_close(struct inode *inode, struct file *file)
1751 +{
1752 +       struct irctl *ir = &irctls[MINOR(inode->i_rdev)];
1753 +
1754 +       dprintk(LOGHEAD "close called\n", ir->p.name, ir->p.minor);
1755 +
1756 +       /* if the plugin has a close function use it instead */
1757 +       if(ir->p.fops && ir->p.fops->release)
1758 +               return ir->p.fops->release(inode, file);
1759 +
1760 +       down_interruptible(&plugin_lock);
1761 +
1762 +       --ir->open;
1763 +       ir->p.set_use_dec(ir->p.data);
1764 +
1765 +       up(&plugin_lock);
1766 +
1767 +       return SUCCESS;
1768 +}
1769 +
1770 +/*
1771 + *
1772 + */
1773 +static unsigned int irctl_poll(struct file *file, poll_table *wait)
1774 +{
1775 +       struct irctl *ir = &irctls[MINOR(file->f_dentry->d_inode->i_rdev)];
1776 +
1777 +       dprintk(LOGHEAD "poll called\n", ir->p.name, ir->p.minor);
1778 +
1779 +       /* if the plugin has a poll function use it instead */
1780 +       if(ir->p.fops && ir->p.fops->poll)
1781 +               return ir->p.fops->poll(file, wait);
1782 +
1783 +       poll_wait(file, &ir->buf->wait_poll, wait);
1784 +
1785 +       dprintk(LOGHEAD "poll result = %s\n",
1786 +               ir->p.name, ir->p.minor,
1787 +               lirc_buffer_empty(ir->buf) ? "0" : "POLLIN|POLLRDNORM");
1788 +
1789 +       return lirc_buffer_empty(ir->buf) ? 0 : (POLLIN|POLLRDNORM);
1790 +}
1791 +
1792 +/*
1793 + *
1794 + */
1795 +static int irctl_ioctl(struct inode *inode, struct file *file,
1796 +                       unsigned int cmd, unsigned long arg)
1797 +{
1798 +       unsigned long mode;
1799 +       int result;
1800 +       struct irctl *ir = &irctls[MINOR(inode->i_rdev)];
1801 +
1802 +       dprintk(LOGHEAD "ioctl called (%u)\n",
1803 +               ir->p.name, ir->p.minor, cmd);
1804 +
1805 +       /* if the plugin has a ioctl function use it instead */
1806 +       if(ir->p.fops && ir->p.fops->ioctl)
1807 +               return ir->p.fops->ioctl(inode, file, cmd, arg);
1808 +
1809 +       if (ir->p.minor == NOPLUG) {
1810 +               dprintk(LOGHEAD "ioctl result = -ENODEV\n",
1811 +                       ir->p.name, ir->p.minor);
1812 +               return -ENODEV;
1813 +       }
1814 +
1815 +       /* Give the plugin a chance to handle the ioctl */
1816 +       if(ir->p.ioctl){
1817 +               result = ir->p.ioctl(inode, file, cmd, arg);
1818 +               if (result != -ENOIOCTLCMD)
1819 +                       return result;
1820 +       }
1821 +       /* The plugin can't handle cmd */
1822 +       result = SUCCESS;
1823 +
1824 +       switch(cmd)
1825 +       {
1826 +       case LIRC_GET_FEATURES:
1827 +               result = put_user(ir->p.features, (unsigned long*)arg);
1828 +               break;
1829 +       case LIRC_GET_REC_MODE:
1830 +               if(!(ir->p.features&LIRC_CAN_REC_MASK))
1831 +                       return(-ENOSYS);
1832 +
1833 +               result = put_user(LIRC_REC2MODE
1834 +                                 (ir->p.features&LIRC_CAN_REC_MASK),
1835 +                                 (unsigned long*)arg);
1836 +               break;
1837 +       case LIRC_SET_REC_MODE:
1838 +               if(!(ir->p.features&LIRC_CAN_REC_MASK))
1839 +                       return(-ENOSYS);
1840 +
1841 +               result = get_user(mode, (unsigned long*)arg);
1842 +               if(!result && !(LIRC_MODE2REC(mode) & ir->p.features)) {
1843 +                       result = -EINVAL;
1844 +               }
1845 +               /* FIXME: We should actually set the mode somehow
1846 +                * but for now, lirc_serial doesn't support mode changin
1847 +                * eighter */
1848 +               break;
1849 +       case LIRC_GET_LENGTH:
1850 +               result = put_user((unsigned long)ir->p.code_length,
1851 +                                 (unsigned long *)arg);
1852 +               break;
1853 +       default:
1854 +               result = -ENOIOCTLCMD;
1855 +       }
1856 +
1857 +       dprintk(LOGHEAD "ioctl result = %d\n",
1858 +               ir->p.name, ir->p.minor, result);
1859 +
1860 +       return result;
1861 +}
1862 +
1863 +/*
1864 + *
1865 + */
1866 +static ssize_t irctl_read(struct file *file,
1867 +                         char *buffer,
1868 +                         size_t length,
1869 +                         loff_t *ppos)
1870 +{
1871 +       struct irctl *ir = &irctls[MINOR(file->f_dentry->d_inode->i_rdev)];
1872 +       unsigned char buf[ir->buf->chunk_size];
1873 +       int ret=0, written=0;
1874 +       DECLARE_WAITQUEUE(wait, current);
1875 +
1876 +       dprintk(LOGHEAD "read called\n", ir->p.name, ir->p.minor);
1877 +
1878 +       /* if the plugin has a specific read function use it instead */
1879 +       if(ir->p.fops && ir->p.fops->read)
1880 +               return ir->p.fops->read(file, buffer, length, ppos);
1881 +
1882 +       if (length % ir->buf->chunk_size) {
1883 +               dprintk(LOGHEAD "read result = -EINVAL\n",
1884 +                       ir->p.name, ir->p.minor);
1885 +               return -EINVAL;
1886 +       }
1887 +
1888 +       /* we add ourselves to the task queue before buffer check
1889 +         * to avoid losing scan code (in case when queue is awaken somewhere
1890 +        * beetwen while condition checking and scheduling)
1891 +        */
1892 +       add_wait_queue(&ir->buf->wait_poll, &wait);
1893 +       current->state = TASK_INTERRUPTIBLE;
1894 +
1895 +       /* while we did't provide 'length' bytes, device is opened in blocking
1896 +        * mode and 'copy_to_user' is happy, wait for data.
1897 +        */
1898 +       while (written < length && ret == 0) {
1899 +               if (lirc_buffer_empty(ir->buf)) {
1900 +                       /* According to the read(2) man page, 'written' can be
1901 +                        * returned as less than 'length', instead of blocking
1902 +                        * again, returning -EWOULDBLOCK, or returning
1903 +                        * -ERESTARTSYS */
1904 +                       if (written) break;
1905 +                       if (file->f_flags & O_NONBLOCK) {
1906 +                               dprintk(LOGHEAD "read result = -EWOULDBLOCK\n",
1907 +                                               ir->p.name, ir->p.minor);
1908 +                               remove_wait_queue(&ir->buf->wait_poll, &wait);
1909 +                               current->state = TASK_RUNNING;
1910 +                               return -EWOULDBLOCK;
1911 +                       }
1912 +                       if (signal_pending(current)) {
1913 +                               dprintk(LOGHEAD "read result = -ERESTARTSYS\n",
1914 +                                               ir->p.name, ir->p.minor);
1915 +                               remove_wait_queue(&ir->buf->wait_poll, &wait);
1916 +                               current->state = TASK_RUNNING;
1917 +                               return -ERESTARTSYS;
1918 +                       }
1919 +                       schedule();
1920 +                       current->state = TASK_INTERRUPTIBLE;
1921 +               } else {
1922 +                       lirc_buffer_read_1(ir->buf, buf);
1923 +                       ret = copy_to_user((void *)buffer+written, buf,
1924 +                                          ir->buf->chunk_size);
1925 +                       written += ir->buf->chunk_size;
1926 +               }
1927 +       }
1928 +
1929 +       remove_wait_queue(&ir->buf->wait_poll, &wait);
1930 +       current->state = TASK_RUNNING;
1931 +
1932 +       dprintk(LOGHEAD "read result = %s (%d)\n",
1933 +               ir->p.name, ir->p.minor, ret ? "-EFAULT" : "OK", ret);
1934 +
1935 +       return ret ? -EFAULT : written;
1936 +}
1937 +
1938 +static ssize_t irctl_write(struct file *file, const char *buffer,
1939 +                          size_t length, loff_t * ppos)
1940 +{
1941 +       struct irctl *ir = &irctls[MINOR(file->f_dentry->d_inode->i_rdev)];
1942 +
1943 +       dprintk(LOGHEAD "write called\n", ir->p.name, ir->p.minor);
1944 +
1945 +       /* if the plugin has a specific read function use it instead */
1946 +       if(ir->p.fops && ir->p.fops->write)
1947 +               return ir->p.fops->write(file, buffer, length, ppos);
1948 +
1949 +       return -EINVAL;
1950 +}
1951 +
1952 +
1953 +static struct file_operations fops = {
1954 +       read:    irctl_read,
1955 +       write:   irctl_write,
1956 +       poll:    irctl_poll,
1957 +       ioctl:   irctl_ioctl,
1958 +       open:    irctl_open,
1959 +       release: irctl_close
1960 +};
1961 +
1962 +static int __init lirc_dev_init(void)
1963 +{
1964 +       int i;
1965 +
1966 +       for (i=0; i < CONFIG_LIRC_MAX_DEV; ++i) {
1967 +               init_irctl(&irctls[i]);
1968 +       }
1969 +
1970 +       i = register_chrdev(IRCTL_DEV_MAJOR,
1971 +                                  IRCTL_DEV_NAME,
1972 +                                  &fops);
1973 +
1974 +       if (i < 0) {
1975 +               printk ("lirc_dev: device registration failed with %d\n", i);
1976 +               return i;
1977 +       }
1978 +
1979 +       printk("lirc_dev: IR Remote Control driver registered, at major %d \n",
1980 +              IRCTL_DEV_MAJOR);
1981 +
1982 +       return SUCCESS;
1983 +}
1984 +
1985 +static void __exit lirc_dev_exit(void)
1986 +{
1987 +       int ret;
1988 +
1989 +       ret = unregister_chrdev(IRCTL_DEV_MAJOR, IRCTL_DEV_NAME);
1990 +
1991 +       if (0 > ret){
1992 +               printk("lirc_dev: error in module_unregister_chrdev: %d\n",
1993 +                      ret);
1994 +       } else {
1995 +               dprintk("lirc_dev: module successfully unloaded\n");
1996 +       }
1997 +}
1998 +
1999 +/* ---------------------------------------------------------------------- */
2000 +
2001 +/* For now dont try to use it as a static version !  */
2002 +
2003 +MODULE_DESCRIPTION("LIRC base driver module");
2004 +MODULE_AUTHOR("Artur Lipowski");
2005 +MODULE_LICENSE("GPL");
2006 +
2007 +EXPORT_SYMBOL(lirc_register_plugin);
2008 +EXPORT_SYMBOL(lirc_unregister_plugin);
2009 +
2010 +module_init(lirc_dev_init);
2011 +module_exit(lirc_dev_exit);
2012 +
2013 +/*
2014 + * Overrides for Emacs so that we follow Linus's tabbing style.
2015 + * ---------------------------------------------------------------------------
2016 + * Local variables:
2017 + * c-basic-offset: 4
2018 + * End:
2019 + */
2020 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_dev.h linux-2.6.5/drivers/char/lirc/lirc_dev.h
2021 --- linux-2.6.5.orig/drivers/char/lirc/lirc_dev.h       1970-01-01 01:00:00.000000000 +0100
2022 +++ linux-2.6.5/drivers/char/lirc/lirc_dev.h    2004-04-04 14:05:29.409628328 +0200
2023 @@ -0,0 +1,237 @@
2024 +/*
2025 + * LIRC base driver
2026 + *
2027 + * (L) by Artur Lipowski <alipowski@interia.pl>
2028 + *        This code is licensed under GNU GPL
2029 + *
2030 + * $Id$
2031 + *
2032 + */
2033 +
2034 +#ifndef _LINUX_LIRC_DEV_H
2035 +#define _LINUX_LIRC_DEV_H
2036 +
2037 +#define BUFLEN            16
2038 +
2039 +//#define LIRC_BUFF_POWER_OF_2
2040 +#ifdef LIRC_BUFF_POWER_OF_2
2041 +#define mod(n, div) ((n) & ((div) -1))
2042 +#else
2043 +#define mod(n, div) ((n) % (div))
2044 +#endif
2045 +#include <linux/slab.h>
2046 +#include <linux/fs.h>
2047 +struct lirc_buffer
2048 +{
2049 +        wait_queue_head_t wait_poll;
2050 +       spinlock_t lock;
2051 +
2052 +       unsigned char *data;
2053 +       unsigned int chunk_size;
2054 +       unsigned int size; /* in chunks */
2055 +       unsigned int fill; /* in chunks */
2056 +       int head, tail;    /* in chunks */
2057 +       /* Using chunks instead of bytes pretends to simplify boundary checking
2058 +        * And should allow for some performance fine tunning later */
2059 +};
2060 +static inline int lirc_buffer_init(struct lirc_buffer *buf,
2061 +                                   unsigned int chunk_size,
2062 +                                   unsigned int size)
2063 +{
2064 +       /* Adjusting size to the next power of 2 would allow for
2065 +        * inconditional LIRC_BUFF_POWER_OF_2 optimization */
2066 +       init_waitqueue_head(&buf->wait_poll);
2067 +       spin_lock_init(&buf->lock);
2068 +       buf->head = buf->tail = buf->fill = 0;
2069 +       buf->chunk_size = chunk_size;
2070 +       buf->size = size;
2071 +       buf->data = kmalloc(size*chunk_size, GFP_KERNEL);
2072 +       if (buf->data == NULL)
2073 +               return -1;
2074 +       memset(buf->data, 0, size*chunk_size);
2075 +       return 0;
2076 +}
2077 +static inline void lirc_buffer_free(struct lirc_buffer *buf)
2078 +{
2079 +       kfree(buf->data);
2080 +       buf->data = NULL;
2081 +       buf->head = buf->tail = buf->fill = 0;
2082 +       buf->chunk_size = 0;
2083 +       buf->size = 0;
2084 +}
2085 +static inline int  lirc_buffer_full(struct lirc_buffer *buf)
2086 +{
2087 +       return (buf->fill >= buf->size);
2088 +}
2089 +static inline int  lirc_buffer_empty(struct lirc_buffer *buf)
2090 +{
2091 +       return !(buf->fill);
2092 +}
2093 +static inline int  lirc_buffer_available(struct lirc_buffer *buf)
2094 +{
2095 +    return (buf->size - buf->fill);
2096 +}
2097 +extern inline void lirc_buffer_lock(struct lirc_buffer *buf, unsigned long *flags)
2098 +{
2099 +       spin_lock_irqsave(&buf->lock, *flags);
2100 +}
2101 +extern inline void lirc_buffer_unlock(struct lirc_buffer *buf, unsigned long *flags)
2102 +{
2103 +       spin_unlock_irqrestore(&buf->lock, *flags);
2104 +}
2105 +static inline void _lirc_buffer_remove_1(struct lirc_buffer *buf)
2106 +{
2107 +       buf->head = mod(buf->head+1, buf->size);
2108 +       buf->fill -= 1;
2109 +}
2110 +static inline void lirc_buffer_remove_1(struct lirc_buffer *buf)
2111 +{
2112 +       unsigned long flags;
2113 +       lirc_buffer_lock(buf, &flags);
2114 +       _lirc_buffer_remove_1(buf);
2115 +       lirc_buffer_unlock(buf, &flags);
2116 +}
2117 +static inline void _lirc_buffer_read_1(struct lirc_buffer *buf,
2118 +                                    unsigned char *dest)
2119 +{
2120 +       memcpy(dest, &buf->data[buf->head*buf->chunk_size], buf->chunk_size);
2121 +       buf->head = mod(buf->head+1, buf->size);
2122 +       buf->fill -= 1;
2123 +}
2124 +static inline void lirc_buffer_read_1(struct lirc_buffer *buf,
2125 +                                     unsigned char *dest)
2126 +{
2127 +       unsigned long flags;
2128 +       lirc_buffer_lock(buf, &flags);
2129 +       _lirc_buffer_read_1(buf, dest);
2130 +       lirc_buffer_unlock(buf, &flags);
2131 +}
2132 +static inline void _lirc_buffer_write_1(struct lirc_buffer *buf,
2133 +                                     unsigned char *orig)
2134 +{
2135 +       memcpy(&buf->data[buf->tail*buf->chunk_size], orig, buf->chunk_size);
2136 +       buf->tail = mod(buf->tail+1, buf->size);
2137 +       buf->fill++;
2138 +}
2139 +static inline void lirc_buffer_write_1(struct lirc_buffer *buf,
2140 +                                      unsigned char *orig)
2141 +{
2142 +       unsigned long flags;
2143 +       lirc_buffer_lock(buf, &flags);
2144 +       _lirc_buffer_write_1(buf, orig);
2145 +       lirc_buffer_unlock(buf, &flags);
2146 +}
2147 +static inline void _lirc_buffer_write_n(struct lirc_buffer *buf,
2148 +                       unsigned char* orig, int count)
2149 +{
2150 +    memcpy(&buf->data[buf->tail*buf->chunk_size], orig, count*buf->chunk_size);
2151 +    buf->tail = mod(buf->tail+count, buf->size);
2152 +    buf->fill += count;
2153 +}
2154 +static inline void lirc_buffer_write_n(struct lirc_buffer *buf,
2155 +                       unsigned char* orig, int count)
2156 +{
2157 +    unsigned long flags;
2158 +    int space1;
2159 +    lirc_buffer_lock(buf,&flags);
2160 +    if( buf->head > buf->tail ) space1 = buf->head - buf->tail;
2161 +    else space1 = buf->size - buf->tail;
2162 +    
2163 +    if( count > space1 )
2164 +    {
2165 +        _lirc_buffer_write_n(buf, orig, space1);
2166 +        _lirc_buffer_write_n(buf, orig+(space1*buf->chunk_size), count-space1);
2167 +    }
2168 +    else
2169 +        _lirc_buffer_write_n(buf, orig, count);
2170 +    lirc_buffer_unlock(buf, &flags);
2171 +}
2172 +
2173 +struct lirc_plugin
2174 +{
2175 +     char name[40];
2176 +     int minor;
2177 +     int code_length;
2178 +     int sample_rate;
2179 +     unsigned long features;
2180 +     void* data;
2181 +     int (*add_to_buf) (void* data, struct lirc_buffer* buf);
2182 +     wait_queue_head_t* (*get_queue) (void* data);
2183 +     struct lirc_buffer *rbuf;
2184 +     int (*set_use_inc) (void* data);
2185 +     void (*set_use_dec) (void* data);
2186 +     int (*ioctl) (struct inode *,struct file *,unsigned int, unsigned long);
2187 +     struct file_operations *fops;
2188 +};
2189 +/* name:
2190 + * this string will be used for logs
2191 + *
2192 + * minor:
2193 + * indicates minor device (/dev/lircd) number for registered plugin
2194 + * if caller fills it with negative value, then the first free minor
2195 + * number will be used (if available)
2196 + *
2197 + * code_length:
2198 + * length of the remote control key code expressed in bits
2199 + * if code_length > 8 then many bytes are returned through the device read
2200 + * in such situation get_key should return key code values starting
2201 + * from most significant byte (device read will preseve this order)
2202 + * in addition if code_length > 8 then get_key will be called
2203 + * several (ceil(code_length/8)) times in one pool pass (or after task queue
2204 + * awake) key_no parameter denotes number of the requested byte (0 means first
2205 + * byte)
2206 + *
2207 + * sample_rate:
2208 + * sample_rate equal to 0 means that no polling will be performed and add_to_buf
2209 + * will be triggered by external events (through task queue returned by
2210 + * get_queue)
2211 + *
2212 + * data:
2213 + * it may point to any plugin data and this pointer will be passed to all
2214 + * callback functions
2215 + *
2216 + * add_to_buf:
2217 + * add_to_buf will be called after specified period of the time or triggered
2218 + * by the external event, this behavior depends on value of the sample_rate
2219 + * this function will be called in user context. this routine should return 0
2220 + * if data was added to the buffer and -ENODATA if none was available. this should
2221 + * add some number of bits evenly divisible by code_length to the buffer
2222 + *
2223 + * get_queue:
2224 + * this callback should return a pointer to the task queue which will be used
2225 + * for external event waiting
2226 + *
2227 + * rbuf:
2228 + * if not NULL, it will be used as a read buffer, you will have to write to
2229 + * the buffer by other means, like irq's (see also lirc_serial.c).
2230 + *
2231 + * set_use_inc:
2232 + * set_use_inc will be called after device is opened
2233 + *
2234 + * set_use_dec:
2235 + * set_use_dec will be called after device is closed
2236 + *
2237 + * ioctl:
2238 + * Some ioctl's can be directly handled by lirc_dev but will be forwared here
2239 + * if not NULL and only handled if it returns -ENOIOCTLCMD (see also
2240 + * lirc_serial.c).
2241 + *
2242 + * fops:
2243 + * file_operations for drivers which don't fit the current plugin model.
2244 + */
2245 +
2246 +
2247 +/* following functions can be called ONLY from user context
2248 + *
2249 + * returns negative value on error or minor number
2250 + * of the registered device if success
2251 + * contens of the structure pointed by p is copied
2252 + */
2253 +extern int lirc_register_plugin(struct lirc_plugin *p);
2254 +
2255 +/* returns negative value on error or 0 if success
2256 +*/
2257 +extern int lirc_unregister_plugin(int minor);
2258 +
2259 +
2260 +#endif
2261 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_gpio.c linux-2.6.5/drivers/char/lirc/lirc_gpio.c
2262 --- linux-2.6.5.orig/drivers/char/lirc/lirc_gpio.c      1970-01-01 01:00:00.000000000 +0100
2263 +++ linux-2.6.5/drivers/char/lirc/lirc_gpio.c   2004-04-04 14:05:29.411628024 +0200
2264 @@ -0,0 +1,546 @@
2265 +/*
2266 + * Remote control driver for the TV-card
2267 + * key codes are obtained from GPIO port
2268 + *
2269 + * (L) by Artur Lipowski <alipowski@interia.pl>
2270 + *     patch for the AverMedia by Santiago Garcia Mantinan <manty@i.am>
2271 + *                            and Christoph Bartelmus <lirc@bartelmus.de>
2272 + *     patch for the BestBuy by Miguel Angel Alvarez <maacruz@navegalia.com>
2273 + *     patch for the Winfast TV2000 by Juan Toledo
2274 + *     <toledo@users.sourceforge.net>
2275 + *     patch for the I-O Data GV-BCTV5/PCI by Jens C. Rasmussen
2276 + *     <jens.rasmussen@ieee.org>
2277 + *
2278 + *  This program is free software; you can redistribute it and/or modify
2279 + *  it under the terms of the GNU General Public License as published by
2280 + *  the Free Software Foundation; either version 2 of the License, or
2281 + *  (at your option) any later version.
2282 + *
2283 + *  This program is distributed in the hope that it will be useful,
2284 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
2285 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2286 + *  GNU General Public License for more details.
2287 + *
2288 + *  You should have received a copy of the GNU General Public License
2289 + *  along with this program; if not, write to the Free Software
2290 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2291 + *
2292 + * $Id$
2293 + *
2294 + */
2295 +
2296 +#include <linux/version.h>
2297 +
2298 +#include <linux/module.h>
2299 +#include <linux/kmod.h>
2300 +#include <linux/sched.h>
2301 +#include <linux/errno.h>
2302 +#include <linux/init.h>
2303 +#include <linux/moduleparam.h>
2304 +
2305 +#include "../../media/video/bttv.h"
2306 +#include "../../media/video/bttvp.h"
2307 +
2308 +#include "lirc_dev.h"
2309 +
2310 +static int debug = 0;
2311 +static int card = 0;
2312 +static int minor = -1;
2313 +static int bttv_id = BTTV_UNKNOWN;
2314 +static unsigned long gpio_mask = 0;
2315 +static unsigned long gpio_enable = 0;
2316 +static unsigned long gpio_lock_mask = 0;
2317 +static unsigned long gpio_xor_mask = 0;
2318 +static unsigned int soft_gap = 0;
2319 +static unsigned char sample_rate = 10;
2320 +
2321 +MODULE_PARM(debug,"i");
2322 +MODULE_PARM(card,"i");
2323 +MODULE_PARM(minor,"i");
2324 +MODULE_PARM(gpio_mask,"l");
2325 +MODULE_PARM(gpio_lock_mask,"l");
2326 +MODULE_PARM(gpio_xor_mask,"l");
2327 +MODULE_PARM(soft_gap,"i");
2328 +MODULE_PARM(sample_rate,"b");
2329 +MODULE_PARM(bttv_id,"i");
2330 +
2331 +#undef dprintk
2332 +#define dprintk  if (debug) printk
2333 +
2334 +struct rcv_info {
2335 +       int bttv_id;
2336 +       int card_id;
2337 +       unsigned long gpio_mask;
2338 +       unsigned long gpio_enable;
2339 +       unsigned long gpio_lock_mask;
2340 +       unsigned long gpio_xor_mask;
2341 +       unsigned int soft_gap;
2342 +       unsigned char sample_rate;
2343 +       unsigned char code_length;
2344 +};
2345 +
2346 +static struct rcv_info rcv_infos[] = {
2347 +       {BTTV_UNKNOWN,                0,          0,          0,         0,          0,   0,  1,  0},
2348 +       {BTTV_PXELVWPLTVPAK,          0, 0x00003e00,          0, 0x0010000,          0,   0, 15, 32},
2349 +       {BTTV_PXELVWPLTVPRO,          0, 0x00001f00,          0, 0x0008000,          0, 500, 12, 32},
2350 +       {BTTV_PV_BT878P_9B,           0, 0x00001f00,          0, 0x0008000,          0, 500, 12, 32},
2351 +       {BTTV_PV_BT878P_PLUS,         0, 0x00001f00,          0, 0x0008000,          0, 500, 12, 32},
2352 +       {BTTV_AVERMEDIA,              0, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32},
2353 +       {BTTV_AVPHONE98,     0x00011461, 0x003b8000, 0x00004000, 0x0800000, 0x00800000,   0, 10,  0}, /*mapped to Capture98*/
2354 +       {BTTV_AVERMEDIA98,   0x00021461, 0x003b8000, 0x00004000, 0x0800000, 0x00800000,   0, 10,  0}, /*mapped to Capture98*/
2355 +       {BTTV_AVPHONE98,     0x00031461, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32}, /*mapped to Phone98*/
2356 +       /* is this one correct? */
2357 +       {BTTV_AVERMEDIA98,   0x00041461, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32}, /*mapped to Phone98*/
2358 +       /* work-around for VDOMATE */
2359 +       {BTTV_AVERMEDIA98,   0x03001461, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32}, /*mapped to Phone98*/
2360 +       /* reported by Danijel Korzinek, AVerTV GOw/FM */
2361 +       {BTTV_AVERMEDIA98,   0x00000000, 0x00f88000,          0, 0x0010000, 0x00010000,   0, 10, 32}, /*mapped to Phone98*/
2362 +       {BTTV_CHRONOS_VS2,            0, 0x000000f8,          0, 0x0000100,          0,   0, 20,  0},
2363 +       /* CPH031 and CPH033 cards (?) */
2364 +       /* MIRO was just a work-around */
2365 +       {BTTV_MIRO,                   0, 0x00001f00,          0, 0x0004000,          0,   0, 10, 32},
2366 +       {BTTV_DYNALINK,               0, 0x00001f00,          0, 0x0004000,          0,   0, 10, 32},
2367 +       {BTTV_WINVIEW_601,            0, 0x00001f00,          0, 0x0004000,          0,   0,  0, 32},
2368 +#ifdef BTTV_KWORLD
2369 +       {BTTV_KWORLD,                 0, 0x00007f00,          0, 0x0004000,          0,   0, 12, 32},
2370 +#endif
2371 +       /* just a guess */
2372 +       {BTTV_MAGICTVIEW061,          0, 0x0028e000,          0, 0x0020000,          0,   0, 20, 32},
2373 +       {BTTV_MAGICTVIEW063,          0, 0x0028e000,          0, 0x0020000,          0,   0, 20, 32},
2374 +       {BTTV_PHOEBE_TVMAS,           0, 0x0028e000,          0, 0x0020000,          0,   0, 20, 32},
2375 +#ifdef BTTV_BESTBUY_EASYTV2
2376 +        {BTTV_BESTBUY_EASYTV,         0, 0x00007F00,          0, 0x0004000,          0,   0, 10,  8},
2377 +        {BTTV_BESTBUY_EASYTV2,        0, 0x00007F00,          0, 0x0008000,          0,   0, 10,  8},
2378 +#endif
2379 +       /* lock_mask probably also 0x100, or maybe it is 0x0 for all others !?! */
2380 +       {BTTV_FLYVIDEO,               0, 0x000000f8,          0,         0,          0,   0,  0, 42},
2381 +       {BTTV_FLYVIDEO_98,            0, 0x000000f8,          0, 0x0000100,          0,   0,  0, 42},
2382 +       {BTTV_TYPHOON_TVIEW,          0, 0x000000f8,          0, 0x0000100,          0,   0,  0, 42},
2383 +#ifdef BTTV_FLYVIDEO_98FM
2384 +       /* smorar@alfonzo.smuts.uct.ac.za */
2385 +       {BTTV_FLYVIDEO_98FM,          0, 0x000000f8,          0, 0x0000100,          0,   0,  0, 42},
2386 +#endif
2387 +       /* The Leadtek WinFast TV 2000 XP card (id 0x6606107d) uses an
2388 +        * extra gpio bit compared to the original TV 2000 card (id
2389 +        * 0x217d6606); as the bttv-0.7.100 driver does not
2390 +        * distinguish between the two cards, we enable the extra bit
2391 +        * based on the card id: */
2392 +       {BTTV_WINFAST2000,   0x6606107d, 0x000008f8,          0, 0x0000100,          0,   0,  0, 32},
2393 +       /* default: */
2394 +       {BTTV_WINFAST2000,            0, 0x000000f8,          0, 0x0000100,          0,   0,  0, 32},
2395 +#ifdef BTTV_GVBCTV5PCI
2396 +       {BTTV_GVBCTV5PCI,             0, 0x00f0b000,          0,         0,          0,   0, 20,  8},
2397 +#endif
2398 +};
2399 +
2400 +static unsigned char code_length = 0;
2401 +static unsigned char code_bytes = 1;
2402 +
2403 +#define MAX_BYTES 8
2404 +
2405 +#define SUCCESS 0
2406 +#define LOGHEAD "lirc_gpio (%d): "
2407 +
2408 +/* how many bits GPIO value can be shifted right before processing
2409 + * it is computed from the value of gpio_mask_parameter
2410 + */
2411 +static unsigned char gpio_pre_shift = 0;
2412 +
2413 +
2414 +static inline int reverse(int data, int bits)
2415 +{
2416 +       int i;
2417 +       int c;
2418 +
2419 +       for (c=0,i=0; i<bits; i++) {
2420 +               c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
2421 +       }
2422 +
2423 +       return c;
2424 +}
2425 +
2426 +static int build_key(unsigned long gpio_val, unsigned char codes[MAX_BYTES])
2427 +{
2428 +       unsigned long mask = gpio_mask;
2429 +       unsigned char shift = 0;
2430 +
2431 +       dprintk(LOGHEAD "gpio_val is %lx\n",card,(unsigned long) gpio_val);
2432 +
2433 +       gpio_val ^= gpio_xor_mask;
2434 +
2435 +       if (gpio_lock_mask && (gpio_val & gpio_lock_mask)) {
2436 +               return -EBUSY;
2437 +       }
2438 +
2439 +       switch (bttv_id)
2440 +       {
2441 +       case BTTV_AVERMEDIA98:
2442 +               if (bttv_write_gpio(card, gpio_enable, gpio_enable)) {
2443 +                       dprintk(LOGHEAD "cannot write to GPIO\n", card);
2444 +                       return -EIO;
2445 +               }
2446 +               if (bttv_read_gpio(card, &gpio_val)) {
2447 +                       dprintk(LOGHEAD "cannot read GPIO\n", card);
2448 +                       return -EIO;
2449 +               }
2450 +               if (bttv_write_gpio(card, gpio_enable, 0)) {
2451 +                       dprintk(LOGHEAD "cannot write to GPIO\n", card);
2452 +                       return -EIO;
2453 +               }
2454 +               break;
2455 +       default:
2456 +               break;
2457 +       }
2458 +
2459 +       /* extract bits from "raw" GPIO value using gpio_mask */
2460 +       codes[0] = 0;
2461 +       gpio_val >>= gpio_pre_shift;
2462 +       while (mask) {
2463 +               if (mask & 1u) {
2464 +                       codes[0] |= (gpio_val & 1u) << shift++;
2465 +               }
2466 +               mask >>= 1;
2467 +               gpio_val >>= 1;
2468 +       }
2469 +
2470 +       dprintk(LOGHEAD "code is %lx\n",card,(unsigned long) codes[0]);
2471 +       switch (bttv_id)
2472 +       {
2473 +       case BTTV_AVERMEDIA:
2474 +               codes[2] = (codes[0]<<2)&0xff;
2475 +               codes[3] = (~codes[2])&0xff;
2476 +               codes[0] = 0x02;
2477 +               codes[1] = 0xFD;
2478 +               break;
2479 +       case BTTV_AVPHONE98:
2480 +               codes[2] = ((codes[0]&(~0x1))<<2)&0xff;
2481 +               codes[3] = (~codes[2])&0xff;
2482 +               if (codes[0]&0x1) {
2483 +                       codes[0] = 0xc0;
2484 +                       codes[1] = 0x3f;
2485 +               } else {
2486 +                       codes[0] = 0x40;
2487 +                       codes[1] = 0xbf;
2488 +               }
2489 +               break;
2490 +       case BTTV_AVERMEDIA98:
2491 +               break;
2492 +       case BTTV_FLYVIDEO:
2493 +       case BTTV_FLYVIDEO_98:
2494 +       case BTTV_TYPHOON_TVIEW:
2495 +#ifdef BTTV_FLYVIDEO_98FM
2496 +       case BTTV_FLYVIDEO_98FM:
2497 +#endif
2498 +               codes[4]=codes[0]<<3;
2499 +               codes[5]=((~codes[4])&0xff);
2500 +
2501 +               codes[0]=0x00;
2502 +               codes[1]=0x1A;
2503 +               codes[2]=0x1F;
2504 +               codes[3]=0x2F;
2505 +               break;
2506 +        case BTTV_MAGICTVIEW061:
2507 +        case BTTV_MAGICTVIEW063:
2508 +       case BTTV_PHOEBE_TVMAS:
2509 +               codes[0] = (codes[0]&0x01)
2510 +                       |((codes[0]&0x02)<<1)
2511 +                       |((codes[0]&0x04)<<2)
2512 +                       |((codes[0]&0x08)>>2)
2513 +                       |((codes[0]&0x10)>>1);
2514 +               /* FALLTHROUGH */
2515 +       case BTTV_MIRO:
2516 +       case BTTV_DYNALINK:
2517 +       case BTTV_PXELVWPLTVPAK:
2518 +       case BTTV_PXELVWPLTVPRO:
2519 +       case BTTV_PV_BT878P_9B:
2520 +       case BTTV_PV_BT878P_PLUS:
2521 +#ifdef BTTV_KWORLD
2522 +       case BTTV_KWORLD:
2523 +#endif
2524 +               codes[2] = reverse(codes[0],8);
2525 +               codes[3] = (~codes[2])&0xff;
2526 +               codes[0] = 0x61;
2527 +               codes[1] = 0xD6;
2528 +               break;
2529 +#if 0
2530 +               /* derived from e-tech config file */
2531 +               /* 26 + 16 bits */
2532 +               /* won't apply it until it's confirmed with a fly98 */
2533 +       case BTTV_FLYVIDEO_98:
2534 +       case BTTV_FLYVIDEO_98FM:
2535 +               codes[4]=codes[0]<<3;
2536 +               codes[5]=(~codes[4])&0xff;
2537 +
2538 +               codes[0]=0x00;
2539 +               codes[1]=0x1A;
2540 +               codes[2]=0x1F;
2541 +               codes[3]=0x2F;
2542 +               break;
2543 +#endif
2544 +       case BTTV_WINFAST2000:
2545 +               /* shift extra bit */
2546 +               codes[0] = (codes[0]&0x1f) | ((codes[0]&0x20) << 1);
2547 +       case BTTV_WINVIEW_601:
2548 +               codes[2] = reverse(codes[0],8);
2549 +               codes[3] = (~codes[2])&0xff;
2550 +               codes[0] = 0xC0;
2551 +               codes[1] = 0x3F;
2552 +               break;
2553 +       default:
2554 +               break;
2555 +       }
2556 +
2557 +       return SUCCESS;
2558 +}
2559 +
2560 +/* add_to_buf - copy a code to the buffer */
2561 +static int add_to_buf(void* data, struct lirc_buffer* buf)
2562 +{
2563 +       static unsigned long next_time = 0;
2564 +       static unsigned char prev_codes[MAX_BYTES];
2565 +       unsigned long code = 0;
2566 +       unsigned char cur_codes[MAX_BYTES];
2567 +
2568 +       if (bttv_read_gpio(card, &code)) {
2569 +               dprintk(LOGHEAD "cannot read GPIO\n", card);
2570 +               return -EIO;
2571 +       }
2572 +
2573 +       if (build_key(code, cur_codes)) {
2574 +               return -EFAULT;
2575 +       }
2576 +
2577 +               
2578 +       /* XXX this should be double checked; i think the soft_gap
2579 +        * here is supposed to track repeats, which is why i save off
2580 +        * the prev_codes, but i'm not certain this is correct
2581 +        */
2582 +       if (soft_gap) {
2583 +               if (!memcmp(prev_codes, cur_codes, code_bytes) &&
2584 +                       jiffies < next_time) {
2585 +                       return -EAGAIN;
2586 +               }
2587 +               next_time = jiffies + soft_gap;
2588 +       }
2589 +       memcpy( prev_codes, cur_codes, code_bytes );
2590 +
2591 +       lirc_buffer_write_1( buf, cur_codes );
2592 +
2593 +       return SUCCESS;
2594 +}
2595 +
2596 +static int set_use_inc(void* data)
2597 +{
2598 +       try_module_get(THIS_MODULE);
2599 +       return 0;
2600 +}
2601 +
2602 +static void set_use_dec(void* data)
2603 +{
2604 +       module_put(THIS_MODULE);
2605 +}
2606 +
2607 +static wait_queue_head_t* get_queue(void* data)
2608 +{
2609 +       return bttv_get_gpio_queue(card);
2610 +}
2611 +
2612 +static struct lirc_plugin plugin = {
2613 +       .name           = "lirc_gpio  ",
2614 +       .add_to_buf     = add_to_buf,
2615 +       .get_queue      = get_queue,
2616 +       .set_use_inc    = set_use_inc,
2617 +       .set_use_dec    = set_use_dec,
2618 +};
2619 +
2620 +/*
2621 + *
2622 + */
2623 +int gpio_remote_init(void)
2624 +{
2625 +       int ret;
2626 +       unsigned int mask;
2627 +
2628 +       /* "normalize" gpio_mask
2629 +        * this means shift it right until first bit is set
2630 +        */
2631 +       while (!(gpio_mask & 1u)) {
2632 +               gpio_pre_shift++;
2633 +               gpio_mask >>= 1;
2634 +       }
2635 +
2636 +       if (code_length) {
2637 +               plugin.code_length = code_length;
2638 +       } else {
2639 +               /* calculate scan code length in bits if needed */
2640 +               plugin.code_length = 1;
2641 +               mask = gpio_mask >> 1;
2642 +               while (mask) {
2643 +                       if (mask & 1u) {
2644 +                               plugin.code_length++;
2645 +                       }
2646 +                       mask >>= 1;
2647 +               }
2648 +       }
2649 +
2650 +       code_bytes = (plugin.code_length/8) + (plugin.code_length%8 ? 1 : 0);
2651 +       if (MAX_BYTES < code_bytes) {
2652 +               printk (LOGHEAD "scan code too long (%d bytes)\n",
2653 +                       minor, code_bytes);
2654 +               return -EBADRQC;
2655 +       }
2656 +
2657 +       if (gpio_enable) {
2658 +               if(bttv_gpio_enable(card, gpio_enable, gpio_enable)) {
2659 +                       printk(LOGHEAD "gpio_enable failure\n", minor);
2660 +                       return -EIO;
2661 +               }
2662 +       }
2663 +
2664 +
2665 +       /* translate ms to jiffies */
2666 +       soft_gap = (soft_gap*HZ) / 1000;
2667 +
2668 +       plugin.minor = minor;
2669 +       plugin.sample_rate = sample_rate;
2670 +
2671 +       ret = lirc_register_plugin(&plugin);
2672 +
2673 +       if (0 > ret) {
2674 +               printk (LOGHEAD "device registration failed with %d\n",
2675 +                       minor, ret);
2676 +               return ret;
2677 +       }
2678 +
2679 +       minor = ret;
2680 +       printk(LOGHEAD "driver registered\n", minor);
2681 +
2682 +       return SUCCESS;
2683 +}
2684 +
2685 +static int __init lirc_gpio_init(void)
2686 +{
2687 +       int type,cardid,card_type;
2688 +
2689 +       if (CONFIG_LIRC_MAX_DEV < minor) {
2690 +               printk("lirc_gpio: parameter minor (%d) must be less than %d!\n",
2691 +                      minor, CONFIG_LIRC_MAX_DEV-1);
2692 +               return -EBADRQC;
2693 +       }
2694 +
2695 +       request_module("bttv");
2696 +
2697 +       /* if gpio_mask not zero then use module parameters
2698 +        * instead of autodetecting TV card
2699 +        */
2700 +       if (gpio_mask) {
2701 +               if (sample_rate!=0 && (2 > sample_rate || 50 < sample_rate)) {
2702 +                       printk(LOGHEAD "parameter sample_rate "
2703 +                              "must be beetween 2 and 50!\n", minor);
2704 +                       return -EBADRQC;
2705 +               }
2706 +
2707 +               if (sample_rate!=0 && soft_gap &&
2708 +                   ((2000/sample_rate) > soft_gap || 1000 < soft_gap)) {
2709 +                       printk(LOGHEAD "parameter soft_gap "
2710 +                              "must be beetween %d and 1000!\n",
2711 +                              minor, 2000/sample_rate);
2712 +                       return -EBADRQC;
2713 +               }
2714 +       } else {
2715 +               if(bttv_get_cardinfo(card,&type,&cardid)==-1) {
2716 +                       printk(LOGHEAD "could not get card type\n", minor);
2717 +               }
2718 +               printk(LOGHEAD "card type 0x%x, id 0x%x\n",minor,
2719 +                      type,cardid);
2720 +
2721 +               if (type == BTTV_UNKNOWN) {
2722 +                       printk(LOGHEAD "cannot detect TV card nr %d!\n",
2723 +                              minor, card);
2724 +                       return -EBADRQC;
2725 +               }
2726 +               for (card_type = 1;
2727 +                    card_type < sizeof(rcv_infos)/sizeof(struct rcv_info);
2728 +                    card_type++) {
2729 +                       if (rcv_infos[card_type].bttv_id == type &&
2730 +                           (rcv_infos[card_type].card_id == 0 ||
2731 +                            rcv_infos[card_type].card_id == cardid)) {
2732 +                               bttv_id = rcv_infos[card_type].bttv_id;
2733 +                               gpio_mask = rcv_infos[card_type].gpio_mask;
2734 +                               gpio_enable = rcv_infos[card_type].gpio_enable;
2735 +                               gpio_lock_mask = rcv_infos[card_type].gpio_lock_mask;
2736 +                               gpio_xor_mask = rcv_infos[card_type].gpio_xor_mask;
2737 +                               soft_gap = rcv_infos[card_type].soft_gap;
2738 +                               sample_rate = rcv_infos[card_type].sample_rate;
2739 +                               code_length = rcv_infos[card_type].code_length;
2740 +                               break;
2741 +                       }
2742 +               }
2743 +               if (type==BTTV_AVPHONE98 && cardid==0x00011461) {
2744 +                       bttv_id = BTTV_AVERMEDIA98;
2745 +               }
2746 +               if (type==BTTV_AVERMEDIA98 && cardid==0x00041461) {
2747 +                       bttv_id = BTTV_AVPHONE98;
2748 +               }
2749 +               if (type==BTTV_AVERMEDIA98 && cardid==0x03001461) {
2750 +                       bttv_id = BTTV_AVPHONE98;
2751 +               }
2752 +               if (type==BTTV_AVERMEDIA98 && cardid==0x00000000) {
2753 +                       bttv_id = BTTV_AVPHONE98;
2754 +               }
2755 +               if (card_type == sizeof(rcv_infos)/sizeof(struct rcv_info)) {
2756 +                       printk(LOGHEAD "TV card type 0x%x not supported!\n",
2757 +                              minor, type);
2758 +                       return -EBADRQC;
2759 +               }
2760 +       }
2761 +
2762 +       request_module("lirc_dev");
2763 +
2764 +       return gpio_remote_init();
2765 +}
2766 +
2767 +void __exit lirc_gpio_exit(void)
2768 +{
2769 +       int ret;
2770 +
2771 +       ret = lirc_unregister_plugin(minor);
2772 +
2773 +       if (0 > ret) {
2774 +               printk(LOGHEAD "error in lirc_unregister_minor: %d\n"
2775 +                      "Trying again...\n",
2776 +                      minor, ret);
2777 +
2778 +               current->state = TASK_INTERRUPTIBLE;
2779 +               schedule_timeout(HZ);
2780 +
2781 +               ret = lirc_unregister_plugin(minor);
2782 +
2783 +               if (0 > ret) {
2784 +                       printk(LOGHEAD "error in lirc_unregister_minor: %d!!!\n",
2785 +                              minor, ret);
2786 +                       return;
2787 +               }
2788 +       }
2789 +
2790 +       dprintk(LOGHEAD "module successfully unloaded\n", minor);
2791 +}
2792 +
2793 +MODULE_DESCRIPTION("Driver module for remote control (data from bt848 GPIO port)");
2794 +MODULE_AUTHOR("Artur Lipowski");
2795 +MODULE_LICENSE("GPL");
2796 +
2797 +#ifdef MODULE
2798 +       module_init(lirc_gpio_init);
2799 +#else
2800 +       late_initcall(lirc_gpio_init);
2801 +#endif
2802 +module_exit(lirc_gpio_exit);
2803 +
2804 +/*
2805 + * Overrides for Emacs so that we follow Linus's tabbing style.
2806 + * ---------------------------------------------------------------------------
2807 + * Local variables:
2808 + * c-basic-offset: 8
2809 + * End:
2810 + */
2811 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_i2c.c linux-2.6.5/drivers/char/lirc/lirc_i2c.c
2812 --- linux-2.6.5.orig/drivers/char/lirc/lirc_i2c.c       1970-01-01 01:00:00.000000000 +0100
2813 +++ linux-2.6.5/drivers/char/lirc/lirc_i2c.c    2004-04-04 14:05:29.413627720 +0200
2814 @@ -0,0 +1,438 @@
2815 +/*      $Id$      */
2816 +
2817 +/*
2818 + * i2c IR lirc plugin for Hauppauge and Pixelview cards - new 2.8.x i2c stack
2819 + *
2820 + * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
2821 + * modified for PixelView (BT878P+W/FM) by
2822 + *      Michal Kochanowicz <mkochano@pld.org.pl>
2823 + *      Christoph Bartelmus <lirc@bartelmus.de>
2824 + * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
2825 + *      Ulrich Mueller <ulrich.mueller42@web.de>
2826 + * modified for Asus TV-Box and Creative/VisionTek BreakOut-Box by
2827 + *      Stefan Jahn <stefan@lkcc.org>
2828 + * modified for Linux 2.6 by
2829 + *     Jeffrey Clark <jeff@clarkmania.com>
2830 + *
2831 + * parts are cut&pasted from the old lirc_haup.c driver
2832 + *
2833 + *  This program is free software; you can redistribute it and/or modify
2834 + *  it under the terms of the GNU General Public License as published by
2835 + *  the Free Software Foundation; either version 2 of the License, or
2836 + *  (at your option) any later version.
2837 + *
2838 + *  This program is distributed in the hope that it will be useful,
2839 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
2840 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2841 + *  GNU General Public License for more details.
2842 + *
2843 + *  You should have received a copy of the GNU General Public License
2844 + *  along with this program; if not, write to the Free Software
2845 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
2846 + *
2847 + */
2848 +
2849 +#include <linux/module.h>
2850 +#include <linux/kmod.h>
2851 +#include <linux/kernel.h>
2852 +#include <linux/sched.h>
2853 +#include <linux/string.h>
2854 +#include <linux/timer.h>
2855 +#include <linux/delay.h>
2856 +#include <linux/errno.h>
2857 +#include <linux/slab.h>
2858 +#include <linux/init.h>
2859 +#include <linux/moduleparam.h>
2860 +
2861 +#include <linux/i2c.h>
2862 +#include <linux/i2c-algo-bit.h>
2863 +#include <asm/semaphore.h>
2864 +
2865 +#include "../../media/video/bttv.h"
2866 +
2867 +#include "lirc_dev.h"
2868 +
2869 +static unsigned short normal_i2c[] = { 0x1a, 0x18, 0x4b, 0x64, 0x30, 0x21, 0x23, I2C_CLIENT_END };
2870 +static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
2871 +static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
2872 +static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
2873 +static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
2874 +static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
2875 +static unsigned short force[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
2876 +
2877 +static struct i2c_client_address_data addr_data = {
2878 +       .normal_i2c             = normal_i2c,
2879 +       .normal_i2c_range       = normal_i2c_range,
2880 +       .probe                  = probe,
2881 +       .probe_range            = probe_range,
2882 +       .ignore                 = ignore,
2883 +       .ignore_range           = ignore_range,
2884 +       .force                  = force
2885 +};
2886 +
2887 +struct i2c_ir {
2888 +       struct lirc_plugin lirc;
2889 +       struct i2c_client client;
2890 +       int nextkey;
2891 +       unsigned char b[3];
2892 +       unsigned char bits;
2893 +       unsigned char flag;
2894 +};
2895 +
2896 +/* ----------------------------------------------------------------------- */
2897 +/* insmod parameters                                                       */
2898 +
2899 +static int debug   = 0;    /* debug output */
2900 +static int minor   = -1;   /* minor number */
2901 +
2902 +MODULE_PARM(debug,"i");
2903 +MODULE_PARM(minor,"i");
2904 +
2905 +MODULE_DESCRIPTION("Infrared receiver driver for Hauppauge and Pixelview cards (i2c stack)");
2906 +MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller, Stefan Jahn, Jeffrey Clark");
2907 +MODULE_LICENSE("GPL");
2908 +
2909 +#define dprintk if (debug) printk
2910 +
2911 +/* ----------------------------------------------------------------------- */
2912 +
2913 +#define DRIVER_NAME "lirc_i2c"
2914 +#define DEVICE_NAME DRIVER_NAME
2915 +
2916 +/* ----------------------------------------------------------------------- */
2917 +
2918 +static inline int reverse(int data, int bits)
2919 +{
2920 +       int i;
2921 +       int c;
2922 +       
2923 +       for (c=0,i=0; i<bits; i++) {
2924 +               c |= (((data & (1<<i)) ? 1:0)) << (bits-1-i);
2925 +       }
2926 +
2927 +       return c;
2928 +}
2929 +
2930 +static int add_to_buf_pcf8574(void* data, struct lirc_buffer* buf)
2931 +{
2932 +       struct i2c_ir *ir = data;
2933 +       int rc;
2934 +       unsigned char all, mask;
2935 +       unsigned char key;
2936 +
2937 +       /* compute all valid bits (key code + pressed/release flag) */
2938 +       all = ir->bits | ir->flag;
2939 +
2940 +       /* save IR writable mask bits */
2941 +       mask = i2c_smbus_read_byte(&ir->client) & ~all;
2942 +
2943 +       /* send bit mask */
2944 +       rc = i2c_smbus_write_byte(&ir->client, (0xff & all) | mask);
2945 +
2946 +       /* receive scan code */
2947 +       rc = i2c_smbus_read_byte(&ir->client);
2948 +
2949 +       if (rc == -1) {
2950 +               /* should this return -EIO? */  
2951 +               dprintk(DEVICE_NAME ": %s read error\n", ir->c.name);
2952 +               return -ENODATA;
2953 +       }
2954 +
2955 +       /* drop duplicate polls */
2956 +       if (ir->b[0] == (rc & all)) {
2957 +               return -ENODATA;
2958 +       }
2959 +       ir->b[0] = rc & all;
2960 +
2961 +       dprintk(DRIVER_NAME ": %s key 0x%02X %s\n",
2962 +               ir->client.name, rc & ir->bits,
2963 +               (rc & ir->flag) ? "released" : "pressed");
2964 +
2965 +       if (rc & ir->flag) {
2966 +               /* ignore released buttons */
2967 +               return -ENODATA;
2968 +       }
2969 +
2970 +       /* set valid key code */
2971 +       key  = rc & ir->bits;
2972 +       lirc_buffer_write_1( buf, &key );
2973 +       return 0;
2974 +}
2975 +
2976 +static int add_to_buf_haup(void* data, struct lirc_buffer* buf)
2977 +{
2978 +       struct i2c_ir *ir = data;
2979 +        unsigned char keybuf[3];
2980 +       __u16 code;
2981 +       unsigned char codes[2];
2982 +
2983 +       /* poll IR chip */
2984 +       if (3 == i2c_master_recv(&ir->c,keybuf,3)) {
2985 +               ir->b[0] = keybuf[0];
2986 +               ir->b[1] = keybuf[1];
2987 +               ir->b[2] = keybuf[2];
2988 +       } else {
2989 +               dprintk(DRIVER_NAME ": read error\n");
2990 +               /* keep last successfull read buffer */
2991 +       }
2992 +
2993 +       /* key pressed ? */
2994 +       if ((ir->b[0] & 0x80) == 0)
2995 +               return -ENODATA;
2996 +       
2997 +       dprintk(DRIVER_NAME ": key (0x%02x/0x%02x)\n",
2998 +       ir->b[0], ir->b[1]);
2999 +
3000 +       /* look what we have */
3001 +       code = (((__u16)ir->b[0]&0x7f)<<6) | (ir->b[1]>>2);
3002 +
3003 +       codes[0] = (code >> 8) & 0xff;
3004 +       codes[1] = code & 0xff;
3005 +
3006 +       /* return it */
3007 +       lirc_buffer_write_1( buf, codes );
3008 +       return 0;
3009 +}
3010 +
3011 +static int add_to_buf_pixelview(void* data, struct lirc_buffer* buf)
3012 +{
3013 +       struct i2c_ir *ir = data;
3014 +       unsigned char key;
3015 +       
3016 +       /* poll IR chip */
3017 +       if (1 != i2c_master_recv(&ir->c,&key,1)) {
3018 +               dprintk(DRIVER_NAME ": read error\n");
3019 +               return -1;
3020 +       }
3021 +
3022 +       dprintk(KERN_DEBUG DEVICE_NAME ": key %02x\n", key);
3023 +
3024 +       /* return it */
3025 +       lirc_buffer_write_1( buf, &key );
3026 +       *key = b;
3027 +       return 0;
3028 +}
3029 +
3030 +static int add_to_buf_pv951(void* data, struct lirc_buffer* buf)
3031 +{
3032 +       struct i2c_ir *ir = data;
3033 +       unsigned char key;
3034 +       unsigned char codes[4];
3035 +       
3036 +       /* poll IR chip */
3037 +       if (1 != i2c_master_recv(&ir->c,&key,1)) {
3038 +               dprintk(DRIVER_NAME ": read error\n");
3039 +               return -ENODATA;
3040 +       }
3041 +       /* ignore 0xaa */
3042 +       if (key==0xaa)
3043 +               return -ENODATA;
3044 +       dprintk(KERN_DEBUG DEVICE_NAME ": key %02x\n", key);
3045 +
3046 +       codes[3] = 0x61;
3047 +       codes[2] = 0xD6;
3048 +       codes[1] = reverse(key,8);
3049 +       codes[0] = (~codes[2])&0xff;
3050 +       
3051 +       lirc_buffer_write_1( buf, codes );
3052 +       return 0;
3053 +}
3054 +
3055 +static int add_to_buf_knc1(void *data, struct lirc_buffer* buf)
3056 +{
3057 +       static unsigned char last_key = 0xFF;
3058 +       struct i2c_ir *ir = data;
3059 +       unsigned char key;
3060 +       
3061 +       /* poll IR chip */
3062 +       if (1 != i2c_master_recv(&ir->c,&key,1)) {
3063 +               dprintk(DRIVER_NAME ": read error\n");
3064 +               return -ENODATA;
3065 +       }
3066 +       
3067 +       /* it seems that 0xFE indicates that a button is still hold
3068 +          down, while 0xFF indicates that no button is hold
3069 +          down. 0xFE sequences are sometimes interrupted by 0xFF */
3070 +       
3071 +       dprintk(KERN_DEBUG DEVICE_NAME ": key %02x\n", key);
3072 +       
3073 +       if( key == 0xFF )
3074 +               return -ENODATA;
3075 +
3076 +       if ( key == 0xFE )
3077 +               key = last_key;
3078 +
3079 +       last_key = key;
3080 +       lirc_buffer_write_1( buf, &key );
3081 +
3082 +       return 0;
3083 +}
3084 +
3085 +static int set_use_inc(void* data)
3086 +{
3087 +       try_module_get(THIS_MODULE);
3088 +       return 0;
3089 +}
3090 +
3091 +static void set_use_dec(void* data)
3092 +{
3093 +       module_put(THIS_MODULE);
3094 +}
3095 +
3096 +static struct lirc_plugin lirc_template = {
3097 +       .name =        "lirc_i2c",
3098 +       .set_use_inc = set_use_inc,
3099 +       .set_use_dec = set_use_dec
3100 +};
3101 +
3102 +/* ----------------------------------------------------------------------- */
3103 +
3104 +static int lirc_i2c_attach(struct i2c_adapter *adap, int addr, int kind);
3105 +static int lirc_i2c_detach(struct i2c_client *client);
3106 +static int lirc_i2c_probe(struct i2c_adapter *adap);
3107 +
3108 +static struct i2c_driver driver = {
3109 +       .owner          = THIS_MODULE,
3110 +        .name           = DRIVER_NAME,
3111 +        .id             = I2C_DRIVERID_EXP3, /* FIXME */
3112 +        .flags          = I2C_DF_NOTIFY,
3113 +        .attach_adapter = lirc_i2c_probe,
3114 +        .detach_client  = lirc_i2c_detach,
3115 +};
3116 +
3117 +static struct i2c_client client_template = 
3118 +{
3119 +        I2C_DEVNAME("(unset)"),
3120 +       .flags  = I2C_CLIENT_ALLOW_USE,
3121 +        .driver = &driver
3122 +};
3123 +
3124 +static int lirc_i2c_attach(struct i2c_adapter *adap, int addr, int kind)
3125 +{
3126 +        struct i2c_ir *ir;
3127 +       int ret;
3128 +       
3129 +        client_template.adapter = adap;
3130 +        client_template.addr = addr;
3131 +       
3132 +       if (NULL == (ir = kmalloc(sizeof(struct i2c_ir),GFP_KERNEL)))
3133 +                return -ENOMEM;
3134 +       memset(ir,0,sizeof(struct i2c_ir));
3135 +       memcpy(&ir->client,&client_template,sizeof(struct i2c_client));
3136 +        memcpy(&ir->lirc,&lirc_template,sizeof(struct lirc_plugin));
3137 +       
3138 +       ir->lirc.data           = ir;
3139 +       ir->lirc.minor          = minor;
3140 +       ir->nextkey             = -1;
3141 +
3142 +       i2c_set_clientdata(&ir->client,ir);
3143 +
3144 +       switch(addr)
3145 +       {
3146 +       case 0x64:
3147 +               strncpy(ir->client.name, "Pixelview IR", I2C_NAME_SIZE);
3148 +               ir->lirc.code_length = 8;
3149 +               ir->lirc.sample_rate = 10;
3150 +               ir->lirc.add_to_buf = add_to_buf_pixelview;
3151 +               break;
3152 +       case 0x4b:
3153 +               strncpy(ir->client.name,"PV951 IR", I2C_NAME_SIZE);
3154 +               ir->lirc.code_length = 32;
3155 +               ir->lirc.sample_rate = 10;
3156 +               ir->lirc.add_to_buf = add_to_buf_pv951;
3157 +               break;
3158 +       case 0x18:
3159 +       case 0x1a:
3160 +               strncpy(ir->client.name,"Hauppauge IR", I2C_NAME_SIZE);
3161 +               ir->lirc.code_length = 13;
3162 +               ir->lirc.sample_rate = 6;
3163 +               ir->lirc.add_to_buf = add_to_buf_haup;
3164 +               break;
3165 +       case 0x30:
3166 +               strncpy(ir->client.name,"KNC ONE IR", I2C_NAME_SIZE);
3167 +               ir->lirc.code_length = 8;
3168 +               ir->lirc.sample_rate = 10;
3169 +               ir->lirc.add_to_buf = add_to_buf_knc1;
3170 +               break;
3171 +       case 0x21:
3172 +       case 0x23:
3173 +               strncpy(ir->client.name,"TV-Box IR", I2C_NAME_SIZE);
3174 +               ir->lirc.code_length = 8;
3175 +               ir->lirc.sample_rate = 10;
3176 +               ir->lirc.add_to_buf = add_to_buf_pcf8574;
3177 +               ir->bits = ir->client.flags & 0xff;
3178 +               ir->flag = (ir->client.flags >> 8) & 0xff;
3179 +               break;
3180 +               
3181 +       default:
3182 +               /* shouldn't happen */
3183 +               dprintk(DRIVER_NAME ": unknown i2c address (0x%02x)?\n",addr);
3184 +               kfree(ir);
3185 +               return -1;
3186 +       }
3187 +       dprintk(DRIVER_NAME ": chip found @ 0x%02x (%s)\n",addr,
3188 +                       ir->client.name);
3189 +       
3190 +       /* register device */
3191 +       i2c_attach_client(&ir->client);
3192 +       
3193 +       if((ret = lirc_register_plugin(&ir->lirc))) {
3194 +               dprintk(DRIVER_NAME ": device registration failed with %d\n",
3195 +                               ret);
3196 +               kfree(ir);
3197 +               return -1;
3198 +       }
3199 +
3200 +       ir->lirc.minor = ret;
3201 +       dprintk(DRIVER_NAME ": driver registered\n");
3202 +
3203 +       return 0;
3204 +}
3205 +
3206 +static int lirc_i2c_detach(struct i2c_client *client)
3207 +{
3208 +        struct i2c_ir *ir = i2c_get_clientdata(client);
3209 +       int err;
3210 +       
3211 +       /* unregister device */
3212 +       if ((err = lirc_unregister_plugin(ir->lirc.minor))) {
3213 +               dprintk(DRIVER_NAME ": lirc unregister failed\n");
3214 +               return err;
3215 +       } else {
3216 +               dprintk(DRIVER_NAME ": lirc unregister successful\n");
3217 +       }
3218 +
3219 +       if ((err = i2c_detach_client(&ir->client))) {
3220 +               dprintk(DRIVER_NAME ": i2c detach failed\n");
3221 +               return err;
3222 +       } else {
3223 +               dprintk(DRIVER_NAME ": i2c detach successful\n");
3224 +       }
3225 +
3226 +       /* free memory */
3227 +       kfree(ir);
3228 +       return 0;
3229 +}
3230 +
3231 +static int lirc_i2c_probe(struct i2c_adapter *adap) {
3232 +       dprintk(DRIVER_NAME ": starting probe for adapter %s (0x%x)\n",
3233 +                       adap->name, adap->id);
3234 +       return i2c_probe(adap, &addr_data, lirc_i2c_attach);
3235 +}
3236 +
3237 +static int __init lirc_i2c_init(void)
3238 +{
3239 +       dprintk(DRIVER_NAME ": init\n");
3240 +       request_module("bttv");
3241 +       request_module("lirc_dev");
3242 +       return i2c_add_driver(&driver);
3243 +}
3244 +
3245 +static void __exit lirc_i2c_exit(void)
3246 +{
3247 +       dprintk(DRIVER_NAME ": exit\n");
3248 +       i2c_del_driver(&driver);
3249 +}
3250 +
3251 +module_init(lirc_i2c_init);
3252 +module_exit(lirc_i2c_exit);
3253 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_it87.c linux-2.6.5/drivers/char/lirc/lirc_it87.c
3254 --- linux-2.6.5.orig/drivers/char/lirc/lirc_it87.c      1970-01-01 01:00:00.000000000 +0100
3255 +++ linux-2.6.5/drivers/char/lirc/lirc_it87.c   2004-04-04 14:05:29.416627264 +0200
3256 @@ -0,0 +1,956 @@
3257 +/*
3258 + * LIRC driver for ITE IT8712/IT8705 CIR port
3259 + *
3260 + * Copyright (C) 2001 Hans-Günter Lütke Uphues <hg_lu@web.de>
3261 + *
3262 + * This program is free software; you can redistribute it and/or
3263 + * modify it under the terms of the GNU General Public License as
3264 + * published by the Free Software Foundation; either version 2 of the
3265 + * License, or (at your option) any later version.
3266 + *
3267 + * This program is distributed in the hope that it will be useful, but
3268 + * WITHOUT ANY WARRANTY; without even the implied warranty of
3269 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3270 + * General Public License for more details.
3271 +
3272 + * You should have received a copy of the GNU General Public License
3273 + * along with this program; if not, write to the Free Software
3274 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
3275 + * USA
3276 + *
3277 + * ITE IT8705 and IT8712(not tested) CIR-port support for lirc based
3278 + * via cut and paste from lirc_sir.c (C) 2000 Milan Pikula
3279 + *
3280 + * Attention: Sendmode only tested with debugging logs
3281 + *
3282 + * 2001/02/27 Christoph Bartelmus <lirc@bartelmus.de> :
3283 + *   reimplemented read function
3284 + */
3285 +
3286 +
3287 +#include <linux/version.h>
3288 +#include <linux/module.h>
3289 +
3290 +#include <linux/config.h>
3291 +
3292 +
3293 +#include <linux/sched.h>
3294 +#include <linux/errno.h>
3295 +#include <linux/signal.h>
3296 +#include <linux/fs.h>
3297 +#include <linux/interrupt.h>
3298 +#include <linux/ioport.h>
3299 +#include <linux/kernel.h>
3300 +#include <linux/major.h>
3301 +#include <linux/serial_reg.h>
3302 +#include <linux/time.h>
3303 +#include <linux/string.h>
3304 +#include <linux/types.h>
3305 +#include <linux/wait.h>
3306 +#include <linux/mm.h>
3307 +#include <linux/delay.h>
3308 +#include <linux/poll.h>
3309 +#include <asm/system.h>
3310 +#include <asm/segment.h>
3311 +#include <asm/io.h>
3312 +#include <asm/irq.h>
3313 +#include <asm/fcntl.h>
3314 +
3315 +#include <linux/timer.h>
3316 +
3317 +#include <linux/lirc.h>
3318 +#include "lirc_dev.h"
3319 +
3320 +#include "lirc_it87.h"
3321 +
3322 +static unsigned long it87_bits_in_byte_out = 0;
3323 +static unsigned long it87_send_counter = 0;
3324 +static unsigned char it87_RXEN_mask = IT87_CIR_RCR_RXEN;
3325 +
3326 +#define RBUF_LEN 1024
3327 +#define WBUF_LEN 1024
3328 +
3329 +#define LIRC_DRIVER_NAME "lirc_it87"
3330 +
3331 +/* timeout for sequences in jiffies (=5/100s) */
3332 +/* must be longer than TIME_CONST */
3333 +#define IT87_TIMEOUT   (HZ*5/100)
3334 +
3335 +static int io = IT87_CIR_DEFAULT_IOBASE;
3336 +static int irq = IT87_CIR_DEFAULT_IRQ;
3337 +static unsigned char it87_freq = 38; /* kHz */
3338 +/* receiver demodulator default: off */
3339 +static unsigned char it87_enable_demodulator = 0;
3340 +
3341 +static spinlock_t timer_lock = SPIN_LOCK_UNLOCKED;
3342 +static struct timer_list timerlist;
3343 +/* time of last signal change detected */
3344 +static struct timeval last_tv = {0, 0};
3345 +/* time of last UART data ready interrupt */
3346 +static struct timeval last_intr_tv = {0, 0};
3347 +static int last_value = 0;
3348 +
3349 +static DECLARE_WAIT_QUEUE_HEAD(lirc_read_queue);
3350 +
3351 +static spinlock_t hardware_lock = SPIN_LOCK_UNLOCKED;
3352 +static spinlock_t dev_lock = SPIN_LOCK_UNLOCKED;
3353 +
3354 +static lirc_t rx_buf[RBUF_LEN]; unsigned int rx_tail = 0, rx_head = 0;
3355 +static lirc_t tx_buf[WBUF_LEN];
3356 +
3357 +/* SECTION: Prototypes */
3358 +
3359 +/* Communication with user-space */
3360 +static int lirc_open(struct inode * inode,
3361 +                    struct file * file);
3362 +static int lirc_close(struct inode * inode,
3363 +                     struct file *file);
3364 +static unsigned int lirc_poll(struct file * file,
3365 +                             poll_table * wait);
3366 +static ssize_t lirc_read(struct file * file,
3367 +                        char * buf,
3368 +                        size_t count,
3369 +                        loff_t * ppos);
3370 +static ssize_t lirc_write(struct file * file,
3371 +                         const char * buf,
3372 +                         size_t n,
3373 +                         loff_t * pos);
3374 +static int lirc_ioctl(struct inode *node,
3375 +                     struct file *filep,
3376 +                     unsigned int cmd,
3377 +                     unsigned long arg);
3378 +static void add_read_queue(int flag,
3379 +                          unsigned long val);
3380 +static int init_chrdev(void);
3381 +static void drop_chrdev(void);
3382 +       /* Hardware */
3383 +static void it87_interrupt(int irq,
3384 +                          void * dev_id,
3385 +                          struct pt_regs * regs);
3386 +static void send_space(unsigned long len);
3387 +static void send_pulse(unsigned long len);
3388 +static void init_send(void);
3389 +static void terminate_send(unsigned long len);
3390 +static int init_hardware(void);
3391 +static void drop_hardware(void);
3392 +       /* Initialisation */
3393 +static int init_port(void);
3394 +static void drop_port(void);
3395 +int init_module(void);
3396 +void cleanup_module(void);
3397 +
3398 +
3399 +/* SECTION: Communication with user-space */
3400 +
3401 +static int lirc_open(struct inode * inode,
3402 +                    struct file * file)
3403 +{
3404 +       spin_lock(&dev_lock);
3405 +#ifdef CONFIG_MODULE_UNLOAD
3406 +       if (module_refcount(THIS_MODULE))
3407 +       {
3408 +               spin_unlock(&dev_lock);
3409 +               return -EBUSY;
3410 +       }
3411 +#endif
3412 +       try_module_get(THIS_MODULE);
3413 +       spin_unlock(&dev_lock);
3414 +       return 0;
3415 +}
3416 +
3417 +
3418 +static int lirc_close(struct inode * inode,
3419 +                     struct file *file)
3420 +{
3421 +       module_put(THIS_MODULE);
3422 +       return 0;
3423 +}
3424 +
3425 +
3426 +static unsigned int lirc_poll(struct file * file,
3427 +                             poll_table * wait)
3428 +{
3429 +       poll_wait(file, &lirc_read_queue, wait);
3430 +       if (rx_head != rx_tail)
3431 +               return POLLIN | POLLRDNORM;
3432 +       return 0;
3433 +}
3434 +
3435 +
3436 +static ssize_t lirc_read(struct file * file,
3437 +                        char * buf,
3438 +                        size_t count,
3439 +                        loff_t * ppos)
3440 +{
3441 +       int n=0;
3442 +       int retval=0;
3443 +
3444 +       while(n<count)
3445 +       {
3446 +               if(file->f_flags & O_NONBLOCK &&
3447 +                  rx_head==rx_tail)
3448 +               {
3449 +                       retval = -EAGAIN;
3450 +                       break;
3451 +               }
3452 +               retval=wait_event_interruptible(lirc_read_queue,
3453 +                                               rx_head!=rx_tail);
3454 +               if(retval)
3455 +               {
3456 +                       break;
3457 +               }
3458 +
3459 +               retval=verify_area(VERIFY_WRITE,(void *) buf+n,
3460 +                                  sizeof(lirc_t));
3461 +               if (retval)
3462 +               {
3463 +                       return retval;
3464 +               }
3465 +               copy_to_user((void *) buf+n,(void *) (rx_buf+rx_head),
3466 +                            sizeof(lirc_t));
3467 +               rx_head=(rx_head+1)&(RBUF_LEN-1);
3468 +               n+=sizeof(lirc_t);
3469 +       }
3470 +       if(n)
3471 +       {
3472 +               return n;
3473 +       }
3474 +       return retval;
3475 +}
3476 +
3477 +
3478 +static ssize_t lirc_write(struct file * file,
3479 +                         const char * buf,
3480 +                         size_t n,
3481 +                         loff_t * pos)
3482 +{
3483 +       int i;
3484 +       int retval;
3485 +
3486 +        if(n%sizeof(lirc_t) || (n/sizeof(lirc_t)) > WBUF_LEN)
3487 +               return(-EINVAL);
3488 +       retval = verify_area(VERIFY_READ, buf, n);
3489 +       if (retval)
3490 +               return retval;
3491 +       copy_from_user(tx_buf, buf, n);
3492 +       i = 0;
3493 +       n/=sizeof(lirc_t);
3494 +       init_send();
3495 +       while (1) {
3496 +               if (i >= n)
3497 +                       break;
3498 +               if (tx_buf[i])
3499 +                       send_pulse(tx_buf[i]);
3500 +               i++;
3501 +               if (i >= n)
3502 +                       break;
3503 +               if (tx_buf[i])
3504 +                       send_space(tx_buf[i]);
3505 +               i++;
3506 +       }
3507 +       terminate_send(tx_buf[i-1]);
3508 +       return n;
3509 +}
3510 +
3511 +
3512 +static int lirc_ioctl(struct inode *node,
3513 +                     struct file *filep,
3514 +                     unsigned int cmd,
3515 +                     unsigned long arg)
3516 +{
3517 +       int retval = 0;
3518 +       unsigned long value = 0;
3519 +       unsigned int ivalue;
3520 +
3521 +       if (cmd == LIRC_GET_FEATURES)
3522 +               value = LIRC_CAN_SEND_PULSE |
3523 +                       LIRC_CAN_SET_SEND_CARRIER |
3524 +                       LIRC_CAN_REC_MODE2;
3525 +       else if (cmd == LIRC_GET_SEND_MODE)
3526 +               value = LIRC_MODE_PULSE;
3527 +       else if (cmd == LIRC_GET_REC_MODE)
3528 +               value = LIRC_MODE_MODE2;
3529 +
3530 +       switch (cmd) {
3531 +       case LIRC_GET_FEATURES:
3532 +       case LIRC_GET_SEND_MODE:
3533 +       case LIRC_GET_REC_MODE:
3534 +               retval = put_user(value, (unsigned long *) arg);
3535 +               break;
3536 +
3537 +       case LIRC_SET_SEND_MODE:
3538 +       case LIRC_SET_REC_MODE:
3539 +               retval = get_user(value, (unsigned long *) arg);
3540 +               break;
3541 +
3542 +       case LIRC_SET_SEND_CARRIER:
3543 +               retval=get_user(ivalue,(unsigned int *) arg);
3544 +               if(retval) return(retval);
3545 +               ivalue /= 1000;
3546 +               if (ivalue > IT87_CIR_FREQ_MAX ||
3547 +                   ivalue < IT87_CIR_FREQ_MIN) return(-EINVAL);
3548 +
3549 +               it87_freq = ivalue;
3550 +               {
3551 +                       unsigned long hw_flags;
3552 +
3553 +                       spin_lock_irqsave(&hardware_lock, hw_flags);
3554 +                       outb(((inb(io + IT87_CIR_TCR2) & IT87_CIR_TCR2_TXMPW) |
3555 +                             (it87_freq - IT87_CIR_FREQ_MIN) << 3),
3556 +                            io + IT87_CIR_TCR2);
3557 +                       spin_unlock_irqrestore(&hardware_lock, hw_flags);
3558 +#ifdef DEBUG
3559 +                       printk(KERN_DEBUG LIRC_DRIVER_NAME
3560 +                              " demodulation frequency: %d kHz\n", it87_freq);
3561 +#endif
3562 +               }
3563 +
3564 +               break;
3565 +
3566 +       default:
3567 +               retval = -ENOIOCTLCMD;
3568 +       }
3569 +
3570 +       if (retval)
3571 +               return retval;
3572 +
3573 +       if (cmd == LIRC_SET_REC_MODE) {
3574 +               if (value != LIRC_MODE_MODE2)
3575 +                       retval = -ENOSYS;
3576 +       } else if (cmd == LIRC_SET_SEND_MODE) {
3577 +               if (value != LIRC_MODE_PULSE)
3578 +                       retval = -ENOSYS;
3579 +       }
3580 +       return retval;
3581 +}
3582 +
3583 +static void add_read_queue(int flag,
3584 +                          unsigned long val)
3585 +{
3586 +       unsigned int new_rx_tail;
3587 +       lirc_t newval;
3588 +
3589 +#ifdef DEBUG_SIGNAL
3590 +       printk(KERN_DEBUG LIRC_DRIVER_NAME
3591 +              ": add flag %d with val %lu\n",
3592 +              flag,val);
3593 +#endif
3594 +
3595 +       newval = val & PULSE_MASK;
3596 +
3597 +       /* statistically pulses are ~TIME_CONST/2 too long: we could
3598 +          maybe make this more exactly but this is good enough */
3599 +       if(flag) /* pulse */ {
3600 +               if(newval>TIME_CONST/2) {
3601 +                       newval-=TIME_CONST/2;
3602 +               }
3603 +               else /* should not ever happen */ {
3604 +                       newval=1;
3605 +               }
3606 +               newval|=PULSE_BIT;
3607 +       }
3608 +       else {
3609 +               newval+=TIME_CONST/2;
3610 +       }
3611 +       new_rx_tail = (rx_tail + 1) & (RBUF_LEN - 1);
3612 +       if (new_rx_tail == rx_head) {
3613 +#ifdef DEBUG
3614 +               printk(KERN_WARNING LIRC_DRIVER_NAME ": Buffer overrun.\n");
3615 +#endif
3616 +               return;
3617 +       }
3618 +       rx_buf[rx_tail] = newval;
3619 +       rx_tail = new_rx_tail;
3620 +       wake_up_interruptible(&lirc_read_queue);
3621 +}
3622 +
3623 +
3624 +static struct file_operations lirc_fops = {
3625 +       read:    lirc_read,
3626 +       write:   lirc_write,
3627 +       poll:    lirc_poll,
3628 +       ioctl:   lirc_ioctl,
3629 +       open:    lirc_open,
3630 +       release: lirc_close,
3631 +};
3632 +
3633 +static int set_use_inc(void* data)
3634 +{
3635 +#if WE_DONT_USE_LOCAL_OPEN_CLOSE
3636 +       try_module_get(THIS_MODULE);
3637 +#endif
3638 +       return 0;
3639 +}
3640 +
3641 +static void set_use_dec(void* data)
3642 +{
3643 +#if WE_DONT_USE_LOCAL_OPEN_CLOSE
3644 +       module_put(THIS_MODULE);
3645 +#endif
3646 +}
3647 +static struct lirc_plugin plugin = {
3648 +       name:           LIRC_DRIVER_NAME,
3649 +       minor:          -1,
3650 +       code_length:    1,
3651 +       sample_rate:    0,
3652 +       data:           NULL,
3653 +       add_to_buf:     NULL,
3654 +       get_queue:      NULL,
3655 +       set_use_inc:    set_use_inc,
3656 +       set_use_dec:    set_use_dec,
3657 +       fops:           &lirc_fops,
3658 +};
3659 +
3660 +
3661 +int init_chrdev(void)
3662 +{
3663 +       plugin.minor = lirc_register_plugin(&plugin);
3664 +
3665 +       if (plugin.minor < 0) {
3666 +               printk(KERN_ERR LIRC_DRIVER_NAME ": init_chrdev() failed.\n");
3667 +               return -EIO;
3668 +       }
3669 +       return 0;
3670 +}
3671 +
3672 +
3673 +static void drop_chrdev(void)
3674 +{
3675 +       lirc_unregister_plugin(plugin.minor);
3676 +}
3677 +
3678 +/* SECTION: Hardware */
3679 +static long delta(struct timeval * tv1,
3680 +                 struct timeval * tv2)
3681 +{
3682 +       unsigned long deltv;
3683 +
3684 +       deltv = tv2->tv_sec - tv1->tv_sec;
3685 +       if (deltv > 15)
3686 +               deltv = 0xFFFFFF;
3687 +       else
3688 +               deltv = deltv*1000000 +
3689 +                       tv2->tv_usec -
3690 +                       tv1->tv_usec;
3691 +       return deltv;
3692 +}
3693 +
3694 +
3695 +static void it87_timeout(unsigned long data)
3696 +{
3697 +       /* if last received signal was a pulse, but receiving stopped
3698 +          within the 9 bit frame, we need to finish this pulse and
3699 +          simulate a signal change to from pulse to space. Otherwise
3700 +          upper layers will receive two sequences next time. */
3701 +
3702 +       unsigned long flags;
3703 +       unsigned long pulse_end;
3704 +
3705 +       /* avoid interference with interrupt */
3706 +       spin_lock_irqsave(&timer_lock, flags);
3707 +       if (last_value) {
3708 +               /* determine 'virtual' pulse end: */
3709 +               pulse_end = delta(&last_tv, &last_intr_tv);
3710 +#ifdef DEBUG_SIGNAL
3711 +               printk(KERN_DEBUG LIRC_DRIVER_NAME
3712 +                      ": timeout add %d for %lu usec\n",
3713 +                      last_value,
3714 +                      pulse_end);
3715 +#endif
3716 +               add_read_queue(last_value,
3717 +                              pulse_end);
3718 +               last_value = 0;
3719 +               last_tv=last_intr_tv;
3720 +       }
3721 +       spin_unlock_irqrestore(&timer_lock, flags);
3722 +}
3723 +
3724 +
3725 +static void it87_interrupt(int irq,
3726 +                          void * dev_id,
3727 +                          struct pt_regs * regs)
3728 +{
3729 +       unsigned char data;
3730 +       struct timeval curr_tv;
3731 +       static unsigned long deltv;
3732 +       unsigned long deltintrtv;
3733 +       unsigned long flags, hw_flags;
3734 +       int iir, lsr;
3735 +       int fifo = 0;
3736 +
3737 +       iir = inb(io + IT87_CIR_IIR);
3738 +
3739 +       switch (iir & IT87_CIR_IIR_IID) {
3740 +       case 0x4:
3741 +       case 0x6:
3742 +               lsr = inb(io + IT87_CIR_RSR) & (IT87_CIR_RSR_RXFTO |
3743 +                                                   IT87_CIR_RSR_RXFBC);
3744 +               fifo = lsr & IT87_CIR_RSR_RXFBC;
3745 +#ifdef DEBUG_SIGNAL
3746 +               printk(KERN_DEBUG LIRC_DRIVER_NAME
3747 +                      "iir: 0x%x fifo: 0x%x\n", iir, lsr);
3748 +#endif
3749 +
3750 +               /* avoid interference with timer */
3751 +               spin_lock_irqsave(&timer_lock, flags);
3752 +               spin_lock_irqsave(&hardware_lock, hw_flags);
3753 +               do {
3754 +                       del_timer(&timerlist);
3755 +                       data = inb(io + IT87_CIR_DR);
3756 +#ifdef DEBUG_SIGNAL
3757 +                       printk(KERN_DEBUG LIRC_DRIVER_NAME
3758 +                              ": data=%.2x\n",
3759 +                              data);
3760 +#endif
3761 +                       do_gettimeofday(&curr_tv);
3762 +                       deltv = delta(&last_tv, &curr_tv);
3763 +                       deltintrtv = delta(&last_intr_tv, &curr_tv);
3764 +#ifdef DEBUG_SIGNAL
3765 +                       printk(KERN_DEBUG LIRC_DRIVER_NAME
3766 +                              ": t %lu , d %d\n",
3767 +                              deltintrtv,
3768 +                              (int)data);
3769 +#endif
3770 +                       /* if nothing came in last 2 cycles,
3771 +                          it was gap */
3772 +                       if (deltintrtv > TIME_CONST * 2) {
3773 +                               if (last_value) {
3774 +#ifdef DEBUG_SIGNAL
3775 +                                       printk(KERN_DEBUG LIRC_DRIVER_NAME ": GAP\n");
3776 +#endif
3777 +                                       /* simulate signal change */
3778 +                                       add_read_queue(last_value,
3779 +                                                      deltv-
3780 +                                                      deltintrtv);
3781 +                                       last_value = 0;
3782 +                                       last_tv.tv_sec = last_intr_tv.tv_sec;
3783 +                                       last_tv.tv_usec = last_intr_tv.tv_usec;
3784 +                                       deltv = deltintrtv;
3785 +                               }
3786 +                       }
3787 +                       data = 1;
3788 +                       if (data ^ last_value) {
3789 +                               /* deltintrtv > 2*TIME_CONST,
3790 +                                  remember ? */
3791 +                               /* the other case is timeout */
3792 +                               add_read_queue(last_value,
3793 +                                              deltv-TIME_CONST);
3794 +                               last_value = data;
3795 +                               last_tv = curr_tv;
3796 +                               if(last_tv.tv_usec>=TIME_CONST) {
3797 +                                       last_tv.tv_usec-=TIME_CONST;
3798 +                               }
3799 +                               else {
3800 +                                       last_tv.tv_sec--;
3801 +                                       last_tv.tv_usec+=1000000-
3802 +                                               TIME_CONST;
3803 +                               }
3804 +                       }
3805 +                       last_intr_tv = curr_tv;
3806 +                       if (data) {
3807 +                               /* start timer for end of sequence detection */
3808 +                               timerlist.expires = jiffies + IT87_TIMEOUT;
3809 +                               add_timer(&timerlist);
3810 +                       }
3811 +                       outb((inb(io + IT87_CIR_RCR) & ~IT87_CIR_RCR_RXEN) |
3812 +                            IT87_CIR_RCR_RXACT,
3813 +                            io + IT87_CIR_RCR);
3814 +                       if (it87_RXEN_mask) {
3815 +                               outb(inb(io + IT87_CIR_RCR) | IT87_CIR_RCR_RXEN,
3816 +                                    io + IT87_CIR_RCR);
3817 +                       }
3818 +                       fifo--;
3819 +               }
3820 +               while (fifo != 0);
3821 +               spin_unlock_irqrestore(&hardware_lock, hw_flags);
3822 +               spin_unlock_irqrestore(&timer_lock, flags);
3823 +               break;
3824 +
3825 +       default:
3826 +               /* not our irq */
3827 +#ifdef DEBUG_SIGNAL
3828 +               printk(KERN_DEBUG LIRC_DRIVER_NAME
3829 +                      "unknown IRQ (shouldn't happen) !!\n");
3830 +#endif
3831 +               break;
3832 +       }
3833 +}
3834 +
3835 +
3836 +static void send_it87(unsigned long len,
3837 +                     unsigned long stime,
3838 +                     unsigned char send_byte,
3839 +                     unsigned int count_bits)
3840 +{
3841 +        long count = len / stime;
3842 +       long time_left = 0;
3843 +       static unsigned char byte_out = 0;
3844 +
3845 +#ifdef DEBUG_SIGNAL
3846 +       printk(KERN_DEBUG LIRC_DRIVER_NAME
3847 +              "send_it87: len=%ld, sb=%d\n",
3848 +              len,
3849 +              send_byte);
3850 +#endif
3851 +       time_left = (long)len - (long)count * (long)stime;
3852 +       count += ((2 * time_left) / stime);
3853 +       while (count) {
3854 +               long i=0;
3855 +               for (i=0; i<count_bits; i++) {
3856 +                       byte_out = (byte_out << 1) | (send_byte & 1);
3857 +                       it87_bits_in_byte_out++;
3858 +               }
3859 +               if (it87_bits_in_byte_out == 8) {
3860 +#ifdef DEBUG_SIGNAL
3861 +                       printk(KERN_DEBUG LIRC_DRIVER_NAME
3862 +                              "out=0x%x, tsr_txfbc: 0x%x\n",
3863 +                              byte_out,
3864 +                              inb(io + IT87_CIR_TSR) &
3865 +                              IT87_CIR_TSR_TXFBC);
3866 +#endif
3867 +                       while ((inb(io + IT87_CIR_TSR) &
3868 +                               IT87_CIR_TSR_TXFBC) >= IT87_CIR_FIFO_SIZE);
3869 +                       {
3870 +                               unsigned long hw_flags;
3871 +
3872 +                               spin_lock_irqsave(&hardware_lock, hw_flags);
3873 +                               outb(byte_out, io + IT87_CIR_DR);
3874 +                               spin_unlock_irqrestore(&hardware_lock, hw_flags);
3875 +                       }
3876 +                       it87_bits_in_byte_out = 0;
3877 +                       it87_send_counter++;
3878 +                       byte_out = 0;
3879 +               }
3880 +               count--;
3881 +       }
3882 +}
3883 +
3884 +
3885 +/*
3886 +maybe: exchange space and pulse because
3887 +it8705 only modulates 0-bits
3888 +*/
3889 +
3890 +
3891 +static void send_space(unsigned long len)
3892 +{
3893 +       send_it87(len,
3894 +                 TIME_CONST,
3895 +                 IT87_CIR_SPACE,
3896 +                 IT87_CIR_BAUDRATE_DIVISOR);
3897 +}
3898 +
3899 +static void send_pulse(unsigned long len)
3900 +{
3901 +       send_it87(len,
3902 +                 TIME_CONST,
3903 +                 IT87_CIR_PULSE,
3904 +                 IT87_CIR_BAUDRATE_DIVISOR);
3905 +}
3906 +
3907 +
3908 +static void init_send()
3909 +{
3910 +       unsigned long flags;
3911 +
3912 +       spin_lock_irqsave(&hardware_lock, flags);
3913 +       /* RXEN=0: receiver disable */
3914 +       it87_RXEN_mask = 0;
3915 +       outb(inb(io + IT87_CIR_RCR) & ~IT87_CIR_RCR_RXEN,
3916 +            io + IT87_CIR_RCR);
3917 +       spin_unlock_irqrestore(&hardware_lock, flags);
3918 +       it87_bits_in_byte_out = 0;
3919 +       it87_send_counter = 0;
3920 +}
3921 +
3922 +
3923 +static void terminate_send(unsigned long len)
3924 +{
3925 +       unsigned long flags;
3926 +       unsigned long last = 0;
3927 +
3928 +       last = it87_send_counter;
3929 +       /* make sure all necessary data has been sent */
3930 +       while (last == it87_send_counter)
3931 +               send_space(len);
3932 +       /* wait until all data sent */
3933 +       while ((inb(io + IT87_CIR_TSR) & IT87_CIR_TSR_TXFBC) != 0);
3934 +       /* then reenable receiver */
3935 +       spin_lock_irqsave(&hardware_lock, flags);
3936 +       it87_RXEN_mask = IT87_CIR_RCR_RXEN;
3937 +       outb(inb(io + IT87_CIR_RCR) | IT87_CIR_RCR_RXEN,
3938 +            io + IT87_CIR_RCR);
3939 +       spin_unlock_irqrestore(&hardware_lock, flags);
3940 +}
3941 +
3942 +
3943 +static int init_hardware(void)
3944 +{
3945 +       unsigned long flags;
3946 +       unsigned char it87_rcr = 0;
3947 +
3948 +       spin_lock_irqsave(&hardware_lock, flags);
3949 +       /* init cir-port */
3950 +       /* enable r/w-access to Baudrate-Register */
3951 +       outb(IT87_CIR_IER_BR, io + IT87_CIR_IER);
3952 +       outb(IT87_CIR_BAUDRATE_DIVISOR % 0x100, io+IT87_CIR_BDLR);
3953 +       outb(IT87_CIR_BAUDRATE_DIVISOR / 0x100, io+IT87_CIR_BDHR);
3954 +       /* Baudrate Register off, define IRQs: Input only */
3955 +       outb(IT87_CIR_IER_IEC | IT87_CIR_IER_RDAIE, io + IT87_CIR_IER);
3956 +       /* RX: HCFS=0, RXDCR = 001b (35,6..40,3 kHz), RXEN=1 */
3957 +       it87_rcr = (IT87_CIR_RCR_RXEN & it87_RXEN_mask) | 0x1;
3958 +       if (it87_enable_demodulator)
3959 +               it87_rcr |= IT87_CIR_RCR_RXEND;
3960 +       outb(it87_rcr, io + IT87_CIR_RCR);
3961 +       /* TX: 38kHz, 13,3us (pulse-width */
3962 +       outb(((it87_freq - IT87_CIR_FREQ_MIN) << 3) | 0x06,
3963 +            io + IT87_CIR_TCR2);
3964 +       spin_unlock_irqrestore(&hardware_lock, flags);
3965 +       return 0;
3966 +}
3967 +
3968 +
3969 +static void drop_hardware(void)
3970 +{
3971 +       unsigned long flags;
3972 +
3973 +       spin_lock_irqsave(&hardware_lock, flags);
3974 +       disable_irq(irq);
3975 +       /* receiver disable */
3976 +       it87_RXEN_mask = 0;
3977 +       outb(0x1, io + IT87_CIR_RCR);
3978 +       /* turn off irqs */
3979 +       outb(0, io + IT87_CIR_IER);
3980 +       /* fifo clear */
3981 +        outb(IT87_CIR_TCR1_FIFOCLR, io+IT87_CIR_TCR1);
3982 +        /* reset */
3983 +        outb(IT87_CIR_IER_RESET, io+IT87_CIR_IER);
3984 +       enable_irq(irq);
3985 +       spin_unlock_irqrestore(&hardware_lock, flags);
3986 +}
3987 +
3988 +
3989 +static unsigned char it87_read(unsigned char port)
3990 +{
3991 +       outb(port, IT87_ADRPORT);
3992 +       return inb(IT87_DATAPORT);
3993 +}
3994 +
3995 +
3996 +static void it87_write(unsigned char port,
3997 +                      unsigned char data)
3998 +{
3999 +       outb(port, IT87_ADRPORT);
4000 +       outb(data, IT87_DATAPORT);
4001 +}
4002 +
4003 +
4004 +/* SECTION: Initialisation */
4005 +
4006 +static int init_port(void)
4007 +{
4008 +       int retval = 0;
4009 +
4010 +       unsigned char init_bytes[4] = {IT87_INIT};
4011 +       unsigned char it87_chipid = 0;
4012 +       unsigned char ldn = 0;
4013 +       unsigned int  it87_io = 0;
4014 +       unsigned int  it87_irq = 0;
4015 +
4016 +       /* Enter MB PnP Mode */
4017 +       outb(init_bytes[0], IT87_ADRPORT);
4018 +       outb(init_bytes[1], IT87_ADRPORT);
4019 +       outb(init_bytes[2], IT87_ADRPORT);
4020 +       outb(init_bytes[3], IT87_ADRPORT);
4021 +
4022 +       /* 8712 or 8705 ? */
4023 +       it87_chipid = it87_read(IT87_CHIP_ID1);
4024 +       if (it87_chipid != 0x87) {
4025 +               retval = -ENXIO;
4026 +               return retval;
4027 +       }
4028 +       it87_chipid = it87_read(IT87_CHIP_ID2);
4029 +       if ((it87_chipid != 0x12) && (it87_chipid != 0x05)) {
4030 +               printk(KERN_INFO LIRC_DRIVER_NAME
4031 +                      ": no IT8705/12 found, exiting..\n");
4032 +               retval = -ENXIO;
4033 +               return retval;
4034 +       }
4035 +       printk(KERN_INFO LIRC_DRIVER_NAME
4036 +              ": found IT87%.2x.\n",
4037 +              it87_chipid);
4038 +
4039 +       /* get I/O-Port and IRQ */
4040 +       if (it87_chipid == 0x12)
4041 +               ldn = IT8712_CIR_LDN;
4042 +       else
4043 +               ldn = IT8705_CIR_LDN;
4044 +       it87_write(IT87_LDN, ldn);
4045 +
4046 +       it87_io = it87_read(IT87_CIR_BASE_MSB) * 256 +
4047 +               it87_read(IT87_CIR_BASE_LSB);
4048 +       if (it87_io == 0) {
4049 +               if (io == 0)
4050 +                       io = IT87_CIR_DEFAULT_IOBASE;
4051 +               printk(KERN_INFO LIRC_DRIVER_NAME
4052 +                      ": set default io 0x%x\n",
4053 +                      io);
4054 +               it87_write(IT87_CIR_BASE_MSB, io / 0x100);
4055 +               it87_write(IT87_CIR_BASE_LSB, io % 0x100);
4056 +       }
4057 +       else
4058 +               io = it87_io;
4059 +
4060 +       it87_irq = it87_read(IT87_CIR_IRQ);
4061 +       if (it87_irq == 0) {
4062 +               if (irq == 0)
4063 +                       irq = IT87_CIR_DEFAULT_IRQ;
4064 +               printk(KERN_INFO LIRC_DRIVER_NAME
4065 +                      ": set default irq 0x%x\n",
4066 +                      irq);
4067 +               it87_write(IT87_CIR_IRQ, irq);
4068 +       }
4069 +       else
4070 +               irq = it87_irq;
4071 +
4072 +       {
4073 +               unsigned long hw_flags;
4074 +
4075 +               spin_lock_irqsave(&hardware_lock, hw_flags);
4076 +               /* reset */
4077 +               outb(IT87_CIR_IER_RESET, io+IT87_CIR_IER);
4078 +               /* fifo clear */
4079 +               outb(IT87_CIR_TCR1_FIFOCLR |
4080 +                    /*      IT87_CIR_TCR1_ILE | */
4081 +                    IT87_CIR_TCR1_TXRLE |
4082 +                    IT87_CIR_TCR1_TXENDF, io+IT87_CIR_TCR1);
4083 +               spin_unlock_irqrestore(&hardware_lock, hw_flags);
4084 +       }
4085 +
4086 +       /* get I/O port access and IRQ line */
4087 +       retval = request_region(io, 8, LIRC_DRIVER_NAME);
4088 +
4089 +       if (!retval) {
4090 +               printk(KERN_ERR LIRC_DRIVER_NAME
4091 +                      ": Unable to reserve IO region for LIRC IT87. Port 0x%.4x already in use.\n",
4092 +                      io);
4093 +               /* Leaving MB PnP Mode */
4094 +               it87_write(IT87_CFGCTRL, 0x2);
4095 +               return retval;
4096 +       }
4097 +
4098 +       /* activate CIR-Device */
4099 +       it87_write(IT87_CIR_ACT, 0x1);
4100 +
4101 +       /* Leaving MB PnP Mode */
4102 +       it87_write(IT87_CFGCTRL, 0x2);
4103 +
4104 +       retval = request_irq(irq, it87_interrupt, 0 /*SA_INTERRUPT*/,
4105 +                            LIRC_DRIVER_NAME, NULL);
4106 +       if (retval < 0) {
4107 +               printk(KERN_ERR LIRC_DRIVER_NAME
4108 +                      ": IRQ %d already in use.\n",
4109 +                      irq);
4110 +               return retval;
4111 +       }
4112 +
4113 +       printk(KERN_INFO LIRC_DRIVER_NAME
4114 +              ": I/O port 0x%.4x, IRQ %d.\n",
4115 +              io,
4116 +              irq);
4117 +
4118 +       request_region(io, 8, LIRC_DRIVER_NAME);
4119 +       init_timer(&timerlist);
4120 +       timerlist.function = it87_timeout;
4121 +       timerlist.data = 0xabadcafe;
4122 +
4123 +       return 0;
4124 +}
4125 +
4126 +
4127 +static void drop_port(void)
4128 +{
4129 +/*
4130 +        unsigned char init_bytes[4] = {IT87_INIT};
4131 +
4132 +        / * Enter MB PnP Mode * /
4133 +        outb(init_bytes[0], IT87_ADRPORT);
4134 +        outb(init_bytes[1], IT87_ADRPORT);
4135 +        outb(init_bytes[2], IT87_ADRPORT);
4136 +        outb(init_bytes[3], IT87_ADRPORT);
4137 +
4138 +        / * deactivate CIR-Device * /
4139 +        it87_write(IT87_CIR_ACT, 0x0);
4140 +
4141 +        / * Leaving MB PnP Mode * /
4142 +        it87_write(IT87_CFGCTRL, 0x2);
4143 +*/
4144 +
4145 +       del_timer_sync(&timerlist);
4146 +       free_irq(irq, NULL);
4147 +       release_region(io, 8);
4148 +}
4149 +
4150 +
4151 +int init_lirc_it87(void)
4152 +{
4153 +       int retval;
4154 +
4155 +       init_waitqueue_head(&lirc_read_queue);
4156 +       retval = init_port();
4157 +       if (retval < 0)
4158 +               return retval;
4159 +       init_hardware();
4160 +       printk(KERN_INFO LIRC_DRIVER_NAME
4161 +              ": Installed.\n");
4162 +       return 0;
4163 +}
4164 +
4165 +
4166 +MODULE_AUTHOR("Hans-Günter Lütke Uphues");
4167 +MODULE_DESCRIPTION("LIRC driver for ITE IT8712/IT8705 CIR port");
4168 +MODULE_PARM(io, "i");
4169 +MODULE_PARM_DESC(io,
4170 +                "I/O base address (default: 0x310)");
4171 +MODULE_PARM(irq, "i");
4172 +MODULE_PARM_DESC(irq,
4173 +                "Interrupt (1,3-12) (default: 7)");
4174 +MODULE_PARM(it87_enable_demodulator, "i");
4175 +MODULE_PARM_DESC(it87_enable_demodulator,
4176 +                "Receiver demodulator enable/disable (1/0), default: 0");
4177 +MODULE_LICENSE("GPL");
4178 +
4179 +static int __init lirc_it87_init(void)
4180 +{
4181 +       int retval;
4182 +
4183 +       retval=init_chrdev();
4184 +       if(retval < 0)
4185 +               return retval;
4186 +       retval = init_lirc_it87();
4187 +       if (retval) {
4188 +               drop_chrdev();
4189 +               return retval;
4190 +       }
4191 +       return 0;
4192 +}
4193 +
4194 +
4195 +static void __exit lirc_it87_exit(void)
4196 +{
4197 +       drop_hardware();
4198 +       drop_chrdev();
4199 +       drop_port();
4200 +       printk(KERN_INFO LIRC_DRIVER_NAME ": Uninstalled.\n");
4201 +}
4202 +
4203 +module_init(lirc_it87_init);
4204 +module_exit(lirc_it87_exit);
4205 +
4206 +/*
4207 + * Overrides for Emacs so that we follow Linus's tabbing style.
4208 + * ---------------------------------------------------------------------------
4209 + * Local variables:
4210 + * c-basic-offset: 8
4211 + * End:
4212 + */
4213 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_it87.h linux-2.6.5/drivers/char/lirc/lirc_it87.h
4214 --- linux-2.6.5.orig/drivers/char/lirc/lirc_it87.h      1970-01-01 01:00:00.000000000 +0100
4215 +++ linux-2.6.5/drivers/char/lirc/lirc_it87.h   2004-04-04 14:05:29.417627112 +0200
4216 @@ -0,0 +1,116 @@
4217 +/* lirc_it87.h */
4218 +/* SECTION: Definitions */
4219 +
4220 +/********************************* ITE IT87xx ************************/
4221 +
4222 +/* based on the following documentation from ITE:
4223 +   a) IT8712F Preliminary CIR Programming Guide V0.1
4224 +   b) IT8705F Simple LPC I/O Preliminary Specifiction V0.3
4225 +   c) IT8712F EC-LPC I/O Preliminary Specification V0.5
4226 +*/
4227 +
4228 +/* IT8712/05 Ports: */
4229 +#define IT87_ADRPORT      0x2e
4230 +#define IT87_DATAPORT     0x2f
4231 +#define IT87_INIT         0x87, 0x01, 0x55, 0x55
4232 +
4233 +/* alternate Ports: */
4234 +/*
4235 +#define IT87_ADRPORT      0x4e
4236 +#define IT87_DATAPORT     0x4f
4237 +#define IT87_INIT         0x87, 0x01, 0x55, 0xaa
4238 + */
4239 +
4240 +/* IT8712/05 Registers */
4241 +#define IT87_CFGCTRL      0x2
4242 +#define IT87_LDN          0x7
4243 +#define IT87_CHIP_ID1     0x20
4244 +#define IT87_CHIP_ID2     0x21
4245 +#define IT87_CFG_VERSION  0x22
4246 +#define IT87_SWSUSPEND    0x23
4247 +
4248 +#define IT8712_CIR_LDN    0xa
4249 +#define IT8705_CIR_LDN    0x7
4250 +
4251 +/* CIR Configuration Registers: */
4252 +#define IT87_CIR_ACT      0x30
4253 +#define IT87_CIR_BASE_MSB 0x60
4254 +#define IT87_CIR_BASE_LSB 0x61
4255 +#define IT87_CIR_IRQ      0x70
4256 +#define IT87_CIR_CONFIG   0xf0
4257 +
4258 +/* List of IT87_CIR registers: offset to BaseAddr */
4259 +#define IT87_CIR_DR   0
4260 +#define IT87_CIR_IER  1
4261 +#define IT87_CIR_RCR  2
4262 +#define IT87_CIR_TCR1 3
4263 +#define IT87_CIR_TCR2 4
4264 +#define IT87_CIR_TSR  5
4265 +#define IT87_CIR_RSR  6
4266 +#define IT87_CIR_BDLR 5
4267 +#define IT87_CIR_BDHR 6
4268 +#define IT87_CIR_IIR  7
4269 +
4270 +/* Bit Definitionen */
4271 +/* IER: */
4272 +#define IT87_CIR_IER_TM_EN   0x80
4273 +#define IT87_CIR_IER_RESEVED 0x40
4274 +#define IT87_CIR_IER_RESET   0x20
4275 +#define IT87_CIR_IER_BR      0x10
4276 +#define IT87_CIR_IER_IEC     0x8
4277 +#define IT87_CIR_IER_RFOIE   0x4
4278 +#define IT87_CIR_IER_RDAIE   0x2
4279 +#define IT87_CIR_IER_TLDLIE  0x1
4280 +
4281 +/* RCR: */
4282 +#define IT87_CIR_RCR_RDWOS  0x80
4283 +#define IT87_CIR_RCR_HCFS   0x40
4284 +#define IT87_CIR_RCR_RXEN   0x20
4285 +#define IT87_CIR_RCR_RXEND  0x10
4286 +#define IT87_CIR_RCR_RXACT  0x8
4287 +#define IT87_CIR_RCR_RXDCR  0x7
4288 +
4289 +/* TCR1: */
4290 +#define IT87_CIR_TCR1_FIFOCLR 0x80
4291 +#define IT87_CIR_TCR1_ILE     0x40
4292 +#define IT87_CIR_TCR1_FIFOTL  0x30
4293 +#define IT87_CIR_TCR1_TXRLE   0x8
4294 +#define IT87_CIR_TCR1_TXENDF  0x4
4295 +#define IT87_CIR_TCR1_TXMPM   0x3
4296 +
4297 +/* TCR2: */
4298 +#define IT87_CIR_TCR2_CFQ   0xf8
4299 +#define IT87_CIR_TCR2_TXMPW 0x7
4300 +
4301 +/* TSR: */
4302 +#define IT87_CIR_TSR_RESERVED 0xc0
4303 +#define IT87_CIR_TSR_TXFBC    0x3f
4304 +
4305 +/* RSR: */
4306 +#define IT87_CIR_RSR_RXFTO    0x80
4307 +#define IT87_CIR_RSR_RESERVED 0x40
4308 +#define IT87_CIR_RSR_RXFBC    0x3f
4309 +
4310 +/* IIR: */
4311 +#define IT87_CIR_IIR_RESERVED 0xf8
4312 +#define IT87_CIR_IIR_IID      0x6
4313 +#define IT87_CIR_IIR_IIP      0x1
4314 +
4315 +/* TM: */
4316 +#define IT87_CIR_TM_IL_SEL    0x80
4317 +#define IT87_CIR_TM_RESERVED  0x40
4318 +#define IT87_CIR_TM_TM_REG    0x3f
4319 +
4320 +#define IT87_CIR_FIFO_SIZE 32
4321 +
4322 +/* Baudratedivisor for IT87: power of 2: only 1,2,4 or 8) */
4323 +#define IT87_CIR_BAUDRATE_DIVISOR 0x1
4324 +#define IT87_CIR_DEFAULT_IOBASE 0x310
4325 +#define IT87_CIR_DEFAULT_IRQ    0x7
4326 +#define IT87_CIR_SPACE 0x00
4327 +#define IT87_CIR_PULSE 0xff
4328 +#define IT87_CIR_FREQ_MIN 27
4329 +#define IT87_CIR_FREQ_MAX 58
4330 +#define TIME_CONST (IT87_CIR_BAUDRATE_DIVISOR * 8000000ul / 115200ul)
4331 +
4332 +/********************************* ITE IT87xx ************************/
4333 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_mceusb.c linux-2.6.5/drivers/char/lirc/lirc_mceusb.c
4334 --- linux-2.6.5.orig/drivers/char/lirc/lirc_mceusb.c    1970-01-01 01:00:00.000000000 +0100
4335 +++ linux-2.6.5/drivers/char/lirc/lirc_mceusb.c 2004-04-04 14:05:29.422626352 +0200
4336 @@ -0,0 +1,1530 @@
4337 +/*
4338 + * USB Microsoft IR Transceiver driver - 0.1
4339 + *
4340 + * Copyright (c) 2003-2004 Dan Conti (dconti@acm.wwu.edu)
4341 + *
4342 + * This driver is based on the USB skeleton driver packaged with the
4343 + * kernel, and the notice from that package has been retained below.
4344 + *
4345 + * The Microsoft IR Transceiver is a neat little IR receiver with two
4346 + * emitters on it designed for Windows Media Center. This driver might
4347 + * work for all media center remotes, but I have only tested it with
4348 + * the philips model. The first revision of this driver only supports
4349 + * the receive function - the transmit function will be much more
4350 + * tricky due to the nature of the hardware. Microsoft chose to build
4351 + * this device inexpensively, therefore making it extra dumb.  There
4352 + * is no interrupt endpoint on this device; all usb traffic happens
4353 + * over two bulk endpoints. As a result of this, poll() for this
4354 + * device is an actual hardware poll (instead of a receive queue
4355 + * check) and is rather expensive.
4356 + *
4357 + * This driver is structured in three basic layers
4358 + *  - lower  - interface with the usb device and manage usb data
4359 + *  - middle - api to convert usb data into mode2 and provide this in
4360 + *    _read calls
4361 + *  - mceusb_* - linux driver interface
4362 + *
4363 + * The key routines are as follows:
4364 + *  msir_fetch_more_data - this reads incoming data, strips off the
4365 + *                         start codes the ir receiver places on them,
4366 + *                         and dumps it in an * internal buffer
4367 + *  msir_generate_mode2  - this takes the above data, depacketizes it,
4368 + *                         and generates mode2 data to feed out
4369 + *                         through read calls
4370 + *
4371 + *
4372 + * All trademarks property of their respective owners.
4373 + *
4374 + * 2003_11_11 - Restructured to minimalize code interpretation in the
4375 + *              driver. The normal use case will be with lirc.
4376 + *
4377 + * 2004_01_01 - Removed all code interpretation. Generate mode2 data
4378 + *              for passing off to lirc. Cleanup
4379 + *
4380 + * 2004_01_04 - Removed devfs handle. Put in a temporary workaround
4381 + *              for a known issue where repeats generate two
4382 + *              sequential spaces * (last_was_repeat_gap)
4383 + *
4384 + * TODO
4385 + *   - Fix up minor number, registration of major/minor with usb subsystem
4386 + *   - Fix up random EINTR being sent
4387 + *   - Fix problem where third key in a repeat sequence is randomly truncated
4388 + *
4389 + */
4390 +/*
4391 + * USB Skeleton driver - 0.6
4392 + *
4393 + * Copyright (c) 2001 Greg Kroah-Hartman (greg@kroah.com)
4394 + *
4395 + *     This program is free software; you can redistribute it and/or
4396 + *     modify it under the terms of the GNU General Public License as
4397 + *     published by the Free Software Foundation; either version 2 of
4398 + *     the License, or (at your option) any later version.
4399 + *
4400 + *
4401 + * This driver is to be used as a skeleton driver to be able to create a
4402 + * USB driver quickly.  The design of it is based on the usb-serial and
4403 + * dc2xx drivers.
4404 + *
4405 + * Thanks to Oliver Neukum and David Brownell for their help in debugging
4406 + * this driver.
4407 + *
4408 + * TODO:
4409 + *     - fix urb->status race condition in write sequence
4410 + *     - move minor_table to a dynamic list.
4411 + *
4412 + * History:
4413 + *
4414 + * 2001_11_05 - 0.6 - fix minor locking problem in skel_disconnect.
4415 + *                     Thanks to Pete Zaitcev for the fix.
4416 + * 2001_09_04 - 0.5 - fix devfs bug in skel_disconnect. Thanks to wim delvaux
4417 + * 2001_08_21 - 0.4 - more small bug fixes.
4418 + * 2001_05_29 - 0.3 - more bug fixes based on review from linux-usb-devel
4419 + * 2001_05_24 - 0.2 - bug fixes based on review from linux-usb-devel people
4420 + * 2001_05_01 - 0.1 - first version
4421 + *
4422 + */
4423 +
4424 +#include <linux/config.h>
4425 +#include <linux/kernel.h>
4426 +#include <linux/sched.h>
4427 +#include <linux/wait.h>
4428 +#include <linux/signal.h>
4429 +#include <linux/errno.h>
4430 +#include <linux/poll.h>
4431 +#include <linux/init.h>
4432 +#include <linux/slab.h>
4433 +#include <linux/fcntl.h>
4434 +#include <linux/module.h>
4435 +#include <linux/spinlock.h>
4436 +#include <linux/list.h>
4437 +#include <linux/smp_lock.h>
4438 +#include <linux/usb.h>
4439 +
4440 +#ifdef CONFIG_USB_DEBUG
4441 +       static int debug = 1;
4442 +#else
4443 +       static int debug = 1;
4444 +#endif
4445 +
4446 +#include <linux/lirc.h>
4447 +#include "drivers/lirc_dev/lirc_dev.h"
4448 +
4449 +/* Version Information */
4450 +#define DRIVER_VERSION "v0.1"
4451 +#define DRIVER_AUTHOR "Dan Conti, dconti@acm.wwu.edu"
4452 +#define DRIVER_DESC "USB Microsoft IR Transceiver Driver"
4453 +
4454 +/* Module paramaters */
4455 +MODULE_PARM(debug, "i");
4456 +MODULE_PARM_DESC(debug, "Debug enabled or not");
4457 +
4458 +/* Define these values to match your device */
4459 +#define USB_MCEUSB_VENDOR_ID   0x045e
4460 +#define USB_MCEUSB_PRODUCT_ID  0x006d
4461 +
4462 +/* table of devices that work with this driver */
4463 +static struct usb_device_id mceusb_table [] = {
4464 +       { USB_DEVICE(USB_MCEUSB_VENDOR_ID, USB_MCEUSB_PRODUCT_ID) },
4465 +       { }                                     /* Terminating entry */
4466 +};
4467 +
4468 +MODULE_DEVICE_TABLE (usb, mceusb_table);
4469 +
4470 +/* XXX TODO, 244 is likely unused but not reserved */
4471 +/* Get a minor range for your devices from the usb maintainer */
4472 +#define USB_MCEUSB_MINOR_BASE  244
4473 +
4474 +
4475 +/* we can have up to this number of device plugged in at once */
4476 +#define MAX_DEVICES            16
4477 +
4478 +/* Structure to hold all of our device specific stuff */
4479 +struct usb_skel             {
4480 +       /* save off the usb device pointer */
4481 +       struct usb_device *         udev;
4482 +       /* the interface for this device */
4483 +       struct usb_interface *  interface;
4484 +       /* the starting minor number for this device */
4485 +       unsigned char   minor;
4486 +       /* the number of ports this device has */
4487 +       unsigned char   num_ports;
4488 +       /* number of interrupt in endpoints we have */
4489 +       char            num_interrupt_in;
4490 +       /* number of bulk in endpoints we have */
4491 +       char            num_bulk_in;
4492 +       /* number of bulk out endpoints we have */
4493 +       char            num_bulk_out;
4494 +
4495 +       /* the buffer to receive data */
4496 +       unsigned char *    bulk_in_buffer;
4497 +       /* the size of the receive buffer */
4498 +       int                bulk_in_size;
4499 +       /* the address of the bulk in endpoint */
4500 +       __u8               bulk_in_endpointAddr;
4501 +
4502 +       /* the buffer to send data */
4503 +       unsigned char *    bulk_out_buffer;
4504 +       /* the size of the send buffer */
4505 +       int                bulk_out_size;
4506 +       /* the urb used to send data */
4507 +       struct urb *       write_urb;
4508 +       /* the address of the bulk out endpoint */
4509 +       __u8               bulk_out_endpointAddr;
4510 +
4511 +       wait_queue_head_t  wait_q;      /* for timeouts */
4512 +       int                open_count;  /* number of times this port
4513 +                                        * has been opened */
4514 +       struct semaphore   sem;         /* locks this structure */
4515 +
4516 +       struct lirc_plugin* plugin;
4517 +
4518 +       /* Used in converting to mode2 and storing */
4519 +        /* buffer for the mode2 data, since lirc reads 4bytes */
4520 +       int    mode2_data[256];
4521 +       int    mode2_idx;               /* read index */
4522 +       int    mode2_count;             /* words available (i.e. write
4523 +                                        * index) */
4524 +       int    mode2_partial_pkt_size;
4525 +       int    mode2_once;
4526 +
4527 +       /* Used for storing preprocessed usb data before converting to mode2*/
4528 +       char   usb_dbuffer[1024];
4529 +       int    usb_dstart;
4530 +       int    usb_dcount;
4531 +       int    usb_valid_bytes_in_bulk_buffer;
4532 +
4533 +       /* Set to 1 if the last value we adjusted was a repeat gap; we
4534 +        * need to hold this value around until we process a lead
4535 +        * space on the repeat code, otherwise we pass off two
4536 +        * sequential spaces */
4537 +       int    last_was_repeat_gap;
4538 +};
4539 +
4540 +/* driver api */
4541 +static ssize_t mceusb_read     (struct file *file, char *buffer,
4542 +                                size_t count, loff_t *ppos);
4543 +static ssize_t mceusb_write    (struct file *file, const char *buffer,
4544 +                                size_t count, loff_t *ppos);
4545 +static unsigned int mceusb_poll (struct file* file, poll_table* wait);
4546 +
4547 +static int mceusb_open         (struct inode *inode, struct file *file);
4548 +static int mceusb_release      (struct inode *inode, struct file *file);
4549 +
4550 +static void * mceusb_probe     (struct usb_device *dev, unsigned int ifnum,
4551 +                                const struct usb_device_id *id);
4552 +static void *mceusb_disconnect (struct usb_interface *intf);
4553 +
4554 +static void mceusb_write_bulk_callback (struct urb *urb);
4555 +
4556 +/* lower level api */
4557 +static int msir_fetch_more_data( struct usb_skel* dev, int dont_block );
4558 +static int msir_read_from_buffer( struct usb_skel* dev, char* buffer, int len );
4559 +static int msir_mark_as_read( struct usb_skel* dev, int count );
4560 +static int msir_available_data( struct usb_skel* dev );
4561 +
4562 +/* middle */
4563 +static int msir_generate_mode2( struct usb_skel* dev, signed char* usb_data,
4564 +                               int bytecount );
4565 +static int msir_copy_mode2( struct usb_skel* dev, int* mode2_data, int count );
4566 +static int msir_available_mode2( struct usb_skel* dev );
4567 +
4568 +/* helper functions */
4569 +static void msir_cleanup( struct usb_skel* dev );
4570 +static int set_use_inc(void* data);
4571 +static void set_use_dec(void* data);
4572 +
4573 +/* array of pointers to our devices that are currently connected */
4574 +static struct usb_skel         *minor_table[MAX_DEVICES];
4575 +
4576 +/* lock to protect the minor_table structure */
4577 +static DECLARE_MUTEX (minor_table_mutex);
4578 +
4579 +/*
4580 + * File operations needed when we register this driver.
4581 + * This assumes that this driver NEEDS file operations,
4582 + * of course, which means that the driver is expected
4583 + * to have a node in the /dev directory. If the USB
4584 + * device were for a network interface then the driver
4585 + * would use "struct net_driver" instead, and a serial
4586 + * device would use "struct tty_driver".
4587 + */
4588 +static struct file_operations mceusb_fops = {
4589 +       /*
4590 +        * The owner field is part of the module-locking
4591 +        * mechanism. The idea is that the kernel knows
4592 +        * which module to increment the use-counter of
4593 +        * BEFORE it calls the device's open() function.
4594 +        * This also means that the kernel can decrement
4595 +        * the use-counter again before calling release()
4596 +        * or should the open() function fail.
4597 +        *
4598 +        * Not all device structures have an "owner" field
4599 +        * yet. "struct file_operations" and "struct net_device"
4600 +        * do, while "struct tty_driver" does not. If the struct
4601 +        * has an "owner" field, then initialize it to the value
4602 +        * THIS_MODULE and the kernel will handle all module
4603 +        * locking for you automatically. Otherwise, you must
4604 +        * increment the use-counter in the open() function
4605 +        * and decrement it again in the release() function
4606 +        * yourself.
4607 +        */
4608 +       owner:          THIS_MODULE,
4609 +
4610 +       read:           mceusb_read,
4611 +       write:          mceusb_write,
4612 +       poll:           mceusb_poll,
4613 +       ioctl:          NULL,
4614 +       open:           mceusb_open,
4615 +       release:        mceusb_release,
4616 +};
4617 +
4618 +
4619 +/* usb specific object needed to register this driver with the usb subsystem */
4620 +static struct usb_driver mceusb_driver = {
4621 +       name:           "ir_transceiver",
4622 +       probe:          mceusb_probe,
4623 +       disconnect:     mceusb_disconnect,
4624 +       fops:           &mceusb_fops,
4625 +       minor:          USB_MCEUSB_MINOR_BASE,
4626 +       id_table:       mceusb_table,
4627 +};
4628 +
4629 +
4630 +/**
4631 + *     usb_mceusb_debug_data
4632 + */
4633 +static inline void usb_mceusb_debug_data (const char *function, int size,
4634 +                                         const unsigned char *data)
4635 +{
4636 +       int i;
4637 +
4638 +       if (!debug)
4639 +               return;
4640 +
4641 +       printk (KERN_DEBUG __FILE__": %s - length = %d, data = ",
4642 +               function, size);
4643 +       for (i = 0; i < size; ++i) {
4644 +               printk ("%.2x ", data[i]);
4645 +       }
4646 +       printk ("\n");
4647 +}
4648 +
4649 +
4650 +/**
4651 + *     mceusb_delete
4652 + */
4653 +static inline void mceusb_delete (struct usb_skel *dev)
4654 +{
4655 +       minor_table[dev->minor] = NULL;
4656 +       if (dev->bulk_in_buffer != NULL)
4657 +               kfree (dev->bulk_in_buffer);
4658 +       if (dev->bulk_out_buffer != NULL)
4659 +               kfree (dev->bulk_out_buffer);
4660 +       if (dev->write_urb != NULL)
4661 +               usb_free_urb (dev->write_urb);
4662 +       kfree (dev);
4663 +}
4664 +
4665 +static void mceusb_setup( struct usb_device *udev )
4666 +{
4667 +       char data[8];
4668 +       int res;
4669 +       memset( data, 0, 8 );
4670 +
4671 +       /* Get Status */
4672 +       res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
4673 +                             USB_REQ_GET_STATUS, USB_DIR_IN,
4674 +                             0, 0, data, 2, HZ * 3);
4675 +
4676 +       /*    res = usb_get_status( udev, 0, 0, data ); */
4677 +       dbg(__FUNCTION__ " res = %d status = 0x%x 0x%x",
4678 +           res, data[0], data[1] );
4679 +
4680 +       /* This is a strange one. They issue a set address to the
4681 +        * device on the receive control pipe and expect a certain
4682 +        * value pair back
4683 +        */
4684 +       memset( data, 0, 8 );
4685 +
4686 +       res = usb_control_msg( udev, usb_rcvctrlpipe(udev, 0),
4687 +                              5, USB_TYPE_VENDOR, 0, 0,
4688 +                              data, 2, HZ * 3 );
4689 +       dbg(__FUNCTION__ " res = %d, devnum = %d", res, udev->devnum);
4690 +       dbg(__FUNCTION__ " data[0] = %d, data[1] = %d", data[0], data[1] );
4691 +
4692 +       /* set feature */
4693 +       res = usb_control_msg( udev, usb_sndctrlpipe(udev, 0),
4694 +                              USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
4695 +                              0xc04e, 0x0000, NULL, 0, HZ * 3 );
4696 +
4697 +       dbg(__FUNCTION__ " res = %d", res);
4698 +
4699 +       /* These two are sent by the windows driver, but stall for
4700 +        * me. I dont have an analyzer on the linux side so i can't
4701 +        * see what is actually different and why * the device takes
4702 +        * issue with them
4703 +        */
4704 +#if 0
4705 +       /* this is some custom control message they send */
4706 +       res = usb_control_msg( udev, usb_sndctrlpipe(udev, 0),
4707 +                              0x04, USB_TYPE_VENDOR,
4708 +                              0x0808, 0x0000, NULL, 0, HZ * 3 );
4709 +
4710 +       dbg(__FUNCTION__ " res = %d", res);
4711 +
4712 +       /* this is another custom control message they send */
4713 +       res = usb_control_msg( udev, usb_sndctrlpipe(udev, 0),
4714 +                              0x02, USB_TYPE_VENDOR,
4715 +                              0x0000, 0x0100, NULL, 0, HZ * 3 );
4716 +
4717 +       dbg(__FUNCTION__ " res = %d", res);
4718 +#endif
4719 +}
4720 +
4721 +/**
4722 + *     mceusb_open
4723 + */
4724 +static int mceusb_open (struct inode *inode, struct file *file)
4725 +{
4726 +       struct usb_skel *dev = NULL;
4727 +       struct usb_device* udev = NULL;
4728 +       int subminor;
4729 +       int retval = 0;
4730 +
4731 +       dbg(__FUNCTION__);
4732 +
4733 +       /* This is a very sucky point. On lirc, we get passed the
4734 +        * minor number of the lirc device, which is totally
4735 +        * retarded. We want to support people opening /dev/usb/msir0
4736 +        * directly though, so try and determine who the hell is
4737 +        * calling us here
4738 +        */
4739 +       if( MAJOR( inode->i_rdev ) != USB_MAJOR )
4740 +       {
4741 +               /* This is the lirc device just passing on the
4742 +                * request. We probably mismatch minor numbers here,
4743 +                * but the lucky fact is that nobody will ever use two
4744 +                * of the exact same remotes with two recievers on one
4745 +                * machine
4746 +                */
4747 +               subminor = 0;
4748 +       } else {
4749 +               subminor = MINOR (inode->i_rdev) - USB_MCEUSB_MINOR_BASE;
4750 +       }
4751 +       if ((subminor < 0) ||
4752 +           (subminor >= MAX_DEVICES)) {
4753 +               dbg("subminor %d", subminor);
4754 +               return -ENODEV;
4755 +       }
4756 +
4757 +       /* Increment our usage count for the module.
4758 +        * This is redundant here, because "struct file_operations"
4759 +        * has an "owner" field. This line is included here soley as
4760 +        * a reference for drivers using lesser structures... ;-)
4761 +        */
4762 +       try_module_get(THIS_MODULE);
4763 +
4764 +       /* lock our minor table and get our local data for this minor */
4765 +       down (&minor_table_mutex);
4766 +       dev = minor_table[subminor];
4767 +       if (dev == NULL) {
4768 +               dbg("dev == NULL");
4769 +               up (&minor_table_mutex);
4770 +               module_put(THIS_MODULE);
4771 +               return -ENODEV;
4772 +       }
4773 +       udev = dev->udev;
4774 +
4775 +       /* lock this device */
4776 +       down (&dev->sem);
4777 +
4778 +       /* unlock the minor table */
4779 +       up (&minor_table_mutex);
4780 +
4781 +       /* increment our usage count for the driver */
4782 +       ++dev->open_count;
4783 +
4784 +       /* save our object in the file's private structure */
4785 +       file->private_data = dev;
4786 +
4787 +       /* init the waitq */
4788 +       init_waitqueue_head( &dev->wait_q );
4789 +
4790 +       /* clear off the first few messages. these look like
4791 +        * calibration or test data, i can't really tell
4792 +        * this also flushes in case we have random ir data queued up
4793 +        */
4794 +       {
4795 +               char junk[64];
4796 +               int partial = 0, retval, i;
4797 +               for( i = 0; i < 40; i++ )
4798 +               {
4799 +                       retval = usb_bulk_msg (udev,
4800 +                                              usb_rcvbulkpipe
4801 +                                              (udev,
4802 +                                               dev->bulk_in_endpointAddr),
4803 +                                              junk, 64,
4804 +                                              &partial, HZ*10);
4805 +               }
4806 +       }
4807 +
4808 +       msir_cleanup( dev );
4809 +
4810 +       /* unlock this device */
4811 +       up (&dev->sem);
4812 +
4813 +       return retval;
4814 +}
4815 +
4816 +
4817 +/**
4818 + *     mceusb_release
4819 + */
4820 +static int mceusb_release (struct inode *inode, struct file *file)
4821 +{
4822 +       struct usb_skel *dev;
4823 +       int retval = 0;
4824 +
4825 +       dev = (struct usb_skel *)file->private_data;
4826 +       if (dev == NULL) {
4827 +               dbg (__FUNCTION__ " - object is NULL");
4828 +               return -ENODEV;
4829 +       }
4830 +
4831 +       dbg(__FUNCTION__ " - minor %d", dev->minor);
4832 +
4833 +       /* lock our minor table */
4834 +       down (&minor_table_mutex);
4835 +
4836 +       /* lock our device */
4837 +       down (&dev->sem);
4838 +
4839 +       if (dev->open_count <= 0) {
4840 +               dbg (__FUNCTION__ " - device not opened");
4841 +               retval = -ENODEV;
4842 +               goto exit_not_opened;
4843 +       }
4844 +
4845 +       if (dev->udev == NULL) {
4846 +               /* the device was unplugged before the file was released */
4847 +               up (&dev->sem);
4848 +               mceusb_delete (dev);
4849 +               up (&minor_table_mutex);
4850 +               module_put(THIS_MODULE);
4851 +               return 0;
4852 +       }
4853 +
4854 +       /* decrement our usage count for the device */
4855 +       --dev->open_count;
4856 +       if (dev->open_count <= 0) {
4857 +               /* shutdown any bulk writes that might be going on */
4858 +               usb_unlink_urb (dev->write_urb);
4859 +               dev->open_count = 0;
4860 +       }
4861 +
4862 +       /* decrement our usage count for the module */
4863 +       module_put(THIS_MODULE);
4864 +
4865 +  exit_not_opened:
4866 +       up (&dev->sem);
4867 +       up (&minor_table_mutex);
4868 +
4869 +       return retval;
4870 +}
4871 +
4872 +static void msir_cleanup( struct usb_skel* dev )
4873 +{
4874 +       memset( dev->bulk_in_buffer, 0, dev->bulk_in_size );
4875 +
4876 +       memset( dev->usb_dbuffer, 0, sizeof(dev->usb_dbuffer) );
4877 +       dev->usb_dstart = 0;
4878 +       dev->usb_dcount = 0;
4879 +       dev->usb_valid_bytes_in_bulk_buffer = 0;
4880 +
4881 +       memset( dev->mode2_data, 0, sizeof(dev->mode2_data) );
4882 +       dev->mode2_partial_pkt_size = 0;
4883 +       dev->mode2_count = 0;
4884 +       dev->mode2_idx = 0;
4885 +       dev->mode2_once = 0;
4886 +       dev->last_was_repeat_gap = 0;
4887 +}
4888 +
4889 +static int set_use_inc(void* data)
4890 +{
4891 +       /*    struct usb_skel* skel = (struct usb_skel*)data; */
4892 +
4893 +       try_module_get(THIS_MODULE);
4894 +       return 0;
4895 +}
4896 +
4897 +static void set_use_dec(void* data)
4898 +{
4899 +       /* check for unplug here */
4900 +       struct usb_skel* dev = (struct usb_skel*) data;
4901 +       if( !dev->udev )
4902 +       {
4903 +               lirc_unregister_plugin( dev->minor );
4904 +               lirc_buffer_free( dev->plugin->rbuf );
4905 +               kfree( dev->plugin->rbuf );
4906 +               kfree( dev->plugin );
4907 +       }
4908 +
4909 +       module_put(THIS_MODULE);
4910 +}
4911 +
4912 +static int msir_available_mode2( struct usb_skel* dev )
4913 +{
4914 +       return dev->mode2_count - dev->last_was_repeat_gap;
4915 +}
4916 +
4917 +static int msir_available_data( struct usb_skel* dev )
4918 +{
4919 +       return dev->usb_dcount;
4920 +}
4921 +
4922 +static int msir_copy_mode2( struct usb_skel* dev, int* mode2_data, int count )
4923 +{
4924 +       int words_to_read = count;
4925 +       //    int words_avail   = dev->mode2_count;
4926 +       int words_avail = msir_available_mode2( dev );
4927 +
4928 +       if( !dev->mode2_once && words_avail )
4929 +       {
4930 +               int space = PULSE_MASK;
4931 +               count--;
4932 +               copy_to_user( mode2_data, &space, 4 );
4933 +               dev->mode2_once = 1;
4934 +
4935 +               if( count )
4936 +               {
4937 +                       mode2_data++;
4938 +               }
4939 +               else
4940 +               {
4941 +                       return 1;
4942 +               }
4943 +       }
4944 +
4945 +       if( !words_avail )
4946 +       {
4947 +               return 0;
4948 +       }
4949 +
4950 +       if( words_to_read > words_avail )
4951 +       {
4952 +               words_to_read = words_avail;
4953 +       }
4954 +
4955 +       dbg(__FUNCTION__ " dev->mode2_count %d, dev->mode2_idx %d",
4956 +           dev->mode2_count, dev->mode2_idx);
4957 +       dbg(__FUNCTION__ " words_avail %d words_to_read %d",
4958 +           words_avail, words_to_read);
4959 +       copy_to_user( mode2_data, &( dev->mode2_data[dev->mode2_idx] ),
4960 +                     words_to_read<<2 );
4961 +       dbg(__FUNCTION__ " would copy_to_user() %d w", words_to_read);
4962 +
4963 +       dev->mode2_idx += words_to_read;
4964 +       dev->mode2_count -= words_to_read;
4965 +
4966 +       if( dev->mode2_count == 0 )
4967 +       {
4968 +               dev->mode2_idx = 0;
4969 +       }
4970 +       else if( dev->mode2_count == 1 && dev->last_was_repeat_gap )
4971 +       {
4972 +               // shift down the repeat gap and map it up to a
4973 +               // lirc-acceptable value
4974 +               dev->mode2_data[0] = dev->mode2_data[dev->mode2_idx];
4975 +               if( dev->mode2_data[0] >= 60000 &&
4976 +                   dev->mode2_data[0] <= 70000 )
4977 +                       dev->mode2_data[0] = 95000;
4978 +               //printk(__FUNCTION__ " shifting value %d down from %d prev %d\n", dev->mode2_data[0], dev->mode2_idx,
4979 +               //    dev->mode2_data[dev->mode2_idx-1]);
4980 +               dev->mode2_idx = 0;
4981 +       }
4982 +
4983 +       return words_to_read;
4984 +}
4985 +
4986 +static int msir_read_from_buffer( struct usb_skel* dev, char* buffer, int len )
4987 +{
4988 +       if( len > dev->usb_dcount )
4989 +       {
4990 +               len = dev->usb_dcount;
4991 +       }
4992 +       memcpy( buffer, dev->usb_dbuffer + dev->usb_dstart, len );
4993 +       return len;
4994 +}
4995 +
4996 +static int msir_mark_as_read( struct usb_skel* dev, int count )
4997 +{
4998 +       //    if( count != dev->usb_dcount )
4999 +       //        printk(KERN_INFO __FUNCTION__ " count %d dev->usb_dcount %d dev->usb_dstart %d", count, dev->usb_dcount, dev->usb_dstart );
5000 +       if( count > dev->usb_dcount )
5001 +               count = dev->usb_dcount;
5002 +       dev->usb_dcount -= count;
5003 +       dev->usb_dstart += count;
5004 +
5005 +       if( !dev->usb_dcount )
5006 +               dev->usb_dstart = 0;
5007 +
5008 +       return 0;
5009 +}
5010 +
5011 +
5012 +/*
5013 + * msir_fetch_more_data
5014 + *
5015 + * The goal here is to read in more remote codes from the remote. In
5016 + * the event that the remote isn't sending us anything, the caller
5017 + * will block until a key is pressed (i.e. this performs phys read,
5018 + * filtering, and queueing of data) unless dont_block is set to 1; in
5019 + * this situation, it will perform a few reads and will exit out if it
5020 + * does not see any appropriate data
5021 + *
5022 + * dev->sem should be locked when this function is called - fine grain
5023 + * locking isn't really important here anyways
5024 + *
5025 + * TODO change this to do partials based on term codes, or not always fill
5026 + */
5027 +
5028 +static int msir_fetch_more_data( struct usb_skel* dev, int dont_block )
5029 +{
5030 +       int retries = 0;
5031 +       int count, this_read, partial;
5032 +       int retval;
5033 +       int writeindex, terminators = 0;
5034 +       int bytes_to_read = sizeof(dev->usb_dbuffer) - dev->usb_dcount;
5035 +       signed char* ibuf;
5036 +       int sequential_empty_reads = 0;
5037 +
5038 +       /* special case where we are already full */
5039 +       if( bytes_to_read == 0 )
5040 +               return dev->usb_dcount;
5041 +
5042 +       /* shift down */
5043 +       if( dev->usb_dcount && dev->usb_dstart != 0 )
5044 +       {
5045 +               printk( __FUNCTION__ " shifting %d bytes from %d\n",
5046 +                       dev->usb_dcount, dev->usb_dstart );
5047 +               memcpy( dev->usb_dbuffer, dev->usb_dbuffer + dev->usb_dstart,
5048 +                       dev->usb_dcount );
5049 +       }
5050 +
5051 +       dev->usb_dstart = 0;
5052 +
5053 +       writeindex = dev->usb_dcount;
5054 +
5055 +       count = bytes_to_read;
5056 +
5057 +       ibuf = (signed char*)dev->bulk_in_buffer;
5058 +       if( !dev->usb_valid_bytes_in_bulk_buffer )
5059 +       {
5060 +               memset( ibuf, 0, dev->bulk_in_size );
5061 +       }
5062 +
5063 +#if 0
5064 +       printk( __FUNCTION__ " going to read, dev->usb_dcount %d, bytes_to_read %d vbb %d\n", dev->usb_dcount, bytes_to_read,
5065 +               dev->usb_valid_bytes_in_bulk_buffer );
5066 +#endif
5067 +       /* 8 is the minimum read size */
5068 +       while( count > 8 )
5069 +       {
5070 +               int i, goodbytes = 0;
5071 +
5072 +               /* break out if we were interrupted */
5073 +               if( signal_pending(current) )
5074 +               {
5075 +                       printk( __FUNCTION__ " got signal %ld\n",
5076 +                               current->pending.signal.sig[0]);
5077 +                       return dev->usb_dcount ? dev->usb_dcount : -EINTR;
5078 +               }
5079 +
5080 +               /* or if we were unplugged */
5081 +               if( !dev->udev )
5082 +               {
5083 +                       return -ENODEV;
5084 +               }
5085 +
5086 +               /* or on data issues */
5087 +               if( writeindex == sizeof(dev->usb_dbuffer) )
5088 +               {
5089 +                       printk( __FUNCTION__ " buffer full, returning\n");
5090 +                       return dev->usb_dcount;
5091 +               }
5092 +
5093 +               // always read the maximum
5094 +               this_read = dev->bulk_in_size;
5095 +
5096 +               partial = 0;
5097 +
5098 +               if( dev->usb_valid_bytes_in_bulk_buffer ) {
5099 +                       retval = 0;
5100 +                       this_read = partial = dev->usb_valid_bytes_in_bulk_buffer;
5101 +                       dev->usb_valid_bytes_in_bulk_buffer = 0;
5102 +               } else {
5103 +                       // This call always returns almost immediately
5104 +                       // with data, since this device will always
5105 +                       // provide a 2 byte response on a bulk
5106 +                       // read. Not exactly friendly to the usb bus
5107 +                       // or our load avg. We attempt to compensate
5108 +                       // for this on 2 byte reads below
5109 +
5110 +                       memset( ibuf, 0, dev->bulk_in_size );
5111 +                       retval = usb_bulk_msg (dev->udev,
5112 +                                              usb_rcvbulkpipe
5113 +                                              (dev->udev,
5114 +                                               dev->bulk_in_endpointAddr),
5115 +                                              (unsigned char*)ibuf, this_read,
5116 +                                              &partial, HZ*10);
5117 +               }
5118 +
5119 +               if( retval )
5120 +               {
5121 +                       /* break out on errors */
5122 +                       printk(__FUNCTION__ " got retval %d %d %d",
5123 +                              retval, this_read, partial );
5124 +                       if( retval == USB_ST_DATAOVERRUN && retries < 5 )
5125 +                       {
5126 +                               retries++;
5127 +                               interruptible_sleep_on_timeout
5128 +                                       ( &dev->wait_q, HZ );
5129 +                               continue;
5130 +                       }
5131 +                       else
5132 +                       {
5133 +                               return -EIO;
5134 +                       }
5135 +               } else {
5136 +                       retries = 0;
5137 +               }
5138 +
5139 +               if( partial )
5140 +               {
5141 +                       this_read = partial;
5142 +               }
5143 +
5144 +               /* All packets i've seen start with b1 60. If no data
5145 +                * was actually available, the transceiver still gives
5146 +                * this byte pair back. We only care about actual
5147 +                * codes, so we can safely ignore these 2 byte reads
5148 +                */
5149 +               if( this_read > 2 )
5150 +               {
5151 +#if 0
5152 +                       printk( __FUNCTION__ " read %d bytes partial %d goodbytes %d writeidx %d\n",
5153 +                               this_read, partial, goodbytes, writeindex );
5154 +#endif
5155 +                       sequential_empty_reads = 0;
5156 +                       /* copy from the input buffer to the capture buffer */
5157 +                       for( i = 0; i < this_read; i++ )
5158 +                       {
5159 +                               if( (((unsigned char*)ibuf)[i] == 0xb1) ||
5160 +                                   (ibuf[i] == 0x60) )
5161 +                                       ;
5162 +                               else
5163 +                               {
5164 +                                       if( writeindex == sizeof(dev->usb_dbuffer) )
5165 +                                       {
5166 +                                               /* this can happen in
5167 +                                                * repeats, where
5168 +                                                * basically the bulk
5169 +                                                * buffer is getting
5170 +                                                * spammed and we
5171 +                                                * aren't processing
5172 +                                                * data fast enough
5173 +                                                */
5174 +#if 1
5175 +                                               dev->usb_valid_bytes_in_bulk_buffer = this_read - i;
5176 +                                               memcpy( ibuf, &( ibuf[i] ),
5177 +                                                       dev->usb_valid_bytes_in_bulk_buffer );
5178 +#endif
5179 +                                               break;
5180 +                                       }
5181 +                                       dev->usb_dbuffer[writeindex++] = ibuf[i];
5182 +                                       goodbytes++;
5183 +
5184 +                                       if( ibuf[i] == 0x7f )
5185 +                                       {
5186 +                                               terminators++;
5187 +
5188 +                                               /* This is a bug - we should either get 10 or 15 */
5189 +                                               if( terminators > 15 )
5190 +                                               {
5191 +                                                       dbg("bugbug - terminators %d at %d gb %d", terminators, i, goodbytes );
5192 +                                               } else
5193 +                                                       dbg("terminator %d at %d gb %d", terminators, i, goodbytes );
5194 +                                               dbg("writeindex %d", writeindex);
5195 +                                       }
5196 +                                       else if( terminators )
5197 +                                       {
5198 +                                               if( ((unsigned char*)ibuf)[i] == 128 )
5199 +                                               {
5200 +                                                       /* copy back any remainder and break out */
5201 +                                                       dev->usb_valid_bytes_in_bulk_buffer = this_read - (i + 1);
5202 +                                                       if( dev->usb_valid_bytes_in_bulk_buffer )
5203 +                                                       {
5204 +                                                               memcpy( ibuf, &( ibuf[i+1] ), dev->usb_valid_bytes_in_bulk_buffer );
5205 +                                                       }
5206 +
5207 +                                                       count = 0;
5208 +                                                       break;
5209 +                                               }
5210 +                                               if( terminators == 10 ||
5211 +                                                   terminators == 15 )
5212 +                                                       dbg("post-termination data %d idx %d %d", ibuf[i], dev->usb_dcount, i);
5213 +                                       }
5214 +                               }
5215 +                       }
5216 +                       dev->usb_dcount += goodbytes;
5217 +                       count -= goodbytes;
5218 +               } else {
5219 +                       sequential_empty_reads++;
5220 +
5221 +                       // assume no data
5222 +                       if( dont_block && sequential_empty_reads == 5 )
5223 +                               break;
5224 +
5225 +                       // Try to be nice to the usb bus by sleeping
5226 +                       // for a bit here before going in to the next
5227 +                       // read
5228 +                       interruptible_sleep_on_timeout( &dev->wait_q, 1 );
5229 +               }
5230 +
5231 +       }
5232 +       /* return the number of bytes available now */
5233 +       return dev->usb_dcount;
5234 +}
5235 +
5236 +// layout of data, per Christoph Bartelmus
5237 +// The protocol is:
5238 +// 1 byte: -length of following packet
5239 +// the following bytes of the packet are:
5240 +// negative value:
5241 +//   -(number of time units) of pulse
5242 +// positive value:
5243 +//   (number of time units) of space
5244 +// one time unit is 50us
5245 +
5246 +#define MCE_TIME_UNIT 50
5247 +
5248 +// returns the number of bytes processed from the 'usb_data' array
5249 +static int msir_generate_mode2( struct usb_skel* dev, signed char* usb_data,
5250 +                               int bytecount )
5251 +{
5252 +       int bytes_left_in_packet = 0;
5253 +       int pos = 0;
5254 +       int mode2count = 0;
5255 +       int last_was_pulse = 1;
5256 +       int last_pkt = 0;
5257 +       int split_pkt_size = 0;
5258 +       // XXX no bounds checking here
5259 +       int* mode2_data;
5260 +       int mode2_limit = sizeof( dev->mode2_data ) - dev->mode2_count;
5261 +
5262 +       // If data exists in the buffer, we have to point to the last
5263 +       // item there so we can append consecutive pulse/space
5264 +       // ops. Otherwise, set last_was_pulse 1 (since the first byte
5265 +       // is a pulse, and we want to store in the first array
5266 +       // location
5267 +       if( dev->mode2_count == 0 )
5268 +       {
5269 +               mode2_data = &( dev->mode2_data[0] );
5270 +               last_was_pulse = (dev->mode2_once ? 1 : 0);
5271 +               mode2_data[0] = 0;
5272 +       }
5273 +       else
5274 +       {
5275 +               mode2_data = &( dev->mode2_data[dev->mode2_idx +
5276 +                                               dev->mode2_count - 1] );
5277 +               last_was_pulse = (mode2_data[0] & PULSE_BIT) ? 1 : 0;
5278 +       }
5279 +
5280 +       while( pos < bytecount && !last_pkt &&
5281 +              (mode2_limit > (dev->mode2_count + mode2count)) )
5282 +       {
5283 +               if( dev->mode2_partial_pkt_size )
5284 +               {
5285 +                       bytes_left_in_packet = dev->mode2_partial_pkt_size;
5286 +                       dev->mode2_partial_pkt_size = 0;
5287 +               }
5288 +               else {
5289 +                       bytes_left_in_packet = 128 + usb_data[pos];
5290 +
5291 +                       // XXX out of sync? find the next packet
5292 +                       // header, establish a distance, and fix the
5293 +                       // packet size
5294 +                       if( bytes_left_in_packet > 4 )
5295 +                       {
5296 +                               int i;
5297 +                               for( i = pos + 1; i < pos + 4; i++ )
5298 +                               {
5299 +                                       if( (int)(128 + usb_data[i]) <= 4 )
5300 +                                       {
5301 +                                               bytes_left_in_packet = i - pos;
5302 +                                               break;
5303 +                                       }
5304 +                               }
5305 +                       }
5306 +                       else
5307 +                       {
5308 +                               // otherwise, increment past the header
5309 +                               pos++;
5310 +                       }
5311 +               }
5312 +
5313 +               // special case where we have a terminator at the
5314 +               // start but not at the end of this packet, indicating
5315 +               // potential repeat, or the packet is less than 4
5316 +               // bytes, indicating end also special case a split
5317 +               // starting packet
5318 +               if( pos > 1 && bytes_left_in_packet < 4 )
5319 +               {
5320 +                       // end
5321 +                       last_pkt = 1;
5322 +               }
5323 +               else if( usb_data[pos] == 127 &&
5324 +                        usb_data[pos+bytes_left_in_packet-1] != 127 )
5325 +               {
5326 +                       // the genius ir transciever is blending data
5327 +                       // from the repeat events into a single
5328 +                       // packet. how we handle this is by splitting
5329 +                       // the packet (and truncating the packet size
5330 +                       // value we read), then rewriting a new packet
5331 +                       // header onto the outbound data.  it's
5332 +                       // ultraghetto.
5333 +                       while( usb_data[pos+bytes_left_in_packet-1] != 127 )
5334 +                       {
5335 +                               bytes_left_in_packet--;
5336 +                               split_pkt_size++;
5337 +                       }
5338 +                       // repeat code
5339 +                       last_pkt = 2;
5340 +               }
5341 +               while( bytes_left_in_packet && pos < bytecount )
5342 +               {
5343 +                       int keycode = usb_data[pos];
5344 +                       int pulse = 0;
5345 +
5346 +                       pos++;
5347 +                       if( keycode < 0 )
5348 +                       {
5349 +                               pulse = 1;
5350 +                               keycode += 128;
5351 +                       }
5352 +                       keycode *= MCE_TIME_UNIT;
5353 +
5354 +                       // on a state change, increment the position
5355 +                       // for the output buffer and initialize the
5356 +                       // current spot to 0; otherwise we need to
5357 +                       // concatenate pulse/gap values for lirc to be
5358 +                       // happy
5359 +                       if( pulse != last_was_pulse &&
5360 +                           (mode2count || mode2_data[mode2count]))
5361 +                       {
5362 +                               if( dev->last_was_repeat_gap )
5363 +                               {
5364 +                                       //printk( __FUNCTION__ " transition with lwrg set lastval %d idx1 %d idx2 %d\n",
5365 +                                       //  mode2_data[mode2count],mode2count, dev->mode2_count+dev->mode2_idx-1 );
5366 +                               }
5367 +                               mode2count++;
5368 +                               mode2_data[mode2count] = 0;
5369 +                       }
5370 +
5371 +                       mode2_data[mode2count] += keycode;
5372 +
5373 +                       // Or in the pulse bit, and map all gap
5374 +                       // lengths to a fixed value; this makes lirc
5375 +                       // happy, sort of.
5376 +                       if( pulse ) {
5377 +                               mode2_data[mode2count] |= PULSE_BIT;
5378 +                               dev->last_was_repeat_gap = 0;
5379 +                       }
5380 +
5381 +                       last_was_pulse = pulse;
5382 +                       bytes_left_in_packet--;
5383 +               }
5384 +       }
5385 +
5386 +       // If the last value in the data array is a repeat gap, set
5387 +       // the last_was_repeat_gap flag
5388 +       if( mode2_data[mode2count] > 20000 && mode2_data[mode2count] < 70000 )
5389 +       {
5390 +               // printk(__FUNCTION__ " setting lwrg for val %d idx1 %d idx2 %d\n",
5391 +               //    mode2_data[mode2count], mode2count, dev->mode2_count+dev->mode2_idx-1 );
5392 +               dev->last_was_repeat_gap = 1;
5393 +       } else {
5394 +               dev->last_was_repeat_gap = 0;
5395 +       }
5396 +
5397 +       // this is a bit tricky; we need to change to a counter, but
5398 +       // if we already had data in dev->mode2_data, then byte 0
5399 +       // actually was pre-existing data and shouldn't be counted
5400 +       if( mode2count && !dev->mode2_count )
5401 +       {
5402 +               mode2count++;
5403 +               //        printk(__FUNCTION__ " mode2count++ to %d\n", mode2count);
5404 +       }
5405 +
5406 +       // never lie about how much output we have
5407 +       dev->mode2_count += mode2count;
5408 +
5409 +       if( last_pkt == 1 )
5410 +       {
5411 +               return bytecount;
5412 +       }
5413 +       else
5414 +       {
5415 +               //  note the partial pkt size, and make sure we only claim
5416 +               //  the bytes we processed
5417 +               if( last_pkt == 2 )
5418 +               {
5419 +                       dev->mode2_partial_pkt_size = split_pkt_size;
5420 +               }
5421 +#if 1
5422 +               // XXX this i am not sure about; it seems like this should be required, but it
5423 +               // isn't, and seems to cause problems
5424 +               else
5425 +               {
5426 +                       dev->mode2_partial_pkt_size = bytes_left_in_packet;
5427 +               }
5428 +#endif
5429 +               return pos;
5430 +       }
5431 +}
5432 +
5433 +static ssize_t mceusb_read( struct file* file, char* buffer,
5434 +                           size_t count, loff_t* ppos)
5435 +{
5436 +       char _data_buffer[128];
5437 +       struct usb_skel* dev;
5438 +       int read_count;
5439 +       int bytes_copied = 0;
5440 +
5441 +       dev = (struct usb_skel*) file->private_data;
5442 +
5443 +       if( (count % 4) != 0 )
5444 +       {
5445 +               return -EINVAL;
5446 +       }
5447 +
5448 +       down( &dev->sem );
5449 +
5450 +       /* verify that the device wasn't unplugged */
5451 +       if (dev->udev == NULL) {
5452 +               up( &dev->sem );
5453 +               return -ENODEV;
5454 +       }
5455 +
5456 +       dbg(__FUNCTION__ " (1) calling msir_copy_mode2 with %d", count);
5457 +       bytes_copied = 4 * msir_copy_mode2( dev, (int*)buffer, count >> 2 );
5458 +       if( bytes_copied == count )
5459 +       {
5460 +               up( &dev->sem );
5461 +               return count;
5462 +       }
5463 +
5464 +       /* we didn't get enough mode2 data. the process now is a bit complex
5465 +        * 1. see if we have data read from the usb device that hasn't
5466 +        *    been converted to mode2; if so, convert that, and try to
5467 +        *    copy that out
5468 +        * 2. otherwise, go ahead and read more, then convert that, then copy
5469 +        */
5470 +
5471 +       if( dev->usb_dcount )
5472 +       {
5473 +               read_count = msir_read_from_buffer( dev, _data_buffer, 128 );
5474 +               read_count = msir_generate_mode2
5475 +                       ( dev, (signed char*)_data_buffer, read_count );
5476 +               msir_mark_as_read( dev, read_count );
5477 +               bytes_copied += (4 * msir_copy_mode2
5478 +                                ( dev, (int*)(buffer + bytes_copied),
5479 +                                  (count-bytes_copied) >> 2 ));
5480 +       }
5481 +
5482 +       if( bytes_copied == count )
5483 +       {
5484 +               up( &dev->sem );
5485 +               return count;
5486 +       }
5487 +
5488 +       /* read more data in a loop until we get enough */
5489 +       while( bytes_copied < count )
5490 +       {
5491 +               read_count = msir_fetch_more_data
5492 +                       ( dev, (file->f_flags & O_NONBLOCK ? 1 : 0) );
5493 +
5494 +               if( read_count <= 0 )
5495 +               {
5496 +                       up( &dev->sem );
5497 +                       return (read_count ? read_count : -EWOULDBLOCK);
5498 +               }
5499 +
5500 +               read_count = msir_read_from_buffer( dev, _data_buffer, 128 );
5501 +               read_count = msir_generate_mode2
5502 +                       ( dev, (signed char*)_data_buffer, read_count );
5503 +               msir_mark_as_read( dev, read_count );
5504 +
5505 +               bytes_copied += (4 * msir_copy_mode2
5506 +                                ( dev, (int*)(buffer + bytes_copied),
5507 +                                  (count-bytes_copied) >> 2 ));
5508 +       }
5509 +
5510 +       up( &dev->sem );
5511 +       return bytes_copied;
5512 +}
5513 +
5514 +/**
5515 + * mceusb_poll
5516 + */
5517 +static unsigned int mceusb_poll(struct file* file, poll_table* wait)
5518 +{
5519 +       struct usb_skel* dev;
5520 +       int data;
5521 +       dev = (struct usb_skel*)file->private_data;
5522 +
5523 +       // So this is a crummy poll. Unfortunately all the lirc tools
5524 +       // assume your hardware is interrupt driven. Instead, we have
5525 +       // to actually read here to see whether or not there is data
5526 +       // (unless we have a key saved up - unlikely )
5527 +
5528 +       //    if( dev->usb_dcount || dev->mode2_count )
5529 +       if( msir_available_data( dev ) || msir_available_mode2( dev ) )
5530 +       {
5531 +               return POLLIN | POLLRDNORM;
5532 +       }
5533 +       else {
5534 +               down( &dev->sem );
5535 +               data = msir_fetch_more_data( dev, 1 );
5536 +               up( &dev->sem );
5537 +
5538 +               if( data )
5539 +                       return POLLIN | POLLRDNORM;
5540 +       }
5541 +
5542 +       return 0;
5543 +}
5544 +
5545 +/**
5546 + *     mceusb_write
5547 + */
5548 +static ssize_t mceusb_write (struct file *file, const char *buffer, size_t count, loff_t *ppos)
5549 +{
5550 +       struct usb_skel *dev;
5551 +       ssize_t bytes_written = 0;
5552 +       int retval = 0;
5553 +
5554 +       dev = (struct usb_skel *)file->private_data;
5555 +
5556 +       dbg(__FUNCTION__ " - minor %d, count = %d", dev->minor, count);
5557 +
5558 +       /* lock this object */
5559 +       down (&dev->sem);
5560 +
5561 +       /* verify that the device wasn't unplugged */
5562 +       if (dev->udev == NULL) {
5563 +               retval = -ENODEV;
5564 +               goto exit;
5565 +       }
5566 +
5567 +       /* verify that we actually have some data to write */
5568 +       if (count == 0) {
5569 +               dbg(__FUNCTION__ " - write request of 0 bytes");
5570 +               goto exit;
5571 +       }
5572 +
5573 +       /* see if we are already in the middle of a write */
5574 +       if (dev->write_urb->status == -EINPROGRESS) {
5575 +               dbg (__FUNCTION__ " - already writing");
5576 +               goto exit;
5577 +       }
5578 +
5579 +       /* we can only write as much as 1 urb will hold */
5580 +       bytes_written = (count > dev->bulk_out_size) ?
5581 +               dev->bulk_out_size : count;
5582 +
5583 +       /* copy the data from userspace into our urb */
5584 +       if (copy_from_user(dev->write_urb->transfer_buffer, buffer,
5585 +                          bytes_written)) {
5586 +               retval = -EFAULT;
5587 +               goto exit;
5588 +       }
5589 +
5590 +       usb_mceusb_debug_data (__FUNCTION__, bytes_written,
5591 +                              dev->write_urb->transfer_buffer);
5592 +
5593 +       /* set up our urb */
5594 +       FILL_BULK_URB(dev->write_urb, dev->udev,
5595 +                     usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
5596 +                     dev->write_urb->transfer_buffer, bytes_written,
5597 +                     mceusb_write_bulk_callback, dev);
5598 +
5599 +       /* send the data out the bulk port */
5600 +       retval = usb_submit_urb(dev->write_urb);
5601 +       if (retval) {
5602 +               err(__FUNCTION__ " - failed submitting write urb, error %d",
5603 +                   retval);
5604 +       } else {
5605 +               retval = bytes_written;
5606 +       }
5607 +
5608 +  exit:
5609 +       /* unlock the device */
5610 +       up (&dev->sem);
5611 +
5612 +       return retval;
5613 +}
5614 +
5615 +/*
5616 + *     mceusb_write_bulk_callback
5617 + */
5618 +
5619 +static void mceusb_write_bulk_callback (struct urb *urb)
5620 +{
5621 +       struct usb_skel *dev = (struct usb_skel *)urb->context;
5622 +
5623 +       dbg(__FUNCTION__ " - minor %d", dev->minor);
5624 +
5625 +       if ((urb->status != -ENOENT) &&
5626 +           (urb->status != -ECONNRESET)) {
5627 +               dbg(__FUNCTION__ " - nonzero write bulk status received: %d",
5628 +                   urb->status);
5629 +               return;
5630 +       }
5631 +
5632 +       return;
5633 +}
5634 +
5635 +/**
5636 + *     mceusb_probe
5637 + *
5638 + *     Called by the usb core when a new device is connected that it
5639 + *     thinks this driver might be interested in.
5640 + */
5641 +static void * mceusb_probe(struct usb_device *udev, unsigned int ifnum,
5642 +                          const struct usb_device_id *id)
5643 +{
5644 +       struct usb_skel *dev = NULL;
5645 +       struct usb_interface *interface;
5646 +       struct usb_interface_descriptor *iface_desc;
5647 +       struct usb_endpoint_descriptor *endpoint;
5648 +       struct lirc_plugin* plugin;
5649 +       struct lirc_buffer* rbuf;
5650 +
5651 +       int minor;
5652 +       int buffer_size;
5653 +       int i;
5654 +       char name[10];
5655 +
5656 +
5657 +       /* See if the device offered us matches what we can accept */
5658 +       if ((udev->descriptor.idVendor != USB_MCEUSB_VENDOR_ID) ||
5659 +           (udev->descriptor.idProduct != USB_MCEUSB_PRODUCT_ID)) {
5660 +               return NULL;
5661 +       }
5662 +
5663 +       /* select a "subminor" number (part of a minor number) */
5664 +       down (&minor_table_mutex);
5665 +       for (minor = 0; minor < MAX_DEVICES; ++minor) {
5666 +               if (minor_table[minor] == NULL)
5667 +                       break;
5668 +       }
5669 +       if (minor >= MAX_DEVICES) {
5670 +               info ("Too many devices plugged in, can not handle this device.");
5671 +               goto exit;
5672 +       }
5673 +
5674 +       /* allocate memory for our device state and intialize it */
5675 +       dev = kmalloc (sizeof(struct usb_skel), GFP_KERNEL);
5676 +       if (dev == NULL) {
5677 +               err ("Out of memory");
5678 +               goto exit;
5679 +       }
5680 +       minor_table[minor] = dev;
5681 +
5682 +       interface = &udev->actconfig->interface[ifnum];
5683 +
5684 +       init_MUTEX (&dev->sem);
5685 +       dev->udev = udev;
5686 +       dev->interface = interface;
5687 +       dev->minor = minor;
5688 +
5689 +       /* set up the endpoint information */
5690 +       /* check out the endpoints */
5691 +       iface_desc = &interface->altsetting[0];
5692 +       for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
5693 +               endpoint = &iface_desc->endpoint[i];
5694 +
5695 +               if ((endpoint->bEndpointAddress & 0x80) &&
5696 +                   ((endpoint->bmAttributes & 3) == 0x02)) {
5697 +                       /* we found a bulk in endpoint */
5698 +                       buffer_size = endpoint->wMaxPacketSize;
5699 +                       dev->bulk_in_size = buffer_size;
5700 +                       dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
5701 +                       dev->bulk_in_buffer = kmalloc (buffer_size, GFP_KERNEL);
5702 +                       if (!dev->bulk_in_buffer) {
5703 +                               err("Couldn't allocate bulk_in_buffer");
5704 +                               goto error;
5705 +                       }
5706 +               }
5707 +
5708 +               if (((endpoint->bEndpointAddress & 0x80) == 0x00) &&
5709 +                   ((endpoint->bmAttributes & 3) == 0x02)) {
5710 +                       /* we found a bulk out endpoint */
5711 +                       dev->write_urb = usb_alloc_urb(0);
5712 +                       if (!dev->write_urb) {
5713 +                               err("No free urbs available");
5714 +                               goto error;
5715 +                       }
5716 +                       buffer_size = endpoint->wMaxPacketSize;
5717 +                       dev->bulk_out_size = buffer_size;
5718 +                       dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
5719 +                       dev->bulk_out_buffer = kmalloc (buffer_size, GFP_KERNEL);
5720 +                       if (!dev->bulk_out_buffer) {
5721 +                               err("Couldn't allocate bulk_out_buffer");
5722 +                               goto error;
5723 +                       }
5724 +                       FILL_BULK_URB(dev->write_urb, udev,
5725 +                                     usb_sndbulkpipe
5726 +                                     (udev, endpoint->bEndpointAddress),
5727 +                                     dev->bulk_out_buffer, buffer_size,
5728 +                                     mceusb_write_bulk_callback, dev);
5729 +               }
5730 +       }
5731 +
5732 +       memset( dev->mode2_data, 0, sizeof( dev->mode2_data ) );
5733 +       dev->mode2_idx = 0;
5734 +       dev->mode2_count = 0;
5735 +       dev->mode2_partial_pkt_size = 0;
5736 +       dev->mode2_once = 0;
5737 +       dev->last_was_repeat_gap = 0;
5738 +
5739 +       /* Set up our lirc plugin */
5740 +       if(!(plugin = kmalloc(sizeof(struct lirc_plugin), GFP_KERNEL))) {
5741 +               err("out of memory");
5742 +               goto error;
5743 +       }
5744 +       memset( plugin, 0, sizeof(struct lirc_plugin) );
5745 +
5746 +       if(!(rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL))) {
5747 +               err("out of memory");
5748 +               kfree( plugin );
5749 +               goto error;
5750 +       }
5751 +       /* the lirc_atiusb module doesn't memset rbuf here ... ? */
5752 +       if( lirc_buffer_init( rbuf, sizeof(lirc_t),
5753 +                             sizeof(struct lirc_buffer))) {
5754 +               err("out of memory");
5755 +               kfree( plugin );
5756 +               kfree( rbuf );
5757 +               goto error;
5758 +       }
5759 +       strcpy(plugin->name, "lirc_mce ");
5760 +       plugin->minor       = minor;
5761 +       plugin->code_length = sizeof(lirc_t);
5762 +       plugin->features    = LIRC_CAN_REC_MODE2; // | LIRC_CAN_SEND_MODE2;
5763 +       plugin->data        = dev;
5764 +       plugin->rbuf        = rbuf;
5765 +       plugin->ioctl       = NULL;
5766 +       plugin->set_use_inc = &set_use_inc;
5767 +       plugin->set_use_dec = &set_use_dec;
5768 +       plugin->fops        = &mceusb_fops;
5769 +       if( lirc_register_plugin( plugin ) < 0 )
5770 +       {
5771 +               kfree( plugin );
5772 +               lirc_buffer_free( rbuf );
5773 +               kfree( rbuf );
5774 +               goto error;
5775 +       }
5776 +       dev->plugin = plugin;
5777 +
5778 +       mceusb_setup( udev );
5779 +
5780 +       /* let the user know what node this device is now attached to */
5781 +       info ("USB Microsoft IR Transceiver device now attached to msir%d",
5782 +             dev->minor);
5783 +       goto exit;
5784 +
5785 +  error:
5786 +       mceusb_delete (dev);
5787 +       dev = NULL;
5788 +
5789 +  exit:
5790 +       up (&minor_table_mutex);
5791 +       return dev;
5792 +}
5793 +
5794 +/**
5795 + *     mceusb_disconnect
5796 + *
5797 + *     Called by the usb core when the device is removed from the system.
5798 + */
5799 +static void mceusb_disconnect(struct usb_device *udev, void *ptr)
5800 +{
5801 +       int minor;
5802 +
5803 +       down (&minor_table_mutex);
5804 +       down (&dev->sem);
5805 +
5806 +       minor = dev->minor;
5807 +
5808 +       /* unhook lirc things */
5809 +       lirc_unregister_plugin( dev->minor );
5810 +       lirc_buffer_free( dev->plugin->rbuf );
5811 +       kfree( dev->plugin->rbuf );
5812 +       kfree( dev->plugin );
5813 +
5814 +       /* if the device is not opened, then we clean up right now */
5815 +       if (!dev->open_count) {
5816 +               up (&dev->sem);
5817 +               mceusb_delete (dev);
5818 +       } else {
5819 +               dev->udev = NULL;
5820 +               up (&dev->sem);
5821 +       }
5822 +
5823 +       info("USB Skeleton #%d now disconnected", minor);
5824 +       up (&minor_table_mutex);
5825 +       return NULL;
5826 +}
5827 +
5828 +
5829 +
5830 +/**
5831 + *     usb_mceusb_init
5832 + */
5833 +static int __init usb_mceusb_init(void)
5834 +{
5835 +       int result;
5836 +
5837 +       /* register this driver with the USB subsystem */
5838 +       result = usb_register(&mceusb_driver);
5839 +       if (result < 0) {
5840 +               err("usb_register failed for the "__FILE__" driver. Error number %d",
5841 +                   result);
5842 +               return -1;
5843 +       }
5844 +
5845 +       info(DRIVER_DESC " " DRIVER_VERSION);
5846 +       return 0;
5847 +}
5848 +
5849 +
5850 +/**
5851 + *     usb_mceusb_exit
5852 + */
5853 +static void __exit usb_mceusb_exit(void)
5854 +{
5855 +       /* deregister this driver with the USB subsystem */
5856 +       usb_deregister(&mceusb_driver);
5857 +}
5858 +
5859 +
5860 +module_init (usb_mceusb_init);
5861 +module_exit (usb_mceusb_exit);
5862 +
5863 +MODULE_AUTHOR(DRIVER_AUTHOR);
5864 +MODULE_DESCRIPTION(DRIVER_DESC);
5865 +MODULE_LICENSE("GPL");
5866 +
5867 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_parallel.c linux-2.6.5/drivers/char/lirc/lirc_parallel.c
5868 --- linux-2.6.5.orig/drivers/char/lirc/lirc_parallel.c  1970-01-01 01:00:00.000000000 +0100
5869 +++ linux-2.6.5/drivers/char/lirc/lirc_parallel.c       2004-04-04 14:05:29.424626048 +0200
5870 @@ -0,0 +1,739 @@
5871 +/*      $Id$      */
5872 +
5873 +/****************************************************************************
5874 + ** lirc_parallel.c *********************************************************
5875 + ****************************************************************************
5876 + *
5877 + * lirc_parallel - device driver for infra-red signal receiving and
5878 + *                 transmitting unit built by the author
5879 + *
5880 + * Copyright (C) 1998 Christoph Bartelmus <lirc@bartelmus.de>
5881 + *
5882 + *  This program is free software; you can redistribute it and/or modify
5883 + *  it under the terms of the GNU General Public License as published by
5884 + *  the Free Software Foundation; either version 2 of the License, or
5885 + *  (at your option) any later version.
5886 + *
5887 + *  This program is distributed in the hope that it will be useful,
5888 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
5889 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5890 + *  GNU General Public License for more details.
5891 + *
5892 + *  You should have received a copy of the GNU General Public License
5893 + *  along with this program; if not, write to the Free Software
5894 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
5895 + *
5896 + */
5897 +
5898 +/***********************************************************************
5899 + *************************       Includes        ***********************
5900 + ***********************************************************************/
5901 +
5902 +#include <linux/version.h>
5903 +
5904 +#include <linux/config.h>
5905 +
5906 +#include <linux/module.h>
5907 +#include <linux/sched.h>
5908 +#include <linux/errno.h>
5909 +#include <linux/signal.h>
5910 +#include <linux/config.h>
5911 +#include <linux/fs.h>
5912 +#include <linux/kernel.h>
5913 +#include <linux/ioport.h>
5914 +#include <linux/time.h>
5915 +#include <linux/mm.h>
5916 +#include <linux/delay.h>
5917 +#include <linux/init.h>
5918 +
5919 +#include <asm/io.h>
5920 +#include <asm/signal.h>
5921 +#include <asm/irq.h>
5922 +#include <asm/system.h>
5923 +
5924 +#include <asm/uaccess.h>
5925 +#include <linux/poll.h>
5926 +#include <linux/parport.h>
5927 +
5928 +#include <linux/lirc.h>
5929 +#include "lirc_dev.h"
5930 +
5931 +#include "lirc_parallel.h"
5932 +
5933 +#define LIRC_DRIVER_NAME "lirc_parallel"
5934 +
5935 +/***********************************************************************
5936 + *************************   Globale Variablen   ***********************
5937 + ***********************************************************************/
5938 +
5939 +unsigned int irq = CONFIG_LIRC_IRQ_PARALLEL;
5940 +unsigned int io = CONFIG_LIRC_PORT_PARALLEL;
5941 +#ifdef CONFIG_LIRC_TIMER
5942 +unsigned int timer = 0;
5943 +unsigned int default_timer = CONFIG_LIRC_TIMER;
5944 +#endif
5945 +
5946 +#define WBUF_SIZE (256)
5947 +#define RBUF_SIZE (256) /* this must be a power of 2 larger than 1 */
5948 +
5949 +static lirc_t wbuf[WBUF_SIZE];
5950 +static lirc_t rbuf[RBUF_SIZE];
5951 +
5952 +DECLARE_WAIT_QUEUE_HEAD(lirc_wait);
5953 +
5954 +unsigned int rptr=0,wptr=0;
5955 +unsigned int lost_irqs=0;
5956 +int is_open=0;
5957 +
5958 +struct parport *pport;
5959 +struct pardevice *ppdevice;
5960 +int is_claimed=0;
5961 +
5962 +int pf(void *handle);
5963 +void kf(void *handle);
5964 +
5965 +/***********************************************************************
5966 + *************************   Interne Funktionen  ***********************
5967 + ***********************************************************************/
5968 +
5969 +unsigned int __inline__ in(int offset)
5970 +{
5971 +       switch(offset)
5972 +       {
5973 +       case LIRC_LP_BASE:
5974 +               return(parport_read_data(pport));
5975 +       case LIRC_LP_STATUS:
5976 +               return(parport_read_status(pport));
5977 +       case LIRC_LP_CONTROL:
5978 +               return(parport_read_control(pport));
5979 +       }
5980 +       return(0); /* make compiler happy */
5981 +}
5982 +
5983 +void __inline__ out(int offset, int value)
5984 +{
5985 +       switch(offset)
5986 +       {
5987 +       case LIRC_LP_BASE:
5988 +               parport_write_data(pport,value);
5989 +               break;
5990 +       case LIRC_LP_CONTROL:
5991 +               parport_write_control(pport,value);
5992 +               break;
5993 +       case LIRC_LP_STATUS:
5994 +               printk(KERN_INFO "%s: attempt to write to status register\n",
5995 +                      LIRC_DRIVER_NAME);
5996 +               break;
5997 +       }
5998 +}
5999 +
6000 +unsigned int __inline__ lirc_get_timer(void)
6001 +{
6002 +       return(in(LIRC_PORT_TIMER)&LIRC_PORT_TIMER_BIT);
6003 +}
6004 +
6005 +unsigned int __inline__  lirc_get_signal(void)
6006 +{
6007 +       return(in(LIRC_PORT_SIGNAL)&LIRC_PORT_SIGNAL_BIT);
6008 +}
6009 +
6010 +void __inline__ lirc_on(void)
6011 +{
6012 +       out(LIRC_PORT_DATA,LIRC_PORT_DATA_BIT);
6013 +}
6014 +
6015 +void __inline__ lirc_off(void)
6016 +{
6017 +       out(LIRC_PORT_DATA,0);
6018 +}
6019 +
6020 +unsigned int init_CONFIG_LIRC_TIMER(void)
6021 +{
6022 +       struct timeval tv,now;
6023 +       unsigned int level,newlevel,timeelapsed,newtimer;
6024 +       int count=0;
6025 +
6026 +       do_gettimeofday(&tv);
6027 +       tv.tv_sec++;                     /* wait max. 1 sec. */
6028 +       level=lirc_get_timer();
6029 +       do
6030 +       {
6031 +               newlevel=lirc_get_timer();
6032 +               if(level==0 && newlevel!=0) count++;
6033 +               level=newlevel;
6034 +               do_gettimeofday(&now);
6035 +       }
6036 +       while(count<1000 && (now.tv_sec<tv.tv_sec
6037 +                            || (now.tv_sec==tv.tv_sec
6038 +                                && now.tv_usec<tv.tv_usec)));
6039 +
6040 +       timeelapsed=((now.tv_sec+1-tv.tv_sec)*1000000
6041 +                    +(now.tv_usec-tv.tv_usec));
6042 +       if(count>=1000 && timeelapsed>0)
6043 +       {
6044 +               if(default_timer==0)                    /* autodetect timer */
6045 +               {
6046 +                       newtimer=(1000000*count)/timeelapsed;
6047 +                       printk(KERN_INFO "%s: %u Hz timer detected\n",
6048 +                              LIRC_DRIVER_NAME,newtimer);
6049 +                       return(newtimer);
6050 +               }
6051 +               else
6052 +               {
6053 +                       newtimer=(1000000*count)/timeelapsed;
6054 +                       if(abs(newtimer-default_timer)>
6055 +                          default_timer/10) /* bad timer */
6056 +                       {
6057 +                               printk(KERN_NOTICE "%s: bad timer: %u Hz\n",
6058 +                                      LIRC_DRIVER_NAME,newtimer);
6059 +                               printk(KERN_NOTICE "%s: using default timer: "
6060 +                                      "%u Hz\n",
6061 +                                      LIRC_DRIVER_NAME,default_timer);
6062 +                               return(default_timer);
6063 +                       }
6064 +                       else
6065 +                       {
6066 +                               printk(KERN_INFO "%s: %u Hz timer detected\n",
6067 +                                      LIRC_DRIVER_NAME,newtimer);
6068 +                               return(newtimer); /* use detected value */
6069 +                       }
6070 +               }
6071 +       }
6072 +       else
6073 +       {
6074 +               printk(KERN_NOTICE "%s: no timer detected\n",LIRC_DRIVER_NAME);
6075 +               return(0);
6076 +       }
6077 +}
6078 +
6079 +int lirc_claim(void)
6080 +{
6081 +       if(parport_claim(ppdevice)!=0)
6082 +       {
6083 +               printk(KERN_WARNING "%s: could not claim port\n",
6084 +                      LIRC_DRIVER_NAME);
6085 +               printk(KERN_WARNING "%s: waiting for port becoming available"
6086 +                      "\n",LIRC_DRIVER_NAME);
6087 +               if(parport_claim_or_block(ppdevice)<0)
6088 +               {
6089 +                       printk(KERN_NOTICE "%s: could not claim port, giving"
6090 +                              " up\n",LIRC_DRIVER_NAME);
6091 +                       return(0);
6092 +               }
6093 +       }
6094 +       out(LIRC_LP_CONTROL,LP_PSELECP|LP_PINITP);
6095 +       is_claimed=1;
6096 +       return(1);
6097 +}
6098 +
6099 +/***********************************************************************
6100 + *************************   interrupt handler  ************************
6101 + ***********************************************************************/
6102 +
6103 +static inline void rbuf_write(lirc_t signal)
6104 +{
6105 +       unsigned int nwptr;
6106 +
6107 +       nwptr=(wptr+1) & (RBUF_SIZE-1);
6108 +       if(nwptr==rptr) /* no new signals will be accepted */
6109 +       {
6110 +               lost_irqs++;
6111 +               printk(KERN_NOTICE "%s: buffer overrun\n",LIRC_DRIVER_NAME);
6112 +               return;
6113 +       }
6114 +       rbuf[wptr]=signal;
6115 +       wptr=nwptr;
6116 +}
6117 +
6118 +void irq_handler(int i,void *blah,struct pt_regs * regs)
6119 +{
6120 +       struct timeval tv;
6121 +       static struct timeval lasttv;
6122 +       static int init=0;
6123 +       long signal;
6124 +       lirc_t data;
6125 +       unsigned int level,newlevel;
6126 +       unsigned int timeout;
6127 +
6128 +#ifdef CONFIG_MODULE_UNLOAD
6129 +       if(!module_refcount(THIS_MODULE))
6130 +               return;
6131 +#endif
6132 +
6133 +       if(!is_claimed)
6134 +       {
6135 +               return;
6136 +       }
6137 +
6138 +       /* disable interrupt */
6139 +       /*
6140 +         disable_irq(irq);
6141 +         out(LIRC_PORT_IRQ,in(LIRC_PORT_IRQ)&(~LP_PINTEN));
6142 +       */
6143 +       if(in(1)&LP_PSELECD)
6144 +       {
6145 +               return;
6146 +       }
6147 +
6148 +#ifdef CONFIG_LIRC_TIMER
6149 +       if(init)
6150 +       {
6151 +               do_gettimeofday(&tv);
6152 +
6153 +               signal=tv.tv_sec-lasttv.tv_sec;
6154 +               if(signal>15)
6155 +               {
6156 +                       data=PULSE_MASK;  /* really long time */
6157 +               }
6158 +               else
6159 +               {
6160 +                       data=(lirc_t) (signal*1000000+
6161 +                                      tv.tv_usec-lasttv.tv_usec+
6162 +                                      LIRC_SFH506_DELAY);
6163 +               };
6164 +
6165 +               rbuf_write(data); /* space */
6166 +       }
6167 +       else
6168 +       {
6169 +               if(timer==0) /* wake up; we'll lose this signal
6170 +                               but it will be garbage if the device
6171 +                               is turned on anyway
6172 +                             */
6173 +               {
6174 +                       timer=init_CONFIG_LIRC_TIMER();
6175 +                       /* enable_irq(irq); */
6176 +                       return;
6177 +               }
6178 +               init=1;
6179 +       }
6180 +
6181 +       timeout=timer/10;           /* timeout after 1/10 sec. */
6182 +       signal=1;
6183 +       level=lirc_get_timer();
6184 +       do{
6185 +               newlevel=lirc_get_timer();
6186 +               if(level==0 && newlevel!=0) signal++;
6187 +               level=newlevel;
6188 +
6189 +               /* giving up */
6190 +               if(signal>timeout || (in(1)&LP_PSELECD))
6191 +               {
6192 +                       signal=0;
6193 +                       printk(KERN_NOTICE "%s: timeout\n",LIRC_DRIVER_NAME);
6194 +                       break;
6195 +               }
6196 +       }
6197 +       while(lirc_get_signal());
6198 +       if(signal!=0)
6199 +       {
6200 +               /* ajust value to usecs */
6201 +               signal=(long) (((unsigned long long) signal)*1000000)/timer;
6202 +
6203 +               if(signal>LIRC_SFH506_DELAY)
6204 +               {
6205 +                       data=signal-LIRC_SFH506_DELAY;
6206 +               }
6207 +               else
6208 +               {
6209 +                       data=1;
6210 +               }
6211 +               rbuf_write(PULSE_BIT|data); /* pulse */
6212 +       }
6213 +       do_gettimeofday(&lasttv);
6214 +#else
6215 +       /* add your code here */
6216 +#endif
6217 +
6218 +       wake_up_interruptible(&lirc_wait);
6219 +
6220 +       /* enable interrupt */
6221 +       /*
6222 +         enable_irq(irq);
6223 +         out(LIRC_PORT_IRQ,in(LIRC_PORT_IRQ)|LP_PINTEN);
6224 +       */
6225 +}
6226 +
6227 +/***********************************************************************
6228 + **************************   file_operations   ************************
6229 + ***********************************************************************/
6230 +
6231 +static loff_t lirc_lseek(struct file *filep,loff_t offset,int orig)
6232 +{
6233 +       return(-ESPIPE);
6234 +}
6235 +
6236 +static ssize_t lirc_read(struct file *filep,char *buf,size_t n,loff_t *ppos)
6237 +{
6238 +       int result;
6239 +       int count=0;
6240 +       DECLARE_WAITQUEUE(wait, current);
6241 +
6242 +       if(n%sizeof(lirc_t)) return(-EINVAL);
6243 +
6244 +       result=verify_area(VERIFY_WRITE,buf,n);
6245 +       if(result) return(result);
6246 +
6247 +       add_wait_queue(&lirc_wait,&wait);
6248 +       current->state=TASK_INTERRUPTIBLE;
6249 +       while(count<n)
6250 +       {
6251 +               if(rptr!=wptr)
6252 +               {
6253 +                       copy_to_user(buf+count,(char *) &rbuf[rptr],
6254 +                                    sizeof(lirc_t));
6255 +                       rptr=(rptr+1)&(RBUF_SIZE-1);
6256 +                       count+=sizeof(lirc_t);
6257 +               }
6258 +               else
6259 +               {
6260 +                       if(filep->f_flags & O_NONBLOCK)
6261 +                       {
6262 +                               result=-EAGAIN;
6263 +                               break;
6264 +                       }
6265 +                       if (signal_pending(current))
6266 +                       {
6267 +                               result=-ERESTARTSYS;
6268 +                               break;
6269 +                       }
6270 +                       schedule();
6271 +                       current->state=TASK_INTERRUPTIBLE;
6272 +               }
6273 +       }
6274 +       remove_wait_queue(&lirc_wait,&wait);
6275 +       current->state=TASK_RUNNING;
6276 +       return(count ? count:result);
6277 +}
6278 +
6279 +static ssize_t lirc_write(struct file *filep,const char *buf,size_t n,
6280 +                         loff_t *ppos)
6281 +{
6282 +       int result,count;
6283 +       unsigned int i;
6284 +       unsigned int level,newlevel;
6285 +       unsigned long flags;
6286 +       lirc_t counttimer;
6287 +
6288 +       if(!is_claimed)
6289 +       {
6290 +               return(-EBUSY);
6291 +       }
6292 +       if(n%sizeof(lirc_t)) return(-EINVAL);
6293 +       result=verify_area(VERIFY_READ,buf,n);
6294 +       if(result) return(result);
6295 +
6296 +       count=n/sizeof(lirc_t);
6297 +
6298 +       if(count>WBUF_SIZE || count%2==0) return(-EINVAL);
6299 +
6300 +       copy_from_user(wbuf,buf,n);
6301 +
6302 +#ifdef CONFIG_LIRC_TIMER
6303 +       if(timer==0) /* try again if device is ready */
6304 +       {
6305 +               timer=init_CONFIG_LIRC_TIMER();
6306 +               if(timer==0) return(-EIO);
6307 +       }
6308 +
6309 +       /* ajust values from usecs */
6310 +       for(i=0;i<count;i++)
6311 +       {
6312 +               wbuf[i]=(lirc_t) (((double) wbuf[i])*timer/1000000);
6313 +       }
6314 +
6315 +       local_save_flags(flags);local_irq_disable();
6316 +       i=0;
6317 +       while(i<count)
6318 +       {
6319 +               level=lirc_get_timer();
6320 +               counttimer=0;
6321 +               lirc_on();
6322 +               do
6323 +               {
6324 +                       newlevel=lirc_get_timer();
6325 +                       if(level==0 && newlevel!=0) counttimer++;
6326 +                       level=newlevel;
6327 +                       if(in(1)&LP_PSELECD)
6328 +                       {
6329 +                               lirc_off();
6330 +                               local_irq_restore(flags); /* sti(); */
6331 +                               return(-EIO);
6332 +                       }
6333 +               }
6334 +               while(counttimer<wbuf[i]);i++;
6335 +
6336 +               lirc_off();
6337 +               if(i==count) break;
6338 +               counttimer=0;
6339 +               do
6340 +               {
6341 +                       newlevel=lirc_get_timer();
6342 +                       if(level==0 && newlevel!=0) counttimer++;
6343 +                       level=newlevel;
6344 +                       if(in(1)&LP_PSELECD)
6345 +                       {
6346 +                               local_irq_restore(flags); /* sti(); */
6347 +                               return(-EIO);
6348 +                       }
6349 +               }
6350 +               while(counttimer<wbuf[i]);i++;
6351 +       }
6352 +       local_irq_restore(flags); /* sti(); */
6353 +#else
6354 +       /*
6355 +          place code that handles write
6356 +          without extarnal timer here
6357 +       */
6358 +#endif
6359 +       return(n);
6360 +}
6361 +
6362 +static unsigned int lirc_poll(struct file *file, poll_table * wait)
6363 +{
6364 +       poll_wait(file, &lirc_wait,wait);
6365 +       if (rptr!=wptr)
6366 +               return(POLLIN|POLLRDNORM);
6367 +       return(0);
6368 +}
6369 +
6370 +static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
6371 +                     unsigned long arg)
6372 +{
6373 +        int result;
6374 +       unsigned long features=LIRC_CAN_SEND_PULSE|LIRC_CAN_REC_MODE2,mode;
6375 +
6376 +       switch(cmd)
6377 +       {
6378 +       case LIRC_GET_FEATURES:
6379 +               result=put_user(features,(unsigned long *) arg);
6380 +               if(result) return(result);
6381 +               break;
6382 +       case LIRC_GET_SEND_MODE:
6383 +               result=put_user(LIRC_MODE_PULSE,(unsigned long *) arg);
6384 +               if(result) return(result);
6385 +               break;
6386 +       case LIRC_GET_REC_MODE:
6387 +               result=put_user(LIRC_MODE_MODE2,(unsigned long *) arg);
6388 +               if(result) return(result);
6389 +               break;
6390 +       case LIRC_SET_SEND_MODE:
6391 +               result=get_user(mode,(unsigned long *) arg);
6392 +               if(result) return(result);
6393 +               if(mode!=LIRC_MODE_PULSE) return(-EINVAL);
6394 +               break;
6395 +       case LIRC_SET_REC_MODE:
6396 +               result=get_user(mode,(unsigned long *) arg);
6397 +               if(result) return(result);
6398 +               if(mode!=LIRC_MODE_MODE2) return(-ENOSYS);
6399 +               break;
6400 +       default:
6401 +               return(-ENOIOCTLCMD);
6402 +       }
6403 +       return(0);
6404 +}
6405 +
6406 +static int lirc_open(struct inode* node,struct file* filep)
6407 +{
6408 +#ifdef CONFIG_MODULE_UNLOAD
6409 +       if(module_refcount(THIS_MODULE))
6410 +       {
6411 +               return(-EBUSY);
6412 +       }
6413 +#endif
6414 +       if(!lirc_claim())
6415 +       {
6416 +               return(-EBUSY);
6417 +       }
6418 +       pport->ops->enable_irq(pport);
6419 +
6420 +       /* init read ptr */
6421 +       rptr=wptr=0;
6422 +       lost_irqs=0;
6423 +
6424 +       try_module_get(THIS_MODULE);
6425 +       is_open=1;
6426 +       return(0);
6427 +}
6428 +
6429 +static int lirc_close(struct inode* node,struct file* filep)
6430 +{
6431 +       if(is_claimed)
6432 +       {
6433 +               is_claimed=0;
6434 +               parport_release(ppdevice);
6435 +       }
6436 +       is_open=0;
6437 +       module_put(THIS_MODULE);
6438 +       return(0);
6439 +}
6440 +
6441 +static struct file_operations lirc_fops =
6442 +{
6443 +       llseek:  lirc_lseek,
6444 +       read:    lirc_read,
6445 +       write:   lirc_write,
6446 +       poll:    lirc_poll,
6447 +       ioctl:   lirc_ioctl,
6448 +       open:    lirc_open,
6449 +       release: lirc_close
6450 +};
6451 +
6452 +static int set_use_inc(void* data)
6453 +{
6454 +#if WE_DONT_USE_LOCAL_OPEN_CLOSE
6455 +       try_module_get(THIS_MODULE);
6456 +#endif
6457 +       return 0;
6458 +}
6459 +
6460 +static void set_use_dec(void* data)
6461 +{
6462 +#if WE_DONT_USE_LOCAL_OPEN_CLOSE
6463 +       module_put(THIS_MODULE);
6464 +#endif
6465 +}
6466 +static struct lirc_plugin plugin = {
6467 +       name:           LIRC_DRIVER_NAME,
6468 +       minor:          -1,
6469 +       code_length:    1,
6470 +       sample_rate:    0,
6471 +       data:           NULL,
6472 +       add_to_buf:     NULL,
6473 +       get_queue:      NULL,
6474 +       set_use_inc:    set_use_inc,
6475 +       set_use_dec:    set_use_dec,
6476 +       fops:           &lirc_fops,
6477 +};
6478 +
6479 +static int __init lirc_parallel_init (void)
6480 +{
6481 +       pport=parport_enumerate();
6482 +       while(pport!=NULL)
6483 +       {
6484 +               if(pport->base==io)
6485 +               {
6486 +                       break;
6487 +               }
6488 +               pport=pport->next;
6489 +       }
6490 +       if(pport==NULL)
6491 +       {
6492 +               printk(KERN_NOTICE "%s: no port at %x found\n",
6493 +                      LIRC_DRIVER_NAME,io);
6494 +               return(-ENXIO);
6495 +       }
6496 +       ppdevice=parport_register_device(pport,LIRC_DRIVER_NAME,
6497 +                                        pf,kf,irq_handler,0,NULL);
6498 +       if(ppdevice==NULL)
6499 +       {
6500 +               printk(KERN_NOTICE "%s: parport_register_device() failed\n",
6501 +                      LIRC_DRIVER_NAME);
6502 +               return(-ENXIO);
6503 +       }
6504 +       if(parport_claim(ppdevice)!=0)
6505 +               goto skip_init;
6506 +       is_claimed=1;
6507 +       out(LIRC_LP_CONTROL,LP_PSELECP|LP_PINITP);
6508 +
6509 +#ifdef CONFIG_LIRC_TIMER
6510 +#       ifdef DEBUG
6511 +       out(LIRC_PORT_DATA,LIRC_PORT_DATA_BIT);
6512 +#       endif
6513 +
6514 +       timer=init_CONFIG_LIRC_TIMER();
6515 +
6516 +#       if 0   /* continue even if device is offline */
6517 +       if(timer==0)
6518 +       {
6519 +               is_claimed=0;
6520 +               parport_release(pport);
6521 +               parport_unregister_device(ppdevice);
6522 +               return(-EIO);
6523 +       }
6524 +
6525 +#       endif
6526 +#       ifdef DEBUG
6527 +       out(LIRC_PORT_DATA,0);
6528 +#       endif
6529 +#endif
6530 +
6531 +       is_claimed=0;
6532 +       parport_release(ppdevice);
6533 + skip_init:
6534 +       if ((plugin.minor = lirc_register_plugin(&plugin)) < 0)
6535 +       {
6536 +               printk(KERN_NOTICE "%s: register_chrdev() failed\n",LIRC_DRIVER_NAME);
6537 +               parport_unregister_device(ppdevice);
6538 +               return(-EIO);
6539 +       }
6540 +       printk(KERN_INFO "%s: installed using port 0x%04x irq %d\n",LIRC_DRIVER_NAME,io,irq);
6541 +       return(0);
6542 +}
6543 +
6544 +static void __exit lirc_parallel_exit(void)
6545 +{
6546 +#ifdef CONFIG_MODULE_UNLOAD
6547 +       if(module_refcount(THIS_MODULE)) return;
6548 +#endif
6549 +       parport_unregister_device(ppdevice);
6550 +       lirc_unregister_plugin(plugin.minor);
6551 +}
6552 +
6553 +static struct timer_list poll_timer;
6554 +static void poll_state(unsigned long ignored);
6555 +
6556 +static void poll_state(unsigned long ignored)
6557 +{
6558 +       printk(KERN_NOTICE "%s: time\n",
6559 +              LIRC_DRIVER_NAME);
6560 +       del_timer(&poll_timer);
6561 +       if(is_claimed)
6562 +               return;
6563 +       kf(NULL);
6564 +       if(!is_claimed)
6565 +       {
6566 +               printk(KERN_NOTICE "%s: could not claim port, giving up\n",
6567 +                      LIRC_DRIVER_NAME);
6568 +               init_timer(&poll_timer);
6569 +               poll_timer.expires=jiffies+HZ;
6570 +               poll_timer.data=(unsigned long) current;
6571 +               poll_timer.function=poll_state;
6572 +               add_timer(&poll_timer);
6573 +       }
6574 +}
6575 +
6576 +int pf(void *handle)
6577 +{
6578 +       pport->ops->disable_irq(pport);
6579 +       is_claimed=0;
6580 +       return(0);
6581 +}
6582 +
6583 +
6584 +void kf(void *handle)
6585 +{
6586 +       if(!is_open)
6587 +               return;
6588 +       if(!lirc_claim())
6589 +               return;
6590 +       pport->ops->enable_irq(pport);
6591 +       /* this is a bit annoying when you actually print...*/
6592 +       /*
6593 +       printk(KERN_INFO "%s: reclaimed port\n",LIRC_DRIVER_NAME);
6594 +       */
6595 +}
6596 +
6597 +MODULE_AUTHOR("Christoph Bartelmus");
6598 +MODULE_DESCRIPTION("Infrared receiver driver for parallel ports.");
6599 +MODULE_LICENSE("GPL");
6600 +
6601 +MODULE_PARM(io, "i");
6602 +MODULE_PARM_DESC(io, "I/O address base (0x3bc, 0x378 or 0x278)");
6603 +
6604 +MODULE_PARM(irq, "i");
6605 +MODULE_PARM_DESC(irq, "Interrupt (7 or 5)");
6606 +
6607 +module_init(lirc_parallel_init);
6608 +module_exit(lirc_parallel_exit);
6609 +
6610 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_parallel.h linux-2.6.5/drivers/char/lirc/lirc_parallel.h
6611 --- linux-2.6.5.orig/drivers/char/lirc/lirc_parallel.h  1970-01-01 01:00:00.000000000 +0100
6612 +++ linux-2.6.5/drivers/char/lirc/lirc_parallel.h       2004-04-04 14:05:29.425625896 +0200
6613 @@ -0,0 +1,24 @@
6614 +/*      $Id$      */
6615 +
6616 +#ifndef _LIRC_PARALLEL_H
6617 +#define _LIRC_PARALLEL_H
6618 +
6619 +#include <linux/lp.h>
6620 +
6621 +#define LIRC_PORT_LEN 3
6622 +
6623 +#define LIRC_LP_BASE    0
6624 +#define LIRC_LP_STATUS  1
6625 +#define LIRC_LP_CONTROL 2
6626 +
6627 +#define LIRC_PORT_DATA           LIRC_LP_BASE    /* base */
6628 +#define LIRC_PORT_DATA_BIT               0x01    /* 1st bit */
6629 +#define LIRC_PORT_TIMER        LIRC_LP_STATUS    /* status port */
6630 +#define LIRC_PORT_TIMER_BIT          LP_PBUSY    /* busy signal */
6631 +#define LIRC_PORT_SIGNAL       LIRC_LP_STATUS    /* status port */
6632 +#define LIRC_PORT_SIGNAL_BIT          LP_PACK    /* ack signal */
6633 +#define LIRC_PORT_IRQ         LIRC_LP_CONTROL    /* control port */
6634 +
6635 +#define LIRC_SFH506_DELAY 0             /* delay t_phl in usecs */
6636 +
6637 +#endif
6638 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_serial.c linux-2.6.5/drivers/char/lirc/lirc_serial.c
6639 --- linux-2.6.5.orig/drivers/char/lirc/lirc_serial.c    1970-01-01 01:00:00.000000000 +0100
6640 +++ linux-2.6.5/drivers/char/lirc/lirc_serial.c 2004-04-04 14:05:29.428625440 +0200
6641 @@ -0,0 +1,1056 @@
6642 +/*      $Id$      */
6643 +
6644 +/****************************************************************************
6645 + ** lirc_serial.c ***********************************************************
6646 + ****************************************************************************
6647 + *
6648 + * lirc_serial - Device driver that records pulse- and pause-lengths
6649 + *               (space-lengths) between DDCD event on a serial port.
6650 + *
6651 + * Copyright (C) 1996,97 Ralph Metzler <rjkm@thp.uni-koeln.de>
6652 + * Copyright (C) 1998 Trent Piepho <xyzzy@u.washington.edu>
6653 + * Copyright (C) 1998 Ben Pfaff <blp@gnu.org>
6654 + * Copyright (C) 1999 Christoph Bartelmus <lirc@bartelmus.de>
6655 + *
6656 + *  This program is free software; you can redistribute it and/or modify
6657 + *  it under the terms of the GNU General Public License as published by
6658 + *  the Free Software Foundation; either version 2 of the License, or
6659 + *  (at your option) any later version.
6660 + *
6661 + *  This program is distributed in the hope that it will be useful,
6662 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
6663 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6664 + *  GNU General Public License for more details.
6665 + *
6666 + *  You should have received a copy of the GNU General Public License
6667 + *  along with this program; if not, write to the Free Software
6668 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
6669 + *
6670 + */
6671 +
6672 +/* Steve's changes to improve transmission fidelity:
6673 +     - for systems with the rdtsc instruction and the clock counter, a
6674 +       send_pule that times the pulses directly using the counter.
6675 +       This means that the CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY fudge is
6676 +       not needed. Measurement shows very stable waveform, even where
6677 +       PCI activity slows the access to the UART, which trips up other
6678 +       versions.
6679 +     - For other system, non-integer-microsecond pulse/space lengths,
6680 +       done using fixed point binary. So, much more accurate carrier
6681 +       frequency.
6682 +     - fine tuned transmitter latency, taking advantage of fractional
6683 +       microseconds in previous change
6684 +     - Fixed bug in the way transmitter latency was accounted for by
6685 +       tuning the pulse lengths down - the send_pulse routine ignored
6686 +       this overhead as it timed the overall pulse length - so the
6687 +       pulse frequency was right but overall pulse length was too
6688 +       long. Fixed by accounting for latency on each pulse/space
6689 +       iteration.
6690 +
6691 +   Steve Davies <steve@daviesfam.org>  July 2001
6692 +
6693 +   Flameeyes Patches Contribution
6694 +    - Ronald Wahl <ronald.wahl@informatik.tu-chemnitz.de> sent a patch to
6695 +      eliminate a deadlock on SMP systems.
6696 +    - Florian Steinel <Florian.Steinel@t-online.de> sent a patch to fix irq
6697 +      disabling by kernel.
6698 +    - Jindrich Makovicka <makovick@kmlinux.fjfi.cvut.cz> sent a patch fixing
6699 +      one-shot use of lirc_serial.
6700 +*/
6701 +
6702 +#include <linux/config.h>
6703 +#include <linux/module.h>
6704 +#include <linux/errno.h>
6705 +#include <linux/signal.h>
6706 +#include <linux/sched.h>
6707 +#include <linux/fs.h>
6708 +#include <linux/interrupt.h>
6709 +#include <linux/ioport.h>
6710 +#include <linux/kernel.h>
6711 +#include <linux/major.h>
6712 +#include <linux/serial_reg.h>
6713 +#include <linux/time.h>
6714 +#include <linux/string.h>
6715 +#include <linux/types.h>
6716 +#include <linux/wait.h>
6717 +#include <linux/mm.h>
6718 +#include <linux/delay.h>
6719 +#include <linux/poll.h>
6720 +#include <linux/smp_lock.h>
6721 +
6722 +#include <asm/system.h>
6723 +#include <asm/segment.h>
6724 +#include <asm/io.h>
6725 +#include <asm/irq.h>
6726 +#include <asm/fcntl.h>
6727 +
6728 +#include <linux/lirc.h>
6729 +#include "lirc_dev.h"
6730 +
6731 +#if defined(rdtsc)
6732 +
6733 +#define USE_RDTSC
6734 +#warning "Note: using rdtsc instruction"
6735 +#endif
6736 +
6737 +struct lirc_serial
6738 +{
6739 +       int type;
6740 +       int signal_pin;
6741 +       int signal_pin_change;
6742 +       int on;
6743 +       int off;
6744 +       long (*send_pulse)(unsigned long length);
6745 +       void (*send_space)(long length);
6746 +       int features;
6747 +};
6748 +
6749 +#define LIRC_HOMEBREW        0
6750 +#define LIRC_IRDEO           1
6751 +#define LIRC_IRDEO_REMOTE    2
6752 +#define LIRC_ANIMAX          3
6753 +#define LIRC_IGOR            4
6754 +
6755 +#ifdef CONFIG_LIRC_SERIAL_IRDEO
6756 +int type=LIRC_IRDEO;
6757 +#elif defined(CONFIG_LIRC_SERIAL_IRDEO_REMOTE)
6758 +int type=LIRC_IRDEO_REMOTE;
6759 +#elif defined(CONFIG_LIRC_SERIAL_ANIMAX)
6760 +int type=LIRC_ANIMAX;
6761 +#elif defined(CONFIG_LIRC_SERIAL_IGOR)
6762 +int type=LIRC_IGOR;
6763 +#else
6764 +int type=LIRC_HOMEBREW;
6765 +#endif
6766 +
6767 +#ifdef CONFIG_LIRC_SERIAL_SOFTCARRIER
6768 +int softcarrier=1;
6769 +#else
6770 +int softcarrier=0;
6771 +#endif
6772 +
6773 +static int sense = -1;   /* -1 = auto, 0 = active high, 1 = active low */
6774 +
6775 +static int io = CONFIG_LIRC_PORT_SERIAL;
6776 +
6777 +static int irq = CONFIG_LIRC_IRQ_SERIAL;
6778 +
6779 +static int debug = 0;
6780 +
6781 +MODULE_PARM(type, "i");
6782 +MODULE_PARM_DESC(type, "Hardware type (0 = home-brew, 1 = IRdeo,"
6783 +                " 2 = IRdeo Remote, 3 = AnimaX");
6784 +
6785 +MODULE_PARM(io, "i");
6786 +MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
6787 +
6788 +MODULE_PARM(irq, "i");
6789 +MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
6790 +
6791 +MODULE_PARM(sense, "i");
6792 +MODULE_PARM_DESC(sense, "Override autodetection of IR receiver circuit"
6793 +                " (0 = active high, 1 = active low )");
6794 +
6795 +MODULE_PARM(softcarrier, "i");
6796 +MODULE_PARM_DESC(softcarrier, "Software carrier (0 = off, 1 = on)");
6797 +
6798 +MODULE_PARM(debug,"i");
6799 +
6800 +#define dprintk           if (debug) printk
6801 +
6802 +#define LOGHEAD           "lirc_serial: "
6803 +
6804 +/* forward declarations */
6805 +long send_pulse_irdeo(unsigned long length);
6806 +long send_pulse_homebrew(unsigned long length);
6807 +void send_space_irdeo(long length);
6808 +void send_space_homebrew(long length);
6809 +
6810 +struct lirc_serial hardware[]=
6811 +{
6812 +       /* home-brew receiver/transmitter */
6813 +       {
6814 +               LIRC_HOMEBREW,
6815 +               UART_MSR_DCD,
6816 +               UART_MSR_DDCD,
6817 +               UART_MCR_RTS|UART_MCR_OUT2|UART_MCR_DTR,
6818 +               UART_MCR_RTS|UART_MCR_OUT2,
6819 +               send_pulse_homebrew,
6820 +               send_space_homebrew,
6821 +               (
6822 +#ifdef CONFIG_LIRC_SERIAL_TRANSMITTER
6823 +                LIRC_CAN_SET_SEND_DUTY_CYCLE|
6824 +                LIRC_CAN_SET_SEND_CARRIER|
6825 +                LIRC_CAN_SEND_PULSE|
6826 +#endif
6827 +                LIRC_CAN_REC_MODE2)
6828 +       },
6829 +
6830 +       /* IRdeo classic */
6831 +       {
6832 +               LIRC_IRDEO,
6833 +               UART_MSR_DSR,
6834 +               UART_MSR_DDSR,
6835 +               UART_MCR_OUT2,
6836 +               UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
6837 +               send_pulse_irdeo,
6838 +               send_space_irdeo,
6839 +               (LIRC_CAN_SET_SEND_DUTY_CYCLE|
6840 +                LIRC_CAN_SEND_PULSE|
6841 +                LIRC_CAN_REC_MODE2)
6842 +       },
6843 +
6844 +       /* IRdeo remote */
6845 +       {
6846 +               LIRC_IRDEO_REMOTE,
6847 +               UART_MSR_DSR,
6848 +               UART_MSR_DDSR,
6849 +               UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
6850 +               UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
6851 +               send_pulse_irdeo,
6852 +               send_space_irdeo,
6853 +               (LIRC_CAN_SET_SEND_DUTY_CYCLE|
6854 +                LIRC_CAN_SEND_PULSE|
6855 +                LIRC_CAN_REC_MODE2)
6856 +       },
6857 +
6858 +       /* AnimaX */
6859 +       {
6860 +               LIRC_ANIMAX,
6861 +               UART_MSR_DCD,
6862 +               UART_MSR_DDCD,
6863 +               0,
6864 +               UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2,
6865 +               NULL,
6866 +               NULL,
6867 +               LIRC_CAN_REC_MODE2
6868 +       },
6869 +
6870 +       /* home-brew receiver/transmitter (Igor Cesko's variation) */
6871 +       {
6872 +               LIRC_HOMEBREW,
6873 +               UART_MSR_DSR,
6874 +               UART_MSR_DDSR,
6875 +               UART_MCR_RTS|UART_MCR_OUT2|UART_MCR_DTR,
6876 +               UART_MCR_RTS|UART_MCR_OUT2,
6877 +               send_pulse_homebrew,
6878 +               send_space_homebrew,
6879 +               (
6880 +#ifdef CONFIG_LIRC_SERIAL_TRANSMITTER
6881 +                LIRC_CAN_SET_SEND_DUTY_CYCLE|
6882 +                LIRC_CAN_SET_SEND_CARRIER|
6883 +                LIRC_CAN_SEND_PULSE|
6884 +#endif
6885 +                LIRC_CAN_REC_MODE2)
6886 +       }
6887 +
6888 +};
6889 +
6890 +#define LIRC_DRIVER_NAME "lirc_serial"
6891 +
6892 +#define RS_ISR_PASS_LIMIT 256
6893 +
6894 +/* A long pulse code from a remote might take upto 300 bytes.  The
6895 +   daemon should read the bytes as soon as they are generated, so take
6896 +   the number of keys you think you can push before the daemon runs
6897 +   and multiply by 300.  The driver will warn you if you overrun this
6898 +   buffer.  If you have a slow computer or non-busmastering IDE disks,
6899 +   maybe you will need to increase this.  */
6900 +
6901 +/* This MUST be a power of two!  It has to be larger than 1 as well. */
6902 +
6903 +#define RBUF_LEN 256
6904 +#define WBUF_LEN 256
6905 +
6906 +static struct timeval lasttv = {0, 0};
6907 +
6908 +static spinlock_t lirc_lock = SPIN_LOCK_UNLOCKED;
6909 +
6910 +static struct lirc_buffer rbuf;
6911 +
6912 +static lirc_t wbuf[WBUF_LEN];
6913 +
6914 +unsigned int freq = 38000;
6915 +unsigned int duty_cycle = 50;
6916 +
6917 +/* Initialized in init_timing_params() */
6918 +unsigned long period = 0;
6919 +unsigned long pulse_width = 0;
6920 +unsigned long space_width = 0;
6921 +
6922 +#if defined(__i386__)
6923 +/*
6924 +  From:
6925 +  Linux I/O port programming mini-HOWTO
6926 +  Author: Riku Saikkonen <Riku.Saikkonen@hut.fi>
6927 +  v, 28 December 1997
6928 +
6929 +  [...]
6930 +  Actually, a port I/O instruction on most ports in the 0-0x3ff range
6931 +  takes almost exactly 1 microsecond, so if you're, for example, using
6932 +  the parallel port directly, just do additional inb()s from that port
6933 +  to delay.
6934 +  [...]
6935 +*/
6936 +/* transmitter latency 1.5625us 0x1.90 - this figure arrived at from
6937 + * comment above plus trimming to match actual measured frequency.
6938 + * This will be sensitive to cpu speed, though hopefully most of the 1.5us
6939 + * is spent in the uart access.  Still - for reference test machine was a
6940 + * 1.13GHz Athlon system - Steve
6941 + */
6942 +
6943 +/* changed from 400 to 450 as this works better on slower machines;
6944 +   faster machines will use the rdtsc code anyway */
6945 +
6946 +#define CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY 450
6947 +
6948 +#else
6949 +
6950 +/* does anybody have information on other platforms ? */
6951 +/* 256 = 1<<8 */
6952 +#define CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY 256
6953 +
6954 +#endif  /* __i386__ */
6955 +
6956 +static inline unsigned int sinp(int offset)
6957 +{
6958 +       return inb(io + offset);
6959 +}
6960 +
6961 +static inline void soutp(int offset, int value)
6962 +{
6963 +       outb(value, io + offset);
6964 +}
6965 +
6966 +static inline void on(void)
6967 +{
6968 +       soutp(UART_MCR,hardware[type].on);
6969 +}
6970 +
6971 +static inline void off(void)
6972 +{
6973 +       soutp(UART_MCR,hardware[type].off);
6974 +}
6975 +
6976 +#ifndef MAX_UDELAY_MS
6977 +#define MAX_UDELAY_US 5000
6978 +#else
6979 +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
6980 +#endif
6981 +
6982 +static inline void safe_udelay(unsigned long usecs)
6983 +{
6984 +       while(usecs>MAX_UDELAY_US)
6985 +       {
6986 +               udelay(MAX_UDELAY_US);
6987 +               usecs-=MAX_UDELAY_US;
6988 +       }
6989 +       udelay(usecs);
6990 +}
6991 +
6992 +#ifdef USE_RDTSC
6993 +/* This is an overflow/precision juggle, complicated in that we can't
6994 +   do long long divide in the kernel */
6995 +
6996 +/* When we use the rdtsc instruction to measure clocks, we keep the
6997 + * pulse and space widths as clock cycles.  As this is CPU speed
6998 + * dependent, the widths must be calculated in init_port and ioctl
6999 + * time
7000 + */
7001 +
7002 +/* So send_pulse can quickly convert microseconds to clocks */
7003 +unsigned long conv_us_to_clocks = 0;
7004 +
7005 +static inline int init_timing_params(unsigned int new_duty_cycle,
7006 +               unsigned int new_freq)
7007 +{
7008 +       unsigned long long loops_per_sec,work;
7009 +
7010 +       duty_cycle=new_duty_cycle;
7011 +       freq=new_freq;
7012 +
7013 +       loops_per_sec=current_cpu_data.loops_per_jiffy;
7014 +       loops_per_sec*=HZ;
7015 +
7016 +       /* How many clocks in a microsecond?, avoiding long long divide */
7017 +       work=loops_per_sec;
7018 +       work*=4295;  /* 4295 = 2^32 / 1e6 */
7019 +       conv_us_to_clocks=(work>>32);
7020 +
7021 +       /* Carrier period in clocks, approach good up to 32GHz clock,
7022 +           gets carrier frequency within 8Hz */
7023 +       period=loops_per_sec>>3;
7024 +       period/=(freq>>3);
7025 +
7026 +       /* Derive pulse and space from the period */
7027 +
7028 +       pulse_width = period*duty_cycle/100;
7029 +       space_width = period - pulse_width;
7030 +       dprintk(LOGHEAD
7031 +              ": in init_timing_params, freq=%d, duty_cycle=%d, "
7032 +              "clk/jiffy=%ld, pulse=%ld, space=%ld, conv_us_to_clocks=%ld\n",
7033 +              freq, duty_cycle, current_cpu_data.loops_per_jiffy,
7034 +              pulse_width, space_width, conv_us_to_clocks);
7035 +       return 0;
7036 +}
7037 +#else /* ! USE_RDTSC */
7038 +static inline int init_timing_params(unsigned int new_duty_cycle,
7039 +               unsigned int new_freq)
7040 +{
7041 +/* period, pulse/space width are kept with 8 binary places -
7042 + * IE multiplied by 256. */
7043 +       if(256*1000000L/new_freq*new_duty_cycle/100<=
7044 +          CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY) return(-EINVAL);
7045 +       if(256*1000000L/new_freq*(100-new_duty_cycle)/100<=
7046 +          CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY) return(-EINVAL);
7047 +       duty_cycle=new_duty_cycle;
7048 +       freq=new_freq;
7049 +       period=256*1000000L/freq;
7050 +       pulse_width=period*duty_cycle/100;
7051 +       space_width=period-pulse_width;
7052 +       dprintk(LOGHEAD
7053 +              ": in init_timing_params, freq=%d pulse=%ld, "
7054 +              "space=%ld\n", freq, pulse_width, space_width);
7055 +       return 0;
7056 +}
7057 +#endif /* USE_RDTSC */
7058 +
7059 +
7060 +/* return value: space length delta */
7061 +
7062 +long send_pulse_irdeo(unsigned long length)
7063 +{
7064 +       long rawbits;
7065 +       int i;
7066 +       unsigned char output;
7067 +       unsigned char chunk,shifted;
7068 +
7069 +       /* how many bits have to be sent ? */
7070 +       rawbits=length*1152/10000;
7071 +       if(duty_cycle>50) chunk=3;
7072 +       else chunk=1;
7073 +       for(i=0,output=0x7f;rawbits>0;rawbits-=3)
7074 +       {
7075 +               shifted=chunk<<(i*3);
7076 +               shifted>>=1;
7077 +               output&=(~shifted);
7078 +               i++;
7079 +               if(i==3)
7080 +               {
7081 +                       soutp(UART_TX,output);
7082 +                       while(!(sinp(UART_LSR) & UART_LSR_THRE));
7083 +                       output=0x7f;
7084 +                       i=0;
7085 +               }
7086 +       }
7087 +       if(i!=0)
7088 +       {
7089 +               soutp(UART_TX,output);
7090 +               while(!(sinp(UART_LSR) & UART_LSR_TEMT));
7091 +       }
7092 +
7093 +       if(i==0)
7094 +       {
7095 +               return((-rawbits)*10000/1152);
7096 +       }
7097 +       else
7098 +       {
7099 +               return((3-i)*3*10000/1152+(-rawbits)*10000/1152);
7100 +       }
7101 +}
7102 +
7103 +#ifdef USE_RDTSC
7104 +/* Version that uses Pentium rdtsc instruction to measure clocks */
7105 +
7106 +/* This version does sub-microsecond timing using rdtsc instruction,
7107 + * and does away with the fudged CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY
7108 + * Implicitly i586 architecture...  - Steve
7109 + */
7110 +
7111 +static inline long send_pulse_homebrew_softcarrier(unsigned long length)
7112 +{
7113 +       int flag;
7114 +       unsigned long target, start, now;
7115 +
7116 +       /* Get going quick as we can */
7117 +       rdtscl(start);on();
7118 +       /* Convert length from microseconds to clocks */
7119 +       length*=conv_us_to_clocks;
7120 +       /* And loop till time is up - flipping at right intervals */
7121 +       now=start;
7122 +       target=pulse_width;
7123 +       flag=1;
7124 +       while((now-start)<length)
7125 +       {
7126 +               /* Delay till flip time */
7127 +               do
7128 +               {
7129 +                       rdtscl(now);
7130 +               }
7131 +               while ((now-start)<target);
7132 +               /* flip */
7133 +               if(flag)
7134 +               {
7135 +                       rdtscl(now);off();
7136 +                       target+=space_width;
7137 +               }
7138 +               else
7139 +               {
7140 +                       rdtscl(now);on();
7141 +                       target+=pulse_width;
7142 +               }
7143 +               flag=!flag;
7144 +       }
7145 +       rdtscl(now);
7146 +       return(((now-start)-length)/conv_us_to_clocks);
7147 +}
7148 +#else /* ! USE_RDTSC */
7149 +/* Version using udelay() */
7150 +
7151 +/* here we use fixed point arithmetic, with 8
7152 +   fractional bits.  that gets us within 0.1% or so of the right average
7153 +   frequency, albeit with some jitter in pulse length - Steve */
7154 +
7155 +/* To match 8 fractional bits used for pulse/space length */
7156 +
7157 +static inline long send_pulse_homebrew_softcarrier(unsigned long length)
7158 +{
7159 +       int flag;
7160 +       unsigned long actual, target, d;
7161 +       length<<=8;
7162 +
7163 +       actual=target=0; flag=0;
7164 +       while(actual<length)
7165 +       {
7166 +               if(flag)
7167 +               {
7168 +                       off();
7169 +                       target+=space_width;
7170 +               }
7171 +               else
7172 +               {
7173 +                       on();
7174 +                       target+=pulse_width;
7175 +               }
7176 +               d=(target-actual-CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY+128)>>8;
7177 +               /* Note - we've checked in ioctl that the pulse/space
7178 +                  widths are big enough so that d is > 0 */
7179 +               udelay(d);
7180 +               actual+=(d<<8)+CONFIG_LIRC_SERIAL_TRANSMITTER_LATENCY;
7181 +               flag=!flag;
7182 +       }
7183 +       return((actual-length)>>8);
7184 +}
7185 +#endif /* USE_RDTSC */
7186 +
7187 +long send_pulse_homebrew(unsigned long length)
7188 +{
7189 +       if(length<=0) return 0;
7190 +       if(softcarrier)
7191 +       {
7192 +               return send_pulse_homebrew_softcarrier(length);
7193 +       }
7194 +       else
7195 +       {
7196 +               on();
7197 +               safe_udelay(length);
7198 +               return(0);
7199 +       }
7200 +}
7201 +
7202 +void send_space_irdeo(long length)
7203 +{
7204 +       if(length<=0) return;
7205 +       safe_udelay(length);
7206 +}
7207 +
7208 +void send_space_homebrew(long length)
7209 +{
7210 +        off();
7211 +       if(length<=0) return;
7212 +       safe_udelay(length);
7213 +}
7214 +
7215 +static void inline rbwrite(lirc_t l)
7216 +{
7217 +       if(lirc_buffer_full(&rbuf))    /* no new signals will be accepted */
7218 +       {
7219 +               dprintk(LOGHEAD ": Buffer overrun\n");
7220 +               return;
7221 +       }
7222 +       _lirc_buffer_write_1(&rbuf, (void *)&l);
7223 +}
7224 +
7225 +static void inline frbwrite(lirc_t l)
7226 +{
7227 +       /* simple noise filter */
7228 +       static lirc_t pulse=0L,space=0L;
7229 +       static unsigned int ptr=0;
7230 +
7231 +       if(ptr>0 && (l&PULSE_BIT))
7232 +       {
7233 +               pulse+=l&PULSE_MASK;
7234 +               if(pulse>250)
7235 +               {
7236 +                       rbwrite(space);
7237 +                       rbwrite(pulse|PULSE_BIT);
7238 +                       ptr=0;
7239 +                       pulse=0;
7240 +               }
7241 +               return;
7242 +       }
7243 +       if(!(l&PULSE_BIT))
7244 +       {
7245 +               if(ptr==0)
7246 +               {
7247 +                       if(l>20000)
7248 +                       {
7249 +                               space=l;
7250 +                               ptr++;
7251 +                               return;
7252 +                       }
7253 +               }
7254 +               else
7255 +               {
7256 +                       if(l>20000)
7257 +                       {
7258 +                               space+=pulse;
7259 +                               if(space>PULSE_MASK) space=PULSE_MASK;
7260 +                               space+=l;
7261 +                               if(space>PULSE_MASK) space=PULSE_MASK;
7262 +                               pulse=0;
7263 +                               return;
7264 +                       }
7265 +                       rbwrite(space);
7266 +                       rbwrite(pulse|PULSE_BIT);
7267 +                       ptr=0;
7268 +                       pulse=0;
7269 +               }
7270 +       }
7271 +       rbwrite(l);
7272 +}
7273 +
7274 +irqreturn_t irq_handler(int i, void *blah, struct pt_regs *regs)
7275 +{
7276 +       struct timeval tv;
7277 +       int status,counter,dcd;
7278 +       long deltv;
7279 +       lirc_t data;
7280 +
7281 +       counter=0;
7282 +       do{
7283 +               counter++;
7284 +               status=sinp(UART_MSR);
7285 +               if(counter>RS_ISR_PASS_LIMIT)
7286 +               {
7287 +                       printk(KERN_WARNING LIRC_DRIVER_NAME ": AIEEEE: "
7288 +                              "We're caught!\n");
7289 +                       break;
7290 +               }
7291 +               if((status&hardware[type].signal_pin_change) && sense!=-1)
7292 +               {
7293 +                       /* get current time */
7294 +                       do_gettimeofday(&tv);
7295 +
7296 +                       /* New mode, written by Trent Piepho
7297 +                          <xyzzy@u.washington.edu>. */
7298 +
7299 +                       /* The old format was not very portable.
7300 +                          We now use the type lirc_t to pass pulses
7301 +                          and spaces to user space.
7302 +
7303 +                          If PULSE_BIT is set a pulse has been
7304 +                          received, otherwise a space has been
7305 +                          received.  The driver needs to know if your
7306 +                          receiver is active high or active low, or
7307 +                          the space/pulse sense could be
7308 +                          inverted. The bits denoted by PULSE_MASK are
7309 +                          the length in microseconds. Lengths greater
7310 +                          than or equal to 16 seconds are clamped to
7311 +                          PULSE_MASK.  All other bits are unused.
7312 +                          This is a much simpler interface for user
7313 +                          programs, as well as eliminating "out of
7314 +                          phase" errors with space/pulse
7315 +                          autodetection. */
7316 +
7317 +                       /* calculate time since last interrupt in
7318 +                          microseconds */
7319 +                       dcd=(status & hardware[type].signal_pin) ? 1:0;
7320 +
7321 +                       deltv=tv.tv_sec-lasttv.tv_sec;
7322 +                       if(deltv>15)
7323 +                       {
7324 +                               dprintk(LOGHEAD
7325 +                                      ": AIEEEE: %d %d %lx %lx %lx %lx\n",
7326 +                                      dcd,sense,
7327 +                                      tv.tv_sec,lasttv.tv_sec,
7328 +                                      tv.tv_usec,lasttv.tv_usec);
7329 +                               data=PULSE_MASK; /* really long time */
7330 +                               if(!(dcd^sense)) /* sanity check */
7331 +                               {
7332 +                                       /* detecting pulse while this
7333 +                                          MUST be a space! */
7334 +                                       sense=sense ? 0:1;
7335 +                               }
7336 +                       }
7337 +                       else
7338 +                       {
7339 +                               data=(lirc_t) (deltv*1000000+
7340 +                                              tv.tv_usec-
7341 +                                              lasttv.tv_usec);
7342 +                       };
7343 +                       if(tv.tv_sec<lasttv.tv_sec ||
7344 +                          (tv.tv_sec==lasttv.tv_sec &&
7345 +                           tv.tv_usec<lasttv.tv_usec))
7346 +                       {
7347 +                               printk(KERN_WARNING LIRC_DRIVER_NAME
7348 +                                      ": AIEEEE: your clock just jumped "
7349 +                                      "backwards\n");
7350 +                               printk(KERN_WARNING LIRC_DRIVER_NAME
7351 +                                      ": %d %d %lx %lx %lx %lx\n",
7352 +                                      dcd,sense,
7353 +                                      tv.tv_sec,lasttv.tv_sec,
7354 +                                      tv.tv_usec,lasttv.tv_usec);
7355 +                               data=PULSE_MASK;
7356 +                       }
7357 +                       frbwrite(dcd^sense ? data : (data|PULSE_BIT));
7358 +                       lasttv=tv;
7359 +                       wake_up_interruptible(&rbuf.wait_poll);
7360 +               }
7361 +       } while(!(sinp(UART_IIR) & UART_IIR_NO_INT)); /* still pending ? */
7362 +
7363 +       return IRQ_HANDLED;
7364 +}
7365 +
7366 +static DECLARE_WAIT_QUEUE_HEAD(power_supply_queue);
7367 +static spinlock_t lirc_lock;
7368 +
7369 +static int init_port(void)
7370 +{
7371 +       unsigned long flags;
7372 +
7373 +       /* Reserve io region. */
7374 +       if(!request_region(io, 8, LIRC_DRIVER_NAME))
7375 +       {
7376 +               printk(KERN_ERR  LIRC_DRIVER_NAME
7377 +                      ": port %04x already in use\n", io);
7378 +               printk(KERN_WARNING LIRC_DRIVER_NAME
7379 +                      ": use 'setserial /dev/ttySX uart none'\n");
7380 +               printk(KERN_WARNING LIRC_DRIVER_NAME
7381 +                      ": or compile the serial port driver as module and\n");
7382 +               printk(KERN_WARNING LIRC_DRIVER_NAME
7383 +                      ": make sure this module is loaded first\n");
7384 +               return(-EBUSY);
7385 +       }
7386 +
7387 +       lock_kernel();
7388 +
7389 +       spin_lock_irqsave(&lirc_lock, flags);
7390 +
7391 +       /* Set DLAB 0. */
7392 +       soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
7393 +
7394 +       /* First of all, disable all interrupts */
7395 +       soutp(UART_IER, sinp(UART_IER)&
7396 +             (~(UART_IER_MSI|UART_IER_RLSI|UART_IER_THRI|UART_IER_RDI)));
7397 +
7398 +       /* Clear registers. */
7399 +       sinp(UART_LSR);
7400 +       sinp(UART_RX);
7401 +       sinp(UART_IIR);
7402 +       sinp(UART_MSR);
7403 +
7404 +       /* Set line for power source */
7405 +       soutp(UART_MCR, hardware[type].off);
7406 +
7407 +       /* Clear registers again to be sure. */
7408 +       sinp(UART_LSR);
7409 +       sinp(UART_RX);
7410 +       sinp(UART_IIR);
7411 +       sinp(UART_MSR);
7412 +
7413 +       switch(hardware[type].type)
7414 +       {
7415 +       case LIRC_IRDEO:
7416 +       case LIRC_IRDEO_REMOTE:
7417 +               /* setup port to 7N1 @ 115200 Baud */
7418 +               /* 7N1+start = 9 bits at 115200 ~ 3 bits at 38kHz */
7419 +
7420 +               /* Set DLAB 1. */
7421 +               soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
7422 +               /* Set divisor to 1 => 115200 Baud */
7423 +               soutp(UART_DLM,0);
7424 +               soutp(UART_DLL,1);
7425 +               /* Set DLAB 0 +  7N1 */
7426 +               soutp(UART_LCR,UART_LCR_WLEN7);
7427 +               /* THR interrupt already disabled at this point */
7428 +               break;
7429 +       default:
7430 +               break;
7431 +       }
7432 +
7433 +       spin_unlock_irqrestore(&lirc_lock, flags);
7434 +
7435 +       /* Initialize pulse/space widths */
7436 +       init_timing_params(duty_cycle, freq);
7437 +
7438 +       /* If pin is high, then this must be an active low receiver. */
7439 +       if(sense==-1)
7440 +       {
7441 +               /* wait 1 sec for the power supply */
7442 +
7443 +               sleep_on_timeout(&power_supply_queue,HZ);
7444 +
7445 +               sense=(sinp(UART_MSR) & hardware[type].signal_pin) ? 1:0;
7446 +               printk(KERN_INFO  LIRC_DRIVER_NAME  ": auto-detected active "
7447 +                      "%s receiver\n",sense ? "low":"high");
7448 +       }
7449 +       else
7450 +       {
7451 +               printk(KERN_INFO  LIRC_DRIVER_NAME  ": Manually using active "
7452 +                      "%s receiver\n",sense ? "low":"high");
7453 +       };
7454 +
7455 +       unlock_kernel();
7456 +
7457 +       return 0;
7458 +}
7459 +
7460 +static int set_use_inc(void* data)
7461 +{
7462 +       int result;
7463 +       unsigned long flags;
7464 +
7465 +       spin_lock(&lirc_lock);
7466 +#ifdef CONFIG_MODULE_UNLOAD
7467 +       if(module_refcount(THIS_MODULE))
7468 +       {
7469 +               spin_unlock(&lirc_lock);
7470 +               return -EBUSY;
7471 +       }
7472 +#endif
7473 +
7474 +       /* initialize timestamp */
7475 +       do_gettimeofday(&lasttv);
7476 +
7477 +       result=request_irq(irq,irq_handler,SA_INTERRUPT,LIRC_DRIVER_NAME,NULL);
7478 +       switch(result)
7479 +       {
7480 +       case -EBUSY:
7481 +               printk(KERN_ERR LIRC_DRIVER_NAME ": IRQ %d busy\n", irq);
7482 +               spin_unlock(&lirc_lock);
7483 +               return -EBUSY;
7484 +       case -EINVAL:
7485 +               printk(KERN_ERR LIRC_DRIVER_NAME
7486 +                      ": Bad irq number or handler\n");
7487 +               spin_unlock(&lirc_lock);
7488 +               return -EINVAL;
7489 +       default:
7490 +               dprintk(LOGHEAD
7491 +                      ": Interrupt %d, port %04x obtained\n", irq, io);
7492 +               break;
7493 +       };
7494 +
7495 +       local_irq_save(flags);
7496 +
7497 +       /* Set DLAB 0. */
7498 +       soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
7499 +
7500 +       soutp(UART_IER, sinp(UART_IER)|UART_IER_MSI);
7501 +
7502 +       local_irq_restore(flags);
7503 +
7504 +       try_module_get(THIS_MODULE);
7505 +       spin_unlock(&lirc_lock);
7506 +       return 0;
7507 +}
7508 +
7509 +static void set_use_dec(void* data)
7510 +{      unsigned long flags;
7511 +
7512 +       spin_lock_irqsave(&lirc_lock, flags);
7513 +
7514 +       /* Set DLAB 0. */
7515 +       soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
7516 +
7517 +       /* First of all, disable all interrupts */
7518 +       soutp(UART_IER, sinp(UART_IER)&
7519 +             (~(UART_IER_MSI|UART_IER_RLSI|UART_IER_THRI|UART_IER_RDI)));
7520 +       spin_unlock_irqrestore(&lirc_lock, flags);
7521 +
7522 +       free_irq(irq, NULL);
7523 +       dprintk(LOGHEAD ": freed IRQ %d\n", irq);
7524 +
7525 +       module_put(THIS_MODULE);
7526 +}
7527 +
7528 +static ssize_t lirc_write(struct file *file, const char *buf,
7529 +                        size_t n, loff_t * ppos)
7530 +{
7531 +       int retval,i,count;
7532 +       unsigned long flags;
7533 +       long delta=0;
7534 +
7535 +       if(!(hardware[type].features&LIRC_CAN_SEND_PULSE))
7536 +       {
7537 +               return(-EBADF);
7538 +       }
7539 +
7540 +       if(n%sizeof(lirc_t)) return(-EINVAL);
7541 +       retval=verify_area(VERIFY_READ,buf,n);
7542 +       if(retval) return(retval);
7543 +       count=n/sizeof(lirc_t);
7544 +       if(count>WBUF_LEN || count%2==0) return(-EINVAL);
7545 +       copy_from_user(wbuf,buf,n);
7546 +       spin_lock_irqsave(&lirc_lock, flags);
7547 +       if(hardware[type].type==LIRC_IRDEO)
7548 +       {
7549 +               /* DTR, RTS down */
7550 +               on();
7551 +       }
7552 +       for(i=0;i<count;i++)
7553 +       {
7554 +               if(i%2) hardware[type].send_space(wbuf[i]-delta);
7555 +               else delta=hardware[type].send_pulse(wbuf[i]);
7556 +       }
7557 +       off();
7558 +       spin_unlock_irqrestore(&lirc_lock, flags);
7559 +       return(n);
7560 +}
7561 +
7562 +static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
7563 +                     unsigned long arg)
7564 +{
7565 +        int result;
7566 +       unsigned long value;
7567 +       unsigned int ivalue;
7568 +
7569 +       switch(cmd)
7570 +       {
7571 +       case LIRC_GET_SEND_MODE:
7572 +               if(!(hardware[type].features&LIRC_CAN_SEND_MASK))
7573 +               {
7574 +                       return(-ENOIOCTLCMD);
7575 +               }
7576 +
7577 +               result=put_user(LIRC_SEND2MODE
7578 +                               (hardware[type].features&LIRC_CAN_SEND_MASK),
7579 +                               (unsigned long *) arg);
7580 +               if(result) return(result);
7581 +               break;
7582 +
7583 +       case LIRC_SET_SEND_MODE:
7584 +               if(!(hardware[type].features&LIRC_CAN_SEND_MASK))
7585 +               {
7586 +                       return(-ENOIOCTLCMD);
7587 +               }
7588 +
7589 +               result=get_user(value,(unsigned long *) arg);
7590 +               if(result) return(result);
7591 +               /* only LIRC_MODE_PULSE supported */
7592 +               if(value!=LIRC_MODE_PULSE) return(-ENOSYS);
7593 +               break;
7594 +
7595 +       case LIRC_GET_LENGTH:
7596 +               return(-ENOSYS);
7597 +               break;
7598 +
7599 +       case LIRC_SET_SEND_DUTY_CYCLE:
7600 +               dprintk(LOGHEAD ": SET_SEND_DUTY_CYCLE\n");
7601 +               if(!(hardware[type].features&LIRC_CAN_SET_SEND_DUTY_CYCLE))
7602 +               {
7603 +                       return(-ENOIOCTLCMD);
7604 +               }
7605 +
7606 +               result=get_user(ivalue,(unsigned int *) arg);
7607 +               if(result) return(result);
7608 +               if(ivalue<=0 || ivalue>100) return(-EINVAL);
7609 +               return init_timing_params(ivalue, freq);
7610 +               break;
7611 +
7612 +       case LIRC_SET_SEND_CARRIER:
7613 +               dprintk(LOGHEAD ": SET_SEND_CARRIER\n");
7614 +               if(!(hardware[type].features&LIRC_CAN_SET_SEND_CARRIER))
7615 +               {
7616 +                       return(-ENOIOCTLCMD);
7617 +               }
7618 +
7619 +               result=get_user(ivalue,(unsigned int *) arg);
7620 +               if(result) return(result);
7621 +               if(ivalue>500000 || ivalue<20000) return(-EINVAL);
7622 +               return init_timing_params(duty_cycle, ivalue);
7623 +               break;
7624 +
7625 +       default:
7626 +               return(-ENOIOCTLCMD);
7627 +       }
7628 +       return(0);
7629 +}
7630 +
7631 +static struct file_operations lirc_fops =
7632 +{
7633 +       write:   lirc_write,
7634 +};
7635 +
7636 +static struct lirc_plugin plugin = {
7637 +       name:           LIRC_DRIVER_NAME,
7638 +       minor:          -1,
7639 +       code_length:    1,
7640 +       sample_rate:    0,
7641 +       data:           NULL,
7642 +       add_to_buf:     NULL,
7643 +       get_queue:      NULL,
7644 +       rbuf:           &rbuf,
7645 +       set_use_inc:    set_use_inc,
7646 +       set_use_dec:    set_use_dec,
7647 +       ioctl:          lirc_ioctl,
7648 +       fops:           &lirc_fops,
7649 +};
7650 +
7651 +MODULE_AUTHOR("Ralph Metzler, Trent Piepho, Ben Pfaff, Christoph Bartelmus");
7652 +MODULE_DESCRIPTION("Infra-red receiver driver for serial ports.");
7653 +MODULE_LICENSE("GPL");
7654 +
7655 +static int __init lirc_serial_init(void)
7656 +{
7657 +       int result;
7658 +
7659 +       switch(type)
7660 +       {
7661 +       case LIRC_HOMEBREW:
7662 +       case LIRC_IRDEO:
7663 +       case LIRC_IRDEO_REMOTE:
7664 +       case LIRC_ANIMAX:
7665 +       case LIRC_IGOR:
7666 +               break;
7667 +       default:
7668 +               return(-EINVAL);
7669 +       }
7670 +       if(!softcarrier && hardware[type].type==LIRC_HOMEBREW)
7671 +       {
7672 +               hardware[type].features&=~(LIRC_CAN_SET_SEND_DUTY_CYCLE|
7673 +                                          LIRC_CAN_SET_SEND_CARRIER);
7674 +       }
7675 +       if ((result = init_port()) < 0)
7676 +               return result;
7677 +       plugin.features = hardware[type].features;
7678 +       lirc_buffer_init(&rbuf, sizeof(lirc_t), RBUF_LEN);
7679 +       if ((plugin.minor = lirc_register_plugin(&plugin)) < 0) {
7680 +               printk(KERN_ERR  LIRC_DRIVER_NAME
7681 +                      ": register_chrdev failed!\n");
7682 +               release_region(io, 8);
7683 +               return -EIO;
7684 +       }
7685 +       return 0;
7686 +}
7687 +
7688 +static void __exit lirc_serial_exit(void)
7689 +{
7690 +       release_region(io, 8);
7691 +       lirc_buffer_free(&rbuf);
7692 +       lirc_unregister_plugin(plugin.minor);
7693 +       dprintk(LOGHEAD ": cleaned up module\n");
7694 +}
7695 +
7696 +module_init(lirc_serial_init);
7697 +module_exit(lirc_serial_exit);
7698 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/lirc_sir.c linux-2.6.5/drivers/char/lirc/lirc_sir.c
7699 --- linux-2.6.5.orig/drivers/char/lirc/lirc_sir.c       1970-01-01 01:00:00.000000000 +0100
7700 +++ linux-2.6.5/drivers/char/lirc/lirc_sir.c    2004-04-04 14:05:29.432624832 +0200
7701 @@ -0,0 +1,1309 @@
7702 +/*
7703 + * LIRC SIR driver, (C) 2000 Milan Pikula <www@fornax.sk>
7704 + *
7705 + * lirc_sir - Device driver for use with SIR (serial infra red)
7706 + * mode of IrDA on many notebooks.
7707 + *
7708 + *  This program is free software; you can redistribute it and/or modify
7709 + *  it under the terms of the GNU General Public License as published by
7710 + *  the Free Software Foundation; either version 2 of the License, or
7711 + *  (at your option) any later version.
7712 + *
7713 + *  This program is distributed in the hope that it will be useful,
7714 + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
7715 + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7716 + *  GNU General Public License for more details.
7717 + *
7718 + *  You should have received a copy of the GNU General Public License
7719 + *  along with this program; if not, write to the Free Software
7720 + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
7721 + *
7722 + *
7723 + * 2000/09/16 Frank Przybylski <mail@frankprzybylski.de> :
7724 + *  added timeout and relaxed pulse detection, removed gap bug
7725 + *
7726 + * 2000/12/15 Christoph Bartelmus <lirc@bartelmus.de> :
7727 + *   added support for Tekram Irmate 210 (sending does not work yet,
7728 + *   kind of disappointing that nobody was able to implement that
7729 + *   before),
7730 + *   major clean-up
7731 + *
7732 + * 2001/02/27 Christoph Bartelmus <lirc@bartelmus.de> :
7733 + *   added support for StrongARM SA1100 embedded microprocessor
7734 + *   parts cut'n'pasted from sa1100_ir.c (C) 2000 Russell King
7735 + */
7736 +
7737 +
7738 +#include <linux/version.h>
7739 +#include <linux/module.h>
7740 +#include <linux/config.h>
7741 +
7742 +#if !defined(CONFIG_LIRC_ON_SA1100) && !defined(CONFIG_SERIAL_MODULE)
7743 +#warning "******************************************"
7744 +#warning " Your serial port driver is compiled into "
7745 +#warning " the kernel. You will have to release the "
7746 +#warning " port you want to use for LIRC with:      "
7747 +#warning "    setserial /dev/ttySx uart none        "
7748 +#warning "******************************************"
7749 +#endif
7750 +
7751 +#include <linux/sched.h>
7752 +#include <linux/errno.h>
7753 +#include <linux/signal.h>
7754 +#include <linux/fs.h>
7755 +#include <linux/interrupt.h>
7756 +#include <linux/ioport.h>
7757 +#include <linux/kernel.h>
7758 +#include <linux/major.h>
7759 +#include <linux/serial_reg.h>
7760 +#include <linux/time.h>
7761 +#include <linux/string.h>
7762 +#include <linux/types.h>
7763 +#include <linux/wait.h>
7764 +#include <linux/mm.h>
7765 +#include <linux/delay.h>
7766 +#include <linux/poll.h>
7767 +#include <asm/system.h>
7768 +#include <asm/segment.h>
7769 +#include <asm/io.h>
7770 +#include <asm/irq.h>
7771 +#include <asm/fcntl.h>
7772 +#ifdef CONFIG_LIRC_ON_SA1100
7773 +#include <asm/hardware.h>
7774 +#ifdef CONFIG_SA1100_COLLIE
7775 +#include <asm/arch/tc35143.h>
7776 +#include <asm/ucb1200.h>
7777 +#endif
7778 +#endif
7779 +
7780 +#include <linux/timer.h>
7781 +
7782 +#include <linux/lirc.h>
7783 +#include "lirc_dev.h"
7784 +
7785 +/* SECTION: Definitions */
7786 +
7787 +/**************************** Tekram dongle ***************************/
7788 +#ifdef CONFIG_LIRC_SIR_TEKRAM
7789 +/* stolen from kernel source */
7790 +/* definitions for Tekram dongle */
7791 +#define TEKRAM_115200 0x00
7792 +#define TEKRAM_57600  0x01
7793 +#define TEKRAM_38400  0x02
7794 +#define TEKRAM_19200  0x03
7795 +#define TEKRAM_9600   0x04
7796 +#define TEKRAM_2400   0x08
7797 +
7798 +#define TEKRAM_PW 0x10 /* Pulse select bit */
7799 +
7800 +/* 10bit * 1s/115200bit in milli seconds = 87ms*/
7801 +#define TIME_CONST (10000000ul/115200ul)
7802 +
7803 +#endif
7804 +
7805 +#ifdef CONFIG_LIRC_SIR_ACTISYS_ACT200L
7806 +static void init_act200(void);
7807 +#endif
7808 +
7809 +/******************************* SA1100 ********************************/
7810 +#ifdef CONFIG_LIRC_ON_SA1100
7811 +struct sa1100_ser2_registers
7812 +{
7813 +       /* HSSP control register */
7814 +       unsigned char hscr0;
7815 +       /* UART registers */
7816 +       unsigned char utcr0;
7817 +       unsigned char utcr1;
7818 +       unsigned char utcr2;
7819 +       unsigned char utcr3;
7820 +       unsigned char utcr4;
7821 +       unsigned char utdr;
7822 +       unsigned char utsr0;
7823 +       unsigned char utsr1;
7824 +} sr;
7825 +
7826 +static int irq=IRQ_Ser2ICP;
7827 +
7828 +#define LIRC_ON_SA1100_TRANSMITTER_LATENCY 0
7829 +
7830 +/* pulse/space ratio of 50/50 */
7831 +unsigned long pulse_width = (13-LIRC_ON_SA1100_TRANSMITTER_LATENCY);
7832 +/* 1000000/freq-pulse_width */
7833 +unsigned long space_width = (13-LIRC_ON_SA1100_TRANSMITTER_LATENCY);
7834 +unsigned int freq = 38000;      /* modulation frequency */
7835 +unsigned int duty_cycle = 50;   /* duty cycle of 50% */
7836 +
7837 +#endif
7838 +
7839 +#define RBUF_LEN 1024
7840 +#define WBUF_LEN 1024
7841 +
7842 +#define LIRC_DRIVER_NAME "lirc_sir"
7843 +
7844 +#ifndef CONFIG_LIRC_SIR_TEKRAM
7845 +#define PULSE '['
7846 +
7847 +/* 9bit * 1s/115200bit in milli seconds = 78.125ms*/
7848 +#define TIME_CONST (9000000ul/115200ul)
7849 +#endif
7850 +
7851 +
7852 +/* timeout for sequences in jiffies (=5/100s) */
7853 +/* must be longer than TIME_CONST */
7854 +#define SIR_TIMEOUT    (HZ*5/100)
7855 +
7856 +#ifndef CONFIG_LIRC_ON_SA1100
7857 +static int io = CONFIG_LIRC_PORT_SIR;
7858 +static int irq = CONFIG_LIRC_IRQ_SIR;
7859 +static int threshold = 3;
7860 +#endif
7861 +
7862 +static spinlock_t timer_lock = SPIN_LOCK_UNLOCKED;
7863 +static struct timer_list timerlist;
7864 +/* time of last signal change detected */
7865 +static struct timeval last_tv = {0, 0};
7866 +/* time of last UART data ready interrupt */
7867 +static struct timeval last_intr_tv = {0, 0};
7868 +static int last_value = 0;
7869 +static spinlock_t lirc_lock;
7870 +
7871 +static DECLARE_WAIT_QUEUE_HEAD(lirc_read_queue);
7872 +
7873 +static spinlock_t hardware_lock = SPIN_LOCK_UNLOCKED;
7874 +static spinlock_t dev_lock = SPIN_LOCK_UNLOCKED;
7875 +
7876 +static lirc_t rx_buf[RBUF_LEN]; unsigned int rx_tail = 0, rx_head = 0;
7877 +#ifndef CONFIG_LIRC_SIR_TEKRAM
7878 +static lirc_t tx_buf[WBUF_LEN];
7879 +#endif
7880 +
7881 +/* SECTION: Prototypes */
7882 +
7883 +/* Communication with user-space */
7884 +static int lirc_open(struct inode * inode, struct file * file);
7885 +static int lirc_close(struct inode * inode, struct file *file);
7886 +static unsigned int lirc_poll(struct file * file, poll_table * wait);
7887 +static ssize_t lirc_read(struct file * file, char * buf, size_t count,
7888 +               loff_t * ppos);
7889 +static ssize_t lirc_write(struct file * file, const char * buf, size_t n, loff_t * pos);
7890 +static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
7891 +               unsigned long arg);
7892 +static void add_read_queue(int flag, unsigned long val);
7893 +#ifdef MODULE
7894 +static int init_chrdev(void);
7895 +static void drop_chrdev(void);
7896 +#endif
7897 +       /* Hardware */
7898 +static irqreturn_t sir_interrupt(int irq, void * dev_id, struct pt_regs * regs);
7899 +#ifndef CONFIG_LIRC_SIR_TEKRAM
7900 +static void send_space(unsigned long len);
7901 +static void send_pulse(unsigned long len);
7902 +#endif
7903 +static int init_hardware(void);
7904 +static void drop_hardware(void);
7905 +       /* Initialisation */
7906 +static int init_port(void);
7907 +static void drop_port(void);
7908 +int init_module(void);
7909 +void cleanup_module(void);
7910 +
7911 +#ifdef CONFIG_LIRC_ON_SA1100
7912 +void inline on(void)
7913 +{
7914 +       PPSR|=PPC_TXD2;
7915 +}
7916 +
7917 +void inline off(void)
7918 +{
7919 +       PPSR&=~PPC_TXD2;
7920 +}
7921 +#else
7922 +static inline unsigned int sinp(int offset)
7923 +{
7924 +       return inb(io + offset);
7925 +}
7926 +
7927 +static inline void soutp(int offset, int value)
7928 +{
7929 +       outb(value, io + offset);
7930 +}
7931 +#endif
7932 +
7933 +#ifndef MAX_UDELAY_MS
7934 +#define MAX_UDELAY_US 5000
7935 +#else
7936 +#define MAX_UDELAY_US (MAX_UDELAY_MS*1000)
7937 +#endif
7938 +
7939 +static inline void safe_udelay(unsigned long usecs)
7940 +{
7941 +       while(usecs>MAX_UDELAY_US)
7942 +       {
7943 +               udelay(MAX_UDELAY_US);
7944 +               usecs-=MAX_UDELAY_US;
7945 +       }
7946 +       udelay(usecs);
7947 +}
7948 +
7949 +/* SECTION: Communication with user-space */
7950 +
7951 +static int lirc_open(struct inode * inode, struct file * file)
7952 +{
7953 +       spin_lock(&dev_lock);
7954 +#ifdef CONFIG_MODULE_UNLOAD
7955 +       if (module_refcount(THIS_MODULE))
7956 +       {
7957 +               spin_unlock(&dev_lock);
7958 +               return -EBUSY;
7959 +       }
7960 +#endif
7961 +       if (!try_module_get(THIS_MODULE))
7962 +       {
7963 +               spin_unlock(&dev_lock);
7964 +               return -EINVAL;
7965 +       }
7966 +       spin_unlock(&dev_lock);
7967 +       return 0;
7968 +}
7969 +
7970 +static int lirc_close(struct inode * inode, struct file *file)
7971 +{
7972 +       module_put(THIS_MODULE);
7973 +       return 0;
7974 +}
7975 +
7976 +static unsigned int lirc_poll(struct file * file, poll_table * wait)
7977 +{
7978 +       poll_wait(file, &lirc_read_queue, wait);
7979 +       if (rx_head != rx_tail)
7980 +               return POLLIN | POLLRDNORM;
7981 +       return 0;
7982 +}
7983 +
7984 +static ssize_t lirc_read(struct file * file, char * buf, size_t count,
7985 +               loff_t * ppos)
7986 +{
7987 +       int n=0;
7988 +       int retval = 0;
7989 +       DECLARE_WAITQUEUE(wait,current);
7990 +
7991 +       if(n%sizeof(lirc_t)) return(-EINVAL);
7992 +
7993 +       add_wait_queue(&lirc_read_queue,&wait);
7994 +       current->state=TASK_INTERRUPTIBLE;
7995 +       while(n<count)
7996 +       {
7997 +               if(rx_head!=rx_tail)
7998 +               {
7999 +                       retval=verify_area(VERIFY_WRITE,
8000 +                                          (void *) buf+n,sizeof(lirc_t));
8001 +                       if (retval)
8002 +                       {
8003 +                               return retval;
8004 +                       }
8005 +                       copy_to_user((void *) buf+n,(void *) (rx_buf+rx_head),
8006 +                                    sizeof(lirc_t));
8007 +                       rx_head=(rx_head+1)&(RBUF_LEN-1);
8008 +                       n+=sizeof(lirc_t);
8009 +               }
8010 +               else
8011 +               {
8012 +                       if(file->f_flags & O_NONBLOCK)
8013 +                       {
8014 +                               retval=-EAGAIN;
8015 +                               break;
8016 +                       }
8017 +                       if(signal_pending(current))
8018 +                       {
8019 +                               retval=-ERESTARTSYS;
8020 +                               break;
8021 +                       }
8022 +                       schedule();
8023 +                       current->state=TASK_INTERRUPTIBLE;
8024 +               }
8025 +       }
8026 +       remove_wait_queue(&lirc_read_queue,&wait);
8027 +       current->state=TASK_RUNNING;
8028 +       return (n ? n : retval);
8029 +}
8030 +static ssize_t lirc_write(struct file * file, const char * buf, size_t n, loff_t * pos)
8031 +{
8032 +       unsigned long flags;
8033 +#ifdef CONFIG_LIRC_SIR_TEKRAM
8034 +       return(-EBADF);
8035 +#else
8036 +       int i;
8037 +       int retval;
8038 +
8039 +        if(n%sizeof(lirc_t) || (n/sizeof(lirc_t)) > WBUF_LEN)
8040 +               return(-EINVAL);
8041 +       retval = verify_area(VERIFY_READ, buf, n);
8042 +       if (retval)
8043 +               return retval;
8044 +       copy_from_user(tx_buf, buf, n);
8045 +       i = 0;
8046 +       n/=sizeof(lirc_t);
8047 +#ifdef CONFIG_LIRC_ON_SA1100
8048 +       /* disable receiver */
8049 +       Ser2UTCR3=0;
8050 +#endif
8051 +       spin_lock_irqsave(&lirc_lock, flags);
8052 +       while (1) {
8053 +               if (i >= n)
8054 +                       break;
8055 +               if (tx_buf[i])
8056 +                       send_pulse(tx_buf[i]);
8057 +               i++;
8058 +               if (i >= n)
8059 +                       break;
8060 +               if (tx_buf[i])
8061 +                       send_space(tx_buf[i]);
8062 +               i++;
8063 +       }
8064 +       spin_unlock_irqrestore(&lirc_lock, flags);
8065 +#ifdef CONFIG_LIRC_ON_SA1100
8066 +       off();
8067 +       udelay(1000); /* wait 1ms for IR diode to recover */
8068 +       Ser2UTCR3=0;
8069 +       /* clear status register to prevent unwanted interrupts */
8070 +       Ser2UTSR0 &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
8071 +       /* enable receiver */
8072 +       Ser2UTCR3=UTCR3_RXE|UTCR3_RIE;
8073 +#endif
8074 +       return n;
8075 +#endif
8076 +}
8077 +
8078 +static int lirc_ioctl(struct inode *node,struct file *filep,unsigned int cmd,
8079 +               unsigned long arg)
8080 +{
8081 +       int retval = 0;
8082 +       unsigned long value = 0;
8083 +#ifdef CONFIG_LIRC_ON_SA1100
8084 +       unsigned int ivalue;
8085 +#endif
8086 +
8087 +#ifdef CONFIG_LIRC_SIR_TEKRAM
8088 +       if (cmd == LIRC_GET_FEATURES)
8089 +               value = LIRC_CAN_REC_MODE2;
8090 +       else if (cmd == LIRC_GET_SEND_MODE)
8091 +               value = 0;
8092 +       else if (cmd == LIRC_GET_REC_MODE)
8093 +               value = LIRC_MODE_MODE2;
8094 +#elif defined(CONFIG_LIRC_ON_SA1100)
8095 +       if (cmd == LIRC_GET_FEATURES)
8096 +               value = LIRC_CAN_SEND_PULSE |
8097 +                       LIRC_CAN_SET_SEND_DUTY_CYCLE |
8098 +                       LIRC_CAN_SET_SEND_CARRIER |
8099 +                       LIRC_CAN_REC_MODE2;
8100 +       else if (cmd == LIRC_GET_SEND_MODE)
8101 +               value = LIRC_MODE_PULSE;
8102 +       else if (cmd == LIRC_GET_REC_MODE)
8103 +               value = LIRC_MODE_MODE2;
8104 +#else
8105 +       if (cmd == LIRC_GET_FEATURES)
8106 +               value = LIRC_CAN_SEND_PULSE | LIRC_CAN_REC_MODE2;
8107 +       else if (cmd == LIRC_GET_SEND_MODE)
8108 +               value = LIRC_MODE_PULSE;
8109 +       else if (cmd == LIRC_GET_REC_MODE)
8110 +               value = LIRC_MODE_MODE2;
8111 +#endif
8112 +
8113 +       switch (cmd) {
8114 +       case LIRC_GET_FEATURES:
8115 +       case LIRC_GET_SEND_MODE:
8116 +       case LIRC_GET_REC_MODE:
8117 +               retval = put_user(value, (unsigned long *) arg);
8118 +               break;
8119 +
8120 +       case LIRC_SET_SEND_MODE:
8121 +       case LIRC_SET_REC_MODE:
8122 +               retval = get_user(value, (unsigned long *) arg);
8123 +               break;
8124 +#ifdef CONFIG_LIRC_ON_SA1100
8125 +       case LIRC_SET_SEND_DUTY_CYCLE:
8126 +               retval=get_user(ivalue,(unsigned int *) arg);
8127 +               if(retval) return(retval);
8128 +               if(ivalue<=0 || ivalue>100) return(-EINVAL);
8129 +               /* (ivalue/100)*(1000000/freq) */
8130 +               duty_cycle=ivalue;
8131 +               pulse_width=(unsigned long) duty_cycle*10000/freq;
8132 +               space_width=(unsigned long) 1000000L/freq-pulse_width;
8133 +               if(pulse_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
8134 +                       pulse_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
8135 +               if(space_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
8136 +                       space_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
8137 +               break;
8138 +       case LIRC_SET_SEND_CARRIER:
8139 +               retval=get_user(ivalue,(unsigned int *) arg);
8140 +               if(retval) return(retval);
8141 +               if(ivalue>500000 || ivalue<20000) return(-EINVAL);
8142 +               freq=ivalue;
8143 +               pulse_width=(unsigned long) duty_cycle*10000/freq;
8144 +               space_width=(unsigned long) 1000000L/freq-pulse_width;
8145 +               if(pulse_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
8146 +                       pulse_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
8147 +               if(space_width>=LIRC_ON_SA1100_TRANSMITTER_LATENCY)
8148 +                       space_width-=LIRC_ON_SA1100_TRANSMITTER_LATENCY;
8149 +               break;
8150 +#endif
8151 +       default:
8152 +               retval = -ENOIOCTLCMD;
8153 +
8154 +       }
8155 +
8156 +       if (retval)
8157 +               return retval;
8158 +
8159 +#ifdef CONFIG_LIRC_SIR_TEKRAM
8160 +       if (cmd == LIRC_SET_REC_MODE) {
8161 +               if (value != LIRC_MODE_MODE2)
8162 +                       retval = -ENOSYS;
8163 +       } else if (cmd == LIRC_SET_SEND_MODE) {
8164 +               retval = -ENOSYS;
8165 +       }
8166 +#else
8167 +       if (cmd == LIRC_SET_REC_MODE) {
8168 +               if (value != LIRC_MODE_MODE2)
8169 +                       retval = -ENOSYS;
8170 +       } else if (cmd == LIRC_SET_SEND_MODE) {
8171 +               if (value != LIRC_MODE_PULSE)
8172 +                       retval = -ENOSYS;
8173 +       }
8174 +#endif
8175 +       return retval;
8176 +}
8177 +
8178 +static void add_read_queue(int flag, unsigned long val)
8179 +{
8180 +       unsigned int new_rx_tail;
8181 +       lirc_t newval;
8182 +
8183 +#ifdef DEBUG_SIGNAL
8184 +       printk(KERN_DEBUG LIRC_DRIVER_NAME
8185 +               ": add flag %d with val %lu\n",
8186 +               flag,val);
8187 +#endif
8188 +
8189 +       newval = val & PULSE_MASK;
8190 +
8191 +       /* statistically pulses are ~TIME_CONST/2 too long: we could
8192 +          maybe make this more exactly but this is good enough */
8193 +       if(flag) /* pulse */
8194 +       {
8195 +               if(newval>TIME_CONST/2)
8196 +               {
8197 +                       newval-=TIME_CONST/2;
8198 +               }
8199 +               else /* should not ever happen */
8200 +               {
8201 +                       newval=1;
8202 +               }
8203 +               newval|=PULSE_BIT;
8204 +       }
8205 +       else
8206 +       {
8207 +               newval+=TIME_CONST/2;
8208 +       }
8209 +       new_rx_tail = (rx_tail + 1) & (RBUF_LEN - 1);
8210 +       if (new_rx_tail == rx_head) {
8211 +#               ifdef DEBUG
8212 +               printk(KERN_WARNING LIRC_DRIVER_NAME ": Buffer overrun.\n");
8213 +#               endif
8214 +               return;
8215 +       }
8216 +       rx_buf[rx_tail] = newval;
8217 +       rx_tail = new_rx_tail;
8218 +       wake_up_interruptible(&lirc_read_queue);
8219 +}
8220 +
8221 +static struct file_operations lirc_fops =
8222 +{
8223 +       .read    = lirc_read,
8224 +       .write   = lirc_write,
8225 +       .poll    = lirc_poll,
8226 +       .ioctl   = lirc_ioctl,
8227 +       .open    = lirc_open,
8228 +       .release = lirc_close,
8229 +};
8230 +
8231 +static int set_use_inc(void* data)
8232 +{
8233 +#if WE_DONT_USE_LOCAL_OPEN_CLOSE
8234 +       if (!try_module_get(THIS_MODULE)) {
8235 +                return -EINVAL;
8236 +       }
8237 +#endif
8238 +       return 0;
8239 +}
8240 +
8241 +static void set_use_dec(void* data)
8242 +{
8243 +#if WE_DONT_USE_LOCAL_OPEN_CLOSE
8244 +       module_put(THIS_MODULE);
8245 +#endif
8246 +}
8247 +static struct lirc_plugin plugin = {
8248 +       .name          = LIRC_DRIVER_NAME,
8249 +       .minor         = -1,
8250 +       .code_length   = 1,
8251 +       .sample_rate   = 0,
8252 +       .data          = NULL,
8253 +       .add_to_buf    = NULL,
8254 +       .get_queue     = NULL,
8255 +       .set_use_inc   = set_use_inc,
8256 +       .set_use_dec   = set_use_dec,
8257 +       .fops          = &lirc_fops,
8258 +};
8259 +
8260 +int init_chrdev(void)
8261 +{
8262 +       plugin.minor = lirc_register_plugin(&plugin);
8263 +       if (plugin.minor < 0) {
8264 +               printk(KERN_ERR LIRC_DRIVER_NAME ": init_chrdev() failed.\n");
8265 +               return -EIO;
8266 +       }
8267 +       return 0;
8268 +}
8269 +
8270 +static void drop_chrdev(void)
8271 +{
8272 +       lirc_unregister_plugin(plugin.minor);
8273 +}
8274 +
8275 +/* SECTION: Hardware */
8276 +static long delta(struct timeval * tv1, struct timeval * tv2)
8277 +{
8278 +       unsigned long deltv;
8279 +
8280 +       deltv = tv2->tv_sec - tv1->tv_sec;
8281 +       if (deltv > 15)
8282 +               deltv = 0xFFFFFF;
8283 +       else
8284 +               deltv = deltv*1000000 +
8285 +                       tv2->tv_usec -
8286 +                       tv1->tv_usec;
8287 +       return deltv;
8288 +}
8289 +
8290 +static void sir_timeout(unsigned long data)
8291 +{
8292 +       /* if last received signal was a pulse, but receiving stopped
8293 +          within the 9 bit frame, we need to finish this pulse and
8294 +          simulate a signal change to from pulse to space. Otherwise
8295 +          upper layers will receive two sequences next time. */
8296 +
8297 +       unsigned long flags;
8298 +       unsigned long pulse_end;
8299 +
8300 +       /* avoid interference with interrupt */
8301 +       spin_lock_irqsave(&timer_lock, flags);
8302 +       if (last_value)
8303 +       {
8304 +#ifndef CONFIG_LIRC_ON_SA1100
8305 +               /* clear unread bits in UART and restart */
8306 +               outb(UART_FCR_CLEAR_RCVR, io + UART_FCR);
8307 +#endif
8308 +               /* determine 'virtual' pulse end: */
8309 +               pulse_end = delta(&last_tv, &last_intr_tv);
8310 +#ifdef DEBUG_SIGNAL
8311 +               printk(KERN_DEBUG LIRC_DRIVER_NAME
8312 +                       ": timeout add %d for %lu usec\n",last_value,pulse_end);
8313 +#endif
8314 +               add_read_queue(last_value,pulse_end);
8315 +               last_value = 0;
8316 +               last_tv=last_intr_tv;
8317 +       }
8318 +       spin_unlock_irqrestore(&timer_lock, flags);
8319 +}
8320 +
8321 +static irqreturn_t sir_interrupt(int irq, void *dev_id, struct pt_regs *regs)
8322 +{
8323 +       unsigned char data;
8324 +       struct timeval curr_tv;
8325 +       static unsigned long deltv;
8326 +#ifdef CONFIG_LIRC_ON_SA1100
8327 +       int status;
8328 +       static int n=0;
8329 +
8330 +       //printk("interrupt\n");
8331 +       status = Ser2UTSR0;
8332 +       /*
8333 +        * Deal with any receive errors first.  The bytes in error may be
8334 +        * the only bytes in the receive FIFO, so we do this first.
8335 +        */
8336 +       while (status & UTSR0_EIF)
8337 +       {
8338 +               int bstat;
8339 +
8340 +#ifdef DEBUG
8341 +               printk("EIF\n");
8342 +               bstat = Ser2UTSR1;
8343 +
8344 +               if (bstat & UTSR1_FRE)
8345 +                       printk("frame error\n");
8346 +               if (bstat & UTSR1_ROR)
8347 +                       printk("receive fifo overrun\n");
8348 +               if(bstat&UTSR1_PRE)
8349 +                       printk("parity error\n");
8350 +#endif
8351 +
8352 +               bstat = Ser2UTDR;
8353 +               n++;
8354 +               status = Ser2UTSR0;
8355 +       }
8356 +
8357 +       if (status & (UTSR0_RFS | UTSR0_RID))
8358 +       {
8359 +               do_gettimeofday(&curr_tv);
8360 +               deltv = delta(&last_tv, &curr_tv);
8361 +               do
8362 +               {
8363 +#ifdef DEBUG_SIGNAL
8364 +                       printk(KERN_DEBUG LIRC_DRIVER_NAME": t %lu , d %d\n",
8365 +                              deltintrtv,(int)data);
8366 +#endif
8367 +                       data=Ser2UTDR;
8368 +                       //printk("data: %d\n",data);
8369 +                       n++;
8370 +               }
8371 +               while(status&UTSR0_RID && /* do not empty fifo in
8372 +                                             order to get UTSR0_RID in
8373 +                                             any case */
8374 +                     Ser2UTSR1 & UTSR1_RNE); /* data ready */
8375 +
8376 +               if(status&UTSR0_RID)
8377 +               {
8378 +                       //printk("add\n");
8379 +                       add_read_queue(0,deltv-n*TIME_CONST); /*space*/
8380 +                       add_read_queue(1,n*TIME_CONST); /*pulse*/
8381 +                       n=0;
8382 +                       last_tv=curr_tv;
8383 +               }
8384 +       }
8385 +
8386 +       if (status & UTSR0_TFS) {
8387 +
8388 +               printk("transmit fifo not full, shouldn't ever happen\n");
8389 +       }
8390 +
8391 +       /*
8392 +        * We must clear certain bits.
8393 +        */
8394 +       status &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
8395 +       if (status)
8396 +               Ser2UTSR0 = status;
8397 +#else
8398 +       unsigned long deltintrtv;
8399 +       unsigned long flags;
8400 +       int iir, lsr;
8401 +
8402 +       while ((iir = inb(io + UART_IIR) & UART_IIR_ID)) {
8403 +               switch (iir&UART_IIR_ID) { /* FIXME toto treba preriedit */
8404 +               case UART_IIR_MSI:
8405 +                       (void) inb(io + UART_MSR);
8406 +                       break;
8407 +               case UART_IIR_RLSI:
8408 +                       (void) inb(io + UART_LSR);
8409 +                       break;
8410 +               case UART_IIR_THRI:
8411 +#if 0
8412 +                       if (lsr & UART_LSR_THRE) /* FIFO is empty */
8413 +                               outb(data, io + UART_TX)
8414 +#endif
8415 +                       break;
8416 +               case UART_IIR_RDI:
8417 +                       /* avoid interference with timer */
8418 +                       spin_lock_irqsave(&timer_lock, flags);
8419 +                       do
8420 +                       {
8421 +                               del_timer(&timerlist);
8422 +                               data = inb(io + UART_RX);
8423 +                               do_gettimeofday(&curr_tv);
8424 +                               deltv = delta(&last_tv, &curr_tv);
8425 +                               deltintrtv = delta(&last_intr_tv, &curr_tv);
8426 +#ifdef DEBUG_SIGNAL
8427 +                               printk(KERN_DEBUG LIRC_DRIVER_NAME": t %lu , d %d\n",deltintrtv,(int)data);
8428 +#endif
8429 +                               /* if nothing came in last X cycles,
8430 +                                  it was gap */
8431 +                               if (deltintrtv > TIME_CONST * threshold) {
8432 +                                       if (last_value) {
8433 +#ifdef DEBUG_SIGNAL
8434 +                                               printk(KERN_DEBUG LIRC_DRIVER_NAME ": GAP\n");
8435 +#endif
8436 +                                               /* simulate signal change */
8437 +                                               add_read_queue(last_value,
8438 +                                                              deltv-
8439 +                                                              deltintrtv);
8440 +                                               last_value = 0;
8441 +                                               last_tv.tv_sec = last_intr_tv.tv_sec;
8442 +                                               last_tv.tv_usec = last_intr_tv.tv_usec;
8443 +                                               deltv = deltintrtv;
8444 +                                       }
8445 +                               }
8446 +                               data = 1;
8447 +                               if (data ^ last_value) {
8448 +                                       /* deltintrtv > 2*TIME_CONST,
8449 +                                           remember ? */
8450 +                                       /* the other case is timeout */
8451 +                                       add_read_queue(last_value,
8452 +                                                      deltv-TIME_CONST);
8453 +                                       last_value = data;
8454 +                                       last_tv = curr_tv;
8455 +                                       if(last_tv.tv_usec>=TIME_CONST)
8456 +                                       {
8457 +                                               last_tv.tv_usec-=TIME_CONST;
8458 +                                       }
8459 +                                       else
8460 +                                       {
8461 +                                               last_tv.tv_sec--;
8462 +                                               last_tv.tv_usec+=1000000-
8463 +                                                       TIME_CONST;
8464 +                                       }
8465 +                               }
8466 +                               last_intr_tv = curr_tv;
8467 +                               if (data)
8468 +                               {
8469 +                                       /* start timer for end of sequence detection */
8470 +                                       timerlist.expires = jiffies + SIR_TIMEOUT;
8471 +                                       add_timer(&timerlist);
8472 +                               }
8473 +                       }
8474 +                       while ((lsr = inb(io + UART_LSR))
8475 +                               & UART_LSR_DR); /* data ready */
8476 +                       spin_unlock_irqrestore(&timer_lock, flags);
8477 +                       break;
8478 +               default:
8479 +                       break;
8480 +               }
8481 +       }
8482 +#endif
8483 +       return IRQ_HANDLED;
8484 +}
8485 +
8486 +#ifdef CONFIG_LIRC_ON_SA1100
8487 +void send_pulse(unsigned long length)
8488 +{
8489 +       unsigned long k,delay;
8490 +       int flag;
8491 +
8492 +       if(length==0) return;
8493 +       /* this won't give us the carrier frequency we really want
8494 +          due to integer arithmetic, but we can accept this inaccuracy */
8495 +
8496 +       for(k=flag=0;k<length;k+=delay,flag=!flag)
8497 +       {
8498 +               if(flag)
8499 +               {
8500 +                       off();
8501 +                       delay=space_width;
8502 +               }
8503 +               else
8504 +               {
8505 +                       on();
8506 +                       delay=pulse_width;
8507 +               }
8508 +               safe_udelay(delay);
8509 +       }
8510 +       off();
8511 +}
8512 +
8513 +void send_space(unsigned long length)
8514 +{
8515 +       if(length==0) return;
8516 +       off();
8517 +       safe_udelay(length);
8518 +}
8519 +#elif defined(CONFIG_LIRC_SIR_TEKRAM)
8520 +#else
8521 +static void send_space(unsigned long len)
8522 +{
8523 +       safe_udelay(len);
8524 +}
8525 +
8526 +static void send_pulse(unsigned long len)
8527 +{
8528 +       long bytes_out = len / TIME_CONST;
8529 +       long time_left;
8530 +
8531 +       if (!bytes_out)
8532 +               bytes_out++;
8533 +       time_left = (long)len - (long)bytes_out * (long)TIME_CONST;
8534 +       while (--bytes_out) {
8535 +               outb(PULSE, io + UART_TX);
8536 +               /* FIXME treba seriozne cakanie z drivers/char/serial.c */
8537 +               while (!(inb(io + UART_LSR) & UART_LSR_THRE));
8538 +       }
8539 +#if 0
8540 +       if (time_left > 0)
8541 +               safe_udelay(time_left);
8542 +#endif
8543 +}
8544 +#endif
8545 +
8546 +#ifdef CONFIG_SA1100_COLLIE
8547 +static inline int sa1100_irda_set_power_collie(int state)
8548 +{
8549 +       if (state) {
8550 +               /*
8551 +                *  0 - off
8552 +                *  1 - short range, lowest power
8553 +                *  2 - medium range, medium power
8554 +                *  3 - maximum range, high power
8555 +                */
8556 +               ucb1200_set_io_direction(TC35143_GPIO_IR_ON,
8557 +                                        TC35143_IODIR_OUTPUT);
8558 +               ucb1200_set_io(TC35143_GPIO_IR_ON, TC35143_IODAT_LOW);
8559 +               udelay(100);
8560 +       }
8561 +       else {
8562 +               /* OFF */
8563 +               ucb1200_set_io_direction(TC35143_GPIO_IR_ON,
8564 +                                        TC35143_IODIR_OUTPUT);
8565 +               ucb1200_set_io(TC35143_GPIO_IR_ON, TC35143_IODAT_HIGH);
8566 +       }
8567 +       return 0;
8568 +}
8569 +#endif
8570 +
8571 +static int init_hardware(void)
8572 +{
8573 +       int flags;
8574 +
8575 +       spin_lock_irqsave(&hardware_lock, flags);
8576 +       /* reset UART */
8577 +#ifdef CONFIG_LIRC_ON_SA1100
8578 +#ifdef CONFIG_SA1100_BITSY
8579 +       if (machine_is_bitsy()) {
8580 +               printk("Power on IR module\n");
8581 +               set_bitsy_egpio(EGPIO_BITSY_IR_ON);
8582 +       }
8583 +#endif
8584 +#ifdef CONFIG_SA1100_COLLIE
8585 +       sa1100_irda_set_power_collie(3);        /* power on */
8586 +#endif
8587 +       sr.hscr0=Ser2HSCR0;
8588 +
8589 +       sr.utcr0=Ser2UTCR0;
8590 +       sr.utcr1=Ser2UTCR1;
8591 +       sr.utcr2=Ser2UTCR2;
8592 +       sr.utcr3=Ser2UTCR3;
8593 +       sr.utcr4=Ser2UTCR4;
8594 +
8595 +       sr.utdr=Ser2UTDR;
8596 +       sr.utsr0=Ser2UTSR0;
8597 +       sr.utsr1=Ser2UTSR1;
8598 +
8599 +       /* configure GPIO */
8600 +       /* output */
8601 +       PPDR|=PPC_TXD2;
8602 +       PSDR|=PPC_TXD2;
8603 +       /* set output to 0 */
8604 +       off();
8605 +
8606 +       /*
8607 +        * Enable HP-SIR modulation, and ensure that the port is disabled.
8608 +        */
8609 +       Ser2UTCR3=0;
8610 +       Ser2HSCR0=sr.hscr0 & (~HSCR0_HSSP);
8611 +
8612 +       /* clear status register to prevent unwanted interrupts */
8613 +       Ser2UTSR0 &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
8614 +
8615 +       /* 7N1 */
8616 +       Ser2UTCR0=UTCR0_1StpBit|UTCR0_7BitData;
8617 +       /* 115200 */
8618 +       Ser2UTCR1=0;
8619 +       Ser2UTCR2=1;
8620 +       /* use HPSIR, 1.6 usec pulses */
8621 +       Ser2UTCR4=UTCR4_HPSIR|UTCR4_Z1_6us;
8622 +
8623 +       /* enable receiver, receive fifo interrupt */
8624 +       Ser2UTCR3=UTCR3_RXE|UTCR3_RIE;
8625 +
8626 +       /* clear status register to prevent unwanted interrupts */
8627 +       Ser2UTSR0 &= (UTSR0_RID | UTSR0_RBB | UTSR0_REB);
8628 +
8629 +#elif defined(CONFIG_LIRC_SIR_TEKRAM)
8630 +       /* disable FIFO */
8631 +       soutp(UART_FCR,
8632 +             UART_FCR_CLEAR_RCVR|
8633 +             UART_FCR_CLEAR_XMIT|
8634 +             UART_FCR_TRIGGER_1);
8635 +
8636 +       /* Set DLAB 0. */
8637 +       soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
8638 +
8639 +       /* First of all, disable all interrupts */
8640 +       soutp(UART_IER, sinp(UART_IER)&
8641 +             (~(UART_IER_MSI|UART_IER_RLSI|UART_IER_THRI|UART_IER_RDI)));
8642 +
8643 +       /* Set DLAB 1. */
8644 +       soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
8645 +
8646 +       /* Set divisor to 12 => 9600 Baud */
8647 +       soutp(UART_DLM,0);
8648 +       soutp(UART_DLL,12);
8649 +
8650 +       /* Set DLAB 0. */
8651 +       soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
8652 +
8653 +       /* power supply */
8654 +       soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
8655 +       safe_udelay(50*1000);
8656 +
8657 +       /* -DTR low -> reset PIC */
8658 +       soutp(UART_MCR, UART_MCR_RTS|UART_MCR_OUT2);
8659 +       udelay(1*1000);
8660 +
8661 +       soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
8662 +       udelay(100);
8663 +
8664 +
8665 +        /* -RTS low -> send control byte */
8666 +       soutp(UART_MCR, UART_MCR_DTR|UART_MCR_OUT2);
8667 +       udelay(7);
8668 +       soutp(UART_TX, TEKRAM_115200|TEKRAM_PW);
8669 +
8670 +       /* one byte takes ~1042 usec to transmit at 9600,8N1 */
8671 +       udelay(1500);
8672 +
8673 +       /* back to normal operation */
8674 +       soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
8675 +       udelay(50);
8676 +
8677 +       udelay(1500);
8678 +
8679 +       /* read previous control byte */
8680 +       printk(KERN_INFO LIRC_DRIVER_NAME
8681 +              ": 0x%02x\n",sinp(UART_RX));
8682 +
8683 +       /* Set DLAB 1. */
8684 +       soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
8685 +
8686 +       /* Set divisor to 1 => 115200 Baud */
8687 +       soutp(UART_DLM,0);
8688 +       soutp(UART_DLL,1);
8689 +
8690 +       /* Set DLAB 0, 8 Bit */
8691 +       soutp(UART_LCR, UART_LCR_WLEN8);
8692 +       /* enable interrupts */
8693 +       soutp(UART_IER, sinp(UART_IER)|UART_IER_RDI);
8694 +#else
8695 +       outb(0, io + UART_MCR);
8696 +       outb(0, io + UART_IER);
8697 +       /* init UART */
8698 +               /* set DLAB, speed = 115200 */
8699 +       outb(UART_LCR_DLAB | UART_LCR_WLEN7, io + UART_LCR);
8700 +       outb(1, io + UART_DLL); outb(0, io + UART_DLM);
8701 +               /* 7N1+start = 9 bits at 115200 ~ 3 bits at 44000 */
8702 +       outb(UART_LCR_WLEN7, io + UART_LCR);
8703 +               /* FIFO operation */
8704 +       outb(UART_FCR_ENABLE_FIFO, io + UART_FCR);
8705 +               /* interrupts */
8706 +       // outb(UART_IER_RLSI|UART_IER_RDI|UART_IER_THRI, io + UART_IER);
8707 +       outb(UART_IER_RDI, io + UART_IER);
8708 +       /* turn on UART */
8709 +       outb(UART_MCR_DTR|UART_MCR_RTS|UART_MCR_OUT2, io + UART_MCR);
8710 +#ifdef CONFIG_LIRC_SIR_ACTISYS_ACT200L
8711 +       init_act200();
8712 +#endif
8713 +#endif
8714 +       spin_unlock_irqrestore(&hardware_lock, flags);
8715 +       return 0;
8716 +}
8717 +
8718 +static void drop_hardware(void)
8719 +{
8720 +       int flags;
8721 +
8722 +       spin_lock_irqsave(&hardware_lock, flags);
8723 +
8724 +#ifdef CONFIG_LIRC_ON_SA1100
8725 +       Ser2UTCR3=0;
8726 +
8727 +       Ser2UTCR0=sr.utcr0;
8728 +       Ser2UTCR1=sr.utcr1;
8729 +       Ser2UTCR2=sr.utcr2;
8730 +       Ser2UTCR4=sr.utcr4;
8731 +       Ser2UTCR3=sr.utcr3;
8732 +
8733 +       Ser2HSCR0=sr.hscr0;
8734 +#ifdef CONFIG_SA1100_BITSY
8735 +       if (machine_is_bitsy()) {
8736 +               clr_bitsy_egpio(EGPIO_BITSY_IR_ON);
8737 +       }
8738 +#endif
8739 +#ifdef CONFIG_SA1100_COLLIE
8740 +       sa1100_irda_set_power_collie(0);        /* power off */
8741 +#endif
8742 +#else
8743 +       /* turn off interrupts */
8744 +       outb(0, io + UART_IER);
8745 +#endif
8746 +       spin_unlock_irqrestore(&hardware_lock, flags);
8747 +}
8748 +
8749 +/* SECTION: Initialisation */
8750 +
8751 +static int init_port(void)
8752 +{
8753 +       int retval;
8754 +
8755 +#ifndef CONFIG_LIRC_ON_SA1100
8756 +       /* get I/O port access and IRQ line */
8757 +       retval = request_region(io, 8, LIRC_DRIVER_NAME);
8758 +       if (!retval) {
8759 +               printk(KERN_ERR LIRC_DRIVER_NAME
8760 +                       ": i/o port 0x%.4x already in use.\n",
8761 +                       io);
8762 +               return retval;
8763 +       }
8764 +       printk(KERN_INFO LIRC_DRIVER_NAME
8765 +               ": I/O port 0x%.4x, IRQ %d.\n",
8766 +               io, irq);
8767 +#endif
8768 +       retval = request_irq(irq, sir_interrupt, SA_INTERRUPT,
8769 +                            LIRC_DRIVER_NAME, NULL);
8770 +       if (retval < 0) {
8771 +               printk(KERN_ERR LIRC_DRIVER_NAME
8772 +                       ": IRQ %d already in use.\n",
8773 +                       irq);
8774 +               return retval;
8775 +       }
8776 +
8777 +       init_timer(&timerlist);
8778 +       timerlist.function = sir_timeout;
8779 +       timerlist.data = 0xabadcafe;
8780 +
8781 +       return 0;
8782 +}
8783 +
8784 +static void drop_port(void)
8785 +{
8786 +       disable_irq(irq);
8787 +       free_irq(irq, NULL);
8788 +       del_timer_sync(&timerlist);
8789 +#ifndef CONFIG_LIRC_ON_SA1100
8790 +       release_region(io, 8);
8791 +#endif
8792 +}
8793 +
8794 +#ifdef CONFIG_LIRC_SIR_ACTISYS_ACT200L
8795 +/******************************************************/
8796 +/* Crystal/Cirrus CS8130 IR transceiver, used in Actisys Act200L dongle */
8797 +/* some code borrowed from Linux IRDA driver */
8798 +
8799 +/* Regsiter 0: Control register #1 */
8800 +#define ACT200L_REG0    0x00
8801 +#define ACT200L_TXEN    0x01 /* Enable transmitter */
8802 +#define ACT200L_RXEN    0x02 /* Enable receiver */
8803 +#define ACT200L_ECHO    0x08 /* Echo control chars */
8804 +
8805 +/* Register 1: Control register #2 */
8806 +#define ACT200L_REG1    0x10
8807 +#define ACT200L_LODB    0x01 /* Load new baud rate count value */
8808 +#define ACT200L_WIDE    0x04 /* Expand the maximum allowable pulse */
8809 +
8810 +/* Register 3: Transmit mode register #2 */
8811 +#define ACT200L_REG3    0x30
8812 +#define ACT200L_B0      0x01 /* DataBits, 0=6, 1=7, 2=8, 3=9(8P)  */
8813 +#define ACT200L_B1      0x02 /* DataBits, 0=6, 1=7, 2=8, 3=9(8P)  */
8814 +#define ACT200L_CHSY    0x04 /* StartBit Synced 0=bittime, 1=startbit */
8815 +
8816 +/* Register 4: Output Power register */
8817 +#define ACT200L_REG4    0x40
8818 +#define ACT200L_OP0     0x01 /* Enable LED1C output */
8819 +#define ACT200L_OP1     0x02 /* Enable LED2C output */
8820 +#define ACT200L_BLKR    0x04
8821 +
8822 +/* Register 5: Receive Mode register */
8823 +#define ACT200L_REG5    0x50
8824 +#define ACT200L_RWIDL   0x01 /* fixed 1.6us pulse mode */
8825 +    /*.. other various IRDA bit modes, and TV remote modes..*/
8826 +
8827 +/* Register 6: Receive Sensitivity register #1 */
8828 +#define ACT200L_REG6    0x60
8829 +#define ACT200L_RS0     0x01 /* receive threshold bit 0 */
8830 +#define ACT200L_RS1     0x02 /* receive threshold bit 1 */
8831 +
8832 +/* Register 7: Receive Sensitivity register #2 */
8833 +#define ACT200L_REG7    0x70
8834 +#define ACT200L_ENPOS   0x04 /* Ignore the falling edge */
8835 +
8836 +/* Register 8,9: Baud Rate Dvider register #1,#2 */
8837 +#define ACT200L_REG8    0x80
8838 +#define ACT200L_REG9    0x90
8839 +
8840 +#define ACT200L_2400    0x5f
8841 +#define ACT200L_9600    0x17
8842 +#define ACT200L_19200   0x0b
8843 +#define ACT200L_38400   0x05
8844 +#define ACT200L_57600   0x03
8845 +#define ACT200L_115200  0x01
8846 +
8847 +/* Register 13: Control register #3 */
8848 +#define ACT200L_REG13   0xd0
8849 +#define ACT200L_SHDW    0x01 /* Enable access to shadow registers */
8850 +
8851 +/* Register 15: Status register */
8852 +#define ACT200L_REG15   0xf0
8853 +
8854 +/* Register 21: Control register #4 */
8855 +#define ACT200L_REG21   0x50
8856 +#define ACT200L_EXCK    0x02 /* Disable clock output driver */
8857 +#define ACT200L_OSCL    0x04 /* oscillator in low power, medium accuracy mode */
8858 +
8859 +static void init_act200(void)
8860 +{
8861 +  int i;
8862 +       __u8 control[] = {
8863 +               ACT200L_REG15,
8864 +               ACT200L_REG13 | ACT200L_SHDW,
8865 +               ACT200L_REG21 | ACT200L_EXCK | ACT200L_OSCL,
8866 +               ACT200L_REG13,
8867 +               ACT200L_REG7  | ACT200L_ENPOS,
8868 +               ACT200L_REG6  | ACT200L_RS0  | ACT200L_RS1,
8869 +               ACT200L_REG5  | ACT200L_RWIDL,
8870 +               ACT200L_REG4  | ACT200L_OP0  | ACT200L_OP1 | ACT200L_BLKR,
8871 +               ACT200L_REG3  | ACT200L_B0,
8872 +               ACT200L_REG0  | ACT200L_TXEN | ACT200L_RXEN,
8873 +               ACT200L_REG8 |  (ACT200L_115200       & 0x0f),
8874 +               ACT200L_REG9 | ((ACT200L_115200 >> 4) & 0x0f),
8875 +               ACT200L_REG1 | ACT200L_LODB | ACT200L_WIDE
8876 +       };
8877 +
8878 +       /* Set DLAB 1. */
8879 +       soutp(UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN8);
8880 +
8881 +       /* Set divisor to 12 => 9600 Baud */
8882 +       soutp(UART_DLM,0);
8883 +       soutp(UART_DLL,12);
8884 +
8885 +       /* Set DLAB 0. */
8886 +       soutp(UART_LCR, UART_LCR_WLEN8);
8887 +       /* Set divisor to 12 => 9600 Baud */
8888 +
8889 +       /* power supply */
8890 +       soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
8891 +       for (i=0; i<50; i++) {
8892 +               safe_udelay(1000);
8893 +       }
8894 +
8895 +               /* Reset the dongle : set RTS low for 25 ms */
8896 +       soutp(UART_MCR, UART_MCR_DTR|UART_MCR_OUT2);
8897 +       for (i=0; i<25; i++) {
8898 +               udelay(1000);
8899 +       }
8900 +
8901 +       soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
8902 +       udelay(100);
8903 +
8904 +       /* Clear DTR and set RTS to enter command mode */
8905 +       soutp(UART_MCR, UART_MCR_RTS|UART_MCR_OUT2);
8906 +       udelay(7);
8907 +
8908 +/* send out the control register settings for 115K 7N1 SIR operation */
8909 +       for (i=0; i<sizeof(control); i++) {
8910 +               soutp(UART_TX, control[i]);
8911 +               /* one byte takes ~1042 usec to transmit at 9600,8N1 */
8912 +               udelay(1500);
8913 +       }
8914 +
8915 +       /* back to normal operation */
8916 +       soutp(UART_MCR, UART_MCR_RTS|UART_MCR_DTR|UART_MCR_OUT2);
8917 +       udelay(50);
8918 +
8919 +       udelay(1500);
8920 +       soutp(UART_LCR, sinp(UART_LCR) | UART_LCR_DLAB);
8921 +
8922 +       /* Set DLAB 1. */
8923 +       soutp(UART_LCR, UART_LCR_DLAB | UART_LCR_WLEN7);
8924 +
8925 +       /* Set divisor to 1 => 115200 Baud */
8926 +       soutp(UART_DLM,0);
8927 +       soutp(UART_DLL,1);
8928 +
8929 +       /* Set DLAB 0. */
8930 +       soutp(UART_LCR, sinp(UART_LCR) & (~UART_LCR_DLAB));
8931 +
8932 +       /* Set DLAB 0, 7 Bit */
8933 +       soutp(UART_LCR, UART_LCR_WLEN7);
8934 +
8935 +       /* enable interrupts */
8936 +       soutp(UART_IER, sinp(UART_IER)|UART_IER_RDI);
8937 +}
8938 +#endif
8939 +
8940 +int init_lirc_sir(void)
8941 +{
8942 +       int retval;
8943 +
8944 +       init_waitqueue_head(&lirc_read_queue);
8945 +       retval = init_port();
8946 +       if (retval < 0)
8947 +               return retval;
8948 +       init_hardware();
8949 +       enable_irq(irq);
8950 +       printk(KERN_INFO LIRC_DRIVER_NAME
8951 +               ": Installed.\n");
8952 +       return 0;
8953 +}
8954 +
8955 +#ifdef CONFIG_LIRC_SIR_TEKRAM
8956 +MODULE_AUTHOR("Christoph Bartelmus");
8957 +MODULE_DESCRIPTION("Infrared receiver driver for Tekram Irmate 210");
8958 +#elif defined(CONFIG_LIRC_ON_SA1100)
8959 +MODULE_AUTHOR("Christoph Bartelmus");
8960 +MODULE_DESCRIPTION("LIRC driver for StrongARM SA1100 embedded microprocessor");
8961 +#elif defined(CONFIG_LIRC_SIR_ACTISYS_ACT200L)
8962 +MODULE_AUTHOR("Karl Bongers");
8963 +MODULE_DESCRIPTION("LIRC driver for Actisys Act200L");
8964 +#else
8965 +MODULE_AUTHOR("Milan Pikula");
8966 +MODULE_DESCRIPTION("Infrared receiver driver for SIR type serial ports");
8967 +#endif
8968 +
8969 +#ifdef CONFIG_LIRC_ON_SA1100
8970 +MODULE_PARM(irq, "i");
8971 +MODULE_PARM_DESC(irq, "Interrupt (16)");
8972 +#else
8973 +MODULE_PARM(io, "i");
8974 +MODULE_PARM_DESC(io, "I/O address base (0x3f8 or 0x2f8)");
8975 +MODULE_PARM(irq, "i");
8976 +MODULE_PARM_DESC(irq, "Interrupt (4 or 3)");
8977 +MODULE_PARM(threshold, "i");
8978 +MODULE_PARM_DESC(threshold, "space detection threshold (3)");
8979 +#endif
8980 +
8981 +MODULE_LICENSE("GPL");
8982 +
8983 +static int __init lirc_sir_init(void)
8984 +{
8985 +       int retval;
8986 +
8987 +       retval=init_chrdev();
8988 +       if(retval < 0)
8989 +               return retval;
8990 +       retval = init_lirc_sir();
8991 +       if (retval) {
8992 +               drop_chrdev();
8993 +               return retval;
8994 +       }
8995 +       return 0;
8996 +}
8997 +
8998 +static void __exit lirc_sir_exit(void)
8999 +{
9000 +#ifdef CONFIG_MODULE_UNLOAD
9001 +       if(module_refcount(THIS_MODULE)) return;
9002 +#endif
9003 +       drop_hardware();
9004 +       drop_chrdev();
9005 +       drop_port();
9006 +       printk(KERN_INFO LIRC_DRIVER_NAME ": Uninstalled.\n");
9007 +}
9008 +
9009 +module_init(lirc_sir_init);
9010 +module_exit(lirc_sir_exit);
9011 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/lirc/Makefile linux-2.6.5/drivers/char/lirc/Makefile
9012 --- linux-2.6.5.orig/drivers/char/lirc/Makefile 1970-01-01 01:00:00.000000000 +0100
9013 +++ linux-2.6.5/drivers/char/lirc/Makefile      2004-04-04 14:05:29.434624528 +0200
9014 @@ -0,0 +1,14 @@
9015 +#
9016 +# Makefile for the lirc drivers
9017 +#
9018 +
9019 +obj-$(CONFIG_LIRC_SUPPORT) += lirc_dev.o
9020 +obj-$(CONFIG_LIRC_GPIO) += lirc_gpio.o
9021 +obj-$(CONFIG_LIRC_BT829) += lirc_bt829.o
9022 +obj-$(CONFIG_LIRC_IT87) += lirc_it87.o
9023 +obj-$(CONFIG_LIRC_PARALLEL) += lirc_parallel.o
9024 +obj-$(CONFIG_LIRC_SERIAL) += lirc_serial.o
9025 +obj-$(CONFIG_LIRC_SIR) += lirc_sir.o
9026 +obj-$(CONFIG_LIRC_ATIUSB) += lirc_atiusb.o
9027 +obj-$(CONFIG_LIRC_MCEUSB) += lirc_mceusb.o
9028 +obj-$(CONFIG_LIRC_I2C) += lirc_i2c.o
9029 diff -uprN -X dontdiff linux-2.6.5.orig/drivers/char/Makefile linux-2.6.5/drivers/char/Makefile
9030 --- linux-2.6.5.orig/drivers/char/Makefile      2004-04-04 13:59:17.630147400 +0200
9031 +++ linux-2.6.5/drivers/char/Makefile   2004-04-04 14:05:29.437624072 +0200
9032 @@ -7,7 +7,7 @@
9033  #
9034  FONTMAPFILE = cp437.uni
9035  
9036 -obj-y   += mem.o random.o tty_io.o n_tty.o tty_ioctl.o pty.o misc.o
9037 +obj-y   += mem.o random.o tty_io.o n_tty.o tty_ioctl.o pty.o misc.o lirc/
9038  
9039  obj-$(CONFIG_VT)               += vt_ioctl.o vc_screen.o consolemap.o \
9040                                    consolemap_deftbl.o selection.o keyboard.o
9041 diff -uprN -X dontdiff linux-2.6.5.orig/include/linux/lirc.h linux-2.6.5/include/linux/lirc.h
9042 --- linux-2.6.5.orig/include/linux/lirc.h       1970-01-01 01:00:00.000000000 +0100
9043 +++ linux-2.6.5/include/linux/lirc.h    2004-04-04 14:05:29.439623768 +0200
9044 @@ -0,0 +1,103 @@
9045 +/*      $Id$      */
9046 +
9047 +#ifndef _LINUX_LIRC_H
9048 +#define _LINUX_LIRC_H
9049 +
9050 +#if defined (__linux__)
9051 +#include <asm/types.h>
9052 +#include <linux/ioctl.h>
9053 +#else
9054 +#include <sys/types.h>
9055 +typedef u_int32_t __u32;
9056 +#endif
9057 +
9058 +#define PULSE_BIT  0x01000000
9059 +#define PULSE_MASK 0x00FFFFFF
9060 +
9061 +typedef int lirc_t;
9062 +
9063 +/*
9064 + * lirc compatible hardware features
9065 + */
9066 +
9067 +
9068 +#define LIRC_MODE2SEND(x) (x)
9069 +#define LIRC_SEND2MODE(x) (x)
9070 +#define LIRC_MODE2REC(x) ((x) << 16)
9071 +#define LIRC_REC2MODE(x) ((x) >> 16)
9072 +
9073 +#define LIRC_MODE_RAW                  0x00000001
9074 +#define LIRC_MODE_PULSE                0x00000002
9075 +#define LIRC_MODE_MODE2                0x00000004
9076 +#define LIRC_MODE_CODE                 0x00000008
9077 +#define LIRC_MODE_LIRCCODE             0x00000010
9078 +#define LIRC_MODE_STRING               0x00000020
9079 +
9080 +
9081 +#define LIRC_CAN_SEND_RAW              LIRC_MODE2SEND(LIRC_MODE_RAW)
9082 +#define LIRC_CAN_SEND_PULSE            LIRC_MODE2SEND(LIRC_MODE_PULSE)
9083 +#define LIRC_CAN_SEND_MODE2            LIRC_MODE2SEND(LIRC_MODE_MODE2)
9084 +#define LIRC_CAN_SEND_CODE             LIRC_MODE2SEND(LIRC_MODE_CODE)
9085 +#define LIRC_CAN_SEND_LIRCCODE         LIRC_MODE2SEND(LIRC_MODE_LIRCCODE)
9086 +#define LIRC_CAN_SEND_STRING           LIRC_MODE2SEND(LIRC_MODE_STRING)
9087 +
9088 +#define LIRC_CAN_SEND_MASK             0x0000003f
9089 +
9090 +#define LIRC_CAN_SET_SEND_CARRIER      0x00000100
9091 +#define LIRC_CAN_SET_SEND_DUTY_CYCLE   0x00000200
9092 +
9093 +#define LIRC_CAN_REC_RAW               LIRC_MODE2REC(LIRC_MODE_RAW)
9094 +#define LIRC_CAN_REC_PULSE             LIRC_MODE2REC(LIRC_MODE_PULSE)
9095 +#define LIRC_CAN_REC_MODE2             LIRC_MODE2REC(LIRC_MODE_MODE2)
9096 +#define LIRC_CAN_REC_CODE              LIRC_MODE2REC(LIRC_MODE_CODE)
9097 +#define LIRC_CAN_REC_LIRCCODE          LIRC_MODE2REC(LIRC_MODE_LIRCCODE)
9098 +#define LIRC_CAN_REC_STRING            LIRC_MODE2REC(LIRC_MODE_STRING)
9099 +
9100 +#define LIRC_CAN_REC_MASK              LIRC_MODE2REC(LIRC_CAN_SEND_MASK)
9101 +
9102 +#define LIRC_CAN_SET_REC_CARRIER       (LIRC_CAN_SET_SEND_CARRIER << 16)
9103 +#define LIRC_CAN_SET_REC_DUTY_CYCLE    (LIRC_CAN_SET_SEND_DUTY_CYCLE << 16)
9104 +
9105 +#define LIRC_CAN_SET_REC_DUTY_CYCLE_RANGE 0x40000000
9106 +#define LIRC_CAN_SET_REC_CARRIER_RANGE    0x80000000
9107 +
9108 +
9109 +#define LIRC_CAN_SEND(x) ((x)&LIRC_CAN_SEND_MASK)
9110 +#define LIRC_CAN_REC(x) ((x)&LIRC_CAN_REC_MASK)
9111 +
9112 +/*
9113 + * IOCTL commands for lirc driver
9114 + */
9115 +
9116 +#define LIRC_GET_FEATURES              _IOR('i', 0x00000000, __u32)
9117 +
9118 +#define LIRC_GET_SEND_MODE             _IOR('i', 0x00000001, __u32)
9119 +#define LIRC_GET_REC_MODE              _IOR('i', 0x00000002, __u32)
9120 +#define LIRC_GET_SEND_CARRIER          _IOR('i', 0x00000003, __u32)
9121 +#define LIRC_GET_REC_CARRIER           _IOR('i', 0x00000004, __u32)
9122 +#define LIRC_GET_SEND_DUTY_CYCLE       _IOR('i', 0x00000005, __u32)
9123 +#define LIRC_GET_REC_DUTY_CYCLE        _IOR('i', 0x00000006, __u32)
9124 +
9125 +/* code length in bits, currently only for LIRC_MODE_LIRCCODE */
9126 +#define LIRC_GET_LENGTH                _IOR('i', 0x0000000f, __u32)
9127 +
9128 +#define LIRC_SET_SEND_MODE             _IOW('i', 0x00000011, __u32)
9129 +#define LIRC_SET_REC_MODE              _IOW('i', 0x00000012, __u32)
9130 +/* Note: these can reset the according pulse_width */
9131 +#define LIRC_SET_SEND_CARRIER          _IOW('i', 0x00000013, __u32)
9132 +#define LIRC_SET_REC_CARRIER           _IOW('i', 0x00000014, __u32)
9133 +#define LIRC_SET_SEND_DUTY_CYCLE       _IOW('i', 0x00000015, __u32)
9134 +#define LIRC_SET_REC_DUTY_CYCLE        _IOW('i', 0x00000016, __u32)
9135 +
9136 +/* to set a range use
9137 +   LIRC_SET_REC_DUTY_CYCLE_RANGE/LIRC_SET_REC_CARRIER_RANGE with the
9138 +   lower bound first and later
9139 +   LIRC_SET_REC_DUTY_CYCLE/LIRC_SET_REC_CARRIER with the upper bound */
9140 +
9141 +#define LIRC_SET_REC_DUTY_CYCLE_RANGE  _IOW('i', 0x0000001e, __u32)
9142 +#define LIRC_SET_REC_CARRIER_RANGE     _IOW('i', 0x0000001f, __u32)
9143 +
9144 +#define DEV_LIRC       "lirc"
9145 +#define IRCTL_DEV_MAJOR 61
9146 +
9147 +#endif
This page took 0.688201 seconds and 3 git commands to generate.