serio.c 25 KB

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