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 <linux/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. } else {
  354. error = -EINVAL;
  355. }
  356. mutex_unlock(&gameport_mutex);
  357. return error ? error : count;
  358. }
  359. static struct device_attribute gameport_device_attrs[] = {
  360. __ATTR(description, S_IRUGO, gameport_show_description, NULL),
  361. __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
  362. __ATTR_NULL
  363. };
  364. static void gameport_release_port(struct device *dev)
  365. {
  366. struct gameport *gameport = to_gameport_port(dev);
  367. kfree(gameport);
  368. module_put(THIS_MODULE);
  369. }
  370. void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
  371. {
  372. va_list args;
  373. va_start(args, fmt);
  374. vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
  375. va_end(args);
  376. }
  377. EXPORT_SYMBOL(gameport_set_phys);
  378. /*
  379. * Prepare gameport port for registration.
  380. */
  381. static void gameport_init_port(struct gameport *gameport)
  382. {
  383. static atomic_t gameport_no = ATOMIC_INIT(0);
  384. __module_get(THIS_MODULE);
  385. mutex_init(&gameport->drv_mutex);
  386. device_initialize(&gameport->dev);
  387. dev_set_name(&gameport->dev, "gameport%lu",
  388. (unsigned long)atomic_inc_return(&gameport_no) - 1);
  389. gameport->dev.bus = &gameport_bus;
  390. gameport->dev.release = gameport_release_port;
  391. if (gameport->parent)
  392. gameport->dev.parent = &gameport->parent->dev;
  393. INIT_LIST_HEAD(&gameport->node);
  394. spin_lock_init(&gameport->timer_lock);
  395. init_timer(&gameport->poll_timer);
  396. gameport->poll_timer.function = gameport_run_poll_handler;
  397. gameport->poll_timer.data = (unsigned long)gameport;
  398. }
  399. /*
  400. * Complete gameport port registration.
  401. * Driver core will attempt to find appropriate driver for the port.
  402. */
  403. static void gameport_add_port(struct gameport *gameport)
  404. {
  405. int error;
  406. if (gameport->parent)
  407. gameport->parent->child = gameport;
  408. gameport->speed = gameport_measure_speed(gameport);
  409. list_add_tail(&gameport->node, &gameport_list);
  410. if (gameport->io)
  411. dev_info(&gameport->dev, "%s is %s, io %#x, speed %dkHz\n",
  412. gameport->name, gameport->phys, gameport->io, gameport->speed);
  413. else
  414. dev_info(&gameport->dev, "%s is %s, speed %dkHz\n",
  415. gameport->name, gameport->phys, gameport->speed);
  416. error = device_add(&gameport->dev);
  417. if (error)
  418. dev_err(&gameport->dev,
  419. "device_add() failed for %s (%s), error: %d\n",
  420. gameport->phys, gameport->name, error);
  421. }
  422. /*
  423. * gameport_destroy_port() completes deregistration process and removes
  424. * port from the system
  425. */
  426. static void gameport_destroy_port(struct gameport *gameport)
  427. {
  428. struct gameport *child;
  429. child = gameport_get_pending_child(gameport);
  430. if (child) {
  431. gameport_remove_pending_events(child);
  432. put_device(&child->dev);
  433. }
  434. if (gameport->parent) {
  435. gameport->parent->child = NULL;
  436. gameport->parent = NULL;
  437. }
  438. if (device_is_registered(&gameport->dev))
  439. device_del(&gameport->dev);
  440. list_del_init(&gameport->node);
  441. gameport_remove_pending_events(gameport);
  442. put_device(&gameport->dev);
  443. }
  444. /*
  445. * Reconnect gameport port and all its children (re-initialize attached devices)
  446. */
  447. static void gameport_reconnect_port(struct gameport *gameport)
  448. {
  449. do {
  450. if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
  451. gameport_disconnect_port(gameport);
  452. gameport_find_driver(gameport);
  453. /* Ok, old children are now gone, we are done */
  454. break;
  455. }
  456. gameport = gameport->child;
  457. } while (gameport);
  458. }
  459. /*
  460. * gameport_disconnect_port() unbinds a port from its driver. As a side effect
  461. * all child ports are unbound and destroyed.
  462. */
  463. static void gameport_disconnect_port(struct gameport *gameport)
  464. {
  465. struct gameport *s, *parent;
  466. if (gameport->child) {
  467. /*
  468. * Children ports should be disconnected and destroyed
  469. * first, staring with the leaf one, since we don't want
  470. * to do recursion
  471. */
  472. for (s = gameport; s->child; s = s->child)
  473. /* empty */;
  474. do {
  475. parent = s->parent;
  476. device_release_driver(&s->dev);
  477. gameport_destroy_port(s);
  478. } while ((s = parent) != gameport);
  479. }
  480. /*
  481. * Ok, no children left, now disconnect this port
  482. */
  483. device_release_driver(&gameport->dev);
  484. }
  485. /*
  486. * Submits register request to kgameportd for subsequent execution.
  487. * Note that port registration is always asynchronous.
  488. */
  489. void __gameport_register_port(struct gameport *gameport, struct module *owner)
  490. {
  491. gameport_init_port(gameport);
  492. gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
  493. }
  494. EXPORT_SYMBOL(__gameport_register_port);
  495. /*
  496. * Synchronously unregisters gameport port.
  497. */
  498. void gameport_unregister_port(struct gameport *gameport)
  499. {
  500. mutex_lock(&gameport_mutex);
  501. gameport_disconnect_port(gameport);
  502. gameport_destroy_port(gameport);
  503. mutex_unlock(&gameport_mutex);
  504. }
  505. EXPORT_SYMBOL(gameport_unregister_port);
  506. /*
  507. * Gameport driver operations
  508. */
  509. static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
  510. {
  511. struct gameport_driver *driver = to_gameport_driver(drv);
  512. return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
  513. }
  514. static struct driver_attribute gameport_driver_attrs[] = {
  515. __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
  516. __ATTR_NULL
  517. };
  518. static int gameport_driver_probe(struct device *dev)
  519. {
  520. struct gameport *gameport = to_gameport_port(dev);
  521. struct gameport_driver *drv = to_gameport_driver(dev->driver);
  522. drv->connect(gameport, drv);
  523. return gameport->drv ? 0 : -ENODEV;
  524. }
  525. static int gameport_driver_remove(struct device *dev)
  526. {
  527. struct gameport *gameport = to_gameport_port(dev);
  528. struct gameport_driver *drv = to_gameport_driver(dev->driver);
  529. drv->disconnect(gameport);
  530. return 0;
  531. }
  532. static void gameport_attach_driver(struct gameport_driver *drv)
  533. {
  534. int error;
  535. error = driver_attach(&drv->driver);
  536. if (error)
  537. pr_err("driver_attach() failed for %s, error: %d\n",
  538. drv->driver.name, error);
  539. }
  540. int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
  541. const char *mod_name)
  542. {
  543. int error;
  544. drv->driver.bus = &gameport_bus;
  545. drv->driver.owner = owner;
  546. drv->driver.mod_name = mod_name;
  547. /*
  548. * Temporarily disable automatic binding because probing
  549. * takes long time and we are better off doing it in kgameportd
  550. */
  551. drv->ignore = true;
  552. error = driver_register(&drv->driver);
  553. if (error) {
  554. pr_err("driver_register() failed for %s, error: %d\n",
  555. drv->driver.name, error);
  556. return error;
  557. }
  558. /*
  559. * Reset ignore flag and let kgameportd bind the driver to free ports
  560. */
  561. drv->ignore = false;
  562. error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
  563. if (error) {
  564. driver_unregister(&drv->driver);
  565. return error;
  566. }
  567. return 0;
  568. }
  569. EXPORT_SYMBOL(__gameport_register_driver);
  570. void gameport_unregister_driver(struct gameport_driver *drv)
  571. {
  572. struct gameport *gameport;
  573. mutex_lock(&gameport_mutex);
  574. drv->ignore = true; /* so gameport_find_driver ignores it */
  575. gameport_remove_pending_events(drv);
  576. start_over:
  577. list_for_each_entry(gameport, &gameport_list, node) {
  578. if (gameport->drv == drv) {
  579. gameport_disconnect_port(gameport);
  580. gameport_find_driver(gameport);
  581. /* we could've deleted some ports, restart */
  582. goto start_over;
  583. }
  584. }
  585. driver_unregister(&drv->driver);
  586. mutex_unlock(&gameport_mutex);
  587. }
  588. EXPORT_SYMBOL(gameport_unregister_driver);
  589. static int gameport_bus_match(struct device *dev, struct device_driver *drv)
  590. {
  591. struct gameport_driver *gameport_drv = to_gameport_driver(drv);
  592. return !gameport_drv->ignore;
  593. }
  594. static struct bus_type gameport_bus = {
  595. .name = "gameport",
  596. .dev_attrs = gameport_device_attrs,
  597. .drv_attrs = gameport_driver_attrs,
  598. .match = gameport_bus_match,
  599. .probe = gameport_driver_probe,
  600. .remove = gameport_driver_remove,
  601. };
  602. static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
  603. {
  604. mutex_lock(&gameport->drv_mutex);
  605. gameport->drv = drv;
  606. mutex_unlock(&gameport->drv_mutex);
  607. }
  608. int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
  609. {
  610. if (gameport->open) {
  611. if (gameport->open(gameport, mode)) {
  612. return -1;
  613. }
  614. } else {
  615. if (mode != GAMEPORT_MODE_RAW)
  616. return -1;
  617. }
  618. gameport_set_drv(gameport, drv);
  619. return 0;
  620. }
  621. EXPORT_SYMBOL(gameport_open);
  622. void gameport_close(struct gameport *gameport)
  623. {
  624. del_timer_sync(&gameport->poll_timer);
  625. gameport->poll_handler = NULL;
  626. gameport->poll_interval = 0;
  627. gameport_set_drv(gameport, NULL);
  628. if (gameport->close)
  629. gameport->close(gameport);
  630. }
  631. EXPORT_SYMBOL(gameport_close);
  632. static int __init gameport_init(void)
  633. {
  634. int error;
  635. error = bus_register(&gameport_bus);
  636. if (error) {
  637. pr_err("failed to register gameport bus, error: %d\n", error);
  638. return error;
  639. }
  640. return 0;
  641. }
  642. static void __exit gameport_exit(void)
  643. {
  644. bus_unregister(&gameport_bus);
  645. /*
  646. * There should not be any outstanding events but work may
  647. * still be scheduled so simply cancel it.
  648. */
  649. cancel_work_sync(&gameport_event_work);
  650. }
  651. subsys_initcall(gameport_init);
  652. module_exit(gameport_exit);