blob: cedc665364cd6e872ee8a86795b0de19913b8a28 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Torokhovfc99ec62010-01-05 17:56:03 -080014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#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 Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/slab.h>
22#include <linux/delay.h>
Dmitry Torokhovc44f2422010-11-15 01:39:57 -080023#include <linux/workqueue.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080024#include <linux/sched.h> /* HZ */
Arjan van de Ven286295e2006-02-19 00:22:03 -050025#include <linux/mutex.h>
Takashi Iwai76460a72014-09-11 10:28:30 -070026#include <linux/timekeeping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*#include <asm/io.h>*/
29
30MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
31MODULE_DESCRIPTION("Generic gameport layer");
32MODULE_LICENSE("GPL");
33
Takashi Iwai76460a72014-09-11 10:28:30 -070034static bool use_ktime = true;
35module_param(use_ktime, bool, 0400);
36MODULE_PARM_DESC(use_ktime, "Use ktime for measuring I/O speed");
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
Arjan van de Ven286295e2006-02-19 00:22:03 -050039 * gameport_mutex protects entire gameport subsystem and is taken
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * every time gameport port or driver registrered or unregistered.
41 */
Arjan van de Ven286295e2006-02-19 00:22:03 -050042static DEFINE_MUTEX(gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static LIST_HEAD(gameport_list);
45
Russell King29a4a202006-01-05 14:38:22 +000046static struct bus_type gameport_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static void gameport_add_port(struct gameport *gameport);
Dmitry Torokhov4ced8e72009-04-13 15:27:49 -070049static void gameport_attach_driver(struct gameport_driver *drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050static void gameport_reconnect_port(struct gameport *gameport);
51static void gameport_disconnect_port(struct gameport *gameport);
52
53#if defined(__i386__)
54
Ralf Baechle334955e2011-06-01 19:04:57 +010055#include <linux/i8253.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#define DELTA(x,y) ((y)-(x)+((y)<(x)?1193182/HZ:0))
58#define GET_TIME(x) do { x = get_time_pit(); } while (0)
59
60static unsigned int get_time_pit(void)
61{
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 unsigned long flags;
63 unsigned int count;
64
Thomas Gleixnerced918e2010-02-17 16:47:10 +000065 raw_spin_lock_irqsave(&i8253_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 outb_p(0x00, 0x43);
67 count = inb_p(0x40);
68 count |= inb_p(0x40) << 8;
Thomas Gleixnerced918e2010-02-17 16:47:10 +000069 raw_spin_unlock_irqrestore(&i8253_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 return count;
72}
73
74#endif
75
76
77
78/*
79 * gameport_measure_speed() measures the gameport i/o speed.
80 */
81
82static int gameport_measure_speed(struct gameport *gameport)
83{
Takashi Iwai76460a72014-09-11 10:28:30 -070084 unsigned int i, t, tx;
85 u64 t1, t2, t3;
86 unsigned long flags;
87
88 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
89 return 0;
90
91 tx = ~0;
92
93 for (i = 0; i < 50; i++) {
94 local_irq_save(flags);
95 t1 = ktime_get_ns();
96 for (t = 0; t < 50; t++)
97 gameport_read(gameport);
98 t2 = ktime_get_ns();
99 t3 = ktime_get_ns();
100 local_irq_restore(flags);
101 udelay(i * 10);
102 t = (t2 - t1) - (t3 - t2);
103 if (t < tx)
104 tx = t;
105 }
106
107 gameport_close(gameport);
108 t = 1000000 * 50;
109 if (tx)
110 t /= tx;
111 return t;
112}
113
114static int old_gameport_measure_speed(struct gameport *gameport)
115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116#if defined(__i386__)
117
118 unsigned int i, t, t1, t2, t3, tx;
119 unsigned long flags;
120
121 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
122 return 0;
123
124 tx = 1 << 30;
125
126 for(i = 0; i < 50; i++) {
127 local_irq_save(flags);
128 GET_TIME(t1);
129 for (t = 0; t < 50; t++) gameport_read(gameport);
130 GET_TIME(t2);
131 GET_TIME(t3);
132 local_irq_restore(flags);
133 udelay(i * 10);
134 if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
135 }
136
137 gameport_close(gameport);
138 return 59659 / (tx < 1 ? 1 : tx);
139
140#elif defined (__x86_64__)
141
142 unsigned int i, t;
143 unsigned long tx, t1, t2, flags;
144
145 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
146 return 0;
147
148 tx = 1 << 30;
149
150 for(i = 0; i < 50; i++) {
151 local_irq_save(flags);
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200152 t1 = rdtsc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 for (t = 0; t < 50; t++) gameport_read(gameport);
Andy Lutomirski4ea16362015-06-25 18:44:07 +0200154 t2 = rdtsc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 local_irq_restore(flags);
156 udelay(i * 10);
157 if (t2 - t1 < tx) tx = t2 - t1;
158 }
159
160 gameport_close(gameport);
Christoph Lameter55ee4ef2010-12-16 12:15:15 -0600161 return (this_cpu_read(cpu_info.loops_per_jiffy) *
Mike Travis92cb7612007-10-19 20:35:04 +0200162 (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164#else
165
166 unsigned int j, t = 0;
167
168 if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
169 return 0;
170
171 j = jiffies; while (j == jiffies);
172 j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
173
174 gameport_close(gameport);
175 return t * HZ / 1000;
176
177#endif
178}
179
180void gameport_start_polling(struct gameport *gameport)
181{
182 spin_lock(&gameport->timer_lock);
183
184 if (!gameport->poll_cnt++) {
185 BUG_ON(!gameport->poll_handler);
186 BUG_ON(!gameport->poll_interval);
187 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
188 }
189
190 spin_unlock(&gameport->timer_lock);
191}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700192EXPORT_SYMBOL(gameport_start_polling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194void gameport_stop_polling(struct gameport *gameport)
195{
196 spin_lock(&gameport->timer_lock);
197
198 if (!--gameport->poll_cnt)
199 del_timer(&gameport->poll_timer);
200
201 spin_unlock(&gameport->timer_lock);
202}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700203EXPORT_SYMBOL(gameport_stop_polling);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205static void gameport_run_poll_handler(unsigned long d)
206{
207 struct gameport *gameport = (struct gameport *)d;
208
209 gameport->poll_handler(gameport);
210 if (gameport->poll_cnt)
211 mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
212}
213
214/*
215 * Basic gameport -> driver core mappings
216 */
217
Dmitry Torokhov79580052007-04-10 00:40:48 -0400218static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400220 int error;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 gameport->dev.driver = &drv->driver;
223 if (drv->connect(gameport, drv)) {
224 gameport->dev.driver = NULL;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400225 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400227
228 error = device_bind_driver(&gameport->dev);
229 if (error) {
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800230 dev_warn(&gameport->dev,
231 "device_bind_driver() failed for %s (%s) and %s, error: %d\n",
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400232 gameport->phys, gameport->name,
233 drv->description, error);
234 drv->disconnect(gameport);
235 gameport->dev.driver = NULL;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400236 return error;
Dmitry Torokhov23de1512006-10-12 01:06:34 -0400237 }
238
Dmitry Torokhov79580052007-04-10 00:40:48 -0400239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242static void gameport_find_driver(struct gameport *gameport)
243{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400244 int error;
245
Randy Dunlap73b59a32006-07-19 01:14:55 -0400246 error = device_attach(&gameport->dev);
247 if (error < 0)
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800248 dev_warn(&gameport->dev,
249 "device_attach() failed for %s (%s), error: %d\n",
250 gameport->phys, gameport->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253
254/*
255 * Gameport event processing.
256 */
257
258enum gameport_event_type {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 GAMEPORT_REGISTER_PORT,
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400260 GAMEPORT_ATTACH_DRIVER,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
263struct gameport_event {
264 enum gameport_event_type type;
265 void *object;
266 struct module *owner;
267 struct list_head node;
268};
269
270static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
271static LIST_HEAD(gameport_event_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800273static struct gameport_event *gameport_get_event(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800275 struct gameport_event *event = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 spin_lock_irqsave(&gameport_event_lock, flags);
279
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800280 if (!list_empty(&gameport_event_list)) {
281 event = list_first_entry(&gameport_event_list,
282 struct gameport_event, node);
283 list_del_init(&event->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 spin_unlock_irqrestore(&gameport_event_lock, flags);
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800287 return event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
290static void gameport_free_event(struct gameport_event *event)
291{
292 module_put(event->owner);
293 kfree(event);
294}
295
296static void gameport_remove_duplicate_events(struct gameport_event *event)
297{
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800298 struct gameport_event *e, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 unsigned long flags;
300
301 spin_lock_irqsave(&gameport_event_lock, flags);
302
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800303 list_for_each_entry_safe(e, next, &gameport_event_list, node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (event->object == e->object) {
305 /*
306 * If this event is of different type we should not
307 * look further - we only suppress duplicate events
308 * that were sent back-to-back.
309 */
310 if (event->type != e->type)
311 break;
312
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800313 list_del_init(&e->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 gameport_free_event(e);
315 }
316 }
317
318 spin_unlock_irqrestore(&gameport_event_lock, flags);
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800322static void gameport_handle_events(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct gameport_event *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Arjan van de Ven286295e2006-02-19 00:22:03 -0500326 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Dmitry Torokhov9e50afd2005-11-20 00:56:43 -0500328 /*
329 * Note that we handle only one event here to give swsusp
330 * a chance to freeze kgameportd thread. Gameport events
331 * should be pretty rare so we are not concerned about
332 * taking performance hit.
333 */
334 if ((event = gameport_get_event())) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 switch (event->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800338 case GAMEPORT_REGISTER_PORT:
339 gameport_add_port(event->object);
340 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800342 case GAMEPORT_ATTACH_DRIVER:
343 gameport_attach_driver(event->object);
344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 gameport_remove_duplicate_events(event);
348 gameport_free_event(event);
349 }
350
Arjan van de Ven286295e2006-02-19 00:22:03 -0500351 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800354static DECLARE_WORK(gameport_event_work, gameport_handle_events);
355
356static int gameport_queue_event(void *object, struct module *owner,
357 enum gameport_event_type event_type)
358{
359 unsigned long flags;
360 struct gameport_event *event;
361 int retval = 0;
362
363 spin_lock_irqsave(&gameport_event_lock, flags);
364
365 /*
366 * Scan event list for the other events for the same gameport port,
367 * starting with the most recent one. If event is the same we
368 * do not need add new one. If event is of different type we
369 * need to add this event and should not look further because
370 * we need to preserve sequence of distinct events.
371 */
372 list_for_each_entry_reverse(event, &gameport_event_list, node) {
373 if (event->object == object) {
374 if (event->type == event_type)
375 goto out;
376 break;
377 }
378 }
379
380 event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC);
381 if (!event) {
382 pr_err("Not enough memory to queue event %d\n", event_type);
383 retval = -ENOMEM;
384 goto out;
385 }
386
387 if (!try_module_get(owner)) {
Joe Perchesfef5f562017-03-17 17:15:38 -0700388 pr_warn("Can't get module reference, dropping event %d\n",
389 event_type);
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800390 kfree(event);
391 retval = -EINVAL;
392 goto out;
393 }
394
395 event->type = event_type;
396 event->object = object;
397 event->owner = owner;
398
399 list_add_tail(&event->node, &gameport_event_list);
Dmitry Torokhov1d64b652011-02-23 08:51:28 -0800400 queue_work(system_long_wq, &gameport_event_work);
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800401
402out:
403 spin_unlock_irqrestore(&gameport_event_lock, flags);
404 return retval;
405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/*
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400408 * Remove all events that have been submitted for a given object,
409 * be it a gameport port or a driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400411static void gameport_remove_pending_events(void *object)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800413 struct gameport_event *event, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 unsigned long flags;
415
416 spin_lock_irqsave(&gameport_event_lock, flags);
417
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800418 list_for_each_entry_safe(event, next, &gameport_event_list, node) {
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400419 if (event->object == object) {
Dmitry Torokhovd621af42010-01-05 17:56:03 -0800420 list_del_init(&event->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 gameport_free_event(event);
422 }
423 }
424
425 spin_unlock_irqrestore(&gameport_event_lock, flags);
426}
427
428/*
429 * Destroy child gameport port (if any) that has not been fully registered yet.
430 *
431 * Note that we rely on the fact that port can have only one child and therefore
432 * only one child registration request can be pending. Additionally, children
433 * are registered by driver's connect() handler so there can't be a grandchild
434 * pending registration together with a child.
435 */
436static struct gameport *gameport_get_pending_child(struct gameport *parent)
437{
438 struct gameport_event *event;
439 struct gameport *gameport, *child = NULL;
440 unsigned long flags;
441
442 spin_lock_irqsave(&gameport_event_lock, flags);
443
444 list_for_each_entry(event, &gameport_event_list, node) {
445 if (event->type == GAMEPORT_REGISTER_PORT) {
446 gameport = event->object;
447 if (gameport->parent == parent) {
448 child = gameport;
449 break;
450 }
451 }
452 }
453
454 spin_unlock_irqrestore(&gameport_event_lock, flags);
455 return child;
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458/*
459 * Gameport port operations
460 */
461
Greg Kroah-Hartman0cba7de2013-10-07 18:27:37 -0700462static ssize_t gameport_description_show(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct gameport *gameport = to_gameport_port(dev);
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return sprintf(buf, "%s\n", gameport->name);
467}
Greg Kroah-Hartman0cba7de2013-10-07 18:27:37 -0700468static DEVICE_ATTR(description, S_IRUGO, gameport_description_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Greg Kroah-Hartman0cba7de2013-10-07 18:27:37 -0700470static ssize_t drvctl_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct gameport *gameport = to_gameport_port(dev);
473 struct device_driver *drv;
Dmitry Torokhov79580052007-04-10 00:40:48 -0400474 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Dmitry Torokhov79580052007-04-10 00:40:48 -0400476 error = mutex_lock_interruptible(&gameport_mutex);
477 if (error)
478 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (!strncmp(buf, "none", count)) {
481 gameport_disconnect_port(gameport);
482 } else if (!strncmp(buf, "reconnect", count)) {
483 gameport_reconnect_port(gameport);
484 } else if (!strncmp(buf, "rescan", count)) {
485 gameport_disconnect_port(gameport);
486 gameport_find_driver(gameport);
487 } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
488 gameport_disconnect_port(gameport);
Dmitry Torokhov79580052007-04-10 00:40:48 -0400489 error = gameport_bind_driver(gameport, to_gameport_driver(drv));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 } else {
Dmitry Torokhov79580052007-04-10 00:40:48 -0400491 error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Arjan van de Ven286295e2006-02-19 00:22:03 -0500494 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Dmitry Torokhov79580052007-04-10 00:40:48 -0400496 return error ? error : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
Greg Kroah-Hartman0cba7de2013-10-07 18:27:37 -0700498static DEVICE_ATTR_WO(drvctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Greg Kroah-Hartman0cba7de2013-10-07 18:27:37 -0700500static struct attribute *gameport_device_attrs[] = {
501 &dev_attr_description.attr,
502 &dev_attr_drvctl.attr,
503 NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504};
Greg Kroah-Hartman0cba7de2013-10-07 18:27:37 -0700505ATTRIBUTE_GROUPS(gameport_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507static 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
515void 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}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700523EXPORT_SYMBOL(gameport_set_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525/*
526 * Prepare gameport port for registration.
527 */
528static void gameport_init_port(struct gameport *gameport)
529{
Aniroop Mathur939ffb12014-12-03 14:27:42 -0800530 static atomic_t gameport_no = ATOMIC_INIT(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 __module_get(THIS_MODULE);
533
Arjan van de Ven286295e2006-02-19 00:22:03 -0500534 mutex_init(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 device_initialize(&gameport->dev);
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800536 dev_set_name(&gameport->dev, "gameport%lu",
Aniroop Mathur939ffb12014-12-03 14:27:42 -0800537 (unsigned long)atomic_inc_return(&gameport_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 gameport->dev.bus = &gameport_bus;
539 gameport->dev.release = gameport_release_port;
540 if (gameport->parent)
541 gameport->dev.parent = &gameport->parent->dev;
542
Randy Dunlap73b59a32006-07-19 01:14:55 -0400543 INIT_LIST_HEAD(&gameport->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 spin_lock_init(&gameport->timer_lock);
Geliang Tangd8f2bed2017-04-10 20:37:53 -0700545 setup_timer(&gameport->poll_timer, gameport_run_poll_handler,
546 (unsigned long)gameport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/*
550 * Complete gameport port registration.
551 * Driver core will attempt to find appropriate driver for the port.
552 */
553static void gameport_add_port(struct gameport *gameport)
554{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400555 int error;
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (gameport->parent)
558 gameport->parent->child = gameport;
559
Takashi Iwai76460a72014-09-11 10:28:30 -0700560 gameport->speed = use_ktime ?
561 gameport_measure_speed(gameport) :
562 old_gameport_measure_speed(gameport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 list_add_tail(&gameport->node, &gameport_list);
565
566 if (gameport->io)
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800567 dev_info(&gameport->dev, "%s is %s, io %#x, speed %dkHz\n",
568 gameport->name, gameport->phys, gameport->io, gameport->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 else
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800570 dev_info(&gameport->dev, "%s is %s, speed %dkHz\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 gameport->name, gameport->phys, gameport->speed);
572
Randy Dunlap73b59a32006-07-19 01:14:55 -0400573 error = device_add(&gameport->dev);
574 if (error)
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800575 dev_err(&gameport->dev,
576 "device_add() failed for %s (%s), error: %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400577 gameport->phys, gameport->name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/*
581 * gameport_destroy_port() completes deregistration process and removes
582 * port from the system
583 */
584static 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
Dmitry Torokhov361b7b52010-01-05 17:56:03 -0800599 if (device_is_registered(&gameport->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 device_del(&gameport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Randy Dunlap73b59a32006-07-19 01:14:55 -0400602 list_del_init(&gameport->node);
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 gameport_remove_pending_events(gameport);
605 put_device(&gameport->dev);
606}
607
608/*
609 * Reconnect gameport port and all its children (re-initialize attached devices)
610 */
611static void gameport_reconnect_port(struct gameport *gameport)
612{
613 do {
614 if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
615 gameport_disconnect_port(gameport);
616 gameport_find_driver(gameport);
617 /* Ok, old children are now gone, we are done */
618 break;
619 }
620 gameport = gameport->child;
621 } while (gameport);
622}
623
624/*
625 * gameport_disconnect_port() unbinds a port from its driver. As a side effect
626 * all child ports are unbound and destroyed.
627 */
628static void gameport_disconnect_port(struct gameport *gameport)
629{
630 struct gameport *s, *parent;
631
632 if (gameport->child) {
633 /*
634 * Children ports should be disconnected and destroyed
635 * first, staring with the leaf one, since we don't want
636 * to do recursion
637 */
638 for (s = gameport; s->child; s = s->child)
639 /* empty */;
640
641 do {
642 parent = s->parent;
643
Dmitry Torokhov79580052007-04-10 00:40:48 -0400644 device_release_driver(&s->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 gameport_destroy_port(s);
646 } while ((s = parent) != gameport);
647 }
648
649 /*
650 * Ok, no children left, now disconnect this port
651 */
Dmitry Torokhov79580052007-04-10 00:40:48 -0400652 device_release_driver(&gameport->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653}
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
656 * Submits register request to kgameportd for subsequent execution.
657 * Note that port registration is always asynchronous.
658 */
659void __gameport_register_port(struct gameport *gameport, struct module *owner)
660{
661 gameport_init_port(gameport);
662 gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
663}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700664EXPORT_SYMBOL(__gameport_register_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666/*
667 * Synchronously unregisters gameport port.
668 */
669void gameport_unregister_port(struct gameport *gameport)
670{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500671 mutex_lock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 gameport_disconnect_port(gameport);
673 gameport_destroy_port(gameport);
Arjan van de Ven286295e2006-02-19 00:22:03 -0500674 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700676EXPORT_SYMBOL(gameport_unregister_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678
679/*
680 * Gameport driver operations
681 */
682
Greg Kroah-Hartman5e9e4912013-08-23 14:24:33 -0700683static ssize_t description_show(struct device_driver *drv, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
685 struct gameport_driver *driver = to_gameport_driver(drv);
686 return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
687}
Greg Kroah-Hartman5e9e4912013-08-23 14:24:33 -0700688static DRIVER_ATTR_RO(description);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Greg Kroah-Hartman5e9e4912013-08-23 14:24:33 -0700690static struct attribute *gameport_driver_attrs[] = {
691 &driver_attr_description.attr,
692 NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693};
Greg Kroah-Hartman5e9e4912013-08-23 14:24:33 -0700694ATTRIBUTE_GROUPS(gameport_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696static int gameport_driver_probe(struct device *dev)
697{
698 struct gameport *gameport = to_gameport_port(dev);
699 struct gameport_driver *drv = to_gameport_driver(dev->driver);
700
701 drv->connect(gameport, drv);
702 return gameport->drv ? 0 : -ENODEV;
703}
704
705static int gameport_driver_remove(struct device *dev)
706{
707 struct gameport *gameport = to_gameport_port(dev);
708 struct gameport_driver *drv = to_gameport_driver(dev->driver);
709
710 drv->disconnect(gameport);
711 return 0;
712}
713
Dmitry Torokhov4ced8e72009-04-13 15:27:49 -0700714static void gameport_attach_driver(struct gameport_driver *drv)
Randy Dunlap73b59a32006-07-19 01:14:55 -0400715{
716 int error;
717
Dmitry Torokhov4ced8e72009-04-13 15:27:49 -0700718 error = driver_attach(&drv->driver);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400719 if (error)
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800720 pr_err("driver_attach() failed for %s, error: %d\n",
Randy Dunlap73b59a32006-07-19 01:14:55 -0400721 drv->driver.name, error);
722}
723
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400724int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
725 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400727 int error;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 drv->driver.bus = &gameport_bus;
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400730 drv->driver.owner = owner;
731 drv->driver.mod_name = mod_name;
732
733 /*
734 * Temporarily disable automatic binding because probing
735 * takes long time and we are better off doing it in kgameportd
736 */
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700737 drv->ignore = true;
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400738
739 error = driver_register(&drv->driver);
740 if (error) {
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800741 pr_err("driver_register() failed for %s, error: %d\n",
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400742 drv->driver.name, error);
743 return error;
744 }
745
746 /*
747 * Reset ignore flag and let kgameportd bind the driver to free ports
748 */
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700749 drv->ignore = false;
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400750 error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
751 if (error) {
752 driver_unregister(&drv->driver);
753 return error;
754 }
755
756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700758EXPORT_SYMBOL(__gameport_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760void gameport_unregister_driver(struct gameport_driver *drv)
761{
762 struct gameport *gameport;
763
Arjan van de Ven286295e2006-02-19 00:22:03 -0500764 mutex_lock(&gameport_mutex);
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400765
Dmitry Torokhov7e044e02009-05-09 16:08:05 -0700766 drv->ignore = true; /* so gameport_find_driver ignores it */
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400767 gameport_remove_pending_events(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769start_over:
770 list_for_each_entry(gameport, &gameport_list, node) {
771 if (gameport->drv == drv) {
772 gameport_disconnect_port(gameport);
773 gameport_find_driver(gameport);
774 /* we could've deleted some ports, restart */
775 goto start_over;
776 }
777 }
778
779 driver_unregister(&drv->driver);
Dmitry Torokhov6902c0b2008-06-06 01:33:22 -0400780
Arjan van de Ven286295e2006-02-19 00:22:03 -0500781 mutex_unlock(&gameport_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700783EXPORT_SYMBOL(gameport_unregister_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785static int gameport_bus_match(struct device *dev, struct device_driver *drv)
786{
787 struct gameport_driver *gameport_drv = to_gameport_driver(drv);
788
789 return !gameport_drv->ignore;
790}
791
Dmitry Torokhovb187dd72006-11-02 23:27:30 -0500792static struct bus_type gameport_bus = {
793 .name = "gameport",
Greg Kroah-Hartman0cba7de2013-10-07 18:27:37 -0700794 .dev_groups = gameport_device_groups,
Greg Kroah-Hartman5e9e4912013-08-23 14:24:33 -0700795 .drv_groups = gameport_driver_groups,
Dmitry Torokhovb187dd72006-11-02 23:27:30 -0500796 .match = gameport_bus_match,
797 .probe = gameport_driver_probe,
798 .remove = gameport_driver_remove,
799};
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
802{
Arjan van de Ven286295e2006-02-19 00:22:03 -0500803 mutex_lock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 gameport->drv = drv;
Arjan van de Ven286295e2006-02-19 00:22:03 -0500805 mutex_unlock(&gameport->drv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
809{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (gameport->open) {
811 if (gameport->open(gameport, mode)) {
812 return -1;
813 }
814 } else {
815 if (mode != GAMEPORT_MODE_RAW)
816 return -1;
817 }
818
819 gameport_set_drv(gameport, drv);
820 return 0;
821}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700822EXPORT_SYMBOL(gameport_open);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824void gameport_close(struct gameport *gameport)
825{
826 del_timer_sync(&gameport->poll_timer);
827 gameport->poll_handler = NULL;
828 gameport->poll_interval = 0;
829 gameport_set_drv(gameport, NULL);
830 if (gameport->close)
831 gameport->close(gameport);
832}
Dmitry Torokhov3e650672009-04-17 20:12:05 -0700833EXPORT_SYMBOL(gameport_close);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835static int __init gameport_init(void)
836{
Randy Dunlap73b59a32006-07-19 01:14:55 -0400837 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Randy Dunlap73b59a32006-07-19 01:14:55 -0400839 error = bus_register(&gameport_bus);
840 if (error) {
Dmitry Torokhovfc99ec62010-01-05 17:56:03 -0800841 pr_err("failed to register gameport bus, error: %d\n", error);
Randy Dunlap73b59a32006-07-19 01:14:55 -0400842 return error;
843 }
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 return 0;
847}
848
849static void __exit gameport_exit(void)
850{
851 bus_unregister(&gameport_bus);
Dmitry Torokhovc44f2422010-11-15 01:39:57 -0800852
853 /*
854 * There should not be any outstanding events but work may
855 * still be scheduled so simply cancel it.
856 */
857 cancel_work_sync(&gameport_event_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Dmitry Torokhov51c38f92006-02-19 00:22:51 -0500860subsys_initcall(gameport_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861module_exit(gameport_exit);