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