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