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