xref: /netbsd/usr.sbin/btattach/init_csr.c (revision 6550d01e)
1 /*	$NetBSD: init_csr.c,v 1.2 2009/12/06 12:29:48 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_csr.c,v 1.2 2009/12/06 12:29:48 kiyohara Exp $");
35 
36 #include <bluetooth.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <termios.h>
41 
42 #include <dev/bluetooth/bcsp.h>
43 
44 #include "btattach.h"
45 
46 struct bccmd {
47 	uint8_t	chanid;
48 
49 	struct {
50 		uint16_t type;
51 		uint16_t length;
52 		uint16_t seqno;
53 		uint16_t varid;
54 		uint16_t status;
55 		uint16_t payload[4];
56 	} message;
57 } __attribute__ ((__packed__));
58 
59 #define CSR_BCCMD_FIRST		(1<<6)
60 #define CSR_BCCMD_LAST		(1<<7)
61 
62 #define CSR_BCCMD_MESSAGE_TYPE_GETREQ	0x0000
63 #define CSR_BCCMD_MESSAGE_TYPE_GETRESP	0x0001
64 #define CSR_BCCMD_MESSAGE_TYPE_SETREQ	0x0002
65 
66 #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART		0x6802
67 #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_STOPB	0x2000
68 #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARENB	0x4000
69 #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARODD	0x8000
70 
71 #define CSR_BCCMD_MESSAGE_STATUS_OK			0x0000
72 #define CSR_BCCMD_MESSAGE_STATUS_NO_SUCH_VARID		0x0001
73 #define CSR_BCCMD_MESSAGE_STATUS_TOO_BIG		0x0002
74 #define CSR_BCCMD_MESSAGE_STATUS_NO_VALUE		0x0003
75 #define CSR_BCCMD_MESSAGE_STATUS_BAD_REQ		0x0004
76 #define CSR_BCCMD_MESSAGE_STATUS_NO_ACCESS		0x0005
77 #define CSR_BCCMD_MESSAGE_STATUS_READ_ONLY		0x0006
78 #define CSR_BCCMD_MESSAGE_STATUS_WRITE_ONLY		0x0007
79 #define CSR_BCCMD_MESSAGE_STATUS_ERROR			0x0008
80 #define CSR_BCCMD_MESSAGE_STATUS_PERMISION_DENIED	0x0009
81 
82 void
83 init_csr(int fd, unsigned int speed)
84 {
85 	struct bccmd cmd;
86 
87 	/* setup BlueCore command packet */
88 	memset(&cmd, 0, sizeof(cmd));
89 
90 	cmd.chanid = CSR_BCCMD_LAST | CSR_BCCMD_FIRST | BCSP_CHANNEL_BCCMD;
91 
92 	cmd.message.type = htole16(CSR_BCCMD_MESSAGE_TYPE_SETREQ);
93 	cmd.message.length = htole16(sizeof(cmd.message) >> 1);
94 	cmd.message.seqno = htole16(0);
95 	cmd.message.varid = htole16(CSR_BCCMD_MESSAGE_VARID_CONFIG_UART);
96 	cmd.message.status = htole16(CSR_BCCMD_MESSAGE_STATUS_OK);
97 
98 	/* Value = (baud rate / 244.140625) | no parity | 1 stop bit. */
99 	cmd.message.payload[0] = htole16((speed * 64 + 7812) / 15625);
100 
101 	uart_send_cmd(fd, HCI_CMD_CSR_EXTN, &cmd, sizeof(cmd));
102 	uart_recv_cc(fd, HCI_CMD_CSR_EXTN, NULL, 0);
103 	/* assume it succeeded? */
104 }
105