[PATCH] Cleanup patch for process freezing
[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                 try_to_freeze();
395
396                 if (down_interruptible(&fepriv->sem))
397                         break;
398
399                 /* if we've got no parameters, just keep idling */
400                 if (fepriv->state & FESTATE_IDLE) {
401                         delay = 3*HZ;
402                         quality = 0;
403                         continue;
404                 }
405
406                 /* get the frontend status */
407                 if (fepriv->state & FESTATE_RETUNE) {
408                         s = 0;
409                 } else {
410                         if (fe->ops->read_status)
411                                 fe->ops->read_status(fe, &s);
412                         if (s != fepriv->status) {
413                                 dvb_frontend_add_event(fe, s);
414                                 fepriv->status = s;
415                         }
416                 }
417                 /* if we're not tuned, and we have a lock, move to the TUNED state */
418                 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
419                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
420                         fepriv->state = FESTATE_TUNED;
421
422                         /* if we're tuned, then we have determined the correct inversion */
423                         if ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) &&
424                             (fepriv->parameters.inversion == INVERSION_AUTO)) {
425                                 fepriv->parameters.inversion = fepriv->inversion;
426                         }
427                         continue;
428                 }
429
430                 /* if we are tuned already, check we're still locked */
431                 if (fepriv->state & FESTATE_TUNED) {
432                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
433
434                         /* we're tuned, and the lock is still good... */
435                         if (s & FE_HAS_LOCK)
436                                 continue;
437                         else {
438                                 /* if we _WERE_ tuned, but now don't have a lock,
439                                  * need to zigzag */
440                                 fepriv->state = FESTATE_ZIGZAG_FAST;
441                                 fepriv->started_auto_step = fepriv->auto_step;
442                                 check_wrapped = 0;
443                         }
444                 }
445
446                 /* don't actually do anything if we're in the LOSTLOCK state,
447                  * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
448                 if ((fepriv->state & FESTATE_LOSTLOCK) &&
449                     (fe->ops->info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
450                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
451                         continue;
452                 }
453
454                 /* don't do anything if we're in the DISEQC state, since this
455                  * might be someone with a motorized dish controlled by DISEQC.
456                  * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
457                 if (fepriv->state & FESTATE_DISEQC) {
458                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
459                         continue;
460                 }
461
462                 /* if we're in the RETUNE state, set everything up for a brand
463                  * new scan, keeping the current inversion setting, as the next
464                  * tune is _very_ likely to require the same */
465                 if (fepriv->state & FESTATE_RETUNE) {
466                         fepriv->lnb_drift = 0;
467                         fepriv->auto_step = 0;
468                         fepriv->auto_sub_step = 0;
469                         fepriv->started_auto_step = 0;
470                         check_wrapped = 0;
471                 }
472
473                 /* fast zigzag. */
474                 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
475                         delay = fepriv->min_delay;
476
477                         /* peform a tune */
478                         if (dvb_frontend_autotune(fe, check_wrapped)) {
479                                 /* OK, if we've run out of trials at the fast speed.
480                                  * Drop back to slow for the _next_ attempt */
481                                 fepriv->state = FESTATE_SEARCHING_SLOW;
482                                 fepriv->started_auto_step = fepriv->auto_step;
483                                 continue;
484                         }
485                         check_wrapped = 1;
486
487                         /* if we've just retuned, enter the ZIGZAG_FAST state.
488                          * This ensures we cannot return from an
489                          * FE_SET_FRONTEND ioctl before the first frontend tune
490                          * occurs */
491                         if (fepriv->state & FESTATE_RETUNE) {
492                                 fepriv->state = FESTATE_TUNING_FAST;
493                         }
494                 }
495
496                 /* slow zigzag */
497                 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
498                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
499
500                         /* Note: don't bother checking for wrapping; we stay in this
501                          * state until we get a lock */
502                         dvb_frontend_autotune(fe, 0);
503                 }
504         }
505
506         if (dvb_shutdown_timeout) {
507                 if (dvb_powerdown_on_sleep)
508                         if (fe->ops->set_voltage)
509                                 fe->ops->set_voltage(fe, SEC_VOLTAGE_OFF);
510                 if (fe->ops->sleep)
511                         fe->ops->sleep(fe);
512         }
513
514         fepriv->thread_pid = 0;
515         mb();
516
517         dvb_frontend_wakeup(fe);
518         return 0;
519 }
520
521 static void dvb_frontend_stop(struct dvb_frontend *fe)
522 {
523         unsigned long ret;
524         struct dvb_frontend_private *fepriv = fe->frontend_priv;
525
526         dprintk ("%s\n", __FUNCTION__);
527
528         fepriv->exit = 1;
529         mb();
530
531         if (!fepriv->thread_pid)
532                 return;
533
534         /* check if the thread is really alive */
535         if (kill_proc(fepriv->thread_pid, 0, 1) == -ESRCH) {
536                 printk("dvb_frontend_stop: thread PID %d already died\n",
537                                 fepriv->thread_pid);
538                 /* make sure the mutex was not held by the thread */
539                 init_MUTEX (&fepriv->sem);
540                 return;
541         }
542
543         /* wake up the frontend thread, so it notices that fe->exit == 1 */
544         dvb_frontend_wakeup(fe);
545
546         /* wait until the frontend thread has exited */
547         ret = wait_event_interruptible(fepriv->wait_queue,0 == fepriv->thread_pid);
548         if (-ERESTARTSYS != ret) {
549                 fepriv->state = FESTATE_IDLE;
550                 return;
551         }
552         fepriv->state = FESTATE_IDLE;
553
554         /* paranoia check in case a signal arrived */
555         if (fepriv->thread_pid)
556                 printk("dvb_frontend_stop: warning: thread PID %d won't exit\n",
557                                 fepriv->thread_pid);
558 }
559
560 static int dvb_frontend_start(struct dvb_frontend *fe)
561 {
562         int ret;
563         struct dvb_frontend_private *fepriv = fe->frontend_priv;
564
565         dprintk ("%s\n", __FUNCTION__);
566
567         if (fepriv->thread_pid) {
568                 if (!fepriv->exit)
569                         return 0;
570                 else
571                         dvb_frontend_stop (fe);
572         }
573
574         if (signal_pending(current))
575                 return -EINTR;
576         if (down_interruptible (&fepriv->sem))
577                 return -EINTR;
578
579         fepriv->state = FESTATE_IDLE;
580         fepriv->exit = 0;
581         fepriv->thread_pid = 0;
582         mb();
583
584         ret = kernel_thread (dvb_frontend_thread, fe, 0);
585
586         if (ret < 0) {
587                 printk("dvb_frontend_start: failed to start kernel_thread (%d)\n", ret);
588                 up(&fepriv->sem);
589                 return ret;
590         }
591         fepriv->thread_pid = ret;
592
593         return 0;
594 }
595
596 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
597                         unsigned int cmd, void *parg)
598 {
599         struct dvb_device *dvbdev = file->private_data;
600         struct dvb_frontend *fe = dvbdev->priv;
601         struct dvb_frontend_private *fepriv = fe->frontend_priv;
602         int err = -EOPNOTSUPP;
603
604         dprintk ("%s\n", __FUNCTION__);
605
606         if (!fe || fepriv->exit)
607                 return -ENODEV;
608
609         if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
610             (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
611              cmd == FE_DISEQC_RECV_SLAVE_REPLY))
612                 return -EPERM;
613
614         if (down_interruptible (&fepriv->sem))
615                 return -ERESTARTSYS;
616
617         switch (cmd) {
618         case FE_GET_INFO: {
619                 struct dvb_frontend_info* info = parg;
620                 memcpy(info, &fe->ops->info, sizeof(struct dvb_frontend_info));
621
622                 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
623                  * do it, it is done for it. */
624                 info->caps |= FE_CAN_INVERSION_AUTO;
625                 err = 0;
626                 break;
627         }
628
629         case FE_READ_STATUS:
630                 if (fe->ops->read_status)
631                         err = fe->ops->read_status(fe, (fe_status_t*) parg);
632                 break;
633
634         case FE_READ_BER:
635                 if (fe->ops->read_ber)
636                         err = fe->ops->read_ber(fe, (__u32*) parg);
637                 break;
638
639         case FE_READ_SIGNAL_STRENGTH:
640                 if (fe->ops->read_signal_strength)
641                         err = fe->ops->read_signal_strength(fe, (__u16*) parg);
642                 break;
643
644         case FE_READ_SNR:
645                 if (fe->ops->read_snr)
646                         err = fe->ops->read_snr(fe, (__u16*) parg);
647                 break;
648
649         case FE_READ_UNCORRECTED_BLOCKS:
650                 if (fe->ops->read_ucblocks)
651                         err = fe->ops->read_ucblocks(fe, (__u32*) parg);
652                 break;
653
654
655         case FE_DISEQC_RESET_OVERLOAD:
656                 if (fe->ops->diseqc_reset_overload) {
657                         err = fe->ops->diseqc_reset_overload(fe);
658                         fepriv->state = FESTATE_DISEQC;
659                         fepriv->status = 0;
660                 }
661                 break;
662
663         case FE_DISEQC_SEND_MASTER_CMD:
664                 if (fe->ops->diseqc_send_master_cmd) {
665                         err = fe->ops->diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
666                         fepriv->state = FESTATE_DISEQC;
667                         fepriv->status = 0;
668                 }
669                 break;
670
671         case FE_DISEQC_SEND_BURST:
672                 if (fe->ops->diseqc_send_burst) {
673                         err = fe->ops->diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
674                         fepriv->state = FESTATE_DISEQC;
675                         fepriv->status = 0;
676                 }
677                 break;
678
679         case FE_SET_TONE:
680                 if (fe->ops->set_tone) {
681                         err = fe->ops->set_tone(fe, (fe_sec_tone_mode_t) parg);
682                         fepriv->state = FESTATE_DISEQC;
683                         fepriv->status = 0;
684                 }
685                 break;
686
687         case FE_SET_VOLTAGE:
688                 if (fe->ops->set_voltage) {
689                         err = fe->ops->set_voltage(fe, (fe_sec_voltage_t) parg);
690                         fepriv->state = FESTATE_DISEQC;
691                         fepriv->status = 0;
692                 }
693                 break;
694
695         case FE_DISHNETWORK_SEND_LEGACY_CMD:
696                 if (fe->ops->dishnetwork_send_legacy_command) {
697                         err = fe->ops->dishnetwork_send_legacy_command(fe, (unsigned int) parg);
698                         fepriv->state = FESTATE_DISEQC;
699                         fepriv->status = 0;
700                 }
701                 break;
702
703         case FE_DISEQC_RECV_SLAVE_REPLY:
704                 if (fe->ops->diseqc_recv_slave_reply)
705                         err = fe->ops->diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
706                 break;
707
708         case FE_ENABLE_HIGH_LNB_VOLTAGE:
709                 if (fe->ops->enable_high_lnb_voltage)
710                         err = fe->ops->enable_high_lnb_voltage(fe, (int) parg);
711                 break;
712
713         case FE_SET_FRONTEND: {
714                 struct dvb_frontend_tune_settings fetunesettings;
715
716                 memcpy (&fepriv->parameters, parg,
717                         sizeof (struct dvb_frontend_parameters));
718
719                 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
720                 memcpy(&fetunesettings.parameters, parg,
721                        sizeof (struct dvb_frontend_parameters));
722
723                 /* force auto frequency inversion if requested */
724                 if (dvb_force_auto_inversion) {
725                         fepriv->parameters.inversion = INVERSION_AUTO;
726                         fetunesettings.parameters.inversion = INVERSION_AUTO;
727                 }
728                 if (fe->ops->info.type == FE_OFDM) {
729                         /* without hierachical coding code_rate_LP is irrelevant,
730                          * so we tolerate the otherwise invalid FEC_NONE setting */
731                         if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
732                             fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
733                                 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
734                 }
735
736                 /* get frontend-specific tuning settings */
737                 if (fe->ops->get_tune_settings && (fe->ops->get_tune_settings(fe, &fetunesettings) == 0)) {
738                         fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
739                         fepriv->max_drift = fetunesettings.max_drift;
740                         fepriv->step_size = fetunesettings.step_size;
741                 } else {
742                         /* default values */
743                         switch(fe->ops->info.type) {
744                         case FE_QPSK:
745                                 fepriv->min_delay = HZ/20;
746                                 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
747                                 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
748                                 break;
749
750                         case FE_QAM:
751                                 fepriv->min_delay = HZ/20;
752                                 fepriv->step_size = 0; /* no zigzag */
753                                 fepriv->max_drift = 0;
754                                 break;
755
756                         case FE_OFDM:
757                                 fepriv->min_delay = HZ/20;
758                                 fepriv->step_size = fe->ops->info.frequency_stepsize * 2;
759                                 fepriv->max_drift = (fe->ops->info.frequency_stepsize * 2) + 1;
760                                 break;
761                         case FE_ATSC:
762                                 printk("dvb-core: FE_ATSC not handled yet.\n");
763                                 break;
764                         }
765                 }
766                 if (dvb_override_tune_delay > 0)
767                         fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
768
769                 fepriv->state = FESTATE_RETUNE;
770                 dvb_frontend_wakeup(fe);
771                 dvb_frontend_add_event(fe, 0);
772                 fepriv->status = 0;
773                 err = 0;
774                 break;
775         }
776
777         case FE_GET_EVENT:
778                 err = dvb_frontend_get_event (fe, parg, file->f_flags);
779                 break;
780
781         case FE_GET_FRONTEND:
782                 if (fe->ops->get_frontend) {
783                         memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
784                         err = fe->ops->get_frontend(fe, (struct dvb_frontend_parameters*) parg);
785                 }
786                 break;
787         };
788
789         up (&fepriv->sem);
790         return err;
791 }
792
793 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
794 {
795         struct dvb_device *dvbdev = file->private_data;
796         struct dvb_frontend *fe = dvbdev->priv;
797         struct dvb_frontend_private *fepriv = fe->frontend_priv;
798
799         dprintk ("%s\n", __FUNCTION__);
800
801         poll_wait (file, &fepriv->events.wait_queue, wait);
802
803         if (fepriv->events.eventw != fepriv->events.eventr)
804                 return (POLLIN | POLLRDNORM | POLLPRI);
805
806         return 0;
807 }
808
809 static int dvb_frontend_open(struct inode *inode, struct file *file)
810 {
811         struct dvb_device *dvbdev = file->private_data;
812         struct dvb_frontend *fe = dvbdev->priv;
813         struct dvb_frontend_private *fepriv = fe->frontend_priv;
814         int ret;
815
816         dprintk ("%s\n", __FUNCTION__);
817
818         if ((ret = dvb_generic_open (inode, file)) < 0)
819                 return ret;
820
821         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
822                 ret = dvb_frontend_start (fe);
823                 if (ret)
824                         dvb_generic_release (inode, file);
825
826                 /*  empty event queue */
827                 fepriv->events.eventr = fepriv->events.eventw = 0;
828         }
829
830         return ret;
831 }
832
833 static int dvb_frontend_release(struct inode *inode, struct file *file)
834 {
835         struct dvb_device *dvbdev = file->private_data;
836         struct dvb_frontend *fe = dvbdev->priv;
837         struct dvb_frontend_private *fepriv = fe->frontend_priv;
838
839         dprintk ("%s\n", __FUNCTION__);
840
841         if ((file->f_flags & O_ACCMODE) != O_RDONLY)
842                 fepriv->release_jiffies = jiffies;
843
844         return dvb_generic_release (inode, file);
845 }
846
847 static struct file_operations dvb_frontend_fops = {
848         .owner          = THIS_MODULE,
849         .ioctl          = dvb_generic_ioctl,
850         .poll           = dvb_frontend_poll,
851         .open           = dvb_frontend_open,
852         .release        = dvb_frontend_release
853 };
854
855 int dvb_register_frontend(struct dvb_adapter* dvb,
856                           struct dvb_frontend* fe)
857 {
858         struct dvb_frontend_private *fepriv;
859         static const struct dvb_device dvbdev_template = {
860                 .users = ~0,
861                 .writers = 1,
862                 .readers = (~0)-1,
863                 .fops = &dvb_frontend_fops,
864                 .kernel_ioctl = dvb_frontend_ioctl
865         };
866
867         dprintk ("%s\n", __FUNCTION__);
868
869         if (down_interruptible (&frontend_mutex))
870                 return -ERESTARTSYS;
871
872         fe->frontend_priv = kmalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
873         if (fe->frontend_priv == NULL) {
874                 up(&frontend_mutex);
875                 return -ENOMEM;
876         }
877         fepriv = fe->frontend_priv;
878         memset(fe->frontend_priv, 0, sizeof(struct dvb_frontend_private));
879
880         init_MUTEX (&fepriv->sem);
881         init_waitqueue_head (&fepriv->wait_queue);
882         init_waitqueue_head (&fepriv->events.wait_queue);
883         init_MUTEX (&fepriv->events.sem);
884         fe->dvb = dvb;
885         fepriv->inversion = INVERSION_OFF;
886
887         printk ("DVB: registering frontend %i (%s)...\n",
888                 fe->dvb->num,
889                 fe->ops->info.name);
890
891         dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
892                              fe, DVB_DEVICE_FRONTEND);
893
894         up (&frontend_mutex);
895         return 0;
896 }
897 EXPORT_SYMBOL(dvb_register_frontend);
898
899 int dvb_unregister_frontend(struct dvb_frontend* fe)
900 {
901         struct dvb_frontend_private *fepriv = fe->frontend_priv;
902         dprintk ("%s\n", __FUNCTION__);
903
904         down (&frontend_mutex);
905         dvb_unregister_device (fepriv->dvbdev);
906         dvb_frontend_stop (fe);
907         if (fe->ops->release)
908                 fe->ops->release(fe);
909         else
910                 printk("dvb_frontend: Demodulator (%s) does not have a release callback!\n", fe->ops->info.name);
911         /* fe is invalid now */
912         kfree(fepriv);
913         up (&frontend_mutex);
914         return 0;
915 }
916 EXPORT_SYMBOL(dvb_unregister_frontend);