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 <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <asm/div64.h>
29 #include "dvb_demux.h"
30 #include "dvb_frontend.h"
32 #include "smscoreapi.h"
33 #include "sms-cards.h"
37 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
39 static struct list_head g_smsdvb_clients
;
40 static struct mutex g_smsdvb_clientslock
;
43 module_param_named(debug
, sms_dbg
, int, 0644);
44 MODULE_PARM_DESC(debug
, "set debug level (info=1, adv=2 (or-able))");
47 static u32 sms_to_guard_interval_table
[] = {
48 [0] = GUARD_INTERVAL_1_32
,
49 [1] = GUARD_INTERVAL_1_16
,
50 [2] = GUARD_INTERVAL_1_8
,
51 [3] = GUARD_INTERVAL_1_4
,
54 static u32 sms_to_code_rate_table
[] = {
63 static u32 sms_to_hierarchy_table
[] = {
70 static u32 sms_to_modulation_table
[] = {
78 /* Events that may come from DVB v3 adapter */
79 static void sms_board_dvb3_event(struct smsdvb_client_t
*client
,
80 enum SMS_DVB3_EVENTS event
) {
82 struct smscore_device_t
*coredev
= client
->coredev
;
85 sms_debug("DVB3_EVENT_INIT");
86 sms_board_event(coredev
, BOARD_EVENT_BIND
);
88 case DVB3_EVENT_SLEEP
:
89 sms_debug("DVB3_EVENT_SLEEP");
90 sms_board_event(coredev
, BOARD_EVENT_POWER_SUSPEND
);
92 case DVB3_EVENT_HOTPLUG
:
93 sms_debug("DVB3_EVENT_HOTPLUG");
94 sms_board_event(coredev
, BOARD_EVENT_POWER_INIT
);
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
);
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
);
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
);
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
);
126 sms_err("Unknown dvb3 api event");
131 static void smsdvb_stats_not_ready(struct dvb_frontend
*fe
)
133 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
;
139 switch (smscore_get_device_mode(coredev
)) {
140 case DEVICE_MODE_ISDBT
:
141 case DEVICE_MODE_ISDBT_BDA
:
151 c
->strength
.stat
[0].scale
= FE_SCALE_DECIBEL
;
152 c
->cnr
.stat
[0].scale
= FE_SCALE_DECIBEL
;
154 /* 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
;
161 * Put all of them at FE_SCALE_NOT_AVAILABLE. They're dynamically
162 * changed when the stats become available.
164 for (i
= 0; i
< n_layers
; i
++) {
165 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
;
172 static inline int sms_to_mode(u32 mode
)
176 return TRANSMISSION_MODE_2K
;
178 return TRANSMISSION_MODE_4K
;
180 return TRANSMISSION_MODE_8K
;
182 return TRANSMISSION_MODE_AUTO
;
185 static inline int sms_to_status(u32 is_demod_locked
, u32 is_rf_locked
)
188 return FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
189 FE_HAS_SYNC
| FE_HAS_LOCK
;
192 return FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
197 static inline u32
sms_to_bw(u32 value
)
199 return value
* 1000000;
202 #define convert_from_table(value, table, defval) ({ \
204 if (value < ARRAY_SIZE(table)) \
205 __ret = table[value]; \
211 #define sms_to_guard_interval(value) \
212 convert_from_table(value, sms_to_guard_interval_table, \
213 GUARD_INTERVAL_AUTO);
215 #define sms_to_code_rate(value) \
216 convert_from_table(value, sms_to_code_rate_table, \
219 #define sms_to_hierarchy(value) \
220 convert_from_table(value, sms_to_hierarchy_table, \
223 #define sms_to_modulation(value) \
224 convert_from_table(value, sms_to_modulation_table, \
227 static void smsdvb_update_tx_params(struct smsdvb_client_t
*client
,
228 struct sms_tx_stats
*p
)
230 struct dvb_frontend
*fe
= &client
->frontend
;
231 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
233 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
);
244 static void smsdvb_update_per_slices(struct smsdvb_client_t
*client
,
245 struct RECEPTION_STATISTICS_PER_SLICES_S
*p
)
247 struct dvb_frontend
*fe
= &client
->frontend
;
248 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
251 client
->fe_status
= sms_to_status(p
->is_demod_locked
, p
->is_rf_locked
);
252 c
->modulation
= sms_to_modulation(p
->constellation
);
254 /* signal Strength, in DBm */
255 c
->strength
.stat
[0].uvalue
= p
->in_band_power
* 1000;
257 /* Carrier to noise ratio, in DB */
258 c
->cnr
.stat
[0].svalue
= p
->snr
* 1000;
260 /* PER/BER requires demod lock */
261 if (!p
->is_demod_locked
)
265 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
;
268 c
->block_error
.stat
[0].uvalue
+= p
->ets_packets
;
269 c
->block_count
.stat
[0].uvalue
+= p
->ets_packets
+ p
->ts_packets
;
272 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
273 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
274 c
->post_bit_error
.stat
[0].uvalue
+= p
->ber_error_count
;
275 c
->post_bit_count
.stat
[0].uvalue
+= p
->ber_bit_count
;
278 tmp
= p
->ets_packets
* 65535;
279 if (p
->ts_packets
+ p
->ets_packets
)
280 do_div(tmp
, p
->ts_packets
+ p
->ets_packets
);
281 client
->legacy_per
= tmp
;
284 static void smsdvb_update_dvb_stats(struct smsdvb_client_t
*client
,
287 struct dvb_frontend
*fe
= &client
->frontend
;
288 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
290 if (client
->prt_dvb_stats
)
291 client
->prt_dvb_stats(client
->debug_data
, p
);
293 client
->fe_status
= sms_to_status(p
->is_demod_locked
, p
->is_rf_locked
);
295 /* Update DVB modulation parameters */
296 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
);
306 /* update reception data */
307 c
->lna
= p
->is_external_lna_on
? 1 : 0;
309 /* Carrier to noise ratio, in DB */
310 c
->cnr
.stat
[0].svalue
= p
->SNR
* 1000;
312 /* signal Strength, in DBm */
313 c
->strength
.stat
[0].uvalue
= p
->in_band_pwr
* 1000;
315 /* PER/BER requires demod lock */
316 if (!p
->is_demod_locked
)
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
;
323 c
->block_error
.stat
[0].uvalue
+= p
->error_ts_packets
;
324 c
->block_count
.stat
[0].uvalue
+= p
->total_ts_packets
;
327 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
328 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
329 c
->post_bit_error
.stat
[0].uvalue
+= p
->ber_error_count
;
330 c
->post_bit_count
.stat
[0].uvalue
+= p
->ber_bit_count
;
333 client
->legacy_ber
= p
->ber
;
336 static void smsdvb_update_isdbt_stats(struct smsdvb_client_t
*client
,
337 struct sms_isdbt_stats
*p
)
339 struct dvb_frontend
*fe
= &client
->frontend
;
340 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
341 struct sms_isdbt_layer_stats
*lr
;
344 if (client
->prt_isdb_stats
)
345 client
->prt_isdb_stats(client
->debug_data
, p
);
347 client
->fe_status
= sms_to_status(p
->is_demod_locked
, p
->is_rf_locked
);
350 * Firmware 2.1 seems to report only lock status and
351 * signal strength. The signal strength indicator is at the
354 if (p
->statistics_type
== 0) {
355 c
->strength
.stat
[0].uvalue
= ((s32
)p
->transmission_mode
) * 1000;
356 c
->cnr
.stat
[0].scale
= FE_SCALE_NOT_AVAILABLE
;
360 /* Update ISDB-T transmission parameters */
361 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
;
371 c
->isdbt_layer_enabled
= 0;
373 /* update reception data */
374 c
->lna
= p
->is_external_lna_on
? 1 : 0;
376 /* Carrier to noise ratio, in DB */
377 c
->cnr
.stat
[0].svalue
= p
->SNR
* 1000;
379 /* signal Strength, in DBm */
380 c
->strength
.stat
[0].uvalue
= p
->in_band_pwr
* 1000;
382 /* PER/BER and per-layer stats require demod lock */
383 if (!p
->is_demod_locked
)
386 client
->last_per
= c
->block_error
.stat
[0].uvalue
;
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;
391 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
392 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
393 c
->post_bit_error
.stat
[0].uvalue
= 0;
394 c
->post_bit_count
.stat
[0].uvalue
= 0;
395 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
396 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
398 for (i
= 0; i
< n_layers
; i
++) {
399 lr
= &p
->layer_info
[i
];
401 /* Update per-layer transmission parameters */
402 if (lr
->number_of_segments
> 0 && lr
->number_of_segments
< 13) {
403 c
->isdbt_layer_enabled
|= 1 << i
;
404 c
->layer
[i
].segment_count
= lr
->number_of_segments
;
408 c
->layer
[i
].modulation
= sms_to_modulation(lr
->constellation
);
411 c
->block_error
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
412 c
->block_count
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
413 c
->block_error
.stat
[i
+ 1].uvalue
+= lr
->error_ts_packets
;
414 c
->block_count
.stat
[i
+ 1].uvalue
+= lr
->total_ts_packets
;
416 /* Update global PER counter */
417 c
->block_error
.stat
[0].uvalue
+= lr
->error_ts_packets
;
418 c
->block_count
.stat
[0].uvalue
+= lr
->total_ts_packets
;
421 c
->post_bit_error
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
422 c
->post_bit_count
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
423 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
;
426 /* Update global BER counter */
427 c
->post_bit_error
.stat
[0].uvalue
+= lr
->ber_error_count
;
428 c
->post_bit_count
.stat
[0].uvalue
+= lr
->ber_bit_count
;
432 static void smsdvb_update_isdbt_stats_ex(struct smsdvb_client_t
*client
,
433 struct sms_isdbt_stats_ex
*p
)
435 struct dvb_frontend
*fe
= &client
->frontend
;
436 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
437 struct sms_isdbt_layer_stats
*lr
;
440 if (client
->prt_isdb_stats_ex
)
441 client
->prt_isdb_stats_ex(client
->debug_data
, p
);
443 /* Update ISDB-T transmission parameters */
444 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
;
455 c
->isdbt_layer_enabled
= 0;
457 /* update reception data */
458 c
->lna
= p
->is_external_lna_on
? 1 : 0;
460 /* Carrier to noise ratio, in DB */
461 c
->cnr
.stat
[0].svalue
= p
->SNR
* 1000;
463 /* signal Strength, in DBm */
464 c
->strength
.stat
[0].uvalue
= p
->in_band_pwr
* 1000;
466 /* PER/BER and per-layer stats require demod lock */
467 if (!p
->is_demod_locked
)
470 client
->last_per
= c
->block_error
.stat
[0].uvalue
;
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;
475 c
->block_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
476 c
->block_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
477 c
->post_bit_error
.stat
[0].uvalue
= 0;
478 c
->post_bit_count
.stat
[0].uvalue
= 0;
479 c
->post_bit_error
.stat
[0].scale
= FE_SCALE_COUNTER
;
480 c
->post_bit_count
.stat
[0].scale
= FE_SCALE_COUNTER
;
482 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;
486 for (i
= 0; i
< n_layers
; i
++) {
487 lr
= &p
->layer_info
[i
];
489 /* Update per-layer transmission parameters */
490 if (lr
->number_of_segments
> 0 && lr
->number_of_segments
< 13) {
491 c
->isdbt_layer_enabled
|= 1 << i
;
492 c
->layer
[i
].segment_count
= lr
->number_of_segments
;
496 c
->layer
[i
].modulation
= sms_to_modulation(lr
->constellation
);
499 c
->block_error
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
500 c
->block_count
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
501 c
->block_error
.stat
[i
+ 1].uvalue
+= lr
->error_ts_packets
;
502 c
->block_count
.stat
[i
+ 1].uvalue
+= lr
->total_ts_packets
;
504 /* Update global PER counter */
505 c
->block_error
.stat
[0].uvalue
+= lr
->error_ts_packets
;
506 c
->block_count
.stat
[0].uvalue
+= lr
->total_ts_packets
;
509 c
->post_bit_error
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
510 c
->post_bit_count
.stat
[i
+ 1].scale
= FE_SCALE_COUNTER
;
511 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
;
514 /* Update global ber counter */
515 c
->post_bit_error
.stat
[0].uvalue
+= lr
->ber_error_count
;
516 c
->post_bit_count
.stat
[0].uvalue
+= lr
->ber_bit_count
;
520 static int smsdvb_onresponse(void *context
, struct smscore_buffer_t
*cb
)
522 struct smsdvb_client_t
*client
= (struct smsdvb_client_t
*) context
;
523 struct sms_msg_hdr
*phdr
= (struct sms_msg_hdr
*) (((u8
*) cb
->p
)
526 struct dvb_frontend
*fe
= &client
->frontend
;
527 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
528 bool is_status_update
= false;
530 switch (phdr
->msg_type
) {
531 case MSG_SMS_DVBT_BDA_DATA
:
533 * Only feed data to dvb demux if are there any feed listening
534 * to it and if the device has tuned
536 if (client
->feed_users
&& client
->has_tuned
)
537 dvb_dmx_swfilter(&client
->demux
, p
,
538 cb
->size
- sizeof(struct sms_msg_hdr
));
541 case MSG_SMS_RF_TUNE_RES
:
542 case MSG_SMS_ISDBT_TUNE_RES
:
543 complete(&client
->tune_done
);
546 case MSG_SMS_SIGNAL_DETECTED_IND
:
547 client
->fe_status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
548 FE_HAS_VITERBI
| FE_HAS_SYNC
|
551 is_status_update
= true;
554 case MSG_SMS_NO_SIGNAL_IND
:
555 client
->fe_status
= 0;
557 is_status_update
= true;
560 case MSG_SMS_TRANSMISSION_IND
:
561 smsdvb_update_tx_params(client
, p
);
563 is_status_update
= true;
566 case MSG_SMS_HO_PER_SLICES_IND
:
567 smsdvb_update_per_slices(client
, p
);
569 is_status_update
= true;
572 case MSG_SMS_GET_STATISTICS_RES
:
573 switch (smscore_get_device_mode(client
->coredev
)) {
574 case DEVICE_MODE_ISDBT
:
575 case DEVICE_MODE_ISDBT_BDA
:
576 smsdvb_update_isdbt_stats(client
, p
);
579 /* Skip sms_msg_statistics_info:request_result field */
580 smsdvb_update_dvb_stats(client
, p
+ sizeof(u32
));
583 is_status_update
= true;
586 /* Only for ISDB-T */
587 case MSG_SMS_GET_STATISTICS_EX_RES
:
588 /* Skip sms_msg_statistics_info:request_result field? */
589 smsdvb_update_isdbt_stats_ex(client
, p
+ sizeof(u32
));
590 is_status_update
= true;
593 sms_info("message not handled");
595 smscore_putbuffer(client
->coredev
, cb
);
597 if (is_status_update
) {
598 if (client
->fe_status
& FE_HAS_LOCK
) {
599 sms_board_dvb3_event(client
, DVB3_EVENT_FE_LOCK
);
600 if (client
->last_per
== c
->block_error
.stat
[0].uvalue
)
601 sms_board_dvb3_event(client
, DVB3_EVENT_UNC_OK
);
603 sms_board_dvb3_event(client
, DVB3_EVENT_UNC_ERR
);
604 client
->has_tuned
= true;
606 smsdvb_stats_not_ready(fe
);
607 client
->has_tuned
= false;
608 sms_board_dvb3_event(client
, DVB3_EVENT_FE_UNLOCK
);
610 complete(&client
->stats_done
);
616 static void smsdvb_unregister_client(struct smsdvb_client_t
*client
)
618 /* must be called under clientslock */
620 list_del(&client
->entry
);
622 smsdvb_debugfs_release(client
);
623 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
);
631 static void smsdvb_onremove(void *context
)
633 kmutex_lock(&g_smsdvb_clientslock
);
635 smsdvb_unregister_client((struct smsdvb_client_t
*) context
);
637 kmutex_unlock(&g_smsdvb_clientslock
);
640 static int smsdvb_start_feed(struct dvb_demux_feed
*feed
)
642 struct smsdvb_client_t
*client
=
643 container_of(feed
->demux
, struct smsdvb_client_t
, demux
);
644 struct sms_msg_data pid_msg
;
646 sms_debug("add pid %d(%x)",
647 feed
->pid
, feed
->pid
);
649 client
->feed_users
++;
651 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
;
658 return smsclient_sendrequest(client
->smsclient
,
659 &pid_msg
, sizeof(pid_msg
));
662 static int smsdvb_stop_feed(struct dvb_demux_feed
*feed
)
664 struct smsdvb_client_t
*client
=
665 container_of(feed
->demux
, struct smsdvb_client_t
, demux
);
666 struct sms_msg_data pid_msg
;
668 sms_debug("remove pid %d(%x)",
669 feed
->pid
, feed
->pid
);
671 client
->feed_users
--;
673 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
;
680 return smsclient_sendrequest(client
->smsclient
,
681 &pid_msg
, sizeof(pid_msg
));
684 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t
*client
,
685 void *buffer
, size_t size
,
686 struct completion
*completion
)
690 rc
= smsclient_sendrequest(client
->smsclient
, buffer
, size
);
694 return wait_for_completion_timeout(completion
,
695 msecs_to_jiffies(2000)) ?
699 static int smsdvb_send_statistics_request(struct smsdvb_client_t
*client
)
702 struct sms_msg_hdr msg
;
704 /* Don't request stats too fast */
705 if (client
->get_stats_jiffies
&&
706 (!time_after(jiffies
, client
->get_stats_jiffies
)))
708 client
->get_stats_jiffies
= jiffies
+ msecs_to_jiffies(100);
710 msg
.msg_src_id
= DVBT_BDA_CONTROL_MSG_ID
;
711 msg
.msg_dst_id
= HIF_TASK
;
713 msg
.msg_length
= sizeof(msg
);
715 switch (smscore_get_device_mode(client
->coredev
)) {
716 case DEVICE_MODE_ISDBT
:
717 case DEVICE_MODE_ISDBT_BDA
:
719 * Check for firmware version, to avoid breaking for old cards
721 if (client
->coredev
->fw_version
>= 0x800)
722 msg
.msg_type
= MSG_SMS_GET_STATISTICS_EX_REQ
;
724 msg
.msg_type
= MSG_SMS_GET_STATISTICS_REQ
;
727 msg
.msg_type
= MSG_SMS_GET_STATISTICS_REQ
;
730 rc
= smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
731 &client
->stats_done
);
736 static inline int led_feedback(struct smsdvb_client_t
*client
)
738 if (!(client
->fe_status
& FE_HAS_LOCK
))
739 return sms_board_led_feedback(client
->coredev
, SMS_LED_OFF
);
741 return sms_board_led_feedback(client
->coredev
,
742 (client
->legacy_ber
== 0) ?
743 SMS_LED_HI
: SMS_LED_LO
);
746 static int smsdvb_read_status(struct dvb_frontend
*fe
, fe_status_t
*stat
)
749 struct smsdvb_client_t
*client
;
750 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
752 rc
= smsdvb_send_statistics_request(client
);
754 *stat
= client
->fe_status
;
756 led_feedback(client
);
761 static int smsdvb_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
764 struct smsdvb_client_t
*client
;
766 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
768 rc
= smsdvb_send_statistics_request(client
);
770 *ber
= client
->legacy_ber
;
772 led_feedback(client
);
777 static int smsdvb_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
779 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
781 s32 power
= (s32
) c
->strength
.stat
[0].uvalue
;
782 struct smsdvb_client_t
*client
;
784 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
786 rc
= smsdvb_send_statistics_request(client
);
790 else if (power
> -29)
793 *strength
= (power
+ 95) * 65535 / 66;
795 led_feedback(client
);
800 static int smsdvb_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
802 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
804 struct smsdvb_client_t
*client
;
806 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
808 rc
= smsdvb_send_statistics_request(client
);
810 /* Preferred scale for SNR with legacy API: 0.1 dB */
811 *snr
= ((u32
)c
->cnr
.stat
[0].svalue
) / 100;
813 led_feedback(client
);
818 static int smsdvb_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
821 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
822 struct smsdvb_client_t
*client
;
824 client
= container_of(fe
, struct smsdvb_client_t
, frontend
);
826 rc
= smsdvb_send_statistics_request(client
);
828 *ucblocks
= c
->block_error
.stat
[0].uvalue
;
830 led_feedback(client
);
835 static int smsdvb_get_tune_settings(struct dvb_frontend
*fe
,
836 struct dvb_frontend_tune_settings
*tune
)
840 tune
->min_delay_ms
= 400;
841 tune
->step_size
= 250000;
846 static int smsdvb_dvbt_set_frontend(struct dvb_frontend
*fe
)
848 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
849 struct smsdvb_client_t
*client
=
850 container_of(fe
, struct smsdvb_client_t
, frontend
);
853 struct sms_msg_hdr msg
;
859 client
->fe_status
= 0;
860 client
->event_fe_state
= -1;
861 client
->event_unc_state
= -1;
862 fe
->dtv_property_cache
.delivery_system
= SYS_DVBT
;
864 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;
872 sms_info("%s: freq %d band %d", __func__
, c
->frequency
,
875 switch (c
->bandwidth_hz
/ 1000000) {
877 msg
.Data
[1] = BW_8_MHZ
;
880 msg
.Data
[1] = BW_7_MHZ
;
883 msg
.Data
[1] = BW_6_MHZ
;
890 /* Disable LNA, if any. An error is returned if no LNA is present */
891 ret
= sms_board_lna_control(client
->coredev
, 0);
895 /* tune with LNA off at first */
896 ret
= smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
899 smsdvb_read_status(fe
, &status
);
901 if (status
& FE_HAS_LOCK
)
904 /* previous tune didn't lock - enable LNA and tune again */
905 sms_board_lna_control(client
->coredev
, 1);
908 return smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
912 static int smsdvb_isdbt_set_frontend(struct dvb_frontend
*fe
)
914 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
915 struct smsdvb_client_t
*client
=
916 container_of(fe
, struct smsdvb_client_t
, frontend
);
917 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
;
923 struct sms_msg_hdr msg
;
927 fe
->dtv_property_cache
.delivery_system
= SYS_ISDBT
;
929 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
);
935 if (c
->isdbt_sb_segment_idx
== -1)
936 c
->isdbt_sb_segment_idx
= 0;
938 if (!c
->isdbt_layer_enabled
)
939 c
->isdbt_layer_enabled
= 7;
941 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
;
946 if (c
->isdbt_partial_reception
) {
947 if ((type
== SMS_PELE
|| type
== SMS_RIO
) &&
948 c
->isdbt_sb_segment_count
> 3)
949 msg
.Data
[1] = BW_ISDBT_13SEG
;
950 else if (c
->isdbt_sb_segment_count
> 1)
951 msg
.Data
[1] = BW_ISDBT_3SEG
;
952 } else if (type
== SMS_PELE
|| type
== SMS_RIO
)
953 msg
.Data
[1] = BW_ISDBT_13SEG
;
955 c
->bandwidth_hz
= 6000000;
957 sms_info("%s: freq %d segwidth %d segindex %d", __func__
,
958 c
->frequency
, c
->isdbt_sb_segment_count
,
959 c
->isdbt_sb_segment_idx
);
961 /* Disable LNA, if any. An error is returned if no LNA is present */
962 ret
= sms_board_lna_control(client
->coredev
, 0);
966 /* tune with LNA off at first */
967 ret
= smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
970 smsdvb_read_status(fe
, &status
);
972 if (status
& FE_HAS_LOCK
)
975 /* previous tune didn't lock - enable LNA and tune again */
976 sms_board_lna_control(client
->coredev
, 1);
978 return smsdvb_sendrequest_and_wait(client
, &msg
, sizeof(msg
),
982 static int smsdvb_set_frontend(struct dvb_frontend
*fe
)
984 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
985 struct smsdvb_client_t
*client
=
986 container_of(fe
, struct smsdvb_client_t
, frontend
);
987 struct smscore_device_t
*coredev
= client
->coredev
;
989 smsdvb_stats_not_ready(fe
);
990 c
->strength
.stat
[0].uvalue
= 0;
991 c
->cnr
.stat
[0].uvalue
= 0;
993 client
->has_tuned
= false;
995 switch (smscore_get_device_mode(coredev
)) {
996 case DEVICE_MODE_DVBT
:
997 case DEVICE_MODE_DVBT_BDA
:
998 return smsdvb_dvbt_set_frontend(fe
);
999 case DEVICE_MODE_ISDBT
:
1000 case DEVICE_MODE_ISDBT_BDA
:
1001 return smsdvb_isdbt_set_frontend(fe
);
1007 /* Nothing to do here, as stats are automatically updated */
1008 static int smsdvb_get_frontend(struct dvb_frontend
*fe
)
1013 static int smsdvb_init(struct dvb_frontend
*fe
)
1015 struct smsdvb_client_t
*client
=
1016 container_of(fe
, struct smsdvb_client_t
, frontend
);
1018 sms_board_power(client
->coredev
, 1);
1020 sms_board_dvb3_event(client
, DVB3_EVENT_INIT
);
1024 static int smsdvb_sleep(struct dvb_frontend
*fe
)
1026 struct smsdvb_client_t
*client
=
1027 container_of(fe
, struct smsdvb_client_t
, frontend
);
1029 sms_board_led_feedback(client
->coredev
, SMS_LED_OFF
);
1030 sms_board_power(client
->coredev
, 0);
1032 sms_board_dvb3_event(client
, DVB3_EVENT_SLEEP
);
1037 static void smsdvb_release(struct dvb_frontend
*fe
)
1042 static struct dvb_frontend_ops smsdvb_fe_ops
= {
1044 .name
= "Siano Mobile Digital MDTV Receiver",
1045 .frequency_min
= 44250000,
1046 .frequency_max
= 867250000,
1047 .frequency_stepsize
= 250000,
1048 .caps
= FE_CAN_INVERSION_AUTO
|
1049 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
|
1055 FE_CAN_HIERARCHY_AUTO
,
1058 .release
= smsdvb_release
,
1060 .set_frontend
= smsdvb_set_frontend
,
1061 .get_frontend
= smsdvb_get_frontend
,
1062 .get_tune_settings
= smsdvb_get_tune_settings
,
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
,
1068 .read_ucblocks
= smsdvb_read_ucblocks
,
1070 .init
= smsdvb_init
,
1071 .sleep
= smsdvb_sleep
,
1074 static int smsdvb_hotplug(struct smscore_device_t
*coredev
,
1075 struct device
*device
, int arrival
)
1077 struct smsclient_params_t params
;
1078 struct smsdvb_client_t
*client
;
1081 /* device removal handled by onremove callback */
1084 client
= kzalloc(sizeof(struct smsdvb_client_t
), GFP_KERNEL
);
1086 sms_err("kmalloc() failed");
1090 /* register dvb adapter */
1091 rc
= dvb_register_adapter(&client
->adapter
,
1093 smscore_get_board_id(coredev
))->name
,
1094 THIS_MODULE
, device
, adapter_nr
);
1096 sms_err("dvb_register_adapter() failed %d", rc
);
1100 /* init dvb demux */
1101 client
->demux
.dmx
.capabilities
= DMX_TS_FILTERING
;
1102 client
->demux
.filternum
= 32; /* todo: nova ??? */
1103 client
->demux
.feednum
= 32;
1104 client
->demux
.start_feed
= smsdvb_start_feed
;
1105 client
->demux
.stop_feed
= smsdvb_stop_feed
;
1107 rc
= dvb_dmx_init(&client
->demux
);
1109 sms_err("dvb_dmx_init failed %d", rc
);
1114 client
->dmxdev
.filternum
= 32;
1115 client
->dmxdev
.demux
= &client
->demux
.dmx
;
1116 client
->dmxdev
.capabilities
= 0;
1118 rc
= dvb_dmxdev_init(&client
->dmxdev
, &client
->adapter
);
1120 sms_err("dvb_dmxdev_init failed %d", rc
);
1124 /* init and register frontend */
1125 memcpy(&client
->frontend
.ops
, &smsdvb_fe_ops
,
1126 sizeof(struct dvb_frontend_ops
));
1128 switch (smscore_get_device_mode(coredev
)) {
1129 case DEVICE_MODE_DVBT
:
1130 case DEVICE_MODE_DVBT_BDA
:
1131 client
->frontend
.ops
.delsys
[0] = SYS_DVBT
;
1133 case DEVICE_MODE_ISDBT
:
1134 case DEVICE_MODE_ISDBT_BDA
:
1135 client
->frontend
.ops
.delsys
[0] = SYS_ISDBT
;
1139 rc
= dvb_register_frontend(&client
->adapter
, &client
->frontend
);
1141 sms_err("frontend registration failed %d", rc
);
1142 goto frontend_error
;
1145 params
.initial_id
= 1;
1146 params
.data_type
= MSG_SMS_DVBT_BDA_DATA
;
1147 params
.onresponse_handler
= smsdvb_onresponse
;
1148 params
.onremove_handler
= smsdvb_onremove
;
1149 params
.context
= client
;
1151 rc
= smscore_register_client(coredev
, ¶ms
, &client
->smsclient
);
1153 sms_err("smscore_register_client() failed %d", rc
);
1157 client
->coredev
= coredev
;
1159 init_completion(&client
->tune_done
);
1160 init_completion(&client
->stats_done
);
1162 kmutex_lock(&g_smsdvb_clientslock
);
1164 list_add(&client
->entry
, &g_smsdvb_clients
);
1166 kmutex_unlock(&g_smsdvb_clientslock
);
1168 client
->event_fe_state
= -1;
1169 client
->event_unc_state
= -1;
1170 sms_board_dvb3_event(client
, DVB3_EVENT_HOTPLUG
);
1172 sms_info("success");
1173 sms_board_setup(coredev
);
1175 if (smsdvb_debugfs_create(client
) < 0)
1176 sms_info("failed to create debugfs node");
1181 dvb_unregister_frontend(&client
->frontend
);
1184 dvb_dmxdev_release(&client
->dmxdev
);
1187 dvb_dmx_release(&client
->demux
);
1190 dvb_unregister_adapter(&client
->adapter
);
1197 static int __init
smsdvb_module_init(void)
1201 INIT_LIST_HEAD(&g_smsdvb_clients
);
1202 kmutex_init(&g_smsdvb_clientslock
);
1204 smsdvb_debugfs_register();
1206 rc
= smscore_register_hotplug(smsdvb_hotplug
);
1213 static void __exit
smsdvb_module_exit(void)
1215 smscore_unregister_hotplug(smsdvb_hotplug
);
1217 kmutex_lock(&g_smsdvb_clientslock
);
1219 while (!list_empty(&g_smsdvb_clients
))
1220 smsdvb_unregister_client((struct smsdvb_client_t
*)g_smsdvb_clients
.next
);
1222 smsdvb_debugfs_unregister();
1224 kmutex_unlock(&g_smsdvb_clientslock
);
1227 module_init(smsdvb_module_init
);
1228 module_exit(smsdvb_module_exit
);
1230 MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
1231 MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
1232 MODULE_LICENSE("GPL");