gameport.c 19 KB

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