smsdvb.c 28.8 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 25
#include <linux/init.h>

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

31
#include "smscoreapi.h"
32
#include "smsendian.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

52 53 54
	struct SMSHOSTLIB_STATISTICS_DVB_S sms_stat_dvb;
	int event_fe_state;
	int event_unc_state;
55 56
};

57 58
static struct list_head g_smsdvb_clients;
static struct mutex g_smsdvb_clientslock;
59

60 61 62 63
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))");

64 65 66
/* Events that may come from DVB v3 adapter */
static void sms_board_dvb3_event(struct smsdvb_client_t *client,
		enum SMS_DVB3_EVENTS event) {
67 68 69 70 71 72 73 74 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

	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;
	}
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 151 152 153 154 155 156 157 158 159 160 161 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

static void smsdvb_update_dvb_stats(struct RECEPTION_STATISTICS_S *pReceptionData,
				   struct SMSHOSTLIB_STATISTICS_ST *p)
{
	if (sms_dbg & 2) {
		printk(KERN_DEBUG "Reserved = %d", p->Reserved);
		printk(KERN_DEBUG "IsRfLocked = %d", p->IsRfLocked);
		printk(KERN_DEBUG "IsDemodLocked = %d", p->IsDemodLocked);
		printk(KERN_DEBUG "IsExternalLNAOn = %d", p->IsExternalLNAOn);
		printk(KERN_DEBUG "SNR = %d", p->SNR);
		printk(KERN_DEBUG "BER = %d", p->BER);
		printk(KERN_DEBUG "FIB_CRC = %d", p->FIB_CRC);
		printk(KERN_DEBUG "TS_PER = %d", p->TS_PER);
		printk(KERN_DEBUG "MFER = %d", p->MFER);
		printk(KERN_DEBUG "RSSI = %d", p->RSSI);
		printk(KERN_DEBUG "InBandPwr = %d", p->InBandPwr);
		printk(KERN_DEBUG "CarrierOffset = %d", p->CarrierOffset);
		printk(KERN_DEBUG "Frequency = %d", p->Frequency);
		printk(KERN_DEBUG "Bandwidth = %d", p->Bandwidth);
		printk(KERN_DEBUG "TransmissionMode = %d", p->TransmissionMode);
		printk(KERN_DEBUG "ModemState = %d", p->ModemState);
		printk(KERN_DEBUG "GuardInterval = %d", p->GuardInterval);
		printk(KERN_DEBUG "CodeRate = %d", p->CodeRate);
		printk(KERN_DEBUG "LPCodeRate = %d", p->LPCodeRate);
		printk(KERN_DEBUG "Hierarchy = %d", p->Hierarchy);
		printk(KERN_DEBUG "Constellation = %d", p->Constellation);
		printk(KERN_DEBUG "BurstSize = %d", p->BurstSize);
		printk(KERN_DEBUG "BurstDuration = %d", p->BurstDuration);
		printk(KERN_DEBUG "BurstCycleTime = %d", p->BurstCycleTime);
		printk(KERN_DEBUG "CalculatedBurstCycleTime = %d", p->CalculatedBurstCycleTime);
		printk(KERN_DEBUG "NumOfRows = %d", p->NumOfRows);
		printk(KERN_DEBUG "NumOfPaddCols = %d", p->NumOfPaddCols);
		printk(KERN_DEBUG "NumOfPunctCols = %d", p->NumOfPunctCols);
		printk(KERN_DEBUG "ErrorTSPackets = %d", p->ErrorTSPackets);
		printk(KERN_DEBUG "TotalTSPackets = %d", p->TotalTSPackets);
		printk(KERN_DEBUG "NumOfValidMpeTlbs = %d", p->NumOfValidMpeTlbs);
		printk(KERN_DEBUG "NumOfInvalidMpeTlbs = %d", p->NumOfInvalidMpeTlbs);
		printk(KERN_DEBUG "NumOfCorrectedMpeTlbs = %d", p->NumOfCorrectedMpeTlbs);
		printk(KERN_DEBUG "BERErrorCount = %d", p->BERErrorCount);
		printk(KERN_DEBUG "BERBitCount = %d", p->BERBitCount);
		printk(KERN_DEBUG "SmsToHostTxErrors = %d", p->SmsToHostTxErrors);
		printk(KERN_DEBUG "PreBER = %d", p->PreBER);
		printk(KERN_DEBUG "CellId = %d", p->CellId);
		printk(KERN_DEBUG "DvbhSrvIndHP = %d", p->DvbhSrvIndHP);
		printk(KERN_DEBUG "DvbhSrvIndLP = %d", p->DvbhSrvIndLP);
		printk(KERN_DEBUG "NumMPEReceived = %d", p->NumMPEReceived);
	}

	pReceptionData->IsDemodLocked = p->IsDemodLocked;

	pReceptionData->SNR = p->SNR;
	pReceptionData->BER = p->BER;
	pReceptionData->BERErrorCount = p->BERErrorCount;
	pReceptionData->InBandPwr = p->InBandPwr;
	pReceptionData->ErrorTSPackets = p->ErrorTSPackets;
};


static void smsdvb_update_isdbt_stats(struct RECEPTION_STATISTICS_S *pReceptionData,
				    struct SMSHOSTLIB_STATISTICS_ISDBT_ST *p)
{
	int i;

	if (sms_dbg & 2) {
		printk(KERN_DEBUG "IsRfLocked = %d", p->IsRfLocked);
		printk(KERN_DEBUG "IsDemodLocked = %d", p->IsDemodLocked);
		printk(KERN_DEBUG "IsExternalLNAOn = %d", p->IsExternalLNAOn);
		printk(KERN_DEBUG "SNR = %d", p->SNR);
		printk(KERN_DEBUG "RSSI = %d", p->RSSI);
		printk(KERN_DEBUG "InBandPwr = %d", p->InBandPwr);
		printk(KERN_DEBUG "CarrierOffset = %d", p->CarrierOffset);
		printk(KERN_DEBUG "Frequency = %d", p->Frequency);
		printk(KERN_DEBUG "Bandwidth = %d", p->Bandwidth);
		printk(KERN_DEBUG "TransmissionMode = %d", p->TransmissionMode);
		printk(KERN_DEBUG "ModemState = %d", p->ModemState);
		printk(KERN_DEBUG "GuardInterval = %d", p->GuardInterval);
		printk(KERN_DEBUG "SystemType = %d", p->SystemType);
		printk(KERN_DEBUG "PartialReception = %d", p->PartialReception);
		printk(KERN_DEBUG "NumOfLayers = %d", p->NumOfLayers);
		printk(KERN_DEBUG "SmsToHostTxErrors = %d", p->SmsToHostTxErrors);

		for (i = 0; i < 3; i++) {
			printk(KERN_DEBUG "%d: CodeRate = %d", i, p->LayerInfo[i].CodeRate);
			printk(KERN_DEBUG "%d: Constellation = %d", i, p->LayerInfo[i].Constellation);
			printk(KERN_DEBUG "%d: BER = %d", i, p->LayerInfo[i].BER);
			printk(KERN_DEBUG "%d: BERErrorCount = %d", i, p->LayerInfo[i].BERErrorCount);
			printk(KERN_DEBUG "%d: BERBitCount = %d", i, p->LayerInfo[i].BERBitCount);
			printk(KERN_DEBUG "%d: PreBER = %d", i, p->LayerInfo[i].PreBER);
			printk(KERN_DEBUG "%d: TS_PER = %d", i, p->LayerInfo[i].TS_PER);
			printk(KERN_DEBUG "%d: ErrorTSPackets = %d", i, p->LayerInfo[i].ErrorTSPackets);
			printk(KERN_DEBUG "%d: TotalTSPackets = %d", i, p->LayerInfo[i].TotalTSPackets);
			printk(KERN_DEBUG "%d: TILdepthI = %d", i, p->LayerInfo[i].TILdepthI);
			printk(KERN_DEBUG "%d: NumberOfSegments = %d", i, p->LayerInfo[i].NumberOfSegments);
			printk(KERN_DEBUG "%d: TMCCErrors = %d", i, p->LayerInfo[i].TMCCErrors);
		}
	}

	pReceptionData->IsDemodLocked = p->IsDemodLocked;

	pReceptionData->SNR = p->SNR;
	pReceptionData->InBandPwr = p->InBandPwr;

	pReceptionData->ErrorTSPackets = 0;
	pReceptionData->BER = 0;
	pReceptionData->BERErrorCount = 0;
	for (i = 0; i < 3; i++) {
		pReceptionData->BER += p->LayerInfo[i].BER;
		pReceptionData->BERErrorCount += p->LayerInfo[i].BERErrorCount;
		pReceptionData->ErrorTSPackets += p->LayerInfo[i].ErrorTSPackets;
	}
}

229
static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
230
{
231
	struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
232 233 234 235 236
	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) (((u8 *) cb->p)
			+ cb->offset);
	u32 *pMsgData = (u32 *) phdr + 1;
	/*u32 MsgDataLen = phdr->msgLength - sizeof(struct SmsMsgHdr_ST);*/
	bool is_status_update = false;
237

238 239
	smsendian_handle_rx_message((struct SmsMsgData_ST *) phdr);

240
	switch (phdr->msgType) {
241 242
	case MSG_SMS_DVBT_BDA_DATA:
		dvb_dmx_swfilter(&client->demux, (u8 *)(phdr + 1),
243
				 cb->size - sizeof(struct SmsMsgHdr_ST));
244 245 246
		break;

	case MSG_SMS_RF_TUNE_RES:
247
	case MSG_SMS_ISDBT_TUNE_RES:
248 249
		complete(&client->tune_done);
		break;
250

251 252 253 254 255 256 257 258 259 260 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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	case MSG_SMS_SIGNAL_DETECTED_IND:
		sms_info("MSG_SMS_SIGNAL_DETECTED_IND");
		client->sms_stat_dvb.TransmissionData.IsDemodLocked = true;
		is_status_update = true;
		break;

	case MSG_SMS_NO_SIGNAL_IND:
		sms_info("MSG_SMS_NO_SIGNAL_IND");
		client->sms_stat_dvb.TransmissionData.IsDemodLocked = false;
		is_status_update = true;
		break;

	case MSG_SMS_TRANSMISSION_IND: {
		sms_info("MSG_SMS_TRANSMISSION_IND");

		pMsgData++;
		memcpy(&client->sms_stat_dvb.TransmissionData, pMsgData,
				sizeof(struct TRANSMISSION_STATISTICS_S));

		/* Mo need to correct guard interval
		 * (as opposed to old statistics message).
		 */
		CORRECT_STAT_BANDWIDTH(client->sms_stat_dvb.TransmissionData);
		CORRECT_STAT_TRANSMISSON_MODE(
				client->sms_stat_dvb.TransmissionData);
		is_status_update = true;
		break;
	}
	case MSG_SMS_HO_PER_SLICES_IND: {
		struct RECEPTION_STATISTICS_S *pReceptionData =
				&client->sms_stat_dvb.ReceptionData;
		struct SRVM_SIGNAL_STATUS_S SignalStatusData;

		/*sms_info("MSG_SMS_HO_PER_SLICES_IND");*/
		pMsgData++;
		SignalStatusData.result = pMsgData[0];
		SignalStatusData.snr = pMsgData[1];
		SignalStatusData.inBandPower = (s32) pMsgData[2];
		SignalStatusData.tsPackets = pMsgData[3];
		SignalStatusData.etsPackets = pMsgData[4];
		SignalStatusData.constellation = pMsgData[5];
		SignalStatusData.hpCode = pMsgData[6];
		SignalStatusData.tpsSrvIndLP = pMsgData[7] & 0x03;
		SignalStatusData.tpsSrvIndHP = pMsgData[8] & 0x03;
		SignalStatusData.cellId = pMsgData[9] & 0xFFFF;
		SignalStatusData.reason = pMsgData[10];
		SignalStatusData.requestId = pMsgData[11];
		pReceptionData->IsRfLocked = pMsgData[16];
		pReceptionData->IsDemodLocked = pMsgData[17];
		pReceptionData->ModemState = pMsgData[12];
		pReceptionData->SNR = pMsgData[1];
		pReceptionData->BER = pMsgData[13];
		pReceptionData->RSSI = pMsgData[14];
		CORRECT_STAT_RSSI(client->sms_stat_dvb.ReceptionData);

		pReceptionData->InBandPwr = (s32) pMsgData[2];
		pReceptionData->CarrierOffset = (s32) pMsgData[15];
		pReceptionData->TotalTSPackets = pMsgData[3];
		pReceptionData->ErrorTSPackets = pMsgData[4];

		/* TS PER */
		if ((SignalStatusData.tsPackets + SignalStatusData.etsPackets)
				> 0) {
			pReceptionData->TS_PER = (SignalStatusData.etsPackets
					* 100) / (SignalStatusData.tsPackets
					+ SignalStatusData.etsPackets);
317
		} else {
318
			pReceptionData->TS_PER = 0;
319
		}
320

321 322
		pReceptionData->BERBitCount = pMsgData[18];
		pReceptionData->BERErrorCount = pMsgData[19];
323

324 325 326 327 328 329 330
		pReceptionData->MRC_SNR = pMsgData[20];
		pReceptionData->MRC_InBandPwr = pMsgData[21];
		pReceptionData->MRC_RSSI = pMsgData[22];

		is_status_update = true;
		break;
	}
331
	case MSG_SMS_GET_STATISTICS_RES: {
332 333 334 335
		union {
			struct SMSHOSTLIB_STATISTICS_ISDBT_ST  isdbt;
			struct SmsMsgStatisticsInfo_ST         dvb;
		} *p = (void *) (phdr + 1);
336 337 338 339 340 341
		struct RECEPTION_STATISTICS_S *pReceptionData =
				&client->sms_stat_dvb.ReceptionData;

		sms_info("MSG_SMS_GET_STATISTICS_RES");

		is_status_update = true;
342 343 344 345 346 347 348 349 350

		switch (smscore_get_device_mode(client->coredev)) {
		case DEVICE_MODE_ISDBT:
		case DEVICE_MODE_ISDBT_BDA:
			smsdvb_update_isdbt_stats(pReceptionData, &p->isdbt);
			break;
		default:
			smsdvb_update_dvb_stats(pReceptionData, &p->dvb.Stat);
		}
351 352 353 354 355 356 357 358 359 360 361 362 363 364
		if (!pReceptionData->IsDemodLocked) {
			pReceptionData->SNR = 0;
			pReceptionData->BER = 0;
			pReceptionData->BERErrorCount = 0;
			pReceptionData->InBandPwr = 0;
			pReceptionData->ErrorTSPackets = 0;
		}

		complete(&client->tune_done);
		break;
	}
	default:
		sms_info("Unhandled message %d", phdr->msgType);

365
	}
366 367
	smscore_putbuffer(client->coredev, cb);

368 369 370 371 372 373 374 375 376 377 378 379 380
	if (is_status_update) {
		if (client->sms_stat_dvb.ReceptionData.IsDemodLocked) {
			client->fe_status = FE_HAS_SIGNAL | FE_HAS_CARRIER
				| FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
			sms_board_dvb3_event(client, DVB3_EVENT_FE_LOCK);
			if (client->sms_stat_dvb.ReceptionData.ErrorTSPackets
					== 0)
				sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
			else
				sms_board_dvb3_event(client,
						DVB3_EVENT_UNC_ERR);

		} else {
381 382 383 384
			if (client->sms_stat_dvb.ReceptionData.IsRfLocked)
				client->fe_status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
			else
				client->fe_status = 0;
385 386 387 388
			sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
		}
	}

389 390 391
	return 0;
}

392
static void smsdvb_unregister_client(struct smsdvb_client_t *client)
393
{
394
	/* must be called under clientslock */
395 396 397 398 399 400 401 402 403 404 405

	list_del(&client->entry);

	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);
}

406
static void smsdvb_onremove(void *context)
407 408 409
{
	kmutex_lock(&g_smsdvb_clientslock);

410
	smsdvb_unregister_client((struct smsdvb_client_t *) context);
411 412 413 414 415 416

	kmutex_unlock(&g_smsdvb_clientslock);
}

static int smsdvb_start_feed(struct dvb_demux_feed *feed)
{
417 418 419
	struct smsdvb_client_t *client =
		container_of(feed->demux, struct smsdvb_client_t, demux);
	struct SmsMsgData_ST PidMsg;
420

421
	sms_debug("add pid %d(%x)",
422
		  feed->pid, feed->pid);
423 424 425 426 427 428 429 430

	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;

431
	smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
432 433
	return smsclient_sendrequest(client->smsclient,
				     &PidMsg, sizeof(PidMsg));
434 435 436 437
}

static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
{
438 439 440
	struct smsdvb_client_t *client =
		container_of(feed->demux, struct smsdvb_client_t, demux);
	struct SmsMsgData_ST PidMsg;
441

442
	sms_debug("remove pid %d(%x)",
443
		  feed->pid, feed->pid);
444 445 446 447 448 449 450 451

	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;

452
	smsendian_handle_tx_message((struct SmsMsgHdr_ST *)&PidMsg);
453 454
	return smsclient_sendrequest(client->smsclient,
				     &PidMsg, sizeof(PidMsg));
455 456
}

457
static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
458 459
					void *buffer, size_t size,
					struct completion *completion)
460
{
461 462 463 464
	int rc;

	smsendian_handle_tx_message((struct SmsMsgHdr_ST *)buffer);
	rc = smsclient_sendrequest(client->smsclient, buffer, size);
465 466 467
	if (rc < 0)
		return rc;

468 469 470
	return wait_for_completion_timeout(completion,
					   msecs_to_jiffies(2000)) ?
						0 : -ETIME;
471 472
}

473 474 475 476 477 478 479 480 481 482 483 484 485 486
static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
{
	int rc;
	struct SmsMsgHdr_ST Msg = { MSG_SMS_GET_STATISTICS_REQ,
				    DVBT_BDA_CONTROL_MSG_ID,
				    HIF_TASK,
				    sizeof(struct SmsMsgHdr_ST), 0 };

	rc = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
					  &client->tune_done);

	return rc;
}

487 488 489 490 491 492 493 494 495 496
static inline int led_feedback(struct smsdvb_client_t *client)
{
	if (client->fe_status & FE_HAS_LOCK)
		return sms_board_led_feedback(client->coredev,
			(client->sms_stat_dvb.ReceptionData.BER
			== 0) ? SMS_LED_HI : SMS_LED_LO);
	else
		return sms_board_led_feedback(client->coredev, SMS_LED_OFF);
}

497 498
static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
{
499
	int rc;
500 501
	struct smsdvb_client_t *client;
	client = container_of(fe, struct smsdvb_client_t, frontend);
502

503 504
	rc = smsdvb_send_statistics_request(client);

505
	*stat = client->fe_status;
506

507 508
	led_feedback(client);

509
	return rc;
510 511 512 513
}

static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
{
514
	int rc;
515 516
	struct smsdvb_client_t *client;
	client = container_of(fe, struct smsdvb_client_t, frontend);
517

518 519
	rc = smsdvb_send_statistics_request(client);

520
	*ber = client->sms_stat_dvb.ReceptionData.BER;
521

522 523
	led_feedback(client);

524
	return rc;
525 526 527 528
}

static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
{
529 530
	int rc;

531 532
	struct smsdvb_client_t *client;
	client = container_of(fe, struct smsdvb_client_t, frontend);
533

534 535
	rc = smsdvb_send_statistics_request(client);

536 537 538 539 540 541 542 543
	if (client->sms_stat_dvb.ReceptionData.InBandPwr < -95)
		*strength = 0;
		else if (client->sms_stat_dvb.ReceptionData.InBandPwr > -29)
			*strength = 100;
		else
			*strength =
				(client->sms_stat_dvb.ReceptionData.InBandPwr
				+ 95) * 3 / 2;
544

545 546
	led_feedback(client);

547
	return rc;
548 549 550 551
}

static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
{
552
	int rc;
553 554
	struct smsdvb_client_t *client;
	client = container_of(fe, struct smsdvb_client_t, frontend);
555

556 557
	rc = smsdvb_send_statistics_request(client);

558
	*snr = client->sms_stat_dvb.ReceptionData.SNR;
559

560 561
	led_feedback(client);

562
	return rc;
563 564
}

565 566
static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
{
567
	int rc;
568 569
	struct smsdvb_client_t *client;
	client = container_of(fe, struct smsdvb_client_t, frontend);
570

571 572
	rc = smsdvb_send_statistics_request(client);

573
	*ucblocks = client->sms_stat_dvb.ReceptionData.ErrorTSPackets;
574

575 576
	led_feedback(client);

577
	return rc;
578 579
}

580 581
static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
				    struct dvb_frontend_tune_settings *tune)
582
{
583
	sms_debug("");
584 585 586 587 588 589 590

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

591
static int smsdvb_dvbt_set_frontend(struct dvb_frontend *fe)
592
{
593
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
594 595
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);
596

597 598
	struct {
		struct SmsMsgHdr_ST	Msg;
599
		u32		Data[3];
600 601
	} Msg;

602 603
	int ret;

604 605 606
	client->fe_status = FE_HAS_SIGNAL;
	client->event_fe_state = -1;
	client->event_unc_state = -1;
607
	fe->dtv_property_cache.delivery_system = SYS_DVBT;
608 609 610 611 612

	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;
613
	Msg.Msg.msgLength = sizeof(Msg);
614
	Msg.Data[0] = c->frequency;
615 616
	Msg.Data[2] = 12000000;

617 618
	sms_info("%s: freq %d band %d", __func__, c->frequency,
		 c->bandwidth_hz);
619

620
	switch (c->bandwidth_hz / 1000000) {
621 622 623 624 625 626 627 628 629 630 631 632 633
	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;
634
	}
635 636 637 638 639 640 641 642 643 644 645 646 647 648
	/* 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 已提交
649
		/* previous tune didn't lock - enable LNA and tune again */
650 651
		sms_board_lna_control(client->coredev, 1);
	}
652

653 654
	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
					   &client->tune_done);
655 656
}

657
static int smsdvb_isdbt_set_frontend(struct dvb_frontend *fe)
658
{
659
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
660 661 662 663 664 665 666 667
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);

	struct {
		struct SmsMsgHdr_ST	Msg;
		u32		Data[4];
	} Msg;

668 669
	fe->dtv_property_cache.delivery_system = SYS_ISDBT;

670 671 672 673 674 675
	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);

676 677
	if (c->isdbt_sb_segment_idx == -1)
		c->isdbt_sb_segment_idx = 0;
678

679 680
	switch (c->isdbt_sb_segment_count) {
	case 3:
681 682
		Msg.Data[1] = BW_ISDBT_3SEG;
		break;
683
	case 1:
684 685
		Msg.Data[1] = BW_ISDBT_1SEG;
		break;
686
	case 0:	/* AUTO */
687
		switch (c->bandwidth_hz / 1000000) {
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
		case 8:
		case 7:
			c->isdbt_sb_segment_count = 3;
			Msg.Data[1] = BW_ISDBT_3SEG;
			break;
		case 6:
			c->isdbt_sb_segment_count = 1;
			Msg.Data[1] = BW_ISDBT_1SEG;
			break;
		default: /* Assumes 6 MHZ bw */
			c->isdbt_sb_segment_count = 1;
			c->bandwidth_hz = 6000;
			Msg.Data[1] = BW_ISDBT_1SEG;
			break;
		}
		break;
704
	default:
705
		sms_info("Segment count %d not supported", c->isdbt_sb_segment_count);
706 707 708
		return -EINVAL;
	}

709 710 711 712 713 714 715 716
	Msg.Data[0] = c->frequency;
	Msg.Data[2] = 12000000;
	Msg.Data[3] = c->isdbt_sb_segment_idx;

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

717 718 719 720
	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
					   &client->tune_done);
}

721
static int smsdvb_set_frontend(struct dvb_frontend *fe)
722 723 724 725 726 727 728 729
{
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);
	struct smscore_device_t *coredev = client->coredev;

	switch (smscore_get_device_mode(coredev)) {
	case DEVICE_MODE_DVBT:
	case DEVICE_MODE_DVBT_BDA:
730
		return smsdvb_dvbt_set_frontend(fe);
731 732
	case DEVICE_MODE_ISDBT:
	case DEVICE_MODE_ISDBT_BDA:
733
		return smsdvb_isdbt_set_frontend(fe);
734 735 736 737 738
	default:
		return -EINVAL;
	}
}

739
static int smsdvb_get_frontend(struct dvb_frontend *fe)
740
{
741
	struct dtv_frontend_properties *fep = &fe->dtv_property_cache;
742 743
	struct smsdvb_client_t *client =
		container_of(fe, struct smsdvb_client_t, frontend);
744 745 746
	struct smscore_device_t *coredev = client->coredev;
	struct TRANSMISSION_STATISTICS_S *td =
		&client->sms_stat_dvb.TransmissionData;
747

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 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 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
	switch (smscore_get_device_mode(coredev)) {
	case DEVICE_MODE_DVBT:
	case DEVICE_MODE_DVBT_BDA:
		fep->frequency = td->Frequency;

		switch (td->Bandwidth) {
		case 6:
			fep->bandwidth_hz = 6000000;
			break;
		case 7:
			fep->bandwidth_hz = 7000000;
			break;
		case 8:
			fep->bandwidth_hz = 8000000;
			break;
		}

		switch (td->TransmissionMode) {
		case 2:
			fep->transmission_mode = TRANSMISSION_MODE_2K;
			break;
		case 8:
			fep->transmission_mode = TRANSMISSION_MODE_8K;
		}

		switch (td->GuardInterval) {
		case 0:
			fep->guard_interval = GUARD_INTERVAL_1_32;
			break;
		case 1:
			fep->guard_interval = GUARD_INTERVAL_1_16;
			break;
		case 2:
			fep->guard_interval = GUARD_INTERVAL_1_8;
			break;
		case 3:
			fep->guard_interval = GUARD_INTERVAL_1_4;
			break;
		}

		switch (td->CodeRate) {
		case 0:
			fep->code_rate_HP = FEC_1_2;
			break;
		case 1:
			fep->code_rate_HP = FEC_2_3;
			break;
		case 2:
			fep->code_rate_HP = FEC_3_4;
			break;
		case 3:
			fep->code_rate_HP = FEC_5_6;
			break;
		case 4:
			fep->code_rate_HP = FEC_7_8;
			break;
		}

		switch (td->LPCodeRate) {
		case 0:
			fep->code_rate_LP = FEC_1_2;
			break;
		case 1:
			fep->code_rate_LP = FEC_2_3;
			break;
		case 2:
			fep->code_rate_LP = FEC_3_4;
			break;
		case 3:
			fep->code_rate_LP = FEC_5_6;
			break;
		case 4:
			fep->code_rate_LP = FEC_7_8;
			break;
		}

		switch (td->Constellation) {
		case 0:
			fep->modulation = QPSK;
			break;
		case 1:
			fep->modulation = QAM_16;
			break;
		case 2:
			fep->modulation = QAM_64;
			break;
		}

		switch (td->Hierarchy) {
		case 0:
			fep->hierarchy = HIERARCHY_NONE;
			break;
		case 1:
			fep->hierarchy = HIERARCHY_1;
			break;
		case 2:
			fep->hierarchy = HIERARCHY_2;
			break;
		case 3:
			fep->hierarchy = HIERARCHY_4;
			break;
		}
850

851 852 853 854 855 856 857 858 859 860 861
		fep->inversion = INVERSION_AUTO;
		break;
	case DEVICE_MODE_ISDBT:
	case DEVICE_MODE_ISDBT_BDA:
		fep->frequency = td->Frequency;
		fep->bandwidth_hz = 6000000;
		/* todo: retrive the other parameters */
		break;
	default:
		return -EINVAL;
	}
862 863 864 865 866 867 868 869 870

	return 0;
}

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

871 872
	sms_board_power(client->coredev, 1);

873
	sms_board_dvb3_event(client, DVB3_EVENT_INIT);
874 875 876 877 878 879 880 881
	return 0;
}

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

882 883 884
	sms_board_led_feedback(client->coredev, SMS_LED_OFF);
	sms_board_power(client->coredev, 0);

885
	sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
886

887 888 889 890 891
	return 0;
}

static void smsdvb_release(struct dvb_frontend *fe)
{
892
	/* do nothing */
893 894 895 896
}

static struct dvb_frontend_ops smsdvb_fe_ops = {
	.info = {
897
		.name			= "Siano Mobile Digital MDTV Receiver",
898 899 900 901
		.frequency_min		= 44250000,
		.frequency_max		= 867250000,
		.frequency_stepsize	= 250000,
		.caps = FE_CAN_INVERSION_AUTO |
902 903 904 905 906 907 908
			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,
909 910 911 912
	},

	.release = smsdvb_release,

913 914
	.set_frontend = smsdvb_set_frontend,
	.get_frontend = smsdvb_get_frontend,
915 916 917 918 919 920
	.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,
921
	.read_ucblocks = smsdvb_read_ucblocks,
922 923 924

	.init = smsdvb_init,
	.sleep = smsdvb_sleep,
925 926
};

927 928
static int smsdvb_hotplug(struct smscore_device_t *coredev,
			  struct device *device, int arrival)
929
{
930 931
	struct smsclient_params_t params;
	struct smsdvb_client_t *client;
932 933
	int rc;

934
	/* device removal handled by onremove callback */
935 936
	if (!arrival)
		return 0;
937
	client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
938
	if (!client) {
939
		sms_err("kmalloc() failed");
940 941 942
		return -ENOMEM;
	}

943
	/* register dvb adapter */
944 945 946
	rc = dvb_register_adapter(&client->adapter,
				  sms_get_board(
					smscore_get_board_id(coredev))->name,
947 948
				  THIS_MODULE, device, adapter_nr);
	if (rc < 0) {
949
		sms_err("dvb_register_adapter() failed %d", rc);
950 951 952
		goto adapter_error;
	}

953
	/* init dvb demux */
954
	client->demux.dmx.capabilities = DMX_TS_FILTERING;
955
	client->demux.filternum = 32; /* todo: nova ??? */
956 957 958 959 960
	client->demux.feednum = 32;
	client->demux.start_feed = smsdvb_start_feed;
	client->demux.stop_feed = smsdvb_stop_feed;

	rc = dvb_dmx_init(&client->demux);
961
	if (rc < 0) {
962
		sms_err("dvb_dmx_init failed %d", rc);
963 964 965
		goto dvbdmx_error;
	}

966
	/* init dmxdev */
967 968 969 970 971
	client->dmxdev.filternum = 32;
	client->dmxdev.demux = &client->demux.dmx;
	client->dmxdev.capabilities = 0;

	rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
972
	if (rc < 0) {
973
		sms_err("dvb_dmxdev_init failed %d", rc);
974 975 976
		goto dmxdev_error;
	}

977
	/* init and register frontend */
978 979
	memcpy(&client->frontend.ops, &smsdvb_fe_ops,
	       sizeof(struct dvb_frontend_ops));
980

981 982 983
	switch (smscore_get_device_mode(coredev)) {
	case DEVICE_MODE_DVBT:
	case DEVICE_MODE_DVBT_BDA:
984
		client->frontend.ops.delsys[0] = SYS_DVBT;
985 986 987
		break;
	case DEVICE_MODE_ISDBT:
	case DEVICE_MODE_ISDBT_BDA:
988
		client->frontend.ops.delsys[0] = SYS_ISDBT;
989 990 991
		break;
	}

992
	rc = dvb_register_frontend(&client->adapter, &client->frontend);
993
	if (rc < 0) {
994
		sms_err("frontend registration failed %d", rc);
995 996 997
		goto frontend_error;
	}

998
	params.initial_id = 1;
999 1000 1001 1002 1003 1004
	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);
1005
	if (rc < 0) {
1006
		sms_err("smscore_register_client() failed %d", rc);
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
		goto client_error;
	}

	client->coredev = coredev;

	init_completion(&client->tune_done);

	kmutex_lock(&g_smsdvb_clientslock);

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

	kmutex_unlock(&g_smsdvb_clientslock);

1020 1021 1022
	client->event_fe_state = -1;
	client->event_unc_state = -1;
	sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
1023

1024
	sms_info("success");
1025 1026
	sms_board_setup(coredev);

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	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;
}

1046
static int __init smsdvb_module_init(void)
1047 1048 1049 1050 1051 1052 1053 1054
{
	int rc;

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

	rc = smscore_register_hotplug(smsdvb_hotplug);

1055
	sms_debug("");
1056 1057 1058 1059

	return rc;
}

1060
static void __exit smsdvb_module_exit(void)
1061 1062 1063 1064 1065 1066
{
	smscore_unregister_hotplug(smsdvb_hotplug);

	kmutex_lock(&g_smsdvb_clientslock);

	while (!list_empty(&g_smsdvb_clients))
1067
	       smsdvb_unregister_client(
1068
			(struct smsdvb_client_t *) g_smsdvb_clients.next);
1069 1070 1071

	kmutex_unlock(&g_smsdvb_clientslock);
}
1072 1073 1074 1075 1076

module_init(smsdvb_module_init);
module_exit(smsdvb_module_exit);

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