xref: /linux/drivers/input/gameport/gameport.c (revision be54f8c558027a218423134dd9b8c7c46d92204a)
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/io.h>
15 #include <linux/ioport.h>
16 #include <linux/init.h>
17 #include <linux/gameport.h>
18 #include <linux/slab.h>
19 #include <linux/delay.h>
20 #include <linux/workqueue.h>
21 #include <linux/sched.h>	/* HZ */
22 #include <linux/mutex.h>
23 #include <linux/timekeeping.h>
24 
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
26 MODULE_DESCRIPTION("Generic gameport layer");
27 MODULE_LICENSE("GPL");
28 
29 static bool use_ktime = true;
30 module_param(use_ktime, bool, 0400);
31 MODULE_PARM_DESC(use_ktime, "Use ktime for measuring I/O speed");
32 
33 /*
34  * gameport_mutex protects entire gameport subsystem and is taken
35  * every time gameport port or driver registrered or unregistered.
36  */
37 static DEFINE_MUTEX(gameport_mutex);
38 
39 static LIST_HEAD(gameport_list);
40 
41 static const struct bus_type gameport_bus;
42 
43 static void gameport_add_port(struct gameport *gameport);
44 static void gameport_attach_driver(struct gameport_driver *drv);
45 static void gameport_reconnect_port(struct gameport *gameport);
46 static 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 
get_time_pit(void)55 static 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 
gameport_measure_speed(struct gameport * gameport)77 static int gameport_measure_speed(struct gameport *gameport)
78 {
79 	unsigned int i, t, tx;
80 	u64 t1, t2, t3;
81 	unsigned long flags;
82 
83 	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
84 		return 0;
85 
86 	tx = ~0;
87 
88 	for (i = 0; i < 50; i++) {
89 		local_irq_save(flags);
90 		t1 = ktime_get_ns();
91 		for (t = 0; t < 50; t++)
92 			gameport_read(gameport);
93 		t2 = ktime_get_ns();
94 		t3 = ktime_get_ns();
95 		local_irq_restore(flags);
96 		udelay(i * 10);
97 		t = (t2 - t1) - (t3 - t2);
98 		if (t < tx)
99 			tx = t;
100 	}
101 
102 	gameport_close(gameport);
103 	t = 1000000 * 50;
104 	if (tx)
105 		t /= tx;
106 	return t;
107 }
108 
old_gameport_measure_speed(struct gameport * gameport)109 static int old_gameport_measure_speed(struct gameport *gameport)
110 {
111 #if defined(__i386__)
112 
113 	unsigned int i, t, t1, t2, t3, tx;
114 	unsigned long flags;
115 
116 	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
117 		return 0;
118 
119 	tx = 1 << 30;
120 
121 	for(i = 0; i < 50; i++) {
122 		local_irq_save(flags);
123 		GET_TIME(t1);
124 		for (t = 0; t < 50; t++) gameport_read(gameport);
125 		GET_TIME(t2);
126 		GET_TIME(t3);
127 		local_irq_restore(flags);
128 		udelay(i * 10);
129 		if ((t = DELTA(t2,t1) - DELTA(t3,t2)) < tx) tx = t;
130 	}
131 
132 	gameport_close(gameport);
133 	return 59659 / (tx < 1 ? 1 : tx);
134 
135 #elif defined (__x86_64__)
136 
137 	unsigned int i, t;
138 	unsigned long tx, t1, t2, flags;
139 
140 	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
141 		return 0;
142 
143 	tx = 1 << 30;
144 
145 	for(i = 0; i < 50; i++) {
146 		local_irq_save(flags);
147 		t1 = rdtsc();
148 		for (t = 0; t < 50; t++) gameport_read(gameport);
149 		t2 = rdtsc();
150 		local_irq_restore(flags);
151 		udelay(i * 10);
152 		if (t2 - t1 < tx) tx = t2 - t1;
153 	}
154 
155 	gameport_close(gameport);
156 	return (this_cpu_read(cpu_info.loops_per_jiffy) *
157 		(unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
158 
159 #else
160 
161 	unsigned int j, t = 0;
162 
163 	if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
164 		return 0;
165 
166 	j = jiffies; while (j == jiffies);
167 	j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
168 
169 	gameport_close(gameport);
170 	return t * HZ / 1000;
171 
172 #endif
173 }
174 
gameport_start_polling(struct gameport * gameport)175 void gameport_start_polling(struct gameport *gameport)
176 {
177 	spin_lock(&gameport->timer_lock);
178 
179 	if (!gameport->poll_cnt++) {
180 		BUG_ON(!gameport->poll_handler);
181 		BUG_ON(!gameport->poll_interval);
182 		mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
183 	}
184 
185 	spin_unlock(&gameport->timer_lock);
186 }
187 EXPORT_SYMBOL(gameport_start_polling);
188 
gameport_stop_polling(struct gameport * gameport)189 void gameport_stop_polling(struct gameport *gameport)
190 {
191 	spin_lock(&gameport->timer_lock);
192 
193 	if (!--gameport->poll_cnt)
194 		timer_delete(&gameport->poll_timer);
195 
196 	spin_unlock(&gameport->timer_lock);
197 }
198 EXPORT_SYMBOL(gameport_stop_polling);
199 
gameport_run_poll_handler(struct timer_list * t)200 static void gameport_run_poll_handler(struct timer_list *t)
201 {
202 	struct gameport *gameport = timer_container_of(gameport, t,
203 						       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 
gameport_bind_driver(struct gameport * gameport,struct gameport_driver * drv)214 static 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 
gameport_find_driver(struct gameport * gameport)238 static 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 
254 enum gameport_event_type {
255 	GAMEPORT_REGISTER_PORT,
256 	GAMEPORT_ATTACH_DRIVER,
257 };
258 
259 struct gameport_event {
260 	enum gameport_event_type type;
261 	void *object;
262 	struct module *owner;
263 	struct list_head node;
264 };
265 
266 static DEFINE_SPINLOCK(gameport_event_lock);	/* protects gameport_event_list */
267 static LIST_HEAD(gameport_event_list);
268 
gameport_get_event(void)269 static 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 
gameport_free_event(struct gameport_event * event)286 static void gameport_free_event(struct gameport_event *event)
287 {
288 	module_put(event->owner);
289 	kfree(event);
290 }
291 
gameport_remove_duplicate_events(struct gameport_event * event)292 static 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 
gameport_handle_events(struct work_struct * work)318 static 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 
350 static DECLARE_WORK(gameport_event_work, gameport_handle_events);
351 
gameport_queue_event(void * object,struct module * owner,enum gameport_event_type event_type)352 static 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(*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 
398 out:
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  */
gameport_remove_pending_events(void * object)407 static 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  */
gameport_get_pending_child(struct gameport * parent)432 static 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 
gameport_description_show(struct device * dev,struct device_attribute * attr,char * buf)458 static 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 }
464 static DEVICE_ATTR(description, S_IRUGO, gameport_description_show, NULL);
465 
drvctl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)466 static 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 }
494 static DEVICE_ATTR_WO(drvctl);
495 
496 static struct attribute *gameport_device_attrs[] = {
497 	&dev_attr_description.attr,
498 	&dev_attr_drvctl.attr,
499 	NULL,
500 };
501 ATTRIBUTE_GROUPS(gameport_device);
502 
gameport_release_port(struct device * dev)503 static 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 
gameport_set_phys(struct gameport * gameport,const char * fmt,...)511 void 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 }
519 EXPORT_SYMBOL(gameport_set_phys);
520 
gameport_default_trigger(struct gameport * gameport)521 static void gameport_default_trigger(struct gameport *gameport)
522 {
523 #ifdef CONFIG_HAS_IOPORT
524 	outb(0xff, gameport->io);
525 #endif
526 }
527 
gameport_default_read(struct gameport * gameport)528 static unsigned char gameport_default_read(struct gameport *gameport)
529 {
530 #ifdef CONFIG_HAS_IOPORT
531 	return inb(gameport->io);
532 #else
533 	return 0xff;
534 #endif
535 }
536 
gameport_setup_default_handlers(struct gameport * gameport)537 static void gameport_setup_default_handlers(struct gameport *gameport)
538 {
539 	if ((!gameport->trigger || !gameport->read) &&
540 	    !IS_ENABLED(CONFIG_HAS_IOPORT))
541 		dev_err(&gameport->dev,
542 			"I/O port access is required for %s (%s) but is not available\n",
543 			gameport->phys, gameport->name);
544 
545 	if (!gameport->trigger)
546 		gameport->trigger = gameport_default_trigger;
547 	if (!gameport->read)
548 		gameport->read = gameport_default_read;
549 }
550 
551 /*
552  * Prepare gameport port for registration.
553  */
gameport_init_port(struct gameport * gameport)554 static void gameport_init_port(struct gameport *gameport)
555 {
556 	static atomic_t gameport_no = ATOMIC_INIT(-1);
557 
558 	__module_get(THIS_MODULE);
559 
560 	mutex_init(&gameport->drv_mutex);
561 	device_initialize(&gameport->dev);
562 	dev_set_name(&gameport->dev, "gameport%lu",
563 			(unsigned long)atomic_inc_return(&gameport_no));
564 	gameport->dev.bus = &gameport_bus;
565 	gameport->dev.release = gameport_release_port;
566 	if (gameport->parent)
567 		gameport->dev.parent = &gameport->parent->dev;
568 
569 	gameport_setup_default_handlers(gameport);
570 	INIT_LIST_HEAD(&gameport->node);
571 	spin_lock_init(&gameport->timer_lock);
572 	timer_setup(&gameport->poll_timer, gameport_run_poll_handler, 0);
573 }
574 
575 /*
576  * Complete gameport port registration.
577  * Driver core will attempt to find appropriate driver for the port.
578  */
gameport_add_port(struct gameport * gameport)579 static void gameport_add_port(struct gameport *gameport)
580 {
581 	int error;
582 
583 	if (gameport->parent)
584 		gameport->parent->child = gameport;
585 
586 	gameport->speed = use_ktime ?
587 		gameport_measure_speed(gameport) :
588 		old_gameport_measure_speed(gameport);
589 
590 	list_add_tail(&gameport->node, &gameport_list);
591 
592 	if (gameport->io)
593 		dev_info(&gameport->dev, "%s is %s, io %#x, speed %dkHz\n",
594 			 gameport->name, gameport->phys, gameport->io, gameport->speed);
595 	else
596 		dev_info(&gameport->dev, "%s is %s, speed %dkHz\n",
597 			gameport->name, gameport->phys, gameport->speed);
598 
599 	error = device_add(&gameport->dev);
600 	if (error)
601 		dev_err(&gameport->dev,
602 			"device_add() failed for %s (%s), error: %d\n",
603 			gameport->phys, gameport->name, error);
604 }
605 
606 /*
607  * gameport_destroy_port() completes deregistration process and removes
608  * port from the system
609  */
gameport_destroy_port(struct gameport * gameport)610 static void gameport_destroy_port(struct gameport *gameport)
611 {
612 	struct gameport *child;
613 
614 	child = gameport_get_pending_child(gameport);
615 	if (child) {
616 		gameport_remove_pending_events(child);
617 		put_device(&child->dev);
618 	}
619 
620 	if (gameport->parent) {
621 		gameport->parent->child = NULL;
622 		gameport->parent = NULL;
623 	}
624 
625 	if (device_is_registered(&gameport->dev))
626 		device_del(&gameport->dev);
627 
628 	list_del_init(&gameport->node);
629 
630 	gameport_remove_pending_events(gameport);
631 	put_device(&gameport->dev);
632 }
633 
634 /*
635  * Reconnect gameport port and all its children (re-initialize attached devices)
636  */
gameport_reconnect_port(struct gameport * gameport)637 static void gameport_reconnect_port(struct gameport *gameport)
638 {
639 	do {
640 		if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
641 			gameport_disconnect_port(gameport);
642 			gameport_find_driver(gameport);
643 			/* Ok, old children are now gone, we are done */
644 			break;
645 		}
646 		gameport = gameport->child;
647 	} while (gameport);
648 }
649 
650 /*
651  * gameport_disconnect_port() unbinds a port from its driver. As a side effect
652  * all child ports are unbound and destroyed.
653  */
gameport_disconnect_port(struct gameport * gameport)654 static void gameport_disconnect_port(struct gameport *gameport)
655 {
656 	struct gameport *s, *parent;
657 
658 	if (gameport->child) {
659 		/*
660 		 * Children ports should be disconnected and destroyed
661 		 * first, staring with the leaf one, since we don't want
662 		 * to do recursion
663 		 */
664 		for (s = gameport; s->child; s = s->child)
665 			/* empty */;
666 
667 		do {
668 			parent = s->parent;
669 
670 			device_release_driver(&s->dev);
671 			gameport_destroy_port(s);
672 		} while ((s = parent) != gameport);
673 	}
674 
675 	/*
676 	 * Ok, no children left, now disconnect this port
677 	 */
678 	device_release_driver(&gameport->dev);
679 }
680 
681 /*
682  * Submits register request to kgameportd for subsequent execution.
683  * Note that port registration is always asynchronous.
684  */
__gameport_register_port(struct gameport * gameport,struct module * owner)685 void __gameport_register_port(struct gameport *gameport, struct module *owner)
686 {
687 	gameport_init_port(gameport);
688 	gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
689 }
690 EXPORT_SYMBOL(__gameport_register_port);
691 
692 /*
693  * Synchronously unregisters gameport port.
694  */
gameport_unregister_port(struct gameport * gameport)695 void gameport_unregister_port(struct gameport *gameport)
696 {
697 	mutex_lock(&gameport_mutex);
698 	gameport_disconnect_port(gameport);
699 	gameport_destroy_port(gameport);
700 	mutex_unlock(&gameport_mutex);
701 }
702 EXPORT_SYMBOL(gameport_unregister_port);
703 
704 
705 /*
706  * Gameport driver operations
707  */
708 
description_show(struct device_driver * drv,char * buf)709 static ssize_t description_show(struct device_driver *drv, char *buf)
710 {
711 	struct gameport_driver *driver = to_gameport_driver(drv);
712 	return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
713 }
714 static DRIVER_ATTR_RO(description);
715 
716 static struct attribute *gameport_driver_attrs[] = {
717 	&driver_attr_description.attr,
718 	NULL
719 };
720 ATTRIBUTE_GROUPS(gameport_driver);
721 
gameport_driver_probe(struct device * dev)722 static int gameport_driver_probe(struct device *dev)
723 {
724 	struct gameport *gameport = to_gameport_port(dev);
725 	struct gameport_driver *drv = to_gameport_driver(dev->driver);
726 
727 	drv->connect(gameport, drv);
728 	return gameport->drv ? 0 : -ENODEV;
729 }
730 
gameport_driver_remove(struct device * dev)731 static void gameport_driver_remove(struct device *dev)
732 {
733 	struct gameport *gameport = to_gameport_port(dev);
734 	struct gameport_driver *drv = to_gameport_driver(dev->driver);
735 
736 	drv->disconnect(gameport);
737 }
738 
gameport_attach_driver(struct gameport_driver * drv)739 static void gameport_attach_driver(struct gameport_driver *drv)
740 {
741 	int error;
742 
743 	error = driver_attach(&drv->driver);
744 	if (error)
745 		pr_err("driver_attach() failed for %s, error: %d\n",
746 			drv->driver.name, error);
747 }
748 
__gameport_register_driver(struct gameport_driver * drv,struct module * owner,const char * mod_name)749 int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
750 				const char *mod_name)
751 {
752 	int error;
753 
754 	drv->driver.bus = &gameport_bus;
755 	drv->driver.owner = owner;
756 	drv->driver.mod_name = mod_name;
757 
758 	/*
759 	 * Temporarily disable automatic binding because probing
760 	 * takes long time and we are better off doing it in kgameportd
761 	 */
762 	drv->ignore = true;
763 
764 	error = driver_register(&drv->driver);
765 	if (error) {
766 		pr_err("driver_register() failed for %s, error: %d\n",
767 			drv->driver.name, error);
768 		return error;
769 	}
770 
771 	/*
772 	 * Reset ignore flag and let kgameportd bind the driver to free ports
773 	 */
774 	drv->ignore = false;
775 	error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
776 	if (error) {
777 		driver_unregister(&drv->driver);
778 		return error;
779 	}
780 
781 	return 0;
782 }
783 EXPORT_SYMBOL(__gameport_register_driver);
784 
gameport_unregister_driver(struct gameport_driver * drv)785 void gameport_unregister_driver(struct gameport_driver *drv)
786 {
787 	struct gameport *gameport;
788 
789 	mutex_lock(&gameport_mutex);
790 
791 	drv->ignore = true;	/* so gameport_find_driver ignores it */
792 	gameport_remove_pending_events(drv);
793 
794 start_over:
795 	list_for_each_entry(gameport, &gameport_list, node) {
796 		if (gameport->drv == drv) {
797 			gameport_disconnect_port(gameport);
798 			gameport_find_driver(gameport);
799 			/* we could've deleted some ports, restart */
800 			goto start_over;
801 		}
802 	}
803 
804 	driver_unregister(&drv->driver);
805 
806 	mutex_unlock(&gameport_mutex);
807 }
808 EXPORT_SYMBOL(gameport_unregister_driver);
809 
gameport_bus_match(struct device * dev,const struct device_driver * drv)810 static int gameport_bus_match(struct device *dev, const struct device_driver *drv)
811 {
812 	const struct gameport_driver *gameport_drv = to_gameport_driver(drv);
813 
814 	return !gameport_drv->ignore;
815 }
816 
817 static const struct bus_type gameport_bus = {
818 	.name		= "gameport",
819 	.dev_groups	= gameport_device_groups,
820 	.drv_groups	= gameport_driver_groups,
821 	.match		= gameport_bus_match,
822 	.probe		= gameport_driver_probe,
823 	.remove		= gameport_driver_remove,
824 };
825 
gameport_set_drv(struct gameport * gameport,struct gameport_driver * drv)826 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
827 {
828 	mutex_lock(&gameport->drv_mutex);
829 	gameport->drv = drv;
830 	mutex_unlock(&gameport->drv_mutex);
831 }
832 
gameport_open(struct gameport * gameport,struct gameport_driver * drv,int mode)833 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
834 {
835 	if (gameport->open) {
836 		if (gameport->open(gameport, mode)) {
837 			return -1;
838 		}
839 	} else {
840 		if (mode != GAMEPORT_MODE_RAW)
841 			return -1;
842 	}
843 
844 	gameport_set_drv(gameport, drv);
845 	return 0;
846 }
847 EXPORT_SYMBOL(gameport_open);
848 
gameport_close(struct gameport * gameport)849 void gameport_close(struct gameport *gameport)
850 {
851 	timer_delete_sync(&gameport->poll_timer);
852 	gameport->poll_handler = NULL;
853 	gameport->poll_interval = 0;
854 	gameport_set_drv(gameport, NULL);
855 	if (gameport->close)
856 		gameport->close(gameport);
857 }
858 EXPORT_SYMBOL(gameport_close);
859 
gameport_init(void)860 static int __init gameport_init(void)
861 {
862 	int error;
863 
864 	error = bus_register(&gameport_bus);
865 	if (error) {
866 		pr_err("failed to register gameport bus, error: %d\n", error);
867 		return error;
868 	}
869 
870 
871 	return 0;
872 }
873 
gameport_exit(void)874 static void __exit gameport_exit(void)
875 {
876 	bus_unregister(&gameport_bus);
877 
878 	/*
879 	 * There should not be any outstanding events but work may
880 	 * still be scheduled so simply cancel it.
881 	 */
882 	cancel_work_sync(&gameport_event_work);
883 }
884 
885 subsys_initcall(gameport_init);
886 module_exit(gameport_exit);
887