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  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26  */
27 
28 /* Enables DVBv3 compatibility bits at the headers */
29 #define __DVB_CORE__
30 
31 #include <linux/string.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/semaphore.h>
38 #include <linux/module.h>
39 #include <linux/list.h>
40 #include <linux/freezer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kthread.h>
43 #include <asm/processor.h>
44 
45 #include "dvb_frontend.h"
46 #include "dvbdev.h"
47 #include <linux/dvb/version.h>
48 
49 static int dvb_frontend_debug;
50 static int dvb_shutdown_timeout;
51 static int dvb_force_auto_inversion;
52 static int dvb_override_tune_delay;
53 static int dvb_powerdown_on_sleep = 1;
54 static int dvb_mfe_wait_time = 5;
55 
56 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
57 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
58 module_param(dvb_shutdown_timeout, int, 0644);
59 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
60 module_param(dvb_force_auto_inversion, int, 0644);
61 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
62 module_param(dvb_override_tune_delay, int, 0644);
63 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
64 module_param(dvb_powerdown_on_sleep, int, 0644);
65 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
66 module_param(dvb_mfe_wait_time, int, 0644);
67 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)");
68 
69 #define dprintk if (dvb_frontend_debug) printk
70 
71 #define FESTATE_IDLE 1
72 #define FESTATE_RETUNE 2
73 #define FESTATE_TUNING_FAST 4
74 #define FESTATE_TUNING_SLOW 8
75 #define FESTATE_TUNED 16
76 #define FESTATE_ZIGZAG_FAST 32
77 #define FESTATE_ZIGZAG_SLOW 64
78 #define FESTATE_DISEQC 128
79 #define FESTATE_ERROR 256
80 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
81 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
82 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
83 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
84 
85 #define FE_ALGO_HW		1
86 /*
87  * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
88  * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
89  * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
90  * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
91  * FESTATE_TUNED. The frontend has successfully locked on.
92  * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
93  * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
94  * FESTATE_DISEQC. A DISEQC command has just been issued.
95  * FESTATE_WAITFORLOCK. When we're waiting for a lock.
96  * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
97  * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
98  * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
99  */
100 
101 #define DVB_FE_NO_EXIT	0
102 #define DVB_FE_NORMAL_EXIT	1
103 #define DVB_FE_DEVICE_REMOVED	2
104 
105 static DEFINE_MUTEX(frontend_mutex);
106 
107 struct dvb_frontend_private {
108 
109 	/* thread/frontend values */
110 	struct dvb_device *dvbdev;
111 	struct dvb_frontend_parameters parameters_out;
112 	struct dvb_fe_events events;
113 	struct semaphore sem;
114 	struct list_head list_head;
115 	wait_queue_head_t wait_queue;
116 	struct task_struct *thread;
117 	unsigned long release_jiffies;
118 	unsigned int exit;
119 	unsigned int wakeup;
120 	fe_status_t status;
121 	unsigned long tune_mode_flags;
122 	unsigned int delay;
123 	unsigned int reinitialise;
124 	int tone;
125 	int voltage;
126 
127 	/* swzigzag values */
128 	unsigned int state;
129 	unsigned int bending;
130 	int lnb_drift;
131 	unsigned int inversion;
132 	unsigned int auto_step;
133 	unsigned int auto_sub_step;
134 	unsigned int started_auto_step;
135 	unsigned int min_delay;
136 	unsigned int max_drift;
137 	unsigned int step_size;
138 	int quality;
139 	unsigned int check_wrapped;
140 	enum dvbfe_search algo_status;
141 };
142 
143 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
144 static int dtv_get_frontend(struct dvb_frontend *fe,
145 			    struct dvb_frontend_parameters *p_out);
146 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
147 					   struct dvb_frontend_parameters *p);
148 
has_get_frontend(struct dvb_frontend * fe)149 static bool has_get_frontend(struct dvb_frontend *fe)
150 {
151 	return fe->ops.get_frontend != NULL;
152 }
153 
154 /*
155  * Due to DVBv3 API calls, a delivery system should be mapped into one of
156  * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
157  * otherwise, a DVBv3 call will fail.
158  */
159 enum dvbv3_emulation_type {
160 	DVBV3_UNKNOWN,
161 	DVBV3_QPSK,
162 	DVBV3_QAM,
163 	DVBV3_OFDM,
164 	DVBV3_ATSC,
165 };
166 
dvbv3_type(u32 delivery_system)167 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
168 {
169 	switch (delivery_system) {
170 	case SYS_DVBC_ANNEX_A:
171 	case SYS_DVBC_ANNEX_C:
172 		return DVBV3_QAM;
173 	case SYS_DVBS:
174 	case SYS_DVBS2:
175 	case SYS_TURBO:
176 	case SYS_ISDBS:
177 	case SYS_DSS:
178 		return DVBV3_QPSK;
179 	case SYS_DVBT:
180 	case SYS_DVBT2:
181 	case SYS_ISDBT:
182 	case SYS_DMBTH:
183 		return DVBV3_OFDM;
184 	case SYS_ATSC:
185 	case SYS_DVBC_ANNEX_B:
186 		return DVBV3_ATSC;
187 	case SYS_UNDEFINED:
188 	case SYS_ISDBC:
189 	case SYS_DVBH:
190 	case SYS_DAB:
191 	case SYS_ATSCMH:
192 	default:
193 		/*
194 		 * Doesn't know how to emulate those types and/or
195 		 * there's no frontend driver from this type yet
196 		 * with some emulation code, so, we're not sure yet how
197 		 * to handle them, or they're not compatible with a DVBv3 call.
198 		 */
199 		return DVBV3_UNKNOWN;
200 	}
201 }
202 
dvb_frontend_add_event(struct dvb_frontend * fe,fe_status_t status)203 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
204 {
205 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
206 	struct dvb_fe_events *events = &fepriv->events;
207 	struct dvb_frontend_event *e;
208 	int wp;
209 
210 	dprintk ("%s\n", __func__);
211 
212 	if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
213 		dtv_get_frontend(fe, &fepriv->parameters_out);
214 
215 	mutex_lock(&events->mtx);
216 
217 	wp = (events->eventw + 1) % MAX_EVENT;
218 	if (wp == events->eventr) {
219 		events->overflow = 1;
220 		events->eventr = (events->eventr + 1) % MAX_EVENT;
221 	}
222 
223 	e = &events->events[events->eventw];
224 	e->status = status;
225 	e->parameters = fepriv->parameters_out;
226 
227 	events->eventw = wp;
228 
229 	mutex_unlock(&events->mtx);
230 
231 	wake_up_interruptible (&events->wait_queue);
232 }
233 
dvb_frontend_get_event(struct dvb_frontend * fe,struct dvb_frontend_event * event,int flags)234 static int dvb_frontend_get_event(struct dvb_frontend *fe,
235 			    struct dvb_frontend_event *event, int flags)
236 {
237 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
238 	struct dvb_fe_events *events = &fepriv->events;
239 
240 	dprintk ("%s\n", __func__);
241 
242 	if (events->overflow) {
243 		events->overflow = 0;
244 		return -EOVERFLOW;
245 	}
246 
247 	if (events->eventw == events->eventr) {
248 		int ret;
249 
250 		if (flags & O_NONBLOCK)
251 			return -EWOULDBLOCK;
252 
253 		up(&fepriv->sem);
254 
255 		ret = wait_event_interruptible (events->wait_queue,
256 						events->eventw != events->eventr);
257 
258 		if (down_interruptible (&fepriv->sem))
259 			return -ERESTARTSYS;
260 
261 		if (ret < 0)
262 			return ret;
263 	}
264 
265 	mutex_lock(&events->mtx);
266 	*event = events->events[events->eventr];
267 	events->eventr = (events->eventr + 1) % MAX_EVENT;
268 	mutex_unlock(&events->mtx);
269 
270 	return 0;
271 }
272 
dvb_frontend_clear_events(struct dvb_frontend * fe)273 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
274 {
275 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
276 	struct dvb_fe_events *events = &fepriv->events;
277 
278 	mutex_lock(&events->mtx);
279 	events->eventr = events->eventw;
280 	mutex_unlock(&events->mtx);
281 }
282 
dvb_frontend_init(struct dvb_frontend * fe)283 static void dvb_frontend_init(struct dvb_frontend *fe)
284 {
285 	dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
286 		 fe->dvb->num,
287 		 fe->id,
288 		 fe->ops.info.name);
289 
290 	if (fe->ops.init)
291 		fe->ops.init(fe);
292 	if (fe->ops.tuner_ops.init) {
293 		if (fe->ops.i2c_gate_ctrl)
294 			fe->ops.i2c_gate_ctrl(fe, 1);
295 		fe->ops.tuner_ops.init(fe);
296 		if (fe->ops.i2c_gate_ctrl)
297 			fe->ops.i2c_gate_ctrl(fe, 0);
298 	}
299 }
300 
dvb_frontend_reinitialise(struct dvb_frontend * fe)301 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
302 {
303 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
304 
305 	fepriv->reinitialise = 1;
306 	dvb_frontend_wakeup(fe);
307 }
308 EXPORT_SYMBOL(dvb_frontend_reinitialise);
309 
dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private * fepriv,int locked)310 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
311 {
312 	int q2;
313 
314 	dprintk ("%s\n", __func__);
315 
316 	if (locked)
317 		(fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
318 	else
319 		(fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
320 
321 	q2 = fepriv->quality - 128;
322 	q2 *= q2;
323 
324 	fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
325 }
326 
327 /**
328  * Performs automatic twiddling of frontend parameters.
329  *
330  * @param fe The frontend concerned.
331  * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
332  * @returns Number of complete iterations that have been performed.
333  */
dvb_frontend_swzigzag_autotune(struct dvb_frontend * fe,int check_wrapped)334 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
335 {
336 	int autoinversion;
337 	int ready = 0;
338 	int fe_set_err = 0;
339 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
340 	struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
341 	int original_inversion = c->inversion;
342 	u32 original_frequency = c->frequency;
343 
344 	/* are we using autoinversion? */
345 	autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
346 			 (c->inversion == INVERSION_AUTO));
347 
348 	/* setup parameters correctly */
349 	while(!ready) {
350 		/* calculate the lnb_drift */
351 		fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
352 
353 		/* wrap the auto_step if we've exceeded the maximum drift */
354 		if (fepriv->lnb_drift > fepriv->max_drift) {
355 			fepriv->auto_step = 0;
356 			fepriv->auto_sub_step = 0;
357 			fepriv->lnb_drift = 0;
358 		}
359 
360 		/* perform inversion and +/- zigzag */
361 		switch(fepriv->auto_sub_step) {
362 		case 0:
363 			/* try with the current inversion and current drift setting */
364 			ready = 1;
365 			break;
366 
367 		case 1:
368 			if (!autoinversion) break;
369 
370 			fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
371 			ready = 1;
372 			break;
373 
374 		case 2:
375 			if (fepriv->lnb_drift == 0) break;
376 
377 			fepriv->lnb_drift = -fepriv->lnb_drift;
378 			ready = 1;
379 			break;
380 
381 		case 3:
382 			if (fepriv->lnb_drift == 0) break;
383 			if (!autoinversion) break;
384 
385 			fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
386 			fepriv->lnb_drift = -fepriv->lnb_drift;
387 			ready = 1;
388 			break;
389 
390 		default:
391 			fepriv->auto_step++;
392 			fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
393 			break;
394 		}
395 
396 		if (!ready) fepriv->auto_sub_step++;
397 	}
398 
399 	/* if this attempt would hit where we started, indicate a complete
400 	 * iteration has occurred */
401 	if ((fepriv->auto_step == fepriv->started_auto_step) &&
402 	    (fepriv->auto_sub_step == 0) && check_wrapped) {
403 		return 1;
404 	}
405 
406 	dprintk("%s: drift:%i inversion:%i auto_step:%i "
407 		"auto_sub_step:%i started_auto_step:%i\n",
408 		__func__, fepriv->lnb_drift, fepriv->inversion,
409 		fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
410 
411 	/* set the frontend itself */
412 	c->frequency += fepriv->lnb_drift;
413 	if (autoinversion)
414 		c->inversion = fepriv->inversion;
415 	tmp = *c;
416 	if (fe->ops.set_frontend)
417 		fe_set_err = fe->ops.set_frontend(fe);
418 	*c = tmp;
419 	if (fe_set_err < 0) {
420 		fepriv->state = FESTATE_ERROR;
421 		return fe_set_err;
422 	}
423 
424 	c->frequency = original_frequency;
425 	c->inversion = original_inversion;
426 
427 	fepriv->auto_sub_step++;
428 	return 0;
429 }
430 
dvb_frontend_swzigzag(struct dvb_frontend * fe)431 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
432 {
433 	fe_status_t s = 0;
434 	int retval = 0;
435 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
436 	struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
437 
438 	/* if we've got no parameters, just keep idling */
439 	if (fepriv->state & FESTATE_IDLE) {
440 		fepriv->delay = 3*HZ;
441 		fepriv->quality = 0;
442 		return;
443 	}
444 
445 	/* in SCAN mode, we just set the frontend when asked and leave it alone */
446 	if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
447 		if (fepriv->state & FESTATE_RETUNE) {
448 			tmp = *c;
449 			if (fe->ops.set_frontend)
450 				retval = fe->ops.set_frontend(fe);
451 			*c = tmp;
452 			if (retval < 0)
453 				fepriv->state = FESTATE_ERROR;
454 			else
455 				fepriv->state = FESTATE_TUNED;
456 		}
457 		fepriv->delay = 3*HZ;
458 		fepriv->quality = 0;
459 		return;
460 	}
461 
462 	/* get the frontend status */
463 	if (fepriv->state & FESTATE_RETUNE) {
464 		s = 0;
465 	} else {
466 		if (fe->ops.read_status)
467 			fe->ops.read_status(fe, &s);
468 		if (s != fepriv->status) {
469 			dvb_frontend_add_event(fe, s);
470 			fepriv->status = s;
471 		}
472 	}
473 
474 	/* if we're not tuned, and we have a lock, move to the TUNED state */
475 	if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
476 		dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
477 		fepriv->state = FESTATE_TUNED;
478 
479 		/* if we're tuned, then we have determined the correct inversion */
480 		if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
481 		    (c->inversion == INVERSION_AUTO)) {
482 			c->inversion = fepriv->inversion;
483 		}
484 		return;
485 	}
486 
487 	/* if we are tuned already, check we're still locked */
488 	if (fepriv->state & FESTATE_TUNED) {
489 		dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
490 
491 		/* we're tuned, and the lock is still good... */
492 		if (s & FE_HAS_LOCK) {
493 			return;
494 		} else { /* if we _WERE_ tuned, but now don't have a lock */
495 			fepriv->state = FESTATE_ZIGZAG_FAST;
496 			fepriv->started_auto_step = fepriv->auto_step;
497 			fepriv->check_wrapped = 0;
498 		}
499 	}
500 
501 	/* don't actually do anything if we're in the LOSTLOCK state,
502 	 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
503 	if ((fepriv->state & FESTATE_LOSTLOCK) &&
504 	    (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
505 		dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
506 		return;
507 	}
508 
509 	/* don't do anything if we're in the DISEQC state, since this
510 	 * might be someone with a motorized dish controlled by DISEQC.
511 	 * If its actually a re-tune, there will be a SET_FRONTEND soon enough.	*/
512 	if (fepriv->state & FESTATE_DISEQC) {
513 		dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
514 		return;
515 	}
516 
517 	/* if we're in the RETUNE state, set everything up for a brand
518 	 * new scan, keeping the current inversion setting, as the next
519 	 * tune is _very_ likely to require the same */
520 	if (fepriv->state & FESTATE_RETUNE) {
521 		fepriv->lnb_drift = 0;
522 		fepriv->auto_step = 0;
523 		fepriv->auto_sub_step = 0;
524 		fepriv->started_auto_step = 0;
525 		fepriv->check_wrapped = 0;
526 	}
527 
528 	/* fast zigzag. */
529 	if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
530 		fepriv->delay = fepriv->min_delay;
531 
532 		/* perform a tune */
533 		retval = dvb_frontend_swzigzag_autotune(fe,
534 							fepriv->check_wrapped);
535 		if (retval < 0) {
536 			return;
537 		} else if (retval) {
538 			/* OK, if we've run out of trials at the fast speed.
539 			 * Drop back to slow for the _next_ attempt */
540 			fepriv->state = FESTATE_SEARCHING_SLOW;
541 			fepriv->started_auto_step = fepriv->auto_step;
542 			return;
543 		}
544 		fepriv->check_wrapped = 1;
545 
546 		/* if we've just retuned, enter the ZIGZAG_FAST state.
547 		 * This ensures we cannot return from an
548 		 * FE_SET_FRONTEND ioctl before the first frontend tune
549 		 * occurs */
550 		if (fepriv->state & FESTATE_RETUNE) {
551 			fepriv->state = FESTATE_TUNING_FAST;
552 		}
553 	}
554 
555 	/* slow zigzag */
556 	if (fepriv->state & FESTATE_SEARCHING_SLOW) {
557 		dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
558 
559 		/* Note: don't bother checking for wrapping; we stay in this
560 		 * state until we get a lock */
561 		dvb_frontend_swzigzag_autotune(fe, 0);
562 	}
563 }
564 
dvb_frontend_is_exiting(struct dvb_frontend * fe)565 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
566 {
567 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
568 
569 	if (fepriv->exit != DVB_FE_NO_EXIT)
570 		return 1;
571 
572 	if (fepriv->dvbdev->writers == 1)
573 		if (time_after_eq(jiffies, fepriv->release_jiffies +
574 				  dvb_shutdown_timeout * HZ))
575 			return 1;
576 
577 	return 0;
578 }
579 
dvb_frontend_should_wakeup(struct dvb_frontend * fe)580 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
581 {
582 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
583 
584 	if (fepriv->wakeup) {
585 		fepriv->wakeup = 0;
586 		return 1;
587 	}
588 	return dvb_frontend_is_exiting(fe);
589 }
590 
dvb_frontend_wakeup(struct dvb_frontend * fe)591 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
592 {
593 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
594 
595 	fepriv->wakeup = 1;
596 	wake_up_interruptible(&fepriv->wait_queue);
597 }
598 
dvb_frontend_thread(void * data)599 static int dvb_frontend_thread(void *data)
600 {
601 	struct dvb_frontend *fe = data;
602 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
603 	fe_status_t s;
604 	enum dvbfe_algo algo;
605 
606 	bool re_tune = false;
607 
608 	dprintk("%s\n", __func__);
609 
610 	fepriv->check_wrapped = 0;
611 	fepriv->quality = 0;
612 	fepriv->delay = 3*HZ;
613 	fepriv->status = 0;
614 	fepriv->wakeup = 0;
615 	fepriv->reinitialise = 0;
616 
617 	dvb_frontend_init(fe);
618 
619 	set_freezable();
620 	while (1) {
621 		up(&fepriv->sem);	    /* is locked when we enter the thread... */
622 restart:
623 		wait_event_interruptible_timeout(fepriv->wait_queue,
624 			dvb_frontend_should_wakeup(fe) || kthread_should_stop()
625 				|| freezing(current),
626 			fepriv->delay);
627 
628 		if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
629 			/* got signal or quitting */
630 			fepriv->exit = DVB_FE_NORMAL_EXIT;
631 			break;
632 		}
633 
634 		if (try_to_freeze())
635 			goto restart;
636 
637 		if (down_interruptible(&fepriv->sem))
638 			break;
639 
640 		if (fepriv->reinitialise) {
641 			dvb_frontend_init(fe);
642 			if (fe->ops.set_tone && fepriv->tone != -1)
643 				fe->ops.set_tone(fe, fepriv->tone);
644 			if (fe->ops.set_voltage && fepriv->voltage != -1)
645 				fe->ops.set_voltage(fe, fepriv->voltage);
646 			fepriv->reinitialise = 0;
647 		}
648 
649 		/* do an iteration of the tuning loop */
650 		if (fe->ops.get_frontend_algo) {
651 			algo = fe->ops.get_frontend_algo(fe);
652 			switch (algo) {
653 			case DVBFE_ALGO_HW:
654 				dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
655 
656 				if (fepriv->state & FESTATE_RETUNE) {
657 					dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
658 					re_tune = true;
659 					fepriv->state = FESTATE_TUNED;
660 				} else {
661 					re_tune = false;
662 				}
663 
664 				if (fe->ops.tune)
665 					fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
666 
667 				if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
668 					dprintk("%s: state changed, adding current state\n", __func__);
669 					dvb_frontend_add_event(fe, s);
670 					fepriv->status = s;
671 				}
672 				break;
673 			case DVBFE_ALGO_SW:
674 				dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
675 				dvb_frontend_swzigzag(fe);
676 				break;
677 			case DVBFE_ALGO_CUSTOM:
678 				dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
679 				if (fepriv->state & FESTATE_RETUNE) {
680 					dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
681 					fepriv->state = FESTATE_TUNED;
682 				}
683 				/* Case where we are going to search for a carrier
684 				 * User asked us to retune again for some reason, possibly
685 				 * requesting a search with a new set of parameters
686 				 */
687 				if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
688 					if (fe->ops.search) {
689 						fepriv->algo_status = fe->ops.search(fe);
690 						/* We did do a search as was requested, the flags are
691 						 * now unset as well and has the flags wrt to search.
692 						 */
693 					} else {
694 						fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
695 					}
696 				}
697 				/* Track the carrier if the search was successful */
698 				if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
699 					fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
700 					fepriv->delay = HZ / 2;
701 				}
702 				dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
703 				fe->ops.read_status(fe, &s);
704 				if (s != fepriv->status) {
705 					dvb_frontend_add_event(fe, s); /* update event list */
706 					fepriv->status = s;
707 					if (!(s & FE_HAS_LOCK)) {
708 						fepriv->delay = HZ / 10;
709 						fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
710 					} else {
711 						fepriv->delay = 60 * HZ;
712 					}
713 				}
714 				break;
715 			default:
716 				dprintk("%s: UNDEFINED ALGO !\n", __func__);
717 				break;
718 			}
719 		} else {
720 			dvb_frontend_swzigzag(fe);
721 		}
722 	}
723 
724 	if (dvb_powerdown_on_sleep) {
725 		if (fe->ops.set_voltage)
726 			fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
727 		if (fe->ops.tuner_ops.sleep) {
728 			if (fe->ops.i2c_gate_ctrl)
729 				fe->ops.i2c_gate_ctrl(fe, 1);
730 			fe->ops.tuner_ops.sleep(fe);
731 			if (fe->ops.i2c_gate_ctrl)
732 				fe->ops.i2c_gate_ctrl(fe, 0);
733 		}
734 		if (fe->ops.sleep)
735 			fe->ops.sleep(fe);
736 	}
737 
738 	fepriv->thread = NULL;
739 	if (kthread_should_stop())
740 		fepriv->exit = DVB_FE_DEVICE_REMOVED;
741 	else
742 		fepriv->exit = DVB_FE_NO_EXIT;
743 	mb();
744 
745 	dvb_frontend_wakeup(fe);
746 	return 0;
747 }
748 
dvb_frontend_stop(struct dvb_frontend * fe)749 static void dvb_frontend_stop(struct dvb_frontend *fe)
750 {
751 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
752 
753 	dprintk ("%s\n", __func__);
754 
755 	fepriv->exit = DVB_FE_NORMAL_EXIT;
756 	mb();
757 
758 	if (!fepriv->thread)
759 		return;
760 
761 	kthread_stop(fepriv->thread);
762 
763 	sema_init(&fepriv->sem, 1);
764 	fepriv->state = FESTATE_IDLE;
765 
766 	/* paranoia check in case a signal arrived */
767 	if (fepriv->thread)
768 		printk("dvb_frontend_stop: warning: thread %p won't exit\n",
769 				fepriv->thread);
770 }
771 
timeval_usec_diff(struct timeval lasttime,struct timeval curtime)772 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
773 {
774 	return ((curtime.tv_usec < lasttime.tv_usec) ?
775 		1000000 - lasttime.tv_usec + curtime.tv_usec :
776 		curtime.tv_usec - lasttime.tv_usec);
777 }
778 EXPORT_SYMBOL(timeval_usec_diff);
779 
timeval_usec_add(struct timeval * curtime,u32 add_usec)780 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
781 {
782 	curtime->tv_usec += add_usec;
783 	if (curtime->tv_usec >= 1000000) {
784 		curtime->tv_usec -= 1000000;
785 		curtime->tv_sec++;
786 	}
787 }
788 
789 /*
790  * Sleep until gettimeofday() > waketime + add_usec
791  * This needs to be as precise as possible, but as the delay is
792  * usually between 2ms and 32ms, it is done using a scheduled msleep
793  * followed by usleep (normally a busy-wait loop) for the remainder
794  */
dvb_frontend_sleep_until(struct timeval * waketime,u32 add_usec)795 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
796 {
797 	struct timeval lasttime;
798 	s32 delta, newdelta;
799 
800 	timeval_usec_add(waketime, add_usec);
801 
802 	do_gettimeofday(&lasttime);
803 	delta = timeval_usec_diff(lasttime, *waketime);
804 	if (delta > 2500) {
805 		msleep((delta - 1500) / 1000);
806 		do_gettimeofday(&lasttime);
807 		newdelta = timeval_usec_diff(lasttime, *waketime);
808 		delta = (newdelta > delta) ? 0 : newdelta;
809 	}
810 	if (delta > 0)
811 		udelay(delta);
812 }
813 EXPORT_SYMBOL(dvb_frontend_sleep_until);
814 
dvb_frontend_start(struct dvb_frontend * fe)815 static int dvb_frontend_start(struct dvb_frontend *fe)
816 {
817 	int ret;
818 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
819 	struct task_struct *fe_thread;
820 
821 	dprintk ("%s\n", __func__);
822 
823 	if (fepriv->thread) {
824 		if (fepriv->exit == DVB_FE_NO_EXIT)
825 			return 0;
826 		else
827 			dvb_frontend_stop (fe);
828 	}
829 
830 	if (signal_pending(current))
831 		return -EINTR;
832 	if (down_interruptible (&fepriv->sem))
833 		return -EINTR;
834 
835 	fepriv->state = FESTATE_IDLE;
836 	fepriv->exit = DVB_FE_NO_EXIT;
837 	fepriv->thread = NULL;
838 	mb();
839 
840 	fe_thread = kthread_run(dvb_frontend_thread, fe,
841 		"kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
842 	if (IS_ERR(fe_thread)) {
843 		ret = PTR_ERR(fe_thread);
844 		printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
845 		up(&fepriv->sem);
846 		return ret;
847 	}
848 	fepriv->thread = fe_thread;
849 	return 0;
850 }
851 
dvb_frontend_get_frequency_limits(struct dvb_frontend * fe,u32 * freq_min,u32 * freq_max)852 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
853 					u32 *freq_min, u32 *freq_max)
854 {
855 	*freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
856 
857 	if (fe->ops.info.frequency_max == 0)
858 		*freq_max = fe->ops.tuner_ops.info.frequency_max;
859 	else if (fe->ops.tuner_ops.info.frequency_max == 0)
860 		*freq_max = fe->ops.info.frequency_max;
861 	else
862 		*freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
863 
864 	if (*freq_min == 0 || *freq_max == 0)
865 		printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
866 		       fe->dvb->num,fe->id);
867 }
868 
dvb_frontend_check_parameters(struct dvb_frontend * fe)869 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
870 {
871 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
872 	u32 freq_min;
873 	u32 freq_max;
874 
875 	/* range check: frequency */
876 	dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
877 	if ((freq_min && c->frequency < freq_min) ||
878 	    (freq_max && c->frequency > freq_max)) {
879 		printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
880 		       fe->dvb->num, fe->id, c->frequency, freq_min, freq_max);
881 		return -EINVAL;
882 	}
883 
884 	/* range check: symbol rate */
885 	switch (c->delivery_system) {
886 	case SYS_DVBS:
887 	case SYS_DVBS2:
888 	case SYS_TURBO:
889 	case SYS_DVBC_ANNEX_A:
890 	case SYS_DVBC_ANNEX_C:
891 		if ((fe->ops.info.symbol_rate_min &&
892 		     c->symbol_rate < fe->ops.info.symbol_rate_min) ||
893 		    (fe->ops.info.symbol_rate_max &&
894 		     c->symbol_rate > fe->ops.info.symbol_rate_max)) {
895 			printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
896 			       fe->dvb->num, fe->id, c->symbol_rate,
897 			       fe->ops.info.symbol_rate_min,
898 			       fe->ops.info.symbol_rate_max);
899 			return -EINVAL;
900 		}
901 	default:
902 		break;
903 	}
904 
905 	return 0;
906 }
907 
dvb_frontend_clear_cache(struct dvb_frontend * fe)908 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
909 {
910 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
911 	int i;
912 	u32 delsys;
913 
914 	delsys = c->delivery_system;
915 	memset(c, 0, sizeof(struct dtv_frontend_properties));
916 	c->delivery_system = delsys;
917 
918 	c->state = DTV_CLEAR;
919 
920 	dprintk("%s() Clearing cache for delivery system %d\n", __func__,
921 		c->delivery_system);
922 
923 	c->transmission_mode = TRANSMISSION_MODE_AUTO;
924 	c->bandwidth_hz = 0;	/* AUTO */
925 	c->guard_interval = GUARD_INTERVAL_AUTO;
926 	c->hierarchy = HIERARCHY_AUTO;
927 	c->symbol_rate = 0;
928 	c->code_rate_HP = FEC_AUTO;
929 	c->code_rate_LP = FEC_AUTO;
930 	c->fec_inner = FEC_AUTO;
931 	c->rolloff = ROLLOFF_AUTO;
932 	c->voltage = SEC_VOLTAGE_OFF;
933 	c->sectone = SEC_TONE_OFF;
934 	c->pilot = PILOT_AUTO;
935 
936 	c->isdbt_partial_reception = 0;
937 	c->isdbt_sb_mode = 0;
938 	c->isdbt_sb_subchannel = 0;
939 	c->isdbt_sb_segment_idx = 0;
940 	c->isdbt_sb_segment_count = 0;
941 	c->isdbt_layer_enabled = 0;
942 	for (i = 0; i < 3; i++) {
943 		c->layer[i].fec = FEC_AUTO;
944 		c->layer[i].modulation = QAM_AUTO;
945 		c->layer[i].interleaving = 0;
946 		c->layer[i].segment_count = 0;
947 	}
948 
949 	c->isdbs_ts_id = 0;
950 	c->dvbt2_plp_id = 0;
951 
952 	switch (c->delivery_system) {
953 	case SYS_DVBS:
954 	case SYS_DVBS2:
955 	case SYS_TURBO:
956 		c->modulation = QPSK;   /* implied for DVB-S in legacy API */
957 		c->rolloff = ROLLOFF_35;/* implied for DVB-S */
958 		break;
959 	case SYS_ATSC:
960 		c->modulation = VSB_8;
961 		break;
962 	default:
963 		c->modulation = QAM_AUTO;
964 		break;
965 	}
966 
967 	return 0;
968 }
969 
970 #define _DTV_CMD(n, s, b) \
971 [n] = { \
972 	.name = #n, \
973 	.cmd  = n, \
974 	.set  = s,\
975 	.buffer = b \
976 }
977 
978 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
979 	_DTV_CMD(DTV_TUNE, 1, 0),
980 	_DTV_CMD(DTV_CLEAR, 1, 0),
981 
982 	/* Set */
983 	_DTV_CMD(DTV_FREQUENCY, 1, 0),
984 	_DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
985 	_DTV_CMD(DTV_MODULATION, 1, 0),
986 	_DTV_CMD(DTV_INVERSION, 1, 0),
987 	_DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
988 	_DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
989 	_DTV_CMD(DTV_INNER_FEC, 1, 0),
990 	_DTV_CMD(DTV_VOLTAGE, 1, 0),
991 	_DTV_CMD(DTV_TONE, 1, 0),
992 	_DTV_CMD(DTV_PILOT, 1, 0),
993 	_DTV_CMD(DTV_ROLLOFF, 1, 0),
994 	_DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
995 	_DTV_CMD(DTV_HIERARCHY, 1, 0),
996 	_DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
997 	_DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
998 	_DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
999 	_DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1000 
1001 	_DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1002 	_DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1003 	_DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1004 	_DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1005 	_DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1006 	_DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1007 	_DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1008 	_DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1009 	_DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1010 	_DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1011 	_DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1012 	_DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1013 	_DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1014 	_DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1015 	_DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1016 	_DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1017 	_DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1018 	_DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1019 
1020 	_DTV_CMD(DTV_ISDBS_TS_ID, 1, 0),
1021 	_DTV_CMD(DTV_DVBT2_PLP_ID, 1, 0),
1022 
1023 	/* Get */
1024 	_DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1025 	_DTV_CMD(DTV_API_VERSION, 0, 0),
1026 	_DTV_CMD(DTV_CODE_RATE_HP, 0, 0),
1027 	_DTV_CMD(DTV_CODE_RATE_LP, 0, 0),
1028 	_DTV_CMD(DTV_GUARD_INTERVAL, 0, 0),
1029 	_DTV_CMD(DTV_TRANSMISSION_MODE, 0, 0),
1030 	_DTV_CMD(DTV_HIERARCHY, 0, 0),
1031 
1032 	_DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1033 };
1034 
dtv_property_dump(struct dtv_property * tvp)1035 static void dtv_property_dump(struct dtv_property *tvp)
1036 {
1037 	int i;
1038 
1039 	if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1040 		printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
1041 			__func__, tvp->cmd);
1042 		return;
1043 	}
1044 
1045 	dprintk("%s() tvp.cmd    = 0x%08x (%s)\n"
1046 		,__func__
1047 		,tvp->cmd
1048 		,dtv_cmds[ tvp->cmd ].name);
1049 
1050 	if(dtv_cmds[ tvp->cmd ].buffer) {
1051 
1052 		dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1053 			,__func__
1054 			,tvp->u.buffer.len);
1055 
1056 		for(i = 0; i < tvp->u.buffer.len; i++)
1057 			dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1058 				,__func__
1059 				,i
1060 				,tvp->u.buffer.data[i]);
1061 
1062 	} else
1063 		dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
1064 }
1065 
1066 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1067  * drivers can use a single set_frontend tuning function, regardless of whether
1068  * it's being used for the legacy or new API, reducing code and complexity.
1069  */
dtv_property_cache_sync(struct dvb_frontend * fe,struct dtv_frontend_properties * c,const struct dvb_frontend_parameters * p)1070 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1071 				   struct dtv_frontend_properties *c,
1072 				   const struct dvb_frontend_parameters *p)
1073 {
1074 	c->frequency = p->frequency;
1075 	c->inversion = p->inversion;
1076 
1077 	switch (dvbv3_type(c->delivery_system)) {
1078 	case DVBV3_QPSK:
1079 		dprintk("%s() Preparing QPSK req\n", __func__);
1080 		c->symbol_rate = p->u.qpsk.symbol_rate;
1081 		c->fec_inner = p->u.qpsk.fec_inner;
1082 		break;
1083 	case DVBV3_QAM:
1084 		dprintk("%s() Preparing QAM req\n", __func__);
1085 		c->symbol_rate = p->u.qam.symbol_rate;
1086 		c->fec_inner = p->u.qam.fec_inner;
1087 		c->modulation = p->u.qam.modulation;
1088 		break;
1089 	case DVBV3_OFDM:
1090 		dprintk("%s() Preparing OFDM req\n", __func__);
1091 		switch (p->u.ofdm.bandwidth) {
1092 		case BANDWIDTH_10_MHZ:
1093 			c->bandwidth_hz = 10000000;
1094 			break;
1095 		case BANDWIDTH_8_MHZ:
1096 			c->bandwidth_hz = 8000000;
1097 			break;
1098 		case BANDWIDTH_7_MHZ:
1099 			c->bandwidth_hz = 7000000;
1100 			break;
1101 		case BANDWIDTH_6_MHZ:
1102 			c->bandwidth_hz = 6000000;
1103 			break;
1104 		case BANDWIDTH_5_MHZ:
1105 			c->bandwidth_hz = 5000000;
1106 			break;
1107 		case BANDWIDTH_1_712_MHZ:
1108 			c->bandwidth_hz = 1712000;
1109 			break;
1110 		case BANDWIDTH_AUTO:
1111 			c->bandwidth_hz = 0;
1112 		}
1113 
1114 		c->code_rate_HP = p->u.ofdm.code_rate_HP;
1115 		c->code_rate_LP = p->u.ofdm.code_rate_LP;
1116 		c->modulation = p->u.ofdm.constellation;
1117 		c->transmission_mode = p->u.ofdm.transmission_mode;
1118 		c->guard_interval = p->u.ofdm.guard_interval;
1119 		c->hierarchy = p->u.ofdm.hierarchy_information;
1120 		break;
1121 	case DVBV3_ATSC:
1122 		dprintk("%s() Preparing ATSC req\n", __func__);
1123 		c->modulation = p->u.vsb.modulation;
1124 		if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1125 			c->delivery_system = SYS_ATSC;
1126 		else
1127 			c->delivery_system = SYS_DVBC_ANNEX_B;
1128 		break;
1129 	case DVBV3_UNKNOWN:
1130 		printk(KERN_ERR
1131 		       "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1132 		       __func__, c->delivery_system);
1133 		return -EINVAL;
1134 	}
1135 
1136 	return 0;
1137 }
1138 
1139 /* Ensure the cached values are set correctly in the frontend
1140  * legacy tuning structures, for the advanced tuning API.
1141  */
dtv_property_legacy_params_sync(struct dvb_frontend * fe,struct dvb_frontend_parameters * p)1142 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1143 					    struct dvb_frontend_parameters *p)
1144 {
1145 	const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1146 
1147 	p->frequency = c->frequency;
1148 	p->inversion = c->inversion;
1149 
1150 	switch (dvbv3_type(c->delivery_system)) {
1151 	case DVBV3_UNKNOWN:
1152 		printk(KERN_ERR
1153 		       "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1154 		       __func__, c->delivery_system);
1155 		return -EINVAL;
1156 	case DVBV3_QPSK:
1157 		dprintk("%s() Preparing QPSK req\n", __func__);
1158 		p->u.qpsk.symbol_rate = c->symbol_rate;
1159 		p->u.qpsk.fec_inner = c->fec_inner;
1160 		break;
1161 	case DVBV3_QAM:
1162 		dprintk("%s() Preparing QAM req\n", __func__);
1163 		p->u.qam.symbol_rate = c->symbol_rate;
1164 		p->u.qam.fec_inner = c->fec_inner;
1165 		p->u.qam.modulation = c->modulation;
1166 		break;
1167 	case DVBV3_OFDM:
1168 		dprintk("%s() Preparing OFDM req\n", __func__);
1169 
1170 		switch (c->bandwidth_hz) {
1171 		case 10000000:
1172 			p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1173 			break;
1174 		case 8000000:
1175 			p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1176 			break;
1177 		case 7000000:
1178 			p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1179 			break;
1180 		case 6000000:
1181 			p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1182 			break;
1183 		case 5000000:
1184 			p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1185 			break;
1186 		case 1712000:
1187 			p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1188 			break;
1189 		case 0:
1190 		default:
1191 			p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1192 		}
1193 		p->u.ofdm.code_rate_HP = c->code_rate_HP;
1194 		p->u.ofdm.code_rate_LP = c->code_rate_LP;
1195 		p->u.ofdm.constellation = c->modulation;
1196 		p->u.ofdm.transmission_mode = c->transmission_mode;
1197 		p->u.ofdm.guard_interval = c->guard_interval;
1198 		p->u.ofdm.hierarchy_information = c->hierarchy;
1199 		break;
1200 	case DVBV3_ATSC:
1201 		dprintk("%s() Preparing VSB req\n", __func__);
1202 		p->u.vsb.modulation = c->modulation;
1203 		break;
1204 	}
1205 	return 0;
1206 }
1207 
1208 /**
1209  * dtv_get_frontend - calls a callback for retrieving DTV parameters
1210  * @fe:		struct dvb_frontend pointer
1211  * @c:		struct dtv_frontend_properties pointer (DVBv5 cache)
1212  * @p_out	struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1213  *
1214  * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1215  * If c is not null, it will update the DVBv5 cache struct pointed by it.
1216  * If p_out is not null, it will update the DVBv3 params pointed by it.
1217  */
dtv_get_frontend(struct dvb_frontend * fe,struct dvb_frontend_parameters * p_out)1218 static int dtv_get_frontend(struct dvb_frontend *fe,
1219 			    struct dvb_frontend_parameters *p_out)
1220 {
1221 	int r;
1222 
1223 	if (fe->ops.get_frontend) {
1224 		r = fe->ops.get_frontend(fe);
1225 		if (unlikely(r < 0))
1226 			return r;
1227 		if (p_out)
1228 			dtv_property_legacy_params_sync(fe, p_out);
1229 		return 0;
1230 	}
1231 
1232 	/* As everything is in cache, get_frontend fops are always supported */
1233 	return 0;
1234 }
1235 
1236 static int dvb_frontend_ioctl_legacy(struct file *file,
1237 			unsigned int cmd, void *parg);
1238 static int dvb_frontend_ioctl_properties(struct file *file,
1239 			unsigned int cmd, void *parg);
1240 
dtv_property_process_get(struct dvb_frontend * fe,const struct dtv_frontend_properties * c,struct dtv_property * tvp,struct file * file)1241 static int dtv_property_process_get(struct dvb_frontend *fe,
1242 				    const struct dtv_frontend_properties *c,
1243 				    struct dtv_property *tvp,
1244 				    struct file *file)
1245 {
1246 	int r, ncaps;
1247 
1248 	switch(tvp->cmd) {
1249 	case DTV_ENUM_DELSYS:
1250 		ncaps = 0;
1251 		while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1252 			tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1253 			ncaps++;
1254 		}
1255 		tvp->u.buffer.len = ncaps;
1256 		break;
1257 	case DTV_FREQUENCY:
1258 		tvp->u.data = c->frequency;
1259 		break;
1260 	case DTV_MODULATION:
1261 		tvp->u.data = c->modulation;
1262 		break;
1263 	case DTV_BANDWIDTH_HZ:
1264 		tvp->u.data = c->bandwidth_hz;
1265 		break;
1266 	case DTV_INVERSION:
1267 		tvp->u.data = c->inversion;
1268 		break;
1269 	case DTV_SYMBOL_RATE:
1270 		tvp->u.data = c->symbol_rate;
1271 		break;
1272 	case DTV_INNER_FEC:
1273 		tvp->u.data = c->fec_inner;
1274 		break;
1275 	case DTV_PILOT:
1276 		tvp->u.data = c->pilot;
1277 		break;
1278 	case DTV_ROLLOFF:
1279 		tvp->u.data = c->rolloff;
1280 		break;
1281 	case DTV_DELIVERY_SYSTEM:
1282 		tvp->u.data = c->delivery_system;
1283 		break;
1284 	case DTV_VOLTAGE:
1285 		tvp->u.data = c->voltage;
1286 		break;
1287 	case DTV_TONE:
1288 		tvp->u.data = c->sectone;
1289 		break;
1290 	case DTV_API_VERSION:
1291 		tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1292 		break;
1293 	case DTV_CODE_RATE_HP:
1294 		tvp->u.data = c->code_rate_HP;
1295 		break;
1296 	case DTV_CODE_RATE_LP:
1297 		tvp->u.data = c->code_rate_LP;
1298 		break;
1299 	case DTV_GUARD_INTERVAL:
1300 		tvp->u.data = c->guard_interval;
1301 		break;
1302 	case DTV_TRANSMISSION_MODE:
1303 		tvp->u.data = c->transmission_mode;
1304 		break;
1305 	case DTV_HIERARCHY:
1306 		tvp->u.data = c->hierarchy;
1307 		break;
1308 
1309 	/* ISDB-T Support here */
1310 	case DTV_ISDBT_PARTIAL_RECEPTION:
1311 		tvp->u.data = c->isdbt_partial_reception;
1312 		break;
1313 	case DTV_ISDBT_SOUND_BROADCASTING:
1314 		tvp->u.data = c->isdbt_sb_mode;
1315 		break;
1316 	case DTV_ISDBT_SB_SUBCHANNEL_ID:
1317 		tvp->u.data = c->isdbt_sb_subchannel;
1318 		break;
1319 	case DTV_ISDBT_SB_SEGMENT_IDX:
1320 		tvp->u.data = c->isdbt_sb_segment_idx;
1321 		break;
1322 	case DTV_ISDBT_SB_SEGMENT_COUNT:
1323 		tvp->u.data = c->isdbt_sb_segment_count;
1324 		break;
1325 	case DTV_ISDBT_LAYER_ENABLED:
1326 		tvp->u.data = c->isdbt_layer_enabled;
1327 		break;
1328 	case DTV_ISDBT_LAYERA_FEC:
1329 		tvp->u.data = c->layer[0].fec;
1330 		break;
1331 	case DTV_ISDBT_LAYERA_MODULATION:
1332 		tvp->u.data = c->layer[0].modulation;
1333 		break;
1334 	case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1335 		tvp->u.data = c->layer[0].segment_count;
1336 		break;
1337 	case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1338 		tvp->u.data = c->layer[0].interleaving;
1339 		break;
1340 	case DTV_ISDBT_LAYERB_FEC:
1341 		tvp->u.data = c->layer[1].fec;
1342 		break;
1343 	case DTV_ISDBT_LAYERB_MODULATION:
1344 		tvp->u.data = c->layer[1].modulation;
1345 		break;
1346 	case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1347 		tvp->u.data = c->layer[1].segment_count;
1348 		break;
1349 	case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1350 		tvp->u.data = c->layer[1].interleaving;
1351 		break;
1352 	case DTV_ISDBT_LAYERC_FEC:
1353 		tvp->u.data = c->layer[2].fec;
1354 		break;
1355 	case DTV_ISDBT_LAYERC_MODULATION:
1356 		tvp->u.data = c->layer[2].modulation;
1357 		break;
1358 	case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1359 		tvp->u.data = c->layer[2].segment_count;
1360 		break;
1361 	case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1362 		tvp->u.data = c->layer[2].interleaving;
1363 		break;
1364 	case DTV_ISDBS_TS_ID:
1365 		tvp->u.data = c->isdbs_ts_id;
1366 		break;
1367 	case DTV_DVBT2_PLP_ID:
1368 		tvp->u.data = c->dvbt2_plp_id;
1369 		break;
1370 	default:
1371 		return -EINVAL;
1372 	}
1373 
1374 	/* Allow the frontend to override outgoing properties */
1375 	if (fe->ops.get_property) {
1376 		r = fe->ops.get_property(fe, tvp);
1377 		if (r < 0)
1378 			return r;
1379 	}
1380 
1381 	dtv_property_dump(tvp);
1382 
1383 	return 0;
1384 }
1385 
1386 static int dtv_set_frontend(struct dvb_frontend *fe);
1387 
is_dvbv3_delsys(u32 delsys)1388 static bool is_dvbv3_delsys(u32 delsys)
1389 {
1390 	bool status;
1391 
1392 	status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1393 		 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1394 
1395 	return status;
1396 }
1397 
set_delivery_system(struct dvb_frontend * fe,u32 desired_system)1398 static int set_delivery_system(struct dvb_frontend *fe, u32 desired_system)
1399 {
1400 	int ncaps, i;
1401 	u32 delsys = SYS_UNDEFINED;
1402 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1403 	enum dvbv3_emulation_type type;
1404 
1405 	/*
1406 	 * It was reported that some old DVBv5 applications were
1407 	 * filling delivery_system with SYS_UNDEFINED. If this happens,
1408 	 * assume that the application wants to use the first supported
1409 	 * delivery system.
1410 	 */
1411 	if (c->delivery_system == SYS_UNDEFINED)
1412 	        c->delivery_system = fe->ops.delsys[0];
1413 
1414 	if (desired_system == SYS_UNDEFINED) {
1415 		/*
1416 		 * A DVBv3 call doesn't know what's the desired system.
1417 		 * Also, DVBv3 applications don't know that ops.info->type
1418 		 * could be changed, and they simply dies when it doesn't
1419 		 * match.
1420 		 * So, don't change the current delivery system, as it
1421 		 * may be trying to do the wrong thing, like setting an
1422 		 * ISDB-T frontend as DVB-T. Instead, find the closest
1423 		 * DVBv3 system that matches the delivery system.
1424 		 */
1425 		if (is_dvbv3_delsys(c->delivery_system)) {
1426 			dprintk("%s() Using delivery system to %d\n",
1427 				__func__, c->delivery_system);
1428 			return 0;
1429 		}
1430 		type = dvbv3_type(c->delivery_system);
1431 		switch (type) {
1432 		case DVBV3_QPSK:
1433 			desired_system = SYS_DVBS;
1434 			break;
1435 		case DVBV3_QAM:
1436 			desired_system = SYS_DVBC_ANNEX_A;
1437 			break;
1438 		case DVBV3_ATSC:
1439 			desired_system = SYS_ATSC;
1440 			break;
1441 		case DVBV3_OFDM:
1442 			desired_system = SYS_DVBT;
1443 			break;
1444 		default:
1445 			dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
1446 				__func__);
1447 			return -EINVAL;
1448 		}
1449 		/*
1450 		 * Get a delivery system that is compatible with DVBv3
1451 		 * NOTE: in order for this to work with softwares like Kaffeine that
1452 		 *	uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1453 		 *	DVB-S, drivers that support both should put the SYS_DVBS entry
1454 		 *	before the SYS_DVBS2, otherwise it won't switch back to DVB-S.
1455 		 *	The real fix is that userspace applications should not use DVBv3
1456 		 *	and not trust on calling FE_SET_FRONTEND to switch the delivery
1457 		 *	system.
1458 		 */
1459 		ncaps = 0;
1460 		while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1461 			if (fe->ops.delsys[ncaps] == desired_system) {
1462 				delsys = desired_system;
1463 				break;
1464 			}
1465 			ncaps++;
1466 		}
1467 		if (delsys == SYS_UNDEFINED) {
1468 			dprintk("%s() Couldn't find a delivery system that matches %d\n",
1469 				__func__, desired_system);
1470 		}
1471 	} else {
1472 		/*
1473 		 * This is a DVBv5 call. So, it likely knows the supported
1474 		 * delivery systems.
1475 		 */
1476 
1477 		/* Check if the desired delivery system is supported */
1478 		ncaps = 0;
1479 		while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1480 			if (fe->ops.delsys[ncaps] == desired_system) {
1481 				c->delivery_system = desired_system;
1482 				dprintk("%s() Changing delivery system to %d\n",
1483 					__func__, desired_system);
1484 				return 0;
1485 			}
1486 			ncaps++;
1487 		}
1488 		type = dvbv3_type(desired_system);
1489 
1490 		/*
1491 		 * The delivery system is not supported. See if it can be
1492 		 * emulated.
1493 		 * The emulation only works if the desired system is one of the
1494 		 * DVBv3 delivery systems
1495 		 */
1496 		if (!is_dvbv3_delsys(desired_system)) {
1497 			dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1498 				__func__);
1499 			return -EINVAL;
1500 		}
1501 
1502 		/*
1503 		 * Get the last non-DVBv3 delivery system that has the same type
1504 		 * of the desired system
1505 		 */
1506 		ncaps = 0;
1507 		while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1508 			if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
1509 			    !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
1510 				delsys = fe->ops.delsys[ncaps];
1511 			ncaps++;
1512 		}
1513 		/* There's nothing compatible with the desired delivery system */
1514 		if (delsys == SYS_UNDEFINED) {
1515 			dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1516 				__func__);
1517 			return -EINVAL;
1518 		}
1519 	}
1520 
1521 	c->delivery_system = delsys;
1522 
1523 	/*
1524 	 * The DVBv3 or DVBv5 call is requesting a different system. So,
1525 	 * emulation is needed.
1526 	 *
1527 	 * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
1528 	 * for older DVBv5 applications. The emulation will try to use
1529 	 * the auto mode for most things, and will assume that the desired
1530 	 * delivery system is the last one at the ops.delsys[] array
1531 	 */
1532 	dprintk("%s() Using delivery system %d emulated as if it were a %d\n",
1533 		__func__, delsys, desired_system);
1534 
1535 	/*
1536 	 * For now, handles ISDB-T calls. More code may be needed here for the
1537 	 * other emulated stuff
1538 	 */
1539 	if (type == DVBV3_OFDM) {
1540 		if (c->delivery_system == SYS_ISDBT) {
1541 			dprintk("%s() Using defaults for SYS_ISDBT\n",
1542 				__func__);
1543 			if (!c->bandwidth_hz)
1544 				c->bandwidth_hz = 6000000;
1545 
1546 			c->isdbt_partial_reception = 0;
1547 			c->isdbt_sb_mode = 0;
1548 			c->isdbt_sb_subchannel = 0;
1549 			c->isdbt_sb_segment_idx = 0;
1550 			c->isdbt_sb_segment_count = 0;
1551 			c->isdbt_layer_enabled = 0;
1552 			for (i = 0; i < 3; i++) {
1553 				c->layer[i].fec = FEC_AUTO;
1554 				c->layer[i].modulation = QAM_AUTO;
1555 				c->layer[i].interleaving = 0;
1556 				c->layer[i].segment_count = 0;
1557 			}
1558 		}
1559 	}
1560 	dprintk("change delivery system on cache to %d\n", c->delivery_system);
1561 
1562 	return 0;
1563 }
1564 
dtv_property_process_set(struct dvb_frontend * fe,struct dtv_property * tvp,struct file * file)1565 static int dtv_property_process_set(struct dvb_frontend *fe,
1566 				    struct dtv_property *tvp,
1567 				    struct file *file)
1568 {
1569 	int r = 0;
1570 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1571 
1572 	/* Allow the frontend to validate incoming properties */
1573 	if (fe->ops.set_property) {
1574 		r = fe->ops.set_property(fe, tvp);
1575 		if (r < 0)
1576 			return r;
1577 	}
1578 
1579 	switch(tvp->cmd) {
1580 	case DTV_CLEAR:
1581 		/*
1582 		 * Reset a cache of data specific to the frontend here. This does
1583 		 * not effect hardware.
1584 		 */
1585 		dvb_frontend_clear_cache(fe);
1586 		break;
1587 	case DTV_TUNE:
1588 		/* interpret the cache of data, build either a traditional frontend
1589 		 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1590 		 * ioctl.
1591 		 */
1592 		c->state = tvp->cmd;
1593 		dprintk("%s() Finalised property cache\n", __func__);
1594 
1595 		r = dtv_set_frontend(fe);
1596 		break;
1597 	case DTV_FREQUENCY:
1598 		c->frequency = tvp->u.data;
1599 		break;
1600 	case DTV_MODULATION:
1601 		c->modulation = tvp->u.data;
1602 		break;
1603 	case DTV_BANDWIDTH_HZ:
1604 		c->bandwidth_hz = tvp->u.data;
1605 		break;
1606 	case DTV_INVERSION:
1607 		c->inversion = tvp->u.data;
1608 		break;
1609 	case DTV_SYMBOL_RATE:
1610 		c->symbol_rate = tvp->u.data;
1611 		break;
1612 	case DTV_INNER_FEC:
1613 		c->fec_inner = tvp->u.data;
1614 		break;
1615 	case DTV_PILOT:
1616 		c->pilot = tvp->u.data;
1617 		break;
1618 	case DTV_ROLLOFF:
1619 		c->rolloff = tvp->u.data;
1620 		break;
1621 	case DTV_DELIVERY_SYSTEM:
1622 		r = set_delivery_system(fe, tvp->u.data);
1623 		break;
1624 	case DTV_VOLTAGE:
1625 		c->voltage = tvp->u.data;
1626 		r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1627 			(void *)c->voltage);
1628 		break;
1629 	case DTV_TONE:
1630 		c->sectone = tvp->u.data;
1631 		r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1632 			(void *)c->sectone);
1633 		break;
1634 	case DTV_CODE_RATE_HP:
1635 		c->code_rate_HP = tvp->u.data;
1636 		break;
1637 	case DTV_CODE_RATE_LP:
1638 		c->code_rate_LP = tvp->u.data;
1639 		break;
1640 	case DTV_GUARD_INTERVAL:
1641 		c->guard_interval = tvp->u.data;
1642 		break;
1643 	case DTV_TRANSMISSION_MODE:
1644 		c->transmission_mode = tvp->u.data;
1645 		break;
1646 	case DTV_HIERARCHY:
1647 		c->hierarchy = tvp->u.data;
1648 		break;
1649 
1650 	/* ISDB-T Support here */
1651 	case DTV_ISDBT_PARTIAL_RECEPTION:
1652 		c->isdbt_partial_reception = tvp->u.data;
1653 		break;
1654 	case DTV_ISDBT_SOUND_BROADCASTING:
1655 		c->isdbt_sb_mode = tvp->u.data;
1656 		break;
1657 	case DTV_ISDBT_SB_SUBCHANNEL_ID:
1658 		c->isdbt_sb_subchannel = tvp->u.data;
1659 		break;
1660 	case DTV_ISDBT_SB_SEGMENT_IDX:
1661 		c->isdbt_sb_segment_idx = tvp->u.data;
1662 		break;
1663 	case DTV_ISDBT_SB_SEGMENT_COUNT:
1664 		c->isdbt_sb_segment_count = tvp->u.data;
1665 		break;
1666 	case DTV_ISDBT_LAYER_ENABLED:
1667 		c->isdbt_layer_enabled = tvp->u.data;
1668 		break;
1669 	case DTV_ISDBT_LAYERA_FEC:
1670 		c->layer[0].fec = tvp->u.data;
1671 		break;
1672 	case DTV_ISDBT_LAYERA_MODULATION:
1673 		c->layer[0].modulation = tvp->u.data;
1674 		break;
1675 	case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1676 		c->layer[0].segment_count = tvp->u.data;
1677 		break;
1678 	case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1679 		c->layer[0].interleaving = tvp->u.data;
1680 		break;
1681 	case DTV_ISDBT_LAYERB_FEC:
1682 		c->layer[1].fec = tvp->u.data;
1683 		break;
1684 	case DTV_ISDBT_LAYERB_MODULATION:
1685 		c->layer[1].modulation = tvp->u.data;
1686 		break;
1687 	case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1688 		c->layer[1].segment_count = tvp->u.data;
1689 		break;
1690 	case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1691 		c->layer[1].interleaving = tvp->u.data;
1692 		break;
1693 	case DTV_ISDBT_LAYERC_FEC:
1694 		c->layer[2].fec = tvp->u.data;
1695 		break;
1696 	case DTV_ISDBT_LAYERC_MODULATION:
1697 		c->layer[2].modulation = tvp->u.data;
1698 		break;
1699 	case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1700 		c->layer[2].segment_count = tvp->u.data;
1701 		break;
1702 	case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1703 		c->layer[2].interleaving = tvp->u.data;
1704 		break;
1705 	case DTV_ISDBS_TS_ID:
1706 		c->isdbs_ts_id = tvp->u.data;
1707 		break;
1708 	case DTV_DVBT2_PLP_ID:
1709 		c->dvbt2_plp_id = tvp->u.data;
1710 		break;
1711 	default:
1712 		return -EINVAL;
1713 	}
1714 
1715 	return r;
1716 }
1717 
dvb_frontend_ioctl(struct file * file,unsigned int cmd,void * parg)1718 static int dvb_frontend_ioctl(struct file *file,
1719 			unsigned int cmd, void *parg)
1720 {
1721 	struct dvb_device *dvbdev = file->private_data;
1722 	struct dvb_frontend *fe = dvbdev->priv;
1723 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1724 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
1725 	int err = -EOPNOTSUPP;
1726 
1727 	dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
1728 
1729 	if (fepriv->exit != DVB_FE_NO_EXIT)
1730 		return -ENODEV;
1731 
1732 	if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1733 	    (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1734 	     cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1735 		return -EPERM;
1736 
1737 	if (down_interruptible (&fepriv->sem))
1738 		return -ERESTARTSYS;
1739 
1740 	if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1741 		err = dvb_frontend_ioctl_properties(file, cmd, parg);
1742 	else {
1743 		c->state = DTV_UNDEFINED;
1744 		err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1745 	}
1746 
1747 	up(&fepriv->sem);
1748 	return err;
1749 }
1750 
dvb_frontend_ioctl_properties(struct file * file,unsigned int cmd,void * parg)1751 static int dvb_frontend_ioctl_properties(struct file *file,
1752 			unsigned int cmd, void *parg)
1753 {
1754 	struct dvb_device *dvbdev = file->private_data;
1755 	struct dvb_frontend *fe = dvbdev->priv;
1756 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
1757 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1758 	int err = 0;
1759 
1760 	struct dtv_properties *tvps = NULL;
1761 	struct dtv_property *tvp = NULL;
1762 	int i;
1763 
1764 	dprintk("%s\n", __func__);
1765 
1766 	if(cmd == FE_SET_PROPERTY) {
1767 		tvps = (struct dtv_properties __user *)parg;
1768 
1769 		dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1770 		dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1771 
1772 		/* Put an arbitrary limit on the number of messages that can
1773 		 * be sent at once */
1774 		if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1775 			return -EINVAL;
1776 
1777 		tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1778 		if (!tvp) {
1779 			err = -ENOMEM;
1780 			goto out;
1781 		}
1782 
1783 		if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1784 			err = -EFAULT;
1785 			goto out;
1786 		}
1787 
1788 		for (i = 0; i < tvps->num; i++) {
1789 			err = dtv_property_process_set(fe, tvp + i, file);
1790 			if (err < 0)
1791 				goto out;
1792 			(tvp + i)->result = err;
1793 		}
1794 
1795 		if (c->state == DTV_TUNE)
1796 			dprintk("%s() Property cache is full, tuning\n", __func__);
1797 
1798 	} else
1799 	if(cmd == FE_GET_PROPERTY) {
1800 		tvps = (struct dtv_properties __user *)parg;
1801 
1802 		dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1803 		dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1804 
1805 		/* Put an arbitrary limit on the number of messages that can
1806 		 * be sent at once */
1807 		if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1808 			return -EINVAL;
1809 
1810 		tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1811 		if (!tvp) {
1812 			err = -ENOMEM;
1813 			goto out;
1814 		}
1815 
1816 		if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1817 			err = -EFAULT;
1818 			goto out;
1819 		}
1820 
1821 		/*
1822 		 * Fills the cache out struct with the cache contents, plus
1823 		 * the data retrieved from get_frontend, if the frontend
1824 		 * is not idle. Otherwise, returns the cached content
1825 		 */
1826 		if (fepriv->state != FESTATE_IDLE) {
1827 			err = dtv_get_frontend(fe, NULL);
1828 			if (err < 0)
1829 				goto out;
1830 		}
1831 		for (i = 0; i < tvps->num; i++) {
1832 			err = dtv_property_process_get(fe, c, tvp + i, file);
1833 			if (err < 0)
1834 				goto out;
1835 			(tvp + i)->result = err;
1836 		}
1837 
1838 		if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1839 			err = -EFAULT;
1840 			goto out;
1841 		}
1842 
1843 	} else
1844 		err = -EOPNOTSUPP;
1845 
1846 out:
1847 	kfree(tvp);
1848 	return err;
1849 }
1850 
dtv_set_frontend(struct dvb_frontend * fe)1851 static int dtv_set_frontend(struct dvb_frontend *fe)
1852 {
1853 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
1854 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1855 	struct dvb_frontend_tune_settings fetunesettings;
1856 	u32 rolloff = 0;
1857 
1858 	if (dvb_frontend_check_parameters(fe) < 0)
1859 		return -EINVAL;
1860 
1861 	/*
1862 	 * Initialize output parameters to match the values given by
1863 	 * the user. FE_SET_FRONTEND triggers an initial frontend event
1864 	 * with status = 0, which copies output parameters to userspace.
1865 	 */
1866 	dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
1867 
1868 	/*
1869 	 * Be sure that the bandwidth will be filled for all
1870 	 * non-satellite systems, as tuners need to know what
1871 	 * low pass/Nyquist half filter should be applied, in
1872 	 * order to avoid inter-channel noise.
1873 	 *
1874 	 * ISDB-T and DVB-T/T2 already sets bandwidth.
1875 	 * ATSC and DVB-C don't set, so, the core should fill it.
1876 	 *
1877 	 * On DVB-C Annex A and C, the bandwidth is a function of
1878 	 * the roll-off and symbol rate. Annex B defines different
1879 	 * roll-off factors depending on the modulation. Fortunately,
1880 	 * Annex B is only used with 6MHz, so there's no need to
1881 	 * calculate it.
1882 	 *
1883 	 * While not officially supported, a side effect of handling it at
1884 	 * the cache level is that a program could retrieve the bandwidth
1885 	 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1886 	 */
1887 	switch (c->delivery_system) {
1888 	case SYS_ATSC:
1889 	case SYS_DVBC_ANNEX_B:
1890 		c->bandwidth_hz = 6000000;
1891 		break;
1892 	case SYS_DVBC_ANNEX_A:
1893 		rolloff = 115;
1894 		break;
1895 	case SYS_DVBC_ANNEX_C:
1896 		rolloff = 113;
1897 		break;
1898 	default:
1899 		break;
1900 	}
1901 	if (rolloff)
1902 		c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
1903 
1904 	/* force auto frequency inversion if requested */
1905 	if (dvb_force_auto_inversion)
1906 		c->inversion = INVERSION_AUTO;
1907 
1908 	/*
1909 	 * without hierarchical coding code_rate_LP is irrelevant,
1910 	 * so we tolerate the otherwise invalid FEC_NONE setting
1911 	 */
1912 	if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
1913 		c->code_rate_LP = FEC_AUTO;
1914 
1915 	/* get frontend-specific tuning settings */
1916 	memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1917 	if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1918 		fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1919 		fepriv->max_drift = fetunesettings.max_drift;
1920 		fepriv->step_size = fetunesettings.step_size;
1921 	} else {
1922 		/* default values */
1923 		switch (c->delivery_system) {
1924 		case SYS_DVBS:
1925 		case SYS_DVBS2:
1926 		case SYS_ISDBS:
1927 		case SYS_TURBO:
1928 		case SYS_DVBC_ANNEX_A:
1929 		case SYS_DVBC_ANNEX_C:
1930 			fepriv->min_delay = HZ / 20;
1931 			fepriv->step_size = c->symbol_rate / 16000;
1932 			fepriv->max_drift = c->symbol_rate / 2000;
1933 			break;
1934 		case SYS_DVBT:
1935 		case SYS_DVBT2:
1936 		case SYS_ISDBT:
1937 		case SYS_DMBTH:
1938 			fepriv->min_delay = HZ / 20;
1939 			fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1940 			fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1941 			break;
1942 		default:
1943 			/*
1944 			 * FIXME: This sounds wrong! if freqency_stepsize is
1945 			 * defined by the frontend, why not use it???
1946 			 */
1947 			fepriv->min_delay = HZ / 20;
1948 			fepriv->step_size = 0; /* no zigzag */
1949 			fepriv->max_drift = 0;
1950 			break;
1951 		}
1952 	}
1953 	if (dvb_override_tune_delay > 0)
1954 		fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1955 
1956 	fepriv->state = FESTATE_RETUNE;
1957 
1958 	/* Request the search algorithm to search */
1959 	fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1960 
1961 	dvb_frontend_clear_events(fe);
1962 	dvb_frontend_add_event(fe, 0);
1963 	dvb_frontend_wakeup(fe);
1964 	fepriv->status = 0;
1965 
1966 	return 0;
1967 }
1968 
1969 
dvb_frontend_ioctl_legacy(struct file * file,unsigned int cmd,void * parg)1970 static int dvb_frontend_ioctl_legacy(struct file *file,
1971 			unsigned int cmd, void *parg)
1972 {
1973 	struct dvb_device *dvbdev = file->private_data;
1974 	struct dvb_frontend *fe = dvbdev->priv;
1975 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
1976 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1977 	int cb_err, err = -EOPNOTSUPP;
1978 
1979 	if (fe->dvb->fe_ioctl_override) {
1980 		cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1981 						    DVB_FE_IOCTL_PRE);
1982 		if (cb_err < 0)
1983 			return cb_err;
1984 		if (cb_err > 0)
1985 			return 0;
1986 		/* fe_ioctl_override returning 0 allows
1987 		 * dvb-core to continue handling the ioctl */
1988 	}
1989 
1990 	switch (cmd) {
1991 	case FE_GET_INFO: {
1992 		struct dvb_frontend_info* info = parg;
1993 
1994 		memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1995 		dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
1996 
1997 		/*
1998 		 * Associate the 4 delivery systems supported by DVBv3
1999 		 * API with their DVBv5 counterpart. For the other standards,
2000 		 * use the closest type, assuming that it would hopefully
2001 		 * work with a DVBv3 application.
2002 		 * It should be noticed that, on multi-frontend devices with
2003 		 * different types (terrestrial and cable, for example),
2004 		 * a pure DVBv3 application won't be able to use all delivery
2005 		 * systems. Yet, changing the DVBv5 cache to the other delivery
2006 		 * system should be enough for making it work.
2007 		 */
2008 		switch (dvbv3_type(c->delivery_system)) {
2009 		case DVBV3_QPSK:
2010 			info->type = FE_QPSK;
2011 			break;
2012 		case DVBV3_ATSC:
2013 			info->type = FE_ATSC;
2014 			break;
2015 		case DVBV3_QAM:
2016 			info->type = FE_QAM;
2017 			break;
2018 		case DVBV3_OFDM:
2019 			info->type = FE_OFDM;
2020 			break;
2021 		default:
2022 			printk(KERN_ERR
2023 			       "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2024 			       __func__, c->delivery_system);
2025 			fe->ops.info.type = FE_OFDM;
2026 		}
2027 		dprintk("current delivery system on cache: %d, V3 type: %d\n",
2028 			c->delivery_system, fe->ops.info.type);
2029 
2030 		/* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2031 		 * do it, it is done for it. */
2032 		info->caps |= FE_CAN_INVERSION_AUTO;
2033 		err = 0;
2034 		break;
2035 	}
2036 
2037 	case FE_READ_STATUS: {
2038 		fe_status_t* status = parg;
2039 
2040 		/* if retune was requested but hasn't occurred yet, prevent
2041 		 * that user get signal state from previous tuning */
2042 		if (fepriv->state == FESTATE_RETUNE ||
2043 		    fepriv->state == FESTATE_ERROR) {
2044 			err=0;
2045 			*status = 0;
2046 			break;
2047 		}
2048 
2049 		if (fe->ops.read_status)
2050 			err = fe->ops.read_status(fe, status);
2051 		break;
2052 	}
2053 	case FE_READ_BER:
2054 		if (fe->ops.read_ber)
2055 			err = fe->ops.read_ber(fe, (__u32*) parg);
2056 		break;
2057 
2058 	case FE_READ_SIGNAL_STRENGTH:
2059 		if (fe->ops.read_signal_strength)
2060 			err = fe->ops.read_signal_strength(fe, (__u16*) parg);
2061 		break;
2062 
2063 	case FE_READ_SNR:
2064 		if (fe->ops.read_snr)
2065 			err = fe->ops.read_snr(fe, (__u16*) parg);
2066 		break;
2067 
2068 	case FE_READ_UNCORRECTED_BLOCKS:
2069 		if (fe->ops.read_ucblocks)
2070 			err = fe->ops.read_ucblocks(fe, (__u32*) parg);
2071 		break;
2072 
2073 
2074 	case FE_DISEQC_RESET_OVERLOAD:
2075 		if (fe->ops.diseqc_reset_overload) {
2076 			err = fe->ops.diseqc_reset_overload(fe);
2077 			fepriv->state = FESTATE_DISEQC;
2078 			fepriv->status = 0;
2079 		}
2080 		break;
2081 
2082 	case FE_DISEQC_SEND_MASTER_CMD:
2083 		if (fe->ops.diseqc_send_master_cmd) {
2084 			err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2085 			fepriv->state = FESTATE_DISEQC;
2086 			fepriv->status = 0;
2087 		}
2088 		break;
2089 
2090 	case FE_DISEQC_SEND_BURST:
2091 		if (fe->ops.diseqc_send_burst) {
2092 			err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2093 			fepriv->state = FESTATE_DISEQC;
2094 			fepriv->status = 0;
2095 		}
2096 		break;
2097 
2098 	case FE_SET_TONE:
2099 		if (fe->ops.set_tone) {
2100 			err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2101 			fepriv->tone = (fe_sec_tone_mode_t) parg;
2102 			fepriv->state = FESTATE_DISEQC;
2103 			fepriv->status = 0;
2104 		}
2105 		break;
2106 
2107 	case FE_SET_VOLTAGE:
2108 		if (fe->ops.set_voltage) {
2109 			err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2110 			fepriv->voltage = (fe_sec_voltage_t) parg;
2111 			fepriv->state = FESTATE_DISEQC;
2112 			fepriv->status = 0;
2113 		}
2114 		break;
2115 
2116 	case FE_DISHNETWORK_SEND_LEGACY_CMD:
2117 		if (fe->ops.dishnetwork_send_legacy_command) {
2118 			err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2119 			fepriv->state = FESTATE_DISEQC;
2120 			fepriv->status = 0;
2121 		} else if (fe->ops.set_voltage) {
2122 			/*
2123 			 * NOTE: This is a fallback condition.  Some frontends
2124 			 * (stv0299 for instance) take longer than 8msec to
2125 			 * respond to a set_voltage command.  Those switches
2126 			 * need custom routines to switch properly.  For all
2127 			 * other frontends, the following should work ok.
2128 			 * Dish network legacy switches (as used by Dish500)
2129 			 * are controlled by sending 9-bit command words
2130 			 * spaced 8msec apart.
2131 			 * the actual command word is switch/port dependent
2132 			 * so it is up to the userspace application to send
2133 			 * the right command.
2134 			 * The command must always start with a '0' after
2135 			 * initialization, so parg is 8 bits and does not
2136 			 * include the initialization or start bit
2137 			 */
2138 			unsigned long swcmd = ((unsigned long) parg) << 1;
2139 			struct timeval nexttime;
2140 			struct timeval tv[10];
2141 			int i;
2142 			u8 last = 1;
2143 			if (dvb_frontend_debug)
2144 				printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2145 			do_gettimeofday(&nexttime);
2146 			if (dvb_frontend_debug)
2147 				memcpy(&tv[0], &nexttime, sizeof(struct timeval));
2148 			/* before sending a command, initialize by sending
2149 			 * a 32ms 18V to the switch
2150 			 */
2151 			fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2152 			dvb_frontend_sleep_until(&nexttime, 32000);
2153 
2154 			for (i = 0; i < 9; i++) {
2155 				if (dvb_frontend_debug)
2156 					do_gettimeofday(&tv[i + 1]);
2157 				if ((swcmd & 0x01) != last) {
2158 					/* set voltage to (last ? 13V : 18V) */
2159 					fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2160 					last = (last) ? 0 : 1;
2161 				}
2162 				swcmd = swcmd >> 1;
2163 				if (i != 8)
2164 					dvb_frontend_sleep_until(&nexttime, 8000);
2165 			}
2166 			if (dvb_frontend_debug) {
2167 				printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2168 					__func__, fe->dvb->num);
2169 				for (i = 1; i < 10; i++)
2170 					printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2171 			}
2172 			err = 0;
2173 			fepriv->state = FESTATE_DISEQC;
2174 			fepriv->status = 0;
2175 		}
2176 		break;
2177 
2178 	case FE_DISEQC_RECV_SLAVE_REPLY:
2179 		if (fe->ops.diseqc_recv_slave_reply)
2180 			err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2181 		break;
2182 
2183 	case FE_ENABLE_HIGH_LNB_VOLTAGE:
2184 		if (fe->ops.enable_high_lnb_voltage)
2185 			err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2186 		break;
2187 
2188 	case FE_SET_FRONTEND:
2189 		err = set_delivery_system(fe, SYS_UNDEFINED);
2190 		if (err)
2191 			break;
2192 
2193 		err = dtv_property_cache_sync(fe, c, parg);
2194 		if (err)
2195 			break;
2196 		err = dtv_set_frontend(fe);
2197 		break;
2198 	case FE_GET_EVENT:
2199 		err = dvb_frontend_get_event (fe, parg, file->f_flags);
2200 		break;
2201 
2202 	case FE_GET_FRONTEND:
2203 		err = dtv_get_frontend(fe, parg);
2204 		break;
2205 
2206 	case FE_SET_FRONTEND_TUNE_MODE:
2207 		fepriv->tune_mode_flags = (unsigned long) parg;
2208 		err = 0;
2209 		break;
2210 	};
2211 
2212 	if (fe->dvb->fe_ioctl_override) {
2213 		cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
2214 						    DVB_FE_IOCTL_POST);
2215 		if (cb_err < 0)
2216 			return cb_err;
2217 	}
2218 
2219 	return err;
2220 }
2221 
2222 
dvb_frontend_poll(struct file * file,struct poll_table_struct * wait)2223 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2224 {
2225 	struct dvb_device *dvbdev = file->private_data;
2226 	struct dvb_frontend *fe = dvbdev->priv;
2227 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
2228 
2229 	dprintk ("%s\n", __func__);
2230 
2231 	poll_wait (file, &fepriv->events.wait_queue, wait);
2232 
2233 	if (fepriv->events.eventw != fepriv->events.eventr)
2234 		return (POLLIN | POLLRDNORM | POLLPRI);
2235 
2236 	return 0;
2237 }
2238 
dvb_frontend_open(struct inode * inode,struct file * file)2239 static int dvb_frontend_open(struct inode *inode, struct file *file)
2240 {
2241 	struct dvb_device *dvbdev = file->private_data;
2242 	struct dvb_frontend *fe = dvbdev->priv;
2243 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
2244 	struct dvb_adapter *adapter = fe->dvb;
2245 	int ret;
2246 
2247 	dprintk ("%s\n", __func__);
2248 	if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2249 		return -ENODEV;
2250 
2251 	if (adapter->mfe_shared) {
2252 		mutex_lock (&adapter->mfe_lock);
2253 
2254 		if (adapter->mfe_dvbdev == NULL)
2255 			adapter->mfe_dvbdev = dvbdev;
2256 
2257 		else if (adapter->mfe_dvbdev != dvbdev) {
2258 			struct dvb_device
2259 				*mfedev = adapter->mfe_dvbdev;
2260 			struct dvb_frontend
2261 				*mfe = mfedev->priv;
2262 			struct dvb_frontend_private
2263 				*mfepriv = mfe->frontend_priv;
2264 			int mferetry = (dvb_mfe_wait_time << 1);
2265 
2266 			mutex_unlock (&adapter->mfe_lock);
2267 			while (mferetry-- && (mfedev->users != -1 ||
2268 					mfepriv->thread != NULL)) {
2269 				if(msleep_interruptible(500)) {
2270 					if(signal_pending(current))
2271 						return -EINTR;
2272 				}
2273 			}
2274 
2275 			mutex_lock (&adapter->mfe_lock);
2276 			if(adapter->mfe_dvbdev != dvbdev) {
2277 				mfedev = adapter->mfe_dvbdev;
2278 				mfe = mfedev->priv;
2279 				mfepriv = mfe->frontend_priv;
2280 				if (mfedev->users != -1 ||
2281 						mfepriv->thread != NULL) {
2282 					mutex_unlock (&adapter->mfe_lock);
2283 					return -EBUSY;
2284 				}
2285 				adapter->mfe_dvbdev = dvbdev;
2286 			}
2287 		}
2288 	}
2289 
2290 	if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2291 		if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2292 			goto err0;
2293 
2294 		/* If we took control of the bus, we need to force
2295 		   reinitialization.  This is because many ts_bus_ctrl()
2296 		   functions strobe the RESET pin on the demod, and if the
2297 		   frontend thread already exists then the dvb_init() routine
2298 		   won't get called (which is what usually does initial
2299 		   register configuration). */
2300 		fepriv->reinitialise = 1;
2301 	}
2302 
2303 	if ((ret = dvb_generic_open (inode, file)) < 0)
2304 		goto err1;
2305 
2306 	if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2307 		/* normal tune mode when opened R/W */
2308 		fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2309 		fepriv->tone = -1;
2310 		fepriv->voltage = -1;
2311 
2312 		ret = dvb_frontend_start (fe);
2313 		if (ret)
2314 			goto err2;
2315 
2316 		/*  empty event queue */
2317 		fepriv->events.eventr = fepriv->events.eventw = 0;
2318 	}
2319 
2320 	if (adapter->mfe_shared)
2321 		mutex_unlock (&adapter->mfe_lock);
2322 	return ret;
2323 
2324 err2:
2325 	dvb_generic_release(inode, file);
2326 err1:
2327 	if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2328 		fe->ops.ts_bus_ctrl(fe, 0);
2329 err0:
2330 	if (adapter->mfe_shared)
2331 		mutex_unlock (&adapter->mfe_lock);
2332 	return ret;
2333 }
2334 
dvb_frontend_release(struct inode * inode,struct file * file)2335 static int dvb_frontend_release(struct inode *inode, struct file *file)
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 	int ret;
2341 
2342 	dprintk ("%s\n", __func__);
2343 
2344 	if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2345 		fepriv->release_jiffies = jiffies;
2346 		mb();
2347 	}
2348 
2349 	ret = dvb_generic_release (inode, file);
2350 
2351 	if (dvbdev->users == -1) {
2352 		wake_up(&fepriv->wait_queue);
2353 		if (fepriv->exit != DVB_FE_NO_EXIT) {
2354 			fops_put(file->f_op);
2355 			file->f_op = NULL;
2356 			wake_up(&dvbdev->wait_queue);
2357 		}
2358 		if (fe->ops.ts_bus_ctrl)
2359 			fe->ops.ts_bus_ctrl(fe, 0);
2360 	}
2361 
2362 	return ret;
2363 }
2364 
2365 static const struct file_operations dvb_frontend_fops = {
2366 	.owner		= THIS_MODULE,
2367 	.unlocked_ioctl	= dvb_generic_ioctl,
2368 	.poll		= dvb_frontend_poll,
2369 	.open		= dvb_frontend_open,
2370 	.release	= dvb_frontend_release,
2371 	.llseek		= noop_llseek,
2372 };
2373 
dvb_register_frontend(struct dvb_adapter * dvb,struct dvb_frontend * fe)2374 int dvb_register_frontend(struct dvb_adapter* dvb,
2375 			  struct dvb_frontend* fe)
2376 {
2377 	struct dvb_frontend_private *fepriv;
2378 	static const struct dvb_device dvbdev_template = {
2379 		.users = ~0,
2380 		.writers = 1,
2381 		.readers = (~0)-1,
2382 		.fops = &dvb_frontend_fops,
2383 		.kernel_ioctl = dvb_frontend_ioctl
2384 	};
2385 
2386 	dprintk ("%s\n", __func__);
2387 
2388 	if (mutex_lock_interruptible(&frontend_mutex))
2389 		return -ERESTARTSYS;
2390 
2391 	fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2392 	if (fe->frontend_priv == NULL) {
2393 		mutex_unlock(&frontend_mutex);
2394 		return -ENOMEM;
2395 	}
2396 	fepriv = fe->frontend_priv;
2397 
2398 	sema_init(&fepriv->sem, 1);
2399 	init_waitqueue_head (&fepriv->wait_queue);
2400 	init_waitqueue_head (&fepriv->events.wait_queue);
2401 	mutex_init(&fepriv->events.mtx);
2402 	fe->dvb = dvb;
2403 	fepriv->inversion = INVERSION_OFF;
2404 
2405 	printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2406 		fe->dvb->num,
2407 		fe->id,
2408 		fe->ops.info.name);
2409 
2410 	dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2411 			     fe, DVB_DEVICE_FRONTEND);
2412 
2413 	/*
2414 	 * Initialize the cache to the proper values according with the
2415 	 * first supported delivery system (ops->delsys[0])
2416 	 */
2417 
2418         fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2419 	dvb_frontend_clear_cache(fe);
2420 
2421 	mutex_unlock(&frontend_mutex);
2422 	return 0;
2423 }
2424 EXPORT_SYMBOL(dvb_register_frontend);
2425 
dvb_unregister_frontend(struct dvb_frontend * fe)2426 int dvb_unregister_frontend(struct dvb_frontend* fe)
2427 {
2428 	struct dvb_frontend_private *fepriv = fe->frontend_priv;
2429 	dprintk ("%s\n", __func__);
2430 
2431 	mutex_lock(&frontend_mutex);
2432 	dvb_frontend_stop (fe);
2433 	mutex_unlock(&frontend_mutex);
2434 
2435 	if (fepriv->dvbdev->users < -1)
2436 		wait_event(fepriv->dvbdev->wait_queue,
2437 				fepriv->dvbdev->users==-1);
2438 
2439 	mutex_lock(&frontend_mutex);
2440 	dvb_unregister_device (fepriv->dvbdev);
2441 
2442 	/* fe is invalid now */
2443 	kfree(fepriv);
2444 	mutex_unlock(&frontend_mutex);
2445 	return 0;
2446 }
2447 EXPORT_SYMBOL(dvb_unregister_frontend);
2448 
2449 #ifdef CONFIG_MEDIA_ATTACH
dvb_frontend_detach(struct dvb_frontend * fe)2450 void dvb_frontend_detach(struct dvb_frontend* fe)
2451 {
2452 	void *ptr;
2453 
2454 	if (fe->ops.release_sec) {
2455 		fe->ops.release_sec(fe);
2456 		symbol_put_addr(fe->ops.release_sec);
2457 	}
2458 	if (fe->ops.tuner_ops.release) {
2459 		fe->ops.tuner_ops.release(fe);
2460 		symbol_put_addr(fe->ops.tuner_ops.release);
2461 	}
2462 	if (fe->ops.analog_ops.release) {
2463 		fe->ops.analog_ops.release(fe);
2464 		symbol_put_addr(fe->ops.analog_ops.release);
2465 	}
2466 	ptr = (void*)fe->ops.release;
2467 	if (ptr) {
2468 		fe->ops.release(fe);
2469 		symbol_put_addr(ptr);
2470 	}
2471 }
2472 #else
dvb_frontend_detach(struct dvb_frontend * fe)2473 void dvb_frontend_detach(struct dvb_frontend* fe)
2474 {
2475 	if (fe->ops.release_sec)
2476 		fe->ops.release_sec(fe);
2477 	if (fe->ops.tuner_ops.release)
2478 		fe->ops.tuner_ops.release(fe);
2479 	if (fe->ops.analog_ops.release)
2480 		fe->ops.analog_ops.release(fe);
2481 	if (fe->ops.release)
2482 		fe->ops.release(fe);
2483 }
2484 #endif
2485 EXPORT_SYMBOL(dvb_frontend_detach);
2486