blob: ec1b6cfefcd3274d9e645e7ee2e944dd0ded0d93 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/slab.h>
22#include <linux/delay.h>
Linus Torvalds3c803e82005-06-27 17:49:45 -070023#include <linux/kthread.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080024#include <linux/sched.h> /* HZ */
Arjan van de Ven286295e2006-02-19 00:22:03 -050025#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*#include <asm/io.h>*/
29
30MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
31MODULE_DESCRIPTION("Generic gameport layer");
32MODULE_LICENSE("GPL");
33
34EXPORT_SYMBOL(__gameport_register_port);
35EXPORT_SYMBOL(gameport_unregister_port);
36EXPORT_SYMBOL(__gameport_register_driver);
37EXPORT_SYMBOL(gameport_unregister_driver);
38EXPORT_SYMBOL(gameport_open);
39EXPORT_SYMBOL(gameport_close);
40EXPORT_SYMBOL(gameport_rescan);
41EXPORT_SYMBOL(gameport_cooked_read);
42EXPORT_SYMBOL(gameport_set_name);
43EXPORT_SYMBOL(gameport_set_phys);
44EXPORT_SYMBOL(gameport_start_polling);
45EXPORT_SYMBOL(gameport_stop_polling);
46
47/*
Arjan van de Ven286295e2006-02-19 00:22:03 -050048 * gameport_mutex protects entire gameport subsystem and is taken
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * every time gameport port or driver registrered or unregistered.
50 */
Arjan van de Ven286295e2006-02-19 00:22:03 -050051static DEFINE_MUTEX(gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static LIST_HEAD(gameport_list);
54
Russell King29a4a202006-01-05 14:38:22 +000055static struct bus_type gameport_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Randy Dunlap73b59a32006-07-19 01:14:55 -040057static void gameport_add_driver(struct gameport_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void gameport_add_port(struct gameport *gameport);
59static void gameport_destroy_port(struct gameport *gameport);
60static void gameport_reconnect_port(struct gameport *gameport);
61static void gameport_disconnect_port(struct gameport *gameport);
62
63#if defined(__i386__)
64
Ingo Molnar306e4402005-06-30 02:58:55 -070065#include <asm/i8253.h>
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
68#define GET_TIME(x) do { x = get_time_pit(); } while (0)
69
70static unsigned int get_time_pit(void)
71{
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 unsigned long flags;
73 unsigned int count;
74
75 spin_lock_irqsave(&i8253_lock, flags);
76 outb_p(0x00, 0x43);
77 count = inb_p(0x40);
78 count |= inb_p(0x40) << 8;
79 spin_unlock_irqrestore(&i8253_lock, flags);
80
81 return count;
82}
83
84#endif
85
86
87
88/*
89 * gameport_measure_speed() measures the gameport i/o speed.
90 */
91
92static int gameport_measure_speed(struct gameport *gameport)
93{
94#if defined(__i386__)
95
96 unsigned int i, t, t1, t2, t3, tx;
97 unsigned long flags;
98
99 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
100 return 0;
101
102 tx = 1 << 30;
103
104 for(i = 0; i < 50; i++) {
105 local_irq_save(flags);
106 GET_TIME(t1);
107 for (t = 0; t < 50; t++) gameport_read(gameport);
108 GET_TIME(t2);
109 GET_TIME(t3);
110 local_irq_restore(flags);
111 udelay(i * 10);
112 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
113 }
114
115 gameport_close(gameport);
116 return 59659 / (tx < 1 ? 1 : tx);
117
118#elif defined (__x86_64__)
119
120 unsigned int i, t;
121 unsigned long tx, t1, t2, flags;
122
123 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
124 return 0;
125
126 tx = 1 << 30;
127
128 for(i = 0; i < 50; i++) {
129 local_irq_save(flags);
130 rdtscl(t1);
131 for (t = 0; t < 50; t++) gameport_read(gameport);
132 rdtscl(t2);
133 local_irq_restore(flags);
134 udelay(i * 10);
135 if (t2 - t1 < tx) tx = t2 - t1;
136 }
137
138 gameport_close(gameport);
Ingo Molnar39c715b2005-06-21 17:14:34 -0700139 return (cpu_data[raw_smp_processor_id()].loops_per_jiffy * (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141#else
142
143 unsigned int j, t = 0;
144
145 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
146 return 0;
147
148 j = jiffies; while (j == jiffies);
149 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
150
151 gameport_close(gameport);
152 return t * HZ / 1000;
153
154#endif
155}
156
157void gameport_start_polling(struct gameport *gameport)
158{
159 spin_lock(&gameport->timer_lock);
160
161 if (!gameport->poll_cnt++) {
162 BUG_ON(!gameport->poll_handler);
163 BUG_ON(!gameport->poll_interval);
164 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
165 }
166
167 spin_unlock(&gameport->timer_lock);
168}
169
170void gameport_stop_polling(struct gameport *gameport)
171{
172 spin_lock(&gameport->timer_lock);
173
174 if (!--gameport->poll_cnt)
175 del_timer(&gameport->poll_timer);
176
177 spin_unlock(&gameport->timer_lock);
178}
179
180static void gameport_run_poll_handler(unsigned long d)
181{
182 struct gameport *gameport = (struct gameport *)d;
183
184 gameport->poll_handler(gameport);
185 if (gameport->poll_cnt)
186 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
187}
188
189/*
190 * Basic gameport -> driver core mappings
191 */
192
Dmitry Torokhov79580052007-04-10 00:40:48 -0400193static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400195 int error;
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 gameport->dev.driver = &drv->driver;
198 if (drv->connect(gameport, drv)) {
199 gameport->dev.driver = NULL;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400200 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400202
203 error = device_bind_driver(&gameport->dev);
204 if (error) {
205 printk(KERN_WARNING
206 "gameport: device_bind_driver() failed "
207 "for %s (%s) and %s, error: %d\n",
208 gameport->phys, gameport->name,
209 drv->description, error);
210 drv->disconnect(gameport);
211 gameport->dev.driver = NULL;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400212 return error;
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400213 }
214
Dmitry Torokhov79580052007-04-10 00:40:48 -0400215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218static void gameport_find_driver(struct gameport *gameport)
219{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400220 int error;
221
Randy Dunlap73b59a32006-07-19 01:14:55 -0400222 error = device_attach(&gameport->dev);
223 if (error < 0)
224 printk(KERN_WARNING
225 "gameport: device_attach() failed for %s (%s), error: %d\n",
226 gameport->phys, gameport->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229
230/*
231 * Gameport event processing.
232 */
233
234enum gameport_event_type {
235 GAMEPORT_RESCAN,
236 GAMEPORT_RECONNECT,
237 GAMEPORT_REGISTER_PORT,
238 GAMEPORT_REGISTER_DRIVER,
239};
240
241struct gameport_event {
242 enum gameport_event_type type;
243 void *object;
244 struct module *owner;
245 struct list_head node;
246};
247
248static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
249static LIST_HEAD(gameport_event_list);
250static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700251static struct task_struct *gameport_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253static void gameport_queue_event(void *object, struct module *owner,
254 enum gameport_event_type event_type)
255{
256 unsigned long flags;
257 struct gameport_event *event;
258
259 spin_lock_irqsave(&gameport_event_lock, flags);
260
261 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700262 * Scan event list for the other events for the same gameport port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * starting with the most recent one. If event is the same we
264 * do not need add new one. If event is of different type we
265 * need to add this event and should not look further because
266 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700267 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 list_for_each_entry_reverse(event, &gameport_event_list, node) {
269 if (event->object == object) {
270 if (event->type == event_type)
271 goto out;
272 break;
273 }
274 }
275
276 if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
277 if (!try_module_get(owner)) {
278 printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
Adrian Bunk642fde12006-03-14 00:13:34 -0500279 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 goto out;
281 }
282
283 event->type = event_type;
284 event->object = object;
285 event->owner = owner;
286
287 list_add_tail(&event->node, &gameport_event_list);
288 wake_up(&gameport_wait);
289 } else {
290 printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
291 }
292out:
293 spin_unlock_irqrestore(&gameport_event_lock, flags);
294}
295
296static void gameport_free_event(struct gameport_event *event)
297{
298 module_put(event->owner);
299 kfree(event);
300}
301
302static void gameport_remove_duplicate_events(struct gameport_event *event)
303{
304 struct list_head *node, *next;
305 struct gameport_event *e;
306 unsigned long flags;
307
308 spin_lock_irqsave(&gameport_event_lock, flags);
309
310 list_for_each_safe(node, next, &gameport_event_list) {
311 e = list_entry(node, struct gameport_event, node);
312 if (event->object == e->object) {
313 /*
314 * If this event is of different type we should not
315 * look further - we only suppress duplicate events
316 * that were sent back-to-back.
317 */
318 if (event->type != e->type)
319 break;
320
321 list_del_init(node);
322 gameport_free_event(e);
323 }
324 }
325
326 spin_unlock_irqrestore(&gameport_event_lock, flags);
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static struct gameport_event *gameport_get_event(void)
330{
331 struct gameport_event *event;
332 struct list_head *node;
333 unsigned long flags;
334
335 spin_lock_irqsave(&gameport_event_lock, flags);
336
337 if (list_empty(&gameport_event_list)) {
338 spin_unlock_irqrestore(&gameport_event_lock, flags);
339 return NULL;
340 }
341
342 node = gameport_event_list.next;
343 event = list_entry(node, struct gameport_event, node);
344 list_del_init(node);
345
346 spin_unlock_irqrestore(&gameport_event_lock, flags);
347
348 return event;
349}
350
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500351static void gameport_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct gameport_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Arjan van de Ven286295e2006-02-19 00:22:03 -0500355 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500357 /*
358 * Note that we handle only one event here to give swsusp
359 * a chance to freeze kgameportd thread. Gameport events
360 * should be pretty rare so we are not concerned about
361 * taking performance hit.
362 */
363 if ((event = gameport_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 switch (event->type) {
366 case GAMEPORT_REGISTER_PORT:
367 gameport_add_port(event->object);
368 break;
369
370 case GAMEPORT_RECONNECT:
371 gameport_reconnect_port(event->object);
372 break;
373
374 case GAMEPORT_RESCAN:
375 gameport_disconnect_port(event->object);
376 gameport_find_driver(event->object);
377 break;
378
379 case GAMEPORT_REGISTER_DRIVER:
Randy Dunlap73b59a32006-07-19 01:14:55 -0400380 gameport_add_driver(event->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
382
383 default:
384 break;
385 }
386
387 gameport_remove_duplicate_events(event);
388 gameport_free_event(event);
389 }
390
Arjan van de Ven286295e2006-02-19 00:22:03 -0500391 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394/*
395 * Remove all events that have been submitted for a given gameport port.
396 */
397static void gameport_remove_pending_events(struct gameport *gameport)
398{
399 struct list_head *node, *next;
400 struct gameport_event *event;
401 unsigned long flags;
402
403 spin_lock_irqsave(&gameport_event_lock, flags);
404
405 list_for_each_safe(node, next, &gameport_event_list) {
406 event = list_entry(node, struct gameport_event, node);
407 if (event->object == gameport) {
408 list_del_init(node);
409 gameport_free_event(event);
410 }
411 }
412
413 spin_unlock_irqrestore(&gameport_event_lock, flags);
414}
415
416/*
417 * Destroy child gameport port (if any) that has not been fully registered yet.
418 *
419 * Note that we rely on the fact that port can have only one child and therefore
420 * only one child registration request can be pending. Additionally, children
421 * are registered by driver's connect() handler so there can't be a grandchild
422 * pending registration together with a child.
423 */
424static struct gameport *gameport_get_pending_child(struct gameport *parent)
425{
426 struct gameport_event *event;
427 struct gameport *gameport, *child = NULL;
428 unsigned long flags;
429
430 spin_lock_irqsave(&gameport_event_lock, flags);
431
432 list_for_each_entry(event, &gameport_event_list, node) {
433 if (event->type == GAMEPORT_REGISTER_PORT) {
434 gameport = event->object;
435 if (gameport->parent == parent) {
436 child = gameport;
437 break;
438 }
439 }
440 }
441
442 spin_unlock_irqrestore(&gameport_event_lock, flags);
443 return child;
444}
445
446static int gameport_thread(void *nothing)
447{
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700448 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500450 gameport_handle_event();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700451 wait_event_freezable(gameport_wait,
Linus Torvalds3c803e82005-06-27 17:49:45 -0700452 kthread_should_stop() || !list_empty(&gameport_event_list));
Linus Torvalds3c803e82005-06-27 17:49:45 -0700453 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457}
458
459
460/*
461 * Gameport port operations
462 */
463
Yani Ioannoue404e272005-05-17 06:42:58 -0400464static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct gameport *gameport = to_gameport_port(dev);
467 return sprintf(buf, "%s\n", gameport->name);
468}
469
Yani Ioannoue404e272005-05-17 06:42:58 -0400470static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct gameport *gameport = to_gameport_port(dev);
473 struct device_driver *drv;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400474 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dmitry Torokhov79580052007-04-10 00:40:48 -0400476 error = mutex_lock_interruptible(&gameport_mutex);
477 if (error)
478 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!strncmp(buf, "none", count)) {
481 gameport_disconnect_port(gameport);
482 } else if (!strncmp(buf, "reconnect", count)) {
483 gameport_reconnect_port(gameport);
484 } else if (!strncmp(buf, "rescan", count)) {
485 gameport_disconnect_port(gameport);
486 gameport_find_driver(gameport);
487 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
488 gameport_disconnect_port(gameport);
Dmitry Torokhov79580052007-04-10 00:40:48 -0400489 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 put_driver(drv);
491 } else {
Dmitry Torokhov79580052007-04-10 00:40:48 -0400492 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494
Arjan van de Ven286295e2006-02-19 00:22:03 -0500495 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Dmitry Torokhov79580052007-04-10 00:40:48 -0400497 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500static struct device_attribute gameport_device_attrs[] = {
501 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
502 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
503 __ATTR_NULL
504};
505
506static void gameport_release_port(struct device *dev)
507{
508 struct gameport *gameport = to_gameport_port(dev);
509
510 kfree(gameport);
511 module_put(THIS_MODULE);
512}
513
514void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
515{
516 va_list args;
517
518 va_start(args, fmt);
519 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
520 va_end(args);
521}
522
523/*
524 * Prepare gameport port for registration.
525 */
526static void gameport_init_port(struct gameport *gameport)
527{
528 static atomic_t gameport_no = ATOMIC_INIT(0);
529
530 __module_get(THIS_MODULE);
531
Arjan van de Ven286295e2006-02-19 00:22:03 -0500532 mutex_init(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 device_initialize(&gameport->dev);
534 snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
535 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
536 gameport->dev.bus = &gameport_bus;
537 gameport->dev.release = gameport_release_port;
538 if (gameport->parent)
539 gameport->dev.parent = &gameport->parent->dev;
540
Randy Dunlap73b59a32006-07-19 01:14:55 -0400541 INIT_LIST_HEAD(&gameport->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 spin_lock_init(&gameport->timer_lock);
543 init_timer(&gameport->poll_timer);
544 gameport->poll_timer.function = gameport_run_poll_handler;
545 gameport->poll_timer.data = (unsigned long)gameport;
546}
547
548/*
549 * Complete gameport port registration.
550 * Driver core will attempt to find appropriate driver for the port.
551 */
552static void gameport_add_port(struct gameport *gameport)
553{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400554 int error;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (gameport->parent)
557 gameport->parent->child = gameport;
558
559 gameport->speed = gameport_measure_speed(gameport);
560
561 list_add_tail(&gameport->node, &gameport_list);
562
563 if (gameport->io)
564 printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
565 gameport->name, gameport->phys, gameport->io, gameport->speed);
566 else
567 printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
568 gameport->name, gameport->phys, gameport->speed);
569
Randy Dunlap73b59a32006-07-19 01:14:55 -0400570 error = device_add(&gameport->dev);
571 if (error)
572 printk(KERN_ERR
573 "gameport: device_add() failed for %s (%s), error: %d\n",
574 gameport->phys, gameport->name, error);
575 else
576 gameport->registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
579/*
580 * gameport_destroy_port() completes deregistration process and removes
581 * port from the system
582 */
583static void gameport_destroy_port(struct gameport *gameport)
584{
585 struct gameport *child;
586
587 child = gameport_get_pending_child(gameport);
588 if (child) {
589 gameport_remove_pending_events(child);
590 put_device(&child->dev);
591 }
592
593 if (gameport->parent) {
594 gameport->parent->child = NULL;
595 gameport->parent = NULL;
596 }
597
598 if (gameport->registered) {
599 device_del(&gameport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 gameport->registered = 0;
601 }
602
Randy Dunlap73b59a32006-07-19 01:14:55 -0400603 list_del_init(&gameport->node);
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 gameport_remove_pending_events(gameport);
606 put_device(&gameport->dev);
607}
608
609/*
610 * Reconnect gameport port and all its children (re-initialize attached devices)
611 */
612static void gameport_reconnect_port(struct gameport *gameport)
613{
614 do {
615 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
616 gameport_disconnect_port(gameport);
617 gameport_find_driver(gameport);
618 /* Ok, old children are now gone, we are done */
619 break;
620 }
621 gameport = gameport->child;
622 } while (gameport);
623}
624
625/*
626 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
627 * all child ports are unbound and destroyed.
628 */
629static void gameport_disconnect_port(struct gameport *gameport)
630{
631 struct gameport *s, *parent;
632
633 if (gameport->child) {
634 /*
635 * Children ports should be disconnected and destroyed
636 * first, staring with the leaf one, since we don't want
637 * to do recursion
638 */
639 for (s = gameport; s->child; s = s->child)
640 /* empty */;
641
642 do {
643 parent = s->parent;
644
Dmitry Torokhov79580052007-04-10 00:40:48 -0400645 device_release_driver(&s->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 gameport_destroy_port(s);
647 } while ((s = parent) != gameport);
648 }
649
650 /*
651 * Ok, no children left, now disconnect this port
652 */
Dmitry Torokhov79580052007-04-10 00:40:48 -0400653 device_release_driver(&gameport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656void gameport_rescan(struct gameport *gameport)
657{
658 gameport_queue_event(gameport, NULL, GAMEPORT_RESCAN);
659}
660
661void gameport_reconnect(struct gameport *gameport)
662{
663 gameport_queue_event(gameport, NULL, GAMEPORT_RECONNECT);
664}
665
666/*
667 * Submits register request to kgameportd for subsequent execution.
668 * Note that port registration is always asynchronous.
669 */
670void __gameport_register_port(struct gameport *gameport, struct module *owner)
671{
672 gameport_init_port(gameport);
673 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
674}
675
676/*
677 * Synchronously unregisters gameport port.
678 */
679void gameport_unregister_port(struct gameport *gameport)
680{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500681 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 gameport_disconnect_port(gameport);
683 gameport_destroy_port(gameport);
Arjan van de Ven286295e2006-02-19 00:22:03 -0500684 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687
688/*
689 * Gameport driver operations
690 */
691
692static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
693{
694 struct gameport_driver *driver = to_gameport_driver(drv);
695 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
696}
697
698static struct driver_attribute gameport_driver_attrs[] = {
699 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
700 __ATTR_NULL
701};
702
703static int gameport_driver_probe(struct device *dev)
704{
705 struct gameport *gameport = to_gameport_port(dev);
706 struct gameport_driver *drv = to_gameport_driver(dev->driver);
707
708 drv->connect(gameport, drv);
709 return gameport->drv ? 0 : -ENODEV;
710}
711
712static int gameport_driver_remove(struct device *dev)
713{
714 struct gameport *gameport = to_gameport_port(dev);
715 struct gameport_driver *drv = to_gameport_driver(dev->driver);
716
717 drv->disconnect(gameport);
718 return 0;
719}
720
Randy Dunlap73b59a32006-07-19 01:14:55 -0400721static void gameport_add_driver(struct gameport_driver *drv)
722{
723 int error;
724
725 error = driver_register(&drv->driver);
726 if (error)
727 printk(KERN_ERR
728 "gameport: driver_register() failed for %s, error: %d\n",
729 drv->driver.name, error);
730}
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
733{
734 drv->driver.bus = &gameport_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
736}
737
738void gameport_unregister_driver(struct gameport_driver *drv)
739{
740 struct gameport *gameport;
741
Arjan van de Ven286295e2006-02-19 00:22:03 -0500742 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 drv->ignore = 1; /* so gameport_find_driver ignores it */
744
745start_over:
746 list_for_each_entry(gameport, &gameport_list, node) {
747 if (gameport->drv == drv) {
748 gameport_disconnect_port(gameport);
749 gameport_find_driver(gameport);
750 /* we could've deleted some ports, restart */
751 goto start_over;
752 }
753 }
754
755 driver_unregister(&drv->driver);
Arjan van de Ven286295e2006-02-19 00:22:03 -0500756 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759static int gameport_bus_match(struct device *dev, struct device_driver *drv)
760{
761 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
762
763 return !gameport_drv->ignore;
764}
765
Dmitry Torokhovb187dd72006-11-02 23:27:30 -0500766static struct bus_type gameport_bus = {
767 .name = "gameport",
768 .dev_attrs = gameport_device_attrs,
769 .drv_attrs = gameport_driver_attrs,
770 .match = gameport_bus_match,
771 .probe = gameport_driver_probe,
772 .remove = gameport_driver_remove,
773};
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
776{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500777 mutex_lock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 gameport->drv = drv;
Arjan van de Ven286295e2006-02-19 00:22:03 -0500779 mutex_unlock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
782int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
783{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (gameport->open) {
785 if (gameport->open(gameport, mode)) {
786 return -1;
787 }
788 } else {
789 if (mode != GAMEPORT_MODE_RAW)
790 return -1;
791 }
792
793 gameport_set_drv(gameport, drv);
794 return 0;
795}
796
797void gameport_close(struct gameport *gameport)
798{
799 del_timer_sync(&gameport->poll_timer);
800 gameport->poll_handler = NULL;
801 gameport->poll_interval = 0;
802 gameport_set_drv(gameport, NULL);
803 if (gameport->close)
804 gameport->close(gameport);
805}
806
807static int __init gameport_init(void)
808{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400809 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Randy Dunlap73b59a32006-07-19 01:14:55 -0400811 error = bus_register(&gameport_bus);
812 if (error) {
813 printk(KERN_ERR "gameport: 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 printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
822 return error;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 return 0;
826}
827
828static void __exit gameport_exit(void)
829{
830 bus_unregister(&gameport_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700831 kthread_stop(gameport_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833
Dmitry Torokhov51c38f92006-02-19 00:22:51 -0500834subsys_initcall(gameport_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835module_exit(gameport_exit);