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