hvc_vio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "hvc_console.h"
  37. char hvc_driver_name[] = "hvc_console";
  38. static struct vio_device_id hvc_driver_table[] __devinitdata = {
  39. {"serial", "hvterm1"},
  40. { "", "" }
  41. };
  42. MODULE_DEVICE_TABLE(vio, hvc_driver_table);
  43. static int filtered_get_chars(uint32_t vtermno, char *buf, int count)
  44. {
  45. unsigned long got;
  46. int i;
  47. /*
  48. * Vio firmware will read up to SIZE_VIO_GET_CHARS at its own discretion
  49. * so we play safe and avoid the situation where got > count which could
  50. * overload the flip buffer.
  51. */
  52. if (count < SIZE_VIO_GET_CHARS)
  53. return -EAGAIN;
  54. got = hvc_get_chars(vtermno, buf, count);
  55. /*
  56. * Work around a HV bug where it gives us a null
  57. * after every \r. -- paulus
  58. */
  59. for (i = 1; i < got; ++i) {
  60. if (buf[i] == 0 && buf[i-1] == '\r') {
  61. --got;
  62. if (i < got)
  63. memmove(&buf[i], &buf[i+1],
  64. got - i);
  65. }
  66. }
  67. return got;
  68. }
  69. static struct hv_ops hvc_get_put_ops = {
  70. .get_chars = filtered_get_chars,
  71. .put_chars = hvc_put_chars,
  72. };
  73. static int __devinit hvc_vio_probe(struct vio_dev *vdev,
  74. const struct vio_device_id *id)
  75. {
  76. struct hvc_struct *hp;
  77. /* probed with invalid parameters. */
  78. if (!vdev || !id)
  79. return -EPERM;
  80. hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops);
  81. if (IS_ERR(hp))
  82. return PTR_ERR(hp);
  83. dev_set_drvdata(&vdev->dev, hp);
  84. return 0;
  85. }
  86. static int __devexit hvc_vio_remove(struct vio_dev *vdev)
  87. {
  88. struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
  89. return hvc_remove(hp);
  90. }
  91. static struct vio_driver hvc_vio_driver = {
  92. .id_table = hvc_driver_table,
  93. .probe = hvc_vio_probe,
  94. .remove = hvc_vio_remove,
  95. .driver = {
  96. .name = hvc_driver_name,
  97. .owner = THIS_MODULE,
  98. }
  99. };
  100. static int hvc_vio_init(void)
  101. {
  102. int rc;
  103. /* Register as a vio device to receive callbacks */
  104. rc = vio_register_driver(&hvc_vio_driver);
  105. return rc;
  106. }
  107. module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
  108. static void hvc_vio_exit(void)
  109. {
  110. vio_unregister_driver(&hvc_vio_driver);
  111. }
  112. module_exit(hvc_vio_exit);
  113. /* the device tree order defines our numbering */
  114. static int hvc_find_vtys(void)
  115. {
  116. struct device_node *vty;
  117. int num_found = 0;
  118. for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
  119. vty = of_find_node_by_name(vty, "vty")) {
  120. uint32_t *vtermno;
  121. /* We have statically defined space for only a certain number
  122. * of console adapters.
  123. */
  124. if (num_found >= MAX_NR_HVC_CONSOLES)
  125. break;
  126. vtermno = (uint32_t *)get_property(vty, "reg", NULL);
  127. if (!vtermno)
  128. continue;
  129. if (device_is_compatible(vty, "hvterm1")) {
  130. hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
  131. ++num_found;
  132. }
  133. }
  134. return num_found;
  135. }
  136. console_initcall(hvc_find_vtys);