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 * (unsigned long)HZ / (1000 / 50)) / (tx < 1 ? 1 : tx);
  109. #else
  110. unsigned int j, t = 0;
  111. if (gameport_open(gameport, NULL, GAMEPORT_MODE_RAW))
  112. return 0;
  113. j = jiffies; while (j == jiffies);
  114. j = jiffies; while (j == jiffies) { t++; gameport_read(gameport); }
  115. gameport_close(gameport);
  116. return t * HZ / 1000;
  117. #endif
  118. }
  119. void gameport_start_polling(struct gameport *gameport)
  120. {
  121. spin_lock(&gameport->timer_lock);
  122. if (!gameport->poll_cnt++) {
  123. BUG_ON(!gameport->poll_handler);
  124. BUG_ON(!gameport->poll_interval);
  125. mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
  126. }
  127. spin_unlock(&gameport->timer_lock);
  128. }
  129. void gameport_stop_polling(struct gameport *gameport)
  130. {
  131. spin_lock(&gameport->timer_lock);
  132. if (!--gameport->poll_cnt)
  133. del_timer(&gameport->poll_timer);
  134. spin_unlock(&gameport->timer_lock);
  135. }
  136. static void gameport_run_poll_handler(unsigned long d)
  137. {
  138. struct gameport *gameport = (struct gameport *)d;
  139. gameport->poll_handler(gameport);
  140. if (gameport->poll_cnt)
  141. mod_timer(&gameport->poll_timer, jiffies + msecs_to_jiffies(gameport->poll_interval));
  142. }
  143. /*
  144. * Basic gameport -> driver core mappings
  145. */
  146. static int gameport_bind_driver(struct gameport *gameport, struct gameport_driver *drv)
  147. {
  148. int error;
  149. gameport->dev.driver = &drv->driver;
  150. if (drv->connect(gameport, drv)) {
  151. gameport->dev.driver = NULL;
  152. return -ENODEV;
  153. }
  154. error = device_bind_driver(&gameport->dev);
  155. if (error) {
  156. printk(KERN_WARNING
  157. "gameport: device_bind_driver() failed "
  158. "for %s (%s) and %s, error: %d\n",
  159. gameport->phys, gameport->name,
  160. drv->description, error);
  161. drv->disconnect(gameport);
  162. gameport->dev.driver = NULL;
  163. return error;
  164. }
  165. return 0;
  166. }
  167. static void gameport_find_driver(struct gameport *gameport)
  168. {
  169. int error;
  170. error = device_attach(&gameport->dev);
  171. if (error < 0)
  172. printk(KERN_WARNING
  173. "gameport: device_attach() failed for %s (%s), error: %d\n",
  174. gameport->phys, gameport->name, error);
  175. }
  176. /*
  177. * Gameport event processing.
  178. */
  179. enum gameport_event_type {
  180. GAMEPORT_RESCAN,
  181. GAMEPORT_RECONNECT,
  182. GAMEPORT_REGISTER_PORT,
  183. GAMEPORT_REGISTER_DRIVER,
  184. };
  185. struct gameport_event {
  186. enum gameport_event_type type;
  187. void *object;
  188. struct module *owner;
  189. struct list_head node;
  190. };
  191. static DEFINE_SPINLOCK(gameport_event_lock); /* protects gameport_event_list */
  192. static LIST_HEAD(gameport_event_list);
  193. static DECLARE_WAIT_QUEUE_HEAD(gameport_wait);
  194. static struct task_struct *gameport_task;
  195. static void gameport_queue_event(void *object, struct module *owner,
  196. enum gameport_event_type event_type)
  197. {
  198. unsigned long flags;
  199. struct gameport_event *event;
  200. spin_lock_irqsave(&gameport_event_lock, flags);
  201. /*
  202. * Scan event list for the other events for the same gameport port,
  203. * starting with the most recent one. If event is the same we
  204. * do not need add new one. If event is of different type we
  205. * need to add this event and should not look further because
  206. * we need to preseve sequence of distinct events.
  207. */
  208. list_for_each_entry_reverse(event, &gameport_event_list, node) {
  209. if (event->object == object) {
  210. if (event->type == event_type)
  211. goto out;
  212. break;
  213. }
  214. }
  215. if ((event = kmalloc(sizeof(struct gameport_event), GFP_ATOMIC))) {
  216. if (!try_module_get(owner)) {
  217. printk(KERN_WARNING "gameport: Can't get module reference, dropping event %d\n", event_type);
  218. kfree(event);
  219. goto out;
  220. }
  221. event->type = event_type;
  222. event->object = object;
  223. event->owner = owner;
  224. list_add_tail(&event->node, &gameport_event_list);
  225. wake_up(&gameport_wait);
  226. } else {
  227. printk(KERN_ERR "gameport: Not enough memory to queue event %d\n", event_type);
  228. }
  229. out:
  230. spin_unlock_irqrestore(&gameport_event_lock, flags);
  231. }
  232. static void gameport_free_event(struct gameport_event *event)
  233. {
  234. module_put(event->owner);
  235. kfree(event);
  236. }
  237. static void gameport_remove_duplicate_events(struct gameport_event *event)
  238. {
  239. struct list_head *node, *next;
  240. struct gameport_event *e;
  241. unsigned long flags;
  242. spin_lock_irqsave(&gameport_event_lock, flags);
  243. list_for_each_safe(node, next, &gameport_event_list) {
  244. e = list_entry(node, struct gameport_event, node);
  245. if (event->object == e->object) {
  246. /*
  247. * If this event is of different type we should not
  248. * look further - we only suppress duplicate events
  249. * that were sent back-to-back.
  250. */
  251. if (event->type != e->type)
  252. break;
  253. list_del_init(node);
  254. gameport_free_event(e);
  255. }
  256. }
  257. spin_unlock_irqrestore(&gameport_event_lock, flags);
  258. }
  259. static struct gameport_event *gameport_get_event(void)
  260. {
  261. struct gameport_event *event;
  262. struct list_head *node;
  263. unsigned long flags;
  264. spin_lock_irqsave(&gameport_event_lock, flags);
  265. if (list_empty(&gameport_event_list)) {
  266. spin_unlock_irqrestore(&gameport_event_lock, flags);
  267. return NULL;
  268. }
  269. node = gameport_event_list.next;
  270. event = list_entry(node, struct gameport_event, node);
  271. list_del_init(node);
  272. spin_unlock_irqrestore(&gameport_event_lock, flags);
  273. return event;
  274. }
  275. static void gameport_handle_event(void)
  276. {
  277. struct gameport_event *event;
  278. mutex_lock(&gameport_mutex);
  279. /*
  280. * Note that we handle only one event here to give swsusp
  281. * a chance to freeze kgameportd thread. Gameport events
  282. * should be pretty rare so we are not concerned about
  283. * taking performance hit.
  284. */
  285. if ((event = gameport_get_event())) {
  286. switch (event->type) {
  287. case GAMEPORT_REGISTER_PORT:
  288. gameport_add_port(event->object);
  289. break;
  290. case GAMEPORT_RECONNECT:
  291. gameport_reconnect_port(event->object);
  292. break;
  293. case GAMEPORT_RESCAN:
  294. gameport_disconnect_port(event->object);
  295. gameport_find_driver(event->object);
  296. break;
  297. case GAMEPORT_REGISTER_DRIVER:
  298. gameport_add_driver(event->object);
  299. break;
  300. default:
  301. break;
  302. }
  303. gameport_remove_duplicate_events(event);
  304. gameport_free_event(event);
  305. }
  306. mutex_unlock(&gameport_mutex);
  307. }
  308. /*
  309. * Remove all events that have been submitted for a given gameport port.
  310. */
  311. static void gameport_remove_pending_events(struct gameport *gameport)
  312. {
  313. struct list_head *node, *next;
  314. struct gameport_event *event;
  315. unsigned long flags;
  316. spin_lock_irqsave(&gameport_event_lock, flags);
  317. list_for_each_safe(node, next, &gameport_event_list) {
  318. event = list_entry(node, struct gameport_event, node);
  319. if (event->object == gameport) {
  320. list_del_init(node);
  321. gameport_free_event(event);
  322. }
  323. }
  324. spin_unlock_irqrestore(&gameport_event_lock, flags);
  325. }
  326. /*
  327. * Destroy child gameport port (if any) that has not been fully registered yet.
  328. *
  329. * Note that we rely on the fact that port can have only one child and therefore
  330. * only one child registration request can be pending. Additionally, children
  331. * are registered by driver's connect() handler so there can't be a grandchild
  332. * pending registration together with a child.
  333. */
  334. static struct gameport *gameport_get_pending_child(struct gameport *parent)
  335. {
  336. struct gameport_event *event;
  337. struct gameport *gameport, *child = NULL;
  338. unsigned long flags;
  339. spin_lock_irqsave(&gameport_event_lock, flags);
  340. list_for_each_entry(event, &gameport_event_list, node) {
  341. if (event->type == GAMEPORT_REGISTER_PORT) {
  342. gameport = event->object;
  343. if (gameport->parent == parent) {
  344. child = gameport;
  345. break;
  346. }
  347. }
  348. }
  349. spin_unlock_irqrestore(&gameport_event_lock, flags);
  350. return child;
  351. }
  352. static int gameport_thread(void *nothing)
  353. {
  354. set_freezable();
  355. do {
  356. gameport_handle_event();
  357. wait_event_interruptible(gameport_wait,
  358. kthread_should_stop() || !list_empty(&gameport_event_list));
  359. try_to_freeze();
  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);