xref: /linux/drivers/input/gameport/gameport.c (revision de2fe5e07d58424bc286fff3fd3c1b0bf933cd58)
1 /*
2  * Generic gameport layer
3  *
4  * Copyright (c) 1999-2002 Vojtech Pavlik
5  * Copyright (c) 2005 Dmitry Torokhov
6  */
7 
8 /*
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2 as published by
11  * the Free Software Foundation.
12  */
13 
14 #include <linux/stddef.h>
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/gameport.h>
19 #include <linux/wait.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/kthread.h>
24 #include <linux/sched.h>	/* HZ */
25 
26 /*#include <asm/io.h>*/
27 
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29 MODULE_DESCRIPTION("Generic gameport layer");
30 MODULE_LICENSE("GPL");
31 
32 EXPORT_SYMBOL(__gameport_register_port);
33 EXPORT_SYMBOL(gameport_unregister_port);
34 EXPORT_SYMBOL(__gameport_register_driver);
35 EXPORT_SYMBOL(gameport_unregister_driver);
36 EXPORT_SYMBOL(gameport_open);
37 EXPORT_SYMBOL(gameport_close);
38 EXPORT_SYMBOL(gameport_rescan);
39 EXPORT_SYMBOL(gameport_cooked_read);
40 EXPORT_SYMBOL(gameport_set_name);
41 EXPORT_SYMBOL(gameport_set_phys);
42 EXPORT_SYMBOL(gameport_start_polling);
43 EXPORT_SYMBOL(gameport_stop_polling);
44 
45 /*
46  * gameport_sem protects entire gameport subsystem and is taken
47  * every time gameport port or driver registrered or unregistered.
48  */
49 static DECLARE_MUTEX(gameport_sem);
50 
51 static LIST_HEAD(gameport_list);
52 
53 static struct bus_type gameport_bus;
54 
55 static void gameport_add_port(struct gameport *gameport);
56 static void gameport_destroy_port(struct gameport *gameport);
57 static void gameport_reconnect_port(struct gameport *gameport);
58 static void gameport_disconnect_port(struct gameport *gameport);
59 
60 #if defined(__i386__)
61 
62 #include <asm/i8253.h>
63 
64 #define DELTA(x,y)      ((y)-(x)+((y)<(x)?1193182/HZ:0))
65 #define GET_TIME(x)     do { x = get_time_pit(); } while (0)
66 
67 static unsigned int get_time_pit(void)
68 {
69 	unsigned long flags;
70 	unsigned int count;
71 
72 	spin_lock_irqsave(&i8253_lock, flags);
73 	outb_p(0x00, 0x43);
74 	count = inb_p(0x40);
75 	count |= inb_p(0x40) << 8;
76 	spin_unlock_irqrestore(&i8253_lock, flags);
77 
78 	return count;
79 }
80 
81 #endif
82 
83 
84 
85 /*
86  * gameport_measure_speed() measures the gameport i/o speed.
87  */
88 
89 static int gameport_measure_speed(struct gameport *gameport)
90 {
91 #if defined(__i386__)
92 
93 	unsigned int i, t, t1, t2, t3, tx;
94 	unsigned long flags;
95 
96 	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
97 		return 0;
98 
99 	tx = 1 << 30;
100 
101 	for(i = 0; i < 50; i++) {
102 		local_irq_save(flags);
103 		GET_TIME(t1);
104 		for (t = 0; t < 50; t++) gameport_read(gameport);
105 		GET_TIME(t2);
106 		GET_TIME(t3);
107 		local_irq_restore(flags);
108 		udelay(i * 10);
109 		if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
110 	}
111 
112 	gameport_close(gameport);
113 	return 59659 / (tx < 1 ? 1 : tx);
114 
115 #elif defined (__x86_64__)
116 
117 	unsigned int i, t;
118 	unsigned long tx, t1, t2, flags;
119 
120 	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
121 		return 0;
122 
123 	tx = 1 << 30;
124 
125 	for(i = 0; i < 50; i++) {
126 		local_irq_save(flags);
127 		rdtscl(t1);
128 		for (t = 0; t < 50; t++) gameport_read(gameport);
129 		rdtscl(t2);
130 		local_irq_restore(flags);
131 		udelay(i * 10);
132 		if (t2 - t1 < tx) tx = t2 - t1;
133 	}
134 
135 	gameport_close(gameport);
136 	return (cpu_data[raw_smp_processor_id()].loops_per_jiffy * (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
137 
138 #else
139 
140 	unsigned int j, t = 0;
141 
142 	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
143 		return 0;
144 
145 	j = jiffies; while (j == jiffies);
146 	j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
147 
148 	gameport_close(gameport);
149 	return t * HZ / 1000;
150 
151 #endif
152 }
153 
154 void gameport_start_polling(struct gameport *gameport)
155 {
156 	spin_lock(&gameport->timer_lock);
157 
158 	if (!gameport->poll_cnt++) {
159 		BUG_ON(!gameport->poll_handler);
160 		BUG_ON(!gameport->poll_interval);
161 		mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
162 	}
163 
164 	spin_unlock(&gameport->timer_lock);
165 }
166 
167 void gameport_stop_polling(struct gameport *gameport)
168 {
169 	spin_lock(&gameport->timer_lock);
170 
171 	if (!--gameport->poll_cnt)
172 		del_timer(&gameport->poll_timer);
173 
174 	spin_unlock(&gameport->timer_lock);
175 }
176 
177 static void gameport_run_poll_handler(unsigned long d)
178 {
179 	struct gameport *gameport = (struct gameport *)d;
180 
181 	gameport->poll_handler(gameport);
182 	if (gameport->poll_cnt)
183 		mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
184 }
185 
186 /*
187  * Basic gameport -> driver core mappings
188  */
189 
190 static void gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
191 {
192 	down_write(&gameport_bus.subsys.rwsem);
193 
194 	gameport->dev.driver = &drv->driver;
195 	if (drv->connect(gameport, drv)) {
196 		gameport->dev.driver = NULL;
197 		goto out;
198 	}
199 	device_bind_driver(&gameport->dev);
200 out:
201 	up_write(&gameport_bus.subsys.rwsem);
202 }
203 
204 static void gameport_release_driver(struct gameport *gameport)
205 {
206 	down_write(&gameport_bus.subsys.rwsem);
207 	device_release_driver(&gameport->dev);
208 	up_write(&gameport_bus.subsys.rwsem);
209 }
210 
211 static void gameport_find_driver(struct gameport *gameport)
212 {
213 	down_write(&gameport_bus.subsys.rwsem);
214 	device_attach(&gameport->dev);
215 	up_write(&gameport_bus.subsys.rwsem);
216 }
217 
218 
219 /*
220  * Gameport event processing.
221  */
222 
223 enum gameport_event_type {
224 	GAMEPORT_RESCAN,
225 	GAMEPORT_RECONNECT,
226 	GAMEPORT_REGISTER_PORT,
227 	GAMEPORT_REGISTER_DRIVER,
228 };
229 
230 struct gameport_event {
231 	enum gameport_event_type type;
232 	void *object;
233 	struct module *owner;
234 	struct list_head node;
235 };
236 
237 static DEFINE_SPINLOCK(gameport_event_lock);	/* protects gameport_event_list */
238 static LIST_HEAD(gameport_event_list);
239 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
240 static struct task_struct *gameport_task;
241 
242 static void gameport_queue_event(void *object, struct module *owner,
243 			      enum gameport_event_type event_type)
244 {
245 	unsigned long flags;
246 	struct gameport_event *event;
247 
248 	spin_lock_irqsave(&gameport_event_lock, flags);
249 
250 	/*
251 	 * Scan event list for the other events for the same gameport port,
252 	 * starting with the most recent one. If event is the same we
253 	 * do not need add new one. If event is of different type we
254 	 * need to add this event and should not look further because
255 	 * we need to preseve sequence of distinct events.
256 	 */
257 	list_for_each_entry_reverse(event, &gameport_event_list, node) {
258 		if (event->object == object) {
259 			if (event->type == event_type)
260 				goto out;
261 			break;
262 		}
263 	}
264 
265 	if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
266 		if (!try_module_get(owner)) {
267 			printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
268 			goto out;
269 		}
270 
271 		event->type = event_type;
272 		event->object = object;
273 		event->owner = owner;
274 
275 		list_add_tail(&event->node, &gameport_event_list);
276 		wake_up(&gameport_wait);
277 	} else {
278 		printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
279 	}
280 out:
281 	spin_unlock_irqrestore(&gameport_event_lock, flags);
282 }
283 
284 static void gameport_free_event(struct gameport_event *event)
285 {
286 	module_put(event->owner);
287 	kfree(event);
288 }
289 
290 static void gameport_remove_duplicate_events(struct gameport_event *event)
291 {
292 	struct list_head *node, *next;
293 	struct gameport_event *e;
294 	unsigned long flags;
295 
296 	spin_lock_irqsave(&gameport_event_lock, flags);
297 
298 	list_for_each_safe(node, next, &gameport_event_list) {
299 		e = list_entry(node, struct gameport_event, node);
300 		if (event->object == e->object) {
301 			/*
302 			 * If this event is of different type we should not
303 			 * look further - we only suppress duplicate events
304 			 * that were sent back-to-back.
305 			 */
306 			if (event->type != e->type)
307 				break;
308 
309 			list_del_init(node);
310 			gameport_free_event(e);
311 		}
312 	}
313 
314 	spin_unlock_irqrestore(&gameport_event_lock, flags);
315 }
316 
317 
318 static struct gameport_event *gameport_get_event(void)
319 {
320 	struct gameport_event *event;
321 	struct list_head *node;
322 	unsigned long flags;
323 
324 	spin_lock_irqsave(&gameport_event_lock, flags);
325 
326 	if (list_empty(&gameport_event_list)) {
327 		spin_unlock_irqrestore(&gameport_event_lock, flags);
328 		return NULL;
329 	}
330 
331 	node = gameport_event_list.next;
332 	event = list_entry(node, struct gameport_event, node);
333 	list_del_init(node);
334 
335 	spin_unlock_irqrestore(&gameport_event_lock, flags);
336 
337 	return event;
338 }
339 
340 static void gameport_handle_event(void)
341 {
342 	struct gameport_event *event;
343 	struct gameport_driver *gameport_drv;
344 
345 	down(&gameport_sem);
346 
347 	/*
348 	 * Note that we handle only one event here to give swsusp
349 	 * a chance to freeze kgameportd thread. Gameport events
350 	 * should be pretty rare so we are not concerned about
351 	 * taking performance hit.
352 	 */
353 	if ((event = gameport_get_event())) {
354 
355 		switch (event->type) {
356 			case GAMEPORT_REGISTER_PORT:
357 				gameport_add_port(event->object);
358 				break;
359 
360 			case GAMEPORT_RECONNECT:
361 				gameport_reconnect_port(event->object);
362 				break;
363 
364 			case GAMEPORT_RESCAN:
365 				gameport_disconnect_port(event->object);
366 				gameport_find_driver(event->object);
367 				break;
368 
369 			case GAMEPORT_REGISTER_DRIVER:
370 				gameport_drv = event->object;
371 				driver_register(&gameport_drv->driver);
372 				break;
373 
374 			default:
375 				break;
376 		}
377 
378 		gameport_remove_duplicate_events(event);
379 		gameport_free_event(event);
380 	}
381 
382 	up(&gameport_sem);
383 }
384 
385 /*
386  * Remove all events that have been submitted for a given gameport port.
387  */
388 static void gameport_remove_pending_events(struct gameport *gameport)
389 {
390 	struct list_head *node, *next;
391 	struct gameport_event *event;
392 	unsigned long flags;
393 
394 	spin_lock_irqsave(&gameport_event_lock, flags);
395 
396 	list_for_each_safe(node, next, &gameport_event_list) {
397 		event = list_entry(node, struct gameport_event, node);
398 		if (event->object == gameport) {
399 			list_del_init(node);
400 			gameport_free_event(event);
401 		}
402 	}
403 
404 	spin_unlock_irqrestore(&gameport_event_lock, flags);
405 }
406 
407 /*
408  * Destroy child gameport port (if any) that has not been fully registered yet.
409  *
410  * Note that we rely on the fact that port can have only one child and therefore
411  * only one child registration request can be pending. Additionally, children
412  * are registered by driver's connect() handler so there can't be a grandchild
413  * pending registration together with a child.
414  */
415 static struct gameport *gameport_get_pending_child(struct gameport *parent)
416 {
417 	struct gameport_event *event;
418 	struct gameport *gameport, *child = NULL;
419 	unsigned long flags;
420 
421 	spin_lock_irqsave(&gameport_event_lock, flags);
422 
423 	list_for_each_entry(event, &gameport_event_list, node) {
424 		if (event->type == GAMEPORT_REGISTER_PORT) {
425 			gameport = event->object;
426 			if (gameport->parent == parent) {
427 				child = gameport;
428 				break;
429 			}
430 		}
431 	}
432 
433 	spin_unlock_irqrestore(&gameport_event_lock, flags);
434 	return child;
435 }
436 
437 static int gameport_thread(void *nothing)
438 {
439 	do {
440 		gameport_handle_event();
441 		wait_event_interruptible(gameport_wait,
442 			kthread_should_stop() || !list_empty(&gameport_event_list));
443 		try_to_freeze();
444 	} while (!kthread_should_stop());
445 
446 	printk(KERN_DEBUG "gameport: kgameportd exiting\n");
447 	return 0;
448 }
449 
450 
451 /*
452  * Gameport port operations
453  */
454 
455 static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
456 {
457 	struct gameport *gameport = to_gameport_port(dev);
458 	return sprintf(buf, "%s\n", gameport->name);
459 }
460 
461 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
462 {
463 	struct gameport *gameport = to_gameport_port(dev);
464 	struct device_driver *drv;
465 	int retval;
466 
467 	retval = down_interruptible(&gameport_sem);
468 	if (retval)
469 		return retval;
470 
471 	retval = count;
472 	if (!strncmp(buf, "none", count)) {
473 		gameport_disconnect_port(gameport);
474 	} else if (!strncmp(buf, "reconnect", count)) {
475 		gameport_reconnect_port(gameport);
476 	} else if (!strncmp(buf, "rescan", count)) {
477 		gameport_disconnect_port(gameport);
478 		gameport_find_driver(gameport);
479 	} else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
480 		gameport_disconnect_port(gameport);
481 		gameport_bind_driver(gameport, to_gameport_driver(drv));
482 		put_driver(drv);
483 	} else {
484 		retval = -EINVAL;
485 	}
486 
487 	up(&gameport_sem);
488 
489 	return retval;
490 }
491 
492 static struct device_attribute gameport_device_attrs[] = {
493 	__ATTR(description, S_IRUGO, gameport_show_description, NULL),
494 	__ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
495 	__ATTR_NULL
496 };
497 
498 static void gameport_release_port(struct device *dev)
499 {
500 	struct gameport *gameport = to_gameport_port(dev);
501 
502 	kfree(gameport);
503 	module_put(THIS_MODULE);
504 }
505 
506 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
507 {
508 	va_list args;
509 
510 	va_start(args, fmt);
511 	vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
512 	va_end(args);
513 }
514 
515 /*
516  * Prepare gameport port for registration.
517  */
518 static void gameport_init_port(struct gameport *gameport)
519 {
520 	static atomic_t gameport_no = ATOMIC_INIT(0);
521 
522 	__module_get(THIS_MODULE);
523 
524 	init_MUTEX(&gameport->drv_sem);
525 	device_initialize(&gameport->dev);
526 	snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
527 		 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
528 	gameport->dev.bus = &gameport_bus;
529 	gameport->dev.release = gameport_release_port;
530 	if (gameport->parent)
531 		gameport->dev.parent = &gameport->parent->dev;
532 
533 	spin_lock_init(&gameport->timer_lock);
534 	init_timer(&gameport->poll_timer);
535 	gameport->poll_timer.function = gameport_run_poll_handler;
536 	gameport->poll_timer.data = (unsigned long)gameport;
537 }
538 
539 /*
540  * Complete gameport port registration.
541  * Driver core will attempt to find appropriate driver for the port.
542  */
543 static void gameport_add_port(struct gameport *gameport)
544 {
545 	if (gameport->parent)
546 		gameport->parent->child = gameport;
547 
548 	gameport->speed = gameport_measure_speed(gameport);
549 
550 	list_add_tail(&gameport->node, &gameport_list);
551 
552 	if (gameport->io)
553 		printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
554 			gameport->name, gameport->phys, gameport->io, gameport->speed);
555 	else
556 		printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
557 			gameport->name, gameport->phys, gameport->speed);
558 
559 	device_add(&gameport->dev);
560 	gameport->registered = 1;
561 }
562 
563 /*
564  * gameport_destroy_port() completes deregistration process and removes
565  * port from the system
566  */
567 static void gameport_destroy_port(struct gameport *gameport)
568 {
569 	struct gameport *child;
570 
571 	child = gameport_get_pending_child(gameport);
572 	if (child) {
573 		gameport_remove_pending_events(child);
574 		put_device(&child->dev);
575 	}
576 
577 	if (gameport->parent) {
578 		gameport->parent->child = NULL;
579 		gameport->parent = NULL;
580 	}
581 
582 	if (gameport->registered) {
583 		device_del(&gameport->dev);
584 		list_del_init(&gameport->node);
585 		gameport->registered = 0;
586 	}
587 
588 	gameport_remove_pending_events(gameport);
589 	put_device(&gameport->dev);
590 }
591 
592 /*
593  * Reconnect gameport port and all its children (re-initialize attached devices)
594  */
595 static void gameport_reconnect_port(struct gameport *gameport)
596 {
597 	do {
598 		if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
599 			gameport_disconnect_port(gameport);
600 			gameport_find_driver(gameport);
601 			/* Ok, old children are now gone, we are done */
602 			break;
603 		}
604 		gameport = gameport->child;
605 	} while (gameport);
606 }
607 
608 /*
609  * gameport_disconnect_port() unbinds a port from its driver. As a side effect
610  * all child ports are unbound and destroyed.
611  */
612 static void gameport_disconnect_port(struct gameport *gameport)
613 {
614 	struct gameport *s, *parent;
615 
616 	if (gameport->child) {
617 		/*
618 		 * Children ports should be disconnected and destroyed
619 		 * first, staring with the leaf one, since we don't want
620 		 * to do recursion
621 		 */
622 		for (s = gameport; s->child; s = s->child)
623 			/* empty */;
624 
625 		do {
626 			parent = s->parent;
627 
628 			gameport_release_driver(s);
629 			gameport_destroy_port(s);
630 		} while ((s = parent) != gameport);
631 	}
632 
633 	/*
634 	 * Ok, no children left, now disconnect this port
635 	 */
636 	gameport_release_driver(gameport);
637 }
638 
639 void gameport_rescan(struct gameport *gameport)
640 {
641 	gameport_queue_event(gameport, NULL, GAMEPORT_RESCAN);
642 }
643 
644 void gameport_reconnect(struct gameport *gameport)
645 {
646 	gameport_queue_event(gameport, NULL, GAMEPORT_RECONNECT);
647 }
648 
649 /*
650  * Submits register request to kgameportd for subsequent execution.
651  * Note that port registration is always asynchronous.
652  */
653 void __gameport_register_port(struct gameport *gameport, struct module *owner)
654 {
655 	gameport_init_port(gameport);
656 	gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
657 }
658 
659 /*
660  * Synchronously unregisters gameport port.
661  */
662 void gameport_unregister_port(struct gameport *gameport)
663 {
664 	down(&gameport_sem);
665 	gameport_disconnect_port(gameport);
666 	gameport_destroy_port(gameport);
667 	up(&gameport_sem);
668 }
669 
670 
671 /*
672  * Gameport driver operations
673  */
674 
675 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
676 {
677 	struct gameport_driver *driver = to_gameport_driver(drv);
678 	return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
679 }
680 
681 static struct driver_attribute gameport_driver_attrs[] = {
682 	__ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
683 	__ATTR_NULL
684 };
685 
686 static int gameport_driver_probe(struct device *dev)
687 {
688 	struct gameport *gameport = to_gameport_port(dev);
689 	struct gameport_driver *drv = to_gameport_driver(dev->driver);
690 
691 	drv->connect(gameport, drv);
692 	return gameport->drv ? 0 : -ENODEV;
693 }
694 
695 static int gameport_driver_remove(struct device *dev)
696 {
697 	struct gameport *gameport = to_gameport_port(dev);
698 	struct gameport_driver *drv = to_gameport_driver(dev->driver);
699 
700 	drv->disconnect(gameport);
701 	return 0;
702 }
703 
704 static struct bus_type gameport_bus = {
705 	.name =	"gameport",
706 	.probe = gameport_driver_probe,
707 	.remove = gameport_driver_remove,
708 };
709 
710 void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
711 {
712 	drv->driver.bus = &gameport_bus;
713 	gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
714 }
715 
716 void gameport_unregister_driver(struct gameport_driver *drv)
717 {
718 	struct gameport *gameport;
719 
720 	down(&gameport_sem);
721 	drv->ignore = 1;	/* so gameport_find_driver ignores it */
722 
723 start_over:
724 	list_for_each_entry(gameport, &gameport_list, node) {
725 		if (gameport->drv == drv) {
726 			gameport_disconnect_port(gameport);
727 			gameport_find_driver(gameport);
728 			/* we could've deleted some ports, restart */
729 			goto start_over;
730 		}
731 	}
732 
733 	driver_unregister(&drv->driver);
734 	up(&gameport_sem);
735 }
736 
737 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
738 {
739 	struct gameport_driver *gameport_drv = to_gameport_driver(drv);
740 
741 	return !gameport_drv->ignore;
742 }
743 
744 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
745 {
746 	down(&gameport->drv_sem);
747 	gameport->drv = drv;
748 	up(&gameport->drv_sem);
749 }
750 
751 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
752 {
753 
754 	if (gameport->open) {
755 		if (gameport->open(gameport, mode)) {
756 			return -1;
757 		}
758 	} else {
759 		if (mode != GAMEPORT_MODE_RAW)
760 			return -1;
761 	}
762 
763 	gameport_set_drv(gameport, drv);
764 	return 0;
765 }
766 
767 void gameport_close(struct gameport *gameport)
768 {
769 	del_timer_sync(&gameport->poll_timer);
770 	gameport->poll_handler = NULL;
771 	gameport->poll_interval = 0;
772 	gameport_set_drv(gameport, NULL);
773 	if (gameport->close)
774 		gameport->close(gameport);
775 }
776 
777 static int __init gameport_init(void)
778 {
779 	gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
780 	if (IS_ERR(gameport_task)) {
781 		printk(KERN_ERR "gameport: Failed to start kgameportd\n");
782 		return PTR_ERR(gameport_task);
783 	}
784 
785 	gameport_bus.dev_attrs = gameport_device_attrs;
786 	gameport_bus.drv_attrs = gameport_driver_attrs;
787 	gameport_bus.match = gameport_bus_match;
788 	bus_register(&gameport_bus);
789 
790 	return 0;
791 }
792 
793 static void __exit gameport_exit(void)
794 {
795 	bus_unregister(&gameport_bus);
796 	kthread_stop(gameport_task);
797 }
798 
799 module_init(gameport_init);
800 module_exit(gameport_exit);
801