gameport.c 20 KB

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