hvc_vio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * vio driver interface to hvc_console.c
  3. *
  4. * This code was moved here to allow the remaing code to be reused as a
  5. * generic polling mode with semi-reliable transport driver core to the
  6. * console and tty subsystems.
  7. *
  8. *
  9. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  10. * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
  11. * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  12. * Copyright (C) 2004 IBM Corporation
  13. *
  14. * Additional Author(s):
  15. * Ryan S. Arnold <rsa@us.ibm.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. */
  31. #include <linux/types.h>
  32. #include <linux/init.h>
  33. #include <asm/hvconsole.h>
  34. #include <asm/vio.h>
  35. #include <asm/prom.h>
  36. #include <asm/firmware.h>
  37. #include "hvc_console.h"
  38. char hvc_driver_name[] = "hvc_console";
  39. static struct vio_device_id hvc_driver_table[] __devinitdata = {
  40. {"serial", "hvterm1"},
  41. { "", "" }
  42. };
  43. MODULE_DEVICE_TABLE(vio, hvc_driver_table);
  44. static int filtered_get_chars(uint32_t vtermno, char *buf, int count)
  45. {
  46. unsigned long got;
  47. int i;
  48. /*
  49. * Vio firmware will read up to SIZE_VIO_GET_CHARS at its own discretion
  50. * so we play safe and avoid the situation where got > count which could
  51. * overload the flip buffer.
  52. */
  53. if (count < SIZE_VIO_GET_CHARS)
  54. return -EAGAIN;
  55. got = hvc_get_chars(vtermno, buf, count);
  56. /*
  57. * Work around a HV bug where it gives us a null
  58. * after every \r. -- paulus
  59. */
  60. for (i = 1; i < got; ++i) {
  61. if (buf[i] == 0 && buf[i-1] == '\r') {
  62. --got;
  63. if (i < got)
  64. memmove(&buf[i], &buf[i+1],
  65. got - i);
  66. }
  67. }
  68. return got;
  69. }
  70. static struct hv_ops hvc_get_put_ops = {
  71. .get_chars = filtered_get_chars,
  72. .put_chars = hvc_put_chars,
  73. .notifier_add = notifier_add_irq,
  74. .notifier_del = notifier_del_irq,
  75. .notifier_hangup = notifier_hangup_irq,
  76. };
  77. static int __devinit hvc_vio_probe(struct vio_dev *vdev,
  78. const struct vio_device_id *id)
  79. {
  80. struct hvc_struct *hp;
  81. /* probed with invalid parameters. */
  82. if (!vdev || !id)
  83. return -EPERM;
  84. hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
  85. MAX_VIO_PUT_CHARS);
  86. if (IS_ERR(hp))
  87. return PTR_ERR(hp);
  88. dev_set_drvdata(&vdev->dev, hp);
  89. return 0;
  90. }
  91. static int __devexit hvc_vio_remove(struct vio_dev *vdev)
  92. {
  93. struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
  94. return hvc_remove(hp);
  95. }
  96. static struct vio_driver hvc_vio_driver = {
  97. .id_table = hvc_driver_table,
  98. .probe = hvc_vio_probe,
  99. .remove = hvc_vio_remove,
  100. .driver = {
  101. .name = hvc_driver_name,
  102. .owner = THIS_MODULE,
  103. }
  104. };
  105. static int hvc_vio_init(void)
  106. {
  107. int rc;
  108. if (firmware_has_feature(FW_FEATURE_ISERIES))
  109. return -EIO;
  110. /* Register as a vio device to receive callbacks */
  111. rc = vio_register_driver(&hvc_vio_driver);
  112. return rc;
  113. }
  114. module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
  115. static void hvc_vio_exit(void)
  116. {
  117. vio_unregister_driver(&hvc_vio_driver);
  118. }
  119. module_exit(hvc_vio_exit);
  120. /* the device tree order defines our numbering */
  121. static int hvc_find_vtys(void)
  122. {
  123. struct device_node *vty;
  124. int num_found = 0;
  125. for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
  126. vty = of_find_node_by_name(vty, "vty")) {
  127. const uint32_t *vtermno;
  128. /* We have statically defined space for only a certain number
  129. * of console adapters.
  130. */
  131. if (num_found >= MAX_NR_HVC_CONSOLES) {
  132. of_node_put(vty);
  133. break;
  134. }
  135. vtermno = of_get_property(vty, "reg", NULL);
  136. if (!vtermno)
  137. continue;
  138. if (of_device_is_compatible(vty, "hvterm1")) {
  139. hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
  140. ++num_found;
  141. }
  142. }
  143. return num_found;
  144. }
  145. console_initcall(hvc_find_vtys);