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