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
30 #ifndef __TIZEN_PATCH__
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
48 #include "lib/bluetooth.h"
50 #include "lib/hci_lib.h"
52 #include "hciattach.h"
64 int (*init) (int fd, struct uart_t *u, struct termios *ti);
65 int (*post) (int fd, struct uart_t *u, struct termios *ti);
68 #if defined __TIZEN_PATCH__ && defined __TI_PATCH__
69 uint16_t device_param;
73 #if defined __TIZEN_PATCH__ && defined __TI_PATCH__
74 int firmware_path = 0;
77 #ifdef __TIZEN_PATCH__
78 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
79 #define TIOSETBRFPOWER 0x6000
80 #define BRF_DEEP_SLEEP_OPCODE_BYTE_1 0x0c
81 #define BRF_DEEP_SLEEP_OPCODE_BYTE_2 0xfd
82 #define BRF_DEEP_SLEEP_OPCODE \
83 (BRF_DEEP_SLEEP_OPCODE_BYTE_1 | (BRF_DEEP_SLEEP_OPCODE_BYTE_2 << 8))
86 #define FLOW_CTL 0x0001
87 #define AMP_DEV 0x0002
91 static volatile sig_atomic_t __io_canceled = 0;
93 static void sig_hup(int sig)
97 static void sig_term(int sig)
102 static void sig_alarm(int sig)
104 fprintf(stderr, "Initialization timed out.\n");
108 int uart_speed(int s)
164 int set_speed(int fd, struct termios *ti, int speed)
166 if (cfsetospeed(ti, uart_speed(speed)) < 0)
169 if (cfsetispeed(ti, uart_speed(speed)) < 0)
172 if (tcsetattr(fd, TCSANOW, ti) < 0)
179 * Read an HCI event from the given file descriptor.
181 int read_hci_event(int fd, unsigned char* buf, int size)
189 /* The first byte identifies the packet type. For HCI event packets, it
190 * should be 0x04, so we read until we get to the 0x04. */
192 r = read(fd, buf, 1);
200 /* The next two bytes are the event code and parameter total length. */
202 r = read(fd, buf + count, 3 - count);
208 /* Now we read the parameters. */
209 if (buf[2] < (size - 3))
214 while ((count - 3) < remain) {
215 r = read(fd, buf + count, remain - (count - 3));
225 * Ericsson specific initialization
227 static int ericsson(int fd, struct uart_t *u, struct termios *ti)
229 struct timespec tm = {0, 50000};
232 cmd[0] = HCI_COMMAND_PKT;
265 fprintf(stderr, "Invalid speed requested, using %d bps instead\n", u->speed);
269 /* Send initialization command */
270 if (write(fd, cmd, 5) != 5) {
271 perror("Failed to write init command");
275 nanosleep(&tm, NULL);
280 * Digianswer specific initialization
282 static int digi(int fd, struct uart_t *u, struct termios *ti)
284 struct timespec tm = {0, 50000};
287 /* DigiAnswer set baud rate command */
288 cmd[0] = HCI_COMMAND_PKT;
306 /* Send initialization command */
307 if (write(fd, cmd, 5) != 5) {
308 perror("Failed to write init command");
312 nanosleep(&tm, NULL);
316 static int texas(int fd, struct uart_t *u, struct termios *ti)
318 return texas_init(fd, &u->speed, ti);
321 static int texas2(int fd, struct uart_t *u, struct termios *ti)
323 return texas_post(fd, ti);
326 static int texasalt(int fd, struct uart_t *u, struct termios *ti)
328 return texasalt_init(fd, u->speed, ti);
331 static int ath3k_ps(int fd, struct uart_t *u, struct termios *ti)
333 return ath3k_init(fd, u->speed, u->init_speed, u->bdaddr, ti);
336 static int ath3k_pm(int fd, struct uart_t *u, struct termios *ti)
338 return ath3k_post(fd, u->pm);
341 static int qualcomm(int fd, struct uart_t *u, struct termios *ti)
343 return qualcomm_init(fd, u->speed, ti, u->bdaddr);
346 static int intel(int fd, struct uart_t *u, struct termios *ti)
348 return intel_init(fd, u->init_speed, &u->speed, ti);
351 static int bcm43xx(int fd, struct uart_t *u, struct termios *ti)
353 return bcm43xx_init(fd, u->init_speed, u->speed, ti, u->bdaddr);
356 static int read_check(int fd, void *buf, int count)
361 res = read(fd, buf, count);
366 } while (count && (errno == 0 || errno == EINTR));
375 * BCSP specific initialization
377 static int serial_fd;
378 static int bcsp_max_retries = 10;
380 static void bcsp_tshy_sig_alarm(int sig)
382 unsigned char bcsp_sync_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xda,0xdc,0xed,0xed,0xc0};
383 static int retries = 0;
385 if (retries < bcsp_max_retries) {
387 if (write(serial_fd, &bcsp_sync_pkt, 10) < 0)
393 tcflush(serial_fd, TCIOFLUSH);
394 fprintf(stderr, "BCSP initialization timed out\n");
398 static void bcsp_tconf_sig_alarm(int sig)
400 unsigned char bcsp_conf_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xad,0xef,0xac,0xed,0xc0};
401 static int retries = 0;
403 if (retries < bcsp_max_retries){
405 if (write(serial_fd, &bcsp_conf_pkt, 10) < 0)
411 tcflush(serial_fd, TCIOFLUSH);
412 fprintf(stderr, "BCSP initialization timed out\n");
416 static int bcsp(int fd, struct uart_t *u, struct termios *ti)
418 unsigned char byte, bcsph[4], bcspp[4],
419 bcsp_sync_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xac,0xaf,0xef,0xee,0xc0},
420 bcsp_conf_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xde,0xad,0xd0,0xd0,0xc0},
421 bcspsync[4] = {0xda, 0xdc, 0xed, 0xed},
422 bcspsyncresp[4] = {0xac,0xaf,0xef,0xee},
423 bcspconf[4] = {0xad,0xef,0xac,0xed},
424 bcspconfresp[4] = {0xde,0xad,0xd0,0xd0};
428 if (set_speed(fd, ti, u->speed) < 0) {
429 perror("Can't set default baud rate");
433 ti->c_cflag |= PARENB;
434 ti->c_cflag &= ~(PARODD);
436 if (tcsetattr(fd, TCSANOW, ti) < 0) {
437 perror("Can't set port settings");
444 memset(&sa, 0, sizeof(sa));
445 sa.sa_flags = SA_NOCLDSTOP;
446 sa.sa_handler = bcsp_tshy_sig_alarm;
447 sigaction(SIGALRM, &sa, NULL);
451 bcsp_tshy_sig_alarm(0);
454 if (read_check(fd, &byte, 1) == -1){
455 perror("Failed to read");
458 } while (byte != 0xC0);
461 if ( read_check(fd, &bcsph[0], 1) == -1){
462 perror("Failed to read");
465 } while (bcsph[0] == 0xC0);
467 if ( read_check(fd, &bcsph[1], 3) == -1){
468 perror("Failed to read");
472 if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
474 if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
477 if (read_check(fd, &bcspp, 4) == -1){
478 perror("Failed to read");
482 if (!memcmp(bcspp, bcspsync, 4)) {
483 if (write(fd, &bcsp_sync_resp_pkt,10) < 0)
485 } else if (!memcmp(bcspp, bcspsyncresp, 4))
489 /* State = curious */
492 sa.sa_handler = bcsp_tconf_sig_alarm;
493 sigaction(SIGALRM, &sa, NULL);
498 if (read_check(fd, &byte, 1) == -1){
499 perror("Failed to read");
502 } while (byte != 0xC0);
505 if (read_check(fd, &bcsph[0], 1) == -1){
506 perror("Failed to read");
509 } while (bcsph[0] == 0xC0);
511 if (read_check(fd, &bcsph[1], 3) == -1){
512 perror("Failed to read");
516 if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
519 if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
522 if (read_check(fd, &bcspp, 4) == -1){
523 perror("Failed to read");
527 if (!memcmp(bcspp, bcspsync, 4))
528 len = write(fd, &bcsp_sync_resp_pkt, 10);
529 else if (!memcmp(bcspp, bcspconf, 4))
530 len = write(fd, &bcsp_conf_resp_pkt, 10);
531 else if (!memcmp(bcspp, bcspconfresp, 4))
540 /* State = garrulous */
546 * CSR specific initialization
547 * Inspired strongly by code in OpenBT and experimentations with Brainboxes
549 * Jean Tourrilhes <jt@hpl.hp.com> - 14.11.01
551 static int csr(int fd, struct uart_t *u, struct termios *ti)
553 struct timespec tm = {0, 10000000}; /* 10ms - be generous */
554 unsigned char cmd[30]; /* Command */
555 unsigned char resp[30]; /* Response */
556 int clen = 0; /* Command len */
557 static int csr_seq = 0; /* Sequence number of command */
560 /* It seems that if we set the CSR UART speed straight away, it
561 * won't work, the CSR UART gets into a state where we can't talk
563 * On the other hand, doing a read before setting the CSR speed
565 * Therefore, the strategy is to read the build ID (useful for
566 * debugging) and only then set the CSR UART speed. Doing like
567 * this is more complex but at least it works ;-)
568 * The CSR UART control may be slow to wake up or something because
569 * every time I read its speed, its bogus...
572 /* Try to read the build ID of the CSR chip */
573 clen = 5 + (5 + 6) * 2;
575 cmd[0] = HCI_COMMAND_PKT;
576 cmd[1] = 0x00; /* CSR command */
577 cmd[2] = 0xfc; /* MANUFACTURER_SPEC */
578 cmd[3] = 1 + (5 + 6) * 2; /* len */
580 cmd[4] = 0xC2; /* first+last+channel=BCC */
582 cmd[5] = 0x00; /* type = GET-REQ */
583 cmd[6] = 0x00; /* - msB */
584 cmd[7] = 5 + 4; /* len */
585 cmd[8] = 0x00; /* - msB */
586 cmd[9] = csr_seq & 0xFF;/* seq num */
587 cmd[10] = (csr_seq >> 8) & 0xFF; /* - msB */
589 cmd[11] = 0x19; /* var_id = CSR_CMD_BUILD_ID */
590 cmd[12] = 0x28; /* - msB */
591 cmd[13] = 0x00; /* status = STATUS_OK */
592 cmd[14] = 0x00; /* - msB */
593 /* CSR BCC payload */
594 memset(cmd + 15, 0, 6 * 2);
598 if (write(fd, cmd, clen) != clen) {
599 perror("Failed to write init command (GET_BUILD_ID)");
604 if (read_hci_event(fd, resp, 100) < 0) {
605 perror("Failed to read init response (GET_BUILD_ID)");
609 /* Event code 0xFF is for vendor-specific events, which is
610 * what we're looking for. */
611 } while (resp[1] != 0xFF);
617 for (i=0; i < rlen; i++)
618 sprintf(temp + (i*3), "-%02X", resp[i]);
619 fprintf(stderr, "Reading CSR build ID %d [%s]\n", rlen, temp + 1);
620 // In theory, it should look like :
621 // 04-FF-13-FF-01-00-09-00-00-00-19-28-00-00-73-00-00-00-00-00-00-00
624 /* Display that to user */
625 fprintf(stderr, "CSR build ID 0x%02X-0x%02X\n",
626 resp[15] & 0xFF, resp[14] & 0xFF);
628 /* Try to read the current speed of the CSR chip */
629 clen = 5 + (5 + 4)*2;
631 cmd[3] = 1 + (5 + 4)*2; /* len */
632 /* -- CSR BCC header -- */
633 cmd[9] = csr_seq & 0xFF; /* seq num */
634 cmd[10] = (csr_seq >> 8) & 0xFF; /* - msB */
636 cmd[11] = 0x02; /* var_id = CONFIG_UART */
637 cmd[12] = 0x68; /* - msB */
642 if (write(fd, cmd, clen) != clen) {
643 perror("Failed to write init command (GET_BUILD_ID)");
648 if (read_hci_event(fd, resp, 100) < 0) {
649 perror("Failed to read init response (GET_BUILD_ID)");
653 /* Event code 0xFF is for vendor-specific events, which is
654 * what we're looking for. */
655 } while (resp[1] != 0xFF);
660 for (i=0; i < rlen; i++)
661 sprintf(temp + (i*3), "-%02X", resp[i]);
662 fprintf(stderr, "Reading CSR UART speed %d [%s]\n", rlen, temp+1);
666 if (u->speed > 1500000) {
667 fprintf(stderr, "Speed %d too high. Remaining at %d baud\n",
668 u->speed, u->init_speed);
669 u->speed = u->init_speed;
670 } else if (u->speed != 57600 && uart_speed(u->speed) == B57600) {
671 /* Unknown speed. Why oh why can't we just pass an int to the kernel? */
672 fprintf(stderr, "Speed %d unrecognised. Remaining at %d baud\n",
673 u->speed, u->init_speed);
674 u->speed = u->init_speed;
676 if (u->speed == u->init_speed)
679 /* Now, create the command that will set the UART speed */
681 cmd[5] = 0x02; /* type = SET-REQ */
682 cmd[6] = 0x00; /* - msB */
683 cmd[9] = csr_seq & 0xFF; /* seq num */
684 cmd[10] = (csr_seq >> 8) & 0xFF;/* - msB */
687 divisor = (u->speed*64+7812)/15625;
689 /* No parity, one stop bit -> divisor |= 0x0000; */
690 cmd[15] = (divisor) & 0xFF; /* divider */
691 cmd[16] = (divisor >> 8) & 0xFF; /* - msB */
692 /* The rest of the payload will be 0x00 */
698 for(i = 0; i < clen; i++)
699 sprintf(temp + (i*3), "-%02X", cmd[i]);
700 fprintf(stderr, "Writing CSR UART speed %d [%s]\n", clen, temp + 1);
701 // In theory, it should look like :
702 // 01-00-FC-13-C2-02-00-09-00-03-00-02-68-00-00-BF-0E-00-00-00-00-00-00
703 // 01-00-FC-13-C2-02-00-09-00-01-00-02-68-00-00-D8-01-00-00-00-00-00-00
707 /* Send the command to set the CSR UART speed */
708 if (write(fd, cmd, clen) != clen) {
709 perror("Failed to write init command (SET_UART_SPEED)");
713 nanosleep(&tm, NULL);
718 * Silicon Wave specific initialization
719 * Thomas Moser <thomas.moser@tmoser.ch>
721 static int swave(int fd, struct uart_t *u, struct termios *ti)
723 struct timespec tm = { 0, 500000 };
724 char cmd[10], rsp[100];
727 // Silicon Wave set baud rate command
728 // see HCI Vendor Specific Interface from Silicon Wave
729 // first send a "param access set" command to set the
730 // appropriate data fields in RAM. Then send a "HCI Reset
731 // Subcommand", e.g. "soft reset" to make the changes effective.
733 cmd[0] = HCI_COMMAND_PKT; // it's a command packet
734 cmd[1] = 0x0B; // OCF 0x0B = param access set
735 cmd[2] = 0xfc; // OGF bx111111 = vendor specific
736 cmd[3] = 0x06; // 6 bytes of data following
737 cmd[4] = 0x01; // param sub command
738 cmd[5] = 0x11; // tag 17 = 0x11 = HCI Transport Params
739 cmd[6] = 0x03; // length of the parameter following
740 cmd[7] = 0x01; // HCI Transport flow control enable
741 cmd[8] = 0x01; // HCI Transport Type = UART
762 /* Send initialization command */
763 if (write(fd, cmd, 10) != 10) {
764 perror("Failed to write init command");
768 // We should wait for a "GET Event" to confirm the success of
769 // the baud rate setting. Wait some time before reading. Better:
770 // read with timeout, parse data
771 // until correct answer, else error handling ... todo ...
773 nanosleep(&tm, NULL);
775 r = read(fd, rsp, sizeof(rsp));
777 // guess it's okay, but we should parse the reply. But since
778 // I don't react on an error anyway ... todo
779 // Response packet format:
781 // FF Vendor specific
782 // 07 Parameter length
785 // 11 Tag specifying HCI Transport Layer Parameter
788 // 01 Hci Transport type = Uart
789 // xx Baud rate set (see above)
795 // we probably got the reply. Now we must send the "soft reset"
796 // which is standard HCI RESET.
798 cmd[0] = HCI_COMMAND_PKT; // it's a command packet
803 /* Send reset command */
804 if (write(fd, cmd, 4) != 4) {
805 perror("Can't write Silicon Wave reset cmd.");
809 nanosleep(&tm, NULL);
811 // now the uart baud rate on the silicon wave module is set and effective.
812 // change our own baud rate as well. Then there is a reset event coming in
813 // on the *new* baud rate. This is *undocumented*! The packet looks like this:
814 // 04 FF 01 0B (which would make that a confirmation of 0x0B = "Param
815 // subcommand class". So: change to new baud rate, read with timeout, parse
816 // data, error handling. BTW: all param access in Silicon Wave is done this way.
817 // Maybe this code would belong in a separate file, or at least code reuse...
823 * ST Microelectronics specific initialization
824 * Marcel Holtmann <marcel@holtmann.org>
826 static int st(int fd, struct uart_t *u, struct termios *ti)
828 struct timespec tm = {0, 50000};
831 /* ST Microelectronics set baud rate command */
832 cmd[0] = HCI_COMMAND_PKT;
833 cmd[1] = 0x46; // OCF = Hci_Cmd_ST_Set_Uart_Baud_Rate
834 cmd[2] = 0xfc; // OGF = Vendor specific
868 /* Send initialization command */
869 if (write(fd, cmd, 5) != 5) {
870 perror("Failed to write init command");
874 nanosleep(&tm, NULL);
878 static int stlc2500(int fd, struct uart_t *u, struct termios *ti)
881 unsigned char resp[10];
885 /* STLC2500 has an ericsson core */
886 rvalue = ericsson(fd, u, ti);
890 #ifdef STLC2500_DEBUG
891 fprintf(stderr, "Setting speed\n");
893 if (set_speed(fd, ti, u->speed) < 0) {
894 perror("Can't set baud rate");
898 #ifdef STLC2500_DEBUG
899 fprintf(stderr, "Speed set...\n");
903 if ((n = read_hci_event(fd, resp, 10)) < 0) {
904 fprintf(stderr, "Failed to set baud rate on chip\n");
908 #ifdef STLC2500_DEBUG
909 for (i = 0; i < n; i++) {
910 fprintf(stderr, "resp[%d] = %02x\n", i, resp[i]);
914 str2ba(u->bdaddr, &bdaddr);
915 return stlc2500_init(fd, &bdaddr);
918 static int bgb2xx(int fd, struct uart_t *u, struct termios *ti)
922 str2ba(u->bdaddr, &bdaddr);
924 return bgb2xx_init(fd, &bdaddr);
928 * Broadcom specific initialization
929 * Extracted from Jungo openrg
931 static int bcm2035(int fd, struct uart_t *u, struct termios *ti)
934 unsigned char cmd[30], resp[30];
936 /* Reset the BT Chip */
937 memset(cmd, 0, sizeof(cmd));
938 memset(resp, 0, sizeof(resp));
939 cmd[0] = HCI_COMMAND_PKT;
945 if (write(fd, cmd, 4) != 4) {
946 fprintf(stderr, "Failed to write reset command\n");
951 if ((n = read_hci_event(fd, resp, 4)) < 0) {
952 fprintf(stderr, "Failed to reset chip\n");
956 if (u->bdaddr != NULL) {
958 memset(cmd, 0, sizeof(cmd));
959 memset(resp, 0, sizeof(resp));
960 cmd[0] = HCI_COMMAND_PKT;
964 str2ba(u->bdaddr, (bdaddr_t *) (cmd + 4));
967 if (write(fd, cmd, 10) != 10) {
968 fprintf(stderr, "Failed to write BD_ADDR command\n");
973 if ((n = read_hci_event(fd, resp, 10)) < 0) {
974 fprintf(stderr, "Failed to set BD_ADDR\n");
979 /* Read the local version info */
980 memset(cmd, 0, sizeof(cmd));
981 memset(resp, 0, sizeof(resp));
982 cmd[0] = HCI_COMMAND_PKT;
988 if (write(fd, cmd, 4) != 4) {
989 fprintf(stderr, "Failed to write \"read local version\" "
995 if ((n = read_hci_event(fd, resp, 4)) < 0) {
996 fprintf(stderr, "Failed to read local version\n");
1000 /* Read the local supported commands info */
1001 memset(cmd, 0, sizeof(cmd));
1002 memset(resp, 0, sizeof(resp));
1003 cmd[0] = HCI_COMMAND_PKT;
1009 if (write(fd, cmd, 4) != 4) {
1010 fprintf(stderr, "Failed to write \"read local supported "
1011 "commands\" command\n");
1016 if ((n = read_hci_event(fd, resp, 4)) < 0) {
1017 fprintf(stderr, "Failed to read local supported commands\n");
1021 /* Set the baud rate */
1022 memset(cmd, 0, sizeof(cmd));
1023 memset(resp, 0, sizeof(resp));
1025 /* __TIZEN_PATCH__ */
1026 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1027 cmd[0] = HCI_COMMAND_PKT;
1049 /* Default is 115200 */
1054 fprintf(stderr, "Baud rate parameters: DHBR=0x%2x,DLBR=0x%2x\n",
1058 if (write(fd, cmd, 6) != 6) {
1059 fprintf(stderr, "Failed to write \"set baud rate\" command\n");
1063 cmd[0] = HCI_COMMAND_PKT;
1081 cmd[6] = u->speed & 0xFF;
1082 cmd[7] = (u->speed >> 8) & 0xFF;
1083 cmd[8] = (u->speed >> 16) & 0xFF;
1084 cmd[9] = (u->speed >> 24) & 0xFF;
1086 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] );
1089 if (write(fd, cmd, 10) != 10) {
1090 fprintf(stderr, "Failed to write \"set baud rate\" command\n");
1096 if ((n = read_hci_event(fd, resp, 6)) < 0) {
1097 fprintf(stderr, "Failed to set baud rate\n");
1104 #ifdef __TIZEN_PATCH__ /*SPRD add Start*/
1105 static int init_sprd_config(int fd, struct uart_t *u, struct termios *ti)
1108 return sprd_config_init(fd, u->bdaddr, ti);
1112 struct uart_t uart[] = {
1113 { "any", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1114 FLOW_CTL, DISABLE_PM, NULL, NULL },
1115 #ifdef __TIZEN_PATCH__ /*SPRD*/
1116 { "sprd", 0x0000, 0x0000, HCI_UART_H4, 3000000, 3000000,
1117 FLOW_CTL, DISABLE_PM, NULL, init_sprd_config },
1119 { "ericsson", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200,
1120 FLOW_CTL, DISABLE_PM, NULL, ericsson },
1122 { "digi", 0x0000, 0x0000, HCI_UART_H4, 9600, 115200,
1123 FLOW_CTL, DISABLE_PM, NULL, digi },
1125 { "bcsp", 0x0000, 0x0000, HCI_UART_BCSP, 115200, 115200,
1126 0, DISABLE_PM, NULL, bcsp },
1128 /* Xircom PCMCIA cards: Credit Card Adapter and Real Port Adapter */
1129 { "xircom", 0x0105, 0x080a, HCI_UART_H4, 115200, 115200,
1130 FLOW_CTL, DISABLE_PM, NULL, NULL },
1132 /* CSR Casira serial adapter or BrainBoxes serial dongle (BL642) */
1133 { "csr", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1134 FLOW_CTL, DISABLE_PM, NULL, csr },
1136 /* BrainBoxes PCMCIA card (BL620) */
1137 { "bboxes", 0x0160, 0x0002, HCI_UART_H4, 115200, 460800,
1138 FLOW_CTL, DISABLE_PM, NULL, csr },
1140 /* Silicon Wave kits */
1141 { "swave", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1142 FLOW_CTL, DISABLE_PM, NULL, swave },
1144 /* __TIZEN_PATCH__ */
1145 #if defined __TIZEN_PATCH__ && defined __TI_PATCH__
1146 /* Texas Instruments BRF63xx modules */
1147 { "texas", 0x0000, 0x0000, HCI_UART_LL, 115200,3000000, FLOW_CTL, NULL, texas, NULL/*texas_continue_script*/, BRF_DEEP_SLEEP_OPCODE},
1149 /* Texas Instruments Bluelink (BRF) modules */
1150 { "texas", 0x0000, 0x0000, HCI_UART_LL, 115200, 115200,
1151 FLOW_CTL, DISABLE_PM, NULL, texas, texas2 },
1153 { "texasalt", 0x0000, 0x0000, HCI_UART_LL, 115200, 115200,
1154 FLOW_CTL, DISABLE_PM, NULL, texasalt, NULL },
1157 /* ST Microelectronics minikits based on STLC2410/STLC2415 */
1158 { "st", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200,
1159 FLOW_CTL, DISABLE_PM, NULL, st },
1161 /* ST Microelectronics minikits based on STLC2500 */
1162 { "stlc2500", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1163 FLOW_CTL, DISABLE_PM, "00:80:E1:00:AB:BA", stlc2500 },
1165 /* Philips generic Ericsson IP core based */
1166 { "philips", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1167 FLOW_CTL, DISABLE_PM, NULL, NULL },
1169 /* Philips BGB2xx Module */
1170 { "bgb2xx", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1171 FLOW_CTL, DISABLE_PM, "BD:B2:10:00:AB:BA", bgb2xx },
1173 /* Sphinx Electronics PICO Card */
1174 { "picocard", 0x025e, 0x1000, HCI_UART_H4, 115200, 115200,
1175 FLOW_CTL, DISABLE_PM, NULL, NULL },
1177 /* Inventel BlueBird Module */
1178 { "inventel", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1179 FLOW_CTL, DISABLE_PM, NULL, NULL },
1181 /* COM One Platinium Bluetooth PC Card */
1182 { "comone", 0xffff, 0x0101, HCI_UART_BCSP, 115200, 115200,
1183 0, DISABLE_PM, NULL, bcsp },
1185 /* TDK Bluetooth PC Card and IBM Bluetooth PC Card II */
1186 { "tdk", 0x0105, 0x4254, HCI_UART_BCSP, 115200, 115200,
1187 0, DISABLE_PM, NULL, bcsp },
1189 /* Socket Bluetooth CF Card (Rev G) */
1190 { "socket", 0x0104, 0x0096, HCI_UART_BCSP, 230400, 230400,
1191 0, DISABLE_PM, NULL, bcsp },
1193 /* 3Com Bluetooth Card (Version 3.0) */
1194 { "3com", 0x0101, 0x0041, HCI_UART_H4, 115200, 115200,
1195 FLOW_CTL, DISABLE_PM, NULL, csr },
1197 /* AmbiCom BT2000C Bluetooth PC/CF Card */
1198 { "bt2000c", 0x022d, 0x2000, HCI_UART_H4, 57600, 460800,
1199 FLOW_CTL, DISABLE_PM, NULL, csr },
1201 /* Zoom Bluetooth PCMCIA Card */
1202 { "zoom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1203 0, DISABLE_PM, NULL, bcsp },
1205 /* Sitecom CN-504 PCMCIA Card */
1206 { "sitecom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1207 0, DISABLE_PM, NULL, bcsp },
1209 /* Billionton PCBTC1 PCMCIA Card */
1210 { "billionton", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1211 0, DISABLE_PM, NULL, bcsp },
1213 /* Broadcom BCM2035 */
1214 { "bcm2035", 0x0A5C, 0x2035, HCI_UART_H4, 115200, 460800,
1215 FLOW_CTL, DISABLE_PM, NULL, bcm2035 },
1217 /* Broadcom BCM43XX */
1218 { "bcm43xx", 0x0000, 0x0000, HCI_UART_H4, 115200, 3000000,
1219 FLOW_CTL, DISABLE_PM, NULL, bcm43xx, NULL },
1221 { "ath3k", 0x0000, 0x0000, HCI_UART_ATH3K, 115200, 115200,
1222 FLOW_CTL, DISABLE_PM, NULL, ath3k_ps, ath3k_pm },
1225 { "qualcomm", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1226 FLOW_CTL, DISABLE_PM, NULL, qualcomm, NULL },
1228 /* Intel Bluetooth Module */
1229 { "intel", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1230 FLOW_CTL, DISABLE_PM, NULL, intel, NULL },
1232 /* Three-wire UART */
1233 { "3wire", 0x0000, 0x0000, HCI_UART_3WIRE, 115200, 115200,
1234 0, DISABLE_PM, NULL, NULL, NULL },
1236 /* AMP controller UART */
1237 { "amp", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1238 AMP_DEV, DISABLE_PM, NULL, NULL, NULL },
1243 static struct uart_t * get_by_id(int m_id, int p_id)
1246 for (i = 0; uart[i].type; i++) {
1247 if (uart[i].m_id == m_id && uart[i].p_id == p_id)
1253 static struct uart_t * get_by_type(char *type)
1256 for (i = 0; uart[i].type; i++) {
1257 if (!strcmp(uart[i].type, type))
1263 #if defined __TIZEN_PATCH__ && defined __BROADCOM_PATCH__
1264 static int enable_hci(char *dev, struct uart_t *u)
1267 unsigned long flags = 0;
1269 fd = open(dev, O_RDWR | O_NOCTTY);
1271 fprintf(stderr, "Can't open serial port");
1275 tcflush(fd, TCIOFLUSH);
1277 /* Set TTY to N_HCI line discipline */
1279 if (ioctl(fd, TIOCSETD, &i) < 0) {
1280 fprintf(stderr, "Can't set line discipline");
1285 if (flags && ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
1286 fprintf(stderr, "Can't set UART flags");
1291 tcflush(fd, TCIOFLUSH);
1293 if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
1294 fprintf(stderr, "Can't set device");
1302 #else /* __BROADCOM_PATCH__ */
1304 /* Initialize UART driver */
1305 static int init_uart(char *dev, struct uart_t *u, int send_break, int raw)
1309 unsigned long flags = 0;
1311 /* __TIZEN_PATCH__ */
1312 #ifdef __TIZEN_PATCH__
1313 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1319 flags |= 1 << HCI_UART_RAW_DEVICE;
1321 if (u->flags & AMP_DEV)
1322 flags |= 1 << HCI_UART_CREATE_AMP;
1324 fd = open(dev, O_RDWR | O_NOCTTY);
1326 perror("Can't open serial port");
1330 tcflush(fd, TCIOFLUSH);
1332 if (tcgetattr(fd, &ti) < 0) {
1333 perror("Can't get port settings");
1339 /* __TIZEN_PATCH__ */
1340 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1341 ti.c_cflag |= CLOCAL;
1342 if (u->flags & FLOW_CTL)
1343 ti.c_cflag |= CRTSCTS;
1345 ti.c_cflag &= ~CRTSCTS;
1348 if (tcsetattr(fd, TCSANOW, &ti) < 0) {
1349 perror("Can't set port settings");
1353 /* Set initial baudrate */
1354 if (set_speed(fd, &ti, u->init_speed) < 0) {
1355 perror("Can't set initial baud rate");
1359 tcflush(fd, TCIOFLUSH);
1366 /* __TIZEN_PATCH__ */
1367 #ifdef __TIZEN_PATCH__
1368 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1369 /* Power up the BRF chip */
1371 ioctl(fd, TIOSETBRFPOWER, &power);
1378 if (u->init && u->init(fd, u, &ti) < 0)
1381 tcflush(fd, TCIOFLUSH);
1383 /* Set actual baudrate */
1384 if (set_speed(fd, &ti, u->speed) < 0) {
1385 perror("Can't set baud rate");
1389 /* Set TTY to N_HCI line discipline */
1391 if (ioctl(fd, TIOCSETD, &i) < 0) {
1392 perror("Can't set line discipline");
1396 if (flags && ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
1397 perror("Can't set UART flags");
1401 if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
1402 perror("Can't set device");
1406 #ifndef __TIZEN_PATCH__
1407 if (u->post && u->post(fd, u, &ti) < 0)
1418 #endif /* __BROADCOM_PATCH__ */
1420 static void usage(void)
1422 printf("hciattach - HCI UART driver initialization utility\n");
1425 /* __TIZEN_PATCH__ */
1426 #if defined(__TIZEN_PATCH__) && !defined(__TI_PATCH__)
\r
1427 /* This commented code was present before bluez 5.25 upgrade
1428 * printf("\thciattach [-n] [-p] [-b] [-g device_param] [-r] [-f] [-t timeout] [-s initial_speed] <tty> <type | id> [speed] [flow|noflow] [bdaddr]\n");*/
1429 printf("\thciattach [-n] [-p] [-b] [-g device_param] [-r] [-f]"
1430 " [-t timeout] [-s initial_speed]"
1431 " <tty> <type | id> [speed] [flow|noflow]"
1432 " [sleep|nosleep] [bdaddr]\n");
1434 printf("\thciattach [-n] [-p] [-b] [-r] [-t timeout] [-s initial_speed]"
1435 " <tty> <type | id> [speed] [flow|noflow]"
1436 " [sleep|nosleep] [bdaddr]\n");
1438 printf("\thciattach -l\n");
1441 int main(int argc, char *argv[])
1443 struct uart_t *u = NULL;
1444 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1445 int detach, printpid, raw, opt, i, n, ld, err;
1447 int detach, printpid, opt, i, n, ld, err;
1451 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1455 struct sigaction sa;
1460 #ifdef __TIZEN_PATCH__
1461 /* __TIZEN_PATCH__ */
1462 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1467 uint16_t device_param = 0;
1468 int reset_device = 0;
1473 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1476 #if defined(__TIZEN_PATCH__) && !defined(__TI_PATCH__)
1477 while ((opt=getopt(argc, argv, "bnprft:g:s:l")) != EOF) {
1479 while ((opt=getopt(argc, argv, "bnpt:s:lr")) != EOF) {
1483 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1500 /* __TIZEN_PATCH__ */
1501 #if defined(__TIZEN_PATCH__) && defined(__TI_PATCH__)
1503 device_param = (uint16_t)strtol(optarg, NULL, 16);
1515 init_speed = atoi(optarg);
1519 for (i = 0; uart[i].type; i++) {
1520 printf("%-10s0x%04x,0x%04x\n", uart[i].type,
1521 uart[i].m_id, uart[i].p_id);
1526 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1538 /* __TIZEN_PATCH__ */
1539 #if defined(__TIZEN_PATCH__) && defined(__TI_PATCH__)
1540 if (!reset_device || (reset_device && n < 1))
1547 for (n = 0; optind < argc; n++, optind++) {
1555 if (!strchr(opt, '/'))
1556 strcpy(dev, "/dev/");
1558 if (strlen(opt) > PATH_MAX - (strlen(dev) + 1)) {
1559 fprintf(stderr, "Invalid serial device\n");
1567 if (strchr(argv[optind], ',')) {
1569 sscanf(argv[optind], "%x,%x", &m_id, &p_id);
1570 u = get_by_id(m_id, p_id);
1572 u = get_by_type(opt);
1576 fprintf(stderr, "Unknown device type or id\n");
1583 u->speed = atoi(argv[optind]);
1587 if (!strcmp("flow", argv[optind]))
1588 u->flags |= FLOW_CTL;
1590 u->flags &= ~FLOW_CTL;
1594 if (!strcmp("sleep", argv[optind]))
1601 u->bdaddr = argv[optind];
1605 /* __TIZEN_PATCH__ */
1606 #if defined(__TIZEN_PATCH__) && defined(__TI_PATCH__)
1610 bt_fd = open(dev, O_RDWR | O_NOCTTY);
1612 perror("Can't open serial port");
1615 /* Power up the BRF chip */
1617 ioctl(bt_fd, TIOSETBRFPOWER, &power);
1623 fprintf(stderr, "Unknown device type or id\n");
1627 #ifdef __TIZEN_PATCH__
1628 // __hci_attach_log_init();
1631 /* If user specified a initial speed, use that instead of
1632 the hardware's default */
1634 u->init_speed = init_speed;
1635 #if defined(__TIZEN_PATCH__) && defined(__TI_PATCH__)
1636 /* If user specified a device parameter, use that instead of
1637 the hardware's default */
1639 u->device_param = device_param;
1642 memset(&sa, 0, sizeof(sa));
1643 sa.sa_flags = SA_NOCLDSTOP;
1644 sa.sa_handler = sig_alarm;
1645 sigaction(SIGALRM, &sa, NULL);
1647 /* 10 seconds should be enough for initialization */
1649 bcsp_max_retries = to;
1650 #if defined __TIZEN_PATCH__ && defined __BROADCOM_PATCH__
1651 n = enable_hci(dev, u);
1653 n = init_uart(dev, u, send_break, raw);
1656 perror("Can't initialize device");
1660 printf("Device setup complete\n");
1664 memset(&sa, 0, sizeof(sa));
1665 sa.sa_flags = SA_NOCLDSTOP;
1666 sa.sa_handler = SIG_IGN;
1667 sigaction(SIGCHLD, &sa, NULL);
1668 sigaction(SIGPIPE, &sa, NULL);
1670 sa.sa_handler = sig_term;
1671 sigaction(SIGTERM, &sa, NULL);
1672 sigaction(SIGINT, &sa, NULL);
1674 sa.sa_handler = sig_hup;
1675 sigaction(SIGHUP, &sa, NULL);
1678 if ((pid = fork())) {
1680 printf("%d\n", pid);
1684 for (i = 0; i < 20; i++)
1690 p.events = POLLERR | POLLHUP;
1693 sigdelset(&sigs, SIGCHLD);
1694 sigdelset(&sigs, SIGPIPE);
1695 sigdelset(&sigs, SIGTERM);
1696 sigdelset(&sigs, SIGINT);
1697 sigdelset(&sigs, SIGHUP);
1699 while (!__io_canceled) {
1701 err = ppoll(&p, 1, NULL, &sigs);
1702 if (err < 0 && errno == EINTR)
1708 /* Restore TTY line discipline */
1710 if (ioctl(n, TIOCSETD, &ld) < 0) {
1711 perror("Can't restore line discipline");
1715 /* __TIZEN_PATCH__ */
1716 #ifdef __TIZEN_PATCH__
1717 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1718 /* Power down the BRF or BCMchip */
1720 ioctl(n, TIOSETBRFPOWER, &power);