fdc: Add fallback option
[qemu/ar7.git] / hw / usb / dev-bluetooth.c
blobb19ec76b007ad1050144f38ed12a50d6d4fa5107
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 "qemu/error-report.h"
23 #include "hw/usb.h"
24 #include "hw/usb/desc.h"
25 #include "sysemu/bt.h"
26 #include "hw/bt.h"
28 struct USBBtState {
29 USBDevice dev;
30 struct HCIInfo *hci;
31 USBEndpoint *intr;
33 int config;
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];
39 struct {
40 uint8_t *data;
41 int len;
42 } fifo[CFIFO_LEN_MASK + 1];
43 int dstart, dlen, dsize, start, len;
44 } evt, acl, sco;
46 struct usb_hci_out_fifo_s {
47 uint8_t data[4096];
48 int len;
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)
55 #define USB_EVT_EP 1
56 #define USB_ACL_EP 2
57 #define USB_SCO_EP 3
59 enum {
60 STR_MANUFACTURER = 1,
61 STR_SERIALNUMBER,
64 static const USBDescStrings desc_strings = {
65 [STR_MANUFACTURER] = "QEMU",
66 [STR_SERIALNUMBER] = "1",
69 static const USBDescIface desc_iface_bluetooth[] = {
71 .bInterfaceNumber = 0,
72 .bNumEndpoints = 3,
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,
81 .bInterval = 0x02,
84 .bEndpointAddress = USB_DIR_OUT | USB_ACL_EP,
85 .bmAttributes = USB_ENDPOINT_XFER_BULK,
86 .wMaxPacketSize = 0x40,
87 .bInterval = 0x0a,
90 .bEndpointAddress = USB_DIR_IN | USB_ACL_EP,
91 .bmAttributes = USB_ENDPOINT_XFER_BULK,
92 .wMaxPacketSize = 0x40,
93 .bInterval = 0x0a,
96 },{
97 .bInterfaceNumber = 1,
98 .bAlternateSetting = 0,
99 .bNumEndpoints = 2,
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,
107 .wMaxPacketSize = 0,
108 .bInterval = 0x01,
111 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
112 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
113 .wMaxPacketSize = 0,
114 .bInterval = 0x01,
118 .bInterfaceNumber = 1,
119 .bAlternateSetting = 1,
120 .bNumEndpoints = 2,
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,
129 .bInterval = 0x01,
132 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
133 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
134 .wMaxPacketSize = 0x09,
135 .bInterval = 0x01,
139 .bInterfaceNumber = 1,
140 .bAlternateSetting = 2,
141 .bNumEndpoints = 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,
150 .bInterval = 0x01,
153 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
154 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
155 .wMaxPacketSize = 0x11,
156 .bInterval = 0x01,
160 .bInterfaceNumber = 1,
161 .bAlternateSetting = 3,
162 .bNumEndpoints = 2,
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,
171 .bInterval = 0x01,
174 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
175 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
176 .wMaxPacketSize = 0x19,
177 .bInterval = 0x01,
181 .bInterfaceNumber = 1,
182 .bAlternateSetting = 4,
183 .bNumEndpoints = 2,
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,
192 .bInterval = 0x01,
195 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
196 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
197 .wMaxPacketSize = 0x21,
198 .bInterval = 0x01,
202 .bInterfaceNumber = 1,
203 .bAlternateSetting = 5,
204 .bNumEndpoints = 2,
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,
213 .bInterval = 0x01,
216 .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
217 .bmAttributes = USB_ENDPOINT_XFER_ISOC,
218 .wMaxPacketSize = 0x31,
219 .bInterval = 0x01,
225 static const USBDescDevice desc_device_bluetooth = {
226 .bcdUSB = 0x0110,
227 .bDeviceClass = 0xe0, /* Wireless */
228 .bDeviceSubClass = 0x01, /* Radio Frequency */
229 .bDeviceProtocol = 0x01, /* Bluetooth */
230 .bMaxPacketSize0 = 64,
231 .bNumConfigurations = 1,
232 .confs = (USBDescConfig[]) {
234 .bNumInterfaces = 2,
235 .bConfigurationValue = 1,
236 .bmAttributes = USB_CFG_ATT_ONE | USB_CFG_ATT_SELFPOWER,
237 .bMaxPower = 0,
238 .nif = ARRAY_SIZE(desc_iface_bluetooth),
239 .ifs = desc_iface_bluetooth,
244 static const USBDesc desc_bluetooth = {
245 .id = {
246 .idVendor = 0x0a12,
247 .idProduct = 0x0001,
248 .bcdDevice = 0x1958,
249 .iManufacturer = STR_MANUFACTURER,
250 .iProduct = 0,
251 .iSerialNumber = STR_SERIALNUMBER,
253 .full = &desc_device_bluetooth,
254 .str = desc_strings,
257 static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s *fifo)
259 fifo->dstart = 0;
260 fifo->dlen = 0;
261 fifo->dsize = DFIFO_LEN_MASK + 1;
262 fifo->start = 0;
263 fifo->len = 0;
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;
270 uint8_t *buf;
272 fifo->dlen += len;
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);
277 exit(-1);
279 buf = fifo->data + off;
280 } else {
281 if (fifo->dlen > fifo->dsize) {
282 fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
283 exit(-1);
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,
294 USBPacket *p)
296 int len;
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;
305 } else {
306 fifo->start ++;
307 fifo->start &= CFIFO_LEN_MASK;
308 fifo->len --;
311 fifo->dstart += len;
312 fifo->dlen -= len;
313 if (fifo->dstart >= fifo->dsize) {
314 fifo->dstart = 0;
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),
323 USBPacket *p)
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);
329 fifo->len = 0;
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;
338 return len >= 0 &&
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;
345 return len >= 0 &&
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;
352 return len >= 0 &&
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);
363 s->outcmd.len = 0;
364 s->outacl.len = 0;
365 s->outsco.len = 0;
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;
372 int ret;
374 ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
375 if (ret >= 0) {
376 switch (request) {
377 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
378 s->config = 0;
379 break;
380 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
381 s->config = 1;
382 usb_bt_fifo_reset(&s->evt);
383 usb_bt_fifo_reset(&s->acl);
384 usb_bt_fifo_reset(&s->sco);
385 break;
387 return;
390 switch (request) {
391 case InterfaceRequest | USB_REQ_GET_STATUS:
392 case EndpointRequest | USB_REQ_GET_STATUS:
393 data[0] = 0x00;
394 data[1] = 0x00;
395 p->actual_length = 2;
396 break;
397 case InterfaceOutRequest | USB_REQ_CLEAR_FEATURE:
398 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
399 goto fail;
400 case InterfaceOutRequest | USB_REQ_SET_FEATURE:
401 case EndpointOutRequest | USB_REQ_SET_FEATURE:
402 goto fail;
403 break;
404 case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_DEVICE) << 8):
405 if (s->config)
406 usb_bt_fifo_out_enqueue(s, &s->outcmd, s->hci->cmd_send,
407 usb_bt_hci_cmd_complete, p);
408 break;
409 default:
410 fail:
411 p->status = USB_RET_STALL;
412 break;
416 static void usb_bt_handle_data(USBDevice *dev, USBPacket *p)
418 struct USBBtState *s = (struct USBBtState *) dev->opaque;
420 if (!s->config)
421 goto fail;
423 switch (p->pid) {
424 case USB_TOKEN_IN:
425 switch (p->ep->nr) {
426 case USB_EVT_EP:
427 if (s->evt.len == 0) {
428 p->status = USB_RET_NAK;
429 break;
431 usb_bt_fifo_dequeue(&s->evt, p);
432 break;
434 case USB_ACL_EP:
435 if (s->evt.len == 0) {
436 p->status = USB_RET_STALL;
437 break;
439 usb_bt_fifo_dequeue(&s->acl, p);
440 break;
442 case USB_SCO_EP:
443 if (s->evt.len == 0) {
444 p->status = USB_RET_STALL;
445 break;
447 usb_bt_fifo_dequeue(&s->sco, p);
448 break;
450 default:
451 goto fail;
453 break;
455 case USB_TOKEN_OUT:
456 switch (p->ep->nr) {
457 case USB_ACL_EP:
458 usb_bt_fifo_out_enqueue(s, &s->outacl, s->hci->acl_send,
459 usb_bt_hci_acl_complete, p);
460 break;
462 case USB_SCO_EP:
463 usb_bt_fifo_out_enqueue(s, &s->outsco, s->hci->sco_send,
464 usb_bt_hci_sco_complete, p);
465 break;
467 default:
468 goto fail;
470 break;
472 default:
473 fail:
474 p->status = USB_RET_STALL;
475 break;
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);
512 usb_desc_init(dev);
513 s->dev.opaque = s;
514 if (!s->hci) {
515 s->hci = bt_new_hci(qemu_find_bt_vlan(0));
517 s->hci->opaque = s;
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)
526 USBDevice *dev;
527 struct USBBtState *s;
528 HCIInfo *hci;
529 const char *name = TYPE_USB_BT;
531 if (*cmdline) {
532 hci = hci_init(cmdline);
533 } else {
534 hci = bt_new_hci(qemu_find_bt_vlan(0));
536 if (!hci)
537 return NULL;
539 dev = usb_create(bus, name);
540 s = USB_BT(dev);
541 s->hci = hci;
542 return dev;
545 static const VMStateDescription vmstate_usb_bt = {
546 .name = "usb-bt",
547 .unmigratable = 1,
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 = {
567 .name = TYPE_USB_BT,
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)