qxl-logger: add timestamp to command log
[qemu.git] / hw / usb-bt.c
blobbaae4876eab956ae49f244089dada01405c8da3d
1 /*
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 "usb.h"
23 #include "usb-desc.h"
24 #include "net.h"
25 #include "bt.h"
27 struct USBBtState {
28 USBDevice dev;
29 struct HCIInfo *hci;
31 int altsetting;
32 int config;
34 #define CFIFO_LEN_MASK 255
35 #define DFIFO_LEN_MASK 4095
36 struct usb_hci_in_fifo_s {
37 uint8_t data[(DFIFO_LEN_MASK + 1) * 2];
38 struct {
39 uint8_t *data;
40 int len;
41 } fifo[CFIFO_LEN_MASK + 1];
42 int dstart, dlen, dsize, start, len;
43 } evt, acl, sco;
45 struct usb_hci_out_fifo_s {
46 uint8_t data[4096];
47 int len;
48 } outcmd, outacl, outsco;
51 #define USB_EVT_EP 1
52 #define USB_ACL_EP 2
53 #define USB_SCO_EP 3
55 enum {
56 STR_MANUFACTURER = 1,
57 STR_SERIALNUMBER,
60 static const USBDescStrings desc_strings = {
61 [STR_MANUFACTURER] = "QEMU " QEMU_VERSION,
62 [STR_SERIALNUMBER] = "1",
65 static const USBDescIface desc_iface_bluetooth[] = {
67 .bInterfaceNumber = 0,
68 .bNumEndpoints = 3,
69 .bInterfaceClass = 0xe0, /* Wireless */
70 .bInterfaceSubClass = 0x01, /* Radio Frequency */
71 .bInterfaceProtocol = 0x01, /* Bluetooth */
72 .eps = (USBDescEndpoint[]) {
74 .bEndpointAddress = USB_DIR_IN | USB_EVT_EP,
75 .bmAttributes = USB_ENDPOINT_XFER_INT,
76 .wMaxPacketSize = 0x10,
77 .bInterval = 0x02,
80 .bEndpointAddress = USB_DIR_OUT | USB_ACL_EP,
81 .bmAttributes = USB_ENDPOINT_XFER_BULK,
82 .wMaxPacketSize = 0x40,
83 .bInterval = 0x0a,
86 .bEndpointAddress = USB_DIR_IN | USB_ACL_EP,
87 .bmAttributes = USB_ENDPOINT_XFER_BULK,
88 .wMaxPacketSize = 0x40,
89 .bInterval = 0x0a,
92 },{
93 .bInterfaceNumber = 1,
94 .bAlternateSetting = 0,
95 .bNumEndpoints = 2,
96 .bInterfaceClass = 0xe0, /* Wireless */
97 .bInterfaceSubClass = 0x01, /* Radio Frequency */
98 .bInterfaceProtocol = 0x01, /* Bluetooth */
99 .eps = (USBDescEndpoint[]) {
101 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
102 .bmAttributes = USB_ENDPOINT_XFER_INT,
103 .wMaxPacketSize = 0,
104 .bInterval = 0x01,
107 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
108 .bmAttributes = USB_ENDPOINT_XFER_INT,
109 .wMaxPacketSize = 0,
110 .bInterval = 0x01,
114 .bInterfaceNumber = 1,
115 .bAlternateSetting = 1,
116 .bNumEndpoints = 2,
117 .bInterfaceClass = 0xe0, /* Wireless */
118 .bInterfaceSubClass = 0x01, /* Radio Frequency */
119 .bInterfaceProtocol = 0x01, /* Bluetooth */
120 .eps = (USBDescEndpoint[]) {
122 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
123 .bmAttributes = USB_ENDPOINT_XFER_INT,
124 .wMaxPacketSize = 0x09,
125 .bInterval = 0x01,
128 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
129 .bmAttributes = USB_ENDPOINT_XFER_INT,
130 .wMaxPacketSize = 0x09,
131 .bInterval = 0x01,
135 .bInterfaceNumber = 1,
136 .bAlternateSetting = 2,
137 .bNumEndpoints = 2,
138 .bInterfaceClass = 0xe0, /* Wireless */
139 .bInterfaceSubClass = 0x01, /* Radio Frequency */
140 .bInterfaceProtocol = 0x01, /* Bluetooth */
141 .eps = (USBDescEndpoint[]) {
143 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
144 .bmAttributes = USB_ENDPOINT_XFER_INT,
145 .wMaxPacketSize = 0x11,
146 .bInterval = 0x01,
149 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
150 .bmAttributes = USB_ENDPOINT_XFER_INT,
151 .wMaxPacketSize = 0x11,
152 .bInterval = 0x01,
156 .bInterfaceNumber = 1,
157 .bAlternateSetting = 3,
158 .bNumEndpoints = 2,
159 .bInterfaceClass = 0xe0, /* Wireless */
160 .bInterfaceSubClass = 0x01, /* Radio Frequency */
161 .bInterfaceProtocol = 0x01, /* Bluetooth */
162 .eps = (USBDescEndpoint[]) {
164 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
165 .bmAttributes = USB_ENDPOINT_XFER_INT,
166 .wMaxPacketSize = 0x19,
167 .bInterval = 0x01,
170 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
171 .bmAttributes = USB_ENDPOINT_XFER_INT,
172 .wMaxPacketSize = 0x19,
173 .bInterval = 0x01,
177 .bInterfaceNumber = 1,
178 .bAlternateSetting = 4,
179 .bNumEndpoints = 2,
180 .bInterfaceClass = 0xe0, /* Wireless */
181 .bInterfaceSubClass = 0x01, /* Radio Frequency */
182 .bInterfaceProtocol = 0x01, /* Bluetooth */
183 .eps = (USBDescEndpoint[]) {
185 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
186 .bmAttributes = USB_ENDPOINT_XFER_INT,
187 .wMaxPacketSize = 0x21,
188 .bInterval = 0x01,
191 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
192 .bmAttributes = USB_ENDPOINT_XFER_INT,
193 .wMaxPacketSize = 0x21,
194 .bInterval = 0x01,
198 .bInterfaceNumber = 1,
199 .bAlternateSetting = 5,
200 .bNumEndpoints = 2,
201 .bInterfaceClass = 0xe0, /* Wireless */
202 .bInterfaceSubClass = 0x01, /* Radio Frequency */
203 .bInterfaceProtocol = 0x01, /* Bluetooth */
204 .eps = (USBDescEndpoint[]) {
206 .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
207 .bmAttributes = USB_ENDPOINT_XFER_INT,
208 .wMaxPacketSize = 0x31,
209 .bInterval = 0x01,
212 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
213 .bmAttributes = USB_ENDPOINT_XFER_INT,
214 .wMaxPacketSize = 0x31,
215 .bInterval = 0x01,
221 static const USBDescDevice desc_device_bluetooth = {
222 .bcdUSB = 0x0110,
223 .bDeviceClass = 0xe0, /* Wireless */
224 .bDeviceSubClass = 0x01, /* Radio Frequency */
225 .bDeviceProtocol = 0x01, /* Bluetooth */
226 .bMaxPacketSize0 = 64,
227 .bNumConfigurations = 1,
228 .confs = (USBDescConfig[]) {
230 .bNumInterfaces = 2,
231 .bConfigurationValue = 1,
232 .bmAttributes = 0xc0,
233 .bMaxPower = 0,
234 .nif = ARRAY_SIZE(desc_iface_bluetooth),
235 .ifs = desc_iface_bluetooth,
240 static const USBDesc desc_bluetooth = {
241 .id = {
242 .idVendor = 0x0a12,
243 .idProduct = 0x0001,
244 .bcdDevice = 0x1958,
245 .iManufacturer = STR_MANUFACTURER,
246 .iProduct = 0,
247 .iSerialNumber = STR_SERIALNUMBER,
249 .full = &desc_device_bluetooth,
250 .str = desc_strings,
253 static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s *fifo)
255 fifo->dstart = 0;
256 fifo->dlen = 0;
257 fifo->dsize = DFIFO_LEN_MASK + 1;
258 fifo->start = 0;
259 fifo->len = 0;
262 static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s *fifo,
263 const uint8_t *data, int len)
265 int off = fifo->dstart + fifo->dlen;
266 uint8_t *buf;
268 fifo->dlen += len;
269 if (off <= DFIFO_LEN_MASK) {
270 if (off + len > DFIFO_LEN_MASK + 1 &&
271 (fifo->dsize = off + len) > (DFIFO_LEN_MASK + 1) * 2) {
272 fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
273 exit(-1);
275 buf = fifo->data + off;
276 } else {
277 if (fifo->dlen > fifo->dsize) {
278 fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
279 exit(-1);
281 buf = fifo->data + off - fifo->dsize;
284 off = (fifo->start + fifo->len ++) & CFIFO_LEN_MASK;
285 fifo->fifo[off].data = memcpy(buf, data, len);
286 fifo->fifo[off].len = len;
289 static inline int usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s *fifo,
290 USBPacket *p)
292 int len;
294 if (likely(!fifo->len))
295 return USB_RET_STALL;
297 len = MIN(p->len, fifo->fifo[fifo->start].len);
298 memcpy(p->data, fifo->fifo[fifo->start].data, len);
299 if (len == p->len) {
300 fifo->fifo[fifo->start].len -= len;
301 fifo->fifo[fifo->start].data += len;
302 } else {
303 fifo->start ++;
304 fifo->start &= CFIFO_LEN_MASK;
305 fifo->len --;
308 fifo->dstart += len;
309 fifo->dlen -= len;
310 if (fifo->dstart >= fifo->dsize) {
311 fifo->dstart = 0;
312 fifo->dsize = DFIFO_LEN_MASK + 1;
315 return len;
318 static inline void usb_bt_fifo_out_enqueue(struct USBBtState *s,
319 struct usb_hci_out_fifo_s *fifo,
320 void (*send)(struct HCIInfo *, const uint8_t *, int),
321 int (*complete)(const uint8_t *, int),
322 const uint8_t *data, int len)
324 if (fifo->len) {
325 memcpy(fifo->data + fifo->len, data, len);
326 fifo->len += len;
327 if (complete(fifo->data, fifo->len)) {
328 send(s->hci, fifo->data, fifo->len);
329 fifo->len = 0;
331 } else if (complete(data, len))
332 send(s->hci, data, len);
333 else {
334 memcpy(fifo->data, data, len);
335 fifo->len = len;
338 /* TODO: do we need to loop? */
341 static int usb_bt_hci_cmd_complete(const uint8_t *data, int len)
343 len -= HCI_COMMAND_HDR_SIZE;
344 return len >= 0 &&
345 len >= ((struct hci_command_hdr *) data)->plen;
348 static int usb_bt_hci_acl_complete(const uint8_t *data, int len)
350 len -= HCI_ACL_HDR_SIZE;
351 return len >= 0 &&
352 len >= le16_to_cpu(((struct hci_acl_hdr *) data)->dlen);
355 static int usb_bt_hci_sco_complete(const uint8_t *data, int len)
357 len -= HCI_SCO_HDR_SIZE;
358 return len >= 0 &&
359 len >= ((struct hci_sco_hdr *) data)->dlen;
362 static void usb_bt_handle_reset(USBDevice *dev)
364 struct USBBtState *s = (struct USBBtState *) dev->opaque;
366 usb_bt_fifo_reset(&s->evt);
367 usb_bt_fifo_reset(&s->acl);
368 usb_bt_fifo_reset(&s->sco);
369 s->outcmd.len = 0;
370 s->outacl.len = 0;
371 s->outsco.len = 0;
372 s->altsetting = 0;
375 static int usb_bt_handle_control(USBDevice *dev, USBPacket *p,
376 int request, int value, int index, int length, uint8_t *data)
378 struct USBBtState *s = (struct USBBtState *) dev->opaque;
379 int ret;
381 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
382 if (ret >= 0) {
383 switch (request) {
384 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
385 s->config = 0;
386 break;
387 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
388 s->config = 1;
389 usb_bt_fifo_reset(&s->evt);
390 usb_bt_fifo_reset(&s->acl);
391 usb_bt_fifo_reset(&s->sco);
392 break;
394 return ret;
397 ret = 0;
398 switch (request) {
399 case InterfaceRequest | USB_REQ_GET_STATUS:
400 case EndpointRequest | USB_REQ_GET_STATUS:
401 data[0] = 0x00;
402 data[1] = 0x00;
403 ret = 2;
404 break;
405 case InterfaceOutRequest | USB_REQ_CLEAR_FEATURE:
406 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
407 goto fail;
408 case InterfaceOutRequest | USB_REQ_SET_FEATURE:
409 case EndpointOutRequest | USB_REQ_SET_FEATURE:
410 goto fail;
411 break;
412 case InterfaceRequest | USB_REQ_GET_INTERFACE:
413 if (value != 0 || (index & ~1) || length != 1)
414 goto fail;
415 if (index == 1)
416 data[0] = s->altsetting;
417 else
418 data[0] = 0;
419 ret = 1;
420 break;
421 case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
422 if ((index & ~1) || length != 0 ||
423 (index == 1 && (value < 0 || value > 4)) ||
424 (index == 0 && value != 0)) {
425 printf("%s: Wrong SET_INTERFACE request (%i, %i)\n",
426 __FUNCTION__, index, value);
427 goto fail;
429 s->altsetting = value;
430 ret = 0;
431 break;
432 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_DEVICE) << 8):
433 if (s->config)
434 usb_bt_fifo_out_enqueue(s, &s->outcmd, s->hci->cmd_send,
435 usb_bt_hci_cmd_complete, data, length);
436 break;
437 default:
438 fail:
439 ret = USB_RET_STALL;
440 break;
442 return ret;
445 static int usb_bt_handle_data(USBDevice *dev, USBPacket *p)
447 struct USBBtState *s = (struct USBBtState *) dev->opaque;
448 int ret = 0;
450 if (!s->config)
451 goto fail;
453 switch (p->pid) {
454 case USB_TOKEN_IN:
455 switch (p->devep & 0xf) {
456 case USB_EVT_EP:
457 ret = usb_bt_fifo_dequeue(&s->evt, p);
458 break;
460 case USB_ACL_EP:
461 ret = usb_bt_fifo_dequeue(&s->acl, p);
462 break;
464 case USB_SCO_EP:
465 ret = usb_bt_fifo_dequeue(&s->sco, p);
466 break;
468 default:
469 goto fail;
471 break;
473 case USB_TOKEN_OUT:
474 switch (p->devep & 0xf) {
475 case USB_ACL_EP:
476 usb_bt_fifo_out_enqueue(s, &s->outacl, s->hci->acl_send,
477 usb_bt_hci_acl_complete, p->data, p->len);
478 break;
480 case USB_SCO_EP:
481 usb_bt_fifo_out_enqueue(s, &s->outsco, s->hci->sco_send,
482 usb_bt_hci_sco_complete, p->data, p->len);
483 break;
485 default:
486 goto fail;
488 break;
490 default:
491 fail:
492 ret = USB_RET_STALL;
493 break;
496 return ret;
499 static void usb_bt_out_hci_packet_event(void *opaque,
500 const uint8_t *data, int len)
502 struct USBBtState *s = (struct USBBtState *) opaque;
504 usb_bt_fifo_enqueue(&s->evt, data, len);
507 static void usb_bt_out_hci_packet_acl(void *opaque,
508 const uint8_t *data, int len)
510 struct USBBtState *s = (struct USBBtState *) opaque;
512 usb_bt_fifo_enqueue(&s->acl, data, len);
515 static void usb_bt_handle_destroy(USBDevice *dev)
517 struct USBBtState *s = (struct USBBtState *) dev->opaque;
519 s->hci->opaque = NULL;
520 s->hci->evt_recv = NULL;
521 s->hci->acl_recv = NULL;
524 static int usb_bt_initfn(USBDevice *dev)
526 usb_desc_init(dev);
527 return 0;
530 USBDevice *usb_bt_init(HCIInfo *hci)
532 USBDevice *dev;
533 struct USBBtState *s;
535 if (!hci)
536 return NULL;
537 dev = usb_create_simple(NULL /* FIXME */, "usb-bt-dongle");
538 s = DO_UPCAST(struct USBBtState, dev, dev);
539 s->dev.opaque = s;
541 s->hci = hci;
542 s->hci->opaque = s;
543 s->hci->evt_recv = usb_bt_out_hci_packet_event;
544 s->hci->acl_recv = usb_bt_out_hci_packet_acl;
546 usb_bt_handle_reset(&s->dev);
548 return dev;
551 static struct USBDeviceInfo bt_info = {
552 .product_desc = "QEMU BT dongle",
553 .qdev.name = "usb-bt-dongle",
554 .qdev.size = sizeof(struct USBBtState),
555 .usb_desc = &desc_bluetooth,
556 .init = usb_bt_initfn,
557 .handle_packet = usb_generic_handle_packet,
558 .handle_reset = usb_bt_handle_reset,
559 .handle_control = usb_bt_handle_control,
560 .handle_data = usb_bt_handle_data,
561 .handle_destroy = usb_bt_handle_destroy,
564 static void usb_bt_register_devices(void)
566 usb_qdev_register(&bt_info);
568 device_init(usb_bt_register_devices)