gameport.c 19 KB

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