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