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