joydev.c 22.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * 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.
 */

J
Joe Perches 已提交
13 14
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
15 16 17 18 19 20 21
#include <asm/io.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>
22
#include <linux/sched.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29
#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>
30
#include <linux/cdev.h>
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44

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 open;
	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
	struct device dev;
49
	struct cdev cdev;
50
	bool exist;
51

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

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

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

	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 已提交
89 90
	}

91
	return value < -32767 ? -32767 : (value > 32767 ? 32767 : value);
L
Linus Torvalds 已提交
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
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 已提交
120 121
{
	struct joydev *joydev = handle->private;
122
	struct joydev_client *client;
L
Linus Torvalds 已提交
123 124 125 126
	struct js_event event;

	switch (type) {

127 128 129 130 131 132 133
	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 已提交
134

135 136 137 138 139 140
	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 已提交
141
			return;
142 143 144 145 146
		joydev->abs[event.number] = event.value;
		break;

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

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

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

	wake_up_interruptible(&joydev->wait);
}

static int joydev_fasync(int fd, struct file *file, int on)
{
161
	struct joydev_client *client = file->private_data;
162

163
	return fasync_helper(fd, file, on, &client->fasync);
L
Linus Torvalds 已提交
164 165
}

166
static void joydev_free(struct device *dev)
L
Linus Torvalds 已提交
167
{
168 169
	struct joydev *joydev = container_of(dev, struct joydev, dev);

170
	input_put_device(joydev->handle.dev);
L
Linus Torvalds 已提交
171 172 173
	kfree(joydev);
}

174 175 176 177 178 179 180 181 182 183 184 185 186 187
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);
}

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 已提交
188
	synchronize_rcu();
189 190 191 192 193 194 195 196 197 198 199 200
}

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;
201
	else if (!joydev->open++) {
202
		retval = input_open_device(&joydev->handle);
203 204 205
		if (retval)
			joydev->open--;
	}
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

	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);
}

237
static int joydev_release(struct inode *inode, struct file *file)
L
Linus Torvalds 已提交
238
{
239 240
	struct joydev_client *client = file->private_data;
	struct joydev *joydev = client->joydev;
L
Linus Torvalds 已提交
241

242
	joydev_detach_client(joydev, client);
243
	kfree(client);
L
Linus Torvalds 已提交
244

245
	joydev_close_device(joydev);
246
	put_device(&joydev->dev);
L
Linus Torvalds 已提交
247 248 249 250 251 252

	return 0;
}

static int joydev_open(struct inode *inode, struct file *file)
{
253 254
	struct joydev *joydev =
			container_of(inode->i_cdev, struct joydev, cdev);
255
	struct joydev_client *client;
256
	int error;
L
Linus Torvalds 已提交
257

258
	client = kzalloc(sizeof(struct joydev_client), GFP_KERNEL);
259 260
	if (!client)
		return -ENOMEM;
L
Linus Torvalds 已提交
261

262
	spin_lock_init(&client->buffer_lock);
263
	client->joydev = joydev;
264
	joydev_attach_client(joydev, client);
L
Linus Torvalds 已提交
265

266 267 268
	error = joydev_open_device(joydev);
	if (error)
		goto err_free_client;
L
Linus Torvalds 已提交
269

270
	file->private_data = client;
271 272
	nonseekable_open(inode, file);

273
	get_device(&joydev->dev);
L
Linus Torvalds 已提交
274
	return 0;
275 276

 err_free_client:
277
	joydev_detach_client(joydev, client);
278 279
	kfree(client);
	return error;
L
Linus Torvalds 已提交
280 281
}

282 283 284
static int joydev_generate_startup_event(struct joydev_client *client,
					 struct input_dev *input,
					 struct js_event *event)
L
Linus Torvalds 已提交
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
	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 已提交
368 369
}

370 371 372 373 374 375 376 377 378 379
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 已提交
380
{
381 382
	struct joydev_client *client = file->private_data;
	struct joydev *joydev = client->joydev;
L
Linus Torvalds 已提交
383
	struct input_dev *input = joydev->handle.dev;
384 385
	struct js_event event;
	int retval;
L
Linus Torvalds 已提交
386

387
	if (!joydev->exist)
L
Linus Torvalds 已提交
388 389 390 391 392
		return -ENODEV;

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

393 394
	if (count == sizeof(struct JS_DATA_TYPE))
		return joydev_0x_read(client, input, buf);
L
Linus Torvalds 已提交
395

396
	if (!joydev_data_pending(client) && (file->f_flags & O_NONBLOCK))
397
		return -EAGAIN;
L
Linus Torvalds 已提交
398

399
	retval = wait_event_interruptible(joydev->wait,
400
			!joydev->exist || joydev_data_pending(client));
L
Linus Torvalds 已提交
401 402 403
	if (retval)
		return retval;

404
	if (!joydev->exist)
L
Linus Torvalds 已提交
405 406
		return -ENODEV;

407 408
	while (retval + sizeof(struct js_event) <= count &&
	       joydev_generate_startup_event(client, input, &event)) {
L
Linus Torvalds 已提交
409 410 411 412 413 414 415

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

		retval += sizeof(struct js_event);
	}

416 417
	while (retval + sizeof(struct js_event) <= count &&
	       joydev_fetch_next_event(client, &event)) {
L
Linus Torvalds 已提交
418

419
		if (copy_to_user(buf + retval, &event, sizeof(struct js_event)))
L
Linus Torvalds 已提交
420 421 422 423 424 425 426 427 428 429 430
			return -EFAULT;

		retval += sizeof(struct js_event);
	}

	return retval;
}

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

434
	poll_wait(file, &joydev->wait, wait);
435 436
	return (joydev_data_pending(client) ? (POLLIN | POLLRDNORM) : 0) |
		(joydev->exist ?  0 : (POLLHUP | POLLERR));
L
Linus Torvalds 已提交
437 438
}

439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
static int joydev_handle_JSIOCSAXMAP(struct joydev *joydev,
				     void __user *argp, size_t len)
{
	__u8 *abspam;
	int i;
	int retval = 0;

	len = min(len, sizeof(joydev->abspam));

	/* Validate the map. */
	abspam = kmalloc(len, GFP_KERNEL);
	if (!abspam)
		return -ENOMEM;

	if (copy_from_user(abspam, argp, len)) {
		retval = -EFAULT;
		goto out;
	}

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

	memcpy(joydev->abspam, abspam, len);

467 468 469
	for (i = 0; i < joydev->nabs; i++)
		joydev->absmap[joydev->abspam[i]] = i;

470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
 out:
	kfree(abspam);
	return retval;
}

static int joydev_handle_JSIOCSBTNMAP(struct joydev *joydev,
				      void __user *argp, size_t len)
{
	__u16 *keypam;
	int i;
	int retval = 0;

	len = min(len, sizeof(joydev->keypam));

	/* Validate the map. */
	keypam = kmalloc(len, GFP_KERNEL);
	if (!keypam)
		return -ENOMEM;

	if (copy_from_user(keypam, argp, len)) {
		retval = -EFAULT;
		goto out;
	}

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

	memcpy(joydev->keypam, keypam, len);

	for (i = 0; i < joydev->nkey; i++)
		joydev->keymap[keypam[i] - BTN_MISC] = i;

 out:
	kfree(keypam);
	return retval;
}


512 513
static int joydev_ioctl_common(struct joydev *joydev,
				unsigned int cmd, void __user *argp)
L
Linus Torvalds 已提交
514 515
{
	struct input_dev *dev = joydev->handle.dev;
516
	size_t len;
517
	int i;
518
	const char *name;
L
Linus Torvalds 已提交
519

520
	/* Process fixed-sized commands. */
L
Linus Torvalds 已提交
521 522
	switch (cmd) {

523 524
	case JS_SET_CAL:
		return copy_from_user(&joydev->glue.JS_CORR, argp,
525
				sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
526

527 528
	case JS_GET_CAL:
		return copy_to_user(argp, &joydev->glue.JS_CORR,
529
				sizeof(joydev->glue.JS_CORR)) ? -EFAULT : 0;
530

531 532
	case JS_SET_TIMEOUT:
		return get_user(joydev->glue.JS_TIMEOUT, (s32 __user *) argp);
533

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

537 538
	case JSIOCGVERSION:
		return put_user(JS_VERSION, (__u32 __user *) argp);
539

540 541
	case JSIOCGAXES:
		return put_user(joydev->nabs, (__u8 __user *) argp);
542

543 544
	case JSIOCGBUTTONS:
		return put_user(joydev->nkey, (__u8 __user *) argp);
545

546 547 548
	case JSIOCSCORR:
		if (copy_from_user(joydev->corr, argp,
			      sizeof(joydev->corr[0]) * joydev->nabs))
549
			return -EFAULT;
550

551
		for (i = 0; i < joydev->nabs; i++) {
552 553
			int val = input_abs_get_val(dev, joydev->abspam[i]);
			joydev->abs[i] = joydev_correct(val, &joydev->corr[i]);
554 555
		}
		return 0;
556

557 558 559 560
	case JSIOCGCORR:
		return copy_to_user(argp, joydev->corr,
			sizeof(joydev->corr[0]) * joydev->nabs) ? -EFAULT : 0;

561 562 563 564 565 566 567 568 569 570
	}

	/*
	 * Process variable-sized commands (the axis and button map commands
	 * are considered variable-sized to decouple them from the values of
	 * ABS_MAX and KEY_MAX).
	 */
	switch (cmd & ~IOCSIZE_MASK) {

	case (JSIOCSAXMAP & ~IOCSIZE_MASK):
571
		return joydev_handle_JSIOCSAXMAP(joydev, argp, _IOC_SIZE(cmd));
572

573 574
	case (JSIOCGAXMAP & ~IOCSIZE_MASK):
		len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->abspam));
575
		return copy_to_user(argp, joydev->abspam, len) ? -EFAULT : len;
576 577

	case (JSIOCSBTNMAP & ~IOCSIZE_MASK):
578
		return joydev_handle_JSIOCSBTNMAP(joydev, argp, _IOC_SIZE(cmd));
579

580 581
	case (JSIOCGBTNMAP & ~IOCSIZE_MASK):
		len = min_t(size_t, _IOC_SIZE(cmd), sizeof(joydev->keypam));
582
		return copy_to_user(argp, joydev->keypam, len) ? -EFAULT : len;
583

584 585 586 587 588 589 590
	case JSIOCGNAME(0):
		name = dev->name;
		if (!name)
			return 0;

		len = min_t(size_t, _IOC_SIZE(cmd), strlen(name) + 1);
		return copy_to_user(argp, name, len) ? -EFAULT : len;
L
Linus Torvalds 已提交
591
	}
592

L
Linus Torvalds 已提交
593 594 595
	return -EINVAL;
}

596
#ifdef CONFIG_COMPAT
597 598
static long joydev_compat_ioctl(struct file *file,
				unsigned int cmd, unsigned long arg)
599
{
600 601
	struct joydev_client *client = file->private_data;
	struct joydev *joydev = client->joydev;
602 603 604
	void __user *argp = (void __user *)arg;
	s32 tmp32;
	struct JS_DATA_SAVE_TYPE_32 ds32;
605
	int retval;
606

607 608 609 610 611 612 613 614 615 616
	retval = mutex_lock_interruptible(&joydev->mutex);
	if (retval)
		return retval;

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

	switch (cmd) {
617

618
	case JS_SET_TIMELIMIT:
619 620
		retval = get_user(tmp32, (s32 __user *) arg);
		if (retval == 0)
621 622
			joydev->glue.JS_TIMELIMIT = tmp32;
		break;
623

624 625
	case JS_GET_TIMELIMIT:
		tmp32 = joydev->glue.JS_TIMELIMIT;
626
		retval = put_user(tmp32, (s32 __user *) arg);
627 628 629
		break;

	case JS_SET_ALL:
630 631 632
		retval = copy_from_user(&ds32, argp,
					sizeof(ds32)) ? -EFAULT : 0;
		if (retval == 0) {
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
			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;

650
		retval = copy_to_user(argp, &ds32, sizeof(ds32)) ? -EFAULT : 0;
651 652 653
		break;

	default:
654 655
		retval = joydev_ioctl_common(joydev, cmd, argp);
		break;
656
	}
657 658 659 660

 out:
	mutex_unlock(&joydev->mutex);
	return retval;
661 662 663
}
#endif /* CONFIG_COMPAT */

664 665
static long joydev_ioctl(struct file *file,
			 unsigned int cmd, unsigned long arg)
666
{
667 668
	struct joydev_client *client = file->private_data;
	struct joydev *joydev = client->joydev;
669
	void __user *argp = (void __user *)arg;
670
	int retval;
671

672 673 674
	retval = mutex_lock_interruptible(&joydev->mutex);
	if (retval)
		return retval;
675

676 677 678
	if (!joydev->exist) {
		retval = -ENODEV;
		goto out;
679
	}
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

	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,
B
Baodong Chen 已提交
695
					sizeof(joydev->glue)) ? -EFAULT : 0;
696 697 698 699 700 701 702 703 704 705 706 707 708 709
		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;
710 711
}

D
Dmitry Torokhov 已提交
712
static const struct file_operations joydev_fops = {
713 714 715 716 717 718
	.owner		= THIS_MODULE,
	.read		= joydev_read,
	.poll		= joydev_poll,
	.open		= joydev_open,
	.release	= joydev_release,
	.unlocked_ioctl	= joydev_ioctl,
719
#ifdef CONFIG_COMPAT
720
	.compat_ioctl	= joydev_compat_ioctl,
721
#endif
722
	.fasync		= joydev_fasync,
723
	.llseek		= no_llseek,
L
Linus Torvalds 已提交
724 725
};

726
/*
L
Lucas De Marchi 已提交
727
 * Mark device non-existent. This disables writes, ioctls and
728 729 730 731 732 733
 * 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);
734
	joydev->exist = false;
735 736 737 738 739 740 741 742 743
	mutex_unlock(&joydev->mutex);
}

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

	joydev_mark_dead(joydev);
	joydev_hangup(joydev);
744 745

	cdev_del(&joydev->cdev);
746

L
Lucas De Marchi 已提交
747
	/* joydev is marked dead so no one else accesses joydev->open */
748 749 750 751
	if (joydev->open)
		input_close_device(handle);
}

752 753 754 755 756 757 758 759 760 761 762 763 764 765

static bool joydev_match(struct input_handler *handler, struct input_dev *dev)
{
	/* Avoid touchpads and touchscreens */
	if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_TOUCH, dev->keybit))
		return false;

	/* Avoid tablets, digitisers and similar devices */
	if (test_bit(EV_KEY, dev->evbit) && test_bit(BTN_DIGI, dev->keybit))
		return false;

	return true;
}

766 767
static int joydev_connect(struct input_handler *handler, struct input_dev *dev,
			  const struct input_device_id *id)
L
Linus Torvalds 已提交
768 769
{
	struct joydev *joydev;
770
	int i, j, t, minor, dev_no;
771
	int error;
L
Linus Torvalds 已提交
772

773 774 775 776 777
	minor = input_get_new_minor(JOYDEV_MINOR_BASE, JOYDEV_MINORS, true);
	if (minor < 0) {
		error = minor;
		pr_err("failed to reserve new minor: %d\n", error);
		return error;
L
Linus Torvalds 已提交
778 779
	}

780
	joydev = kzalloc(sizeof(struct joydev), GFP_KERNEL);
781 782 783 784
	if (!joydev) {
		error = -ENOMEM;
		goto err_free_minor;
	}
L
Linus Torvalds 已提交
785

786
	INIT_LIST_HEAD(&joydev->client_list);
787 788
	spin_lock_init(&joydev->client_lock);
	mutex_init(&joydev->mutex);
L
Linus Torvalds 已提交
789
	init_waitqueue_head(&joydev->wait);
790
	joydev->exist = true;
791 792 793 794 795 796

	dev_no = minor;
	/* Normalize device number if it falls into legacy range */
	if (dev_no < JOYDEV_MINOR_BASE + JOYDEV_MINORS)
		dev_no -= JOYDEV_MINOR_BASE;
	dev_set_name(&joydev->dev, "js%d", dev_no);
797

798
	joydev->handle.dev = input_get_device(dev);
799
	joydev->handle.name = dev_name(&joydev->dev);
L
Linus Torvalds 已提交
800 801 802
	joydev->handle.handler = handler;
	joydev->handle.private = joydev;

803
	for (i = 0; i < ABS_CNT; i++)
L
Linus Torvalds 已提交
804 805 806 807 808 809 810 811 812 813 814 815 816
		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++;
		}

817
	for (i = 0; i < BTN_JOYSTICK - BTN_MISC; i++)
L
Linus Torvalds 已提交
818 819 820 821 822 823 824 825
		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];
826
		if (input_abs_get_max(dev, j) == input_abs_get_min(dev, j)) {
L
Linus Torvalds 已提交
827
			joydev->corr[i].type = JS_CORR_NONE;
828
			joydev->abs[i] = input_abs_get_val(dev, j);
L
Linus Torvalds 已提交
829 830 831
			continue;
		}
		joydev->corr[i].type = JS_CORR_BROKEN;
832 833 834 835 836
		joydev->corr[i].prec = input_abs_get_fuzz(dev, j);

		t = (input_abs_get_max(dev, j) + input_abs_get_min(dev, j)) / 2;
		joydev->corr[i].coef[0] = t - input_abs_get_flat(dev, j);
		joydev->corr[i].coef[1] = t + input_abs_get_flat(dev, j);
837

838 839
		t = (input_abs_get_max(dev, j) - input_abs_get_min(dev, j)) / 2
			- 2 * input_abs_get_flat(dev, j);
840 841 842 843
		if (t) {
			joydev->corr[i].coef[2] = (1 << 29) / t;
			joydev->corr[i].coef[3] = (1 << 29) / t;

844 845 846
			joydev->abs[i] =
				joydev_correct(input_abs_get_val(dev, j),
					       joydev->corr + i);
847
		}
L
Linus Torvalds 已提交
848 849
	}

850
	joydev->dev.devt = MKDEV(INPUT_MAJOR, minor);
851 852 853 854
	joydev->dev.class = &input_class;
	joydev->dev.parent = &dev->dev;
	joydev->dev.release = joydev_free;
	device_initialize(&joydev->dev);
855

856
	error = input_register_handle(&joydev->handle);
857
	if (error)
858
		goto err_free_joydev;
859

860 861
	cdev_init(&joydev->cdev, &joydev_fops);
	error = cdev_add(&joydev->cdev, joydev->dev.devt, 1);
862
	if (error)
863 864 865 866 867
		goto err_unregister_handle;

	error = device_add(&joydev->dev);
	if (error)
		goto err_cleanup_joydev;
868 869

	return 0;
L
Linus Torvalds 已提交
870

871 872 873 874
 err_cleanup_joydev:
	joydev_cleanup(joydev);
 err_unregister_handle:
	input_unregister_handle(&joydev->handle);
875
 err_free_joydev:
876
	put_device(&joydev->dev);
877 878
 err_free_minor:
	input_free_minor(minor);
879
	return error;
L
Linus Torvalds 已提交
880 881 882 883 884 885
}

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

886
	device_del(&joydev->dev);
887
	joydev_cleanup(joydev);
888
	input_free_minor(MINOR(joydev->dev.devt));
889
	input_unregister_handle(handle);
890
	put_device(&joydev->dev);
L
Linus Torvalds 已提交
891 892
}

D
Dmitry Torokhov 已提交
893
static const struct input_device_id joydev_ids[] = {
L
Linus Torvalds 已提交
894
	{
895 896
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_ABSBIT,
897 898
		.evbit = { BIT_MASK(EV_ABS) },
		.absbit = { BIT_MASK(ABS_X) },
L
Linus Torvalds 已提交
899 900
	},
	{
901 902
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_ABSBIT,
903 904
		.evbit = { BIT_MASK(EV_ABS) },
		.absbit = { BIT_MASK(ABS_WHEEL) },
L
Linus Torvalds 已提交
905 906
	},
	{
907 908
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_ABSBIT,
909 910
		.evbit = { BIT_MASK(EV_ABS) },
		.absbit = { BIT_MASK(ABS_THROTTLE) },
L
Linus Torvalds 已提交
911
	},
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_KEYBIT,
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = {[BIT_WORD(BTN_JOYSTICK)] = BIT_MASK(BTN_JOYSTICK) },
	},
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_KEYBIT,
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(BTN_GAMEPAD)] = BIT_MASK(BTN_GAMEPAD) },
	},
	{
		.flags = INPUT_DEVICE_ID_MATCH_EVBIT |
				INPUT_DEVICE_ID_MATCH_KEYBIT,
		.evbit = { BIT_MASK(EV_KEY) },
		.keybit = { [BIT_WORD(BTN_TRIGGER_HAPPY)] = BIT_MASK(BTN_TRIGGER_HAPPY) },
	},
930
	{ }	/* Terminating entry */
L
Linus Torvalds 已提交
931 932 933 934 935
};

MODULE_DEVICE_TABLE(input, joydev_ids);

static struct input_handler joydev_handler = {
936
	.event		= joydev_event,
937
	.match		= joydev_match,
938 939
	.connect	= joydev_connect,
	.disconnect	= joydev_disconnect,
940
	.legacy_minors	= true,
941 942 943
	.minor		= JOYDEV_MINOR_BASE,
	.name		= "joydev",
	.id_table	= joydev_ids,
L
Linus Torvalds 已提交
944 945 946 947
};

static int __init joydev_init(void)
{
948
	return input_register_handler(&joydev_handler);
L
Linus Torvalds 已提交
949 950 951 952 953 954 955 956 957
}

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

module_init(joydev_init);
module_exit(joydev_exit);