blob: c5600ac5feb3aa6c99dba80628d501d515488ebf [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/slab.h>
21#include <linux/delay.h>
Linus Torvalds3c803e82005-06-27 17:49:45 -070022#include <linux/kthread.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <linux/sched.h> /* HZ */
Arjan van de Ven286295e2006-02-19 00:22:03 -050024#include <linux/mutex.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080025#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*#include <asm/io.h>*/
28
29MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
30MODULE_DESCRIPTION("Generic gameport layer");
31MODULE_LICENSE("GPL");
32
33EXPORT_SYMBOL(__gameport_register_port);
34EXPORT_SYMBOL(gameport_unregister_port);
35EXPORT_SYMBOL(__gameport_register_driver);
36EXPORT_SYMBOL(gameport_unregister_driver);
37EXPORT_SYMBOL(gameport_open);
38EXPORT_SYMBOL(gameport_close);
39EXPORT_SYMBOL(gameport_rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040EXPORT_SYMBOL(gameport_set_phys);
41EXPORT_SYMBOL(gameport_start_polling);
42EXPORT_SYMBOL(gameport_stop_polling);
43
44/*
Arjan van de Ven286295e2006-02-19 00:22:03 -050045 * gameport_mutex protects entire gameport subsystem and is taken
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * every time gameport port or driver registrered or unregistered.
47 */
Arjan van de Ven286295e2006-02-19 00:22:03 -050048static DEFINE_MUTEX(gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static LIST_HEAD(gameport_list);
51
Russell King29a4a202006-01-05 14:38:22 +000052static struct bus_type gameport_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Randy Dunlap73b59a32006-07-19 01:14:55 -040054static void gameport_add_driver(struct gameport_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static void gameport_add_port(struct gameport *gameport);
56static void gameport_destroy_port(struct gameport *gameport);
57static void gameport_reconnect_port(struct gameport *gameport);
58static void gameport_disconnect_port(struct gameport *gameport);
59
60#if defined(__i386__)
61
Ingo Molnar306e4402005-06-30 02:58:55 -070062#include <asm/i8253.h>
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
65#define GET_TIME(x) do { x = get_time_pit(); } while (0)
66
67static unsigned int get_time_pit(void)
68{
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned long flags;
70 unsigned int count;
71
72 spin_lock_irqsave(&i8253_lock, flags);
73 outb_p(0x00, 0x43);
74 count = inb_p(0x40);
75 count |= inb_p(0x40) << 8;
76 spin_unlock_irqrestore(&i8253_lock, flags);
77
78 return count;
79}
80
81#endif
82
83
84
85/*
86 * gameport_measure_speed() measures the gameport i/o speed.
87 */
88
89static int gameport_measure_speed(struct gameport *gameport)
90{
91#if defined(__i386__)
92
93 unsigned int i, t, t1, t2, t3, tx;
94 unsigned long flags;
95
96 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
97 return 0;
98
99 tx = 1 << 30;
100
101 for(i = 0; i < 50; i++) {
102 local_irq_save(flags);
103 GET_TIME(t1);
104 for (t = 0; t < 50; t++) gameport_read(gameport);
105 GET_TIME(t2);
106 GET_TIME(t3);
107 local_irq_restore(flags);
108 udelay(i * 10);
109 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
110 }
111
112 gameport_close(gameport);
113 return 59659 / (tx < 1 ? 1 : tx);
114
115#elif defined (__x86_64__)
116
117 unsigned int i, t;
118 unsigned long tx, t1, t2, flags;
119
120 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
121 return 0;
122
123 tx = 1 << 30;
124
125 for(i = 0; i < 50; i++) {
126 local_irq_save(flags);
127 rdtscl(t1);
128 for (t = 0; t < 50; t++) gameport_read(gameport);
129 rdtscl(t2);
130 local_irq_restore(flags);
131 udelay(i * 10);
132 if (t2 - t1 < tx) tx = t2 - t1;
133 }
134
135 gameport_close(gameport);
Mike Travis92cb7612007-10-19 20:35:04 +0200136 return (cpu_data(raw_smp_processor_id()).loops_per_jiffy *
137 (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139#else
140
141 unsigned int j, t = 0;
142
143 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
144 return 0;
145
146 j = jiffies; while (j == jiffies);
147 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
148
149 gameport_close(gameport);
150 return t * HZ / 1000;
151
152#endif
153}
154
155void gameport_start_polling(struct gameport *gameport)
156{
157 spin_lock(&gameport->timer_lock);
158
159 if (!gameport->poll_cnt++) {
160 BUG_ON(!gameport->poll_handler);
161 BUG_ON(!gameport->poll_interval);
162 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
163 }
164
165 spin_unlock(&gameport->timer_lock);
166}
167
168void gameport_stop_polling(struct gameport *gameport)
169{
170 spin_lock(&gameport->timer_lock);
171
172 if (!--gameport->poll_cnt)
173 del_timer(&gameport->poll_timer);
174
175 spin_unlock(&gameport->timer_lock);
176}
177
178static void gameport_run_poll_handler(unsigned long d)
179{
180 struct gameport *gameport = (struct gameport *)d;
181
182 gameport->poll_handler(gameport);
183 if (gameport->poll_cnt)
184 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
185}
186
187/*
188 * Basic gameport -> driver core mappings
189 */
190
Dmitry Torokhov79580052007-04-10 00:40:48 -0400191static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400193 int error;
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 gameport->dev.driver = &drv->driver;
196 if (drv->connect(gameport, drv)) {
197 gameport->dev.driver = NULL;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400198 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400200
201 error = device_bind_driver(&gameport->dev);
202 if (error) {
203 printk(KERN_WARNING
204 "gameport: device_bind_driver() failed "
205 "for %s (%s) and %s, error: %d\n",
206 gameport->phys, gameport->name,
207 drv->description, error);
208 drv->disconnect(gameport);
209 gameport->dev.driver = NULL;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400210 return error;
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400211 }
212
Dmitry Torokhov79580052007-04-10 00:40:48 -0400213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216static void gameport_find_driver(struct gameport *gameport)
217{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400218 int error;
219
Randy Dunlap73b59a32006-07-19 01:14:55 -0400220 error = device_attach(&gameport->dev);
221 if (error < 0)
222 printk(KERN_WARNING
223 "gameport: device_attach() failed for %s (%s), error: %d\n",
224 gameport->phys, gameport->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227
228/*
229 * Gameport event processing.
230 */
231
232enum gameport_event_type {
233 GAMEPORT_RESCAN,
234 GAMEPORT_RECONNECT,
235 GAMEPORT_REGISTER_PORT,
236 GAMEPORT_REGISTER_DRIVER,
237};
238
239struct gameport_event {
240 enum gameport_event_type type;
241 void *object;
242 struct module *owner;
243 struct list_head node;
244};
245
246static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
247static LIST_HEAD(gameport_event_list);
248static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700249static struct task_struct *gameport_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251static void gameport_queue_event(void *object, struct module *owner,
252 enum gameport_event_type event_type)
253{
254 unsigned long flags;
255 struct gameport_event *event;
256
257 spin_lock_irqsave(&gameport_event_lock, flags);
258
259 /*
Linus Torvalds3c803e82005-06-27 17:49:45 -0700260 * Scan event list for the other events for the same gameport port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * starting with the most recent one. If event is the same we
262 * do not need add new one. If event is of different type we
263 * need to add this event and should not look further because
264 * we need to preseve sequence of distinct events.
Linus Torvalds3c803e82005-06-27 17:49:45 -0700265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 list_for_each_entry_reverse(event, &gameport_event_list, node) {
267 if (event->object == object) {
268 if (event->type == event_type)
269 goto out;
270 break;
271 }
272 }
273
274 if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
275 if (!try_module_get(owner)) {
276 printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
Adrian Bunk642fde12006-03-14 00:13:34 -0500277 kfree(event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 goto out;
279 }
280
281 event->type = event_type;
282 event->object = object;
283 event->owner = owner;
284
285 list_add_tail(&event->node, &gameport_event_list);
286 wake_up(&gameport_wait);
287 } else {
288 printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
289 }
290out:
291 spin_unlock_irqrestore(&gameport_event_lock, flags);
292}
293
294static void gameport_free_event(struct gameport_event *event)
295{
296 module_put(event->owner);
297 kfree(event);
298}
299
300static void gameport_remove_duplicate_events(struct gameport_event *event)
301{
302 struct list_head *node, *next;
303 struct gameport_event *e;
304 unsigned long flags;
305
306 spin_lock_irqsave(&gameport_event_lock, flags);
307
308 list_for_each_safe(node, next, &gameport_event_list) {
309 e = list_entry(node, struct gameport_event, node);
310 if (event->object == e->object) {
311 /*
312 * If this event is of different type we should not
313 * look further - we only suppress duplicate events
314 * that were sent back-to-back.
315 */
316 if (event->type != e->type)
317 break;
318
319 list_del_init(node);
320 gameport_free_event(e);
321 }
322 }
323
324 spin_unlock_irqrestore(&gameport_event_lock, flags);
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static struct gameport_event *gameport_get_event(void)
328{
329 struct gameport_event *event;
330 struct list_head *node;
331 unsigned long flags;
332
333 spin_lock_irqsave(&gameport_event_lock, flags);
334
335 if (list_empty(&gameport_event_list)) {
336 spin_unlock_irqrestore(&gameport_event_lock, flags);
337 return NULL;
338 }
339
340 node = gameport_event_list.next;
341 event = list_entry(node, struct gameport_event, node);
342 list_del_init(node);
343
344 spin_unlock_irqrestore(&gameport_event_lock, flags);
345
346 return event;
347}
348
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500349static void gameport_handle_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct gameport_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Arjan van de Ven286295e2006-02-19 00:22:03 -0500353 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500355 /*
356 * Note that we handle only one event here to give swsusp
357 * a chance to freeze kgameportd thread. Gameport events
358 * should be pretty rare so we are not concerned about
359 * taking performance hit.
360 */
361 if ((event = gameport_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 switch (event->type) {
364 case GAMEPORT_REGISTER_PORT:
365 gameport_add_port(event->object);
366 break;
367
368 case GAMEPORT_RECONNECT:
369 gameport_reconnect_port(event->object);
370 break;
371
372 case GAMEPORT_RESCAN:
373 gameport_disconnect_port(event->object);
374 gameport_find_driver(event->object);
375 break;
376
377 case GAMEPORT_REGISTER_DRIVER:
Randy Dunlap73b59a32006-07-19 01:14:55 -0400378 gameport_add_driver(event->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380
381 default:
382 break;
383 }
384
385 gameport_remove_duplicate_events(event);
386 gameport_free_event(event);
387 }
388
Arjan van de Ven286295e2006-02-19 00:22:03 -0500389 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392/*
393 * Remove all events that have been submitted for a given gameport port.
394 */
395static void gameport_remove_pending_events(struct gameport *gameport)
396{
397 struct list_head *node, *next;
398 struct gameport_event *event;
399 unsigned long flags;
400
401 spin_lock_irqsave(&gameport_event_lock, flags);
402
403 list_for_each_safe(node, next, &gameport_event_list) {
404 event = list_entry(node, struct gameport_event, node);
405 if (event->object == gameport) {
406 list_del_init(node);
407 gameport_free_event(event);
408 }
409 }
410
411 spin_unlock_irqrestore(&gameport_event_lock, flags);
412}
413
414/*
415 * Destroy child gameport port (if any) that has not been fully registered yet.
416 *
417 * Note that we rely on the fact that port can have only one child and therefore
418 * only one child registration request can be pending. Additionally, children
419 * are registered by driver's connect() handler so there can't be a grandchild
420 * pending registration together with a child.
421 */
422static struct gameport *gameport_get_pending_child(struct gameport *parent)
423{
424 struct gameport_event *event;
425 struct gameport *gameport, *child = NULL;
426 unsigned long flags;
427
428 spin_lock_irqsave(&gameport_event_lock, flags);
429
430 list_for_each_entry(event, &gameport_event_list, node) {
431 if (event->type == GAMEPORT_REGISTER_PORT) {
432 gameport = event->object;
433 if (gameport->parent == parent) {
434 child = gameport;
435 break;
436 }
437 }
438 }
439
440 spin_unlock_irqrestore(&gameport_event_lock, flags);
441 return child;
442}
443
444static int gameport_thread(void *nothing)
445{
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700446 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 do {
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500448 gameport_handle_event();
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700449 wait_event_freezable(gameport_wait,
Linus Torvalds3c803e82005-06-27 17:49:45 -0700450 kthread_should_stop() || !list_empty(&gameport_event_list));
Linus Torvalds3c803e82005-06-27 17:49:45 -0700451 } while (!kthread_should_stop());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 printk(KERN_DEBUG "gameport: kgameportd exiting\n");
Linus Torvalds3c803e82005-06-27 17:49:45 -0700454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457
458/*
459 * Gameport port operations
460 */
461
Yani Ioannoue404e272005-05-17 06:42:58 -0400462static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct gameport *gameport = to_gameport_port(dev);
465 return sprintf(buf, "%s\n", gameport->name);
466}
467
Yani Ioannoue404e272005-05-17 06:42:58 -0400468static 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 -0700469{
470 struct gameport *gameport = to_gameport_port(dev);
471 struct device_driver *drv;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400472 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Dmitry Torokhov79580052007-04-10 00:40:48 -0400474 error = mutex_lock_interruptible(&gameport_mutex);
475 if (error)
476 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (!strncmp(buf, "none", count)) {
479 gameport_disconnect_port(gameport);
480 } else if (!strncmp(buf, "reconnect", count)) {
481 gameport_reconnect_port(gameport);
482 } else if (!strncmp(buf, "rescan", count)) {
483 gameport_disconnect_port(gameport);
484 gameport_find_driver(gameport);
485 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
486 gameport_disconnect_port(gameport);
Dmitry Torokhov79580052007-04-10 00:40:48 -0400487 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 put_driver(drv);
489 } else {
Dmitry Torokhov79580052007-04-10 00:40:48 -0400490 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Arjan van de Ven286295e2006-02-19 00:22:03 -0500493 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Dmitry Torokhov79580052007-04-10 00:40:48 -0400495 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498static struct device_attribute gameport_device_attrs[] = {
499 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
500 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
501 __ATTR_NULL
502};
503
504static void gameport_release_port(struct device *dev)
505{
506 struct gameport *gameport = to_gameport_port(dev);
507
508 kfree(gameport);
509 module_put(THIS_MODULE);
510}
511
512void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
513{
514 va_list args;
515
516 va_start(args, fmt);
517 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
518 va_end(args);
519}
520
521/*
522 * Prepare gameport port for registration.
523 */
524static void gameport_init_port(struct gameport *gameport)
525{
526 static atomic_t gameport_no = ATOMIC_INIT(0);
527
528 __module_get(THIS_MODULE);
529
Arjan van de Ven286295e2006-02-19 00:22:03 -0500530 mutex_init(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 device_initialize(&gameport->dev);
532 snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
533 "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
534 gameport->dev.bus = &gameport_bus;
535 gameport->dev.release = gameport_release_port;
536 if (gameport->parent)
537 gameport->dev.parent = &gameport->parent->dev;
538
Randy Dunlap73b59a32006-07-19 01:14:55 -0400539 INIT_LIST_HEAD(&gameport->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 spin_lock_init(&gameport->timer_lock);
541 init_timer(&gameport->poll_timer);
542 gameport->poll_timer.function = gameport_run_poll_handler;
543 gameport->poll_timer.data = (unsigned long)gameport;
544}
545
546/*
547 * Complete gameport port registration.
548 * Driver core will attempt to find appropriate driver for the port.
549 */
550static void gameport_add_port(struct gameport *gameport)
551{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400552 int error;
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (gameport->parent)
555 gameport->parent->child = gameport;
556
557 gameport->speed = gameport_measure_speed(gameport);
558
559 list_add_tail(&gameport->node, &gameport_list);
560
561 if (gameport->io)
562 printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
563 gameport->name, gameport->phys, gameport->io, gameport->speed);
564 else
565 printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
566 gameport->name, gameport->phys, gameport->speed);
567
Randy Dunlap73b59a32006-07-19 01:14:55 -0400568 error = device_add(&gameport->dev);
569 if (error)
570 printk(KERN_ERR
571 "gameport: device_add() failed for %s (%s), error: %d\n",
572 gameport->phys, gameport->name, error);
573 else
574 gameport->registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575}
576
577/*
578 * gameport_destroy_port() completes deregistration process and removes
579 * port from the system
580 */
581static void gameport_destroy_port(struct gameport *gameport)
582{
583 struct gameport *child;
584
585 child = gameport_get_pending_child(gameport);
586 if (child) {
587 gameport_remove_pending_events(child);
588 put_device(&child->dev);
589 }
590
591 if (gameport->parent) {
592 gameport->parent->child = NULL;
593 gameport->parent = NULL;
594 }
595
596 if (gameport->registered) {
597 device_del(&gameport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 gameport->registered = 0;
599 }
600
Randy Dunlap73b59a32006-07-19 01:14:55 -0400601 list_del_init(&gameport->node);
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 gameport_remove_pending_events(gameport);
604 put_device(&gameport->dev);
605}
606
607/*
608 * Reconnect gameport port and all its children (re-initialize attached devices)
609 */
610static void gameport_reconnect_port(struct gameport *gameport)
611{
612 do {
613 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
614 gameport_disconnect_port(gameport);
615 gameport_find_driver(gameport);
616 /* Ok, old children are now gone, we are done */
617 break;
618 }
619 gameport = gameport->child;
620 } while (gameport);
621}
622
623/*
624 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
625 * all child ports are unbound and destroyed.
626 */
627static void gameport_disconnect_port(struct gameport *gameport)
628{
629 struct gameport *s, *parent;
630
631 if (gameport->child) {
632 /*
633 * Children ports should be disconnected and destroyed
634 * first, staring with the leaf one, since we don't want
635 * to do recursion
636 */
637 for (s = gameport; s->child; s = s->child)
638 /* empty */;
639
640 do {
641 parent = s->parent;
642
Dmitry Torokhov79580052007-04-10 00:40:48 -0400643 device_release_driver(&s->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 gameport_destroy_port(s);
645 } while ((s = parent) != gameport);
646 }
647
648 /*
649 * Ok, no children left, now disconnect this port
650 */
Dmitry Torokhov79580052007-04-10 00:40:48 -0400651 device_release_driver(&gameport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654void gameport_rescan(struct gameport *gameport)
655{
656 gameport_queue_event(gameport, NULL, GAMEPORT_RESCAN);
657}
658
659void gameport_reconnect(struct gameport *gameport)
660{
661 gameport_queue_event(gameport, NULL, GAMEPORT_RECONNECT);
662}
663
664/*
665 * Submits register request to kgameportd for subsequent execution.
666 * Note that port registration is always asynchronous.
667 */
668void __gameport_register_port(struct gameport *gameport, struct module *owner)
669{
670 gameport_init_port(gameport);
671 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
672}
673
674/*
675 * Synchronously unregisters gameport port.
676 */
677void gameport_unregister_port(struct gameport *gameport)
678{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500679 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 gameport_disconnect_port(gameport);
681 gameport_destroy_port(gameport);
Arjan van de Ven286295e2006-02-19 00:22:03 -0500682 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685
686/*
687 * Gameport driver operations
688 */
689
690static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
691{
692 struct gameport_driver *driver = to_gameport_driver(drv);
693 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
694}
695
696static struct driver_attribute gameport_driver_attrs[] = {
697 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
698 __ATTR_NULL
699};
700
701static int gameport_driver_probe(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->connect(gameport, drv);
707 return gameport->drv ? 0 : -ENODEV;
708}
709
710static int gameport_driver_remove(struct device *dev)
711{
712 struct gameport *gameport = to_gameport_port(dev);
713 struct gameport_driver *drv = to_gameport_driver(dev->driver);
714
715 drv->disconnect(gameport);
716 return 0;
717}
718
Randy Dunlap73b59a32006-07-19 01:14:55 -0400719static void gameport_add_driver(struct gameport_driver *drv)
720{
721 int error;
722
723 error = driver_register(&drv->driver);
724 if (error)
725 printk(KERN_ERR
726 "gameport: driver_register() failed for %s, error: %d\n",
727 drv->driver.name, error);
728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
731{
732 drv->driver.bus = &gameport_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
734}
735
736void gameport_unregister_driver(struct gameport_driver *drv)
737{
738 struct gameport *gameport;
739
Arjan van de Ven286295e2006-02-19 00:22:03 -0500740 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 drv->ignore = 1; /* so gameport_find_driver ignores it */
742
743start_over:
744 list_for_each_entry(gameport, &gameport_list, node) {
745 if (gameport->drv == drv) {
746 gameport_disconnect_port(gameport);
747 gameport_find_driver(gameport);
748 /* we could've deleted some ports, restart */
749 goto start_over;
750 }
751 }
752
753 driver_unregister(&drv->driver);
Arjan van de Ven286295e2006-02-19 00:22:03 -0500754 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757static 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
Dmitry Torokhovb187dd72006-11-02 23:27:30 -0500764static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
774{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500775 mutex_lock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 gameport->drv = drv;
Arjan van de Ven286295e2006-02-19 00:22:03 -0500777 mutex_unlock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
781{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 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
795void gameport_close(struct gameport *gameport)
796{
797 del_timer_sync(&gameport->poll_timer);
798 gameport->poll_handler = NULL;
799 gameport->poll_interval = 0;
800 gameport_set_drv(gameport, NULL);
801 if (gameport->close)
802 gameport->close(gameport);
803}
804
805static int __init gameport_init(void)
806{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400807 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Randy Dunlap73b59a32006-07-19 01:14:55 -0400809 error = bus_register(&gameport_bus);
810 if (error) {
811 printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
812 return error;
813 }
814
815 gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
816 if (IS_ERR(gameport_task)) {
817 bus_unregister(&gameport_bus);
818 error = PTR_ERR(gameport_task);
819 printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
820 return error;
821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 return 0;
824}
825
826static void __exit gameport_exit(void)
827{
828 bus_unregister(&gameport_bus);
Linus Torvalds3c803e82005-06-27 17:49:45 -0700829 kthread_stop(gameport_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}
831
Dmitry Torokhov51c38f92006-02-19 00:22:51 -0500832subsys_initcall(gameport_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833module_exit(gameport_exit);