No empty .Rs/.Re
[netbsd-mini2440.git] / usr.sbin / btattach / init_stlc2500.c
blob4fd79360508f5039a28a9b2f2a57f9117cd231b1
1 /* $NetBSD: init_stlc2500.c,v 1.1 2008/04/15 11:17:48 plunky Exp $ */
3 /*-
4 * Copyright (c) 2008 Iain Hibbert
5 * All rights reserved.
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.
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.
29 * init information in this file gleaned from hciattach(8)
30 * command from BlueZ for Linux - see http://www.bluez.org/
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: init_stlc2500.c,v 1.1 2008/04/15 11:17:48 plunky Exp $");
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>
44 #include "btattach.h"
46 #define HCI_CMD_ERICSSON_READ_REVISION_INFO \
47 HCI_OPCODE(HCI_OGF_VENDOR, 0x00f)
49 #define HCI_CMD_ST_STORE_IN_NVDS \
50 HCI_OPCODE(HCI_OGF_VENDOR, 0x022)
52 typedef struct {
53 uint8_t d0; /* ? */
54 uint8_t d1; /* ? */
55 bdaddr_t bdaddr;
56 } __attribute__ ((__packed__)) hci_store_in_nvds_cp;
58 static const char *default_bdaddr = "00:80:e1:00:ab:ba";
60 #define HCI_CMD_ST_LOAD_FIRMWARE \
61 HCI_OPCODE(HCI_OGF_VENDOR, 0x02e)
63 static int
64 firmload_stlc2500(int fd, uint16_t revision, const char *ext)
66 uint8_t buf[0x100], seq;
67 char *name;
68 ssize_t n;
69 int ff;
71 /* we should look up hw.firmware.path and search for the file */
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);
79 free(name);
80 return -1;
83 for (seq = 0;; seq++) {
84 n = read(ff, buf + 1, sizeof(buf) - 1);
85 if (n < 0)
86 err(EXIT_FAILURE, "%s", name);
88 if (n == 0)
89 break;
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? */
96 if (n != 1 || buf[0] != seq)
97 err(EXIT_FAILURE, "sequence mismatch");
100 close(ff);
101 free(name);
102 return 0;
105 void
106 init_stlc2500(int fd, unsigned int speed)
108 hci_read_local_ver_rp rp;
109 hci_store_in_nvds_cp cp;
110 struct termios tio;
111 int n;
113 /* STLC2500 has an ericsson core */
114 init_ericsson(fd, speed);
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");
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");
126 if (firmload_stlc2500(fd, rp.hci_revision, "ptc") < 0)
127 warn("no ROM patch file");
129 if (firmload_stlc2500(fd, rp.hci_revision, "ssf") < 0)
130 warn("no static settings file");
132 cp.d0 = 0xfe; /* ? */
133 cp.d1 = 0x06; /* ? */
134 bt_aton(default_bdaddr, &cp.bdaddr);
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? */
141 uart_send_cmd(fd, HCI_CMD_RESET, NULL, 0);
142 uart_recv_cc(fd, HCI_CMD_RESET, NULL, 0);
143 /* assume it succeeded? */