serio.c 24 KB

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