serio.c 25 KB

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