blob: 6cc2631d8f0e29e6637b1a9c06c0bcbeca2d9bbc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dvb_frontend.c: DVB frontend tuning interface/thread
3 *
4 *
5 * Copyright (C) 1999-2001 Ralph Metzler
6 * Marcus Metzler
7 * Holger Waechtler
8 * for convergence integrated media GmbH
9 *
10 * Copyright (C) 2004 Andrew de Quincey (tuning thread cleanup)
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26 */
27
Mauro Carvalho Chehab8de85942011-12-26 20:22:50 -030028/* Enables DVBv3 compatibility bits at the headers */
29#define __DVB_CORE__
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/string.h>
32#include <linux/kernel.h>
33#include <linux/sched.h>
34#include <linux/wait.h>
35#include <linux/slab.h>
36#include <linux/poll.h>
Andrew Morton4c4cb1b2009-12-01 13:17:41 -080037#include <linux/semaphore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/list.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080040#include <linux/freezer.h>
Johannes Stezenbachcc89c222005-06-23 22:02:39 -070041#include <linux/jiffies.h>
Herbert Poetzl8eec1422007-02-08 14:32:43 -030042#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include "dvb_frontend.h"
46#include "dvbdev.h"
Steven Totheacf8d82008-09-26 00:29:49 -030047#include <linux/dvb/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static int dvb_frontend_debug;
Oliver Endriss849be2c2007-08-25 12:22:16 -030050static int dvb_shutdown_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051static int dvb_force_auto_inversion;
52static int dvb_override_tune_delay;
53static int dvb_powerdown_on_sleep = 1;
Darron Broad59b18422008-10-11 11:44:05 -030054static int dvb_mfe_wait_time = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
Johannes Stezenbachf4f009a2005-05-16 21:54:21 -070057MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
Manu Abraham6baad3f2006-02-27 00:09:32 -030058module_param(dvb_shutdown_timeout, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
Manu Abraham6baad3f2006-02-27 00:09:32 -030060module_param(dvb_force_auto_inversion, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
Manu Abraham6baad3f2006-02-27 00:09:32 -030062module_param(dvb_override_tune_delay, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
Manu Abraham6baad3f2006-02-27 00:09:32 -030064module_param(dvb_powerdown_on_sleep, int, 0644);
Uwe Buglaa85585772006-04-11 10:21:37 -030065MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
Darron Broad59b18422008-10-11 11:44:05 -030066module_param(dvb_mfe_wait_time, int, 0644);
67MODULE_PARM_DESC(dvb_mfe_wait_time, "Wait up to <mfe_wait_time> seconds on open() for multi-frontend to become available (default:5 seconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define FESTATE_IDLE 1
70#define FESTATE_RETUNE 2
71#define FESTATE_TUNING_FAST 4
72#define FESTATE_TUNING_SLOW 8
73#define FESTATE_TUNED 16
74#define FESTATE_ZIGZAG_FAST 32
75#define FESTATE_ZIGZAG_SLOW 64
76#define FESTATE_DISEQC 128
Janne Grunau01886252009-09-01 19:23:09 -030077#define FESTATE_ERROR 256
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
79#define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
80#define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
81#define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
Manu Abraham0249ef12006-06-21 10:27:31 -030082
83#define FE_ALGO_HW 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
86 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
87 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
88 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
89 * FESTATE_TUNED. The frontend has successfully locked on.
90 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
91 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
92 * FESTATE_DISEQC. A DISEQC command has just been issued.
93 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
94 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
95 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
96 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
97 */
98
matthieu castete36309f2010-05-05 15:59:20 -030099#define DVB_FE_NO_EXIT 0
100#define DVB_FE_NORMAL_EXIT 1
101#define DVB_FE_DEVICE_REMOVED 2
102
Ingo Molnar3593cab2006-02-07 06:49:14 -0200103static DEFINE_MUTEX(frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105struct dvb_frontend_private {
106
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200107 /* thread/frontend values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 struct dvb_device *dvbdev;
Andreas Oberrittera5959db2011-05-08 20:03:40 -0300109 struct dvb_frontend_parameters parameters_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct dvb_fe_events events;
111 struct semaphore sem;
112 struct list_head list_head;
113 wait_queue_head_t wait_queue;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300114 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 unsigned long release_jiffies;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200116 unsigned int exit;
117 unsigned int wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 fe_status_t status;
Peter Beutner400b7082006-01-09 15:32:43 -0200119 unsigned long tune_mode_flags;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200120 unsigned int delay;
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300121 unsigned int reinitialise;
Andrew de Quincey64454012006-04-06 14:32:23 -0300122 int tone;
123 int voltage;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200124
125 /* swzigzag values */
126 unsigned int state;
127 unsigned int bending;
128 int lnb_drift;
129 unsigned int inversion;
130 unsigned int auto_step;
131 unsigned int auto_sub_step;
132 unsigned int started_auto_step;
133 unsigned int min_delay;
134 unsigned int max_drift;
135 unsigned int step_size;
136 int quality;
137 unsigned int check_wrapped;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300138 enum dvbfe_search algo_status;
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
202{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700203 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 struct dvb_fe_events *events = &fepriv->events;
205 struct dvb_frontend_event *e;
206 int wp;
207
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300208 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300210 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -0300211 dtv_get_frontend(fe, &fepriv->parameters_out);
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300212
213 mutex_lock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 wp = (events->eventw + 1) % MAX_EVENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (wp == events->eventr) {
217 events->overflow = 1;
218 events->eventr = (events->eventr + 1) % MAX_EVENT;
219 }
220
221 e = &events->events[events->eventw];
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300222 e->status = status;
Andreas Oberrittera5959db2011-05-08 20:03:40 -0300223 e->parameters = fepriv->parameters_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 events->eventw = wp;
226
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300227 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 wake_up_interruptible (&events->wait_queue);
230}
231
232static int dvb_frontend_get_event(struct dvb_frontend *fe,
233 struct dvb_frontend_event *event, int flags)
234{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700235 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct dvb_fe_events *events = &fepriv->events;
237
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300238 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (events->overflow) {
241 events->overflow = 0;
242 return -EOVERFLOW;
243 }
244
245 if (events->eventw == events->eventr) {
246 int ret;
247
248 if (flags & O_NONBLOCK)
249 return -EWOULDBLOCK;
250
251 up(&fepriv->sem);
252
253 ret = wait_event_interruptible (events->wait_queue,
254 events->eventw != events->eventr);
255
256 if (down_interruptible (&fepriv->sem))
257 return -ERESTARTSYS;
258
259 if (ret < 0)
260 return ret;
261 }
262
Andreas Oberritter77b1e2f2011-08-04 12:33:15 -0300263 mutex_lock(&events->mtx);
264 *event = events->events[events->eventr];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 events->eventr = (events->eventr + 1) % MAX_EVENT;
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300266 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 return 0;
269}
270
Andreas Oberritter20640be2011-08-04 12:33:14 -0300271static void dvb_frontend_clear_events(struct dvb_frontend *fe)
272{
273 struct dvb_frontend_private *fepriv = fe->frontend_priv;
274 struct dvb_fe_events *events = &fepriv->events;
275
276 mutex_lock(&events->mtx);
277 events->eventr = events->eventw;
278 mutex_unlock(&events->mtx);
279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static void dvb_frontend_init(struct dvb_frontend *fe)
282{
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300283 dev_dbg(fe->dvb->device,
284 "%s: initialising adapter %i frontend %i (%s)...\n",
285 __func__, fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Patrick Boettcherdea74862006-05-14 05:01:31 -0300287 if (fe->ops.init)
288 fe->ops.init(fe);
289 if (fe->ops.tuner_ops.init) {
Manu Abraham3b37a152008-10-20 18:14:14 -0300290 if (fe->ops.i2c_gate_ctrl)
291 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300292 fe->ops.tuner_ops.init(fe);
293 if (fe->ops.i2c_gate_ctrl)
294 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300298void dvb_frontend_reinitialise(struct dvb_frontend *fe)
299{
300 struct dvb_frontend_private *fepriv = fe->frontend_priv;
301
302 fepriv->reinitialise = 1;
303 dvb_frontend_wakeup(fe);
304}
305EXPORT_SYMBOL(dvb_frontend_reinitialise);
306
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200307static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200309 int q2;
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300310 struct dvb_frontend *fe = fepriv->dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300312 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200314 if (locked)
315 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
316 else
317 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200319 q2 = fepriv->quality - 128;
320 q2 *= q2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200322 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/**
326 * Performs automatic twiddling of frontend parameters.
327 *
328 * @param fe The frontend concerned.
329 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
330 * @returns Number of complete iterations that have been performed.
331 */
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200332static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
334 int autoinversion;
335 int ready = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300336 int fe_set_err = 0;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700337 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300338 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
339 int original_inversion = c->inversion;
340 u32 original_frequency = c->frequency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* are we using autoinversion? */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300343 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300344 (c->inversion == INVERSION_AUTO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 /* setup parameters correctly */
347 while(!ready) {
348 /* calculate the lnb_drift */
349 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
350
351 /* wrap the auto_step if we've exceeded the maximum drift */
352 if (fepriv->lnb_drift > fepriv->max_drift) {
353 fepriv->auto_step = 0;
354 fepriv->auto_sub_step = 0;
355 fepriv->lnb_drift = 0;
356 }
357
358 /* perform inversion and +/- zigzag */
359 switch(fepriv->auto_sub_step) {
360 case 0:
361 /* try with the current inversion and current drift setting */
362 ready = 1;
363 break;
364
365 case 1:
366 if (!autoinversion) break;
367
368 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
369 ready = 1;
370 break;
371
372 case 2:
373 if (fepriv->lnb_drift == 0) break;
374
375 fepriv->lnb_drift = -fepriv->lnb_drift;
376 ready = 1;
377 break;
378
379 case 3:
380 if (fepriv->lnb_drift == 0) break;
381 if (!autoinversion) break;
382
383 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
384 fepriv->lnb_drift = -fepriv->lnb_drift;
385 ready = 1;
386 break;
387
388 default:
389 fepriv->auto_step++;
390 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
391 break;
392 }
393
394 if (!ready) fepriv->auto_sub_step++;
395 }
396
397 /* if this attempt would hit where we started, indicate a complete
398 * iteration has occurred */
399 if ((fepriv->auto_step == fepriv->started_auto_step) &&
400 (fepriv->auto_sub_step == 0) && check_wrapped) {
401 return 1;
402 }
403
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300404 dev_dbg(fe->dvb->device, "%s: drift:%i inversion:%i auto_step:%i " \
405 "auto_sub_step:%i started_auto_step:%i\n",
406 __func__, fepriv->lnb_drift, fepriv->inversion,
407 fepriv->auto_step, fepriv->auto_sub_step,
408 fepriv->started_auto_step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* set the frontend itself */
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300411 c->frequency += fepriv->lnb_drift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 if (autoinversion)
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300413 c->inversion = fepriv->inversion;
414 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300415 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300416 fe_set_err = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300417 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300418 if (fe_set_err < 0) {
419 fepriv->state = FESTATE_ERROR;
420 return fe_set_err;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300423 c->frequency = original_frequency;
424 c->inversion = original_inversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 fepriv->auto_sub_step++;
427 return 0;
428}
429
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200430static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
431{
Andrew de Quincey30d94642006-11-16 22:12:40 -0300432 fe_status_t s = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300433 int retval = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200434 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300435 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200436
437 /* if we've got no parameters, just keep idling */
438 if (fepriv->state & FESTATE_IDLE) {
439 fepriv->delay = 3*HZ;
440 fepriv->quality = 0;
441 return;
442 }
443
444 /* in SCAN mode, we just set the frontend when asked and leave it alone */
445 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
446 if (fepriv->state & FESTATE_RETUNE) {
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300447 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300448 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300449 retval = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300450 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300451 if (retval < 0)
452 fepriv->state = FESTATE_ERROR;
453 else
454 fepriv->state = FESTATE_TUNED;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200455 }
456 fepriv->delay = 3*HZ;
457 fepriv->quality = 0;
458 return;
459 }
460
461 /* get the frontend status */
462 if (fepriv->state & FESTATE_RETUNE) {
463 s = 0;
464 } else {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300465 if (fe->ops.read_status)
466 fe->ops.read_status(fe, &s);
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200467 if (s != fepriv->status) {
468 dvb_frontend_add_event(fe, s);
469 fepriv->status = s;
470 }
471 }
472
473 /* if we're not tuned, and we have a lock, move to the TUNED state */
474 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
475 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
476 fepriv->state = FESTATE_TUNED;
477
478 /* if we're tuned, then we have determined the correct inversion */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300479 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300480 (c->inversion == INVERSION_AUTO)) {
481 c->inversion = fepriv->inversion;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200482 }
483 return;
484 }
485
486 /* if we are tuned already, check we're still locked */
487 if (fepriv->state & FESTATE_TUNED) {
488 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
489
490 /* we're tuned, and the lock is still good... */
491 if (s & FE_HAS_LOCK) {
492 return;
493 } else { /* if we _WERE_ tuned, but now don't have a lock */
494 fepriv->state = FESTATE_ZIGZAG_FAST;
495 fepriv->started_auto_step = fepriv->auto_step;
496 fepriv->check_wrapped = 0;
497 }
498 }
499
500 /* don't actually do anything if we're in the LOSTLOCK state,
501 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
502 if ((fepriv->state & FESTATE_LOSTLOCK) &&
Patrick Boettcherdea74862006-05-14 05:01:31 -0300503 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200504 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
505 return;
506 }
507
508 /* don't do anything if we're in the DISEQC state, since this
509 * might be someone with a motorized dish controlled by DISEQC.
510 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
511 if (fepriv->state & FESTATE_DISEQC) {
512 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
513 return;
514 }
515
516 /* if we're in the RETUNE state, set everything up for a brand
517 * new scan, keeping the current inversion setting, as the next
518 * tune is _very_ likely to require the same */
519 if (fepriv->state & FESTATE_RETUNE) {
520 fepriv->lnb_drift = 0;
521 fepriv->auto_step = 0;
522 fepriv->auto_sub_step = 0;
523 fepriv->started_auto_step = 0;
524 fepriv->check_wrapped = 0;
525 }
526
527 /* fast zigzag. */
528 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
529 fepriv->delay = fepriv->min_delay;
530
Guillaume Audirac2030c032010-05-06 09:30:25 -0300531 /* perform a tune */
Janne Grunau01886252009-09-01 19:23:09 -0300532 retval = dvb_frontend_swzigzag_autotune(fe,
533 fepriv->check_wrapped);
534 if (retval < 0) {
535 return;
536 } else if (retval) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200537 /* OK, if we've run out of trials at the fast speed.
538 * Drop back to slow for the _next_ attempt */
539 fepriv->state = FESTATE_SEARCHING_SLOW;
540 fepriv->started_auto_step = fepriv->auto_step;
541 return;
542 }
543 fepriv->check_wrapped = 1;
544
545 /* if we've just retuned, enter the ZIGZAG_FAST state.
546 * This ensures we cannot return from an
547 * FE_SET_FRONTEND ioctl before the first frontend tune
548 * occurs */
549 if (fepriv->state & FESTATE_RETUNE) {
550 fepriv->state = FESTATE_TUNING_FAST;
551 }
552 }
553
554 /* slow zigzag */
555 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
556 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
557
558 /* Note: don't bother checking for wrapping; we stay in this
559 * state until we get a lock */
560 dvb_frontend_swzigzag_autotune(fe, 0);
561 }
562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
565{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700566 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
matthieu castete36309f2010-05-05 15:59:20 -0300568 if (fepriv->exit != DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return 1;
570
571 if (fepriv->dvbdev->writers == 1)
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -0300572 if (time_after_eq(jiffies, fepriv->release_jiffies +
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200573 dvb_shutdown_timeout * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return 1;
575
576 return 0;
577}
578
579static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
580{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700581 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 if (fepriv->wakeup) {
584 fepriv->wakeup = 0;
585 return 1;
586 }
587 return dvb_frontend_is_exiting(fe);
588}
589
590static void dvb_frontend_wakeup(struct dvb_frontend *fe)
591{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700592 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 fepriv->wakeup = 1;
595 wake_up_interruptible(&fepriv->wait_queue);
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static int dvb_frontend_thread(void *data)
599{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700600 struct dvb_frontend *fe = data;
601 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 fe_status_t s;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300603 enum dvbfe_algo algo;
604
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300605 bool re_tune = false;
Juergen Lock6ae23222012-12-23 17:23:06 -0300606 bool semheld = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300608 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200610 fepriv->check_wrapped = 0;
611 fepriv->quality = 0;
612 fepriv->delay = 3*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 fepriv->wakeup = 0;
Andrew de Quincey04c56d02006-07-10 03:34:14 -0300615 fepriv->reinitialise = 0;
616
617 dvb_frontend_init(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700619 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 while (1) {
621 up(&fepriv->sem); /* is locked when we enter the thread... */
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300622restart:
Hans Verkuil94238e92011-08-27 11:30:25 -0300623 wait_event_interruptible_timeout(fepriv->wait_queue,
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700624 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
625 || freezing(current),
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300626 fepriv->delay);
627
628 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* got signal or quitting */
Juergen Lock6ae23222012-12-23 17:23:06 -0300630 if (!down_interruptible(&fepriv->sem))
631 semheld = true;
matthieu castete36309f2010-05-05 15:59:20 -0300632 fepriv->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634 }
635
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300636 if (try_to_freeze())
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300637 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (down_interruptible(&fepriv->sem))
640 break;
641
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300642 if (fepriv->reinitialise) {
643 dvb_frontend_init(fe);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300644 if (fe->ops.set_tone && fepriv->tone != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300645 fe->ops.set_tone(fe, fepriv->tone);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300646 if (fe->ops.set_voltage && fepriv->voltage != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300647 fe->ops.set_voltage(fe, fepriv->voltage);
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300648 fepriv->reinitialise = 0;
649 }
650
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200651 /* do an iteration of the tuning loop */
Manu Abrahamd772bd02006-06-24 11:18:58 -0300652 if (fe->ops.get_frontend_algo) {
Manu Abrahamc59e7872008-10-14 16:34:07 -0300653 algo = fe->ops.get_frontend_algo(fe);
654 switch (algo) {
655 case DVBFE_ALGO_HW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300656 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300657
Manu Abrahamd772bd02006-06-24 11:18:58 -0300658 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300659 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTATE_RETUNE\n", __func__);
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300660 re_tune = true;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300661 fepriv->state = FESTATE_TUNED;
Simon Arlott45145b62012-02-06 17:57:01 -0300662 } else {
663 re_tune = false;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Manu Abrahamc59e7872008-10-14 16:34:07 -0300666 if (fe->ops.tune)
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300667 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300668
Darron Broad2fac9a02008-12-18 06:27:50 -0300669 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300670 dev_dbg(fe->dvb->device, "%s: state changed, adding current state\n", __func__);
Manu Abrahamd772bd02006-06-24 11:18:58 -0300671 dvb_frontend_add_event(fe, s);
672 fepriv->status = s;
673 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300674 break;
675 case DVBFE_ALGO_SW:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300676 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
Manu Abraham70d90632006-06-29 22:05:23 -0300677 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300678 break;
679 case DVBFE_ALGO_CUSTOM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300680 dev_dbg(fe->dvb->device, "%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300681 if (fepriv->state & FESTATE_RETUNE) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300682 dev_dbg(fe->dvb->device, "%s: Retune requested, FESTAT_RETUNE\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300683 fepriv->state = FESTATE_TUNED;
684 }
685 /* Case where we are going to search for a carrier
686 * User asked us to retune again for some reason, possibly
687 * requesting a search with a new set of parameters
688 */
689 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300690 if (fe->ops.search) {
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -0300691 fepriv->algo_status = fe->ops.search(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300692 /* We did do a search as was requested, the flags are
693 * now unset as well and has the flags wrt to search.
694 */
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300695 } else {
696 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
697 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300698 }
699 /* Track the carrier if the search was successful */
Mauro Carvalho Chehab1b5d8712011-12-26 18:11:25 -0300700 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300701 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
702 fepriv->delay = HZ / 2;
703 }
Chris Rankin556a0442012-04-06 18:38:18 -0300704 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300705 fe->ops.read_status(fe, &s);
706 if (s != fepriv->status) {
707 dvb_frontend_add_event(fe, s); /* update event list */
708 fepriv->status = s;
709 if (!(s & FE_HAS_LOCK)) {
710 fepriv->delay = HZ / 10;
711 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
712 } else {
713 fepriv->delay = 60 * HZ;
714 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300715 }
716 break;
717 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300718 dev_dbg(fe->dvb->device, "%s: UNDEFINED ALGO !\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300719 break;
720 }
721 } else {
Manu Abraham4a4edcc2006-06-25 05:46:26 -0300722 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
Oliver Endriss608f62d2007-08-25 13:17:53 -0300726 if (dvb_powerdown_on_sleep) {
727 if (fe->ops.set_voltage)
728 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300729 if (fe->ops.tuner_ops.sleep) {
Devin Heitmueller41286d92008-11-16 00:44:52 -0300730 if (fe->ops.i2c_gate_ctrl)
731 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300732 fe->ops.tuner_ops.sleep(fe);
733 if (fe->ops.i2c_gate_ctrl)
734 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300735 }
Patrick Boettcherdea74862006-05-14 05:01:31 -0300736 if (fe->ops.sleep)
737 fe->ops.sleep(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300740 fepriv->thread = NULL;
matthieu castete36309f2010-05-05 15:59:20 -0300741 if (kthread_should_stop())
742 fepriv->exit = DVB_FE_DEVICE_REMOVED;
743 else
744 fepriv->exit = DVB_FE_NO_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 mb();
746
Juergen Lock6ae23222012-12-23 17:23:06 -0300747 if (semheld)
748 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 dvb_frontend_wakeup(fe);
750 return 0;
751}
752
753static void dvb_frontend_stop(struct dvb_frontend *fe)
754{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700755 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300757 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
matthieu castete36309f2010-05-05 15:59:20 -0300759 fepriv->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 mb();
761
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300762 if (!fepriv->thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return;
764
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300765 kthread_stop(fepriv->thread);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300766
Thomas Gleixnera0a47142010-09-07 11:33:27 -0300767 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 fepriv->state = FESTATE_IDLE;
769
770 /* paranoia check in case a signal arrived */
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300771 if (fepriv->thread)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300772 dev_warn(fe->dvb->device,
773 "dvb_frontend_stop: warning: thread %p won't exit\n",
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300774 fepriv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
NooneImportant83b75b02005-11-08 21:35:27 -0800777s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
778{
779 return ((curtime.tv_usec < lasttime.tv_usec) ?
780 1000000 - lasttime.tv_usec + curtime.tv_usec :
781 curtime.tv_usec - lasttime.tv_usec);
782}
783EXPORT_SYMBOL(timeval_usec_diff);
784
785static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
786{
787 curtime->tv_usec += add_usec;
788 if (curtime->tv_usec >= 1000000) {
789 curtime->tv_usec -= 1000000;
790 curtime->tv_sec++;
791 }
792}
793
794/*
795 * Sleep until gettimeofday() > waketime + add_usec
796 * This needs to be as precise as possible, but as the delay is
797 * usually between 2ms and 32ms, it is done using a scheduled msleep
798 * followed by usleep (normally a busy-wait loop) for the remainder
799 */
800void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
801{
802 struct timeval lasttime;
803 s32 delta, newdelta;
804
805 timeval_usec_add(waketime, add_usec);
806
807 do_gettimeofday(&lasttime);
808 delta = timeval_usec_diff(lasttime, *waketime);
809 if (delta > 2500) {
810 msleep((delta - 1500) / 1000);
811 do_gettimeofday(&lasttime);
812 newdelta = timeval_usec_diff(lasttime, *waketime);
813 delta = (newdelta > delta) ? 0 : newdelta;
814 }
815 if (delta > 0)
816 udelay(delta);
817}
818EXPORT_SYMBOL(dvb_frontend_sleep_until);
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820static int dvb_frontend_start(struct dvb_frontend *fe)
821{
822 int ret;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700823 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300824 struct task_struct *fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300826 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300828 if (fepriv->thread) {
matthieu castete36309f2010-05-05 15:59:20 -0300829 if (fepriv->exit == DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return 0;
831 else
832 dvb_frontend_stop (fe);
833 }
834
835 if (signal_pending(current))
836 return -EINTR;
837 if (down_interruptible (&fepriv->sem))
838 return -EINTR;
839
840 fepriv->state = FESTATE_IDLE;
matthieu castete36309f2010-05-05 15:59:20 -0300841 fepriv->exit = DVB_FE_NO_EXIT;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300842 fepriv->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 mb();
844
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300845 fe_thread = kthread_run(dvb_frontend_thread, fe,
Steven Toth363c35f2008-10-11 11:05:50 -0300846 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300847 if (IS_ERR(fe_thread)) {
848 ret = PTR_ERR(fe_thread);
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300849 dev_warn(fe->dvb->device,
850 "dvb_frontend_start: failed to start kthread (%d)\n",
851 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 up(&fepriv->sem);
853 return ret;
854 }
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300855 fepriv->thread = fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return 0;
857}
858
Guillaume Audirac2030c032010-05-06 09:30:25 -0300859static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
Oliver Endrissc471b332007-08-09 01:03:42 -0300860 u32 *freq_min, u32 *freq_max)
861{
862 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
863
864 if (fe->ops.info.frequency_max == 0)
865 *freq_max = fe->ops.tuner_ops.info.frequency_max;
866 else if (fe->ops.tuner_ops.info.frequency_max == 0)
867 *freq_max = fe->ops.info.frequency_max;
868 else
869 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
870
871 if (*freq_min == 0 || *freq_max == 0)
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300872 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
873 fe->dvb->num, fe->id);
Oliver Endrissc471b332007-08-09 01:03:42 -0300874}
875
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300876static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300877{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300878 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Oliver Endrissc471b332007-08-09 01:03:42 -0300879 u32 freq_min;
880 u32 freq_max;
881
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300882 /* range check: frequency */
Guillaume Audirac2030c032010-05-06 09:30:25 -0300883 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300884 if ((freq_min && c->frequency < freq_min) ||
885 (freq_max && c->frequency > freq_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300886 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
887 fe->dvb->num, fe->id, c->frequency,
888 freq_min, freq_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300889 return -EINVAL;
890 }
891
892 /* range check: symbol rate */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300893 switch (c->delivery_system) {
894 case SYS_DVBS:
895 case SYS_DVBS2:
896 case SYS_TURBO:
897 case SYS_DVBC_ANNEX_A:
898 case SYS_DVBC_ANNEX_C:
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300899 if ((fe->ops.info.symbol_rate_min &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300900 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300901 (fe->ops.info.symbol_rate_max &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300902 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300903 dev_warn(fe->dvb->device, "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
904 fe->dvb->num, fe->id, c->symbol_rate,
905 fe->ops.info.symbol_rate_min,
906 fe->ops.info.symbol_rate_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300907 return -EINVAL;
908 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300909 default:
910 break;
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300911 }
912
913 return 0;
914}
915
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300916static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
917{
Andreas Oberritter50727712011-05-08 20:03:39 -0300918 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300919 int i;
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200920 u32 delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300921
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200922 delsys = c->delivery_system;
Mauro Carvalho Chehab808d24d2013-03-20 10:39:31 -0300923 memset(c, 0, offsetof(struct dtv_frontend_properties, strength));
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200924 c->delivery_system = delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300925
Andreas Oberritter50727712011-05-08 20:03:39 -0300926 c->state = DTV_CLEAR;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300927
Antti Palosaari36bdbc32012-08-15 20:28:37 -0300928 dev_dbg(fe->dvb->device, "%s: Clearing cache for delivery system %d\n",
929 __func__, c->delivery_system);
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300930
Andreas Oberritter50727712011-05-08 20:03:39 -0300931 c->transmission_mode = TRANSMISSION_MODE_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300932 c->bandwidth_hz = 0; /* AUTO */
Andreas Oberritter50727712011-05-08 20:03:39 -0300933 c->guard_interval = GUARD_INTERVAL_AUTO;
934 c->hierarchy = HIERARCHY_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300935 c->symbol_rate = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -0300936 c->code_rate_HP = FEC_AUTO;
937 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300938 c->fec_inner = FEC_AUTO;
Mauro Carvalho Chehab39ce61a2011-11-11 12:46:23 -0200939 c->rolloff = ROLLOFF_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300940 c->voltage = SEC_VOLTAGE_OFF;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300941 c->sectone = SEC_TONE_OFF;
942 c->pilot = PILOT_AUTO;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300943
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300944 c->isdbt_partial_reception = 0;
945 c->isdbt_sb_mode = 0;
946 c->isdbt_sb_subchannel = 0;
947 c->isdbt_sb_segment_idx = 0;
948 c->isdbt_sb_segment_count = 0;
949 c->isdbt_layer_enabled = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300950 for (i = 0; i < 3; i++) {
Andreas Oberritter50727712011-05-08 20:03:39 -0300951 c->layer[i].fec = FEC_AUTO;
952 c->layer[i].modulation = QAM_AUTO;
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300953 c->layer[i].interleaving = 0;
954 c->layer[i].segment_count = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300955 }
956
Evgeny Plehov287cefd2012-09-13 10:13:30 -0300957 c->stream_id = NO_STREAM_ID_FILTER;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300958
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300959 switch (c->delivery_system) {
960 case SYS_DVBS:
961 case SYS_DVBS2:
962 case SYS_TURBO:
963 c->modulation = QPSK; /* implied for DVB-S in legacy API */
964 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
965 break;
966 case SYS_ATSC:
967 c->modulation = VSB_8;
968 break;
969 default:
970 c->modulation = QAM_AUTO;
971 break;
972 }
973
Antti Palosaari33eebec2012-10-03 04:28:56 -0300974 c->lna = LNA_AUTO;
975
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300976 return 0;
977}
978
979#define _DTV_CMD(n, s, b) \
980[n] = { \
981 .name = #n, \
982 .cmd = n, \
983 .set = s,\
984 .buffer = b \
985}
986
Andreas Oberritterec05a642011-05-26 07:54:14 -0300987static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300988 _DTV_CMD(DTV_TUNE, 1, 0),
989 _DTV_CMD(DTV_CLEAR, 1, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -0300990
991 /* Set */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300992 _DTV_CMD(DTV_FREQUENCY, 1, 0),
993 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
994 _DTV_CMD(DTV_MODULATION, 1, 0),
995 _DTV_CMD(DTV_INVERSION, 1, 0),
996 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
997 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
998 _DTV_CMD(DTV_INNER_FEC, 1, 0),
999 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1000 _DTV_CMD(DTV_TONE, 1, 0),
1001 _DTV_CMD(DTV_PILOT, 1, 0),
1002 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1003 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1004 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1005 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1006 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1007 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1008 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
Antti Palosaari224b6642012-08-12 22:33:21 -03001009 _DTV_CMD(DTV_INTERLEAVING, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001010
1011 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1012 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1013 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1014 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1015 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
Patrick Boettchere7b79492009-08-14 05:24:19 -03001016 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001017 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1018 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1019 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1020 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1021 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1022 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1023 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1024 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1025 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1026 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1027 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1028 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1029
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001030 _DTV_CMD(DTV_STREAM_ID, 1, 0),
1031 _DTV_CMD(DTV_DVBT2_PLP_ID_LEGACY, 1, 0),
Antti Palosaari8a2697a2012-07-11 21:54:50 -03001032 _DTV_CMD(DTV_LNA, 1, 0),
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001033
Steven Toth6b73eea2008-09-04 01:12:25 -03001034 /* Get */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001035 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1036 _DTV_CMD(DTV_API_VERSION, 0, 0),
Manu Abrahamba2780c2011-11-13 18:47:44 -03001037
1038 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001039
1040 _DTV_CMD(DTV_ATSCMH_PARADE_ID, 1, 0),
1041 _DTV_CMD(DTV_ATSCMH_RS_FRAME_ENSEMBLE, 1, 0),
1042
1043 _DTV_CMD(DTV_ATSCMH_FIC_VER, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001044 _DTV_CMD(DTV_ATSCMH_NOG, 0, 0),
1045 _DTV_CMD(DTV_ATSCMH_TNOG, 0, 0),
1046 _DTV_CMD(DTV_ATSCMH_SGN, 0, 0),
1047 _DTV_CMD(DTV_ATSCMH_PRC, 0, 0),
1048 _DTV_CMD(DTV_ATSCMH_RS_FRAME_MODE, 0, 0),
Michael Krufkyca689482012-01-29 15:44:58 -03001049 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_PRI, 0, 0),
1050 _DTV_CMD(DTV_ATSCMH_RS_CODE_MODE_SEC, 0, 0),
1051 _DTV_CMD(DTV_ATSCMH_SCCC_BLOCK_MODE, 0, 0),
1052 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_A, 0, 0),
1053 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_B, 0, 0),
1054 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_C, 0, 0),
1055 _DTV_CMD(DTV_ATSCMH_SCCC_CODE_MODE_D, 0, 0),
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001056
1057 /* Statistics API */
1058 _DTV_CMD(DTV_STAT_SIGNAL_STRENGTH, 0, 0),
1059 _DTV_CMD(DTV_STAT_CNR, 0, 0),
1060 _DTV_CMD(DTV_STAT_PRE_ERROR_BIT_COUNT, 0, 0),
1061 _DTV_CMD(DTV_STAT_PRE_TOTAL_BIT_COUNT, 0, 0),
1062 _DTV_CMD(DTV_STAT_POST_ERROR_BIT_COUNT, 0, 0),
1063 _DTV_CMD(DTV_STAT_POST_TOTAL_BIT_COUNT, 0, 0),
1064 _DTV_CMD(DTV_STAT_ERROR_BLOCK_COUNT, 0, 0),
1065 _DTV_CMD(DTV_STAT_TOTAL_BLOCK_COUNT, 0, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -03001066};
1067
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001068static void dtv_property_dump(struct dvb_frontend *fe, struct dtv_property *tvp)
Steven Toth6b73eea2008-09-04 01:12:25 -03001069{
1070 int i;
1071
Mauro Carvalho Chehab106ff9e2011-07-15 08:49:06 -03001072 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001073 dev_warn(fe->dvb->device, "%s: tvp.cmd = 0x%08x undefined\n",
1074 __func__, tvp->cmd);
Darron Broad4aae8ef2008-10-03 11:50:00 -03001075 return;
1076 }
1077
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001078 dev_dbg(fe->dvb->device, "%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1079 tvp->cmd, dtv_cmds[tvp->cmd].name);
Steven Toth6b73eea2008-09-04 01:12:25 -03001080
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001081 if (dtv_cmds[tvp->cmd].buffer) {
1082 dev_dbg(fe->dvb->device, "%s: tvp.u.buffer.len = 0x%02x\n",
1083 __func__, tvp->u.buffer.len);
Steven Toth6b73eea2008-09-04 01:12:25 -03001084
1085 for(i = 0; i < tvp->u.buffer.len; i++)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001086 dev_dbg(fe->dvb->device,
1087 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1088 __func__, i, tvp->u.buffer.data[i]);
1089 } else {
1090 dev_dbg(fe->dvb->device, "%s: tvp.u.data = 0x%08x\n", __func__,
1091 tvp->u.data);
1092 }
Steven Toth6b73eea2008-09-04 01:12:25 -03001093}
1094
Steven Tothee33c522008-09-11 23:52:32 -03001095/* Synchronise the legacy tuning parameters into the cache, so that demodulator
1096 * drivers can use a single set_frontend tuning function, regardless of whether
1097 * it's being used for the legacy or new API, reducing code and complexity.
1098 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001099static int dtv_property_cache_sync(struct dvb_frontend *fe,
1100 struct dtv_frontend_properties *c,
1101 const struct dvb_frontend_parameters *p)
Steven Tothee33c522008-09-11 23:52:32 -03001102{
Steven Tothee33c522008-09-11 23:52:32 -03001103 c->frequency = p->frequency;
1104 c->inversion = p->inversion;
1105
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001106 switch (dvbv3_type(c->delivery_system)) {
1107 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001108 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001109 c->symbol_rate = p->u.qpsk.symbol_rate;
1110 c->fec_inner = p->u.qpsk.fec_inner;
1111 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001112 case DVBV3_QAM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001113 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001114 c->symbol_rate = p->u.qam.symbol_rate;
1115 c->fec_inner = p->u.qam.fec_inner;
1116 c->modulation = p->u.qam.modulation;
1117 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001118 case DVBV3_OFDM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001119 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
1120
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001121 switch (p->u.ofdm.bandwidth) {
1122 case BANDWIDTH_10_MHZ:
1123 c->bandwidth_hz = 10000000;
1124 break;
1125 case BANDWIDTH_8_MHZ:
Steven Toth75b7f942008-09-13 16:56:34 -03001126 c->bandwidth_hz = 8000000;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001127 break;
1128 case BANDWIDTH_7_MHZ:
1129 c->bandwidth_hz = 7000000;
1130 break;
1131 case BANDWIDTH_6_MHZ:
1132 c->bandwidth_hz = 6000000;
1133 break;
1134 case BANDWIDTH_5_MHZ:
1135 c->bandwidth_hz = 5000000;
1136 break;
1137 case BANDWIDTH_1_712_MHZ:
1138 c->bandwidth_hz = 1712000;
1139 break;
1140 case BANDWIDTH_AUTO:
Steven Toth75b7f942008-09-13 16:56:34 -03001141 c->bandwidth_hz = 0;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001142 }
1143
Steven Tothee33c522008-09-11 23:52:32 -03001144 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1145 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1146 c->modulation = p->u.ofdm.constellation;
1147 c->transmission_mode = p->u.ofdm.transmission_mode;
1148 c->guard_interval = p->u.ofdm.guard_interval;
1149 c->hierarchy = p->u.ofdm.hierarchy_information;
1150 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001151 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001152 dev_dbg(fe->dvb->device, "%s: Preparing ATSC req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001153 c->modulation = p->u.vsb.modulation;
Michael Krufkyca689482012-01-29 15:44:58 -03001154 if (c->delivery_system == SYS_ATSCMH)
1155 break;
Steven Toth80a773c2008-09-12 00:53:50 -03001156 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1157 c->delivery_system = SYS_ATSC;
1158 else
1159 c->delivery_system = SYS_DVBC_ANNEX_B;
Steven Tothee33c522008-09-11 23:52:32 -03001160 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001161 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001162 dev_err(fe->dvb->device,
1163 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1164 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001165 return -EINVAL;
Steven Tothee33c522008-09-11 23:52:32 -03001166 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001167
1168 return 0;
Steven Tothee33c522008-09-11 23:52:32 -03001169}
1170
Steven Tothd5748f12008-09-12 00:27:13 -03001171/* Ensure the cached values are set correctly in the frontend
1172 * legacy tuning structures, for the advanced tuning API.
1173 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001174static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001175 struct dvb_frontend_parameters *p)
Steven Toth6b73eea2008-09-04 01:12:25 -03001176{
Andreas Oberritter15cc2bb2011-05-08 20:03:35 -03001177 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001178
Steven Tothd5748f12008-09-12 00:27:13 -03001179 p->frequency = c->frequency;
1180 p->inversion = c->inversion;
1181
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001182 switch (dvbv3_type(c->delivery_system)) {
1183 case DVBV3_UNKNOWN:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001184 dev_err(fe->dvb->device,
1185 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1186 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001187 return -EINVAL;
1188 case DVBV3_QPSK:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001189 dev_dbg(fe->dvb->device, "%s: Preparing QPSK req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001190 p->u.qpsk.symbol_rate = c->symbol_rate;
1191 p->u.qpsk.fec_inner = c->fec_inner;
1192 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001193 case DVBV3_QAM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001194 dev_dbg(fe->dvb->device, "%s: Preparing QAM req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001195 p->u.qam.symbol_rate = c->symbol_rate;
1196 p->u.qam.fec_inner = c->fec_inner;
1197 p->u.qam.modulation = c->modulation;
1198 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001199 case DVBV3_OFDM:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001200 dev_dbg(fe->dvb->device, "%s: Preparing OFDM req\n", __func__);
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001201 switch (c->bandwidth_hz) {
1202 case 10000000:
1203 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1204 break;
1205 case 8000000:
Steven Toth75b7f942008-09-13 16:56:34 -03001206 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001207 break;
1208 case 7000000:
1209 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1210 break;
1211 case 6000000:
1212 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1213 break;
1214 case 5000000:
1215 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1216 break;
1217 case 1712000:
1218 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1219 break;
1220 case 0:
1221 default:
Steven Toth75b7f942008-09-13 16:56:34 -03001222 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001223 }
Steven Tothd5748f12008-09-12 00:27:13 -03001224 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1225 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1226 p->u.ofdm.constellation = c->modulation;
1227 p->u.ofdm.transmission_mode = c->transmission_mode;
1228 p->u.ofdm.guard_interval = c->guard_interval;
1229 p->u.ofdm.hierarchy_information = c->hierarchy;
1230 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001231 case DVBV3_ATSC:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001232 dev_dbg(fe->dvb->device, "%s: Preparing VSB req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001233 p->u.vsb.modulation = c->modulation;
1234 break;
1235 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001236 return 0;
Steven Toth6b73eea2008-09-04 01:12:25 -03001237}
1238
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001239/**
1240 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1241 * @fe: struct dvb_frontend pointer
1242 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1243 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1244 *
1245 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1246 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1247 * If p_out is not null, it will update the DVBv3 params pointed by it.
1248 */
1249static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001250 struct dvb_frontend_parameters *p_out)
1251{
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001252 int r;
1253
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001254 if (fe->ops.get_frontend) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001255 r = fe->ops.get_frontend(fe);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001256 if (unlikely(r < 0))
1257 return r;
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001258 if (p_out)
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001259 dtv_property_legacy_params_sync(fe, p_out);
1260 return 0;
1261 }
1262
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001263 /* As everything is in cache, get_frontend fops are always supported */
Mauro Carvalho Chehabb1e9a65012011-12-26 16:43:32 -03001264 return 0;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001265}
1266
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001267static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001268 unsigned int cmd, void *parg);
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001269static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001270 unsigned int cmd, void *parg);
1271
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001272static int dtv_property_process_get(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001273 const struct dtv_frontend_properties *c,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001274 struct dtv_property *tvp,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001275 struct file *file)
Steven Toth363429a2008-09-12 01:34:28 -03001276{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001277 int r, ncaps;
Steven Tothbfbf2da2008-09-12 01:37:37 -03001278
Steven Toth363429a2008-09-12 01:34:28 -03001279 switch(tvp->cmd) {
Manu Abrahamba2780c2011-11-13 18:47:44 -03001280 case DTV_ENUM_DELSYS:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001281 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001282 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001283 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1284 ncaps++;
1285 }
1286 tvp->u.buffer.len = ncaps;
Manu Abrahamba2780c2011-11-13 18:47:44 -03001287 break;
Steven Toth363429a2008-09-12 01:34:28 -03001288 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001289 tvp->u.data = c->frequency;
Steven Toth363429a2008-09-12 01:34:28 -03001290 break;
1291 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001292 tvp->u.data = c->modulation;
Steven Toth363429a2008-09-12 01:34:28 -03001293 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001294 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001295 tvp->u.data = c->bandwidth_hz;
Steven Toth363429a2008-09-12 01:34:28 -03001296 break;
1297 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001298 tvp->u.data = c->inversion;
Steven Toth363429a2008-09-12 01:34:28 -03001299 break;
1300 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001301 tvp->u.data = c->symbol_rate;
Steven Toth363429a2008-09-12 01:34:28 -03001302 break;
1303 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001304 tvp->u.data = c->fec_inner;
Steven Toth363429a2008-09-12 01:34:28 -03001305 break;
1306 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001307 tvp->u.data = c->pilot;
Steven Toth363429a2008-09-12 01:34:28 -03001308 break;
1309 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001310 tvp->u.data = c->rolloff;
Steven Toth363429a2008-09-12 01:34:28 -03001311 break;
1312 case DTV_DELIVERY_SYSTEM:
Andreas Oberritter50727712011-05-08 20:03:39 -03001313 tvp->u.data = c->delivery_system;
Steven Toth363429a2008-09-12 01:34:28 -03001314 break;
Steven Toth363429a2008-09-12 01:34:28 -03001315 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001316 tvp->u.data = c->voltage;
Steven Toth363429a2008-09-12 01:34:28 -03001317 break;
1318 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001319 tvp->u.data = c->sectone;
Steven Toth363429a2008-09-12 01:34:28 -03001320 break;
Steven Totheacf8d82008-09-26 00:29:49 -03001321 case DTV_API_VERSION:
1322 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1323 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001324 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001325 tvp->u.data = c->code_rate_HP;
Steven Totha4de91b2008-10-06 21:55:46 -03001326 break;
1327 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001328 tvp->u.data = c->code_rate_LP;
Steven Totha4de91b2008-10-06 21:55:46 -03001329 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001330 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001331 tvp->u.data = c->guard_interval;
Steven Tothb87625f2008-10-06 21:56:59 -03001332 break;
1333 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001334 tvp->u.data = c->transmission_mode;
Steven Tothb87625f2008-10-06 21:56:59 -03001335 break;
Steven Tothef526f42008-10-06 22:01:47 -03001336 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001337 tvp->u.data = c->hierarchy;
Steven Tothef526f42008-10-06 22:01:47 -03001338 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001339 case DTV_INTERLEAVING:
1340 tvp->u.data = c->interleaving;
1341 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001342
1343 /* ISDB-T Support here */
1344 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001345 tvp->u.data = c->isdbt_partial_reception;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001346 break;
1347 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001348 tvp->u.data = c->isdbt_sb_mode;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001349 break;
1350 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001351 tvp->u.data = c->isdbt_sb_subchannel;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001352 break;
1353 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001354 tvp->u.data = c->isdbt_sb_segment_idx;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001355 break;
1356 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001357 tvp->u.data = c->isdbt_sb_segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001358 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001359 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001360 tvp->u.data = c->isdbt_layer_enabled;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001361 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001362 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001363 tvp->u.data = c->layer[0].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001364 break;
1365 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001366 tvp->u.data = c->layer[0].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001367 break;
1368 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001369 tvp->u.data = c->layer[0].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001370 break;
1371 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001372 tvp->u.data = c->layer[0].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001373 break;
1374 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001375 tvp->u.data = c->layer[1].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001376 break;
1377 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001378 tvp->u.data = c->layer[1].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001379 break;
1380 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001381 tvp->u.data = c->layer[1].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001382 break;
1383 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001384 tvp->u.data = c->layer[1].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001385 break;
1386 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001387 tvp->u.data = c->layer[2].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001388 break;
1389 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001390 tvp->u.data = c->layer[2].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001391 break;
1392 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001393 tvp->u.data = c->layer[2].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001394 break;
1395 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001396 tvp->u.data = c->layer[2].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001397 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001398
1399 /* Multistream support */
1400 case DTV_STREAM_ID:
1401 case DTV_DVBT2_PLP_ID_LEGACY:
1402 tvp->u.data = c->stream_id;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001403 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001404
1405 /* ATSC-MH */
1406 case DTV_ATSCMH_FIC_VER:
1407 tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
1408 break;
1409 case DTV_ATSCMH_PARADE_ID:
1410 tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
1411 break;
1412 case DTV_ATSCMH_NOG:
1413 tvp->u.data = fe->dtv_property_cache.atscmh_nog;
1414 break;
1415 case DTV_ATSCMH_TNOG:
1416 tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
1417 break;
1418 case DTV_ATSCMH_SGN:
1419 tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
1420 break;
1421 case DTV_ATSCMH_PRC:
1422 tvp->u.data = fe->dtv_property_cache.atscmh_prc;
1423 break;
1424 case DTV_ATSCMH_RS_FRAME_MODE:
1425 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
1426 break;
1427 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1428 tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
1429 break;
1430 case DTV_ATSCMH_RS_CODE_MODE_PRI:
1431 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
1432 break;
1433 case DTV_ATSCMH_RS_CODE_MODE_SEC:
1434 tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
1435 break;
1436 case DTV_ATSCMH_SCCC_BLOCK_MODE:
1437 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
1438 break;
1439 case DTV_ATSCMH_SCCC_CODE_MODE_A:
1440 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
1441 break;
1442 case DTV_ATSCMH_SCCC_CODE_MODE_B:
1443 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
1444 break;
1445 case DTV_ATSCMH_SCCC_CODE_MODE_C:
1446 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
1447 break;
1448 case DTV_ATSCMH_SCCC_CODE_MODE_D:
1449 tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
1450 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001451
Antti Palosaari33eebec2012-10-03 04:28:56 -03001452 case DTV_LNA:
1453 tvp->u.data = c->lna;
1454 break;
1455
Mauro Carvalho Chehab7cd4ece2013-01-07 15:41:35 -03001456 /* Fill quality measures */
1457 case DTV_STAT_SIGNAL_STRENGTH:
1458 tvp->u.st = c->strength;
1459 break;
1460 case DTV_STAT_CNR:
1461 tvp->u.st = c->cnr;
1462 break;
1463 case DTV_STAT_PRE_ERROR_BIT_COUNT:
1464 tvp->u.st = c->pre_bit_error;
1465 break;
1466 case DTV_STAT_PRE_TOTAL_BIT_COUNT:
1467 tvp->u.st = c->pre_bit_count;
1468 break;
1469 case DTV_STAT_POST_ERROR_BIT_COUNT:
1470 tvp->u.st = c->post_bit_error;
1471 break;
1472 case DTV_STAT_POST_TOTAL_BIT_COUNT:
1473 tvp->u.st = c->post_bit_count;
1474 break;
1475 case DTV_STAT_ERROR_BLOCK_COUNT:
1476 tvp->u.st = c->block_error;
1477 break;
1478 case DTV_STAT_TOTAL_BLOCK_COUNT:
1479 tvp->u.st = c->block_count;
1480 break;
Steven Toth363429a2008-09-12 01:34:28 -03001481 default:
Mauro Carvalho Chehab94a93e52013-01-23 17:06:02 -03001482 dev_dbg(fe->dvb->device,
1483 "%s: FE property %d doesn't exist\n",
1484 __func__, tvp->cmd);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001485 return -EINVAL;
Steven Toth363429a2008-09-12 01:34:28 -03001486 }
1487
Andreas Oberrittere23d9ae2011-05-08 20:03:36 -03001488 /* Allow the frontend to override outgoing properties */
1489 if (fe->ops.get_property) {
1490 r = fe->ops.get_property(fe, tvp);
1491 if (r < 0)
1492 return r;
1493 }
1494
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001495 dtv_property_dump(fe, tvp);
Mauro Carvalho Chehab639544d2009-12-31 17:49:32 -03001496
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001497 return 0;
Steven Toth363429a2008-09-12 01:34:28 -03001498}
1499
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001500static int dtv_set_frontend(struct dvb_frontend *fe);
1501
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001502static bool is_dvbv3_delsys(u32 delsys)
1503{
1504 bool status;
1505
1506 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1507 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1508
1509 return status;
1510}
1511
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001512/**
1513 * emulate_delivery_system - emulate a DVBv5 delivery system with a DVBv3 type
1514 * @fe: struct frontend;
1515 * @delsys: DVBv5 type that will be used for emulation
1516 *
1517 * Provides emulation for delivery systems that are compatible with the old
1518 * DVBv3 call. Among its usages, it provices support for ISDB-T, and allows
1519 * using a DVB-S2 only frontend just like it were a DVB-S, if the frontent
1520 * parameters are compatible with DVB-S spec.
1521 */
1522static int emulate_delivery_system(struct dvb_frontend *fe, u32 delsys)
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001523{
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001524 int i;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001525 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001526
Mauro Carvalho Chehab36264792012-04-17 18:32:19 -03001527 c->delivery_system = delsys;
1528
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001529 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001530 * If the call is for ISDB-T, put it into full-seg, auto mode, TV
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001531 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001532 if (c->delivery_system == SYS_ISDBT) {
1533 dev_dbg(fe->dvb->device,
1534 "%s: Using defaults for SYS_ISDBT\n",
1535 __func__);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001536
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001537 if (!c->bandwidth_hz)
1538 c->bandwidth_hz = 6000000;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001539
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001540 c->isdbt_partial_reception = 0;
1541 c->isdbt_sb_mode = 0;
1542 c->isdbt_sb_subchannel = 0;
1543 c->isdbt_sb_segment_idx = 0;
1544 c->isdbt_sb_segment_count = 0;
1545 c->isdbt_layer_enabled = 7;
1546 for (i = 0; i < 3; i++) {
1547 c->layer[i].fec = FEC_AUTO;
1548 c->layer[i].modulation = QAM_AUTO;
1549 c->layer[i].interleaving = 0;
1550 c->layer[i].segment_count = 0;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001551 }
1552 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001553 dev_dbg(fe->dvb->device, "%s: change delivery system on cache to %d\n",
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001554 __func__, c->delivery_system);
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001555
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001556 return 0;
1557}
1558
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001559/**
1560 * dvbv5_set_delivery_system - Sets the delivery system for a DVBv5 API call
1561 * @fe: frontend struct
1562 * @desired_system: delivery system requested by the user
1563 *
1564 * A DVBv5 call know what's the desired system it wants. So, set it.
1565 *
1566 * There are, however, a few known issues with early DVBv5 applications that
1567 * are also handled by this logic:
1568 *
1569 * 1) Some early apps use SYS_UNDEFINED as the desired delivery system.
1570 * This is an API violation, but, as we don't want to break userspace,
1571 * convert it to the first supported delivery system.
1572 * 2) Some apps might be using a DVBv5 call in a wrong way, passing, for
1573 * example, SYS_DVBT instead of SYS_ISDBT. This is because early usage of
1574 * ISDB-T provided backward compat with DVB-T.
1575 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001576static int dvbv5_set_delivery_system(struct dvb_frontend *fe,
1577 u32 desired_system)
1578{
1579 int ncaps;
1580 u32 delsys = SYS_UNDEFINED;
1581 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1582 enum dvbv3_emulation_type type;
1583
1584 /*
1585 * It was reported that some old DVBv5 applications were
1586 * filling delivery_system with SYS_UNDEFINED. If this happens,
1587 * assume that the application wants to use the first supported
1588 * delivery system.
1589 */
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001590 if (desired_system == SYS_UNDEFINED)
1591 desired_system = fe->ops.delsys[0];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001592
1593 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001594 * This is a DVBv5 call. So, it likely knows the supported
1595 * delivery systems. So, check if the desired delivery system is
1596 * supported
1597 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001598 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001599 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001600 if (fe->ops.delsys[ncaps] == desired_system) {
1601 c->delivery_system = desired_system;
1602 dev_dbg(fe->dvb->device,
1603 "%s: Changing delivery system to %d\n",
1604 __func__, desired_system);
1605 return 0;
1606 }
1607 ncaps++;
1608 }
1609
1610 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001611 * The requested delivery system isn't supported. Maybe userspace
1612 * is requesting a DVBv3 compatible delivery system.
1613 *
1614 * The emulation only works if the desired system is one of the
1615 * delivery systems supported by DVBv3 API
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001616 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001617 if (!is_dvbv3_delsys(desired_system)) {
1618 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001619 "%s: Delivery system %d not supported.\n",
1620 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001621 return -EINVAL;
1622 }
1623
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001624 type = dvbv3_type(desired_system);
1625
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001626 /*
1627 * Get the last non-DVBv3 delivery system that has the same type
1628 * of the desired system
1629 */
1630 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001631 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001632 if (dvbv3_type(fe->ops.delsys[ncaps]) == type)
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001633 delsys = fe->ops.delsys[ncaps];
1634 ncaps++;
1635 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001636
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001637 /* There's nothing compatible with the desired delivery system */
1638 if (delsys == SYS_UNDEFINED) {
1639 dev_dbg(fe->dvb->device,
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001640 "%s: Delivery system %d not supported on emulation mode.\n",
1641 __func__, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001642 return -EINVAL;
1643 }
1644
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001645 dev_dbg(fe->dvb->device,
1646 "%s: Using delivery system %d emulated as if it were %d\n",
1647 __func__, delsys, desired_system);
1648
1649 return emulate_delivery_system(fe, desired_system);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001650}
1651
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001652/**
1653 * dvbv3_set_delivery_system - Sets the delivery system for a DVBv3 API call
1654 * @fe: frontend struct
1655 *
1656 * A DVBv3 call doesn't know what's the desired system it wants. It also
1657 * doesn't allow to switch between different types. Due to that, userspace
1658 * should use DVBv5 instead.
1659 * However, in order to avoid breaking userspace API, limited backward
1660 * compatibility support is provided.
1661 *
1662 * There are some delivery systems that are incompatible with DVBv3 calls.
1663 *
1664 * This routine should work fine for frontends that support just one delivery
1665 * system.
1666 *
1667 * For frontends that support multiple frontends:
1668 * 1) It defaults to use the first supported delivery system. There's an
1669 * userspace application that allows changing it at runtime;
1670 *
1671 * 2) If the current delivery system is not compatible with DVBv3, it gets
1672 * the first one that it is compatible.
1673 *
1674 * NOTE: in order for this to work with applications like Kaffeine that
1675 * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
1676 * DVB-S, drivers that support both DVB-S and DVB-S2 should have the
1677 * SYS_DVBS entry before the SYS_DVBS2, otherwise it won't switch back
1678 * to DVB-S.
1679 */
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001680static int dvbv3_set_delivery_system(struct dvb_frontend *fe)
1681{
1682 int ncaps;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001683 u32 delsys = SYS_UNDEFINED;
1684 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001685
1686 /* If not set yet, defaults to the first supported delivery system */
1687 if (c->delivery_system == SYS_UNDEFINED)
1688 c->delivery_system = fe->ops.delsys[0];
1689
1690 /*
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001691 * Trivial case: just use the current one, if it already a DVBv3
1692 * delivery system
1693 */
1694 if (is_dvbv3_delsys(c->delivery_system)) {
1695 dev_dbg(fe->dvb->device,
1696 "%s: Using delivery system to %d\n",
1697 __func__, c->delivery_system);
1698 return 0;
1699 }
1700
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001701 /*
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001702 * Seek for the first delivery system that it is compatible with a
1703 * DVBv3 standard
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001704 */
1705 ncaps = 0;
Ole Ernst0123f292014-03-05 14:08:15 -03001706 while (ncaps < MAX_DELSYS && fe->ops.delsys[ncaps]) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001707 if (dvbv3_type(fe->ops.delsys[ncaps]) != DVBV3_UNKNOWN) {
1708 delsys = fe->ops.delsys[ncaps];
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001709 break;
1710 }
1711 ncaps++;
1712 }
1713 if (delsys == SYS_UNDEFINED) {
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001714 dev_dbg(fe->dvb->device,
1715 "%s: Couldn't find a delivery system that works with FE_SET_FRONTEND\n",
1716 __func__);
1717 return -EINVAL;
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001718 }
Mauro Carvalho Chehab52dee392013-03-18 15:25:37 -03001719 return emulate_delivery_system(fe, delsys);
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001720}
1721
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001722static int dtv_property_process_set(struct dvb_frontend *fe,
1723 struct dtv_property *tvp,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001724 struct file *file)
Steven Toth6b73eea2008-09-04 01:12:25 -03001725{
1726 int r = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -03001727 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001728
Steven Tothbfbf2da2008-09-12 01:37:37 -03001729 /* Allow the frontend to validate incoming properties */
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001730 if (fe->ops.set_property) {
Steven Tothbfbf2da2008-09-12 01:37:37 -03001731 r = fe->ops.set_property(fe, tvp);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001732 if (r < 0)
1733 return r;
1734 }
Steven Tothbfbf2da2008-09-12 01:37:37 -03001735
Steven Toth6b73eea2008-09-04 01:12:25 -03001736 switch(tvp->cmd) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001737 case DTV_CLEAR:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001738 /*
1739 * Reset a cache of data specific to the frontend here. This does
Steven Toth6b73eea2008-09-04 01:12:25 -03001740 * not effect hardware.
1741 */
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001742 dvb_frontend_clear_cache(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001743 break;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001744 case DTV_TUNE:
Steven Toth6b73eea2008-09-04 01:12:25 -03001745 /* interpret the cache of data, build either a traditional frontend
Steven Tothd5748f12008-09-12 00:27:13 -03001746 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1747 * ioctl.
Steven Toth6b73eea2008-09-04 01:12:25 -03001748 */
Andreas Oberritter50727712011-05-08 20:03:39 -03001749 c->state = tvp->cmd;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001750 dev_dbg(fe->dvb->device, "%s: Finalised property cache\n",
1751 __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001752
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001753 r = dtv_set_frontend(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001754 break;
Steven Toth363429a2008-09-12 01:34:28 -03001755 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001756 c->frequency = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001757 break;
Steven Toth363429a2008-09-12 01:34:28 -03001758 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001759 c->modulation = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001760 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001761 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001762 c->bandwidth_hz = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001763 break;
Steven Toth363429a2008-09-12 01:34:28 -03001764 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001765 c->inversion = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001766 break;
Steven Toth363429a2008-09-12 01:34:28 -03001767 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001768 c->symbol_rate = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001769 break;
Steven Toth363429a2008-09-12 01:34:28 -03001770 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001771 c->fec_inner = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001772 break;
Steven Toth363429a2008-09-12 01:34:28 -03001773 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001774 c->pilot = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001775 break;
Steven Toth363429a2008-09-12 01:34:28 -03001776 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001777 c->rolloff = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001778 break;
Steven Toth363429a2008-09-12 01:34:28 -03001779 case DTV_DELIVERY_SYSTEM:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03001780 r = dvbv5_set_delivery_system(fe, tvp->u.data);
Steven Toth6b73eea2008-09-04 01:12:25 -03001781 break;
Steven Toth363429a2008-09-12 01:34:28 -03001782 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001783 c->voltage = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001784 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001785 (void *)c->voltage);
Steven Toth13c97bf2008-09-04 21:19:43 -03001786 break;
Steven Toth363429a2008-09-12 01:34:28 -03001787 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001788 c->sectone = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001789 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001790 (void *)c->sectone);
Steven Toth13c97bf2008-09-04 21:19:43 -03001791 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001792 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001793 c->code_rate_HP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001794 break;
1795 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001796 c->code_rate_LP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001797 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001798 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001799 c->guard_interval = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001800 break;
1801 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001802 c->transmission_mode = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001803 break;
Steven Tothef526f42008-10-06 22:01:47 -03001804 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001805 c->hierarchy = tvp->u.data;
Steven Tothef526f42008-10-06 22:01:47 -03001806 break;
Antti Palosaari224b6642012-08-12 22:33:21 -03001807 case DTV_INTERLEAVING:
1808 c->interleaving = tvp->u.data;
1809 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001810
1811 /* ISDB-T Support here */
1812 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001813 c->isdbt_partial_reception = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001814 break;
1815 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001816 c->isdbt_sb_mode = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001817 break;
1818 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001819 c->isdbt_sb_subchannel = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001820 break;
1821 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001822 c->isdbt_sb_segment_idx = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001823 break;
1824 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001825 c->isdbt_sb_segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001826 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001827 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001828 c->isdbt_layer_enabled = tvp->u.data;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001829 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001830 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001831 c->layer[0].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001832 break;
1833 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001834 c->layer[0].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001835 break;
1836 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001837 c->layer[0].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001838 break;
1839 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001840 c->layer[0].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001841 break;
1842 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001843 c->layer[1].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001844 break;
1845 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001846 c->layer[1].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001847 break;
1848 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001849 c->layer[1].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001850 break;
1851 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001852 c->layer[1].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001853 break;
1854 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001855 c->layer[2].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001856 break;
1857 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001858 c->layer[2].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001859 break;
1860 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001861 c->layer[2].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001862 break;
1863 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001864 c->layer[2].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001865 break;
Evgeny Plehov287cefd2012-09-13 10:13:30 -03001866
1867 /* Multistream support */
1868 case DTV_STREAM_ID:
1869 case DTV_DVBT2_PLP_ID_LEGACY:
1870 c->stream_id = tvp->u.data;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001871 break;
Michael Krufkyca689482012-01-29 15:44:58 -03001872
1873 /* ATSC-MH */
1874 case DTV_ATSCMH_PARADE_ID:
1875 fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
1876 break;
1877 case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
1878 fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
1879 break;
1880
Antti Palosaari33eebec2012-10-03 04:28:56 -03001881 case DTV_LNA:
1882 c->lna = tvp->u.data;
1883 if (fe->ops.set_lna)
1884 r = fe->ops.set_lna(fe);
Mauro Carvalho Chehabe6876692014-02-16 06:36:17 -03001885 if (r < 0)
1886 c->lna = LNA_AUTO;
Antti Palosaari33eebec2012-10-03 04:28:56 -03001887 break;
1888
Steven Toth363429a2008-09-12 01:34:28 -03001889 default:
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001890 return -EINVAL;
Steven Toth6b73eea2008-09-04 01:12:25 -03001891 }
1892
Steven Toth13c97bf2008-09-04 21:19:43 -03001893 return r;
Steven Toth6b73eea2008-09-04 01:12:25 -03001894}
1895
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001896static int dvb_frontend_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 unsigned int cmd, void *parg)
1898{
1899 struct dvb_device *dvbdev = file->private_data;
1900 struct dvb_frontend *fe = dvbdev->priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001901 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001902 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02001903 int err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001905 dev_dbg(fe->dvb->device, "%s: (%d)\n", __func__, _IOC_NR(cmd));
Juergen Lock6ae23222012-12-23 17:23:06 -03001906 if (down_interruptible(&fepriv->sem))
1907 return -ERESTARTSYS;
1908
1909 if (fepriv->exit != DVB_FE_NO_EXIT) {
1910 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return -ENODEV;
Juergen Lock6ae23222012-12-23 17:23:06 -03001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1915 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
Juergen Lock6ae23222012-12-23 17:23:06 -03001916 cmd == FE_DISEQC_RECV_SLAVE_REPLY)) {
1917 up(&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return -EPERM;
Juergen Lock6ae23222012-12-23 17:23:06 -03001919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Steven Toth13c97bf2008-09-04 21:19:43 -03001921 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001922 err = dvb_frontend_ioctl_properties(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001923 else {
Andreas Oberritter50727712011-05-08 20:03:39 -03001924 c->state = DTV_UNDEFINED;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001925 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001926 }
Steven Toth13c97bf2008-09-04 21:19:43 -03001927
1928 up(&fepriv->sem);
1929 return err;
1930}
1931
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001932static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001933 unsigned int cmd, void *parg)
1934{
1935 struct dvb_device *dvbdev = file->private_data;
1936 struct dvb_frontend *fe = dvbdev->priv;
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02001937 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001938 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Igor M. Liplianine6f9ec82008-09-12 14:49:06 -03001939 int err = 0;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001940
1941 struct dtv_properties *tvps = NULL;
1942 struct dtv_property *tvp = NULL;
1943 int i;
Steven Toth13c97bf2008-09-04 21:19:43 -03001944
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001945 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Steven Toth13c97bf2008-09-04 21:19:43 -03001946
Steven Toth6b73eea2008-09-04 01:12:25 -03001947 if(cmd == FE_SET_PROPERTY) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001948 tvps = (struct dtv_properties __user *)parg;
Steven Toth6b73eea2008-09-04 01:12:25 -03001949
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001950 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1951 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001952
1953 /* Put an arbitrary limit on the number of messages that can
1954 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001955 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Tothe7fee0f32008-09-11 10:23:01 -03001956 return -EINVAL;
1957
Thiago Farina6e3924a2010-01-16 10:25:38 -03001958 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001959 if (!tvp) {
1960 err = -ENOMEM;
1961 goto out;
Steven Toth6b73eea2008-09-04 01:12:25 -03001962 }
1963
Steven Tothe7fee0f32008-09-11 10:23:01 -03001964 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1965 err = -EFAULT;
1966 goto out;
1967 }
1968
Steven Tothd48cb402008-09-26 00:16:25 -03001969 for (i = 0; i < tvps->num; i++) {
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001970 err = dtv_property_process_set(fe, tvp + i, file);
1971 if (err < 0)
1972 goto out;
1973 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03001974 }
Steven Tothe7fee0f32008-09-11 10:23:01 -03001975
Andreas Oberritter50727712011-05-08 20:03:39 -03001976 if (c->state == DTV_TUNE)
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001977 dev_dbg(fe->dvb->device, "%s: Property cache is full, tuning\n", __func__);
Steven Tothbfbf2da2008-09-12 01:37:37 -03001978
Steven Toth363429a2008-09-12 01:34:28 -03001979 } else
1980 if(cmd == FE_GET_PROPERTY) {
Steven Toth363429a2008-09-12 01:34:28 -03001981 tvps = (struct dtv_properties __user *)parg;
1982
Antti Palosaari36bdbc32012-08-15 20:28:37 -03001983 dev_dbg(fe->dvb->device, "%s: properties.num = %d\n", __func__, tvps->num);
1984 dev_dbg(fe->dvb->device, "%s: properties.props = %p\n", __func__, tvps->props);
Steven Toth363429a2008-09-12 01:34:28 -03001985
1986 /* Put an arbitrary limit on the number of messages that can
1987 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001988 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Toth363429a2008-09-12 01:34:28 -03001989 return -EINVAL;
1990
Thiago Farina6e3924a2010-01-16 10:25:38 -03001991 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Toth363429a2008-09-12 01:34:28 -03001992 if (!tvp) {
1993 err = -ENOMEM;
1994 goto out;
1995 }
1996
1997 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1998 err = -EFAULT;
1999 goto out;
2000 }
2001
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03002002 /*
2003 * Fills the cache out struct with the cache contents, plus
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002004 * the data retrieved from get_frontend, if the frontend
2005 * is not idle. Otherwise, returns the cached content
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03002006 */
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02002007 if (fepriv->state != FESTATE_IDLE) {
2008 err = dtv_get_frontend(fe, NULL);
2009 if (err < 0)
2010 goto out;
2011 }
Steven Tothd48cb402008-09-26 00:16:25 -03002012 for (i = 0; i < tvps->num; i++) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03002013 err = dtv_property_process_get(fe, c, tvp + i, file);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01002014 if (err < 0)
2015 goto out;
2016 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03002017 }
Steven Toth363429a2008-09-12 01:34:28 -03002018
2019 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
2020 err = -EFAULT;
2021 goto out;
2022 }
2023
Steven Toth363429a2008-09-12 01:34:28 -03002024 } else
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002025 err = -EOPNOTSUPP;
Steven Toth363429a2008-09-12 01:34:28 -03002026
Steven Tothe7fee0f32008-09-11 10:23:01 -03002027out:
2028 kfree(tvp);
Steven Toth13c97bf2008-09-04 21:19:43 -03002029 return err;
2030}
2031
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002032static int dtv_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002033{
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002034 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2035 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
2036 struct dvb_frontend_tune_settings fetunesettings;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002037 u32 rolloff = 0;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002038
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002039 if (dvb_frontend_check_parameters(fe) < 0)
2040 return -EINVAL;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002041
2042 /*
Chris Rankin556a0442012-04-06 18:38:18 -03002043 * Initialize output parameters to match the values given by
2044 * the user. FE_SET_FRONTEND triggers an initial frontend event
2045 * with status = 0, which copies output parameters to userspace.
2046 */
2047 dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
2048
2049 /*
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002050 * Be sure that the bandwidth will be filled for all
2051 * non-satellite systems, as tuners need to know what
2052 * low pass/Nyquist half filter should be applied, in
2053 * order to avoid inter-channel noise.
2054 *
2055 * ISDB-T and DVB-T/T2 already sets bandwidth.
2056 * ATSC and DVB-C don't set, so, the core should fill it.
2057 *
2058 * On DVB-C Annex A and C, the bandwidth is a function of
2059 * the roll-off and symbol rate. Annex B defines different
2060 * roll-off factors depending on the modulation. Fortunately,
2061 * Annex B is only used with 6MHz, so there's no need to
2062 * calculate it.
2063 *
2064 * While not officially supported, a side effect of handling it at
2065 * the cache level is that a program could retrieve the bandwidth
2066 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
2067 */
2068 switch (c->delivery_system) {
2069 case SYS_ATSC:
2070 case SYS_DVBC_ANNEX_B:
2071 c->bandwidth_hz = 6000000;
2072 break;
2073 case SYS_DVBC_ANNEX_A:
2074 rolloff = 115;
2075 break;
2076 case SYS_DVBC_ANNEX_C:
2077 rolloff = 113;
2078 break;
2079 default:
2080 break;
2081 }
2082 if (rolloff)
2083 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002084
2085 /* force auto frequency inversion if requested */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002086 if (dvb_force_auto_inversion)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002087 c->inversion = INVERSION_AUTO;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002088
2089 /*
2090 * without hierarchical coding code_rate_LP is irrelevant,
2091 * so we tolerate the otherwise invalid FEC_NONE setting
2092 */
2093 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
2094 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002095
2096 /* get frontend-specific tuning settings */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002097 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002098 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2099 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
2100 fepriv->max_drift = fetunesettings.max_drift;
2101 fepriv->step_size = fetunesettings.step_size;
2102 } else {
2103 /* default values */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002104 switch (c->delivery_system) {
Mauro Carvalho Chehab910a5f22012-05-07 15:01:37 -03002105 case SYS_DVBS:
2106 case SYS_DVBS2:
2107 case SYS_ISDBS:
2108 case SYS_TURBO:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002109 case SYS_DVBC_ANNEX_A:
2110 case SYS_DVBC_ANNEX_C:
2111 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002112 fepriv->step_size = c->symbol_rate / 16000;
2113 fepriv->max_drift = c->symbol_rate / 2000;
2114 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002115 case SYS_DVBT:
2116 case SYS_DVBT2:
2117 case SYS_ISDBT:
Antti Palosaari224b6642012-08-12 22:33:21 -03002118 case SYS_DTMB:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002119 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002120 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
2121 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
2122 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002123 default:
2124 /*
2125 * FIXME: This sounds wrong! if freqency_stepsize is
2126 * defined by the frontend, why not use it???
2127 */
2128 fepriv->min_delay = HZ / 20;
2129 fepriv->step_size = 0; /* no zigzag */
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002130 fepriv->max_drift = 0;
2131 break;
2132 }
2133 }
2134 if (dvb_override_tune_delay > 0)
2135 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
2136
2137 fepriv->state = FESTATE_RETUNE;
2138
2139 /* Request the search algorithm to search */
2140 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
2141
2142 dvb_frontend_clear_events(fe);
2143 dvb_frontend_add_event(fe, 0);
2144 dvb_frontend_wakeup(fe);
2145 fepriv->status = 0;
2146
2147 return 0;
2148}
2149
2150
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002151static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03002152 unsigned int cmd, void *parg)
2153{
2154 struct dvb_device *dvbdev = file->private_data;
2155 struct dvb_frontend *fe = dvbdev->priv;
2156 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002157 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Mauro Carvalho Chehabac897582013-02-15 13:39:31 -02002158 int err = -EOPNOTSUPP;
Steven Toth13c97bf2008-09-04 21:19:43 -03002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 switch (cmd) {
2161 case FE_GET_INFO: {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002162 struct dvb_frontend_info* info = parg;
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002163
Patrick Boettcherdea74862006-05-14 05:01:31 -03002164 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
Guillaume Audirac2030c032010-05-06 09:30:25 -03002165 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002167 /*
2168 * Associate the 4 delivery systems supported by DVBv3
2169 * API with their DVBv5 counterpart. For the other standards,
2170 * use the closest type, assuming that it would hopefully
2171 * work with a DVBv3 application.
2172 * It should be noticed that, on multi-frontend devices with
2173 * different types (terrestrial and cable, for example),
2174 * a pure DVBv3 application won't be able to use all delivery
2175 * systems. Yet, changing the DVBv5 cache to the other delivery
2176 * system should be enough for making it work.
2177 */
2178 switch (dvbv3_type(c->delivery_system)) {
2179 case DVBV3_QPSK:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002180 info->type = FE_QPSK;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002181 break;
2182 case DVBV3_ATSC:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002183 info->type = FE_ATSC;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002184 break;
2185 case DVBV3_QAM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002186 info->type = FE_QAM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002187 break;
2188 case DVBV3_OFDM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002189 info->type = FE_OFDM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002190 break;
2191 default:
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002192 dev_err(fe->dvb->device,
2193 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2194 __func__, c->delivery_system);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002195 fe->ops.info.type = FE_OFDM;
2196 }
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002197 dev_dbg(fe->dvb->device, "%s: current delivery system on cache: %d, V3 type: %d\n",
2198 __func__, c->delivery_system, fe->ops.info.type);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2201 * do it, it is done for it. */
2202 info->caps |= FE_CAN_INVERSION_AUTO;
2203 err = 0;
2204 break;
2205 }
2206
Peter Beutner6757ccc2005-07-07 17:57:36 -07002207 case FE_READ_STATUS: {
2208 fe_status_t* status = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002210 /* if retune was requested but hasn't occurred yet, prevent
Peter Beutner6757ccc2005-07-07 17:57:36 -07002211 * that user get signal state from previous tuning */
Janne Grunau01886252009-09-01 19:23:09 -03002212 if (fepriv->state == FESTATE_RETUNE ||
2213 fepriv->state == FESTATE_ERROR) {
Peter Beutner6757ccc2005-07-07 17:57:36 -07002214 err=0;
2215 *status = 0;
2216 break;
2217 }
2218
Patrick Boettcherdea74862006-05-14 05:01:31 -03002219 if (fe->ops.read_status)
2220 err = fe->ops.read_status(fe, status);
Peter Beutner6757ccc2005-07-07 17:57:36 -07002221 break;
2222 }
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 case FE_READ_BER:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002225 if (fe->ops.read_ber) {
2226 if (fepriv->thread)
2227 err = fe->ops.read_ber(fe, (__u32 *) parg);
2228 else
2229 err = -EAGAIN;
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 break;
2232
2233 case FE_READ_SIGNAL_STRENGTH:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002234 if (fe->ops.read_signal_strength) {
2235 if (fepriv->thread)
2236 err = fe->ops.read_signal_strength(fe, (__u16 *) parg);
2237 else
2238 err = -EAGAIN;
2239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 break;
2241
2242 case FE_READ_SNR:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002243 if (fe->ops.read_snr) {
2244 if (fepriv->thread)
2245 err = fe->ops.read_snr(fe, (__u16 *) parg);
2246 else
2247 err = -EAGAIN;
2248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 break;
2250
2251 case FE_READ_UNCORRECTED_BLOCKS:
Antti Palosaari48caa6f2012-08-09 19:00:51 -03002252 if (fe->ops.read_ucblocks) {
2253 if (fepriv->thread)
2254 err = fe->ops.read_ucblocks(fe, (__u32 *) parg);
2255 else
2256 err = -EAGAIN;
2257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 break;
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 case FE_DISEQC_RESET_OVERLOAD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002261 if (fe->ops.diseqc_reset_overload) {
2262 err = fe->ops.diseqc_reset_overload(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 fepriv->state = FESTATE_DISEQC;
2264 fepriv->status = 0;
2265 }
2266 break;
2267
2268 case FE_DISEQC_SEND_MASTER_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002269 if (fe->ops.diseqc_send_master_cmd) {
2270 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 fepriv->state = FESTATE_DISEQC;
2272 fepriv->status = 0;
2273 }
2274 break;
2275
2276 case FE_DISEQC_SEND_BURST:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002277 if (fe->ops.diseqc_send_burst) {
2278 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 fepriv->state = FESTATE_DISEQC;
2280 fepriv->status = 0;
2281 }
2282 break;
2283
2284 case FE_SET_TONE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002285 if (fe->ops.set_tone) {
2286 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03002287 fepriv->tone = (fe_sec_tone_mode_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 fepriv->state = FESTATE_DISEQC;
2289 fepriv->status = 0;
2290 }
2291 break;
2292
2293 case FE_SET_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002294 if (fe->ops.set_voltage) {
2295 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03002296 fepriv->voltage = (fe_sec_voltage_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 fepriv->state = FESTATE_DISEQC;
2298 fepriv->status = 0;
2299 }
2300 break;
2301
2302 case FE_DISHNETWORK_SEND_LEGACY_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002303 if (fe->ops.dishnetwork_send_legacy_command) {
2304 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 fepriv->state = FESTATE_DISEQC;
2306 fepriv->status = 0;
Patrick Boettcherdea74862006-05-14 05:01:31 -03002307 } else if (fe->ops.set_voltage) {
NooneImportant83b75b02005-11-08 21:35:27 -08002308 /*
2309 * NOTE: This is a fallback condition. Some frontends
2310 * (stv0299 for instance) take longer than 8msec to
2311 * respond to a set_voltage command. Those switches
2312 * need custom routines to switch properly. For all
Guillaume Audirac2030c032010-05-06 09:30:25 -03002313 * other frontends, the following should work ok.
NooneImportant83b75b02005-11-08 21:35:27 -08002314 * Dish network legacy switches (as used by Dish500)
2315 * are controlled by sending 9-bit command words
2316 * spaced 8msec apart.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002317 * the actual command word is switch/port dependent
NooneImportant83b75b02005-11-08 21:35:27 -08002318 * so it is up to the userspace application to send
2319 * the right command.
2320 * The command must always start with a '0' after
2321 * initialization, so parg is 8 bits and does not
2322 * include the initialization or start bit
2323 */
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002324 unsigned long swcmd = ((unsigned long) parg) << 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002325 struct timeval nexttime;
2326 struct timeval tv[10];
2327 int i;
2328 u8 last = 1;
2329 if (dvb_frontend_debug)
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002330 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
NooneImportant83b75b02005-11-08 21:35:27 -08002331 do_gettimeofday(&nexttime);
2332 if (dvb_frontend_debug)
Ezequiel Garciab9b1b3a2012-10-23 15:57:23 -03002333 tv[0] = nexttime;
NooneImportant83b75b02005-11-08 21:35:27 -08002334 /* before sending a command, initialize by sending
2335 * a 32ms 18V to the switch
2336 */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002337 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002338 dvb_frontend_sleep_until(&nexttime, 32000);
2339
2340 for (i = 0; i < 9; i++) {
2341 if (dvb_frontend_debug)
2342 do_gettimeofday(&tv[i + 1]);
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002343 if ((swcmd & 0x01) != last) {
NooneImportant83b75b02005-11-08 21:35:27 -08002344 /* set voltage to (last ? 13V : 18V) */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002345 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002346 last = (last) ? 0 : 1;
2347 }
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002348 swcmd = swcmd >> 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002349 if (i != 8)
2350 dvb_frontend_sleep_until(&nexttime, 8000);
2351 }
2352 if (dvb_frontend_debug) {
2353 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002354 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -08002355 for (i = 1; i < 10; i++)
2356 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2357 }
2358 err = 0;
2359 fepriv->state = FESTATE_DISEQC;
2360 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 }
2362 break;
2363
2364 case FE_DISEQC_RECV_SLAVE_REPLY:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002365 if (fe->ops.diseqc_recv_slave_reply)
2366 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 break;
2368
2369 case FE_ENABLE_HIGH_LNB_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002370 if (fe->ops.enable_high_lnb_voltage)
2371 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 break;
2373
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002374 case FE_SET_FRONTEND:
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002375 err = dvbv3_set_delivery_system(fe);
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03002376 if (err)
2377 break;
2378
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002379 err = dtv_property_cache_sync(fe, c, parg);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002380 if (err)
2381 break;
2382 err = dtv_set_frontend(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 case FE_GET_EVENT:
2385 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2386 break;
2387
2388 case FE_GET_FRONTEND:
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002389 err = dtv_get_frontend(fe, parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 break;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002391
2392 case FE_SET_FRONTEND_TUNE_MODE:
Hans Verkuile18828e2006-01-09 15:25:28 -02002393 fepriv->tune_mode_flags = (unsigned long) parg;
Trent Piepho1b172e02006-06-29 13:16:04 -03002394 err = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002395 break;
Peter Senna Tschudinc2c1b412012-09-28 05:37:22 -03002396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 return err;
2399}
2400
Steven Toth6b73eea2008-09-04 01:12:25 -03002401
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2403{
2404 struct dvb_device *dvbdev = file->private_data;
2405 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002406 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002408 dev_dbg_ratelimited(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 poll_wait (file, &fepriv->events.wait_queue, wait);
2411
2412 if (fepriv->events.eventw != fepriv->events.eventr)
2413 return (POLLIN | POLLRDNORM | POLLPRI);
2414
2415 return 0;
2416}
2417
2418static int dvb_frontend_open(struct inode *inode, struct file *file)
2419{
2420 struct dvb_device *dvbdev = file->private_data;
2421 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002422 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Darron Broad59b18422008-10-11 11:44:05 -03002423 struct dvb_adapter *adapter = fe->dvb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 int ret;
2425
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002426 dev_dbg(fe->dvb->device, "%s:\n", __func__);
matthieu castete36309f2010-05-05 15:59:20 -03002427 if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2428 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Darron Broad59b18422008-10-11 11:44:05 -03002430 if (adapter->mfe_shared) {
2431 mutex_lock (&adapter->mfe_lock);
Darron Broad65946902008-10-15 13:37:59 -03002432
2433 if (adapter->mfe_dvbdev == NULL)
2434 adapter->mfe_dvbdev = dvbdev;
2435
2436 else if (adapter->mfe_dvbdev != dvbdev) {
2437 struct dvb_device
2438 *mfedev = adapter->mfe_dvbdev;
2439 struct dvb_frontend
2440 *mfe = mfedev->priv;
2441 struct dvb_frontend_private
2442 *mfepriv = mfe->frontend_priv;
2443 int mferetry = (dvb_mfe_wait_time << 1);
2444
2445 mutex_unlock (&adapter->mfe_lock);
2446 while (mferetry-- && (mfedev->users != -1 ||
2447 mfepriv->thread != NULL)) {
2448 if(msleep_interruptible(500)) {
2449 if(signal_pending(current))
2450 return -EINTR;
Darron Broad59b18422008-10-11 11:44:05 -03002451 }
2452 }
Darron Broad65946902008-10-15 13:37:59 -03002453
2454 mutex_lock (&adapter->mfe_lock);
2455 if(adapter->mfe_dvbdev != dvbdev) {
2456 mfedev = adapter->mfe_dvbdev;
2457 mfe = mfedev->priv;
2458 mfepriv = mfe->frontend_priv;
2459 if (mfedev->users != -1 ||
2460 mfepriv->thread != NULL) {
2461 mutex_unlock (&adapter->mfe_lock);
2462 return -EBUSY;
2463 }
2464 adapter->mfe_dvbdev = dvbdev;
2465 }
Darron Broad59b18422008-10-11 11:44:05 -03002466 }
2467 }
2468
Oliver Endriss48136e12007-08-25 12:00:23 -03002469 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2470 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
Darron Broad59b18422008-10-11 11:44:05 -03002471 goto err0;
Devin Heitmueller2d196932011-07-04 21:55:01 -03002472
2473 /* If we took control of the bus, we need to force
2474 reinitialization. This is because many ts_bus_ctrl()
2475 functions strobe the RESET pin on the demod, and if the
2476 frontend thread already exists then the dvb_init() routine
2477 won't get called (which is what usually does initial
2478 register configuration). */
2479 fepriv->reinitialise = 1;
Steven Tothba7e6f32006-09-25 12:41:53 -03002480 }
2481
Oliver Endriss48136e12007-08-25 12:00:23 -03002482 if ((ret = dvb_generic_open (inode, file)) < 0)
2483 goto err1;
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002484
Oliver Endriss48136e12007-08-25 12:00:23 -03002485 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002486 /* normal tune mode when opened R/W */
2487 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2488 fepriv->tone = -1;
2489 fepriv->voltage = -1;
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 ret = dvb_frontend_start (fe);
2492 if (ret)
Oliver Endriss48136e12007-08-25 12:00:23 -03002493 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
2495 /* empty event queue */
2496 fepriv->events.eventr = fepriv->events.eventw = 0;
2497 }
2498
Darron Broad59b18422008-10-11 11:44:05 -03002499 if (adapter->mfe_shared)
2500 mutex_unlock (&adapter->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return ret;
Oliver Endriss48136e12007-08-25 12:00:23 -03002502
2503err2:
2504 dvb_generic_release(inode, file);
2505err1:
2506 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2507 fe->ops.ts_bus_ctrl(fe, 0);
Darron Broad59b18422008-10-11 11:44:05 -03002508err0:
2509 if (adapter->mfe_shared)
2510 mutex_unlock (&adapter->mfe_lock);
Oliver Endriss48136e12007-08-25 12:00:23 -03002511 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514static int dvb_frontend_release(struct inode *inode, struct file *file)
2515{
2516 struct dvb_device *dvbdev = file->private_data;
2517 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002518 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002519 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002521 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002523 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 fepriv->release_jiffies = jiffies;
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002525 mb();
2526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002528 ret = dvb_generic_release (inode, file);
2529
Oliver Endriss48136e12007-08-25 12:00:23 -03002530 if (dvbdev->users == -1) {
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002531 wake_up(&fepriv->wait_queue);
Al Viro03feee32013-04-15 13:58:21 -04002532 if (fepriv->exit != DVB_FE_NO_EXIT)
Oliver Endriss48136e12007-08-25 12:00:23 -03002533 wake_up(&dvbdev->wait_queue);
Oliver Endriss48136e12007-08-25 12:00:23 -03002534 if (fe->ops.ts_bus_ctrl)
2535 fe->ops.ts_bus_ctrl(fe, 0);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002536 }
Oliver Endriss48136e12007-08-25 12:00:23 -03002537
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
Jan Engelhardt784e29d2009-01-11 06:12:43 -03002541static const struct file_operations dvb_frontend_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 .owner = THIS_MODULE,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002543 .unlocked_ioctl = dvb_generic_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 .poll = dvb_frontend_poll,
2545 .open = dvb_frontend_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002546 .release = dvb_frontend_release,
2547 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548};
2549
Antti Palosaari06bae122012-08-14 22:21:06 -03002550int dvb_frontend_suspend(struct dvb_frontend *fe)
2551{
2552 int ret = 0;
2553
2554 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2555 fe->id);
2556
2557 if (fe->ops.tuner_ops.sleep)
2558 ret = fe->ops.tuner_ops.sleep(fe);
2559
2560 if (fe->ops.sleep)
2561 ret = fe->ops.sleep(fe);
2562
2563 return ret;
2564}
2565EXPORT_SYMBOL(dvb_frontend_suspend);
2566
2567int dvb_frontend_resume(struct dvb_frontend *fe)
2568{
2569 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2570 int ret = 0;
2571
2572 dev_dbg(fe->dvb->device, "%s: adap=%d fe=%d\n", __func__, fe->dvb->num,
2573 fe->id);
2574
2575 if (fe->ops.init)
2576 ret = fe->ops.init(fe);
2577
2578 if (fe->ops.tuner_ops.init)
2579 ret = fe->ops.tuner_ops.init(fe);
2580
2581 fepriv->state = FESTATE_RETUNE;
2582 dvb_frontend_wakeup(fe);
2583
2584 return ret;
2585}
2586EXPORT_SYMBOL(dvb_frontend_resume);
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588int dvb_register_frontend(struct dvb_adapter* dvb,
2589 struct dvb_frontend* fe)
2590{
2591 struct dvb_frontend_private *fepriv;
2592 static const struct dvb_device dvbdev_template = {
2593 .users = ~0,
2594 .writers = 1,
2595 .readers = (~0)-1,
2596 .fops = &dvb_frontend_fops,
2597 .kernel_ioctl = dvb_frontend_ioctl
2598 };
2599
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002600 dev_dbg(dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Ingo Molnar3593cab2006-02-07 06:49:14 -02002602 if (mutex_lock_interruptible(&frontend_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return -ERESTARTSYS;
2604
Panagiotis Issaris74081872006-01-11 19:40:56 -02002605 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (fe->frontend_priv == NULL) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002607 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return -ENOMEM;
2609 }
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002610 fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Thomas Gleixnera0a47142010-09-07 11:33:27 -03002612 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 init_waitqueue_head (&fepriv->wait_queue);
2614 init_waitqueue_head (&fepriv->events.wait_queue);
Matthias Kaehlcke03b76122007-07-30 14:58:10 -03002615 mutex_init(&fepriv->events.mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 fe->dvb = dvb;
2617 fepriv->inversion = INVERSION_OFF;
2618
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002619 dev_info(fe->dvb->device,
2620 "DVB: registering adapter %i frontend %i (%s)...\n",
2621 fe->dvb->num, fe->id, fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2624 fe, DVB_DEVICE_FRONTEND);
2625
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002626 /*
2627 * Initialize the cache to the proper values according with the
2628 * first supported delivery system (ops->delsys[0])
2629 */
Mauro Carvalho Chehab240ab502012-01-10 18:00:50 -03002630
Mauro Carvalho Chehabbe431b12013-03-18 15:25:36 -03002631 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002632 dvb_frontend_clear_cache(fe);
2633
Ingo Molnar3593cab2006-02-07 06:49:14 -02002634 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return 0;
2636}
2637EXPORT_SYMBOL(dvb_register_frontend);
2638
2639int dvb_unregister_frontend(struct dvb_frontend* fe)
2640{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002641 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Antti Palosaari36bdbc32012-08-15 20:28:37 -03002642 dev_dbg(fe->dvb->device, "%s:\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Markus Rechberger57861b42007-04-14 10:19:18 -03002644 mutex_lock(&frontend_mutex);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002645 dvb_frontend_stop (fe);
Markus Rechberger57861b42007-04-14 10:19:18 -03002646 mutex_unlock(&frontend_mutex);
2647
2648 if (fepriv->dvbdev->users < -1)
2649 wait_event(fepriv->dvbdev->wait_queue,
2650 fepriv->dvbdev->users==-1);
2651
Ingo Molnar3593cab2006-02-07 06:49:14 -02002652 mutex_lock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 dvb_unregister_device (fepriv->dvbdev);
Andrew de Quinceyd9955062006-08-08 09:10:08 -03002654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 /* fe is invalid now */
2656 kfree(fepriv);
Ingo Molnar3593cab2006-02-07 06:49:14 -02002657 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 return 0;
2659}
2660EXPORT_SYMBOL(dvb_unregister_frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002661
Mauro Carvalho Chehab149ef722008-04-29 21:38:46 -03002662#ifdef CONFIG_MEDIA_ATTACH
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002663void dvb_frontend_detach(struct dvb_frontend* fe)
2664{
2665 void *ptr;
2666
2667 if (fe->ops.release_sec) {
2668 fe->ops.release_sec(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002669 dvb_detach(fe->ops.release_sec);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002670 }
2671 if (fe->ops.tuner_ops.release) {
2672 fe->ops.tuner_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002673 dvb_detach(fe->ops.tuner_ops.release);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002674 }
Michael Krufky2426a272007-12-21 11:34:45 -03002675 if (fe->ops.analog_ops.release) {
2676 fe->ops.analog_ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002677 dvb_detach(fe->ops.analog_ops.release);
Michael Krufky2426a272007-12-21 11:34:45 -03002678 }
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002679 ptr = (void*)fe->ops.release;
2680 if (ptr) {
2681 fe->ops.release(fe);
Mauro Carvalho Chehab4647f482014-05-29 09:20:13 -03002682 dvb_detach(ptr);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002683 }
2684}
2685#else
2686void dvb_frontend_detach(struct dvb_frontend* fe)
2687{
2688 if (fe->ops.release_sec)
2689 fe->ops.release_sec(fe);
2690 if (fe->ops.tuner_ops.release)
2691 fe->ops.tuner_ops.release(fe);
Michael Krufky2426a272007-12-21 11:34:45 -03002692 if (fe->ops.analog_ops.release)
2693 fe->ops.analog_ops.release(fe);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002694 if (fe->ops.release)
2695 fe->ops.release(fe);
2696}
2697#endif
2698EXPORT_SYMBOL(dvb_frontend_detach);