gameport.c 20 KB

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