blob: c351aa421f8f7b79c41ac8d6f581de6f8f3c6571 [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
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -080014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/stddef.h>
17#include <linux/module.h>
18#include <linux/ioport.h>
19#include <linux/init.h>
20#include <linux/gameport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/slab.h>
22#include <linux/delay.h>
Dmitry Torokhovc44f2422010-11-15 01:39:57 -080023#include <linux/workqueue.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>
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
Arjan van de Ven286295e2006-02-19 00:22:03 -050034 * gameport_mutex protects entire gameport subsystem and is taken
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * every time gameport port or driver registrered or unregistered.
36 */
Arjan van de Ven286295e2006-02-19 00:22:03 -050037static DEFINE_MUTEX(gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39static LIST_HEAD(gameport_list);
40
Russell King29a4a202006-01-05 14:38:22 +000041static struct bus_type gameport_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static void gameport_add_port(struct gameport *gameport);
Dmitry Torokhov4ced8e72009-04-13 15:27:49 -070044static void gameport_attach_driver(struct gameport_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static void gameport_reconnect_port(struct gameport *gameport);
46static void gameport_disconnect_port(struct gameport *gameport);
47
48#if defined(__i386__)
49
Ralf Baechle334955e2011-06-01 19:04:57 +010050#include <linux/i8253.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
53#define GET_TIME(x) do { x = get_time_pit(); } while (0)
54
55static unsigned int get_time_pit(void)
56{
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned long flags;
58 unsigned int count;
59
Thomas Gleixnerced918e2010-02-17 16:47:10 +000060 raw_spin_lock_irqsave(&i8253_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 outb_p(0x00, 0x43);
62 count = inb_p(0x40);
63 count |= inb_p(0x40) << 8;
Thomas Gleixnerced918e2010-02-17 16:47:10 +000064 raw_spin_unlock_irqrestore(&i8253_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 return count;
67}
68
69#endif
70
71
72
73/*
74 * gameport_measure_speed() measures the gameport i/o speed.
75 */
76
77static int gameport_measure_speed(struct gameport *gameport)
78{
79#if defined(__i386__)
80
81 unsigned int i, t, t1, t2, t3, tx;
82 unsigned long flags;
83
84 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
85 return 0;
86
87 tx = 1 << 30;
88
89 for(i = 0; i < 50; i++) {
90 local_irq_save(flags);
91 GET_TIME(t1);
92 for (t = 0; t < 50; t++) gameport_read(gameport);
93 GET_TIME(t2);
94 GET_TIME(t3);
95 local_irq_restore(flags);
96 udelay(i * 10);
97 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
98 }
99
100 gameport_close(gameport);
101 return 59659 / (tx < 1 ? 1 : tx);
102
103#elif defined (__x86_64__)
104
105 unsigned int i, t;
106 unsigned long tx, t1, t2, flags;
107
108 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
109 return 0;
110
111 tx = 1 << 30;
112
113 for(i = 0; i < 50; i++) {
114 local_irq_save(flags);
115 rdtscl(t1);
116 for (t = 0; t < 50; t++) gameport_read(gameport);
117 rdtscl(t2);
118 local_irq_restore(flags);
119 udelay(i * 10);
120 if (t2 - t1 < tx) tx = t2 - t1;
121 }
122
123 gameport_close(gameport);
Christoph Lameter55ee4ef2010-12-16 12:15:15 -0600124 return (this_cpu_read(cpu_info.loops_per_jiffy) *
Mike Travis92cb7612007-10-19 20:35:04 +0200125 (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127#else
128
129 unsigned int j, t = 0;
130
131 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
132 return 0;
133
134 j = jiffies; while (j == jiffies);
135 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
136
137 gameport_close(gameport);
138 return t * HZ / 1000;
139
140#endif
141}
142
143void gameport_start_polling(struct gameport *gameport)
144{
145 spin_lock(&gameport->timer_lock);
146
147 if (!gameport->poll_cnt++) {
148 BUG_ON(!gameport->poll_handler);
149 BUG_ON(!gameport->poll_interval);
150 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
151 }
152
153 spin_unlock(&gameport->timer_lock);
154}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700155EXPORT_SYMBOL(gameport_start_polling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157void gameport_stop_polling(struct gameport *gameport)
158{
159 spin_lock(&gameport->timer_lock);
160
161 if (!--gameport->poll_cnt)
162 del_timer(&gameport->poll_timer);
163
164 spin_unlock(&gameport->timer_lock);
165}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700166EXPORT_SYMBOL(gameport_stop_polling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168static void gameport_run_poll_handler(unsigned long d)
169{
170 struct gameport *gameport = (struct gameport *)d;
171
172 gameport->poll_handler(gameport);
173 if (gameport->poll_cnt)
174 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
175}
176
177/*
178 * Basic gameport -> driver core mappings
179 */
180
Dmitry Torokhov79580052007-04-10 00:40:48 -0400181static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400183 int error;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 gameport->dev.driver = &drv->driver;
186 if (drv->connect(gameport, drv)) {
187 gameport->dev.driver = NULL;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400188 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400190
191 error = device_bind_driver(&gameport->dev);
192 if (error) {
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800193 dev_warn(&gameport->dev,
194 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400195 gameport->phys, gameport->name,
196 drv->description, error);
197 drv->disconnect(gameport);
198 gameport->dev.driver = NULL;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400199 return error;
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400200 }
201
Dmitry Torokhov79580052007-04-10 00:40:48 -0400202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205static void gameport_find_driver(struct gameport *gameport)
206{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400207 int error;
208
Randy Dunlap73b59a32006-07-19 01:14:55 -0400209 error = device_attach(&gameport->dev);
210 if (error < 0)
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800211 dev_warn(&gameport->dev,
212 "device_attach() failed for %s (%s), error: %d\n",
213 gameport->phys, gameport->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216
217/*
218 * Gameport event processing.
219 */
220
221enum gameport_event_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 GAMEPORT_REGISTER_PORT,
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400223 GAMEPORT_ATTACH_DRIVER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224};
225
226struct gameport_event {
227 enum gameport_event_type type;
228 void *object;
229 struct module *owner;
230 struct list_head node;
231};
232
233static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
234static LIST_HEAD(gameport_event_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800236static struct gameport_event *gameport_get_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800238 struct gameport_event *event = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 spin_lock_irqsave(&gameport_event_lock, flags);
242
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800243 if (!list_empty(&gameport_event_list)) {
244 event = list_first_entry(&gameport_event_list,
245 struct gameport_event, node);
246 list_del_init(&event->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 spin_unlock_irqrestore(&gameport_event_lock, flags);
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800250 return event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253static void gameport_free_event(struct gameport_event *event)
254{
255 module_put(event->owner);
256 kfree(event);
257}
258
259static void gameport_remove_duplicate_events(struct gameport_event *event)
260{
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800261 struct gameport_event *e, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 unsigned long flags;
263
264 spin_lock_irqsave(&gameport_event_lock, flags);
265
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800266 list_for_each_entry_safe(e, next, &gameport_event_list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (event->object == e->object) {
268 /*
269 * If this event is of different type we should not
270 * look further - we only suppress duplicate events
271 * that were sent back-to-back.
272 */
273 if (event->type != e->type)
274 break;
275
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800276 list_del_init(&e->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 gameport_free_event(e);
278 }
279 }
280
281 spin_unlock_irqrestore(&gameport_event_lock, flags);
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800285static void gameport_handle_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct gameport_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Arjan van de Ven286295e2006-02-19 00:22:03 -0500289 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500291 /*
292 * Note that we handle only one event here to give swsusp
293 * a chance to freeze kgameportd thread. Gameport events
294 * should be pretty rare so we are not concerned about
295 * taking performance hit.
296 */
297 if ((event = gameport_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 switch (event->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800301 case GAMEPORT_REGISTER_PORT:
302 gameport_add_port(event->object);
303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800305 case GAMEPORT_ATTACH_DRIVER:
306 gameport_attach_driver(event->object);
307 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
310 gameport_remove_duplicate_events(event);
311 gameport_free_event(event);
312 }
313
Arjan van de Ven286295e2006-02-19 00:22:03 -0500314 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800317static DECLARE_WORK(gameport_event_work, gameport_handle_events);
318
319static int gameport_queue_event(void *object, struct module *owner,
320 enum gameport_event_type event_type)
321{
322 unsigned long flags;
323 struct gameport_event *event;
324 int retval = 0;
325
326 spin_lock_irqsave(&gameport_event_lock, flags);
327
328 /*
329 * Scan event list for the other events for the same gameport port,
330 * starting with the most recent one. If event is the same we
331 * do not need add new one. If event is of different type we
332 * need to add this event and should not look further because
333 * we need to preserve sequence of distinct events.
334 */
335 list_for_each_entry_reverse(event, &gameport_event_list, node) {
336 if (event->object == object) {
337 if (event->type == event_type)
338 goto out;
339 break;
340 }
341 }
342
343 event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC);
344 if (!event) {
345 pr_err("Not enough memory to queue event %d\n", event_type);
346 retval = -ENOMEM;
347 goto out;
348 }
349
350 if (!try_module_get(owner)) {
351 pr_warning("Can't get module reference, dropping event %d\n",
352 event_type);
353 kfree(event);
354 retval = -EINVAL;
355 goto out;
356 }
357
358 event->type = event_type;
359 event->object = object;
360 event->owner = owner;
361
362 list_add_tail(&event->node, &gameport_event_list);
Dmitry Torokhov1d64b652011-02-23 08:51:28 -0800363 queue_work(system_long_wq, &gameport_event_work);
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800364
365out:
366 spin_unlock_irqrestore(&gameport_event_lock, flags);
367 return retval;
368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/*
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400371 * Remove all events that have been submitted for a given object,
372 * be it a gameport port or a driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 */
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400374static void gameport_remove_pending_events(void *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800376 struct gameport_event *event, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 unsigned long flags;
378
379 spin_lock_irqsave(&gameport_event_lock, flags);
380
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800381 list_for_each_entry_safe(event, next, &gameport_event_list, node) {
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400382 if (event->object == object) {
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800383 list_del_init(&event->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 gameport_free_event(event);
385 }
386 }
387
388 spin_unlock_irqrestore(&gameport_event_lock, flags);
389}
390
391/*
392 * Destroy child gameport port (if any) that has not been fully registered yet.
393 *
394 * Note that we rely on the fact that port can have only one child and therefore
395 * only one child registration request can be pending. Additionally, children
396 * are registered by driver's connect() handler so there can't be a grandchild
397 * pending registration together with a child.
398 */
399static struct gameport *gameport_get_pending_child(struct gameport *parent)
400{
401 struct gameport_event *event;
402 struct gameport *gameport, *child = NULL;
403 unsigned long flags;
404
405 spin_lock_irqsave(&gameport_event_lock, flags);
406
407 list_for_each_entry(event, &gameport_event_list, node) {
408 if (event->type == GAMEPORT_REGISTER_PORT) {
409 gameport = event->object;
410 if (gameport->parent == parent) {
411 child = gameport;
412 break;
413 }
414 }
415 }
416
417 spin_unlock_irqrestore(&gameport_event_lock, flags);
418 return child;
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*
422 * Gameport port operations
423 */
424
Yani Ioannoue404e272005-05-17 06:42:58 -0400425static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 struct gameport *gameport = to_gameport_port(dev);
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return sprintf(buf, "%s\n", gameport->name);
430}
431
Yani Ioannoue404e272005-05-17 06:42:58 -0400432static 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 -0700433{
434 struct gameport *gameport = to_gameport_port(dev);
435 struct device_driver *drv;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400436 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Dmitry Torokhov79580052007-04-10 00:40:48 -0400438 error = mutex_lock_interruptible(&gameport_mutex);
439 if (error)
440 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (!strncmp(buf, "none", count)) {
443 gameport_disconnect_port(gameport);
444 } else if (!strncmp(buf, "reconnect", count)) {
445 gameport_reconnect_port(gameport);
446 } else if (!strncmp(buf, "rescan", count)) {
447 gameport_disconnect_port(gameport);
448 gameport_find_driver(gameport);
449 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
450 gameport_disconnect_port(gameport);
Dmitry Torokhov79580052007-04-10 00:40:48 -0400451 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 put_driver(drv);
453 } else {
Dmitry Torokhov79580052007-04-10 00:40:48 -0400454 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456
Arjan van de Ven286295e2006-02-19 00:22:03 -0500457 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dmitry Torokhov79580052007-04-10 00:40:48 -0400459 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462static struct device_attribute gameport_device_attrs[] = {
463 __ATTR(description, S_IRUGO, gameport_show_description, NULL),
464 __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
465 __ATTR_NULL
466};
467
468static void gameport_release_port(struct device *dev)
469{
470 struct gameport *gameport = to_gameport_port(dev);
471
472 kfree(gameport);
473 module_put(THIS_MODULE);
474}
475
476void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
477{
478 va_list args;
479
480 va_start(args, fmt);
481 vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
482 va_end(args);
483}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700484EXPORT_SYMBOL(gameport_set_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486/*
487 * Prepare gameport port for registration.
488 */
489static void gameport_init_port(struct gameport *gameport)
490{
491 static atomic_t gameport_no = ATOMIC_INIT(0);
492
493 __module_get(THIS_MODULE);
494
Arjan van de Ven286295e2006-02-19 00:22:03 -0500495 mutex_init(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 device_initialize(&gameport->dev);
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800497 dev_set_name(&gameport->dev, "gameport%lu",
498 (unsigned long)atomic_inc_return(&gameport_no) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 gameport->dev.bus = &gameport_bus;
500 gameport->dev.release = gameport_release_port;
501 if (gameport->parent)
502 gameport->dev.parent = &gameport->parent->dev;
503
Randy Dunlap73b59a32006-07-19 01:14:55 -0400504 INIT_LIST_HEAD(&gameport->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 spin_lock_init(&gameport->timer_lock);
506 init_timer(&gameport->poll_timer);
507 gameport->poll_timer.function = gameport_run_poll_handler;
508 gameport->poll_timer.data = (unsigned long)gameport;
509}
510
511/*
512 * Complete gameport port registration.
513 * Driver core will attempt to find appropriate driver for the port.
514 */
515static void gameport_add_port(struct gameport *gameport)
516{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400517 int error;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (gameport->parent)
520 gameport->parent->child = gameport;
521
522 gameport->speed = gameport_measure_speed(gameport);
523
524 list_add_tail(&gameport->node, &gameport_list);
525
526 if (gameport->io)
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800527 dev_info(&gameport->dev, "%s is %s, io %#x, speed %dkHz\n",
528 gameport->name, gameport->phys, gameport->io, gameport->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 else
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800530 dev_info(&gameport->dev, "%s is %s, speed %dkHz\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 gameport->name, gameport->phys, gameport->speed);
532
Randy Dunlap73b59a32006-07-19 01:14:55 -0400533 error = device_add(&gameport->dev);
534 if (error)
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800535 dev_err(&gameport->dev,
536 "device_add() failed for %s (%s), error: %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400537 gameport->phys, gameport->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/*
541 * gameport_destroy_port() completes deregistration process and removes
542 * port from the system
543 */
544static void gameport_destroy_port(struct gameport *gameport)
545{
546 struct gameport *child;
547
548 child = gameport_get_pending_child(gameport);
549 if (child) {
550 gameport_remove_pending_events(child);
551 put_device(&child->dev);
552 }
553
554 if (gameport->parent) {
555 gameport->parent->child = NULL;
556 gameport->parent = NULL;
557 }
558
Dmitry Torokhov361b7b52010-01-05 17:56:03 -0800559 if (device_is_registered(&gameport->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 device_del(&gameport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Randy Dunlap73b59a32006-07-19 01:14:55 -0400562 list_del_init(&gameport->node);
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 gameport_remove_pending_events(gameport);
565 put_device(&gameport->dev);
566}
567
568/*
569 * Reconnect gameport port and all its children (re-initialize attached devices)
570 */
571static void gameport_reconnect_port(struct gameport *gameport)
572{
573 do {
574 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
575 gameport_disconnect_port(gameport);
576 gameport_find_driver(gameport);
577 /* Ok, old children are now gone, we are done */
578 break;
579 }
580 gameport = gameport->child;
581 } while (gameport);
582}
583
584/*
585 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
586 * all child ports are unbound and destroyed.
587 */
588static void gameport_disconnect_port(struct gameport *gameport)
589{
590 struct gameport *s, *parent;
591
592 if (gameport->child) {
593 /*
594 * Children ports should be disconnected and destroyed
595 * first, staring with the leaf one, since we don't want
596 * to do recursion
597 */
598 for (s = gameport; s->child; s = s->child)
599 /* empty */;
600
601 do {
602 parent = s->parent;
603
Dmitry Torokhov79580052007-04-10 00:40:48 -0400604 device_release_driver(&s->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 gameport_destroy_port(s);
606 } while ((s = parent) != gameport);
607 }
608
609 /*
610 * Ok, no children left, now disconnect this port
611 */
Dmitry Torokhov79580052007-04-10 00:40:48 -0400612 device_release_driver(&gameport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/*
616 * Submits register request to kgameportd for subsequent execution.
617 * Note that port registration is always asynchronous.
618 */
619void __gameport_register_port(struct gameport *gameport, struct module *owner)
620{
621 gameport_init_port(gameport);
622 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
623}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700624EXPORT_SYMBOL(__gameport_register_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626/*
627 * Synchronously unregisters gameport port.
628 */
629void gameport_unregister_port(struct gameport *gameport)
630{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500631 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 gameport_disconnect_port(gameport);
633 gameport_destroy_port(gameport);
Arjan van de Ven286295e2006-02-19 00:22:03 -0500634 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700636EXPORT_SYMBOL(gameport_unregister_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638
639/*
640 * Gameport driver operations
641 */
642
643static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
644{
645 struct gameport_driver *driver = to_gameport_driver(drv);
646 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
647}
648
649static struct driver_attribute gameport_driver_attrs[] = {
650 __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
651 __ATTR_NULL
652};
653
654static int gameport_driver_probe(struct device *dev)
655{
656 struct gameport *gameport = to_gameport_port(dev);
657 struct gameport_driver *drv = to_gameport_driver(dev->driver);
658
659 drv->connect(gameport, drv);
660 return gameport->drv ? 0 : -ENODEV;
661}
662
663static int gameport_driver_remove(struct device *dev)
664{
665 struct gameport *gameport = to_gameport_port(dev);
666 struct gameport_driver *drv = to_gameport_driver(dev->driver);
667
668 drv->disconnect(gameport);
669 return 0;
670}
671
Dmitry Torokhov4ced8e72009-04-13 15:27:49 -0700672static void gameport_attach_driver(struct gameport_driver *drv)
Randy Dunlap73b59a32006-07-19 01:14:55 -0400673{
674 int error;
675
Dmitry Torokhov4ced8e72009-04-13 15:27:49 -0700676 error = driver_attach(&drv->driver);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400677 if (error)
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800678 pr_err("driver_attach() failed for %s, error: %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400679 drv->driver.name, error);
680}
681
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400682int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
683 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400685 int error;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 drv->driver.bus = &gameport_bus;
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400688 drv->driver.owner = owner;
689 drv->driver.mod_name = mod_name;
690
691 /*
692 * Temporarily disable automatic binding because probing
693 * takes long time and we are better off doing it in kgameportd
694 */
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700695 drv->ignore = true;
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400696
697 error = driver_register(&drv->driver);
698 if (error) {
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800699 pr_err("driver_register() failed for %s, error: %d\n",
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400700 drv->driver.name, error);
701 return error;
702 }
703
704 /*
705 * Reset ignore flag and let kgameportd bind the driver to free ports
706 */
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700707 drv->ignore = false;
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400708 error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
709 if (error) {
710 driver_unregister(&drv->driver);
711 return error;
712 }
713
714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700716EXPORT_SYMBOL(__gameport_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718void gameport_unregister_driver(struct gameport_driver *drv)
719{
720 struct gameport *gameport;
721
Arjan van de Ven286295e2006-02-19 00:22:03 -0500722 mutex_lock(&gameport_mutex);
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400723
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700724 drv->ignore = true; /* so gameport_find_driver ignores it */
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400725 gameport_remove_pending_events(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727start_over:
728 list_for_each_entry(gameport, &gameport_list, node) {
729 if (gameport->drv == drv) {
730 gameport_disconnect_port(gameport);
731 gameport_find_driver(gameport);
732 /* we could've deleted some ports, restart */
733 goto start_over;
734 }
735 }
736
737 driver_unregister(&drv->driver);
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400738
Arjan van de Ven286295e2006-02-19 00:22:03 -0500739 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700741EXPORT_SYMBOL(gameport_unregister_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743static int gameport_bus_match(struct device *dev, struct device_driver *drv)
744{
745 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
746
747 return !gameport_drv->ignore;
748}
749
Dmitry Torokhovb187dd72006-11-02 23:27:30 -0500750static struct bus_type gameport_bus = {
751 .name = "gameport",
752 .dev_attrs = gameport_device_attrs,
753 .drv_attrs = gameport_driver_attrs,
754 .match = gameport_bus_match,
755 .probe = gameport_driver_probe,
756 .remove = gameport_driver_remove,
757};
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
760{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500761 mutex_lock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 gameport->drv = drv;
Arjan van de Ven286295e2006-02-19 00:22:03 -0500763 mutex_unlock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
767{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (gameport->open) {
769 if (gameport->open(gameport, mode)) {
770 return -1;
771 }
772 } else {
773 if (mode != GAMEPORT_MODE_RAW)
774 return -1;
775 }
776
777 gameport_set_drv(gameport, drv);
778 return 0;
779}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700780EXPORT_SYMBOL(gameport_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782void gameport_close(struct gameport *gameport)
783{
784 del_timer_sync(&gameport->poll_timer);
785 gameport->poll_handler = NULL;
786 gameport->poll_interval = 0;
787 gameport_set_drv(gameport, NULL);
788 if (gameport->close)
789 gameport->close(gameport);
790}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700791EXPORT_SYMBOL(gameport_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793static int __init gameport_init(void)
794{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400795 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Randy Dunlap73b59a32006-07-19 01:14:55 -0400797 error = bus_register(&gameport_bus);
798 if (error) {
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800799 pr_err("failed to register gameport bus, error: %d\n", error);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400800 return error;
801 }
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 return 0;
805}
806
807static void __exit gameport_exit(void)
808{
809 bus_unregister(&gameport_bus);
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800810
811 /*
812 * There should not be any outstanding events but work may
813 * still be scheduled so simply cancel it.
814 */
815 cancel_work_sync(&gameport_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Dmitry Torokhov51c38f92006-02-19 00:22:51 -0500818subsys_initcall(gameport_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819module_exit(gameport_exit);