summaryrefslogtreecommitdiff
path: root/usr.sbin/btattach/init_stlc2500.c
blob: 50ecf6d625ab93c80604a88eaf4fab8bb672ec4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*	$NetBSD: init_stlc2500.c,v 1.3 2010/03/09 02:01:51 kiyohara Exp $	*/

/*-
 * Copyright (c) 2008 Iain Hibbert
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * init information in this file gleaned from hciattach(8)
 * command from BlueZ for Linux - see http://www.bluez.org/
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: init_stlc2500.c,v 1.3 2010/03/09 02:01:51 kiyohara Exp $");

#include <bluetooth.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>

#include "btattach.h"

#define HCI_CMD_ERICSSON_READ_REVISION_INFO \
	HCI_OPCODE(HCI_OGF_VENDOR, 0x00f)

#define HCI_CMD_ST_STORE_IN_NVDS \
	HCI_OPCODE(HCI_OGF_VENDOR, 0x022)

typedef struct {
	uint8_t		d0;	/* ? */
	uint8_t		d1;	/* ? */
	bdaddr_t	bdaddr;
} __attribute__ ((__packed__)) hci_store_in_nvds_cp;

static const char *default_bdaddr = "00:80:e1:00:ab:ba";

#define HCI_CMD_ST_LOAD_FIRMWARE \
	HCI_OPCODE(HCI_OGF_VENDOR, 0x02e)

static int
firmload_stlc2500(int fd, uint16_t revision, const char *ext)
{
	uint8_t buf[0x100], seq;
	char *name;
	ssize_t n;
	int ff;

	/* we should look up hw.firmware.path and search for the file */

	asprintf(&name, "STLC2500_R%d_%02d_%s", (revision & 0xff), (revision >> 8), ext);
	ff = open(name, O_RDONLY, 0);
	if (ff < 0) {
		if (errno != ENOENT)
			err(EXIT_FAILURE, "%s", name);

		free(name);
		return -1;
	}

	for (seq = 0;; seq++) {
		n = read(ff, buf + 1, sizeof(buf) - 1);
		if (n < 0)
			err(EXIT_FAILURE, "%s", name);

		if (n == 0)
			break;

		buf[0] = seq;
		uart_send_cmd(fd, HCI_CMD_ST_LOAD_FIRMWARE, buf, (size_t)(n + 1));
		n = uart_recv_cc(fd, HCI_CMD_ST_LOAD_FIRMWARE, buf, 1);
		/* command complete ok? */

		if (n != 1 || buf[0] != seq)
			err(EXIT_FAILURE, "sequence mismatch");
	}

	close(ff);
	free(name);
	return 0;
}

void
init_stlc2500(int fd, unsigned int speed)
{
	hci_read_local_ver_rp rp;
	hci_store_in_nvds_cp cp;
	struct termios tio;
	int n;

	/* STLC2500 has an ericsson core */
	init_ericsson(fd, speed);

	if (tcgetattr(fd, &tio) != 0 ||
	    cfsetspeed(&tio, speed) != 0 ||
	    tcsetattr(fd, TCSANOW, &tio) != 0)
		err(EXIT_FAILURE, "can't change baud rate");

	uart_send_cmd(fd, HCI_CMD_READ_LOCAL_VER, NULL, 0);
	n = uart_recv_cc(fd, HCI_CMD_READ_LOCAL_VER, &rp, sizeof(rp));
	if (n != sizeof(rp) || rp.status != 0x00)
		errx(EXIT_FAILURE, "read local version command failed");

	if (firmload_stlc2500(fd, rp.hci_revision, "ptc") < 0)
		warn("no ROM patch file");

	if (firmload_stlc2500(fd, rp.hci_revision, "ssf") < 0)
		warn("no static settings file");

	cp.d0 = 0xfe;	/* ? */
	cp.d1 = 0x06;	/* ? */
	bt_aton(default_bdaddr, &cp.bdaddr);

	uart_send_cmd(fd, HCI_CMD_ST_STORE_IN_NVDS, &cp, sizeof(cp));
	uart_recv_cc(fd, HCI_CMD_ST_STORE_IN_NVDS, NULL, 0);
	/* command complete ok? */
	/* assume it succeeded? */

	uart_send_cmd(fd, HCI_CMD_RESET, NULL, 0);
	uart_recv_cc(fd, HCI_CMD_RESET, NULL, 0);
	/* assume it succeeded? */
}