gameport.c 20.0 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
/*
 * Generic gameport layer
 *
 * Copyright (c) 1999-2002 Vojtech Pavlik
 * Copyright (c) 2005 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/stddef.h>
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/gameport.h>
#include <linux/wait.h>
#include <linux/slab.h>
#include <linux/delay.h>
22
#include <linux/kthread.h>
T
Tim Schmielau 已提交
23
#include <linux/sched.h>	/* HZ */
24
#include <linux/mutex.h>
25
#include <linux/freezer.h>
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

/*#include <asm/io.h>*/

MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
MODULE_DESCRIPTION("Generic gameport layer");
MODULE_LICENSE("GPL");

EXPORT_SYMBOL(__gameport_register_port);
EXPORT_SYMBOL(gameport_unregister_port);
EXPORT_SYMBOL(__gameport_register_driver);
EXPORT_SYMBOL(gameport_unregister_driver);
EXPORT_SYMBOL(gameport_open);
EXPORT_SYMBOL(gameport_close);
EXPORT_SYMBOL(gameport_set_phys);
EXPORT_SYMBOL(gameport_start_polling);
EXPORT_SYMBOL(gameport_stop_polling);

/*
44
 * gameport_mutex protects entire gameport subsystem and is taken
L
Linus Torvalds 已提交
45 46
 * every time gameport port or driver registrered or unregistered.
 */
47
static DEFINE_MUTEX(gameport_mutex);
L
Linus Torvalds 已提交
48 49 50

static LIST_HEAD(gameport_list);

51
static struct bus_type gameport_bus;
L
Linus Torvalds 已提交
52

53
static void gameport_add_driver(struct gameport_driver *drv);
L
Linus Torvalds 已提交
54 55 56 57 58 59 60
static void gameport_add_port(struct gameport *gameport);
static void gameport_destroy_port(struct gameport *gameport);
static void gameport_reconnect_port(struct gameport *gameport);
static void gameport_disconnect_port(struct gameport *gameport);

#if defined(__i386__)

61 62
#include <asm/i8253.h>

L
Linus Torvalds 已提交
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
#define DELTA(x,y)      ((y)-(x)+((y)<(x)?1193182/HZ:0))
#define GET_TIME(x)     do { x = get_time_pit(); } while (0)

static unsigned int get_time_pit(void)
{
	unsigned long flags;
	unsigned int count;

	spin_lock_irqsave(&i8253_lock, flags);
	outb_p(0x00, 0x43);
	count = inb_p(0x40);
	count |= inb_p(0x40) << 8;
	spin_unlock_irqrestore(&i8253_lock, flags);

	return count;
}

#endif



/*
 * gameport_measure_speed() measures the gameport i/o speed.
 */

static int gameport_measure_speed(struct gameport *gameport)
{
#if defined(__i386__)

	unsigned int i, t, t1, t2, t3, tx;
	unsigned long flags;

	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
		return 0;

	tx = 1 << 30;

	for(i = 0; i < 50; i++) {
		local_irq_save(flags);
		GET_TIME(t1);
		for (t = 0; t < 50; t++) gameport_read(gameport);
		GET_TIME(t2);
		GET_TIME(t3);
		local_irq_restore(flags);
		udelay(i * 10);
		if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
	}

	gameport_close(gameport);
	return 59659 / (tx < 1 ? 1 : tx);

#elif defined (__x86_64__)

	unsigned int i, t;
	unsigned long tx, t1, t2, flags;

	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
		return 0;

	tx = 1 << 30;

	for(i = 0; i < 50; i++) {
		local_irq_save(flags);
		rdtscl(t1);
		for (t = 0; t < 50; t++) gameport_read(gameport);
		rdtscl(t2);
		local_irq_restore(flags);
		udelay(i * 10);
		if (t2 - t1 < tx) tx = t2 - t1;
	}

	gameport_close(gameport);
135 136
	return (cpu_data(raw_smp_processor_id()).loops_per_jiffy *
		(unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
L
Linus Torvalds 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 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

#else

	unsigned int j, t = 0;

	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
		return 0;

	j = jiffies; while (j == jiffies);
	j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }

	gameport_close(gameport);
	return t * HZ / 1000;

#endif
}

void gameport_start_polling(struct gameport *gameport)
{
	spin_lock(&gameport->timer_lock);

	if (!gameport->poll_cnt++) {
		BUG_ON(!gameport->poll_handler);
		BUG_ON(!gameport->poll_interval);
		mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
	}

	spin_unlock(&gameport->timer_lock);
}

void gameport_stop_polling(struct gameport *gameport)
{
	spin_lock(&gameport->timer_lock);

	if (!--gameport->poll_cnt)
		del_timer(&gameport->poll_timer);

	spin_unlock(&gameport->timer_lock);
}

static void gameport_run_poll_handler(unsigned long d)
{
	struct gameport *gameport = (struct gameport *)d;

	gameport->poll_handler(gameport);
	if (gameport->poll_cnt)
		mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
}

/*
 * Basic gameport -> driver core mappings
 */

190
static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
L
Linus Torvalds 已提交
191
{
192 193
	int error;

L
Linus Torvalds 已提交
194 195 196
	gameport->dev.driver = &drv->driver;
	if (drv->connect(gameport, drv)) {
		gameport->dev.driver = NULL;
197
		return -ENODEV;
L
Linus Torvalds 已提交
198
	}
199 200 201 202 203 204 205 206 207 208

	error = device_bind_driver(&gameport->dev);
	if (error) {
		printk(KERN_WARNING
			"gameport: device_bind_driver() failed "
			"for %s (%s) and %s, error: %d\n",
			gameport->phys, gameport->name,
			drv->description, error);
		drv->disconnect(gameport);
		gameport->dev.driver = NULL;
209
		return error;
210 211
	}

212
	return 0;
L
Linus Torvalds 已提交
213 214 215 216
}

static void gameport_find_driver(struct gameport *gameport)
{
217 218 219 220 221 222 223
	int error;

	error = device_attach(&gameport->dev);
	if (error < 0)
		printk(KERN_WARNING
			"gameport: device_attach() failed for %s (%s), error: %d\n",
			gameport->phys, gameport->name, error);
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231 232 233
}


/*
 * Gameport event processing.
 */

enum gameport_event_type {
	GAMEPORT_REGISTER_PORT,
	GAMEPORT_REGISTER_DRIVER,
234
	GAMEPORT_ATTACH_DRIVER,
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243 244 245 246
};

struct gameport_event {
	enum gameport_event_type type;
	void *object;
	struct module *owner;
	struct list_head node;
};

static DEFINE_SPINLOCK(gameport_event_lock);	/* protects gameport_event_list */
static LIST_HEAD(gameport_event_list);
static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
247
static struct task_struct *gameport_task;
L
Linus Torvalds 已提交
248

249 250
static int gameport_queue_event(void *object, struct module *owner,
				enum gameport_event_type event_type)
L
Linus Torvalds 已提交
251 252 253
{
	unsigned long flags;
	struct gameport_event *event;
254
	int retval = 0;
L
Linus Torvalds 已提交
255 256 257 258

	spin_lock_irqsave(&gameport_event_lock, flags);

	/*
259
	 * Scan event list for the other events for the same gameport port,
L
Linus Torvalds 已提交
260 261 262 263
	 * starting with the most recent one. If event is the same we
	 * do not need add new one. If event is of different type we
	 * need to add this event and should not look further because
	 * we need to preseve sequence of distinct events.
264
	 */
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272
	list_for_each_entry_reverse(event, &gameport_event_list, node) {
		if (event->object == object) {
			if (event->type == event_type)
				goto out;
			break;
		}
	}

273 274 275 276 277 278 279 280
	event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC);
	if (!event) {
		printk(KERN_ERR
			"gameport: Not enough memory to queue event %d\n",
			event_type);
		retval = -ENOMEM;
		goto out;
	}
L
Linus Torvalds 已提交
281

282 283 284 285 286 287 288
	if (!try_module_get(owner)) {
		printk(KERN_WARNING
			"gameport: Can't get module reference, dropping event %d\n",
			event_type);
		kfree(event);
		retval = -EINVAL;
		goto out;
L
Linus Torvalds 已提交
289
	}
290 291 292 293 294 295 296 297

	event->type = event_type;
	event->object = object;
	event->owner = owner;

	list_add_tail(&event->node, &gameport_event_list);
	wake_up(&gameport_wait);

L
Linus Torvalds 已提交
298 299
out:
	spin_unlock_irqrestore(&gameport_event_lock, flags);
300
	return retval;
L
Linus Torvalds 已提交
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
}

static void gameport_free_event(struct gameport_event *event)
{
	module_put(event->owner);
	kfree(event);
}

static void gameport_remove_duplicate_events(struct gameport_event *event)
{
	struct list_head *node, *next;
	struct gameport_event *e;
	unsigned long flags;

	spin_lock_irqsave(&gameport_event_lock, flags);

	list_for_each_safe(node, next, &gameport_event_list) {
		e = list_entry(node, struct gameport_event, node);
		if (event->object == e->object) {
			/*
			 * If this event is of different type we should not
			 * look further - we only suppress duplicate events
			 * that were sent back-to-back.
			 */
			if (event->type != e->type)
				break;

			list_del_init(node);
			gameport_free_event(e);
		}
	}

	spin_unlock_irqrestore(&gameport_event_lock, flags);
}

static struct gameport_event *gameport_get_event(void)
{
	struct gameport_event *event;
	struct list_head *node;
	unsigned long flags;

	spin_lock_irqsave(&gameport_event_lock, flags);

	if (list_empty(&gameport_event_list)) {
		spin_unlock_irqrestore(&gameport_event_lock, flags);
		return NULL;
	}

	node = gameport_event_list.next;
	event = list_entry(node, struct gameport_event, node);
	list_del_init(node);

	spin_unlock_irqrestore(&gameport_event_lock, flags);

	return event;
}

358
static void gameport_handle_event(void)
L
Linus Torvalds 已提交
359 360 361
{
	struct gameport_event *event;

362
	mutex_lock(&gameport_mutex);
L
Linus Torvalds 已提交
363

364 365 366 367 368 369 370
	/*
	 * Note that we handle only one event here to give swsusp
	 * a chance to freeze kgameportd thread. Gameport events
	 * should be pretty rare so we are not concerned about
	 * taking performance hit.
	 */
	if ((event = gameport_get_event())) {
L
Linus Torvalds 已提交
371 372 373 374 375 376 377

		switch (event->type) {
			case GAMEPORT_REGISTER_PORT:
				gameport_add_port(event->object);
				break;

			case GAMEPORT_REGISTER_DRIVER:
378
				gameport_add_driver(event->object);
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388
				break;

			default:
				break;
		}

		gameport_remove_duplicate_events(event);
		gameport_free_event(event);
	}

389
	mutex_unlock(&gameport_mutex);
L
Linus Torvalds 已提交
390 391 392
}

/*
393 394
 * Remove all events that have been submitted for a given object,
 * be it a gameport port or a driver.
L
Linus Torvalds 已提交
395
 */
396
static void gameport_remove_pending_events(void *object)
L
Linus Torvalds 已提交
397 398 399 400 401 402 403 404 405
{
	struct list_head *node, *next;
	struct gameport_event *event;
	unsigned long flags;

	spin_lock_irqsave(&gameport_event_lock, flags);

	list_for_each_safe(node, next, &gameport_event_list) {
		event = list_entry(node, struct gameport_event, node);
406
		if (event->object == object) {
L
Linus Torvalds 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
			list_del_init(node);
			gameport_free_event(event);
		}
	}

	spin_unlock_irqrestore(&gameport_event_lock, flags);
}

/*
 * Destroy child gameport port (if any) that has not been fully registered yet.
 *
 * Note that we rely on the fact that port can have only one child and therefore
 * only one child registration request can be pending. Additionally, children
 * are registered by driver's connect() handler so there can't be a grandchild
 * pending registration together with a child.
 */
static struct gameport *gameport_get_pending_child(struct gameport *parent)
{
	struct gameport_event *event;
	struct gameport *gameport, *child = NULL;
	unsigned long flags;

	spin_lock_irqsave(&gameport_event_lock, flags);

	list_for_each_entry(event, &gameport_event_list, node) {
		if (event->type == GAMEPORT_REGISTER_PORT) {
			gameport = event->object;
			if (gameport->parent == parent) {
				child = gameport;
				break;
			}
		}
	}

	spin_unlock_irqrestore(&gameport_event_lock, flags);
	return child;
}

static int gameport_thread(void *nothing)
{
447
	set_freezable();
L
Linus Torvalds 已提交
448
	do {
449
		gameport_handle_event();
450
		wait_event_freezable(gameport_wait,
451 452
			kthread_should_stop() || !list_empty(&gameport_event_list));
	} while (!kthread_should_stop());
L
Linus Torvalds 已提交
453 454

	printk(KERN_DEBUG "gameport: kgameportd exiting\n");
455
	return 0;
L
Linus Torvalds 已提交
456 457 458 459 460 461 462
}


/*
 * Gameport port operations
 */

463
static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
464 465 466 467 468
{
	struct gameport *gameport = to_gameport_port(dev);
	return sprintf(buf, "%s\n", gameport->name);
}

469
static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
470 471 472
{
	struct gameport *gameport = to_gameport_port(dev);
	struct device_driver *drv;
473
	int error;
L
Linus Torvalds 已提交
474

475 476 477
	error = mutex_lock_interruptible(&gameport_mutex);
	if (error)
		return error;
L
Linus Torvalds 已提交
478 479 480 481 482 483 484 485 486 487

	if (!strncmp(buf, "none", count)) {
		gameport_disconnect_port(gameport);
	} else if (!strncmp(buf, "reconnect", count)) {
		gameport_reconnect_port(gameport);
	} else if (!strncmp(buf, "rescan", count)) {
		gameport_disconnect_port(gameport);
		gameport_find_driver(gameport);
	} else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
		gameport_disconnect_port(gameport);
488
		error = gameport_bind_driver(gameport, to_gameport_driver(drv));
L
Linus Torvalds 已提交
489 490
		put_driver(drv);
	} else {
491
		error = -EINVAL;
L
Linus Torvalds 已提交
492 493
	}

494
	mutex_unlock(&gameport_mutex);
L
Linus Torvalds 已提交
495

496
	return error ? error : count;
L
Linus Torvalds 已提交
497 498 499 500 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
}

static struct device_attribute gameport_device_attrs[] = {
	__ATTR(description, S_IRUGO, gameport_show_description, NULL),
	__ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
	__ATTR_NULL
};

static void gameport_release_port(struct device *dev)
{
	struct gameport *gameport = to_gameport_port(dev);

	kfree(gameport);
	module_put(THIS_MODULE);
}

void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
	va_end(args);
}

/*
 * Prepare gameport port for registration.
 */
static void gameport_init_port(struct gameport *gameport)
{
	static atomic_t gameport_no = ATOMIC_INIT(0);

	__module_get(THIS_MODULE);

531
	mutex_init(&gameport->drv_mutex);
L
Linus Torvalds 已提交
532 533 534 535 536 537 538 539
	device_initialize(&gameport->dev);
	snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
		 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
	gameport->dev.bus = &gameport_bus;
	gameport->dev.release = gameport_release_port;
	if (gameport->parent)
		gameport->dev.parent = &gameport->parent->dev;

540
	INIT_LIST_HEAD(&gameport->node);
L
Linus Torvalds 已提交
541 542 543 544 545 546 547 548 549 550 551 552
	spin_lock_init(&gameport->timer_lock);
	init_timer(&gameport->poll_timer);
	gameport->poll_timer.function = gameport_run_poll_handler;
	gameport->poll_timer.data = (unsigned long)gameport;
}

/*
 * Complete gameport port registration.
 * Driver core will attempt to find appropriate driver for the port.
 */
static void gameport_add_port(struct gameport *gameport)
{
553 554
	int error;

L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564 565 566 567 568
	if (gameport->parent)
		gameport->parent->child = gameport;

	gameport->speed = gameport_measure_speed(gameport);

	list_add_tail(&gameport->node, &gameport_list);

	if (gameport->io)
		printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
			gameport->name, gameport->phys, gameport->io, gameport->speed);
	else
		printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
			gameport->name, gameport->phys, gameport->speed);

569 570 571 572 573 574 575
	error = device_add(&gameport->dev);
	if (error)
		printk(KERN_ERR
			"gameport: device_add() failed for %s (%s), error: %d\n",
			gameport->phys, gameport->name, error);
	else
		gameport->registered = 1;
L
Linus Torvalds 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
}

/*
 * gameport_destroy_port() completes deregistration process and removes
 * port from the system
 */
static void gameport_destroy_port(struct gameport *gameport)
{
	struct gameport *child;

	child = gameport_get_pending_child(gameport);
	if (child) {
		gameport_remove_pending_events(child);
		put_device(&child->dev);
	}

	if (gameport->parent) {
		gameport->parent->child = NULL;
		gameport->parent = NULL;
	}

	if (gameport->registered) {
		device_del(&gameport->dev);
		gameport->registered = 0;
	}

602 603
	list_del_init(&gameport->node);

L
Linus Torvalds 已提交
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	gameport_remove_pending_events(gameport);
	put_device(&gameport->dev);
}

/*
 * Reconnect gameport port and all its children (re-initialize attached devices)
 */
static void gameport_reconnect_port(struct gameport *gameport)
{
	do {
		if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
			gameport_disconnect_port(gameport);
			gameport_find_driver(gameport);
			/* Ok, old children are now gone, we are done */
			break;
		}
		gameport = gameport->child;
	} while (gameport);
}

/*
 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
 * all child ports are unbound and destroyed.
 */
static void gameport_disconnect_port(struct gameport *gameport)
{
	struct gameport *s, *parent;

	if (gameport->child) {
		/*
		 * Children ports should be disconnected and destroyed
		 * first, staring with the leaf one, since we don't want
		 * to do recursion
		 */
		for (s = gameport; s->child; s = s->child)
			/* empty */;

		do {
			parent = s->parent;

644
			device_release_driver(&s->dev);
L
Linus Torvalds 已提交
645 646 647 648 649 650 651
			gameport_destroy_port(s);
		} while ((s = parent) != gameport);
	}

	/*
	 * Ok, no children left, now disconnect this port
	 */
652
	device_release_driver(&gameport->dev);
L
Linus Torvalds 已提交
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
}

/*
 * Submits register request to kgameportd for subsequent execution.
 * Note that port registration is always asynchronous.
 */
void __gameport_register_port(struct gameport *gameport, struct module *owner)
{
	gameport_init_port(gameport);
	gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
}

/*
 * Synchronously unregisters gameport port.
 */
void gameport_unregister_port(struct gameport *gameport)
{
670
	mutex_lock(&gameport_mutex);
L
Linus Torvalds 已提交
671 672
	gameport_disconnect_port(gameport);
	gameport_destroy_port(gameport);
673
	mutex_unlock(&gameport_mutex);
L
Linus Torvalds 已提交
674 675 676 677 678 679 680 681 682 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
}


/*
 * Gameport driver operations
 */

static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
{
	struct gameport_driver *driver = to_gameport_driver(drv);
	return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
}

static struct driver_attribute gameport_driver_attrs[] = {
	__ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
	__ATTR_NULL
};

static int gameport_driver_probe(struct device *dev)
{
	struct gameport *gameport = to_gameport_port(dev);
	struct gameport_driver *drv = to_gameport_driver(dev->driver);

	drv->connect(gameport, drv);
	return gameport->drv ? 0 : -ENODEV;
}

static int gameport_driver_remove(struct device *dev)
{
	struct gameport *gameport = to_gameport_port(dev);
	struct gameport_driver *drv = to_gameport_driver(dev->driver);

	drv->disconnect(gameport);
	return 0;
}

710 711 712 713 714 715 716 717 718 719 720
static void gameport_add_driver(struct gameport_driver *drv)
{
	int error;

	error = driver_register(&drv->driver);
	if (error)
		printk(KERN_ERR
			"gameport: driver_register() failed for %s, error: %d\n",
			drv->driver.name, error);
}

721 722
int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
				const char *mod_name)
L
Linus Torvalds 已提交
723
{
724 725
	int error;

L
Linus Torvalds 已提交
726
	drv->driver.bus = &gameport_bus;
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
	drv->driver.owner = owner;
	drv->driver.mod_name = mod_name;

	/*
	 * Temporarily disable automatic binding because probing
	 * takes long time and we are better off doing it in kgameportd
	 */
	drv->ignore = 1;

	error = driver_register(&drv->driver);
	if (error) {
		printk(KERN_ERR
			"gameport: driver_register() failed for %s, error: %d\n",
			drv->driver.name, error);
		return error;
	}

	/*
	 * Reset ignore flag and let kgameportd bind the driver to free ports
	 */
	drv->ignore = 0;
	error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
	if (error) {
		driver_unregister(&drv->driver);
		return error;
	}

	return 0;
L
Linus Torvalds 已提交
755 756 757 758 759 760
}

void gameport_unregister_driver(struct gameport_driver *drv)
{
	struct gameport *gameport;

761
	mutex_lock(&gameport_mutex);
762

L
Linus Torvalds 已提交
763
	drv->ignore = 1;	/* so gameport_find_driver ignores it */
764
	gameport_remove_pending_events(drv);
L
Linus Torvalds 已提交
765 766 767 768 769 770 771 772 773 774 775 776

start_over:
	list_for_each_entry(gameport, &gameport_list, node) {
		if (gameport->drv == drv) {
			gameport_disconnect_port(gameport);
			gameport_find_driver(gameport);
			/* we could've deleted some ports, restart */
			goto start_over;
		}
	}

	driver_unregister(&drv->driver);
777

778
	mutex_unlock(&gameport_mutex);
L
Linus Torvalds 已提交
779 780 781 782 783 784 785 786 787
}

static int gameport_bus_match(struct device *dev, struct device_driver *drv)
{
	struct gameport_driver *gameport_drv = to_gameport_driver(drv);

	return !gameport_drv->ignore;
}

788 789 790 791 792 793 794 795 796
static struct bus_type gameport_bus = {
	.name		= "gameport",
	.dev_attrs	= gameport_device_attrs,
	.drv_attrs	= gameport_driver_attrs,
	.match		= gameport_bus_match,
	.probe		= gameport_driver_probe,
	.remove		= gameport_driver_remove,
};

L
Linus Torvalds 已提交
797 798
static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
{
799
	mutex_lock(&gameport->drv_mutex);
L
Linus Torvalds 已提交
800
	gameport->drv = drv;
801
	mutex_unlock(&gameport->drv_mutex);
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
}

int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
{
	if (gameport->open) {
		if (gameport->open(gameport, mode)) {
			return -1;
		}
	} else {
		if (mode != GAMEPORT_MODE_RAW)
			return -1;
	}

	gameport_set_drv(gameport, drv);
	return 0;
}

void gameport_close(struct gameport *gameport)
{
	del_timer_sync(&gameport->poll_timer);
	gameport->poll_handler = NULL;
	gameport->poll_interval = 0;
	gameport_set_drv(gameport, NULL);
	if (gameport->close)
		gameport->close(gameport);
}

static int __init gameport_init(void)
{
831
	int error;
L
Linus Torvalds 已提交
832

833 834 835 836 837 838 839 840 841 842 843 844 845
	error = bus_register(&gameport_bus);
	if (error) {
		printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
		return error;
	}

	gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
	if (IS_ERR(gameport_task)) {
		bus_unregister(&gameport_bus);
		error = PTR_ERR(gameport_task);
		printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
		return error;
	}
L
Linus Torvalds 已提交
846 847 848 849 850 851 852

	return 0;
}

static void __exit gameport_exit(void)
{
	bus_unregister(&gameport_bus);
853
	kthread_stop(gameport_task);
L
Linus Torvalds 已提交
854 855
}

856
subsys_initcall(gameport_init);
L
Linus Torvalds 已提交
857
module_exit(gameport_exit);