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