Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / tools / hciattach.c
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 #ifndef __TIZEN_PATCH__
31 #define _GNU_SOURCE
32 #endif
33 #include <stdio.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <signal.h>
40 #include <syslog.h>
41 #include <termios.h>
42 #include <time.h>
43 #include <poll.h>
44 #include <sys/time.h>
45 #include <sys/param.h>
46 #include <sys/ioctl.h>
47
48 #include "lib/bluetooth.h"
49 #include "lib/hci.h"
50 #include "lib/hci_lib.h"
51
52 #include "hciattach.h"
53
54 struct uart_t {
55         char *type;
56         int  m_id;
57         int  p_id;
58         int  proto;
59         int  init_speed;
60         int  speed;
61         int  flags;
62         int  pm;
63         char *bdaddr;
64         int  (*init) (int fd, struct uart_t *u, struct termios *ti);
65         int  (*post) (int fd, struct uart_t *u, struct termios *ti);
66
67 /* __TIZEN_PATCH__ */
68 #if defined __TIZEN_PATCH__ && defined __TI_PATCH__
69         uint16_t device_param;
70 #endif
71 };
72
73 #if defined __TIZEN_PATCH__ && defined __TI_PATCH__
74         int firmware_path = 0;
75 #endif
76
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))
84 #endif
85 #endif
86 #define FLOW_CTL        0x0001
87 #define AMP_DEV         0x0002
88 #define ENABLE_PM       1
89 #define DISABLE_PM      0
90
91 static volatile sig_atomic_t __io_canceled = 0;
92
93 static void sig_hup(int sig)
94 {
95 }
96
97 static void sig_term(int sig)
98 {
99         __io_canceled = 1;
100 }
101
102 static void sig_alarm(int sig)
103 {
104         fprintf(stderr, "Initialization timed out.\n");
105         exit(1);
106 }
107
108 int uart_speed(int s)
109 {
110         switch (s) {
111         case 9600:
112                 return B9600;
113         case 19200:
114                 return B19200;
115         case 38400:
116                 return B38400;
117         case 57600:
118                 return B57600;
119         case 115200:
120                 return B115200;
121         case 230400:
122                 return B230400;
123         case 460800:
124                 return B460800;
125         case 500000:
126                 return B500000;
127         case 576000:
128                 return B576000;
129         case 921600:
130                 return B921600;
131         case 1000000:
132                 return B1000000;
133         case 1152000:
134                 return B1152000;
135         case 1500000:
136                 return B1500000;
137         case 2000000:
138                 return B2000000;
139 #ifdef B2500000
140         case 2500000:
141                 return B2500000;
142 #endif
143 #ifdef B3000000
144         case 3000000:
145                 return B3000000;
146 #endif
147 #ifdef B3500000
148         case 3500000:
149                 return B3500000;
150 #endif
151 #ifdef B3710000
152         case 3710000:
153                 return B3710000;
154 #endif
155 #ifdef B4000000
156         case 4000000:
157                 return B4000000;
158 #endif
159         default:
160                 return B57600;
161         }
162 }
163
164 int set_speed(int fd, struct termios *ti, int speed)
165 {
166         if (cfsetospeed(ti, uart_speed(speed)) < 0)
167                 return -errno;
168
169         if (cfsetispeed(ti, uart_speed(speed)) < 0)
170                 return -errno;
171
172         if (tcsetattr(fd, TCSANOW, ti) < 0)
173                 return -errno;
174
175         return 0;
176 }
177
178 /*
179  * Read an HCI event from the given file descriptor.
180  */
181 int read_hci_event(int fd, unsigned char* buf, int size)
182 {
183         int remain, r;
184         int count = 0;
185
186         if (size <= 0)
187                 return -1;
188
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. */
191         while (1) {
192                 r = read(fd, buf, 1);
193                 if (r <= 0)
194                         return -1;
195                 if (buf[0] == 0x04)
196                         break;
197         }
198         count++;
199
200         /* The next two bytes are the event code and parameter total length. */
201         while (count < 3) {
202                 r = read(fd, buf + count, 3 - count);
203                 if (r <= 0)
204                         return -1;
205                 count += r;
206         }
207
208         /* Now we read the parameters. */
209         if (buf[2] < (size - 3))
210                 remain = buf[2];
211         else
212                 remain = size - 3;
213
214         while ((count - 3) < remain) {
215                 r = read(fd, buf + count, remain - (count - 3));
216                 if (r <= 0)
217                         return -1;
218                 count += r;
219         }
220
221         return count;
222 }
223
224 /*
225  * Ericsson specific initialization
226  */
227 static int ericsson(int fd, struct uart_t *u, struct termios *ti)
228 {
229         struct timespec tm = {0, 50000};
230         char cmd[5];
231
232         cmd[0] = HCI_COMMAND_PKT;
233         cmd[1] = 0x09;
234         cmd[2] = 0xfc;
235         cmd[3] = 0x01;
236
237         switch (u->speed) {
238         case 57600:
239                 cmd[4] = 0x03;
240                 break;
241         case 115200:
242                 cmd[4] = 0x02;
243                 break;
244         case 230400:
245                 cmd[4] = 0x01;
246                 break;
247         case 460800:
248                 cmd[4] = 0x00;
249                 break;
250         case 921600:
251                 cmd[4] = 0x20;
252                 break;
253         case 2000000:
254                 cmd[4] = 0x25;
255                 break;
256         case 3000000:
257                 cmd[4] = 0x27;
258                 break;
259         case 4000000:
260                 cmd[4] = 0x2B;
261                 break;
262         default:
263                 cmd[4] = 0x03;
264                 u->speed = 57600;
265                 fprintf(stderr, "Invalid speed requested, using %d bps instead\n", u->speed);
266                 break;
267         }
268
269         /* Send initialization command */
270         if (write(fd, cmd, 5) != 5) {
271                 perror("Failed to write init command");
272                 return -1;
273         }
274
275         nanosleep(&tm, NULL);
276         return 0;
277 }
278
279 /*
280  * Digianswer specific initialization
281  */
282 static int digi(int fd, struct uart_t *u, struct termios *ti)
283 {
284         struct timespec tm = {0, 50000};
285         char cmd[5];
286
287         /* DigiAnswer set baud rate command */
288         cmd[0] = HCI_COMMAND_PKT;
289         cmd[1] = 0x07;
290         cmd[2] = 0xfc;
291         cmd[3] = 0x01;
292
293         switch (u->speed) {
294         case 57600:
295                 cmd[4] = 0x08;
296                 break;
297         case 115200:
298                 cmd[4] = 0x09;
299                 break;
300         default:
301                 cmd[4] = 0x09;
302                 u->speed = 115200;
303                 break;
304         }
305
306         /* Send initialization command */
307         if (write(fd, cmd, 5) != 5) {
308                 perror("Failed to write init command");
309                 return -1;
310         }
311
312         nanosleep(&tm, NULL);
313         return 0;
314 }
315
316 static int texas(int fd, struct uart_t *u, struct termios *ti)
317 {
318         return texas_init(fd, &u->speed, ti);
319 }
320
321 static int texas2(int fd, struct uart_t *u, struct termios *ti)
322 {
323         return texas_post(fd, ti);
324 }
325
326 static int texasalt(int fd, struct uart_t *u, struct termios *ti)
327 {
328         return texasalt_init(fd, u->speed, ti);
329 }
330
331 static int ath3k_ps(int fd, struct uart_t *u, struct termios *ti)
332 {
333         return ath3k_init(fd, u->speed, u->init_speed, u->bdaddr, ti);
334 }
335
336 static int ath3k_pm(int fd, struct uart_t *u, struct termios *ti)
337 {
338         return ath3k_post(fd, u->pm);
339 }
340
341 static int qualcomm(int fd, struct uart_t *u, struct termios *ti)
342 {
343         return qualcomm_init(fd, u->speed, ti, u->bdaddr);
344 }
345
346 static int intel(int fd, struct uart_t *u, struct termios *ti)
347 {
348         return intel_init(fd, u->init_speed, &u->speed, ti);
349 }
350
351 static int bcm43xx(int fd, struct uart_t *u, struct termios *ti)
352 {
353         return bcm43xx_init(fd, u->init_speed, u->speed, ti, u->bdaddr);
354 }
355
356 static int read_check(int fd, void *buf, int count)
357 {
358         int res;
359
360         do {
361                 res = read(fd, buf, count);
362                 if (res != -1) {
363                         buf += res;
364                         count -= res;
365                 }
366         } while (count && (errno == 0 || errno == EINTR));
367
368         if (count)
369                 return -1;
370
371         return 0;
372 }
373
374 /*
375  * BCSP specific initialization
376  */
377 static int serial_fd;
378 static int bcsp_max_retries = 10;
379
380 static void bcsp_tshy_sig_alarm(int sig)
381 {
382         unsigned char bcsp_sync_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xda,0xdc,0xed,0xed,0xc0};
383         static int retries = 0;
384
385         if (retries < bcsp_max_retries) {
386                 retries++;
387                 if (write(serial_fd, &bcsp_sync_pkt, 10) < 0)
388                         return;
389                 alarm(1);
390                 return;
391         }
392
393         tcflush(serial_fd, TCIOFLUSH);
394         fprintf(stderr, "BCSP initialization timed out\n");
395         exit(1);
396 }
397
398 static void bcsp_tconf_sig_alarm(int sig)
399 {
400         unsigned char bcsp_conf_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xad,0xef,0xac,0xed,0xc0};
401         static int retries = 0;
402
403         if (retries < bcsp_max_retries){
404                 retries++;
405                 if (write(serial_fd, &bcsp_conf_pkt, 10) < 0)
406                         return;
407                 alarm(1);
408                 return;
409         }
410
411         tcflush(serial_fd, TCIOFLUSH);
412         fprintf(stderr, "BCSP initialization timed out\n");
413         exit(1);
414 }
415
416 static int bcsp(int fd, struct uart_t *u, struct termios *ti)
417 {
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};
425         struct sigaction sa;
426         int len;
427
428         if (set_speed(fd, ti, u->speed) < 0) {
429                 perror("Can't set default baud rate");
430                 return -1;
431         }
432
433         ti->c_cflag |= PARENB;
434         ti->c_cflag &= ~(PARODD);
435
436         if (tcsetattr(fd, TCSANOW, ti) < 0) {
437                 perror("Can't set port settings");
438                 return -1;
439         }
440
441         alarm(0);
442
443         serial_fd = fd;
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);
448
449         /* State = shy */
450
451         bcsp_tshy_sig_alarm(0);
452         while (1) {
453                 do {
454                         if (read_check(fd, &byte, 1) == -1){
455                                 perror("Failed to read");
456                                 return -1;
457                         }
458                 } while (byte != 0xC0);
459
460                 do {
461                         if ( read_check(fd, &bcsph[0], 1) == -1){
462                                 perror("Failed to read");
463                                 return -1;
464                         }
465                 } while (bcsph[0] == 0xC0);
466
467                 if ( read_check(fd, &bcsph[1], 3) == -1){
468                         perror("Failed to read");
469                         return -1;
470                 }
471
472                 if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
473                         continue;
474                 if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
475                         continue;
476
477                 if (read_check(fd, &bcspp, 4) == -1){
478                         perror("Failed to read");
479                         return -1;
480                 }
481
482                 if (!memcmp(bcspp, bcspsync, 4)) {
483                         if (write(fd, &bcsp_sync_resp_pkt,10) < 0)
484                                 return -1;
485                 } else if (!memcmp(bcspp, bcspsyncresp, 4))
486                         break;
487         }
488
489         /* State = curious */
490
491         alarm(0);
492         sa.sa_handler = bcsp_tconf_sig_alarm;
493         sigaction(SIGALRM, &sa, NULL);
494         alarm(1);
495
496         while (1) {
497                 do {
498                         if (read_check(fd, &byte, 1) == -1){
499                                 perror("Failed to read");
500                                 return -1;
501                         }
502                 } while (byte != 0xC0);
503
504                 do {
505                         if (read_check(fd, &bcsph[0], 1) == -1){
506                                 perror("Failed to read");
507                                 return -1;
508                         }
509                 } while (bcsph[0] == 0xC0);
510
511                 if (read_check(fd, &bcsph[1], 3) == -1){
512                         perror("Failed to read");
513                         return -1;
514                 }
515
516                 if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
517                         continue;
518
519                 if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
520                         continue;
521
522                 if (read_check(fd, &bcspp, 4) == -1){
523                         perror("Failed to read");
524                         return -1;
525                 }
526
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))
532                         break;
533                 else
534                         continue;
535
536                 if (len < 0)
537                         return -errno;
538         }
539
540         /* State = garrulous */
541
542         return 0;
543 }
544
545 /*
546  * CSR specific initialization
547  * Inspired strongly by code in OpenBT and experimentations with Brainboxes
548  * Pcmcia card.
549  * Jean Tourrilhes <jt@hpl.hp.com> - 14.11.01
550  */
551 static int csr(int fd, struct uart_t *u, struct termios *ti)
552 {
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 */
558         int  divisor;
559
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
562          * to it anymore.
563          * On the other hand, doing a read before setting the CSR speed
564          * seems to be ok.
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...
570          * Jean II */
571
572         /* Try to read the build ID of the CSR chip */
573         clen = 5 + (5 + 6) * 2;
574         /* HCI header */
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 */
579         /* CSR MSG header */
580         cmd[4] = 0xC2;          /* first+last+channel=BCC */
581         /* CSR BCC header */
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 */
588         csr_seq++;
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);
595
596         /* Send command */
597         do {
598                 if (write(fd, cmd, clen) != clen) {
599                         perror("Failed to write init command (GET_BUILD_ID)");
600                         return -1;
601                 }
602
603                 /* Read reply. */
604                 if (read_hci_event(fd, resp, 100) < 0) {
605                         perror("Failed to read init response (GET_BUILD_ID)");
606                         return -1;
607                 }
608
609         /* Event code 0xFF is for vendor-specific events, which is
610          * what we're looking for. */
611         } while (resp[1] != 0xFF);
612
613 #ifdef CSR_DEBUG
614         {
615         char temp[512];
616         int i;
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
622         }
623 #endif
624         /* Display that to user */
625         fprintf(stderr, "CSR build ID 0x%02X-0x%02X\n",
626                 resp[15] & 0xFF, resp[14] & 0xFF);
627
628         /* Try to read the current speed of the CSR chip */
629         clen = 5 + (5 + 4)*2;
630         /* -- HCI header */
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 */
635         csr_seq++;
636         cmd[11] = 0x02;         /* var_id = CONFIG_UART */
637         cmd[12] = 0x68;         /* - msB */
638
639 #ifdef CSR_DEBUG
640         /* Send command */
641         do {
642                 if (write(fd, cmd, clen) != clen) {
643                         perror("Failed to write init command (GET_BUILD_ID)");
644                         return -1;
645                 }
646
647                 /* Read reply. */
648                 if (read_hci_event(fd, resp, 100) < 0) {
649                         perror("Failed to read init response (GET_BUILD_ID)");
650                         return -1;
651                 }
652
653         /* Event code 0xFF is for vendor-specific events, which is
654          * what we're looking for. */
655         } while (resp[1] != 0xFF);
656
657         {
658         char temp[512];
659         int i;
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);
663         }
664 #endif
665
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;
675         }
676         if (u->speed == u->init_speed)
677                 return 0;
678
679         /* Now, create the command that will set the UART speed */
680         /* CSR BCC header */
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 */
685         csr_seq++;
686
687         divisor = (u->speed*64+7812)/15625;
688
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 */
693
694 #ifdef CSR_DEBUG
695         {
696         char temp[512];
697         int i;
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
704         }
705 #endif
706
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)");
710                 return -1;
711         }
712
713         nanosleep(&tm, NULL);
714         return 0;
715 }
716
717 /*
718  * Silicon Wave specific initialization
719  * Thomas Moser <thomas.moser@tmoser.ch>
720  */
721 static int swave(int fd, struct uart_t *u, struct termios *ti)
722 {
723         struct timespec tm = { 0, 500000 };
724         char cmd[10], rsp[100];
725         int r;
726
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.
732
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
742
743         switch (u->speed) {
744         case 19200:
745                 cmd[9] = 0x03;
746                 break;
747         case 38400:
748                 cmd[9] = 0x02;
749                 break;
750         case 57600:
751                 cmd[9] = 0x01;
752                 break;
753         case 115200:
754                 cmd[9] = 0x00;
755                 break;
756         default:
757                 u->speed = 115200;
758                 cmd[9] = 0x00;
759                 break;
760         }
761
762         /* Send initialization command */
763         if (write(fd, cmd, 10) != 10) {
764                 perror("Failed to write init command");
765                 return -1;
766         }
767
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 ...
772
773         nanosleep(&tm, NULL);
774
775         r = read(fd, rsp, sizeof(rsp));
776         if (r > 0) {
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:
780                 //  04  Event
781                 //  FF  Vendor specific
782                 //  07  Parameter length
783                 //  0B  Subcommand
784                 //  01  Setevent
785                 //  11  Tag specifying HCI Transport Layer Parameter
786                 //  03  length
787                 //  01  flow on
788                 //  01  Hci Transport type = Uart
789                 //  xx  Baud rate set (see above)
790         } else {
791                 // ups, got error.
792                 return -1;
793         }
794
795         // we probably got the reply. Now we must send the "soft reset"
796         // which is standard HCI RESET.
797
798         cmd[0] = HCI_COMMAND_PKT;       // it's a command packet
799         cmd[1] = 0x03;
800         cmd[2] = 0x0c;
801         cmd[3] = 0x00;
802
803         /* Send reset command */
804         if (write(fd, cmd, 4) != 4) {
805                 perror("Can't write Silicon Wave reset cmd.");
806                 return -1;
807         }
808
809         nanosleep(&tm, NULL);
810
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...
818
819         return 0;
820 }
821
822 /*
823  * ST Microelectronics specific initialization
824  * Marcel Holtmann <marcel@holtmann.org>
825  */
826 static int st(int fd, struct uart_t *u, struct termios *ti)
827 {
828         struct timespec tm = {0, 50000};
829         char cmd[5];
830
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
835         cmd[3] = 0x01;
836
837         switch (u->speed) {
838         case 9600:
839                 cmd[4] = 0x09;
840                 break;
841         case 19200:
842                 cmd[4] = 0x0b;
843                 break;
844         case 38400:
845                 cmd[4] = 0x0d;
846                 break;
847         case 57600:
848                 cmd[4] = 0x0e;
849                 break;
850         case 115200:
851                 cmd[4] = 0x10;
852                 break;
853         case 230400:
854                 cmd[4] = 0x12;
855                 break;
856         case 460800:
857                 cmd[4] = 0x13;
858                 break;
859         case 921600:
860                 cmd[4] = 0x14;
861                 break;
862         default:
863                 cmd[4] = 0x10;
864                 u->speed = 115200;
865                 break;
866         }
867
868         /* Send initialization command */
869         if (write(fd, cmd, 5) != 5) {
870                 perror("Failed to write init command");
871                 return -1;
872         }
873
874         nanosleep(&tm, NULL);
875         return 0;
876 }
877
878 static int stlc2500(int fd, struct uart_t *u, struct termios *ti)
879 {
880         bdaddr_t bdaddr;
881         unsigned char resp[10];
882         int n;
883         int rvalue;
884
885         /* STLC2500 has an ericsson core */
886         rvalue = ericsson(fd, u, ti);
887         if (rvalue != 0)
888                 return rvalue;
889
890 #ifdef STLC2500_DEBUG
891         fprintf(stderr, "Setting speed\n");
892 #endif
893         if (set_speed(fd, ti, u->speed) < 0) {
894                 perror("Can't set baud rate");
895                 return -1;
896         }
897
898 #ifdef STLC2500_DEBUG
899         fprintf(stderr, "Speed set...\n");
900 #endif
901
902         /* Read reply */
903         if ((n = read_hci_event(fd, resp, 10)) < 0) {
904                 fprintf(stderr, "Failed to set baud rate on chip\n");
905                 return -1;
906         }
907
908 #ifdef STLC2500_DEBUG
909         for (i = 0; i < n; i++) {
910                 fprintf(stderr, "resp[%d] = %02x\n", i, resp[i]);
911         }
912 #endif
913
914         str2ba(u->bdaddr, &bdaddr);
915         return stlc2500_init(fd, &bdaddr);
916 }
917
918 static int bgb2xx(int fd, struct uart_t *u, struct termios *ti)
919 {
920         bdaddr_t bdaddr;
921
922         str2ba(u->bdaddr, &bdaddr);
923
924         return bgb2xx_init(fd, &bdaddr);
925 }
926
927 /*
928  * Broadcom specific initialization
929  * Extracted from Jungo openrg
930  */
931 static int bcm2035(int fd, struct uart_t *u, struct termios *ti)
932 {
933         int n;
934         unsigned char cmd[30], resp[30];
935
936         /* Reset the BT Chip */
937         memset(cmd, 0, sizeof(cmd));
938         memset(resp, 0, sizeof(resp));
939         cmd[0] = HCI_COMMAND_PKT;
940         cmd[1] = 0x03;
941         cmd[2] = 0x0c;
942         cmd[3] = 0x00;
943
944         /* Send command */
945         if (write(fd, cmd, 4) != 4) {
946                 fprintf(stderr, "Failed to write reset command\n");
947                 return -1;
948         }
949
950         /* Read reply */
951         if ((n = read_hci_event(fd, resp, 4)) < 0) {
952                 fprintf(stderr, "Failed to reset chip\n");
953                 return -1;
954         }
955
956         if (u->bdaddr != NULL) {
957                 /* Set BD_ADDR */
958                 memset(cmd, 0, sizeof(cmd));
959                 memset(resp, 0, sizeof(resp));
960                 cmd[0] = HCI_COMMAND_PKT;
961                 cmd[1] = 0x01;
962                 cmd[2] = 0xfc;
963                 cmd[3] = 0x06;
964                 str2ba(u->bdaddr, (bdaddr_t *) (cmd + 4));
965
966                 /* Send command */
967                 if (write(fd, cmd, 10) != 10) {
968                         fprintf(stderr, "Failed to write BD_ADDR command\n");
969                         return -1;
970                 }
971
972                 /* Read reply */
973                 if ((n = read_hci_event(fd, resp, 10)) < 0) {
974                         fprintf(stderr, "Failed to set BD_ADDR\n");
975                         return -1;
976                 }
977         }
978
979         /* Read the local version info */
980         memset(cmd, 0, sizeof(cmd));
981         memset(resp, 0, sizeof(resp));
982         cmd[0] = HCI_COMMAND_PKT;
983         cmd[1] = 0x01;
984         cmd[2] = 0x10;
985         cmd[3] = 0x00;
986
987         /* Send command */
988         if (write(fd, cmd, 4) != 4) {
989                 fprintf(stderr, "Failed to write \"read local version\" "
990                         "command\n");
991                 return -1;
992         }
993
994         /* Read reply */
995         if ((n = read_hci_event(fd, resp, 4)) < 0) {
996                 fprintf(stderr, "Failed to read local version\n");
997                 return -1;
998         }
999
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;
1004         cmd[1] = 0x02;
1005         cmd[2] = 0x10;
1006         cmd[3] = 0x00;
1007
1008         /* Send command */
1009         if (write(fd, cmd, 4) != 4) {
1010                 fprintf(stderr, "Failed to write \"read local supported "
1011                                                 "commands\" command\n");
1012                 return -1;
1013         }
1014
1015         /* Read reply */
1016         if ((n = read_hci_event(fd, resp, 4)) < 0) {
1017                 fprintf(stderr, "Failed to read local supported commands\n");
1018                 return -1;
1019         }
1020
1021         /* Set the baud rate */
1022         memset(cmd, 0, sizeof(cmd));
1023         memset(resp, 0, sizeof(resp));
1024
1025 /* __TIZEN_PATCH__ */
1026 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1027         cmd[0] = HCI_COMMAND_PKT;
1028         cmd[1] = 0x18;
1029         cmd[2] = 0xfc;
1030         cmd[3] = 0x02;
1031         switch (u->speed) {
1032         case 57600:
1033                 cmd[4] = 0x00;
1034                 cmd[5] = 0xe6;
1035                 break;
1036         case 230400:
1037                 cmd[4] = 0x22;
1038                 cmd[5] = 0xfa;
1039                 break;
1040         case 460800:
1041                 cmd[4] = 0x22;
1042                 cmd[5] = 0xfd;
1043                 break;
1044         case 921600:
1045                 cmd[4] = 0x55;
1046                 cmd[5] = 0xff;
1047                 break;
1048         default:
1049                 /* Default is 115200 */
1050                 cmd[4] = 0x00;
1051                 cmd[5] = 0xf3;
1052                 break;
1053         }
1054         fprintf(stderr, "Baud rate parameters: DHBR=0x%2x,DLBR=0x%2x\n",
1055                 cmd[4], cmd[5]);
1056
1057         /* Send command */
1058         if (write(fd, cmd, 6) != 6) {
1059                 fprintf(stderr, "Failed to write \"set baud rate\" command\n");
1060                 return -1;
1061         }
1062 #else
1063         cmd[0] = HCI_COMMAND_PKT;
1064         cmd[1] = 0x18;
1065         cmd[2] = 0xfc;
1066
1067         switch (u->speed) {
1068         case 57600:
1069         case 230400:
1070         case 460800:
1071         case 921600:
1072         case 3000000:
1073                 break;
1074         default:
1075                 break;
1076         }
1077
1078         cmd[3] = 0x06;
1079         cmd[4] = 0x00;
1080         cmd[5] = 0x00;
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;
1085
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] );
1087
1088         /* Send command */
1089         if (write(fd, cmd, 10) != 10) {
1090                 fprintf(stderr, "Failed to write \"set baud rate\" command\n");
1091                 return -1;
1092         }
1093
1094 #endif
1095
1096         if ((n = read_hci_event(fd, resp, 6)) < 0) {
1097                 fprintf(stderr, "Failed to set baud rate\n");
1098                 return -1;
1099         }
1100
1101         return 0;
1102 }
1103
1104 #ifdef __TIZEN_PATCH__ /*SPRD add Start*/
1105 static int init_sprd_config(int fd, struct uart_t *u, struct termios *ti)
1106 {
1107
1108         return sprd_config_init(fd, u->bdaddr, ti);
1109 }
1110 #endif
1111
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 },
1118 #endif
1119         { "ericsson",   0x0000, 0x0000, HCI_UART_H4,   57600,  115200,
1120                                 FLOW_CTL, DISABLE_PM, NULL, ericsson },
1121
1122         { "digi",       0x0000, 0x0000, HCI_UART_H4,   9600,   115200,
1123                                 FLOW_CTL, DISABLE_PM, NULL, digi     },
1124
1125         { "bcsp",       0x0000, 0x0000, HCI_UART_BCSP, 115200, 115200,
1126                                 0, DISABLE_PM, NULL, bcsp     },
1127
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     },
1131
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      },
1135
1136         /* BrainBoxes PCMCIA card (BL620) */
1137         { "bboxes",     0x0160, 0x0002, HCI_UART_H4,   115200, 460800,
1138                                 FLOW_CTL, DISABLE_PM, NULL, csr      },
1139
1140         /* Silicon Wave kits */
1141         { "swave",      0x0000, 0x0000, HCI_UART_H4,   115200, 115200,
1142                                 FLOW_CTL, DISABLE_PM, NULL, swave    },
1143
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},
1148 #else
1149         /* Texas Instruments Bluelink (BRF) modules */
1150         { "texas",      0x0000, 0x0000, HCI_UART_LL,   115200, 115200,
1151                                 FLOW_CTL, DISABLE_PM, NULL, texas,    texas2 },
1152
1153         { "texasalt",   0x0000, 0x0000, HCI_UART_LL,   115200, 115200,
1154                                 FLOW_CTL, DISABLE_PM, NULL, texasalt, NULL   },
1155 #endif
1156
1157         /* ST Microelectronics minikits based on STLC2410/STLC2415 */
1158         { "st",         0x0000, 0x0000, HCI_UART_H4,    57600, 115200,
1159                                 FLOW_CTL, DISABLE_PM,  NULL, st       },
1160
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 },
1164
1165         /* Philips generic Ericsson IP core based */
1166         { "philips",    0x0000, 0x0000, HCI_UART_H4,   115200, 115200,
1167                                 FLOW_CTL, DISABLE_PM, NULL, NULL     },
1168
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 },
1172
1173         /* Sphinx Electronics PICO Card */
1174         { "picocard",   0x025e, 0x1000, HCI_UART_H4, 115200, 115200,
1175                                 FLOW_CTL, DISABLE_PM, NULL, NULL     },
1176
1177         /* Inventel BlueBird Module */
1178         { "inventel",   0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1179                                 FLOW_CTL, DISABLE_PM, NULL, NULL     },
1180
1181         /* COM One Platinium Bluetooth PC Card */
1182         { "comone",     0xffff, 0x0101, HCI_UART_BCSP, 115200, 115200,
1183                                 0, DISABLE_PM,  NULL, bcsp     },
1184
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     },
1188
1189         /* Socket Bluetooth CF Card (Rev G) */
1190         { "socket",     0x0104, 0x0096, HCI_UART_BCSP, 230400, 230400,
1191                                 0, DISABLE_PM, NULL, bcsp     },
1192
1193         /* 3Com Bluetooth Card (Version 3.0) */
1194         { "3com",       0x0101, 0x0041, HCI_UART_H4,   115200, 115200,
1195                                 FLOW_CTL, DISABLE_PM, NULL, csr      },
1196
1197         /* AmbiCom BT2000C Bluetooth PC/CF Card */
1198         { "bt2000c",    0x022d, 0x2000, HCI_UART_H4,    57600, 460800,
1199                                 FLOW_CTL, DISABLE_PM, NULL, csr      },
1200
1201         /* Zoom Bluetooth PCMCIA Card */
1202         { "zoom",       0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1203                                 0, DISABLE_PM, NULL, bcsp     },
1204
1205         /* Sitecom CN-504 PCMCIA Card */
1206         { "sitecom",    0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1207                                 0, DISABLE_PM, NULL, bcsp     },
1208
1209         /* Billionton PCBTC1 PCMCIA Card */
1210         { "billionton", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1211                                 0, DISABLE_PM, NULL, bcsp     },
1212
1213         /* Broadcom BCM2035 */
1214         { "bcm2035",    0x0A5C, 0x2035, HCI_UART_H4,   115200, 460800,
1215                                 FLOW_CTL, DISABLE_PM, NULL, bcm2035  },
1216
1217         /* Broadcom BCM43XX */
1218         { "bcm43xx",    0x0000, 0x0000, HCI_UART_H4,   115200, 3000000,
1219                                 FLOW_CTL, DISABLE_PM, NULL, bcm43xx, NULL  },
1220
1221         { "ath3k",    0x0000, 0x0000, HCI_UART_ATH3K, 115200, 115200,
1222                         FLOW_CTL, DISABLE_PM, NULL, ath3k_ps, ath3k_pm  },
1223
1224         /* QUALCOMM BTS */
1225         { "qualcomm",   0x0000, 0x0000, HCI_UART_H4,   115200, 115200,
1226                         FLOW_CTL, DISABLE_PM, NULL, qualcomm, NULL },
1227
1228         /* Intel Bluetooth Module */
1229         { "intel",      0x0000, 0x0000, HCI_UART_H4,   115200, 115200,
1230                         FLOW_CTL, DISABLE_PM, NULL, intel, NULL },
1231
1232         /* Three-wire UART */
1233         { "3wire",      0x0000, 0x0000, HCI_UART_3WIRE, 115200, 115200,
1234                         0, DISABLE_PM, NULL, NULL, NULL },
1235
1236         /* AMP controller UART */
1237         { "amp",        0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1238                         AMP_DEV, DISABLE_PM, NULL, NULL, NULL },
1239
1240         { NULL, 0 }
1241 };
1242
1243 static struct uart_t * get_by_id(int m_id, int p_id)
1244 {
1245         int i;
1246         for (i = 0; uart[i].type; i++) {
1247                 if (uart[i].m_id == m_id && uart[i].p_id == p_id)
1248                         return &uart[i];
1249         }
1250         return NULL;
1251 }
1252
1253 static struct uart_t * get_by_type(char *type)
1254 {
1255         int i;
1256         for (i = 0; uart[i].type; i++) {
1257                 if (!strcmp(uart[i].type, type))
1258                         return &uart[i];
1259         }
1260         return NULL;
1261 }
1262
1263 #if defined __TIZEN_PATCH__ && defined __BROADCOM_PATCH__
1264 static int enable_hci(char *dev, struct uart_t *u)
1265 {
1266         int fd, i;
1267         unsigned long flags = 0;
1268
1269         fd = open(dev, O_RDWR | O_NOCTTY);
1270         if (fd < 0) {
1271                 fprintf(stderr, "Can't open serial port");
1272                 return -1;
1273         }
1274
1275         tcflush(fd, TCIOFLUSH);
1276
1277         /* Set TTY to N_HCI line discipline */
1278         i = N_HCI;
1279         if (ioctl(fd, TIOCSETD, &i) < 0) {
1280                 fprintf(stderr, "Can't set line discipline");
1281                 close(fd);
1282                 return -1;
1283         }
1284
1285         if (flags && ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
1286                 fprintf(stderr, "Can't set UART flags");
1287                 close(fd);
1288                 return -1;
1289         }
1290
1291         tcflush(fd, TCIOFLUSH);
1292
1293         if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
1294                 fprintf(stderr, "Can't set device");
1295                 close(fd);
1296                 return -1;
1297         }
1298
1299         return fd;
1300 }
1301
1302 #else   /* __BROADCOM_PATCH__ */
1303
1304 /* Initialize UART driver */
1305 static int init_uart(char *dev, struct uart_t *u, int send_break, int raw)
1306 {
1307         struct termios ti;
1308         int fd, i;
1309         unsigned long flags = 0;
1310
1311 /* __TIZEN_PATCH__ */
1312 #ifdef __TIZEN_PATCH__
1313 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1314         int power;
1315 #endif
1316 #endif
1317
1318         if (raw)
1319                 flags |= 1 << HCI_UART_RAW_DEVICE;
1320
1321         if (u->flags & AMP_DEV)
1322                 flags |= 1 << HCI_UART_CREATE_AMP;
1323
1324         fd = open(dev, O_RDWR | O_NOCTTY);
1325         if (fd < 0) {
1326                 perror("Can't open serial port");
1327                 return -1;
1328         }
1329
1330         tcflush(fd, TCIOFLUSH);
1331
1332         if (tcgetattr(fd, &ti) < 0) {
1333                 perror("Can't get port settings");
1334                 goto fail;
1335         }
1336
1337         cfmakeraw(&ti);
1338
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;
1344         else
1345                 ti.c_cflag &= ~CRTSCTS;
1346 #endif
1347
1348         if (tcsetattr(fd, TCSANOW, &ti) < 0) {
1349                 perror("Can't set port settings");
1350                 goto fail;
1351         }
1352
1353         /* Set initial baudrate */
1354         if (set_speed(fd, &ti, u->init_speed) < 0) {
1355                 perror("Can't set initial baud rate");
1356                 goto fail;
1357         }
1358
1359         tcflush(fd, TCIOFLUSH);
1360
1361         if (send_break) {
1362                 tcsendbreak(fd, 0);
1363                 usleep(500000);
1364         }
1365
1366 /* __TIZEN_PATCH__ */
1367 #ifdef __TIZEN_PATCH__
1368 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1369         /* Power up the BRF chip */
1370         power = 1;
1371         ioctl(fd, TIOSETBRFPOWER, &power);
1372 #endif
1373 #ifdef __TI_PATCH__
1374         usleep(500000);
1375 #endif
1376 #endif
1377
1378         if (u->init && u->init(fd, u, &ti) < 0)
1379                 goto fail;
1380
1381         tcflush(fd, TCIOFLUSH);
1382
1383         /* Set actual baudrate */
1384         if (set_speed(fd, &ti, u->speed) < 0) {
1385                 perror("Can't set baud rate");
1386                 goto fail;
1387         }
1388
1389         /* Set TTY to N_HCI line discipline */
1390         i = N_HCI;
1391         if (ioctl(fd, TIOCSETD, &i) < 0) {
1392                 perror("Can't set line discipline");
1393                 goto fail;
1394         }
1395
1396         if (flags && ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
1397                 perror("Can't set UART flags");
1398                 goto fail;
1399         }
1400
1401         if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
1402                 perror("Can't set device");
1403                 goto fail;
1404         }
1405
1406 #ifndef __TIZEN_PATCH__
1407         if (u->post && u->post(fd, u, &ti) < 0)
1408                 goto fail;
1409 #endif
1410
1411         return fd;
1412
1413 fail:
1414         close(fd);
1415         return -1;
1416 }
1417
1418 #endif  /* __BROADCOM_PATCH__ */
1419
1420 static void usage(void)
1421 {
1422         printf("hciattach - HCI UART driver initialization utility\n");
1423         printf("Usage:\n");
1424
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");
1433 #else
1434         printf("\thciattach [-n] [-p] [-b] [-r] [-t timeout] [-s initial_speed]"
1435                         " <tty> <type | id> [speed] [flow|noflow]"
1436                         " [sleep|nosleep] [bdaddr]\n");
1437 #endif
1438         printf("\thciattach -l\n");
1439 }
1440
1441 int main(int argc, char *argv[])
1442 {
1443         struct uart_t *u = NULL;
1444 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1445         int detach, printpid, raw, opt, i, n, ld, err;
1446 #else
1447         int detach, printpid, opt, i, n, ld, err;
1448 #endif
1449         int to = 10;
1450         int init_speed = 0;
1451 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1452         int send_break = 0;
1453 #endif
1454         pid_t pid;
1455         struct sigaction sa;
1456         struct pollfd p;
1457         sigset_t sigs;
1458         char dev[PATH_MAX];
1459
1460 #ifdef __TIZEN_PATCH__
1461 /* __TIZEN_PATCH__ */
1462 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1463         int power;
1464 #endif
1465 #endif
1466 #ifdef __TI_PATCH__
1467         uint16_t device_param = 0;
1468         int reset_device = 0;
1469         int bt_fd;
1470 #endif
1471         detach = 1;
1472         printpid = 0;
1473 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1474         raw = 0;
1475 #endif
1476 #if defined(__TIZEN_PATCH__) && !defined(__TI_PATCH__)
1477         while ((opt=getopt(argc, argv, "bnprft:g:s:l")) != EOF) {
1478 #else
1479         while ((opt=getopt(argc, argv, "bnpt:s:lr")) != EOF) {
1480 #endif
1481                 switch(opt) {
1482                 case 'b':
1483 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1484                         send_break = 1;
1485 #endif
1486                         break;
1487
1488                 case 'n':
1489                         detach = 0;
1490                         break;
1491
1492                 case 'p':
1493                         printpid = 1;
1494                         break;
1495
1496                 case 't':
1497                         to = atoi(optarg);
1498                         break;
1499
1500 /* __TIZEN_PATCH__ */
1501 #if defined(__TIZEN_PATCH__) && defined(__TI_PATCH__)
1502                 case 'g':
1503                         device_param = (uint16_t)strtol(optarg, NULL, 16);
1504                         break;
1505
1506                 case 'r':
1507                         reset_device = 1;
1508                         break;
1509
1510                 case 'f':
1511                         firmware_path = 1;
1512                         break;
1513 #endif
1514                 case 's':
1515                         init_speed = atoi(optarg);
1516                         break;
1517
1518                 case 'l':
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);
1522                         }
1523                         exit(0);
1524
1525                 case 'r':
1526 #if defined(__TIZEN_PATCH__) && !defined(__BROADCOM_PATCH__)
1527                         raw = 1;
1528 #endif
1529                         break;
1530
1531                 default:
1532                         usage();
1533                         exit(1);
1534                 }
1535         }
1536
1537         n = argc - optind;
1538 /* __TIZEN_PATCH__ */
1539 #if defined(__TIZEN_PATCH__) && defined(__TI_PATCH__)
1540         if (!reset_device || (reset_device && n < 1))
1541 #endif
1542         if (n < 2) {
1543                 usage();
1544                 exit(1);
1545         }
1546
1547         for (n = 0; optind < argc; n++, optind++) {
1548                 char *opt;
1549
1550                 opt = argv[optind];
1551
1552                 switch(n) {
1553                 case 0:
1554                         dev[0] = 0;
1555                         if (!strchr(opt, '/'))
1556                                 strcpy(dev, "/dev/");
1557
1558                         if (strlen(opt) > PATH_MAX - (strlen(dev) + 1)) {
1559                                 fprintf(stderr, "Invalid serial device\n");
1560                                 exit(1);
1561                         }
1562
1563                         strcat(dev, opt);
1564                         break;
1565
1566                 case 1:
1567                         if (strchr(argv[optind], ',')) {
1568                                 int m_id, p_id;
1569                                 sscanf(argv[optind], "%x,%x", &m_id, &p_id);
1570                                 u = get_by_id(m_id, p_id);
1571                         } else {
1572                                 u = get_by_type(opt);
1573                         }
1574
1575                         if (!u) {
1576                                 fprintf(stderr, "Unknown device type or id\n");
1577                                 exit(1);
1578                         }
1579
1580                         break;
1581
1582                 case 2:
1583                         u->speed = atoi(argv[optind]);
1584                         break;
1585
1586                 case 3:
1587                         if (!strcmp("flow", argv[optind]))
1588                                 u->flags |=  FLOW_CTL;
1589                         else
1590                                 u->flags &= ~FLOW_CTL;
1591                         break;
1592
1593                 case 4:
1594                         if (!strcmp("sleep", argv[optind]))
1595                                 u->pm = ENABLE_PM;
1596                         else
1597                                 u->pm = DISABLE_PM;
1598                         break;
1599
1600                 case 5:
1601                         u->bdaddr = argv[optind];
1602                         break;
1603                 }
1604         }
1605 /* __TIZEN_PATCH__ */
1606 #if defined(__TIZEN_PATCH__) && defined(__TI_PATCH__)
1607         if (reset_device)
1608         {
1609                 // Reset row device
1610                 bt_fd = open(dev, O_RDWR | O_NOCTTY);
1611                 if (bt_fd< 0) {
1612                         perror("Can't open serial port");
1613                         return -1;
1614                 }
1615                 /* Power up the BRF chip */
1616                 power = 0;
1617                 ioctl(bt_fd, TIOSETBRFPOWER, &power);
1618                 return 0;
1619         }
1620 #endif
1621
1622         if (!u) {
1623                 fprintf(stderr, "Unknown device type or id\n");
1624                 exit(1);
1625         }
1626
1627 #ifdef __TIZEN_PATCH__
1628 //      __hci_attach_log_init();
1629 #endif
1630
1631         /* If user specified a initial speed, use that instead of
1632            the hardware's default */
1633         if (init_speed)
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 */
1638         if (device_param)
1639                 u->device_param = device_param;
1640 #endif
1641
1642         memset(&sa, 0, sizeof(sa));
1643         sa.sa_flags   = SA_NOCLDSTOP;
1644         sa.sa_handler = sig_alarm;
1645         sigaction(SIGALRM, &sa, NULL);
1646
1647         /* 10 seconds should be enough for initialization */
1648         alarm(to);
1649         bcsp_max_retries = to;
1650 #if defined __TIZEN_PATCH__ && defined __BROADCOM_PATCH__
1651         n = enable_hci(dev, u);
1652 #else
1653         n = init_uart(dev, u, send_break, raw);
1654 #endif
1655         if (n < 0) {
1656                 perror("Can't initialize device");
1657                 exit(1);
1658         }
1659
1660         printf("Device setup complete\n");
1661
1662         alarm(0);
1663
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);
1669
1670         sa.sa_handler = sig_term;
1671         sigaction(SIGTERM, &sa, NULL);
1672         sigaction(SIGINT,  &sa, NULL);
1673
1674         sa.sa_handler = sig_hup;
1675         sigaction(SIGHUP, &sa, NULL);
1676
1677         if (detach) {
1678                 if ((pid = fork())) {
1679                         if (printpid)
1680                                 printf("%d\n", pid);
1681                         return 0;
1682                 }
1683
1684                 for (i = 0; i < 20; i++)
1685                         if (i != n)
1686                                 close(i);
1687         }
1688
1689         p.fd = n;
1690         p.events = POLLERR | POLLHUP;
1691
1692         sigfillset(&sigs);
1693         sigdelset(&sigs, SIGCHLD);
1694         sigdelset(&sigs, SIGPIPE);
1695         sigdelset(&sigs, SIGTERM);
1696         sigdelset(&sigs, SIGINT);
1697         sigdelset(&sigs, SIGHUP);
1698
1699         while (!__io_canceled) {
1700                 p.revents = 0;
1701                 err = ppoll(&p, 1, NULL, &sigs);
1702                 if (err < 0 && errno == EINTR)
1703                         continue;
1704                 if (err)
1705                         break;
1706         }
1707
1708         /* Restore TTY line discipline */
1709         ld = N_TTY;
1710         if (ioctl(n, TIOCSETD, &ld) < 0) {
1711                 perror("Can't restore line discipline");
1712                 exit(1);
1713         }
1714
1715 /* __TIZEN_PATCH__ */
1716 #ifdef __TIZEN_PATCH__
1717 #if defined(__TI_PATCH__) || defined(__BROADCOM_PATCH__)
1718         /* Power down the BRF or BCMchip */
1719         power = 0;
1720         ioctl(n, TIOSETBRFPOWER, &power);
1721 #endif
1722 #endif
1723         return 0;
1724 }