summaryrefslogtreecommitdiffstats
path: root/drivers/misc/ibmasm/remote.c
blob: 520c3f10c271e0cc928ae96e51cc032631b105d8 (plain)
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

/*
 * IBM ASM Service Processor Device Driver
 *
 * 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 of the License, 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Copyright (C) IBM Corporation, 2004
 *
 * Author: Max Asböck <amax@us.ibm.com> 
 *
 */

/* Remote mouse and keyboard event handling functions */

#include "ibmasm.h"
#include "remote.h"

int ibmasm_init_remote_queue(struct service_processor *sp)
{
	struct remote_queue *q = &sp->remote_queue;

	disable_mouse_interrupts(sp);

	q->open = 0;
	q->size = 0;

	q->start = kmalloc(DRIVER_REMOTE_QUEUE_SIZE * sizeof(struct remote_event), GFP_KERNEL);
        if (q->start == 0)
                return -ENOMEM;

	q->end = q->start + DRIVER_REMOTE_QUEUE_SIZE;
	q->reader = q->start;
	q->writer = q->start;
	q->size = DRIVER_REMOTE_QUEUE_SIZE;
	init_waitqueue_head(&q->wait);

	return 0;
}

void ibmasm_free_remote_queue(struct service_processor *sp)
{
	kfree(sp->remote_queue.start);
}

void ibmasm_advance_reader(struct remote_queue *q, unsigned int n)
{
	q->reader += n;
	if (q->reader >= q->end)
		q->reader -= q->size;
}

size_t ibmasm_events_available(struct remote_queue *q)
{
	ssize_t diff = q->writer - q->reader;
 
	return (diff >= 0) ? diff : q->end - q->reader;	
}
	

static int space_free(struct remote_queue *q)
{
	if (q->reader == q->writer)
		return q->size - 1;

	return ( (q->reader + q->size - q->writer) % q->size ) - 1;
}

static void set_mouse_event(struct remote_input *input, struct mouse_event *mouse)
{
	static char last_buttons = 0;

	mouse->x = input->data.mouse.x;
	mouse->y = input->data.mouse.y;

	if (input->mouse_buttons == REMOTE_MOUSE_DOUBLE_CLICK) {
		mouse->buttons = REMOTE_MOUSE_DOUBLE_CLICK;
		last_buttons = 0;
		return;
	}
	mouse->transitions = last_buttons ^ input->mouse_buttons;
	mouse->buttons = input->mouse_buttons;

	last_buttons = input->mouse_buttons;
}

static void set_keyboard_event(struct remote_input *input, struct keyboard_event *keyboard)
{
	keyboard->key_code = input->data.keyboard.key_code;
	keyboard->key_down = input->data.keyboard.key_down;
}

static int add_to_driver_queue(struct remote_queue *q, struct remote_input *input)
{
	struct remote_event *event = q->writer;

	if (space_free(q) < 1) {
		return 1;
	}

	switch(input->type) {
	case (INPUT_TYPE_MOUSE):
		event->type = INPUT_TYPE_MOUSE;
		set_mouse_event(input, &event->data.mouse);
		break;
	case (INPUT_TYPE_KEYBOARD):
		event->type = INPUT_TYPE_KEYBOARD;
		set_keyboard_event(input, &event->data.keyboard);
		break;
	default:
		return 0;
	}
	event->type = input->type;

	q->writer++;
	if (q->writer == q->end)
		q->writer = q->start;

	return 0;
}
	

void ibmasm_handle_mouse_interrupt(struct service_processor *sp)
{
	unsigned long reader;
	unsigned long writer;
	struct remote_input input;

	reader = get_queue_reader(sp);
	writer = get_queue_writer(sp);

	while (reader != writer) {
		memcpy(&input, (void *)get_queue_entry(sp, reader), sizeof(struct remote_input));

		if (add_to_driver_queue(&sp->remote_queue, &input))
			break;

		reader = advance_queue_reader(sp, reader);
	}
	wake_up_interruptible(&sp->remote_queue.wait);
}
OpenPOWER on IntegriCloud