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
;
59 static const USBDescStrings desc_strings
= {
60 [STR_MANUFACTURER
] = "QEMU " QEMU_VERSION
,
61 [STR_SERIALNUMBER
] = "1",
64 static const USBDescIface desc_iface_bluetooth
[] = {
66 .bInterfaceNumber
= 0,
68 .bInterfaceClass
= 0xe0, /* Wireless */
69 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
70 .bInterfaceProtocol
= 0x01, /* Bluetooth */
71 .eps
= (USBDescEndpoint
[]) {
73 .bEndpointAddress
= USB_DIR_IN
| USB_EVT_EP
,
74 .bmAttributes
= USB_ENDPOINT_XFER_INT
,
75 .wMaxPacketSize
= 0x10,
79 .bEndpointAddress
= USB_DIR_OUT
| USB_ACL_EP
,
80 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
81 .wMaxPacketSize
= 0x40,
85 .bEndpointAddress
= USB_DIR_IN
| USB_ACL_EP
,
86 .bmAttributes
= USB_ENDPOINT_XFER_BULK
,
87 .wMaxPacketSize
= 0x40,
92 .bInterfaceNumber
= 1,
93 .bAlternateSetting
= 0,
95 .bInterfaceClass
= 0xe0, /* Wireless */
96 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
97 .bInterfaceProtocol
= 0x01, /* Bluetooth */
98 .eps
= (USBDescEndpoint
[]) {
100 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
101 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
106 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
107 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
113 .bInterfaceNumber
= 1,
114 .bAlternateSetting
= 1,
116 .bInterfaceClass
= 0xe0, /* Wireless */
117 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
118 .bInterfaceProtocol
= 0x01, /* Bluetooth */
119 .eps
= (USBDescEndpoint
[]) {
121 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
122 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
123 .wMaxPacketSize
= 0x09,
127 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
128 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
129 .wMaxPacketSize
= 0x09,
134 .bInterfaceNumber
= 1,
135 .bAlternateSetting
= 2,
137 .bInterfaceClass
= 0xe0, /* Wireless */
138 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
139 .bInterfaceProtocol
= 0x01, /* Bluetooth */
140 .eps
= (USBDescEndpoint
[]) {
142 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
143 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
144 .wMaxPacketSize
= 0x11,
148 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
149 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
150 .wMaxPacketSize
= 0x11,
155 .bInterfaceNumber
= 1,
156 .bAlternateSetting
= 3,
158 .bInterfaceClass
= 0xe0, /* Wireless */
159 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
160 .bInterfaceProtocol
= 0x01, /* Bluetooth */
161 .eps
= (USBDescEndpoint
[]) {
163 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
164 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
165 .wMaxPacketSize
= 0x19,
169 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
170 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
171 .wMaxPacketSize
= 0x19,
176 .bInterfaceNumber
= 1,
177 .bAlternateSetting
= 4,
179 .bInterfaceClass
= 0xe0, /* Wireless */
180 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
181 .bInterfaceProtocol
= 0x01, /* Bluetooth */
182 .eps
= (USBDescEndpoint
[]) {
184 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
185 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
186 .wMaxPacketSize
= 0x21,
190 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
191 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
192 .wMaxPacketSize
= 0x21,
197 .bInterfaceNumber
= 1,
198 .bAlternateSetting
= 5,
200 .bInterfaceClass
= 0xe0, /* Wireless */
201 .bInterfaceSubClass
= 0x01, /* Radio Frequency */
202 .bInterfaceProtocol
= 0x01, /* Bluetooth */
203 .eps
= (USBDescEndpoint
[]) {
205 .bEndpointAddress
= USB_DIR_OUT
| USB_SCO_EP
,
206 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
207 .wMaxPacketSize
= 0x31,
211 .bEndpointAddress
= USB_DIR_IN
| USB_SCO_EP
,
212 .bmAttributes
= USB_ENDPOINT_XFER_ISOC
,
213 .wMaxPacketSize
= 0x31,
220 static const USBDescDevice desc_device_bluetooth
= {
222 .bDeviceClass
= 0xe0, /* Wireless */
223 .bDeviceSubClass
= 0x01, /* Radio Frequency */
224 .bDeviceProtocol
= 0x01, /* Bluetooth */
225 .bMaxPacketSize0
= 64,
226 .bNumConfigurations
= 1,
227 .confs
= (USBDescConfig
[]) {
230 .bConfigurationValue
= 1,
231 .bmAttributes
= 0xc0,
233 .nif
= ARRAY_SIZE(desc_iface_bluetooth
),
234 .ifs
= desc_iface_bluetooth
,
239 static const USBDesc desc_bluetooth
= {
244 .iManufacturer
= STR_MANUFACTURER
,
246 .iSerialNumber
= STR_SERIALNUMBER
,
248 .full
= &desc_device_bluetooth
,
252 static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s
*fifo
)
256 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
261 static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s
*fifo
,
262 const uint8_t *data
, int len
)
264 int off
= fifo
->dstart
+ fifo
->dlen
;
268 if (off
<= DFIFO_LEN_MASK
) {
269 if (off
+ len
> DFIFO_LEN_MASK
+ 1 &&
270 (fifo
->dsize
= off
+ len
) > (DFIFO_LEN_MASK
+ 1) * 2) {
271 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
274 buf
= fifo
->data
+ off
;
276 if (fifo
->dlen
> fifo
->dsize
) {
277 fprintf(stderr
, "%s: can't alloc %i bytes\n", __FUNCTION__
, len
);
280 buf
= fifo
->data
+ off
- fifo
->dsize
;
283 off
= (fifo
->start
+ fifo
->len
++) & CFIFO_LEN_MASK
;
284 fifo
->fifo
[off
].data
= memcpy(buf
, data
, len
);
285 fifo
->fifo
[off
].len
= len
;
288 static inline int usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s
*fifo
,
293 if (likely(!fifo
->len
))
294 return USB_RET_STALL
;
296 len
= MIN(p
->iov
.size
, fifo
->fifo
[fifo
->start
].len
);
297 usb_packet_copy(p
, fifo
->fifo
[fifo
->start
].data
, len
);
298 if (len
== p
->iov
.size
) {
299 fifo
->fifo
[fifo
->start
].len
-= len
;
300 fifo
->fifo
[fifo
->start
].data
+= len
;
303 fifo
->start
&= CFIFO_LEN_MASK
;
309 if (fifo
->dstart
>= fifo
->dsize
) {
311 fifo
->dsize
= DFIFO_LEN_MASK
+ 1;
317 static inline void usb_bt_fifo_out_enqueue(struct USBBtState
*s
,
318 struct usb_hci_out_fifo_s
*fifo
,
319 void (*send
)(struct HCIInfo
*, const uint8_t *, int),
320 int (*complete
)(const uint8_t *, int),
323 usb_packet_copy(p
, fifo
->data
+ fifo
->len
, p
->iov
.size
);
324 fifo
->len
+= p
->iov
.size
;
325 if (complete(fifo
->data
, fifo
->len
)) {
326 send(s
->hci
, fifo
->data
, fifo
->len
);
330 /* TODO: do we need to loop? */
333 static int usb_bt_hci_cmd_complete(const uint8_t *data
, int len
)
335 len
-= HCI_COMMAND_HDR_SIZE
;
337 len
>= ((struct hci_command_hdr
*) data
)->plen
;
340 static int usb_bt_hci_acl_complete(const uint8_t *data
, int len
)
342 len
-= HCI_ACL_HDR_SIZE
;
344 len
>= le16_to_cpu(((struct hci_acl_hdr
*) data
)->dlen
);
347 static int usb_bt_hci_sco_complete(const uint8_t *data
, int len
)
349 len
-= HCI_SCO_HDR_SIZE
;
351 len
>= ((struct hci_sco_hdr
*) data
)->dlen
;
354 static void usb_bt_handle_reset(USBDevice
*dev
)
356 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
358 usb_bt_fifo_reset(&s
->evt
);
359 usb_bt_fifo_reset(&s
->acl
);
360 usb_bt_fifo_reset(&s
->sco
);
366 static int usb_bt_handle_control(USBDevice
*dev
, USBPacket
*p
,
367 int request
, int value
, int index
, int length
, uint8_t *data
)
369 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
372 ret
= usb_desc_handle_control(dev
, p
, request
, value
, index
, length
, data
);
375 case DeviceRequest
| USB_REQ_GET_CONFIGURATION
:
378 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
380 usb_bt_fifo_reset(&s
->evt
);
381 usb_bt_fifo_reset(&s
->acl
);
382 usb_bt_fifo_reset(&s
->sco
);
390 case InterfaceRequest
| USB_REQ_GET_STATUS
:
391 case EndpointRequest
| USB_REQ_GET_STATUS
:
396 case InterfaceOutRequest
| USB_REQ_CLEAR_FEATURE
:
397 case EndpointOutRequest
| USB_REQ_CLEAR_FEATURE
:
399 case InterfaceOutRequest
| USB_REQ_SET_FEATURE
:
400 case EndpointOutRequest
| USB_REQ_SET_FEATURE
:
403 case ((USB_DIR_OUT
| USB_TYPE_CLASS
| USB_RECIP_DEVICE
) << 8):
405 usb_bt_fifo_out_enqueue(s
, &s
->outcmd
, s
->hci
->cmd_send
,
406 usb_bt_hci_cmd_complete
, p
);
416 static int usb_bt_handle_data(USBDevice
*dev
, USBPacket
*p
)
418 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
426 switch (p
->devep
& 0xf) {
428 ret
= usb_bt_fifo_dequeue(&s
->evt
, p
);
432 ret
= usb_bt_fifo_dequeue(&s
->acl
, p
);
436 ret
= usb_bt_fifo_dequeue(&s
->sco
, p
);
445 switch (p
->devep
& 0xf) {
447 usb_bt_fifo_out_enqueue(s
, &s
->outacl
, s
->hci
->acl_send
,
448 usb_bt_hci_acl_complete
, p
);
452 usb_bt_fifo_out_enqueue(s
, &s
->outsco
, s
->hci
->sco_send
,
453 usb_bt_hci_sco_complete
, p
);
470 static void usb_bt_out_hci_packet_event(void *opaque
,
471 const uint8_t *data
, int len
)
473 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
475 usb_bt_fifo_enqueue(&s
->evt
, data
, len
);
478 static void usb_bt_out_hci_packet_acl(void *opaque
,
479 const uint8_t *data
, int len
)
481 struct USBBtState
*s
= (struct USBBtState
*) opaque
;
483 usb_bt_fifo_enqueue(&s
->acl
, data
, len
);
486 static void usb_bt_handle_destroy(USBDevice
*dev
)
488 struct USBBtState
*s
= (struct USBBtState
*) dev
->opaque
;
490 s
->hci
->opaque
= NULL
;
491 s
->hci
->evt_recv
= NULL
;
492 s
->hci
->acl_recv
= NULL
;
495 static int usb_bt_initfn(USBDevice
*dev
)
501 USBDevice
*usb_bt_init(HCIInfo
*hci
)
504 struct USBBtState
*s
;
508 dev
= usb_create_simple(NULL
/* FIXME */, "usb-bt-dongle");
512 s
= DO_UPCAST(struct USBBtState
, dev
, dev
);
517 s
->hci
->evt_recv
= usb_bt_out_hci_packet_event
;
518 s
->hci
->acl_recv
= usb_bt_out_hci_packet_acl
;
520 usb_bt_handle_reset(&s
->dev
);
525 static const VMStateDescription vmstate_usb_bt
= {
530 static void usb_bt_class_initfn(ObjectClass
*klass
, void *data
)
532 USBDeviceClass
*uc
= USB_DEVICE_CLASS(klass
);
534 uc
->init
= usb_bt_initfn
;
535 uc
->product_desc
= "QEMU BT dongle";
536 uc
->usb_desc
= &desc_bluetooth
;
537 uc
->handle_packet
= usb_generic_handle_packet
;
538 uc
->handle_reset
= usb_bt_handle_reset
;
539 uc
->handle_control
= usb_bt_handle_control
;
540 uc
->handle_data
= usb_bt_handle_data
;
541 uc
->handle_destroy
= usb_bt_handle_destroy
;
544 static struct DeviceInfo bt_info
= {
545 .name
= "usb-bt-dongle",
546 .size
= sizeof(struct USBBtState
),
547 .vmsd
= &vmstate_usb_bt
,
548 .class_init
= usb_bt_class_initfn
,
551 static void usb_bt_register_devices(void)
553 usb_qdev_register(&bt_info
, NULL
, NULL
);
555 device_init(usb_bt_register_devices
)