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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 #include "qemu-common.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
;
56 static const uint8_t qemu_bt_dev_descriptor
[] = {
57 0x12, /* u8 bLength; */
58 USB_DT_DEVICE
, /* u8 bDescriptorType; Device */
59 0x10, 0x01, /* u16 bcdUSB; v1.10 */
61 0xe0, /* u8 bDeviceClass; Wireless */
62 0x01, /* u8 bDeviceSubClass; Radio Frequency */
63 0x01, /* u8 bDeviceProtocol; Bluetooth */
64 0x40, /* u8 bMaxPacketSize0; 64 Bytes */
66 0x12, 0x0a, /* u16 idVendor; */
67 0x01, 0x00, /* u16 idProduct; Bluetooth Dongle (HCI mode) */
68 0x58, 0x19, /* u16 bcdDevice; (some devices have 0x48, 0x02) */
70 0x00, /* u8 iManufacturer; */
71 0x00, /* u8 iProduct; */
72 0x00, /* u8 iSerialNumber; */
73 0x01, /* u8 bNumConfigurations; */
76 static const uint8_t qemu_bt_config_descriptor
[] = {
77 /* one configuration */
78 0x09, /* u8 bLength; */
79 USB_DT_CONFIG
, /* u8 bDescriptorType; */
80 0xb1, 0x00, /* u16 wTotalLength; */
81 0x02, /* u8 bNumInterfaces; (2) */
82 0x01, /* u8 bConfigurationValue; */
83 0x00, /* u8 iConfiguration; */
84 0xc0, /* u8 bmAttributes;
89 0x00, /* u8 MaxPower; */
92 * USB 2.0, single TT organization (mandatory):
93 * one interface, protocol 0
95 * USB 2.0, multiple TT organization (optional):
96 * two interfaces, protocols 1 (like single TT)
97 * and 2 (multiple TT mode) ... config is
103 0x09, /* u8 if_bLength; */
104 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
105 0x00, /* u8 if_bInterfaceNumber; */
106 0x00, /* u8 if_bAlternateSetting; */
107 0x03, /* u8 if_bNumEndpoints; */
108 0xe0, /* u8 if_bInterfaceClass; Wireless */
109 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
110 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
111 0x00, /* u8 if_iInterface; */
114 0x07, /* u8 ep_bLength; */
115 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
116 USB_DIR_IN
| USB_EVT_EP
, /* u8 ep_bEndpointAddress; */
117 0x03, /* u8 ep_bmAttributes; Interrupt */
118 0x10, 0x00, /* u16 ep_wMaxPacketSize; */
119 0x02, /* u8 ep_bInterval; */
122 0x07, /* u8 ep_bLength; */
123 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
124 USB_DIR_OUT
| USB_ACL_EP
, /* u8 ep_bEndpointAddress; */
125 0x02, /* u8 ep_bmAttributes; Bulk */
126 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
127 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
130 0x07, /* u8 ep_bLength; */
131 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
132 USB_DIR_IN
| USB_ACL_EP
, /* u8 ep_bEndpointAddress; */
133 0x02, /* u8 ep_bmAttributes; Bulk */
134 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
135 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
137 /* interface two setting one */
138 0x09, /* u8 if_bLength; */
139 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
140 0x01, /* u8 if_bInterfaceNumber; */
141 0x00, /* u8 if_bAlternateSetting; */
142 0x02, /* u8 if_bNumEndpoints; */
143 0xe0, /* u8 if_bInterfaceClass; Wireless */
144 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
145 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
146 0x00, /* u8 if_iInterface; */
149 0x07, /* u8 ep_bLength; */
150 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
151 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
152 0x01, /* u8 ep_bmAttributes; Isochronous */
153 0x00, 0x00, /* u16 ep_wMaxPacketSize; */
154 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
157 0x07, /* u8 ep_bLength; */
158 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
159 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
160 0x01, /* u8 ep_bmAttributes; Isochronous */
161 0x00, 0x00, /* u16 ep_wMaxPacketSize; */
162 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
164 /* interface two setting two */
165 0x09, /* u8 if_bLength; */
166 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
167 0x01, /* u8 if_bInterfaceNumber; */
168 0x01, /* u8 if_bAlternateSetting; */
169 0x02, /* u8 if_bNumEndpoints; */
170 0xe0, /* u8 if_bInterfaceClass; Wireless */
171 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
172 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
173 0x00, /* u8 if_iInterface; */
176 0x07, /* u8 ep_bLength; */
177 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
178 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
179 0x01, /* u8 ep_bmAttributes; Isochronous */
180 0x09, 0x00, /* u16 ep_wMaxPacketSize; */
181 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
184 0x07, /* u8 ep_bLength; */
185 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
186 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
187 0x01, /* u8 ep_bmAttributes; Isochronous */
188 0x09, 0x00, /* u16 ep_wMaxPacketSize; */
189 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
191 /* interface two setting three */
192 0x09, /* u8 if_bLength; */
193 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
194 0x01, /* u8 if_bInterfaceNumber; */
195 0x02, /* u8 if_bAlternateSetting; */
196 0x02, /* u8 if_bNumEndpoints; */
197 0xe0, /* u8 if_bInterfaceClass; Wireless */
198 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
199 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
200 0x00, /* u8 if_iInterface; */
203 0x07, /* u8 ep_bLength; */
204 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
205 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
206 0x01, /* u8 ep_bmAttributes; Isochronous */
207 0x11, 0x00, /* u16 ep_wMaxPacketSize; */
208 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
211 0x07, /* u8 ep_bLength; */
212 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
213 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
214 0x01, /* u8 ep_bmAttributes; Isochronous */
215 0x11, 0x00, /* u16 ep_wMaxPacketSize; */
216 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
218 /* interface two setting four */
219 0x09, /* u8 if_bLength; */
220 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
221 0x01, /* u8 if_bInterfaceNumber; */
222 0x03, /* u8 if_bAlternateSetting; */
223 0x02, /* u8 if_bNumEndpoints; */
224 0xe0, /* u8 if_bInterfaceClass; Wireless */
225 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
226 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
227 0x00, /* u8 if_iInterface; */
230 0x07, /* u8 ep_bLength; */
231 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
232 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
233 0x01, /* u8 ep_bmAttributes; Isochronous */
234 0x19, 0x00, /* u16 ep_wMaxPacketSize; */
235 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
238 0x07, /* u8 ep_bLength; */
239 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
240 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
241 0x01, /* u8 ep_bmAttributes; Isochronous */
242 0x19, 0x00, /* u16 ep_wMaxPacketSize; */
243 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
245 /* interface two setting five */
246 0x09, /* u8 if_bLength; */
247 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
248 0x01, /* u8 if_bInterfaceNumber; */
249 0x04, /* u8 if_bAlternateSetting; */
250 0x02, /* u8 if_bNumEndpoints; */
251 0xe0, /* u8 if_bInterfaceClass; Wireless */
252 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
253 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
254 0x00, /* u8 if_iInterface; */
257 0x07, /* u8 ep_bLength; */
258 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
259 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
260 0x01, /* u8 ep_bmAttributes; Isochronous */
261 0x21, 0x00, /* u16 ep_wMaxPacketSize; */
262 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
265 0x07, /* u8 ep_bLength; */
266 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
267 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
268 0x01, /* u8 ep_bmAttributes; Isochronous */
269 0x21, 0x00, /* u16 ep_wMaxPacketSize; */
270 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
272 /* interface two setting six */
273 0x09, /* u8 if_bLength; */
274 USB_DT_INTERFACE
, /* u8 if_bDescriptorType; */
275 0x01, /* u8 if_bInterfaceNumber; */
276 0x05, /* u8 if_bAlternateSetting; */
277 0x02, /* u8 if_bNumEndpoints; */
278 0xe0, /* u8 if_bInterfaceClass; Wireless */
279 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
280 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
281 0x00, /* u8 if_iInterface; */
284 0x07, /* u8 ep_bLength; */
285 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
286 USB_DIR_OUT
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
287 0x01, /* u8 ep_bmAttributes; Isochronous */
288 0x31, 0x00, /* u16 ep_wMaxPacketSize; */
289 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
292 0x07, /* u8 ep_bLength; */
293 USB_DT_ENDPOINT
, /* u8 ep_bDescriptorType; */
294 USB_DIR_IN
| USB_SCO_EP
, /* u8 ep_bEndpointAddress; */
295 0x01, /* u8 ep_bmAttributes; Isochronous */
296 0x31, 0x00, /* u16 ep_wMaxPacketSize; */
297 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
299 /* If implemented, the DFU interface descriptor goes here with no
300 * endpoints or alternative settings. */
303 static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s
*fifo
)
307 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
312 static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s
*fifo
,
313 const uint8_t *data
, int len
)
315 int off
= fifo
->dstart
+ fifo
->dlen
;
319 if (off
<= DFIFO_LEN_MASK
) {
320 if (off
+ len
> DFIFO_LEN_MASK
+ 1 &&
321 (fifo
->dsize
= off
+ len
) > (DFIFO_LEN_MASK
+ 1) * 2) {
322 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
325 buf
= fifo
->data
+ off
;
327 if (fifo
->dlen
> fifo
->dsize
) {
328 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
331 buf
= fifo
->data
+ off
- fifo
->dsize
;
334 off
= (fifo
->start
+ fifo
->len
++) & CFIFO_LEN_MASK
;
335 fifo
->fifo
[off
].data
= memcpy(buf
, data
, len
);
336 fifo
->fifo
[off
].len
= len
;
339 static inline int usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s
*fifo
,
344 if (likely(!fifo
->len
))
345 return USB_RET_STALL
;
347 len
= MIN(p
->len
, fifo
->fifo
[fifo
->start
].len
);
348 memcpy(p
->data
, fifo
->fifo
[fifo
->start
].data
, len
);
350 fifo
->fifo
[fifo
->start
].len
-= len
;
351 fifo
->fifo
[fifo
->start
].data
+= len
;
354 fifo
->start
&= CFIFO_LEN_MASK
;
360 if (fifo
->dstart
>= fifo
->dsize
) {
362 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
368 static void inline usb_bt_fifo_out_enqueue(struct USBBtState
*s
,
369 struct usb_hci_out_fifo_s
*fifo
,
370 void (*send
)(struct HCIInfo
*, const uint8_t *, int),
371 int (*complete
)(const uint8_t *, int),
372 const uint8_t *data
, int len
)
375 memcpy(fifo
->data
+ fifo
->len
, data
, len
);
377 if (complete(fifo
->data
, fifo
->len
)) {
378 send(s
->hci
, fifo
->data
, fifo
->len
);
381 } else if (complete(data
, len
))
382 send(s
->hci
, data
, len
);
384 memcpy(fifo
->data
, data
, len
);
388 /* TODO: do we need to loop? */
391 static int usb_bt_hci_cmd_complete(const uint8_t *data
, int len
)
393 len
-= HCI_COMMAND_HDR_SIZE
;
395 len
>= ((struct hci_command_hdr
*) data
)->plen
;
398 static int usb_bt_hci_acl_complete(const uint8_t *data
, int len
)
400 len
-= HCI_ACL_HDR_SIZE
;
402 len
>= le16_to_cpu(((struct hci_acl_hdr
*) data
)->dlen
);
405 static int usb_bt_hci_sco_complete(const uint8_t *data
, int len
)
407 len
-= HCI_SCO_HDR_SIZE
;
409 len
>= ((struct hci_sco_hdr
*) data
)->dlen
;
412 static void usb_bt_handle_reset(USBDevice
*dev
)
414 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
416 usb_bt_fifo_reset(&s
->evt
);
417 usb_bt_fifo_reset(&s
->acl
);
418 usb_bt_fifo_reset(&s
->sco
);
425 static int usb_bt_handle_control(USBDevice
*dev
, int request
, int value
,
426 int index
, int length
, uint8_t *data
)
428 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
432 case DeviceRequest
| USB_REQ_GET_STATUS
:
433 case InterfaceRequest
| USB_REQ_GET_STATUS
:
434 case EndpointRequest
| USB_REQ_GET_STATUS
:
435 data
[0] = (1 << USB_DEVICE_SELF_POWERED
) |
436 (dev
->remote_wakeup
<< USB_DEVICE_REMOTE_WAKEUP
);
440 case DeviceOutRequest
| USB_REQ_CLEAR_FEATURE
:
441 case InterfaceOutRequest
| USB_REQ_CLEAR_FEATURE
:
442 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
443 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
444 dev
->remote_wakeup
= 0;
450 case DeviceOutRequest
| USB_REQ_SET_FEATURE
:
451 case InterfaceOutRequest
| USB_REQ_SET_FEATURE
:
452 case EndpointOutRequest
| USB_REQ_SET_FEATURE
:
453 if (value
== USB_DEVICE_REMOTE_WAKEUP
) {
454 dev
->remote_wakeup
= 1;
460 case DeviceOutRequest
| USB_REQ_SET_ADDRESS
:
464 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
465 switch (value
>> 8) {
467 ret
= sizeof(qemu_bt_dev_descriptor
);
468 memcpy(data
, qemu_bt_dev_descriptor
, ret
);
471 ret
= sizeof(qemu_bt_config_descriptor
);
472 memcpy(data
, qemu_bt_config_descriptor
, ret
);
475 switch(value
& 0xff) {
492 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
493 data
[0] = qemu_bt_config_descriptor
[0x5];
497 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
499 if (value
!= qemu_bt_config_descriptor
[0x5] && value
!= 0) {
500 printf("%s: Wrong SET_CONFIGURATION request (%i)\n",
501 __FUNCTION__
, value
);
505 usb_bt_fifo_reset(&s
->evt
);
506 usb_bt_fifo_reset(&s
->acl
);
507 usb_bt_fifo_reset(&s
->sco
);
509 case InterfaceRequest
| USB_REQ_GET_INTERFACE
:
510 if (value
!= 0 || (index
& ~1) || length
!= 1)
513 data
[0] = s
->altsetting
;
518 case InterfaceOutRequest
| USB_REQ_SET_INTERFACE
:
519 if ((index
& ~1) || length
!= 0 ||
520 (index
== 1 && (value
< 0 || value
> 4)) ||
521 (index
== 0 && value
!= 0)) {
522 printf("%s: Wrong SET_INTERFACE request (%i, %i)\n",
523 __FUNCTION__
, index
, value
);
526 s
->altsetting
= value
;
529 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_DEVICE
) << 8):
531 usb_bt_fifo_out_enqueue(s
, &s
->outcmd
, s
->hci
->cmd_send
,
532 usb_bt_hci_cmd_complete
, data
, length
);
542 static int usb_bt_handle_data(USBDevice
*dev
, USBPacket
*p
)
544 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
552 switch (p
->devep
& 0xf) {
554 ret
= usb_bt_fifo_dequeue(&s
->evt
, p
);
558 ret
= usb_bt_fifo_dequeue(&s
->acl
, p
);
562 ret
= usb_bt_fifo_dequeue(&s
->sco
, p
);
571 switch (p
->devep
& 0xf) {
573 usb_bt_fifo_out_enqueue(s
, &s
->outacl
, s
->hci
->acl_send
,
574 usb_bt_hci_acl_complete
, p
->data
, p
->len
);
578 usb_bt_fifo_out_enqueue(s
, &s
->outsco
, s
->hci
->sco_send
,
579 usb_bt_hci_sco_complete
, p
->data
, p
->len
);
596 static void usb_bt_out_hci_packet_event(void *opaque
,
597 const uint8_t *data
, int len
)
599 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
601 usb_bt_fifo_enqueue(&s
->evt
, data
, len
);
604 static void usb_bt_out_hci_packet_acl(void *opaque
,
605 const uint8_t *data
, int len
)
607 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
609 usb_bt_fifo_enqueue(&s
->acl
, data
, len
);
612 static void usb_bt_handle_destroy(USBDevice
*dev
)
614 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
617 s
->hci
->evt_recv
= 0;
618 s
->hci
->acl_recv
= 0;
622 USBDevice
*usb_bt_init(HCIInfo
*hci
)
624 struct USBBtState
*s
;
628 s
= qemu_mallocz(sizeof(struct USBBtState
));
632 s
->dev
.speed
= USB_SPEED_HIGH
;
633 s
->dev
.handle_packet
= usb_generic_handle_packet
;
634 pstrcpy(s
->dev
.devname
, sizeof(s
->dev
.devname
), "QEMU BT dongle");
636 s
->dev
.handle_reset
= usb_bt_handle_reset
;
637 s
->dev
.handle_control
= usb_bt_handle_control
;
638 s
->dev
.handle_data
= usb_bt_handle_data
;
639 s
->dev
.handle_destroy
= usb_bt_handle_destroy
;
643 s
->hci
->evt_recv
= usb_bt_out_hci_packet_event
;
644 s
->hci
->acl_recv
= usb_bt_out_hci_packet_acl
;
646 usb_bt_handle_reset(&s
->dev
);