hci-csr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /*
  2. * Bluetooth serial HCI transport.
  3. * CSR41814 HCI with H4p vendor extensions.
  4. *
  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 "chardev/char-serial.h"
  24. #include "qemu/timer.h"
  25. #include "qemu/bswap.h"
  26. #include "hw/irq.h"
  27. #include "sysemu/bt.h"
  28. #include "hw/bt.h"
  29. #include "qapi/error.h"
  30. struct csrhci_s {
  31. Chardev parent;
  32. int enable;
  33. qemu_irq *pins;
  34. int pin_state;
  35. int modem_state;
  36. #define FIFO_LEN 4096
  37. int out_start;
  38. int out_len;
  39. int out_size;
  40. uint8_t outfifo[FIFO_LEN * 2];
  41. uint8_t inpkt[FIFO_LEN];
  42. enum {
  43. CSR_HDR_LEN,
  44. CSR_DATA_LEN,
  45. CSR_DATA
  46. } in_state;
  47. int in_len;
  48. int in_hdr;
  49. int in_needed;
  50. QEMUTimer *out_tm;
  51. int64_t baud_delay;
  52. bdaddr_t bd_addr;
  53. struct HCIInfo *hci;
  54. };
  55. #define TYPE_CHARDEV_HCI "chardev-hci"
  56. #define HCI_CHARDEV(obj) OBJECT_CHECK(struct csrhci_s, (obj), TYPE_CHARDEV_HCI)
  57. /* H4+ packet types */
  58. enum {
  59. H4_CMD_PKT = 1,
  60. H4_ACL_PKT = 2,
  61. H4_SCO_PKT = 3,
  62. H4_EVT_PKT = 4,
  63. H4_NEG_PKT = 6,
  64. H4_ALIVE_PKT = 7,
  65. };
  66. /* CSR41814 negotiation start magic packet */
  67. static const uint8_t csrhci_neg_packet[] = {
  68. H4_NEG_PKT, 10,
  69. 0x00, 0xa0, 0x01, 0x00, 0x00,
  70. 0x4c, 0x00, 0x96, 0x00, 0x00,
  71. };
  72. /* CSR41814 vendor-specific command OCFs */
  73. enum {
  74. OCF_CSR_SEND_FIRMWARE = 0x000,
  75. };
  76. static inline void csrhci_fifo_wake(struct csrhci_s *s)
  77. {
  78. Chardev *chr = CHARDEV(s);
  79. if (!s->enable || !s->out_len)
  80. return;
  81. /* XXX: Should wait for s->modem_state & CHR_TIOCM_RTS? */
  82. if (qemu_chr_be_can_write(chr)) {
  83. qemu_chr_be_write(chr, s->outfifo + s->out_start++, 1);
  84. s->out_len--;
  85. if (s->out_start >= s->out_size) {
  86. s->out_start = 0;
  87. s->out_size = FIFO_LEN;
  88. }
  89. }
  90. if (s->out_len)
  91. timer_mod(s->out_tm, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->baud_delay);
  92. }
  93. #define csrhci_out_packetz(s, len) memset(csrhci_out_packet(s, len), 0, len)
  94. static uint8_t *csrhci_out_packet(struct csrhci_s *s, int len)
  95. {
  96. int off = s->out_start + s->out_len;
  97. /* TODO: do the padding here, i.e. align len */
  98. s->out_len += len;
  99. if (off < FIFO_LEN) {
  100. if (off + len > FIFO_LEN && (s->out_size = off + len) > FIFO_LEN * 2) {
  101. error_report("%s: can't alloc %i bytes", __func__, len);
  102. exit(-1);
  103. }
  104. return s->outfifo + off;
  105. }
  106. if (s->out_len > s->out_size) {
  107. error_report("%s: can't alloc %i bytes", __func__, len);
  108. exit(-1);
  109. }
  110. return s->outfifo + off - s->out_size;
  111. }
  112. static inline uint8_t *csrhci_out_packet_csr(struct csrhci_s *s,
  113. int type, int len)
  114. {
  115. uint8_t *ret = csrhci_out_packetz(s, len + 2);
  116. *ret ++ = type;
  117. *ret ++ = len;
  118. return ret;
  119. }
  120. static inline uint8_t *csrhci_out_packet_event(struct csrhci_s *s,
  121. int evt, int len)
  122. {
  123. uint8_t *ret = csrhci_out_packetz(s,
  124. len + 1 + sizeof(struct hci_event_hdr));
  125. *ret ++ = H4_EVT_PKT;
  126. ((struct hci_event_hdr *) ret)->evt = evt;
  127. ((struct hci_event_hdr *) ret)->plen = len;
  128. return ret + sizeof(struct hci_event_hdr);
  129. }
  130. static void csrhci_in_packet_vendor(struct csrhci_s *s, int ocf,
  131. uint8_t *data, int len)
  132. {
  133. int offset;
  134. uint8_t *rpkt;
  135. switch (ocf) {
  136. case OCF_CSR_SEND_FIRMWARE:
  137. /* Check if this is the bd_address packet */
  138. if (len >= 18 + 8 && data[12] == 0x01 && data[13] == 0x00) {
  139. offset = 18;
  140. s->bd_addr.b[0] = data[offset + 7]; /* Beyond cmd packet end(!?) */
  141. s->bd_addr.b[1] = data[offset + 6];
  142. s->bd_addr.b[2] = data[offset + 4];
  143. s->bd_addr.b[3] = data[offset + 0];
  144. s->bd_addr.b[4] = data[offset + 3];
  145. s->bd_addr.b[5] = data[offset + 2];
  146. s->hci->bdaddr_set(s->hci, s->bd_addr.b);
  147. error_report("%s: bd_address loaded from firmware: "
  148. "%02x:%02x:%02x:%02x:%02x:%02x", __func__,
  149. s->bd_addr.b[0], s->bd_addr.b[1], s->bd_addr.b[2],
  150. s->bd_addr.b[3], s->bd_addr.b[4], s->bd_addr.b[5]);
  151. }
  152. rpkt = csrhci_out_packet_event(s, EVT_VENDOR, 11);
  153. /* Status bytes: no error */
  154. rpkt[9] = 0x00;
  155. rpkt[10] = 0x00;
  156. break;
  157. default:
  158. error_report("%s: got a bad CMD packet", __func__);
  159. return;
  160. }
  161. csrhci_fifo_wake(s);
  162. }
  163. static void csrhci_in_packet(struct csrhci_s *s, uint8_t *pkt)
  164. {
  165. uint8_t *rpkt;
  166. int opc;
  167. switch (*pkt ++) {
  168. case H4_CMD_PKT:
  169. opc = le16_to_cpu(((struct hci_command_hdr *) pkt)->opcode);
  170. if (cmd_opcode_ogf(opc) == OGF_VENDOR_CMD) {
  171. csrhci_in_packet_vendor(s, cmd_opcode_ocf(opc),
  172. pkt + sizeof(struct hci_command_hdr),
  173. s->in_len - sizeof(struct hci_command_hdr) - 1);
  174. return;
  175. }
  176. /* TODO: if the command is OCF_READ_LOCAL_COMMANDS or the likes,
  177. * we need to send it to the HCI layer and then add our supported
  178. * commands to the returned mask (such as OGF_VENDOR_CMD). With
  179. * bt-hci.c we could just have hooks for this kind of commands but
  180. * we can't with bt-host.c. */
  181. s->hci->cmd_send(s->hci, pkt, s->in_len - 1);
  182. break;
  183. case H4_EVT_PKT:
  184. goto bad_pkt;
  185. case H4_ACL_PKT:
  186. s->hci->acl_send(s->hci, pkt, s->in_len - 1);
  187. break;
  188. case H4_SCO_PKT:
  189. s->hci->sco_send(s->hci, pkt, s->in_len - 1);
  190. break;
  191. case H4_NEG_PKT:
  192. if (s->in_hdr != sizeof(csrhci_neg_packet) ||
  193. memcmp(pkt - 1, csrhci_neg_packet, s->in_hdr)) {
  194. error_report("%s: got a bad NEG packet", __func__);
  195. return;
  196. }
  197. pkt += 2;
  198. rpkt = csrhci_out_packet_csr(s, H4_NEG_PKT, 10);
  199. *rpkt ++ = 0x20; /* Operational settings negotiation Ok */
  200. memcpy(rpkt, pkt, 7); rpkt += 7;
  201. *rpkt ++ = 0xff;
  202. *rpkt = 0xff;
  203. break;
  204. case H4_ALIVE_PKT:
  205. if (s->in_hdr != 4 || pkt[1] != 0x55 || pkt[2] != 0x00) {
  206. error_report("%s: got a bad ALIVE packet", __func__);
  207. return;
  208. }
  209. rpkt = csrhci_out_packet_csr(s, H4_ALIVE_PKT, 2);
  210. *rpkt ++ = 0xcc;
  211. *rpkt = 0x00;
  212. break;
  213. default:
  214. bad_pkt:
  215. /* TODO: error out */
  216. error_report("%s: got a bad packet", __func__);
  217. break;
  218. }
  219. csrhci_fifo_wake(s);
  220. }
  221. static int csrhci_header_len(const uint8_t *pkt)
  222. {
  223. switch (pkt[0]) {
  224. case H4_CMD_PKT:
  225. return HCI_COMMAND_HDR_SIZE;
  226. case H4_EVT_PKT:
  227. return HCI_EVENT_HDR_SIZE;
  228. case H4_ACL_PKT:
  229. return HCI_ACL_HDR_SIZE;
  230. case H4_SCO_PKT:
  231. return HCI_SCO_HDR_SIZE;
  232. case H4_NEG_PKT:
  233. return pkt[1] + 1;
  234. case H4_ALIVE_PKT:
  235. return 3;
  236. }
  237. exit(-1);
  238. }
  239. static int csrhci_data_len(const uint8_t *pkt)
  240. {
  241. switch (*pkt ++) {
  242. case H4_CMD_PKT:
  243. /* It seems that vendor-specific command packets for H4+ are all
  244. * one byte longer than indicated in the standard header. */
  245. if (le16_to_cpu(((struct hci_command_hdr *) pkt)->opcode) == 0xfc00)
  246. return (((struct hci_command_hdr *) pkt)->plen + 1) & ~1;
  247. return ((struct hci_command_hdr *) pkt)->plen;
  248. case H4_EVT_PKT:
  249. return ((struct hci_event_hdr *) pkt)->plen;
  250. case H4_ACL_PKT:
  251. return le16_to_cpu(((struct hci_acl_hdr *) pkt)->dlen);
  252. case H4_SCO_PKT:
  253. return ((struct hci_sco_hdr *) pkt)->dlen;
  254. case H4_NEG_PKT:
  255. case H4_ALIVE_PKT:
  256. return 0;
  257. }
  258. exit(-1);
  259. }
  260. static void csrhci_ready_for_next_inpkt(struct csrhci_s *s)
  261. {
  262. s->in_state = CSR_HDR_LEN;
  263. s->in_len = 0;
  264. s->in_needed = 2;
  265. s->in_hdr = INT_MAX;
  266. }
  267. static int csrhci_write(struct Chardev *chr,
  268. const uint8_t *buf, int len)
  269. {
  270. struct csrhci_s *s = (struct csrhci_s *)chr;
  271. int total = 0;
  272. if (!s->enable)
  273. return 0;
  274. for (;;) {
  275. int cnt = MIN(len, s->in_needed - s->in_len);
  276. if (cnt) {
  277. memcpy(s->inpkt + s->in_len, buf, cnt);
  278. s->in_len += cnt;
  279. buf += cnt;
  280. len -= cnt;
  281. total += cnt;
  282. }
  283. if (s->in_len < s->in_needed) {
  284. break;
  285. }
  286. if (s->in_state == CSR_HDR_LEN) {
  287. s->in_hdr = csrhci_header_len(s->inpkt) + 1;
  288. assert(s->in_hdr >= s->in_needed);
  289. s->in_needed = s->in_hdr;
  290. s->in_state = CSR_DATA_LEN;
  291. continue;
  292. }
  293. if (s->in_state == CSR_DATA_LEN) {
  294. s->in_needed += csrhci_data_len(s->inpkt);
  295. /* hci_acl_hdr could specify more than 4096 bytes, so assert. */
  296. assert(s->in_needed <= sizeof(s->inpkt));
  297. s->in_state = CSR_DATA;
  298. continue;
  299. }
  300. if (s->in_state == CSR_DATA) {
  301. csrhci_in_packet(s, s->inpkt);
  302. csrhci_ready_for_next_inpkt(s);
  303. }
  304. }
  305. return total;
  306. }
  307. static void csrhci_out_hci_packet_event(void *opaque,
  308. const uint8_t *data, int len)
  309. {
  310. struct csrhci_s *s = (struct csrhci_s *) opaque;
  311. uint8_t *pkt = csrhci_out_packet(s, (len + 2) & ~1); /* Align */
  312. *pkt ++ = H4_EVT_PKT;
  313. memcpy(pkt, data, len);
  314. csrhci_fifo_wake(s);
  315. }
  316. static void csrhci_out_hci_packet_acl(void *opaque,
  317. const uint8_t *data, int len)
  318. {
  319. struct csrhci_s *s = (struct csrhci_s *) opaque;
  320. uint8_t *pkt = csrhci_out_packet(s, (len + 2) & ~1); /* Align */
  321. *pkt ++ = H4_ACL_PKT;
  322. pkt[len & ~1] = 0;
  323. memcpy(pkt, data, len);
  324. csrhci_fifo_wake(s);
  325. }
  326. static int csrhci_ioctl(struct Chardev *chr, int cmd, void *arg)
  327. {
  328. QEMUSerialSetParams *ssp;
  329. struct csrhci_s *s = (struct csrhci_s *) chr;
  330. int prev_state = s->modem_state;
  331. switch (cmd) {
  332. case CHR_IOCTL_SERIAL_SET_PARAMS:
  333. ssp = (QEMUSerialSetParams *) arg;
  334. s->baud_delay = NANOSECONDS_PER_SECOND / ssp->speed;
  335. /* Moments later... (but shorter than 100ms) */
  336. s->modem_state |= CHR_TIOCM_CTS;
  337. break;
  338. case CHR_IOCTL_SERIAL_GET_TIOCM:
  339. *(int *) arg = s->modem_state;
  340. break;
  341. case CHR_IOCTL_SERIAL_SET_TIOCM:
  342. s->modem_state = *(int *) arg;
  343. if (~s->modem_state & prev_state & CHR_TIOCM_RTS)
  344. s->modem_state &= ~CHR_TIOCM_CTS;
  345. break;
  346. default:
  347. return -ENOTSUP;
  348. }
  349. return 0;
  350. }
  351. static void csrhci_reset(struct csrhci_s *s)
  352. {
  353. s->out_len = 0;
  354. s->out_size = FIFO_LEN;
  355. csrhci_ready_for_next_inpkt(s);
  356. s->baud_delay = NANOSECONDS_PER_SECOND;
  357. s->enable = 0;
  358. s->modem_state = 0;
  359. /* After a while... (but sooner than 10ms) */
  360. s->modem_state |= CHR_TIOCM_CTS;
  361. memset(&s->bd_addr, 0, sizeof(bdaddr_t));
  362. }
  363. static void csrhci_out_tick(void *opaque)
  364. {
  365. csrhci_fifo_wake((struct csrhci_s *) opaque);
  366. }
  367. static void csrhci_pins(void *opaque, int line, int level)
  368. {
  369. struct csrhci_s *s = (struct csrhci_s *) opaque;
  370. int state = s->pin_state;
  371. s->pin_state &= ~(1 << line);
  372. s->pin_state |= (!!level) << line;
  373. if ((state & ~s->pin_state) & (1 << csrhci_pin_reset)) {
  374. /* TODO: Disappear from lower layers */
  375. csrhci_reset(s);
  376. }
  377. if (s->pin_state == 3 && state != 3) {
  378. s->enable = 1;
  379. /* TODO: Wake lower layers up */
  380. }
  381. }
  382. qemu_irq *csrhci_pins_get(Chardev *chr)
  383. {
  384. struct csrhci_s *s = (struct csrhci_s *) chr;
  385. return s->pins;
  386. }
  387. static void csrhci_open(Chardev *chr,
  388. ChardevBackend *backend,
  389. bool *be_opened,
  390. Error **errp)
  391. {
  392. struct csrhci_s *s = HCI_CHARDEV(chr);
  393. s->hci = qemu_next_hci();
  394. s->hci->opaque = s;
  395. s->hci->evt_recv = csrhci_out_hci_packet_event;
  396. s->hci->acl_recv = csrhci_out_hci_packet_acl;
  397. s->out_tm = timer_new_ns(QEMU_CLOCK_VIRTUAL, csrhci_out_tick, s);
  398. s->pins = qemu_allocate_irqs(csrhci_pins, s, __csrhci_pins);
  399. csrhci_reset(s);
  400. *be_opened = false;
  401. }
  402. static void char_hci_class_init(ObjectClass *oc, void *data)
  403. {
  404. ChardevClass *cc = CHARDEV_CLASS(oc);
  405. cc->internal = true;
  406. cc->open = csrhci_open;
  407. cc->chr_write = csrhci_write;
  408. cc->chr_ioctl = csrhci_ioctl;
  409. }
  410. static const TypeInfo char_hci_type_info = {
  411. .name = TYPE_CHARDEV_HCI,
  412. .parent = TYPE_CHARDEV,
  413. .instance_size = sizeof(struct csrhci_s),
  414. .class_init = char_hci_class_init,
  415. };
  416. Chardev *uart_hci_init(void)
  417. {
  418. return qemu_chardev_new(NULL, TYPE_CHARDEV_HCI,
  419. NULL, NULL, &error_abort);
  420. }
  421. static void register_types(void)
  422. {
  423. type_register_static(&char_hci_type_info);
  424. }
  425. type_init(register_types);