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"
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];
40 } fifo
[CFIFO_LEN_MASK
+ 1];
41 int dstart
, dlen
, dsize
, start
, len
;
44 struct usb_hci_out_fifo_s
{
47 } outcmd
, outacl
, outsco
;
54 static const uint8_t qemu_bt_dev_descriptor
[] = {
55 0x12, /* u8 bLength; */
56 USB_DT_DEVICE
, /* u8 bDescriptorType; Device */
57 0x10, 0x01, /* u16 bcdUSB; v1.10 */
59 0xe0, /* u8 bDeviceClass; Wireless */
60 0x01, /* u8 bDeviceSubClass; Radio Frequency */
61 0x01, /* u8 bDeviceProtocol; Bluetooth */
62 0x40, /* u8 bMaxPacketSize0; 64 Bytes */
64 0x12, 0x0a, /* u16 idVendor; */
65 0x01, 0x00, /* u16 idProduct; Bluetooth Dongle (HCI mode) */
66 0x58, 0x19, /* u16 bcdDevice; (some devices have 0x48, 0x02) */
68 0x00, /* u8 iManufacturer; */
69 0x00, /* u8 iProduct; */
70 0x00, /* u8 iSerialNumber; */
71 0x01, /* u8 bNumConfigurations; */
74 static const uint8_t qemu_bt_config_descriptor
[] = {
75 /* one configuration */
76 0x09, /* u8 bLength; */
77 USB_DT_CONFIG
, /* u8 bDescriptorType; */
78 0xb1, 0x00, /* u16 wTotalLength; */
79 0x02, /* u8 bNumInterfaces; (2) */
80 0x01, /* u8 bConfigurationValue; */
81 0x00, /* u8 iConfiguration; */
82 0xc0, /* u8 bmAttributes;
87 0x00, /* u8 MaxPower; */
90 * USB 2.0, single TT organization (mandatory):
91 * one interface, protocol 0
93 * USB 2.0, multiple TT organization (optional):
94 * two interfaces, protocols 1 (like single TT)
95 * and 2 (multiple TT mode) ... config is
101 0x09, /* u8 if_bLength; */
102 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
103 0x00, /* u8 if_bInterfaceNumber; */
104 0x00, /* u8 if_bAlternateSetting; */
105 0x03, /* u8 if_bNumEndpoints; */
106 0xe0, /* u8 if_bInterfaceClass; Wireless */
107 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
108 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
109 0x00, /* u8 if_iInterface; */
112 0x07, /* u8 ep_bLength; */
113 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
114 USB_DIR_IN
| USB_EVT_EP
, /* u8 ep_bEndpointAddress; */
115 0x03, /* u8 ep_bmAttributes; Interrupt */
116 0x10, 0x00, /* u16 ep_wMaxPacketSize; */
117 0x02, /* u8 ep_bInterval; */
120 0x07, /* u8 ep_bLength; */
121 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
122 USB_DIR_OUT
| USB_ACL_EP
, /* u8 ep_bEndpointAddress; */
123 0x02, /* u8 ep_bmAttributes; Bulk */
124 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
125 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
128 0x07, /* u8 ep_bLength; */
129 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
130 USB_DIR_IN
| USB_ACL_EP
, /* u8 ep_bEndpointAddress; */
131 0x02, /* u8 ep_bmAttributes; Bulk */
132 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
133 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
135 /* interface two setting one */
136 0x09, /* u8 if_bLength; */
137 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
138 0x01, /* u8 if_bInterfaceNumber; */
139 0x00, /* u8 if_bAlternateSetting; */
140 0x02, /* u8 if_bNumEndpoints; */
141 0xe0, /* u8 if_bInterfaceClass; Wireless */
142 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
143 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
144 0x00, /* u8 if_iInterface; */
147 0x07, /* u8 ep_bLength; */
148 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
149 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
150 0x01, /* u8 ep_bmAttributes; Isochronous */
151 0x00, 0x00, /* u16 ep_wMaxPacketSize; */
152 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
155 0x07, /* u8 ep_bLength; */
156 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
157 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
158 0x01, /* u8 ep_bmAttributes; Isochronous */
159 0x00, 0x00, /* u16 ep_wMaxPacketSize; */
160 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
162 /* interface two setting two */
163 0x09, /* u8 if_bLength; */
164 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
165 0x01, /* u8 if_bInterfaceNumber; */
166 0x01, /* u8 if_bAlternateSetting; */
167 0x02, /* u8 if_bNumEndpoints; */
168 0xe0, /* u8 if_bInterfaceClass; Wireless */
169 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
170 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
171 0x00, /* u8 if_iInterface; */
174 0x07, /* u8 ep_bLength; */
175 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
176 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
177 0x01, /* u8 ep_bmAttributes; Isochronous */
178 0x09, 0x00, /* u16 ep_wMaxPacketSize; */
179 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
182 0x07, /* u8 ep_bLength; */
183 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
184 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
185 0x01, /* u8 ep_bmAttributes; Isochronous */
186 0x09, 0x00, /* u16 ep_wMaxPacketSize; */
187 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
189 /* interface two setting three */
190 0x09, /* u8 if_bLength; */
191 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
192 0x01, /* u8 if_bInterfaceNumber; */
193 0x02, /* u8 if_bAlternateSetting; */
194 0x02, /* u8 if_bNumEndpoints; */
195 0xe0, /* u8 if_bInterfaceClass; Wireless */
196 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
197 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
198 0x00, /* u8 if_iInterface; */
201 0x07, /* u8 ep_bLength; */
202 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
203 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
204 0x01, /* u8 ep_bmAttributes; Isochronous */
205 0x11, 0x00, /* u16 ep_wMaxPacketSize; */
206 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
209 0x07, /* u8 ep_bLength; */
210 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
211 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
212 0x01, /* u8 ep_bmAttributes; Isochronous */
213 0x11, 0x00, /* u16 ep_wMaxPacketSize; */
214 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
216 /* interface two setting four */
217 0x09, /* u8 if_bLength; */
218 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
219 0x01, /* u8 if_bInterfaceNumber; */
220 0x03, /* u8 if_bAlternateSetting; */
221 0x02, /* u8 if_bNumEndpoints; */
222 0xe0, /* u8 if_bInterfaceClass; Wireless */
223 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
224 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
225 0x00, /* u8 if_iInterface; */
228 0x07, /* u8 ep_bLength; */
229 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
230 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
231 0x01, /* u8 ep_bmAttributes; Isochronous */
232 0x19, 0x00, /* u16 ep_wMaxPacketSize; */
233 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
236 0x07, /* u8 ep_bLength; */
237 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
238 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
239 0x01, /* u8 ep_bmAttributes; Isochronous */
240 0x19, 0x00, /* u16 ep_wMaxPacketSize; */
241 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
243 /* interface two setting five */
244 0x09, /* u8 if_bLength; */
245 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
246 0x01, /* u8 if_bInterfaceNumber; */
247 0x04, /* u8 if_bAlternateSetting; */
248 0x02, /* u8 if_bNumEndpoints; */
249 0xe0, /* u8 if_bInterfaceClass; Wireless */
250 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
251 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
252 0x00, /* u8 if_iInterface; */
255 0x07, /* u8 ep_bLength; */
256 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
257 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
258 0x01, /* u8 ep_bmAttributes; Isochronous */
259 0x21, 0x00, /* u16 ep_wMaxPacketSize; */
260 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
263 0x07, /* u8 ep_bLength; */
264 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
265 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
266 0x01, /* u8 ep_bmAttributes; Isochronous */
267 0x21, 0x00, /* u16 ep_wMaxPacketSize; */
268 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
270 /* interface two setting six */
271 0x09, /* u8 if_bLength; */
272 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
273 0x01, /* u8 if_bInterfaceNumber; */
274 0x05, /* u8 if_bAlternateSetting; */
275 0x02, /* u8 if_bNumEndpoints; */
276 0xe0, /* u8 if_bInterfaceClass; Wireless */
277 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
278 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
279 0x00, /* u8 if_iInterface; */
282 0x07, /* u8 ep_bLength; */
283 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
284 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
285 0x01, /* u8 ep_bmAttributes; Isochronous */
286 0x31, 0x00, /* u16 ep_wMaxPacketSize; */
287 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
290 0x07, /* u8 ep_bLength; */
291 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
292 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
293 0x01, /* u8 ep_bmAttributes; Isochronous */
294 0x31, 0x00, /* u16 ep_wMaxPacketSize; */
295 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
297 /* If implemented, the DFU interface descriptor goes here with no
298 * endpoints or alternative settings. */
301 static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s
*fifo
)
305 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
310 static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s
*fifo
,
311 const uint8_t *data
, int len
)
313 int off
= fifo
->dstart
+ fifo
->dlen
;
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
);
323 buf
= fifo
->data
+ off
;
325 if (fifo
->dlen
> fifo
->dsize
) {
326 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
329 buf
= fifo
->data
+ off
- fifo
->dsize
;
332 off
= (fifo
->start
+ fifo
->len
++) & CFIFO_LEN_MASK
;
333 fifo
->fifo
[off
].data
= memcpy(buf
, data
, len
);
334 fifo
->fifo
[off
].len
= len
;
337 static inline int usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s
*fifo
,
342 if (likely(!fifo
->len
))
343 return USB_RET_STALL
;
345 len
= MIN(p
->len
, fifo
->fifo
[fifo
->start
].len
);
346 memcpy(p
->data
, fifo
->fifo
[fifo
->start
].data
, len
);
348 fifo
->fifo
[fifo
->start
].len
-= len
;
349 fifo
->fifo
[fifo
->start
].data
+= len
;
352 fifo
->start
&= CFIFO_LEN_MASK
;
358 if (fifo
->dstart
>= fifo
->dsize
) {
360 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
366 static 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
)
373 memcpy(fifo
->data
+ fifo
->len
, data
, len
);
375 if (complete(fifo
->data
, fifo
->len
)) {
376 send(s
->hci
, fifo
->data
, fifo
->len
);
379 } else if (complete(data
, len
))
380 send(s
->hci
, data
, len
);
382 memcpy(fifo
->data
, data
, len
);
386 /* TODO: do we need to loop? */
389 static int usb_bt_hci_cmd_complete(const uint8_t *data
, int len
)
391 len
-= HCI_COMMAND_HDR_SIZE
;
393 len
>= ((struct hci_command_hdr
*) data
)->plen
;
396 static int usb_bt_hci_acl_complete(const uint8_t *data
, int len
)
398 len
-= HCI_ACL_HDR_SIZE
;
400 len
>= le16_to_cpu(((struct hci_acl_hdr
*) data
)->dlen
);
403 static int usb_bt_hci_sco_complete(const uint8_t *data
, int len
)
405 len
-= HCI_SCO_HDR_SIZE
;
407 len
>= ((struct hci_sco_hdr
*) data
)->dlen
;
410 static void usb_bt_handle_reset(USBDevice
*dev
)
412 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
414 usb_bt_fifo_reset(&s
->evt
);
415 usb_bt_fifo_reset(&s
->acl
);
416 usb_bt_fifo_reset(&s
->sco
);
423 static int usb_bt_handle_control(USBDevice
*dev
, int request
, int value
,
424 int index
, int length
, uint8_t *data
)
426 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
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
);
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;
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;
458 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
462 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
463 switch (value
>> 8) {
465 ret
= sizeof(qemu_bt_dev_descriptor
);
466 memcpy(data
, qemu_bt_dev_descriptor
, ret
);
469 ret
= sizeof(qemu_bt_config_descriptor
);
470 memcpy(data
, qemu_bt_config_descriptor
, ret
);
473 switch(value
& 0xff) {
490 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
491 data
[0] = qemu_bt_config_descriptor
[0x5];
495 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
497 if (value
!= qemu_bt_config_descriptor
[0x5] && value
!= 0) {
498 printf("%s: Wrong SET_CONFIGURATION request (%i)\n",
499 __FUNCTION__
, value
);
503 usb_bt_fifo_reset(&s
->evt
);
504 usb_bt_fifo_reset(&s
->acl
);
505 usb_bt_fifo_reset(&s
->sco
);
507 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
508 if (value
!= 0 || (index
& ~1) || length
!= 1)
511 data
[0] = s
->altsetting
;
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
);
524 s
->altsetting
= value
;
527 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_DEVICE
) << 8):
529 usb_bt_fifo_out_enqueue(s
, &s
->outcmd
, s
->hci
->cmd_send
,
530 usb_bt_hci_cmd_complete
, data
, length
);
540 static int usb_bt_handle_data(USBDevice
*dev
, USBPacket
*p
)
542 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
550 switch (p
->devep
& 0xf) {
552 ret
= usb_bt_fifo_dequeue(&s
->evt
, p
);
556 ret
= usb_bt_fifo_dequeue(&s
->acl
, p
);
560 ret
= usb_bt_fifo_dequeue(&s
->sco
, p
);
569 switch (p
->devep
& 0xf) {
571 usb_bt_fifo_out_enqueue(s
, &s
->outacl
, s
->hci
->acl_send
,
572 usb_bt_hci_acl_complete
, p
->data
, p
->len
);
576 usb_bt_fifo_out_enqueue(s
, &s
->outsco
, s
->hci
->sco_send
,
577 usb_bt_hci_sco_complete
, p
->data
, p
->len
);
594 static void usb_bt_out_hci_packet_event(void *opaque
,
595 const uint8_t *data
, int len
)
597 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
599 usb_bt_fifo_enqueue(&s
->evt
, data
, len
);
602 static void usb_bt_out_hci_packet_acl(void *opaque
,
603 const uint8_t *data
, int len
)
605 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
607 usb_bt_fifo_enqueue(&s
->acl
, data
, len
);
610 static void usb_bt_handle_destroy(USBDevice
*dev
)
612 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
614 s
->hci
->opaque
= NULL
;
615 s
->hci
->evt_recv
= NULL
;
616 s
->hci
->acl_recv
= NULL
;
620 static int usb_bt_initfn(USBDevice
*dev
)
622 struct USBBtState
*s
= DO_UPCAST(struct USBBtState
, dev
, dev
);
623 s
->dev
.speed
= USB_SPEED_HIGH
;
627 USBDevice
*usb_bt_init(HCIInfo
*hci
)
630 struct USBBtState
*s
;
634 dev
= usb_create_simple(NULL
/* FIXME */, "QEMU BT dongle");
635 s
= DO_UPCAST(struct USBBtState
, dev
, dev
);
640 s
->hci
->evt_recv
= usb_bt_out_hci_packet_event
;
641 s
->hci
->acl_recv
= usb_bt_out_hci_packet_acl
;
643 usb_bt_handle_reset(&s
->dev
);
648 static struct USBDeviceInfo bt_info
= {
649 .qdev
.name
= "QEMU 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
,
659 static void usb_bt_register_devices(void)
661 usb_qdev_register(&bt_info
);
663 device_init(usb_bt_register_devices
)