smsdvb-main.c revision e1b2ac4d1e6bb214823c42bb807a6cc5f21aa223
1/****************************************************************
2
3Siano Mobile Silicon, Inc.
4MDTV receiver kernel modules.
5Copyright (C) 2006-2008, Uri Shkolnik
6
7This program is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20****************************************************************/
21
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/init.h>
25
26#include "dmxdev.h"
27#include "dvbdev.h"
28#include "dvb_demux.h"
29#include "dvb_frontend.h"
30
31#include "smscoreapi.h"
32#include "sms-cards.h"
33
34#include "smsdvb.h"
35
36DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
37
38static struct list_head g_smsdvb_clients;
39static struct mutex g_smsdvb_clientslock;
40
41static int sms_dbg;
42module_param_named(debug, sms_dbg, int, 0644);
43MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");
44
45
46u32 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,
51};
52
53u32 sms_to_code_rate_table[] = {
54	[0] = FEC_1_2,
55	[1] = FEC_2_3,
56	[2] = FEC_3_4,
57	[3] = FEC_5_6,
58	[4] = FEC_7_8,
59};
60
61
62u32 sms_to_hierarchy_table[] = {
63	[0] = HIERARCHY_NONE,
64	[1] = HIERARCHY_1,
65	[2] = HIERARCHY_2,
66	[3] = HIERARCHY_4,
67};
68
69u32 sms_to_modulation_table[] = {
70	[0] = QPSK,
71	[1] = QAM_16,
72	[2] = QAM_64,
73	[3] = DQPSK,
74};
75
76
77/* Events that may come from DVB v3 adapter */
78static void sms_board_dvb3_event(struct smsdvb_client_t *client,
79		enum SMS_DVB3_EVENTS event) {
80
81	struct smscore_device_t *coredev = client->coredev;
82	switch (event) {
83	case DVB3_EVENT_INIT:
84		sms_debug("DVB3_EVENT_INIT");
85		sms_board_event(coredev, BOARD_EVENT_BIND);
86		break;
87	case DVB3_EVENT_SLEEP:
88		sms_debug("DVB3_EVENT_SLEEP");
89		sms_board_event(coredev, BOARD_EVENT_POWER_SUSPEND);
90		break;
91	case DVB3_EVENT_HOTPLUG:
92		sms_debug("DVB3_EVENT_HOTPLUG");
93		sms_board_event(coredev, BOARD_EVENT_POWER_INIT);
94		break;
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);
100		}
101		break;
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);
107		}
108		break;
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);
114		}
115		break;
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);
121		}
122		break;
123
124	default:
125		sms_err("Unknown dvb3 api event");
126		break;
127	}
128}
129
130static void smsdvb_stats_not_ready(struct dvb_frontend *fe)
131{
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;
136	int i, n_layers;
137
138	switch (smscore_get_device_mode(coredev)) {
139	case DEVICE_MODE_ISDBT:
140	case DEVICE_MODE_ISDBT_BDA:
141		n_layers = 4;
142	default:
143		n_layers = 1;
144	}
145
146	/* Global stats */
147	c->strength.len = 1;
148	c->cnr.len = 1;
149	c->strength.stat[0].scale = FE_SCALE_DECIBEL;
150	c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
151
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;
157
158	/*
159	 * Put all of them at FE_SCALE_NOT_AVAILABLE. They're dynamically
160	 * changed when the stats become available.
161	 */
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;
167	}
168}
169
170static inline int sms_to_mode(u32 mode)
171{
172	switch (mode) {
173	case 2:
174		return TRANSMISSION_MODE_2K;
175	case 4:
176		return TRANSMISSION_MODE_4K;
177	case 8:
178		return TRANSMISSION_MODE_8K;
179	}
180	return TRANSMISSION_MODE_AUTO;
181}
182
183static inline int sms_to_status(u32 is_demod_locked, u32 is_rf_locked)
184{
185	if (is_demod_locked)
186		return FE_HAS_SIGNAL  | FE_HAS_CARRIER | FE_HAS_VITERBI |
187		       FE_HAS_SYNC    | FE_HAS_LOCK;
188
189	if (is_rf_locked)
190		return FE_HAS_SIGNAL | FE_HAS_CARRIER;
191
192	return 0;
193}
194
195static inline u32 sms_to_bw(u32 value)
196{
197	return value * 1000000;
198}
199
200#define convert_from_table(value, table, defval) ({			\
201	u32 __ret;							\
202	if (value < ARRAY_SIZE(table))					\
203		__ret = table[value];					\
204	else								\
205		__ret = defval;						\
206	__ret;								\
207})
208
209#define sms_to_guard_interval(value)					\
210	convert_from_table(value, sms_to_guard_interval_table,		\
211			   GUARD_INTERVAL_AUTO);
212
213#define sms_to_code_rate(value)						\
214	convert_from_table(value, sms_to_code_rate_table,		\
215			   FEC_NONE);
216
217#define sms_to_hierarchy(value)						\
218	convert_from_table(value, sms_to_hierarchy_table,		\
219			   FEC_NONE);
220
221#define sms_to_modulation(value)					\
222	convert_from_table(value, sms_to_modulation_table,		\
223			   FEC_NONE);
224
225static void smsdvb_update_tx_params(struct smsdvb_client_t *client,
226				    struct TRANSMISSION_STATISTICS_S *p)
227{
228	struct dvb_frontend *fe = &client->frontend;
229	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
230
231	c->frequency = p->Frequency;
232	client->fe_status = sms_to_status(p->IsDemodLocked, 0);
233	c->bandwidth_hz = sms_to_bw(p->Bandwidth);
234	c->transmission_mode = sms_to_mode(p->TransmissionMode);
235	c->guard_interval = sms_to_guard_interval(p->GuardInterval);
236	c->code_rate_HP = sms_to_code_rate(p->CodeRate);
237	c->code_rate_LP = sms_to_code_rate(p->LPCodeRate);
238	c->hierarchy = sms_to_hierarchy(p->Hierarchy);
239	c->modulation = sms_to_modulation(p->Constellation);
240}
241
242static void smsdvb_update_per_slices(struct smsdvb_client_t *client,
243				     struct RECEPTION_STATISTICS_PER_SLICES_S *p)
244{
245	struct dvb_frontend *fe = &client->frontend;
246	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
247
248	client->fe_status = sms_to_status(p->IsDemodLocked, p->IsRfLocked);
249	c->modulation = sms_to_modulation(p->constellation);
250
251	/* Signal Strength, in DBm */
252	c->strength.stat[0].uvalue = p->inBandPower * 1000;
253
254	/* Carrier to Noise ratio, in DB */
255	c->cnr.stat[0].svalue = p->snr * 1000;
256
257	/* PER/BER requires demod lock */
258	if (!p->IsDemodLocked)
259		return;
260
261	/* TS PER */
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->etsPackets;
266	c->block_count.stat[0].uvalue += p->etsPackets + p->tsPackets;
267
268	/* BER */
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->BERErrorCount;
272	c->post_bit_count.stat[0].uvalue += p->BERBitCount;
273
274	/* Legacy PER/BER */
275	client->legacy_per = (p->etsPackets * 65535) /
276			     (p->tsPackets + p->etsPackets);
277}
278
279static void smsdvb_update_dvb_stats(struct smsdvb_client_t *client,
280				    struct SMSHOSTLIB_STATISTICS_ST *p)
281{
282	struct dvb_frontend *fe = &client->frontend;
283	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
284
285	if (client->prt_dvb_stats)
286		client->prt_dvb_stats(client->debug_data, p);
287
288	client->fe_status = sms_to_status(p->IsDemodLocked, p->IsRfLocked);
289
290	/* Update DVB modulation parameters */
291	c->frequency = p->Frequency;
292	client->fe_status = sms_to_status(p->IsDemodLocked, 0);
293	c->bandwidth_hz = sms_to_bw(p->Bandwidth);
294	c->transmission_mode = sms_to_mode(p->TransmissionMode);
295	c->guard_interval = sms_to_guard_interval(p->GuardInterval);
296	c->code_rate_HP = sms_to_code_rate(p->CodeRate);
297	c->code_rate_LP = sms_to_code_rate(p->LPCodeRate);
298	c->hierarchy = sms_to_hierarchy(p->Hierarchy);
299	c->modulation = sms_to_modulation(p->Constellation);
300
301	/* update reception data */
302	c->lna = p->IsExternalLNAOn ? 1 : 0;
303
304	/* Carrier to Noise ratio, in DB */
305	c->cnr.stat[0].svalue = p->SNR * 1000;
306
307	/* Signal Strength, in DBm */
308	c->strength.stat[0].uvalue = p->InBandPwr * 1000;
309
310	/* PER/BER requires demod lock */
311	if (!p->IsDemodLocked)
312		return;
313
314	/* TS PER */
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->ErrorTSPackets;
319	c->block_count.stat[0].uvalue += p->TotalTSPackets;
320
321	/* BER */
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->BERErrorCount;
325	c->post_bit_count.stat[0].uvalue += p->BERBitCount;
326
327	/* Legacy PER/BER */
328	client->legacy_ber = p->BER;
329};
330
331static void smsdvb_update_isdbt_stats(struct smsdvb_client_t *client,
332				      struct SMSHOSTLIB_STATISTICS_ISDBT_ST *p)
333{
334	struct dvb_frontend *fe = &client->frontend;
335	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
336	struct SMSHOSTLIB_ISDBT_LAYER_STAT_ST *lr;
337	int i, n_layers;
338
339	if (client->prt_isdb_stats)
340		client->prt_isdb_stats(client->debug_data, p);
341
342	/* Update ISDB-T transmission parameters */
343	c->frequency = p->Frequency;
344	client->fe_status = sms_to_status(p->IsDemodLocked, 0);
345	c->bandwidth_hz = sms_to_bw(p->Bandwidth);
346	c->transmission_mode = sms_to_mode(p->TransmissionMode);
347	c->guard_interval = sms_to_guard_interval(p->GuardInterval);
348	c->isdbt_partial_reception = p->PartialReception ? 1 : 0;
349	n_layers = p->NumOfLayers;
350	if (n_layers < 1)
351		n_layers = 1;
352	if (n_layers > 3)
353		n_layers = 3;
354	c->isdbt_layer_enabled = 0;
355
356	/* update reception data */
357	c->lna = p->IsExternalLNAOn ? 1 : 0;
358
359	/* Carrier to Noise ratio, in DB */
360	c->cnr.stat[0].svalue = p->SNR * 1000;
361
362	/* Signal Strength, in DBm */
363	c->strength.stat[0].uvalue = p->InBandPwr * 1000;
364
365	/* PER/BER and per-layer stats require demod lock */
366	if (!p->IsDemodLocked)
367		return;
368
369	client->last_per = c->block_error.stat[0].uvalue;
370
371	/* Clears global counters, as the code below will sum it again */
372	c->block_error.stat[0].uvalue = 0;
373	c->block_count.stat[0].uvalue = 0;
374	c->block_error.stat[0].scale = FE_SCALE_COUNTER;
375	c->block_count.stat[0].scale = FE_SCALE_COUNTER;
376	c->post_bit_error.stat[0].uvalue = 0;
377	c->post_bit_count.stat[0].uvalue = 0;
378	c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
379	c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
380
381	for (i = 0; i < n_layers; i++) {
382		lr = &p->LayerInfo[i];
383
384		/* Update per-layer transmission parameters */
385		if (lr->NumberOfSegments > 0 && lr->NumberOfSegments < 13) {
386			c->isdbt_layer_enabled |= 1 << i;
387			c->layer[i].segment_count = lr->NumberOfSegments;
388		} else {
389			continue;
390		}
391		c->layer[i].modulation = sms_to_modulation(lr->Constellation);
392
393		/* TS PER */
394		c->block_error.stat[i + 1].scale = FE_SCALE_COUNTER;
395		c->block_count.stat[i + 1].scale = FE_SCALE_COUNTER;
396		c->block_error.stat[i + 1].uvalue += lr->ErrorTSPackets;
397		c->block_count.stat[i + 1].uvalue += lr->TotalTSPackets;
398
399		/* Update global PER counter */
400		c->block_error.stat[0].uvalue += lr->ErrorTSPackets;
401		c->block_count.stat[0].uvalue += lr->TotalTSPackets;
402
403		/* BER */
404		c->post_bit_error.stat[i + 1].scale = FE_SCALE_COUNTER;
405		c->post_bit_count.stat[i + 1].scale = FE_SCALE_COUNTER;
406		c->post_bit_error.stat[i + 1].uvalue += lr->BERErrorCount;
407		c->post_bit_count.stat[i + 1].uvalue += lr->BERBitCount;
408
409		/* Update global BER counter */
410		c->post_bit_error.stat[0].uvalue += lr->BERErrorCount;
411		c->post_bit_count.stat[0].uvalue += lr->BERBitCount;
412	}
413}
414
415static void smsdvb_update_isdbt_stats_ex(struct smsdvb_client_t *client,
416					 struct SMSHOSTLIB_STATISTICS_ISDBT_EX_ST *p)
417{
418	struct dvb_frontend *fe = &client->frontend;
419	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
420	struct SMSHOSTLIB_ISDBT_LAYER_STAT_ST *lr;
421	int i, n_layers;
422
423	if (client->prt_isdb_stats_ex)
424		client->prt_isdb_stats_ex(client->debug_data, p);
425
426	/* Update ISDB-T transmission parameters */
427	c->frequency = p->Frequency;
428	client->fe_status = sms_to_status(p->IsDemodLocked, 0);
429	c->bandwidth_hz = sms_to_bw(p->Bandwidth);
430	c->transmission_mode = sms_to_mode(p->TransmissionMode);
431	c->guard_interval = sms_to_guard_interval(p->GuardInterval);
432	c->isdbt_partial_reception = p->PartialReception ? 1 : 0;
433	n_layers = p->NumOfLayers;
434	if (n_layers < 1)
435		n_layers = 1;
436	if (n_layers > 3)
437		n_layers = 3;
438	c->isdbt_layer_enabled = 0;
439
440	/* update reception data */
441	c->lna = p->IsExternalLNAOn ? 1 : 0;
442
443	/* Carrier to Noise ratio, in DB */
444	c->cnr.stat[0].svalue = p->SNR * 1000;
445
446	/* Signal Strength, in DBm */
447	c->strength.stat[0].uvalue = p->InBandPwr * 1000;
448
449	/* PER/BER and per-layer stats require demod lock */
450	if (!p->IsDemodLocked)
451		return;
452
453	client->last_per = c->block_error.stat[0].uvalue;
454
455	/* Clears global counters, as the code below will sum it again */
456	c->block_error.stat[0].uvalue = 0;
457	c->block_count.stat[0].uvalue = 0;
458	c->block_error.stat[0].scale = FE_SCALE_COUNTER;
459	c->block_count.stat[0].scale = FE_SCALE_COUNTER;
460	c->post_bit_error.stat[0].uvalue = 0;
461	c->post_bit_count.stat[0].uvalue = 0;
462	c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
463	c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
464
465	c->post_bit_error.len = n_layers + 1;
466	c->post_bit_count.len = n_layers + 1;
467	c->block_error.len = n_layers + 1;
468	c->block_count.len = n_layers + 1;
469	for (i = 0; i < n_layers; i++) {
470		lr = &p->LayerInfo[i];
471
472		/* Update per-layer transmission parameters */
473		if (lr->NumberOfSegments > 0 && lr->NumberOfSegments < 13) {
474			c->isdbt_layer_enabled |= 1 << i;
475			c->layer[i].segment_count = lr->NumberOfSegments;
476		} else {
477			continue;
478		}
479		c->layer[i].modulation = sms_to_modulation(lr->Constellation);
480
481		/* TS PER */
482		c->block_error.stat[i + 1].scale = FE_SCALE_COUNTER;
483		c->block_count.stat[i + 1].scale = FE_SCALE_COUNTER;
484		c->block_error.stat[i + 1].uvalue += lr->ErrorTSPackets;
485		c->block_count.stat[i + 1].uvalue += lr->TotalTSPackets;
486
487		/* Update global PER counter */
488		c->block_error.stat[0].uvalue += lr->ErrorTSPackets;
489		c->block_count.stat[0].uvalue += lr->TotalTSPackets;
490
491		/* BER */
492		c->post_bit_error.stat[i + 1].scale = FE_SCALE_COUNTER;
493		c->post_bit_count.stat[i + 1].scale = FE_SCALE_COUNTER;
494		c->post_bit_error.stat[i + 1].uvalue += lr->BERErrorCount;
495		c->post_bit_count.stat[i + 1].uvalue += lr->BERBitCount;
496
497		/* Update global BER counter */
498		c->post_bit_error.stat[0].uvalue += lr->BERErrorCount;
499		c->post_bit_count.stat[0].uvalue += lr->BERBitCount;
500	}
501}
502
503static int smsdvb_onresponse(void *context, struct smscore_buffer_t *cb)
504{
505	struct smsdvb_client_t *client = (struct smsdvb_client_t *) context;
506	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) (((u8 *) cb->p)
507			+ cb->offset);
508	void *p = phdr + 1;
509	struct dvb_frontend *fe = &client->frontend;
510	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
511	bool is_status_update = false;
512
513	switch (phdr->msgType) {
514	case MSG_SMS_DVBT_BDA_DATA:
515		/*
516		 * Only feed data to dvb demux if are there any feed listening
517		 * to it and if the device has tuned
518		 */
519		if (client->feed_users && client->has_tuned)
520			dvb_dmx_swfilter(&client->demux, p,
521					 cb->size - sizeof(struct SmsMsgHdr_ST));
522		break;
523
524	case MSG_SMS_RF_TUNE_RES:
525	case MSG_SMS_ISDBT_TUNE_RES:
526		complete(&client->tune_done);
527		break;
528
529	case MSG_SMS_SIGNAL_DETECTED_IND:
530		client->fe_status = FE_HAS_SIGNAL  | FE_HAS_CARRIER |
531				    FE_HAS_VITERBI | FE_HAS_SYNC    |
532				    FE_HAS_LOCK;
533
534		is_status_update = true;
535		break;
536
537	case MSG_SMS_NO_SIGNAL_IND:
538		client->fe_status = 0;
539
540		is_status_update = true;
541		break;
542
543	case MSG_SMS_TRANSMISSION_IND:
544		smsdvb_update_tx_params(client, p);
545
546		is_status_update = true;
547		break;
548
549	case MSG_SMS_HO_PER_SLICES_IND:
550		smsdvb_update_per_slices(client, p);
551
552		is_status_update = true;
553		break;
554
555	case MSG_SMS_GET_STATISTICS_RES:
556		switch (smscore_get_device_mode(client->coredev)) {
557		case DEVICE_MODE_ISDBT:
558		case DEVICE_MODE_ISDBT_BDA:
559			smsdvb_update_isdbt_stats(client, p);
560			break;
561		default:
562			/* Skip SmsMsgStatisticsInfo_ST:RequestResult field */
563			smsdvb_update_dvb_stats(client, p + sizeof(u32));
564		}
565
566		is_status_update = true;
567		break;
568
569	/* Only for ISDB-T */
570	case MSG_SMS_GET_STATISTICS_EX_RES:
571		/* Skip SmsMsgStatisticsInfo_ST:RequestResult field? */
572		smsdvb_update_isdbt_stats_ex(client, p + sizeof(u32));
573		is_status_update = true;
574		break;
575	default:
576		sms_info("message not handled");
577	}
578	smscore_putbuffer(client->coredev, cb);
579
580	if (is_status_update) {
581		if (client->fe_status & FE_HAS_LOCK) {
582			sms_board_dvb3_event(client, DVB3_EVENT_FE_LOCK);
583			if (client->last_per == c->block_error.stat[0].uvalue)
584				sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
585			else
586				sms_board_dvb3_event(client, DVB3_EVENT_UNC_ERR);
587			client->has_tuned = true;
588		} else {
589			smsdvb_stats_not_ready(fe);
590			client->has_tuned = false;
591			sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
592		}
593		complete(&client->stats_done);
594	}
595
596	return 0;
597}
598
599static void smsdvb_unregister_client(struct smsdvb_client_t *client)
600{
601	/* must be called under clientslock */
602
603	list_del(&client->entry);
604
605	smsdvb_debugfs_release(client);
606	smscore_unregister_client(client->smsclient);
607	dvb_unregister_frontend(&client->frontend);
608	dvb_dmxdev_release(&client->dmxdev);
609	dvb_dmx_release(&client->demux);
610	dvb_unregister_adapter(&client->adapter);
611	kfree(client);
612}
613
614static void smsdvb_onremove(void *context)
615{
616	kmutex_lock(&g_smsdvb_clientslock);
617
618	smsdvb_unregister_client((struct smsdvb_client_t *) context);
619
620	kmutex_unlock(&g_smsdvb_clientslock);
621}
622
623static int smsdvb_start_feed(struct dvb_demux_feed *feed)
624{
625	struct smsdvb_client_t *client =
626		container_of(feed->demux, struct smsdvb_client_t, demux);
627	struct SmsMsgData_ST PidMsg;
628
629	sms_debug("add pid %d(%x)",
630		  feed->pid, feed->pid);
631
632	client->feed_users++;
633
634	PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
635	PidMsg.xMsgHeader.msgDstId = HIF_TASK;
636	PidMsg.xMsgHeader.msgFlags = 0;
637	PidMsg.xMsgHeader.msgType  = MSG_SMS_ADD_PID_FILTER_REQ;
638	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
639	PidMsg.msgData[0] = feed->pid;
640
641	return smsclient_sendrequest(client->smsclient,
642				     &PidMsg, sizeof(PidMsg));
643}
644
645static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
646{
647	struct smsdvb_client_t *client =
648		container_of(feed->demux, struct smsdvb_client_t, demux);
649	struct SmsMsgData_ST PidMsg;
650
651	sms_debug("remove pid %d(%x)",
652		  feed->pid, feed->pid);
653
654	client->feed_users--;
655
656	PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
657	PidMsg.xMsgHeader.msgDstId = HIF_TASK;
658	PidMsg.xMsgHeader.msgFlags = 0;
659	PidMsg.xMsgHeader.msgType  = MSG_SMS_REMOVE_PID_FILTER_REQ;
660	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
661	PidMsg.msgData[0] = feed->pid;
662
663	return smsclient_sendrequest(client->smsclient,
664				     &PidMsg, sizeof(PidMsg));
665}
666
667static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
668					void *buffer, size_t size,
669					struct completion *completion)
670{
671	int rc;
672
673	rc = smsclient_sendrequest(client->smsclient, buffer, size);
674	if (rc < 0)
675		return rc;
676
677	return wait_for_completion_timeout(completion,
678					   msecs_to_jiffies(2000)) ?
679						0 : -ETIME;
680}
681
682static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
683{
684	int rc;
685	struct SmsMsgHdr_ST Msg;
686
687	/* Don't request stats too fast */
688	if (client->get_stats_jiffies &&
689	   (!time_after(jiffies, client->get_stats_jiffies)))
690		return 0;
691	client->get_stats_jiffies = jiffies + msecs_to_jiffies(100);
692
693	Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
694	Msg.msgDstId = HIF_TASK;
695	Msg.msgFlags = 0;
696	Msg.msgLength = sizeof(Msg);
697
698	switch (smscore_get_device_mode(client->coredev)) {
699	case DEVICE_MODE_ISDBT:
700	case DEVICE_MODE_ISDBT_BDA:
701		/*
702		* Check for firmware version, to avoid breaking for old cards
703		*/
704		if (client->coredev->fw_version >= 0x800)
705			Msg.msgType = MSG_SMS_GET_STATISTICS_EX_REQ;
706		else
707			Msg.msgType = MSG_SMS_GET_STATISTICS_REQ;
708		break;
709	default:
710		Msg.msgType = MSG_SMS_GET_STATISTICS_REQ;
711	}
712
713	rc = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
714					 &client->stats_done);
715
716	return rc;
717}
718
719static inline int led_feedback(struct smsdvb_client_t *client)
720{
721	if (!(client->fe_status & FE_HAS_LOCK))
722		return sms_board_led_feedback(client->coredev, SMS_LED_OFF);
723
724	return sms_board_led_feedback(client->coredev,
725				     (client->legacy_ber == 0) ?
726				     SMS_LED_HI : SMS_LED_LO);
727}
728
729static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
730{
731	int rc;
732	struct smsdvb_client_t *client;
733	client = container_of(fe, struct smsdvb_client_t, frontend);
734
735	rc = smsdvb_send_statistics_request(client);
736
737	*stat = client->fe_status;
738
739	led_feedback(client);
740
741	return rc;
742}
743
744static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
745{
746	int rc;
747	struct smsdvb_client_t *client;
748
749	client = container_of(fe, struct smsdvb_client_t, frontend);
750
751	rc = smsdvb_send_statistics_request(client);
752
753	*ber = client->legacy_ber;
754
755	led_feedback(client);
756
757	return rc;
758}
759
760static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
761{
762	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
763	int rc;
764	s32 power = (s32) c->strength.stat[0].uvalue;
765	struct smsdvb_client_t *client;
766
767	client = container_of(fe, struct smsdvb_client_t, frontend);
768
769	rc = smsdvb_send_statistics_request(client);
770
771	if (power < -95)
772		*strength = 0;
773		else if (power > -29)
774			*strength = 65535;
775		else
776			*strength = (power + 95) * 65535 / 66;
777
778	led_feedback(client);
779
780	return rc;
781}
782
783static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
784{
785	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
786	int rc;
787	struct smsdvb_client_t *client;
788
789	client = container_of(fe, struct smsdvb_client_t, frontend);
790
791	rc = smsdvb_send_statistics_request(client);
792
793	/* Preferred scale for SNR with legacy API: 0.1 dB */
794	*snr = c->cnr.stat[0].svalue / 100;
795
796	led_feedback(client);
797
798	return rc;
799}
800
801static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
802{
803	int rc;
804	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
805	struct smsdvb_client_t *client;
806
807	client = container_of(fe, struct smsdvb_client_t, frontend);
808
809	rc = smsdvb_send_statistics_request(client);
810
811	*ucblocks = c->block_error.stat[0].uvalue;
812
813	led_feedback(client);
814
815	return rc;
816}
817
818static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
819				    struct dvb_frontend_tune_settings *tune)
820{
821	sms_debug("");
822
823	tune->min_delay_ms = 400;
824	tune->step_size = 250000;
825	tune->max_drift = 0;
826	return 0;
827}
828
829static int smsdvb_dvbt_set_frontend(struct dvb_frontend *fe)
830{
831	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
832	struct smsdvb_client_t *client =
833		container_of(fe, struct smsdvb_client_t, frontend);
834
835	struct {
836		struct SmsMsgHdr_ST	Msg;
837		u32		Data[3];
838	} Msg;
839
840	int ret;
841
842	client->fe_status = 0;
843	client->event_fe_state = -1;
844	client->event_unc_state = -1;
845	fe->dtv_property_cache.delivery_system = SYS_DVBT;
846
847	Msg.Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
848	Msg.Msg.msgDstId = HIF_TASK;
849	Msg.Msg.msgFlags = 0;
850	Msg.Msg.msgType = MSG_SMS_RF_TUNE_REQ;
851	Msg.Msg.msgLength = sizeof(Msg);
852	Msg.Data[0] = c->frequency;
853	Msg.Data[2] = 12000000;
854
855	sms_info("%s: freq %d band %d", __func__, c->frequency,
856		 c->bandwidth_hz);
857
858	switch (c->bandwidth_hz / 1000000) {
859	case 8:
860		Msg.Data[1] = BW_8_MHZ;
861		break;
862	case 7:
863		Msg.Data[1] = BW_7_MHZ;
864		break;
865	case 6:
866		Msg.Data[1] = BW_6_MHZ;
867		break;
868	case 0:
869		return -EOPNOTSUPP;
870	default:
871		return -EINVAL;
872	}
873	/* Disable LNA, if any. An error is returned if no LNA is present */
874	ret = sms_board_lna_control(client->coredev, 0);
875	if (ret == 0) {
876		fe_status_t status;
877
878		/* tune with LNA off at first */
879		ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
880						  &client->tune_done);
881
882		smsdvb_read_status(fe, &status);
883
884		if (status & FE_HAS_LOCK)
885			return ret;
886
887		/* previous tune didn't lock - enable LNA and tune again */
888		sms_board_lna_control(client->coredev, 1);
889	}
890
891	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
892					   &client->tune_done);
893}
894
895static int smsdvb_isdbt_set_frontend(struct dvb_frontend *fe)
896{
897	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
898	struct smsdvb_client_t *client =
899		container_of(fe, struct smsdvb_client_t, frontend);
900	int board_id = smscore_get_board_id(client->coredev);
901	struct sms_board *board = sms_get_board(board_id);
902	enum sms_device_type_st type = board->type;
903	int ret;
904
905	struct {
906		struct SmsMsgHdr_ST	Msg;
907		u32		Data[4];
908	} Msg;
909
910	fe->dtv_property_cache.delivery_system = SYS_ISDBT;
911
912	Msg.Msg.msgSrcId  = DVBT_BDA_CONTROL_MSG_ID;
913	Msg.Msg.msgDstId  = HIF_TASK;
914	Msg.Msg.msgFlags  = 0;
915	Msg.Msg.msgType   = MSG_SMS_ISDBT_TUNE_REQ;
916	Msg.Msg.msgLength = sizeof(Msg);
917
918	if (c->isdbt_sb_segment_idx == -1)
919		c->isdbt_sb_segment_idx = 0;
920
921	if (!c->isdbt_layer_enabled)
922		c->isdbt_layer_enabled = 7;
923
924	Msg.Data[0] = c->frequency;
925	Msg.Data[1] = BW_ISDBT_1SEG;
926	Msg.Data[2] = 12000000;
927	Msg.Data[3] = c->isdbt_sb_segment_idx;
928
929	if (c->isdbt_partial_reception) {
930		if ((type == SMS_PELE || type == SMS_RIO) &&
931		    c->isdbt_sb_segment_count > 3)
932			Msg.Data[1] = BW_ISDBT_13SEG;
933		else if (c->isdbt_sb_segment_count > 1)
934			Msg.Data[1] = BW_ISDBT_3SEG;
935	} else if (type == SMS_PELE || type == SMS_RIO)
936		Msg.Data[1] = BW_ISDBT_13SEG;
937
938	c->bandwidth_hz = 6000000;
939
940	sms_info("%s: freq %d segwidth %d segindex %d\n", __func__,
941		 c->frequency, c->isdbt_sb_segment_count,
942		 c->isdbt_sb_segment_idx);
943
944	/* Disable LNA, if any. An error is returned if no LNA is present */
945	ret = sms_board_lna_control(client->coredev, 0);
946	if (ret == 0) {
947		fe_status_t status;
948
949		/* tune with LNA off at first */
950		ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
951						  &client->tune_done);
952
953		smsdvb_read_status(fe, &status);
954
955		if (status & FE_HAS_LOCK)
956			return ret;
957
958		/* previous tune didn't lock - enable LNA and tune again */
959		sms_board_lna_control(client->coredev, 1);
960	}
961	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
962					   &client->tune_done);
963}
964
965static int smsdvb_set_frontend(struct dvb_frontend *fe)
966{
967	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
968	struct smsdvb_client_t *client =
969		container_of(fe, struct smsdvb_client_t, frontend);
970	struct smscore_device_t *coredev = client->coredev;
971
972	smsdvb_stats_not_ready(fe);
973	c->strength.stat[0].uvalue = 0;
974	c->cnr.stat[0].uvalue = 0;
975
976	client->has_tuned = false;
977
978	switch (smscore_get_device_mode(coredev)) {
979	case DEVICE_MODE_DVBT:
980	case DEVICE_MODE_DVBT_BDA:
981		return smsdvb_dvbt_set_frontend(fe);
982	case DEVICE_MODE_ISDBT:
983	case DEVICE_MODE_ISDBT_BDA:
984		return smsdvb_isdbt_set_frontend(fe);
985	default:
986		return -EINVAL;
987	}
988}
989
990/* Nothing to do here, as stats are automatically updated */
991static int smsdvb_get_frontend(struct dvb_frontend *fe)
992{
993	return 0;
994}
995
996static int smsdvb_init(struct dvb_frontend *fe)
997{
998	struct smsdvb_client_t *client =
999		container_of(fe, struct smsdvb_client_t, frontend);
1000
1001	sms_board_power(client->coredev, 1);
1002
1003	sms_board_dvb3_event(client, DVB3_EVENT_INIT);
1004	return 0;
1005}
1006
1007static int smsdvb_sleep(struct dvb_frontend *fe)
1008{
1009	struct smsdvb_client_t *client =
1010		container_of(fe, struct smsdvb_client_t, frontend);
1011
1012	sms_board_led_feedback(client->coredev, SMS_LED_OFF);
1013	sms_board_power(client->coredev, 0);
1014
1015	sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
1016
1017	return 0;
1018}
1019
1020static void smsdvb_release(struct dvb_frontend *fe)
1021{
1022	/* do nothing */
1023}
1024
1025static struct dvb_frontend_ops smsdvb_fe_ops = {
1026	.info = {
1027		.name			= "Siano Mobile Digital MDTV Receiver",
1028		.frequency_min		= 44250000,
1029		.frequency_max		= 867250000,
1030		.frequency_stepsize	= 250000,
1031		.caps = FE_CAN_INVERSION_AUTO |
1032			FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1033			FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1034			FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1035			FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
1036			FE_CAN_GUARD_INTERVAL_AUTO |
1037			FE_CAN_RECOVER |
1038			FE_CAN_HIERARCHY_AUTO,
1039	},
1040
1041	.release = smsdvb_release,
1042
1043	.set_frontend = smsdvb_set_frontend,
1044	.get_frontend = smsdvb_get_frontend,
1045	.get_tune_settings = smsdvb_get_tune_settings,
1046
1047	.read_status = smsdvb_read_status,
1048	.read_ber = smsdvb_read_ber,
1049	.read_signal_strength = smsdvb_read_signal_strength,
1050	.read_snr = smsdvb_read_snr,
1051	.read_ucblocks = smsdvb_read_ucblocks,
1052
1053	.init = smsdvb_init,
1054	.sleep = smsdvb_sleep,
1055};
1056
1057static int smsdvb_hotplug(struct smscore_device_t *coredev,
1058			  struct device *device, int arrival)
1059{
1060	struct smsclient_params_t params;
1061	struct smsdvb_client_t *client;
1062	int rc;
1063
1064	/* device removal handled by onremove callback */
1065	if (!arrival)
1066		return 0;
1067	client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
1068	if (!client) {
1069		sms_err("kmalloc() failed");
1070		return -ENOMEM;
1071	}
1072
1073	/* register dvb adapter */
1074	rc = dvb_register_adapter(&client->adapter,
1075				  sms_get_board(
1076					smscore_get_board_id(coredev))->name,
1077				  THIS_MODULE, device, adapter_nr);
1078	if (rc < 0) {
1079		sms_err("dvb_register_adapter() failed %d", rc);
1080		goto adapter_error;
1081	}
1082
1083	/* init dvb demux */
1084	client->demux.dmx.capabilities = DMX_TS_FILTERING;
1085	client->demux.filternum = 32; /* todo: nova ??? */
1086	client->demux.feednum = 32;
1087	client->demux.start_feed = smsdvb_start_feed;
1088	client->demux.stop_feed = smsdvb_stop_feed;
1089
1090	rc = dvb_dmx_init(&client->demux);
1091	if (rc < 0) {
1092		sms_err("dvb_dmx_init failed %d", rc);
1093		goto dvbdmx_error;
1094	}
1095
1096	/* init dmxdev */
1097	client->dmxdev.filternum = 32;
1098	client->dmxdev.demux = &client->demux.dmx;
1099	client->dmxdev.capabilities = 0;
1100
1101	rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
1102	if (rc < 0) {
1103		sms_err("dvb_dmxdev_init failed %d", rc);
1104		goto dmxdev_error;
1105	}
1106
1107	/* init and register frontend */
1108	memcpy(&client->frontend.ops, &smsdvb_fe_ops,
1109	       sizeof(struct dvb_frontend_ops));
1110
1111	switch (smscore_get_device_mode(coredev)) {
1112	case DEVICE_MODE_DVBT:
1113	case DEVICE_MODE_DVBT_BDA:
1114		client->frontend.ops.delsys[0] = SYS_DVBT;
1115		break;
1116	case DEVICE_MODE_ISDBT:
1117	case DEVICE_MODE_ISDBT_BDA:
1118		client->frontend.ops.delsys[0] = SYS_ISDBT;
1119		break;
1120	}
1121
1122	rc = dvb_register_frontend(&client->adapter, &client->frontend);
1123	if (rc < 0) {
1124		sms_err("frontend registration failed %d", rc);
1125		goto frontend_error;
1126	}
1127
1128	params.initial_id = 1;
1129	params.data_type = MSG_SMS_DVBT_BDA_DATA;
1130	params.onresponse_handler = smsdvb_onresponse;
1131	params.onremove_handler = smsdvb_onremove;
1132	params.context = client;
1133
1134	rc = smscore_register_client(coredev, &params, &client->smsclient);
1135	if (rc < 0) {
1136		sms_err("smscore_register_client() failed %d", rc);
1137		goto client_error;
1138	}
1139
1140	client->coredev = coredev;
1141
1142	init_completion(&client->tune_done);
1143	init_completion(&client->stats_done);
1144
1145	kmutex_lock(&g_smsdvb_clientslock);
1146
1147	list_add(&client->entry, &g_smsdvb_clients);
1148
1149	kmutex_unlock(&g_smsdvb_clientslock);
1150
1151	client->event_fe_state = -1;
1152	client->event_unc_state = -1;
1153	sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
1154
1155	sms_info("success");
1156	sms_board_setup(coredev);
1157
1158	if (smsdvb_debugfs_create(client) < 0)
1159		sms_info("failed to create debugfs node");
1160
1161	return 0;
1162
1163client_error:
1164	dvb_unregister_frontend(&client->frontend);
1165
1166frontend_error:
1167	dvb_dmxdev_release(&client->dmxdev);
1168
1169dmxdev_error:
1170	dvb_dmx_release(&client->demux);
1171
1172dvbdmx_error:
1173	dvb_unregister_adapter(&client->adapter);
1174
1175adapter_error:
1176	kfree(client);
1177	return rc;
1178}
1179
1180static int __init smsdvb_module_init(void)
1181{
1182	int rc;
1183
1184	INIT_LIST_HEAD(&g_smsdvb_clients);
1185	kmutex_init(&g_smsdvb_clientslock);
1186
1187	smsdvb_debugfs_register();
1188
1189	rc = smscore_register_hotplug(smsdvb_hotplug);
1190
1191	sms_debug("");
1192
1193	return rc;
1194}
1195
1196static void __exit smsdvb_module_exit(void)
1197{
1198	smscore_unregister_hotplug(smsdvb_hotplug);
1199
1200	kmutex_lock(&g_smsdvb_clientslock);
1201
1202	while (!list_empty(&g_smsdvb_clients))
1203		smsdvb_unregister_client((struct smsdvb_client_t *)g_smsdvb_clients.next);
1204
1205	smsdvb_debugfs_unregister();
1206
1207	kmutex_unlock(&g_smsdvb_clientslock);
1208}
1209
1210module_init(smsdvb_module_init);
1211module_exit(smsdvb_module_exit);
1212
1213MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
1214MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
1215MODULE_LICENSE("GPL");
1216