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