serio.c 24 KB

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