bt-vhci.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Support for host VHCIs inside qemu scatternets.
  3. *
  4. * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 or
  9. * (at your option) version 3 of the License.
  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. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu-common.h"
  21. #include "sysemu/bt.h"
  22. #include "hw/bt.h"
  23. #include "qemu/main-loop.h"
  24. #define VHCI_DEV "/dev/vhci"
  25. #define VHCI_UDEV "/dev/hci_vhci"
  26. struct bt_vhci_s {
  27. int fd;
  28. struct HCIInfo *info;
  29. uint8_t hdr[4096];
  30. int len;
  31. };
  32. static void vhci_read(void *opaque)
  33. {
  34. struct bt_vhci_s *s = (struct bt_vhci_s *) opaque;
  35. uint8_t *pkt;
  36. int pktlen;
  37. /* Seems that we can't read only the header first and then the amount
  38. * of data indicated in the header because Linux will discard everything
  39. * that's not been read in one go. */
  40. s->len = read(s->fd, s->hdr, sizeof(s->hdr));
  41. if (s->len < 0) {
  42. fprintf(stderr, "qemu: error %i reading the PDU\n", errno);
  43. return;
  44. }
  45. pkt = s->hdr;
  46. while (s->len --)
  47. switch (*pkt ++) {
  48. case HCI_COMMAND_PKT:
  49. if (s->len < 3)
  50. goto bad_pkt;
  51. pktlen = MIN(pkt[2] + 3, s->len);
  52. s->info->cmd_send(s->info, pkt, pktlen);
  53. s->len -= pktlen;
  54. pkt += pktlen;
  55. break;
  56. case HCI_ACLDATA_PKT:
  57. if (s->len < 4)
  58. goto bad_pkt;
  59. pktlen = MIN(((pkt[3] << 8) | pkt[2]) + 4, s->len);
  60. s->info->acl_send(s->info, pkt, pktlen);
  61. s->len -= pktlen;
  62. pkt += pktlen;
  63. break;
  64. case HCI_SCODATA_PKT:
  65. if (s->len < 3)
  66. goto bad_pkt;
  67. pktlen = MIN(pkt[2] + 3, s->len);
  68. s->info->sco_send(s->info, pkt, pktlen);
  69. s->len -= pktlen;
  70. pkt += pktlen;
  71. break;
  72. default:
  73. bad_pkt:
  74. fprintf(stderr, "qemu: bad HCI packet type %02x\n", pkt[-1]);
  75. }
  76. }
  77. static void vhci_host_send(void *opaque,
  78. int type, const uint8_t *data, int len)
  79. {
  80. struct bt_vhci_s *s = (struct bt_vhci_s *) opaque;
  81. #if 0
  82. uint8_t pkt = type;
  83. struct iovec iv[2];
  84. iv[0].iov_base = &pkt;
  85. iv[0].iov_len = 1;
  86. iv[1].iov_base = (void *) data;
  87. iv[1].iov_len = len;
  88. while (writev(s->fd, iv, 2) < 0)
  89. if (errno != EAGAIN && errno != EINTR) {
  90. fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
  91. errno);
  92. return;
  93. }
  94. #else
  95. /* Apparently VHCI wants us to write everything in one chunk :-( */
  96. static uint8_t buf[4096];
  97. buf[0] = type;
  98. memcpy(buf + 1, data, len);
  99. while (write(s->fd, buf, len + 1) < 0)
  100. if (errno != EAGAIN && errno != EINTR) {
  101. fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
  102. errno);
  103. return;
  104. }
  105. #endif
  106. }
  107. static void vhci_out_hci_packet_event(void *opaque,
  108. const uint8_t *data, int len)
  109. {
  110. vhci_host_send(opaque, HCI_EVENT_PKT, data, len);
  111. }
  112. static void vhci_out_hci_packet_acl(void *opaque,
  113. const uint8_t *data, int len)
  114. {
  115. vhci_host_send(opaque, HCI_ACLDATA_PKT, data, len);
  116. }
  117. void bt_vhci_init(struct HCIInfo *info)
  118. {
  119. struct bt_vhci_s *s;
  120. int err[2];
  121. int fd;
  122. fd = open(VHCI_DEV, O_RDWR);
  123. err[0] = errno;
  124. if (fd < 0) {
  125. fd = open(VHCI_UDEV, O_RDWR);
  126. err[1] = errno;
  127. }
  128. if (fd < 0) {
  129. fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n",
  130. VHCI_DEV, strerror(err[0]), err[0]);
  131. fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n",
  132. VHCI_UDEV, strerror(err[1]), err[1]);
  133. exit(-1);
  134. }
  135. s = g_malloc0(sizeof(struct bt_vhci_s));
  136. s->fd = fd;
  137. s->info = info ?: qemu_next_hci();
  138. s->info->opaque = s;
  139. s->info->evt_recv = vhci_out_hci_packet_event;
  140. s->info->acl_recv = vhci_out_hci_packet_acl;
  141. qemu_set_fd_handler(s->fd, vhci_read, NULL, s);
  142. }