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