hvc_vio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. };
  74. static int __devinit hvc_vio_probe(struct vio_dev *vdev,
  75. const struct vio_device_id *id)
  76. {
  77. struct hvc_struct *hp;
  78. /* probed with invalid parameters. */
  79. if (!vdev || !id)
  80. return -EPERM;
  81. hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
  82. MAX_VIO_PUT_CHARS);
  83. if (IS_ERR(hp))
  84. return PTR_ERR(hp);
  85. dev_set_drvdata(&vdev->dev, hp);
  86. return 0;
  87. }
  88. static int __devexit hvc_vio_remove(struct vio_dev *vdev)
  89. {
  90. struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
  91. return hvc_remove(hp);
  92. }
  93. static struct vio_driver hvc_vio_driver = {
  94. .id_table = hvc_driver_table,
  95. .probe = hvc_vio_probe,
  96. .remove = hvc_vio_remove,
  97. .driver = {
  98. .name = hvc_driver_name,
  99. .owner = THIS_MODULE,
  100. }
  101. };
  102. static int hvc_vio_init(void)
  103. {
  104. int rc;
  105. if (firmware_has_feature(FW_FEATURE_ISERIES))
  106. return -EIO;
  107. /* Register as a vio device to receive callbacks */
  108. rc = vio_register_driver(&hvc_vio_driver);
  109. return rc;
  110. }
  111. module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
  112. static void hvc_vio_exit(void)
  113. {
  114. vio_unregister_driver(&hvc_vio_driver);
  115. }
  116. module_exit(hvc_vio_exit);
  117. /* the device tree order defines our numbering */
  118. static int hvc_find_vtys(void)
  119. {
  120. struct device_node *vty;
  121. int num_found = 0;
  122. for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
  123. vty = of_find_node_by_name(vty, "vty")) {
  124. const uint32_t *vtermno;
  125. /* We have statically defined space for only a certain number
  126. * of console adapters.
  127. */
  128. if (num_found >= MAX_NR_HVC_CONSOLES)
  129. break;
  130. vtermno = get_property(vty, "reg", NULL);
  131. if (!vtermno)
  132. continue;
  133. if (device_is_compatible(vty, "hvterm1")) {
  134. hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
  135. ++num_found;
  136. }
  137. }
  138. return num_found;
  139. }
  140. console_initcall(hvc_find_vtys);