libps2.c 7.3 KB
Newer Older
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
/*
 * PS/2 driver library
 *
 * Copyright (c) 1999-2002 Vojtech Pavlik
 * Copyright (c) 2004 Dmitry Torokhov
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

#include <linux/delay.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/serio.h>
#include <linux/init.h>
#include <linux/libps2.h>

#define DRIVER_DESC	"PS/2 driver library"

MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
MODULE_DESCRIPTION("PS/2 driver library");
MODULE_LICENSE("GPL");

EXPORT_SYMBOL(ps2_init);
EXPORT_SYMBOL(ps2_sendbyte);
EXPORT_SYMBOL(ps2_command);
EXPORT_SYMBOL(ps2_schedule_command);
EXPORT_SYMBOL(ps2_handle_ack);
EXPORT_SYMBOL(ps2_handle_response);
EXPORT_SYMBOL(ps2_cmd_aborted);

/* Work structure to schedule execution of a command */
struct ps2work {
	struct work_struct work;
	struct ps2dev *ps2dev;
	int command;
	unsigned char param[0];
};


/*
 * ps2_sendbyte() sends a byte to the mouse, and waits for acknowledge.
 * It doesn't handle retransmission, though it could - because when there would
 * be need for retransmissions, the mouse has to be replaced anyway.
 *
 * ps2_sendbyte() can only be called from a process context
 */

55
int ps2_sendbyte(struct ps2dev *ps2dev, unsigned char byte, int timeout)
56 57 58 59 60 61 62
{
	serio_pause_rx(ps2dev->serio);
	ps2dev->nak = 1;
	ps2dev->flags |= PS2_FLAG_ACK;
	serio_continue_rx(ps2dev->serio);

	if (serio_write(ps2dev->serio, byte) == 0)
63 64 65
		wait_event_timeout(ps2dev->wait,
				   !(ps2dev->flags & PS2_FLAG_ACK),
				   msecs_to_jiffies(timeout));
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

	serio_pause_rx(ps2dev->serio);
	ps2dev->flags &= ~PS2_FLAG_ACK;
	serio_continue_rx(ps2dev->serio);

	return -ps2dev->nak;
}

/*
 * ps2_command() sends a command and its parameters to the mouse,
 * then waits for the response and puts it in the param array.
 *
 * ps2_command() can only be called from a process context
 */

int ps2_command(struct ps2dev *ps2dev, unsigned char *param, int command)
{
	int timeout;
	int send = (command >> 12) & 0xf;
	int receive = (command >> 8) & 0xf;
	int rc = -1;
	int i;

	down(&ps2dev->cmd_sem);

	serio_pause_rx(ps2dev->serio);
	ps2dev->flags = command == PS2_CMD_GETID ? PS2_FLAG_WAITID : 0;
	ps2dev->cmdcnt = receive;
	if (receive && param)
		for (i = 0; i < receive; i++)
			ps2dev->cmdbuf[(receive - 1) - i] = param[i];
	serio_continue_rx(ps2dev->serio);

99 100 101 102 103
	/*
	 * Some devices (Synaptics) peform the reset before
	 * ACKing the reset command, and so it can take a long
	 * time before the ACK arrrives.
	 */
104
	if (command & 0xff)
105 106
		if (ps2_sendbyte(ps2dev, command & 0xff,
			command == PS2_CMD_RESET_BAT ? 1000 : 200))
107 108 109
			goto out;

	for (i = 0; i < send; i++)
110
		if (ps2_sendbyte(ps2dev, param[i], 200))
111 112
			goto out;

113 114 115 116 117
	/*
	 * The reset command takes a long time to execute.
	 */
	timeout = msecs_to_jiffies(command == PS2_CMD_RESET_BAT ? 4000 : 500);

118 119
	timeout = wait_event_timeout(ps2dev->wait,
				     !(ps2dev->flags & PS2_FLAG_CMD1), timeout);
120 121

	if (ps2dev->cmdcnt && timeout > 0) {
122 123 124 125 126 127 128 129

		if (command == PS2_CMD_RESET_BAT && timeout > msecs_to_jiffies(100)) {
			/*
			 * Device has sent the first response byte
			 * after a reset command, reset is thus done,
			 * shorten the timeout. The next byte will come
			 * soon (keyboard) or not at all (mouse).
			 */
130
			timeout = msecs_to_jiffies(100);
131
		}
132 133

		if (command == PS2_CMD_GETID &&
134 135 136 137 138 139
		    ps2dev->cmdbuf[receive - 1] != 0xab && /* Regular keyboards */
		    ps2dev->cmdbuf[receive - 1] != 0xac && /* NCD Sun keyboard */
		    ps2dev->cmdbuf[receive - 1] != 0x2b && /* Trust keyboard, translated */
		    ps2dev->cmdbuf[receive - 1] != 0x5d && /* Trust keyboard */
		    ps2dev->cmdbuf[receive - 1] != 0x60 && /* NMB SGI keyboard, translated */
		    ps2dev->cmdbuf[receive - 1] != 0x47) { /* NMB SGI keyboard */
140 141 142 143 144 145 146 147 148 149
			/*
			 * Device behind the port is not a keyboard
			 * so we don't need to wait for the 2nd byte
			 * of ID response.
			 */
			serio_pause_rx(ps2dev->serio);
			ps2dev->flags = ps2dev->cmdcnt = 0;
			serio_continue_rx(ps2dev->serio);
		}

150 151
		wait_event_timeout(ps2dev->wait,
				   !(ps2dev->flags & PS2_FLAG_CMD), timeout);
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
	}

	if (param)
		for (i = 0; i < receive; i++)
			param[i] = ps2dev->cmdbuf[(receive - 1) - i];

	if (ps2dev->cmdcnt && (command != PS2_CMD_RESET_BAT || ps2dev->cmdcnt != 1))
		goto out;

	rc = 0;

out:
	serio_pause_rx(ps2dev->serio);
	ps2dev->flags = 0;
	serio_continue_rx(ps2dev->serio);

	up(&ps2dev->cmd_sem);
	return rc;
}

/*
 * ps2_execute_scheduled_command() sends a command, previously scheduled by
 * ps2_schedule_command(), to a PS/2 device (keyboard, mouse, etc.)
 */

static void ps2_execute_scheduled_command(void *data)
{
	struct ps2work *ps2work = data;

	ps2_command(ps2work->ps2dev, ps2work->param, ps2work->command);
	kfree(ps2work);
}

/*
 * ps2_schedule_command() allows to schedule delayed execution of a PS/2
 * command and can be used to issue a command from an interrupt or softirq
 * context.
 */

int ps2_schedule_command(struct ps2dev *ps2dev, unsigned char *param, int command)
{
	struct ps2work *ps2work;
	int send = (command >> 12) & 0xf;
	int receive = (command >> 8) & 0xf;

	if (!(ps2work = kmalloc(sizeof(struct ps2work) + max(send, receive), GFP_ATOMIC)))
		return -1;

	memset(ps2work, 0, sizeof(struct ps2work));
	ps2work->ps2dev = ps2dev;
	ps2work->command = command;
	memcpy(ps2work->param, param, send);
	INIT_WORK(&ps2work->work, ps2_execute_scheduled_command, ps2work);

	if (!schedule_work(&ps2work->work)) {
		kfree(ps2work);
		return -1;
	}

	return 0;
}

/*
 * ps2_init() initializes ps2dev structure
 */

void ps2_init(struct ps2dev *ps2dev, struct serio *serio)
{
	init_MUTEX(&ps2dev->cmd_sem);
	init_waitqueue_head(&ps2dev->wait);
	ps2dev->serio = serio;
}

/*
226 227
 * ps2_handle_ack() is supposed to be used in interrupt handler
 * to properly process ACK/NAK of a command from a PS/2 device.
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
 */

int ps2_handle_ack(struct ps2dev *ps2dev, unsigned char data)
{
	switch (data) {
		case PS2_RET_ACK:
			ps2dev->nak = 0;
			break;

		case PS2_RET_NAK:
			ps2dev->nak = 1;
			break;

		/*
		 * Workaround for mice which don't ACK the Get ID command.
		 * These are valid mouse IDs that we recognize.
		 */
		case 0x00:
		case 0x03:
		case 0x04:
			if (ps2dev->flags & PS2_FLAG_WAITID) {
				ps2dev->nak = 0;
				break;
			}
			/* Fall through */
		default:
254
			return 0;
255 256
	}

257

258 259 260 261
	if (!ps2dev->nak && ps2dev->cmdcnt)
		ps2dev->flags |= PS2_FLAG_CMD | PS2_FLAG_CMD1;

	ps2dev->flags &= ~PS2_FLAG_ACK;
262
	wake_up(&ps2dev->wait);
263

264 265 266 267
	if (data != PS2_RET_ACK)
		ps2_handle_response(ps2dev, data);

	return 1;
268 269
}

270 271 272 273 274
/*
 * ps2_handle_response() is supposed to be used in interrupt handler
 * to properly store device's response to a command and notify process
 * waiting for completion of the command.
 */
275 276 277 278 279 280 281 282 283

int ps2_handle_response(struct ps2dev *ps2dev, unsigned char data)
{
	if (ps2dev->cmdcnt)
		ps2dev->cmdbuf[--ps2dev->cmdcnt] = data;

	if (ps2dev->flags & PS2_FLAG_CMD1) {
		ps2dev->flags &= ~PS2_FLAG_CMD1;
		if (ps2dev->cmdcnt)
284
			wake_up(&ps2dev->wait);
285 286 287 288
	}

	if (!ps2dev->cmdcnt) {
		ps2dev->flags &= ~PS2_FLAG_CMD;
289
		wake_up(&ps2dev->wait);
290 291 292 293 294 295 296 297 298 299 300
	}

	return 1;
}

void ps2_cmd_aborted(struct ps2dev *ps2dev)
{
	if (ps2dev->flags & PS2_FLAG_ACK)
		ps2dev->nak = 1;

	if (ps2dev->flags & (PS2_FLAG_ACK | PS2_FLAG_CMD))
301
		wake_up(&ps2dev->wait);
302 303 304 305

	ps2dev->flags = 0;
}