smsdvb.c 44.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/****************************************************************

Siano Mobile Silicon, Inc.
MDTV receiver kernel modules.
Copyright (C) 2006-2008, Uri Shkolnik

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

 This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

****************************************************************/
21

22
#include <linux/module.h>
23
#include <linux/slab.h>
24
#include <linux/init.h>
25
#include <linux/debugfs.h>
26

27 28 29 30 31
#include "dmxdev.h"
#include "dvbdev.h"
#include "dvb_demux.h"
#include "dvb_frontend.h"

32
#include "smscoreapi.h"
33
#include "sms-cards.h"
34

35 36
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

37 38 39 40 41 42 43 44 45 46 47 48 49
struct smsdvb_client_t {
	struct list_head entry;

	struct smscore_device_t *coredev;
	struct smscore_client_t *smsclient;

	struct dvb_adapter      adapter;
	struct dvb_demux        demux;
	struct dmxdev           dmxdev;
	struct dvb_frontend     frontend;

	fe_status_t             fe_status;

50
	struct completion       tune_done;
51
	struct completion       stats_done;
52

53 54 55 56
	int last_per;

	int legacy_ber, legacy_per;

57 58
	int event_fe_state;
	int event_unc_state;
59 60 61 62 63 64 65

	/* Stats debugfs data */
	struct dentry		*debugfs;
	char			*stats_data;
	atomic_t		stats_count;
	bool			stats_was_read;
	wait_queue_head_t	stats_queue;
66 67
};

68 69
static struct list_head g_smsdvb_clients;
static struct mutex g_smsdvb_clientslock;
70

71 72 73 74
static int sms_dbg;
module_param_named(debug, sms_dbg, int, 0644);
MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
/*
 * This struct is a mix of RECEPTION_STATISTICS_EX_S and SRVM_SIGNAL_STATUS_S.
 * It was obtained by comparing the way it was filled by the original code
 */
struct RECEPTION_STATISTICS_PER_SLICES_S {
	u32 result;
	u32 snr;
	s32 inBandPower;
	u32 tsPackets;
	u32 etsPackets;
	u32 constellation;
	u32 hpCode;
	u32 tpsSrvIndLP;
	u32 tpsSrvIndHP;
	u32 cellId;
	u32 reason;
	u32 requestId;
	u32 ModemState;		/* from SMSHOSTLIB_DVB_MODEM_STATE_ET */

	u32 BER;		/* Post Viterbi BER [1E-5] */
	s32 RSSI;		/* dBm */
	s32 CarrierOffset;	/* Carrier Offset in bin/1024 */

	u32 IsRfLocked;		/* 0 - not locked, 1 - locked */
	u32 IsDemodLocked;	/* 0 - not locked, 1 - locked */

	u32 BERBitCount;	/* Total number of SYNC bits. */
	u32 BERErrorCount;	/* Number of erronous SYNC bits. */

	s32 MRC_SNR;		/* dB */
	s32 MRC_InBandPwr;	/* In band power in dBM */
	s32 MRC_RSSI;		/* dBm */
};

u32 sms_to_bw_table[] = {
	[BW_8_MHZ]		= 8000000,
	[BW_7_MHZ]		= 7000000,
	[BW_6_MHZ]		= 6000000,
	[BW_5_MHZ]		= 5000000,
	[BW_2_MHZ]		= 2000000,
	[BW_1_5_MHZ]		= 1500000,
	[BW_ISDBT_1SEG]		= 6000000,
	[BW_ISDBT_3SEG]		= 6000000,
	[BW_ISDBT_13SEG]	= 6000000,
};

u32 sms_to_guard_interval_table[] = {
	[0] = GUARD_INTERVAL_1_32,
	[1] = GUARD_INTERVAL_1_16,
	[2] = GUARD_INTERVAL_1_8,
	[3] = GUARD_INTERVAL_1_4,
};

u32 sms_to_code_rate_table[] = {
	[0] = FEC_1_2,
	[1] = FEC_2_3,
	[2] = FEC_3_4,
	[3] = FEC_5_6,
	[4] = FEC_7_8,
};


u32 sms_to_hierarchy_table[] = {
	[0] = HIERARCHY_NONE,
	[1] = HIERARCHY_1,
	[2] = HIERARCHY_2,
	[3] = HIERARCHY_4,
};

u32 sms_to_modulation_table[] = {
	[0] = QPSK,
	[1] = QAM_16,
	[2] = QAM_64,
	[3] = DQPSK,
};

151 152 153 154
static struct dentry *smsdvb_debugfs;

static void smsdvb_print_dvb_stats(struct smsdvb_client_t *client,
				   struct SMSHOSTLIB_STATISTICS_ST *p)
155
{
156 157 158 159
	int n = 0;
	char *buf;

	if (!client->stats_data || atomic_read(&client->stats_count))
160 161
		return;

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
	buf = client->stats_data;

	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "IsRfLocked = %d\n", p->IsRfLocked);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "IsDemodLocked = %d\n", p->IsDemodLocked);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "IsExternalLNAOn = %d\n", p->IsExternalLNAOn);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "SNR = %d\n", p->SNR);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "BER = %d\n", p->BER);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "FIB_CRC = %d\n", p->FIB_CRC);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "TS_PER = %d\n", p->TS_PER);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "MFER = %d\n", p->MFER);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "RSSI = %d\n", p->RSSI);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "InBandPwr = %d\n", p->InBandPwr);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "CarrierOffset = %d\n", p->CarrierOffset);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "ModemState = %d\n", p->ModemState);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "Frequency = %d\n", p->Frequency);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "Bandwidth = %d\n", p->Bandwidth);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "TransmissionMode = %d\n", p->TransmissionMode);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "ModemState = %d\n", p->ModemState);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "GuardInterval = %d\n", p->GuardInterval);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "CodeRate = %d\n", p->CodeRate);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "LPCodeRate = %d\n", p->LPCodeRate);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "Hierarchy = %d\n", p->Hierarchy);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "Constellation = %d\n", p->Constellation);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "BurstSize = %d\n", p->BurstSize);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "BurstDuration = %d\n", p->BurstDuration);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "BurstCycleTime = %d\n", p->BurstCycleTime);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "CalculatedBurstCycleTime = %d\n",
		      p->CalculatedBurstCycleTime);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "NumOfRows = %d\n", p->NumOfRows);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "NumOfPaddCols = %d\n", p->NumOfPaddCols);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "NumOfPunctCols = %d\n", p->NumOfPunctCols);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "ErrorTSPackets = %d\n", p->ErrorTSPackets);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "TotalTSPackets = %d\n", p->TotalTSPackets);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "NumOfValidMpeTlbs = %d\n", p->NumOfValidMpeTlbs);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "NumOfInvalidMpeTlbs = %d\n", p->NumOfInvalidMpeTlbs);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "NumOfCorrectedMpeTlbs = %d\n", p->NumOfCorrectedMpeTlbs);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "BERErrorCount = %d\n", p->BERErrorCount);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "BERBitCount = %d\n", p->BERBitCount);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "SmsToHostTxErrors = %d\n", p->SmsToHostTxErrors);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "PreBER = %d\n", p->PreBER);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "CellId = %d\n", p->CellId);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "DvbhSrvIndHP = %d\n", p->DvbhSrvIndHP);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "DvbhSrvIndLP = %d\n", p->DvbhSrvIndLP);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "NumMPEReceived = %d\n", p->NumMPEReceived);

	atomic_set(&client->stats_count, n);
	wake_up(&client->stats_queue);
250 251
}

252 253
static void smsdvb_print_isdb_stats(struct smsdvb_client_t *client,
				    struct SMSHOSTLIB_STATISTICS_ISDBT_ST *p)
254
{
255 256
	int i, n = 0;
	char *buf;
257

258
	if (!client->stats_data || atomic_read(&client->stats_count))
259 260
		return;

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	buf = client->stats_data;

	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "IsRfLocked = %d\t\t", p->IsRfLocked);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "IsDemodLocked = %d\t", p->IsDemodLocked);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "IsExternalLNAOn = %d\n", p->IsExternalLNAOn);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "SNR = %d dB\t\t", p->SNR);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "RSSI = %d dBm\t\t", p->RSSI);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "InBandPwr = %d dBm\n", p->InBandPwr);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "CarrierOffset = %d\t", p->CarrierOffset);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "Bandwidth = %d\t\t", p->Bandwidth);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "Frequency = %d Hz\n", p->Frequency);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "TransmissionMode = %d\t", p->TransmissionMode);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "ModemState = %d\t\t", p->ModemState);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "GuardInterval = %d\n", p->GuardInterval);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "SystemType = %d\t\t", p->SystemType);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "PartialReception = %d\t", p->PartialReception);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "NumOfLayers = %d\n", p->NumOfLayers);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "SmsToHostTxErrors = %d\n", p->SmsToHostTxErrors);
295 296

	for (i = 0; i < 3; i++) {
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
		if (p->LayerInfo[i].NumberOfSegments < 1 ||
		    p->LayerInfo[i].NumberOfSegments > 13)
			continue;

		n += snprintf(&buf[n], PAGE_SIZE - n, "\nLayer %d\n", i);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tCodeRate = %d\t",
			      p->LayerInfo[i].CodeRate);
		n += snprintf(&buf[n], PAGE_SIZE - n, "Constellation = %d\n",
			      p->LayerInfo[i].Constellation);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tBER = %-5d\t",
			      p->LayerInfo[i].BER);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tBERErrorCount = %-5d\t",
			      p->LayerInfo[i].BERErrorCount);
		n += snprintf(&buf[n], PAGE_SIZE - n, "BERBitCount = %-5d\n",
			      p->LayerInfo[i].BERBitCount);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tPreBER = %-5d\t",
			      p->LayerInfo[i].PreBER);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tTS_PER = %-5d\n",
			      p->LayerInfo[i].TS_PER);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tErrorTSPackets = %-5d\t",
			      p->LayerInfo[i].ErrorTSPackets);
		n += snprintf(&buf[n], PAGE_SIZE - n, "TotalTSPackets = %-5d\t",
			      p->LayerInfo[i].TotalTSPackets);
		n += snprintf(&buf[n], PAGE_SIZE - n, "TILdepthI = %d\n",
			      p->LayerInfo[i].TILdepthI);
		n += snprintf(&buf[n], PAGE_SIZE - n,
			      "\tNumberOfSegments = %d\t",
			      p->LayerInfo[i].NumberOfSegments);
		n += snprintf(&buf[n], PAGE_SIZE - n, "TMCCErrors = %d\n",
			      p->LayerInfo[i].TMCCErrors);
327
	}
328 329 330

	atomic_set(&client->stats_count, n);
	wake_up(&client->stats_queue);
331 332 333
}

static void
334 335
smsdvb_print_isdb_stats_ex(struct smsdvb_client_t *client,
			   struct SMSHOSTLIB_STATISTICS_ISDBT_EX_ST *p)
336
{
337 338
	int i, n = 0;
	char *buf;
339

340
	if (!client->stats_data || atomic_read(&client->stats_count))
341 342
		return;

343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
	buf = client->stats_data;

	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "IsRfLocked = %d\t\t", p->IsRfLocked);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "IsDemodLocked = %d\t", p->IsDemodLocked);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "IsExternalLNAOn = %d\n", p->IsExternalLNAOn);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "SNR = %d dB\t\t", p->SNR);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "RSSI = %d dBm\t\t", p->RSSI);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "InBandPwr = %d dBm\n", p->InBandPwr);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "CarrierOffset = %d\t", p->CarrierOffset);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "Bandwidth = %d\t\t", p->Bandwidth);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "Frequency = %d Hz\n", p->Frequency);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "TransmissionMode = %d\t", p->TransmissionMode);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "ModemState = %d\t\t", p->ModemState);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "GuardInterval = %d\n", p->GuardInterval);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "SystemType = %d\t\t", p->SystemType);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "PartialReception = %d\t", p->PartialReception);
	n += snprintf(&buf[n], PAGE_SIZE - n,
		      "NumOfLayers = %d\n", p->NumOfLayers);
	n += snprintf(&buf[n], PAGE_SIZE - n, "SegmentNumber = %d\t",
		      p->SegmentNumber);
	n += snprintf(&buf[n], PAGE_SIZE - n, "TuneBW = %d\n",
		      p->TuneBW);
379 380

	for (i = 0; i < 3; i++) {
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
		if (p->LayerInfo[i].NumberOfSegments < 1 ||
		    p->LayerInfo[i].NumberOfSegments > 13)
			continue;

		n += snprintf(&buf[n], PAGE_SIZE - n, "\nLayer %d\n", i);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tCodeRate = %d\t",
			      p->LayerInfo[i].CodeRate);
		n += snprintf(&buf[n], PAGE_SIZE - n, "Constellation = %d\n",
			      p->LayerInfo[i].Constellation);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tBER = %-5d\t",
			      p->LayerInfo[i].BER);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tBERErrorCount = %-5d\t",
			      p->LayerInfo[i].BERErrorCount);
		n += snprintf(&buf[n], PAGE_SIZE - n, "BERBitCount = %-5d\n",
			      p->LayerInfo[i].BERBitCount);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tPreBER = %-5d\t",
			      p->LayerInfo[i].PreBER);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tTS_PER = %-5d\n",
			      p->LayerInfo[i].TS_PER);
		n += snprintf(&buf[n], PAGE_SIZE - n, "\tErrorTSPackets = %-5d\t",
			      p->LayerInfo[i].ErrorTSPackets);
		n += snprintf(&buf[n], PAGE_SIZE - n, "TotalTSPackets = %-5d\t",
			      p->LayerInfo[i].TotalTSPackets);
		n += snprintf(&buf[n], PAGE_SIZE - n, "TILdepthI = %d\n",
			      p->LayerInfo[i].TILdepthI);
		n += snprintf(&buf[n], PAGE_SIZE - n,
			      "\tNumberOfSegments = %d\t",
			      p->LayerInfo[i].NumberOfSegments);
		n += snprintf(&buf[n], PAGE_SIZE - n, "TMCCErrors = %d\n",
			      p->LayerInfo[i].TMCCErrors);
	}

	atomic_set(&client->stats_count, n);
	wake_up(&client->stats_queue);
}

static int smsdvb_stats_open(struct inode *inode, struct file *file)
{
	struct smsdvb_client_t *client = inode->i_private;

	atomic_set(&client->stats_count, 0);
	client->stats_was_read = false;

	init_waitqueue_head(&client->stats_queue);

	client->stats_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (client->stats_data == NULL)
		return -ENOMEM;

	file->private_data = client;

	return 0;
}

static ssize_t smsdvb_stats_read(struct file *file, char __user *user_buf,
				      size_t nbytes, loff_t *ppos)
{
	struct smsdvb_client_t *client = file->private_data;

	if (!client->stats_data || client->stats_was_read)
		return 0;

	wait_event_interruptible(client->stats_queue,
				 atomic_read(&client->stats_count));

	return simple_read_from_buffer(user_buf, nbytes, ppos,
				       client->stats_data,
				       atomic_read(&client->stats_count));

	client->stats_was_read = true;
}

static int smsdvb_stats_release(struct inode *inode, struct file *file)
{
	struct smsdvb_client_t *client = file->private_data;

	kfree(client->stats_data);
	client->stats_data = NULL;

	return 0;
}

static const struct file_operations debugfs_stats_ops = {
	.open = smsdvb_stats_open,
	.read = smsdvb_stats_read,
	.release = smsdvb_stats_release,
	.llseek = generic_file_llseek,
};

static int create_stats_debugfs(struct smsdvb_client_t *client)
{
	struct smscore_device_t *coredev = client->coredev;
	struct dentry *d;

	if (!smsdvb_debugfs)
		return -ENODEV;

	client->debugfs = debugfs_create_dir(coredev->devpath, smsdvb_debugfs);
	if (IS_ERR_OR_NULL(client->debugfs)) {
		sms_info("Unable to create debugfs %s directory.\n",
			 coredev->devpath);
		return -ENODEV;
483
	}
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

	d = debugfs_create_file("stats", S_IRUGO | S_IWUSR, client->debugfs,
				client, &debugfs_stats_ops);
	if (!d) {
		debugfs_remove(client->debugfs);
		return -ENOMEM;
	}

	return 0;
}

static void release_stats_debugfs(struct smsdvb_client_t *client)
{
	if (!client->debugfs)
		return;

	debugfs_remove_recursive(client->debugfs);

	client->debugfs = NULL;
503 504
}

505 506 507
/* Events that may come from DVB v3 adapter */
static void sms_board_dvb3_event(struct smsdvb_client_t *client,
		enum SMS_DVB3_EVENTS event) {
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555

	struct smscore_device_t *coredev = client->coredev;
	switch (event) {
	case DVB3_EVENT_INIT:
		sms_debug("DVB3_EVENT_INIT");
		sms_board_event(coredev, BOARD_EVENT_BIND);
		break;
	case DVB3_EVENT_SLEEP:
		sms_debug("DVB3_EVENT_SLEEP");
		sms_board_event(coredev, BOARD_EVENT_POWER_SUSPEND);
		break;
	case DVB3_EVENT_HOTPLUG:
		sms_debug("DVB3_EVENT_HOTPLUG");
		sms_board_event(coredev, BOARD_EVENT_POWER_INIT);
		break;
	case DVB3_EVENT_FE_LOCK:
		if (client->event_fe_state != DVB3_EVENT_FE_LOCK) {
			client->event_fe_state = DVB3_EVENT_FE_LOCK;
			sms_debug("DVB3_EVENT_FE_LOCK");
			sms_board_event(coredev, BOARD_EVENT_FE_LOCK);
		}
		break;
	case DVB3_EVENT_FE_UNLOCK:
		if (client->event_fe_state != DVB3_EVENT_FE_UNLOCK) {
			client->event_fe_state = DVB3_EVENT_FE_UNLOCK;
			sms_debug("DVB3_EVENT_FE_UNLOCK");
			sms_board_event(coredev, BOARD_EVENT_FE_UNLOCK);
		}
		break;
	case DVB3_EVENT_UNC_OK:
		if (client->event_unc_state != DVB3_EVENT_UNC_OK) {
			client->event_unc_state = DVB3_EVENT_UNC_OK;
			sms_debug("DVB3_EVENT_UNC_OK");
			sms_board_event(coredev, BOARD_EVENT_MULTIPLEX_OK);
		}
		break;
	case DVB3_EVENT_UNC_ERR:
		if (client->event_unc_state != DVB3_EVENT_UNC_ERR) {
			client->event_unc_state = DVB3_EVENT_UNC_ERR;
			sms_debug("DVB3_EVENT_UNC_ERR");
			sms_board_event(coredev, BOARD_EVENT_MULTIPLEX_ERRORS);
		}
		break;

	default:
		sms_err("Unknown dvb3 api event");
		break;
	}
556 557
}

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
static void smsdvb_stats_not_ready(struct dvb_frontend *fe)
{
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);
	struct smscore_device_t *coredev = client->coredev;
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
	int i, n_layers;

	switch (smscore_get_device_mode(coredev)) {
	case DEVICE_MODE_ISDBT:
	case DEVICE_MODE_ISDBT_BDA:
		n_layers = 4;
	default:
		n_layers = 1;
	}

	/* Fill the length of each status counter */

	/* Only global stats */
	c->strength.len = 1;
	c->cnr.len = 1;

	/* Per-layer stats */
	c->post_bit_error.len = n_layers;
	c->post_bit_count.len = n_layers;
	c->block_error.len = n_layers;
	c->block_count.len = n_layers;

	/* Signal is always available */
	c->strength.stat[0].scale = FE_SCALE_RELATIVE;
	c->strength.stat[0].uvalue = 0;

	/* Put all of them at FE_SCALE_NOT_AVAILABLE */
	for (i = 0; i < n_layers; i++) {
		c->cnr.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
		c->post_bit_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
		c->post_bit_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
		c->block_error.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
		c->block_count.stat[i].scale = FE_SCALE_NOT_AVAILABLE;
	}
}

static inline int sms_to_mode(u32 mode)
{
	switch (mode) {
	case 2:
		return TRANSMISSION_MODE_2K;
	case 4:
		return TRANSMISSION_MODE_4K;
	case 8:
		return TRANSMISSION_MODE_8K;
	}
	return TRANSMISSION_MODE_AUTO;
}

static inline int sms_to_status(u32 is_demod_locked, u32 is_rf_locked)
{
	if (is_demod_locked)
		return FE_HAS_SIGNAL  | FE_HAS_CARRIER | FE_HAS_VITERBI |
		       FE_HAS_SYNC    | FE_HAS_LOCK;

	if (is_rf_locked)
		return FE_HAS_SIGNAL | FE_HAS_CARRIER;

	return 0;
}


#define convert_from_table(value, table, defval) ({			\
	u32 __ret;							\
	if (value < ARRAY_SIZE(table))					\
		__ret = table[value];					\
	else								\
		__ret = defval;						\
	__ret;								\
})

#define sms_to_bw(value)						\
	convert_from_table(value, sms_to_bw_table, 0);

#define sms_to_guard_interval(value)					\
	convert_from_table(value, sms_to_guard_interval_table,		\
			   GUARD_INTERVAL_AUTO);

#define sms_to_code_rate(value)						\
	convert_from_table(value, sms_to_code_rate_table,		\
			   FEC_NONE);

#define sms_to_hierarchy(value)						\
	convert_from_table(value, sms_to_hierarchy_table,		\
			   FEC_NONE);

#define sms_to_modulation(value)					\
	convert_from_table(value, sms_to_modulation_table,		\
			   FEC_NONE);

static void smsdvb_update_tx_params(struct smsdvb_client_t *client,
				    struct TRANSMISSION_STATISTICS_S *p)
{
	struct dvb_frontend *fe = &client->frontend;
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;

	c->frequency = p->Frequency;
	client->fe_status = sms_to_status(p->IsDemodLocked, 0);
	c->bandwidth_hz = sms_to_bw(p->Bandwidth);
	c->transmission_mode = sms_to_mode(p->TransmissionMode);
	c->guard_interval = sms_to_guard_interval(p->GuardInterval);
	c->code_rate_HP = sms_to_code_rate(p->CodeRate);
	c->code_rate_LP = sms_to_code_rate(p->LPCodeRate);
	c->hierarchy = sms_to_hierarchy(p->Hierarchy);
	c->modulation = sms_to_modulation(p->Constellation);
}

static void smsdvb_update_per_slices(struct smsdvb_client_t *client,
				     struct RECEPTION_STATISTICS_PER_SLICES_S *p)
{
	struct dvb_frontend *fe = &client->frontend;
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;

	client->fe_status = sms_to_status(p->IsDemodLocked, p->IsRfLocked);
	c->modulation = sms_to_modulation(p->constellation);

	/* TS PER */
	client->last_per = c->block_error.stat[0].uvalue;
	c->block_error.stat[0].scale = FE_SCALE_COUNTER;
	c->block_count.stat[0].scale = FE_SCALE_COUNTER;
	c->block_error.stat[0].uvalue += p->etsPackets;
	c->block_count.stat[0].uvalue += p->etsPackets + p->tsPackets;

	/* BER */
	c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
	c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
	c->post_bit_error.stat[0].uvalue += p->BERErrorCount;
	c->post_bit_count.stat[0].uvalue += p->BERBitCount;

	/* Legacy PER/BER */
	client->legacy_per = (p->etsPackets * 65535) /
			     (p->tsPackets + p->etsPackets);

	/* Signal Strength, in DBm */
	c->strength.stat[0].uvalue = p->RSSI * 1000;

	/* Carrier to Noise ratio, in DB */
	c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
	c->cnr.stat[0].svalue = p->snr * 1000;
}

static void smsdvb_update_dvb_stats(struct smsdvb_client_t *client,
706
				    struct SMSHOSTLIB_STATISTICS_ST *p)
707
{
708 709 710
	struct dvb_frontend *fe = &client->frontend;
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;

711
	smsdvb_print_dvb_stats(client, p);
712

713 714 715 716 717 718 719 720 721 722 723 724 725
	client->fe_status = sms_to_status(p->IsDemodLocked, p->IsRfLocked);

	/* Update DVB modulation parameters */
	c->frequency = p->Frequency;
	client->fe_status = sms_to_status(p->IsDemodLocked, 0);
	c->bandwidth_hz = sms_to_bw(p->Bandwidth);
	c->transmission_mode = sms_to_mode(p->TransmissionMode);
	c->guard_interval = sms_to_guard_interval(p->GuardInterval);
	c->code_rate_HP = sms_to_code_rate(p->CodeRate);
	c->code_rate_LP = sms_to_code_rate(p->LPCodeRate);
	c->hierarchy = sms_to_hierarchy(p->Hierarchy);
	c->modulation = sms_to_modulation(p->Constellation);

726
	/* update reception data */
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
	c->lna = p->IsExternalLNAOn ? 1 : 0;

	/* Carrier to Noise ratio, in DB */
	c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
	c->cnr.stat[0].svalue = p->SNR * 1000;

	/* Signal Strength, in DBm */
	c->strength.stat[0].uvalue = p->RSSI * 1000;

	/* TS PER */
	client->last_per = c->block_error.stat[0].uvalue;
	c->block_error.stat[0].scale = FE_SCALE_COUNTER;
	c->block_count.stat[0].scale = FE_SCALE_COUNTER;
	c->block_error.stat[0].uvalue += p->ErrorTSPackets;
	c->block_count.stat[0].uvalue += p->TotalTSPackets;

	/* BER */
	c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
	c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
	c->post_bit_error.stat[0].uvalue += p->BERErrorCount;
	c->post_bit_count.stat[0].uvalue += p->BERBitCount;

	/* Legacy PER/BER */
	client->legacy_ber = p->BER;
751 752
};

753
static void smsdvb_update_isdbt_stats(struct smsdvb_client_t *client,
754
				      struct SMSHOSTLIB_STATISTICS_ISDBT_ST *p)
755
{
756 757 758 759 760
	struct dvb_frontend *fe = &client->frontend;
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
	struct SMSHOSTLIB_ISDBT_LAYER_STAT_ST *lr;
	int i, n_layers;

761
	smsdvb_print_isdb_stats(client, p);
762

763 764 765 766 767 768 769 770 771 772 773 774 775 776
	/* Update ISDB-T transmission parameters */
	c->frequency = p->Frequency;
	client->fe_status = sms_to_status(p->IsDemodLocked, 0);
	c->bandwidth_hz = sms_to_bw(p->Bandwidth);
	c->transmission_mode = sms_to_mode(p->TransmissionMode);
	c->guard_interval = sms_to_guard_interval(p->GuardInterval);
	c->isdbt_partial_reception = p->PartialReception ? 1 : 0;
	n_layers = p->NumOfLayers;
	if (n_layers < 1)
		n_layers = 1;
	if (n_layers > 3)
		n_layers = 3;
	c->isdbt_layer_enabled = 0;

777
	/* update reception data */
778
	c->lna = p->IsExternalLNAOn ? 1 : 0;
779

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
	/* Carrier to Noise ratio, in DB */
	c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
	c->cnr.stat[0].svalue = p->SNR * 1000;

	/* Signal Strength, in DBm */
	c->strength.stat[0].uvalue = p->RSSI * 1000;

	client->last_per = c->block_error.stat[0].uvalue;

	/* Clears global counters, as the code below will sum it again */
	c->block_error.stat[0].uvalue = 0;
	c->block_count.stat[0].uvalue = 0;
	c->post_bit_error.stat[0].uvalue = 0;
	c->post_bit_count.stat[0].uvalue = 0;

	for (i = 0; i < n_layers; i++) {
		lr = &p->LayerInfo[i];

		/* Update per-layer transmission parameters */
		if (lr->NumberOfSegments > 0 && lr->NumberOfSegments < 13) {
			c->isdbt_layer_enabled |= 1 << i;
			c->layer[i].segment_count = lr->NumberOfSegments;
		} else {
			continue;
		}
		c->layer[i].modulation = sms_to_modulation(lr->Constellation);

		/* TS PER */
		c->block_error.stat[i].scale = FE_SCALE_COUNTER;
		c->block_count.stat[i].scale = FE_SCALE_COUNTER;
		c->block_error.stat[i].uvalue += lr->ErrorTSPackets;
		c->block_count.stat[i].uvalue += lr->TotalTSPackets;

		/* Update global PER counter */
		c->block_error.stat[0].uvalue += lr->ErrorTSPackets;
		c->block_count.stat[0].uvalue += lr->TotalTSPackets;

		/* BER */
		c->post_bit_error.stat[i].scale = FE_SCALE_COUNTER;
		c->post_bit_count.stat[i].scale = FE_SCALE_COUNTER;
		c->post_bit_error.stat[i].uvalue += lr->BERErrorCount;
		c->post_bit_count.stat[i].uvalue += lr->BERBitCount;

		/* Update global BER counter */
		c->post_bit_error.stat[0].uvalue += lr->BERErrorCount;
		c->post_bit_count.stat[0].uvalue += lr->BERBitCount;
826 827 828
	}
}

829 830
static void smsdvb_update_isdbt_stats_ex(struct smsdvb_client_t *client,
					 struct SMSHOSTLIB_STATISTICS_ISDBT_EX_ST *p)
831
{
832 833 834 835 836
	struct dvb_frontend *fe = &client->frontend;
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
	struct SMSHOSTLIB_ISDBT_LAYER_STAT_ST *lr;
	int i, n_layers;

837
	smsdvb_print_isdb_stats_ex(client, p);
838

839 840 841 842 843 844 845 846 847 848 849 850 851 852
	/* Update ISDB-T transmission parameters */
	c->frequency = p->Frequency;
	client->fe_status = sms_to_status(p->IsDemodLocked, 0);
	c->bandwidth_hz = sms_to_bw(p->Bandwidth);
	c->transmission_mode = sms_to_mode(p->TransmissionMode);
	c->guard_interval = sms_to_guard_interval(p->GuardInterval);
	c->isdbt_partial_reception = p->PartialReception ? 1 : 0;
	n_layers = p->NumOfLayers;
	if (n_layers < 1)
		n_layers = 1;
	if (n_layers > 3)
		n_layers = 3;
	c->isdbt_layer_enabled = 0;

853
	/* update reception data */
854
	c->lna = p->IsExternalLNAOn ? 1 : 0;
855

856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
	/* Carrier to Noise ratio, in DB */
	c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
	c->cnr.stat[0].svalue = p->SNR * 1000;

	/* Signal Strength, in DBm */
	c->strength.stat[0].uvalue = p->RSSI * 1000;

	client->last_per = c->block_error.stat[0].uvalue;

	/* Clears global counters, as the code below will sum it again */
	c->block_error.stat[0].uvalue = 0;
	c->block_count.stat[0].uvalue = 0;
	c->post_bit_error.stat[0].uvalue = 0;
	c->post_bit_count.stat[0].uvalue = 0;

	for (i = 0; i < n_layers; i++) {
		lr = &p->LayerInfo[i];

		/* Update per-layer transmission parameters */
		if (lr->NumberOfSegments > 0 && lr->NumberOfSegments < 13) {
			c->isdbt_layer_enabled |= 1 << i;
			c->layer[i].segment_count = lr->NumberOfSegments;
		} else {
			continue;
		}
		c->layer[i].modulation = sms_to_modulation(lr->Constellation);

		/* TS PER */
		c->block_error.stat[i].scale = FE_SCALE_COUNTER;
		c->block_count.stat[i].scale = FE_SCALE_COUNTER;
		c->block_error.stat[i].uvalue += lr->ErrorTSPackets;
		c->block_count.stat[i].uvalue += lr->TotalTSPackets;

		/* Update global PER counter */
		c->block_error.stat[0].uvalue += lr->ErrorTSPackets;
		c->block_count.stat[0].uvalue += lr->TotalTSPackets;

		/* BER */
		c->post_bit_error.stat[i].scale = FE_SCALE_COUNTER;
		c->post_bit_count.stat[i].scale = FE_SCALE_COUNTER;
		c->post_bit_error.stat[i].uvalue += lr->BERErrorCount;
		c->post_bit_count.stat[i].uvalue += lr->BERBitCount;

		/* Update global BER counter */
		c->post_bit_error.stat[0].uvalue += lr->BERErrorCount;
		c->post_bit_count.stat[0].uvalue += lr->BERBitCount;
902 903 904
	}
}

905
static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
906
{
907
	struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
908 909
	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) (((u8 *) cb->p)
			+ cb->offset);
910 911 912
	void *p = phdr + 1;
	struct dvb_frontend *fe = &client->frontend;
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
913
	bool is_status_update = false;
914

915
	switch (phdr->msgType) {
916
	case MSG_SMS_DVBT_BDA_DATA:
917
		dvb_dmx_swfilter(&client->demux, p,
918
				 cb->size - sizeof(struct SmsMsgHdr_ST));
919 920 921
		break;

	case MSG_SMS_RF_TUNE_RES:
922
	case MSG_SMS_ISDBT_TUNE_RES:
923 924
		complete(&client->tune_done);
		break;
925

926
	case MSG_SMS_SIGNAL_DETECTED_IND:
927 928 929 930
		client->fe_status = FE_HAS_SIGNAL  | FE_HAS_CARRIER |
				    FE_HAS_VITERBI | FE_HAS_SYNC    |
				    FE_HAS_LOCK;

931 932 933 934
		is_status_update = true;
		break;

	case MSG_SMS_NO_SIGNAL_IND:
935 936
		client->fe_status = 0;

937 938 939
		is_status_update = true;
		break;

940 941
	case MSG_SMS_TRANSMISSION_IND:
		smsdvb_update_tx_params(client, p);
942 943 944 945

		is_status_update = true;
		break;

946 947
	case MSG_SMS_HO_PER_SLICES_IND:
		smsdvb_update_per_slices(client, p);
948 949 950

		is_status_update = true;
		break;
951

952
	case MSG_SMS_GET_STATISTICS_RES:
953 954 955
		switch (smscore_get_device_mode(client->coredev)) {
		case DEVICE_MODE_ISDBT:
		case DEVICE_MODE_ISDBT_BDA:
956
			smsdvb_update_isdbt_stats(client, p);
957 958
			break;
		default:
959 960
			/* Skip SmsMsgStatisticsInfo_ST:RequestResult field */
			smsdvb_update_dvb_stats(client, p + sizeof(u32));
961 962
		}

963
		is_status_update = true;
964
		break;
965

966 967
	/* Only for ISDB-T */
	case MSG_SMS_GET_STATISTICS_EX_RES:
968 969
		/* Skip SmsMsgStatisticsInfo_ST:RequestResult field? */
		smsdvb_update_isdbt_stats_ex(client, p + sizeof(u32));
970 971
		is_status_update = true;
		break;
972
	default:
973
		sms_info("message not handled");
974
	}
975 976
	smscore_putbuffer(client->coredev, cb);

977
	if (is_status_update) {
978
		if (client->fe_status == FE_HAS_LOCK) {
979
			sms_board_dvb3_event(client, DVB3_EVENT_FE_LOCK);
980
			if (client->last_per == c->block_error.stat[0].uvalue)
981 982
				sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
			else
983
				sms_board_dvb3_event(client, DVB3_EVENT_UNC_ERR);
984
		} else {
985 986
			smsdvb_stats_not_ready(fe);

987 988
			sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
		}
989
		complete(&client->stats_done);
990 991
	}

992 993 994
	return 0;
}

995
static void smsdvb_unregister_client(struct smsdvb_client_t *client)
996
{
997
	/* must be called under clientslock */
998 999 1000

	list_del(&client->entry);

1001
	release_stats_debugfs(client);
1002 1003 1004 1005 1006 1007 1008 1009
	smscore_unregister_client(client->smsclient);
	dvb_unregister_frontend(&client->frontend);
	dvb_dmxdev_release(&client->dmxdev);
	dvb_dmx_release(&client->demux);
	dvb_unregister_adapter(&client->adapter);
	kfree(client);
}

1010
static void smsdvb_onremove(void *context)
1011 1012 1013
{
	kmutex_lock(&g_smsdvb_clientslock);

1014
	smsdvb_unregister_client((struct smsdvb_client_t *) context);
1015 1016 1017 1018 1019 1020

	kmutex_unlock(&g_smsdvb_clientslock);
}

static int smsdvb_start_feed(struct dvb_demux_feed *feed)
{
1021 1022 1023
	struct smsdvb_client_t *client =
		container_of(feed->demux, struct smsdvb_client_t, demux);
	struct SmsMsgData_ST PidMsg;
1024

1025
	sms_debug("add pid %d(%x)",
1026
		  feed->pid, feed->pid);
1027 1028 1029 1030 1031 1032 1033 1034

	PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	PidMsg.xMsgHeader.msgDstId = HIF_TASK;
	PidMsg.xMsgHeader.msgFlags = 0;
	PidMsg.xMsgHeader.msgType  = MSG_SMS_ADD_PID_FILTER_REQ;
	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
	PidMsg.msgData[0] = feed->pid;

1035 1036
	return smsclient_sendrequest(client->smsclient,
				     &PidMsg, sizeof(PidMsg));
1037 1038 1039 1040
}

static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
{
1041 1042 1043
	struct smsdvb_client_t *client =
		container_of(feed->demux, struct smsdvb_client_t, demux);
	struct SmsMsgData_ST PidMsg;
1044

1045
	sms_debug("remove pid %d(%x)",
1046
		  feed->pid, feed->pid);
1047 1048 1049 1050 1051 1052 1053 1054

	PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	PidMsg.xMsgHeader.msgDstId = HIF_TASK;
	PidMsg.xMsgHeader.msgFlags = 0;
	PidMsg.xMsgHeader.msgType  = MSG_SMS_REMOVE_PID_FILTER_REQ;
	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
	PidMsg.msgData[0] = feed->pid;

1055 1056
	return smsclient_sendrequest(client->smsclient,
				     &PidMsg, sizeof(PidMsg));
1057 1058
}

1059
static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
1060 1061
					void *buffer, size_t size,
					struct completion *completion)
1062
{
1063 1064 1065
	int rc;

	rc = smsclient_sendrequest(client->smsclient, buffer, size);
1066 1067 1068
	if (rc < 0)
		return rc;

1069 1070 1071
	return wait_for_completion_timeout(completion,
					   msecs_to_jiffies(2000)) ?
						0 : -ETIME;
1072 1073
}

1074 1075 1076
static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
{
	int rc;
1077 1078 1079 1080 1081 1082 1083 1084
	struct SmsMsgHdr_ST Msg;


	Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	Msg.msgDstId = HIF_TASK;
	Msg.msgFlags = 0;
	Msg.msgLength = sizeof(Msg);

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	switch (smscore_get_device_mode(client->coredev)) {
	case DEVICE_MODE_ISDBT:
	case DEVICE_MODE_ISDBT_BDA:
		/*
		* Check for firmware version, to avoid breaking for old cards
		*/
		if (client->coredev->fw_version >= 0x800)
			Msg.msgType = MSG_SMS_GET_STATISTICS_EX_REQ;
		else
			Msg.msgType = MSG_SMS_GET_STATISTICS_REQ;
		break;
	default:
1097
		Msg.msgType = MSG_SMS_GET_STATISTICS_REQ;
1098
	}
1099

1100
	rc = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
1101
					 &client->stats_done);
1102 1103 1104 1105

	return rc;
}

1106 1107
static inline int led_feedback(struct smsdvb_client_t *client)
{
1108
	if (!(client->fe_status & FE_HAS_LOCK))
1109
		return sms_board_led_feedback(client->coredev, SMS_LED_OFF);
1110 1111 1112 1113

	return sms_board_led_feedback(client->coredev,
				     (client->legacy_ber == 0) ?
				     SMS_LED_HI : SMS_LED_LO);
1114 1115
}

1116 1117
static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
{
1118
	int rc;
1119 1120
	struct smsdvb_client_t *client;
	client = container_of(fe, struct smsdvb_client_t, frontend);
1121

1122 1123
	rc = smsdvb_send_statistics_request(client);

1124
	*stat = client->fe_status;
1125

1126 1127
	led_feedback(client);

1128
	return rc;
1129 1130 1131 1132
}

static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
{
1133
	int rc;
1134
	struct smsdvb_client_t *client;
1135

1136
	client = container_of(fe, struct smsdvb_client_t, frontend);
1137

1138 1139
	rc = smsdvb_send_statistics_request(client);

1140
	*ber = client->legacy_ber;
1141

1142 1143
	led_feedback(client);

1144
	return rc;
1145 1146 1147 1148
}

static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
{
1149
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1150
	int rc;
1151
	s32 power = (s32) c->strength.stat[0].uvalue;
1152
	struct smsdvb_client_t *client;
1153

1154
	client = container_of(fe, struct smsdvb_client_t, frontend);
1155

1156 1157
	rc = smsdvb_send_statistics_request(client);

1158
	if (power < -95)
1159
		*strength = 0;
1160 1161
		else if (power > -29)
			*strength = 65535;
1162
		else
1163
			*strength = (power + 95) * 65535 / 66;
1164

1165 1166
	led_feedback(client);

1167
	return rc;
1168 1169 1170 1171
}

static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
{
1172
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1173
	int rc;
1174
	struct smsdvb_client_t *client;
1175

1176
	client = container_of(fe, struct smsdvb_client_t, frontend);
1177

1178 1179
	rc = smsdvb_send_statistics_request(client);

1180 1181
	/* Preferred scale for SNR with legacy API: 0.1 dB */
	*snr = c->cnr.stat[0].svalue / 100;
1182

1183 1184
	led_feedback(client);

1185
	return rc;
1186 1187
}

1188 1189
static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
{
1190
	int rc;
1191
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1192
	struct smsdvb_client_t *client;
1193

1194
	client = container_of(fe, struct smsdvb_client_t, frontend);
1195

1196 1197
	rc = smsdvb_send_statistics_request(client);

1198
	*ucblocks = c->block_error.stat[0].uvalue;
1199

1200 1201
	led_feedback(client);

1202
	return rc;
1203 1204
}

1205 1206
static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
				    struct dvb_frontend_tune_settings *tune)
1207
{
1208
	sms_debug("");
1209 1210 1211 1212 1213 1214 1215

	tune->min_delay_ms = 400;
	tune->step_size = 250000;
	tune->max_drift = 0;
	return 0;
}

1216
static int smsdvb_dvbt_set_frontend(struct dvb_frontend *fe)
1217
{
1218
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1219 1220
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);
1221

1222 1223
	struct {
		struct SmsMsgHdr_ST	Msg;
1224
		u32		Data[3];
1225 1226
	} Msg;

1227 1228
	int ret;

1229
	client->fe_status = 0;
1230 1231
	client->event_fe_state = -1;
	client->event_unc_state = -1;
1232
	fe->dtv_property_cache.delivery_system = SYS_DVBT;
1233 1234 1235 1236 1237

	Msg.Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	Msg.Msg.msgDstId = HIF_TASK;
	Msg.Msg.msgFlags = 0;
	Msg.Msg.msgType = MSG_SMS_RF_TUNE_REQ;
1238
	Msg.Msg.msgLength = sizeof(Msg);
1239
	Msg.Data[0] = c->frequency;
1240 1241
	Msg.Data[2] = 12000000;

1242 1243
	sms_info("%s: freq %d band %d", __func__, c->frequency,
		 c->bandwidth_hz);
1244

1245
	switch (c->bandwidth_hz / 1000000) {
1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	case 8:
		Msg.Data[1] = BW_8_MHZ;
		break;
	case 7:
		Msg.Data[1] = BW_7_MHZ;
		break;
	case 6:
		Msg.Data[1] = BW_6_MHZ;
		break;
	case 0:
		return -EOPNOTSUPP;
	default:
		return -EINVAL;
1259
	}
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	/* Disable LNA, if any. An error is returned if no LNA is present */
	ret = sms_board_lna_control(client->coredev, 0);
	if (ret == 0) {
		fe_status_t status;

		/* tune with LNA off at first */
		ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
						  &client->tune_done);

		smsdvb_read_status(fe, &status);

		if (status & FE_HAS_LOCK)
			return ret;

L
Lucas De Marchi 已提交
1274
		/* previous tune didn't lock - enable LNA and tune again */
1275 1276
		sms_board_lna_control(client->coredev, 1);
	}
1277

1278 1279
	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
					   &client->tune_done);
1280 1281
}

1282
static int smsdvb_isdbt_set_frontend(struct dvb_frontend *fe)
1283
{
1284
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1285 1286
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);
1287 1288 1289
	int board_id = smscore_get_board_id(client->coredev);
	struct sms_board *board = sms_get_board(board_id);
	enum sms_device_type_st type = board->type;
1290
	int ret;
1291 1292 1293 1294 1295
	struct {
		struct SmsMsgHdr_ST	Msg;
		u32		Data[4];
	} Msg;

1296 1297
	fe->dtv_property_cache.delivery_system = SYS_ISDBT;

1298 1299 1300 1301 1302 1303
	Msg.Msg.msgSrcId  = DVBT_BDA_CONTROL_MSG_ID;
	Msg.Msg.msgDstId  = HIF_TASK;
	Msg.Msg.msgFlags  = 0;
	Msg.Msg.msgType   = MSG_SMS_ISDBT_TUNE_REQ;
	Msg.Msg.msgLength = sizeof(Msg);

1304 1305
	if (c->isdbt_sb_segment_idx == -1)
		c->isdbt_sb_segment_idx = 0;
1306

1307 1308
	if (!c->isdbt_layer_enabled)
		c->isdbt_layer_enabled = 7;
1309

1310
	Msg.Data[0] = c->frequency;
1311
	Msg.Data[1] = BW_ISDBT_1SEG;
1312 1313 1314
	Msg.Data[2] = 12000000;
	Msg.Data[3] = c->isdbt_sb_segment_idx;

1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
	if (c->isdbt_partial_reception) {
		if ((type == SMS_PELE || type == SMS_RIO) &&
		    c->isdbt_sb_segment_count > 3)
			Msg.Data[1] = BW_ISDBT_13SEG;
		else if (c->isdbt_sb_segment_count > 1)
			Msg.Data[1] = BW_ISDBT_3SEG;
	} else if (type == SMS_PELE || type == SMS_RIO)
		Msg.Data[1] = BW_ISDBT_13SEG;

	c->bandwidth_hz = 6000000;

1326 1327 1328 1329
	sms_info("%s: freq %d segwidth %d segindex %d\n", __func__,
		 c->frequency, c->isdbt_sb_segment_count,
		 c->isdbt_sb_segment_idx);

1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
	/* Disable LNA, if any. An error is returned if no LNA is present */
	ret = sms_board_lna_control(client->coredev, 0);
	if (ret == 0) {
		fe_status_t status;

		/* tune with LNA off at first */
		ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
						  &client->tune_done);

		smsdvb_read_status(fe, &status);

		if (status & FE_HAS_LOCK)
			return ret;

		/* previous tune didn't lock - enable LNA and tune again */
		sms_board_lna_control(client->coredev, 1);
	}
1347 1348 1349 1350
	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
					   &client->tune_done);
}

1351
static int smsdvb_set_frontend(struct dvb_frontend *fe)
1352 1353 1354 1355 1356
{
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);
	struct smscore_device_t *coredev = client->coredev;

1357 1358
	smsdvb_stats_not_ready(fe);

1359 1360 1361
	switch (smscore_get_device_mode(coredev)) {
	case DEVICE_MODE_DVBT:
	case DEVICE_MODE_DVBT_BDA:
1362
		return smsdvb_dvbt_set_frontend(fe);
1363 1364
	case DEVICE_MODE_ISDBT:
	case DEVICE_MODE_ISDBT_BDA:
1365
		return smsdvb_isdbt_set_frontend(fe);
1366 1367 1368 1369 1370
	default:
		return -EINVAL;
	}
}

1371
/* Nothing to do here, as stats are automatically updated */
1372 1373
static int smsdvb_get_frontend(struct dvb_frontend *fe)
{
1374
	return 0;
1375 1376 1377 1378 1379 1380 1381
}

static int smsdvb_init(struct dvb_frontend *fe)
{
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);

1382 1383
	sms_board_power(client->coredev, 1);

1384
	sms_board_dvb3_event(client, DVB3_EVENT_INIT);
1385 1386 1387 1388 1389 1390 1391 1392
	return 0;
}

static int smsdvb_sleep(struct dvb_frontend *fe)
{
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);

1393 1394 1395
	sms_board_led_feedback(client->coredev, SMS_LED_OFF);
	sms_board_power(client->coredev, 0);

1396
	sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
1397

1398 1399 1400 1401 1402
	return 0;
}

static void smsdvb_release(struct dvb_frontend *fe)
{
1403
	/* do nothing */
1404 1405 1406 1407
}

static struct dvb_frontend_ops smsdvb_fe_ops = {
	.info = {
1408
		.name			= "Siano Mobile Digital MDTV Receiver",
1409 1410 1411 1412
		.frequency_min		= 44250000,
		.frequency_max		= 867250000,
		.frequency_stepsize	= 250000,
		.caps = FE_CAN_INVERSION_AUTO |
1413 1414 1415 1416 1417 1418 1419
			FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
			FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
			FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
			FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
			FE_CAN_GUARD_INTERVAL_AUTO |
			FE_CAN_RECOVER |
			FE_CAN_HIERARCHY_AUTO,
1420 1421 1422 1423
	},

	.release = smsdvb_release,

1424 1425
	.set_frontend = smsdvb_set_frontend,
	.get_frontend = smsdvb_get_frontend,
1426 1427 1428 1429 1430 1431
	.get_tune_settings = smsdvb_get_tune_settings,

	.read_status = smsdvb_read_status,
	.read_ber = smsdvb_read_ber,
	.read_signal_strength = smsdvb_read_signal_strength,
	.read_snr = smsdvb_read_snr,
1432
	.read_ucblocks = smsdvb_read_ucblocks,
1433 1434 1435

	.init = smsdvb_init,
	.sleep = smsdvb_sleep,
1436 1437
};

1438 1439
static int smsdvb_hotplug(struct smscore_device_t *coredev,
			  struct device *device, int arrival)
1440
{
1441 1442
	struct smsclient_params_t params;
	struct smsdvb_client_t *client;
1443 1444
	int rc;

1445
	/* device removal handled by onremove callback */
1446 1447
	if (!arrival)
		return 0;
1448
	client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
1449
	if (!client) {
1450
		sms_err("kmalloc() failed");
1451 1452 1453
		return -ENOMEM;
	}

1454
	/* register dvb adapter */
1455 1456 1457
	rc = dvb_register_adapter(&client->adapter,
				  sms_get_board(
					smscore_get_board_id(coredev))->name,
1458 1459
				  THIS_MODULE, device, adapter_nr);
	if (rc < 0) {
1460
		sms_err("dvb_register_adapter() failed %d", rc);
1461 1462 1463
		goto adapter_error;
	}

1464
	/* init dvb demux */
1465
	client->demux.dmx.capabilities = DMX_TS_FILTERING;
1466
	client->demux.filternum = 32; /* todo: nova ??? */
1467 1468 1469 1470 1471
	client->demux.feednum = 32;
	client->demux.start_feed = smsdvb_start_feed;
	client->demux.stop_feed = smsdvb_stop_feed;

	rc = dvb_dmx_init(&client->demux);
1472
	if (rc < 0) {
1473
		sms_err("dvb_dmx_init failed %d", rc);
1474 1475 1476
		goto dvbdmx_error;
	}

1477
	/* init dmxdev */
1478 1479 1480 1481 1482
	client->dmxdev.filternum = 32;
	client->dmxdev.demux = &client->demux.dmx;
	client->dmxdev.capabilities = 0;

	rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
1483
	if (rc < 0) {
1484
		sms_err("dvb_dmxdev_init failed %d", rc);
1485 1486 1487
		goto dmxdev_error;
	}

1488
	/* init and register frontend */
1489 1490
	memcpy(&client->frontend.ops, &smsdvb_fe_ops,
	       sizeof(struct dvb_frontend_ops));
1491

1492 1493 1494
	switch (smscore_get_device_mode(coredev)) {
	case DEVICE_MODE_DVBT:
	case DEVICE_MODE_DVBT_BDA:
1495
		client->frontend.ops.delsys[0] = SYS_DVBT;
1496 1497 1498
		break;
	case DEVICE_MODE_ISDBT:
	case DEVICE_MODE_ISDBT_BDA:
1499
		client->frontend.ops.delsys[0] = SYS_ISDBT;
1500 1501 1502
		break;
	}

1503
	rc = dvb_register_frontend(&client->adapter, &client->frontend);
1504
	if (rc < 0) {
1505
		sms_err("frontend registration failed %d", rc);
1506 1507 1508
		goto frontend_error;
	}

1509
	params.initial_id = 1;
1510 1511 1512 1513 1514 1515
	params.data_type = MSG_SMS_DVBT_BDA_DATA;
	params.onresponse_handler = smsdvb_onresponse;
	params.onremove_handler = smsdvb_onremove;
	params.context = client;

	rc = smscore_register_client(coredev, &params, &client->smsclient);
1516
	if (rc < 0) {
1517
		sms_err("smscore_register_client() failed %d", rc);
1518 1519 1520 1521 1522 1523
		goto client_error;
	}

	client->coredev = coredev;

	init_completion(&client->tune_done);
1524
	init_completion(&client->stats_done);
1525 1526 1527 1528 1529 1530 1531

	kmutex_lock(&g_smsdvb_clientslock);

	list_add(&client->entry, &g_smsdvb_clients);

	kmutex_unlock(&g_smsdvb_clientslock);

1532 1533 1534
	client->event_fe_state = -1;
	client->event_unc_state = -1;
	sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
1535

1536
	sms_info("success");
1537 1538
	sms_board_setup(coredev);

1539 1540 1541
	if (create_stats_debugfs(client) < 0)
		sms_info("failed to create debugfs node");

1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
	return 0;

client_error:
	dvb_unregister_frontend(&client->frontend);

frontend_error:
	dvb_dmxdev_release(&client->dmxdev);

dmxdev_error:
	dvb_dmx_release(&client->demux);

dvbdmx_error:
	dvb_unregister_adapter(&client->adapter);

adapter_error:
	kfree(client);
	return rc;
}

1561
static int __init smsdvb_module_init(void)
1562 1563
{
	int rc;
1564
	struct dentry *d;
1565 1566 1567 1568

	INIT_LIST_HEAD(&g_smsdvb_clients);
	kmutex_init(&g_smsdvb_clientslock);

1569 1570 1571 1572 1573 1574
	d = debugfs_create_dir("smsdvb", usb_debug_root);
	if (IS_ERR_OR_NULL(d))
		sms_err("Couldn't create sysfs node for smsdvb");
	else
		smsdvb_debugfs = d;

1575 1576
	rc = smscore_register_hotplug(smsdvb_hotplug);

1577
	sms_debug("");
1578 1579 1580 1581

	return rc;
}

1582
static void __exit smsdvb_module_exit(void)
1583 1584 1585 1586 1587 1588
{
	smscore_unregister_hotplug(smsdvb_hotplug);

	kmutex_lock(&g_smsdvb_clientslock);

	while (!list_empty(&g_smsdvb_clients))
1589
	       smsdvb_unregister_client(
1590
			(struct smsdvb_client_t *) g_smsdvb_clients.next);
1591

1592 1593 1594
	if (smsdvb_debugfs)
		debugfs_remove_recursive(smsdvb_debugfs);

1595 1596
	kmutex_unlock(&g_smsdvb_clientslock);
}
1597 1598 1599 1600 1601

module_init(smsdvb_module_init);
module_exit(smsdvb_module_exit);

MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
1602
MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
1603
MODULE_LICENSE("GPL");