serio.c 24 KB

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