dev-bluetooth.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * QEMU Bluetooth HCI USB Transport Layer v1.0
  3. *
  4. * Copyright (C) 2007 OpenMoko, Inc.
  5. * Copyright (C) 2008 Andrzej Zaborowski <balrog@zabor.org>
  6. *
  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.
  11. *
  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.
  16. *
  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/>.
  19. */
  20. #include "qemu-common.h"
  21. #include "hw/usb.h"
  22. #include "hw/usb/desc.h"
  23. #include "bt/bt.h"
  24. #include "hw/bt.h"
  25. struct USBBtState {
  26. USBDevice dev;
  27. struct HCIInfo *hci;
  28. USBEndpoint *intr;
  29. int config;
  30. #define CFIFO_LEN_MASK 255
  31. #define DFIFO_LEN_MASK 4095
  32. struct usb_hci_in_fifo_s {
  33. uint8_t data[(DFIFO_LEN_MASK + 1) * 2];
  34. struct {
  35. uint8_t *data;
  36. int len;
  37. } fifo[CFIFO_LEN_MASK + 1];
  38. int dstart, dlen, dsize, start, len;
  39. } evt, acl, sco;
  40. struct usb_hci_out_fifo_s {
  41. uint8_t data[4096];
  42. int len;
  43. } outcmd, outacl, outsco;
  44. };
  45. #define USB_EVT_EP 1
  46. #define USB_ACL_EP 2
  47. #define USB_SCO_EP 3
  48. enum {
  49. STR_MANUFACTURER = 1,
  50. STR_SERIALNUMBER,
  51. };
  52. static const USBDescStrings desc_strings = {
  53. [STR_MANUFACTURER] = "QEMU",
  54. [STR_SERIALNUMBER] = "1",
  55. };
  56. static const USBDescIface desc_iface_bluetooth[] = {
  57. {
  58. .bInterfaceNumber = 0,
  59. .bNumEndpoints = 3,
  60. .bInterfaceClass = 0xe0, /* Wireless */
  61. .bInterfaceSubClass = 0x01, /* Radio Frequency */
  62. .bInterfaceProtocol = 0x01, /* Bluetooth */
  63. .eps = (USBDescEndpoint[]) {
  64. {
  65. .bEndpointAddress = USB_DIR_IN | USB_EVT_EP,
  66. .bmAttributes = USB_ENDPOINT_XFER_INT,
  67. .wMaxPacketSize = 0x10,
  68. .bInterval = 0x02,
  69. },
  70. {
  71. .bEndpointAddress = USB_DIR_OUT | USB_ACL_EP,
  72. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  73. .wMaxPacketSize = 0x40,
  74. .bInterval = 0x0a,
  75. },
  76. {
  77. .bEndpointAddress = USB_DIR_IN | USB_ACL_EP,
  78. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  79. .wMaxPacketSize = 0x40,
  80. .bInterval = 0x0a,
  81. },
  82. },
  83. },{
  84. .bInterfaceNumber = 1,
  85. .bAlternateSetting = 0,
  86. .bNumEndpoints = 2,
  87. .bInterfaceClass = 0xe0, /* Wireless */
  88. .bInterfaceSubClass = 0x01, /* Radio Frequency */
  89. .bInterfaceProtocol = 0x01, /* Bluetooth */
  90. .eps = (USBDescEndpoint[]) {
  91. {
  92. .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
  93. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  94. .wMaxPacketSize = 0,
  95. .bInterval = 0x01,
  96. },
  97. {
  98. .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
  99. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  100. .wMaxPacketSize = 0,
  101. .bInterval = 0x01,
  102. },
  103. },
  104. },{
  105. .bInterfaceNumber = 1,
  106. .bAlternateSetting = 1,
  107. .bNumEndpoints = 2,
  108. .bInterfaceClass = 0xe0, /* Wireless */
  109. .bInterfaceSubClass = 0x01, /* Radio Frequency */
  110. .bInterfaceProtocol = 0x01, /* Bluetooth */
  111. .eps = (USBDescEndpoint[]) {
  112. {
  113. .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
  114. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  115. .wMaxPacketSize = 0x09,
  116. .bInterval = 0x01,
  117. },
  118. {
  119. .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
  120. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  121. .wMaxPacketSize = 0x09,
  122. .bInterval = 0x01,
  123. },
  124. },
  125. },{
  126. .bInterfaceNumber = 1,
  127. .bAlternateSetting = 2,
  128. .bNumEndpoints = 2,
  129. .bInterfaceClass = 0xe0, /* Wireless */
  130. .bInterfaceSubClass = 0x01, /* Radio Frequency */
  131. .bInterfaceProtocol = 0x01, /* Bluetooth */
  132. .eps = (USBDescEndpoint[]) {
  133. {
  134. .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
  135. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  136. .wMaxPacketSize = 0x11,
  137. .bInterval = 0x01,
  138. },
  139. {
  140. .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
  141. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  142. .wMaxPacketSize = 0x11,
  143. .bInterval = 0x01,
  144. },
  145. },
  146. },{
  147. .bInterfaceNumber = 1,
  148. .bAlternateSetting = 3,
  149. .bNumEndpoints = 2,
  150. .bInterfaceClass = 0xe0, /* Wireless */
  151. .bInterfaceSubClass = 0x01, /* Radio Frequency */
  152. .bInterfaceProtocol = 0x01, /* Bluetooth */
  153. .eps = (USBDescEndpoint[]) {
  154. {
  155. .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
  156. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  157. .wMaxPacketSize = 0x19,
  158. .bInterval = 0x01,
  159. },
  160. {
  161. .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
  162. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  163. .wMaxPacketSize = 0x19,
  164. .bInterval = 0x01,
  165. },
  166. },
  167. },{
  168. .bInterfaceNumber = 1,
  169. .bAlternateSetting = 4,
  170. .bNumEndpoints = 2,
  171. .bInterfaceClass = 0xe0, /* Wireless */
  172. .bInterfaceSubClass = 0x01, /* Radio Frequency */
  173. .bInterfaceProtocol = 0x01, /* Bluetooth */
  174. .eps = (USBDescEndpoint[]) {
  175. {
  176. .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
  177. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  178. .wMaxPacketSize = 0x21,
  179. .bInterval = 0x01,
  180. },
  181. {
  182. .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
  183. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  184. .wMaxPacketSize = 0x21,
  185. .bInterval = 0x01,
  186. },
  187. },
  188. },{
  189. .bInterfaceNumber = 1,
  190. .bAlternateSetting = 5,
  191. .bNumEndpoints = 2,
  192. .bInterfaceClass = 0xe0, /* Wireless */
  193. .bInterfaceSubClass = 0x01, /* Radio Frequency */
  194. .bInterfaceProtocol = 0x01, /* Bluetooth */
  195. .eps = (USBDescEndpoint[]) {
  196. {
  197. .bEndpointAddress = USB_DIR_OUT | USB_SCO_EP,
  198. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  199. .wMaxPacketSize = 0x31,
  200. .bInterval = 0x01,
  201. },
  202. {
  203. .bEndpointAddress = USB_DIR_IN | USB_SCO_EP,
  204. .bmAttributes = USB_ENDPOINT_XFER_ISOC,
  205. .wMaxPacketSize = 0x31,
  206. .bInterval = 0x01,
  207. },
  208. },
  209. }
  210. };
  211. static const USBDescDevice desc_device_bluetooth = {
  212. .bcdUSB = 0x0110,
  213. .bDeviceClass = 0xe0, /* Wireless */
  214. .bDeviceSubClass = 0x01, /* Radio Frequency */
  215. .bDeviceProtocol = 0x01, /* Bluetooth */
  216. .bMaxPacketSize0 = 64,
  217. .bNumConfigurations = 1,
  218. .confs = (USBDescConfig[]) {
  219. {
  220. .bNumInterfaces = 2,
  221. .bConfigurationValue = 1,
  222. .bmAttributes = 0xc0,
  223. .bMaxPower = 0,
  224. .nif = ARRAY_SIZE(desc_iface_bluetooth),
  225. .ifs = desc_iface_bluetooth,
  226. },
  227. },
  228. };
  229. static const USBDesc desc_bluetooth = {
  230. .id = {
  231. .idVendor = 0x0a12,
  232. .idProduct = 0x0001,
  233. .bcdDevice = 0x1958,
  234. .iManufacturer = STR_MANUFACTURER,
  235. .iProduct = 0,
  236. .iSerialNumber = STR_SERIALNUMBER,
  237. },
  238. .full = &desc_device_bluetooth,
  239. .str = desc_strings,
  240. };
  241. static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s *fifo)
  242. {
  243. fifo->dstart = 0;
  244. fifo->dlen = 0;
  245. fifo->dsize = DFIFO_LEN_MASK + 1;
  246. fifo->start = 0;
  247. fifo->len = 0;
  248. }
  249. static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s *fifo,
  250. const uint8_t *data, int len)
  251. {
  252. int off = fifo->dstart + fifo->dlen;
  253. uint8_t *buf;
  254. fifo->dlen += len;
  255. if (off <= DFIFO_LEN_MASK) {
  256. if (off + len > DFIFO_LEN_MASK + 1 &&
  257. (fifo->dsize = off + len) > (DFIFO_LEN_MASK + 1) * 2) {
  258. fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
  259. exit(-1);
  260. }
  261. buf = fifo->data + off;
  262. } else {
  263. if (fifo->dlen > fifo->dsize) {
  264. fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
  265. exit(-1);
  266. }
  267. buf = fifo->data + off - fifo->dsize;
  268. }
  269. off = (fifo->start + fifo->len ++) & CFIFO_LEN_MASK;
  270. fifo->fifo[off].data = memcpy(buf, data, len);
  271. fifo->fifo[off].len = len;
  272. }
  273. static inline void usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s *fifo,
  274. USBPacket *p)
  275. {
  276. int len;
  277. assert(fifo->len != 0);
  278. len = MIN(p->iov.size, fifo->fifo[fifo->start].len);
  279. usb_packet_copy(p, fifo->fifo[fifo->start].data, len);
  280. if (len == p->iov.size) {
  281. fifo->fifo[fifo->start].len -= len;
  282. fifo->fifo[fifo->start].data += len;
  283. } else {
  284. fifo->start ++;
  285. fifo->start &= CFIFO_LEN_MASK;
  286. fifo->len --;
  287. }
  288. fifo->dstart += len;
  289. fifo->dlen -= len;
  290. if (fifo->dstart >= fifo->dsize) {
  291. fifo->dstart = 0;
  292. fifo->dsize = DFIFO_LEN_MASK + 1;
  293. }
  294. }
  295. static inline void usb_bt_fifo_out_enqueue(struct USBBtState *s,
  296. struct usb_hci_out_fifo_s *fifo,
  297. void (*send)(struct HCIInfo *, const uint8_t *, int),
  298. int (*complete)(const uint8_t *, int),
  299. USBPacket *p)
  300. {
  301. usb_packet_copy(p, fifo->data + fifo->len, p->iov.size);
  302. fifo->len += p->iov.size;
  303. if (complete(fifo->data, fifo->len)) {
  304. send(s->hci, fifo->data, fifo->len);
  305. fifo->len = 0;
  306. }
  307. /* TODO: do we need to loop? */
  308. }
  309. static int usb_bt_hci_cmd_complete(const uint8_t *data, int len)
  310. {
  311. len -= HCI_COMMAND_HDR_SIZE;
  312. return len >= 0 &&
  313. len >= ((struct hci_command_hdr *) data)->plen;
  314. }
  315. static int usb_bt_hci_acl_complete(const uint8_t *data, int len)
  316. {
  317. len -= HCI_ACL_HDR_SIZE;
  318. return len >= 0 &&
  319. len >= le16_to_cpu(((struct hci_acl_hdr *) data)->dlen);
  320. }
  321. static int usb_bt_hci_sco_complete(const uint8_t *data, int len)
  322. {
  323. len -= HCI_SCO_HDR_SIZE;
  324. return len >= 0 &&
  325. len >= ((struct hci_sco_hdr *) data)->dlen;
  326. }
  327. static void usb_bt_handle_reset(USBDevice *dev)
  328. {
  329. struct USBBtState *s = (struct USBBtState *) dev->opaque;
  330. usb_bt_fifo_reset(&s->evt);
  331. usb_bt_fifo_reset(&s->acl);
  332. usb_bt_fifo_reset(&s->sco);
  333. s->outcmd.len = 0;
  334. s->outacl.len = 0;
  335. s->outsco.len = 0;
  336. }
  337. static void usb_bt_handle_control(USBDevice *dev, USBPacket *p,
  338. int request, int value, int index, int length, uint8_t *data)
  339. {
  340. struct USBBtState *s = (struct USBBtState *) dev->opaque;
  341. int ret;
  342. ret = usb_desc_handle_control(dev, p, request, value, index, length, data);
  343. if (ret >= 0) {
  344. switch (request) {
  345. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  346. s->config = 0;
  347. break;
  348. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  349. s->config = 1;
  350. usb_bt_fifo_reset(&s->evt);
  351. usb_bt_fifo_reset(&s->acl);
  352. usb_bt_fifo_reset(&s->sco);
  353. break;
  354. }
  355. return;
  356. }
  357. switch (request) {
  358. case InterfaceRequest | USB_REQ_GET_STATUS:
  359. case EndpointRequest | USB_REQ_GET_STATUS:
  360. data[0] = 0x00;
  361. data[1] = 0x00;
  362. p->actual_length = 2;
  363. break;
  364. case InterfaceOutRequest | USB_REQ_CLEAR_FEATURE:
  365. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  366. goto fail;
  367. case InterfaceOutRequest | USB_REQ_SET_FEATURE:
  368. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  369. goto fail;
  370. break;
  371. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_DEVICE) << 8):
  372. if (s->config)
  373. usb_bt_fifo_out_enqueue(s, &s->outcmd, s->hci->cmd_send,
  374. usb_bt_hci_cmd_complete, p);
  375. break;
  376. default:
  377. fail:
  378. p->status = USB_RET_STALL;
  379. break;
  380. }
  381. }
  382. static void usb_bt_handle_data(USBDevice *dev, USBPacket *p)
  383. {
  384. struct USBBtState *s = (struct USBBtState *) dev->opaque;
  385. if (!s->config)
  386. goto fail;
  387. switch (p->pid) {
  388. case USB_TOKEN_IN:
  389. switch (p->ep->nr) {
  390. case USB_EVT_EP:
  391. if (s->evt.len == 0) {
  392. p->status = USB_RET_NAK;
  393. break;
  394. }
  395. usb_bt_fifo_dequeue(&s->evt, p);
  396. break;
  397. case USB_ACL_EP:
  398. if (s->evt.len == 0) {
  399. p->status = USB_RET_STALL;
  400. break;
  401. }
  402. usb_bt_fifo_dequeue(&s->acl, p);
  403. break;
  404. case USB_SCO_EP:
  405. if (s->evt.len == 0) {
  406. p->status = USB_RET_STALL;
  407. break;
  408. }
  409. usb_bt_fifo_dequeue(&s->sco, p);
  410. break;
  411. default:
  412. goto fail;
  413. }
  414. break;
  415. case USB_TOKEN_OUT:
  416. switch (p->ep->nr) {
  417. case USB_ACL_EP:
  418. usb_bt_fifo_out_enqueue(s, &s->outacl, s->hci->acl_send,
  419. usb_bt_hci_acl_complete, p);
  420. break;
  421. case USB_SCO_EP:
  422. usb_bt_fifo_out_enqueue(s, &s->outsco, s->hci->sco_send,
  423. usb_bt_hci_sco_complete, p);
  424. break;
  425. default:
  426. goto fail;
  427. }
  428. break;
  429. default:
  430. fail:
  431. p->status = USB_RET_STALL;
  432. break;
  433. }
  434. }
  435. static void usb_bt_out_hci_packet_event(void *opaque,
  436. const uint8_t *data, int len)
  437. {
  438. struct USBBtState *s = (struct USBBtState *) opaque;
  439. if (s->evt.len == 0) {
  440. usb_wakeup(s->intr);
  441. }
  442. usb_bt_fifo_enqueue(&s->evt, data, len);
  443. }
  444. static void usb_bt_out_hci_packet_acl(void *opaque,
  445. const uint8_t *data, int len)
  446. {
  447. struct USBBtState *s = (struct USBBtState *) opaque;
  448. usb_bt_fifo_enqueue(&s->acl, data, len);
  449. }
  450. static void usb_bt_handle_destroy(USBDevice *dev)
  451. {
  452. struct USBBtState *s = (struct USBBtState *) dev->opaque;
  453. s->hci->opaque = NULL;
  454. s->hci->evt_recv = NULL;
  455. s->hci->acl_recv = NULL;
  456. }
  457. static int usb_bt_initfn(USBDevice *dev)
  458. {
  459. struct USBBtState *s = DO_UPCAST(struct USBBtState, dev, dev);
  460. usb_desc_create_serial(dev);
  461. usb_desc_init(dev);
  462. s->intr = usb_ep_get(dev, USB_TOKEN_IN, USB_EVT_EP);
  463. return 0;
  464. }
  465. USBDevice *usb_bt_init(USBBus *bus, HCIInfo *hci)
  466. {
  467. USBDevice *dev;
  468. struct USBBtState *s;
  469. if (!hci)
  470. return NULL;
  471. dev = usb_create_simple(bus, "usb-bt-dongle");
  472. if (!dev) {
  473. return NULL;
  474. }
  475. s = DO_UPCAST(struct USBBtState, dev, dev);
  476. s->dev.opaque = s;
  477. s->hci = hci;
  478. s->hci->opaque = s;
  479. s->hci->evt_recv = usb_bt_out_hci_packet_event;
  480. s->hci->acl_recv = usb_bt_out_hci_packet_acl;
  481. usb_bt_handle_reset(&s->dev);
  482. return dev;
  483. }
  484. static const VMStateDescription vmstate_usb_bt = {
  485. .name = "usb-bt",
  486. .unmigratable = 1,
  487. };
  488. static void usb_bt_class_initfn(ObjectClass *klass, void *data)
  489. {
  490. DeviceClass *dc = DEVICE_CLASS(klass);
  491. USBDeviceClass *uc = USB_DEVICE_CLASS(klass);
  492. uc->init = usb_bt_initfn;
  493. uc->product_desc = "QEMU BT dongle";
  494. uc->usb_desc = &desc_bluetooth;
  495. uc->handle_reset = usb_bt_handle_reset;
  496. uc->handle_control = usb_bt_handle_control;
  497. uc->handle_data = usb_bt_handle_data;
  498. uc->handle_destroy = usb_bt_handle_destroy;
  499. dc->vmsd = &vmstate_usb_bt;
  500. }
  501. static const TypeInfo bt_info = {
  502. .name = "usb-bt-dongle",
  503. .parent = TYPE_USB_DEVICE,
  504. .instance_size = sizeof(struct USBBtState),
  505. .class_init = usb_bt_class_initfn,
  506. };
  507. static void usb_bt_register_types(void)
  508. {
  509. type_register_static(&bt_info);
  510. }
  511. type_init(usb_bt_register_types)