gameport.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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. do {
  355. gameport_handle_event();
  356. wait_event_interruptible(gameport_wait,
  357. kthread_should_stop() || !list_empty(&gameport_event_list));
  358. try_to_freeze();
  359. } while (!kthread_should_stop());
  360. printk(KERN_DEBUG "gameport: kgameportd exiting\n");
  361. return 0;
  362. }
  363. /*
  364. * Gameport port operations
  365. */
  366. static ssize_t gameport_show_description(struct device *dev, struct device_attribute *attr, char *buf)
  367. {
  368. struct gameport *gameport = to_gameport_port(dev);
  369. return sprintf(buf, "%s\n", gameport->name);
  370. }
  371. static ssize_t gameport_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  372. {
  373. struct gameport *gameport = to_gameport_port(dev);
  374. struct device_driver *drv;
  375. int error;
  376. error = mutex_lock_interruptible(&gameport_mutex);
  377. if (error)
  378. return error;
  379. if (!strncmp(buf, "none", count)) {
  380. gameport_disconnect_port(gameport);
  381. } else if (!strncmp(buf, "reconnect", count)) {
  382. gameport_reconnect_port(gameport);
  383. } else if (!strncmp(buf, "rescan", count)) {
  384. gameport_disconnect_port(gameport);
  385. gameport_find_driver(gameport);
  386. } else if ((drv = driver_find(buf, &gameport_bus)) != NULL) {
  387. gameport_disconnect_port(gameport);
  388. error = gameport_bind_driver(gameport, to_gameport_driver(drv));
  389. put_driver(drv);
  390. } else {
  391. error = -EINVAL;
  392. }
  393. mutex_unlock(&gameport_mutex);
  394. return error ? error : count;
  395. }
  396. static struct device_attribute gameport_device_attrs[] = {
  397. __ATTR(description, S_IRUGO, gameport_show_description, NULL),
  398. __ATTR(drvctl, S_IWUSR, NULL, gameport_rebind_driver),
  399. __ATTR_NULL
  400. };
  401. static void gameport_release_port(struct device *dev)
  402. {
  403. struct gameport *gameport = to_gameport_port(dev);
  404. kfree(gameport);
  405. module_put(THIS_MODULE);
  406. }
  407. void gameport_set_phys(struct gameport *gameport, const char *fmt, ...)
  408. {
  409. va_list args;
  410. va_start(args, fmt);
  411. vsnprintf(gameport->phys, sizeof(gameport->phys), fmt, args);
  412. va_end(args);
  413. }
  414. /*
  415. * Prepare gameport port for registration.
  416. */
  417. static void gameport_init_port(struct gameport *gameport)
  418. {
  419. static atomic_t gameport_no = ATOMIC_INIT(0);
  420. __module_get(THIS_MODULE);
  421. mutex_init(&gameport->drv_mutex);
  422. device_initialize(&gameport->dev);
  423. snprintf(gameport->dev.bus_id, sizeof(gameport->dev.bus_id),
  424. "gameport%lu", (unsigned long)atomic_inc_return(&gameport_no) - 1);
  425. gameport->dev.bus = &gameport_bus;
  426. gameport->dev.release = gameport_release_port;
  427. if (gameport->parent)
  428. gameport->dev.parent = &gameport->parent->dev;
  429. INIT_LIST_HEAD(&gameport->node);
  430. spin_lock_init(&gameport->timer_lock);
  431. init_timer(&gameport->poll_timer);
  432. gameport->poll_timer.function = gameport_run_poll_handler;
  433. gameport->poll_timer.data = (unsigned long)gameport;
  434. }
  435. /*
  436. * Complete gameport port registration.
  437. * Driver core will attempt to find appropriate driver for the port.
  438. */
  439. static void gameport_add_port(struct gameport *gameport)
  440. {
  441. int error;
  442. if (gameport->parent)
  443. gameport->parent->child = gameport;
  444. gameport->speed = gameport_measure_speed(gameport);
  445. list_add_tail(&gameport->node, &gameport_list);
  446. if (gameport->io)
  447. printk(KERN_INFO "gameport: %s is %s, io %#x, speed %dkHz\n",
  448. gameport->name, gameport->phys, gameport->io, gameport->speed);
  449. else
  450. printk(KERN_INFO "gameport: %s is %s, speed %dkHz\n",
  451. gameport->name, gameport->phys, gameport->speed);
  452. error = device_add(&gameport->dev);
  453. if (error)
  454. printk(KERN_ERR
  455. "gameport: device_add() failed for %s (%s), error: %d\n",
  456. gameport->phys, gameport->name, error);
  457. else
  458. gameport->registered = 1;
  459. }
  460. /*
  461. * gameport_destroy_port() completes deregistration process and removes
  462. * port from the system
  463. */
  464. static void gameport_destroy_port(struct gameport *gameport)
  465. {
  466. struct gameport *child;
  467. child = gameport_get_pending_child(gameport);
  468. if (child) {
  469. gameport_remove_pending_events(child);
  470. put_device(&child->dev);
  471. }
  472. if (gameport->parent) {
  473. gameport->parent->child = NULL;
  474. gameport->parent = NULL;
  475. }
  476. if (gameport->registered) {
  477. device_del(&gameport->dev);
  478. gameport->registered = 0;
  479. }
  480. list_del_init(&gameport->node);
  481. gameport_remove_pending_events(gameport);
  482. put_device(&gameport->dev);
  483. }
  484. /*
  485. * Reconnect gameport port and all its children (re-initialize attached devices)
  486. */
  487. static void gameport_reconnect_port(struct gameport *gameport)
  488. {
  489. do {
  490. if (!gameport->drv || !gameport->drv->reconnect || gameport->drv->reconnect(gameport)) {
  491. gameport_disconnect_port(gameport);
  492. gameport_find_driver(gameport);
  493. /* Ok, old children are now gone, we are done */
  494. break;
  495. }
  496. gameport = gameport->child;
  497. } while (gameport);
  498. }
  499. /*
  500. * gameport_disconnect_port() unbinds a port from its driver. As a side effect
  501. * all child ports are unbound and destroyed.
  502. */
  503. static void gameport_disconnect_port(struct gameport *gameport)
  504. {
  505. struct gameport *s, *parent;
  506. if (gameport->child) {
  507. /*
  508. * Children ports should be disconnected and destroyed
  509. * first, staring with the leaf one, since we don't want
  510. * to do recursion
  511. */
  512. for (s = gameport; s->child; s = s->child)
  513. /* empty */;
  514. do {
  515. parent = s->parent;
  516. device_release_driver(&s->dev);
  517. gameport_destroy_port(s);
  518. } while ((s = parent) != gameport);
  519. }
  520. /*
  521. * Ok, no children left, now disconnect this port
  522. */
  523. device_release_driver(&gameport->dev);
  524. }
  525. void gameport_rescan(struct gameport *gameport)
  526. {
  527. gameport_queue_event(gameport, NULL, GAMEPORT_RESCAN);
  528. }
  529. void gameport_reconnect(struct gameport *gameport)
  530. {
  531. gameport_queue_event(gameport, NULL, GAMEPORT_RECONNECT);
  532. }
  533. /*
  534. * Submits register request to kgameportd for subsequent execution.
  535. * Note that port registration is always asynchronous.
  536. */
  537. void __gameport_register_port(struct gameport *gameport, struct module *owner)
  538. {
  539. gameport_init_port(gameport);
  540. gameport_queue_event(gameport, owner, GAMEPORT_REGISTER_PORT);
  541. }
  542. /*
  543. * Synchronously unregisters gameport port.
  544. */
  545. void gameport_unregister_port(struct gameport *gameport)
  546. {
  547. mutex_lock(&gameport_mutex);
  548. gameport_disconnect_port(gameport);
  549. gameport_destroy_port(gameport);
  550. mutex_unlock(&gameport_mutex);
  551. }
  552. /*
  553. * Gameport driver operations
  554. */
  555. static ssize_t gameport_driver_show_description(struct device_driver *drv, char *buf)
  556. {
  557. struct gameport_driver *driver = to_gameport_driver(drv);
  558. return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
  559. }
  560. static struct driver_attribute gameport_driver_attrs[] = {
  561. __ATTR(description, S_IRUGO, gameport_driver_show_description, NULL),
  562. __ATTR_NULL
  563. };
  564. static int gameport_driver_probe(struct device *dev)
  565. {
  566. struct gameport *gameport = to_gameport_port(dev);
  567. struct gameport_driver *drv = to_gameport_driver(dev->driver);
  568. drv->connect(gameport, drv);
  569. return gameport->drv ? 0 : -ENODEV;
  570. }
  571. static int gameport_driver_remove(struct device *dev)
  572. {
  573. struct gameport *gameport = to_gameport_port(dev);
  574. struct gameport_driver *drv = to_gameport_driver(dev->driver);
  575. drv->disconnect(gameport);
  576. return 0;
  577. }
  578. static void gameport_add_driver(struct gameport_driver *drv)
  579. {
  580. int error;
  581. error = driver_register(&drv->driver);
  582. if (error)
  583. printk(KERN_ERR
  584. "gameport: driver_register() failed for %s, error: %d\n",
  585. drv->driver.name, error);
  586. }
  587. void __gameport_register_driver(struct gameport_driver *drv, struct module *owner)
  588. {
  589. drv->driver.bus = &gameport_bus;
  590. gameport_queue_event(drv, owner, GAMEPORT_REGISTER_DRIVER);
  591. }
  592. void gameport_unregister_driver(struct gameport_driver *drv)
  593. {
  594. struct gameport *gameport;
  595. mutex_lock(&gameport_mutex);
  596. drv->ignore = 1; /* so gameport_find_driver ignores it */
  597. start_over:
  598. list_for_each_entry(gameport, &gameport_list, node) {
  599. if (gameport->drv == drv) {
  600. gameport_disconnect_port(gameport);
  601. gameport_find_driver(gameport);
  602. /* we could've deleted some ports, restart */
  603. goto start_over;
  604. }
  605. }
  606. driver_unregister(&drv->driver);
  607. mutex_unlock(&gameport_mutex);
  608. }
  609. static int gameport_bus_match(struct device *dev, struct device_driver *drv)
  610. {
  611. struct gameport_driver *gameport_drv = to_gameport_driver(drv);
  612. return !gameport_drv->ignore;
  613. }
  614. static struct bus_type gameport_bus = {
  615. .name = "gameport",
  616. .dev_attrs = gameport_device_attrs,
  617. .drv_attrs = gameport_driver_attrs,
  618. .match = gameport_bus_match,
  619. .probe = gameport_driver_probe,
  620. .remove = gameport_driver_remove,
  621. };
  622. static void gameport_set_drv(struct gameport *gameport, struct gameport_driver *drv)
  623. {
  624. mutex_lock(&gameport->drv_mutex);
  625. gameport->drv = drv;
  626. mutex_unlock(&gameport->drv_mutex);
  627. }
  628. int gameport_open(struct gameport *gameport, struct gameport_driver *drv, int mode)
  629. {
  630. if (gameport->open) {
  631. if (gameport->open(gameport, mode)) {
  632. return -1;
  633. }
  634. } else {
  635. if (mode != GAMEPORT_MODE_RAW)
  636. return -1;
  637. }
  638. gameport_set_drv(gameport, drv);
  639. return 0;
  640. }
  641. void gameport_close(struct gameport *gameport)
  642. {
  643. del_timer_sync(&gameport->poll_timer);
  644. gameport->poll_handler = NULL;
  645. gameport->poll_interval = 0;
  646. gameport_set_drv(gameport, NULL);
  647. if (gameport->close)
  648. gameport->close(gameport);
  649. }
  650. static int __init gameport_init(void)
  651. {
  652. int error;
  653. error = bus_register(&gameport_bus);
  654. if (error) {
  655. printk(KERN_ERR "gameport: failed to register gameport bus, error: %d\n", error);
  656. return error;
  657. }
  658. gameport_task = kthread_run(gameport_thread, NULL, "kgameportd");
  659. if (IS_ERR(gameport_task)) {
  660. bus_unregister(&gameport_bus);
  661. error = PTR_ERR(gameport_task);
  662. printk(KERN_ERR "gameport: Failed to start kgameportd, error: %d\n", error);
  663. return error;
  664. }
  665. return 0;
  666. }
  667. static void __exit gameport_exit(void)
  668. {
  669. bus_unregister(&gameport_bus);
  670. kthread_stop(gameport_task);
  671. }
  672. subsys_initcall(gameport_init);
  673. module_exit(gameport_exit);