1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu-common.h"
22#include "hw/usb.h"
23#include "hw/usb/desc.h"
24#include "sysemu/bt.h"
25#include "hw/bt.h"
26
27struct USBBtState {
28 USBDevice dev;
29 struct HCIInfo *hci;
30 USBEndpoint *intr;
31
32 int config;
33
34#define CFIFO_LEN_MASK 255
35#define DFIFO_LEN_MASK 4095
36 struct usb_hci_in_fifo_s {
37 uint8_t data[(DFIFO_LEN_MASK + 1) * 2];
38 struct {
39 uint8_t *data;
40 int len;
41 } fifo[CFIFO_LEN_MASK + 1];
42 int dstart, dlen, dsize, start, len;
43 } evt, acl, sco;
44
45 struct usb_hci_out_fifo_s {
46 uint8_t data[4096];
47 int len;
48 } outcmd, outacl, outsco;
49};
50
51#define USB_EVT_EP 1
52#define USB_ACL_EP 2
53#define USB_SCO_EP 3
54
55enum {
56 STR_MANUFACTURER = 1,
57 STR_SERIALNUMBER,
58};
59
60static const USBDescStrings desc_strings = {
61 [STR_MANUFACTURER] = "QEMU",
62 [STR_SERIALNUMBER] = "1",
63};
64
65static const USBDescIface desc_iface_bluetooth[] = {
66 {
67 .bInterfaceNumber = 0,
68 .bNumEndpoints = 3,
69 .bInterfaceClass = 0xe0,
70 .bInterfaceSubClass = 0x01,
71 .bInterfaceProtocol = 0x01,
72 .eps = (USBDescEndpoint[]) {
73 {
74 .bEndpointAddress = USB_DIR_IN | USB_EVT_EP,
75 .bmAttributes = USB_ENDPOINT_XFER_INT,
76 .wMaxPacketSize = 0x10,
77 .bInterval = 0x02,
78 },
79 {
80 .bEndpointAddress = USB_DIR_OUT | USB_ACL_EP,
81 .bmAttributes = USB_ENDPOINT_XFER_BULK,
82 .wMaxPacketSize = 0x40,
83 .bInterval = 0x0a,
84 },
85 {
86 .bEndpointAddress = USB_DIR_IN | USB_ACL_EP,
87 .bmAttributes = USB_ENDPOINT_XFER_BULK,
88 .wMaxPacketSize = 0x40,
89 .bInterval = 0x0a,
90 },
91 },
92 },{
93 .bInterfaceNumber = 1,
94 .bAlternateSetting = 0,
95 .bNumEndpoints = 2,
96 .bInterfaceClass = 0xe0,
97 .bInterfaceSubClass = 0x01,
98 .bInterfaceProtocol = 0x01,
99 .eps = (USBDescEndpoint[]) {
100 {
101 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
102 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
103 .wMaxPacketSize = 0,
104 .bInterval = 0x01,
105 },
106 {
107 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
108 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
109 .wMaxPacketSize = 0,
110 .bInterval = 0x01,
111 },
112 },
113 },{
114 .bInterfaceNumber = 1,
115 .bAlternateSetting = 1,
116 .bNumEndpoints = 2,
117 .bInterfaceClass = 0xe0,
118 .bInterfaceSubClass = 0x01,
119 .bInterfaceProtocol = 0x01,
120 .eps = (USBDescEndpoint[]) {
121 {
122 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
123 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
124 .wMaxPacketSize = 0x09,
125 .bInterval = 0x01,
126 },
127 {
128 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
129 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
130 .wMaxPacketSize = 0x09,
131 .bInterval = 0x01,
132 },
133 },
134 },{
135 .bInterfaceNumber = 1,
136 .bAlternateSetting = 2,
137 .bNumEndpoints = 2,
138 .bInterfaceClass = 0xe0,
139 .bInterfaceSubClass = 0x01,
140 .bInterfaceProtocol = 0x01,
141 .eps = (USBDescEndpoint[]) {
142 {
143 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
144 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
145 .wMaxPacketSize = 0x11,
146 .bInterval = 0x01,
147 },
148 {
149 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
150 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
151 .wMaxPacketSize = 0x11,
152 .bInterval = 0x01,
153 },
154 },
155 },{
156 .bInterfaceNumber = 1,
157 .bAlternateSetting = 3,
158 .bNumEndpoints = 2,
159 .bInterfaceClass = 0xe0,
160 .bInterfaceSubClass = 0x01,
161 .bInterfaceProtocol = 0x01,
162 .eps = (USBDescEndpoint[]) {
163 {
164 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
165 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
166 .wMaxPacketSize = 0x19,
167 .bInterval = 0x01,
168 },
169 {
170 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
171 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
172 .wMaxPacketSize = 0x19,
173 .bInterval = 0x01,
174 },
175 },
176 },{
177 .bInterfaceNumber = 1,
178 .bAlternateSetting = 4,
179 .bNumEndpoints = 2,
180 .bInterfaceClass = 0xe0,
181 .bInterfaceSubClass = 0x01,
182 .bInterfaceProtocol = 0x01,
183 .eps = (USBDescEndpoint[]) {
184 {
185 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
186 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
187 .wMaxPacketSize = 0x21,
188 .bInterval = 0x01,
189 },
190 {
191 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
192 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
193 .wMaxPacketSize = 0x21,
194 .bInterval = 0x01,
195 },
196 },
197 },{
198 .bInterfaceNumber = 1,
199 .bAlternateSetting = 5,
200 .bNumEndpoints = 2,
201 .bInterfaceClass = 0xe0,
202 .bInterfaceSubClass = 0x01,
203 .bInterfaceProtocol = 0x01,
204 .eps = (USBDescEndpoint[]) {
205 {
206 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
207 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
208 .wMaxPacketSize = 0x31,
209 .bInterval = 0x01,
210 },
211 {
212 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
213 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
214 .wMaxPacketSize = 0x31,
215 .bInterval = 0x01,
216 },
217 },
218 }
219};
220
221static const USBDescDevice desc_device_bluetooth = {
222 .bcdUSB = 0x0110,
223 .bDeviceClass = 0xe0,
224 .bDeviceSubClass = 0x01,
225 .bDeviceProtocol = 0x01,
226 .bMaxPacketSize0 = 64,
227 .bNumConfigurations = 1,
228 .confs = (USBDescConfig[]) {
229 {
230 .bNumInterfaces = 2,
231 .bConfigurationValue = 1,
232 .bmAttributes = 0xc0,
233 .bMaxPower = 0,
234 .nif = ARRAY_SIZE(desc_iface_bluetooth),
235 .ifs = desc_iface_bluetooth,
236 },
237 },
238};
239
240static const USBDesc desc_bluetooth = {
241 .id = {
242 .idVendor = 0x0a12,
243 .idProduct = 0x0001,
244 .bcdDevice = 0x1958,
245 .iManufacturer = STR_MANUFACTURER,
246 .iProduct = 0,
247 .iSerialNumber = STR_SERIALNUMBER,
248 },
249 .full = &desc_device_bluetooth,
250 .str = desc_strings,
251};
252
253static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s *fifo)
254{
255 fifo->dstart = 0;
256 fifo->dlen = 0;
257 fifo->dsize = DFIFO_LEN_MASK + 1;
258 fifo->start = 0;
259 fifo->len = 0;
260}
261
262static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s *fifo,
263 const uint8_t *data, int len)
264{
265 int off = fifo->dstart + fifo->dlen;
266 uint8_t *buf;
267
268 fifo->dlen += len;
269 if (off <= DFIFO_LEN_MASK) {
270 if (off + len > DFIFO_LEN_MASK + 1 &&
271 (fifo->dsize = off + len) > (DFIFO_LEN_MASK + 1) * 2) {
272 fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
273 exit(-1);
274 }
275 buf = fifo->data + off;
276 } else {
277 if (fifo->dlen > fifo->dsize) {
278 fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
279 exit(-1);
280 }
281 buf = fifo->data + off - fifo->dsize;
282 }
283
284 off = (fifo->start + fifo->len ++) & CFIFO_LEN_MASK;
285 fifo->fifo[off].data = memcpy(buf, data, len);
286 fifo->fifo[off].len = len;
287}
288
289static inline void usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s *fifo,
290 USBPacket *p)
291{
292 int len;
293
294 assert(fifo->len != 0);
295
296 len = MIN(p->iov.size, fifo->fifo[fifo->start].len);
297 usb_packet_copy(p, fifo->fifo[fifo->start].data, len);
298 if (len == p->iov.size) {
299 fifo->fifo[fifo->start].len -= len;
300 fifo->fifo[fifo->start].data += len;
301 } else {
302 fifo->start ++;
303 fifo->start &= CFIFO_LEN_MASK;
304 fifo->len --;
305 }
306
307 fifo->dstart += len;
308 fifo->dlen -= len;
309 if (fifo->dstart >= fifo->dsize) {
310 fifo->dstart = 0;
311 fifo->dsize = DFIFO_LEN_MASK + 1;
312 }
313}
314
315static inline void usb_bt_fifo_out_enqueue(struct USBBtState *s,
316 struct usb_hci_out_fifo_s *fifo,
317 void (*send)(struct HCIInfo *, const uint8_t *, int),
318 int (*complete)(const uint8_t *, int),
319 USBPacket *p)
320{
321 usb_packet_copy(p, fifo->data + fifo->len, p->iov.size);
322 fifo->len += p->iov.size;
323 if (complete(fifo->data, fifo->len)) {
324 send(s->hci, fifo->data, fifo->len);
325 fifo->len = 0;
326 }
327
328
329}
330
331static int usb_bt_hci_cmd_complete(const uint8_t *data, int len)
332{
333 len -= HCI_COMMAND_HDR_SIZE;
334 return len >= 0 &&
335 len >= ((struct hci_command_hdr *) data)->plen;
336}
337
338static int usb_bt_hci_acl_complete(const uint8_t *data, int len)
339{
340 len -= HCI_ACL_HDR_SIZE;
341 return len >= 0 &&
342 len >= le16_to_cpu(((struct hci_acl_hdr *) data)->dlen);
343}
344
345static int usb_bt_hci_sco_complete(const uint8_t *data, int len)
346{
347 len -= HCI_SCO_HDR_SIZE;
348 return len >= 0 &&
349 len >= ((struct hci_sco_hdr *) data)->dlen;
350}
351
352static void usb_bt_handle_reset(USBDevice *dev)
353{
354 struct USBBtState *s = (struct USBBtState *) dev->opaque;
355
356 usb_bt_fifo_reset(&s->evt);
357 usb_bt_fifo_reset(&s->acl);
358 usb_bt_fifo_reset(&s->sco);
359 s->outcmd.len = 0;
360 s->outacl.len = 0;
361 s->outsco.len = 0;
362}
363
364static void usb_bt_handle_control(USBDevice *dev, USBPacket *p,
365 int request, int value, int index, int length, uint8_t *data)
366{
367 struct USBBtState *s = (struct USBBtState *) dev->opaque;
368 int ret;
369
370 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
371 if (ret >= 0) {
372 switch (request) {
373 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
374 s->config = 0;
375 break;
376 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
377 s->config = 1;
378 usb_bt_fifo_reset(&s->evt);
379 usb_bt_fifo_reset(&s->acl);
380 usb_bt_fifo_reset(&s->sco);
381 break;
382 }
383 return;
384 }
385
386 switch (request) {
387 case InterfaceRequest | USB_REQ_GET_STATUS:
388 case EndpointRequest | USB_REQ_GET_STATUS:
389 data[0] = 0x00;
390 data[1] = 0x00;
391 p->actual_length = 2;
392 break;
393 case InterfaceOutRequest | USB_REQ_CLEAR_FEATURE:
394 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
395 goto fail;
396 case InterfaceOutRequest | USB_REQ_SET_FEATURE:
397 case EndpointOutRequest | USB_REQ_SET_FEATURE:
398 goto fail;
399 break;
400 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_DEVICE) << 8):
401 if (s->config)
402 usb_bt_fifo_out_enqueue(s, &s->outcmd, s->hci->cmd_send,
403 usb_bt_hci_cmd_complete, p);
404 break;
405 default:
406 fail:
407 p->status = USB_RET_STALL;
408 break;
409 }
410}
411
412static void usb_bt_handle_data(USBDevice *dev, USBPacket *p)
413{
414 struct USBBtState *s = (struct USBBtState *) dev->opaque;
415
416 if (!s->config)
417 goto fail;
418
419 switch (p->pid) {
420 case USB_TOKEN_IN:
421 switch (p->ep->nr) {
422 case USB_EVT_EP:
423 if (s->evt.len == 0) {
424 p->status = USB_RET_NAK;
425 break;
426 }
427 usb_bt_fifo_dequeue(&s->evt, p);
428 break;
429
430 case USB_ACL_EP:
431 if (s->evt.len == 0) {
432 p->status = USB_RET_STALL;
433 break;
434 }
435 usb_bt_fifo_dequeue(&s->acl, p);
436 break;
437
438 case USB_SCO_EP:
439 if (s->evt.len == 0) {
440 p->status = USB_RET_STALL;
441 break;
442 }
443 usb_bt_fifo_dequeue(&s->sco, p);
444 break;
445
446 default:
447 goto fail;
448 }
449 break;
450
451 case USB_TOKEN_OUT:
452 switch (p->ep->nr) {
453 case USB_ACL_EP:
454 usb_bt_fifo_out_enqueue(s, &s->outacl, s->hci->acl_send,
455 usb_bt_hci_acl_complete, p);
456 break;
457
458 case USB_SCO_EP:
459 usb_bt_fifo_out_enqueue(s, &s->outsco, s->hci->sco_send,
460 usb_bt_hci_sco_complete, p);
461 break;
462
463 default:
464 goto fail;
465 }
466 break;
467
468 default:
469 fail:
470 p->status = USB_RET_STALL;
471 break;
472 }
473}
474
475static void usb_bt_out_hci_packet_event(void *opaque,
476 const uint8_t *data, int len)
477{
478 struct USBBtState *s = (struct USBBtState *) opaque;
479
480 if (s->evt.len == 0) {
481 usb_wakeup(s->intr, 0);
482 }
483 usb_bt_fifo_enqueue(&s->evt, data, len);
484}
485
486static void usb_bt_out_hci_packet_acl(void *opaque,
487 const uint8_t *data, int len)
488{
489 struct USBBtState *s = (struct USBBtState *) opaque;
490
491 usb_bt_fifo_enqueue(&s->acl, data, len);
492}
493
494static void usb_bt_handle_destroy(USBDevice *dev)
495{
496 struct USBBtState *s = (struct USBBtState *) dev->opaque;
497
498 s->hci->opaque = NULL;
499 s->hci->evt_recv = NULL;
500 s->hci->acl_recv = NULL;
501}
502
503static int usb_bt_initfn(USBDevice *dev)
504{
505 struct USBBtState *s = DO_UPCAST(struct USBBtState, dev, dev);
506
507 usb_desc_create_serial(dev);
508 usb_desc_init(dev);
509 s->intr = usb_ep_get(dev, USB_TOKEN_IN, USB_EVT_EP);
510
511 return 0;
512}
513
514static USBDevice *usb_bt_init(USBBus *bus, const char *cmdline)
515{
516 USBDevice *dev;
517 struct USBBtState *s;
518 HCIInfo *hci;
519
520 if (*cmdline) {
521 hci = hci_init(cmdline);
522 } else {
523 hci = bt_new_hci(qemu_find_bt_vlan(0));
524 }
525
526 if (!hci)
527 return NULL;
528 dev = usb_create_simple(bus, "usb-bt-dongle");
529 if (!dev) {
530 return NULL;
531 }
532 s = DO_UPCAST(struct USBBtState, dev, dev);
533 s->dev.opaque = s;
534
535 s->hci = hci;
536 s->hci->opaque = s;
537 s->hci->evt_recv = usb_bt_out_hci_packet_event;
538 s->hci->acl_recv = usb_bt_out_hci_packet_acl;
539
540 usb_bt_handle_reset(&s->dev);
541
542 return dev;
543}
544
545static const VMStateDescription vmstate_usb_bt = {
546 .name = "usb-bt",
547 .unmigratable = 1,
548};
549
550static void usb_bt_class_initfn(ObjectClass *klass, void *data)
551{
552 DeviceClass *dc = DEVICE_CLASS(klass);
553 USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
554
555 uc->init = usb_bt_initfn;
556 uc->product_desc = "QEMU BT dongle";
557 uc->usb_desc = &desc_bluetooth;
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;
562 dc->vmsd = &vmstate_usb_bt;
563 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
564}
565
566static const TypeInfo bt_info = {
567 .name = "usb-bt-dongle",
568 .parent = TYPE_USB_DEVICE,
569 .instance_size = sizeof(struct USBBtState),
570 .class_init = usb_bt_class_initfn,
571};
572
573static void usb_bt_register_types(void)
574{
575 type_register_static(&bt_info);
576 usb_legacy_register("usb-bt-dongle", "bt", usb_bt_init);
577}
578
579type_init(usb_bt_register_types)
580