serio.c 24 KB

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