Home | History | Annotate | Line # | Download | only in btattach
      1 /*	$NetBSD: init_unistone.c,v 1.3 2015/06/16 23:54:58 christos Exp $	*/
      2 /*
      3  * Copyright (c) 2009 KIYOHARA Takashi
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 /*
     28  * init information in this file gleaned from hciattach(8)
     29  * command from Gumstix's patch for BlueZ.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __RCSID("$NetBSD: init_unistone.c,v 1.3 2015/06/16 23:54:58 christos Exp $");
     34 
     35 #include <bluetooth.h>
     36 #include <err.h>
     37 #include <errno.h>
     38 #include <stdlib.h>
     39 #include <termios.h>
     40 
     41 #include <unistd.h>
     42 #include <sys/ioctl.h>
     43 
     44 #include "btattach.h"
     45 
     46 
     47 #define HCI_CMD_INFINEON_SET_UART_BAUDRATE	\
     48         HCI_OPCODE(HCI_OGF_VENDOR, 0x006)
     49 
     50 
     51 static int infineon_manufacturer_mode(int, int);
     52 
     53 static int
     54 infineon_manufacturer_mode(int fd, int enable)
     55 {
     56 	hci_status_rp rp;
     57 	uint8_t cmd[2];
     58 	int n;
     59 
     60 	cmd[0] = enable;
     61 	cmd[1] = 0;	/* No reset */
     62 
     63 	uart_send_cmd(fd, 0xfc11, cmd, sizeof(cmd));
     64 	n = uart_recv_cc(fd, 0xfc11, &rp, sizeof(rp));
     65 	if (n != sizeof(rp) || rp.status != 0x00)
     66 		errx(EXIT_FAILURE, "Manufacturer mode %s failed",
     67 		    enable ? "enable" : "disable");
     68 
     69 	return 0;
     70 }
     71 
     72 void
     73 init_unistone(int fd, unsigned int speed)
     74 {
     75 	hci_command_status_ep cs;
     76 	struct termios tio;
     77 	uint8_t rate, v[2];
     78 	int n;
     79 
     80 	switch(speed) {
     81 	case B9600:	rate = 0x00;	break;
     82 	case B19200:	rate = 0x01;	break;
     83 	case B38400:	rate = 0x02;	break;
     84 	case B57600:	rate = 0x03;	break;
     85 	case B115200:	rate = 0x04;	break;
     86 	case B230400:	rate = 0x05;	break;
     87 	case B460800:	rate = 0x06;	break;
     88 	case B921600:	rate = 0x07;	break;
     89 #if 0
     90 	case B1843200:	rate = 0x08;	break;
     91 #endif
     92 	default:
     93 		errx(EXIT_FAILURE, "invalid speed for infineon unistone: %u",
     94 		    speed);
     95 	}
     96 
     97 	if (tcgetattr(fd, &tio) != 0)
     98 		err(EXIT_FAILURE, "can't get baud rate");
     99 
    100 	infineon_manufacturer_mode(fd, 1);
    101 
    102 	uart_send_cmd(fd, HCI_CMD_INFINEON_SET_UART_BAUDRATE, &rate,
    103 	    sizeof(rate));
    104 
    105 	n = uart_recv_ev(fd, HCI_EVENT_COMMAND_STATUS, &cs, sizeof(cs));
    106 	if (n != sizeof(cs) ||
    107 	    cs.status != 0x00 ||
    108 	    cs.opcode != HCI_CMD_INFINEON_SET_UART_BAUDRATE)
    109 		errx(EXIT_FAILURE, "Set_UART_Baudrate failed");
    110 
    111 	if (cfsetspeed(&tio, speed) != 0 ||
    112 	    tcsetattr(fd, TCSANOW, &tio) != 0)
    113 		err(EXIT_FAILURE, "can't change baud rate");
    114 
    115 	n = uart_recv_ev(fd, HCI_EVENT_VENDOR, &v, sizeof(v));
    116 	if (n != sizeof(v) ||
    117 	    v[0] != 0x12 ||
    118 	    v[1] != 0x00)
    119 		errx(EXIT_FAILURE, "Set_UART_Baudrate not complete");
    120 
    121 	infineon_manufacturer_mode(fd, 0);
    122 }
    123