gameport.c 20 KB

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