serio.c 23 KB

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