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