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/osdep.h"
22 #include "qemu-common.h"
23 #include "qemu/error-report.h"
26 #include "sysemu/bt.h"
36 #define CFIFO_LEN_MASK 255
37 #define DFIFO_LEN_MASK 4095
38 struct usb_hci_in_fifo_s
{
39 uint8_t data
[(DFIFO_LEN_MASK
+ 1) * 2];
43 } fifo
[CFIFO_LEN_MASK
+ 1];
44 int dstart
, dlen
, dsize
, start
, len
;
47 struct usb_hci_out_fifo_s
{
50 } outcmd
, outacl
, outsco
;
53 #define TYPE_USB_BT "usb-bt-dongle"
54 #define USB_BT(obj) OBJECT_CHECK(struct USBBtState, (obj), TYPE_USB_BT)
65 static const USBDescStrings desc_strings
= {
66 [STR_MANUFACTURER
] = "QEMU",
67 [STR_SERIALNUMBER
] = "1",
70 static const USBDescIface desc_iface_bluetooth
[] = {
72 .bInterfaceNumber
= 0,
74 .bInterfaceClass
= 0xe0, /* Wireless */
75 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
76 .bInterfaceProtocol
= 0x01, /* Bluetooth */
77 .eps
= (USBDescEndpoint
[]) {
79 .bEndpointAddress
= USB_DIR_IN
| USB_EVT_EP
,
80 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
81 .wMaxPacketSize
= 0x10,
85 .bEndpointAddress
= USB_DIR_OUT
| USB_ACL_EP
,
86 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
87 .wMaxPacketSize
= 0x40,
91 .bEndpointAddress
= USB_DIR_IN
| USB_ACL_EP
,
92 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
93 .wMaxPacketSize
= 0x40,
98 .bInterfaceNumber
= 1,
99 .bAlternateSetting
= 0,
101 .bInterfaceClass
= 0xe0, /* Wireless */
102 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
103 .bInterfaceProtocol
= 0x01, /* Bluetooth */
104 .eps
= (USBDescEndpoint
[]) {
106 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
107 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
112 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
113 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
119 .bInterfaceNumber
= 1,
120 .bAlternateSetting
= 1,
122 .bInterfaceClass
= 0xe0, /* Wireless */
123 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
124 .bInterfaceProtocol
= 0x01, /* Bluetooth */
125 .eps
= (USBDescEndpoint
[]) {
127 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
128 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
129 .wMaxPacketSize
= 0x09,
133 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
134 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
135 .wMaxPacketSize
= 0x09,
140 .bInterfaceNumber
= 1,
141 .bAlternateSetting
= 2,
143 .bInterfaceClass
= 0xe0, /* Wireless */
144 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
145 .bInterfaceProtocol
= 0x01, /* Bluetooth */
146 .eps
= (USBDescEndpoint
[]) {
148 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
149 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
150 .wMaxPacketSize
= 0x11,
154 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
155 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
156 .wMaxPacketSize
= 0x11,
161 .bInterfaceNumber
= 1,
162 .bAlternateSetting
= 3,
164 .bInterfaceClass
= 0xe0, /* Wireless */
165 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
166 .bInterfaceProtocol
= 0x01, /* Bluetooth */
167 .eps
= (USBDescEndpoint
[]) {
169 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
170 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
171 .wMaxPacketSize
= 0x19,
175 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
176 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
177 .wMaxPacketSize
= 0x19,
182 .bInterfaceNumber
= 1,
183 .bAlternateSetting
= 4,
185 .bInterfaceClass
= 0xe0, /* Wireless */
186 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
187 .bInterfaceProtocol
= 0x01, /* Bluetooth */
188 .eps
= (USBDescEndpoint
[]) {
190 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
191 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
192 .wMaxPacketSize
= 0x21,
196 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
197 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
198 .wMaxPacketSize
= 0x21,
203 .bInterfaceNumber
= 1,
204 .bAlternateSetting
= 5,
206 .bInterfaceClass
= 0xe0, /* Wireless */
207 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
208 .bInterfaceProtocol
= 0x01, /* Bluetooth */
209 .eps
= (USBDescEndpoint
[]) {
211 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
212 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
213 .wMaxPacketSize
= 0x31,
217 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
218 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
219 .wMaxPacketSize
= 0x31,
226 static const USBDescDevice desc_device_bluetooth
= {
228 .bDeviceClass
= 0xe0, /* Wireless */
229 .bDeviceSubClass
= 0x01, /* Radio Frequency */
230 .bDeviceProtocol
= 0x01, /* Bluetooth */
231 .bMaxPacketSize0
= 64,
232 .bNumConfigurations
= 1,
233 .confs
= (USBDescConfig
[]) {
236 .bConfigurationValue
= 1,
237 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
239 .nif
= ARRAY_SIZE(desc_iface_bluetooth
),
240 .ifs
= desc_iface_bluetooth
,
245 static const USBDesc desc_bluetooth
= {
250 .iManufacturer
= STR_MANUFACTURER
,
252 .iSerialNumber
= STR_SERIALNUMBER
,
254 .full
= &desc_device_bluetooth
,
258 static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s
*fifo
)
262 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
267 static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s
*fifo
,
268 const uint8_t *data
, int len
)
270 int off
= fifo
->dstart
+ fifo
->dlen
;
274 if (off
<= DFIFO_LEN_MASK
) {
275 if (off
+ len
> DFIFO_LEN_MASK
+ 1 &&
276 (fifo
->dsize
= off
+ len
) > (DFIFO_LEN_MASK
+ 1) * 2) {
277 fprintf(stderr
, "%s: can't alloc %i bytes\n", __func__
, len
);
280 buf
= fifo
->data
+ off
;
282 if (fifo
->dlen
> fifo
->dsize
) {
283 fprintf(stderr
, "%s: can't alloc %i bytes\n", __func__
, len
);
286 buf
= fifo
->data
+ off
- fifo
->dsize
;
289 off
= (fifo
->start
+ fifo
->len
++) & CFIFO_LEN_MASK
;
290 fifo
->fifo
[off
].data
= memcpy(buf
, data
, len
);
291 fifo
->fifo
[off
].len
= len
;
294 static inline void usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s
*fifo
,
299 assert(fifo
->len
!= 0);
301 len
= MIN(p
->iov
.size
, fifo
->fifo
[fifo
->start
].len
);
302 usb_packet_copy(p
, fifo
->fifo
[fifo
->start
].data
, len
);
303 if (len
== p
->iov
.size
) {
304 fifo
->fifo
[fifo
->start
].len
-= len
;
305 fifo
->fifo
[fifo
->start
].data
+= len
;
308 fifo
->start
&= CFIFO_LEN_MASK
;
314 if (fifo
->dstart
>= fifo
->dsize
) {
316 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
320 static inline void usb_bt_fifo_out_enqueue(struct USBBtState
*s
,
321 struct usb_hci_out_fifo_s
*fifo
,
322 void (*send
)(struct HCIInfo
*, const uint8_t *, int),
323 int (*complete
)(const uint8_t *, int),
326 usb_packet_copy(p
, fifo
->data
+ fifo
->len
, p
->iov
.size
);
327 fifo
->len
+= p
->iov
.size
;
328 if (complete(fifo
->data
, fifo
->len
)) {
329 send(s
->hci
, fifo
->data
, fifo
->len
);
333 /* TODO: do we need to loop? */
336 static int usb_bt_hci_cmd_complete(const uint8_t *data
, int len
)
338 len
-= HCI_COMMAND_HDR_SIZE
;
340 len
>= ((struct hci_command_hdr
*) data
)->plen
;
343 static int usb_bt_hci_acl_complete(const uint8_t *data
, int len
)
345 len
-= HCI_ACL_HDR_SIZE
;
347 len
>= le16_to_cpu(((struct hci_acl_hdr
*) data
)->dlen
);
350 static int usb_bt_hci_sco_complete(const uint8_t *data
, int len
)
352 len
-= HCI_SCO_HDR_SIZE
;
354 len
>= ((struct hci_sco_hdr
*) data
)->dlen
;
357 static void usb_bt_handle_reset(USBDevice
*dev
)
359 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
361 usb_bt_fifo_reset(&s
->evt
);
362 usb_bt_fifo_reset(&s
->acl
);
363 usb_bt_fifo_reset(&s
->sco
);
369 static void usb_bt_handle_control(USBDevice
*dev
, USBPacket
*p
,
370 int request
, int value
, int index
, int length
, uint8_t *data
)
372 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
375 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
378 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
381 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
383 usb_bt_fifo_reset(&s
->evt
);
384 usb_bt_fifo_reset(&s
->acl
);
385 usb_bt_fifo_reset(&s
->sco
);
392 case InterfaceRequest
| USB_REQ_GET_STATUS
:
393 case EndpointRequest
| USB_REQ_GET_STATUS
:
396 p
->actual_length
= 2;
398 case InterfaceOutRequest
| USB_REQ_CLEAR_FEATURE
:
399 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
401 case InterfaceOutRequest
| USB_REQ_SET_FEATURE
:
402 case EndpointOutRequest
| USB_REQ_SET_FEATURE
:
405 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_DEVICE
) << 8):
407 usb_bt_fifo_out_enqueue(s
, &s
->outcmd
, s
->hci
->cmd_send
,
408 usb_bt_hci_cmd_complete
, p
);
412 p
->status
= USB_RET_STALL
;
417 static void usb_bt_handle_data(USBDevice
*dev
, USBPacket
*p
)
419 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
428 if (s
->evt
.len
== 0) {
429 p
->status
= USB_RET_NAK
;
432 usb_bt_fifo_dequeue(&s
->evt
, p
);
436 if (s
->evt
.len
== 0) {
437 p
->status
= USB_RET_STALL
;
440 usb_bt_fifo_dequeue(&s
->acl
, p
);
444 if (s
->evt
.len
== 0) {
445 p
->status
= USB_RET_STALL
;
448 usb_bt_fifo_dequeue(&s
->sco
, p
);
459 usb_bt_fifo_out_enqueue(s
, &s
->outacl
, s
->hci
->acl_send
,
460 usb_bt_hci_acl_complete
, p
);
464 usb_bt_fifo_out_enqueue(s
, &s
->outsco
, s
->hci
->sco_send
,
465 usb_bt_hci_sco_complete
, p
);
475 p
->status
= USB_RET_STALL
;
480 static void usb_bt_out_hci_packet_event(void *opaque
,
481 const uint8_t *data
, int len
)
483 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
485 if (s
->evt
.len
== 0) {
486 usb_wakeup(s
->intr
, 0);
488 usb_bt_fifo_enqueue(&s
->evt
, data
, len
);
491 static void usb_bt_out_hci_packet_acl(void *opaque
,
492 const uint8_t *data
, int len
)
494 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
496 usb_bt_fifo_enqueue(&s
->acl
, data
, len
);
499 static void usb_bt_unrealize(USBDevice
*dev
, Error
**errp
)
501 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
503 s
->hci
->opaque
= NULL
;
504 s
->hci
->evt_recv
= NULL
;
505 s
->hci
->acl_recv
= NULL
;
508 static void usb_bt_realize(USBDevice
*dev
, Error
**errp
)
510 struct USBBtState
*s
= USB_BT(dev
);
512 usb_desc_create_serial(dev
);
516 s
->hci
= bt_new_hci(qemu_find_bt_vlan(0));
519 s
->hci
->evt_recv
= usb_bt_out_hci_packet_event
;
520 s
->hci
->acl_recv
= usb_bt_out_hci_packet_acl
;
521 usb_bt_handle_reset(&s
->dev
);
522 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, USB_EVT_EP
);
525 static USBDevice
*usb_bt_init(USBBus
*bus
, const char *cmdline
)
528 struct USBBtState
*s
;
530 const char *name
= TYPE_USB_BT
;
533 hci
= hci_init(cmdline
);
535 hci
= bt_new_hci(qemu_find_bt_vlan(0));
540 dev
= usb_create(bus
, name
);
546 static const VMStateDescription vmstate_usb_bt
= {
551 static void usb_bt_class_initfn(ObjectClass
*klass
, void *data
)
553 DeviceClass
*dc
= DEVICE_CLASS(klass
);
554 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
556 uc
->realize
= usb_bt_realize
;
557 uc
->product_desc
= "QEMU BT dongle";
558 uc
->usb_desc
= &desc_bluetooth
;
559 uc
->handle_reset
= usb_bt_handle_reset
;
560 uc
->handle_control
= usb_bt_handle_control
;
561 uc
->handle_data
= usb_bt_handle_data
;
562 uc
->unrealize
= usb_bt_unrealize
;
563 dc
->vmsd
= &vmstate_usb_bt
;
564 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
567 static const TypeInfo bt_info
= {
569 .parent
= TYPE_USB_DEVICE
,
570 .instance_size
= sizeof(struct USBBtState
),
571 .class_init
= usb_bt_class_initfn
,
574 static void usb_bt_register_types(void)
576 type_register_static(&bt_info
);
577 usb_legacy_register(TYPE_USB_BT
, "bt", usb_bt_init
);
580 type_init(usb_bt_register_types
)