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