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
;
52 #define TYPE_USB_BT "usb-bt-dongle"
53 #define USB_BT(obj) OBJECT_CHECK(struct USBBtState, (obj), TYPE_USB_BT)
64 static const USBDescStrings desc_strings
= {
65 [STR_MANUFACTURER
] = "QEMU",
66 [STR_SERIALNUMBER
] = "1",
69 static const USBDescIface desc_iface_bluetooth
[] = {
71 .bInterfaceNumber
= 0,
73 .bInterfaceClass
= 0xe0, /* Wireless */
74 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
75 .bInterfaceProtocol
= 0x01, /* Bluetooth */
76 .eps
= (USBDescEndpoint
[]) {
78 .bEndpointAddress
= USB_DIR_IN
| USB_EVT_EP
,
79 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
80 .wMaxPacketSize
= 0x10,
84 .bEndpointAddress
= USB_DIR_OUT
| USB_ACL_EP
,
85 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
86 .wMaxPacketSize
= 0x40,
90 .bEndpointAddress
= USB_DIR_IN
| USB_ACL_EP
,
91 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
92 .wMaxPacketSize
= 0x40,
97 .bInterfaceNumber
= 1,
98 .bAlternateSetting
= 0,
100 .bInterfaceClass
= 0xe0, /* Wireless */
101 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
102 .bInterfaceProtocol
= 0x01, /* Bluetooth */
103 .eps
= (USBDescEndpoint
[]) {
105 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
106 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
111 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
112 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
118 .bInterfaceNumber
= 1,
119 .bAlternateSetting
= 1,
121 .bInterfaceClass
= 0xe0, /* Wireless */
122 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
123 .bInterfaceProtocol
= 0x01, /* Bluetooth */
124 .eps
= (USBDescEndpoint
[]) {
126 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
127 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
128 .wMaxPacketSize
= 0x09,
132 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
133 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
134 .wMaxPacketSize
= 0x09,
139 .bInterfaceNumber
= 1,
140 .bAlternateSetting
= 2,
142 .bInterfaceClass
= 0xe0, /* Wireless */
143 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
144 .bInterfaceProtocol
= 0x01, /* Bluetooth */
145 .eps
= (USBDescEndpoint
[]) {
147 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
148 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
149 .wMaxPacketSize
= 0x11,
153 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
154 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
155 .wMaxPacketSize
= 0x11,
160 .bInterfaceNumber
= 1,
161 .bAlternateSetting
= 3,
163 .bInterfaceClass
= 0xe0, /* Wireless */
164 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
165 .bInterfaceProtocol
= 0x01, /* Bluetooth */
166 .eps
= (USBDescEndpoint
[]) {
168 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
169 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
170 .wMaxPacketSize
= 0x19,
174 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
175 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
176 .wMaxPacketSize
= 0x19,
181 .bInterfaceNumber
= 1,
182 .bAlternateSetting
= 4,
184 .bInterfaceClass
= 0xe0, /* Wireless */
185 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
186 .bInterfaceProtocol
= 0x01, /* Bluetooth */
187 .eps
= (USBDescEndpoint
[]) {
189 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
190 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
191 .wMaxPacketSize
= 0x21,
195 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
196 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
197 .wMaxPacketSize
= 0x21,
202 .bInterfaceNumber
= 1,
203 .bAlternateSetting
= 5,
205 .bInterfaceClass
= 0xe0, /* Wireless */
206 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
207 .bInterfaceProtocol
= 0x01, /* Bluetooth */
208 .eps
= (USBDescEndpoint
[]) {
210 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
211 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
212 .wMaxPacketSize
= 0x31,
216 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
217 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
218 .wMaxPacketSize
= 0x31,
225 static const USBDescDevice desc_device_bluetooth
= {
227 .bDeviceClass
= 0xe0, /* Wireless */
228 .bDeviceSubClass
= 0x01, /* Radio Frequency */
229 .bDeviceProtocol
= 0x01, /* Bluetooth */
230 .bMaxPacketSize0
= 64,
231 .bNumConfigurations
= 1,
232 .confs
= (USBDescConfig
[]) {
235 .bConfigurationValue
= 1,
236 .bmAttributes
= USB_CFG_ATT_ONE
| USB_CFG_ATT_SELFPOWER
,
238 .nif
= ARRAY_SIZE(desc_iface_bluetooth
),
239 .ifs
= desc_iface_bluetooth
,
244 static const USBDesc desc_bluetooth
= {
249 .iManufacturer
= STR_MANUFACTURER
,
251 .iSerialNumber
= STR_SERIALNUMBER
,
253 .full
= &desc_device_bluetooth
,
257 static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s
*fifo
)
261 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
266 static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s
*fifo
,
267 const uint8_t *data
, int len
)
269 int off
= fifo
->dstart
+ fifo
->dlen
;
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
);
279 buf
= fifo
->data
+ off
;
281 if (fifo
->dlen
> fifo
->dsize
) {
282 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
285 buf
= fifo
->data
+ off
- fifo
->dsize
;
288 off
= (fifo
->start
+ fifo
->len
++) & CFIFO_LEN_MASK
;
289 fifo
->fifo
[off
].data
= memcpy(buf
, data
, len
);
290 fifo
->fifo
[off
].len
= len
;
293 static inline void usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s
*fifo
,
298 assert(fifo
->len
!= 0);
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
) {
303 fifo
->fifo
[fifo
->start
].len
-= len
;
304 fifo
->fifo
[fifo
->start
].data
+= len
;
307 fifo
->start
&= CFIFO_LEN_MASK
;
313 if (fifo
->dstart
>= fifo
->dsize
) {
315 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
319 static inline void usb_bt_fifo_out_enqueue(struct USBBtState
*s
,
320 struct usb_hci_out_fifo_s
*fifo
,
321 void (*send
)(struct HCIInfo
*, const uint8_t *, int),
322 int (*complete
)(const uint8_t *, int),
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
);
332 /* TODO: do we need to loop? */
335 static int usb_bt_hci_cmd_complete(const uint8_t *data
, int len
)
337 len
-= HCI_COMMAND_HDR_SIZE
;
339 len
>= ((struct hci_command_hdr
*) data
)->plen
;
342 static int usb_bt_hci_acl_complete(const uint8_t *data
, int len
)
344 len
-= HCI_ACL_HDR_SIZE
;
346 len
>= le16_to_cpu(((struct hci_acl_hdr
*) data
)->dlen
);
349 static int usb_bt_hci_sco_complete(const uint8_t *data
, int len
)
351 len
-= HCI_SCO_HDR_SIZE
;
353 len
>= ((struct hci_sco_hdr
*) data
)->dlen
;
356 static void usb_bt_handle_reset(USBDevice
*dev
)
358 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
360 usb_bt_fifo_reset(&s
->evt
);
361 usb_bt_fifo_reset(&s
->acl
);
362 usb_bt_fifo_reset(&s
->sco
);
368 static void usb_bt_handle_control(USBDevice
*dev
, USBPacket
*p
,
369 int request
, int value
, int index
, int length
, uint8_t *data
)
371 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
374 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
377 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
380 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
382 usb_bt_fifo_reset(&s
->evt
);
383 usb_bt_fifo_reset(&s
->acl
);
384 usb_bt_fifo_reset(&s
->sco
);
391 case InterfaceRequest
| USB_REQ_GET_STATUS
:
392 case EndpointRequest
| USB_REQ_GET_STATUS
:
395 p
->actual_length
= 2;
397 case InterfaceOutRequest
| USB_REQ_CLEAR_FEATURE
:
398 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
400 case InterfaceOutRequest
| USB_REQ_SET_FEATURE
:
401 case EndpointOutRequest
| USB_REQ_SET_FEATURE
:
404 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_DEVICE
) << 8):
406 usb_bt_fifo_out_enqueue(s
, &s
->outcmd
, s
->hci
->cmd_send
,
407 usb_bt_hci_cmd_complete
, p
);
411 p
->status
= USB_RET_STALL
;
416 static void usb_bt_handle_data(USBDevice
*dev
, USBPacket
*p
)
418 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
427 if (s
->evt
.len
== 0) {
428 p
->status
= USB_RET_NAK
;
431 usb_bt_fifo_dequeue(&s
->evt
, p
);
435 if (s
->evt
.len
== 0) {
436 p
->status
= USB_RET_STALL
;
439 usb_bt_fifo_dequeue(&s
->acl
, p
);
443 if (s
->evt
.len
== 0) {
444 p
->status
= USB_RET_STALL
;
447 usb_bt_fifo_dequeue(&s
->sco
, p
);
458 usb_bt_fifo_out_enqueue(s
, &s
->outacl
, s
->hci
->acl_send
,
459 usb_bt_hci_acl_complete
, p
);
463 usb_bt_fifo_out_enqueue(s
, &s
->outsco
, s
->hci
->sco_send
,
464 usb_bt_hci_sco_complete
, p
);
474 p
->status
= USB_RET_STALL
;
479 static void usb_bt_out_hci_packet_event(void *opaque
,
480 const uint8_t *data
, int len
)
482 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
484 if (s
->evt
.len
== 0) {
485 usb_wakeup(s
->intr
, 0);
487 usb_bt_fifo_enqueue(&s
->evt
, data
, len
);
490 static void usb_bt_out_hci_packet_acl(void *opaque
,
491 const uint8_t *data
, int len
)
493 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
495 usb_bt_fifo_enqueue(&s
->acl
, data
, len
);
498 static void usb_bt_handle_destroy(USBDevice
*dev
)
500 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
502 s
->hci
->opaque
= NULL
;
503 s
->hci
->evt_recv
= NULL
;
504 s
->hci
->acl_recv
= NULL
;
507 static void usb_bt_realize(USBDevice
*dev
, Error
**errp
)
509 struct USBBtState
*s
= USB_BT(dev
);
511 usb_desc_create_serial(dev
);
515 s
->hci
= bt_new_hci(qemu_find_bt_vlan(0));
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
);
521 s
->intr
= usb_ep_get(dev
, USB_TOKEN_IN
, USB_EVT_EP
);
524 static USBDevice
*usb_bt_init(USBBus
*bus
, const char *cmdline
)
527 struct USBBtState
*s
;
529 const char *name
= TYPE_USB_BT
;
532 hci
= hci_init(cmdline
);
534 hci
= bt_new_hci(qemu_find_bt_vlan(0));
539 dev
= usb_create(bus
, name
);
545 static const VMStateDescription vmstate_usb_bt
= {
550 static void usb_bt_class_initfn(ObjectClass
*klass
, void *data
)
552 DeviceClass
*dc
= DEVICE_CLASS(klass
);
553 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
555 uc
->realize
= usb_bt_realize
;
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
);
566 static const TypeInfo bt_info
= {
568 .parent
= TYPE_USB_DEVICE
,
569 .instance_size
= sizeof(struct USBBtState
),
570 .class_init
= usb_bt_class_initfn
,
573 static void usb_bt_register_types(void)
575 type_register_static(&bt_info
);
576 usb_legacy_register(TYPE_USB_BT
, "bt", usb_bt_init
);
579 type_init(usb_bt_register_types
)