serio.c 24 KB

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