blob: 4555baa383b26ec58b987bb65fecbe8303d7c504 [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;
Simon Arlott45145b62012-02-06 17:57:01 -0300658 } else {
659 re_tune = false;
Manu Abrahamd772bd02006-06-24 11:18:58 -0300660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Manu Abrahamc59e7872008-10-14 16:34:07 -0300662 if (fe->ops.tune)
Mauro Carvalho Chehab7e072222011-12-26 17:48:33 -0300663 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300664
Darron Broad2fac9a02008-12-18 06:27:50 -0300665 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
Manu Abrahamc59e7872008-10-14 16:34:07 -0300666 dprintk("%s: state changed, adding current state\n", __func__);
Manu Abrahamd772bd02006-06-24 11:18:58 -0300667 dvb_frontend_add_event(fe, s);
668 fepriv->status = s;
669 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300670 break;
671 case DVBFE_ALGO_SW:
672 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
Manu Abraham70d90632006-06-29 22:05:23 -0300673 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300674 break;
675 case DVBFE_ALGO_CUSTOM:
Manu Abrahamc59e7872008-10-14 16:34:07 -0300676 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
677 if (fepriv->state & FESTATE_RETUNE) {
678 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
679 fepriv->state = FESTATE_TUNED;
680 }
681 /* Case where we are going to search for a carrier
682 * User asked us to retune again for some reason, possibly
683 * requesting a search with a new set of parameters
684 */
685 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300686 if (fe->ops.search) {
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -0300687 fepriv->algo_status = fe->ops.search(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300688 /* We did do a search as was requested, the flags are
689 * now unset as well and has the flags wrt to search.
690 */
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300691 } else {
692 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
693 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300694 }
695 /* Track the carrier if the search was successful */
Mauro Carvalho Chehab1b5d8712011-12-26 18:11:25 -0300696 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
Arvo Jarve7bcbdf32008-10-20 06:05:21 -0300697 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
698 fepriv->delay = HZ / 2;
699 }
700 fe->ops.read_status(fe, &s);
701 if (s != fepriv->status) {
702 dvb_frontend_add_event(fe, s); /* update event list */
703 fepriv->status = s;
704 if (!(s & FE_HAS_LOCK)) {
705 fepriv->delay = HZ / 10;
706 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
707 } else {
708 fepriv->delay = 60 * HZ;
709 }
Manu Abrahamc59e7872008-10-14 16:34:07 -0300710 }
711 break;
712 default:
713 dprintk("%s: UNDEFINED ALGO !\n", __func__);
714 break;
715 }
716 } else {
Manu Abraham4a4edcc2006-06-25 05:46:26 -0300717 dvb_frontend_swzigzag(fe);
Manu Abrahamc59e7872008-10-14 16:34:07 -0300718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Oliver Endriss608f62d2007-08-25 13:17:53 -0300721 if (dvb_powerdown_on_sleep) {
722 if (fe->ops.set_voltage)
723 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300724 if (fe->ops.tuner_ops.sleep) {
Devin Heitmueller41286d92008-11-16 00:44:52 -0300725 if (fe->ops.i2c_gate_ctrl)
726 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300727 fe->ops.tuner_ops.sleep(fe);
728 if (fe->ops.i2c_gate_ctrl)
729 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300730 }
Patrick Boettcherdea74862006-05-14 05:01:31 -0300731 if (fe->ops.sleep)
732 fe->ops.sleep(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300735 fepriv->thread = NULL;
matthieu castete36309f2010-05-05 15:59:20 -0300736 if (kthread_should_stop())
737 fepriv->exit = DVB_FE_DEVICE_REMOVED;
738 else
739 fepriv->exit = DVB_FE_NO_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 mb();
741
742 dvb_frontend_wakeup(fe);
743 return 0;
744}
745
746static void dvb_frontend_stop(struct dvb_frontend *fe)
747{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700748 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300750 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
matthieu castete36309f2010-05-05 15:59:20 -0300752 fepriv->exit = DVB_FE_NORMAL_EXIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 mb();
754
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300755 if (!fepriv->thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return;
757
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300758 kthread_stop(fepriv->thread);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300759
Thomas Gleixnera0a47142010-09-07 11:33:27 -0300760 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 fepriv->state = FESTATE_IDLE;
762
763 /* paranoia check in case a signal arrived */
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300764 if (fepriv->thread)
765 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
766 fepriv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
NooneImportant83b75b02005-11-08 21:35:27 -0800769s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
770{
771 return ((curtime.tv_usec < lasttime.tv_usec) ?
772 1000000 - lasttime.tv_usec + curtime.tv_usec :
773 curtime.tv_usec - lasttime.tv_usec);
774}
775EXPORT_SYMBOL(timeval_usec_diff);
776
777static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
778{
779 curtime->tv_usec += add_usec;
780 if (curtime->tv_usec >= 1000000) {
781 curtime->tv_usec -= 1000000;
782 curtime->tv_sec++;
783 }
784}
785
786/*
787 * Sleep until gettimeofday() > waketime + add_usec
788 * This needs to be as precise as possible, but as the delay is
789 * usually between 2ms and 32ms, it is done using a scheduled msleep
790 * followed by usleep (normally a busy-wait loop) for the remainder
791 */
792void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
793{
794 struct timeval lasttime;
795 s32 delta, newdelta;
796
797 timeval_usec_add(waketime, add_usec);
798
799 do_gettimeofday(&lasttime);
800 delta = timeval_usec_diff(lasttime, *waketime);
801 if (delta > 2500) {
802 msleep((delta - 1500) / 1000);
803 do_gettimeofday(&lasttime);
804 newdelta = timeval_usec_diff(lasttime, *waketime);
805 delta = (newdelta > delta) ? 0 : newdelta;
806 }
807 if (delta > 0)
808 udelay(delta);
809}
810EXPORT_SYMBOL(dvb_frontend_sleep_until);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812static int dvb_frontend_start(struct dvb_frontend *fe)
813{
814 int ret;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700815 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300816 struct task_struct *fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300818 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300820 if (fepriv->thread) {
matthieu castete36309f2010-05-05 15:59:20 -0300821 if (fepriv->exit == DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823 else
824 dvb_frontend_stop (fe);
825 }
826
827 if (signal_pending(current))
828 return -EINTR;
829 if (down_interruptible (&fepriv->sem))
830 return -EINTR;
831
832 fepriv->state = FESTATE_IDLE;
matthieu castete36309f2010-05-05 15:59:20 -0300833 fepriv->exit = DVB_FE_NO_EXIT;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300834 fepriv->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 mb();
836
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300837 fe_thread = kthread_run(dvb_frontend_thread, fe,
Steven Toth363c35f2008-10-11 11:05:50 -0300838 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300839 if (IS_ERR(fe_thread)) {
840 ret = PTR_ERR(fe_thread);
841 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 up(&fepriv->sem);
843 return ret;
844 }
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300845 fepriv->thread = fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return 0;
847}
848
Guillaume Audirac2030c032010-05-06 09:30:25 -0300849static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
Oliver Endrissc471b332007-08-09 01:03:42 -0300850 u32 *freq_min, u32 *freq_max)
851{
852 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
853
854 if (fe->ops.info.frequency_max == 0)
855 *freq_max = fe->ops.tuner_ops.info.frequency_max;
856 else if (fe->ops.tuner_ops.info.frequency_max == 0)
857 *freq_max = fe->ops.info.frequency_max;
858 else
859 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
860
861 if (*freq_min == 0 || *freq_max == 0)
Steven Toth363c35f2008-10-11 11:05:50 -0300862 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
863 fe->dvb->num,fe->id);
Oliver Endrissc471b332007-08-09 01:03:42 -0300864}
865
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300866static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300867{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300868 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Oliver Endrissc471b332007-08-09 01:03:42 -0300869 u32 freq_min;
870 u32 freq_max;
871
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300872 /* range check: frequency */
Guillaume Audirac2030c032010-05-06 09:30:25 -0300873 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300874 if ((freq_min && c->frequency < freq_min) ||
875 (freq_max && c->frequency > freq_max)) {
Steven Toth363c35f2008-10-11 11:05:50 -0300876 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300877 fe->dvb->num, fe->id, c->frequency, freq_min, freq_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300878 return -EINVAL;
879 }
880
881 /* range check: symbol rate */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300882 switch (c->delivery_system) {
883 case SYS_DVBS:
884 case SYS_DVBS2:
885 case SYS_TURBO:
886 case SYS_DVBC_ANNEX_A:
887 case SYS_DVBC_ANNEX_C:
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300888 if ((fe->ops.info.symbol_rate_min &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300889 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300890 (fe->ops.info.symbol_rate_max &&
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300891 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
Steven Toth363c35f2008-10-11 11:05:50 -0300892 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 -0300893 fe->dvb->num, fe->id, c->symbol_rate,
894 fe->ops.info.symbol_rate_min,
895 fe->ops.info.symbol_rate_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300896 return -EINVAL;
897 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300898 default:
899 break;
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300900 }
901
902 return 0;
903}
904
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300905static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
906{
Andreas Oberritter50727712011-05-08 20:03:39 -0300907 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300908 int i;
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200909 u32 delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300910
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200911 delsys = c->delivery_system;
Andreas Oberritter50727712011-05-08 20:03:39 -0300912 memset(c, 0, sizeof(struct dtv_frontend_properties));
Mauro Carvalho Chehab149709f2012-01-13 11:46:36 -0200913 c->delivery_system = delsys;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300914
Andreas Oberritter50727712011-05-08 20:03:39 -0300915 c->state = DTV_CLEAR;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300916
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300917 dprintk("%s() Clearing cache for delivery system %d\n", __func__,
918 c->delivery_system);
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300919
Andreas Oberritter50727712011-05-08 20:03:39 -0300920 c->transmission_mode = TRANSMISSION_MODE_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300921 c->bandwidth_hz = 0; /* AUTO */
Andreas Oberritter50727712011-05-08 20:03:39 -0300922 c->guard_interval = GUARD_INTERVAL_AUTO;
923 c->hierarchy = HIERARCHY_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300924 c->symbol_rate = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -0300925 c->code_rate_HP = FEC_AUTO;
926 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300927 c->fec_inner = FEC_AUTO;
Mauro Carvalho Chehab39ce61a2011-11-11 12:46:23 -0200928 c->rolloff = ROLLOFF_AUTO;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300929 c->voltage = SEC_VOLTAGE_OFF;
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300930 c->sectone = SEC_TONE_OFF;
931 c->pilot = PILOT_AUTO;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300932
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300933 c->isdbt_partial_reception = 0;
934 c->isdbt_sb_mode = 0;
935 c->isdbt_sb_subchannel = 0;
936 c->isdbt_sb_segment_idx = 0;
937 c->isdbt_sb_segment_count = 0;
938 c->isdbt_layer_enabled = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300939 for (i = 0; i < 3; i++) {
Andreas Oberritter50727712011-05-08 20:03:39 -0300940 c->layer[i].fec = FEC_AUTO;
941 c->layer[i].modulation = QAM_AUTO;
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -0300942 c->layer[i].interleaving = 0;
943 c->layer[i].segment_count = 0;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300944 }
945
Mauro Carvalho Chehab26c924f2012-01-01 16:11:10 -0300946 c->isdbs_ts_id = 0;
947 c->dvbt2_plp_id = 0;
948
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -0300949 switch (c->delivery_system) {
950 case SYS_DVBS:
951 case SYS_DVBS2:
952 case SYS_TURBO:
953 c->modulation = QPSK; /* implied for DVB-S in legacy API */
954 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
955 break;
956 case SYS_ATSC:
957 c->modulation = VSB_8;
958 break;
959 default:
960 c->modulation = QAM_AUTO;
961 break;
962 }
963
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300964 return 0;
965}
966
967#define _DTV_CMD(n, s, b) \
968[n] = { \
969 .name = #n, \
970 .cmd = n, \
971 .set = s,\
972 .buffer = b \
973}
974
Andreas Oberritterec05a642011-05-26 07:54:14 -0300975static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300976 _DTV_CMD(DTV_TUNE, 1, 0),
977 _DTV_CMD(DTV_CLEAR, 1, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -0300978
979 /* Set */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -0300980 _DTV_CMD(DTV_FREQUENCY, 1, 0),
981 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
982 _DTV_CMD(DTV_MODULATION, 1, 0),
983 _DTV_CMD(DTV_INVERSION, 1, 0),
984 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
985 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
986 _DTV_CMD(DTV_INNER_FEC, 1, 0),
987 _DTV_CMD(DTV_VOLTAGE, 1, 0),
988 _DTV_CMD(DTV_TONE, 1, 0),
989 _DTV_CMD(DTV_PILOT, 1, 0),
990 _DTV_CMD(DTV_ROLLOFF, 1, 0),
991 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
992 _DTV_CMD(DTV_HIERARCHY, 1, 0),
993 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
994 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
995 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
996 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -0300997
998 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
999 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1000 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1001 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1002 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
Patrick Boettchere7b79492009-08-14 05:24:19 -03001003 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001004 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1005 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1006 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1007 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1008 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1009 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1010 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1011 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1012 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1013 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1014 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1015 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1016
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001017 _DTV_CMD(DTV_ISDBS_TS_ID, 1, 0),
Andreas Oberritterec05a642011-05-26 07:54:14 -03001018 _DTV_CMD(DTV_DVBT2_PLP_ID, 1, 0),
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001019
Steven Toth6b73eea2008-09-04 01:12:25 -03001020 /* Get */
Mauro Carvalho Chehab06d3a392009-10-23 20:09:42 -03001021 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1022 _DTV_CMD(DTV_API_VERSION, 0, 0),
1023 _DTV_CMD(DTV_CODE_RATE_HP, 0, 0),
1024 _DTV_CMD(DTV_CODE_RATE_LP, 0, 0),
1025 _DTV_CMD(DTV_GUARD_INTERVAL, 0, 0),
1026 _DTV_CMD(DTV_TRANSMISSION_MODE, 0, 0),
1027 _DTV_CMD(DTV_HIERARCHY, 0, 0),
Manu Abrahamba2780c2011-11-13 18:47:44 -03001028
1029 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
Steven Toth6b73eea2008-09-04 01:12:25 -03001030};
1031
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001032static void dtv_property_dump(struct dtv_property *tvp)
Steven Toth6b73eea2008-09-04 01:12:25 -03001033{
1034 int i;
1035
Mauro Carvalho Chehab106ff9e2011-07-15 08:49:06 -03001036 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
Steven Totha1bc84c2008-10-17 00:20:19 -03001037 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
Darron Broad4aae8ef2008-10-03 11:50:00 -03001038 __func__, tvp->cmd);
1039 return;
1040 }
1041
Steven Totha1bc84c2008-10-17 00:20:19 -03001042 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
1043 ,__func__
Steven Toth6b73eea2008-09-04 01:12:25 -03001044 ,tvp->cmd
Steven Toth56f06802008-09-11 10:19:27 -03001045 ,dtv_cmds[ tvp->cmd ].name);
Steven Toth6b73eea2008-09-04 01:12:25 -03001046
Steven Toth56f06802008-09-11 10:19:27 -03001047 if(dtv_cmds[ tvp->cmd ].buffer) {
Steven Toth6b73eea2008-09-04 01:12:25 -03001048
Steven Totha1bc84c2008-10-17 00:20:19 -03001049 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1050 ,__func__
Steven Toth6b73eea2008-09-04 01:12:25 -03001051 ,tvp->u.buffer.len);
1052
1053 for(i = 0; i < tvp->u.buffer.len; i++)
Steven Totha1bc84c2008-10-17 00:20:19 -03001054 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1055 ,__func__
Steven Toth6b73eea2008-09-04 01:12:25 -03001056 ,i
1057 ,tvp->u.buffer.data[i]);
1058
1059 } else
Steven Totha1bc84c2008-10-17 00:20:19 -03001060 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
Steven Toth6b73eea2008-09-04 01:12:25 -03001061}
1062
Steven Tothee33c522008-09-11 23:52:32 -03001063/* Synchronise the legacy tuning parameters into the cache, so that demodulator
1064 * drivers can use a single set_frontend tuning function, regardless of whether
1065 * it's being used for the legacy or new API, reducing code and complexity.
1066 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001067static int dtv_property_cache_sync(struct dvb_frontend *fe,
1068 struct dtv_frontend_properties *c,
1069 const struct dvb_frontend_parameters *p)
Steven Tothee33c522008-09-11 23:52:32 -03001070{
Steven Tothee33c522008-09-11 23:52:32 -03001071 c->frequency = p->frequency;
1072 c->inversion = p->inversion;
1073
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001074 switch (dvbv3_type(c->delivery_system)) {
1075 case DVBV3_QPSK:
1076 dprintk("%s() Preparing QPSK req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001077 c->symbol_rate = p->u.qpsk.symbol_rate;
1078 c->fec_inner = p->u.qpsk.fec_inner;
1079 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001080 case DVBV3_QAM:
1081 dprintk("%s() Preparing QAM req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001082 c->symbol_rate = p->u.qam.symbol_rate;
1083 c->fec_inner = p->u.qam.fec_inner;
1084 c->modulation = p->u.qam.modulation;
1085 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001086 case DVBV3_OFDM:
1087 dprintk("%s() Preparing OFDM req\n", __func__);
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001088 switch (p->u.ofdm.bandwidth) {
1089 case BANDWIDTH_10_MHZ:
1090 c->bandwidth_hz = 10000000;
1091 break;
1092 case BANDWIDTH_8_MHZ:
Steven Toth75b7f942008-09-13 16:56:34 -03001093 c->bandwidth_hz = 8000000;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001094 break;
1095 case BANDWIDTH_7_MHZ:
1096 c->bandwidth_hz = 7000000;
1097 break;
1098 case BANDWIDTH_6_MHZ:
1099 c->bandwidth_hz = 6000000;
1100 break;
1101 case BANDWIDTH_5_MHZ:
1102 c->bandwidth_hz = 5000000;
1103 break;
1104 case BANDWIDTH_1_712_MHZ:
1105 c->bandwidth_hz = 1712000;
1106 break;
1107 case BANDWIDTH_AUTO:
Steven Toth75b7f942008-09-13 16:56:34 -03001108 c->bandwidth_hz = 0;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001109 }
1110
Steven Tothee33c522008-09-11 23:52:32 -03001111 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1112 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1113 c->modulation = p->u.ofdm.constellation;
1114 c->transmission_mode = p->u.ofdm.transmission_mode;
1115 c->guard_interval = p->u.ofdm.guard_interval;
1116 c->hierarchy = p->u.ofdm.hierarchy_information;
1117 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001118 case DVBV3_ATSC:
1119 dprintk("%s() Preparing ATSC req\n", __func__);
Steven Tothee33c522008-09-11 23:52:32 -03001120 c->modulation = p->u.vsb.modulation;
Steven Toth80a773c2008-09-12 00:53:50 -03001121 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1122 c->delivery_system = SYS_ATSC;
1123 else
1124 c->delivery_system = SYS_DVBC_ANNEX_B;
Steven Tothee33c522008-09-11 23:52:32 -03001125 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001126 case DVBV3_UNKNOWN:
1127 printk(KERN_ERR
1128 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1129 __func__, c->delivery_system);
1130 return -EINVAL;
Steven Tothee33c522008-09-11 23:52:32 -03001131 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001132
1133 return 0;
Steven Tothee33c522008-09-11 23:52:32 -03001134}
1135
Steven Tothd5748f12008-09-12 00:27:13 -03001136/* Ensure the cached values are set correctly in the frontend
1137 * legacy tuning structures, for the advanced tuning API.
1138 */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001139static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001140 struct dvb_frontend_parameters *p)
Steven Toth6b73eea2008-09-04 01:12:25 -03001141{
Andreas Oberritter15cc2bb2011-05-08 20:03:35 -03001142 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001143
Steven Tothd5748f12008-09-12 00:27:13 -03001144 p->frequency = c->frequency;
1145 p->inversion = c->inversion;
1146
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001147 switch (dvbv3_type(c->delivery_system)) {
1148 case DVBV3_UNKNOWN:
1149 printk(KERN_ERR
1150 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1151 __func__, c->delivery_system);
1152 return -EINVAL;
1153 case DVBV3_QPSK:
Steven Totha1bc84c2008-10-17 00:20:19 -03001154 dprintk("%s() Preparing QPSK req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001155 p->u.qpsk.symbol_rate = c->symbol_rate;
1156 p->u.qpsk.fec_inner = c->fec_inner;
1157 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001158 case DVBV3_QAM:
Steven Totha1bc84c2008-10-17 00:20:19 -03001159 dprintk("%s() Preparing QAM req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001160 p->u.qam.symbol_rate = c->symbol_rate;
1161 p->u.qam.fec_inner = c->fec_inner;
1162 p->u.qam.modulation = c->modulation;
1163 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001164 case DVBV3_OFDM:
Steven Totha1bc84c2008-10-17 00:20:19 -03001165 dprintk("%s() Preparing OFDM req\n", __func__);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001166
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001167 switch (c->bandwidth_hz) {
1168 case 10000000:
1169 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1170 break;
1171 case 8000000:
Steven Toth75b7f942008-09-13 16:56:34 -03001172 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001173 break;
1174 case 7000000:
1175 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1176 break;
1177 case 6000000:
1178 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1179 break;
1180 case 5000000:
1181 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1182 break;
1183 case 1712000:
1184 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1185 break;
1186 case 0:
1187 default:
Steven Toth75b7f942008-09-13 16:56:34 -03001188 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
Mauro Carvalho Chehab9a27e6a2012-01-01 16:11:11 -03001189 }
Steven Tothd5748f12008-09-12 00:27:13 -03001190 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1191 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1192 p->u.ofdm.constellation = c->modulation;
1193 p->u.ofdm.transmission_mode = c->transmission_mode;
1194 p->u.ofdm.guard_interval = c->guard_interval;
1195 p->u.ofdm.hierarchy_information = c->hierarchy;
1196 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001197 case DVBV3_ATSC:
Steven Totha1bc84c2008-10-17 00:20:19 -03001198 dprintk("%s() Preparing VSB req\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001199 p->u.vsb.modulation = c->modulation;
1200 break;
1201 }
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001202 return 0;
Steven Toth6b73eea2008-09-04 01:12:25 -03001203}
1204
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001205/**
1206 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1207 * @fe: struct dvb_frontend pointer
1208 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1209 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1210 *
1211 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1212 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1213 * If p_out is not null, it will update the DVBv3 params pointed by it.
1214 */
1215static int dtv_get_frontend(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001216 struct dvb_frontend_parameters *p_out)
1217{
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001218 int r;
1219
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001220 if (fe->ops.get_frontend) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001221 r = fe->ops.get_frontend(fe);
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001222 if (unlikely(r < 0))
1223 return r;
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001224 if (p_out)
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001225 dtv_property_legacy_params_sync(fe, p_out);
1226 return 0;
1227 }
1228
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001229 /* As everything is in cache, get_frontend fops are always supported */
Mauro Carvalho Chehabb1e9a65012011-12-26 16:43:32 -03001230 return 0;
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001231}
1232
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001233static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001234 unsigned int cmd, void *parg);
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001235static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001236 unsigned int cmd, void *parg);
1237
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001238static int dtv_property_process_get(struct dvb_frontend *fe,
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001239 const struct dtv_frontend_properties *c,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001240 struct dtv_property *tvp,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001241 struct file *file)
Steven Toth363429a2008-09-12 01:34:28 -03001242{
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001243 int r, ncaps;
Steven Tothbfbf2da2008-09-12 01:37:37 -03001244
Steven Toth363429a2008-09-12 01:34:28 -03001245 switch(tvp->cmd) {
Manu Abrahamba2780c2011-11-13 18:47:44 -03001246 case DTV_ENUM_DELSYS:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001247 ncaps = 0;
1248 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1249 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1250 ncaps++;
1251 }
1252 tvp->u.buffer.len = ncaps;
Manu Abrahamba2780c2011-11-13 18:47:44 -03001253 break;
Steven Toth363429a2008-09-12 01:34:28 -03001254 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001255 tvp->u.data = c->frequency;
Steven Toth363429a2008-09-12 01:34:28 -03001256 break;
1257 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001258 tvp->u.data = c->modulation;
Steven Toth363429a2008-09-12 01:34:28 -03001259 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001260 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001261 tvp->u.data = c->bandwidth_hz;
Steven Toth363429a2008-09-12 01:34:28 -03001262 break;
1263 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001264 tvp->u.data = c->inversion;
Steven Toth363429a2008-09-12 01:34:28 -03001265 break;
1266 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001267 tvp->u.data = c->symbol_rate;
Steven Toth363429a2008-09-12 01:34:28 -03001268 break;
1269 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001270 tvp->u.data = c->fec_inner;
Steven Toth363429a2008-09-12 01:34:28 -03001271 break;
1272 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001273 tvp->u.data = c->pilot;
Steven Toth363429a2008-09-12 01:34:28 -03001274 break;
1275 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001276 tvp->u.data = c->rolloff;
Steven Toth363429a2008-09-12 01:34:28 -03001277 break;
1278 case DTV_DELIVERY_SYSTEM:
Andreas Oberritter50727712011-05-08 20:03:39 -03001279 tvp->u.data = c->delivery_system;
Steven Toth363429a2008-09-12 01:34:28 -03001280 break;
Steven Toth363429a2008-09-12 01:34:28 -03001281 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001282 tvp->u.data = c->voltage;
Steven Toth363429a2008-09-12 01:34:28 -03001283 break;
1284 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001285 tvp->u.data = c->sectone;
Steven Toth363429a2008-09-12 01:34:28 -03001286 break;
Steven Totheacf8d82008-09-26 00:29:49 -03001287 case DTV_API_VERSION:
1288 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1289 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001290 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001291 tvp->u.data = c->code_rate_HP;
Steven Totha4de91b2008-10-06 21:55:46 -03001292 break;
1293 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001294 tvp->u.data = c->code_rate_LP;
Steven Totha4de91b2008-10-06 21:55:46 -03001295 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001296 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001297 tvp->u.data = c->guard_interval;
Steven Tothb87625f2008-10-06 21:56:59 -03001298 break;
1299 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001300 tvp->u.data = c->transmission_mode;
Steven Tothb87625f2008-10-06 21:56:59 -03001301 break;
Steven Tothef526f42008-10-06 22:01:47 -03001302 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001303 tvp->u.data = c->hierarchy;
Steven Tothef526f42008-10-06 22:01:47 -03001304 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001305
1306 /* ISDB-T Support here */
1307 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001308 tvp->u.data = c->isdbt_partial_reception;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001309 break;
1310 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001311 tvp->u.data = c->isdbt_sb_mode;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001312 break;
1313 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001314 tvp->u.data = c->isdbt_sb_subchannel;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001315 break;
1316 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001317 tvp->u.data = c->isdbt_sb_segment_idx;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001318 break;
1319 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001320 tvp->u.data = c->isdbt_sb_segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001321 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001322 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001323 tvp->u.data = c->isdbt_layer_enabled;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001324 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001325 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001326 tvp->u.data = c->layer[0].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001327 break;
1328 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001329 tvp->u.data = c->layer[0].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001330 break;
1331 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001332 tvp->u.data = c->layer[0].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001333 break;
1334 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001335 tvp->u.data = c->layer[0].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001336 break;
1337 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001338 tvp->u.data = c->layer[1].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001339 break;
1340 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001341 tvp->u.data = c->layer[1].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001342 break;
1343 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001344 tvp->u.data = c->layer[1].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001345 break;
1346 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001347 tvp->u.data = c->layer[1].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001348 break;
1349 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001350 tvp->u.data = c->layer[2].fec;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001351 break;
1352 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001353 tvp->u.data = c->layer[2].modulation;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001354 break;
1355 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001356 tvp->u.data = c->layer[2].segment_count;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001357 break;
1358 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001359 tvp->u.data = c->layer[2].interleaving;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001360 break;
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001361 case DTV_ISDBS_TS_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001362 tvp->u.data = c->isdbs_ts_id;
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001363 break;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001364 case DTV_DVBT2_PLP_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001365 tvp->u.data = c->dvbt2_plp_id;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001366 break;
Steven Toth363429a2008-09-12 01:34:28 -03001367 default:
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001368 return -EINVAL;
Steven Toth363429a2008-09-12 01:34:28 -03001369 }
1370
Andreas Oberrittere23d9ae2011-05-08 20:03:36 -03001371 /* Allow the frontend to override outgoing properties */
1372 if (fe->ops.get_property) {
1373 r = fe->ops.get_property(fe, tvp);
1374 if (r < 0)
1375 return r;
1376 }
1377
Mauro Carvalho Chehab639544d2009-12-31 17:49:32 -03001378 dtv_property_dump(tvp);
1379
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001380 return 0;
Steven Toth363429a2008-09-12 01:34:28 -03001381}
1382
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001383static int dtv_set_frontend(struct dvb_frontend *fe);
1384
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001385static bool is_dvbv3_delsys(u32 delsys)
1386{
1387 bool status;
1388
1389 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1390 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1391
1392 return status;
1393}
1394
1395static int set_delivery_system(struct dvb_frontend *fe, u32 desired_system)
1396{
1397 int ncaps, i;
1398 u32 delsys = SYS_UNDEFINED;
1399 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1400 enum dvbv3_emulation_type type;
1401
Mauro Carvalho Chehabf4b19522012-01-13 11:09:23 -02001402 /*
1403 * It was reported that some old DVBv5 applications were
1404 * filling delivery_system with SYS_UNDEFINED. If this happens,
1405 * assume that the application wants to use the first supported
1406 * delivery system.
1407 */
1408 if (c->delivery_system == SYS_UNDEFINED)
1409 c->delivery_system = fe->ops.delsys[0];
1410
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001411 if (desired_system == SYS_UNDEFINED) {
1412 /*
1413 * A DVBv3 call doesn't know what's the desired system.
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001414 * Also, DVBv3 applications don't know that ops.info->type
1415 * could be changed, and they simply dies when it doesn't
1416 * match.
1417 * So, don't change the current delivery system, as it
1418 * may be trying to do the wrong thing, like setting an
1419 * ISDB-T frontend as DVB-T. Instead, find the closest
1420 * DVBv3 system that matches the delivery system.
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001421 */
1422 if (is_dvbv3_delsys(c->delivery_system)) {
1423 dprintk("%s() Using delivery system to %d\n",
1424 __func__, c->delivery_system);
1425 return 0;
1426 }
1427 type = dvbv3_type(c->delivery_system);
1428 switch (type) {
1429 case DVBV3_QPSK:
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001430 desired_system = SYS_DVBS;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001431 break;
1432 case DVBV3_QAM:
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001433 desired_system = SYS_DVBC_ANNEX_A;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001434 break;
1435 case DVBV3_ATSC:
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001436 desired_system = SYS_ATSC;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001437 break;
1438 case DVBV3_OFDM:
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001439 desired_system = SYS_DVBT;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001440 break;
1441 default:
1442 dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
1443 __func__);
1444 return -EINVAL;
1445 }
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001446 } else {
1447 /*
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001448 * This is a DVBv5 call. So, it likely knows the supported
1449 * delivery systems.
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001450 */
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001451
1452 /* Check if the desired delivery system is supported */
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001453 ncaps = 0;
1454 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1455 if (fe->ops.delsys[ncaps] == desired_system) {
1456 c->delivery_system = desired_system;
1457 dprintk("%s() Changing delivery system to %d\n",
1458 __func__, desired_system);
1459 return 0;
1460 }
Mauro Carvalho Chehab6c4b78c2012-01-05 11:24:41 -02001461 ncaps++;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001462 }
1463 type = dvbv3_type(desired_system);
1464
1465 /*
1466 * The delivery system is not supported. See if it can be
1467 * emulated.
1468 * The emulation only works if the desired system is one of the
1469 * DVBv3 delivery systems
1470 */
1471 if (!is_dvbv3_delsys(desired_system)) {
1472 dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1473 __func__);
1474 return -EINVAL;
1475 }
1476
1477 /*
1478 * Get the last non-DVBv3 delivery system that has the same type
1479 * of the desired system
1480 */
1481 ncaps = 0;
1482 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1483 if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
1484 !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
1485 delsys = fe->ops.delsys[ncaps];
1486 ncaps++;
1487 }
1488 /* There's nothing compatible with the desired delivery system */
1489 if (delsys == SYS_UNDEFINED) {
1490 dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1491 __func__);
1492 return -EINVAL;
1493 }
1494 c->delivery_system = delsys;
1495 }
1496
1497 /*
Mauro Carvalho Chehab37fa53c2012-01-05 14:39:05 -02001498 * The DVBv3 or DVBv5 call is requesting a different system. So,
1499 * emulation is needed.
1500 *
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001501 * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
1502 * for older DVBv5 applications. The emulation will try to use
1503 * the auto mode for most things, and will assume that the desired
1504 * delivery system is the last one at the ops.delsys[] array
1505 */
1506 dprintk("%s() Using delivery system %d emulated as if it were a %d\n",
1507 __func__, delsys, desired_system);
1508
1509 /*
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -03001510 * For now, handles ISDB-T calls. More code may be needed here for the
1511 * other emulated stuff
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001512 */
1513 if (type == DVBV3_OFDM) {
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -03001514 if (c->delivery_system == SYS_ISDBT) {
1515 dprintk("%s() Using defaults for SYS_ISDBT\n",
1516 __func__);
1517 if (!c->bandwidth_hz)
1518 c->bandwidth_hz = 6000000;
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001519
Mauro Carvalho Chehaba520ca92012-01-01 16:11:15 -03001520 c->isdbt_partial_reception = 0;
1521 c->isdbt_sb_mode = 0;
1522 c->isdbt_sb_subchannel = 0;
1523 c->isdbt_sb_segment_idx = 0;
1524 c->isdbt_sb_segment_count = 0;
1525 c->isdbt_layer_enabled = 0;
1526 for (i = 0; i < 3; i++) {
1527 c->layer[i].fec = FEC_AUTO;
1528 c->layer[i].modulation = QAM_AUTO;
1529 c->layer[i].interleaving = 0;
1530 c->layer[i].segment_count = 0;
1531 }
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001532 }
1533 }
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001534 dprintk("change delivery system on cache to %d\n", c->delivery_system);
1535
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001536 return 0;
1537}
1538
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001539static int dtv_property_process_set(struct dvb_frontend *fe,
1540 struct dtv_property *tvp,
Mauro Carvalho Chehab072ce0c2009-01-05 01:19:06 -03001541 struct file *file)
Steven Toth6b73eea2008-09-04 01:12:25 -03001542{
1543 int r = 0;
Andreas Oberritter50727712011-05-08 20:03:39 -03001544 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001545
Steven Tothbfbf2da2008-09-12 01:37:37 -03001546 /* Allow the frontend to validate incoming properties */
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001547 if (fe->ops.set_property) {
Steven Tothbfbf2da2008-09-12 01:37:37 -03001548 r = fe->ops.set_property(fe, tvp);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001549 if (r < 0)
1550 return r;
1551 }
Steven Tothbfbf2da2008-09-12 01:37:37 -03001552
Steven Toth6b73eea2008-09-04 01:12:25 -03001553 switch(tvp->cmd) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001554 case DTV_CLEAR:
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001555 /*
1556 * Reset a cache of data specific to the frontend here. This does
Steven Toth6b73eea2008-09-04 01:12:25 -03001557 * not effect hardware.
1558 */
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001559 dvb_frontend_clear_cache(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001560 break;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001561 case DTV_TUNE:
Steven Toth6b73eea2008-09-04 01:12:25 -03001562 /* interpret the cache of data, build either a traditional frontend
Steven Tothd5748f12008-09-12 00:27:13 -03001563 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1564 * ioctl.
Steven Toth6b73eea2008-09-04 01:12:25 -03001565 */
Andreas Oberritter50727712011-05-08 20:03:39 -03001566 c->state = tvp->cmd;
Steven Totha1bc84c2008-10-17 00:20:19 -03001567 dprintk("%s() Finalised property cache\n", __func__);
Steven Tothd5748f12008-09-12 00:27:13 -03001568
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001569 r = dtv_set_frontend(fe);
Steven Toth6b73eea2008-09-04 01:12:25 -03001570 break;
Steven Toth363429a2008-09-12 01:34:28 -03001571 case DTV_FREQUENCY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001572 c->frequency = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001573 break;
Steven Toth363429a2008-09-12 01:34:28 -03001574 case DTV_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001575 c->modulation = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001576 break;
Steven Toth75b7f942008-09-13 16:56:34 -03001577 case DTV_BANDWIDTH_HZ:
Andreas Oberritter50727712011-05-08 20:03:39 -03001578 c->bandwidth_hz = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001579 break;
Steven Toth363429a2008-09-12 01:34:28 -03001580 case DTV_INVERSION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001581 c->inversion = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001582 break;
Steven Toth363429a2008-09-12 01:34:28 -03001583 case DTV_SYMBOL_RATE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001584 c->symbol_rate = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001585 break;
Steven Toth363429a2008-09-12 01:34:28 -03001586 case DTV_INNER_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001587 c->fec_inner = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001588 break;
Steven Toth363429a2008-09-12 01:34:28 -03001589 case DTV_PILOT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001590 c->pilot = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001591 break;
Steven Toth363429a2008-09-12 01:34:28 -03001592 case DTV_ROLLOFF:
Andreas Oberritter50727712011-05-08 20:03:39 -03001593 c->rolloff = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001594 break;
Steven Toth363429a2008-09-12 01:34:28 -03001595 case DTV_DELIVERY_SYSTEM:
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03001596 r = set_delivery_system(fe, tvp->u.data);
Steven Toth6b73eea2008-09-04 01:12:25 -03001597 break;
Steven Toth363429a2008-09-12 01:34:28 -03001598 case DTV_VOLTAGE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001599 c->voltage = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001600 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001601 (void *)c->voltage);
Steven Toth13c97bf2008-09-04 21:19:43 -03001602 break;
Steven Toth363429a2008-09-12 01:34:28 -03001603 case DTV_TONE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001604 c->sectone = tvp->u.data;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001605 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
Andreas Oberritter50727712011-05-08 20:03:39 -03001606 (void *)c->sectone);
Steven Toth13c97bf2008-09-04 21:19:43 -03001607 break;
Steven Totha4de91b2008-10-06 21:55:46 -03001608 case DTV_CODE_RATE_HP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001609 c->code_rate_HP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001610 break;
1611 case DTV_CODE_RATE_LP:
Andreas Oberritter50727712011-05-08 20:03:39 -03001612 c->code_rate_LP = tvp->u.data;
Steven Totha4de91b2008-10-06 21:55:46 -03001613 break;
Steven Tothb87625f2008-10-06 21:56:59 -03001614 case DTV_GUARD_INTERVAL:
Andreas Oberritter50727712011-05-08 20:03:39 -03001615 c->guard_interval = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001616 break;
1617 case DTV_TRANSMISSION_MODE:
Andreas Oberritter50727712011-05-08 20:03:39 -03001618 c->transmission_mode = tvp->u.data;
Steven Tothb87625f2008-10-06 21:56:59 -03001619 break;
Steven Tothef526f42008-10-06 22:01:47 -03001620 case DTV_HIERARCHY:
Andreas Oberritter50727712011-05-08 20:03:39 -03001621 c->hierarchy = tvp->u.data;
Steven Tothef526f42008-10-06 22:01:47 -03001622 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001623
1624 /* ISDB-T Support here */
1625 case DTV_ISDBT_PARTIAL_RECEPTION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001626 c->isdbt_partial_reception = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001627 break;
1628 case DTV_ISDBT_SOUND_BROADCASTING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001629 c->isdbt_sb_mode = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001630 break;
1631 case DTV_ISDBT_SB_SUBCHANNEL_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001632 c->isdbt_sb_subchannel = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001633 break;
1634 case DTV_ISDBT_SB_SEGMENT_IDX:
Andreas Oberritter50727712011-05-08 20:03:39 -03001635 c->isdbt_sb_segment_idx = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001636 break;
1637 case DTV_ISDBT_SB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001638 c->isdbt_sb_segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001639 break;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001640 case DTV_ISDBT_LAYER_ENABLED:
Andreas Oberritter50727712011-05-08 20:03:39 -03001641 c->isdbt_layer_enabled = tvp->u.data;
Patrick Boettchere7b79492009-08-14 05:24:19 -03001642 break;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001643 case DTV_ISDBT_LAYERA_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001644 c->layer[0].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001645 break;
1646 case DTV_ISDBT_LAYERA_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001647 c->layer[0].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001648 break;
1649 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001650 c->layer[0].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001651 break;
1652 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001653 c->layer[0].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001654 break;
1655 case DTV_ISDBT_LAYERB_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001656 c->layer[1].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001657 break;
1658 case DTV_ISDBT_LAYERB_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001659 c->layer[1].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001660 break;
1661 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001662 c->layer[1].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001663 break;
1664 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001665 c->layer[1].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001666 break;
1667 case DTV_ISDBT_LAYERC_FEC:
Andreas Oberritter50727712011-05-08 20:03:39 -03001668 c->layer[2].fec = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001669 break;
1670 case DTV_ISDBT_LAYERC_MODULATION:
Andreas Oberritter50727712011-05-08 20:03:39 -03001671 c->layer[2].modulation = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001672 break;
1673 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
Andreas Oberritter50727712011-05-08 20:03:39 -03001674 c->layer[2].segment_count = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001675 break;
1676 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
Andreas Oberritter50727712011-05-08 20:03:39 -03001677 c->layer[2].interleaving = tvp->u.data;
Patrick Boettcherb6e760f2009-08-03 14:39:15 -03001678 break;
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001679 case DTV_ISDBS_TS_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001680 c->isdbs_ts_id = tvp->u.data;
HIRANO Takahito98293ef2009-09-18 11:17:54 -03001681 break;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001682 case DTV_DVBT2_PLP_ID:
Andreas Oberritter50727712011-05-08 20:03:39 -03001683 c->dvbt2_plp_id = tvp->u.data;
Andreas Oberritter94d56ff2011-05-12 18:11:06 -03001684 break;
Steven Toth363429a2008-09-12 01:34:28 -03001685 default:
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001686 return -EINVAL;
Steven Toth6b73eea2008-09-04 01:12:25 -03001687 }
1688
Steven Toth13c97bf2008-09-04 21:19:43 -03001689 return r;
Steven Toth6b73eea2008-09-04 01:12:25 -03001690}
1691
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001692static int dvb_frontend_ioctl(struct file *file,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 unsigned int cmd, void *parg)
1694{
1695 struct dvb_device *dvbdev = file->private_data;
1696 struct dvb_frontend *fe = dvbdev->priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001697 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001698 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 int err = -EOPNOTSUPP;
1700
Mauro Carvalho Chehabfb8253b2009-12-31 18:14:54 -03001701 dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
matthieu castete36309f2010-05-05 15:59:20 -03001703 if (fepriv->exit != DVB_FE_NO_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return -ENODEV;
1705
1706 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1707 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1708 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1709 return -EPERM;
1710
1711 if (down_interruptible (&fepriv->sem))
1712 return -ERESTARTSYS;
1713
Steven Toth13c97bf2008-09-04 21:19:43 -03001714 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001715 err = dvb_frontend_ioctl_properties(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001716 else {
Andreas Oberritter50727712011-05-08 20:03:39 -03001717 c->state = DTV_UNDEFINED;
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001718 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
Steven Toth4dd88be2008-09-13 15:09:07 -03001719 }
Steven Toth13c97bf2008-09-04 21:19:43 -03001720
1721 up(&fepriv->sem);
1722 return err;
1723}
1724
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001725static int dvb_frontend_ioctl_properties(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001726 unsigned int cmd, void *parg)
1727{
1728 struct dvb_device *dvbdev = file->private_data;
1729 struct dvb_frontend *fe = dvbdev->priv;
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02001730 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Andreas Oberritter50727712011-05-08 20:03:39 -03001731 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Igor M. Liplianine6f9ec82008-09-12 14:49:06 -03001732 int err = 0;
Steven Tothe7fee0f32008-09-11 10:23:01 -03001733
1734 struct dtv_properties *tvps = NULL;
1735 struct dtv_property *tvp = NULL;
1736 int i;
Steven Toth13c97bf2008-09-04 21:19:43 -03001737
1738 dprintk("%s\n", __func__);
1739
Steven Toth6b73eea2008-09-04 01:12:25 -03001740 if(cmd == FE_SET_PROPERTY) {
Steven Tothe7fee0f32008-09-11 10:23:01 -03001741 tvps = (struct dtv_properties __user *)parg;
Steven Toth6b73eea2008-09-04 01:12:25 -03001742
Steven Totha1bc84c2008-10-17 00:20:19 -03001743 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1744 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001745
1746 /* Put an arbitrary limit on the number of messages that can
1747 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001748 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Tothe7fee0f32008-09-11 10:23:01 -03001749 return -EINVAL;
1750
Thiago Farina6e3924a2010-01-16 10:25:38 -03001751 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Tothe7fee0f32008-09-11 10:23:01 -03001752 if (!tvp) {
1753 err = -ENOMEM;
1754 goto out;
Steven Toth6b73eea2008-09-04 01:12:25 -03001755 }
1756
Steven Tothe7fee0f32008-09-11 10:23:01 -03001757 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1758 err = -EFAULT;
1759 goto out;
1760 }
1761
Steven Tothd48cb402008-09-26 00:16:25 -03001762 for (i = 0; i < tvps->num; i++) {
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001763 err = dtv_property_process_set(fe, tvp + i, file);
1764 if (err < 0)
1765 goto out;
1766 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03001767 }
Steven Tothe7fee0f32008-09-11 10:23:01 -03001768
Andreas Oberritter50727712011-05-08 20:03:39 -03001769 if (c->state == DTV_TUNE)
Steven Totha1bc84c2008-10-17 00:20:19 -03001770 dprintk("%s() Property cache is full, tuning\n", __func__);
Steven Tothbfbf2da2008-09-12 01:37:37 -03001771
Steven Toth363429a2008-09-12 01:34:28 -03001772 } else
1773 if(cmd == FE_GET_PROPERTY) {
Steven Toth363429a2008-09-12 01:34:28 -03001774 tvps = (struct dtv_properties __user *)parg;
1775
Steven Totha1bc84c2008-10-17 00:20:19 -03001776 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1777 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
Steven Toth363429a2008-09-12 01:34:28 -03001778
1779 /* Put an arbitrary limit on the number of messages that can
1780 * be sent at once */
Steven Toth6068f502008-10-06 22:46:08 -03001781 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
Steven Toth363429a2008-09-12 01:34:28 -03001782 return -EINVAL;
1783
Thiago Farina6e3924a2010-01-16 10:25:38 -03001784 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
Steven Toth363429a2008-09-12 01:34:28 -03001785 if (!tvp) {
1786 err = -ENOMEM;
1787 goto out;
1788 }
1789
1790 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1791 err = -EFAULT;
1792 goto out;
1793 }
1794
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001795 /*
1796 * Fills the cache out struct with the cache contents, plus
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02001797 * the data retrieved from get_frontend, if the frontend
1798 * is not idle. Otherwise, returns the cached content
Mauro Carvalho Chehaba689e362011-12-22 14:47:48 -03001799 */
Mauro Carvalho Chehab51dcb192012-01-17 16:20:37 -02001800 if (fepriv->state != FESTATE_IDLE) {
1801 err = dtv_get_frontend(fe, NULL);
1802 if (err < 0)
1803 goto out;
1804 }
Steven Tothd48cb402008-09-26 00:16:25 -03001805 for (i = 0; i < tvps->num; i++) {
Mauro Carvalho Chehab7c61d802011-12-30 11:30:21 -03001806 err = dtv_property_process_get(fe, c, tvp + i, file);
Andreas Oberritteraff8ab52011-05-09 00:03:34 +01001807 if (err < 0)
1808 goto out;
1809 (tvp + i)->result = err;
Steven Tothd48cb402008-09-26 00:16:25 -03001810 }
Steven Toth363429a2008-09-12 01:34:28 -03001811
1812 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1813 err = -EFAULT;
1814 goto out;
1815 }
1816
Steven Toth363429a2008-09-12 01:34:28 -03001817 } else
1818 err = -EOPNOTSUPP;
1819
Steven Tothe7fee0f32008-09-11 10:23:01 -03001820out:
1821 kfree(tvp);
Steven Toth13c97bf2008-09-04 21:19:43 -03001822 return err;
1823}
1824
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001825static int dtv_set_frontend(struct dvb_frontend *fe)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001826{
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001827 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1828 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1829 struct dvb_frontend_tune_settings fetunesettings;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001830 u32 rolloff = 0;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001831
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001832 if (dvb_frontend_check_parameters(fe) < 0)
1833 return -EINVAL;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001834
1835 /*
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001836 * Be sure that the bandwidth will be filled for all
1837 * non-satellite systems, as tuners need to know what
1838 * low pass/Nyquist half filter should be applied, in
1839 * order to avoid inter-channel noise.
1840 *
1841 * ISDB-T and DVB-T/T2 already sets bandwidth.
1842 * ATSC and DVB-C don't set, so, the core should fill it.
1843 *
1844 * On DVB-C Annex A and C, the bandwidth is a function of
1845 * the roll-off and symbol rate. Annex B defines different
1846 * roll-off factors depending on the modulation. Fortunately,
1847 * Annex B is only used with 6MHz, so there's no need to
1848 * calculate it.
1849 *
1850 * While not officially supported, a side effect of handling it at
1851 * the cache level is that a program could retrieve the bandwidth
1852 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1853 */
1854 switch (c->delivery_system) {
1855 case SYS_ATSC:
1856 case SYS_DVBC_ANNEX_B:
1857 c->bandwidth_hz = 6000000;
1858 break;
1859 case SYS_DVBC_ANNEX_A:
1860 rolloff = 115;
1861 break;
1862 case SYS_DVBC_ANNEX_C:
1863 rolloff = 113;
1864 break;
1865 default:
1866 break;
1867 }
1868 if (rolloff)
1869 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001870
1871 /* force auto frequency inversion if requested */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001872 if (dvb_force_auto_inversion)
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001873 c->inversion = INVERSION_AUTO;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001874
1875 /*
1876 * without hierarchical coding code_rate_LP is irrelevant,
1877 * so we tolerate the otherwise invalid FEC_NONE setting
1878 */
1879 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
1880 c->code_rate_LP = FEC_AUTO;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001881
1882 /* get frontend-specific tuning settings */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001883 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001884 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1885 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1886 fepriv->max_drift = fetunesettings.max_drift;
1887 fepriv->step_size = fetunesettings.step_size;
1888 } else {
1889 /* default values */
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001890 switch (c->delivery_system) {
1891 case SYS_DVBC_ANNEX_A:
1892 case SYS_DVBC_ANNEX_C:
1893 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001894 fepriv->step_size = c->symbol_rate / 16000;
1895 fepriv->max_drift = c->symbol_rate / 2000;
1896 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001897 case SYS_DVBT:
1898 case SYS_DVBT2:
1899 case SYS_ISDBT:
1900 case SYS_DMBTH:
1901 fepriv->min_delay = HZ / 20;
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001902 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1903 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1904 break;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001905 default:
1906 /*
1907 * FIXME: This sounds wrong! if freqency_stepsize is
1908 * defined by the frontend, why not use it???
1909 */
1910 fepriv->min_delay = HZ / 20;
1911 fepriv->step_size = 0; /* no zigzag */
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03001912 fepriv->max_drift = 0;
1913 break;
1914 }
1915 }
1916 if (dvb_override_tune_delay > 0)
1917 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1918
1919 fepriv->state = FESTATE_RETUNE;
1920
1921 /* Request the search algorithm to search */
1922 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1923
1924 dvb_frontend_clear_events(fe);
1925 dvb_frontend_add_event(fe, 0);
1926 dvb_frontend_wakeup(fe);
1927 fepriv->status = 0;
1928
1929 return 0;
1930}
1931
1932
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02001933static int dvb_frontend_ioctl_legacy(struct file *file,
Steven Toth13c97bf2008-09-04 21:19:43 -03001934 unsigned int cmd, void *parg)
1935{
1936 struct dvb_device *dvbdev = file->private_data;
1937 struct dvb_frontend *fe = dvbdev->priv;
1938 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001939 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Michael Krufky9133aee2009-05-23 18:00:59 -03001940 int cb_err, err = -EOPNOTSUPP;
1941
1942 if (fe->dvb->fe_ioctl_override) {
1943 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1944 DVB_FE_IOCTL_PRE);
1945 if (cb_err < 0)
1946 return cb_err;
1947 if (cb_err > 0)
1948 return 0;
1949 /* fe_ioctl_override returning 0 allows
1950 * dvb-core to continue handling the ioctl */
1951 }
Steven Toth13c97bf2008-09-04 21:19:43 -03001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 switch (cmd) {
1954 case FE_GET_INFO: {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001955 struct dvb_frontend_info* info = parg;
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001956
Patrick Boettcherdea74862006-05-14 05:01:31 -03001957 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
Guillaume Audirac2030c032010-05-06 09:30:25 -03001958 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001960 /*
1961 * Associate the 4 delivery systems supported by DVBv3
1962 * API with their DVBv5 counterpart. For the other standards,
1963 * use the closest type, assuming that it would hopefully
1964 * work with a DVBv3 application.
1965 * It should be noticed that, on multi-frontend devices with
1966 * different types (terrestrial and cable, for example),
1967 * a pure DVBv3 application won't be able to use all delivery
1968 * systems. Yet, changing the DVBv5 cache to the other delivery
1969 * system should be enough for making it work.
1970 */
1971 switch (dvbv3_type(c->delivery_system)) {
1972 case DVBV3_QPSK:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001973 info->type = FE_QPSK;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001974 break;
1975 case DVBV3_ATSC:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001976 info->type = FE_ATSC;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001977 break;
1978 case DVBV3_QAM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001979 info->type = FE_QAM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001980 break;
1981 case DVBV3_OFDM:
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001982 info->type = FE_OFDM;
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001983 break;
1984 default:
1985 printk(KERN_ERR
1986 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1987 __func__, c->delivery_system);
1988 fe->ops.info.type = FE_OFDM;
1989 }
Mauro Carvalho Chehab9474c5e2012-01-05 12:28:20 -02001990 dprintk("current delivery system on cache: %d, V3 type: %d\n",
1991 c->delivery_system, fe->ops.info.type);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1994 * do it, it is done for it. */
1995 info->caps |= FE_CAN_INVERSION_AUTO;
1996 err = 0;
1997 break;
1998 }
1999
Peter Beutner6757ccc2005-07-07 17:57:36 -07002000 case FE_READ_STATUS: {
2001 fe_status_t* status = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002003 /* if retune was requested but hasn't occurred yet, prevent
Peter Beutner6757ccc2005-07-07 17:57:36 -07002004 * that user get signal state from previous tuning */
Janne Grunau01886252009-09-01 19:23:09 -03002005 if (fepriv->state == FESTATE_RETUNE ||
2006 fepriv->state == FESTATE_ERROR) {
Peter Beutner6757ccc2005-07-07 17:57:36 -07002007 err=0;
2008 *status = 0;
2009 break;
2010 }
2011
Patrick Boettcherdea74862006-05-14 05:01:31 -03002012 if (fe->ops.read_status)
2013 err = fe->ops.read_status(fe, status);
Peter Beutner6757ccc2005-07-07 17:57:36 -07002014 break;
2015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 case FE_READ_BER:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002017 if (fe->ops.read_ber)
2018 err = fe->ops.read_ber(fe, (__u32*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 break;
2020
2021 case FE_READ_SIGNAL_STRENGTH:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002022 if (fe->ops.read_signal_strength)
2023 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 break;
2025
2026 case FE_READ_SNR:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002027 if (fe->ops.read_snr)
2028 err = fe->ops.read_snr(fe, (__u16*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 break;
2030
2031 case FE_READ_UNCORRECTED_BLOCKS:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002032 if (fe->ops.read_ucblocks)
2033 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
2035
2036
2037 case FE_DISEQC_RESET_OVERLOAD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002038 if (fe->ops.diseqc_reset_overload) {
2039 err = fe->ops.diseqc_reset_overload(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 fepriv->state = FESTATE_DISEQC;
2041 fepriv->status = 0;
2042 }
2043 break;
2044
2045 case FE_DISEQC_SEND_MASTER_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002046 if (fe->ops.diseqc_send_master_cmd) {
2047 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 fepriv->state = FESTATE_DISEQC;
2049 fepriv->status = 0;
2050 }
2051 break;
2052
2053 case FE_DISEQC_SEND_BURST:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002054 if (fe->ops.diseqc_send_burst) {
2055 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 fepriv->state = FESTATE_DISEQC;
2057 fepriv->status = 0;
2058 }
2059 break;
2060
2061 case FE_SET_TONE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002062 if (fe->ops.set_tone) {
2063 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03002064 fepriv->tone = (fe_sec_tone_mode_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 fepriv->state = FESTATE_DISEQC;
2066 fepriv->status = 0;
2067 }
2068 break;
2069
2070 case FE_SET_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002071 if (fe->ops.set_voltage) {
2072 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03002073 fepriv->voltage = (fe_sec_voltage_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 fepriv->state = FESTATE_DISEQC;
2075 fepriv->status = 0;
2076 }
2077 break;
2078
2079 case FE_DISHNETWORK_SEND_LEGACY_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002080 if (fe->ops.dishnetwork_send_legacy_command) {
2081 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 fepriv->state = FESTATE_DISEQC;
2083 fepriv->status = 0;
Patrick Boettcherdea74862006-05-14 05:01:31 -03002084 } else if (fe->ops.set_voltage) {
NooneImportant83b75b02005-11-08 21:35:27 -08002085 /*
2086 * NOTE: This is a fallback condition. Some frontends
2087 * (stv0299 for instance) take longer than 8msec to
2088 * respond to a set_voltage command. Those switches
2089 * need custom routines to switch properly. For all
Guillaume Audirac2030c032010-05-06 09:30:25 -03002090 * other frontends, the following should work ok.
NooneImportant83b75b02005-11-08 21:35:27 -08002091 * Dish network legacy switches (as used by Dish500)
2092 * are controlled by sending 9-bit command words
2093 * spaced 8msec apart.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002094 * the actual command word is switch/port dependent
NooneImportant83b75b02005-11-08 21:35:27 -08002095 * so it is up to the userspace application to send
2096 * the right command.
2097 * The command must always start with a '0' after
2098 * initialization, so parg is 8 bits and does not
2099 * include the initialization or start bit
2100 */
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002101 unsigned long swcmd = ((unsigned long) parg) << 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002102 struct timeval nexttime;
2103 struct timeval tv[10];
2104 int i;
2105 u8 last = 1;
2106 if (dvb_frontend_debug)
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002107 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
NooneImportant83b75b02005-11-08 21:35:27 -08002108 do_gettimeofday(&nexttime);
2109 if (dvb_frontend_debug)
2110 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
2111 /* before sending a command, initialize by sending
2112 * a 32ms 18V to the switch
2113 */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002114 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002115 dvb_frontend_sleep_until(&nexttime, 32000);
2116
2117 for (i = 0; i < 9; i++) {
2118 if (dvb_frontend_debug)
2119 do_gettimeofday(&tv[i + 1]);
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002120 if ((swcmd & 0x01) != last) {
NooneImportant83b75b02005-11-08 21:35:27 -08002121 /* set voltage to (last ? 13V : 18V) */
Patrick Boettcherdea74862006-05-14 05:01:31 -03002122 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08002123 last = (last) ? 0 : 1;
2124 }
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03002125 swcmd = swcmd >> 1;
NooneImportant83b75b02005-11-08 21:35:27 -08002126 if (i != 8)
2127 dvb_frontend_sleep_until(&nexttime, 8000);
2128 }
2129 if (dvb_frontend_debug) {
2130 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002131 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -08002132 for (i = 1; i < 10; i++)
2133 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2134 }
2135 err = 0;
2136 fepriv->state = FESTATE_DISEQC;
2137 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
2139 break;
2140
2141 case FE_DISEQC_RECV_SLAVE_REPLY:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002142 if (fe->ops.diseqc_recv_slave_reply)
2143 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 break;
2145
2146 case FE_ENABLE_HIGH_LNB_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03002147 if (fe->ops.enable_high_lnb_voltage)
2148 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 break;
2150
Mauro Carvalho Chehab9682cea2012-01-01 16:11:12 -03002151 case FE_SET_FRONTEND:
Mauro Carvalho Chehab04be0f72012-01-01 16:11:14 -03002152 err = set_delivery_system(fe, SYS_UNDEFINED);
2153 if (err)
2154 break;
2155
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002156 err = dtv_property_cache_sync(fe, c, parg);
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002157 if (err)
2158 break;
2159 err = dtv_set_frontend(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 case FE_GET_EVENT:
2162 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2163 break;
2164
2165 case FE_GET_FRONTEND:
Mauro Carvalho Chehabe399ce72012-01-01 16:11:16 -03002166 err = dtv_get_frontend(fe, parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 break;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002168
2169 case FE_SET_FRONTEND_TUNE_MODE:
Hans Verkuile18828e2006-01-09 15:25:28 -02002170 fepriv->tune_mode_flags = (unsigned long) parg;
Trent Piepho1b172e02006-06-29 13:16:04 -03002171 err = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02002172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 };
2174
Michael Krufky9133aee2009-05-23 18:00:59 -03002175 if (fe->dvb->fe_ioctl_override) {
2176 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
2177 DVB_FE_IOCTL_POST);
2178 if (cb_err < 0)
2179 return cb_err;
2180 }
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return err;
2183}
2184
Steven Toth6b73eea2008-09-04 01:12:25 -03002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2187{
2188 struct dvb_device *dvbdev = file->private_data;
2189 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002190 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002192 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 poll_wait (file, &fepriv->events.wait_queue, wait);
2195
2196 if (fepriv->events.eventw != fepriv->events.eventr)
2197 return (POLLIN | POLLRDNORM | POLLPRI);
2198
2199 return 0;
2200}
2201
2202static int dvb_frontend_open(struct inode *inode, struct file *file)
2203{
2204 struct dvb_device *dvbdev = file->private_data;
2205 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002206 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Darron Broad59b18422008-10-11 11:44:05 -03002207 struct dvb_adapter *adapter = fe->dvb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 int ret;
2209
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002210 dprintk ("%s\n", __func__);
matthieu castete36309f2010-05-05 15:59:20 -03002211 if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2212 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Darron Broad59b18422008-10-11 11:44:05 -03002214 if (adapter->mfe_shared) {
2215 mutex_lock (&adapter->mfe_lock);
Darron Broad65946902008-10-15 13:37:59 -03002216
2217 if (adapter->mfe_dvbdev == NULL)
2218 adapter->mfe_dvbdev = dvbdev;
2219
2220 else if (adapter->mfe_dvbdev != dvbdev) {
2221 struct dvb_device
2222 *mfedev = adapter->mfe_dvbdev;
2223 struct dvb_frontend
2224 *mfe = mfedev->priv;
2225 struct dvb_frontend_private
2226 *mfepriv = mfe->frontend_priv;
2227 int mferetry = (dvb_mfe_wait_time << 1);
2228
2229 mutex_unlock (&adapter->mfe_lock);
2230 while (mferetry-- && (mfedev->users != -1 ||
2231 mfepriv->thread != NULL)) {
2232 if(msleep_interruptible(500)) {
2233 if(signal_pending(current))
2234 return -EINTR;
Darron Broad59b18422008-10-11 11:44:05 -03002235 }
2236 }
Darron Broad65946902008-10-15 13:37:59 -03002237
2238 mutex_lock (&adapter->mfe_lock);
2239 if(adapter->mfe_dvbdev != dvbdev) {
2240 mfedev = adapter->mfe_dvbdev;
2241 mfe = mfedev->priv;
2242 mfepriv = mfe->frontend_priv;
2243 if (mfedev->users != -1 ||
2244 mfepriv->thread != NULL) {
2245 mutex_unlock (&adapter->mfe_lock);
2246 return -EBUSY;
2247 }
2248 adapter->mfe_dvbdev = dvbdev;
2249 }
Darron Broad59b18422008-10-11 11:44:05 -03002250 }
2251 }
2252
Oliver Endriss48136e12007-08-25 12:00:23 -03002253 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2254 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
Darron Broad59b18422008-10-11 11:44:05 -03002255 goto err0;
Devin Heitmueller2d196932011-07-04 21:55:01 -03002256
2257 /* If we took control of the bus, we need to force
2258 reinitialization. This is because many ts_bus_ctrl()
2259 functions strobe the RESET pin on the demod, and if the
2260 frontend thread already exists then the dvb_init() routine
2261 won't get called (which is what usually does initial
2262 register configuration). */
2263 fepriv->reinitialise = 1;
Steven Tothba7e6f32006-09-25 12:41:53 -03002264 }
2265
Oliver Endriss48136e12007-08-25 12:00:23 -03002266 if ((ret = dvb_generic_open (inode, file)) < 0)
2267 goto err1;
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002268
Oliver Endriss48136e12007-08-25 12:00:23 -03002269 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Andrew de Quincey04c56d02006-07-10 03:34:14 -03002270 /* normal tune mode when opened R/W */
2271 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2272 fepriv->tone = -1;
2273 fepriv->voltage = -1;
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 ret = dvb_frontend_start (fe);
2276 if (ret)
Oliver Endriss48136e12007-08-25 12:00:23 -03002277 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 /* empty event queue */
2280 fepriv->events.eventr = fepriv->events.eventw = 0;
2281 }
2282
Darron Broad59b18422008-10-11 11:44:05 -03002283 if (adapter->mfe_shared)
2284 mutex_unlock (&adapter->mfe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return ret;
Oliver Endriss48136e12007-08-25 12:00:23 -03002286
2287err2:
2288 dvb_generic_release(inode, file);
2289err1:
2290 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2291 fe->ops.ts_bus_ctrl(fe, 0);
Darron Broad59b18422008-10-11 11:44:05 -03002292err0:
2293 if (adapter->mfe_shared)
2294 mutex_unlock (&adapter->mfe_lock);
Oliver Endriss48136e12007-08-25 12:00:23 -03002295 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
2298static int dvb_frontend_release(struct inode *inode, struct file *file)
2299{
2300 struct dvb_device *dvbdev = file->private_data;
2301 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002302 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002303 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002305 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002307 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 fepriv->release_jiffies = jiffies;
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002309 mb();
2310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002312 ret = dvb_generic_release (inode, file);
2313
Oliver Endriss48136e12007-08-25 12:00:23 -03002314 if (dvbdev->users == -1) {
Andreas Oberritterb9d5efc2011-12-09 21:37:00 -03002315 wake_up(&fepriv->wait_queue);
matthieu castete36309f2010-05-05 15:59:20 -03002316 if (fepriv->exit != DVB_FE_NO_EXIT) {
Oliver Endriss48136e12007-08-25 12:00:23 -03002317 fops_put(file->f_op);
2318 file->f_op = NULL;
2319 wake_up(&dvbdev->wait_queue);
2320 }
2321 if (fe->ops.ts_bus_ctrl)
2322 fe->ops.ts_bus_ctrl(fe, 0);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002323 }
Oliver Endriss48136e12007-08-25 12:00:23 -03002324
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002325 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Jan Engelhardt784e29d2009-01-11 06:12:43 -03002328static const struct file_operations dvb_frontend_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 .owner = THIS_MODULE,
Arnd Bergmann16ef8de2010-04-27 00:24:00 +02002330 .unlocked_ioctl = dvb_generic_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 .poll = dvb_frontend_poll,
2332 .open = dvb_frontend_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002333 .release = dvb_frontend_release,
2334 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335};
2336
2337int dvb_register_frontend(struct dvb_adapter* dvb,
2338 struct dvb_frontend* fe)
2339{
2340 struct dvb_frontend_private *fepriv;
2341 static const struct dvb_device dvbdev_template = {
2342 .users = ~0,
2343 .writers = 1,
2344 .readers = (~0)-1,
2345 .fops = &dvb_frontend_fops,
2346 .kernel_ioctl = dvb_frontend_ioctl
2347 };
2348
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002349 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Ingo Molnar3593cab2006-02-07 06:49:14 -02002351 if (mutex_lock_interruptible(&frontend_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return -ERESTARTSYS;
2353
Panagiotis Issaris74081872006-01-11 19:40:56 -02002354 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (fe->frontend_priv == NULL) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02002356 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return -ENOMEM;
2358 }
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002359 fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Thomas Gleixnera0a47142010-09-07 11:33:27 -03002361 sema_init(&fepriv->sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 init_waitqueue_head (&fepriv->wait_queue);
2363 init_waitqueue_head (&fepriv->events.wait_queue);
Matthias Kaehlcke03b76122007-07-30 14:58:10 -03002364 mutex_init(&fepriv->events.mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 fe->dvb = dvb;
2366 fepriv->inversion = INVERSION_OFF;
2367
Steven Toth363c35f2008-10-11 11:05:50 -03002368 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 fe->dvb->num,
Steven Toth363c35f2008-10-11 11:05:50 -03002370 fe->id,
Patrick Boettcherdea74862006-05-14 05:01:31 -03002371 fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2374 fe, DVB_DEVICE_FRONTEND);
2375
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002376 /*
2377 * Initialize the cache to the proper values according with the
2378 * first supported delivery system (ops->delsys[0])
2379 */
Mauro Carvalho Chehab240ab502012-01-10 18:00:50 -03002380
2381 fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
Mauro Carvalho Chehab5bfaadd2012-01-01 16:11:13 -03002382 dvb_frontend_clear_cache(fe);
2383
Ingo Molnar3593cab2006-02-07 06:49:14 -02002384 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return 0;
2386}
2387EXPORT_SYMBOL(dvb_register_frontend);
2388
2389int dvb_unregister_frontend(struct dvb_frontend* fe)
2390{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07002391 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03002392 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Markus Rechberger57861b42007-04-14 10:19:18 -03002394 mutex_lock(&frontend_mutex);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03002395 dvb_frontend_stop (fe);
Markus Rechberger57861b42007-04-14 10:19:18 -03002396 mutex_unlock(&frontend_mutex);
2397
2398 if (fepriv->dvbdev->users < -1)
2399 wait_event(fepriv->dvbdev->wait_queue,
2400 fepriv->dvbdev->users==-1);
2401
Ingo Molnar3593cab2006-02-07 06:49:14 -02002402 mutex_lock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 dvb_unregister_device (fepriv->dvbdev);
Andrew de Quinceyd9955062006-08-08 09:10:08 -03002404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /* fe is invalid now */
2406 kfree(fepriv);
Ingo Molnar3593cab2006-02-07 06:49:14 -02002407 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return 0;
2409}
2410EXPORT_SYMBOL(dvb_unregister_frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002411
Mauro Carvalho Chehab149ef722008-04-29 21:38:46 -03002412#ifdef CONFIG_MEDIA_ATTACH
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002413void dvb_frontend_detach(struct dvb_frontend* fe)
2414{
2415 void *ptr;
2416
2417 if (fe->ops.release_sec) {
2418 fe->ops.release_sec(fe);
2419 symbol_put_addr(fe->ops.release_sec);
2420 }
2421 if (fe->ops.tuner_ops.release) {
2422 fe->ops.tuner_ops.release(fe);
2423 symbol_put_addr(fe->ops.tuner_ops.release);
2424 }
Michael Krufky2426a272007-12-21 11:34:45 -03002425 if (fe->ops.analog_ops.release) {
2426 fe->ops.analog_ops.release(fe);
2427 symbol_put_addr(fe->ops.analog_ops.release);
2428 }
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002429 ptr = (void*)fe->ops.release;
2430 if (ptr) {
2431 fe->ops.release(fe);
2432 symbol_put_addr(ptr);
2433 }
2434}
2435#else
2436void dvb_frontend_detach(struct dvb_frontend* fe)
2437{
2438 if (fe->ops.release_sec)
2439 fe->ops.release_sec(fe);
2440 if (fe->ops.tuner_ops.release)
2441 fe->ops.tuner_ops.release(fe);
Michael Krufky2426a272007-12-21 11:34:45 -03002442 if (fe->ops.analog_ops.release)
2443 fe->ops.analog_ops.release(fe);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03002444 if (fe->ops.release)
2445 fe->ops.release(fe);
2446}
2447#endif
2448EXPORT_SYMBOL(dvb_frontend_detach);