1*69144e75Skiyohara /* $NetBSD: init_csr.c,v 1.2 2009/12/06 12:29:48 kiyohara Exp $ */
2486e4624Splunky
3486e4624Splunky /*-
4486e4624Splunky * Copyright (c) 2008 Iain Hibbert
5486e4624Splunky * All rights reserved.
6486e4624Splunky *
7486e4624Splunky * Redistribution and use in source and binary forms, with or without
8486e4624Splunky * modification, are permitted provided that the following conditions
9486e4624Splunky * are met:
10486e4624Splunky * 1. Redistributions of source code must retain the above copyright
11486e4624Splunky * notice, this list of conditions and the following disclaimer.
12486e4624Splunky * 2. Redistributions in binary form must reproduce the above copyright
13486e4624Splunky * notice, this list of conditions and the following disclaimer in the
14486e4624Splunky * documentation and/or other materials provided with the distribution.
15486e4624Splunky *
16486e4624Splunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17486e4624Splunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18486e4624Splunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19486e4624Splunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20486e4624Splunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21486e4624Splunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22486e4624Splunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23486e4624Splunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24486e4624Splunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25486e4624Splunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26486e4624Splunky */
27486e4624Splunky
28486e4624Splunky /*
29486e4624Splunky * init information in this file gleaned from hciattach(8)
30486e4624Splunky * command from BlueZ for Linux - see http://www.bluez.org/
31486e4624Splunky */
32486e4624Splunky
33486e4624Splunky #include <sys/cdefs.h>
34*69144e75Skiyohara __RCSID("$NetBSD: init_csr.c,v 1.2 2009/12/06 12:29:48 kiyohara Exp $");
35486e4624Splunky
36486e4624Splunky #include <bluetooth.h>
37486e4624Splunky #include <errno.h>
38486e4624Splunky #include <stdlib.h>
39486e4624Splunky #include <string.h>
40486e4624Splunky #include <termios.h>
41486e4624Splunky
42486e4624Splunky #include <dev/bluetooth/bcsp.h>
43486e4624Splunky
44486e4624Splunky #include "btattach.h"
45486e4624Splunky
46486e4624Splunky struct bccmd {
47486e4624Splunky uint8_t chanid;
48486e4624Splunky
49486e4624Splunky struct {
50486e4624Splunky uint16_t type;
51486e4624Splunky uint16_t length;
52486e4624Splunky uint16_t seqno;
53486e4624Splunky uint16_t varid;
54486e4624Splunky uint16_t status;
55486e4624Splunky uint16_t payload[4];
56486e4624Splunky } message;
57486e4624Splunky } __attribute__ ((__packed__));
58486e4624Splunky
59486e4624Splunky #define CSR_BCCMD_FIRST (1<<6)
60486e4624Splunky #define CSR_BCCMD_LAST (1<<7)
61486e4624Splunky
62486e4624Splunky #define CSR_BCCMD_MESSAGE_TYPE_GETREQ 0x0000
63486e4624Splunky #define CSR_BCCMD_MESSAGE_TYPE_GETRESP 0x0001
64486e4624Splunky #define CSR_BCCMD_MESSAGE_TYPE_SETREQ 0x0002
65486e4624Splunky
66486e4624Splunky #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART 0x6802
67486e4624Splunky #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_STOPB 0x2000
68486e4624Splunky #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARENB 0x4000
69486e4624Splunky #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARODD 0x8000
70486e4624Splunky
71486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_OK 0x0000
72486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_NO_SUCH_VARID 0x0001
73486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_TOO_BIG 0x0002
74486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_NO_VALUE 0x0003
75486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_BAD_REQ 0x0004
76486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_NO_ACCESS 0x0005
77486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_READ_ONLY 0x0006
78486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_WRITE_ONLY 0x0007
79486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_ERROR 0x0008
80486e4624Splunky #define CSR_BCCMD_MESSAGE_STATUS_PERMISION_DENIED 0x0009
81486e4624Splunky
82486e4624Splunky void
init_csr(int fd,unsigned int speed)83486e4624Splunky init_csr(int fd, unsigned int speed)
84486e4624Splunky {
85486e4624Splunky struct bccmd cmd;
86486e4624Splunky
87*69144e75Skiyohara /* setup BlueCore command packet */
88486e4624Splunky memset(&cmd, 0, sizeof(cmd));
89486e4624Splunky
90486e4624Splunky cmd.chanid = CSR_BCCMD_LAST | CSR_BCCMD_FIRST | BCSP_CHANNEL_BCCMD;
91486e4624Splunky
92486e4624Splunky cmd.message.type = htole16(CSR_BCCMD_MESSAGE_TYPE_SETREQ);
93486e4624Splunky cmd.message.length = htole16(sizeof(cmd.message) >> 1);
94486e4624Splunky cmd.message.seqno = htole16(0);
95486e4624Splunky cmd.message.varid = htole16(CSR_BCCMD_MESSAGE_VARID_CONFIG_UART);
96486e4624Splunky cmd.message.status = htole16(CSR_BCCMD_MESSAGE_STATUS_OK);
97486e4624Splunky
98486e4624Splunky /* Value = (baud rate / 244.140625) | no parity | 1 stop bit. */
99486e4624Splunky cmd.message.payload[0] = htole16((speed * 64 + 7812) / 15625);
100486e4624Splunky
101486e4624Splunky uart_send_cmd(fd, HCI_CMD_CSR_EXTN, &cmd, sizeof(cmd));
102486e4624Splunky uart_recv_cc(fd, HCI_CMD_CSR_EXTN, NULL, 0);
103486e4624Splunky /* assume it succeeded? */
104486e4624Splunky }
105