1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Siano core API module
4  *
5  *  This file contains implementation for the interface to sms core component
6  *
7  *  author: Uri Shkolnik
8  *
9  *  Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
10  */
11 
12 #include "smscoreapi.h"
13 
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/delay.h>
20 #include <linux/io.h>
21 #include <linux/slab.h>
22 
23 #include <linux/firmware.h>
24 #include <linux/wait.h>
25 #include <asm/byteorder.h>
26 
27 #include "sms-cards.h"
28 #include "smsir.h"
29 
30 struct smscore_device_notifyee_t {
31 	struct list_head entry;
32 	hotplug_t hotplug;
33 };
34 
35 struct smscore_idlist_t {
36 	struct list_head entry;
37 	int		id;
38 	int		data_type;
39 };
40 
41 struct smscore_client_t {
42 	struct list_head entry;
43 	struct smscore_device_t *coredev;
44 	void			*context;
45 	struct list_head	idlist;
46 	onresponse_t		onresponse_handler;
47 	onremove_t		onremove_handler;
48 };
49 
50 static char *siano_msgs[] = {
51 	[MSG_TYPE_BASE_VAL                           - MSG_TYPE_BASE_VAL] = "MSG_TYPE_BASE_VAL",
52 	[MSG_SMS_GET_VERSION_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_VERSION_REQ",
53 	[MSG_SMS_GET_VERSION_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_VERSION_RES",
54 	[MSG_SMS_MULTI_BRIDGE_CFG                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_MULTI_BRIDGE_CFG",
55 	[MSG_SMS_GPIO_CONFIG_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_CONFIG_REQ",
56 	[MSG_SMS_GPIO_CONFIG_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_CONFIG_RES",
57 	[MSG_SMS_GPIO_SET_LEVEL_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_SET_LEVEL_REQ",
58 	[MSG_SMS_GPIO_SET_LEVEL_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_SET_LEVEL_RES",
59 	[MSG_SMS_GPIO_GET_LEVEL_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_GET_LEVEL_REQ",
60 	[MSG_SMS_GPIO_GET_LEVEL_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_GET_LEVEL_RES",
61 	[MSG_SMS_EEPROM_BURN_IND                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EEPROM_BURN_IND",
62 	[MSG_SMS_LOG_ENABLE_CHANGE_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOG_ENABLE_CHANGE_REQ",
63 	[MSG_SMS_LOG_ENABLE_CHANGE_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOG_ENABLE_CHANGE_RES",
64 	[MSG_SMS_SET_MAX_TX_MSG_LEN_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_MAX_TX_MSG_LEN_REQ",
65 	[MSG_SMS_SET_MAX_TX_MSG_LEN_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_MAX_TX_MSG_LEN_RES",
66 	[MSG_SMS_SPI_HALFDUPLEX_TOKEN_HOST_TO_DEVICE - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_HALFDUPLEX_TOKEN_HOST_TO_DEVICE",
67 	[MSG_SMS_SPI_HALFDUPLEX_TOKEN_DEVICE_TO_HOST - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_HALFDUPLEX_TOKEN_DEVICE_TO_HOST",
68 	[MSG_SMS_BACKGROUND_SCAN_FLAG_CHANGE_REQ     - MSG_TYPE_BASE_VAL] = "MSG_SMS_BACKGROUND_SCAN_FLAG_CHANGE_REQ",
69 	[MSG_SMS_BACKGROUND_SCAN_FLAG_CHANGE_RES     - MSG_TYPE_BASE_VAL] = "MSG_SMS_BACKGROUND_SCAN_FLAG_CHANGE_RES",
70 	[MSG_SMS_BACKGROUND_SCAN_SIGNAL_DETECTED_IND - MSG_TYPE_BASE_VAL] = "MSG_SMS_BACKGROUND_SCAN_SIGNAL_DETECTED_IND",
71 	[MSG_SMS_BACKGROUND_SCAN_NO_SIGNAL_IND       - MSG_TYPE_BASE_VAL] = "MSG_SMS_BACKGROUND_SCAN_NO_SIGNAL_IND",
72 	[MSG_SMS_CONFIGURE_RF_SWITCH_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_CONFIGURE_RF_SWITCH_REQ",
73 	[MSG_SMS_CONFIGURE_RF_SWITCH_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_CONFIGURE_RF_SWITCH_RES",
74 	[MSG_SMS_MRC_PATH_DISCONNECT_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_PATH_DISCONNECT_REQ",
75 	[MSG_SMS_MRC_PATH_DISCONNECT_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_PATH_DISCONNECT_RES",
76 	[MSG_SMS_RECEIVE_1SEG_THROUGH_FULLSEG_REQ    - MSG_TYPE_BASE_VAL] = "MSG_SMS_RECEIVE_1SEG_THROUGH_FULLSEG_REQ",
77 	[MSG_SMS_RECEIVE_1SEG_THROUGH_FULLSEG_RES    - MSG_TYPE_BASE_VAL] = "MSG_SMS_RECEIVE_1SEG_THROUGH_FULLSEG_RES",
78 	[MSG_SMS_RECEIVE_VHF_VIA_VHF_INPUT_REQ       - MSG_TYPE_BASE_VAL] = "MSG_SMS_RECEIVE_VHF_VIA_VHF_INPUT_REQ",
79 	[MSG_SMS_RECEIVE_VHF_VIA_VHF_INPUT_RES       - MSG_TYPE_BASE_VAL] = "MSG_SMS_RECEIVE_VHF_VIA_VHF_INPUT_RES",
80 	[MSG_WR_REG_RFT_REQ                          - MSG_TYPE_BASE_VAL] = "MSG_WR_REG_RFT_REQ",
81 	[MSG_WR_REG_RFT_RES                          - MSG_TYPE_BASE_VAL] = "MSG_WR_REG_RFT_RES",
82 	[MSG_RD_REG_RFT_REQ                          - MSG_TYPE_BASE_VAL] = "MSG_RD_REG_RFT_REQ",
83 	[MSG_RD_REG_RFT_RES                          - MSG_TYPE_BASE_VAL] = "MSG_RD_REG_RFT_RES",
84 	[MSG_RD_REG_ALL_RFT_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_RD_REG_ALL_RFT_REQ",
85 	[MSG_RD_REG_ALL_RFT_RES                      - MSG_TYPE_BASE_VAL] = "MSG_RD_REG_ALL_RFT_RES",
86 	[MSG_HELP_INT                                - MSG_TYPE_BASE_VAL] = "MSG_HELP_INT",
87 	[MSG_RUN_SCRIPT_INT                          - MSG_TYPE_BASE_VAL] = "MSG_RUN_SCRIPT_INT",
88 	[MSG_SMS_EWS_INBAND_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_EWS_INBAND_REQ",
89 	[MSG_SMS_EWS_INBAND_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_EWS_INBAND_RES",
90 	[MSG_SMS_RFS_SELECT_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_RFS_SELECT_REQ",
91 	[MSG_SMS_RFS_SELECT_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_RFS_SELECT_RES",
92 	[MSG_SMS_MB_GET_VER_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_GET_VER_REQ",
93 	[MSG_SMS_MB_GET_VER_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_GET_VER_RES",
94 	[MSG_SMS_MB_WRITE_CFGFILE_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_WRITE_CFGFILE_REQ",
95 	[MSG_SMS_MB_WRITE_CFGFILE_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_WRITE_CFGFILE_RES",
96 	[MSG_SMS_MB_READ_CFGFILE_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_READ_CFGFILE_REQ",
97 	[MSG_SMS_MB_READ_CFGFILE_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_READ_CFGFILE_RES",
98 	[MSG_SMS_RD_MEM_REQ                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_RD_MEM_REQ",
99 	[MSG_SMS_RD_MEM_RES                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_RD_MEM_RES",
100 	[MSG_SMS_WR_MEM_REQ                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_WR_MEM_REQ",
101 	[MSG_SMS_WR_MEM_RES                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_WR_MEM_RES",
102 	[MSG_SMS_UPDATE_MEM_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_UPDATE_MEM_REQ",
103 	[MSG_SMS_UPDATE_MEM_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_UPDATE_MEM_RES",
104 	[MSG_SMS_ISDBT_ENABLE_FULL_PARAMS_SET_REQ    - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_ENABLE_FULL_PARAMS_SET_REQ",
105 	[MSG_SMS_ISDBT_ENABLE_FULL_PARAMS_SET_RES    - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_ENABLE_FULL_PARAMS_SET_RES",
106 	[MSG_SMS_RF_TUNE_REQ                         - MSG_TYPE_BASE_VAL] = "MSG_SMS_RF_TUNE_REQ",
107 	[MSG_SMS_RF_TUNE_RES                         - MSG_TYPE_BASE_VAL] = "MSG_SMS_RF_TUNE_RES",
108 	[MSG_SMS_ISDBT_ENABLE_HIGH_MOBILITY_REQ      - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_ENABLE_HIGH_MOBILITY_REQ",
109 	[MSG_SMS_ISDBT_ENABLE_HIGH_MOBILITY_RES      - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_ENABLE_HIGH_MOBILITY_RES",
110 	[MSG_SMS_ISDBT_SB_RECEPTION_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_SB_RECEPTION_REQ",
111 	[MSG_SMS_ISDBT_SB_RECEPTION_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_SB_RECEPTION_RES",
112 	[MSG_SMS_GENERIC_EPROM_WRITE_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_EPROM_WRITE_REQ",
113 	[MSG_SMS_GENERIC_EPROM_WRITE_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_EPROM_WRITE_RES",
114 	[MSG_SMS_GENERIC_EPROM_READ_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_EPROM_READ_REQ",
115 	[MSG_SMS_GENERIC_EPROM_READ_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_EPROM_READ_RES",
116 	[MSG_SMS_EEPROM_WRITE_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_EEPROM_WRITE_REQ",
117 	[MSG_SMS_EEPROM_WRITE_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_EEPROM_WRITE_RES",
118 	[MSG_SMS_CUSTOM_READ_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_CUSTOM_READ_REQ",
119 	[MSG_SMS_CUSTOM_READ_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_CUSTOM_READ_RES",
120 	[MSG_SMS_CUSTOM_WRITE_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_CUSTOM_WRITE_REQ",
121 	[MSG_SMS_CUSTOM_WRITE_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_CUSTOM_WRITE_RES",
122 	[MSG_SMS_INIT_DEVICE_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_INIT_DEVICE_REQ",
123 	[MSG_SMS_INIT_DEVICE_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_INIT_DEVICE_RES",
124 	[MSG_SMS_ATSC_SET_ALL_IP_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_SET_ALL_IP_REQ",
125 	[MSG_SMS_ATSC_SET_ALL_IP_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_SET_ALL_IP_RES",
126 	[MSG_SMS_ATSC_START_ENSEMBLE_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_START_ENSEMBLE_REQ",
127 	[MSG_SMS_ATSC_START_ENSEMBLE_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_START_ENSEMBLE_RES",
128 	[MSG_SMS_SET_OUTPUT_MODE_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_OUTPUT_MODE_REQ",
129 	[MSG_SMS_SET_OUTPUT_MODE_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_OUTPUT_MODE_RES",
130 	[MSG_SMS_ATSC_IP_FILTER_GET_LIST_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_GET_LIST_REQ",
131 	[MSG_SMS_ATSC_IP_FILTER_GET_LIST_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_GET_LIST_RES",
132 	[MSG_SMS_SUB_CHANNEL_START_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SUB_CHANNEL_START_REQ",
133 	[MSG_SMS_SUB_CHANNEL_START_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SUB_CHANNEL_START_RES",
134 	[MSG_SMS_SUB_CHANNEL_STOP_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SUB_CHANNEL_STOP_REQ",
135 	[MSG_SMS_SUB_CHANNEL_STOP_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SUB_CHANNEL_STOP_RES",
136 	[MSG_SMS_ATSC_IP_FILTER_ADD_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_ADD_REQ",
137 	[MSG_SMS_ATSC_IP_FILTER_ADD_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_ADD_RES",
138 	[MSG_SMS_ATSC_IP_FILTER_REMOVE_REQ           - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_REMOVE_REQ",
139 	[MSG_SMS_ATSC_IP_FILTER_REMOVE_RES           - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_REMOVE_RES",
140 	[MSG_SMS_ATSC_IP_FILTER_REMOVE_ALL_REQ       - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_REMOVE_ALL_REQ",
141 	[MSG_SMS_ATSC_IP_FILTER_REMOVE_ALL_RES       - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_IP_FILTER_REMOVE_ALL_RES",
142 	[MSG_SMS_WAIT_CMD                            - MSG_TYPE_BASE_VAL] = "MSG_SMS_WAIT_CMD",
143 	[MSG_SMS_ADD_PID_FILTER_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_ADD_PID_FILTER_REQ",
144 	[MSG_SMS_ADD_PID_FILTER_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_ADD_PID_FILTER_RES",
145 	[MSG_SMS_REMOVE_PID_FILTER_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_REMOVE_PID_FILTER_REQ",
146 	[MSG_SMS_REMOVE_PID_FILTER_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_REMOVE_PID_FILTER_RES",
147 	[MSG_SMS_FAST_INFORMATION_CHANNEL_REQ        - MSG_TYPE_BASE_VAL] = "MSG_SMS_FAST_INFORMATION_CHANNEL_REQ",
148 	[MSG_SMS_FAST_INFORMATION_CHANNEL_RES        - MSG_TYPE_BASE_VAL] = "MSG_SMS_FAST_INFORMATION_CHANNEL_RES",
149 	[MSG_SMS_DAB_CHANNEL                         - MSG_TYPE_BASE_VAL] = "MSG_SMS_DAB_CHANNEL",
150 	[MSG_SMS_GET_PID_FILTER_LIST_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_PID_FILTER_LIST_REQ",
151 	[MSG_SMS_GET_PID_FILTER_LIST_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_PID_FILTER_LIST_RES",
152 	[MSG_SMS_POWER_DOWN_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_DOWN_REQ",
153 	[MSG_SMS_POWER_DOWN_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_DOWN_RES",
154 	[MSG_SMS_ATSC_SLT_EXIST_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_SLT_EXIST_IND",
155 	[MSG_SMS_ATSC_NO_SLT_IND                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_ATSC_NO_SLT_IND",
156 	[MSG_SMS_GET_STATISTICS_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_STATISTICS_REQ",
157 	[MSG_SMS_GET_STATISTICS_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_STATISTICS_RES",
158 	[MSG_SMS_SEND_DUMP                           - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_DUMP",
159 	[MSG_SMS_SCAN_START_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_START_REQ",
160 	[MSG_SMS_SCAN_START_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_START_RES",
161 	[MSG_SMS_SCAN_STOP_REQ                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_STOP_REQ",
162 	[MSG_SMS_SCAN_STOP_RES                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_STOP_RES",
163 	[MSG_SMS_SCAN_PROGRESS_IND                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_PROGRESS_IND",
164 	[MSG_SMS_SCAN_COMPLETE_IND                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_SCAN_COMPLETE_IND",
165 	[MSG_SMS_LOG_ITEM                            - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOG_ITEM",
166 	[MSG_SMS_DAB_SUBCHANNEL_RECONFIG_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_DAB_SUBCHANNEL_RECONFIG_REQ",
167 	[MSG_SMS_DAB_SUBCHANNEL_RECONFIG_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_DAB_SUBCHANNEL_RECONFIG_RES",
168 	[MSG_SMS_HO_PER_SLICES_IND                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_PER_SLICES_IND",
169 	[MSG_SMS_HO_INBAND_POWER_IND                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_INBAND_POWER_IND",
170 	[MSG_SMS_MANUAL_DEMOD_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_MANUAL_DEMOD_REQ",
171 	[MSG_SMS_HO_TUNE_ON_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_TUNE_ON_REQ",
172 	[MSG_SMS_HO_TUNE_ON_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_TUNE_ON_RES",
173 	[MSG_SMS_HO_TUNE_OFF_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_TUNE_OFF_REQ",
174 	[MSG_SMS_HO_TUNE_OFF_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_TUNE_OFF_RES",
175 	[MSG_SMS_HO_PEEK_FREQ_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_PEEK_FREQ_REQ",
176 	[MSG_SMS_HO_PEEK_FREQ_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_PEEK_FREQ_RES",
177 	[MSG_SMS_HO_PEEK_FREQ_IND                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_HO_PEEK_FREQ_IND",
178 	[MSG_SMS_MB_ATTEN_SET_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_ATTEN_SET_REQ",
179 	[MSG_SMS_MB_ATTEN_SET_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_MB_ATTEN_SET_RES",
180 	[MSG_SMS_ENABLE_STAT_IN_I2C_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ENABLE_STAT_IN_I2C_REQ",
181 	[MSG_SMS_ENABLE_STAT_IN_I2C_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_ENABLE_STAT_IN_I2C_RES",
182 	[MSG_SMS_SET_ANTENNA_CONFIG_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_ANTENNA_CONFIG_REQ",
183 	[MSG_SMS_SET_ANTENNA_CONFIG_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_ANTENNA_CONFIG_RES",
184 	[MSG_SMS_GET_STATISTICS_EX_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_STATISTICS_EX_REQ",
185 	[MSG_SMS_GET_STATISTICS_EX_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_STATISTICS_EX_RES",
186 	[MSG_SMS_SLEEP_RESUME_COMP_IND               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SLEEP_RESUME_COMP_IND",
187 	[MSG_SMS_SWITCH_HOST_INTERFACE_REQ           - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWITCH_HOST_INTERFACE_REQ",
188 	[MSG_SMS_SWITCH_HOST_INTERFACE_RES           - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWITCH_HOST_INTERFACE_RES",
189 	[MSG_SMS_DATA_DOWNLOAD_REQ                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_DOWNLOAD_REQ",
190 	[MSG_SMS_DATA_DOWNLOAD_RES                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_DOWNLOAD_RES",
191 	[MSG_SMS_DATA_VALIDITY_REQ                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_VALIDITY_REQ",
192 	[MSG_SMS_DATA_VALIDITY_RES                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_VALIDITY_RES",
193 	[MSG_SMS_SWDOWNLOAD_TRIGGER_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWDOWNLOAD_TRIGGER_REQ",
194 	[MSG_SMS_SWDOWNLOAD_TRIGGER_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWDOWNLOAD_TRIGGER_RES",
195 	[MSG_SMS_SWDOWNLOAD_BACKDOOR_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWDOWNLOAD_BACKDOOR_REQ",
196 	[MSG_SMS_SWDOWNLOAD_BACKDOOR_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_SWDOWNLOAD_BACKDOOR_RES",
197 	[MSG_SMS_GET_VERSION_EX_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_VERSION_EX_REQ",
198 	[MSG_SMS_GET_VERSION_EX_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_VERSION_EX_RES",
199 	[MSG_SMS_CLOCK_OUTPUT_CONFIG_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_CLOCK_OUTPUT_CONFIG_REQ",
200 	[MSG_SMS_CLOCK_OUTPUT_CONFIG_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_CLOCK_OUTPUT_CONFIG_RES",
201 	[MSG_SMS_I2C_SET_FREQ_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_I2C_SET_FREQ_REQ",
202 	[MSG_SMS_I2C_SET_FREQ_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_I2C_SET_FREQ_RES",
203 	[MSG_SMS_GENERIC_I2C_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_I2C_REQ",
204 	[MSG_SMS_GENERIC_I2C_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_GENERIC_I2C_RES",
205 	[MSG_SMS_DVBT_BDA_DATA                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_DVBT_BDA_DATA",
206 	[MSG_SW_RELOAD_REQ                           - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_REQ",
207 	[MSG_SMS_DATA_MSG                            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_MSG",
208 	[MSG_TABLE_UPLOAD_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_TABLE_UPLOAD_REQ",
209 	[MSG_TABLE_UPLOAD_RES                        - MSG_TYPE_BASE_VAL] = "MSG_TABLE_UPLOAD_RES",
210 	[MSG_SW_RELOAD_START_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_START_REQ",
211 	[MSG_SW_RELOAD_START_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_START_RES",
212 	[MSG_SW_RELOAD_EXEC_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_EXEC_REQ",
213 	[MSG_SW_RELOAD_EXEC_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SW_RELOAD_EXEC_RES",
214 	[MSG_SMS_SPI_INT_LINE_SET_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_INT_LINE_SET_REQ",
215 	[MSG_SMS_SPI_INT_LINE_SET_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_INT_LINE_SET_RES",
216 	[MSG_SMS_GPIO_CONFIG_EX_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_CONFIG_EX_REQ",
217 	[MSG_SMS_GPIO_CONFIG_EX_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_GPIO_CONFIG_EX_RES",
218 	[MSG_SMS_WATCHDOG_ACT_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_WATCHDOG_ACT_REQ",
219 	[MSG_SMS_WATCHDOG_ACT_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_WATCHDOG_ACT_RES",
220 	[MSG_SMS_LOOPBACK_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOOPBACK_REQ",
221 	[MSG_SMS_LOOPBACK_RES                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOOPBACK_RES",
222 	[MSG_SMS_RAW_CAPTURE_START_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_START_REQ",
223 	[MSG_SMS_RAW_CAPTURE_START_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_START_RES",
224 	[MSG_SMS_RAW_CAPTURE_ABORT_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_ABORT_REQ",
225 	[MSG_SMS_RAW_CAPTURE_ABORT_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_ABORT_RES",
226 	[MSG_SMS_RAW_CAPTURE_COMPLETE_IND            - MSG_TYPE_BASE_VAL] = "MSG_SMS_RAW_CAPTURE_COMPLETE_IND",
227 	[MSG_SMS_DATA_PUMP_IND                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_PUMP_IND",
228 	[MSG_SMS_DATA_PUMP_REQ                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_PUMP_REQ",
229 	[MSG_SMS_DATA_PUMP_RES                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_DATA_PUMP_RES",
230 	[MSG_SMS_FLASH_DL_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_FLASH_DL_REQ",
231 	[MSG_SMS_EXEC_TEST_1_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXEC_TEST_1_REQ",
232 	[MSG_SMS_EXEC_TEST_1_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXEC_TEST_1_RES",
233 	[MSG_SMS_ENABLE_TS_INTERFACE_REQ             - MSG_TYPE_BASE_VAL] = "MSG_SMS_ENABLE_TS_INTERFACE_REQ",
234 	[MSG_SMS_ENABLE_TS_INTERFACE_RES             - MSG_TYPE_BASE_VAL] = "MSG_SMS_ENABLE_TS_INTERFACE_RES",
235 	[MSG_SMS_SPI_SET_BUS_WIDTH_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_SET_BUS_WIDTH_REQ",
236 	[MSG_SMS_SPI_SET_BUS_WIDTH_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SPI_SET_BUS_WIDTH_RES",
237 	[MSG_SMS_SEND_EMM_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_EMM_REQ",
238 	[MSG_SMS_SEND_EMM_RES                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_EMM_RES",
239 	[MSG_SMS_DISABLE_TS_INTERFACE_REQ            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DISABLE_TS_INTERFACE_REQ",
240 	[MSG_SMS_DISABLE_TS_INTERFACE_RES            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DISABLE_TS_INTERFACE_RES",
241 	[MSG_SMS_IS_BUF_FREE_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_IS_BUF_FREE_REQ",
242 	[MSG_SMS_IS_BUF_FREE_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_IS_BUF_FREE_RES",
243 	[MSG_SMS_EXT_ANTENNA_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXT_ANTENNA_REQ",
244 	[MSG_SMS_EXT_ANTENNA_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXT_ANTENNA_RES",
245 	[MSG_SMS_CMMB_GET_NET_OF_FREQ_REQ_OBSOLETE   - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_NET_OF_FREQ_REQ_OBSOLETE",
246 	[MSG_SMS_CMMB_GET_NET_OF_FREQ_RES_OBSOLETE   - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_NET_OF_FREQ_RES_OBSOLETE",
247 	[MSG_SMS_BATTERY_LEVEL_REQ                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_BATTERY_LEVEL_REQ",
248 	[MSG_SMS_BATTERY_LEVEL_RES                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_BATTERY_LEVEL_RES",
249 	[MSG_SMS_CMMB_INJECT_TABLE_REQ_OBSOLETE      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_INJECT_TABLE_REQ_OBSOLETE",
250 	[MSG_SMS_CMMB_INJECT_TABLE_RES_OBSOLETE      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_INJECT_TABLE_RES_OBSOLETE",
251 	[MSG_SMS_FM_RADIO_BLOCK_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_FM_RADIO_BLOCK_IND",
252 	[MSG_SMS_HOST_NOTIFICATION_IND               - MSG_TYPE_BASE_VAL] = "MSG_SMS_HOST_NOTIFICATION_IND",
253 	[MSG_SMS_CMMB_GET_CONTROL_TABLE_REQ_OBSOLETE - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_CONTROL_TABLE_REQ_OBSOLETE",
254 	[MSG_SMS_CMMB_GET_CONTROL_TABLE_RES_OBSOLETE - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_CONTROL_TABLE_RES_OBSOLETE",
255 	[MSG_SMS_CMMB_GET_NETWORKS_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_NETWORKS_REQ",
256 	[MSG_SMS_CMMB_GET_NETWORKS_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_NETWORKS_RES",
257 	[MSG_SMS_CMMB_START_SERVICE_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_START_SERVICE_REQ",
258 	[MSG_SMS_CMMB_START_SERVICE_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_START_SERVICE_RES",
259 	[MSG_SMS_CMMB_STOP_SERVICE_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_STOP_SERVICE_REQ",
260 	[MSG_SMS_CMMB_STOP_SERVICE_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_STOP_SERVICE_RES",
261 	[MSG_SMS_CMMB_ADD_CHANNEL_FILTER_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_ADD_CHANNEL_FILTER_REQ",
262 	[MSG_SMS_CMMB_ADD_CHANNEL_FILTER_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_ADD_CHANNEL_FILTER_RES",
263 	[MSG_SMS_CMMB_REMOVE_CHANNEL_FILTER_REQ      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_REMOVE_CHANNEL_FILTER_REQ",
264 	[MSG_SMS_CMMB_REMOVE_CHANNEL_FILTER_RES      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_REMOVE_CHANNEL_FILTER_RES",
265 	[MSG_SMS_CMMB_START_CONTROL_INFO_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_START_CONTROL_INFO_REQ",
266 	[MSG_SMS_CMMB_START_CONTROL_INFO_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_START_CONTROL_INFO_RES",
267 	[MSG_SMS_CMMB_STOP_CONTROL_INFO_REQ          - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_STOP_CONTROL_INFO_REQ",
268 	[MSG_SMS_CMMB_STOP_CONTROL_INFO_RES          - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_STOP_CONTROL_INFO_RES",
269 	[MSG_SMS_ISDBT_TUNE_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_TUNE_REQ",
270 	[MSG_SMS_ISDBT_TUNE_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_ISDBT_TUNE_RES",
271 	[MSG_SMS_TRANSMISSION_IND                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_TRANSMISSION_IND",
272 	[MSG_SMS_PID_STATISTICS_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_PID_STATISTICS_IND",
273 	[MSG_SMS_POWER_DOWN_IND                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_DOWN_IND",
274 	[MSG_SMS_POWER_DOWN_CONF                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_DOWN_CONF",
275 	[MSG_SMS_POWER_UP_IND                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_UP_IND",
276 	[MSG_SMS_POWER_UP_CONF                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_UP_CONF",
277 	[MSG_SMS_POWER_MODE_SET_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_MODE_SET_REQ",
278 	[MSG_SMS_POWER_MODE_SET_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_POWER_MODE_SET_RES",
279 	[MSG_SMS_DEBUG_HOST_EVENT_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DEBUG_HOST_EVENT_REQ",
280 	[MSG_SMS_DEBUG_HOST_EVENT_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DEBUG_HOST_EVENT_RES",
281 	[MSG_SMS_NEW_CRYSTAL_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_NEW_CRYSTAL_REQ",
282 	[MSG_SMS_NEW_CRYSTAL_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_NEW_CRYSTAL_RES",
283 	[MSG_SMS_CONFIG_SPI_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CONFIG_SPI_REQ",
284 	[MSG_SMS_CONFIG_SPI_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_CONFIG_SPI_RES",
285 	[MSG_SMS_I2C_SHORT_STAT_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_I2C_SHORT_STAT_IND",
286 	[MSG_SMS_START_IR_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_START_IR_REQ",
287 	[MSG_SMS_START_IR_RES                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_START_IR_RES",
288 	[MSG_SMS_IR_SAMPLES_IND                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_IR_SAMPLES_IND",
289 	[MSG_SMS_CMMB_CA_SERVICE_IND                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_CA_SERVICE_IND",
290 	[MSG_SMS_SLAVE_DEVICE_DETECTED               - MSG_TYPE_BASE_VAL] = "MSG_SMS_SLAVE_DEVICE_DETECTED",
291 	[MSG_SMS_INTERFACE_LOCK_IND                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_INTERFACE_LOCK_IND",
292 	[MSG_SMS_INTERFACE_UNLOCK_IND                - MSG_TYPE_BASE_VAL] = "MSG_SMS_INTERFACE_UNLOCK_IND",
293 	[MSG_SMS_SEND_ROSUM_BUFF_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_ROSUM_BUFF_REQ",
294 	[MSG_SMS_SEND_ROSUM_BUFF_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_ROSUM_BUFF_RES",
295 	[MSG_SMS_ROSUM_BUFF                          - MSG_TYPE_BASE_VAL] = "MSG_SMS_ROSUM_BUFF",
296 	[MSG_SMS_SET_AES128_KEY_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_AES128_KEY_REQ",
297 	[MSG_SMS_SET_AES128_KEY_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_AES128_KEY_RES",
298 	[MSG_SMS_MBBMS_WRITE_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_MBBMS_WRITE_REQ",
299 	[MSG_SMS_MBBMS_WRITE_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_MBBMS_WRITE_RES",
300 	[MSG_SMS_MBBMS_READ_IND                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_MBBMS_READ_IND",
301 	[MSG_SMS_IQ_STREAM_START_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_START_REQ",
302 	[MSG_SMS_IQ_STREAM_START_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_START_RES",
303 	[MSG_SMS_IQ_STREAM_STOP_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_STOP_REQ",
304 	[MSG_SMS_IQ_STREAM_STOP_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_STOP_RES",
305 	[MSG_SMS_IQ_STREAM_DATA_BLOCK                - MSG_TYPE_BASE_VAL] = "MSG_SMS_IQ_STREAM_DATA_BLOCK",
306 	[MSG_SMS_GET_EEPROM_VERSION_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_EEPROM_VERSION_REQ",
307 	[MSG_SMS_GET_EEPROM_VERSION_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_GET_EEPROM_VERSION_RES",
308 	[MSG_SMS_SIGNAL_DETECTED_IND                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SIGNAL_DETECTED_IND",
309 	[MSG_SMS_NO_SIGNAL_IND                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_NO_SIGNAL_IND",
310 	[MSG_SMS_MRC_SHUTDOWN_SLAVE_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_SHUTDOWN_SLAVE_REQ",
311 	[MSG_SMS_MRC_SHUTDOWN_SLAVE_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_SHUTDOWN_SLAVE_RES",
312 	[MSG_SMS_MRC_BRINGUP_SLAVE_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_BRINGUP_SLAVE_REQ",
313 	[MSG_SMS_MRC_BRINGUP_SLAVE_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_BRINGUP_SLAVE_RES",
314 	[MSG_SMS_EXTERNAL_LNA_CTRL_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXTERNAL_LNA_CTRL_REQ",
315 	[MSG_SMS_EXTERNAL_LNA_CTRL_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_EXTERNAL_LNA_CTRL_RES",
316 	[MSG_SMS_SET_PERIODIC_STATISTICS_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_PERIODIC_STATISTICS_REQ",
317 	[MSG_SMS_SET_PERIODIC_STATISTICS_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_SET_PERIODIC_STATISTICS_RES",
318 	[MSG_SMS_CMMB_SET_AUTO_OUTPUT_TS0_REQ        - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_AUTO_OUTPUT_TS0_REQ",
319 	[MSG_SMS_CMMB_SET_AUTO_OUTPUT_TS0_RES        - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_AUTO_OUTPUT_TS0_RES",
320 	[LOCAL_TUNE                                  - MSG_TYPE_BASE_VAL] = "LOCAL_TUNE",
321 	[LOCAL_IFFT_H_ICI                            - MSG_TYPE_BASE_VAL] = "LOCAL_IFFT_H_ICI",
322 	[MSG_RESYNC_REQ                              - MSG_TYPE_BASE_VAL] = "MSG_RESYNC_REQ",
323 	[MSG_SMS_CMMB_GET_MRC_STATISTICS_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_MRC_STATISTICS_REQ",
324 	[MSG_SMS_CMMB_GET_MRC_STATISTICS_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_MRC_STATISTICS_RES",
325 	[MSG_SMS_LOG_EX_ITEM                         - MSG_TYPE_BASE_VAL] = "MSG_SMS_LOG_EX_ITEM",
326 	[MSG_SMS_DEVICE_DATA_LOSS_IND                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DEVICE_DATA_LOSS_IND",
327 	[MSG_SMS_MRC_WATCHDOG_TRIGGERED_IND          - MSG_TYPE_BASE_VAL] = "MSG_SMS_MRC_WATCHDOG_TRIGGERED_IND",
328 	[MSG_SMS_USER_MSG_REQ                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_USER_MSG_REQ",
329 	[MSG_SMS_USER_MSG_RES                        - MSG_TYPE_BASE_VAL] = "MSG_SMS_USER_MSG_RES",
330 	[MSG_SMS_SMART_CARD_INIT_REQ                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_INIT_REQ",
331 	[MSG_SMS_SMART_CARD_INIT_RES                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_INIT_RES",
332 	[MSG_SMS_SMART_CARD_WRITE_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_WRITE_REQ",
333 	[MSG_SMS_SMART_CARD_WRITE_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_WRITE_RES",
334 	[MSG_SMS_SMART_CARD_READ_IND                 - MSG_TYPE_BASE_VAL] = "MSG_SMS_SMART_CARD_READ_IND",
335 	[MSG_SMS_TSE_ENABLE_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_TSE_ENABLE_REQ",
336 	[MSG_SMS_TSE_ENABLE_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_TSE_ENABLE_RES",
337 	[MSG_SMS_CMMB_GET_SHORT_STATISTICS_REQ       - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_SHORT_STATISTICS_REQ",
338 	[MSG_SMS_CMMB_GET_SHORT_STATISTICS_RES       - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_GET_SHORT_STATISTICS_RES",
339 	[MSG_SMS_LED_CONFIG_REQ                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_LED_CONFIG_REQ",
340 	[MSG_SMS_LED_CONFIG_RES                      - MSG_TYPE_BASE_VAL] = "MSG_SMS_LED_CONFIG_RES",
341 	[MSG_PWM_ANTENNA_REQ                         - MSG_TYPE_BASE_VAL] = "MSG_PWM_ANTENNA_REQ",
342 	[MSG_PWM_ANTENNA_RES                         - MSG_TYPE_BASE_VAL] = "MSG_PWM_ANTENNA_RES",
343 	[MSG_SMS_CMMB_SMD_SN_REQ                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SMD_SN_REQ",
344 	[MSG_SMS_CMMB_SMD_SN_RES                     - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SMD_SN_RES",
345 	[MSG_SMS_CMMB_SET_CA_CW_REQ                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_CA_CW_REQ",
346 	[MSG_SMS_CMMB_SET_CA_CW_RES                  - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_CA_CW_RES",
347 	[MSG_SMS_CMMB_SET_CA_SALT_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_CA_SALT_REQ",
348 	[MSG_SMS_CMMB_SET_CA_SALT_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_CMMB_SET_CA_SALT_RES",
349 	[MSG_SMS_NSCD_INIT_REQ                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_INIT_REQ",
350 	[MSG_SMS_NSCD_INIT_RES                       - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_INIT_RES",
351 	[MSG_SMS_NSCD_PROCESS_SECTION_REQ            - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_PROCESS_SECTION_REQ",
352 	[MSG_SMS_NSCD_PROCESS_SECTION_RES            - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_PROCESS_SECTION_RES",
353 	[MSG_SMS_DBD_CREATE_OBJECT_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_CREATE_OBJECT_REQ",
354 	[MSG_SMS_DBD_CREATE_OBJECT_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_CREATE_OBJECT_RES",
355 	[MSG_SMS_DBD_CONFIGURE_REQ                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_CONFIGURE_REQ",
356 	[MSG_SMS_DBD_CONFIGURE_RES                   - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_CONFIGURE_RES",
357 	[MSG_SMS_DBD_SET_KEYS_REQ                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_SET_KEYS_REQ",
358 	[MSG_SMS_DBD_SET_KEYS_RES                    - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_SET_KEYS_RES",
359 	[MSG_SMS_DBD_PROCESS_HEADER_REQ              - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_HEADER_REQ",
360 	[MSG_SMS_DBD_PROCESS_HEADER_RES              - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_HEADER_RES",
361 	[MSG_SMS_DBD_PROCESS_DATA_REQ                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_DATA_REQ",
362 	[MSG_SMS_DBD_PROCESS_DATA_RES                - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_DATA_RES",
363 	[MSG_SMS_DBD_PROCESS_GET_DATA_REQ            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_GET_DATA_REQ",
364 	[MSG_SMS_DBD_PROCESS_GET_DATA_RES            - MSG_TYPE_BASE_VAL] = "MSG_SMS_DBD_PROCESS_GET_DATA_RES",
365 	[MSG_SMS_NSCD_OPEN_SESSION_REQ               - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_OPEN_SESSION_REQ",
366 	[MSG_SMS_NSCD_OPEN_SESSION_RES               - MSG_TYPE_BASE_VAL] = "MSG_SMS_NSCD_OPEN_SESSION_RES",
367 	[MSG_SMS_SEND_HOST_DATA_TO_DEMUX_REQ         - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_HOST_DATA_TO_DEMUX_REQ",
368 	[MSG_SMS_SEND_HOST_DATA_TO_DEMUX_RES         - MSG_TYPE_BASE_VAL] = "MSG_SMS_SEND_HOST_DATA_TO_DEMUX_RES",
369 	[MSG_LAST_MSG_TYPE                           - MSG_TYPE_BASE_VAL] = "MSG_LAST_MSG_TYPE",
370 };
371 
372 char *smscore_translate_msg(enum msg_types msgtype)
373 {
374 	int i = msgtype - MSG_TYPE_BASE_VAL;
375 	char *msg;
376 
377 	if (i < 0 || i >= ARRAY_SIZE(siano_msgs))
378 		return "Unknown msg type";
379 
380 	msg = siano_msgs[i];
381 
382 	if (!*msg)
383 		return "Unknown msg type";
384 
385 	return msg;
386 }
387 EXPORT_SYMBOL_GPL(smscore_translate_msg);
388 
389 void smscore_set_board_id(struct smscore_device_t *core, int id)
390 {
391 	core->board_id = id;
392 }
393 
394 int smscore_led_state(struct smscore_device_t *core, int led)
395 {
396 	if (led >= 0)
397 		core->led_state = led;
398 	return core->led_state;
399 }
400 EXPORT_SYMBOL_GPL(smscore_set_board_id);
401 
402 int smscore_get_board_id(struct smscore_device_t *core)
403 {
404 	return core->board_id;
405 }
406 EXPORT_SYMBOL_GPL(smscore_get_board_id);
407 
408 struct smscore_registry_entry_t {
409 	struct list_head entry;
410 	char devpath[32];
411 	int mode;
412 	enum sms_device_type_st	type;
413 };
414 
415 static struct list_head g_smscore_notifyees;
416 static struct list_head g_smscore_devices;
417 static struct mutex g_smscore_deviceslock;
418 
419 static struct list_head g_smscore_registry;
420 static struct mutex g_smscore_registrylock;
421 
422 static int default_mode = DEVICE_MODE_NONE;
423 
424 module_param(default_mode, int, 0644);
425 MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");
426 
427 static struct smscore_registry_entry_t *smscore_find_registry(char *devpath)
428 {
429 	struct smscore_registry_entry_t *entry;
430 	struct list_head *next;
431 
432 	mutex_lock(&g_smscore_registrylock);
433 	for (next = g_smscore_registry.next;
434 	     next != &g_smscore_registry;
435 	     next = next->next) {
436 		entry = (struct smscore_registry_entry_t *) next;
437 		if (!strncmp(entry->devpath, devpath, sizeof(entry->devpath))) {
438 			mutex_unlock(&g_smscore_registrylock);
439 			return entry;
440 		}
441 	}
442 	entry = kmalloc(sizeof(*entry), GFP_KERNEL);
443 	if (entry) {
444 		entry->mode = default_mode;
445 		strscpy(entry->devpath, devpath, sizeof(entry->devpath));
446 		list_add(&entry->entry, &g_smscore_registry);
447 	} else
448 		pr_err("failed to create smscore_registry.\n");
449 	mutex_unlock(&g_smscore_registrylock);
450 	return entry;
451 }
452 
453 int smscore_registry_getmode(char *devpath)
454 {
455 	struct smscore_registry_entry_t *entry;
456 
457 	entry = smscore_find_registry(devpath);
458 	if (entry)
459 		return entry->mode;
460 	else
461 		pr_err("No registry found.\n");
462 
463 	return default_mode;
464 }
465 EXPORT_SYMBOL_GPL(smscore_registry_getmode);
466 
467 static enum sms_device_type_st smscore_registry_gettype(char *devpath)
468 {
469 	struct smscore_registry_entry_t *entry;
470 
471 	entry = smscore_find_registry(devpath);
472 	if (entry)
473 		return entry->type;
474 	else
475 		pr_err("No registry found.\n");
476 
477 	return -EINVAL;
478 }
479 
480 static void smscore_registry_setmode(char *devpath, int mode)
481 {
482 	struct smscore_registry_entry_t *entry;
483 
484 	entry = smscore_find_registry(devpath);
485 	if (entry)
486 		entry->mode = mode;
487 	else
488 		pr_err("No registry found.\n");
489 }
490 
491 static void smscore_registry_settype(char *devpath,
492 				     enum sms_device_type_st type)
493 {
494 	struct smscore_registry_entry_t *entry;
495 
496 	entry = smscore_find_registry(devpath);
497 	if (entry)
498 		entry->type = type;
499 	else
500 		pr_err("No registry found.\n");
501 }
502 
503 
504 static void list_add_locked(struct list_head *new, struct list_head *head,
505 			    spinlock_t *lock)
506 {
507 	unsigned long flags;
508 
509 	spin_lock_irqsave(lock, flags);
510 
511 	list_add(new, head);
512 
513 	spin_unlock_irqrestore(lock, flags);
514 }
515 
516 /*
517  * register a client callback that called when device plugged in/unplugged
518  * NOTE: if devices exist callback is called immediately for each device
519  *
520  * @param hotplug callback
521  *
522  * return: 0 on success, <0 on error.
523  */
524 int smscore_register_hotplug(hotplug_t hotplug)
525 {
526 	struct smscore_device_notifyee_t *notifyee;
527 	struct list_head *next, *first;
528 	int rc = 0;
529 
530 	mutex_lock(&g_smscore_deviceslock);
531 	notifyee = kmalloc(sizeof(*notifyee), GFP_KERNEL);
532 	if (notifyee) {
533 		/* now notify callback about existing devices */
534 		first = &g_smscore_devices;
535 		for (next = first->next;
536 		     next != first && !rc;
537 		     next = next->next) {
538 			struct smscore_device_t *coredev =
539 				(struct smscore_device_t *) next;
540 			rc = hotplug(coredev, coredev->device, 1);
541 		}
542 
543 		if (rc >= 0) {
544 			notifyee->hotplug = hotplug;
545 			list_add(&notifyee->entry, &g_smscore_notifyees);
546 		} else
547 			kfree(notifyee);
548 	} else
549 		rc = -ENOMEM;
550 
551 	mutex_unlock(&g_smscore_deviceslock);
552 
553 	return rc;
554 }
555 EXPORT_SYMBOL_GPL(smscore_register_hotplug);
556 
557 /*
558  * unregister a client callback that called when device plugged in/unplugged
559  *
560  * @param hotplug callback
561  *
562  */
563 void smscore_unregister_hotplug(hotplug_t hotplug)
564 {
565 	struct list_head *next, *first;
566 
567 	mutex_lock(&g_smscore_deviceslock);
568 
569 	first = &g_smscore_notifyees;
570 
571 	for (next = first->next; next != first;) {
572 		struct smscore_device_notifyee_t *notifyee =
573 			(struct smscore_device_notifyee_t *) next;
574 		next = next->next;
575 
576 		if (notifyee->hotplug == hotplug) {
577 			list_del(&notifyee->entry);
578 			kfree(notifyee);
579 		}
580 	}
581 
582 	mutex_unlock(&g_smscore_deviceslock);
583 }
584 EXPORT_SYMBOL_GPL(smscore_unregister_hotplug);
585 
586 static void smscore_notify_clients(struct smscore_device_t *coredev)
587 {
588 	struct smscore_client_t *client;
589 
590 	/* the client must call smscore_unregister_client from remove handler */
591 	while (!list_empty(&coredev->clients)) {
592 		client = (struct smscore_client_t *) coredev->clients.next;
593 		client->onremove_handler(client->context);
594 	}
595 }
596 
597 static int smscore_notify_callbacks(struct smscore_device_t *coredev,
598 				    struct device *device, int arrival)
599 {
600 	struct smscore_device_notifyee_t *elem;
601 	int rc = 0;
602 
603 	/* note: must be called under g_deviceslock */
604 
605 	list_for_each_entry(elem, &g_smscore_notifyees, entry) {
606 		rc = elem->hotplug(coredev, device, arrival);
607 		if (rc < 0)
608 			break;
609 	}
610 
611 	return rc;
612 }
613 
614 static struct
615 smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
616 				       dma_addr_t common_buffer_phys)
617 {
618 	struct smscore_buffer_t *cb;
619 
620 	cb = kzalloc(sizeof(*cb), GFP_KERNEL);
621 	if (!cb)
622 		return NULL;
623 
624 	cb->p = buffer;
625 	cb->offset_in_common = buffer - (u8 *) common_buffer;
626 	if (common_buffer_phys)
627 		cb->phys = common_buffer_phys + cb->offset_in_common;
628 
629 	return cb;
630 }
631 
632 /*
633  * creates coredev object for a device, prepares buffers,
634  * creates buffer mappings, notifies registered hotplugs about new device.
635  *
636  * @param params device pointer to struct with device specific parameters
637  *               and handlers
638  * @param coredev pointer to a value that receives created coredev object
639  *
640  * return: 0 on success, <0 on error.
641  */
642 int smscore_register_device(struct smsdevice_params_t *params,
643 			    struct smscore_device_t **coredev,
644 			    gfp_t gfp_buf_flags,
645 			    void *mdev)
646 {
647 	struct smscore_device_t *dev;
648 	u8 *buffer;
649 
650 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
651 	if (!dev)
652 		return -ENOMEM;
653 
654 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
655 	dev->media_dev = mdev;
656 #endif
657 	dev->gfp_buf_flags = gfp_buf_flags;
658 
659 	/* init list entry so it could be safe in smscore_unregister_device */
660 	INIT_LIST_HEAD(&dev->entry);
661 
662 	/* init queues */
663 	INIT_LIST_HEAD(&dev->clients);
664 	INIT_LIST_HEAD(&dev->buffers);
665 
666 	/* init locks */
667 	spin_lock_init(&dev->clientslock);
668 	spin_lock_init(&dev->bufferslock);
669 
670 	/* init completion events */
671 	init_completion(&dev->version_ex_done);
672 	init_completion(&dev->data_download_done);
673 	init_completion(&dev->data_validity_done);
674 	init_completion(&dev->trigger_done);
675 	init_completion(&dev->init_device_done);
676 	init_completion(&dev->reload_start_done);
677 	init_completion(&dev->resume_done);
678 	init_completion(&dev->gpio_configuration_done);
679 	init_completion(&dev->gpio_set_level_done);
680 	init_completion(&dev->gpio_get_level_done);
681 	init_completion(&dev->ir_init_done);
682 
683 	/* Buffer management */
684 	init_waitqueue_head(&dev->buffer_mng_waitq);
685 
686 	/* alloc common buffer */
687 	dev->common_buffer_size = params->buffer_size * params->num_buffers;
688 	if (params->usb_device)
689 		buffer = kzalloc(dev->common_buffer_size, GFP_KERNEL);
690 	else
691 		buffer = dma_alloc_coherent(params->device,
692 					    dev->common_buffer_size,
693 					    &dev->common_buffer_phys,
694 					    GFP_KERNEL | dev->gfp_buf_flags);
695 	if (!buffer) {
696 		smscore_unregister_device(dev);
697 		return -ENOMEM;
698 	}
699 	dev->common_buffer = buffer;
700 
701 	/* prepare dma buffers */
702 	for (; dev->num_buffers < params->num_buffers;
703 	     dev->num_buffers++, buffer += params->buffer_size) {
704 		struct smscore_buffer_t *cb;
705 
706 		cb = smscore_createbuffer(buffer, dev->common_buffer,
707 					  dev->common_buffer_phys);
708 		if (!cb) {
709 			smscore_unregister_device(dev);
710 			return -ENOMEM;
711 		}
712 
713 		smscore_putbuffer(dev, cb);
714 	}
715 
716 	pr_debug("allocated %d buffers\n", dev->num_buffers);
717 
718 	dev->mode = DEVICE_MODE_NONE;
719 	dev->board_id = SMS_BOARD_UNKNOWN;
720 	dev->context = params->context;
721 	dev->device = params->device;
722 	dev->usb_device = params->usb_device;
723 	dev->setmode_handler = params->setmode_handler;
724 	dev->detectmode_handler = params->detectmode_handler;
725 	dev->sendrequest_handler = params->sendrequest_handler;
726 	dev->preload_handler = params->preload_handler;
727 	dev->postload_handler = params->postload_handler;
728 
729 	dev->device_flags = params->flags;
730 	strscpy(dev->devpath, params->devpath, sizeof(dev->devpath));
731 
732 	smscore_registry_settype(dev->devpath, params->device_type);
733 
734 	/* add device to devices list */
735 	mutex_lock(&g_smscore_deviceslock);
736 	list_add(&dev->entry, &g_smscore_devices);
737 	mutex_unlock(&g_smscore_deviceslock);
738 
739 	*coredev = dev;
740 
741 	pr_debug("device %p created\n", dev);
742 
743 	return 0;
744 }
745 EXPORT_SYMBOL_GPL(smscore_register_device);
746 
747 
748 static int smscore_sendrequest_and_wait(struct smscore_device_t *coredev,
749 		void *buffer, size_t size, struct completion *completion) {
750 	int rc;
751 
752 	if (!completion)
753 		return -EINVAL;
754 	init_completion(completion);
755 
756 	rc = coredev->sendrequest_handler(coredev->context, buffer, size);
757 	if (rc < 0) {
758 		pr_info("sendrequest returned error %d\n", rc);
759 		return rc;
760 	}
761 
762 	return wait_for_completion_timeout(completion,
763 			msecs_to_jiffies(SMS_PROTOCOL_MAX_RAOUNDTRIP_MS)) ?
764 			0 : -ETIME;
765 }
766 
767 /*
768  * Starts & enables IR operations
769  *
770  * return: 0 on success, < 0 on error.
771  */
772 static int smscore_init_ir(struct smscore_device_t *coredev)
773 {
774 	int ir_io;
775 	int rc;
776 	void *buffer;
777 
778 	coredev->ir.dev = NULL;
779 	ir_io = sms_get_board(smscore_get_board_id(coredev))->board_cfg.ir;
780 	if (ir_io) {/* only if IR port exist we use IR sub-module */
781 		pr_debug("IR loading\n");
782 		rc = sms_ir_init(coredev);
783 
784 		if	(rc != 0)
785 			pr_err("Error initialization DTV IR sub-module\n");
786 		else {
787 			buffer = kmalloc(sizeof(struct sms_msg_data2) +
788 						SMS_DMA_ALIGNMENT,
789 						GFP_KERNEL | coredev->gfp_buf_flags);
790 			if (buffer) {
791 				struct sms_msg_data2 *msg =
792 				(struct sms_msg_data2 *)
793 				SMS_ALIGN_ADDRESS(buffer);
794 
795 				SMS_INIT_MSG(&msg->x_msg_header,
796 						MSG_SMS_START_IR_REQ,
797 						sizeof(struct sms_msg_data2));
798 				msg->msg_data[0] = coredev->ir.controller;
799 				msg->msg_data[1] = coredev->ir.timeout;
800 
801 				rc = smscore_sendrequest_and_wait(coredev, msg,
802 						msg->x_msg_header. msg_length,
803 						&coredev->ir_init_done);
804 
805 				kfree(buffer);
806 			} else
807 				pr_err("Sending IR initialization message failed\n");
808 		}
809 	} else
810 		pr_info("IR port has not been detected\n");
811 
812 	return 0;
813 }
814 
815 /*
816  * configures device features according to board configuration structure.
817  *
818  * @param coredev pointer to a coredev object returned by
819  *                smscore_register_device
820  *
821  * return: 0 on success, <0 on error.
822  */
823 static int smscore_configure_board(struct smscore_device_t *coredev)
824 {
825 	struct sms_board *board;
826 
827 	board = sms_get_board(coredev->board_id);
828 	if (!board) {
829 		pr_err("no board configuration exist.\n");
830 		return -EINVAL;
831 	}
832 
833 	if (board->mtu) {
834 		struct sms_msg_data mtu_msg;
835 		pr_debug("set max transmit unit %d\n", board->mtu);
836 
837 		mtu_msg.x_msg_header.msg_src_id = 0;
838 		mtu_msg.x_msg_header.msg_dst_id = HIF_TASK;
839 		mtu_msg.x_msg_header.msg_flags = 0;
840 		mtu_msg.x_msg_header.msg_type = MSG_SMS_SET_MAX_TX_MSG_LEN_REQ;
841 		mtu_msg.x_msg_header.msg_length = sizeof(mtu_msg);
842 		mtu_msg.msg_data[0] = board->mtu;
843 
844 		coredev->sendrequest_handler(coredev->context, &mtu_msg,
845 					     sizeof(mtu_msg));
846 	}
847 
848 	if (board->crystal) {
849 		struct sms_msg_data crys_msg;
850 		pr_debug("set crystal value %d\n", board->crystal);
851 
852 		SMS_INIT_MSG(&crys_msg.x_msg_header,
853 				MSG_SMS_NEW_CRYSTAL_REQ,
854 				sizeof(crys_msg));
855 		crys_msg.msg_data[0] = board->crystal;
856 
857 		coredev->sendrequest_handler(coredev->context, &crys_msg,
858 					     sizeof(crys_msg));
859 	}
860 
861 	return 0;
862 }
863 
864 /*
865  * sets initial device mode and notifies client hotplugs that device is ready
866  *
867  * @param coredev pointer to a coredev object returned by
868  *		  smscore_register_device
869  *
870  * return: 0 on success, <0 on error.
871  */
872 int smscore_start_device(struct smscore_device_t *coredev)
873 {
874 	int rc;
875 	int board_id = smscore_get_board_id(coredev);
876 	int mode = smscore_registry_getmode(coredev->devpath);
877 
878 	/* Device is initialized as DEVICE_MODE_NONE */
879 	if (board_id != SMS_BOARD_UNKNOWN && mode == DEVICE_MODE_NONE)
880 		mode = sms_get_board(board_id)->default_mode;
881 
882 	rc = smscore_set_device_mode(coredev, mode);
883 	if (rc < 0) {
884 		pr_info("set device mode failed , rc %d\n", rc);
885 		return rc;
886 	}
887 	rc = smscore_configure_board(coredev);
888 	if (rc < 0) {
889 		pr_info("configure board failed , rc %d\n", rc);
890 		return rc;
891 	}
892 
893 	mutex_lock(&g_smscore_deviceslock);
894 
895 	rc = smscore_notify_callbacks(coredev, coredev->device, 1);
896 	smscore_init_ir(coredev);
897 
898 	pr_debug("device %p started, rc %d\n", coredev, rc);
899 
900 	mutex_unlock(&g_smscore_deviceslock);
901 
902 	return rc;
903 }
904 EXPORT_SYMBOL_GPL(smscore_start_device);
905 
906 
907 static int smscore_load_firmware_family2(struct smscore_device_t *coredev,
908 					 void *buffer, size_t size)
909 {
910 	struct sms_firmware *firmware = (struct sms_firmware *) buffer;
911 	struct sms_msg_data4 *msg;
912 	u32 mem_address,  calc_checksum = 0;
913 	u32 i, *ptr;
914 	u8 *payload = firmware->payload;
915 	int rc = 0;
916 	firmware->start_address = le32_to_cpup((__le32 *)&firmware->start_address);
917 	firmware->length = le32_to_cpup((__le32 *)&firmware->length);
918 
919 	mem_address = firmware->start_address;
920 
921 	pr_debug("loading FW to addr 0x%x size %d\n",
922 		 mem_address, firmware->length);
923 	if (coredev->preload_handler) {
924 		rc = coredev->preload_handler(coredev->context);
925 		if (rc < 0)
926 			return rc;
927 	}
928 
929 	/* PAGE_SIZE buffer shall be enough and dma aligned */
930 	msg = kmalloc(PAGE_SIZE, GFP_KERNEL | coredev->gfp_buf_flags);
931 	if (!msg)
932 		return -ENOMEM;
933 
934 	if (coredev->mode != DEVICE_MODE_NONE) {
935 		pr_debug("sending reload command.\n");
936 		SMS_INIT_MSG(&msg->x_msg_header, MSG_SW_RELOAD_START_REQ,
937 			     sizeof(struct sms_msg_hdr));
938 		rc = smscore_sendrequest_and_wait(coredev, msg,
939 						  msg->x_msg_header.msg_length,
940 						  &coredev->reload_start_done);
941 		if (rc < 0) {
942 			pr_err("device reload failed, rc %d\n", rc);
943 			goto exit_fw_download;
944 		}
945 		mem_address = *(u32 *) &payload[20];
946 	}
947 
948 	for (i = 0, ptr = (u32 *)firmware->payload; i < firmware->length/4 ;
949 	     i++, ptr++)
950 		calc_checksum += *ptr;
951 
952 	while (size && rc >= 0) {
953 		struct sms_data_download *data_msg =
954 			(struct sms_data_download *) msg;
955 		int payload_size = min_t(int, size, SMS_MAX_PAYLOAD_SIZE);
956 
957 		SMS_INIT_MSG(&msg->x_msg_header, MSG_SMS_DATA_DOWNLOAD_REQ,
958 			     (u16)(sizeof(struct sms_msg_hdr) +
959 				      sizeof(u32) + payload_size));
960 
961 		data_msg->mem_addr = mem_address;
962 		memcpy(data_msg->payload, payload, payload_size);
963 
964 		rc = smscore_sendrequest_and_wait(coredev, data_msg,
965 				data_msg->x_msg_header.msg_length,
966 				&coredev->data_download_done);
967 
968 		payload += payload_size;
969 		size -= payload_size;
970 		mem_address += payload_size;
971 	}
972 
973 	if (rc < 0)
974 		goto exit_fw_download;
975 
976 	pr_debug("sending MSG_SMS_DATA_VALIDITY_REQ expecting 0x%x\n",
977 		calc_checksum);
978 	SMS_INIT_MSG(&msg->x_msg_header, MSG_SMS_DATA_VALIDITY_REQ,
979 			sizeof(msg->x_msg_header) +
980 			sizeof(u32) * 3);
981 	msg->msg_data[0] = firmware->start_address;
982 		/* Entry point */
983 	msg->msg_data[1] = firmware->length;
984 	msg->msg_data[2] = 0; /* Regular checksum*/
985 	rc = smscore_sendrequest_and_wait(coredev, msg,
986 					  msg->x_msg_header.msg_length,
987 					  &coredev->data_validity_done);
988 	if (rc < 0)
989 		goto exit_fw_download;
990 
991 	if (coredev->mode == DEVICE_MODE_NONE) {
992 		struct sms_msg_data *trigger_msg =
993 			(struct sms_msg_data *) msg;
994 
995 		pr_debug("sending MSG_SMS_SWDOWNLOAD_TRIGGER_REQ\n");
996 		SMS_INIT_MSG(&msg->x_msg_header,
997 				MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
998 				sizeof(struct sms_msg_hdr) +
999 				sizeof(u32) * 5);
1000 
1001 		trigger_msg->msg_data[0] = firmware->start_address;
1002 					/* Entry point */
1003 		trigger_msg->msg_data[1] = 6; /* Priority */
1004 		trigger_msg->msg_data[2] = 0x200; /* Stack size */
1005 		trigger_msg->msg_data[3] = 0; /* Parameter */
1006 		trigger_msg->msg_data[4] = 4; /* Task ID */
1007 
1008 		rc = smscore_sendrequest_and_wait(coredev, trigger_msg,
1009 					trigger_msg->x_msg_header.msg_length,
1010 					&coredev->trigger_done);
1011 	} else {
1012 		SMS_INIT_MSG(&msg->x_msg_header, MSG_SW_RELOAD_EXEC_REQ,
1013 				sizeof(struct sms_msg_hdr));
1014 		rc = coredev->sendrequest_handler(coredev->context, msg,
1015 				msg->x_msg_header.msg_length);
1016 	}
1017 
1018 	if (rc < 0)
1019 		goto exit_fw_download;
1020 
1021 	/*
1022 	 * backward compatibility - wait to device_ready_done for
1023 	 * not more than 400 ms
1024 	 */
1025 	msleep(400);
1026 
1027 exit_fw_download:
1028 	kfree(msg);
1029 
1030 	if (coredev->postload_handler) {
1031 		pr_debug("rc=%d, postload=0x%p\n",
1032 			 rc, coredev->postload_handler);
1033 		if (rc >= 0)
1034 			return coredev->postload_handler(coredev->context);
1035 	}
1036 
1037 	pr_debug("rc=%d\n", rc);
1038 	return rc;
1039 }
1040 
1041 static char *smscore_fw_lkup[][DEVICE_MODE_MAX] = {
1042 	[SMS_NOVA_A0] = {
1043 		[DEVICE_MODE_DVBT]		= SMS_FW_DVB_NOVA_12MHZ,
1044 		[DEVICE_MODE_DVBH]		= SMS_FW_DVB_NOVA_12MHZ,
1045 		[DEVICE_MODE_DAB_TDMB]		= SMS_FW_TDMB_NOVA_12MHZ,
1046 		[DEVICE_MODE_DVBT_BDA]		= SMS_FW_DVB_NOVA_12MHZ,
1047 		[DEVICE_MODE_ISDBT]		= SMS_FW_ISDBT_NOVA_12MHZ,
1048 		[DEVICE_MODE_ISDBT_BDA]		= SMS_FW_ISDBT_NOVA_12MHZ,
1049 	},
1050 	[SMS_NOVA_B0] = {
1051 		[DEVICE_MODE_DVBT]		= SMS_FW_DVB_NOVA_12MHZ_B0,
1052 		[DEVICE_MODE_DVBH]		= SMS_FW_DVB_NOVA_12MHZ_B0,
1053 		[DEVICE_MODE_DAB_TDMB]		= SMS_FW_TDMB_NOVA_12MHZ_B0,
1054 		[DEVICE_MODE_DVBT_BDA]		= SMS_FW_DVB_NOVA_12MHZ_B0,
1055 		[DEVICE_MODE_ISDBT]		= SMS_FW_ISDBT_NOVA_12MHZ_B0,
1056 		[DEVICE_MODE_ISDBT_BDA]		= SMS_FW_ISDBT_NOVA_12MHZ_B0,
1057 		[DEVICE_MODE_FM_RADIO]		= SMS_FW_FM_RADIO,
1058 		[DEVICE_MODE_FM_RADIO_BDA]	= SMS_FW_FM_RADIO,
1059 	},
1060 	[SMS_VEGA] = {
1061 		[DEVICE_MODE_CMMB]		= SMS_FW_CMMB_VEGA_12MHZ,
1062 	},
1063 	[SMS_VENICE] = {
1064 		[DEVICE_MODE_CMMB]		= SMS_FW_CMMB_VENICE_12MHZ,
1065 	},
1066 	[SMS_MING] = {
1067 		[DEVICE_MODE_CMMB]		= SMS_FW_CMMB_MING_APP,
1068 	},
1069 	[SMS_PELE] = {
1070 		[DEVICE_MODE_ISDBT]		= SMS_FW_ISDBT_PELE,
1071 		[DEVICE_MODE_ISDBT_BDA]		= SMS_FW_ISDBT_PELE,
1072 	},
1073 	[SMS_RIO] = {
1074 		[DEVICE_MODE_DVBT]		= SMS_FW_DVB_RIO,
1075 		[DEVICE_MODE_DVBH]		= SMS_FW_DVBH_RIO,
1076 		[DEVICE_MODE_DVBT_BDA]		= SMS_FW_DVB_RIO,
1077 		[DEVICE_MODE_ISDBT]		= SMS_FW_ISDBT_RIO,
1078 		[DEVICE_MODE_ISDBT_BDA]		= SMS_FW_ISDBT_RIO,
1079 		[DEVICE_MODE_FM_RADIO]		= SMS_FW_FM_RADIO_RIO,
1080 		[DEVICE_MODE_FM_RADIO_BDA]	= SMS_FW_FM_RADIO_RIO,
1081 	},
1082 	[SMS_DENVER_1530] = {
1083 		[DEVICE_MODE_ATSC]		= SMS_FW_ATSC_DENVER,
1084 	},
1085 	[SMS_DENVER_2160] = {
1086 		[DEVICE_MODE_DAB_TDMB]		= SMS_FW_TDMB_DENVER,
1087 	},
1088 };
1089 
1090 /*
1091  * get firmware file name from one of the two mechanisms : sms_boards or
1092  * smscore_fw_lkup.
1093  * @param coredev pointer to a coredev object returned by
1094  *		  smscore_register_device
1095  * @param mode requested mode of operation
1096  * @param lookup if 1, always get the fw filename from smscore_fw_lkup
1097  *	 table. if 0, try first to get from sms_boards
1098  *
1099  * return: 0 on success, <0 on error.
1100  */
1101 static char *smscore_get_fw_filename(struct smscore_device_t *coredev,
1102 			      int mode)
1103 {
1104 	char **fw;
1105 	int board_id = smscore_get_board_id(coredev);
1106 	enum sms_device_type_st type;
1107 
1108 	type = smscore_registry_gettype(coredev->devpath);
1109 
1110 	/* Prevent looking outside the smscore_fw_lkup table */
1111 	if (type <= SMS_UNKNOWN_TYPE || type >= SMS_NUM_OF_DEVICE_TYPES)
1112 		return NULL;
1113 	if (mode <= DEVICE_MODE_NONE || mode >= DEVICE_MODE_MAX)
1114 		return NULL;
1115 
1116 	pr_debug("trying to get fw name from sms_boards board_id %d mode %d\n",
1117 		  board_id, mode);
1118 	fw = sms_get_board(board_id)->fw;
1119 	if (!fw || !fw[mode]) {
1120 		pr_debug("cannot find fw name in sms_boards, getting from lookup table mode %d type %d\n",
1121 			  mode, type);
1122 		return smscore_fw_lkup[type][mode];
1123 	}
1124 
1125 	return fw[mode];
1126 }
1127 
1128 /*
1129  * loads specified firmware into a buffer and calls device loadfirmware_handler
1130  *
1131  * @param coredev pointer to a coredev object returned by
1132  *                smscore_register_device
1133  * @param filename null-terminated string specifies firmware file name
1134  * @param loadfirmware_handler device handler that loads firmware
1135  *
1136  * return: 0 on success, <0 on error.
1137  */
1138 static int smscore_load_firmware_from_file(struct smscore_device_t *coredev,
1139 					   int mode,
1140 					   loadfirmware_t loadfirmware_handler)
1141 {
1142 	int rc = -ENOENT;
1143 	u8 *fw_buf;
1144 	u32 fw_buf_size;
1145 	const struct firmware *fw;
1146 
1147 	char *fw_filename = smscore_get_fw_filename(coredev, mode);
1148 	if (!fw_filename) {
1149 		pr_err("mode %d not supported on this device\n", mode);
1150 		return -ENOENT;
1151 	}
1152 	pr_debug("Firmware name: %s\n", fw_filename);
1153 
1154 	if (!loadfirmware_handler &&
1155 	    !(coredev->device_flags & SMS_DEVICE_FAMILY2))
1156 		return -EINVAL;
1157 
1158 	rc = request_firmware(&fw, fw_filename, coredev->device);
1159 	if (rc < 0) {
1160 		pr_err("failed to open firmware file '%s'\n", fw_filename);
1161 		return rc;
1162 	}
1163 	pr_debug("read fw %s, buffer size=0x%zx\n", fw_filename, fw->size);
1164 	fw_buf = kmalloc(ALIGN(fw->size + sizeof(struct sms_firmware),
1165 			 SMS_ALLOC_ALIGNMENT), GFP_KERNEL | coredev->gfp_buf_flags);
1166 	if (!fw_buf) {
1167 		pr_err("failed to allocate firmware buffer\n");
1168 		rc = -ENOMEM;
1169 	} else {
1170 		memcpy(fw_buf, fw->data, fw->size);
1171 		fw_buf_size = fw->size;
1172 
1173 		rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
1174 			smscore_load_firmware_family2(coredev, fw_buf, fw_buf_size)
1175 			: loadfirmware_handler(coredev->context, fw_buf,
1176 			fw_buf_size);
1177 	}
1178 
1179 	kfree(fw_buf);
1180 	release_firmware(fw);
1181 
1182 	return rc;
1183 }
1184 
1185 /*
1186  * notifies all clients registered with the device, notifies hotplugs,
1187  * frees all buffers and coredev object
1188  *
1189  * @param coredev pointer to a coredev object returned by
1190  *                smscore_register_device
1191  *
1192  * return: 0 on success, <0 on error.
1193  */
1194 void smscore_unregister_device(struct smscore_device_t *coredev)
1195 {
1196 	struct smscore_buffer_t *cb;
1197 	int num_buffers = 0;
1198 	int retry = 0;
1199 
1200 	mutex_lock(&g_smscore_deviceslock);
1201 
1202 	/* Release input device (IR) resources */
1203 	sms_ir_exit(coredev);
1204 
1205 	smscore_notify_clients(coredev);
1206 	smscore_notify_callbacks(coredev, NULL, 0);
1207 
1208 	/* at this point all buffers should be back
1209 	 * onresponse must no longer be called */
1210 
1211 	while (1) {
1212 		while (!list_empty(&coredev->buffers)) {
1213 			cb = (struct smscore_buffer_t *) coredev->buffers.next;
1214 			list_del(&cb->entry);
1215 			kfree(cb);
1216 			num_buffers++;
1217 		}
1218 		if (num_buffers == coredev->num_buffers)
1219 			break;
1220 		if (++retry > 10) {
1221 			pr_info("exiting although not all buffers released.\n");
1222 			break;
1223 		}
1224 
1225 		pr_debug("waiting for %d buffer(s)\n",
1226 			 coredev->num_buffers - num_buffers);
1227 		mutex_unlock(&g_smscore_deviceslock);
1228 		msleep(100);
1229 		mutex_lock(&g_smscore_deviceslock);
1230 	}
1231 
1232 	pr_debug("freed %d buffers\n", num_buffers);
1233 
1234 	if (coredev->common_buffer) {
1235 		if (coredev->usb_device)
1236 			kfree(coredev->common_buffer);
1237 		else
1238 			dma_free_coherent(coredev->device,
1239 					  coredev->common_buffer_size,
1240 					  coredev->common_buffer,
1241 					  coredev->common_buffer_phys);
1242 	}
1243 	kfree(coredev->fw_buf);
1244 
1245 	list_del(&coredev->entry);
1246 	kfree(coredev);
1247 
1248 	mutex_unlock(&g_smscore_deviceslock);
1249 
1250 	pr_debug("device %p destroyed\n", coredev);
1251 }
1252 EXPORT_SYMBOL_GPL(smscore_unregister_device);
1253 
1254 static int smscore_detect_mode(struct smscore_device_t *coredev)
1255 {
1256 	void *buffer = kmalloc(sizeof(struct sms_msg_hdr) + SMS_DMA_ALIGNMENT,
1257 			       GFP_KERNEL | coredev->gfp_buf_flags);
1258 	struct sms_msg_hdr *msg =
1259 		(struct sms_msg_hdr *) SMS_ALIGN_ADDRESS(buffer);
1260 	int rc;
1261 
1262 	if (!buffer)
1263 		return -ENOMEM;
1264 
1265 	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
1266 		     sizeof(struct sms_msg_hdr));
1267 
1268 	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msg_length,
1269 					  &coredev->version_ex_done);
1270 	if (rc == -ETIME) {
1271 		pr_err("MSG_SMS_GET_VERSION_EX_REQ failed first try\n");
1272 
1273 		if (wait_for_completion_timeout(&coredev->resume_done,
1274 						msecs_to_jiffies(5000))) {
1275 			rc = smscore_sendrequest_and_wait(
1276 				coredev, msg, msg->msg_length,
1277 				&coredev->version_ex_done);
1278 			if (rc < 0)
1279 				pr_err("MSG_SMS_GET_VERSION_EX_REQ failed second try, rc %d\n",
1280 					rc);
1281 		} else
1282 			rc = -ETIME;
1283 	}
1284 
1285 	kfree(buffer);
1286 
1287 	return rc;
1288 }
1289 
1290 /*
1291  * send init device request and wait for response
1292  *
1293  * @param coredev pointer to a coredev object returned by
1294  *                smscore_register_device
1295  * @param mode requested mode of operation
1296  *
1297  * return: 0 on success, <0 on error.
1298  */
1299 static int smscore_init_device(struct smscore_device_t *coredev, int mode)
1300 {
1301 	void *buffer;
1302 	struct sms_msg_data *msg;
1303 	int rc = 0;
1304 
1305 	buffer = kmalloc(sizeof(struct sms_msg_data) +
1306 			SMS_DMA_ALIGNMENT, GFP_KERNEL | coredev->gfp_buf_flags);
1307 	if (!buffer)
1308 		return -ENOMEM;
1309 
1310 	msg = (struct sms_msg_data *)SMS_ALIGN_ADDRESS(buffer);
1311 	SMS_INIT_MSG(&msg->x_msg_header, MSG_SMS_INIT_DEVICE_REQ,
1312 			sizeof(struct sms_msg_data));
1313 	msg->msg_data[0] = mode;
1314 
1315 	rc = smscore_sendrequest_and_wait(coredev, msg,
1316 			msg->x_msg_header. msg_length,
1317 			&coredev->init_device_done);
1318 
1319 	kfree(buffer);
1320 	return rc;
1321 }
1322 
1323 /*
1324  * calls device handler to change mode of operation
1325  * NOTE: stellar/usb may disconnect when changing mode
1326  *
1327  * @param coredev pointer to a coredev object returned by
1328  *                smscore_register_device
1329  * @param mode requested mode of operation
1330  *
1331  * return: 0 on success, <0 on error.
1332  */
1333 int smscore_set_device_mode(struct smscore_device_t *coredev, int mode)
1334 {
1335 	int rc = 0;
1336 
1337 	pr_debug("set device mode to %d\n", mode);
1338 	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
1339 		if (mode <= DEVICE_MODE_NONE || mode >= DEVICE_MODE_MAX) {
1340 			pr_err("invalid mode specified %d\n", mode);
1341 			return -EINVAL;
1342 		}
1343 
1344 		smscore_registry_setmode(coredev->devpath, mode);
1345 
1346 		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
1347 			rc = smscore_detect_mode(coredev);
1348 			if (rc < 0) {
1349 				pr_err("mode detect failed %d\n", rc);
1350 				return rc;
1351 			}
1352 		}
1353 
1354 		if (coredev->mode == mode) {
1355 			pr_debug("device mode %d already set\n", mode);
1356 			return 0;
1357 		}
1358 
1359 		if (!(coredev->modes_supported & (1 << mode))) {
1360 			rc = smscore_load_firmware_from_file(coredev,
1361 							     mode, NULL);
1362 			if (rc >= 0)
1363 				pr_debug("firmware download success\n");
1364 		} else {
1365 			pr_debug("mode %d is already supported by running firmware\n",
1366 				 mode);
1367 		}
1368 		if (coredev->fw_version >= 0x800) {
1369 			rc = smscore_init_device(coredev, mode);
1370 			if (rc < 0)
1371 				pr_err("device init failed, rc %d.\n", rc);
1372 		}
1373 	} else {
1374 		if (mode <= DEVICE_MODE_NONE || mode >= DEVICE_MODE_MAX) {
1375 			pr_err("invalid mode specified %d\n", mode);
1376 			return -EINVAL;
1377 		}
1378 
1379 		smscore_registry_setmode(coredev->devpath, mode);
1380 
1381 		if (coredev->detectmode_handler)
1382 			coredev->detectmode_handler(coredev->context,
1383 						    &coredev->mode);
1384 
1385 		if (coredev->mode != mode && coredev->setmode_handler)
1386 			rc = coredev->setmode_handler(coredev->context, mode);
1387 	}
1388 
1389 	if (rc >= 0) {
1390 		char *buffer;
1391 		coredev->mode = mode;
1392 		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
1393 
1394 		buffer = kmalloc(sizeof(struct sms_msg_data) +
1395 				 SMS_DMA_ALIGNMENT, GFP_KERNEL | coredev->gfp_buf_flags);
1396 		if (buffer) {
1397 			struct sms_msg_data *msg = (struct sms_msg_data *) SMS_ALIGN_ADDRESS(buffer);
1398 
1399 			SMS_INIT_MSG(&msg->x_msg_header, MSG_SMS_INIT_DEVICE_REQ,
1400 				     sizeof(struct sms_msg_data));
1401 			msg->msg_data[0] = mode;
1402 
1403 			rc = smscore_sendrequest_and_wait(
1404 				coredev, msg, msg->x_msg_header.msg_length,
1405 				&coredev->init_device_done);
1406 
1407 			kfree(buffer);
1408 		}
1409 	}
1410 
1411 	if (rc < 0)
1412 		pr_err("return error code %d.\n", rc);
1413 	else
1414 		pr_debug("Success setting device mode.\n");
1415 
1416 	return rc;
1417 }
1418 
1419 /*
1420  * calls device handler to get current mode of operation
1421  *
1422  * @param coredev pointer to a coredev object returned by
1423  *                smscore_register_device
1424  *
1425  * return: current mode
1426  */
1427 int smscore_get_device_mode(struct smscore_device_t *coredev)
1428 {
1429 	return coredev->mode;
1430 }
1431 EXPORT_SYMBOL_GPL(smscore_get_device_mode);
1432 
1433 /*
1434  * find client by response id & type within the clients list.
1435  * return client handle or NULL.
1436  *
1437  * @param coredev pointer to a coredev object returned by
1438  *                smscore_register_device
1439  * @param data_type client data type (SMS_DONT_CARE for all types)
1440  * @param id client id (SMS_DONT_CARE for all id)
1441  *
1442  */
1443 static struct
1444 smscore_client_t *smscore_find_client(struct smscore_device_t *coredev,
1445 				      int data_type, int id)
1446 {
1447 	struct list_head *first;
1448 	struct smscore_client_t *client;
1449 	unsigned long flags;
1450 	struct list_head *firstid;
1451 	struct smscore_idlist_t *client_id;
1452 
1453 	spin_lock_irqsave(&coredev->clientslock, flags);
1454 	first = &coredev->clients;
1455 	list_for_each_entry(client, first, entry) {
1456 		firstid = &client->idlist;
1457 		list_for_each_entry(client_id, firstid, entry) {
1458 			if ((client_id->id == id) &&
1459 			    (client_id->data_type == data_type ||
1460 			    (client_id->data_type == 0)))
1461 				goto found;
1462 		}
1463 	}
1464 	client = NULL;
1465 found:
1466 	spin_unlock_irqrestore(&coredev->clientslock, flags);
1467 	return client;
1468 }
1469 
1470 /*
1471  * find client by response id/type, call clients onresponse handler
1472  * return buffer to pool on error
1473  *
1474  * @param coredev pointer to a coredev object returned by
1475  *                smscore_register_device
1476  * @param cb pointer to response buffer descriptor
1477  *
1478  */
1479 void smscore_onresponse(struct smscore_device_t *coredev,
1480 		struct smscore_buffer_t *cb) {
1481 	struct sms_msg_hdr *phdr = (struct sms_msg_hdr *) ((u8 *) cb->p
1482 			+ cb->offset);
1483 	struct smscore_client_t *client;
1484 	int rc = -EBUSY;
1485 	static unsigned long last_sample_time; /* = 0; */
1486 	static int data_total; /* = 0; */
1487 	unsigned long time_now = jiffies_to_msecs(jiffies);
1488 
1489 	if (!last_sample_time)
1490 		last_sample_time = time_now;
1491 
1492 	if (time_now - last_sample_time > 10000) {
1493 		pr_debug("data rate %d bytes/secs\n",
1494 			  (int)((data_total * 1000) /
1495 				(time_now - last_sample_time)));
1496 
1497 		last_sample_time = time_now;
1498 		data_total = 0;
1499 	}
1500 
1501 	data_total += cb->size;
1502 	/* Do we need to re-route? */
1503 	if ((phdr->msg_type == MSG_SMS_HO_PER_SLICES_IND) ||
1504 			(phdr->msg_type == MSG_SMS_TRANSMISSION_IND)) {
1505 		if (coredev->mode == DEVICE_MODE_DVBT_BDA)
1506 			phdr->msg_dst_id = DVBT_BDA_CONTROL_MSG_ID;
1507 	}
1508 
1509 
1510 	client = smscore_find_client(coredev, phdr->msg_type, phdr->msg_dst_id);
1511 
1512 	/* If no client registered for type & id,
1513 	 * check for control client where type is not registered */
1514 	if (client)
1515 		rc = client->onresponse_handler(client->context, cb);
1516 
1517 	if (rc < 0) {
1518 		switch (phdr->msg_type) {
1519 		case MSG_SMS_ISDBT_TUNE_RES:
1520 			break;
1521 		case MSG_SMS_RF_TUNE_RES:
1522 			break;
1523 		case MSG_SMS_SIGNAL_DETECTED_IND:
1524 			break;
1525 		case MSG_SMS_NO_SIGNAL_IND:
1526 			break;
1527 		case MSG_SMS_SPI_INT_LINE_SET_RES:
1528 			break;
1529 		case MSG_SMS_INTERFACE_LOCK_IND:
1530 			break;
1531 		case MSG_SMS_INTERFACE_UNLOCK_IND:
1532 			break;
1533 		case MSG_SMS_GET_VERSION_EX_RES:
1534 		{
1535 			struct sms_version_res *ver =
1536 				(struct sms_version_res *) phdr;
1537 			pr_debug("Firmware id %d prots 0x%x ver %d.%d\n",
1538 				  ver->firmware_id, ver->supported_protocols,
1539 				  ver->rom_ver_major, ver->rom_ver_minor);
1540 
1541 			coredev->mode = ver->firmware_id == 255 ?
1542 				DEVICE_MODE_NONE : ver->firmware_id;
1543 			coredev->modes_supported = ver->supported_protocols;
1544 			coredev->fw_version = ver->rom_ver_major << 8 |
1545 					      ver->rom_ver_minor;
1546 
1547 			complete(&coredev->version_ex_done);
1548 			break;
1549 		}
1550 		case MSG_SMS_INIT_DEVICE_RES:
1551 			complete(&coredev->init_device_done);
1552 			break;
1553 		case MSG_SW_RELOAD_START_RES:
1554 			complete(&coredev->reload_start_done);
1555 			break;
1556 		case MSG_SMS_DATA_VALIDITY_RES:
1557 		{
1558 			struct sms_msg_data *validity = (struct sms_msg_data *) phdr;
1559 
1560 			pr_debug("MSG_SMS_DATA_VALIDITY_RES, checksum = 0x%x\n",
1561 				validity->msg_data[0]);
1562 			complete(&coredev->data_validity_done);
1563 			break;
1564 		}
1565 		case MSG_SMS_DATA_DOWNLOAD_RES:
1566 			complete(&coredev->data_download_done);
1567 			break;
1568 		case MSG_SW_RELOAD_EXEC_RES:
1569 			break;
1570 		case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
1571 			complete(&coredev->trigger_done);
1572 			break;
1573 		case MSG_SMS_SLEEP_RESUME_COMP_IND:
1574 			complete(&coredev->resume_done);
1575 			break;
1576 		case MSG_SMS_GPIO_CONFIG_EX_RES:
1577 			complete(&coredev->gpio_configuration_done);
1578 			break;
1579 		case MSG_SMS_GPIO_SET_LEVEL_RES:
1580 			complete(&coredev->gpio_set_level_done);
1581 			break;
1582 		case MSG_SMS_GPIO_GET_LEVEL_RES:
1583 		{
1584 			u32 *msgdata = (u32 *) phdr;
1585 			coredev->gpio_get_res = msgdata[1];
1586 			pr_debug("gpio level %d\n",
1587 					coredev->gpio_get_res);
1588 			complete(&coredev->gpio_get_level_done);
1589 			break;
1590 		}
1591 		case MSG_SMS_START_IR_RES:
1592 			complete(&coredev->ir_init_done);
1593 			break;
1594 		case MSG_SMS_IR_SAMPLES_IND:
1595 			sms_ir_event(coredev,
1596 				(const char *)
1597 				((char *)phdr
1598 				+ sizeof(struct sms_msg_hdr)),
1599 				(int)phdr->msg_length
1600 				- sizeof(struct sms_msg_hdr));
1601 			break;
1602 
1603 		case MSG_SMS_DVBT_BDA_DATA:
1604 			/*
1605 			 * It can be received here, if the frontend is
1606 			 * tuned into a valid channel and the proper firmware
1607 			 * is loaded. That happens when the module got removed
1608 			 * and re-inserted, without powering the device off
1609 			 */
1610 			break;
1611 
1612 		default:
1613 			pr_debug("message %s(%d) not handled.\n",
1614 				  smscore_translate_msg(phdr->msg_type),
1615 				  phdr->msg_type);
1616 			break;
1617 		}
1618 		smscore_putbuffer(coredev, cb);
1619 	}
1620 }
1621 EXPORT_SYMBOL_GPL(smscore_onresponse);
1622 
1623 /*
1624  * return pointer to next free buffer descriptor from core pool
1625  *
1626  * @param coredev pointer to a coredev object returned by
1627  *                smscore_register_device
1628  *
1629  * return: pointer to descriptor on success, NULL on error.
1630  */
1631 
1632 static struct smscore_buffer_t *get_entry(struct smscore_device_t *coredev)
1633 {
1634 	struct smscore_buffer_t *cb = NULL;
1635 	unsigned long flags;
1636 
1637 	spin_lock_irqsave(&coredev->bufferslock, flags);
1638 	if (!list_empty(&coredev->buffers)) {
1639 		cb = (struct smscore_buffer_t *) coredev->buffers.next;
1640 		list_del(&cb->entry);
1641 	}
1642 	spin_unlock_irqrestore(&coredev->bufferslock, flags);
1643 	return cb;
1644 }
1645 
1646 struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
1647 {
1648 	struct smscore_buffer_t *cb = NULL;
1649 
1650 	wait_event(coredev->buffer_mng_waitq, (cb = get_entry(coredev)));
1651 
1652 	return cb;
1653 }
1654 EXPORT_SYMBOL_GPL(smscore_getbuffer);
1655 
1656 /*
1657  * return buffer descriptor to a pool
1658  *
1659  * @param coredev pointer to a coredev object returned by
1660  *                smscore_register_device
1661  * @param cb pointer buffer descriptor
1662  *
1663  */
1664 void smscore_putbuffer(struct smscore_device_t *coredev,
1665 		struct smscore_buffer_t *cb) {
1666 	wake_up_interruptible(&coredev->buffer_mng_waitq);
1667 	list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
1668 }
1669 EXPORT_SYMBOL_GPL(smscore_putbuffer);
1670 
1671 static int smscore_validate_client(struct smscore_device_t *coredev,
1672 				   struct smscore_client_t *client,
1673 				   int data_type, int id)
1674 {
1675 	struct smscore_idlist_t *listentry;
1676 	struct smscore_client_t *registered_client;
1677 
1678 	if (!client) {
1679 		pr_err("bad parameter.\n");
1680 		return -EINVAL;
1681 	}
1682 	registered_client = smscore_find_client(coredev, data_type, id);
1683 	if (registered_client == client)
1684 		return 0;
1685 
1686 	if (registered_client) {
1687 		pr_err("The msg ID already registered to another client.\n");
1688 		return -EEXIST;
1689 	}
1690 	listentry = kzalloc(sizeof(*listentry), GFP_KERNEL);
1691 	if (!listentry)
1692 		return -ENOMEM;
1693 
1694 	listentry->id = id;
1695 	listentry->data_type = data_type;
1696 	list_add_locked(&listentry->entry, &client->idlist,
1697 			&coredev->clientslock);
1698 	return 0;
1699 }
1700 
1701 /*
1702  * creates smsclient object, check that id is taken by another client
1703  *
1704  * @param coredev pointer to a coredev object from clients hotplug
1705  * @param initial_id all messages with this id would be sent to this client
1706  * @param data_type all messages of this type would be sent to this client
1707  * @param onresponse_handler client handler that is called to
1708  *                           process incoming messages
1709  * @param onremove_handler client handler that is called when device is removed
1710  * @param context client-specific context
1711  * @param client pointer to a value that receives created smsclient object
1712  *
1713  * return: 0 on success, <0 on error.
1714  */
1715 int smscore_register_client(struct smscore_device_t *coredev,
1716 			    struct smsclient_params_t *params,
1717 			    struct smscore_client_t **client)
1718 {
1719 	struct smscore_client_t *newclient;
1720 	/* check that no other channel with same parameters exists */
1721 	if (smscore_find_client(coredev, params->data_type,
1722 				params->initial_id)) {
1723 		pr_err("Client already exist.\n");
1724 		return -EEXIST;
1725 	}
1726 
1727 	newclient = kzalloc(sizeof(*newclient), GFP_KERNEL);
1728 	if (!newclient)
1729 		return -ENOMEM;
1730 
1731 	INIT_LIST_HEAD(&newclient->idlist);
1732 	newclient->coredev = coredev;
1733 	newclient->onresponse_handler = params->onresponse_handler;
1734 	newclient->onremove_handler = params->onremove_handler;
1735 	newclient->context = params->context;
1736 	list_add_locked(&newclient->entry, &coredev->clients,
1737 			&coredev->clientslock);
1738 	smscore_validate_client(coredev, newclient, params->data_type,
1739 				params->initial_id);
1740 	*client = newclient;
1741 	pr_debug("%p %d %d\n", params->context, params->data_type,
1742 		  params->initial_id);
1743 
1744 	return 0;
1745 }
1746 EXPORT_SYMBOL_GPL(smscore_register_client);
1747 
1748 /*
1749  * frees smsclient object and all subclients associated with it
1750  *
1751  * @param client pointer to smsclient object returned by
1752  *               smscore_register_client
1753  *
1754  */
1755 void smscore_unregister_client(struct smscore_client_t *client)
1756 {
1757 	struct smscore_device_t *coredev = client->coredev;
1758 	unsigned long flags;
1759 
1760 	spin_lock_irqsave(&coredev->clientslock, flags);
1761 
1762 
1763 	while (!list_empty(&client->idlist)) {
1764 		struct smscore_idlist_t *identry =
1765 			(struct smscore_idlist_t *) client->idlist.next;
1766 		list_del(&identry->entry);
1767 		kfree(identry);
1768 	}
1769 
1770 	pr_debug("%p\n", client->context);
1771 
1772 	list_del(&client->entry);
1773 	kfree(client);
1774 
1775 	spin_unlock_irqrestore(&coredev->clientslock, flags);
1776 }
1777 EXPORT_SYMBOL_GPL(smscore_unregister_client);
1778 
1779 /*
1780  * verifies that source id is not taken by another client,
1781  * calls device handler to send requests to the device
1782  *
1783  * @param client pointer to smsclient object returned by
1784  *               smscore_register_client
1785  * @param buffer pointer to a request buffer
1786  * @param size size (in bytes) of request buffer
1787  *
1788  * return: 0 on success, <0 on error.
1789  */
1790 int smsclient_sendrequest(struct smscore_client_t *client,
1791 			  void *buffer, size_t size)
1792 {
1793 	struct smscore_device_t *coredev;
1794 	struct sms_msg_hdr *phdr = (struct sms_msg_hdr *) buffer;
1795 	int rc;
1796 
1797 	if (!client) {
1798 		pr_err("Got NULL client\n");
1799 		return -EINVAL;
1800 	}
1801 
1802 	coredev = client->coredev;
1803 
1804 	/* check that no other channel with same id exists */
1805 	if (!coredev) {
1806 		pr_err("Got NULL coredev\n");
1807 		return -EINVAL;
1808 	}
1809 
1810 	rc = smscore_validate_client(client->coredev, client, 0,
1811 				     phdr->msg_src_id);
1812 	if (rc < 0)
1813 		return rc;
1814 
1815 	return coredev->sendrequest_handler(coredev->context, buffer, size);
1816 }
1817 EXPORT_SYMBOL_GPL(smsclient_sendrequest);
1818 
1819 
1820 /* old GPIO managements implementation */
1821 int smscore_configure_gpio(struct smscore_device_t *coredev, u32 pin,
1822 			   struct smscore_config_gpio *pinconfig)
1823 {
1824 	struct {
1825 		struct sms_msg_hdr hdr;
1826 		u32 data[6];
1827 	} msg;
1828 
1829 	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
1830 		msg.hdr.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
1831 		msg.hdr.msg_dst_id = HIF_TASK;
1832 		msg.hdr.msg_flags = 0;
1833 		msg.hdr.msg_type  = MSG_SMS_GPIO_CONFIG_EX_REQ;
1834 		msg.hdr.msg_length = sizeof(msg);
1835 
1836 		msg.data[0] = pin;
1837 		msg.data[1] = pinconfig->pullupdown;
1838 
1839 		/* Convert slew rate for Nova: Fast(0) = 3 / Slow(1) = 0; */
1840 		msg.data[2] = pinconfig->outputslewrate == 0 ? 3 : 0;
1841 
1842 		switch (pinconfig->outputdriving) {
1843 		case SMS_GPIO_OUTPUTDRIVING_S_16mA:
1844 			msg.data[3] = 7; /* Nova - 16mA */
1845 			break;
1846 		case SMS_GPIO_OUTPUTDRIVING_S_12mA:
1847 			msg.data[3] = 5; /* Nova - 11mA */
1848 			break;
1849 		case SMS_GPIO_OUTPUTDRIVING_S_8mA:
1850 			msg.data[3] = 3; /* Nova - 7mA */
1851 			break;
1852 		case SMS_GPIO_OUTPUTDRIVING_S_4mA:
1853 		default:
1854 			msg.data[3] = 2; /* Nova - 4mA */
1855 			break;
1856 		}
1857 
1858 		msg.data[4] = pinconfig->direction;
1859 		msg.data[5] = 0;
1860 	} else /* TODO: SMS_DEVICE_FAMILY1 */
1861 		return -EINVAL;
1862 
1863 	return coredev->sendrequest_handler(coredev->context,
1864 					    &msg, sizeof(msg));
1865 }
1866 
1867 int smscore_set_gpio(struct smscore_device_t *coredev, u32 pin, int level)
1868 {
1869 	struct {
1870 		struct sms_msg_hdr hdr;
1871 		u32 data[3];
1872 	} msg;
1873 
1874 	if (pin > MAX_GPIO_PIN_NUMBER)
1875 		return -EINVAL;
1876 
1877 	msg.hdr.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
1878 	msg.hdr.msg_dst_id = HIF_TASK;
1879 	msg.hdr.msg_flags = 0;
1880 	msg.hdr.msg_type  = MSG_SMS_GPIO_SET_LEVEL_REQ;
1881 	msg.hdr.msg_length = sizeof(msg);
1882 
1883 	msg.data[0] = pin;
1884 	msg.data[1] = level ? 1 : 0;
1885 	msg.data[2] = 0;
1886 
1887 	return coredev->sendrequest_handler(coredev->context,
1888 					    &msg, sizeof(msg));
1889 }
1890 
1891 /* new GPIO management implementation */
1892 static int get_gpio_pin_params(u32 pin_num, u32 *p_translatedpin_num,
1893 		u32 *p_group_num, u32 *p_group_cfg) {
1894 
1895 	*p_group_cfg = 1;
1896 
1897 	if (pin_num <= 1)	{
1898 		*p_translatedpin_num = 0;
1899 		*p_group_num = 9;
1900 		*p_group_cfg = 2;
1901 	} else if (pin_num >= 2 && pin_num <= 6) {
1902 		*p_translatedpin_num = 2;
1903 		*p_group_num = 0;
1904 		*p_group_cfg = 2;
1905 	} else if (pin_num >= 7 && pin_num <= 11) {
1906 		*p_translatedpin_num = 7;
1907 		*p_group_num = 1;
1908 	} else if (pin_num >= 12 && pin_num <= 15) {
1909 		*p_translatedpin_num = 12;
1910 		*p_group_num = 2;
1911 		*p_group_cfg = 3;
1912 	} else if (pin_num == 16) {
1913 		*p_translatedpin_num = 16;
1914 		*p_group_num = 23;
1915 	} else if (pin_num >= 17 && pin_num <= 24) {
1916 		*p_translatedpin_num = 17;
1917 		*p_group_num = 3;
1918 	} else if (pin_num == 25) {
1919 		*p_translatedpin_num = 25;
1920 		*p_group_num = 6;
1921 	} else if (pin_num >= 26 && pin_num <= 28) {
1922 		*p_translatedpin_num = 26;
1923 		*p_group_num = 4;
1924 	} else if (pin_num == 29) {
1925 		*p_translatedpin_num = 29;
1926 		*p_group_num = 5;
1927 		*p_group_cfg = 2;
1928 	} else if (pin_num == 30) {
1929 		*p_translatedpin_num = 30;
1930 		*p_group_num = 8;
1931 	} else if (pin_num == 31) {
1932 		*p_translatedpin_num = 31;
1933 		*p_group_num = 17;
1934 	} else
1935 		return -1;
1936 
1937 	*p_group_cfg <<= 24;
1938 
1939 	return 0;
1940 }
1941 
1942 int smscore_gpio_configure(struct smscore_device_t *coredev, u8 pin_num,
1943 		struct smscore_config_gpio *p_gpio_config) {
1944 
1945 	u32 total_len;
1946 	u32 translatedpin_num = 0;
1947 	u32 group_num = 0;
1948 	u32 electric_char;
1949 	u32 group_cfg;
1950 	void *buffer;
1951 	int rc;
1952 
1953 	struct set_gpio_msg {
1954 		struct sms_msg_hdr x_msg_header;
1955 		u32 msg_data[6];
1956 	} *p_msg;
1957 
1958 
1959 	if (pin_num > MAX_GPIO_PIN_NUMBER)
1960 		return -EINVAL;
1961 
1962 	if (!p_gpio_config)
1963 		return -EINVAL;
1964 
1965 	total_len = sizeof(struct sms_msg_hdr) + (sizeof(u32) * 6);
1966 
1967 	buffer = kmalloc(total_len + SMS_DMA_ALIGNMENT,
1968 			GFP_KERNEL | coredev->gfp_buf_flags);
1969 	if (!buffer)
1970 		return -ENOMEM;
1971 
1972 	p_msg = (struct set_gpio_msg *) SMS_ALIGN_ADDRESS(buffer);
1973 
1974 	p_msg->x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
1975 	p_msg->x_msg_header.msg_dst_id = HIF_TASK;
1976 	p_msg->x_msg_header.msg_flags = 0;
1977 	p_msg->x_msg_header.msg_length = (u16) total_len;
1978 	p_msg->msg_data[0] = pin_num;
1979 
1980 	if (!(coredev->device_flags & SMS_DEVICE_FAMILY2)) {
1981 		p_msg->x_msg_header.msg_type = MSG_SMS_GPIO_CONFIG_REQ;
1982 		if (get_gpio_pin_params(pin_num, &translatedpin_num, &group_num,
1983 				&group_cfg) != 0) {
1984 			rc = -EINVAL;
1985 			goto free;
1986 		}
1987 
1988 		p_msg->msg_data[1] = translatedpin_num;
1989 		p_msg->msg_data[2] = group_num;
1990 		electric_char = (p_gpio_config->pullupdown)
1991 				| (p_gpio_config->inputcharacteristics << 2)
1992 				| (p_gpio_config->outputslewrate << 3)
1993 				| (p_gpio_config->outputdriving << 4);
1994 		p_msg->msg_data[3] = electric_char;
1995 		p_msg->msg_data[4] = p_gpio_config->direction;
1996 		p_msg->msg_data[5] = group_cfg;
1997 	} else {
1998 		p_msg->x_msg_header.msg_type = MSG_SMS_GPIO_CONFIG_EX_REQ;
1999 		p_msg->msg_data[1] = p_gpio_config->pullupdown;
2000 		p_msg->msg_data[2] = p_gpio_config->outputslewrate;
2001 		p_msg->msg_data[3] = p_gpio_config->outputdriving;
2002 		p_msg->msg_data[4] = p_gpio_config->direction;
2003 		p_msg->msg_data[5] = 0;
2004 	}
2005 
2006 	rc = smscore_sendrequest_and_wait(coredev, p_msg, total_len,
2007 			&coredev->gpio_configuration_done);
2008 
2009 	if (rc != 0) {
2010 		if (rc == -ETIME)
2011 			pr_err("smscore_gpio_configure timeout\n");
2012 		else
2013 			pr_err("smscore_gpio_configure error\n");
2014 	}
2015 free:
2016 	kfree(buffer);
2017 
2018 	return rc;
2019 }
2020 
2021 int smscore_gpio_set_level(struct smscore_device_t *coredev, u8 pin_num,
2022 		u8 new_level) {
2023 
2024 	u32 total_len;
2025 	int rc;
2026 	void *buffer;
2027 
2028 	struct set_gpio_msg {
2029 		struct sms_msg_hdr x_msg_header;
2030 		u32 msg_data[3]; /* keep it 3 ! */
2031 	} *p_msg;
2032 
2033 	if ((new_level > 1) || (pin_num > MAX_GPIO_PIN_NUMBER))
2034 		return -EINVAL;
2035 
2036 	total_len = sizeof(struct sms_msg_hdr) +
2037 			(3 * sizeof(u32)); /* keep it 3 ! */
2038 
2039 	buffer = kmalloc(total_len + SMS_DMA_ALIGNMENT,
2040 			GFP_KERNEL | coredev->gfp_buf_flags);
2041 	if (!buffer)
2042 		return -ENOMEM;
2043 
2044 	p_msg = (struct set_gpio_msg *) SMS_ALIGN_ADDRESS(buffer);
2045 
2046 	p_msg->x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
2047 	p_msg->x_msg_header.msg_dst_id = HIF_TASK;
2048 	p_msg->x_msg_header.msg_flags = 0;
2049 	p_msg->x_msg_header.msg_type = MSG_SMS_GPIO_SET_LEVEL_REQ;
2050 	p_msg->x_msg_header.msg_length = (u16) total_len;
2051 	p_msg->msg_data[0] = pin_num;
2052 	p_msg->msg_data[1] = new_level;
2053 
2054 	/* Send message to SMS */
2055 	rc = smscore_sendrequest_and_wait(coredev, p_msg, total_len,
2056 			&coredev->gpio_set_level_done);
2057 
2058 	if (rc != 0) {
2059 		if (rc == -ETIME)
2060 			pr_err("smscore_gpio_set_level timeout\n");
2061 		else
2062 			pr_err("smscore_gpio_set_level error\n");
2063 	}
2064 	kfree(buffer);
2065 
2066 	return rc;
2067 }
2068 
2069 int smscore_gpio_get_level(struct smscore_device_t *coredev, u8 pin_num,
2070 		u8 *level) {
2071 
2072 	u32 total_len;
2073 	int rc;
2074 	void *buffer;
2075 
2076 	struct set_gpio_msg {
2077 		struct sms_msg_hdr x_msg_header;
2078 		u32 msg_data[2];
2079 	} *p_msg;
2080 
2081 
2082 	if (pin_num > MAX_GPIO_PIN_NUMBER)
2083 		return -EINVAL;
2084 
2085 	total_len = sizeof(struct sms_msg_hdr) + (2 * sizeof(u32));
2086 
2087 	buffer = kmalloc(total_len + SMS_DMA_ALIGNMENT,
2088 			GFP_KERNEL | coredev->gfp_buf_flags);
2089 	if (!buffer)
2090 		return -ENOMEM;
2091 
2092 	p_msg = (struct set_gpio_msg *) SMS_ALIGN_ADDRESS(buffer);
2093 
2094 	p_msg->x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
2095 	p_msg->x_msg_header.msg_dst_id = HIF_TASK;
2096 	p_msg->x_msg_header.msg_flags = 0;
2097 	p_msg->x_msg_header.msg_type = MSG_SMS_GPIO_GET_LEVEL_REQ;
2098 	p_msg->x_msg_header.msg_length = (u16) total_len;
2099 	p_msg->msg_data[0] = pin_num;
2100 	p_msg->msg_data[1] = 0;
2101 
2102 	/* Send message to SMS */
2103 	rc = smscore_sendrequest_and_wait(coredev, p_msg, total_len,
2104 			&coredev->gpio_get_level_done);
2105 
2106 	if (rc != 0) {
2107 		if (rc == -ETIME)
2108 			pr_err("smscore_gpio_get_level timeout\n");
2109 		else
2110 			pr_err("smscore_gpio_get_level error\n");
2111 	}
2112 	kfree(buffer);
2113 
2114 	/* Its a race between other gpio_get_level() and the copy of the single
2115 	 * global 'coredev->gpio_get_res' to  the function's variable 'level'
2116 	 */
2117 	*level = coredev->gpio_get_res;
2118 
2119 	return rc;
2120 }
2121 
2122 static int __init smscore_module_init(void)
2123 {
2124 	INIT_LIST_HEAD(&g_smscore_notifyees);
2125 	INIT_LIST_HEAD(&g_smscore_devices);
2126 	mutex_init(&g_smscore_deviceslock);
2127 
2128 	INIT_LIST_HEAD(&g_smscore_registry);
2129 	mutex_init(&g_smscore_registrylock);
2130 
2131 	return 0;
2132 }
2133 
2134 static void __exit smscore_module_exit(void)
2135 {
2136 	mutex_lock(&g_smscore_deviceslock);
2137 	while (!list_empty(&g_smscore_notifyees)) {
2138 		struct smscore_device_notifyee_t *notifyee =
2139 			(struct smscore_device_notifyee_t *)
2140 				g_smscore_notifyees.next;
2141 
2142 		list_del(&notifyee->entry);
2143 		kfree(notifyee);
2144 	}
2145 	mutex_unlock(&g_smscore_deviceslock);
2146 
2147 	mutex_lock(&g_smscore_registrylock);
2148 	while (!list_empty(&g_smscore_registry)) {
2149 		struct smscore_registry_entry_t *entry =
2150 			(struct smscore_registry_entry_t *)
2151 				g_smscore_registry.next;
2152 
2153 		list_del(&entry->entry);
2154 		kfree(entry);
2155 	}
2156 	mutex_unlock(&g_smscore_registrylock);
2157 
2158 	pr_debug("\n");
2159 }
2160 
2161 module_init(smscore_module_init);
2162 module_exit(smscore_module_exit);
2163 
2164 MODULE_DESCRIPTION("Siano MDTV Core module");
2165 MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
2166 MODULE_LICENSE("GPL");
2167 
2168 /* This should match what's defined at smscoreapi.h */
2169 MODULE_FIRMWARE(SMS_FW_ATSC_DENVER);
2170 MODULE_FIRMWARE(SMS_FW_CMMB_MING_APP);
2171 MODULE_FIRMWARE(SMS_FW_CMMB_VEGA_12MHZ);
2172 MODULE_FIRMWARE(SMS_FW_CMMB_VENICE_12MHZ);
2173 MODULE_FIRMWARE(SMS_FW_DVBH_RIO);
2174 MODULE_FIRMWARE(SMS_FW_DVB_NOVA_12MHZ_B0);
2175 MODULE_FIRMWARE(SMS_FW_DVB_NOVA_12MHZ);
2176 MODULE_FIRMWARE(SMS_FW_DVB_RIO);
2177 MODULE_FIRMWARE(SMS_FW_FM_RADIO);
2178 MODULE_FIRMWARE(SMS_FW_FM_RADIO_RIO);
2179 MODULE_FIRMWARE(SMS_FW_DVBT_HCW_55XXX);
2180 MODULE_FIRMWARE(SMS_FW_ISDBT_HCW_55XXX);
2181 MODULE_FIRMWARE(SMS_FW_ISDBT_NOVA_12MHZ_B0);
2182 MODULE_FIRMWARE(SMS_FW_ISDBT_NOVA_12MHZ);
2183 MODULE_FIRMWARE(SMS_FW_ISDBT_PELE);
2184 MODULE_FIRMWARE(SMS_FW_ISDBT_RIO);
2185 MODULE_FIRMWARE(SMS_FW_DVBT_NOVA_A);
2186 MODULE_FIRMWARE(SMS_FW_DVBT_NOVA_B);
2187 MODULE_FIRMWARE(SMS_FW_DVBT_STELLAR);
2188 MODULE_FIRMWARE(SMS_FW_TDMB_DENVER);
2189 MODULE_FIRMWARE(SMS_FW_TDMB_NOVA_12MHZ_B0);
2190 MODULE_FIRMWARE(SMS_FW_TDMB_NOVA_12MHZ);
2191