usb-bt.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include "qemu-common.h"
  22. #include "usb.h"
  23. #include "net.h"
  24. #include "bt.h"
  25. struct USBBtState {
  26. USBDevice dev;
  27. struct HCIInfo *hci;
  28. int altsetting;
  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. static const uint8_t qemu_bt_dev_descriptor[] = {
  49. 0x12, /* u8 bLength; */
  50. USB_DT_DEVICE, /* u8 bDescriptorType; Device */
  51. 0x10, 0x01, /* u16 bcdUSB; v1.10 */
  52. 0xe0, /* u8 bDeviceClass; Wireless */
  53. 0x01, /* u8 bDeviceSubClass; Radio Frequency */
  54. 0x01, /* u8 bDeviceProtocol; Bluetooth */
  55. 0x40, /* u8 bMaxPacketSize0; 64 Bytes */
  56. 0x12, 0x0a, /* u16 idVendor; */
  57. 0x01, 0x00, /* u16 idProduct; Bluetooth Dongle (HCI mode) */
  58. 0x58, 0x19, /* u16 bcdDevice; (some devices have 0x48, 0x02) */
  59. 0x00, /* u8 iManufacturer; */
  60. 0x00, /* u8 iProduct; */
  61. 0x00, /* u8 iSerialNumber; */
  62. 0x01, /* u8 bNumConfigurations; */
  63. };
  64. static const uint8_t qemu_bt_config_descriptor[] = {
  65. /* one configuration */
  66. 0x09, /* u8 bLength; */
  67. USB_DT_CONFIG, /* u8 bDescriptorType; */
  68. 0xb1, 0x00, /* u16 wTotalLength; */
  69. 0x02, /* u8 bNumInterfaces; (2) */
  70. 0x01, /* u8 bConfigurationValue; */
  71. 0x00, /* u8 iConfiguration; */
  72. 0xc0, /* u8 bmAttributes;
  73. Bit 7: must be set,
  74. 6: Self-powered,
  75. 5: Remote wakeup,
  76. 4..0: resvd */
  77. 0x00, /* u8 MaxPower; */
  78. /* USB 1.1:
  79. * USB 2.0, single TT organization (mandatory):
  80. * one interface, protocol 0
  81. *
  82. * USB 2.0, multiple TT organization (optional):
  83. * two interfaces, protocols 1 (like single TT)
  84. * and 2 (multiple TT mode) ... config is
  85. * sometimes settable
  86. * NOT IMPLEMENTED
  87. */
  88. /* interface one */
  89. 0x09, /* u8 if_bLength; */
  90. USB_DT_INTERFACE, /* u8 if_bDescriptorType; */
  91. 0x00, /* u8 if_bInterfaceNumber; */
  92. 0x00, /* u8 if_bAlternateSetting; */
  93. 0x03, /* u8 if_bNumEndpoints; */
  94. 0xe0, /* u8 if_bInterfaceClass; Wireless */
  95. 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
  96. 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
  97. 0x00, /* u8 if_iInterface; */
  98. /* endpoint one */
  99. 0x07, /* u8 ep_bLength; */
  100. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  101. USB_DIR_IN | USB_EVT_EP, /* u8 ep_bEndpointAddress; */
  102. 0x03, /* u8 ep_bmAttributes; Interrupt */
  103. 0x10, 0x00, /* u16 ep_wMaxPacketSize; */
  104. 0x02, /* u8 ep_bInterval; */
  105. /* endpoint two */
  106. 0x07, /* u8 ep_bLength; */
  107. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  108. USB_DIR_OUT | USB_ACL_EP, /* u8 ep_bEndpointAddress; */
  109. 0x02, /* u8 ep_bmAttributes; Bulk */
  110. 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
  111. 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  112. /* endpoint three */
  113. 0x07, /* u8 ep_bLength; */
  114. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  115. USB_DIR_IN | USB_ACL_EP, /* u8 ep_bEndpointAddress; */
  116. 0x02, /* u8 ep_bmAttributes; Bulk */
  117. 0x40, 0x00, /* u16 ep_wMaxPacketSize; */
  118. 0x0a, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  119. /* interface two setting one */
  120. 0x09, /* u8 if_bLength; */
  121. USB_DT_INTERFACE, /* u8 if_bDescriptorType; */
  122. 0x01, /* u8 if_bInterfaceNumber; */
  123. 0x00, /* u8 if_bAlternateSetting; */
  124. 0x02, /* u8 if_bNumEndpoints; */
  125. 0xe0, /* u8 if_bInterfaceClass; Wireless */
  126. 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
  127. 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
  128. 0x00, /* u8 if_iInterface; */
  129. /* endpoint one */
  130. 0x07, /* u8 ep_bLength; */
  131. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  132. USB_DIR_OUT | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  133. 0x01, /* u8 ep_bmAttributes; Isochronous */
  134. 0x00, 0x00, /* u16 ep_wMaxPacketSize; */
  135. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  136. /* endpoint two */
  137. 0x07, /* u8 ep_bLength; */
  138. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  139. USB_DIR_IN | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  140. 0x01, /* u8 ep_bmAttributes; Isochronous */
  141. 0x00, 0x00, /* u16 ep_wMaxPacketSize; */
  142. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  143. /* interface two setting two */
  144. 0x09, /* u8 if_bLength; */
  145. USB_DT_INTERFACE, /* u8 if_bDescriptorType; */
  146. 0x01, /* u8 if_bInterfaceNumber; */
  147. 0x01, /* u8 if_bAlternateSetting; */
  148. 0x02, /* u8 if_bNumEndpoints; */
  149. 0xe0, /* u8 if_bInterfaceClass; Wireless */
  150. 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
  151. 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
  152. 0x00, /* u8 if_iInterface; */
  153. /* endpoint one */
  154. 0x07, /* u8 ep_bLength; */
  155. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  156. USB_DIR_OUT | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  157. 0x01, /* u8 ep_bmAttributes; Isochronous */
  158. 0x09, 0x00, /* u16 ep_wMaxPacketSize; */
  159. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  160. /* endpoint two */
  161. 0x07, /* u8 ep_bLength; */
  162. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  163. USB_DIR_IN | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  164. 0x01, /* u8 ep_bmAttributes; Isochronous */
  165. 0x09, 0x00, /* u16 ep_wMaxPacketSize; */
  166. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  167. /* interface two setting three */
  168. 0x09, /* u8 if_bLength; */
  169. USB_DT_INTERFACE, /* u8 if_bDescriptorType; */
  170. 0x01, /* u8 if_bInterfaceNumber; */
  171. 0x02, /* u8 if_bAlternateSetting; */
  172. 0x02, /* u8 if_bNumEndpoints; */
  173. 0xe0, /* u8 if_bInterfaceClass; Wireless */
  174. 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
  175. 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
  176. 0x00, /* u8 if_iInterface; */
  177. /* endpoint one */
  178. 0x07, /* u8 ep_bLength; */
  179. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  180. USB_DIR_OUT | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  181. 0x01, /* u8 ep_bmAttributes; Isochronous */
  182. 0x11, 0x00, /* u16 ep_wMaxPacketSize; */
  183. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  184. /* endpoint two */
  185. 0x07, /* u8 ep_bLength; */
  186. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  187. USB_DIR_IN | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  188. 0x01, /* u8 ep_bmAttributes; Isochronous */
  189. 0x11, 0x00, /* u16 ep_wMaxPacketSize; */
  190. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  191. /* interface two setting four */
  192. 0x09, /* u8 if_bLength; */
  193. USB_DT_INTERFACE, /* u8 if_bDescriptorType; */
  194. 0x01, /* u8 if_bInterfaceNumber; */
  195. 0x03, /* u8 if_bAlternateSetting; */
  196. 0x02, /* u8 if_bNumEndpoints; */
  197. 0xe0, /* u8 if_bInterfaceClass; Wireless */
  198. 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
  199. 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
  200. 0x00, /* u8 if_iInterface; */
  201. /* endpoint one */
  202. 0x07, /* u8 ep_bLength; */
  203. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  204. USB_DIR_OUT | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  205. 0x01, /* u8 ep_bmAttributes; Isochronous */
  206. 0x19, 0x00, /* u16 ep_wMaxPacketSize; */
  207. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  208. /* endpoint two */
  209. 0x07, /* u8 ep_bLength; */
  210. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  211. USB_DIR_IN | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  212. 0x01, /* u8 ep_bmAttributes; Isochronous */
  213. 0x19, 0x00, /* u16 ep_wMaxPacketSize; */
  214. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  215. /* interface two setting five */
  216. 0x09, /* u8 if_bLength; */
  217. USB_DT_INTERFACE, /* u8 if_bDescriptorType; */
  218. 0x01, /* u8 if_bInterfaceNumber; */
  219. 0x04, /* u8 if_bAlternateSetting; */
  220. 0x02, /* u8 if_bNumEndpoints; */
  221. 0xe0, /* u8 if_bInterfaceClass; Wireless */
  222. 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
  223. 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
  224. 0x00, /* u8 if_iInterface; */
  225. /* endpoint one */
  226. 0x07, /* u8 ep_bLength; */
  227. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  228. USB_DIR_OUT | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  229. 0x01, /* u8 ep_bmAttributes; Isochronous */
  230. 0x21, 0x00, /* u16 ep_wMaxPacketSize; */
  231. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  232. /* endpoint two */
  233. 0x07, /* u8 ep_bLength; */
  234. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  235. USB_DIR_IN | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  236. 0x01, /* u8 ep_bmAttributes; Isochronous */
  237. 0x21, 0x00, /* u16 ep_wMaxPacketSize; */
  238. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  239. /* interface two setting six */
  240. 0x09, /* u8 if_bLength; */
  241. USB_DT_INTERFACE, /* u8 if_bDescriptorType; */
  242. 0x01, /* u8 if_bInterfaceNumber; */
  243. 0x05, /* u8 if_bAlternateSetting; */
  244. 0x02, /* u8 if_bNumEndpoints; */
  245. 0xe0, /* u8 if_bInterfaceClass; Wireless */
  246. 0x01, /* u8 if_bInterfaceSubClass; Radio Frequency */
  247. 0x01, /* u8 if_bInterfaceProtocol; Bluetooth */
  248. 0x00, /* u8 if_iInterface; */
  249. /* endpoint one */
  250. 0x07, /* u8 ep_bLength; */
  251. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  252. USB_DIR_OUT | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  253. 0x01, /* u8 ep_bmAttributes; Isochronous */
  254. 0x31, 0x00, /* u16 ep_wMaxPacketSize; */
  255. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  256. /* endpoint two */
  257. 0x07, /* u8 ep_bLength; */
  258. USB_DT_ENDPOINT, /* u8 ep_bDescriptorType; */
  259. USB_DIR_IN | USB_SCO_EP, /* u8 ep_bEndpointAddress; */
  260. 0x01, /* u8 ep_bmAttributes; Isochronous */
  261. 0x31, 0x00, /* u16 ep_wMaxPacketSize; */
  262. 0x01, /* u8 ep_bInterval; (255ms -- usb 2.0 spec) */
  263. /* If implemented, the DFU interface descriptor goes here with no
  264. * endpoints or alternative settings. */
  265. };
  266. static void usb_bt_fifo_reset(struct usb_hci_in_fifo_s *fifo)
  267. {
  268. fifo->dstart = 0;
  269. fifo->dlen = 0;
  270. fifo->dsize = DFIFO_LEN_MASK + 1;
  271. fifo->start = 0;
  272. fifo->len = 0;
  273. }
  274. static void usb_bt_fifo_enqueue(struct usb_hci_in_fifo_s *fifo,
  275. const uint8_t *data, int len)
  276. {
  277. int off = fifo->dstart + fifo->dlen;
  278. uint8_t *buf;
  279. fifo->dlen += len;
  280. if (off <= DFIFO_LEN_MASK) {
  281. if (off + len > DFIFO_LEN_MASK + 1 &&
  282. (fifo->dsize = off + len) > (DFIFO_LEN_MASK + 1) * 2) {
  283. fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
  284. exit(-1);
  285. }
  286. buf = fifo->data + off;
  287. } else {
  288. if (fifo->dlen > fifo->dsize) {
  289. fprintf(stderr, "%s: can't alloc %i bytes\n", __FUNCTION__, len);
  290. exit(-1);
  291. }
  292. buf = fifo->data + off - fifo->dsize;
  293. }
  294. off = (fifo->start + fifo->len ++) & CFIFO_LEN_MASK;
  295. fifo->fifo[off].data = memcpy(buf, data, len);
  296. fifo->fifo[off].len = len;
  297. }
  298. static inline int usb_bt_fifo_dequeue(struct usb_hci_in_fifo_s *fifo,
  299. USBPacket *p)
  300. {
  301. int len;
  302. if (likely(!fifo->len))
  303. return USB_RET_STALL;
  304. len = MIN(p->len, fifo->fifo[fifo->start].len);
  305. memcpy(p->data, fifo->fifo[fifo->start].data, len);
  306. if (len == p->len) {
  307. fifo->fifo[fifo->start].len -= len;
  308. fifo->fifo[fifo->start].data += len;
  309. } else {
  310. fifo->start ++;
  311. fifo->start &= CFIFO_LEN_MASK;
  312. fifo->len --;
  313. }
  314. fifo->dstart += len;
  315. fifo->dlen -= len;
  316. if (fifo->dstart >= fifo->dsize) {
  317. fifo->dstart = 0;
  318. fifo->dsize = DFIFO_LEN_MASK + 1;
  319. }
  320. return len;
  321. }
  322. static void inline usb_bt_fifo_out_enqueue(struct USBBtState *s,
  323. struct usb_hci_out_fifo_s *fifo,
  324. void (*send)(struct HCIInfo *, const uint8_t *, int),
  325. int (*complete)(const uint8_t *, int),
  326. const uint8_t *data, int len)
  327. {
  328. if (fifo->len) {
  329. memcpy(fifo->data + fifo->len, data, len);
  330. fifo->len += len;
  331. if (complete(fifo->data, fifo->len)) {
  332. send(s->hci, fifo->data, fifo->len);
  333. fifo->len = 0;
  334. }
  335. } else if (complete(data, len))
  336. send(s->hci, data, len);
  337. else {
  338. memcpy(fifo->data, data, len);
  339. fifo->len = len;
  340. }
  341. /* TODO: do we need to loop? */
  342. }
  343. static int usb_bt_hci_cmd_complete(const uint8_t *data, int len)
  344. {
  345. len -= HCI_COMMAND_HDR_SIZE;
  346. return len >= 0 &&
  347. len >= ((struct hci_command_hdr *) data)->plen;
  348. }
  349. static int usb_bt_hci_acl_complete(const uint8_t *data, int len)
  350. {
  351. len -= HCI_ACL_HDR_SIZE;
  352. return len >= 0 &&
  353. len >= le16_to_cpu(((struct hci_acl_hdr *) data)->dlen);
  354. }
  355. static int usb_bt_hci_sco_complete(const uint8_t *data, int len)
  356. {
  357. len -= HCI_SCO_HDR_SIZE;
  358. return len >= 0 &&
  359. len >= ((struct hci_sco_hdr *) data)->dlen;
  360. }
  361. static void usb_bt_handle_reset(USBDevice *dev)
  362. {
  363. struct USBBtState *s = (struct USBBtState *) dev->opaque;
  364. usb_bt_fifo_reset(&s->evt);
  365. usb_bt_fifo_reset(&s->acl);
  366. usb_bt_fifo_reset(&s->sco);
  367. s->outcmd.len = 0;
  368. s->outacl.len = 0;
  369. s->outsco.len = 0;
  370. s->altsetting = 0;
  371. }
  372. static int usb_bt_handle_control(USBDevice *dev, int request, int value,
  373. int index, int length, uint8_t *data)
  374. {
  375. struct USBBtState *s = (struct USBBtState *) dev->opaque;
  376. int ret = 0;
  377. switch (request) {
  378. case DeviceRequest | USB_REQ_GET_STATUS:
  379. case InterfaceRequest | USB_REQ_GET_STATUS:
  380. case EndpointRequest | USB_REQ_GET_STATUS:
  381. data[0] = (1 << USB_DEVICE_SELF_POWERED) |
  382. (dev->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP);
  383. data[1] = 0x00;
  384. ret = 2;
  385. break;
  386. case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
  387. case InterfaceOutRequest | USB_REQ_CLEAR_FEATURE:
  388. case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
  389. if (value == USB_DEVICE_REMOTE_WAKEUP) {
  390. dev->remote_wakeup = 0;
  391. } else {
  392. goto fail;
  393. }
  394. ret = 0;
  395. break;
  396. case DeviceOutRequest | USB_REQ_SET_FEATURE:
  397. case InterfaceOutRequest | USB_REQ_SET_FEATURE:
  398. case EndpointOutRequest | USB_REQ_SET_FEATURE:
  399. if (value == USB_DEVICE_REMOTE_WAKEUP) {
  400. dev->remote_wakeup = 1;
  401. } else {
  402. goto fail;
  403. }
  404. ret = 0;
  405. break;
  406. case DeviceOutRequest | USB_REQ_SET_ADDRESS:
  407. dev->addr = value;
  408. ret = 0;
  409. break;
  410. case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
  411. switch (value >> 8) {
  412. case USB_DT_DEVICE:
  413. ret = sizeof(qemu_bt_dev_descriptor);
  414. memcpy(data, qemu_bt_dev_descriptor, ret);
  415. break;
  416. case USB_DT_CONFIG:
  417. ret = sizeof(qemu_bt_config_descriptor);
  418. memcpy(data, qemu_bt_config_descriptor, ret);
  419. break;
  420. case USB_DT_STRING:
  421. switch(value & 0xff) {
  422. case 0:
  423. /* language ids */
  424. data[0] = 4;
  425. data[1] = 3;
  426. data[2] = 0x09;
  427. data[3] = 0x04;
  428. ret = 4;
  429. break;
  430. default:
  431. goto fail;
  432. }
  433. break;
  434. default:
  435. goto fail;
  436. }
  437. break;
  438. case DeviceRequest | USB_REQ_GET_CONFIGURATION:
  439. data[0] = qemu_bt_config_descriptor[0x5];
  440. ret = 1;
  441. s->config = 0;
  442. break;
  443. case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
  444. ret = 0;
  445. if (value != qemu_bt_config_descriptor[0x5] && value != 0) {
  446. printf("%s: Wrong SET_CONFIGURATION request (%i)\n",
  447. __FUNCTION__, value);
  448. goto fail;
  449. }
  450. s->config = 1;
  451. usb_bt_fifo_reset(&s->evt);
  452. usb_bt_fifo_reset(&s->acl);
  453. usb_bt_fifo_reset(&s->sco);
  454. break;
  455. case InterfaceRequest | USB_REQ_GET_INTERFACE:
  456. if (value != 0 || (index & ~1) || length != 1)
  457. goto fail;
  458. if (index == 1)
  459. data[0] = s->altsetting;
  460. else
  461. data[0] = 0;
  462. ret = 1;
  463. break;
  464. case InterfaceOutRequest | USB_REQ_SET_INTERFACE:
  465. if ((index & ~1) || length != 0 ||
  466. (index == 1 && (value < 0 || value > 4)) ||
  467. (index == 0 && value != 0)) {
  468. printf("%s: Wrong SET_INTERFACE request (%i, %i)\n",
  469. __FUNCTION__, index, value);
  470. goto fail;
  471. }
  472. s->altsetting = value;
  473. ret = 0;
  474. break;
  475. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_DEVICE) << 8):
  476. if (s->config)
  477. usb_bt_fifo_out_enqueue(s, &s->outcmd, s->hci->cmd_send,
  478. usb_bt_hci_cmd_complete, data, length);
  479. break;
  480. default:
  481. fail:
  482. ret = USB_RET_STALL;
  483. break;
  484. }
  485. return ret;
  486. }
  487. static int usb_bt_handle_data(USBDevice *dev, USBPacket *p)
  488. {
  489. struct USBBtState *s = (struct USBBtState *) dev->opaque;
  490. int ret = 0;
  491. if (!s->config)
  492. goto fail;
  493. switch (p->pid) {
  494. case USB_TOKEN_IN:
  495. switch (p->devep & 0xf) {
  496. case USB_EVT_EP:
  497. ret = usb_bt_fifo_dequeue(&s->evt, p);
  498. break;
  499. case USB_ACL_EP:
  500. ret = usb_bt_fifo_dequeue(&s->acl, p);
  501. break;
  502. case USB_SCO_EP:
  503. ret = usb_bt_fifo_dequeue(&s->sco, p);
  504. break;
  505. default:
  506. goto fail;
  507. }
  508. break;
  509. case USB_TOKEN_OUT:
  510. switch (p->devep & 0xf) {
  511. case USB_ACL_EP:
  512. usb_bt_fifo_out_enqueue(s, &s->outacl, s->hci->acl_send,
  513. usb_bt_hci_acl_complete, p->data, p->len);
  514. break;
  515. case USB_SCO_EP:
  516. usb_bt_fifo_out_enqueue(s, &s->outsco, s->hci->sco_send,
  517. usb_bt_hci_sco_complete, p->data, p->len);
  518. break;
  519. default:
  520. goto fail;
  521. }
  522. break;
  523. default:
  524. fail:
  525. ret = USB_RET_STALL;
  526. break;
  527. }
  528. return ret;
  529. }
  530. static void usb_bt_out_hci_packet_event(void *opaque,
  531. const uint8_t *data, int len)
  532. {
  533. struct USBBtState *s = (struct USBBtState *) opaque;
  534. usb_bt_fifo_enqueue(&s->evt, data, len);
  535. }
  536. static void usb_bt_out_hci_packet_acl(void *opaque,
  537. const uint8_t *data, int len)
  538. {
  539. struct USBBtState *s = (struct USBBtState *) opaque;
  540. usb_bt_fifo_enqueue(&s->acl, data, len);
  541. }
  542. static void usb_bt_handle_destroy(USBDevice *dev)
  543. {
  544. struct USBBtState *s = (struct USBBtState *) dev->opaque;
  545. s->hci->opaque = 0;
  546. s->hci->evt_recv = 0;
  547. s->hci->acl_recv = 0;
  548. qemu_free(s);
  549. }
  550. USBDevice *usb_bt_init(HCIInfo *hci)
  551. {
  552. struct USBBtState *s;
  553. if (!hci)
  554. return NULL;
  555. s = qemu_mallocz(sizeof(struct USBBtState));
  556. s->dev.opaque = s;
  557. s->dev.speed = USB_SPEED_HIGH;
  558. s->dev.handle_packet = usb_generic_handle_packet;
  559. pstrcpy(s->dev.devname, sizeof(s->dev.devname), "QEMU BT dongle");
  560. s->dev.handle_reset = usb_bt_handle_reset;
  561. s->dev.handle_control = usb_bt_handle_control;
  562. s->dev.handle_data = usb_bt_handle_data;
  563. s->dev.handle_destroy = usb_bt_handle_destroy;
  564. s->hci = hci;
  565. s->hci->opaque = s;
  566. s->hci->evt_recv = usb_bt_out_hci_packet_event;
  567. s->hci->acl_recv = usb_bt_out_hci_packet_acl;
  568. usb_bt_handle_reset(&s->dev);
  569. return &s->dev;
  570. }