aboutsummaryrefslogtreecommitdiffstats
path: root/ubuntu/omnibook/kbc.c
blob: 5634b40565c3d00f6008ddb21f1c8296ef0d785e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * kbc.c -- low level functions to access Keyboard Controller
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Written by Soós Péter <sp@osb.hu>, 2002-2004
 * Modified by Mathieu Bérard <mathieu.berard@crans.org>, 2006
 */

#include "omnibook.h"

#include <linux/types.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/spinlock.h>
#include <linux/ioport.h>

#include <asm/io.h>
#include "hardware.h"

extern int omnibook_key_polling_enable(void);
extern int omnibook_key_polling_disable(void);

/*
 * Registers of the keyboard controller
 */

#define OMNIBOOK_KBC_DATA		0x60
#define OMNIBOOK_KBC_SC			0x64

/*
 * Keyboard controller status register bits
 */

#define OMNIBOOK_KBC_STAT_OBF		0x01	/* Output buffer full */
#define OMNIBOOK_KBC_STAT_IBF		0x02	/* Input buffer full */


/*
 * Interrupt control
 */

static DEFINE_SPINLOCK(omnibook_kbc_lock);

/*
 * Wait for keyboard buffer
 */

static int omnibook_kbc_wait(u8 event)
{
	int timeout = OMNIBOOK_TIMEOUT;

	switch (event) {
	case OMNIBOOK_KBC_STAT_OBF:
		while (!(inb(OMNIBOOK_KBC_SC) & event) && timeout--)
			mdelay(1);
		break;
	case OMNIBOOK_KBC_STAT_IBF:
		while ((inb(OMNIBOOK_KBC_SC) & event) && timeout--)
			mdelay(1);
		break;
	default:
		return -EINVAL;
	}
	if (timeout > 0)
		return 0;
	return -ETIME;
}

/*
 * Write to the keyboard command register
 */

static int omnibook_kbc_write_command(u8 cmd)
{
	int retval;

	spin_lock_irq(&omnibook_kbc_lock);
	retval = omnibook_kbc_wait(OMNIBOOK_KBC_STAT_IBF);
	if (retval)
		goto end;
	outb(cmd, OMNIBOOK_KBC_SC);
	retval = omnibook_kbc_wait(OMNIBOOK_KBC_STAT_IBF);
      end:
	spin_unlock_irq(&omnibook_kbc_lock);
	return retval;
}

/*
 * Write to the keyboard data register
 */

static int omnibook_kbc_write_data(u8 data)
{
	int retval;

	spin_lock_irq(&omnibook_kbc_lock);
	retval = omnibook_kbc_wait(OMNIBOOK_KBC_STAT_IBF);
	if (retval)
		goto end;
	outb(data, OMNIBOOK_KBC_DATA);
	retval = omnibook_kbc_wait(OMNIBOOK_KBC_STAT_IBF);
      end:
	spin_unlock_irq(&omnibook_kbc_lock);
	return retval;
}

/*
 * Send a command to keyboard controller
 */

static int omnibook_kbc_command(const struct omnibook_operation *io_op, u8 data)
{
	int retval;

	if ((retval = omnibook_kbc_write_command(OMNIBOOK_KBC_CONTROL_CMD)))
		return retval;

	retval = omnibook_kbc_write_data(data);
	return retval;
}

/*
 * Onetouch button hotkey handler
 */
static int omnibook_kbc_hotkeys(const struct omnibook_operation *io_op, unsigned int state)
{
	int retval;

	retval = __omnibook_toggle(io_op, !!(state & HKEY_ONETOUCH));
	return retval;
}

/*
 * Backend interface declarations
 */
struct omnibook_backend kbc_backend = {
	.name = "i8042",
	.hotkeys_write_cap = HKEY_ONETOUCH,
	.byte_write = omnibook_kbc_command,
	.hotkeys_set = omnibook_kbc_hotkeys,
};

/* End of file */