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