hciattach.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2000-2001 Qualcomm Incorporated
  7. * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
  8. * Copyright (C) 2002-2010 Marcel Holtmann <marcel@holtmann.org>
  9. *
  10. *
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include <config.h>
  14. #endif
  15. #define _GNU_SOURCE
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <signal.h>
  23. #include <syslog.h>
  24. #include <termios.h>
  25. #include <time.h>
  26. #include <poll.h>
  27. #include <sys/time.h>
  28. #include <sys/param.h>
  29. #include <sys/ioctl.h>
  30. #include "lib/bluetooth.h"
  31. #include "lib/hci.h"
  32. #include "lib/hci_lib.h"
  33. #include "src/shared/tty.h"
  34. #include "hciattach.h"
  35. struct uart_t {
  36. char *type;
  37. int m_id;
  38. int p_id;
  39. int proto;
  40. int init_speed;
  41. int speed;
  42. int flags;
  43. int pm;
  44. char *bdaddr;
  45. int (*init) (int fd, struct uart_t *u, struct termios *ti);
  46. int (*post) (int fd, struct uart_t *u, struct termios *ti);
  47. };
  48. #define FLOW_CTL 0x0001
  49. #define AMP_DEV 0x0002
  50. #define ENABLE_PM 1
  51. #define DISABLE_PM 0
  52. static volatile sig_atomic_t __io_canceled = 0;
  53. static void sig_hup(int sig)
  54. {
  55. }
  56. static void sig_term(int sig)
  57. {
  58. __io_canceled = 1;
  59. }
  60. static void sig_alarm(int sig)
  61. {
  62. fprintf(stderr, "Initialization timed out.\n");
  63. exit(1);
  64. }
  65. int set_speed(int fd, struct termios *ti, int speed)
  66. {
  67. if (cfsetospeed(ti, tty_get_speed(speed)) < 0)
  68. return -errno;
  69. if (cfsetispeed(ti, tty_get_speed(speed)) < 0)
  70. return -errno;
  71. if (tcsetattr(fd, TCSANOW, ti) < 0)
  72. return -errno;
  73. return 0;
  74. }
  75. /*
  76. * Read an HCI event from the given file descriptor.
  77. */
  78. int read_hci_event(int fd, unsigned char* buf, int size)
  79. {
  80. int remain, r;
  81. int count = 0;
  82. if (size <= 0)
  83. return -1;
  84. /* The first byte identifies the packet type. For HCI event packets, it
  85. * should be 0x04, so we read until we get to the 0x04. */
  86. while (1) {
  87. r = read(fd, buf, 1);
  88. if (r <= 0)
  89. return -1;
  90. if (buf[0] == 0x04)
  91. break;
  92. }
  93. count++;
  94. /* The next two bytes are the event code and parameter total length. */
  95. while (count < 3) {
  96. r = read(fd, buf + count, 3 - count);
  97. if (r <= 0)
  98. return -1;
  99. count += r;
  100. }
  101. /* Now we read the parameters. */
  102. if (buf[2] < (size - 3))
  103. remain = buf[2];
  104. else
  105. remain = size - 3;
  106. while ((count - 3) < remain) {
  107. r = read(fd, buf + count, remain - (count - 3));
  108. if (r <= 0)
  109. return -1;
  110. count += r;
  111. }
  112. return count;
  113. }
  114. /*
  115. * Ericsson specific initialization
  116. */
  117. static int ericsson(int fd, struct uart_t *u, struct termios *ti)
  118. {
  119. struct timespec tm = {0, 50000};
  120. char cmd[5];
  121. cmd[0] = HCI_COMMAND_PKT;
  122. cmd[1] = 0x09;
  123. cmd[2] = 0xfc;
  124. cmd[3] = 0x01;
  125. switch (u->speed) {
  126. case 57600:
  127. cmd[4] = 0x03;
  128. break;
  129. case 115200:
  130. cmd[4] = 0x02;
  131. break;
  132. case 230400:
  133. cmd[4] = 0x01;
  134. break;
  135. case 460800:
  136. cmd[4] = 0x00;
  137. break;
  138. case 921600:
  139. cmd[4] = 0x20;
  140. break;
  141. case 2000000:
  142. cmd[4] = 0x25;
  143. break;
  144. case 3000000:
  145. cmd[4] = 0x27;
  146. break;
  147. case 4000000:
  148. cmd[4] = 0x2B;
  149. break;
  150. default:
  151. cmd[4] = 0x03;
  152. u->speed = 57600;
  153. fprintf(stderr, "Invalid speed requested, using %d bps instead\n", u->speed);
  154. break;
  155. }
  156. /* Send initialization command */
  157. if (write(fd, cmd, 5) != 5) {
  158. perror("Failed to write init command");
  159. return -1;
  160. }
  161. nanosleep(&tm, NULL);
  162. return 0;
  163. }
  164. /*
  165. * Digianswer specific initialization
  166. */
  167. static int digi(int fd, struct uart_t *u, struct termios *ti)
  168. {
  169. struct timespec tm = {0, 50000};
  170. char cmd[5];
  171. /* DigiAnswer set baud rate command */
  172. cmd[0] = HCI_COMMAND_PKT;
  173. cmd[1] = 0x07;
  174. cmd[2] = 0xfc;
  175. cmd[3] = 0x01;
  176. switch (u->speed) {
  177. case 57600:
  178. cmd[4] = 0x08;
  179. break;
  180. case 115200:
  181. cmd[4] = 0x09;
  182. break;
  183. default:
  184. cmd[4] = 0x09;
  185. u->speed = 115200;
  186. break;
  187. }
  188. /* Send initialization command */
  189. if (write(fd, cmd, 5) != 5) {
  190. perror("Failed to write init command");
  191. return -1;
  192. }
  193. nanosleep(&tm, NULL);
  194. return 0;
  195. }
  196. static int texas(int fd, struct uart_t *u, struct termios *ti)
  197. {
  198. return texas_init(fd, &u->speed, ti);
  199. }
  200. static int texas2(int fd, struct uart_t *u, struct termios *ti)
  201. {
  202. return texas_post(fd, ti);
  203. }
  204. static int texasalt(int fd, struct uart_t *u, struct termios *ti)
  205. {
  206. return texasalt_init(fd, u->speed, ti);
  207. }
  208. static int ath3k_ps(int fd, struct uart_t *u, struct termios *ti)
  209. {
  210. return ath3k_init(fd, u->speed, u->init_speed, u->bdaddr, ti);
  211. }
  212. static int ath3k_pm(int fd, struct uart_t *u, struct termios *ti)
  213. {
  214. return ath3k_post(fd, u->pm);
  215. }
  216. static int qualcomm(int fd, struct uart_t *u, struct termios *ti)
  217. {
  218. return qualcomm_init(fd, u->speed, ti, u->bdaddr);
  219. }
  220. static int intel(int fd, struct uart_t *u, struct termios *ti)
  221. {
  222. return intel_init(fd, u->init_speed, &u->speed, ti);
  223. }
  224. static int bcm43xx(int fd, struct uart_t *u, struct termios *ti)
  225. {
  226. return bcm43xx_init(fd, u->init_speed, u->speed, ti, u->bdaddr);
  227. }
  228. static int read_check(int fd, void *buf, int count)
  229. {
  230. int res;
  231. do {
  232. res = read(fd, buf, count);
  233. if (res != -1) {
  234. buf += res;
  235. count -= res;
  236. }
  237. } while (count && (errno == 0 || errno == EINTR));
  238. if (count)
  239. return -1;
  240. return 0;
  241. }
  242. /*
  243. * BCSP specific initialization
  244. */
  245. static int serial_fd;
  246. static int bcsp_max_retries = 10;
  247. static void bcsp_tshy_sig_alarm(int sig)
  248. {
  249. unsigned char bcsp_sync_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xda,0xdc,0xed,0xed,0xc0};
  250. static int retries = 0;
  251. if (retries < bcsp_max_retries) {
  252. retries++;
  253. if (write(serial_fd, &bcsp_sync_pkt, 10) < 0)
  254. return;
  255. alarm(1);
  256. return;
  257. }
  258. tcflush(serial_fd, TCIOFLUSH);
  259. fprintf(stderr, "BCSP initialization timed out\n");
  260. exit(1);
  261. }
  262. static void bcsp_tconf_sig_alarm(int sig)
  263. {
  264. unsigned char bcsp_conf_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xad,0xef,0xac,0xed,0xc0};
  265. static int retries = 0;
  266. if (retries < bcsp_max_retries){
  267. retries++;
  268. if (write(serial_fd, &bcsp_conf_pkt, 10) < 0)
  269. return;
  270. alarm(1);
  271. return;
  272. }
  273. tcflush(serial_fd, TCIOFLUSH);
  274. fprintf(stderr, "BCSP initialization timed out\n");
  275. exit(1);
  276. }
  277. static int bcsp(int fd, struct uart_t *u, struct termios *ti)
  278. {
  279. unsigned char byte, bcsph[4], bcspp[4],
  280. bcsp_sync_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xac,0xaf,0xef,0xee,0xc0},
  281. bcsp_conf_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xde,0xad,0xd0,0xd0,0xc0},
  282. bcspsync[4] = {0xda, 0xdc, 0xed, 0xed},
  283. bcspsyncresp[4] = {0xac,0xaf,0xef,0xee},
  284. bcspconf[4] = {0xad,0xef,0xac,0xed},
  285. bcspconfresp[4] = {0xde,0xad,0xd0,0xd0};
  286. struct sigaction sa;
  287. int len;
  288. if (set_speed(fd, ti, u->speed) < 0) {
  289. perror("Can't set default baud rate");
  290. return -1;
  291. }
  292. ti->c_cflag |= PARENB;
  293. ti->c_cflag &= ~(PARODD);
  294. if (tcsetattr(fd, TCSANOW, ti) < 0) {
  295. perror("Can't set port settings");
  296. return -1;
  297. }
  298. alarm(0);
  299. serial_fd = fd;
  300. memset(&sa, 0, sizeof(sa));
  301. sa.sa_flags = SA_NOCLDSTOP;
  302. sa.sa_handler = bcsp_tshy_sig_alarm;
  303. sigaction(SIGALRM, &sa, NULL);
  304. /* State = shy */
  305. bcsp_tshy_sig_alarm(0);
  306. while (1) {
  307. do {
  308. if (read_check(fd, &byte, 1) == -1){
  309. perror("Failed to read");
  310. return -1;
  311. }
  312. } while (byte != 0xC0);
  313. do {
  314. if ( read_check(fd, &bcsph[0], 1) == -1){
  315. perror("Failed to read");
  316. return -1;
  317. }
  318. } while (bcsph[0] == 0xC0);
  319. if ( read_check(fd, &bcsph[1], 3) == -1){
  320. perror("Failed to read");
  321. return -1;
  322. }
  323. if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
  324. continue;
  325. if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
  326. continue;
  327. if (read_check(fd, &bcspp, 4) == -1){
  328. perror("Failed to read");
  329. return -1;
  330. }
  331. if (!memcmp(bcspp, bcspsync, 4)) {
  332. if (write(fd, &bcsp_sync_resp_pkt,10) < 0)
  333. return -1;
  334. } else if (!memcmp(bcspp, bcspsyncresp, 4))
  335. break;
  336. }
  337. /* State = curious */
  338. alarm(0);
  339. sa.sa_handler = bcsp_tconf_sig_alarm;
  340. sigaction(SIGALRM, &sa, NULL);
  341. alarm(1);
  342. while (1) {
  343. do {
  344. if (read_check(fd, &byte, 1) == -1){
  345. perror("Failed to read");
  346. return -1;
  347. }
  348. } while (byte != 0xC0);
  349. do {
  350. if (read_check(fd, &bcsph[0], 1) == -1){
  351. perror("Failed to read");
  352. return -1;
  353. }
  354. } while (bcsph[0] == 0xC0);
  355. if (read_check(fd, &bcsph[1], 3) == -1){
  356. perror("Failed to read");
  357. return -1;
  358. }
  359. if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
  360. continue;
  361. if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
  362. continue;
  363. if (read_check(fd, &bcspp, 4) == -1){
  364. perror("Failed to read");
  365. return -1;
  366. }
  367. if (!memcmp(bcspp, bcspsync, 4))
  368. len = write(fd, &bcsp_sync_resp_pkt, 10);
  369. else if (!memcmp(bcspp, bcspconf, 4))
  370. len = write(fd, &bcsp_conf_resp_pkt, 10);
  371. else if (!memcmp(bcspp, bcspconfresp, 4))
  372. break;
  373. else
  374. continue;
  375. if (len < 0)
  376. return -errno;
  377. }
  378. /* State = garrulous */
  379. return 0;
  380. }
  381. /*
  382. * CSR specific initialization
  383. * Inspired strongly by code in OpenBT and experimentations with Brainboxes
  384. * Pcmcia card.
  385. * Jean Tourrilhes <jt@hpl.hp.com> - 14.11.01
  386. */
  387. static int csr(int fd, struct uart_t *u, struct termios *ti)
  388. {
  389. struct timespec tm = {0, 10000000}; /* 10ms - be generous */
  390. unsigned char cmd[30]; /* Command */
  391. unsigned char resp[30]; /* Response */
  392. int clen = 0; /* Command len */
  393. static int csr_seq = 0; /* Sequence number of command */
  394. int divisor;
  395. /* It seems that if we set the CSR UART speed straight away, it
  396. * won't work, the CSR UART gets into a state where we can't talk
  397. * to it anymore.
  398. * On the other hand, doing a read before setting the CSR speed
  399. * seems to be ok.
  400. * Therefore, the strategy is to read the build ID (useful for
  401. * debugging) and only then set the CSR UART speed. Doing like
  402. * this is more complex but at least it works ;-)
  403. * The CSR UART control may be slow to wake up or something because
  404. * every time I read its speed, its bogus...
  405. * Jean II */
  406. /* Try to read the build ID of the CSR chip */
  407. clen = 5 + (5 + 6) * 2;
  408. /* HCI header */
  409. cmd[0] = HCI_COMMAND_PKT;
  410. cmd[1] = 0x00; /* CSR command */
  411. cmd[2] = 0xfc; /* MANUFACTURER_SPEC */
  412. cmd[3] = 1 + (5 + 6) * 2; /* len */
  413. /* CSR MSG header */
  414. cmd[4] = 0xC2; /* first+last+channel=BCC */
  415. /* CSR BCC header */
  416. cmd[5] = 0x00; /* type = GET-REQ */
  417. cmd[6] = 0x00; /* - msB */
  418. cmd[7] = 5 + 4; /* len */
  419. cmd[8] = 0x00; /* - msB */
  420. cmd[9] = csr_seq & 0xFF;/* seq num */
  421. cmd[10] = (csr_seq >> 8) & 0xFF; /* - msB */
  422. csr_seq++;
  423. cmd[11] = 0x19; /* var_id = CSR_CMD_BUILD_ID */
  424. cmd[12] = 0x28; /* - msB */
  425. cmd[13] = 0x00; /* status = STATUS_OK */
  426. cmd[14] = 0x00; /* - msB */
  427. /* CSR BCC payload */
  428. memset(cmd + 15, 0, 6 * 2);
  429. /* Send command */
  430. do {
  431. if (write(fd, cmd, clen) != clen) {
  432. perror("Failed to write init command (GET_BUILD_ID)");
  433. return -1;
  434. }
  435. /* Read reply. */
  436. if (read_hci_event(fd, resp, 100) < 0) {
  437. perror("Failed to read init response (GET_BUILD_ID)");
  438. return -1;
  439. }
  440. /* Event code 0xFF is for vendor-specific events, which is
  441. * what we're looking for. */
  442. } while (resp[1] != 0xFF);
  443. #ifdef CSR_DEBUG
  444. {
  445. char temp[512];
  446. int i;
  447. for (i=0; i < rlen; i++)
  448. sprintf(temp + (i*3), "-%02X", resp[i]);
  449. fprintf(stderr, "Reading CSR build ID %d [%s]\n", rlen, temp + 1);
  450. // In theory, it should look like :
  451. // 04-FF-13-FF-01-00-09-00-00-00-19-28-00-00-73-00-00-00-00-00-00-00
  452. }
  453. #endif
  454. /* Display that to user */
  455. fprintf(stderr, "CSR build ID 0x%02X-0x%02X\n",
  456. resp[15] & 0xFF, resp[14] & 0xFF);
  457. /* Try to read the current speed of the CSR chip */
  458. clen = 5 + (5 + 4)*2;
  459. /* -- HCI header */
  460. cmd[3] = 1 + (5 + 4)*2; /* len */
  461. /* -- CSR BCC header -- */
  462. cmd[9] = csr_seq & 0xFF; /* seq num */
  463. cmd[10] = (csr_seq >> 8) & 0xFF; /* - msB */
  464. csr_seq++;
  465. cmd[11] = 0x02; /* var_id = CONFIG_UART */
  466. cmd[12] = 0x68; /* - msB */
  467. #ifdef CSR_DEBUG
  468. /* Send command */
  469. do {
  470. if (write(fd, cmd, clen) != clen) {
  471. perror("Failed to write init command (GET_BUILD_ID)");
  472. return -1;
  473. }
  474. /* Read reply. */
  475. if (read_hci_event(fd, resp, 100) < 0) {
  476. perror("Failed to read init response (GET_BUILD_ID)");
  477. return -1;
  478. }
  479. /* Event code 0xFF is for vendor-specific events, which is
  480. * what we're looking for. */
  481. } while (resp[1] != 0xFF);
  482. {
  483. char temp[512];
  484. int i;
  485. for (i=0; i < rlen; i++)
  486. sprintf(temp + (i*3), "-%02X", resp[i]);
  487. fprintf(stderr, "Reading CSR UART speed %d [%s]\n", rlen, temp+1);
  488. }
  489. #endif
  490. if (u->speed > 1500000) {
  491. fprintf(stderr, "Speed %d too high. Remaining at %d baud\n",
  492. u->speed, u->init_speed);
  493. u->speed = u->init_speed;
  494. } else if (!tty_get_speed(u->speed)) {
  495. /* Unknown speed. Why oh why can't we just pass an int to the kernel? */
  496. fprintf(stderr, "Speed %d unrecognised. Remaining at %d baud\n",
  497. u->speed, u->init_speed);
  498. u->speed = u->init_speed;
  499. }
  500. if (u->speed == u->init_speed)
  501. return 0;
  502. /* Now, create the command that will set the UART speed */
  503. /* CSR BCC header */
  504. cmd[5] = 0x02; /* type = SET-REQ */
  505. cmd[6] = 0x00; /* - msB */
  506. cmd[9] = csr_seq & 0xFF; /* seq num */
  507. cmd[10] = (csr_seq >> 8) & 0xFF;/* - msB */
  508. csr_seq++;
  509. divisor = (u->speed*64+7812)/15625;
  510. /* No parity, one stop bit -> divisor |= 0x0000; */
  511. cmd[15] = (divisor) & 0xFF; /* divider */
  512. cmd[16] = (divisor >> 8) & 0xFF; /* - msB */
  513. /* The rest of the payload will be 0x00 */
  514. #ifdef CSR_DEBUG
  515. {
  516. char temp[512];
  517. int i;
  518. for(i = 0; i < clen; i++)
  519. sprintf(temp + (i*3), "-%02X", cmd[i]);
  520. fprintf(stderr, "Writing CSR UART speed %d [%s]\n", clen, temp + 1);
  521. // In theory, it should look like :
  522. // 01-00-FC-13-C2-02-00-09-00-03-00-02-68-00-00-BF-0E-00-00-00-00-00-00
  523. // 01-00-FC-13-C2-02-00-09-00-01-00-02-68-00-00-D8-01-00-00-00-00-00-00
  524. }
  525. #endif
  526. /* Send the command to set the CSR UART speed */
  527. if (write(fd, cmd, clen) != clen) {
  528. perror("Failed to write init command (SET_UART_SPEED)");
  529. return -1;
  530. }
  531. nanosleep(&tm, NULL);
  532. return 0;
  533. }
  534. /*
  535. * Silicon Wave specific initialization
  536. * Thomas Moser <thomas.moser@tmoser.ch>
  537. */
  538. static int swave(int fd, struct uart_t *u, struct termios *ti)
  539. {
  540. struct timespec tm = { 0, 500000 };
  541. char cmd[10], rsp[100];
  542. int r;
  543. // Silicon Wave set baud rate command
  544. // see HCI Vendor Specific Interface from Silicon Wave
  545. // first send a "param access set" command to set the
  546. // appropriate data fields in RAM. Then send a "HCI Reset
  547. // Subcommand", e.g. "soft reset" to make the changes effective.
  548. cmd[0] = HCI_COMMAND_PKT; // it's a command packet
  549. cmd[1] = 0x0B; // OCF 0x0B = param access set
  550. cmd[2] = 0xfc; // OGF bx111111 = vendor specific
  551. cmd[3] = 0x06; // 6 bytes of data following
  552. cmd[4] = 0x01; // param sub command
  553. cmd[5] = 0x11; // tag 17 = 0x11 = HCI Transport Params
  554. cmd[6] = 0x03; // length of the parameter following
  555. cmd[7] = 0x01; // HCI Transport flow control enable
  556. cmd[8] = 0x01; // HCI Transport Type = UART
  557. switch (u->speed) {
  558. case 19200:
  559. cmd[9] = 0x03;
  560. break;
  561. case 38400:
  562. cmd[9] = 0x02;
  563. break;
  564. case 57600:
  565. cmd[9] = 0x01;
  566. break;
  567. case 115200:
  568. cmd[9] = 0x00;
  569. break;
  570. default:
  571. u->speed = 115200;
  572. cmd[9] = 0x00;
  573. break;
  574. }
  575. /* Send initialization command */
  576. if (write(fd, cmd, 10) != 10) {
  577. perror("Failed to write init command");
  578. return -1;
  579. }
  580. // We should wait for a "GET Event" to confirm the success of
  581. // the baud rate setting. Wait some time before reading. Better:
  582. // read with timeout, parse data
  583. // until correct answer, else error handling ... todo ...
  584. nanosleep(&tm, NULL);
  585. r = read(fd, rsp, sizeof(rsp));
  586. if (r > 0) {
  587. // guess it's okay, but we should parse the reply. But since
  588. // I don't react on an error anyway ... todo
  589. // Response packet format:
  590. // 04 Event
  591. // FF Vendor specific
  592. // 07 Parameter length
  593. // 0B Subcommand
  594. // 01 Setevent
  595. // 11 Tag specifying HCI Transport Layer Parameter
  596. // 03 length
  597. // 01 flow on
  598. // 01 Hci Transport type = Uart
  599. // xx Baud rate set (see above)
  600. } else {
  601. // ups, got error.
  602. return -1;
  603. }
  604. // we probably got the reply. Now we must send the "soft reset"
  605. // which is standard HCI RESET.
  606. cmd[0] = HCI_COMMAND_PKT; // it's a command packet
  607. cmd[1] = 0x03;
  608. cmd[2] = 0x0c;
  609. cmd[3] = 0x00;
  610. /* Send reset command */
  611. if (write(fd, cmd, 4) != 4) {
  612. perror("Can't write Silicon Wave reset cmd.");
  613. return -1;
  614. }
  615. nanosleep(&tm, NULL);
  616. // now the uart baud rate on the silicon wave module is set and effective.
  617. // change our own baud rate as well. Then there is a reset event coming in
  618. // on the *new* baud rate. This is *undocumented*! The packet looks like this:
  619. // 04 FF 01 0B (which would make that a confirmation of 0x0B = "Param
  620. // subcommand class". So: change to new baud rate, read with timeout, parse
  621. // data, error handling. BTW: all param access in Silicon Wave is done this way.
  622. // Maybe this code would belong in a separate file, or at least code reuse...
  623. return 0;
  624. }
  625. /*
  626. * ST Microelectronics specific initialization
  627. * Marcel Holtmann <marcel@holtmann.org>
  628. */
  629. static int st(int fd, struct uart_t *u, struct termios *ti)
  630. {
  631. struct timespec tm = {0, 50000};
  632. char cmd[5];
  633. /* ST Microelectronics set baud rate command */
  634. cmd[0] = HCI_COMMAND_PKT;
  635. cmd[1] = 0x46; // OCF = Hci_Cmd_ST_Set_Uart_Baud_Rate
  636. cmd[2] = 0xfc; // OGF = Vendor specific
  637. cmd[3] = 0x01;
  638. switch (u->speed) {
  639. case 9600:
  640. cmd[4] = 0x09;
  641. break;
  642. case 19200:
  643. cmd[4] = 0x0b;
  644. break;
  645. case 38400:
  646. cmd[4] = 0x0d;
  647. break;
  648. case 57600:
  649. cmd[4] = 0x0e;
  650. break;
  651. case 115200:
  652. cmd[4] = 0x10;
  653. break;
  654. case 230400:
  655. cmd[4] = 0x12;
  656. break;
  657. case 460800:
  658. cmd[4] = 0x13;
  659. break;
  660. case 921600:
  661. cmd[4] = 0x14;
  662. break;
  663. default:
  664. cmd[4] = 0x10;
  665. u->speed = 115200;
  666. break;
  667. }
  668. /* Send initialization command */
  669. if (write(fd, cmd, 5) != 5) {
  670. perror("Failed to write init command");
  671. return -1;
  672. }
  673. nanosleep(&tm, NULL);
  674. return 0;
  675. }
  676. static int stlc2500(int fd, struct uart_t *u, struct termios *ti)
  677. {
  678. bdaddr_t bdaddr;
  679. unsigned char resp[10];
  680. int n;
  681. int rvalue;
  682. /* STLC2500 has an ericsson core */
  683. rvalue = ericsson(fd, u, ti);
  684. if (rvalue != 0)
  685. return rvalue;
  686. #ifdef STLC2500_DEBUG
  687. fprintf(stderr, "Setting speed\n");
  688. #endif
  689. if (set_speed(fd, ti, u->speed) < 0) {
  690. perror("Can't set baud rate");
  691. return -1;
  692. }
  693. #ifdef STLC2500_DEBUG
  694. fprintf(stderr, "Speed set...\n");
  695. #endif
  696. /* Read reply */
  697. if ((n = read_hci_event(fd, resp, 10)) < 0) {
  698. fprintf(stderr, "Failed to set baud rate on chip\n");
  699. return -1;
  700. }
  701. #ifdef STLC2500_DEBUG
  702. for (i = 0; i < n; i++) {
  703. fprintf(stderr, "resp[%d] = %02x\n", i, resp[i]);
  704. }
  705. #endif
  706. str2ba(u->bdaddr, &bdaddr);
  707. return stlc2500_init(fd, &bdaddr);
  708. }
  709. static int bgb2xx(int fd, struct uart_t *u, struct termios *ti)
  710. {
  711. bdaddr_t bdaddr;
  712. str2ba(u->bdaddr, &bdaddr);
  713. return bgb2xx_init(fd, &bdaddr);
  714. }
  715. /*
  716. * Broadcom specific initialization
  717. * Extracted from Jungo openrg
  718. */
  719. static int bcm2035(int fd, struct uart_t *u, struct termios *ti)
  720. {
  721. int n;
  722. unsigned char cmd[30], resp[30];
  723. /* Reset the BT Chip */
  724. memset(cmd, 0, sizeof(cmd));
  725. memset(resp, 0, sizeof(resp));
  726. cmd[0] = HCI_COMMAND_PKT;
  727. cmd[1] = 0x03;
  728. cmd[2] = 0x0c;
  729. cmd[3] = 0x00;
  730. /* Send command */
  731. if (write(fd, cmd, 4) != 4) {
  732. fprintf(stderr, "Failed to write reset command\n");
  733. return -1;
  734. }
  735. /* Read reply */
  736. if ((n = read_hci_event(fd, resp, 4)) < 0) {
  737. fprintf(stderr, "Failed to reset chip\n");
  738. return -1;
  739. }
  740. if (u->bdaddr != NULL) {
  741. /* Set BD_ADDR */
  742. memset(cmd, 0, sizeof(cmd));
  743. memset(resp, 0, sizeof(resp));
  744. cmd[0] = HCI_COMMAND_PKT;
  745. cmd[1] = 0x01;
  746. cmd[2] = 0xfc;
  747. cmd[3] = 0x06;
  748. str2ba(u->bdaddr, (bdaddr_t *) (cmd + 4));
  749. /* Send command */
  750. if (write(fd, cmd, 10) != 10) {
  751. fprintf(stderr, "Failed to write BD_ADDR command\n");
  752. return -1;
  753. }
  754. /* Read reply */
  755. if ((n = read_hci_event(fd, resp, 10)) < 0) {
  756. fprintf(stderr, "Failed to set BD_ADDR\n");
  757. return -1;
  758. }
  759. }
  760. /* Read the local version info */
  761. memset(cmd, 0, sizeof(cmd));
  762. memset(resp, 0, sizeof(resp));
  763. cmd[0] = HCI_COMMAND_PKT;
  764. cmd[1] = 0x01;
  765. cmd[2] = 0x10;
  766. cmd[3] = 0x00;
  767. /* Send command */
  768. if (write(fd, cmd, 4) != 4) {
  769. fprintf(stderr, "Failed to write \"read local version\" "
  770. "command\n");
  771. return -1;
  772. }
  773. /* Read reply */
  774. if ((n = read_hci_event(fd, resp, 4)) < 0) {
  775. fprintf(stderr, "Failed to read local version\n");
  776. return -1;
  777. }
  778. /* Read the local supported commands info */
  779. memset(cmd, 0, sizeof(cmd));
  780. memset(resp, 0, sizeof(resp));
  781. cmd[0] = HCI_COMMAND_PKT;
  782. cmd[1] = 0x02;
  783. cmd[2] = 0x10;
  784. cmd[3] = 0x00;
  785. /* Send command */
  786. if (write(fd, cmd, 4) != 4) {
  787. fprintf(stderr, "Failed to write \"read local supported "
  788. "commands\" command\n");
  789. return -1;
  790. }
  791. /* Read reply */
  792. if ((n = read_hci_event(fd, resp, 4)) < 0) {
  793. fprintf(stderr, "Failed to read local supported commands\n");
  794. return -1;
  795. }
  796. /* Set the baud rate */
  797. memset(cmd, 0, sizeof(cmd));
  798. memset(resp, 0, sizeof(resp));
  799. cmd[0] = HCI_COMMAND_PKT;
  800. cmd[1] = 0x18;
  801. cmd[2] = 0xfc;
  802. cmd[3] = 0x02;
  803. switch (u->speed) {
  804. case 57600:
  805. cmd[4] = 0x00;
  806. cmd[5] = 0xe6;
  807. break;
  808. case 230400:
  809. cmd[4] = 0x22;
  810. cmd[5] = 0xfa;
  811. break;
  812. case 460800:
  813. cmd[4] = 0x22;
  814. cmd[5] = 0xfd;
  815. break;
  816. case 921600:
  817. cmd[4] = 0x55;
  818. cmd[5] = 0xff;
  819. break;
  820. default:
  821. /* Default is 115200 */
  822. cmd[4] = 0x00;
  823. cmd[5] = 0xf3;
  824. break;
  825. }
  826. fprintf(stderr, "Baud rate parameters: DHBR=0x%2x,DLBR=0x%2x\n",
  827. cmd[4], cmd[5]);
  828. /* Send command */
  829. if (write(fd, cmd, 6) != 6) {
  830. fprintf(stderr, "Failed to write \"set baud rate\" command\n");
  831. return -1;
  832. }
  833. if ((n = read_hci_event(fd, resp, 6)) < 0) {
  834. fprintf(stderr, "Failed to set baud rate\n");
  835. return -1;
  836. }
  837. return 0;
  838. }
  839. struct uart_t uart[] = {
  840. { "any", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  841. FLOW_CTL, DISABLE_PM, NULL, NULL },
  842. { "ericsson", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200,
  843. FLOW_CTL, DISABLE_PM, NULL, ericsson },
  844. { "digi", 0x0000, 0x0000, HCI_UART_H4, 9600, 115200,
  845. FLOW_CTL, DISABLE_PM, NULL, digi },
  846. { "bcsp", 0x0000, 0x0000, HCI_UART_BCSP, 115200, 115200,
  847. 0, DISABLE_PM, NULL, bcsp },
  848. /* Xircom PCMCIA cards: Credit Card Adapter and Real Port Adapter */
  849. { "xircom", 0x0105, 0x080a, HCI_UART_H4, 115200, 115200,
  850. FLOW_CTL, DISABLE_PM, NULL, NULL },
  851. /* CSR Casira serial adapter or BrainBoxes serial dongle (BL642) */
  852. { "csr", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  853. FLOW_CTL, DISABLE_PM, NULL, csr },
  854. /* BrainBoxes PCMCIA card (BL620) */
  855. { "bboxes", 0x0160, 0x0002, HCI_UART_H4, 115200, 460800,
  856. FLOW_CTL, DISABLE_PM, NULL, csr },
  857. /* Silicon Wave kits */
  858. { "swave", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  859. FLOW_CTL, DISABLE_PM, NULL, swave },
  860. /* Texas Instruments Bluelink (BRF) modules */
  861. { "texas", 0x0000, 0x0000, HCI_UART_LL, 115200, 115200,
  862. FLOW_CTL, DISABLE_PM, NULL, texas, texas2 },
  863. { "texasalt", 0x0000, 0x0000, HCI_UART_LL, 115200, 115200,
  864. FLOW_CTL, DISABLE_PM, NULL, texasalt, NULL },
  865. /* ST Microelectronics minikits based on STLC2410/STLC2415 */
  866. { "st", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200,
  867. FLOW_CTL, DISABLE_PM, NULL, st },
  868. /* ST Microelectronics minikits based on STLC2500 */
  869. { "stlc2500", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  870. FLOW_CTL, DISABLE_PM, "00:80:E1:00:AB:BA", stlc2500 },
  871. /* Philips generic Ericsson IP core based */
  872. { "philips", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  873. FLOW_CTL, DISABLE_PM, NULL, NULL },
  874. /* Philips BGB2xx Module */
  875. { "bgb2xx", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  876. FLOW_CTL, DISABLE_PM, "BD:B2:10:00:AB:BA", bgb2xx },
  877. /* Sphinx Electronics PICO Card */
  878. { "picocard", 0x025e, 0x1000, HCI_UART_H4, 115200, 115200,
  879. FLOW_CTL, DISABLE_PM, NULL, NULL },
  880. /* Inventel BlueBird Module */
  881. { "inventel", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  882. FLOW_CTL, DISABLE_PM, NULL, NULL },
  883. /* COM One Platinium Bluetooth PC Card */
  884. { "comone", 0xffff, 0x0101, HCI_UART_BCSP, 115200, 115200,
  885. 0, DISABLE_PM, NULL, bcsp },
  886. /* TDK Bluetooth PC Card and IBM Bluetooth PC Card II */
  887. { "tdk", 0x0105, 0x4254, HCI_UART_BCSP, 115200, 115200,
  888. 0, DISABLE_PM, NULL, bcsp },
  889. /* Socket Bluetooth CF Card (Rev G) */
  890. { "socket", 0x0104, 0x0096, HCI_UART_BCSP, 230400, 230400,
  891. 0, DISABLE_PM, NULL, bcsp },
  892. /* 3Com Bluetooth Card (Version 3.0) */
  893. { "3com", 0x0101, 0x0041, HCI_UART_H4, 115200, 115200,
  894. FLOW_CTL, DISABLE_PM, NULL, csr },
  895. /* AmbiCom BT2000C Bluetooth PC/CF Card */
  896. { "bt2000c", 0x022d, 0x2000, HCI_UART_H4, 57600, 460800,
  897. FLOW_CTL, DISABLE_PM, NULL, csr },
  898. /* Zoom Bluetooth PCMCIA Card */
  899. { "zoom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
  900. 0, DISABLE_PM, NULL, bcsp },
  901. /* Sitecom CN-504 PCMCIA Card */
  902. { "sitecom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
  903. 0, DISABLE_PM, NULL, bcsp },
  904. /* Billionton PCBTC1 PCMCIA Card */
  905. { "billionton", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
  906. 0, DISABLE_PM, NULL, bcsp },
  907. /* Broadcom BCM2035 */
  908. { "bcm2035", 0x0A5C, 0x2035, HCI_UART_H4, 115200, 460800,
  909. FLOW_CTL, DISABLE_PM, NULL, bcm2035 },
  910. /* Broadcom BCM43XX */
  911. { "bcm43xx", 0x0000, 0x0000, HCI_UART_H4, 115200, 3000000,
  912. FLOW_CTL, DISABLE_PM, NULL, bcm43xx, NULL },
  913. { "ath3k", 0x0000, 0x0000, HCI_UART_ATH3K, 115200, 115200,
  914. FLOW_CTL, DISABLE_PM, NULL, ath3k_ps, ath3k_pm },
  915. /* QUALCOMM BTS */
  916. { "qualcomm", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  917. FLOW_CTL, DISABLE_PM, NULL, qualcomm, NULL },
  918. /* Intel Bluetooth Module */
  919. { "intel", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  920. FLOW_CTL, DISABLE_PM, NULL, intel, NULL },
  921. /* Three-wire UART */
  922. { "3wire", 0x0000, 0x0000, HCI_UART_3WIRE, 115200, 115200,
  923. 0, DISABLE_PM, NULL, NULL, NULL },
  924. /* AMP controller UART */
  925. { "amp", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
  926. AMP_DEV, DISABLE_PM, NULL, NULL, NULL },
  927. { NULL, 0 }
  928. };
  929. static struct uart_t * get_by_id(int m_id, int p_id)
  930. {
  931. int i;
  932. for (i = 0; uart[i].type; i++) {
  933. if (uart[i].m_id == m_id && uart[i].p_id == p_id)
  934. return &uart[i];
  935. }
  936. return NULL;
  937. }
  938. static struct uart_t * get_by_type(char *type)
  939. {
  940. int i;
  941. for (i = 0; uart[i].type; i++) {
  942. if (!strcmp(uart[i].type, type))
  943. return &uart[i];
  944. }
  945. return NULL;
  946. }
  947. /* Initialize UART driver */
  948. static int init_uart(char *dev, struct uart_t *u, int send_break, int raw)
  949. {
  950. struct termios ti;
  951. int fd, i;
  952. unsigned long flags = 0;
  953. if (raw)
  954. flags |= 1 << HCI_UART_RAW_DEVICE;
  955. if (u->flags & AMP_DEV)
  956. flags |= 1 << HCI_UART_CREATE_AMP;
  957. fd = open(dev, O_RDWR | O_NOCTTY);
  958. if (fd < 0) {
  959. perror("Can't open serial port");
  960. return -1;
  961. }
  962. tcflush(fd, TCIOFLUSH);
  963. if (tcgetattr(fd, &ti) < 0) {
  964. perror("Can't get port settings");
  965. goto fail;
  966. }
  967. cfmakeraw(&ti);
  968. ti.c_cflag |= CLOCAL;
  969. if (u->flags & FLOW_CTL)
  970. ti.c_cflag |= CRTSCTS;
  971. else
  972. ti.c_cflag &= ~CRTSCTS;
  973. if (tcsetattr(fd, TCSANOW, &ti) < 0) {
  974. perror("Can't set port settings");
  975. goto fail;
  976. }
  977. /* Set initial baudrate */
  978. if (set_speed(fd, &ti, u->init_speed) < 0) {
  979. perror("Can't set initial baud rate");
  980. goto fail;
  981. }
  982. tcflush(fd, TCIOFLUSH);
  983. if (send_break) {
  984. tcsendbreak(fd, 0);
  985. usleep(500000);
  986. }
  987. if (u->init && u->init(fd, u, &ti) < 0)
  988. goto fail;
  989. tcflush(fd, TCIOFLUSH);
  990. /* Set actual baudrate */
  991. if (set_speed(fd, &ti, u->speed) < 0) {
  992. perror("Can't set baud rate");
  993. goto fail;
  994. }
  995. /* Set TTY to N_HCI line discipline */
  996. i = N_HCI;
  997. if (ioctl(fd, TIOCSETD, &i) < 0) {
  998. perror("Can't set line discipline");
  999. goto fail;
  1000. }
  1001. if (flags && ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
  1002. perror("Can't set UART flags");
  1003. goto fail;
  1004. }
  1005. if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
  1006. perror("Can't set device");
  1007. goto fail;
  1008. }
  1009. if (u->post && u->post(fd, u, &ti) < 0)
  1010. goto fail;
  1011. return fd;
  1012. fail:
  1013. close(fd);
  1014. return -1;
  1015. }
  1016. static void usage(void)
  1017. {
  1018. printf("hciattach - HCI UART driver initialization utility\n");
  1019. printf("Usage:\n");
  1020. printf("\thciattach [-n] [-p] [-b] [-r] [-t timeout] [-s initial_speed]"
  1021. " <tty> <type | id> [speed] [flow|noflow]"
  1022. " [sleep|nosleep] [bdaddr]\n");
  1023. printf("\thciattach -l\n");
  1024. }
  1025. int main(int argc, char *argv[])
  1026. {
  1027. struct uart_t *u = NULL;
  1028. int detach, printpid, raw, opt, i, n, ld, err;
  1029. int to = 10;
  1030. int init_speed = 0;
  1031. int send_break = 0;
  1032. pid_t pid;
  1033. struct sigaction sa;
  1034. struct pollfd p;
  1035. sigset_t sigs;
  1036. char dev[PATH_MAX];
  1037. detach = 1;
  1038. printpid = 0;
  1039. raw = 0;
  1040. while ((opt=getopt(argc, argv, "bnpt:s:lr")) != EOF) {
  1041. switch(opt) {
  1042. case 'b':
  1043. send_break = 1;
  1044. break;
  1045. case 'n':
  1046. detach = 0;
  1047. break;
  1048. case 'p':
  1049. printpid = 1;
  1050. break;
  1051. case 't':
  1052. to = atoi(optarg);
  1053. break;
  1054. case 's':
  1055. init_speed = atoi(optarg);
  1056. break;
  1057. case 'l':
  1058. for (i = 0; uart[i].type; i++) {
  1059. printf("%-10s0x%04x,0x%04x\n", uart[i].type,
  1060. uart[i].m_id, uart[i].p_id);
  1061. }
  1062. exit(0);
  1063. case 'r':
  1064. raw = 1;
  1065. break;
  1066. default:
  1067. usage();
  1068. exit(1);
  1069. }
  1070. }
  1071. n = argc - optind;
  1072. if (n < 2) {
  1073. usage();
  1074. exit(1);
  1075. }
  1076. for (n = 0; optind < argc; n++, optind++) {
  1077. char *opt;
  1078. opt = argv[optind];
  1079. switch(n) {
  1080. case 0:
  1081. dev[0] = 0;
  1082. if (!strchr(opt, '/'))
  1083. strcpy(dev, "/dev/");
  1084. if (strlen(opt) > PATH_MAX - (strlen(dev) + 1)) {
  1085. fprintf(stderr, "Invalid serial device\n");
  1086. exit(1);
  1087. }
  1088. strcat(dev, opt);
  1089. break;
  1090. case 1:
  1091. if (strchr(argv[optind], ',')) {
  1092. int m_id, p_id;
  1093. sscanf(argv[optind], "%x,%x", &m_id, &p_id);
  1094. u = get_by_id(m_id, p_id);
  1095. } else {
  1096. u = get_by_type(opt);
  1097. }
  1098. if (!u) {
  1099. fprintf(stderr, "Unknown device type or id\n");
  1100. exit(1);
  1101. }
  1102. break;
  1103. case 2:
  1104. u->speed = atoi(argv[optind]);
  1105. break;
  1106. case 3:
  1107. if (!strcmp("flow", argv[optind]))
  1108. u->flags |= FLOW_CTL;
  1109. else
  1110. u->flags &= ~FLOW_CTL;
  1111. break;
  1112. case 4:
  1113. if (!strcmp("sleep", argv[optind]))
  1114. u->pm = ENABLE_PM;
  1115. else
  1116. u->pm = DISABLE_PM;
  1117. break;
  1118. case 5:
  1119. u->bdaddr = argv[optind];
  1120. break;
  1121. }
  1122. }
  1123. if (!u) {
  1124. fprintf(stderr, "Unknown device type or id\n");
  1125. exit(1);
  1126. }
  1127. /* If user specified a initial speed, use that instead of
  1128. the hardware's default */
  1129. if (init_speed)
  1130. u->init_speed = init_speed;
  1131. memset(&sa, 0, sizeof(sa));
  1132. sa.sa_flags = SA_NOCLDSTOP;
  1133. sa.sa_handler = sig_alarm;
  1134. sigaction(SIGALRM, &sa, NULL);
  1135. /* 10 seconds should be enough for initialization */
  1136. alarm(to);
  1137. bcsp_max_retries = to;
  1138. n = init_uart(dev, u, send_break, raw);
  1139. if (n < 0) {
  1140. perror("Can't initialize device");
  1141. exit(1);
  1142. }
  1143. printf("Device setup complete\n");
  1144. alarm(0);
  1145. memset(&sa, 0, sizeof(sa));
  1146. sa.sa_flags = SA_NOCLDSTOP;
  1147. sa.sa_handler = SIG_IGN;
  1148. sigaction(SIGCHLD, &sa, NULL);
  1149. sigaction(SIGPIPE, &sa, NULL);
  1150. sa.sa_handler = sig_term;
  1151. sigaction(SIGTERM, &sa, NULL);
  1152. sigaction(SIGINT, &sa, NULL);
  1153. sa.sa_handler = sig_hup;
  1154. sigaction(SIGHUP, &sa, NULL);
  1155. if (detach) {
  1156. if ((pid = fork())) {
  1157. if (printpid)
  1158. printf("%d\n", pid);
  1159. return 0;
  1160. }
  1161. for (i = 0; i < 20; i++)
  1162. if (i != n)
  1163. close(i);
  1164. }
  1165. p.fd = n;
  1166. p.events = POLLERR | POLLHUP;
  1167. sigfillset(&sigs);
  1168. sigdelset(&sigs, SIGCHLD);
  1169. sigdelset(&sigs, SIGPIPE);
  1170. sigdelset(&sigs, SIGTERM);
  1171. sigdelset(&sigs, SIGINT);
  1172. sigdelset(&sigs, SIGHUP);
  1173. while (!__io_canceled) {
  1174. p.revents = 0;
  1175. err = ppoll(&p, 1, NULL, &sigs);
  1176. if (err < 0 && errno == EINTR)
  1177. continue;
  1178. if (err)
  1179. break;
  1180. }
  1181. /* Restore TTY line discipline */
  1182. ld = N_TTY;
  1183. if (ioctl(n, TIOCSETD, &ld) < 0) {
  1184. perror("Can't restore line discipline");
  1185. exit(1);
  1186. }
  1187. return 0;
  1188. }