Linux Audio

Check our new training course

Yocto / OpenEmbedded training

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