joydev.c 20.8 KB
Newer Older
L
Linus Torvalds 已提交
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
/*
 * Joystick device driver for the input driver suite.
 *
 * Copyright (c) 1999-2002 Vojtech Pavlik
 * Copyright (c) 1999 Colin Van Dyke
 *
 * 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.
 */

#include <asm/io.h>
#include <asm/system.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/joystick.h>
#include <linux/input.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/poll.h>
#include <linux/init.h>
#include <linux/device.h>

MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
MODULE_DESCRIPTION("Joystick device interfaces");
MODULE_SUPPORTED_DEVICE("input/js");
MODULE_LICENSE("GPL");

#define JOYDEV_MINOR_BASE	0
#define JOYDEV_MINORS		16
#define JOYDEV_BUFFER_SIZE	64

struct joydev {
	int exist;
	int open;
	int minor;
	char name[16];
	struct input_handle handle;
	wait_queue_head_t wait;
45
	struct list_head client_list;
46 47
	spinlock_t client_lock; /* protects client_list */
	struct mutex mutex;
48 49
	struct device dev;

L
Linus Torvalds 已提交
50 51 52 53 54 55 56 57 58 59 60
	struct js_corr corr[ABS_MAX + 1];
	struct JS_DATA_SAVE_TYPE glue;
	int nabs;
	int nkey;
	__u16 keymap[KEY_MAX - BTN_MISC + 1];
	__u16 keypam[KEY_MAX - BTN_MISC + 1];
	__u8 absmap[ABS_MAX + 1];
	__u8 abspam[ABS_MAX + 1];
	__s16 abs[ABS_MAX + 1];
};

61
struct joydev_client {
L
Linus Torvalds 已提交
62 63 64 65
	struct js_event buffer[JOYDEV_BUFFER_SIZE];
	int head;
	int tail;
	int startup;
66
	spinlock_t buffer_lock; /* protects access to buffer, head and tail */
L
Linus Torvalds 已提交
67 68 69 70 71 72
	struct fasync_struct *fasync;
	struct joydev *joydev;
	struct list_head node;
};

static struct joydev *joydev_table[JOYDEV_MINORS];
73
static DEFINE_MUTEX(joydev_table_mutex);
L
Linus Torvalds 已提交
74 75 76 77

static int joydev_correct(int value, struct js_corr *corr)
{
	switch (corr->type) {
78 79 80 81 82 83 84 85 86 87 88 89

	case JS_CORR_NONE:
		break;

	case JS_CORR_BROKEN:
		value = value > corr->coef[0] ? (value < corr->coef[1] ? 0 :
			((corr->coef[3] * (value - corr->coef[1])) >> 14)) :
			((corr->coef[2] * (value - corr->coef[0])) >> 14);
		break;

	default:
		return 0;
L
Linus Torvalds 已提交
90 91
	}

92
	return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
L
Linus Torvalds 已提交
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
static void joydev_pass_event(struct joydev_client *client,
			      struct js_event *event)
{
	struct joydev *joydev = client->joydev;

	/*
	 * IRQs already disabled, just acquire the lock
	 */
	spin_lock(&client->buffer_lock);

	client->buffer[client->head] = *event;

	if (client->startup == joydev->nabs + joydev->nkey) {
		client->head++;
		client->head &= JOYDEV_BUFFER_SIZE - 1;
		if (client->tail == client->head)
			client->startup = 0;
	}

	spin_unlock(&client->buffer_lock);

	kill_fasync(&client->fasync, SIGIO, POLL_IN);
}

static void joydev_event(struct input_handle *handle,
			 unsigned int type, unsigned int code, int value)
L
Linus Torvalds 已提交
121 122
{
	struct joydev *joydev = handle->private;
123
	struct joydev_client *client;
L
Linus Torvalds 已提交
124 125 126 127
	struct js_event event;

	switch (type) {

128 129 130 131 132 133 134
	case EV_KEY:
		if (code < BTN_MISC || value == 2)
			return;
		event.type = JS_EVENT_BUTTON;
		event.number = joydev->keymap[code - BTN_MISC];
		event.value = value;
		break;
L
Linus Torvalds 已提交
135

136 137 138 139 140 141
	case EV_ABS:
		event.type = JS_EVENT_AXIS;
		event.number = joydev->absmap[code];
		event.value = joydev_correct(value,
					&joydev->corr[event.number]);
		if (event.value == joydev->abs[event.number])
L
Linus Torvalds 已提交
142
			return;
143 144 145 146 147
		joydev->abs[event.number] = event.value;
		break;

	default:
		return;
L
Linus Torvalds 已提交
148 149
	}

150
	event.time = jiffies_to_msecs(jiffies);
L
Linus Torvalds 已提交
151

D
Dmitry Torokhov 已提交
152
	rcu_read_lock();
153 154
	list_for_each_entry_rcu(client, &joydev->client_list, node)
		joydev_pass_event(client, &event);
D
Dmitry Torokhov 已提交
155
	rcu_read_unlock();
L
Linus Torvalds 已提交
156 157 158 159 160 161 162

	wake_up_interruptible(&joydev->wait);
}

static int joydev_fasync(int fd, struct file *file, int on)
{
	int retval;
163
	struct joydev_client *client = file->private_data;
164

165
	retval = fasync_helper(fd, file, on, &client->fasync);
166

L
Linus Torvalds 已提交
167 168 169
	return retval < 0 ? retval : 0;
}

170
static void joydev_free(struct device *dev)
L
Linus Torvalds 已提交
171
{
172 173
	struct joydev *joydev = container_of(dev, struct joydev, dev);

L
Linus Torvalds 已提交
174 175 176
	kfree(joydev);
}

177 178 179 180 181 182
static void joydev_attach_client(struct joydev *joydev,
				 struct joydev_client *client)
{
	spin_lock(&joydev->client_lock);
	list_add_tail_rcu(&client->node, &joydev->client_list);
	spin_unlock(&joydev->client_lock);
D
Dmitry Torokhov 已提交
183
	synchronize_rcu();
184 185 186 187 188 189 190 191
}

static void joydev_detach_client(struct joydev *joydev,
				 struct joydev_client *client)
{
	spin_lock(&joydev->client_lock);
	list_del_rcu(&client->node);
	spin_unlock(&joydev->client_lock);
D
Dmitry Torokhov 已提交
192
	synchronize_rcu();
193 194 195 196 197 198 199 200 201 202 203 204
}

static int joydev_open_device(struct joydev *joydev)
{
	int retval;

	retval = mutex_lock_interruptible(&joydev->mutex);
	if (retval)
		return retval;

	if (!joydev->exist)
		retval = -ENODEV;
205
	else if (!joydev->open++) {
206
		retval = input_open_device(&joydev->handle);
207 208 209
		if (retval)
			joydev->open--;
	}
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

	mutex_unlock(&joydev->mutex);
	return retval;
}

static void joydev_close_device(struct joydev *joydev)
{
	mutex_lock(&joydev->mutex);

	if (joydev->exist && !--joydev->open)
		input_close_device(&joydev->handle);

	mutex_unlock(&joydev->mutex);
}

/*
 * Wake up users waiting for IO so they can disconnect from
 * dead device.
 */
static void joydev_hangup(struct joydev *joydev)
{
	struct joydev_client *client;

	spin_lock(&joydev->client_lock);
	list_for_each_entry(client, &joydev->client_list, node)
		kill_fasync(&client->fasync, SIGIO, POLL_HUP);
	spin_unlock(&joydev->client_lock);

	wake_up_interruptible(&joydev->wait);
}

241
static int joydev_release(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
242
{
243 244
	struct joydev_client *client = file->private_data;
	struct joydev *joydev = client->joydev;
L
Linus Torvalds 已提交
245 246

	joydev_fasync(-1, file, 0);
247
	joydev_detach_client(joydev, client);
248
	kfree(client);
L
Linus Torvalds 已提交
249

250
	joydev_close_device(joydev);
251
	put_device(&joydev->dev);
L
Linus Torvalds 已提交
252 253 254 255 256 257

	return 0;
}

static int joydev_open(struct inode *inode, struct file *file)
{
258 259
	struct joydev_client *client;
	struct joydev *joydev;
L
Linus Torvalds 已提交
260
	int i = iminor(inode) - JOYDEV_MINOR_BASE;
261
	int error;
L
Linus Torvalds 已提交
262

263 264
	if (i >= JOYDEV_MINORS)
		return -ENODEV;
L
Linus Torvalds 已提交
265

266 267 268
	error = mutex_lock_interruptible(&joydev_table_mutex);
	if (error)
		return error;
269
	joydev = joydev_table[i];
270 271 272
	if (joydev)
		get_device(&joydev->dev);
	mutex_unlock(&joydev_table_mutex);
L
Linus Torvalds 已提交
273

274 275
	if (!joydev)
		return -ENODEV;
276

277
	client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
278 279 280 281
	if (!client) {
		error = -ENOMEM;
		goto err_put_joydev;
	}
L
Linus Torvalds 已提交
282

283
	spin_lock_init(&client->buffer_lock);
284
	client->joydev = joydev;
285
	joydev_attach_client(joydev, client);
L
Linus Torvalds 已提交
286

287 288 289
	error = joydev_open_device(joydev);
	if (error)
		goto err_free_client;
L
Linus Torvalds 已提交
290

291
	file->private_data = client;
L
Linus Torvalds 已提交
292
	return 0;
293 294

 err_free_client:
295
	joydev_detach_client(joydev, client);
296 297 298 299
	kfree(client);
 err_put_joydev:
	put_device(&joydev->dev);
	return error;
L
Linus Torvalds 已提交
300 301
}

302 303 304
static int joydev_generate_startup_event(struct joydev_client *client,
					 struct input_dev *input,
					 struct js_event *event)
L
Linus Torvalds 已提交
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
	struct joydev *joydev = client->joydev;
	int have_event;

	spin_lock_irq(&client->buffer_lock);

	have_event = client->startup < joydev->nabs + joydev->nkey;

	if (have_event) {

		event->time = jiffies_to_msecs(jiffies);
		if (client->startup < joydev->nkey) {
			event->type = JS_EVENT_BUTTON | JS_EVENT_INIT;
			event->number = client->startup;
			event->value = !!test_bit(joydev->keypam[event->number],
						  input->key);
		} else {
			event->type = JS_EVENT_AXIS | JS_EVENT_INIT;
			event->number = client->startup - joydev->nkey;
			event->value = joydev->abs[event->number];
		}
		client->startup++;
	}

	spin_unlock_irq(&client->buffer_lock);

	return have_event;
}

static int joydev_fetch_next_event(struct joydev_client *client,
				   struct js_event *event)
{
	int have_event;

	spin_lock_irq(&client->buffer_lock);

	have_event = client->head != client->tail;
	if (have_event) {
		*event = client->buffer[client->tail++];
		client->tail &= JOYDEV_BUFFER_SIZE - 1;
	}

	spin_unlock_irq(&client->buffer_lock);

	return have_event;
}

/*
 * Old joystick interface
 */
static ssize_t joydev_0x_read(struct joydev_client *client,
			      struct input_dev *input,
			      char __user *buf)
{
	struct joydev *joydev = client->joydev;
	struct JS_DATA_TYPE data;
	int i;

	spin_lock_irq(&input->event_lock);

	/*
	 * Get device state
	 */
	for (data.buttons = i = 0; i < 32 && i < joydev->nkey; i++)
		data.buttons |=
			test_bit(joydev->keypam[i], input->key) ? (1 << i) : 0;
	data.x = (joydev->abs[0] / 256 + 128) >> joydev->glue.JS_CORR.x;
	data.y = (joydev->abs[1] / 256 + 128) >> joydev->glue.JS_CORR.y;

	/*
	 * Reset reader's event queue
	 */
	spin_lock(&client->buffer_lock);
	client->startup = 0;
	client->tail = client->head;
	spin_unlock(&client->buffer_lock);

	spin_unlock_irq(&input->event_lock);

	if (copy_to_user(buf, &data, sizeof(struct JS_DATA_TYPE)))
		return -EFAULT;

	return sizeof(struct JS_DATA_TYPE);
L
Linus Torvalds 已提交
388 389
}

390 391 392 393 394 395 396 397 398 399
static inline int joydev_data_pending(struct joydev_client *client)
{
	struct joydev *joydev = client->joydev;

	return client->startup < joydev->nabs + joydev->nkey ||
		client->head != client->tail;
}

static ssize_t joydev_read(struct file *file, char __user *buf,
			   size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
400
{
401 402
	struct joydev_client *client = file->private_data;
	struct joydev *joydev = client->joydev;
L
Linus Torvalds 已提交
403
	struct input_dev *input = joydev->handle.dev;
404 405
	struct js_event event;
	int retval;
L
Linus Torvalds 已提交
406

407
	if (!joydev->exist)
L
Linus Torvalds 已提交
408 409 410 411 412
		return -ENODEV;

	if (count < sizeof(struct js_event))
		return -EINVAL;

413 414
	if (count == sizeof(struct JS_DATA_TYPE))
		return joydev_0x_read(client, input, buf);
L
Linus Torvalds 已提交
415

416
	if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
417
		return -EAGAIN;
L
Linus Torvalds 已提交
418

419
	retval = wait_event_interruptible(joydev->wait,
420
			!joydev->exist || joydev_data_pending(client));
L
Linus Torvalds 已提交
421 422 423
	if (retval)
		return retval;

424
	if (!joydev->exist)
L
Linus Torvalds 已提交
425 426
		return -ENODEV;

427 428
	while (retval + sizeof(struct js_event) <= count &&
	       joydev_generate_startup_event(client, input, &event)) {
L
Linus Torvalds 已提交
429 430 431 432 433 434 435

		if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
			return -EFAULT;

		retval += sizeof(struct js_event);
	}

436 437
	while (retval + sizeof(struct js_event) <= count &&
	       joydev_fetch_next_event(client, &event)) {
L
Linus Torvalds 已提交
438

439
		if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
L
Linus Torvalds 已提交
440 441 442 443 444 445 446 447 448 449 450
			return -EFAULT;

		retval += sizeof(struct js_event);
	}

	return retval;
}

/* No kernel lock - fine */
static unsigned int joydev_poll(struct file *file, poll_table *wait)
{
451 452
	struct joydev_client *client = file->private_data;
	struct joydev *joydev = client->joydev;
453

454
	poll_wait(file, &joydev->wait, wait);
455 456
	return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
		(joydev->exist ?  0 : (POLLHUP | POLLERR));
L
Linus Torvalds 已提交
457 458
}

459 460
static int joydev_ioctl_common(struct joydev *joydev,
				unsigned int cmd, void __user *argp)
L
Linus Torvalds 已提交
461 462 463 464 465 466
{
	struct input_dev *dev = joydev->handle.dev;
	int i, j;

	switch (cmd) {

467 468
	case JS_SET_CAL:
		return copy_from_user(&joydev->glue.JS_CORR, argp,
469
				sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
470

471 472
	case JS_GET_CAL:
		return copy_to_user(argp, &joydev->glue.JS_CORR,
473
				sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
474

475 476
	case JS_SET_TIMEOUT:
		return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
477

478 479
	case JS_GET_TIMEOUT:
		return put_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
L
Linus Torvalds 已提交
480

481 482
	case JSIOCGVERSION:
		return put_user(JS_VERSION, (__u32 __user *) argp);
483

484 485
	case JSIOCGAXES:
		return put_user(joydev->nabs, (__u8 __user *) argp);
486

487 488
	case JSIOCGBUTTONS:
		return put_user(joydev->nkey, (__u8 __user *) argp);
489

490 491 492 493
	case JSIOCSCORR:
		if (copy_from_user(joydev->corr, argp,
			      sizeof(joydev->corr[0]) * joydev->nabs))
		    return -EFAULT;
494

495 496 497 498 499 500
		for (i = 0; i < joydev->nabs; i++) {
			j = joydev->abspam[i];
			joydev->abs[i] = joydev_correct(dev->abs[j],
							&joydev->corr[i]);
		}
		return 0;
501

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
	case JSIOCGCORR:
		return copy_to_user(argp, joydev->corr,
			sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;

	case JSIOCSAXMAP:
		if (copy_from_user(joydev->abspam, argp,
				   sizeof(__u8) * (ABS_MAX + 1)))
			return -EFAULT;

		for (i = 0; i < joydev->nabs; i++) {
			if (joydev->abspam[i] > ABS_MAX)
				return -EINVAL;
			joydev->absmap[joydev->abspam[i]] = i;
		}
		return 0;

	case JSIOCGAXMAP:
		return copy_to_user(argp, joydev->abspam,
			sizeof(__u8) * (ABS_MAX + 1)) ? -EFAULT : 0;

	case JSIOCSBTNMAP:
		if (copy_from_user(joydev->keypam, argp,
				   sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)))
			return -EFAULT;

		for (i = 0; i < joydev->nkey; i++) {
			if (joydev->keypam[i] > KEY_MAX ||
			    joydev->keypam[i] < BTN_MISC)
				return -EINVAL;
			joydev->keymap[joydev->keypam[i] - BTN_MISC] = i;
		}

		return 0;

	case JSIOCGBTNMAP:
		return copy_to_user(argp, joydev->keypam,
			sizeof(__u16) * (KEY_MAX - BTN_MISC + 1)) ? -EFAULT : 0;

	default:
		if ((cmd & ~IOCSIZE_MASK) == JSIOCGNAME(0)) {
			int len;
			if (!dev->name)
				return 0;
			len = strlen(dev->name) + 1;
			if (len > _IOC_SIZE(cmd))
				len = _IOC_SIZE(cmd);
			if (copy_to_user(argp, dev->name, len))
L
Linus Torvalds 已提交
549
				return -EFAULT;
550 551
			return len;
		}
L
Linus Torvalds 已提交
552 553 554 555
	}
	return -EINVAL;
}

556
#ifdef CONFIG_COMPAT
557 558
static long joydev_compat_ioctl(struct file *file,
				unsigned int cmd, unsigned long arg)
559
{
560 561
	struct joydev_client *client = file->private_data;
	struct joydev *joydev = client->joydev;
562 563 564
	void __user *argp = (void __user *)arg;
	s32 tmp32;
	struct JS_DATA_SAVE_TYPE_32 ds32;
565
	int retval;
566

567 568 569 570 571 572 573 574 575 576
	retval = mutex_lock_interruptible(&joydev->mutex);
	if (retval)
		return retval;

	if (!joydev->exist) {
		retval = -ENODEV;
		goto out;
	}

	switch (cmd) {
577

578
	case JS_SET_TIMELIMIT:
579 580
		retval = get_user(tmp32, (s32 __user *) arg);
		if (retval == 0)
581 582
			joydev->glue.JS_TIMELIMIT = tmp32;
		break;
583

584 585
	case JS_GET_TIMELIMIT:
		tmp32 = joydev->glue.JS_TIMELIMIT;
586
		retval = put_user(tmp32, (s32 __user *) arg);
587 588 589
		break;

	case JS_SET_ALL:
590 591 592
		retval = copy_from_user(&ds32, argp,
					sizeof(ds32)) ? -EFAULT : 0;
		if (retval == 0) {
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
			joydev->glue.JS_TIMEOUT    = ds32.JS_TIMEOUT;
			joydev->glue.BUSY          = ds32.BUSY;
			joydev->glue.JS_EXPIRETIME = ds32.JS_EXPIRETIME;
			joydev->glue.JS_TIMELIMIT  = ds32.JS_TIMELIMIT;
			joydev->glue.JS_SAVE       = ds32.JS_SAVE;
			joydev->glue.JS_CORR       = ds32.JS_CORR;
		}
		break;

	case JS_GET_ALL:
		ds32.JS_TIMEOUT    = joydev->glue.JS_TIMEOUT;
		ds32.BUSY          = joydev->glue.BUSY;
		ds32.JS_EXPIRETIME = joydev->glue.JS_EXPIRETIME;
		ds32.JS_TIMELIMIT  = joydev->glue.JS_TIMELIMIT;
		ds32.JS_SAVE       = joydev->glue.JS_SAVE;
		ds32.JS_CORR       = joydev->glue.JS_CORR;

610
		retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
611 612 613
		break;

	default:
614 615
		retval = joydev_ioctl_common(joydev, cmd, argp);
		break;
616
	}
617 618 619 620

 out:
	mutex_unlock(&joydev->mutex);
	return retval;
621 622 623
}
#endif /* CONFIG_COMPAT */

624 625
static long joydev_ioctl(struct file *file,
			 unsigned int cmd, unsigned long arg)
626
{
627 628
	struct joydev_client *client = file->private_data;
	struct joydev *joydev = client->joydev;
629
	void __user *argp = (void __user *)arg;
630
	int retval;
631

632 633 634
	retval = mutex_lock_interruptible(&joydev->mutex);
	if (retval)
		return retval;
635

636 637 638
	if (!joydev->exist) {
		retval = -ENODEV;
		goto out;
639
	}
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669

	switch (cmd) {

	case JS_SET_TIMELIMIT:
		retval = get_user(joydev->glue.JS_TIMELIMIT,
				  (long __user *) arg);
		break;

	case JS_GET_TIMELIMIT:
		retval = put_user(joydev->glue.JS_TIMELIMIT,
				  (long __user *) arg);
		break;

	case JS_SET_ALL:
		retval = copy_from_user(&joydev->glue, argp,
					sizeof(joydev->glue)) ? -EFAULT: 0;
		break;

	case JS_GET_ALL:
		retval = copy_to_user(argp, &joydev->glue,
				      sizeof(joydev->glue)) ? -EFAULT : 0;
		break;

	default:
		retval = joydev_ioctl_common(joydev, cmd, argp);
		break;
	}
 out:
	mutex_unlock(&joydev->mutex);
	return retval;
670 671
}

D
Dmitry Torokhov 已提交
672
static const struct file_operations joydev_fops = {
673 674 675 676 677 678
	.owner		= THIS_MODULE,
	.read		= joydev_read,
	.poll		= joydev_poll,
	.open		= joydev_open,
	.release	= joydev_release,
	.unlocked_ioctl	= joydev_ioctl,
679
#ifdef CONFIG_COMPAT
680
	.compat_ioctl	= joydev_compat_ioctl,
681
#endif
682
	.fasync		= joydev_fasync,
L
Linus Torvalds 已提交
683 684
};

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
static int joydev_install_chrdev(struct joydev *joydev)
{
	joydev_table[joydev->minor] = joydev;
	return 0;
}

static void joydev_remove_chrdev(struct joydev *joydev)
{
	mutex_lock(&joydev_table_mutex);
	joydev_table[joydev->minor] = NULL;
	mutex_unlock(&joydev_table_mutex);
}

/*
 * Mark device non-existant. This disables writes, ioctls and
 * prevents new users from opening the device. Already posted
 * blocking reads will stay, however new ones will fail.
 */
static void joydev_mark_dead(struct joydev *joydev)
{
	mutex_lock(&joydev->mutex);
	joydev->exist = 0;
	mutex_unlock(&joydev->mutex);
}

static void joydev_cleanup(struct joydev *joydev)
{
	struct input_handle *handle = &joydev->handle;

	joydev_mark_dead(joydev);
	joydev_hangup(joydev);
	joydev_remove_chrdev(joydev);

	/* joydev is marked dead so noone else accesses joydev->open */
	if (joydev->open)
		input_close_device(handle);
}

723 724
static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
			  const struct input_device_id *id)
L
Linus Torvalds 已提交
725 726 727
{
	struct joydev *joydev;
	int i, j, t, minor;
728
	int error;
L
Linus Torvalds 已提交
729

730 731 732 733
	for (minor = 0; minor < JOYDEV_MINORS; minor++)
		if (!joydev_table[minor])
			break;

L
Linus Torvalds 已提交
734 735
	if (minor == JOYDEV_MINORS) {
		printk(KERN_ERR "joydev: no more free joydev devices\n");
736
		return -ENFILE;
L
Linus Torvalds 已提交
737 738
	}

739 740 741
	joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
	if (!joydev)
		return -ENOMEM;
L
Linus Torvalds 已提交
742

743
	INIT_LIST_HEAD(&joydev->client_list);
744 745
	spin_lock_init(&joydev->client_lock);
	mutex_init(&joydev->mutex);
L
Linus Torvalds 已提交
746 747
	init_waitqueue_head(&joydev->wait);

748 749
	snprintf(joydev->name, sizeof(joydev->name), "js%d", minor);
	joydev->exist = 1;
L
Linus Torvalds 已提交
750
	joydev->minor = minor;
751

L
Linus Torvalds 已提交
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
	joydev->exist = 1;
	joydev->handle.dev = dev;
	joydev->handle.name = joydev->name;
	joydev->handle.handler = handler;
	joydev->handle.private = joydev;

	for (i = 0; i < ABS_MAX + 1; i++)
		if (test_bit(i, dev->absbit)) {
			joydev->absmap[i] = joydev->nabs;
			joydev->abspam[joydev->nabs] = i;
			joydev->nabs++;
		}

	for (i = BTN_JOYSTICK - BTN_MISC; i < KEY_MAX - BTN_MISC + 1; i++)
		if (test_bit(i + BTN_MISC, dev->keybit)) {
			joydev->keymap[i] = joydev->nkey;
			joydev->keypam[joydev->nkey] = i + BTN_MISC;
			joydev->nkey++;
		}

772
	for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
L
Linus Torvalds 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
		if (test_bit(i + BTN_MISC, dev->keybit)) {
			joydev->keymap[i] = joydev->nkey;
			joydev->keypam[joydev->nkey] = i + BTN_MISC;
			joydev->nkey++;
		}

	for (i = 0; i < joydev->nabs; i++) {
		j = joydev->abspam[i];
		if (dev->absmax[j] == dev->absmin[j]) {
			joydev->corr[i].type = JS_CORR_NONE;
			joydev->abs[i] = dev->abs[j];
			continue;
		}
		joydev->corr[i].type = JS_CORR_BROKEN;
		joydev->corr[i].prec = dev->absfuzz[j];
788 789 790 791 792 793 794 795 796 797 798 799 800
		joydev->corr[i].coef[0] =
			(dev->absmax[j] + dev->absmin[j]) / 2 - dev->absflat[j];
		joydev->corr[i].coef[1] =
			(dev->absmax[j] + dev->absmin[j]) / 2 + dev->absflat[j];

		t = (dev->absmax[j] - dev->absmin[j]) / 2 - 2 * dev->absflat[j];
		if (t) {
			joydev->corr[i].coef[2] = (1 << 29) / t;
			joydev->corr[i].coef[3] = (1 << 29) / t;

			joydev->abs[i] = joydev_correct(dev->abs[j],
							joydev->corr + i);
		}
L
Linus Torvalds 已提交
801 802
	}

803 804
	strlcpy(joydev->dev.bus_id, joydev->name, sizeof(joydev->dev.bus_id));
	joydev->dev.devt = MKDEV(INPUT_MAJOR, JOYDEV_MINOR_BASE + minor);
805 806 807 808
	joydev->dev.class = &input_class;
	joydev->dev.parent = &dev->dev;
	joydev->dev.release = joydev_free;
	device_initialize(&joydev->dev);
809

810
	error = input_register_handle(&joydev->handle);
811
	if (error)
812
		goto err_free_joydev;
813

814
	error = joydev_install_chrdev(joydev);
815
	if (error)
816 817 818 819 820
		goto err_unregister_handle;

	error = device_add(&joydev->dev);
	if (error)
		goto err_cleanup_joydev;
821 822

	return 0;
L
Linus Torvalds 已提交
823

824 825 826 827
 err_cleanup_joydev:
	joydev_cleanup(joydev);
 err_unregister_handle:
	input_unregister_handle(&joydev->handle);
828
 err_free_joydev:
829
	put_device(&joydev->dev);
830
	return error;
L
Linus Torvalds 已提交
831 832 833 834 835 836
}

static void joydev_disconnect(struct input_handle *handle)
{
	struct joydev *joydev = handle->private;

837
	device_del(&joydev->dev);
838 839
	joydev_cleanup(joydev);
	input_unregister_handle(handle);
840
	put_device(&joydev->dev);
L
Linus Torvalds 已提交
841 842
}

D
Dmitry Torokhov 已提交
843
static const struct input_device_id joydev_blacklist[] = {
L
Linus Torvalds 已提交
844
	{
845 846
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_KEYBIT,
847 848
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH) },
849 850
	},	/* Avoid itouchpads, touchscreens and tablets */
	{ }	/* Terminating entry */
L
Linus Torvalds 已提交
851 852
};

D
Dmitry Torokhov 已提交
853
static const struct input_device_id joydev_ids[] = {
L
Linus Torvalds 已提交
854
	{
855 856
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_ABSBIT,
857 858
		.evbit = { BIT_MASK(EV_ABS) },
		.absbit = { BIT_MASK(ABS_X) },
L
Linus Torvalds 已提交
859 860
	},
	{
861 862
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_ABSBIT,
863 864
		.evbit = { BIT_MASK(EV_ABS) },
		.absbit = { BIT_MASK(ABS_WHEEL) },
L
Linus Torvalds 已提交
865 866
	},
	{
867 868
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_ABSBIT,
869 870
		.evbit = { BIT_MASK(EV_ABS) },
		.absbit = { BIT_MASK(ABS_THROTTLE) },
L
Linus Torvalds 已提交
871
	},
872
	{ }	/* Terminating entry */
L
Linus Torvalds 已提交
873 874 875 876 877
};

MODULE_DEVICE_TABLE(input, joydev_ids);

static struct input_handler joydev_handler = {
878 879 880 881 882 883 884 885
	.event		= joydev_event,
	.connect	= joydev_connect,
	.disconnect	= joydev_disconnect,
	.fops		= &joydev_fops,
	.minor		= JOYDEV_MINOR_BASE,
	.name		= "joydev",
	.id_table	= joydev_ids,
	.blacklist	= joydev_blacklist,
L
Linus Torvalds 已提交
886 887 888 889
};

static int __init joydev_init(void)
{
890
	return input_register_handler(&joydev_handler);
L
Linus Torvalds 已提交
891 892 893 894 895 896 897 898 899
}

static void __exit joydev_exit(void)
{
	input_unregister_handler(&joydev_handler);
}

module_init(joydev_init);
module_exit(joydev_exit);