serio.c 24 KB

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