CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/cache/sources/hcitools/hciattach.c
Views: 3959
1
/*
2
*
3
* BlueZ - Bluetooth protocol stack for Linux
4
*
5
* Copyright (C) 2000-2001 Qualcomm Incorporated
6
* Copyright (C) 2002-2003 Maxim Krasnyansky <[email protected]>
7
* Copyright (C) 2002-2010 Marcel Holtmann <[email protected]>
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
static volatile sig_atomic_t __io_canceled = 0;
52
53
static void sig_hup(int sig)
54
{
55
}
56
57
static void sig_term(int sig)
58
{
59
__io_canceled = 1;
60
}
61
62
static void sig_alarm(int sig)
63
{
64
fprintf(stderr, "Initialization timed out.\n");
65
exit(1);
66
}
67
68
int uart_speed(int s)
69
{
70
switch (s) {
71
case 9600:
72
return B9600;
73
case 19200:
74
return B19200;
75
case 38400:
76
return B38400;
77
case 57600:
78
return B57600;
79
case 115200:
80
return B115200;
81
case 230400:
82
return B230400;
83
case 460800:
84
return B460800;
85
case 500000:
86
return B500000;
87
case 576000:
88
return B576000;
89
case 921600:
90
return B921600;
91
case 1000000:
92
return B1000000;
93
case 1152000:
94
return B1152000;
95
case 1500000:
96
return B1500000;
97
case 2000000:
98
return B2000000;
99
#ifdef B2500000
100
case 2500000:
101
return B2500000;
102
#endif
103
#ifdef B3000000
104
case 3000000:
105
return B3000000;
106
#endif
107
#ifdef B3500000
108
case 3500000:
109
return B3500000;
110
#endif
111
#ifdef B3710000
112
case 3710000
113
return B3710000;
114
#endif
115
#ifdef B4000000
116
case 4000000:
117
return B4000000;
118
#endif
119
default:
120
return B57600;
121
}
122
}
123
124
int set_speed(int fd, struct termios *ti, int speed)
125
{
126
if (cfsetospeed(ti, uart_speed(speed)) < 0)
127
return -errno;
128
129
if (cfsetispeed(ti, uart_speed(speed)) < 0)
130
return -errno;
131
132
if (tcsetattr(fd, TCSANOW, ti) < 0)
133
return -errno;
134
135
return 0;
136
}
137
138
/*
139
* Read an HCI event from the given file descriptor.
140
*/
141
int read_hci_event(int fd, unsigned char* buf, int size)
142
{
143
int remain, r;
144
int count = 0;
145
146
if (size <= 0)
147
return -1;
148
149
/* The first byte identifies the packet type. For HCI event packets, it
150
* should be 0x04, so we read until we get to the 0x04. */
151
while (1) {
152
r = read(fd, buf, 1);
153
if (r <= 0)
154
return -1;
155
if (buf[0] == 0x04)
156
break;
157
}
158
count++;
159
160
/* The next two bytes are the event code and parameter total length. */
161
while (count < 3) {
162
r = read(fd, buf + count, 3 - count);
163
if (r <= 0)
164
return -1;
165
count += r;
166
}
167
168
/* Now we read the parameters. */
169
if (buf[2] < (size - 3))
170
remain = buf[2];
171
else
172
remain = size - 3;
173
174
while ((count - 3) < remain) {
175
r = read(fd, buf + count, remain - (count - 3));
176
if (r <= 0)
177
return -1;
178
count += r;
179
}
180
181
return count;
182
}
183
184
/*
185
* Ericsson specific initialization
186
*/
187
static int ericsson(int fd, struct uart_t *u, struct termios *ti)
188
{
189
struct timespec tm = {0, 50000};
190
char cmd[5];
191
192
cmd[0] = HCI_COMMAND_PKT;
193
cmd[1] = 0x09;
194
cmd[2] = 0xfc;
195
cmd[3] = 0x01;
196
197
switch (u->speed) {
198
case 57600:
199
cmd[4] = 0x03;
200
break;
201
case 115200:
202
cmd[4] = 0x02;
203
break;
204
case 230400:
205
cmd[4] = 0x01;
206
break;
207
case 460800:
208
cmd[4] = 0x00;
209
break;
210
case 921600:
211
cmd[4] = 0x20;
212
break;
213
case 2000000:
214
cmd[4] = 0x25;
215
break;
216
case 3000000:
217
cmd[4] = 0x27;
218
break;
219
case 4000000:
220
cmd[4] = 0x2B;
221
break;
222
default:
223
cmd[4] = 0x03;
224
u->speed = 57600;
225
fprintf(stderr, "Invalid speed requested, using %d bps instead\n", u->speed);
226
break;
227
}
228
229
/* Send initialization command */
230
if (write(fd, cmd, 5) != 5) {
231
perror("Failed to write init command");
232
return -1;
233
}
234
235
nanosleep(&tm, NULL);
236
return 0;
237
}
238
239
/*
240
* Digianswer specific initialization
241
*/
242
static int digi(int fd, struct uart_t *u, struct termios *ti)
243
{
244
struct timespec tm = {0, 50000};
245
char cmd[5];
246
247
/* DigiAnswer set baud rate command */
248
cmd[0] = HCI_COMMAND_PKT;
249
cmd[1] = 0x07;
250
cmd[2] = 0xfc;
251
cmd[3] = 0x01;
252
253
switch (u->speed) {
254
case 57600:
255
cmd[4] = 0x08;
256
break;
257
case 115200:
258
cmd[4] = 0x09;
259
break;
260
default:
261
cmd[4] = 0x09;
262
u->speed = 115200;
263
break;
264
}
265
266
/* Send initialization command */
267
if (write(fd, cmd, 5) != 5) {
268
perror("Failed to write init command");
269
return -1;
270
}
271
272
nanosleep(&tm, NULL);
273
return 0;
274
}
275
276
static int texas(int fd, struct uart_t *u, struct termios *ti)
277
{
278
return texas_init(fd, &u->speed, ti);
279
}
280
281
static int texas2(int fd, struct uart_t *u, struct termios *ti)
282
{
283
return texas_post(fd, ti);
284
}
285
286
static int texasalt(int fd, struct uart_t *u, struct termios *ti)
287
{
288
return texasalt_init(fd, u->speed, ti);
289
}
290
291
static int ath3k_ps(int fd, struct uart_t *u, struct termios *ti)
292
{
293
return ath3k_init(fd, u->speed, u->init_speed, u->bdaddr, ti);
294
}
295
296
static int ath3k_pm(int fd, struct uart_t *u, struct termios *ti)
297
{
298
return ath3k_post(fd, u->pm);
299
}
300
301
static int qualcomm(int fd, struct uart_t *u, struct termios *ti)
302
{
303
return qualcomm_init(fd, u->speed, ti, u->bdaddr);
304
}
305
306
static int intel(int fd, struct uart_t *u, struct termios *ti)
307
{
308
return intel_init(fd, u->init_speed, &u->speed, ti);
309
}
310
311
static int bcm43xx(int fd, struct uart_t *u, struct termios *ti)
312
{
313
return bcm43xx_init(fd, u->speed, ti, u->bdaddr);
314
}
315
316
//Realtek_add_start
317
//add realtek Bluetooth init and post function.
318
static int realtek_init(int fd, struct uart_t *u, struct termios *ti)
319
{
320
fprintf(stderr, "Realtek Bluetooth init uart with init speed:%d, final_speed:%d, type:HCI UART %s\n", u->init_speed, u->speed, (u->proto == HCI_UART_H4)? "H4":"H5" );
321
return rtk_init(fd, u->proto, u->speed, ti);
322
}
323
324
static int realtek_post(int fd, struct uart_t *u, struct termios *ti)
325
{
326
fprintf(stderr, "Realtek Bluetooth post process\n");
327
return rtk_post(fd, u->proto, ti);
328
}
329
330
// add xradio Bluetooth init and post function.
331
static int xradio_init(int fd, struct uart_t *u, struct termios *ti)
332
{
333
fprintf(stderr, "XRADIO Bluetooth init uart with init speed:%d, final_speed:%d, type:HCI UART %s\n", u->init_speed, u->speed, (u->proto == HCI_UART_H4)? "H4":"H5" );
334
return xr_init(fd, u, ti);
335
}
336
337
static int xradio_post(int fd, struct uart_t *u, struct termios *ti)
338
{
339
fprintf(stderr, "XRADIO Bluetooth post process\n");
340
return xr_post(fd, u, ti);
341
}
342
343
// add sprd Bluetooth init and post function.
344
static int sprd_init(int fd, struct uart_t *u, struct termios *ti)
345
{
346
fprintf(stderr, "SPRD Bluetooth init uart with init speed:%d, final_speed:%d, type:HCI UART %s\n", u->init_speed, u->speed, (u->proto == HCI_UART_H4)? "H4":"H5" );
347
return sprd_config_init(fd, u, ti);
348
}
349
350
static int sprd_post(int fd, struct uart_t *u, struct termios *ti)
351
{
352
fprintf(stderr, "SPRD Bluetooth post process\n");
353
return sprd_config_post(fd, u, ti);
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 <[email protected]> - 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 <[email protected]>
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 <[email protected]>
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
cmd[0] = HCI_COMMAND_PKT;
1025
cmd[1] = 0x18;
1026
cmd[2] = 0xfc;
1027
cmd[3] = 0x02;
1028
switch (u->speed) {
1029
case 57600:
1030
cmd[4] = 0x00;
1031
cmd[5] = 0xe6;
1032
break;
1033
case 230400:
1034
cmd[4] = 0x22;
1035
cmd[5] = 0xfa;
1036
break;
1037
case 460800:
1038
cmd[4] = 0x22;
1039
cmd[5] = 0xfd;
1040
break;
1041
case 921600:
1042
cmd[4] = 0x55;
1043
cmd[5] = 0xff;
1044
break;
1045
default:
1046
/* Default is 115200 */
1047
cmd[4] = 0x00;
1048
cmd[5] = 0xf3;
1049
break;
1050
}
1051
fprintf(stderr, "Baud rate parameters: DHBR=0x%2x,DLBR=0x%2x\n",
1052
cmd[4], cmd[5]);
1053
1054
/* Send command */
1055
if (write(fd, cmd, 6) != 6) {
1056
fprintf(stderr, "Failed to write \"set baud rate\" command\n");
1057
return -1;
1058
}
1059
1060
if ((n = read_hci_event(fd, resp, 6)) < 0) {
1061
fprintf(stderr, "Failed to set baud rate\n");
1062
return -1;
1063
}
1064
1065
return 0;
1066
}
1067
1068
struct uart_t uart[] = {
1069
{ "any", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1070
FLOW_CTL, DISABLE_PM, NULL, NULL, NULL },
1071
1072
{ "ericsson", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200,
1073
FLOW_CTL, DISABLE_PM, NULL, ericsson, NULL },
1074
1075
{ "digi", 0x0000, 0x0000, HCI_UART_H4, 9600, 115200,
1076
FLOW_CTL, DISABLE_PM, NULL, digi, NULL },
1077
1078
{ "bcsp", 0x0000, 0x0000, HCI_UART_BCSP, 115200, 115200,
1079
0, DISABLE_PM, NULL, bcsp, NULL },
1080
1081
/* Xircom PCMCIA cards: Credit Card Adapter and Real Port Adapter */
1082
{ "xircom", 0x0105, 0x080a, HCI_UART_H4, 115200, 115200,
1083
FLOW_CTL, DISABLE_PM, NULL, NULL, NULL },
1084
1085
/* CSR Casira serial adapter or BrainBoxes serial dongle (BL642) */
1086
{ "csr", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1087
FLOW_CTL, DISABLE_PM, NULL, csr, NULL },
1088
1089
/* BrainBoxes PCMCIA card (BL620) */
1090
{ "bboxes", 0x0160, 0x0002, HCI_UART_H4, 115200, 460800,
1091
FLOW_CTL, DISABLE_PM, NULL, csr, NULL },
1092
1093
/* Silicon Wave kits */
1094
{ "swave", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1095
FLOW_CTL, DISABLE_PM, NULL, swave, NULL },
1096
1097
/* Texas Instruments Bluelink (BRF) modules */
1098
{ "texas", 0x0000, 0x0000, HCI_UART_LL, 115200, 115200,
1099
FLOW_CTL, DISABLE_PM, NULL, texas, texas2 },
1100
1101
{ "texasalt", 0x0000, 0x0000, HCI_UART_LL, 115200, 115200,
1102
FLOW_CTL, DISABLE_PM, NULL, texasalt, NULL },
1103
1104
/* ST Microelectronics minikits based on STLC2410/STLC2415 */
1105
{ "st", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200,
1106
FLOW_CTL, DISABLE_PM, NULL, st, NULL },
1107
1108
/* ST Microelectronics minikits based on STLC2500 */
1109
{ "stlc2500", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1110
FLOW_CTL, DISABLE_PM, "00:80:E1:00:AB:BA", stlc2500, NULL },
1111
1112
/* Philips generic Ericsson IP core based */
1113
{ "philips", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1114
FLOW_CTL, DISABLE_PM, NULL, NULL, NULL },
1115
1116
/* Philips BGB2xx Module */
1117
{ "bgb2xx", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1118
FLOW_CTL, DISABLE_PM, "BD:B2:10:00:AB:BA", bgb2xx, NULL },
1119
1120
/* Sphinx Electronics PICO Card */
1121
{ "picocard", 0x025e, 0x1000, HCI_UART_H4, 115200, 115200,
1122
FLOW_CTL, DISABLE_PM, NULL, NULL, NULL },
1123
1124
/* Inventel BlueBird Module */
1125
{ "inventel", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1126
FLOW_CTL, DISABLE_PM, NULL, NULL, NULL },
1127
1128
/* COM One Platinium Bluetooth PC Card */
1129
{ "comone", 0xffff, 0x0101, HCI_UART_BCSP, 115200, 115200,
1130
0, DISABLE_PM, NULL, bcsp, NULL },
1131
1132
/* TDK Bluetooth PC Card and IBM Bluetooth PC Card II */
1133
{ "tdk", 0x0105, 0x4254, HCI_UART_BCSP, 115200, 115200,
1134
0, DISABLE_PM, NULL, bcsp, NULL },
1135
1136
/* Socket Bluetooth CF Card (Rev G) */
1137
{ "socket", 0x0104, 0x0096, HCI_UART_BCSP, 230400, 230400,
1138
0, DISABLE_PM, NULL, bcsp, NULL },
1139
1140
/* 3Com Bluetooth Card (Version 3.0) */
1141
{ "3com", 0x0101, 0x0041, HCI_UART_H4, 115200, 115200,
1142
FLOW_CTL, DISABLE_PM, NULL, csr, NULL },
1143
1144
/* AmbiCom BT2000C Bluetooth PC/CF Card */
1145
{ "bt2000c", 0x022d, 0x2000, HCI_UART_H4, 57600, 460800,
1146
FLOW_CTL, DISABLE_PM, NULL, csr, NULL },
1147
1148
/* Zoom Bluetooth PCMCIA Card */
1149
{ "zoom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1150
0, DISABLE_PM, NULL, bcsp, NULL },
1151
1152
/* Sitecom CN-504 PCMCIA Card */
1153
{ "sitecom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1154
0, DISABLE_PM, NULL, bcsp, NULL },
1155
1156
/* Billionton PCBTC1 PCMCIA Card */
1157
{ "billionton", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200,
1158
0, DISABLE_PM, NULL, bcsp, NULL },
1159
1160
/* Broadcom BCM2035 */
1161
{ "bcm2035", 0x0A5C, 0x2035, HCI_UART_H4, 115200, 460800,
1162
FLOW_CTL, DISABLE_PM, NULL, bcm2035, NULL },
1163
1164
/* Broadcom BCM43XX */
1165
{ "bcm43xx", 0x0000, 0x0000, HCI_UART_H4, 115200, 3000000,
1166
FLOW_CTL, DISABLE_PM, NULL, bcm43xx, NULL },
1167
1168
{ "ath3k", 0x0000, 0x0000, HCI_UART_ATH3K, 115200, 115200,
1169
FLOW_CTL, DISABLE_PM, NULL, ath3k_ps, ath3k_pm },
1170
1171
/* QUALCOMM BTS */
1172
{ "qualcomm", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1173
FLOW_CTL, DISABLE_PM, NULL, qualcomm, NULL },
1174
1175
/* Intel Bluetooth Module */
1176
{ "intel", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1177
FLOW_CTL, DISABLE_PM, NULL, intel, NULL },
1178
1179
/* Three-wire UART */
1180
{ "3wire", 0x0000, 0x0000, HCI_UART_3WIRE, 115200, 115200,
1181
0, DISABLE_PM, NULL, NULL, NULL },
1182
1183
/* AMP controller UART */
1184
{ "amp", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200,
1185
AMP_DEV, DISABLE_PM, NULL, NULL, NULL },
1186
1187
//Realtek_add_start
1188
/* Realtek Bluetooth H4*/
1189
/* H4 will set 115200 baudrate and flow control enable by default*/
1190
{ "rtk_h4", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200, FLOW_CTL, DISABLE_PM, NULL, realtek_init, realtek_post},
1191
/* Realtek Bluetooth H5*/
1192
/* H5 will set 921600 baudrate and flow control disable by default */
1193
/* H5 will be realtek's recommanded protocol */
1194
{ "rtk_h5", 0x0000, 0x0000, HCI_UART_3WIRE, 115200,115200, 0, DISABLE_PM, NULL, realtek_init, realtek_post},
1195
//Realtek_add_end
1196
1197
{ "xradio", 0x0000, 0x0000, HCI_UART_H4, 115200, 1500000, 0, DISABLE_PM, NULL, xradio_init, xradio_post},
1198
{ "sprd", 0x0000, 0x0000, NULL, 115200, 1500000, FLOW_CTL, DISABLE_PM, NULL, sprd_init, sprd_post},
1199
1200
{ NULL, 0 }
1201
};
1202
1203
static struct uart_t * get_by_id(int m_id, int p_id)
1204
{
1205
int i;
1206
for (i = 0; uart[i].type; i++) {
1207
if (uart[i].m_id == m_id && uart[i].p_id == p_id)
1208
return &uart[i];
1209
}
1210
return NULL;
1211
}
1212
1213
static struct uart_t * get_by_type(char *type)
1214
{
1215
int i;
1216
for (i = 0; uart[i].type; i++) {
1217
if (!strcmp(uart[i].type, type))
1218
return &uart[i];
1219
}
1220
return NULL;
1221
}
1222
1223
/* Initialize UART driver */
1224
static int init_uart(char *dev, struct uart_t *u, int send_break, int raw)
1225
{
1226
struct termios ti;
1227
int fd, i;
1228
unsigned long flags = 0;
1229
1230
if (raw)
1231
flags |= 1 << HCI_UART_RAW_DEVICE;
1232
1233
if (u->flags & AMP_DEV)
1234
flags |= 1 << HCI_UART_CREATE_AMP;
1235
1236
fd = open(dev, O_RDWR | O_NOCTTY);
1237
if (fd < 0) {
1238
perror("Can't open serial port");
1239
return -1;
1240
}
1241
1242
tcflush(fd, TCIOFLUSH);
1243
1244
if (tcgetattr(fd, &ti) < 0) {
1245
perror("Can't get port settings");
1246
return -1;
1247
}
1248
1249
cfmakeraw(&ti);
1250
1251
ti.c_cflag |= CLOCAL;
1252
if (u->flags & FLOW_CTL)
1253
ti.c_cflag |= CRTSCTS;
1254
else
1255
ti.c_cflag &= ~CRTSCTS;
1256
1257
if (tcsetattr(fd, TCSANOW, &ti) < 0) {
1258
perror("Can't set port settings");
1259
return -1;
1260
}
1261
1262
/* Set initial baudrate */
1263
if (set_speed(fd, &ti, u->init_speed) < 0) {
1264
perror("Can't set initial baud rate");
1265
return -1;
1266
}
1267
1268
tcflush(fd, TCIOFLUSH);
1269
1270
if (send_break) {
1271
tcsendbreak(fd, 0);
1272
usleep(500000);
1273
}
1274
1275
if (u->init && u->init(fd, u, &ti) < 0)
1276
return -1;
1277
1278
tcflush(fd, TCIOFLUSH);
1279
1280
/* Set actual baudrate */
1281
if (set_speed(fd, &ti, u->speed) < 0) {
1282
perror("Can't set baud rate");
1283
return -1;
1284
}
1285
1286
/* Set TTY to N_HCI line discipline */
1287
i = N_HCI;
1288
if (ioctl(fd, TIOCSETD, &i) < 0) {
1289
perror("Can't set line discipline");
1290
return -1;
1291
}
1292
1293
if (flags && ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
1294
perror("Can't set UART flags");
1295
return -1;
1296
}
1297
1298
if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
1299
perror("Can't set device");
1300
return -1;
1301
}
1302
1303
if (u->post && u->post(fd, u, &ti) < 0)
1304
return -1;
1305
1306
return fd;
1307
}
1308
1309
static void usage(void)
1310
{
1311
printf("hciattach - HCI UART driver initialization utility\n");
1312
printf("Usage:\n");
1313
printf("\thciattach [-n] [-p] [-b] [-r] [-t timeout] [-s initial_speed] <tty> <type | id> [speed] [flow|noflow] [bdaddr]\n");
1314
printf("\thciattach -l\n");
1315
}
1316
1317
int main(int argc, char *argv[])
1318
{
1319
struct uart_t *u = NULL;
1320
int detach, printpid, raw, opt, i, n, ld, err;
1321
int to = 10;
1322
int init_speed = 0;
1323
int send_break = 0;
1324
pid_t pid;
1325
struct sigaction sa;
1326
struct pollfd p;
1327
sigset_t sigs;
1328
char dev[PATH_MAX];
1329
1330
detach = 1;
1331
printpid = 0;
1332
raw = 0;
1333
1334
while ((opt=getopt(argc, argv, "bnpt:s:lr")) != EOF) {
1335
switch(opt) {
1336
case 'b':
1337
send_break = 1;
1338
break;
1339
1340
case 'n':
1341
detach = 0;
1342
break;
1343
1344
case 'p':
1345
printpid = 1;
1346
break;
1347
1348
case 't':
1349
to = atoi(optarg);
1350
break;
1351
1352
case 's':
1353
init_speed = atoi(optarg);
1354
break;
1355
1356
case 'l':
1357
for (i = 0; uart[i].type; i++) {
1358
printf("%-10s0x%04x,0x%04x\n", uart[i].type,
1359
uart[i].m_id, uart[i].p_id);
1360
}
1361
exit(0);
1362
1363
case 'r':
1364
raw = 1;
1365
break;
1366
1367
default:
1368
usage();
1369
exit(1);
1370
}
1371
}
1372
1373
n = argc - optind;
1374
if (n < 2) {
1375
usage();
1376
exit(1);
1377
}
1378
1379
for (n = 0; optind < argc; n++, optind++) {
1380
char *opt;
1381
1382
opt = argv[optind];
1383
1384
switch(n) {
1385
case 0:
1386
dev[0] = 0;
1387
if (!strchr(opt, '/'))
1388
strcpy(dev, "/dev/");
1389
strcat(dev, opt);
1390
break;
1391
1392
case 1:
1393
if (strchr(argv[optind], ',')) {
1394
uint32_t m_id, p_id;
1395
sscanf(argv[optind], "%x,%x", &m_id, &p_id);
1396
u = get_by_id(m_id, p_id);
1397
} else {
1398
u = get_by_type(opt);
1399
}
1400
1401
if (!u) {
1402
fprintf(stderr, "Unknown device type or id\n");
1403
exit(1);
1404
}
1405
1406
break;
1407
1408
case 2:
1409
u->speed = atoi(argv[optind]);
1410
break;
1411
1412
case 3:
1413
if (!strcmp("flow", argv[optind]))
1414
u->flags |= FLOW_CTL;
1415
else
1416
u->flags &= ~FLOW_CTL;
1417
break;
1418
1419
case 4:
1420
if (!strcmp("sleep", argv[optind]))
1421
u->pm = ENABLE_PM;
1422
else
1423
u->pm = DISABLE_PM;
1424
break;
1425
1426
case 5:
1427
u->bdaddr = argv[optind];
1428
break;
1429
}
1430
}
1431
1432
if (!u) {
1433
fprintf(stderr, "Unknown device type or id\n");
1434
exit(1);
1435
}
1436
1437
/* If user specified a initial speed, use that instead of
1438
the hardware's default */
1439
if (init_speed)
1440
u->init_speed = init_speed;
1441
1442
memset(&sa, 0, sizeof(sa));
1443
sa.sa_flags = SA_NOCLDSTOP;
1444
sa.sa_handler = sig_alarm;
1445
sigaction(SIGALRM, &sa, NULL);
1446
1447
/* 10 seconds should be enough for initialization */
1448
alarm(to);
1449
bcsp_max_retries = to;
1450
1451
n = init_uart(dev, u, send_break, raw);
1452
if (n < 0) {
1453
perror("Can't initialize device");
1454
exit(1);
1455
}
1456
1457
printf("Device setup complete\n");
1458
1459
alarm(0);
1460
1461
memset(&sa, 0, sizeof(sa));
1462
sa.sa_flags = SA_NOCLDSTOP;
1463
sa.sa_handler = SIG_IGN;
1464
sigaction(SIGCHLD, &sa, NULL);
1465
sigaction(SIGPIPE, &sa, NULL);
1466
1467
sa.sa_handler = sig_term;
1468
sigaction(SIGTERM, &sa, NULL);
1469
sigaction(SIGINT, &sa, NULL);
1470
1471
sa.sa_handler = sig_hup;
1472
sigaction(SIGHUP, &sa, NULL);
1473
1474
if (detach) {
1475
if ((pid = fork())) {
1476
if (printpid)
1477
printf("%d\n", pid);
1478
return 0;
1479
}
1480
1481
for (i = 0; i < 20; i++)
1482
if (i != n)
1483
close(i);
1484
}
1485
1486
p.fd = n;
1487
p.events = POLLERR | POLLHUP;
1488
1489
sigfillset(&sigs);
1490
sigdelset(&sigs, SIGCHLD);
1491
sigdelset(&sigs, SIGPIPE);
1492
sigdelset(&sigs, SIGTERM);
1493
sigdelset(&sigs, SIGINT);
1494
sigdelset(&sigs, SIGHUP);
1495
1496
while (!__io_canceled) {
1497
p.revents = 0;
1498
err = ppoll(&p, 1, NULL, &sigs);
1499
if (err < 0 && errno == EINTR)
1500
continue;
1501
if (err)
1502
break;
1503
}
1504
1505
/* Restore TTY line discipline */
1506
ld = N_TTY;
1507
if (ioctl(n, TIOCSETD, &ld) < 0) {
1508
perror("Can't restore line discipline");
1509
exit(1);
1510
}
1511
1512
return 0;
1513
}
1514
1515