smsdvb-main.c revision 5c3b87435b291efb260aec37fdbe397859e550c5
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		dvb_dmx_swfilter(&client->demux, p,
516				 cb->size - sizeof(struct SmsMsgHdr_ST));
517		break;
518
519	case MSG_SMS_RF_TUNE_RES:
520	case MSG_SMS_ISDBT_TUNE_RES:
521		complete(&client->tune_done);
522		break;
523
524	case MSG_SMS_SIGNAL_DETECTED_IND:
525		client->fe_status = FE_HAS_SIGNAL  | FE_HAS_CARRIER |
526				    FE_HAS_VITERBI | FE_HAS_SYNC    |
527				    FE_HAS_LOCK;
528
529		is_status_update = true;
530		break;
531
532	case MSG_SMS_NO_SIGNAL_IND:
533		client->fe_status = 0;
534
535		is_status_update = true;
536		break;
537
538	case MSG_SMS_TRANSMISSION_IND:
539		smsdvb_update_tx_params(client, p);
540
541		is_status_update = true;
542		break;
543
544	case MSG_SMS_HO_PER_SLICES_IND:
545		smsdvb_update_per_slices(client, p);
546
547		is_status_update = true;
548		break;
549
550	case MSG_SMS_GET_STATISTICS_RES:
551		switch (smscore_get_device_mode(client->coredev)) {
552		case DEVICE_MODE_ISDBT:
553		case DEVICE_MODE_ISDBT_BDA:
554			smsdvb_update_isdbt_stats(client, p);
555			break;
556		default:
557			/* Skip SmsMsgStatisticsInfo_ST:RequestResult field */
558			smsdvb_update_dvb_stats(client, p + sizeof(u32));
559		}
560
561		is_status_update = true;
562		break;
563
564	/* Only for ISDB-T */
565	case MSG_SMS_GET_STATISTICS_EX_RES:
566		/* Skip SmsMsgStatisticsInfo_ST:RequestResult field? */
567		smsdvb_update_isdbt_stats_ex(client, p + sizeof(u32));
568		is_status_update = true;
569		break;
570	default:
571		sms_info("message not handled");
572	}
573	smscore_putbuffer(client->coredev, cb);
574
575	if (is_status_update) {
576		if (client->fe_status & FE_HAS_LOCK) {
577			sms_board_dvb3_event(client, DVB3_EVENT_FE_LOCK);
578			if (client->last_per == c->block_error.stat[0].uvalue)
579				sms_board_dvb3_event(client, DVB3_EVENT_UNC_OK);
580			else
581				sms_board_dvb3_event(client, DVB3_EVENT_UNC_ERR);
582		} else {
583			smsdvb_stats_not_ready(fe);
584
585			sms_board_dvb3_event(client, DVB3_EVENT_FE_UNLOCK);
586		}
587		complete(&client->stats_done);
588	}
589
590	return 0;
591}
592
593static void smsdvb_unregister_client(struct smsdvb_client_t *client)
594{
595	/* must be called under clientslock */
596
597	list_del(&client->entry);
598
599	smsdvb_debugfs_release(client);
600	smscore_unregister_client(client->smsclient);
601	dvb_unregister_frontend(&client->frontend);
602	dvb_dmxdev_release(&client->dmxdev);
603	dvb_dmx_release(&client->demux);
604	dvb_unregister_adapter(&client->adapter);
605	kfree(client);
606}
607
608static void smsdvb_onremove(void *context)
609{
610	kmutex_lock(&g_smsdvb_clientslock);
611
612	smsdvb_unregister_client((struct smsdvb_client_t *) context);
613
614	kmutex_unlock(&g_smsdvb_clientslock);
615}
616
617static int smsdvb_start_feed(struct dvb_demux_feed *feed)
618{
619	struct smsdvb_client_t *client =
620		container_of(feed->demux, struct smsdvb_client_t, demux);
621	struct SmsMsgData_ST PidMsg;
622
623	sms_debug("add pid %d(%x)",
624		  feed->pid, feed->pid);
625
626	PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
627	PidMsg.xMsgHeader.msgDstId = HIF_TASK;
628	PidMsg.xMsgHeader.msgFlags = 0;
629	PidMsg.xMsgHeader.msgType  = MSG_SMS_ADD_PID_FILTER_REQ;
630	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
631	PidMsg.msgData[0] = feed->pid;
632
633	return smsclient_sendrequest(client->smsclient,
634				     &PidMsg, sizeof(PidMsg));
635}
636
637static int smsdvb_stop_feed(struct dvb_demux_feed *feed)
638{
639	struct smsdvb_client_t *client =
640		container_of(feed->demux, struct smsdvb_client_t, demux);
641	struct SmsMsgData_ST PidMsg;
642
643	sms_debug("remove pid %d(%x)",
644		  feed->pid, feed->pid);
645
646	PidMsg.xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
647	PidMsg.xMsgHeader.msgDstId = HIF_TASK;
648	PidMsg.xMsgHeader.msgFlags = 0;
649	PidMsg.xMsgHeader.msgType  = MSG_SMS_REMOVE_PID_FILTER_REQ;
650	PidMsg.xMsgHeader.msgLength = sizeof(PidMsg);
651	PidMsg.msgData[0] = feed->pid;
652
653	return smsclient_sendrequest(client->smsclient,
654				     &PidMsg, sizeof(PidMsg));
655}
656
657static int smsdvb_sendrequest_and_wait(struct smsdvb_client_t *client,
658					void *buffer, size_t size,
659					struct completion *completion)
660{
661	int rc;
662
663	rc = smsclient_sendrequest(client->smsclient, buffer, size);
664	if (rc < 0)
665		return rc;
666
667	return wait_for_completion_timeout(completion,
668					   msecs_to_jiffies(2000)) ?
669						0 : -ETIME;
670}
671
672static int smsdvb_send_statistics_request(struct smsdvb_client_t *client)
673{
674	int rc;
675	struct SmsMsgHdr_ST Msg;
676
677	/* Don't request stats too fast */
678	if (client->get_stats_jiffies &&
679	   (!time_after(jiffies, client->get_stats_jiffies)))
680		return 0;
681	client->get_stats_jiffies = jiffies + msecs_to_jiffies(100);
682
683	Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
684	Msg.msgDstId = HIF_TASK;
685	Msg.msgFlags = 0;
686	Msg.msgLength = sizeof(Msg);
687
688	switch (smscore_get_device_mode(client->coredev)) {
689	case DEVICE_MODE_ISDBT:
690	case DEVICE_MODE_ISDBT_BDA:
691		/*
692		* Check for firmware version, to avoid breaking for old cards
693		*/
694		if (client->coredev->fw_version >= 0x800)
695			Msg.msgType = MSG_SMS_GET_STATISTICS_EX_REQ;
696		else
697			Msg.msgType = MSG_SMS_GET_STATISTICS_REQ;
698		break;
699	default:
700		Msg.msgType = MSG_SMS_GET_STATISTICS_REQ;
701	}
702
703	rc = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
704					 &client->stats_done);
705
706	return rc;
707}
708
709static inline int led_feedback(struct smsdvb_client_t *client)
710{
711	if (!(client->fe_status & FE_HAS_LOCK))
712		return sms_board_led_feedback(client->coredev, SMS_LED_OFF);
713
714	return sms_board_led_feedback(client->coredev,
715				     (client->legacy_ber == 0) ?
716				     SMS_LED_HI : SMS_LED_LO);
717}
718
719static int smsdvb_read_status(struct dvb_frontend *fe, fe_status_t *stat)
720{
721	int rc;
722	struct smsdvb_client_t *client;
723	client = container_of(fe, struct smsdvb_client_t, frontend);
724
725	rc = smsdvb_send_statistics_request(client);
726
727	*stat = client->fe_status;
728
729	led_feedback(client);
730
731	return rc;
732}
733
734static int smsdvb_read_ber(struct dvb_frontend *fe, u32 *ber)
735{
736	int rc;
737	struct smsdvb_client_t *client;
738
739	client = container_of(fe, struct smsdvb_client_t, frontend);
740
741	rc = smsdvb_send_statistics_request(client);
742
743	*ber = client->legacy_ber;
744
745	led_feedback(client);
746
747	return rc;
748}
749
750static int smsdvb_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
751{
752	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
753	int rc;
754	s32 power = (s32) c->strength.stat[0].uvalue;
755	struct smsdvb_client_t *client;
756
757	client = container_of(fe, struct smsdvb_client_t, frontend);
758
759	rc = smsdvb_send_statistics_request(client);
760
761	if (power < -95)
762		*strength = 0;
763		else if (power > -29)
764			*strength = 65535;
765		else
766			*strength = (power + 95) * 65535 / 66;
767
768	led_feedback(client);
769
770	return rc;
771}
772
773static int smsdvb_read_snr(struct dvb_frontend *fe, u16 *snr)
774{
775	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
776	int rc;
777	struct smsdvb_client_t *client;
778
779	client = container_of(fe, struct smsdvb_client_t, frontend);
780
781	rc = smsdvb_send_statistics_request(client);
782
783	/* Preferred scale for SNR with legacy API: 0.1 dB */
784	*snr = c->cnr.stat[0].svalue / 100;
785
786	led_feedback(client);
787
788	return rc;
789}
790
791static int smsdvb_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
792{
793	int rc;
794	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
795	struct smsdvb_client_t *client;
796
797	client = container_of(fe, struct smsdvb_client_t, frontend);
798
799	rc = smsdvb_send_statistics_request(client);
800
801	*ucblocks = c->block_error.stat[0].uvalue;
802
803	led_feedback(client);
804
805	return rc;
806}
807
808static int smsdvb_get_tune_settings(struct dvb_frontend *fe,
809				    struct dvb_frontend_tune_settings *tune)
810{
811	sms_debug("");
812
813	tune->min_delay_ms = 400;
814	tune->step_size = 250000;
815	tune->max_drift = 0;
816	return 0;
817}
818
819static int smsdvb_dvbt_set_frontend(struct dvb_frontend *fe)
820{
821	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
822	struct smsdvb_client_t *client =
823		container_of(fe, struct smsdvb_client_t, frontend);
824
825	struct {
826		struct SmsMsgHdr_ST	Msg;
827		u32		Data[3];
828	} Msg;
829
830	int ret;
831
832	client->fe_status = 0;
833	client->event_fe_state = -1;
834	client->event_unc_state = -1;
835	fe->dtv_property_cache.delivery_system = SYS_DVBT;
836
837	Msg.Msg.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
838	Msg.Msg.msgDstId = HIF_TASK;
839	Msg.Msg.msgFlags = 0;
840	Msg.Msg.msgType = MSG_SMS_RF_TUNE_REQ;
841	Msg.Msg.msgLength = sizeof(Msg);
842	Msg.Data[0] = c->frequency;
843	Msg.Data[2] = 12000000;
844
845	sms_info("%s: freq %d band %d", __func__, c->frequency,
846		 c->bandwidth_hz);
847
848	switch (c->bandwidth_hz / 1000000) {
849	case 8:
850		Msg.Data[1] = BW_8_MHZ;
851		break;
852	case 7:
853		Msg.Data[1] = BW_7_MHZ;
854		break;
855	case 6:
856		Msg.Data[1] = BW_6_MHZ;
857		break;
858	case 0:
859		return -EOPNOTSUPP;
860	default:
861		return -EINVAL;
862	}
863	/* Disable LNA, if any. An error is returned if no LNA is present */
864	ret = sms_board_lna_control(client->coredev, 0);
865	if (ret == 0) {
866		fe_status_t status;
867
868		/* tune with LNA off at first */
869		ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
870						  &client->tune_done);
871
872		smsdvb_read_status(fe, &status);
873
874		if (status & FE_HAS_LOCK)
875			return ret;
876
877		/* previous tune didn't lock - enable LNA and tune again */
878		sms_board_lna_control(client->coredev, 1);
879	}
880
881	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
882					   &client->tune_done);
883}
884
885static int smsdvb_isdbt_set_frontend(struct dvb_frontend *fe)
886{
887	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
888	struct smsdvb_client_t *client =
889		container_of(fe, struct smsdvb_client_t, frontend);
890	int board_id = smscore_get_board_id(client->coredev);
891	struct sms_board *board = sms_get_board(board_id);
892	enum sms_device_type_st type = board->type;
893	int ret;
894
895	struct {
896		struct SmsMsgHdr_ST	Msg;
897		u32		Data[4];
898	} Msg;
899
900	fe->dtv_property_cache.delivery_system = SYS_ISDBT;
901
902	Msg.Msg.msgSrcId  = DVBT_BDA_CONTROL_MSG_ID;
903	Msg.Msg.msgDstId  = HIF_TASK;
904	Msg.Msg.msgFlags  = 0;
905	Msg.Msg.msgType   = MSG_SMS_ISDBT_TUNE_REQ;
906	Msg.Msg.msgLength = sizeof(Msg);
907
908	if (c->isdbt_sb_segment_idx == -1)
909		c->isdbt_sb_segment_idx = 0;
910
911	if (!c->isdbt_layer_enabled)
912		c->isdbt_layer_enabled = 7;
913
914	Msg.Data[0] = c->frequency;
915	Msg.Data[1] = BW_ISDBT_1SEG;
916	Msg.Data[2] = 12000000;
917	Msg.Data[3] = c->isdbt_sb_segment_idx;
918
919	if (c->isdbt_partial_reception) {
920		if ((type == SMS_PELE || type == SMS_RIO) &&
921		    c->isdbt_sb_segment_count > 3)
922			Msg.Data[1] = BW_ISDBT_13SEG;
923		else if (c->isdbt_sb_segment_count > 1)
924			Msg.Data[1] = BW_ISDBT_3SEG;
925	} else if (type == SMS_PELE || type == SMS_RIO)
926		Msg.Data[1] = BW_ISDBT_13SEG;
927
928	c->bandwidth_hz = 6000000;
929
930	sms_info("%s: freq %d segwidth %d segindex %d\n", __func__,
931		 c->frequency, c->isdbt_sb_segment_count,
932		 c->isdbt_sb_segment_idx);
933
934	/* Disable LNA, if any. An error is returned if no LNA is present */
935	ret = sms_board_lna_control(client->coredev, 0);
936	if (ret == 0) {
937		fe_status_t status;
938
939		/* tune with LNA off at first */
940		ret = smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
941						  &client->tune_done);
942
943		smsdvb_read_status(fe, &status);
944
945		if (status & FE_HAS_LOCK)
946			return ret;
947
948		/* previous tune didn't lock - enable LNA and tune again */
949		sms_board_lna_control(client->coredev, 1);
950	}
951	return smsdvb_sendrequest_and_wait(client, &Msg, sizeof(Msg),
952					   &client->tune_done);
953}
954
955static int smsdvb_set_frontend(struct dvb_frontend *fe)
956{
957	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
958	struct smsdvb_client_t *client =
959		container_of(fe, struct smsdvb_client_t, frontend);
960	struct smscore_device_t *coredev = client->coredev;
961
962	smsdvb_stats_not_ready(fe);
963	c->strength.stat[0].uvalue = 0;
964	c->cnr.stat[0].uvalue = 0;
965
966	switch (smscore_get_device_mode(coredev)) {
967	case DEVICE_MODE_DVBT:
968	case DEVICE_MODE_DVBT_BDA:
969		return smsdvb_dvbt_set_frontend(fe);
970	case DEVICE_MODE_ISDBT:
971	case DEVICE_MODE_ISDBT_BDA:
972		return smsdvb_isdbt_set_frontend(fe);
973	default:
974		return -EINVAL;
975	}
976}
977
978/* Nothing to do here, as stats are automatically updated */
979static int smsdvb_get_frontend(struct dvb_frontend *fe)
980{
981	return 0;
982}
983
984static int smsdvb_init(struct dvb_frontend *fe)
985{
986	struct smsdvb_client_t *client =
987		container_of(fe, struct smsdvb_client_t, frontend);
988
989	sms_board_power(client->coredev, 1);
990
991	sms_board_dvb3_event(client, DVB3_EVENT_INIT);
992	return 0;
993}
994
995static int smsdvb_sleep(struct dvb_frontend *fe)
996{
997	struct smsdvb_client_t *client =
998		container_of(fe, struct smsdvb_client_t, frontend);
999
1000	sms_board_led_feedback(client->coredev, SMS_LED_OFF);
1001	sms_board_power(client->coredev, 0);
1002
1003	sms_board_dvb3_event(client, DVB3_EVENT_SLEEP);
1004
1005	return 0;
1006}
1007
1008static void smsdvb_release(struct dvb_frontend *fe)
1009{
1010	/* do nothing */
1011}
1012
1013static struct dvb_frontend_ops smsdvb_fe_ops = {
1014	.info = {
1015		.name			= "Siano Mobile Digital MDTV Receiver",
1016		.frequency_min		= 44250000,
1017		.frequency_max		= 867250000,
1018		.frequency_stepsize	= 250000,
1019		.caps = FE_CAN_INVERSION_AUTO |
1020			FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
1021			FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
1022			FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
1023			FE_CAN_QAM_AUTO | FE_CAN_TRANSMISSION_MODE_AUTO |
1024			FE_CAN_GUARD_INTERVAL_AUTO |
1025			FE_CAN_RECOVER |
1026			FE_CAN_HIERARCHY_AUTO,
1027	},
1028
1029	.release = smsdvb_release,
1030
1031	.set_frontend = smsdvb_set_frontend,
1032	.get_frontend = smsdvb_get_frontend,
1033	.get_tune_settings = smsdvb_get_tune_settings,
1034
1035	.read_status = smsdvb_read_status,
1036	.read_ber = smsdvb_read_ber,
1037	.read_signal_strength = smsdvb_read_signal_strength,
1038	.read_snr = smsdvb_read_snr,
1039	.read_ucblocks = smsdvb_read_ucblocks,
1040
1041	.init = smsdvb_init,
1042	.sleep = smsdvb_sleep,
1043};
1044
1045static int smsdvb_hotplug(struct smscore_device_t *coredev,
1046			  struct device *device, int arrival)
1047{
1048	struct smsclient_params_t params;
1049	struct smsdvb_client_t *client;
1050	int rc;
1051
1052	/* device removal handled by onremove callback */
1053	if (!arrival)
1054		return 0;
1055	client = kzalloc(sizeof(struct smsdvb_client_t), GFP_KERNEL);
1056	if (!client) {
1057		sms_err("kmalloc() failed");
1058		return -ENOMEM;
1059	}
1060
1061	/* register dvb adapter */
1062	rc = dvb_register_adapter(&client->adapter,
1063				  sms_get_board(
1064					smscore_get_board_id(coredev))->name,
1065				  THIS_MODULE, device, adapter_nr);
1066	if (rc < 0) {
1067		sms_err("dvb_register_adapter() failed %d", rc);
1068		goto adapter_error;
1069	}
1070
1071	/* init dvb demux */
1072	client->demux.dmx.capabilities = DMX_TS_FILTERING;
1073	client->demux.filternum = 32; /* todo: nova ??? */
1074	client->demux.feednum = 32;
1075	client->demux.start_feed = smsdvb_start_feed;
1076	client->demux.stop_feed = smsdvb_stop_feed;
1077
1078	rc = dvb_dmx_init(&client->demux);
1079	if (rc < 0) {
1080		sms_err("dvb_dmx_init failed %d", rc);
1081		goto dvbdmx_error;
1082	}
1083
1084	/* init dmxdev */
1085	client->dmxdev.filternum = 32;
1086	client->dmxdev.demux = &client->demux.dmx;
1087	client->dmxdev.capabilities = 0;
1088
1089	rc = dvb_dmxdev_init(&client->dmxdev, &client->adapter);
1090	if (rc < 0) {
1091		sms_err("dvb_dmxdev_init failed %d", rc);
1092		goto dmxdev_error;
1093	}
1094
1095	/* init and register frontend */
1096	memcpy(&client->frontend.ops, &smsdvb_fe_ops,
1097	       sizeof(struct dvb_frontend_ops));
1098
1099	switch (smscore_get_device_mode(coredev)) {
1100	case DEVICE_MODE_DVBT:
1101	case DEVICE_MODE_DVBT_BDA:
1102		client->frontend.ops.delsys[0] = SYS_DVBT;
1103		break;
1104	case DEVICE_MODE_ISDBT:
1105	case DEVICE_MODE_ISDBT_BDA:
1106		client->frontend.ops.delsys[0] = SYS_ISDBT;
1107		break;
1108	}
1109
1110	rc = dvb_register_frontend(&client->adapter, &client->frontend);
1111	if (rc < 0) {
1112		sms_err("frontend registration failed %d", rc);
1113		goto frontend_error;
1114	}
1115
1116	params.initial_id = 1;
1117	params.data_type = MSG_SMS_DVBT_BDA_DATA;
1118	params.onresponse_handler = smsdvb_onresponse;
1119	params.onremove_handler = smsdvb_onremove;
1120	params.context = client;
1121
1122	rc = smscore_register_client(coredev, &params, &client->smsclient);
1123	if (rc < 0) {
1124		sms_err("smscore_register_client() failed %d", rc);
1125		goto client_error;
1126	}
1127
1128	client->coredev = coredev;
1129
1130	init_completion(&client->tune_done);
1131	init_completion(&client->stats_done);
1132
1133	kmutex_lock(&g_smsdvb_clientslock);
1134
1135	list_add(&client->entry, &g_smsdvb_clients);
1136
1137	kmutex_unlock(&g_smsdvb_clientslock);
1138
1139	client->event_fe_state = -1;
1140	client->event_unc_state = -1;
1141	sms_board_dvb3_event(client, DVB3_EVENT_HOTPLUG);
1142
1143	sms_info("success");
1144	sms_board_setup(coredev);
1145
1146	if (smsdvb_debugfs_create(client) < 0)
1147		sms_info("failed to create debugfs node");
1148
1149	return 0;
1150
1151client_error:
1152	dvb_unregister_frontend(&client->frontend);
1153
1154frontend_error:
1155	dvb_dmxdev_release(&client->dmxdev);
1156
1157dmxdev_error:
1158	dvb_dmx_release(&client->demux);
1159
1160dvbdmx_error:
1161	dvb_unregister_adapter(&client->adapter);
1162
1163adapter_error:
1164	kfree(client);
1165	return rc;
1166}
1167
1168static int __init smsdvb_module_init(void)
1169{
1170	int rc;
1171
1172	INIT_LIST_HEAD(&g_smsdvb_clients);
1173	kmutex_init(&g_smsdvb_clientslock);
1174
1175	smsdvb_debugfs_register();
1176
1177	rc = smscore_register_hotplug(smsdvb_hotplug);
1178
1179	sms_debug("");
1180
1181	return rc;
1182}
1183
1184static void __exit smsdvb_module_exit(void)
1185{
1186	smscore_unregister_hotplug(smsdvb_hotplug);
1187
1188	kmutex_lock(&g_smsdvb_clientslock);
1189
1190	while (!list_empty(&g_smsdvb_clients))
1191		smsdvb_unregister_client((struct smsdvb_client_t *)g_smsdvb_clients.next);
1192
1193	smsdvb_debugfs_unregister();
1194
1195	kmutex_unlock(&g_smsdvb_clientslock);
1196}
1197
1198module_init(smsdvb_module_init);
1199module_exit(smsdvb_module_exit);
1200
1201MODULE_DESCRIPTION("SMS DVB subsystem adaptation module");
1202MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
1203MODULE_LICENSE("GPL");
1204