]> git.proxmox.com Git - qemu.git/blame - bt-vhci.c
monitor: Drop banner hiding (Jan Kiszka)
[qemu.git] / bt-vhci.c
CommitLineData
ab2b6f50
AZ
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 *
fad6cb1a
AJ
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
ab2b6f50
AZ
19 */
20
21#include "qemu-common.h"
22#include "qemu-char.h"
23#include "sysemu.h"
24#include "net.h"
25#include "hw/bt.h"
26
2e9b08e5 27#define VHCI_DEV "/dev/vhci"
ab2b6f50
AZ
28#define VHCI_UDEV "/dev/hci_vhci"
29
30struct bt_vhci_s {
31 int fd;
32 struct HCIInfo *info;
33
34 uint8_t hdr[4096];
35 int len;
36};
37
38static void vhci_read(void *opaque)
39{
40 struct bt_vhci_s *s = (struct bt_vhci_s *) opaque;
41 uint8_t *pkt;
42 int pktlen;
43
44 /* Seems that we can't read only the header first and then the amount
45 * of data indicated in the header because Linux will discard everything
46 * that's not been read in one go. */
47 s->len = read(s->fd, s->hdr, sizeof(s->hdr));
48
49 if (s->len < 0) {
50 fprintf(stderr, "qemu: error %i reading the PDU\n", errno);
51 return;
52 }
53
54 pkt = s->hdr;
55 while (s->len --)
56 switch (*pkt ++) {
57 case HCI_COMMAND_PKT:
58 if (s->len < 3)
59 goto bad_pkt;
60
61 pktlen = MIN(pkt[2] + 3, s->len);
62 s->info->cmd_send(s->info, pkt, pktlen);
63 s->len -= pktlen;
64 pkt += pktlen;
65 break;
66
67 case HCI_ACLDATA_PKT:
68 if (s->len < 4)
69 goto bad_pkt;
70
71 pktlen = MIN(((pkt[3] << 8) | pkt[2]) + 4, s->len);
72 s->info->acl_send(s->info, pkt, pktlen);
73 s->len -= pktlen;
74 pkt += pktlen;
75 break;
76
77 case HCI_SCODATA_PKT:
78 if (s->len < 3)
79 goto bad_pkt;
80
81 pktlen = MIN(pkt[2] + 3, s->len);
82 s->info->sco_send(s->info, pkt, pktlen);
83 s->len -= pktlen;
84 pkt += pktlen;
85 break;
86
87 default:
88 bad_pkt:
89 fprintf(stderr, "qemu: bad HCI packet type %02x\n", pkt[-1]);
90 }
91}
92
93static void vhci_host_send(void *opaque,
94 int type, const uint8_t *data, int len)
95{
96 struct bt_vhci_s *s = (struct bt_vhci_s *) opaque;
97#if 0
98 uint8_t pkt = type;
99 struct iovec iv[2];
100
101 iv[0].iov_base = &pkt;
102 iv[0].iov_len = 1;
103 iv[1].iov_base = (void *) data;
104 iv[1].iov_len = len;
105
106 while (writev(s->fd, iv, 2) < 0)
107 if (errno != EAGAIN && errno != EINTR) {
108 fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
109 errno);
110 return;
111 }
112#else
113 /* Apparently VHCI wants us to write everything in one chunk :-( */
114 static uint8_t buf[4096];
115
116 buf[0] = type;
117 memcpy(buf + 1, data, len);
118
119 while (write(s->fd, buf, len + 1) < 0)
120 if (errno != EAGAIN && errno != EINTR) {
121 fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
122 errno);
123 return;
124 }
125#endif
126}
127
128static void vhci_out_hci_packet_event(void *opaque,
129 const uint8_t *data, int len)
130{
131 vhci_host_send(opaque, HCI_EVENT_PKT, data, len);
132}
133
134static void vhci_out_hci_packet_acl(void *opaque,
135 const uint8_t *data, int len)
136{
137 vhci_host_send(opaque, HCI_ACLDATA_PKT, data, len);
138}
139
140void bt_vhci_init(struct HCIInfo *info)
141{
142 struct bt_vhci_s *s;
143 int err[2];
144 int fd;
145
146 fd = open(VHCI_DEV, O_RDWR);
147 err[0] = errno;
148 if (fd < 0) {
149 fd = open(VHCI_UDEV, O_RDWR);
150 err[1] = errno;
151 }
152
153 if (fd < 0) {
154 fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n",
155 VHCI_DEV, strerror(err[0]), err[0]);
156 fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n",
157 VHCI_UDEV, strerror(err[1]), err[1]);
158 exit(-1);
159 }
160
161 s = qemu_mallocz(sizeof(struct bt_vhci_s));
162 s->fd = fd;
163 s->info = info ?: qemu_next_hci();
164 s->info->opaque = s;
165 s->info->evt_recv = vhci_out_hci_packet_event;
166 s->info->acl_recv = vhci_out_hci_packet_acl;
167
168 qemu_set_fd_handler(s->fd, vhci_read, 0, s);
169}