Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gameport.c
Go to the documentation of this file.
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 
29 MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");
30 MODULE_DESCRIPTION("Generic gameport layer");
31 MODULE_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  */
37 static DEFINE_MUTEX(gameport_mutex);
38 
39 static LIST_HEAD(gameport_list);
40 
41 static 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 
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 
77 static 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 
143 void 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 }
156 
157 void 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 }
167 
168 static 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 
181 static 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 
205 static 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 
224 };
225 
228  void *object;
229  struct module *owner;
230  struct list_head node;
231 };
232 
233 static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
234 static LIST_HEAD(gameport_event_list);
235 
236 static 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 
253 static void gameport_free_event(struct gameport_event *event)
254 {
255  module_put(event->owner);
256  kfree(event);
257 }
258 
259 static 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 
285 static 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 
302  gameport_add_port(event->object);
303  break;
304 
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 
317 static DECLARE_WORK(gameport_event_work, gameport_handle_events);
318 
319 static int gameport_queue_event(void *object, struct module *owner,
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 
365 out:
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  */
374 static 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  */
399 static 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 
425 static ssize_t gameport_show_description(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 }
431 
432 static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
433 {
434  struct gameport *gameport = to_gameport_port(dev);
435  struct device_driver *drv;
436  int error;
437 
438  error = mutex_lock_interruptible(&gameport_mutex);
439  if (error)
440  return error;
441 
442  if (!strncmp(buf, "none", count)) {
443  gameport_disconnect_port(gameport);
444  } else if (!strncmp(buf, "reconnect", count)) {
445  gameport_reconnect_port(gameport);
446  } else if (!strncmp(buf, "rescan", count)) {
447  gameport_disconnect_port(gameport);
448  gameport_find_driver(gameport);
449  } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
450  gameport_disconnect_port(gameport);
451  error = gameport_bind_driver(gameport, to_gameport_driver(drv));
452  } else {
453  error = -EINVAL;
454  }
455 
456  mutex_unlock(&gameport_mutex);
457 
458  return error ? error : count;
459 }
460 
461 static struct device_attribute gameport_device_attrs[] = {
462  __ATTR(description, S_IRUGO, gameport_show_description, NULL),
463  __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
465 };
466 
467 static void gameport_release_port(struct device *dev)
468 {
469  struct gameport *gameport = to_gameport_port(dev);
470 
471  kfree(gameport);
472  module_put(THIS_MODULE);
473 }
474 
475 void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
476 {
477  va_list args;
478 
479  va_start(args, fmt);
480  vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
481  va_end(args);
482 }
484 
485 /*
486  * Prepare gameport port for registration.
487  */
488 static void gameport_init_port(struct gameport *gameport)
489 {
490  static atomic_t gameport_no = ATOMIC_INIT(0);
491 
492  __module_get(THIS_MODULE);
493 
494  mutex_init(&gameport->drv_mutex);
495  device_initialize(&gameport->dev);
496  dev_set_name(&gameport->dev, "gameport%lu",
497  (unsigned long)atomic_inc_return(&gameport_no) - 1);
498  gameport->dev.bus = &gameport_bus;
499  gameport->dev.release = gameport_release_port;
500  if (gameport->parent)
501  gameport->dev.parent = &gameport->parent->dev;
502 
503  INIT_LIST_HEAD(&gameport->node);
504  spin_lock_init(&gameport->timer_lock);
505  init_timer(&gameport->poll_timer);
506  gameport->poll_timer.function = gameport_run_poll_handler;
507  gameport->poll_timer.data = (unsigned long)gameport;
508 }
509 
510 /*
511  * Complete gameport port registration.
512  * Driver core will attempt to find appropriate driver for the port.
513  */
514 static void gameport_add_port(struct gameport *gameport)
515 {
516  int error;
517 
518  if (gameport->parent)
519  gameport->parent->child = gameport;
520 
521  gameport->speed = gameport_measure_speed(gameport);
522 
523  list_add_tail(&gameport->node, &gameport_list);
524 
525  if (gameport->io)
526  dev_info(&gameport->dev, "%s is %s, io %#x, speed %dkHz\n",
527  gameport->name, gameport->phys, gameport->io, gameport->speed);
528  else
529  dev_info(&gameport->dev, "%s is %s, speed %dkHz\n",
530  gameport->name, gameport->phys, gameport->speed);
531 
532  error = device_add(&gameport->dev);
533  if (error)
534  dev_err(&gameport->dev,
535  "device_add() failed for %s (%s), error: %d\n",
536  gameport->phys, gameport->name, error);
537 }
538 
539 /*
540  * gameport_destroy_port() completes deregistration process and removes
541  * port from the system
542  */
543 static void gameport_destroy_port(struct gameport *gameport)
544 {
545  struct gameport *child;
546 
547  child = gameport_get_pending_child(gameport);
548  if (child) {
549  gameport_remove_pending_events(child);
550  put_device(&child->dev);
551  }
552 
553  if (gameport->parent) {
554  gameport->parent->child = NULL;
555  gameport->parent = NULL;
556  }
557 
558  if (device_is_registered(&gameport->dev))
559  device_del(&gameport->dev);
560 
561  list_del_init(&gameport->node);
562 
563  gameport_remove_pending_events(gameport);
564  put_device(&gameport->dev);
565 }
566 
567 /*
568  * Reconnect gameport port and all its children (re-initialize attached devices)
569  */
570 static void gameport_reconnect_port(struct gameport *gameport)
571 {
572  do {
573  if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
574  gameport_disconnect_port(gameport);
575  gameport_find_driver(gameport);
576  /* Ok, old children are now gone, we are done */
577  break;
578  }
579  gameport = gameport->child;
580  } while (gameport);
581 }
582 
583 /*
584  * gameport_disconnect_port() unbinds a port from its driver. As a side effect
585  * all child ports are unbound and destroyed.
586  */
587 static void gameport_disconnect_port(struct gameport *gameport)
588 {
589  struct gameport *s, *parent;
590 
591  if (gameport->child) {
592  /*
593  * Children ports should be disconnected and destroyed
594  * first, staring with the leaf one, since we don't want
595  * to do recursion
596  */
597  for (s = gameport; s->child; s = s->child)
598  /* empty */;
599 
600  do {
601  parent = s->parent;
602 
604  gameport_destroy_port(s);
605  } while ((s = parent) != gameport);
606  }
607 
608  /*
609  * Ok, no children left, now disconnect this port
610  */
611  device_release_driver(&gameport->dev);
612 }
613 
614 /*
615  * Submits register request to kgameportd for subsequent execution.
616  * Note that port registration is always asynchronous.
617  */
618 void __gameport_register_port(struct gameport *gameport, struct module *owner)
619 {
620  gameport_init_port(gameport);
621  gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
622 }
624 
625 /*
626  * Synchronously unregisters gameport port.
627  */
628 void gameport_unregister_port(struct gameport *gameport)
629 {
630  mutex_lock(&gameport_mutex);
631  gameport_disconnect_port(gameport);
632  gameport_destroy_port(gameport);
633  mutex_unlock(&gameport_mutex);
634 }
636 
637 
638 /*
639  * Gameport driver operations
640  */
641 
642 static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
643 {
645  return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
646 }
647 
648 static struct driver_attribute gameport_driver_attrs[] = {
649  __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
651 };
652 
653 static int gameport_driver_probe(struct device *dev)
654 {
655  struct gameport *gameport = to_gameport_port(dev);
656  struct gameport_driver *drv = to_gameport_driver(dev->driver);
657 
658  drv->connect(gameport, drv);
659  return gameport->drv ? 0 : -ENODEV;
660 }
661 
662 static int gameport_driver_remove(struct device *dev)
663 {
664  struct gameport *gameport = to_gameport_port(dev);
665  struct gameport_driver *drv = to_gameport_driver(dev->driver);
666 
667  drv->disconnect(gameport);
668  return 0;
669 }
670 
671 static void gameport_attach_driver(struct gameport_driver *drv)
672 {
673  int error;
674 
675  error = driver_attach(&drv->driver);
676  if (error)
677  pr_err("driver_attach() failed for %s, error: %d\n",
678  drv->driver.name, error);
679 }
680 
681 int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
682  const char *mod_name)
683 {
684  int error;
685 
686  drv->driver.bus = &gameport_bus;
687  drv->driver.owner = owner;
688  drv->driver.mod_name = mod_name;
689 
690  /*
691  * Temporarily disable automatic binding because probing
692  * takes long time and we are better off doing it in kgameportd
693  */
694  drv->ignore = true;
695 
696  error = driver_register(&drv->driver);
697  if (error) {
698  pr_err("driver_register() failed for %s, error: %d\n",
699  drv->driver.name, error);
700  return error;
701  }
702 
703  /*
704  * Reset ignore flag and let kgameportd bind the driver to free ports
705  */
706  drv->ignore = false;
707  error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
708  if (error) {
709  driver_unregister(&drv->driver);
710  return error;
711  }
712 
713  return 0;
714 }
716 
718 {
719  struct gameport *gameport;
720 
721  mutex_lock(&gameport_mutex);
722 
723  drv->ignore = true; /* so gameport_find_driver ignores it */
724  gameport_remove_pending_events(drv);
725 
726 start_over:
727  list_for_each_entry(gameport, &gameport_list, node) {
728  if (gameport->drv == drv) {
729  gameport_disconnect_port(gameport);
730  gameport_find_driver(gameport);
731  /* we could've deleted some ports, restart */
732  goto start_over;
733  }
734  }
735 
736  driver_unregister(&drv->driver);
737 
738  mutex_unlock(&gameport_mutex);
739 }
741 
742 static int gameport_bus_match(struct device *dev, struct device_driver *drv)
743 {
744  struct gameport_driver *gameport_drv = to_gameport_driver(drv);
745 
746  return !gameport_drv->ignore;
747 }
748 
749 static struct bus_type gameport_bus = {
750  .name = "gameport",
751  .dev_attrs = gameport_device_attrs,
752  .drv_attrs = gameport_driver_attrs,
753  .match = gameport_bus_match,
754  .probe = gameport_driver_probe,
755  .remove = gameport_driver_remove,
756 };
757 
758 static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
759 {
760  mutex_lock(&gameport->drv_mutex);
761  gameport->drv = drv;
762  mutex_unlock(&gameport->drv_mutex);
763 }
764 
765 int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
766 {
767  if (gameport->open) {
768  if (gameport->open(gameport, mode)) {
769  return -1;
770  }
771  } else {
772  if (mode != GAMEPORT_MODE_RAW)
773  return -1;
774  }
775 
776  gameport_set_drv(gameport, drv);
777  return 0;
778 }
780 
781 void gameport_close(struct gameport *gameport)
782 {
783  del_timer_sync(&gameport->poll_timer);
784  gameport->poll_handler = NULL;
785  gameport->poll_interval = 0;
786  gameport_set_drv(gameport, NULL);
787  if (gameport->close)
788  gameport->close(gameport);
789 }
791 
792 static int __init gameport_init(void)
793 {
794  int error;
795 
796  error = bus_register(&gameport_bus);
797  if (error) {
798  pr_err("failed to register gameport bus, error: %d\n", error);
799  return error;
800  }
801 
802 
803  return 0;
804 }
805 
806 static void __exit gameport_exit(void)
807 {
808  bus_unregister(&gameport_bus);
809 
810  /*
811  * There should not be any outstanding events but work may
812  * still be scheduled so simply cancel it.
813  */
814  cancel_work_sync(&gameport_event_work);
815 }
816 
817 subsys_initcall(gameport_init);
818 module_exit(gameport_exit);