smscoreapi.c 79.5 KB
Newer Older
1
/*
2 3 4 5
 *  Siano core API module
 *
 *  This file contains implementation for the interface to sms core component
 *
6
 *  author: Uri Shkolnik
7
 *
8
 *  Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
9 10
 *
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License version 2 as
12
 *  published by the Free Software Foundation;
13
 *
14 15
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
16
 *
17
 *  See the GNU General Public License for more details.
18 19 20 21 22 23
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

24 25 26 27 28 29
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
30
#include <linux/io.h>
31
#include <linux/slab.h>
32 33

#include <linux/firmware.h>
34
#include <linux/wait.h>
35
#include <asm/byteorder.h>
36 37

#include "smscoreapi.h"
38
#include "sms-cards.h"
39
#include "smsir.h"
40

41
static int sms_dbg;
42
module_param_named(debug, sms_dbg, int, 0644);
43 44
MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");

45
struct smscore_device_notifyee_t {
46 47
	struct list_head entry;
	hotplug_t hotplug;
48
};
49

50
struct smscore_idlist_t {
51 52 53
	struct list_head entry;
	int		id;
	int		data_type;
54
};
55

56
struct smscore_client_t {
57
	struct list_head entry;
58
	struct smscore_device_t *coredev;
59
	void			*context;
60 61
	struct list_head	idlist;
	onresponse_t		onresponse_handler;
62
	onremove_t		onremove_handler;
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
static char *siano_msgs[] = {
	[MSG_TYPE_BASE_VAL                           - MSG_TYPE_BASE_VAL] = "MSG_TYPE_BASE_VAL",
	[MSG_SMS_GET_VERSION_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_VERSION_REQ",
	[MSG_SMS_GET_VERSION_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_VERSION_RES",
	[MSG_SMS_MULTI_BRIDGE_CFG                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_MULTI_BRIDGE_CFG",
	[MSG_SMS_GPIO_CONFIG_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_CONFIG_REQ",
	[MSG_SMS_GPIO_CONFIG_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_CONFIG_RES",
	[MSG_SMS_GPIO_SET_LEVEL_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_SET_LEVEL_REQ",
	[MSG_SMS_GPIO_SET_LEVEL_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_SET_LEVEL_RES",
	[MSG_SMS_GPIO_GET_LEVEL_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_GET_LEVEL_REQ",
	[MSG_SMS_GPIO_GET_LEVEL_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_GET_LEVEL_RES",
	[MSG_SMS_EEPROM_BURN_IND                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EEPROM_BURN_IND",
	[MSG_SMS_LOG_ENABLE_CHANGE_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOG_ENABLE_CHANGE_REQ",
	[MSG_SMS_LOG_ENABLE_CHANGE_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOG_ENABLE_CHANGE_RES",
	[MSG_SMS_SET_MAX_TX_MSG_LEN_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_MAX_TX_MSG_LEN_REQ",
	[MSG_SMS_SET_MAX_TX_MSG_LEN_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_MAX_TX_MSG_LEN_RES",
	[MSG_SMS_SPI_HALFDUPLEX_TOKEN_HOST_TO_DEVICE - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_HALFDUPLEX_TOKEN_HOST_TO_DEVICE",
	[MSG_SMS_SPI_HALFDUPLEX_TOKEN_DEVICE_TO_HOST - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_HALFDUPLEX_TOKEN_DEVICE_TO_HOST",
	[MSG_SMS_BACKGROUND_SCAN_FLAG_CHANGE_REQ     - MSG_TYPE_BASE_VAL] = "MSG_SMS_BACKGROUND_SCAN_FLAG_CHANGE_REQ",
	[MSG_SMS_BACKGROUND_SCAN_FLAG_CHANGE_RES     - MSG_TYPE_BASE_VAL] = "MSG_SMS_BACKGROUND_SCAN_FLAG_CHANGE_RES",
	[MSG_SMS_BACKGROUND_SCAN_SIGNAL_DETECTED_IND - MSG_TYPE_BASE_VAL] = "MSG_SMS_BACKGROUND_SCAN_SIGNAL_DETECTED_IND",
	[MSG_SMS_BACKGROUND_SCAN_NO_SIGNAL_IND       - MSG_TYPE_BASE_VAL] = "MSG_SMS_BACKGROUND_SCAN_NO_SIGNAL_IND",
	[MSG_SMS_CONFIGURE_RF_SWITCH_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_CONFIGURE_RF_SWITCH_REQ",
	[MSG_SMS_CONFIGURE_RF_SWITCH_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_CONFIGURE_RF_SWITCH_RES",
	[MSG_SMS_MRC_PATH_DISCONNECT_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_PATH_DISCONNECT_REQ",
	[MSG_SMS_MRC_PATH_DISCONNECT_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_PATH_DISCONNECT_RES",
	[MSG_SMS_RECEIVE_1SEG_THROUGH_FULLSEG_REQ    - MSG_TYPE_BASE_VAL] = "MSG_SMS_RECEIVE_1SEG_THROUGH_FULLSEG_REQ",
	[MSG_SMS_RECEIVE_1SEG_THROUGH_FULLSEG_RES    - MSG_TYPE_BASE_VAL] = "MSG_SMS_RECEIVE_1SEG_THROUGH_FULLSEG_RES",
	[MSG_SMS_RECEIVE_VHF_VIA_VHF_INPUT_REQ       - MSG_TYPE_BASE_VAL] = "MSG_SMS_RECEIVE_VHF_VIA_VHF_INPUT_REQ",
	[MSG_SMS_RECEIVE_VHF_VIA_VHF_INPUT_RES       - MSG_TYPE_BASE_VAL] = "MSG_SMS_RECEIVE_VHF_VIA_VHF_INPUT_RES",
	[MSG_WR_REG_RFT_REQ                          - MSG_TYPE_BASE_VAL] = "MSG_WR_REG_RFT_REQ",
	[MSG_WR_REG_RFT_RES                          - MSG_TYPE_BASE_VAL] = "MSG_WR_REG_RFT_RES",
	[MSG_RD_REG_RFT_REQ                          - MSG_TYPE_BASE_VAL] = "MSG_RD_REG_RFT_REQ",
	[MSG_RD_REG_RFT_RES                          - MSG_TYPE_BASE_VAL] = "MSG_RD_REG_RFT_RES",
	[MSG_RD_REG_ALL_RFT_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_RD_REG_ALL_RFT_REQ",
	[MSG_RD_REG_ALL_RFT_RES                      - MSG_TYPE_BASE_VAL] = "MSG_RD_REG_ALL_RFT_RES",
	[MSG_HELP_INT                                - MSG_TYPE_BASE_VAL] = "MSG_HELP_INT",
	[MSG_RUN_SCRIPT_INT                          - MSG_TYPE_BASE_VAL] = "MSG_RUN_SCRIPT_INT",
	[MSG_SMS_EWS_INBAND_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_EWS_INBAND_REQ",
	[MSG_SMS_EWS_INBAND_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_EWS_INBAND_RES",
	[MSG_SMS_RFS_SELECT_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_RFS_SELECT_REQ",
	[MSG_SMS_RFS_SELECT_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_RFS_SELECT_RES",
	[MSG_SMS_MB_GET_VER_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_GET_VER_REQ",
	[MSG_SMS_MB_GET_VER_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_GET_VER_RES",
	[MSG_SMS_MB_WRITE_CFGFILE_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_WRITE_CFGFILE_REQ",
	[MSG_SMS_MB_WRITE_CFGFILE_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_WRITE_CFGFILE_RES",
	[MSG_SMS_MB_READ_CFGFILE_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_READ_CFGFILE_REQ",
	[MSG_SMS_MB_READ_CFGFILE_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_READ_CFGFILE_RES",
	[MSG_SMS_RD_MEM_REQ                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_RD_MEM_REQ",
	[MSG_SMS_RD_MEM_RES                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_RD_MEM_RES",
	[MSG_SMS_WR_MEM_REQ                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_WR_MEM_REQ",
	[MSG_SMS_WR_MEM_RES                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_WR_MEM_RES",
	[MSG_SMS_UPDATE_MEM_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_UPDATE_MEM_REQ",
	[MSG_SMS_UPDATE_MEM_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_UPDATE_MEM_RES",
	[MSG_SMS_ISDBT_ENABLE_FULL_PARAMS_SET_REQ    - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_ENABLE_FULL_PARAMS_SET_REQ",
	[MSG_SMS_ISDBT_ENABLE_FULL_PARAMS_SET_RES    - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_ENABLE_FULL_PARAMS_SET_RES",
	[MSG_SMS_RF_TUNE_REQ                         - MSG_TYPE_BASE_VAL] = "MSG_SMS_RF_TUNE_REQ",
	[MSG_SMS_RF_TUNE_RES                         - MSG_TYPE_BASE_VAL] = "MSG_SMS_RF_TUNE_RES",
	[MSG_SMS_ISDBT_ENABLE_HIGH_MOBILITY_REQ      - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_ENABLE_HIGH_MOBILITY_REQ",
	[MSG_SMS_ISDBT_ENABLE_HIGH_MOBILITY_RES      - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_ENABLE_HIGH_MOBILITY_RES",
	[MSG_SMS_ISDBT_SB_RECEPTION_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_SB_RECEPTION_REQ",
	[MSG_SMS_ISDBT_SB_RECEPTION_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_SB_RECEPTION_RES",
	[MSG_SMS_GENERIC_EPROM_WRITE_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_EPROM_WRITE_REQ",
	[MSG_SMS_GENERIC_EPROM_WRITE_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_EPROM_WRITE_RES",
	[MSG_SMS_GENERIC_EPROM_READ_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_EPROM_READ_REQ",
	[MSG_SMS_GENERIC_EPROM_READ_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_EPROM_READ_RES",
	[MSG_SMS_EEPROM_WRITE_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_EEPROM_WRITE_REQ",
	[MSG_SMS_EEPROM_WRITE_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_EEPROM_WRITE_RES",
	[MSG_SMS_CUSTOM_READ_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_CUSTOM_READ_REQ",
	[MSG_SMS_CUSTOM_READ_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_CUSTOM_READ_RES",
	[MSG_SMS_CUSTOM_WRITE_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_CUSTOM_WRITE_REQ",
	[MSG_SMS_CUSTOM_WRITE_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_CUSTOM_WRITE_RES",
	[MSG_SMS_INIT_DEVICE_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_INIT_DEVICE_REQ",
	[MSG_SMS_INIT_DEVICE_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_INIT_DEVICE_RES",
	[MSG_SMS_ATSC_SET_ALL_IP_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_SET_ALL_IP_REQ",
	[MSG_SMS_ATSC_SET_ALL_IP_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_SET_ALL_IP_RES",
	[MSG_SMS_ATSC_START_ENSEMBLE_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_START_ENSEMBLE_REQ",
	[MSG_SMS_ATSC_START_ENSEMBLE_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_START_ENSEMBLE_RES",
	[MSG_SMS_SET_OUTPUT_MODE_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_OUTPUT_MODE_REQ",
	[MSG_SMS_SET_OUTPUT_MODE_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_OUTPUT_MODE_RES",
	[MSG_SMS_ATSC_IP_FILTER_GET_LIST_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_GET_LIST_REQ",
	[MSG_SMS_ATSC_IP_FILTER_GET_LIST_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_GET_LIST_RES",
	[MSG_SMS_SUB_CHANNEL_START_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SUB_CHANNEL_START_REQ",
	[MSG_SMS_SUB_CHANNEL_START_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SUB_CHANNEL_START_RES",
	[MSG_SMS_SUB_CHANNEL_STOP_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SUB_CHANNEL_STOP_REQ",
	[MSG_SMS_SUB_CHANNEL_STOP_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SUB_CHANNEL_STOP_RES",
	[MSG_SMS_ATSC_IP_FILTER_ADD_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_ADD_REQ",
	[MSG_SMS_ATSC_IP_FILTER_ADD_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_ADD_RES",
	[MSG_SMS_ATSC_IP_FILTER_REMOVE_REQ           - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_REMOVE_REQ",
	[MSG_SMS_ATSC_IP_FILTER_REMOVE_RES           - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_REMOVE_RES",
	[MSG_SMS_ATSC_IP_FILTER_REMOVE_ALL_REQ       - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_REMOVE_ALL_REQ",
	[MSG_SMS_ATSC_IP_FILTER_REMOVE_ALL_RES       - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_REMOVE_ALL_RES",
	[MSG_SMS_WAIT_CMD                            - MSG_TYPE_BASE_VAL] = "MSG_SMS_WAIT_CMD",
	[MSG_SMS_ADD_PID_FILTER_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_ADD_PID_FILTER_REQ",
	[MSG_SMS_ADD_PID_FILTER_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_ADD_PID_FILTER_RES",
	[MSG_SMS_REMOVE_PID_FILTER_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_REMOVE_PID_FILTER_REQ",
	[MSG_SMS_REMOVE_PID_FILTER_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_REMOVE_PID_FILTER_RES",
	[MSG_SMS_FAST_INFORMATION_CHANNEL_REQ        - MSG_TYPE_BASE_VAL] = "MSG_SMS_FAST_INFORMATION_CHANNEL_REQ",
	[MSG_SMS_FAST_INFORMATION_CHANNEL_RES        - MSG_TYPE_BASE_VAL] = "MSG_SMS_FAST_INFORMATION_CHANNEL_RES",
	[MSG_SMS_DAB_CHANNEL                         - MSG_TYPE_BASE_VAL] = "MSG_SMS_DAB_CHANNEL",
	[MSG_SMS_GET_PID_FILTER_LIST_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_PID_FILTER_LIST_REQ",
	[MSG_SMS_GET_PID_FILTER_LIST_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_PID_FILTER_LIST_RES",
	[MSG_SMS_POWER_DOWN_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_DOWN_REQ",
	[MSG_SMS_POWER_DOWN_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_DOWN_RES",
	[MSG_SMS_ATSC_SLT_EXIST_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_SLT_EXIST_IND",
	[MSG_SMS_ATSC_NO_SLT_IND                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_NO_SLT_IND",
	[MSG_SMS_GET_STATISTICS_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_STATISTICS_REQ",
	[MSG_SMS_GET_STATISTICS_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_STATISTICS_RES",
	[MSG_SMS_SEND_DUMP                           - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_DUMP",
	[MSG_SMS_SCAN_START_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_START_REQ",
	[MSG_SMS_SCAN_START_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_START_RES",
	[MSG_SMS_SCAN_STOP_REQ                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_STOP_REQ",
	[MSG_SMS_SCAN_STOP_RES                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_STOP_RES",
	[MSG_SMS_SCAN_PROGRESS_IND                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_PROGRESS_IND",
	[MSG_SMS_SCAN_COMPLETE_IND                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_COMPLETE_IND",
	[MSG_SMS_LOG_ITEM                            - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOG_ITEM",
	[MSG_SMS_DAB_SUBCHANNEL_RECONFIG_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_DAB_SUBCHANNEL_RECONFIG_REQ",
	[MSG_SMS_DAB_SUBCHANNEL_RECONFIG_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_DAB_SUBCHANNEL_RECONFIG_RES",
	[MSG_SMS_HO_PER_SLICES_IND                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_PER_SLICES_IND",
	[MSG_SMS_HO_INBAND_POWER_IND                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_INBAND_POWER_IND",
	[MSG_SMS_MANUAL_DEMOD_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_MANUAL_DEMOD_REQ",
	[MSG_SMS_HO_TUNE_ON_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_TUNE_ON_REQ",
	[MSG_SMS_HO_TUNE_ON_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_TUNE_ON_RES",
	[MSG_SMS_HO_TUNE_OFF_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_TUNE_OFF_REQ",
	[MSG_SMS_HO_TUNE_OFF_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_TUNE_OFF_RES",
	[MSG_SMS_HO_PEEK_FREQ_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_PEEK_FREQ_REQ",
	[MSG_SMS_HO_PEEK_FREQ_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_PEEK_FREQ_RES",
	[MSG_SMS_HO_PEEK_FREQ_IND                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_PEEK_FREQ_IND",
	[MSG_SMS_MB_ATTEN_SET_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_ATTEN_SET_REQ",
	[MSG_SMS_MB_ATTEN_SET_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_ATTEN_SET_RES",
	[MSG_SMS_ENABLE_STAT_IN_I2C_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ENABLE_STAT_IN_I2C_REQ",
	[MSG_SMS_ENABLE_STAT_IN_I2C_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ENABLE_STAT_IN_I2C_RES",
	[MSG_SMS_SET_ANTENNA_CONFIG_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_ANTENNA_CONFIG_REQ",
	[MSG_SMS_SET_ANTENNA_CONFIG_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_ANTENNA_CONFIG_RES",
	[MSG_SMS_GET_STATISTICS_EX_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_STATISTICS_EX_REQ",
	[MSG_SMS_GET_STATISTICS_EX_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_STATISTICS_EX_RES",
	[MSG_SMS_SLEEP_RESUME_COMP_IND               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SLEEP_RESUME_COMP_IND",
	[MSG_SMS_SWITCH_HOST_INTERFACE_REQ           - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWITCH_HOST_INTERFACE_REQ",
	[MSG_SMS_SWITCH_HOST_INTERFACE_RES           - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWITCH_HOST_INTERFACE_RES",
	[MSG_SMS_DATA_DOWNLOAD_REQ                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_DOWNLOAD_REQ",
	[MSG_SMS_DATA_DOWNLOAD_RES                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_DOWNLOAD_RES",
	[MSG_SMS_DATA_VALIDITY_REQ                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_VALIDITY_REQ",
	[MSG_SMS_DATA_VALIDITY_RES                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_VALIDITY_RES",
	[MSG_SMS_SWDOWNLOAD_TRIGGER_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWDOWNLOAD_TRIGGER_REQ",
	[MSG_SMS_SWDOWNLOAD_TRIGGER_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWDOWNLOAD_TRIGGER_RES",
	[MSG_SMS_SWDOWNLOAD_BACKDOOR_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWDOWNLOAD_BACKDOOR_REQ",
	[MSG_SMS_SWDOWNLOAD_BACKDOOR_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWDOWNLOAD_BACKDOOR_RES",
	[MSG_SMS_GET_VERSION_EX_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_VERSION_EX_REQ",
	[MSG_SMS_GET_VERSION_EX_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_VERSION_EX_RES",
	[MSG_SMS_CLOCK_OUTPUT_CONFIG_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_CLOCK_OUTPUT_CONFIG_REQ",
	[MSG_SMS_CLOCK_OUTPUT_CONFIG_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_CLOCK_OUTPUT_CONFIG_RES",
	[MSG_SMS_I2C_SET_FREQ_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_I2C_SET_FREQ_REQ",
	[MSG_SMS_I2C_SET_FREQ_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_I2C_SET_FREQ_RES",
	[MSG_SMS_GENERIC_I2C_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_I2C_REQ",
	[MSG_SMS_GENERIC_I2C_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_I2C_RES",
	[MSG_SMS_DVBT_BDA_DATA                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_DVBT_BDA_DATA",
	[MSG_SW_RELOAD_REQ                           - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_REQ",
	[MSG_SMS_DATA_MSG                            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_MSG",
	[MSG_TABLE_UPLOAD_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_TABLE_UPLOAD_REQ",
	[MSG_TABLE_UPLOAD_RES                        - MSG_TYPE_BASE_VAL] = "MSG_TABLE_UPLOAD_RES",
	[MSG_SW_RELOAD_START_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_START_REQ",
	[MSG_SW_RELOAD_START_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_START_RES",
	[MSG_SW_RELOAD_EXEC_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_EXEC_REQ",
	[MSG_SW_RELOAD_EXEC_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_EXEC_RES",
	[MSG_SMS_SPI_INT_LINE_SET_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_INT_LINE_SET_REQ",
	[MSG_SMS_SPI_INT_LINE_SET_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_INT_LINE_SET_RES",
	[MSG_SMS_GPIO_CONFIG_EX_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_CONFIG_EX_REQ",
	[MSG_SMS_GPIO_CONFIG_EX_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_CONFIG_EX_RES",
	[MSG_SMS_WATCHDOG_ACT_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_WATCHDOG_ACT_REQ",
	[MSG_SMS_WATCHDOG_ACT_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_WATCHDOG_ACT_RES",
	[MSG_SMS_LOOPBACK_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOOPBACK_REQ",
	[MSG_SMS_LOOPBACK_RES                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOOPBACK_RES",
	[MSG_SMS_RAW_CAPTURE_START_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_START_REQ",
	[MSG_SMS_RAW_CAPTURE_START_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_START_RES",
	[MSG_SMS_RAW_CAPTURE_ABORT_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_ABORT_REQ",
	[MSG_SMS_RAW_CAPTURE_ABORT_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_ABORT_RES",
	[MSG_SMS_RAW_CAPTURE_COMPLETE_IND            - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_COMPLETE_IND",
	[MSG_SMS_DATA_PUMP_IND                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_PUMP_IND",
	[MSG_SMS_DATA_PUMP_REQ                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_PUMP_REQ",
	[MSG_SMS_DATA_PUMP_RES                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_PUMP_RES",
	[MSG_SMS_FLASH_DL_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_FLASH_DL_REQ",
	[MSG_SMS_EXEC_TEST_1_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXEC_TEST_1_REQ",
	[MSG_SMS_EXEC_TEST_1_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXEC_TEST_1_RES",
	[MSG_SMS_ENBALE_TS_INTERFACE_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_ENBALE_TS_INTERFACE_REQ",
	[MSG_SMS_ENBALE_TS_INTERFACE_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_ENBALE_TS_INTERFACE_RES",
	[MSG_SMS_SPI_SET_BUS_WIDTH_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_SET_BUS_WIDTH_REQ",
	[MSG_SMS_SPI_SET_BUS_WIDTH_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_SET_BUS_WIDTH_RES",
	[MSG_SMS_SEND_EMM_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_EMM_REQ",
	[MSG_SMS_SEND_EMM_RES                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_EMM_RES",
	[MSG_SMS_DISABLE_TS_INTERFACE_REQ            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DISABLE_TS_INTERFACE_REQ",
	[MSG_SMS_DISABLE_TS_INTERFACE_RES            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DISABLE_TS_INTERFACE_RES",
	[MSG_SMS_IS_BUF_FREE_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_IS_BUF_FREE_REQ",
	[MSG_SMS_IS_BUF_FREE_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_IS_BUF_FREE_RES",
	[MSG_SMS_EXT_ANTENNA_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXT_ANTENNA_REQ",
	[MSG_SMS_EXT_ANTENNA_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXT_ANTENNA_RES",
	[MSG_SMS_CMMB_GET_NET_OF_FREQ_REQ_OBSOLETE   - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_NET_OF_FREQ_REQ_OBSOLETE",
	[MSG_SMS_CMMB_GET_NET_OF_FREQ_RES_OBSOLETE   - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_NET_OF_FREQ_RES_OBSOLETE",
	[MSG_SMS_BATTERY_LEVEL_REQ                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_BATTERY_LEVEL_REQ",
	[MSG_SMS_BATTERY_LEVEL_RES                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_BATTERY_LEVEL_RES",
	[MSG_SMS_CMMB_INJECT_TABLE_REQ_OBSOLETE      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_INJECT_TABLE_REQ_OBSOLETE",
	[MSG_SMS_CMMB_INJECT_TABLE_RES_OBSOLETE      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_INJECT_TABLE_RES_OBSOLETE",
	[MSG_SMS_FM_RADIO_BLOCK_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_FM_RADIO_BLOCK_IND",
	[MSG_SMS_HOST_NOTIFICATION_IND               - MSG_TYPE_BASE_VAL] = "MSG_SMS_HOST_NOTIFICATION_IND",
	[MSG_SMS_CMMB_GET_CONTROL_TABLE_REQ_OBSOLETE - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_CONTROL_TABLE_REQ_OBSOLETE",
	[MSG_SMS_CMMB_GET_CONTROL_TABLE_RES_OBSOLETE - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_CONTROL_TABLE_RES_OBSOLETE",
	[MSG_SMS_CMMB_GET_NETWORKS_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_NETWORKS_REQ",
	[MSG_SMS_CMMB_GET_NETWORKS_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_NETWORKS_RES",
	[MSG_SMS_CMMB_START_SERVICE_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_START_SERVICE_REQ",
	[MSG_SMS_CMMB_START_SERVICE_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_START_SERVICE_RES",
	[MSG_SMS_CMMB_STOP_SERVICE_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_STOP_SERVICE_REQ",
	[MSG_SMS_CMMB_STOP_SERVICE_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_STOP_SERVICE_RES",
	[MSG_SMS_CMMB_ADD_CHANNEL_FILTER_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_ADD_CHANNEL_FILTER_REQ",
	[MSG_SMS_CMMB_ADD_CHANNEL_FILTER_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_ADD_CHANNEL_FILTER_RES",
	[MSG_SMS_CMMB_REMOVE_CHANNEL_FILTER_REQ      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_REMOVE_CHANNEL_FILTER_REQ",
	[MSG_SMS_CMMB_REMOVE_CHANNEL_FILTER_RES      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_REMOVE_CHANNEL_FILTER_RES",
	[MSG_SMS_CMMB_START_CONTROL_INFO_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_START_CONTROL_INFO_REQ",
	[MSG_SMS_CMMB_START_CONTROL_INFO_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_START_CONTROL_INFO_RES",
	[MSG_SMS_CMMB_STOP_CONTROL_INFO_REQ          - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_STOP_CONTROL_INFO_REQ",
	[MSG_SMS_CMMB_STOP_CONTROL_INFO_RES          - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_STOP_CONTROL_INFO_RES",
	[MSG_SMS_ISDBT_TUNE_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_TUNE_REQ",
	[MSG_SMS_ISDBT_TUNE_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_TUNE_RES",
	[MSG_SMS_TRANSMISSION_IND                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_TRANSMISSION_IND",
	[MSG_SMS_PID_STATISTICS_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_PID_STATISTICS_IND",
	[MSG_SMS_POWER_DOWN_IND                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_DOWN_IND",
	[MSG_SMS_POWER_DOWN_CONF                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_DOWN_CONF",
	[MSG_SMS_POWER_UP_IND                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_UP_IND",
	[MSG_SMS_POWER_UP_CONF                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_UP_CONF",
	[MSG_SMS_POWER_MODE_SET_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_MODE_SET_REQ",
	[MSG_SMS_POWER_MODE_SET_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_MODE_SET_RES",
	[MSG_SMS_DEBUG_HOST_EVENT_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DEBUG_HOST_EVENT_REQ",
	[MSG_SMS_DEBUG_HOST_EVENT_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DEBUG_HOST_EVENT_RES",
	[MSG_SMS_NEW_CRYSTAL_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_NEW_CRYSTAL_REQ",
	[MSG_SMS_NEW_CRYSTAL_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_NEW_CRYSTAL_RES",
	[MSG_SMS_CONFIG_SPI_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CONFIG_SPI_REQ",
	[MSG_SMS_CONFIG_SPI_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CONFIG_SPI_RES",
	[MSG_SMS_I2C_SHORT_STAT_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_I2C_SHORT_STAT_IND",
	[MSG_SMS_START_IR_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_START_IR_REQ",
	[MSG_SMS_START_IR_RES                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_START_IR_RES",
	[MSG_SMS_IR_SAMPLES_IND                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_IR_SAMPLES_IND",
	[MSG_SMS_CMMB_CA_SERVICE_IND                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_CA_SERVICE_IND",
	[MSG_SMS_SLAVE_DEVICE_DETECTED               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SLAVE_DEVICE_DETECTED",
	[MSG_SMS_INTERFACE_LOCK_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_INTERFACE_LOCK_IND",
	[MSG_SMS_INTERFACE_UNLOCK_IND                - MSG_TYPE_BASE_VAL] = "MSG_SMS_INTERFACE_UNLOCK_IND",
	[MSG_SMS_SEND_ROSUM_BUFF_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_ROSUM_BUFF_REQ",
	[MSG_SMS_SEND_ROSUM_BUFF_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_ROSUM_BUFF_RES",
	[MSG_SMS_ROSUM_BUFF                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_ROSUM_BUFF",
	[MSG_SMS_SET_AES128_KEY_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_AES128_KEY_REQ",
	[MSG_SMS_SET_AES128_KEY_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_AES128_KEY_RES",
	[MSG_SMS_MBBMS_WRITE_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_MBBMS_WRITE_REQ",
	[MSG_SMS_MBBMS_WRITE_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_MBBMS_WRITE_RES",
	[MSG_SMS_MBBMS_READ_IND                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_MBBMS_READ_IND",
	[MSG_SMS_IQ_STREAM_START_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_START_REQ",
	[MSG_SMS_IQ_STREAM_START_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_START_RES",
	[MSG_SMS_IQ_STREAM_STOP_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_STOP_REQ",
	[MSG_SMS_IQ_STREAM_STOP_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_STOP_RES",
	[MSG_SMS_IQ_STREAM_DATA_BLOCK                - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_DATA_BLOCK",
	[MSG_SMS_GET_EEPROM_VERSION_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_EEPROM_VERSION_REQ",
	[MSG_SMS_GET_EEPROM_VERSION_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_EEPROM_VERSION_RES",
	[MSG_SMS_SIGNAL_DETECTED_IND                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SIGNAL_DETECTED_IND",
	[MSG_SMS_NO_SIGNAL_IND                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_NO_SIGNAL_IND",
	[MSG_SMS_MRC_SHUTDOWN_SLAVE_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_SHUTDOWN_SLAVE_REQ",
	[MSG_SMS_MRC_SHUTDOWN_SLAVE_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_SHUTDOWN_SLAVE_RES",
	[MSG_SMS_MRC_BRINGUP_SLAVE_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_BRINGUP_SLAVE_REQ",
	[MSG_SMS_MRC_BRINGUP_SLAVE_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_BRINGUP_SLAVE_RES",
	[MSG_SMS_EXTERNAL_LNA_CTRL_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXTERNAL_LNA_CTRL_REQ",
	[MSG_SMS_EXTERNAL_LNA_CTRL_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXTERNAL_LNA_CTRL_RES",
	[MSG_SMS_SET_PERIODIC_STATISTICS_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_PERIODIC_STATISTICS_REQ",
	[MSG_SMS_SET_PERIODIC_STATISTICS_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_PERIODIC_STATISTICS_RES",
	[MSG_SMS_CMMB_SET_AUTO_OUTPUT_TS0_REQ        - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_AUTO_OUTPUT_TS0_REQ",
	[MSG_SMS_CMMB_SET_AUTO_OUTPUT_TS0_RES        - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_AUTO_OUTPUT_TS0_RES",
	[LOCAL_TUNE                                  - MSG_TYPE_BASE_VAL] = "LOCAL_TUNE",
	[LOCAL_IFFT_H_ICI                            - MSG_TYPE_BASE_VAL] = "LOCAL_IFFT_H_ICI",
	[MSG_RESYNC_REQ                              - MSG_TYPE_BASE_VAL] = "MSG_RESYNC_REQ",
	[MSG_SMS_CMMB_GET_MRC_STATISTICS_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_MRC_STATISTICS_REQ",
	[MSG_SMS_CMMB_GET_MRC_STATISTICS_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_MRC_STATISTICS_RES",
	[MSG_SMS_LOG_EX_ITEM                         - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOG_EX_ITEM",
	[MSG_SMS_DEVICE_DATA_LOSS_IND                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DEVICE_DATA_LOSS_IND",
	[MSG_SMS_MRC_WATCHDOG_TRIGGERED_IND          - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_WATCHDOG_TRIGGERED_IND",
	[MSG_SMS_USER_MSG_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_USER_MSG_REQ",
	[MSG_SMS_USER_MSG_RES                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_USER_MSG_RES",
	[MSG_SMS_SMART_CARD_INIT_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_INIT_REQ",
	[MSG_SMS_SMART_CARD_INIT_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_INIT_RES",
	[MSG_SMS_SMART_CARD_WRITE_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_WRITE_REQ",
	[MSG_SMS_SMART_CARD_WRITE_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_WRITE_RES",
	[MSG_SMS_SMART_CARD_READ_IND                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_READ_IND",
	[MSG_SMS_TSE_ENABLE_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_TSE_ENABLE_REQ",
	[MSG_SMS_TSE_ENABLE_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_TSE_ENABLE_RES",
	[MSG_SMS_CMMB_GET_SHORT_STATISTICS_REQ       - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_SHORT_STATISTICS_REQ",
	[MSG_SMS_CMMB_GET_SHORT_STATISTICS_RES       - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_SHORT_STATISTICS_RES",
	[MSG_SMS_LED_CONFIG_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_LED_CONFIG_REQ",
	[MSG_SMS_LED_CONFIG_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_LED_CONFIG_RES",
	[MSG_PWM_ANTENNA_REQ                         - MSG_TYPE_BASE_VAL] = "MSG_PWM_ANTENNA_REQ",
	[MSG_PWM_ANTENNA_RES                         - MSG_TYPE_BASE_VAL] = "MSG_PWM_ANTENNA_RES",
	[MSG_SMS_CMMB_SMD_SN_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SMD_SN_REQ",
	[MSG_SMS_CMMB_SMD_SN_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SMD_SN_RES",
	[MSG_SMS_CMMB_SET_CA_CW_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_CA_CW_REQ",
	[MSG_SMS_CMMB_SET_CA_CW_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_CA_CW_RES",
	[MSG_SMS_CMMB_SET_CA_SALT_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_CA_SALT_REQ",
	[MSG_SMS_CMMB_SET_CA_SALT_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_CA_SALT_RES",
	[MSG_SMS_NSCD_INIT_REQ                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_INIT_REQ",
	[MSG_SMS_NSCD_INIT_RES                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_INIT_RES",
	[MSG_SMS_NSCD_PROCESS_SECTION_REQ            - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_PROCESS_SECTION_REQ",
	[MSG_SMS_NSCD_PROCESS_SECTION_RES            - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_PROCESS_SECTION_RES",
	[MSG_SMS_DBD_CREATE_OBJECT_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_CREATE_OBJECT_REQ",
	[MSG_SMS_DBD_CREATE_OBJECT_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_CREATE_OBJECT_RES",
	[MSG_SMS_DBD_CONFIGURE_REQ                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_CONFIGURE_REQ",
	[MSG_SMS_DBD_CONFIGURE_RES                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_CONFIGURE_RES",
	[MSG_SMS_DBD_SET_KEYS_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_SET_KEYS_REQ",
	[MSG_SMS_DBD_SET_KEYS_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_SET_KEYS_RES",
	[MSG_SMS_DBD_PROCESS_HEADER_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_HEADER_REQ",
	[MSG_SMS_DBD_PROCESS_HEADER_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_HEADER_RES",
	[MSG_SMS_DBD_PROCESS_DATA_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_DATA_REQ",
	[MSG_SMS_DBD_PROCESS_DATA_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_DATA_RES",
	[MSG_SMS_DBD_PROCESS_GET_DATA_REQ            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_GET_DATA_REQ",
	[MSG_SMS_DBD_PROCESS_GET_DATA_RES            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_GET_DATA_RES",
	[MSG_SMS_NSCD_OPEN_SESSION_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_OPEN_SESSION_REQ",
	[MSG_SMS_NSCD_OPEN_SESSION_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_OPEN_SESSION_RES",
	[MSG_SMS_SEND_HOST_DATA_TO_DEMUX_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_HOST_DATA_TO_DEMUX_REQ",
	[MSG_SMS_SEND_HOST_DATA_TO_DEMUX_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_HOST_DATA_TO_DEMUX_RES",
	[MSG_LAST_MSG_TYPE                           - MSG_TYPE_BASE_VAL] = "MSG_LAST_MSG_TYPE",
};

char *smscore_translate_msg(enum msg_types msgtype)
{
	int i = msgtype - MSG_TYPE_BASE_VAL;
	char *msg;

	if (i < 0 || i > ARRAY_SIZE(siano_msgs))
		return "Unknown msg type";

	msg = siano_msgs[i];

	if (!*msg)
		return "Unknown msg type";

	return msg;
}
EXPORT_SYMBOL_GPL(smscore_translate_msg);

404 405 406 407 408
void smscore_set_board_id(struct smscore_device_t *core, int id)
{
	core->board_id = id;
}

409 410 411 412 413 414
int smscore_led_state(struct smscore_device_t *core, int led)
{
	if (led >= 0)
		core->led_state = led;
	return core->led_state;
}
415
EXPORT_SYMBOL_GPL(smscore_set_board_id);
416

417 418 419 420
int smscore_get_board_id(struct smscore_device_t *core)
{
	return core->board_id;
}
421
EXPORT_SYMBOL_GPL(smscore_get_board_id);
422

423
struct smscore_registry_entry_t {
424 425 426
	struct list_head entry;
	char			devpath[32];
	int				mode;
427 428
	enum sms_device_type_st	type;
};
429

430 431 432
static struct list_head g_smscore_notifyees;
static struct list_head g_smscore_devices;
static struct mutex g_smscore_deviceslock;
433

434 435
static struct list_head g_smscore_registry;
static struct mutex g_smscore_registrylock;
436

437
static int default_mode = DEVICE_MODE_NONE;
438

439 440 441
module_param(default_mode, int, 0644);
MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");

442
static struct smscore_registry_entry_t *smscore_find_registry(char *devpath)
443
{
444
	struct smscore_registry_entry_t *entry;
445 446 447
	struct list_head *next;

	kmutex_lock(&g_smscore_registrylock);
448 449 450
	for (next = g_smscore_registry.next;
	     next != &g_smscore_registry;
	     next = next->next) {
451
		entry = (struct smscore_registry_entry_t *) next;
452
		if (!strcmp(entry->devpath, devpath)) {
453
			kmutex_unlock(&g_smscore_registrylock);
454
			return entry;
455 456
		}
	}
457
	entry = kmalloc(sizeof(struct smscore_registry_entry_t), GFP_KERNEL);
458
	if (entry) {
459 460 461
		entry->mode = default_mode;
		strcpy(entry->devpath, devpath);
		list_add(&entry->entry, &g_smscore_registry);
462
	} else
463
		sms_err("failed to create smscore_registry.");
464
	kmutex_unlock(&g_smscore_registrylock);
465 466
	return entry;
}
467

468
int smscore_registry_getmode(char *devpath)
469
{
470
	struct smscore_registry_entry_t *entry;
471

472 473
	entry = smscore_find_registry(devpath);
	if (entry)
474 475
		return entry->mode;
	else
476
		sms_err("No registry found.");
477

478 479
	return default_mode;
}
480
EXPORT_SYMBOL_GPL(smscore_registry_getmode);
481

482
static enum sms_device_type_st smscore_registry_gettype(char *devpath)
483
{
484
	struct smscore_registry_entry_t *entry;
485

486 487
	entry = smscore_find_registry(devpath);
	if (entry)
488 489
		return entry->type;
	else
490
		sms_err("No registry found.");
491

492
	return -EINVAL;
493
}
494

495
static void smscore_registry_setmode(char *devpath, int mode)
496
{
497
	struct smscore_registry_entry_t *entry;
498

499 500 501
	entry = smscore_find_registry(devpath);
	if (entry)
		entry->mode = mode;
502
	else
503
		sms_err("No registry found.");
504
}
505

506 507
static void smscore_registry_settype(char *devpath,
				     enum sms_device_type_st type)
508
{
509
	struct smscore_registry_entry_t *entry;
510

511 512
	entry = smscore_find_registry(devpath);
	if (entry)
513 514
		entry->type = type;
	else
515
		sms_err("No registry found.");
516 517 518
}


519 520
static void list_add_locked(struct list_head *new, struct list_head *head,
			    spinlock_t *lock)
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
{
	unsigned long flags;

	spin_lock_irqsave(lock, flags);

	list_add(new, head);

	spin_unlock_irqrestore(lock, flags);
}

/**
 * register a client callback that called when device plugged in/unplugged
 * NOTE: if devices exist callback is called immediately for each device
 *
 * @param hotplug callback
 *
 * @return 0 on success, <0 on error.
 */
int smscore_register_hotplug(hotplug_t hotplug)
{
541
	struct smscore_device_notifyee_t *notifyee;
542 543 544 545 546
	struct list_head *next, *first;
	int rc = 0;

	kmutex_lock(&g_smscore_deviceslock);

547 548
	notifyee = kmalloc(sizeof(struct smscore_device_notifyee_t),
			   GFP_KERNEL);
549 550
	if (notifyee) {
		/* now notify callback about existing devices */
551
		first = &g_smscore_devices;
552 553 554
		for (next = first->next;
		     next != first && !rc;
		     next = next->next) {
555 556
			struct smscore_device_t *coredev =
				(struct smscore_device_t *) next;
557 558 559
			rc = hotplug(coredev, coredev->device, 1);
		}

560
		if (rc >= 0) {
561 562
			notifyee->hotplug = hotplug;
			list_add(&notifyee->entry, &g_smscore_notifyees);
563
		} else
564
			kfree(notifyee);
565
	} else
566 567 568 569 570 571
		rc = -ENOMEM;

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}
572
EXPORT_SYMBOL_GPL(smscore_register_hotplug);
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

/**
 * unregister a client callback that called when device plugged in/unplugged
 *
 * @param hotplug callback
 *
 */
void smscore_unregister_hotplug(hotplug_t hotplug)
{
	struct list_head *next, *first;

	kmutex_lock(&g_smscore_deviceslock);

	first = &g_smscore_notifyees;

588
	for (next = first->next; next != first;) {
589 590
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *) next;
591 592
		next = next->next;

593
		if (notifyee->hotplug == hotplug) {
594 595 596 597 598 599 600
			list_del(&notifyee->entry);
			kfree(notifyee);
		}
	}

	kmutex_unlock(&g_smscore_deviceslock);
}
601
EXPORT_SYMBOL_GPL(smscore_unregister_hotplug);
602

603
static void smscore_notify_clients(struct smscore_device_t *coredev)
604
{
605
	struct smscore_client_t *client;
606

607 608
	/* the client must call smscore_unregister_client from remove handler */
	while (!list_empty(&coredev->clients)) {
609
		client = (struct smscore_client_t *) coredev->clients.next;
610 611 612 613
		client->onremove_handler(client->context);
	}
}

614 615
static int smscore_notify_callbacks(struct smscore_device_t *coredev,
				    struct device *device, int arrival)
616
{
617
	struct smscore_device_notifyee_t *elem;
618 619
	int rc = 0;

620
	/* note: must be called under g_deviceslock */
621

622 623
	list_for_each_entry(elem, &g_smscore_notifyees, entry) {
		rc = elem->hotplug(coredev, device, arrival);
624 625 626 627 628 629 630
		if (rc < 0)
			break;
	}

	return rc;
}

631 632
static struct
smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
633
				       dma_addr_t common_buffer_phys)
634
{
635 636 637
	struct smscore_buffer_t *cb;

	cb = kzalloc(sizeof(struct smscore_buffer_t), GFP_KERNEL);
638
	if (!cb) {
639
		sms_info("kzalloc(...) failed");
640 641 642 643
		return NULL;
	}

	cb->p = buffer;
644
	cb->offset_in_common = buffer - (u8 *) common_buffer;
645 646 647 648 649 650
	cb->phys = common_buffer_phys + cb->offset_in_common;

	return cb;
}

/**
651 652
 * creates coredev object for a device, prepares buffers,
 * creates buffer mappings, notifies registered hotplugs about new device.
653
 *
654 655
 * @param params device pointer to struct with device specific parameters
 *               and handlers
656 657 658 659
 * @param coredev pointer to a value that receives created coredev object
 *
 * @return 0 on success, <0 on error.
 */
660 661
int smscore_register_device(struct smsdevice_params_t *params,
			    struct smscore_device_t **coredev)
662
{
663
	struct smscore_device_t *dev;
664 665
	u8 *buffer;

666
	dev = kzalloc(sizeof(struct smscore_device_t), GFP_KERNEL);
667
	if (!dev) {
668
		sms_info("kzalloc(...) failed");
669 670 671
		return -ENOMEM;
	}

672
	/* init list entry so it could be safe in smscore_unregister_device */
673 674
	INIT_LIST_HEAD(&dev->entry);

675
	/* init queues */
676 677 678
	INIT_LIST_HEAD(&dev->clients);
	INIT_LIST_HEAD(&dev->buffers);

679
	/* init locks */
680 681 682
	spin_lock_init(&dev->clientslock);
	spin_lock_init(&dev->bufferslock);

683
	/* init completion events */
684 685
	init_completion(&dev->version_ex_done);
	init_completion(&dev->data_download_done);
686
	init_completion(&dev->data_validity_done);
687 688 689 690
	init_completion(&dev->trigger_done);
	init_completion(&dev->init_device_done);
	init_completion(&dev->reload_start_done);
	init_completion(&dev->resume_done);
691 692 693
	init_completion(&dev->gpio_configuration_done);
	init_completion(&dev->gpio_set_level_done);
	init_completion(&dev->gpio_get_level_done);
694
	init_completion(&dev->ir_init_done);
695

696 697 698
	/* Buffer management */
	init_waitqueue_head(&dev->buffer_mng_waitq);

699
	/* alloc common buffer */
700
	dev->common_buffer_size = params->buffer_size * params->num_buffers;
701 702 703 704
	dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
						&dev->common_buffer_phys,
						GFP_KERNEL | GFP_DMA);
	if (!dev->common_buffer) {
705 706 707 708
		smscore_unregister_device(dev);
		return -ENOMEM;
	}

709 710 711
	/* prepare dma buffers */
	for (buffer = dev->common_buffer;
	     dev->num_buffers < params->num_buffers;
712
	     dev->num_buffers++, buffer += params->buffer_size) {
713 714 715 716
		struct smscore_buffer_t *cb;

		cb = smscore_createbuffer(buffer, dev->common_buffer,
					  dev->common_buffer_phys);
717
		if (!cb) {
718 719 720 721 722 723 724
			smscore_unregister_device(dev);
			return -ENOMEM;
		}

		smscore_putbuffer(dev, cb);
	}

725
	sms_info("allocated %d buffers", dev->num_buffers);
726 727

	dev->mode = DEVICE_MODE_NONE;
728
	dev->board_id = SMS_BOARD_UNKNOWN;
729 730 731 732 733 734 735 736 737 738 739
	dev->context = params->context;
	dev->device = params->device;
	dev->setmode_handler = params->setmode_handler;
	dev->detectmode_handler = params->detectmode_handler;
	dev->sendrequest_handler = params->sendrequest_handler;
	dev->preload_handler = params->preload_handler;
	dev->postload_handler = params->postload_handler;

	dev->device_flags = params->flags;
	strcpy(dev->devpath, params->devpath);

740
	smscore_registry_settype(dev->devpath, params->device_type);
741

742
	/* add device to devices list */
743 744 745 746 747 748
	kmutex_lock(&g_smscore_deviceslock);
	list_add(&dev->entry, &g_smscore_devices);
	kmutex_unlock(&g_smscore_deviceslock);

	*coredev = dev;

749
	sms_info("device %p created", dev);
750 751 752

	return 0;
}
753
EXPORT_SYMBOL_GPL(smscore_register_device);
754

755 756 757

static int smscore_sendrequest_and_wait(struct smscore_device_t *coredev,
		void *buffer, size_t size, struct completion *completion) {
758 759 760 761 762 763 764
	int rc;

	if (completion == NULL)
		return -EINVAL;
	init_completion(completion);

	rc = coredev->sendrequest_handler(coredev->context, buffer, size);
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
	if (rc < 0) {
		sms_info("sendrequest returned error %d", rc);
		return rc;
	}

	return wait_for_completion_timeout(completion,
			msecs_to_jiffies(SMS_PROTOCOL_MAX_RAOUNDTRIP_MS)) ?
			0 : -ETIME;
}

/**
 * Starts & enables IR operations
 *
 * @return 0 on success, < 0 on error.
 */
static int smscore_init_ir(struct smscore_device_t *coredev)
{
	int ir_io;
	int rc;
	void *buffer;

786
	coredev->ir.dev = NULL;
787 788 789 790 791 792 793 794
	ir_io = sms_get_board(smscore_get_board_id(coredev))->board_cfg.ir;
	if (ir_io) {/* only if IR port exist we use IR sub-module */
		sms_info("IR loading");
		rc = sms_ir_init(coredev);

		if	(rc != 0)
			sms_err("Error initialization DTV IR sub-module");
		else {
795
			buffer = kmalloc(sizeof(struct sms_msg_data2) +
796 797 798
						SMS_DMA_ALIGNMENT,
						GFP_KERNEL | GFP_DMA);
			if (buffer) {
799 800
				struct sms_msg_data2 *msg =
				(struct sms_msg_data2 *)
801 802
				SMS_ALIGN_ADDRESS(buffer);

803
				SMS_INIT_MSG(&msg->x_msg_header,
804
						MSG_SMS_START_IR_REQ,
805
						sizeof(struct sms_msg_data2));
806 807
				msg->msg_data[0] = coredev->ir.controller;
				msg->msg_data[1] = coredev->ir.timeout;
808 809

				rc = smscore_sendrequest_and_wait(coredev, msg,
810
						msg->x_msg_header. msg_length,
811 812 813 814 815 816 817 818 819 820 821 822 823
						&coredev->ir_init_done);

				kfree(buffer);
			} else
				sms_err
				("Sending IR initialization message failed");
		}
	} else
		sms_info("IR port has not been detected");

	return 0;
}

824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
/**
 * configures device features according to board configuration structure.
 *
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
 *
 * @return 0 on success, <0 on error.
 */
int smscore_configure_board(struct smscore_device_t *coredev)
{
	struct sms_board *board;

	board = sms_get_board(coredev->board_id);
	if (!board) {
		sms_err("no board configuration exist.");
		return -EINVAL;
	}

	if (board->mtu) {
843
		struct sms_msg_data mtu_msg;
844 845
		sms_debug("set max transmit unit %d", board->mtu);

846 847 848 849 850 851
		mtu_msg.x_msg_header.msg_src_id = 0;
		mtu_msg.x_msg_header.msg_dst_id = HIF_TASK;
		mtu_msg.x_msg_header.msg_flags = 0;
		mtu_msg.x_msg_header.msg_type = MSG_SMS_SET_MAX_TX_MSG_LEN_REQ;
		mtu_msg.x_msg_header.msg_length = sizeof(mtu_msg);
		mtu_msg.msg_data[0] = board->mtu;
852

853 854
		coredev->sendrequest_handler(coredev->context, &mtu_msg,
					     sizeof(mtu_msg));
855 856 857
	}

	if (board->crystal) {
858
		struct sms_msg_data crys_msg;
859 860
		sms_debug("set crystal value %d", board->crystal);

861
		SMS_INIT_MSG(&crys_msg.x_msg_header,
862
				MSG_SMS_NEW_CRYSTAL_REQ,
863 864
				sizeof(crys_msg));
		crys_msg.msg_data[0] = board->crystal;
865

866 867
		coredev->sendrequest_handler(coredev->context, &crys_msg,
					     sizeof(crys_msg));
868 869 870 871 872
	}

	return 0;
}

873 874 875
/**
 * sets initial device mode and notifies client hotplugs that device is ready
 *
876
 * @param coredev pointer to a coredev object returned by
877
 *		  smscore_register_device
878 879 880
 *
 * @return 0 on success, <0 on error.
 */
881
int smscore_start_device(struct smscore_device_t *coredev)
882
{
883 884 885 886 887 888 889 890 891
	int rc;
	int board_id = smscore_get_board_id(coredev);
	int mode = smscore_registry_getmode(coredev->devpath);

	/* Device is initialized as DEVICE_MODE_NONE */
	if (board_id != SMS_BOARD_UNKNOWN && mode == DEVICE_MODE_NONE)
		mode = sms_get_board(board_id)->default_mode;

	rc = smscore_set_device_mode(coredev, mode);
892
	if (rc < 0) {
893
		sms_info("set device mode faile , rc %d", rc);
894
		return rc;
895
	}
896 897 898 899 900
	rc = smscore_configure_board(coredev);
	if (rc < 0) {
		sms_info("configure board failed , rc %d", rc);
		return rc;
	}
901 902 903 904

	kmutex_lock(&g_smscore_deviceslock);

	rc = smscore_notify_callbacks(coredev, coredev->device, 1);
905
	smscore_init_ir(coredev);
906

907
	sms_info("device %p started, rc %d", coredev, rc);
908 909 910 911 912

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}
913
EXPORT_SYMBOL_GPL(smscore_start_device);
914 915


916 917
static int smscore_load_firmware_family2(struct smscore_device_t *coredev,
					 void *buffer, size_t size)
918
{
919 920
	struct sms_firmware *firmware = (struct sms_firmware *) buffer;
	struct sms_msg_data4 *msg;
921 922
	u32 mem_address,  calc_checksum = 0;
	u32 i, *ptr;
923
	u8 *payload = firmware->payload;
924
	int rc = 0;
925 926
	firmware->start_address = le32_to_cpu(firmware->start_address);
	firmware->length = le32_to_cpu(firmware->length);
927

928
	mem_address = firmware->start_address;
929

930
	sms_info("loading FW to addr 0x%x size %d",
931
		 mem_address, firmware->length);
932
	if (coredev->preload_handler) {
933 934 935 936 937
		rc = coredev->preload_handler(coredev->context);
		if (rc < 0)
			return rc;
	}

938
	/* PAGE_SIZE buffer shall be enough and dma aligned */
939
	msg = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
940 941 942
	if (!msg)
		return -ENOMEM;

943
	if (coredev->mode != DEVICE_MODE_NONE) {
944
		sms_debug("sending reload command.");
945 946
		SMS_INIT_MSG(&msg->x_msg_header, MSG_SW_RELOAD_START_REQ,
			     sizeof(struct sms_msg_hdr));
947
		rc = smscore_sendrequest_and_wait(coredev, msg,
948
						  msg->x_msg_header.msg_length,
949
						  &coredev->reload_start_done);
950 951 952 953
		if (rc < 0) {
			sms_err("device reload failed, rc %d", rc);
			goto exit_fw_download;
		}
954
		mem_address = *(u32 *) &payload[20];
955 956
	}

957
	for (i = 0, ptr = (u32 *)firmware->payload; i < firmware->length/4 ;
958 959 960
	     i++, ptr++)
		calc_checksum += *ptr;

961
	while (size && rc >= 0) {
962
		struct sms_data_download *data_msg =
963
			(struct sms_data_download *) msg;
964
		int payload_size = min_t(int, size, SMS_MAX_PAYLOAD_SIZE);
965

966 967
		SMS_INIT_MSG(&msg->x_msg_header, MSG_SMS_DATA_DOWNLOAD_REQ,
			     (u16)(sizeof(struct sms_msg_hdr) +
968
				      sizeof(u32) + payload_size));
969

970 971
		data_msg->mem_addr = mem_address;
		memcpy(data_msg->payload, payload, payload_size);
972

973 974
		rc = smscore_sendrequest_and_wait(coredev, data_msg,
				data_msg->x_msg_header.msg_length,
975
				&coredev->data_download_done);
976 977 978 979 980 981

		payload += payload_size;
		size -= payload_size;
		mem_address += payload_size;
	}

982 983 984 985 986
	if (rc < 0)
		goto exit_fw_download;

	sms_err("sending MSG_SMS_DATA_VALIDITY_REQ expecting 0x%x",
		calc_checksum);
987 988
	SMS_INIT_MSG(&msg->x_msg_header, MSG_SMS_DATA_VALIDITY_REQ,
			sizeof(msg->x_msg_header) +
989
			sizeof(u32) * 3);
990
	msg->msg_data[0] = firmware->start_address;
991
		/* Entry point */
992 993
	msg->msg_data[1] = firmware->length;
	msg->msg_data[2] = 0; /* Regular checksum*/
994
	rc = smscore_sendrequest_and_wait(coredev, msg,
995
					  msg->x_msg_header.msg_length,
996 997 998 999 1000
					  &coredev->data_validity_done);
	if (rc < 0)
		goto exit_fw_download;

	if (coredev->mode == DEVICE_MODE_NONE) {
1001
		struct sms_msg_data *trigger_msg =
1002
			(struct sms_msg_data *) msg;
1003 1004

		sms_debug("sending MSG_SMS_SWDOWNLOAD_TRIGGER_REQ");
1005
		SMS_INIT_MSG(&msg->x_msg_header,
1006
				MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
1007
				sizeof(struct sms_msg_hdr) +
1008 1009
				sizeof(u32) * 5);

1010
		trigger_msg->msg_data[0] = firmware->start_address;
1011
					/* Entry point */
1012 1013 1014 1015
		trigger_msg->msg_data[1] = 6; /* Priority */
		trigger_msg->msg_data[2] = 0x200; /* Stack size */
		trigger_msg->msg_data[3] = 0; /* Parameter */
		trigger_msg->msg_data[4] = 4; /* Task ID */
1016

1017 1018
		rc = smscore_sendrequest_and_wait(coredev, trigger_msg,
					trigger_msg->x_msg_header.msg_length,
1019
					&coredev->trigger_done);
1020
	} else {
1021 1022
		SMS_INIT_MSG(&msg->x_msg_header, MSG_SW_RELOAD_EXEC_REQ,
				sizeof(struct sms_msg_hdr));
1023
		rc = coredev->sendrequest_handler(coredev->context, msg,
1024
				msg->x_msg_header.msg_length);
1025 1026
	}

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
	if (rc < 0)
		goto exit_fw_download;

	/*
	 * backward compatibility - wait to device_ready_done for
	 * not more than 400 ms
	 */
	msleep(400);

exit_fw_download:
1037 1038
	kfree(msg);

1039 1040 1041 1042 1043 1044 1045 1046
	if (coredev->postload_handler) {
		sms_debug("rc=%d, postload=0x%p", rc, coredev->postload_handler);
		if (rc >= 0)
			return coredev->postload_handler(coredev->context);
	}

	sms_debug("rc=%d", rc);
	return rc;
1047 1048
}

1049 1050
static char *smscore_fw_lkup[][DEVICE_MODE_MAX] = {
	[SMS_NOVA_A0] = {
1051 1052 1053 1054 1055 1056
		[DEVICE_MODE_DVBT]		= SMS_FW_DVB_NOVA_12MHZ,
		[DEVICE_MODE_DVBH]		= SMS_FW_DVB_NOVA_12MHZ,
		[DEVICE_MODE_DAB_TDMB]		= SMS_FW_TDMB_NOVA_12MHZ,
		[DEVICE_MODE_DVBT_BDA]		= SMS_FW_DVB_NOVA_12MHZ,
		[DEVICE_MODE_ISDBT]		= SMS_FW_ISDBT_NOVA_12MHZ,
		[DEVICE_MODE_ISDBT_BDA]		= SMS_FW_ISDBT_NOVA_12MHZ,
1057 1058
	},
	[SMS_NOVA_B0] = {
1059 1060 1061 1062 1063 1064 1065 1066
		[DEVICE_MODE_DVBT]		= SMS_FW_DVB_NOVA_12MHZ_B0,
		[DEVICE_MODE_DVBH]		= SMS_FW_DVB_NOVA_12MHZ_B0,
		[DEVICE_MODE_DAB_TDMB]		= SMS_FW_TDMB_NOVA_12MHZ_B0,
		[DEVICE_MODE_DVBT_BDA]		= SMS_FW_DVB_NOVA_12MHZ_B0,
		[DEVICE_MODE_ISDBT]		= SMS_FW_ISDBT_NOVA_12MHZ_B0,
		[DEVICE_MODE_ISDBT_BDA]		= SMS_FW_ISDBT_NOVA_12MHZ_B0,
		[DEVICE_MODE_FM_RADIO]		= SMS_FW_FM_RADIO,
		[DEVICE_MODE_FM_RADIO_BDA]	= SMS_FW_FM_RADIO,
1067 1068
	},
	[SMS_VEGA] = {
1069
		[DEVICE_MODE_CMMB]		= SMS_FW_CMMB_VEGA_12MHZ,
1070 1071
	},
	[SMS_VENICE] = {
1072
		[DEVICE_MODE_CMMB]		= SMS_FW_CMMB_VENICE_12MHZ,
1073 1074
	},
	[SMS_MING] = {
1075
		[DEVICE_MODE_CMMB]		= SMS_FW_CMMB_MING_APP,
1076 1077
	},
	[SMS_PELE] = {
1078 1079
		[DEVICE_MODE_ISDBT]		= SMS_FW_ISDBT_PELE,
		[DEVICE_MODE_ISDBT_BDA]		= SMS_FW_ISDBT_PELE,
1080 1081
	},
	[SMS_RIO] = {
1082 1083 1084 1085 1086 1087 1088
		[DEVICE_MODE_DVBT]		= SMS_FW_DVB_RIO,
		[DEVICE_MODE_DVBH]		= SMS_FW_DVBH_RIO,
		[DEVICE_MODE_DVBT_BDA]		= SMS_FW_DVB_RIO,
		[DEVICE_MODE_ISDBT]		= SMS_FW_ISDBT_RIO,
		[DEVICE_MODE_ISDBT_BDA]		= SMS_FW_ISDBT_RIO,
		[DEVICE_MODE_FM_RADIO]		= SMS_FW_FM_RADIO_RIO,
		[DEVICE_MODE_FM_RADIO_BDA]	= SMS_FW_FM_RADIO_RIO,
1089 1090
	},
	[SMS_DENVER_1530] = {
1091
		[DEVICE_MODE_ATSC]		= SMS_FW_ATSC_DENVER,
1092 1093
	},
	[SMS_DENVER_2160] = {
1094
		[DEVICE_MODE_DAB_TDMB]		= SMS_FW_TDMB_DENVER,
1095 1096
	},
};
1097

1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
/**
 * get firmware file name from one of the two mechanisms : sms_boards or
 * smscore_fw_lkup.
 * @param coredev pointer to a coredev object returned by
 *		  smscore_register_device
 * @param mode requested mode of operation
 * @param lookup if 1, always get the fw filename from smscore_fw_lkup
 *	 table. if 0, try first to get from sms_boards
 *
 * @return 0 on success, <0 on error.
 */
1109
static char *smscore_get_fw_filename(struct smscore_device_t *coredev,
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
			      int mode)
{
	char **fw;
	int board_id = smscore_get_board_id(coredev);
	enum sms_device_type_st type;

	type = smscore_registry_gettype(coredev->devpath);

	/* Prevent looking outside the smscore_fw_lkup table */
	if (type <= SMS_UNKNOWN_TYPE || type >= SMS_NUM_OF_DEVICE_TYPES)
		return NULL;
	if (mode <= DEVICE_MODE_NONE || mode >= DEVICE_MODE_MAX)
		return NULL;

	sms_debug("trying to get fw name from sms_boards board_id %d mode %d",
		  board_id, mode);
	fw = sms_get_board(board_id)->fw;
	if (!fw || !fw[mode]) {
		sms_debug("cannot find fw name in sms_boards, getting from lookup table mode %d type %d",
			  mode, type);
		return smscore_fw_lkup[type][mode];
	}

	return fw[mode];
}
1135

1136 1137 1138
/**
 * loads specified firmware into a buffer and calls device loadfirmware_handler
 *
1139 1140
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1141 1142 1143 1144 1145
 * @param filename null-terminated string specifies firmware file name
 * @param loadfirmware_handler device handler that loads firmware
 *
 * @return 0 on success, <0 on error.
 */
1146
static int smscore_load_firmware_from_file(struct smscore_device_t *coredev,
1147
					   int mode,
1148
					   loadfirmware_t loadfirmware_handler)
1149 1150
{
	int rc = -ENOENT;
1151 1152
	u8 *fw_buf;
	u32 fw_buf_size;
1153 1154
	const struct firmware *fw;

1155
	char *fw_filename = smscore_get_fw_filename(coredev, mode);
1156 1157
	if (!fw_filename) {
		sms_info("mode %d not supported on this device", mode);
1158
		return -ENOENT;
1159 1160
	}
	sms_debug("Firmware name: %s", fw_filename);
1161 1162 1163

	if (loadfirmware_handler == NULL && !(coredev->device_flags
			& SMS_DEVICE_FAMILY2))
1164 1165
		return -EINVAL;

1166
	rc = request_firmware(&fw, fw_filename, coredev->device);
1167
	if (rc < 0) {
1168
		sms_info("failed to open \"%s\"", fw_filename);
1169 1170
		return rc;
	}
1171 1172 1173 1174
	sms_info("read fw %s, buffer size=0x%zx", fw_filename, fw->size);
	fw_buf = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
			 GFP_KERNEL | GFP_DMA);
	if (!fw_buf) {
1175
		sms_info("failed to allocate firmware buffer");
1176
		return -ENOMEM;
1177
	}
1178 1179 1180 1181 1182 1183 1184
	memcpy(fw_buf, fw->data, fw->size);
	fw_buf_size = fw->size;

	rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
		smscore_load_firmware_family2(coredev, fw_buf, fw_buf_size)
		: loadfirmware_handler(coredev->context, fw_buf,
		fw_buf_size);
1185

1186
	kfree(fw_buf);
1187 1188 1189 1190 1191 1192
	release_firmware(fw);

	return rc;
}

/**
1193 1194
 * notifies all clients registered with the device, notifies hotplugs,
 * frees all buffers and coredev object
1195
 *
1196 1197
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1198 1199 1200
 *
 * @return 0 on success, <0 on error.
 */
1201
void smscore_unregister_device(struct smscore_device_t *coredev)
1202
{
1203
	struct smscore_buffer_t *cb;
1204
	int num_buffers = 0;
1205
	int retry = 0;
1206 1207 1208

	kmutex_lock(&g_smscore_deviceslock);

1209 1210 1211
	/* Release input device (IR) resources */
	sms_ir_exit(coredev);

1212 1213 1214
	smscore_notify_clients(coredev);
	smscore_notify_callbacks(coredev, NULL, 0);

1215 1216
	/* at this point all buffers should be back
	 * onresponse must no longer be called */
1217

1218
	while (1) {
1219 1220 1221
		while (!list_empty(&coredev->buffers)) {
			cb = (struct smscore_buffer_t *) coredev->buffers.next;
			list_del(&cb->entry);
1222
			kfree(cb);
1223
			num_buffers++;
1224 1225 1226
		}
		if (num_buffers == coredev->num_buffers)
			break;
1227
		if (++retry > 10) {
1228
			sms_info("exiting although not all buffers released.");
1229 1230
			break;
		}
1231

1232
		sms_info("waiting for %d buffer(s)",
1233
			 coredev->num_buffers - num_buffers);
1234
		kmutex_unlock(&g_smscore_deviceslock);
1235
		msleep(100);
1236
		kmutex_lock(&g_smscore_deviceslock);
1237 1238
	}

1239
	sms_info("freed %d buffers", num_buffers);
1240 1241

	if (coredev->common_buffer)
1242
		dma_free_coherent(NULL, coredev->common_buffer_size,
1243 1244
			coredev->common_buffer, coredev->common_buffer_phys);

1245
	kfree(coredev->fw_buf);
1246 1247 1248 1249 1250 1251

	list_del(&coredev->entry);
	kfree(coredev);

	kmutex_unlock(&g_smscore_deviceslock);

1252
	sms_info("device %p destroyed", coredev);
1253
}
1254
EXPORT_SYMBOL_GPL(smscore_unregister_device);
1255

1256
static int smscore_detect_mode(struct smscore_device_t *coredev)
1257
{
1258
	void *buffer = kmalloc(sizeof(struct sms_msg_hdr) + SMS_DMA_ALIGNMENT,
1259
			       GFP_KERNEL | GFP_DMA);
1260 1261
	struct sms_msg_hdr *msg =
		(struct sms_msg_hdr *) SMS_ALIGN_ADDRESS(buffer);
1262 1263 1264 1265 1266
	int rc;

	if (!buffer)
		return -ENOMEM;

1267
	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
1268
		     sizeof(struct sms_msg_hdr));
1269

1270
	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msg_length,
1271 1272
					  &coredev->version_ex_done);
	if (rc == -ETIME) {
1273
		sms_err("MSG_SMS_GET_VERSION_EX_REQ failed first try");
1274

1275 1276
		if (wait_for_completion_timeout(&coredev->resume_done,
						msecs_to_jiffies(5000))) {
1277
			rc = smscore_sendrequest_and_wait(
1278
				coredev, msg, msg->msg_length,
1279
				&coredev->version_ex_done);
1280
			if (rc < 0)
1281 1282
				sms_err("MSG_SMS_GET_VERSION_EX_REQ failed second try, rc %d",
					rc);
1283
		} else
1284 1285 1286 1287 1288 1289 1290 1291
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
/**
 * send init device request and wait for response
 *
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
 * @param mode requested mode of operation
 *
 * @return 0 on success, <0 on error.
 */
int smscore_init_device(struct smscore_device_t *coredev, int mode)
{
	void *buffer;
1304
	struct sms_msg_data *msg;
1305 1306
	int rc = 0;

1307
	buffer = kmalloc(sizeof(struct sms_msg_data) +
1308 1309 1310 1311 1312 1313
			SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
	if (!buffer) {
		sms_err("Could not allocate buffer for init device message.");
		return -ENOMEM;
	}

1314 1315 1316
	msg = (struct sms_msg_data *)SMS_ALIGN_ADDRESS(buffer);
	SMS_INIT_MSG(&msg->x_msg_header, MSG_SMS_INIT_DEVICE_REQ,
			sizeof(struct sms_msg_data));
1317
	msg->msg_data[0] = mode;
1318 1319

	rc = smscore_sendrequest_and_wait(coredev, msg,
1320
			msg->x_msg_header. msg_length,
1321 1322 1323 1324 1325 1326
			&coredev->init_device_done);

	kfree(buffer);
	return rc;
}

1327 1328 1329 1330
/**
 * calls device handler to change mode of operation
 * NOTE: stellar/usb may disconnect when changing mode
 *
1331 1332
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1333 1334 1335 1336
 * @param mode requested mode of operation
 *
 * @return 0 on success, <0 on error.
 */
1337
int smscore_set_device_mode(struct smscore_device_t *coredev, int mode)
1338 1339 1340
{
	int rc = 0;

1341
	sms_debug("set device mode to %d", mode);
1342
	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
1343
		if (mode <= DEVICE_MODE_NONE || mode >= DEVICE_MODE_MAX) {
1344
			sms_err("invalid mode specified %d", mode);
1345 1346 1347
			return -EINVAL;
		}

1348 1349
		smscore_registry_setmode(coredev->devpath, mode);

1350
		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
1351
			rc = smscore_detect_mode(coredev);
1352
			if (rc < 0) {
1353
				sms_err("mode detect failed %d", rc);
1354
				return rc;
1355
			}
1356
		}
1357

1358
		if (coredev->mode == mode) {
1359
			sms_info("device mode %d already set", mode);
1360 1361 1362
			return 0;
		}

1363
		if (!(coredev->modes_supported & (1 << mode))) {
1364
			rc = smscore_load_firmware_from_file(coredev,
1365
							     mode, NULL);
1366 1367
			if (rc >= 0)
				sms_info("firmware download success");
1368
		} else {
1369 1370
			sms_info("mode %d is already supported by running firmware",
				 mode);
1371
		}
1372 1373 1374 1375 1376
		if (coredev->fw_version >= 0x800) {
			rc = smscore_init_device(coredev, mode);
			if (rc < 0)
				sms_err("device init failed, rc %d.", rc);
		}
1377
	} else {
1378
		if (mode <= DEVICE_MODE_NONE || mode >= DEVICE_MODE_MAX) {
1379
			sms_err("invalid mode specified %d", mode);
1380 1381 1382 1383 1384
			return -EINVAL;
		}

		smscore_registry_setmode(coredev->devpath, mode);

1385
		if (coredev->detectmode_handler)
1386 1387
			coredev->detectmode_handler(coredev->context,
						    &coredev->mode);
1388 1389 1390 1391 1392

		if (coredev->mode != mode && coredev->setmode_handler)
			rc = coredev->setmode_handler(coredev->context, mode);
	}

1393
	if (rc >= 0) {
1394
		char *buffer;
1395 1396
		coredev->mode = mode;
		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
1397

1398
		buffer = kmalloc(sizeof(struct sms_msg_data) +
1399 1400
				 SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
		if (buffer) {
1401
			struct sms_msg_data *msg = (struct sms_msg_data *) SMS_ALIGN_ADDRESS(buffer);
1402

1403 1404
			SMS_INIT_MSG(&msg->x_msg_header, MSG_SMS_INIT_DEVICE_REQ,
				     sizeof(struct sms_msg_data));
1405
			msg->msg_data[0] = mode;
1406 1407

			rc = smscore_sendrequest_and_wait(
1408
				coredev, msg, msg->x_msg_header.msg_length,
1409 1410 1411 1412
				&coredev->init_device_done);

			kfree(buffer);
		}
1413 1414
	}

1415
	if (rc < 0)
1416
		sms_err("return error code %d.", rc);
1417 1418 1419
	else
		sms_debug("Success setting device mode.");

1420 1421 1422 1423 1424 1425
	return rc;
}

/**
 * calls device handler to get current mode of operation
 *
1426 1427
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1428 1429 1430
 *
 * @return current mode
 */
1431
int smscore_get_device_mode(struct smscore_device_t *coredev)
1432 1433 1434
{
	return coredev->mode;
}
1435
EXPORT_SYMBOL_GPL(smscore_get_device_mode);
1436

1437 1438 1439 1440
/**
 * find client by response id & type within the clients list.
 * return client handle or NULL.
 *
1441 1442
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1443
 * @param data_type client data type (SMS_DONT_CARE for all types)
1444
 * @param id client id (SMS_DONT_CARE for all id)
1445 1446
 *
 */
1447 1448
static struct
smscore_client_t *smscore_find_client(struct smscore_device_t *coredev,
1449
				      int data_type, int id)
1450
{
1451 1452
	struct list_head *first;
	struct smscore_client_t *client;
1453
	unsigned long flags;
1454 1455
	struct list_head *firstid;
	struct smscore_idlist_t *client_id;
1456 1457 1458

	spin_lock_irqsave(&coredev->clientslock, flags);
	first = &coredev->clients;
1459 1460 1461 1462 1463 1464 1465
	list_for_each_entry(client, first, entry) {
		firstid = &client->idlist;
		list_for_each_entry(client_id, firstid, entry) {
			if ((client_id->id == id) &&
			    (client_id->data_type == data_type ||
			    (client_id->data_type == 0)))
				goto found;
1466 1467
		}
	}
1468 1469
	client = NULL;
found:
1470 1471 1472 1473 1474 1475 1476 1477
	spin_unlock_irqrestore(&coredev->clientslock, flags);
	return client;
}

/**
 * find client by response id/type, call clients onresponse handler
 * return buffer to pool on error
 *
1478 1479
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1480 1481 1482
 * @param cb pointer to response buffer descriptor
 *
 */
1483
void smscore_onresponse(struct smscore_device_t *coredev,
1484
		struct smscore_buffer_t *cb) {
1485
	struct sms_msg_hdr *phdr = (struct sms_msg_hdr *) ((u8 *) cb->p
1486 1487
			+ cb->offset);
	struct smscore_client_t *client;
1488
	int rc = -EBUSY;
1489 1490
	static unsigned long last_sample_time; /* = 0; */
	static int data_total; /* = 0; */
1491 1492 1493 1494 1495
	unsigned long time_now = jiffies_to_msecs(jiffies);

	if (!last_sample_time)
		last_sample_time = time_now;

1496
	if (time_now - last_sample_time > 10000) {
1497
		sms_debug("data rate %d bytes/secs",
1498 1499
			  (int)((data_total * 1000) /
				(time_now - last_sample_time)));
1500 1501 1502 1503 1504 1505

		last_sample_time = time_now;
		data_total = 0;
	}

	data_total += cb->size;
1506
	/* Do we need to re-route? */
1507 1508
	if ((phdr->msg_type == MSG_SMS_HO_PER_SLICES_IND) ||
			(phdr->msg_type == MSG_SMS_TRANSMISSION_IND)) {
1509
		if (coredev->mode == DEVICE_MODE_DVBT_BDA)
1510
			phdr->msg_dst_id = DVBT_BDA_CONTROL_MSG_ID;
1511 1512 1513
	}


1514
	client = smscore_find_client(coredev, phdr->msg_type, phdr->msg_dst_id);
1515

1516 1517
	/* If no client registered for type & id,
	 * check for control client where type is not registered */
1518 1519 1520
	if (client)
		rc = client->onresponse_handler(client->context, cb);

1521
	if (rc < 0) {
1522
		switch (phdr->msg_type) {
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
		case MSG_SMS_ISDBT_TUNE_RES:
			break;
		case MSG_SMS_RF_TUNE_RES:
			break;
		case MSG_SMS_SIGNAL_DETECTED_IND:
			break;
		case MSG_SMS_NO_SIGNAL_IND:
			break;
		case MSG_SMS_SPI_INT_LINE_SET_RES:
			break;
		case MSG_SMS_INTERFACE_LOCK_IND:
			break;
		case MSG_SMS_INTERFACE_UNLOCK_IND:
			break;
1537
		case MSG_SMS_GET_VERSION_EX_RES:
1538
		{
1539 1540
			struct sms_version_res *ver =
				(struct sms_version_res *) phdr;
1541
			sms_debug("Firmware id %d prots 0x%x ver %d.%d",
1542 1543
				  ver->firmware_id, ver->supported_protocols,
				  ver->rom_ver_major, ver->rom_ver_minor);
1544

1545 1546 1547 1548 1549
			coredev->mode = ver->firmware_id == 255 ?
				DEVICE_MODE_NONE : ver->firmware_id;
			coredev->modes_supported = ver->supported_protocols;
			coredev->fw_version = ver->rom_ver_major << 8 |
					      ver->rom_ver_minor;
1550

1551 1552 1553 1554 1555 1556 1557 1558 1559
			complete(&coredev->version_ex_done);
			break;
		}
		case MSG_SMS_INIT_DEVICE_RES:
			complete(&coredev->init_device_done);
			break;
		case MSG_SW_RELOAD_START_RES:
			complete(&coredev->reload_start_done);
			break;
1560 1561
		case MSG_SMS_DATA_VALIDITY_RES:
		{
1562
			struct sms_msg_data *validity = (struct sms_msg_data *) phdr;
1563 1564

			sms_err("MSG_SMS_DATA_VALIDITY_RES, checksum = 0x%x",
1565
				validity->msg_data[0]);
1566 1567 1568
			complete(&coredev->data_validity_done);
			break;
		}
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
		case MSG_SMS_DATA_DOWNLOAD_RES:
			complete(&coredev->data_download_done);
			break;
		case MSG_SW_RELOAD_EXEC_RES:
			break;
		case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
			complete(&coredev->trigger_done);
			break;
		case MSG_SMS_SLEEP_RESUME_COMP_IND:
			complete(&coredev->resume_done);
			break;
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
		case MSG_SMS_GPIO_CONFIG_EX_RES:
			complete(&coredev->gpio_configuration_done);
			break;
		case MSG_SMS_GPIO_SET_LEVEL_RES:
			complete(&coredev->gpio_set_level_done);
			break;
		case MSG_SMS_GPIO_GET_LEVEL_RES:
		{
			u32 *msgdata = (u32 *) phdr;
			coredev->gpio_get_res = msgdata[1];
1590
			sms_debug("gpio level %d",
1591 1592 1593 1594
					coredev->gpio_get_res);
			complete(&coredev->gpio_get_level_done);
			break;
		}
1595 1596 1597 1598 1599 1600 1601
		case MSG_SMS_START_IR_RES:
			complete(&coredev->ir_init_done);
			break;
		case MSG_SMS_IR_SAMPLES_IND:
			sms_ir_event(coredev,
				(const char *)
				((char *)phdr
1602 1603 1604
				+ sizeof(struct sms_msg_hdr)),
				(int)phdr->msg_length
				- sizeof(struct sms_msg_hdr));
1605 1606
			break;

1607 1608 1609 1610 1611 1612 1613 1614 1615
		case MSG_SMS_DVBT_BDA_DATA:
			/*
			 * It can be received here, if the frontend is
			 * tuned into a valid channel and the proper firmware
			 * is loaded. That happens when the module got removed
			 * and re-inserted, without powering the device off
			 */
			break;

1616
		default:
1617
			sms_debug("message %s(%d) not handled.",
1618 1619
				  smscore_translate_msg(phdr->msg_type),
				  phdr->msg_type);
1620
			break;
1621 1622 1623 1624
		}
		smscore_putbuffer(coredev, cb);
	}
}
1625
EXPORT_SYMBOL_GPL(smscore_onresponse);
1626 1627 1628 1629

/**
 * return pointer to next free buffer descriptor from core pool
 *
1630 1631
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1632 1633 1634
 *
 * @return pointer to descriptor on success, NULL on error.
 */
1635

1636
static struct smscore_buffer_t *get_entry(struct smscore_device_t *coredev)
1637
{
1638
	struct smscore_buffer_t *cb = NULL;
1639 1640 1641
	unsigned long flags;

	spin_lock_irqsave(&coredev->bufferslock, flags);
1642 1643 1644
	if (!list_empty(&coredev->buffers)) {
		cb = (struct smscore_buffer_t *) coredev->buffers.next;
		list_del(&cb->entry);
1645
	}
1646 1647 1648
	spin_unlock_irqrestore(&coredev->bufferslock, flags);
	return cb;
}
1649

1650 1651 1652
struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
{
	struct smscore_buffer_t *cb = NULL;
1653

1654
	wait_event(coredev->buffer_mng_waitq, (cb = get_entry(coredev)));
1655 1656 1657

	return cb;
}
1658
EXPORT_SYMBOL_GPL(smscore_getbuffer);
1659 1660 1661 1662

/**
 * return buffer descriptor to a pool
 *
1663 1664
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1665 1666 1667
 * @param cb pointer buffer descriptor
 *
 */
1668
void smscore_putbuffer(struct smscore_device_t *coredev,
1669 1670
		struct smscore_buffer_t *cb) {
	wake_up_interruptible(&coredev->buffer_mng_waitq);
1671 1672
	list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
}
1673
EXPORT_SYMBOL_GPL(smscore_putbuffer);
1674

1675 1676 1677
static int smscore_validate_client(struct smscore_device_t *coredev,
				   struct smscore_client_t *client,
				   int data_type, int id)
1678
{
1679 1680
	struct smscore_idlist_t *listentry;
	struct smscore_client_t *registered_client;
1681

1682
	if (!client) {
1683
		sms_err("bad parameter.");
1684
		return -EINVAL;
1685 1686
	}
	registered_client = smscore_find_client(coredev, data_type, id);
1687
	if (registered_client == client)
1688
		return 0;
1689

1690
	if (registered_client) {
1691
		sms_err("The msg ID already registered to another client.");
1692 1693
		return -EEXIST;
	}
1694
	listentry = kzalloc(sizeof(struct smscore_idlist_t), GFP_KERNEL);
1695
	if (!listentry) {
1696
		sms_err("Can't allocate memory for client id.");
1697
		return -ENOMEM;
1698 1699 1700
	}
	listentry->id = id;
	listentry->data_type = data_type;
1701 1702
	list_add_locked(&listentry->entry, &client->idlist,
			&coredev->clientslock);
1703 1704 1705 1706 1707 1708 1709 1710 1711
	return 0;
}

/**
 * creates smsclient object, check that id is taken by another client
 *
 * @param coredev pointer to a coredev object from clients hotplug
 * @param initial_id all messages with this id would be sent to this client
 * @param data_type all messages of this type would be sent to this client
1712 1713
 * @param onresponse_handler client handler that is called to
 *                           process incoming messages
1714 1715 1716 1717 1718 1719
 * @param onremove_handler client handler that is called when device is removed
 * @param context client-specific context
 * @param client pointer to a value that receives created smsclient object
 *
 * @return 0 on success, <0 on error.
 */
1720 1721 1722
int smscore_register_client(struct smscore_device_t *coredev,
			    struct smsclient_params_t *params,
			    struct smscore_client_t **client)
1723
{
1724
	struct smscore_client_t *newclient;
1725 1726 1727
	/* check that no other channel with same parameters exists */
	if (smscore_find_client(coredev, params->data_type,
				params->initial_id)) {
1728
		sms_err("Client already exist.");
1729
		return -EEXIST;
1730
	}
1731

1732
	newclient = kzalloc(sizeof(struct smscore_client_t), GFP_KERNEL);
1733
	if (!newclient) {
1734
		sms_err("Failed to allocate memory for client.");
1735
		return -ENOMEM;
1736 1737
	}

1738
	INIT_LIST_HEAD(&newclient->idlist);
1739 1740 1741 1742
	newclient->coredev = coredev;
	newclient->onresponse_handler = params->onresponse_handler;
	newclient->onremove_handler = params->onremove_handler;
	newclient->context = params->context;
1743 1744 1745 1746
	list_add_locked(&newclient->entry, &coredev->clients,
			&coredev->clientslock);
	smscore_validate_client(coredev, newclient, params->data_type,
				params->initial_id);
1747
	*client = newclient;
1748 1749
	sms_debug("%p %d %d", params->context, params->data_type,
		  params->initial_id);
1750 1751 1752

	return 0;
}
1753
EXPORT_SYMBOL_GPL(smscore_register_client);
1754 1755 1756 1757

/**
 * frees smsclient object and all subclients associated with it
 *
1758 1759
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1760 1761
 *
 */
1762
void smscore_unregister_client(struct smscore_client_t *client)
1763
{
1764
	struct smscore_device_t *coredev = client->coredev;
1765 1766 1767 1768 1769
	unsigned long flags;

	spin_lock_irqsave(&coredev->clientslock, flags);


1770
	while (!list_empty(&client->idlist)) {
1771 1772
		struct smscore_idlist_t *identry =
			(struct smscore_idlist_t *) client->idlist.next;
1773 1774
		list_del(&identry->entry);
		kfree(identry);
1775 1776
	}

1777
	sms_info("%p", client->context);
1778 1779 1780 1781 1782 1783

	list_del(&client->entry);
	kfree(client);

	spin_unlock_irqrestore(&coredev->clientslock, flags);
}
1784
EXPORT_SYMBOL_GPL(smscore_unregister_client);
1785 1786 1787 1788 1789

/**
 * verifies that source id is not taken by another client,
 * calls device handler to send requests to the device
 *
1790 1791
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1792 1793 1794 1795 1796
 * @param buffer pointer to a request buffer
 * @param size size (in bytes) of request buffer
 *
 * @return 0 on success, <0 on error.
 */
1797 1798
int smsclient_sendrequest(struct smscore_client_t *client,
			  void *buffer, size_t size)
1799
{
1800
	struct smscore_device_t *coredev;
1801
	struct sms_msg_hdr *phdr = (struct sms_msg_hdr *) buffer;
1802 1803
	int rc;

1804
	if (client == NULL) {
1805
		sms_err("Got NULL client");
1806 1807 1808 1809
		return -EINVAL;
	}

	coredev = client->coredev;
1810

1811 1812
	/* check that no other channel with same id exists */
	if (coredev == NULL) {
1813
		sms_err("Got NULL coredev");
1814 1815 1816
		return -EINVAL;
	}

1817
	rc = smscore_validate_client(client->coredev, client, 0,
1818
				     phdr->msg_src_id);
1819 1820 1821 1822 1823
	if (rc < 0)
		return rc;

	return coredev->sendrequest_handler(coredev->context, buffer, size);
}
1824
EXPORT_SYMBOL_GPL(smsclient_sendrequest);
1825 1826


1827
/* old GPIO managements implementation */
1828
int smscore_configure_gpio(struct smscore_device_t *coredev, u32 pin,
1829
			   struct smscore_config_gpio *pinconfig)
1830 1831
{
	struct {
1832
		struct sms_msg_hdr hdr;
1833 1834 1835 1836
		u32 data[6];
	} msg;

	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
1837 1838 1839 1840 1841
		msg.hdr.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
		msg.hdr.msg_dst_id = HIF_TASK;
		msg.hdr.msg_flags = 0;
		msg.hdr.msg_type  = MSG_SMS_GPIO_CONFIG_EX_REQ;
		msg.hdr.msg_length = sizeof(msg);
1842 1843 1844 1845 1846 1847 1848 1849

		msg.data[0] = pin;
		msg.data[1] = pinconfig->pullupdown;

		/* Convert slew rate for Nova: Fast(0) = 3 / Slow(1) = 0; */
		msg.data[2] = pinconfig->outputslewrate == 0 ? 3 : 0;

		switch (pinconfig->outputdriving) {
1850
		case SMS_GPIO_OUTPUTDRIVING_S_16mA:
1851 1852
			msg.data[3] = 7; /* Nova - 16mA */
			break;
1853
		case SMS_GPIO_OUTPUTDRIVING_S_12mA:
1854 1855
			msg.data[3] = 5; /* Nova - 11mA */
			break;
1856
		case SMS_GPIO_OUTPUTDRIVING_S_8mA:
1857 1858
			msg.data[3] = 3; /* Nova - 7mA */
			break;
1859
		case SMS_GPIO_OUTPUTDRIVING_S_4mA:
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876
		default:
			msg.data[3] = 2; /* Nova - 4mA */
			break;
		}

		msg.data[4] = pinconfig->direction;
		msg.data[5] = 0;
	} else /* TODO: SMS_DEVICE_FAMILY1 */
		return -EINVAL;

	return coredev->sendrequest_handler(coredev->context,
					    &msg, sizeof(msg));
}

int smscore_set_gpio(struct smscore_device_t *coredev, u32 pin, int level)
{
	struct {
1877
		struct sms_msg_hdr hdr;
1878 1879 1880 1881 1882 1883
		u32 data[3];
	} msg;

	if (pin > MAX_GPIO_PIN_NUMBER)
		return -EINVAL;

1884 1885 1886 1887 1888
	msg.hdr.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
	msg.hdr.msg_dst_id = HIF_TASK;
	msg.hdr.msg_flags = 0;
	msg.hdr.msg_type  = MSG_SMS_GPIO_SET_LEVEL_REQ;
	msg.hdr.msg_length = sizeof(msg);
1889 1890 1891 1892 1893 1894 1895 1896 1897

	msg.data[0] = pin;
	msg.data[1] = level ? 1 : 0;
	msg.data[2] = 0;

	return coredev->sendrequest_handler(coredev->context,
					    &msg, sizeof(msg));
}

1898
/* new GPIO management implementation */
1899 1900
static int get_gpio_pin_params(u32 pin_num, u32 *p_translatedpin_num,
		u32 *p_group_num, u32 *p_group_cfg) {
1901

1902
	*p_group_cfg = 1;
1903

1904
	if (pin_num <= 1)	{
1905 1906 1907
		*p_translatedpin_num = 0;
		*p_group_num = 9;
		*p_group_cfg = 2;
1908
	} else if (pin_num >= 2 && pin_num <= 6) {
1909 1910 1911
		*p_translatedpin_num = 2;
		*p_group_num = 0;
		*p_group_cfg = 2;
1912
	} else if (pin_num >= 7 && pin_num <= 11) {
1913 1914
		*p_translatedpin_num = 7;
		*p_group_num = 1;
1915
	} else if (pin_num >= 12 && pin_num <= 15) {
1916 1917 1918
		*p_translatedpin_num = 12;
		*p_group_num = 2;
		*p_group_cfg = 3;
1919
	} else if (pin_num == 16) {
1920 1921
		*p_translatedpin_num = 16;
		*p_group_num = 23;
1922
	} else if (pin_num >= 17 && pin_num <= 24) {
1923 1924
		*p_translatedpin_num = 17;
		*p_group_num = 3;
1925
	} else if (pin_num == 25) {
1926 1927
		*p_translatedpin_num = 25;
		*p_group_num = 6;
1928
	} else if (pin_num >= 26 && pin_num <= 28) {
1929 1930
		*p_translatedpin_num = 26;
		*p_group_num = 4;
1931
	} else if (pin_num == 29) {
1932 1933 1934
		*p_translatedpin_num = 29;
		*p_group_num = 5;
		*p_group_cfg = 2;
1935
	} else if (pin_num == 30) {
1936 1937
		*p_translatedpin_num = 30;
		*p_group_num = 8;
1938
	} else if (pin_num == 31) {
1939 1940
		*p_translatedpin_num = 31;
		*p_group_num = 17;
1941 1942 1943
	} else
		return -1;

1944
	*p_group_cfg <<= 24;
1945 1946 1947 1948

	return 0;
}

1949 1950
int smscore_gpio_configure(struct smscore_device_t *coredev, u8 pin_num,
		struct smscore_config_gpio *p_gpio_config) {
1951

1952 1953 1954 1955 1956
	u32 total_len;
	u32 translatedpin_num = 0;
	u32 group_num = 0;
	u32 electric_char;
	u32 group_cfg;
1957 1958 1959
	void *buffer;
	int rc;

1960
	struct set_gpio_msg {
1961
		struct sms_msg_hdr x_msg_header;
1962 1963
		u32 msg_data[6];
	} *p_msg;
1964 1965


1966
	if (pin_num > MAX_GPIO_PIN_NUMBER)
1967 1968
		return -EINVAL;

1969
	if (p_gpio_config == NULL)
1970 1971
		return -EINVAL;

1972
	total_len = sizeof(struct sms_msg_hdr) + (sizeof(u32) * 6);
1973

1974
	buffer = kmalloc(total_len + SMS_DMA_ALIGNMENT,
1975 1976 1977 1978
			GFP_KERNEL | GFP_DMA);
	if (!buffer)
		return -ENOMEM;

1979
	p_msg = (struct set_gpio_msg *) SMS_ALIGN_ADDRESS(buffer);
1980

1981 1982 1983 1984 1985
	p_msg->x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
	p_msg->x_msg_header.msg_dst_id = HIF_TASK;
	p_msg->x_msg_header.msg_flags = 0;
	p_msg->x_msg_header.msg_length = (u16) total_len;
	p_msg->msg_data[0] = pin_num;
1986 1987

	if (!(coredev->device_flags & SMS_DEVICE_FAMILY2)) {
1988 1989 1990
		p_msg->x_msg_header.msg_type = MSG_SMS_GPIO_CONFIG_REQ;
		if (get_gpio_pin_params(pin_num, &translatedpin_num, &group_num,
				&group_cfg) != 0) {
1991 1992 1993
			rc = -EINVAL;
			goto free;
		}
1994

1995 1996 1997
		p_msg->msg_data[1] = translatedpin_num;
		p_msg->msg_data[2] = group_num;
		electric_char = (p_gpio_config->pullupdown)
1998 1999 2000
				| (p_gpio_config->inputcharacteristics << 2)
				| (p_gpio_config->outputslewrate << 3)
				| (p_gpio_config->outputdriving << 4);
2001 2002 2003
		p_msg->msg_data[3] = electric_char;
		p_msg->msg_data[4] = p_gpio_config->direction;
		p_msg->msg_data[5] = group_cfg;
2004
	} else {
2005 2006 2007 2008 2009 2010
		p_msg->x_msg_header.msg_type = MSG_SMS_GPIO_CONFIG_EX_REQ;
		p_msg->msg_data[1] = p_gpio_config->pullupdown;
		p_msg->msg_data[2] = p_gpio_config->outputslewrate;
		p_msg->msg_data[3] = p_gpio_config->outputdriving;
		p_msg->msg_data[4] = p_gpio_config->direction;
		p_msg->msg_data[5] = 0;
2011 2012
	}

2013
	rc = smscore_sendrequest_and_wait(coredev, p_msg, total_len,
2014 2015 2016 2017 2018 2019 2020 2021
			&coredev->gpio_configuration_done);

	if (rc != 0) {
		if (rc == -ETIME)
			sms_err("smscore_gpio_configure timeout");
		else
			sms_err("smscore_gpio_configure error");
	}
2022
free:
2023 2024 2025 2026 2027
	kfree(buffer);

	return rc;
}

2028 2029
int smscore_gpio_set_level(struct smscore_device_t *coredev, u8 pin_num,
		u8 new_level) {
2030

2031
	u32 total_len;
2032 2033 2034
	int rc;
	void *buffer;

2035
	struct set_gpio_msg {
2036
		struct sms_msg_hdr x_msg_header;
2037 2038
		u32 msg_data[3]; /* keep it 3 ! */
	} *p_msg;
2039

2040
	if ((new_level > 1) || (pin_num > MAX_GPIO_PIN_NUMBER))
2041 2042
		return -EINVAL;

2043
	total_len = sizeof(struct sms_msg_hdr) +
2044 2045
			(3 * sizeof(u32)); /* keep it 3 ! */

2046
	buffer = kmalloc(total_len + SMS_DMA_ALIGNMENT,
2047 2048 2049 2050
			GFP_KERNEL | GFP_DMA);
	if (!buffer)
		return -ENOMEM;

2051
	p_msg = (struct set_gpio_msg *) SMS_ALIGN_ADDRESS(buffer);
2052

2053 2054 2055 2056 2057 2058 2059
	p_msg->x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
	p_msg->x_msg_header.msg_dst_id = HIF_TASK;
	p_msg->x_msg_header.msg_flags = 0;
	p_msg->x_msg_header.msg_type = MSG_SMS_GPIO_SET_LEVEL_REQ;
	p_msg->x_msg_header.msg_length = (u16) total_len;
	p_msg->msg_data[0] = pin_num;
	p_msg->msg_data[1] = new_level;
2060 2061

	/* Send message to SMS */
2062
	rc = smscore_sendrequest_and_wait(coredev, p_msg, total_len,
2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
			&coredev->gpio_set_level_done);

	if (rc != 0) {
		if (rc == -ETIME)
			sms_err("smscore_gpio_set_level timeout");
		else
			sms_err("smscore_gpio_set_level error");
	}
	kfree(buffer);

	return rc;
}

2076
int smscore_gpio_get_level(struct smscore_device_t *coredev, u8 pin_num,
2077 2078
		u8 *level) {

2079
	u32 total_len;
2080 2081 2082
	int rc;
	void *buffer;

2083
	struct set_gpio_msg {
2084
		struct sms_msg_hdr x_msg_header;
2085 2086
		u32 msg_data[2];
	} *p_msg;
2087 2088


2089
	if (pin_num > MAX_GPIO_PIN_NUMBER)
2090 2091
		return -EINVAL;

2092
	total_len = sizeof(struct sms_msg_hdr) + (2 * sizeof(u32));
2093

2094
	buffer = kmalloc(total_len + SMS_DMA_ALIGNMENT,
2095 2096 2097 2098
			GFP_KERNEL | GFP_DMA);
	if (!buffer)
		return -ENOMEM;

2099
	p_msg = (struct set_gpio_msg *) SMS_ALIGN_ADDRESS(buffer);
2100

2101 2102 2103 2104 2105 2106 2107
	p_msg->x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
	p_msg->x_msg_header.msg_dst_id = HIF_TASK;
	p_msg->x_msg_header.msg_flags = 0;
	p_msg->x_msg_header.msg_type = MSG_SMS_GPIO_GET_LEVEL_REQ;
	p_msg->x_msg_header.msg_length = (u16) total_len;
	p_msg->msg_data[0] = pin_num;
	p_msg->msg_data[1] = 0;
2108 2109

	/* Send message to SMS */
2110
	rc = smscore_sendrequest_and_wait(coredev, p_msg, total_len,
2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
			&coredev->gpio_get_level_done);

	if (rc != 0) {
		if (rc == -ETIME)
			sms_err("smscore_gpio_get_level timeout");
		else
			sms_err("smscore_gpio_get_level error");
	}
	kfree(buffer);

	/* Its a race between other gpio_get_level() and the copy of the single
	 * global 'coredev->gpio_get_res' to  the function's variable 'level'
	 */
	*level = coredev->gpio_get_res;

	return rc;
}

2129
static int __init smscore_module_init(void)
2130
{
2131
	int rc = 0;
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142

	INIT_LIST_HEAD(&g_smscore_notifyees);
	INIT_LIST_HEAD(&g_smscore_devices);
	kmutex_init(&g_smscore_deviceslock);

	INIT_LIST_HEAD(&g_smscore_registry);
	kmutex_init(&g_smscore_registrylock);

	return rc;
}

2143
static void __exit smscore_module_exit(void)
2144 2145
{
	kmutex_lock(&g_smscore_deviceslock);
2146
	while (!list_empty(&g_smscore_notifyees)) {
2147 2148 2149
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *)
				g_smscore_notifyees.next;
2150 2151 2152 2153 2154 2155 2156

		list_del(&notifyee->entry);
		kfree(notifyee);
	}
	kmutex_unlock(&g_smscore_deviceslock);

	kmutex_lock(&g_smscore_registrylock);
2157
	while (!list_empty(&g_smscore_registry)) {
2158 2159 2160
		struct smscore_registry_entry_t *entry =
			(struct smscore_registry_entry_t *)
				g_smscore_registry.next;
2161 2162 2163 2164 2165 2166

		list_del(&entry->entry);
		kfree(entry);
	}
	kmutex_unlock(&g_smscore_registrylock);

2167
	sms_debug("");
2168 2169 2170 2171 2172
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

2173 2174
MODULE_DESCRIPTION("Siano MDTV Core module");
MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
2175
MODULE_LICENSE("GPL");
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199

/* This should match what's defined at smscoreapi.h */
MODULE_FIRMWARE(SMS_FW_ATSC_DENVER);
MODULE_FIRMWARE(SMS_FW_CMMB_MING_APP);
MODULE_FIRMWARE(SMS_FW_CMMB_VEGA_12MHZ);
MODULE_FIRMWARE(SMS_FW_CMMB_VENICE_12MHZ);
MODULE_FIRMWARE(SMS_FW_DVBH_RIO);
MODULE_FIRMWARE(SMS_FW_DVB_NOVA_12MHZ_B0);
MODULE_FIRMWARE(SMS_FW_DVB_NOVA_12MHZ);
MODULE_FIRMWARE(SMS_FW_DVB_RIO);
MODULE_FIRMWARE(SMS_FW_FM_RADIO);
MODULE_FIRMWARE(SMS_FW_FM_RADIO_RIO);
MODULE_FIRMWARE(SMS_FW_DVBT_HCW_55XXX);
MODULE_FIRMWARE(SMS_FW_ISDBT_HCW_55XXX);
MODULE_FIRMWARE(SMS_FW_ISDBT_NOVA_12MHZ_B0);
MODULE_FIRMWARE(SMS_FW_ISDBT_NOVA_12MHZ);
MODULE_FIRMWARE(SMS_FW_ISDBT_PELE);
MODULE_FIRMWARE(SMS_FW_ISDBT_RIO);
MODULE_FIRMWARE(SMS_FW_DVBT_NOVA_A);
MODULE_FIRMWARE(SMS_FW_DVBT_NOVA_B);
MODULE_FIRMWARE(SMS_FW_DVBT_STELLAR);
MODULE_FIRMWARE(SMS_FW_TDMB_DENVER);
MODULE_FIRMWARE(SMS_FW_TDMB_NOVA_12MHZ_B0);
MODULE_FIRMWARE(SMS_FW_TDMB_NOVA_12MHZ);