serio.c 23 KB

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