serio_raw.c 9.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * Raw serio device providing access to a raw byte stream from underlying
 * serio port. Closely emulates behavior of pre-2.6 /dev/psaux device
 *
 * 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.
 */

12
#include <linux/kref.h>
13
#include <linux/sched.h>
L
Linus Torvalds 已提交
14 15 16 17 18 19 20 21 22
#include <linux/slab.h>
#include <linux/poll.h>
#include <linux/module.h>
#include <linux/serio.h>
#include <linux/init.h>
#include <linux/major.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <linux/wait.h>
23
#include <linux/mutex.h>
L
Linus Torvalds 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36

#define DRIVER_DESC	"Raw serio driver"

MODULE_AUTHOR("Dmitry Torokhov <dtor@mail.ru>");
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");

#define SERIO_RAW_QUEUE_LEN	64
struct serio_raw {
	unsigned char queue[SERIO_RAW_QUEUE_LEN];
	unsigned int tail, head;

	char name[16];
37
	struct kref kref;
L
Linus Torvalds 已提交
38 39 40
	struct serio *serio;
	struct miscdevice dev;
	wait_queue_head_t wait;
41
	struct list_head client_list;
L
Linus Torvalds 已提交
42
	struct list_head node;
43
	bool dead;
L
Linus Torvalds 已提交
44 45
};

46
struct serio_raw_client {
L
Linus Torvalds 已提交
47 48 49 50 51
	struct fasync_struct *fasync;
	struct serio_raw *serio_raw;
	struct list_head node;
};

52
static DEFINE_MUTEX(serio_raw_mutex);
L
Linus Torvalds 已提交
53 54 55 56 57 58 59 60
static LIST_HEAD(serio_raw_list);

/*********************************************************************
 *             Interface with userspace (file operations)            *
 *********************************************************************/

static int serio_raw_fasync(int fd, struct file *file, int on)
{
61
	struct serio_raw_client *client = file->private_data;
L
Linus Torvalds 已提交
62

63
	return fasync_helper(fd, file, on, &client->fasync);
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
}

static struct serio_raw *serio_raw_locate(int minor)
{
	struct serio_raw *serio_raw;

	list_for_each_entry(serio_raw, &serio_raw_list, node) {
		if (serio_raw->dev.minor == minor)
			return serio_raw;
	}

	return NULL;
}

static int serio_raw_open(struct inode *inode, struct file *file)
{
	struct serio_raw *serio_raw;
81 82
	struct serio_raw_client *client;
	int retval;
L
Linus Torvalds 已提交
83

84
	retval = mutex_lock_interruptible(&serio_raw_mutex);
L
Linus Torvalds 已提交
85
	if (retval)
86
		return retval;
L
Linus Torvalds 已提交
87

88 89
	serio_raw = serio_raw_locate(iminor(inode));
	if (!serio_raw) {
L
Linus Torvalds 已提交
90 91 92 93
		retval = -ENODEV;
		goto out;
	}

94
	if (serio_raw->dead) {
L
Linus Torvalds 已提交
95 96 97 98
		retval = -ENODEV;
		goto out;
	}

99 100
	client = kzalloc(sizeof(struct serio_raw_client), GFP_KERNEL);
	if (!client) {
L
Linus Torvalds 已提交
101 102 103 104
		retval = -ENOMEM;
		goto out;
	}

105 106
	client->serio_raw = serio_raw;
	file->private_data = client;
L
Linus Torvalds 已提交
107

108
	kref_get(&serio_raw->kref);
109 110

	serio_pause_rx(serio_raw->serio);
111
	list_add_tail(&client->node, &serio_raw->client_list);
112
	serio_continue_rx(serio_raw->serio);
L
Linus Torvalds 已提交
113 114

out:
115
	mutex_unlock(&serio_raw_mutex);
L
Linus Torvalds 已提交
116 117 118
	return retval;
}

119
static void serio_raw_free(struct kref *kref)
L
Linus Torvalds 已提交
120
{
121 122
	struct serio_raw *serio_raw =
			container_of(kref, struct serio_raw, kref);
L
Linus Torvalds 已提交
123

124
	put_device(&serio_raw->serio->dev);
125
	kfree(serio_raw);
L
Linus Torvalds 已提交
126 127 128 129
}

static int serio_raw_release(struct inode *inode, struct file *file)
{
130 131
	struct serio_raw_client *client = file->private_data;
	struct serio_raw *serio_raw = client->serio_raw;
L
Linus Torvalds 已提交
132

133 134 135
	serio_pause_rx(serio_raw->serio);
	list_del(&client->node);
	serio_continue_rx(serio_raw->serio);
L
Linus Torvalds 已提交
136

137 138 139
	kfree(client);

	kref_put(&serio_raw->kref, serio_raw_free);
L
Linus Torvalds 已提交
140 141 142 143

	return 0;
}

144
static bool serio_raw_fetch_byte(struct serio_raw *serio_raw, char *c)
L
Linus Torvalds 已提交
145
{
146
	bool empty;
L
Linus Torvalds 已提交
147

148
	serio_pause_rx(serio_raw->serio);
L
Linus Torvalds 已提交
149 150 151 152 153 154 155

	empty = serio_raw->head == serio_raw->tail;
	if (!empty) {
		*c = serio_raw->queue[serio_raw->tail];
		serio_raw->tail = (serio_raw->tail + 1) % SERIO_RAW_QUEUE_LEN;
	}

156
	serio_continue_rx(serio_raw->serio);
L
Linus Torvalds 已提交
157 158 159 160

	return !empty;
}

161 162
static ssize_t serio_raw_read(struct file *file, char __user *buffer,
			      size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
163
{
164 165
	struct serio_raw_client *client = file->private_data;
	struct serio_raw *serio_raw = client->serio_raw;
166
	char uninitialized_var(c);
167 168
	ssize_t read = 0;
	int retval;
L
Linus Torvalds 已提交
169

170
	if (serio_raw->dead)
L
Linus Torvalds 已提交
171 172 173 174 175
		return -ENODEV;

	if (serio_raw->head == serio_raw->tail && (file->f_flags & O_NONBLOCK))
		return -EAGAIN;

176
	retval = wait_event_interruptible(serio_raw->wait,
177
			serio_raw->head != serio_raw->tail || serio_raw->dead);
L
Linus Torvalds 已提交
178 179 180
	if (retval)
		return retval;

181
	if (serio_raw->dead)
L
Linus Torvalds 已提交
182 183
		return -ENODEV;

184 185 186 187 188 189
	while (read < count && serio_raw_fetch_byte(serio_raw, &c)) {
		if (put_user(c, buffer++)) {
			retval = -EFAULT;
			break;
		}
		read++;
L
Linus Torvalds 已提交
190 191
	}

192
	return read ?: retval;
L
Linus Torvalds 已提交
193 194
}

195 196
static ssize_t serio_raw_write(struct file *file, const char __user *buffer,
			       size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
197
{
198 199
	struct serio_raw_client *client = file->private_data;
	struct serio_raw *serio_raw = client->serio_raw;
L
Linus Torvalds 已提交
200 201 202 203
	ssize_t written = 0;
	int retval;
	unsigned char c;

204
	retval = mutex_lock_interruptible(&serio_raw_mutex);
L
Linus Torvalds 已提交
205 206 207
	if (retval)
		return retval;

208
	if (serio_raw->dead) {
L
Linus Torvalds 已提交
209 210 211 212 213 214 215 216 217 218 219 220
		retval = -ENODEV;
		goto out;
	}

	if (count > 32)
		count = 32;

	while (count--) {
		if (get_user(c, buffer++)) {
			retval = -EFAULT;
			goto out;
		}
221
		if (serio_write(serio_raw->serio, c)) {
L
Linus Torvalds 已提交
222 223 224 225
			retval = -EIO;
			goto out;
		}
		written++;
226
	}
L
Linus Torvalds 已提交
227 228

out:
229
	mutex_unlock(&serio_raw_mutex);
230
	return written ?: retval;
L
Linus Torvalds 已提交
231 232 233 234
}

static unsigned int serio_raw_poll(struct file *file, poll_table *wait)
{
235 236
	struct serio_raw_client *client = file->private_data;
	struct serio_raw *serio_raw = client->serio_raw;
237
	unsigned int mask;
L
Linus Torvalds 已提交
238

239
	poll_wait(file, &serio_raw->wait, wait);
L
Linus Torvalds 已提交
240

241
	mask = serio_raw->dead ? POLLHUP | POLLERR : POLLOUT | POLLWRNORM;
242
	if (serio_raw->head != serio_raw->tail)
243
		mask |= POLLIN | POLLRDNORM;
L
Linus Torvalds 已提交
244

245
	return mask;
L
Linus Torvalds 已提交
246 247
}

248
static const struct file_operations serio_raw_fops = {
249 250 251 252 253 254 255 256
	.owner		= THIS_MODULE,
	.open		= serio_raw_open,
	.release	= serio_raw_release,
	.read		= serio_raw_read,
	.write		= serio_raw_write,
	.poll		= serio_raw_poll,
	.fasync		= serio_raw_fasync,
	.llseek		= noop_llseek,
L
Linus Torvalds 已提交
257 258 259 260
};


/*********************************************************************
261
 *                   Interface with serio port                       *
L
Linus Torvalds 已提交
262 263 264
 *********************************************************************/

static irqreturn_t serio_raw_interrupt(struct serio *serio, unsigned char data,
265
					unsigned int dfl)
L
Linus Torvalds 已提交
266 267
{
	struct serio_raw *serio_raw = serio_get_drvdata(serio);
268
	struct serio_raw_client *client;
L
Linus Torvalds 已提交
269 270
	unsigned int head = serio_raw->head;

271
	/* we are holding serio->lock here so we are protected */
L
Linus Torvalds 已提交
272 273 274 275
	serio_raw->queue[head] = data;
	head = (head + 1) % SERIO_RAW_QUEUE_LEN;
	if (likely(head != serio_raw->tail)) {
		serio_raw->head = head;
276 277
		list_for_each_entry(client, &serio_raw->client_list, node)
			kill_fasync(&client->fasync, SIGIO, POLL_IN);
L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285
		wake_up_interruptible(&serio_raw->wait);
	}

	return IRQ_HANDLED;
}

static int serio_raw_connect(struct serio *serio, struct serio_driver *drv)
{
286
	static atomic_t serio_raw_no = ATOMIC_INIT(0);
L
Linus Torvalds 已提交
287 288 289
	struct serio_raw *serio_raw;
	int err;

290 291
	serio_raw = kzalloc(sizeof(struct serio_raw), GFP_KERNEL);
	if (!serio_raw) {
292
		dev_dbg(&serio->dev, "can't allocate memory for a device\n");
L
Linus Torvalds 已提交
293 294 295
		return -ENOMEM;
	}

296
	snprintf(serio_raw->name, sizeof(serio_raw->name),
297
		 "serio_raw%ld", (long)atomic_inc_return(&serio_raw_no) - 1);
298
	kref_init(&serio_raw->kref);
299
	INIT_LIST_HEAD(&serio_raw->client_list);
L
Linus Torvalds 已提交
300 301
	init_waitqueue_head(&serio_raw->wait);

302 303 304
	serio_raw->serio = serio;
	get_device(&serio->dev);

L
Linus Torvalds 已提交
305 306 307 308
	serio_set_drvdata(serio, serio_raw);

	err = serio_open(serio, drv);
	if (err)
309 310 311 312 313
		goto err_free;

	err = mutex_lock_killable(&serio_raw_mutex);
	if (err)
		goto err_close;
L
Linus Torvalds 已提交
314 315

	list_add_tail(&serio_raw->node, &serio_raw_list);
316
	mutex_unlock(&serio_raw_mutex);
L
Linus Torvalds 已提交
317 318 319

	serio_raw->dev.minor = PSMOUSE_MINOR;
	serio_raw->dev.name = serio_raw->name;
320
	serio_raw->dev.parent = &serio->dev;
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329
	serio_raw->dev.fops = &serio_raw_fops;

	err = misc_register(&serio_raw->dev);
	if (err) {
		serio_raw->dev.minor = MISC_DYNAMIC_MINOR;
		err = misc_register(&serio_raw->dev);
	}

	if (err) {
330 331
		dev_err(&serio->dev,
			"failed to register raw access device for %s\n",
L
Linus Torvalds 已提交
332
			serio->phys);
333
		goto err_unlink;
L
Linus Torvalds 已提交
334 335
	}

336 337
	dev_info(&serio->dev, "raw access enabled on %s (%s, minor %d)\n",
		 serio->phys, serio_raw->name, serio_raw->dev.minor);
338
	return 0;
L
Linus Torvalds 已提交
339

340
err_unlink:
L
Linus Torvalds 已提交
341
	list_del_init(&serio_raw->node);
342 343 344
err_close:
	serio_close(serio);
err_free:
L
Linus Torvalds 已提交
345
	serio_set_drvdata(serio, NULL);
346
	kref_put(&serio_raw->kref, serio_raw_free);
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355
	return err;
}

static int serio_raw_reconnect(struct serio *serio)
{
	struct serio_raw *serio_raw = serio_get_drvdata(serio);
	struct serio_driver *drv = serio->drv;

	if (!drv || !serio_raw) {
356 357
		dev_dbg(&serio->dev,
			"reconnect request, but serio is disconnected, ignoring...\n");
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367
		return -1;
	}

	/*
	 * Nothing needs to be done here, we just need this method to
	 * keep the same device.
	 */
	return 0;
}

368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
/*
 * Wake up users waiting for IO so they can disconnect from
 * dead device.
 */
static void serio_raw_hangup(struct serio_raw *serio_raw)
{
	struct serio_raw_client *client;

	serio_pause_rx(serio_raw->serio);
	list_for_each_entry(client, &serio_raw->client_list, node)
		kill_fasync(&client->fasync, SIGIO, POLL_HUP);
	serio_continue_rx(serio_raw->serio);

	wake_up_interruptible(&serio_raw->wait);
}


L
Linus Torvalds 已提交
385 386
static void serio_raw_disconnect(struct serio *serio)
{
387
	struct serio_raw *serio_raw = serio_get_drvdata(serio);
L
Linus Torvalds 已提交
388

389
	misc_deregister(&serio_raw->dev);
L
Linus Torvalds 已提交
390

391
	mutex_lock(&serio_raw_mutex);
392
	serio_raw->dead = true;
393 394 395
	list_del_init(&serio_raw->node);
	mutex_unlock(&serio_raw_mutex);

396
	serio_raw_hangup(serio_raw);
L
Linus Torvalds 已提交
397

398 399
	serio_close(serio);
	kref_put(&serio_raw->kref, serio_raw_free);
400 401

	serio_set_drvdata(serio, NULL);
L
Linus Torvalds 已提交
402 403 404 405 406 407 408 409 410
}

static struct serio_device_id serio_raw_serio_ids[] = {
	{
		.type	= SERIO_8042,
		.proto	= SERIO_ANY,
		.id	= SERIO_ANY,
		.extra	= SERIO_ANY,
	},
411 412 413 414 415 416
	{
		.type	= SERIO_8042_XL,
		.proto	= SERIO_ANY,
		.id	= SERIO_ANY,
		.extra	= SERIO_ANY,
	},
L
Linus Torvalds 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	{ 0 }
};

MODULE_DEVICE_TABLE(serio, serio_raw_serio_ids);

static struct serio_driver serio_raw_drv = {
	.driver		= {
		.name	= "serio_raw",
	},
	.description	= DRIVER_DESC,
	.id_table	= serio_raw_serio_ids,
	.interrupt	= serio_raw_interrupt,
	.connect	= serio_raw_connect,
	.reconnect	= serio_raw_reconnect,
	.disconnect	= serio_raw_disconnect,
432
	.manual_bind	= true,
L
Linus Torvalds 已提交
433 434
};

435
module_serio_driver(serio_raw_drv);