serio.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * The Serio abstraction module
  3. *
  4. * Copyright (c) 1999-2004 Vojtech Pavlik
  5. * Copyright (c) 2004 Dmitry Torokhov
  6. * Copyright (c) 2003 Daniele Bellucci
  7. */
  8. /*
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * Should you need to contact me, the author, you can do so either by
  24. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  25. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  26. */
  27. #include <linux/stddef.h>
  28. #include <linux/module.h>
  29. #include <linux/serio.h>
  30. #include <linux/errno.h>
  31. #include <linux/wait.h>
  32. #include <linux/completion.h>
  33. #include <linux/sched.h>
  34. #include <linux/smp_lock.h>
  35. #include <linux/slab.h>
  36. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  37. MODULE_DESCRIPTION("Serio abstraction core");
  38. MODULE_LICENSE("GPL");
  39. EXPORT_SYMBOL(serio_interrupt);
  40. EXPORT_SYMBOL(__serio_register_port);
  41. EXPORT_SYMBOL(serio_unregister_port);
  42. EXPORT_SYMBOL(__serio_unregister_port_delayed);
  43. EXPORT_SYMBOL(__serio_register_driver);
  44. EXPORT_SYMBOL(serio_unregister_driver);
  45. EXPORT_SYMBOL(serio_open);
  46. EXPORT_SYMBOL(serio_close);
  47. EXPORT_SYMBOL(serio_rescan);
  48. EXPORT_SYMBOL(serio_reconnect);
  49. /*
  50. * serio_sem protects entire serio subsystem and is taken every time
  51. * serio port or driver registrered or unregistered.
  52. */
  53. static DECLARE_MUTEX(serio_sem);
  54. static LIST_HEAD(serio_list);
  55. static struct bus_type serio_bus = {
  56. .name = "serio",
  57. };
  58. static void serio_add_port(struct serio *serio);
  59. static void serio_destroy_port(struct serio *serio);
  60. static void serio_reconnect_port(struct serio *serio);
  61. static void serio_disconnect_port(struct serio *serio);
  62. static int serio_match_port(const struct serio_device_id *ids, struct serio *serio)
  63. {
  64. while (ids->type || ids->proto) {
  65. if ((ids->type == SERIO_ANY || ids->type == serio->id.type) &&
  66. (ids->proto == SERIO_ANY || ids->proto == serio->id.proto) &&
  67. (ids->extra == SERIO_ANY || ids->extra == serio->id.extra) &&
  68. (ids->id == SERIO_ANY || ids->id == serio->id.id))
  69. return 1;
  70. ids++;
  71. }
  72. return 0;
  73. }
  74. /*
  75. * Basic serio -> driver core mappings
  76. */
  77. static void serio_bind_driver(struct serio *serio, struct serio_driver *drv)
  78. {
  79. down_write(&serio_bus.subsys.rwsem);
  80. if (serio_match_port(drv->id_table, serio)) {
  81. serio->dev.driver = &drv->driver;
  82. if (drv->connect(serio, drv)) {
  83. serio->dev.driver = NULL;
  84. goto out;
  85. }
  86. device_bind_driver(&serio->dev);
  87. }
  88. out:
  89. up_write(&serio_bus.subsys.rwsem);
  90. }
  91. static void serio_release_driver(struct serio *serio)
  92. {
  93. down_write(&serio_bus.subsys.rwsem);
  94. device_release_driver(&serio->dev);
  95. up_write(&serio_bus.subsys.rwsem);
  96. }
  97. static void serio_find_driver(struct serio *serio)
  98. {
  99. down_write(&serio_bus.subsys.rwsem);
  100. device_attach(&serio->dev);
  101. up_write(&serio_bus.subsys.rwsem);
  102. }
  103. /*
  104. * Serio event processing.
  105. */
  106. enum serio_event_type {
  107. SERIO_RESCAN,
  108. SERIO_RECONNECT,
  109. SERIO_REGISTER_PORT,
  110. SERIO_UNREGISTER_PORT,
  111. SERIO_REGISTER_DRIVER,
  112. };
  113. struct serio_event {
  114. enum serio_event_type type;
  115. void *object;
  116. struct module *owner;
  117. struct list_head node;
  118. };
  119. static DEFINE_SPINLOCK(serio_event_lock); /* protects serio_event_list */
  120. static LIST_HEAD(serio_event_list);
  121. static DECLARE_WAIT_QUEUE_HEAD(serio_wait);
  122. static DECLARE_COMPLETION(serio_exited);
  123. static int serio_pid;
  124. static void serio_queue_event(void *object, struct module *owner,
  125. enum serio_event_type event_type)
  126. {
  127. unsigned long flags;
  128. struct serio_event *event;
  129. spin_lock_irqsave(&serio_event_lock, flags);
  130. /*
  131. * Scan event list for the other events for the same serio port,
  132. * starting with the most recent one. If event is the same we
  133. * do not need add new one. If event is of different type we
  134. * need to add this event and should not look further because
  135. * we need to preseve sequence of distinct events.
  136. */
  137. list_for_each_entry_reverse(event, &serio_event_list, node) {
  138. if (event->object == object) {
  139. if (event->type == event_type)
  140. goto out;
  141. break;
  142. }
  143. }
  144. if ((event = kmalloc(sizeof(struct serio_event), GFP_ATOMIC))) {
  145. if (!try_module_get(owner)) {
  146. printk(KERN_WARNING "serio: Can't get module reference, dropping event %d\n", event_type);
  147. goto out;
  148. }
  149. event->type = event_type;
  150. event->object = object;
  151. event->owner = owner;
  152. list_add_tail(&event->node, &serio_event_list);
  153. wake_up(&serio_wait);
  154. } else {
  155. printk(KERN_ERR "serio: Not enough memory to queue event %d\n", event_type);
  156. }
  157. out:
  158. spin_unlock_irqrestore(&serio_event_lock, flags);
  159. }
  160. static void serio_free_event(struct serio_event *event)
  161. {
  162. module_put(event->owner);
  163. kfree(event);
  164. }
  165. static void serio_remove_duplicate_events(struct serio_event *event)
  166. {
  167. struct list_head *node, *next;
  168. struct serio_event *e;
  169. unsigned long flags;
  170. spin_lock_irqsave(&serio_event_lock, flags);
  171. list_for_each_safe(node, next, &serio_event_list) {
  172. e = list_entry(node, struct serio_event, node);
  173. if (event->object == e->object) {
  174. /*
  175. * If this event is of different type we should not
  176. * look further - we only suppress duplicate events
  177. * that were sent back-to-back.
  178. */
  179. if (event->type != e->type)
  180. break;
  181. list_del_init(node);
  182. serio_free_event(e);
  183. }
  184. }
  185. spin_unlock_irqrestore(&serio_event_lock, flags);
  186. }
  187. static struct serio_event *serio_get_event(void)
  188. {
  189. struct serio_event *event;
  190. struct list_head *node;
  191. unsigned long flags;
  192. spin_lock_irqsave(&serio_event_lock, flags);
  193. if (list_empty(&serio_event_list)) {
  194. spin_unlock_irqrestore(&serio_event_lock, flags);
  195. return NULL;
  196. }
  197. node = serio_event_list.next;
  198. event = list_entry(node, struct serio_event, node);
  199. list_del_init(node);
  200. spin_unlock_irqrestore(&serio_event_lock, flags);
  201. return event;
  202. }
  203. static void serio_handle_events(void)
  204. {
  205. struct serio_event *event;
  206. struct serio_driver *serio_drv;
  207. down(&serio_sem);
  208. while ((event = serio_get_event())) {
  209. switch (event->type) {
  210. case SERIO_REGISTER_PORT:
  211. serio_add_port(event->object);
  212. break;
  213. case SERIO_UNREGISTER_PORT:
  214. serio_disconnect_port(event->object);
  215. serio_destroy_port(event->object);
  216. break;
  217. case SERIO_RECONNECT:
  218. serio_reconnect_port(event->object);
  219. break;
  220. case SERIO_RESCAN:
  221. serio_disconnect_port(event->object);
  222. serio_find_driver(event->object);
  223. break;
  224. case SERIO_REGISTER_DRIVER:
  225. serio_drv = event->object;
  226. driver_register(&serio_drv->driver);
  227. break;
  228. default:
  229. break;
  230. }
  231. serio_remove_duplicate_events(event);
  232. serio_free_event(event);
  233. }
  234. up(&serio_sem);
  235. }
  236. /*
  237. * Remove all events that have been submitted for a given serio port.
  238. */
  239. static void serio_remove_pending_events(struct serio *serio)
  240. {
  241. struct list_head *node, *next;
  242. struct serio_event *event;
  243. unsigned long flags;
  244. spin_lock_irqsave(&serio_event_lock, flags);
  245. list_for_each_safe(node, next, &serio_event_list) {
  246. event = list_entry(node, struct serio_event, node);
  247. if (event->object == serio) {
  248. list_del_init(node);
  249. serio_free_event(event);
  250. }
  251. }
  252. spin_unlock_irqrestore(&serio_event_lock, flags);
  253. }
  254. /*
  255. * Destroy child serio port (if any) that has not been fully registered yet.
  256. *
  257. * Note that we rely on the fact that port can have only one child and therefore
  258. * only one child registration request can be pending. Additionally, children
  259. * are registered by driver's connect() handler so there can't be a grandchild
  260. * pending registration together with a child.
  261. */
  262. static struct serio *serio_get_pending_child(struct serio *parent)
  263. {
  264. struct serio_event *event;
  265. struct serio *serio, *child = NULL;
  266. unsigned long flags;
  267. spin_lock_irqsave(&serio_event_lock, flags);
  268. list_for_each_entry(event, &serio_event_list, node) {
  269. if (event->type == SERIO_REGISTER_PORT) {
  270. serio = event->object;
  271. if (serio->parent == parent) {
  272. child = serio;
  273. break;
  274. }
  275. }
  276. }
  277. spin_unlock_irqrestore(&serio_event_lock, flags);
  278. return child;
  279. }
  280. static int serio_thread(void *nothing)
  281. {
  282. lock_kernel();
  283. daemonize("kseriod");
  284. allow_signal(SIGTERM);
  285. do {
  286. serio_handle_events();
  287. wait_event_interruptible(serio_wait, !list_empty(&serio_event_list));
  288. try_to_freeze();
  289. } while (!signal_pending(current));
  290. printk(KERN_DEBUG "serio: kseriod exiting\n");
  291. unlock_kernel();
  292. complete_and_exit(&serio_exited, 0);
  293. }
  294. /*
  295. * Serio port operations
  296. */
  297. static ssize_t serio_show_description(struct device *dev, struct device_attribute *attr, char *buf)
  298. {
  299. struct serio *serio = to_serio_port(dev);
  300. return sprintf(buf, "%s\n", serio->name);
  301. }
  302. static ssize_t serio_show_id_type(struct device *dev, struct device_attribute *attr, char *buf)
  303. {
  304. struct serio *serio = to_serio_port(dev);
  305. return sprintf(buf, "%02x\n", serio->id.type);
  306. }
  307. static ssize_t serio_show_id_proto(struct device *dev, struct device_attribute *attr, char *buf)
  308. {
  309. struct serio *serio = to_serio_port(dev);
  310. return sprintf(buf, "%02x\n", serio->id.proto);
  311. }
  312. static ssize_t serio_show_id_id(struct device *dev, struct device_attribute *attr, char *buf)
  313. {
  314. struct serio *serio = to_serio_port(dev);
  315. return sprintf(buf, "%02x\n", serio->id.id);
  316. }
  317. static ssize_t serio_show_id_extra(struct device *dev, struct device_attribute *attr, char *buf)
  318. {
  319. struct serio *serio = to_serio_port(dev);
  320. return sprintf(buf, "%02x\n", serio->id.extra);
  321. }
  322. static DEVICE_ATTR(type, S_IRUGO, serio_show_id_type, NULL);
  323. static DEVICE_ATTR(proto, S_IRUGO, serio_show_id_proto, NULL);
  324. static DEVICE_ATTR(id, S_IRUGO, serio_show_id_id, NULL);
  325. static DEVICE_ATTR(extra, S_IRUGO, serio_show_id_extra, NULL);
  326. static struct attribute *serio_device_id_attrs[] = {
  327. &dev_attr_type.attr,
  328. &dev_attr_proto.attr,
  329. &dev_attr_id.attr,
  330. &dev_attr_extra.attr,
  331. NULL
  332. };
  333. static struct attribute_group serio_id_attr_group = {
  334. .name = "id",
  335. .attrs = serio_device_id_attrs,
  336. };
  337. static ssize_t serio_rebind_driver(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  338. {
  339. struct serio *serio = to_serio_port(dev);
  340. struct device_driver *drv;
  341. int retval;
  342. retval = down_interruptible(&serio_sem);
  343. if (retval)
  344. return retval;
  345. retval = count;
  346. if (!strncmp(buf, "none", count)) {
  347. serio_disconnect_port(serio);
  348. } else if (!strncmp(buf, "reconnect", count)) {
  349. serio_reconnect_port(serio);
  350. } else if (!strncmp(buf, "rescan", count)) {
  351. serio_disconnect_port(serio);
  352. serio_find_driver(serio);
  353. } else if ((drv = driver_find(buf, &serio_bus)) != NULL) {
  354. serio_disconnect_port(serio);
  355. serio_bind_driver(serio, to_serio_driver(drv));
  356. put_driver(drv);
  357. } else {
  358. retval = -EINVAL;
  359. }
  360. up(&serio_sem);
  361. return retval;
  362. }
  363. static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
  364. {
  365. struct serio *serio = to_serio_port(dev);
  366. return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
  367. }
  368. static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  369. {
  370. struct serio *serio = to_serio_port(dev);
  371. int retval;
  372. retval = count;
  373. if (!strncmp(buf, "manual", count)) {
  374. serio->manual_bind = 1;
  375. } else if (!strncmp(buf, "auto", count)) {
  376. serio->manual_bind = 0;
  377. } else {
  378. retval = -EINVAL;
  379. }
  380. return retval;
  381. }
  382. static struct device_attribute serio_device_attrs[] = {
  383. __ATTR(description, S_IRUGO, serio_show_description, NULL),
  384. __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
  385. __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
  386. __ATTR_NULL
  387. };
  388. static void serio_release_port(struct device *dev)
  389. {
  390. struct serio *serio = to_serio_port(dev);
  391. kfree(serio);
  392. module_put(THIS_MODULE);
  393. }
  394. /*
  395. * Prepare serio port for registration.
  396. */
  397. static void serio_init_port(struct serio *serio)
  398. {
  399. static atomic_t serio_no = ATOMIC_INIT(0);
  400. __module_get(THIS_MODULE);
  401. spin_lock_init(&serio->lock);
  402. init_MUTEX(&serio->drv_sem);
  403. device_initialize(&serio->dev);
  404. snprintf(serio->dev.bus_id, sizeof(serio->dev.bus_id),
  405. "serio%ld", (long)atomic_inc_return(&serio_no) - 1);
  406. serio->dev.bus = &serio_bus;
  407. serio->dev.release = serio_release_port;
  408. if (serio->parent)
  409. serio->dev.parent = &serio->parent->dev;
  410. }
  411. /*
  412. * Complete serio port registration.
  413. * Driver core will attempt to find appropriate driver for the port.
  414. */
  415. static void serio_add_port(struct serio *serio)
  416. {
  417. if (serio->parent) {
  418. serio_pause_rx(serio->parent);
  419. serio->parent->child = serio;
  420. serio_continue_rx(serio->parent);
  421. }
  422. list_add_tail(&serio->node, &serio_list);
  423. if (serio->start)
  424. serio->start(serio);
  425. device_add(&serio->dev);
  426. sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
  427. serio->registered = 1;
  428. }
  429. /*
  430. * serio_destroy_port() completes deregistration process and removes
  431. * port from the system
  432. */
  433. static void serio_destroy_port(struct serio *serio)
  434. {
  435. struct serio *child;
  436. child = serio_get_pending_child(serio);
  437. if (child) {
  438. serio_remove_pending_events(child);
  439. put_device(&child->dev);
  440. }
  441. if (serio->stop)
  442. serio->stop(serio);
  443. if (serio->parent) {
  444. serio_pause_rx(serio->parent);
  445. serio->parent->child = NULL;
  446. serio_continue_rx(serio->parent);
  447. serio->parent = NULL;
  448. }
  449. if (serio->registered) {
  450. sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
  451. device_del(&serio->dev);
  452. list_del_init(&serio->node);
  453. serio->registered = 0;
  454. }
  455. serio_remove_pending_events(serio);
  456. put_device(&serio->dev);
  457. }
  458. /*
  459. * Reconnect serio port and all its children (re-initialize attached devices)
  460. */
  461. static void serio_reconnect_port(struct serio *serio)
  462. {
  463. do {
  464. if (!serio->drv || !serio->drv->reconnect || serio->drv->reconnect(serio)) {
  465. serio_disconnect_port(serio);
  466. serio_find_driver(serio);
  467. /* Ok, old children are now gone, we are done */
  468. break;
  469. }
  470. serio = serio->child;
  471. } while (serio);
  472. }
  473. /*
  474. * serio_disconnect_port() unbinds a port from its driver. As a side effect
  475. * all child ports are unbound and destroyed.
  476. */
  477. static void serio_disconnect_port(struct serio *serio)
  478. {
  479. struct serio *s, *parent;
  480. if (serio->child) {
  481. /*
  482. * Children ports should be disconnected and destroyed
  483. * first, staring with the leaf one, since we don't want
  484. * to do recursion
  485. */
  486. for (s = serio; s->child; s = s->child)
  487. /* empty */;
  488. do {
  489. parent = s->parent;
  490. serio_release_driver(s);
  491. serio_destroy_port(s);
  492. } while ((s = parent) != serio);
  493. }
  494. /*
  495. * Ok, no children left, now disconnect this port
  496. */
  497. serio_release_driver(serio);
  498. }
  499. void serio_rescan(struct serio *serio)
  500. {
  501. serio_queue_event(serio, NULL, SERIO_RESCAN);
  502. }
  503. void serio_reconnect(struct serio *serio)
  504. {
  505. serio_queue_event(serio, NULL, SERIO_RECONNECT);
  506. }
  507. /*
  508. * Submits register request to kseriod for subsequent execution.
  509. * Note that port registration is always asynchronous.
  510. */
  511. void __serio_register_port(struct serio *serio, struct module *owner)
  512. {
  513. serio_init_port(serio);
  514. serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
  515. }
  516. /*
  517. * Synchronously unregisters serio port.
  518. */
  519. void serio_unregister_port(struct serio *serio)
  520. {
  521. down(&serio_sem);
  522. serio_disconnect_port(serio);
  523. serio_destroy_port(serio);
  524. up(&serio_sem);
  525. }
  526. /*
  527. * Submits register request to kseriod for subsequent execution.
  528. * Can be used when it is not obvious whether the serio_sem is
  529. * taken or not and when delayed execution is feasible.
  530. */
  531. void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
  532. {
  533. serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
  534. }
  535. /*
  536. * Serio driver operations
  537. */
  538. static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
  539. {
  540. struct serio_driver *driver = to_serio_driver(drv);
  541. return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
  542. }
  543. static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
  544. {
  545. struct serio_driver *serio_drv = to_serio_driver(drv);
  546. return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
  547. }
  548. static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
  549. {
  550. struct serio_driver *serio_drv = to_serio_driver(drv);
  551. int retval;
  552. retval = count;
  553. if (!strncmp(buf, "manual", count)) {
  554. serio_drv->manual_bind = 1;
  555. } else if (!strncmp(buf, "auto", count)) {
  556. serio_drv->manual_bind = 0;
  557. } else {
  558. retval = -EINVAL;
  559. }
  560. return retval;
  561. }
  562. static struct driver_attribute serio_driver_attrs[] = {
  563. __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
  564. __ATTR(bind_mode, S_IWUSR | S_IRUGO,
  565. serio_driver_show_bind_mode, serio_driver_set_bind_mode),
  566. __ATTR_NULL
  567. };
  568. static int serio_driver_probe(struct device *dev)
  569. {
  570. struct serio *serio = to_serio_port(dev);
  571. struct serio_driver *drv = to_serio_driver(dev->driver);
  572. return drv->connect(serio, drv);
  573. }
  574. static int serio_driver_remove(struct device *dev)
  575. {
  576. struct serio *serio = to_serio_port(dev);
  577. struct serio_driver *drv = to_serio_driver(dev->driver);
  578. drv->disconnect(serio);
  579. return 0;
  580. }
  581. void __serio_register_driver(struct serio_driver *drv, struct module *owner)
  582. {
  583. drv->driver.bus = &serio_bus;
  584. drv->driver.probe = serio_driver_probe;
  585. drv->driver.remove = serio_driver_remove;
  586. serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
  587. }
  588. void serio_unregister_driver(struct serio_driver *drv)
  589. {
  590. struct serio *serio;
  591. down(&serio_sem);
  592. drv->manual_bind = 1; /* so serio_find_driver ignores it */
  593. start_over:
  594. list_for_each_entry(serio, &serio_list, node) {
  595. if (serio->drv == drv) {
  596. serio_disconnect_port(serio);
  597. serio_find_driver(serio);
  598. /* we could've deleted some ports, restart */
  599. goto start_over;
  600. }
  601. }
  602. driver_unregister(&drv->driver);
  603. up(&serio_sem);
  604. }
  605. static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
  606. {
  607. down(&serio->drv_sem);
  608. serio_pause_rx(serio);
  609. serio->drv = drv;
  610. serio_continue_rx(serio);
  611. up(&serio->drv_sem);
  612. }
  613. static int serio_bus_match(struct device *dev, struct device_driver *drv)
  614. {
  615. struct serio *serio = to_serio_port(dev);
  616. struct serio_driver *serio_drv = to_serio_driver(drv);
  617. if (serio->manual_bind || serio_drv->manual_bind)
  618. return 0;
  619. return serio_match_port(serio_drv->id_table, serio);
  620. }
  621. #ifdef CONFIG_HOTPLUG
  622. #define PUT_ENVP(fmt, val) \
  623. do { \
  624. envp[i++] = buffer; \
  625. length += snprintf(buffer, buffer_size - length, fmt, val); \
  626. if (buffer_size - length <= 0 || i >= num_envp) \
  627. return -ENOMEM; \
  628. length++; \
  629. buffer += length; \
  630. } while (0)
  631. static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  632. {
  633. struct serio *serio;
  634. int i = 0;
  635. int length = 0;
  636. if (!dev)
  637. return -ENODEV;
  638. serio = to_serio_port(dev);
  639. PUT_ENVP("SERIO_TYPE=%02x", serio->id.type);
  640. PUT_ENVP("SERIO_PROTO=%02x", serio->id.proto);
  641. PUT_ENVP("SERIO_ID=%02x", serio->id.id);
  642. PUT_ENVP("SERIO_EXTRA=%02x", serio->id.extra);
  643. envp[i] = NULL;
  644. return 0;
  645. }
  646. #undef PUT_ENVP
  647. #else
  648. static int serio_hotplug(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  649. {
  650. return -ENODEV;
  651. }
  652. #endif /* CONFIG_HOTPLUG */
  653. static int serio_resume(struct device *dev)
  654. {
  655. struct serio *serio = to_serio_port(dev);
  656. if (!serio->drv || !serio->drv->reconnect || serio->drv->reconnect(serio)) {
  657. /*
  658. * Driver re-probing can take a while, so better let kseriod
  659. * deal with it.
  660. */
  661. serio_rescan(serio);
  662. }
  663. return 0;
  664. }
  665. /* called from serio_driver->connect/disconnect methods under serio_sem */
  666. int serio_open(struct serio *serio, struct serio_driver *drv)
  667. {
  668. serio_set_drv(serio, drv);
  669. if (serio->open && serio->open(serio)) {
  670. serio_set_drv(serio, NULL);
  671. return -1;
  672. }
  673. return 0;
  674. }
  675. /* called from serio_driver->connect/disconnect methods under serio_sem */
  676. void serio_close(struct serio *serio)
  677. {
  678. if (serio->close)
  679. serio->close(serio);
  680. serio_set_drv(serio, NULL);
  681. }
  682. irqreturn_t serio_interrupt(struct serio *serio,
  683. unsigned char data, unsigned int dfl, struct pt_regs *regs)
  684. {
  685. unsigned long flags;
  686. irqreturn_t ret = IRQ_NONE;
  687. spin_lock_irqsave(&serio->lock, flags);
  688. if (likely(serio->drv)) {
  689. ret = serio->drv->interrupt(serio, data, dfl, regs);
  690. } else if (!dfl && serio->registered) {
  691. serio_rescan(serio);
  692. ret = IRQ_HANDLED;
  693. }
  694. spin_unlock_irqrestore(&serio->lock, flags);
  695. return ret;
  696. }
  697. static int __init serio_init(void)
  698. {
  699. if (!(serio_pid = kernel_thread(serio_thread, NULL, CLONE_KERNEL))) {
  700. printk(KERN_ERR "serio: Failed to start kseriod\n");
  701. return -1;
  702. }
  703. serio_bus.dev_attrs = serio_device_attrs;
  704. serio_bus.drv_attrs = serio_driver_attrs;
  705. serio_bus.match = serio_bus_match;
  706. serio_bus.hotplug = serio_hotplug;
  707. serio_bus.resume = serio_resume;
  708. bus_register(&serio_bus);
  709. return 0;
  710. }
  711. static void __exit serio_exit(void)
  712. {
  713. bus_unregister(&serio_bus);
  714. kill_proc(serio_pid, SIGTERM, 1);
  715. wait_for_completion(&serio_exited);
  716. }
  717. module_init(serio_init);
  718. module_exit(serio_exit);