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