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