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