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

14 15
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
16 17 18 19 20 21 22
#include <linux/stddef.h>
#include <linux/module.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/gameport.h>
#include <linux/slab.h>
#include <linux/delay.h>
23
#include <linux/workqueue.h>
T
Tim Schmielau 已提交
24
#include <linux/sched.h>	/* HZ */
25
#include <linux/mutex.h>
26
#include <linux/timekeeping.h>
L
Linus Torvalds 已提交
27 28 29 30 31 32 33

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

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

34 35 36 37
static bool use_ktime = true;
module_param(use_ktime, bool, 0400);
MODULE_PARM_DESC(use_ktime, "Use ktime for measuring I/O speed");

L
Linus Torvalds 已提交
38
/*
39
 * gameport_mutex protects entire gameport subsystem and is taken
L
Linus Torvalds 已提交
40 41
 * every time gameport port or driver registrered or unregistered.
 */
42
static DEFINE_MUTEX(gameport_mutex);
L
Linus Torvalds 已提交
43 44 45

static LIST_HEAD(gameport_list);

46
static struct bus_type gameport_bus;
L
Linus Torvalds 已提交
47 48

static void gameport_add_port(struct gameport *gameport);
49
static void gameport_attach_driver(struct gameport_driver *drv);
L
Linus Torvalds 已提交
50 51 52 53 54
static void gameport_reconnect_port(struct gameport *gameport);
static void gameport_disconnect_port(struct gameport *gameport);

#if defined(__i386__)

55
#include <linux/i8253.h>
56

L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64
#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;

65
	raw_spin_lock_irqsave(&i8253_lock, flags);
L
Linus Torvalds 已提交
66 67 68
	outb_p(0x00, 0x43);
	count = inb_p(0x40);
	count |= inb_p(0x40) << 8;
69
	raw_spin_unlock_irqrestore(&i8253_lock, flags);
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82

	return count;
}

#endif



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

static int gameport_measure_speed(struct gameport *gameport)
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
{
	unsigned int i, t, tx;
	u64 t1, t2, t3;
	unsigned long flags;

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

	tx = ~0;

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

	gameport_close(gameport);
	t = 1000000 * 50;
	if (tx)
		t /= tx;
	return t;
}

static int old_gameport_measure_speed(struct gameport *gameport)
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
{
#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);
161
	return (this_cpu_read(cpu_info.loops_per_jiffy) *
162
		(unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

#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);
}
192
EXPORT_SYMBOL(gameport_start_polling);
L
Linus Torvalds 已提交
193 194 195 196 197 198 199 200 201 202

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);
}
203
EXPORT_SYMBOL(gameport_stop_polling);
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217

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
 */

218
static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
L
Linus Torvalds 已提交
219
{
220 221
	int error;

L
Linus Torvalds 已提交
222 223 224
	gameport->dev.driver = &drv->driver;
	if (drv->connect(gameport, drv)) {
		gameport->dev.driver = NULL;
225
		return -ENODEV;
L
Linus Torvalds 已提交
226
	}
227 228 229

	error = device_bind_driver(&gameport->dev);
	if (error) {
230 231
		dev_warn(&gameport->dev,
			 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
232 233 234 235
			gameport->phys, gameport->name,
			drv->description, error);
		drv->disconnect(gameport);
		gameport->dev.driver = NULL;
236
		return error;
237 238
	}

239
	return 0;
L
Linus Torvalds 已提交
240 241 242 243
}

static void gameport_find_driver(struct gameport *gameport)
{
244 245 246 247
	int error;

	error = device_attach(&gameport->dev);
	if (error < 0)
248 249 250
		dev_warn(&gameport->dev,
			 "device_attach() failed for %s (%s), error: %d\n",
			 gameport->phys, gameport->name, error);
L
Linus Torvalds 已提交
251 252 253 254 255 256 257 258 259
}


/*
 * Gameport event processing.
 */

enum gameport_event_type {
	GAMEPORT_REGISTER_PORT,
260
	GAMEPORT_ATTACH_DRIVER,
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272
};

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

273
static struct gameport_event *gameport_get_event(void)
L
Linus Torvalds 已提交
274
{
275
	struct gameport_event *event = NULL;
L
Linus Torvalds 已提交
276 277 278 279
	unsigned long flags;

	spin_lock_irqsave(&gameport_event_lock, flags);

280 281 282 283
	if (!list_empty(&gameport_event_list)) {
		event = list_first_entry(&gameport_event_list,
					 struct gameport_event, node);
		list_del_init(&event->node);
L
Linus Torvalds 已提交
284
	}
285

L
Linus Torvalds 已提交
286
	spin_unlock_irqrestore(&gameport_event_lock, flags);
287
	return event;
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296 297
}

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)
{
298
	struct gameport_event *e, *next;
L
Linus Torvalds 已提交
299 300 301 302
	unsigned long flags;

	spin_lock_irqsave(&gameport_event_lock, flags);

303
	list_for_each_entry_safe(e, next, &gameport_event_list, node) {
L
Linus Torvalds 已提交
304 305 306 307 308 309 310 311 312
		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;

313
			list_del_init(&e->node);
L
Linus Torvalds 已提交
314 315 316 317 318 319 320 321
			gameport_free_event(e);
		}
	}

	spin_unlock_irqrestore(&gameport_event_lock, flags);
}


322
static void gameport_handle_events(struct work_struct *work)
L
Linus Torvalds 已提交
323 324 325
{
	struct gameport_event *event;

326
	mutex_lock(&gameport_mutex);
L
Linus Torvalds 已提交
327

328 329 330 331 332 333 334
	/*
	 * 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 已提交
335 336 337

		switch (event->type) {

338 339 340
		case GAMEPORT_REGISTER_PORT:
			gameport_add_port(event->object);
			break;
L
Linus Torvalds 已提交
341

342 343 344
		case GAMEPORT_ATTACH_DRIVER:
			gameport_attach_driver(event->object);
			break;
L
Linus Torvalds 已提交
345 346 347 348 349 350
		}

		gameport_remove_duplicate_events(event);
		gameport_free_event(event);
	}

351
	mutex_unlock(&gameport_mutex);
L
Linus Torvalds 已提交
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 388 389 390 391 392 393 394 395 396 397 398 399
static DECLARE_WORK(gameport_event_work, gameport_handle_events);

static int gameport_queue_event(void *object, struct module *owner,
				enum gameport_event_type event_type)
{
	unsigned long flags;
	struct gameport_event *event;
	int retval = 0;

	spin_lock_irqsave(&gameport_event_lock, flags);

	/*
	 * Scan event list for the other events for the same gameport port,
	 * 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 preserve sequence of distinct events.
	 */
	list_for_each_entry_reverse(event, &gameport_event_list, node) {
		if (event->object == object) {
			if (event->type == event_type)
				goto out;
			break;
		}
	}

	event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC);
	if (!event) {
		pr_err("Not enough memory to queue event %d\n", event_type);
		retval = -ENOMEM;
		goto out;
	}

	if (!try_module_get(owner)) {
		pr_warning("Can't get module reference, dropping event %d\n",
			   event_type);
		kfree(event);
		retval = -EINVAL;
		goto out;
	}

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

	list_add_tail(&event->node, &gameport_event_list);
400
	queue_work(system_long_wq, &gameport_event_work);
401 402 403 404 405 406

out:
	spin_unlock_irqrestore(&gameport_event_lock, flags);
	return retval;
}

L
Linus Torvalds 已提交
407
/*
408 409
 * Remove all events that have been submitted for a given object,
 * be it a gameport port or a driver.
L
Linus Torvalds 已提交
410
 */
411
static void gameport_remove_pending_events(void *object)
L
Linus Torvalds 已提交
412
{
413
	struct gameport_event *event, *next;
L
Linus Torvalds 已提交
414 415 416 417
	unsigned long flags;

	spin_lock_irqsave(&gameport_event_lock, flags);

418
	list_for_each_entry_safe(event, next, &gameport_event_list, node) {
419
		if (event->object == object) {
420
			list_del_init(&event->node);
L
Linus Torvalds 已提交
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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
			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;
}

/*
 * Gameport port operations
 */

462
static ssize_t gameport_description_show(struct device *dev, struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
463 464
{
	struct gameport *gameport = to_gameport_port(dev);
465

L
Linus Torvalds 已提交
466 467
	return sprintf(buf, "%s\n", gameport->name);
}
468
static DEVICE_ATTR(description, S_IRUGO, gameport_description_show, NULL);
L
Linus Torvalds 已提交
469

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

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

	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);
489
		error = gameport_bind_driver(gameport, to_gameport_driver(drv));
L
Linus Torvalds 已提交
490
	} 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
static DEVICE_ATTR_WO(drvctl);
L
Linus Torvalds 已提交
499

500 501 502 503
static struct attribute *gameport_device_attrs[] = {
	&dev_attr_description.attr,
	&dev_attr_drvctl.attr,
	NULL,
L
Linus Torvalds 已提交
504
};
505
ATTRIBUTE_GROUPS(gameport_device);
L
Linus Torvalds 已提交
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

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);
}
523
EXPORT_SYMBOL(gameport_set_phys);
L
Linus Torvalds 已提交
524 525 526 527 528 529

/*
 * Prepare gameport port for registration.
 */
static void gameport_init_port(struct gameport *gameport)
{
530
	static atomic_t gameport_no = ATOMIC_INIT(-1);
L
Linus Torvalds 已提交
531 532 533

	__module_get(THIS_MODULE);

534
	mutex_init(&gameport->drv_mutex);
L
Linus Torvalds 已提交
535
	device_initialize(&gameport->dev);
536
	dev_set_name(&gameport->dev, "gameport%lu",
537
			(unsigned long)atomic_inc_return(&gameport_no));
L
Linus Torvalds 已提交
538 539 540 541 542
	gameport->dev.bus = &gameport_bus;
	gameport->dev.release = gameport_release_port;
	if (gameport->parent)
		gameport->dev.parent = &gameport->parent->dev;

543
	INIT_LIST_HEAD(&gameport->node);
L
Linus Torvalds 已提交
544 545 546 547 548 549 550 551 552 553 554 555
	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)
{
556 557
	int error;

L
Linus Torvalds 已提交
558 559 560
	if (gameport->parent)
		gameport->parent->child = gameport;

561 562 563
	gameport->speed = use_ktime ?
		gameport_measure_speed(gameport) :
		old_gameport_measure_speed(gameport);
L
Linus Torvalds 已提交
564 565 566 567

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

	if (gameport->io)
568 569
		dev_info(&gameport->dev, "%s is %s, io %#x, speed %dkHz\n",
			 gameport->name, gameport->phys, gameport->io, gameport->speed);
L
Linus Torvalds 已提交
570
	else
571
		dev_info(&gameport->dev, "%s is %s, speed %dkHz\n",
L
Linus Torvalds 已提交
572 573
			gameport->name, gameport->phys, gameport->speed);

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

/*
 * 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;
	}

600
	if (device_is_registered(&gameport->dev))
L
Linus Torvalds 已提交
601 602
		device_del(&gameport->dev);

603 604
	list_del_init(&gameport->node);

L
Linus Torvalds 已提交
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 644
	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;

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

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

/*
 * 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);
}
665
EXPORT_SYMBOL(__gameport_register_port);
L
Linus Torvalds 已提交
666 667 668 669 670 671

/*
 * Synchronously unregisters gameport port.
 */
void gameport_unregister_port(struct gameport *gameport)
{
672
	mutex_lock(&gameport_mutex);
L
Linus Torvalds 已提交
673 674
	gameport_disconnect_port(gameport);
	gameport_destroy_port(gameport);
675
	mutex_unlock(&gameport_mutex);
L
Linus Torvalds 已提交
676
}
677
EXPORT_SYMBOL(gameport_unregister_port);
L
Linus Torvalds 已提交
678 679 680 681 682 683


/*
 * Gameport driver operations
 */

684
static ssize_t description_show(struct device_driver *drv, char *buf)
L
Linus Torvalds 已提交
685 686 687 688
{
	struct gameport_driver *driver = to_gameport_driver(drv);
	return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
}
689
static DRIVER_ATTR_RO(description);
L
Linus Torvalds 已提交
690

691 692 693
static struct attribute *gameport_driver_attrs[] = {
	&driver_attr_description.attr,
	NULL
L
Linus Torvalds 已提交
694
};
695
ATTRIBUTE_GROUPS(gameport_driver);
L
Linus Torvalds 已提交
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714

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

715
static void gameport_attach_driver(struct gameport_driver *drv)
716 717 718
{
	int error;

719
	error = driver_attach(&drv->driver);
720
	if (error)
721
		pr_err("driver_attach() failed for %s, error: %d\n",
722 723 724
			drv->driver.name, error);
}

725 726
int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
				const char *mod_name)
L
Linus Torvalds 已提交
727
{
728 729
	int error;

L
Linus Torvalds 已提交
730
	drv->driver.bus = &gameport_bus;
731 732 733 734 735 736 737
	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
	 */
738
	drv->ignore = true;
739 740 741

	error = driver_register(&drv->driver);
	if (error) {
742
		pr_err("driver_register() failed for %s, error: %d\n",
743 744 745 746 747 748 749
			drv->driver.name, error);
		return error;
	}

	/*
	 * Reset ignore flag and let kgameportd bind the driver to free ports
	 */
750
	drv->ignore = false;
751 752 753 754 755 756 757
	error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
	if (error) {
		driver_unregister(&drv->driver);
		return error;
	}

	return 0;
L
Linus Torvalds 已提交
758
}
759
EXPORT_SYMBOL(__gameport_register_driver);
L
Linus Torvalds 已提交
760 761 762 763 764

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

765
	mutex_lock(&gameport_mutex);
766

767
	drv->ignore = true;	/* so gameport_find_driver ignores it */
768
	gameport_remove_pending_events(drv);
L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778 779 780

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

782
	mutex_unlock(&gameport_mutex);
L
Linus Torvalds 已提交
783
}
784
EXPORT_SYMBOL(gameport_unregister_driver);
L
Linus Torvalds 已提交
785 786 787 788 789 790 791 792

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

793 794
static struct bus_type gameport_bus = {
	.name		= "gameport",
795
	.dev_groups	= gameport_device_groups,
796
	.drv_groups	= gameport_driver_groups,
797 798 799 800 801
	.match		= gameport_bus_match,
	.probe		= gameport_driver_probe,
	.remove		= gameport_driver_remove,
};

L
Linus Torvalds 已提交
802 803
static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
{
804
	mutex_lock(&gameport->drv_mutex);
L
Linus Torvalds 已提交
805
	gameport->drv = drv;
806
	mutex_unlock(&gameport->drv_mutex);
L
Linus Torvalds 已提交
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
}

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;
}
823
EXPORT_SYMBOL(gameport_open);
L
Linus Torvalds 已提交
824 825 826 827 828 829 830 831 832 833

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);
}
834
EXPORT_SYMBOL(gameport_close);
L
Linus Torvalds 已提交
835 836 837

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

840 841
	error = bus_register(&gameport_bus);
	if (error) {
842
		pr_err("failed to register gameport bus, error: %d\n", error);
843 844 845
		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 854 855 856 857 858

	/*
	 * There should not be any outstanding events but work may
	 * still be scheduled so simply cancel it.
	 */
	cancel_work_sync(&gameport_event_work);
L
Linus Torvalds 已提交
859 860
}

861
subsys_initcall(gameport_init);
L
Linus Torvalds 已提交
862
module_exit(gameport_exit);