Home | History | Annotate | Line # | Download | only in btattach
init_bcm43xx.c revision 1.2
      1  1.2  jakllsch /*	$NetBSD: init_bcm43xx.c,v 1.2 2017/08/10 18:45:20 jakllsch Exp $	*/
      2  1.1       nat 
      3  1.1       nat /*-
      4  1.1       nat  * Copyright (c) 2017 Iain Hibbert
      5  1.1       nat  * All rights reserved.
      6  1.1       nat  *
      7  1.1       nat  * Copyright (c) 2008 Iain Hibbert
      8  1.1       nat  * All rights reserved.
      9  1.1       nat  *
     10  1.1       nat  * Redistribution and use in source and binary forms, with or without
     11  1.1       nat  * modification, are permitted provided that the following conditions
     12  1.1       nat  * are met:
     13  1.1       nat  * 1. Redistributions of source code must retain the above copyright
     14  1.1       nat  *    notice, this list of conditions and the following disclaimer.
     15  1.1       nat  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1       nat  *    notice, this list of conditions and the following disclaimer in the
     17  1.1       nat  *    documentation and/or other materials provided with the distribution.
     18  1.1       nat  *
     19  1.1       nat  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1       nat  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1       nat  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1       nat  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1       nat  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1       nat  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1       nat  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1       nat  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1       nat  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1       nat  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1       nat  */
     30  1.1       nat 
     31  1.1       nat /*
     32  1.1       nat  * init information in this file gleaned from hciattach(8)
     33  1.1       nat  * command from BlueZ for Linux - see http://www.bluez.org/
     34  1.1       nat  */
     35  1.1       nat 
     36  1.1       nat #include <sys/cdefs.h>
     37  1.2  jakllsch __RCSID("$NetBSD: init_bcm43xx.c,v 1.2 2017/08/10 18:45:20 jakllsch Exp $");
     38  1.1       nat 
     39  1.1       nat #include <bluetooth.h>
     40  1.1       nat #include <err.h>
     41  1.1       nat #include <errno.h>
     42  1.1       nat #include <fcntl.h>
     43  1.1       nat #include <stdlib.h>
     44  1.1       nat #include <string.h>
     45  1.1       nat #include <termios.h>
     46  1.1       nat #include <unistd.h>
     47  1.1       nat 
     48  1.1       nat #include "btattach.h"
     49  1.1       nat 
     50  1.1       nat #define HCI_CMD_BCM43XX_SET_UART_BAUD_RATE	\
     51  1.1       nat 	HCI_OPCODE(HCI_OGF_VENDOR, 0x018)
     52  1.1       nat 
     53  1.1       nat #define HCI_CMD_BCM43XX_SET_BDADDR		\
     54  1.1       nat 	HCI_OPCODE(HCI_OGF_VENDOR, 0x006)
     55  1.1       nat 
     56  1.1       nat #define HCI_CMD_43XXFWDN 			\
     57  1.1       nat 	HCI_OPCODE(HCI_OGF_VENDOR, 0x02e)
     58  1.1       nat 
     59  1.1       nat void
     60  1.1       nat init_bcm43xx(int fd, unsigned int speed)
     61  1.1       nat {
     62  1.1       nat 	uint8_t rate[6];
     63  1.1       nat 	uint8_t fw_buf[1024];
     64  1.1       nat 	char fw[] = "./BCM43430A1.hcd";
     65  1.2  jakllsch 	int fwfd, fw_len;
     66  1.1       nat 	uint8_t resp[7];
     67  1.1       nat 	uint16_t fw_cmd;
     68  1.1       nat 
     69  1.1       nat 	memset(rate, 0, sizeof(rate));
     70  1.1       nat 
     71  1.1       nat 	uart_send_cmd(fd, HCI_CMD_RESET, NULL, 0);
     72  1.1       nat 	uart_recv_cc(fd, HCI_CMD_RESET, &resp, sizeof(resp));
     73  1.1       nat 	/* assume it succeeded? */
     74  1.1       nat 
     75  1.1       nat 	fwfd = open(fw, O_RDONLY);
     76  1.1       nat 	if (fwfd < 0) {
     77  1.1       nat 		fprintf(stderr, "Unable to open firmware: %s\n", fw);
     78  1.1       nat 		return;
     79  1.1       nat 	}
     80  1.1       nat 
     81  1.1       nat 	uart_send_cmd(fd, HCI_CMD_43XXFWDN, NULL, 0);
     82  1.1       nat 	uart_recv_cc(fd, HCI_CMD_43XXFWDN, &resp, sizeof(resp));
     83  1.1       nat 		sleep(1);
     84  1.1       nat 
     85  1.1       nat 	while (read(fwfd, &fw_buf[1], 3) == 3) {
     86  1.1       nat 		fw_buf[0] = HCI_CMD_PKT;
     87  1.1       nat 		fw_len = fw_buf[3];
     88  1.1       nat 		if (read(fwfd, &fw_buf[4], fw_len) != fw_len)
     89  1.1       nat 			break;
     90  1.1       nat 		fw_cmd = fw_buf[2] << 8 | fw_buf[1];
     91  1.1       nat 		uart_send_cmd(fd, fw_cmd, &fw_buf[4], fw_len);
     92  1.1       nat 		uart_recv_cc(fd, fw_cmd, &resp, sizeof(resp));
     93  1.1       nat 	}
     94  1.1       nat 
     95  1.1       nat 	close(fwfd);
     96  1.1       nat 
     97  1.1       nat 	sleep(4);
     98  1.1       nat 	uart_send_cmd(fd, HCI_CMD_RESET, NULL, 0);
     99  1.1       nat 	uart_recv_cc(fd, HCI_CMD_RESET, &resp, sizeof(resp));
    100  1.1       nat 	/* assume it succeeded? */
    101  1.1       nat 
    102  1.1       nat 	rate[2] = speed;
    103  1.1       nat 	rate[3] = speed >> 8;
    104  1.1       nat 	rate[4] = speed >> 16;
    105  1.1       nat 	rate[5] = speed >> 24;
    106  1.1       nat 
    107  1.1       nat 	uart_send_cmd(fd, HCI_CMD_BCM43XX_SET_UART_BAUD_RATE, &rate, sizeof(rate));
    108  1.1       nat 	uart_recv_cc(fd, HCI_CMD_BCM43XX_SET_UART_BAUD_RATE, &resp, sizeof(resp));
    109  1.1       nat }
    110