serio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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. serio->depth = serio->parent->depth + 1;
  449. } else
  450. serio->depth = 0;
  451. lockdep_set_subclass(&serio->lock, serio->depth);
  452. }
  453. /*
  454. * Complete serio port registration.
  455. * Driver core will attempt to find appropriate driver for the port.
  456. */
  457. static void serio_add_port(struct serio *serio)
  458. {
  459. int error;
  460. if (serio->parent) {
  461. serio_pause_rx(serio->parent);
  462. serio->parent->child = serio;
  463. serio_continue_rx(serio->parent);
  464. }
  465. list_add_tail(&serio->node, &serio_list);
  466. if (serio->start)
  467. serio->start(serio);
  468. error = device_add(&serio->dev);
  469. if (error)
  470. printk(KERN_ERR
  471. "serio: device_add() failed for %s (%s), error: %d\n",
  472. serio->phys, serio->name, error);
  473. else {
  474. serio->registered = 1;
  475. error = sysfs_create_group(&serio->dev.kobj, &serio_id_attr_group);
  476. if (error)
  477. printk(KERN_ERR
  478. "serio: sysfs_create_group() failed for %s (%s), error: %d\n",
  479. serio->phys, serio->name, error);
  480. }
  481. }
  482. /*
  483. * serio_destroy_port() completes deregistration process and removes
  484. * port from the system
  485. */
  486. static void serio_destroy_port(struct serio *serio)
  487. {
  488. struct serio *child;
  489. child = serio_get_pending_child(serio);
  490. if (child) {
  491. serio_remove_pending_events(child);
  492. put_device(&child->dev);
  493. }
  494. if (serio->stop)
  495. serio->stop(serio);
  496. if (serio->parent) {
  497. serio_pause_rx(serio->parent);
  498. serio->parent->child = NULL;
  499. serio_continue_rx(serio->parent);
  500. serio->parent = NULL;
  501. }
  502. if (serio->registered) {
  503. sysfs_remove_group(&serio->dev.kobj, &serio_id_attr_group);
  504. device_del(&serio->dev);
  505. serio->registered = 0;
  506. }
  507. list_del_init(&serio->node);
  508. serio_remove_pending_events(serio);
  509. put_device(&serio->dev);
  510. }
  511. /*
  512. * Reconnect serio port and all its children (re-initialize attached devices)
  513. */
  514. static void serio_reconnect_port(struct serio *serio)
  515. {
  516. do {
  517. if (serio_reconnect_driver(serio)) {
  518. serio_disconnect_port(serio);
  519. serio_find_driver(serio);
  520. /* Ok, old children are now gone, we are done */
  521. break;
  522. }
  523. serio = serio->child;
  524. } while (serio);
  525. }
  526. /*
  527. * serio_disconnect_port() unbinds a port from its driver. As a side effect
  528. * all child ports are unbound and destroyed.
  529. */
  530. static void serio_disconnect_port(struct serio *serio)
  531. {
  532. struct serio *s, *parent;
  533. if (serio->child) {
  534. /*
  535. * Children ports should be disconnected and destroyed
  536. * first, staring with the leaf one, since we don't want
  537. * to do recursion
  538. */
  539. for (s = serio; s->child; s = s->child)
  540. /* empty */;
  541. do {
  542. parent = s->parent;
  543. serio_release_driver(s);
  544. serio_destroy_port(s);
  545. } while ((s = parent) != serio);
  546. }
  547. /*
  548. * Ok, no children left, now disconnect this port
  549. */
  550. serio_release_driver(serio);
  551. }
  552. void serio_rescan(struct serio *serio)
  553. {
  554. serio_queue_event(serio, NULL, SERIO_RESCAN);
  555. }
  556. void serio_reconnect(struct serio *serio)
  557. {
  558. serio_queue_event(serio, NULL, SERIO_RECONNECT);
  559. }
  560. /*
  561. * Submits register request to kseriod for subsequent execution.
  562. * Note that port registration is always asynchronous.
  563. */
  564. void __serio_register_port(struct serio *serio, struct module *owner)
  565. {
  566. serio_init_port(serio);
  567. serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
  568. }
  569. /*
  570. * Synchronously unregisters serio port.
  571. */
  572. void serio_unregister_port(struct serio *serio)
  573. {
  574. mutex_lock(&serio_mutex);
  575. serio_disconnect_port(serio);
  576. serio_destroy_port(serio);
  577. mutex_unlock(&serio_mutex);
  578. }
  579. /*
  580. * Safely unregisters child port if one is present.
  581. */
  582. void serio_unregister_child_port(struct serio *serio)
  583. {
  584. mutex_lock(&serio_mutex);
  585. if (serio->child) {
  586. serio_disconnect_port(serio->child);
  587. serio_destroy_port(serio->child);
  588. }
  589. mutex_unlock(&serio_mutex);
  590. }
  591. /*
  592. * Submits register request to kseriod for subsequent execution.
  593. * Can be used when it is not obvious whether the serio_mutex is
  594. * taken or not and when delayed execution is feasible.
  595. */
  596. void __serio_unregister_port_delayed(struct serio *serio, struct module *owner)
  597. {
  598. serio_queue_event(serio, owner, SERIO_UNREGISTER_PORT);
  599. }
  600. /*
  601. * Serio driver operations
  602. */
  603. static ssize_t serio_driver_show_description(struct device_driver *drv, char *buf)
  604. {
  605. struct serio_driver *driver = to_serio_driver(drv);
  606. return sprintf(buf, "%s\n", driver->description ? driver->description : "(none)");
  607. }
  608. static ssize_t serio_driver_show_bind_mode(struct device_driver *drv, char *buf)
  609. {
  610. struct serio_driver *serio_drv = to_serio_driver(drv);
  611. return sprintf(buf, "%s\n", serio_drv->manual_bind ? "manual" : "auto");
  612. }
  613. static ssize_t serio_driver_set_bind_mode(struct device_driver *drv, const char *buf, size_t count)
  614. {
  615. struct serio_driver *serio_drv = to_serio_driver(drv);
  616. int retval;
  617. retval = count;
  618. if (!strncmp(buf, "manual", count)) {
  619. serio_drv->manual_bind = 1;
  620. } else if (!strncmp(buf, "auto", count)) {
  621. serio_drv->manual_bind = 0;
  622. } else {
  623. retval = -EINVAL;
  624. }
  625. return retval;
  626. }
  627. static struct driver_attribute serio_driver_attrs[] = {
  628. __ATTR(description, S_IRUGO, serio_driver_show_description, NULL),
  629. __ATTR(bind_mode, S_IWUSR | S_IRUGO,
  630. serio_driver_show_bind_mode, serio_driver_set_bind_mode),
  631. __ATTR_NULL
  632. };
  633. static int serio_driver_probe(struct device *dev)
  634. {
  635. struct serio *serio = to_serio_port(dev);
  636. struct serio_driver *drv = to_serio_driver(dev->driver);
  637. return serio_connect_driver(serio, drv);
  638. }
  639. static int serio_driver_remove(struct device *dev)
  640. {
  641. struct serio *serio = to_serio_port(dev);
  642. serio_disconnect_driver(serio);
  643. return 0;
  644. }
  645. static struct bus_type serio_bus = {
  646. .name = "serio",
  647. .probe = serio_driver_probe,
  648. .remove = serio_driver_remove,
  649. };
  650. static void serio_add_driver(struct serio_driver *drv)
  651. {
  652. int error;
  653. error = driver_register(&drv->driver);
  654. if (error)
  655. printk(KERN_ERR
  656. "serio: driver_register() failed for %s, error: %d\n",
  657. drv->driver.name, error);
  658. }
  659. void __serio_register_driver(struct serio_driver *drv, struct module *owner)
  660. {
  661. drv->driver.bus = &serio_bus;
  662. serio_queue_event(drv, owner, SERIO_REGISTER_DRIVER);
  663. }
  664. void serio_unregister_driver(struct serio_driver *drv)
  665. {
  666. struct serio *serio;
  667. mutex_lock(&serio_mutex);
  668. drv->manual_bind = 1; /* so serio_find_driver ignores it */
  669. start_over:
  670. list_for_each_entry(serio, &serio_list, node) {
  671. if (serio->drv == drv) {
  672. serio_disconnect_port(serio);
  673. serio_find_driver(serio);
  674. /* we could've deleted some ports, restart */
  675. goto start_over;
  676. }
  677. }
  678. driver_unregister(&drv->driver);
  679. mutex_unlock(&serio_mutex);
  680. }
  681. static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
  682. {
  683. serio_pause_rx(serio);
  684. serio->drv = drv;
  685. serio_continue_rx(serio);
  686. }
  687. static int serio_bus_match(struct device *dev, struct device_driver *drv)
  688. {
  689. struct serio *serio = to_serio_port(dev);
  690. struct serio_driver *serio_drv = to_serio_driver(drv);
  691. if (serio->manual_bind || serio_drv->manual_bind)
  692. return 0;
  693. return serio_match_port(serio_drv->id_table, serio);
  694. }
  695. #ifdef CONFIG_HOTPLUG
  696. #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
  697. do { \
  698. int err = add_uevent_var(envp, num_envp, &i, \
  699. buffer, buffer_size, &len, \
  700. fmt, val); \
  701. if (err) \
  702. return err; \
  703. } while (0)
  704. static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  705. {
  706. struct serio *serio;
  707. int i = 0;
  708. int len = 0;
  709. if (!dev)
  710. return -ENODEV;
  711. serio = to_serio_port(dev);
  712. SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
  713. SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
  714. SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
  715. SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
  716. SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
  717. serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
  718. envp[i] = NULL;
  719. return 0;
  720. }
  721. #undef SERIO_ADD_UEVENT_VAR
  722. #else
  723. static int serio_uevent(struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size)
  724. {
  725. return -ENODEV;
  726. }
  727. #endif /* CONFIG_HOTPLUG */
  728. static int serio_resume(struct device *dev)
  729. {
  730. struct serio *serio = to_serio_port(dev);
  731. if (serio_reconnect_driver(serio)) {
  732. /*
  733. * Driver re-probing can take a while, so better let kseriod
  734. * deal with it.
  735. */
  736. serio_rescan(serio);
  737. }
  738. return 0;
  739. }
  740. /* called from serio_driver->connect/disconnect methods under serio_mutex */
  741. int serio_open(struct serio *serio, struct serio_driver *drv)
  742. {
  743. serio_set_drv(serio, drv);
  744. if (serio->open && serio->open(serio)) {
  745. serio_set_drv(serio, NULL);
  746. return -1;
  747. }
  748. return 0;
  749. }
  750. /* called from serio_driver->connect/disconnect methods under serio_mutex */
  751. void serio_close(struct serio *serio)
  752. {
  753. if (serio->close)
  754. serio->close(serio);
  755. serio_set_drv(serio, NULL);
  756. }
  757. irqreturn_t serio_interrupt(struct serio *serio,
  758. unsigned char data, unsigned int dfl)
  759. {
  760. unsigned long flags;
  761. irqreturn_t ret = IRQ_NONE;
  762. spin_lock_irqsave(&serio->lock, flags);
  763. if (likely(serio->drv)) {
  764. ret = serio->drv->interrupt(serio, data, dfl);
  765. } else if (!dfl && serio->registered) {
  766. serio_rescan(serio);
  767. ret = IRQ_HANDLED;
  768. }
  769. spin_unlock_irqrestore(&serio->lock, flags);
  770. return ret;
  771. }
  772. static int __init serio_init(void)
  773. {
  774. int error;
  775. serio_bus.dev_attrs = serio_device_attrs;
  776. serio_bus.drv_attrs = serio_driver_attrs;
  777. serio_bus.match = serio_bus_match;
  778. serio_bus.uevent = serio_uevent;
  779. serio_bus.resume = serio_resume;
  780. error = bus_register(&serio_bus);
  781. if (error) {
  782. printk(KERN_ERR "serio: failed to register serio bus, error: %d\n", error);
  783. return error;
  784. }
  785. serio_task = kthread_run(serio_thread, NULL, "kseriod");
  786. if (IS_ERR(serio_task)) {
  787. bus_unregister(&serio_bus);
  788. error = PTR_ERR(serio_task);
  789. printk(KERN_ERR "serio: Failed to start kseriod, error: %d\n", error);
  790. return error;
  791. }
  792. return 0;
  793. }
  794. static void __exit serio_exit(void)
  795. {
  796. bus_unregister(&serio_bus);
  797. kthread_stop(serio_task);
  798. }
  799. subsys_initcall(serio_init);
  800. module_exit(serio_exit);