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