serio.c 24 KB

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