d6b7a9de471eebf50fa9c5bb04ec52c63350b535
[linux-2.6.git] / drivers / media / dvb / dvb-core / dvb_frontend.c
1 /*
2  * dvb_frontend.c: DVB frontend tuning interface/thread
3  *
4  *
5  * Copyright (C) 1999-2001 Ralph  Metzler
6  *                         Marcus Metzler
7  *                         Holger Waechtler
8  *                                    for convergence integrated media GmbH
9  *
10  * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup)
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26  */
27
28 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/wait.h>
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/list.h>
37 #include <linux/suspend.h>
38 #include <linux/jiffies.h>
39 #include <asm/processor.h>
40 #include <asm/semaphore.h>
41
42 #include "dvb_frontend.h"
43 #include "dvbdev.h"
44
45 static int dvb_frontend_debug;
46 static int dvb_shutdown_timeout = 5;
47 static int dvb_force_auto_inversion;
48 static int dvb_override_tune_delay;
49 static int dvb_powerdown_on_sleep = 1;
50
51 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
52 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
53 module_param(dvb_shutdown_timeout, int, 0444);
54 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
55 module_param(dvb_force_auto_inversion, int, 0444);
56 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
57 module_param(dvb_override_tune_delay, int, 0444);
58 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
59 module_param(dvb_powerdown_on_sleep, int, 0444);
60 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB volatage off on sleep (default)");
61
62 #define dprintk if (dvb_frontend_debug) printk
63
64 #define FESTATE_IDLE 1
65 #define FESTATE_RETUNE 2
66 #define FESTATE_TUNING_FAST 4
67 #define FESTATE_TUNING_SLOW 8
68 #define FESTATE_TUNED 16
69 #define FESTATE_ZIGZAG_FAST 32
70 #define FESTATE_ZIGZAG_SLOW 64
71 #define FESTATE_DISEQC 128
72 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
73 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
74 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
75 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
76 /*
77  * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
78  * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
79  * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
80  * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
81  * FESTATE_TUNED. The frontend has successfully locked on.
82  * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
83  * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
84  * FESTATE_DISEQC. A DISEQC command has just been issued.
85  * FESTATE_WAITFORLOCK. When we're waiting for a lock.
86  * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
87  * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
88  * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
89  */
90
91 static DECLARE_MUTEX(frontend_mutex);
92
93 struct dvb_frontend_private {
94
95         struct dvb_device *dvbdev;
96         struct dvb_frontend_parameters parameters;
97         struct dvb_fe_events events;
98         struct semaphore sem;
99         struct list_head list_head;
100         wait_queue_head_t wait_queue;
101         pid_t thread_pid;
102         unsigned long release_jiffies;
103         int state;
104         int bending;
105         int lnb_drift;
106         int inversion;
107         int auto_step;
108         int auto_sub_step;
109         int started_auto_step;
110         int min_delay;
111         int max_drift;
112         int step_size;
113         int exit;
114         int wakeup;
115         fe_status_t status;
116 };
117
118
119 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
120 {
121         struct dvb_frontend_private *fepriv = fe->frontend_priv;
122         struct dvb_fe_events *events = &fepriv->events;
123         struct dvb_frontend_event *e;
124         int wp;
125
126         dprintk ("%s\n", __FUNCTION__);
127
128         if (down_interruptible (&events->sem))
129                 return;
130
131         wp = (events->eventw + 1) % MAX_EVENT;
132
133         if (wp == events->eventr) {
134                 events->overflow = 1;
135                 events->eventr = (events->eventr + 1) % MAX_EVENT;
136         }
137
138         e = &events->events[events->eventw];
139
140         memcpy (&e->parameters, &fepriv->parameters,
141                 sizeof (struct dvb_frontend_parameters));
142
143         if (status & FE_HAS_LOCK)
144                 if (fe->ops->get_frontend)
145                         fe->ops->get_frontend(fe, &e->parameters);
146
147         events->eventw = wp;
148
149         up (&events->sem);
150
151         e->status = status;
152
153         wake_up_interruptible (&events->wait_queue);
154 }
155
156 static int dvb_frontend_get_event(struct dvb_frontend *fe,
157                             struct dvb_frontend_event *event, int flags)
158 {
159         struct dvb_frontend_private *fepriv = fe->frontend_priv;
160         struct dvb_fe_events *events = &fepriv->events;
161
162         dprintk ("%s\n", __FUNCTION__);
163
164         if (events->overflow) {
165                 events->overflow = 0;
166                 return -EOVERFLOW;
167         }
168
169         if (events->eventw == events->eventr) {
170                 int ret;
171
172                 if (flags & O_NONBLOCK)
173                         return -EWOULDBLOCK;
174
175                 up(&fepriv->sem);
176
177                 ret = wait_event_interruptible (events->wait_queue,
178                                                 events->eventw != events->eventr);
179
180                 if (down_interruptible (&fepriv->sem))
181                         return -ERESTARTSYS;
182
183                 if (ret < 0)
184                         return ret;
185         }
186
187         if (down_interruptible (&events->sem))
188                 return -ERESTARTSYS;
189
190         memcpy (event, &events->events[events->eventr],
191                 sizeof(struct dvb_frontend_event));
192
193         events->eventr = (events->eventr + 1) % MAX_EVENT;
194
195         up (&events->sem);
196
197         return 0;
198 }
199
200 static void dvb_frontend_init(struct dvb_frontend *fe)
201 {
202         dprintk ("DVB: initialising frontend %i (%s)...\n",
203                  fe->dvb->num,
204                  fe->ops->info.name);
205
206         if (fe->ops->init)
207                 fe->ops->init(fe);
208 }
209
210 static void update_delay(int *quality, int *delay, int min_delay, int locked)
211 {
212             int q2;
213
214             dprintk ("%s\n", __FUNCTION__);
215
216             if (locked)
217                       (*quality) = (*quality * 220 + 36*256) / 256;
218             else
219                       (*quality) = (*quality * 220 + 0) / 256;
220
221             q2 = *quality - 128;
222             q2 *= q2;
223
224             *delay = min_delay + q2 * HZ / (128*128);
225 }
226
227 /**
228  * Performs automatic twiddling of frontend parameters.
229  *
230  * @param fe The frontend concerned.
231  * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
232  * @returns Number of complete iterations that have been performed.
233  */
234 static int dvb_frontend_autotune(struct dvb_frontend *fe, int check_wrapped)
235 {
236         int autoinversion;
237         int ready = 0;
238         struct dvb_frontend_private *fepriv = fe->frontend_priv;
239         int original_inversion = fepriv->parameters.inversion;
240         u32 original_frequency = fepriv->parameters.frequency;
241
242         /* are we using autoinversion? */
243         autoinversion = ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) &&
244                          (fepriv->parameters.inversion == INVERSION_AUTO));
245
246         /* setup parameters correctly */
247         while(!ready) {
248                 /* calculate the lnb_drift */
249                 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
250
251                 /* wrap the auto_step if we've exceeded the maximum drift */
252                 if (fepriv->lnb_drift > fepriv->max_drift) {
253                         fepriv->auto_step = 0;
254                         fepriv->auto_sub_step = 0;
255                         fepriv->lnb_drift = 0;
256                 }
257
258                 /* perform inversion and +/- zigzag */
259                 switch(fepriv->auto_sub_step) {
260                 case 0:
261                         /* try with the current inversion and current drift setting */
262                         ready = 1;
263                         break;
264
265                 case 1:
266                         if (!autoinversion) break;
267
268                         fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
269                         ready = 1;
270                         break;
271
272                 case 2:
273                         if (fepriv->lnb_drift == 0) break;
274
275                         fepriv->lnb_drift = -fepriv->lnb_drift;
276                         ready = 1;
277                         break;
278
279                 case 3:
280                         if (fepriv->lnb_drift == 0) break;
281                         if (!autoinversion) break;
282
283                         fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
284                         fepriv->lnb_drift = -fepriv->lnb_drift;
285                         ready = 1;
286                         break;
287
288                 default:
289                         fepriv->auto_step++;
290                         fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
291                         break;
292                 }
293
294                 if (!ready) fepriv->auto_sub_step++;
295         }
296
297         /* if this attempt would hit where we started, indicate a complete
298          * iteration has occurred */
299         if ((fepriv->auto_step == fepriv->started_auto_step) &&
300             (fepriv->auto_sub_step == 0) && check_wrapped) {
301                 return 1;
302         }
303
304         dprintk("%s: drift:%i inversion:%i auto_step:%i "
305                 "auto_sub_step:%i started_auto_step:%i\n",
306                 __FUNCTION__, fepriv->lnb_drift, fepriv->inversion,
307                 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
308
309         /* set the frontend itself */
310         fepriv->parameters.frequency += fepriv->lnb_drift;
311         if (autoinversion)
312                 fepriv->parameters.inversion = fepriv->inversion;
313         if (fe->ops->set_frontend)
314                 fe->ops->set_frontend(fe, &fepriv->parameters);
315
316         fepriv->parameters.frequency = original_frequency;
317         fepriv->parameters.inversion = original_inversion;
318
319         fepriv->auto_sub_step++;
320         return 0;
321 }
322
323 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
324 {
325         struct dvb_frontend_private *fepriv = fe->frontend_priv;
326
327         if (fepriv->exit)
328                 return 1;
329
330         if (fepriv->dvbdev->writers == 1)
331                 if (time_after(jiffies, fepriv->release_jiffies +
332                                         dvb_shutdown_timeout * HZ))
333                         return 1;
334
335         return 0;
336 }
337
338 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
339 {
340         struct dvb_frontend_private *fepriv = fe->frontend_priv;
341
342         if (fepriv->wakeup) {
343                 fepriv->wakeup = 0;
344                 return 1;
345         }
346         return dvb_frontend_is_exiting(fe);
347 }
348
349 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
350 {
351         struct dvb_frontend_private *fepriv = fe->frontend_priv;
352
353         fepriv->wakeup = 1;
354         wake_up_interruptible(&fepriv->wait_queue);
355 }
356
357 /*
358  * FIXME: use linux/kthread.h
359  */
360 static int dvb_frontend_thread(void *data)
361 {
362         struct dvb_frontend *fe = data;
363         struct dvb_frontend_private *fepriv = fe->frontend_priv;
364         unsigned long timeout;
365         char name [15];
366         int quality = 0, delay = 3*HZ;
367         fe_status_t s;
368         int check_wrapped = 0;
369
370         dprintk("%s\n", __FUNCTION__);
371
372         snprintf (name, sizeof(name), "kdvb-fe-%i", fe->dvb->num);
373
374         lock_kernel();
375         daemonize(name);
376         sigfillset(&current->blocked);
377         unlock_kernel();
378
379         fepriv->status = 0;
380         dvb_frontend_init(fe);
381         fepriv->wakeup = 0;
382
383         while (1) {
384                 up(&fepriv->sem);           /* is locked when we enter the thread... */
385
386                 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
387                                                            dvb_frontend_should_wakeup(fe),
388                                                            delay);
389                 if (0 != dvb_frontend_is_exiting(fe)) {
390                         /* got signal or quitting */
391                         break;
392                 }
393
394                 if (current->flags & PF_FREEZE)
395                         refrigerator(PF_FREEZE);
396
397                 if (down_interruptible(&fepriv->sem))
398                         break;
399
400                 /* if we've got no parameters, just keep idling */
401                 if (fepriv->state & FESTATE_IDLE) {
402                         delay = 3*HZ;
403                         quality = 0;
404                         continue;
405                 }
406
407                 /* get the frontend status */
408                 if (fepriv->state & FESTATE_RETUNE) {
409                         s = 0;
410                 } else {
411                         if (fe->ops->read_status)
412                                 fe->ops->read_status(fe, &s);
413                         if (s != fepriv->status) {
414                                 dvb_frontend_add_event(fe, s);
415                                 fepriv->status = s;
416                         }
417                 }
418                 /* if we're not tuned, and we have a lock, move to the TUNED state */
419                 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
420                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
421                         fepriv->state = FESTATE_TUNED;
422
423                         /* if we're tuned, then we have determined the correct inversion */
424                         if ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) &&
425                             (fepriv->parameters.inversion == INVERSION_AUTO)) {
426                                 fepriv->parameters.inversion = fepriv->inversion;
427                         }
428                         continue;
429                 }
430
431                 /* if we are tuned already, check we're still locked */
432                 if (fepriv->state & FESTATE_TUNED) {
433                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
434
435                         /* we're tuned, and the lock is still good... */
436                         if (s & FE_HAS_LOCK)
437                                 continue;
438                         else {
439                                 /* if we _WERE_ tuned, but now don't have a lock,
440                                  * need to zigzag */
441                                 fepriv->state = FESTATE_ZIGZAG_FAST;
442                                 fepriv->started_auto_step = fepriv->auto_step;
443                                 check_wrapped = 0;
444                         }
445                 }
446
447                 /* don't actually do anything if we're in the LOSTLOCK state,
448                  * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
449                 if ((fepriv->state & FESTATE_LOSTLOCK) &&
450                     (fe->ops->info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
451                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
452                         continue;
453                 }
454
455                 /* don't do anything if we're in the DISEQC state, since this
456                  * might be someone with a motorized dish controlled by DISEQC.
457                  * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
458                 if (fepriv->state & FESTATE_DISEQC) {
459                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
460                         continue;
461                 }
462
463                 /* if we're in the RETUNE state, set everything up for a brand
464                  * new scan, keeping the current inversion setting, as the next
465                  * tune is _very_ likely to require the same */
466                 if (fepriv->state & FESTATE_RETUNE) {
467                         fepriv->lnb_drift = 0;
468                         fepriv->auto_step = 0;
469                         fepriv->auto_sub_step = 0;
470                         fepriv->started_auto_step = 0;
471                         check_wrapped = 0;
472                 }
473
474                 /* fast zigzag. */
475                 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
476                         delay = fepriv->min_delay;
477
478                         /* peform a tune */
479                         if (dvb_frontend_autotune(fe, check_wrapped)) {
480                                 /* OK, if we've run out of trials at the fast speed.
481                                  * Drop back to slow for the _next_ attempt */
482                                 fepriv->state = FESTATE_SEARCHING_SLOW;
483                                 fepriv->started_auto_step = fepriv->auto_step;
484                                 continue;
485                         }
486                         check_wrapped = 1;
487
488                         /* if we've just retuned, enter the ZIGZAG_FAST state.
489                          * This ensures we cannot return from an
490                          * FE_SET_FRONTEND ioctl before the first frontend tune
491                          * occurs */
492                         if (fepriv->state & FESTATE_RETUNE) {
493                                 fepriv->state = FESTATE_TUNING_FAST;
494                         }
495                 }
496
497                 /* slow zigzag */
498                 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
499                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
500
501                         /* Note: don't bother checking for wrapping; we stay in this
502                          * state until we get a lock */
503                         dvb_frontend_autotune(fe, 0);
504                 }
505         }
506
507         if (dvb_shutdown_timeout) {
508                 if (dvb_powerdown_on_sleep)
509                         if (fe->ops->set_voltage)
510                                 fe->ops->set_voltage(fe, SEC_VOLTAGE_OFF);
511                 if (fe->ops->sleep)
512                         fe->ops->sleep(fe);
513         }
514
515         fepriv->thread_pid = 0;
516         mb();
517
518         dvb_frontend_wakeup(fe);
519         return 0;
520 }
521
522 static void dvb_frontend_stop(struct dvb_frontend *fe)
523 {
524         unsigned long ret;
525         struct dvb_frontend_private *fepriv = fe->frontend_priv;
526
527         dprintk ("%s\n", __FUNCTION__);
528
529         fepriv->exit = 1;
530         mb();
531
532         if (!fepriv->thread_pid)
533                 return;
534
535         /* check if the thread is really alive */
536         if (kill_proc(fepriv->thread_pid, 0, 1) == -ESRCH) {
537                 printk("dvb_frontend_stop: thread PID %d already died\n",
538                                 fepriv->thread_pid);
539                 /* make sure the mutex was not held by the thread */
540                 init_MUTEX (&fepriv->sem);
541                 return;
542         }
543
544         /* wake up the frontend thread, so it notices that fe->exit == 1 */
545         dvb_frontend_wakeup(fe);
546
547         /* wait until the frontend thread has exited */
548         ret = wait_event_interruptible(fepriv->wait_queue,0 == fepriv->thread_pid);
549         if (-ERESTARTSYS != ret) {
550                 fepriv->state = FESTATE_IDLE;
551                 return;
552         }
553         fepriv->state = FESTATE_IDLE;
554
555         /* paranoia check in case a signal arrived */
556         if (fepriv->thread_pid)
557                 printk("dvb_frontend_stop: warning: thread PID %d won't exit\n",
558                                 fepriv->thread_pid);
559 }
560
561 static int dvb_frontend_start(struct dvb_frontend *fe)
562 {
563         int ret;
564         struct dvb_frontend_private *fepriv = fe->frontend_priv;
565
566         dprintk ("%s\n", __FUNCTION__);
567
568         if (fepriv->thread_pid) {
569                 if (!fepriv->exit)
570                         return 0;
571                 else
572                         dvb_frontend_stop (fe);
573         }
574
575         if (signal_pending(current))
576                 return -EINTR;
577         if (down_interruptible (&fepriv->sem))
578                 return -EINTR;
579
580         fepriv->state = FESTATE_IDLE;
581         fepriv->exit = 0;
582         fepriv->thread_pid = 0;
583         mb();
584
585         ret = kernel_thread (dvb_frontend_thread, fe, 0);
586
587         if (ret < 0) {
588                 printk("dvb_frontend_start: failed to start kernel_thread (%d)\n", ret);
589                 up(&fepriv->sem);
590                 return ret;
591         }
592         fepriv->thread_pid = ret;
593
594         return 0;
595 }
596
597 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
598                         unsigned int cmd, void *parg)
599 {
600         struct dvb_device *dvbdev = file->private_data;
601         struct dvb_frontend *fe = dvbdev->priv;
602         struct dvb_frontend_private *fepriv = fe->frontend_priv;
603         int err = -EOPNOTSUPP;
604
605         dprintk ("%s\n", __FUNCTION__);
606
607         if (!fe || fepriv->exit)
608                 return -ENODEV;
609
610         if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
611             (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
612              cmd == FE_DISEQC_RECV_SLAVE_REPLY))
613                 return -EPERM;
614
615         if (down_interruptible (&fepriv->sem))
616                 return -ERESTARTSYS;
617
618         switch (cmd) {
619         case FE_GET_INFO: {
620                 struct dvb_frontend_info* info = parg;
621                 memcpy(info, &fe->ops->info, sizeof(struct dvb_frontend_info));
622
623                 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
624                  * do it, it is done for it. */
625                 info->caps |= FE_CAN_INVERSION_AUTO;
626                 err = 0;
627                 break;
628         }
629
630         case FE_READ_STATUS:
631                 if (fe->ops->read_status)
632                         err = fe->ops->read_status(fe, (fe_status_t*) parg);
633                 break;
634
635         case FE_READ_BER:
636                 if (fe->ops->read_ber)
637                         err = fe->ops->read_ber(fe, (__u32*) parg);
638                 break;
639
640         case FE_READ_SIGNAL_STRENGTH:
641                 if (fe->ops->read_signal_strength)
642                         err = fe->ops->read_signal_strength(fe, (__u16*) parg);
643                 break;
644
645         case FE_READ_SNR:
646                 if (fe->ops->read_snr)
647                         err = fe->ops->read_snr(fe, (__u16*) parg);
648                 break;
649
650         case FE_READ_UNCORRECTED_BLOCKS:
651                 if (fe->ops->read_ucblocks)
652                         err = fe->ops->read_ucblocks(fe, (__u32*) parg);
653                 break;
654
655
656         case FE_DISEQC_RESET_OVERLOAD:
657                 if (fe->ops->diseqc_reset_overload) {
658                         err = fe->ops->diseqc_reset_overload(fe);
659                         fepriv->state = FESTATE_DISEQC;
660                         fepriv->status = 0;
661                 }
662                 break;
663
664         case FE_DISEQC_SEND_MASTER_CMD:
665                 if (fe->ops->diseqc_send_master_cmd) {
666                         err = fe->ops->diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
667                         fepriv->state = FESTATE_DISEQC;
668                         fepriv->status = 0;
669                 }
670                 break;
671
672         case FE_DISEQC_SEND_BURST:
673                 if (fe->ops->diseqc_send_burst) {
674                         err = fe->ops->diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
675                         fepriv->state = FESTATE_DISEQC;
676                         fepriv->status = 0;
677                 }
678                 break;
679
680         case FE_SET_TONE:
681                 if (fe->ops->set_tone) {
682                         err = fe->ops->set_tone(fe, (fe_sec_tone_mode_t) parg);
683                         fepriv->state = FESTATE_DISEQC;
684                         fepriv->status = 0;
685                 }
686                 break;
687
688         case FE_SET_VOLTAGE:
689                 if (fe->ops->set_voltage) {
690                         err = fe->ops->set_voltage(fe, (fe_sec_voltage_t) parg);
691                         fepriv->state = FESTATE_DISEQC;
692                         fepriv->status = 0;
693                 }
694                 break;
695
696         case FE_DISHNETWORK_SEND_LEGACY_CMD:
697                 if (fe->ops->dishnetwork_send_legacy_command) {
698                         err = fe->ops->dishnetwork_send_legacy_command(fe, (unsigned int) parg);
699                         fepriv->state = FESTATE_DISEQC;
700                         fepriv->status = 0;
701                 }
702                 break;
703
704         case FE_DISEQC_RECV_SLAVE_REPLY:
705                 if (fe->ops->diseqc_recv_slave_reply)
706                         err = fe->ops->diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
707                 break;
708
709         case FE_ENABLE_HIGH_LNB_VOLTAGE:
710                 if (fe->ops->enable_high_lnb_voltage)
711                         err = fe->ops->enable_high_lnb_voltage(fe, (int) parg);
712                 break;
713
714         case FE_SET_FRONTEND: {
715                 struct dvb_frontend_tune_settings fetunesettings;
716
717                 memcpy (&fepriv->parameters, parg,
718                         sizeof (struct dvb_frontend_parameters));
719
720                 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
721                 memcpy(&fetunesettings.parameters, parg,
722                        sizeof (struct dvb_frontend_parameters));
723
724                 /* force auto frequency inversion if requested */
725                 if (dvb_force_auto_inversion) {
726                         fepriv->parameters.inversion = INVERSION_AUTO;
727                         fetunesettings.parameters.inversion = INVERSION_AUTO;
728                 }
729                 if (fe->ops->info.type == FE_OFDM) {
730                         /* without hierachical coding code_rate_LP is irrelevant,
731                          * so we tolerate the otherwise invalid FEC_NONE setting */
732                         if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
733                             fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
734                                 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
735                 }
736
737                 /* get frontend-specific tuning settings */
738                 if (fe->ops->get_tune_settings && (fe->ops->get_tune_settings(fe, &fetunesettings) == 0)) {
739                         fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
740                         fepriv->max_drift = fetunesettings.max_drift;
741                         fepriv->step_size = fetunesettings.step_size;
742                 } else {
743                         /* default values */
744                         switch(fe->ops->info.type) {
745                         case FE_QPSK:
746                                 fepriv->min_delay = HZ/20;
747                                 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
748                                 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
749                                 break;
750
751                         case FE_QAM:
752                                 fepriv->min_delay = HZ/20;
753                                 fepriv->step_size = 0; /* no zigzag */
754                                 fepriv->max_drift = 0;
755                                 break;
756
757                         case FE_OFDM:
758                                 fepriv->min_delay = HZ/20;
759                                 fepriv->step_size = fe->ops->info.frequency_stepsize * 2;
760                                 fepriv->max_drift = (fe->ops->info.frequency_stepsize * 2) + 1;
761                                 break;
762                         case FE_ATSC:
763                                 printk("dvb-core: FE_ATSC not handled yet.\n");
764                                 break;
765                         }
766                 }
767                 if (dvb_override_tune_delay > 0)
768                         fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
769
770                 fepriv->state = FESTATE_RETUNE;
771                 dvb_frontend_wakeup(fe);
772                 dvb_frontend_add_event(fe, 0);
773                 fepriv->status = 0;
774                 err = 0;
775                 break;
776         }
777
778         case FE_GET_EVENT:
779                 err = dvb_frontend_get_event (fe, parg, file->f_flags);
780                 break;
781
782         case FE_GET_FRONTEND:
783                 if (fe->ops->get_frontend) {
784                         memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
785                         err = fe->ops->get_frontend(fe, (struct dvb_frontend_parameters*) parg);
786                 }
787                 break;
788         };
789
790         up (&fepriv->sem);
791         return err;
792 }
793
794 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
795 {
796         struct dvb_device *dvbdev = file->private_data;
797         struct dvb_frontend *fe = dvbdev->priv;
798         struct dvb_frontend_private *fepriv = fe->frontend_priv;
799
800         dprintk ("%s\n", __FUNCTION__);
801
802         poll_wait (file, &fepriv->events.wait_queue, wait);
803
804         if (fepriv->events.eventw != fepriv->events.eventr)
805                 return (POLLIN | POLLRDNORM | POLLPRI);
806
807         return 0;
808 }
809
810 static int dvb_frontend_open(struct inode *inode, struct file *file)
811 {
812         struct dvb_device *dvbdev = file->private_data;
813         struct dvb_frontend *fe = dvbdev->priv;
814         struct dvb_frontend_private *fepriv = fe->frontend_priv;
815         int ret;
816
817         dprintk ("%s\n", __FUNCTION__);
818
819         if ((ret = dvb_generic_open (inode, file)) < 0)
820                 return ret;
821
822         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
823                 ret = dvb_frontend_start (fe);
824                 if (ret)
825                         dvb_generic_release (inode, file);
826
827                 /*  empty event queue */
828                 fepriv->events.eventr = fepriv->events.eventw = 0;
829         }
830
831         return ret;
832 }
833
834 static int dvb_frontend_release(struct inode *inode, struct file *file)
835 {
836         struct dvb_device *dvbdev = file->private_data;
837         struct dvb_frontend *fe = dvbdev->priv;
838         struct dvb_frontend_private *fepriv = fe->frontend_priv;
839
840         dprintk ("%s\n", __FUNCTION__);
841
842         if ((file->f_flags & O_ACCMODE) != O_RDONLY)
843                 fepriv->release_jiffies = jiffies;
844
845         return dvb_generic_release (inode, file);
846 }
847
848 static struct file_operations dvb_frontend_fops = {
849         .owner          = THIS_MODULE,
850         .ioctl          = dvb_generic_ioctl,
851         .poll           = dvb_frontend_poll,
852         .open           = dvb_frontend_open,
853         .release        = dvb_frontend_release
854 };
855
856 int dvb_register_frontend(struct dvb_adapter* dvb,
857                           struct dvb_frontend* fe)
858 {
859         struct dvb_frontend_private *fepriv;
860         static const struct dvb_device dvbdev_template = {
861                 .users = ~0,
862                 .writers = 1,
863                 .readers = (~0)-1,
864                 .fops = &dvb_frontend_fops,
865                 .kernel_ioctl = dvb_frontend_ioctl
866         };
867
868         dprintk ("%s\n", __FUNCTION__);
869
870         if (down_interruptible (&frontend_mutex))
871                 return -ERESTARTSYS;
872
873         fe->frontend_priv = kmalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
874         if (fe->frontend_priv == NULL) {
875                 up(&frontend_mutex);
876                 return -ENOMEM;
877         }
878         fepriv = fe->frontend_priv;
879         memset(fe->frontend_priv, 0, sizeof(struct dvb_frontend_private));
880
881         init_MUTEX (&fepriv->sem);
882         init_waitqueue_head (&fepriv->wait_queue);
883         init_waitqueue_head (&fepriv->events.wait_queue);
884         init_MUTEX (&fepriv->events.sem);
885         fe->dvb = dvb;
886         fepriv->inversion = INVERSION_OFF;
887
888         printk ("DVB: registering frontend %i (%s)...\n",
889                 fe->dvb->num,
890                 fe->ops->info.name);
891
892         dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
893                              fe, DVB_DEVICE_FRONTEND);
894
895         up (&frontend_mutex);
896         return 0;
897 }
898 EXPORT_SYMBOL(dvb_register_frontend);
899
900 int dvb_unregister_frontend(struct dvb_frontend* fe)
901 {
902         struct dvb_frontend_private *fepriv = fe->frontend_priv;
903         dprintk ("%s\n", __FUNCTION__);
904
905         down (&frontend_mutex);
906         dvb_unregister_device (fepriv->dvbdev);
907         dvb_frontend_stop (fe);
908         if (fe->ops->release)
909                 fe->ops->release(fe);
910         else
911                 printk("dvb_frontend: Demodulator (%s) does not have a release callback!\n", fe->ops->info.name);
912         /* fe is invalid now */
913         kfree(fepriv);
914         up (&frontend_mutex);
915         return 0;
916 }
917 EXPORT_SYMBOL(dvb_unregister_frontend);