serio.c 22 KB

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