126236Swpaul/*	$NetBSD: init_csr.c,v 1.1 2008/04/15 11:17:48 plunky Exp $	*/
226236Swpaul
326236Swpaul/*-
426236Swpaul * Copyright (c) 2008 Iain Hibbert
526236Swpaul * All rights reserved.
626236Swpaul *
726236Swpaul * Redistribution and use in source and binary forms, with or without
826236Swpaul * modification, are permitted provided that the following conditions
926236Swpaul * are met:
1026236Swpaul * 1. Redistributions of source code must retain the above copyright
1126236Swpaul *    notice, this list of conditions and the following disclaimer.
1226236Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1326236Swpaul *    notice, this list of conditions and the following disclaimer in the
1426236Swpaul *    documentation and/or other materials provided with the distribution.
1526236Swpaul *
1626236Swpaul * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1726236Swpaul * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1826236Swpaul * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1926236Swpaul * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2026236Swpaul * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2126236Swpaul * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2226236Swpaul * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2326236Swpaul * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2426236Swpaul * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2526236Swpaul * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626236Swpaul */
2726236Swpaul
2826236Swpaul/*
2926236Swpaul * init information in this file gleaned from hciattach(8)
3026236Swpaul * command from BlueZ for Linux - see http://www.bluez.org/
3126236Swpaul */
3226236Swpaul
3330378Scharnier#include <sys/cdefs.h>
3430378Scharnier__RCSID("$NetBSD: init_csr.c,v 1.1 2008/04/15 11:17:48 plunky Exp $");
3550479Speter
3630378Scharnier#include <bluetooth.h>
3730378Scharnier#include <errno.h>
3826236Swpaul#include <stdlib.h>
3926236Swpaul#include <string.h>
4026236Swpaul#include <termios.h>
4126236Swpaul
4226236Swpaul#include <dev/bluetooth/bcsp.h>
4326236Swpaul
4426236Swpaul#include "btattach.h"
4526236Swpaul
4626236Swpaulstruct bccmd {
4726236Swpaul	uint8_t	chanid;
4826236Swpaul
4926236Swpaul	struct {
5026236Swpaul		uint16_t type;
5126236Swpaul		uint16_t length;
5226236Swpaul		uint16_t seqno;
5326236Swpaul		uint16_t varid;
5426236Swpaul		uint16_t status;
5526236Swpaul		uint16_t payload[4];
5626236Swpaul	} message;
5726236Swpaul} __attribute__ ((__packed__));
5826236Swpaul
5926236Swpaul#define CSR_BCCMD_FIRST		(1<<6)
6026236Swpaul#define CSR_BCCMD_LAST		(1<<7)
6126236Swpaul
6226236Swpaul#define CSR_BCCMD_MESSAGE_TYPE_GETREQ	0x0000
6326236Swpaul#define CSR_BCCMD_MESSAGE_TYPE_GETRESP	0x0001
6426236Swpaul#define CSR_BCCMD_MESSAGE_TYPE_SETREQ	0x0002
6526236Swpaul
6626236Swpaul#define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART		0x6802
6726236Swpaul#define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_STOPB	0x2000
6826236Swpaul#define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARENB	0x4000
6926236Swpaul#define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARODD	0x8000
7026236Swpaul
7126236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_OK			0x0000
7226236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_NO_SUCH_VARID		0x0001
7326236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_TOO_BIG		0x0002
7426236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_NO_VALUE		0x0003
7526236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_BAD_REQ		0x0004
7626236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_NO_ACCESS		0x0005
7726236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_READ_ONLY		0x0006
7826236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_WRITE_ONLY		0x0007
7926236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_ERROR			0x0008
8026236Swpaul#define CSR_BCCMD_MESSAGE_STATUS_PERMISION_DENIED	0x0009
8126236Swpaul
8226236Swpaulvoid
8326236Swpaulinit_csr(int fd, unsigned int speed)
8426236Swpaul{
8526236Swpaul	struct bccmd cmd;
8626236Swpaul
8762989Skris	/* setup BlueCore command packet */
8826236Swpaul	memset(&cmd, 0, sizeof(cmd));
8930378Scharnier
9026236Swpaul	cmd.chanid = CSR_BCCMD_LAST | CSR_BCCMD_FIRST | BCSP_CHANNEL_BCCMD;
9162989Skris
9226236Swpaul	cmd.message.type = htole16(CSR_BCCMD_MESSAGE_TYPE_SETREQ);
9326236Swpaul	cmd.message.length = htole16(sizeof(cmd.message) >> 1);
9426236Swpaul	cmd.message.seqno = htole16(0);
9526236Swpaul	cmd.message.varid = htole16(CSR_BCCMD_MESSAGE_VARID_CONFIG_UART);
9626236Swpaul	cmd.message.status = htole16(CSR_BCCMD_MESSAGE_STATUS_OK);
9726236Swpaul
9826236Swpaul	/* Value = (baud rate / 244.140625) | no parity | 1 stop bit. */
9926236Swpaul	cmd.message.payload[0] = htole16((speed * 64 + 7812) / 15625);
10026236Swpaul
10126236Swpaul	uart_send_cmd(fd, HCI_CMD_CSR_EXTN, &cmd, sizeof(cmd));
10226236Swpaul	uart_recv_cc(fd, HCI_CMD_CSR_EXTN, NULL, 0);
10326236Swpaul	/* assume it succeeded? */
10426236Swpaul}
10526236Swpaul