blob: f5fa7aa896189db8f1276648278c2df9c10c1e3f [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
69#define dprintk if (dvb_frontend_debug) printk
70
71#define FESTATE_IDLE 1
72#define FESTATE_RETUNE 2
73#define FESTATE_TUNING_FAST 4
74#define FESTATE_TUNING_SLOW 8
75#define FESTATE_TUNED 16
76#define FESTATE_ZIGZAG_FAST 32
77#define FESTATE_ZIGZAG_SLOW 64
78#define FESTATE_DISEQC 128
Janne Grunau01886252009-09-01 19:23:09 -030079#define FESTATE_ERROR 256
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
81#define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
82#define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
83#define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
Manu Abraham0249ef12006-06-21 10:27:31 -030084
85#define FE_ALGO_HW 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
88 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
89 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
90 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
91 * FESTATE_TUNED. The frontend has successfully locked on.
92 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
93 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
94 * FESTATE_DISEQC. A DISEQC command has just been issued.
95 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
96 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
97 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
98 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
99 */
100
matthieu castete36309f2010-05-05 15:59:20 -0300101#define DVB_FE_NO_EXIT 0
102#define DVB_FE_NORMAL_EXIT 1
103#define DVB_FE_DEVICE_REMOVED 2
104
Ingo Molnar3593cab2006-02-07 06:49:14 -0200105static DEFINE_MUTEX(frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107struct dvb_frontend_private {
108
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200109 /* thread/frontend values */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 struct dvb_device *dvbdev;
Andreas Oberrittera5959db2011-05-08 20:03:40 -0300111 struct dvb_frontend_parameters parameters_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct dvb_fe_events events;
113 struct semaphore sem;
114 struct list_head list_head;
115 wait_queue_head_t wait_queue;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300116 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 unsigned long release_jiffies;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200118 unsigned int exit;
119 unsigned int wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 fe_status_t status;
Peter Beutner400b7082006-01-09 15:32:43 -0200121 unsigned long tune_mode_flags;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200122 unsigned int delay;
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300123 unsigned int reinitialise;
Andrew de Quincey64454012006-04-06 14:32:23 -0300124 int tone;
125 int voltage;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200126
127 /* swzigzag values */
128 unsigned int state;
129 unsigned int bending;
130 int lnb_drift;
131 unsigned int inversion;
132 unsigned int auto_step;
133 unsigned int auto_sub_step;
134 unsigned int started_auto_step;
135 unsigned int min_delay;
136 unsigned int max_drift;
137 unsigned int step_size;
138 int quality;
139 unsigned int check_wrapped;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300140 enum dvbfe_search algo_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300143static void dvb_frontend_wakeup(struct dvb_frontend *fe);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300144static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -0300145 struct dvb_frontend_parameters *p_out);
146
147static bool has_get_frontend(struct dvb_frontend *fe)
148{
Mauro Carvalho Chehabb1e9a65012011-12-26 16:43:32 -0300149 return fe->ops.get_frontend;
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:
180 case SYS_DMBTH:
181 return DVBV3_OFDM;
182 case SYS_ATSC:
183 case SYS_DVBC_ANNEX_B:
184 return DVBV3_ATSC;
185 case SYS_UNDEFINED:
186 case SYS_ISDBC:
187 case SYS_DVBH:
188 case SYS_DAB:
189 case SYS_ATSCMH:
190 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
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300208 dprintk ("%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
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300238 dprintk ("%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{
Steven Toth363c35f2008-10-11 11:05:50 -0300283 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 fe->dvb->num,
Steven Toth363c35f2008-10-11 11:05:50 -0300285 fe->id,
Patrick Boettcherdea74862006-05-14 05:01:31 -0300286 fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Patrick Boettcherdea74862006-05-14 05:01:31 -0300288 if (fe->ops.init)
289 fe->ops.init(fe);
290 if (fe->ops.tuner_ops.init) {
Manu Abraham3b37a152008-10-20 18:14:14 -0300291 if (fe->ops.i2c_gate_ctrl)
292 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300293 fe->ops.tuner_ops.init(fe);
294 if (fe->ops.i2c_gate_ctrl)
295 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300299void dvb_frontend_reinitialise(struct dvb_frontend *fe)
300{
301 struct dvb_frontend_private *fepriv = fe->frontend_priv;
302
303 fepriv->reinitialise = 1;
304 dvb_frontend_wakeup(fe);
305}
306EXPORT_SYMBOL(dvb_frontend_reinitialise);
307
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200308static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200310 int q2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300312 dprintk ("%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
404 dprintk("%s: drift:%i inversion:%i auto_step:%i "
405 "auto_sub_step:%i started_auto_step:%i\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300406 __func__, fepriv->lnb_drift, fepriv->inversion,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
408
409 /* set the frontend itself */
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300410 c->frequency += fepriv->lnb_drift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (autoinversion)
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300412 c->inversion = fepriv->inversion;
413 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300414 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300415 fe_set_err = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300416 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300417 if (fe_set_err < 0) {
418 fepriv->state = FESTATE_ERROR;
419 return fe_set_err;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300422 c->frequency = original_frequency;
423 c->inversion = original_inversion;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 fepriv->auto_sub_step++;
426 return 0;
427}
428
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200429static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
430{
Andrew de Quincey30d94642006-11-16 22:12:40 -0300431 fe_status_t s = 0;
Janne Grunau01886252009-09-01 19:23:09 -0300432 int retval = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200433 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300434 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200435
436 /* if we've got no parameters, just keep idling */
437 if (fepriv->state & FESTATE_IDLE) {
438 fepriv->delay = 3*HZ;
439 fepriv->quality = 0;
440 return;
441 }
442
443 /* in SCAN mode, we just set the frontend when asked and leave it alone */
444 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
445 if (fepriv->state & FESTATE_RETUNE) {
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300446 tmp = *c;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300447 if (fe->ops.set_frontend)
Mauro Carvalho Chehabbc9cd272011-12-20 15:31:54 -0300448 retval = fe->ops.set_frontend(fe);
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300449 *c = tmp;
Janne Grunau01886252009-09-01 19:23:09 -0300450 if (retval < 0)
451 fepriv->state = FESTATE_ERROR;
452 else
453 fepriv->state = FESTATE_TUNED;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200454 }
455 fepriv->delay = 3*HZ;
456 fepriv->quality = 0;
457 return;
458 }
459
460 /* get the frontend status */
461 if (fepriv->state & FESTATE_RETUNE) {
462 s = 0;
463 } else {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300464 if (fe->ops.read_status)
465 fe->ops.read_status(fe, &s);
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200466 if (s != fepriv->status) {
467 dvb_frontend_add_event(fe, s);
468 fepriv->status = s;
469 }
470 }
471
472 /* if we're not tuned, and we have a lock, move to the TUNED state */
473 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
474 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
475 fepriv->state = FESTATE_TUNED;
476
477 /* if we're tuned, then we have determined the correct inversion */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300478 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Mauro Carvalho Chehabac3852c2011-12-30 09:30:25 -0300479 (c->inversion == INVERSION_AUTO)) {
480 c->inversion = fepriv->inversion;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200481 }
482 return;
483 }
484
485 /* if we are tuned already, check we're still locked */
486 if (fepriv->state & FESTATE_TUNED) {
487 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
488
489 /* we're tuned, and the lock is still good... */
490 if (s & FE_HAS_LOCK) {
491 return;
492 } else { /* if we _WERE_ tuned, but now don't have a lock */
493 fepriv->state = FESTATE_ZIGZAG_FAST;
494 fepriv->started_auto_step = fepriv->auto_step;
495 fepriv->check_wrapped = 0;
496 }
497 }
498
499 /* don't actually do anything if we're in the LOSTLOCK state,
500 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
501 if ((fepriv->state & FESTATE_LOSTLOCK) &&
Patrick Boettcherdea74862006-05-14 05:01:31 -0300502 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200503 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
504 return;
505 }
506
507 /* don't do anything if we're in the DISEQC state, since this
508 * might be someone with a motorized dish controlled by DISEQC.
509 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
510 if (fepriv->state & FESTATE_DISEQC) {
511 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
512 return;
513 }
514
515 /* if we're in the RETUNE state, set everything up for a brand
516 * new scan, keeping the current inversion setting, as the next
517 * tune is _very_ likely to require the same */
518 if (fepriv->state & FESTATE_RETUNE) {
519 fepriv->lnb_drift = 0;
520 fepriv->auto_step = 0;
521 fepriv->auto_sub_step = 0;
522 fepriv->started_auto_step = 0;
523 fepriv->check_wrapped = 0;
524 }
525
526 /* fast zigzag. */
527 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
528 fepriv->delay = fepriv->min_delay;
529
Guillaume Audirac2030c032010-05-06 09:30:25 -0300530 /* perform a tune */
Janne Grunau01886252009-09-01 19:23:09 -0300531 retval = dvb_frontend_swzigzag_autotune(fe,
532 fepriv->check_wrapped);
533 if (retval < 0) {
534 return;
535 } else if (retval) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200536 /* OK, if we've run out of trials at the fast speed.
537 * Drop back to slow for the _next_ attempt */
538 fepriv->state = FESTATE_SEARCHING_SLOW;
539 fepriv->started_auto_step = fepriv->auto_step;
540 return;
541 }
542 fepriv->check_wrapped = 1;
543
544 /* if we've just retuned, enter the ZIGZAG_FAST state.
545 * This ensures we cannot return from an
546 * FE_SET_FRONTEND ioctl before the first frontend tune
547 * occurs */
548 if (fepriv->state & FESTATE_RETUNE) {
549 fepriv->state = FESTATE_TUNING_FAST;
550 }
551 }
552
553 /* slow zigzag */
554 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
555 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
556
557 /* Note: don't bother checking for wrapping; we stay in this
558 * state until we get a lock */
559 dvb_frontend_swzigzag_autotune(fe, 0);
560 }
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
564{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700565 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
matthieu castete36309f2010-05-05 15:59:20 -0300567 if (fepriv->exit != DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 1;
569
570 if (fepriv->dvbdev->writers == 1)
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -0300571 if (time_after_eq(jiffies, fepriv->release_jiffies +
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200572 dvb_shutdown_timeout * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return 1;
574
575 return 0;
576}
577
578static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
579{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700580 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (fepriv->wakeup) {
583 fepriv->wakeup = 0;
584 return 1;
585 }
586 return dvb_frontend_is_exiting(fe);
587}
588
589static void dvb_frontend_wakeup(struct dvb_frontend *fe)
590{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700591 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 fepriv->wakeup = 1;
594 wake_up_interruptible(&fepriv->wait_queue);
595}
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597static int dvb_frontend_thread(void *data)
598{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700599 struct dvb_frontend *fe = data;
600 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 fe_status_t s;
Manu Abrahamc59e7872008-10-14 16:34:07 -0300602 enum dvbfe_algo algo;
603
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300604 bool re_tune = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300606 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200608 fepriv->check_wrapped = 0;
609 fepriv->quality = 0;
610 fepriv->delay = 3*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 fepriv->wakeup = 0;
Andrew de Quincey04c56d02006-07-10 03:34:14 -0300613 fepriv->reinitialise = 0;
614
615 dvb_frontend_init(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700617 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 while (1) {
619 up(&fepriv->sem); /* is locked when we enter the thread... */
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300620restart:
Hans Verkuil94238e92011-08-27 11:30:25 -0300621 wait_event_interruptible_timeout(fepriv->wait_queue,
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700622 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
623 || freezing(current),
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300624 fepriv->delay);
625
626 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* got signal or quitting */
matthieu castete36309f2010-05-05 15:59:20 -0300628 fepriv->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
630 }
631
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300632 if (try_to_freeze())
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300633 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 if (down_interruptible(&fepriv->sem))
636 break;
637
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300638 if (fepriv->reinitialise) {
639 dvb_frontend_init(fe);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300640 if (fe->ops.set_tone && fepriv->tone != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300641 fe->ops.set_tone(fe, fepriv->tone);
Andreas Oberritter42f4e772011-08-24 14:33:52 -0300642 if (fe->ops.set_voltage && fepriv->voltage != -1)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300643 fe->ops.set_voltage(fe, fepriv->voltage);
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300644 fepriv->reinitialise = 0;
645 }
646
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200647 /* do an iteration of the tuning loop */
Manu Abrahamd772bd02006-06-24 11:18:58 -0300648 if (fe->ops.get_frontend_algo) {
Manu Abrahamc59e7872008-10-14 16:34:07 -0300649 algo = fe->ops.get_frontend_algo(fe);
650 switch (algo) {
651 case DVBFE_ALGO_HW:
652 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300653
Manu Abrahamd772bd02006-06-24 11:18:58 -0300654 if (fepriv->state & FESTATE_RETUNE) {
Manu Abrahamc59e7872008-10-14 16:34:07 -0300655 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300656 re_tune = true;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300657 fepriv->state = FESTATE_TUNED;
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Manu Abrahamc59e7872008-10-14 16:34:07 -0300660 if (fe->ops.tune)
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300661 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300662
Darron Broad2fac9a02008-12-18 06:27:50 -0300663 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
Manu Abrahamc59e7872008-10-14 16:34:07 -0300664 dprintk("%s: state changed, adding current state\n", __func__);
Manu Abrahamd772bd02006-06-24 11:18:58 -0300665 dvb_frontend_add_event(fe, s);
666 fepriv->status = s;
667 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300668 break;
669 case DVBFE_ALGO_SW:
670 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
Manu Abraham70d90632006-06-29 22:05:23 -0300671 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300672 break;
673 case DVBFE_ALGO_CUSTOM:
Manu Abrahamc59e7872008-10-14 16:34:07 -0300674 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
675 if (fepriv->state & FESTATE_RETUNE) {
676 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
677 fepriv->state = FESTATE_TUNED;
678 }
679 /* Case where we are going to search for a carrier
680 * User asked us to retune again for some reason, possibly
681 * requesting a search with a new set of parameters
682 */
683 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300684 if (fe->ops.search) {
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -0300685 fepriv->algo_status = fe->ops.search(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300686 /* We did do a search as was requested, the flags are
687 * now unset as well and has the flags wrt to search.
688 */
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300689 } else {
690 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
691 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300692 }
693 /* Track the carrier if the search was successful */
Mauro Carvalho Chehab1b5d8712011-12-26 18:11:25 -0300694 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300695 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
696 fepriv->delay = HZ / 2;
697 }
698 fe->ops.read_status(fe, &s);
699 if (s != fepriv->status) {
700 dvb_frontend_add_event(fe, s); /* update event list */
701 fepriv->status = s;
702 if (!(s & FE_HAS_LOCK)) {
703 fepriv->delay = HZ / 10;
704 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
705 } else {
706 fepriv->delay = 60 * HZ;
707 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300708 }
709 break;
710 default:
711 dprintk("%s: UNDEFINED ALGO !\n", __func__);
712 break;
713 }
714 } else {
Manu Abraham4a4edcc2006-06-25 05:46:26 -0300715 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
Oliver Endriss608f62d2007-08-25 13:17:53 -0300719 if (dvb_powerdown_on_sleep) {
720 if (fe->ops.set_voltage)
721 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300722 if (fe->ops.tuner_ops.sleep) {
Devin Heitmueller41286d92008-11-16 00:44:52 -0300723 if (fe->ops.i2c_gate_ctrl)
724 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300725 fe->ops.tuner_ops.sleep(fe);
726 if (fe->ops.i2c_gate_ctrl)
727 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300728 }
Patrick Boettcherdea74862006-05-14 05:01:31 -0300729 if (fe->ops.sleep)
730 fe->ops.sleep(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300733 fepriv->thread = NULL;
matthieu castete36309f2010-05-05 15:59:20 -0300734 if (kthread_should_stop())
735 fepriv->exit = DVB_FE_DEVICE_REMOVED;
736 else
737 fepriv->exit = DVB_FE_NO_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 mb();
739
740 dvb_frontend_wakeup(fe);
741 return 0;
742}
743
744static void dvb_frontend_stop(struct dvb_frontend *fe)
745{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700746 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300748 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
matthieu castete36309f2010-05-05 15:59:20 -0300750 fepriv->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 mb();
752
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300753 if (!fepriv->thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return;
755
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300756 kthread_stop(fepriv->thread);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300757
Thomas Gleixnera0a47142010-09-07 11:33:27 -0300758 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 fepriv->state = FESTATE_IDLE;
760
761 /* paranoia check in case a signal arrived */
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300762 if (fepriv->thread)
763 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
764 fepriv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
NooneImportant83b75b02005-11-08 21:35:27 -0800767s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
768{
769 return ((curtime.tv_usec < lasttime.tv_usec) ?
770 1000000 - lasttime.tv_usec + curtime.tv_usec :
771 curtime.tv_usec - lasttime.tv_usec);
772}
773EXPORT_SYMBOL(timeval_usec_diff);
774
775static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
776{
777 curtime->tv_usec += add_usec;
778 if (curtime->tv_usec >= 1000000) {
779 curtime->tv_usec -= 1000000;
780 curtime->tv_sec++;
781 }
782}
783
784/*
785 * Sleep until gettimeofday() > waketime + add_usec
786 * This needs to be as precise as possible, but as the delay is
787 * usually between 2ms and 32ms, it is done using a scheduled msleep
788 * followed by usleep (normally a busy-wait loop) for the remainder
789 */
790void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
791{
792 struct timeval lasttime;
793 s32 delta, newdelta;
794
795 timeval_usec_add(waketime, add_usec);
796
797 do_gettimeofday(&lasttime);
798 delta = timeval_usec_diff(lasttime, *waketime);
799 if (delta > 2500) {
800 msleep((delta - 1500) / 1000);
801 do_gettimeofday(&lasttime);
802 newdelta = timeval_usec_diff(lasttime, *waketime);
803 delta = (newdelta > delta) ? 0 : newdelta;
804 }
805 if (delta > 0)
806 udelay(delta);
807}
808EXPORT_SYMBOL(dvb_frontend_sleep_until);
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static int dvb_frontend_start(struct dvb_frontend *fe)
811{
812 int ret;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700813 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300814 struct task_struct *fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300816 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300818 if (fepriv->thread) {
matthieu castete36309f2010-05-05 15:59:20 -0300819 if (fepriv->exit == DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return 0;
821 else
822 dvb_frontend_stop (fe);
823 }
824
825 if (signal_pending(current))
826 return -EINTR;
827 if (down_interruptible (&fepriv->sem))
828 return -EINTR;
829
830 fepriv->state = FESTATE_IDLE;
matthieu castete36309f2010-05-05 15:59:20 -0300831 fepriv->exit = DVB_FE_NO_EXIT;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300832 fepriv->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 mb();
834
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300835 fe_thread = kthread_run(dvb_frontend_thread, fe,
Steven Toth363c35f2008-10-11 11:05:50 -0300836 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300837 if (IS_ERR(fe_thread)) {
838 ret = PTR_ERR(fe_thread);
839 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 up(&fepriv->sem);
841 return ret;
842 }
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300843 fepriv->thread = fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return 0;
845}
846
Guillaume Audirac2030c032010-05-06 09:30:25 -0300847static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
Oliver Endrissc471b332007-08-09 01:03:42 -0300848 u32 *freq_min, u32 *freq_max)
849{
850 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
851
852 if (fe->ops.info.frequency_max == 0)
853 *freq_max = fe->ops.tuner_ops.info.frequency_max;
854 else if (fe->ops.tuner_ops.info.frequency_max == 0)
855 *freq_max = fe->ops.info.frequency_max;
856 else
857 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
858
859 if (*freq_min == 0 || *freq_max == 0)
Steven Toth363c35f2008-10-11 11:05:50 -0300860 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
861 fe->dvb->num,fe->id);
Oliver Endrissc471b332007-08-09 01:03:42 -0300862}
863
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300864static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300865{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300866 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Oliver Endrissc471b332007-08-09 01:03:42 -0300867 u32 freq_min;
868 u32 freq_max;
869
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300870 /* range check: frequency */
Guillaume Audirac2030c032010-05-06 09:30:25 -0300871 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300872 if ((freq_min && c->frequency < freq_min) ||
873 (freq_max && c->frequency > freq_max)) {
Steven Toth363c35f2008-10-11 11:05:50 -0300874 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300875 fe->dvb->num, fe->id, c->frequency, freq_min, freq_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300876 return -EINVAL;
877 }
878
879 /* range check: symbol rate */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300880 switch (c->delivery_system) {
881 case SYS_DVBS:
882 case SYS_DVBS2:
883 case SYS_TURBO:
884 case SYS_DVBC_ANNEX_A:
885 case SYS_DVBC_ANNEX_C:
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300886 if ((fe->ops.info.symbol_rate_min &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300887 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300888 (fe->ops.info.symbol_rate_max &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300889 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
Steven Toth363c35f2008-10-11 11:05:50 -0300890 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300891 fe->dvb->num, fe->id, c->symbol_rate,
892 fe->ops.info.symbol_rate_min,
893 fe->ops.info.symbol_rate_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300894 return -EINVAL;
895 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300896 default:
897 break;
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300898 }
899
900 return 0;
901}
902
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300903static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
904{
Andreas Oberritter50727712011-05-08 20:03:39 -0300905 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300906 int i;
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200907 u32 delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300908
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200909 delsys = c->delivery_system;
Andreas Oberritter50727712011-05-08 20:03:39 -0300910 memset(c, 0, sizeof(struct dtv_frontend_properties));
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200911 c->delivery_system = delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300912
Andreas Oberritter50727712011-05-08 20:03:39 -0300913 c->state = DTV_CLEAR;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300914
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300915 dprintk("%s() Clearing cache for delivery system %d\n", __func__,
916 c->delivery_system);
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300917
Andreas Oberritter50727712011-05-08 20:03:39 -0300918 c->transmission_mode = TRANSMISSION_MODE_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300919 c->bandwidth_hz = 0; /* AUTO */
Andreas Oberritter50727712011-05-08 20:03:39 -0300920 c->guard_interval = GUARD_INTERVAL_AUTO;
921 c->hierarchy = HIERARCHY_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300922 c->symbol_rate = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -0300923 c->code_rate_HP = FEC_AUTO;
924 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300925 c->fec_inner = FEC_AUTO;
Mauro Carvalho Chehab39ce61a2011-11-11 12:46:23 -0200926 c->rolloff = ROLLOFF_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300927 c->voltage = SEC_VOLTAGE_OFF;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300928 c->sectone = SEC_TONE_OFF;
929 c->pilot = PILOT_AUTO;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300930
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300931 c->isdbt_partial_reception = 0;
932 c->isdbt_sb_mode = 0;
933 c->isdbt_sb_subchannel = 0;
934 c->isdbt_sb_segment_idx = 0;
935 c->isdbt_sb_segment_count = 0;
936 c->isdbt_layer_enabled = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300937 for (i = 0; i < 3; i++) {
Andreas Oberritter50727712011-05-08 20:03:39 -0300938 c->layer[i].fec = FEC_AUTO;
939 c->layer[i].modulation = QAM_AUTO;
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300940 c->layer[i].interleaving = 0;
941 c->layer[i].segment_count = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300942 }
943
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300944 c->isdbs_ts_id = 0;
945 c->dvbt2_plp_id = 0;
946
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300947 switch (c->delivery_system) {
948 case SYS_DVBS:
949 case SYS_DVBS2:
950 case SYS_TURBO:
951 c->modulation = QPSK; /* implied for DVB-S in legacy API */
952 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
953 break;
954 case SYS_ATSC:
955 c->modulation = VSB_8;
956 break;
957 default:
958 c->modulation = QAM_AUTO;
959 break;
960 }
961
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300962 return 0;
963}
964
965#define _DTV_CMD(n, s, b) \
966[n] = { \
967 .name = #n, \
968 .cmd = n, \
969 .set = s,\
970 .buffer = b \
971}
972
Andreas Oberritterec05a642011-05-26 07:54:14 -0300973static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300974 _DTV_CMD(DTV_TUNE, 1, 0),
975 _DTV_CMD(DTV_CLEAR, 1, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -0300976
977 /* Set */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300978 _DTV_CMD(DTV_FREQUENCY, 1, 0),
979 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
980 _DTV_CMD(DTV_MODULATION, 1, 0),
981 _DTV_CMD(DTV_INVERSION, 1, 0),
982 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
983 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
984 _DTV_CMD(DTV_INNER_FEC, 1, 0),
985 _DTV_CMD(DTV_VOLTAGE, 1, 0),
986 _DTV_CMD(DTV_TONE, 1, 0),
987 _DTV_CMD(DTV_PILOT, 1, 0),
988 _DTV_CMD(DTV_ROLLOFF, 1, 0),
989 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
990 _DTV_CMD(DTV_HIERARCHY, 1, 0),
991 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
992 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
993 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
994 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300995
996 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
997 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
998 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
999 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1000 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
Patrick Boettchere7b79492009-08-14 05:24:19 -03001001 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001002 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1003 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1004 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1005 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1006 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1007 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1008 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1009 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1010 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1011 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1012 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1013 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1014
1015 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 0, 0),
1016 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 0, 0),
1017 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 0, 0),
1018 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 0, 0),
1019 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 0, 0),
Patrick Boettchere7b79492009-08-14 05:24:19 -03001020 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 0, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001021 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 0, 0),
1022 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 0, 0),
1023 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 0, 0),
1024 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 0, 0),
1025 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 0, 0),
1026 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 0, 0),
1027 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 0, 0),
1028 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 0, 0),
1029 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 0, 0),
1030 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 0, 0),
1031 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 0, 0),
1032 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 0, 0),
1033
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001034 _DTV_CMD(DTV_ISDBS_TS_ID, 1, 0),
Andreas Oberritterec05a642011-05-26 07:54:14 -03001035 _DTV_CMD(DTV_DVBT2_PLP_ID, 1, 0),
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001036
Steven Toth6b73eea2008-09-04 01:12:25 -03001037 /* Get */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001038 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1039 _DTV_CMD(DTV_API_VERSION, 0, 0),
1040 _DTV_CMD(DTV_CODE_RATE_HP, 0, 0),
1041 _DTV_CMD(DTV_CODE_RATE_LP, 0, 0),
1042 _DTV_CMD(DTV_GUARD_INTERVAL, 0, 0),
1043 _DTV_CMD(DTV_TRANSMISSION_MODE, 0, 0),
1044 _DTV_CMD(DTV_HIERARCHY, 0, 0),
Manu Abrahamba2780c2011-11-13 18:47:44 -03001045
1046 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -03001047};
1048
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001049static void dtv_property_dump(struct dtv_property *tvp)
Steven Toth6b73eea2008-09-04 01:12:25 -03001050{
1051 int i;
1052
Mauro Carvalho Chehab106ff9e2011-07-15 08:49:06 -03001053 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
Steven Totha1bc84c2008-10-17 00:20:19 -03001054 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
Darron Broad4aae8ef2008-10-03 11:50:00 -03001055 __func__, tvp->cmd);
1056 return;
1057 }
1058
Steven Totha1bc84c2008-10-17 00:20:19 -03001059 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
1060 ,__func__
Steven Toth6b73eea2008-09-04 01:12:25 -03001061 ,tvp->cmd
Steven Toth56f06802008-09-11 10:19:27 -03001062 ,dtv_cmds[ tvp->cmd ].name);
Steven Toth6b73eea2008-09-04 01:12:25 -03001063
Steven Toth56f06802008-09-11 10:19:27 -03001064 if(dtv_cmds[ tvp->cmd ].buffer) {
Steven Toth6b73eea2008-09-04 01:12:25 -03001065
Steven Totha1bc84c2008-10-17 00:20:19 -03001066 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1067 ,__func__
Steven Toth6b73eea2008-09-04 01:12:25 -03001068 ,tvp->u.buffer.len);
1069
1070 for(i = 0; i < tvp->u.buffer.len; i++)
Steven Totha1bc84c2008-10-17 00:20:19 -03001071 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1072 ,__func__
Steven Toth6b73eea2008-09-04 01:12:25 -03001073 ,i
1074 ,tvp->u.buffer.data[i]);
1075
1076 } else
Steven Totha1bc84c2008-10-17 00:20:19 -03001077 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
Steven Toth6b73eea2008-09-04 01:12:25 -03001078}
1079
Steven Tothee33c522008-09-11 23:52:32 -03001080/* Synchronise the legacy tuning parameters into the cache, so that demodulator
1081 * drivers can use a single set_frontend tuning function, regardless of whether
1082 * it's being used for the legacy or new API, reducing code and complexity.
1083 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001084static int dtv_property_cache_sync(struct dvb_frontend *fe,
1085 struct dtv_frontend_properties *c,
1086 const struct dvb_frontend_parameters *p)
Steven Tothee33c522008-09-11 23:52:32 -03001087{
Steven Tothee33c522008-09-11 23:52:32 -03001088 c->frequency = p->frequency;
1089 c->inversion = p->inversion;
1090
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001091 switch (dvbv3_type(c->delivery_system)) {
1092 case DVBV3_QPSK:
1093 dprintk("%s() Preparing QPSK req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001094 c->symbol_rate = p->u.qpsk.symbol_rate;
1095 c->fec_inner = p->u.qpsk.fec_inner;
1096 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001097 case DVBV3_QAM:
1098 dprintk("%s() Preparing QAM req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001099 c->symbol_rate = p->u.qam.symbol_rate;
1100 c->fec_inner = p->u.qam.fec_inner;
1101 c->modulation = p->u.qam.modulation;
1102 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001103 case DVBV3_OFDM:
1104 dprintk("%s() Preparing OFDM req\n", __func__);
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001105 switch (p->u.ofdm.bandwidth) {
1106 case BANDWIDTH_10_MHZ:
1107 c->bandwidth_hz = 10000000;
1108 break;
1109 case BANDWIDTH_8_MHZ:
Steven Toth75b7f942008-09-13 16:56:34 -03001110 c->bandwidth_hz = 8000000;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001111 break;
1112 case BANDWIDTH_7_MHZ:
1113 c->bandwidth_hz = 7000000;
1114 break;
1115 case BANDWIDTH_6_MHZ:
1116 c->bandwidth_hz = 6000000;
1117 break;
1118 case BANDWIDTH_5_MHZ:
1119 c->bandwidth_hz = 5000000;
1120 break;
1121 case BANDWIDTH_1_712_MHZ:
1122 c->bandwidth_hz = 1712000;
1123 break;
1124 case BANDWIDTH_AUTO:
Steven Toth75b7f942008-09-13 16:56:34 -03001125 c->bandwidth_hz = 0;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001126 }
1127
Steven Tothee33c522008-09-11 23:52:32 -03001128 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1129 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1130 c->modulation = p->u.ofdm.constellation;
1131 c->transmission_mode = p->u.ofdm.transmission_mode;
1132 c->guard_interval = p->u.ofdm.guard_interval;
1133 c->hierarchy = p->u.ofdm.hierarchy_information;
1134 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001135 case DVBV3_ATSC:
1136 dprintk("%s() Preparing ATSC req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001137 c->modulation = p->u.vsb.modulation;
Steven Toth80a773c2008-09-12 00:53:50 -03001138 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1139 c->delivery_system = SYS_ATSC;
1140 else
1141 c->delivery_system = SYS_DVBC_ANNEX_B;
Steven Tothee33c522008-09-11 23:52:32 -03001142 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001143 case DVBV3_UNKNOWN:
1144 printk(KERN_ERR
1145 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1146 __func__, c->delivery_system);
1147 return -EINVAL;
Steven Tothee33c522008-09-11 23:52:32 -03001148 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001149
1150 return 0;
Steven Tothee33c522008-09-11 23:52:32 -03001151}
1152
Steven Tothd5748f12008-09-12 00:27:13 -03001153/* Ensure the cached values are set correctly in the frontend
1154 * legacy tuning structures, for the advanced tuning API.
1155 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001156static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001157 struct dvb_frontend_parameters *p)
Steven Toth6b73eea2008-09-04 01:12:25 -03001158{
Andreas Oberritter15cc2bb2011-05-08 20:03:35 -03001159 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001160
Steven Tothd5748f12008-09-12 00:27:13 -03001161 p->frequency = c->frequency;
1162 p->inversion = c->inversion;
1163
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001164 switch (dvbv3_type(c->delivery_system)) {
1165 case DVBV3_UNKNOWN:
1166 printk(KERN_ERR
1167 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1168 __func__, c->delivery_system);
1169 return -EINVAL;
1170 case DVBV3_QPSK:
Steven Totha1bc84c2008-10-17 00:20:19 -03001171 dprintk("%s() Preparing QPSK req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001172 p->u.qpsk.symbol_rate = c->symbol_rate;
1173 p->u.qpsk.fec_inner = c->fec_inner;
1174 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001175 case DVBV3_QAM:
Steven Totha1bc84c2008-10-17 00:20:19 -03001176 dprintk("%s() Preparing QAM req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001177 p->u.qam.symbol_rate = c->symbol_rate;
1178 p->u.qam.fec_inner = c->fec_inner;
1179 p->u.qam.modulation = c->modulation;
1180 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001181 case DVBV3_OFDM:
Steven Totha1bc84c2008-10-17 00:20:19 -03001182 dprintk("%s() Preparing OFDM req\n", __func__);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001183
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001184 switch (c->bandwidth_hz) {
1185 case 10000000:
1186 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1187 break;
1188 case 8000000:
Steven Toth75b7f942008-09-13 16:56:34 -03001189 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001190 break;
1191 case 7000000:
1192 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1193 break;
1194 case 6000000:
1195 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1196 break;
1197 case 5000000:
1198 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1199 break;
1200 case 1712000:
1201 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1202 break;
1203 case 0:
1204 default:
Steven Toth75b7f942008-09-13 16:56:34 -03001205 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001206 }
Steven Tothd5748f12008-09-12 00:27:13 -03001207 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1208 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1209 p->u.ofdm.constellation = c->modulation;
1210 p->u.ofdm.transmission_mode = c->transmission_mode;
1211 p->u.ofdm.guard_interval = c->guard_interval;
1212 p->u.ofdm.hierarchy_information = c->hierarchy;
1213 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001214 case DVBV3_ATSC:
Steven Totha1bc84c2008-10-17 00:20:19 -03001215 dprintk("%s() Preparing VSB req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001216 p->u.vsb.modulation = c->modulation;
1217 break;
1218 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001219 return 0;
Steven Toth6b73eea2008-09-04 01:12:25 -03001220}
1221
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001222/**
1223 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1224 * @fe: struct dvb_frontend pointer
1225 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1226 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1227 *
1228 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1229 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1230 * If p_out is not null, it will update the DVBv3 params pointed by it.
1231 */
1232static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001233 struct dvb_frontend_parameters *p_out)
1234{
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001235 int r;
1236
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001237 if (fe->ops.get_frontend) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001238 r = fe->ops.get_frontend(fe);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001239 if (unlikely(r < 0))
1240 return r;
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001241 if (p_out)
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001242 dtv_property_legacy_params_sync(fe, p_out);
1243 return 0;
1244 }
1245
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001246 /* As everything is in cache, get_frontend fops are always supported */
Mauro Carvalho Chehabb1e9a65012011-12-26 16:43:32 -03001247 return 0;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001248}
1249
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001250static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001251 unsigned int cmd, void *parg);
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001252static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001253 unsigned int cmd, void *parg);
1254
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001255static int dtv_property_process_get(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001256 const struct dtv_frontend_properties *c,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001257 struct dtv_property *tvp,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001258 struct file *file)
Steven Toth363429a2008-09-12 01:34:28 -03001259{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001260 int r, ncaps;
Steven Tothbfbf2da2008-09-12 01:37:37 -03001261
Steven Toth363429a2008-09-12 01:34:28 -03001262 switch(tvp->cmd) {
Manu Abrahamba2780c2011-11-13 18:47:44 -03001263 case DTV_ENUM_DELSYS:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001264 ncaps = 0;
1265 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1266 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1267 ncaps++;
1268 }
1269 tvp->u.buffer.len = ncaps;
Manu Abrahamba2780c2011-11-13 18:47:44 -03001270 break;
Steven Toth363429a2008-09-12 01:34:28 -03001271 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001272 tvp->u.data = c->frequency;
Steven Toth363429a2008-09-12 01:34:28 -03001273 break;
1274 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001275 tvp->u.data = c->modulation;
Steven Toth363429a2008-09-12 01:34:28 -03001276 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001277 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001278 tvp->u.data = c->bandwidth_hz;
Steven Toth363429a2008-09-12 01:34:28 -03001279 break;
1280 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001281 tvp->u.data = c->inversion;
Steven Toth363429a2008-09-12 01:34:28 -03001282 break;
1283 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001284 tvp->u.data = c->symbol_rate;
Steven Toth363429a2008-09-12 01:34:28 -03001285 break;
1286 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001287 tvp->u.data = c->fec_inner;
Steven Toth363429a2008-09-12 01:34:28 -03001288 break;
1289 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001290 tvp->u.data = c->pilot;
Steven Toth363429a2008-09-12 01:34:28 -03001291 break;
1292 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001293 tvp->u.data = c->rolloff;
Steven Toth363429a2008-09-12 01:34:28 -03001294 break;
1295 case DTV_DELIVERY_SYSTEM:
Andreas Oberritter50727712011-05-08 20:03:39 -03001296 tvp->u.data = c->delivery_system;
Steven Toth363429a2008-09-12 01:34:28 -03001297 break;
Steven Toth363429a2008-09-12 01:34:28 -03001298 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001299 tvp->u.data = c->voltage;
Steven Toth363429a2008-09-12 01:34:28 -03001300 break;
1301 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001302 tvp->u.data = c->sectone;
Steven Toth363429a2008-09-12 01:34:28 -03001303 break;
Steven Totheacf8d82008-09-26 00:29:49 -03001304 case DTV_API_VERSION:
1305 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1306 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001307 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001308 tvp->u.data = c->code_rate_HP;
Steven Totha4de91b2008-10-06 21:55:46 -03001309 break;
1310 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001311 tvp->u.data = c->code_rate_LP;
Steven Totha4de91b2008-10-06 21:55:46 -03001312 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001313 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001314 tvp->u.data = c->guard_interval;
Steven Tothb87625f2008-10-06 21:56:59 -03001315 break;
1316 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001317 tvp->u.data = c->transmission_mode;
Steven Tothb87625f2008-10-06 21:56:59 -03001318 break;
Steven Tothef526f42008-10-06 22:01:47 -03001319 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001320 tvp->u.data = c->hierarchy;
Steven Tothef526f42008-10-06 22:01:47 -03001321 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001322
1323 /* ISDB-T Support here */
1324 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001325 tvp->u.data = c->isdbt_partial_reception;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001326 break;
1327 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001328 tvp->u.data = c->isdbt_sb_mode;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001329 break;
1330 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001331 tvp->u.data = c->isdbt_sb_subchannel;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001332 break;
1333 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001334 tvp->u.data = c->isdbt_sb_segment_idx;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001335 break;
1336 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001337 tvp->u.data = c->isdbt_sb_segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001338 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001339 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001340 tvp->u.data = c->isdbt_layer_enabled;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001341 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001342 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001343 tvp->u.data = c->layer[0].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001344 break;
1345 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001346 tvp->u.data = c->layer[0].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001347 break;
1348 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001349 tvp->u.data = c->layer[0].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001350 break;
1351 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001352 tvp->u.data = c->layer[0].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001353 break;
1354 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001355 tvp->u.data = c->layer[1].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001356 break;
1357 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001358 tvp->u.data = c->layer[1].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001359 break;
1360 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001361 tvp->u.data = c->layer[1].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001362 break;
1363 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001364 tvp->u.data = c->layer[1].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001365 break;
1366 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001367 tvp->u.data = c->layer[2].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001368 break;
1369 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001370 tvp->u.data = c->layer[2].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001371 break;
1372 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001373 tvp->u.data = c->layer[2].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001374 break;
1375 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001376 tvp->u.data = c->layer[2].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001377 break;
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001378 case DTV_ISDBS_TS_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001379 tvp->u.data = c->isdbs_ts_id;
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001380 break;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001381 case DTV_DVBT2_PLP_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001382 tvp->u.data = c->dvbt2_plp_id;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001383 break;
Steven Toth363429a2008-09-12 01:34:28 -03001384 default:
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001385 return -EINVAL;
Steven Toth363429a2008-09-12 01:34:28 -03001386 }
1387
Andreas Oberrittere23d9ae2011-05-08 20:03:36 -03001388 /* Allow the frontend to override outgoing properties */
1389 if (fe->ops.get_property) {
1390 r = fe->ops.get_property(fe, tvp);
1391 if (r < 0)
1392 return r;
1393 }
1394
Mauro Carvalho Chehab639544d2009-12-31 17:49:32 -03001395 dtv_property_dump(tvp);
1396
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001397 return 0;
Steven Toth363429a2008-09-12 01:34:28 -03001398}
1399
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001400static int dtv_set_frontend(struct dvb_frontend *fe);
1401
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001402static bool is_dvbv3_delsys(u32 delsys)
1403{
1404 bool status;
1405
1406 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1407 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1408
1409 return status;
1410}
1411
1412static int set_delivery_system(struct dvb_frontend *fe, u32 desired_system)
1413{
1414 int ncaps, i;
1415 u32 delsys = SYS_UNDEFINED;
1416 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1417 enum dvbv3_emulation_type type;
1418
Mauro Carvalho Chehabf4b19522012-01-13 11:09:23 -02001419 /*
1420 * It was reported that some old DVBv5 applications were
1421 * filling delivery_system with SYS_UNDEFINED. If this happens,
1422 * assume that the application wants to use the first supported
1423 * delivery system.
1424 */
1425 if (c->delivery_system == SYS_UNDEFINED)
1426 c->delivery_system = fe->ops.delsys[0];
1427
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001428 if (desired_system == SYS_UNDEFINED) {
1429 /*
1430 * A DVBv3 call doesn't know what's the desired system.
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001431 * Also, DVBv3 applications don't know that ops.info->type
1432 * could be changed, and they simply dies when it doesn't
1433 * match.
1434 * So, don't change the current delivery system, as it
1435 * may be trying to do the wrong thing, like setting an
1436 * ISDB-T frontend as DVB-T. Instead, find the closest
1437 * DVBv3 system that matches the delivery system.
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001438 */
1439 if (is_dvbv3_delsys(c->delivery_system)) {
1440 dprintk("%s() Using delivery system to %d\n",
1441 __func__, c->delivery_system);
1442 return 0;
1443 }
1444 type = dvbv3_type(c->delivery_system);
1445 switch (type) {
1446 case DVBV3_QPSK:
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001447 desired_system = SYS_DVBS;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001448 break;
1449 case DVBV3_QAM:
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001450 desired_system = SYS_DVBC_ANNEX_A;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001451 break;
1452 case DVBV3_ATSC:
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001453 desired_system = SYS_ATSC;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001454 break;
1455 case DVBV3_OFDM:
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001456 desired_system = SYS_DVBT;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001457 break;
1458 default:
1459 dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
1460 __func__);
1461 return -EINVAL;
1462 }
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001463 } else {
1464 /*
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001465 * This is a DVBv5 call. So, it likely knows the supported
1466 * delivery systems.
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001467 */
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001468
1469 /* Check if the desired delivery system is supported */
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001470 ncaps = 0;
1471 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1472 if (fe->ops.delsys[ncaps] == desired_system) {
1473 c->delivery_system = desired_system;
1474 dprintk("%s() Changing delivery system to %d\n",
1475 __func__, desired_system);
1476 return 0;
1477 }
Mauro Carvalho Chehab6c4b78c2012-01-05 11:24:41 -02001478 ncaps++;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001479 }
1480 type = dvbv3_type(desired_system);
1481
1482 /*
1483 * The delivery system is not supported. See if it can be
1484 * emulated.
1485 * The emulation only works if the desired system is one of the
1486 * DVBv3 delivery systems
1487 */
1488 if (!is_dvbv3_delsys(desired_system)) {
1489 dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1490 __func__);
1491 return -EINVAL;
1492 }
1493
1494 /*
1495 * Get the last non-DVBv3 delivery system that has the same type
1496 * of the desired system
1497 */
1498 ncaps = 0;
1499 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1500 if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
1501 !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
1502 delsys = fe->ops.delsys[ncaps];
1503 ncaps++;
1504 }
1505 /* There's nothing compatible with the desired delivery system */
1506 if (delsys == SYS_UNDEFINED) {
1507 dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1508 __func__);
1509 return -EINVAL;
1510 }
1511 c->delivery_system = delsys;
1512 }
1513
1514 /*
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001515 * The DVBv3 or DVBv5 call is requesting a different system. So,
1516 * emulation is needed.
1517 *
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001518 * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
1519 * for older DVBv5 applications. The emulation will try to use
1520 * the auto mode for most things, and will assume that the desired
1521 * delivery system is the last one at the ops.delsys[] array
1522 */
1523 dprintk("%s() Using delivery system %d emulated as if it were a %d\n",
1524 __func__, delsys, desired_system);
1525
1526 /*
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -03001527 * For now, handles ISDB-T calls. More code may be needed here for the
1528 * other emulated stuff
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001529 */
1530 if (type == DVBV3_OFDM) {
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -03001531 if (c->delivery_system == SYS_ISDBT) {
1532 dprintk("%s() Using defaults for SYS_ISDBT\n",
1533 __func__);
1534 if (!c->bandwidth_hz)
1535 c->bandwidth_hz = 6000000;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001536
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -03001537 c->isdbt_partial_reception = 0;
1538 c->isdbt_sb_mode = 0;
1539 c->isdbt_sb_subchannel = 0;
1540 c->isdbt_sb_segment_idx = 0;
1541 c->isdbt_sb_segment_count = 0;
1542 c->isdbt_layer_enabled = 0;
1543 for (i = 0; i < 3; i++) {
1544 c->layer[i].fec = FEC_AUTO;
1545 c->layer[i].modulation = QAM_AUTO;
1546 c->layer[i].interleaving = 0;
1547 c->layer[i].segment_count = 0;
1548 }
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001549 }
1550 }
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001551 dprintk("change delivery system on cache to %d\n", c->delivery_system);
1552
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001553 return 0;
1554}
1555
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001556static int dtv_property_process_set(struct dvb_frontend *fe,
1557 struct dtv_property *tvp,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001558 struct file *file)
Steven Toth6b73eea2008-09-04 01:12:25 -03001559{
1560 int r = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -03001561 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001562
Steven Tothbfbf2da2008-09-12 01:37:37 -03001563 /* Allow the frontend to validate incoming properties */
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001564 if (fe->ops.set_property) {
Steven Tothbfbf2da2008-09-12 01:37:37 -03001565 r = fe->ops.set_property(fe, tvp);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001566 if (r < 0)
1567 return r;
1568 }
Steven Tothbfbf2da2008-09-12 01:37:37 -03001569
Steven Toth6b73eea2008-09-04 01:12:25 -03001570 switch(tvp->cmd) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001571 case DTV_CLEAR:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001572 /*
1573 * Reset a cache of data specific to the frontend here. This does
Steven Toth6b73eea2008-09-04 01:12:25 -03001574 * not effect hardware.
1575 */
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001576 dvb_frontend_clear_cache(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001577 break;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001578 case DTV_TUNE:
Steven Toth6b73eea2008-09-04 01:12:25 -03001579 /* interpret the cache of data, build either a traditional frontend
Steven Tothd5748f12008-09-12 00:27:13 -03001580 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1581 * ioctl.
Steven Toth6b73eea2008-09-04 01:12:25 -03001582 */
Andreas Oberritter50727712011-05-08 20:03:39 -03001583 c->state = tvp->cmd;
Steven Totha1bc84c2008-10-17 00:20:19 -03001584 dprintk("%s() Finalised property cache\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001585
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001586 r = dtv_set_frontend(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001587 break;
Steven Toth363429a2008-09-12 01:34:28 -03001588 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001589 c->frequency = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001590 break;
Steven Toth363429a2008-09-12 01:34:28 -03001591 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001592 c->modulation = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001593 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001594 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001595 c->bandwidth_hz = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001596 break;
Steven Toth363429a2008-09-12 01:34:28 -03001597 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001598 c->inversion = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001599 break;
Steven Toth363429a2008-09-12 01:34:28 -03001600 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001601 c->symbol_rate = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001602 break;
Steven Toth363429a2008-09-12 01:34:28 -03001603 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001604 c->fec_inner = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001605 break;
Steven Toth363429a2008-09-12 01:34:28 -03001606 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001607 c->pilot = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001608 break;
Steven Toth363429a2008-09-12 01:34:28 -03001609 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001610 c->rolloff = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001611 break;
Steven Toth363429a2008-09-12 01:34:28 -03001612 case DTV_DELIVERY_SYSTEM:
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001613 r = set_delivery_system(fe, tvp->u.data);
Steven Toth6b73eea2008-09-04 01:12:25 -03001614 break;
Steven Toth363429a2008-09-12 01:34:28 -03001615 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001616 c->voltage = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001617 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001618 (void *)c->voltage);
Steven Toth13c97bf2008-09-04 21:19:43 -03001619 break;
Steven Toth363429a2008-09-12 01:34:28 -03001620 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001621 c->sectone = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001622 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001623 (void *)c->sectone);
Steven Toth13c97bf2008-09-04 21:19:43 -03001624 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001625 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001626 c->code_rate_HP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001627 break;
1628 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001629 c->code_rate_LP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001630 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001631 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001632 c->guard_interval = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001633 break;
1634 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001635 c->transmission_mode = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001636 break;
Steven Tothef526f42008-10-06 22:01:47 -03001637 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001638 c->hierarchy = tvp->u.data;
Steven Tothef526f42008-10-06 22:01:47 -03001639 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001640
1641 /* ISDB-T Support here */
1642 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001643 c->isdbt_partial_reception = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001644 break;
1645 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001646 c->isdbt_sb_mode = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001647 break;
1648 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001649 c->isdbt_sb_subchannel = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001650 break;
1651 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001652 c->isdbt_sb_segment_idx = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001653 break;
1654 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001655 c->isdbt_sb_segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001656 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001657 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001658 c->isdbt_layer_enabled = tvp->u.data;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001659 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001660 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001661 c->layer[0].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001662 break;
1663 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001664 c->layer[0].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001665 break;
1666 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001667 c->layer[0].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001668 break;
1669 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001670 c->layer[0].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001671 break;
1672 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001673 c->layer[1].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001674 break;
1675 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001676 c->layer[1].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001677 break;
1678 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001679 c->layer[1].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001680 break;
1681 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001682 c->layer[1].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001683 break;
1684 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001685 c->layer[2].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001686 break;
1687 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001688 c->layer[2].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001689 break;
1690 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001691 c->layer[2].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001692 break;
1693 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001694 c->layer[2].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001695 break;
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001696 case DTV_ISDBS_TS_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001697 c->isdbs_ts_id = tvp->u.data;
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001698 break;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001699 case DTV_DVBT2_PLP_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001700 c->dvbt2_plp_id = tvp->u.data;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001701 break;
Steven Toth363429a2008-09-12 01:34:28 -03001702 default:
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001703 return -EINVAL;
Steven Toth6b73eea2008-09-04 01:12:25 -03001704 }
1705
Steven Toth13c97bf2008-09-04 21:19:43 -03001706 return r;
Steven Toth6b73eea2008-09-04 01:12:25 -03001707}
1708
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001709static int dvb_frontend_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 unsigned int cmd, void *parg)
1711{
1712 struct dvb_device *dvbdev = file->private_data;
1713 struct dvb_frontend *fe = dvbdev->priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001714 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001715 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int err = -EOPNOTSUPP;
1717
Mauro Carvalho Chehabfb8253b2009-12-31 18:14:54 -03001718 dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
matthieu castete36309f2010-05-05 15:59:20 -03001720 if (fepriv->exit != DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return -ENODEV;
1722
1723 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1724 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1725 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1726 return -EPERM;
1727
1728 if (down_interruptible (&fepriv->sem))
1729 return -ERESTARTSYS;
1730
Steven Toth13c97bf2008-09-04 21:19:43 -03001731 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001732 err = dvb_frontend_ioctl_properties(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001733 else {
Andreas Oberritter50727712011-05-08 20:03:39 -03001734 c->state = DTV_UNDEFINED;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001735 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001736 }
Steven Toth13c97bf2008-09-04 21:19:43 -03001737
1738 up(&fepriv->sem);
1739 return err;
1740}
1741
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001742static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001743 unsigned int cmd, void *parg)
1744{
1745 struct dvb_device *dvbdev = file->private_data;
1746 struct dvb_frontend *fe = dvbdev->priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001747 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Igor M. Liplianine6f9ec82008-09-12 14:49:06 -03001748 int err = 0;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001749
1750 struct dtv_properties *tvps = NULL;
1751 struct dtv_property *tvp = NULL;
1752 int i;
Steven Toth13c97bf2008-09-04 21:19:43 -03001753
1754 dprintk("%s\n", __func__);
1755
Steven Toth6b73eea2008-09-04 01:12:25 -03001756 if(cmd == FE_SET_PROPERTY) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001757 tvps = (struct dtv_properties __user *)parg;
Steven Toth6b73eea2008-09-04 01:12:25 -03001758
Steven Totha1bc84c2008-10-17 00:20:19 -03001759 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1760 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001761
1762 /* Put an arbitrary limit on the number of messages that can
1763 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001764 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Tothe7fee0f32008-09-11 10:23:01 -03001765 return -EINVAL;
1766
Thiago Farina6e3924a2010-01-16 10:25:38 -03001767 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001768 if (!tvp) {
1769 err = -ENOMEM;
1770 goto out;
Steven Toth6b73eea2008-09-04 01:12:25 -03001771 }
1772
Steven Tothe7fee0f32008-09-11 10:23:01 -03001773 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1774 err = -EFAULT;
1775 goto out;
1776 }
1777
Steven Tothd48cb402008-09-26 00:16:25 -03001778 for (i = 0; i < tvps->num; i++) {
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001779 err = dtv_property_process_set(fe, tvp + i, file);
1780 if (err < 0)
1781 goto out;
1782 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03001783 }
Steven Tothe7fee0f32008-09-11 10:23:01 -03001784
Andreas Oberritter50727712011-05-08 20:03:39 -03001785 if (c->state == DTV_TUNE)
Steven Totha1bc84c2008-10-17 00:20:19 -03001786 dprintk("%s() Property cache is full, tuning\n", __func__);
Steven Tothbfbf2da2008-09-12 01:37:37 -03001787
Steven Toth363429a2008-09-12 01:34:28 -03001788 } else
1789 if(cmd == FE_GET_PROPERTY) {
Steven Toth363429a2008-09-12 01:34:28 -03001790 tvps = (struct dtv_properties __user *)parg;
1791
Steven Totha1bc84c2008-10-17 00:20:19 -03001792 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1793 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
Steven Toth363429a2008-09-12 01:34:28 -03001794
1795 /* Put an arbitrary limit on the number of messages that can
1796 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001797 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Toth363429a2008-09-12 01:34:28 -03001798 return -EINVAL;
1799
Thiago Farina6e3924a2010-01-16 10:25:38 -03001800 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Toth363429a2008-09-12 01:34:28 -03001801 if (!tvp) {
1802 err = -ENOMEM;
1803 goto out;
1804 }
1805
1806 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1807 err = -EFAULT;
1808 goto out;
1809 }
1810
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001811 /*
1812 * Fills the cache out struct with the cache contents, plus
Mauro Carvalho Chehabb1e9a65012011-12-26 16:43:32 -03001813 * the data retrieved from get_frontend.
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001814 */
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001815 dtv_get_frontend(fe, NULL);
Steven Tothd48cb402008-09-26 00:16:25 -03001816 for (i = 0; i < tvps->num; i++) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001817 err = dtv_property_process_get(fe, c, tvp + i, file);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001818 if (err < 0)
1819 goto out;
1820 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03001821 }
Steven Toth363429a2008-09-12 01:34:28 -03001822
1823 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1824 err = -EFAULT;
1825 goto out;
1826 }
1827
Steven Toth363429a2008-09-12 01:34:28 -03001828 } else
1829 err = -EOPNOTSUPP;
1830
Steven Tothe7fee0f32008-09-11 10:23:01 -03001831out:
1832 kfree(tvp);
Steven Toth13c97bf2008-09-04 21:19:43 -03001833 return err;
1834}
1835
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001836static int dtv_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001837{
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001838 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1839 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1840 struct dvb_frontend_tune_settings fetunesettings;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001841 u32 rolloff = 0;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001842
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001843 if (dvb_frontend_check_parameters(fe) < 0)
1844 return -EINVAL;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001845
1846 /*
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001847 * Be sure that the bandwidth will be filled for all
1848 * non-satellite systems, as tuners need to know what
1849 * low pass/Nyquist half filter should be applied, in
1850 * order to avoid inter-channel noise.
1851 *
1852 * ISDB-T and DVB-T/T2 already sets bandwidth.
1853 * ATSC and DVB-C don't set, so, the core should fill it.
1854 *
1855 * On DVB-C Annex A and C, the bandwidth is a function of
1856 * the roll-off and symbol rate. Annex B defines different
1857 * roll-off factors depending on the modulation. Fortunately,
1858 * Annex B is only used with 6MHz, so there's no need to
1859 * calculate it.
1860 *
1861 * While not officially supported, a side effect of handling it at
1862 * the cache level is that a program could retrieve the bandwidth
1863 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1864 */
1865 switch (c->delivery_system) {
1866 case SYS_ATSC:
1867 case SYS_DVBC_ANNEX_B:
1868 c->bandwidth_hz = 6000000;
1869 break;
1870 case SYS_DVBC_ANNEX_A:
1871 rolloff = 115;
1872 break;
1873 case SYS_DVBC_ANNEX_C:
1874 rolloff = 113;
1875 break;
1876 default:
1877 break;
1878 }
1879 if (rolloff)
1880 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001881
1882 /* force auto frequency inversion if requested */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001883 if (dvb_force_auto_inversion)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001884 c->inversion = INVERSION_AUTO;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001885
1886 /*
1887 * without hierarchical coding code_rate_LP is irrelevant,
1888 * so we tolerate the otherwise invalid FEC_NONE setting
1889 */
1890 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
1891 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001892
1893 /* get frontend-specific tuning settings */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001894 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001895 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1896 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1897 fepriv->max_drift = fetunesettings.max_drift;
1898 fepriv->step_size = fetunesettings.step_size;
1899 } else {
1900 /* default values */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001901 switch (c->delivery_system) {
1902 case SYS_DVBC_ANNEX_A:
1903 case SYS_DVBC_ANNEX_C:
1904 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001905 fepriv->step_size = c->symbol_rate / 16000;
1906 fepriv->max_drift = c->symbol_rate / 2000;
1907 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001908 case SYS_DVBT:
1909 case SYS_DVBT2:
1910 case SYS_ISDBT:
1911 case SYS_DMBTH:
1912 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001913 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1914 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1915 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001916 default:
1917 /*
1918 * FIXME: This sounds wrong! if freqency_stepsize is
1919 * defined by the frontend, why not use it???
1920 */
1921 fepriv->min_delay = HZ / 20;
1922 fepriv->step_size = 0; /* no zigzag */
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001923 fepriv->max_drift = 0;
1924 break;
1925 }
1926 }
1927 if (dvb_override_tune_delay > 0)
1928 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1929
1930 fepriv->state = FESTATE_RETUNE;
1931
1932 /* Request the search algorithm to search */
1933 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1934
1935 dvb_frontend_clear_events(fe);
1936 dvb_frontend_add_event(fe, 0);
1937 dvb_frontend_wakeup(fe);
1938 fepriv->status = 0;
1939
1940 return 0;
1941}
1942
1943
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001944static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001945 unsigned int cmd, void *parg)
1946{
1947 struct dvb_device *dvbdev = file->private_data;
1948 struct dvb_frontend *fe = dvbdev->priv;
1949 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001950 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Michael Krufky9133aee2009-05-23 18:00:59 -03001951 int cb_err, err = -EOPNOTSUPP;
1952
1953 if (fe->dvb->fe_ioctl_override) {
1954 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1955 DVB_FE_IOCTL_PRE);
1956 if (cb_err < 0)
1957 return cb_err;
1958 if (cb_err > 0)
1959 return 0;
1960 /* fe_ioctl_override returning 0 allows
1961 * dvb-core to continue handling the ioctl */
1962 }
Steven Toth13c97bf2008-09-04 21:19:43 -03001963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 switch (cmd) {
1965 case FE_GET_INFO: {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001966 struct dvb_frontend_info* info = parg;
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001967
Patrick Boettcherdea74862006-05-14 05:01:31 -03001968 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
Guillaume Audirac2030c032010-05-06 09:30:25 -03001969 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001971 /*
1972 * Associate the 4 delivery systems supported by DVBv3
1973 * API with their DVBv5 counterpart. For the other standards,
1974 * use the closest type, assuming that it would hopefully
1975 * work with a DVBv3 application.
1976 * It should be noticed that, on multi-frontend devices with
1977 * different types (terrestrial and cable, for example),
1978 * a pure DVBv3 application won't be able to use all delivery
1979 * systems. Yet, changing the DVBv5 cache to the other delivery
1980 * system should be enough for making it work.
1981 */
1982 switch (dvbv3_type(c->delivery_system)) {
1983 case DVBV3_QPSK:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001984 info->type = FE_QPSK;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001985 break;
1986 case DVBV3_ATSC:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001987 info->type = FE_ATSC;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001988 break;
1989 case DVBV3_QAM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001990 info->type = FE_QAM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001991 break;
1992 case DVBV3_OFDM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001993 info->type = FE_OFDM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001994 break;
1995 default:
1996 printk(KERN_ERR
1997 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1998 __func__, c->delivery_system);
1999 fe->ops.info.type = FE_OFDM;
2000 }
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02002001 dprintk("current delivery system on cache: %d, V3 type: %d\n",
2002 c->delivery_system, fe->ops.info.type);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2005 * do it, it is done for it. */
2006 info->caps |= FE_CAN_INVERSION_AUTO;
2007 err = 0;
2008 break;
2009 }
2010
Peter Beutner6757ccc2005-07-07 17:57:36 -07002011 case FE_READ_STATUS: {
2012 fe_status_t* status = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002014 /* if retune was requested but hasn't occurred yet, prevent
Peter Beutner6757ccc2005-07-07 17:57:36 -07002015 * that user get signal state from previous tuning */
Janne Grunau01886252009-09-01 19:23:09 -03002016 if (fepriv->state == FESTATE_RETUNE ||
2017 fepriv->state == FESTATE_ERROR) {
Peter Beutner6757ccc2005-07-07 17:57:36 -07002018 err=0;
2019 *status = 0;
2020 break;
2021 }
2022
Patrick Boettcherdea74862006-05-14 05:01:31 -03002023 if (fe->ops.read_status)
2024 err = fe->ops.read_status(fe, status);
Peter Beutner6757ccc2005-07-07 17:57:36 -07002025 break;
2026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 case FE_READ_BER:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002028 if (fe->ops.read_ber)
2029 err = fe->ops.read_ber(fe, (__u32*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 break;
2031
2032 case FE_READ_SIGNAL_STRENGTH:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002033 if (fe->ops.read_signal_strength)
2034 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 break;
2036
2037 case FE_READ_SNR:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002038 if (fe->ops.read_snr)
2039 err = fe->ops.read_snr(fe, (__u16*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 break;
2041
2042 case FE_READ_UNCORRECTED_BLOCKS:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002043 if (fe->ops.read_ucblocks)
2044 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 break;
2046
2047
2048 case FE_DISEQC_RESET_OVERLOAD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002049 if (fe->ops.diseqc_reset_overload) {
2050 err = fe->ops.diseqc_reset_overload(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 fepriv->state = FESTATE_DISEQC;
2052 fepriv->status = 0;
2053 }
2054 break;
2055
2056 case FE_DISEQC_SEND_MASTER_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002057 if (fe->ops.diseqc_send_master_cmd) {
2058 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 fepriv->state = FESTATE_DISEQC;
2060 fepriv->status = 0;
2061 }
2062 break;
2063
2064 case FE_DISEQC_SEND_BURST:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002065 if (fe->ops.diseqc_send_burst) {
2066 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 fepriv->state = FESTATE_DISEQC;
2068 fepriv->status = 0;
2069 }
2070 break;
2071
2072 case FE_SET_TONE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002073 if (fe->ops.set_tone) {
2074 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03002075 fepriv->tone = (fe_sec_tone_mode_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 fepriv->state = FESTATE_DISEQC;
2077 fepriv->status = 0;
2078 }
2079 break;
2080
2081 case FE_SET_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002082 if (fe->ops.set_voltage) {
2083 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03002084 fepriv->voltage = (fe_sec_voltage_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 fepriv->state = FESTATE_DISEQC;
2086 fepriv->status = 0;
2087 }
2088 break;
2089
2090 case FE_DISHNETWORK_SEND_LEGACY_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002091 if (fe->ops.dishnetwork_send_legacy_command) {
2092 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 fepriv->state = FESTATE_DISEQC;
2094 fepriv->status = 0;
Patrick Boettcherdea74862006-05-14 05:01:31 -03002095 } else if (fe->ops.set_voltage) {
NooneImportant83b75b02005-11-08 21:35:27 -08002096 /*
2097 * NOTE: This is a fallback condition. Some frontends
2098 * (stv0299 for instance) take longer than 8msec to
2099 * respond to a set_voltage command. Those switches
2100 * need custom routines to switch properly. For all
Guillaume Audirac2030c032010-05-06 09:30:25 -03002101 * other frontends, the following should work ok.
NooneImportant83b75b02005-11-08 21:35:27 -08002102 * Dish network legacy switches (as used by Dish500)
2103 * are controlled by sending 9-bit command words
2104 * spaced 8msec apart.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002105 * the actual command word is switch/port dependent
NooneImportant83b75b02005-11-08 21:35:27 -08002106 * so it is up to the userspace application to send
2107 * the right command.
2108 * The command must always start with a '0' after
2109 * initialization, so parg is 8 bits and does not
2110 * include the initialization or start bit
2111 */
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002112 unsigned long swcmd = ((unsigned long) parg) << 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002113 struct timeval nexttime;
2114 struct timeval tv[10];
2115 int i;
2116 u8 last = 1;
2117 if (dvb_frontend_debug)
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002118 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
NooneImportant83b75b02005-11-08 21:35:27 -08002119 do_gettimeofday(&nexttime);
2120 if (dvb_frontend_debug)
2121 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
2122 /* before sending a command, initialize by sending
2123 * a 32ms 18V to the switch
2124 */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002125 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002126 dvb_frontend_sleep_until(&nexttime, 32000);
2127
2128 for (i = 0; i < 9; i++) {
2129 if (dvb_frontend_debug)
2130 do_gettimeofday(&tv[i + 1]);
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002131 if ((swcmd & 0x01) != last) {
NooneImportant83b75b02005-11-08 21:35:27 -08002132 /* set voltage to (last ? 13V : 18V) */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002133 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002134 last = (last) ? 0 : 1;
2135 }
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002136 swcmd = swcmd >> 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002137 if (i != 8)
2138 dvb_frontend_sleep_until(&nexttime, 8000);
2139 }
2140 if (dvb_frontend_debug) {
2141 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002142 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -08002143 for (i = 1; i < 10; i++)
2144 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2145 }
2146 err = 0;
2147 fepriv->state = FESTATE_DISEQC;
2148 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150 break;
2151
2152 case FE_DISEQC_RECV_SLAVE_REPLY:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002153 if (fe->ops.diseqc_recv_slave_reply)
2154 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 break;
2156
2157 case FE_ENABLE_HIGH_LNB_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002158 if (fe->ops.enable_high_lnb_voltage)
2159 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 break;
2161
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002162 case FE_SET_FRONTEND:
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03002163 err = set_delivery_system(fe, SYS_UNDEFINED);
2164 if (err)
2165 break;
2166
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002167 err = dtv_property_cache_sync(fe, c, parg);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002168 if (err)
2169 break;
2170 err = dtv_set_frontend(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 case FE_GET_EVENT:
2173 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2174 break;
2175
2176 case FE_GET_FRONTEND:
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002177 err = dtv_get_frontend(fe, parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 break;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002179
2180 case FE_SET_FRONTEND_TUNE_MODE:
Hans Verkuile18828e2006-01-09 15:25:28 -02002181 fepriv->tune_mode_flags = (unsigned long) parg;
Trent Piepho1b172e02006-06-29 13:16:04 -03002182 err = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002183 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 };
2185
Michael Krufky9133aee2009-05-23 18:00:59 -03002186 if (fe->dvb->fe_ioctl_override) {
2187 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
2188 DVB_FE_IOCTL_POST);
2189 if (cb_err < 0)
2190 return cb_err;
2191 }
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return err;
2194}
2195
Steven Toth6b73eea2008-09-04 01:12:25 -03002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2198{
2199 struct dvb_device *dvbdev = file->private_data;
2200 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002201 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002203 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 poll_wait (file, &fepriv->events.wait_queue, wait);
2206
2207 if (fepriv->events.eventw != fepriv->events.eventr)
2208 return (POLLIN | POLLRDNORM | POLLPRI);
2209
2210 return 0;
2211}
2212
2213static int dvb_frontend_open(struct inode *inode, struct file *file)
2214{
2215 struct dvb_device *dvbdev = file->private_data;
2216 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002217 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Darron Broad59b18422008-10-11 11:44:05 -03002218 struct dvb_adapter *adapter = fe->dvb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 int ret;
2220
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002221 dprintk ("%s\n", __func__);
matthieu castete36309f2010-05-05 15:59:20 -03002222 if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2223 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Darron Broad59b18422008-10-11 11:44:05 -03002225 if (adapter->mfe_shared) {
2226 mutex_lock (&adapter->mfe_lock);
Darron Broad65946902008-10-15 13:37:59 -03002227
2228 if (adapter->mfe_dvbdev == NULL)
2229 adapter->mfe_dvbdev = dvbdev;
2230
2231 else if (adapter->mfe_dvbdev != dvbdev) {
2232 struct dvb_device
2233 *mfedev = adapter->mfe_dvbdev;
2234 struct dvb_frontend
2235 *mfe = mfedev->priv;
2236 struct dvb_frontend_private
2237 *mfepriv = mfe->frontend_priv;
2238 int mferetry = (dvb_mfe_wait_time << 1);
2239
2240 mutex_unlock (&adapter->mfe_lock);
2241 while (mferetry-- && (mfedev->users != -1 ||
2242 mfepriv->thread != NULL)) {
2243 if(msleep_interruptible(500)) {
2244 if(signal_pending(current))
2245 return -EINTR;
Darron Broad59b18422008-10-11 11:44:05 -03002246 }
2247 }
Darron Broad65946902008-10-15 13:37:59 -03002248
2249 mutex_lock (&adapter->mfe_lock);
2250 if(adapter->mfe_dvbdev != dvbdev) {
2251 mfedev = adapter->mfe_dvbdev;
2252 mfe = mfedev->priv;
2253 mfepriv = mfe->frontend_priv;
2254 if (mfedev->users != -1 ||
2255 mfepriv->thread != NULL) {
2256 mutex_unlock (&adapter->mfe_lock);
2257 return -EBUSY;
2258 }
2259 adapter->mfe_dvbdev = dvbdev;
2260 }
Darron Broad59b18422008-10-11 11:44:05 -03002261 }
2262 }
2263
Oliver Endriss48136e12007-08-25 12:00:23 -03002264 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2265 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
Darron Broad59b18422008-10-11 11:44:05 -03002266 goto err0;
Devin Heitmueller2d196932011-07-04 21:55:01 -03002267
2268 /* If we took control of the bus, we need to force
2269 reinitialization. This is because many ts_bus_ctrl()
2270 functions strobe the RESET pin on the demod, and if the
2271 frontend thread already exists then the dvb_init() routine
2272 won't get called (which is what usually does initial
2273 register configuration). */
2274 fepriv->reinitialise = 1;
Steven Tothba7e6f32006-09-25 12:41:53 -03002275 }
2276
Oliver Endriss48136e12007-08-25 12:00:23 -03002277 if ((ret = dvb_generic_open (inode, file)) < 0)
2278 goto err1;
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002279
Oliver Endriss48136e12007-08-25 12:00:23 -03002280 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002281 /* normal tune mode when opened R/W */
2282 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2283 fepriv->tone = -1;
2284 fepriv->voltage = -1;
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 ret = dvb_frontend_start (fe);
2287 if (ret)
Oliver Endriss48136e12007-08-25 12:00:23 -03002288 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 /* empty event queue */
2291 fepriv->events.eventr = fepriv->events.eventw = 0;
2292 }
2293
Darron Broad59b18422008-10-11 11:44:05 -03002294 if (adapter->mfe_shared)
2295 mutex_unlock (&adapter->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return ret;
Oliver Endriss48136e12007-08-25 12:00:23 -03002297
2298err2:
2299 dvb_generic_release(inode, file);
2300err1:
2301 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2302 fe->ops.ts_bus_ctrl(fe, 0);
Darron Broad59b18422008-10-11 11:44:05 -03002303err0:
2304 if (adapter->mfe_shared)
2305 mutex_unlock (&adapter->mfe_lock);
Oliver Endriss48136e12007-08-25 12:00:23 -03002306 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
2309static int dvb_frontend_release(struct inode *inode, struct file *file)
2310{
2311 struct dvb_device *dvbdev = file->private_data;
2312 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002313 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002314 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002316 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002318 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 fepriv->release_jiffies = jiffies;
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002320 mb();
2321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002323 ret = dvb_generic_release (inode, file);
2324
Oliver Endriss48136e12007-08-25 12:00:23 -03002325 if (dvbdev->users == -1) {
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002326 wake_up(&fepriv->wait_queue);
matthieu castete36309f2010-05-05 15:59:20 -03002327 if (fepriv->exit != DVB_FE_NO_EXIT) {
Oliver Endriss48136e12007-08-25 12:00:23 -03002328 fops_put(file->f_op);
2329 file->f_op = NULL;
2330 wake_up(&dvbdev->wait_queue);
2331 }
2332 if (fe->ops.ts_bus_ctrl)
2333 fe->ops.ts_bus_ctrl(fe, 0);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002334 }
Oliver Endriss48136e12007-08-25 12:00:23 -03002335
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
Jan Engelhardt784e29d2009-01-11 06:12:43 -03002339static const struct file_operations dvb_frontend_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 .owner = THIS_MODULE,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002341 .unlocked_ioctl = dvb_generic_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 .poll = dvb_frontend_poll,
2343 .open = dvb_frontend_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002344 .release = dvb_frontend_release,
2345 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346};
2347
2348int dvb_register_frontend(struct dvb_adapter* dvb,
2349 struct dvb_frontend* fe)
2350{
2351 struct dvb_frontend_private *fepriv;
2352 static const struct dvb_device dvbdev_template = {
2353 .users = ~0,
2354 .writers = 1,
2355 .readers = (~0)-1,
2356 .fops = &dvb_frontend_fops,
2357 .kernel_ioctl = dvb_frontend_ioctl
2358 };
2359
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002360 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Ingo Molnar3593cab2006-02-07 06:49:14 -02002362 if (mutex_lock_interruptible(&frontend_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return -ERESTARTSYS;
2364
Panagiotis Issaris74081872006-01-11 19:40:56 -02002365 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (fe->frontend_priv == NULL) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002367 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return -ENOMEM;
2369 }
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002370 fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Thomas Gleixnera0a47142010-09-07 11:33:27 -03002372 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 init_waitqueue_head (&fepriv->wait_queue);
2374 init_waitqueue_head (&fepriv->events.wait_queue);
Matthias Kaehlcke03b76122007-07-30 14:58:10 -03002375 mutex_init(&fepriv->events.mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 fe->dvb = dvb;
2377 fepriv->inversion = INVERSION_OFF;
2378
Steven Toth363c35f2008-10-11 11:05:50 -03002379 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 fe->dvb->num,
Steven Toth363c35f2008-10-11 11:05:50 -03002381 fe->id,
Patrick Boettcherdea74862006-05-14 05:01:31 -03002382 fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2385 fe, DVB_DEVICE_FRONTEND);
2386
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002387 /*
2388 * Initialize the cache to the proper values according with the
2389 * first supported delivery system (ops->delsys[0])
2390 */
Mauro Carvalho Chehab240ab502012-01-10 18:00:50 -03002391
2392 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002393 dvb_frontend_clear_cache(fe);
2394
Ingo Molnar3593cab2006-02-07 06:49:14 -02002395 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return 0;
2397}
2398EXPORT_SYMBOL(dvb_register_frontend);
2399
2400int dvb_unregister_frontend(struct dvb_frontend* fe)
2401{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002402 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002403 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Markus Rechberger57861b42007-04-14 10:19:18 -03002405 mutex_lock(&frontend_mutex);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002406 dvb_frontend_stop (fe);
Markus Rechberger57861b42007-04-14 10:19:18 -03002407 mutex_unlock(&frontend_mutex);
2408
2409 if (fepriv->dvbdev->users < -1)
2410 wait_event(fepriv->dvbdev->wait_queue,
2411 fepriv->dvbdev->users==-1);
2412
Ingo Molnar3593cab2006-02-07 06:49:14 -02002413 mutex_lock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 dvb_unregister_device (fepriv->dvbdev);
Andrew de Quinceyd9955062006-08-08 09:10:08 -03002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 /* fe is invalid now */
2417 kfree(fepriv);
Ingo Molnar3593cab2006-02-07 06:49:14 -02002418 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return 0;
2420}
2421EXPORT_SYMBOL(dvb_unregister_frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002422
Mauro Carvalho Chehab149ef722008-04-29 21:38:46 -03002423#ifdef CONFIG_MEDIA_ATTACH
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002424void dvb_frontend_detach(struct dvb_frontend* fe)
2425{
2426 void *ptr;
2427
2428 if (fe->ops.release_sec) {
2429 fe->ops.release_sec(fe);
2430 symbol_put_addr(fe->ops.release_sec);
2431 }
2432 if (fe->ops.tuner_ops.release) {
2433 fe->ops.tuner_ops.release(fe);
2434 symbol_put_addr(fe->ops.tuner_ops.release);
2435 }
Michael Krufky2426a272007-12-21 11:34:45 -03002436 if (fe->ops.analog_ops.release) {
2437 fe->ops.analog_ops.release(fe);
2438 symbol_put_addr(fe->ops.analog_ops.release);
2439 }
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002440 ptr = (void*)fe->ops.release;
2441 if (ptr) {
2442 fe->ops.release(fe);
2443 symbol_put_addr(ptr);
2444 }
2445}
2446#else
2447void dvb_frontend_detach(struct dvb_frontend* fe)
2448{
2449 if (fe->ops.release_sec)
2450 fe->ops.release_sec(fe);
2451 if (fe->ops.tuner_ops.release)
2452 fe->ops.tuner_ops.release(fe);
Michael Krufky2426a272007-12-21 11:34:45 -03002453 if (fe->ops.analog_ops.release)
2454 fe->ops.analog_ops.release(fe);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002455 if (fe->ops.release)
2456 fe->ops.release(fe);
2457}
2458#endif
2459EXPORT_SYMBOL(dvb_frontend_detach);