1 /*
2 * dvb_frontend.c: DVB frontend tuning interface/thread
3 *
4 *
5 * Copyright (C) 1999-2001 Ralph Metzler
6 * Marcus Metzler
7 * Holger Waechtler
8 * for convergence integrated media GmbH
9 *
10 * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup)
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26 */
27
28 /* Enables DVBv3 compatibility bits at the headers */
29 #define __DVB_CORE__
30
31 #include <linux/string.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/semaphore.h>
38 #include <linux/module.h>
39 #include <linux/list.h>
40 #include <linux/freezer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kthread.h>
43 #include <linux/ktime.h>
44 #include <asm/processor.h>
45
46 #include "dvb_frontend.h"
47 #include "dvbdev.h"
48 #include <linux/dvb/version.h>
49
50 static int dvb_frontend_debug;
51 static int dvb_shutdown_timeout;
52 static int dvb_force_auto_inversion;
53 static int dvb_override_tune_delay;
54 static int dvb_powerdown_on_sleep = 1;
55 static int dvb_mfe_wait_time = 5;
56
57 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
58 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
59 module_param(dvb_shutdown_timeout, int, 0644);
60 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
61 module_param(dvb_force_auto_inversion, int, 0644);
62 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
63 module_param(dvb_override_tune_delay, int, 0644);
64 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
65 module_param(dvb_powerdown_on_sleep, int, 0644);
66 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
67 module_param(dvb_mfe_wait_time, int, 0644);
68 MODULE_PARM_DESC(dvb_mfe_wait_time, "Wait up to <mfe_wait_time> seconds on open() for multi-frontend to become available (default:5 seconds)");
69
70 #define FESTATE_IDLE 1
71 #define FESTATE_RETUNE 2
72 #define FESTATE_TUNING_FAST 4
73 #define FESTATE_TUNING_SLOW 8
74 #define FESTATE_TUNED 16
75 #define FESTATE_ZIGZAG_FAST 32
76 #define FESTATE_ZIGZAG_SLOW 64
77 #define FESTATE_DISEQC 128
78 #define FESTATE_ERROR 256
79 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
80 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
81 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
82 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
83
84 /*
85 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
86 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
87 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
88 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
89 * FESTATE_TUNED. The frontend has successfully locked on.
90 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
91 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
92 * FESTATE_DISEQC. A DISEQC command has just been issued.
93 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
94 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
95 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
96 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
97 */
98
99 static DEFINE_MUTEX(frontend_mutex);
100
101 struct dvb_frontend_private {
102
103 /* thread/frontend values */
104 struct dvb_device *dvbdev;
105 struct dvb_frontend_parameters parameters_out;
106 struct dvb_fe_events events;
107 struct semaphore sem;
108 struct list_head list_head;
109 wait_queue_head_t wait_queue;
110 struct task_struct *thread;
111 unsigned long release_jiffies;
112 unsigned int wakeup;
113 enum fe_status status;
114 unsigned long tune_mode_flags;
115 unsigned int delay;
116 unsigned int reinitialise;
117 int tone;
118 int voltage;
119
120 /* swzigzag values */
121 unsigned int state;
122 unsigned int bending;
123 int lnb_drift;
124 unsigned int inversion;
125 unsigned int auto_step;
126 unsigned int auto_sub_step;
127 unsigned int started_auto_step;
128 unsigned int min_delay;
129 unsigned int max_drift;
130 unsigned int step_size;
131 int quality;
132 unsigned int check_wrapped;
133 enum dvbfe_search algo_status;
134
135 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
136 struct media_pipeline pipe;
137 struct media_entity *pipe_start_entity;
138 #endif
139 };
140
141 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
142 static int dtv_get_frontend(struct dvb_frontend *fe,
143 struct dvb_frontend_parameters *p_out);
144 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
145 struct dvb_frontend_parameters *p);
146
has_get_frontend(struct dvb_frontend * fe)147 static bool has_get_frontend(struct dvb_frontend *fe)
148 {
149 return fe->ops.get_frontend != NULL;
150 }
151
152 /*
153 * Due to DVBv3 API calls, a delivery system should be mapped into one of
154 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
155 * otherwise, a DVBv3 call will fail.
156 */
157 enum dvbv3_emulation_type {
158 DVBV3_UNKNOWN,
159 DVBV3_QPSK,
160 DVBV3_QAM,
161 DVBV3_OFDM,
162 DVBV3_ATSC,
163 };
164
dvbv3_type(u32 delivery_system)165 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
166 {
167 switch (delivery_system) {
168 case SYS_DVBC_ANNEX_A:
169 case SYS_DVBC_ANNEX_C:
170 return DVBV3_QAM;
171 case SYS_DVBS:
172 case SYS_DVBS2:
173 case SYS_TURBO:
174 case SYS_ISDBS:
175 case SYS_DSS:
176 return DVBV3_QPSK;
177 case SYS_DVBT:
178 case SYS_DVBT2:
179 case SYS_ISDBT:
180 case SYS_DTMB:
181 return DVBV3_OFDM;
182 case SYS_ATSC:
183 case SYS_ATSCMH:
184 case SYS_DVBC_ANNEX_B:
185 return DVBV3_ATSC;
186 case SYS_UNDEFINED:
187 case SYS_ISDBC:
188 case SYS_DVBH:
189 case SYS_DAB:
190 default:
191 /*
192 * Doesn't know how to emulate those types and/or
193 * there's no frontend driver from this type yet
194 * with some emulation code, so, we're not sure yet how
195 * to handle them, or they're not compatible with a DVBv3 call.
196 */
197 return DVBV3_UNKNOWN;
198 }
199 }
200
dvb_frontend_add_event(struct dvb_frontend * fe,enum fe_status status)201 static void dvb_frontend_add_event(struct dvb_frontend *fe,
202 enum fe_status status)
203 {
204 struct dvb_frontend_private *fepriv = fe->frontend_priv;
205 struct dvb_fe_events *events = &fepriv->events;
206 struct dvb_frontend_event *e;
207 int wp;
208
209 dev_dbg(fe->dvb->device, "%s:\n", __func__);
210
211 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
212 dtv_get_frontend(fe, &fepriv->parameters_out);
213
214 mutex_lock(&events->mtx);
215
216 wp = (events->eventw + 1) % MAX_EVENT;
217 if (wp == events->eventr) {
218 events->overflow = 1;
219 events->eventr = (events->eventr + 1) % MAX_EVENT;
220 }
221
222 e = &events->events[events->eventw];
223 e->status = status;
224 e->parameters = fepriv->parameters_out;
225
226 events->eventw = wp;
227
228 mutex_unlock(&events->mtx);
229
230 wake_up_interruptible (&events->wait_queue);
231 }
232
dvb_frontend_test_event(struct dvb_frontend_private * fepriv,struct dvb_fe_events * events)233 static int dvb_frontend_test_event(struct dvb_frontend_private *fepriv,
234 struct dvb_fe_events *events)
235 {
236 int ret;
237
238 up(&fepriv->sem);
239 ret = events->eventw != events->eventr;
240 down(&fepriv->sem);
241
242 return ret;
243 }
244
dvb_frontend_get_event(struct dvb_frontend * fe,struct dvb_frontend_event * event,int flags)245 static int dvb_frontend_get_event(struct dvb_frontend *fe,
246 struct dvb_frontend_event *event, int flags)
247 {
248 struct dvb_frontend_private *fepriv = fe->frontend_priv;
249 struct dvb_fe_events *events = &fepriv->events;
250
251 dev_dbg(fe->dvb->device, "%s:\n", __func__);
252
253 if (events->overflow) {
254 events->overflow = 0;
255 return -EOVERFLOW;
256 }
257
258 if (events->eventw == events->eventr) {
259 int ret;
260
261 if (flags & O_NONBLOCK)
262 return -EWOULDBLOCK;
263
264 ret = wait_event_interruptible(events->wait_queue,
265 dvb_frontend_test_event(fepriv, events));
266
267 if (ret < 0)
268 return ret;
269 }
270
271 mutex_lock(&events->mtx);
272 *event = events->events[events->eventr];
273 events->eventr = (events->eventr + 1) % MAX_EVENT;
274 mutex_unlock(&events->mtx);
275
276 return 0;
277 }
278
dvb_frontend_clear_events(struct dvb_frontend * fe)279 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
280 {
281 struct dvb_frontend_private *fepriv = fe->frontend_priv;
282 struct dvb_fe_events *events = &fepriv->events;
283
284 mutex_lock(&events->mtx);
285 events->eventr = events->eventw;
286 mutex_unlock(&events->mtx);
287 }
288
dvb_frontend_init(struct dvb_frontend * fe)289 static void dvb_frontend_init(struct dvb_frontend *fe)
290 {
291 dev_dbg(fe->dvb->device,
292 "%s: initialising adapter %i frontend %i (%s)...\n",
293 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
294
295 if (fe->ops.init)
296 fe->ops.init(fe);
297 if (fe->ops.tuner_ops.init) {
298 if (fe->ops.i2c_gate_ctrl)
299 fe->ops.i2c_gate_ctrl(fe, 1);
300 fe->ops.tuner_ops.init(fe);
301 if (fe->ops.i2c_gate_ctrl)
302 fe->ops.i2c_gate_ctrl(fe, 0);
303 }
304 }
305
dvb_frontend_reinitialise(struct dvb_frontend * fe)306 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
307 {
308 struct dvb_frontend_private *fepriv = fe->frontend_priv;
309
310 fepriv->reinitialise = 1;
311 dvb_frontend_wakeup(fe);
312 }
313 EXPORT_SYMBOL(dvb_frontend_reinitialise);
314
dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private * fepriv,int locked)315 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
316 {
317 int q2;
318 struct dvb_frontend *fe = fepriv->dvbdev->priv;
319
320 dev_dbg(fe->dvb->device, "%s:\n", __func__);
321
322 if (locked)
323 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
324 else
325 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
326
327 q2 = fepriv->quality - 128;
328 q2 *= q2;
329
330 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
331 }
332
333 /**
334 * Performs automatic twiddling of frontend parameters.
335 *
336 * @param fe The frontend concerned.
337 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
338 * @returns Number of complete iterations that have been performed.
339 */
dvb_frontend_swzigzag_autotune(struct dvb_frontend * fe,int check_wrapped)340 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
341 {
342 int autoinversion;
343 int ready = 0;
344 int fe_set_err = 0;
345 struct dvb_frontend_private *fepriv = fe->frontend_priv;
346 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
347 int original_inversion = c->inversion;
348 u32 original_frequency = c->frequency;
349
350 /* are we using autoinversion? */
351 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
352 (c->inversion == INVERSION_AUTO));
353
354 /* setup parameters correctly */
355 while(!ready) {
356 /* calculate the lnb_drift */
357 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
358
359 /* wrap the auto_step if we've exceeded the maximum drift */
360 if (fepriv->lnb_drift > fepriv->max_drift) {
361 fepriv->auto_step = 0;
362 fepriv->auto_sub_step = 0;
363 fepriv->lnb_drift = 0;
364 }
365
366 /* perform inversion and +/- zigzag */
367 switch(fepriv->auto_sub_step) {
368 case 0:
369 /* try with the current inversion and current drift setting */
370 ready = 1;
371 break;
372
373 case 1:
374 if (!autoinversion) break;
375
376 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
377 ready = 1;
378 break;
379
380 case 2:
381 if (fepriv->lnb_drift == 0) break;
382
383 fepriv->lnb_drift = -fepriv->lnb_drift;
384 ready = 1;
385 break;
386
387 case 3:
388 if (fepriv->lnb_drift == 0) break;
389 if (!autoinversion) break;
390
391 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
392 fepriv->lnb_drift = -fepriv->lnb_drift;
393 ready = 1;
394 break;
395
396 default:
397 fepriv->auto_step++;
398 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
399 break;
400 }
401
402 if (!ready) fepriv->auto_sub_step++;
403 }
404
405 /* if this attempt would hit where we started, indicate a complete
406 * iteration has occurred */
407 if ((fepriv->auto_step == fepriv->started_auto_step) &&
408 (fepriv->auto_sub_step == 0) && check_wrapped) {
409 return 1;
410 }
411
412 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
413 "auto_sub_step:%i started_auto_step:%i\n",
414 __func__, fepriv->lnb_drift, fepriv->inversion,
415 fepriv->auto_step, fepriv->auto_sub_step,
416 fepriv->started_auto_step);
417
418 /* set the frontend itself */
419 c->frequency += fepriv->lnb_drift;
420 if (autoinversion)
421 c->inversion = fepriv->inversion;
422 tmp = *c;
423 if (fe->ops.set_frontend)
424 fe_set_err = fe->ops.set_frontend(fe);
425 *c = tmp;
426 if (fe_set_err < 0) {
427 fepriv->state = FESTATE_ERROR;
428 return fe_set_err;
429 }
430
431 c->frequency = original_frequency;
432 c->inversion = original_inversion;
433
434 fepriv->auto_sub_step++;
435 return 0;
436 }
437
dvb_frontend_swzigzag(struct dvb_frontend * fe)438 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
439 {
440 enum fe_status s = 0;
441 int retval = 0;
442 struct dvb_frontend_private *fepriv = fe->frontend_priv;
443 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
444
445 /* if we've got no parameters, just keep idling */
446 if (fepriv->state & FESTATE_IDLE) {
447 fepriv->delay = 3*HZ;
448 fepriv->quality = 0;
449 return;
450 }
451
452 /* in SCAN mode, we just set the frontend when asked and leave it alone */
453 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
454 if (fepriv->state & FESTATE_RETUNE) {
455 tmp = *c;
456 if (fe->ops.set_frontend)
457 retval = fe->ops.set_frontend(fe);
458 *c = tmp;
459 if (retval < 0)
460 fepriv->state = FESTATE_ERROR;
461 else
462 fepriv->state = FESTATE_TUNED;
463 }
464 fepriv->delay = 3*HZ;
465 fepriv->quality = 0;
466 return;
467 }
468
469 /* get the frontend status */
470 if (fepriv->state & FESTATE_RETUNE) {
471 s = 0;
472 } else {
473 if (fe->ops.read_status)
474 fe->ops.read_status(fe, &s);
475 if (s != fepriv->status) {
476 dvb_frontend_add_event(fe, s);
477 fepriv->status = s;
478 }
479 }
480
481 /* if we're not tuned, and we have a lock, move to the TUNED state */
482 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
483 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
484 fepriv->state = FESTATE_TUNED;
485
486 /* if we're tuned, then we have determined the correct inversion */
487 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
488 (c->inversion == INVERSION_AUTO)) {
489 c->inversion = fepriv->inversion;
490 }
491 return;
492 }
493
494 /* if we are tuned already, check we're still locked */
495 if (fepriv->state & FESTATE_TUNED) {
496 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
497
498 /* we're tuned, and the lock is still good... */
499 if (s & FE_HAS_LOCK) {
500 return;
501 } else { /* if we _WERE_ tuned, but now don't have a lock */
502 fepriv->state = FESTATE_ZIGZAG_FAST;
503 fepriv->started_auto_step = fepriv->auto_step;
504 fepriv->check_wrapped = 0;
505 }
506 }
507
508 /* don't actually do anything if we're in the LOSTLOCK state,
509 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
510 if ((fepriv->state & FESTATE_LOSTLOCK) &&
511 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
512 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
513 return;
514 }
515
516 /* don't do anything if we're in the DISEQC state, since this
517 * might be someone with a motorized dish controlled by DISEQC.
518 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
519 if (fepriv->state & FESTATE_DISEQC) {
520 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
521 return;
522 }
523
524 /* if we're in the RETUNE state, set everything up for a brand
525 * new scan, keeping the current inversion setting, as the next
526 * tune is _very_ likely to require the same */
527 if (fepriv->state & FESTATE_RETUNE) {
528 fepriv->lnb_drift = 0;
529 fepriv->auto_step = 0;
530 fepriv->auto_sub_step = 0;
531 fepriv->started_auto_step = 0;
532 fepriv->check_wrapped = 0;
533 }
534
535 /* fast zigzag. */
536 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
537 fepriv->delay = fepriv->min_delay;
538
539 /* perform a tune */
540 retval = dvb_frontend_swzigzag_autotune(fe,
541 fepriv->check_wrapped);
542 if (retval < 0) {
543 return;
544 } else if (retval) {
545 /* OK, if we've run out of trials at the fast speed.
546 * Drop back to slow for the _next_ attempt */
547 fepriv->state = FESTATE_SEARCHING_SLOW;
548 fepriv->started_auto_step = fepriv->auto_step;
549 return;
550 }
551 fepriv->check_wrapped = 1;
552
553 /* if we've just retuned, enter the ZIGZAG_FAST state.
554 * This ensures we cannot return from an
555 * FE_SET_FRONTEND ioctl before the first frontend tune
556 * occurs */
557 if (fepriv->state & FESTATE_RETUNE) {
558 fepriv->state = FESTATE_TUNING_FAST;
559 }
560 }
561
562 /* slow zigzag */
563 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
564 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
565
566 /* Note: don't bother checking for wrapping; we stay in this
567 * state until we get a lock */
568 dvb_frontend_swzigzag_autotune(fe, 0);
569 }
570 }
571
dvb_frontend_is_exiting(struct dvb_frontend * fe)572 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
573 {
574 struct dvb_frontend_private *fepriv = fe->frontend_priv;
575
576 if (fe->exit != DVB_FE_NO_EXIT)
577 return 1;
578
579 if (fepriv->dvbdev->writers == 1)
580 if (time_after_eq(jiffies, fepriv->release_jiffies +
581 dvb_shutdown_timeout * HZ))
582 return 1;
583
584 return 0;
585 }
586
dvb_frontend_should_wakeup(struct dvb_frontend * fe)587 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
588 {
589 struct dvb_frontend_private *fepriv = fe->frontend_priv;
590
591 if (fepriv->wakeup) {
592 fepriv->wakeup = 0;
593 return 1;
594 }
595 return dvb_frontend_is_exiting(fe);
596 }
597
dvb_frontend_wakeup(struct dvb_frontend * fe)598 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
599 {
600 struct dvb_frontend_private *fepriv = fe->frontend_priv;
601
602 fepriv->wakeup = 1;
603 wake_up_interruptible(&fepriv->wait_queue);
604 }
605
606 /**
607 * dvb_enable_media_tuner() - tries to enable the DVB tuner
608 *
609 * @fe: struct dvb_frontend pointer
610 *
611 * This function ensures that just one media tuner is enabled for a given
612 * frontend. It has two different behaviors:
613 * - For trivial devices with just one tuner:
614 * it just enables the existing tuner->fe link
615 * - For devices with more than one tuner:
616 * It is up to the driver to implement the logic that will enable one tuner
617 * and disable the other ones. However, if more than one tuner is enabled for
618 * the same frontend, it will print an error message and return -EINVAL.
619 *
620 * At return, it will return the error code returned by media_entity_setup_link,
621 * or 0 if everything is OK, if no tuner is linked to the frontend or if the
622 * mdev is NULL.
623 */
624 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
dvb_enable_media_tuner(struct dvb_frontend * fe)625 static int dvb_enable_media_tuner(struct dvb_frontend *fe)
626 {
627 struct dvb_frontend_private *fepriv = fe->frontend_priv;
628 struct dvb_adapter *adapter = fe->dvb;
629 struct media_device *mdev = adapter->mdev;
630 struct media_entity *entity, *source;
631 struct media_link *link, *found_link = NULL;
632 int i, ret, n_links = 0, active_links = 0;
633
634 fepriv->pipe_start_entity = NULL;
635
636 if (!mdev)
637 return 0;
638
639 entity = fepriv->dvbdev->entity;
640 fepriv->pipe_start_entity = entity;
641
642 for (i = 0; i < entity->num_links; i++) {
643 link = &entity->links[i];
644 if (link->sink->entity == entity) {
645 found_link = link;
646 n_links++;
647 if (link->flags & MEDIA_LNK_FL_ENABLED)
648 active_links++;
649 }
650 }
651
652 if (!n_links || active_links == 1 || !found_link)
653 return 0;
654
655 /*
656 * If a frontend has more than one tuner linked, it is up to the driver
657 * to select with one will be the active one, as the frontend core can't
658 * guess. If the driver doesn't do that, it is a bug.
659 */
660 if (n_links > 1 && active_links != 1) {
661 dev_err(fe->dvb->device,
662 "WARNING: there are %d active links among %d tuners. This is a driver's bug!\n",
663 active_links, n_links);
664 return -EINVAL;
665 }
666
667 source = found_link->source->entity;
668 fepriv->pipe_start_entity = source;
669 for (i = 0; i < source->num_links; i++) {
670 struct media_entity *sink;
671 int flags = 0;
672
673 link = &source->links[i];
674 sink = link->sink->entity;
675
676 if (sink == entity)
677 flags = MEDIA_LNK_FL_ENABLED;
678
679 ret = media_entity_setup_link(link, flags);
680 if (ret) {
681 dev_err(fe->dvb->device,
682 "Couldn't change link %s->%s to %s. Error %d\n",
683 source->name, sink->name,
684 flags ? "enabled" : "disabled",
685 ret);
686 return ret;
687 } else
688 dev_dbg(fe->dvb->device,
689 "link %s->%s was %s\n",
690 source->name, sink->name,
691 flags ? "ENABLED" : "disabled");
692 }
693 return 0;
694 }
695 #endif
696
dvb_frontend_thread(void * data)697 static int dvb_frontend_thread(void *data)
698 {
699 struct dvb_frontend *fe = data;
700 struct dvb_frontend_private *fepriv = fe->frontend_priv;
701 enum fe_status s;
702 enum dvbfe_algo algo;
703 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
704 int ret;
705 #endif
706
707 bool re_tune = false;
708 bool semheld = false;
709
710 dev_dbg(fe->dvb->device, "%s:\n", __func__);
711
712 fepriv->check_wrapped = 0;
713 fepriv->quality = 0;
714 fepriv->delay = 3*HZ;
715 fepriv->status = 0;
716 fepriv->wakeup = 0;
717 fepriv->reinitialise = 0;
718
719 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
720 ret = dvb_enable_media_tuner(fe);
721 if (ret) {
722 /* FIXME: return an error if it fails */
723 dev_info(fe->dvb->device,
724 "proceeding with FE task\n");
725 } else if (fepriv->pipe_start_entity) {
726 ret = media_entity_pipeline_start(fepriv->pipe_start_entity,
727 &fepriv->pipe);
728 if (ret)
729 return ret;
730 }
731 #endif
732
733 dvb_frontend_init(fe);
734
735 set_freezable();
736 while (1) {
737 up(&fepriv->sem); /* is locked when we enter the thread... */
738 restart:
739 wait_event_interruptible_timeout(fepriv->wait_queue,
740 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
741 || freezing(current),
742 fepriv->delay);
743
744 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
745 /* got signal or quitting */
746 if (!down_interruptible(&fepriv->sem))
747 semheld = true;
748 fe->exit = DVB_FE_NORMAL_EXIT;
749 break;
750 }
751
752 if (try_to_freeze())
753 goto restart;
754
755 if (down_interruptible(&fepriv->sem))
756 break;
757
758 if (fepriv->reinitialise) {
759 dvb_frontend_init(fe);
760 if (fe->ops.set_tone && fepriv->tone != -1)
761 fe->ops.set_tone(fe, fepriv->tone);
762 if (fe->ops.set_voltage && fepriv->voltage != -1)
763 fe->ops.set_voltage(fe, fepriv->voltage);
764 fepriv->reinitialise = 0;
765 }
766
767 /* do an iteration of the tuning loop */
768 if (fe->ops.get_frontend_algo) {
769 algo = fe->ops.get_frontend_algo(fe);
770 switch (algo) {
771 case DVBFE_ALGO_HW:
772 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
773
774 if (fepriv->state & FESTATE_RETUNE) {
775 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
776 re_tune = true;
777 fepriv->state = FESTATE_TUNED;
778 } else {
779 re_tune = false;
780 }
781
782 if (fe->ops.tune)
783 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
784
785 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
786 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
787 dvb_frontend_add_event(fe, s);
788 fepriv->status = s;
789 }
790 break;
791 case DVBFE_ALGO_SW:
792 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
793 dvb_frontend_swzigzag(fe);
794 break;
795 case DVBFE_ALGO_CUSTOM:
796 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
797 if (fepriv->state & FESTATE_RETUNE) {
798 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
799 fepriv->state = FESTATE_TUNED;
800 }
801 /* Case where we are going to search for a carrier
802 * User asked us to retune again for some reason, possibly
803 * requesting a search with a new set of parameters
804 */
805 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
806 if (fe->ops.search) {
807 fepriv->algo_status = fe->ops.search(fe);
808 /* We did do a search as was requested, the flags are
809 * now unset as well and has the flags wrt to search.
810 */
811 } else {
812 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
813 }
814 }
815 /* Track the carrier if the search was successful */
816 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
817 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
818 fepriv->delay = HZ / 2;
819 }
820 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
821 fe->ops.read_status(fe, &s);
822 if (s != fepriv->status) {
823 dvb_frontend_add_event(fe, s); /* update event list */
824 fepriv->status = s;
825 if (!(s & FE_HAS_LOCK)) {
826 fepriv->delay = HZ / 10;
827 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
828 } else {
829 fepriv->delay = 60 * HZ;
830 }
831 }
832 break;
833 default:
834 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
835 break;
836 }
837 } else {
838 dvb_frontend_swzigzag(fe);
839 }
840 }
841
842 #ifdef CONFIG_MEDIA_CONTROLLER_DVB
843 if (fepriv->pipe_start_entity)
844 media_entity_pipeline_stop(fepriv->pipe_start_entity);
845 fepriv->pipe_start_entity = NULL;
846 #endif
847
848 if (dvb_powerdown_on_sleep) {
849 if (fe->ops.set_voltage)
850 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
851 if (fe->ops.tuner_ops.sleep) {
852 if (fe->ops.i2c_gate_ctrl)
853 fe->ops.i2c_gate_ctrl(fe, 1);
854 fe->ops.tuner_ops.sleep(fe);
855 if (fe->ops.i2c_gate_ctrl)
856 fe->ops.i2c_gate_ctrl(fe, 0);
857 }
858 if (fe->ops.sleep)
859 fe->ops.sleep(fe);
860 }
861
862 fepriv->thread = NULL;
863 if (kthread_should_stop())
864 fe->exit = DVB_FE_DEVICE_REMOVED;
865 else
866 fe->exit = DVB_FE_NO_EXIT;
867 mb();
868
869 if (semheld)
870 up(&fepriv->sem);
871 dvb_frontend_wakeup(fe);
872 return 0;
873 }
874
dvb_frontend_stop(struct dvb_frontend * fe)875 static void dvb_frontend_stop(struct dvb_frontend *fe)
876 {
877 struct dvb_frontend_private *fepriv = fe->frontend_priv;
878
879 dev_dbg(fe->dvb->device, "%s:\n", __func__);
880
881 if (fe->exit != DVB_FE_DEVICE_REMOVED)
882 fe->exit = DVB_FE_NORMAL_EXIT;
883 mb();
884
885 if (!fepriv->thread)
886 return;
887
888 kthread_stop(fepriv->thread);
889
890 sema_init(&fepriv->sem, 1);
891 fepriv->state = FESTATE_IDLE;
892
893 /* paranoia check in case a signal arrived */
894 if (fepriv->thread)
895 dev_warn(fe->dvb->device,
896 "dvb_frontend_stop: warning: thread %p won't exit\n",
897 fepriv->thread);
898 }
899
900 /*
901 * Sleep until gettimeofday() > waketime + add_usec
902 * This needs to be as precise as possible, but as the delay is
903 * usually between 2ms and 32ms, it is done using a scheduled msleep
904 * followed by usleep (normally a busy-wait loop) for the remainder
905 */
dvb_frontend_sleep_until(ktime_t * waketime,u32 add_usec)906 void dvb_frontend_sleep_until(ktime_t *waketime, u32 add_usec)
907 {
908 s32 delta, newdelta;
909
910 ktime_add_us(*waketime, add_usec);
911 delta = ktime_us_delta(ktime_get_real(), *waketime);
912 if (delta > 2500) {
913 msleep((delta - 1500) / 1000);
914 newdelta = ktime_us_delta(ktime_get_real(), *waketime);
915 delta = (newdelta > delta) ? 0 : newdelta;
916 }
917 if (delta > 0)
918 udelay(delta);
919 }
920 EXPORT_SYMBOL(dvb_frontend_sleep_until);
921
dvb_frontend_start(struct dvb_frontend * fe)922 static int dvb_frontend_start(struct dvb_frontend *fe)
923 {
924 int ret;
925 struct dvb_frontend_private *fepriv = fe->frontend_priv;
926 struct task_struct *fe_thread;
927
928 dev_dbg(fe->dvb->device, "%s:\n", __func__);
929
930 if (fepriv->thread) {
931 if (fe->exit == DVB_FE_NO_EXIT)
932 return 0;
933 else
934 dvb_frontend_stop (fe);
935 }
936
937 if (signal_pending(current))
938 return -EINTR;
939 if (down_interruptible (&fepriv->sem))
940 return -EINTR;
941
942 fepriv->state = FESTATE_IDLE;
943 fe->exit = DVB_FE_NO_EXIT;
944 fepriv->thread = NULL;
945 mb();
946
947 fe_thread = kthread_run(dvb_frontend_thread, fe,
948 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
949 if (IS_ERR(fe_thread)) {
950 ret = PTR_ERR(fe_thread);
951 dev_warn(fe->dvb->device,
952 "dvb_frontend_start: failed to start kthread (%d)\n",
953 ret);
954 up(&fepriv->sem);
955 return ret;
956 }
957 fepriv->thread = fe_thread;
958 return 0;
959 }
960
dvb_frontend_get_frequency_limits(struct dvb_frontend * fe,u32 * freq_min,u32 * freq_max)961 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
962 u32 *freq_min, u32 *freq_max)
963 {
964 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
965
966 if (fe->ops.info.frequency_max == 0)
967 *freq_max = fe->ops.tuner_ops.info.frequency_max;
968 else if (fe->ops.tuner_ops.info.frequency_max == 0)
969 *freq_max = fe->ops.info.frequency_max;
970 else
971 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
972
973 if (*freq_min == 0 || *freq_max == 0)
974 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
975 fe->dvb->num, fe->id);
976 }
977
dvb_frontend_check_parameters(struct dvb_frontend * fe)978 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
979 {
980 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
981 u32 freq_min;
982 u32 freq_max;
983
984 /* range check: frequency */
985 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
986 if ((freq_min && c->frequency < freq_min) ||
987 (freq_max && c->frequency > freq_max)) {
988 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
989 fe->dvb->num, fe->id, c->frequency,
990 freq_min, freq_max);
991 return -EINVAL;
992 }
993
994 /* range check: symbol rate */
995 switch (c->delivery_system) {
996 case SYS_DVBS:
997 case SYS_DVBS2:
998 case SYS_TURBO:
999 case SYS_DVBC_ANNEX_A:
1000 case SYS_DVBC_ANNEX_C:
1001 if ((fe->ops.info.symbol_rate_min &&
1002 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
1003 (fe->ops.info.symbol_rate_max &&
1004 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
1005 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
1006 fe->dvb->num, fe->id, c->symbol_rate,
1007 fe->ops.info.symbol_rate_min,
1008 fe->ops.info.symbol_rate_max);
1009 return -EINVAL;
1010 }
1011 default:
1012 break;
1013 }
1014
1015 return 0;
1016 }
1017
dvb_frontend_clear_cache(struct dvb_frontend * fe)1018 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
1019 {
1020 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1021 int i;
1022 u32 delsys;
1023
1024 delsys = c->delivery_system;
1025 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
1026 c->delivery_system = delsys;
1027
1028 c->state = DTV_CLEAR;
1029
1030 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
1031 __func__, c->delivery_system);
1032
1033 c->transmission_mode = TRANSMISSION_MODE_AUTO;
1034 c->bandwidth_hz = 0; /* AUTO */
1035 c->guard_interval = GUARD_INTERVAL_AUTO;
1036 c->hierarchy = HIERARCHY_AUTO;
1037 c->symbol_rate = 0;
1038 c->code_rate_HP = FEC_AUTO;
1039 c->code_rate_LP = FEC_AUTO;
1040 c->fec_inner = FEC_AUTO;
1041 c->rolloff = ROLLOFF_AUTO;
1042 c->voltage = SEC_VOLTAGE_OFF;
1043 c->sectone = SEC_TONE_OFF;
1044 c->pilot = PILOT_AUTO;
1045
1046 c->isdbt_partial_reception = 0;
1047 c->isdbt_sb_mode = 0;
1048 c->isdbt_sb_subchannel = 0;
1049 c->isdbt_sb_segment_idx = 0;
1050 c->isdbt_sb_segment_count = 0;
1051 c->isdbt_layer_enabled = 0;
1052 for (i = 0; i < 3; i++) {
1053 c->layer[i].fec = FEC_AUTO;
1054 c->layer[i].modulation = QAM_AUTO;
1055 c->layer[i].interleaving = 0;
1056 c->layer[i].segment_count = 0;
1057 }
1058
1059 c->stream_id = NO_STREAM_ID_FILTER;
1060
1061 switch (c->delivery_system) {
1062 case SYS_DVBS:
1063 case SYS_DVBS2:
1064 case SYS_TURBO:
1065 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1066 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1067 break;
1068 case SYS_ATSC:
1069 c->modulation = VSB_8;
1070 break;
1071 case SYS_ISDBS:
1072 c->symbol_rate = 28860000;
1073 c->rolloff = ROLLOFF_35;
1074 c->bandwidth_hz = c->symbol_rate / 100 * 135;
1075 break;
1076 default:
1077 c->modulation = QAM_AUTO;
1078 break;
1079 }
1080
1081 c->lna = LNA_AUTO;
1082
1083 return 0;
1084 }
1085
1086 #define _DTV_CMD(n, s, b) \
1087 [n] = { \
1088 .name = #n, \
1089 .cmd = n, \
1090 .set = s,\
1091 .buffer = b \
1092 }
1093
1094 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
1095 _DTV_CMD(DTV_TUNE, 1, 0),
1096 _DTV_CMD(DTV_CLEAR, 1, 0),
1097
1098 /* Set */
1099 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1100 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1101 _DTV_CMD(DTV_MODULATION, 1, 0),
1102 _DTV_CMD(DTV_INVERSION, 1, 0),
1103 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1104 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1105 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1106 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1107 _DTV_CMD(DTV_TONE, 1, 0),
1108 _DTV_CMD(DTV_PILOT, 1, 0),
1109 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1110 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1111 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1112 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1113 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1114 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1115 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1116 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
1117
1118 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1119 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1120 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1121 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1122 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1123 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1124 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1125 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1126 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1127 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1128 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1129 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1130 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1131 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1132 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1133 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1134 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1135 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1136
1137 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1138 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
1139 _DTV_CMD(DTV_LNA, 1, 0),
1140
1141 /* Get */
1142 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1143 _DTV_CMD(DTV_API_VERSION, 0, 0),
1144
1145 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1146
1147 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1148 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1149
1150 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
1151 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1152 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1153 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1154 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1155 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
1156 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1157 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1158 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1159 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1160 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1161 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1162 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
1163
1164 /* Statistics API */
1165 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1166 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1167 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1168 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1169 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1170 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1171 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1172 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
1173 };
1174
dtv_property_dump(struct dvb_frontend * fe,struct dtv_property * tvp)1175 static void dtv_property_dump(struct dvb_frontend *fe, struct dtv_property *tvp)
1176 {
1177 int i;
1178
1179 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1180 dev_warn(fe->dvb->device, "%s: tvp.cmd = 0x%08x undefined\n",
1181 __func__, tvp->cmd);
1182 return;
1183 }
1184
1185 dev_dbg(fe->dvb->device, "%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1186 tvp->cmd, dtv_cmds[tvp->cmd].name);
1187
1188 if (dtv_cmds[tvp->cmd].buffer) {
1189 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1190 __func__, tvp->u.buffer.len);
1191
1192 for(i = 0; i < tvp->u.buffer.len; i++)
1193 dev_dbg(fe->dvb->device,
1194 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1195 __func__, i, tvp->u.buffer.data[i]);
1196 } else {
1197 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1198 tvp->u.data);
1199 }
1200 }
1201
1202 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1203 * drivers can use a single set_frontend tuning function, regardless of whether
1204 * it's being used for the legacy or new API, reducing code and complexity.
1205 */
dtv_property_cache_sync(struct dvb_frontend * fe,struct dtv_frontend_properties * c,const struct dvb_frontend_parameters * p)1206 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1207 struct dtv_frontend_properties *c,
1208 const struct dvb_frontend_parameters *p)
1209 {
1210 c->frequency = p->frequency;
1211 c->inversion = p->inversion;
1212
1213 switch (dvbv3_type(c->delivery_system)) {
1214 case DVBV3_QPSK:
1215 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1216 c->symbol_rate = p->u.qpsk.symbol_rate;
1217 c->fec_inner = p->u.qpsk.fec_inner;
1218 break;
1219 case DVBV3_QAM:
1220 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1221 c->symbol_rate = p->u.qam.symbol_rate;
1222 c->fec_inner = p->u.qam.fec_inner;
1223 c->modulation = p->u.qam.modulation;
1224 break;
1225 case DVBV3_OFDM:
1226 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1227
1228 switch (p->u.ofdm.bandwidth) {
1229 case BANDWIDTH_10_MHZ:
1230 c->bandwidth_hz = 10000000;
1231 break;
1232 case BANDWIDTH_8_MHZ:
1233 c->bandwidth_hz = 8000000;
1234 break;
1235 case BANDWIDTH_7_MHZ:
1236 c->bandwidth_hz = 7000000;
1237 break;
1238 case BANDWIDTH_6_MHZ:
1239 c->bandwidth_hz = 6000000;
1240 break;
1241 case BANDWIDTH_5_MHZ:
1242 c->bandwidth_hz = 5000000;
1243 break;
1244 case BANDWIDTH_1_712_MHZ:
1245 c->bandwidth_hz = 1712000;
1246 break;
1247 case BANDWIDTH_AUTO:
1248 c->bandwidth_hz = 0;
1249 }
1250
1251 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1252 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1253 c->modulation = p->u.ofdm.constellation;
1254 c->transmission_mode = p->u.ofdm.transmission_mode;
1255 c->guard_interval = p->u.ofdm.guard_interval;
1256 c->hierarchy = p->u.ofdm.hierarchy_information;
1257 break;
1258 case DVBV3_ATSC:
1259 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1260 c->modulation = p->u.vsb.modulation;
1261 if (c->delivery_system == SYS_ATSCMH)
1262 break;
1263 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1264 c->delivery_system = SYS_ATSC;
1265 else
1266 c->delivery_system = SYS_DVBC_ANNEX_B;
1267 break;
1268 case DVBV3_UNKNOWN:
1269 dev_err(fe->dvb->device,
1270 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1271 __func__, c->delivery_system);
1272 return -EINVAL;
1273 }
1274
1275 return 0;
1276 }
1277
1278 /* Ensure the cached values are set correctly in the frontend
1279 * legacy tuning structures, for the advanced tuning API.
1280 */
dtv_property_legacy_params_sync(struct dvb_frontend * fe,struct dvb_frontend_parameters * p)1281 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1282 struct dvb_frontend_parameters *p)
1283 {
1284 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1285
1286 p->frequency = c->frequency;
1287 p->inversion = c->inversion;
1288
1289 switch (dvbv3_type(c->delivery_system)) {
1290 case DVBV3_UNKNOWN:
1291 dev_err(fe->dvb->device,
1292 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1293 __func__, c->delivery_system);
1294 return -EINVAL;
1295 case DVBV3_QPSK:
1296 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1297 p->u.qpsk.symbol_rate = c->symbol_rate;
1298 p->u.qpsk.fec_inner = c->fec_inner;
1299 break;
1300 case DVBV3_QAM:
1301 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1302 p->u.qam.symbol_rate = c->symbol_rate;
1303 p->u.qam.fec_inner = c->fec_inner;
1304 p->u.qam.modulation = c->modulation;
1305 break;
1306 case DVBV3_OFDM:
1307 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1308 switch (c->bandwidth_hz) {
1309 case 10000000:
1310 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1311 break;
1312 case 8000000:
1313 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1314 break;
1315 case 7000000:
1316 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1317 break;
1318 case 6000000:
1319 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1320 break;
1321 case 5000000:
1322 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1323 break;
1324 case 1712000:
1325 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1326 break;
1327 case 0:
1328 default:
1329 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1330 }
1331 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1332 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1333 p->u.ofdm.constellation = c->modulation;
1334 p->u.ofdm.transmission_mode = c->transmission_mode;
1335 p->u.ofdm.guard_interval = c->guard_interval;
1336 p->u.ofdm.hierarchy_information = c->hierarchy;
1337 break;
1338 case DVBV3_ATSC:
1339 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1340 p->u.vsb.modulation = c->modulation;
1341 break;
1342 }
1343 return 0;
1344 }
1345
1346 /**
1347 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1348 * @fe: struct dvb_frontend pointer
1349 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1350 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1351 *
1352 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1353 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1354 * If p_out is not null, it will update the DVBv3 params pointed by it.
1355 */
dtv_get_frontend(struct dvb_frontend * fe,struct dvb_frontend_parameters * p_out)1356 static int dtv_get_frontend(struct dvb_frontend *fe,
1357 struct dvb_frontend_parameters *p_out)
1358 {
1359 int r;
1360
1361 if (fe->ops.get_frontend) {
1362 r = fe->ops.get_frontend(fe);
1363 if (unlikely(r < 0))
1364 return r;
1365 if (p_out)
1366 dtv_property_legacy_params_sync(fe, p_out);
1367 return 0;
1368 }
1369
1370 /* As everything is in cache, get_frontend fops are always supported */
1371 return 0;
1372 }
1373
1374 static int dvb_frontend_ioctl_legacy(struct file *file,
1375 unsigned int cmd, void *parg);
1376 static int dvb_frontend_ioctl_properties(struct file *file,
1377 unsigned int cmd, void *parg);
1378
dtv_property_process_get(struct dvb_frontend * fe,const struct dtv_frontend_properties * c,struct dtv_property * tvp,struct file * file)1379 static int dtv_property_process_get(struct dvb_frontend *fe,
1380 const struct dtv_frontend_properties *c,
1381 struct dtv_property *tvp,
1382 struct file *file)
1383 {
1384 int r, ncaps;
1385
1386 switch(tvp->cmd) {
1387 case DTV_ENUM_DELSYS:
1388 ncaps = 0;
1389 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1390 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1391 ncaps++;
1392 }
1393 tvp->u.buffer.len = ncaps;
1394 break;
1395 case DTV_FREQUENCY:
1396 tvp->u.data = c->frequency;
1397 break;
1398 case DTV_MODULATION:
1399 tvp->u.data = c->modulation;
1400 break;
1401 case DTV_BANDWIDTH_HZ:
1402 tvp->u.data = c->bandwidth_hz;
1403 break;
1404 case DTV_INVERSION:
1405 tvp->u.data = c->inversion;
1406 break;
1407 case DTV_SYMBOL_RATE:
1408 tvp->u.data = c->symbol_rate;
1409 break;
1410 case DTV_INNER_FEC:
1411 tvp->u.data = c->fec_inner;
1412 break;
1413 case DTV_PILOT:
1414 tvp->u.data = c->pilot;
1415 break;
1416 case DTV_ROLLOFF:
1417 tvp->u.data = c->rolloff;
1418 break;
1419 case DTV_DELIVERY_SYSTEM:
1420 tvp->u.data = c->delivery_system;
1421 break;
1422 case DTV_VOLTAGE:
1423 tvp->u.data = c->voltage;
1424 break;
1425 case DTV_TONE:
1426 tvp->u.data = c->sectone;
1427 break;
1428 case DTV_API_VERSION:
1429 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1430 break;
1431 case DTV_CODE_RATE_HP:
1432 tvp->u.data = c->code_rate_HP;
1433 break;
1434 case DTV_CODE_RATE_LP:
1435 tvp->u.data = c->code_rate_LP;
1436 break;
1437 case DTV_GUARD_INTERVAL:
1438 tvp->u.data = c->guard_interval;
1439 break;
1440 case DTV_TRANSMISSION_MODE:
1441 tvp->u.data = c->transmission_mode;
1442 break;
1443 case DTV_HIERARCHY:
1444 tvp->u.data = c->hierarchy;
1445 break;
1446 case DTV_INTERLEAVING:
1447 tvp->u.data = c->interleaving;
1448 break;
1449
1450 /* ISDB-T Support here */
1451 case DTV_ISDBT_PARTIAL_RECEPTION:
1452 tvp->u.data = c->isdbt_partial_reception;
1453 break;
1454 case DTV_ISDBT_SOUND_BROADCASTING:
1455 tvp->u.data = c->isdbt_sb_mode;
1456 break;
1457 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1458 tvp->u.data = c->isdbt_sb_subchannel;
1459 break;
1460 case DTV_ISDBT_SB_SEGMENT_IDX:
1461 tvp->u.data = c->isdbt_sb_segment_idx;
1462 break;
1463 case DTV_ISDBT_SB_SEGMENT_COUNT:
1464 tvp->u.data = c->isdbt_sb_segment_count;
1465 break;
1466 case DTV_ISDBT_LAYER_ENABLED:
1467 tvp->u.data = c->isdbt_layer_enabled;
1468 break;
1469 case DTV_ISDBT_LAYERA_FEC:
1470 tvp->u.data = c->layer[0].fec;
1471 break;
1472 case DTV_ISDBT_LAYERA_MODULATION:
1473 tvp->u.data = c->layer[0].modulation;
1474 break;
1475 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1476 tvp->u.data = c->layer[0].segment_count;
1477 break;
1478 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1479 tvp->u.data = c->layer[0].interleaving;
1480 break;
1481 case DTV_ISDBT_LAYERB_FEC:
1482 tvp->u.data = c->layer[1].fec;
1483 break;
1484 case DTV_ISDBT_LAYERB_MODULATION:
1485 tvp->u.data = c->layer[1].modulation;
1486 break;
1487 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1488 tvp->u.data = c->layer[1].segment_count;
1489 break;
1490 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1491 tvp->u.data = c->layer[1].interleaving;
1492 break;
1493 case DTV_ISDBT_LAYERC_FEC:
1494 tvp->u.data = c->layer[2].fec;
1495 break;
1496 case DTV_ISDBT_LAYERC_MODULATION:
1497 tvp->u.data = c->layer[2].modulation;
1498 break;
1499 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1500 tvp->u.data = c->layer[2].segment_count;
1501 break;
1502 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1503 tvp->u.data = c->layer[2].interleaving;
1504 break;
1505
1506 /* Multistream support */
1507 case DTV_STREAM_ID:
1508 case DTV_DVBT2_PLP_ID_LEGACY:
1509 tvp->u.data = c->stream_id;
1510 break;
1511
1512 /* ATSC-MH */
1513 case DTV_ATSCMH_FIC_VER:
1514 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1515 break;
1516 case DTV_ATSCMH_PARADE_ID:
1517 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1518 break;
1519 case DTV_ATSCMH_NOG:
1520 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1521 break;
1522 case DTV_ATSCMH_TNOG:
1523 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1524 break;
1525 case DTV_ATSCMH_SGN:
1526 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1527 break;
1528 case DTV_ATSCMH_PRC:
1529 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1530 break;
1531 case DTV_ATSCMH_RS_FRAME_MODE:
1532 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1533 break;
1534 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1535 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1536 break;
1537 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1538 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1539 break;
1540 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1541 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1542 break;
1543 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1544 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1545 break;
1546 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1547 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1548 break;
1549 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1550 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1551 break;
1552 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1553 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1554 break;
1555 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1556 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1557 break;
1558
1559 case DTV_LNA:
1560 tvp->u.data = c->lna;
1561 break;
1562
1563 /* Fill quality measures */
1564 case DTV_STAT_SIGNAL_STRENGTH:
1565 tvp->u.st = c->strength;
1566 break;
1567 case DTV_STAT_CNR:
1568 tvp->u.st = c->cnr;
1569 break;
1570 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1571 tvp->u.st = c->pre_bit_error;
1572 break;
1573 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1574 tvp->u.st = c->pre_bit_count;
1575 break;
1576 case DTV_STAT_POST_ERROR_BIT_COUNT:
1577 tvp->u.st = c->post_bit_error;
1578 break;
1579 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1580 tvp->u.st = c->post_bit_count;
1581 break;
1582 case DTV_STAT_ERROR_BLOCK_COUNT:
1583 tvp->u.st = c->block_error;
1584 break;
1585 case DTV_STAT_TOTAL_BLOCK_COUNT:
1586 tvp->u.st = c->block_count;
1587 break;
1588 default:
1589 dev_dbg(fe->dvb->device,
1590 "%s: FE property %d doesn't exist\n",
1591 __func__, tvp->cmd);
1592 return -EINVAL;
1593 }
1594
1595 /* Allow the frontend to override outgoing properties */
1596 if (fe->ops.get_property) {
1597 r = fe->ops.get_property(fe, tvp);
1598 if (r < 0)
1599 return r;
1600 }
1601
1602 dtv_property_dump(fe, tvp);
1603
1604 return 0;
1605 }
1606
1607 static int dtv_set_frontend(struct dvb_frontend *fe);
1608
is_dvbv3_delsys(u32 delsys)1609 static bool is_dvbv3_delsys(u32 delsys)
1610 {
1611 bool status;
1612
1613 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1614 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1615
1616 return status;
1617 }
1618
1619 /**
1620 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1621 * @fe: struct frontend;
1622 * @delsys: DVBv5 type that will be used for emulation
1623 *
1624 * Provides emulation for delivery systems that are compatible with the old
1625 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1626 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1627 * parameters are compatible with DVB-S spec.
1628 */
emulate_delivery_system(struct dvb_frontend * fe,u32 delsys)1629 static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
1630 {
1631 int i;
1632 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1633
1634 c->delivery_system = delsys;
1635
1636 /*
1637 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
1638 */
1639 if (c->delivery_system == SYS_ISDBT) {
1640 dev_dbg(fe->dvb->device,
1641 "%s: Using defaults for SYS_ISDBT\n",
1642 __func__);
1643
1644 if (!c->bandwidth_hz)
1645 c->bandwidth_hz = 6000000;
1646
1647 c->isdbt_partial_reception = 0;
1648 c->isdbt_sb_mode = 0;
1649 c->isdbt_sb_subchannel = 0;
1650 c->isdbt_sb_segment_idx = 0;
1651 c->isdbt_sb_segment_count = 0;
1652 c->isdbt_layer_enabled = 7;
1653 for (i = 0; i < 3; i++) {
1654 c->layer[i].fec = FEC_AUTO;
1655 c->layer[i].modulation = QAM_AUTO;
1656 c->layer[i].interleaving = 0;
1657 c->layer[i].segment_count = 0;
1658 }
1659 }
1660 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1661 __func__, c->delivery_system);
1662
1663 return 0;
1664 }
1665
1666 /**
1667 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1668 * @fe: frontend struct
1669 * @desired_system: delivery system requested by the user
1670 *
1671 * A DVBv5 call know what's the desired system it wants. So, set it.
1672 *
1673 * There are, however, a few known issues with early DVBv5 applications that
1674 * are also handled by this logic:
1675 *
1676 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1677 * This is an API violation, but, as we don't want to break userspace,
1678 * convert it to the first supported delivery system.
1679 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1680 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1681 * ISDB-T provided backward compat with DVB-T.
1682 */
dvbv5_set_delivery_system(struct dvb_frontend * fe,u32 desired_system)1683 static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1684 u32 desired_system)
1685 {
1686 int ncaps;
1687 u32 delsys = SYS_UNDEFINED;
1688 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1689 enum dvbv3_emulation_type type;
1690
1691 /*
1692 * It was reported that some old DVBv5 applications were
1693 * filling delivery_system with SYS_UNDEFINED. If this happens,
1694 * assume that the application wants to use the first supported
1695 * delivery system.
1696 */
1697 if (desired_system == SYS_UNDEFINED)
1698 desired_system = fe->ops.delsys[0];
1699
1700 /*
1701 * This is a DVBv5 call. So, it likely knows the supported
1702 * delivery systems. So, check if the desired delivery system is
1703 * supported
1704 */
1705 ncaps = 0;
1706 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1707 if (fe->ops.delsys[ncaps] == desired_system) {
1708 c->delivery_system = desired_system;
1709 dev_dbg(fe->dvb->device,
1710 "%s: Changing delivery system to %d\n",
1711 __func__, desired_system);
1712 return 0;
1713 }
1714 ncaps++;
1715 }
1716
1717 /*
1718 * The requested delivery system isn't supported. Maybe userspace
1719 * is requesting a DVBv3 compatible delivery system.
1720 *
1721 * The emulation only works if the desired system is one of the
1722 * delivery systems supported by DVBv3 API
1723 */
1724 if (!is_dvbv3_delsys(desired_system)) {
1725 dev_dbg(fe->dvb->device,
1726 "%s: Delivery system %d not supported.\n",
1727 __func__, desired_system);
1728 return -EINVAL;
1729 }
1730
1731 type = dvbv3_type(desired_system);
1732
1733 /*
1734 * Get the last non-DVBv3 delivery system that has the same type
1735 * of the desired system
1736 */
1737 ncaps = 0;
1738 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1739 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
1740 delsys = fe->ops.delsys[ncaps];
1741 ncaps++;
1742 }
1743
1744 /* There's nothing compatible with the desired delivery system */
1745 if (delsys == SYS_UNDEFINED) {
1746 dev_dbg(fe->dvb->device,
1747 "%s: Delivery system %d not supported on emulation mode.\n",
1748 __func__, desired_system);
1749 return -EINVAL;
1750 }
1751
1752 dev_dbg(fe->dvb->device,
1753 "%s: Using delivery system %d emulated as if it were %d\n",
1754 __func__, delsys, desired_system);
1755
1756 return emulate_delivery_system(fe, desired_system);
1757 }
1758
1759 /**
1760 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1761 * @fe: frontend struct
1762 *
1763 * A DVBv3 call doesn't know what's the desired system it wants. It also
1764 * doesn't allow to switch between different types. Due to that, userspace
1765 * should use DVBv5 instead.
1766 * However, in order to avoid breaking userspace API, limited backward
1767 * compatibility support is provided.
1768 *
1769 * There are some delivery systems that are incompatible with DVBv3 calls.
1770 *
1771 * This routine should work fine for frontends that support just one delivery
1772 * system.
1773 *
1774 * For frontends that support multiple frontends:
1775 * 1) It defaults to use the first supported delivery system. There's an
1776 * userspace application that allows changing it at runtime;
1777 *
1778 * 2) If the current delivery system is not compatible with DVBv3, it gets
1779 * the first one that it is compatible.
1780 *
1781 * NOTE: in order for this to work with applications like Kaffeine that
1782 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1783 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1784 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1785 * to DVB-S.
1786 */
dvbv3_set_delivery_system(struct dvb_frontend * fe)1787 static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1788 {
1789 int ncaps;
1790 u32 delsys = SYS_UNDEFINED;
1791 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1792
1793 /* If not set yet, defaults to the first supported delivery system */
1794 if (c->delivery_system == SYS_UNDEFINED)
1795 c->delivery_system = fe->ops.delsys[0];
1796
1797 /*
1798 * Trivial case: just use the current one, if it already a DVBv3
1799 * delivery system
1800 */
1801 if (is_dvbv3_delsys(c->delivery_system)) {
1802 dev_dbg(fe->dvb->device,
1803 "%s: Using delivery system to %d\n",
1804 __func__, c->delivery_system);
1805 return 0;
1806 }
1807
1808 /*
1809 * Seek for the first delivery system that it is compatible with a
1810 * DVBv3 standard
1811 */
1812 ncaps = 0;
1813 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
1814 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1815 delsys = fe->ops.delsys[ncaps];
1816 break;
1817 }
1818 ncaps++;
1819 }
1820 if (delsys == SYS_UNDEFINED) {
1821 dev_dbg(fe->dvb->device,
1822 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1823 __func__);
1824 return -EINVAL;
1825 }
1826 return emulate_delivery_system(fe, delsys);
1827 }
1828
dtv_property_process_set(struct dvb_frontend * fe,struct dtv_property * tvp,struct file * file)1829 static int dtv_property_process_set(struct dvb_frontend *fe,
1830 struct dtv_property *tvp,
1831 struct file *file)
1832 {
1833 int r = 0;
1834 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1835
1836 /* Allow the frontend to validate incoming properties */
1837 if (fe->ops.set_property) {
1838 r = fe->ops.set_property(fe, tvp);
1839 if (r < 0)
1840 return r;
1841 }
1842
1843 switch(tvp->cmd) {
1844 case DTV_CLEAR:
1845 /*
1846 * Reset a cache of data specific to the frontend here. This does
1847 * not effect hardware.
1848 */
1849 dvb_frontend_clear_cache(fe);
1850 break;
1851 case DTV_TUNE:
1852 /* interpret the cache of data, build either a traditional frontend
1853 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1854 * ioctl.
1855 */
1856 c->state = tvp->cmd;
1857 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1858 __func__);
1859
1860 r = dtv_set_frontend(fe);
1861 break;
1862 case DTV_FREQUENCY:
1863 c->frequency = tvp->u.data;
1864 break;
1865 case DTV_MODULATION:
1866 c->modulation = tvp->u.data;
1867 break;
1868 case DTV_BANDWIDTH_HZ:
1869 c->bandwidth_hz = tvp->u.data;
1870 break;
1871 case DTV_INVERSION:
1872 c->inversion = tvp->u.data;
1873 break;
1874 case DTV_SYMBOL_RATE:
1875 c->symbol_rate = tvp->u.data;
1876 break;
1877 case DTV_INNER_FEC:
1878 c->fec_inner = tvp->u.data;
1879 break;
1880 case DTV_PILOT:
1881 c->pilot = tvp->u.data;
1882 break;
1883 case DTV_ROLLOFF:
1884 c->rolloff = tvp->u.data;
1885 break;
1886 case DTV_DELIVERY_SYSTEM:
1887 r = dvbv5_set_delivery_system(fe, tvp->u.data);
1888 break;
1889 case DTV_VOLTAGE:
1890 c->voltage = tvp->u.data;
1891 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1892 (void *)c->voltage);
1893 break;
1894 case DTV_TONE:
1895 c->sectone = tvp->u.data;
1896 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1897 (void *)c->sectone);
1898 break;
1899 case DTV_CODE_RATE_HP:
1900 c->code_rate_HP = tvp->u.data;
1901 break;
1902 case DTV_CODE_RATE_LP:
1903 c->code_rate_LP = tvp->u.data;
1904 break;
1905 case DTV_GUARD_INTERVAL:
1906 c->guard_interval = tvp->u.data;
1907 break;
1908 case DTV_TRANSMISSION_MODE:
1909 c->transmission_mode = tvp->u.data;
1910 break;
1911 case DTV_HIERARCHY:
1912 c->hierarchy = tvp->u.data;
1913 break;
1914 case DTV_INTERLEAVING:
1915 c->interleaving = tvp->u.data;
1916 break;
1917
1918 /* ISDB-T Support here */
1919 case DTV_ISDBT_PARTIAL_RECEPTION:
1920 c->isdbt_partial_reception = tvp->u.data;
1921 break;
1922 case DTV_ISDBT_SOUND_BROADCASTING:
1923 c->isdbt_sb_mode = tvp->u.data;
1924 break;
1925 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1926 c->isdbt_sb_subchannel = tvp->u.data;
1927 break;
1928 case DTV_ISDBT_SB_SEGMENT_IDX:
1929 c->isdbt_sb_segment_idx = tvp->u.data;
1930 break;
1931 case DTV_ISDBT_SB_SEGMENT_COUNT:
1932 c->isdbt_sb_segment_count = tvp->u.data;
1933 break;
1934 case DTV_ISDBT_LAYER_ENABLED:
1935 c->isdbt_layer_enabled = tvp->u.data;
1936 break;
1937 case DTV_ISDBT_LAYERA_FEC:
1938 c->layer[0].fec = tvp->u.data;
1939 break;
1940 case DTV_ISDBT_LAYERA_MODULATION:
1941 c->layer[0].modulation = tvp->u.data;
1942 break;
1943 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1944 c->layer[0].segment_count = tvp->u.data;
1945 break;
1946 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1947 c->layer[0].interleaving = tvp->u.data;
1948 break;
1949 case DTV_ISDBT_LAYERB_FEC:
1950 c->layer[1].fec = tvp->u.data;
1951 break;
1952 case DTV_ISDBT_LAYERB_MODULATION:
1953 c->layer[1].modulation = tvp->u.data;
1954 break;
1955 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1956 c->layer[1].segment_count = tvp->u.data;
1957 break;
1958 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1959 c->layer[1].interleaving = tvp->u.data;
1960 break;
1961 case DTV_ISDBT_LAYERC_FEC:
1962 c->layer[2].fec = tvp->u.data;
1963 break;
1964 case DTV_ISDBT_LAYERC_MODULATION:
1965 c->layer[2].modulation = tvp->u.data;
1966 break;
1967 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1968 c->layer[2].segment_count = tvp->u.data;
1969 break;
1970 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1971 c->layer[2].interleaving = tvp->u.data;
1972 break;
1973
1974 /* Multistream support */
1975 case DTV_STREAM_ID:
1976 case DTV_DVBT2_PLP_ID_LEGACY:
1977 c->stream_id = tvp->u.data;
1978 break;
1979
1980 /* ATSC-MH */
1981 case DTV_ATSCMH_PARADE_ID:
1982 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1983 break;
1984 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1985 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1986 break;
1987
1988 case DTV_LNA:
1989 c->lna = tvp->u.data;
1990 if (fe->ops.set_lna)
1991 r = fe->ops.set_lna(fe);
1992 if (r < 0)
1993 c->lna = LNA_AUTO;
1994 break;
1995
1996 default:
1997 return -EINVAL;
1998 }
1999
2000 return r;
2001 }
2002
dvb_frontend_ioctl(struct file * file,unsigned int cmd,void * parg)2003 static int dvb_frontend_ioctl(struct file *file,
2004 unsigned int cmd, void *parg)
2005 {
2006 struct dvb_device *dvbdev = file->private_data;
2007 struct dvb_frontend *fe = dvbdev->priv;
2008 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2009 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2010 int err = -EOPNOTSUPP;
2011
2012 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
2013 if (down_interruptible(&fepriv->sem))
2014 return -ERESTARTSYS;
2015
2016 if (fe->exit != DVB_FE_NO_EXIT) {
2017 up(&fepriv->sem);
2018 return -ENODEV;
2019 }
2020
2021 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
2022 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
2023 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
2024 up(&fepriv->sem);
2025 return -EPERM;
2026 }
2027
2028 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
2029 err = dvb_frontend_ioctl_properties(file, cmd, parg);
2030 else {
2031 c->state = DTV_UNDEFINED;
2032 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
2033 }
2034
2035 up(&fepriv->sem);
2036 return err;
2037 }
2038
dvb_frontend_ioctl_properties(struct file * file,unsigned int cmd,void * parg)2039 static int dvb_frontend_ioctl_properties(struct file *file,
2040 unsigned int cmd, void *parg)
2041 {
2042 struct dvb_device *dvbdev = file->private_data;
2043 struct dvb_frontend *fe = dvbdev->priv;
2044 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2045 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2046 int err = 0;
2047
2048 struct dtv_properties *tvps = parg;
2049 struct dtv_property *tvp = NULL;
2050 int i;
2051
2052 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2053
2054 if (cmd == FE_SET_PROPERTY) {
2055 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2056 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2057
2058 /* Put an arbitrary limit on the number of messages that can
2059 * be sent at once */
2060 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2061 return -EINVAL;
2062
2063 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
2064 if (!tvp) {
2065 err = -ENOMEM;
2066 goto out;
2067 }
2068
2069 if (copy_from_user(tvp, (void __user *)tvps->props,
2070 tvps->num * sizeof(struct dtv_property))) {
2071 err = -EFAULT;
2072 goto out;
2073 }
2074
2075 for (i = 0; i < tvps->num; i++) {
2076 err = dtv_property_process_set(fe, tvp + i, file);
2077 if (err < 0)
2078 goto out;
2079 (tvp + i)->result = err;
2080 }
2081
2082 if (c->state == DTV_TUNE)
2083 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
2084
2085 } else if (cmd == FE_GET_PROPERTY) {
2086 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2087 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
2088
2089 /* Put an arbitrary limit on the number of messages that can
2090 * be sent at once */
2091 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
2092 return -EINVAL;
2093
2094 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
2095 if (!tvp) {
2096 err = -ENOMEM;
2097 goto out;
2098 }
2099
2100 if (copy_from_user(tvp, (void __user *)tvps->props,
2101 tvps->num * sizeof(struct dtv_property))) {
2102 err = -EFAULT;
2103 goto out;
2104 }
2105
2106 /*
2107 * Fills the cache out struct with the cache contents, plus
2108 * the data retrieved from get_frontend, if the frontend
2109 * is not idle. Otherwise, returns the cached content
2110 */
2111 if (fepriv->state != FESTATE_IDLE) {
2112 err = dtv_get_frontend(fe, NULL);
2113 if (err < 0)
2114 goto out;
2115 }
2116 for (i = 0; i < tvps->num; i++) {
2117 err = dtv_property_process_get(fe, c, tvp + i, file);
2118 if (err < 0)
2119 goto out;
2120 (tvp + i)->result = err;
2121 }
2122
2123 if (copy_to_user((void __user *)tvps->props, tvp,
2124 tvps->num * sizeof(struct dtv_property))) {
2125 err = -EFAULT;
2126 goto out;
2127 }
2128
2129 } else
2130 err = -EOPNOTSUPP;
2131
2132 out:
2133 kfree(tvp);
2134 return err;
2135 }
2136
dtv_set_frontend(struct dvb_frontend * fe)2137 static int dtv_set_frontend(struct dvb_frontend *fe)
2138 {
2139 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2140 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2141 struct dvb_frontend_tune_settings fetunesettings;
2142 u32 rolloff = 0;
2143
2144 if (dvb_frontend_check_parameters(fe) < 0)
2145 return -EINVAL;
2146
2147 /*
2148 * Initialize output parameters to match the values given by
2149 * the user. FE_SET_FRONTEND triggers an initial frontend event
2150 * with status = 0, which copies output parameters to userspace.
2151 */
2152 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
2153
2154 /*
2155 * Be sure that the bandwidth will be filled for all
2156 * non-satellite systems, as tuners need to know what
2157 * low pass/Nyquist half filter should be applied, in
2158 * order to avoid inter-channel noise.
2159 *
2160 * ISDB-T and DVB-T/T2 already sets bandwidth.
2161 * ATSC and DVB-C don't set, so, the core should fill it.
2162 *
2163 * On DVB-C Annex A and C, the bandwidth is a function of
2164 * the roll-off and symbol rate. Annex B defines different
2165 * roll-off factors depending on the modulation. Fortunately,
2166 * Annex B is only used with 6MHz, so there's no need to
2167 * calculate it.
2168 *
2169 * While not officially supported, a side effect of handling it at
2170 * the cache level is that a program could retrieve the bandwidth
2171 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2172 */
2173 switch (c->delivery_system) {
2174 case SYS_ATSC:
2175 case SYS_DVBC_ANNEX_B:
2176 c->bandwidth_hz = 6000000;
2177 break;
2178 case SYS_DVBC_ANNEX_A:
2179 rolloff = 115;
2180 break;
2181 case SYS_DVBC_ANNEX_C:
2182 rolloff = 113;
2183 break;
2184 case SYS_DVBS:
2185 case SYS_TURBO:
2186 case SYS_ISDBS:
2187 rolloff = 135;
2188 break;
2189 case SYS_DVBS2:
2190 switch (c->rolloff) {
2191 case ROLLOFF_20:
2192 rolloff = 120;
2193 break;
2194 case ROLLOFF_25:
2195 rolloff = 125;
2196 break;
2197 default:
2198 case ROLLOFF_35:
2199 rolloff = 135;
2200 }
2201 break;
2202 default:
2203 break;
2204 }
2205 if (rolloff)
2206 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100);
2207
2208 /* force auto frequency inversion if requested */
2209 if (dvb_force_auto_inversion)
2210 c->inversion = INVERSION_AUTO;
2211
2212 /*
2213 * without hierarchical coding code_rate_LP is irrelevant,
2214 * so we tolerate the otherwise invalid FEC_NONE setting
2215 */
2216 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2217 c->code_rate_LP = FEC_AUTO;
2218
2219 /* get frontend-specific tuning settings */
2220 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
2221 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2222 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2223 fepriv->max_drift = fetunesettings.max_drift;
2224 fepriv->step_size = fetunesettings.step_size;
2225 } else {
2226 /* default values */
2227 switch (c->delivery_system) {
2228 case SYS_DVBS:
2229 case SYS_DVBS2:
2230 case SYS_ISDBS:
2231 case SYS_TURBO:
2232 case SYS_DVBC_ANNEX_A:
2233 case SYS_DVBC_ANNEX_C:
2234 fepriv->min_delay = HZ / 20;
2235 fepriv->step_size = c->symbol_rate / 16000;
2236 fepriv->max_drift = c->symbol_rate / 2000;
2237 break;
2238 case SYS_DVBT:
2239 case SYS_DVBT2:
2240 case SYS_ISDBT:
2241 case SYS_DTMB:
2242 fepriv->min_delay = HZ / 20;
2243 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2244 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2245 break;
2246 default:
2247 /*
2248 * FIXME: This sounds wrong! if freqency_stepsize is
2249 * defined by the frontend, why not use it???
2250 */
2251 fepriv->min_delay = HZ / 20;
2252 fepriv->step_size = 0; /* no zigzag */
2253 fepriv->max_drift = 0;
2254 break;
2255 }
2256 }
2257 if (dvb_override_tune_delay > 0)
2258 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2259
2260 fepriv->state = FESTATE_RETUNE;
2261
2262 /* Request the search algorithm to search */
2263 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2264
2265 dvb_frontend_clear_events(fe);
2266 dvb_frontend_add_event(fe, 0);
2267 dvb_frontend_wakeup(fe);
2268 fepriv->status = 0;
2269
2270 return 0;
2271 }
2272
2273
dvb_frontend_ioctl_legacy(struct file * file,unsigned int cmd,void * parg)2274 static int dvb_frontend_ioctl_legacy(struct file *file,
2275 unsigned int cmd, void *parg)
2276 {
2277 struct dvb_device *dvbdev = file->private_data;
2278 struct dvb_frontend *fe = dvbdev->priv;
2279 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2280 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2281 int err = -EOPNOTSUPP;
2282
2283 switch (cmd) {
2284 case FE_GET_INFO: {
2285 struct dvb_frontend_info* info = parg;
2286
2287 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2288 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2289
2290 /*
2291 * Associate the 4 delivery systems supported by DVBv3
2292 * API with their DVBv5 counterpart. For the other standards,
2293 * use the closest type, assuming that it would hopefully
2294 * work with a DVBv3 application.
2295 * It should be noticed that, on multi-frontend devices with
2296 * different types (terrestrial and cable, for example),
2297 * a pure DVBv3 application won't be able to use all delivery
2298 * systems. Yet, changing the DVBv5 cache to the other delivery
2299 * system should be enough for making it work.
2300 */
2301 switch (dvbv3_type(c->delivery_system)) {
2302 case DVBV3_QPSK:
2303 info->type = FE_QPSK;
2304 break;
2305 case DVBV3_ATSC:
2306 info->type = FE_ATSC;
2307 break;
2308 case DVBV3_QAM:
2309 info->type = FE_QAM;
2310 break;
2311 case DVBV3_OFDM:
2312 info->type = FE_OFDM;
2313 break;
2314 default:
2315 dev_err(fe->dvb->device,
2316 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2317 __func__, c->delivery_system);
2318 fe->ops.info.type = FE_OFDM;
2319 }
2320 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2321 __func__, c->delivery_system, fe->ops.info.type);
2322
2323 /* Set CAN_INVERSION_AUTO bit on in other than oneshot mode */
2324 if (!(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT))
2325 info->caps |= FE_CAN_INVERSION_AUTO;
2326 err = 0;
2327 break;
2328 }
2329
2330 case FE_READ_STATUS: {
2331 enum fe_status *status = parg;
2332
2333 /* if retune was requested but hasn't occurred yet, prevent
2334 * that user get signal state from previous tuning */
2335 if (fepriv->state == FESTATE_RETUNE ||
2336 fepriv->state == FESTATE_ERROR) {
2337 err=0;
2338 *status = 0;
2339 break;
2340 }
2341
2342 if (fe->ops.read_status)
2343 err = fe->ops.read_status(fe, status);
2344 break;
2345 }
2346
2347 case FE_READ_BER:
2348 if (fe->ops.read_ber) {
2349 if (fepriv->thread)
2350 err = fe->ops.read_ber(fe, (__u32 *) parg);
2351 else
2352 err = -EAGAIN;
2353 }
2354 break;
2355
2356 case FE_READ_SIGNAL_STRENGTH:
2357 if (fe->ops.read_signal_strength) {
2358 if (fepriv->thread)
2359 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2360 else
2361 err = -EAGAIN;
2362 }
2363 break;
2364
2365 case FE_READ_SNR:
2366 if (fe->ops.read_snr) {
2367 if (fepriv->thread)
2368 err = fe->ops.read_snr(fe, (__u16 *) parg);
2369 else
2370 err = -EAGAIN;
2371 }
2372 break;
2373
2374 case FE_READ_UNCORRECTED_BLOCKS:
2375 if (fe->ops.read_ucblocks) {
2376 if (fepriv->thread)
2377 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2378 else
2379 err = -EAGAIN;
2380 }
2381 break;
2382
2383 case FE_DISEQC_RESET_OVERLOAD:
2384 if (fe->ops.diseqc_reset_overload) {
2385 err = fe->ops.diseqc_reset_overload(fe);
2386 fepriv->state = FESTATE_DISEQC;
2387 fepriv->status = 0;
2388 }
2389 break;
2390
2391 case FE_DISEQC_SEND_MASTER_CMD:
2392 if (fe->ops.diseqc_send_master_cmd) {
2393 struct dvb_diseqc_master_cmd *cmd = parg;
2394
2395 if (cmd->msg_len > sizeof(cmd->msg)) {
2396 err = -EINVAL;
2397 break;
2398 }
2399 err = fe->ops.diseqc_send_master_cmd(fe, cmd);
2400 fepriv->state = FESTATE_DISEQC;
2401 fepriv->status = 0;
2402 }
2403 break;
2404
2405 case FE_DISEQC_SEND_BURST:
2406 if (fe->ops.diseqc_send_burst) {
2407 err = fe->ops.diseqc_send_burst(fe,
2408 (enum fe_sec_mini_cmd)parg);
2409 fepriv->state = FESTATE_DISEQC;
2410 fepriv->status = 0;
2411 }
2412 break;
2413
2414 case FE_SET_TONE:
2415 if (fe->ops.set_tone) {
2416 err = fe->ops.set_tone(fe,
2417 (enum fe_sec_tone_mode)parg);
2418 fepriv->tone = (enum fe_sec_tone_mode)parg;
2419 fepriv->state = FESTATE_DISEQC;
2420 fepriv->status = 0;
2421 }
2422 break;
2423
2424 case FE_SET_VOLTAGE:
2425 if (fe->ops.set_voltage) {
2426 err = fe->ops.set_voltage(fe,
2427 (enum fe_sec_voltage)parg);
2428 fepriv->voltage = (enum fe_sec_voltage)parg;
2429 fepriv->state = FESTATE_DISEQC;
2430 fepriv->status = 0;
2431 }
2432 break;
2433
2434 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2435 if (fe->ops.dishnetwork_send_legacy_command) {
2436 err = fe->ops.dishnetwork_send_legacy_command(fe,
2437 (unsigned long)parg);
2438 fepriv->state = FESTATE_DISEQC;
2439 fepriv->status = 0;
2440 } else if (fe->ops.set_voltage) {
2441 /*
2442 * NOTE: This is a fallback condition. Some frontends
2443 * (stv0299 for instance) take longer than 8msec to
2444 * respond to a set_voltage command. Those switches
2445 * need custom routines to switch properly. For all
2446 * other frontends, the following should work ok.
2447 * Dish network legacy switches (as used by Dish500)
2448 * are controlled by sending 9-bit command words
2449 * spaced 8msec apart.
2450 * the actual command word is switch/port dependent
2451 * so it is up to the userspace application to send
2452 * the right command.
2453 * The command must always start with a '0' after
2454 * initialization, so parg is 8 bits and does not
2455 * include the initialization or start bit
2456 */
2457 unsigned long swcmd = ((unsigned long) parg) << 1;
2458 ktime_t nexttime;
2459 ktime_t tv[10];
2460 int i;
2461 u8 last = 1;
2462 if (dvb_frontend_debug)
2463 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2464 nexttime = ktime_get_real();
2465 if (dvb_frontend_debug)
2466 tv[0] = nexttime;
2467 /* before sending a command, initialize by sending
2468 * a 32ms 18V to the switch
2469 */
2470 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2471 dvb_frontend_sleep_until(&nexttime, 32000);
2472
2473 for (i = 0; i < 9; i++) {
2474 if (dvb_frontend_debug)
2475 tv[i+1] = ktime_get_real();
2476 if ((swcmd & 0x01) != last) {
2477 /* set voltage to (last ? 13V : 18V) */
2478 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2479 last = (last) ? 0 : 1;
2480 }
2481 swcmd = swcmd >> 1;
2482 if (i != 8)
2483 dvb_frontend_sleep_until(&nexttime, 8000);
2484 }
2485 if (dvb_frontend_debug) {
2486 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2487 __func__, fe->dvb->num);
2488 for (i = 1; i < 10; i++)
2489 printk("%d: %d\n", i,
2490 (int) ktime_us_delta(tv[i], tv[i-1]));
2491 }
2492 err = 0;
2493 fepriv->state = FESTATE_DISEQC;
2494 fepriv->status = 0;
2495 }
2496 break;
2497
2498 case FE_DISEQC_RECV_SLAVE_REPLY:
2499 if (fe->ops.diseqc_recv_slave_reply)
2500 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2501 break;
2502
2503 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2504 if (fe->ops.enable_high_lnb_voltage)
2505 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2506 break;
2507
2508 case FE_SET_FRONTEND:
2509 err = dvbv3_set_delivery_system(fe);
2510 if (err)
2511 break;
2512
2513 err = dtv_property_cache_sync(fe, c, parg);
2514 if (err)
2515 break;
2516 err = dtv_set_frontend(fe);
2517 break;
2518 case FE_GET_EVENT:
2519 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2520 break;
2521
2522 case FE_GET_FRONTEND:
2523 err = dtv_get_frontend(fe, parg);
2524 break;
2525
2526 case FE_SET_FRONTEND_TUNE_MODE:
2527 fepriv->tune_mode_flags = (unsigned long) parg;
2528 err = 0;
2529 break;
2530 }
2531
2532 return err;
2533 }
2534
2535
dvb_frontend_poll(struct file * file,struct poll_table_struct * wait)2536 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2537 {
2538 struct dvb_device *dvbdev = file->private_data;
2539 struct dvb_frontend *fe = dvbdev->priv;
2540 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2541
2542 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2543
2544 poll_wait (file, &fepriv->events.wait_queue, wait);
2545
2546 if (fepriv->events.eventw != fepriv->events.eventr)
2547 return (POLLIN | POLLRDNORM | POLLPRI);
2548
2549 return 0;
2550 }
2551
dvb_frontend_open(struct inode * inode,struct file * file)2552 static int dvb_frontend_open(struct inode *inode, struct file *file)
2553 {
2554 struct dvb_device *dvbdev = file->private_data;
2555 struct dvb_frontend *fe = dvbdev->priv;
2556 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2557 struct dvb_adapter *adapter = fe->dvb;
2558 int ret;
2559
2560 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2561 if (fe->exit == DVB_FE_DEVICE_REMOVED)
2562 return -ENODEV;
2563
2564 if (adapter->mfe_shared) {
2565 mutex_lock (&adapter->mfe_lock);
2566
2567 if (adapter->mfe_dvbdev == NULL)
2568 adapter->mfe_dvbdev = dvbdev;
2569
2570 else if (adapter->mfe_dvbdev != dvbdev) {
2571 struct dvb_device
2572 *mfedev = adapter->mfe_dvbdev;
2573 struct dvb_frontend
2574 *mfe = mfedev->priv;
2575 struct dvb_frontend_private
2576 *mfepriv = mfe->frontend_priv;
2577 int mferetry = (dvb_mfe_wait_time << 1);
2578
2579 mutex_unlock (&adapter->mfe_lock);
2580 while (mferetry-- && (mfedev->users != -1 ||
2581 mfepriv->thread != NULL)) {
2582 if(msleep_interruptible(500)) {
2583 if(signal_pending(current))
2584 return -EINTR;
2585 }
2586 }
2587
2588 mutex_lock (&adapter->mfe_lock);
2589 if(adapter->mfe_dvbdev != dvbdev) {
2590 mfedev = adapter->mfe_dvbdev;
2591 mfe = mfedev->priv;
2592 mfepriv = mfe->frontend_priv;
2593 if (mfedev->users != -1 ||
2594 mfepriv->thread != NULL) {
2595 mutex_unlock (&adapter->mfe_lock);
2596 return -EBUSY;
2597 }
2598 adapter->mfe_dvbdev = dvbdev;
2599 }
2600 }
2601 }
2602
2603 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2604 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2605 goto err0;
2606
2607 /* If we took control of the bus, we need to force
2608 reinitialization. This is because many ts_bus_ctrl()
2609 functions strobe the RESET pin on the demod, and if the
2610 frontend thread already exists then the dvb_init() routine
2611 won't get called (which is what usually does initial
2612 register configuration). */
2613 fepriv->reinitialise = 1;
2614 }
2615
2616 if ((ret = dvb_generic_open (inode, file)) < 0)
2617 goto err1;
2618
2619 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2620 /* normal tune mode when opened R/W */
2621 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2622 fepriv->tone = -1;
2623 fepriv->voltage = -1;
2624
2625 ret = dvb_frontend_start (fe);
2626 if (ret)
2627 goto err2;
2628
2629 /* empty event queue */
2630 fepriv->events.eventr = fepriv->events.eventw = 0;
2631 }
2632
2633 if (adapter->mfe_shared)
2634 mutex_unlock (&adapter->mfe_lock);
2635 return ret;
2636
2637 err2:
2638 dvb_generic_release(inode, file);
2639 err1:
2640 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2641 fe->ops.ts_bus_ctrl(fe, 0);
2642 err0:
2643 if (adapter->mfe_shared)
2644 mutex_unlock (&adapter->mfe_lock);
2645 return ret;
2646 }
2647
dvb_frontend_release(struct inode * inode,struct file * file)2648 static int dvb_frontend_release(struct inode *inode, struct file *file)
2649 {
2650 struct dvb_device *dvbdev = file->private_data;
2651 struct dvb_frontend *fe = dvbdev->priv;
2652 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2653 int ret;
2654
2655 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2656
2657 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2658 fepriv->release_jiffies = jiffies;
2659 mb();
2660 }
2661
2662 ret = dvb_generic_release (inode, file);
2663
2664 if (dvbdev->users == -1) {
2665 wake_up(&fepriv->wait_queue);
2666 if (fe->exit != DVB_FE_NO_EXIT)
2667 wake_up(&dvbdev->wait_queue);
2668 if (fe->ops.ts_bus_ctrl)
2669 fe->ops.ts_bus_ctrl(fe, 0);
2670 }
2671
2672 return ret;
2673 }
2674
2675 static const struct file_operations dvb_frontend_fops = {
2676 .owner = THIS_MODULE,
2677 .unlocked_ioctl = dvb_generic_ioctl,
2678 .poll = dvb_frontend_poll,
2679 .open = dvb_frontend_open,
2680 .release = dvb_frontend_release,
2681 .llseek = noop_llseek,
2682 };
2683
dvb_frontend_suspend(struct dvb_frontend * fe)2684 int dvb_frontend_suspend(struct dvb_frontend *fe)
2685 {
2686 int ret = 0;
2687
2688 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2689 fe->id);
2690
2691 if (fe->ops.tuner_ops.suspend)
2692 ret = fe->ops.tuner_ops.suspend(fe);
2693 else if (fe->ops.tuner_ops.sleep)
2694 ret = fe->ops.tuner_ops.sleep(fe);
2695
2696 if (fe->ops.sleep)
2697 ret = fe->ops.sleep(fe);
2698
2699 return ret;
2700 }
2701 EXPORT_SYMBOL(dvb_frontend_suspend);
2702
dvb_frontend_resume(struct dvb_frontend * fe)2703 int dvb_frontend_resume(struct dvb_frontend *fe)
2704 {
2705 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2706 int ret = 0;
2707
2708 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2709 fe->id);
2710
2711 fe->exit = DVB_FE_DEVICE_RESUME;
2712 if (fe->ops.init)
2713 ret = fe->ops.init(fe);
2714
2715 if (fe->ops.tuner_ops.resume)
2716 ret = fe->ops.tuner_ops.resume(fe);
2717 else if (fe->ops.tuner_ops.init)
2718 ret = fe->ops.tuner_ops.init(fe);
2719
2720 fe->exit = DVB_FE_NO_EXIT;
2721 fepriv->state = FESTATE_RETUNE;
2722 dvb_frontend_wakeup(fe);
2723
2724 return ret;
2725 }
2726 EXPORT_SYMBOL(dvb_frontend_resume);
2727
dvb_register_frontend(struct dvb_adapter * dvb,struct dvb_frontend * fe)2728 int dvb_register_frontend(struct dvb_adapter* dvb,
2729 struct dvb_frontend* fe)
2730 {
2731 struct dvb_frontend_private *fepriv;
2732 const struct dvb_device dvbdev_template = {
2733 .users = ~0,
2734 .writers = 1,
2735 .readers = (~0)-1,
2736 .fops = &dvb_frontend_fops,
2737 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2738 .name = fe->ops.info.name,
2739 #endif
2740 .kernel_ioctl = dvb_frontend_ioctl
2741 };
2742
2743 dev_dbg(dvb->device, "%s:\n", __func__);
2744
2745 if (mutex_lock_interruptible(&frontend_mutex))
2746 return -ERESTARTSYS;
2747
2748 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2749 if (fe->frontend_priv == NULL) {
2750 mutex_unlock(&frontend_mutex);
2751 return -ENOMEM;
2752 }
2753 fepriv = fe->frontend_priv;
2754
2755 sema_init(&fepriv->sem, 1);
2756 init_waitqueue_head (&fepriv->wait_queue);
2757 init_waitqueue_head (&fepriv->events.wait_queue);
2758 mutex_init(&fepriv->events.mtx);
2759 fe->dvb = dvb;
2760 fepriv->inversion = INVERSION_OFF;
2761
2762 dev_info(fe->dvb->device,
2763 "DVB: registering adapter %i frontend %i (%s)...\n",
2764 fe->dvb->num, fe->id, fe->ops.info.name);
2765
2766 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2767 fe, DVB_DEVICE_FRONTEND);
2768
2769 /*
2770 * Initialize the cache to the proper values according with the
2771 * first supported delivery system (ops->delsys[0])
2772 */
2773
2774 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2775 dvb_frontend_clear_cache(fe);
2776
2777 mutex_unlock(&frontend_mutex);
2778 return 0;
2779 }
2780 EXPORT_SYMBOL(dvb_register_frontend);
2781
dvb_unregister_frontend(struct dvb_frontend * fe)2782 int dvb_unregister_frontend(struct dvb_frontend* fe)
2783 {
2784 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2785 dev_dbg(fe->dvb->device, "%s:\n", __func__);
2786
2787 mutex_lock(&frontend_mutex);
2788 dvb_frontend_stop (fe);
2789 mutex_unlock(&frontend_mutex);
2790
2791 if (fepriv->dvbdev->users < -1)
2792 wait_event(fepriv->dvbdev->wait_queue,
2793 fepriv->dvbdev->users==-1);
2794
2795 mutex_lock(&frontend_mutex);
2796 dvb_unregister_device (fepriv->dvbdev);
2797
2798 /* fe is invalid now */
2799 kfree(fepriv);
2800 mutex_unlock(&frontend_mutex);
2801 return 0;
2802 }
2803 EXPORT_SYMBOL(dvb_unregister_frontend);
2804
2805 #ifdef CONFIG_MEDIA_ATTACH
dvb_frontend_detach(struct dvb_frontend * fe)2806 void dvb_frontend_detach(struct dvb_frontend* fe)
2807 {
2808 void *ptr;
2809
2810 if (fe->ops.release_sec) {
2811 fe->ops.release_sec(fe);
2812 dvb_detach(fe->ops.release_sec);
2813 }
2814 if (fe->ops.tuner_ops.release) {
2815 fe->ops.tuner_ops.release(fe);
2816 dvb_detach(fe->ops.tuner_ops.release);
2817 }
2818 if (fe->ops.analog_ops.release) {
2819 fe->ops.analog_ops.release(fe);
2820 dvb_detach(fe->ops.analog_ops.release);
2821 }
2822 ptr = (void*)fe->ops.release;
2823 if (ptr) {
2824 fe->ops.release(fe);
2825 dvb_detach(ptr);
2826 }
2827 }
2828 #else
dvb_frontend_detach(struct dvb_frontend * fe)2829 void dvb_frontend_detach(struct dvb_frontend* fe)
2830 {
2831 if (fe->ops.release_sec)
2832 fe->ops.release_sec(fe);
2833 if (fe->ops.tuner_ops.release)
2834 fe->ops.tuner_ops.release(fe);
2835 if (fe->ops.analog_ops.release)
2836 fe->ops.analog_ops.release(fe);
2837 if (fe->ops.release)
2838 fe->ops.release(fe);
2839 }
2840 #endif
2841 EXPORT_SYMBOL(dvb_frontend_detach);
2842