2
0

dev-bluetooth.c 18 KB

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