1 1.2 kiyohara /* $NetBSD: init_csr.c,v 1.2 2009/12/06 12:29:48 kiyohara Exp $ */ 2 1.1 plunky 3 1.1 plunky /*- 4 1.1 plunky * Copyright (c) 2008 Iain Hibbert 5 1.1 plunky * All rights reserved. 6 1.1 plunky * 7 1.1 plunky * Redistribution and use in source and binary forms, with or without 8 1.1 plunky * modification, are permitted provided that the following conditions 9 1.1 plunky * are met: 10 1.1 plunky * 1. Redistributions of source code must retain the above copyright 11 1.1 plunky * notice, this list of conditions and the following disclaimer. 12 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 plunky * notice, this list of conditions and the following disclaimer in the 14 1.1 plunky * documentation and/or other materials provided with the distribution. 15 1.1 plunky * 16 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 plunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 plunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 plunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 plunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 plunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 plunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 plunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 plunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 plunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 plunky */ 27 1.1 plunky 28 1.1 plunky /* 29 1.1 plunky * init information in this file gleaned from hciattach(8) 30 1.1 plunky * command from BlueZ for Linux - see http://www.bluez.org/ 31 1.1 plunky */ 32 1.1 plunky 33 1.1 plunky #include <sys/cdefs.h> 34 1.2 kiyohara __RCSID("$NetBSD: init_csr.c,v 1.2 2009/12/06 12:29:48 kiyohara Exp $"); 35 1.1 plunky 36 1.1 plunky #include <bluetooth.h> 37 1.1 plunky #include <errno.h> 38 1.1 plunky #include <stdlib.h> 39 1.1 plunky #include <string.h> 40 1.1 plunky #include <termios.h> 41 1.1 plunky 42 1.1 plunky #include <dev/bluetooth/bcsp.h> 43 1.1 plunky 44 1.1 plunky #include "btattach.h" 45 1.1 plunky 46 1.1 plunky struct bccmd { 47 1.1 plunky uint8_t chanid; 48 1.1 plunky 49 1.1 plunky struct { 50 1.1 plunky uint16_t type; 51 1.1 plunky uint16_t length; 52 1.1 plunky uint16_t seqno; 53 1.1 plunky uint16_t varid; 54 1.1 plunky uint16_t status; 55 1.1 plunky uint16_t payload[4]; 56 1.1 plunky } message; 57 1.1 plunky } __attribute__ ((__packed__)); 58 1.1 plunky 59 1.1 plunky #define CSR_BCCMD_FIRST (1<<6) 60 1.1 plunky #define CSR_BCCMD_LAST (1<<7) 61 1.1 plunky 62 1.1 plunky #define CSR_BCCMD_MESSAGE_TYPE_GETREQ 0x0000 63 1.1 plunky #define CSR_BCCMD_MESSAGE_TYPE_GETRESP 0x0001 64 1.1 plunky #define CSR_BCCMD_MESSAGE_TYPE_SETREQ 0x0002 65 1.1 plunky 66 1.1 plunky #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART 0x6802 67 1.1 plunky #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_STOPB 0x2000 68 1.1 plunky #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARENB 0x4000 69 1.1 plunky #define CSR_BCCMD_MESSAGE_VARID_CONFIG_UART_PARODD 0x8000 70 1.1 plunky 71 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_OK 0x0000 72 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_NO_SUCH_VARID 0x0001 73 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_TOO_BIG 0x0002 74 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_NO_VALUE 0x0003 75 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_BAD_REQ 0x0004 76 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_NO_ACCESS 0x0005 77 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_READ_ONLY 0x0006 78 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_WRITE_ONLY 0x0007 79 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_ERROR 0x0008 80 1.1 plunky #define CSR_BCCMD_MESSAGE_STATUS_PERMISION_DENIED 0x0009 81 1.1 plunky 82 1.1 plunky void 83 1.1 plunky init_csr(int fd, unsigned int speed) 84 1.1 plunky { 85 1.1 plunky struct bccmd cmd; 86 1.1 plunky 87 1.2 kiyohara /* setup BlueCore command packet */ 88 1.1 plunky memset(&cmd, 0, sizeof(cmd)); 89 1.1 plunky 90 1.1 plunky cmd.chanid = CSR_BCCMD_LAST | CSR_BCCMD_FIRST | BCSP_CHANNEL_BCCMD; 91 1.1 plunky 92 1.1 plunky cmd.message.type = htole16(CSR_BCCMD_MESSAGE_TYPE_SETREQ); 93 1.1 plunky cmd.message.length = htole16(sizeof(cmd.message) >> 1); 94 1.1 plunky cmd.message.seqno = htole16(0); 95 1.1 plunky cmd.message.varid = htole16(CSR_BCCMD_MESSAGE_VARID_CONFIG_UART); 96 1.1 plunky cmd.message.status = htole16(CSR_BCCMD_MESSAGE_STATUS_OK); 97 1.1 plunky 98 1.1 plunky /* Value = (baud rate / 244.140625) | no parity | 1 stop bit. */ 99 1.1 plunky cmd.message.payload[0] = htole16((speed * 64 + 7812) / 15625); 100 1.1 plunky 101 1.1 plunky uart_send_cmd(fd, HCI_CMD_CSR_EXTN, &cmd, sizeof(cmd)); 102 1.1 plunky uart_recv_cc(fd, HCI_CMD_CSR_EXTN, NULL, 0); 103 1.1 plunky /* assume it succeeded? */ 104 1.1 plunky } 105