1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu-common.h"
21#include "qemu-char.h"
22#include "sysemu.h"
23#include "net.h"
24#include "hw/bt.h"
25
26#define VHCI_DEV "/dev/vhci"
27#define VHCI_UDEV "/dev/hci_vhci"
28
29struct bt_vhci_s {
30 int fd;
31 struct HCIInfo *info;
32
33 uint8_t hdr[4096];
34 int len;
35};
36
37static void vhci_read(void *opaque)
38{
39 struct bt_vhci_s *s = (struct bt_vhci_s *) opaque;
40 uint8_t *pkt;
41 int pktlen;
42
43
44
45
46 s->len = read(s->fd, s->hdr, sizeof(s->hdr));
47
48 if (s->len < 0) {
49 fprintf(stderr, "qemu: error %i reading the PDU\n", errno);
50 return;
51 }
52
53 pkt = s->hdr;
54 while (s->len --)
55 switch (*pkt ++) {
56 case HCI_COMMAND_PKT:
57 if (s->len < 3)
58 goto bad_pkt;
59
60 pktlen = MIN(pkt[2] + 3, s->len);
61 s->info->cmd_send(s->info, pkt, pktlen);
62 s->len -= pktlen;
63 pkt += pktlen;
64 break;
65
66 case HCI_ACLDATA_PKT:
67 if (s->len < 4)
68 goto bad_pkt;
69
70 pktlen = MIN(((pkt[3] << 8) | pkt[2]) + 4, s->len);
71 s->info->acl_send(s->info, pkt, pktlen);
72 s->len -= pktlen;
73 pkt += pktlen;
74 break;
75
76 case HCI_SCODATA_PKT:
77 if (s->len < 3)
78 goto bad_pkt;
79
80 pktlen = MIN(pkt[2] + 3, s->len);
81 s->info->sco_send(s->info, pkt, pktlen);
82 s->len -= pktlen;
83 pkt += pktlen;
84 break;
85
86 default:
87 bad_pkt:
88 fprintf(stderr, "qemu: bad HCI packet type %02x\n", pkt[-1]);
89 }
90}
91
92static void vhci_host_send(void *opaque,
93 int type, const uint8_t *data, int len)
94{
95 struct bt_vhci_s *s = (struct bt_vhci_s *) opaque;
96#if 0
97 uint8_t pkt = type;
98 struct iovec iv[2];
99
100 iv[0].iov_base = &pkt;
101 iv[0].iov_len = 1;
102 iv[1].iov_base = (void *) data;
103 iv[1].iov_len = len;
104
105 while (writev(s->fd, iv, 2) < 0)
106 if (errno != EAGAIN && errno != EINTR) {
107 fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
108 errno);
109 return;
110 }
111#else
112
113 static uint8_t buf[4096];
114
115 buf[0] = type;
116 memcpy(buf + 1, data, len);
117
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",
121 errno);
122 return;
123 }
124#endif
125}
126
127static void vhci_out_hci_packet_event(void *opaque,
128 const uint8_t *data, int len)
129{
130 vhci_host_send(opaque, HCI_EVENT_PKT, data, len);
131}
132
133static void vhci_out_hci_packet_acl(void *opaque,
134 const uint8_t *data, int len)
135{
136 vhci_host_send(opaque, HCI_ACLDATA_PKT, data, len);
137}
138
139void bt_vhci_init(struct HCIInfo *info)
140{
141 struct bt_vhci_s *s;
142 int err[2];
143 int fd;
144
145 fd = open(VHCI_DEV, O_RDWR);
146 err[0] = errno;
147 if (fd < 0) {
148 fd = open(VHCI_UDEV, O_RDWR);
149 err[1] = errno;
150 }
151
152 if (fd < 0) {
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]);
157 exit(-1);
158 }
159
160 s = qemu_mallocz(sizeof(struct bt_vhci_s));
161 s->fd = fd;
162 s->info = info ?: qemu_next_hci();
163 s->info->opaque = s;
164 s->info->evt_recv = vhci_out_hci_packet_event;
165 s->info->acl_recv = vhci_out_hci_packet_acl;
166
167 qemu_set_fd_handler(s->fd, vhci_read, NULL, s);
168}
169