serio.c 22 KB

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