gameport.c 20 KB

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