serio.c 24 KB

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