gameport.c 19 KB

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