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