blob: 85151efdd94c75d96db013151ba6b6d2c0d04841 [file] [log] [blame]
Uri Shkolnike5275792009-04-27 09:09:47 -03001/****************************************************************
2
3Siano Mobile Silicon, Inc.
4MDTV receiver kernel modules.
5Copyright (C) 2006-2008, Uri Shkolnik
6
7This program is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20****************************************************************/
Steven Toth8d4f9d02008-05-22 18:05:26 -030021
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030022#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030024#include <linux/init.h>
Mauro Carvalho Chehabf35d09d2013-03-22 06:22:45 -030025#include <asm/div64.h>
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030026
Uri Shkolnikc9679e32009-05-14 16:28:17 -030027#include "dmxdev.h"
28#include "dvbdev.h"
29#include "dvb_demux.h"
30#include "dvb_frontend.h"
31
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030032#include "smscoreapi.h"
Michael Krufky1c11d542008-06-18 22:09:55 -030033#include "sms-cards.h"
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030034
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -030035#include "smsdvb.h"
36
Michael Krufky9c59f9682008-05-19 18:57:12 -030037DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
38
Adrian Bunkc5e0bd12008-07-21 23:17:36 -030039static struct list_head g_smsdvb_clients;
40static struct mutex g_smsdvb_clientslock;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -030041
Michael Krufky0d02efe2009-02-27 02:42:16 -030042static int sms_dbg;
43module_param_named(debug, sms_dbg, int, 0644);
44MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
45
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -030046
Hans Verkuil16688442013-10-04 11:01:52 -030047static u32 sms_to_guard_interval_table[] = {
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -030048 [0] = GUARD_INTERVAL_1_32,
49 [1] = GUARD_INTERVAL_1_16,
50 [2] = GUARD_INTERVAL_1_8,
51 [3] = GUARD_INTERVAL_1_4,
52};
53
Hans Verkuil16688442013-10-04 11:01:52 -030054static u32 sms_to_code_rate_table[] = {
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -030055 [0] = FEC_1_2,
56 [1] = FEC_2_3,
57 [2] = FEC_3_4,
58 [3] = FEC_5_6,
59 [4] = FEC_7_8,
60};
61
62
Hans Verkuil16688442013-10-04 11:01:52 -030063static u32 sms_to_hierarchy_table[] = {
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -030064 [0] = HIERARCHY_NONE,
65 [1] = HIERARCHY_1,
66 [2] = HIERARCHY_2,
67 [3] = HIERARCHY_4,
68};
69
Hans Verkuil16688442013-10-04 11:01:52 -030070static u32 sms_to_modulation_table[] = {
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -030071 [0] = QPSK,
72 [1] = QAM_16,
73 [2] = QAM_64,
74 [3] = DQPSK,
75};
76
Mauro Carvalho Chehabff702eb2013-03-09 15:54:46 -030077
Uri Shkolnik793786d2009-05-12 12:28:46 -030078/* Events that may come from DVB v3 adapter */
79static void sms_board_dvb3_event(struct smsdvb_client_t *client,
80 enum SMS_DVB3_EVENTS event) {
Uri Shkolnik4db989f2009-05-19 12:28:02 -030081
82 struct smscore_device_t *coredev = client->coredev;
83 switch (event) {
84 case DVB3_EVENT_INIT:
85 sms_debug("DVB3_EVENT_INIT");
86 sms_board_event(coredev, BOARD_EVENT_BIND);
87 break;
88 case DVB3_EVENT_SLEEP:
89 sms_debug("DVB3_EVENT_SLEEP");
90 sms_board_event(coredev, BOARD_EVENT_POWER_SUSPEND);
91 break;
92 case DVB3_EVENT_HOTPLUG:
93 sms_debug("DVB3_EVENT_HOTPLUG");
94 sms_board_event(coredev, BOARD_EVENT_POWER_INIT);
95 break;
96 case DVB3_EVENT_FE_LOCK:
97 if (client->event_fe_state != DVB3_EVENT_FE_LOCK) {
98 client->event_fe_state = DVB3_EVENT_FE_LOCK;
99 sms_debug("DVB3_EVENT_FE_LOCK");
100 sms_board_event(coredev, BOARD_EVENT_FE_LOCK);
101 }
102 break;
103 case DVB3_EVENT_FE_UNLOCK:
104 if (client->event_fe_state != DVB3_EVENT_FE_UNLOCK) {
105 client->event_fe_state = DVB3_EVENT_FE_UNLOCK;
106 sms_debug("DVB3_EVENT_FE_UNLOCK");
107 sms_board_event(coredev, BOARD_EVENT_FE_UNLOCK);
108 }
109 break;
110 case DVB3_EVENT_UNC_OK:
111 if (client->event_unc_state != DVB3_EVENT_UNC_OK) {
112 client->event_unc_state = DVB3_EVENT_UNC_OK;
113 sms_debug("DVB3_EVENT_UNC_OK");
114 sms_board_event(coredev, BOARD_EVENT_MULTIPLEX_OK);
115 }
116 break;
117 case DVB3_EVENT_UNC_ERR:
118 if (client->event_unc_state != DVB3_EVENT_UNC_ERR) {
119 client->event_unc_state = DVB3_EVENT_UNC_ERR;
120 sms_debug("DVB3_EVENT_UNC_ERR");
121 sms_board_event(coredev, BOARD_EVENT_MULTIPLEX_ERRORS);
122 }
123 break;
124
125 default:
126 sms_err("Unknown dvb3 api event");
127 break;
128 }
Uri Shkolnik793786d2009-05-12 12:28:46 -0300129}
130
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300131static void smsdvb_stats_not_ready(struct dvb_frontend *fe)
Mauro Carvalho Chehab5eb23972009-12-25 11:29:42 -0300132{
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300133 struct smsdvb_client_t *client =
134 container_of(fe, struct smsdvb_client_t, frontend);
135 struct smscore_device_t *coredev = client->coredev;
136 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
137 int i, n_layers;
Mauro Carvalho Chehab5eb23972009-12-25 11:29:42 -0300138
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300139 switch (smscore_get_device_mode(coredev)) {
140 case DEVICE_MODE_ISDBT:
141 case DEVICE_MODE_ISDBT_BDA:
142 n_layers = 4;
Jakob Normark250a51c2013-05-19 09:30:23 -0300143 break;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300144 default:
145 n_layers = 1;
146 }
Mauro Carvalho Chehab5eb23972009-12-25 11:29:42 -0300147
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300148 /* Global stats */
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300149 c->strength.len = 1;
150 c->cnr.len = 1;
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300151 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
152 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
Mauro Carvalho Chehab5eb23972009-12-25 11:29:42 -0300153
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300154 /* Per-layer stats */
155 c->post_bit_error.len = n_layers;
156 c->post_bit_count.len = n_layers;
157 c->block_error.len = n_layers;
158 c->block_count.len = n_layers;
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300159
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300160 /*
161 * Put all of them at FE_SCALE_NOT_AVAILABLE. They're dynamically
162 * changed when the stats become available.
163 */
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300164 for (i = 0; i < n_layers; i++) {
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300165 c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
166 c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
167 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
168 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300169 }
170}
171
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300172static inline int sms_to_mode(u32 mode)
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300173{
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300174 switch (mode) {
175 case 2:
176 return TRANSMISSION_MODE_2K;
177 case 4:
178 return TRANSMISSION_MODE_4K;
179 case 8:
180 return TRANSMISSION_MODE_8K;
181 }
182 return TRANSMISSION_MODE_AUTO;
183}
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300184
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300185static inline int sms_to_status(u32 is_demod_locked, u32 is_rf_locked)
186{
187 if (is_demod_locked)
188 return FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
189 FE_HAS_SYNC | FE_HAS_LOCK;
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300190
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300191 if (is_rf_locked)
192 return FE_HAS_SIGNAL | FE_HAS_CARRIER;
193
194 return 0;
195}
196
Mauro Carvalho Chehab5c3b8742013-03-10 19:21:13 -0300197static inline u32 sms_to_bw(u32 value)
198{
199 return value * 1000000;
200}
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300201
202#define convert_from_table(value, table, defval) ({ \
203 u32 __ret; \
204 if (value < ARRAY_SIZE(table)) \
205 __ret = table[value]; \
206 else \
207 __ret = defval; \
208 __ret; \
209})
210
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300211#define sms_to_guard_interval(value) \
212 convert_from_table(value, sms_to_guard_interval_table, \
213 GUARD_INTERVAL_AUTO);
214
215#define sms_to_code_rate(value) \
216 convert_from_table(value, sms_to_code_rate_table, \
217 FEC_NONE);
218
219#define sms_to_hierarchy(value) \
220 convert_from_table(value, sms_to_hierarchy_table, \
221 FEC_NONE);
222
223#define sms_to_modulation(value) \
224 convert_from_table(value, sms_to_modulation_table, \
225 FEC_NONE);
226
227static void smsdvb_update_tx_params(struct smsdvb_client_t *client,
Mauro Carvalho Chehabcf0e9cf2013-03-21 09:06:03 -0300228 struct sms_tx_stats *p)
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300229{
230 struct dvb_frontend *fe = &client->frontend;
231 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
232
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300233 c->frequency = p->frequency;
234 client->fe_status = sms_to_status(p->is_demod_locked, 0);
235 c->bandwidth_hz = sms_to_bw(p->bandwidth);
236 c->transmission_mode = sms_to_mode(p->transmission_mode);
237 c->guard_interval = sms_to_guard_interval(p->guard_interval);
238 c->code_rate_HP = sms_to_code_rate(p->code_rate);
239 c->code_rate_LP = sms_to_code_rate(p->lp_code_rate);
240 c->hierarchy = sms_to_hierarchy(p->hierarchy);
241 c->modulation = sms_to_modulation(p->constellation);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300242}
243
244static void smsdvb_update_per_slices(struct smsdvb_client_t *client,
245 struct RECEPTION_STATISTICS_PER_SLICES_S *p)
246{
247 struct dvb_frontend *fe = &client->frontend;
248 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabf35d09d2013-03-22 06:22:45 -0300249 u64 tmp;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300250
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300251 client->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300252 c->modulation = sms_to_modulation(p->constellation);
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300253
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300254 /* signal Strength, in DBm */
255 c->strength.stat[0].uvalue = p->in_band_power * 1000;
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300256
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300257 /* Carrier to noise ratio, in DB */
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300258 c->cnr.stat[0].svalue = p->snr * 1000;
259
260 /* PER/BER requires demod lock */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300261 if (!p->is_demod_locked)
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300262 return;
263
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300264 /* TS PER */
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300265 client->last_per = c->block_error.stat[0].uvalue;
266 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
267 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300268 c->block_error.stat[0].uvalue += p->ets_packets;
269 c->block_count.stat[0].uvalue += p->ets_packets + p->ts_packets;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300270
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300271 /* ber */
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300272 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
273 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300274 c->post_bit_error.stat[0].uvalue += p->ber_error_count;
275 c->post_bit_count.stat[0].uvalue += p->ber_bit_count;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300276
277 /* Legacy PER/BER */
Mauro Carvalho Chehabf35d09d2013-03-22 06:22:45 -0300278 tmp = p->ets_packets * 65535;
Bjørn Morkec532502013-08-14 05:24:39 -0300279 if (p->ts_packets + p->ets_packets)
280 do_div(tmp, p->ts_packets + p->ets_packets);
Mauro Carvalho Chehabf35d09d2013-03-22 06:22:45 -0300281 client->legacy_per = tmp;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300282}
283
284static void smsdvb_update_dvb_stats(struct smsdvb_client_t *client,
Mauro Carvalho Chehabcf0e9cf2013-03-21 09:06:03 -0300285 struct sms_stats *p)
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300286{
287 struct dvb_frontend *fe = &client->frontend;
288 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
289
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -0300290 if (client->prt_dvb_stats)
291 client->prt_dvb_stats(client->debug_data, p);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300292
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300293 client->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300294
295 /* Update DVB modulation parameters */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300296 c->frequency = p->frequency;
297 client->fe_status = sms_to_status(p->is_demod_locked, 0);
298 c->bandwidth_hz = sms_to_bw(p->bandwidth);
299 c->transmission_mode = sms_to_mode(p->transmission_mode);
300 c->guard_interval = sms_to_guard_interval(p->guard_interval);
301 c->code_rate_HP = sms_to_code_rate(p->code_rate);
302 c->code_rate_LP = sms_to_code_rate(p->lp_code_rate);
303 c->hierarchy = sms_to_hierarchy(p->hierarchy);
304 c->modulation = sms_to_modulation(p->constellation);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300305
306 /* update reception data */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300307 c->lna = p->is_external_lna_on ? 1 : 0;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300308
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300309 /* Carrier to noise ratio, in DB */
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300310 c->cnr.stat[0].svalue = p->SNR * 1000;
311
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300312 /* signal Strength, in DBm */
313 c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300314
315 /* PER/BER requires demod lock */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300316 if (!p->is_demod_locked)
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300317 return;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300318
319 /* TS PER */
320 client->last_per = c->block_error.stat[0].uvalue;
321 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
322 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300323 c->block_error.stat[0].uvalue += p->error_ts_packets;
324 c->block_count.stat[0].uvalue += p->total_ts_packets;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300325
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300326 /* ber */
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300327 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
328 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300329 c->post_bit_error.stat[0].uvalue += p->ber_error_count;
330 c->post_bit_count.stat[0].uvalue += p->ber_bit_count;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300331
332 /* Legacy PER/BER */
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300333 client->legacy_ber = p->ber;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300334};
335
336static void smsdvb_update_isdbt_stats(struct smsdvb_client_t *client,
Mauro Carvalho Chehabcf0e9cf2013-03-21 09:06:03 -0300337 struct sms_isdbt_stats *p)
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300338{
339 struct dvb_frontend *fe = &client->frontend;
340 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabcf0e9cf2013-03-21 09:06:03 -0300341 struct sms_isdbt_layer_stats *lr;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300342 int i, n_layers;
343
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -0300344 if (client->prt_isdb_stats)
345 client->prt_isdb_stats(client->debug_data, p);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300346
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300347 client->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
Mauro Carvalho Chehab773adad2013-03-16 21:05:30 -0300348
349 /*
350 * Firmware 2.1 seems to report only lock status and
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300351 * signal strength. The signal strength indicator is at the
Mauro Carvalho Chehab773adad2013-03-16 21:05:30 -0300352 * wrong field.
353 */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300354 if (p->statistics_type == 0) {
355 c->strength.stat[0].uvalue = ((s32)p->transmission_mode) * 1000;
Mauro Carvalho Chehab773adad2013-03-16 21:05:30 -0300356 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
357 return;
358 }
359
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300360 /* Update ISDB-T transmission parameters */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300361 c->frequency = p->frequency;
362 c->bandwidth_hz = sms_to_bw(p->bandwidth);
363 c->transmission_mode = sms_to_mode(p->transmission_mode);
364 c->guard_interval = sms_to_guard_interval(p->guard_interval);
365 c->isdbt_partial_reception = p->partial_reception ? 1 : 0;
366 n_layers = p->num_of_layers;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300367 if (n_layers < 1)
368 n_layers = 1;
369 if (n_layers > 3)
370 n_layers = 3;
371 c->isdbt_layer_enabled = 0;
372
373 /* update reception data */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300374 c->lna = p->is_external_lna_on ? 1 : 0;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300375
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300376 /* Carrier to noise ratio, in DB */
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300377 c->cnr.stat[0].svalue = p->SNR * 1000;
378
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300379 /* signal Strength, in DBm */
380 c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300381
382 /* PER/BER and per-layer stats require demod lock */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300383 if (!p->is_demod_locked)
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300384 return;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300385
386 client->last_per = c->block_error.stat[0].uvalue;
387
388 /* Clears global counters, as the code below will sum it again */
389 c->block_error.stat[0].uvalue = 0;
390 c->block_count.stat[0].uvalue = 0;
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300391 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
392 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300393 c->post_bit_error.stat[0].uvalue = 0;
394 c->post_bit_count.stat[0].uvalue = 0;
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300395 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
396 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300397
398 for (i = 0; i < n_layers; i++) {
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300399 lr = &p->layer_info[i];
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300400
401 /* Update per-layer transmission parameters */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300402 if (lr->number_of_segments > 0 && lr->number_of_segments < 13) {
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300403 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300404 c->layer[i].segment_count = lr->number_of_segments;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300405 } else {
406 continue;
407 }
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300408 c->layer[i].modulation = sms_to_modulation(lr->constellation);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300409
410 /* TS PER */
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300411 c->block_error.stat[i + 1].scale = FE_SCALE_COUNTER;
412 c->block_count.stat[i + 1].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300413 c->block_error.stat[i + 1].uvalue += lr->error_ts_packets;
414 c->block_count.stat[i + 1].uvalue += lr->total_ts_packets;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300415
416 /* Update global PER counter */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300417 c->block_error.stat[0].uvalue += lr->error_ts_packets;
418 c->block_count.stat[0].uvalue += lr->total_ts_packets;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300419
420 /* BER */
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300421 c->post_bit_error.stat[i + 1].scale = FE_SCALE_COUNTER;
422 c->post_bit_count.stat[i + 1].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300423 c->post_bit_error.stat[i + 1].uvalue += lr->ber_error_count;
424 c->post_bit_count.stat[i + 1].uvalue += lr->ber_bit_count;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300425
426 /* Update global BER counter */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300427 c->post_bit_error.stat[0].uvalue += lr->ber_error_count;
428 c->post_bit_count.stat[0].uvalue += lr->ber_bit_count;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300429 }
430}
431
432static void smsdvb_update_isdbt_stats_ex(struct smsdvb_client_t *client,
Mauro Carvalho Chehabcf0e9cf2013-03-21 09:06:03 -0300433 struct sms_isdbt_stats_ex *p)
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300434{
435 struct dvb_frontend *fe = &client->frontend;
436 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabcf0e9cf2013-03-21 09:06:03 -0300437 struct sms_isdbt_layer_stats *lr;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300438 int i, n_layers;
439
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -0300440 if (client->prt_isdb_stats_ex)
441 client->prt_isdb_stats_ex(client->debug_data, p);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300442
443 /* Update ISDB-T transmission parameters */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300444 c->frequency = p->frequency;
445 client->fe_status = sms_to_status(p->is_demod_locked, 0);
446 c->bandwidth_hz = sms_to_bw(p->bandwidth);
447 c->transmission_mode = sms_to_mode(p->transmission_mode);
448 c->guard_interval = sms_to_guard_interval(p->guard_interval);
449 c->isdbt_partial_reception = p->partial_reception ? 1 : 0;
450 n_layers = p->num_of_layers;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300451 if (n_layers < 1)
452 n_layers = 1;
453 if (n_layers > 3)
454 n_layers = 3;
455 c->isdbt_layer_enabled = 0;
456
457 /* update reception data */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300458 c->lna = p->is_external_lna_on ? 1 : 0;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300459
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300460 /* Carrier to noise ratio, in DB */
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300461 c->cnr.stat[0].svalue = p->SNR * 1000;
462
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300463 /* signal Strength, in DBm */
464 c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300465
466 /* PER/BER and per-layer stats require demod lock */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300467 if (!p->is_demod_locked)
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300468 return;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300469
470 client->last_per = c->block_error.stat[0].uvalue;
471
472 /* Clears global counters, as the code below will sum it again */
473 c->block_error.stat[0].uvalue = 0;
474 c->block_count.stat[0].uvalue = 0;
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300475 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
476 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300477 c->post_bit_error.stat[0].uvalue = 0;
478 c->post_bit_count.stat[0].uvalue = 0;
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300479 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
480 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300481
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300482 c->post_bit_error.len = n_layers + 1;
483 c->post_bit_count.len = n_layers + 1;
484 c->block_error.len = n_layers + 1;
485 c->block_count.len = n_layers + 1;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300486 for (i = 0; i < n_layers; i++) {
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300487 lr = &p->layer_info[i];
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300488
489 /* Update per-layer transmission parameters */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300490 if (lr->number_of_segments > 0 && lr->number_of_segments < 13) {
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300491 c->isdbt_layer_enabled |= 1 << i;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300492 c->layer[i].segment_count = lr->number_of_segments;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300493 } else {
494 continue;
495 }
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300496 c->layer[i].modulation = sms_to_modulation(lr->constellation);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300497
498 /* TS PER */
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300499 c->block_error.stat[i + 1].scale = FE_SCALE_COUNTER;
500 c->block_count.stat[i + 1].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300501 c->block_error.stat[i + 1].uvalue += lr->error_ts_packets;
502 c->block_count.stat[i + 1].uvalue += lr->total_ts_packets;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300503
504 /* Update global PER counter */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300505 c->block_error.stat[0].uvalue += lr->error_ts_packets;
506 c->block_count.stat[0].uvalue += lr->total_ts_packets;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300507
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300508 /* ber */
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300509 c->post_bit_error.stat[i + 1].scale = FE_SCALE_COUNTER;
510 c->post_bit_count.stat[i + 1].scale = FE_SCALE_COUNTER;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300511 c->post_bit_error.stat[i + 1].uvalue += lr->ber_error_count;
512 c->post_bit_count.stat[i + 1].uvalue += lr->ber_bit_count;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300513
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300514 /* Update global ber counter */
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300515 c->post_bit_error.stat[0].uvalue += lr->ber_error_count;
516 c->post_bit_count.stat[0].uvalue += lr->ber_bit_count;
Mauro Carvalho Chehab5eb23972009-12-25 11:29:42 -0300517 }
518}
519
Michael Krufky0c071f32008-06-21 02:44:02 -0300520static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300521{
Michael Krufky18245e12008-06-15 17:52:24 -0300522 struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300523 struct sms_msg_hdr *phdr = (struct sms_msg_hdr *) (((u8 *) cb->p)
Uri Shkolnik793786d2009-05-12 12:28:46 -0300524 + cb->offset);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300525 void *p = phdr + 1;
526 struct dvb_frontend *fe = &client->frontend;
527 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300528 bool is_status_update = false;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300529
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300530 switch (phdr->msg_type) {
Michael Krufky82237412008-06-15 15:14:13 -0300531 case MSG_SMS_DVBT_BDA_DATA:
Mauro Carvalho Chehabe1b2ac42013-03-15 07:22:08 -0300532 /*
533 * Only feed data to dvb demux if are there any feed listening
534 * to it and if the device has tuned
535 */
536 if (client->feed_users && client->has_tuned)
537 dvb_dmx_swfilter(&client->demux, p,
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300538 cb->size - sizeof(struct sms_msg_hdr));
Michael Krufky82237412008-06-15 15:14:13 -0300539 break;
540
541 case MSG_SMS_RF_TUNE_RES:
Michael Krufky6b26fce2009-12-22 21:08:49 -0300542 case MSG_SMS_ISDBT_TUNE_RES:
Michael Krufky82237412008-06-15 15:14:13 -0300543 complete(&client->tune_done);
544 break;
545
Uri Shkolnik793786d2009-05-12 12:28:46 -0300546 case MSG_SMS_SIGNAL_DETECTED_IND:
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300547 client->fe_status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
548 FE_HAS_VITERBI | FE_HAS_SYNC |
549 FE_HAS_LOCK;
550
Uri Shkolnik793786d2009-05-12 12:28:46 -0300551 is_status_update = true;
552 break;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300553
Uri Shkolnik793786d2009-05-12 12:28:46 -0300554 case MSG_SMS_NO_SIGNAL_IND:
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300555 client->fe_status = 0;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300556
557 is_status_update = true;
558 break;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300559
560 case MSG_SMS_TRANSMISSION_IND:
561 smsdvb_update_tx_params(client, p);
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300562
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300563 is_status_update = true;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300564 break;
Mauro Carvalho Chehab5eb23972009-12-25 11:29:42 -0300565
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300566 case MSG_SMS_HO_PER_SLICES_IND:
567 smsdvb_update_per_slices(client, p);
568
569 is_status_update = true;
570 break;
571
572 case MSG_SMS_GET_STATISTICS_RES:
Mauro Carvalho Chehab5eb23972009-12-25 11:29:42 -0300573 switch (smscore_get_device_mode(client->coredev)) {
574 case DEVICE_MODE_ISDBT:
575 case DEVICE_MODE_ISDBT_BDA:
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300576 smsdvb_update_isdbt_stats(client, p);
Mauro Carvalho Chehab5eb23972009-12-25 11:29:42 -0300577 break;
578 default:
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300579 /* Skip sms_msg_statistics_info:request_result field */
Mauro Carvalho Chehabc02272f2013-03-09 23:01:48 -0300580 smsdvb_update_dvb_stats(client, p + sizeof(u32));
Mauro Carvalho Chehab5eb23972009-12-25 11:29:42 -0300581 }
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300582
583 is_status_update = true;
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300584 break;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300585
586 /* Only for ISDB-T */
587 case MSG_SMS_GET_STATISTICS_EX_RES:
Mauro Carvalho Chehabdfef84f2013-03-21 08:49:43 -0300588 /* Skip sms_msg_statistics_info:request_result field? */
Mauro Carvalho Chehabc02272f2013-03-09 23:01:48 -0300589 smsdvb_update_isdbt_stats_ex(client, p + sizeof(u32));
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300590 is_status_update = true;
591 break;
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300592 default:
Mauro Carvalho Chehab4c3bdb52013-03-09 09:27:39 -0300593 sms_info("message not handled");
Uri Shkolnik793786d2009-05-12 12:28:46 -0300594 }
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300595 smscore_putbuffer(client->coredev, cb);
596
Uri Shkolnik793786d2009-05-12 12:28:46 -0300597 if (is_status_update) {
Mauro Carvalho Chehab96710452013-03-10 13:38:41 -0300598 if (client->fe_status & FE_HAS_LOCK) {
Uri Shkolnik793786d2009-05-12 12:28:46 -0300599 sms_board_dvb3_event(client, DVB3_EVENT_FE_LOCK);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300600 if (client->last_per == c->block_error.stat[0].uvalue)
Uri Shkolnik793786d2009-05-12 12:28:46 -0300601 sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
602 else
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300603 sms_board_dvb3_event(client, DVB3_EVENT_UNC_ERR);
Mauro Carvalho Chehabe1b2ac42013-03-15 07:22:08 -0300604 client->has_tuned = true;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300605 } else {
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300606 smsdvb_stats_not_ready(fe);
Mauro Carvalho Chehabe1b2ac42013-03-15 07:22:08 -0300607 client->has_tuned = false;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300608 sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
609 }
Mauro Carvalho Chehab76e41a62013-03-07 16:32:33 -0300610 complete(&client->stats_done);
Uri Shkolnik793786d2009-05-12 12:28:46 -0300611 }
612
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300613 return 0;
614}
615
Michael Krufky0c071f32008-06-21 02:44:02 -0300616static void smsdvb_unregister_client(struct smsdvb_client_t *client)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300617{
Michael Krufkyfa830e82008-06-15 15:52:43 -0300618 /* must be called under clientslock */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300619
620 list_del(&client->entry);
621
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -0300622 smsdvb_debugfs_release(client);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300623 smscore_unregister_client(client->smsclient);
624 dvb_unregister_frontend(&client->frontend);
625 dvb_dmxdev_release(&client->dmxdev);
626 dvb_dmx_release(&client->demux);
627 dvb_unregister_adapter(&client->adapter);
628 kfree(client);
629}
630
Michael Krufky0c071f32008-06-21 02:44:02 -0300631static void smsdvb_onremove(void *context)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300632{
633 kmutex_lock(&g_smsdvb_clientslock);
634
Michael Krufky18245e12008-06-15 17:52:24 -0300635 smsdvb_unregister_client((struct smsdvb_client_t *) context);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300636
637 kmutex_unlock(&g_smsdvb_clientslock);
638}
639
640static int smsdvb_start_feed(struct dvb_demux_feed *feed)
641{
Michael Krufky18245e12008-06-15 17:52:24 -0300642 struct smsdvb_client_t *client =
643 container_of(feed->demux, struct smsdvb_client_t, demux);
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300644 struct sms_msg_data pid_msg;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300645
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300646 sms_debug("add pid %d(%x)",
Michael Krufky068d6c02008-06-19 01:15:46 -0300647 feed->pid, feed->pid);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300648
Mauro Carvalho Chehabe1b2ac42013-03-15 07:22:08 -0300649 client->feed_users++;
650
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300651 pid_msg.x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
652 pid_msg.x_msg_header.msg_dst_id = HIF_TASK;
653 pid_msg.x_msg_header.msg_flags = 0;
654 pid_msg.x_msg_header.msg_type = MSG_SMS_ADD_PID_FILTER_REQ;
655 pid_msg.x_msg_header.msg_length = sizeof(pid_msg);
656 pid_msg.msg_data[0] = feed->pid;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300657
Michael Krufky82237412008-06-15 15:14:13 -0300658 return smsclient_sendrequest(client->smsclient,
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300659 &pid_msg, sizeof(pid_msg));
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300660}
661
662static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
663{
Michael Krufky18245e12008-06-15 17:52:24 -0300664 struct smsdvb_client_t *client =
665 container_of(feed->demux, struct smsdvb_client_t, demux);
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300666 struct sms_msg_data pid_msg;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300667
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300668 sms_debug("remove pid %d(%x)",
Michael Krufky068d6c02008-06-19 01:15:46 -0300669 feed->pid, feed->pid);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300670
Mauro Carvalho Chehabe1b2ac42013-03-15 07:22:08 -0300671 client->feed_users--;
672
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300673 pid_msg.x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
674 pid_msg.x_msg_header.msg_dst_id = HIF_TASK;
675 pid_msg.x_msg_header.msg_flags = 0;
676 pid_msg.x_msg_header.msg_type = MSG_SMS_REMOVE_PID_FILTER_REQ;
677 pid_msg.x_msg_header.msg_length = sizeof(pid_msg);
678 pid_msg.msg_data[0] = feed->pid;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300679
Michael Krufky82237412008-06-15 15:14:13 -0300680 return smsclient_sendrequest(client->smsclient,
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300681 &pid_msg, sizeof(pid_msg));
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300682}
683
Michael Krufky18245e12008-06-15 17:52:24 -0300684static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
Michael Krufkya83ccdd2008-05-06 03:11:51 -0300685 void *buffer, size_t size,
686 struct completion *completion)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300687{
Uri Shkolnikba79bb22009-05-12 11:37:09 -0300688 int rc;
689
Uri Shkolnikba79bb22009-05-12 11:37:09 -0300690 rc = smsclient_sendrequest(client->smsclient, buffer, size);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300691 if (rc < 0)
692 return rc;
693
Michael Krufky82237412008-06-15 15:14:13 -0300694 return wait_for_completion_timeout(completion,
695 msecs_to_jiffies(2000)) ?
696 0 : -ETIME;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300697}
698
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300699static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
700{
701 int rc;
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300702 struct sms_msg_hdr msg;
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300703
Mauro Carvalho Chehaba9b9fbd2013-03-10 10:51:25 -0300704 /* Don't request stats too fast */
705 if (client->get_stats_jiffies &&
706 (!time_after(jiffies, client->get_stats_jiffies)))
707 return 0;
708 client->get_stats_jiffies = jiffies + msecs_to_jiffies(100);
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300709
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300710 msg.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
711 msg.msg_dst_id = HIF_TASK;
712 msg.msg_flags = 0;
713 msg.msg_length = sizeof(msg);
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300714
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300715 switch (smscore_get_device_mode(client->coredev)) {
716 case DEVICE_MODE_ISDBT:
717 case DEVICE_MODE_ISDBT_BDA:
718 /*
719 * Check for firmware version, to avoid breaking for old cards
720 */
721 if (client->coredev->fw_version >= 0x800)
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300722 msg.msg_type = MSG_SMS_GET_STATISTICS_EX_REQ;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300723 else
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300724 msg.msg_type = MSG_SMS_GET_STATISTICS_REQ;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300725 break;
726 default:
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300727 msg.msg_type = MSG_SMS_GET_STATISTICS_REQ;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300728 }
Mauro Carvalho Chehabb4059092013-03-07 21:58:47 -0300729
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300730 rc = smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
Mauro Carvalho Chehab76e41a62013-03-07 16:32:33 -0300731 &client->stats_done);
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300732
733 return rc;
734}
735
Michael Krufky3746b612009-07-12 23:30:14 -0300736static inline int led_feedback(struct smsdvb_client_t *client)
737{
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300738 if (!(client->fe_status & FE_HAS_LOCK))
Michael Krufky3746b612009-07-12 23:30:14 -0300739 return sms_board_led_feedback(client->coredev, SMS_LED_OFF);
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300740
741 return sms_board_led_feedback(client->coredev,
742 (client->legacy_ber == 0) ?
743 SMS_LED_HI : SMS_LED_LO);
Michael Krufky3746b612009-07-12 23:30:14 -0300744}
745
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300746static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
747{
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300748 int rc;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300749 struct smsdvb_client_t *client;
750 client = container_of(fe, struct smsdvb_client_t, frontend);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300751
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300752 rc = smsdvb_send_statistics_request(client);
753
Uri Shkolnik793786d2009-05-12 12:28:46 -0300754 *stat = client->fe_status;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300755
Michael Krufky3746b612009-07-12 23:30:14 -0300756 led_feedback(client);
757
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300758 return rc;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300759}
760
761static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
762{
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300763 int rc;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300764 struct smsdvb_client_t *client;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300765
Uri Shkolnik793786d2009-05-12 12:28:46 -0300766 client = container_of(fe, struct smsdvb_client_t, frontend);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300767
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300768 rc = smsdvb_send_statistics_request(client);
769
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300770 *ber = client->legacy_ber;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300771
Michael Krufky3746b612009-07-12 23:30:14 -0300772 led_feedback(client);
773
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300774 return rc;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300775}
776
777static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
778{
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300779 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300780 int rc;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300781 s32 power = (s32) c->strength.stat[0].uvalue;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300782 struct smsdvb_client_t *client;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300783
Uri Shkolnik793786d2009-05-12 12:28:46 -0300784 client = container_of(fe, struct smsdvb_client_t, frontend);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300785
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300786 rc = smsdvb_send_statistics_request(client);
787
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300788 if (power < -95)
Uri Shkolnik793786d2009-05-12 12:28:46 -0300789 *strength = 0;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300790 else if (power > -29)
791 *strength = 65535;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300792 else
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300793 *strength = (power + 95) * 65535 / 66;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300794
Michael Krufky3746b612009-07-12 23:30:14 -0300795 led_feedback(client);
796
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300797 return rc;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300798}
799
800static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
801{
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300802 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300803 int rc;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300804 struct smsdvb_client_t *client;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300805
Uri Shkolnik793786d2009-05-12 12:28:46 -0300806 client = container_of(fe, struct smsdvb_client_t, frontend);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300807
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300808 rc = smsdvb_send_statistics_request(client);
809
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300810 /* Preferred scale for SNR with legacy API: 0.1 dB */
Mauro Carvalho Chehabf35d09d2013-03-22 06:22:45 -0300811 *snr = ((u32)c->cnr.stat[0].svalue) / 100;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300812
Michael Krufky3746b612009-07-12 23:30:14 -0300813 led_feedback(client);
814
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300815 return rc;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300816}
817
Michael Krufky851a9092008-11-22 14:56:37 -0300818static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
819{
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300820 int rc;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300821 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300822 struct smsdvb_client_t *client;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300823
Uri Shkolnik793786d2009-05-12 12:28:46 -0300824 client = container_of(fe, struct smsdvb_client_t, frontend);
Michael Krufky851a9092008-11-22 14:56:37 -0300825
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300826 rc = smsdvb_send_statistics_request(client);
827
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300828 *ucblocks = c->block_error.stat[0].uvalue;
Michael Krufky851a9092008-11-22 14:56:37 -0300829
Michael Krufky3746b612009-07-12 23:30:14 -0300830 led_feedback(client);
831
Mauro Carvalho Chehab67ae1d22009-12-23 10:07:16 -0300832 return rc;
Michael Krufky851a9092008-11-22 14:56:37 -0300833}
834
Michael Krufky82237412008-06-15 15:14:13 -0300835static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
836 struct dvb_frontend_tune_settings *tune)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300837{
Michael Krufkya0c0abc2008-06-19 20:35:21 -0300838 sms_debug("");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300839
840 tune->min_delay_ms = 400;
841 tune->step_size = 250000;
842 tune->max_drift = 0;
843 return 0;
844}
845
Mauro Carvalho Chehab15115c12011-12-26 16:31:29 -0300846static int smsdvb_dvbt_set_frontend(struct dvb_frontend *fe)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300847{
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300848 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Michael Krufky18245e12008-06-15 17:52:24 -0300849 struct smsdvb_client_t *client =
850 container_of(fe, struct smsdvb_client_t, frontend);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300851
Michael Krufky18245e12008-06-15 17:52:24 -0300852 struct {
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300853 struct sms_msg_hdr msg;
Michael Krufky82237412008-06-15 15:14:13 -0300854 u32 Data[3];
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300855 } msg;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300856
Michael Krufky3746b612009-07-12 23:30:14 -0300857 int ret;
858
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300859 client->fe_status = 0;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300860 client->event_fe_state = -1;
861 client->event_unc_state = -1;
Mauro Carvalho Chehabe85c97a2009-12-25 07:17:03 -0300862 fe->dtv_property_cache.delivery_system = SYS_DVBT;
Uri Shkolnik793786d2009-05-12 12:28:46 -0300863
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300864 msg.msg.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
865 msg.msg.msg_dst_id = HIF_TASK;
866 msg.msg.msg_flags = 0;
867 msg.msg.msg_type = MSG_SMS_RF_TUNE_REQ;
868 msg.msg.msg_length = sizeof(msg);
869 msg.Data[0] = c->frequency;
870 msg.Data[2] = 12000000;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300871
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300872 sms_info("%s: freq %d band %d", __func__, c->frequency,
873 c->bandwidth_hz);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300874
Mauro Carvalho Chehab643e15a2009-12-25 07:29:06 -0300875 switch (c->bandwidth_hz / 1000000) {
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300876 case 8:
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300877 msg.Data[1] = BW_8_MHZ;
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300878 break;
879 case 7:
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300880 msg.Data[1] = BW_7_MHZ;
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300881 break;
882 case 6:
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300883 msg.Data[1] = BW_6_MHZ;
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300884 break;
885 case 0:
886 return -EOPNOTSUPP;
887 default:
888 return -EINVAL;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300889 }
Michael Krufky3746b612009-07-12 23:30:14 -0300890 /* Disable LNA, if any. An error is returned if no LNA is present */
891 ret = sms_board_lna_control(client->coredev, 0);
892 if (ret == 0) {
893 fe_status_t status;
894
895 /* tune with LNA off at first */
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300896 ret = smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
Michael Krufky3746b612009-07-12 23:30:14 -0300897 &client->tune_done);
898
899 smsdvb_read_status(fe, &status);
900
901 if (status & FE_HAS_LOCK)
902 return ret;
903
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300904 /* previous tune didn't lock - enable LNA and tune again */
Michael Krufky3746b612009-07-12 23:30:14 -0300905 sms_board_lna_control(client->coredev, 1);
906 }
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300907
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300908 return smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
Michael Krufky82237412008-06-15 15:14:13 -0300909 &client->tune_done);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -0300910}
911
Mauro Carvalho Chehab15115c12011-12-26 16:31:29 -0300912static int smsdvb_isdbt_set_frontend(struct dvb_frontend *fe)
Michael Krufky6b26fce2009-12-22 21:08:49 -0300913{
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300914 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Michael Krufky6b26fce2009-12-22 21:08:49 -0300915 struct smsdvb_client_t *client =
916 container_of(fe, struct smsdvb_client_t, frontend);
Mauro Carvalho Chehaba51fea42013-03-07 16:34:06 -0300917 int board_id = smscore_get_board_id(client->coredev);
918 struct sms_board *board = sms_get_board(board_id);
919 enum sms_device_type_st type = board->type;
Mauro Carvalho Chehab0c189fa2013-03-07 16:34:53 -0300920 int ret;
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -0300921
Michael Krufky6b26fce2009-12-22 21:08:49 -0300922 struct {
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300923 struct sms_msg_hdr msg;
Michael Krufky6b26fce2009-12-22 21:08:49 -0300924 u32 Data[4];
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300925 } msg;
Michael Krufky6b26fce2009-12-22 21:08:49 -0300926
Mauro Carvalho Chehabe85c97a2009-12-25 07:17:03 -0300927 fe->dtv_property_cache.delivery_system = SYS_ISDBT;
928
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300929 msg.msg.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
930 msg.msg.msg_dst_id = HIF_TASK;
931 msg.msg.msg_flags = 0;
932 msg.msg.msg_type = MSG_SMS_ISDBT_TUNE_REQ;
933 msg.msg.msg_length = sizeof(msg);
Michael Krufky6b26fce2009-12-22 21:08:49 -0300934
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300935 if (c->isdbt_sb_segment_idx == -1)
936 c->isdbt_sb_segment_idx = 0;
Michael Krufky6b26fce2009-12-22 21:08:49 -0300937
Mauro Carvalho Chehaba51fea42013-03-07 16:34:06 -0300938 if (!c->isdbt_layer_enabled)
939 c->isdbt_layer_enabled = 7;
Michael Krufky6b26fce2009-12-22 21:08:49 -0300940
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300941 msg.Data[0] = c->frequency;
942 msg.Data[1] = BW_ISDBT_1SEG;
943 msg.Data[2] = 12000000;
944 msg.Data[3] = c->isdbt_sb_segment_idx;
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300945
Mauro Carvalho Chehaba51fea42013-03-07 16:34:06 -0300946 if (c->isdbt_partial_reception) {
947 if ((type == SMS_PELE || type == SMS_RIO) &&
948 c->isdbt_sb_segment_count > 3)
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300949 msg.Data[1] = BW_ISDBT_13SEG;
Mauro Carvalho Chehaba51fea42013-03-07 16:34:06 -0300950 else if (c->isdbt_sb_segment_count > 1)
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300951 msg.Data[1] = BW_ISDBT_3SEG;
Mauro Carvalho Chehaba51fea42013-03-07 16:34:06 -0300952 } else if (type == SMS_PELE || type == SMS_RIO)
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300953 msg.Data[1] = BW_ISDBT_13SEG;
Mauro Carvalho Chehaba51fea42013-03-07 16:34:06 -0300954
955 c->bandwidth_hz = 6000000;
956
Mauro Carvalho Chehab98c3f942013-03-19 13:06:28 -0300957 sms_info("%s: freq %d segwidth %d segindex %d", __func__,
Mauro Carvalho Chehabcf4fab72009-12-23 11:28:46 -0300958 c->frequency, c->isdbt_sb_segment_count,
959 c->isdbt_sb_segment_idx);
960
Mauro Carvalho Chehab0c189fa2013-03-07 16:34:53 -0300961 /* Disable LNA, if any. An error is returned if no LNA is present */
962 ret = sms_board_lna_control(client->coredev, 0);
963 if (ret == 0) {
964 fe_status_t status;
965
966 /* tune with LNA off at first */
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300967 ret = smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
Mauro Carvalho Chehab0c189fa2013-03-07 16:34:53 -0300968 &client->tune_done);
969
970 smsdvb_read_status(fe, &status);
971
972 if (status & FE_HAS_LOCK)
973 return ret;
974
975 /* previous tune didn't lock - enable LNA and tune again */
976 sms_board_lna_control(client->coredev, 1);
977 }
Mauro Carvalho Chehab28a59df2013-03-21 09:27:37 -0300978 return smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
Michael Krufky6b26fce2009-12-22 21:08:49 -0300979 &client->tune_done);
980}
981
Mauro Carvalho Chehab15115c12011-12-26 16:31:29 -0300982static int smsdvb_set_frontend(struct dvb_frontend *fe)
Michael Krufky6b26fce2009-12-22 21:08:49 -0300983{
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300984 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Michael Krufky6b26fce2009-12-22 21:08:49 -0300985 struct smsdvb_client_t *client =
986 container_of(fe, struct smsdvb_client_t, frontend);
987 struct smscore_device_t *coredev = client->coredev;
988
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300989 smsdvb_stats_not_ready(fe);
Mauro Carvalho Chehabf5de95e2013-03-10 12:06:30 -0300990 c->strength.stat[0].uvalue = 0;
991 c->cnr.stat[0].uvalue = 0;
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -0300992
Mauro Carvalho Chehabe1b2ac42013-03-15 07:22:08 -0300993 client->has_tuned = false;
994
Michael Krufky6b26fce2009-12-22 21:08:49 -0300995 switch (smscore_get_device_mode(coredev)) {
996 case DEVICE_MODE_DVBT:
997 case DEVICE_MODE_DVBT_BDA:
Mauro Carvalho Chehab15115c12011-12-26 16:31:29 -0300998 return smsdvb_dvbt_set_frontend(fe);
Michael Krufky6b26fce2009-12-22 21:08:49 -0300999 case DEVICE_MODE_ISDBT:
1000 case DEVICE_MODE_ISDBT_BDA:
Mauro Carvalho Chehab15115c12011-12-26 16:31:29 -03001001 return smsdvb_isdbt_set_frontend(fe);
Michael Krufky6b26fce2009-12-22 21:08:49 -03001002 default:
1003 return -EINVAL;
1004 }
1005}
1006
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -03001007/* Nothing to do here, as stats are automatically updated */
Mauro Carvalho Chehabeab0fa02013-03-09 12:05:58 -03001008static int smsdvb_get_frontend(struct dvb_frontend *fe)
1009{
Mauro Carvalho Chehabd42f1cb2013-03-09 19:26:28 -03001010 return 0;
Michael Krufky7a6fbed2008-11-22 14:26:37 -03001011}
1012
1013static int smsdvb_init(struct dvb_frontend *fe)
1014{
1015 struct smsdvb_client_t *client =
1016 container_of(fe, struct smsdvb_client_t, frontend);
1017
Michael Krufky3746b612009-07-12 23:30:14 -03001018 sms_board_power(client->coredev, 1);
1019
Uri Shkolnik793786d2009-05-12 12:28:46 -03001020 sms_board_dvb3_event(client, DVB3_EVENT_INIT);
Michael Krufky7a6fbed2008-11-22 14:26:37 -03001021 return 0;
1022}
1023
1024static int smsdvb_sleep(struct dvb_frontend *fe)
1025{
1026 struct smsdvb_client_t *client =
1027 container_of(fe, struct smsdvb_client_t, frontend);
1028
Michael Krufky3746b612009-07-12 23:30:14 -03001029 sms_board_led_feedback(client->coredev, SMS_LED_OFF);
1030 sms_board_power(client->coredev, 0);
1031
Uri Shkolnik793786d2009-05-12 12:28:46 -03001032 sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
Michael Krufky7a6fbed2008-11-22 14:26:37 -03001033
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001034 return 0;
1035}
1036
1037static void smsdvb_release(struct dvb_frontend *fe)
1038{
Michael Krufkyfa830e82008-06-15 15:52:43 -03001039 /* do nothing */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001040}
1041
1042static struct dvb_frontend_ops smsdvb_fe_ops = {
1043 .info = {
Uri Shkolnike0f14c22008-08-31 00:44:04 -03001044 .name = "Siano Mobile Digital MDTV Receiver",
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001045 .frequency_min = 44250000,
1046 .frequency_max = 867250000,
1047 .frequency_stepsize = 250000,
1048 .caps = FE_CAN_INVERSION_AUTO |
Michael Krufky82237412008-06-15 15:14:13 -03001049 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1050 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1051 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1052 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
1053 FE_CAN_GUARD_INTERVAL_AUTO |
1054 FE_CAN_RECOVER |
1055 FE_CAN_HIERARCHY_AUTO,
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001056 },
1057
1058 .release = smsdvb_release,
1059
Mauro Carvalho Chehab15115c12011-12-26 16:31:29 -03001060 .set_frontend = smsdvb_set_frontend,
1061 .get_frontend = smsdvb_get_frontend,
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001062 .get_tune_settings = smsdvb_get_tune_settings,
1063
1064 .read_status = smsdvb_read_status,
1065 .read_ber = smsdvb_read_ber,
1066 .read_signal_strength = smsdvb_read_signal_strength,
1067 .read_snr = smsdvb_read_snr,
Michael Krufky851a9092008-11-22 14:56:37 -03001068 .read_ucblocks = smsdvb_read_ucblocks,
Michael Krufky7a6fbed2008-11-22 14:26:37 -03001069
1070 .init = smsdvb_init,
1071 .sleep = smsdvb_sleep,
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001072};
1073
Michael Krufky0c071f32008-06-21 02:44:02 -03001074static int smsdvb_hotplug(struct smscore_device_t *coredev,
1075 struct device *device, int arrival)
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001076{
Michael Krufky18245e12008-06-15 17:52:24 -03001077 struct smsclient_params_t params;
1078 struct smsdvb_client_t *client;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001079 int rc;
1080
Michael Krufkyfa830e82008-06-15 15:52:43 -03001081 /* device removal handled by onremove callback */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001082 if (!arrival)
1083 return 0;
Michael Krufky18245e12008-06-15 17:52:24 -03001084 client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
Michael Krufky82237412008-06-15 15:14:13 -03001085 if (!client) {
Michael Krufkyeb250942008-06-19 22:07:23 -03001086 sms_err("kmalloc() failed");
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001087 return -ENOMEM;
1088 }
1089
Michael Krufkyfa830e82008-06-15 15:52:43 -03001090 /* register dvb adapter */
Michael Krufky1c11d542008-06-18 22:09:55 -03001091 rc = dvb_register_adapter(&client->adapter,
1092 sms_get_board(
1093 smscore_get_board_id(coredev))->name,
Michael Krufky82237412008-06-15 15:14:13 -03001094 THIS_MODULE, device, adapter_nr);
1095 if (rc < 0) {
Michael Krufkya0c0abc2008-06-19 20:35:21 -03001096 sms_err("dvb_register_adapter() failed %d", rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001097 goto adapter_error;
1098 }
1099
Michael Krufkyfa830e82008-06-15 15:52:43 -03001100 /* init dvb demux */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001101 client->demux.dmx.capabilities = DMX_TS_FILTERING;
Michael Krufkyfa830e82008-06-15 15:52:43 -03001102 client->demux.filternum = 32; /* todo: nova ??? */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001103 client->demux.feednum = 32;
1104 client->demux.start_feed = smsdvb_start_feed;
1105 client->demux.stop_feed = smsdvb_stop_feed;
1106
1107 rc = dvb_dmx_init(&client->demux);
Michael Krufky82237412008-06-15 15:14:13 -03001108 if (rc < 0) {
Michael Krufkya0c0abc2008-06-19 20:35:21 -03001109 sms_err("dvb_dmx_init failed %d", rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001110 goto dvbdmx_error;
1111 }
1112
Michael Krufkyfa830e82008-06-15 15:52:43 -03001113 /* init dmxdev */
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001114 client->dmxdev.filternum = 32;
1115 client->dmxdev.demux = &client->demux.dmx;
1116 client->dmxdev.capabilities = 0;
1117
1118 rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
Michael Krufky82237412008-06-15 15:14:13 -03001119 if (rc < 0) {
Michael Krufkya0c0abc2008-06-19 20:35:21 -03001120 sms_err("dvb_dmxdev_init failed %d", rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001121 goto dmxdev_error;
1122 }
1123
Michael Krufkyfa830e82008-06-15 15:52:43 -03001124 /* init and register frontend */
Michael Krufky82237412008-06-15 15:14:13 -03001125 memcpy(&client->frontend.ops, &smsdvb_fe_ops,
1126 sizeof(struct dvb_frontend_ops));
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001127
Mauro Carvalho Chehab15115c12011-12-26 16:31:29 -03001128 switch (smscore_get_device_mode(coredev)) {
1129 case DEVICE_MODE_DVBT:
1130 case DEVICE_MODE_DVBT_BDA:
Mauro Carvalho Chehab9bd58e72012-03-01 07:25:39 -03001131 client->frontend.ops.delsys[0] = SYS_DVBT;
Mauro Carvalho Chehab15115c12011-12-26 16:31:29 -03001132 break;
1133 case DEVICE_MODE_ISDBT:
1134 case DEVICE_MODE_ISDBT_BDA:
Mauro Carvalho Chehab9bd58e72012-03-01 07:25:39 -03001135 client->frontend.ops.delsys[0] = SYS_ISDBT;
Mauro Carvalho Chehab15115c12011-12-26 16:31:29 -03001136 break;
1137 }
1138
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001139 rc = dvb_register_frontend(&client->adapter, &client->frontend);
Michael Krufky82237412008-06-15 15:14:13 -03001140 if (rc < 0) {
Michael Krufkya0c0abc2008-06-19 20:35:21 -03001141 sms_err("frontend registration failed %d", rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001142 goto frontend_error;
1143 }
1144
Michael Krufkyf17407a2008-06-14 00:43:26 -03001145 params.initial_id = 1;
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001146 params.data_type = MSG_SMS_DVBT_BDA_DATA;
1147 params.onresponse_handler = smsdvb_onresponse;
1148 params.onremove_handler = smsdvb_onremove;
1149 params.context = client;
1150
1151 rc = smscore_register_client(coredev, &params, &client->smsclient);
Michael Krufky82237412008-06-15 15:14:13 -03001152 if (rc < 0) {
Michael Krufkyeb250942008-06-19 22:07:23 -03001153 sms_err("smscore_register_client() failed %d", rc);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001154 goto client_error;
1155 }
1156
1157 client->coredev = coredev;
1158
1159 init_completion(&client->tune_done);
Mauro Carvalho Chehab76e41a62013-03-07 16:32:33 -03001160 init_completion(&client->stats_done);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001161
1162 kmutex_lock(&g_smsdvb_clientslock);
1163
1164 list_add(&client->entry, &g_smsdvb_clients);
1165
1166 kmutex_unlock(&g_smsdvb_clientslock);
1167
Uri Shkolnik793786d2009-05-12 12:28:46 -03001168 client->event_fe_state = -1;
1169 client->event_unc_state = -1;
1170 sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001171
Uri Shkolnik793786d2009-05-12 12:28:46 -03001172 sms_info("success");
Michael Krufky250fa672008-11-16 22:45:42 -03001173 sms_board_setup(coredev);
1174
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -03001175 if (smsdvb_debugfs_create(client) < 0)
Mauro Carvalho Chehab3f6b87c2013-03-09 22:33:06 -03001176 sms_info("failed to create debugfs node");
1177
Michael Krufky2e5c1ec82008-05-19 18:56:13 -03001178 return 0;
1179
1180client_error:
1181 dvb_unregister_frontend(&client->frontend);
1182
1183frontend_error:
1184 dvb_dmxdev_release(&client->dmxdev);
1185
1186dmxdev_error:
1187 dvb_dmx_release(&client->demux);
1188
1189dvbdmx_error:
1190 dvb_unregister_adapter(&client->adapter);
1191
1192adapter_error:
1193 kfree(client);
1194 return rc;
1195}
1196
Márton Németh2184dda2009-12-11 20:05:10 -03001197static int __init smsdvb_module_init(void)
Steven Totheae55662008-05-22 18:04:36 -03001198{
1199 int rc;
1200
1201 INIT_LIST_HEAD(&g_smsdvb_clients);
1202 kmutex_init(&g_smsdvb_clientslock);
1203
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -03001204 smsdvb_debugfs_register();
Mauro Carvalho Chehab3f6b87c2013-03-09 22:33:06 -03001205
Steven Totheae55662008-05-22 18:04:36 -03001206 rc = smscore_register_hotplug(smsdvb_hotplug);
1207
Michael Krufkya0c0abc2008-06-19 20:35:21 -03001208 sms_debug("");
Steven Totheae55662008-05-22 18:04:36 -03001209
1210 return rc;
1211}
1212
Márton Németh2184dda2009-12-11 20:05:10 -03001213static void __exit smsdvb_module_exit(void)
Steven Totheae55662008-05-22 18:04:36 -03001214{
1215 smscore_unregister_hotplug(smsdvb_hotplug);
1216
1217 kmutex_lock(&g_smsdvb_clientslock);
1218
1219 while (!list_empty(&g_smsdvb_clients))
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -03001220 smsdvb_unregister_client((struct smsdvb_client_t *)g_smsdvb_clients.next);
Steven Totheae55662008-05-22 18:04:36 -03001221
Mauro Carvalho Chehab503efe52013-03-10 09:04:44 -03001222 smsdvb_debugfs_unregister();
Mauro Carvalho Chehab3f6b87c2013-03-09 22:33:06 -03001223
Steven Totheae55662008-05-22 18:04:36 -03001224 kmutex_unlock(&g_smsdvb_clientslock);
Steven Totheae55662008-05-22 18:04:36 -03001225}
Uri Shkolnike0f14c22008-08-31 00:44:04 -03001226
1227module_init(smsdvb_module_init);
1228module_exit(smsdvb_module_exit);
1229
1230MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
Uri Shkolnik843d0602009-05-12 13:13:13 -03001231MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
Uri Shkolnike0f14c22008-08-31 00:44:04 -03001232MODULE_LICENSE("GPL");