2 * Support for host VHCIs inside qemu scatternets.
4 * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
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.
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.
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/>.
20 #include "qemu-common.h"
21 #include "qemu-char.h"
24 #include "main-loop.h"
26 #define VHCI_DEV "/dev/vhci"
27 #define VHCI_UDEV "/dev/hci_vhci"
37 static void vhci_read(void *opaque)
39 struct bt_vhci_s *s = (struct bt_vhci_s *) opaque;
43 /* Seems that we can't read only the header first and then the amount
44 * of data indicated in the header because Linux will discard everything
45 * that's not been read in one go. */
46 s->len = read(s->fd, s->hdr, sizeof(s->hdr));
49 fprintf(stderr, "qemu: error %i reading the PDU\n", errno);
60 pktlen = MIN(pkt[2] + 3, s->len);
61 s->info->cmd_send(s->info, pkt, pktlen);
70 pktlen = MIN(((pkt[3] << 8) | pkt[2]) + 4, s->len);
71 s->info->acl_send(s->info, pkt, pktlen);
80 pktlen = MIN(pkt[2] + 3, s->len);
81 s->info->sco_send(s->info, pkt, pktlen);
88 fprintf(stderr, "qemu: bad HCI packet type %02x\n", pkt[-1]);
92 static void vhci_host_send(void *opaque,
93 int type, const uint8_t *data, int len)
95 struct bt_vhci_s *s = (struct bt_vhci_s *) opaque;
100 iv[0].iov_base = &pkt;
102 iv[1].iov_base = (void *) data;
105 while (writev(s->fd, iv, 2) < 0)
106 if (errno != EAGAIN && errno != EINTR) {
107 fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
112 /* Apparently VHCI wants us to write everything in one chunk :-( */
113 static uint8_t buf[4096];
116 memcpy(buf + 1, data, len);
118 while (write(s->fd, buf, len + 1) < 0)
119 if (errno != EAGAIN && errno != EINTR) {
120 fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
127 static void vhci_out_hci_packet_event(void *opaque,
128 const uint8_t *data, int len)
130 vhci_host_send(opaque, HCI_EVENT_PKT, data, len);
133 static void vhci_out_hci_packet_acl(void *opaque,
134 const uint8_t *data, int len)
136 vhci_host_send(opaque, HCI_ACLDATA_PKT, data, len);
139 void bt_vhci_init(struct HCIInfo *info)
145 fd = open(VHCI_DEV, O_RDWR);
148 fd = open(VHCI_UDEV, O_RDWR);
153 fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n",
154 VHCI_DEV, strerror(err[0]), err[0]);
155 fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n",
156 VHCI_UDEV, strerror(err[1]), err[1]);
160 s = g_malloc0(sizeof(struct bt_vhci_s));
162 s->info = info ?: qemu_next_hci();
164 s->info->evt_recv = vhci_out_hci_packet_event;
165 s->info->acl_recv = vhci_out_hci_packet_acl;
167 qemu_set_fd_handler(s->fd, vhci_read, NULL, s);