dev-bluetooth.c 18 KB

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