serio.c 22 KB

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