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