Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dvb_frontend.c
Go to the documentation of this file.
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 /* Enables DVBv3 compatibility bits at the headers */
29 #define __DVB_CORE__
30 
31 #include <linux/string.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/semaphore.h>
38 #include <linux/module.h>
39 #include <linux/list.h>
40 #include <linux/freezer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kthread.h>
43 #include <asm/processor.h>
44 
45 #include "dvb_frontend.h"
46 #include "dvbdev.h"
47 #include <linux/dvb/version.h>
48 
49 static int dvb_frontend_debug;
50 static int dvb_shutdown_timeout;
51 static int dvb_force_auto_inversion;
52 static int dvb_override_tune_delay;
53 static int dvb_powerdown_on_sleep = 1;
54 static int dvb_mfe_wait_time = 5;
55 
56 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
57 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
58 module_param(dvb_shutdown_timeout, int, 0644);
59 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
60 module_param(dvb_force_auto_inversion, int, 0644);
61 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
62 module_param(dvb_override_tune_delay, int, 0644);
63 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
64 module_param(dvb_powerdown_on_sleep, int, 0644);
65 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
66 module_param(dvb_mfe_wait_time, int, 0644);
67 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)");
68 
69 #define FESTATE_IDLE 1
70 #define FESTATE_RETUNE 2
71 #define FESTATE_TUNING_FAST 4
72 #define FESTATE_TUNING_SLOW 8
73 #define FESTATE_TUNED 16
74 #define FESTATE_ZIGZAG_FAST 32
75 #define FESTATE_ZIGZAG_SLOW 64
76 #define FESTATE_DISEQC 128
77 #define FESTATE_ERROR 256
78 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
79 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
80 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
81 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
82 
83 #define FE_ALGO_HW 1
84 /*
85  * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
86  * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
87  * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
88  * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
89  * FESTATE_TUNED. The frontend has successfully locked on.
90  * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
91  * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
92  * FESTATE_DISEQC. A DISEQC command has just been issued.
93  * FESTATE_WAITFORLOCK. When we're waiting for a lock.
94  * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
95  * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
96  * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
97  */
98 
99 #define DVB_FE_NO_EXIT 0
100 #define DVB_FE_NORMAL_EXIT 1
101 #define DVB_FE_DEVICE_REMOVED 2
102 
103 static DEFINE_MUTEX(frontend_mutex);
104 
106 
107  /* thread/frontend values */
110  struct dvb_fe_events events;
111  struct semaphore sem;
115  unsigned long release_jiffies;
116  unsigned int exit;
117  unsigned int wakeup;
119  unsigned long tune_mode_flags;
120  unsigned int delay;
121  unsigned int reinitialise;
122  int tone;
123  int voltage;
124 
125  /* swzigzag values */
126  unsigned int state;
127  unsigned int bending;
129  unsigned int inversion;
130  unsigned int auto_step;
131  unsigned int auto_sub_step;
132  unsigned int started_auto_step;
133  unsigned int min_delay;
134  unsigned int max_drift;
135  unsigned int step_size;
136  int quality;
137  unsigned int check_wrapped;
139 };
140 
141 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
142 static int dtv_get_frontend(struct dvb_frontend *fe,
143  struct dvb_frontend_parameters *p_out);
144 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
145  struct dvb_frontend_parameters *p);
146 
147 static bool has_get_frontend(struct dvb_frontend *fe)
148 {
149  return fe->ops.get_frontend != NULL;
150 }
151 
152 /*
153  * Due to DVBv3 API calls, a delivery system should be mapped into one of
154  * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
155  * otherwise, a DVBv3 call will fail.
156  */
163 };
164 
165 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
166 {
167  switch (delivery_system) {
168  case SYS_DVBC_ANNEX_A:
169  case SYS_DVBC_ANNEX_C:
170  return DVBV3_QAM;
171  case SYS_DVBS:
172  case SYS_DVBS2:
173  case SYS_TURBO:
174  case SYS_ISDBS:
175  case SYS_DSS:
176  return DVBV3_QPSK;
177  case SYS_DVBT:
178  case SYS_DVBT2:
179  case SYS_ISDBT:
180  case SYS_DTMB:
181  return DVBV3_OFDM;
182  case SYS_ATSC:
183  case SYS_ATSCMH:
184  case SYS_DVBC_ANNEX_B:
185  return DVBV3_ATSC;
186  case SYS_UNDEFINED:
187  case SYS_ISDBC:
188  case SYS_DVBH:
189  case SYS_DAB:
190  default:
191  /*
192  * Doesn't know how to emulate those types and/or
193  * there's no frontend driver from this type yet
194  * with some emulation code, so, we're not sure yet how
195  * to handle them, or they're not compatible with a DVBv3 call.
196  */
197  return DVBV3_UNKNOWN;
198  }
199 }
200 
201 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
202 {
203  struct dvb_frontend_private *fepriv = fe->frontend_priv;
204  struct dvb_fe_events *events = &fepriv->events;
205  struct dvb_frontend_event *e;
206  int wp;
207 
208  dev_dbg(fe->dvb->device, "%s:\n", __func__);
209 
210  if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
211  dtv_get_frontend(fe, &fepriv->parameters_out);
212 
213  mutex_lock(&events->mtx);
214 
215  wp = (events->eventw + 1) % MAX_EVENT;
216  if (wp == events->eventr) {
217  events->overflow = 1;
218  events->eventr = (events->eventr + 1) % MAX_EVENT;
219  }
220 
221  e = &events->events[events->eventw];
222  e->status = status;
223  e->parameters = fepriv->parameters_out;
224 
225  events->eventw = wp;
226 
227  mutex_unlock(&events->mtx);
228 
229  wake_up_interruptible (&events->wait_queue);
230 }
231 
232 static int dvb_frontend_get_event(struct dvb_frontend *fe,
233  struct dvb_frontend_event *event, int flags)
234 {
235  struct dvb_frontend_private *fepriv = fe->frontend_priv;
236  struct dvb_fe_events *events = &fepriv->events;
237 
238  dev_dbg(fe->dvb->device, "%s:\n", __func__);
239 
240  if (events->overflow) {
241  events->overflow = 0;
242  return -EOVERFLOW;
243  }
244 
245  if (events->eventw == events->eventr) {
246  int ret;
247 
248  if (flags & O_NONBLOCK)
249  return -EWOULDBLOCK;
250 
251  up(&fepriv->sem);
252 
253  ret = wait_event_interruptible (events->wait_queue,
254  events->eventw != events->eventr);
255 
256  if (down_interruptible (&fepriv->sem))
257  return -ERESTARTSYS;
258 
259  if (ret < 0)
260  return ret;
261  }
262 
263  mutex_lock(&events->mtx);
264  *event = events->events[events->eventr];
265  events->eventr = (events->eventr + 1) % MAX_EVENT;
266  mutex_unlock(&events->mtx);
267 
268  return 0;
269 }
270 
271 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
272 {
273  struct dvb_frontend_private *fepriv = fe->frontend_priv;
274  struct dvb_fe_events *events = &fepriv->events;
275 
276  mutex_lock(&events->mtx);
277  events->eventr = events->eventw;
278  mutex_unlock(&events->mtx);
279 }
280 
281 static void dvb_frontend_init(struct dvb_frontend *fe)
282 {
283  dev_dbg(fe->dvb->device,
284  "%s: initialising adapter %i frontend %i (%s)...\n",
285  __func__, fe->dvb->num, fe->id, fe->ops.info.name);
286 
287  if (fe->ops.init)
288  fe->ops.init(fe);
289  if (fe->ops.tuner_ops.init) {
290  if (fe->ops.i2c_gate_ctrl)
291  fe->ops.i2c_gate_ctrl(fe, 1);
292  fe->ops.tuner_ops.init(fe);
293  if (fe->ops.i2c_gate_ctrl)
294  fe->ops.i2c_gate_ctrl(fe, 0);
295  }
296 }
297 
299 {
300  struct dvb_frontend_private *fepriv = fe->frontend_priv;
301 
302  fepriv->reinitialise = 1;
303  dvb_frontend_wakeup(fe);
304 }
306 
307 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
308 {
309  int q2;
310  struct dvb_frontend *fe = fepriv->dvbdev->priv;
311 
312  dev_dbg(fe->dvb->device, "%s:\n", __func__);
313 
314  if (locked)
315  (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
316  else
317  (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
318 
319  q2 = fepriv->quality - 128;
320  q2 *= q2;
321 
322  fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
323 }
324 
332 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
333 {
334  int autoinversion;
335  int ready = 0;
336  int fe_set_err = 0;
337  struct dvb_frontend_private *fepriv = fe->frontend_priv;
339  int original_inversion = c->inversion;
340  u32 original_frequency = c->frequency;
341 
342  /* are we using autoinversion? */
343  autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
344  (c->inversion == INVERSION_AUTO));
345 
346  /* setup parameters correctly */
347  while(!ready) {
348  /* calculate the lnb_drift */
349  fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
350 
351  /* wrap the auto_step if we've exceeded the maximum drift */
352  if (fepriv->lnb_drift > fepriv->max_drift) {
353  fepriv->auto_step = 0;
354  fepriv->auto_sub_step = 0;
355  fepriv->lnb_drift = 0;
356  }
357 
358  /* perform inversion and +/- zigzag */
359  switch(fepriv->auto_sub_step) {
360  case 0:
361  /* try with the current inversion and current drift setting */
362  ready = 1;
363  break;
364 
365  case 1:
366  if (!autoinversion) break;
367 
368  fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
369  ready = 1;
370  break;
371 
372  case 2:
373  if (fepriv->lnb_drift == 0) break;
374 
375  fepriv->lnb_drift = -fepriv->lnb_drift;
376  ready = 1;
377  break;
378 
379  case 3:
380  if (fepriv->lnb_drift == 0) break;
381  if (!autoinversion) break;
382 
383  fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
384  fepriv->lnb_drift = -fepriv->lnb_drift;
385  ready = 1;
386  break;
387 
388  default:
389  fepriv->auto_step++;
390  fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
391  break;
392  }
393 
394  if (!ready) fepriv->auto_sub_step++;
395  }
396 
397  /* if this attempt would hit where we started, indicate a complete
398  * iteration has occurred */
399  if ((fepriv->auto_step == fepriv->started_auto_step) &&
400  (fepriv->auto_sub_step == 0) && check_wrapped) {
401  return 1;
402  }
403 
404  dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
405  "auto_sub_step:%i started_auto_step:%i\n",
406  __func__, fepriv->lnb_drift, fepriv->inversion,
407  fepriv->auto_step, fepriv->auto_sub_step,
408  fepriv->started_auto_step);
409 
410  /* set the frontend itself */
411  c->frequency += fepriv->lnb_drift;
412  if (autoinversion)
413  c->inversion = fepriv->inversion;
414  tmp = *c;
415  if (fe->ops.set_frontend)
416  fe_set_err = fe->ops.set_frontend(fe);
417  *c = tmp;
418  if (fe_set_err < 0) {
419  fepriv->state = FESTATE_ERROR;
420  return fe_set_err;
421  }
422 
423  c->frequency = original_frequency;
424  c->inversion = original_inversion;
425 
426  fepriv->auto_sub_step++;
427  return 0;
428 }
429 
430 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
431 {
432  fe_status_t s = 0;
433  int retval = 0;
434  struct dvb_frontend_private *fepriv = fe->frontend_priv;
436 
437  /* if we've got no parameters, just keep idling */
438  if (fepriv->state & FESTATE_IDLE) {
439  fepriv->delay = 3*HZ;
440  fepriv->quality = 0;
441  return;
442  }
443 
444  /* in SCAN mode, we just set the frontend when asked and leave it alone */
445  if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
446  if (fepriv->state & FESTATE_RETUNE) {
447  tmp = *c;
448  if (fe->ops.set_frontend)
449  retval = fe->ops.set_frontend(fe);
450  *c = tmp;
451  if (retval < 0)
452  fepriv->state = FESTATE_ERROR;
453  else
454  fepriv->state = FESTATE_TUNED;
455  }
456  fepriv->delay = 3*HZ;
457  fepriv->quality = 0;
458  return;
459  }
460 
461  /* get the frontend status */
462  if (fepriv->state & FESTATE_RETUNE) {
463  s = 0;
464  } else {
465  if (fe->ops.read_status)
466  fe->ops.read_status(fe, &s);
467  if (s != fepriv->status) {
468  dvb_frontend_add_event(fe, s);
469  fepriv->status = s;
470  }
471  }
472 
473  /* if we're not tuned, and we have a lock, move to the TUNED state */
474  if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
475  dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
476  fepriv->state = FESTATE_TUNED;
477 
478  /* if we're tuned, then we have determined the correct inversion */
479  if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
480  (c->inversion == INVERSION_AUTO)) {
481  c->inversion = fepriv->inversion;
482  }
483  return;
484  }
485 
486  /* if we are tuned already, check we're still locked */
487  if (fepriv->state & FESTATE_TUNED) {
488  dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
489 
490  /* we're tuned, and the lock is still good... */
491  if (s & FE_HAS_LOCK) {
492  return;
493  } else { /* if we _WERE_ tuned, but now don't have a lock */
494  fepriv->state = FESTATE_ZIGZAG_FAST;
495  fepriv->started_auto_step = fepriv->auto_step;
496  fepriv->check_wrapped = 0;
497  }
498  }
499 
500  /* don't actually do anything if we're in the LOSTLOCK state,
501  * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
502  if ((fepriv->state & FESTATE_LOSTLOCK) &&
503  (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
504  dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
505  return;
506  }
507 
508  /* don't do anything if we're in the DISEQC state, since this
509  * might be someone with a motorized dish controlled by DISEQC.
510  * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
511  if (fepriv->state & FESTATE_DISEQC) {
512  dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
513  return;
514  }
515 
516  /* if we're in the RETUNE state, set everything up for a brand
517  * new scan, keeping the current inversion setting, as the next
518  * tune is _very_ likely to require the same */
519  if (fepriv->state & FESTATE_RETUNE) {
520  fepriv->lnb_drift = 0;
521  fepriv->auto_step = 0;
522  fepriv->auto_sub_step = 0;
523  fepriv->started_auto_step = 0;
524  fepriv->check_wrapped = 0;
525  }
526 
527  /* fast zigzag. */
528  if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
529  fepriv->delay = fepriv->min_delay;
530 
531  /* perform a tune */
532  retval = dvb_frontend_swzigzag_autotune(fe,
533  fepriv->check_wrapped);
534  if (retval < 0) {
535  return;
536  } else if (retval) {
537  /* OK, if we've run out of trials at the fast speed.
538  * Drop back to slow for the _next_ attempt */
539  fepriv->state = FESTATE_SEARCHING_SLOW;
540  fepriv->started_auto_step = fepriv->auto_step;
541  return;
542  }
543  fepriv->check_wrapped = 1;
544 
545  /* if we've just retuned, enter the ZIGZAG_FAST state.
546  * This ensures we cannot return from an
547  * FE_SET_FRONTEND ioctl before the first frontend tune
548  * occurs */
549  if (fepriv->state & FESTATE_RETUNE) {
550  fepriv->state = FESTATE_TUNING_FAST;
551  }
552  }
553 
554  /* slow zigzag */
555  if (fepriv->state & FESTATE_SEARCHING_SLOW) {
556  dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
557 
558  /* Note: don't bother checking for wrapping; we stay in this
559  * state until we get a lock */
560  dvb_frontend_swzigzag_autotune(fe, 0);
561  }
562 }
563 
564 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
565 {
566  struct dvb_frontend_private *fepriv = fe->frontend_priv;
567 
568  if (fepriv->exit != DVB_FE_NO_EXIT)
569  return 1;
570 
571  if (fepriv->dvbdev->writers == 1)
572  if (time_after_eq(jiffies, fepriv->release_jiffies +
573  dvb_shutdown_timeout * HZ))
574  return 1;
575 
576  return 0;
577 }
578 
579 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
580 {
581  struct dvb_frontend_private *fepriv = fe->frontend_priv;
582 
583  if (fepriv->wakeup) {
584  fepriv->wakeup = 0;
585  return 1;
586  }
587  return dvb_frontend_is_exiting(fe);
588 }
589 
590 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
591 {
592  struct dvb_frontend_private *fepriv = fe->frontend_priv;
593 
594  fepriv->wakeup = 1;
596 }
597 
598 static int dvb_frontend_thread(void *data)
599 {
600  struct dvb_frontend *fe = data;
601  struct dvb_frontend_private *fepriv = fe->frontend_priv;
602  fe_status_t s;
603  enum dvbfe_algo algo;
604 
605  bool re_tune = false;
606 
607  dev_dbg(fe->dvb->device, "%s:\n", __func__);
608 
609  fepriv->check_wrapped = 0;
610  fepriv->quality = 0;
611  fepriv->delay = 3*HZ;
612  fepriv->status = 0;
613  fepriv->wakeup = 0;
614  fepriv->reinitialise = 0;
615 
616  dvb_frontend_init(fe);
617 
618  set_freezable();
619  while (1) {
620  up(&fepriv->sem); /* is locked when we enter the thread... */
621 restart:
623  dvb_frontend_should_wakeup(fe) || kthread_should_stop()
624  || freezing(current),
625  fepriv->delay);
626 
627  if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
628  /* got signal or quitting */
629  fepriv->exit = DVB_FE_NORMAL_EXIT;
630  break;
631  }
632 
633  if (try_to_freeze())
634  goto restart;
635 
636  if (down_interruptible(&fepriv->sem))
637  break;
638 
639  if (fepriv->reinitialise) {
640  dvb_frontend_init(fe);
641  if (fe->ops.set_tone && fepriv->tone != -1)
642  fe->ops.set_tone(fe, fepriv->tone);
643  if (fe->ops.set_voltage && fepriv->voltage != -1)
644  fe->ops.set_voltage(fe, fepriv->voltage);
645  fepriv->reinitialise = 0;
646  }
647 
648  /* do an iteration of the tuning loop */
649  if (fe->ops.get_frontend_algo) {
650  algo = fe->ops.get_frontend_algo(fe);
651  switch (algo) {
652  case DVBFE_ALGO_HW:
653  dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
654 
655  if (fepriv->state & FESTATE_RETUNE) {
656  dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
657  re_tune = true;
658  fepriv->state = FESTATE_TUNED;
659  } else {
660  re_tune = false;
661  }
662 
663  if (fe->ops.tune)
664  fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
665 
666  if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
667  dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
668  dvb_frontend_add_event(fe, s);
669  fepriv->status = s;
670  }
671  break;
672  case DVBFE_ALGO_SW:
673  dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
674  dvb_frontend_swzigzag(fe);
675  break;
676  case DVBFE_ALGO_CUSTOM:
677  dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
678  if (fepriv->state & FESTATE_RETUNE) {
679  dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
680  fepriv->state = FESTATE_TUNED;
681  }
682  /* Case where we are going to search for a carrier
683  * User asked us to retune again for some reason, possibly
684  * requesting a search with a new set of parameters
685  */
686  if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
687  if (fe->ops.search) {
688  fepriv->algo_status = fe->ops.search(fe);
689  /* We did do a search as was requested, the flags are
690  * now unset as well and has the flags wrt to search.
691  */
692  } else {
694  }
695  }
696  /* Track the carrier if the search was successful */
697  if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
699  fepriv->delay = HZ / 2;
700  }
701  dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
702  fe->ops.read_status(fe, &s);
703  if (s != fepriv->status) {
704  dvb_frontend_add_event(fe, s); /* update event list */
705  fepriv->status = s;
706  if (!(s & FE_HAS_LOCK)) {
707  fepriv->delay = HZ / 10;
709  } else {
710  fepriv->delay = 60 * HZ;
711  }
712  }
713  break;
714  default:
715  dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
716  break;
717  }
718  } else {
719  dvb_frontend_swzigzag(fe);
720  }
721  }
722 
723  if (dvb_powerdown_on_sleep) {
724  if (fe->ops.set_voltage)
725  fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
726  if (fe->ops.tuner_ops.sleep) {
727  if (fe->ops.i2c_gate_ctrl)
728  fe->ops.i2c_gate_ctrl(fe, 1);
729  fe->ops.tuner_ops.sleep(fe);
730  if (fe->ops.i2c_gate_ctrl)
731  fe->ops.i2c_gate_ctrl(fe, 0);
732  }
733  if (fe->ops.sleep)
734  fe->ops.sleep(fe);
735  }
736 
737  fepriv->thread = NULL;
738  if (kthread_should_stop())
739  fepriv->exit = DVB_FE_DEVICE_REMOVED;
740  else
741  fepriv->exit = DVB_FE_NO_EXIT;
742  mb();
743 
744  dvb_frontend_wakeup(fe);
745  return 0;
746 }
747 
748 static void dvb_frontend_stop(struct dvb_frontend *fe)
749 {
750  struct dvb_frontend_private *fepriv = fe->frontend_priv;
751 
752  dev_dbg(fe->dvb->device, "%s:\n", __func__);
753 
754  fepriv->exit = DVB_FE_NORMAL_EXIT;
755  mb();
756 
757  if (!fepriv->thread)
758  return;
759 
760  kthread_stop(fepriv->thread);
761 
762  sema_init(&fepriv->sem, 1);
763  fepriv->state = FESTATE_IDLE;
764 
765  /* paranoia check in case a signal arrived */
766  if (fepriv->thread)
767  dev_warn(fe->dvb->device,
768  "dvb_frontend_stop: warning: thread %p won't exit\n",
769  fepriv->thread);
770 }
771 
772 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
773 {
774  return ((curtime.tv_usec < lasttime.tv_usec) ?
775  1000000 - lasttime.tv_usec + curtime.tv_usec :
776  curtime.tv_usec - lasttime.tv_usec);
777 }
779 
780 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
781 {
782  curtime->tv_usec += add_usec;
783  if (curtime->tv_usec >= 1000000) {
784  curtime->tv_usec -= 1000000;
785  curtime->tv_sec++;
786  }
787 }
788 
789 /*
790  * Sleep until gettimeofday() > waketime + add_usec
791  * This needs to be as precise as possible, but as the delay is
792  * usually between 2ms and 32ms, it is done using a scheduled msleep
793  * followed by usleep (normally a busy-wait loop) for the remainder
794  */
795 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
796 {
797  struct timeval lasttime;
798  s32 delta, newdelta;
799 
800  timeval_usec_add(waketime, add_usec);
801 
802  do_gettimeofday(&lasttime);
803  delta = timeval_usec_diff(lasttime, *waketime);
804  if (delta > 2500) {
805  msleep((delta - 1500) / 1000);
806  do_gettimeofday(&lasttime);
807  newdelta = timeval_usec_diff(lasttime, *waketime);
808  delta = (newdelta > delta) ? 0 : newdelta;
809  }
810  if (delta > 0)
811  udelay(delta);
812 }
814 
815 static int dvb_frontend_start(struct dvb_frontend *fe)
816 {
817  int ret;
818  struct dvb_frontend_private *fepriv = fe->frontend_priv;
819  struct task_struct *fe_thread;
820 
821  dev_dbg(fe->dvb->device, "%s:\n", __func__);
822 
823  if (fepriv->thread) {
824  if (fepriv->exit == DVB_FE_NO_EXIT)
825  return 0;
826  else
827  dvb_frontend_stop (fe);
828  }
829 
830  if (signal_pending(current))
831  return -EINTR;
832  if (down_interruptible (&fepriv->sem))
833  return -EINTR;
834 
835  fepriv->state = FESTATE_IDLE;
836  fepriv->exit = DVB_FE_NO_EXIT;
837  fepriv->thread = NULL;
838  mb();
839 
840  fe_thread = kthread_run(dvb_frontend_thread, fe,
841  "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
842  if (IS_ERR(fe_thread)) {
843  ret = PTR_ERR(fe_thread);
844  dev_warn(fe->dvb->device,
845  "dvb_frontend_start: failed to start kthread (%d)\n",
846  ret);
847  up(&fepriv->sem);
848  return ret;
849  }
850  fepriv->thread = fe_thread;
851  return 0;
852 }
853 
854 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
855  u32 *freq_min, u32 *freq_max)
856 {
857  *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
858 
859  if (fe->ops.info.frequency_max == 0)
860  *freq_max = fe->ops.tuner_ops.info.frequency_max;
861  else if (fe->ops.tuner_ops.info.frequency_max == 0)
862  *freq_max = fe->ops.info.frequency_max;
863  else
864  *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
865 
866  if (*freq_min == 0 || *freq_max == 0)
867  dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
868  fe->dvb->num, fe->id);
869 }
870 
871 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
872 {
874  u32 freq_min;
875  u32 freq_max;
876 
877  /* range check: frequency */
878  dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
879  if ((freq_min && c->frequency < freq_min) ||
880  (freq_max && c->frequency > freq_max)) {
881  dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
882  fe->dvb->num, fe->id, c->frequency,
883  freq_min, freq_max);
884  return -EINVAL;
885  }
886 
887  /* range check: symbol rate */
888  switch (c->delivery_system) {
889  case SYS_DVBS:
890  case SYS_DVBS2:
891  case SYS_TURBO:
892  case SYS_DVBC_ANNEX_A:
893  case SYS_DVBC_ANNEX_C:
894  if ((fe->ops.info.symbol_rate_min &&
895  c->symbol_rate < fe->ops.info.symbol_rate_min) ||
896  (fe->ops.info.symbol_rate_max &&
897  c->symbol_rate > fe->ops.info.symbol_rate_max)) {
898  dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
899  fe->dvb->num, fe->id, c->symbol_rate,
900  fe->ops.info.symbol_rate_min,
901  fe->ops.info.symbol_rate_max);
902  return -EINVAL;
903  }
904  default:
905  break;
906  }
907 
908  return 0;
909 }
910 
911 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
912 {
914  int i;
915  u32 delsys;
916 
917  delsys = c->delivery_system;
918  memset(c, 0, sizeof(struct dtv_frontend_properties));
919  c->delivery_system = delsys;
920 
921  c->state = DTV_CLEAR;
922 
923  dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
924  __func__, c->delivery_system);
925 
927  c->bandwidth_hz = 0; /* AUTO */
930  c->symbol_rate = 0;
931  c->code_rate_HP = FEC_AUTO;
932  c->code_rate_LP = FEC_AUTO;
933  c->fec_inner = FEC_AUTO;
934  c->rolloff = ROLLOFF_AUTO;
936  c->sectone = SEC_TONE_OFF;
937  c->pilot = PILOT_AUTO;
938 
940  c->isdbt_sb_mode = 0;
941  c->isdbt_sb_subchannel = 0;
942  c->isdbt_sb_segment_idx = 0;
943  c->isdbt_sb_segment_count = 0;
944  c->isdbt_layer_enabled = 0;
945  for (i = 0; i < 3; i++) {
946  c->layer[i].fec = FEC_AUTO;
947  c->layer[i].modulation = QAM_AUTO;
948  c->layer[i].interleaving = 0;
949  c->layer[i].segment_count = 0;
950  }
951 
953 
954  switch (c->delivery_system) {
955  case SYS_DVBS:
956  case SYS_DVBS2:
957  case SYS_TURBO:
958  c->modulation = QPSK; /* implied for DVB-S in legacy API */
959  c->rolloff = ROLLOFF_35;/* implied for DVB-S */
960  break;
961  case SYS_ATSC:
962  c->modulation = VSB_8;
963  break;
964  default:
965  c->modulation = QAM_AUTO;
966  break;
967  }
968 
969  c->lna = LNA_AUTO;
970 
971  return 0;
972 }
973 
974 #define _DTV_CMD(n, s, b) \
975 [n] = { \
976  .name = #n, \
977  .cmd = n, \
978  .set = s,\
979  .buffer = b \
980 }
981 
982 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
983  _DTV_CMD(DTV_TUNE, 1, 0),
984  _DTV_CMD(DTV_CLEAR, 1, 0),
985 
986  /* Set */
987  _DTV_CMD(DTV_FREQUENCY, 1, 0),
988  _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
989  _DTV_CMD(DTV_MODULATION, 1, 0),
990  _DTV_CMD(DTV_INVERSION, 1, 0),
992  _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
993  _DTV_CMD(DTV_INNER_FEC, 1, 0),
994  _DTV_CMD(DTV_VOLTAGE, 1, 0),
995  _DTV_CMD(DTV_TONE, 1, 0),
996  _DTV_CMD(DTV_PILOT, 1, 0),
997  _DTV_CMD(DTV_ROLLOFF, 1, 0),
999  _DTV_CMD(DTV_HIERARCHY, 1, 0),
1000  _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1001  _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1004  _DTV_CMD(DTV_INTERLEAVING, 1, 0),
1005 
1024 
1025  _DTV_CMD(DTV_STREAM_ID, 1, 0),
1027  _DTV_CMD(DTV_LNA, 1, 0),
1028 
1029  /* Get */
1031  _DTV_CMD(DTV_API_VERSION, 0, 0),
1032  _DTV_CMD(DTV_CODE_RATE_HP, 0, 0),
1033  _DTV_CMD(DTV_CODE_RATE_LP, 0, 0),
1036  _DTV_CMD(DTV_HIERARCHY, 0, 0),
1037  _DTV_CMD(DTV_INTERLEAVING, 0, 0),
1038 
1039  _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1040 
1043 
1046  _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1047  _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1048  _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1049  _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1059 
1060  _DTV_CMD(DTV_LNA, 0, 0),
1061 };
1062 
1063 static void dtv_property_dump(struct dvb_frontend *fe, struct dtv_property *tvp)
1064 {
1065  int i;
1066 
1067  if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1068  dev_warn(fe->dvb->device, "%s: tvp.cmd = 0x%08x undefined\n",
1069  __func__, tvp->cmd);
1070  return;
1071  }
1072 
1073  dev_dbg(fe->dvb->device, "%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1074  tvp->cmd, dtv_cmds[tvp->cmd].name);
1075 
1076  if (dtv_cmds[tvp->cmd].buffer) {
1077  dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1078  __func__, tvp->u.buffer.len);
1079 
1080  for(i = 0; i < tvp->u.buffer.len; i++)
1081  dev_dbg(fe->dvb->device,
1082  "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1083  __func__, i, tvp->u.buffer.data[i]);
1084  } else {
1085  dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1086  tvp->u.data);
1087  }
1088 }
1089 
1090 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1091  * drivers can use a single set_frontend tuning function, regardless of whether
1092  * it's being used for the legacy or new API, reducing code and complexity.
1093  */
1094 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1095  struct dtv_frontend_properties *c,
1096  const struct dvb_frontend_parameters *p)
1097 {
1098  c->frequency = p->frequency;
1099  c->inversion = p->inversion;
1100 
1101  switch (dvbv3_type(c->delivery_system)) {
1102  case DVBV3_QPSK:
1103  dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1104  c->symbol_rate = p->u.qpsk.symbol_rate;
1105  c->fec_inner = p->u.qpsk.fec_inner;
1106  break;
1107  case DVBV3_QAM:
1108  dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1109  c->symbol_rate = p->u.qam.symbol_rate;
1110  c->fec_inner = p->u.qam.fec_inner;
1111  c->modulation = p->u.qam.modulation;
1112  break;
1113  case DVBV3_OFDM:
1114  dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1115 
1116  switch (p->u.ofdm.bandwidth) {
1117  case BANDWIDTH_10_MHZ:
1118  c->bandwidth_hz = 10000000;
1119  break;
1120  case BANDWIDTH_8_MHZ:
1121  c->bandwidth_hz = 8000000;
1122  break;
1123  case BANDWIDTH_7_MHZ:
1124  c->bandwidth_hz = 7000000;
1125  break;
1126  case BANDWIDTH_6_MHZ:
1127  c->bandwidth_hz = 6000000;
1128  break;
1129  case BANDWIDTH_5_MHZ:
1130  c->bandwidth_hz = 5000000;
1131  break;
1132  case BANDWIDTH_1_712_MHZ:
1133  c->bandwidth_hz = 1712000;
1134  break;
1135  case BANDWIDTH_AUTO:
1136  c->bandwidth_hz = 0;
1137  }
1138 
1139  c->code_rate_HP = p->u.ofdm.code_rate_HP;
1140  c->code_rate_LP = p->u.ofdm.code_rate_LP;
1141  c->modulation = p->u.ofdm.constellation;
1142  c->transmission_mode = p->u.ofdm.transmission_mode;
1143  c->guard_interval = p->u.ofdm.guard_interval;
1144  c->hierarchy = p->u.ofdm.hierarchy_information;
1145  break;
1146  case DVBV3_ATSC:
1147  dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
1148  c->modulation = p->u.vsb.modulation;
1149  if (c->delivery_system == SYS_ATSCMH)
1150  break;
1151  if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1153  else
1155  break;
1156  case DVBV3_UNKNOWN:
1157  dev_err(fe->dvb->device,
1158  "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1159  __func__, c->delivery_system);
1160  return -EINVAL;
1161  }
1162 
1163  return 0;
1164 }
1165 
1166 /* Ensure the cached values are set correctly in the frontend
1167  * legacy tuning structures, for the advanced tuning API.
1168  */
1169 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1170  struct dvb_frontend_parameters *p)
1171 {
1172  const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1173 
1174  p->frequency = c->frequency;
1175  p->inversion = c->inversion;
1176 
1177  switch (dvbv3_type(c->delivery_system)) {
1178  case DVBV3_UNKNOWN:
1179  dev_err(fe->dvb->device,
1180  "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1181  __func__, c->delivery_system);
1182  return -EINVAL;
1183  case DVBV3_QPSK:
1184  dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
1185  p->u.qpsk.symbol_rate = c->symbol_rate;
1186  p->u.qpsk.fec_inner = c->fec_inner;
1187  break;
1188  case DVBV3_QAM:
1189  dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
1190  p->u.qam.symbol_rate = c->symbol_rate;
1191  p->u.qam.fec_inner = c->fec_inner;
1192  p->u.qam.modulation = c->modulation;
1193  break;
1194  case DVBV3_OFDM:
1195  dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1196  switch (c->bandwidth_hz) {
1197  case 10000000:
1198  p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1199  break;
1200  case 8000000:
1201  p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1202  break;
1203  case 7000000:
1204  p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1205  break;
1206  case 6000000:
1207  p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1208  break;
1209  case 5000000:
1210  p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1211  break;
1212  case 1712000:
1213  p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1214  break;
1215  case 0:
1216  default:
1217  p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1218  }
1219  p->u.ofdm.code_rate_HP = c->code_rate_HP;
1220  p->u.ofdm.code_rate_LP = c->code_rate_LP;
1221  p->u.ofdm.constellation = c->modulation;
1222  p->u.ofdm.transmission_mode = c->transmission_mode;
1223  p->u.ofdm.guard_interval = c->guard_interval;
1224  p->u.ofdm.hierarchy_information = c->hierarchy;
1225  break;
1226  case DVBV3_ATSC:
1227  dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
1228  p->u.vsb.modulation = c->modulation;
1229  break;
1230  }
1231  return 0;
1232 }
1233 
1244 static int dtv_get_frontend(struct dvb_frontend *fe,
1245  struct dvb_frontend_parameters *p_out)
1246 {
1247  int r;
1248 
1249  if (fe->ops.get_frontend) {
1250  r = fe->ops.get_frontend(fe);
1251  if (unlikely(r < 0))
1252  return r;
1253  if (p_out)
1254  dtv_property_legacy_params_sync(fe, p_out);
1255  return 0;
1256  }
1257 
1258  /* As everything is in cache, get_frontend fops are always supported */
1259  return 0;
1260 }
1261 
1262 static int dvb_frontend_ioctl_legacy(struct file *file,
1263  unsigned int cmd, void *parg);
1264 static int dvb_frontend_ioctl_properties(struct file *file,
1265  unsigned int cmd, void *parg);
1266 
1267 static int dtv_property_process_get(struct dvb_frontend *fe,
1268  const struct dtv_frontend_properties *c,
1269  struct dtv_property *tvp,
1270  struct file *file)
1271 {
1272  int r, ncaps;
1273 
1274  switch(tvp->cmd) {
1275  case DTV_ENUM_DELSYS:
1276  ncaps = 0;
1277  while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1278  tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1279  ncaps++;
1280  }
1281  tvp->u.buffer.len = ncaps;
1282  break;
1283  case DTV_FREQUENCY:
1284  tvp->u.data = c->frequency;
1285  break;
1286  case DTV_MODULATION:
1287  tvp->u.data = c->modulation;
1288  break;
1289  case DTV_BANDWIDTH_HZ:
1290  tvp->u.data = c->bandwidth_hz;
1291  break;
1292  case DTV_INVERSION:
1293  tvp->u.data = c->inversion;
1294  break;
1295  case DTV_SYMBOL_RATE:
1296  tvp->u.data = c->symbol_rate;
1297  break;
1298  case DTV_INNER_FEC:
1299  tvp->u.data = c->fec_inner;
1300  break;
1301  case DTV_PILOT:
1302  tvp->u.data = c->pilot;
1303  break;
1304  case DTV_ROLLOFF:
1305  tvp->u.data = c->rolloff;
1306  break;
1307  case DTV_DELIVERY_SYSTEM:
1308  tvp->u.data = c->delivery_system;
1309  break;
1310  case DTV_VOLTAGE:
1311  tvp->u.data = c->voltage;
1312  break;
1313  case DTV_TONE:
1314  tvp->u.data = c->sectone;
1315  break;
1316  case DTV_API_VERSION:
1317  tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1318  break;
1319  case DTV_CODE_RATE_HP:
1320  tvp->u.data = c->code_rate_HP;
1321  break;
1322  case DTV_CODE_RATE_LP:
1323  tvp->u.data = c->code_rate_LP;
1324  break;
1325  case DTV_GUARD_INTERVAL:
1326  tvp->u.data = c->guard_interval;
1327  break;
1328  case DTV_TRANSMISSION_MODE:
1329  tvp->u.data = c->transmission_mode;
1330  break;
1331  case DTV_HIERARCHY:
1332  tvp->u.data = c->hierarchy;
1333  break;
1334  case DTV_INTERLEAVING:
1335  tvp->u.data = c->interleaving;
1336  break;
1337 
1338  /* ISDB-T Support here */
1340  tvp->u.data = c->isdbt_partial_reception;
1341  break;
1343  tvp->u.data = c->isdbt_sb_mode;
1344  break;
1346  tvp->u.data = c->isdbt_sb_subchannel;
1347  break;
1349  tvp->u.data = c->isdbt_sb_segment_idx;
1350  break;
1352  tvp->u.data = c->isdbt_sb_segment_count;
1353  break;
1355  tvp->u.data = c->isdbt_layer_enabled;
1356  break;
1357  case DTV_ISDBT_LAYERA_FEC:
1358  tvp->u.data = c->layer[0].fec;
1359  break;
1361  tvp->u.data = c->layer[0].modulation;
1362  break;
1364  tvp->u.data = c->layer[0].segment_count;
1365  break;
1367  tvp->u.data = c->layer[0].interleaving;
1368  break;
1369  case DTV_ISDBT_LAYERB_FEC:
1370  tvp->u.data = c->layer[1].fec;
1371  break;
1373  tvp->u.data = c->layer[1].modulation;
1374  break;
1376  tvp->u.data = c->layer[1].segment_count;
1377  break;
1379  tvp->u.data = c->layer[1].interleaving;
1380  break;
1381  case DTV_ISDBT_LAYERC_FEC:
1382  tvp->u.data = c->layer[2].fec;
1383  break;
1385  tvp->u.data = c->layer[2].modulation;
1386  break;
1388  tvp->u.data = c->layer[2].segment_count;
1389  break;
1391  tvp->u.data = c->layer[2].interleaving;
1392  break;
1393 
1394  /* Multistream support */
1395  case DTV_STREAM_ID:
1397  tvp->u.data = c->stream_id;
1398  break;
1399 
1400  /* ATSC-MH */
1401  case DTV_ATSCMH_FIC_VER:
1402  tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1403  break;
1404  case DTV_ATSCMH_PARADE_ID:
1405  tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1406  break;
1407  case DTV_ATSCMH_NOG:
1408  tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1409  break;
1410  case DTV_ATSCMH_TNOG:
1411  tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1412  break;
1413  case DTV_ATSCMH_SGN:
1414  tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1415  break;
1416  case DTV_ATSCMH_PRC:
1417  tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1418  break;
1420  tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1421  break;
1423  tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1424  break;
1426  tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1427  break;
1429  tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1430  break;
1432  tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1433  break;
1435  tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1436  break;
1438  tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1439  break;
1441  tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1442  break;
1444  tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1445  break;
1446 
1447  case DTV_LNA:
1448  tvp->u.data = c->lna;
1449  break;
1450 
1451  default:
1452  return -EINVAL;
1453  }
1454 
1455  /* Allow the frontend to override outgoing properties */
1456  if (fe->ops.get_property) {
1457  r = fe->ops.get_property(fe, tvp);
1458  if (r < 0)
1459  return r;
1460  }
1461 
1462  dtv_property_dump(fe, tvp);
1463 
1464  return 0;
1465 }
1466 
1467 static int dtv_set_frontend(struct dvb_frontend *fe);
1468 
1469 static bool is_dvbv3_delsys(u32 delsys)
1470 {
1471  bool status;
1472 
1473  status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1474  (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1475 
1476  return status;
1477 }
1478 
1479 static int set_delivery_system(struct dvb_frontend *fe, u32 desired_system)
1480 {
1481  int ncaps, i;
1482  u32 delsys = SYS_UNDEFINED;
1485 
1486  /*
1487  * It was reported that some old DVBv5 applications were
1488  * filling delivery_system with SYS_UNDEFINED. If this happens,
1489  * assume that the application wants to use the first supported
1490  * delivery system.
1491  */
1492  if (c->delivery_system == SYS_UNDEFINED)
1493  c->delivery_system = fe->ops.delsys[0];
1494 
1495  if (desired_system == SYS_UNDEFINED) {
1496  /*
1497  * A DVBv3 call doesn't know what's the desired system.
1498  * Also, DVBv3 applications don't know that ops.info->type
1499  * could be changed, and they simply dies when it doesn't
1500  * match.
1501  * So, don't change the current delivery system, as it
1502  * may be trying to do the wrong thing, like setting an
1503  * ISDB-T frontend as DVB-T. Instead, find the closest
1504  * DVBv3 system that matches the delivery system.
1505  */
1506  if (is_dvbv3_delsys(c->delivery_system)) {
1507  dev_dbg(fe->dvb->device,
1508  "%s: Using delivery system to %d\n",
1509  __func__, c->delivery_system);
1510  return 0;
1511  }
1512  type = dvbv3_type(c->delivery_system);
1513  switch (type) {
1514  case DVBV3_QPSK:
1515  desired_system = SYS_DVBS;
1516  break;
1517  case DVBV3_QAM:
1518  desired_system = SYS_DVBC_ANNEX_A;
1519  break;
1520  case DVBV3_ATSC:
1521  desired_system = SYS_ATSC;
1522  break;
1523  case DVBV3_OFDM:
1524  desired_system = SYS_DVBT;
1525  break;
1526  default:
1527  dev_dbg(fe->dvb->device, "%s: This frontend doesn't support DVBv3 calls\n",
1528  __func__);
1529  return -EINVAL;
1530  }
1531  /*
1532  * Get a delivery system that is compatible with DVBv3
1533  * NOTE: in order for this to work with softwares like Kaffeine that
1534  * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1535  * DVB-S, drivers that support both should put the SYS_DVBS entry
1536  * before the SYS_DVBS2, otherwise it won't switch back to DVB-S.
1537  * The real fix is that userspace applications should not use DVBv3
1538  * and not trust on calling FE_SET_FRONTEND to switch the delivery
1539  * system.
1540  */
1541  ncaps = 0;
1542  while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1543  if (fe->ops.delsys[ncaps] == desired_system) {
1544  delsys = desired_system;
1545  break;
1546  }
1547  ncaps++;
1548  }
1549  if (delsys == SYS_UNDEFINED) {
1550  dev_dbg(fe->dvb->device, "%s: Couldn't find a delivery system that matches %d\n",
1551  __func__, desired_system);
1552  }
1553  } else {
1554  /*
1555  * This is a DVBv5 call. So, it likely knows the supported
1556  * delivery systems.
1557  */
1558 
1559  /* Check if the desired delivery system is supported */
1560  ncaps = 0;
1561  while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1562  if (fe->ops.delsys[ncaps] == desired_system) {
1563  c->delivery_system = desired_system;
1564  dev_dbg(fe->dvb->device,
1565  "%s: Changing delivery system to %d\n",
1566  __func__, desired_system);
1567  return 0;
1568  }
1569  ncaps++;
1570  }
1571  type = dvbv3_type(desired_system);
1572 
1573  /*
1574  * The delivery system is not supported. See if it can be
1575  * emulated.
1576  * The emulation only works if the desired system is one of the
1577  * DVBv3 delivery systems
1578  */
1579  if (!is_dvbv3_delsys(desired_system)) {
1580  dev_dbg(fe->dvb->device,
1581  "%s: can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1582  __func__);
1583  return -EINVAL;
1584  }
1585 
1586  /*
1587  * Get the last non-DVBv3 delivery system that has the same type
1588  * of the desired system
1589  */
1590  ncaps = 0;
1591  while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1592  if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
1593  !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
1594  delsys = fe->ops.delsys[ncaps];
1595  ncaps++;
1596  }
1597  /* There's nothing compatible with the desired delivery system */
1598  if (delsys == SYS_UNDEFINED) {
1599  dev_dbg(fe->dvb->device,
1600  "%s: Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1601  __func__);
1602  return -EINVAL;
1603  }
1604  }
1605 
1606  c->delivery_system = delsys;
1607 
1608  /*
1609  * The DVBv3 or DVBv5 call is requesting a different system. So,
1610  * emulation is needed.
1611  *
1612  * Emulate newer delivery systems like ISDBT, DVBT and DTMB
1613  * for older DVBv5 applications. The emulation will try to use
1614  * the auto mode for most things, and will assume that the desired
1615  * delivery system is the last one at the ops.delsys[] array
1616  */
1617  dev_dbg(fe->dvb->device,
1618  "%s: Using delivery system %d emulated as if it were a %d\n",
1619  __func__, delsys, desired_system);
1620 
1621  /*
1622  * For now, handles ISDB-T calls. More code may be needed here for the
1623  * other emulated stuff
1624  */
1625  if (type == DVBV3_OFDM) {
1626  if (c->delivery_system == SYS_ISDBT) {
1627  dev_dbg(fe->dvb->device,
1628  "%s: Using defaults for SYS_ISDBT\n",
1629  __func__);
1630 
1631  if (!c->bandwidth_hz)
1632  c->bandwidth_hz = 6000000;
1633 
1634  c->isdbt_partial_reception = 0;
1635  c->isdbt_sb_mode = 0;
1636  c->isdbt_sb_subchannel = 0;
1637  c->isdbt_sb_segment_idx = 0;
1638  c->isdbt_sb_segment_count = 0;
1639  c->isdbt_layer_enabled = 0;
1640  for (i = 0; i < 3; i++) {
1641  c->layer[i].fec = FEC_AUTO;
1642  c->layer[i].modulation = QAM_AUTO;
1643  c->layer[i].interleaving = 0;
1644  c->layer[i].segment_count = 0;
1645  }
1646  }
1647  }
1648  dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
1649  __func__, c->delivery_system);
1650 
1651  return 0;
1652 }
1653 
1654 static int dtv_property_process_set(struct dvb_frontend *fe,
1655  struct dtv_property *tvp,
1656  struct file *file)
1657 {
1658  int r = 0;
1660 
1661  /* Allow the frontend to validate incoming properties */
1662  if (fe->ops.set_property) {
1663  r = fe->ops.set_property(fe, tvp);
1664  if (r < 0)
1665  return r;
1666  }
1667 
1668  switch(tvp->cmd) {
1669  case DTV_CLEAR:
1670  /*
1671  * Reset a cache of data specific to the frontend here. This does
1672  * not effect hardware.
1673  */
1674  dvb_frontend_clear_cache(fe);
1675  break;
1676  case DTV_TUNE:
1677  /* interpret the cache of data, build either a traditional frontend
1678  * tunerequest so we can pass validation in the FE_SET_FRONTEND
1679  * ioctl.
1680  */
1681  c->state = tvp->cmd;
1682  dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1683  __func__);
1684 
1685  r = dtv_set_frontend(fe);
1686  break;
1687  case DTV_FREQUENCY:
1688  c->frequency = tvp->u.data;
1689  break;
1690  case DTV_MODULATION:
1691  c->modulation = tvp->u.data;
1692  break;
1693  case DTV_BANDWIDTH_HZ:
1694  c->bandwidth_hz = tvp->u.data;
1695  break;
1696  case DTV_INVERSION:
1697  c->inversion = tvp->u.data;
1698  break;
1699  case DTV_SYMBOL_RATE:
1700  c->symbol_rate = tvp->u.data;
1701  break;
1702  case DTV_INNER_FEC:
1703  c->fec_inner = tvp->u.data;
1704  break;
1705  case DTV_PILOT:
1706  c->pilot = tvp->u.data;
1707  break;
1708  case DTV_ROLLOFF:
1709  c->rolloff = tvp->u.data;
1710  break;
1711  case DTV_DELIVERY_SYSTEM:
1712  r = set_delivery_system(fe, tvp->u.data);
1713  break;
1714  case DTV_VOLTAGE:
1715  c->voltage = tvp->u.data;
1716  r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1717  (void *)c->voltage);
1718  break;
1719  case DTV_TONE:
1720  c->sectone = tvp->u.data;
1721  r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1722  (void *)c->sectone);
1723  break;
1724  case DTV_CODE_RATE_HP:
1725  c->code_rate_HP = tvp->u.data;
1726  break;
1727  case DTV_CODE_RATE_LP:
1728  c->code_rate_LP = tvp->u.data;
1729  break;
1730  case DTV_GUARD_INTERVAL:
1731  c->guard_interval = tvp->u.data;
1732  break;
1733  case DTV_TRANSMISSION_MODE:
1734  c->transmission_mode = tvp->u.data;
1735  break;
1736  case DTV_HIERARCHY:
1737  c->hierarchy = tvp->u.data;
1738  break;
1739  case DTV_INTERLEAVING:
1740  c->interleaving = tvp->u.data;
1741  break;
1742 
1743  /* ISDB-T Support here */
1745  c->isdbt_partial_reception = tvp->u.data;
1746  break;
1748  c->isdbt_sb_mode = tvp->u.data;
1749  break;
1751  c->isdbt_sb_subchannel = tvp->u.data;
1752  break;
1754  c->isdbt_sb_segment_idx = tvp->u.data;
1755  break;
1757  c->isdbt_sb_segment_count = tvp->u.data;
1758  break;
1760  c->isdbt_layer_enabled = tvp->u.data;
1761  break;
1762  case DTV_ISDBT_LAYERA_FEC:
1763  c->layer[0].fec = tvp->u.data;
1764  break;
1766  c->layer[0].modulation = tvp->u.data;
1767  break;
1769  c->layer[0].segment_count = tvp->u.data;
1770  break;
1772  c->layer[0].interleaving = tvp->u.data;
1773  break;
1774  case DTV_ISDBT_LAYERB_FEC:
1775  c->layer[1].fec = tvp->u.data;
1776  break;
1778  c->layer[1].modulation = tvp->u.data;
1779  break;
1781  c->layer[1].segment_count = tvp->u.data;
1782  break;
1784  c->layer[1].interleaving = tvp->u.data;
1785  break;
1786  case DTV_ISDBT_LAYERC_FEC:
1787  c->layer[2].fec = tvp->u.data;
1788  break;
1790  c->layer[2].modulation = tvp->u.data;
1791  break;
1793  c->layer[2].segment_count = tvp->u.data;
1794  break;
1796  c->layer[2].interleaving = tvp->u.data;
1797  break;
1798 
1799  /* Multistream support */
1800  case DTV_STREAM_ID:
1802  c->stream_id = tvp->u.data;
1803  break;
1804 
1805  /* ATSC-MH */
1806  case DTV_ATSCMH_PARADE_ID:
1807  fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1808  break;
1810  fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1811  break;
1812 
1813  case DTV_LNA:
1814  c->lna = tvp->u.data;
1815  if (fe->ops.set_lna)
1816  r = fe->ops.set_lna(fe);
1817  break;
1818 
1819  default:
1820  return -EINVAL;
1821  }
1822 
1823  return r;
1824 }
1825 
1826 static int dvb_frontend_ioctl(struct file *file,
1827  unsigned int cmd, void *parg)
1828 {
1829  struct dvb_device *dvbdev = file->private_data;
1830  struct dvb_frontend *fe = dvbdev->priv;
1832  struct dvb_frontend_private *fepriv = fe->frontend_priv;
1833  int err = -ENOTTY;
1834 
1835  dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
1836  if (fepriv->exit != DVB_FE_NO_EXIT)
1837  return -ENODEV;
1838 
1839  if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1840  (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1842  return -EPERM;
1843 
1844  if (down_interruptible (&fepriv->sem))
1845  return -ERESTARTSYS;
1846 
1847  if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1848  err = dvb_frontend_ioctl_properties(file, cmd, parg);
1849  else {
1850  c->state = DTV_UNDEFINED;
1851  err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1852  }
1853 
1854  up(&fepriv->sem);
1855  return err;
1856 }
1857 
1858 static int dvb_frontend_ioctl_properties(struct file *file,
1859  unsigned int cmd, void *parg)
1860 {
1861  struct dvb_device *dvbdev = file->private_data;
1862  struct dvb_frontend *fe = dvbdev->priv;
1863  struct dvb_frontend_private *fepriv = fe->frontend_priv;
1865  int err = 0;
1866 
1867  struct dtv_properties *tvps = NULL;
1868  struct dtv_property *tvp = NULL;
1869  int i;
1870 
1871  dev_dbg(fe->dvb->device, "%s:\n", __func__);
1872 
1873  if(cmd == FE_SET_PROPERTY) {
1874  tvps = (struct dtv_properties __user *)parg;
1875 
1876  dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1877  dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
1878 
1879  /* Put an arbitrary limit on the number of messages that can
1880  * be sent at once */
1881  if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1882  return -EINVAL;
1883 
1884  tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1885  if (!tvp) {
1886  err = -ENOMEM;
1887  goto out;
1888  }
1889 
1890  if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1891  err = -EFAULT;
1892  goto out;
1893  }
1894 
1895  for (i = 0; i < tvps->num; i++) {
1896  err = dtv_property_process_set(fe, tvp + i, file);
1897  if (err < 0)
1898  goto out;
1899  (tvp + i)->result = err;
1900  }
1901 
1902  if (c->state == DTV_TUNE)
1903  dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
1904 
1905  } else
1906  if(cmd == FE_GET_PROPERTY) {
1907  tvps = (struct dtv_properties __user *)parg;
1908 
1909  dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1910  dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
1911 
1912  /* Put an arbitrary limit on the number of messages that can
1913  * be sent at once */
1914  if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1915  return -EINVAL;
1916 
1917  tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1918  if (!tvp) {
1919  err = -ENOMEM;
1920  goto out;
1921  }
1922 
1923  if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1924  err = -EFAULT;
1925  goto out;
1926  }
1927 
1928  /*
1929  * Fills the cache out struct with the cache contents, plus
1930  * the data retrieved from get_frontend, if the frontend
1931  * is not idle. Otherwise, returns the cached content
1932  */
1933  if (fepriv->state != FESTATE_IDLE) {
1934  err = dtv_get_frontend(fe, NULL);
1935  if (err < 0)
1936  goto out;
1937  }
1938  for (i = 0; i < tvps->num; i++) {
1939  err = dtv_property_process_get(fe, c, tvp + i, file);
1940  if (err < 0)
1941  goto out;
1942  (tvp + i)->result = err;
1943  }
1944 
1945  if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1946  err = -EFAULT;
1947  goto out;
1948  }
1949 
1950  } else
1951  err = -ENOTTY;
1952 
1953 out:
1954  kfree(tvp);
1955  return err;
1956 }
1957 
1958 static int dtv_set_frontend(struct dvb_frontend *fe)
1959 {
1960  struct dvb_frontend_private *fepriv = fe->frontend_priv;
1962  struct dvb_frontend_tune_settings fetunesettings;
1963  u32 rolloff = 0;
1964 
1965  if (dvb_frontend_check_parameters(fe) < 0)
1966  return -EINVAL;
1967 
1968  /*
1969  * Initialize output parameters to match the values given by
1970  * the user. FE_SET_FRONTEND triggers an initial frontend event
1971  * with status = 0, which copies output parameters to userspace.
1972  */
1973  dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
1974 
1975  /*
1976  * Be sure that the bandwidth will be filled for all
1977  * non-satellite systems, as tuners need to know what
1978  * low pass/Nyquist half filter should be applied, in
1979  * order to avoid inter-channel noise.
1980  *
1981  * ISDB-T and DVB-T/T2 already sets bandwidth.
1982  * ATSC and DVB-C don't set, so, the core should fill it.
1983  *
1984  * On DVB-C Annex A and C, the bandwidth is a function of
1985  * the roll-off and symbol rate. Annex B defines different
1986  * roll-off factors depending on the modulation. Fortunately,
1987  * Annex B is only used with 6MHz, so there's no need to
1988  * calculate it.
1989  *
1990  * While not officially supported, a side effect of handling it at
1991  * the cache level is that a program could retrieve the bandwidth
1992  * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1993  */
1994  switch (c->delivery_system) {
1995  case SYS_ATSC:
1996  case SYS_DVBC_ANNEX_B:
1997  c->bandwidth_hz = 6000000;
1998  break;
1999  case SYS_DVBC_ANNEX_A:
2000  rolloff = 115;
2001  break;
2002  case SYS_DVBC_ANNEX_C:
2003  rolloff = 113;
2004  break;
2005  default:
2006  break;
2007  }
2008  if (rolloff)
2009  c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
2010 
2011  /* force auto frequency inversion if requested */
2012  if (dvb_force_auto_inversion)
2014 
2015  /*
2016  * without hierarchical coding code_rate_LP is irrelevant,
2017  * so we tolerate the otherwise invalid FEC_NONE setting
2018  */
2019  if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2020  c->code_rate_LP = FEC_AUTO;
2021 
2022  /* get frontend-specific tuning settings */
2023  memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
2024  if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2025  fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2026  fepriv->max_drift = fetunesettings.max_drift;
2027  fepriv->step_size = fetunesettings.step_size;
2028  } else {
2029  /* default values */
2030  switch (c->delivery_system) {
2031  case SYS_DVBS:
2032  case SYS_DVBS2:
2033  case SYS_ISDBS:
2034  case SYS_TURBO:
2035  case SYS_DVBC_ANNEX_A:
2036  case SYS_DVBC_ANNEX_C:
2037  fepriv->min_delay = HZ / 20;
2038  fepriv->step_size = c->symbol_rate / 16000;
2039  fepriv->max_drift = c->symbol_rate / 2000;
2040  break;
2041  case SYS_DVBT:
2042  case SYS_DVBT2:
2043  case SYS_ISDBT:
2044  case SYS_DTMB:
2045  fepriv->min_delay = HZ / 20;
2046  fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2047  fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2048  break;
2049  default:
2050  /*
2051  * FIXME: This sounds wrong! if freqency_stepsize is
2052  * defined by the frontend, why not use it???
2053  */
2054  fepriv->min_delay = HZ / 20;
2055  fepriv->step_size = 0; /* no zigzag */
2056  fepriv->max_drift = 0;
2057  break;
2058  }
2059  }
2060  if (dvb_override_tune_delay > 0)
2061  fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2062 
2063  fepriv->state = FESTATE_RETUNE;
2064 
2065  /* Request the search algorithm to search */
2067 
2068  dvb_frontend_clear_events(fe);
2069  dvb_frontend_add_event(fe, 0);
2070  dvb_frontend_wakeup(fe);
2071  fepriv->status = 0;
2072 
2073  return 0;
2074 }
2075 
2076 
2077 static int dvb_frontend_ioctl_legacy(struct file *file,
2078  unsigned int cmd, void *parg)
2079 {
2080  struct dvb_device *dvbdev = file->private_data;
2081  struct dvb_frontend *fe = dvbdev->priv;
2082  struct dvb_frontend_private *fepriv = fe->frontend_priv;
2084  int err = -ENOTTY;
2085 
2086  switch (cmd) {
2087  case FE_GET_INFO: {
2088  struct dvb_frontend_info* info = parg;
2089 
2090  memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
2091  dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
2092 
2093  /*
2094  * Associate the 4 delivery systems supported by DVBv3
2095  * API with their DVBv5 counterpart. For the other standards,
2096  * use the closest type, assuming that it would hopefully
2097  * work with a DVBv3 application.
2098  * It should be noticed that, on multi-frontend devices with
2099  * different types (terrestrial and cable, for example),
2100  * a pure DVBv3 application won't be able to use all delivery
2101  * systems. Yet, changing the DVBv5 cache to the other delivery
2102  * system should be enough for making it work.
2103  */
2104  switch (dvbv3_type(c->delivery_system)) {
2105  case DVBV3_QPSK:
2106  info->type = FE_QPSK;
2107  break;
2108  case DVBV3_ATSC:
2109  info->type = FE_ATSC;
2110  break;
2111  case DVBV3_QAM:
2112  info->type = FE_QAM;
2113  break;
2114  case DVBV3_OFDM:
2115  info->type = FE_OFDM;
2116  break;
2117  default:
2118  dev_err(fe->dvb->device,
2119  "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2120  __func__, c->delivery_system);
2121  fe->ops.info.type = FE_OFDM;
2122  }
2123  dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2124  __func__, c->delivery_system, fe->ops.info.type);
2125 
2126  /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2127  * do it, it is done for it. */
2128  info->caps |= FE_CAN_INVERSION_AUTO;
2129  err = 0;
2130  break;
2131  }
2132 
2133  case FE_READ_STATUS: {
2134  fe_status_t* status = parg;
2135 
2136  /* if retune was requested but hasn't occurred yet, prevent
2137  * that user get signal state from previous tuning */
2138  if (fepriv->state == FESTATE_RETUNE ||
2139  fepriv->state == FESTATE_ERROR) {
2140  err=0;
2141  *status = 0;
2142  break;
2143  }
2144 
2145  if (fe->ops.read_status)
2146  err = fe->ops.read_status(fe, status);
2147  break;
2148  }
2149 
2150  case FE_READ_BER:
2151  if (fe->ops.read_ber) {
2152  if (fepriv->thread)
2153  err = fe->ops.read_ber(fe, (__u32 *) parg);
2154  else
2155  err = -EAGAIN;
2156  }
2157  break;
2158 
2160  if (fe->ops.read_signal_strength) {
2161  if (fepriv->thread)
2162  err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2163  else
2164  err = -EAGAIN;
2165  }
2166  break;
2167 
2168  case FE_READ_SNR:
2169  if (fe->ops.read_snr) {
2170  if (fepriv->thread)
2171  err = fe->ops.read_snr(fe, (__u16 *) parg);
2172  else
2173  err = -EAGAIN;
2174  }
2175  break;
2176 
2178  if (fe->ops.read_ucblocks) {
2179  if (fepriv->thread)
2180  err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2181  else
2182  err = -EAGAIN;
2183  }
2184  break;
2185 
2187  if (fe->ops.diseqc_reset_overload) {
2188  err = fe->ops.diseqc_reset_overload(fe);
2189  fepriv->state = FESTATE_DISEQC;
2190  fepriv->status = 0;
2191  }
2192  break;
2193 
2195  if (fe->ops.diseqc_send_master_cmd) {
2196  err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2197  fepriv->state = FESTATE_DISEQC;
2198  fepriv->status = 0;
2199  }
2200  break;
2201 
2202  case FE_DISEQC_SEND_BURST:
2203  if (fe->ops.diseqc_send_burst) {
2204  err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2205  fepriv->state = FESTATE_DISEQC;
2206  fepriv->status = 0;
2207  }
2208  break;
2209 
2210  case FE_SET_TONE:
2211  if (fe->ops.set_tone) {
2212  err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2213  fepriv->tone = (fe_sec_tone_mode_t) parg;
2214  fepriv->state = FESTATE_DISEQC;
2215  fepriv->status = 0;
2216  }
2217  break;
2218 
2219  case FE_SET_VOLTAGE:
2220  if (fe->ops.set_voltage) {
2221  err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2222  fepriv->voltage = (fe_sec_voltage_t) parg;
2223  fepriv->state = FESTATE_DISEQC;
2224  fepriv->status = 0;
2225  }
2226  break;
2227 
2229  if (fe->ops.dishnetwork_send_legacy_command) {
2230  err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2231  fepriv->state = FESTATE_DISEQC;
2232  fepriv->status = 0;
2233  } else if (fe->ops.set_voltage) {
2234  /*
2235  * NOTE: This is a fallback condition. Some frontends
2236  * (stv0299 for instance) take longer than 8msec to
2237  * respond to a set_voltage command. Those switches
2238  * need custom routines to switch properly. For all
2239  * other frontends, the following should work ok.
2240  * Dish network legacy switches (as used by Dish500)
2241  * are controlled by sending 9-bit command words
2242  * spaced 8msec apart.
2243  * the actual command word is switch/port dependent
2244  * so it is up to the userspace application to send
2245  * the right command.
2246  * The command must always start with a '0' after
2247  * initialization, so parg is 8 bits and does not
2248  * include the initialization or start bit
2249  */
2250  unsigned long swcmd = ((unsigned long) parg) << 1;
2251  struct timeval nexttime;
2252  struct timeval tv[10];
2253  int i;
2254  u8 last = 1;
2255  if (dvb_frontend_debug)
2256  printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2257  do_gettimeofday(&nexttime);
2258  if (dvb_frontend_debug)
2259  memcpy(&tv[0], &nexttime, sizeof(struct timeval));
2260  /* before sending a command, initialize by sending
2261  * a 32ms 18V to the switch
2262  */
2263  fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2264  dvb_frontend_sleep_until(&nexttime, 32000);
2265 
2266  for (i = 0; i < 9; i++) {
2267  if (dvb_frontend_debug)
2268  do_gettimeofday(&tv[i + 1]);
2269  if ((swcmd & 0x01) != last) {
2270  /* set voltage to (last ? 13V : 18V) */
2271  fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2272  last = (last) ? 0 : 1;
2273  }
2274  swcmd = swcmd >> 1;
2275  if (i != 8)
2276  dvb_frontend_sleep_until(&nexttime, 8000);
2277  }
2278  if (dvb_frontend_debug) {
2279  printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2280  __func__, fe->dvb->num);
2281  for (i = 1; i < 10; i++)
2282  printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2283  }
2284  err = 0;
2285  fepriv->state = FESTATE_DISEQC;
2286  fepriv->status = 0;
2287  }
2288  break;
2289 
2291  if (fe->ops.diseqc_recv_slave_reply)
2292  err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2293  break;
2294 
2296  if (fe->ops.enable_high_lnb_voltage)
2297  err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2298  break;
2299 
2300  case FE_SET_FRONTEND:
2301  err = set_delivery_system(fe, SYS_UNDEFINED);
2302  if (err)
2303  break;
2304 
2305  err = dtv_property_cache_sync(fe, c, parg);
2306  if (err)
2307  break;
2308  err = dtv_set_frontend(fe);
2309  break;
2310  case FE_GET_EVENT:
2311  err = dvb_frontend_get_event (fe, parg, file->f_flags);
2312  break;
2313 
2314  case FE_GET_FRONTEND:
2315  err = dtv_get_frontend(fe, parg);
2316  break;
2317 
2319  fepriv->tune_mode_flags = (unsigned long) parg;
2320  err = 0;
2321  break;
2322  }
2323 
2324  return err;
2325 }
2326 
2327 
2328 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2329 {
2330  struct dvb_device *dvbdev = file->private_data;
2331  struct dvb_frontend *fe = dvbdev->priv;
2332  struct dvb_frontend_private *fepriv = fe->frontend_priv;
2333 
2334  dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
2335 
2336  poll_wait (file, &fepriv->events.wait_queue, wait);
2337 
2338  if (fepriv->events.eventw != fepriv->events.eventr)
2339  return (POLLIN | POLLRDNORM | POLLPRI);
2340 
2341  return 0;
2342 }
2343 
2344 static int dvb_frontend_open(struct inode *inode, struct file *file)
2345 {
2346  struct dvb_device *dvbdev = file->private_data;
2347  struct dvb_frontend *fe = dvbdev->priv;
2348  struct dvb_frontend_private *fepriv = fe->frontend_priv;
2349  struct dvb_adapter *adapter = fe->dvb;
2350  int ret;
2351 
2352  dev_dbg(fe->dvb->device, "%s:\n", __func__);
2353  if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2354  return -ENODEV;
2355 
2356  if (adapter->mfe_shared) {
2357  mutex_lock (&adapter->mfe_lock);
2358 
2359  if (adapter->mfe_dvbdev == NULL)
2360  adapter->mfe_dvbdev = dvbdev;
2361 
2362  else if (adapter->mfe_dvbdev != dvbdev) {
2363  struct dvb_device
2364  *mfedev = adapter->mfe_dvbdev;
2365  struct dvb_frontend
2366  *mfe = mfedev->priv;
2367  struct dvb_frontend_private
2368  *mfepriv = mfe->frontend_priv;
2369  int mferetry = (dvb_mfe_wait_time << 1);
2370 
2371  mutex_unlock (&adapter->mfe_lock);
2372  while (mferetry-- && (mfedev->users != -1 ||
2373  mfepriv->thread != NULL)) {
2374  if(msleep_interruptible(500)) {
2375  if(signal_pending(current))
2376  return -EINTR;
2377  }
2378  }
2379 
2380  mutex_lock (&adapter->mfe_lock);
2381  if(adapter->mfe_dvbdev != dvbdev) {
2382  mfedev = adapter->mfe_dvbdev;
2383  mfe = mfedev->priv;
2384  mfepriv = mfe->frontend_priv;
2385  if (mfedev->users != -1 ||
2386  mfepriv->thread != NULL) {
2387  mutex_unlock (&adapter->mfe_lock);
2388  return -EBUSY;
2389  }
2390  adapter->mfe_dvbdev = dvbdev;
2391  }
2392  }
2393  }
2394 
2395  if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2396  if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2397  goto err0;
2398 
2399  /* If we took control of the bus, we need to force
2400  reinitialization. This is because many ts_bus_ctrl()
2401  functions strobe the RESET pin on the demod, and if the
2402  frontend thread already exists then the dvb_init() routine
2403  won't get called (which is what usually does initial
2404  register configuration). */
2405  fepriv->reinitialise = 1;
2406  }
2407 
2408  if ((ret = dvb_generic_open (inode, file)) < 0)
2409  goto err1;
2410 
2411  if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2412  /* normal tune mode when opened R/W */
2414  fepriv->tone = -1;
2415  fepriv->voltage = -1;
2416 
2417  ret = dvb_frontend_start (fe);
2418  if (ret)
2419  goto err2;
2420 
2421  /* empty event queue */
2422  fepriv->events.eventr = fepriv->events.eventw = 0;
2423  }
2424 
2425  if (adapter->mfe_shared)
2426  mutex_unlock (&adapter->mfe_lock);
2427  return ret;
2428 
2429 err2:
2430  dvb_generic_release(inode, file);
2431 err1:
2432  if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2433  fe->ops.ts_bus_ctrl(fe, 0);
2434 err0:
2435  if (adapter->mfe_shared)
2436  mutex_unlock (&adapter->mfe_lock);
2437  return ret;
2438 }
2439 
2440 static int dvb_frontend_release(struct inode *inode, struct file *file)
2441 {
2442  struct dvb_device *dvbdev = file->private_data;
2443  struct dvb_frontend *fe = dvbdev->priv;
2444  struct dvb_frontend_private *fepriv = fe->frontend_priv;
2445  int ret;
2446 
2447  dev_dbg(fe->dvb->device, "%s:\n", __func__);
2448 
2449  if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2450  fepriv->release_jiffies = jiffies;
2451  mb();
2452  }
2453 
2454  ret = dvb_generic_release (inode, file);
2455 
2456  if (dvbdev->users == -1) {
2457  wake_up(&fepriv->wait_queue);
2458  if (fepriv->exit != DVB_FE_NO_EXIT) {
2459  fops_put(file->f_op);
2460  file->f_op = NULL;
2461  wake_up(&dvbdev->wait_queue);
2462  }
2463  if (fe->ops.ts_bus_ctrl)
2464  fe->ops.ts_bus_ctrl(fe, 0);
2465  }
2466 
2467  return ret;
2468 }
2469 
2470 static const struct file_operations dvb_frontend_fops = {
2471  .owner = THIS_MODULE,
2472  .unlocked_ioctl = dvb_generic_ioctl,
2473  .poll = dvb_frontend_poll,
2474  .open = dvb_frontend_open,
2475  .release = dvb_frontend_release,
2476  .llseek = noop_llseek,
2477 };
2478 
2480 {
2481  int ret = 0;
2482 
2483  dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2484  fe->id);
2485 
2486  if (fe->ops.tuner_ops.sleep)
2487  ret = fe->ops.tuner_ops.sleep(fe);
2488 
2489  if (fe->ops.sleep)
2490  ret = fe->ops.sleep(fe);
2491 
2492  return ret;
2493 }
2495 
2497 {
2498  struct dvb_frontend_private *fepriv = fe->frontend_priv;
2499  int ret = 0;
2500 
2501  dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2502  fe->id);
2503 
2504  if (fe->ops.init)
2505  ret = fe->ops.init(fe);
2506 
2507  if (fe->ops.tuner_ops.init)
2508  ret = fe->ops.tuner_ops.init(fe);
2509 
2510  fepriv->state = FESTATE_RETUNE;
2511  dvb_frontend_wakeup(fe);
2512 
2513  return ret;
2514 }
2516 
2518  struct dvb_frontend* fe)
2519 {
2520  struct dvb_frontend_private *fepriv;
2521  static const struct dvb_device dvbdev_template = {
2522  .users = ~0,
2523  .writers = 1,
2524  .readers = (~0)-1,
2525  .fops = &dvb_frontend_fops,
2526  .kernel_ioctl = dvb_frontend_ioctl
2527  };
2528 
2529  dev_dbg(dvb->device, "%s:\n", __func__);
2530 
2531  if (mutex_lock_interruptible(&frontend_mutex))
2532  return -ERESTARTSYS;
2533 
2534  fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2535  if (fe->frontend_priv == NULL) {
2536  mutex_unlock(&frontend_mutex);
2537  return -ENOMEM;
2538  }
2539  fepriv = fe->frontend_priv;
2540 
2541  sema_init(&fepriv->sem, 1);
2542  init_waitqueue_head (&fepriv->wait_queue);
2543  init_waitqueue_head (&fepriv->events.wait_queue);
2544  mutex_init(&fepriv->events.mtx);
2545  fe->dvb = dvb;
2546  fepriv->inversion = INVERSION_OFF;
2547 
2548  dev_info(fe->dvb->device,
2549  "DVB: registering adapter %i frontend %i (%s)...\n",
2550  fe->dvb->num, fe->id, fe->ops.info.name);
2551 
2552  dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2553  fe, DVB_DEVICE_FRONTEND);
2554 
2555  /*
2556  * Initialize the cache to the proper values according with the
2557  * first supported delivery system (ops->delsys[0])
2558  */
2559 
2560  fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
2561  dvb_frontend_clear_cache(fe);
2562 
2563  mutex_unlock(&frontend_mutex);
2564  return 0;
2565 }
2567 
2569 {
2570  struct dvb_frontend_private *fepriv = fe->frontend_priv;
2571  dev_dbg(fe->dvb->device, "%s:\n", __func__);
2572 
2573  mutex_lock(&frontend_mutex);
2574  dvb_frontend_stop (fe);
2575  mutex_unlock(&frontend_mutex);
2576 
2577  if (fepriv->dvbdev->users < -1)
2578  wait_event(fepriv->dvbdev->wait_queue,
2579  fepriv->dvbdev->users==-1);
2580 
2581  mutex_lock(&frontend_mutex);
2582  dvb_unregister_device (fepriv->dvbdev);
2583 
2584  /* fe is invalid now */
2585  kfree(fepriv);
2586  mutex_unlock(&frontend_mutex);
2587  return 0;
2588 }
2590 
2591 #ifdef CONFIG_MEDIA_ATTACH
2592 void dvb_frontend_detach(struct dvb_frontend* fe)
2593 {
2594  void *ptr;
2595 
2596  if (fe->ops.release_sec) {
2597  fe->ops.release_sec(fe);
2598  symbol_put_addr(fe->ops.release_sec);
2599  }
2600  if (fe->ops.tuner_ops.release) {
2601  fe->ops.tuner_ops.release(fe);
2602  symbol_put_addr(fe->ops.tuner_ops.release);
2603  }
2604  if (fe->ops.analog_ops.release) {
2605  fe->ops.analog_ops.release(fe);
2606  symbol_put_addr(fe->ops.analog_ops.release);
2607  }
2608  ptr = (void*)fe->ops.release;
2609  if (ptr) {
2610  fe->ops.release(fe);
2611  symbol_put_addr(ptr);
2612  }
2613 }
2614 #else
2616 {
2617  if (fe->ops.release_sec)
2618  fe->ops.release_sec(fe);
2619  if (fe->ops.tuner_ops.release)
2620  fe->ops.tuner_ops.release(fe);
2621  if (fe->ops.analog_ops.release)
2622  fe->ops.analog_ops.release(fe);
2623  if (fe->ops.release)
2624  fe->ops.release(fe);
2625 }
2626 #endif