You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bt-vhci.c 4.5KB

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