3 * BlueZ - Bluetooth protocol stack for Linux
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>
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.
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.
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
42 #include <sys/param.h>
43 #include <sys/ioctl.h>
45 #include <bluetooth/bluetooth.h>
46 #include <bluetooth/hci.h>
47 #include <bluetooth/hci_lib.h>
49 #include "hciattach.h"
61 int (*init) (int fd, struct uart_t *u, struct termios *ti);
62 int (*post) (int fd, struct uart_t *u, struct termios *ti);
66 uint16_t device_param;
71 int firmware_path = 0;
74 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
75 #define TIOSETBRFPOWER 0x6000
76 #define BRF_DEEP_SLEEP_OPCODE_BYTE_1 0x0c
77 #define BRF_DEEP_SLEEP_OPCODE_BYTE_2 0xfd
78 #define BRF_DEEP_SLEEP_OPCODE \
79 (BRF_DEEP_SLEEP_OPCODE_BYTE_1 | (BRF_DEEP_SLEEP_OPCODE_BYTE_2 << 8))
81 #define FLOW_CTL 0x0001
82 #define AMP_DEV 0x0002
86 static volatile sig_atomic_t __io_canceled = 0;
88 static void sig_hup(int sig)
92 static void sig_term(int sig)
97 static void sig_alarm(int sig)
99 fprintf(stderr, "Initialization timed out.\n");
103 int uart_speed(int s)
159 int set_speed(int fd, struct termios *ti, int speed)
161 if (cfsetospeed(ti, uart_speed(speed)) < 0)
164 if (cfsetispeed(ti, uart_speed(speed)) < 0)
167 if (tcsetattr(fd, TCSANOW, ti) < 0)
174 * Read an HCI event from the given file descriptor.
176 int read_hci_event(int fd, unsigned char* buf, int size)
184 /* The first byte identifies the packet type. For HCI event packets, it
185 * should be 0x04, so we read until we get to the 0x04. */
187 r = read(fd, buf, 1);
195 /* The next two bytes are the event code and parameter total length. */
197 r = read(fd, buf + count, 3 - count);
203 /* Now we read the parameters. */
204 if (buf[2] < (size - 3))
209 while ((count - 3) < remain) {
210 r = read(fd, buf + count, remain - (count - 3));
220 * Ericsson specific initialization
222 static int ericsson(int fd, struct uart_t *u, struct termios *ti)
224 struct timespec tm = {0, 50000};
227 cmd[0] = HCI_COMMAND_PKT;
260 fprintf(stderr, "Invalid speed requested, using %d bps instead\n", u->speed);
264 /* Send initialization command */
265 if (write(fd, cmd, 5) != 5) {
266 perror("Failed to write init command");
270 nanosleep(&tm, NULL);
275 * Digianswer specific initialization
277 static int digi(int fd, struct uart_t *u, struct termios *ti)
279 struct timespec tm = {0, 50000};
282 /* DigiAnswer set baud rate command */
283 cmd[0] = HCI_COMMAND_PKT;
301 /* Send initialization command */
302 if (write(fd, cmd, 5) != 5) {
303 perror("Failed to write init command");
307 nanosleep(&tm, NULL);
311 static int texas(int fd, struct uart_t *u, struct termios *ti)
313 return texas_init(fd, &u->speed, ti);
316 static int texas2(int fd, struct uart_t *u, struct termios *ti)
318 return texas_post(fd, ti);
321 static int texasalt(int fd, struct uart_t *u, struct termios *ti)
323 return texasalt_init(fd, u->speed, ti);
326 static int ath3k_ps(int fd, struct uart_t *u, struct termios *ti)
328 return ath3k_init(fd, u->speed, u->init_speed, u->bdaddr, ti);
331 static int ath3k_pm(int fd, struct uart_t *u, struct termios *ti)
333 return ath3k_post(fd, u->pm);
336 static int qualcomm(int fd, struct uart_t *u, struct termios *ti)
338 return qualcomm_init(fd, u->speed, ti, u->bdaddr);
341 static int intel(int fd, struct uart_t *u, struct termios *ti)
343 return intel_init(fd, u->init_speed, &u->speed, ti);
346 static int bcm43xx(int fd, struct uart_t *u, struct termios *ti)
348 return bcm43xx_init(fd, u->speed, ti, u->bdaddr);
351 static int read_check(int fd, void *buf, int count)
356 res = read(fd, buf, count);
361 } while (count && (errno == 0 || errno == EINTR));
370 * BCSP specific initialization
372 static int serial_fd;
373 static int bcsp_max_retries = 10;
375 static void bcsp_tshy_sig_alarm(int sig)
377 unsigned char bcsp_sync_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xda,0xdc,0xed,0xed,0xc0};
378 static int retries = 0;
380 if (retries < bcsp_max_retries) {
382 if (write(serial_fd, &bcsp_sync_pkt, 10) < 0)
388 tcflush(serial_fd, TCIOFLUSH);
389 fprintf(stderr, "BCSP initialization timed out\n");
393 static void bcsp_tconf_sig_alarm(int sig)
395 unsigned char bcsp_conf_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xad,0xef,0xac,0xed,0xc0};
396 static int retries = 0;
398 if (retries < bcsp_max_retries){
400 if (write(serial_fd, &bcsp_conf_pkt, 10) < 0)
406 tcflush(serial_fd, TCIOFLUSH);
407 fprintf(stderr, "BCSP initialization timed out\n");
411 static int bcsp(int fd, struct uart_t *u, struct termios *ti)
413 unsigned char byte, bcsph[4], bcspp[4],
414 bcsp_sync_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xac,0xaf,0xef,0xee,0xc0},
415 bcsp_conf_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xde,0xad,0xd0,0xd0,0xc0},
416 bcspsync[4] = {0xda, 0xdc, 0xed, 0xed},
417 bcspsyncresp[4] = {0xac,0xaf,0xef,0xee},
418 bcspconf[4] = {0xad,0xef,0xac,0xed},
419 bcspconfresp[4] = {0xde,0xad,0xd0,0xd0};
423 if (set_speed(fd, ti, u->speed) < 0) {
424 perror("Can't set default baud rate");
428 ti->c_cflag |= PARENB;
429 ti->c_cflag &= ~(PARODD);
431 if (tcsetattr(fd, TCSANOW, ti) < 0) {
432 perror("Can't set port settings");
439 memset(&sa, 0, sizeof(sa));
440 sa.sa_flags = SA_NOCLDSTOP;
441 sa.sa_handler = bcsp_tshy_sig_alarm;
442 sigaction(SIGALRM, &sa, NULL);
446 bcsp_tshy_sig_alarm(0);
449 if (read_check(fd, &byte, 1) == -1){
450 perror("Failed to read");
453 } while (byte != 0xC0);
456 if ( read_check(fd, &bcsph[0], 1) == -1){
457 perror("Failed to read");
460 } while (bcsph[0] == 0xC0);
462 if ( read_check(fd, &bcsph[1], 3) == -1){
463 perror("Failed to read");
467 if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
469 if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
472 if (read_check(fd, &bcspp, 4) == -1){
473 perror("Failed to read");
477 if (!memcmp(bcspp, bcspsync, 4)) {
478 if (write(fd, &bcsp_sync_resp_pkt,10) < 0)
480 } else if (!memcmp(bcspp, bcspsyncresp, 4))
484 /* State = curious */
487 sa.sa_handler = bcsp_tconf_sig_alarm;
488 sigaction(SIGALRM, &sa, NULL);
493 if (read_check(fd, &byte, 1) == -1){
494 perror("Failed to read");
497 } while (byte != 0xC0);
500 if (read_check(fd, &bcsph[0], 1) == -1){
501 perror("Failed to read");
504 } while (bcsph[0] == 0xC0);
506 if (read_check(fd, &bcsph[1], 3) == -1){
507 perror("Failed to read");
511 if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
514 if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
517 if (read_check(fd, &bcspp, 4) == -1){
518 perror("Failed to read");
522 if (!memcmp(bcspp, bcspsync, 4))
523 len = write(fd, &bcsp_sync_resp_pkt, 10);
524 else if (!memcmp(bcspp, bcspconf, 4))
525 len = write(fd, &bcsp_conf_resp_pkt, 10);
526 else if (!memcmp(bcspp, bcspconfresp, 4))
535 /* State = garrulous */
541 * CSR specific initialization
542 * Inspired strongly by code in OpenBT and experimentations with Brainboxes
544 * Jean Tourrilhes <jt@hpl.hp.com> - 14.11.01
546 static int csr(int fd, struct uart_t *u, struct termios *ti)
548 struct timespec tm = {0, 10000000}; /* 10ms - be generous */
549 unsigned char cmd[30]; /* Command */
550 unsigned char resp[30]; /* Response */
551 int clen = 0; /* Command len */
552 static int csr_seq = 0; /* Sequence number of command */
555 /* It seems that if we set the CSR UART speed straight away, it
556 * won't work, the CSR UART gets into a state where we can't talk
558 * On the other hand, doing a read before setting the CSR speed
560 * Therefore, the strategy is to read the build ID (useful for
561 * debugging) and only then set the CSR UART speed. Doing like
562 * this is more complex but at least it works ;-)
563 * The CSR UART control may be slow to wake up or something because
564 * every time I read its speed, its bogus...
567 /* Try to read the build ID of the CSR chip */
568 clen = 5 + (5 + 6) * 2;
570 cmd[0] = HCI_COMMAND_PKT;
571 cmd[1] = 0x00; /* CSR command */
572 cmd[2] = 0xfc; /* MANUFACTURER_SPEC */
573 cmd[3] = 1 + (5 + 6) * 2; /* len */
575 cmd[4] = 0xC2; /* first+last+channel=BCC */
577 cmd[5] = 0x00; /* type = GET-REQ */
578 cmd[6] = 0x00; /* - msB */
579 cmd[7] = 5 + 4; /* len */
580 cmd[8] = 0x00; /* - msB */
581 cmd[9] = csr_seq & 0xFF;/* seq num */
582 cmd[10] = (csr_seq >> 8) & 0xFF; /* - msB */
584 cmd[11] = 0x19; /* var_id = CSR_CMD_BUILD_ID */
585 cmd[12] = 0x28; /* - msB */
586 cmd[13] = 0x00; /* status = STATUS_OK */
587 cmd[14] = 0x00; /* - msB */
588 /* CSR BCC payload */
589 memset(cmd + 15, 0, 6 * 2);
593 if (write(fd, cmd, clen) != clen) {
594 perror("Failed to write init command (GET_BUILD_ID)");
599 if (read_hci_event(fd, resp, 100) < 0) {
600 perror("Failed to read init response (GET_BUILD_ID)");
604 /* Event code 0xFF is for vendor-specific events, which is
605 * what we're looking for. */
606 } while (resp[1] != 0xFF);
612 for (i=0; i < rlen; i++)
613 sprintf(temp + (i*3), "-%02X", resp[i]);
614 fprintf(stderr, "Reading CSR build ID %d [%s]\n", rlen, temp + 1);
615 // In theory, it should look like :
616 // 04-FF-13-FF-01-00-09-00-00-00-19-28-00-00-73-00-00-00-00-00-00-00
619 /* Display that to user */
620 fprintf(stderr, "CSR build ID 0x%02X-0x%02X\n",
621 resp[15] & 0xFF, resp[14] & 0xFF);
623 /* Try to read the current speed of the CSR chip */
624 clen = 5 + (5 + 4)*2;
626 cmd[3] = 1 + (5 + 4)*2; /* len */
627 /* -- CSR BCC header -- */
628 cmd[9] = csr_seq & 0xFF; /* seq num */
629 cmd[10] = (csr_seq >> 8) & 0xFF; /* - msB */
631 cmd[11] = 0x02; /* var_id = CONFIG_UART */
632 cmd[12] = 0x68; /* - msB */
637 if (write(fd, cmd, clen) != clen) {
638 perror("Failed to write init command (GET_BUILD_ID)");
643 if (read_hci_event(fd, resp, 100) < 0) {
644 perror("Failed to read init response (GET_BUILD_ID)");
648 /* Event code 0xFF is for vendor-specific events, which is
649 * what we're looking for. */
650 } while (resp[1] != 0xFF);
655 for (i=0; i < rlen; i++)
656 sprintf(temp + (i*3), "-%02X", resp[i]);
657 fprintf(stderr, "Reading CSR UART speed %d [%s]\n", rlen, temp+1);
661 if (u->speed > 1500000) {
662 fprintf(stderr, "Speed %d too high. Remaining at %d baud\n",
663 u->speed, u->init_speed);
664 u->speed = u->init_speed;
665 } else if (u->speed != 57600 && uart_speed(u->speed) == B57600) {
666 /* Unknown speed. Why oh why can't we just pass an int to the kernel? */
667 fprintf(stderr, "Speed %d unrecognised. Remaining at %d baud\n",
668 u->speed, u->init_speed);
669 u->speed = u->init_speed;
671 if (u->speed == u->init_speed)
674 /* Now, create the command that will set the UART speed */
676 cmd[5] = 0x02; /* type = SET-REQ */
677 cmd[6] = 0x00; /* - msB */
678 cmd[9] = csr_seq & 0xFF; /* seq num */
679 cmd[10] = (csr_seq >> 8) & 0xFF;/* - msB */
682 divisor = (u->speed*64+7812)/15625;
684 /* No parity, one stop bit -> divisor |= 0x0000; */
685 cmd[15] = (divisor) & 0xFF; /* divider */
686 cmd[16] = (divisor >> 8) & 0xFF; /* - msB */
687 /* The rest of the payload will be 0x00 */
693 for(i = 0; i < clen; i++)
694 sprintf(temp + (i*3), "-%02X", cmd[i]);
695 fprintf(stderr, "Writing CSR UART speed %d [%s]\n", clen, temp + 1);
696 // In theory, it should look like :
697 // 01-00-FC-13-C2-02-00-09-00-03-00-02-68-00-00-BF-0E-00-00-00-00-00-00
698 // 01-00-FC-13-C2-02-00-09-00-01-00-02-68-00-00-D8-01-00-00-00-00-00-00
702 /* Send the command to set the CSR UART speed */
703 if (write(fd, cmd, clen) != clen) {
704 perror("Failed to write init command (SET_UART_SPEED)");
708 nanosleep(&tm, NULL);
713 * Silicon Wave specific initialization
714 * Thomas Moser <thomas.moser@tmoser.ch>
716 static int swave(int fd, struct uart_t *u, struct termios *ti)
718 struct timespec tm = { 0, 500000 };
719 char cmd[10], rsp[100];
722 // Silicon Wave set baud rate command
723 // see HCI Vendor Specific Interface from Silicon Wave
724 // first send a "param access set" command to set the
725 // appropriate data fields in RAM. Then send a "HCI Reset
726 // Subcommand", e.g. "soft reset" to make the changes effective.
728 cmd[0] = HCI_COMMAND_PKT; // it's a command packet
729 cmd[1] = 0x0B; // OCF 0x0B = param access set
730 cmd[2] = 0xfc; // OGF bx111111 = vendor specific
731 cmd[3] = 0x06; // 6 bytes of data following
732 cmd[4] = 0x01; // param sub command
733 cmd[5] = 0x11; // tag 17 = 0x11 = HCI Transport Params
734 cmd[6] = 0x03; // length of the parameter following
735 cmd[7] = 0x01; // HCI Transport flow control enable
736 cmd[8] = 0x01; // HCI Transport Type = UART
757 /* Send initialization command */
758 if (write(fd, cmd, 10) != 10) {
759 perror("Failed to write init command");
763 // We should wait for a "GET Event" to confirm the success of
764 // the baud rate setting. Wait some time before reading. Better:
765 // read with timeout, parse data
766 // until correct answer, else error handling ... todo ...
768 nanosleep(&tm, NULL);
770 r = read(fd, rsp, sizeof(rsp));
772 // guess it's okay, but we should parse the reply. But since
773 // I don't react on an error anyway ... todo
774 // Response packet format:
776 // FF Vendor specific
777 // 07 Parameter length
780 // 11 Tag specifying HCI Transport Layer Parameter
783 // 01 Hci Transport type = Uart
784 // xx Baud rate set (see above)
790 // we probably got the reply. Now we must send the "soft reset"
791 // which is standard HCI RESET.
793 cmd[0] = HCI_COMMAND_PKT; // it's a command packet
798 /* Send reset command */
799 if (write(fd, cmd, 4) != 4) {
800 perror("Can't write Silicon Wave reset cmd.");
804 nanosleep(&tm, NULL);
806 // now the uart baud rate on the silicon wave module is set and effective.
807 // change our own baud rate as well. Then there is a reset event coming in
808 // on the *new* baud rate. This is *undocumented*! The packet looks like this:
809 // 04 FF 01 0B (which would make that a confirmation of 0x0B = "Param
810 // subcommand class". So: change to new baud rate, read with timeout, parse
811 // data, error handling. BTW: all param access in Silicon Wave is done this way.
812 // Maybe this code would belong in a separate file, or at least code reuse...
818 * ST Microelectronics specific initialization
819 * Marcel Holtmann <marcel@holtmann.org>
821 static int st(int fd, struct uart_t *u, struct termios *ti)
823 struct timespec tm = {0, 50000};
826 /* ST Microelectronics set baud rate command */
827 cmd[0] = HCI_COMMAND_PKT;
828 cmd[1] = 0x46; // OCF = Hci_Cmd_ST_Set_Uart_Baud_Rate
829 cmd[2] = 0xfc; // OGF = Vendor specific
863 /* Send initialization command */
864 if (write(fd, cmd, 5) != 5) {
865 perror("Failed to write init command");
869 nanosleep(&tm, NULL);
873 static int stlc2500(int fd, struct uart_t *u, struct termios *ti)
876 unsigned char resp[10];
880 /* STLC2500 has an ericsson core */
881 rvalue = ericsson(fd, u, ti);
885 #ifdef STLC2500_DEBUG
886 fprintf(stderr, "Setting speed\n");
888 if (set_speed(fd, ti, u->speed) < 0) {
889 perror("Can't set baud rate");
893 #ifdef STLC2500_DEBUG
894 fprintf(stderr, "Speed set...\n");
898 if ((n = read_hci_event(fd, resp, 10)) < 0) {
899 fprintf(stderr, "Failed to set baud rate on chip\n");
903 #ifdef STLC2500_DEBUG
904 for (i = 0; i < n; i++) {
905 fprintf(stderr, "resp[%d] = %02x\n", i, resp[i]);
909 str2ba(u->bdaddr, &bdaddr);
910 return stlc2500_init(fd, &bdaddr);
913 static int bgb2xx(int fd, struct uart_t *u, struct termios *ti)
917 str2ba(u->bdaddr, &bdaddr);
919 return bgb2xx_init(fd, &bdaddr);
923 * Broadcom specific initialization
924 * Extracted from Jungo openrg
926 static int bcm2035(int fd, struct uart_t *u, struct termios *ti)
929 unsigned char cmd[30], resp[30];
931 /* Reset the BT Chip */
932 memset(cmd, 0, sizeof(cmd));
933 memset(resp, 0, sizeof(resp));
934 cmd[0] = HCI_COMMAND_PKT;
940 if (write(fd, cmd, 4) != 4) {
941 fprintf(stderr, "Failed to write reset command\n");
946 if ((n = read_hci_event(fd, resp, 4)) < 0) {
947 fprintf(stderr, "Failed to reset chip\n");
951 if (u->bdaddr != NULL) {
953 memset(cmd, 0, sizeof(cmd));
954 memset(resp, 0, sizeof(resp));
955 cmd[0] = HCI_COMMAND_PKT;
959 str2ba(u->bdaddr, (bdaddr_t *) (cmd + 4));
962 if (write(fd, cmd, 10) != 10) {
963 fprintf(stderr, "Failed to write BD_ADDR command\n");
968 if ((n = read_hci_event(fd, resp, 10)) < 0) {
969 fprintf(stderr, "Failed to set BD_ADDR\n");
974 /* Read the local version info */
975 memset(cmd, 0, sizeof(cmd));
976 memset(resp, 0, sizeof(resp));
977 cmd[0] = HCI_COMMAND_PKT;
983 if (write(fd, cmd, 4) != 4) {
984 fprintf(stderr, "Failed to write \"read local version\" "
990 if ((n = read_hci_event(fd, resp, 4)) < 0) {
991 fprintf(stderr, "Failed to read local version\n");
995 /* Read the local supported commands info */
996 memset(cmd, 0, sizeof(cmd));
997 memset(resp, 0, sizeof(resp));
998 cmd[0] = HCI_COMMAND_PKT;
1004 if (write(fd, cmd, 4) != 4) {
1005 fprintf(stderr, "Failed to write \"read local supported "
1006 "commands\" command\n");
1011 if ((n = read_hci_event(fd, resp, 4)) < 0) {
1012 fprintf(stderr, "Failed to read local supported commands\n");
1016 /* Set the baud rate */
1017 memset(cmd, 0, sizeof(cmd));
1018 memset(resp, 0, sizeof(resp));
1020 /* __TIZEN_PATCH__ */
1021 #ifndef __BROADCOM_PATCH__
1022 cmd[0] = HCI_COMMAND_PKT;
1044 /* Default is 115200 */
1049 fprintf(stderr, "Baud rate parameters: DHBR=0x%2x,DLBR=0x%2x\n",
1053 if (write(fd, cmd, 6) != 6) {
1054 fprintf(stderr, "Failed to write \"set baud rate\" command\n");
1058 cmd[0] = HCI_COMMAND_PKT;
1076 cmd[6] = u->speed & 0xFF;
1077 cmd[7] = (u->speed >> 8) & 0xFF;
1078 cmd[8] = (u->speed >> 16) & 0xFF;
1079 cmd[9] = (u->speed >> 24) & 0xFF;
1081 fprintf(stderr, "Set the baud rate %d : 0x%02x,0x%02x,0x%02x,0x%02x\n",u->speed,cmd[6],cmd[7],cmd[8],cmd[9] );
1084 if (write(fd, cmd, 10) != 10) {
1085 fprintf(stderr, "Failed to write \"set baud rate\" command\n");
1091 if ((n = read_hci_event(fd, resp, 6)) < 0) {
1092 fprintf(stderr, "Failed to set baud rate\n");
1099 struct uart_t uart[] = {
1100 { "any", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1101 FLOW_CTL, DISABLE_PM, NULL, NULL },
1103 { "ericsson", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200,
1104 FLOW_CTL, DISABLE_PM, NULL, ericsson },
1106 { "digi", 0x0000, 0x0000, HCI_UART_H4, 9600, 115200,
1107 FLOW_CTL, DISABLE_PM, NULL, digi },
1109 { "bcsp", 0x0000, 0x0000, HCI_UART_BCSP, 115200, 115200,
1110 0, DISABLE_PM, NULL, bcsp },
1112 /* Xircom PCMCIA cards: Credit Card Adapter and Real Port Adapter */
1113 { "xircom", 0x0105, 0x080a, HCI_UART_H4, 115200, 115200,
1114 FLOW_CTL, DISABLE_PM, NULL, NULL },
1116 /* CSR Casira serial adapter or BrainBoxes serial dongle (BL642) */
1117 { "csr", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1118 FLOW_CTL, DISABLE_PM, NULL, csr },
1120 /* BrainBoxes PCMCIA card (BL620) */
1121 { "bboxes", 0x0160, 0x0002, HCI_UART_H4, 115200, 460800,
1122 FLOW_CTL, DISABLE_PM, NULL, csr },
1124 /* Silicon Wave kits */
1125 { "swave", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1126 FLOW_CTL, DISABLE_PM, NULL, swave },
1128 /* __TIZEN_PATCH__ */
1130 /* Texas Instruments BRF63xx modules */
1131 { "texas", 0x0000, 0x0000, HCI_UART_LL, 115200,3000000, FLOW_CTL, NULL, texas, NULL/*texas_continue_script*/, BRF_DEEP_SLEEP_OPCODE},
1133 /* Texas Instruments Bluelink (BRF) modules */
1134 { "texas", 0x0000, 0x0000, HCI_UART_LL, 115200, 115200,
1135 FLOW_CTL, DISABLE_PM, NULL, texas, texas2 },
1137 { "texasalt", 0x0000, 0x0000, HCI_UART_LL, 115200, 115200,
1138 FLOW_CTL, DISABLE_PM, NULL, texasalt, NULL },
1141 /* ST Microelectronics minikits based on STLC2410/STLC2415 */
1142 { "st", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200,
1143 FLOW_CTL, DISABLE_PM, NULL, st },
1145 /* ST Microelectronics minikits based on STLC2500 */
1146 { "stlc2500", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1147 FLOW_CTL, DISABLE_PM, "00:80:E1:00:AB:BA", stlc2500 },
1149 /* Philips generic Ericsson IP core based */
1150 { "philips", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1151 FLOW_CTL, DISABLE_PM, NULL, NULL },
1153 /* Philips BGB2xx Module */
1154 { "bgb2xx", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1155 FLOW_CTL, DISABLE_PM, "BD:B2:10:00:AB:BA", bgb2xx },
1157 /* Sphinx Electronics PICO Card */
1158 { "picocard", 0x025e, 0x1000, HCI_UART_H4, 115200, 115200,
1159 FLOW_CTL, DISABLE_PM, NULL, NULL },
1161 /* Inventel BlueBird Module */
1162 { "inventel", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1163 FLOW_CTL, DISABLE_PM, NULL, NULL },
1165 /* COM One Platinium Bluetooth PC Card */
1166 { "comone", 0xffff, 0x0101, HCI_UART_BCSP, 115200, 115200,
1167 0, DISABLE_PM, NULL, bcsp },
1169 /* TDK Bluetooth PC Card and IBM Bluetooth PC Card II */
1170 { "tdk", 0x0105, 0x4254, HCI_UART_BCSP, 115200, 115200,
1171 0, DISABLE_PM, NULL, bcsp },
1173 /* Socket Bluetooth CF Card (Rev G) */
1174 { "socket", 0x0104, 0x0096, HCI_UART_BCSP, 230400, 230400,
1175 0, DISABLE_PM, NULL, bcsp },
1177 /* 3Com Bluetooth Card (Version 3.0) */
1178 { "3com", 0x0101, 0x0041, HCI_UART_H4, 115200, 115200,
1179 FLOW_CTL, DISABLE_PM, NULL, csr },
1181 /* AmbiCom BT2000C Bluetooth PC/CF Card */
1182 { "bt2000c", 0x022d, 0x2000, HCI_UART_H4, 57600, 460800,
1183 FLOW_CTL, DISABLE_PM, NULL, csr },
1185 /* Zoom Bluetooth PCMCIA Card */
1186 { "zoom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1187 0, DISABLE_PM, NULL, bcsp },
1189 /* Sitecom CN-504 PCMCIA Card */
1190 { "sitecom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1191 0, DISABLE_PM, NULL, bcsp },
1193 /* Billionton PCBTC1 PCMCIA Card */
1194 { "billionton", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1195 0, DISABLE_PM, NULL, bcsp },
1197 /* Broadcom BCM2035 */
1198 { "bcm2035", 0x0A5C, 0x2035, HCI_UART_H4, 115200, 460800,
1199 FLOW_CTL, DISABLE_PM, NULL, bcm2035 },
1201 /* Broadcom BCM43XX */
1202 { "bcm43xx", 0x0000, 0x0000, HCI_UART_H4, 115200, 3000000,
1203 FLOW_CTL, DISABLE_PM, NULL, bcm43xx, NULL },
1205 { "ath3k", 0x0000, 0x0000, HCI_UART_ATH3K, 115200, 115200,
1206 FLOW_CTL, DISABLE_PM, NULL, ath3k_ps, ath3k_pm },
1209 { "qualcomm", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1210 FLOW_CTL, DISABLE_PM, NULL, qualcomm, NULL },
1212 /* Intel Bluetooth Module */
1213 { "intel", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1214 FLOW_CTL, DISABLE_PM, NULL, intel, NULL },
1216 /* Three-wire UART */
1217 { "3wire", 0x0000, 0x0000, HCI_UART_3WIRE, 115200, 115200,
1218 0, DISABLE_PM, NULL, NULL, NULL },
1220 /* AMP controller UART */
1221 { "amp", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1222 AMP_DEV, DISABLE_PM, NULL, NULL, NULL },
1227 static struct uart_t * get_by_id(int m_id, int p_id)
1230 for (i = 0; uart[i].type; i++) {
1231 if (uart[i].m_id == m_id && uart[i].p_id == p_id)
1237 static struct uart_t * get_by_type(char *type)
1240 for (i = 0; uart[i].type; i++) {
1241 if (!strcmp(uart[i].type, type))
1247 /* Initialize UART driver */
1248 static int init_uart(char *dev, struct uart_t *u, int send_break, int raw)
1252 unsigned long flags = 0;
1254 /* __TIZEN_PATCH__ */
1255 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1260 flags |= 1 << HCI_UART_RAW_DEVICE;
1262 if (u->flags & AMP_DEV)
1263 flags |= 1 << HCI_UART_CREATE_AMP;
1265 fd = open(dev, O_RDWR | O_NOCTTY);
1267 perror("Can't open serial port");
1271 tcflush(fd, TCIOFLUSH);
1273 if (tcgetattr(fd, &ti) < 0) {
1274 perror("Can't get port settings");
1280 /* __TIZEN_PATCH__ */
1281 #ifndef __BROADCOM_PATCH__
1282 ti.c_cflag |= CLOCAL;
1283 if (u->flags & FLOW_CTL)
1284 ti.c_cflag |= CRTSCTS;
1286 ti.c_cflag &= ~CRTSCTS;
1289 if (tcsetattr(fd, TCSANOW, &ti) < 0) {
1290 perror("Can't set port settings");
1294 /* Set initial baudrate */
1295 if (set_speed(fd, &ti, u->init_speed) < 0) {
1296 perror("Can't set initial baud rate");
1300 tcflush(fd, TCIOFLUSH);
1307 /* __TIZEN_PATCH__ */
1308 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1309 /* Power up the BRF chip */
1311 ioctl(fd, TIOSETBRFPOWER, &power);
1317 if (u->init && u->init(fd, u, &ti) < 0)
1320 tcflush(fd, TCIOFLUSH);
1322 /* Set actual baudrate */
1323 if (set_speed(fd, &ti, u->speed) < 0) {
1324 perror("Can't set baud rate");
1328 /* Set TTY to N_HCI line discipline */
1330 if (ioctl(fd, TIOCSETD, &i) < 0) {
1331 perror("Can't set line discipline");
1335 if (flags && ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
1336 perror("Can't set UART flags");
1340 if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
1341 perror("Can't set device");
1345 if (u->post && u->post(fd, u, &ti) < 0)
1351 static void usage(void)
1353 printf("hciattach - HCI UART driver initialization utility\n");
1356 /* __TIZEN_PATCH__ */
1358 printf("\thciattach [-n] [-p] [-b] [-g device_param] [-r] [-f] [-t timeout] [-s initial_speed] <tty> <type | id> [speed] [flow|noflow] [bdaddr]\n");
1360 printf("\thciattach [-n] [-p] [-b] [-r] [-t timeout] [-s initial_speed] <tty> <type | id> [speed] [flow|noflow] [bdaddr]\n");
1362 printf("\thciattach -l\n");
1365 int main(int argc, char *argv[])
1367 struct uart_t *u = NULL;
1368 int detach, printpid, raw, opt, i, n, ld, err;
1373 struct sigaction sa;
1378 /* __TIZEN_PATCH__ */
1379 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1383 uint16_t device_param = 0;
1384 int reset_device = 0;
1391 while ((opt=getopt(argc, argv, "bnprft:g:s:l")) != EOF) {
1393 while ((opt=getopt(argc, argv, "bnpt:s:lr")) != EOF) {
1412 /* __TIZEN_PATCH__ */
1415 device_param = (uint16_t)strtol(optarg, NULL, 16);
1427 init_speed = atoi(optarg);
1431 for (i = 0; uart[i].type; i++) {
1432 printf("%-10s0x%04x,0x%04x\n", uart[i].type,
1433 uart[i].m_id, uart[i].p_id);
1448 /* __TIZEN_PATCH__ */
1450 if (!reset_device || (reset_device && n < 1))
1457 for (n = 0; optind < argc; n++, optind++) {
1465 if (!strchr(opt, '/'))
1466 strcpy(dev, "/dev/");
1467 strncat(dev, opt, PATH_MAX);
1471 if (strchr(argv[optind], ',')) {
1473 sscanf(argv[optind], "%x,%x", &m_id, &p_id);
1474 u = get_by_id(m_id, p_id);
1476 u = get_by_type(opt);
1480 fprintf(stderr, "Unknown device type or id\n");
1487 u->speed = atoi(argv[optind]);
1491 if (!strcmp("flow", argv[optind]))
1492 u->flags |= FLOW_CTL;
1494 u->flags &= ~FLOW_CTL;
1498 if (!strcmp("sleep", argv[optind]))
1505 u->bdaddr = argv[optind];
1509 /* __TIZEN_PATCH__ */
1514 bt_fd = open(dev, O_RDWR | O_NOCTTY);
1516 perror("Can't open serial port");
1519 /* Power up the BRF chip */
1521 ioctl(bt_fd, TIOSETBRFPOWER, &power);
1527 fprintf(stderr, "Unknown device type or id\n");
1531 /* If user specified a initial speed, use that instead of
1532 the hardware's default */
1534 u->init_speed = init_speed;
1536 /* If user specified a device parameter, use that instead of
1537 the hardware's default */
1539 u->device_param = device_param;
1542 memset(&sa, 0, sizeof(sa));
1543 sa.sa_flags = SA_NOCLDSTOP;
1544 sa.sa_handler = sig_alarm;
1545 sigaction(SIGALRM, &sa, NULL);
1547 /* 10 seconds should be enough for initialization */
1549 bcsp_max_retries = to;
1551 n = init_uart(dev, u, send_break, raw);
1553 perror("Can't initialize device");
1557 printf("Device setup complete\n");
1561 memset(&sa, 0, sizeof(sa));
1562 sa.sa_flags = SA_NOCLDSTOP;
1563 sa.sa_handler = SIG_IGN;
1564 sigaction(SIGCHLD, &sa, NULL);
1565 sigaction(SIGPIPE, &sa, NULL);
1567 sa.sa_handler = sig_term;
1568 sigaction(SIGTERM, &sa, NULL);
1569 sigaction(SIGINT, &sa, NULL);
1571 sa.sa_handler = sig_hup;
1572 sigaction(SIGHUP, &sa, NULL);
1575 if ((pid = fork())) {
1577 printf("%d\n", pid);
1581 for (i = 0; i < 20; i++)
1587 p.events = POLLERR | POLLHUP;
1590 sigdelset(&sigs, SIGCHLD);
1591 sigdelset(&sigs, SIGPIPE);
1592 sigdelset(&sigs, SIGTERM);
1593 sigdelset(&sigs, SIGINT);
1594 sigdelset(&sigs, SIGHUP);
1596 while (!__io_canceled) {
1598 err = ppoll(&p, 1, NULL, &sigs);
1599 if (err < 0 && errno == EINTR)
1605 /* Restore TTY line discipline */
1607 if (ioctl(n, TIOCSETD, &ld) < 0) {
1608 perror("Can't restore line discipline");
1612 /* __TIZEN_PATCH__ */
1613 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1614 /* Power down the BRF or BCMchip */
1616 ioctl(n, TIOSETBRFPOWER, &power);