187628Sdwmalone/*	$NetBSD$	*/
287304Sdwmalone/*
31590Srgrimes * Copyright (c) 2009 KIYOHARA Takashi
4265420Simp * All rights reserved.
5263227Sjmmv *
61590Srgrimes * Redistribution and use in source and binary forms, with or without
796942Sjmallett * modification, are permitted provided that the following conditions
8265420Simp * are met:
91590Srgrimes * 1. Redistributions of source code must retain the above copyright
10263227Sjmmv *    notice, this list of conditions and the following disclaimer.
11263227Sjmmv * 2. Redistributions in binary form must reproduce the above copyright
12263227Sjmmv *    notice, this list of conditions and the following disclaimer in the
13263227Sjmmv *    documentation and/or other materials provided with the distribution.
141590Srgrimes *
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_ericsson.c,v 1.2 2009/04/15 00:32:23 lukem 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
51static int infineon_manufacturer_mode(int, int);
52
53static int
54infineon_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
72void
73init_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\n",
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\n");
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\n");
120
121	infineon_manufacturer_mode(fd, 0);
122}
123