xref: /netbsd-src/usr.sbin/btattach/init_stlc2500.c (revision cecd9b8efc203298af0073b9a484d92facee99ef)
1 /*	$NetBSD: init_stlc2500.c,v 1.3 2010/03/09 02:01:51 kiyohara Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 Iain Hibbert
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * init information in this file gleaned from hciattach(8)
30  * command from BlueZ for Linux - see http://www.bluez.org/
31  */
32 
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: init_stlc2500.c,v 1.3 2010/03/09 02:01:51 kiyohara Exp $");
35 
36 #include <bluetooth.h>
37 #include <err.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <termios.h>
42 #include <unistd.h>
43 
44 #include "btattach.h"
45 
46 #define HCI_CMD_ERICSSON_READ_REVISION_INFO \
47 	HCI_OPCODE(HCI_OGF_VENDOR, 0x00f)
48 
49 #define HCI_CMD_ST_STORE_IN_NVDS \
50 	HCI_OPCODE(HCI_OGF_VENDOR, 0x022)
51 
52 typedef struct {
53 	uint8_t		d0;	/* ? */
54 	uint8_t		d1;	/* ? */
55 	bdaddr_t	bdaddr;
56 } __attribute__ ((__packed__)) hci_store_in_nvds_cp;
57 
58 static const char *default_bdaddr = "00:80:e1:00:ab:ba";
59 
60 #define HCI_CMD_ST_LOAD_FIRMWARE \
61 	HCI_OPCODE(HCI_OGF_VENDOR, 0x02e)
62 
63 static int
firmload_stlc2500(int fd,uint16_t revision,const char * ext)64 firmload_stlc2500(int fd, uint16_t revision, const char *ext)
65 {
66 	uint8_t buf[0x100], seq;
67 	char *name;
68 	ssize_t n;
69 	int ff;
70 
71 	/* we should look up hw.firmware.path and search for the file */
72 
73 	asprintf(&name, "STLC2500_R%d_%02d_%s", (revision & 0xff), (revision >> 8), ext);
74 	ff = open(name, O_RDONLY, 0);
75 	if (ff < 0) {
76 		if (errno != ENOENT)
77 			err(EXIT_FAILURE, "%s", name);
78 
79 		free(name);
80 		return -1;
81 	}
82 
83 	for (seq = 0;; seq++) {
84 		n = read(ff, buf + 1, sizeof(buf) - 1);
85 		if (n < 0)
86 			err(EXIT_FAILURE, "%s", name);
87 
88 		if (n == 0)
89 			break;
90 
91 		buf[0] = seq;
92 		uart_send_cmd(fd, HCI_CMD_ST_LOAD_FIRMWARE, buf, (size_t)(n + 1));
93 		n = uart_recv_cc(fd, HCI_CMD_ST_LOAD_FIRMWARE, buf, 1);
94 		/* command complete ok? */
95 
96 		if (n != 1 || buf[0] != seq)
97 			err(EXIT_FAILURE, "sequence mismatch");
98 	}
99 
100 	close(ff);
101 	free(name);
102 	return 0;
103 }
104 
105 void
init_stlc2500(int fd,unsigned int speed)106 init_stlc2500(int fd, unsigned int speed)
107 {
108 	hci_read_local_ver_rp rp;
109 	hci_store_in_nvds_cp cp;
110 	struct termios tio;
111 	int n;
112 
113 	/* STLC2500 has an ericsson core */
114 	init_ericsson(fd, speed);
115 
116 	if (tcgetattr(fd, &tio) != 0 ||
117 	    cfsetspeed(&tio, speed) != 0 ||
118 	    tcsetattr(fd, TCSANOW, &tio) != 0)
119 		err(EXIT_FAILURE, "can't change baud rate");
120 
121 	uart_send_cmd(fd, HCI_CMD_READ_LOCAL_VER, NULL, 0);
122 	n = uart_recv_cc(fd, HCI_CMD_READ_LOCAL_VER, &rp, sizeof(rp));
123 	if (n != sizeof(rp) || rp.status != 0x00)
124 		errx(EXIT_FAILURE, "read local version command failed");
125 
126 	if (firmload_stlc2500(fd, rp.hci_revision, "ptc") < 0)
127 		warn("no ROM patch file");
128 
129 	if (firmload_stlc2500(fd, rp.hci_revision, "ssf") < 0)
130 		warn("no static settings file");
131 
132 	cp.d0 = 0xfe;	/* ? */
133 	cp.d1 = 0x06;	/* ? */
134 	bt_aton(default_bdaddr, &cp.bdaddr);
135 
136 	uart_send_cmd(fd, HCI_CMD_ST_STORE_IN_NVDS, &cp, sizeof(cp));
137 	uart_recv_cc(fd, HCI_CMD_ST_STORE_IN_NVDS, NULL, 0);
138 	/* command complete ok? */
139 	/* assume it succeeded? */
140 
141 	uart_send_cmd(fd, HCI_CMD_RESET, NULL, 0);
142 	uart_recv_cc(fd, HCI_CMD_RESET, NULL, 0);
143 	/* assume it succeeded? */
144 }
145