gameport.c 20 KB

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