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