gameport.c 19 KB

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