[media] dvb-core: fix a regression with MythTV
[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         /*
1417          * It was reported that some old DVBv5 applications were
1418          * filling delivery_system with SYS_UNDEFINED. If this happens,
1419          * assume that the application wants to use the first supported
1420          * delivery system.
1421          */
1422         if (c->delivery_system == SYS_UNDEFINED)
1423                 c->delivery_system = fe->ops.delsys[0];
1424
1425         if (desired_system == SYS_UNDEFINED) {
1426                 /*
1427                  * A DVBv3 call doesn't know what's the desired system.
1428                  * Also, DVBv3 applications don't know that ops.info->type
1429                  * could be changed, and they simply dies when it doesn't
1430                  * match.
1431                  * So, don't change the current delivery system, as it
1432                  * may be trying to do the wrong thing, like setting an
1433                  * ISDB-T frontend as DVB-T. Instead, find the closest
1434                  * DVBv3 system that matches the delivery system.
1435                  */
1436                 if (is_dvbv3_delsys(c->delivery_system)) {
1437                         dprintk("%s() Using delivery system to %d\n",
1438                                 __func__, c->delivery_system);
1439                         return 0;
1440                 }
1441                 type = dvbv3_type(c->delivery_system);
1442                 switch (type) {
1443                 case DVBV3_QPSK:
1444                         desired_system = SYS_DVBS;
1445                         break;
1446                 case DVBV3_QAM:
1447                         desired_system = SYS_DVBC_ANNEX_A;
1448                         break;
1449                 case DVBV3_ATSC:
1450                         desired_system = SYS_ATSC;
1451                         break;
1452                 case DVBV3_OFDM:
1453                         desired_system = SYS_DVBT;
1454                         break;
1455                 default:
1456                         dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
1457                                 __func__);
1458                         return -EINVAL;
1459                 }
1460         } else {
1461                 /*
1462                  * This is a DVBv5 call. So, it likely knows the supported
1463                  * delivery systems.
1464                  */
1465
1466                 /* Check if the desired delivery system is supported */
1467                 ncaps = 0;
1468                 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1469                         if (fe->ops.delsys[ncaps] == desired_system) {
1470                                 c->delivery_system = desired_system;
1471                                 dprintk("%s() Changing delivery system to %d\n",
1472                                         __func__, desired_system);
1473                                 return 0;
1474                         }
1475                         ncaps++;
1476                 }
1477                 type = dvbv3_type(desired_system);
1478
1479                 /*
1480                  * The delivery system is not supported. See if it can be
1481                  * emulated.
1482                  * The emulation only works if the desired system is one of the
1483                  * DVBv3 delivery systems
1484                  */
1485                 if (!is_dvbv3_delsys(desired_system)) {
1486                         dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1487                                 __func__);
1488                         return -EINVAL;
1489                 }
1490
1491                 /*
1492                  * Get the last non-DVBv3 delivery system that has the same type
1493                  * of the desired system
1494                  */
1495                 ncaps = 0;
1496                 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1497                         if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
1498                             !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
1499                                 delsys = fe->ops.delsys[ncaps];
1500                         ncaps++;
1501                 }
1502                 /* There's nothing compatible with the desired delivery system */
1503                 if (delsys == SYS_UNDEFINED) {
1504                         dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1505                                 __func__);
1506                         return -EINVAL;
1507                 }
1508                 c->delivery_system = delsys;
1509         }
1510
1511         /*
1512          * The DVBv3 or DVBv5 call is requesting a different system. So,
1513          * emulation is needed.
1514          *
1515          * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
1516          * for older DVBv5 applications. The emulation will try to use
1517          * the auto mode for most things, and will assume that the desired
1518          * delivery system is the last one at the ops.delsys[] array
1519          */
1520         dprintk("%s() Using delivery system %d emulated as if it were a %d\n",
1521                 __func__, delsys, desired_system);
1522
1523         /*
1524          * For now, handles ISDB-T calls. More code may be needed here for the
1525          * other emulated stuff
1526          */
1527         if (type == DVBV3_OFDM) {
1528                 if (c->delivery_system == SYS_ISDBT) {
1529                         dprintk("%s() Using defaults for SYS_ISDBT\n",
1530                                 __func__);
1531                         if (!c->bandwidth_hz)
1532                                 c->bandwidth_hz = 6000000;
1533
1534                         c->isdbt_partial_reception = 0;
1535                         c->isdbt_sb_mode = 0;
1536                         c->isdbt_sb_subchannel = 0;
1537                         c->isdbt_sb_segment_idx = 0;
1538                         c->isdbt_sb_segment_count = 0;
1539                         c->isdbt_layer_enabled = 0;
1540                         for (i = 0; i < 3; i++) {
1541                                 c->layer[i].fec = FEC_AUTO;
1542                                 c->layer[i].modulation = QAM_AUTO;
1543                                 c->layer[i].interleaving = 0;
1544                                 c->layer[i].segment_count = 0;
1545                         }
1546                 }
1547         }
1548         dprintk("change delivery system on cache to %d\n", c->delivery_system);
1549
1550         return 0;
1551 }
1552
1553 static int dtv_property_process_set(struct dvb_frontend *fe,
1554                                     struct dtv_property *tvp,
1555                                     struct file *file)
1556 {
1557         int r = 0;
1558         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1559
1560         /* Allow the frontend to validate incoming properties */
1561         if (fe->ops.set_property) {
1562                 r = fe->ops.set_property(fe, tvp);
1563                 if (r < 0)
1564                         return r;
1565         }
1566
1567         switch(tvp->cmd) {
1568         case DTV_CLEAR:
1569                 /*
1570                  * Reset a cache of data specific to the frontend here. This does
1571                  * not effect hardware.
1572                  */
1573                 dvb_frontend_clear_cache(fe);
1574                 break;
1575         case DTV_TUNE:
1576                 /* interpret the cache of data, build either a traditional frontend
1577                  * tunerequest so we can pass validation in the FE_SET_FRONTEND
1578                  * ioctl.
1579                  */
1580                 c->state = tvp->cmd;
1581                 dprintk("%s() Finalised property cache\n", __func__);
1582
1583                 r = dtv_set_frontend(fe);
1584                 break;
1585         case DTV_FREQUENCY:
1586                 c->frequency = tvp->u.data;
1587                 break;
1588         case DTV_MODULATION:
1589                 c->modulation = tvp->u.data;
1590                 break;
1591         case DTV_BANDWIDTH_HZ:
1592                 c->bandwidth_hz = tvp->u.data;
1593                 break;
1594         case DTV_INVERSION:
1595                 c->inversion = tvp->u.data;
1596                 break;
1597         case DTV_SYMBOL_RATE:
1598                 c->symbol_rate = tvp->u.data;
1599                 break;
1600         case DTV_INNER_FEC:
1601                 c->fec_inner = tvp->u.data;
1602                 break;
1603         case DTV_PILOT:
1604                 c->pilot = tvp->u.data;
1605                 break;
1606         case DTV_ROLLOFF:
1607                 c->rolloff = tvp->u.data;
1608                 break;
1609         case DTV_DELIVERY_SYSTEM:
1610                 r = set_delivery_system(fe, tvp->u.data);
1611                 break;
1612         case DTV_VOLTAGE:
1613                 c->voltage = tvp->u.data;
1614                 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1615                         (void *)c->voltage);
1616                 break;
1617         case DTV_TONE:
1618                 c->sectone = tvp->u.data;
1619                 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1620                         (void *)c->sectone);
1621                 break;
1622         case DTV_CODE_RATE_HP:
1623                 c->code_rate_HP = tvp->u.data;
1624                 break;
1625         case DTV_CODE_RATE_LP:
1626                 c->code_rate_LP = tvp->u.data;
1627                 break;
1628         case DTV_GUARD_INTERVAL:
1629                 c->guard_interval = tvp->u.data;
1630                 break;
1631         case DTV_TRANSMISSION_MODE:
1632                 c->transmission_mode = tvp->u.data;
1633                 break;
1634         case DTV_HIERARCHY:
1635                 c->hierarchy = tvp->u.data;
1636                 break;
1637
1638         /* ISDB-T Support here */
1639         case DTV_ISDBT_PARTIAL_RECEPTION:
1640                 c->isdbt_partial_reception = tvp->u.data;
1641                 break;
1642         case DTV_ISDBT_SOUND_BROADCASTING:
1643                 c->isdbt_sb_mode = tvp->u.data;
1644                 break;
1645         case DTV_ISDBT_SB_SUBCHANNEL_ID:
1646                 c->isdbt_sb_subchannel = tvp->u.data;
1647                 break;
1648         case DTV_ISDBT_SB_SEGMENT_IDX:
1649                 c->isdbt_sb_segment_idx = tvp->u.data;
1650                 break;
1651         case DTV_ISDBT_SB_SEGMENT_COUNT:
1652                 c->isdbt_sb_segment_count = tvp->u.data;
1653                 break;
1654         case DTV_ISDBT_LAYER_ENABLED:
1655                 c->isdbt_layer_enabled = tvp->u.data;
1656                 break;
1657         case DTV_ISDBT_LAYERA_FEC:
1658                 c->layer[0].fec = tvp->u.data;
1659                 break;
1660         case DTV_ISDBT_LAYERA_MODULATION:
1661                 c->layer[0].modulation = tvp->u.data;
1662                 break;
1663         case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1664                 c->layer[0].segment_count = tvp->u.data;
1665                 break;
1666         case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1667                 c->layer[0].interleaving = tvp->u.data;
1668                 break;
1669         case DTV_ISDBT_LAYERB_FEC:
1670                 c->layer[1].fec = tvp->u.data;
1671                 break;
1672         case DTV_ISDBT_LAYERB_MODULATION:
1673                 c->layer[1].modulation = tvp->u.data;
1674                 break;
1675         case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1676                 c->layer[1].segment_count = tvp->u.data;
1677                 break;
1678         case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1679                 c->layer[1].interleaving = tvp->u.data;
1680                 break;
1681         case DTV_ISDBT_LAYERC_FEC:
1682                 c->layer[2].fec = tvp->u.data;
1683                 break;
1684         case DTV_ISDBT_LAYERC_MODULATION:
1685                 c->layer[2].modulation = tvp->u.data;
1686                 break;
1687         case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1688                 c->layer[2].segment_count = tvp->u.data;
1689                 break;
1690         case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1691                 c->layer[2].interleaving = tvp->u.data;
1692                 break;
1693         case DTV_ISDBS_TS_ID:
1694                 c->isdbs_ts_id = tvp->u.data;
1695                 break;
1696         case DTV_DVBT2_PLP_ID:
1697                 c->dvbt2_plp_id = tvp->u.data;
1698                 break;
1699         default:
1700                 return -EINVAL;
1701         }
1702
1703         return r;
1704 }
1705
1706 static int dvb_frontend_ioctl(struct file *file,
1707                         unsigned int cmd, void *parg)
1708 {
1709         struct dvb_device *dvbdev = file->private_data;
1710         struct dvb_frontend *fe = dvbdev->priv;
1711         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1712         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1713         int err = -EOPNOTSUPP;
1714
1715         dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
1716
1717         if (fepriv->exit != DVB_FE_NO_EXIT)
1718                 return -ENODEV;
1719
1720         if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1721             (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1722              cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1723                 return -EPERM;
1724
1725         if (down_interruptible (&fepriv->sem))
1726                 return -ERESTARTSYS;
1727
1728         if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1729                 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1730         else {
1731                 c->state = DTV_UNDEFINED;
1732                 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1733         }
1734
1735         up(&fepriv->sem);
1736         return err;
1737 }
1738
1739 static int dvb_frontend_ioctl_properties(struct file *file,
1740                         unsigned int cmd, void *parg)
1741 {
1742         struct dvb_device *dvbdev = file->private_data;
1743         struct dvb_frontend *fe = dvbdev->priv;
1744         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1745         int err = 0;
1746
1747         struct dtv_properties *tvps = NULL;
1748         struct dtv_property *tvp = NULL;
1749         int i;
1750
1751         dprintk("%s\n", __func__);
1752
1753         if(cmd == FE_SET_PROPERTY) {
1754                 tvps = (struct dtv_properties __user *)parg;
1755
1756                 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1757                 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1758
1759                 /* Put an arbitrary limit on the number of messages that can
1760                  * be sent at once */
1761                 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1762                         return -EINVAL;
1763
1764                 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1765                 if (!tvp) {
1766                         err = -ENOMEM;
1767                         goto out;
1768                 }
1769
1770                 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1771                         err = -EFAULT;
1772                         goto out;
1773                 }
1774
1775                 for (i = 0; i < tvps->num; i++) {
1776                         err = dtv_property_process_set(fe, tvp + i, file);
1777                         if (err < 0)
1778                                 goto out;
1779                         (tvp + i)->result = err;
1780                 }
1781
1782                 if (c->state == DTV_TUNE)
1783                         dprintk("%s() Property cache is full, tuning\n", __func__);
1784
1785         } else
1786         if(cmd == FE_GET_PROPERTY) {
1787                 tvps = (struct dtv_properties __user *)parg;
1788
1789                 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1790                 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1791
1792                 /* Put an arbitrary limit on the number of messages that can
1793                  * be sent at once */
1794                 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1795                         return -EINVAL;
1796
1797                 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1798                 if (!tvp) {
1799                         err = -ENOMEM;
1800                         goto out;
1801                 }
1802
1803                 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1804                         err = -EFAULT;
1805                         goto out;
1806                 }
1807
1808                 /*
1809                  * Fills the cache out struct with the cache contents, plus
1810                  * the data retrieved from get_frontend.
1811                  */
1812                 dtv_get_frontend(fe, NULL);
1813                 for (i = 0; i < tvps->num; i++) {
1814                         err = dtv_property_process_get(fe, c, tvp + i, file);
1815                         if (err < 0)
1816                                 goto out;
1817                         (tvp + i)->result = err;
1818                 }
1819
1820                 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1821                         err = -EFAULT;
1822                         goto out;
1823                 }
1824
1825         } else
1826                 err = -EOPNOTSUPP;
1827
1828 out:
1829         kfree(tvp);
1830         return err;
1831 }
1832
1833 static int dtv_set_frontend(struct dvb_frontend *fe)
1834 {
1835         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1836         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1837         struct dvb_frontend_tune_settings fetunesettings;
1838         u32 rolloff = 0;
1839
1840         if (dvb_frontend_check_parameters(fe) < 0)
1841                 return -EINVAL;
1842
1843         /*
1844          * Be sure that the bandwidth will be filled for all
1845          * non-satellite systems, as tuners need to know what
1846          * low pass/Nyquist half filter should be applied, in
1847          * order to avoid inter-channel noise.
1848          *
1849          * ISDB-T and DVB-T/T2 already sets bandwidth.
1850          * ATSC and DVB-C don't set, so, the core should fill it.
1851          *
1852          * On DVB-C Annex A and C, the bandwidth is a function of
1853          * the roll-off and symbol rate. Annex B defines different
1854          * roll-off factors depending on the modulation. Fortunately,
1855          * Annex B is only used with 6MHz, so there's no need to
1856          * calculate it.
1857          *
1858          * While not officially supported, a side effect of handling it at
1859          * the cache level is that a program could retrieve the bandwidth
1860          * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1861          */
1862         switch (c->delivery_system) {
1863         case SYS_ATSC:
1864         case SYS_DVBC_ANNEX_B:
1865                 c->bandwidth_hz = 6000000;
1866                 break;
1867         case SYS_DVBC_ANNEX_A:
1868                 rolloff = 115;
1869                 break;
1870         case SYS_DVBC_ANNEX_C:
1871                 rolloff = 113;
1872                 break;
1873         default:
1874                 break;
1875         }
1876         if (rolloff)
1877                 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
1878
1879         /* force auto frequency inversion if requested */
1880         if (dvb_force_auto_inversion)
1881                 c->inversion = INVERSION_AUTO;
1882
1883         /*
1884          * without hierarchical coding code_rate_LP is irrelevant,
1885          * so we tolerate the otherwise invalid FEC_NONE setting
1886          */
1887         if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
1888                 c->code_rate_LP = FEC_AUTO;
1889
1890         /* get frontend-specific tuning settings */
1891         memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1892         if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1893                 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1894                 fepriv->max_drift = fetunesettings.max_drift;
1895                 fepriv->step_size = fetunesettings.step_size;
1896         } else {
1897                 /* default values */
1898                 switch (c->delivery_system) {
1899                 case SYS_DVBC_ANNEX_A:
1900                 case SYS_DVBC_ANNEX_C:
1901                         fepriv->min_delay = HZ / 20;
1902                         fepriv->step_size = c->symbol_rate / 16000;
1903                         fepriv->max_drift = c->symbol_rate / 2000;
1904                         break;
1905                 case SYS_DVBT:
1906                 case SYS_DVBT2:
1907                 case SYS_ISDBT:
1908                 case SYS_DMBTH:
1909                         fepriv->min_delay = HZ / 20;
1910                         fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1911                         fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1912                         break;
1913                 default:
1914                         /*
1915                          * FIXME: This sounds wrong! if freqency_stepsize is
1916                          * defined by the frontend, why not use it???
1917                          */
1918                         fepriv->min_delay = HZ / 20;
1919                         fepriv->step_size = 0; /* no zigzag */
1920                         fepriv->max_drift = 0;
1921                         break;
1922                 }
1923         }
1924         if (dvb_override_tune_delay > 0)
1925                 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1926
1927         fepriv->state = FESTATE_RETUNE;
1928
1929         /* Request the search algorithm to search */
1930         fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1931
1932         dvb_frontend_clear_events(fe);
1933         dvb_frontend_add_event(fe, 0);
1934         dvb_frontend_wakeup(fe);
1935         fepriv->status = 0;
1936
1937         return 0;
1938 }
1939
1940
1941 static int dvb_frontend_ioctl_legacy(struct file *file,
1942                         unsigned int cmd, void *parg)
1943 {
1944         struct dvb_device *dvbdev = file->private_data;
1945         struct dvb_frontend *fe = dvbdev->priv;
1946         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1947         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1948         int cb_err, err = -EOPNOTSUPP;
1949
1950         if (fe->dvb->fe_ioctl_override) {
1951                 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1952                                                     DVB_FE_IOCTL_PRE);
1953                 if (cb_err < 0)
1954                         return cb_err;
1955                 if (cb_err > 0)
1956                         return 0;
1957                 /* fe_ioctl_override returning 0 allows
1958                  * dvb-core to continue handling the ioctl */
1959         }
1960
1961         switch (cmd) {
1962         case FE_GET_INFO: {
1963                 struct dvb_frontend_info* info = parg;
1964
1965                 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1966                 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
1967
1968                 /*
1969                  * Associate the 4 delivery systems supported by DVBv3
1970                  * API with their DVBv5 counterpart. For the other standards,
1971                  * use the closest type, assuming that it would hopefully
1972                  * work with a DVBv3 application.
1973                  * It should be noticed that, on multi-frontend devices with
1974                  * different types (terrestrial and cable, for example),
1975                  * a pure DVBv3 application won't be able to use all delivery
1976                  * systems. Yet, changing the DVBv5 cache to the other delivery
1977                  * system should be enough for making it work.
1978                  */
1979                 switch (dvbv3_type(c->delivery_system)) {
1980                 case DVBV3_QPSK:
1981                         info->type = FE_QPSK;
1982                         break;
1983                 case DVBV3_ATSC:
1984                         info->type = FE_ATSC;
1985                         break;
1986                 case DVBV3_QAM:
1987                         info->type = FE_QAM;
1988                         break;
1989                 case DVBV3_OFDM:
1990                         info->type = FE_OFDM;
1991                         break;
1992                 default:
1993                         printk(KERN_ERR
1994                                "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1995                                __func__, c->delivery_system);
1996                         fe->ops.info.type = FE_OFDM;
1997                 }
1998                 dprintk("current delivery system on cache: %d, V3 type: %d\n",
1999                         c->delivery_system, fe->ops.info.type);
2000
2001                 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2002                  * do it, it is done for it. */
2003                 info->caps |= FE_CAN_INVERSION_AUTO;
2004                 err = 0;
2005                 break;
2006         }
2007
2008         case FE_READ_STATUS: {
2009                 fe_status_t* status = parg;
2010
2011                 /* if retune was requested but hasn't occurred yet, prevent
2012                  * that user get signal state from previous tuning */
2013                 if (fepriv->state == FESTATE_RETUNE ||
2014                     fepriv->state == FESTATE_ERROR) {
2015                         err=0;
2016                         *status = 0;
2017                         break;
2018                 }
2019
2020                 if (fe->ops.read_status)
2021                         err = fe->ops.read_status(fe, status);
2022                 break;
2023         }
2024         case FE_READ_BER:
2025                 if (fe->ops.read_ber)
2026                         err = fe->ops.read_ber(fe, (__u32*) parg);
2027                 break;
2028
2029         case FE_READ_SIGNAL_STRENGTH:
2030                 if (fe->ops.read_signal_strength)
2031                         err = fe->ops.read_signal_strength(fe, (__u16*) parg);
2032                 break;
2033
2034         case FE_READ_SNR:
2035                 if (fe->ops.read_snr)
2036                         err = fe->ops.read_snr(fe, (__u16*) parg);
2037                 break;
2038
2039         case FE_READ_UNCORRECTED_BLOCKS:
2040                 if (fe->ops.read_ucblocks)
2041                         err = fe->ops.read_ucblocks(fe, (__u32*) parg);
2042                 break;
2043
2044
2045         case FE_DISEQC_RESET_OVERLOAD:
2046                 if (fe->ops.diseqc_reset_overload) {
2047                         err = fe->ops.diseqc_reset_overload(fe);
2048                         fepriv->state = FESTATE_DISEQC;
2049                         fepriv->status = 0;
2050                 }
2051                 break;
2052
2053         case FE_DISEQC_SEND_MASTER_CMD:
2054                 if (fe->ops.diseqc_send_master_cmd) {
2055                         err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2056                         fepriv->state = FESTATE_DISEQC;
2057                         fepriv->status = 0;
2058                 }
2059                 break;
2060
2061         case FE_DISEQC_SEND_BURST:
2062                 if (fe->ops.diseqc_send_burst) {
2063                         err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2064                         fepriv->state = FESTATE_DISEQC;
2065                         fepriv->status = 0;
2066                 }
2067                 break;
2068
2069         case FE_SET_TONE:
2070                 if (fe->ops.set_tone) {
2071                         err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2072                         fepriv->tone = (fe_sec_tone_mode_t) parg;
2073                         fepriv->state = FESTATE_DISEQC;
2074                         fepriv->status = 0;
2075                 }
2076                 break;
2077
2078         case FE_SET_VOLTAGE:
2079                 if (fe->ops.set_voltage) {
2080                         err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2081                         fepriv->voltage = (fe_sec_voltage_t) parg;
2082                         fepriv->state = FESTATE_DISEQC;
2083                         fepriv->status = 0;
2084                 }
2085                 break;
2086
2087         case FE_DISHNETWORK_SEND_LEGACY_CMD:
2088                 if (fe->ops.dishnetwork_send_legacy_command) {
2089                         err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2090                         fepriv->state = FESTATE_DISEQC;
2091                         fepriv->status = 0;
2092                 } else if (fe->ops.set_voltage) {
2093                         /*
2094                          * NOTE: This is a fallback condition.  Some frontends
2095                          * (stv0299 for instance) take longer than 8msec to
2096                          * respond to a set_voltage command.  Those switches
2097                          * need custom routines to switch properly.  For all
2098                          * other frontends, the following should work ok.
2099                          * Dish network legacy switches (as used by Dish500)
2100                          * are controlled by sending 9-bit command words
2101                          * spaced 8msec apart.
2102                          * the actual command word is switch/port dependent
2103                          * so it is up to the userspace application to send
2104                          * the right command.
2105                          * The command must always start with a '0' after
2106                          * initialization, so parg is 8 bits and does not
2107                          * include the initialization or start bit
2108                          */
2109                         unsigned long swcmd = ((unsigned long) parg) << 1;
2110                         struct timeval nexttime;
2111                         struct timeval tv[10];
2112                         int i;
2113                         u8 last = 1;
2114                         if (dvb_frontend_debug)
2115                                 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2116                         do_gettimeofday(&nexttime);
2117                         if (dvb_frontend_debug)
2118                                 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
2119                         /* before sending a command, initialize by sending
2120                          * a 32ms 18V to the switch
2121                          */
2122                         fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2123                         dvb_frontend_sleep_until(&nexttime, 32000);
2124
2125                         for (i = 0; i < 9; i++) {
2126                                 if (dvb_frontend_debug)
2127                                         do_gettimeofday(&tv[i + 1]);
2128                                 if ((swcmd & 0x01) != last) {
2129                                         /* set voltage to (last ? 13V : 18V) */
2130                                         fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2131                                         last = (last) ? 0 : 1;
2132                                 }
2133                                 swcmd = swcmd >> 1;
2134                                 if (i != 8)
2135                                         dvb_frontend_sleep_until(&nexttime, 8000);
2136                         }
2137                         if (dvb_frontend_debug) {
2138                                 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2139                                         __func__, fe->dvb->num);
2140                                 for (i = 1; i < 10; i++)
2141                                         printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2142                         }
2143                         err = 0;
2144                         fepriv->state = FESTATE_DISEQC;
2145                         fepriv->status = 0;
2146                 }
2147                 break;
2148
2149         case FE_DISEQC_RECV_SLAVE_REPLY:
2150                 if (fe->ops.diseqc_recv_slave_reply)
2151                         err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2152                 break;
2153
2154         case FE_ENABLE_HIGH_LNB_VOLTAGE:
2155                 if (fe->ops.enable_high_lnb_voltage)
2156                         err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2157                 break;
2158
2159         case FE_SET_FRONTEND:
2160                 err = set_delivery_system(fe, SYS_UNDEFINED);
2161                 if (err)
2162                         break;
2163
2164                 err = dtv_property_cache_sync(fe, c, parg);
2165                 if (err)
2166                         break;
2167                 err = dtv_set_frontend(fe);
2168                 break;
2169         case FE_GET_EVENT:
2170                 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2171                 break;
2172
2173         case FE_GET_FRONTEND:
2174                 err = dtv_get_frontend(fe, parg);
2175                 break;
2176
2177         case FE_SET_FRONTEND_TUNE_MODE:
2178                 fepriv->tune_mode_flags = (unsigned long) parg;
2179                 err = 0;
2180                 break;
2181         };
2182
2183         if (fe->dvb->fe_ioctl_override) {
2184                 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
2185                                                     DVB_FE_IOCTL_POST);
2186                 if (cb_err < 0)
2187                         return cb_err;
2188         }
2189
2190         return err;
2191 }
2192
2193
2194 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2195 {
2196         struct dvb_device *dvbdev = file->private_data;
2197         struct dvb_frontend *fe = dvbdev->priv;
2198         struct dvb_frontend_private *fepriv = fe->frontend_priv;
2199
2200         dprintk ("%s\n", __func__);
2201
2202         poll_wait (file, &fepriv->events.wait_queue, wait);
2203
2204         if (fepriv->events.eventw != fepriv->events.eventr)
2205                 return (POLLIN | POLLRDNORM | POLLPRI);
2206
2207         return 0;
2208 }
2209
2210 static int dvb_frontend_open(struct inode *inode, struct file *file)
2211 {
2212         struct dvb_device *dvbdev = file->private_data;
2213         struct dvb_frontend *fe = dvbdev->priv;
2214         struct dvb_frontend_private *fepriv = fe->frontend_priv;
2215         struct dvb_adapter *adapter = fe->dvb;
2216         int ret;
2217
2218         dprintk ("%s\n", __func__);
2219         if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2220                 return -ENODEV;
2221
2222         if (adapter->mfe_shared) {
2223                 mutex_lock (&adapter->mfe_lock);
2224
2225                 if (adapter->mfe_dvbdev == NULL)
2226                         adapter->mfe_dvbdev = dvbdev;
2227
2228                 else if (adapter->mfe_dvbdev != dvbdev) {
2229                         struct dvb_device
2230                                 *mfedev = adapter->mfe_dvbdev;
2231                         struct dvb_frontend
2232                                 *mfe = mfedev->priv;
2233                         struct dvb_frontend_private
2234                                 *mfepriv = mfe->frontend_priv;
2235                         int mferetry = (dvb_mfe_wait_time << 1);
2236
2237                         mutex_unlock (&adapter->mfe_lock);
2238                         while (mferetry-- && (mfedev->users != -1 ||
2239                                         mfepriv->thread != NULL)) {
2240                                 if(msleep_interruptible(500)) {
2241                                         if(signal_pending(current))
2242                                                 return -EINTR;
2243                                 }
2244                         }
2245
2246                         mutex_lock (&adapter->mfe_lock);
2247                         if(adapter->mfe_dvbdev != dvbdev) {
2248                                 mfedev = adapter->mfe_dvbdev;
2249                                 mfe = mfedev->priv;
2250                                 mfepriv = mfe->frontend_priv;
2251                                 if (mfedev->users != -1 ||
2252                                                 mfepriv->thread != NULL) {
2253                                         mutex_unlock (&adapter->mfe_lock);
2254                                         return -EBUSY;
2255                                 }
2256                                 adapter->mfe_dvbdev = dvbdev;
2257                         }
2258                 }
2259         }
2260
2261         if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2262                 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2263                         goto err0;
2264
2265                 /* If we took control of the bus, we need to force
2266                    reinitialization.  This is because many ts_bus_ctrl()
2267                    functions strobe the RESET pin on the demod, and if the
2268                    frontend thread already exists then the dvb_init() routine
2269                    won't get called (which is what usually does initial
2270                    register configuration). */
2271                 fepriv->reinitialise = 1;
2272         }
2273
2274         if ((ret = dvb_generic_open (inode, file)) < 0)
2275                 goto err1;
2276
2277         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2278                 /* normal tune mode when opened R/W */
2279                 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2280                 fepriv->tone = -1;
2281                 fepriv->voltage = -1;
2282
2283                 ret = dvb_frontend_start (fe);
2284                 if (ret)
2285                         goto err2;
2286
2287                 /*  empty event queue */
2288                 fepriv->events.eventr = fepriv->events.eventw = 0;
2289         }
2290
2291         if (adapter->mfe_shared)
2292                 mutex_unlock (&adapter->mfe_lock);
2293         return ret;
2294
2295 err2:
2296         dvb_generic_release(inode, file);
2297 err1:
2298         if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2299                 fe->ops.ts_bus_ctrl(fe, 0);
2300 err0:
2301         if (adapter->mfe_shared)
2302                 mutex_unlock (&adapter->mfe_lock);
2303         return ret;
2304 }
2305
2306 static int dvb_frontend_release(struct inode *inode, struct file *file)
2307 {
2308         struct dvb_device *dvbdev = file->private_data;
2309         struct dvb_frontend *fe = dvbdev->priv;
2310         struct dvb_frontend_private *fepriv = fe->frontend_priv;
2311         int ret;
2312
2313         dprintk ("%s\n", __func__);
2314
2315         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2316                 fepriv->release_jiffies = jiffies;
2317                 mb();
2318         }
2319
2320         ret = dvb_generic_release (inode, file);
2321
2322         if (dvbdev->users == -1) {
2323                 wake_up(&fepriv->wait_queue);
2324                 if (fepriv->exit != DVB_FE_NO_EXIT) {
2325                         fops_put(file->f_op);
2326                         file->f_op = NULL;
2327                         wake_up(&dvbdev->wait_queue);
2328                 }
2329                 if (fe->ops.ts_bus_ctrl)
2330                         fe->ops.ts_bus_ctrl(fe, 0);
2331         }
2332
2333         return ret;
2334 }
2335
2336 static const struct file_operations dvb_frontend_fops = {
2337         .owner          = THIS_MODULE,
2338         .unlocked_ioctl = dvb_generic_ioctl,
2339         .poll           = dvb_frontend_poll,
2340         .open           = dvb_frontend_open,
2341         .release        = dvb_frontend_release,
2342         .llseek         = noop_llseek,
2343 };
2344
2345 int dvb_register_frontend(struct dvb_adapter* dvb,
2346                           struct dvb_frontend* fe)
2347 {
2348         struct dvb_frontend_private *fepriv;
2349         static const struct dvb_device dvbdev_template = {
2350                 .users = ~0,
2351                 .writers = 1,
2352                 .readers = (~0)-1,
2353                 .fops = &dvb_frontend_fops,
2354                 .kernel_ioctl = dvb_frontend_ioctl
2355         };
2356
2357         dprintk ("%s\n", __func__);
2358
2359         if (mutex_lock_interruptible(&frontend_mutex))
2360                 return -ERESTARTSYS;
2361
2362         fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2363         if (fe->frontend_priv == NULL) {
2364                 mutex_unlock(&frontend_mutex);
2365                 return -ENOMEM;
2366         }
2367         fepriv = fe->frontend_priv;
2368
2369         sema_init(&fepriv->sem, 1);
2370         init_waitqueue_head (&fepriv->wait_queue);
2371         init_waitqueue_head (&fepriv->events.wait_queue);
2372         mutex_init(&fepriv->events.mtx);
2373         fe->dvb = dvb;
2374         fepriv->inversion = INVERSION_OFF;
2375
2376         printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2377                 fe->dvb->num,
2378                 fe->id,
2379                 fe->ops.info.name);
2380
2381         dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2382                              fe, DVB_DEVICE_FRONTEND);
2383
2384         /*
2385          * Initialize the cache to the proper values according with the
2386          * first supported delivery system (ops->delsys[0])
2387          */
2388
2389         fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2390         dvb_frontend_clear_cache(fe);
2391
2392         mutex_unlock(&frontend_mutex);
2393         return 0;
2394 }
2395 EXPORT_SYMBOL(dvb_register_frontend);
2396
2397 int dvb_unregister_frontend(struct dvb_frontend* fe)
2398 {
2399         struct dvb_frontend_private *fepriv = fe->frontend_priv;
2400         dprintk ("%s\n", __func__);
2401
2402         mutex_lock(&frontend_mutex);
2403         dvb_frontend_stop (fe);
2404         mutex_unlock(&frontend_mutex);
2405
2406         if (fepriv->dvbdev->users < -1)
2407                 wait_event(fepriv->dvbdev->wait_queue,
2408                                 fepriv->dvbdev->users==-1);
2409
2410         mutex_lock(&frontend_mutex);
2411         dvb_unregister_device (fepriv->dvbdev);
2412
2413         /* fe is invalid now */
2414         kfree(fepriv);
2415         mutex_unlock(&frontend_mutex);
2416         return 0;
2417 }
2418 EXPORT_SYMBOL(dvb_unregister_frontend);
2419
2420 #ifdef CONFIG_MEDIA_ATTACH
2421 void dvb_frontend_detach(struct dvb_frontend* fe)
2422 {
2423         void *ptr;
2424
2425         if (fe->ops.release_sec) {
2426                 fe->ops.release_sec(fe);
2427                 symbol_put_addr(fe->ops.release_sec);
2428         }
2429         if (fe->ops.tuner_ops.release) {
2430                 fe->ops.tuner_ops.release(fe);
2431                 symbol_put_addr(fe->ops.tuner_ops.release);
2432         }
2433         if (fe->ops.analog_ops.release) {
2434                 fe->ops.analog_ops.release(fe);
2435                 symbol_put_addr(fe->ops.analog_ops.release);
2436         }
2437         ptr = (void*)fe->ops.release;
2438         if (ptr) {
2439                 fe->ops.release(fe);
2440                 symbol_put_addr(ptr);
2441         }
2442 }
2443 #else
2444 void dvb_frontend_detach(struct dvb_frontend* fe)
2445 {
2446         if (fe->ops.release_sec)
2447                 fe->ops.release_sec(fe);
2448         if (fe->ops.tuner_ops.release)
2449                 fe->ops.tuner_ops.release(fe);
2450         if (fe->ops.analog_ops.release)
2451                 fe->ops.analog_ops.release(fe);
2452         if (fe->ops.release)
2453                 fe->ops.release(fe);
2454 }
2455 #endif
2456 EXPORT_SYMBOL(dvb_frontend_detach);