gameport.c 20 KB

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