72ce7935f2539182120a47fa67069c81b3502d18
[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 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/wait.h>
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/module.h>
35 #include <linux/list.h>
36 #include <linux/freezer.h>
37 #include <linux/jiffies.h>
38 #include <linux/kthread.h>
39 #include <asm/processor.h>
40
41 #include "dvb_frontend.h"
42 #include "dvbdev.h"
43 #include <linux/dvb/version.h>
44
45 static int dvb_frontend_debug;
46 static int dvb_shutdown_timeout;
47 static int dvb_force_auto_inversion;
48 static int dvb_override_tune_delay;
49 static int dvb_powerdown_on_sleep = 1;
50
51 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
52 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
53 module_param(dvb_shutdown_timeout, int, 0644);
54 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
55 module_param(dvb_force_auto_inversion, int, 0644);
56 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
57 module_param(dvb_override_tune_delay, int, 0644);
58 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
59 module_param(dvb_powerdown_on_sleep, int, 0644);
60 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
61
62 #define dprintk if (dvb_frontend_debug) printk
63
64 #define FESTATE_IDLE 1
65 #define FESTATE_RETUNE 2
66 #define FESTATE_TUNING_FAST 4
67 #define FESTATE_TUNING_SLOW 8
68 #define FESTATE_TUNED 16
69 #define FESTATE_ZIGZAG_FAST 32
70 #define FESTATE_ZIGZAG_SLOW 64
71 #define FESTATE_DISEQC 128
72 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
73 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
74 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
75 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
76
77 #define FE_ALGO_HW              1
78 /*
79  * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
80  * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
81  * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
82  * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
83  * FESTATE_TUNED. The frontend has successfully locked on.
84  * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
85  * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
86  * FESTATE_DISEQC. A DISEQC command has just been issued.
87  * FESTATE_WAITFORLOCK. When we're waiting for a lock.
88  * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
89  * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
90  * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
91  */
92
93 static DEFINE_MUTEX(frontend_mutex);
94
95 struct dvb_frontend_private {
96
97         /* thread/frontend values */
98         struct dvb_device *dvbdev;
99         struct dvb_frontend_parameters parameters;
100         struct dvb_fe_events events;
101         struct semaphore sem;
102         struct list_head list_head;
103         wait_queue_head_t wait_queue;
104         struct task_struct *thread;
105         unsigned long release_jiffies;
106         unsigned int exit;
107         unsigned int wakeup;
108         fe_status_t status;
109         unsigned long tune_mode_flags;
110         unsigned int delay;
111         unsigned int reinitialise;
112         int tone;
113         int voltage;
114
115         /* swzigzag values */
116         unsigned int state;
117         unsigned int bending;
118         int lnb_drift;
119         unsigned int inversion;
120         unsigned int auto_step;
121         unsigned int auto_sub_step;
122         unsigned int started_auto_step;
123         unsigned int min_delay;
124         unsigned int max_drift;
125         unsigned int step_size;
126         int quality;
127         unsigned int check_wrapped;
128 };
129
130 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
131
132 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
133 {
134         struct dvb_frontend_private *fepriv = fe->frontend_priv;
135         struct dvb_fe_events *events = &fepriv->events;
136         struct dvb_frontend_event *e;
137         int wp;
138
139         dprintk ("%s\n", __func__);
140
141         if (mutex_lock_interruptible (&events->mtx))
142                 return;
143
144         wp = (events->eventw + 1) % MAX_EVENT;
145
146         if (wp == events->eventr) {
147                 events->overflow = 1;
148                 events->eventr = (events->eventr + 1) % MAX_EVENT;
149         }
150
151         e = &events->events[events->eventw];
152
153         memcpy (&e->parameters, &fepriv->parameters,
154                 sizeof (struct dvb_frontend_parameters));
155
156         if (status & FE_HAS_LOCK)
157                 if (fe->ops.get_frontend)
158                         fe->ops.get_frontend(fe, &e->parameters);
159
160         events->eventw = wp;
161
162         mutex_unlock(&events->mtx);
163
164         e->status = status;
165
166         wake_up_interruptible (&events->wait_queue);
167 }
168
169 static int dvb_frontend_get_event(struct dvb_frontend *fe,
170                             struct dvb_frontend_event *event, int flags)
171 {
172         struct dvb_frontend_private *fepriv = fe->frontend_priv;
173         struct dvb_fe_events *events = &fepriv->events;
174
175         dprintk ("%s\n", __func__);
176
177         if (events->overflow) {
178                 events->overflow = 0;
179                 return -EOVERFLOW;
180         }
181
182         if (events->eventw == events->eventr) {
183                 int ret;
184
185                 if (flags & O_NONBLOCK)
186                         return -EWOULDBLOCK;
187
188                 up(&fepriv->sem);
189
190                 ret = wait_event_interruptible (events->wait_queue,
191                                                 events->eventw != events->eventr);
192
193                 if (down_interruptible (&fepriv->sem))
194                         return -ERESTARTSYS;
195
196                 if (ret < 0)
197                         return ret;
198         }
199
200         if (mutex_lock_interruptible (&events->mtx))
201                 return -ERESTARTSYS;
202
203         memcpy (event, &events->events[events->eventr],
204                 sizeof(struct dvb_frontend_event));
205
206         events->eventr = (events->eventr + 1) % MAX_EVENT;
207
208         mutex_unlock(&events->mtx);
209
210         return 0;
211 }
212
213 static void dvb_frontend_init(struct dvb_frontend *fe)
214 {
215         dprintk ("DVB: initialising frontend %i (%s)...\n",
216                  fe->dvb->num,
217                  fe->ops.info.name);
218
219         if (fe->ops.init)
220                 fe->ops.init(fe);
221         if (fe->ops.tuner_ops.init) {
222                 fe->ops.tuner_ops.init(fe);
223                 if (fe->ops.i2c_gate_ctrl)
224                         fe->ops.i2c_gate_ctrl(fe, 0);
225         }
226 }
227
228 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
229 {
230         struct dvb_frontend_private *fepriv = fe->frontend_priv;
231
232         fepriv->reinitialise = 1;
233         dvb_frontend_wakeup(fe);
234 }
235 EXPORT_SYMBOL(dvb_frontend_reinitialise);
236
237 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
238 {
239         int q2;
240
241         dprintk ("%s\n", __func__);
242
243         if (locked)
244                 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
245         else
246                 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
247
248         q2 = fepriv->quality - 128;
249         q2 *= q2;
250
251         fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
252 }
253
254 /**
255  * Performs automatic twiddling of frontend parameters.
256  *
257  * @param fe The frontend concerned.
258  * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
259  * @returns Number of complete iterations that have been performed.
260  */
261 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
262 {
263         int autoinversion;
264         int ready = 0;
265         struct dvb_frontend_private *fepriv = fe->frontend_priv;
266         int original_inversion = fepriv->parameters.inversion;
267         u32 original_frequency = fepriv->parameters.frequency;
268
269         /* are we using autoinversion? */
270         autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
271                          (fepriv->parameters.inversion == INVERSION_AUTO));
272
273         /* setup parameters correctly */
274         while(!ready) {
275                 /* calculate the lnb_drift */
276                 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
277
278                 /* wrap the auto_step if we've exceeded the maximum drift */
279                 if (fepriv->lnb_drift > fepriv->max_drift) {
280                         fepriv->auto_step = 0;
281                         fepriv->auto_sub_step = 0;
282                         fepriv->lnb_drift = 0;
283                 }
284
285                 /* perform inversion and +/- zigzag */
286                 switch(fepriv->auto_sub_step) {
287                 case 0:
288                         /* try with the current inversion and current drift setting */
289                         ready = 1;
290                         break;
291
292                 case 1:
293                         if (!autoinversion) break;
294
295                         fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
296                         ready = 1;
297                         break;
298
299                 case 2:
300                         if (fepriv->lnb_drift == 0) break;
301
302                         fepriv->lnb_drift = -fepriv->lnb_drift;
303                         ready = 1;
304                         break;
305
306                 case 3:
307                         if (fepriv->lnb_drift == 0) break;
308                         if (!autoinversion) break;
309
310                         fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
311                         fepriv->lnb_drift = -fepriv->lnb_drift;
312                         ready = 1;
313                         break;
314
315                 default:
316                         fepriv->auto_step++;
317                         fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
318                         break;
319                 }
320
321                 if (!ready) fepriv->auto_sub_step++;
322         }
323
324         /* if this attempt would hit where we started, indicate a complete
325          * iteration has occurred */
326         if ((fepriv->auto_step == fepriv->started_auto_step) &&
327             (fepriv->auto_sub_step == 0) && check_wrapped) {
328                 return 1;
329         }
330
331         dprintk("%s: drift:%i inversion:%i auto_step:%i "
332                 "auto_sub_step:%i started_auto_step:%i\n",
333                 __func__, fepriv->lnb_drift, fepriv->inversion,
334                 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
335
336         /* set the frontend itself */
337         fepriv->parameters.frequency += fepriv->lnb_drift;
338         if (autoinversion)
339                 fepriv->parameters.inversion = fepriv->inversion;
340         if (fe->ops.set_frontend)
341                 fe->ops.set_frontend(fe, &fepriv->parameters);
342
343         fepriv->parameters.frequency = original_frequency;
344         fepriv->parameters.inversion = original_inversion;
345
346         fepriv->auto_sub_step++;
347         return 0;
348 }
349
350 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
351 {
352         fe_status_t s = 0;
353         struct dvb_frontend_private *fepriv = fe->frontend_priv;
354
355         /* if we've got no parameters, just keep idling */
356         if (fepriv->state & FESTATE_IDLE) {
357                 fepriv->delay = 3*HZ;
358                 fepriv->quality = 0;
359                 return;
360         }
361
362         /* in SCAN mode, we just set the frontend when asked and leave it alone */
363         if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
364                 if (fepriv->state & FESTATE_RETUNE) {
365                         if (fe->ops.set_frontend)
366                                 fe->ops.set_frontend(fe, &fepriv->parameters);
367                         fepriv->state = FESTATE_TUNED;
368                 }
369                 fepriv->delay = 3*HZ;
370                 fepriv->quality = 0;
371                 return;
372         }
373
374         /* get the frontend status */
375         if (fepriv->state & FESTATE_RETUNE) {
376                 s = 0;
377         } else {
378                 if (fe->ops.read_status)
379                         fe->ops.read_status(fe, &s);
380                 if (s != fepriv->status) {
381                         dvb_frontend_add_event(fe, s);
382                         fepriv->status = s;
383                 }
384         }
385
386         /* if we're not tuned, and we have a lock, move to the TUNED state */
387         if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
388                 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
389                 fepriv->state = FESTATE_TUNED;
390
391                 /* if we're tuned, then we have determined the correct inversion */
392                 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
393                     (fepriv->parameters.inversion == INVERSION_AUTO)) {
394                         fepriv->parameters.inversion = fepriv->inversion;
395                 }
396                 return;
397         }
398
399         /* if we are tuned already, check we're still locked */
400         if (fepriv->state & FESTATE_TUNED) {
401                 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
402
403                 /* we're tuned, and the lock is still good... */
404                 if (s & FE_HAS_LOCK) {
405                         return;
406                 } else { /* if we _WERE_ tuned, but now don't have a lock */
407                         fepriv->state = FESTATE_ZIGZAG_FAST;
408                         fepriv->started_auto_step = fepriv->auto_step;
409                         fepriv->check_wrapped = 0;
410                 }
411         }
412
413         /* don't actually do anything if we're in the LOSTLOCK state,
414          * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
415         if ((fepriv->state & FESTATE_LOSTLOCK) &&
416             (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
417                 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
418                 return;
419         }
420
421         /* don't do anything if we're in the DISEQC state, since this
422          * might be someone with a motorized dish controlled by DISEQC.
423          * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
424         if (fepriv->state & FESTATE_DISEQC) {
425                 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
426                 return;
427         }
428
429         /* if we're in the RETUNE state, set everything up for a brand
430          * new scan, keeping the current inversion setting, as the next
431          * tune is _very_ likely to require the same */
432         if (fepriv->state & FESTATE_RETUNE) {
433                 fepriv->lnb_drift = 0;
434                 fepriv->auto_step = 0;
435                 fepriv->auto_sub_step = 0;
436                 fepriv->started_auto_step = 0;
437                 fepriv->check_wrapped = 0;
438         }
439
440         /* fast zigzag. */
441         if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
442                 fepriv->delay = fepriv->min_delay;
443
444                 /* peform a tune */
445                 if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) {
446                         /* OK, if we've run out of trials at the fast speed.
447                          * Drop back to slow for the _next_ attempt */
448                         fepriv->state = FESTATE_SEARCHING_SLOW;
449                         fepriv->started_auto_step = fepriv->auto_step;
450                         return;
451                 }
452                 fepriv->check_wrapped = 1;
453
454                 /* if we've just retuned, enter the ZIGZAG_FAST state.
455                  * This ensures we cannot return from an
456                  * FE_SET_FRONTEND ioctl before the first frontend tune
457                  * occurs */
458                 if (fepriv->state & FESTATE_RETUNE) {
459                         fepriv->state = FESTATE_TUNING_FAST;
460                 }
461         }
462
463         /* slow zigzag */
464         if (fepriv->state & FESTATE_SEARCHING_SLOW) {
465                 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
466
467                 /* Note: don't bother checking for wrapping; we stay in this
468                  * state until we get a lock */
469                 dvb_frontend_swzigzag_autotune(fe, 0);
470         }
471 }
472
473 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
474 {
475         struct dvb_frontend_private *fepriv = fe->frontend_priv;
476
477         if (fepriv->exit)
478                 return 1;
479
480         if (fepriv->dvbdev->writers == 1)
481                 if (time_after(jiffies, fepriv->release_jiffies +
482                                   dvb_shutdown_timeout * HZ))
483                         return 1;
484
485         return 0;
486 }
487
488 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
489 {
490         struct dvb_frontend_private *fepriv = fe->frontend_priv;
491
492         if (fepriv->wakeup) {
493                 fepriv->wakeup = 0;
494                 return 1;
495         }
496         return dvb_frontend_is_exiting(fe);
497 }
498
499 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
500 {
501         struct dvb_frontend_private *fepriv = fe->frontend_priv;
502
503         fepriv->wakeup = 1;
504         wake_up_interruptible(&fepriv->wait_queue);
505 }
506
507 static int dvb_frontend_thread(void *data)
508 {
509         struct dvb_frontend *fe = data;
510         struct dvb_frontend_private *fepriv = fe->frontend_priv;
511         unsigned long timeout;
512         fe_status_t s;
513         struct dvb_frontend_parameters *params;
514
515         dprintk("%s\n", __func__);
516
517         fepriv->check_wrapped = 0;
518         fepriv->quality = 0;
519         fepriv->delay = 3*HZ;
520         fepriv->status = 0;
521         fepriv->wakeup = 0;
522         fepriv->reinitialise = 0;
523
524         dvb_frontend_init(fe);
525
526         set_freezable();
527         while (1) {
528                 up(&fepriv->sem);           /* is locked when we enter the thread... */
529 restart:
530                 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
531                         dvb_frontend_should_wakeup(fe) || kthread_should_stop()
532                                 || freezing(current),
533                         fepriv->delay);
534
535                 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
536                         /* got signal or quitting */
537                         break;
538                 }
539
540                 if (try_to_freeze())
541                         goto restart;
542
543                 if (down_interruptible(&fepriv->sem))
544                         break;
545
546                 if (fepriv->reinitialise) {
547                         dvb_frontend_init(fe);
548                         if (fepriv->tone != -1) {
549                                 fe->ops.set_tone(fe, fepriv->tone);
550                         }
551                         if (fepriv->voltage != -1) {
552                                 fe->ops.set_voltage(fe, fepriv->voltage);
553                         }
554                         fepriv->reinitialise = 0;
555                 }
556
557                 /* do an iteration of the tuning loop */
558                 if (fe->ops.get_frontend_algo) {
559                         if (fe->ops.get_frontend_algo(fe) == FE_ALGO_HW) {
560                                 /* have we been asked to retune? */
561                                 params = NULL;
562                                 if (fepriv->state & FESTATE_RETUNE) {
563                                         params = &fepriv->parameters;
564                                         fepriv->state = FESTATE_TUNED;
565                                 }
566
567                                 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
568                                 if (s != fepriv->status) {
569                                         dvb_frontend_add_event(fe, s);
570                                         fepriv->status = s;
571                                 }
572                         } else
573                                 dvb_frontend_swzigzag(fe);
574                 } else
575                         dvb_frontend_swzigzag(fe);
576         }
577
578         if (dvb_powerdown_on_sleep) {
579                 if (fe->ops.set_voltage)
580                         fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
581                 if (fe->ops.tuner_ops.sleep) {
582                         fe->ops.tuner_ops.sleep(fe);
583                         if (fe->ops.i2c_gate_ctrl)
584                                 fe->ops.i2c_gate_ctrl(fe, 0);
585                 }
586                 if (fe->ops.sleep)
587                         fe->ops.sleep(fe);
588         }
589
590         fepriv->thread = NULL;
591         mb();
592
593         dvb_frontend_wakeup(fe);
594         return 0;
595 }
596
597 static void dvb_frontend_stop(struct dvb_frontend *fe)
598 {
599         struct dvb_frontend_private *fepriv = fe->frontend_priv;
600
601         dprintk ("%s\n", __func__);
602
603         fepriv->exit = 1;
604         mb();
605
606         if (!fepriv->thread)
607                 return;
608
609         kthread_stop(fepriv->thread);
610
611         init_MUTEX (&fepriv->sem);
612         fepriv->state = FESTATE_IDLE;
613
614         /* paranoia check in case a signal arrived */
615         if (fepriv->thread)
616                 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
617                                 fepriv->thread);
618 }
619
620 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
621 {
622         return ((curtime.tv_usec < lasttime.tv_usec) ?
623                 1000000 - lasttime.tv_usec + curtime.tv_usec :
624                 curtime.tv_usec - lasttime.tv_usec);
625 }
626 EXPORT_SYMBOL(timeval_usec_diff);
627
628 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
629 {
630         curtime->tv_usec += add_usec;
631         if (curtime->tv_usec >= 1000000) {
632                 curtime->tv_usec -= 1000000;
633                 curtime->tv_sec++;
634         }
635 }
636
637 /*
638  * Sleep until gettimeofday() > waketime + add_usec
639  * This needs to be as precise as possible, but as the delay is
640  * usually between 2ms and 32ms, it is done using a scheduled msleep
641  * followed by usleep (normally a busy-wait loop) for the remainder
642  */
643 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
644 {
645         struct timeval lasttime;
646         s32 delta, newdelta;
647
648         timeval_usec_add(waketime, add_usec);
649
650         do_gettimeofday(&lasttime);
651         delta = timeval_usec_diff(lasttime, *waketime);
652         if (delta > 2500) {
653                 msleep((delta - 1500) / 1000);
654                 do_gettimeofday(&lasttime);
655                 newdelta = timeval_usec_diff(lasttime, *waketime);
656                 delta = (newdelta > delta) ? 0 : newdelta;
657         }
658         if (delta > 0)
659                 udelay(delta);
660 }
661 EXPORT_SYMBOL(dvb_frontend_sleep_until);
662
663 static int dvb_frontend_start(struct dvb_frontend *fe)
664 {
665         int ret;
666         struct dvb_frontend_private *fepriv = fe->frontend_priv;
667         struct task_struct *fe_thread;
668
669         dprintk ("%s\n", __func__);
670
671         if (fepriv->thread) {
672                 if (!fepriv->exit)
673                         return 0;
674                 else
675                         dvb_frontend_stop (fe);
676         }
677
678         if (signal_pending(current))
679                 return -EINTR;
680         if (down_interruptible (&fepriv->sem))
681                 return -EINTR;
682
683         fepriv->state = FESTATE_IDLE;
684         fepriv->exit = 0;
685         fepriv->thread = NULL;
686         mb();
687
688         fe_thread = kthread_run(dvb_frontend_thread, fe,
689                 "kdvb-fe-%i", fe->dvb->num);
690         if (IS_ERR(fe_thread)) {
691                 ret = PTR_ERR(fe_thread);
692                 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
693                 up(&fepriv->sem);
694                 return ret;
695         }
696         fepriv->thread = fe_thread;
697         return 0;
698 }
699
700 static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
701                                         u32 *freq_min, u32 *freq_max)
702 {
703         *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
704
705         if (fe->ops.info.frequency_max == 0)
706                 *freq_max = fe->ops.tuner_ops.info.frequency_max;
707         else if (fe->ops.tuner_ops.info.frequency_max == 0)
708                 *freq_max = fe->ops.info.frequency_max;
709         else
710                 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
711
712         if (*freq_min == 0 || *freq_max == 0)
713                 printk(KERN_WARNING "DVB: frontend %u frequency limits undefined - fix the driver\n",
714                        fe->dvb->num);
715 }
716
717 static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
718                                 struct dvb_frontend_parameters *parms)
719 {
720         u32 freq_min;
721         u32 freq_max;
722
723         /* range check: frequency */
724         dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
725         if ((freq_min && parms->frequency < freq_min) ||
726             (freq_max && parms->frequency > freq_max)) {
727                 printk(KERN_WARNING "DVB: frontend %u frequency %u out of range (%u..%u)\n",
728                        fe->dvb->num, parms->frequency, freq_min, freq_max);
729                 return -EINVAL;
730         }
731
732         /* range check: symbol rate */
733         if (fe->ops.info.type == FE_QPSK) {
734                 if ((fe->ops.info.symbol_rate_min &&
735                      parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
736                     (fe->ops.info.symbol_rate_max &&
737                      parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
738                         printk(KERN_WARNING "DVB: frontend %u symbol rate %u out of range (%u..%u)\n",
739                                fe->dvb->num, parms->u.qpsk.symbol_rate,
740                                fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
741                         return -EINVAL;
742                 }
743
744         } else if (fe->ops.info.type == FE_QAM) {
745                 if ((fe->ops.info.symbol_rate_min &&
746                      parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
747                     (fe->ops.info.symbol_rate_max &&
748                      parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
749                         printk(KERN_WARNING "DVB: frontend %u symbol rate %u out of range (%u..%u)\n",
750                                fe->dvb->num, parms->u.qam.symbol_rate,
751                                fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
752                         return -EINVAL;
753                 }
754         }
755
756         return 0;
757 }
758
759 struct dtv_cmds_h dtv_cmds[] = {
760         [DTV_TUNE] = {
761                 .name   = "DTV_TUNE",
762                 .cmd    = DTV_TUNE,
763                 .set    = 1,
764         },
765         [DTV_CLEAR] = {
766                 .name   = "DTV_CLEAR",
767                 .cmd    = DTV_CLEAR,
768                 .set    = 1,
769         },
770
771         /* Set */
772         [DTV_FREQUENCY] = {
773                 .name   = "DTV_FREQUENCY",
774                 .cmd    = DTV_FREQUENCY,
775                 .set    = 1,
776         },
777         [DTV_BANDWIDTH_HZ] = {
778                 .name   = "DTV_BANDWIDTH_HZ",
779                 .cmd    = DTV_BANDWIDTH_HZ,
780                 .set    = 1,
781         },
782         [DTV_MODULATION] = {
783                 .name   = "DTV_MODULATION",
784                 .cmd    = DTV_MODULATION,
785                 .set    = 1,
786         },
787         [DTV_INVERSION] = {
788                 .name   = "DTV_INVERSION",
789                 .cmd    = DTV_INVERSION,
790                 .set    = 1,
791         },
792         [DTV_DISEQC_MASTER] = {
793                 .name   = "DTV_DISEQC_MASTER",
794                 .cmd    = DTV_DISEQC_MASTER,
795                 .set    = 1,
796                 .buffer = 1,
797         },
798         [DTV_SYMBOL_RATE] = {
799                 .name   = "DTV_SYMBOL_RATE",
800                 .cmd    = DTV_SYMBOL_RATE,
801                 .set    = 1,
802         },
803         [DTV_INNER_FEC] = {
804                 .name   = "DTV_INNER_FEC",
805                 .cmd    = DTV_INNER_FEC,
806                 .set    = 1,
807         },
808         [DTV_VOLTAGE] = {
809                 .name   = "DTV_VOLTAGE",
810                 .cmd    = DTV_VOLTAGE,
811                 .set    = 1,
812         },
813         [DTV_TONE] = {
814                 .name   = "DTV_TONE",
815                 .cmd    = DTV_TONE,
816                 .set    = 1,
817         },
818         [DTV_PILOT] = {
819                 .name   = "DTV_PILOT",
820                 .cmd    = DTV_PILOT,
821                 .set    = 1,
822         },
823         [DTV_ROLLOFF] = {
824                 .name   = "DTV_ROLLOFF",
825                 .cmd    = DTV_ROLLOFF,
826                 .set    = 1,
827         },
828         [DTV_DELIVERY_SYSTEM] = {
829                 .name   = "DTV_DELIVERY_SYSTEM",
830                 .cmd    = DTV_DELIVERY_SYSTEM,
831                 .set    = 1,
832         },
833         [DTV_HIERARCHY] = {
834                 .name   = "DTV_HIERARCHY",
835                 .cmd    = DTV_HIERARCHY,
836                 .set    = 1,
837         },
838         [DTV_CODE_RATE_HP] = {
839                 .name   = "DTV_CODE_RATE_HP",
840                 .cmd    = DTV_CODE_RATE_HP,
841                 .set    = 1,
842         },
843         [DTV_CODE_RATE_LP] = {
844                 .name   = "DTV_CODE_RATE_LP",
845                 .cmd    = DTV_CODE_RATE_LP,
846                 .set    = 1,
847         },
848         [DTV_GUARD_INTERVAL] = {
849                 .name   = "DTV_GUARD_INTERVAL",
850                 .cmd    = DTV_GUARD_INTERVAL,
851                 .set    = 1,
852         },
853         [DTV_TRANSMISSION_MODE] = {
854                 .name   = "DTV_TRANSMISSION_MODE",
855                 .cmd    = DTV_TRANSMISSION_MODE,
856                 .set    = 1,
857         },
858         /* Get */
859         [DTV_DISEQC_SLAVE_REPLY] = {
860                 .name   = "DTV_DISEQC_SLAVE_REPLY",
861                 .cmd    = DTV_DISEQC_SLAVE_REPLY,
862                 .set    = 0,
863                 .buffer = 1,
864         },
865         [DTV_API_VERSION] = {
866                 .name   = "DTV_API_VERSION",
867                 .cmd    = DTV_API_VERSION,
868                 .set    = 0,
869         },
870         [DTV_CODE_RATE_HP] = {
871                 .name   = "DTV_CODE_RATE_HP",
872                 .cmd    = DTV_CODE_RATE_HP,
873                 .set    = 0,
874         },
875         [DTV_CODE_RATE_LP] = {
876                 .name   = "DTV_CODE_RATE_LP",
877                 .cmd    = DTV_CODE_RATE_LP,
878                 .set    = 0,
879         },
880         [DTV_GUARD_INTERVAL] = {
881                 .name   = "DTV_GUARD_INTERVAL",
882                 .cmd    = DTV_GUARD_INTERVAL,
883                 .set    = 0,
884         },
885         [DTV_TRANSMISSION_MODE] = {
886                 .name   = "DTV_TRANSMISSION_MODE",
887                 .cmd    = DTV_TRANSMISSION_MODE,
888                 .set    = 0,
889         },
890         [DTV_HIERARCHY] = {
891                 .name   = "DTV_HIERARCHY",
892                 .cmd    = DTV_HIERARCHY,
893                 .set    = 0,
894         },
895 };
896
897 void dtv_property_dump(struct dtv_property *tvp)
898 {
899         int i;
900
901         if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
902                 printk("%s: tvp.cmd = 0x%08x (undefined/unknown/invalid)\n",
903                         __func__, tvp->cmd);
904                 return;
905         }
906
907         printk("%s() tvp.cmd    = 0x%08x (%s)\n"
908                 ,__FUNCTION__
909                 ,tvp->cmd
910                 ,dtv_cmds[ tvp->cmd ].name);
911
912         if(dtv_cmds[ tvp->cmd ].buffer) {
913
914                 printk("%s() tvp.u.buffer.len = 0x%02x\n"
915                         ,__FUNCTION__
916                         ,tvp->u.buffer.len);
917
918                 for(i = 0; i < tvp->u.buffer.len; i++)
919                         printk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
920                                 ,__FUNCTION__
921                                 ,i
922                                 ,tvp->u.buffer.data[i]);
923
924         } else
925                 printk("%s() tvp.u.data = 0x%08x\n", __FUNCTION__, tvp->u.data);
926 }
927
928 int is_legacy_delivery_system(fe_delivery_system_t s)
929 {
930         if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
931                 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS))
932                 return 1;
933
934         return 0;
935 }
936
937 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
938  * drivers can use a single set_frontend tuning function, regardless of whether
939  * it's being used for the legacy or new API, reducing code and complexity.
940  */
941 void dtv_property_cache_sync(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
942 {
943         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
944
945         printk("%s()\n", __FUNCTION__);
946
947         c->frequency = p->frequency;
948         c->inversion = p->inversion;
949
950         switch (fe->ops.info.type) {
951         case FE_QPSK:
952                 c->modulation = QPSK;   /* implied for DVB-S in legacy API */
953                 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
954                 c->symbol_rate = p->u.qpsk.symbol_rate;
955                 c->fec_inner = p->u.qpsk.fec_inner;
956                 c->delivery_system = SYS_DVBS;
957                 break;
958         case FE_QAM:
959                 c->symbol_rate = p->u.qam.symbol_rate;
960                 c->fec_inner = p->u.qam.fec_inner;
961                 c->modulation = p->u.qam.modulation;
962                 c->delivery_system = SYS_DVBC_ANNEX_AC;
963                 break;
964         case FE_OFDM:
965                 if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
966                         c->bandwidth_hz = 6000000;
967                 else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
968                         c->bandwidth_hz = 7000000;
969                 else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
970                         c->bandwidth_hz = 8000000;
971                 else
972                         /* Including BANDWIDTH_AUTO */
973                         c->bandwidth_hz = 0;
974                 c->code_rate_HP = p->u.ofdm.code_rate_HP;
975                 c->code_rate_LP = p->u.ofdm.code_rate_LP;
976                 c->modulation = p->u.ofdm.constellation;
977                 c->transmission_mode = p->u.ofdm.transmission_mode;
978                 c->guard_interval = p->u.ofdm.guard_interval;
979                 c->hierarchy = p->u.ofdm.hierarchy_information;
980                 c->delivery_system = SYS_DVBT;
981                 break;
982         case FE_ATSC:
983                 c->modulation = p->u.vsb.modulation;
984                 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
985                         c->delivery_system = SYS_ATSC;
986                 else
987                         c->delivery_system = SYS_DVBC_ANNEX_B;
988                 break;
989         }
990 }
991
992 /* Ensure the cached values are set correctly in the frontend
993  * legacy tuning structures, for the advanced tuning API.
994  */
995 void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
996 {
997         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
998         struct dvb_frontend_private *fepriv = fe->frontend_priv;
999         struct dvb_frontend_parameters *p = &fepriv->parameters;
1000
1001         printk("%s()\n", __FUNCTION__);
1002
1003         p->frequency = c->frequency;
1004         p->inversion = c->inversion;
1005
1006         switch (fe->ops.info.type) {
1007         case FE_QPSK:
1008                 printk("%s() Preparing QPSK req\n", __FUNCTION__);
1009                 p->u.qpsk.symbol_rate = c->symbol_rate;
1010                 p->u.qpsk.fec_inner = c->fec_inner;
1011                 c->delivery_system = SYS_DVBS;
1012                 break;
1013         case FE_QAM:
1014                 printk("%s() Preparing QAM req\n", __FUNCTION__);
1015                 p->u.qam.symbol_rate = c->symbol_rate;
1016                 p->u.qam.fec_inner = c->fec_inner;
1017                 p->u.qam.modulation = c->modulation;
1018                 c->delivery_system = SYS_DVBC_ANNEX_AC;
1019                 break;
1020         case FE_OFDM:
1021                 printk("%s() Preparing OFDM req\n", __FUNCTION__);
1022                 if (c->bandwidth_hz == 6000000)
1023                         p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1024                 else if (c->bandwidth_hz == 7000000)
1025                         p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1026                 else if (c->bandwidth_hz == 8000000)
1027                         p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1028                 else
1029                         p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1030                 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1031                 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1032                 p->u.ofdm.constellation = c->modulation;
1033                 p->u.ofdm.transmission_mode = c->transmission_mode;
1034                 p->u.ofdm.guard_interval = c->guard_interval;
1035                 p->u.ofdm.hierarchy_information = c->hierarchy;
1036                 c->delivery_system = SYS_DVBT;
1037                 break;
1038         case FE_ATSC:
1039                 printk("%s() Preparing VSB req\n", __FUNCTION__);
1040                 p->u.vsb.modulation = c->modulation;
1041                 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1042                         c->delivery_system = SYS_ATSC;
1043                 else
1044                         c->delivery_system = SYS_DVBC_ANNEX_B;
1045                 break;
1046         }
1047 }
1048
1049 /* Ensure the cached values are set correctly in the frontend
1050  * legacy tuning structures, for the legacy tuning API.
1051  */
1052 void dtv_property_adv_params_sync(struct dvb_frontend *fe)
1053 {
1054         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1055         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1056         struct dvb_frontend_parameters *p = &fepriv->parameters;
1057
1058         printk("%s()\n", __FUNCTION__);
1059
1060         p->frequency = c->frequency;
1061         p->inversion = c->inversion;
1062
1063         switch(c->modulation) {
1064         case PSK_8:
1065         case APSK_16:
1066         case APSK_32:
1067         case QPSK:
1068                 p->u.qpsk.symbol_rate = c->symbol_rate;
1069                 p->u.qpsk.fec_inner = c->fec_inner;
1070                 break;
1071         default:
1072                 break;
1073         }
1074
1075         if(c->delivery_system == SYS_ISDBT) {
1076                 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1077                 p->frequency = c->frequency;
1078                 p->inversion = INVERSION_AUTO;
1079                 p->u.ofdm.constellation = QAM_AUTO;
1080                 p->u.ofdm.code_rate_HP = FEC_AUTO;
1081                 p->u.ofdm.code_rate_LP = FEC_AUTO;
1082                 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1083                 p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1084                 p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1085                 p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1086         }
1087 }
1088
1089 void dtv_property_cache_submit(struct dvb_frontend *fe)
1090 {
1091         struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1092
1093         printk("%s()\n", __FUNCTION__);
1094
1095         /* For legacy delivery systems we don't need the delivery_system to
1096          * be specified, but we populate the older structures from the cache
1097          * so we can call set_frontend on older drivers.
1098          */
1099         if(is_legacy_delivery_system(c->delivery_system)) {
1100
1101                 printk("%s() legacy, modulation = %d\n", __FUNCTION__, c->modulation);
1102                 dtv_property_legacy_params_sync(fe);
1103
1104         } else {
1105                 printk("%s() adv, modulation = %d\n", __FUNCTION__, c->modulation);
1106
1107                 /* For advanced delivery systems / modulation types ...
1108                  * we seed the lecacy dvb_frontend_parameters structure
1109                  * so that the sanity checking code later in the IOCTL processing
1110                  * can validate our basic frequency ranges, symbolrates, modulation
1111                  * etc.
1112                  */
1113                 dtv_property_adv_params_sync(fe);
1114         }
1115 }
1116
1117 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1118                         unsigned int cmd, void *parg);
1119 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1120                         unsigned int cmd, void *parg);
1121
1122 int dtv_property_process_get(struct dvb_frontend *fe, struct dtv_property *tvp,
1123         struct inode *inode, struct file *file)
1124 {
1125         int r = 0;
1126
1127         printk("%s()\n", __FUNCTION__);
1128
1129         dtv_property_dump(tvp);
1130
1131         /* Allow the frontend to validate incoming properties */
1132         if (fe->ops.get_property)
1133                 r = fe->ops.get_property(fe, tvp);
1134
1135         if (r < 0)
1136                 return r;
1137
1138         switch(tvp->cmd) {
1139         case DTV_FREQUENCY:
1140                 tvp->u.data = fe->dtv_property_cache.frequency;
1141                 break;
1142         case DTV_MODULATION:
1143                 tvp->u.data = fe->dtv_property_cache.modulation;
1144                 break;
1145         case DTV_BANDWIDTH_HZ:
1146                 tvp->u.data = fe->dtv_property_cache.bandwidth_hz;
1147                 break;
1148         case DTV_INVERSION:
1149                 tvp->u.data = fe->dtv_property_cache.inversion;
1150                 break;
1151         case DTV_SYMBOL_RATE:
1152                 tvp->u.data = fe->dtv_property_cache.symbol_rate;
1153                 break;
1154         case DTV_INNER_FEC:
1155                 tvp->u.data = fe->dtv_property_cache.fec_inner;
1156                 break;
1157         case DTV_PILOT:
1158                 tvp->u.data = fe->dtv_property_cache.pilot;
1159                 break;
1160         case DTV_ROLLOFF:
1161                 tvp->u.data = fe->dtv_property_cache.rolloff;
1162                 break;
1163         case DTV_DELIVERY_SYSTEM:
1164                 tvp->u.data = fe->dtv_property_cache.delivery_system;
1165                 break;
1166         case DTV_VOLTAGE:
1167                 tvp->u.data = fe->dtv_property_cache.voltage;
1168                 break;
1169         case DTV_TONE:
1170                 tvp->u.data = fe->dtv_property_cache.sectone;
1171                 break;
1172         case DTV_API_VERSION:
1173                 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1174                 break;
1175         case DTV_CODE_RATE_HP:
1176                 tvp->u.data = fe->dtv_property_cache.code_rate_HP;
1177                 break;
1178         case DTV_CODE_RATE_LP:
1179                 tvp->u.data = fe->dtv_property_cache.code_rate_LP;
1180                 break;
1181         case DTV_GUARD_INTERVAL:
1182                 tvp->u.data = fe->dtv_property_cache.guard_interval;
1183                 break;
1184         case DTV_TRANSMISSION_MODE:
1185                 tvp->u.data = fe->dtv_property_cache.transmission_mode;
1186                 break;
1187         case DTV_HIERARCHY:
1188                 tvp->u.data = fe->dtv_property_cache.hierarchy;
1189                 break;
1190         default:
1191                 r = -1;
1192         }
1193
1194         return r;
1195 }
1196
1197 int dtv_property_process_set(struct dvb_frontend *fe, struct dtv_property *tvp,
1198         struct inode *inode, struct file *file)
1199 {
1200         int r = 0;
1201         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1202         printk("%s()\n", __FUNCTION__);
1203         dtv_property_dump(tvp);
1204
1205         /* Allow the frontend to validate incoming properties */
1206         if (fe->ops.set_property)
1207                 r = fe->ops.set_property(fe, tvp);
1208
1209         if (r < 0)
1210                 return r;
1211
1212         switch(tvp->cmd) {
1213         case DTV_CLEAR:
1214                 /* Reset a cache of data specific to the frontend here. This does
1215                  * not effect hardware.
1216                  */
1217                 printk("%s() Flushing property cache\n", __FUNCTION__);
1218                 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
1219                 fe->dtv_property_cache.state = tvp->cmd;
1220                 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
1221                 break;
1222         case DTV_TUNE:
1223                 /* interpret the cache of data, build either a traditional frontend
1224                  * tunerequest so we can pass validation in the FE_SET_FRONTEND
1225                  * ioctl.
1226                  */
1227                 fe->dtv_property_cache.state = tvp->cmd;
1228                 printk("%s() Finalised property cache\n", __FUNCTION__);
1229                 dtv_property_cache_submit(fe);
1230
1231                 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1232                         &fepriv->parameters);
1233                 break;
1234         case DTV_FREQUENCY:
1235                 fe->dtv_property_cache.frequency = tvp->u.data;
1236                 break;
1237         case DTV_MODULATION:
1238                 fe->dtv_property_cache.modulation = tvp->u.data;
1239                 break;
1240         case DTV_BANDWIDTH_HZ:
1241                 fe->dtv_property_cache.bandwidth_hz = tvp->u.data;
1242                 break;
1243         case DTV_INVERSION:
1244                 fe->dtv_property_cache.inversion = tvp->u.data;
1245                 break;
1246         case DTV_SYMBOL_RATE:
1247                 fe->dtv_property_cache.symbol_rate = tvp->u.data;
1248                 break;
1249         case DTV_INNER_FEC:
1250                 fe->dtv_property_cache.fec_inner = tvp->u.data;
1251                 break;
1252         case DTV_PILOT:
1253                 fe->dtv_property_cache.pilot = tvp->u.data;
1254                 break;
1255         case DTV_ROLLOFF:
1256                 fe->dtv_property_cache.rolloff = tvp->u.data;
1257                 break;
1258         case DTV_DELIVERY_SYSTEM:
1259                 fe->dtv_property_cache.delivery_system = tvp->u.data;
1260                 break;
1261         case DTV_VOLTAGE:
1262                 fe->dtv_property_cache.voltage = tvp->u.data;
1263                 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
1264                         (void *)fe->dtv_property_cache.voltage);
1265                 break;
1266         case DTV_TONE:
1267                 fe->dtv_property_cache.sectone = tvp->u.data;
1268                 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
1269                         (void *)fe->dtv_property_cache.sectone);
1270                 break;
1271         case DTV_CODE_RATE_HP:
1272                 fe->dtv_property_cache.code_rate_HP = tvp->u.data;
1273                 break;
1274         case DTV_CODE_RATE_LP:
1275                 fe->dtv_property_cache.code_rate_LP = tvp->u.data;
1276                 break;
1277         case DTV_GUARD_INTERVAL:
1278                 fe->dtv_property_cache.guard_interval = tvp->u.data;
1279                 break;
1280         case DTV_TRANSMISSION_MODE:
1281                 fe->dtv_property_cache.transmission_mode = tvp->u.data;
1282                 break;
1283         case DTV_HIERARCHY:
1284                 fe->dtv_property_cache.hierarchy = tvp->u.data;
1285                 break;
1286         default:
1287                 r = -1;
1288         }
1289
1290         return r;
1291 }
1292
1293 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1294                         unsigned int cmd, void *parg)
1295 {
1296         struct dvb_device *dvbdev = file->private_data;
1297         struct dvb_frontend *fe = dvbdev->priv;
1298         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1299         int err = -EOPNOTSUPP;
1300
1301         dprintk ("%s\n", __func__);
1302
1303         if (fepriv->exit)
1304                 return -ENODEV;
1305
1306         if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1307             (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1308              cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1309                 return -EPERM;
1310
1311         if (down_interruptible (&fepriv->sem))
1312                 return -ERESTARTSYS;
1313
1314         if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1315                 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1316         else {
1317                 fe->dtv_property_cache.state = DTV_UNDEFINED;
1318                 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1319         }
1320
1321         up(&fepriv->sem);
1322         return err;
1323 }
1324
1325 static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1326                         unsigned int cmd, void *parg)
1327 {
1328         struct dvb_device *dvbdev = file->private_data;
1329         struct dvb_frontend *fe = dvbdev->priv;
1330         int err = 0;
1331
1332         struct dtv_properties *tvps = NULL;
1333         struct dtv_property *tvp = NULL;
1334         int i;
1335
1336         dprintk("%s\n", __func__);
1337
1338         if(cmd == FE_SET_PROPERTY) {
1339                 printk("%s() FE_SET_PROPERTY\n", __FUNCTION__);
1340
1341                 tvps = (struct dtv_properties __user *)parg;
1342
1343                 printk("%s() properties.num = %d\n", __FUNCTION__, tvps->num);
1344                 printk("%s() properties.props = %p\n", __FUNCTION__, tvps->props);
1345
1346                 /* Put an arbitrary limit on the number of messages that can
1347                  * be sent at once */
1348                 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1349                         return -EINVAL;
1350
1351                 tvp = (struct dtv_property *) kmalloc(tvps->num *
1352                         sizeof(struct dtv_property), GFP_KERNEL);
1353                 if (!tvp) {
1354                         err = -ENOMEM;
1355                         goto out;
1356                 }
1357
1358                 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1359                         err = -EFAULT;
1360                         goto out;
1361                 }
1362
1363                 for (i = 0; i < tvps->num; i++) {
1364                         (tvp + i)->result = dtv_property_process_set(fe, tvp + i, inode, file);
1365                         err |= (tvp + i)->result;
1366                 }
1367
1368                 if(fe->dtv_property_cache.state == DTV_TUNE) {
1369                         printk("%s() Property cache is full, tuning\n", __FUNCTION__);
1370                 }
1371
1372         } else
1373         if(cmd == FE_GET_PROPERTY) {
1374                 printk("%s() FE_GET_PROPERTY\n", __FUNCTION__);
1375
1376                 tvps = (struct dtv_properties __user *)parg;
1377
1378                 printk("%s() properties.num = %d\n", __FUNCTION__, tvps->num);
1379                 printk("%s() properties.props = %p\n", __FUNCTION__, tvps->props);
1380
1381                 /* Put an arbitrary limit on the number of messages that can
1382                  * be sent at once */
1383                 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1384                         return -EINVAL;
1385
1386                 tvp = (struct dtv_property *) kmalloc(tvps->num *
1387                         sizeof(struct dtv_property), GFP_KERNEL);
1388                 if (!tvp) {
1389                         err = -ENOMEM;
1390                         goto out;
1391                 }
1392
1393                 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1394                         err = -EFAULT;
1395                         goto out;
1396                 }
1397
1398                 for (i = 0; i < tvps->num; i++) {
1399                         (tvp + i)->result = dtv_property_process_get(fe, tvp + i, inode, file);
1400                         err |= (tvp + i)->result;
1401                 }
1402
1403                 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1404                         err = -EFAULT;
1405                         goto out;
1406                 }
1407
1408         } else
1409                 err = -EOPNOTSUPP;
1410
1411 out:
1412         kfree(tvp);
1413         return err;
1414 }
1415
1416 static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1417                         unsigned int cmd, void *parg)
1418 {
1419         struct dvb_device *dvbdev = file->private_data;
1420         struct dvb_frontend *fe = dvbdev->priv;
1421         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1422         int err = -EOPNOTSUPP;
1423
1424         switch (cmd) {
1425         case FE_GET_INFO: {
1426                 struct dvb_frontend_info* info = parg;
1427                 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1428                 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1429
1430                 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1431                  * do it, it is done for it. */
1432                 info->caps |= FE_CAN_INVERSION_AUTO;
1433                 err = 0;
1434                 break;
1435         }
1436
1437         case FE_READ_STATUS: {
1438                 fe_status_t* status = parg;
1439
1440                 /* if retune was requested but hasn't occured yet, prevent
1441                  * that user get signal state from previous tuning */
1442                 if(fepriv->state == FESTATE_RETUNE) {
1443                         err=0;
1444                         *status = 0;
1445                         break;
1446                 }
1447
1448                 if (fe->ops.read_status)
1449                         err = fe->ops.read_status(fe, status);
1450                 break;
1451         }
1452         case FE_READ_BER:
1453                 if (fe->ops.read_ber)
1454                         err = fe->ops.read_ber(fe, (__u32*) parg);
1455                 break;
1456
1457         case FE_READ_SIGNAL_STRENGTH:
1458                 if (fe->ops.read_signal_strength)
1459                         err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1460                 break;
1461
1462         case FE_READ_SNR:
1463                 if (fe->ops.read_snr)
1464                         err = fe->ops.read_snr(fe, (__u16*) parg);
1465                 break;
1466
1467         case FE_READ_UNCORRECTED_BLOCKS:
1468                 if (fe->ops.read_ucblocks)
1469                         err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1470                 break;
1471
1472
1473         case FE_DISEQC_RESET_OVERLOAD:
1474                 if (fe->ops.diseqc_reset_overload) {
1475                         err = fe->ops.diseqc_reset_overload(fe);
1476                         fepriv->state = FESTATE_DISEQC;
1477                         fepriv->status = 0;
1478                 }
1479                 break;
1480
1481         case FE_DISEQC_SEND_MASTER_CMD:
1482                 if (fe->ops.diseqc_send_master_cmd) {
1483                         err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1484                         fepriv->state = FESTATE_DISEQC;
1485                         fepriv->status = 0;
1486                 }
1487                 break;
1488
1489         case FE_DISEQC_SEND_BURST:
1490                 if (fe->ops.diseqc_send_burst) {
1491                         err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1492                         fepriv->state = FESTATE_DISEQC;
1493                         fepriv->status = 0;
1494                 }
1495                 break;
1496
1497         case FE_SET_TONE:
1498                 if (fe->ops.set_tone) {
1499                         err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
1500                         fepriv->tone = (fe_sec_tone_mode_t) parg;
1501                         fepriv->state = FESTATE_DISEQC;
1502                         fepriv->status = 0;
1503                 }
1504                 break;
1505
1506         case FE_SET_VOLTAGE:
1507                 if (fe->ops.set_voltage) {
1508                         err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
1509                         fepriv->voltage = (fe_sec_voltage_t) parg;
1510                         fepriv->state = FESTATE_DISEQC;
1511                         fepriv->status = 0;
1512                 }
1513                 break;
1514
1515         case FE_DISHNETWORK_SEND_LEGACY_CMD:
1516                 if (fe->ops.dishnetwork_send_legacy_command) {
1517                         err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1518                         fepriv->state = FESTATE_DISEQC;
1519                         fepriv->status = 0;
1520                 } else if (fe->ops.set_voltage) {
1521                         /*
1522                          * NOTE: This is a fallback condition.  Some frontends
1523                          * (stv0299 for instance) take longer than 8msec to
1524                          * respond to a set_voltage command.  Those switches
1525                          * need custom routines to switch properly.  For all
1526                          * other frontends, the following shoule work ok.
1527                          * Dish network legacy switches (as used by Dish500)
1528                          * are controlled by sending 9-bit command words
1529                          * spaced 8msec apart.
1530                          * the actual command word is switch/port dependant
1531                          * so it is up to the userspace application to send
1532                          * the right command.
1533                          * The command must always start with a '0' after
1534                          * initialization, so parg is 8 bits and does not
1535                          * include the initialization or start bit
1536                          */
1537                         unsigned long swcmd = ((unsigned long) parg) << 1;
1538                         struct timeval nexttime;
1539                         struct timeval tv[10];
1540                         int i;
1541                         u8 last = 1;
1542                         if (dvb_frontend_debug)
1543                                 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
1544                         do_gettimeofday(&nexttime);
1545                         if (dvb_frontend_debug)
1546                                 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1547                         /* before sending a command, initialize by sending
1548                          * a 32ms 18V to the switch
1549                          */
1550                         fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
1551                         dvb_frontend_sleep_until(&nexttime, 32000);
1552
1553                         for (i = 0; i < 9; i++) {
1554                                 if (dvb_frontend_debug)
1555                                         do_gettimeofday(&tv[i + 1]);
1556                                 if ((swcmd & 0x01) != last) {
1557                                         /* set voltage to (last ? 13V : 18V) */
1558                                         fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
1559                                         last = (last) ? 0 : 1;
1560                                 }
1561                                 swcmd = swcmd >> 1;
1562                                 if (i != 8)
1563                                         dvb_frontend_sleep_until(&nexttime, 8000);
1564                         }
1565                         if (dvb_frontend_debug) {
1566                                 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
1567                                         __func__, fe->dvb->num);
1568                                 for (i = 1; i < 10; i++)
1569                                         printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1570                         }
1571                         err = 0;
1572                         fepriv->state = FESTATE_DISEQC;
1573                         fepriv->status = 0;
1574                 }
1575                 break;
1576
1577         case FE_DISEQC_RECV_SLAVE_REPLY:
1578                 if (fe->ops.diseqc_recv_slave_reply)
1579                         err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1580                 break;
1581
1582         case FE_ENABLE_HIGH_LNB_VOLTAGE:
1583                 if (fe->ops.enable_high_lnb_voltage)
1584                         err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1585                 break;
1586
1587         case FE_SET_FRONTEND: {
1588                 struct dvb_frontend_tune_settings fetunesettings;
1589
1590                 if(fe->dtv_property_cache.state == DTV_TUNE) {
1591                         if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1592                                 err = -EINVAL;
1593                                 break;
1594                         }
1595                 } else {
1596                         if (dvb_frontend_check_parameters(fe, parg) < 0) {
1597                                 err = -EINVAL;
1598                                 break;
1599                         }
1600
1601                         memcpy (&fepriv->parameters, parg,
1602                                 sizeof (struct dvb_frontend_parameters));
1603                         dtv_property_cache_sync(fe, &fepriv->parameters);
1604                 }
1605
1606                 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1607                 memcpy(&fetunesettings.parameters, parg,
1608                        sizeof (struct dvb_frontend_parameters));
1609
1610                 /* force auto frequency inversion if requested */
1611                 if (dvb_force_auto_inversion) {
1612                         fepriv->parameters.inversion = INVERSION_AUTO;
1613                         fetunesettings.parameters.inversion = INVERSION_AUTO;
1614                 }
1615                 if (fe->ops.info.type == FE_OFDM) {
1616                         /* without hierarchical coding code_rate_LP is irrelevant,
1617                          * so we tolerate the otherwise invalid FEC_NONE setting */
1618                         if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1619                             fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1620                                 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1621                 }
1622
1623                 /* get frontend-specific tuning settings */
1624                 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1625                         fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1626                         fepriv->max_drift = fetunesettings.max_drift;
1627                         fepriv->step_size = fetunesettings.step_size;
1628                 } else {
1629                         /* default values */
1630                         switch(fe->ops.info.type) {
1631                         case FE_QPSK:
1632                                 fepriv->min_delay = HZ/20;
1633                                 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1634                                 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1635                                 break;
1636
1637                         case FE_QAM:
1638                                 fepriv->min_delay = HZ/20;
1639                                 fepriv->step_size = 0; /* no zigzag */
1640                                 fepriv->max_drift = 0;
1641                                 break;
1642
1643                         case FE_OFDM:
1644                                 fepriv->min_delay = HZ/20;
1645                                 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1646                                 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1647                                 break;
1648                         case FE_ATSC:
1649                                 fepriv->min_delay = HZ/20;
1650                                 fepriv->step_size = 0;
1651                                 fepriv->max_drift = 0;
1652                                 break;
1653                         }
1654                 }
1655                 if (dvb_override_tune_delay > 0)
1656                         fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1657
1658                 fepriv->state = FESTATE_RETUNE;
1659                 dvb_frontend_wakeup(fe);
1660                 dvb_frontend_add_event(fe, 0);
1661                 fepriv->status = 0;
1662                 err = 0;
1663                 break;
1664         }
1665
1666         case FE_GET_EVENT:
1667                 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1668                 break;
1669
1670         case FE_GET_FRONTEND:
1671                 if (fe->ops.get_frontend) {
1672                         memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
1673                         err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1674                 }
1675                 break;
1676
1677         case FE_SET_FRONTEND_TUNE_MODE:
1678                 fepriv->tune_mode_flags = (unsigned long) parg;
1679                 err = 0;
1680                 break;
1681         };
1682
1683         return err;
1684 }
1685
1686
1687 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1688 {
1689         struct dvb_device *dvbdev = file->private_data;
1690         struct dvb_frontend *fe = dvbdev->priv;
1691         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1692
1693         dprintk ("%s\n", __func__);
1694
1695         poll_wait (file, &fepriv->events.wait_queue, wait);
1696
1697         if (fepriv->events.eventw != fepriv->events.eventr)
1698                 return (POLLIN | POLLRDNORM | POLLPRI);
1699
1700         return 0;
1701 }
1702
1703 static int dvb_frontend_open(struct inode *inode, struct file *file)
1704 {
1705         struct dvb_device *dvbdev = file->private_data;
1706         struct dvb_frontend *fe = dvbdev->priv;
1707         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1708         int ret;
1709
1710         dprintk ("%s\n", __func__);
1711
1712         if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1713                 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
1714                         return ret;
1715         }
1716
1717         if ((ret = dvb_generic_open (inode, file)) < 0)
1718                 goto err1;
1719
1720         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1721                 /* normal tune mode when opened R/W */
1722                 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1723                 fepriv->tone = -1;
1724                 fepriv->voltage = -1;
1725
1726                 ret = dvb_frontend_start (fe);
1727                 if (ret)
1728                         goto err2;
1729
1730                 /*  empty event queue */
1731                 fepriv->events.eventr = fepriv->events.eventw = 0;
1732         }
1733
1734         return ret;
1735
1736 err2:
1737         dvb_generic_release(inode, file);
1738 err1:
1739         if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1740                 fe->ops.ts_bus_ctrl(fe, 0);
1741         return ret;
1742 }
1743
1744 static int dvb_frontend_release(struct inode *inode, struct file *file)
1745 {
1746         struct dvb_device *dvbdev = file->private_data;
1747         struct dvb_frontend *fe = dvbdev->priv;
1748         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1749         int ret;
1750
1751         dprintk ("%s\n", __func__);
1752
1753         if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1754                 fepriv->release_jiffies = jiffies;
1755
1756         ret = dvb_generic_release (inode, file);
1757
1758         if (dvbdev->users == -1) {
1759                 if (fepriv->exit == 1) {
1760                         fops_put(file->f_op);
1761                         file->f_op = NULL;
1762                         wake_up(&dvbdev->wait_queue);
1763                 }
1764                 if (fe->ops.ts_bus_ctrl)
1765                         fe->ops.ts_bus_ctrl(fe, 0);
1766         }
1767
1768         return ret;
1769 }
1770
1771 static struct file_operations dvb_frontend_fops = {
1772         .owner          = THIS_MODULE,
1773         .ioctl          = dvb_generic_ioctl,
1774         .poll           = dvb_frontend_poll,
1775         .open           = dvb_frontend_open,
1776         .release        = dvb_frontend_release
1777 };
1778
1779 int dvb_register_frontend(struct dvb_adapter* dvb,
1780                           struct dvb_frontend* fe)
1781 {
1782         struct dvb_frontend_private *fepriv;
1783         static const struct dvb_device dvbdev_template = {
1784                 .users = ~0,
1785                 .writers = 1,
1786                 .readers = (~0)-1,
1787                 .fops = &dvb_frontend_fops,
1788                 .kernel_ioctl = dvb_frontend_ioctl
1789         };
1790
1791         dprintk ("%s\n", __func__);
1792
1793         if (mutex_lock_interruptible(&frontend_mutex))
1794                 return -ERESTARTSYS;
1795
1796         fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1797         if (fe->frontend_priv == NULL) {
1798                 mutex_unlock(&frontend_mutex);
1799                 return -ENOMEM;
1800         }
1801         fepriv = fe->frontend_priv;
1802
1803         init_MUTEX (&fepriv->sem);
1804         init_waitqueue_head (&fepriv->wait_queue);
1805         init_waitqueue_head (&fepriv->events.wait_queue);
1806         mutex_init(&fepriv->events.mtx);
1807         fe->dvb = dvb;
1808         fepriv->inversion = INVERSION_OFF;
1809
1810         printk ("DVB: registering frontend %i (%s)...\n",
1811                 fe->dvb->num,
1812                 fe->ops.info.name);
1813
1814         dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1815                              fe, DVB_DEVICE_FRONTEND);
1816
1817         mutex_unlock(&frontend_mutex);
1818         return 0;
1819 }
1820 EXPORT_SYMBOL(dvb_register_frontend);
1821
1822 int dvb_unregister_frontend(struct dvb_frontend* fe)
1823 {
1824         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1825         dprintk ("%s\n", __func__);
1826
1827         mutex_lock(&frontend_mutex);
1828         dvb_frontend_stop (fe);
1829         mutex_unlock(&frontend_mutex);
1830
1831         if (fepriv->dvbdev->users < -1)
1832                 wait_event(fepriv->dvbdev->wait_queue,
1833                                 fepriv->dvbdev->users==-1);
1834
1835         mutex_lock(&frontend_mutex);
1836         dvb_unregister_device (fepriv->dvbdev);
1837
1838         /* fe is invalid now */
1839         kfree(fepriv);
1840         mutex_unlock(&frontend_mutex);
1841         return 0;
1842 }
1843 EXPORT_SYMBOL(dvb_unregister_frontend);
1844
1845 #ifdef CONFIG_MEDIA_ATTACH
1846 void dvb_frontend_detach(struct dvb_frontend* fe)
1847 {
1848         void *ptr;
1849
1850         if (fe->ops.release_sec) {
1851                 fe->ops.release_sec(fe);
1852                 symbol_put_addr(fe->ops.release_sec);
1853         }
1854         if (fe->ops.tuner_ops.release) {
1855                 fe->ops.tuner_ops.release(fe);
1856                 symbol_put_addr(fe->ops.tuner_ops.release);
1857         }
1858         if (fe->ops.analog_ops.release) {
1859                 fe->ops.analog_ops.release(fe);
1860                 symbol_put_addr(fe->ops.analog_ops.release);
1861         }
1862         ptr = (void*)fe->ops.release;
1863         if (ptr) {
1864                 fe->ops.release(fe);
1865                 symbol_put_addr(ptr);
1866         }
1867 }
1868 #else
1869 void dvb_frontend_detach(struct dvb_frontend* fe)
1870 {
1871         if (fe->ops.release_sec)
1872                 fe->ops.release_sec(fe);
1873         if (fe->ops.tuner_ops.release)
1874                 fe->ops.tuner_ops.release(fe);
1875         if (fe->ops.analog_ops.release)
1876                 fe->ops.analog_ops.release(fe);
1877         if (fe->ops.release)
1878                 fe->ops.release(fe);
1879 }
1880 #endif
1881 EXPORT_SYMBOL(dvb_frontend_detach);