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