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