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 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 u32 sms_to_code_rate_table[] = {
63 u32 sms_to_hierarchy_table[] = {
70 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:
150 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
151 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
153 /* Per-layer stats */
154 c->post_bit_error.len = n_layers;
155 c->post_bit_count.len = n_layers;
156 c->block_error.len = n_layers;
157 c->block_count.len = n_layers;
160 * Put all of them at FE_SCALE_NOT_AVAILABLE. They're dynamically
161 * changed when the stats become available.
163 for (i = 0; i < n_layers; i++) {
164 c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
165 c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
166 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
167 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
171 static inline int sms_to_mode(u32 mode)
175 return TRANSMISSION_MODE_2K;
177 return TRANSMISSION_MODE_4K;
179 return TRANSMISSION_MODE_8K;
181 return TRANSMISSION_MODE_AUTO;
184 static inline int sms_to_status(u32 is_demod_locked, u32 is_rf_locked)
187 return FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
188 FE_HAS_SYNC | FE_HAS_LOCK;
191 return FE_HAS_SIGNAL | FE_HAS_CARRIER;
196 static inline u32 sms_to_bw(u32 value)
198 return value * 1000000;
201 #define convert_from_table(value, table, defval) ({ \
203 if (value < ARRAY_SIZE(table)) \
204 __ret = table[value]; \
210 #define sms_to_guard_interval(value) \
211 convert_from_table(value, sms_to_guard_interval_table, \
212 GUARD_INTERVAL_AUTO);
214 #define sms_to_code_rate(value) \
215 convert_from_table(value, sms_to_code_rate_table, \
218 #define sms_to_hierarchy(value) \
219 convert_from_table(value, sms_to_hierarchy_table, \
222 #define sms_to_modulation(value) \
223 convert_from_table(value, sms_to_modulation_table, \
226 static void smsdvb_update_tx_params(struct smsdvb_client_t *client,
227 struct sms_tx_stats *p)
229 struct dvb_frontend *fe = &client->frontend;
230 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
232 c->frequency = p->frequency;
233 client->fe_status = sms_to_status(p->is_demod_locked, 0);
234 c->bandwidth_hz = sms_to_bw(p->bandwidth);
235 c->transmission_mode = sms_to_mode(p->transmission_mode);
236 c->guard_interval = sms_to_guard_interval(p->guard_interval);
237 c->code_rate_HP = sms_to_code_rate(p->code_rate);
238 c->code_rate_LP = sms_to_code_rate(p->lp_code_rate);
239 c->hierarchy = sms_to_hierarchy(p->hierarchy);
240 c->modulation = sms_to_modulation(p->constellation);
243 static void smsdvb_update_per_slices(struct smsdvb_client_t *client,
244 struct RECEPTION_STATISTICS_PER_SLICES_S *p)
246 struct dvb_frontend *fe = &client->frontend;
247 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
250 client->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
251 c->modulation = sms_to_modulation(p->constellation);
253 /* signal Strength, in DBm */
254 c->strength.stat[0].uvalue = p->in_band_power * 1000;
256 /* Carrier to noise ratio, in DB */
257 c->cnr.stat[0].svalue = p->snr * 1000;
259 /* PER/BER requires demod lock */
260 if (!p->is_demod_locked)
264 client->last_per = c->block_error.stat[0].uvalue;
265 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
266 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
267 c->block_error.stat[0].uvalue += p->ets_packets;
268 c->block_count.stat[0].uvalue += p->ets_packets + p->ts_packets;
271 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
272 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
273 c->post_bit_error.stat[0].uvalue += p->ber_error_count;
274 c->post_bit_count.stat[0].uvalue += p->ber_bit_count;
277 tmp = p->ets_packets * 65535;
278 if (p->ts_packets + p->ets_packets)
279 do_div(tmp, p->ts_packets + p->ets_packets);
280 client->legacy_per = tmp;
283 static void smsdvb_update_dvb_stats(struct smsdvb_client_t *client,
286 struct dvb_frontend *fe = &client->frontend;
287 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
289 if (client->prt_dvb_stats)
290 client->prt_dvb_stats(client->debug_data, p);
292 client->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
294 /* Update DVB modulation parameters */
295 c->frequency = p->frequency;
296 client->fe_status = sms_to_status(p->is_demod_locked, 0);
297 c->bandwidth_hz = sms_to_bw(p->bandwidth);
298 c->transmission_mode = sms_to_mode(p->transmission_mode);
299 c->guard_interval = sms_to_guard_interval(p->guard_interval);
300 c->code_rate_HP = sms_to_code_rate(p->code_rate);
301 c->code_rate_LP = sms_to_code_rate(p->lp_code_rate);
302 c->hierarchy = sms_to_hierarchy(p->hierarchy);
303 c->modulation = sms_to_modulation(p->constellation);
305 /* update reception data */
306 c->lna = p->is_external_lna_on ? 1 : 0;
308 /* Carrier to noise ratio, in DB */
309 c->cnr.stat[0].svalue = p->SNR * 1000;
311 /* signal Strength, in DBm */
312 c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
314 /* PER/BER requires demod lock */
315 if (!p->is_demod_locked)
319 client->last_per = c->block_error.stat[0].uvalue;
320 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
321 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
322 c->block_error.stat[0].uvalue += p->error_ts_packets;
323 c->block_count.stat[0].uvalue += p->total_ts_packets;
326 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
327 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
328 c->post_bit_error.stat[0].uvalue += p->ber_error_count;
329 c->post_bit_count.stat[0].uvalue += p->ber_bit_count;
332 client->legacy_ber = p->ber;
335 static void smsdvb_update_isdbt_stats(struct smsdvb_client_t *client,
336 struct sms_isdbt_stats *p)
338 struct dvb_frontend *fe = &client->frontend;
339 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
340 struct sms_isdbt_layer_stats *lr;
343 if (client->prt_isdb_stats)
344 client->prt_isdb_stats(client->debug_data, p);
346 client->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
349 * Firmware 2.1 seems to report only lock status and
350 * signal strength. The signal strength indicator is at the
353 if (p->statistics_type == 0) {
354 c->strength.stat[0].uvalue = ((s32)p->transmission_mode) * 1000;
355 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
359 /* Update ISDB-T transmission parameters */
360 c->frequency = p->frequency;
361 c->bandwidth_hz = sms_to_bw(p->bandwidth);
362 c->transmission_mode = sms_to_mode(p->transmission_mode);
363 c->guard_interval = sms_to_guard_interval(p->guard_interval);
364 c->isdbt_partial_reception = p->partial_reception ? 1 : 0;
365 n_layers = p->num_of_layers;
370 c->isdbt_layer_enabled = 0;
372 /* update reception data */
373 c->lna = p->is_external_lna_on ? 1 : 0;
375 /* Carrier to noise ratio, in DB */
376 c->cnr.stat[0].svalue = p->SNR * 1000;
378 /* signal Strength, in DBm */
379 c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
381 /* PER/BER and per-layer stats require demod lock */
382 if (!p->is_demod_locked)
385 client->last_per = c->block_error.stat[0].uvalue;
387 /* Clears global counters, as the code below will sum it again */
388 c->block_error.stat[0].uvalue = 0;
389 c->block_count.stat[0].uvalue = 0;
390 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
391 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
392 c->post_bit_error.stat[0].uvalue = 0;
393 c->post_bit_count.stat[0].uvalue = 0;
394 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
395 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
397 for (i = 0; i < n_layers; i++) {
398 lr = &p->layer_info[i];
400 /* Update per-layer transmission parameters */
401 if (lr->number_of_segments > 0 && lr->number_of_segments < 13) {
402 c->isdbt_layer_enabled |= 1 << i;
403 c->layer[i].segment_count = lr->number_of_segments;
407 c->layer[i].modulation = sms_to_modulation(lr->constellation);
410 c->block_error.stat[i + 1].scale = FE_SCALE_COUNTER;
411 c->block_count.stat[i + 1].scale = FE_SCALE_COUNTER;
412 c->block_error.stat[i + 1].uvalue += lr->error_ts_packets;
413 c->block_count.stat[i + 1].uvalue += lr->total_ts_packets;
415 /* Update global PER counter */
416 c->block_error.stat[0].uvalue += lr->error_ts_packets;
417 c->block_count.stat[0].uvalue += lr->total_ts_packets;
420 c->post_bit_error.stat[i + 1].scale = FE_SCALE_COUNTER;
421 c->post_bit_count.stat[i + 1].scale = FE_SCALE_COUNTER;
422 c->post_bit_error.stat[i + 1].uvalue += lr->ber_error_count;
423 c->post_bit_count.stat[i + 1].uvalue += lr->ber_bit_count;
425 /* Update global BER counter */
426 c->post_bit_error.stat[0].uvalue += lr->ber_error_count;
427 c->post_bit_count.stat[0].uvalue += lr->ber_bit_count;
431 static void smsdvb_update_isdbt_stats_ex(struct smsdvb_client_t *client,
432 struct sms_isdbt_stats_ex *p)
434 struct dvb_frontend *fe = &client->frontend;
435 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
436 struct sms_isdbt_layer_stats *lr;
439 if (client->prt_isdb_stats_ex)
440 client->prt_isdb_stats_ex(client->debug_data, p);
442 /* Update ISDB-T transmission parameters */
443 c->frequency = p->frequency;
444 client->fe_status = sms_to_status(p->is_demod_locked, 0);
445 c->bandwidth_hz = sms_to_bw(p->bandwidth);
446 c->transmission_mode = sms_to_mode(p->transmission_mode);
447 c->guard_interval = sms_to_guard_interval(p->guard_interval);
448 c->isdbt_partial_reception = p->partial_reception ? 1 : 0;
449 n_layers = p->num_of_layers;
454 c->isdbt_layer_enabled = 0;
456 /* update reception data */
457 c->lna = p->is_external_lna_on ? 1 : 0;
459 /* Carrier to noise ratio, in DB */
460 c->cnr.stat[0].svalue = p->SNR * 1000;
462 /* signal Strength, in DBm */
463 c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
465 /* PER/BER and per-layer stats require demod lock */
466 if (!p->is_demod_locked)
469 client->last_per = c->block_error.stat[0].uvalue;
471 /* Clears global counters, as the code below will sum it again */
472 c->block_error.stat[0].uvalue = 0;
473 c->block_count.stat[0].uvalue = 0;
474 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
475 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
476 c->post_bit_error.stat[0].uvalue = 0;
477 c->post_bit_count.stat[0].uvalue = 0;
478 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
479 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
481 c->post_bit_error.len = n_layers + 1;
482 c->post_bit_count.len = n_layers + 1;
483 c->block_error.len = n_layers + 1;
484 c->block_count.len = n_layers + 1;
485 for (i = 0; i < n_layers; i++) {
486 lr = &p->layer_info[i];
488 /* Update per-layer transmission parameters */
489 if (lr->number_of_segments > 0 && lr->number_of_segments < 13) {
490 c->isdbt_layer_enabled |= 1 << i;
491 c->layer[i].segment_count = lr->number_of_segments;
495 c->layer[i].modulation = sms_to_modulation(lr->constellation);
498 c->block_error.stat[i + 1].scale = FE_SCALE_COUNTER;
499 c->block_count.stat[i + 1].scale = FE_SCALE_COUNTER;
500 c->block_error.stat[i + 1].uvalue += lr->error_ts_packets;
501 c->block_count.stat[i + 1].uvalue += lr->total_ts_packets;
503 /* Update global PER counter */
504 c->block_error.stat[0].uvalue += lr->error_ts_packets;
505 c->block_count.stat[0].uvalue += lr->total_ts_packets;
508 c->post_bit_error.stat[i + 1].scale = FE_SCALE_COUNTER;
509 c->post_bit_count.stat[i + 1].scale = FE_SCALE_COUNTER;
510 c->post_bit_error.stat[i + 1].uvalue += lr->ber_error_count;
511 c->post_bit_count.stat[i + 1].uvalue += lr->ber_bit_count;
513 /* Update global ber counter */
514 c->post_bit_error.stat[0].uvalue += lr->ber_error_count;
515 c->post_bit_count.stat[0].uvalue += lr->ber_bit_count;
519 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
521 struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
522 struct sms_msg_hdr *phdr = (struct sms_msg_hdr *) (((u8 *) cb->p)
525 struct dvb_frontend *fe = &client->frontend;
526 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
527 bool is_status_update = false;
529 switch (phdr->msg_type) {
530 case MSG_SMS_DVBT_BDA_DATA:
532 * Only feed data to dvb demux if are there any feed listening
533 * to it and if the device has tuned
535 if (client->feed_users && client->has_tuned)
536 dvb_dmx_swfilter(&client->demux, p,
537 cb->size - sizeof(struct sms_msg_hdr));
540 case MSG_SMS_RF_TUNE_RES:
541 case MSG_SMS_ISDBT_TUNE_RES:
542 complete(&client->tune_done);
545 case MSG_SMS_SIGNAL_DETECTED_IND:
546 client->fe_status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
547 FE_HAS_VITERBI | FE_HAS_SYNC |
550 is_status_update = true;
553 case MSG_SMS_NO_SIGNAL_IND:
554 client->fe_status = 0;
556 is_status_update = true;
559 case MSG_SMS_TRANSMISSION_IND:
560 smsdvb_update_tx_params(client, p);
562 is_status_update = true;
565 case MSG_SMS_HO_PER_SLICES_IND:
566 smsdvb_update_per_slices(client, p);
568 is_status_update = true;
571 case MSG_SMS_GET_STATISTICS_RES:
572 switch (smscore_get_device_mode(client->coredev)) {
573 case DEVICE_MODE_ISDBT:
574 case DEVICE_MODE_ISDBT_BDA:
575 smsdvb_update_isdbt_stats(client, p);
578 /* Skip sms_msg_statistics_info:request_result field */
579 smsdvb_update_dvb_stats(client, p + sizeof(u32));
582 is_status_update = true;
585 /* Only for ISDB-T */
586 case MSG_SMS_GET_STATISTICS_EX_RES:
587 /* Skip sms_msg_statistics_info:request_result field? */
588 smsdvb_update_isdbt_stats_ex(client, p + sizeof(u32));
589 is_status_update = true;
592 sms_info("message not handled");
594 smscore_putbuffer(client->coredev, cb);
596 if (is_status_update) {
597 if (client->fe_status & FE_HAS_LOCK) {
598 sms_board_dvb3_event(client, DVB3_EVENT_FE_LOCK);
599 if (client->last_per == c->block_error.stat[0].uvalue)
600 sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
602 sms_board_dvb3_event(client, DVB3_EVENT_UNC_ERR);
603 client->has_tuned = true;
605 smsdvb_stats_not_ready(fe);
606 client->has_tuned = false;
607 sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
609 complete(&client->stats_done);
615 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
617 /* must be called under clientslock */
619 list_del(&client->entry);
621 smsdvb_debugfs_release(client);
622 smscore_unregister_client(client->smsclient);
623 dvb_unregister_frontend(&client->frontend);
624 dvb_dmxdev_release(&client->dmxdev);
625 dvb_dmx_release(&client->demux);
626 dvb_unregister_adapter(&client->adapter);
630 static void smsdvb_onremove(void *context)
632 kmutex_lock(&g_smsdvb_clientslock);
634 smsdvb_unregister_client((struct smsdvb_client_t *) context);
636 kmutex_unlock(&g_smsdvb_clientslock);
639 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
641 struct smsdvb_client_t *client =
642 container_of(feed->demux, struct smsdvb_client_t, demux);
643 struct sms_msg_data pid_msg;
645 sms_debug("add pid %d(%x)",
646 feed->pid, feed->pid);
648 client->feed_users++;
650 pid_msg.x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
651 pid_msg.x_msg_header.msg_dst_id = HIF_TASK;
652 pid_msg.x_msg_header.msg_flags = 0;
653 pid_msg.x_msg_header.msg_type = MSG_SMS_ADD_PID_FILTER_REQ;
654 pid_msg.x_msg_header.msg_length = sizeof(pid_msg);
655 pid_msg.msg_data[0] = feed->pid;
657 return smsclient_sendrequest(client->smsclient,
658 &pid_msg, sizeof(pid_msg));
661 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
663 struct smsdvb_client_t *client =
664 container_of(feed->demux, struct smsdvb_client_t, demux);
665 struct sms_msg_data pid_msg;
667 sms_debug("remove pid %d(%x)",
668 feed->pid, feed->pid);
670 client->feed_users--;
672 pid_msg.x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
673 pid_msg.x_msg_header.msg_dst_id = HIF_TASK;
674 pid_msg.x_msg_header.msg_flags = 0;
675 pid_msg.x_msg_header.msg_type = MSG_SMS_REMOVE_PID_FILTER_REQ;
676 pid_msg.x_msg_header.msg_length = sizeof(pid_msg);
677 pid_msg.msg_data[0] = feed->pid;
679 return smsclient_sendrequest(client->smsclient,
680 &pid_msg, sizeof(pid_msg));
683 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
684 void *buffer, size_t size,
685 struct completion *completion)
689 rc = smsclient_sendrequest(client->smsclient, buffer, size);
693 return wait_for_completion_timeout(completion,
694 msecs_to_jiffies(2000)) ?
698 static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
701 struct sms_msg_hdr msg;
703 /* Don't request stats too fast */
704 if (client->get_stats_jiffies &&
705 (!time_after(jiffies, client->get_stats_jiffies)))
707 client->get_stats_jiffies = jiffies + msecs_to_jiffies(100);
709 msg.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
710 msg.msg_dst_id = HIF_TASK;
712 msg.msg_length = sizeof(msg);
714 switch (smscore_get_device_mode(client->coredev)) {
715 case DEVICE_MODE_ISDBT:
716 case DEVICE_MODE_ISDBT_BDA:
718 * Check for firmware version, to avoid breaking for old cards
720 if (client->coredev->fw_version >= 0x800)
721 msg.msg_type = MSG_SMS_GET_STATISTICS_EX_REQ;
723 msg.msg_type = MSG_SMS_GET_STATISTICS_REQ;
726 msg.msg_type = MSG_SMS_GET_STATISTICS_REQ;
729 rc = smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
730 &client->stats_done);
735 static inline int led_feedback(struct smsdvb_client_t *client)
737 if (!(client->fe_status & FE_HAS_LOCK))
738 return sms_board_led_feedback(client->coredev, SMS_LED_OFF);
740 return sms_board_led_feedback(client->coredev,
741 (client->legacy_ber == 0) ?
742 SMS_LED_HI : SMS_LED_LO);
745 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
748 struct smsdvb_client_t *client;
749 client = container_of(fe, struct smsdvb_client_t, frontend);
751 rc = smsdvb_send_statistics_request(client);
753 *stat = client->fe_status;
755 led_feedback(client);
760 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
763 struct smsdvb_client_t *client;
765 client = container_of(fe, struct smsdvb_client_t, frontend);
767 rc = smsdvb_send_statistics_request(client);
769 *ber = client->legacy_ber;
771 led_feedback(client);
776 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
778 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
780 s32 power = (s32) c->strength.stat[0].uvalue;
781 struct smsdvb_client_t *client;
783 client = container_of(fe, struct smsdvb_client_t, frontend);
785 rc = smsdvb_send_statistics_request(client);
789 else if (power > -29)
792 *strength = (power + 95) * 65535 / 66;
794 led_feedback(client);
799 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
801 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
803 struct smsdvb_client_t *client;
805 client = container_of(fe, struct smsdvb_client_t, frontend);
807 rc = smsdvb_send_statistics_request(client);
809 /* Preferred scale for SNR with legacy API: 0.1 dB */
810 *snr = ((u32)c->cnr.stat[0].svalue) / 100;
812 led_feedback(client);
817 static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
820 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
821 struct smsdvb_client_t *client;
823 client = container_of(fe, struct smsdvb_client_t, frontend);
825 rc = smsdvb_send_statistics_request(client);
827 *ucblocks = c->block_error.stat[0].uvalue;
829 led_feedback(client);
834 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
835 struct dvb_frontend_tune_settings *tune)
839 tune->min_delay_ms = 400;
840 tune->step_size = 250000;
845 static int smsdvb_dvbt_set_frontend(struct dvb_frontend *fe)
847 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
848 struct smsdvb_client_t *client =
849 container_of(fe, struct smsdvb_client_t, frontend);
852 struct sms_msg_hdr msg;
858 client->fe_status = 0;
859 client->event_fe_state = -1;
860 client->event_unc_state = -1;
861 fe->dtv_property_cache.delivery_system = SYS_DVBT;
863 msg.msg.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
864 msg.msg.msg_dst_id = HIF_TASK;
865 msg.msg.msg_flags = 0;
866 msg.msg.msg_type = MSG_SMS_RF_TUNE_REQ;
867 msg.msg.msg_length = sizeof(msg);
868 msg.Data[0] = c->frequency;
869 msg.Data[2] = 12000000;
871 sms_info("%s: freq %d band %d", __func__, c->frequency,
874 switch (c->bandwidth_hz / 1000000) {
876 msg.Data[1] = BW_8_MHZ;
879 msg.Data[1] = BW_7_MHZ;
882 msg.Data[1] = BW_6_MHZ;
889 /* Disable LNA, if any. An error is returned if no LNA is present */
890 ret = sms_board_lna_control(client->coredev, 0);
894 /* tune with LNA off at first */
895 ret = smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
898 smsdvb_read_status(fe, &status);
900 if (status & FE_HAS_LOCK)
903 /* previous tune didn't lock - enable LNA and tune again */
904 sms_board_lna_control(client->coredev, 1);
907 return smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
911 static int smsdvb_isdbt_set_frontend(struct dvb_frontend *fe)
913 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
914 struct smsdvb_client_t *client =
915 container_of(fe, struct smsdvb_client_t, frontend);
916 int board_id = smscore_get_board_id(client->coredev);
917 struct sms_board *board = sms_get_board(board_id);
918 enum sms_device_type_st type = board->type;
922 struct sms_msg_hdr msg;
926 fe->dtv_property_cache.delivery_system = SYS_ISDBT;
928 msg.msg.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
929 msg.msg.msg_dst_id = HIF_TASK;
930 msg.msg.msg_flags = 0;
931 msg.msg.msg_type = MSG_SMS_ISDBT_TUNE_REQ;
932 msg.msg.msg_length = sizeof(msg);
934 if (c->isdbt_sb_segment_idx == -1)
935 c->isdbt_sb_segment_idx = 0;
937 if (!c->isdbt_layer_enabled)
938 c->isdbt_layer_enabled = 7;
940 msg.Data[0] = c->frequency;
941 msg.Data[1] = BW_ISDBT_1SEG;
942 msg.Data[2] = 12000000;
943 msg.Data[3] = c->isdbt_sb_segment_idx;
945 if (c->isdbt_partial_reception) {
946 if ((type == SMS_PELE || type == SMS_RIO) &&
947 c->isdbt_sb_segment_count > 3)
948 msg.Data[1] = BW_ISDBT_13SEG;
949 else if (c->isdbt_sb_segment_count > 1)
950 msg.Data[1] = BW_ISDBT_3SEG;
951 } else if (type == SMS_PELE || type == SMS_RIO)
952 msg.Data[1] = BW_ISDBT_13SEG;
954 c->bandwidth_hz = 6000000;
956 sms_info("%s: freq %d segwidth %d segindex %d", __func__,
957 c->frequency, c->isdbt_sb_segment_count,
958 c->isdbt_sb_segment_idx);
960 /* Disable LNA, if any. An error is returned if no LNA is present */
961 ret = sms_board_lna_control(client->coredev, 0);
965 /* tune with LNA off at first */
966 ret = smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
969 smsdvb_read_status(fe, &status);
971 if (status & FE_HAS_LOCK)
974 /* previous tune didn't lock - enable LNA and tune again */
975 sms_board_lna_control(client->coredev, 1);
977 return smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
981 static int smsdvb_set_frontend(struct dvb_frontend *fe)
983 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
984 struct smsdvb_client_t *client =
985 container_of(fe, struct smsdvb_client_t, frontend);
986 struct smscore_device_t *coredev = client->coredev;
988 smsdvb_stats_not_ready(fe);
989 c->strength.stat[0].uvalue = 0;
990 c->cnr.stat[0].uvalue = 0;
992 client->has_tuned = false;
994 switch (smscore_get_device_mode(coredev)) {
995 case DEVICE_MODE_DVBT:
996 case DEVICE_MODE_DVBT_BDA:
997 return smsdvb_dvbt_set_frontend(fe);
998 case DEVICE_MODE_ISDBT:
999 case DEVICE_MODE_ISDBT_BDA:
1000 return smsdvb_isdbt_set_frontend(fe);
1006 /* Nothing to do here, as stats are automatically updated */
1007 static int smsdvb_get_frontend(struct dvb_frontend *fe)
1012 static int smsdvb_init(struct dvb_frontend *fe)
1014 struct smsdvb_client_t *client =
1015 container_of(fe, struct smsdvb_client_t, frontend);
1017 sms_board_power(client->coredev, 1);
1019 sms_board_dvb3_event(client, DVB3_EVENT_INIT);
1023 static int smsdvb_sleep(struct dvb_frontend *fe)
1025 struct smsdvb_client_t *client =
1026 container_of(fe, struct smsdvb_client_t, frontend);
1028 sms_board_led_feedback(client->coredev, SMS_LED_OFF);
1029 sms_board_power(client->coredev, 0);
1031 sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
1036 static void smsdvb_release(struct dvb_frontend *fe)
1041 static struct dvb_frontend_ops smsdvb_fe_ops = {
1043 .name = "Siano Mobile Digital MDTV Receiver",
1044 .frequency_min = 44250000,
1045 .frequency_max = 867250000,
1046 .frequency_stepsize = 250000,
1047 .caps = FE_CAN_INVERSION_AUTO |
1048 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1049 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1050 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1051 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
1052 FE_CAN_GUARD_INTERVAL_AUTO |
1054 FE_CAN_HIERARCHY_AUTO,
1057 .release = smsdvb_release,
1059 .set_frontend = smsdvb_set_frontend,
1060 .get_frontend = smsdvb_get_frontend,
1061 .get_tune_settings = smsdvb_get_tune_settings,
1063 .read_status = smsdvb_read_status,
1064 .read_ber = smsdvb_read_ber,
1065 .read_signal_strength = smsdvb_read_signal_strength,
1066 .read_snr = smsdvb_read_snr,
1067 .read_ucblocks = smsdvb_read_ucblocks,
1069 .init = smsdvb_init,
1070 .sleep = smsdvb_sleep,
1073 static int smsdvb_hotplug(struct smscore_device_t *coredev,
1074 struct device *device, int arrival)
1076 struct smsclient_params_t params;
1077 struct smsdvb_client_t *client;
1080 /* device removal handled by onremove callback */
1083 client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
1085 sms_err("kmalloc() failed");
1089 /* register dvb adapter */
1090 rc = dvb_register_adapter(&client->adapter,
1092 smscore_get_board_id(coredev))->name,
1093 THIS_MODULE, device, adapter_nr);
1095 sms_err("dvb_register_adapter() failed %d", rc);
1099 /* init dvb demux */
1100 client->demux.dmx.capabilities = DMX_TS_FILTERING;
1101 client->demux.filternum = 32; /* todo: nova ??? */
1102 client->demux.feednum = 32;
1103 client->demux.start_feed = smsdvb_start_feed;
1104 client->demux.stop_feed = smsdvb_stop_feed;
1106 rc = dvb_dmx_init(&client->demux);
1108 sms_err("dvb_dmx_init failed %d", rc);
1113 client->dmxdev.filternum = 32;
1114 client->dmxdev.demux = &client->demux.dmx;
1115 client->dmxdev.capabilities = 0;
1117 rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
1119 sms_err("dvb_dmxdev_init failed %d", rc);
1123 /* init and register frontend */
1124 memcpy(&client->frontend.ops, &smsdvb_fe_ops,
1125 sizeof(struct dvb_frontend_ops));
1127 switch (smscore_get_device_mode(coredev)) {
1128 case DEVICE_MODE_DVBT:
1129 case DEVICE_MODE_DVBT_BDA:
1130 client->frontend.ops.delsys[0] = SYS_DVBT;
1132 case DEVICE_MODE_ISDBT:
1133 case DEVICE_MODE_ISDBT_BDA:
1134 client->frontend.ops.delsys[0] = SYS_ISDBT;
1138 rc = dvb_register_frontend(&client->adapter, &client->frontend);
1140 sms_err("frontend registration failed %d", rc);
1141 goto frontend_error;
1144 params.initial_id = 1;
1145 params.data_type = MSG_SMS_DVBT_BDA_DATA;
1146 params.onresponse_handler = smsdvb_onresponse;
1147 params.onremove_handler = smsdvb_onremove;
1148 params.context = client;
1150 rc = smscore_register_client(coredev, ¶ms, &client->smsclient);
1152 sms_err("smscore_register_client() failed %d", rc);
1156 client->coredev = coredev;
1158 init_completion(&client->tune_done);
1159 init_completion(&client->stats_done);
1161 kmutex_lock(&g_smsdvb_clientslock);
1163 list_add(&client->entry, &g_smsdvb_clients);
1165 kmutex_unlock(&g_smsdvb_clientslock);
1167 client->event_fe_state = -1;
1168 client->event_unc_state = -1;
1169 sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
1171 sms_info("success");
1172 sms_board_setup(coredev);
1174 if (smsdvb_debugfs_create(client) < 0)
1175 sms_info("failed to create debugfs node");
1180 dvb_unregister_frontend(&client->frontend);
1183 dvb_dmxdev_release(&client->dmxdev);
1186 dvb_dmx_release(&client->demux);
1189 dvb_unregister_adapter(&client->adapter);
1196 static int __init smsdvb_module_init(void)
1200 INIT_LIST_HEAD(&g_smsdvb_clients);
1201 kmutex_init(&g_smsdvb_clientslock);
1203 smsdvb_debugfs_register();
1205 rc = smscore_register_hotplug(smsdvb_hotplug);
1212 static void __exit smsdvb_module_exit(void)
1214 smscore_unregister_hotplug(smsdvb_hotplug);
1216 kmutex_lock(&g_smsdvb_clientslock);
1218 while (!list_empty(&g_smsdvb_clients))
1219 smsdvb_unregister_client((struct smsdvb_client_t *)g_smsdvb_clients.next);
1221 smsdvb_debugfs_unregister();
1223 kmutex_unlock(&g_smsdvb_clientslock);
1226 module_init(smsdvb_module_init);
1227 module_exit(smsdvb_module_exit);
1229 MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
1230 MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
1231 MODULE_LICENSE("GPL");