hvc_lguest.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Simple console for lguest.
  2. *
  3. * Copyright (C) 2006 Rusty Russell, IBM Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <linux/err.h>
  20. #include <linux/init.h>
  21. #include <linux/lguest_bus.h>
  22. #include "hvc_console.h"
  23. static char inbuf[256];
  24. static struct lguest_dma cons_input = { .used_len = 0,
  25. .addr[0] = __pa(inbuf),
  26. .len[0] = sizeof(inbuf),
  27. .len[1] = 0 };
  28. static int put_chars(u32 vtermno, const char *buf, int count)
  29. {
  30. struct lguest_dma dma;
  31. /* FIXME: what if it's over a page boundary? */
  32. dma.len[0] = count;
  33. dma.len[1] = 0;
  34. dma.addr[0] = __pa(buf);
  35. lguest_send_dma(LGUEST_CONSOLE_DMA_KEY, &dma);
  36. return count;
  37. }
  38. static int get_chars(u32 vtermno, char *buf, int count)
  39. {
  40. static int cons_offset;
  41. if (!cons_input.used_len)
  42. return 0;
  43. if (cons_input.used_len - cons_offset < count)
  44. count = cons_input.used_len - cons_offset;
  45. memcpy(buf, inbuf + cons_offset, count);
  46. cons_offset += count;
  47. if (cons_offset == cons_input.used_len) {
  48. cons_offset = 0;
  49. cons_input.used_len = 0;
  50. }
  51. return count;
  52. }
  53. static struct hv_ops lguest_cons = {
  54. .get_chars = get_chars,
  55. .put_chars = put_chars,
  56. };
  57. static int __init cons_init(void)
  58. {
  59. if (strcmp(paravirt_ops.name, "lguest") != 0)
  60. return 0;
  61. return hvc_instantiate(0, 0, &lguest_cons);
  62. }
  63. console_initcall(cons_init);
  64. static int lguestcons_probe(struct lguest_device *lgdev)
  65. {
  66. int err;
  67. lgdev->private = hvc_alloc(0, lgdev_irq(lgdev), &lguest_cons, 256);
  68. if (IS_ERR(lgdev->private))
  69. return PTR_ERR(lgdev->private);
  70. err = lguest_bind_dma(LGUEST_CONSOLE_DMA_KEY, &cons_input, 1,
  71. lgdev_irq(lgdev));
  72. if (err)
  73. printk("lguest console: failed to bind buffer.\n");
  74. return err;
  75. }
  76. static struct lguest_driver lguestcons_drv = {
  77. .name = "lguestcons",
  78. .owner = THIS_MODULE,
  79. .device_type = LGUEST_DEVICE_T_CONSOLE,
  80. .probe = lguestcons_probe,
  81. };
  82. static int __init hvc_lguest_init(void)
  83. {
  84. return register_lguest_driver(&lguestcons_drv);
  85. }
  86. module_init(hvc_lguest_init);