gameport.c 19 KB

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