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