• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define _GNU_SOURCE
31 #include <stdio.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <signal.h>
38 #include <syslog.h>
39 #include <termios.h>
40 #include <time.h>
41 #include <sys/time.h>
42 #include <sys/poll.h>
43 #include <sys/param.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <sys/uio.h>
47 
48 #include <bluetooth/bluetooth.h>
49 #include <bluetooth/hci.h>
50 #include <bluetooth/hci_lib.h>
51 
52 #include "hciattach.h"
53 
54 #include "ppoll.h"
55 
56 struct uart_t {
57 	char *type;
58 	int  m_id;
59 	int  p_id;
60 	int  proto;
61 	int  init_speed;
62 	int  speed;
63 	int  flags;
64 	char *bdaddr;
65 	int  (*init) (int fd, struct uart_t *u, struct termios *ti);
66 	int  (*post) (int fd, struct uart_t *u, struct termios *ti);
67 };
68 
69 #define FLOW_CTL	0x0001
70 
71 static volatile sig_atomic_t __io_canceled = 0;
72 
sig_hup(int sig)73 static void sig_hup(int sig)
74 {
75 }
76 
sig_term(int sig)77 static void sig_term(int sig)
78 {
79 	__io_canceled = 1;
80 }
81 
sig_alarm(int sig)82 static void sig_alarm(int sig)
83 {
84 	fprintf(stderr, "Initialization timed out.\n");
85 	exit(1);
86 }
87 
uart_speed(int s)88 static int uart_speed(int s)
89 {
90 	switch (s) {
91 	case 9600:
92 		return B9600;
93 	case 19200:
94 		return B19200;
95 	case 38400:
96 		return B38400;
97 	case 57600:
98 		return B57600;
99 	case 115200:
100 		return B115200;
101 	case 230400:
102 		return B230400;
103 	case 460800:
104 		return B460800;
105 	case 500000:
106 		return B500000;
107 	case 576000:
108 		return B576000;
109 	case 921600:
110 		return B921600;
111 	case 1000000:
112 		return B1000000;
113 	case 1152000:
114 		return B1152000;
115 	case 1500000:
116 		return B1500000;
117 	case 2000000:
118 		return B2000000;
119 #ifdef B2500000
120 	case 2500000:
121 		return B2500000;
122 #endif
123 #ifdef B3000000
124 	case 3000000:
125 		return B3000000;
126 #endif
127 #ifdef B3500000
128 	case 3500000:
129 		return B3500000;
130 #endif
131 #ifdef B4000000
132 	case 4000000:
133 		return B4000000;
134 #endif
135 	default:
136 		return B57600;
137 	}
138 }
139 
set_speed(int fd,struct termios * ti,int speed)140 int set_speed(int fd, struct termios *ti, int speed)
141 {
142 	cfsetospeed(ti, uart_speed(speed));
143 	cfsetispeed(ti, uart_speed(speed));
144 	return tcsetattr(fd, TCSANOW, ti);
145 }
146 
147 /*
148  * Read an HCI event from the given file descriptor.
149  */
read_hci_event(int fd,unsigned char * buf,int size)150 int read_hci_event(int fd, unsigned char* buf, int size)
151 {
152 	int remain, r;
153 	int count = 0;
154 
155 	if (size <= 0)
156 		return -1;
157 
158 	/* The first byte identifies the packet type. For HCI event packets, it
159 	 * should be 0x04, so we read until we get to the 0x04. */
160 	while (1) {
161 		r = read(fd, buf, 1);
162 		if (r <= 0)
163 			return -1;
164 		if (buf[0] == 0x04)
165 			break;
166 	}
167 	count++;
168 
169 	/* The next two bytes are the event code and parameter total length. */
170 	while (count < 3) {
171 		r = read(fd, buf + count, 3 - count);
172 		if (r <= 0)
173 			return -1;
174 		count += r;
175 	}
176 
177 	/* Now we read the parameters. */
178 	if (buf[2] < (size - 3))
179 		remain = buf[2];
180 	else
181 		remain = size - 3;
182 
183 	while ((count - 3) < remain) {
184 		r = read(fd, buf + count, remain - (count - 3));
185 		if (r <= 0)
186 			return -1;
187 		count += r;
188 	}
189 
190 	return count;
191 }
192 
193 /*
194  * Ericsson specific initialization
195  */
ericsson(int fd,struct uart_t * u,struct termios * ti)196 static int ericsson(int fd, struct uart_t *u, struct termios *ti)
197 {
198 	struct timespec tm = {0, 50000};
199 	char cmd[5];
200 
201 	cmd[0] = HCI_COMMAND_PKT;
202 	cmd[1] = 0x09;
203 	cmd[2] = 0xfc;
204 	cmd[3] = 0x01;
205 
206 	switch (u->speed) {
207 	case 57600:
208 		cmd[4] = 0x03;
209 		break;
210 	case 115200:
211 		cmd[4] = 0x02;
212 		break;
213 	case 230400:
214 		cmd[4] = 0x01;
215 		break;
216 	case 460800:
217 		cmd[4] = 0x00;
218 		break;
219 	case 921600:
220 		cmd[4] = 0x20;
221 		break;
222 	case 2000000:
223 		cmd[4] = 0x25;
224 		break;
225 	case 3000000:
226 		cmd[4] = 0x27;
227 		break;
228 	case 4000000:
229 		cmd[4] = 0x2B;
230 		break;
231 	default:
232 		cmd[4] = 0x03;
233 		u->speed = 57600;
234 		fprintf(stderr, "Invalid speed requested, using %d bps instead\n", u->speed);
235 		break;
236 	}
237 
238 	/* Send initialization command */
239 	if (write(fd, cmd, 5) != 5) {
240 		perror("Failed to write init command");
241 		return -1;
242 	}
243 
244 	nanosleep(&tm, NULL);
245 	return 0;
246 }
247 
248 /*
249  * Digianswer specific initialization
250  */
digi(int fd,struct uart_t * u,struct termios * ti)251 static int digi(int fd, struct uart_t *u, struct termios *ti)
252 {
253 	struct timespec tm = {0, 50000};
254 	char cmd[5];
255 
256 	/* DigiAnswer set baud rate command */
257 	cmd[0] = HCI_COMMAND_PKT;
258 	cmd[1] = 0x07;
259 	cmd[2] = 0xfc;
260 	cmd[3] = 0x01;
261 
262 	switch (u->speed) {
263 	case 57600:
264 		cmd[4] = 0x08;
265 		break;
266 	case 115200:
267 		cmd[4] = 0x09;
268 		break;
269 	default:
270 		cmd[4] = 0x09;
271 		u->speed = 115200;
272 		break;
273 	}
274 
275 	/* Send initialization command */
276 	if (write(fd, cmd, 5) != 5) {
277 		perror("Failed to write init command");
278 		return -1;
279 	}
280 
281 	nanosleep(&tm, NULL);
282 	return 0;
283 }
284 
texas(int fd,struct uart_t * u,struct termios * ti)285 static int texas(int fd, struct uart_t *u, struct termios *ti)
286 {
287 	return texas_init(fd, ti);
288 }
289 
texas2(int fd,struct uart_t * u,struct termios * ti)290 static int texas2(int fd, struct uart_t *u, struct termios *ti)
291 {
292 	return texas_post(fd, ti);
293 }
294 
texasalt(int fd,struct uart_t * u,struct termios * ti)295 static int texasalt(int fd, struct uart_t *u, struct termios *ti)
296 {
297 	return texasalt_init(fd, u->speed, ti);
298 }
299 
read_check(int fd,void * buf,int count)300 static int read_check(int fd, void *buf, int count)
301 {
302 	int res;
303 
304 	do {
305 		res = read(fd, buf, count);
306 		if (res != -1) {
307 			buf += res;
308 			count -= res;
309 		}
310 	} while (count && (errno == 0 || errno == EINTR));
311 
312 	if (count)
313 		return -1;
314 
315 	return 0;
316 }
317 
318 /*
319  * BCSP specific initialization
320  */
321 static int serial_fd;
322 static int bcsp_max_retries = 10;
323 
bcsp_tshy_sig_alarm(int sig)324 static void bcsp_tshy_sig_alarm(int sig)
325 {
326 	unsigned char bcsp_sync_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xda,0xdc,0xed,0xed,0xc0};
327 	int len;
328 	static int retries = 0;
329 
330 	if (retries < bcsp_max_retries) {
331 		retries++;
332 		len = write(serial_fd, &bcsp_sync_pkt, 10);
333 		alarm(1);
334 		return;
335 	}
336 
337 	tcflush(serial_fd, TCIOFLUSH);
338 	fprintf(stderr, "BCSP initialization timed out\n");
339 	exit(1);
340 }
341 
bcsp_tconf_sig_alarm(int sig)342 static void bcsp_tconf_sig_alarm(int sig)
343 {
344 	unsigned char bcsp_conf_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xad,0xef,0xac,0xed,0xc0};
345 	int len;
346 	static int retries = 0;
347 
348 	if (retries < bcsp_max_retries){
349 		retries++;
350 		len = write(serial_fd, &bcsp_conf_pkt, 10);
351 		alarm(1);
352 		return;
353 	}
354 
355 	tcflush(serial_fd, TCIOFLUSH);
356 	fprintf(stderr, "BCSP initialization timed out\n");
357 	exit(1);
358 }
359 
bcsp(int fd,struct uart_t * u,struct termios * ti)360 static int bcsp(int fd, struct uart_t *u, struct termios *ti)
361 {
362 	unsigned char byte, bcsph[4], bcspp[4],
363 		bcsp_sync_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xac,0xaf,0xef,0xee,0xc0},
364 		bcsp_conf_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xde,0xad,0xd0,0xd0,0xc0},
365 		bcspsync[4]     = {0xda, 0xdc, 0xed, 0xed},
366 		bcspsyncresp[4] = {0xac,0xaf,0xef,0xee},
367 		bcspconf[4]     = {0xad,0xef,0xac,0xed},
368 		bcspconfresp[4] = {0xde,0xad,0xd0,0xd0};
369 	struct sigaction sa;
370 	int len;
371 
372 	if (set_speed(fd, ti, u->speed) < 0) {
373 		perror("Can't set default baud rate");
374 		return -1;
375 	}
376 
377 	ti->c_cflag |= PARENB;
378 	ti->c_cflag &= ~(PARODD);
379 
380 	if (tcsetattr(fd, TCSANOW, ti) < 0) {
381 		perror("Can't set port settings");
382 		return -1;
383 	}
384 
385 	alarm(0);
386 
387 	serial_fd = fd;
388 	memset(&sa, 0, sizeof(sa));
389 	sa.sa_flags = SA_NOCLDSTOP;
390 	sa.sa_handler = bcsp_tshy_sig_alarm;
391 	sigaction(SIGALRM, &sa, NULL);
392 
393 	/* State = shy */
394 
395 	bcsp_tshy_sig_alarm(0);
396 	while (1) {
397 		do {
398 			if (read_check(fd, &byte, 1) == -1){
399 				perror("Failed to read");
400 				return -1;
401 			}
402 		} while (byte != 0xC0);
403 
404 		do {
405 			if ( read_check(fd, &bcsph[0], 1) == -1){
406 				perror("Failed to read");
407 				return -1;
408 			}
409 		} while (bcsph[0] == 0xC0);
410 
411 		if ( read_check(fd, &bcsph[1], 3) == -1){
412 			perror("Failed to read");
413 			return -1;
414 		}
415 
416 		if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
417 			continue;
418 		if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
419 			continue;
420 
421 		if (read_check(fd, &bcspp, 4) == -1){
422 			perror("Failed to read");
423 			return -1;
424 		}
425 
426 		if (!memcmp(bcspp, bcspsync, 4)) {
427 			len = write(fd, &bcsp_sync_resp_pkt,10);
428 		} else if (!memcmp(bcspp, bcspsyncresp, 4))
429 			break;
430 	}
431 
432 	/* State = curious */
433 
434 	alarm(0);
435 	sa.sa_handler = bcsp_tconf_sig_alarm;
436 	sigaction(SIGALRM, &sa, NULL);
437 	alarm(1);
438 
439 	while (1) {
440 		do {
441 			if (read_check(fd, &byte, 1) == -1){
442 				perror("Failed to read");
443 				return -1;
444 			}
445 		} while (byte != 0xC0);
446 
447 		do {
448 			if (read_check(fd, &bcsph[0], 1) == -1){
449 				perror("Failed to read");
450 				return -1;
451 			}
452 		} while (bcsph[0] == 0xC0);
453 
454 		if (read_check(fd, &bcsph[1], 3) == -1){
455 			perror("Failed to read");
456 			return -1;
457 		}
458 
459 		if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
460 			continue;
461 
462 		if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
463 			continue;
464 
465 		if (read_check(fd, &bcspp, 4) == -1){
466 			perror("Failed to read");
467 			return -1;
468 		}
469 
470 		if (!memcmp(bcspp, bcspsync, 4))
471 			len = write(fd, &bcsp_sync_resp_pkt, 10);
472 		else if (!memcmp(bcspp, bcspconf, 4))
473 			len = write(fd, &bcsp_conf_resp_pkt, 10);
474 		else if (!memcmp(bcspp, bcspconfresp,  4))
475 			break;
476 	}
477 
478 	/* State = garrulous */
479 
480 	return 0;
481 }
482 
483 /*
484  * CSR specific initialization
485  * Inspired strongly by code in OpenBT and experimentations with Brainboxes
486  * Pcmcia card.
487  * Jean Tourrilhes <jt@hpl.hp.com> - 14.11.01
488  */
csr(int fd,struct uart_t * u,struct termios * ti)489 static int csr(int fd, struct uart_t *u, struct termios *ti)
490 {
491 	struct timespec tm = {0, 10000000};	/* 10ms - be generous */
492 	unsigned char cmd[30];		/* Command */
493 	unsigned char resp[30];		/* Response */
494 	int  clen = 0;		/* Command len */
495 	static int csr_seq = 0;	/* Sequence number of command */
496 	int  divisor;
497 
498 	/* It seems that if we set the CSR UART speed straight away, it
499 	 * won't work, the CSR UART gets into a state where we can't talk
500 	 * to it anymore.
501 	 * On the other hand, doing a read before setting the CSR speed
502 	 * seems to be ok.
503 	 * Therefore, the strategy is to read the build ID (useful for
504 	 * debugging) and only then set the CSR UART speed. Doing like
505 	 * this is more complex but at least it works ;-)
506 	 * The CSR UART control may be slow to wake up or something because
507 	 * every time I read its speed, its bogus...
508 	 * Jean II */
509 
510 	/* Try to read the build ID of the CSR chip */
511 	clen = 5 + (5 + 6) * 2;
512 	/* HCI header */
513 	cmd[0] = HCI_COMMAND_PKT;
514 	cmd[1] = 0x00;		/* CSR command */
515 	cmd[2] = 0xfc;		/* MANUFACTURER_SPEC */
516 	cmd[3] = 1 + (5 + 6) * 2;	/* len */
517 	/* CSR MSG header */
518 	cmd[4] = 0xC2;		/* first+last+channel=BCC */
519 	/* CSR BCC header */
520 	cmd[5] = 0x00;		/* type = GET-REQ */
521 	cmd[6] = 0x00;		/* - msB */
522 	cmd[7] = 5 + 4;		/* len */
523 	cmd[8] = 0x00;		/* - msB */
524 	cmd[9] = csr_seq & 0xFF;/* seq num */
525 	cmd[10] = (csr_seq >> 8) & 0xFF;	/* - msB */
526 	csr_seq++;
527 	cmd[11] = 0x19;		/* var_id = CSR_CMD_BUILD_ID */
528 	cmd[12] = 0x28;		/* - msB */
529 	cmd[13] = 0x00;		/* status = STATUS_OK */
530 	cmd[14] = 0x00;		/* - msB */
531 	/* CSR BCC payload */
532 	memset(cmd + 15, 0, 6 * 2);
533 
534 	/* Send command */
535 	do {
536 		if (write(fd, cmd, clen) != clen) {
537 			perror("Failed to write init command (GET_BUILD_ID)");
538 			return -1;
539 		}
540 
541 		/* Read reply. */
542 		if (read_hci_event(fd, resp, 100) < 0) {
543 			perror("Failed to read init response (GET_BUILD_ID)");
544 			return -1;
545 		}
546 
547 	/* Event code 0xFF is for vendor-specific events, which is
548 	 * what we're looking for. */
549 	} while (resp[1] != 0xFF);
550 
551 #ifdef CSR_DEBUG
552 	{
553 	char temp[512];
554 	int i;
555 	for (i=0; i < rlen; i++)
556 		sprintf(temp + (i*3), "-%02X", resp[i]);
557 	fprintf(stderr, "Reading CSR build ID %d [%s]\n", rlen, temp + 1);
558 	// In theory, it should look like :
559 	// 04-FF-13-FF-01-00-09-00-00-00-19-28-00-00-73-00-00-00-00-00-00-00
560 	}
561 #endif
562 	/* Display that to user */
563 	fprintf(stderr, "CSR build ID 0x%02X-0x%02X\n",
564 		resp[15] & 0xFF, resp[14] & 0xFF);
565 
566 	/* Try to read the current speed of the CSR chip */
567 	clen = 5 + (5 + 4)*2;
568 	/* -- HCI header */
569 	cmd[3] = 1 + (5 + 4)*2;	/* len */
570 	/* -- CSR BCC header -- */
571 	cmd[9] = csr_seq & 0xFF;	/* seq num */
572 	cmd[10] = (csr_seq >> 8) & 0xFF;	/* - msB */
573 	csr_seq++;
574 	cmd[11] = 0x02;		/* var_id = CONFIG_UART */
575 	cmd[12] = 0x68;		/* - msB */
576 
577 #ifdef CSR_DEBUG
578 	/* Send command */
579 	do {
580 		if (write(fd, cmd, clen) != clen) {
581 			perror("Failed to write init command (GET_BUILD_ID)");
582 			return -1;
583 		}
584 
585 		/* Read reply. */
586 		if (read_hci_event(fd, resp, 100) < 0) {
587 			perror("Failed to read init response (GET_BUILD_ID)");
588 			return -1;
589 		}
590 
591 	/* Event code 0xFF is for vendor-specific events, which is
592 	 * what we're looking for. */
593 	} while (resp[1] != 0xFF);
594 
595 	{
596 	char temp[512];
597 	int i;
598 	for (i=0; i < rlen; i++)
599 		sprintf(temp + (i*3), "-%02X", resp[i]);
600 	fprintf(stderr, "Reading CSR UART speed %d [%s]\n", rlen, temp+1);
601 	}
602 #endif
603 
604 	if (u->speed > 1500000) {
605 		fprintf(stderr, "Speed %d too high. Remaining at %d baud\n",
606 			u->speed, u->init_speed);
607 		u->speed = u->init_speed;
608 	} else if (u->speed != 57600 && uart_speed(u->speed) == B57600) {
609 		/* Unknown speed. Why oh why can't we just pass an int to the kernel? */
610 		fprintf(stderr, "Speed %d unrecognised. Remaining at %d baud\n",
611 			u->speed, u->init_speed);
612 		u->speed = u->init_speed;
613 	}
614 	if (u->speed == u->init_speed)
615 		return 0;
616 
617 	/* Now, create the command that will set the UART speed */
618 	/* CSR BCC header */
619 	cmd[5] = 0x02;			/* type = SET-REQ */
620 	cmd[6] = 0x00;			/* - msB */
621 	cmd[9] = csr_seq & 0xFF;	/* seq num */
622 	cmd[10] = (csr_seq >> 8) & 0xFF;/* - msB */
623 	csr_seq++;
624 
625 	divisor = (u->speed*64+7812)/15625;
626 
627 	/* No parity, one stop bit -> divisor |= 0x0000; */
628 	cmd[15] = (divisor) & 0xFF;		/* divider */
629 	cmd[16] = (divisor >> 8) & 0xFF;	/* - msB */
630 	/* The rest of the payload will be 0x00 */
631 
632 #ifdef CSR_DEBUG
633 	{
634 	char temp[512];
635 	int i;
636 	for(i = 0; i < clen; i++)
637 		sprintf(temp + (i*3), "-%02X", cmd[i]);
638 	fprintf(stderr, "Writing CSR UART speed %d [%s]\n", clen, temp + 1);
639 	// In theory, it should look like :
640 	// 01-00-FC-13-C2-02-00-09-00-03-00-02-68-00-00-BF-0E-00-00-00-00-00-00
641 	// 01-00-FC-13-C2-02-00-09-00-01-00-02-68-00-00-D8-01-00-00-00-00-00-00
642 	}
643 #endif
644 
645 	/* Send the command to set the CSR UART speed */
646 	if (write(fd, cmd, clen) != clen) {
647 		perror("Failed to write init command (SET_UART_SPEED)");
648 		return -1;
649 	}
650 
651 	nanosleep(&tm, NULL);
652 	return 0;
653 }
654 
655 /*
656  * Silicon Wave specific initialization
657  * Thomas Moser <thomas.moser@tmoser.ch>
658  */
swave(int fd,struct uart_t * u,struct termios * ti)659 static int swave(int fd, struct uart_t *u, struct termios *ti)
660 {
661 	struct timespec tm = { 0, 500000 };
662 	char cmd[10], rsp[100];
663 	int r;
664 
665 	// Silicon Wave set baud rate command
666 	// see HCI Vendor Specific Interface from Silicon Wave
667 	// first send a "param access set" command to set the
668 	// appropriate data fields in RAM. Then send a "HCI Reset
669 	// Subcommand", e.g. "soft reset" to make the changes effective.
670 
671 	cmd[0] = HCI_COMMAND_PKT;	// it's a command packet
672 	cmd[1] = 0x0B;			// OCF 0x0B	= param access set
673 	cmd[2] = 0xfc;			// OGF bx111111 = vendor specific
674 	cmd[3] = 0x06;			// 6 bytes of data following
675 	cmd[4] = 0x01;			// param sub command
676 	cmd[5] = 0x11;			// tag 17 = 0x11 = HCI Transport Params
677 	cmd[6] = 0x03;			// length of the parameter following
678 	cmd[7] = 0x01;			// HCI Transport flow control enable
679 	cmd[8] = 0x01;			// HCI Transport Type = UART
680 
681 	switch (u->speed) {
682 	case 19200:
683 		cmd[9] = 0x03;
684 		break;
685 	case 38400:
686 		cmd[9] = 0x02;
687 		break;
688 	case 57600:
689 		cmd[9] = 0x01;
690 		break;
691 	case 115200:
692 		cmd[9] = 0x00;
693 		break;
694 	default:
695 		u->speed = 115200;
696 		cmd[9] = 0x00;
697 		break;
698 	}
699 
700 	/* Send initialization command */
701 	if (write(fd, cmd, 10) != 10) {
702 		perror("Failed to write init command");
703 		return -1;
704 	}
705 
706 	// We should wait for a "GET Event" to confirm the success of
707 	// the baud rate setting. Wait some time before reading. Better:
708 	// read with timeout, parse data
709 	// until correct answer, else error handling ... todo ...
710 
711 	nanosleep(&tm, NULL);
712 
713 	r = read(fd, rsp, sizeof(rsp));
714 	if (r > 0) {
715 		// guess it's okay, but we should parse the reply. But since
716 		// I don't react on an error anyway ... todo
717 		// Response packet format:
718 		//  04	Event
719 		//  FF	Vendor specific
720 		//  07	Parameter length
721 		//  0B	Subcommand
722 		//  01	Setevent
723 		//  11	Tag specifying HCI Transport Layer Parameter
724 		//  03	length
725 		//  01	flow on
726 		//  01 	Hci Transport type = Uart
727 		//  xx	Baud rate set (see above)
728 	} else {
729 		// ups, got error.
730 		return -1;
731 	}
732 
733 	// we probably got the reply. Now we must send the "soft reset"
734 	// which is standard HCI RESET.
735 
736 	cmd[0] = HCI_COMMAND_PKT;	// it's a command packet
737 	cmd[1] = 0x03;
738 	cmd[2] = 0x0c;
739 	cmd[3] = 0x00;
740 
741 	/* Send reset command */
742 	if (write(fd, cmd, 4) != 4) {
743 		perror("Can't write Silicon Wave reset cmd.");
744 		return -1;
745 	}
746 
747 	nanosleep(&tm, NULL);
748 
749 	// now the uart baud rate on the silicon wave module is set and effective.
750 	// change our own baud rate as well. Then there is a reset event comming in
751  	// on the *new* baud rate. This is *undocumented*! The packet looks like this:
752 	// 04 FF 01 0B (which would make that a confirmation of 0x0B = "Param
753 	// subcommand class". So: change to new baud rate, read with timeout, parse
754 	// data, error handling. BTW: all param access in Silicon Wave is done this way.
755 	// Maybe this code would belong in a seperate file, or at least code reuse...
756 
757 	return 0;
758 }
759 
760 /*
761  * ST Microelectronics specific initialization
762  * Marcel Holtmann <marcel@holtmann.org>
763  */
st(int fd,struct uart_t * u,struct termios * ti)764 static int st(int fd, struct uart_t *u, struct termios *ti)
765 {
766 	struct timespec tm = {0, 50000};
767 	char cmd[5];
768 
769 	/* ST Microelectronics set baud rate command */
770 	cmd[0] = HCI_COMMAND_PKT;
771 	cmd[1] = 0x46;			// OCF = Hci_Cmd_ST_Set_Uart_Baud_Rate
772 	cmd[2] = 0xfc;			// OGF = Vendor specific
773 	cmd[3] = 0x01;
774 
775 	switch (u->speed) {
776 	case 9600:
777 		cmd[4] = 0x09;
778 		break;
779 	case 19200:
780 		cmd[4] = 0x0b;
781 		break;
782 	case 38400:
783 		cmd[4] = 0x0d;
784 		break;
785 	case 57600:
786 		cmd[4] = 0x0e;
787 		break;
788 	case 115200:
789 		cmd[4] = 0x10;
790 		break;
791 	case 230400:
792 		cmd[4] = 0x12;
793 		break;
794 	case 460800:
795 		cmd[4] = 0x13;
796 		break;
797 	case 921600:
798 		cmd[4] = 0x14;
799 		break;
800 	default:
801 		cmd[4] = 0x10;
802 		u->speed = 115200;
803 		break;
804 	}
805 
806 	/* Send initialization command */
807 	if (write(fd, cmd, 5) != 5) {
808 		perror("Failed to write init command");
809 		return -1;
810 	}
811 
812 	nanosleep(&tm, NULL);
813 	return 0;
814 }
815 
stlc2500(int fd,struct uart_t * u,struct termios * ti)816 static int stlc2500(int fd, struct uart_t *u, struct termios *ti)
817 {
818 	bdaddr_t bdaddr;
819 	unsigned char resp[10];
820 	int n;
821 	int rvalue;
822 
823 	/* STLC2500 has an ericsson core */
824 	rvalue = ericsson(fd, u, ti);
825 	if (rvalue != 0)
826 		return rvalue;
827 
828 #ifdef STLC2500_DEBUG
829 	fprintf(stderr, "Setting speed\n");
830 #endif
831 	if (set_speed(fd, ti, u->speed) < 0) {
832 		perror("Can't set baud rate");
833 		return -1;
834 	}
835 
836 #ifdef STLC2500_DEBUG
837 	fprintf(stderr, "Speed set...\n");
838 #endif
839 
840 	/* Read reply */
841 	if ((n = read_hci_event(fd, resp, 10)) < 0) {
842 		fprintf(stderr, "Failed to set baud rate on chip\n");
843 		return -1;
844 	}
845 
846 #ifdef STLC2500_DEBUG
847 	for (i = 0; i < n; i++) {
848 		fprintf(stderr, "resp[%d] = %02x\n", i, resp[i]);
849 	}
850 #endif
851 
852 	str2ba(u->bdaddr, &bdaddr);
853 	return stlc2500_init(fd, &bdaddr);
854 }
855 
bgb2xx(int fd,struct uart_t * u,struct termios * ti)856 static int bgb2xx(int fd, struct uart_t *u, struct termios *ti)
857 {
858 	bdaddr_t bdaddr;
859 
860 	str2ba(u->bdaddr, &bdaddr);
861 
862 	return bgb2xx_init(fd, &bdaddr);
863 }
864 
865 /*
866  * Broadcom specific initialization
867  * Extracted from Jungo openrg
868  */
bcm2035(int fd,struct uart_t * u,struct termios * ti)869 static int bcm2035(int fd, struct uart_t *u, struct termios *ti)
870 {
871 	int n;
872 	unsigned char cmd[30], resp[30];
873 
874 	/* Reset the BT Chip */
875 	memset(cmd, 0, sizeof(cmd));
876 	memset(resp, 0, sizeof(resp));
877 	cmd[0] = HCI_COMMAND_PKT;
878 	cmd[1] = 0x03;
879 	cmd[2] = 0x0c;
880 	cmd[3] = 0x00;
881 
882 	/* Send command */
883 	if (write(fd, cmd, 4) != 4) {
884 		fprintf(stderr, "Failed to write reset command\n");
885 		return -1;
886 	}
887 
888 	/* Read reply */
889 	if ((n = read_hci_event(fd, resp, 4)) < 0) {
890 		fprintf(stderr, "Failed to reset chip\n");
891 		return -1;
892 	}
893 
894 	if (u->bdaddr != NULL) {
895 		/* Set BD_ADDR */
896 		memset(cmd, 0, sizeof(cmd));
897 		memset(resp, 0, sizeof(resp));
898 		cmd[0] = HCI_COMMAND_PKT;
899 		cmd[1] = 0x01;
900 		cmd[2] = 0xfc;
901 		cmd[3] = 0x06;
902 		str2ba(u->bdaddr, (bdaddr_t *) (cmd + 4));
903 
904 		/* Send command */
905 		if (write(fd, cmd, 10) != 10) {
906 			fprintf(stderr, "Failed to write BD_ADDR command\n");
907 			return -1;
908 		}
909 
910 		/* Read reply */
911 		if ((n = read_hci_event(fd, resp, 10)) < 0) {
912 			fprintf(stderr, "Failed to set BD_ADDR\n");
913 			return -1;
914 		}
915 	}
916 
917 	/* Read the local version info */
918 	memset(cmd, 0, sizeof(cmd));
919 	memset(resp, 0, sizeof(resp));
920 	cmd[0] = HCI_COMMAND_PKT;
921 	cmd[1] = 0x01;
922 	cmd[2] = 0x10;
923 	cmd[3] = 0x00;
924 
925 	/* Send command */
926 	if (write(fd, cmd, 4) != 4) {
927 		fprintf(stderr, "Failed to write \"read local version\" "
928 			"command\n");
929 		return -1;
930 	}
931 
932 	/* Read reply */
933 	if ((n = read_hci_event(fd, resp, 4)) < 0) {
934 		fprintf(stderr, "Failed to read local version\n");
935 		return -1;
936 	}
937 
938 	/* Read the local supported commands info */
939 	memset(cmd, 0, sizeof(cmd));
940 	memset(resp, 0, sizeof(resp));
941 	cmd[0] = HCI_COMMAND_PKT;
942 	cmd[1] = 0x02;
943 	cmd[2] = 0x10;
944 	cmd[3] = 0x00;
945 
946 	/* Send command */
947 	if (write(fd, cmd, 4) != 4) {
948 		fprintf(stderr, "Failed to write \"read local supported "
949 						"commands\" command\n");
950 		return -1;
951 	}
952 
953 	/* Read reply */
954 	if ((n = read_hci_event(fd, resp, 4)) < 0) {
955 		fprintf(stderr, "Failed to read local supported commands\n");
956 		return -1;
957 	}
958 
959 	/* Set the baud rate */
960 	memset(cmd, 0, sizeof(cmd));
961 	memset(resp, 0, sizeof(resp));
962 	cmd[0] = HCI_COMMAND_PKT;
963 	cmd[1] = 0x18;
964 	cmd[2] = 0xfc;
965 	cmd[3] = 0x02;
966 	switch (u->speed) {
967 	case 57600:
968 		cmd[4] = 0x00;
969 		cmd[5] = 0xe6;
970 		break;
971 	case 230400:
972 		cmd[4] = 0x22;
973 		cmd[5] = 0xfa;
974 		break;
975 	case 460800:
976 		cmd[4] = 0x22;
977 		cmd[5] = 0xfd;
978 		break;
979 	case 921600:
980 		cmd[4] = 0x55;
981 		cmd[5] = 0xff;
982 		break;
983 	default:
984 		/* Default is 115200 */
985 		cmd[4] = 0x00;
986 		cmd[5] = 0xf3;
987 		break;
988 	}
989 	fprintf(stderr, "Baud rate parameters: DHBR=0x%2x,DLBR=0x%2x\n",
990 		cmd[4], cmd[5]);
991 
992 	/* Send command */
993 	if (write(fd, cmd, 6) != 6) {
994 		fprintf(stderr, "Failed to write \"set baud rate\" command\n");
995 		return -1;
996 	}
997 
998 	if ((n = read_hci_event(fd, resp, 6)) < 0) {
999 		fprintf(stderr, "Failed to set baud rate\n");
1000 		return -1;
1001 	}
1002 
1003 	return 0;
1004 }
1005 
1006 struct uart_t uart[] = {
1007 	{ "any",        0x0000, 0x0000, HCI_UART_H4,   115200, 115200, FLOW_CTL, NULL, NULL     },
1008 	{ "ericsson",   0x0000, 0x0000, HCI_UART_H4,   57600,  115200, FLOW_CTL, NULL, ericsson },
1009 	{ "digi",       0x0000, 0x0000, HCI_UART_H4,   9600,   115200, FLOW_CTL, NULL, digi     },
1010 
1011 	{ "bcsp",       0x0000, 0x0000, HCI_UART_BCSP, 115200, 115200, 0,        NULL, bcsp     },
1012 
1013 	/* Xircom PCMCIA cards: Credit Card Adapter and Real Port Adapter */
1014 	{ "xircom",     0x0105, 0x080a, HCI_UART_H4,   115200, 115200, FLOW_CTL, NULL, NULL     },
1015 
1016 	/* CSR Casira serial adapter or BrainBoxes serial dongle (BL642) */
1017 	{ "csr",        0x0000, 0x0000, HCI_UART_H4,   115200, 115200, FLOW_CTL, NULL, csr      },
1018 
1019 	/* BrainBoxes PCMCIA card (BL620) */
1020 	{ "bboxes",     0x0160, 0x0002, HCI_UART_H4,   115200, 460800, FLOW_CTL, NULL, csr      },
1021 
1022 	/* Silicon Wave kits */
1023 	{ "swave",      0x0000, 0x0000, HCI_UART_H4,   115200, 115200, FLOW_CTL, NULL, swave    },
1024 
1025 	/* Texas Instruments Bluelink (BRF) modules */
1026 	{ "texas",      0x0000, 0x0000, HCI_UART_LL,   115200, 115200, FLOW_CTL, NULL, texas,    texas2 },
1027 	{ "texasalt",   0x0000, 0x0000, HCI_UART_LL,   115200, 115200, FLOW_CTL, NULL, texasalt, NULL   },
1028 
1029 	/* ST Microelectronics minikits based on STLC2410/STLC2415 */
1030 	{ "st",         0x0000, 0x0000, HCI_UART_H4,    57600, 115200, FLOW_CTL, NULL, st       },
1031 
1032 	/* ST Microelectronics minikits based on STLC2500 */
1033 	{ "stlc2500",   0x0000, 0x0000, HCI_UART_H4,   115200, 115200, FLOW_CTL, "00:80:E1:00:AB:BA", stlc2500 },
1034 
1035 	/* Philips generic Ericsson IP core based */
1036 	{ "philips",    0x0000, 0x0000, HCI_UART_H4,   115200, 115200, FLOW_CTL, NULL, NULL     },
1037 
1038 	/* Philips BGB2xx Module */
1039 	{ "bgb2xx",    0x0000, 0x0000, HCI_UART_H4,   115200, 115200, FLOW_CTL, "BD:B2:10:00:AB:BA", bgb2xx },
1040 
1041 	/* Sphinx Electronics PICO Card */
1042 	{ "picocard",   0x025e, 0x1000, HCI_UART_H4,   115200, 115200, FLOW_CTL, NULL, NULL     },
1043 
1044 	/* Inventel BlueBird Module */
1045 	{ "inventel",   0x0000, 0x0000, HCI_UART_H4,   115200, 115200, FLOW_CTL, NULL, NULL     },
1046 
1047 	/* COM One Platinium Bluetooth PC Card */
1048 	{ "comone",     0xffff, 0x0101, HCI_UART_BCSP, 115200, 115200, 0,        NULL, bcsp     },
1049 
1050 	/* TDK Bluetooth PC Card and IBM Bluetooth PC Card II */
1051 	{ "tdk",        0x0105, 0x4254, HCI_UART_BCSP, 115200, 115200, 0,        NULL, bcsp     },
1052 
1053 	/* Socket Bluetooth CF Card (Rev G) */
1054 	{ "socket",     0x0104, 0x0096, HCI_UART_BCSP, 230400, 230400, 0,        NULL, bcsp     },
1055 
1056 	/* 3Com Bluetooth Card (Version 3.0) */
1057 	{ "3com",       0x0101, 0x0041, HCI_UART_H4,   115200, 115200, FLOW_CTL, NULL, csr      },
1058 
1059 	/* AmbiCom BT2000C Bluetooth PC/CF Card */
1060 	{ "bt2000c",    0x022d, 0x2000, HCI_UART_H4,    57600, 460800, FLOW_CTL, NULL, csr      },
1061 
1062 	/* Zoom Bluetooth PCMCIA Card */
1063 	{ "zoom",       0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200, 0,        NULL, bcsp     },
1064 
1065 	/* Sitecom CN-504 PCMCIA Card */
1066 	{ "sitecom",    0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200, 0,        NULL, bcsp     },
1067 
1068 	/* Billionton PCBTC1 PCMCIA Card */
1069 	{ "billionton", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200, 0,        NULL, bcsp     },
1070 
1071 	/* Broadcom BCM2035 */
1072 	{ "bcm2035",    0x0A5C, 0x2035, HCI_UART_H4,   115200, 460800, FLOW_CTL, NULL, bcm2035  },
1073 
1074 	{ NULL, 0 }
1075 };
1076 
get_by_id(int m_id,int p_id)1077 static struct uart_t * get_by_id(int m_id, int p_id)
1078 {
1079 	int i;
1080 	for (i = 0; uart[i].type; i++) {
1081 		if (uart[i].m_id == m_id && uart[i].p_id == p_id)
1082 			return &uart[i];
1083 	}
1084 	return NULL;
1085 }
1086 
get_by_type(char * type)1087 static struct uart_t * get_by_type(char *type)
1088 {
1089 	int i;
1090 	for (i = 0; uart[i].type; i++) {
1091 		if (!strcmp(uart[i].type, type))
1092 			return &uart[i];
1093 	}
1094 	return NULL;
1095 }
1096 
1097 /* Initialize UART driver */
init_uart(char * dev,struct uart_t * u,int send_break,int raw)1098 static int init_uart(char *dev, struct uart_t *u, int send_break, int raw)
1099 {
1100 	struct termios ti;
1101 	int fd, i;
1102 	unsigned long flags = 0;
1103 
1104 	if (raw)
1105 		flags |= 1 << HCI_UART_RAW_DEVICE;
1106 
1107 	fd = open(dev, O_RDWR | O_NOCTTY);
1108 	if (fd < 0) {
1109 		perror("Can't open serial port");
1110 		return -1;
1111 	}
1112 
1113 	tcflush(fd, TCIOFLUSH);
1114 
1115 	if (tcgetattr(fd, &ti) < 0) {
1116 		perror("Can't get port settings");
1117 		return -1;
1118 	}
1119 
1120 	cfmakeraw(&ti);
1121 
1122 	ti.c_cflag |= CLOCAL;
1123 	if (u->flags & FLOW_CTL)
1124 		ti.c_cflag |= CRTSCTS;
1125 	else
1126 		ti.c_cflag &= ~CRTSCTS;
1127 
1128 	if (tcsetattr(fd, TCSANOW, &ti) < 0) {
1129 		perror("Can't set port settings");
1130 		return -1;
1131 	}
1132 
1133 	/* Set initial baudrate */
1134 	if (set_speed(fd, &ti, u->init_speed) < 0) {
1135 		perror("Can't set initial baud rate");
1136 		return -1;
1137 	}
1138 
1139 	tcflush(fd, TCIOFLUSH);
1140 
1141 	if (send_break) {
1142 		tcsendbreak(fd, 0);
1143 		usleep(500000);
1144 	}
1145 
1146 	if (u->init && u->init(fd, u, &ti) < 0)
1147 		return -1;
1148 
1149 	tcflush(fd, TCIOFLUSH);
1150 
1151 	/* Set actual baudrate */
1152 	if (set_speed(fd, &ti, u->speed) < 0) {
1153 		perror("Can't set baud rate");
1154 		return -1;
1155 	}
1156 
1157 	/* Set TTY to N_HCI line discipline */
1158 	i = N_HCI;
1159 	if (ioctl(fd, TIOCSETD, &i) < 0) {
1160 		perror("Can't set line discipline");
1161 		return -1;
1162 	}
1163 
1164 	if (flags && ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
1165 		perror("Can't set UART flags");
1166 		return -1;
1167 	}
1168 
1169 	if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
1170 		perror("Can't set device");
1171 		return -1;
1172 	}
1173 
1174 	if (u->post && u->post(fd, u, &ti) < 0)
1175 		return -1;
1176 
1177 	return fd;
1178 }
1179 
usage(void)1180 static void usage(void)
1181 {
1182 	printf("hciattach - HCI UART driver initialization utility\n");
1183 	printf("Usage:\n");
1184 	printf("\thciattach [-n] [-p] [-b] [-r] [-t timeout] [-s initial_speed] <tty> <type | id> [speed] [flow|noflow] [bdaddr]\n");
1185 	printf("\thciattach -l\n");
1186 }
1187 
main(int argc,char * argv[])1188 int main(int argc, char *argv[])
1189 {
1190 	struct uart_t *u = NULL;
1191 	int detach, printpid, raw, opt, i, n, ld, err;
1192 	int to = 10;
1193 	int init_speed = 0;
1194 	int send_break = 0;
1195 	pid_t pid;
1196 	struct sigaction sa;
1197 	struct pollfd p;
1198 	sigset_t sigs;
1199 	char dev[PATH_MAX];
1200 
1201 	detach = 1;
1202 	printpid = 0;
1203 	raw = 0;
1204 
1205 	while ((opt=getopt(argc, argv, "bnpt:s:lr")) != EOF) {
1206 		switch(opt) {
1207 		case 'b':
1208 			send_break = 1;
1209 			break;
1210 
1211 		case 'n':
1212 			detach = 0;
1213 			break;
1214 
1215 		case 'p':
1216 			printpid = 1;
1217 			break;
1218 
1219 		case 't':
1220 			to = atoi(optarg);
1221 			break;
1222 
1223 		case 's':
1224 			init_speed = atoi(optarg);
1225 			break;
1226 
1227 		case 'l':
1228 			for (i = 0; uart[i].type; i++) {
1229 				printf("%-10s0x%04x,0x%04x\n", uart[i].type,
1230 							uart[i].m_id, uart[i].p_id);
1231 			}
1232 			exit(0);
1233 
1234 		case 'r':
1235 			raw = 1;
1236 			break;
1237 
1238 		default:
1239 			usage();
1240 			exit(1);
1241 		}
1242 	}
1243 
1244 	n = argc - optind;
1245 	if (n < 2) {
1246 		usage();
1247 		exit(1);
1248 	}
1249 
1250 	for (n = 0; optind < argc; n++, optind++) {
1251 		char *opt;
1252 
1253 		opt = argv[optind];
1254 
1255 		switch(n) {
1256 		case 0:
1257 			dev[0] = 0;
1258 			if (!strchr(opt, '/'))
1259 				strcpy(dev, "/dev/");
1260 			strcat(dev, opt);
1261 			break;
1262 
1263 		case 1:
1264 			if (strchr(argv[optind], ',')) {
1265 				int m_id, p_id;
1266 				sscanf(argv[optind], "%x,%x", &m_id, &p_id);
1267 				u = get_by_id(m_id, p_id);
1268 			} else {
1269 				u = get_by_type(opt);
1270 			}
1271 
1272 			if (!u) {
1273 				fprintf(stderr, "Unknown device type or id\n");
1274 				exit(1);
1275 			}
1276 
1277 			break;
1278 
1279 		case 2:
1280 			u->speed = atoi(argv[optind]);
1281 			break;
1282 
1283 		case 3:
1284 			if (!strcmp("flow", argv[optind]))
1285 				u->flags |=  FLOW_CTL;
1286 			else
1287 				u->flags &= ~FLOW_CTL;
1288 			break;
1289 
1290 		case 4:
1291 			u->bdaddr = argv[optind];
1292 			break;
1293 		}
1294 	}
1295 
1296 	if (!u) {
1297 		fprintf(stderr, "Unknown device type or id\n");
1298 		exit(1);
1299 	}
1300 
1301 	/* If user specified a initial speed, use that instead of
1302 	   the hardware's default */
1303 	if (init_speed)
1304 		u->init_speed = init_speed;
1305 
1306 	memset(&sa, 0, sizeof(sa));
1307 	sa.sa_flags   = SA_NOCLDSTOP;
1308 	sa.sa_handler = sig_alarm;
1309 	sigaction(SIGALRM, &sa, NULL);
1310 
1311 	/* 10 seconds should be enough for initialization */
1312 	alarm(to);
1313 	bcsp_max_retries = to;
1314 
1315 	n = init_uart(dev, u, send_break, raw);
1316 	if (n < 0) {
1317 		perror("Can't initialize device");
1318 		exit(1);
1319 	}
1320 
1321 	printf("Device setup complete\n");
1322 
1323 	alarm(0);
1324 
1325 	memset(&sa, 0, sizeof(sa));
1326 	sa.sa_flags   = SA_NOCLDSTOP;
1327 	sa.sa_handler = SIG_IGN;
1328 	sigaction(SIGCHLD, &sa, NULL);
1329 	sigaction(SIGPIPE, &sa, NULL);
1330 
1331 	sa.sa_handler = sig_term;
1332 	sigaction(SIGTERM, &sa, NULL);
1333 	sigaction(SIGINT,  &sa, NULL);
1334 
1335 	sa.sa_handler = sig_hup;
1336 	sigaction(SIGHUP, &sa, NULL);
1337 
1338 	if (detach) {
1339 		if ((pid = fork())) {
1340 			if (printpid)
1341 				printf("%d\n", pid);
1342 			return 0;
1343 		}
1344 
1345 		for (i = 0; i < 20; i++)
1346 			if (i != n)
1347 				close(i);
1348 	}
1349 
1350 	p.fd = n;
1351 	p.events = POLLERR | POLLHUP;
1352 
1353 	sigfillset(&sigs);
1354 	sigdelset(&sigs, SIGCHLD);
1355 	sigdelset(&sigs, SIGPIPE);
1356 	sigdelset(&sigs, SIGTERM);
1357 	sigdelset(&sigs, SIGINT);
1358 	sigdelset(&sigs, SIGHUP);
1359 
1360 	while (!__io_canceled) {
1361 		p.revents = 0;
1362 		err = ppoll(&p, 1, NULL, &sigs);
1363 		if (err < 0 && errno == EINTR)
1364 			continue;
1365 		if (err)
1366 			break;
1367 	}
1368 
1369 	/* Restore TTY line discipline */
1370 	ld = N_TTY;
1371 	if (ioctl(n, TIOCSETD, &ld) < 0) {
1372 		perror("Can't restore line discipline");
1373 		exit(1);
1374 	}
1375 
1376 	return 0;
1377 }
1378