gameport.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 list_head *node, *next;
  236. struct gameport_event *e;
  237. unsigned long flags;
  238. spin_lock_irqsave(&gameport_event_lock, flags);
  239. list_for_each_safe(node, next, &gameport_event_list) {
  240. e = list_entry(node, struct gameport_event, node);
  241. if (event->object == e->object) {
  242. /*
  243. * If this event is of different type we should not
  244. * look further - we only suppress duplicate events
  245. * that were sent back-to-back.
  246. */
  247. if (event->type != e->type)
  248. break;
  249. list_del_init(node);
  250. gameport_free_event(e);
  251. }
  252. }
  253. spin_unlock_irqrestore(&gameport_event_lock, flags);
  254. }
  255. static struct gameport_event *gameport_get_event(void)
  256. {
  257. struct gameport_event *event;
  258. struct list_head *node;
  259. unsigned long flags;
  260. spin_lock_irqsave(&gameport_event_lock, flags);
  261. if (list_empty(&gameport_event_list)) {
  262. spin_unlock_irqrestore(&gameport_event_lock, flags);
  263. return NULL;
  264. }
  265. node = gameport_event_list.next;
  266. event = list_entry(node, struct gameport_event, node);
  267. list_del_init(node);
  268. spin_unlock_irqrestore(&gameport_event_lock, flags);
  269. return event;
  270. }
  271. static void gameport_handle_event(void)
  272. {
  273. struct gameport_event *event;
  274. mutex_lock(&gameport_mutex);
  275. /*
  276. * Note that we handle only one event here to give swsusp
  277. * a chance to freeze kgameportd thread. Gameport events
  278. * should be pretty rare so we are not concerned about
  279. * taking performance hit.
  280. */
  281. if ((event = gameport_get_event())) {
  282. switch (event->type) {
  283. case GAMEPORT_REGISTER_PORT:
  284. gameport_add_port(event->object);
  285. break;
  286. case GAMEPORT_ATTACH_DRIVER:
  287. gameport_attach_driver(event->object);
  288. break;
  289. default:
  290. break;
  291. }
  292. gameport_remove_duplicate_events(event);
  293. gameport_free_event(event);
  294. }
  295. mutex_unlock(&gameport_mutex);
  296. }
  297. /*
  298. * Remove all events that have been submitted for a given object,
  299. * be it a gameport port or a driver.
  300. */
  301. static void gameport_remove_pending_events(void *object)
  302. {
  303. struct list_head *node, *next;
  304. struct gameport_event *event;
  305. unsigned long flags;
  306. spin_lock_irqsave(&gameport_event_lock, flags);
  307. list_for_each_safe(node, next, &gameport_event_list) {
  308. event = list_entry(node, struct gameport_event, node);
  309. if (event->object == object) {
  310. list_del_init(node);
  311. gameport_free_event(event);
  312. }
  313. }
  314. spin_unlock_irqrestore(&gameport_event_lock, flags);
  315. }
  316. /*
  317. * Destroy child gameport port (if any) that has not been fully registered yet.
  318. *
  319. * Note that we rely on the fact that port can have only one child and therefore
  320. * only one child registration request can be pending. Additionally, children
  321. * are registered by driver's connect() handler so there can't be a grandchild
  322. * pending registration together with a child.
  323. */
  324. static struct gameport *gameport_get_pending_child(struct gameport *parent)
  325. {
  326. struct gameport_event *event;
  327. struct gameport *gameport, *child = NULL;
  328. unsigned long flags;
  329. spin_lock_irqsave(&gameport_event_lock, flags);
  330. list_for_each_entry(event, &gameport_event_list, node) {
  331. if (event->type == GAMEPORT_REGISTER_PORT) {
  332. gameport = event->object;
  333. if (gameport->parent == parent) {
  334. child = gameport;
  335. break;
  336. }
  337. }
  338. }
  339. spin_unlock_irqrestore(&gameport_event_lock, flags);
  340. return child;
  341. }
  342. static int gameport_thread(void *nothing)
  343. {
  344. set_freezable();
  345. do {
  346. gameport_handle_event();
  347. wait_event_freezable(gameport_wait,
  348. kthread_should_stop() || !list_empty(&gameport_event_list));
  349. } while (!kthread_should_stop());
  350. printk(KERN_DEBUG "gameport: kgameportd exiting\n");
  351. return 0;
  352. }
  353. /*
  354. * Gameport port operations
  355. */
  356. static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
  357. {
  358. struct gameport *gameport = to_gameport_port(dev);
  359. return sprintf(buf, "%s\n", gameport->name);
  360. }
  361. static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  362. {
  363. struct gameport *gameport = to_gameport_port(dev);
  364. struct device_driver *drv;
  365. int error;
  366. error = mutex_lock_interruptible(&gameport_mutex);
  367. if (error)
  368. return error;
  369. if (!strncmp(buf, "none", count)) {
  370. gameport_disconnect_port(gameport);
  371. } else if (!strncmp(buf, "reconnect", count)) {
  372. gameport_reconnect_port(gameport);
  373. } else if (!strncmp(buf, "rescan", count)) {
  374. gameport_disconnect_port(gameport);
  375. gameport_find_driver(gameport);
  376. } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
  377. gameport_disconnect_port(gameport);
  378. error = gameport_bind_driver(gameport, to_gameport_driver(drv));
  379. put_driver(drv);
  380. } else {
  381. error = -EINVAL;
  382. }
  383. mutex_unlock(&gameport_mutex);
  384. return error ? error : count;
  385. }
  386. static struct device_attribute gameport_device_attrs[] = {
  387. __ATTR(description, S_IRUGO, gameport_show_description, NULL),
  388. __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
  389. __ATTR_NULL
  390. };
  391. static void gameport_release_port(struct device *dev)
  392. {
  393. struct gameport *gameport = to_gameport_port(dev);
  394. kfree(gameport);
  395. module_put(THIS_MODULE);
  396. }
  397. void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
  398. {
  399. va_list args;
  400. va_start(args, fmt);
  401. vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
  402. va_end(args);
  403. }
  404. EXPORT_SYMBOL(gameport_set_phys);
  405. /*
  406. * Prepare gameport port for registration.
  407. */
  408. static void gameport_init_port(struct gameport *gameport)
  409. {
  410. static atomic_t gameport_no = ATOMIC_INIT(0);
  411. __module_get(THIS_MODULE);
  412. mutex_init(&gameport->drv_mutex);
  413. device_initialize(&gameport->dev);
  414. dev_set_name(&gameport->dev, "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
  415. gameport->dev.bus = &gameport_bus;
  416. gameport->dev.release = gameport_release_port;
  417. if (gameport->parent)
  418. gameport->dev.parent = &gameport->parent->dev;
  419. INIT_LIST_HEAD(&gameport->node);
  420. spin_lock_init(&gameport->timer_lock);
  421. init_timer(&gameport->poll_timer);
  422. gameport->poll_timer.function = gameport_run_poll_handler;
  423. gameport->poll_timer.data = (unsigned long)gameport;
  424. }
  425. /*
  426. * Complete gameport port registration.
  427. * Driver core will attempt to find appropriate driver for the port.
  428. */
  429. static void gameport_add_port(struct gameport *gameport)
  430. {
  431. int error;
  432. if (gameport->parent)
  433. gameport->parent->child = gameport;
  434. gameport->speed = gameport_measure_speed(gameport);
  435. list_add_tail(&gameport->node, &gameport_list);
  436. if (gameport->io)
  437. printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
  438. gameport->name, gameport->phys, gameport->io, gameport->speed);
  439. else
  440. printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
  441. gameport->name, gameport->phys, gameport->speed);
  442. error = device_add(&gameport->dev);
  443. if (error)
  444. printk(KERN_ERR
  445. "gameport: device_add() failed for %s (%s), error: %d\n",
  446. gameport->phys, gameport->name, error);
  447. else
  448. gameport->registered = 1;
  449. }
  450. /*
  451. * gameport_destroy_port() completes deregistration process and removes
  452. * port from the system
  453. */
  454. static void gameport_destroy_port(struct gameport *gameport)
  455. {
  456. struct gameport *child;
  457. child = gameport_get_pending_child(gameport);
  458. if (child) {
  459. gameport_remove_pending_events(child);
  460. put_device(&child->dev);
  461. }
  462. if (gameport->parent) {
  463. gameport->parent->child = NULL;
  464. gameport->parent = NULL;
  465. }
  466. if (gameport->registered) {
  467. device_del(&gameport->dev);
  468. gameport->registered = 0;
  469. }
  470. list_del_init(&gameport->node);
  471. gameport_remove_pending_events(gameport);
  472. put_device(&gameport->dev);
  473. }
  474. /*
  475. * Reconnect gameport port and all its children (re-initialize attached devices)
  476. */
  477. static void gameport_reconnect_port(struct gameport *gameport)
  478. {
  479. do {
  480. if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
  481. gameport_disconnect_port(gameport);
  482. gameport_find_driver(gameport);
  483. /* Ok, old children are now gone, we are done */
  484. break;
  485. }
  486. gameport = gameport->child;
  487. } while (gameport);
  488. }
  489. /*
  490. * gameport_disconnect_port() unbinds a port from its driver. As a side effect
  491. * all child ports are unbound and destroyed.
  492. */
  493. static void gameport_disconnect_port(struct gameport *gameport)
  494. {
  495. struct gameport *s, *parent;
  496. if (gameport->child) {
  497. /*
  498. * Children ports should be disconnected and destroyed
  499. * first, staring with the leaf one, since we don't want
  500. * to do recursion
  501. */
  502. for (s = gameport; s->child; s = s->child)
  503. /* empty */;
  504. do {
  505. parent = s->parent;
  506. device_release_driver(&s->dev);
  507. gameport_destroy_port(s);
  508. } while ((s = parent) != gameport);
  509. }
  510. /*
  511. * Ok, no children left, now disconnect this port
  512. */
  513. device_release_driver(&gameport->dev);
  514. }
  515. /*
  516. * Submits register request to kgameportd for subsequent execution.
  517. * Note that port registration is always asynchronous.
  518. */
  519. void __gameport_register_port(struct gameport *gameport, struct module *owner)
  520. {
  521. gameport_init_port(gameport);
  522. gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
  523. }
  524. EXPORT_SYMBOL(__gameport_register_port);
  525. /*
  526. * Synchronously unregisters gameport port.
  527. */
  528. void gameport_unregister_port(struct gameport *gameport)
  529. {
  530. mutex_lock(&gameport_mutex);
  531. gameport_disconnect_port(gameport);
  532. gameport_destroy_port(gameport);
  533. mutex_unlock(&gameport_mutex);
  534. }
  535. EXPORT_SYMBOL(gameport_unregister_port);
  536. /*
  537. * Gameport driver operations
  538. */
  539. static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
  540. {
  541. struct gameport_driver *driver = to_gameport_driver(drv);
  542. return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
  543. }
  544. static struct driver_attribute gameport_driver_attrs[] = {
  545. __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
  546. __ATTR_NULL
  547. };
  548. static int gameport_driver_probe(struct device *dev)
  549. {
  550. struct gameport *gameport = to_gameport_port(dev);
  551. struct gameport_driver *drv = to_gameport_driver(dev->driver);
  552. drv->connect(gameport, drv);
  553. return gameport->drv ? 0 : -ENODEV;
  554. }
  555. static int gameport_driver_remove(struct device *dev)
  556. {
  557. struct gameport *gameport = to_gameport_port(dev);
  558. struct gameport_driver *drv = to_gameport_driver(dev->driver);
  559. drv->disconnect(gameport);
  560. return 0;
  561. }
  562. static void gameport_attach_driver(struct gameport_driver *drv)
  563. {
  564. int error;
  565. error = driver_attach(&drv->driver);
  566. if (error)
  567. printk(KERN_ERR
  568. "gameport: driver_attach() failed for %s, error: %d\n",
  569. drv->driver.name, error);
  570. }
  571. int __gameport_register_driver(struct gameport_driver *drv, struct module *owner,
  572. const char *mod_name)
  573. {
  574. int error;
  575. drv->driver.bus = &gameport_bus;
  576. drv->driver.owner = owner;
  577. drv->driver.mod_name = mod_name;
  578. /*
  579. * Temporarily disable automatic binding because probing
  580. * takes long time and we are better off doing it in kgameportd
  581. */
  582. drv->ignore = true;
  583. error = driver_register(&drv->driver);
  584. if (error) {
  585. printk(KERN_ERR
  586. "gameport: driver_register() failed for %s, error: %d\n",
  587. drv->driver.name, error);
  588. return error;
  589. }
  590. /*
  591. * Reset ignore flag and let kgameportd bind the driver to free ports
  592. */
  593. drv->ignore = false;
  594. error = gameport_queue_event(drv, NULL, GAMEPORT_ATTACH_DRIVER);
  595. if (error) {
  596. driver_unregister(&drv->driver);
  597. return error;
  598. }
  599. return 0;
  600. }
  601. EXPORT_SYMBOL(__gameport_register_driver);
  602. void gameport_unregister_driver(struct gameport_driver *drv)
  603. {
  604. struct gameport *gameport;
  605. mutex_lock(&gameport_mutex);
  606. drv->ignore = true; /* so gameport_find_driver ignores it */
  607. gameport_remove_pending_events(drv);
  608. start_over:
  609. list_for_each_entry(gameport, &gameport_list, node) {
  610. if (gameport->drv == drv) {
  611. gameport_disconnect_port(gameport);
  612. gameport_find_driver(gameport);
  613. /* we could've deleted some ports, restart */
  614. goto start_over;
  615. }
  616. }
  617. driver_unregister(&drv->driver);
  618. mutex_unlock(&gameport_mutex);
  619. }
  620. EXPORT_SYMBOL(gameport_unregister_driver);
  621. static int gameport_bus_match(struct device *dev, struct device_driver *drv)
  622. {
  623. struct gameport_driver *gameport_drv = to_gameport_driver(drv);
  624. return !gameport_drv->ignore;
  625. }
  626. static struct bus_type gameport_bus = {
  627. .name = "gameport",
  628. .dev_attrs = gameport_device_attrs,
  629. .drv_attrs = gameport_driver_attrs,
  630. .match = gameport_bus_match,
  631. .probe = gameport_driver_probe,
  632. .remove = gameport_driver_remove,
  633. };
  634. static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
  635. {
  636. mutex_lock(&gameport->drv_mutex);
  637. gameport->drv = drv;
  638. mutex_unlock(&gameport->drv_mutex);
  639. }
  640. int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
  641. {
  642. if (gameport->open) {
  643. if (gameport->open(gameport, mode)) {
  644. return -1;
  645. }
  646. } else {
  647. if (mode != GAMEPORT_MODE_RAW)
  648. return -1;
  649. }
  650. gameport_set_drv(gameport, drv);
  651. return 0;
  652. }
  653. EXPORT_SYMBOL(gameport_open);
  654. void gameport_close(struct gameport *gameport)
  655. {
  656. del_timer_sync(&gameport->poll_timer);
  657. gameport->poll_handler = NULL;
  658. gameport->poll_interval = 0;
  659. gameport_set_drv(gameport, NULL);
  660. if (gameport->close)
  661. gameport->close(gameport);
  662. }
  663. EXPORT_SYMBOL(gameport_close);
  664. static int __init gameport_init(void)
  665. {
  666. int error;
  667. error = bus_register(&gameport_bus);
  668. if (error) {
  669. printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
  670. return error;
  671. }
  672. gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
  673. if (IS_ERR(gameport_task)) {
  674. bus_unregister(&gameport_bus);
  675. error = PTR_ERR(gameport_task);
  676. printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
  677. return error;
  678. }
  679. return 0;
  680. }
  681. static void __exit gameport_exit(void)
  682. {
  683. bus_unregister(&gameport_bus);
  684. kthread_stop(gameport_task);
  685. }
  686. subsys_initcall(gameport_init);
  687. module_exit(gameport_exit);