balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Bluetooth HCI USB Transport Layer v1.0 |
| 3 | * |
| 4 | * Copyright (C) 2007 OpenMoko, Inc. |
| 5 | * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 or |
| 10 | * (at your option) version 3 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
aurel32 | fad6cb1 | 2009-01-04 22:05:52 +0000 | [diff] [blame] | 17 | * 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] | 18 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "qemu-common.h" |
Hani Benhabiles | c340a28 | 2014-06-18 00:23:34 +0100 | [diff] [blame] | 22 | #include "qemu/error-report.h" |
Gerd Hoffmann | f1ae32a | 2012-03-07 14:55:18 +0100 | [diff] [blame] | 23 | #include "hw/usb.h" |
| 24 | #include "hw/usb/desc.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 25 | #include "sysemu/bt.h" |
Gerd Hoffmann | f1ae32a | 2012-03-07 14:55:18 +0100 | [diff] [blame] | 26 | #include "hw/bt.h" |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 27 | |
| 28 | struct USBBtState { |
| 29 | USBDevice dev; |
| 30 | struct HCIInfo *hci; |
Hans de Goede | 33c1a68 | 2012-11-17 12:15:01 +0100 | [diff] [blame] | 31 | USBEndpoint *intr; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 32 | |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 33 | int config; |
| 34 | |
| 35 | #define CFIFO_LEN_MASK 255 |
| 36 | #define DFIFO_LEN_MASK 4095 |
| 37 | struct usb_hci_in_fifo_s { |
| 38 | uint8_t data[(DFIFO_LEN_MASK + 1) * 2]; |
| 39 | struct { |
| 40 | uint8_t *data; |
| 41 | int len; |
| 42 | } fifo[CFIFO_LEN_MASK + 1]; |
| 43 | int dstart, dlen, dsize, start, len; |
| 44 | } evt, acl, sco; |
| 45 | |
| 46 | struct usb_hci_out_fifo_s { |
| 47 | uint8_t data[4096]; |
| 48 | int len; |
| 49 | } outcmd, outacl, outsco; |
| 50 | }; |
| 51 | |
Gonglei | a293e82 | 2015-05-06 20:55:25 +0800 | [diff] [blame] | 52 | #define TYPE_USB_BT "usb-bt-dongle" |
| 53 | #define USB_BT(obj) OBJECT_CHECK(struct USBBtState, (obj), TYPE_USB_BT) |
| 54 | |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 55 | #define USB_EVT_EP 1 |
| 56 | #define USB_ACL_EP 2 |
| 57 | #define USB_SCO_EP 3 |
| 58 | |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 59 | enum { |
| 60 | STR_MANUFACTURER = 1, |
| 61 | STR_SERIALNUMBER, |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 64 | static const USBDescStrings desc_strings = { |
Crístian Viana | 93bfef4 | 2012-05-30 00:35:51 -0300 | [diff] [blame] | 65 | [STR_MANUFACTURER] = "QEMU", |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 66 | [STR_SERIALNUMBER] = "1", |
| 67 | }; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 68 | |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 69 | static const USBDescIface desc_iface_bluetooth[] = { |
| 70 | { |
| 71 | .bInterfaceNumber = 0, |
| 72 | .bNumEndpoints = 3, |
| 73 | .bInterfaceClass = 0xe0, /* Wireless */ |
| 74 | .bInterfaceSubClass = 0x01, /* Radio Frequency */ |
| 75 | .bInterfaceProtocol = 0x01, /* Bluetooth */ |
| 76 | .eps = (USBDescEndpoint[]) { |
| 77 | { |
| 78 | .bEndpointAddress = USB_DIR_IN | USB_EVT_EP, |
| 79 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 80 | .wMaxPacketSize = 0x10, |
| 81 | .bInterval = 0x02, |
| 82 | }, |
| 83 | { |
| 84 | .bEndpointAddress = USB_DIR_OUT | USB_ACL_EP, |
| 85 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 86 | .wMaxPacketSize = 0x40, |
| 87 | .bInterval = 0x0a, |
| 88 | }, |
| 89 | { |
| 90 | .bEndpointAddress = USB_DIR_IN | USB_ACL_EP, |
| 91 | .bmAttributes = USB_ENDPOINT_XFER_BULK, |
| 92 | .wMaxPacketSize = 0x40, |
| 93 | .bInterval = 0x0a, |
| 94 | }, |
| 95 | }, |
| 96 | },{ |
| 97 | .bInterfaceNumber = 1, |
| 98 | .bAlternateSetting = 0, |
| 99 | .bNumEndpoints = 2, |
| 100 | .bInterfaceClass = 0xe0, /* Wireless */ |
| 101 | .bInterfaceSubClass = 0x01, /* Radio Frequency */ |
| 102 | .bInterfaceProtocol = 0x01, /* Bluetooth */ |
| 103 | .eps = (USBDescEndpoint[]) { |
| 104 | { |
| 105 | .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 106 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 107 | .wMaxPacketSize = 0, |
| 108 | .bInterval = 0x01, |
| 109 | }, |
| 110 | { |
| 111 | .bEndpointAddress = USB_DIR_IN | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 112 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 113 | .wMaxPacketSize = 0, |
| 114 | .bInterval = 0x01, |
| 115 | }, |
| 116 | }, |
| 117 | },{ |
| 118 | .bInterfaceNumber = 1, |
| 119 | .bAlternateSetting = 1, |
| 120 | .bNumEndpoints = 2, |
| 121 | .bInterfaceClass = 0xe0, /* Wireless */ |
| 122 | .bInterfaceSubClass = 0x01, /* Radio Frequency */ |
| 123 | .bInterfaceProtocol = 0x01, /* Bluetooth */ |
| 124 | .eps = (USBDescEndpoint[]) { |
| 125 | { |
| 126 | .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 127 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 128 | .wMaxPacketSize = 0x09, |
| 129 | .bInterval = 0x01, |
| 130 | }, |
| 131 | { |
| 132 | .bEndpointAddress = USB_DIR_IN | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 133 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 134 | .wMaxPacketSize = 0x09, |
| 135 | .bInterval = 0x01, |
| 136 | }, |
| 137 | }, |
| 138 | },{ |
| 139 | .bInterfaceNumber = 1, |
| 140 | .bAlternateSetting = 2, |
| 141 | .bNumEndpoints = 2, |
| 142 | .bInterfaceClass = 0xe0, /* Wireless */ |
| 143 | .bInterfaceSubClass = 0x01, /* Radio Frequency */ |
| 144 | .bInterfaceProtocol = 0x01, /* Bluetooth */ |
| 145 | .eps = (USBDescEndpoint[]) { |
| 146 | { |
| 147 | .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 148 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 149 | .wMaxPacketSize = 0x11, |
| 150 | .bInterval = 0x01, |
| 151 | }, |
| 152 | { |
| 153 | .bEndpointAddress = USB_DIR_IN | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 154 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 155 | .wMaxPacketSize = 0x11, |
| 156 | .bInterval = 0x01, |
| 157 | }, |
| 158 | }, |
| 159 | },{ |
| 160 | .bInterfaceNumber = 1, |
| 161 | .bAlternateSetting = 3, |
| 162 | .bNumEndpoints = 2, |
| 163 | .bInterfaceClass = 0xe0, /* Wireless */ |
| 164 | .bInterfaceSubClass = 0x01, /* Radio Frequency */ |
| 165 | .bInterfaceProtocol = 0x01, /* Bluetooth */ |
| 166 | .eps = (USBDescEndpoint[]) { |
| 167 | { |
| 168 | .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 169 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 170 | .wMaxPacketSize = 0x19, |
| 171 | .bInterval = 0x01, |
| 172 | }, |
| 173 | { |
| 174 | .bEndpointAddress = USB_DIR_IN | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 175 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 176 | .wMaxPacketSize = 0x19, |
| 177 | .bInterval = 0x01, |
| 178 | }, |
| 179 | }, |
| 180 | },{ |
| 181 | .bInterfaceNumber = 1, |
| 182 | .bAlternateSetting = 4, |
| 183 | .bNumEndpoints = 2, |
| 184 | .bInterfaceClass = 0xe0, /* Wireless */ |
| 185 | .bInterfaceSubClass = 0x01, /* Radio Frequency */ |
| 186 | .bInterfaceProtocol = 0x01, /* Bluetooth */ |
| 187 | .eps = (USBDescEndpoint[]) { |
| 188 | { |
| 189 | .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 190 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 191 | .wMaxPacketSize = 0x21, |
| 192 | .bInterval = 0x01, |
| 193 | }, |
| 194 | { |
| 195 | .bEndpointAddress = USB_DIR_IN | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 196 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 197 | .wMaxPacketSize = 0x21, |
| 198 | .bInterval = 0x01, |
| 199 | }, |
| 200 | }, |
| 201 | },{ |
| 202 | .bInterfaceNumber = 1, |
| 203 | .bAlternateSetting = 5, |
| 204 | .bNumEndpoints = 2, |
| 205 | .bInterfaceClass = 0xe0, /* Wireless */ |
| 206 | .bInterfaceSubClass = 0x01, /* Radio Frequency */ |
| 207 | .bInterfaceProtocol = 0x01, /* Bluetooth */ |
| 208 | .eps = (USBDescEndpoint[]) { |
| 209 | { |
| 210 | .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 211 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 212 | .wMaxPacketSize = 0x31, |
| 213 | .bInterval = 0x01, |
| 214 | }, |
| 215 | { |
| 216 | .bEndpointAddress = USB_DIR_IN | USB_SCO_EP, |
Gerd Hoffmann | dd850cf | 2011-07-06 12:40:28 +0200 | [diff] [blame] | 217 | .bmAttributes = USB_ENDPOINT_XFER_ISOC, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 218 | .wMaxPacketSize = 0x31, |
| 219 | .bInterval = 0x01, |
| 220 | }, |
| 221 | }, |
| 222 | } |
| 223 | }; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 224 | |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 225 | static const USBDescDevice desc_device_bluetooth = { |
| 226 | .bcdUSB = 0x0110, |
| 227 | .bDeviceClass = 0xe0, /* Wireless */ |
| 228 | .bDeviceSubClass = 0x01, /* Radio Frequency */ |
| 229 | .bDeviceProtocol = 0x01, /* Bluetooth */ |
| 230 | .bMaxPacketSize0 = 64, |
| 231 | .bNumConfigurations = 1, |
| 232 | .confs = (USBDescConfig[]) { |
| 233 | { |
| 234 | .bNumInterfaces = 2, |
| 235 | .bConfigurationValue = 1, |
Pantelis Koukousoulas | bd93976 | 2013-12-16 09:42:49 +0200 | [diff] [blame] | 236 | .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER, |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 237 | .bMaxPower = 0, |
| 238 | .nif = ARRAY_SIZE(desc_iface_bluetooth), |
| 239 | .ifs = desc_iface_bluetooth, |
| 240 | }, |
| 241 | }, |
| 242 | }; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 243 | |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 244 | static const USBDesc desc_bluetooth = { |
| 245 | .id = { |
| 246 | .idVendor = 0x0a12, |
| 247 | .idProduct = 0x0001, |
| 248 | .bcdDevice = 0x1958, |
| 249 | .iManufacturer = STR_MANUFACTURER, |
| 250 | .iProduct = 0, |
| 251 | .iSerialNumber = STR_SERIALNUMBER, |
| 252 | }, |
| 253 | .full = &desc_device_bluetooth, |
| 254 | .str = desc_strings, |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s *fifo) |
| 258 | { |
| 259 | fifo->dstart = 0; |
| 260 | fifo->dlen = 0; |
| 261 | fifo->dsize = DFIFO_LEN_MASK + 1; |
| 262 | fifo->start = 0; |
| 263 | fifo->len = 0; |
| 264 | } |
| 265 | |
| 266 | static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s *fifo, |
| 267 | const uint8_t *data, int len) |
| 268 | { |
| 269 | int off = fifo->dstart + fifo->dlen; |
| 270 | uint8_t *buf; |
| 271 | |
| 272 | fifo->dlen += len; |
| 273 | if (off <= DFIFO_LEN_MASK) { |
| 274 | if (off + len > DFIFO_LEN_MASK + 1 && |
| 275 | (fifo->dsize = off + len) > (DFIFO_LEN_MASK + 1) * 2) { |
| 276 | fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len); |
| 277 | exit(-1); |
| 278 | } |
| 279 | buf = fifo->data + off; |
| 280 | } else { |
| 281 | if (fifo->dlen > fifo->dsize) { |
| 282 | fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len); |
| 283 | exit(-1); |
| 284 | } |
| 285 | buf = fifo->data + off - fifo->dsize; |
| 286 | } |
| 287 | |
| 288 | off = (fifo->start + fifo->len ++) & CFIFO_LEN_MASK; |
| 289 | fifo->fifo[off].data = memcpy(buf, data, len); |
| 290 | fifo->fifo[off].len = len; |
| 291 | } |
| 292 | |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 293 | static inline void usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s *fifo, |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 294 | USBPacket *p) |
| 295 | { |
| 296 | int len; |
| 297 | |
Hans de Goede | 33c1a68 | 2012-11-17 12:15:01 +0100 | [diff] [blame] | 298 | assert(fifo->len != 0); |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 299 | |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 300 | len = MIN(p->iov.size, fifo->fifo[fifo->start].len); |
| 301 | usb_packet_copy(p, fifo->fifo[fifo->start].data, len); |
| 302 | if (len == p->iov.size) { |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 303 | fifo->fifo[fifo->start].len -= len; |
| 304 | fifo->fifo[fifo->start].data += len; |
| 305 | } else { |
| 306 | fifo->start ++; |
| 307 | fifo->start &= CFIFO_LEN_MASK; |
| 308 | fifo->len --; |
| 309 | } |
| 310 | |
| 311 | fifo->dstart += len; |
| 312 | fifo->dlen -= len; |
| 313 | if (fifo->dstart >= fifo->dsize) { |
| 314 | fifo->dstart = 0; |
| 315 | fifo->dsize = DFIFO_LEN_MASK + 1; |
| 316 | } |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Juan Quintela | 86178a5 | 2009-09-23 01:19:00 +0200 | [diff] [blame] | 319 | static inline void usb_bt_fifo_out_enqueue(struct USBBtState *s, |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 320 | struct usb_hci_out_fifo_s *fifo, |
| 321 | void (*send)(struct HCIInfo *, const uint8_t *, int), |
| 322 | int (*complete)(const uint8_t *, int), |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 323 | USBPacket *p) |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 324 | { |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 325 | usb_packet_copy(p, fifo->data + fifo->len, p->iov.size); |
| 326 | fifo->len += p->iov.size; |
| 327 | if (complete(fifo->data, fifo->len)) { |
| 328 | send(s->hci, fifo->data, fifo->len); |
| 329 | fifo->len = 0; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /* TODO: do we need to loop? */ |
| 333 | } |
| 334 | |
| 335 | static int usb_bt_hci_cmd_complete(const uint8_t *data, int len) |
| 336 | { |
| 337 | len -= HCI_COMMAND_HDR_SIZE; |
| 338 | return len >= 0 && |
| 339 | len >= ((struct hci_command_hdr *) data)->plen; |
| 340 | } |
| 341 | |
| 342 | static int usb_bt_hci_acl_complete(const uint8_t *data, int len) |
| 343 | { |
| 344 | len -= HCI_ACL_HDR_SIZE; |
| 345 | return len >= 0 && |
| 346 | len >= le16_to_cpu(((struct hci_acl_hdr *) data)->dlen); |
| 347 | } |
| 348 | |
| 349 | static int usb_bt_hci_sco_complete(const uint8_t *data, int len) |
| 350 | { |
| 351 | len -= HCI_SCO_HDR_SIZE; |
| 352 | return len >= 0 && |
| 353 | len >= ((struct hci_sco_hdr *) data)->dlen; |
| 354 | } |
| 355 | |
| 356 | static void usb_bt_handle_reset(USBDevice *dev) |
| 357 | { |
| 358 | struct USBBtState *s = (struct USBBtState *) dev->opaque; |
| 359 | |
| 360 | usb_bt_fifo_reset(&s->evt); |
| 361 | usb_bt_fifo_reset(&s->acl); |
| 362 | usb_bt_fifo_reset(&s->sco); |
| 363 | s->outcmd.len = 0; |
| 364 | s->outacl.len = 0; |
| 365 | s->outsco.len = 0; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 368 | static void usb_bt_handle_control(USBDevice *dev, USBPacket *p, |
Hans de Goede | 007fd62 | 2011-02-02 16:33:13 +0100 | [diff] [blame] | 369 | int request, int value, int index, int length, uint8_t *data) |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 370 | { |
| 371 | struct USBBtState *s = (struct USBBtState *) dev->opaque; |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 372 | int ret; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 373 | |
Hans de Goede | 007fd62 | 2011-02-02 16:33:13 +0100 | [diff] [blame] | 374 | ret = usb_desc_handle_control(dev, p, request, value, index, length, data); |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 375 | if (ret >= 0) { |
Gerd Hoffmann | a980a06 | 2010-11-26 20:20:41 +0100 | [diff] [blame] | 376 | switch (request) { |
| 377 | case DeviceRequest | USB_REQ_GET_CONFIGURATION: |
| 378 | s->config = 0; |
| 379 | break; |
| 380 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
| 381 | s->config = 1; |
| 382 | usb_bt_fifo_reset(&s->evt); |
| 383 | usb_bt_fifo_reset(&s->acl); |
| 384 | usb_bt_fifo_reset(&s->sco); |
| 385 | break; |
| 386 | } |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 387 | return; |
Gerd Hoffmann | 4696425 | 2010-11-25 16:12:18 +0100 | [diff] [blame] | 388 | } |
| 389 | |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 390 | switch (request) { |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 391 | case InterfaceRequest | USB_REQ_GET_STATUS: |
| 392 | case EndpointRequest | USB_REQ_GET_STATUS: |
Gerd Hoffmann | ed5a83d | 2010-11-30 17:35:34 +0100 | [diff] [blame] | 393 | data[0] = 0x00; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 394 | data[1] = 0x00; |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 395 | p->actual_length = 2; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 396 | break; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 397 | case InterfaceOutRequest | USB_REQ_CLEAR_FEATURE: |
| 398 | case EndpointOutRequest | USB_REQ_CLEAR_FEATURE: |
Gerd Hoffmann | ed5a83d | 2010-11-30 17:35:34 +0100 | [diff] [blame] | 399 | goto fail; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 400 | case InterfaceOutRequest | USB_REQ_SET_FEATURE: |
| 401 | case EndpointOutRequest | USB_REQ_SET_FEATURE: |
Gerd Hoffmann | ed5a83d | 2010-11-30 17:35:34 +0100 | [diff] [blame] | 402 | goto fail; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 403 | break; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 404 | case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_DEVICE) << 8): |
| 405 | if (s->config) |
| 406 | usb_bt_fifo_out_enqueue(s, &s->outcmd, s->hci->cmd_send, |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 407 | usb_bt_hci_cmd_complete, p); |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 408 | break; |
| 409 | default: |
| 410 | fail: |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 411 | p->status = USB_RET_STALL; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 412 | break; |
| 413 | } |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 416 | static void usb_bt_handle_data(USBDevice *dev, USBPacket *p) |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 417 | { |
| 418 | struct USBBtState *s = (struct USBBtState *) dev->opaque; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 419 | |
| 420 | if (!s->config) |
| 421 | goto fail; |
| 422 | |
| 423 | switch (p->pid) { |
| 424 | case USB_TOKEN_IN: |
Gerd Hoffmann | 079d0b7 | 2012-01-12 13:23:01 +0100 | [diff] [blame] | 425 | switch (p->ep->nr) { |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 426 | case USB_EVT_EP: |
Hans de Goede | 33c1a68 | 2012-11-17 12:15:01 +0100 | [diff] [blame] | 427 | if (s->evt.len == 0) { |
| 428 | p->status = USB_RET_NAK; |
| 429 | break; |
| 430 | } |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 431 | usb_bt_fifo_dequeue(&s->evt, p); |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 432 | break; |
| 433 | |
| 434 | case USB_ACL_EP: |
Hans de Goede | 33c1a68 | 2012-11-17 12:15:01 +0100 | [diff] [blame] | 435 | if (s->evt.len == 0) { |
| 436 | p->status = USB_RET_STALL; |
| 437 | break; |
| 438 | } |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 439 | usb_bt_fifo_dequeue(&s->acl, p); |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 440 | break; |
| 441 | |
| 442 | case USB_SCO_EP: |
Hans de Goede | 33c1a68 | 2012-11-17 12:15:01 +0100 | [diff] [blame] | 443 | if (s->evt.len == 0) { |
| 444 | p->status = USB_RET_STALL; |
| 445 | break; |
| 446 | } |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 447 | usb_bt_fifo_dequeue(&s->sco, p); |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 448 | break; |
| 449 | |
| 450 | default: |
| 451 | goto fail; |
| 452 | } |
| 453 | break; |
| 454 | |
| 455 | case USB_TOKEN_OUT: |
Gerd Hoffmann | 079d0b7 | 2012-01-12 13:23:01 +0100 | [diff] [blame] | 456 | switch (p->ep->nr) { |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 457 | case USB_ACL_EP: |
| 458 | usb_bt_fifo_out_enqueue(s, &s->outacl, s->hci->acl_send, |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 459 | usb_bt_hci_acl_complete, p); |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 460 | break; |
| 461 | |
| 462 | case USB_SCO_EP: |
| 463 | usb_bt_fifo_out_enqueue(s, &s->outsco, s->hci->sco_send, |
Gerd Hoffmann | 4f4321c | 2011-07-12 15:22:25 +0200 | [diff] [blame] | 464 | usb_bt_hci_sco_complete, p); |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 465 | break; |
| 466 | |
| 467 | default: |
| 468 | goto fail; |
| 469 | } |
| 470 | break; |
| 471 | |
| 472 | default: |
| 473 | fail: |
Hans de Goede | 9a77a0f | 2012-11-01 17:15:01 +0100 | [diff] [blame] | 474 | p->status = USB_RET_STALL; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 475 | break; |
| 476 | } |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | static void usb_bt_out_hci_packet_event(void *opaque, |
| 480 | const uint8_t *data, int len) |
| 481 | { |
| 482 | struct USBBtState *s = (struct USBBtState *) opaque; |
| 483 | |
Hans de Goede | 33c1a68 | 2012-11-17 12:15:01 +0100 | [diff] [blame] | 484 | if (s->evt.len == 0) { |
Gerd Hoffmann | 8550a02 | 2013-01-29 12:44:35 +0100 | [diff] [blame] | 485 | usb_wakeup(s->intr, 0); |
Hans de Goede | 33c1a68 | 2012-11-17 12:15:01 +0100 | [diff] [blame] | 486 | } |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 487 | usb_bt_fifo_enqueue(&s->evt, data, len); |
| 488 | } |
| 489 | |
| 490 | static void usb_bt_out_hci_packet_acl(void *opaque, |
| 491 | const uint8_t *data, int len) |
| 492 | { |
| 493 | struct USBBtState *s = (struct USBBtState *) opaque; |
| 494 | |
| 495 | usb_bt_fifo_enqueue(&s->acl, data, len); |
| 496 | } |
| 497 | |
| 498 | static void usb_bt_handle_destroy(USBDevice *dev) |
| 499 | { |
| 500 | struct USBBtState *s = (struct USBBtState *) dev->opaque; |
| 501 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 502 | s->hci->opaque = NULL; |
| 503 | s->hci->evt_recv = NULL; |
| 504 | s->hci->acl_recv = NULL; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Gonglei | 63cdca3 | 2014-09-19 14:48:33 +0800 | [diff] [blame] | 507 | static void usb_bt_realize(USBDevice *dev, Error **errp) |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 508 | { |
Gonglei | a293e82 | 2015-05-06 20:55:25 +0800 | [diff] [blame] | 509 | struct USBBtState *s = USB_BT(dev); |
Hans de Goede | 33c1a68 | 2012-11-17 12:15:01 +0100 | [diff] [blame] | 510 | |
Gerd Hoffmann | 9d55d1a | 2012-04-20 12:33:30 +0200 | [diff] [blame] | 511 | usb_desc_create_serial(dev); |
Gerd Hoffmann | a980a06 | 2010-11-26 20:20:41 +0100 | [diff] [blame] | 512 | usb_desc_init(dev); |
Hani Benhabiles | c340a28 | 2014-06-18 00:23:34 +0100 | [diff] [blame] | 513 | s->dev.opaque = s; |
| 514 | if (!s->hci) { |
| 515 | s->hci = bt_new_hci(qemu_find_bt_vlan(0)); |
| 516 | } |
| 517 | s->hci->opaque = s; |
| 518 | s->hci->evt_recv = usb_bt_out_hci_packet_event; |
| 519 | s->hci->acl_recv = usb_bt_out_hci_packet_acl; |
| 520 | usb_bt_handle_reset(&s->dev); |
Hans de Goede | 33c1a68 | 2012-11-17 12:15:01 +0100 | [diff] [blame] | 521 | s->intr = usb_ep_get(dev, USB_TOKEN_IN, USB_EVT_EP); |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Miroslav Rezanina | 615fe4d | 2013-09-03 11:23:09 +0200 | [diff] [blame] | 524 | static USBDevice *usb_bt_init(USBBus *bus, const char *cmdline) |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 525 | { |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 526 | USBDevice *dev; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 527 | struct USBBtState *s; |
Miroslav Rezanina | 615fe4d | 2013-09-03 11:23:09 +0200 | [diff] [blame] | 528 | HCIInfo *hci; |
Gonglei | a293e82 | 2015-05-06 20:55:25 +0800 | [diff] [blame] | 529 | const char *name = TYPE_USB_BT; |
Miroslav Rezanina | 615fe4d | 2013-09-03 11:23:09 +0200 | [diff] [blame] | 530 | |
| 531 | if (*cmdline) { |
| 532 | hci = hci_init(cmdline); |
| 533 | } else { |
| 534 | hci = bt_new_hci(qemu_find_bt_vlan(0)); |
| 535 | } |
balrog | 2d56469 | 2008-11-09 02:24:54 +0000 | [diff] [blame] | 536 | if (!hci) |
| 537 | return NULL; |
Markus Armbruster | 4806ec9 | 2015-02-04 13:28:08 +0100 | [diff] [blame] | 538 | |
Hani Benhabiles | c340a28 | 2014-06-18 00:23:34 +0100 | [diff] [blame] | 539 | dev = usb_create(bus, name); |
Gonglei | a293e82 | 2015-05-06 20:55:25 +0800 | [diff] [blame] | 540 | s = USB_BT(dev); |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 541 | s->hci = hci; |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 542 | return dev; |
balrog | e6a6d5a | 2008-09-29 00:40:44 +0000 | [diff] [blame] | 543 | } |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 544 | |
Gerd Hoffmann | 2474e50 | 2011-07-20 10:02:40 +0200 | [diff] [blame] | 545 | static const VMStateDescription vmstate_usb_bt = { |
| 546 | .name = "usb-bt", |
| 547 | .unmigratable = 1, |
| 548 | }; |
| 549 | |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 550 | static void usb_bt_class_initfn(ObjectClass *klass, void *data) |
| 551 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 552 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 553 | USBDeviceClass *uc = USB_DEVICE_CLASS(klass); |
| 554 | |
Gonglei | 63cdca3 | 2014-09-19 14:48:33 +0800 | [diff] [blame] | 555 | uc->realize = usb_bt_realize; |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 556 | uc->product_desc = "QEMU BT dongle"; |
| 557 | uc->usb_desc = &desc_bluetooth; |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 558 | uc->handle_reset = usb_bt_handle_reset; |
| 559 | uc->handle_control = usb_bt_handle_control; |
| 560 | uc->handle_data = usb_bt_handle_data; |
| 561 | uc->handle_destroy = usb_bt_handle_destroy; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 562 | dc->vmsd = &vmstate_usb_bt; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 563 | set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); |
Anthony Liguori | 62aed76 | 2011-12-15 14:53:10 -0600 | [diff] [blame] | 564 | } |
| 565 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 566 | static const TypeInfo bt_info = { |
Gonglei | a293e82 | 2015-05-06 20:55:25 +0800 | [diff] [blame] | 567 | .name = TYPE_USB_BT, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 568 | .parent = TYPE_USB_DEVICE, |
| 569 | .instance_size = sizeof(struct USBBtState), |
| 570 | .class_init = usb_bt_class_initfn, |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 571 | }; |
| 572 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 573 | static void usb_bt_register_types(void) |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 574 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 575 | type_register_static(&bt_info); |
Gonglei | a293e82 | 2015-05-06 20:55:25 +0800 | [diff] [blame] | 576 | usb_legacy_register(TYPE_USB_BT, "bt", usb_bt_init); |
Gerd Hoffmann | 806b602 | 2009-08-31 14:23:59 +0200 | [diff] [blame] | 577 | } |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 578 | |
| 579 | type_init(usb_bt_register_types) |