hciattach_st.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2005-2010 Marcel Holtmann <marcel@holtmann.org>
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <dirent.h>
  21. #include <sys/param.h>
  22. #include "lib/bluetooth.h"
  23. #include "hciattach.h"
  24. static int debug = 0;
  25. static int do_command(int fd, uint8_t ogf, uint16_t ocf,
  26. uint8_t *cparam, int clen, uint8_t *rparam, int rlen)
  27. {
  28. //uint16_t opcode = (uint16_t) ((ocf & 0x03ff) | (ogf << 10));
  29. unsigned char cp[260], rp[260];
  30. int len, size, offset = 3;
  31. cp[0] = 0x01;
  32. cp[1] = ocf & 0xff;
  33. cp[2] = ogf << 2 | ocf >> 8;
  34. cp[3] = clen;
  35. if (clen > 0)
  36. memcpy(cp + 4, cparam, clen);
  37. if (debug) {
  38. int i;
  39. printf("[<");
  40. for (i = 0; i < clen + 4; i++)
  41. printf(" %02x", cp[i]);
  42. printf("]\n");
  43. }
  44. if (write(fd, cp, clen + 4) < 0)
  45. return -1;
  46. do {
  47. if (read(fd, rp, 1) < 1)
  48. return -1;
  49. } while (rp[0] != 0x04);
  50. if (read(fd, rp + 1, 2) < 2)
  51. return -1;
  52. do {
  53. len = read(fd, rp + offset, sizeof(rp) - offset);
  54. offset += len;
  55. } while (offset < rp[2] + 3);
  56. if (debug) {
  57. int i;
  58. printf("[>");
  59. for (i = 0; i < offset; i++)
  60. printf(" %02x", rp[i]);
  61. printf("]\n");
  62. }
  63. if (rp[0] != 0x04) {
  64. errno = EIO;
  65. return -1;
  66. }
  67. switch (rp[1]) {
  68. case 0x0e: /* command complete */
  69. if (rp[6] != 0x00)
  70. return -ENXIO;
  71. offset = 3 + 4;
  72. size = rp[2] - 4;
  73. break;
  74. case 0x0f: /* command status */
  75. /* fall through */
  76. default:
  77. offset = 3;
  78. size = rp[2];
  79. break;
  80. }
  81. if (!rparam || rlen < size)
  82. return -ENXIO;
  83. memcpy(rparam, rp + offset, size);
  84. return size;
  85. }
  86. static int load_file(int dd, uint16_t version, const char *suffix)
  87. {
  88. DIR *dir;
  89. struct dirent *d;
  90. char pathname[PATH_MAX], filename[PATH_MAX + NAME_MAX + 1], prefix[20];
  91. unsigned char cmd[256];
  92. unsigned char buf[256];
  93. uint8_t seqnum = 0;
  94. int fd, size, len, found_fw_file;
  95. memset(filename, 0, sizeof(filename));
  96. snprintf(prefix, sizeof(prefix), "STLC2500_R%d_%02d_",
  97. version >> 8, version & 0xff);
  98. strcpy(pathname, "/lib/firmware");
  99. dir = opendir(pathname);
  100. if (!dir) {
  101. strcpy(pathname, ".");
  102. dir = opendir(pathname);
  103. if (!dir)
  104. return -errno;
  105. }
  106. found_fw_file = 0;
  107. while (1) {
  108. d = readdir(dir);
  109. if (!d)
  110. break;
  111. if (strncmp(d->d_name + strlen(d->d_name) - strlen(suffix),
  112. suffix, strlen(suffix)))
  113. continue;
  114. if (strncmp(d->d_name, prefix, strlen(prefix)))
  115. continue;
  116. snprintf(filename, sizeof(filename), "%s/%s",
  117. pathname, d->d_name);
  118. found_fw_file = 1;
  119. }
  120. closedir(dir);
  121. if (!found_fw_file)
  122. return -ENOENT;
  123. printf("Loading file %s\n", filename);
  124. fd = open(filename, O_RDONLY);
  125. if (fd < 0) {
  126. perror("Can't open firmware file");
  127. return -errno;
  128. }
  129. while (1) {
  130. size = read(fd, cmd + 1, 254);
  131. if (size <= 0)
  132. break;
  133. cmd[0] = seqnum;
  134. len = do_command(dd, 0xff, 0x002e, cmd, size + 1, buf, sizeof(buf));
  135. if (len < 1)
  136. break;
  137. if (buf[0] != seqnum) {
  138. fprintf(stderr, "Sequence number mismatch\n");
  139. break;
  140. }
  141. seqnum++;
  142. }
  143. close(fd);
  144. return 0;
  145. }
  146. int stlc2500_init(int dd, bdaddr_t *bdaddr)
  147. {
  148. unsigned char cmd[16];
  149. unsigned char buf[254];
  150. uint16_t version;
  151. int len;
  152. int err;
  153. /* Hci_Cmd_Ericsson_Read_Revision_Information */
  154. len = do_command(dd, 0xff, 0x000f, NULL, 0, buf, sizeof(buf));
  155. if (len < 0)
  156. return -1;
  157. printf("%s\n", buf);
  158. /* HCI_Read_Local_Version_Information */
  159. len = do_command(dd, 0x04, 0x0001, NULL, 0, buf, sizeof(buf));
  160. if (len < 0)
  161. return -1;
  162. version = buf[2] << 8 | buf[1];
  163. err = load_file(dd, version, ".ptc");
  164. if (err < 0) {
  165. if (err == -ENOENT)
  166. fprintf(stderr, "No ROM patch file loaded.\n");
  167. else
  168. return -1;
  169. }
  170. err = load_file(dd, buf[2] << 8 | buf[1], ".ssf");
  171. if (err < 0) {
  172. if (err == -ENOENT)
  173. fprintf(stderr, "No static settings file loaded.\n");
  174. else
  175. return -1;
  176. }
  177. cmd[0] = 0xfe;
  178. cmd[1] = 0x06;
  179. bacpy((bdaddr_t *) (cmd + 2), bdaddr);
  180. /* Hci_Cmd_ST_Store_In_NVDS */
  181. len = do_command(dd, 0xff, 0x0022, cmd, 8, buf, sizeof(buf));
  182. if (len < 0)
  183. return -1;
  184. /* HCI_Reset : applies parameters*/
  185. len = do_command(dd, 0x03, 0x0003, NULL, 0, buf, sizeof(buf));
  186. if (len < 0)
  187. return -1;
  188. return 0;
  189. }
  190. int bgb2xx_init(int dd, bdaddr_t *bdaddr)
  191. {
  192. unsigned char cmd[16];
  193. unsigned char buf[254];
  194. int len;
  195. len = do_command(dd, 0xff, 0x000f, NULL, 0, buf, sizeof(buf));
  196. if (len < 0)
  197. return -1;
  198. printf("%s\n", buf);
  199. cmd[0] = 0xfe;
  200. cmd[1] = 0x06;
  201. bacpy((bdaddr_t *) (cmd + 2), bdaddr);
  202. len = do_command(dd, 0xff, 0x0022, cmd, 8, buf, sizeof(buf));
  203. if (len < 0)
  204. return -1;
  205. len = do_command(dd, 0x03, 0x0003, NULL, 0, buf, sizeof(buf));
  206. if (len < 0)
  207. return -1;
  208. return 0;
  209. }