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