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. put_driver(drv);
  364. serio_remove_duplicate_events(serio, SERIO_RESCAN_PORT);
  365. } else {
  366. error = -EINVAL;
  367. }
  368. mutex_unlock(&serio_mutex);
  369. return error ? error : count;
  370. }
  371. static ssize_t serio_show_bind_mode(struct device *dev, struct device_attribute *attr, char *buf)
  372. {
  373. struct serio *serio = to_serio_port(dev);
  374. return sprintf(buf, "%s\n", serio->manual_bind ? "manual" : "auto");
  375. }
  376. static ssize_t serio_set_bind_mode(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  377. {
  378. struct serio *serio = to_serio_port(dev);
  379. int retval;
  380. retval = count;
  381. if (!strncmp(buf, "manual", count)) {
  382. serio->manual_bind = true;
  383. } else if (!strncmp(buf, "auto", count)) {
  384. serio->manual_bind = false;
  385. } else {
  386. retval = -EINVAL;
  387. }
  388. return retval;
  389. }
  390. static struct device_attribute serio_device_attrs[] = {
  391. __ATTR(description, S_IRUGO, serio_show_description, NULL),
  392. __ATTR(modalias, S_IRUGO, serio_show_modalias, NULL),
  393. __ATTR(drvctl, S_IWUSR, NULL, serio_rebind_driver),
  394. __ATTR(bind_mode, S_IWUSR | S_IRUGO, serio_show_bind_mode, serio_set_bind_mode),
  395. __ATTR_NULL
  396. };
  397. static void serio_release_port(struct device *dev)
  398. {
  399. struct serio *serio = to_serio_port(dev);
  400. kfree(serio);
  401. module_put(THIS_MODULE);
  402. }
  403. /*
  404. * Prepare serio port for registration.
  405. */
  406. static void serio_init_port(struct serio *serio)
  407. {
  408. static atomic_t serio_no = ATOMIC_INIT(0);
  409. __module_get(THIS_MODULE);
  410. INIT_LIST_HEAD(&serio->node);
  411. INIT_LIST_HEAD(&serio->child_node);
  412. INIT_LIST_HEAD(&serio->children);
  413. spin_lock_init(&serio->lock);
  414. mutex_init(&serio->drv_mutex);
  415. device_initialize(&serio->dev);
  416. dev_set_name(&serio->dev, "serio%ld",
  417. (long)atomic_inc_return(&serio_no) - 1);
  418. serio->dev.bus = &serio_bus;
  419. serio->dev.release = serio_release_port;
  420. serio->dev.groups = serio_device_attr_groups;
  421. if (serio->parent) {
  422. serio->dev.parent = &serio->parent->dev;
  423. serio->depth = serio->parent->depth + 1;
  424. } else
  425. serio->depth = 0;
  426. lockdep_set_subclass(&serio->lock, serio->depth);
  427. }
  428. /*
  429. * Complete serio port registration.
  430. * Driver core will attempt to find appropriate driver for the port.
  431. */
  432. static void serio_add_port(struct serio *serio)
  433. {
  434. struct serio *parent = serio->parent;
  435. int error;
  436. if (parent) {
  437. serio_pause_rx(parent);
  438. list_add_tail(&serio->child_node, &parent->children);
  439. serio_continue_rx(parent);
  440. }
  441. list_add_tail(&serio->node, &serio_list);
  442. if (serio->start)
  443. serio->start(serio);
  444. error = device_add(&serio->dev);
  445. if (error)
  446. dev_err(&serio->dev,
  447. "device_add() failed for %s (%s), error: %d\n",
  448. serio->phys, serio->name, error);
  449. }
  450. /*
  451. * serio_destroy_port() completes unregistration process and removes
  452. * port from the system
  453. */
  454. static void serio_destroy_port(struct serio *serio)
  455. {
  456. struct serio *child;
  457. while ((child = serio_get_pending_child(serio)) != NULL) {
  458. serio_remove_pending_events(child);
  459. put_device(&child->dev);
  460. }
  461. if (serio->stop)
  462. serio->stop(serio);
  463. if (serio->parent) {
  464. serio_pause_rx(serio->parent);
  465. list_del_init(&serio->child_node);
  466. serio_continue_rx(serio->parent);
  467. serio->parent = NULL;
  468. }
  469. if (device_is_registered(&serio->dev))
  470. device_del(&serio->dev);
  471. list_del_init(&serio->node);
  472. serio_remove_pending_events(serio);
  473. put_device(&serio->dev);
  474. }
  475. /*
  476. * Reconnect serio port (re-initialize attached device).
  477. * If reconnect fails (old device is no longer attached or
  478. * there was no device to begin with) we do full rescan in
  479. * hope of finding a driver for the port.
  480. */
  481. static int serio_reconnect_port(struct serio *serio)
  482. {
  483. int error = serio_reconnect_driver(serio);
  484. if (error) {
  485. serio_disconnect_port(serio);
  486. serio_find_driver(serio);
  487. }
  488. return error;
  489. }
  490. /*
  491. * Reconnect serio port and all its children (re-initialize attached
  492. * devices).
  493. */
  494. static void serio_reconnect_subtree(struct serio *root)
  495. {
  496. struct serio *s = root;
  497. int error;
  498. do {
  499. error = serio_reconnect_port(s);
  500. if (!error) {
  501. /*
  502. * Reconnect was successful, move on to do the
  503. * first child.
  504. */
  505. if (!list_empty(&s->children)) {
  506. s = list_first_entry(&s->children,
  507. struct serio, child_node);
  508. continue;
  509. }
  510. }
  511. /*
  512. * Either it was a leaf node or reconnect failed and it
  513. * became a leaf node. Continue reconnecting starting with
  514. * the next sibling of the parent node.
  515. */
  516. while (s != root) {
  517. struct serio *parent = s->parent;
  518. if (!list_is_last(&s->child_node, &parent->children)) {
  519. s = list_entry(s->child_node.next,
  520. struct serio, child_node);
  521. break;
  522. }
  523. s = parent;
  524. }
  525. } while (s != root);
  526. }
  527. /*
  528. * serio_disconnect_port() unbinds a port from its driver. As a side effect
  529. * all children ports are unbound and destroyed.
  530. */
  531. static void serio_disconnect_port(struct serio *serio)
  532. {
  533. struct serio *s = serio;
  534. /*
  535. * Children ports should be disconnected and destroyed
  536. * first; we travel the tree in depth-first order.
  537. */
  538. while (!list_empty(&serio->children)) {
  539. /* Locate a leaf */
  540. while (!list_empty(&s->children))
  541. s = list_first_entry(&s->children,
  542. struct serio, child_node);
  543. /*
  544. * Prune this leaf node unless it is the one we
  545. * started with.
  546. */
  547. if (s != serio) {
  548. struct serio *parent = s->parent;
  549. device_release_driver(&s->dev);
  550. serio_destroy_port(s);
  551. s = parent;
  552. }
  553. }
  554. /*
  555. * OK, no children left, now disconnect this port.
  556. */
  557. device_release_driver(&serio->dev);
  558. }
  559. void serio_rescan(struct serio *serio)
  560. {
  561. serio_queue_event(serio, NULL, SERIO_RESCAN_PORT);
  562. }
  563. EXPORT_SYMBOL(serio_rescan);
  564. void serio_reconnect(struct serio *serio)
  565. {
  566. serio_queue_event(serio, NULL, SERIO_RECONNECT_SUBTREE);
  567. }
  568. EXPORT_SYMBOL(serio_reconnect);
  569. /*
  570. * Submits register request to kseriod for subsequent execution.
  571. * Note that port registration is always asynchronous.
  572. */
  573. void __serio_register_port(struct serio *serio, struct module *owner)
  574. {
  575. serio_init_port(serio);
  576. serio_queue_event(serio, owner, SERIO_REGISTER_PORT);
  577. }
  578. EXPORT_SYMBOL(__serio_register_port);
  579. /*
  580. * Synchronously unregisters serio port.
  581. */
  582. void serio_unregister_port(struct serio *serio)
  583. {
  584. mutex_lock(&serio_mutex);
  585. serio_disconnect_port(serio);
  586. serio_destroy_port(serio);
  587. mutex_unlock(&serio_mutex);
  588. }
  589. EXPORT_SYMBOL(serio_unregister_port);
  590. /*
  591. * Safely unregisters children ports if they are present.
  592. */
  593. void serio_unregister_child_port(struct serio *serio)
  594. {
  595. struct serio *s, *next;
  596. mutex_lock(&serio_mutex);
  597. list_for_each_entry_safe(s, next, &serio->children, child_node) {
  598. serio_disconnect_port(s);
  599. serio_destroy_port(s);
  600. }
  601. mutex_unlock(&serio_mutex);
  602. }
  603. EXPORT_SYMBOL(serio_unregister_child_port);
  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 = true;
  624. } else if (!strncmp(buf, "auto", count)) {
  625. serio_drv->manual_bind = false;
  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_cleanup(struct serio *serio)
  650. {
  651. mutex_lock(&serio->drv_mutex);
  652. if (serio->drv && serio->drv->cleanup)
  653. serio->drv->cleanup(serio);
  654. mutex_unlock(&serio->drv_mutex);
  655. }
  656. static void serio_shutdown(struct device *dev)
  657. {
  658. struct serio *serio = to_serio_port(dev);
  659. serio_cleanup(serio);
  660. }
  661. static void serio_attach_driver(struct serio_driver *drv)
  662. {
  663. int error;
  664. error = driver_attach(&drv->driver);
  665. if (error)
  666. pr_warning("driver_attach() failed for %s with error %d\n",
  667. drv->driver.name, error);
  668. }
  669. int __serio_register_driver(struct serio_driver *drv, struct module *owner, const char *mod_name)
  670. {
  671. bool manual_bind = drv->manual_bind;
  672. int error;
  673. drv->driver.bus = &serio_bus;
  674. drv->driver.owner = owner;
  675. drv->driver.mod_name = mod_name;
  676. /*
  677. * Temporarily disable automatic binding because probing
  678. * takes long time and we are better off doing it in kseriod
  679. */
  680. drv->manual_bind = true;
  681. error = driver_register(&drv->driver);
  682. if (error) {
  683. pr_err("driver_register() failed for %s, error: %d\n",
  684. drv->driver.name, error);
  685. return error;
  686. }
  687. /*
  688. * Restore original bind mode and let kseriod bind the
  689. * driver to free ports
  690. */
  691. if (!manual_bind) {
  692. drv->manual_bind = false;
  693. error = serio_queue_event(drv, NULL, SERIO_ATTACH_DRIVER);
  694. if (error) {
  695. driver_unregister(&drv->driver);
  696. return error;
  697. }
  698. }
  699. return 0;
  700. }
  701. EXPORT_SYMBOL(__serio_register_driver);
  702. void serio_unregister_driver(struct serio_driver *drv)
  703. {
  704. struct serio *serio;
  705. mutex_lock(&serio_mutex);
  706. drv->manual_bind = true; /* so serio_find_driver ignores it */
  707. serio_remove_pending_events(drv);
  708. start_over:
  709. list_for_each_entry(serio, &serio_list, node) {
  710. if (serio->drv == drv) {
  711. serio_disconnect_port(serio);
  712. serio_find_driver(serio);
  713. /* we could've deleted some ports, restart */
  714. goto start_over;
  715. }
  716. }
  717. driver_unregister(&drv->driver);
  718. mutex_unlock(&serio_mutex);
  719. }
  720. EXPORT_SYMBOL(serio_unregister_driver);
  721. static void serio_set_drv(struct serio *serio, struct serio_driver *drv)
  722. {
  723. serio_pause_rx(serio);
  724. serio->drv = drv;
  725. serio_continue_rx(serio);
  726. }
  727. static int serio_bus_match(struct device *dev, struct device_driver *drv)
  728. {
  729. struct serio *serio = to_serio_port(dev);
  730. struct serio_driver *serio_drv = to_serio_driver(drv);
  731. if (serio->manual_bind || serio_drv->manual_bind)
  732. return 0;
  733. return serio_match_port(serio_drv->id_table, serio);
  734. }
  735. #ifdef CONFIG_HOTPLUG
  736. #define SERIO_ADD_UEVENT_VAR(fmt, val...) \
  737. do { \
  738. int err = add_uevent_var(env, fmt, val); \
  739. if (err) \
  740. return err; \
  741. } while (0)
  742. static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
  743. {
  744. struct serio *serio;
  745. if (!dev)
  746. return -ENODEV;
  747. serio = to_serio_port(dev);
  748. SERIO_ADD_UEVENT_VAR("SERIO_TYPE=%02x", serio->id.type);
  749. SERIO_ADD_UEVENT_VAR("SERIO_PROTO=%02x", serio->id.proto);
  750. SERIO_ADD_UEVENT_VAR("SERIO_ID=%02x", serio->id.id);
  751. SERIO_ADD_UEVENT_VAR("SERIO_EXTRA=%02x", serio->id.extra);
  752. SERIO_ADD_UEVENT_VAR("MODALIAS=serio:ty%02Xpr%02Xid%02Xex%02X",
  753. serio->id.type, serio->id.proto, serio->id.id, serio->id.extra);
  754. return 0;
  755. }
  756. #undef SERIO_ADD_UEVENT_VAR
  757. #else
  758. static int serio_uevent(struct device *dev, struct kobj_uevent_env *env)
  759. {
  760. return -ENODEV;
  761. }
  762. #endif /* CONFIG_HOTPLUG */
  763. #ifdef CONFIG_PM
  764. static int serio_suspend(struct device *dev)
  765. {
  766. struct serio *serio = to_serio_port(dev);
  767. serio_cleanup(serio);
  768. return 0;
  769. }
  770. static int serio_resume(struct device *dev)
  771. {
  772. struct serio *serio = to_serio_port(dev);
  773. /*
  774. * Driver reconnect can take a while, so better let kseriod
  775. * deal with it.
  776. */
  777. serio_queue_event(serio, NULL, SERIO_RECONNECT_PORT);
  778. return 0;
  779. }
  780. static const struct dev_pm_ops serio_pm_ops = {
  781. .suspend = serio_suspend,
  782. .resume = serio_resume,
  783. .poweroff = serio_suspend,
  784. .restore = serio_resume,
  785. };
  786. #endif /* CONFIG_PM */
  787. /* called from serio_driver->connect/disconnect methods under serio_mutex */
  788. int serio_open(struct serio *serio, struct serio_driver *drv)
  789. {
  790. serio_set_drv(serio, drv);
  791. if (serio->open && serio->open(serio)) {
  792. serio_set_drv(serio, NULL);
  793. return -1;
  794. }
  795. return 0;
  796. }
  797. EXPORT_SYMBOL(serio_open);
  798. /* called from serio_driver->connect/disconnect methods under serio_mutex */
  799. void serio_close(struct serio *serio)
  800. {
  801. if (serio->close)
  802. serio->close(serio);
  803. serio_set_drv(serio, NULL);
  804. }
  805. EXPORT_SYMBOL(serio_close);
  806. irqreturn_t serio_interrupt(struct serio *serio,
  807. unsigned char data, unsigned int dfl)
  808. {
  809. unsigned long flags;
  810. irqreturn_t ret = IRQ_NONE;
  811. spin_lock_irqsave(&serio->lock, flags);
  812. if (likely(serio->drv)) {
  813. ret = serio->drv->interrupt(serio, data, dfl);
  814. } else if (!dfl && device_is_registered(&serio->dev)) {
  815. serio_rescan(serio);
  816. ret = IRQ_HANDLED;
  817. }
  818. spin_unlock_irqrestore(&serio->lock, flags);
  819. return ret;
  820. }
  821. EXPORT_SYMBOL(serio_interrupt);
  822. static struct bus_type serio_bus = {
  823. .name = "serio",
  824. .dev_attrs = serio_device_attrs,
  825. .drv_attrs = serio_driver_attrs,
  826. .match = serio_bus_match,
  827. .uevent = serio_uevent,
  828. .probe = serio_driver_probe,
  829. .remove = serio_driver_remove,
  830. .shutdown = serio_shutdown,
  831. #ifdef CONFIG_PM
  832. .pm = &serio_pm_ops,
  833. #endif
  834. };
  835. static int __init serio_init(void)
  836. {
  837. int error;
  838. error = bus_register(&serio_bus);
  839. if (error) {
  840. pr_err("Failed to register serio bus, error: %d\n", error);
  841. return error;
  842. }
  843. return 0;
  844. }
  845. static void __exit serio_exit(void)
  846. {
  847. bus_unregister(&serio_bus);
  848. /*
  849. * There should not be any outstanding events but work may
  850. * still be scheduled so simply cancel it.
  851. */
  852. cancel_work_sync(&serio_event_work);
  853. }
  854. subsys_initcall(serio_init);
  855. module_exit(serio_exit);