rocker_world.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * QEMU rocker switch emulation - switch worlds
  3. *
  4. * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "qemu/iov.h"
  18. #include "rocker.h"
  19. #include "rocker_world.h"
  20. struct world {
  21. Rocker *r;
  22. enum rocker_world_type type;
  23. WorldOps *ops;
  24. };
  25. ssize_t world_ingress(World *world, uint32_t pport,
  26. const struct iovec *iov, int iovcnt)
  27. {
  28. if (world->ops->ig) {
  29. return world->ops->ig(world, pport, iov, iovcnt);
  30. }
  31. return -1;
  32. }
  33. int world_do_cmd(World *world, DescInfo *info,
  34. char *buf, uint16_t cmd, RockerTlv *cmd_info_tlv)
  35. {
  36. if (world->ops->cmd) {
  37. return world->ops->cmd(world, info, buf, cmd, cmd_info_tlv);
  38. }
  39. return -ROCKER_ENOTSUP;
  40. }
  41. World *world_alloc(Rocker *r, size_t sizeof_private,
  42. enum rocker_world_type type, WorldOps *ops)
  43. {
  44. World *w = g_malloc0(sizeof(World) + sizeof_private);
  45. w->r = r;
  46. w->type = type;
  47. w->ops = ops;
  48. if (w->ops->init) {
  49. w->ops->init(w);
  50. }
  51. return w;
  52. }
  53. void world_free(World *world)
  54. {
  55. if (world->ops->uninit) {
  56. world->ops->uninit(world);
  57. }
  58. g_free(world);
  59. }
  60. void world_reset(World *world)
  61. {
  62. if (world->ops->uninit) {
  63. world->ops->uninit(world);
  64. }
  65. if (world->ops->init) {
  66. world->ops->init(world);
  67. }
  68. }
  69. void *world_private(World *world)
  70. {
  71. return world + 1;
  72. }
  73. Rocker *world_rocker(World *world)
  74. {
  75. return world->r;
  76. }
  77. enum rocker_world_type world_type(World *world)
  78. {
  79. return world->type;
  80. }
  81. const char *world_name(World *world)
  82. {
  83. return world->ops->name;
  84. }