blob: d764cffb2102c10ffcd8134c8ecf920137f0bb99 [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 {
102
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200103 /* thread/frontend values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct dvb_device *dvbdev;
Andreas Oberrittera5959db2011-05-08 20:03:40 -0300105 struct dvb_frontend_parameters parameters_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct dvb_fe_events events;
107 struct semaphore sem;
108 struct list_head list_head;
109 wait_queue_head_t wait_queue;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300110 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long release_jiffies;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200112 unsigned int wakeup;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300113 enum fe_status status;
Peter Beutner400b7082006-01-09 15:32:43 -0200114 unsigned long tune_mode_flags;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200115 unsigned int delay;
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300116 unsigned int reinitialise;
Andrew de Quincey64454012006-04-06 14:32:23 -0300117 int tone;
118 int voltage;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200119
120 /* swzigzag values */
121 unsigned int state;
122 unsigned int bending;
123 int lnb_drift;
124 unsigned int inversion;
125 unsigned int auto_step;
126 unsigned int auto_sub_step;
127 unsigned int started_auto_step;
128 unsigned int min_delay;
129 unsigned int max_drift;
130 unsigned int step_size;
131 int quality;
132 unsigned int check_wrapped;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300133 enum dvbfe_search algo_status;
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300134
135#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
136 struct media_pipeline pipe;
137 struct media_entity *pipe_start_entity;
138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139};
140
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300141static void dvb_frontend_wakeup(struct dvb_frontend *fe);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300142static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300143 struct dvb_frontend_parameters *p_out);
Chris Rankin556a0442012-04-06 18:38:18 -0300144static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
145 struct dvb_frontend_parameters *p);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300146
147static bool has_get_frontend(struct dvb_frontend *fe)
148{
Hans Petter Selaskyc065f5b2012-03-27 12:53:19 -0300149 return fe->ops.get_frontend != NULL;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300150}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300152/*
153 * Due to DVBv3 API calls, a delivery system should be mapped into one of
154 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
155 * otherwise, a DVBv3 call will fail.
156 */
157enum dvbv3_emulation_type {
158 DVBV3_UNKNOWN,
159 DVBV3_QPSK,
160 DVBV3_QAM,
161 DVBV3_OFDM,
162 DVBV3_ATSC,
163};
164
165static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
166{
167 switch (delivery_system) {
168 case SYS_DVBC_ANNEX_A:
169 case SYS_DVBC_ANNEX_C:
170 return DVBV3_QAM;
171 case SYS_DVBS:
172 case SYS_DVBS2:
173 case SYS_TURBO:
174 case SYS_ISDBS:
175 case SYS_DSS:
176 return DVBV3_QPSK;
177 case SYS_DVBT:
178 case SYS_DVBT2:
179 case SYS_ISDBT:
Antti Palosaari224b6642012-08-12 22:33:21 -0300180 case SYS_DTMB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300181 return DVBV3_OFDM;
182 case SYS_ATSC:
Michael Krufkyca689482012-01-29 15:44:58 -0300183 case SYS_ATSCMH:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300184 case SYS_DVBC_ANNEX_B:
185 return DVBV3_ATSC;
186 case SYS_UNDEFINED:
187 case SYS_ISDBC:
188 case SYS_DVBH:
189 case SYS_DAB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300190 default:
191 /*
192 * Doesn't know how to emulate those types and/or
193 * there's no frontend driver from this type yet
194 * with some emulation code, so, we're not sure yet how
195 * to handle them, or they're not compatible with a DVBv3 call.
196 */
197 return DVBV3_UNKNOWN;
198 }
199}
200
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300201static void dvb_frontend_add_event(struct dvb_frontend *fe,
202 enum fe_status status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700204 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct dvb_fe_events *events = &fepriv->events;
206 struct dvb_frontend_event *e;
207 int wp;
208
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300209 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300211 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300212 dtv_get_frontend(fe, &fepriv->parameters_out);
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300213
214 mutex_lock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 wp = (events->eventw + 1) % MAX_EVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (wp == events->eventr) {
218 events->overflow = 1;
219 events->eventr = (events->eventr + 1) % MAX_EVENT;
220 }
221
222 e = &events->events[events->eventw];
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300223 e->status = status;
Andreas Oberrittera5959db2011-05-08 20:03:40 -0300224 e->parameters = fepriv->parameters_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 events->eventw = wp;
227
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300228 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 wake_up_interruptible (&events->wait_queue);
231}
232
233static int dvb_frontend_get_event(struct dvb_frontend *fe,
234 struct dvb_frontend_event *event, int flags)
235{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700236 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 struct dvb_fe_events *events = &fepriv->events;
238
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300239 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (events->overflow) {
242 events->overflow = 0;
243 return -EOVERFLOW;
244 }
245
246 if (events->eventw == events->eventr) {
247 int ret;
248
249 if (flags & O_NONBLOCK)
250 return -EWOULDBLOCK;
251
252 up(&fepriv->sem);
253
254 ret = wait_event_interruptible (events->wait_queue,
255 events->eventw != events->eventr);
256
257 if (down_interruptible (&fepriv->sem))
258 return -ERESTARTSYS;
259
260 if (ret < 0)
261 return ret;
262 }
263
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300264 mutex_lock(&events->mtx);
265 *event = events->events[events->eventr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 events->eventr = (events->eventr + 1) % MAX_EVENT;
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300267 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 return 0;
270}
271
Andreas Oberritter20640be2011-08-04 12:33:14 -0300272static void dvb_frontend_clear_events(struct dvb_frontend *fe)
273{
274 struct dvb_frontend_private *fepriv = fe->frontend_priv;
275 struct dvb_fe_events *events = &fepriv->events;
276
277 mutex_lock(&events->mtx);
278 events->eventr = events->eventw;
279 mutex_unlock(&events->mtx);
280}
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282static void dvb_frontend_init(struct dvb_frontend *fe)
283{
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300284 dev_dbg(fe->dvb->device,
285 "%s: initialising adapter %i frontend %i (%s)...\n",
286 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Patrick Boettcherdea74862006-05-14 05:01:31 -0300288 if (fe->ops.init)
289 fe->ops.init(fe);
290 if (fe->ops.tuner_ops.init) {
Manu Abraham3b37a152008-10-20 18:14:14 -0300291 if (fe->ops.i2c_gate_ctrl)
292 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300293 fe->ops.tuner_ops.init(fe);
294 if (fe->ops.i2c_gate_ctrl)
295 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300299void dvb_frontend_reinitialise(struct dvb_frontend *fe)
300{
301 struct dvb_frontend_private *fepriv = fe->frontend_priv;
302
303 fepriv->reinitialise = 1;
304 dvb_frontend_wakeup(fe);
305}
306EXPORT_SYMBOL(dvb_frontend_reinitialise);
307
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200308static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200310 int q2;
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300311 struct dvb_frontend *fe = fepriv->dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300313 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200315 if (locked)
316 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
317 else
318 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200320 q2 = fepriv->quality - 128;
321 q2 *= q2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200323 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/**
327 * Performs automatic twiddling of frontend parameters.
328 *
329 * @param fe The frontend concerned.
330 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
331 * @returns Number of complete iterations that have been performed.
332 */
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200333static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 int autoinversion;
336 int ready = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300337 int fe_set_err = 0;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700338 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300339 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
340 int original_inversion = c->inversion;
341 u32 original_frequency = c->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* are we using autoinversion? */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300344 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300345 (c->inversion == INVERSION_AUTO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* setup parameters correctly */
348 while(!ready) {
349 /* calculate the lnb_drift */
350 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
351
352 /* wrap the auto_step if we've exceeded the maximum drift */
353 if (fepriv->lnb_drift > fepriv->max_drift) {
354 fepriv->auto_step = 0;
355 fepriv->auto_sub_step = 0;
356 fepriv->lnb_drift = 0;
357 }
358
359 /* perform inversion and +/- zigzag */
360 switch(fepriv->auto_sub_step) {
361 case 0:
362 /* try with the current inversion and current drift setting */
363 ready = 1;
364 break;
365
366 case 1:
367 if (!autoinversion) break;
368
369 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
370 ready = 1;
371 break;
372
373 case 2:
374 if (fepriv->lnb_drift == 0) break;
375
376 fepriv->lnb_drift = -fepriv->lnb_drift;
377 ready = 1;
378 break;
379
380 case 3:
381 if (fepriv->lnb_drift == 0) break;
382 if (!autoinversion) break;
383
384 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
385 fepriv->lnb_drift = -fepriv->lnb_drift;
386 ready = 1;
387 break;
388
389 default:
390 fepriv->auto_step++;
391 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
392 break;
393 }
394
395 if (!ready) fepriv->auto_sub_step++;
396 }
397
398 /* if this attempt would hit where we started, indicate a complete
399 * iteration has occurred */
400 if ((fepriv->auto_step == fepriv->started_auto_step) &&
401 (fepriv->auto_sub_step == 0) && check_wrapped) {
402 return 1;
403 }
404
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300405 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
406 "auto_sub_step:%i started_auto_step:%i\n",
407 __func__, fepriv->lnb_drift, fepriv->inversion,
408 fepriv->auto_step, fepriv->auto_sub_step,
409 fepriv->started_auto_step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* set the frontend itself */
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300412 c->frequency += fepriv->lnb_drift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (autoinversion)
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300414 c->inversion = fepriv->inversion;
415 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300416 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300417 fe_set_err = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300418 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300419 if (fe_set_err < 0) {
420 fepriv->state = FESTATE_ERROR;
421 return fe_set_err;
422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300424 c->frequency = original_frequency;
425 c->inversion = original_inversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 fepriv->auto_sub_step++;
428 return 0;
429}
430
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200431static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
432{
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300433 enum fe_status s = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300434 int retval = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200435 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300436 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200437
438 /* if we've got no parameters, just keep idling */
439 if (fepriv->state & FESTATE_IDLE) {
440 fepriv->delay = 3*HZ;
441 fepriv->quality = 0;
442 return;
443 }
444
445 /* in SCAN mode, we just set the frontend when asked and leave it alone */
446 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
447 if (fepriv->state & FESTATE_RETUNE) {
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300448 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300449 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300450 retval = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300451 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300452 if (retval < 0)
453 fepriv->state = FESTATE_ERROR;
454 else
455 fepriv->state = FESTATE_TUNED;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200456 }
457 fepriv->delay = 3*HZ;
458 fepriv->quality = 0;
459 return;
460 }
461
462 /* get the frontend status */
463 if (fepriv->state & FESTATE_RETUNE) {
464 s = 0;
465 } else {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300466 if (fe->ops.read_status)
467 fe->ops.read_status(fe, &s);
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200468 if (s != fepriv->status) {
469 dvb_frontend_add_event(fe, s);
470 fepriv->status = s;
471 }
472 }
473
474 /* if we're not tuned, and we have a lock, move to the TUNED state */
475 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
476 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
477 fepriv->state = FESTATE_TUNED;
478
479 /* if we're tuned, then we have determined the correct inversion */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300480 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300481 (c->inversion == INVERSION_AUTO)) {
482 c->inversion = fepriv->inversion;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200483 }
484 return;
485 }
486
487 /* if we are tuned already, check we're still locked */
488 if (fepriv->state & FESTATE_TUNED) {
489 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
490
491 /* we're tuned, and the lock is still good... */
492 if (s & FE_HAS_LOCK) {
493 return;
494 } else { /* if we _WERE_ tuned, but now don't have a lock */
495 fepriv->state = FESTATE_ZIGZAG_FAST;
496 fepriv->started_auto_step = fepriv->auto_step;
497 fepriv->check_wrapped = 0;
498 }
499 }
500
501 /* don't actually do anything if we're in the LOSTLOCK state,
502 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
503 if ((fepriv->state & FESTATE_LOSTLOCK) &&
Patrick Boettcherdea74862006-05-14 05:01:31 -0300504 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200505 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
506 return;
507 }
508
509 /* don't do anything if we're in the DISEQC state, since this
510 * might be someone with a motorized dish controlled by DISEQC.
511 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
512 if (fepriv->state & FESTATE_DISEQC) {
513 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
514 return;
515 }
516
517 /* if we're in the RETUNE state, set everything up for a brand
518 * new scan, keeping the current inversion setting, as the next
519 * tune is _very_ likely to require the same */
520 if (fepriv->state & FESTATE_RETUNE) {
521 fepriv->lnb_drift = 0;
522 fepriv->auto_step = 0;
523 fepriv->auto_sub_step = 0;
524 fepriv->started_auto_step = 0;
525 fepriv->check_wrapped = 0;
526 }
527
528 /* fast zigzag. */
529 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
530 fepriv->delay = fepriv->min_delay;
531
Guillaume Audirac2030c032010-05-06 09:30:25 -0300532 /* perform a tune */
Janne Grunau01886252009-09-01 19:23:09 -0300533 retval = dvb_frontend_swzigzag_autotune(fe,
534 fepriv->check_wrapped);
535 if (retval < 0) {
536 return;
537 } else if (retval) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200538 /* OK, if we've run out of trials at the fast speed.
539 * Drop back to slow for the _next_ attempt */
540 fepriv->state = FESTATE_SEARCHING_SLOW;
541 fepriv->started_auto_step = fepriv->auto_step;
542 return;
543 }
544 fepriv->check_wrapped = 1;
545
546 /* if we've just retuned, enter the ZIGZAG_FAST state.
547 * This ensures we cannot return from an
548 * FE_SET_FRONTEND ioctl before the first frontend tune
549 * occurs */
550 if (fepriv->state & FESTATE_RETUNE) {
551 fepriv->state = FESTATE_TUNING_FAST;
552 }
553 }
554
555 /* slow zigzag */
556 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
557 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
558
559 /* Note: don't bother checking for wrapping; we stay in this
560 * state until we get a lock */
561 dvb_frontend_swzigzag_autotune(fe, 0);
562 }
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
566{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700567 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Shuah Khan18ed2862014-07-12 13:44:12 -0300569 if (fe->exit != DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return 1;
571
572 if (fepriv->dvbdev->writers == 1)
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -0300573 if (time_after_eq(jiffies, fepriv->release_jiffies +
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200574 dvb_shutdown_timeout * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return 1;
576
577 return 0;
578}
579
580static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
581{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700582 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 if (fepriv->wakeup) {
585 fepriv->wakeup = 0;
586 return 1;
587 }
588 return dvb_frontend_is_exiting(fe);
589}
590
591static void dvb_frontend_wakeup(struct dvb_frontend *fe)
592{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700593 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 fepriv->wakeup = 1;
596 wake_up_interruptible(&fepriv->wait_queue);
597}
598
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300599/**
600 * dvb_enable_media_tuner() - tries to enable the DVB tuner
601 *
602 * @fe: struct dvb_frontend pointer
603 *
604 * This function ensures that just one media tuner is enabled for a given
605 * frontend. It has two different behaviors:
606 * - For trivial devices with just one tuner:
607 * it just enables the existing tuner->fe link
608 * - For devices with more than one tuner:
609 * It is up to the driver to implement the logic that will enable one tuner
610 * and disable the other ones. However, if more than one tuner is enabled for
611 * the same frontend, it will print an error message and return -EINVAL.
612 *
613 * At return, it will return the error code returned by media_entity_setup_link,
614 * or 0 if everything is OK, if no tuner is linked to the frontend or if the
615 * mdev is NULL.
616 */
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300617#ifdef CONFIG_MEDIA_CONTROLLER_DVB
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300618static int dvb_enable_media_tuner(struct dvb_frontend *fe)
619{
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300620 struct dvb_frontend_private *fepriv = fe->frontend_priv;
621 struct dvb_adapter *adapter = fe->dvb;
622 struct media_device *mdev = adapter->mdev;
623 struct media_entity *entity, *source;
624 struct media_link *link, *found_link = NULL;
625 int i, ret, n_links = 0, active_links = 0;
626
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300627 fepriv->pipe_start_entity = NULL;
628
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300629 if (!mdev)
630 return 0;
631
632 entity = fepriv->dvbdev->entity;
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300633 fepriv->pipe_start_entity = entity;
634
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300635 for (i = 0; i < entity->num_links; i++) {
636 link = &entity->links[i];
637 if (link->sink->entity == entity) {
638 found_link = link;
639 n_links++;
640 if (link->flags & MEDIA_LNK_FL_ENABLED)
641 active_links++;
642 }
643 }
644
645 if (!n_links || active_links == 1 || !found_link)
646 return 0;
647
648 /*
649 * If a frontend has more than one tuner linked, it is up to the driver
650 * to select with one will be the active one, as the frontend core can't
651 * guess. If the driver doesn't do that, it is a bug.
652 */
653 if (n_links > 1 && active_links != 1) {
654 dev_err(fe->dvb->device,
655 "WARNING: there are %d active links among %d tuners. This is a driver's bug!\n",
656 active_links, n_links);
657 return -EINVAL;
658 }
659
660 source = found_link->source->entity;
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300661 fepriv->pipe_start_entity = source;
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300662 for (i = 0; i < source->num_links; i++) {
663 struct media_entity *sink;
664 int flags = 0;
665
666 link = &source->links[i];
667 sink = link->sink->entity;
668
669 if (sink == entity)
670 flags = MEDIA_LNK_FL_ENABLED;
671
672 ret = media_entity_setup_link(link, flags);
673 if (ret) {
674 dev_err(fe->dvb->device,
675 "Couldn't change link %s->%s to %s. Error %d\n",
676 source->name, sink->name,
677 flags ? "enabled" : "disabled",
678 ret);
679 return ret;
680 } else
681 dev_dbg(fe->dvb->device,
682 "link %s->%s was %s\n",
683 source->name, sink->name,
684 flags ? "ENABLED" : "disabled");
685 }
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300686 return 0;
687}
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300688#endif
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static int dvb_frontend_thread(void *data)
691{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700692 struct dvb_frontend *fe = data;
693 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300694 enum fe_status s;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300695 enum dvbfe_algo algo;
Mauro Carvalho Chehabc1bd57d2015-02-18 11:43:44 -0300696#ifdef CONFIG_MEDIA_CONTROLLER_DVB
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300697 int ret;
Mauro Carvalho Chehabc1bd57d2015-02-18 11:43:44 -0300698#endif
Manu Abrahamc59e7872008-10-14 16:34:07 -0300699
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300700 bool re_tune = false;
Juergen Lock6ae23222012-12-23 17:23:06 -0300701 bool semheld = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300703 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200705 fepriv->check_wrapped = 0;
706 fepriv->quality = 0;
707 fepriv->delay = 3*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 fepriv->wakeup = 0;
Andrew de Quincey04c56d02006-07-10 03:34:14 -0300710 fepriv->reinitialise = 0;
711
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300712#ifdef CONFIG_MEDIA_CONTROLLER_DVB
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300713 ret = dvb_enable_media_tuner(fe);
714 if (ret) {
715 /* FIXME: return an error if it fails */
716 dev_info(fe->dvb->device,
717 "proceeding with FE task\n");
Mauro Carvalho Chehab8a26a252015-02-23 09:41:17 -0300718 } else if (fepriv->pipe_start_entity) {
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300719 ret = media_entity_pipeline_start(fepriv->pipe_start_entity,
720 &fepriv->pipe);
721 if (ret)
722 return ret;
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300723 }
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300724#endif
Mauro Carvalho Chehab9239effd2015-01-06 13:57:55 -0300725
Andrew de Quincey04c56d02006-07-10 03:34:14 -0300726 dvb_frontend_init(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700728 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 while (1) {
730 up(&fepriv->sem); /* is locked when we enter the thread... */
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300731restart:
Hans Verkuil94238e92011-08-27 11:30:25 -0300732 wait_event_interruptible_timeout(fepriv->wait_queue,
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700733 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
734 || freezing(current),
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300735 fepriv->delay);
736
737 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 /* got signal or quitting */
Juergen Lock6ae23222012-12-23 17:23:06 -0300739 if (!down_interruptible(&fepriv->sem))
740 semheld = true;
Shuah Khan18ed2862014-07-12 13:44:12 -0300741 fe->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743 }
744
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300745 if (try_to_freeze())
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300746 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (down_interruptible(&fepriv->sem))
749 break;
750
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300751 if (fepriv->reinitialise) {
752 dvb_frontend_init(fe);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300753 if (fe->ops.set_tone && fepriv->tone != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300754 fe->ops.set_tone(fe, fepriv->tone);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300755 if (fe->ops.set_voltage && fepriv->voltage != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300756 fe->ops.set_voltage(fe, fepriv->voltage);
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300757 fepriv->reinitialise = 0;
758 }
759
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200760 /* do an iteration of the tuning loop */
Manu Abrahamd772bd02006-06-24 11:18:58 -0300761 if (fe->ops.get_frontend_algo) {
Manu Abrahamc59e7872008-10-14 16:34:07 -0300762 algo = fe->ops.get_frontend_algo(fe);
763 switch (algo) {
764 case DVBFE_ALGO_HW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300765 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300766
Manu Abrahamd772bd02006-06-24 11:18:58 -0300767 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300768 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300769 re_tune = true;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300770 fepriv->state = FESTATE_TUNED;
Simon Arlott45145b62012-02-06 17:57:01 -0300771 } else {
772 re_tune = false;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Manu Abrahamc59e7872008-10-14 16:34:07 -0300775 if (fe->ops.tune)
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300776 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300777
Darron Broad2fac9a02008-12-18 06:27:50 -0300778 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300779 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
Manu Abrahamd772bd02006-06-24 11:18:58 -0300780 dvb_frontend_add_event(fe, s);
781 fepriv->status = s;
782 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300783 break;
784 case DVBFE_ALGO_SW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300785 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
Manu Abraham70d90632006-06-29 22:05:23 -0300786 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300787 break;
788 case DVBFE_ALGO_CUSTOM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300789 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300790 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300791 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300792 fepriv->state = FESTATE_TUNED;
793 }
794 /* Case where we are going to search for a carrier
795 * User asked us to retune again for some reason, possibly
796 * requesting a search with a new set of parameters
797 */
798 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300799 if (fe->ops.search) {
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -0300800 fepriv->algo_status = fe->ops.search(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300801 /* We did do a search as was requested, the flags are
802 * now unset as well and has the flags wrt to search.
803 */
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300804 } else {
805 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
806 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300807 }
808 /* Track the carrier if the search was successful */
Mauro Carvalho Chehab1b5d8712011-12-26 18:11:25 -0300809 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300810 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
811 fepriv->delay = HZ / 2;
812 }
Chris Rankin556a0442012-04-06 18:38:18 -0300813 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300814 fe->ops.read_status(fe, &s);
815 if (s != fepriv->status) {
816 dvb_frontend_add_event(fe, s); /* update event list */
817 fepriv->status = s;
818 if (!(s & FE_HAS_LOCK)) {
819 fepriv->delay = HZ / 10;
820 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
821 } else {
822 fepriv->delay = 60 * HZ;
823 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300824 }
825 break;
826 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300827 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300828 break;
829 }
830 } else {
Manu Abraham4a4edcc2006-06-25 05:46:26 -0300831 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300835#ifdef CONFIG_MEDIA_CONTROLLER_DVB
Mauro Carvalho Chehab8a26a252015-02-23 09:41:17 -0300836 if (fepriv->pipe_start_entity)
837 media_entity_pipeline_stop(fepriv->pipe_start_entity);
Mauro Carvalho Chehab135f9be2015-01-06 17:53:15 -0300838 fepriv->pipe_start_entity = NULL;
839#endif
840
Oliver Endriss608f62d2007-08-25 13:17:53 -0300841 if (dvb_powerdown_on_sleep) {
842 if (fe->ops.set_voltage)
843 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300844 if (fe->ops.tuner_ops.sleep) {
Devin Heitmueller41286d92008-11-16 00:44:52 -0300845 if (fe->ops.i2c_gate_ctrl)
846 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300847 fe->ops.tuner_ops.sleep(fe);
848 if (fe->ops.i2c_gate_ctrl)
849 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300850 }
Patrick Boettcherdea74862006-05-14 05:01:31 -0300851 if (fe->ops.sleep)
852 fe->ops.sleep(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300855 fepriv->thread = NULL;
matthieu castete36309f2010-05-05 15:59:20 -0300856 if (kthread_should_stop())
Shuah Khan18ed2862014-07-12 13:44:12 -0300857 fe->exit = DVB_FE_DEVICE_REMOVED;
matthieu castete36309f2010-05-05 15:59:20 -0300858 else
Shuah Khan18ed2862014-07-12 13:44:12 -0300859 fe->exit = DVB_FE_NO_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 mb();
861
Juergen Lock6ae23222012-12-23 17:23:06 -0300862 if (semheld)
863 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 dvb_frontend_wakeup(fe);
865 return 0;
866}
867
868static void dvb_frontend_stop(struct dvb_frontend *fe)
869{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700870 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300872 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Shuah Khan18ed2862014-07-12 13:44:12 -0300874 if (fe->exit != DVB_FE_DEVICE_REMOVED)
875 fe->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 mb();
877
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300878 if (!fepriv->thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return;
880
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300881 kthread_stop(fepriv->thread);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300882
Thomas Gleixnera0a47142010-09-07 11:33:27 -0300883 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 fepriv->state = FESTATE_IDLE;
885
886 /* paranoia check in case a signal arrived */
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300887 if (fepriv->thread)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300888 dev_warn(fe->dvb->device,
889 "dvb_frontend_stop: warning: thread %p won't exit\n",
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300890 fepriv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
NooneImportant83b75b02005-11-08 21:35:27 -0800893/*
Mauro Carvalho Chehab4deea4c2015-11-09 23:24:10 -0200894 * Sleep for the amount of time given by add_usec parameter
895 *
896 * This needs to be as precise as possible, as it affects the detection of
897 * the dish tone command at the satellite subsystem. The precision is improved
898 * by using a scheduled msleep followed by udelay for the remainder.
NooneImportant83b75b02005-11-08 21:35:27 -0800899 */
Tina Ruchandani9056a232015-05-31 04:17:06 -0300900void dvb_frontend_sleep_until(ktime_t *waketime, u32 add_usec)
NooneImportant83b75b02005-11-08 21:35:27 -0800901{
NooneImportant83b75b02005-11-08 21:35:27 -0800902 s32 delta, newdelta;
903
Tina Ruchandani9056a232015-05-31 04:17:06 -0300904 ktime_add_us(*waketime, add_usec);
905 delta = ktime_us_delta(ktime_get_real(), *waketime);
NooneImportant83b75b02005-11-08 21:35:27 -0800906 if (delta > 2500) {
907 msleep((delta - 1500) / 1000);
Tina Ruchandani9056a232015-05-31 04:17:06 -0300908 newdelta = ktime_us_delta(ktime_get_real(), *waketime);
NooneImportant83b75b02005-11-08 21:35:27 -0800909 delta = (newdelta > delta) ? 0 : newdelta;
910 }
911 if (delta > 0)
912 udelay(delta);
913}
914EXPORT_SYMBOL(dvb_frontend_sleep_until);
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916static int dvb_frontend_start(struct dvb_frontend *fe)
917{
918 int ret;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700919 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300920 struct task_struct *fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300922 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300924 if (fepriv->thread) {
Shuah Khan18ed2862014-07-12 13:44:12 -0300925 if (fe->exit == DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 return 0;
927 else
928 dvb_frontend_stop (fe);
929 }
930
931 if (signal_pending(current))
932 return -EINTR;
933 if (down_interruptible (&fepriv->sem))
934 return -EINTR;
935
936 fepriv->state = FESTATE_IDLE;
Shuah Khan18ed2862014-07-12 13:44:12 -0300937 fe->exit = DVB_FE_NO_EXIT;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300938 fepriv->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 mb();
940
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300941 fe_thread = kthread_run(dvb_frontend_thread, fe,
Steven Toth363c35f2008-10-11 11:05:50 -0300942 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300943 if (IS_ERR(fe_thread)) {
944 ret = PTR_ERR(fe_thread);
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300945 dev_warn(fe->dvb->device,
946 "dvb_frontend_start: failed to start kthread (%d)\n",
947 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 up(&fepriv->sem);
949 return ret;
950 }
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300951 fepriv->thread = fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953}
954
Guillaume Audirac2030c032010-05-06 09:30:25 -0300955static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
Oliver Endrissc471b332007-08-09 01:03:42 -0300956 u32 *freq_min, u32 *freq_max)
957{
958 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
959
960 if (fe->ops.info.frequency_max == 0)
961 *freq_max = fe->ops.tuner_ops.info.frequency_max;
962 else if (fe->ops.tuner_ops.info.frequency_max == 0)
963 *freq_max = fe->ops.info.frequency_max;
964 else
965 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
966
967 if (*freq_min == 0 || *freq_max == 0)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300968 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
969 fe->dvb->num, fe->id);
Oliver Endrissc471b332007-08-09 01:03:42 -0300970}
971
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300972static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300973{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300974 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Oliver Endrissc471b332007-08-09 01:03:42 -0300975 u32 freq_min;
976 u32 freq_max;
977
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300978 /* range check: frequency */
Guillaume Audirac2030c032010-05-06 09:30:25 -0300979 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300980 if ((freq_min && c->frequency < freq_min) ||
981 (freq_max && c->frequency > freq_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300982 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
983 fe->dvb->num, fe->id, c->frequency,
984 freq_min, freq_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300985 return -EINVAL;
986 }
987
988 /* range check: symbol rate */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300989 switch (c->delivery_system) {
990 case SYS_DVBS:
991 case SYS_DVBS2:
992 case SYS_TURBO:
993 case SYS_DVBC_ANNEX_A:
994 case SYS_DVBC_ANNEX_C:
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300995 if ((fe->ops.info.symbol_rate_min &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300996 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300997 (fe->ops.info.symbol_rate_max &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300998 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300999 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
1000 fe->dvb->num, fe->id, c->symbol_rate,
1001 fe->ops.info.symbol_rate_min,
1002 fe->ops.info.symbol_rate_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -03001003 return -EINVAL;
1004 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001005 default:
1006 break;
Oliver Endriss1fab46f2007-07-23 21:00:36 -03001007 }
1008
1009 return 0;
1010}
1011
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001012static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
1013{
Andreas Oberritter50727712011-05-08 20:03:39 -03001014 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001015 int i;
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -02001016 u32 delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001017
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -02001018 delsys = c->delivery_system;
Mauro Carvalho Chehab808d24d2013-03-20 10:39:31 -03001019 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -02001020 c->delivery_system = delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001021
Andreas Oberritter50727712011-05-08 20:03:39 -03001022 c->state = DTV_CLEAR;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -03001023
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001024 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
1025 __func__, c->delivery_system);
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -03001026
Andreas Oberritter50727712011-05-08 20:03:39 -03001027 c->transmission_mode = TRANSMISSION_MODE_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -03001028 c->bandwidth_hz = 0; /* AUTO */
Andreas Oberritter50727712011-05-08 20:03:39 -03001029 c->guard_interval = GUARD_INTERVAL_AUTO;
1030 c->hierarchy = HIERARCHY_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -03001031 c->symbol_rate = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -03001032 c->code_rate_HP = FEC_AUTO;
1033 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -03001034 c->fec_inner = FEC_AUTO;
Mauro Carvalho Chehab39ce61a2011-11-11 12:46:23 -02001035 c->rolloff = ROLLOFF_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -03001036 c->voltage = SEC_VOLTAGE_OFF;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -03001037 c->sectone = SEC_TONE_OFF;
1038 c->pilot = PILOT_AUTO;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001039
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -03001040 c->isdbt_partial_reception = 0;
1041 c->isdbt_sb_mode = 0;
1042 c->isdbt_sb_subchannel = 0;
1043 c->isdbt_sb_segment_idx = 0;
1044 c->isdbt_sb_segment_count = 0;
1045 c->isdbt_layer_enabled = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001046 for (i = 0; i < 3; i++) {
Andreas Oberritter50727712011-05-08 20:03:39 -03001047 c->layer[i].fec = FEC_AUTO;
1048 c->layer[i].modulation = QAM_AUTO;
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -03001049 c->layer[i].interleaving = 0;
1050 c->layer[i].segment_count = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001051 }
1052
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001053 c->stream_id = NO_STREAM_ID_FILTER;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -03001054
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001055 switch (c->delivery_system) {
1056 case SYS_DVBS:
1057 case SYS_DVBS2:
1058 case SYS_TURBO:
1059 c->modulation = QPSK; /* implied for DVB-S in legacy API */
1060 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
1061 break;
1062 case SYS_ATSC:
1063 c->modulation = VSB_8;
1064 break;
Akihiro Tsukada8e281fa2014-10-31 10:19:39 -03001065 case SYS_ISDBS:
1066 c->symbol_rate = 28860000;
1067 c->rolloff = ROLLOFF_35;
1068 c->bandwidth_hz = c->symbol_rate / 100 * 135;
1069 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001070 default:
1071 c->modulation = QAM_AUTO;
1072 break;
1073 }
1074
Antti Palosaari33eebec2012-10-03 04:28:56 -03001075 c->lna = LNA_AUTO;
1076
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001077 return 0;
1078}
1079
1080#define _DTV_CMD(n, s, b) \
1081[n] = { \
1082 .name = #n, \
1083 .cmd = n, \
1084 .set = s,\
1085 .buffer = b \
1086}
1087
Andreas Oberritterec05a642011-05-26 07:54:14 -03001088static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001089 _DTV_CMD(DTV_TUNE, 1, 0),
1090 _DTV_CMD(DTV_CLEAR, 1, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -03001091
1092 /* Set */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001093 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1094 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1095 _DTV_CMD(DTV_MODULATION, 1, 0),
1096 _DTV_CMD(DTV_INVERSION, 1, 0),
1097 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1098 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1099 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1100 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1101 _DTV_CMD(DTV_TONE, 1, 0),
1102 _DTV_CMD(DTV_PILOT, 1, 0),
1103 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1104 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1105 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1106 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1107 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1108 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1109 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
Antti Palosaari224b6642012-08-12 22:33:21 -03001110 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001111
1112 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1113 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1114 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1115 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1116 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
Patrick Boettchere7b79492009-08-14 05:24:19 -03001117 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001118 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1119 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1120 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1121 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1122 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1123 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1124 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1125 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1126 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1127 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1128 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1129 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1130
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001131 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1132 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
Antti Palosaari8a2697a2012-07-11 21:54:50 -03001133 _DTV_CMD(DTV_LNA, 1, 0),
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001134
Steven Toth6b73eea2008-09-04 01:12:25 -03001135 /* Get */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001136 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1137 _DTV_CMD(DTV_API_VERSION, 0, 0),
Manu Abrahamba2780c2011-11-13 18:47:44 -03001138
1139 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001140
1141 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1142 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1143
1144 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001145 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1146 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1147 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1148 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1149 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001150 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1151 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1152 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1153 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1154 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1155 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1156 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001157
1158 /* Statistics API */
1159 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1160 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1161 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1162 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1163 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1164 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1165 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1166 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -03001167};
1168
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001169static void dtv_property_dump(struct dvb_frontend *fe, struct dtv_property *tvp)
Steven Toth6b73eea2008-09-04 01:12:25 -03001170{
1171 int i;
1172
Mauro Carvalho Chehab106ff9e2011-07-15 08:49:06 -03001173 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001174 dev_warn(fe->dvb->device, "%s: tvp.cmd = 0x%08x undefined\n",
1175 __func__, tvp->cmd);
Darron Broad4aae8ef2008-10-03 11:50:00 -03001176 return;
1177 }
1178
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001179 dev_dbg(fe->dvb->device, "%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1180 tvp->cmd, dtv_cmds[tvp->cmd].name);
Steven Toth6b73eea2008-09-04 01:12:25 -03001181
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001182 if (dtv_cmds[tvp->cmd].buffer) {
1183 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1184 __func__, tvp->u.buffer.len);
Steven Toth6b73eea2008-09-04 01:12:25 -03001185
1186 for(i = 0; i < tvp->u.buffer.len; i++)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001187 dev_dbg(fe->dvb->device,
1188 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1189 __func__, i, tvp->u.buffer.data[i]);
1190 } else {
1191 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1192 tvp->u.data);
1193 }
Steven Toth6b73eea2008-09-04 01:12:25 -03001194}
1195
Steven Tothee33c522008-09-11 23:52:32 -03001196/* Synchronise the legacy tuning parameters into the cache, so that demodulator
1197 * drivers can use a single set_frontend tuning function, regardless of whether
1198 * it's being used for the legacy or new API, reducing code and complexity.
1199 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001200static int dtv_property_cache_sync(struct dvb_frontend *fe,
1201 struct dtv_frontend_properties *c,
1202 const struct dvb_frontend_parameters *p)
Steven Tothee33c522008-09-11 23:52:32 -03001203{
Steven Tothee33c522008-09-11 23:52:32 -03001204 c->frequency = p->frequency;
1205 c->inversion = p->inversion;
1206
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001207 switch (dvbv3_type(c->delivery_system)) {
1208 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001209 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001210 c->symbol_rate = p->u.qpsk.symbol_rate;
1211 c->fec_inner = p->u.qpsk.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 Tothee33c522008-09-11 23:52:32 -03001215 c->symbol_rate = p->u.qam.symbol_rate;
1216 c->fec_inner = p->u.qam.fec_inner;
1217 c->modulation = p->u.qam.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__);
1221
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001222 switch (p->u.ofdm.bandwidth) {
1223 case BANDWIDTH_10_MHZ:
1224 c->bandwidth_hz = 10000000;
1225 break;
1226 case BANDWIDTH_8_MHZ:
Steven Toth75b7f942008-09-13 16:56:34 -03001227 c->bandwidth_hz = 8000000;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001228 break;
1229 case BANDWIDTH_7_MHZ:
1230 c->bandwidth_hz = 7000000;
1231 break;
1232 case BANDWIDTH_6_MHZ:
1233 c->bandwidth_hz = 6000000;
1234 break;
1235 case BANDWIDTH_5_MHZ:
1236 c->bandwidth_hz = 5000000;
1237 break;
1238 case BANDWIDTH_1_712_MHZ:
1239 c->bandwidth_hz = 1712000;
1240 break;
1241 case BANDWIDTH_AUTO:
Steven Toth75b7f942008-09-13 16:56:34 -03001242 c->bandwidth_hz = 0;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001243 }
1244
Steven Tothee33c522008-09-11 23:52:32 -03001245 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1246 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1247 c->modulation = p->u.ofdm.constellation;
1248 c->transmission_mode = p->u.ofdm.transmission_mode;
1249 c->guard_interval = p->u.ofdm.guard_interval;
1250 c->hierarchy = p->u.ofdm.hierarchy_information;
1251 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001252 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001253 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001254 c->modulation = p->u.vsb.modulation;
Michael Krufkyca689482012-01-29 15:44:58 -03001255 if (c->delivery_system == SYS_ATSCMH)
1256 break;
Steven Toth80a773c2008-09-12 00:53:50 -03001257 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1258 c->delivery_system = SYS_ATSC;
1259 else
1260 c->delivery_system = SYS_DVBC_ANNEX_B;
Steven Tothee33c522008-09-11 23:52:32 -03001261 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001262 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001263 dev_err(fe->dvb->device,
1264 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1265 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001266 return -EINVAL;
Steven Tothee33c522008-09-11 23:52:32 -03001267 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001268
1269 return 0;
Steven Tothee33c522008-09-11 23:52:32 -03001270}
1271
Steven Tothd5748f12008-09-12 00:27:13 -03001272/* Ensure the cached values are set correctly in the frontend
1273 * legacy tuning structures, for the advanced tuning API.
1274 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001275static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001276 struct dvb_frontend_parameters *p)
Steven Toth6b73eea2008-09-04 01:12:25 -03001277{
Andreas Oberritter15cc2bb2011-05-08 20:03:35 -03001278 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001279
Steven Tothd5748f12008-09-12 00:27:13 -03001280 p->frequency = c->frequency;
1281 p->inversion = c->inversion;
1282
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001283 switch (dvbv3_type(c->delivery_system)) {
1284 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001285 dev_err(fe->dvb->device,
1286 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1287 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001288 return -EINVAL;
1289 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001290 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001291 p->u.qpsk.symbol_rate = c->symbol_rate;
1292 p->u.qpsk.fec_inner = c->fec_inner;
1293 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001294 case DVBV3_QAM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001295 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001296 p->u.qam.symbol_rate = c->symbol_rate;
1297 p->u.qam.fec_inner = c->fec_inner;
1298 p->u.qam.modulation = c->modulation;
1299 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001300 case DVBV3_OFDM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001301 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001302 switch (c->bandwidth_hz) {
1303 case 10000000:
1304 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1305 break;
1306 case 8000000:
Steven Toth75b7f942008-09-13 16:56:34 -03001307 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001308 break;
1309 case 7000000:
1310 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1311 break;
1312 case 6000000:
1313 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1314 break;
1315 case 5000000:
1316 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1317 break;
1318 case 1712000:
1319 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1320 break;
1321 case 0:
1322 default:
Steven Toth75b7f942008-09-13 16:56:34 -03001323 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001324 }
Steven Tothd5748f12008-09-12 00:27:13 -03001325 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1326 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1327 p->u.ofdm.constellation = c->modulation;
1328 p->u.ofdm.transmission_mode = c->transmission_mode;
1329 p->u.ofdm.guard_interval = c->guard_interval;
1330 p->u.ofdm.hierarchy_information = c->hierarchy;
1331 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001332 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001333 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001334 p->u.vsb.modulation = c->modulation;
1335 break;
1336 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001337 return 0;
Steven Toth6b73eea2008-09-04 01:12:25 -03001338}
1339
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001340/**
1341 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1342 * @fe: struct dvb_frontend pointer
1343 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1344 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1345 *
1346 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1347 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1348 * If p_out is not null, it will update the DVBv3 params pointed by it.
1349 */
1350static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001351 struct dvb_frontend_parameters *p_out)
1352{
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001353 int r;
1354
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001355 if (fe->ops.get_frontend) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001356 r = fe->ops.get_frontend(fe);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001357 if (unlikely(r < 0))
1358 return r;
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001359 if (p_out)
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001360 dtv_property_legacy_params_sync(fe, p_out);
1361 return 0;
1362 }
1363
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001364 /* As everything is in cache, get_frontend fops are always supported */
Mauro Carvalho Chehabb1e9a65012011-12-26 16:43:32 -03001365 return 0;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001366}
1367
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001368static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001369 unsigned int cmd, void *parg);
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001370static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001371 unsigned int cmd, void *parg);
1372
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001373static int dtv_property_process_get(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001374 const struct dtv_frontend_properties *c,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001375 struct dtv_property *tvp,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001376 struct file *file)
Steven Toth363429a2008-09-12 01:34:28 -03001377{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001378 int r, ncaps;
Steven Tothbfbf2da2008-09-12 01:37:37 -03001379
Steven Toth363429a2008-09-12 01:34:28 -03001380 switch(tvp->cmd) {
Manu Abrahamba2780c2011-11-13 18:47:44 -03001381 case DTV_ENUM_DELSYS:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001382 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001383 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001384 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1385 ncaps++;
1386 }
1387 tvp->u.buffer.len = ncaps;
Manu Abrahamba2780c2011-11-13 18:47:44 -03001388 break;
Steven Toth363429a2008-09-12 01:34:28 -03001389 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001390 tvp->u.data = c->frequency;
Steven Toth363429a2008-09-12 01:34:28 -03001391 break;
1392 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001393 tvp->u.data = c->modulation;
Steven Toth363429a2008-09-12 01:34:28 -03001394 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001395 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001396 tvp->u.data = c->bandwidth_hz;
Steven Toth363429a2008-09-12 01:34:28 -03001397 break;
1398 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001399 tvp->u.data = c->inversion;
Steven Toth363429a2008-09-12 01:34:28 -03001400 break;
1401 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001402 tvp->u.data = c->symbol_rate;
Steven Toth363429a2008-09-12 01:34:28 -03001403 break;
1404 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001405 tvp->u.data = c->fec_inner;
Steven Toth363429a2008-09-12 01:34:28 -03001406 break;
1407 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001408 tvp->u.data = c->pilot;
Steven Toth363429a2008-09-12 01:34:28 -03001409 break;
1410 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001411 tvp->u.data = c->rolloff;
Steven Toth363429a2008-09-12 01:34:28 -03001412 break;
1413 case DTV_DELIVERY_SYSTEM:
Andreas Oberritter50727712011-05-08 20:03:39 -03001414 tvp->u.data = c->delivery_system;
Steven Toth363429a2008-09-12 01:34:28 -03001415 break;
Steven Toth363429a2008-09-12 01:34:28 -03001416 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001417 tvp->u.data = c->voltage;
Steven Toth363429a2008-09-12 01:34:28 -03001418 break;
1419 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001420 tvp->u.data = c->sectone;
Steven Toth363429a2008-09-12 01:34:28 -03001421 break;
Steven Totheacf8d82008-09-26 00:29:49 -03001422 case DTV_API_VERSION:
1423 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1424 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001425 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001426 tvp->u.data = c->code_rate_HP;
Steven Totha4de91b2008-10-06 21:55:46 -03001427 break;
1428 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001429 tvp->u.data = c->code_rate_LP;
Steven Totha4de91b2008-10-06 21:55:46 -03001430 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001431 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001432 tvp->u.data = c->guard_interval;
Steven Tothb87625f2008-10-06 21:56:59 -03001433 break;
1434 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001435 tvp->u.data = c->transmission_mode;
Steven Tothb87625f2008-10-06 21:56:59 -03001436 break;
Steven Tothef526f42008-10-06 22:01:47 -03001437 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001438 tvp->u.data = c->hierarchy;
Steven Tothef526f42008-10-06 22:01:47 -03001439 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001440 case DTV_INTERLEAVING:
1441 tvp->u.data = c->interleaving;
1442 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001443
1444 /* ISDB-T Support here */
1445 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001446 tvp->u.data = c->isdbt_partial_reception;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001447 break;
1448 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001449 tvp->u.data = c->isdbt_sb_mode;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001450 break;
1451 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001452 tvp->u.data = c->isdbt_sb_subchannel;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001453 break;
1454 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001455 tvp->u.data = c->isdbt_sb_segment_idx;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001456 break;
1457 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001458 tvp->u.data = c->isdbt_sb_segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001459 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001460 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001461 tvp->u.data = c->isdbt_layer_enabled;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001462 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001463 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001464 tvp->u.data = c->layer[0].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001465 break;
1466 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001467 tvp->u.data = c->layer[0].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001468 break;
1469 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001470 tvp->u.data = c->layer[0].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001471 break;
1472 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001473 tvp->u.data = c->layer[0].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001474 break;
1475 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001476 tvp->u.data = c->layer[1].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001477 break;
1478 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001479 tvp->u.data = c->layer[1].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001480 break;
1481 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001482 tvp->u.data = c->layer[1].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001483 break;
1484 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001485 tvp->u.data = c->layer[1].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001486 break;
1487 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001488 tvp->u.data = c->layer[2].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001489 break;
1490 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001491 tvp->u.data = c->layer[2].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001492 break;
1493 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001494 tvp->u.data = c->layer[2].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001495 break;
1496 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001497 tvp->u.data = c->layer[2].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001498 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001499
1500 /* Multistream support */
1501 case DTV_STREAM_ID:
1502 case DTV_DVBT2_PLP_ID_LEGACY:
1503 tvp->u.data = c->stream_id;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001504 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001505
1506 /* ATSC-MH */
1507 case DTV_ATSCMH_FIC_VER:
1508 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1509 break;
1510 case DTV_ATSCMH_PARADE_ID:
1511 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1512 break;
1513 case DTV_ATSCMH_NOG:
1514 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1515 break;
1516 case DTV_ATSCMH_TNOG:
1517 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1518 break;
1519 case DTV_ATSCMH_SGN:
1520 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1521 break;
1522 case DTV_ATSCMH_PRC:
1523 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1524 break;
1525 case DTV_ATSCMH_RS_FRAME_MODE:
1526 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1527 break;
1528 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1529 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1530 break;
1531 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1532 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1533 break;
1534 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1535 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1536 break;
1537 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1538 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1539 break;
1540 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1541 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1542 break;
1543 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1544 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1545 break;
1546 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1547 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1548 break;
1549 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1550 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1551 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001552
Antti Palosaari33eebec2012-10-03 04:28:56 -03001553 case DTV_LNA:
1554 tvp->u.data = c->lna;
1555 break;
1556
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001557 /* Fill quality measures */
1558 case DTV_STAT_SIGNAL_STRENGTH:
1559 tvp->u.st = c->strength;
1560 break;
1561 case DTV_STAT_CNR:
1562 tvp->u.st = c->cnr;
1563 break;
1564 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1565 tvp->u.st = c->pre_bit_error;
1566 break;
1567 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1568 tvp->u.st = c->pre_bit_count;
1569 break;
1570 case DTV_STAT_POST_ERROR_BIT_COUNT:
1571 tvp->u.st = c->post_bit_error;
1572 break;
1573 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1574 tvp->u.st = c->post_bit_count;
1575 break;
1576 case DTV_STAT_ERROR_BLOCK_COUNT:
1577 tvp->u.st = c->block_error;
1578 break;
1579 case DTV_STAT_TOTAL_BLOCK_COUNT:
1580 tvp->u.st = c->block_count;
1581 break;
Steven Toth363429a2008-09-12 01:34:28 -03001582 default:
Mauro Carvalho Chehab94a93e52013-01-23 17:06:02 -03001583 dev_dbg(fe->dvb->device,
1584 "%s: FE property %d doesn't exist\n",
1585 __func__, tvp->cmd);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001586 return -EINVAL;
Steven Toth363429a2008-09-12 01:34:28 -03001587 }
1588
Andreas Oberrittere23d9ae2011-05-08 20:03:36 -03001589 /* Allow the frontend to override outgoing properties */
1590 if (fe->ops.get_property) {
1591 r = fe->ops.get_property(fe, tvp);
1592 if (r < 0)
1593 return r;
1594 }
1595
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001596 dtv_property_dump(fe, tvp);
Mauro Carvalho Chehab639544d2009-12-31 17:49:32 -03001597
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001598 return 0;
Steven Toth363429a2008-09-12 01:34:28 -03001599}
1600
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001601static int dtv_set_frontend(struct dvb_frontend *fe);
1602
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001603static bool is_dvbv3_delsys(u32 delsys)
1604{
1605 bool status;
1606
1607 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1608 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1609
1610 return status;
1611}
1612
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001613/**
1614 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1615 * @fe: struct frontend;
1616 * @delsys: DVBv5 type that will be used for emulation
1617 *
1618 * Provides emulation for delivery systems that are compatible with the old
1619 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1620 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1621 * parameters are compatible with DVB-S spec.
1622 */
1623static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001624{
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001625 int i;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001626 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001627
Mauro Carvalho Chehab36264792012-04-17 18:32:19 -03001628 c->delivery_system = delsys;
1629
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001630 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001631 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001632 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001633 if (c->delivery_system == SYS_ISDBT) {
1634 dev_dbg(fe->dvb->device,
1635 "%s: Using defaults for SYS_ISDBT\n",
1636 __func__);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001637
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001638 if (!c->bandwidth_hz)
1639 c->bandwidth_hz = 6000000;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001640
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001641 c->isdbt_partial_reception = 0;
1642 c->isdbt_sb_mode = 0;
1643 c->isdbt_sb_subchannel = 0;
1644 c->isdbt_sb_segment_idx = 0;
1645 c->isdbt_sb_segment_count = 0;
1646 c->isdbt_layer_enabled = 7;
1647 for (i = 0; i < 3; i++) {
1648 c->layer[i].fec = FEC_AUTO;
1649 c->layer[i].modulation = QAM_AUTO;
1650 c->layer[i].interleaving = 0;
1651 c->layer[i].segment_count = 0;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001652 }
1653 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001654 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001655 __func__, c->delivery_system);
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001656
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001657 return 0;
1658}
1659
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001660/**
1661 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1662 * @fe: frontend struct
1663 * @desired_system: delivery system requested by the user
1664 *
1665 * A DVBv5 call know what's the desired system it wants. So, set it.
1666 *
1667 * There are, however, a few known issues with early DVBv5 applications that
1668 * are also handled by this logic:
1669 *
1670 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1671 * This is an API violation, but, as we don't want to break userspace,
1672 * convert it to the first supported delivery system.
1673 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1674 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1675 * ISDB-T provided backward compat with DVB-T.
1676 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001677static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1678 u32 desired_system)
1679{
1680 int ncaps;
1681 u32 delsys = SYS_UNDEFINED;
1682 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1683 enum dvbv3_emulation_type type;
1684
1685 /*
1686 * It was reported that some old DVBv5 applications were
1687 * filling delivery_system with SYS_UNDEFINED. If this happens,
1688 * assume that the application wants to use the first supported
1689 * delivery system.
1690 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001691 if (desired_system == SYS_UNDEFINED)
1692 desired_system = fe->ops.delsys[0];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001693
1694 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001695 * This is a DVBv5 call. So, it likely knows the supported
1696 * delivery systems. So, check if the desired delivery system is
1697 * supported
1698 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001699 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001700 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001701 if (fe->ops.delsys[ncaps] == desired_system) {
1702 c->delivery_system = desired_system;
1703 dev_dbg(fe->dvb->device,
1704 "%s: Changing delivery system to %d\n",
1705 __func__, desired_system);
1706 return 0;
1707 }
1708 ncaps++;
1709 }
1710
1711 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001712 * The requested delivery system isn't supported. Maybe userspace
1713 * is requesting a DVBv3 compatible delivery system.
1714 *
1715 * The emulation only works if the desired system is one of the
1716 * delivery systems supported by DVBv3 API
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001717 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001718 if (!is_dvbv3_delsys(desired_system)) {
1719 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001720 "%s: Delivery system %d not supported.\n",
1721 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001722 return -EINVAL;
1723 }
1724
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001725 type = dvbv3_type(desired_system);
1726
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001727 /*
1728 * Get the last non-DVBv3 delivery system that has the same type
1729 * of the desired system
1730 */
1731 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001732 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001733 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001734 delsys = fe->ops.delsys[ncaps];
1735 ncaps++;
1736 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001737
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001738 /* There's nothing compatible with the desired delivery system */
1739 if (delsys == SYS_UNDEFINED) {
1740 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001741 "%s: Delivery system %d not supported on emulation mode.\n",
1742 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001743 return -EINVAL;
1744 }
1745
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001746 dev_dbg(fe->dvb->device,
1747 "%s: Using delivery system %d emulated as if it were %d\n",
1748 __func__, delsys, desired_system);
1749
1750 return emulate_delivery_system(fe, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001751}
1752
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001753/**
1754 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1755 * @fe: frontend struct
1756 *
1757 * A DVBv3 call doesn't know what's the desired system it wants. It also
1758 * doesn't allow to switch between different types. Due to that, userspace
1759 * should use DVBv5 instead.
1760 * However, in order to avoid breaking userspace API, limited backward
1761 * compatibility support is provided.
1762 *
1763 * There are some delivery systems that are incompatible with DVBv3 calls.
1764 *
1765 * This routine should work fine for frontends that support just one delivery
1766 * system.
1767 *
1768 * For frontends that support multiple frontends:
1769 * 1) It defaults to use the first supported delivery system. There's an
1770 * userspace application that allows changing it at runtime;
1771 *
1772 * 2) If the current delivery system is not compatible with DVBv3, it gets
1773 * the first one that it is compatible.
1774 *
1775 * NOTE: in order for this to work with applications like Kaffeine that
1776 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1777 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1778 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1779 * to DVB-S.
1780 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001781static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1782{
1783 int ncaps;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001784 u32 delsys = SYS_UNDEFINED;
1785 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001786
1787 /* If not set yet, defaults to the first supported delivery system */
1788 if (c->delivery_system == SYS_UNDEFINED)
1789 c->delivery_system = fe->ops.delsys[0];
1790
1791 /*
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001792 * Trivial case: just use the current one, if it already a DVBv3
1793 * delivery system
1794 */
1795 if (is_dvbv3_delsys(c->delivery_system)) {
1796 dev_dbg(fe->dvb->device,
1797 "%s: Using delivery system to %d\n",
1798 __func__, c->delivery_system);
1799 return 0;
1800 }
1801
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001802 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001803 * Seek for the first delivery system that it is compatible with a
1804 * DVBv3 standard
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001805 */
1806 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001807 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001808 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1809 delsys = fe->ops.delsys[ncaps];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001810 break;
1811 }
1812 ncaps++;
1813 }
1814 if (delsys == SYS_UNDEFINED) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001815 dev_dbg(fe->dvb->device,
1816 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1817 __func__);
1818 return -EINVAL;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001819 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001820 return emulate_delivery_system(fe, delsys);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001821}
1822
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001823static int dtv_property_process_set(struct dvb_frontend *fe,
1824 struct dtv_property *tvp,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001825 struct file *file)
Steven Toth6b73eea2008-09-04 01:12:25 -03001826{
1827 int r = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -03001828 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001829
Steven Tothbfbf2da2008-09-12 01:37:37 -03001830 /* Allow the frontend to validate incoming properties */
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001831 if (fe->ops.set_property) {
Steven Tothbfbf2da2008-09-12 01:37:37 -03001832 r = fe->ops.set_property(fe, tvp);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001833 if (r < 0)
1834 return r;
1835 }
Steven Tothbfbf2da2008-09-12 01:37:37 -03001836
Steven Toth6b73eea2008-09-04 01:12:25 -03001837 switch(tvp->cmd) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001838 case DTV_CLEAR:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001839 /*
1840 * Reset a cache of data specific to the frontend here. This does
Steven Toth6b73eea2008-09-04 01:12:25 -03001841 * not effect hardware.
1842 */
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001843 dvb_frontend_clear_cache(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001844 break;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001845 case DTV_TUNE:
Steven Toth6b73eea2008-09-04 01:12:25 -03001846 /* interpret the cache of data, build either a traditional frontend
Steven Tothd5748f12008-09-12 00:27:13 -03001847 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1848 * ioctl.
Steven Toth6b73eea2008-09-04 01:12:25 -03001849 */
Andreas Oberritter50727712011-05-08 20:03:39 -03001850 c->state = tvp->cmd;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001851 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1852 __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001853
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001854 r = dtv_set_frontend(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001855 break;
Steven Toth363429a2008-09-12 01:34:28 -03001856 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001857 c->frequency = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001858 break;
Steven Toth363429a2008-09-12 01:34:28 -03001859 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001860 c->modulation = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001861 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001862 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001863 c->bandwidth_hz = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001864 break;
Steven Toth363429a2008-09-12 01:34:28 -03001865 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001866 c->inversion = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001867 break;
Steven Toth363429a2008-09-12 01:34:28 -03001868 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001869 c->symbol_rate = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001870 break;
Steven Toth363429a2008-09-12 01:34:28 -03001871 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001872 c->fec_inner = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001873 break;
Steven Toth363429a2008-09-12 01:34:28 -03001874 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001875 c->pilot = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001876 break;
Steven Toth363429a2008-09-12 01:34:28 -03001877 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001878 c->rolloff = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001879 break;
Steven Toth363429a2008-09-12 01:34:28 -03001880 case DTV_DELIVERY_SYSTEM:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001881 r = dvbv5_set_delivery_system(fe, tvp->u.data);
Steven Toth6b73eea2008-09-04 01:12:25 -03001882 break;
Steven Toth363429a2008-09-12 01:34:28 -03001883 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001884 c->voltage = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001885 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001886 (void *)c->voltage);
Steven Toth13c97bf2008-09-04 21:19:43 -03001887 break;
Steven Toth363429a2008-09-12 01:34:28 -03001888 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001889 c->sectone = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001890 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001891 (void *)c->sectone);
Steven Toth13c97bf2008-09-04 21:19:43 -03001892 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001893 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001894 c->code_rate_HP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001895 break;
1896 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001897 c->code_rate_LP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001898 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001899 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001900 c->guard_interval = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001901 break;
1902 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001903 c->transmission_mode = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001904 break;
Steven Tothef526f42008-10-06 22:01:47 -03001905 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001906 c->hierarchy = tvp->u.data;
Steven Tothef526f42008-10-06 22:01:47 -03001907 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001908 case DTV_INTERLEAVING:
1909 c->interleaving = tvp->u.data;
1910 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001911
1912 /* ISDB-T Support here */
1913 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001914 c->isdbt_partial_reception = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001915 break;
1916 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001917 c->isdbt_sb_mode = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001918 break;
1919 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001920 c->isdbt_sb_subchannel = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001921 break;
1922 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001923 c->isdbt_sb_segment_idx = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001924 break;
1925 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001926 c->isdbt_sb_segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001927 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001928 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001929 c->isdbt_layer_enabled = tvp->u.data;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001930 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001931 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001932 c->layer[0].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001933 break;
1934 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001935 c->layer[0].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001936 break;
1937 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001938 c->layer[0].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001939 break;
1940 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001941 c->layer[0].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001942 break;
1943 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001944 c->layer[1].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001945 break;
1946 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001947 c->layer[1].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001948 break;
1949 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001950 c->layer[1].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001951 break;
1952 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001953 c->layer[1].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001954 break;
1955 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001956 c->layer[2].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001957 break;
1958 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001959 c->layer[2].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001960 break;
1961 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001962 c->layer[2].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001963 break;
1964 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001965 c->layer[2].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001966 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001967
1968 /* Multistream support */
1969 case DTV_STREAM_ID:
1970 case DTV_DVBT2_PLP_ID_LEGACY:
1971 c->stream_id = tvp->u.data;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001972 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001973
1974 /* ATSC-MH */
1975 case DTV_ATSCMH_PARADE_ID:
1976 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1977 break;
1978 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1979 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1980 break;
1981
Antti Palosaari33eebec2012-10-03 04:28:56 -03001982 case DTV_LNA:
1983 c->lna = tvp->u.data;
1984 if (fe->ops.set_lna)
1985 r = fe->ops.set_lna(fe);
Mauro Carvalho Chehabe6876692014-02-16 06:36:17 -03001986 if (r < 0)
1987 c->lna = LNA_AUTO;
Antti Palosaari33eebec2012-10-03 04:28:56 -03001988 break;
1989
Steven Toth363429a2008-09-12 01:34:28 -03001990 default:
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001991 return -EINVAL;
Steven Toth6b73eea2008-09-04 01:12:25 -03001992 }
1993
Steven Toth13c97bf2008-09-04 21:19:43 -03001994 return r;
Steven Toth6b73eea2008-09-04 01:12:25 -03001995}
1996
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001997static int dvb_frontend_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 unsigned int cmd, void *parg)
1999{
2000 struct dvb_device *dvbdev = file->private_data;
2001 struct dvb_frontend *fe = dvbdev->priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03002002 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002003 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002004 int err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002006 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
Juergen Lock6ae23222012-12-23 17:23:06 -03002007 if (down_interruptible(&fepriv->sem))
2008 return -ERESTARTSYS;
2009
Shuah Khan18ed2862014-07-12 13:44:12 -03002010 if (fe->exit != DVB_FE_NO_EXIT) {
Juergen Lock6ae23222012-12-23 17:23:06 -03002011 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return -ENODEV;
Juergen Lock6ae23222012-12-23 17:23:06 -03002013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
2016 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
Juergen Lock6ae23222012-12-23 17:23:06 -03002017 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
2018 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 return -EPERM;
Juergen Lock6ae23222012-12-23 17:23:06 -03002020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Steven Toth13c97bf2008-09-04 21:19:43 -03002022 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002023 err = dvb_frontend_ioctl_properties(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03002024 else {
Andreas Oberritter50727712011-05-08 20:03:39 -03002025 c->state = DTV_UNDEFINED;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002026 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03002027 }
Steven Toth13c97bf2008-09-04 21:19:43 -03002028
2029 up(&fepriv->sem);
2030 return err;
2031}
2032
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002033static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03002034 unsigned int cmd, void *parg)
2035{
2036 struct dvb_device *dvbdev = file->private_data;
2037 struct dvb_frontend *fe = dvbdev->priv;
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002038 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03002039 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Igor M. Liplianine6f9ec82008-09-12 14:49:06 -03002040 int err = 0;
Steven Tothe7fee0f32008-09-11 10:23:01 -03002041
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03002042 struct dtv_properties *tvps = parg;
Steven Tothe7fee0f32008-09-11 10:23:01 -03002043 struct dtv_property *tvp = NULL;
2044 int i;
Steven Toth13c97bf2008-09-04 21:19:43 -03002045
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002046 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Steven Toth13c97bf2008-09-04 21:19:43 -03002047
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03002048 if (cmd == FE_SET_PROPERTY) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002049 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2050 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Tothe7fee0f32008-09-11 10:23:01 -03002051
2052 /* Put an arbitrary limit on the number of messages that can
2053 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03002054 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Tothe7fee0f32008-09-11 10:23:01 -03002055 return -EINVAL;
2056
Thiago Farina6e3924a2010-01-16 10:25:38 -03002057 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Tothe7fee0f32008-09-11 10:23:01 -03002058 if (!tvp) {
2059 err = -ENOMEM;
2060 goto out;
Steven Toth6b73eea2008-09-04 01:12:25 -03002061 }
2062
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03002063 if (copy_from_user(tvp, (void __user *)tvps->props,
2064 tvps->num * sizeof(struct dtv_property))) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03002065 err = -EFAULT;
2066 goto out;
2067 }
2068
Steven Tothd48cb402008-09-26 00:16:25 -03002069 for (i = 0; i < tvps->num; i++) {
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01002070 err = dtv_property_process_set(fe, tvp + i, file);
2071 if (err < 0)
2072 goto out;
2073 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03002074 }
Steven Tothe7fee0f32008-09-11 10:23:01 -03002075
Andreas Oberritter50727712011-05-08 20:03:39 -03002076 if (c->state == DTV_TUNE)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002077 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
Steven Tothbfbf2da2008-09-12 01:37:37 -03002078
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03002079 } else if (cmd == FE_GET_PROPERTY) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002080 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
2081 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Toth363429a2008-09-12 01:34:28 -03002082
2083 /* Put an arbitrary limit on the number of messages that can
2084 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03002085 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Toth363429a2008-09-12 01:34:28 -03002086 return -EINVAL;
2087
Thiago Farina6e3924a2010-01-16 10:25:38 -03002088 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Toth363429a2008-09-12 01:34:28 -03002089 if (!tvp) {
2090 err = -ENOMEM;
2091 goto out;
2092 }
2093
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03002094 if (copy_from_user(tvp, (void __user *)tvps->props,
2095 tvps->num * sizeof(struct dtv_property))) {
Steven Toth363429a2008-09-12 01:34:28 -03002096 err = -EFAULT;
2097 goto out;
2098 }
2099
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03002100 /*
2101 * Fills the cache out struct with the cache contents, plus
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002102 * the data retrieved from get_frontend, if the frontend
2103 * is not idle. Otherwise, returns the cached content
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03002104 */
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002105 if (fepriv->state != FESTATE_IDLE) {
2106 err = dtv_get_frontend(fe, NULL);
2107 if (err < 0)
2108 goto out;
2109 }
Steven Tothd48cb402008-09-26 00:16:25 -03002110 for (i = 0; i < tvps->num; i++) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03002111 err = dtv_property_process_get(fe, c, tvp + i, file);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01002112 if (err < 0)
2113 goto out;
2114 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03002115 }
Steven Toth363429a2008-09-12 01:34:28 -03002116
Mauro Carvalho Chehab71d1b2b2014-09-24 10:15:22 -03002117 if (copy_to_user((void __user *)tvps->props, tvp,
2118 tvps->num * sizeof(struct dtv_property))) {
Steven Toth363429a2008-09-12 01:34:28 -03002119 err = -EFAULT;
2120 goto out;
2121 }
2122
Steven Toth363429a2008-09-12 01:34:28 -03002123 } else
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002124 err = -EOPNOTSUPP;
Steven Toth363429a2008-09-12 01:34:28 -03002125
Steven Tothe7fee0f32008-09-11 10:23:01 -03002126out:
2127 kfree(tvp);
Steven Toth13c97bf2008-09-04 21:19:43 -03002128 return err;
2129}
2130
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002131static int dtv_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002132{
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002133 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2134 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2135 struct dvb_frontend_tune_settings fetunesettings;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002136 u32 rolloff = 0;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002137
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002138 if (dvb_frontend_check_parameters(fe) < 0)
2139 return -EINVAL;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002140
2141 /*
Chris Rankin556a0442012-04-06 18:38:18 -03002142 * Initialize output parameters to match the values given by
2143 * the user. FE_SET_FRONTEND triggers an initial frontend event
2144 * with status = 0, which copies output parameters to userspace.
2145 */
2146 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
2147
2148 /*
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002149 * Be sure that the bandwidth will be filled for all
2150 * non-satellite systems, as tuners need to know what
2151 * low pass/Nyquist half filter should be applied, in
2152 * order to avoid inter-channel noise.
2153 *
2154 * ISDB-T and DVB-T/T2 already sets bandwidth.
2155 * ATSC and DVB-C don't set, so, the core should fill it.
2156 *
2157 * On DVB-C Annex A and C, the bandwidth is a function of
2158 * the roll-off and symbol rate. Annex B defines different
2159 * roll-off factors depending on the modulation. Fortunately,
2160 * Annex B is only used with 6MHz, so there's no need to
2161 * calculate it.
2162 *
2163 * While not officially supported, a side effect of handling it at
2164 * the cache level is that a program could retrieve the bandwidth
2165 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2166 */
2167 switch (c->delivery_system) {
2168 case SYS_ATSC:
2169 case SYS_DVBC_ANNEX_B:
2170 c->bandwidth_hz = 6000000;
2171 break;
2172 case SYS_DVBC_ANNEX_A:
2173 rolloff = 115;
2174 break;
2175 case SYS_DVBC_ANNEX_C:
2176 rolloff = 113;
2177 break;
Mauro Carvalho Chehabc623ed62014-08-22 10:30:17 -05002178 case SYS_DVBS:
2179 case SYS_TURBO:
Akihiro Tsukada8e281fa2014-10-31 10:19:39 -03002180 case SYS_ISDBS:
Mauro Carvalho Chehabc623ed62014-08-22 10:30:17 -05002181 rolloff = 135;
2182 break;
2183 case SYS_DVBS2:
2184 switch (c->rolloff) {
2185 case ROLLOFF_20:
2186 rolloff = 120;
2187 break;
2188 case ROLLOFF_25:
2189 rolloff = 125;
2190 break;
2191 default:
2192 case ROLLOFF_35:
2193 rolloff = 135;
2194 }
2195 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002196 default:
2197 break;
2198 }
2199 if (rolloff)
Antti Palosaari2a80f292015-04-14 15:12:54 -03002200 c->bandwidth_hz = mult_frac(c->symbol_rate, rolloff, 100);
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002201
2202 /* force auto frequency inversion if requested */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002203 if (dvb_force_auto_inversion)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002204 c->inversion = INVERSION_AUTO;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002205
2206 /*
2207 * without hierarchical coding code_rate_LP is irrelevant,
2208 * so we tolerate the otherwise invalid FEC_NONE setting
2209 */
2210 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2211 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002212
2213 /* get frontend-specific tuning settings */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002214 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002215 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2216 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2217 fepriv->max_drift = fetunesettings.max_drift;
2218 fepriv->step_size = fetunesettings.step_size;
2219 } else {
2220 /* default values */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002221 switch (c->delivery_system) {
Mauro Carvalho Chehab910a5f22012-05-07 15:01:37 -03002222 case SYS_DVBS:
2223 case SYS_DVBS2:
2224 case SYS_ISDBS:
2225 case SYS_TURBO:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002226 case SYS_DVBC_ANNEX_A:
2227 case SYS_DVBC_ANNEX_C:
2228 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002229 fepriv->step_size = c->symbol_rate / 16000;
2230 fepriv->max_drift = c->symbol_rate / 2000;
2231 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002232 case SYS_DVBT:
2233 case SYS_DVBT2:
2234 case SYS_ISDBT:
Antti Palosaari224b6642012-08-12 22:33:21 -03002235 case SYS_DTMB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002236 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002237 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2238 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2239 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002240 default:
2241 /*
2242 * FIXME: This sounds wrong! if freqency_stepsize is
2243 * defined by the frontend, why not use it???
2244 */
2245 fepriv->min_delay = HZ / 20;
2246 fepriv->step_size = 0; /* no zigzag */
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002247 fepriv->max_drift = 0;
2248 break;
2249 }
2250 }
2251 if (dvb_override_tune_delay > 0)
2252 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2253
2254 fepriv->state = FESTATE_RETUNE;
2255
2256 /* Request the search algorithm to search */
2257 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2258
2259 dvb_frontend_clear_events(fe);
2260 dvb_frontend_add_event(fe, 0);
2261 dvb_frontend_wakeup(fe);
2262 fepriv->status = 0;
2263
2264 return 0;
2265}
2266
2267
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002268static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03002269 unsigned int cmd, void *parg)
2270{
2271 struct dvb_device *dvbdev = file->private_data;
2272 struct dvb_frontend *fe = dvbdev->priv;
2273 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002274 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002275 int err = -EOPNOTSUPP;
Steven Toth13c97bf2008-09-04 21:19:43 -03002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 switch (cmd) {
2278 case FE_GET_INFO: {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002279 struct dvb_frontend_info* info = parg;
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002280
Patrick Boettcherdea74862006-05-14 05:01:31 -03002281 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
Guillaume Audirac2030c032010-05-06 09:30:25 -03002282 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002284 /*
2285 * Associate the 4 delivery systems supported by DVBv3
2286 * API with their DVBv5 counterpart. For the other standards,
2287 * use the closest type, assuming that it would hopefully
2288 * work with a DVBv3 application.
2289 * It should be noticed that, on multi-frontend devices with
2290 * different types (terrestrial and cable, for example),
2291 * a pure DVBv3 application won't be able to use all delivery
2292 * systems. Yet, changing the DVBv5 cache to the other delivery
2293 * system should be enough for making it work.
2294 */
2295 switch (dvbv3_type(c->delivery_system)) {
2296 case DVBV3_QPSK:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002297 info->type = FE_QPSK;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002298 break;
2299 case DVBV3_ATSC:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002300 info->type = FE_ATSC;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002301 break;
2302 case DVBV3_QAM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002303 info->type = FE_QAM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002304 break;
2305 case DVBV3_OFDM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002306 info->type = FE_OFDM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002307 break;
2308 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002309 dev_err(fe->dvb->device,
2310 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2311 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002312 fe->ops.info.type = FE_OFDM;
2313 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002314 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2315 __func__, c->delivery_system, fe->ops.info.type);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2318 * do it, it is done for it. */
2319 info->caps |= FE_CAN_INVERSION_AUTO;
2320 err = 0;
2321 break;
2322 }
2323
Peter Beutner6757ccc2005-07-07 17:57:36 -07002324 case FE_READ_STATUS: {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002325 enum fe_status *status = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002327 /* if retune was requested but hasn't occurred yet, prevent
Peter Beutner6757ccc2005-07-07 17:57:36 -07002328 * that user get signal state from previous tuning */
Janne Grunau01886252009-09-01 19:23:09 -03002329 if (fepriv->state == FESTATE_RETUNE ||
2330 fepriv->state == FESTATE_ERROR) {
Peter Beutner6757ccc2005-07-07 17:57:36 -07002331 err=0;
2332 *status = 0;
2333 break;
2334 }
2335
Patrick Boettcherdea74862006-05-14 05:01:31 -03002336 if (fe->ops.read_status)
2337 err = fe->ops.read_status(fe, status);
Peter Beutner6757ccc2005-07-07 17:57:36 -07002338 break;
2339 }
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 case FE_READ_BER:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002342 if (fe->ops.read_ber) {
2343 if (fepriv->thread)
2344 err = fe->ops.read_ber(fe, (__u32 *) parg);
2345 else
2346 err = -EAGAIN;
2347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 break;
2349
2350 case FE_READ_SIGNAL_STRENGTH:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002351 if (fe->ops.read_signal_strength) {
2352 if (fepriv->thread)
2353 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2354 else
2355 err = -EAGAIN;
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 break;
2358
2359 case FE_READ_SNR:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002360 if (fe->ops.read_snr) {
2361 if (fepriv->thread)
2362 err = fe->ops.read_snr(fe, (__u16 *) parg);
2363 else
2364 err = -EAGAIN;
2365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 break;
2367
2368 case FE_READ_UNCORRECTED_BLOCKS:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002369 if (fe->ops.read_ucblocks) {
2370 if (fepriv->thread)
2371 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2372 else
2373 err = -EAGAIN;
2374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 break;
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 case FE_DISEQC_RESET_OVERLOAD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002378 if (fe->ops.diseqc_reset_overload) {
2379 err = fe->ops.diseqc_reset_overload(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 fepriv->state = FESTATE_DISEQC;
2381 fepriv->status = 0;
2382 }
2383 break;
2384
2385 case FE_DISEQC_SEND_MASTER_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002386 if (fe->ops.diseqc_send_master_cmd) {
Dan Carpenter8d7e5062015-06-06 13:55:22 -03002387 struct dvb_diseqc_master_cmd *cmd = parg;
2388
2389 if (cmd->msg_len > sizeof(cmd->msg)) {
2390 err = -EINVAL;
2391 break;
2392 }
2393 err = fe->ops.diseqc_send_master_cmd(fe, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 fepriv->state = FESTATE_DISEQC;
2395 fepriv->status = 0;
2396 }
2397 break;
2398
2399 case FE_DISEQC_SEND_BURST:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002400 if (fe->ops.diseqc_send_burst) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002401 err = fe->ops.diseqc_send_burst(fe,
2402 (enum fe_sec_mini_cmd)parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 fepriv->state = FESTATE_DISEQC;
2404 fepriv->status = 0;
2405 }
2406 break;
2407
2408 case FE_SET_TONE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002409 if (fe->ops.set_tone) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002410 err = fe->ops.set_tone(fe,
2411 (enum fe_sec_tone_mode)parg);
2412 fepriv->tone = (enum fe_sec_tone_mode)parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 fepriv->state = FESTATE_DISEQC;
2414 fepriv->status = 0;
2415 }
2416 break;
2417
2418 case FE_SET_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002419 if (fe->ops.set_voltage) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002420 err = fe->ops.set_voltage(fe,
2421 (enum fe_sec_voltage)parg);
2422 fepriv->voltage = (enum fe_sec_voltage)parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 fepriv->state = FESTATE_DISEQC;
2424 fepriv->status = 0;
2425 }
2426 break;
2427
2428 case FE_DISHNETWORK_SEND_LEGACY_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002429 if (fe->ops.dishnetwork_send_legacy_command) {
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -03002430 err = fe->ops.dishnetwork_send_legacy_command(fe,
2431 (unsigned long)parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 fepriv->state = FESTATE_DISEQC;
2433 fepriv->status = 0;
Patrick Boettcherdea74862006-05-14 05:01:31 -03002434 } else if (fe->ops.set_voltage) {
NooneImportant83b75b02005-11-08 21:35:27 -08002435 /*
2436 * NOTE: This is a fallback condition. Some frontends
2437 * (stv0299 for instance) take longer than 8msec to
2438 * respond to a set_voltage command. Those switches
2439 * need custom routines to switch properly. For all
Guillaume Audirac2030c032010-05-06 09:30:25 -03002440 * other frontends, the following should work ok.
NooneImportant83b75b02005-11-08 21:35:27 -08002441 * Dish network legacy switches (as used by Dish500)
2442 * are controlled by sending 9-bit command words
2443 * spaced 8msec apart.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002444 * the actual command word is switch/port dependent
NooneImportant83b75b02005-11-08 21:35:27 -08002445 * so it is up to the userspace application to send
2446 * the right command.
2447 * The command must always start with a '0' after
2448 * initialization, so parg is 8 bits and does not
2449 * include the initialization or start bit
2450 */
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002451 unsigned long swcmd = ((unsigned long) parg) << 1;
Tina Ruchandani9056a232015-05-31 04:17:06 -03002452 ktime_t nexttime;
2453 ktime_t tv[10];
NooneImportant83b75b02005-11-08 21:35:27 -08002454 int i;
2455 u8 last = 1;
2456 if (dvb_frontend_debug)
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002457 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
Tina Ruchandani9056a232015-05-31 04:17:06 -03002458 nexttime = ktime_get_real();
NooneImportant83b75b02005-11-08 21:35:27 -08002459 if (dvb_frontend_debug)
Ezequiel Garciab9b1b3a2012-10-23 15:57:23 -03002460 tv[0] = nexttime;
NooneImportant83b75b02005-11-08 21:35:27 -08002461 /* before sending a command, initialize by sending
2462 * a 32ms 18V to the switch
2463 */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002464 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002465 dvb_frontend_sleep_until(&nexttime, 32000);
2466
2467 for (i = 0; i < 9; i++) {
2468 if (dvb_frontend_debug)
Tina Ruchandani9056a232015-05-31 04:17:06 -03002469 tv[i+1] = ktime_get_real();
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002470 if ((swcmd & 0x01) != last) {
NooneImportant83b75b02005-11-08 21:35:27 -08002471 /* set voltage to (last ? 13V : 18V) */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002472 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002473 last = (last) ? 0 : 1;
2474 }
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002475 swcmd = swcmd >> 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002476 if (i != 8)
2477 dvb_frontend_sleep_until(&nexttime, 8000);
2478 }
2479 if (dvb_frontend_debug) {
2480 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002481 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -08002482 for (i = 1; i < 10; i++)
Tina Ruchandani9056a232015-05-31 04:17:06 -03002483 printk("%d: %d\n", i,
2484 (int) ktime_us_delta(tv[i], tv[i-1]));
NooneImportant83b75b02005-11-08 21:35:27 -08002485 }
2486 err = 0;
2487 fepriv->state = FESTATE_DISEQC;
2488 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490 break;
2491
2492 case FE_DISEQC_RECV_SLAVE_REPLY:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002493 if (fe->ops.diseqc_recv_slave_reply)
2494 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 break;
2496
2497 case FE_ENABLE_HIGH_LNB_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002498 if (fe->ops.enable_high_lnb_voltage)
2499 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 break;
2501
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002502 case FE_SET_FRONTEND:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002503 err = dvbv3_set_delivery_system(fe);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03002504 if (err)
2505 break;
2506
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002507 err = dtv_property_cache_sync(fe, c, parg);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002508 if (err)
2509 break;
2510 err = dtv_set_frontend(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 case FE_GET_EVENT:
2513 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2514 break;
2515
2516 case FE_GET_FRONTEND:
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002517 err = dtv_get_frontend(fe, parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 break;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002519
2520 case FE_SET_FRONTEND_TUNE_MODE:
Hans Verkuile18828e2006-01-09 15:25:28 -02002521 fepriv->tune_mode_flags = (unsigned long) parg;
Trent Piepho1b172e02006-06-29 13:16:04 -03002522 err = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002523 break;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03002524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return err;
2527}
2528
Steven Toth6b73eea2008-09-04 01:12:25 -03002529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2531{
2532 struct dvb_device *dvbdev = file->private_data;
2533 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002534 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002536 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 poll_wait (file, &fepriv->events.wait_queue, wait);
2539
2540 if (fepriv->events.eventw != fepriv->events.eventr)
2541 return (POLLIN | POLLRDNORM | POLLPRI);
2542
2543 return 0;
2544}
2545
2546static int dvb_frontend_open(struct inode *inode, struct file *file)
2547{
2548 struct dvb_device *dvbdev = file->private_data;
2549 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002550 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Darron Broad59b18422008-10-11 11:44:05 -03002551 struct dvb_adapter *adapter = fe->dvb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 int ret;
2553
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002554 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Shuah Khan18ed2862014-07-12 13:44:12 -03002555 if (fe->exit == DVB_FE_DEVICE_REMOVED)
matthieu castete36309f2010-05-05 15:59:20 -03002556 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Darron Broad59b18422008-10-11 11:44:05 -03002558 if (adapter->mfe_shared) {
2559 mutex_lock (&adapter->mfe_lock);
Darron Broad65946902008-10-15 13:37:59 -03002560
2561 if (adapter->mfe_dvbdev == NULL)
2562 adapter->mfe_dvbdev = dvbdev;
2563
2564 else if (adapter->mfe_dvbdev != dvbdev) {
2565 struct dvb_device
2566 *mfedev = adapter->mfe_dvbdev;
2567 struct dvb_frontend
2568 *mfe = mfedev->priv;
2569 struct dvb_frontend_private
2570 *mfepriv = mfe->frontend_priv;
2571 int mferetry = (dvb_mfe_wait_time << 1);
2572
2573 mutex_unlock (&adapter->mfe_lock);
2574 while (mferetry-- && (mfedev->users != -1 ||
2575 mfepriv->thread != NULL)) {
2576 if(msleep_interruptible(500)) {
2577 if(signal_pending(current))
2578 return -EINTR;
Darron Broad59b18422008-10-11 11:44:05 -03002579 }
2580 }
Darron Broad65946902008-10-15 13:37:59 -03002581
2582 mutex_lock (&adapter->mfe_lock);
2583 if(adapter->mfe_dvbdev != dvbdev) {
2584 mfedev = adapter->mfe_dvbdev;
2585 mfe = mfedev->priv;
2586 mfepriv = mfe->frontend_priv;
2587 if (mfedev->users != -1 ||
2588 mfepriv->thread != NULL) {
2589 mutex_unlock (&adapter->mfe_lock);
2590 return -EBUSY;
2591 }
2592 adapter->mfe_dvbdev = dvbdev;
2593 }
Darron Broad59b18422008-10-11 11:44:05 -03002594 }
2595 }
2596
Oliver Endriss48136e12007-08-25 12:00:23 -03002597 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2598 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
Darron Broad59b18422008-10-11 11:44:05 -03002599 goto err0;
Devin Heitmueller2d196932011-07-04 21:55:01 -03002600
2601 /* If we took control of the bus, we need to force
2602 reinitialization. This is because many ts_bus_ctrl()
2603 functions strobe the RESET pin on the demod, and if the
2604 frontend thread already exists then the dvb_init() routine
2605 won't get called (which is what usually does initial
2606 register configuration). */
2607 fepriv->reinitialise = 1;
Steven Tothba7e6f32006-09-25 12:41:53 -03002608 }
2609
Oliver Endriss48136e12007-08-25 12:00:23 -03002610 if ((ret = dvb_generic_open (inode, file)) < 0)
2611 goto err1;
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002612
Oliver Endriss48136e12007-08-25 12:00:23 -03002613 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002614 /* normal tune mode when opened R/W */
2615 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2616 fepriv->tone = -1;
2617 fepriv->voltage = -1;
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 ret = dvb_frontend_start (fe);
2620 if (ret)
Oliver Endriss48136e12007-08-25 12:00:23 -03002621 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 /* empty event queue */
2624 fepriv->events.eventr = fepriv->events.eventw = 0;
2625 }
2626
Darron Broad59b18422008-10-11 11:44:05 -03002627 if (adapter->mfe_shared)
2628 mutex_unlock (&adapter->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return ret;
Oliver Endriss48136e12007-08-25 12:00:23 -03002630
2631err2:
2632 dvb_generic_release(inode, file);
2633err1:
2634 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2635 fe->ops.ts_bus_ctrl(fe, 0);
Darron Broad59b18422008-10-11 11:44:05 -03002636err0:
2637 if (adapter->mfe_shared)
2638 mutex_unlock (&adapter->mfe_lock);
Oliver Endriss48136e12007-08-25 12:00:23 -03002639 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
2642static int dvb_frontend_release(struct inode *inode, struct file *file)
2643{
2644 struct dvb_device *dvbdev = file->private_data;
2645 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002646 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002647 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002649 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002651 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 fepriv->release_jiffies = jiffies;
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002653 mb();
2654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002656 ret = dvb_generic_release (inode, file);
2657
Oliver Endriss48136e12007-08-25 12:00:23 -03002658 if (dvbdev->users == -1) {
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002659 wake_up(&fepriv->wait_queue);
Shuah Khan18ed2862014-07-12 13:44:12 -03002660 if (fe->exit != DVB_FE_NO_EXIT)
Oliver Endriss48136e12007-08-25 12:00:23 -03002661 wake_up(&dvbdev->wait_queue);
Oliver Endriss48136e12007-08-25 12:00:23 -03002662 if (fe->ops.ts_bus_ctrl)
2663 fe->ops.ts_bus_ctrl(fe, 0);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002664 }
Oliver Endriss48136e12007-08-25 12:00:23 -03002665
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002666 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
Jan Engelhardt784e29d2009-01-11 06:12:43 -03002669static const struct file_operations dvb_frontend_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 .owner = THIS_MODULE,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002671 .unlocked_ioctl = dvb_generic_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 .poll = dvb_frontend_poll,
2673 .open = dvb_frontend_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002674 .release = dvb_frontend_release,
2675 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676};
2677
Antti Palosaari06bae122012-08-14 22:21:06 -03002678int dvb_frontend_suspend(struct dvb_frontend *fe)
2679{
2680 int ret = 0;
2681
2682 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2683 fe->id);
2684
Mauro Carvalho Chehab59d78892014-08-09 21:47:19 -03002685 if (fe->ops.tuner_ops.suspend)
2686 ret = fe->ops.tuner_ops.suspend(fe);
2687 else if (fe->ops.tuner_ops.sleep)
Antti Palosaari06bae122012-08-14 22:21:06 -03002688 ret = fe->ops.tuner_ops.sleep(fe);
2689
2690 if (fe->ops.sleep)
2691 ret = fe->ops.sleep(fe);
2692
2693 return ret;
2694}
2695EXPORT_SYMBOL(dvb_frontend_suspend);
2696
2697int dvb_frontend_resume(struct dvb_frontend *fe)
2698{
2699 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2700 int ret = 0;
2701
2702 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2703 fe->id);
2704
Shuah Khan87cd0fa2014-07-24 13:02:14 -03002705 fe->exit = DVB_FE_DEVICE_RESUME;
Antti Palosaari06bae122012-08-14 22:21:06 -03002706 if (fe->ops.init)
2707 ret = fe->ops.init(fe);
2708
Mauro Carvalho Chehab59d78892014-08-09 21:47:19 -03002709 if (fe->ops.tuner_ops.resume)
2710 ret = fe->ops.tuner_ops.resume(fe);
2711 else if (fe->ops.tuner_ops.init)
Antti Palosaari06bae122012-08-14 22:21:06 -03002712 ret = fe->ops.tuner_ops.init(fe);
2713
Shuah Khan87cd0fa2014-07-24 13:02:14 -03002714 fe->exit = DVB_FE_NO_EXIT;
Antti Palosaari06bae122012-08-14 22:21:06 -03002715 fepriv->state = FESTATE_RETUNE;
2716 dvb_frontend_wakeup(fe);
2717
2718 return ret;
2719}
2720EXPORT_SYMBOL(dvb_frontend_resume);
2721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722int dvb_register_frontend(struct dvb_adapter* dvb,
2723 struct dvb_frontend* fe)
2724{
2725 struct dvb_frontend_private *fepriv;
Mauro Carvalho Chehabc1b31b12015-01-02 11:00:17 -03002726 const struct dvb_device dvbdev_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 .users = ~0,
2728 .writers = 1,
2729 .readers = (~0)-1,
2730 .fops = &dvb_frontend_fops,
Mauro Carvalho Chehabc1b31b12015-01-02 11:00:17 -03002731#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
2732 .name = fe->ops.info.name,
2733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 .kernel_ioctl = dvb_frontend_ioctl
2735 };
2736
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002737 dev_dbg(dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Ingo Molnar3593cab2006-02-07 06:49:14 -02002739 if (mutex_lock_interruptible(&frontend_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 return -ERESTARTSYS;
2741
Panagiotis Issaris74081872006-01-11 19:40:56 -02002742 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 if (fe->frontend_priv == NULL) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002744 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return -ENOMEM;
2746 }
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002747 fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Thomas Gleixnera0a47142010-09-07 11:33:27 -03002749 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 init_waitqueue_head (&fepriv->wait_queue);
2751 init_waitqueue_head (&fepriv->events.wait_queue);
Matthias Kaehlcke03b76122007-07-30 14:58:10 -03002752 mutex_init(&fepriv->events.mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 fe->dvb = dvb;
2754 fepriv->inversion = INVERSION_OFF;
2755
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002756 dev_info(fe->dvb->device,
2757 "DVB: registering adapter %i frontend %i (%s)...\n",
2758 fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2761 fe, DVB_DEVICE_FRONTEND);
2762
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002763 /*
2764 * Initialize the cache to the proper values according with the
2765 * first supported delivery system (ops->delsys[0])
2766 */
Mauro Carvalho Chehab240ab502012-01-10 18:00:50 -03002767
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002768 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002769 dvb_frontend_clear_cache(fe);
2770
Ingo Molnar3593cab2006-02-07 06:49:14 -02002771 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return 0;
2773}
2774EXPORT_SYMBOL(dvb_register_frontend);
2775
2776int dvb_unregister_frontend(struct dvb_frontend* fe)
2777{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002778 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002779 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Markus Rechberger57861b42007-04-14 10:19:18 -03002781 mutex_lock(&frontend_mutex);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002782 dvb_frontend_stop (fe);
Markus Rechberger57861b42007-04-14 10:19:18 -03002783 mutex_unlock(&frontend_mutex);
2784
2785 if (fepriv->dvbdev->users < -1)
2786 wait_event(fepriv->dvbdev->wait_queue,
2787 fepriv->dvbdev->users==-1);
2788
Ingo Molnar3593cab2006-02-07 06:49:14 -02002789 mutex_lock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 dvb_unregister_device (fepriv->dvbdev);
Andrew de Quinceyd9955062006-08-08 09:10:08 -03002791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 /* fe is invalid now */
2793 kfree(fepriv);
Ingo Molnar3593cab2006-02-07 06:49:14 -02002794 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return 0;
2796}
2797EXPORT_SYMBOL(dvb_unregister_frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002798
Mauro Carvalho Chehab149ef722008-04-29 21:38:46 -03002799#ifdef CONFIG_MEDIA_ATTACH
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002800void dvb_frontend_detach(struct dvb_frontend* fe)
2801{
2802 void *ptr;
2803
2804 if (fe->ops.release_sec) {
2805 fe->ops.release_sec(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002806 dvb_detach(fe->ops.release_sec);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002807 }
2808 if (fe->ops.tuner_ops.release) {
2809 fe->ops.tuner_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002810 dvb_detach(fe->ops.tuner_ops.release);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002811 }
Michael Krufky2426a272007-12-21 11:34:45 -03002812 if (fe->ops.analog_ops.release) {
2813 fe->ops.analog_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002814 dvb_detach(fe->ops.analog_ops.release);
Michael Krufky2426a272007-12-21 11:34:45 -03002815 }
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002816 ptr = (void*)fe->ops.release;
2817 if (ptr) {
2818 fe->ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002819 dvb_detach(ptr);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002820 }
2821}
2822#else
2823void dvb_frontend_detach(struct dvb_frontend* fe)
2824{
2825 if (fe->ops.release_sec)
2826 fe->ops.release_sec(fe);
2827 if (fe->ops.tuner_ops.release)
2828 fe->ops.tuner_ops.release(fe);
Michael Krufky2426a272007-12-21 11:34:45 -03002829 if (fe->ops.analog_ops.release)
2830 fe->ops.analog_ops.release(fe);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002831 if (fe->ops.release)
2832 fe->ops.release(fe);
2833}
2834#endif
2835EXPORT_SYMBOL(dvb_frontend_detach);