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