serio.c 24 KB

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