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