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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 #include "qemu-common.h"
23 #include "qemu-char.h"
30 #define VHCI_DEV "/dev/vhci"
31 #define VHCI_UDEV "/dev/hci_vhci"
41 static void vhci_read(void *opaque
)
43 struct bt_vhci_s
*s
= (struct bt_vhci_s
*) opaque
;
47 /* Seems that we can't read only the header first and then the amount
48 * of data indicated in the header because Linux will discard everything
49 * that's not been read in one go. */
50 s
->len
= read(s
->fd
, s
->hdr
, sizeof(s
->hdr
));
53 fprintf(stderr
, "qemu: error %i reading the PDU\n", errno
);
64 pktlen
= MIN(pkt
[2] + 3, s
->len
);
65 s
->info
->cmd_send(s
->info
, pkt
, pktlen
);
74 pktlen
= MIN(((pkt
[3] << 8) | pkt
[2]) + 4, s
->len
);
75 s
->info
->acl_send(s
->info
, pkt
, pktlen
);
84 pktlen
= MIN(pkt
[2] + 3, s
->len
);
85 s
->info
->sco_send(s
->info
, pkt
, pktlen
);
92 fprintf(stderr
, "qemu: bad HCI packet type %02x\n", pkt
[-1]);
96 static void vhci_host_send(void *opaque
,
97 int type
, const uint8_t *data
, int len
)
99 struct bt_vhci_s
*s
= (struct bt_vhci_s
*) opaque
;
104 iv
[0].iov_base
= &pkt
;
106 iv
[1].iov_base
= (void *) data
;
109 while (writev(s
->fd
, iv
, 2) < 0)
110 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
111 fprintf(stderr
, "qemu: error %i writing bluetooth packet.\n",
116 /* Apparently VHCI wants us to write everything in one chunk :-( */
117 static uint8_t buf
[4096];
120 memcpy(buf
+ 1, data
, len
);
122 while (write(s
->fd
, buf
, len
+ 1) < 0)
123 if (errno
!= EAGAIN
&& errno
!= EINTR
) {
124 fprintf(stderr
, "qemu: error %i writing bluetooth packet.\n",
131 static void vhci_out_hci_packet_event(void *opaque
,
132 const uint8_t *data
, int len
)
134 vhci_host_send(opaque
, HCI_EVENT_PKT
, data
, len
);
137 static void vhci_out_hci_packet_acl(void *opaque
,
138 const uint8_t *data
, int len
)
140 vhci_host_send(opaque
, HCI_ACLDATA_PKT
, data
, len
);
143 void bt_vhci_init(struct HCIInfo
*info
)
149 fd
= open(VHCI_DEV
, O_RDWR
);
152 fd
= open(VHCI_UDEV
, O_RDWR
);
157 fprintf(stderr
, "qemu: Can't open `%s': %s (%i)\n",
158 VHCI_DEV
, strerror(err
[0]), err
[0]);
159 fprintf(stderr
, "qemu: Can't open `%s': %s (%i)\n",
160 VHCI_UDEV
, strerror(err
[1]), err
[1]);
164 s
= qemu_mallocz(sizeof(struct bt_vhci_s
));
166 s
->info
= info
?: qemu_next_hci();
168 s
->info
->evt_recv
= vhci_out_hci_packet_event
;
169 s
->info
->acl_recv
= vhci_out_hci_packet_acl
;
171 qemu_set_fd_handler(s
->fd
, vhci_read
, 0, s
);