summaryrefslogtreecommitdiff
path: root/drivers/char/ec3104_keyb.c
blob: abac18b1871c5a7102ea15d9b69f39e9dc461875 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 * linux/drivers/char/ec3104_keyb.c
 * 
 * Copyright (C) 2000 Philipp Rumpf <prumpf@tux.org>
 *
 * based on linux/drivers/char/pc_keyb.c, which had the following comments:
 *
 * Separation of the PC low-level part by Geert Uytterhoeven, May 1997
 * See keyboard.c for the whole history.
 *
 * Major cleanup by Martin Mares, May 1997
 *
 * Combined the keyboard and PS/2 mouse handling into one file,
 * because they share the same hardware.
 * Johan Myreen <jem@iki.fi> 1998-10-08.
 *
 * Code fixes to handle mouse ACKs properly.
 * C. Scott Ananian <cananian@alumni.princeton.edu> 1999-01-29.
 */
/* EC3104 note:
 * This code was written without any documentation about the EC3104 chip.  While
 * I hope I got most of the basic functionality right, the register names I use
 * are most likely completely different from those in the chip documentation.
 *
 * If you have any further information about the EC3104, please tell me
 * (prumpf@tux.org).
 */


#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/mm.h>
#include <linux/signal.h>
#include <linux/init.h>
#include <linux/kbd_ll.h>
#include <linux/delay.h>
#include <linux/random.h>
#include <linux/poll.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/kbd_kern.h>
#include <linux/smp_lock.h>
#include <linux/bitops.h>

#include <asm/keyboard.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/system.h>
#include <asm/ec3104.h>

#include <asm/io.h>

/* Some configuration switches are present in the include file... */

#include <linux/pc_keyb.h>

#define MSR_CTS 0x10
#define MCR_RTS 0x02
#define LSR_DR 0x01
#define LSR_BOTH_EMPTY 0x60

static struct e5_struct {
	u8 packet[8];
	int pos;
	int length;

	u8 cached_mcr;
	u8 last_msr;
} ec3104_keyb;
	
/* Simple translation table for the SysRq keys */


#ifdef CONFIG_MAGIC_SYSRQ
unsigned char ec3104_kbd_sysrq_xlate[128] =
	"\000\0331234567890-=\177\t"			/* 0x00 - 0x0f */
	"qwertyuiop[]\r\000as"				/* 0x10 - 0x1f */
	"dfghjkl;'`\000\\zxcv"				/* 0x20 - 0x2f */
	"bnm,./\000*\000 \000\201\202\203\204\205"	/* 0x30 - 0x3f */
	"\206\207\210\211\212\000\000789-456+1"		/* 0x40 - 0x4f */
	"230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
	"\r\000/";					/* 0x60 - 0x6f */
#endif

static void kbd_write_command_w(int data);
static void kbd_write_output_w(int data);
#ifdef CONFIG_PSMOUSE
static void aux_write_ack(int val);
static void __aux_write_ack(int val);
#endif

static DEFINE_SPINLOCK(kbd_controller_lock);
static unsigned char handle_kbd_event(void);

/* used only by send_data - set by keyboard_interrupt */
static volatile unsigned char reply_expected;
static volatile unsigned char acknowledge;
static volatile unsigned char resend;


int ec3104_kbd_setkeycode(unsigned int scancode, unsigned int keycode)
{
	return 0;
}

int ec3104_kbd_getkeycode(unsigned int scancode)
{
	return 0;
}


/* yes, it probably would be faster to use an array.  I don't care. */

static inline unsigned char ec3104_scan2key(unsigned char scancode)
{
	switch (scancode) {
	case  1: /* '`' */
		return 41;
		
	case  2 ... 27:
		return scancode;
		
	case 28: /* '\\' */
		return 43;

	case 29 ... 39:
		return scancode + 1;

	case 40: /* '\r' */
		return 28;

	case 41 ... 50:
		return scancode + 3;

	case 51: /* ' ' */
		return 57;
		
	case 52: /* escape */
		return 1;

	case 54: /* insert/delete (labelled delete) */
		/* this should arguably be 110, but I'd like to have ctrl-alt-del
		 * working with a standard keymap */
		return 111;

	case 55: /* left */
		return 105;
	case 56: /* home */
		return 102;
	case 57: /* end */
		return 107;
	case 58: /* up */
		return 103;
	case 59: /* down */
		return 108;
	case 60: /* pgup */
		return 104;
	case 61: /* pgdown */
		return 109;
	case 62: /* right */
		return 106;

	case 79 ... 88: /* f1 - f10 */
		return scancode - 20;

	case 89 ... 90: /* f11 - f12 */
		return scancode - 2;

	case 91: /* left shift */
		return 42;

	case 92: /* right shift */
		return 54;

	case 93: /* left alt */
		return 56;
	case 94: /* right alt */
		return 100;
	case 95: /* left ctrl */
		return 29;
	case 96: /* right ctrl */
		return 97;

	case 97: /* caps lock */
		return 58;
	case 102: /* left windows */
		return 125;
	case 103: /* right windows */
		return 126;

	case 106: /* Fn */
		/* this is wrong. */
		return 84;

	default:
		return 0;
	}
}
		
int ec3104_kbd_translate(unsigned char scancode, unsigned char *keycode,
		    char raw_mode)
{
	scancode &= 0x7f;

	*keycode = ec3104_scan2key(scancode);

 	return 1;
}

char ec3104_kbd_unexpected_up(unsigned char keycode)
{
	return 0200;
}

static inline void handle_keyboard_event(unsigned char scancode)
{
#ifdef CONFIG_VT
	handle_scancode(scancode, !(scancode & 0x80));
#endif				
	tasklet_schedule(&keyboard_tasklet);
}	

void ec3104_kbd_leds(unsigned char leds)
{
}

static u8 e5_checksum(u8 *packet, int count)
{
	int i;
	u8 sum = 0;

	for (i=0; i<count; i++)
		sum ^= packet[i];
		
	if (sum & 0x80)
		sum ^= 0xc0;

	return sum;
}

static void e5_wait_for_cts(struct e5_struct *k)
{
	u8 msr;
		
	do {
		msr = ctrl_inb(EC3104_SER4_MSR);
	} while (!(msr & MSR_CTS));
}


static void e5_send_byte(u8 byte, struct e5_struct *k)
{
	u8 status;
		
	do {
		status = ctrl_inb(EC3104_SER4_LSR);
	} while ((status & LSR_BOTH_EMPTY) != LSR_BOTH_EMPTY);
	
	printk("<%02x>", byte);

	ctrl_outb(byte, EC3104_SER4_DATA);

	do {
		status = ctrl_inb(EC3104_SER4_LSR);
	} while ((status & LSR_BOTH_EMPTY) != LSR_BOTH_EMPTY);
	
}

static int e5_send_packet(u8 *packet, int count, struct e5_struct *k)
{
	int i;

	disable_irq(EC3104_IRQ_SER4);
	
	if (k->cached_mcr & MCR_RTS) {
		printk("e5_send_packet: too slow\n");
		enable_irq(EC3104_IRQ_SER4);
		return -EAGAIN;
	}

	k->cached_mcr |= MCR_RTS;
	ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);

	e5_wait_for_cts(k);

	printk("p: ");

	for(i=0; i<count; i++)
		e5_send_byte(packet[i], k);

	e5_send_byte(e5_checksum(packet, count), k);

	printk("\n");

	udelay(1500);

	k->cached_mcr &= ~MCR_RTS;
	ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);

	set_current_state(TASK_UNINTERRUPTIBLE);
	
	

	enable_irq(EC3104_IRQ_SER4);

	

	return 0;
}

/*
 * E5 packets we know about:
 * E5->host 0x80 0x05 <checksum> - resend packet
 * host->E5 0x83 0x43 <contrast> - set LCD contrast
 * host->E5 0x85 0x41 0x02 <brightness> 0x02 - set LCD backlight
 * E5->host 0x87 <ps2 packet> 0x00 <checksum> - external PS2 
 * E5->host 0x88 <scancode> <checksum> - key press
 */

static void e5_receive(struct e5_struct *k)
{
	k->packet[k->pos++] = ctrl_inb(EC3104_SER4_DATA);

	if (k->pos == 1) {
		switch(k->packet[0]) {
		case 0x80:
			k->length = 3;
			break;
			
		case 0x87: /* PS2 ext */
			k->length = 6;
			break;

		case 0x88: /* keyboard */
			k->length = 3;
			break;

		default:
			k->length = 1;
			printk(KERN_WARNING "unknown E5 packet %02x\n",
			       k->packet[0]);
		}
	}

	if (k->pos == k->length) {
		int i;

		if (e5_checksum(k->packet, k->length) != 0)
			printk(KERN_WARNING "E5: wrong checksum\n");

#if 0
		printk("E5 packet [");
		for(i=0; i<k->length; i++) {
			printk("%02x ", k->packet[i]);
		}

		printk("(%02x)]\n", e5_checksum(k->packet, k->length-1));
#endif

		switch(k->packet[0]) {
		case 0x80:
		case 0x88:
			handle_keyboard_event(k->packet[1]);
			break;
		}

		k->pos = k->length = 0;
	}
}

static void ec3104_keyb_interrupt(int irq, void *data, struct pt_regs *regs)
{
	struct e5_struct *k = &ec3104_keyb;
	u8 msr, lsr;

	msr = ctrl_inb(EC3104_SER4_MSR);
	
	if ((msr & MSR_CTS) && !(k->last_msr & MSR_CTS)) {
		if (k->cached_mcr & MCR_RTS)
			printk("confused: RTS already high\n");
		/* CTS went high.  Send RTS. */
		k->cached_mcr |= MCR_RTS;
		
		ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
	} else if ((!(msr & MSR_CTS)) && (k->last_msr & MSR_CTS)) {
		/* CTS went low. */
		if (!(k->cached_mcr & MCR_RTS))
			printk("confused: RTS already low\n");

		k->cached_mcr &= ~MCR_RTS;

		ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
	}

	k->last_msr = msr;

	lsr = ctrl_inb(EC3104_SER4_LSR);

	if (lsr & LSR_DR)
		e5_receive(k);
}

static void ec3104_keyb_clear_state(void)
{
	struct e5_struct *k = &ec3104_keyb;
	u8 msr, lsr;
	
	/* we want CTS to be low */
	k->last_msr = 0;

	for (;;) {
		msleep(100);

		msr = ctrl_inb(EC3104_SER4_MSR);
	
		lsr = ctrl_inb(EC3104_SER4_LSR);
		
		if (lsr & LSR_DR) {
			e5_receive(k);
			continue;
		}

		if ((msr & MSR_CTS) && !(k->last_msr & MSR_CTS)) {
			if (k->cached_mcr & MCR_RTS)
				printk("confused: RTS already high\n");
			/* CTS went high.  Send RTS. */
			k->cached_mcr |= MCR_RTS;
		
			ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
		} else if ((!(msr & MSR_CTS)) && (k->last_msr & MSR_CTS)) {
			/* CTS went low. */
			if (!(k->cached_mcr & MCR_RTS))
				printk("confused: RTS already low\n");
			
			k->cached_mcr &= ~MCR_RTS;
			
			ctrl_outb(k->cached_mcr, EC3104_SER4_MCR);
		} else
			break;

		k->last_msr = msr;

		continue;
	}
}

void __init ec3104_kbd_init_hw(void)
{
	ec3104_keyb.last_msr = ctrl_inb(EC3104_SER4_MSR);
	ec3104_keyb.cached_mcr = ctrl_inb(EC3104_SER4_MCR);

	ec3104_keyb_clear_state();

	/* Ok, finally allocate the IRQ, and off we go.. */
	request_irq(EC3104_IRQ_SER4, ec3104_keyb_interrupt, 0, "keyboard", NULL);
}