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