3 * BRIEF MODULE DESCRIPTION
4 * Qtronix 990P infrared keyboard driver.
7 * Copyright 2001 MontaVista Software Inc.
8 * Author: MontaVista Software, Inc.
9 * ppopov@mvista.com or source@mvista.com
12 * The bottom portion of this driver was take from
13 * pc_keyb.c Please see that file for copyrights.
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
23 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * You should have received a copy of the GNU General Public License along
32 * with this program; if not, write to the Free Software Foundation, Inc.,
33 * 675 Mass Ave, Cambridge, MA 02139, USA.
36 #include <linux/config.h>
41 * This driver has only been tested with the Consumer IR
42 * port of the ITE 8172 system controller.
44 * You do not need this driver if you are using the ps/2 or
45 * USB adapter that the keyboard ships with. You only need
46 * this driver if your board has a IR port and the keyboard
47 * data is being sent directly to the IR. In that case,
48 * you also need some low-level IR support. See it8172_cir.c.
52 #ifdef CONFIG_QTRONIX_KEYBOARD
54 #include <linux/module.h>
55 #include <linux/types.h>
56 #include <linux/pci.h>
57 #include <linux/kernel.h>
59 #include <asm/it8172/it8172.h>
60 #include <asm/it8172/it8172_int.h>
61 #include <asm/it8172/it8172_cir.h>
63 #include <linux/spinlock.h>
64 #include <linux/sched.h>
65 #include <linux/interrupt.h>
66 #include <linux/tty.h>
68 #include <linux/signal.h>
69 #include <linux/init.h>
70 #include <linux/kbd_ll.h>
71 #include <linux/delay.h>
72 #include <linux/random.h>
73 #include <linux/poll.h>
74 #include <linux/miscdevice.h>
75 #include <linux/slab.h>
76 #include <linux/kbd_kern.h>
77 #include <linux/smp_lock.h>
79 #include <linux/pc_keyb.h>
81 #include <asm/keyboard.h>
82 #include <asm/bitops.h>
83 #include <asm/uaccess.h>
85 #include <asm/system.h>
90 #define KBD_CIR_PORT 0
91 #define AUX_RECONNECT 170 /* scancode when ps2 device is plugged (back) in */
93 static int data_index
;
95 static unsigned char kbdbytes
[5];
96 static unsigned char cir_data
[32]; /* we only need 16 chars */
98 static void kbd_int_handler(int irq
, void *dev_id
, struct pt_regs
*regs
);
99 static int handle_data(unsigned char *p_data
);
100 static inline void handle_mouse_event(unsigned char scancode
);
101 static inline void handle_keyboard_event(unsigned char scancode
, int down
);
102 static int __init
psaux_init(void);
104 static struct aux_queue
*queue
; /* Mouse data buffer. */
105 static int aux_count
= 0;
108 * Keys accessed through the 'Fn' key
109 * The Fn key does not produce a key-up sequence. So, the first
110 * time the user presses it, it will be key-down event. The key
111 * stays down until the user presses it again.
113 #define NUM_FN_KEYS 56
114 static unsigned char fn_keys
[NUM_FN_KEYS
] = {
115 0,0,0,0,0,0,0,0, /* 0 7 */
116 8,9,10,93,0,0,0,0, /* 8 15 */
117 0,0,0,0,0,0,0,5, /* 16 23 */
118 6,7,91,0,0,0,0,0, /* 24 31 */
119 0,0,0,0,0,2,3,4, /* 32 39 */
120 92,0,0,0,0,0,0,0, /* 40 47 */
121 0,0,0,0,11,0,94,95 /* 48 55 */
125 void __init
init_qtronix_990P_kbd(void)
129 cir
= (struct cir_port
*)kmalloc(sizeof(struct cir_port
), GFP_KERNEL
);
131 printk("Unable to initialize Qtronix keyboard\n");
137 * this should be programmable, somehow by the, by the user.
139 cir
->port
= KBD_CIR_PORT
;
140 cir
->baud_rate
= 0x1d;
148 retval
= request_irq(IT8172_CIR0_IRQ
, kbd_int_handler
,
149 (unsigned long )(SA_INTERRUPT
|SA_SHIRQ
),
150 (const char *)"Qtronix IR Keyboard", (void *)cir
);
153 printk("unable to allocate cir %d irq %d\n",
154 cir
->port
, IT8172_CIR0_IRQ
);
156 #ifdef CONFIG_PSMOUSE
161 static inline unsigned char BitReverse(unsigned short key
)
163 unsigned char rkey
= 0;
164 rkey
|= (key
& 0x1) << 7;
165 rkey
|= (key
& 0x2) << 5;
166 rkey
|= (key
& 0x4) << 3;
167 rkey
|= (key
& 0x8) << 1;
168 rkey
|= (key
& 0x10) >> 1;
169 rkey
|= (key
& 0x20) >> 3;
170 rkey
|= (key
& 0x40) >> 5;
171 rkey
|= (key
& 0x80) >> 7;
177 static inline u_int8_t
UpperByte(u_int8_t data
)
183 static inline u_int8_t
LowerByte(u_int8_t data
)
189 int CheckSumOk(u_int8_t byte1
, u_int8_t byte2
,
190 u_int8_t byte3
, u_int8_t byte4
, u_int8_t byte5
)
194 CheckSum
= (byte1
& 0x0F) + byte2
+ byte3
+ byte4
+ byte5
;
195 if ( LowerByte(UpperByte(CheckSum
) + LowerByte(CheckSum
)) != UpperByte(byte1
) )
202 static void kbd_int_handler(int irq
, void *dev_id
, struct pt_regs
*regs
)
204 struct cir_port
*cir
;
206 unsigned char int_status
;
208 cir
= (struct cir_port
*)dev_id
;
209 int_status
= get_int_status(cir
);
210 if (int_status
& 0x4) {
215 while (cir_get_rx_count(cir
)) {
217 cir_data
[data_index
] = cir_read_data(cir
);
219 if (data_index
== 0) {/* expecting first byte */
220 if (cir_data
[data_index
] != leading1
) {
221 //printk("!leading byte %x\n", cir_data[data_index]);
227 if (data_index
== 1) {
228 if ((cir_data
[data_index
] & 0xf) != leading2
) {
230 data_index
= 0; /* start over */
236 if ( (cir_data
[data_index
] == 0xff)) { /* last byte */
237 //printk("data_index %d\n", data_index);
240 for (j
=0; j
<=data_index
; j
++) {
241 printk("rx_data %d: %x\n", j
, cir_data
[j
]);
245 handle_data(cir_data
);
248 else if (data_index
>16) {
251 printk("warning: data_index %d\n", data_index
);
252 for (j
=0; j
<=data_index
; j
++) {
253 printk("rx_data %d: %x\n", j
, cir_data
[j
]);
265 #define NUM_KBD_BYTES 5
266 static int handle_data(unsigned char *p_data
)
268 u_int32_t bit_bucket
;
270 u_int32_t got_bits
, next_byte
;
273 /* Reorganize the bit stream */
275 p_data
[i
] = BitReverse(~p_data
[i
]);
278 * We've already previously checked that p_data[0]
279 * is equal to leading1 and that (p_data[1] & 0xf)
280 * is equal to leading2. These twelve bits are the
281 * leader code. We can now throw them away (the 12
282 * bits) and continue parsing the stream.
284 bit_bucket
= p_data
[1] << 12;
289 * Process four bits at a time
291 for (i
=0; i
<NUM_KBD_BYTES
; i
++) {
295 for (j
=0; j
<8; j
++) /* 8 bits per byte */
298 bit_bucket
|= (p_data
[next_byte
++] << (8 - got_bits
));
302 if ((bit_bucket
& 0xF000) == 0x8000) {
303 /* Convert 1000b to 1 */
304 kbdbytes
[i
] = 0x80 | (kbdbytes
[i
] >> 1);
306 bit_bucket
= bit_bucket
<< 4;
308 else if ((bit_bucket
& 0xC000) == 0x8000) {
309 /* Convert 10b to 0 */
310 kbdbytes
[i
] = kbdbytes
[i
] >> 1;
312 bit_bucket
= bit_bucket
<< 2;
315 /* bad serial stream */
319 if (next_byte
> 16) {
320 //printk("error: too many bytes\n");
327 if (!CheckSumOk(kbdbytes
[0], kbdbytes
[1],
328 kbdbytes
[2], kbdbytes
[3], kbdbytes
[4])) {
329 //printk("checksum failed\n");
333 if (kbdbytes
[1] & 0x08) {
334 //printk("m: %x %x %x\n", kbdbytes[1], kbdbytes[2], kbdbytes[3]);
335 handle_mouse_event(kbdbytes
[1]);
336 handle_mouse_event(kbdbytes
[2]);
337 handle_mouse_event(kbdbytes
[3]);
340 if (kbdbytes
[2] == 0) down
= 1;
343 printk("down %d\n", kbdbytes
[3]);
345 printk("up %d\n", kbdbytes
[3]);
347 handle_keyboard_event(kbdbytes
[3], down
);
353 spinlock_t kbd_controller_lock
= SPIN_LOCK_UNLOCKED
;
354 static unsigned char handle_kbd_event(void);
357 int kbd_setkeycode(unsigned int scancode
, unsigned int keycode
)
359 printk("kbd_setkeycode scancode %x keycode %x\n", scancode
, keycode
);
363 int kbd_getkeycode(unsigned int scancode
)
369 int kbd_translate(unsigned char scancode
, unsigned char *keycode
,
372 static int prev_scancode
= 0;
374 if (scancode
== 0x00 || scancode
== 0xff) {
380 if (!prev_scancode
&& scancode
== 160) { /* Fn key down */
381 //printk("Fn key down\n");
385 else if (prev_scancode
&& scancode
== 160) { /* Fn key up */
386 //printk("Fn key up\n");
392 if (prev_scancode
== 160) {
393 if (scancode
<= NUM_FN_KEYS
) {
394 *keycode
= fn_keys
[scancode
];
395 //printk("fn keycode %d\n", *keycode);
400 else if (scancode
<= 127) {
410 char kbd_unexpected_up(unsigned char keycode
)
412 //printk("kbd_unexpected_up\n");
416 static unsigned char kbd_exists
= 1;
418 static inline void handle_keyboard_event(unsigned char scancode
, int down
)
421 handle_scancode(scancode
, down
);
422 tasklet_schedule(&keyboard_tasklet
);
426 void kbd_leds(unsigned char leds
)
431 void kbd_init_hw(void)
437 static inline void handle_mouse_event(unsigned char scancode
)
439 if(scancode
== AUX_RECONNECT
){
440 queue
->head
= queue
->tail
= 0; /* Flush input queue */
441 // __aux_write_ack(AUX_ENABLE_DEV); /* ping the mouse :) */
445 add_mouse_randomness(scancode
);
447 int head
= queue
->head
;
449 queue
->buf
[head
] = scancode
;
450 head
= (head
+ 1) & (AUX_BUF_SIZE
-1);
451 if (head
!= queue
->tail
) {
453 kill_fasync(&queue
->fasync
, SIGIO
, POLL_IN
);
454 wake_up_interruptible(&queue
->proc_list
);
459 static unsigned char get_from_queue(void)
461 unsigned char result
;
464 spin_lock_irqsave(&kbd_controller_lock
, flags
);
465 result
= queue
->buf
[queue
->tail
];
466 queue
->tail
= (queue
->tail
+ 1) & (AUX_BUF_SIZE
-1);
467 spin_unlock_irqrestore(&kbd_controller_lock
, flags
);
472 static inline int queue_empty(void)
474 return queue
->head
== queue
->tail
;
477 static int fasync_aux(int fd
, struct file
*filp
, int on
)
481 //printk("fasync_aux\n");
482 retval
= fasync_helper(fd
, filp
, on
, &queue
->fasync
);
490 * Random magic cookie for the aux device
492 #define AUX_DEV ((void *)queue)
494 static int release_aux(struct inode
* inode
, struct file
* file
)
496 fasync_aux(-1, file
, 0);
501 static int open_aux(struct inode
* inode
, struct file
* file
)
506 queue
->head
= queue
->tail
= 0; /* Flush input queue */
511 * Put bytes from input queue to buffer.
514 static ssize_t
read_aux(struct file
* file
, char * buffer
,
515 size_t count
, loff_t
*ppos
)
517 DECLARE_WAITQUEUE(wait
, current
);
522 if (file
->f_flags
& O_NONBLOCK
)
524 add_wait_queue(&queue
->proc_list
, &wait
);
526 set_current_state(TASK_INTERRUPTIBLE
);
527 if (queue_empty() && !signal_pending(current
)) {
531 current
->state
= TASK_RUNNING
;
532 remove_wait_queue(&queue
->proc_list
, &wait
);
534 while (i
> 0 && !queue_empty()) {
535 c
= get_from_queue();
536 put_user(c
, buffer
++);
540 file
->f_dentry
->d_inode
->i_atime
= CURRENT_TIME
;
543 if (signal_pending(current
))
549 * Write to the aux device.
552 static ssize_t
write_aux(struct file
* file
, const char * buffer
,
553 size_t count
, loff_t
*ppos
)
556 * The ITE boards this was tested on did not have the
557 * transmit wires connected.
562 static unsigned int aux_poll(struct file
*file
, poll_table
* wait
)
564 poll_wait(file
, &queue
->proc_list
, wait
);
566 return POLLIN
| POLLRDNORM
;
570 struct file_operations psaux_fops
= {
575 .release
= release_aux
,
576 .fasync
= fasync_aux
,
582 static struct miscdevice psaux_mouse
= {
583 PSMOUSE_MINOR
, "psaux", &psaux_fops
586 static int __init
psaux_init(void)
590 retval
= misc_register(&psaux_mouse
);
594 queue
= (struct aux_queue
*) kmalloc(sizeof(*queue
), GFP_KERNEL
);
595 memset(queue
, 0, sizeof(*queue
));
596 queue
->head
= queue
->tail
= 0;
597 init_waitqueue_head(&queue
->proc_list
);
601 module_init(init_qtronix_990P_kbd
);