hciattach_ti.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2007-2008 Texas Instruments, Inc.
  7. * Copyright (C) 2005-2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #define _GNU_SOURCE
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <termios.h>
  20. #include <time.h>
  21. #include <sys/time.h>
  22. #include <sys/types.h>
  23. #include <sys/param.h>
  24. #include <sys/ioctl.h>
  25. #include "lib/bluetooth.h"
  26. #include "lib/hci.h"
  27. #include "lib/hci_lib.h"
  28. #include "hciattach.h"
  29. #ifdef HCIATTACH_DEBUG
  30. #define DPRINTF(x...) printf(x)
  31. #else
  32. #define DPRINTF(x...)
  33. #endif
  34. #define HCIUARTGETDEVICE _IOR('U', 202, int)
  35. #define MAKEWORD(a, b) ((uint16_t)(((uint8_t)(a)) | ((uint16_t)((uint8_t)(b))) << 8))
  36. #define TI_MANUFACTURER_ID 13
  37. #define FIRMWARE_DIRECTORY "/lib/firmware/ti-connectivity/"
  38. #define ACTION_SEND_COMMAND 1
  39. #define ACTION_WAIT_EVENT 2
  40. #define ACTION_SERIAL 3
  41. #define ACTION_DELAY 4
  42. #define ACTION_RUN_SCRIPT 5
  43. #define ACTION_REMARKS 6
  44. #define BRF_DEEP_SLEEP_OPCODE_BYTE_1 0x0c
  45. #define BRF_DEEP_SLEEP_OPCODE_BYTE_2 0xfd
  46. #define BRF_DEEP_SLEEP_OPCODE \
  47. (BRF_DEEP_SLEEP_OPCODE_BYTE_1 | (BRF_DEEP_SLEEP_OPCODE_BYTE_2 << 8))
  48. #define FILE_HEADER_MAGIC 0x42535442
  49. /*
  50. * BRF Firmware header
  51. */
  52. struct bts_header {
  53. uint32_t magic;
  54. uint32_t version;
  55. uint8_t future[24];
  56. uint8_t actions[0];
  57. }__attribute__ ((packed));
  58. /*
  59. * BRF Actions structure
  60. */
  61. struct bts_action {
  62. uint16_t type;
  63. uint16_t size;
  64. uint8_t data[0];
  65. } __attribute__ ((packed));
  66. struct bts_action_send {
  67. uint8_t data[0];
  68. } __attribute__ ((packed));
  69. struct bts_action_wait {
  70. uint32_t msec;
  71. uint32_t size;
  72. uint8_t data[0];
  73. }__attribute__ ((packed));
  74. struct bts_action_delay {
  75. uint32_t msec;
  76. }__attribute__ ((packed));
  77. struct bts_action_serial {
  78. uint32_t baud;
  79. uint32_t flow_control;
  80. }__attribute__ ((packed));
  81. static FILE *bts_load_script(const char *file_name, uint32_t *version)
  82. {
  83. struct bts_header header;
  84. FILE *fp;
  85. fp = fopen(file_name, "rb");
  86. if (!fp) {
  87. perror("can't open firmware file");
  88. return NULL;
  89. }
  90. if (1 != fread(&header, sizeof(struct bts_header), 1, fp)) {
  91. perror("can't read firmware file");
  92. goto errclose;
  93. }
  94. if (header.magic != FILE_HEADER_MAGIC) {
  95. fprintf(stderr, "%s not a legal TI firmware file\n", file_name);
  96. goto errclose;
  97. }
  98. if (NULL != version)
  99. *version = header.version;
  100. return fp;
  101. errclose:
  102. fclose(fp);
  103. return NULL;
  104. }
  105. static unsigned long bts_fetch_action(FILE *fp, unsigned char *action_buf,
  106. unsigned long buf_size, uint16_t *action_type)
  107. {
  108. struct bts_action action_hdr;
  109. unsigned long nread;
  110. if (!fp)
  111. return 0;
  112. if (1 != fread(&action_hdr, sizeof(struct bts_action), 1, fp))
  113. return 0;
  114. if (action_hdr.size > buf_size) {
  115. fprintf(stderr, "bts_next_action: not enough space to read next action\n");
  116. return 0;
  117. }
  118. nread = fread(action_buf, sizeof(uint8_t), action_hdr.size, fp);
  119. if (nread != (action_hdr.size)) {
  120. fprintf(stderr, "bts_next_action: fread failed to read next action\n");
  121. return 0;
  122. }
  123. *action_type = action_hdr.type;
  124. return nread * sizeof(uint8_t);
  125. }
  126. static void bts_unload_script(FILE *fp)
  127. {
  128. if (fp)
  129. fclose(fp);
  130. }
  131. static int is_it_texas(const uint8_t *respond)
  132. {
  133. uint16_t manufacturer_id;
  134. manufacturer_id = MAKEWORD(respond[11], respond[12]);
  135. return TI_MANUFACTURER_ID == manufacturer_id ? 1 : 0;
  136. }
  137. static const char *get_firmware_name(const uint8_t *respond)
  138. {
  139. static char firmware_file_name[PATH_MAX] = {0};
  140. uint16_t version = 0, chip = 0, min_ver = 0, maj_ver = 0;
  141. version = MAKEWORD(respond[13], respond[14]);
  142. chip = (version & 0x7C00) >> 10;
  143. min_ver = (version & 0x007F);
  144. maj_ver = (version & 0x0380) >> 7;
  145. if (version & 0x8000)
  146. maj_ver |= 0x0008;
  147. sprintf(firmware_file_name, FIRMWARE_DIRECTORY "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
  148. return firmware_file_name;
  149. }
  150. static void brf_delay(struct bts_action_delay *delay)
  151. {
  152. usleep(1000 * delay->msec);
  153. }
  154. static int brf_set_serial_params(struct bts_action_serial *serial_action,
  155. int fd, int *speed, struct termios *ti)
  156. {
  157. fprintf(stderr, "texas: changing baud rate to %u, flow control to %u\n",
  158. serial_action->baud, serial_action->flow_control );
  159. tcflush(fd, TCIOFLUSH);
  160. if (serial_action->flow_control)
  161. ti->c_cflag |= CRTSCTS;
  162. else
  163. ti->c_cflag &= ~CRTSCTS;
  164. if (tcsetattr(fd, TCSANOW, ti) < 0) {
  165. perror("Can't set port settings");
  166. return -1;
  167. }
  168. tcflush(fd, TCIOFLUSH);
  169. if (set_speed(fd, ti, serial_action->baud) < 0) {
  170. perror("Can't set baud rate");
  171. return -1;
  172. }
  173. if (speed)
  174. *speed = serial_action->baud;
  175. return 0;
  176. }
  177. static int brf_send_command_socket(int fd, struct bts_action_send *send_action)
  178. {
  179. char response[1024] = {0};
  180. hci_command_hdr *cmd = (hci_command_hdr *) send_action->data;
  181. uint16_t opcode = cmd->opcode;
  182. struct hci_request rq;
  183. memset(&rq, 0, sizeof(rq));
  184. rq.ogf = cmd_opcode_ogf(opcode);
  185. rq.ocf = cmd_opcode_ocf(opcode);
  186. rq.event = EVT_CMD_COMPLETE;
  187. rq.cparam = &send_action->data[3];
  188. rq.clen = send_action->data[2];
  189. rq.rparam = response;
  190. rq.rlen = sizeof(response);
  191. if (hci_send_req(fd, &rq, 15) < 0) {
  192. perror("Cannot send hci command to socket");
  193. return -1;
  194. }
  195. /* verify success */
  196. if (response[0]) {
  197. errno = EIO;
  198. return -1;
  199. }
  200. return 0;
  201. }
  202. static int brf_send_command_file(int fd, struct bts_action_send *send_action,
  203. long size)
  204. {
  205. unsigned char response[1024] = {0};
  206. long ret = 0;
  207. /* send command */
  208. if (size != write(fd, send_action, size)) {
  209. perror("Texas: Failed to write action command");
  210. return -1;
  211. }
  212. /* read response */
  213. ret = read_hci_event(fd, response, sizeof(response));
  214. if (ret < 0) {
  215. perror("texas: failed to read command response");
  216. return -1;
  217. }
  218. /* verify success */
  219. if (ret < 7 || 0 != response[6]) {
  220. fprintf( stderr, "TI init command failed.\n" );
  221. errno = EIO;
  222. return -1;
  223. }
  224. return 0;
  225. }
  226. static int brf_send_command(int fd, struct bts_action_send *send_action,
  227. long size, int hcill_installed)
  228. {
  229. int ret = 0;
  230. char *fixed_action;
  231. /* remove packet type when giving to socket API */
  232. if (hcill_installed) {
  233. fixed_action = ((char *) send_action) + 1;
  234. ret = brf_send_command_socket(fd, (struct bts_action_send *) fixed_action);
  235. } else {
  236. ret = brf_send_command_file(fd, send_action, size);
  237. }
  238. return ret;
  239. }
  240. static int brf_do_action(uint16_t brf_type, uint8_t *brf_action, long brf_size,
  241. int fd, int *speed, struct termios *ti, int hcill_installed)
  242. {
  243. int ret = 0;
  244. switch (brf_type) {
  245. case ACTION_SEND_COMMAND:
  246. DPRINTF("W");
  247. ret = brf_send_command(fd,
  248. (struct bts_action_send *) brf_action,
  249. brf_size, hcill_installed);
  250. break;
  251. case ACTION_WAIT_EVENT:
  252. DPRINTF("R");
  253. break;
  254. case ACTION_SERIAL:
  255. DPRINTF("S");
  256. ret = brf_set_serial_params((struct bts_action_serial *) brf_action, fd, speed, ti);
  257. break;
  258. case ACTION_DELAY:
  259. DPRINTF("D");
  260. brf_delay((struct bts_action_delay *) brf_action);
  261. break;
  262. case ACTION_REMARKS:
  263. DPRINTF("C");
  264. break;
  265. default:
  266. fprintf(stderr, "brf_init: unknown firmware action type (%d)\n", brf_type);
  267. break;
  268. }
  269. return ret;
  270. }
  271. /*
  272. * tests whether a given brf action is a HCI_VS_Sleep_Mode_Configurations cmd
  273. */
  274. static int brf_action_is_deep_sleep(uint8_t *brf_action, long brf_size,
  275. uint16_t brf_type)
  276. {
  277. uint16_t opcode;
  278. if (brf_type != ACTION_SEND_COMMAND)
  279. return 0;
  280. if (brf_size < 3)
  281. return 0;
  282. if (brf_action[0] != HCI_COMMAND_PKT)
  283. return 0;
  284. /* HCI data is little endian */
  285. opcode = brf_action[1] | (brf_action[2] << 8);
  286. if (opcode != BRF_DEEP_SLEEP_OPCODE)
  287. return 0;
  288. /* action is deep sleep configuration command ! */
  289. return 1;
  290. }
  291. /*
  292. * This function is called twice.
  293. * The first time it is called, it loads the brf script, and executes its
  294. * commands until it reaches a deep sleep command (or its end).
  295. * The second time it is called, it assumes HCILL protocol is set up,
  296. * and sends rest of brf script via the supplied socket.
  297. */
  298. static int brf_do_script(int fd, int *speed, struct termios *ti, const char *bts_file)
  299. {
  300. int ret = 0, hcill_installed = bts_file ? 0 : 1;
  301. uint32_t vers;
  302. static FILE *brf_script_file = NULL;
  303. static uint8_t brf_action[512];
  304. static long brf_size;
  305. static uint16_t brf_type;
  306. /* is it the first time we are called ? */
  307. if (0 == hcill_installed) {
  308. DPRINTF("Sending script to serial device\n");
  309. brf_script_file = bts_load_script(bts_file, &vers );
  310. if (!brf_script_file) {
  311. fprintf(stderr, "Warning: cannot find BTS file: %s\n",
  312. bts_file);
  313. return 0;
  314. }
  315. fprintf( stderr, "Loaded BTS script version %u\n", vers );
  316. brf_size = bts_fetch_action(brf_script_file, brf_action,
  317. sizeof(brf_action), &brf_type);
  318. if (brf_size == 0) {
  319. fprintf(stderr, "Warning: BTS file is empty !");
  320. return 0;
  321. }
  322. }
  323. else {
  324. DPRINTF("Sending script to bluetooth socket\n");
  325. }
  326. /* execute current action and continue to parse brf script file */
  327. while (brf_size != 0) {
  328. ret = brf_do_action(brf_type, brf_action, brf_size,
  329. fd, speed, ti, hcill_installed);
  330. if (ret == -1)
  331. break;
  332. brf_size = bts_fetch_action(brf_script_file, brf_action,
  333. sizeof(brf_action), &brf_type);
  334. /* if this is the first time we run (no HCILL yet) */
  335. /* and a deep sleep command is encountered */
  336. /* we exit */
  337. if (!hcill_installed &&
  338. brf_action_is_deep_sleep(brf_action,
  339. brf_size, brf_type))
  340. return 0;
  341. }
  342. bts_unload_script(brf_script_file);
  343. brf_script_file = NULL;
  344. DPRINTF("\n");
  345. return ret;
  346. }
  347. int texas_init(int fd, int *speed, struct termios *ti)
  348. {
  349. struct timespec tm = {0, 50000};
  350. char cmd[4];
  351. unsigned char resp[100]; /* Response */
  352. const char *bts_file;
  353. int n;
  354. memset(resp,'\0', 100);
  355. /* It is possible to get software version with manufacturer specific
  356. HCI command HCI_VS_TI_Version_Number. But the only thing you get more
  357. is if this is point-to-point or point-to-multipoint module */
  358. /* Get Manufacturer and LMP version */
  359. cmd[0] = HCI_COMMAND_PKT;
  360. cmd[1] = 0x01;
  361. cmd[2] = 0x10;
  362. cmd[3] = 0x00;
  363. do {
  364. n = write(fd, cmd, 4);
  365. if (n < 0) {
  366. perror("Failed to write init command (READ_LOCAL_VERSION_INFORMATION)");
  367. return -1;
  368. }
  369. if (n < 4) {
  370. fprintf(stderr, "Wanted to write 4 bytes, could only write %d. Stop\n", n);
  371. return -1;
  372. }
  373. /* Read reply. */
  374. if (read_hci_event(fd, resp, 100) < 0) {
  375. perror("Failed to read init response (READ_LOCAL_VERSION_INFORMATION)");
  376. return -1;
  377. }
  378. /* Wait for command complete event for our Opcode */
  379. } while (resp[4] != cmd[1] && resp[5] != cmd[2]);
  380. /* Verify manufacturer */
  381. if (! is_it_texas(resp)) {
  382. fprintf(stderr,"ERROR: module's manufacturer is not Texas Instruments\n");
  383. return -1;
  384. }
  385. fprintf(stderr, "Found a Texas Instruments' chip!\n");
  386. bts_file = get_firmware_name(resp);
  387. fprintf(stderr, "Firmware file : %s\n", bts_file);
  388. n = brf_do_script(fd, speed, ti, bts_file);
  389. nanosleep(&tm, NULL);
  390. return n;
  391. }
  392. int texas_post(int fd, struct termios *ti)
  393. {
  394. int dev_id, dd, ret = 0;
  395. sleep(1);
  396. dev_id = ioctl(fd, HCIUARTGETDEVICE, 0);
  397. if (dev_id < 0) {
  398. perror("cannot get device id");
  399. return -1;
  400. }
  401. DPRINTF("\nAdded device hci%d\n", dev_id);
  402. dd = hci_open_dev(dev_id);
  403. if (dd < 0) {
  404. perror("HCI device open failed");
  405. return -1;
  406. }
  407. if (ioctl(dd, HCIDEVUP, dev_id) < 0 && errno != EALREADY) {
  408. fprintf(stderr, "Can't init device hci%d: %s (%d)", dev_id,
  409. strerror(errno), errno);
  410. hci_close_dev(dd);
  411. return -1;
  412. }
  413. ret = brf_do_script(dd, NULL, ti, NULL);
  414. hci_close_dev(dd);
  415. return ret;
  416. }