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