Input: atkbd - cancel delayed work before freeing its structure
[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_driver(struct gameport_driver *drv);
54 static void gameport_add_port(struct gameport *gameport);
55 static void gameport_destroy_port(struct gameport *gameport);
56 static void gameport_reconnect_port(struct gameport *gameport);
57 static void gameport_disconnect_port(struct gameport *gameport);
58
59 #if defined(__i386__)
60
61 #include <asm/i8253.h>
62
63 #define DELTA(x,y)      ((y)-(x)+((y)<(x)?1193182/HZ:0))
64 #define GET_TIME(x)     do { x = get_time_pit(); } while (0)
65
66 static unsigned int get_time_pit(void)
67 {
68         unsigned long flags;
69         unsigned int count;
70
71         spin_lock_irqsave(&i8253_lock, flags);
72         outb_p(0x00, 0x43);
73         count = inb_p(0x40);
74         count |= inb_p(0x40) << 8;
75         spin_unlock_irqrestore(&i8253_lock, flags);
76
77         return count;
78 }
79
80 #endif
81
82
83
84 /*
85  * gameport_measure_speed() measures the gameport i/o speed.
86  */
87
88 static int gameport_measure_speed(struct gameport *gameport)
89 {
90 #if defined(__i386__)
91
92         unsigned int i, t, t1, t2, t3, tx;
93         unsigned long flags;
94
95         if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
96                 return 0;
97
98         tx = 1 << 30;
99
100         for(i = 0; i < 50; i++) {
101                 local_irq_save(flags);
102                 GET_TIME(t1);
103                 for (t = 0; t < 50; t++) gameport_read(gameport);
104                 GET_TIME(t2);
105                 GET_TIME(t3);
106                 local_irq_restore(flags);
107                 udelay(i * 10);
108                 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
109         }
110
111         gameport_close(gameport);
112         return 59659 / (tx < 1 ? 1 : tx);
113
114 #elif defined (__x86_64__)
115
116         unsigned int i, t;
117         unsigned long tx, t1, t2, flags;
118
119         if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
120                 return 0;
121
122         tx = 1 << 30;
123
124         for(i = 0; i < 50; i++) {
125                 local_irq_save(flags);
126                 rdtscl(t1);
127                 for (t = 0; t < 50; t++) gameport_read(gameport);
128                 rdtscl(t2);
129                 local_irq_restore(flags);
130                 udelay(i * 10);
131                 if (t2 - t1 < tx) tx = t2 - t1;
132         }
133
134         gameport_close(gameport);
135         return (cpu_data(raw_smp_processor_id()).loops_per_jiffy *
136                 (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 int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
191 {
192         int error;
193
194         gameport->dev.driver = &drv->driver;
195         if (drv->connect(gameport, drv)) {
196                 gameport->dev.driver = NULL;
197                 return -ENODEV;
198         }
199
200         error = device_bind_driver(&gameport->dev);
201         if (error) {
202                 printk(KERN_WARNING
203                         "gameport: device_bind_driver() failed "
204                         "for %s (%s) and %s, error: %d\n",
205                         gameport->phys, gameport->name,
206                         drv->description, error);
207                 drv->disconnect(gameport);
208                 gameport->dev.driver = NULL;
209                 return error;
210         }
211
212         return 0;
213 }
214
215 static void gameport_find_driver(struct gameport *gameport)
216 {
217         int error;
218
219         error = device_attach(&gameport->dev);
220         if (error < 0)
221                 printk(KERN_WARNING
222                         "gameport: device_attach() failed for %s (%s), error: %d\n",
223                         gameport->phys, gameport->name, error);
224 }
225
226
227 /*
228  * Gameport event processing.
229  */
230
231 enum gameport_event_type {
232         GAMEPORT_REGISTER_PORT,
233         GAMEPORT_REGISTER_DRIVER,
234         GAMEPORT_ATTACH_DRIVER,
235 };
236
237 struct gameport_event {
238         enum gameport_event_type type;
239         void *object;
240         struct module *owner;
241         struct list_head node;
242 };
243
244 static DEFINE_SPINLOCK(gameport_event_lock);    /* protects gameport_event_list */
245 static LIST_HEAD(gameport_event_list);
246 static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
247 static struct task_struct *gameport_task;
248
249 static int gameport_queue_event(void *object, struct module *owner,
250                                 enum gameport_event_type event_type)
251 {
252         unsigned long flags;
253         struct gameport_event *event;
254         int retval = 0;
255
256         spin_lock_irqsave(&gameport_event_lock, flags);
257
258         /*
259          * Scan event list for the other events for the same gameport port,
260          * starting with the most recent one. If event is the same we
261          * do not need add new one. If event is of different type we
262          * need to add this event and should not look further because
263          * we need to preseve sequence of distinct events.
264          */
265         list_for_each_entry_reverse(event, &gameport_event_list, node) {
266                 if (event->object == object) {
267                         if (event->type == event_type)
268                                 goto out;
269                         break;
270                 }
271         }
272
273         event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC);
274         if (!event) {
275                 printk(KERN_ERR
276                         "gameport: Not enough memory to queue event %d\n",
277                         event_type);
278                 retval = -ENOMEM;
279                 goto out;
280         }
281
282         if (!try_module_get(owner)) {
283                 printk(KERN_WARNING
284                         "gameport: Can't get module reference, dropping event %d\n",
285                         event_type);
286                 kfree(event);
287                 retval = -EINVAL;
288                 goto out;
289         }
290
291         event->type = event_type;
292         event->object = object;
293         event->owner = owner;
294
295         list_add_tail(&event->node, &gameport_event_list);
296         wake_up(&gameport_wait);
297
298 out:
299         spin_unlock_irqrestore(&gameport_event_lock, flags);
300         return retval;
301 }
302
303 static void gameport_free_event(struct gameport_event *event)
304 {
305         module_put(event->owner);
306         kfree(event);
307 }
308
309 static void gameport_remove_duplicate_events(struct gameport_event *event)
310 {
311         struct list_head *node, *next;
312         struct gameport_event *e;
313         unsigned long flags;
314
315         spin_lock_irqsave(&gameport_event_lock, flags);
316
317         list_for_each_safe(node, next, &gameport_event_list) {
318                 e = list_entry(node, struct gameport_event, node);
319                 if (event->object == e->object) {
320                         /*
321                          * If this event is of different type we should not
322                          * look further - we only suppress duplicate events
323                          * that were sent back-to-back.
324                          */
325                         if (event->type != e->type)
326                                 break;
327
328                         list_del_init(node);
329                         gameport_free_event(e);
330                 }
331         }
332
333         spin_unlock_irqrestore(&gameport_event_lock, flags);
334 }
335
336 static struct gameport_event *gameport_get_event(void)
337 {
338         struct gameport_event *event;
339         struct list_head *node;
340         unsigned long flags;
341
342         spin_lock_irqsave(&gameport_event_lock, flags);
343
344         if (list_empty(&gameport_event_list)) {
345                 spin_unlock_irqrestore(&gameport_event_lock, flags);
346                 return NULL;
347         }
348
349         node = gameport_event_list.next;
350         event = list_entry(node, struct gameport_event, node);
351         list_del_init(node);
352
353         spin_unlock_irqrestore(&gameport_event_lock, flags);
354
355         return event;
356 }
357
358 static void gameport_handle_event(void)
359 {
360         struct gameport_event *event;
361
362         mutex_lock(&gameport_mutex);
363
364         /*
365          * Note that we handle only one event here to give swsusp
366          * a chance to freeze kgameportd thread. Gameport events
367          * should be pretty rare so we are not concerned about
368          * taking performance hit.
369          */
370         if ((event = gameport_get_event())) {
371
372                 switch (event->type) {
373                         case GAMEPORT_REGISTER_PORT:
374                                 gameport_add_port(event->object);
375                                 break;
376
377                         case GAMEPORT_REGISTER_DRIVER:
378                                 gameport_add_driver(event->object);
379                                 break;
380
381                         default:
382                                 break;
383                 }
384
385                 gameport_remove_duplicate_events(event);
386                 gameport_free_event(event);
387         }
388
389         mutex_unlock(&gameport_mutex);
390 }
391
392 /*
393  * Remove all events that have been submitted for a given object,
394  * be it a gameport port or a driver.
395  */
396 static void gameport_remove_pending_events(void *object)
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 == object) {
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 /*
656  * Submits register request to kgameportd for subsequent execution.
657  * Note that port registration is always asynchronous.
658  */
659 void __gameport_register_port(struct gameport *gameport, struct module *owner)
660 {
661         gameport_init_port(gameport);
662         gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
663 }
664
665 /*
666  * Synchronously unregisters gameport port.
667  */
668 void gameport_unregister_port(struct gameport *gameport)
669 {
670         mutex_lock(&gameport_mutex);
671         gameport_disconnect_port(gameport);
672         gameport_destroy_port(gameport);
673         mutex_unlock(&gameport_mutex);
674 }
675
676
677 /*
678  * Gameport driver operations
679  */
680
681 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
682 {
683         struct gameport_driver *driver = to_gameport_driver(drv);
684         return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
685 }
686
687 static struct driver_attribute gameport_driver_attrs[] = {
688         __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
689         __ATTR_NULL
690 };
691
692 static int gameport_driver_probe(struct device *dev)
693 {
694         struct gameport *gameport = to_gameport_port(dev);
695         struct gameport_driver *drv = to_gameport_driver(dev->driver);
696
697         drv->connect(gameport, drv);
698         return gameport->drv ? 0 : -ENODEV;
699 }
700
701 static int gameport_driver_remove(struct device *dev)
702 {
703         struct gameport *gameport = to_gameport_port(dev);
704         struct gameport_driver *drv = to_gameport_driver(dev->driver);
705
706         drv->disconnect(gameport);
707         return 0;
708 }
709
710 static void gameport_add_driver(struct gameport_driver *drv)
711 {
712         int error;
713
714         error = driver_register(&drv->driver);
715         if (error)
716                 printk(KERN_ERR
717                         "gameport: driver_register() failed for %s, error: %d\n",
718                         drv->driver.name, error);
719 }
720
721 int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
722                                 const char *mod_name)
723 {
724         int error;
725
726         drv->driver.bus = &gameport_bus;
727         drv->driver.owner = owner;
728         drv->driver.mod_name = mod_name;
729
730         /*
731          * Temporarily disable automatic binding because probing
732          * takes long time and we are better off doing it in kgameportd
733          */
734         drv->ignore = 1;
735
736         error = driver_register(&drv->driver);
737         if (error) {
738                 printk(KERN_ERR
739                         "gameport: driver_register() failed for %s, error: %d\n",
740                         drv->driver.name, error);
741                 return error;
742         }
743
744         /*
745          * Reset ignore flag and let kgameportd bind the driver to free ports
746          */
747         drv->ignore = 0;
748         error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
749         if (error) {
750                 driver_unregister(&drv->driver);
751                 return error;
752         }
753
754         return 0;
755 }
756
757 void gameport_unregister_driver(struct gameport_driver *drv)
758 {
759         struct gameport *gameport;
760
761         mutex_lock(&gameport_mutex);
762
763         drv->ignore = 1;        /* so gameport_find_driver ignores it */
764         gameport_remove_pending_events(drv);
765
766 start_over:
767         list_for_each_entry(gameport, &gameport_list, node) {
768                 if (gameport->drv == drv) {
769                         gameport_disconnect_port(gameport);
770                         gameport_find_driver(gameport);
771                         /* we could've deleted some ports, restart */
772                         goto start_over;
773                 }
774         }
775
776         driver_unregister(&drv->driver);
777
778         mutex_unlock(&gameport_mutex);
779 }
780
781 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
782 {
783         struct gameport_driver *gameport_drv = to_gameport_driver(drv);
784
785         return !gameport_drv->ignore;
786 }
787
788 static struct bus_type gameport_bus = {
789         .name           = "gameport",
790         .dev_attrs      = gameport_device_attrs,
791         .drv_attrs      = gameport_driver_attrs,
792         .match          = gameport_bus_match,
793         .probe          = gameport_driver_probe,
794         .remove         = gameport_driver_remove,
795 };
796
797 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
798 {
799         mutex_lock(&gameport->drv_mutex);
800         gameport->drv = drv;
801         mutex_unlock(&gameport->drv_mutex);
802 }
803
804 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
805 {
806         if (gameport->open) {
807                 if (gameport->open(gameport, mode)) {
808                         return -1;
809                 }
810         } else {
811                 if (mode != GAMEPORT_MODE_RAW)
812                         return -1;
813         }
814
815         gameport_set_drv(gameport, drv);
816         return 0;
817 }
818
819 void gameport_close(struct gameport *gameport)
820 {
821         del_timer_sync(&gameport->poll_timer);
822         gameport->poll_handler = NULL;
823         gameport->poll_interval = 0;
824         gameport_set_drv(gameport, NULL);
825         if (gameport->close)
826                 gameport->close(gameport);
827 }
828
829 static int __init gameport_init(void)
830 {
831         int error;
832
833         error = bus_register(&gameport_bus);
834         if (error) {
835                 printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
836                 return error;
837         }
838
839         gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
840         if (IS_ERR(gameport_task)) {
841                 bus_unregister(&gameport_bus);
842                 error = PTR_ERR(gameport_task);
843                 printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
844                 return error;
845         }
846
847         return 0;
848 }
849
850 static void __exit gameport_exit(void)
851 {
852         bus_unregister(&gameport_bus);
853         kthread_stop(gameport_task);
854 }
855
856 subsys_initcall(gameport_init);
857 module_exit(gameport_exit);