gameport.c 20 KB

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