1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __FMR_H__
18 #define __FMR_H__
19 
20 #include <jni.h>
21 #include <utils/Log.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <termios.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <errno.h>
33 #include <dlfcn.h>
34 
35 #include "fm.h"
36 
37 #undef FM_LIB_USE_XLOG
38 
39 #ifdef FM_LIB_USE_XLOG
40 #include <cutils/xlog.h>
41 #undef LOGV
42 #define LOGV(...) XLOGV(__VA_ARGS__)
43 #undef LOGD
44 #define LOGD(...) XLOGD(__VA_ARGS__)
45 #undef LOGI
46 #define LOGI(...) XLOGI(__VA_ARGS__)
47 #undef LOGW
48 #define LOGW(...) XLOGW(__VA_ARGS__)
49 #undef LOGE
50 #define LOGE(...) XLOGE(__VA_ARGS__)
51 #else
52 #undef LOGV
53 #define LOGV(...) ALOGV(__VA_ARGS__)
54 #undef LOGD
55 #define LOGD(...) ALOGD(__VA_ARGS__)
56 #undef LOGI
57 #define LOGI(...) ALOGI(__VA_ARGS__)
58 #undef LOGW
59 #define LOGW(...) ALOGW(__VA_ARGS__)
60 #undef LOGE
61 #define LOGE(...) ALOGE(__VA_ARGS__)
62 #endif
63 
64 #define CUST_LIB_NAME "libfmcust.so"
65 #define FM_DEV_NAME "/dev/fm"
66 
67 #define FM_RDS_PS_LEN 8
68 
69 struct fm_fake_channel
70 {
71     int freq;
72     int rssi_th;
73     int reserve;
74 };
75 
76 struct fm_fake_channel_t
77 {
78     int size;
79     struct fm_fake_channel *chan;
80 };
81 
82 struct CUST_cfg_ds
83 {
84     int16_t chip;
85     int32_t band;
86     int32_t low_band;
87     int32_t high_band;
88     int32_t seek_space;
89     int32_t max_scan_num;
90     int32_t seek_lev;
91     int32_t scan_sort;
92     int32_t short_ana_sup;
93     int32_t rssi_th_l2;
94     struct fm_fake_channel_t *fake_chan;
95 };
96 
97 struct fm_cbk_tbl {
98     //Basic functions.
99     int (*open_dev)(const char *pname, int *fd);
100     int (*close_dev)(int fd);
101     int (*pwr_up)(int fd, int band, int freq);
102     int (*pwr_down)(int fd, int type);
103     int (*seek)(int fd, int *freq, int band, int dir, int lev);
104     int (*scan)(int fd, uint16_t *tbl, int *num, int band, int sort);
105     int (*stop_scan)(int fd);
106     int (*tune)(int fd, int freq, int band);
107     int (*set_mute)(int fd, int mute);
108     int (*is_rdsrx_support)(int fd, int *supt);
109     int (*turn_on_off_rds)(int fd, int onoff);
110     int (*get_chip_id)(int fd, int *chipid);
111     //FOR RDS RX.
112     int (*read_rds_data)(int fd, RDSData_Struct *rds, uint16_t *rds_status);
113     int (*get_ps)(int fd, RDSData_Struct *rds, uint8_t **ps, int *ps_len);
114     int (*get_rt)(int fd, RDSData_Struct *rds, uint8_t **rt, int *rt_len);
115     int (*active_af)(int fd, RDSData_Struct *rds, int band, uint16_t cur_freq, uint16_t *ret_freq);
116     //FM long/short antenna switch
117     int (*ana_switch)(int fd, int antenna);
118     int (*soft_mute_tune)(int fd, fm_softmute_tune_t *para);
119     int (*desense_check)(int fd, int freq, int rssi);
120     int (*pre_search)(int fd);
121     int (*restore_search)(int fd);
122 };
123 
124 typedef int (*CUST_func_type)(struct CUST_cfg_ds *);
125 typedef void (*init_func_type)(struct fm_cbk_tbl *);
126 
127 struct fmr_ds {
128     int fd;
129     int err;
130     uint16_t cur_freq;
131     uint16_t backup_freq;
132     void *priv;
133     void *custom_handler;
134     struct CUST_cfg_ds cfg_data;
135     struct fm_cbk_tbl tbl;
136     CUST_func_type get_cfg;
137     void *init_handler;
138     init_func_type init_func;
139     RDSData_Struct rds;
140     struct fm_hw_info hw_info;
141     fm_bool scan_stop;
142 };
143 
144 enum fmr_err_em {
145     ERR_SUCCESS = 1000, // kernel error begin at here
146     ERR_INVALID_BUF,
147     ERR_INVALID_PARA,
148     ERR_STP,
149     ERR_GET_MUTEX,
150     ERR_FW_NORES,
151     ERR_RDS_CRC,
152     ERR_INVALID_FD, //  native error begin at here
153     ERR_UNSUPPORT_CHIP,
154     ERR_LD_LIB,
155     ERR_FIND_CUST_FNUC,
156     ERR_UNINIT,
157     ERR_NO_MORE_IDX,
158     ERR_RDS_NO_DATA,
159     ERR_UNSUPT_SHORTANA,
160     ERR_MAX
161 };
162 
163 enum fmr_rds_onoff {
164     FMR_RDS_ON,
165     FMR_RDS_OFF,
166     FMR_MAX
167 };
168 
169 typedef enum {
170     FM_LONG_ANA = 0,
171     FM_SHORT_ANA
172 } fm_antenna_type;
173 
174 
175 #define CQI_CH_NUM_MAX 255
176 #define CQI_CH_NUM_MIN 0
177 
178 
179 /****************** Function declaration ******************/
180 //fmr_err.cpp
181 char *FMR_strerr();
182 void FMR_seterr(int err);
183 
184 //fmr_core.cpp
185 int FMR_init(void);
186 int FMR_get_cfgs(int idx);
187 int FMR_open_dev(int idx);
188 int FMR_close_dev(int idx);
189 int FMR_pwr_up(int idx, int freq);
190 int FMR_pwr_down(int idx, int type);
191 int FMR_seek(int idx, int start_freq, int dir, int *ret_freq);
192 int FMR_scan(int idx, uint16_t *tbl, int *num);
193 int FMR_stop_scan(int idx);
194 int FMR_tune(int idx, int freq);
195 int FMR_set_mute(int idx, int mute);
196 int FMR_is_rdsrx_support(int idx, int *supt);
197 int FMR_turn_on_off_rds(int idx, int onoff);
198 int FMR_get_chip_id(int idx, int *chipid);
199 int FMR_read_rds_data(int idx, uint16_t *rds_status);
200 int FMR_get_ps(int idx, uint8_t **ps, int *ps_len);
201 int FMR_get_rssi(int idx, int *rssi);
202 int FMR_get_rt(int idx, uint8_t **rt, int *rt_len);
203 int FMR_active_af(int idx, uint16_t *ret_freq);
204 
205 int FMR_ana_switch(int idx, int antenna);
206 int FMR_Pre_Search(int idx);
207 int FMR_Restore_Search(int idx);
208 
209 //common part
210 int COM_open_dev(const char *pname, int *fd);
211 int COM_close_dev(int fd);
212 int COM_pwr_up(int fd, int band, int freq);
213 int COM_pwr_down(int fd, int type);
214 int COM_seek(int fd, int *freq, int band, int dir, int lev);
215 int COM_Soft_Mute_Tune(int fd, fm_softmute_tune_t *para);
216 
217 int COM_stop_scan(int fd);
218 int COM_tune(int fd, int freq, int band);
219 int COM_set_mute(int fd, int mute);
220 int COM_is_rdsrx_support(int fd, int *supt);
221 int COM_turn_on_off_rds(int fd, int onoff);
222 int COM_get_chip_id(int fd, int *chipid);
223 int COM_read_rds_data(int fd, RDSData_Struct *rds, uint16_t *rds_status);
224 int COM_get_ps(int fd, RDSData_Struct *rds, uint8_t **ps, int *ps_len);
225 int COM_get_rt(int fd, RDSData_Struct *rds, uint8_t **rt, int *rt_len);
226 int COM_active_af(int fd, RDSData_Struct *rds, int band, uint16_t cur_freq, uint16_t *ret_freq);
227 
228 int COM_ana_switch(int fd, int antenna);
229 int COM_desense_check(int fd, int freq, int rssi);
230 int COM_pre_search(int fd);
231 int COM_restore_search(int fd);
232 void FM_interface_init(struct fm_cbk_tbl *cbk_tbl);
233 
234 #define FMR_ASSERT(a) { \
235     if ((a) == NULL) { \
236         LOGE("%s,invalid buf\n", __func__);\
237         return -ERR_INVALID_BUF; \
238     } \
239 }
240 #endif
241 
242