2 * QEMU Bluetooth HCI USB Transport Layer v1.0
4 * Copyright (C) 2007 OpenMoko, Inc.
5 * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
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.
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu-common.h"
22 #include "qemu/error-report.h"
24 #include "hw/usb/desc.h"
25 #include "sysemu/bt.h"
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];
42 } fifo
[CFIFO_LEN_MASK
+ 1];
43 int dstart
, dlen
, dsize
, start
, len
;
46 struct usb_hci_out_fifo_s
{
49 } outcmd
, outacl
, outsco
;
61 static const USBDescStrings desc_strings
= {
62 [STR_MANUFACTURER
] = "QEMU",
63 [STR_SERIALNUMBER
] = "1",
66 static const USBDescIface desc_iface_bluetooth
[] = {
68 .bInterfaceNumber
= 0,
70 .bInterfaceClass
= 0xe0, /* Wireless */
71 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
72 .bInterfaceProtocol
= 0x01, /* Bluetooth */
73 .eps
= (USBDescEndpoint
[]) {
75 .bEndpointAddress
= USB_DIR_IN
| USB_EVT_EP
,
76 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
77 .wMaxPacketSize
= 0x10,
81 .bEndpointAddress
= USB_DIR_OUT
| USB_ACL_EP
,
82 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
83 .wMaxPacketSize
= 0x40,
87 .bEndpointAddress
= USB_DIR_IN
| USB_ACL_EP
,
88 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
89 .wMaxPacketSize
= 0x40,
94 .bInterfaceNumber
= 1,
95 .bAlternateSetting
= 0,
97 .bInterfaceClass
= 0xe0, /* Wireless */
98 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
99 .bInterfaceProtocol
= 0x01, /* Bluetooth */
100 .eps
= (USBDescEndpoint
[]) {
102 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
103 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
108 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
109 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
115 .bInterfaceNumber
= 1,
116 .bAlternateSetting
= 1,
118 .bInterfaceClass
= 0xe0, /* Wireless */
119 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
120 .bInterfaceProtocol
= 0x01, /* Bluetooth */
121 .eps
= (USBDescEndpoint
[]) {
123 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
124 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
125 .wMaxPacketSize
= 0x09,
129 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
130 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
131 .wMaxPacketSize
= 0x09,
136 .bInterfaceNumber
= 1,
137 .bAlternateSetting
= 2,
139 .bInterfaceClass
= 0xe0, /* Wireless */
140 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
141 .bInterfaceProtocol
= 0x01, /* Bluetooth */
142 .eps
= (USBDescEndpoint
[]) {
144 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
145 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
146 .wMaxPacketSize
= 0x11,
150 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
151 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
152 .wMaxPacketSize
= 0x11,
157 .bInterfaceNumber
= 1,
158 .bAlternateSetting
= 3,
160 .bInterfaceClass
= 0xe0, /* Wireless */
161 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
162 .bInterfaceProtocol
= 0x01, /* Bluetooth */
163 .eps
= (USBDescEndpoint
[]) {
165 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
166 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
167 .wMaxPacketSize
= 0x19,
171 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
172 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
173 .wMaxPacketSize
= 0x19,
178 .bInterfaceNumber
= 1,
179 .bAlternateSetting
= 4,
181 .bInterfaceClass
= 0xe0, /* Wireless */
182 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
183 .bInterfaceProtocol
= 0x01, /* Bluetooth */
184 .eps
= (USBDescEndpoint
[]) {
186 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
187 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
188 .wMaxPacketSize
= 0x21,
192 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
193 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
194 .wMaxPacketSize
= 0x21,
199 .bInterfaceNumber
= 1,
200 .bAlternateSetting
= 5,
202 .bInterfaceClass
= 0xe0, /* Wireless */
203 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
204 .bInterfaceProtocol
= 0x01, /* Bluetooth */
205 .eps
= (USBDescEndpoint
[]) {
207 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
208 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
209 .wMaxPacketSize
= 0x31,
213 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
214 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
215 .wMaxPacketSize
= 0x31,
222 static const USBDescDevice desc_device_bluetooth
= {
224 .bDeviceClass
= 0xe0, /* Wireless */
225 .bDeviceSubClass
= 0x01, /* Radio Frequency */
226 .bDeviceProtocol
= 0x01, /* Bluetooth */
227 .bMaxPacketSize0
= 64,
228 .bNumConfigurations
= 1,
229 .confs
= (USBDescConfig
[]) {
232 .bConfigurationValue
= 1,
233 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
235 .nif
= ARRAY_SIZE(desc_iface_bluetooth
),
236 .ifs
= desc_iface_bluetooth
,
241 static const USBDesc desc_bluetooth
= {
246 .iManufacturer
= STR_MANUFACTURER
,
248 .iSerialNumber
= STR_SERIALNUMBER
,
250 .full
= &desc_device_bluetooth
,
254 static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s
*fifo
)
258 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
263 static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s
*fifo
,
264 const uint8_t *data
, int len
)
266 int off
= fifo
->dstart
+ fifo
->dlen
;
270 if (off
<= DFIFO_LEN_MASK
) {
271 if (off
+ len
> DFIFO_LEN_MASK
+ 1 &&
272 (fifo
->dsize
= off
+ len
) > (DFIFO_LEN_MASK
+ 1) * 2) {
273 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
276 buf
= fifo
->data
+ off
;
278 if (fifo
->dlen
> fifo
->dsize
) {
279 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
282 buf
= fifo
->data
+ off
- fifo
->dsize
;
285 off
= (fifo
->start
+ fifo
->len
++) & CFIFO_LEN_MASK
;
286 fifo
->fifo
[off
].data
= memcpy(buf
, data
, len
);
287 fifo
->fifo
[off
].len
= len
;
290 static inline void usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s
*fifo
,
295 assert(fifo
->len
!= 0);
297 len
= MIN(p
->iov
.size
, fifo
->fifo
[fifo
->start
].len
);
298 usb_packet_copy(p
, fifo
->fifo
[fifo
->start
].data
, len
);
299 if (len
== p
->iov
.size
) {
300 fifo
->fifo
[fifo
->start
].len
-= len
;
301 fifo
->fifo
[fifo
->start
].data
+= len
;
304 fifo
->start
&= CFIFO_LEN_MASK
;
310 if (fifo
->dstart
>= fifo
->dsize
) {
312 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
316 static inline void usb_bt_fifo_out_enqueue(struct USBBtState
*s
,
317 struct usb_hci_out_fifo_s
*fifo
,
318 void (*send
)(struct HCIInfo
*, const uint8_t *, int),
319 int (*complete
)(const uint8_t *, int),
322 usb_packet_copy(p
, fifo
->data
+ fifo
->len
, p
->iov
.size
);
323 fifo
->len
+= p
->iov
.size
;
324 if (complete(fifo
->data
, fifo
->len
)) {
325 send(s
->hci
, fifo
->data
, fifo
->len
);
329 /* TODO: do we need to loop? */
332 static int usb_bt_hci_cmd_complete(const uint8_t *data
, int len
)
334 len
-= HCI_COMMAND_HDR_SIZE
;
336 len
>= ((struct hci_command_hdr
*) data
)->plen
;
339 static int usb_bt_hci_acl_complete(const uint8_t *data
, int len
)
341 len
-= HCI_ACL_HDR_SIZE
;
343 len
>= le16_to_cpu(((struct hci_acl_hdr
*) data
)->dlen
);
346 static int usb_bt_hci_sco_complete(const uint8_t *data
, int len
)
348 len
-= HCI_SCO_HDR_SIZE
;
350 len
>= ((struct hci_sco_hdr
*) data
)->dlen
;
353 static void usb_bt_handle_reset(USBDevice
*dev
)
355 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
357 usb_bt_fifo_reset(&s
->evt
);
358 usb_bt_fifo_reset(&s
->acl
);
359 usb_bt_fifo_reset(&s
->sco
);
365 static void usb_bt_handle_control(USBDevice
*dev
, USBPacket
*p
,
366 int request
, int value
, int index
, int length
, uint8_t *data
)
368 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
371 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
374 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
377 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
379 usb_bt_fifo_reset(&s
->evt
);
380 usb_bt_fifo_reset(&s
->acl
);
381 usb_bt_fifo_reset(&s
->sco
);
388 case InterfaceRequest
| USB_REQ_GET_STATUS
:
389 case EndpointRequest
| USB_REQ_GET_STATUS
:
392 p
->actual_length
= 2;
394 case InterfaceOutRequest
| USB_REQ_CLEAR_FEATURE
:
395 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
397 case InterfaceOutRequest
| USB_REQ_SET_FEATURE
:
398 case EndpointOutRequest
| USB_REQ_SET_FEATURE
:
401 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_DEVICE
) << 8):
403 usb_bt_fifo_out_enqueue(s
, &s
->outcmd
, s
->hci
->cmd_send
,
404 usb_bt_hci_cmd_complete
, p
);
408 p
->status
= USB_RET_STALL
;
413 static void usb_bt_handle_data(USBDevice
*dev
, USBPacket
*p
)
415 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
424 if (s
->evt
.len
== 0) {
425 p
->status
= USB_RET_NAK
;
428 usb_bt_fifo_dequeue(&s
->evt
, p
);
432 if (s
->evt
.len
== 0) {
433 p
->status
= USB_RET_STALL
;
436 usb_bt_fifo_dequeue(&s
->acl
, p
);
440 if (s
->evt
.len
== 0) {
441 p
->status
= USB_RET_STALL
;
444 usb_bt_fifo_dequeue(&s
->sco
, p
);
455 usb_bt_fifo_out_enqueue(s
, &s
->outacl
, s
->hci
->acl_send
,
456 usb_bt_hci_acl_complete
, p
);
460 usb_bt_fifo_out_enqueue(s
, &s
->outsco
, s
->hci
->sco_send
,
461 usb_bt_hci_sco_complete
, p
);
471 p
->status
= USB_RET_STALL
;
476 static void usb_bt_out_hci_packet_event(void *opaque
,
477 const uint8_t *data
, int len
)
479 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
481 if (s
->evt
.len
== 0) {
482 usb_wakeup(s
->intr
, 0);
484 usb_bt_fifo_enqueue(&s
->evt
, data
, len
);
487 static void usb_bt_out_hci_packet_acl(void *opaque
,
488 const uint8_t *data
, int len
)
490 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
492 usb_bt_fifo_enqueue(&s
->acl
, data
, len
);
495 static void usb_bt_handle_destroy(USBDevice
*dev
)
497 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
499 s
->hci
->opaque
= NULL
;
500 s
->hci
->evt_recv
= NULL
;
501 s
->hci
->acl_recv
= NULL
;
504 static void usb_bt_realize(USBDevice
*dev
, Error
**errp
)
506 struct USBBtState
*s
= DO_UPCAST(struct USBBtState
, dev
, dev
);
508 usb_desc_create_serial(dev
);
512 s
->hci
= bt_new_hci(qemu_find_bt_vlan(0));
515 s
->hci
->evt_recv
= usb_bt_out_hci_packet_event
;
516 s
->hci
->acl_recv
= usb_bt_out_hci_packet_acl
;
517 usb_bt_handle_reset(&s
->dev
);
518 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, USB_EVT_EP
);
521 static USBDevice
*usb_bt_init(USBBus
*bus
, const char *cmdline
)
524 struct USBBtState
*s
;
526 const char *name
= "usb-bt-dongle";
529 hci
= hci_init(cmdline
);
531 hci
= bt_new_hci(qemu_find_bt_vlan(0));
536 dev
= usb_create(bus
, name
);
538 error_report("Failed to create USB device '%s'", name
);
541 s
= DO_UPCAST(struct USBBtState
, dev
, dev
);
543 if (qdev_init(&dev
->qdev
) < 0) {
544 error_report("Failed to initialize USB device '%s'", name
);
551 static const VMStateDescription vmstate_usb_bt
= {
556 static void usb_bt_class_initfn(ObjectClass
*klass
, void *data
)
558 DeviceClass
*dc
= DEVICE_CLASS(klass
);
559 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
561 uc
->realize
= usb_bt_realize
;
562 uc
->product_desc
= "QEMU BT dongle";
563 uc
->usb_desc
= &desc_bluetooth
;
564 uc
->handle_reset
= usb_bt_handle_reset
;
565 uc
->handle_control
= usb_bt_handle_control
;
566 uc
->handle_data
= usb_bt_handle_data
;
567 uc
->handle_destroy
= usb_bt_handle_destroy
;
568 dc
->vmsd
= &vmstate_usb_bt
;
569 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
572 static const TypeInfo bt_info
= {
573 .name
= "usb-bt-dongle",
574 .parent
= TYPE_USB_DEVICE
,
575 .instance_size
= sizeof(struct USBBtState
),
576 .class_init
= usb_bt_class_initfn
,
579 static void usb_bt_register_types(void)
581 type_register_static(&bt_info
);
582 usb_legacy_register("usb-bt-dongle", "bt", usb_bt_init
);
585 type_init(usb_bt_register_types
)