serio.c 24 KB

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