blob: c2a6a0a858130b4db284ca845134656afcffc2f0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Mauro Carvalho Chehab8de85942011-12-26 20:22:50 -030028/* Enables DVBv3 compatibility bits at the headers */
29#define __DVB_CORE__
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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>
Andrew Morton4c4cb1b2009-12-01 13:17:41 -080037#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/list.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080040#include <linux/freezer.h>
Johannes Stezenbachcc89c222005-06-23 22:02:39 -070041#include <linux/jiffies.h>
Herbert Poetzl8eec1422007-02-08 14:32:43 -030042#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include "dvb_frontend.h"
46#include "dvbdev.h"
Steven Totheacf8d82008-09-26 00:29:49 -030047#include <linux/dvb/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static int dvb_frontend_debug;
Oliver Endriss849be2c2007-08-25 12:22:16 -030050static int dvb_shutdown_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int dvb_force_auto_inversion;
52static int dvb_override_tune_delay;
53static int dvb_powerdown_on_sleep = 1;
Darron Broad59b18422008-10-11 11:44:05 -030054static int dvb_mfe_wait_time = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
Johannes Stezenbachf4f009a2005-05-16 21:54:21 -070057MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
Manu Abraham6baad3f2006-02-27 00:09:32 -030058module_param(dvb_shutdown_timeout, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
Manu Abraham6baad3f2006-02-27 00:09:32 -030060module_param(dvb_force_auto_inversion, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
Manu Abraham6baad3f2006-02-27 00:09:32 -030062module_param(dvb_override_tune_delay, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
Manu Abraham6baad3f2006-02-27 00:09:32 -030064module_param(dvb_powerdown_on_sleep, int, 0644);
Uwe Buglaa85585772006-04-11 10:21:37 -030065MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
Darron Broad59b18422008-10-11 11:44:05 -030066module_param(dvb_mfe_wait_time, int, 0644);
67MODULE_PARM_DESC(dvb_mfe_wait_time, "Wait up to <mfe_wait_time> seconds on open() for multi-frontend to become available (default:5 seconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#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
Janne Grunau01886252009-09-01 19:23:09 -030077#define FESTATE_ERROR 256
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#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)
Manu Abraham0249ef12006-06-21 10:27:31 -030082
83#define FE_ALGO_HW 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
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
Ingo Molnar3593cab2006-02-07 06:49:14 -020099static DEFINE_MUTEX(frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101struct dvb_frontend_private {
102
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200103 /* thread/frontend values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct dvb_device *dvbdev;
Andreas Oberrittera5959db2011-05-08 20:03:40 -0300105 struct dvb_frontend_parameters parameters_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct dvb_fe_events events;
107 struct semaphore sem;
108 struct list_head list_head;
109 wait_queue_head_t wait_queue;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300110 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long release_jiffies;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200112 unsigned int wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 fe_status_t status;
Peter Beutner400b7082006-01-09 15:32:43 -0200114 unsigned long tune_mode_flags;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200115 unsigned int delay;
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300116 unsigned int reinitialise;
Andrew de Quincey64454012006-04-06 14:32:23 -0300117 int tone;
118 int voltage;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200119
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;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300133 enum dvbfe_search algo_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300136static void dvb_frontend_wakeup(struct dvb_frontend *fe);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300137static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300138 struct dvb_frontend_parameters *p_out);
Chris Rankin556a0442012-04-06 18:38:18 -0300139static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
140 struct dvb_frontend_parameters *p);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300141
142static bool has_get_frontend(struct dvb_frontend *fe)
143{
Hans Petter Selaskyc065f5b2012-03-27 12:53:19 -0300144 return fe->ops.get_frontend != NULL;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300147/*
148 * Due to DVBv3 API calls, a delivery system should be mapped into one of
149 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
150 * otherwise, a DVBv3 call will fail.
151 */
152enum dvbv3_emulation_type {
153 DVBV3_UNKNOWN,
154 DVBV3_QPSK,
155 DVBV3_QAM,
156 DVBV3_OFDM,
157 DVBV3_ATSC,
158};
159
160static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
161{
162 switch (delivery_system) {
163 case SYS_DVBC_ANNEX_A:
164 case SYS_DVBC_ANNEX_C:
165 return DVBV3_QAM;
166 case SYS_DVBS:
167 case SYS_DVBS2:
168 case SYS_TURBO:
169 case SYS_ISDBS:
170 case SYS_DSS:
171 return DVBV3_QPSK;
172 case SYS_DVBT:
173 case SYS_DVBT2:
174 case SYS_ISDBT:
Antti Palosaari224b6642012-08-12 22:33:21 -0300175 case SYS_DTMB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300176 return DVBV3_OFDM;
177 case SYS_ATSC:
Michael Krufkyca689482012-01-29 15:44:58 -0300178 case SYS_ATSCMH:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300179 case SYS_DVBC_ANNEX_B:
180 return DVBV3_ATSC;
181 case SYS_UNDEFINED:
182 case SYS_ISDBC:
183 case SYS_DVBH:
184 case SYS_DAB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300185 default:
186 /*
187 * Doesn't know how to emulate those types and/or
188 * there's no frontend driver from this type yet
189 * with some emulation code, so, we're not sure yet how
190 * to handle them, or they're not compatible with a DVBv3 call.
191 */
192 return DVBV3_UNKNOWN;
193 }
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
197{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700198 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct dvb_fe_events *events = &fepriv->events;
200 struct dvb_frontend_event *e;
201 int wp;
202
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300203 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300205 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300206 dtv_get_frontend(fe, &fepriv->parameters_out);
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300207
208 mutex_lock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 wp = (events->eventw + 1) % MAX_EVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (wp == events->eventr) {
212 events->overflow = 1;
213 events->eventr = (events->eventr + 1) % MAX_EVENT;
214 }
215
216 e = &events->events[events->eventw];
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300217 e->status = status;
Andreas Oberrittera5959db2011-05-08 20:03:40 -0300218 e->parameters = fepriv->parameters_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 events->eventw = wp;
221
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300222 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 wake_up_interruptible (&events->wait_queue);
225}
226
227static int dvb_frontend_get_event(struct dvb_frontend *fe,
228 struct dvb_frontend_event *event, int flags)
229{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700230 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 struct dvb_fe_events *events = &fepriv->events;
232
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300233 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if (events->overflow) {
236 events->overflow = 0;
237 return -EOVERFLOW;
238 }
239
240 if (events->eventw == events->eventr) {
241 int ret;
242
243 if (flags & O_NONBLOCK)
244 return -EWOULDBLOCK;
245
246 up(&fepriv->sem);
247
248 ret = wait_event_interruptible (events->wait_queue,
249 events->eventw != events->eventr);
250
251 if (down_interruptible (&fepriv->sem))
252 return -ERESTARTSYS;
253
254 if (ret < 0)
255 return ret;
256 }
257
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300258 mutex_lock(&events->mtx);
259 *event = events->events[events->eventr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 events->eventr = (events->eventr + 1) % MAX_EVENT;
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300261 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 return 0;
264}
265
Andreas Oberritter20640be2011-08-04 12:33:14 -0300266static void dvb_frontend_clear_events(struct dvb_frontend *fe)
267{
268 struct dvb_frontend_private *fepriv = fe->frontend_priv;
269 struct dvb_fe_events *events = &fepriv->events;
270
271 mutex_lock(&events->mtx);
272 events->eventr = events->eventw;
273 mutex_unlock(&events->mtx);
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static void dvb_frontend_init(struct dvb_frontend *fe)
277{
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300278 dev_dbg(fe->dvb->device,
279 "%s: initialising adapter %i frontend %i (%s)...\n",
280 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Patrick Boettcherdea74862006-05-14 05:01:31 -0300282 if (fe->ops.init)
283 fe->ops.init(fe);
284 if (fe->ops.tuner_ops.init) {
Manu Abraham3b37a152008-10-20 18:14:14 -0300285 if (fe->ops.i2c_gate_ctrl)
286 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300287 fe->ops.tuner_ops.init(fe);
288 if (fe->ops.i2c_gate_ctrl)
289 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300293void dvb_frontend_reinitialise(struct dvb_frontend *fe)
294{
295 struct dvb_frontend_private *fepriv = fe->frontend_priv;
296
297 fepriv->reinitialise = 1;
298 dvb_frontend_wakeup(fe);
299}
300EXPORT_SYMBOL(dvb_frontend_reinitialise);
301
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200302static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200304 int q2;
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300305 struct dvb_frontend *fe = fepriv->dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300307 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200309 if (locked)
310 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
311 else
312 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200314 q2 = fepriv->quality - 128;
315 q2 *= q2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200317 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320/**
321 * Performs automatic twiddling of frontend parameters.
322 *
323 * @param fe The frontend concerned.
324 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
325 * @returns Number of complete iterations that have been performed.
326 */
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200327static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 int autoinversion;
330 int ready = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300331 int fe_set_err = 0;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700332 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300333 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
334 int original_inversion = c->inversion;
335 u32 original_frequency = c->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* are we using autoinversion? */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300338 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300339 (c->inversion == INVERSION_AUTO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* setup parameters correctly */
342 while(!ready) {
343 /* calculate the lnb_drift */
344 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
345
346 /* wrap the auto_step if we've exceeded the maximum drift */
347 if (fepriv->lnb_drift > fepriv->max_drift) {
348 fepriv->auto_step = 0;
349 fepriv->auto_sub_step = 0;
350 fepriv->lnb_drift = 0;
351 }
352
353 /* perform inversion and +/- zigzag */
354 switch(fepriv->auto_sub_step) {
355 case 0:
356 /* try with the current inversion and current drift setting */
357 ready = 1;
358 break;
359
360 case 1:
361 if (!autoinversion) break;
362
363 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
364 ready = 1;
365 break;
366
367 case 2:
368 if (fepriv->lnb_drift == 0) break;
369
370 fepriv->lnb_drift = -fepriv->lnb_drift;
371 ready = 1;
372 break;
373
374 case 3:
375 if (fepriv->lnb_drift == 0) break;
376 if (!autoinversion) break;
377
378 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
379 fepriv->lnb_drift = -fepriv->lnb_drift;
380 ready = 1;
381 break;
382
383 default:
384 fepriv->auto_step++;
385 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
386 break;
387 }
388
389 if (!ready) fepriv->auto_sub_step++;
390 }
391
392 /* if this attempt would hit where we started, indicate a complete
393 * iteration has occurred */
394 if ((fepriv->auto_step == fepriv->started_auto_step) &&
395 (fepriv->auto_sub_step == 0) && check_wrapped) {
396 return 1;
397 }
398
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300399 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
400 "auto_sub_step:%i started_auto_step:%i\n",
401 __func__, fepriv->lnb_drift, fepriv->inversion,
402 fepriv->auto_step, fepriv->auto_sub_step,
403 fepriv->started_auto_step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /* set the frontend itself */
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300406 c->frequency += fepriv->lnb_drift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (autoinversion)
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300408 c->inversion = fepriv->inversion;
409 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300410 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300411 fe_set_err = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300412 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300413 if (fe_set_err < 0) {
414 fepriv->state = FESTATE_ERROR;
415 return fe_set_err;
416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300418 c->frequency = original_frequency;
419 c->inversion = original_inversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 fepriv->auto_sub_step++;
422 return 0;
423}
424
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200425static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
426{
Andrew de Quincey30d94642006-11-16 22:12:40 -0300427 fe_status_t s = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300428 int retval = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200429 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300430 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200431
432 /* if we've got no parameters, just keep idling */
433 if (fepriv->state & FESTATE_IDLE) {
434 fepriv->delay = 3*HZ;
435 fepriv->quality = 0;
436 return;
437 }
438
439 /* in SCAN mode, we just set the frontend when asked and leave it alone */
440 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
441 if (fepriv->state & FESTATE_RETUNE) {
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300442 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300443 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300444 retval = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300445 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300446 if (retval < 0)
447 fepriv->state = FESTATE_ERROR;
448 else
449 fepriv->state = FESTATE_TUNED;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200450 }
451 fepriv->delay = 3*HZ;
452 fepriv->quality = 0;
453 return;
454 }
455
456 /* get the frontend status */
457 if (fepriv->state & FESTATE_RETUNE) {
458 s = 0;
459 } else {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300460 if (fe->ops.read_status)
461 fe->ops.read_status(fe, &s);
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200462 if (s != fepriv->status) {
463 dvb_frontend_add_event(fe, s);
464 fepriv->status = s;
465 }
466 }
467
468 /* if we're not tuned, and we have a lock, move to the TUNED state */
469 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
470 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
471 fepriv->state = FESTATE_TUNED;
472
473 /* if we're tuned, then we have determined the correct inversion */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300474 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300475 (c->inversion == INVERSION_AUTO)) {
476 c->inversion = fepriv->inversion;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200477 }
478 return;
479 }
480
481 /* if we are tuned already, check we're still locked */
482 if (fepriv->state & FESTATE_TUNED) {
483 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
484
485 /* we're tuned, and the lock is still good... */
486 if (s & FE_HAS_LOCK) {
487 return;
488 } else { /* if we _WERE_ tuned, but now don't have a lock */
489 fepriv->state = FESTATE_ZIGZAG_FAST;
490 fepriv->started_auto_step = fepriv->auto_step;
491 fepriv->check_wrapped = 0;
492 }
493 }
494
495 /* don't actually do anything if we're in the LOSTLOCK state,
496 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
497 if ((fepriv->state & FESTATE_LOSTLOCK) &&
Patrick Boettcherdea74862006-05-14 05:01:31 -0300498 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200499 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
500 return;
501 }
502
503 /* don't do anything if we're in the DISEQC state, since this
504 * might be someone with a motorized dish controlled by DISEQC.
505 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
506 if (fepriv->state & FESTATE_DISEQC) {
507 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
508 return;
509 }
510
511 /* if we're in the RETUNE state, set everything up for a brand
512 * new scan, keeping the current inversion setting, as the next
513 * tune is _very_ likely to require the same */
514 if (fepriv->state & FESTATE_RETUNE) {
515 fepriv->lnb_drift = 0;
516 fepriv->auto_step = 0;
517 fepriv->auto_sub_step = 0;
518 fepriv->started_auto_step = 0;
519 fepriv->check_wrapped = 0;
520 }
521
522 /* fast zigzag. */
523 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
524 fepriv->delay = fepriv->min_delay;
525
Guillaume Audirac2030c032010-05-06 09:30:25 -0300526 /* perform a tune */
Janne Grunau01886252009-09-01 19:23:09 -0300527 retval = dvb_frontend_swzigzag_autotune(fe,
528 fepriv->check_wrapped);
529 if (retval < 0) {
530 return;
531 } else if (retval) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200532 /* OK, if we've run out of trials at the fast speed.
533 * Drop back to slow for the _next_ attempt */
534 fepriv->state = FESTATE_SEARCHING_SLOW;
535 fepriv->started_auto_step = fepriv->auto_step;
536 return;
537 }
538 fepriv->check_wrapped = 1;
539
540 /* if we've just retuned, enter the ZIGZAG_FAST state.
541 * This ensures we cannot return from an
542 * FE_SET_FRONTEND ioctl before the first frontend tune
543 * occurs */
544 if (fepriv->state & FESTATE_RETUNE) {
545 fepriv->state = FESTATE_TUNING_FAST;
546 }
547 }
548
549 /* slow zigzag */
550 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
551 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
552
553 /* Note: don't bother checking for wrapping; we stay in this
554 * state until we get a lock */
555 dvb_frontend_swzigzag_autotune(fe, 0);
556 }
557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
560{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700561 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Shuah Khan18ed2862014-07-12 13:44:12 -0300563 if (fe->exit != DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return 1;
565
566 if (fepriv->dvbdev->writers == 1)
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -0300567 if (time_after_eq(jiffies, fepriv->release_jiffies +
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200568 dvb_shutdown_timeout * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 1;
570
571 return 0;
572}
573
574static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
575{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700576 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (fepriv->wakeup) {
579 fepriv->wakeup = 0;
580 return 1;
581 }
582 return dvb_frontend_is_exiting(fe);
583}
584
585static void dvb_frontend_wakeup(struct dvb_frontend *fe)
586{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700587 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 fepriv->wakeup = 1;
590 wake_up_interruptible(&fepriv->wait_queue);
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static int dvb_frontend_thread(void *data)
594{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700595 struct dvb_frontend *fe = data;
596 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 fe_status_t s;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300598 enum dvbfe_algo algo;
599
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300600 bool re_tune = false;
Juergen Lock6ae23222012-12-23 17:23:06 -0300601 bool semheld = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300603 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200605 fepriv->check_wrapped = 0;
606 fepriv->quality = 0;
607 fepriv->delay = 3*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 fepriv->wakeup = 0;
Andrew de Quincey04c56d02006-07-10 03:34:14 -0300610 fepriv->reinitialise = 0;
611
612 dvb_frontend_init(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700614 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 while (1) {
616 up(&fepriv->sem); /* is locked when we enter the thread... */
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300617restart:
Hans Verkuil94238e92011-08-27 11:30:25 -0300618 wait_event_interruptible_timeout(fepriv->wait_queue,
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700619 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
620 || freezing(current),
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300621 fepriv->delay);
622
623 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* got signal or quitting */
Juergen Lock6ae23222012-12-23 17:23:06 -0300625 if (!down_interruptible(&fepriv->sem))
626 semheld = true;
Shuah Khan18ed2862014-07-12 13:44:12 -0300627 fe->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 break;
629 }
630
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300631 if (try_to_freeze())
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300632 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (down_interruptible(&fepriv->sem))
635 break;
636
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300637 if (fepriv->reinitialise) {
638 dvb_frontend_init(fe);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300639 if (fe->ops.set_tone && fepriv->tone != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300640 fe->ops.set_tone(fe, fepriv->tone);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300641 if (fe->ops.set_voltage && fepriv->voltage != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300642 fe->ops.set_voltage(fe, fepriv->voltage);
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300643 fepriv->reinitialise = 0;
644 }
645
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200646 /* do an iteration of the tuning loop */
Manu Abrahamd772bd02006-06-24 11:18:58 -0300647 if (fe->ops.get_frontend_algo) {
Manu Abrahamc59e7872008-10-14 16:34:07 -0300648 algo = fe->ops.get_frontend_algo(fe);
649 switch (algo) {
650 case DVBFE_ALGO_HW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300651 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300652
Manu Abrahamd772bd02006-06-24 11:18:58 -0300653 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300654 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300655 re_tune = true;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300656 fepriv->state = FESTATE_TUNED;
Simon Arlott45145b62012-02-06 17:57:01 -0300657 } else {
658 re_tune = false;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Manu Abrahamc59e7872008-10-14 16:34:07 -0300661 if (fe->ops.tune)
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300662 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300663
Darron Broad2fac9a02008-12-18 06:27:50 -0300664 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300665 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
Manu Abrahamd772bd02006-06-24 11:18:58 -0300666 dvb_frontend_add_event(fe, s);
667 fepriv->status = s;
668 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300669 break;
670 case DVBFE_ALGO_SW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300671 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
Manu Abraham70d90632006-06-29 22:05:23 -0300672 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300673 break;
674 case DVBFE_ALGO_CUSTOM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300675 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300676 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300677 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300678 fepriv->state = FESTATE_TUNED;
679 }
680 /* Case where we are going to search for a carrier
681 * User asked us to retune again for some reason, possibly
682 * requesting a search with a new set of parameters
683 */
684 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300685 if (fe->ops.search) {
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -0300686 fepriv->algo_status = fe->ops.search(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300687 /* We did do a search as was requested, the flags are
688 * now unset as well and has the flags wrt to search.
689 */
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300690 } else {
691 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
692 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300693 }
694 /* Track the carrier if the search was successful */
Mauro Carvalho Chehab1b5d8712011-12-26 18:11:25 -0300695 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300696 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
697 fepriv->delay = HZ / 2;
698 }
Chris Rankin556a0442012-04-06 18:38:18 -0300699 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300700 fe->ops.read_status(fe, &s);
701 if (s != fepriv->status) {
702 dvb_frontend_add_event(fe, s); /* update event list */
703 fepriv->status = s;
704 if (!(s & FE_HAS_LOCK)) {
705 fepriv->delay = HZ / 10;
706 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
707 } else {
708 fepriv->delay = 60 * HZ;
709 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300710 }
711 break;
712 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300713 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300714 break;
715 }
716 } else {
Manu Abraham4a4edcc2006-06-25 05:46:26 -0300717 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Oliver Endriss608f62d2007-08-25 13:17:53 -0300721 if (dvb_powerdown_on_sleep) {
722 if (fe->ops.set_voltage)
723 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300724 if (fe->ops.tuner_ops.sleep) {
Devin Heitmueller41286d92008-11-16 00:44:52 -0300725 if (fe->ops.i2c_gate_ctrl)
726 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300727 fe->ops.tuner_ops.sleep(fe);
728 if (fe->ops.i2c_gate_ctrl)
729 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300730 }
Patrick Boettcherdea74862006-05-14 05:01:31 -0300731 if (fe->ops.sleep)
732 fe->ops.sleep(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300735 fepriv->thread = NULL;
matthieu castete36309f2010-05-05 15:59:20 -0300736 if (kthread_should_stop())
Shuah Khan18ed2862014-07-12 13:44:12 -0300737 fe->exit = DVB_FE_DEVICE_REMOVED;
matthieu castete36309f2010-05-05 15:59:20 -0300738 else
Shuah Khan18ed2862014-07-12 13:44:12 -0300739 fe->exit = DVB_FE_NO_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 mb();
741
Juergen Lock6ae23222012-12-23 17:23:06 -0300742 if (semheld)
743 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 dvb_frontend_wakeup(fe);
745 return 0;
746}
747
748static void dvb_frontend_stop(struct dvb_frontend *fe)
749{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700750 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300752 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Shuah Khan18ed2862014-07-12 13:44:12 -0300754 if (fe->exit != DVB_FE_DEVICE_REMOVED)
755 fe->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 mb();
757
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300758 if (!fepriv->thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return;
760
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300761 kthread_stop(fepriv->thread);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300762
Thomas Gleixnera0a47142010-09-07 11:33:27 -0300763 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 fepriv->state = FESTATE_IDLE;
765
766 /* paranoia check in case a signal arrived */
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300767 if (fepriv->thread)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300768 dev_warn(fe->dvb->device,
769 "dvb_frontend_stop: warning: thread %p won't exit\n",
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300770 fepriv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
NooneImportant83b75b02005-11-08 21:35:27 -0800773s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
774{
775 return ((curtime.tv_usec < lasttime.tv_usec) ?
776 1000000 - lasttime.tv_usec + curtime.tv_usec :
777 curtime.tv_usec - lasttime.tv_usec);
778}
779EXPORT_SYMBOL(timeval_usec_diff);
780
781static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
782{
783 curtime->tv_usec += add_usec;
784 if (curtime->tv_usec >= 1000000) {
785 curtime->tv_usec -= 1000000;
786 curtime->tv_sec++;
787 }
788}
789
790/*
791 * Sleep until gettimeofday() > waketime + add_usec
792 * This needs to be as precise as possible, but as the delay is
793 * usually between 2ms and 32ms, it is done using a scheduled msleep
794 * followed by usleep (normally a busy-wait loop) for the remainder
795 */
796void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
797{
798 struct timeval lasttime;
799 s32 delta, newdelta;
800
801 timeval_usec_add(waketime, add_usec);
802
803 do_gettimeofday(&lasttime);
804 delta = timeval_usec_diff(lasttime, *waketime);
805 if (delta > 2500) {
806 msleep((delta - 1500) / 1000);
807 do_gettimeofday(&lasttime);
808 newdelta = timeval_usec_diff(lasttime, *waketime);
809 delta = (newdelta > delta) ? 0 : newdelta;
810 }
811 if (delta > 0)
812 udelay(delta);
813}
814EXPORT_SYMBOL(dvb_frontend_sleep_until);
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static int dvb_frontend_start(struct dvb_frontend *fe)
817{
818 int ret;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700819 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300820 struct task_struct *fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300822 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300824 if (fepriv->thread) {
Shuah Khan18ed2862014-07-12 13:44:12 -0300825 if (fe->exit == DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return 0;
827 else
828 dvb_frontend_stop (fe);
829 }
830
831 if (signal_pending(current))
832 return -EINTR;
833 if (down_interruptible (&fepriv->sem))
834 return -EINTR;
835
836 fepriv->state = FESTATE_IDLE;
Shuah Khan18ed2862014-07-12 13:44:12 -0300837 fe->exit = DVB_FE_NO_EXIT;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300838 fepriv->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 mb();
840
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300841 fe_thread = kthread_run(dvb_frontend_thread, fe,
Steven Toth363c35f2008-10-11 11:05:50 -0300842 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300843 if (IS_ERR(fe_thread)) {
844 ret = PTR_ERR(fe_thread);
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300845 dev_warn(fe->dvb->device,
846 "dvb_frontend_start: failed to start kthread (%d)\n",
847 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 up(&fepriv->sem);
849 return ret;
850 }
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300851 fepriv->thread = fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return 0;
853}
854
Guillaume Audirac2030c032010-05-06 09:30:25 -0300855static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
Oliver Endrissc471b332007-08-09 01:03:42 -0300856 u32 *freq_min, u32 *freq_max)
857{
858 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
859
860 if (fe->ops.info.frequency_max == 0)
861 *freq_max = fe->ops.tuner_ops.info.frequency_max;
862 else if (fe->ops.tuner_ops.info.frequency_max == 0)
863 *freq_max = fe->ops.info.frequency_max;
864 else
865 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
866
867 if (*freq_min == 0 || *freq_max == 0)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300868 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
869 fe->dvb->num, fe->id);
Oliver Endrissc471b332007-08-09 01:03:42 -0300870}
871
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300872static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300873{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300874 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Oliver Endrissc471b332007-08-09 01:03:42 -0300875 u32 freq_min;
876 u32 freq_max;
877
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300878 /* range check: frequency */
Guillaume Audirac2030c032010-05-06 09:30:25 -0300879 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300880 if ((freq_min && c->frequency < freq_min) ||
881 (freq_max && c->frequency > freq_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300882 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
883 fe->dvb->num, fe->id, c->frequency,
884 freq_min, freq_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300885 return -EINVAL;
886 }
887
888 /* range check: symbol rate */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300889 switch (c->delivery_system) {
890 case SYS_DVBS:
891 case SYS_DVBS2:
892 case SYS_TURBO:
893 case SYS_DVBC_ANNEX_A:
894 case SYS_DVBC_ANNEX_C:
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300895 if ((fe->ops.info.symbol_rate_min &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300896 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300897 (fe->ops.info.symbol_rate_max &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300898 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300899 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
900 fe->dvb->num, fe->id, c->symbol_rate,
901 fe->ops.info.symbol_rate_min,
902 fe->ops.info.symbol_rate_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300903 return -EINVAL;
904 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300905 default:
906 break;
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300907 }
908
909 return 0;
910}
911
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300912static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
913{
Andreas Oberritter50727712011-05-08 20:03:39 -0300914 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300915 int i;
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200916 u32 delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300917
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200918 delsys = c->delivery_system;
Mauro Carvalho Chehab808d24d2013-03-20 10:39:31 -0300919 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200920 c->delivery_system = delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300921
Andreas Oberritter50727712011-05-08 20:03:39 -0300922 c->state = DTV_CLEAR;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300923
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300924 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
925 __func__, c->delivery_system);
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300926
Andreas Oberritter50727712011-05-08 20:03:39 -0300927 c->transmission_mode = TRANSMISSION_MODE_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300928 c->bandwidth_hz = 0; /* AUTO */
Andreas Oberritter50727712011-05-08 20:03:39 -0300929 c->guard_interval = GUARD_INTERVAL_AUTO;
930 c->hierarchy = HIERARCHY_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300931 c->symbol_rate = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -0300932 c->code_rate_HP = FEC_AUTO;
933 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300934 c->fec_inner = FEC_AUTO;
Mauro Carvalho Chehab39ce61a2011-11-11 12:46:23 -0200935 c->rolloff = ROLLOFF_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300936 c->voltage = SEC_VOLTAGE_OFF;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300937 c->sectone = SEC_TONE_OFF;
938 c->pilot = PILOT_AUTO;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300939
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300940 c->isdbt_partial_reception = 0;
941 c->isdbt_sb_mode = 0;
942 c->isdbt_sb_subchannel = 0;
943 c->isdbt_sb_segment_idx = 0;
944 c->isdbt_sb_segment_count = 0;
945 c->isdbt_layer_enabled = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300946 for (i = 0; i < 3; i++) {
Andreas Oberritter50727712011-05-08 20:03:39 -0300947 c->layer[i].fec = FEC_AUTO;
948 c->layer[i].modulation = QAM_AUTO;
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300949 c->layer[i].interleaving = 0;
950 c->layer[i].segment_count = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300951 }
952
Evgeny Plehov287cefd2012-09-13 10:13:30 -0300953 c->stream_id = NO_STREAM_ID_FILTER;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300954
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300955 switch (c->delivery_system) {
956 case SYS_DVBS:
957 case SYS_DVBS2:
958 case SYS_TURBO:
959 c->modulation = QPSK; /* implied for DVB-S in legacy API */
960 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
961 break;
962 case SYS_ATSC:
963 c->modulation = VSB_8;
964 break;
965 default:
966 c->modulation = QAM_AUTO;
967 break;
968 }
969
Antti Palosaari33eebec2012-10-03 04:28:56 -0300970 c->lna = LNA_AUTO;
971
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300972 return 0;
973}
974
975#define _DTV_CMD(n, s, b) \
976[n] = { \
977 .name = #n, \
978 .cmd = n, \
979 .set = s,\
980 .buffer = b \
981}
982
Andreas Oberritterec05a642011-05-26 07:54:14 -0300983static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300984 _DTV_CMD(DTV_TUNE, 1, 0),
985 _DTV_CMD(DTV_CLEAR, 1, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -0300986
987 /* Set */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300988 _DTV_CMD(DTV_FREQUENCY, 1, 0),
989 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
990 _DTV_CMD(DTV_MODULATION, 1, 0),
991 _DTV_CMD(DTV_INVERSION, 1, 0),
992 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
993 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
994 _DTV_CMD(DTV_INNER_FEC, 1, 0),
995 _DTV_CMD(DTV_VOLTAGE, 1, 0),
996 _DTV_CMD(DTV_TONE, 1, 0),
997 _DTV_CMD(DTV_PILOT, 1, 0),
998 _DTV_CMD(DTV_ROLLOFF, 1, 0),
999 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1000 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1001 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1002 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1003 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1004 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
Antti Palosaari224b6642012-08-12 22:33:21 -03001005 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001006
1007 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1008 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1009 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1010 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1011 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
Patrick Boettchere7b79492009-08-14 05:24:19 -03001012 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001013 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1014 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1015 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1016 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1017 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1018 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1019 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1020 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1021 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1022 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1023 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1024 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1025
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001026 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1027 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
Antti Palosaari8a2697a2012-07-11 21:54:50 -03001028 _DTV_CMD(DTV_LNA, 1, 0),
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001029
Steven Toth6b73eea2008-09-04 01:12:25 -03001030 /* Get */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001031 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1032 _DTV_CMD(DTV_API_VERSION, 0, 0),
Manu Abrahamba2780c2011-11-13 18:47:44 -03001033
1034 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001035
1036 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1037 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1038
1039 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001040 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1041 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1042 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1043 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1044 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001045 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1046 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1047 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1048 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1049 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1050 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1051 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001052
1053 /* Statistics API */
1054 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1055 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1056 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1057 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1058 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1059 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1060 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1061 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -03001062};
1063
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001064static void dtv_property_dump(struct dvb_frontend *fe, struct dtv_property *tvp)
Steven Toth6b73eea2008-09-04 01:12:25 -03001065{
1066 int i;
1067
Mauro Carvalho Chehab106ff9e2011-07-15 08:49:06 -03001068 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001069 dev_warn(fe->dvb->device, "%s: tvp.cmd = 0x%08x undefined\n",
1070 __func__, tvp->cmd);
Darron Broad4aae8ef2008-10-03 11:50:00 -03001071 return;
1072 }
1073
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001074 dev_dbg(fe->dvb->device, "%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1075 tvp->cmd, dtv_cmds[tvp->cmd].name);
Steven Toth6b73eea2008-09-04 01:12:25 -03001076
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001077 if (dtv_cmds[tvp->cmd].buffer) {
1078 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1079 __func__, tvp->u.buffer.len);
Steven Toth6b73eea2008-09-04 01:12:25 -03001080
1081 for(i = 0; i < tvp->u.buffer.len; i++)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001082 dev_dbg(fe->dvb->device,
1083 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1084 __func__, i, tvp->u.buffer.data[i]);
1085 } else {
1086 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1087 tvp->u.data);
1088 }
Steven Toth6b73eea2008-09-04 01:12:25 -03001089}
1090
Steven Tothee33c522008-09-11 23:52:32 -03001091/* Synchronise the legacy tuning parameters into the cache, so that demodulator
1092 * drivers can use a single set_frontend tuning function, regardless of whether
1093 * it's being used for the legacy or new API, reducing code and complexity.
1094 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001095static int dtv_property_cache_sync(struct dvb_frontend *fe,
1096 struct dtv_frontend_properties *c,
1097 const struct dvb_frontend_parameters *p)
Steven Tothee33c522008-09-11 23:52:32 -03001098{
Steven Tothee33c522008-09-11 23:52:32 -03001099 c->frequency = p->frequency;
1100 c->inversion = p->inversion;
1101
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001102 switch (dvbv3_type(c->delivery_system)) {
1103 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001104 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001105 c->symbol_rate = p->u.qpsk.symbol_rate;
1106 c->fec_inner = p->u.qpsk.fec_inner;
1107 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001108 case DVBV3_QAM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001109 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001110 c->symbol_rate = p->u.qam.symbol_rate;
1111 c->fec_inner = p->u.qam.fec_inner;
1112 c->modulation = p->u.qam.modulation;
1113 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001114 case DVBV3_OFDM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001115 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1116
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001117 switch (p->u.ofdm.bandwidth) {
1118 case BANDWIDTH_10_MHZ:
1119 c->bandwidth_hz = 10000000;
1120 break;
1121 case BANDWIDTH_8_MHZ:
Steven Toth75b7f942008-09-13 16:56:34 -03001122 c->bandwidth_hz = 8000000;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001123 break;
1124 case BANDWIDTH_7_MHZ:
1125 c->bandwidth_hz = 7000000;
1126 break;
1127 case BANDWIDTH_6_MHZ:
1128 c->bandwidth_hz = 6000000;
1129 break;
1130 case BANDWIDTH_5_MHZ:
1131 c->bandwidth_hz = 5000000;
1132 break;
1133 case BANDWIDTH_1_712_MHZ:
1134 c->bandwidth_hz = 1712000;
1135 break;
1136 case BANDWIDTH_AUTO:
Steven Toth75b7f942008-09-13 16:56:34 -03001137 c->bandwidth_hz = 0;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001138 }
1139
Steven Tothee33c522008-09-11 23:52:32 -03001140 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1141 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1142 c->modulation = p->u.ofdm.constellation;
1143 c->transmission_mode = p->u.ofdm.transmission_mode;
1144 c->guard_interval = p->u.ofdm.guard_interval;
1145 c->hierarchy = p->u.ofdm.hierarchy_information;
1146 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001147 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001148 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001149 c->modulation = p->u.vsb.modulation;
Michael Krufkyca689482012-01-29 15:44:58 -03001150 if (c->delivery_system == SYS_ATSCMH)
1151 break;
Steven Toth80a773c2008-09-12 00:53:50 -03001152 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1153 c->delivery_system = SYS_ATSC;
1154 else
1155 c->delivery_system = SYS_DVBC_ANNEX_B;
Steven Tothee33c522008-09-11 23:52:32 -03001156 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001157 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001158 dev_err(fe->dvb->device,
1159 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1160 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001161 return -EINVAL;
Steven Tothee33c522008-09-11 23:52:32 -03001162 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001163
1164 return 0;
Steven Tothee33c522008-09-11 23:52:32 -03001165}
1166
Steven Tothd5748f12008-09-12 00:27:13 -03001167/* Ensure the cached values are set correctly in the frontend
1168 * legacy tuning structures, for the advanced tuning API.
1169 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001170static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001171 struct dvb_frontend_parameters *p)
Steven Toth6b73eea2008-09-04 01:12:25 -03001172{
Andreas Oberritter15cc2bb2011-05-08 20:03:35 -03001173 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001174
Steven Tothd5748f12008-09-12 00:27:13 -03001175 p->frequency = c->frequency;
1176 p->inversion = c->inversion;
1177
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001178 switch (dvbv3_type(c->delivery_system)) {
1179 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001180 dev_err(fe->dvb->device,
1181 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1182 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001183 return -EINVAL;
1184 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001185 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001186 p->u.qpsk.symbol_rate = c->symbol_rate;
1187 p->u.qpsk.fec_inner = c->fec_inner;
1188 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001189 case DVBV3_QAM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001190 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001191 p->u.qam.symbol_rate = c->symbol_rate;
1192 p->u.qam.fec_inner = c->fec_inner;
1193 p->u.qam.modulation = c->modulation;
1194 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001195 case DVBV3_OFDM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001196 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001197 switch (c->bandwidth_hz) {
1198 case 10000000:
1199 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1200 break;
1201 case 8000000:
Steven Toth75b7f942008-09-13 16:56:34 -03001202 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001203 break;
1204 case 7000000:
1205 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1206 break;
1207 case 6000000:
1208 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1209 break;
1210 case 5000000:
1211 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1212 break;
1213 case 1712000:
1214 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1215 break;
1216 case 0:
1217 default:
Steven Toth75b7f942008-09-13 16:56:34 -03001218 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001219 }
Steven Tothd5748f12008-09-12 00:27:13 -03001220 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1221 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1222 p->u.ofdm.constellation = c->modulation;
1223 p->u.ofdm.transmission_mode = c->transmission_mode;
1224 p->u.ofdm.guard_interval = c->guard_interval;
1225 p->u.ofdm.hierarchy_information = c->hierarchy;
1226 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001227 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001228 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001229 p->u.vsb.modulation = c->modulation;
1230 break;
1231 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001232 return 0;
Steven Toth6b73eea2008-09-04 01:12:25 -03001233}
1234
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001235/**
1236 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1237 * @fe: struct dvb_frontend pointer
1238 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1239 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1240 *
1241 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1242 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1243 * If p_out is not null, it will update the DVBv3 params pointed by it.
1244 */
1245static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001246 struct dvb_frontend_parameters *p_out)
1247{
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001248 int r;
1249
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001250 if (fe->ops.get_frontend) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001251 r = fe->ops.get_frontend(fe);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001252 if (unlikely(r < 0))
1253 return r;
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001254 if (p_out)
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001255 dtv_property_legacy_params_sync(fe, p_out);
1256 return 0;
1257 }
1258
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001259 /* As everything is in cache, get_frontend fops are always supported */
Mauro Carvalho Chehabb1e9a65012011-12-26 16:43:32 -03001260 return 0;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001261}
1262
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001263static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001264 unsigned int cmd, void *parg);
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001265static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001266 unsigned int cmd, void *parg);
1267
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001268static int dtv_property_process_get(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001269 const struct dtv_frontend_properties *c,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001270 struct dtv_property *tvp,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001271 struct file *file)
Steven Toth363429a2008-09-12 01:34:28 -03001272{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001273 int r, ncaps;
Steven Tothbfbf2da2008-09-12 01:37:37 -03001274
Steven Toth363429a2008-09-12 01:34:28 -03001275 switch(tvp->cmd) {
Manu Abrahamba2780c2011-11-13 18:47:44 -03001276 case DTV_ENUM_DELSYS:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001277 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001278 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001279 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1280 ncaps++;
1281 }
1282 tvp->u.buffer.len = ncaps;
Manu Abrahamba2780c2011-11-13 18:47:44 -03001283 break;
Steven Toth363429a2008-09-12 01:34:28 -03001284 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001285 tvp->u.data = c->frequency;
Steven Toth363429a2008-09-12 01:34:28 -03001286 break;
1287 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001288 tvp->u.data = c->modulation;
Steven Toth363429a2008-09-12 01:34:28 -03001289 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001290 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001291 tvp->u.data = c->bandwidth_hz;
Steven Toth363429a2008-09-12 01:34:28 -03001292 break;
1293 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001294 tvp->u.data = c->inversion;
Steven Toth363429a2008-09-12 01:34:28 -03001295 break;
1296 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001297 tvp->u.data = c->symbol_rate;
Steven Toth363429a2008-09-12 01:34:28 -03001298 break;
1299 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001300 tvp->u.data = c->fec_inner;
Steven Toth363429a2008-09-12 01:34:28 -03001301 break;
1302 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001303 tvp->u.data = c->pilot;
Steven Toth363429a2008-09-12 01:34:28 -03001304 break;
1305 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001306 tvp->u.data = c->rolloff;
Steven Toth363429a2008-09-12 01:34:28 -03001307 break;
1308 case DTV_DELIVERY_SYSTEM:
Andreas Oberritter50727712011-05-08 20:03:39 -03001309 tvp->u.data = c->delivery_system;
Steven Toth363429a2008-09-12 01:34:28 -03001310 break;
Steven Toth363429a2008-09-12 01:34:28 -03001311 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001312 tvp->u.data = c->voltage;
Steven Toth363429a2008-09-12 01:34:28 -03001313 break;
1314 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001315 tvp->u.data = c->sectone;
Steven Toth363429a2008-09-12 01:34:28 -03001316 break;
Steven Totheacf8d82008-09-26 00:29:49 -03001317 case DTV_API_VERSION:
1318 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1319 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001320 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001321 tvp->u.data = c->code_rate_HP;
Steven Totha4de91b2008-10-06 21:55:46 -03001322 break;
1323 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001324 tvp->u.data = c->code_rate_LP;
Steven Totha4de91b2008-10-06 21:55:46 -03001325 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001326 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001327 tvp->u.data = c->guard_interval;
Steven Tothb87625f2008-10-06 21:56:59 -03001328 break;
1329 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001330 tvp->u.data = c->transmission_mode;
Steven Tothb87625f2008-10-06 21:56:59 -03001331 break;
Steven Tothef526f42008-10-06 22:01:47 -03001332 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001333 tvp->u.data = c->hierarchy;
Steven Tothef526f42008-10-06 22:01:47 -03001334 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001335 case DTV_INTERLEAVING:
1336 tvp->u.data = c->interleaving;
1337 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001338
1339 /* ISDB-T Support here */
1340 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001341 tvp->u.data = c->isdbt_partial_reception;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001342 break;
1343 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001344 tvp->u.data = c->isdbt_sb_mode;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001345 break;
1346 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001347 tvp->u.data = c->isdbt_sb_subchannel;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001348 break;
1349 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001350 tvp->u.data = c->isdbt_sb_segment_idx;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001351 break;
1352 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001353 tvp->u.data = c->isdbt_sb_segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001354 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001355 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001356 tvp->u.data = c->isdbt_layer_enabled;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001357 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001358 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001359 tvp->u.data = c->layer[0].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001360 break;
1361 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001362 tvp->u.data = c->layer[0].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001363 break;
1364 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001365 tvp->u.data = c->layer[0].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001366 break;
1367 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001368 tvp->u.data = c->layer[0].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001369 break;
1370 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001371 tvp->u.data = c->layer[1].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001372 break;
1373 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001374 tvp->u.data = c->layer[1].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001375 break;
1376 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001377 tvp->u.data = c->layer[1].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001378 break;
1379 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001380 tvp->u.data = c->layer[1].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001381 break;
1382 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001383 tvp->u.data = c->layer[2].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001384 break;
1385 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001386 tvp->u.data = c->layer[2].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001387 break;
1388 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001389 tvp->u.data = c->layer[2].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001390 break;
1391 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001392 tvp->u.data = c->layer[2].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001393 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001394
1395 /* Multistream support */
1396 case DTV_STREAM_ID:
1397 case DTV_DVBT2_PLP_ID_LEGACY:
1398 tvp->u.data = c->stream_id;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001399 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001400
1401 /* ATSC-MH */
1402 case DTV_ATSCMH_FIC_VER:
1403 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1404 break;
1405 case DTV_ATSCMH_PARADE_ID:
1406 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1407 break;
1408 case DTV_ATSCMH_NOG:
1409 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1410 break;
1411 case DTV_ATSCMH_TNOG:
1412 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1413 break;
1414 case DTV_ATSCMH_SGN:
1415 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1416 break;
1417 case DTV_ATSCMH_PRC:
1418 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1419 break;
1420 case DTV_ATSCMH_RS_FRAME_MODE:
1421 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1422 break;
1423 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1424 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1425 break;
1426 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1427 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1428 break;
1429 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1430 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1431 break;
1432 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1433 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1434 break;
1435 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1436 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1437 break;
1438 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1439 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1440 break;
1441 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1442 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1443 break;
1444 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1445 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1446 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001447
Antti Palosaari33eebec2012-10-03 04:28:56 -03001448 case DTV_LNA:
1449 tvp->u.data = c->lna;
1450 break;
1451
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001452 /* Fill quality measures */
1453 case DTV_STAT_SIGNAL_STRENGTH:
1454 tvp->u.st = c->strength;
1455 break;
1456 case DTV_STAT_CNR:
1457 tvp->u.st = c->cnr;
1458 break;
1459 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1460 tvp->u.st = c->pre_bit_error;
1461 break;
1462 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1463 tvp->u.st = c->pre_bit_count;
1464 break;
1465 case DTV_STAT_POST_ERROR_BIT_COUNT:
1466 tvp->u.st = c->post_bit_error;
1467 break;
1468 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1469 tvp->u.st = c->post_bit_count;
1470 break;
1471 case DTV_STAT_ERROR_BLOCK_COUNT:
1472 tvp->u.st = c->block_error;
1473 break;
1474 case DTV_STAT_TOTAL_BLOCK_COUNT:
1475 tvp->u.st = c->block_count;
1476 break;
Steven Toth363429a2008-09-12 01:34:28 -03001477 default:
Mauro Carvalho Chehab94a93e52013-01-23 17:06:02 -03001478 dev_dbg(fe->dvb->device,
1479 "%s: FE property %d doesn't exist\n",
1480 __func__, tvp->cmd);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001481 return -EINVAL;
Steven Toth363429a2008-09-12 01:34:28 -03001482 }
1483
Andreas Oberrittere23d9ae2011-05-08 20:03:36 -03001484 /* Allow the frontend to override outgoing properties */
1485 if (fe->ops.get_property) {
1486 r = fe->ops.get_property(fe, tvp);
1487 if (r < 0)
1488 return r;
1489 }
1490
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001491 dtv_property_dump(fe, tvp);
Mauro Carvalho Chehab639544d2009-12-31 17:49:32 -03001492
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001493 return 0;
Steven Toth363429a2008-09-12 01:34:28 -03001494}
1495
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001496static int dtv_set_frontend(struct dvb_frontend *fe);
1497
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001498static bool is_dvbv3_delsys(u32 delsys)
1499{
1500 bool status;
1501
1502 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1503 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1504
1505 return status;
1506}
1507
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001508/**
1509 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1510 * @fe: struct frontend;
1511 * @delsys: DVBv5 type that will be used for emulation
1512 *
1513 * Provides emulation for delivery systems that are compatible with the old
1514 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1515 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1516 * parameters are compatible with DVB-S spec.
1517 */
1518static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001519{
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001520 int i;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001521 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001522
Mauro Carvalho Chehab36264792012-04-17 18:32:19 -03001523 c->delivery_system = delsys;
1524
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001525 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001526 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001527 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001528 if (c->delivery_system == SYS_ISDBT) {
1529 dev_dbg(fe->dvb->device,
1530 "%s: Using defaults for SYS_ISDBT\n",
1531 __func__);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001532
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001533 if (!c->bandwidth_hz)
1534 c->bandwidth_hz = 6000000;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001535
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001536 c->isdbt_partial_reception = 0;
1537 c->isdbt_sb_mode = 0;
1538 c->isdbt_sb_subchannel = 0;
1539 c->isdbt_sb_segment_idx = 0;
1540 c->isdbt_sb_segment_count = 0;
1541 c->isdbt_layer_enabled = 7;
1542 for (i = 0; i < 3; i++) {
1543 c->layer[i].fec = FEC_AUTO;
1544 c->layer[i].modulation = QAM_AUTO;
1545 c->layer[i].interleaving = 0;
1546 c->layer[i].segment_count = 0;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001547 }
1548 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001549 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001550 __func__, c->delivery_system);
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001551
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001552 return 0;
1553}
1554
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001555/**
1556 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1557 * @fe: frontend struct
1558 * @desired_system: delivery system requested by the user
1559 *
1560 * A DVBv5 call know what's the desired system it wants. So, set it.
1561 *
1562 * There are, however, a few known issues with early DVBv5 applications that
1563 * are also handled by this logic:
1564 *
1565 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1566 * This is an API violation, but, as we don't want to break userspace,
1567 * convert it to the first supported delivery system.
1568 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1569 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1570 * ISDB-T provided backward compat with DVB-T.
1571 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001572static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1573 u32 desired_system)
1574{
1575 int ncaps;
1576 u32 delsys = SYS_UNDEFINED;
1577 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1578 enum dvbv3_emulation_type type;
1579
1580 /*
1581 * It was reported that some old DVBv5 applications were
1582 * filling delivery_system with SYS_UNDEFINED. If this happens,
1583 * assume that the application wants to use the first supported
1584 * delivery system.
1585 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001586 if (desired_system == SYS_UNDEFINED)
1587 desired_system = fe->ops.delsys[0];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001588
1589 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001590 * This is a DVBv5 call. So, it likely knows the supported
1591 * delivery systems. So, check if the desired delivery system is
1592 * supported
1593 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001594 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001595 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001596 if (fe->ops.delsys[ncaps] == desired_system) {
1597 c->delivery_system = desired_system;
1598 dev_dbg(fe->dvb->device,
1599 "%s: Changing delivery system to %d\n",
1600 __func__, desired_system);
1601 return 0;
1602 }
1603 ncaps++;
1604 }
1605
1606 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001607 * The requested delivery system isn't supported. Maybe userspace
1608 * is requesting a DVBv3 compatible delivery system.
1609 *
1610 * The emulation only works if the desired system is one of the
1611 * delivery systems supported by DVBv3 API
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001612 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001613 if (!is_dvbv3_delsys(desired_system)) {
1614 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001615 "%s: Delivery system %d not supported.\n",
1616 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001617 return -EINVAL;
1618 }
1619
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001620 type = dvbv3_type(desired_system);
1621
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001622 /*
1623 * Get the last non-DVBv3 delivery system that has the same type
1624 * of the desired system
1625 */
1626 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001627 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001628 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001629 delsys = fe->ops.delsys[ncaps];
1630 ncaps++;
1631 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001632
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001633 /* There's nothing compatible with the desired delivery system */
1634 if (delsys == SYS_UNDEFINED) {
1635 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001636 "%s: Delivery system %d not supported on emulation mode.\n",
1637 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001638 return -EINVAL;
1639 }
1640
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001641 dev_dbg(fe->dvb->device,
1642 "%s: Using delivery system %d emulated as if it were %d\n",
1643 __func__, delsys, desired_system);
1644
1645 return emulate_delivery_system(fe, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001646}
1647
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001648/**
1649 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1650 * @fe: frontend struct
1651 *
1652 * A DVBv3 call doesn't know what's the desired system it wants. It also
1653 * doesn't allow to switch between different types. Due to that, userspace
1654 * should use DVBv5 instead.
1655 * However, in order to avoid breaking userspace API, limited backward
1656 * compatibility support is provided.
1657 *
1658 * There are some delivery systems that are incompatible with DVBv3 calls.
1659 *
1660 * This routine should work fine for frontends that support just one delivery
1661 * system.
1662 *
1663 * For frontends that support multiple frontends:
1664 * 1) It defaults to use the first supported delivery system. There's an
1665 * userspace application that allows changing it at runtime;
1666 *
1667 * 2) If the current delivery system is not compatible with DVBv3, it gets
1668 * the first one that it is compatible.
1669 *
1670 * NOTE: in order for this to work with applications like Kaffeine that
1671 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1672 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1673 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1674 * to DVB-S.
1675 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001676static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1677{
1678 int ncaps;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001679 u32 delsys = SYS_UNDEFINED;
1680 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001681
1682 /* If not set yet, defaults to the first supported delivery system */
1683 if (c->delivery_system == SYS_UNDEFINED)
1684 c->delivery_system = fe->ops.delsys[0];
1685
1686 /*
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001687 * Trivial case: just use the current one, if it already a DVBv3
1688 * delivery system
1689 */
1690 if (is_dvbv3_delsys(c->delivery_system)) {
1691 dev_dbg(fe->dvb->device,
1692 "%s: Using delivery system to %d\n",
1693 __func__, c->delivery_system);
1694 return 0;
1695 }
1696
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001697 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001698 * Seek for the first delivery system that it is compatible with a
1699 * DVBv3 standard
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001700 */
1701 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001702 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001703 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1704 delsys = fe->ops.delsys[ncaps];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001705 break;
1706 }
1707 ncaps++;
1708 }
1709 if (delsys == SYS_UNDEFINED) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001710 dev_dbg(fe->dvb->device,
1711 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1712 __func__);
1713 return -EINVAL;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001714 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001715 return emulate_delivery_system(fe, delsys);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001716}
1717
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001718static int dtv_property_process_set(struct dvb_frontend *fe,
1719 struct dtv_property *tvp,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001720 struct file *file)
Steven Toth6b73eea2008-09-04 01:12:25 -03001721{
1722 int r = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -03001723 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001724
Steven Tothbfbf2da2008-09-12 01:37:37 -03001725 /* Allow the frontend to validate incoming properties */
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001726 if (fe->ops.set_property) {
Steven Tothbfbf2da2008-09-12 01:37:37 -03001727 r = fe->ops.set_property(fe, tvp);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001728 if (r < 0)
1729 return r;
1730 }
Steven Tothbfbf2da2008-09-12 01:37:37 -03001731
Steven Toth6b73eea2008-09-04 01:12:25 -03001732 switch(tvp->cmd) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001733 case DTV_CLEAR:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001734 /*
1735 * Reset a cache of data specific to the frontend here. This does
Steven Toth6b73eea2008-09-04 01:12:25 -03001736 * not effect hardware.
1737 */
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001738 dvb_frontend_clear_cache(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001739 break;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001740 case DTV_TUNE:
Steven Toth6b73eea2008-09-04 01:12:25 -03001741 /* interpret the cache of data, build either a traditional frontend
Steven Tothd5748f12008-09-12 00:27:13 -03001742 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1743 * ioctl.
Steven Toth6b73eea2008-09-04 01:12:25 -03001744 */
Andreas Oberritter50727712011-05-08 20:03:39 -03001745 c->state = tvp->cmd;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001746 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1747 __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001748
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001749 r = dtv_set_frontend(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001750 break;
Steven Toth363429a2008-09-12 01:34:28 -03001751 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001752 c->frequency = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001753 break;
Steven Toth363429a2008-09-12 01:34:28 -03001754 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001755 c->modulation = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001756 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001757 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001758 c->bandwidth_hz = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001759 break;
Steven Toth363429a2008-09-12 01:34:28 -03001760 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001761 c->inversion = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001762 break;
Steven Toth363429a2008-09-12 01:34:28 -03001763 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001764 c->symbol_rate = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001765 break;
Steven Toth363429a2008-09-12 01:34:28 -03001766 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001767 c->fec_inner = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001768 break;
Steven Toth363429a2008-09-12 01:34:28 -03001769 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001770 c->pilot = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001771 break;
Steven Toth363429a2008-09-12 01:34:28 -03001772 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001773 c->rolloff = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001774 break;
Steven Toth363429a2008-09-12 01:34:28 -03001775 case DTV_DELIVERY_SYSTEM:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001776 r = dvbv5_set_delivery_system(fe, tvp->u.data);
Steven Toth6b73eea2008-09-04 01:12:25 -03001777 break;
Steven Toth363429a2008-09-12 01:34:28 -03001778 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001779 c->voltage = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001780 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001781 (void *)c->voltage);
Steven Toth13c97bf2008-09-04 21:19:43 -03001782 break;
Steven Toth363429a2008-09-12 01:34:28 -03001783 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001784 c->sectone = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001785 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001786 (void *)c->sectone);
Steven Toth13c97bf2008-09-04 21:19:43 -03001787 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001788 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001789 c->code_rate_HP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001790 break;
1791 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001792 c->code_rate_LP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001793 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001794 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001795 c->guard_interval = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001796 break;
1797 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001798 c->transmission_mode = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001799 break;
Steven Tothef526f42008-10-06 22:01:47 -03001800 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001801 c->hierarchy = tvp->u.data;
Steven Tothef526f42008-10-06 22:01:47 -03001802 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001803 case DTV_INTERLEAVING:
1804 c->interleaving = tvp->u.data;
1805 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001806
1807 /* ISDB-T Support here */
1808 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001809 c->isdbt_partial_reception = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001810 break;
1811 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001812 c->isdbt_sb_mode = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001813 break;
1814 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001815 c->isdbt_sb_subchannel = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001816 break;
1817 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001818 c->isdbt_sb_segment_idx = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001819 break;
1820 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001821 c->isdbt_sb_segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001822 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001823 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001824 c->isdbt_layer_enabled = tvp->u.data;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001825 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001826 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001827 c->layer[0].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001828 break;
1829 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001830 c->layer[0].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001831 break;
1832 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001833 c->layer[0].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001834 break;
1835 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001836 c->layer[0].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001837 break;
1838 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001839 c->layer[1].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001840 break;
1841 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001842 c->layer[1].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001843 break;
1844 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001845 c->layer[1].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001846 break;
1847 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001848 c->layer[1].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001849 break;
1850 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001851 c->layer[2].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001852 break;
1853 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001854 c->layer[2].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001855 break;
1856 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001857 c->layer[2].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001858 break;
1859 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001860 c->layer[2].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001861 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001862
1863 /* Multistream support */
1864 case DTV_STREAM_ID:
1865 case DTV_DVBT2_PLP_ID_LEGACY:
1866 c->stream_id = tvp->u.data;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001867 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001868
1869 /* ATSC-MH */
1870 case DTV_ATSCMH_PARADE_ID:
1871 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1872 break;
1873 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1874 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1875 break;
1876
Antti Palosaari33eebec2012-10-03 04:28:56 -03001877 case DTV_LNA:
1878 c->lna = tvp->u.data;
1879 if (fe->ops.set_lna)
1880 r = fe->ops.set_lna(fe);
Mauro Carvalho Chehabe6876692014-02-16 06:36:17 -03001881 if (r < 0)
1882 c->lna = LNA_AUTO;
Antti Palosaari33eebec2012-10-03 04:28:56 -03001883 break;
1884
Steven Toth363429a2008-09-12 01:34:28 -03001885 default:
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001886 return -EINVAL;
Steven Toth6b73eea2008-09-04 01:12:25 -03001887 }
1888
Steven Toth13c97bf2008-09-04 21:19:43 -03001889 return r;
Steven Toth6b73eea2008-09-04 01:12:25 -03001890}
1891
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001892static int dvb_frontend_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 unsigned int cmd, void *parg)
1894{
1895 struct dvb_device *dvbdev = file->private_data;
1896 struct dvb_frontend *fe = dvbdev->priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001897 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001898 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02001899 int err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001901 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
Juergen Lock6ae23222012-12-23 17:23:06 -03001902 if (down_interruptible(&fepriv->sem))
1903 return -ERESTARTSYS;
1904
Shuah Khan18ed2862014-07-12 13:44:12 -03001905 if (fe->exit != DVB_FE_NO_EXIT) {
Juergen Lock6ae23222012-12-23 17:23:06 -03001906 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return -ENODEV;
Juergen Lock6ae23222012-12-23 17:23:06 -03001908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1911 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
Juergen Lock6ae23222012-12-23 17:23:06 -03001912 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
1913 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return -EPERM;
Juergen Lock6ae23222012-12-23 17:23:06 -03001915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Steven Toth13c97bf2008-09-04 21:19:43 -03001917 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001918 err = dvb_frontend_ioctl_properties(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001919 else {
Andreas Oberritter50727712011-05-08 20:03:39 -03001920 c->state = DTV_UNDEFINED;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001921 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001922 }
Steven Toth13c97bf2008-09-04 21:19:43 -03001923
1924 up(&fepriv->sem);
1925 return err;
1926}
1927
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001928static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001929 unsigned int cmd, void *parg)
1930{
1931 struct dvb_device *dvbdev = file->private_data;
1932 struct dvb_frontend *fe = dvbdev->priv;
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02001933 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001934 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Igor M. Liplianine6f9ec82008-09-12 14:49:06 -03001935 int err = 0;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001936
1937 struct dtv_properties *tvps = NULL;
1938 struct dtv_property *tvp = NULL;
1939 int i;
Steven Toth13c97bf2008-09-04 21:19:43 -03001940
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001941 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Steven Toth13c97bf2008-09-04 21:19:43 -03001942
Steven Toth6b73eea2008-09-04 01:12:25 -03001943 if(cmd == FE_SET_PROPERTY) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001944 tvps = (struct dtv_properties __user *)parg;
Steven Toth6b73eea2008-09-04 01:12:25 -03001945
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001946 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1947 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001948
1949 /* Put an arbitrary limit on the number of messages that can
1950 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001951 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Tothe7fee0f32008-09-11 10:23:01 -03001952 return -EINVAL;
1953
Thiago Farina6e3924a2010-01-16 10:25:38 -03001954 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001955 if (!tvp) {
1956 err = -ENOMEM;
1957 goto out;
Steven Toth6b73eea2008-09-04 01:12:25 -03001958 }
1959
Steven Tothe7fee0f32008-09-11 10:23:01 -03001960 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1961 err = -EFAULT;
1962 goto out;
1963 }
1964
Steven Tothd48cb402008-09-26 00:16:25 -03001965 for (i = 0; i < tvps->num; i++) {
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001966 err = dtv_property_process_set(fe, tvp + i, file);
1967 if (err < 0)
1968 goto out;
1969 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03001970 }
Steven Tothe7fee0f32008-09-11 10:23:01 -03001971
Andreas Oberritter50727712011-05-08 20:03:39 -03001972 if (c->state == DTV_TUNE)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001973 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
Steven Tothbfbf2da2008-09-12 01:37:37 -03001974
Steven Toth363429a2008-09-12 01:34:28 -03001975 } else
1976 if(cmd == FE_GET_PROPERTY) {
Steven Toth363429a2008-09-12 01:34:28 -03001977 tvps = (struct dtv_properties __user *)parg;
1978
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001979 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1980 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Toth363429a2008-09-12 01:34:28 -03001981
1982 /* Put an arbitrary limit on the number of messages that can
1983 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001984 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Toth363429a2008-09-12 01:34:28 -03001985 return -EINVAL;
1986
Thiago Farina6e3924a2010-01-16 10:25:38 -03001987 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Toth363429a2008-09-12 01:34:28 -03001988 if (!tvp) {
1989 err = -ENOMEM;
1990 goto out;
1991 }
1992
1993 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1994 err = -EFAULT;
1995 goto out;
1996 }
1997
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001998 /*
1999 * Fills the cache out struct with the cache contents, plus
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002000 * the data retrieved from get_frontend, if the frontend
2001 * is not idle. Otherwise, returns the cached content
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03002002 */
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002003 if (fepriv->state != FESTATE_IDLE) {
2004 err = dtv_get_frontend(fe, NULL);
2005 if (err < 0)
2006 goto out;
2007 }
Steven Tothd48cb402008-09-26 00:16:25 -03002008 for (i = 0; i < tvps->num; i++) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03002009 err = dtv_property_process_get(fe, c, tvp + i, file);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01002010 if (err < 0)
2011 goto out;
2012 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03002013 }
Steven Toth363429a2008-09-12 01:34:28 -03002014
2015 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
2016 err = -EFAULT;
2017 goto out;
2018 }
2019
Steven Toth363429a2008-09-12 01:34:28 -03002020 } else
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002021 err = -EOPNOTSUPP;
Steven Toth363429a2008-09-12 01:34:28 -03002022
Steven Tothe7fee0f32008-09-11 10:23:01 -03002023out:
2024 kfree(tvp);
Steven Toth13c97bf2008-09-04 21:19:43 -03002025 return err;
2026}
2027
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002028static int dtv_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002029{
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002030 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2031 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2032 struct dvb_frontend_tune_settings fetunesettings;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002033 u32 rolloff = 0;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002034
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002035 if (dvb_frontend_check_parameters(fe) < 0)
2036 return -EINVAL;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002037
2038 /*
Chris Rankin556a0442012-04-06 18:38:18 -03002039 * Initialize output parameters to match the values given by
2040 * the user. FE_SET_FRONTEND triggers an initial frontend event
2041 * with status = 0, which copies output parameters to userspace.
2042 */
2043 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
2044
2045 /*
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002046 * Be sure that the bandwidth will be filled for all
2047 * non-satellite systems, as tuners need to know what
2048 * low pass/Nyquist half filter should be applied, in
2049 * order to avoid inter-channel noise.
2050 *
2051 * ISDB-T and DVB-T/T2 already sets bandwidth.
2052 * ATSC and DVB-C don't set, so, the core should fill it.
2053 *
2054 * On DVB-C Annex A and C, the bandwidth is a function of
2055 * the roll-off and symbol rate. Annex B defines different
2056 * roll-off factors depending on the modulation. Fortunately,
2057 * Annex B is only used with 6MHz, so there's no need to
2058 * calculate it.
2059 *
2060 * While not officially supported, a side effect of handling it at
2061 * the cache level is that a program could retrieve the bandwidth
2062 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2063 */
2064 switch (c->delivery_system) {
2065 case SYS_ATSC:
2066 case SYS_DVBC_ANNEX_B:
2067 c->bandwidth_hz = 6000000;
2068 break;
2069 case SYS_DVBC_ANNEX_A:
2070 rolloff = 115;
2071 break;
2072 case SYS_DVBC_ANNEX_C:
2073 rolloff = 113;
2074 break;
2075 default:
2076 break;
2077 }
2078 if (rolloff)
2079 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002080
2081 /* force auto frequency inversion if requested */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002082 if (dvb_force_auto_inversion)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002083 c->inversion = INVERSION_AUTO;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002084
2085 /*
2086 * without hierarchical coding code_rate_LP is irrelevant,
2087 * so we tolerate the otherwise invalid FEC_NONE setting
2088 */
2089 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2090 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002091
2092 /* get frontend-specific tuning settings */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002093 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002094 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2095 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2096 fepriv->max_drift = fetunesettings.max_drift;
2097 fepriv->step_size = fetunesettings.step_size;
2098 } else {
2099 /* default values */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002100 switch (c->delivery_system) {
Mauro Carvalho Chehab910a5f22012-05-07 15:01:37 -03002101 case SYS_DVBS:
2102 case SYS_DVBS2:
2103 case SYS_ISDBS:
2104 case SYS_TURBO:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002105 case SYS_DVBC_ANNEX_A:
2106 case SYS_DVBC_ANNEX_C:
2107 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002108 fepriv->step_size = c->symbol_rate / 16000;
2109 fepriv->max_drift = c->symbol_rate / 2000;
2110 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002111 case SYS_DVBT:
2112 case SYS_DVBT2:
2113 case SYS_ISDBT:
Antti Palosaari224b6642012-08-12 22:33:21 -03002114 case SYS_DTMB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002115 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002116 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2117 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2118 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002119 default:
2120 /*
2121 * FIXME: This sounds wrong! if freqency_stepsize is
2122 * defined by the frontend, why not use it???
2123 */
2124 fepriv->min_delay = HZ / 20;
2125 fepriv->step_size = 0; /* no zigzag */
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002126 fepriv->max_drift = 0;
2127 break;
2128 }
2129 }
2130 if (dvb_override_tune_delay > 0)
2131 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2132
2133 fepriv->state = FESTATE_RETUNE;
2134
2135 /* Request the search algorithm to search */
2136 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2137
2138 dvb_frontend_clear_events(fe);
2139 dvb_frontend_add_event(fe, 0);
2140 dvb_frontend_wakeup(fe);
2141 fepriv->status = 0;
2142
2143 return 0;
2144}
2145
2146
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002147static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03002148 unsigned int cmd, void *parg)
2149{
2150 struct dvb_device *dvbdev = file->private_data;
2151 struct dvb_frontend *fe = dvbdev->priv;
2152 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002153 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002154 int err = -EOPNOTSUPP;
Steven Toth13c97bf2008-09-04 21:19:43 -03002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 switch (cmd) {
2157 case FE_GET_INFO: {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002158 struct dvb_frontend_info* info = parg;
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002159
Patrick Boettcherdea74862006-05-14 05:01:31 -03002160 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
Guillaume Audirac2030c032010-05-06 09:30:25 -03002161 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002163 /*
2164 * Associate the 4 delivery systems supported by DVBv3
2165 * API with their DVBv5 counterpart. For the other standards,
2166 * use the closest type, assuming that it would hopefully
2167 * work with a DVBv3 application.
2168 * It should be noticed that, on multi-frontend devices with
2169 * different types (terrestrial and cable, for example),
2170 * a pure DVBv3 application won't be able to use all delivery
2171 * systems. Yet, changing the DVBv5 cache to the other delivery
2172 * system should be enough for making it work.
2173 */
2174 switch (dvbv3_type(c->delivery_system)) {
2175 case DVBV3_QPSK:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002176 info->type = FE_QPSK;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002177 break;
2178 case DVBV3_ATSC:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002179 info->type = FE_ATSC;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002180 break;
2181 case DVBV3_QAM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002182 info->type = FE_QAM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002183 break;
2184 case DVBV3_OFDM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002185 info->type = FE_OFDM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002186 break;
2187 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002188 dev_err(fe->dvb->device,
2189 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2190 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002191 fe->ops.info.type = FE_OFDM;
2192 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002193 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2194 __func__, c->delivery_system, fe->ops.info.type);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2197 * do it, it is done for it. */
2198 info->caps |= FE_CAN_INVERSION_AUTO;
2199 err = 0;
2200 break;
2201 }
2202
Peter Beutner6757ccc2005-07-07 17:57:36 -07002203 case FE_READ_STATUS: {
2204 fe_status_t* status = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002206 /* if retune was requested but hasn't occurred yet, prevent
Peter Beutner6757ccc2005-07-07 17:57:36 -07002207 * that user get signal state from previous tuning */
Janne Grunau01886252009-09-01 19:23:09 -03002208 if (fepriv->state == FESTATE_RETUNE ||
2209 fepriv->state == FESTATE_ERROR) {
Peter Beutner6757ccc2005-07-07 17:57:36 -07002210 err=0;
2211 *status = 0;
2212 break;
2213 }
2214
Patrick Boettcherdea74862006-05-14 05:01:31 -03002215 if (fe->ops.read_status)
2216 err = fe->ops.read_status(fe, status);
Peter Beutner6757ccc2005-07-07 17:57:36 -07002217 break;
2218 }
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 case FE_READ_BER:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002221 if (fe->ops.read_ber) {
2222 if (fepriv->thread)
2223 err = fe->ops.read_ber(fe, (__u32 *) parg);
2224 else
2225 err = -EAGAIN;
2226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 break;
2228
2229 case FE_READ_SIGNAL_STRENGTH:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002230 if (fe->ops.read_signal_strength) {
2231 if (fepriv->thread)
2232 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2233 else
2234 err = -EAGAIN;
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 break;
2237
2238 case FE_READ_SNR:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002239 if (fe->ops.read_snr) {
2240 if (fepriv->thread)
2241 err = fe->ops.read_snr(fe, (__u16 *) parg);
2242 else
2243 err = -EAGAIN;
2244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 break;
2246
2247 case FE_READ_UNCORRECTED_BLOCKS:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002248 if (fe->ops.read_ucblocks) {
2249 if (fepriv->thread)
2250 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2251 else
2252 err = -EAGAIN;
2253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 break;
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 case FE_DISEQC_RESET_OVERLOAD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002257 if (fe->ops.diseqc_reset_overload) {
2258 err = fe->ops.diseqc_reset_overload(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 fepriv->state = FESTATE_DISEQC;
2260 fepriv->status = 0;
2261 }
2262 break;
2263
2264 case FE_DISEQC_SEND_MASTER_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002265 if (fe->ops.diseqc_send_master_cmd) {
2266 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 fepriv->state = FESTATE_DISEQC;
2268 fepriv->status = 0;
2269 }
2270 break;
2271
2272 case FE_DISEQC_SEND_BURST:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002273 if (fe->ops.diseqc_send_burst) {
2274 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 fepriv->state = FESTATE_DISEQC;
2276 fepriv->status = 0;
2277 }
2278 break;
2279
2280 case FE_SET_TONE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002281 if (fe->ops.set_tone) {
2282 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03002283 fepriv->tone = (fe_sec_tone_mode_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 fepriv->state = FESTATE_DISEQC;
2285 fepriv->status = 0;
2286 }
2287 break;
2288
2289 case FE_SET_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002290 if (fe->ops.set_voltage) {
2291 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03002292 fepriv->voltage = (fe_sec_voltage_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 fepriv->state = FESTATE_DISEQC;
2294 fepriv->status = 0;
2295 }
2296 break;
2297
2298 case FE_DISHNETWORK_SEND_LEGACY_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002299 if (fe->ops.dishnetwork_send_legacy_command) {
2300 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 fepriv->state = FESTATE_DISEQC;
2302 fepriv->status = 0;
Patrick Boettcherdea74862006-05-14 05:01:31 -03002303 } else if (fe->ops.set_voltage) {
NooneImportant83b75b02005-11-08 21:35:27 -08002304 /*
2305 * NOTE: This is a fallback condition. Some frontends
2306 * (stv0299 for instance) take longer than 8msec to
2307 * respond to a set_voltage command. Those switches
2308 * need custom routines to switch properly. For all
Guillaume Audirac2030c032010-05-06 09:30:25 -03002309 * other frontends, the following should work ok.
NooneImportant83b75b02005-11-08 21:35:27 -08002310 * Dish network legacy switches (as used by Dish500)
2311 * are controlled by sending 9-bit command words
2312 * spaced 8msec apart.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002313 * the actual command word is switch/port dependent
NooneImportant83b75b02005-11-08 21:35:27 -08002314 * so it is up to the userspace application to send
2315 * the right command.
2316 * The command must always start with a '0' after
2317 * initialization, so parg is 8 bits and does not
2318 * include the initialization or start bit
2319 */
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002320 unsigned long swcmd = ((unsigned long) parg) << 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002321 struct timeval nexttime;
2322 struct timeval tv[10];
2323 int i;
2324 u8 last = 1;
2325 if (dvb_frontend_debug)
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002326 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
NooneImportant83b75b02005-11-08 21:35:27 -08002327 do_gettimeofday(&nexttime);
2328 if (dvb_frontend_debug)
Ezequiel Garciab9b1b3a2012-10-23 15:57:23 -03002329 tv[0] = nexttime;
NooneImportant83b75b02005-11-08 21:35:27 -08002330 /* before sending a command, initialize by sending
2331 * a 32ms 18V to the switch
2332 */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002333 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002334 dvb_frontend_sleep_until(&nexttime, 32000);
2335
2336 for (i = 0; i < 9; i++) {
2337 if (dvb_frontend_debug)
2338 do_gettimeofday(&tv[i + 1]);
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002339 if ((swcmd & 0x01) != last) {
NooneImportant83b75b02005-11-08 21:35:27 -08002340 /* set voltage to (last ? 13V : 18V) */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002341 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002342 last = (last) ? 0 : 1;
2343 }
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002344 swcmd = swcmd >> 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002345 if (i != 8)
2346 dvb_frontend_sleep_until(&nexttime, 8000);
2347 }
2348 if (dvb_frontend_debug) {
2349 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002350 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -08002351 for (i = 1; i < 10; i++)
2352 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2353 }
2354 err = 0;
2355 fepriv->state = FESTATE_DISEQC;
2356 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
2358 break;
2359
2360 case FE_DISEQC_RECV_SLAVE_REPLY:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002361 if (fe->ops.diseqc_recv_slave_reply)
2362 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 break;
2364
2365 case FE_ENABLE_HIGH_LNB_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002366 if (fe->ops.enable_high_lnb_voltage)
2367 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 break;
2369
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002370 case FE_SET_FRONTEND:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002371 err = dvbv3_set_delivery_system(fe);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03002372 if (err)
2373 break;
2374
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002375 err = dtv_property_cache_sync(fe, c, parg);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002376 if (err)
2377 break;
2378 err = dtv_set_frontend(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 case FE_GET_EVENT:
2381 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2382 break;
2383
2384 case FE_GET_FRONTEND:
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002385 err = dtv_get_frontend(fe, parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 break;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002387
2388 case FE_SET_FRONTEND_TUNE_MODE:
Hans Verkuile18828e2006-01-09 15:25:28 -02002389 fepriv->tune_mode_flags = (unsigned long) parg;
Trent Piepho1b172e02006-06-29 13:16:04 -03002390 err = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002391 break;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03002392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 return err;
2395}
2396
Steven Toth6b73eea2008-09-04 01:12:25 -03002397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2399{
2400 struct dvb_device *dvbdev = file->private_data;
2401 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002402 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002404 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 poll_wait (file, &fepriv->events.wait_queue, wait);
2407
2408 if (fepriv->events.eventw != fepriv->events.eventr)
2409 return (POLLIN | POLLRDNORM | POLLPRI);
2410
2411 return 0;
2412}
2413
2414static int dvb_frontend_open(struct inode *inode, struct file *file)
2415{
2416 struct dvb_device *dvbdev = file->private_data;
2417 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002418 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Darron Broad59b18422008-10-11 11:44:05 -03002419 struct dvb_adapter *adapter = fe->dvb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 int ret;
2421
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002422 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Shuah Khan18ed2862014-07-12 13:44:12 -03002423 if (fe->exit == DVB_FE_DEVICE_REMOVED)
matthieu castete36309f2010-05-05 15:59:20 -03002424 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Darron Broad59b18422008-10-11 11:44:05 -03002426 if (adapter->mfe_shared) {
2427 mutex_lock (&adapter->mfe_lock);
Darron Broad65946902008-10-15 13:37:59 -03002428
2429 if (adapter->mfe_dvbdev == NULL)
2430 adapter->mfe_dvbdev = dvbdev;
2431
2432 else if (adapter->mfe_dvbdev != dvbdev) {
2433 struct dvb_device
2434 *mfedev = adapter->mfe_dvbdev;
2435 struct dvb_frontend
2436 *mfe = mfedev->priv;
2437 struct dvb_frontend_private
2438 *mfepriv = mfe->frontend_priv;
2439 int mferetry = (dvb_mfe_wait_time << 1);
2440
2441 mutex_unlock (&adapter->mfe_lock);
2442 while (mferetry-- && (mfedev->users != -1 ||
2443 mfepriv->thread != NULL)) {
2444 if(msleep_interruptible(500)) {
2445 if(signal_pending(current))
2446 return -EINTR;
Darron Broad59b18422008-10-11 11:44:05 -03002447 }
2448 }
Darron Broad65946902008-10-15 13:37:59 -03002449
2450 mutex_lock (&adapter->mfe_lock);
2451 if(adapter->mfe_dvbdev != dvbdev) {
2452 mfedev = adapter->mfe_dvbdev;
2453 mfe = mfedev->priv;
2454 mfepriv = mfe->frontend_priv;
2455 if (mfedev->users != -1 ||
2456 mfepriv->thread != NULL) {
2457 mutex_unlock (&adapter->mfe_lock);
2458 return -EBUSY;
2459 }
2460 adapter->mfe_dvbdev = dvbdev;
2461 }
Darron Broad59b18422008-10-11 11:44:05 -03002462 }
2463 }
2464
Oliver Endriss48136e12007-08-25 12:00:23 -03002465 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2466 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
Darron Broad59b18422008-10-11 11:44:05 -03002467 goto err0;
Devin Heitmueller2d196932011-07-04 21:55:01 -03002468
2469 /* If we took control of the bus, we need to force
2470 reinitialization. This is because many ts_bus_ctrl()
2471 functions strobe the RESET pin on the demod, and if the
2472 frontend thread already exists then the dvb_init() routine
2473 won't get called (which is what usually does initial
2474 register configuration). */
2475 fepriv->reinitialise = 1;
Steven Tothba7e6f32006-09-25 12:41:53 -03002476 }
2477
Oliver Endriss48136e12007-08-25 12:00:23 -03002478 if ((ret = dvb_generic_open (inode, file)) < 0)
2479 goto err1;
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002480
Oliver Endriss48136e12007-08-25 12:00:23 -03002481 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002482 /* normal tune mode when opened R/W */
2483 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2484 fepriv->tone = -1;
2485 fepriv->voltage = -1;
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 ret = dvb_frontend_start (fe);
2488 if (ret)
Oliver Endriss48136e12007-08-25 12:00:23 -03002489 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 /* empty event queue */
2492 fepriv->events.eventr = fepriv->events.eventw = 0;
2493 }
2494
Darron Broad59b18422008-10-11 11:44:05 -03002495 if (adapter->mfe_shared)
2496 mutex_unlock (&adapter->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return ret;
Oliver Endriss48136e12007-08-25 12:00:23 -03002498
2499err2:
2500 dvb_generic_release(inode, file);
2501err1:
2502 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2503 fe->ops.ts_bus_ctrl(fe, 0);
Darron Broad59b18422008-10-11 11:44:05 -03002504err0:
2505 if (adapter->mfe_shared)
2506 mutex_unlock (&adapter->mfe_lock);
Oliver Endriss48136e12007-08-25 12:00:23 -03002507 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510static int dvb_frontend_release(struct inode *inode, struct file *file)
2511{
2512 struct dvb_device *dvbdev = file->private_data;
2513 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002514 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002515 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002517 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002519 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 fepriv->release_jiffies = jiffies;
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002521 mb();
2522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002524 ret = dvb_generic_release (inode, file);
2525
Oliver Endriss48136e12007-08-25 12:00:23 -03002526 if (dvbdev->users == -1) {
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002527 wake_up(&fepriv->wait_queue);
Shuah Khan18ed2862014-07-12 13:44:12 -03002528 if (fe->exit != DVB_FE_NO_EXIT)
Oliver Endriss48136e12007-08-25 12:00:23 -03002529 wake_up(&dvbdev->wait_queue);
Oliver Endriss48136e12007-08-25 12:00:23 -03002530 if (fe->ops.ts_bus_ctrl)
2531 fe->ops.ts_bus_ctrl(fe, 0);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002532 }
Oliver Endriss48136e12007-08-25 12:00:23 -03002533
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002534 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
Jan Engelhardt784e29d2009-01-11 06:12:43 -03002537static const struct file_operations dvb_frontend_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 .owner = THIS_MODULE,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002539 .unlocked_ioctl = dvb_generic_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 .poll = dvb_frontend_poll,
2541 .open = dvb_frontend_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002542 .release = dvb_frontend_release,
2543 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544};
2545
Antti Palosaari06bae122012-08-14 22:21:06 -03002546int dvb_frontend_suspend(struct dvb_frontend *fe)
2547{
2548 int ret = 0;
2549
2550 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2551 fe->id);
2552
2553 if (fe->ops.tuner_ops.sleep)
2554 ret = fe->ops.tuner_ops.sleep(fe);
2555
2556 if (fe->ops.sleep)
2557 ret = fe->ops.sleep(fe);
2558
2559 return ret;
2560}
2561EXPORT_SYMBOL(dvb_frontend_suspend);
2562
2563int dvb_frontend_resume(struct dvb_frontend *fe)
2564{
2565 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2566 int ret = 0;
2567
2568 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2569 fe->id);
2570
Shuah Khan87cd0fa2014-07-24 13:02:14 -03002571 fe->exit = DVB_FE_DEVICE_RESUME;
Antti Palosaari06bae122012-08-14 22:21:06 -03002572 if (fe->ops.init)
2573 ret = fe->ops.init(fe);
2574
2575 if (fe->ops.tuner_ops.init)
2576 ret = fe->ops.tuner_ops.init(fe);
2577
Shuah Khan87cd0fa2014-07-24 13:02:14 -03002578 fe->exit = DVB_FE_NO_EXIT;
Antti Palosaari06bae122012-08-14 22:21:06 -03002579 fepriv->state = FESTATE_RETUNE;
2580 dvb_frontend_wakeup(fe);
2581
2582 return ret;
2583}
2584EXPORT_SYMBOL(dvb_frontend_resume);
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586int dvb_register_frontend(struct dvb_adapter* dvb,
2587 struct dvb_frontend* fe)
2588{
2589 struct dvb_frontend_private *fepriv;
2590 static const struct dvb_device dvbdev_template = {
2591 .users = ~0,
2592 .writers = 1,
2593 .readers = (~0)-1,
2594 .fops = &dvb_frontend_fops,
2595 .kernel_ioctl = dvb_frontend_ioctl
2596 };
2597
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002598 dev_dbg(dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Ingo Molnar3593cab2006-02-07 06:49:14 -02002600 if (mutex_lock_interruptible(&frontend_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 return -ERESTARTSYS;
2602
Panagiotis Issaris74081872006-01-11 19:40:56 -02002603 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (fe->frontend_priv == NULL) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002605 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return -ENOMEM;
2607 }
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002608 fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Thomas Gleixnera0a47142010-09-07 11:33:27 -03002610 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 init_waitqueue_head (&fepriv->wait_queue);
2612 init_waitqueue_head (&fepriv->events.wait_queue);
Matthias Kaehlcke03b76122007-07-30 14:58:10 -03002613 mutex_init(&fepriv->events.mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 fe->dvb = dvb;
2615 fepriv->inversion = INVERSION_OFF;
2616
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002617 dev_info(fe->dvb->device,
2618 "DVB: registering adapter %i frontend %i (%s)...\n",
2619 fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2622 fe, DVB_DEVICE_FRONTEND);
2623
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002624 /*
2625 * Initialize the cache to the proper values according with the
2626 * first supported delivery system (ops->delsys[0])
2627 */
Mauro Carvalho Chehab240ab502012-01-10 18:00:50 -03002628
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002629 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002630 dvb_frontend_clear_cache(fe);
2631
Ingo Molnar3593cab2006-02-07 06:49:14 -02002632 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return 0;
2634}
2635EXPORT_SYMBOL(dvb_register_frontend);
2636
2637int dvb_unregister_frontend(struct dvb_frontend* fe)
2638{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002639 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002640 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Markus Rechberger57861b42007-04-14 10:19:18 -03002642 mutex_lock(&frontend_mutex);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002643 dvb_frontend_stop (fe);
Markus Rechberger57861b42007-04-14 10:19:18 -03002644 mutex_unlock(&frontend_mutex);
2645
2646 if (fepriv->dvbdev->users < -1)
2647 wait_event(fepriv->dvbdev->wait_queue,
2648 fepriv->dvbdev->users==-1);
2649
Ingo Molnar3593cab2006-02-07 06:49:14 -02002650 mutex_lock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 dvb_unregister_device (fepriv->dvbdev);
Andrew de Quinceyd9955062006-08-08 09:10:08 -03002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 /* fe is invalid now */
2654 kfree(fepriv);
Ingo Molnar3593cab2006-02-07 06:49:14 -02002655 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return 0;
2657}
2658EXPORT_SYMBOL(dvb_unregister_frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002659
Mauro Carvalho Chehab149ef722008-04-29 21:38:46 -03002660#ifdef CONFIG_MEDIA_ATTACH
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002661void dvb_frontend_detach(struct dvb_frontend* fe)
2662{
2663 void *ptr;
2664
2665 if (fe->ops.release_sec) {
2666 fe->ops.release_sec(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002667 dvb_detach(fe->ops.release_sec);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002668 }
2669 if (fe->ops.tuner_ops.release) {
2670 fe->ops.tuner_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002671 dvb_detach(fe->ops.tuner_ops.release);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002672 }
Michael Krufky2426a272007-12-21 11:34:45 -03002673 if (fe->ops.analog_ops.release) {
2674 fe->ops.analog_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002675 dvb_detach(fe->ops.analog_ops.release);
Michael Krufky2426a272007-12-21 11:34:45 -03002676 }
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002677 ptr = (void*)fe->ops.release;
2678 if (ptr) {
2679 fe->ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002680 dvb_detach(ptr);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002681 }
2682}
2683#else
2684void dvb_frontend_detach(struct dvb_frontend* fe)
2685{
2686 if (fe->ops.release_sec)
2687 fe->ops.release_sec(fe);
2688 if (fe->ops.tuner_ops.release)
2689 fe->ops.tuner_ops.release(fe);
Michael Krufky2426a272007-12-21 11:34:45 -03002690 if (fe->ops.analog_ops.release)
2691 fe->ops.analog_ops.release(fe);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002692 if (fe->ops.release)
2693 fe->ops.release(fe);
2694}
2695#endif
2696EXPORT_SYMBOL(dvb_frontend_detach);