File Index
Files and directories
Sign in
FILE

gameport.c

File preview with syntax highlighting.

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