blob: 01511e5a5566643a32d03ba64fe314c299860ef6 [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>
Tina Ruchandani9056a232015-05-31 04:17:06 -030043#include <linux/ktime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "dvb_frontend.h"
47#include "dvbdev.h"
Steven Totheacf8d82008-09-26 00:29:49 -030048#include <linux/dvb/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static int dvb_frontend_debug;
Oliver Endriss849be2c2007-08-25 12:22:16 -030051static int dvb_shutdown_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static int dvb_force_auto_inversion;
53static int dvb_override_tune_delay;
54static int dvb_powerdown_on_sleep = 1;
Darron Broad59b18422008-10-11 11:44:05 -030055static int dvb_mfe_wait_time = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
Johannes Stezenbachf4f009a2005-05-16 21:54:21 -070058MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
Manu Abraham6baad3f2006-02-27 00:09:32 -030059module_param(dvb_shutdown_timeout, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
Manu Abraham6baad3f2006-02-27 00:09:32 -030061module_param(dvb_force_auto_inversion, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
Manu Abraham6baad3f2006-02-27 00:09:32 -030063module_param(dvb_override_tune_delay, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064MODULE_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 -030065module_param(dvb_powerdown_on_sleep, int, 0644);
Uwe Buglaa85585772006-04-11 10:21:37 -030066MODULE_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 -030067module_param(dvb_mfe_wait_time, int, 0644);
68MODULE_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 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define FESTATE_IDLE 1
71#define FESTATE_RETUNE 2
72#define FESTATE_TUNING_FAST 4
73#define FESTATE_TUNING_SLOW 8
74#define FESTATE_TUNED 16
75#define FESTATE_ZIGZAG_FAST 32
76#define FESTATE_ZIGZAG_SLOW 64
77#define FESTATE_DISEQC 128
Janne Grunau01886252009-09-01 19:23:09 -030078#define FESTATE_ERROR 256
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
80#define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
81#define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
82#define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
Manu Abraham0249ef12006-06-21 10:27:31 -030083
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 {
Max Kellermannfe356372016-07-04 09:08:51 -0300102 struct kref refcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200104 /* thread/frontend values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct dvb_device *dvbdev;
Andreas Oberrittera5959db2011-05-08 20:03:40 -0300106 struct dvb_frontend_parameters parameters_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 struct dvb_fe_events events;
108 struct semaphore sem;
109 struct list_head list_head;
110 wait_queue_head_t wait_queue;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300111 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 unsigned long release_jiffies;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200113 unsigned int wakeup;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300114 enum fe_status status;
Peter Beutner400b7082006-01-09 15:32:43 -0200115 unsigned long tune_mode_flags;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200116 unsigned int delay;
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300117 unsigned int reinitialise;
Andrew de Quincey64454012006-04-06 14:32:23 -0300118 int tone;
119 int voltage;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200120
121 /* swzigzag values */
122 unsigned int state;
123 unsigned int bending;
124 int lnb_drift;
125 unsigned int inversion;
126 unsigned int auto_step;
127 unsigned int auto_sub_step;
128 unsigned int started_auto_step;
129 unsigned int min_delay;
130 unsigned int max_drift;
131 unsigned int step_size;
132 int quality;
133 unsigned int check_wrapped;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300134 enum dvbfe_search algo_status;
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300135
136#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
137 struct media_pipeline pipe;
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Max Kellermannfe356372016-07-04 09:08:51 -0300141static void dvb_frontend_private_free(struct kref *ref)
142{
143 struct dvb_frontend_private *fepriv =
144 container_of(ref, struct dvb_frontend_private, refcount);
145 kfree(fepriv);
146}
147
148static void dvb_frontend_private_put(struct dvb_frontend_private *fepriv)
149{
150 kref_put(&fepriv->refcount, dvb_frontend_private_free);
151}
152
153static void dvb_frontend_private_get(struct dvb_frontend_private *fepriv)
154{
155 kref_get(&fepriv->refcount);
156}
157
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300158static void dvb_frontend_wakeup(struct dvb_frontend *fe);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300159static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -0200160 struct dtv_frontend_properties *c,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300161 struct dvb_frontend_parameters *p_out);
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -0200162static int
163dtv_property_legacy_params_sync(struct dvb_frontend *fe,
164 const struct dtv_frontend_properties *c,
165 struct dvb_frontend_parameters *p);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300166
167static bool has_get_frontend(struct dvb_frontend *fe)
168{
Hans Petter Selaskyc065f5b2012-03-27 12:53:19 -0300169 return fe->ops.get_frontend != NULL;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300172/*
173 * Due to DVBv3 API calls, a delivery system should be mapped into one of
174 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
175 * otherwise, a DVBv3 call will fail.
176 */
177enum dvbv3_emulation_type {
178 DVBV3_UNKNOWN,
179 DVBV3_QPSK,
180 DVBV3_QAM,
181 DVBV3_OFDM,
182 DVBV3_ATSC,
183};
184
185static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
186{
187 switch (delivery_system) {
188 case SYS_DVBC_ANNEX_A:
189 case SYS_DVBC_ANNEX_C:
190 return DVBV3_QAM;
191 case SYS_DVBS:
192 case SYS_DVBS2:
193 case SYS_TURBO:
194 case SYS_ISDBS:
195 case SYS_DSS:
196 return DVBV3_QPSK;
197 case SYS_DVBT:
198 case SYS_DVBT2:
199 case SYS_ISDBT:
Antti Palosaari224b6642012-08-12 22:33:21 -0300200 case SYS_DTMB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300201 return DVBV3_OFDM;
202 case SYS_ATSC:
Michael Krufkyca689482012-01-29 15:44:58 -0300203 case SYS_ATSCMH:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300204 case SYS_DVBC_ANNEX_B:
205 return DVBV3_ATSC;
206 case SYS_UNDEFINED:
207 case SYS_ISDBC:
208 case SYS_DVBH:
209 case SYS_DAB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300210 default:
211 /*
212 * Doesn't know how to emulate those types and/or
213 * there's no frontend driver from this type yet
214 * with some emulation code, so, we're not sure yet how
215 * to handle them, or they're not compatible with a DVBv3 call.
216 */
217 return DVBV3_UNKNOWN;
218 }
219}
220
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300221static void dvb_frontend_add_event(struct dvb_frontend *fe,
222 enum fe_status status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700224 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -0200225 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 struct dvb_fe_events *events = &fepriv->events;
227 struct dvb_frontend_event *e;
228 int wp;
229
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300230 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300232 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -0200233 dtv_get_frontend(fe, c, &fepriv->parameters_out);
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300234
235 mutex_lock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 wp = (events->eventw + 1) % MAX_EVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (wp == events->eventr) {
239 events->overflow = 1;
240 events->eventr = (events->eventr + 1) % MAX_EVENT;
241 }
242
243 e = &events->events[events->eventw];
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300244 e->status = status;
Andreas Oberrittera5959db2011-05-08 20:03:40 -0300245 e->parameters = fepriv->parameters_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 events->eventw = wp;
248
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300249 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 wake_up_interruptible (&events->wait_queue);
252}
253
254static int dvb_frontend_get_event(struct dvb_frontend *fe,
255 struct dvb_frontend_event *event, int flags)
256{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700257 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct dvb_fe_events *events = &fepriv->events;
259
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300260 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 if (events->overflow) {
263 events->overflow = 0;
264 return -EOVERFLOW;
265 }
266
267 if (events->eventw == events->eventr) {
268 int ret;
269
270 if (flags & O_NONBLOCK)
271 return -EWOULDBLOCK;
272
273 up(&fepriv->sem);
274
275 ret = wait_event_interruptible (events->wait_queue,
276 events->eventw != events->eventr);
277
278 if (down_interruptible (&fepriv->sem))
279 return -ERESTARTSYS;
280
281 if (ret < 0)
282 return ret;
283 }
284
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300285 mutex_lock(&events->mtx);
286 *event = events->events[events->eventr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 events->eventr = (events->eventr + 1) % MAX_EVENT;
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300288 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 return 0;
291}
292
Andreas Oberritter20640be2011-08-04 12:33:14 -0300293static void dvb_frontend_clear_events(struct dvb_frontend *fe)
294{
295 struct dvb_frontend_private *fepriv = fe->frontend_priv;
296 struct dvb_fe_events *events = &fepriv->events;
297
298 mutex_lock(&events->mtx);
299 events->eventr = events->eventw;
300 mutex_unlock(&events->mtx);
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static void dvb_frontend_init(struct dvb_frontend *fe)
304{
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300305 dev_dbg(fe->dvb->device,
306 "%s: initialising adapter %i frontend %i (%s)...\n",
307 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Patrick Boettcherdea74862006-05-14 05:01:31 -0300309 if (fe->ops.init)
310 fe->ops.init(fe);
311 if (fe->ops.tuner_ops.init) {
Manu Abraham3b37a152008-10-20 18:14:14 -0300312 if (fe->ops.i2c_gate_ctrl)
313 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300314 fe->ops.tuner_ops.init(fe);
315 if (fe->ops.i2c_gate_ctrl)
316 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300320void dvb_frontend_reinitialise(struct dvb_frontend *fe)
321{
322 struct dvb_frontend_private *fepriv = fe->frontend_priv;
323
324 fepriv->reinitialise = 1;
325 dvb_frontend_wakeup(fe);
326}
327EXPORT_SYMBOL(dvb_frontend_reinitialise);
328
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200329static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200331 int q2;
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300332 struct dvb_frontend *fe = fepriv->dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300334 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200336 if (locked)
337 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
338 else
339 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200341 q2 = fepriv->quality - 128;
342 q2 *= q2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200344 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/**
348 * Performs automatic twiddling of frontend parameters.
349 *
350 * @param fe The frontend concerned.
351 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
352 * @returns Number of complete iterations that have been performed.
353 */
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200354static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 int autoinversion;
357 int ready = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300358 int fe_set_err = 0;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700359 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300360 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
361 int original_inversion = c->inversion;
362 u32 original_frequency = c->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 /* are we using autoinversion? */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300365 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300366 (c->inversion == INVERSION_AUTO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 /* setup parameters correctly */
369 while(!ready) {
370 /* calculate the lnb_drift */
371 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
372
373 /* wrap the auto_step if we've exceeded the maximum drift */
374 if (fepriv->lnb_drift > fepriv->max_drift) {
375 fepriv->auto_step = 0;
376 fepriv->auto_sub_step = 0;
377 fepriv->lnb_drift = 0;
378 }
379
380 /* perform inversion and +/- zigzag */
381 switch(fepriv->auto_sub_step) {
382 case 0:
383 /* try with the current inversion and current drift setting */
384 ready = 1;
385 break;
386
387 case 1:
388 if (!autoinversion) break;
389
390 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
391 ready = 1;
392 break;
393
394 case 2:
395 if (fepriv->lnb_drift == 0) break;
396
397 fepriv->lnb_drift = -fepriv->lnb_drift;
398 ready = 1;
399 break;
400
401 case 3:
402 if (fepriv->lnb_drift == 0) break;
403 if (!autoinversion) break;
404
405 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
406 fepriv->lnb_drift = -fepriv->lnb_drift;
407 ready = 1;
408 break;
409
410 default:
411 fepriv->auto_step++;
412 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
413 break;
414 }
415
416 if (!ready) fepriv->auto_sub_step++;
417 }
418
419 /* if this attempt would hit where we started, indicate a complete
420 * iteration has occurred */
421 if ((fepriv->auto_step == fepriv->started_auto_step) &&
422 (fepriv->auto_sub_step == 0) && check_wrapped) {
423 return 1;
424 }
425
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300426 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
427 "auto_sub_step:%i started_auto_step:%i\n",
428 __func__, fepriv->lnb_drift, fepriv->inversion,
429 fepriv->auto_step, fepriv->auto_sub_step,
430 fepriv->started_auto_step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* set the frontend itself */
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300433 c->frequency += fepriv->lnb_drift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (autoinversion)
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300435 c->inversion = fepriv->inversion;
436 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300437 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300438 fe_set_err = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300439 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300440 if (fe_set_err < 0) {
441 fepriv->state = FESTATE_ERROR;
442 return fe_set_err;
443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300445 c->frequency = original_frequency;
446 c->inversion = original_inversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 fepriv->auto_sub_step++;
449 return 0;
450}
451
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200452static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
453{
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300454 enum fe_status s = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300455 int retval = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200456 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300457 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200458
459 /* if we've got no parameters, just keep idling */
460 if (fepriv->state & FESTATE_IDLE) {
461 fepriv->delay = 3*HZ;
462 fepriv->quality = 0;
463 return;
464 }
465
466 /* in SCAN mode, we just set the frontend when asked and leave it alone */
467 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
468 if (fepriv->state & FESTATE_RETUNE) {
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300469 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300470 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300471 retval = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300472 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300473 if (retval < 0)
474 fepriv->state = FESTATE_ERROR;
475 else
476 fepriv->state = FESTATE_TUNED;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200477 }
478 fepriv->delay = 3*HZ;
479 fepriv->quality = 0;
480 return;
481 }
482
483 /* get the frontend status */
484 if (fepriv->state & FESTATE_RETUNE) {
485 s = 0;
486 } else {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300487 if (fe->ops.read_status)
488 fe->ops.read_status(fe, &s);
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200489 if (s != fepriv->status) {
490 dvb_frontend_add_event(fe, s);
491 fepriv->status = s;
492 }
493 }
494
495 /* if we're not tuned, and we have a lock, move to the TUNED state */
496 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
497 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
498 fepriv->state = FESTATE_TUNED;
499
500 /* if we're tuned, then we have determined the correct inversion */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300501 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300502 (c->inversion == INVERSION_AUTO)) {
503 c->inversion = fepriv->inversion;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200504 }
505 return;
506 }
507
508 /* if we are tuned already, check we're still locked */
509 if (fepriv->state & FESTATE_TUNED) {
510 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
511
512 /* we're tuned, and the lock is still good... */
513 if (s & FE_HAS_LOCK) {
514 return;
515 } else { /* if we _WERE_ tuned, but now don't have a lock */
516 fepriv->state = FESTATE_ZIGZAG_FAST;
517 fepriv->started_auto_step = fepriv->auto_step;
518 fepriv->check_wrapped = 0;
519 }
520 }
521
522 /* don't actually do anything if we're in the LOSTLOCK state,
523 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
524 if ((fepriv->state & FESTATE_LOSTLOCK) &&
Patrick Boettcherdea74862006-05-14 05:01:31 -0300525 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200526 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
527 return;
528 }
529
530 /* don't do anything if we're in the DISEQC state, since this
531 * might be someone with a motorized dish controlled by DISEQC.
532 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
533 if (fepriv->state & FESTATE_DISEQC) {
534 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
535 return;
536 }
537
538 /* if we're in the RETUNE state, set everything up for a brand
539 * new scan, keeping the current inversion setting, as the next
540 * tune is _very_ likely to require the same */
541 if (fepriv->state & FESTATE_RETUNE) {
542 fepriv->lnb_drift = 0;
543 fepriv->auto_step = 0;
544 fepriv->auto_sub_step = 0;
545 fepriv->started_auto_step = 0;
546 fepriv->check_wrapped = 0;
547 }
548
549 /* fast zigzag. */
550 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
551 fepriv->delay = fepriv->min_delay;
552
Guillaume Audirac2030c032010-05-06 09:30:25 -0300553 /* perform a tune */
Janne Grunau01886252009-09-01 19:23:09 -0300554 retval = dvb_frontend_swzigzag_autotune(fe,
555 fepriv->check_wrapped);
556 if (retval < 0) {
557 return;
558 } else if (retval) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200559 /* OK, if we've run out of trials at the fast speed.
560 * Drop back to slow for the _next_ attempt */
561 fepriv->state = FESTATE_SEARCHING_SLOW;
562 fepriv->started_auto_step = fepriv->auto_step;
563 return;
564 }
565 fepriv->check_wrapped = 1;
566
567 /* if we've just retuned, enter the ZIGZAG_FAST state.
568 * This ensures we cannot return from an
569 * FE_SET_FRONTEND ioctl before the first frontend tune
570 * occurs */
571 if (fepriv->state & FESTATE_RETUNE) {
572 fepriv->state = FESTATE_TUNING_FAST;
573 }
574 }
575
576 /* slow zigzag */
577 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
578 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
579
580 /* Note: don't bother checking for wrapping; we stay in this
581 * state until we get a lock */
582 dvb_frontend_swzigzag_autotune(fe, 0);
583 }
584}
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
587{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700588 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Shuah Khan18ed2862014-07-12 13:44:12 -0300590 if (fe->exit != DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 1;
592
593 if (fepriv->dvbdev->writers == 1)
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -0300594 if (time_after_eq(jiffies, fepriv->release_jiffies +
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200595 dvb_shutdown_timeout * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return 1;
597
598 return 0;
599}
600
601static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
602{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700603 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (fepriv->wakeup) {
606 fepriv->wakeup = 0;
607 return 1;
608 }
609 return dvb_frontend_is_exiting(fe);
610}
611
612static void dvb_frontend_wakeup(struct dvb_frontend *fe)
613{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700614 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 fepriv->wakeup = 1;
617 wake_up_interruptible(&fepriv->wait_queue);
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620static int dvb_frontend_thread(void *data)
621{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700622 struct dvb_frontend *fe = data;
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -0200623 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700624 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300625 enum fe_status s;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300626 enum dvbfe_algo algo;
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300627 bool re_tune = false;
Juergen Lock6ae23222012-12-23 17:23:06 -0300628 bool semheld = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300630 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200632 fepriv->check_wrapped = 0;
633 fepriv->quality = 0;
634 fepriv->delay = 3*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 fepriv->wakeup = 0;
Andrew de Quincey04c56d02006-07-10 03:34:14 -0300637 fepriv->reinitialise = 0;
638
639 dvb_frontend_init(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700641 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 while (1) {
643 up(&fepriv->sem); /* is locked when we enter the thread... */
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300644restart:
Hans Verkuil94238e92011-08-27 11:30:25 -0300645 wait_event_interruptible_timeout(fepriv->wait_queue,
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700646 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
647 || freezing(current),
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300648 fepriv->delay);
649
650 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* got signal or quitting */
Juergen Lock6ae23222012-12-23 17:23:06 -0300652 if (!down_interruptible(&fepriv->sem))
653 semheld = true;
Shuah Khan18ed2862014-07-12 13:44:12 -0300654 fe->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 break;
656 }
657
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300658 if (try_to_freeze())
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300659 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 if (down_interruptible(&fepriv->sem))
662 break;
663
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300664 if (fepriv->reinitialise) {
665 dvb_frontend_init(fe);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300666 if (fe->ops.set_tone && fepriv->tone != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300667 fe->ops.set_tone(fe, fepriv->tone);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300668 if (fe->ops.set_voltage && fepriv->voltage != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300669 fe->ops.set_voltage(fe, fepriv->voltage);
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300670 fepriv->reinitialise = 0;
671 }
672
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200673 /* do an iteration of the tuning loop */
Manu Abrahamd772bd02006-06-24 11:18:58 -0300674 if (fe->ops.get_frontend_algo) {
Manu Abrahamc59e7872008-10-14 16:34:07 -0300675 algo = fe->ops.get_frontend_algo(fe);
676 switch (algo) {
677 case DVBFE_ALGO_HW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300678 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300679
Manu Abrahamd772bd02006-06-24 11:18:58 -0300680 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300681 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300682 re_tune = true;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300683 fepriv->state = FESTATE_TUNED;
Simon Arlott45145b62012-02-06 17:57:01 -0300684 } else {
685 re_tune = false;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Manu Abrahamc59e7872008-10-14 16:34:07 -0300688 if (fe->ops.tune)
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300689 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300690
Darron Broad2fac9a02008-12-18 06:27:50 -0300691 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300692 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
Manu Abrahamd772bd02006-06-24 11:18:58 -0300693 dvb_frontend_add_event(fe, s);
694 fepriv->status = s;
695 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300696 break;
697 case DVBFE_ALGO_SW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300698 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
Manu Abraham70d90632006-06-29 22:05:23 -0300699 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300700 break;
701 case DVBFE_ALGO_CUSTOM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300702 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300703 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300704 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300705 fepriv->state = FESTATE_TUNED;
706 }
707 /* Case where we are going to search for a carrier
708 * User asked us to retune again for some reason, possibly
709 * requesting a search with a new set of parameters
710 */
711 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300712 if (fe->ops.search) {
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -0300713 fepriv->algo_status = fe->ops.search(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300714 /* We did do a search as was requested, the flags are
715 * now unset as well and has the flags wrt to search.
716 */
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300717 } else {
718 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
719 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300720 }
721 /* Track the carrier if the search was successful */
Mauro Carvalho Chehab1b5d8712011-12-26 18:11:25 -0300722 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300723 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
724 fepriv->delay = HZ / 2;
725 }
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -0200726 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300727 fe->ops.read_status(fe, &s);
728 if (s != fepriv->status) {
729 dvb_frontend_add_event(fe, s); /* update event list */
730 fepriv->status = s;
731 if (!(s & FE_HAS_LOCK)) {
732 fepriv->delay = HZ / 10;
733 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
734 } else {
735 fepriv->delay = 60 * HZ;
736 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300737 }
738 break;
739 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300740 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300741 break;
742 }
743 } else {
Manu Abraham4a4edcc2006-06-25 05:46:26 -0300744 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747
Oliver Endriss608f62d2007-08-25 13:17:53 -0300748 if (dvb_powerdown_on_sleep) {
749 if (fe->ops.set_voltage)
750 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300751 if (fe->ops.tuner_ops.sleep) {
Devin Heitmueller41286d92008-11-16 00:44:52 -0300752 if (fe->ops.i2c_gate_ctrl)
753 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300754 fe->ops.tuner_ops.sleep(fe);
755 if (fe->ops.i2c_gate_ctrl)
756 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300757 }
Patrick Boettcherdea74862006-05-14 05:01:31 -0300758 if (fe->ops.sleep)
759 fe->ops.sleep(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300762 fepriv->thread = NULL;
matthieu castete36309f2010-05-05 15:59:20 -0300763 if (kthread_should_stop())
Shuah Khan18ed2862014-07-12 13:44:12 -0300764 fe->exit = DVB_FE_DEVICE_REMOVED;
matthieu castete36309f2010-05-05 15:59:20 -0300765 else
Shuah Khan18ed2862014-07-12 13:44:12 -0300766 fe->exit = DVB_FE_NO_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 mb();
768
Juergen Lock6ae23222012-12-23 17:23:06 -0300769 if (semheld)
770 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 dvb_frontend_wakeup(fe);
772 return 0;
773}
774
775static void dvb_frontend_stop(struct dvb_frontend *fe)
776{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700777 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300779 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Shuah Khan18ed2862014-07-12 13:44:12 -0300781 if (fe->exit != DVB_FE_DEVICE_REMOVED)
782 fe->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 mb();
784
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300785 if (!fepriv->thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return;
787
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300788 kthread_stop(fepriv->thread);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300789
Thomas Gleixnera0a47142010-09-07 11:33:27 -0300790 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 fepriv->state = FESTATE_IDLE;
792
793 /* paranoia check in case a signal arrived */
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300794 if (fepriv->thread)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300795 dev_warn(fe->dvb->device,
796 "dvb_frontend_stop: warning: thread %p won't exit\n",
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300797 fepriv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
NooneImportant83b75b02005-11-08 21:35:27 -0800800/*
Mauro Carvalho Chehab4deea4c2015-11-09 23:24:10 -0200801 * Sleep for the amount of time given by add_usec parameter
802 *
803 * This needs to be as precise as possible, as it affects the detection of
804 * the dish tone command at the satellite subsystem. The precision is improved
805 * by using a scheduled msleep followed by udelay for the remainder.
NooneImportant83b75b02005-11-08 21:35:27 -0800806 */
Tina Ruchandani9056a232015-05-31 04:17:06 -0300807void dvb_frontend_sleep_until(ktime_t *waketime, u32 add_usec)
NooneImportant83b75b02005-11-08 21:35:27 -0800808{
Mauro Carvalho Chehaba733a412015-11-18 12:55:47 -0200809 s32 delta;
NooneImportant83b75b02005-11-08 21:35:27 -0800810
Mauro Carvalho Chehaba733a412015-11-18 12:55:47 -0200811 *waketime = ktime_add_us(*waketime, add_usec);
Abhilash Jindal6b3f9992016-01-31 03:47:31 -0200812 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
NooneImportant83b75b02005-11-08 21:35:27 -0800813 if (delta > 2500) {
814 msleep((delta - 1500) / 1000);
Abhilash Jindal6b3f9992016-01-31 03:47:31 -0200815 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
NooneImportant83b75b02005-11-08 21:35:27 -0800816 }
817 if (delta > 0)
818 udelay(delta);
819}
820EXPORT_SYMBOL(dvb_frontend_sleep_until);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822static int dvb_frontend_start(struct dvb_frontend *fe)
823{
824 int ret;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700825 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300826 struct task_struct *fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300828 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300830 if (fepriv->thread) {
Shuah Khan18ed2862014-07-12 13:44:12 -0300831 if (fe->exit == DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833 else
834 dvb_frontend_stop (fe);
835 }
836
837 if (signal_pending(current))
838 return -EINTR;
839 if (down_interruptible (&fepriv->sem))
840 return -EINTR;
841
842 fepriv->state = FESTATE_IDLE;
Shuah Khan18ed2862014-07-12 13:44:12 -0300843 fe->exit = DVB_FE_NO_EXIT;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300844 fepriv->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 mb();
846
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300847 fe_thread = kthread_run(dvb_frontend_thread, fe,
Steven Toth363c35f2008-10-11 11:05:50 -0300848 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300849 if (IS_ERR(fe_thread)) {
850 ret = PTR_ERR(fe_thread);
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300851 dev_warn(fe->dvb->device,
852 "dvb_frontend_start: failed to start kthread (%d)\n",
853 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 up(&fepriv->sem);
855 return ret;
856 }
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300857 fepriv->thread = fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 0;
859}
860
Guillaume Audirac2030c032010-05-06 09:30:25 -0300861static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
Oliver Endrissc471b332007-08-09 01:03:42 -0300862 u32 *freq_min, u32 *freq_max)
863{
864 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
865
866 if (fe->ops.info.frequency_max == 0)
867 *freq_max = fe->ops.tuner_ops.info.frequency_max;
868 else if (fe->ops.tuner_ops.info.frequency_max == 0)
869 *freq_max = fe->ops.info.frequency_max;
870 else
871 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
872
873 if (*freq_min == 0 || *freq_max == 0)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300874 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
875 fe->dvb->num, fe->id);
Oliver Endrissc471b332007-08-09 01:03:42 -0300876}
877
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300878static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300879{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300880 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Oliver Endrissc471b332007-08-09 01:03:42 -0300881 u32 freq_min;
882 u32 freq_max;
883
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300884 /* range check: frequency */
Guillaume Audirac2030c032010-05-06 09:30:25 -0300885 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300886 if ((freq_min && c->frequency < freq_min) ||
887 (freq_max && c->frequency > freq_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300888 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
889 fe->dvb->num, fe->id, c->frequency,
890 freq_min, freq_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300891 return -EINVAL;
892 }
893
894 /* range check: symbol rate */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300895 switch (c->delivery_system) {
896 case SYS_DVBS:
897 case SYS_DVBS2:
898 case SYS_TURBO:
899 case SYS_DVBC_ANNEX_A:
900 case SYS_DVBC_ANNEX_C:
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300901 if ((fe->ops.info.symbol_rate_min &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300902 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300903 (fe->ops.info.symbol_rate_max &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300904 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300905 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
906 fe->dvb->num, fe->id, c->symbol_rate,
907 fe->ops.info.symbol_rate_min,
908 fe->ops.info.symbol_rate_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300909 return -EINVAL;
910 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300911 default:
912 break;
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300913 }
914
915 return 0;
916}
917
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300918static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
919{
Andreas Oberritter50727712011-05-08 20:03:39 -0300920 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300921 int i;
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200922 u32 delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300923
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200924 delsys = c->delivery_system;
Mauro Carvalho Chehab808d24d2013-03-20 10:39:31 -0300925 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200926 c->delivery_system = delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300927
Andreas Oberritter50727712011-05-08 20:03:39 -0300928 c->state = DTV_CLEAR;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300929
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300930 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
931 __func__, c->delivery_system);
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300932
Andreas Oberritter50727712011-05-08 20:03:39 -0300933 c->transmission_mode = TRANSMISSION_MODE_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300934 c->bandwidth_hz = 0; /* AUTO */
Andreas Oberritter50727712011-05-08 20:03:39 -0300935 c->guard_interval = GUARD_INTERVAL_AUTO;
936 c->hierarchy = HIERARCHY_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300937 c->symbol_rate = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -0300938 c->code_rate_HP = FEC_AUTO;
939 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300940 c->fec_inner = FEC_AUTO;
Mauro Carvalho Chehab39ce61a2011-11-11 12:46:23 -0200941 c->rolloff = ROLLOFF_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300942 c->voltage = SEC_VOLTAGE_OFF;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300943 c->sectone = SEC_TONE_OFF;
944 c->pilot = PILOT_AUTO;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300945
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300946 c->isdbt_partial_reception = 0;
947 c->isdbt_sb_mode = 0;
948 c->isdbt_sb_subchannel = 0;
949 c->isdbt_sb_segment_idx = 0;
950 c->isdbt_sb_segment_count = 0;
951 c->isdbt_layer_enabled = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300952 for (i = 0; i < 3; i++) {
Andreas Oberritter50727712011-05-08 20:03:39 -0300953 c->layer[i].fec = FEC_AUTO;
954 c->layer[i].modulation = QAM_AUTO;
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300955 c->layer[i].interleaving = 0;
956 c->layer[i].segment_count = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300957 }
958
Evgeny Plehov287cefd2012-09-13 10:13:30 -0300959 c->stream_id = NO_STREAM_ID_FILTER;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300960
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300961 switch (c->delivery_system) {
962 case SYS_DVBS:
963 case SYS_DVBS2:
964 case SYS_TURBO:
965 c->modulation = QPSK; /* implied for DVB-S in legacy API */
966 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
967 break;
968 case SYS_ATSC:
969 c->modulation = VSB_8;
970 break;
Akihiro Tsukada8e281fa2014-10-31 10:19:39 -0300971 case SYS_ISDBS:
972 c->symbol_rate = 28860000;
973 c->rolloff = ROLLOFF_35;
974 c->bandwidth_hz = c->symbol_rate / 100 * 135;
975 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300976 default:
977 c->modulation = QAM_AUTO;
978 break;
979 }
980
Antti Palosaari33eebec2012-10-03 04:28:56 -0300981 c->lna = LNA_AUTO;
982
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300983 return 0;
984}
985
986#define _DTV_CMD(n, s, b) \
987[n] = { \
988 .name = #n, \
989 .cmd = n, \
990 .set = s,\
991 .buffer = b \
992}
993
Andreas Oberritterec05a642011-05-26 07:54:14 -0300994static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300995 _DTV_CMD(DTV_TUNE, 1, 0),
996 _DTV_CMD(DTV_CLEAR, 1, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -0300997
998 /* Set */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300999 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1000 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1001 _DTV_CMD(DTV_MODULATION, 1, 0),
1002 _DTV_CMD(DTV_INVERSION, 1, 0),
1003 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1004 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1005 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1006 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1007 _DTV_CMD(DTV_TONE, 1, 0),
1008 _DTV_CMD(DTV_PILOT, 1, 0),
1009 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1010 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1011 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1012 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1013 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1014 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1015 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
Antti Palosaari224b6642012-08-12 22:33:21 -03001016 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001017
1018 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1019 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1020 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1021 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1022 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
Patrick Boettchere7b79492009-08-14 05:24:19 -03001023 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001024 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1025 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1026 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1027 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1028 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1029 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1030 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1031 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1032 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1033 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1034 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1035 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1036
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001037 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1038 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
Antti Palosaari8a2697a2012-07-11 21:54:50 -03001039 _DTV_CMD(DTV_LNA, 1, 0),
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001040
Steven Toth6b73eea2008-09-04 01:12:25 -03001041 /* Get */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001042 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1043 _DTV_CMD(DTV_API_VERSION, 0, 0),
Manu Abrahamba2780c2011-11-13 18:47:44 -03001044
1045 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001046
1047 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1048 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1049
1050 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001051 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1052 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1053 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1054 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1055 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001056 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1057 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1058 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1059 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1060 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1061 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1062 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001063
1064 /* Statistics API */
1065 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1066 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1067 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1068 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1069 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1070 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1071 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1072 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -03001073};
1074
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001075static void dtv_property_dump(struct dvb_frontend *fe,
1076 bool is_set,
1077 struct dtv_property *tvp)
Steven Toth6b73eea2008-09-04 01:12:25 -03001078{
1079 int i;
1080
Mauro Carvalho Chehab106ff9e2011-07-15 08:49:06 -03001081 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001082 dev_warn(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x undefined\n",
1083 __func__,
1084 is_set ? "SET" : "GET",
1085 tvp->cmd);
Darron Broad4aae8ef2008-10-03 11:50:00 -03001086 return;
1087 }
1088
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001089 dev_dbg(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x (%s)\n", __func__,
1090 is_set ? "SET" : "GET",
1091 tvp->cmd,
1092 dtv_cmds[tvp->cmd].name);
Steven Toth6b73eea2008-09-04 01:12:25 -03001093
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001094 if (dtv_cmds[tvp->cmd].buffer) {
1095 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1096 __func__, tvp->u.buffer.len);
Steven Toth6b73eea2008-09-04 01:12:25 -03001097
1098 for(i = 0; i < tvp->u.buffer.len; i++)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001099 dev_dbg(fe->dvb->device,
1100 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1101 __func__, i, tvp->u.buffer.data[i]);
1102 } else {
1103 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1104 tvp->u.data);
1105 }
Steven Toth6b73eea2008-09-04 01:12:25 -03001106}
1107
Steven Tothee33c522008-09-11 23:52:32 -03001108/* Synchronise the legacy tuning parameters into the cache, so that demodulator
1109 * drivers can use a single set_frontend tuning function, regardless of whether
1110 * it's being used for the legacy or new API, reducing code and complexity.
1111 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001112static int dtv_property_cache_sync(struct dvb_frontend *fe,
1113 struct dtv_frontend_properties *c,
1114 const struct dvb_frontend_parameters *p)
Steven Tothee33c522008-09-11 23:52:32 -03001115{
Steven Tothee33c522008-09-11 23:52:32 -03001116 c->frequency = p->frequency;
1117 c->inversion = p->inversion;
1118
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001119 switch (dvbv3_type(c->delivery_system)) {
1120 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001121 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001122 c->symbol_rate = p->u.qpsk.symbol_rate;
1123 c->fec_inner = p->u.qpsk.fec_inner;
1124 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001125 case DVBV3_QAM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001126 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001127 c->symbol_rate = p->u.qam.symbol_rate;
1128 c->fec_inner = p->u.qam.fec_inner;
1129 c->modulation = p->u.qam.modulation;
1130 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001131 case DVBV3_OFDM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001132 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1133
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001134 switch (p->u.ofdm.bandwidth) {
1135 case BANDWIDTH_10_MHZ:
1136 c->bandwidth_hz = 10000000;
1137 break;
1138 case BANDWIDTH_8_MHZ:
Steven Toth75b7f942008-09-13 16:56:34 -03001139 c->bandwidth_hz = 8000000;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001140 break;
1141 case BANDWIDTH_7_MHZ:
1142 c->bandwidth_hz = 7000000;
1143 break;
1144 case BANDWIDTH_6_MHZ:
1145 c->bandwidth_hz = 6000000;
1146 break;
1147 case BANDWIDTH_5_MHZ:
1148 c->bandwidth_hz = 5000000;
1149 break;
1150 case BANDWIDTH_1_712_MHZ:
1151 c->bandwidth_hz = 1712000;
1152 break;
1153 case BANDWIDTH_AUTO:
Steven Toth75b7f942008-09-13 16:56:34 -03001154 c->bandwidth_hz = 0;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001155 }
1156
Steven Tothee33c522008-09-11 23:52:32 -03001157 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1158 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1159 c->modulation = p->u.ofdm.constellation;
1160 c->transmission_mode = p->u.ofdm.transmission_mode;
1161 c->guard_interval = p->u.ofdm.guard_interval;
1162 c->hierarchy = p->u.ofdm.hierarchy_information;
1163 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001164 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001165 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001166 c->modulation = p->u.vsb.modulation;
Michael Krufkyca689482012-01-29 15:44:58 -03001167 if (c->delivery_system == SYS_ATSCMH)
1168 break;
Steven Toth80a773c2008-09-12 00:53:50 -03001169 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1170 c->delivery_system = SYS_ATSC;
1171 else
1172 c->delivery_system = SYS_DVBC_ANNEX_B;
Steven Tothee33c522008-09-11 23:52:32 -03001173 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001174 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001175 dev_err(fe->dvb->device,
1176 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1177 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001178 return -EINVAL;
Steven Tothee33c522008-09-11 23:52:32 -03001179 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001180
1181 return 0;
Steven Tothee33c522008-09-11 23:52:32 -03001182}
1183
Steven Tothd5748f12008-09-12 00:27:13 -03001184/* Ensure the cached values are set correctly in the frontend
1185 * legacy tuning structures, for the advanced tuning API.
1186 */
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -02001187static int
1188dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1189 const struct dtv_frontend_properties *c,
1190 struct dvb_frontend_parameters *p)
Steven Toth6b73eea2008-09-04 01:12:25 -03001191{
Steven Tothd5748f12008-09-12 00:27:13 -03001192 p->frequency = c->frequency;
1193 p->inversion = c->inversion;
1194
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001195 switch (dvbv3_type(c->delivery_system)) {
1196 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001197 dev_err(fe->dvb->device,
1198 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1199 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001200 return -EINVAL;
1201 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001202 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001203 p->u.qpsk.symbol_rate = c->symbol_rate;
1204 p->u.qpsk.fec_inner = c->fec_inner;
1205 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001206 case DVBV3_QAM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001207 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001208 p->u.qam.symbol_rate = c->symbol_rate;
1209 p->u.qam.fec_inner = c->fec_inner;
1210 p->u.qam.modulation = c->modulation;
1211 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001212 case DVBV3_OFDM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001213 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001214 switch (c->bandwidth_hz) {
1215 case 10000000:
1216 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1217 break;
1218 case 8000000:
Steven Toth75b7f942008-09-13 16:56:34 -03001219 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001220 break;
1221 case 7000000:
1222 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1223 break;
1224 case 6000000:
1225 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1226 break;
1227 case 5000000:
1228 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1229 break;
1230 case 1712000:
1231 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1232 break;
1233 case 0:
1234 default:
Steven Toth75b7f942008-09-13 16:56:34 -03001235 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001236 }
Steven Tothd5748f12008-09-12 00:27:13 -03001237 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1238 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1239 p->u.ofdm.constellation = c->modulation;
1240 p->u.ofdm.transmission_mode = c->transmission_mode;
1241 p->u.ofdm.guard_interval = c->guard_interval;
1242 p->u.ofdm.hierarchy_information = c->hierarchy;
1243 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001244 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001245 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001246 p->u.vsb.modulation = c->modulation;
1247 break;
1248 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001249 return 0;
Steven Toth6b73eea2008-09-04 01:12:25 -03001250}
1251
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001252/**
1253 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1254 * @fe: struct dvb_frontend pointer
1255 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1256 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1257 *
1258 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1259 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1260 * If p_out is not null, it will update the DVBv3 params pointed by it.
1261 */
1262static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -02001263 struct dtv_frontend_properties *c,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001264 struct dvb_frontend_parameters *p_out)
1265{
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001266 int r;
1267
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001268 if (fe->ops.get_frontend) {
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02001269 r = fe->ops.get_frontend(fe, c);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001270 if (unlikely(r < 0))
1271 return r;
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001272 if (p_out)
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -02001273 dtv_property_legacy_params_sync(fe, c, p_out);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001274 return 0;
1275 }
1276
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001277 /* As everything is in cache, get_frontend fops are always supported */
Mauro Carvalho Chehabb1e9a65012011-12-26 16:43:32 -03001278 return 0;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001279}
1280
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001281static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001282 unsigned int cmd, void *parg);
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001283static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001284 unsigned int cmd, void *parg);
1285
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001286static int dtv_property_process_get(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001287 const struct dtv_frontend_properties *c,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001288 struct dtv_property *tvp,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001289 struct file *file)
Steven Toth363429a2008-09-12 01:34:28 -03001290{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001291 int r, ncaps;
Steven Tothbfbf2da2008-09-12 01:37:37 -03001292
Steven Toth363429a2008-09-12 01:34:28 -03001293 switch(tvp->cmd) {
Manu Abrahamba2780c2011-11-13 18:47:44 -03001294 case DTV_ENUM_DELSYS:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001295 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001296 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001297 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1298 ncaps++;
1299 }
1300 tvp->u.buffer.len = ncaps;
Manu Abrahamba2780c2011-11-13 18:47:44 -03001301 break;
Steven Toth363429a2008-09-12 01:34:28 -03001302 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001303 tvp->u.data = c->frequency;
Steven Toth363429a2008-09-12 01:34:28 -03001304 break;
1305 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001306 tvp->u.data = c->modulation;
Steven Toth363429a2008-09-12 01:34:28 -03001307 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001308 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001309 tvp->u.data = c->bandwidth_hz;
Steven Toth363429a2008-09-12 01:34:28 -03001310 break;
1311 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001312 tvp->u.data = c->inversion;
Steven Toth363429a2008-09-12 01:34:28 -03001313 break;
1314 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001315 tvp->u.data = c->symbol_rate;
Steven Toth363429a2008-09-12 01:34:28 -03001316 break;
1317 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001318 tvp->u.data = c->fec_inner;
Steven Toth363429a2008-09-12 01:34:28 -03001319 break;
1320 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001321 tvp->u.data = c->pilot;
Steven Toth363429a2008-09-12 01:34:28 -03001322 break;
1323 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001324 tvp->u.data = c->rolloff;
Steven Toth363429a2008-09-12 01:34:28 -03001325 break;
1326 case DTV_DELIVERY_SYSTEM:
Andreas Oberritter50727712011-05-08 20:03:39 -03001327 tvp->u.data = c->delivery_system;
Steven Toth363429a2008-09-12 01:34:28 -03001328 break;
Steven Toth363429a2008-09-12 01:34:28 -03001329 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001330 tvp->u.data = c->voltage;
Steven Toth363429a2008-09-12 01:34:28 -03001331 break;
1332 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001333 tvp->u.data = c->sectone;
Steven Toth363429a2008-09-12 01:34:28 -03001334 break;
Steven Totheacf8d82008-09-26 00:29:49 -03001335 case DTV_API_VERSION:
1336 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1337 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001338 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001339 tvp->u.data = c->code_rate_HP;
Steven Totha4de91b2008-10-06 21:55:46 -03001340 break;
1341 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001342 tvp->u.data = c->code_rate_LP;
Steven Totha4de91b2008-10-06 21:55:46 -03001343 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001344 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001345 tvp->u.data = c->guard_interval;
Steven Tothb87625f2008-10-06 21:56:59 -03001346 break;
1347 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001348 tvp->u.data = c->transmission_mode;
Steven Tothb87625f2008-10-06 21:56:59 -03001349 break;
Steven Tothef526f42008-10-06 22:01:47 -03001350 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001351 tvp->u.data = c->hierarchy;
Steven Tothef526f42008-10-06 22:01:47 -03001352 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001353 case DTV_INTERLEAVING:
1354 tvp->u.data = c->interleaving;
1355 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001356
1357 /* ISDB-T Support here */
1358 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001359 tvp->u.data = c->isdbt_partial_reception;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001360 break;
1361 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001362 tvp->u.data = c->isdbt_sb_mode;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001363 break;
1364 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001365 tvp->u.data = c->isdbt_sb_subchannel;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001366 break;
1367 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001368 tvp->u.data = c->isdbt_sb_segment_idx;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001369 break;
1370 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001371 tvp->u.data = c->isdbt_sb_segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001372 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001373 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001374 tvp->u.data = c->isdbt_layer_enabled;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001375 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001376 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001377 tvp->u.data = c->layer[0].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001378 break;
1379 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001380 tvp->u.data = c->layer[0].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001381 break;
1382 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001383 tvp->u.data = c->layer[0].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001384 break;
1385 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001386 tvp->u.data = c->layer[0].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001387 break;
1388 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001389 tvp->u.data = c->layer[1].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001390 break;
1391 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001392 tvp->u.data = c->layer[1].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001393 break;
1394 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001395 tvp->u.data = c->layer[1].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001396 break;
1397 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001398 tvp->u.data = c->layer[1].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001399 break;
1400 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001401 tvp->u.data = c->layer[2].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001402 break;
1403 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001404 tvp->u.data = c->layer[2].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001405 break;
1406 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001407 tvp->u.data = c->layer[2].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001408 break;
1409 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001410 tvp->u.data = c->layer[2].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001411 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001412
1413 /* Multistream support */
1414 case DTV_STREAM_ID:
1415 case DTV_DVBT2_PLP_ID_LEGACY:
1416 tvp->u.data = c->stream_id;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001417 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001418
1419 /* ATSC-MH */
1420 case DTV_ATSCMH_FIC_VER:
1421 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1422 break;
1423 case DTV_ATSCMH_PARADE_ID:
1424 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1425 break;
1426 case DTV_ATSCMH_NOG:
1427 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1428 break;
1429 case DTV_ATSCMH_TNOG:
1430 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1431 break;
1432 case DTV_ATSCMH_SGN:
1433 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1434 break;
1435 case DTV_ATSCMH_PRC:
1436 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1437 break;
1438 case DTV_ATSCMH_RS_FRAME_MODE:
1439 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1440 break;
1441 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1442 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1443 break;
1444 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1445 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1446 break;
1447 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1448 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1449 break;
1450 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1451 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1452 break;
1453 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1454 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1455 break;
1456 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1457 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1458 break;
1459 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1460 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1461 break;
1462 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1463 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1464 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001465
Antti Palosaari33eebec2012-10-03 04:28:56 -03001466 case DTV_LNA:
1467 tvp->u.data = c->lna;
1468 break;
1469
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001470 /* Fill quality measures */
1471 case DTV_STAT_SIGNAL_STRENGTH:
1472 tvp->u.st = c->strength;
1473 break;
1474 case DTV_STAT_CNR:
1475 tvp->u.st = c->cnr;
1476 break;
1477 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1478 tvp->u.st = c->pre_bit_error;
1479 break;
1480 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1481 tvp->u.st = c->pre_bit_count;
1482 break;
1483 case DTV_STAT_POST_ERROR_BIT_COUNT:
1484 tvp->u.st = c->post_bit_error;
1485 break;
1486 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1487 tvp->u.st = c->post_bit_count;
1488 break;
1489 case DTV_STAT_ERROR_BLOCK_COUNT:
1490 tvp->u.st = c->block_error;
1491 break;
1492 case DTV_STAT_TOTAL_BLOCK_COUNT:
1493 tvp->u.st = c->block_count;
1494 break;
Steven Toth363429a2008-09-12 01:34:28 -03001495 default:
Mauro Carvalho Chehab94a93e52013-01-23 17:06:02 -03001496 dev_dbg(fe->dvb->device,
1497 "%s: FE property %d doesn't exist\n",
1498 __func__, tvp->cmd);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001499 return -EINVAL;
Steven Toth363429a2008-09-12 01:34:28 -03001500 }
1501
Andreas Oberrittere23d9ae2011-05-08 20:03:36 -03001502 /* Allow the frontend to override outgoing properties */
1503 if (fe->ops.get_property) {
1504 r = fe->ops.get_property(fe, tvp);
1505 if (r < 0)
1506 return r;
1507 }
1508
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001509 dtv_property_dump(fe, false, tvp);
Mauro Carvalho Chehab639544d2009-12-31 17:49:32 -03001510
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001511 return 0;
Steven Toth363429a2008-09-12 01:34:28 -03001512}
1513
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001514static int dtv_set_frontend(struct dvb_frontend *fe);
1515
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001516static bool is_dvbv3_delsys(u32 delsys)
1517{
1518 bool status;
1519
1520 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1521 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1522
1523 return status;
1524}
1525
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001526/**
1527 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1528 * @fe: struct frontend;
1529 * @delsys: DVBv5 type that will be used for emulation
1530 *
1531 * Provides emulation for delivery systems that are compatible with the old
1532 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1533 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1534 * parameters are compatible with DVB-S spec.
1535 */
1536static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001537{
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001538 int i;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001539 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001540
Mauro Carvalho Chehab36264792012-04-17 18:32:19 -03001541 c->delivery_system = delsys;
1542
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001543 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001544 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001545 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001546 if (c->delivery_system == SYS_ISDBT) {
1547 dev_dbg(fe->dvb->device,
1548 "%s: Using defaults for SYS_ISDBT\n",
1549 __func__);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001550
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001551 if (!c->bandwidth_hz)
1552 c->bandwidth_hz = 6000000;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001553
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001554 c->isdbt_partial_reception = 0;
1555 c->isdbt_sb_mode = 0;
1556 c->isdbt_sb_subchannel = 0;
1557 c->isdbt_sb_segment_idx = 0;
1558 c->isdbt_sb_segment_count = 0;
1559 c->isdbt_layer_enabled = 7;
1560 for (i = 0; i < 3; i++) {
1561 c->layer[i].fec = FEC_AUTO;
1562 c->layer[i].modulation = QAM_AUTO;
1563 c->layer[i].interleaving = 0;
1564 c->layer[i].segment_count = 0;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001565 }
1566 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001567 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001568 __func__, c->delivery_system);
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001569
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001570 return 0;
1571}
1572
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001573/**
1574 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1575 * @fe: frontend struct
1576 * @desired_system: delivery system requested by the user
1577 *
1578 * A DVBv5 call know what's the desired system it wants. So, set it.
1579 *
1580 * There are, however, a few known issues with early DVBv5 applications that
1581 * are also handled by this logic:
1582 *
1583 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1584 * This is an API violation, but, as we don't want to break userspace,
1585 * convert it to the first supported delivery system.
1586 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1587 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1588 * ISDB-T provided backward compat with DVB-T.
1589 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001590static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1591 u32 desired_system)
1592{
1593 int ncaps;
1594 u32 delsys = SYS_UNDEFINED;
1595 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1596 enum dvbv3_emulation_type type;
1597
1598 /*
1599 * It was reported that some old DVBv5 applications were
1600 * filling delivery_system with SYS_UNDEFINED. If this happens,
1601 * assume that the application wants to use the first supported
1602 * delivery system.
1603 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001604 if (desired_system == SYS_UNDEFINED)
1605 desired_system = fe->ops.delsys[0];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001606
1607 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001608 * This is a DVBv5 call. So, it likely knows the supported
1609 * delivery systems. So, check if the desired delivery system is
1610 * supported
1611 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001612 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001613 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001614 if (fe->ops.delsys[ncaps] == desired_system) {
1615 c->delivery_system = desired_system;
1616 dev_dbg(fe->dvb->device,
1617 "%s: Changing delivery system to %d\n",
1618 __func__, desired_system);
1619 return 0;
1620 }
1621 ncaps++;
1622 }
1623
1624 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001625 * The requested delivery system isn't supported. Maybe userspace
1626 * is requesting a DVBv3 compatible delivery system.
1627 *
1628 * The emulation only works if the desired system is one of the
1629 * delivery systems supported by DVBv3 API
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001630 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001631 if (!is_dvbv3_delsys(desired_system)) {
1632 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001633 "%s: Delivery system %d not supported.\n",
1634 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001635 return -EINVAL;
1636 }
1637
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001638 type = dvbv3_type(desired_system);
1639
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001640 /*
1641 * Get the last non-DVBv3 delivery system that has the same type
1642 * of the desired system
1643 */
1644 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001645 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001646 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001647 delsys = fe->ops.delsys[ncaps];
1648 ncaps++;
1649 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001650
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001651 /* There's nothing compatible with the desired delivery system */
1652 if (delsys == SYS_UNDEFINED) {
1653 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001654 "%s: Delivery system %d not supported on emulation mode.\n",
1655 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001656 return -EINVAL;
1657 }
1658
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001659 dev_dbg(fe->dvb->device,
1660 "%s: Using delivery system %d emulated as if it were %d\n",
1661 __func__, delsys, desired_system);
1662
1663 return emulate_delivery_system(fe, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001664}
1665
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001666/**
1667 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1668 * @fe: frontend struct
1669 *
1670 * A DVBv3 call doesn't know what's the desired system it wants. It also
1671 * doesn't allow to switch between different types. Due to that, userspace
1672 * should use DVBv5 instead.
1673 * However, in order to avoid breaking userspace API, limited backward
1674 * compatibility support is provided.
1675 *
1676 * There are some delivery systems that are incompatible with DVBv3 calls.
1677 *
1678 * This routine should work fine for frontends that support just one delivery
1679 * system.
1680 *
1681 * For frontends that support multiple frontends:
1682 * 1) It defaults to use the first supported delivery system. There's an
1683 * userspace application that allows changing it at runtime;
1684 *
1685 * 2) If the current delivery system is not compatible with DVBv3, it gets
1686 * the first one that it is compatible.
1687 *
1688 * NOTE: in order for this to work with applications like Kaffeine that
1689 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1690 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1691 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1692 * to DVB-S.
1693 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001694static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1695{
1696 int ncaps;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001697 u32 delsys = SYS_UNDEFINED;
1698 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001699
1700 /* If not set yet, defaults to the first supported delivery system */
1701 if (c->delivery_system == SYS_UNDEFINED)
1702 c->delivery_system = fe->ops.delsys[0];
1703
1704 /*
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001705 * Trivial case: just use the current one, if it already a DVBv3
1706 * delivery system
1707 */
1708 if (is_dvbv3_delsys(c->delivery_system)) {
1709 dev_dbg(fe->dvb->device,
1710 "%s: Using delivery system to %d\n",
1711 __func__, c->delivery_system);
1712 return 0;
1713 }
1714
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001715 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001716 * Seek for the first delivery system that it is compatible with a
1717 * DVBv3 standard
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001718 */
1719 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001720 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001721 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1722 delsys = fe->ops.delsys[ncaps];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001723 break;
1724 }
1725 ncaps++;
1726 }
1727 if (delsys == SYS_UNDEFINED) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001728 dev_dbg(fe->dvb->device,
1729 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1730 __func__);
1731 return -EINVAL;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001732 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001733 return emulate_delivery_system(fe, delsys);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001734}
1735
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001736static int dtv_property_process_set(struct dvb_frontend *fe,
1737 struct dtv_property *tvp,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001738 struct file *file)
Steven Toth6b73eea2008-09-04 01:12:25 -03001739{
1740 int r = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -03001741 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001742
Steven Tothbfbf2da2008-09-12 01:37:37 -03001743 /* Allow the frontend to validate incoming properties */
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001744 if (fe->ops.set_property) {
Steven Tothbfbf2da2008-09-12 01:37:37 -03001745 r = fe->ops.set_property(fe, tvp);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001746 if (r < 0)
1747 return r;
1748 }
Steven Tothbfbf2da2008-09-12 01:37:37 -03001749
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001750 dtv_property_dump(fe, true, tvp);
1751
Steven Toth6b73eea2008-09-04 01:12:25 -03001752 switch(tvp->cmd) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001753 case DTV_CLEAR:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001754 /*
1755 * Reset a cache of data specific to the frontend here. This does
Steven Toth6b73eea2008-09-04 01:12:25 -03001756 * not effect hardware.
1757 */
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001758 dvb_frontend_clear_cache(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001759 break;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001760 case DTV_TUNE:
Steven Toth6b73eea2008-09-04 01:12:25 -03001761 /* interpret the cache of data, build either a traditional frontend
Steven Tothd5748f12008-09-12 00:27:13 -03001762 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1763 * ioctl.
Steven Toth6b73eea2008-09-04 01:12:25 -03001764 */
Andreas Oberritter50727712011-05-08 20:03:39 -03001765 c->state = tvp->cmd;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001766 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1767 __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001768
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001769 r = dtv_set_frontend(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001770 break;
Steven Toth363429a2008-09-12 01:34:28 -03001771 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001772 c->frequency = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001773 break;
Steven Toth363429a2008-09-12 01:34:28 -03001774 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001775 c->modulation = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001776 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001777 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001778 c->bandwidth_hz = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001779 break;
Steven Toth363429a2008-09-12 01:34:28 -03001780 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001781 c->inversion = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001782 break;
Steven Toth363429a2008-09-12 01:34:28 -03001783 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001784 c->symbol_rate = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001785 break;
Steven Toth363429a2008-09-12 01:34:28 -03001786 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001787 c->fec_inner = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001788 break;
Steven Toth363429a2008-09-12 01:34:28 -03001789 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001790 c->pilot = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001791 break;
Steven Toth363429a2008-09-12 01:34:28 -03001792 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001793 c->rolloff = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001794 break;
Steven Toth363429a2008-09-12 01:34:28 -03001795 case DTV_DELIVERY_SYSTEM:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001796 r = dvbv5_set_delivery_system(fe, tvp->u.data);
Steven Toth6b73eea2008-09-04 01:12:25 -03001797 break;
Steven Toth363429a2008-09-12 01:34:28 -03001798 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001799 c->voltage = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001800 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001801 (void *)c->voltage);
Steven Toth13c97bf2008-09-04 21:19:43 -03001802 break;
Steven Toth363429a2008-09-12 01:34:28 -03001803 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001804 c->sectone = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001805 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001806 (void *)c->sectone);
Steven Toth13c97bf2008-09-04 21:19:43 -03001807 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001808 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001809 c->code_rate_HP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001810 break;
1811 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001812 c->code_rate_LP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001813 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001814 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001815 c->guard_interval = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001816 break;
1817 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001818 c->transmission_mode = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001819 break;
Steven Tothef526f42008-10-06 22:01:47 -03001820 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001821 c->hierarchy = tvp->u.data;
Steven Tothef526f42008-10-06 22:01:47 -03001822 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001823 case DTV_INTERLEAVING:
1824 c->interleaving = tvp->u.data;
1825 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001826
1827 /* ISDB-T Support here */
1828 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001829 c->isdbt_partial_reception = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001830 break;
1831 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001832 c->isdbt_sb_mode = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001833 break;
1834 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001835 c->isdbt_sb_subchannel = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001836 break;
1837 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001838 c->isdbt_sb_segment_idx = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001839 break;
1840 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001841 c->isdbt_sb_segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001842 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001843 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001844 c->isdbt_layer_enabled = tvp->u.data;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001845 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001846 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001847 c->layer[0].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001848 break;
1849 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001850 c->layer[0].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001851 break;
1852 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001853 c->layer[0].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001854 break;
1855 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001856 c->layer[0].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001857 break;
1858 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001859 c->layer[1].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001860 break;
1861 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001862 c->layer[1].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001863 break;
1864 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001865 c->layer[1].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001866 break;
1867 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001868 c->layer[1].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001869 break;
1870 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001871 c->layer[2].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001872 break;
1873 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001874 c->layer[2].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001875 break;
1876 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001877 c->layer[2].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001878 break;
1879 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001880 c->layer[2].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001881 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001882
1883 /* Multistream support */
1884 case DTV_STREAM_ID:
1885 case DTV_DVBT2_PLP_ID_LEGACY:
1886 c->stream_id = tvp->u.data;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001887 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001888
1889 /* ATSC-MH */
1890 case DTV_ATSCMH_PARADE_ID:
1891 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1892 break;
1893 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1894 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1895 break;
1896
Antti Palosaari33eebec2012-10-03 04:28:56 -03001897 case DTV_LNA:
1898 c->lna = tvp->u.data;
1899 if (fe->ops.set_lna)
1900 r = fe->ops.set_lna(fe);
Mauro Carvalho Chehabe6876692014-02-16 06:36:17 -03001901 if (r < 0)
1902 c->lna = LNA_AUTO;
Antti Palosaari33eebec2012-10-03 04:28:56 -03001903 break;
1904
Steven Toth363429a2008-09-12 01:34:28 -03001905 default:
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001906 return -EINVAL;
Steven Toth6b73eea2008-09-04 01:12:25 -03001907 }
1908
Steven Toth13c97bf2008-09-04 21:19:43 -03001909 return r;
Steven Toth6b73eea2008-09-04 01:12:25 -03001910}
1911
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001912static int dvb_frontend_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 unsigned int cmd, void *parg)
1914{
1915 struct dvb_device *dvbdev = file->private_data;
1916 struct dvb_frontend *fe = dvbdev->priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001917 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001918 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02001919 int err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001921 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
Juergen Lock6ae23222012-12-23 17:23:06 -03001922 if (down_interruptible(&fepriv->sem))
1923 return -ERESTARTSYS;
1924
Shuah Khan18ed2862014-07-12 13:44:12 -03001925 if (fe->exit != DVB_FE_NO_EXIT) {
Juergen Lock6ae23222012-12-23 17:23:06 -03001926 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return -ENODEV;
Juergen Lock6ae23222012-12-23 17:23:06 -03001928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1931 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
Juergen Lock6ae23222012-12-23 17:23:06 -03001932 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
1933 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return -EPERM;
Juergen Lock6ae23222012-12-23 17:23:06 -03001935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Steven Toth13c97bf2008-09-04 21:19:43 -03001937 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001938 err = dvb_frontend_ioctl_properties(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001939 else {
Andreas Oberritter50727712011-05-08 20:03:39 -03001940 c->state = DTV_UNDEFINED;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001941 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001942 }
Steven Toth13c97bf2008-09-04 21:19:43 -03001943
1944 up(&fepriv->sem);
1945 return err;
1946}
1947
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001948static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001949 unsigned int cmd, void *parg)
1950{
1951 struct dvb_device *dvbdev = file->private_data;
1952 struct dvb_frontend *fe = dvbdev->priv;
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02001953 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001954 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Igor M. Liplianine6f9ec82008-09-12 14:49:06 -03001955 int err = 0;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001956
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03001957 struct dtv_properties *tvps = parg;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001958 struct dtv_property *tvp = NULL;
1959 int i;
Steven Toth13c97bf2008-09-04 21:19:43 -03001960
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001961 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Steven Toth13c97bf2008-09-04 21:19:43 -03001962
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03001963 if (cmd == FE_SET_PROPERTY) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001964 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1965 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001966
1967 /* Put an arbitrary limit on the number of messages that can
1968 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001969 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Tothe7fee0f32008-09-11 10:23:01 -03001970 return -EINVAL;
1971
Markus Elfringfb660922016-08-19 05:04:54 -03001972 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
1973 if (IS_ERR(tvp))
1974 return PTR_ERR(tvp);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001975
Steven Tothd48cb402008-09-26 00:16:25 -03001976 for (i = 0; i < tvps->num; i++) {
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001977 err = dtv_property_process_set(fe, tvp + i, file);
1978 if (err < 0)
1979 goto out;
1980 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03001981 }
Steven Tothe7fee0f32008-09-11 10:23:01 -03001982
Andreas Oberritter50727712011-05-08 20:03:39 -03001983 if (c->state == DTV_TUNE)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001984 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
Steven Tothbfbf2da2008-09-12 01:37:37 -03001985
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03001986 } else if (cmd == FE_GET_PROPERTY) {
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02001987 struct dtv_frontend_properties getp = fe->dtv_property_cache;
1988
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001989 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1990 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Toth363429a2008-09-12 01:34:28 -03001991
1992 /* Put an arbitrary limit on the number of messages that can
1993 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001994 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Toth363429a2008-09-12 01:34:28 -03001995 return -EINVAL;
1996
Markus Elfringfb660922016-08-19 05:04:54 -03001997 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
1998 if (IS_ERR(tvp))
1999 return PTR_ERR(tvp);
Steven Toth363429a2008-09-12 01:34:28 -03002000
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03002001 /*
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002002 * Let's use our own copy of property cache, in order to
2003 * avoid mangling with DTV zigzag logic, as drivers might
2004 * return crap, if they don't check if the data is available
2005 * before updating the properties cache.
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03002006 */
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002007 if (fepriv->state != FESTATE_IDLE) {
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002008 err = dtv_get_frontend(fe, &getp, NULL);
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002009 if (err < 0)
2010 goto out;
2011 }
Steven Tothd48cb402008-09-26 00:16:25 -03002012 for (i = 0; i < tvps->num; i++) {
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002013 err = dtv_property_process_get(fe, &getp, tvp + i, file);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01002014 if (err < 0)
2015 goto out;
2016 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03002017 }
Steven Toth363429a2008-09-12 01:34:28 -03002018
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03002019 if (copy_to_user((void __user *)tvps->props, tvp,
2020 tvps->num * sizeof(struct dtv_property))) {
Steven Toth363429a2008-09-12 01:34:28 -03002021 err = -EFAULT;
2022 goto out;
2023 }
2024
Steven Toth363429a2008-09-12 01:34:28 -03002025 } else
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002026 err = -EOPNOTSUPP;
Steven Toth363429a2008-09-12 01:34:28 -03002027
Steven Tothe7fee0f32008-09-11 10:23:01 -03002028out:
2029 kfree(tvp);
Steven Toth13c97bf2008-09-04 21:19:43 -03002030 return err;
2031}
2032
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002033static int dtv_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002034{
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002035 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2036 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2037 struct dvb_frontend_tune_settings fetunesettings;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002038 u32 rolloff = 0;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002039
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002040 if (dvb_frontend_check_parameters(fe) < 0)
2041 return -EINVAL;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002042
2043 /*
Chris Rankin556a0442012-04-06 18:38:18 -03002044 * Initialize output parameters to match the values given by
2045 * the user. FE_SET_FRONTEND triggers an initial frontend event
2046 * with status = 0, which copies output parameters to userspace.
2047 */
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -02002048 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
Chris Rankin556a0442012-04-06 18:38:18 -03002049
2050 /*
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002051 * Be sure that the bandwidth will be filled for all
2052 * non-satellite systems, as tuners need to know what
2053 * low pass/Nyquist half filter should be applied, in
2054 * order to avoid inter-channel noise.
2055 *
2056 * ISDB-T and DVB-T/T2 already sets bandwidth.
2057 * ATSC and DVB-C don't set, so, the core should fill it.
2058 *
2059 * On DVB-C Annex A and C, the bandwidth is a function of
2060 * the roll-off and symbol rate. Annex B defines different
2061 * roll-off factors depending on the modulation. Fortunately,
2062 * Annex B is only used with 6MHz, so there's no need to
2063 * calculate it.
2064 *
2065 * While not officially supported, a side effect of handling it at
2066 * the cache level is that a program could retrieve the bandwidth
2067 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2068 */
2069 switch (c->delivery_system) {
2070 case SYS_ATSC:
2071 case SYS_DVBC_ANNEX_B:
2072 c->bandwidth_hz = 6000000;
2073 break;
2074 case SYS_DVBC_ANNEX_A:
2075 rolloff = 115;
2076 break;
2077 case SYS_DVBC_ANNEX_C:
2078 rolloff = 113;
2079 break;
Mauro Carvalho Chehabc623ed62014-08-22 10:30:17 -05002080 case SYS_DVBS:
2081 case SYS_TURBO:
Akihiro Tsukada8e281fa2014-10-31 10:19:39 -03002082 case SYS_ISDBS:
Mauro Carvalho Chehabc623ed62014-08-22 10:30:17 -05002083 rolloff = 135;
2084 break;
2085 case SYS_DVBS2:
2086 switch (c->rolloff) {
2087 case ROLLOFF_20:
2088 rolloff = 120;
2089 break;
2090 case ROLLOFF_25:
2091 rolloff = 125;
2092 break;
2093 default:
2094 case ROLLOFF_35:
2095 rolloff = 135;
2096 }
2097 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002098 default:
2099 break;
2100 }
2101 if (rolloff)
Antti Palosaari2a80f292015-04-14 15:12:54 -03002102 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100);
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002103
2104 /* force auto frequency inversion if requested */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002105 if (dvb_force_auto_inversion)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002106 c->inversion = INVERSION_AUTO;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002107
2108 /*
2109 * without hierarchical coding code_rate_LP is irrelevant,
2110 * so we tolerate the otherwise invalid FEC_NONE setting
2111 */
2112 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2113 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002114
2115 /* get frontend-specific tuning settings */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002116 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002117 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2118 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2119 fepriv->max_drift = fetunesettings.max_drift;
2120 fepriv->step_size = fetunesettings.step_size;
2121 } else {
2122 /* default values */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002123 switch (c->delivery_system) {
Mauro Carvalho Chehab910a5f22012-05-07 15:01:37 -03002124 case SYS_DVBS:
2125 case SYS_DVBS2:
2126 case SYS_ISDBS:
2127 case SYS_TURBO:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002128 case SYS_DVBC_ANNEX_A:
2129 case SYS_DVBC_ANNEX_C:
2130 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002131 fepriv->step_size = c->symbol_rate / 16000;
2132 fepriv->max_drift = c->symbol_rate / 2000;
2133 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002134 case SYS_DVBT:
2135 case SYS_DVBT2:
2136 case SYS_ISDBT:
Antti Palosaari224b6642012-08-12 22:33:21 -03002137 case SYS_DTMB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002138 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002139 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2140 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2141 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002142 default:
2143 /*
2144 * FIXME: This sounds wrong! if freqency_stepsize is
2145 * defined by the frontend, why not use it???
2146 */
2147 fepriv->min_delay = HZ / 20;
2148 fepriv->step_size = 0; /* no zigzag */
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002149 fepriv->max_drift = 0;
2150 break;
2151 }
2152 }
2153 if (dvb_override_tune_delay > 0)
2154 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2155
2156 fepriv->state = FESTATE_RETUNE;
2157
2158 /* Request the search algorithm to search */
2159 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2160
2161 dvb_frontend_clear_events(fe);
2162 dvb_frontend_add_event(fe, 0);
2163 dvb_frontend_wakeup(fe);
2164 fepriv->status = 0;
2165
2166 return 0;
2167}
2168
2169
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002170static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03002171 unsigned int cmd, void *parg)
2172{
2173 struct dvb_device *dvbdev = file->private_data;
2174 struct dvb_frontend *fe = dvbdev->priv;
2175 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002176 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002177 int err = -EOPNOTSUPP;
Steven Toth13c97bf2008-09-04 21:19:43 -03002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 switch (cmd) {
2180 case FE_GET_INFO: {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002181 struct dvb_frontend_info* info = parg;
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002182
Patrick Boettcherdea74862006-05-14 05:01:31 -03002183 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
Guillaume Audirac2030c032010-05-06 09:30:25 -03002184 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002186 /*
2187 * Associate the 4 delivery systems supported by DVBv3
2188 * API with their DVBv5 counterpart. For the other standards,
2189 * use the closest type, assuming that it would hopefully
2190 * work with a DVBv3 application.
2191 * It should be noticed that, on multi-frontend devices with
2192 * different types (terrestrial and cable, for example),
2193 * a pure DVBv3 application won't be able to use all delivery
2194 * systems. Yet, changing the DVBv5 cache to the other delivery
2195 * system should be enough for making it work.
2196 */
2197 switch (dvbv3_type(c->delivery_system)) {
2198 case DVBV3_QPSK:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002199 info->type = FE_QPSK;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002200 break;
2201 case DVBV3_ATSC:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002202 info->type = FE_ATSC;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002203 break;
2204 case DVBV3_QAM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002205 info->type = FE_QAM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002206 break;
2207 case DVBV3_OFDM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002208 info->type = FE_OFDM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002209 break;
2210 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002211 dev_err(fe->dvb->device,
2212 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2213 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002214 fe->ops.info.type = FE_OFDM;
2215 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002216 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2217 __func__, c->delivery_system, fe->ops.info.type);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002218
Malcolm Priestleyc9d57de2015-08-31 06:13:45 -03002219 /* Set CAN_INVERSION_AUTO bit on in other than oneshot mode */
2220 if (!(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT))
2221 info->caps |= FE_CAN_INVERSION_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 err = 0;
2223 break;
2224 }
2225
Peter Beutner6757ccc2005-07-07 17:57:36 -07002226 case FE_READ_STATUS: {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002227 enum fe_status *status = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002229 /* if retune was requested but hasn't occurred yet, prevent
Peter Beutner6757ccc2005-07-07 17:57:36 -07002230 * that user get signal state from previous tuning */
Janne Grunau01886252009-09-01 19:23:09 -03002231 if (fepriv->state == FESTATE_RETUNE ||
2232 fepriv->state == FESTATE_ERROR) {
Peter Beutner6757ccc2005-07-07 17:57:36 -07002233 err=0;
2234 *status = 0;
2235 break;
2236 }
2237
Patrick Boettcherdea74862006-05-14 05:01:31 -03002238 if (fe->ops.read_status)
2239 err = fe->ops.read_status(fe, status);
Peter Beutner6757ccc2005-07-07 17:57:36 -07002240 break;
2241 }
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 case FE_READ_BER:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002244 if (fe->ops.read_ber) {
2245 if (fepriv->thread)
2246 err = fe->ops.read_ber(fe, (__u32 *) parg);
2247 else
2248 err = -EAGAIN;
2249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 break;
2251
2252 case FE_READ_SIGNAL_STRENGTH:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002253 if (fe->ops.read_signal_strength) {
2254 if (fepriv->thread)
2255 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2256 else
2257 err = -EAGAIN;
2258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 break;
2260
2261 case FE_READ_SNR:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002262 if (fe->ops.read_snr) {
2263 if (fepriv->thread)
2264 err = fe->ops.read_snr(fe, (__u16 *) parg);
2265 else
2266 err = -EAGAIN;
2267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 break;
2269
2270 case FE_READ_UNCORRECTED_BLOCKS:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002271 if (fe->ops.read_ucblocks) {
2272 if (fepriv->thread)
2273 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2274 else
2275 err = -EAGAIN;
2276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 break;
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 case FE_DISEQC_RESET_OVERLOAD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002280 if (fe->ops.diseqc_reset_overload) {
2281 err = fe->ops.diseqc_reset_overload(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 fepriv->state = FESTATE_DISEQC;
2283 fepriv->status = 0;
2284 }
2285 break;
2286
2287 case FE_DISEQC_SEND_MASTER_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002288 if (fe->ops.diseqc_send_master_cmd) {
Dan Carpenter8d7e5062015-06-06 13:55:22 -03002289 struct dvb_diseqc_master_cmd *cmd = parg;
2290
2291 if (cmd->msg_len > sizeof(cmd->msg)) {
2292 err = -EINVAL;
2293 break;
2294 }
2295 err = fe->ops.diseqc_send_master_cmd(fe, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 fepriv->state = FESTATE_DISEQC;
2297 fepriv->status = 0;
2298 }
2299 break;
2300
2301 case FE_DISEQC_SEND_BURST:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002302 if (fe->ops.diseqc_send_burst) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002303 err = fe->ops.diseqc_send_burst(fe,
2304 (enum fe_sec_mini_cmd)parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 fepriv->state = FESTATE_DISEQC;
2306 fepriv->status = 0;
2307 }
2308 break;
2309
2310 case FE_SET_TONE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002311 if (fe->ops.set_tone) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002312 err = fe->ops.set_tone(fe,
2313 (enum fe_sec_tone_mode)parg);
2314 fepriv->tone = (enum fe_sec_tone_mode)parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 fepriv->state = FESTATE_DISEQC;
2316 fepriv->status = 0;
2317 }
2318 break;
2319
2320 case FE_SET_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002321 if (fe->ops.set_voltage) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002322 err = fe->ops.set_voltage(fe,
2323 (enum fe_sec_voltage)parg);
2324 fepriv->voltage = (enum fe_sec_voltage)parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 fepriv->state = FESTATE_DISEQC;
2326 fepriv->status = 0;
2327 }
2328 break;
2329
2330 case FE_DISHNETWORK_SEND_LEGACY_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002331 if (fe->ops.dishnetwork_send_legacy_command) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002332 err = fe->ops.dishnetwork_send_legacy_command(fe,
2333 (unsigned long)parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 fepriv->state = FESTATE_DISEQC;
2335 fepriv->status = 0;
Patrick Boettcherdea74862006-05-14 05:01:31 -03002336 } else if (fe->ops.set_voltage) {
NooneImportant83b75b02005-11-08 21:35:27 -08002337 /*
2338 * NOTE: This is a fallback condition. Some frontends
2339 * (stv0299 for instance) take longer than 8msec to
2340 * respond to a set_voltage command. Those switches
2341 * need custom routines to switch properly. For all
Guillaume Audirac2030c032010-05-06 09:30:25 -03002342 * other frontends, the following should work ok.
NooneImportant83b75b02005-11-08 21:35:27 -08002343 * Dish network legacy switches (as used by Dish500)
2344 * are controlled by sending 9-bit command words
2345 * spaced 8msec apart.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002346 * the actual command word is switch/port dependent
NooneImportant83b75b02005-11-08 21:35:27 -08002347 * so it is up to the userspace application to send
2348 * the right command.
2349 * The command must always start with a '0' after
2350 * initialization, so parg is 8 bits and does not
2351 * include the initialization or start bit
2352 */
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002353 unsigned long swcmd = ((unsigned long) parg) << 1;
Tina Ruchandani9056a232015-05-31 04:17:06 -03002354 ktime_t nexttime;
2355 ktime_t tv[10];
NooneImportant83b75b02005-11-08 21:35:27 -08002356 int i;
2357 u8 last = 1;
2358 if (dvb_frontend_debug)
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002359 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
Abhilash Jindal6b3f9992016-01-31 03:47:31 -02002360 nexttime = ktime_get_boottime();
NooneImportant83b75b02005-11-08 21:35:27 -08002361 if (dvb_frontend_debug)
Ezequiel Garciab9b1b3a2012-10-23 15:57:23 -03002362 tv[0] = nexttime;
NooneImportant83b75b02005-11-08 21:35:27 -08002363 /* before sending a command, initialize by sending
2364 * a 32ms 18V to the switch
2365 */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002366 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002367 dvb_frontend_sleep_until(&nexttime, 32000);
2368
2369 for (i = 0; i < 9; i++) {
2370 if (dvb_frontend_debug)
Abhilash Jindal6b3f9992016-01-31 03:47:31 -02002371 tv[i+1] = ktime_get_boottime();
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002372 if ((swcmd & 0x01) != last) {
NooneImportant83b75b02005-11-08 21:35:27 -08002373 /* set voltage to (last ? 13V : 18V) */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002374 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002375 last = (last) ? 0 : 1;
2376 }
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002377 swcmd = swcmd >> 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002378 if (i != 8)
2379 dvb_frontend_sleep_until(&nexttime, 8000);
2380 }
2381 if (dvb_frontend_debug) {
2382 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002383 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -08002384 for (i = 1; i < 10; i++)
Tina Ruchandani9056a232015-05-31 04:17:06 -03002385 printk("%d: %d\n", i,
2386 (int) ktime_us_delta(tv[i], tv[i-1]));
NooneImportant83b75b02005-11-08 21:35:27 -08002387 }
2388 err = 0;
2389 fepriv->state = FESTATE_DISEQC;
2390 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392 break;
2393
2394 case FE_DISEQC_RECV_SLAVE_REPLY:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002395 if (fe->ops.diseqc_recv_slave_reply)
2396 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 break;
2398
2399 case FE_ENABLE_HIGH_LNB_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002400 if (fe->ops.enable_high_lnb_voltage)
2401 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 break;
2403
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002404 case FE_SET_FRONTEND:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002405 err = dvbv3_set_delivery_system(fe);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03002406 if (err)
2407 break;
2408
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002409 err = dtv_property_cache_sync(fe, c, parg);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002410 if (err)
2411 break;
2412 err = dtv_set_frontend(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 case FE_GET_EVENT:
2415 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2416 break;
2417
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002418 case FE_GET_FRONTEND: {
2419 struct dtv_frontend_properties getp = fe->dtv_property_cache;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002420
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002421 /*
2422 * Let's use our own copy of property cache, in order to
2423 * avoid mangling with DTV zigzag logic, as drivers might
2424 * return crap, if they don't check if the data is available
2425 * before updating the properties cache.
2426 */
2427 err = dtv_get_frontend(fe, &getp, parg);
2428 break;
2429 }
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002430 case FE_SET_FRONTEND_TUNE_MODE:
Hans Verkuile18828e2006-01-09 15:25:28 -02002431 fepriv->tune_mode_flags = (unsigned long) parg;
Trent Piepho1b172e02006-06-29 13:16:04 -03002432 err = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002433 break;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03002434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return err;
2437}
2438
Steven Toth6b73eea2008-09-04 01:12:25 -03002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2441{
2442 struct dvb_device *dvbdev = file->private_data;
2443 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002444 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002446 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 poll_wait (file, &fepriv->events.wait_queue, wait);
2449
2450 if (fepriv->events.eventw != fepriv->events.eventr)
2451 return (POLLIN | POLLRDNORM | POLLPRI);
2452
2453 return 0;
2454}
2455
2456static int dvb_frontend_open(struct inode *inode, struct file *file)
2457{
2458 struct dvb_device *dvbdev = file->private_data;
2459 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002460 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Darron Broad59b18422008-10-11 11:44:05 -03002461 struct dvb_adapter *adapter = fe->dvb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 int ret;
2463
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002464 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Shuah Khan18ed2862014-07-12 13:44:12 -03002465 if (fe->exit == DVB_FE_DEVICE_REMOVED)
matthieu castete36309f2010-05-05 15:59:20 -03002466 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Darron Broad59b18422008-10-11 11:44:05 -03002468 if (adapter->mfe_shared) {
2469 mutex_lock (&adapter->mfe_lock);
Darron Broad65946902008-10-15 13:37:59 -03002470
2471 if (adapter->mfe_dvbdev == NULL)
2472 adapter->mfe_dvbdev = dvbdev;
2473
2474 else if (adapter->mfe_dvbdev != dvbdev) {
2475 struct dvb_device
2476 *mfedev = adapter->mfe_dvbdev;
2477 struct dvb_frontend
2478 *mfe = mfedev->priv;
2479 struct dvb_frontend_private
2480 *mfepriv = mfe->frontend_priv;
2481 int mferetry = (dvb_mfe_wait_time << 1);
2482
2483 mutex_unlock (&adapter->mfe_lock);
2484 while (mferetry-- && (mfedev->users != -1 ||
2485 mfepriv->thread != NULL)) {
2486 if(msleep_interruptible(500)) {
2487 if(signal_pending(current))
2488 return -EINTR;
Darron Broad59b18422008-10-11 11:44:05 -03002489 }
2490 }
Darron Broad65946902008-10-15 13:37:59 -03002491
2492 mutex_lock (&adapter->mfe_lock);
2493 if(adapter->mfe_dvbdev != dvbdev) {
2494 mfedev = adapter->mfe_dvbdev;
2495 mfe = mfedev->priv;
2496 mfepriv = mfe->frontend_priv;
2497 if (mfedev->users != -1 ||
2498 mfepriv->thread != NULL) {
2499 mutex_unlock (&adapter->mfe_lock);
2500 return -EBUSY;
2501 }
2502 adapter->mfe_dvbdev = dvbdev;
2503 }
Darron Broad59b18422008-10-11 11:44:05 -03002504 }
2505 }
2506
Oliver Endriss48136e12007-08-25 12:00:23 -03002507 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2508 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
Darron Broad59b18422008-10-11 11:44:05 -03002509 goto err0;
Devin Heitmueller2d196932011-07-04 21:55:01 -03002510
2511 /* If we took control of the bus, we need to force
2512 reinitialization. This is because many ts_bus_ctrl()
2513 functions strobe the RESET pin on the demod, and if the
2514 frontend thread already exists then the dvb_init() routine
2515 won't get called (which is what usually does initial
2516 register configuration). */
2517 fepriv->reinitialise = 1;
Steven Tothba7e6f32006-09-25 12:41:53 -03002518 }
2519
Oliver Endriss48136e12007-08-25 12:00:23 -03002520 if ((ret = dvb_generic_open (inode, file)) < 0)
2521 goto err1;
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002522
Oliver Endriss48136e12007-08-25 12:00:23 -03002523 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002524 /* normal tune mode when opened R/W */
2525 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2526 fepriv->tone = -1;
2527 fepriv->voltage = -1;
2528
Shuah Khan68ff5762016-02-11 21:41:36 -02002529#ifdef CONFIG_MEDIA_CONTROLLER_DVB
2530 if (fe->dvb->mdev && fe->dvb->mdev->enable_source) {
2531 ret = fe->dvb->mdev->enable_source(dvbdev->entity,
2532 &fepriv->pipe);
2533 if (ret) {
2534 dev_err(fe->dvb->device,
2535 "Tuner is busy. Error %d\n", ret);
2536 goto err2;
2537 }
2538 }
2539#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 ret = dvb_frontend_start (fe);
2541 if (ret)
Shuah Khan68ff5762016-02-11 21:41:36 -02002542 goto err3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 /* empty event queue */
2545 fepriv->events.eventr = fepriv->events.eventw = 0;
2546 }
2547
Max Kellermannfe356372016-07-04 09:08:51 -03002548 dvb_frontend_private_get(fepriv);
2549
Darron Broad59b18422008-10-11 11:44:05 -03002550 if (adapter->mfe_shared)
2551 mutex_unlock (&adapter->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 return ret;
Oliver Endriss48136e12007-08-25 12:00:23 -03002553
Shuah Khan68ff5762016-02-11 21:41:36 -02002554err3:
2555#ifdef CONFIG_MEDIA_CONTROLLER_DVB
2556 if (fe->dvb->mdev && fe->dvb->mdev->disable_source)
2557 fe->dvb->mdev->disable_source(dvbdev->entity);
Oliver Endriss48136e12007-08-25 12:00:23 -03002558err2:
Shuah Khan68ff5762016-02-11 21:41:36 -02002559#endif
Oliver Endriss48136e12007-08-25 12:00:23 -03002560 dvb_generic_release(inode, file);
2561err1:
2562 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2563 fe->ops.ts_bus_ctrl(fe, 0);
Darron Broad59b18422008-10-11 11:44:05 -03002564err0:
2565 if (adapter->mfe_shared)
2566 mutex_unlock (&adapter->mfe_lock);
Oliver Endriss48136e12007-08-25 12:00:23 -03002567 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570static int dvb_frontend_release(struct inode *inode, struct file *file)
2571{
2572 struct dvb_device *dvbdev = file->private_data;
2573 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002574 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002575 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002577 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002579 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 fepriv->release_jiffies = jiffies;
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002581 mb();
2582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002584 ret = dvb_generic_release (inode, file);
2585
Oliver Endriss48136e12007-08-25 12:00:23 -03002586 if (dvbdev->users == -1) {
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002587 wake_up(&fepriv->wait_queue);
Shuah Khan68ff5762016-02-11 21:41:36 -02002588#ifdef CONFIG_MEDIA_CONTROLLER_DVB
2589 if (fe->dvb->mdev && fe->dvb->mdev->disable_source)
2590 fe->dvb->mdev->disable_source(dvbdev->entity);
2591#endif
Shuah Khan18ed2862014-07-12 13:44:12 -03002592 if (fe->exit != DVB_FE_NO_EXIT)
Oliver Endriss48136e12007-08-25 12:00:23 -03002593 wake_up(&dvbdev->wait_queue);
Oliver Endriss48136e12007-08-25 12:00:23 -03002594 if (fe->ops.ts_bus_ctrl)
2595 fe->ops.ts_bus_ctrl(fe, 0);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002596 }
Oliver Endriss48136e12007-08-25 12:00:23 -03002597
Max Kellermannfe356372016-07-04 09:08:51 -03002598 dvb_frontend_private_put(fepriv);
2599
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002600 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601}
2602
Jan Engelhardt784e29d2009-01-11 06:12:43 -03002603static const struct file_operations dvb_frontend_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 .owner = THIS_MODULE,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002605 .unlocked_ioctl = dvb_generic_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 .poll = dvb_frontend_poll,
2607 .open = dvb_frontend_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002608 .release = dvb_frontend_release,
2609 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610};
2611
Antti Palosaari06bae122012-08-14 22:21:06 -03002612int dvb_frontend_suspend(struct dvb_frontend *fe)
2613{
2614 int ret = 0;
2615
2616 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2617 fe->id);
2618
Mauro Carvalho Chehab59d78892014-08-09 21:47:19 -03002619 if (fe->ops.tuner_ops.suspend)
2620 ret = fe->ops.tuner_ops.suspend(fe);
2621 else if (fe->ops.tuner_ops.sleep)
Antti Palosaari06bae122012-08-14 22:21:06 -03002622 ret = fe->ops.tuner_ops.sleep(fe);
2623
2624 if (fe->ops.sleep)
2625 ret = fe->ops.sleep(fe);
2626
2627 return ret;
2628}
2629EXPORT_SYMBOL(dvb_frontend_suspend);
2630
2631int dvb_frontend_resume(struct dvb_frontend *fe)
2632{
2633 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2634 int ret = 0;
2635
2636 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2637 fe->id);
2638
Shuah Khan87cd0fa2014-07-24 13:02:14 -03002639 fe->exit = DVB_FE_DEVICE_RESUME;
Antti Palosaari06bae122012-08-14 22:21:06 -03002640 if (fe->ops.init)
2641 ret = fe->ops.init(fe);
2642
Mauro Carvalho Chehab59d78892014-08-09 21:47:19 -03002643 if (fe->ops.tuner_ops.resume)
2644 ret = fe->ops.tuner_ops.resume(fe);
2645 else if (fe->ops.tuner_ops.init)
Antti Palosaari06bae122012-08-14 22:21:06 -03002646 ret = fe->ops.tuner_ops.init(fe);
2647
Mauro Carvalho Chehab3663b312015-11-10 11:50:30 -02002648 if (fe->ops.set_tone && fepriv->tone != -1)
2649 fe->ops.set_tone(fe, fepriv->tone);
2650 if (fe->ops.set_voltage && fepriv->voltage != -1)
2651 fe->ops.set_voltage(fe, fepriv->voltage);
2652
Shuah Khan87cd0fa2014-07-24 13:02:14 -03002653 fe->exit = DVB_FE_NO_EXIT;
Antti Palosaari06bae122012-08-14 22:21:06 -03002654 fepriv->state = FESTATE_RETUNE;
2655 dvb_frontend_wakeup(fe);
2656
2657 return ret;
2658}
2659EXPORT_SYMBOL(dvb_frontend_resume);
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661int dvb_register_frontend(struct dvb_adapter* dvb,
2662 struct dvb_frontend* fe)
2663{
2664 struct dvb_frontend_private *fepriv;
Mauro Carvalho Chehabc1b31b12015-01-02 11:00:17 -03002665 const struct dvb_device dvbdev_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 .users = ~0,
2667 .writers = 1,
2668 .readers = (~0)-1,
2669 .fops = &dvb_frontend_fops,
Mauro Carvalho Chehabc1b31b12015-01-02 11:00:17 -03002670#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2671 .name = fe->ops.info.name,
2672#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 .kernel_ioctl = dvb_frontend_ioctl
2674 };
2675
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002676 dev_dbg(dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Ingo Molnar3593cab2006-02-07 06:49:14 -02002678 if (mutex_lock_interruptible(&frontend_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return -ERESTARTSYS;
2680
Panagiotis Issaris74081872006-01-11 19:40:56 -02002681 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if (fe->frontend_priv == NULL) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002683 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return -ENOMEM;
2685 }
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002686 fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Max Kellermannfe356372016-07-04 09:08:51 -03002688 kref_init(&fepriv->refcount);
2689
Thomas Gleixnera0a47142010-09-07 11:33:27 -03002690 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 init_waitqueue_head (&fepriv->wait_queue);
2692 init_waitqueue_head (&fepriv->events.wait_queue);
Matthias Kaehlcke03b76122007-07-30 14:58:10 -03002693 mutex_init(&fepriv->events.mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 fe->dvb = dvb;
2695 fepriv->inversion = INVERSION_OFF;
2696
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002697 dev_info(fe->dvb->device,
2698 "DVB: registering adapter %i frontend %i (%s)...\n",
2699 fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -03002702 fe, DVB_DEVICE_FRONTEND, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002704 /*
2705 * Initialize the cache to the proper values according with the
2706 * first supported delivery system (ops->delsys[0])
2707 */
Mauro Carvalho Chehab240ab502012-01-10 18:00:50 -03002708
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002709 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002710 dvb_frontend_clear_cache(fe);
2711
Ingo Molnar3593cab2006-02-07 06:49:14 -02002712 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return 0;
2714}
2715EXPORT_SYMBOL(dvb_register_frontend);
2716
2717int dvb_unregister_frontend(struct dvb_frontend* fe)
2718{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002719 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002720 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Markus Rechberger57861b42007-04-14 10:19:18 -03002722 mutex_lock(&frontend_mutex);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002723 dvb_frontend_stop (fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 dvb_unregister_device (fepriv->dvbdev);
Andrew de Quinceyd9955062006-08-08 09:10:08 -03002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /* fe is invalid now */
Ingo Molnar3593cab2006-02-07 06:49:14 -02002727 mutex_unlock(&frontend_mutex);
Max Kellermannfe356372016-07-04 09:08:51 -03002728 dvb_frontend_private_put(fepriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 return 0;
2730}
2731EXPORT_SYMBOL(dvb_unregister_frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002732
Mauro Carvalho Chehab149ef722008-04-29 21:38:46 -03002733#ifdef CONFIG_MEDIA_ATTACH
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002734void dvb_frontend_detach(struct dvb_frontend* fe)
2735{
2736 void *ptr;
2737
2738 if (fe->ops.release_sec) {
2739 fe->ops.release_sec(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002740 dvb_detach(fe->ops.release_sec);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002741 }
2742 if (fe->ops.tuner_ops.release) {
2743 fe->ops.tuner_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002744 dvb_detach(fe->ops.tuner_ops.release);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002745 }
Michael Krufky2426a272007-12-21 11:34:45 -03002746 if (fe->ops.analog_ops.release) {
2747 fe->ops.analog_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002748 dvb_detach(fe->ops.analog_ops.release);
Michael Krufky2426a272007-12-21 11:34:45 -03002749 }
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002750 ptr = (void*)fe->ops.release;
2751 if (ptr) {
2752 fe->ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002753 dvb_detach(ptr);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002754 }
2755}
2756#else
2757void dvb_frontend_detach(struct dvb_frontend* fe)
2758{
2759 if (fe->ops.release_sec)
2760 fe->ops.release_sec(fe);
2761 if (fe->ops.tuner_ops.release)
2762 fe->ops.tuner_ops.release(fe);
Michael Krufky2426a272007-12-21 11:34:45 -03002763 if (fe->ops.analog_ops.release)
2764 fe->ops.analog_ops.release(fe);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002765 if (fe->ops.release)
2766 fe->ops.release(fe);
2767}
2768#endif
2769EXPORT_SYMBOL(dvb_frontend_detach);