root / bt-vhci.c @ 2ada0ed7
History | View | Annotate | Download (4.6 kB)
1 | ab2b6f50 | balrog | /*
|
---|---|---|---|
2 | ab2b6f50 | balrog | * Support for host VHCIs inside qemu scatternets.
|
3 | ab2b6f50 | balrog | *
|
4 | ab2b6f50 | balrog | * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
|
5 | ab2b6f50 | balrog | *
|
6 | ab2b6f50 | balrog | * This program is free software; you can redistribute it and/or
|
7 | ab2b6f50 | balrog | * modify it under the terms of the GNU General Public License as
|
8 | ab2b6f50 | balrog | * published by the Free Software Foundation; either version 2 or
|
9 | ab2b6f50 | balrog | * (at your option) version 3 of the License.
|
10 | ab2b6f50 | balrog | *
|
11 | ab2b6f50 | balrog | * This program is distributed in the hope that it will be useful,
|
12 | ab2b6f50 | balrog | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | ab2b6f50 | balrog | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 | ab2b6f50 | balrog | * GNU General Public License for more details.
|
15 | ab2b6f50 | balrog | *
|
16 | fad6cb1a | aurel32 | * You should have received a copy of the GNU General Public License along
|
17 | fad6cb1a | aurel32 | * with this program; if not, write to the Free Software Foundation, Inc.,
|
18 | fad6cb1a | aurel32 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
19 | ab2b6f50 | balrog | */
|
20 | ab2b6f50 | balrog | |
21 | ab2b6f50 | balrog | #include "qemu-common.h" |
22 | ab2b6f50 | balrog | #include "qemu-char.h" |
23 | ab2b6f50 | balrog | #include "sysemu.h" |
24 | ab2b6f50 | balrog | #include "net.h" |
25 | ab2b6f50 | balrog | #include "hw/bt.h" |
26 | ab2b6f50 | balrog | |
27 | 2e9b08e5 | balrog | #define VHCI_DEV "/dev/vhci" |
28 | ab2b6f50 | balrog | #define VHCI_UDEV "/dev/hci_vhci" |
29 | ab2b6f50 | balrog | |
30 | ab2b6f50 | balrog | struct bt_vhci_s {
|
31 | ab2b6f50 | balrog | int fd;
|
32 | ab2b6f50 | balrog | struct HCIInfo *info;
|
33 | ab2b6f50 | balrog | |
34 | ab2b6f50 | balrog | uint8_t hdr[4096];
|
35 | ab2b6f50 | balrog | int len;
|
36 | ab2b6f50 | balrog | }; |
37 | ab2b6f50 | balrog | |
38 | ab2b6f50 | balrog | static void vhci_read(void *opaque) |
39 | ab2b6f50 | balrog | { |
40 | ab2b6f50 | balrog | struct bt_vhci_s *s = (struct bt_vhci_s *) opaque; |
41 | ab2b6f50 | balrog | uint8_t *pkt; |
42 | ab2b6f50 | balrog | int pktlen;
|
43 | ab2b6f50 | balrog | |
44 | ab2b6f50 | balrog | /* Seems that we can't read only the header first and then the amount
|
45 | ab2b6f50 | balrog | * of data indicated in the header because Linux will discard everything
|
46 | ab2b6f50 | balrog | * that's not been read in one go. */
|
47 | ab2b6f50 | balrog | s->len = read(s->fd, s->hdr, sizeof(s->hdr));
|
48 | ab2b6f50 | balrog | |
49 | ab2b6f50 | balrog | if (s->len < 0) { |
50 | ab2b6f50 | balrog | fprintf(stderr, "qemu: error %i reading the PDU\n", errno);
|
51 | ab2b6f50 | balrog | return;
|
52 | ab2b6f50 | balrog | } |
53 | ab2b6f50 | balrog | |
54 | ab2b6f50 | balrog | pkt = s->hdr; |
55 | ab2b6f50 | balrog | while (s->len --)
|
56 | ab2b6f50 | balrog | switch (*pkt ++) {
|
57 | ab2b6f50 | balrog | case HCI_COMMAND_PKT:
|
58 | ab2b6f50 | balrog | if (s->len < 3) |
59 | ab2b6f50 | balrog | goto bad_pkt;
|
60 | ab2b6f50 | balrog | |
61 | ab2b6f50 | balrog | pktlen = MIN(pkt[2] + 3, s->len); |
62 | ab2b6f50 | balrog | s->info->cmd_send(s->info, pkt, pktlen); |
63 | ab2b6f50 | balrog | s->len -= pktlen; |
64 | ab2b6f50 | balrog | pkt += pktlen; |
65 | ab2b6f50 | balrog | break;
|
66 | ab2b6f50 | balrog | |
67 | ab2b6f50 | balrog | case HCI_ACLDATA_PKT:
|
68 | ab2b6f50 | balrog | if (s->len < 4) |
69 | ab2b6f50 | balrog | goto bad_pkt;
|
70 | ab2b6f50 | balrog | |
71 | ab2b6f50 | balrog | pktlen = MIN(((pkt[3] << 8) | pkt[2]) + 4, s->len); |
72 | ab2b6f50 | balrog | s->info->acl_send(s->info, pkt, pktlen); |
73 | ab2b6f50 | balrog | s->len -= pktlen; |
74 | ab2b6f50 | balrog | pkt += pktlen; |
75 | ab2b6f50 | balrog | break;
|
76 | ab2b6f50 | balrog | |
77 | ab2b6f50 | balrog | case HCI_SCODATA_PKT:
|
78 | ab2b6f50 | balrog | if (s->len < 3) |
79 | ab2b6f50 | balrog | goto bad_pkt;
|
80 | ab2b6f50 | balrog | |
81 | ab2b6f50 | balrog | pktlen = MIN(pkt[2] + 3, s->len); |
82 | ab2b6f50 | balrog | s->info->sco_send(s->info, pkt, pktlen); |
83 | ab2b6f50 | balrog | s->len -= pktlen; |
84 | ab2b6f50 | balrog | pkt += pktlen; |
85 | ab2b6f50 | balrog | break;
|
86 | ab2b6f50 | balrog | |
87 | ab2b6f50 | balrog | default:
|
88 | ab2b6f50 | balrog | bad_pkt:
|
89 | ab2b6f50 | balrog | fprintf(stderr, "qemu: bad HCI packet type %02x\n", pkt[-1]); |
90 | ab2b6f50 | balrog | } |
91 | ab2b6f50 | balrog | } |
92 | ab2b6f50 | balrog | |
93 | ab2b6f50 | balrog | static void vhci_host_send(void *opaque, |
94 | ab2b6f50 | balrog | int type, const uint8_t *data, int len) |
95 | ab2b6f50 | balrog | { |
96 | ab2b6f50 | balrog | struct bt_vhci_s *s = (struct bt_vhci_s *) opaque; |
97 | ab2b6f50 | balrog | #if 0
|
98 | ab2b6f50 | balrog | uint8_t pkt = type;
|
99 | ab2b6f50 | balrog | struct iovec iv[2];
|
100 | ab2b6f50 | balrog | |
101 | ab2b6f50 | balrog | iv[0].iov_base = &pkt;
|
102 | ab2b6f50 | balrog | iv[0].iov_len = 1;
|
103 | ab2b6f50 | balrog | iv[1].iov_base = (void *) data;
|
104 | ab2b6f50 | balrog | iv[1].iov_len = len;
|
105 | ab2b6f50 | balrog | |
106 | ab2b6f50 | balrog | while (writev(s->fd, iv, 2) < 0)
|
107 | ab2b6f50 | balrog | if (errno != EAGAIN && errno != EINTR) {
|
108 | ab2b6f50 | balrog | fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
|
109 | ab2b6f50 | balrog | errno);
|
110 | ab2b6f50 | balrog | return;
|
111 | ab2b6f50 | balrog | }
|
112 | ab2b6f50 | balrog | #else
|
113 | ab2b6f50 | balrog | /* Apparently VHCI wants us to write everything in one chunk :-( */
|
114 | ab2b6f50 | balrog | static uint8_t buf[4096]; |
115 | ab2b6f50 | balrog | |
116 | ab2b6f50 | balrog | buf[0] = type;
|
117 | ab2b6f50 | balrog | memcpy(buf + 1, data, len);
|
118 | ab2b6f50 | balrog | |
119 | ab2b6f50 | balrog | while (write(s->fd, buf, len + 1) < 0) |
120 | ab2b6f50 | balrog | if (errno != EAGAIN && errno != EINTR) {
|
121 | ab2b6f50 | balrog | fprintf(stderr, "qemu: error %i writing bluetooth packet.\n",
|
122 | ab2b6f50 | balrog | errno); |
123 | ab2b6f50 | balrog | return;
|
124 | ab2b6f50 | balrog | } |
125 | ab2b6f50 | balrog | #endif
|
126 | ab2b6f50 | balrog | } |
127 | ab2b6f50 | balrog | |
128 | ab2b6f50 | balrog | static void vhci_out_hci_packet_event(void *opaque, |
129 | ab2b6f50 | balrog | const uint8_t *data, int len) |
130 | ab2b6f50 | balrog | { |
131 | ab2b6f50 | balrog | vhci_host_send(opaque, HCI_EVENT_PKT, data, len); |
132 | ab2b6f50 | balrog | } |
133 | ab2b6f50 | balrog | |
134 | ab2b6f50 | balrog | static void vhci_out_hci_packet_acl(void *opaque, |
135 | ab2b6f50 | balrog | const uint8_t *data, int len) |
136 | ab2b6f50 | balrog | { |
137 | ab2b6f50 | balrog | vhci_host_send(opaque, HCI_ACLDATA_PKT, data, len); |
138 | ab2b6f50 | balrog | } |
139 | ab2b6f50 | balrog | |
140 | ab2b6f50 | balrog | void bt_vhci_init(struct HCIInfo *info) |
141 | ab2b6f50 | balrog | { |
142 | ab2b6f50 | balrog | struct bt_vhci_s *s;
|
143 | ab2b6f50 | balrog | int err[2]; |
144 | ab2b6f50 | balrog | int fd;
|
145 | ab2b6f50 | balrog | |
146 | ab2b6f50 | balrog | fd = open(VHCI_DEV, O_RDWR); |
147 | ab2b6f50 | balrog | err[0] = errno;
|
148 | ab2b6f50 | balrog | if (fd < 0) { |
149 | ab2b6f50 | balrog | fd = open(VHCI_UDEV, O_RDWR); |
150 | ab2b6f50 | balrog | err[1] = errno;
|
151 | ab2b6f50 | balrog | } |
152 | ab2b6f50 | balrog | |
153 | ab2b6f50 | balrog | if (fd < 0) { |
154 | ab2b6f50 | balrog | fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n",
|
155 | ab2b6f50 | balrog | VHCI_DEV, strerror(err[0]), err[0]); |
156 | ab2b6f50 | balrog | fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n",
|
157 | ab2b6f50 | balrog | VHCI_UDEV, strerror(err[1]), err[1]); |
158 | ab2b6f50 | balrog | exit(-1);
|
159 | ab2b6f50 | balrog | } |
160 | ab2b6f50 | balrog | |
161 | ab2b6f50 | balrog | s = qemu_mallocz(sizeof(struct bt_vhci_s)); |
162 | ab2b6f50 | balrog | s->fd = fd; |
163 | ab2b6f50 | balrog | s->info = info ?: qemu_next_hci(); |
164 | ab2b6f50 | balrog | s->info->opaque = s; |
165 | ab2b6f50 | balrog | s->info->evt_recv = vhci_out_hci_packet_event; |
166 | ab2b6f50 | balrog | s->info->acl_recv = vhci_out_hci_packet_acl; |
167 | ab2b6f50 | balrog | |
168 | 511d2b14 | blueswir1 | qemu_set_fd_handler(s->fd, vhci_read, NULL, s);
|
169 | ab2b6f50 | balrog | } |