Merge branch 'upstream-fixes' of git://git.tuxdriver.com/git/netdev-jwl
[linux-2.6] / drivers / media / dvb / dvb-core / dvb_frontend.c
1 /*
2  * dvb_frontend.c: DVB frontend tuning interface/thread
3  *
4  *
5  * Copyright (C) 1999-2001 Ralph  Metzler
6  *                         Marcus Metzler
7  *                         Holger Waechtler
8  *                                    for convergence integrated media GmbH
9  *
10  * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup)
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26  */
27
28 #include <linux/string.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/wait.h>
32 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/module.h>
35 #include <linux/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         fe_sec_tone_mode_t tone;
117 };
118
119
120 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
121 {
122         struct dvb_frontend_private *fepriv = fe->frontend_priv;
123         struct dvb_fe_events *events = &fepriv->events;
124         struct dvb_frontend_event *e;
125         int wp;
126
127         dprintk ("%s\n", __FUNCTION__);
128
129         if (down_interruptible (&events->sem))
130                 return;
131
132         wp = (events->eventw + 1) % MAX_EVENT;
133
134         if (wp == events->eventr) {
135                 events->overflow = 1;
136                 events->eventr = (events->eventr + 1) % MAX_EVENT;
137         }
138
139         e = &events->events[events->eventw];
140
141         memcpy (&e->parameters, &fepriv->parameters,
142                 sizeof (struct dvb_frontend_parameters));
143
144         if (status & FE_HAS_LOCK)
145                 if (fe->ops->get_frontend)
146                         fe->ops->get_frontend(fe, &e->parameters);
147
148         events->eventw = wp;
149
150         up (&events->sem);
151
152         e->status = status;
153
154         wake_up_interruptible (&events->wait_queue);
155 }
156
157 static int dvb_frontend_get_event(struct dvb_frontend *fe,
158                             struct dvb_frontend_event *event, int flags)
159 {
160         struct dvb_frontend_private *fepriv = fe->frontend_priv;
161         struct dvb_fe_events *events = &fepriv->events;
162
163         dprintk ("%s\n", __FUNCTION__);
164
165         if (events->overflow) {
166                 events->overflow = 0;
167                 return -EOVERFLOW;
168         }
169
170         if (events->eventw == events->eventr) {
171                 int ret;
172
173                 if (flags & O_NONBLOCK)
174                         return -EWOULDBLOCK;
175
176                 up(&fepriv->sem);
177
178                 ret = wait_event_interruptible (events->wait_queue,
179                                                 events->eventw != events->eventr);
180
181                 if (down_interruptible (&fepriv->sem))
182                         return -ERESTARTSYS;
183
184                 if (ret < 0)
185                         return ret;
186         }
187
188         if (down_interruptible (&events->sem))
189                 return -ERESTARTSYS;
190
191         memcpy (event, &events->events[events->eventr],
192                 sizeof(struct dvb_frontend_event));
193
194         events->eventr = (events->eventr + 1) % MAX_EVENT;
195
196         up (&events->sem);
197
198         return 0;
199 }
200
201 static void dvb_frontend_init(struct dvb_frontend *fe)
202 {
203         dprintk ("DVB: initialising frontend %i (%s)...\n",
204                  fe->dvb->num,
205                  fe->ops->info.name);
206
207         if (fe->ops->init)
208                 fe->ops->init(fe);
209 }
210
211 static void update_delay(int *quality, int *delay, int min_delay, int locked)
212 {
213             int q2;
214
215             dprintk ("%s\n", __FUNCTION__);
216
217             if (locked)
218                       (*quality) = (*quality * 220 + 36*256) / 256;
219             else
220                       (*quality) = (*quality * 220 + 0) / 256;
221
222             q2 = *quality - 128;
223             q2 *= q2;
224
225             *delay = min_delay + q2 * HZ / (128*128);
226 }
227
228 /**
229  * Performs automatic twiddling of frontend parameters.
230  *
231  * @param fe The frontend concerned.
232  * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
233  * @returns Number of complete iterations that have been performed.
234  */
235 static int dvb_frontend_autotune(struct dvb_frontend *fe, int check_wrapped)
236 {
237         int autoinversion;
238         int ready = 0;
239         struct dvb_frontend_private *fepriv = fe->frontend_priv;
240         int original_inversion = fepriv->parameters.inversion;
241         u32 original_frequency = fepriv->parameters.frequency;
242
243         /* are we using autoinversion? */
244         autoinversion = ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) &&
245                          (fepriv->parameters.inversion == INVERSION_AUTO));
246
247         /* setup parameters correctly */
248         while(!ready) {
249                 /* calculate the lnb_drift */
250                 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
251
252                 /* wrap the auto_step if we've exceeded the maximum drift */
253                 if (fepriv->lnb_drift > fepriv->max_drift) {
254                         fepriv->auto_step = 0;
255                         fepriv->auto_sub_step = 0;
256                         fepriv->lnb_drift = 0;
257                 }
258
259                 /* perform inversion and +/- zigzag */
260                 switch(fepriv->auto_sub_step) {
261                 case 0:
262                         /* try with the current inversion and current drift setting */
263                         ready = 1;
264                         break;
265
266                 case 1:
267                         if (!autoinversion) break;
268
269                         fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
270                         ready = 1;
271                         break;
272
273                 case 2:
274                         if (fepriv->lnb_drift == 0) break;
275
276                         fepriv->lnb_drift = -fepriv->lnb_drift;
277                         ready = 1;
278                         break;
279
280                 case 3:
281                         if (fepriv->lnb_drift == 0) break;
282                         if (!autoinversion) break;
283
284                         fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
285                         fepriv->lnb_drift = -fepriv->lnb_drift;
286                         ready = 1;
287                         break;
288
289                 default:
290                         fepriv->auto_step++;
291                         fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
292                         break;
293                 }
294
295                 if (!ready) fepriv->auto_sub_step++;
296         }
297
298         /* if this attempt would hit where we started, indicate a complete
299          * iteration has occurred */
300         if ((fepriv->auto_step == fepriv->started_auto_step) &&
301             (fepriv->auto_sub_step == 0) && check_wrapped) {
302                 return 1;
303         }
304
305         dprintk("%s: drift:%i inversion:%i auto_step:%i "
306                 "auto_sub_step:%i started_auto_step:%i\n",
307                 __FUNCTION__, fepriv->lnb_drift, fepriv->inversion,
308                 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
309
310         /* set the frontend itself */
311         fepriv->parameters.frequency += fepriv->lnb_drift;
312         if (autoinversion)
313                 fepriv->parameters.inversion = fepriv->inversion;
314         if (fe->ops->set_frontend)
315                 fe->ops->set_frontend(fe, &fepriv->parameters);
316
317         fepriv->parameters.frequency = original_frequency;
318         fepriv->parameters.inversion = original_inversion;
319
320         fepriv->auto_sub_step++;
321         return 0;
322 }
323
324 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
325 {
326         struct dvb_frontend_private *fepriv = fe->frontend_priv;
327
328         if (fepriv->exit)
329                 return 1;
330
331         if (fepriv->dvbdev->writers == 1)
332                 if (time_after(jiffies, fepriv->release_jiffies +
333                                         dvb_shutdown_timeout * HZ))
334                         return 1;
335
336         return 0;
337 }
338
339 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
340 {
341         struct dvb_frontend_private *fepriv = fe->frontend_priv;
342
343         if (fepriv->wakeup) {
344                 fepriv->wakeup = 0;
345                 return 1;
346         }
347         return dvb_frontend_is_exiting(fe);
348 }
349
350 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
351 {
352         struct dvb_frontend_private *fepriv = fe->frontend_priv;
353
354         fepriv->wakeup = 1;
355         wake_up_interruptible(&fepriv->wait_queue);
356 }
357
358 /*
359  * FIXME: use linux/kthread.h
360  */
361 static int dvb_frontend_thread(void *data)
362 {
363         struct dvb_frontend *fe = data;
364         struct dvb_frontend_private *fepriv = fe->frontend_priv;
365         unsigned long timeout;
366         char name [15];
367         int quality = 0, delay = 3*HZ;
368         fe_status_t s;
369         int check_wrapped = 0;
370
371         dprintk("%s\n", __FUNCTION__);
372
373         snprintf (name, sizeof(name), "kdvb-fe-%i", fe->dvb->num);
374
375         lock_kernel();
376         daemonize(name);
377         sigfillset(&current->blocked);
378         unlock_kernel();
379
380         fepriv->status = 0;
381         dvb_frontend_init(fe);
382         fepriv->wakeup = 0;
383
384         while (1) {
385                 up(&fepriv->sem);           /* is locked when we enter the thread... */
386
387                 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
388                                                            dvb_frontend_should_wakeup(fe),
389                                                            delay);
390                 if (0 != dvb_frontend_is_exiting(fe)) {
391                         /* got signal or quitting */
392                         break;
393                 }
394
395                 try_to_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 { /* if we _WERE_ tuned, but now don't have a lock */
439                                 fepriv->state = FESTATE_ZIGZAG_FAST;
440                                 fepriv->started_auto_step = fepriv->auto_step;
441                                 check_wrapped = 0;
442                         }
443                 }
444
445                 /* don't actually do anything if we're in the LOSTLOCK state,
446                  * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
447                 if ((fepriv->state & FESTATE_LOSTLOCK) &&
448                     (fe->ops->info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
449                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
450                         continue;
451                 }
452
453                 /* don't do anything if we're in the DISEQC state, since this
454                  * might be someone with a motorized dish controlled by DISEQC.
455                  * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
456                 if (fepriv->state & FESTATE_DISEQC) {
457                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
458                         continue;
459                 }
460
461                 /* if we're in the RETUNE state, set everything up for a brand
462                  * new scan, keeping the current inversion setting, as the next
463                  * tune is _very_ likely to require the same */
464                 if (fepriv->state & FESTATE_RETUNE) {
465                         fepriv->lnb_drift = 0;
466                         fepriv->auto_step = 0;
467                         fepriv->auto_sub_step = 0;
468                         fepriv->started_auto_step = 0;
469                         check_wrapped = 0;
470                 }
471
472                 /* fast zigzag. */
473                 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
474                         delay = fepriv->min_delay;
475
476                         /* peform a tune */
477                         if (dvb_frontend_autotune(fe, check_wrapped)) {
478                                 /* OK, if we've run out of trials at the fast speed.
479                                  * Drop back to slow for the _next_ attempt */
480                                 fepriv->state = FESTATE_SEARCHING_SLOW;
481                                 fepriv->started_auto_step = fepriv->auto_step;
482                                 continue;
483                         }
484                         check_wrapped = 1;
485
486                         /* if we've just retuned, enter the ZIGZAG_FAST state.
487                          * This ensures we cannot return from an
488                          * FE_SET_FRONTEND ioctl before the first frontend tune
489                          * occurs */
490                         if (fepriv->state & FESTATE_RETUNE) {
491                                 fepriv->state = FESTATE_TUNING_FAST;
492                         }
493                 }
494
495                 /* slow zigzag */
496                 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
497                         update_delay(&quality, &delay, fepriv->min_delay, s & FE_HAS_LOCK);
498
499                         /* Note: don't bother checking for wrapping; we stay in this
500                          * state until we get a lock */
501                         dvb_frontend_autotune(fe, 0);
502                 }
503         }
504
505         if (dvb_shutdown_timeout) {
506                 if (dvb_powerdown_on_sleep)
507                         if (fe->ops->set_voltage)
508                                 fe->ops->set_voltage(fe, SEC_VOLTAGE_OFF);
509                 if (fe->ops->sleep)
510                         fe->ops->sleep(fe);
511         }
512
513         fepriv->thread_pid = 0;
514         mb();
515
516         dvb_frontend_wakeup(fe);
517         return 0;
518 }
519
520 static void dvb_frontend_stop(struct dvb_frontend *fe)
521 {
522         unsigned long ret;
523         struct dvb_frontend_private *fepriv = fe->frontend_priv;
524
525         dprintk ("%s\n", __FUNCTION__);
526
527         fepriv->exit = 1;
528         mb();
529
530         if (!fepriv->thread_pid)
531                 return;
532
533         /* check if the thread is really alive */
534         if (kill_proc(fepriv->thread_pid, 0, 1) == -ESRCH) {
535                 printk("dvb_frontend_stop: thread PID %d already died\n",
536                                 fepriv->thread_pid);
537                 /* make sure the mutex was not held by the thread */
538                 init_MUTEX (&fepriv->sem);
539                 return;
540         }
541
542         /* wake up the frontend thread, so it notices that fe->exit == 1 */
543         dvb_frontend_wakeup(fe);
544
545         /* wait until the frontend thread has exited */
546         ret = wait_event_interruptible(fepriv->wait_queue,0 == fepriv->thread_pid);
547         if (-ERESTARTSYS != ret) {
548                 fepriv->state = FESTATE_IDLE;
549                 return;
550         }
551         fepriv->state = FESTATE_IDLE;
552
553         /* paranoia check in case a signal arrived */
554         if (fepriv->thread_pid)
555                 printk("dvb_frontend_stop: warning: thread PID %d won't exit\n",
556                                 fepriv->thread_pid);
557 }
558
559 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
560 {
561         return ((curtime.tv_usec < lasttime.tv_usec) ?
562                 1000000 - lasttime.tv_usec + curtime.tv_usec :
563                 curtime.tv_usec - lasttime.tv_usec);
564 }
565 EXPORT_SYMBOL(timeval_usec_diff);
566
567 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
568 {
569         curtime->tv_usec += add_usec;
570         if (curtime->tv_usec >= 1000000) {
571                 curtime->tv_usec -= 1000000;
572                 curtime->tv_sec++;
573         }
574 }
575
576 /*
577  * Sleep until gettimeofday() > waketime + add_usec
578  * This needs to be as precise as possible, but as the delay is
579  * usually between 2ms and 32ms, it is done using a scheduled msleep
580  * followed by usleep (normally a busy-wait loop) for the remainder
581  */
582 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
583 {
584         struct timeval lasttime;
585         s32 delta, newdelta;
586
587         timeval_usec_add(waketime, add_usec);
588
589         do_gettimeofday(&lasttime);
590         delta = timeval_usec_diff(lasttime, *waketime);
591         if (delta > 2500) {
592                 msleep((delta - 1500) / 1000);
593                 do_gettimeofday(&lasttime);
594                 newdelta = timeval_usec_diff(lasttime, *waketime);
595                 delta = (newdelta > delta) ? 0 : newdelta;
596         }
597         if (delta > 0)
598                 udelay(delta);
599 }
600 EXPORT_SYMBOL(dvb_frontend_sleep_until);
601
602 static int dvb_frontend_start(struct dvb_frontend *fe)
603 {
604         int ret;
605         struct dvb_frontend_private *fepriv = fe->frontend_priv;
606
607         dprintk ("%s\n", __FUNCTION__);
608
609         if (fepriv->thread_pid) {
610                 if (!fepriv->exit)
611                         return 0;
612                 else
613                         dvb_frontend_stop (fe);
614         }
615
616         if (signal_pending(current))
617                 return -EINTR;
618         if (down_interruptible (&fepriv->sem))
619                 return -EINTR;
620
621         fepriv->state = FESTATE_IDLE;
622         fepriv->exit = 0;
623         fepriv->thread_pid = 0;
624         mb();
625
626         ret = kernel_thread (dvb_frontend_thread, fe, 0);
627
628         if (ret < 0) {
629                 printk("dvb_frontend_start: failed to start kernel_thread (%d)\n", ret);
630                 up(&fepriv->sem);
631                 return ret;
632         }
633         fepriv->thread_pid = ret;
634
635         return 0;
636 }
637
638 static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
639                         unsigned int cmd, void *parg)
640 {
641         struct dvb_device *dvbdev = file->private_data;
642         struct dvb_frontend *fe = dvbdev->priv;
643         struct dvb_frontend_private *fepriv = fe->frontend_priv;
644         int err = -EOPNOTSUPP;
645
646         dprintk ("%s\n", __FUNCTION__);
647
648         if (!fe || fepriv->exit)
649                 return -ENODEV;
650
651         if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
652             (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
653              cmd == FE_DISEQC_RECV_SLAVE_REPLY))
654                 return -EPERM;
655
656         if (down_interruptible (&fepriv->sem))
657                 return -ERESTARTSYS;
658
659         switch (cmd) {
660         case FE_GET_INFO: {
661                 struct dvb_frontend_info* info = parg;
662                 memcpy(info, &fe->ops->info, sizeof(struct dvb_frontend_info));
663
664                 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
665                  * do it, it is done for it. */
666                 info->caps |= FE_CAN_INVERSION_AUTO;
667                 err = 0;
668                 break;
669         }
670
671         case FE_READ_STATUS: {
672                 fe_status_t* status = parg;
673
674                 /* if retune was requested but hasn't occured yet, prevent
675                  * that user get signal state from previous tuning */
676                 if(fepriv->state == FESTATE_RETUNE) {
677                         err=0;
678                         *status = 0;
679                         break;
680                 }
681
682                 if (fe->ops->read_status)
683                         err = fe->ops->read_status(fe, status);
684                 break;
685         }
686         case FE_READ_BER:
687                 if (fe->ops->read_ber)
688                         err = fe->ops->read_ber(fe, (__u32*) parg);
689                 break;
690
691         case FE_READ_SIGNAL_STRENGTH:
692                 if (fe->ops->read_signal_strength)
693                         err = fe->ops->read_signal_strength(fe, (__u16*) parg);
694                 break;
695
696         case FE_READ_SNR:
697                 if (fe->ops->read_snr)
698                         err = fe->ops->read_snr(fe, (__u16*) parg);
699                 break;
700
701         case FE_READ_UNCORRECTED_BLOCKS:
702                 if (fe->ops->read_ucblocks)
703                         err = fe->ops->read_ucblocks(fe, (__u32*) parg);
704                 break;
705
706
707         case FE_DISEQC_RESET_OVERLOAD:
708                 if (fe->ops->diseqc_reset_overload) {
709                         err = fe->ops->diseqc_reset_overload(fe);
710                         fepriv->state = FESTATE_DISEQC;
711                         fepriv->status = 0;
712                 }
713                 break;
714
715         case FE_DISEQC_SEND_MASTER_CMD:
716                 if (fe->ops->diseqc_send_master_cmd) {
717                         err = fe->ops->diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
718                         fepriv->state = FESTATE_DISEQC;
719                         fepriv->status = 0;
720                 }
721                 break;
722
723         case FE_DISEQC_SEND_BURST:
724                 if (fe->ops->diseqc_send_burst) {
725                         err = fe->ops->diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
726                         fepriv->state = FESTATE_DISEQC;
727                         fepriv->status = 0;
728                 }
729                 break;
730
731         case FE_SET_TONE:
732                 if (fe->ops->set_tone) {
733                         err = fe->ops->set_tone(fe, (fe_sec_tone_mode_t) parg);
734                         fepriv->state = FESTATE_DISEQC;
735                         fepriv->status = 0;
736                         fepriv->tone = (fe_sec_tone_mode_t) parg;
737                 }
738                 break;
739
740         case FE_SET_VOLTAGE:
741                 if (fe->ops->set_voltage) {
742                         err = fe->ops->set_voltage(fe, (fe_sec_voltage_t) parg);
743                         fepriv->state = FESTATE_DISEQC;
744                         fepriv->status = 0;
745                 }
746                 break;
747
748         case FE_DISHNETWORK_SEND_LEGACY_CMD:
749                 if (fe->ops->dishnetwork_send_legacy_command) {
750                         err = fe->ops->dishnetwork_send_legacy_command(fe, (unsigned int) parg);
751                         fepriv->state = FESTATE_DISEQC;
752                         fepriv->status = 0;
753                 } else if (fe->ops->set_voltage) {
754                         /*
755                          * NOTE: This is a fallback condition.  Some frontends
756                          * (stv0299 for instance) take longer than 8msec to
757                          * respond to a set_voltage command.  Those switches
758                          * need custom routines to switch properly.  For all
759                          * other frontends, the following shoule work ok.
760                          * Dish network legacy switches (as used by Dish500)
761                          * are controlled by sending 9-bit command words
762                          * spaced 8msec apart.
763                          * the actual command word is switch/port dependant
764                          * so it is up to the userspace application to send
765                          * the right command.
766                          * The command must always start with a '0' after
767                          * initialization, so parg is 8 bits and does not
768                          * include the initialization or start bit
769                          */
770                         unsigned int cmd = ((unsigned int) parg) << 1;
771                         struct timeval nexttime;
772                         struct timeval tv[10];
773                         int i;
774                         u8 last = 1;
775                         if (dvb_frontend_debug)
776                                 printk("%s switch command: 0x%04x\n", __FUNCTION__, cmd);
777                         do_gettimeofday(&nexttime);
778                         if (dvb_frontend_debug)
779                                 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
780                         /* before sending a command, initialize by sending
781                          * a 32ms 18V to the switch
782                          */
783                         fe->ops->set_voltage(fe, SEC_VOLTAGE_18);
784                         dvb_frontend_sleep_until(&nexttime, 32000);
785
786                         for (i = 0; i < 9; i++) {
787                                 if (dvb_frontend_debug)
788                                         do_gettimeofday(&tv[i + 1]);
789                                 if ((cmd & 0x01) != last) {
790                                         /* set voltage to (last ? 13V : 18V) */
791                                         fe->ops->set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
792                                         last = (last) ? 0 : 1;
793                                 }
794                                 cmd = cmd >> 1;
795                                 if (i != 8)
796                                         dvb_frontend_sleep_until(&nexttime, 8000);
797                         }
798                         if (dvb_frontend_debug) {
799                                 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
800                                         __FUNCTION__, fe->dvb->num);
801                                 for (i = 1; i < 10; i++)
802                                         printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
803                         }
804                         err = 0;
805                         fepriv->state = FESTATE_DISEQC;
806                         fepriv->status = 0;
807                 }
808                 break;
809
810         case FE_DISEQC_RECV_SLAVE_REPLY:
811                 if (fe->ops->diseqc_recv_slave_reply)
812                         err = fe->ops->diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
813                 break;
814
815         case FE_ENABLE_HIGH_LNB_VOLTAGE:
816                 if (fe->ops->enable_high_lnb_voltage)
817                         err = fe->ops->enable_high_lnb_voltage(fe, (int) parg);
818                 break;
819
820         case FE_SET_FRONTEND: {
821                 struct dvb_frontend_tune_settings fetunesettings;
822
823                 memcpy (&fepriv->parameters, parg,
824                         sizeof (struct dvb_frontend_parameters));
825
826                 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
827                 memcpy(&fetunesettings.parameters, parg,
828                        sizeof (struct dvb_frontend_parameters));
829
830                 /* force auto frequency inversion if requested */
831                 if (dvb_force_auto_inversion) {
832                         fepriv->parameters.inversion = INVERSION_AUTO;
833                         fetunesettings.parameters.inversion = INVERSION_AUTO;
834                 }
835                 if (fe->ops->info.type == FE_OFDM) {
836                         /* without hierachical coding code_rate_LP is irrelevant,
837                          * so we tolerate the otherwise invalid FEC_NONE setting */
838                         if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
839                             fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
840                                 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
841                 }
842
843                 /* get frontend-specific tuning settings */
844                 if (fe->ops->get_tune_settings && (fe->ops->get_tune_settings(fe, &fetunesettings) == 0)) {
845                         fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
846                         fepriv->max_drift = fetunesettings.max_drift;
847                         fepriv->step_size = fetunesettings.step_size;
848                 } else {
849                         /* default values */
850                         switch(fe->ops->info.type) {
851                         case FE_QPSK:
852                                 fepriv->min_delay = HZ/20;
853                                 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
854                                 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
855                                 break;
856
857                         case FE_QAM:
858                                 fepriv->min_delay = HZ/20;
859                                 fepriv->step_size = 0; /* no zigzag */
860                                 fepriv->max_drift = 0;
861                                 break;
862
863                         case FE_OFDM:
864                                 fepriv->min_delay = HZ/20;
865                                 fepriv->step_size = fe->ops->info.frequency_stepsize * 2;
866                                 fepriv->max_drift = (fe->ops->info.frequency_stepsize * 2) + 1;
867                                 break;
868                         case FE_ATSC:
869                                 printk("dvb-core: FE_ATSC not handled yet.\n");
870                                 break;
871                         }
872                 }
873                 if (dvb_override_tune_delay > 0)
874                         fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
875
876                 fepriv->state = FESTATE_RETUNE;
877                 dvb_frontend_wakeup(fe);
878                 dvb_frontend_add_event(fe, 0);
879                 fepriv->status = 0;
880                 err = 0;
881                 break;
882         }
883
884         case FE_GET_EVENT:
885                 err = dvb_frontend_get_event (fe, parg, file->f_flags);
886                 break;
887
888         case FE_GET_FRONTEND:
889                 if (fe->ops->get_frontend) {
890                         memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
891                         err = fe->ops->get_frontend(fe, (struct dvb_frontend_parameters*) parg);
892                 }
893                 break;
894         };
895
896         up (&fepriv->sem);
897         return err;
898 }
899
900 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
901 {
902         struct dvb_device *dvbdev = file->private_data;
903         struct dvb_frontend *fe = dvbdev->priv;
904         struct dvb_frontend_private *fepriv = fe->frontend_priv;
905
906         dprintk ("%s\n", __FUNCTION__);
907
908         poll_wait (file, &fepriv->events.wait_queue, wait);
909
910         if (fepriv->events.eventw != fepriv->events.eventr)
911                 return (POLLIN | POLLRDNORM | POLLPRI);
912
913         return 0;
914 }
915
916 static int dvb_frontend_open(struct inode *inode, struct file *file)
917 {
918         struct dvb_device *dvbdev = file->private_data;
919         struct dvb_frontend *fe = dvbdev->priv;
920         struct dvb_frontend_private *fepriv = fe->frontend_priv;
921         int ret;
922
923         dprintk ("%s\n", __FUNCTION__);
924
925         if ((ret = dvb_generic_open (inode, file)) < 0)
926                 return ret;
927
928         if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
929                 ret = dvb_frontend_start (fe);
930                 if (ret)
931                         dvb_generic_release (inode, file);
932
933                 /*  empty event queue */
934                 fepriv->events.eventr = fepriv->events.eventw = 0;
935         }
936
937         return ret;
938 }
939
940 static int dvb_frontend_release(struct inode *inode, struct file *file)
941 {
942         struct dvb_device *dvbdev = file->private_data;
943         struct dvb_frontend *fe = dvbdev->priv;
944         struct dvb_frontend_private *fepriv = fe->frontend_priv;
945
946         dprintk ("%s\n", __FUNCTION__);
947
948         if ((file->f_flags & O_ACCMODE) != O_RDONLY)
949                 fepriv->release_jiffies = jiffies;
950
951         return dvb_generic_release (inode, file);
952 }
953
954 static struct file_operations dvb_frontend_fops = {
955         .owner          = THIS_MODULE,
956         .ioctl          = dvb_generic_ioctl,
957         .poll           = dvb_frontend_poll,
958         .open           = dvb_frontend_open,
959         .release        = dvb_frontend_release
960 };
961
962 int dvb_register_frontend(struct dvb_adapter* dvb,
963                           struct dvb_frontend* fe)
964 {
965         struct dvb_frontend_private *fepriv;
966         static const struct dvb_device dvbdev_template = {
967                 .users = ~0,
968                 .writers = 1,
969                 .readers = (~0)-1,
970                 .fops = &dvb_frontend_fops,
971                 .kernel_ioctl = dvb_frontend_ioctl
972         };
973
974         dprintk ("%s\n", __FUNCTION__);
975
976         if (down_interruptible (&frontend_mutex))
977                 return -ERESTARTSYS;
978
979         fe->frontend_priv = kmalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
980         if (fe->frontend_priv == NULL) {
981                 up(&frontend_mutex);
982                 return -ENOMEM;
983         }
984         fepriv = fe->frontend_priv;
985         memset(fe->frontend_priv, 0, sizeof(struct dvb_frontend_private));
986
987         init_MUTEX (&fepriv->sem);
988         init_waitqueue_head (&fepriv->wait_queue);
989         init_waitqueue_head (&fepriv->events.wait_queue);
990         init_MUTEX (&fepriv->events.sem);
991         fe->dvb = dvb;
992         fepriv->inversion = INVERSION_OFF;
993         fepriv->tone = SEC_TONE_OFF;
994
995         printk ("DVB: registering frontend %i (%s)...\n",
996                 fe->dvb->num,
997                 fe->ops->info.name);
998
999         dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1000                              fe, DVB_DEVICE_FRONTEND);
1001
1002         up (&frontend_mutex);
1003         return 0;
1004 }
1005 EXPORT_SYMBOL(dvb_register_frontend);
1006
1007 int dvb_unregister_frontend(struct dvb_frontend* fe)
1008 {
1009         struct dvb_frontend_private *fepriv = fe->frontend_priv;
1010         dprintk ("%s\n", __FUNCTION__);
1011
1012         down (&frontend_mutex);
1013         dvb_unregister_device (fepriv->dvbdev);
1014         dvb_frontend_stop (fe);
1015         if (fe->ops->release)
1016                 fe->ops->release(fe);
1017         else
1018                 printk("dvb_frontend: Demodulator (%s) does not have a release callback!\n", fe->ops->info.name);
1019         /* fe is invalid now */
1020         kfree(fepriv);
1021         up (&frontend_mutex);
1022         return 0;
1023 }
1024 EXPORT_SYMBOL(dvb_unregister_frontend);