summaryrefslogtreecommitdiff
path: root/libbt-vendor/src/hci_smd.c
blob: 7e5b16d0b6bc6b5900d07e96fb2cb594ac61cd4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/******************************************************************************
 *
 *  Filename:      hci_smd.c
 *
 *  Description:   Contains vendor-specific userial functions
 *
 ******************************************************************************/

#define LOG_TAG "bt_vendor"

#include <utils/Log.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include "bt_vendor_qcom.h"
#include "hci_smd.h"
#include <string.h>
#include <cutils/properties.h>

/*****************************************************************************
**   Macros & Constants
*****************************************************************************/
#define NUM_OF_DEVS 2
static char *s_pszDevSmd[] = {
    "/dev/smd3",
    "/dev/smd2"
};

/******************************************************************************
**  Externs
******************************************************************************/
extern int is_bt_ssr_hci;


/*****************************************************************************
**   Functions
*****************************************************************************/

int bt_hci_init_transport_id (int chId )
{
  struct termios   term;
  int fd = -1;
  int retry = 0;
  char ssrvalue[92]= {'\0'};

  ssrvalue[0] = '0';
  if(chId >= 2 || chId <0)
     return -1;

  fd = open(s_pszDevSmd[chId], (O_RDWR | O_NOCTTY));

  while ((-1 == fd) && (retry < 7)) {
    ALOGE("init_transport: Cannot open %s: %s\n. Retry after 2 seconds",
        s_pszDevSmd[chId], strerror(errno));
    usleep(2000000);
    fd = open(s_pszDevSmd[chId], (O_RDWR | O_NOCTTY));
    retry++;
  }

  if (-1 == fd)
  {
    ALOGE("init_transport: Cannot open %s: %s\n",
        s_pszDevSmd[chId], strerror(errno));
    return -1;
  }

  /* Sleep (0.5sec) added giving time for the smd port to be successfully
     opened internally. Currently successful return from open doesn't
     ensure the smd port is successfully opened.
     TODO: Following sleep to be removed once SMD port is successfully
     opened immediately on return from the aforementioned open call */

  property_get("bluetooth.isSSR", ssrvalue, "");

  if(ssrvalue[0] == '1')
  {
      /*reset the SSR flag */
      if(chId == 1)
      {
          if(property_set("bluetooth.isSSR", "0") < 0)
          {
              ALOGE("SSR: hci_smd.c:SSR case : error in setting up property new\n ");
          }
          else
          {
              ALOGE("SSR: hci_smd.c:SSR case : Reset the SSr Flag new\n ");
          }
      }
      ALOGE("hci_smd.c:IN SSR sleep for 500 msec New \n");
      usleep(500000);
  }

  if (tcflush(fd, TCIOFLUSH) < 0)
  {
    ALOGE("init_uart: Cannot flush %s\n", s_pszDevSmd[chId]);
    close(fd);
    return -1;
  }

  if (tcgetattr(fd, &term) < 0)
  {
    ALOGE("init_uart: Error while getting attributes\n");
    close(fd);
    return -1;
  }

  cfmakeraw(&term);

  /* JN: Do I need to make flow control configurable, since 4020 cannot
   * disable it?
   */
  term.c_cflag |= (CRTSCTS | CLOCAL);

  if (tcsetattr(fd, TCSANOW, &term) < 0)
  {
    ALOGE("init_uart: Error while getting attributes\n");
    close(fd);
    return -1;
  }

  ALOGI("Done intiailizing UART\n");
  return fd;
}

int bt_hci_init_transport(int *pFd)
{
  int i = 0;
  int fd;
  for(i=0; i < NUM_OF_DEVS; i++){
    fd = bt_hci_init_transport_id(i);
    if(fd < 0 ){
      return -1;
    }
    pFd[i] = fd;
   }
   return 0;
}

int bt_hci_deinit_transport(int *pFd)
{
    close(pFd[0]);
    close(pFd[1]);
    return TRUE;
}