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