2002-03-09 05:12:35 +08:00
|
|
|
/*
|
2004-04-28 18:39:47 +08:00
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2001 Qualcomm Incorporated
|
|
|
|
* Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
|
2007-01-14 01:48:12 +08:00
|
|
|
* Copyright (C) 2002-2007 Marcel Holtmann <marcel@holtmann.org>
|
2004-04-28 18:39:47 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
2005-10-30 06:36:31 +08:00
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2004-04-28 18:39:47 +08:00
|
|
|
*
|
2005-10-30 06:36:31 +08:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2004-04-28 18:39:47 +08:00
|
|
|
*
|
2005-10-30 06:36:31 +08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2004-04-28 18:39:47 +08:00
|
|
|
*
|
2002-03-09 05:12:35 +08:00
|
|
|
*/
|
|
|
|
|
2004-04-03 14:04:35 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
#include <stdio.h>
|
2005-01-12 02:46:46 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2002-03-09 05:12:35 +08:00
|
|
|
#include <unistd.h>
|
2005-01-12 02:46:46 +08:00
|
|
|
#include <stdlib.h>
|
2002-03-09 05:12:35 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <termios.h>
|
2005-01-12 02:46:46 +08:00
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
2006-03-21 06:29:03 +08:00
|
|
|
#include <sys/poll.h>
|
2006-06-27 15:33:36 +08:00
|
|
|
#include <sys/param.h>
|
2002-03-09 05:12:35 +08:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2002-08-20 12:39:54 +08:00
|
|
|
#include <bluetooth/bluetooth.h>
|
|
|
|
#include <bluetooth/hci.h>
|
|
|
|
#include <bluetooth/hci_lib.h>
|
2005-06-22 00:49:26 +08:00
|
|
|
|
|
|
|
#ifndef N_HCI
|
|
|
|
#define N_HCI 15
|
|
|
|
#endif
|
|
|
|
|
2007-05-09 17:34:21 +08:00
|
|
|
#define HCIUARTSETPROTO _IOW('U', 200, int)
|
|
|
|
#define HCIUARTGETPROTO _IOR('U', 201, int)
|
|
|
|
#define HCIUARTGETDEVICE _IOR('U', 202, int)
|
2005-06-22 00:49:26 +08:00
|
|
|
|
|
|
|
#define HCI_UART_H4 0
|
|
|
|
#define HCI_UART_BCSP 1
|
|
|
|
#define HCI_UART_3WIRE 2
|
|
|
|
#define HCI_UART_H4DS 3
|
2002-03-09 05:12:35 +08:00
|
|
|
|
|
|
|
struct uart_t {
|
|
|
|
char *type;
|
|
|
|
int m_id;
|
|
|
|
int p_id;
|
|
|
|
int proto;
|
2003-04-08 08:12:02 +08:00
|
|
|
int init_speed;
|
2002-03-09 05:12:35 +08:00
|
|
|
int speed;
|
|
|
|
int flags;
|
2007-04-26 07:09:02 +08:00
|
|
|
char *bdaddr;
|
2002-03-09 05:12:35 +08:00
|
|
|
int (*init) (int fd, struct uart_t *u, struct termios *ti);
|
|
|
|
};
|
|
|
|
|
|
|
|
#define FLOW_CTL 0x0001
|
|
|
|
|
2006-03-21 06:29:03 +08:00
|
|
|
static volatile sig_atomic_t __io_canceled = 0;
|
|
|
|
|
|
|
|
static void sig_hup(int sig)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_term(int sig)
|
|
|
|
{
|
|
|
|
__io_canceled = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sig_alarm(int sig)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Initialization timed out.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
static int uart_speed(int s)
|
|
|
|
{
|
|
|
|
switch (s) {
|
|
|
|
case 9600:
|
|
|
|
return B9600;
|
|
|
|
case 19200:
|
|
|
|
return B19200;
|
|
|
|
case 38400:
|
|
|
|
return B38400;
|
|
|
|
case 57600:
|
|
|
|
return B57600;
|
|
|
|
case 115200:
|
|
|
|
return B115200;
|
|
|
|
case 230400:
|
|
|
|
return B230400;
|
|
|
|
case 460800:
|
|
|
|
return B460800;
|
2004-09-20 23:51:15 +08:00
|
|
|
case 500000:
|
|
|
|
return B500000;
|
|
|
|
case 576000:
|
|
|
|
return B576000;
|
2002-03-09 05:12:35 +08:00
|
|
|
case 921600:
|
|
|
|
return B921600;
|
2002-09-10 00:27:39 +08:00
|
|
|
case 1000000:
|
|
|
|
return B1000000;
|
2003-04-05 22:13:04 +08:00
|
|
|
case 1152000:
|
|
|
|
return B1152000;
|
2004-09-20 23:51:15 +08:00
|
|
|
case 1500000:
|
|
|
|
return B1500000;
|
2002-03-09 05:12:35 +08:00
|
|
|
default:
|
|
|
|
return B57600;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_speed(int fd, struct termios *ti, int speed)
|
|
|
|
{
|
|
|
|
cfsetospeed(ti, uart_speed(speed));
|
|
|
|
return tcsetattr(fd, TCSANOW, ti);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read an HCI event from the given file descriptor.
|
|
|
|
*/
|
|
|
|
static int read_hci_event(int fd, unsigned char* buf, int size)
|
|
|
|
{
|
|
|
|
int remain, r;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (size <= 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* The first byte identifies the packet type. For HCI event packets, it
|
|
|
|
* should be 0x04, so we read until we get to the 0x04. */
|
|
|
|
while (1) {
|
|
|
|
r = read(fd, buf, 1);
|
|
|
|
if (r <= 0)
|
|
|
|
return -1;
|
|
|
|
if (buf[0] == 0x04)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
|
|
|
|
/* The next two bytes are the event code and parameter total length. */
|
|
|
|
while (count < 3) {
|
|
|
|
r = read(fd, buf + count, 3 - count);
|
|
|
|
if (r <= 0)
|
|
|
|
return -1;
|
|
|
|
count += r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we read the parameters. */
|
|
|
|
if (buf[2] < (size - 3))
|
|
|
|
remain = buf[2];
|
|
|
|
else
|
|
|
|
remain = size - 3;
|
|
|
|
|
|
|
|
while ((count - 3) < remain) {
|
|
|
|
r = read(fd, buf + count, remain - (count - 3));
|
|
|
|
if (r <= 0)
|
|
|
|
return -1;
|
|
|
|
count += r;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ericsson specific initialization
|
|
|
|
*/
|
|
|
|
static int ericsson(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
|
|
|
struct timespec tm = {0, 50000};
|
2003-11-18 16:16:21 +08:00
|
|
|
char cmd[5];
|
2002-03-09 05:12:35 +08:00
|
|
|
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x09;
|
|
|
|
cmd[2] = 0xfc;
|
|
|
|
cmd[3] = 0x01;
|
|
|
|
|
|
|
|
switch (u->speed) {
|
|
|
|
case 57600:
|
|
|
|
cmd[4] = 0x03;
|
|
|
|
break;
|
|
|
|
case 115200:
|
|
|
|
cmd[4] = 0x02;
|
|
|
|
break;
|
|
|
|
case 230400:
|
|
|
|
cmd[4] = 0x01;
|
|
|
|
break;
|
|
|
|
case 460800:
|
|
|
|
cmd[4] = 0x00;
|
|
|
|
break;
|
2003-03-20 05:17:46 +08:00
|
|
|
case 921600:
|
|
|
|
cmd[4] = 0x20;
|
|
|
|
break;
|
2002-03-09 05:12:35 +08:00
|
|
|
default:
|
|
|
|
cmd[4] = 0x03;
|
|
|
|
u->speed = 57600;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send initialization command */
|
|
|
|
if (write(fd, cmd, 5) != 5) {
|
|
|
|
perror("Failed to write init command");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
nanosleep(&tm, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Digianswer specific initialization
|
|
|
|
*/
|
|
|
|
static int digi(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
|
|
|
struct timespec tm = {0, 50000};
|
2003-11-18 16:16:21 +08:00
|
|
|
char cmd[5];
|
2002-03-09 05:12:35 +08:00
|
|
|
|
|
|
|
/* DigiAnswer set baud rate command */
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x07;
|
|
|
|
cmd[2] = 0xfc;
|
|
|
|
cmd[3] = 0x01;
|
|
|
|
|
|
|
|
switch (u->speed) {
|
|
|
|
case 57600:
|
|
|
|
cmd[4] = 0x08;
|
|
|
|
break;
|
|
|
|
case 115200:
|
|
|
|
cmd[4] = 0x09;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmd[4] = 0x09;
|
|
|
|
u->speed = 115200;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send initialization command */
|
|
|
|
if (write(fd, cmd, 5) != 5) {
|
|
|
|
perror("Failed to write init command");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
nanosleep(&tm, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-12 21:46:00 +08:00
|
|
|
static int texas(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
|
|
|
struct timespec tm = {0, 50000};
|
2003-11-18 16:16:21 +08:00
|
|
|
char cmd[4];
|
2003-02-12 21:46:00 +08:00
|
|
|
unsigned char resp[100]; /* Response */
|
|
|
|
int n;
|
|
|
|
|
|
|
|
memset(resp,'\0', 100);
|
|
|
|
|
|
|
|
/* It is possible to get software version with manufacturer specific
|
|
|
|
HCI command HCI_VS_TI_Version_Number. But the only thing you get more
|
|
|
|
is if this is point-to-point or point-to-multipoint module */
|
|
|
|
|
|
|
|
/* Get Manufacturer and LMP version */
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x01;
|
|
|
|
cmd[2] = 0x10;
|
2003-11-18 16:16:21 +08:00
|
|
|
cmd[3] = 0x00;
|
2003-02-12 21:46:00 +08:00
|
|
|
|
|
|
|
do {
|
|
|
|
n = write(fd, cmd, 4);
|
|
|
|
if (n < 0) {
|
|
|
|
perror("Failed to write init command (READ_LOCAL_VERSION_INFORMATION)");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (n < 4) {
|
|
|
|
fprintf(stderr, "Wanted to write 4 bytes, could only write %d. Stop\n", n);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read reply. */
|
|
|
|
if (read_hci_event(fd, resp, 100) < 0) {
|
|
|
|
perror("Failed to read init response (READ_LOCAL_VERSION_INFORMATION)");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for command complete event for our Opcode */
|
|
|
|
} while (resp[4] != cmd[1] && resp[5] != cmd[2]);
|
|
|
|
|
|
|
|
/* Verify manufacturer */
|
|
|
|
if ((resp[11] & 0xFF) != 0x0d)
|
|
|
|
fprintf(stderr,"WARNING : module's manufacturer is not Texas Instrument\n");
|
|
|
|
|
|
|
|
/* Print LMP version */
|
|
|
|
fprintf(stderr, "Texas module LMP version : 0x%02x\n", resp[10] & 0xFF);
|
|
|
|
|
|
|
|
/* Print LMP subversion */
|
|
|
|
fprintf(stderr, "Texas module LMP sub-version : 0x%02x%02x\n", resp[14] & 0xFF, resp[13] & 0xFF);
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2003-02-12 21:46:00 +08:00
|
|
|
nanosleep(&tm, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
static int read_check(int fd, void *buf, int count)
|
|
|
|
{
|
|
|
|
int res;
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2003-02-12 21:46:00 +08:00
|
|
|
do {
|
2002-09-26 13:13:23 +08:00
|
|
|
res = read(fd, buf, count);
|
|
|
|
if (res != -1) {
|
2003-02-12 21:46:00 +08:00
|
|
|
buf += res;
|
|
|
|
count -= res;
|
2002-09-26 13:13:23 +08:00
|
|
|
}
|
|
|
|
} while (count && (errno == 0 || errno == EINTR));
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
if (count)
|
|
|
|
return -1;
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-05 06:13:31 +08:00
|
|
|
/*
|
|
|
|
* BCSP specific initialization
|
|
|
|
*/
|
2002-09-26 13:13:23 +08:00
|
|
|
int serial_fd;
|
|
|
|
|
|
|
|
static void bcsp_tshy_sig_alarm(int sig)
|
|
|
|
{
|
|
|
|
static int retries=0;
|
|
|
|
unsigned char bcsp_sync_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xda,0xdc,0xed,0xed,0xc0};
|
2006-07-27 10:08:02 +08:00
|
|
|
int len;
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
if (retries < 10) {
|
|
|
|
retries++;
|
2006-07-27 10:08:02 +08:00
|
|
|
len = write(serial_fd, &bcsp_sync_pkt, 10);
|
2002-09-26 13:13:23 +08:00
|
|
|
alarm(1);
|
|
|
|
return;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2003-03-17 17:06:19 +08:00
|
|
|
tcflush(serial_fd, TCIOFLUSH);
|
2002-09-26 13:13:23 +08:00
|
|
|
fprintf(stderr, "BCSP initialization timed out\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bcsp_tconf_sig_alarm(int sig)
|
|
|
|
{
|
|
|
|
static int retries=0;
|
|
|
|
unsigned char bcsp_conf_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xad,0xef,0xac,0xed,0xc0};
|
2006-07-27 10:08:02 +08:00
|
|
|
int len;
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
if (retries < 10){
|
|
|
|
retries++;
|
2006-07-27 10:08:02 +08:00
|
|
|
len = write(serial_fd, &bcsp_conf_pkt, 10);
|
2002-09-26 13:13:23 +08:00
|
|
|
alarm(1);
|
|
|
|
return;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2003-03-17 17:06:19 +08:00
|
|
|
tcflush(serial_fd, TCIOFLUSH);
|
2002-09-26 13:13:23 +08:00
|
|
|
fprintf(stderr, "BCSP initialization timed out\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-08-05 06:13:31 +08:00
|
|
|
static int bcsp(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
2002-09-10 00:27:39 +08:00
|
|
|
unsigned char byte, bcsph[4], bcspp[4],
|
|
|
|
bcsp_sync_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xac,0xaf,0xef,0xee,0xc0},
|
|
|
|
bcsp_conf_resp_pkt[10] = {0xc0,0x00,0x41,0x00,0xbe,0xde,0xad,0xd0,0xd0,0xc0},
|
2002-09-26 13:13:23 +08:00
|
|
|
bcspsync[4] = {0xda, 0xdc, 0xed, 0xed},
|
2002-09-10 00:27:39 +08:00
|
|
|
bcspsyncresp[4] = {0xac,0xaf,0xef,0xee},
|
2002-09-26 13:13:23 +08:00
|
|
|
bcspconf[4] = {0xad,0xef,0xac,0xed},
|
2002-09-10 00:27:39 +08:00
|
|
|
bcspconfresp[4] = {0xde,0xad,0xd0,0xd0};
|
2002-09-26 13:13:23 +08:00
|
|
|
struct sigaction sa;
|
2006-07-27 10:08:02 +08:00
|
|
|
int len;
|
2002-09-10 00:27:39 +08:00
|
|
|
|
|
|
|
if (set_speed(fd, ti, u->speed) < 0) {
|
|
|
|
perror("Can't set default baud rate");
|
|
|
|
return -1;
|
|
|
|
}
|
2002-08-05 06:13:31 +08:00
|
|
|
|
2002-09-10 00:27:39 +08:00
|
|
|
ti->c_cflag |= PARENB;
|
|
|
|
ti->c_cflag &= ~(PARODD);
|
2002-08-05 06:13:31 +08:00
|
|
|
|
|
|
|
if (tcsetattr(fd, TCSANOW, ti) < 0) {
|
|
|
|
perror("Can't set port settings");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
alarm(0);
|
|
|
|
|
|
|
|
serial_fd = fd;
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_flags = SA_NOCLDSTOP;
|
|
|
|
sa.sa_handler = bcsp_tshy_sig_alarm;
|
|
|
|
sigaction(SIGALRM, &sa, NULL);
|
|
|
|
|
2002-09-10 00:27:39 +08:00
|
|
|
/* State = shy */
|
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
bcsp_tshy_sig_alarm(0);
|
2002-09-10 00:27:39 +08:00
|
|
|
while (1) {
|
|
|
|
do {
|
2002-09-26 13:13:23 +08:00
|
|
|
if (read_check(fd, &byte, 1) == -1){
|
|
|
|
perror("Failed to read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} while (byte != 0xC0);
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-09-10 00:27:39 +08:00
|
|
|
do {
|
2002-09-26 13:13:23 +08:00
|
|
|
if ( read_check(fd, &bcsph[0], 1) == -1){
|
|
|
|
perror("Failed to read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} while (bcsph[0] == 0xC0);
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
if ( read_check(fd, &bcsph[1], 3) == -1){
|
|
|
|
perror("Failed to read");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-09-10 00:27:39 +08:00
|
|
|
if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
|
2002-09-26 13:13:23 +08:00
|
|
|
continue;
|
2002-09-10 00:27:39 +08:00
|
|
|
if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
|
2002-09-26 13:13:23 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (read_check(fd, &bcspp, 4) == -1){
|
|
|
|
perror("Failed to read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-09-10 00:27:39 +08:00
|
|
|
if (!memcmp(bcspp, bcspsync, 4)) {
|
2006-07-27 10:08:02 +08:00
|
|
|
len = write(fd, &bcsp_sync_resp_pkt,10);
|
2002-09-10 00:27:39 +08:00
|
|
|
} else if (!memcmp(bcspp, bcspsyncresp, 4))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* State = curious */
|
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
alarm(0);
|
|
|
|
sa.sa_handler = bcsp_tconf_sig_alarm;
|
|
|
|
sigaction(SIGALRM, &sa, NULL);
|
|
|
|
alarm(1);
|
|
|
|
|
2002-09-10 00:27:39 +08:00
|
|
|
while (1) {
|
|
|
|
do {
|
2002-09-26 13:13:23 +08:00
|
|
|
if (read_check(fd, &byte, 1) == -1){
|
|
|
|
perror("Failed to read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} while (byte != 0xC0);
|
2002-09-10 00:27:39 +08:00
|
|
|
|
|
|
|
do {
|
2002-09-26 13:13:23 +08:00
|
|
|
if (read_check(fd, &bcsph[0], 1) == -1){
|
2006-03-21 06:29:03 +08:00
|
|
|
perror("Failed to read");
|
|
|
|
return -1;
|
2002-09-26 13:13:23 +08:00
|
|
|
}
|
|
|
|
} while (bcsph[0] == 0xC0);
|
2002-09-10 00:27:39 +08:00
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
if (read_check(fd, &bcsph[1], 3) == -1){
|
|
|
|
perror("Failed to read");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-09-10 00:27:39 +08:00
|
|
|
if (((bcsph[0] + bcsph[1] + bcsph[2]) & 0xFF) != (unsigned char)~bcsph[3])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (bcsph[1] != 0x41 || bcsph[2] != 0x00)
|
|
|
|
continue;
|
|
|
|
|
2002-09-26 13:13:23 +08:00
|
|
|
if (read_check(fd, &bcspp, 4) == -1){
|
|
|
|
perror("Failed to read");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-09-10 00:27:39 +08:00
|
|
|
if (!memcmp(bcspp, bcspsync, 4))
|
2006-07-27 10:08:02 +08:00
|
|
|
len = write(fd, &bcsp_sync_resp_pkt, 10);
|
2002-09-10 00:27:39 +08:00
|
|
|
else if (!memcmp(bcspp, bcspconf, 4))
|
2006-07-27 10:08:02 +08:00
|
|
|
len = write(fd, &bcsp_conf_resp_pkt, 10);
|
2002-09-10 00:27:39 +08:00
|
|
|
else if (!memcmp(bcspp, bcspconfresp, 4))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* State = garrulous */
|
|
|
|
|
2002-08-05 06:13:31 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
/*
|
|
|
|
* CSR specific initialization
|
|
|
|
* Inspired strongly by code in OpenBT and experimentations with Brainboxes
|
|
|
|
* Pcmcia card.
|
|
|
|
* Jean Tourrilhes <jt@hpl.hp.com> - 14.11.01
|
|
|
|
*/
|
|
|
|
static int csr(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
|
|
|
struct timespec tm = {0, 10000000}; /* 10ms - be generous */
|
|
|
|
unsigned char cmd[30]; /* Command */
|
|
|
|
unsigned char resp[30]; /* Response */
|
|
|
|
int clen = 0; /* Command len */
|
|
|
|
static int csr_seq = 0; /* Sequence number of command */
|
|
|
|
int divisor;
|
|
|
|
|
|
|
|
/* It seems that if we set the CSR UART speed straight away, it
|
|
|
|
* won't work, the CSR UART gets into a state where we can't talk
|
|
|
|
* to it anymore.
|
|
|
|
* On the other hand, doing a read before setting the CSR speed
|
|
|
|
* seems to be ok.
|
|
|
|
* Therefore, the strategy is to read the build ID (useful for
|
|
|
|
* debugging) and only then set the CSR UART speed. Doing like
|
|
|
|
* this is more complex but at least it works ;-)
|
|
|
|
* The CSR UART control may be slow to wake up or something because
|
|
|
|
* every time I read its speed, its bogus...
|
|
|
|
* Jean II */
|
|
|
|
|
|
|
|
/* Try to read the build ID of the CSR chip */
|
|
|
|
clen = 5 + (5 + 6) * 2;
|
|
|
|
/* HCI header */
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x00; /* CSR command */
|
|
|
|
cmd[2] = 0xfc; /* MANUFACTURER_SPEC */
|
|
|
|
cmd[3] = 1 + (5 + 6) * 2; /* len */
|
|
|
|
/* CSR MSG header */
|
|
|
|
cmd[4] = 0xC2; /* first+last+channel=BCC */
|
|
|
|
/* CSR BCC header */
|
|
|
|
cmd[5] = 0x00; /* type = GET-REQ */
|
|
|
|
cmd[6] = 0x00; /* - msB */
|
|
|
|
cmd[7] = 5 + 4; /* len */
|
|
|
|
cmd[8] = 0x00; /* - msB */
|
|
|
|
cmd[9] = csr_seq & 0xFF;/* seq num */
|
|
|
|
cmd[10] = (csr_seq >> 8) & 0xFF; /* - msB */
|
|
|
|
csr_seq++;
|
|
|
|
cmd[11] = 0x19; /* var_id = CSR_CMD_BUILD_ID */
|
|
|
|
cmd[12] = 0x28; /* - msB */
|
|
|
|
cmd[13] = 0x00; /* status = STATUS_OK */
|
|
|
|
cmd[14] = 0x00; /* - msB */
|
|
|
|
/* CSR BCC payload */
|
|
|
|
memset(cmd + 15, 0, 6 * 2);
|
|
|
|
|
|
|
|
/* Send command */
|
|
|
|
do {
|
|
|
|
if (write(fd, cmd, clen) != clen) {
|
|
|
|
perror("Failed to write init command (GET_BUILD_ID)");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read reply. */
|
|
|
|
if (read_hci_event(fd, resp, 100) < 0) {
|
|
|
|
perror("Failed to read init response (GET_BUILD_ID)");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Event code 0xFF is for vendor-specific events, which is
|
|
|
|
* what we're looking for. */
|
|
|
|
} while (resp[1] != 0xFF);
|
|
|
|
|
|
|
|
#ifdef CSR_DEBUG
|
|
|
|
{
|
|
|
|
char temp[512];
|
|
|
|
int i;
|
|
|
|
for (i=0; i < rlen; i++)
|
|
|
|
sprintf(temp + (i*3), "-%02X", resp[i]);
|
|
|
|
fprintf(stderr, "Reading CSR build ID %d [%s]\n", rlen, temp + 1);
|
|
|
|
// In theory, it should look like :
|
|
|
|
// 04-FF-13-FF-01-00-09-00-00-00-19-28-00-00-73-00-00-00-00-00-00-00
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Display that to user */
|
|
|
|
fprintf(stderr, "CSR build ID 0x%02X-0x%02X\n",
|
|
|
|
resp[15] & 0xFF, resp[14] & 0xFF);
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
/* Try to read the current speed of the CSR chip */
|
|
|
|
clen = 5 + (5 + 4)*2;
|
|
|
|
/* -- HCI header */
|
|
|
|
cmd[3] = 1 + (5 + 4)*2; /* len */
|
|
|
|
/* -- CSR BCC header -- */
|
|
|
|
cmd[9] = csr_seq & 0xFF; /* seq num */
|
|
|
|
cmd[10] = (csr_seq >> 8) & 0xFF; /* - msB */
|
|
|
|
csr_seq++;
|
|
|
|
cmd[11] = 0x02; /* var_id = CONFIG_UART */
|
|
|
|
cmd[12] = 0x68; /* - msB */
|
|
|
|
|
|
|
|
#ifdef CSR_DEBUG
|
|
|
|
/* Send command */
|
|
|
|
do {
|
|
|
|
if (write(fd, cmd, clen) != clen) {
|
|
|
|
perror("Failed to write init command (GET_BUILD_ID)");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read reply. */
|
|
|
|
if (read_hci_event(fd, resp, 100) < 0) {
|
|
|
|
perror("Failed to read init response (GET_BUILD_ID)");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Event code 0xFF is for vendor-specific events, which is
|
|
|
|
* what we're looking for. */
|
|
|
|
} while (resp[1] != 0xFF);
|
|
|
|
|
|
|
|
{
|
|
|
|
char temp[512];
|
|
|
|
int i;
|
|
|
|
for (i=0; i < rlen; i++)
|
|
|
|
sprintf(temp + (i*3), "-%02X", resp[i]);
|
|
|
|
fprintf(stderr, "Reading CSR UART speed %d [%s]\n", rlen, temp+1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-04-05 22:13:04 +08:00
|
|
|
if (u->speed > 1500000) {
|
|
|
|
fprintf(stderr, "Speed %d too high. Remaining at %d baud\n",
|
2003-04-08 08:12:02 +08:00
|
|
|
u->speed, u->init_speed);
|
|
|
|
u->speed = u->init_speed;
|
2003-04-05 22:13:04 +08:00
|
|
|
} else if (u->speed != 57600 && uart_speed(u->speed) == B57600) {
|
|
|
|
/* Unknown speed. Why oh why can't we just pass an int to the kernel? */
|
|
|
|
fprintf(stderr, "Speed %d unrecognised. Remaining at %d baud\n",
|
2003-04-08 08:12:02 +08:00
|
|
|
u->speed, u->init_speed);
|
|
|
|
u->speed = u->init_speed;
|
2003-04-05 22:13:04 +08:00
|
|
|
}
|
2003-04-08 08:12:02 +08:00
|
|
|
if (u->speed == u->init_speed)
|
2003-04-05 22:13:04 +08:00
|
|
|
return 0;
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
/* Now, create the command that will set the UART speed */
|
|
|
|
/* CSR BCC header */
|
|
|
|
cmd[5] = 0x02; /* type = SET-REQ */
|
|
|
|
cmd[6] = 0x00; /* - msB */
|
|
|
|
cmd[9] = csr_seq & 0xFF; /* seq num */
|
|
|
|
cmd[10] = (csr_seq >> 8) & 0xFF;/* - msB */
|
|
|
|
csr_seq++;
|
|
|
|
|
2003-04-05 22:13:04 +08:00
|
|
|
divisor = (u->speed*64+7812)/15625;
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
/* No parity, one stop bit -> divisor |= 0x0000; */
|
|
|
|
cmd[15] = (divisor) & 0xFF; /* divider */
|
|
|
|
cmd[16] = (divisor >> 8) & 0xFF; /* - msB */
|
|
|
|
/* The rest of the payload will be 0x00 */
|
|
|
|
|
|
|
|
#ifdef CSR_DEBUG
|
|
|
|
{
|
|
|
|
char temp[512];
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < clen; i++)
|
|
|
|
sprintf(temp + (i*3), "-%02X", cmd[i]);
|
|
|
|
fprintf(stderr, "Writing CSR UART speed %d [%s]\n", clen, temp + 1);
|
|
|
|
// In theory, it should look like :
|
|
|
|
// 01-00-FC-13-C2-02-00-09-00-03-00-02-68-00-00-BF-0E-00-00-00-00-00-00
|
|
|
|
// 01-00-FC-13-C2-02-00-09-00-01-00-02-68-00-00-D8-01-00-00-00-00-00-00
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Send the command to set the CSR UART speed */
|
|
|
|
if (write(fd, cmd, clen) != clen) {
|
|
|
|
perror("Failed to write init command (SET_UART_SPEED)");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
nanosleep(&tm, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Silicon Wave specific initialization
|
2005-10-30 06:36:31 +08:00
|
|
|
* Thomas Moser <thomas.moser@tmoser.ch>
|
2002-03-09 05:12:35 +08:00
|
|
|
*/
|
|
|
|
static int swave(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
2005-12-07 17:04:58 +08:00
|
|
|
struct timespec tm = { 0, 500000 };
|
2004-05-12 21:47:19 +08:00
|
|
|
char cmd[10], rsp[100];
|
2002-03-09 05:12:35 +08:00
|
|
|
int r;
|
|
|
|
|
|
|
|
// Silicon Wave set baud rate command
|
|
|
|
// see HCI Vendor Specific Interface from Silicon Wave
|
|
|
|
// first send a "param access set" command to set the
|
|
|
|
// appropriate data fields in RAM. Then send a "HCI Reset
|
|
|
|
// Subcommand", e.g. "soft reset" to make the changes effective.
|
|
|
|
|
|
|
|
cmd[0] = HCI_COMMAND_PKT; // it's a command packet
|
|
|
|
cmd[1] = 0x0B; // OCF 0x0B = param access set
|
|
|
|
cmd[2] = 0xfc; // OGF bx111111 = vendor specific
|
|
|
|
cmd[3] = 0x06; // 6 bytes of data following
|
|
|
|
cmd[4] = 0x01; // param sub command
|
|
|
|
cmd[5] = 0x11; // tag 17 = 0x11 = HCI Transport Params
|
|
|
|
cmd[6] = 0x03; // length of the parameter following
|
|
|
|
cmd[7] = 0x01; // HCI Transport flow control enable
|
|
|
|
cmd[8] = 0x01; // HCI Transport Type = UART
|
|
|
|
|
|
|
|
switch (u->speed) {
|
|
|
|
case 19200:
|
|
|
|
cmd[9] = 0x03;
|
|
|
|
break;
|
|
|
|
case 38400:
|
|
|
|
cmd[9] = 0x02;
|
|
|
|
break;
|
|
|
|
case 57600:
|
|
|
|
cmd[9] = 0x01;
|
|
|
|
break;
|
|
|
|
case 115200:
|
|
|
|
cmd[9] = 0x00;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
u->speed = 115200;
|
|
|
|
cmd[9] = 0x00;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send initialization command */
|
2005-12-07 17:04:58 +08:00
|
|
|
if (write(fd, cmd, 10) != 10) {
|
2002-03-09 05:12:35 +08:00
|
|
|
perror("Failed to write init command");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should wait for a "GET Event" to confirm the success of
|
|
|
|
// the baud rate setting. Wait some time before reading. Better:
|
|
|
|
// read with timeout, parse data
|
|
|
|
// until correct answer, else error handling ... todo ...
|
|
|
|
|
|
|
|
nanosleep(&tm, NULL);
|
|
|
|
|
|
|
|
r = read(fd, rsp, sizeof(rsp));
|
|
|
|
if (r > 0) {
|
|
|
|
// guess it's okay, but we should parse the reply. But since
|
|
|
|
// I don't react on an error anyway ... todo
|
|
|
|
// Response packet format:
|
|
|
|
// 04 Event
|
|
|
|
// FF Vendor specific
|
|
|
|
// 07 Parameter length
|
|
|
|
// 0B Subcommand
|
|
|
|
// 01 Setevent
|
|
|
|
// 11 Tag specifying HCI Transport Layer Parameter
|
|
|
|
// 03 length
|
|
|
|
// 01 flow on
|
|
|
|
// 01 Hci Transport type = Uart
|
|
|
|
// xx Baud rate set (see above)
|
2006-03-21 06:29:03 +08:00
|
|
|
} else {
|
2002-03-09 05:12:35 +08:00
|
|
|
// ups, got error.
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-12-07 17:04:58 +08:00
|
|
|
// we probably got the reply. Now we must send the "soft reset"
|
|
|
|
// which is standard HCI RESET.
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
cmd[0] = HCI_COMMAND_PKT; // it's a command packet
|
2005-12-07 17:04:58 +08:00
|
|
|
cmd[1] = 0x03;
|
|
|
|
cmd[2] = 0x0c;
|
|
|
|
cmd[3] = 0x00;
|
|
|
|
|
|
|
|
/* Send reset command */
|
|
|
|
if (write(fd, cmd, 4) != 4) {
|
2002-03-09 05:12:35 +08:00
|
|
|
perror("Can't write Silicon Wave reset cmd.");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
nanosleep(&tm, NULL);
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
// now the uart baud rate on the silicon wave module is set and effective.
|
|
|
|
// change our own baud rate as well. Then there is a reset event comming in
|
|
|
|
// on the *new* baud rate. This is *undocumented*! The packet looks like this:
|
|
|
|
// 04 FF 01 0B (which would make that a confirmation of 0x0B = "Param
|
|
|
|
// subcommand class". So: change to new baud rate, read with timeout, parse
|
|
|
|
// data, error handling. BTW: all param access in Silicon Wave is done this way.
|
|
|
|
// Maybe this code would belong in a seperate file, or at least code reuse...
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-11-18 16:16:21 +08:00
|
|
|
/*
|
|
|
|
* ST Microelectronics specific initialization
|
|
|
|
* Marcel Holtmann <marcel@holtmann.org>
|
|
|
|
*/
|
|
|
|
static int st(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
|
|
|
struct timespec tm = {0, 50000};
|
|
|
|
char cmd[5];
|
|
|
|
|
|
|
|
/* ST Microelectronics set baud rate command */
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x46; // OCF = Hci_Cmd_ST_Set_Uart_Baud_Rate
|
|
|
|
cmd[2] = 0xfc; // OGF = Vendor specific
|
|
|
|
cmd[3] = 0x01;
|
|
|
|
|
|
|
|
switch (u->speed) {
|
|
|
|
case 9600:
|
|
|
|
cmd[4] = 0x09;
|
|
|
|
break;
|
|
|
|
case 19200:
|
|
|
|
cmd[4] = 0x0b;
|
|
|
|
break;
|
|
|
|
case 38400:
|
|
|
|
cmd[4] = 0x0d;
|
|
|
|
break;
|
|
|
|
case 57600:
|
|
|
|
cmd[4] = 0x0e;
|
|
|
|
break;
|
|
|
|
case 115200:
|
|
|
|
cmd[4] = 0x10;
|
|
|
|
break;
|
|
|
|
case 230400:
|
|
|
|
cmd[4] = 0x12;
|
|
|
|
break;
|
|
|
|
case 460800:
|
|
|
|
cmd[4] = 0x13;
|
|
|
|
break;
|
|
|
|
case 921600:
|
|
|
|
cmd[4] = 0x14;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmd[4] = 0x10;
|
2004-05-03 21:25:14 +08:00
|
|
|
u->speed = 115200;
|
2003-11-18 16:16:21 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send initialization command */
|
|
|
|
if (write(fd, cmd, 5) != 5) {
|
|
|
|
perror("Failed to write init command");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2003-11-18 16:16:21 +08:00
|
|
|
nanosleep(&tm, NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-17 20:31:42 +08:00
|
|
|
extern int stlc2500_init(int fd, bdaddr_t *bdaddr);
|
2006-04-17 05:09:01 +08:00
|
|
|
|
|
|
|
static int stlc2500(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
2007-01-17 20:31:42 +08:00
|
|
|
bdaddr_t bdaddr;
|
2007-04-26 07:09:02 +08:00
|
|
|
char cmd[5];
|
|
|
|
unsigned char resp[10];
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* STLC2500 Set Baud Rate stuff */
|
|
|
|
/* We should set the baud rate first, so the firmware download */
|
|
|
|
/* goes much faster */
|
|
|
|
|
|
|
|
/* STLC2500 Seems to support the Ericsson set baud rate stuff */
|
|
|
|
/* It should also support the ST Set Baud Rate command */
|
|
|
|
/* (as in st() function above, but those commands never succeed */
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x09;
|
|
|
|
cmd[2] = 0xfc;
|
|
|
|
cmd[3] = 0x01;
|
|
|
|
|
|
|
|
switch (u->speed) {
|
|
|
|
case 57600:
|
|
|
|
cmd[4] = 0x03;
|
|
|
|
break;
|
|
|
|
case 115200:
|
|
|
|
cmd[4] = 0x02;
|
|
|
|
break;
|
|
|
|
case 230400:
|
|
|
|
cmd[4] = 0x01;
|
|
|
|
break;
|
|
|
|
case 460800:
|
|
|
|
cmd[4] = 0x00;
|
|
|
|
break;
|
|
|
|
case 921600:
|
|
|
|
cmd[4] = 0x20;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cmd[4] = 0x02;
|
|
|
|
u->speed = 115200;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STLC2500_DEBUG
|
|
|
|
fprintf(stderr, "Sending Baud Rate %02x\n", cmd[4]);
|
|
|
|
#endif
|
|
|
|
/* Send initialization command */
|
|
|
|
if (write(fd, cmd, 5) != 5) {
|
|
|
|
perror("Failed to write init command");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-01-17 20:31:42 +08:00
|
|
|
|
2007-04-26 07:09:02 +08:00
|
|
|
/* Need to wait here to give a chance for the device to set baud */
|
|
|
|
/* But no more than 0.5 seconds */
|
|
|
|
usleep(200000);
|
2007-01-17 20:31:42 +08:00
|
|
|
|
2007-04-26 07:09:02 +08:00
|
|
|
#ifdef STLC2500_DEBUG
|
|
|
|
fprintf(stderr, "Setting speed\n");
|
|
|
|
#endif
|
|
|
|
if (set_speed(fd, ti, u->speed) < 0) {
|
|
|
|
perror("Can't set baud rate");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STLC2500_DEBUG
|
|
|
|
fprintf(stderr, "Speed set...\n");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Read reply */
|
|
|
|
if ((n = read_hci_event(fd, resp, 10)) < 0) {
|
|
|
|
fprintf(stderr, "Failed to set baud rate on chip\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STLC2500_DEBUG
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
fprintf(stderr, "resp[%d] = %02x\n", i, resp[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
str2ba(u->bdaddr, &bdaddr);
|
2007-01-17 20:31:42 +08:00
|
|
|
return stlc2500_init(fd, &bdaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern int bgb2xx_init(int fd, bdaddr_t *bdaddr);
|
|
|
|
|
|
|
|
static int bgb2xx(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
|
|
|
bdaddr_t bdaddr;
|
|
|
|
|
2007-04-26 07:09:02 +08:00
|
|
|
str2ba(u->bdaddr, &bdaddr);
|
2007-01-17 20:31:42 +08:00
|
|
|
|
|
|
|
return bgb2xx_init(fd, &bdaddr);
|
2006-04-17 05:09:01 +08:00
|
|
|
}
|
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
/*
|
|
|
|
* Broadcom specific initialization
|
|
|
|
* Extracted from Jungo openrg
|
|
|
|
*/
|
|
|
|
static int bcm2035(int fd, struct uart_t *u, struct termios *ti)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
unsigned char cmd[30], resp[30];
|
|
|
|
|
|
|
|
/* Reset the BT Chip */
|
|
|
|
memset(cmd, 0, sizeof(cmd));
|
|
|
|
memset(resp, 0, sizeof(resp));
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x03;
|
|
|
|
cmd[2] = 0x0c;
|
|
|
|
cmd[3] = 0x00;
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
/* Send command */
|
|
|
|
if (write(fd, cmd, 4) != 4) {
|
|
|
|
fprintf(stderr, "Failed to write reset command\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
/* Read reply */
|
|
|
|
if ((n = read_hci_event(fd, resp, 4)) < 0) {
|
|
|
|
fprintf(stderr, "Failed to reset chip\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-06-22 07:07:13 +08:00
|
|
|
if (u->bdaddr != NULL) {
|
|
|
|
/* Set BD_ADDR */
|
|
|
|
memset(cmd, 0, sizeof(cmd));
|
|
|
|
memset(resp, 0, sizeof(resp));
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x01;
|
|
|
|
cmd[2] = 0xfc;
|
|
|
|
cmd[3] = 0x06;
|
|
|
|
str2ba(u->bdaddr, (bdaddr_t *) (cmd + 4));
|
|
|
|
|
|
|
|
/* Send command */
|
|
|
|
if (write(fd, cmd, 10) != 10) {
|
|
|
|
fprintf(stderr, "Failed to write BD_ADDR command\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read reply */
|
|
|
|
if ((n = read_hci_event(fd, resp, 10)) < 0) {
|
|
|
|
fprintf(stderr, "Failed to set BD_ADDR\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
/* Read the local version info */
|
|
|
|
memset(cmd, 0, sizeof(cmd));
|
|
|
|
memset(resp, 0, sizeof(resp));
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x01;
|
|
|
|
cmd[2] = 0x10;
|
|
|
|
cmd[3] = 0x00;
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
/* Send command */
|
|
|
|
if (write(fd, cmd, 4) != 4) {
|
|
|
|
fprintf(stderr, "Failed to write \"read local version\" "
|
|
|
|
"command\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
/* Read reply */
|
|
|
|
if ((n = read_hci_event(fd, resp, 4)) < 0) {
|
|
|
|
fprintf(stderr, "Failed to read local version\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the local supported commands info */
|
|
|
|
memset(cmd, 0, sizeof(cmd));
|
|
|
|
memset(resp, 0, sizeof(resp));
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x02;
|
|
|
|
cmd[2] = 0x10;
|
|
|
|
cmd[3] = 0x00;
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
/* Send command */
|
|
|
|
if (write(fd, cmd, 4) != 4) {
|
|
|
|
fprintf(stderr, "Failed to write \"read local supported "
|
2007-06-22 07:07:13 +08:00
|
|
|
"commands\" command\n");
|
2006-01-21 09:45:01 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
/* Read reply */
|
|
|
|
if ((n = read_hci_event(fd, resp, 4)) < 0) {
|
|
|
|
fprintf(stderr, "Failed to read local supported commands\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the baud rate */
|
|
|
|
memset(cmd, 0, sizeof(cmd));
|
|
|
|
memset(resp, 0, sizeof(resp));
|
|
|
|
cmd[0] = HCI_COMMAND_PKT;
|
|
|
|
cmd[1] = 0x18;
|
|
|
|
cmd[2] = 0xfc;
|
|
|
|
cmd[3] = 0x02;
|
|
|
|
switch (u->speed) {
|
|
|
|
case 57600:
|
|
|
|
cmd[4] = 0x00;
|
|
|
|
cmd[5] = 0xe6;
|
|
|
|
break;
|
|
|
|
case 230400:
|
|
|
|
cmd[4] = 0x22;
|
|
|
|
cmd[5] = 0xfa;
|
|
|
|
break;
|
|
|
|
case 460800:
|
2007-06-22 07:07:13 +08:00
|
|
|
cmd[4] = 0x22;
|
2006-01-21 09:45:01 +08:00
|
|
|
cmd[5] = 0xfd;
|
|
|
|
break;
|
|
|
|
case 921600:
|
2007-06-22 07:07:13 +08:00
|
|
|
cmd[4] = 0x55;
|
2006-01-21 09:45:01 +08:00
|
|
|
cmd[5] = 0xff;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Default is 115200 */
|
|
|
|
cmd[4] = 0x00;
|
|
|
|
cmd[5] = 0xf3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "Baud rate parameters: DHBR=0x%2x,DLBR=0x%2x\n",
|
|
|
|
cmd[4], cmd[5]);
|
|
|
|
|
|
|
|
/* Send command */
|
|
|
|
if (write(fd, cmd, 6) != 6) {
|
|
|
|
fprintf(stderr, "Failed to write \"set baud rate\" command\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
if ((n = read_hci_event(fd, resp, 6)) < 0) {
|
|
|
|
fprintf(stderr, "Failed to set baud rate\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
struct uart_t uart[] = {
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "any", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200, FLOW_CTL, NULL, NULL },
|
|
|
|
{ "ericsson", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200, FLOW_CTL, NULL, ericsson },
|
|
|
|
{ "digi", 0x0000, 0x0000, HCI_UART_H4, 9600, 115200, FLOW_CTL, NULL, digi },
|
|
|
|
{ "texas", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200, FLOW_CTL, NULL, texas },
|
2002-08-05 06:13:31 +08:00
|
|
|
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "bcsp", 0x0000, 0x0000, HCI_UART_BCSP, 115200, 115200, 0, NULL, bcsp },
|
2002-03-09 05:12:35 +08:00
|
|
|
|
|
|
|
/* Xircom PCMCIA cards: Credit Card Adapter and Real Port Adapter */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "xircom", 0x0105, 0x080a, HCI_UART_H4, 115200, 115200, FLOW_CTL, NULL, NULL },
|
2002-03-09 05:12:35 +08:00
|
|
|
|
|
|
|
/* CSR Casira serial adapter or BrainBoxes serial dongle (BL642) */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "csr", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200, FLOW_CTL, NULL, csr },
|
2002-03-09 05:12:35 +08:00
|
|
|
|
|
|
|
/* BrainBoxes PCMCIA card (BL620) */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "bboxes", 0x0160, 0x0002, HCI_UART_H4, 115200, 460800, FLOW_CTL, NULL, csr },
|
2002-03-09 05:12:35 +08:00
|
|
|
|
|
|
|
/* Silicon Wave kits */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "swave", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200, FLOW_CTL, NULL, swave },
|
2002-03-09 05:12:35 +08:00
|
|
|
|
2003-11-18 16:16:21 +08:00
|
|
|
/* ST Microelectronics minikits based on STLC2410/STLC2415 */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "st", 0x0000, 0x0000, HCI_UART_H4, 57600, 115200, FLOW_CTL, NULL, st },
|
2003-11-18 16:16:21 +08:00
|
|
|
|
2006-04-17 05:09:01 +08:00
|
|
|
/* ST Microelectronics minikits based on STLC2500 */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "stlc2500", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200, FLOW_CTL, "00:80:E1:00:AB:BA", stlc2500 },
|
2006-04-17 05:09:01 +08:00
|
|
|
|
2007-01-17 20:31:42 +08:00
|
|
|
/* Philips generic Ericsson IP core based */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "philips", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200, FLOW_CTL, NULL, NULL },
|
2007-01-17 20:31:42 +08:00
|
|
|
|
|
|
|
/* Philips BGB2xx Module */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "bgb2xx", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200, FLOW_CTL, "BD:B2:10:00:AB:BA", bgb2xx },
|
2007-01-17 20:31:42 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
/* Sphinx Electronics PICO Card */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "picocard", 0x025e, 0x1000, HCI_UART_H4, 115200, 115200, FLOW_CTL, NULL, NULL },
|
2002-03-09 05:12:35 +08:00
|
|
|
|
2002-03-19 03:36:42 +08:00
|
|
|
/* Inventel BlueBird Module */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "inventel", 0x0000, 0x0000, HCI_UART_H4, 115200, 115200, FLOW_CTL, NULL, NULL },
|
2002-03-19 03:36:42 +08:00
|
|
|
|
2002-03-19 13:37:10 +08:00
|
|
|
/* COM One Platinium Bluetooth PC Card */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "comone", 0xffff, 0x0101, HCI_UART_BCSP, 115200, 115200, 0, NULL, bcsp },
|
2002-03-19 13:37:10 +08:00
|
|
|
|
2003-05-30 00:41:48 +08:00
|
|
|
/* TDK Bluetooth PC Card and IBM Bluetooth PC Card II */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "tdk", 0x0105, 0x4254, HCI_UART_BCSP, 115200, 115200, 0, NULL, bcsp },
|
2003-05-30 00:41:48 +08:00
|
|
|
|
|
|
|
/* Socket Bluetooth CF Card (Rev G) */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "socket", 0x0104, 0x0096, HCI_UART_BCSP, 230400, 230400, 0, NULL, bcsp },
|
2003-05-30 00:41:48 +08:00
|
|
|
|
2003-08-27 17:55:43 +08:00
|
|
|
/* 3Com Bluetooth Card (Version 3.0) */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "3com", 0x0101, 0x0041, HCI_UART_H4, 115200, 115200, FLOW_CTL, NULL, csr },
|
2003-08-27 17:55:43 +08:00
|
|
|
|
2004-06-24 00:30:41 +08:00
|
|
|
/* AmbiCom BT2000C Bluetooth PC/CF Card */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "bt2000c", 0x022d, 0x2000, HCI_UART_H4, 57600, 460800, FLOW_CTL, NULL, csr },
|
2004-06-24 00:30:41 +08:00
|
|
|
|
2004-10-11 17:52:09 +08:00
|
|
|
/* Zoom Bluetooth PCMCIA Card */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "zoom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200, 0, NULL, bcsp },
|
2004-10-11 17:52:09 +08:00
|
|
|
|
2005-01-12 02:46:46 +08:00
|
|
|
/* Sitecom CN-504 PCMCIA Card */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "sitecom", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200, 0, NULL, bcsp },
|
2005-01-31 04:38:04 +08:00
|
|
|
|
|
|
|
/* Billionton PCBTC1 PCMCIA Card */
|
2007-04-26 07:09:02 +08:00
|
|
|
{ "billionton", 0x0279, 0x950b, HCI_UART_BCSP, 115200, 115200, 0, NULL, bcsp },
|
2005-01-31 04:38:04 +08:00
|
|
|
|
2006-01-21 09:45:01 +08:00
|
|
|
/* Broadcom BCM2035 */
|
2007-06-22 07:07:13 +08:00
|
|
|
{ "bcm2035", 0x0A5C, 0x2035, HCI_UART_H4, 115200, 460800, FLOW_CTL, NULL, bcm2035 },
|
2006-01-21 09:45:01 +08:00
|
|
|
|
2003-05-30 00:41:48 +08:00
|
|
|
{ NULL, 0 }
|
2002-03-09 05:12:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct uart_t * get_by_id(int m_id, int p_id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; uart[i].type; i++) {
|
|
|
|
if (uart[i].m_id == m_id && uart[i].p_id == p_id)
|
|
|
|
return &uart[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct uart_t * get_by_type(char *type)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; uart[i].type; i++) {
|
|
|
|
if (!strcmp(uart[i].type, type))
|
|
|
|
return &uart[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize UART driver */
|
2003-04-05 22:13:04 +08:00
|
|
|
int init_uart(char *dev, struct uart_t *u, int send_break)
|
2002-03-09 05:12:35 +08:00
|
|
|
{
|
|
|
|
struct termios ti;
|
2006-03-21 06:29:03 +08:00
|
|
|
int fd, i;
|
2002-03-09 05:12:35 +08:00
|
|
|
|
|
|
|
fd = open(dev, O_RDWR | O_NOCTTY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror("Can't open serial port");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tcflush(fd, TCIOFLUSH);
|
|
|
|
|
|
|
|
if (tcgetattr(fd, &ti) < 0) {
|
|
|
|
perror("Can't get port settings");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
cfmakeraw(&ti);
|
|
|
|
|
|
|
|
ti.c_cflag |= CLOCAL;
|
|
|
|
if (u->flags & FLOW_CTL)
|
|
|
|
ti.c_cflag |= CRTSCTS;
|
|
|
|
else
|
|
|
|
ti.c_cflag &= ~CRTSCTS;
|
|
|
|
|
|
|
|
if (tcsetattr(fd, TCSANOW, &ti) < 0) {
|
|
|
|
perror("Can't set port settings");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-04-05 22:13:04 +08:00
|
|
|
/* Set initial baudrate */
|
2003-04-08 08:12:02 +08:00
|
|
|
if (set_speed(fd, &ti, u->init_speed) < 0) {
|
2003-04-05 22:13:04 +08:00
|
|
|
perror("Can't set initial baud rate");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
tcflush(fd, TCIOFLUSH);
|
|
|
|
|
2004-10-11 21:59:57 +08:00
|
|
|
if (send_break) {
|
2003-04-05 22:13:04 +08:00
|
|
|
tcsendbreak(fd, 0);
|
2004-10-11 21:59:57 +08:00
|
|
|
usleep(500000);
|
|
|
|
}
|
2003-04-05 22:13:04 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
if (u->init && u->init(fd, u, &ti) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
tcflush(fd, TCIOFLUSH);
|
|
|
|
|
|
|
|
/* Set actual baudrate */
|
|
|
|
if (set_speed(fd, &ti, u->speed) < 0) {
|
|
|
|
perror("Can't set baud rate");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-02-08 20:08:55 +08:00
|
|
|
/* Set TTY to N_HCI line discipline */
|
2002-03-09 05:12:35 +08:00
|
|
|
i = N_HCI;
|
|
|
|
if (ioctl(fd, TIOCSETD, &i) < 0) {
|
2003-02-08 20:08:55 +08:00
|
|
|
perror("Can't set line discipline");
|
2002-03-09 05:12:35 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(fd, HCIUARTSETPROTO, u->proto) < 0) {
|
|
|
|
perror("Can't set device");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
printf("hciattach - HCI UART driver initialization utility\n");
|
|
|
|
printf("Usage:\n");
|
2007-04-26 07:09:02 +08:00
|
|
|
printf("\thciattach [-n] [-p] [-b] [-t timeout] [-s initial_speed] <tty> <type | id> [speed] [flow|noflow] [bdaddr]\n");
|
2002-03-09 05:12:35 +08:00
|
|
|
printf("\thciattach -l\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
struct uart_t *u = NULL;
|
2007-04-18 07:25:14 +08:00
|
|
|
int detach, printpid, opt, i, n, ld, err;
|
2002-03-09 05:12:35 +08:00
|
|
|
int to = 5;
|
2003-04-08 08:12:02 +08:00
|
|
|
int init_speed = 0;
|
2003-04-05 22:13:04 +08:00
|
|
|
int send_break = 0;
|
2002-06-01 01:14:39 +08:00
|
|
|
pid_t pid;
|
2002-03-09 05:12:35 +08:00
|
|
|
struct sigaction sa;
|
2006-03-21 06:29:03 +08:00
|
|
|
struct pollfd p;
|
2005-12-02 23:01:51 +08:00
|
|
|
char dev[PATH_MAX];
|
2002-03-09 05:12:35 +08:00
|
|
|
|
|
|
|
detach = 1;
|
2002-06-01 01:14:39 +08:00
|
|
|
printpid = 0;
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2003-04-05 22:13:04 +08:00
|
|
|
while ((opt=getopt(argc, argv, "bnpt:s:l")) != EOF) {
|
2002-03-09 05:12:35 +08:00
|
|
|
switch(opt) {
|
2003-04-05 22:13:04 +08:00
|
|
|
case 'b':
|
|
|
|
send_break = 1;
|
|
|
|
break;
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
case 'n':
|
|
|
|
detach = 0;
|
|
|
|
break;
|
2002-06-01 01:14:39 +08:00
|
|
|
|
|
|
|
case 'p':
|
|
|
|
printpid = 1;
|
|
|
|
break;
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
case 't':
|
|
|
|
to = atoi(optarg);
|
|
|
|
break;
|
2002-06-01 01:14:39 +08:00
|
|
|
|
2003-04-05 22:13:04 +08:00
|
|
|
case 's':
|
2003-04-08 08:12:02 +08:00
|
|
|
init_speed = atoi(optarg);
|
2003-04-05 22:13:04 +08:00
|
|
|
break;
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
case 'l':
|
|
|
|
for (i = 0; uart[i].type; i++) {
|
|
|
|
printf("%-10s0x%04x,0x%04x\n", uart[i].type,
|
|
|
|
uart[i].m_id, uart[i].p_id);
|
|
|
|
}
|
|
|
|
exit(0);
|
2002-06-01 01:14:39 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = argc - optind;
|
|
|
|
if (n < 2) {
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (n = 0; optind < argc; n++, optind++) {
|
|
|
|
char *opt;
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
opt = argv[optind];
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
switch(n) {
|
|
|
|
case 0:
|
|
|
|
dev[0] = 0;
|
|
|
|
if (!strchr(opt, '/'))
|
|
|
|
strcpy(dev, "/dev/");
|
|
|
|
strcat(dev, opt);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
if (strchr(argv[optind], ',')) {
|
|
|
|
int m_id, p_id;
|
|
|
|
sscanf(argv[optind], "%x,%x", &m_id, &p_id);
|
|
|
|
u = get_by_id(m_id, p_id);
|
|
|
|
} else {
|
|
|
|
u = get_by_type(opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!u) {
|
2003-02-08 20:08:55 +08:00
|
|
|
fprintf(stderr, "Unknown device type or id\n");
|
2002-03-09 05:12:35 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
u->speed = atoi(argv[optind]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
if (!strcmp("flow", argv[optind]))
|
|
|
|
u->flags |= FLOW_CTL;
|
|
|
|
else
|
|
|
|
u->flags &= ~FLOW_CTL;
|
|
|
|
break;
|
2007-04-26 07:09:02 +08:00
|
|
|
|
|
|
|
case 4:
|
|
|
|
u->bdaddr = argv[optind];
|
|
|
|
break;
|
2002-03-09 05:12:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!u) {
|
2003-02-08 20:08:55 +08:00
|
|
|
fprintf(stderr, "Unknown device type or id\n");
|
2002-03-09 05:12:35 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-04-08 08:12:02 +08:00
|
|
|
/* If user specified a initial speed, use that instead of
|
2003-04-05 22:13:04 +08:00
|
|
|
the hardware's default */
|
2003-04-08 08:12:02 +08:00
|
|
|
if (init_speed)
|
|
|
|
u->init_speed = init_speed;
|
2003-04-05 22:13:04 +08:00
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
2006-03-21 06:29:03 +08:00
|
|
|
sa.sa_flags = SA_NOCLDSTOP;
|
2002-03-09 05:12:35 +08:00
|
|
|
sa.sa_handler = sig_alarm;
|
|
|
|
sigaction(SIGALRM, &sa, NULL);
|
|
|
|
|
2003-02-08 20:08:55 +08:00
|
|
|
/* 5 seconds should be enough for initialization */
|
2002-03-09 05:12:35 +08:00
|
|
|
alarm(to);
|
2006-03-21 06:29:03 +08:00
|
|
|
|
2003-04-05 22:13:04 +08:00
|
|
|
n = init_uart(dev, u, send_break);
|
2002-03-09 05:12:35 +08:00
|
|
|
if (n < 0) {
|
2003-02-08 20:08:55 +08:00
|
|
|
perror("Can't initialize device");
|
2002-03-09 05:12:35 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
alarm(0);
|
|
|
|
|
2006-03-21 06:29:03 +08:00
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_flags = SA_NOCLDSTOP;
|
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGCHLD, &sa, NULL);
|
|
|
|
sigaction(SIGPIPE, &sa, NULL);
|
|
|
|
|
|
|
|
sa.sa_handler = sig_term;
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
|
|
|
|
sa.sa_handler = sig_hup;
|
|
|
|
sigaction(SIGHUP, &sa, NULL);
|
|
|
|
|
2002-03-09 05:12:35 +08:00
|
|
|
if (detach) {
|
2002-06-01 01:14:39 +08:00
|
|
|
if ((pid = fork())) {
|
|
|
|
if (printpid)
|
|
|
|
printf("%d\n", pid);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-03-21 06:29:03 +08:00
|
|
|
|
|
|
|
for (i = 0; i < 20; i++)
|
|
|
|
if (i != n)
|
|
|
|
close(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
p.fd = n;
|
|
|
|
p.events = POLLERR | POLLHUP;
|
|
|
|
|
|
|
|
while (!__io_canceled) {
|
|
|
|
p.revents = 0;
|
2007-04-18 07:25:14 +08:00
|
|
|
err = poll(&p, 1, 500);
|
|
|
|
if (err < 0 && errno == EINTR)
|
|
|
|
continue;
|
|
|
|
if (err)
|
2006-03-21 06:29:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restore TTY line discipline */
|
|
|
|
ld = N_TTY;
|
|
|
|
if (ioctl(n, TIOCSETD, &ld) < 0) {
|
|
|
|
perror("Can't restore line discipline");
|
|
|
|
exit(1);
|
2002-03-09 05:12:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|