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