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 "usb.h"
23#include "net.h"
24#include "bt.h"
25
26struct USBBtState {
27 USBDevice dev;
28 struct HCIInfo *hci;
29
30 int altsetting;
31 int config;
32
33#define CFIFO_LEN_MASK 255
34#define DFIFO_LEN_MASK 4095
35 struct usb_hci_in_fifo_s {
36 uint8_t data[(DFIFO_LEN_MASK + 1) * 2];
37 struct {
38 uint8_t *data;
39 int len;
40 } fifo[CFIFO_LEN_MASK + 1];
41 int dstart, dlen, dsize, start, len;
42 } evt, acl, sco;
43
44 struct usb_hci_out_fifo_s {
45 uint8_t data[4096];
46 int len;
47 } outcmd, outacl, outsco;
48};
49
50#define USB_EVT_EP 1
51#define USB_ACL_EP 2
52#define USB_SCO_EP 3
53
54static const uint8_t qemu_bt_dev_descriptor[] = {
55 0x12,
56 USB_DT_DEVICE,
57 0x10, 0x01,
58
59 0xe0,
60 0x01,
61 0x01,
62 0x40,
63
64 0x12, 0x0a,
65 0x01, 0x00,
66 0x58, 0x19,
67
68 0x00,
69 0x00,
70 0x00,
71 0x01,
72};
73
74static const uint8_t qemu_bt_config_descriptor[] = {
75
76 0x09,
77 USB_DT_CONFIG,
78 0xb1, 0x00,
79 0x02,
80 0x01,
81 0x00,
82 0xc0,
83
84
85
86
87 0x00,
88
89
90
91
92
93
94
95
96
97
98
99
100
101 0x09,
102 USB_DT_INTERFACE,
103 0x00,
104 0x00,
105 0x03,
106 0xe0,
107 0x01,
108 0x01,
109 0x00,
110
111
112 0x07,
113 USB_DT_ENDPOINT,
114 USB_DIR_IN | USB_EVT_EP,
115 0x03,
116 0x10, 0x00,
117 0x02,
118
119
120 0x07,
121 USB_DT_ENDPOINT,
122 USB_DIR_OUT | USB_ACL_EP,
123 0x02,
124 0x40, 0x00,
125 0x0a,
126
127
128 0x07,
129 USB_DT_ENDPOINT,
130 USB_DIR_IN | USB_ACL_EP,
131 0x02,
132 0x40, 0x00,
133 0x0a,
134
135
136 0x09,
137 USB_DT_INTERFACE,
138 0x01,
139 0x00,
140 0x02,
141 0xe0,
142 0x01,
143 0x01,
144 0x00,
145
146
147 0x07,
148 USB_DT_ENDPOINT,
149 USB_DIR_OUT | USB_SCO_EP,
150 0x01,
151 0x00, 0x00,
152 0x01,
153
154
155 0x07,
156 USB_DT_ENDPOINT,
157 USB_DIR_IN | USB_SCO_EP,
158 0x01,
159 0x00, 0x00,
160 0x01,
161
162
163 0x09,
164 USB_DT_INTERFACE,
165 0x01,
166 0x01,
167 0x02,
168 0xe0,
169 0x01,
170 0x01,
171 0x00,
172
173
174 0x07,
175 USB_DT_ENDPOINT,
176 USB_DIR_OUT | USB_SCO_EP,
177 0x01,
178 0x09, 0x00,
179 0x01,
180
181
182 0x07,
183 USB_DT_ENDPOINT,
184 USB_DIR_IN | USB_SCO_EP,
185 0x01,
186 0x09, 0x00,
187 0x01,
188
189
190 0x09,
191 USB_DT_INTERFACE,
192 0x01,
193 0x02,
194 0x02,
195 0xe0,
196 0x01,
197 0x01,
198 0x00,
199
200
201 0x07,
202 USB_DT_ENDPOINT,
203 USB_DIR_OUT | USB_SCO_EP,
204 0x01,
205 0x11, 0x00,
206 0x01,
207
208
209 0x07,
210 USB_DT_ENDPOINT,
211 USB_DIR_IN | USB_SCO_EP,
212 0x01,
213 0x11, 0x00,
214 0x01,
215
216
217 0x09,
218 USB_DT_INTERFACE,
219 0x01,
220 0x03,
221 0x02,
222 0xe0,
223 0x01,
224 0x01,
225 0x00,
226
227
228 0x07,
229 USB_DT_ENDPOINT,
230 USB_DIR_OUT | USB_SCO_EP,
231 0x01,
232 0x19, 0x00,
233 0x01,
234
235
236 0x07,
237 USB_DT_ENDPOINT,
238 USB_DIR_IN | USB_SCO_EP,
239 0x01,
240 0x19, 0x00,
241 0x01,
242
243
244 0x09,
245 USB_DT_INTERFACE,
246 0x01,
247 0x04,
248 0x02,
249 0xe0,
250 0x01,
251 0x01,
252 0x00,
253
254
255 0x07,
256 USB_DT_ENDPOINT,
257 USB_DIR_OUT | USB_SCO_EP,
258 0x01,
259 0x21, 0x00,
260 0x01,
261
262
263 0x07,
264 USB_DT_ENDPOINT,
265 USB_DIR_IN | USB_SCO_EP,
266 0x01,
267 0x21, 0x00,
268 0x01,
269
270
271 0x09,
272 USB_DT_INTERFACE,
273 0x01,
274 0x05,
275 0x02,
276 0xe0,
277 0x01,
278 0x01,
279 0x00,
280
281
282 0x07,
283 USB_DT_ENDPOINT,
284 USB_DIR_OUT | USB_SCO_EP,
285 0x01,
286 0x31, 0x00,
287 0x01,
288
289
290 0x07,
291 USB_DT_ENDPOINT,
292 USB_DIR_IN | USB_SCO_EP,
293 0x01,
294 0x31, 0x00,
295 0x01,
296
297
298
299};
300
301static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s *fifo)
302{
303 fifo->dstart = 0;
304 fifo->dlen = 0;
305 fifo->dsize = DFIFO_LEN_MASK + 1;
306 fifo->start = 0;
307 fifo->len = 0;
308}
309
310static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s *fifo,
311 const uint8_t *data, int len)
312{
313 int off = fifo->dstart + fifo->dlen;
314 uint8_t *buf;
315
316 fifo->dlen += len;
317 if (off <= DFIFO_LEN_MASK) {
318 if (off + len > DFIFO_LEN_MASK + 1 &&
319 (fifo->dsize = off + len) > (DFIFO_LEN_MASK + 1) * 2) {
320 fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
321 exit(-1);
322 }
323 buf = fifo->data + off;
324 } else {
325 if (fifo->dlen > fifo->dsize) {
326 fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
327 exit(-1);
328 }
329 buf = fifo->data + off - fifo->dsize;
330 }
331
332 off = (fifo->start + fifo->len ++) & CFIFO_LEN_MASK;
333 fifo->fifo[off].data = memcpy(buf, data, len);
334 fifo->fifo[off].len = len;
335}
336
337static inline int usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s *fifo,
338 USBPacket *p)
339{
340 int len;
341
342 if (likely(!fifo->len))
343 return USB_RET_STALL;
344
345 len = MIN(p->len, fifo->fifo[fifo->start].len);
346 memcpy(p->data, fifo->fifo[fifo->start].data, len);
347 if (len == p->len) {
348 fifo->fifo[fifo->start].len -= len;
349 fifo->fifo[fifo->start].data += len;
350 } else {
351 fifo->start ++;
352 fifo->start &= CFIFO_LEN_MASK;
353 fifo->len --;
354 }
355
356 fifo->dstart += len;
357 fifo->dlen -= len;
358 if (fifo->dstart >= fifo->dsize) {
359 fifo->dstart = 0;
360 fifo->dsize = DFIFO_LEN_MASK + 1;
361 }
362
363 return len;
364}
365
366static inline void usb_bt_fifo_out_enqueue(struct USBBtState *s,
367 struct usb_hci_out_fifo_s *fifo,
368 void (*send)(struct HCIInfo *, const uint8_t *, int),
369 int (*complete)(const uint8_t *, int),
370 const uint8_t *data, int len)
371{
372 if (fifo->len) {
373 memcpy(fifo->data + fifo->len, data, len);
374 fifo->len += len;
375 if (complete(fifo->data, fifo->len)) {
376 send(s->hci, fifo->data, fifo->len);
377 fifo->len = 0;
378 }
379 } else if (complete(data, len))
380 send(s->hci, data, len);
381 else {
382 memcpy(fifo->data, data, len);
383 fifo->len = len;
384 }
385
386
387}
388
389static int usb_bt_hci_cmd_complete(const uint8_t *data, int len)
390{
391 len -= HCI_COMMAND_HDR_SIZE;
392 return len >= 0 &&
393 len >= ((struct hci_command_hdr *) data)->plen;
394}
395
396static int usb_bt_hci_acl_complete(const uint8_t *data, int len)
397{
398 len -= HCI_ACL_HDR_SIZE;
399 return len >= 0 &&
400 len >= le16_to_cpu(((struct hci_acl_hdr *) data)->dlen);
401}
402
403static int usb_bt_hci_sco_complete(const uint8_t *data, int len)
404{
405 len -= HCI_SCO_HDR_SIZE;
406 return len >= 0 &&
407 len >= ((struct hci_sco_hdr *) data)->dlen;
408}
409
410static void usb_bt_handle_reset(USBDevice *dev)
411{
412 struct USBBtState *s = (struct USBBtState *) dev->opaque;
413
414 usb_bt_fifo_reset(&s->evt);
415 usb_bt_fifo_reset(&s->acl);
416 usb_bt_fifo_reset(&s->sco);
417 s->outcmd.len = 0;
418 s->outacl.len = 0;
419 s->outsco.len = 0;
420 s->altsetting = 0;
421}
422
423static int usb_bt_handle_control(USBDevice *dev, int request, int value,
424 int index, int length, uint8_t *data)
425{
426 struct USBBtState *s = (struct USBBtState *) dev->opaque;
427 int ret = 0;
428
429 switch (request) {
430 case DeviceRequest | USB_REQ_GET_STATUS:
431 case InterfaceRequest | USB_REQ_GET_STATUS:
432 case EndpointRequest | USB_REQ_GET_STATUS:
433 data[0] = (1 << USB_DEVICE_SELF_POWERED) |
434 (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
435 data[1] = 0x00;
436 ret = 2;
437 break;
438 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
439 case InterfaceOutRequest | USB_REQ_CLEAR_FEATURE:
440 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
441 if (value == USB_DEVICE_REMOTE_WAKEUP) {
442 dev->remote_wakeup = 0;
443 } else {
444 goto fail;
445 }
446 ret = 0;
447 break;
448 case DeviceOutRequest | USB_REQ_SET_FEATURE:
449 case InterfaceOutRequest | USB_REQ_SET_FEATURE:
450 case EndpointOutRequest | USB_REQ_SET_FEATURE:
451 if (value == USB_DEVICE_REMOTE_WAKEUP) {
452 dev->remote_wakeup = 1;
453 } else {
454 goto fail;
455 }
456 ret = 0;
457 break;
458 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
459 dev->addr = value;
460 ret = 0;
461 break;
462 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
463 switch (value >> 8) {
464 case USB_DT_DEVICE:
465 ret = sizeof(qemu_bt_dev_descriptor);
466 memcpy(data, qemu_bt_dev_descriptor, ret);
467 break;
468 case USB_DT_CONFIG:
469 ret = sizeof(qemu_bt_config_descriptor);
470 memcpy(data, qemu_bt_config_descriptor, ret);
471 break;
472 case USB_DT_STRING:
473 switch(value & 0xff) {
474 case 0:
475
476 data[0] = 4;
477 data[1] = 3;
478 data[2] = 0x09;
479 data[3] = 0x04;
480 ret = 4;
481 break;
482 default:
483 goto fail;
484 }
485 break;
486 default:
487 goto fail;
488 }
489 break;
490 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
491 data[0] = qemu_bt_config_descriptor[0x5];
492 ret = 1;
493 s->config = 0;
494 break;
495 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
496 ret = 0;
497 if (value != qemu_bt_config_descriptor[0x5] && value != 0) {
498 printf("%s: Wrong SET_CONFIGURATION request (%i)\n",
499 __FUNCTION__, value);
500 goto fail;
501 }
502 s->config = 1;
503 usb_bt_fifo_reset(&s->evt);
504 usb_bt_fifo_reset(&s->acl);
505 usb_bt_fifo_reset(&s->sco);
506 break;
507 case InterfaceRequest | USB_REQ_GET_INTERFACE:
508 if (value != 0 || (index & ~1) || length != 1)
509 goto fail;
510 if (index == 1)
511 data[0] = s->altsetting;
512 else
513 data[0] = 0;
514 ret = 1;
515 break;
516 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
517 if ((index & ~1) || length != 0 ||
518 (index == 1 && (value < 0 || value > 4)) ||
519 (index == 0 && value != 0)) {
520 printf("%s: Wrong SET_INTERFACE request (%i, %i)\n",
521 __FUNCTION__, index, value);
522 goto fail;
523 }
524 s->altsetting = value;
525 ret = 0;
526 break;
527 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_DEVICE) << 8):
528 if (s->config)
529 usb_bt_fifo_out_enqueue(s, &s->outcmd, s->hci->cmd_send,
530 usb_bt_hci_cmd_complete, data, length);
531 break;
532 default:
533 fail:
534 ret = USB_RET_STALL;
535 break;
536 }
537 return ret;
538}
539
540static int usb_bt_handle_data(USBDevice *dev, USBPacket *p)
541{
542 struct USBBtState *s = (struct USBBtState *) dev->opaque;
543 int ret = 0;
544
545 if (!s->config)
546 goto fail;
547
548 switch (p->pid) {
549 case USB_TOKEN_IN:
550 switch (p->devep & 0xf) {
551 case USB_EVT_EP:
552 ret = usb_bt_fifo_dequeue(&s->evt, p);
553 break;
554
555 case USB_ACL_EP:
556 ret = usb_bt_fifo_dequeue(&s->acl, p);
557 break;
558
559 case USB_SCO_EP:
560 ret = usb_bt_fifo_dequeue(&s->sco, p);
561 break;
562
563 default:
564 goto fail;
565 }
566 break;
567
568 case USB_TOKEN_OUT:
569 switch (p->devep & 0xf) {
570 case USB_ACL_EP:
571 usb_bt_fifo_out_enqueue(s, &s->outacl, s->hci->acl_send,
572 usb_bt_hci_acl_complete, p->data, p->len);
573 break;
574
575 case USB_SCO_EP:
576 usb_bt_fifo_out_enqueue(s, &s->outsco, s->hci->sco_send,
577 usb_bt_hci_sco_complete, p->data, p->len);
578 break;
579
580 default:
581 goto fail;
582 }
583 break;
584
585 default:
586 fail:
587 ret = USB_RET_STALL;
588 break;
589 }
590
591 return ret;
592}
593
594static void usb_bt_out_hci_packet_event(void *opaque,
595 const uint8_t *data, int len)
596{
597 struct USBBtState *s = (struct USBBtState *) opaque;
598
599 usb_bt_fifo_enqueue(&s->evt, data, len);
600}
601
602static void usb_bt_out_hci_packet_acl(void *opaque,
603 const uint8_t *data, int len)
604{
605 struct USBBtState *s = (struct USBBtState *) opaque;
606
607 usb_bt_fifo_enqueue(&s->acl, data, len);
608}
609
610static void usb_bt_handle_destroy(USBDevice *dev)
611{
612 struct USBBtState *s = (struct USBBtState *) dev->opaque;
613
614 s->hci->opaque = NULL;
615 s->hci->evt_recv = NULL;
616 s->hci->acl_recv = NULL;
617}
618
619static int usb_bt_initfn(USBDevice *dev)
620{
621 struct USBBtState *s = DO_UPCAST(struct USBBtState, dev, dev);
622 s->dev.speed = USB_SPEED_HIGH;
623 return 0;
624}
625
626USBDevice *usb_bt_init(HCIInfo *hci)
627{
628 USBDevice *dev;
629 struct USBBtState *s;
630
631 if (!hci)
632 return NULL;
633 dev = usb_create_simple(NULL , "usb-bt-dongle");
634 s = DO_UPCAST(struct USBBtState, dev, dev);
635 s->dev.opaque = s;
636
637 s->hci = hci;
638 s->hci->opaque = s;
639 s->hci->evt_recv = usb_bt_out_hci_packet_event;
640 s->hci->acl_recv = usb_bt_out_hci_packet_acl;
641
642 usb_bt_handle_reset(&s->dev);
643
644 return dev;
645}
646
647static struct USBDeviceInfo bt_info = {
648 .product_desc = "QEMU BT dongle",
649 .qdev.name = "usb-bt-dongle",
650 .qdev.size = sizeof(struct USBBtState),
651 .init = usb_bt_initfn,
652 .handle_packet = usb_generic_handle_packet,
653 .handle_reset = usb_bt_handle_reset,
654 .handle_control = usb_bt_handle_control,
655 .handle_data = usb_bt_handle_data,
656 .handle_destroy = usb_bt_handle_destroy,
657};
658
659static void usb_bt_register_devices(void)
660{
661 usb_qdev_register(&bt_info);
662}
663device_init(usb_bt_register_devices)
664