gameport.c 20 KB

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