1 /****************************************************************
3 Siano Mobile Silicon, Inc.
4 MDTV receiver kernel modules.
5 Copyright (C) 2006-2008, Uri Shkolnik
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ****************************************************************/
22 #include "smscoreapi.h"
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <asm/div64.h>
31 #include "dvb_demux.h"
32 #include "dvb_frontend.h"
34 #include "sms-cards.h"
38 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
40 static struct list_head g_smsdvb_clients
;
41 static struct mutex g_smsdvb_clientslock
;
44 module_param_named(debug
, sms_dbg
, int, 0644);
45 MODULE_PARM_DESC(debug
, "set debug level (info=1, adv=2 (or-able))");
48 static u32 sms_to_guard_interval_table
[] = {
49 [0] = GUARD_INTERVAL_1_32
,
50 [1] = GUARD_INTERVAL_1_16
,
51 [2] = GUARD_INTERVAL_1_8
,
52 [3] = GUARD_INTERVAL_1_4
,
55 static u32 sms_to_code_rate_table
[] = {
64 static u32 sms_to_hierarchy_table
[] = {
71 static u32 sms_to_modulation_table
[] = {
79 /* Events that may come from DVB v3 adapter */
80 static void sms_board_dvb3_event(struct smsdvb_client_t
*client
,
81 enum SMS_DVB3_EVENTS event
) {
83 struct smscore_device_t
*coredev
= client
->coredev
;
86 sms_debug("DVB3_EVENT_INIT");
87 sms_board_event(coredev
, BOARD_EVENT_BIND
);
89 case DVB3_EVENT_SLEEP
:
90 sms_debug("DVB3_EVENT_SLEEP");
91 sms_board_event(coredev
, BOARD_EVENT_POWER_SUSPEND
);
93 case DVB3_EVENT_HOTPLUG
:
94 sms_debug("DVB3_EVENT_HOTPLUG");
95 sms_board_event(coredev
, BOARD_EVENT_POWER_INIT
);
97 case DVB3_EVENT_FE_LOCK
:
98 if (client
->event_fe_state
!= DVB3_EVENT_FE_LOCK
) {
99 client
->event_fe_state
= DVB3_EVENT_FE_LOCK
;
100 sms_debug("DVB3_EVENT_FE_LOCK");
101 sms_board_event(coredev
, BOARD_EVENT_FE_LOCK
);
104 case DVB3_EVENT_FE_UNLOCK
:
105 if (client
->event_fe_state
!= DVB3_EVENT_FE_UNLOCK
) {
106 client
->event_fe_state
= DVB3_EVENT_FE_UNLOCK
;
107 sms_debug("DVB3_EVENT_FE_UNLOCK");
108 sms_board_event(coredev
, BOARD_EVENT_FE_UNLOCK
);
111 case DVB3_EVENT_UNC_OK
:
112 if (client
->event_unc_state
!= DVB3_EVENT_UNC_OK
) {
113 client
->event_unc_state
= DVB3_EVENT_UNC_OK
;
114 sms_debug("DVB3_EVENT_UNC_OK");
115 sms_board_event(coredev
, BOARD_EVENT_MULTIPLEX_OK
);
118 case DVB3_EVENT_UNC_ERR
:
119 if (client
->event_unc_state
!= DVB3_EVENT_UNC_ERR
) {
120 client
->event_unc_state
= DVB3_EVENT_UNC_ERR
;
121 sms_debug("DVB3_EVENT_UNC_ERR");
122 sms_board_event(coredev
, BOARD_EVENT_MULTIPLEX_ERRORS
);
127 pr_err("Unknown dvb3 api event\n");
132 static void smsdvb_stats_not_ready(struct dvb_frontend
*fe
)
134 struct smsdvb_client_t
*client
=
135 container_of(fe
, struct smsdvb_client_t
, frontend
);
136 struct smscore_device_t
*coredev
= client
->coredev
;
137 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
140 switch (smscore_get_device_mode(coredev
)) {
141 case DEVICE_MODE_ISDBT
:
142 case DEVICE_MODE_ISDBT_BDA
:
152 c
->strength
.stat
[0].scale
= FE_SCALE_DECIBEL
;
153 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
155 /* Per-layer stats */
156 c
->post_bit_error
.len
= n_layers
;
157 c
->post_bit_count
.len
= n_layers
;
158 c
->block_error
.len
= n_layers
;
159 c
->block_count
.len
= n_layers
;
162 * Put all of them at FE_SCALE_NOT_AVAILABLE. They're dynamically
163 * changed when the stats become available.
165 for (i
= 0; i
< n_layers
; i
++) {
166 c
->post_bit_error
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
167 c
->post_bit_count
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
168 c
->block_error
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
169 c
->block_count
.stat
[i
].scale
= FE_SCALE_NOT_AVAILABLE
;
173 static inline int sms_to_mode(u32 mode
)
177 return TRANSMISSION_MODE_2K
;
179 return TRANSMISSION_MODE_4K
;
181 return TRANSMISSION_MODE_8K
;
183 return TRANSMISSION_MODE_AUTO
;
186 static inline int sms_to_status(u32 is_demod_locked
, u32 is_rf_locked
)
189 return FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
190 FE_HAS_SYNC
| FE_HAS_LOCK
;
193 return FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
198 static inline u32
sms_to_bw(u32 value
)
200 return value
* 1000000;
203 #define convert_from_table(value, table, defval) ({ \
205 if (value < ARRAY_SIZE(table)) \
206 __ret = table[value]; \
212 #define sms_to_guard_interval(value) \
213 convert_from_table(value, sms_to_guard_interval_table, \
214 GUARD_INTERVAL_AUTO);
216 #define sms_to_code_rate(value) \
217 convert_from_table(value, sms_to_code_rate_table, \
220 #define sms_to_hierarchy(value) \
221 convert_from_table(value, sms_to_hierarchy_table, \
224 #define sms_to_modulation(value) \
225 convert_from_table(value, sms_to_modulation_table, \
228 static void smsdvb_update_tx_params(struct smsdvb_client_t
*client
,
229 struct sms_tx_stats
*p
)
231 struct dvb_frontend
*fe
= &client
->frontend
;
232 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
234 c
->frequency
= p
->frequency
;
235 client
->fe_status
= sms_to_status(p
->is_demod_locked
, 0);
236 c
->bandwidth_hz
= sms_to_bw(p
->bandwidth
);
237 c
->transmission_mode
= sms_to_mode(p
->transmission_mode
);
238 c
->guard_interval
= sms_to_guard_interval(p
->guard_interval
);
239 c
->code_rate_HP
= sms_to_code_rate(p
->code_rate
);
240 c
->code_rate_LP
= sms_to_code_rate(p
->lp_code_rate
);
241 c
->hierarchy
= sms_to_hierarchy(p
->hierarchy
);
242 c
->modulation
= sms_to_modulation(p
->constellation
);
245 static void smsdvb_update_per_slices(struct smsdvb_client_t
*client
,
246 struct RECEPTION_STATISTICS_PER_SLICES_S
*p
)
248 struct dvb_frontend
*fe
= &client
->frontend
;
249 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
252 client
->fe_status
= sms_to_status(p
->is_demod_locked
, p
->is_rf_locked
);
253 c
->modulation
= sms_to_modulation(p
->constellation
);
255 /* signal Strength, in DBm */
256 c
->strength
.stat
[0].uvalue
= p
->in_band_power
* 1000;
258 /* Carrier to noise ratio, in DB */
259 c
->cnr
.stat
[0].svalue
= p
->snr
* 1000;
261 /* PER/BER requires demod lock */
262 if (!p
->is_demod_locked
)
266 client
->last_per
= c
->block_error
.stat
[0].uvalue
;
267 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
268 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
269 c
->block_error
.stat
[0].uvalue
+= p
->ets_packets
;
270 c
->block_count
.stat
[0].uvalue
+= p
->ets_packets
+ p
->ts_packets
;
273 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
274 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
275 c
->post_bit_error
.stat
[0].uvalue
+= p
->ber_error_count
;
276 c
->post_bit_count
.stat
[0].uvalue
+= p
->ber_bit_count
;
279 tmp
= p
->ets_packets
* 65535;
280 if (p
->ts_packets
+ p
->ets_packets
)
281 do_div(tmp
, p
->ts_packets
+ p
->ets_packets
);
282 client
->legacy_per
= tmp
;
285 static void smsdvb_update_dvb_stats(struct smsdvb_client_t
*client
,
288 struct dvb_frontend
*fe
= &client
->frontend
;
289 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
291 if (client
->prt_dvb_stats
)
292 client
->prt_dvb_stats(client
->debug_data
, p
);
294 client
->fe_status
= sms_to_status(p
->is_demod_locked
, p
->is_rf_locked
);
296 /* Update DVB modulation parameters */
297 c
->frequency
= p
->frequency
;
298 client
->fe_status
= sms_to_status(p
->is_demod_locked
, 0);
299 c
->bandwidth_hz
= sms_to_bw(p
->bandwidth
);
300 c
->transmission_mode
= sms_to_mode(p
->transmission_mode
);
301 c
->guard_interval
= sms_to_guard_interval(p
->guard_interval
);
302 c
->code_rate_HP
= sms_to_code_rate(p
->code_rate
);
303 c
->code_rate_LP
= sms_to_code_rate(p
->lp_code_rate
);
304 c
->hierarchy
= sms_to_hierarchy(p
->hierarchy
);
305 c
->modulation
= sms_to_modulation(p
->constellation
);
307 /* update reception data */
308 c
->lna
= p
->is_external_lna_on
? 1 : 0;
310 /* Carrier to noise ratio, in DB */
311 c
->cnr
.stat
[0].svalue
= p
->SNR
* 1000;
313 /* signal Strength, in DBm */
314 c
->strength
.stat
[0].uvalue
= p
->in_band_pwr
* 1000;
316 /* PER/BER requires demod lock */
317 if (!p
->is_demod_locked
)
321 client
->last_per
= c
->block_error
.stat
[0].uvalue
;
322 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
323 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
324 c
->block_error
.stat
[0].uvalue
+= p
->error_ts_packets
;
325 c
->block_count
.stat
[0].uvalue
+= p
->total_ts_packets
;
328 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
329 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
330 c
->post_bit_error
.stat
[0].uvalue
+= p
->ber_error_count
;
331 c
->post_bit_count
.stat
[0].uvalue
+= p
->ber_bit_count
;
334 client
->legacy_ber
= p
->ber
;
337 static void smsdvb_update_isdbt_stats(struct smsdvb_client_t
*client
,
338 struct sms_isdbt_stats
*p
)
340 struct dvb_frontend
*fe
= &client
->frontend
;
341 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
342 struct sms_isdbt_layer_stats
*lr
;
345 if (client
->prt_isdb_stats
)
346 client
->prt_isdb_stats(client
->debug_data
, p
);
348 client
->fe_status
= sms_to_status(p
->is_demod_locked
, p
->is_rf_locked
);
351 * Firmware 2.1 seems to report only lock status and
352 * signal strength. The signal strength indicator is at the
355 if (p
->statistics_type
== 0) {
356 c
->strength
.stat
[0].uvalue
= ((s32
)p
->transmission_mode
) * 1000;
357 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
361 /* Update ISDB-T transmission parameters */
362 c
->frequency
= p
->frequency
;
363 c
->bandwidth_hz
= sms_to_bw(p
->bandwidth
);
364 c
->transmission_mode
= sms_to_mode(p
->transmission_mode
);
365 c
->guard_interval
= sms_to_guard_interval(p
->guard_interval
);
366 c
->isdbt_partial_reception
= p
->partial_reception
? 1 : 0;
367 n_layers
= p
->num_of_layers
;
372 c
->isdbt_layer_enabled
= 0;
374 /* update reception data */
375 c
->lna
= p
->is_external_lna_on
? 1 : 0;
377 /* Carrier to noise ratio, in DB */
378 c
->cnr
.stat
[0].svalue
= p
->SNR
* 1000;
380 /* signal Strength, in DBm */
381 c
->strength
.stat
[0].uvalue
= p
->in_band_pwr
* 1000;
383 /* PER/BER and per-layer stats require demod lock */
384 if (!p
->is_demod_locked
)
387 client
->last_per
= c
->block_error
.stat
[0].uvalue
;
389 /* Clears global counters, as the code below will sum it again */
390 c
->block_error
.stat
[0].uvalue
= 0;
391 c
->block_count
.stat
[0].uvalue
= 0;
392 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
393 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
394 c
->post_bit_error
.stat
[0].uvalue
= 0;
395 c
->post_bit_count
.stat
[0].uvalue
= 0;
396 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
397 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
399 for (i
= 0; i
< n_layers
; i
++) {
400 lr
= &p
->layer_info
[i
];
402 /* Update per-layer transmission parameters */
403 if (lr
->number_of_segments
> 0 && lr
->number_of_segments
< 13) {
404 c
->isdbt_layer_enabled
|= 1 << i
;
405 c
->layer
[i
].segment_count
= lr
->number_of_segments
;
409 c
->layer
[i
].modulation
= sms_to_modulation(lr
->constellation
);
412 c
->block_error
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
413 c
->block_count
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
414 c
->block_error
.stat
[i
+ 1].uvalue
+= lr
->error_ts_packets
;
415 c
->block_count
.stat
[i
+ 1].uvalue
+= lr
->total_ts_packets
;
417 /* Update global PER counter */
418 c
->block_error
.stat
[0].uvalue
+= lr
->error_ts_packets
;
419 c
->block_count
.stat
[0].uvalue
+= lr
->total_ts_packets
;
422 c
->post_bit_error
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
423 c
->post_bit_count
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
424 c
->post_bit_error
.stat
[i
+ 1].uvalue
+= lr
->ber_error_count
;
425 c
->post_bit_count
.stat
[i
+ 1].uvalue
+= lr
->ber_bit_count
;
427 /* Update global BER counter */
428 c
->post_bit_error
.stat
[0].uvalue
+= lr
->ber_error_count
;
429 c
->post_bit_count
.stat
[0].uvalue
+= lr
->ber_bit_count
;
433 static void smsdvb_update_isdbt_stats_ex(struct smsdvb_client_t
*client
,
434 struct sms_isdbt_stats_ex
*p
)
436 struct dvb_frontend
*fe
= &client
->frontend
;
437 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
438 struct sms_isdbt_layer_stats
*lr
;
441 if (client
->prt_isdb_stats_ex
)
442 client
->prt_isdb_stats_ex(client
->debug_data
, p
);
444 /* Update ISDB-T transmission parameters */
445 c
->frequency
= p
->frequency
;
446 client
->fe_status
= sms_to_status(p
->is_demod_locked
, 0);
447 c
->bandwidth_hz
= sms_to_bw(p
->bandwidth
);
448 c
->transmission_mode
= sms_to_mode(p
->transmission_mode
);
449 c
->guard_interval
= sms_to_guard_interval(p
->guard_interval
);
450 c
->isdbt_partial_reception
= p
->partial_reception
? 1 : 0;
451 n_layers
= p
->num_of_layers
;
456 c
->isdbt_layer_enabled
= 0;
458 /* update reception data */
459 c
->lna
= p
->is_external_lna_on
? 1 : 0;
461 /* Carrier to noise ratio, in DB */
462 c
->cnr
.stat
[0].svalue
= p
->SNR
* 1000;
464 /* signal Strength, in DBm */
465 c
->strength
.stat
[0].uvalue
= p
->in_band_pwr
* 1000;
467 /* PER/BER and per-layer stats require demod lock */
468 if (!p
->is_demod_locked
)
471 client
->last_per
= c
->block_error
.stat
[0].uvalue
;
473 /* Clears global counters, as the code below will sum it again */
474 c
->block_error
.stat
[0].uvalue
= 0;
475 c
->block_count
.stat
[0].uvalue
= 0;
476 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
477 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
478 c
->post_bit_error
.stat
[0].uvalue
= 0;
479 c
->post_bit_count
.stat
[0].uvalue
= 0;
480 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
481 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
483 c
->post_bit_error
.len
= n_layers
+ 1;
484 c
->post_bit_count
.len
= n_layers
+ 1;
485 c
->block_error
.len
= n_layers
+ 1;
486 c
->block_count
.len
= n_layers
+ 1;
487 for (i
= 0; i
< n_layers
; i
++) {
488 lr
= &p
->layer_info
[i
];
490 /* Update per-layer transmission parameters */
491 if (lr
->number_of_segments
> 0 && lr
->number_of_segments
< 13) {
492 c
->isdbt_layer_enabled
|= 1 << i
;
493 c
->layer
[i
].segment_count
= lr
->number_of_segments
;
497 c
->layer
[i
].modulation
= sms_to_modulation(lr
->constellation
);
500 c
->block_error
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
501 c
->block_count
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
502 c
->block_error
.stat
[i
+ 1].uvalue
+= lr
->error_ts_packets
;
503 c
->block_count
.stat
[i
+ 1].uvalue
+= lr
->total_ts_packets
;
505 /* Update global PER counter */
506 c
->block_error
.stat
[0].uvalue
+= lr
->error_ts_packets
;
507 c
->block_count
.stat
[0].uvalue
+= lr
->total_ts_packets
;
510 c
->post_bit_error
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
511 c
->post_bit_count
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
512 c
->post_bit_error
.stat
[i
+ 1].uvalue
+= lr
->ber_error_count
;
513 c
->post_bit_count
.stat
[i
+ 1].uvalue
+= lr
->ber_bit_count
;
515 /* Update global ber counter */
516 c
->post_bit_error
.stat
[0].uvalue
+= lr
->ber_error_count
;
517 c
->post_bit_count
.stat
[0].uvalue
+= lr
->ber_bit_count
;
521 static int smsdvb_onresponse(void *context
, struct smscore_buffer_t
*cb
)
523 struct smsdvb_client_t
*client
= (struct smsdvb_client_t
*) context
;
524 struct sms_msg_hdr
*phdr
= (struct sms_msg_hdr
*) (((u8
*) cb
->p
)
527 struct dvb_frontend
*fe
= &client
->frontend
;
528 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
529 bool is_status_update
= false;
531 switch (phdr
->msg_type
) {
532 case MSG_SMS_DVBT_BDA_DATA
:
534 * Only feed data to dvb demux if are there any feed listening
535 * to it and if the device has tuned
537 if (client
->feed_users
&& client
->has_tuned
)
538 dvb_dmx_swfilter(&client
->demux
, p
,
539 cb
->size
- sizeof(struct sms_msg_hdr
));
542 case MSG_SMS_RF_TUNE_RES
:
543 case MSG_SMS_ISDBT_TUNE_RES
:
544 complete(&client
->tune_done
);
547 case MSG_SMS_SIGNAL_DETECTED_IND
:
548 client
->fe_status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
549 FE_HAS_VITERBI
| FE_HAS_SYNC
|
552 is_status_update
= true;
555 case MSG_SMS_NO_SIGNAL_IND
:
556 client
->fe_status
= 0;
558 is_status_update
= true;
561 case MSG_SMS_TRANSMISSION_IND
:
562 smsdvb_update_tx_params(client
, p
);
564 is_status_update
= true;
567 case MSG_SMS_HO_PER_SLICES_IND
:
568 smsdvb_update_per_slices(client
, p
);
570 is_status_update
= true;
573 case MSG_SMS_GET_STATISTICS_RES
:
574 switch (smscore_get_device_mode(client
->coredev
)) {
575 case DEVICE_MODE_ISDBT
:
576 case DEVICE_MODE_ISDBT_BDA
:
577 smsdvb_update_isdbt_stats(client
, p
);
580 /* Skip sms_msg_statistics_info:request_result field */
581 smsdvb_update_dvb_stats(client
, p
+ sizeof(u32
));
584 is_status_update
= true;
587 /* Only for ISDB-T */
588 case MSG_SMS_GET_STATISTICS_EX_RES
:
589 /* Skip sms_msg_statistics_info:request_result field? */
590 smsdvb_update_isdbt_stats_ex(client
, p
+ sizeof(u32
));
591 is_status_update
= true;
594 sms_info("message not handled");
596 smscore_putbuffer(client
->coredev
, cb
);
598 if (is_status_update
) {
599 if (client
->fe_status
& FE_HAS_LOCK
) {
600 sms_board_dvb3_event(client
, DVB3_EVENT_FE_LOCK
);
601 if (client
->last_per
== c
->block_error
.stat
[0].uvalue
)
602 sms_board_dvb3_event(client
, DVB3_EVENT_UNC_OK
);
604 sms_board_dvb3_event(client
, DVB3_EVENT_UNC_ERR
);
605 client
->has_tuned
= true;
607 smsdvb_stats_not_ready(fe
);
608 client
->has_tuned
= false;
609 sms_board_dvb3_event(client
, DVB3_EVENT_FE_UNLOCK
);
611 complete(&client
->stats_done
);
617 static void smsdvb_media_device_unregister(struct smsdvb_client_t
*client
)
619 struct smscore_device_t
*coredev
= client
->coredev
;
621 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
622 if (!coredev
->media_dev
)
624 media_device_unregister(coredev
->media_dev
);
625 kfree(coredev
->media_dev
);
626 coredev
->media_dev
= NULL
;
630 static void smsdvb_unregister_client(struct smsdvb_client_t
*client
)
632 /* must be called under clientslock */
634 list_del(&client
->entry
);
636 smsdvb_debugfs_release(client
);
637 smscore_unregister_client(client
->smsclient
);
638 dvb_unregister_frontend(&client
->frontend
);
639 dvb_dmxdev_release(&client
->dmxdev
);
640 dvb_dmx_release(&client
->demux
);
641 smsdvb_media_device_unregister(client
);
642 dvb_unregister_adapter(&client
->adapter
);
646 static void smsdvb_onremove(void *context
)
648 kmutex_lock(&g_smsdvb_clientslock
);
650 smsdvb_unregister_client((struct smsdvb_client_t
*) context
);
652 kmutex_unlock(&g_smsdvb_clientslock
);
655 static int smsdvb_start_feed(struct dvb_demux_feed
*feed
)
657 struct smsdvb_client_t
*client
=
658 container_of(feed
->demux
, struct smsdvb_client_t
, demux
);
659 struct sms_msg_data pid_msg
;
661 sms_debug("add pid %d(%x)",
662 feed
->pid
, feed
->pid
);
664 client
->feed_users
++;
666 pid_msg
.x_msg_header
.msg_src_id
= DVBT_BDA_CONTROL_MSG_ID
;
667 pid_msg
.x_msg_header
.msg_dst_id
= HIF_TASK
;
668 pid_msg
.x_msg_header
.msg_flags
= 0;
669 pid_msg
.x_msg_header
.msg_type
= MSG_SMS_ADD_PID_FILTER_REQ
;
670 pid_msg
.x_msg_header
.msg_length
= sizeof(pid_msg
);
671 pid_msg
.msg_data
[0] = feed
->pid
;
673 return smsclient_sendrequest(client
->smsclient
,
674 &pid_msg
, sizeof(pid_msg
));
677 static int smsdvb_stop_feed(struct dvb_demux_feed
*feed
)
679 struct smsdvb_client_t
*client
=
680 container_of(feed
->demux
, struct smsdvb_client_t
, demux
);
681 struct sms_msg_data pid_msg
;
683 sms_debug("remove pid %d(%x)",
684 feed
->pid
, feed
->pid
);
686 client
->feed_users
--;
688 pid_msg
.x_msg_header
.msg_src_id
= DVBT_BDA_CONTROL_MSG_ID
;
689 pid_msg
.x_msg_header
.msg_dst_id
= HIF_TASK
;
690 pid_msg
.x_msg_header
.msg_flags
= 0;
691 pid_msg
.x_msg_header
.msg_type
= MSG_SMS_REMOVE_PID_FILTER_REQ
;
692 pid_msg
.x_msg_header
.msg_length
= sizeof(pid_msg
);
693 pid_msg
.msg_data
[0] = feed
->pid
;
695 return smsclient_sendrequest(client
->smsclient
,
696 &pid_msg
, sizeof(pid_msg
));
699 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t
*client
,
700 void *buffer
, size_t size
,
701 struct completion
*completion
)
705 rc
= smsclient_sendrequest(client
->smsclient
, buffer
, size
);
709 return wait_for_completion_timeout(completion
,
710 msecs_to_jiffies(2000)) ?
714 static int smsdvb_send_statistics_request(struct smsdvb_client_t
*client
)
717 struct sms_msg_hdr msg
;
719 /* Don't request stats too fast */
720 if (client
->get_stats_jiffies
&&
721 (!time_after(jiffies
, client
->get_stats_jiffies
)))
723 client
->get_stats_jiffies
= jiffies
+ msecs_to_jiffies(100);
725 msg
.msg_src_id
= DVBT_BDA_CONTROL_MSG_ID
;
726 msg
.msg_dst_id
= HIF_TASK
;
728 msg
.msg_length
= sizeof(msg
);
730 switch (smscore_get_device_mode(client
->coredev
)) {
731 case DEVICE_MODE_ISDBT
:
732 case DEVICE_MODE_ISDBT_BDA
:
734 * Check for firmware version, to avoid breaking for old cards
736 if (client
->coredev
->fw_version
>= 0x800)
737 msg
.msg_type
= MSG_SMS_GET_STATISTICS_EX_REQ
;
739 msg
.msg_type
= MSG_SMS_GET_STATISTICS_REQ
;
742 msg
.msg_type
= MSG_SMS_GET_STATISTICS_REQ
;
745 rc
= smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
746 &client
->stats_done
);
751 static inline int led_feedback(struct smsdvb_client_t
*client
)
753 if (!(client
->fe_status
& FE_HAS_LOCK
))
754 return sms_board_led_feedback(client
->coredev
, SMS_LED_OFF
);
756 return sms_board_led_feedback(client
->coredev
,
757 (client
->legacy_ber
== 0) ?
758 SMS_LED_HI
: SMS_LED_LO
);
761 static int smsdvb_read_status(struct dvb_frontend
*fe
, fe_status_t
*stat
)
764 struct smsdvb_client_t
*client
;
765 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
767 rc
= smsdvb_send_statistics_request(client
);
769 *stat
= client
->fe_status
;
771 led_feedback(client
);
776 static int smsdvb_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
779 struct smsdvb_client_t
*client
;
781 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
783 rc
= smsdvb_send_statistics_request(client
);
785 *ber
= client
->legacy_ber
;
787 led_feedback(client
);
792 static int smsdvb_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
794 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
796 s32 power
= (s32
) c
->strength
.stat
[0].uvalue
;
797 struct smsdvb_client_t
*client
;
799 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
801 rc
= smsdvb_send_statistics_request(client
);
805 else if (power
> -29)
808 *strength
= (power
+ 95) * 65535 / 66;
810 led_feedback(client
);
815 static int smsdvb_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
817 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
819 struct smsdvb_client_t
*client
;
821 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
823 rc
= smsdvb_send_statistics_request(client
);
825 /* Preferred scale for SNR with legacy API: 0.1 dB */
826 *snr
= ((u32
)c
->cnr
.stat
[0].svalue
) / 100;
828 led_feedback(client
);
833 static int smsdvb_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
836 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
837 struct smsdvb_client_t
*client
;
839 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
841 rc
= smsdvb_send_statistics_request(client
);
843 *ucblocks
= c
->block_error
.stat
[0].uvalue
;
845 led_feedback(client
);
850 static int smsdvb_get_tune_settings(struct dvb_frontend
*fe
,
851 struct dvb_frontend_tune_settings
*tune
)
855 tune
->min_delay_ms
= 400;
856 tune
->step_size
= 250000;
861 static int smsdvb_dvbt_set_frontend(struct dvb_frontend
*fe
)
863 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
864 struct smsdvb_client_t
*client
=
865 container_of(fe
, struct smsdvb_client_t
, frontend
);
868 struct sms_msg_hdr msg
;
874 client
->fe_status
= 0;
875 client
->event_fe_state
= -1;
876 client
->event_unc_state
= -1;
877 fe
->dtv_property_cache
.delivery_system
= SYS_DVBT
;
879 msg
.msg
.msg_src_id
= DVBT_BDA_CONTROL_MSG_ID
;
880 msg
.msg
.msg_dst_id
= HIF_TASK
;
881 msg
.msg
.msg_flags
= 0;
882 msg
.msg
.msg_type
= MSG_SMS_RF_TUNE_REQ
;
883 msg
.msg
.msg_length
= sizeof(msg
);
884 msg
.Data
[0] = c
->frequency
;
885 msg
.Data
[2] = 12000000;
887 sms_info("%s: freq %d band %d", __func__
, c
->frequency
,
890 switch (c
->bandwidth_hz
/ 1000000) {
892 msg
.Data
[1] = BW_8_MHZ
;
895 msg
.Data
[1] = BW_7_MHZ
;
898 msg
.Data
[1] = BW_6_MHZ
;
905 /* Disable LNA, if any. An error is returned if no LNA is present */
906 ret
= sms_board_lna_control(client
->coredev
, 0);
910 /* tune with LNA off at first */
911 ret
= smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
914 smsdvb_read_status(fe
, &status
);
916 if (status
& FE_HAS_LOCK
)
919 /* previous tune didn't lock - enable LNA and tune again */
920 sms_board_lna_control(client
->coredev
, 1);
923 return smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
927 static int smsdvb_isdbt_set_frontend(struct dvb_frontend
*fe
)
929 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
930 struct smsdvb_client_t
*client
=
931 container_of(fe
, struct smsdvb_client_t
, frontend
);
932 int board_id
= smscore_get_board_id(client
->coredev
);
933 struct sms_board
*board
= sms_get_board(board_id
);
934 enum sms_device_type_st type
= board
->type
;
938 struct sms_msg_hdr msg
;
942 fe
->dtv_property_cache
.delivery_system
= SYS_ISDBT
;
944 msg
.msg
.msg_src_id
= DVBT_BDA_CONTROL_MSG_ID
;
945 msg
.msg
.msg_dst_id
= HIF_TASK
;
946 msg
.msg
.msg_flags
= 0;
947 msg
.msg
.msg_type
= MSG_SMS_ISDBT_TUNE_REQ
;
948 msg
.msg
.msg_length
= sizeof(msg
);
950 if (c
->isdbt_sb_segment_idx
== -1)
951 c
->isdbt_sb_segment_idx
= 0;
953 if (!c
->isdbt_layer_enabled
)
954 c
->isdbt_layer_enabled
= 7;
956 msg
.Data
[0] = c
->frequency
;
957 msg
.Data
[1] = BW_ISDBT_1SEG
;
958 msg
.Data
[2] = 12000000;
959 msg
.Data
[3] = c
->isdbt_sb_segment_idx
;
961 if (c
->isdbt_partial_reception
) {
962 if ((type
== SMS_PELE
|| type
== SMS_RIO
) &&
963 c
->isdbt_sb_segment_count
> 3)
964 msg
.Data
[1] = BW_ISDBT_13SEG
;
965 else if (c
->isdbt_sb_segment_count
> 1)
966 msg
.Data
[1] = BW_ISDBT_3SEG
;
967 } else if (type
== SMS_PELE
|| type
== SMS_RIO
)
968 msg
.Data
[1] = BW_ISDBT_13SEG
;
970 c
->bandwidth_hz
= 6000000;
972 sms_info("%s: freq %d segwidth %d segindex %d", __func__
,
973 c
->frequency
, c
->isdbt_sb_segment_count
,
974 c
->isdbt_sb_segment_idx
);
976 /* Disable LNA, if any. An error is returned if no LNA is present */
977 ret
= sms_board_lna_control(client
->coredev
, 0);
981 /* tune with LNA off at first */
982 ret
= smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
985 smsdvb_read_status(fe
, &status
);
987 if (status
& FE_HAS_LOCK
)
990 /* previous tune didn't lock - enable LNA and tune again */
991 sms_board_lna_control(client
->coredev
, 1);
993 return smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
997 static int smsdvb_set_frontend(struct dvb_frontend
*fe
)
999 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
1000 struct smsdvb_client_t
*client
=
1001 container_of(fe
, struct smsdvb_client_t
, frontend
);
1002 struct smscore_device_t
*coredev
= client
->coredev
;
1004 smsdvb_stats_not_ready(fe
);
1005 c
->strength
.stat
[0].uvalue
= 0;
1006 c
->cnr
.stat
[0].uvalue
= 0;
1008 client
->has_tuned
= false;
1010 switch (smscore_get_device_mode(coredev
)) {
1011 case DEVICE_MODE_DVBT
:
1012 case DEVICE_MODE_DVBT_BDA
:
1013 return smsdvb_dvbt_set_frontend(fe
);
1014 case DEVICE_MODE_ISDBT
:
1015 case DEVICE_MODE_ISDBT_BDA
:
1016 return smsdvb_isdbt_set_frontend(fe
);
1022 /* Nothing to do here, as stats are automatically updated */
1023 static int smsdvb_get_frontend(struct dvb_frontend
*fe
)
1028 static int smsdvb_init(struct dvb_frontend
*fe
)
1030 struct smsdvb_client_t
*client
=
1031 container_of(fe
, struct smsdvb_client_t
, frontend
);
1033 sms_board_power(client
->coredev
, 1);
1035 sms_board_dvb3_event(client
, DVB3_EVENT_INIT
);
1039 static int smsdvb_sleep(struct dvb_frontend
*fe
)
1041 struct smsdvb_client_t
*client
=
1042 container_of(fe
, struct smsdvb_client_t
, frontend
);
1044 sms_board_led_feedback(client
->coredev
, SMS_LED_OFF
);
1045 sms_board_power(client
->coredev
, 0);
1047 sms_board_dvb3_event(client
, DVB3_EVENT_SLEEP
);
1052 static void smsdvb_release(struct dvb_frontend
*fe
)
1057 static struct dvb_frontend_ops smsdvb_fe_ops
= {
1059 .name
= "Siano Mobile Digital MDTV Receiver",
1060 .frequency_min
= 44250000,
1061 .frequency_max
= 867250000,
1062 .frequency_stepsize
= 250000,
1063 .caps
= FE_CAN_INVERSION_AUTO
|
1064 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
1065 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
1066 FE_CAN_QPSK
| FE_CAN_QAM_16
| FE_CAN_QAM_64
|
1067 FE_CAN_QAM_AUTO
| FE_CAN_TRANSMISSION_MODE_AUTO
|
1068 FE_CAN_GUARD_INTERVAL_AUTO
|
1070 FE_CAN_HIERARCHY_AUTO
,
1073 .release
= smsdvb_release
,
1075 .set_frontend
= smsdvb_set_frontend
,
1076 .get_frontend
= smsdvb_get_frontend
,
1077 .get_tune_settings
= smsdvb_get_tune_settings
,
1079 .read_status
= smsdvb_read_status
,
1080 .read_ber
= smsdvb_read_ber
,
1081 .read_signal_strength
= smsdvb_read_signal_strength
,
1082 .read_snr
= smsdvb_read_snr
,
1083 .read_ucblocks
= smsdvb_read_ucblocks
,
1085 .init
= smsdvb_init
,
1086 .sleep
= smsdvb_sleep
,
1089 static int smsdvb_hotplug(struct smscore_device_t
*coredev
,
1090 struct device
*device
, int arrival
)
1092 struct smsclient_params_t params
;
1093 struct smsdvb_client_t
*client
;
1096 /* device removal handled by onremove callback */
1099 client
= kzalloc(sizeof(struct smsdvb_client_t
), GFP_KERNEL
);
1103 /* register dvb adapter */
1104 rc
= dvb_register_adapter(&client
->adapter
,
1106 smscore_get_board_id(coredev
))->name
,
1107 THIS_MODULE
, device
, adapter_nr
);
1109 pr_err("dvb_register_adapter() failed %d\n", rc
);
1112 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
1113 client
->adapter
.mdev
= coredev
->media_dev
;
1116 /* init dvb demux */
1117 client
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
;
1118 client
->demux
.filternum
= 32; /* todo: nova ??? */
1119 client
->demux
.feednum
= 32;
1120 client
->demux
.start_feed
= smsdvb_start_feed
;
1121 client
->demux
.stop_feed
= smsdvb_stop_feed
;
1123 rc
= dvb_dmx_init(&client
->demux
);
1125 pr_err("dvb_dmx_init failed %d\n", rc
);
1130 client
->dmxdev
.filternum
= 32;
1131 client
->dmxdev
.demux
= &client
->demux
.dmx
;
1132 client
->dmxdev
.capabilities
= 0;
1134 rc
= dvb_dmxdev_init(&client
->dmxdev
, &client
->adapter
);
1136 pr_err("dvb_dmxdev_init failed %d\n", rc
);
1140 /* init and register frontend */
1141 memcpy(&client
->frontend
.ops
, &smsdvb_fe_ops
,
1142 sizeof(struct dvb_frontend_ops
));
1144 switch (smscore_get_device_mode(coredev
)) {
1145 case DEVICE_MODE_DVBT
:
1146 case DEVICE_MODE_DVBT_BDA
:
1147 client
->frontend
.ops
.delsys
[0] = SYS_DVBT
;
1149 case DEVICE_MODE_ISDBT
:
1150 case DEVICE_MODE_ISDBT_BDA
:
1151 client
->frontend
.ops
.delsys
[0] = SYS_ISDBT
;
1155 rc
= dvb_register_frontend(&client
->adapter
, &client
->frontend
);
1157 pr_err("frontend registration failed %d\n", rc
);
1158 goto frontend_error
;
1161 params
.initial_id
= 1;
1162 params
.data_type
= MSG_SMS_DVBT_BDA_DATA
;
1163 params
.onresponse_handler
= smsdvb_onresponse
;
1164 params
.onremove_handler
= smsdvb_onremove
;
1165 params
.context
= client
;
1167 rc
= smscore_register_client(coredev
, ¶ms
, &client
->smsclient
);
1169 pr_err("smscore_register_client() failed %d\n", rc
);
1173 client
->coredev
= coredev
;
1175 init_completion(&client
->tune_done
);
1176 init_completion(&client
->stats_done
);
1178 kmutex_lock(&g_smsdvb_clientslock
);
1180 list_add(&client
->entry
, &g_smsdvb_clients
);
1182 kmutex_unlock(&g_smsdvb_clientslock
);
1184 client
->event_fe_state
= -1;
1185 client
->event_unc_state
= -1;
1186 sms_board_dvb3_event(client
, DVB3_EVENT_HOTPLUG
);
1188 sms_info("success");
1189 sms_board_setup(coredev
);
1191 if (smsdvb_debugfs_create(client
) < 0)
1192 sms_info("failed to create debugfs node");
1194 dvb_create_media_graph(coredev
->media_dev
);
1199 dvb_unregister_frontend(&client
->frontend
);
1202 dvb_dmxdev_release(&client
->dmxdev
);
1205 dvb_dmx_release(&client
->demux
);
1208 smsdvb_media_device_unregister(client
);
1209 dvb_unregister_adapter(&client
->adapter
);
1216 static int __init
smsdvb_module_init(void)
1220 INIT_LIST_HEAD(&g_smsdvb_clients
);
1221 kmutex_init(&g_smsdvb_clientslock
);
1223 smsdvb_debugfs_register();
1225 rc
= smscore_register_hotplug(smsdvb_hotplug
);
1232 static void __exit
smsdvb_module_exit(void)
1234 smscore_unregister_hotplug(smsdvb_hotplug
);
1236 kmutex_lock(&g_smsdvb_clientslock
);
1238 while (!list_empty(&g_smsdvb_clients
))
1239 smsdvb_unregister_client((struct smsdvb_client_t
*)g_smsdvb_clients
.next
);
1241 smsdvb_debugfs_unregister();
1243 kmutex_unlock(&g_smsdvb_clientslock
);
1246 module_init(smsdvb_module_init
);
1247 module_exit(smsdvb_module_exit
);
1249 MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
1250 MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
1251 MODULE_LICENSE("GPL");