balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Wrap a host Bluetooth HCI socket in a struct HCIInfo. |
| 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 | * |
aurel32 | fad6cb1 | 2009-01-04 22:05:52 +0000 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 20 | #include "qemu/osdep.h" |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 21 | #include "qemu-common.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 22 | #include "sysemu/bt.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 23 | #include "qemu/main-loop.h" |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 24 | |
balrog | 2e9b08e | 2008-09-30 23:31:35 +0000 | [diff] [blame] | 25 | #ifndef _WIN32 |
balrog | 2e9b08e | 2008-09-30 23:31:35 +0000 | [diff] [blame] | 26 | # include <sys/ioctl.h> |
| 27 | # include <sys/uio.h> |
| 28 | # ifdef CONFIG_BLUEZ |
| 29 | # include <bluetooth/bluetooth.h> |
| 30 | # include <bluetooth/hci.h> |
| 31 | # include <bluetooth/hci_lib.h> |
| 32 | # else |
| 33 | # include "hw/bt.h" |
| 34 | # define HCI_MAX_FRAME_SIZE 1028 |
| 35 | # endif |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 36 | |
| 37 | struct bt_host_hci_s { |
| 38 | struct HCIInfo hci; |
| 39 | int fd; |
| 40 | |
| 41 | uint8_t hdr[HCI_MAX_FRAME_SIZE]; |
| 42 | int len; |
| 43 | }; |
| 44 | |
| 45 | static void bt_host_send(struct HCIInfo *hci, |
| 46 | int type, const uint8_t *data, int len) |
| 47 | { |
| 48 | struct bt_host_hci_s *s = (struct bt_host_hci_s *) hci; |
| 49 | uint8_t pkt = type; |
| 50 | struct iovec iv[2]; |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 51 | |
blueswir1 | 3f4cb3d | 2009-04-13 16:31:01 +0000 | [diff] [blame] | 52 | iv[0].iov_base = (void *)&pkt; |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 53 | iv[0].iov_len = 1; |
| 54 | iv[1].iov_base = (void *) data; |
| 55 | iv[1].iov_len = len; |
| 56 | |
Blue Swirl | 7300c07 | 2010-04-25 18:20:28 +0000 | [diff] [blame] | 57 | while (writev(s->fd, iv, 2) < 0) { |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 58 | if (errno != EAGAIN && errno != EINTR) { |
| 59 | fprintf(stderr, "qemu: error %i writing bluetooth packet.\n", |
| 60 | errno); |
| 61 | return; |
| 62 | } |
Blue Swirl | 7300c07 | 2010-04-25 18:20:28 +0000 | [diff] [blame] | 63 | } |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void bt_host_cmd(struct HCIInfo *hci, const uint8_t *data, int len) |
| 67 | { |
| 68 | bt_host_send(hci, HCI_COMMAND_PKT, data, len); |
| 69 | } |
| 70 | |
| 71 | static void bt_host_acl(struct HCIInfo *hci, const uint8_t *data, int len) |
| 72 | { |
| 73 | bt_host_send(hci, HCI_ACLDATA_PKT, data, len); |
| 74 | } |
| 75 | |
| 76 | static void bt_host_sco(struct HCIInfo *hci, const uint8_t *data, int len) |
| 77 | { |
| 78 | bt_host_send(hci, HCI_SCODATA_PKT, data, len); |
| 79 | } |
| 80 | |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 81 | static void bt_host_read(void *opaque) |
| 82 | { |
| 83 | struct bt_host_hci_s *s = (struct bt_host_hci_s *) opaque; |
| 84 | uint8_t *pkt; |
| 85 | int pktlen; |
| 86 | |
| 87 | /* Seems that we can't read only the header first and then the amount |
| 88 | * of data indicated in the header because Linux will discard everything |
| 89 | * that's not been read in one go. */ |
| 90 | s->len = read(s->fd, s->hdr, sizeof(s->hdr)); |
| 91 | |
| 92 | if (s->len < 0) { |
| 93 | fprintf(stderr, "qemu: error %i reading HCI frame\n", errno); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | pkt = s->hdr; |
| 98 | while (s->len --) |
| 99 | switch (*pkt ++) { |
| 100 | case HCI_EVENT_PKT: |
| 101 | if (s->len < 2) |
| 102 | goto bad_pkt; |
| 103 | |
| 104 | pktlen = MIN(pkt[1] + 2, s->len); |
| 105 | s->hci.evt_recv(s->hci.opaque, pkt, pktlen); |
| 106 | s->len -= pktlen; |
| 107 | pkt += pktlen; |
| 108 | |
| 109 | /* TODO: if this is an Inquiry Result event, it's also |
| 110 | * interpreted by Linux kernel before we received it, possibly |
| 111 | * we should clean the kernel Inquiry cache through |
| 112 | * ioctl(s->fd, HCI_INQUIRY, ...). */ |
| 113 | break; |
| 114 | |
| 115 | case HCI_ACLDATA_PKT: |
| 116 | if (s->len < 4) |
| 117 | goto bad_pkt; |
| 118 | |
| 119 | pktlen = MIN(((pkt[3] << 8) | pkt[2]) + 4, s->len); |
| 120 | s->hci.acl_recv(s->hci.opaque, pkt, pktlen); |
| 121 | s->len -= pktlen; |
| 122 | pkt += pktlen; |
| 123 | break; |
| 124 | |
| 125 | case HCI_SCODATA_PKT: |
| 126 | if (s->len < 3) |
| 127 | goto bad_pkt; |
| 128 | |
| 129 | pktlen = MIN(pkt[2] + 3, s->len); |
| 130 | s->len -= pktlen; |
| 131 | pkt += pktlen; |
Stefan Hajnoczi | f725327 | 2012-01-12 14:17:04 +0000 | [diff] [blame] | 132 | break; |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 133 | |
| 134 | default: |
| 135 | bad_pkt: |
| 136 | fprintf(stderr, "qemu: bad HCI packet type %02x\n", pkt[-1]); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static int bt_host_bdaddr_set(struct HCIInfo *hci, const uint8_t *bd_addr) |
| 141 | { |
| 142 | return -ENOTSUP; |
| 143 | } |
| 144 | |
| 145 | struct HCIInfo *bt_host_hci(const char *id) |
| 146 | { |
| 147 | struct bt_host_hci_s *s; |
| 148 | int fd = -1; |
balrog | 2e9b08e | 2008-09-30 23:31:35 +0000 | [diff] [blame] | 149 | # ifdef CONFIG_BLUEZ |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 150 | int dev_id = hci_devid(id); |
| 151 | struct hci_filter flt; |
| 152 | |
| 153 | if (dev_id < 0) { |
| 154 | fprintf(stderr, "qemu: `%s' not available\n", id); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | fd = hci_open_dev(dev_id); |
| 159 | |
| 160 | /* XXX: can we ensure nobody else has the device opened? */ |
balrog | 2e9b08e | 2008-09-30 23:31:35 +0000 | [diff] [blame] | 161 | # endif |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 162 | |
| 163 | if (fd < 0) { |
| 164 | fprintf(stderr, "qemu: Can't open `%s': %s (%i)\n", |
| 165 | id, strerror(errno), errno); |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 166 | return NULL; |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 167 | } |
| 168 | |
balrog | 2e9b08e | 2008-09-30 23:31:35 +0000 | [diff] [blame] | 169 | # ifdef CONFIG_BLUEZ |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 170 | hci_filter_clear(&flt); |
| 171 | hci_filter_all_ptypes(&flt); |
| 172 | hci_filter_all_events(&flt); |
| 173 | |
Stefan Weil | 9957fc7 | 2013-03-08 19:58:32 +0100 | [diff] [blame] | 174 | if (qemu_setsockopt(fd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 175 | fprintf(stderr, "qemu: Can't set HCI filter on socket (%i)\n", errno); |
| 176 | return 0; |
| 177 | } |
balrog | 2e9b08e | 2008-09-30 23:31:35 +0000 | [diff] [blame] | 178 | # endif |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 179 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 180 | s = g_malloc0(sizeof(struct bt_host_hci_s)); |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 181 | s->fd = fd; |
| 182 | s->hci.cmd_send = bt_host_cmd; |
| 183 | s->hci.sco_send = bt_host_sco; |
| 184 | s->hci.acl_send = bt_host_acl; |
| 185 | s->hci.bdaddr_set = bt_host_bdaddr_set; |
| 186 | |
Juan Quintela | ca96c31 | 2010-03-11 17:55:40 +0100 | [diff] [blame] | 187 | qemu_set_fd_handler(s->fd, bt_host_read, NULL, s); |
balrog | fb599c9 | 2008-09-28 23:49:55 +0000 | [diff] [blame] | 188 | |
| 189 | return &s->hci; |
| 190 | } |
balrog | 2e9b08e | 2008-09-30 23:31:35 +0000 | [diff] [blame] | 191 | #else |
| 192 | struct HCIInfo *bt_host_hci(const char *id) |
| 193 | { |
aurel32 | 978d5d7 | 2008-11-06 09:38:51 +0000 | [diff] [blame] | 194 | fprintf(stderr, "qemu: bluetooth passthrough not supported (yet)\n"); |
balrog | 2e9b08e | 2008-09-30 23:31:35 +0000 | [diff] [blame] | 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | #endif |