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>
28 #include "dvb_demux.h"
29 #include "dvb_frontend.h"
31 #include "smscoreapi.h"
32 #include "sms-cards.h"
36 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
38 static struct list_head g_smsdvb_clients;
39 static struct mutex g_smsdvb_clientslock;
42 module_param_named(debug, sms_dbg, int, 0644);
43 MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
46 u32 sms_to_guard_interval_table[] = {
47 [0] = GUARD_INTERVAL_1_32,
48 [1] = GUARD_INTERVAL_1_16,
49 [2] = GUARD_INTERVAL_1_8,
50 [3] = GUARD_INTERVAL_1_4,
53 u32 sms_to_code_rate_table[] = {
62 u32 sms_to_hierarchy_table[] = {
69 u32 sms_to_modulation_table[] = {
77 /* Events that may come from DVB v3 adapter */
78 static void sms_board_dvb3_event(struct smsdvb_client_t *client,
79 enum SMS_DVB3_EVENTS event) {
81 struct smscore_device_t *coredev = client->coredev;
84 sms_debug("DVB3_EVENT_INIT");
85 sms_board_event(coredev, BOARD_EVENT_BIND);
87 case DVB3_EVENT_SLEEP:
88 sms_debug("DVB3_EVENT_SLEEP");
89 sms_board_event(coredev, BOARD_EVENT_POWER_SUSPEND);
91 case DVB3_EVENT_HOTPLUG:
92 sms_debug("DVB3_EVENT_HOTPLUG");
93 sms_board_event(coredev, BOARD_EVENT_POWER_INIT);
95 case DVB3_EVENT_FE_LOCK:
96 if (client->event_fe_state != DVB3_EVENT_FE_LOCK) {
97 client->event_fe_state = DVB3_EVENT_FE_LOCK;
98 sms_debug("DVB3_EVENT_FE_LOCK");
99 sms_board_event(coredev, BOARD_EVENT_FE_LOCK);
102 case DVB3_EVENT_FE_UNLOCK:
103 if (client->event_fe_state != DVB3_EVENT_FE_UNLOCK) {
104 client->event_fe_state = DVB3_EVENT_FE_UNLOCK;
105 sms_debug("DVB3_EVENT_FE_UNLOCK");
106 sms_board_event(coredev, BOARD_EVENT_FE_UNLOCK);
109 case DVB3_EVENT_UNC_OK:
110 if (client->event_unc_state != DVB3_EVENT_UNC_OK) {
111 client->event_unc_state = DVB3_EVENT_UNC_OK;
112 sms_debug("DVB3_EVENT_UNC_OK");
113 sms_board_event(coredev, BOARD_EVENT_MULTIPLEX_OK);
116 case DVB3_EVENT_UNC_ERR:
117 if (client->event_unc_state != DVB3_EVENT_UNC_ERR) {
118 client->event_unc_state = DVB3_EVENT_UNC_ERR;
119 sms_debug("DVB3_EVENT_UNC_ERR");
120 sms_board_event(coredev, BOARD_EVENT_MULTIPLEX_ERRORS);
125 sms_err("Unknown dvb3 api event");
130 static void smsdvb_stats_not_ready(struct dvb_frontend *fe)
132 struct smsdvb_client_t *client =
133 container_of(fe, struct smsdvb_client_t, frontend);
134 struct smscore_device_t *coredev = client->coredev;
135 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
138 switch (smscore_get_device_mode(coredev)) {
139 case DEVICE_MODE_ISDBT:
140 case DEVICE_MODE_ISDBT_BDA:
149 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
150 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
152 /* Per-layer stats */
153 c->post_bit_error.len = n_layers;
154 c->post_bit_count.len = n_layers;
155 c->block_error.len = n_layers;
156 c->block_count.len = n_layers;
159 * Put all of them at FE_SCALE_NOT_AVAILABLE. They're dynamically
160 * changed when the stats become available.
162 for (i = 0; i < n_layers; i++) {
163 c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
164 c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
165 c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
166 c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
170 static inline int sms_to_mode(u32 mode)
174 return TRANSMISSION_MODE_2K;
176 return TRANSMISSION_MODE_4K;
178 return TRANSMISSION_MODE_8K;
180 return TRANSMISSION_MODE_AUTO;
183 static inline int sms_to_status(u32 is_demod_locked, u32 is_rf_locked)
186 return FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
187 FE_HAS_SYNC | FE_HAS_LOCK;
190 return FE_HAS_SIGNAL | FE_HAS_CARRIER;
195 static inline u32 sms_to_bw(u32 value)
197 return value * 1000000;
200 #define convert_from_table(value, table, defval) ({ \
202 if (value < ARRAY_SIZE(table)) \
203 __ret = table[value]; \
209 #define sms_to_guard_interval(value) \
210 convert_from_table(value, sms_to_guard_interval_table, \
211 GUARD_INTERVAL_AUTO);
213 #define sms_to_code_rate(value) \
214 convert_from_table(value, sms_to_code_rate_table, \
217 #define sms_to_hierarchy(value) \
218 convert_from_table(value, sms_to_hierarchy_table, \
221 #define sms_to_modulation(value) \
222 convert_from_table(value, sms_to_modulation_table, \
225 static void smsdvb_update_tx_params(struct smsdvb_client_t *client,
226 struct sms_tx_stats *p)
228 struct dvb_frontend *fe = &client->frontend;
229 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
231 c->frequency = p->frequency;
232 client->fe_status = sms_to_status(p->is_demod_locked, 0);
233 c->bandwidth_hz = sms_to_bw(p->bandwidth);
234 c->transmission_mode = sms_to_mode(p->transmission_mode);
235 c->guard_interval = sms_to_guard_interval(p->guard_interval);
236 c->code_rate_HP = sms_to_code_rate(p->code_rate);
237 c->code_rate_LP = sms_to_code_rate(p->lp_code_rate);
238 c->hierarchy = sms_to_hierarchy(p->hierarchy);
239 c->modulation = sms_to_modulation(p->constellation);
242 static void smsdvb_update_per_slices(struct smsdvb_client_t *client,
243 struct RECEPTION_STATISTICS_PER_SLICES_S *p)
245 struct dvb_frontend *fe = &client->frontend;
246 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
248 client->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
249 c->modulation = sms_to_modulation(p->constellation);
251 /* signal Strength, in DBm */
252 c->strength.stat[0].uvalue = p->in_band_power * 1000;
254 /* Carrier to noise ratio, in DB */
255 c->cnr.stat[0].svalue = p->snr * 1000;
257 /* PER/BER requires demod lock */
258 if (!p->is_demod_locked)
262 client->last_per = c->block_error.stat[0].uvalue;
263 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
264 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
265 c->block_error.stat[0].uvalue += p->ets_packets;
266 c->block_count.stat[0].uvalue += p->ets_packets + p->ts_packets;
269 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
270 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
271 c->post_bit_error.stat[0].uvalue += p->ber_error_count;
272 c->post_bit_count.stat[0].uvalue += p->ber_bit_count;
275 client->legacy_per = (p->ets_packets * 65535) /
276 (p->ts_packets + p->ets_packets);
279 static void smsdvb_update_dvb_stats(struct smsdvb_client_t *client,
282 struct dvb_frontend *fe = &client->frontend;
283 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
285 if (client->prt_dvb_stats)
286 client->prt_dvb_stats(client->debug_data, p);
288 client->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
290 /* Update DVB modulation parameters */
291 c->frequency = p->frequency;
292 client->fe_status = sms_to_status(p->is_demod_locked, 0);
293 c->bandwidth_hz = sms_to_bw(p->bandwidth);
294 c->transmission_mode = sms_to_mode(p->transmission_mode);
295 c->guard_interval = sms_to_guard_interval(p->guard_interval);
296 c->code_rate_HP = sms_to_code_rate(p->code_rate);
297 c->code_rate_LP = sms_to_code_rate(p->lp_code_rate);
298 c->hierarchy = sms_to_hierarchy(p->hierarchy);
299 c->modulation = sms_to_modulation(p->constellation);
301 /* update reception data */
302 c->lna = p->is_external_lna_on ? 1 : 0;
304 /* Carrier to noise ratio, in DB */
305 c->cnr.stat[0].svalue = p->SNR * 1000;
307 /* signal Strength, in DBm */
308 c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
310 /* PER/BER requires demod lock */
311 if (!p->is_demod_locked)
315 client->last_per = c->block_error.stat[0].uvalue;
316 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
317 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
318 c->block_error.stat[0].uvalue += p->error_ts_packets;
319 c->block_count.stat[0].uvalue += p->total_ts_packets;
322 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
323 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
324 c->post_bit_error.stat[0].uvalue += p->ber_error_count;
325 c->post_bit_count.stat[0].uvalue += p->ber_bit_count;
328 client->legacy_ber = p->ber;
331 static void smsdvb_update_isdbt_stats(struct smsdvb_client_t *client,
332 struct sms_isdbt_stats *p)
334 struct dvb_frontend *fe = &client->frontend;
335 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
336 struct sms_isdbt_layer_stats *lr;
339 if (client->prt_isdb_stats)
340 client->prt_isdb_stats(client->debug_data, p);
342 client->fe_status = sms_to_status(p->is_demod_locked, p->is_rf_locked);
345 * Firmware 2.1 seems to report only lock status and
346 * signal strength. The signal strength indicator is at the
349 if (p->statistics_type == 0) {
350 c->strength.stat[0].uvalue = ((s32)p->transmission_mode) * 1000;
351 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
355 /* Update ISDB-T transmission parameters */
356 c->frequency = p->frequency;
357 c->bandwidth_hz = sms_to_bw(p->bandwidth);
358 c->transmission_mode = sms_to_mode(p->transmission_mode);
359 c->guard_interval = sms_to_guard_interval(p->guard_interval);
360 c->isdbt_partial_reception = p->partial_reception ? 1 : 0;
361 n_layers = p->num_of_layers;
366 c->isdbt_layer_enabled = 0;
368 /* update reception data */
369 c->lna = p->is_external_lna_on ? 1 : 0;
371 /* Carrier to noise ratio, in DB */
372 c->cnr.stat[0].svalue = p->SNR * 1000;
374 /* signal Strength, in DBm */
375 c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
377 /* PER/BER and per-layer stats require demod lock */
378 if (!p->is_demod_locked)
381 client->last_per = c->block_error.stat[0].uvalue;
383 /* Clears global counters, as the code below will sum it again */
384 c->block_error.stat[0].uvalue = 0;
385 c->block_count.stat[0].uvalue = 0;
386 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
387 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
388 c->post_bit_error.stat[0].uvalue = 0;
389 c->post_bit_count.stat[0].uvalue = 0;
390 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
391 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
393 for (i = 0; i < n_layers; i++) {
394 lr = &p->layer_info[i];
396 /* Update per-layer transmission parameters */
397 if (lr->number_of_segments > 0 && lr->number_of_segments < 13) {
398 c->isdbt_layer_enabled |= 1 << i;
399 c->layer[i].segment_count = lr->number_of_segments;
403 c->layer[i].modulation = sms_to_modulation(lr->constellation);
406 c->block_error.stat[i + 1].scale = FE_SCALE_COUNTER;
407 c->block_count.stat[i + 1].scale = FE_SCALE_COUNTER;
408 c->block_error.stat[i + 1].uvalue += lr->error_ts_packets;
409 c->block_count.stat[i + 1].uvalue += lr->total_ts_packets;
411 /* Update global PER counter */
412 c->block_error.stat[0].uvalue += lr->error_ts_packets;
413 c->block_count.stat[0].uvalue += lr->total_ts_packets;
416 c->post_bit_error.stat[i + 1].scale = FE_SCALE_COUNTER;
417 c->post_bit_count.stat[i + 1].scale = FE_SCALE_COUNTER;
418 c->post_bit_error.stat[i + 1].uvalue += lr->ber_error_count;
419 c->post_bit_count.stat[i + 1].uvalue += lr->ber_bit_count;
421 /* Update global BER counter */
422 c->post_bit_error.stat[0].uvalue += lr->ber_error_count;
423 c->post_bit_count.stat[0].uvalue += lr->ber_bit_count;
427 static void smsdvb_update_isdbt_stats_ex(struct smsdvb_client_t *client,
428 struct sms_isdbt_stats_ex *p)
430 struct dvb_frontend *fe = &client->frontend;
431 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
432 struct sms_isdbt_layer_stats *lr;
435 if (client->prt_isdb_stats_ex)
436 client->prt_isdb_stats_ex(client->debug_data, p);
438 /* Update ISDB-T transmission parameters */
439 c->frequency = p->frequency;
440 client->fe_status = sms_to_status(p->is_demod_locked, 0);
441 c->bandwidth_hz = sms_to_bw(p->bandwidth);
442 c->transmission_mode = sms_to_mode(p->transmission_mode);
443 c->guard_interval = sms_to_guard_interval(p->guard_interval);
444 c->isdbt_partial_reception = p->partial_reception ? 1 : 0;
445 n_layers = p->num_of_layers;
450 c->isdbt_layer_enabled = 0;
452 /* update reception data */
453 c->lna = p->is_external_lna_on ? 1 : 0;
455 /* Carrier to noise ratio, in DB */
456 c->cnr.stat[0].svalue = p->SNR * 1000;
458 /* signal Strength, in DBm */
459 c->strength.stat[0].uvalue = p->in_band_pwr * 1000;
461 /* PER/BER and per-layer stats require demod lock */
462 if (!p->is_demod_locked)
465 client->last_per = c->block_error.stat[0].uvalue;
467 /* Clears global counters, as the code below will sum it again */
468 c->block_error.stat[0].uvalue = 0;
469 c->block_count.stat[0].uvalue = 0;
470 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
471 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
472 c->post_bit_error.stat[0].uvalue = 0;
473 c->post_bit_count.stat[0].uvalue = 0;
474 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
475 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
477 c->post_bit_error.len = n_layers + 1;
478 c->post_bit_count.len = n_layers + 1;
479 c->block_error.len = n_layers + 1;
480 c->block_count.len = n_layers + 1;
481 for (i = 0; i < n_layers; i++) {
482 lr = &p->layer_info[i];
484 /* Update per-layer transmission parameters */
485 if (lr->number_of_segments > 0 && lr->number_of_segments < 13) {
486 c->isdbt_layer_enabled |= 1 << i;
487 c->layer[i].segment_count = lr->number_of_segments;
491 c->layer[i].modulation = sms_to_modulation(lr->constellation);
494 c->block_error.stat[i + 1].scale = FE_SCALE_COUNTER;
495 c->block_count.stat[i + 1].scale = FE_SCALE_COUNTER;
496 c->block_error.stat[i + 1].uvalue += lr->error_ts_packets;
497 c->block_count.stat[i + 1].uvalue += lr->total_ts_packets;
499 /* Update global PER counter */
500 c->block_error.stat[0].uvalue += lr->error_ts_packets;
501 c->block_count.stat[0].uvalue += lr->total_ts_packets;
504 c->post_bit_error.stat[i + 1].scale = FE_SCALE_COUNTER;
505 c->post_bit_count.stat[i + 1].scale = FE_SCALE_COUNTER;
506 c->post_bit_error.stat[i + 1].uvalue += lr->ber_error_count;
507 c->post_bit_count.stat[i + 1].uvalue += lr->ber_bit_count;
509 /* Update global ber counter */
510 c->post_bit_error.stat[0].uvalue += lr->ber_error_count;
511 c->post_bit_count.stat[0].uvalue += lr->ber_bit_count;
515 static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
517 struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
518 struct sms_msg_hdr *phdr = (struct sms_msg_hdr *) (((u8 *) cb->p)
521 struct dvb_frontend *fe = &client->frontend;
522 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
523 bool is_status_update = false;
525 switch (phdr->msg_type) {
526 case MSG_SMS_DVBT_BDA_DATA:
528 * Only feed data to dvb demux if are there any feed listening
529 * to it and if the device has tuned
531 if (client->feed_users && client->has_tuned)
532 dvb_dmx_swfilter(&client->demux, p,
533 cb->size - sizeof(struct sms_msg_hdr));
536 case MSG_SMS_RF_TUNE_RES:
537 case MSG_SMS_ISDBT_TUNE_RES:
538 complete(&client->tune_done);
541 case MSG_SMS_SIGNAL_DETECTED_IND:
542 client->fe_status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
543 FE_HAS_VITERBI | FE_HAS_SYNC |
546 is_status_update = true;
549 case MSG_SMS_NO_SIGNAL_IND:
550 client->fe_status = 0;
552 is_status_update = true;
555 case MSG_SMS_TRANSMISSION_IND:
556 smsdvb_update_tx_params(client, p);
558 is_status_update = true;
561 case MSG_SMS_HO_PER_SLICES_IND:
562 smsdvb_update_per_slices(client, p);
564 is_status_update = true;
567 case MSG_SMS_GET_STATISTICS_RES:
568 switch (smscore_get_device_mode(client->coredev)) {
569 case DEVICE_MODE_ISDBT:
570 case DEVICE_MODE_ISDBT_BDA:
571 smsdvb_update_isdbt_stats(client, p);
574 /* Skip sms_msg_statistics_info:request_result field */
575 smsdvb_update_dvb_stats(client, p + sizeof(u32));
578 is_status_update = true;
581 /* Only for ISDB-T */
582 case MSG_SMS_GET_STATISTICS_EX_RES:
583 /* Skip sms_msg_statistics_info:request_result field? */
584 smsdvb_update_isdbt_stats_ex(client, p + sizeof(u32));
585 is_status_update = true;
588 sms_info("message not handled");
590 smscore_putbuffer(client->coredev, cb);
592 if (is_status_update) {
593 if (client->fe_status & FE_HAS_LOCK) {
594 sms_board_dvb3_event(client, DVB3_EVENT_FE_LOCK);
595 if (client->last_per == c->block_error.stat[0].uvalue)
596 sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
598 sms_board_dvb3_event(client, DVB3_EVENT_UNC_ERR);
599 client->has_tuned = true;
601 smsdvb_stats_not_ready(fe);
602 client->has_tuned = false;
603 sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
605 complete(&client->stats_done);
611 static void smsdvb_unregister_client(struct smsdvb_client_t *client)
613 /* must be called under clientslock */
615 list_del(&client->entry);
617 smsdvb_debugfs_release(client);
618 smscore_unregister_client(client->smsclient);
619 dvb_unregister_frontend(&client->frontend);
620 dvb_dmxdev_release(&client->dmxdev);
621 dvb_dmx_release(&client->demux);
622 dvb_unregister_adapter(&client->adapter);
626 static void smsdvb_onremove(void *context)
628 kmutex_lock(&g_smsdvb_clientslock);
630 smsdvb_unregister_client((struct smsdvb_client_t *) context);
632 kmutex_unlock(&g_smsdvb_clientslock);
635 static int smsdvb_start_feed(struct dvb_demux_feed *feed)
637 struct smsdvb_client_t *client =
638 container_of(feed->demux, struct smsdvb_client_t, demux);
639 struct sms_msg_data pid_msg;
641 sms_debug("add pid %d(%x)",
642 feed->pid, feed->pid);
644 client->feed_users++;
646 pid_msg.x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
647 pid_msg.x_msg_header.msg_dst_id = HIF_TASK;
648 pid_msg.x_msg_header.msg_flags = 0;
649 pid_msg.x_msg_header.msg_type = MSG_SMS_ADD_PID_FILTER_REQ;
650 pid_msg.x_msg_header.msg_length = sizeof(pid_msg);
651 pid_msg.msg_data[0] = feed->pid;
653 return smsclient_sendrequest(client->smsclient,
654 &pid_msg, sizeof(pid_msg));
657 static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
659 struct smsdvb_client_t *client =
660 container_of(feed->demux, struct smsdvb_client_t, demux);
661 struct sms_msg_data pid_msg;
663 sms_debug("remove pid %d(%x)",
664 feed->pid, feed->pid);
666 client->feed_users--;
668 pid_msg.x_msg_header.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
669 pid_msg.x_msg_header.msg_dst_id = HIF_TASK;
670 pid_msg.x_msg_header.msg_flags = 0;
671 pid_msg.x_msg_header.msg_type = MSG_SMS_REMOVE_PID_FILTER_REQ;
672 pid_msg.x_msg_header.msg_length = sizeof(pid_msg);
673 pid_msg.msg_data[0] = feed->pid;
675 return smsclient_sendrequest(client->smsclient,
676 &pid_msg, sizeof(pid_msg));
679 static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
680 void *buffer, size_t size,
681 struct completion *completion)
685 rc = smsclient_sendrequest(client->smsclient, buffer, size);
689 return wait_for_completion_timeout(completion,
690 msecs_to_jiffies(2000)) ?
694 static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
697 struct sms_msg_hdr msg;
699 /* Don't request stats too fast */
700 if (client->get_stats_jiffies &&
701 (!time_after(jiffies, client->get_stats_jiffies)))
703 client->get_stats_jiffies = jiffies + msecs_to_jiffies(100);
705 msg.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
706 msg.msg_dst_id = HIF_TASK;
708 msg.msg_length = sizeof(msg);
710 switch (smscore_get_device_mode(client->coredev)) {
711 case DEVICE_MODE_ISDBT:
712 case DEVICE_MODE_ISDBT_BDA:
714 * Check for firmware version, to avoid breaking for old cards
716 if (client->coredev->fw_version >= 0x800)
717 msg.msg_type = MSG_SMS_GET_STATISTICS_EX_REQ;
719 msg.msg_type = MSG_SMS_GET_STATISTICS_REQ;
722 msg.msg_type = MSG_SMS_GET_STATISTICS_REQ;
725 rc = smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
726 &client->stats_done);
731 static inline int led_feedback(struct smsdvb_client_t *client)
733 if (!(client->fe_status & FE_HAS_LOCK))
734 return sms_board_led_feedback(client->coredev, SMS_LED_OFF);
736 return sms_board_led_feedback(client->coredev,
737 (client->legacy_ber == 0) ?
738 SMS_LED_HI : SMS_LED_LO);
741 static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
744 struct smsdvb_client_t *client;
745 client = container_of(fe, struct smsdvb_client_t, frontend);
747 rc = smsdvb_send_statistics_request(client);
749 *stat = client->fe_status;
751 led_feedback(client);
756 static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
759 struct smsdvb_client_t *client;
761 client = container_of(fe, struct smsdvb_client_t, frontend);
763 rc = smsdvb_send_statistics_request(client);
765 *ber = client->legacy_ber;
767 led_feedback(client);
772 static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
774 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
776 s32 power = (s32) c->strength.stat[0].uvalue;
777 struct smsdvb_client_t *client;
779 client = container_of(fe, struct smsdvb_client_t, frontend);
781 rc = smsdvb_send_statistics_request(client);
785 else if (power > -29)
788 *strength = (power + 95) * 65535 / 66;
790 led_feedback(client);
795 static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
797 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
799 struct smsdvb_client_t *client;
801 client = container_of(fe, struct smsdvb_client_t, frontend);
803 rc = smsdvb_send_statistics_request(client);
805 /* Preferred scale for SNR with legacy API: 0.1 dB */
806 *snr = c->cnr.stat[0].svalue / 100;
808 led_feedback(client);
813 static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
816 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
817 struct smsdvb_client_t *client;
819 client = container_of(fe, struct smsdvb_client_t, frontend);
821 rc = smsdvb_send_statistics_request(client);
823 *ucblocks = c->block_error.stat[0].uvalue;
825 led_feedback(client);
830 static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
831 struct dvb_frontend_tune_settings *tune)
835 tune->min_delay_ms = 400;
836 tune->step_size = 250000;
841 static int smsdvb_dvbt_set_frontend(struct dvb_frontend *fe)
843 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
844 struct smsdvb_client_t *client =
845 container_of(fe, struct smsdvb_client_t, frontend);
848 struct sms_msg_hdr msg;
854 client->fe_status = 0;
855 client->event_fe_state = -1;
856 client->event_unc_state = -1;
857 fe->dtv_property_cache.delivery_system = SYS_DVBT;
859 msg.msg.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
860 msg.msg.msg_dst_id = HIF_TASK;
861 msg.msg.msg_flags = 0;
862 msg.msg.msg_type = MSG_SMS_RF_TUNE_REQ;
863 msg.msg.msg_length = sizeof(msg);
864 msg.Data[0] = c->frequency;
865 msg.Data[2] = 12000000;
867 sms_info("%s: freq %d band %d", __func__, c->frequency,
870 switch (c->bandwidth_hz / 1000000) {
872 msg.Data[1] = BW_8_MHZ;
875 msg.Data[1] = BW_7_MHZ;
878 msg.Data[1] = BW_6_MHZ;
885 /* Disable LNA, if any. An error is returned if no LNA is present */
886 ret = sms_board_lna_control(client->coredev, 0);
890 /* tune with LNA off at first */
891 ret = smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
894 smsdvb_read_status(fe, &status);
896 if (status & FE_HAS_LOCK)
899 /* previous tune didn't lock - enable LNA and tune again */
900 sms_board_lna_control(client->coredev, 1);
903 return smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
907 static int smsdvb_isdbt_set_frontend(struct dvb_frontend *fe)
909 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
910 struct smsdvb_client_t *client =
911 container_of(fe, struct smsdvb_client_t, frontend);
912 int board_id = smscore_get_board_id(client->coredev);
913 struct sms_board *board = sms_get_board(board_id);
914 enum sms_device_type_st type = board->type;
918 struct sms_msg_hdr msg;
922 fe->dtv_property_cache.delivery_system = SYS_ISDBT;
924 msg.msg.msg_src_id = DVBT_BDA_CONTROL_MSG_ID;
925 msg.msg.msg_dst_id = HIF_TASK;
926 msg.msg.msg_flags = 0;
927 msg.msg.msg_type = MSG_SMS_ISDBT_TUNE_REQ;
928 msg.msg.msg_length = sizeof(msg);
930 if (c->isdbt_sb_segment_idx == -1)
931 c->isdbt_sb_segment_idx = 0;
933 if (!c->isdbt_layer_enabled)
934 c->isdbt_layer_enabled = 7;
936 msg.Data[0] = c->frequency;
937 msg.Data[1] = BW_ISDBT_1SEG;
938 msg.Data[2] = 12000000;
939 msg.Data[3] = c->isdbt_sb_segment_idx;
941 if (c->isdbt_partial_reception) {
942 if ((type == SMS_PELE || type == SMS_RIO) &&
943 c->isdbt_sb_segment_count > 3)
944 msg.Data[1] = BW_ISDBT_13SEG;
945 else if (c->isdbt_sb_segment_count > 1)
946 msg.Data[1] = BW_ISDBT_3SEG;
947 } else if (type == SMS_PELE || type == SMS_RIO)
948 msg.Data[1] = BW_ISDBT_13SEG;
950 c->bandwidth_hz = 6000000;
952 sms_info("%s: freq %d segwidth %d segindex %d", __func__,
953 c->frequency, c->isdbt_sb_segment_count,
954 c->isdbt_sb_segment_idx);
956 /* Disable LNA, if any. An error is returned if no LNA is present */
957 ret = sms_board_lna_control(client->coredev, 0);
961 /* tune with LNA off at first */
962 ret = smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
965 smsdvb_read_status(fe, &status);
967 if (status & FE_HAS_LOCK)
970 /* previous tune didn't lock - enable LNA and tune again */
971 sms_board_lna_control(client->coredev, 1);
973 return smsdvb_sendrequest_and_wait(client, &msg, sizeof(msg),
977 static int smsdvb_set_frontend(struct dvb_frontend *fe)
979 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
980 struct smsdvb_client_t *client =
981 container_of(fe, struct smsdvb_client_t, frontend);
982 struct smscore_device_t *coredev = client->coredev;
984 smsdvb_stats_not_ready(fe);
985 c->strength.stat[0].uvalue = 0;
986 c->cnr.stat[0].uvalue = 0;
988 client->has_tuned = false;
990 switch (smscore_get_device_mode(coredev)) {
991 case DEVICE_MODE_DVBT:
992 case DEVICE_MODE_DVBT_BDA:
993 return smsdvb_dvbt_set_frontend(fe);
994 case DEVICE_MODE_ISDBT:
995 case DEVICE_MODE_ISDBT_BDA:
996 return smsdvb_isdbt_set_frontend(fe);
1002 /* Nothing to do here, as stats are automatically updated */
1003 static int smsdvb_get_frontend(struct dvb_frontend *fe)
1008 static int smsdvb_init(struct dvb_frontend *fe)
1010 struct smsdvb_client_t *client =
1011 container_of(fe, struct smsdvb_client_t, frontend);
1013 sms_board_power(client->coredev, 1);
1015 sms_board_dvb3_event(client, DVB3_EVENT_INIT);
1019 static int smsdvb_sleep(struct dvb_frontend *fe)
1021 struct smsdvb_client_t *client =
1022 container_of(fe, struct smsdvb_client_t, frontend);
1024 sms_board_led_feedback(client->coredev, SMS_LED_OFF);
1025 sms_board_power(client->coredev, 0);
1027 sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
1032 static void smsdvb_release(struct dvb_frontend *fe)
1037 static struct dvb_frontend_ops smsdvb_fe_ops = {
1039 .name = "Siano Mobile Digital MDTV Receiver",
1040 .frequency_min = 44250000,
1041 .frequency_max = 867250000,
1042 .frequency_stepsize = 250000,
1043 .caps = FE_CAN_INVERSION_AUTO |
1044 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1045 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1046 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1047 FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
1048 FE_CAN_GUARD_INTERVAL_AUTO |
1050 FE_CAN_HIERARCHY_AUTO,
1053 .release = smsdvb_release,
1055 .set_frontend = smsdvb_set_frontend,
1056 .get_frontend = smsdvb_get_frontend,
1057 .get_tune_settings = smsdvb_get_tune_settings,
1059 .read_status = smsdvb_read_status,
1060 .read_ber = smsdvb_read_ber,
1061 .read_signal_strength = smsdvb_read_signal_strength,
1062 .read_snr = smsdvb_read_snr,
1063 .read_ucblocks = smsdvb_read_ucblocks,
1065 .init = smsdvb_init,
1066 .sleep = smsdvb_sleep,
1069 static int smsdvb_hotplug(struct smscore_device_t *coredev,
1070 struct device *device, int arrival)
1072 struct smsclient_params_t params;
1073 struct smsdvb_client_t *client;
1076 /* device removal handled by onremove callback */
1079 client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
1081 sms_err("kmalloc() failed");
1085 /* register dvb adapter */
1086 rc = dvb_register_adapter(&client->adapter,
1088 smscore_get_board_id(coredev))->name,
1089 THIS_MODULE, device, adapter_nr);
1091 sms_err("dvb_register_adapter() failed %d", rc);
1095 /* init dvb demux */
1096 client->demux.dmx.capabilities = DMX_TS_FILTERING;
1097 client->demux.filternum = 32; /* todo: nova ??? */
1098 client->demux.feednum = 32;
1099 client->demux.start_feed = smsdvb_start_feed;
1100 client->demux.stop_feed = smsdvb_stop_feed;
1102 rc = dvb_dmx_init(&client->demux);
1104 sms_err("dvb_dmx_init failed %d", rc);
1109 client->dmxdev.filternum = 32;
1110 client->dmxdev.demux = &client->demux.dmx;
1111 client->dmxdev.capabilities = 0;
1113 rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
1115 sms_err("dvb_dmxdev_init failed %d", rc);
1119 /* init and register frontend */
1120 memcpy(&client->frontend.ops, &smsdvb_fe_ops,
1121 sizeof(struct dvb_frontend_ops));
1123 switch (smscore_get_device_mode(coredev)) {
1124 case DEVICE_MODE_DVBT:
1125 case DEVICE_MODE_DVBT_BDA:
1126 client->frontend.ops.delsys[0] = SYS_DVBT;
1128 case DEVICE_MODE_ISDBT:
1129 case DEVICE_MODE_ISDBT_BDA:
1130 client->frontend.ops.delsys[0] = SYS_ISDBT;
1134 rc = dvb_register_frontend(&client->adapter, &client->frontend);
1136 sms_err("frontend registration failed %d", rc);
1137 goto frontend_error;
1140 params.initial_id = 1;
1141 params.data_type = MSG_SMS_DVBT_BDA_DATA;
1142 params.onresponse_handler = smsdvb_onresponse;
1143 params.onremove_handler = smsdvb_onremove;
1144 params.context = client;
1146 rc = smscore_register_client(coredev, ¶ms, &client->smsclient);
1148 sms_err("smscore_register_client() failed %d", rc);
1152 client->coredev = coredev;
1154 init_completion(&client->tune_done);
1155 init_completion(&client->stats_done);
1157 kmutex_lock(&g_smsdvb_clientslock);
1159 list_add(&client->entry, &g_smsdvb_clients);
1161 kmutex_unlock(&g_smsdvb_clientslock);
1163 client->event_fe_state = -1;
1164 client->event_unc_state = -1;
1165 sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
1167 sms_info("success");
1168 sms_board_setup(coredev);
1170 if (smsdvb_debugfs_create(client) < 0)
1171 sms_info("failed to create debugfs node");
1176 dvb_unregister_frontend(&client->frontend);
1179 dvb_dmxdev_release(&client->dmxdev);
1182 dvb_dmx_release(&client->demux);
1185 dvb_unregister_adapter(&client->adapter);
1192 static int __init smsdvb_module_init(void)
1196 INIT_LIST_HEAD(&g_smsdvb_clients);
1197 kmutex_init(&g_smsdvb_clientslock);
1199 smsdvb_debugfs_register();
1201 rc = smscore_register_hotplug(smsdvb_hotplug);
1208 static void __exit smsdvb_module_exit(void)
1210 smscore_unregister_hotplug(smsdvb_hotplug);
1212 kmutex_lock(&g_smsdvb_clientslock);
1214 while (!list_empty(&g_smsdvb_clients))
1215 smsdvb_unregister_client((struct smsdvb_client_t *)g_smsdvb_clients.next);
1217 smsdvb_debugfs_unregister();
1219 kmutex_unlock(&g_smsdvb_clientslock);
1222 module_init(smsdvb_module_init);
1223 module_exit(smsdvb_module_exit);
1225 MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
1226 MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
1227 MODULE_LICENSE("GPL");