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