blob: 2f054db8807be89152c54980c79b8b6d0e4c6245 [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
Mauro Carvalho Chehabdc00f082018-04-05 05:30:52 -0400254static int dvb_frontend_test_event(struct dvb_frontend_private *fepriv,
255 struct dvb_fe_events *events)
256{
257 int ret;
258
259 up(&fepriv->sem);
260 ret = events->eventw != events->eventr;
261 down(&fepriv->sem);
262
263 return ret;
264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266static int dvb_frontend_get_event(struct dvb_frontend *fe,
Mauro Carvalho Chehabdc00f082018-04-05 05:30:52 -0400267 struct dvb_frontend_event *event, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700269 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct dvb_fe_events *events = &fepriv->events;
271
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300272 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 if (events->overflow) {
275 events->overflow = 0;
276 return -EOVERFLOW;
277 }
278
279 if (events->eventw == events->eventr) {
280 int ret;
281
282 if (flags & O_NONBLOCK)
283 return -EWOULDBLOCK;
284
Mauro Carvalho Chehabdc00f082018-04-05 05:30:52 -0400285 ret = wait_event_interruptible(events->wait_queue,
286 dvb_frontend_test_event(fepriv, events));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 if (ret < 0)
289 return ret;
290 }
291
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300292 mutex_lock(&events->mtx);
293 *event = events->events[events->eventr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 events->eventr = (events->eventr + 1) % MAX_EVENT;
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300295 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 return 0;
298}
299
Andreas Oberritter20640be2011-08-04 12:33:14 -0300300static void dvb_frontend_clear_events(struct dvb_frontend *fe)
301{
302 struct dvb_frontend_private *fepriv = fe->frontend_priv;
303 struct dvb_fe_events *events = &fepriv->events;
304
305 mutex_lock(&events->mtx);
306 events->eventr = events->eventw;
307 mutex_unlock(&events->mtx);
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static void dvb_frontend_init(struct dvb_frontend *fe)
311{
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300312 dev_dbg(fe->dvb->device,
313 "%s: initialising adapter %i frontend %i (%s)...\n",
314 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Patrick Boettcherdea74862006-05-14 05:01:31 -0300316 if (fe->ops.init)
317 fe->ops.init(fe);
318 if (fe->ops.tuner_ops.init) {
Manu Abraham3b37a152008-10-20 18:14:14 -0300319 if (fe->ops.i2c_gate_ctrl)
320 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300321 fe->ops.tuner_ops.init(fe);
322 if (fe->ops.i2c_gate_ctrl)
323 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300327void dvb_frontend_reinitialise(struct dvb_frontend *fe)
328{
329 struct dvb_frontend_private *fepriv = fe->frontend_priv;
330
331 fepriv->reinitialise = 1;
332 dvb_frontend_wakeup(fe);
333}
334EXPORT_SYMBOL(dvb_frontend_reinitialise);
335
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200336static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200338 int q2;
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300339 struct dvb_frontend *fe = fepriv->dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300341 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200343 if (locked)
344 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
345 else
346 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200348 q2 = fepriv->quality - 128;
349 q2 *= q2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200351 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354/**
355 * Performs automatic twiddling of frontend parameters.
356 *
357 * @param fe The frontend concerned.
358 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
359 * @returns Number of complete iterations that have been performed.
360 */
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200361static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int autoinversion;
364 int ready = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300365 int fe_set_err = 0;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700366 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300367 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
368 int original_inversion = c->inversion;
369 u32 original_frequency = c->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /* are we using autoinversion? */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300372 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300373 (c->inversion == INVERSION_AUTO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* setup parameters correctly */
376 while(!ready) {
377 /* calculate the lnb_drift */
378 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
379
380 /* wrap the auto_step if we've exceeded the maximum drift */
381 if (fepriv->lnb_drift > fepriv->max_drift) {
382 fepriv->auto_step = 0;
383 fepriv->auto_sub_step = 0;
384 fepriv->lnb_drift = 0;
385 }
386
387 /* perform inversion and +/- zigzag */
388 switch(fepriv->auto_sub_step) {
389 case 0:
390 /* try with the current inversion and current drift setting */
391 ready = 1;
392 break;
393
394 case 1:
395 if (!autoinversion) break;
396
397 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
398 ready = 1;
399 break;
400
401 case 2:
402 if (fepriv->lnb_drift == 0) break;
403
404 fepriv->lnb_drift = -fepriv->lnb_drift;
405 ready = 1;
406 break;
407
408 case 3:
409 if (fepriv->lnb_drift == 0) break;
410 if (!autoinversion) break;
411
412 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
413 fepriv->lnb_drift = -fepriv->lnb_drift;
414 ready = 1;
415 break;
416
417 default:
418 fepriv->auto_step++;
419 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
420 break;
421 }
422
423 if (!ready) fepriv->auto_sub_step++;
424 }
425
426 /* if this attempt would hit where we started, indicate a complete
427 * iteration has occurred */
428 if ((fepriv->auto_step == fepriv->started_auto_step) &&
429 (fepriv->auto_sub_step == 0) && check_wrapped) {
430 return 1;
431 }
432
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300433 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
434 "auto_sub_step:%i started_auto_step:%i\n",
435 __func__, fepriv->lnb_drift, fepriv->inversion,
436 fepriv->auto_step, fepriv->auto_sub_step,
437 fepriv->started_auto_step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* set the frontend itself */
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300440 c->frequency += fepriv->lnb_drift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (autoinversion)
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300442 c->inversion = fepriv->inversion;
443 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300444 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300445 fe_set_err = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300446 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300447 if (fe_set_err < 0) {
448 fepriv->state = FESTATE_ERROR;
449 return fe_set_err;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300452 c->frequency = original_frequency;
453 c->inversion = original_inversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 fepriv->auto_sub_step++;
456 return 0;
457}
458
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200459static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
460{
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300461 enum fe_status s = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300462 int retval = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200463 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300464 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200465
466 /* if we've got no parameters, just keep idling */
467 if (fepriv->state & FESTATE_IDLE) {
468 fepriv->delay = 3*HZ;
469 fepriv->quality = 0;
470 return;
471 }
472
473 /* in SCAN mode, we just set the frontend when asked and leave it alone */
474 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
475 if (fepriv->state & FESTATE_RETUNE) {
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300476 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300477 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300478 retval = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300479 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300480 if (retval < 0)
481 fepriv->state = FESTATE_ERROR;
482 else
483 fepriv->state = FESTATE_TUNED;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200484 }
485 fepriv->delay = 3*HZ;
486 fepriv->quality = 0;
487 return;
488 }
489
490 /* get the frontend status */
491 if (fepriv->state & FESTATE_RETUNE) {
492 s = 0;
493 } else {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300494 if (fe->ops.read_status)
495 fe->ops.read_status(fe, &s);
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200496 if (s != fepriv->status) {
497 dvb_frontend_add_event(fe, s);
498 fepriv->status = s;
499 }
500 }
501
502 /* if we're not tuned, and we have a lock, move to the TUNED state */
503 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
504 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
505 fepriv->state = FESTATE_TUNED;
506
507 /* if we're tuned, then we have determined the correct inversion */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300508 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300509 (c->inversion == INVERSION_AUTO)) {
510 c->inversion = fepriv->inversion;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200511 }
512 return;
513 }
514
515 /* if we are tuned already, check we're still locked */
516 if (fepriv->state & FESTATE_TUNED) {
517 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
518
519 /* we're tuned, and the lock is still good... */
520 if (s & FE_HAS_LOCK) {
521 return;
522 } else { /* if we _WERE_ tuned, but now don't have a lock */
523 fepriv->state = FESTATE_ZIGZAG_FAST;
524 fepriv->started_auto_step = fepriv->auto_step;
525 fepriv->check_wrapped = 0;
526 }
527 }
528
529 /* don't actually do anything if we're in the LOSTLOCK state,
530 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
531 if ((fepriv->state & FESTATE_LOSTLOCK) &&
Patrick Boettcherdea74862006-05-14 05:01:31 -0300532 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200533 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
534 return;
535 }
536
537 /* don't do anything if we're in the DISEQC state, since this
538 * might be someone with a motorized dish controlled by DISEQC.
539 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
540 if (fepriv->state & FESTATE_DISEQC) {
541 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
542 return;
543 }
544
545 /* if we're in the RETUNE state, set everything up for a brand
546 * new scan, keeping the current inversion setting, as the next
547 * tune is _very_ likely to require the same */
548 if (fepriv->state & FESTATE_RETUNE) {
549 fepriv->lnb_drift = 0;
550 fepriv->auto_step = 0;
551 fepriv->auto_sub_step = 0;
552 fepriv->started_auto_step = 0;
553 fepriv->check_wrapped = 0;
554 }
555
556 /* fast zigzag. */
557 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
558 fepriv->delay = fepriv->min_delay;
559
Guillaume Audirac2030c032010-05-06 09:30:25 -0300560 /* perform a tune */
Janne Grunau01886252009-09-01 19:23:09 -0300561 retval = dvb_frontend_swzigzag_autotune(fe,
562 fepriv->check_wrapped);
563 if (retval < 0) {
564 return;
565 } else if (retval) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200566 /* OK, if we've run out of trials at the fast speed.
567 * Drop back to slow for the _next_ attempt */
568 fepriv->state = FESTATE_SEARCHING_SLOW;
569 fepriv->started_auto_step = fepriv->auto_step;
570 return;
571 }
572 fepriv->check_wrapped = 1;
573
574 /* if we've just retuned, enter the ZIGZAG_FAST state.
575 * This ensures we cannot return from an
576 * FE_SET_FRONTEND ioctl before the first frontend tune
577 * occurs */
578 if (fepriv->state & FESTATE_RETUNE) {
579 fepriv->state = FESTATE_TUNING_FAST;
580 }
581 }
582
583 /* slow zigzag */
584 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
585 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
586
587 /* Note: don't bother checking for wrapping; we stay in this
588 * state until we get a lock */
589 dvb_frontend_swzigzag_autotune(fe, 0);
590 }
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
594{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700595 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Shuah Khan18ed2862014-07-12 13:44:12 -0300597 if (fe->exit != DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return 1;
599
600 if (fepriv->dvbdev->writers == 1)
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -0300601 if (time_after_eq(jiffies, fepriv->release_jiffies +
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200602 dvb_shutdown_timeout * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return 1;
604
605 return 0;
606}
607
608static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
609{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700610 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612 if (fepriv->wakeup) {
613 fepriv->wakeup = 0;
614 return 1;
615 }
616 return dvb_frontend_is_exiting(fe);
617}
618
619static void dvb_frontend_wakeup(struct dvb_frontend *fe)
620{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700621 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 fepriv->wakeup = 1;
624 wake_up_interruptible(&fepriv->wait_queue);
625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627static int dvb_frontend_thread(void *data)
628{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700629 struct dvb_frontend *fe = data;
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -0200630 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700631 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300632 enum fe_status s;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300633 enum dvbfe_algo algo;
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300634 bool re_tune = false;
Juergen Lock6ae23222012-12-23 17:23:06 -0300635 bool semheld = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300637 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200639 fepriv->check_wrapped = 0;
640 fepriv->quality = 0;
641 fepriv->delay = 3*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 fepriv->wakeup = 0;
Andrew de Quincey04c56d02006-07-10 03:34:14 -0300644 fepriv->reinitialise = 0;
645
646 dvb_frontend_init(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700648 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 while (1) {
650 up(&fepriv->sem); /* is locked when we enter the thread... */
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300651restart:
Hans Verkuil94238e92011-08-27 11:30:25 -0300652 wait_event_interruptible_timeout(fepriv->wait_queue,
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700653 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
654 || freezing(current),
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300655 fepriv->delay);
656
657 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* got signal or quitting */
Juergen Lock6ae23222012-12-23 17:23:06 -0300659 if (!down_interruptible(&fepriv->sem))
660 semheld = true;
Shuah Khan18ed2862014-07-12 13:44:12 -0300661 fe->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 }
664
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300665 if (try_to_freeze())
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300666 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 if (down_interruptible(&fepriv->sem))
669 break;
670
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300671 if (fepriv->reinitialise) {
672 dvb_frontend_init(fe);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300673 if (fe->ops.set_tone && fepriv->tone != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300674 fe->ops.set_tone(fe, fepriv->tone);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300675 if (fe->ops.set_voltage && fepriv->voltage != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300676 fe->ops.set_voltage(fe, fepriv->voltage);
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300677 fepriv->reinitialise = 0;
678 }
679
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200680 /* do an iteration of the tuning loop */
Manu Abrahamd772bd02006-06-24 11:18:58 -0300681 if (fe->ops.get_frontend_algo) {
Manu Abrahamc59e7872008-10-14 16:34:07 -0300682 algo = fe->ops.get_frontend_algo(fe);
683 switch (algo) {
684 case DVBFE_ALGO_HW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300685 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300686
Manu Abrahamd772bd02006-06-24 11:18:58 -0300687 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300688 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300689 re_tune = true;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300690 fepriv->state = FESTATE_TUNED;
Simon Arlott45145b62012-02-06 17:57:01 -0300691 } else {
692 re_tune = false;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Manu Abrahamc59e7872008-10-14 16:34:07 -0300695 if (fe->ops.tune)
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300696 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300697
Darron Broad2fac9a02008-12-18 06:27:50 -0300698 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300699 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
Manu Abrahamd772bd02006-06-24 11:18:58 -0300700 dvb_frontend_add_event(fe, s);
701 fepriv->status = s;
702 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300703 break;
704 case DVBFE_ALGO_SW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300705 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
Manu Abraham70d90632006-06-29 22:05:23 -0300706 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300707 break;
708 case DVBFE_ALGO_CUSTOM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300709 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300710 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300711 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300712 fepriv->state = FESTATE_TUNED;
713 }
714 /* Case where we are going to search for a carrier
715 * User asked us to retune again for some reason, possibly
716 * requesting a search with a new set of parameters
717 */
718 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300719 if (fe->ops.search) {
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -0300720 fepriv->algo_status = fe->ops.search(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300721 /* We did do a search as was requested, the flags are
722 * now unset as well and has the flags wrt to search.
723 */
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300724 } else {
725 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
726 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300727 }
728 /* Track the carrier if the search was successful */
Mauro Carvalho Chehab1b5d8712011-12-26 18:11:25 -0300729 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300730 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
731 fepriv->delay = HZ / 2;
732 }
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -0200733 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300734 fe->ops.read_status(fe, &s);
735 if (s != fepriv->status) {
736 dvb_frontend_add_event(fe, s); /* update event list */
737 fepriv->status = s;
738 if (!(s & FE_HAS_LOCK)) {
739 fepriv->delay = HZ / 10;
740 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
741 } else {
742 fepriv->delay = 60 * HZ;
743 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300744 }
745 break;
746 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300747 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300748 break;
749 }
750 } else {
Manu Abraham4a4edcc2006-06-25 05:46:26 -0300751 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
Oliver Endriss608f62d2007-08-25 13:17:53 -0300755 if (dvb_powerdown_on_sleep) {
756 if (fe->ops.set_voltage)
757 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300758 if (fe->ops.tuner_ops.sleep) {
Devin Heitmueller41286d92008-11-16 00:44:52 -0300759 if (fe->ops.i2c_gate_ctrl)
760 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300761 fe->ops.tuner_ops.sleep(fe);
762 if (fe->ops.i2c_gate_ctrl)
763 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300764 }
Patrick Boettcherdea74862006-05-14 05:01:31 -0300765 if (fe->ops.sleep)
766 fe->ops.sleep(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300769 fepriv->thread = NULL;
matthieu castete36309f2010-05-05 15:59:20 -0300770 if (kthread_should_stop())
Shuah Khan18ed2862014-07-12 13:44:12 -0300771 fe->exit = DVB_FE_DEVICE_REMOVED;
matthieu castete36309f2010-05-05 15:59:20 -0300772 else
Shuah Khan18ed2862014-07-12 13:44:12 -0300773 fe->exit = DVB_FE_NO_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 mb();
775
Juergen Lock6ae23222012-12-23 17:23:06 -0300776 if (semheld)
777 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 dvb_frontend_wakeup(fe);
779 return 0;
780}
781
782static void dvb_frontend_stop(struct dvb_frontend *fe)
783{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700784 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300786 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Shuah Khan18ed2862014-07-12 13:44:12 -0300788 if (fe->exit != DVB_FE_DEVICE_REMOVED)
789 fe->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 mb();
791
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300792 if (!fepriv->thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return;
794
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300795 kthread_stop(fepriv->thread);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300796
Thomas Gleixnera0a47142010-09-07 11:33:27 -0300797 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 fepriv->state = FESTATE_IDLE;
799
800 /* paranoia check in case a signal arrived */
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300801 if (fepriv->thread)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300802 dev_warn(fe->dvb->device,
803 "dvb_frontend_stop: warning: thread %p won't exit\n",
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300804 fepriv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
NooneImportant83b75b02005-11-08 21:35:27 -0800807/*
Mauro Carvalho Chehab4deea4c2015-11-09 23:24:10 -0200808 * Sleep for the amount of time given by add_usec parameter
809 *
810 * This needs to be as precise as possible, as it affects the detection of
811 * the dish tone command at the satellite subsystem. The precision is improved
812 * by using a scheduled msleep followed by udelay for the remainder.
NooneImportant83b75b02005-11-08 21:35:27 -0800813 */
Tina Ruchandani9056a232015-05-31 04:17:06 -0300814void dvb_frontend_sleep_until(ktime_t *waketime, u32 add_usec)
NooneImportant83b75b02005-11-08 21:35:27 -0800815{
Mauro Carvalho Chehaba733a412015-11-18 12:55:47 -0200816 s32 delta;
NooneImportant83b75b02005-11-08 21:35:27 -0800817
Mauro Carvalho Chehaba733a412015-11-18 12:55:47 -0200818 *waketime = ktime_add_us(*waketime, add_usec);
Abhilash Jindal6b3f9992016-01-31 03:47:31 -0200819 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
NooneImportant83b75b02005-11-08 21:35:27 -0800820 if (delta > 2500) {
821 msleep((delta - 1500) / 1000);
Abhilash Jindal6b3f9992016-01-31 03:47:31 -0200822 delta = ktime_us_delta(ktime_get_boottime(), *waketime);
NooneImportant83b75b02005-11-08 21:35:27 -0800823 }
824 if (delta > 0)
825 udelay(delta);
826}
827EXPORT_SYMBOL(dvb_frontend_sleep_until);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829static int dvb_frontend_start(struct dvb_frontend *fe)
830{
831 int ret;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700832 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300833 struct task_struct *fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300835 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300837 if (fepriv->thread) {
Shuah Khan18ed2862014-07-12 13:44:12 -0300838 if (fe->exit == DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return 0;
840 else
841 dvb_frontend_stop (fe);
842 }
843
844 if (signal_pending(current))
845 return -EINTR;
846 if (down_interruptible (&fepriv->sem))
847 return -EINTR;
848
849 fepriv->state = FESTATE_IDLE;
Shuah Khan18ed2862014-07-12 13:44:12 -0300850 fe->exit = DVB_FE_NO_EXIT;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300851 fepriv->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 mb();
853
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300854 fe_thread = kthread_run(dvb_frontend_thread, fe,
Steven Toth363c35f2008-10-11 11:05:50 -0300855 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300856 if (IS_ERR(fe_thread)) {
857 ret = PTR_ERR(fe_thread);
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300858 dev_warn(fe->dvb->device,
859 "dvb_frontend_start: failed to start kthread (%d)\n",
860 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 up(&fepriv->sem);
862 return ret;
863 }
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300864 fepriv->thread = fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867
Guillaume Audirac2030c032010-05-06 09:30:25 -0300868static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
Oliver Endrissc471b332007-08-09 01:03:42 -0300869 u32 *freq_min, u32 *freq_max)
870{
871 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
872
873 if (fe->ops.info.frequency_max == 0)
874 *freq_max = fe->ops.tuner_ops.info.frequency_max;
875 else if (fe->ops.tuner_ops.info.frequency_max == 0)
876 *freq_max = fe->ops.info.frequency_max;
877 else
878 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
879
880 if (*freq_min == 0 || *freq_max == 0)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300881 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
882 fe->dvb->num, fe->id);
Oliver Endrissc471b332007-08-09 01:03:42 -0300883}
884
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300885static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300886{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300887 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Oliver Endrissc471b332007-08-09 01:03:42 -0300888 u32 freq_min;
889 u32 freq_max;
890
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300891 /* range check: frequency */
Guillaume Audirac2030c032010-05-06 09:30:25 -0300892 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300893 if ((freq_min && c->frequency < freq_min) ||
894 (freq_max && c->frequency > freq_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300895 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
896 fe->dvb->num, fe->id, c->frequency,
897 freq_min, freq_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300898 return -EINVAL;
899 }
900
901 /* range check: symbol rate */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300902 switch (c->delivery_system) {
903 case SYS_DVBS:
904 case SYS_DVBS2:
905 case SYS_TURBO:
906 case SYS_DVBC_ANNEX_A:
907 case SYS_DVBC_ANNEX_C:
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300908 if ((fe->ops.info.symbol_rate_min &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300909 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300910 (fe->ops.info.symbol_rate_max &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300911 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300912 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
913 fe->dvb->num, fe->id, c->symbol_rate,
914 fe->ops.info.symbol_rate_min,
915 fe->ops.info.symbol_rate_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300916 return -EINVAL;
917 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300918 default:
919 break;
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300920 }
921
922 return 0;
923}
924
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300925static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
926{
Andreas Oberritter50727712011-05-08 20:03:39 -0300927 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300928 int i;
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200929 u32 delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300930
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200931 delsys = c->delivery_system;
Mauro Carvalho Chehab808d24d2013-03-20 10:39:31 -0300932 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200933 c->delivery_system = delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300934
Andreas Oberritter50727712011-05-08 20:03:39 -0300935 c->state = DTV_CLEAR;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300936
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300937 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
938 __func__, c->delivery_system);
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300939
Andreas Oberritter50727712011-05-08 20:03:39 -0300940 c->transmission_mode = TRANSMISSION_MODE_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300941 c->bandwidth_hz = 0; /* AUTO */
Andreas Oberritter50727712011-05-08 20:03:39 -0300942 c->guard_interval = GUARD_INTERVAL_AUTO;
943 c->hierarchy = HIERARCHY_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300944 c->symbol_rate = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -0300945 c->code_rate_HP = FEC_AUTO;
946 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300947 c->fec_inner = FEC_AUTO;
Mauro Carvalho Chehab39ce61a2011-11-11 12:46:23 -0200948 c->rolloff = ROLLOFF_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300949 c->voltage = SEC_VOLTAGE_OFF;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300950 c->sectone = SEC_TONE_OFF;
951 c->pilot = PILOT_AUTO;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300952
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300953 c->isdbt_partial_reception = 0;
954 c->isdbt_sb_mode = 0;
955 c->isdbt_sb_subchannel = 0;
956 c->isdbt_sb_segment_idx = 0;
957 c->isdbt_sb_segment_count = 0;
958 c->isdbt_layer_enabled = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300959 for (i = 0; i < 3; i++) {
Andreas Oberritter50727712011-05-08 20:03:39 -0300960 c->layer[i].fec = FEC_AUTO;
961 c->layer[i].modulation = QAM_AUTO;
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300962 c->layer[i].interleaving = 0;
963 c->layer[i].segment_count = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300964 }
965
Evgeny Plehov287cefd2012-09-13 10:13:30 -0300966 c->stream_id = NO_STREAM_ID_FILTER;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300967
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300968 switch (c->delivery_system) {
969 case SYS_DVBS:
970 case SYS_DVBS2:
971 case SYS_TURBO:
972 c->modulation = QPSK; /* implied for DVB-S in legacy API */
973 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
974 break;
975 case SYS_ATSC:
976 c->modulation = VSB_8;
977 break;
Akihiro Tsukada8e281fa2014-10-31 10:19:39 -0300978 case SYS_ISDBS:
979 c->symbol_rate = 28860000;
980 c->rolloff = ROLLOFF_35;
981 c->bandwidth_hz = c->symbol_rate / 100 * 135;
982 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300983 default:
984 c->modulation = QAM_AUTO;
985 break;
986 }
987
Antti Palosaari33eebec2012-10-03 04:28:56 -0300988 c->lna = LNA_AUTO;
989
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300990 return 0;
991}
992
993#define _DTV_CMD(n, s, b) \
994[n] = { \
995 .name = #n, \
996 .cmd = n, \
997 .set = s,\
998 .buffer = b \
999}
1000
Andreas Oberritterec05a642011-05-26 07:54:14 -03001001static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001002 _DTV_CMD(DTV_TUNE, 1, 0),
1003 _DTV_CMD(DTV_CLEAR, 1, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -03001004
1005 /* Set */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001006 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1007 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1008 _DTV_CMD(DTV_MODULATION, 1, 0),
1009 _DTV_CMD(DTV_INVERSION, 1, 0),
1010 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1011 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1012 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1013 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1014 _DTV_CMD(DTV_TONE, 1, 0),
1015 _DTV_CMD(DTV_PILOT, 1, 0),
1016 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1017 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1018 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1019 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1020 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1021 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1022 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
Antti Palosaari224b6642012-08-12 22:33:21 -03001023 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001024
1025 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1026 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1027 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1028 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1029 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
Patrick Boettchere7b79492009-08-14 05:24:19 -03001030 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001031 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1032 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1033 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1034 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1035 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1036 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1037 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1038 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1039 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1040 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1041 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1042 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1043
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001044 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1045 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
Antti Palosaari8a2697a2012-07-11 21:54:50 -03001046 _DTV_CMD(DTV_LNA, 1, 0),
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001047
Steven Toth6b73eea2008-09-04 01:12:25 -03001048 /* Get */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001049 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1050 _DTV_CMD(DTV_API_VERSION, 0, 0),
Manu Abrahamba2780c2011-11-13 18:47:44 -03001051
1052 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001053
1054 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1055 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1056
1057 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001058 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1059 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1060 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1061 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1062 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001063 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1064 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1065 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1066 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1067 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1068 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1069 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001070
1071 /* Statistics API */
1072 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1073 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1074 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1075 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1076 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1077 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1078 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1079 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -03001080};
1081
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001082static void dtv_property_dump(struct dvb_frontend *fe,
1083 bool is_set,
1084 struct dtv_property *tvp)
Steven Toth6b73eea2008-09-04 01:12:25 -03001085{
1086 int i;
1087
Mauro Carvalho Chehab106ff9e2011-07-15 08:49:06 -03001088 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001089 dev_warn(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x undefined\n",
1090 __func__,
1091 is_set ? "SET" : "GET",
1092 tvp->cmd);
Darron Broad4aae8ef2008-10-03 11:50:00 -03001093 return;
1094 }
1095
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001096 dev_dbg(fe->dvb->device, "%s: %s tvp.cmd = 0x%08x (%s)\n", __func__,
1097 is_set ? "SET" : "GET",
1098 tvp->cmd,
1099 dtv_cmds[tvp->cmd].name);
Steven Toth6b73eea2008-09-04 01:12:25 -03001100
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001101 if (dtv_cmds[tvp->cmd].buffer) {
1102 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1103 __func__, tvp->u.buffer.len);
Steven Toth6b73eea2008-09-04 01:12:25 -03001104
1105 for(i = 0; i < tvp->u.buffer.len; i++)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001106 dev_dbg(fe->dvb->device,
1107 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1108 __func__, i, tvp->u.buffer.data[i]);
1109 } else {
1110 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1111 tvp->u.data);
1112 }
Steven Toth6b73eea2008-09-04 01:12:25 -03001113}
1114
Steven Tothee33c522008-09-11 23:52:32 -03001115/* Synchronise the legacy tuning parameters into the cache, so that demodulator
1116 * drivers can use a single set_frontend tuning function, regardless of whether
1117 * it's being used for the legacy or new API, reducing code and complexity.
1118 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001119static int dtv_property_cache_sync(struct dvb_frontend *fe,
1120 struct dtv_frontend_properties *c,
1121 const struct dvb_frontend_parameters *p)
Steven Tothee33c522008-09-11 23:52:32 -03001122{
Steven Tothee33c522008-09-11 23:52:32 -03001123 c->frequency = p->frequency;
1124 c->inversion = p->inversion;
1125
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001126 switch (dvbv3_type(c->delivery_system)) {
1127 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001128 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001129 c->symbol_rate = p->u.qpsk.symbol_rate;
1130 c->fec_inner = p->u.qpsk.fec_inner;
1131 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001132 case DVBV3_QAM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001133 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001134 c->symbol_rate = p->u.qam.symbol_rate;
1135 c->fec_inner = p->u.qam.fec_inner;
1136 c->modulation = p->u.qam.modulation;
1137 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001138 case DVBV3_OFDM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001139 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1140
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001141 switch (p->u.ofdm.bandwidth) {
1142 case BANDWIDTH_10_MHZ:
1143 c->bandwidth_hz = 10000000;
1144 break;
1145 case BANDWIDTH_8_MHZ:
Steven Toth75b7f942008-09-13 16:56:34 -03001146 c->bandwidth_hz = 8000000;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001147 break;
1148 case BANDWIDTH_7_MHZ:
1149 c->bandwidth_hz = 7000000;
1150 break;
1151 case BANDWIDTH_6_MHZ:
1152 c->bandwidth_hz = 6000000;
1153 break;
1154 case BANDWIDTH_5_MHZ:
1155 c->bandwidth_hz = 5000000;
1156 break;
1157 case BANDWIDTH_1_712_MHZ:
1158 c->bandwidth_hz = 1712000;
1159 break;
1160 case BANDWIDTH_AUTO:
Steven Toth75b7f942008-09-13 16:56:34 -03001161 c->bandwidth_hz = 0;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001162 }
1163
Steven Tothee33c522008-09-11 23:52:32 -03001164 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1165 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1166 c->modulation = p->u.ofdm.constellation;
1167 c->transmission_mode = p->u.ofdm.transmission_mode;
1168 c->guard_interval = p->u.ofdm.guard_interval;
1169 c->hierarchy = p->u.ofdm.hierarchy_information;
1170 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001171 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001172 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001173 c->modulation = p->u.vsb.modulation;
Michael Krufkyca689482012-01-29 15:44:58 -03001174 if (c->delivery_system == SYS_ATSCMH)
1175 break;
Steven Toth80a773c2008-09-12 00:53:50 -03001176 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1177 c->delivery_system = SYS_ATSC;
1178 else
1179 c->delivery_system = SYS_DVBC_ANNEX_B;
Steven Tothee33c522008-09-11 23:52:32 -03001180 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001181 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001182 dev_err(fe->dvb->device,
1183 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1184 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001185 return -EINVAL;
Steven Tothee33c522008-09-11 23:52:32 -03001186 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001187
1188 return 0;
Steven Tothee33c522008-09-11 23:52:32 -03001189}
1190
Steven Tothd5748f12008-09-12 00:27:13 -03001191/* Ensure the cached values are set correctly in the frontend
1192 * legacy tuning structures, for the advanced tuning API.
1193 */
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -02001194static int
1195dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1196 const struct dtv_frontend_properties *c,
1197 struct dvb_frontend_parameters *p)
Steven Toth6b73eea2008-09-04 01:12:25 -03001198{
Steven Tothd5748f12008-09-12 00:27:13 -03001199 p->frequency = c->frequency;
1200 p->inversion = c->inversion;
1201
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001202 switch (dvbv3_type(c->delivery_system)) {
1203 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001204 dev_err(fe->dvb->device,
1205 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1206 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001207 return -EINVAL;
1208 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001209 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001210 p->u.qpsk.symbol_rate = c->symbol_rate;
1211 p->u.qpsk.fec_inner = c->fec_inner;
1212 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001213 case DVBV3_QAM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001214 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001215 p->u.qam.symbol_rate = c->symbol_rate;
1216 p->u.qam.fec_inner = c->fec_inner;
1217 p->u.qam.modulation = c->modulation;
1218 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001219 case DVBV3_OFDM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001220 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001221 switch (c->bandwidth_hz) {
1222 case 10000000:
1223 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1224 break;
1225 case 8000000:
Steven Toth75b7f942008-09-13 16:56:34 -03001226 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001227 break;
1228 case 7000000:
1229 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1230 break;
1231 case 6000000:
1232 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1233 break;
1234 case 5000000:
1235 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1236 break;
1237 case 1712000:
1238 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1239 break;
1240 case 0:
1241 default:
Steven Toth75b7f942008-09-13 16:56:34 -03001242 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001243 }
Steven Tothd5748f12008-09-12 00:27:13 -03001244 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1245 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1246 p->u.ofdm.constellation = c->modulation;
1247 p->u.ofdm.transmission_mode = c->transmission_mode;
1248 p->u.ofdm.guard_interval = c->guard_interval;
1249 p->u.ofdm.hierarchy_information = c->hierarchy;
1250 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001251 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001252 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001253 p->u.vsb.modulation = c->modulation;
1254 break;
1255 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001256 return 0;
Steven Toth6b73eea2008-09-04 01:12:25 -03001257}
1258
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001259/**
1260 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1261 * @fe: struct dvb_frontend pointer
1262 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1263 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1264 *
1265 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1266 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1267 * If p_out is not null, it will update the DVBv3 params pointed by it.
1268 */
1269static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -02001270 struct dtv_frontend_properties *c,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001271 struct dvb_frontend_parameters *p_out)
1272{
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001273 int r;
1274
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001275 if (fe->ops.get_frontend) {
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02001276 r = fe->ops.get_frontend(fe, c);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001277 if (unlikely(r < 0))
1278 return r;
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001279 if (p_out)
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -02001280 dtv_property_legacy_params_sync(fe, c, p_out);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001281 return 0;
1282 }
1283
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001284 /* As everything is in cache, get_frontend fops are always supported */
Mauro Carvalho Chehabb1e9a65012011-12-26 16:43:32 -03001285 return 0;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001286}
1287
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001288static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001289 unsigned int cmd, void *parg);
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001290static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001291 unsigned int cmd, void *parg);
1292
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001293static int dtv_property_process_get(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001294 const struct dtv_frontend_properties *c,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001295 struct dtv_property *tvp,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001296 struct file *file)
Steven Toth363429a2008-09-12 01:34:28 -03001297{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001298 int r, ncaps;
Steven Tothbfbf2da2008-09-12 01:37:37 -03001299
Steven Toth363429a2008-09-12 01:34:28 -03001300 switch(tvp->cmd) {
Manu Abrahamba2780c2011-11-13 18:47:44 -03001301 case DTV_ENUM_DELSYS:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001302 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001303 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001304 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1305 ncaps++;
1306 }
1307 tvp->u.buffer.len = ncaps;
Manu Abrahamba2780c2011-11-13 18:47:44 -03001308 break;
Steven Toth363429a2008-09-12 01:34:28 -03001309 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001310 tvp->u.data = c->frequency;
Steven Toth363429a2008-09-12 01:34:28 -03001311 break;
1312 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001313 tvp->u.data = c->modulation;
Steven Toth363429a2008-09-12 01:34:28 -03001314 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001315 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001316 tvp->u.data = c->bandwidth_hz;
Steven Toth363429a2008-09-12 01:34:28 -03001317 break;
1318 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001319 tvp->u.data = c->inversion;
Steven Toth363429a2008-09-12 01:34:28 -03001320 break;
1321 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001322 tvp->u.data = c->symbol_rate;
Steven Toth363429a2008-09-12 01:34:28 -03001323 break;
1324 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001325 tvp->u.data = c->fec_inner;
Steven Toth363429a2008-09-12 01:34:28 -03001326 break;
1327 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001328 tvp->u.data = c->pilot;
Steven Toth363429a2008-09-12 01:34:28 -03001329 break;
1330 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001331 tvp->u.data = c->rolloff;
Steven Toth363429a2008-09-12 01:34:28 -03001332 break;
1333 case DTV_DELIVERY_SYSTEM:
Andreas Oberritter50727712011-05-08 20:03:39 -03001334 tvp->u.data = c->delivery_system;
Steven Toth363429a2008-09-12 01:34:28 -03001335 break;
Steven Toth363429a2008-09-12 01:34:28 -03001336 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001337 tvp->u.data = c->voltage;
Steven Toth363429a2008-09-12 01:34:28 -03001338 break;
1339 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001340 tvp->u.data = c->sectone;
Steven Toth363429a2008-09-12 01:34:28 -03001341 break;
Steven Totheacf8d82008-09-26 00:29:49 -03001342 case DTV_API_VERSION:
1343 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1344 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001345 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001346 tvp->u.data = c->code_rate_HP;
Steven Totha4de91b2008-10-06 21:55:46 -03001347 break;
1348 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001349 tvp->u.data = c->code_rate_LP;
Steven Totha4de91b2008-10-06 21:55:46 -03001350 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001351 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001352 tvp->u.data = c->guard_interval;
Steven Tothb87625f2008-10-06 21:56:59 -03001353 break;
1354 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001355 tvp->u.data = c->transmission_mode;
Steven Tothb87625f2008-10-06 21:56:59 -03001356 break;
Steven Tothef526f42008-10-06 22:01:47 -03001357 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001358 tvp->u.data = c->hierarchy;
Steven Tothef526f42008-10-06 22:01:47 -03001359 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001360 case DTV_INTERLEAVING:
1361 tvp->u.data = c->interleaving;
1362 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001363
1364 /* ISDB-T Support here */
1365 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001366 tvp->u.data = c->isdbt_partial_reception;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001367 break;
1368 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001369 tvp->u.data = c->isdbt_sb_mode;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001370 break;
1371 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001372 tvp->u.data = c->isdbt_sb_subchannel;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001373 break;
1374 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001375 tvp->u.data = c->isdbt_sb_segment_idx;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001376 break;
1377 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001378 tvp->u.data = c->isdbt_sb_segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001379 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001380 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001381 tvp->u.data = c->isdbt_layer_enabled;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001382 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001383 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001384 tvp->u.data = c->layer[0].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001385 break;
1386 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001387 tvp->u.data = c->layer[0].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001388 break;
1389 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001390 tvp->u.data = c->layer[0].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001391 break;
1392 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001393 tvp->u.data = c->layer[0].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001394 break;
1395 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001396 tvp->u.data = c->layer[1].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001397 break;
1398 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001399 tvp->u.data = c->layer[1].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001400 break;
1401 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001402 tvp->u.data = c->layer[1].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001403 break;
1404 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001405 tvp->u.data = c->layer[1].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001406 break;
1407 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001408 tvp->u.data = c->layer[2].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001409 break;
1410 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001411 tvp->u.data = c->layer[2].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001412 break;
1413 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001414 tvp->u.data = c->layer[2].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001415 break;
1416 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001417 tvp->u.data = c->layer[2].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001418 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001419
1420 /* Multistream support */
1421 case DTV_STREAM_ID:
1422 case DTV_DVBT2_PLP_ID_LEGACY:
1423 tvp->u.data = c->stream_id;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001424 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001425
1426 /* ATSC-MH */
1427 case DTV_ATSCMH_FIC_VER:
1428 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1429 break;
1430 case DTV_ATSCMH_PARADE_ID:
1431 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1432 break;
1433 case DTV_ATSCMH_NOG:
1434 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1435 break;
1436 case DTV_ATSCMH_TNOG:
1437 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1438 break;
1439 case DTV_ATSCMH_SGN:
1440 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1441 break;
1442 case DTV_ATSCMH_PRC:
1443 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1444 break;
1445 case DTV_ATSCMH_RS_FRAME_MODE:
1446 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1447 break;
1448 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1449 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1450 break;
1451 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1452 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1453 break;
1454 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1455 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1456 break;
1457 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1458 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1459 break;
1460 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1461 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1462 break;
1463 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1464 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1465 break;
1466 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1467 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1468 break;
1469 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1470 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1471 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001472
Antti Palosaari33eebec2012-10-03 04:28:56 -03001473 case DTV_LNA:
1474 tvp->u.data = c->lna;
1475 break;
1476
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001477 /* Fill quality measures */
1478 case DTV_STAT_SIGNAL_STRENGTH:
1479 tvp->u.st = c->strength;
1480 break;
1481 case DTV_STAT_CNR:
1482 tvp->u.st = c->cnr;
1483 break;
1484 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1485 tvp->u.st = c->pre_bit_error;
1486 break;
1487 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1488 tvp->u.st = c->pre_bit_count;
1489 break;
1490 case DTV_STAT_POST_ERROR_BIT_COUNT:
1491 tvp->u.st = c->post_bit_error;
1492 break;
1493 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1494 tvp->u.st = c->post_bit_count;
1495 break;
1496 case DTV_STAT_ERROR_BLOCK_COUNT:
1497 tvp->u.st = c->block_error;
1498 break;
1499 case DTV_STAT_TOTAL_BLOCK_COUNT:
1500 tvp->u.st = c->block_count;
1501 break;
Steven Toth363429a2008-09-12 01:34:28 -03001502 default:
Mauro Carvalho Chehab94a93e52013-01-23 17:06:02 -03001503 dev_dbg(fe->dvb->device,
1504 "%s: FE property %d doesn't exist\n",
1505 __func__, tvp->cmd);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001506 return -EINVAL;
Steven Toth363429a2008-09-12 01:34:28 -03001507 }
1508
Andreas Oberrittere23d9ae2011-05-08 20:03:36 -03001509 /* Allow the frontend to override outgoing properties */
1510 if (fe->ops.get_property) {
1511 r = fe->ops.get_property(fe, tvp);
1512 if (r < 0)
1513 return r;
1514 }
1515
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001516 dtv_property_dump(fe, false, tvp);
Mauro Carvalho Chehab639544d2009-12-31 17:49:32 -03001517
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001518 return 0;
Steven Toth363429a2008-09-12 01:34:28 -03001519}
1520
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001521static int dtv_set_frontend(struct dvb_frontend *fe);
1522
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001523static bool is_dvbv3_delsys(u32 delsys)
1524{
1525 bool status;
1526
1527 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1528 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1529
1530 return status;
1531}
1532
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001533/**
1534 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1535 * @fe: struct frontend;
1536 * @delsys: DVBv5 type that will be used for emulation
1537 *
1538 * Provides emulation for delivery systems that are compatible with the old
1539 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1540 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1541 * parameters are compatible with DVB-S spec.
1542 */
1543static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001544{
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001545 int i;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001546 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001547
Mauro Carvalho Chehab36264792012-04-17 18:32:19 -03001548 c->delivery_system = delsys;
1549
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001550 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001551 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001552 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001553 if (c->delivery_system == SYS_ISDBT) {
1554 dev_dbg(fe->dvb->device,
1555 "%s: Using defaults for SYS_ISDBT\n",
1556 __func__);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001557
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001558 if (!c->bandwidth_hz)
1559 c->bandwidth_hz = 6000000;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001560
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001561 c->isdbt_partial_reception = 0;
1562 c->isdbt_sb_mode = 0;
1563 c->isdbt_sb_subchannel = 0;
1564 c->isdbt_sb_segment_idx = 0;
1565 c->isdbt_sb_segment_count = 0;
1566 c->isdbt_layer_enabled = 7;
1567 for (i = 0; i < 3; i++) {
1568 c->layer[i].fec = FEC_AUTO;
1569 c->layer[i].modulation = QAM_AUTO;
1570 c->layer[i].interleaving = 0;
1571 c->layer[i].segment_count = 0;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001572 }
1573 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001574 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001575 __func__, c->delivery_system);
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001576
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001577 return 0;
1578}
1579
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001580/**
1581 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1582 * @fe: frontend struct
1583 * @desired_system: delivery system requested by the user
1584 *
1585 * A DVBv5 call know what's the desired system it wants. So, set it.
1586 *
1587 * There are, however, a few known issues with early DVBv5 applications that
1588 * are also handled by this logic:
1589 *
1590 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1591 * This is an API violation, but, as we don't want to break userspace,
1592 * convert it to the first supported delivery system.
1593 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1594 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1595 * ISDB-T provided backward compat with DVB-T.
1596 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001597static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1598 u32 desired_system)
1599{
1600 int ncaps;
1601 u32 delsys = SYS_UNDEFINED;
1602 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1603 enum dvbv3_emulation_type type;
1604
1605 /*
1606 * It was reported that some old DVBv5 applications were
1607 * filling delivery_system with SYS_UNDEFINED. If this happens,
1608 * assume that the application wants to use the first supported
1609 * delivery system.
1610 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001611 if (desired_system == SYS_UNDEFINED)
1612 desired_system = fe->ops.delsys[0];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001613
1614 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001615 * This is a DVBv5 call. So, it likely knows the supported
1616 * delivery systems. So, check if the desired delivery system is
1617 * supported
1618 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001619 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001620 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001621 if (fe->ops.delsys[ncaps] == desired_system) {
1622 c->delivery_system = desired_system;
1623 dev_dbg(fe->dvb->device,
1624 "%s: Changing delivery system to %d\n",
1625 __func__, desired_system);
1626 return 0;
1627 }
1628 ncaps++;
1629 }
1630
1631 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001632 * The requested delivery system isn't supported. Maybe userspace
1633 * is requesting a DVBv3 compatible delivery system.
1634 *
1635 * The emulation only works if the desired system is one of the
1636 * delivery systems supported by DVBv3 API
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001637 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001638 if (!is_dvbv3_delsys(desired_system)) {
1639 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001640 "%s: Delivery system %d not supported.\n",
1641 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001642 return -EINVAL;
1643 }
1644
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001645 type = dvbv3_type(desired_system);
1646
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001647 /*
1648 * Get the last non-DVBv3 delivery system that has the same type
1649 * of the desired system
1650 */
1651 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001652 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001653 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001654 delsys = fe->ops.delsys[ncaps];
1655 ncaps++;
1656 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001657
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001658 /* There's nothing compatible with the desired delivery system */
1659 if (delsys == SYS_UNDEFINED) {
1660 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001661 "%s: Delivery system %d not supported on emulation mode.\n",
1662 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001663 return -EINVAL;
1664 }
1665
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001666 dev_dbg(fe->dvb->device,
1667 "%s: Using delivery system %d emulated as if it were %d\n",
1668 __func__, delsys, desired_system);
1669
1670 return emulate_delivery_system(fe, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001671}
1672
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001673/**
1674 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1675 * @fe: frontend struct
1676 *
1677 * A DVBv3 call doesn't know what's the desired system it wants. It also
1678 * doesn't allow to switch between different types. Due to that, userspace
1679 * should use DVBv5 instead.
1680 * However, in order to avoid breaking userspace API, limited backward
1681 * compatibility support is provided.
1682 *
1683 * There are some delivery systems that are incompatible with DVBv3 calls.
1684 *
1685 * This routine should work fine for frontends that support just one delivery
1686 * system.
1687 *
1688 * For frontends that support multiple frontends:
1689 * 1) It defaults to use the first supported delivery system. There's an
1690 * userspace application that allows changing it at runtime;
1691 *
1692 * 2) If the current delivery system is not compatible with DVBv3, it gets
1693 * the first one that it is compatible.
1694 *
1695 * NOTE: in order for this to work with applications like Kaffeine that
1696 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1697 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1698 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1699 * to DVB-S.
1700 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001701static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1702{
1703 int ncaps;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001704 u32 delsys = SYS_UNDEFINED;
1705 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001706
1707 /* If not set yet, defaults to the first supported delivery system */
1708 if (c->delivery_system == SYS_UNDEFINED)
1709 c->delivery_system = fe->ops.delsys[0];
1710
1711 /*
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001712 * Trivial case: just use the current one, if it already a DVBv3
1713 * delivery system
1714 */
1715 if (is_dvbv3_delsys(c->delivery_system)) {
1716 dev_dbg(fe->dvb->device,
1717 "%s: Using delivery system to %d\n",
1718 __func__, c->delivery_system);
1719 return 0;
1720 }
1721
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001722 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001723 * Seek for the first delivery system that it is compatible with a
1724 * DVBv3 standard
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001725 */
1726 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001727 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001728 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1729 delsys = fe->ops.delsys[ncaps];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001730 break;
1731 }
1732 ncaps++;
1733 }
1734 if (delsys == SYS_UNDEFINED) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001735 dev_dbg(fe->dvb->device,
1736 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1737 __func__);
1738 return -EINVAL;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001739 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001740 return emulate_delivery_system(fe, delsys);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001741}
1742
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001743static int dtv_property_process_set(struct dvb_frontend *fe,
1744 struct dtv_property *tvp,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001745 struct file *file)
Steven Toth6b73eea2008-09-04 01:12:25 -03001746{
1747 int r = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -03001748 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001749
Steven Tothbfbf2da2008-09-12 01:37:37 -03001750 /* Allow the frontend to validate incoming properties */
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001751 if (fe->ops.set_property) {
Steven Tothbfbf2da2008-09-12 01:37:37 -03001752 r = fe->ops.set_property(fe, tvp);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001753 if (r < 0)
1754 return r;
1755 }
Steven Tothbfbf2da2008-09-12 01:37:37 -03001756
Mauro Carvalho Chehabc29ebb62016-02-03 17:29:41 -02001757 dtv_property_dump(fe, true, tvp);
1758
Steven Toth6b73eea2008-09-04 01:12:25 -03001759 switch(tvp->cmd) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001760 case DTV_CLEAR:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001761 /*
1762 * Reset a cache of data specific to the frontend here. This does
Steven Toth6b73eea2008-09-04 01:12:25 -03001763 * not effect hardware.
1764 */
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001765 dvb_frontend_clear_cache(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001766 break;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001767 case DTV_TUNE:
Steven Toth6b73eea2008-09-04 01:12:25 -03001768 /* interpret the cache of data, build either a traditional frontend
Steven Tothd5748f12008-09-12 00:27:13 -03001769 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1770 * ioctl.
Steven Toth6b73eea2008-09-04 01:12:25 -03001771 */
Andreas Oberritter50727712011-05-08 20:03:39 -03001772 c->state = tvp->cmd;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001773 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1774 __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001775
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001776 r = dtv_set_frontend(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001777 break;
Steven Toth363429a2008-09-12 01:34:28 -03001778 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001779 c->frequency = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001780 break;
Steven Toth363429a2008-09-12 01:34:28 -03001781 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001782 c->modulation = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001783 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001784 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001785 c->bandwidth_hz = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001786 break;
Steven Toth363429a2008-09-12 01:34:28 -03001787 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001788 c->inversion = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001789 break;
Steven Toth363429a2008-09-12 01:34:28 -03001790 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001791 c->symbol_rate = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001792 break;
Steven Toth363429a2008-09-12 01:34:28 -03001793 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001794 c->fec_inner = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001795 break;
Steven Toth363429a2008-09-12 01:34:28 -03001796 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001797 c->pilot = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001798 break;
Steven Toth363429a2008-09-12 01:34:28 -03001799 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001800 c->rolloff = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001801 break;
Steven Toth363429a2008-09-12 01:34:28 -03001802 case DTV_DELIVERY_SYSTEM:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001803 r = dvbv5_set_delivery_system(fe, tvp->u.data);
Steven Toth6b73eea2008-09-04 01:12:25 -03001804 break;
Steven Toth363429a2008-09-12 01:34:28 -03001805 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001806 c->voltage = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001807 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001808 (void *)c->voltage);
Steven Toth13c97bf2008-09-04 21:19:43 -03001809 break;
Steven Toth363429a2008-09-12 01:34:28 -03001810 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001811 c->sectone = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001812 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001813 (void *)c->sectone);
Steven Toth13c97bf2008-09-04 21:19:43 -03001814 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001815 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001816 c->code_rate_HP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001817 break;
1818 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001819 c->code_rate_LP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001820 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001821 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001822 c->guard_interval = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001823 break;
1824 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001825 c->transmission_mode = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001826 break;
Steven Tothef526f42008-10-06 22:01:47 -03001827 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001828 c->hierarchy = tvp->u.data;
Steven Tothef526f42008-10-06 22:01:47 -03001829 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001830 case DTV_INTERLEAVING:
1831 c->interleaving = tvp->u.data;
1832 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001833
1834 /* ISDB-T Support here */
1835 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001836 c->isdbt_partial_reception = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001837 break;
1838 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001839 c->isdbt_sb_mode = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001840 break;
1841 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001842 c->isdbt_sb_subchannel = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001843 break;
1844 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001845 c->isdbt_sb_segment_idx = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001846 break;
1847 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001848 c->isdbt_sb_segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001849 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001850 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001851 c->isdbt_layer_enabled = tvp->u.data;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001852 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001853 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001854 c->layer[0].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001855 break;
1856 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001857 c->layer[0].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001858 break;
1859 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001860 c->layer[0].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001861 break;
1862 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001863 c->layer[0].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001864 break;
1865 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001866 c->layer[1].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001867 break;
1868 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001869 c->layer[1].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001870 break;
1871 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001872 c->layer[1].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001873 break;
1874 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001875 c->layer[1].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001876 break;
1877 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001878 c->layer[2].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001879 break;
1880 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001881 c->layer[2].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001882 break;
1883 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001884 c->layer[2].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001885 break;
1886 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001887 c->layer[2].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001888 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001889
1890 /* Multistream support */
1891 case DTV_STREAM_ID:
1892 case DTV_DVBT2_PLP_ID_LEGACY:
1893 c->stream_id = tvp->u.data;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001894 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001895
1896 /* ATSC-MH */
1897 case DTV_ATSCMH_PARADE_ID:
1898 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1899 break;
1900 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1901 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1902 break;
1903
Antti Palosaari33eebec2012-10-03 04:28:56 -03001904 case DTV_LNA:
1905 c->lna = tvp->u.data;
1906 if (fe->ops.set_lna)
1907 r = fe->ops.set_lna(fe);
Mauro Carvalho Chehabe6876692014-02-16 06:36:17 -03001908 if (r < 0)
1909 c->lna = LNA_AUTO;
Antti Palosaari33eebec2012-10-03 04:28:56 -03001910 break;
1911
Steven Toth363429a2008-09-12 01:34:28 -03001912 default:
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001913 return -EINVAL;
Steven Toth6b73eea2008-09-04 01:12:25 -03001914 }
1915
Steven Toth13c97bf2008-09-04 21:19:43 -03001916 return r;
Steven Toth6b73eea2008-09-04 01:12:25 -03001917}
1918
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001919static int dvb_frontend_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 unsigned int cmd, void *parg)
1921{
1922 struct dvb_device *dvbdev = file->private_data;
1923 struct dvb_frontend *fe = dvbdev->priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001924 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001925 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02001926 int err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001928 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
Juergen Lock6ae23222012-12-23 17:23:06 -03001929 if (down_interruptible(&fepriv->sem))
1930 return -ERESTARTSYS;
1931
Shuah Khan18ed2862014-07-12 13:44:12 -03001932 if (fe->exit != DVB_FE_NO_EXIT) {
Juergen Lock6ae23222012-12-23 17:23:06 -03001933 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return -ENODEV;
Juergen Lock6ae23222012-12-23 17:23:06 -03001935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1938 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
Juergen Lock6ae23222012-12-23 17:23:06 -03001939 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
1940 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 return -EPERM;
Juergen Lock6ae23222012-12-23 17:23:06 -03001942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Steven Toth13c97bf2008-09-04 21:19:43 -03001944 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001945 err = dvb_frontend_ioctl_properties(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001946 else {
Andreas Oberritter50727712011-05-08 20:03:39 -03001947 c->state = DTV_UNDEFINED;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001948 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001949 }
Steven Toth13c97bf2008-09-04 21:19:43 -03001950
1951 up(&fepriv->sem);
1952 return err;
1953}
1954
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001955static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001956 unsigned int cmd, void *parg)
1957{
1958 struct dvb_device *dvbdev = file->private_data;
1959 struct dvb_frontend *fe = dvbdev->priv;
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02001960 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001961 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Igor M. Liplianine6f9ec82008-09-12 14:49:06 -03001962 int err = 0;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001963
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03001964 struct dtv_properties *tvps = parg;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001965 struct dtv_property *tvp = NULL;
1966 int i;
Steven Toth13c97bf2008-09-04 21:19:43 -03001967
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001968 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Steven Toth13c97bf2008-09-04 21:19:43 -03001969
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03001970 if (cmd == FE_SET_PROPERTY) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001971 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1972 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001973
1974 /* Put an arbitrary limit on the number of messages that can
1975 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001976 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Tothe7fee0f32008-09-11 10:23:01 -03001977 return -EINVAL;
1978
Markus Elfringfb660922016-08-19 05:04:54 -03001979 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
1980 if (IS_ERR(tvp))
1981 return PTR_ERR(tvp);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001982
Steven Tothd48cb402008-09-26 00:16:25 -03001983 for (i = 0; i < tvps->num; i++) {
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001984 err = dtv_property_process_set(fe, tvp + i, file);
1985 if (err < 0)
1986 goto out;
1987 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03001988 }
Steven Tothe7fee0f32008-09-11 10:23:01 -03001989
Andreas Oberritter50727712011-05-08 20:03:39 -03001990 if (c->state == DTV_TUNE)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001991 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
Steven Tothbfbf2da2008-09-12 01:37:37 -03001992
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03001993 } else if (cmd == FE_GET_PROPERTY) {
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02001994 struct dtv_frontend_properties getp = fe->dtv_property_cache;
1995
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001996 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1997 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Toth363429a2008-09-12 01:34:28 -03001998
1999 /* Put an arbitrary limit on the number of messages that can
2000 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03002001 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Toth363429a2008-09-12 01:34:28 -03002002 return -EINVAL;
2003
Markus Elfringfb660922016-08-19 05:04:54 -03002004 tvp = memdup_user(tvps->props, tvps->num * sizeof(*tvp));
2005 if (IS_ERR(tvp))
2006 return PTR_ERR(tvp);
Steven Toth363429a2008-09-12 01:34:28 -03002007
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03002008 /*
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002009 * Let's use our own copy of property cache, in order to
2010 * avoid mangling with DTV zigzag logic, as drivers might
2011 * return crap, if they don't check if the data is available
2012 * before updating the properties cache.
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03002013 */
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002014 if (fepriv->state != FESTATE_IDLE) {
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002015 err = dtv_get_frontend(fe, &getp, NULL);
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002016 if (err < 0)
2017 goto out;
2018 }
Steven Tothd48cb402008-09-26 00:16:25 -03002019 for (i = 0; i < tvps->num; i++) {
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002020 err = dtv_property_process_get(fe, &getp, tvp + i, file);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01002021 if (err < 0)
2022 goto out;
2023 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03002024 }
Steven Toth363429a2008-09-12 01:34:28 -03002025
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03002026 if (copy_to_user((void __user *)tvps->props, tvp,
2027 tvps->num * sizeof(struct dtv_property))) {
Steven Toth363429a2008-09-12 01:34:28 -03002028 err = -EFAULT;
2029 goto out;
2030 }
2031
Steven Toth363429a2008-09-12 01:34:28 -03002032 } else
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002033 err = -EOPNOTSUPP;
Steven Toth363429a2008-09-12 01:34:28 -03002034
Steven Tothe7fee0f32008-09-11 10:23:01 -03002035out:
2036 kfree(tvp);
Steven Toth13c97bf2008-09-04 21:19:43 -03002037 return err;
2038}
2039
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002040static int dtv_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002041{
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002042 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2043 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2044 struct dvb_frontend_tune_settings fetunesettings;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002045 u32 rolloff = 0;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002046
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002047 if (dvb_frontend_check_parameters(fe) < 0)
2048 return -EINVAL;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002049
2050 /*
Chris Rankin556a0442012-04-06 18:38:18 -03002051 * Initialize output parameters to match the values given by
2052 * the user. FE_SET_FRONTEND triggers an initial frontend event
2053 * with status = 0, which copies output parameters to userspace.
2054 */
Mauro Carvalho Chehab2ea9a082016-02-04 09:45:14 -02002055 dtv_property_legacy_params_sync(fe, c, &fepriv->parameters_out);
Chris Rankin556a0442012-04-06 18:38:18 -03002056
2057 /*
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002058 * Be sure that the bandwidth will be filled for all
2059 * non-satellite systems, as tuners need to know what
2060 * low pass/Nyquist half filter should be applied, in
2061 * order to avoid inter-channel noise.
2062 *
2063 * ISDB-T and DVB-T/T2 already sets bandwidth.
2064 * ATSC and DVB-C don't set, so, the core should fill it.
2065 *
2066 * On DVB-C Annex A and C, the bandwidth is a function of
2067 * the roll-off and symbol rate. Annex B defines different
2068 * roll-off factors depending on the modulation. Fortunately,
2069 * Annex B is only used with 6MHz, so there's no need to
2070 * calculate it.
2071 *
2072 * While not officially supported, a side effect of handling it at
2073 * the cache level is that a program could retrieve the bandwidth
2074 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2075 */
2076 switch (c->delivery_system) {
2077 case SYS_ATSC:
2078 case SYS_DVBC_ANNEX_B:
2079 c->bandwidth_hz = 6000000;
2080 break;
2081 case SYS_DVBC_ANNEX_A:
2082 rolloff = 115;
2083 break;
2084 case SYS_DVBC_ANNEX_C:
2085 rolloff = 113;
2086 break;
Mauro Carvalho Chehabc623ed62014-08-22 10:30:17 -05002087 case SYS_DVBS:
2088 case SYS_TURBO:
Akihiro Tsukada8e281fa2014-10-31 10:19:39 -03002089 case SYS_ISDBS:
Mauro Carvalho Chehabc623ed62014-08-22 10:30:17 -05002090 rolloff = 135;
2091 break;
2092 case SYS_DVBS2:
2093 switch (c->rolloff) {
2094 case ROLLOFF_20:
2095 rolloff = 120;
2096 break;
2097 case ROLLOFF_25:
2098 rolloff = 125;
2099 break;
2100 default:
2101 case ROLLOFF_35:
2102 rolloff = 135;
2103 }
2104 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002105 default:
2106 break;
2107 }
2108 if (rolloff)
Antti Palosaari2a80f292015-04-14 15:12:54 -03002109 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100);
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002110
2111 /* force auto frequency inversion if requested */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002112 if (dvb_force_auto_inversion)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002113 c->inversion = INVERSION_AUTO;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002114
2115 /*
2116 * without hierarchical coding code_rate_LP is irrelevant,
2117 * so we tolerate the otherwise invalid FEC_NONE setting
2118 */
2119 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2120 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002121
2122 /* get frontend-specific tuning settings */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002123 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002124 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2125 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2126 fepriv->max_drift = fetunesettings.max_drift;
2127 fepriv->step_size = fetunesettings.step_size;
2128 } else {
2129 /* default values */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002130 switch (c->delivery_system) {
Mauro Carvalho Chehab910a5f22012-05-07 15:01:37 -03002131 case SYS_DVBS:
2132 case SYS_DVBS2:
2133 case SYS_ISDBS:
2134 case SYS_TURBO:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002135 case SYS_DVBC_ANNEX_A:
2136 case SYS_DVBC_ANNEX_C:
2137 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002138 fepriv->step_size = c->symbol_rate / 16000;
2139 fepriv->max_drift = c->symbol_rate / 2000;
2140 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002141 case SYS_DVBT:
2142 case SYS_DVBT2:
2143 case SYS_ISDBT:
Antti Palosaari224b6642012-08-12 22:33:21 -03002144 case SYS_DTMB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002145 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002146 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2147 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2148 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002149 default:
2150 /*
2151 * FIXME: This sounds wrong! if freqency_stepsize is
2152 * defined by the frontend, why not use it???
2153 */
2154 fepriv->min_delay = HZ / 20;
2155 fepriv->step_size = 0; /* no zigzag */
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002156 fepriv->max_drift = 0;
2157 break;
2158 }
2159 }
2160 if (dvb_override_tune_delay > 0)
2161 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2162
2163 fepriv->state = FESTATE_RETUNE;
2164
2165 /* Request the search algorithm to search */
2166 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2167
2168 dvb_frontend_clear_events(fe);
2169 dvb_frontend_add_event(fe, 0);
2170 dvb_frontend_wakeup(fe);
2171 fepriv->status = 0;
2172
2173 return 0;
2174}
2175
2176
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002177static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03002178 unsigned int cmd, void *parg)
2179{
2180 struct dvb_device *dvbdev = file->private_data;
2181 struct dvb_frontend *fe = dvbdev->priv;
2182 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002183 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002184 int err = -EOPNOTSUPP;
Steven Toth13c97bf2008-09-04 21:19:43 -03002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 switch (cmd) {
2187 case FE_GET_INFO: {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002188 struct dvb_frontend_info* info = parg;
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002189
Patrick Boettcherdea74862006-05-14 05:01:31 -03002190 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
Guillaume Audirac2030c032010-05-06 09:30:25 -03002191 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002193 /*
2194 * Associate the 4 delivery systems supported by DVBv3
2195 * API with their DVBv5 counterpart. For the other standards,
2196 * use the closest type, assuming that it would hopefully
2197 * work with a DVBv3 application.
2198 * It should be noticed that, on multi-frontend devices with
2199 * different types (terrestrial and cable, for example),
2200 * a pure DVBv3 application won't be able to use all delivery
2201 * systems. Yet, changing the DVBv5 cache to the other delivery
2202 * system should be enough for making it work.
2203 */
2204 switch (dvbv3_type(c->delivery_system)) {
2205 case DVBV3_QPSK:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002206 info->type = FE_QPSK;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002207 break;
2208 case DVBV3_ATSC:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002209 info->type = FE_ATSC;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002210 break;
2211 case DVBV3_QAM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002212 info->type = FE_QAM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002213 break;
2214 case DVBV3_OFDM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002215 info->type = FE_OFDM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002216 break;
2217 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002218 dev_err(fe->dvb->device,
2219 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2220 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002221 fe->ops.info.type = FE_OFDM;
2222 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002223 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2224 __func__, c->delivery_system, fe->ops.info.type);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002225
Malcolm Priestleyc9d57de2015-08-31 06:13:45 -03002226 /* Set CAN_INVERSION_AUTO bit on in other than oneshot mode */
2227 if (!(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT))
2228 info->caps |= FE_CAN_INVERSION_AUTO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 err = 0;
2230 break;
2231 }
2232
Peter Beutner6757ccc2005-07-07 17:57:36 -07002233 case FE_READ_STATUS: {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002234 enum fe_status *status = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002236 /* if retune was requested but hasn't occurred yet, prevent
Peter Beutner6757ccc2005-07-07 17:57:36 -07002237 * that user get signal state from previous tuning */
Janne Grunau01886252009-09-01 19:23:09 -03002238 if (fepriv->state == FESTATE_RETUNE ||
2239 fepriv->state == FESTATE_ERROR) {
Peter Beutner6757ccc2005-07-07 17:57:36 -07002240 err=0;
2241 *status = 0;
2242 break;
2243 }
2244
Patrick Boettcherdea74862006-05-14 05:01:31 -03002245 if (fe->ops.read_status)
2246 err = fe->ops.read_status(fe, status);
Peter Beutner6757ccc2005-07-07 17:57:36 -07002247 break;
2248 }
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 case FE_READ_BER:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002251 if (fe->ops.read_ber) {
2252 if (fepriv->thread)
2253 err = fe->ops.read_ber(fe, (__u32 *) parg);
2254 else
2255 err = -EAGAIN;
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 break;
2258
2259 case FE_READ_SIGNAL_STRENGTH:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002260 if (fe->ops.read_signal_strength) {
2261 if (fepriv->thread)
2262 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2263 else
2264 err = -EAGAIN;
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 break;
2267
2268 case FE_READ_SNR:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002269 if (fe->ops.read_snr) {
2270 if (fepriv->thread)
2271 err = fe->ops.read_snr(fe, (__u16 *) parg);
2272 else
2273 err = -EAGAIN;
2274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 break;
2276
2277 case FE_READ_UNCORRECTED_BLOCKS:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002278 if (fe->ops.read_ucblocks) {
2279 if (fepriv->thread)
2280 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2281 else
2282 err = -EAGAIN;
2283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 break;
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 case FE_DISEQC_RESET_OVERLOAD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002287 if (fe->ops.diseqc_reset_overload) {
2288 err = fe->ops.diseqc_reset_overload(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 fepriv->state = FESTATE_DISEQC;
2290 fepriv->status = 0;
2291 }
2292 break;
2293
2294 case FE_DISEQC_SEND_MASTER_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002295 if (fe->ops.diseqc_send_master_cmd) {
Dan Carpenter8d7e5062015-06-06 13:55:22 -03002296 struct dvb_diseqc_master_cmd *cmd = parg;
2297
2298 if (cmd->msg_len > sizeof(cmd->msg)) {
2299 err = -EINVAL;
2300 break;
2301 }
2302 err = fe->ops.diseqc_send_master_cmd(fe, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 fepriv->state = FESTATE_DISEQC;
2304 fepriv->status = 0;
2305 }
2306 break;
2307
2308 case FE_DISEQC_SEND_BURST:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002309 if (fe->ops.diseqc_send_burst) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002310 err = fe->ops.diseqc_send_burst(fe,
2311 (enum fe_sec_mini_cmd)parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 fepriv->state = FESTATE_DISEQC;
2313 fepriv->status = 0;
2314 }
2315 break;
2316
2317 case FE_SET_TONE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002318 if (fe->ops.set_tone) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002319 err = fe->ops.set_tone(fe,
2320 (enum fe_sec_tone_mode)parg);
2321 fepriv->tone = (enum fe_sec_tone_mode)parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 fepriv->state = FESTATE_DISEQC;
2323 fepriv->status = 0;
2324 }
2325 break;
2326
2327 case FE_SET_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002328 if (fe->ops.set_voltage) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002329 err = fe->ops.set_voltage(fe,
2330 (enum fe_sec_voltage)parg);
2331 fepriv->voltage = (enum fe_sec_voltage)parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 fepriv->state = FESTATE_DISEQC;
2333 fepriv->status = 0;
2334 }
2335 break;
2336
2337 case FE_DISHNETWORK_SEND_LEGACY_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002338 if (fe->ops.dishnetwork_send_legacy_command) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002339 err = fe->ops.dishnetwork_send_legacy_command(fe,
2340 (unsigned long)parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 fepriv->state = FESTATE_DISEQC;
2342 fepriv->status = 0;
Patrick Boettcherdea74862006-05-14 05:01:31 -03002343 } else if (fe->ops.set_voltage) {
NooneImportant83b75b02005-11-08 21:35:27 -08002344 /*
2345 * NOTE: This is a fallback condition. Some frontends
2346 * (stv0299 for instance) take longer than 8msec to
2347 * respond to a set_voltage command. Those switches
2348 * need custom routines to switch properly. For all
Guillaume Audirac2030c032010-05-06 09:30:25 -03002349 * other frontends, the following should work ok.
NooneImportant83b75b02005-11-08 21:35:27 -08002350 * Dish network legacy switches (as used by Dish500)
2351 * are controlled by sending 9-bit command words
2352 * spaced 8msec apart.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002353 * the actual command word is switch/port dependent
NooneImportant83b75b02005-11-08 21:35:27 -08002354 * so it is up to the userspace application to send
2355 * the right command.
2356 * The command must always start with a '0' after
2357 * initialization, so parg is 8 bits and does not
2358 * include the initialization or start bit
2359 */
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002360 unsigned long swcmd = ((unsigned long) parg) << 1;
Tina Ruchandani9056a232015-05-31 04:17:06 -03002361 ktime_t nexttime;
2362 ktime_t tv[10];
NooneImportant83b75b02005-11-08 21:35:27 -08002363 int i;
2364 u8 last = 1;
2365 if (dvb_frontend_debug)
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002366 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
Abhilash Jindal6b3f9992016-01-31 03:47:31 -02002367 nexttime = ktime_get_boottime();
NooneImportant83b75b02005-11-08 21:35:27 -08002368 if (dvb_frontend_debug)
Ezequiel Garciab9b1b3a2012-10-23 15:57:23 -03002369 tv[0] = nexttime;
NooneImportant83b75b02005-11-08 21:35:27 -08002370 /* before sending a command, initialize by sending
2371 * a 32ms 18V to the switch
2372 */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002373 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002374 dvb_frontend_sleep_until(&nexttime, 32000);
2375
2376 for (i = 0; i < 9; i++) {
2377 if (dvb_frontend_debug)
Abhilash Jindal6b3f9992016-01-31 03:47:31 -02002378 tv[i+1] = ktime_get_boottime();
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002379 if ((swcmd & 0x01) != last) {
NooneImportant83b75b02005-11-08 21:35:27 -08002380 /* set voltage to (last ? 13V : 18V) */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002381 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002382 last = (last) ? 0 : 1;
2383 }
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002384 swcmd = swcmd >> 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002385 if (i != 8)
2386 dvb_frontend_sleep_until(&nexttime, 8000);
2387 }
2388 if (dvb_frontend_debug) {
2389 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002390 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -08002391 for (i = 1; i < 10; i++)
Tina Ruchandani9056a232015-05-31 04:17:06 -03002392 printk("%d: %d\n", i,
2393 (int) ktime_us_delta(tv[i], tv[i-1]));
NooneImportant83b75b02005-11-08 21:35:27 -08002394 }
2395 err = 0;
2396 fepriv->state = FESTATE_DISEQC;
2397 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
2399 break;
2400
2401 case FE_DISEQC_RECV_SLAVE_REPLY:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002402 if (fe->ops.diseqc_recv_slave_reply)
2403 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 break;
2405
2406 case FE_ENABLE_HIGH_LNB_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002407 if (fe->ops.enable_high_lnb_voltage)
2408 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 break;
2410
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002411 case FE_SET_FRONTEND:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002412 err = dvbv3_set_delivery_system(fe);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03002413 if (err)
2414 break;
2415
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002416 err = dtv_property_cache_sync(fe, c, parg);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002417 if (err)
2418 break;
2419 err = dtv_set_frontend(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 case FE_GET_EVENT:
2422 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2423 break;
2424
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002425 case FE_GET_FRONTEND: {
2426 struct dtv_frontend_properties getp = fe->dtv_property_cache;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002427
Mauro Carvalho Chehabbb31d232016-02-04 13:26:22 -02002428 /*
2429 * Let's use our own copy of property cache, in order to
2430 * avoid mangling with DTV zigzag logic, as drivers might
2431 * return crap, if they don't check if the data is available
2432 * before updating the properties cache.
2433 */
2434 err = dtv_get_frontend(fe, &getp, parg);
2435 break;
2436 }
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002437 case FE_SET_FRONTEND_TUNE_MODE:
Hans Verkuile18828e2006-01-09 15:25:28 -02002438 fepriv->tune_mode_flags = (unsigned long) parg;
Trent Piepho1b172e02006-06-29 13:16:04 -03002439 err = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002440 break;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03002441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return err;
2444}
2445
Steven Toth6b73eea2008-09-04 01:12:25 -03002446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2448{
2449 struct dvb_device *dvbdev = file->private_data;
2450 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002451 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002453 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 poll_wait (file, &fepriv->events.wait_queue, wait);
2456
2457 if (fepriv->events.eventw != fepriv->events.eventr)
2458 return (POLLIN | POLLRDNORM | POLLPRI);
2459
2460 return 0;
2461}
2462
2463static int dvb_frontend_open(struct inode *inode, struct file *file)
2464{
2465 struct dvb_device *dvbdev = file->private_data;
2466 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002467 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Darron Broad59b18422008-10-11 11:44:05 -03002468 struct dvb_adapter *adapter = fe->dvb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 int ret;
2470
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002471 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Shuah Khan18ed2862014-07-12 13:44:12 -03002472 if (fe->exit == DVB_FE_DEVICE_REMOVED)
matthieu castete36309f2010-05-05 15:59:20 -03002473 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Darron Broad59b18422008-10-11 11:44:05 -03002475 if (adapter->mfe_shared) {
2476 mutex_lock (&adapter->mfe_lock);
Darron Broad65946902008-10-15 13:37:59 -03002477
2478 if (adapter->mfe_dvbdev == NULL)
2479 adapter->mfe_dvbdev = dvbdev;
2480
2481 else if (adapter->mfe_dvbdev != dvbdev) {
2482 struct dvb_device
2483 *mfedev = adapter->mfe_dvbdev;
2484 struct dvb_frontend
2485 *mfe = mfedev->priv;
2486 struct dvb_frontend_private
2487 *mfepriv = mfe->frontend_priv;
2488 int mferetry = (dvb_mfe_wait_time << 1);
2489
2490 mutex_unlock (&adapter->mfe_lock);
2491 while (mferetry-- && (mfedev->users != -1 ||
2492 mfepriv->thread != NULL)) {
2493 if(msleep_interruptible(500)) {
2494 if(signal_pending(current))
2495 return -EINTR;
Darron Broad59b18422008-10-11 11:44:05 -03002496 }
2497 }
Darron Broad65946902008-10-15 13:37:59 -03002498
2499 mutex_lock (&adapter->mfe_lock);
2500 if(adapter->mfe_dvbdev != dvbdev) {
2501 mfedev = adapter->mfe_dvbdev;
2502 mfe = mfedev->priv;
2503 mfepriv = mfe->frontend_priv;
2504 if (mfedev->users != -1 ||
2505 mfepriv->thread != NULL) {
2506 mutex_unlock (&adapter->mfe_lock);
2507 return -EBUSY;
2508 }
2509 adapter->mfe_dvbdev = dvbdev;
2510 }
Darron Broad59b18422008-10-11 11:44:05 -03002511 }
2512 }
2513
Oliver Endriss48136e12007-08-25 12:00:23 -03002514 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2515 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
Darron Broad59b18422008-10-11 11:44:05 -03002516 goto err0;
Devin Heitmueller2d196932011-07-04 21:55:01 -03002517
2518 /* If we took control of the bus, we need to force
2519 reinitialization. This is because many ts_bus_ctrl()
2520 functions strobe the RESET pin on the demod, and if the
2521 frontend thread already exists then the dvb_init() routine
2522 won't get called (which is what usually does initial
2523 register configuration). */
2524 fepriv->reinitialise = 1;
Steven Tothba7e6f32006-09-25 12:41:53 -03002525 }
2526
Oliver Endriss48136e12007-08-25 12:00:23 -03002527 if ((ret = dvb_generic_open (inode, file)) < 0)
2528 goto err1;
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002529
Oliver Endriss48136e12007-08-25 12:00:23 -03002530 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002531 /* normal tune mode when opened R/W */
2532 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2533 fepriv->tone = -1;
2534 fepriv->voltage = -1;
2535
Shuah Khan68ff5762016-02-11 21:41:36 -02002536#ifdef CONFIG_MEDIA_CONTROLLER_DVB
2537 if (fe->dvb->mdev && fe->dvb->mdev->enable_source) {
2538 ret = fe->dvb->mdev->enable_source(dvbdev->entity,
2539 &fepriv->pipe);
2540 if (ret) {
2541 dev_err(fe->dvb->device,
2542 "Tuner is busy. Error %d\n", ret);
2543 goto err2;
2544 }
2545 }
2546#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 ret = dvb_frontend_start (fe);
2548 if (ret)
Shuah Khan68ff5762016-02-11 21:41:36 -02002549 goto err3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 /* empty event queue */
2552 fepriv->events.eventr = fepriv->events.eventw = 0;
2553 }
2554
Max Kellermannfe356372016-07-04 09:08:51 -03002555 dvb_frontend_private_get(fepriv);
2556
Darron Broad59b18422008-10-11 11:44:05 -03002557 if (adapter->mfe_shared)
2558 mutex_unlock (&adapter->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 return ret;
Oliver Endriss48136e12007-08-25 12:00:23 -03002560
Shuah Khan68ff5762016-02-11 21:41:36 -02002561err3:
2562#ifdef CONFIG_MEDIA_CONTROLLER_DVB
2563 if (fe->dvb->mdev && fe->dvb->mdev->disable_source)
2564 fe->dvb->mdev->disable_source(dvbdev->entity);
Oliver Endriss48136e12007-08-25 12:00:23 -03002565err2:
Shuah Khan68ff5762016-02-11 21:41:36 -02002566#endif
Oliver Endriss48136e12007-08-25 12:00:23 -03002567 dvb_generic_release(inode, file);
2568err1:
2569 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2570 fe->ops.ts_bus_ctrl(fe, 0);
Darron Broad59b18422008-10-11 11:44:05 -03002571err0:
2572 if (adapter->mfe_shared)
2573 mutex_unlock (&adapter->mfe_lock);
Oliver Endriss48136e12007-08-25 12:00:23 -03002574 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
2577static int dvb_frontend_release(struct inode *inode, struct file *file)
2578{
2579 struct dvb_device *dvbdev = file->private_data;
2580 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002581 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002582 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002584 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002586 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 fepriv->release_jiffies = jiffies;
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002588 mb();
2589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002591 ret = dvb_generic_release (inode, file);
2592
Oliver Endriss48136e12007-08-25 12:00:23 -03002593 if (dvbdev->users == -1) {
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002594 wake_up(&fepriv->wait_queue);
Shuah Khan68ff5762016-02-11 21:41:36 -02002595#ifdef CONFIG_MEDIA_CONTROLLER_DVB
2596 if (fe->dvb->mdev && fe->dvb->mdev->disable_source)
2597 fe->dvb->mdev->disable_source(dvbdev->entity);
2598#endif
Shuah Khan18ed2862014-07-12 13:44:12 -03002599 if (fe->exit != DVB_FE_NO_EXIT)
Oliver Endriss48136e12007-08-25 12:00:23 -03002600 wake_up(&dvbdev->wait_queue);
Oliver Endriss48136e12007-08-25 12:00:23 -03002601 if (fe->ops.ts_bus_ctrl)
2602 fe->ops.ts_bus_ctrl(fe, 0);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002603 }
Oliver Endriss48136e12007-08-25 12:00:23 -03002604
Max Kellermannfe356372016-07-04 09:08:51 -03002605 dvb_frontend_private_put(fepriv);
2606
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
Jan Engelhardt784e29d2009-01-11 06:12:43 -03002610static const struct file_operations dvb_frontend_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 .owner = THIS_MODULE,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002612 .unlocked_ioctl = dvb_generic_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 .poll = dvb_frontend_poll,
2614 .open = dvb_frontend_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002615 .release = dvb_frontend_release,
2616 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617};
2618
Antti Palosaari06bae122012-08-14 22:21:06 -03002619int dvb_frontend_suspend(struct dvb_frontend *fe)
2620{
2621 int ret = 0;
2622
2623 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2624 fe->id);
2625
Mauro Carvalho Chehab59d78892014-08-09 21:47:19 -03002626 if (fe->ops.tuner_ops.suspend)
2627 ret = fe->ops.tuner_ops.suspend(fe);
2628 else if (fe->ops.tuner_ops.sleep)
Antti Palosaari06bae122012-08-14 22:21:06 -03002629 ret = fe->ops.tuner_ops.sleep(fe);
2630
2631 if (fe->ops.sleep)
2632 ret = fe->ops.sleep(fe);
2633
2634 return ret;
2635}
2636EXPORT_SYMBOL(dvb_frontend_suspend);
2637
2638int dvb_frontend_resume(struct dvb_frontend *fe)
2639{
2640 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2641 int ret = 0;
2642
2643 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2644 fe->id);
2645
Shuah Khan87cd0fa2014-07-24 13:02:14 -03002646 fe->exit = DVB_FE_DEVICE_RESUME;
Antti Palosaari06bae122012-08-14 22:21:06 -03002647 if (fe->ops.init)
2648 ret = fe->ops.init(fe);
2649
Mauro Carvalho Chehab59d78892014-08-09 21:47:19 -03002650 if (fe->ops.tuner_ops.resume)
2651 ret = fe->ops.tuner_ops.resume(fe);
2652 else if (fe->ops.tuner_ops.init)
Antti Palosaari06bae122012-08-14 22:21:06 -03002653 ret = fe->ops.tuner_ops.init(fe);
2654
Mauro Carvalho Chehab3663b312015-11-10 11:50:30 -02002655 if (fe->ops.set_tone && fepriv->tone != -1)
2656 fe->ops.set_tone(fe, fepriv->tone);
2657 if (fe->ops.set_voltage && fepriv->voltage != -1)
2658 fe->ops.set_voltage(fe, fepriv->voltage);
2659
Shuah Khan87cd0fa2014-07-24 13:02:14 -03002660 fe->exit = DVB_FE_NO_EXIT;
Antti Palosaari06bae122012-08-14 22:21:06 -03002661 fepriv->state = FESTATE_RETUNE;
2662 dvb_frontend_wakeup(fe);
2663
2664 return ret;
2665}
2666EXPORT_SYMBOL(dvb_frontend_resume);
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668int dvb_register_frontend(struct dvb_adapter* dvb,
2669 struct dvb_frontend* fe)
2670{
2671 struct dvb_frontend_private *fepriv;
Mauro Carvalho Chehabc1b31b12015-01-02 11:00:17 -03002672 const struct dvb_device dvbdev_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 .users = ~0,
2674 .writers = 1,
2675 .readers = (~0)-1,
2676 .fops = &dvb_frontend_fops,
Mauro Carvalho Chehabc1b31b12015-01-02 11:00:17 -03002677#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2678 .name = fe->ops.info.name,
2679#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 .kernel_ioctl = dvb_frontend_ioctl
2681 };
2682
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002683 dev_dbg(dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Ingo Molnar3593cab2006-02-07 06:49:14 -02002685 if (mutex_lock_interruptible(&frontend_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return -ERESTARTSYS;
2687
Panagiotis Issaris74081872006-01-11 19:40:56 -02002688 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (fe->frontend_priv == NULL) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002690 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return -ENOMEM;
2692 }
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002693 fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Max Kellermannfe356372016-07-04 09:08:51 -03002695 kref_init(&fepriv->refcount);
2696
Thomas Gleixnera0a47142010-09-07 11:33:27 -03002697 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 init_waitqueue_head (&fepriv->wait_queue);
2699 init_waitqueue_head (&fepriv->events.wait_queue);
Matthias Kaehlcke03b76122007-07-30 14:58:10 -03002700 mutex_init(&fepriv->events.mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 fe->dvb = dvb;
2702 fepriv->inversion = INVERSION_OFF;
2703
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002704 dev_info(fe->dvb->device,
2705 "DVB: registering adapter %i frontend %i (%s)...\n",
2706 fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
Mauro Carvalho Chehabdf2f94e2015-08-21 16:18:18 -03002709 fe, DVB_DEVICE_FRONTEND, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002711 /*
2712 * Initialize the cache to the proper values according with the
2713 * first supported delivery system (ops->delsys[0])
2714 */
Mauro Carvalho Chehab240ab502012-01-10 18:00:50 -03002715
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002716 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002717 dvb_frontend_clear_cache(fe);
2718
Ingo Molnar3593cab2006-02-07 06:49:14 -02002719 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return 0;
2721}
2722EXPORT_SYMBOL(dvb_register_frontend);
2723
2724int dvb_unregister_frontend(struct dvb_frontend* fe)
2725{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002726 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002727 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Markus Rechberger57861b42007-04-14 10:19:18 -03002729 mutex_lock(&frontend_mutex);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002730 dvb_frontend_stop (fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 dvb_unregister_device (fepriv->dvbdev);
Andrew de Quinceyd9955062006-08-08 09:10:08 -03002732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /* fe is invalid now */
Ingo Molnar3593cab2006-02-07 06:49:14 -02002734 mutex_unlock(&frontend_mutex);
Max Kellermannfe356372016-07-04 09:08:51 -03002735 dvb_frontend_private_put(fepriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return 0;
2737}
2738EXPORT_SYMBOL(dvb_unregister_frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002739
Mauro Carvalho Chehab149ef722008-04-29 21:38:46 -03002740#ifdef CONFIG_MEDIA_ATTACH
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002741void dvb_frontend_detach(struct dvb_frontend* fe)
2742{
2743 void *ptr;
2744
2745 if (fe->ops.release_sec) {
2746 fe->ops.release_sec(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002747 dvb_detach(fe->ops.release_sec);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002748 }
2749 if (fe->ops.tuner_ops.release) {
2750 fe->ops.tuner_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002751 dvb_detach(fe->ops.tuner_ops.release);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002752 }
Michael Krufky2426a272007-12-21 11:34:45 -03002753 if (fe->ops.analog_ops.release) {
2754 fe->ops.analog_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002755 dvb_detach(fe->ops.analog_ops.release);
Michael Krufky2426a272007-12-21 11:34:45 -03002756 }
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002757 ptr = (void*)fe->ops.release;
2758 if (ptr) {
2759 fe->ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002760 dvb_detach(ptr);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002761 }
2762}
2763#else
2764void dvb_frontend_detach(struct dvb_frontend* fe)
2765{
2766 if (fe->ops.release_sec)
2767 fe->ops.release_sec(fe);
2768 if (fe->ops.tuner_ops.release)
2769 fe->ops.tuner_ops.release(fe);
Michael Krufky2426a272007-12-21 11:34:45 -03002770 if (fe->ops.analog_ops.release)
2771 fe->ops.analog_ops.release(fe);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002772 if (fe->ops.release)
2773 fe->ops.release(fe);
2774}
2775#endif
2776EXPORT_SYMBOL(dvb_frontend_detach);