blob: 9c4761506d2d54638466e60f9d2b2e8438700cb4 [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
28#include <linux/string.h>
29#include <linux/kernel.h>
30#include <linux/sched.h>
31#include <linux/wait.h>
32#include <linux/slab.h>
33#include <linux/poll.h>
34#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/list.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Johannes Stezenbachcc89c222005-06-23 22:02:39 -070037#include <linux/jiffies.h>
Herbert Poetzl8eec1422007-02-08 14:32:43 -030038#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#include "dvb_frontend.h"
42#include "dvbdev.h"
43
44static int dvb_frontend_debug;
Oliver Endriss849be2c2007-08-25 12:22:16 -030045static int dvb_shutdown_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static int dvb_force_auto_inversion;
47static int dvb_override_tune_delay;
48static int dvb_powerdown_on_sleep = 1;
49
50module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
Johannes Stezenbachf4f009a2005-05-16 21:54:21 -070051MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
Manu Abraham6baad3f2006-02-27 00:09:32 -030052module_param(dvb_shutdown_timeout, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
Manu Abraham6baad3f2006-02-27 00:09:32 -030054module_param(dvb_force_auto_inversion, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
Manu Abraham6baad3f2006-02-27 00:09:32 -030056module_param(dvb_override_tune_delay, int, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057MODULE_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 -030058module_param(dvb_powerdown_on_sleep, int, 0644);
Uwe Buglaa85585772006-04-11 10:21:37 -030059MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define dprintk if (dvb_frontend_debug) printk
62
63#define FESTATE_IDLE 1
64#define FESTATE_RETUNE 2
65#define FESTATE_TUNING_FAST 4
66#define FESTATE_TUNING_SLOW 8
67#define FESTATE_TUNED 16
68#define FESTATE_ZIGZAG_FAST 32
69#define FESTATE_ZIGZAG_SLOW 64
70#define FESTATE_DISEQC 128
71#define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
72#define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
73#define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
74#define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
Manu Abraham0249ef12006-06-21 10:27:31 -030075
76#define FE_ALGO_HW 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
79 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
80 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
81 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
82 * FESTATE_TUNED. The frontend has successfully locked on.
83 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
84 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
85 * FESTATE_DISEQC. A DISEQC command has just been issued.
86 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
87 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
88 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
89 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
90 */
91
Ingo Molnar3593cab2006-02-07 06:49:14 -020092static DEFINE_MUTEX(frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94struct dvb_frontend_private {
95
Andrew de Quincey36cb5572006-01-09 15:25:07 -020096 /* thread/frontend values */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct dvb_device *dvbdev;
98 struct dvb_frontend_parameters parameters;
99 struct dvb_fe_events events;
100 struct semaphore sem;
101 struct list_head list_head;
102 wait_queue_head_t wait_queue;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300103 struct task_struct *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long release_jiffies;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200105 unsigned int exit;
106 unsigned int wakeup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 fe_status_t status;
Peter Beutner400b7082006-01-09 15:32:43 -0200108 unsigned long tune_mode_flags;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200109 unsigned int delay;
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300110 unsigned int reinitialise;
Andrew de Quincey64454012006-04-06 14:32:23 -0300111 int tone;
112 int voltage;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200113
114 /* swzigzag values */
115 unsigned int state;
116 unsigned int bending;
117 int lnb_drift;
118 unsigned int inversion;
119 unsigned int auto_step;
120 unsigned int auto_sub_step;
121 unsigned int started_auto_step;
122 unsigned int min_delay;
123 unsigned int max_drift;
124 unsigned int step_size;
125 int quality;
126 unsigned int check_wrapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127};
128
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300129static void dvb_frontend_wakeup(struct dvb_frontend *fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
132{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700133 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct dvb_fe_events *events = &fepriv->events;
135 struct dvb_frontend_event *e;
136 int wp;
137
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300138 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300140 if (mutex_lock_interruptible (&events->mtx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return;
142
143 wp = (events->eventw + 1) % MAX_EVENT;
144
145 if (wp == events->eventr) {
146 events->overflow = 1;
147 events->eventr = (events->eventr + 1) % MAX_EVENT;
148 }
149
150 e = &events->events[events->eventw];
151
152 memcpy (&e->parameters, &fepriv->parameters,
153 sizeof (struct dvb_frontend_parameters));
154
155 if (status & FE_HAS_LOCK)
Patrick Boettcherdea74862006-05-14 05:01:31 -0300156 if (fe->ops.get_frontend)
157 fe->ops.get_frontend(fe, &e->parameters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 events->eventw = wp;
160
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300161 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 e->status = status;
164
165 wake_up_interruptible (&events->wait_queue);
166}
167
168static int dvb_frontend_get_event(struct dvb_frontend *fe,
169 struct dvb_frontend_event *event, int flags)
170{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700171 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct dvb_fe_events *events = &fepriv->events;
173
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300174 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (events->overflow) {
177 events->overflow = 0;
178 return -EOVERFLOW;
179 }
180
181 if (events->eventw == events->eventr) {
182 int ret;
183
184 if (flags & O_NONBLOCK)
185 return -EWOULDBLOCK;
186
187 up(&fepriv->sem);
188
189 ret = wait_event_interruptible (events->wait_queue,
190 events->eventw != events->eventr);
191
192 if (down_interruptible (&fepriv->sem))
193 return -ERESTARTSYS;
194
195 if (ret < 0)
196 return ret;
197 }
198
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300199 if (mutex_lock_interruptible (&events->mtx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -ERESTARTSYS;
201
202 memcpy (event, &events->events[events->eventr],
203 sizeof(struct dvb_frontend_event));
204
205 events->eventr = (events->eventr + 1) % MAX_EVENT;
206
Matthias Kaehlcke03b76122007-07-30 14:58:10 -0300207 mutex_unlock(&events->mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 return 0;
210}
211
212static void dvb_frontend_init(struct dvb_frontend *fe)
213{
214 dprintk ("DVB: initialising frontend %i (%s)...\n",
215 fe->dvb->num,
Patrick Boettcherdea74862006-05-14 05:01:31 -0300216 fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Patrick Boettcherdea74862006-05-14 05:01:31 -0300218 if (fe->ops.init)
219 fe->ops.init(fe);
220 if (fe->ops.tuner_ops.init) {
221 fe->ops.tuner_ops.init(fe);
222 if (fe->ops.i2c_gate_ctrl)
223 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300227void dvb_frontend_reinitialise(struct dvb_frontend *fe)
228{
229 struct dvb_frontend_private *fepriv = fe->frontend_priv;
230
231 fepriv->reinitialise = 1;
232 dvb_frontend_wakeup(fe);
233}
234EXPORT_SYMBOL(dvb_frontend_reinitialise);
235
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200236static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200238 int q2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300240 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200242 if (locked)
243 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
244 else
245 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200247 q2 = fepriv->quality - 128;
248 q2 *= q2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200250 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/**
254 * Performs automatic twiddling of frontend parameters.
255 *
256 * @param fe The frontend concerned.
257 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
258 * @returns Number of complete iterations that have been performed.
259 */
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200260static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int autoinversion;
263 int ready = 0;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700264 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 int original_inversion = fepriv->parameters.inversion;
266 u32 original_frequency = fepriv->parameters.frequency;
267
268 /* are we using autoinversion? */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300269 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 (fepriv->parameters.inversion == INVERSION_AUTO));
271
272 /* setup parameters correctly */
273 while(!ready) {
274 /* calculate the lnb_drift */
275 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
276
277 /* wrap the auto_step if we've exceeded the maximum drift */
278 if (fepriv->lnb_drift > fepriv->max_drift) {
279 fepriv->auto_step = 0;
280 fepriv->auto_sub_step = 0;
281 fepriv->lnb_drift = 0;
282 }
283
284 /* perform inversion and +/- zigzag */
285 switch(fepriv->auto_sub_step) {
286 case 0:
287 /* try with the current inversion and current drift setting */
288 ready = 1;
289 break;
290
291 case 1:
292 if (!autoinversion) break;
293
294 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
295 ready = 1;
296 break;
297
298 case 2:
299 if (fepriv->lnb_drift == 0) break;
300
301 fepriv->lnb_drift = -fepriv->lnb_drift;
302 ready = 1;
303 break;
304
305 case 3:
306 if (fepriv->lnb_drift == 0) break;
307 if (!autoinversion) break;
308
309 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
310 fepriv->lnb_drift = -fepriv->lnb_drift;
311 ready = 1;
312 break;
313
314 default:
315 fepriv->auto_step++;
316 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
317 break;
318 }
319
320 if (!ready) fepriv->auto_sub_step++;
321 }
322
323 /* if this attempt would hit where we started, indicate a complete
324 * iteration has occurred */
325 if ((fepriv->auto_step == fepriv->started_auto_step) &&
326 (fepriv->auto_sub_step == 0) && check_wrapped) {
327 return 1;
328 }
329
330 dprintk("%s: drift:%i inversion:%i auto_step:%i "
331 "auto_sub_step:%i started_auto_step:%i\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300332 __func__, fepriv->lnb_drift, fepriv->inversion,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
334
335 /* set the frontend itself */
336 fepriv->parameters.frequency += fepriv->lnb_drift;
337 if (autoinversion)
338 fepriv->parameters.inversion = fepriv->inversion;
Patrick Boettcherdea74862006-05-14 05:01:31 -0300339 if (fe->ops.set_frontend)
340 fe->ops.set_frontend(fe, &fepriv->parameters);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 fepriv->parameters.frequency = original_frequency;
343 fepriv->parameters.inversion = original_inversion;
344
345 fepriv->auto_sub_step++;
346 return 0;
347}
348
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200349static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
350{
Andrew de Quincey30d94642006-11-16 22:12:40 -0300351 fe_status_t s = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200352 struct dvb_frontend_private *fepriv = fe->frontend_priv;
353
354 /* if we've got no parameters, just keep idling */
355 if (fepriv->state & FESTATE_IDLE) {
356 fepriv->delay = 3*HZ;
357 fepriv->quality = 0;
358 return;
359 }
360
361 /* in SCAN mode, we just set the frontend when asked and leave it alone */
362 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
363 if (fepriv->state & FESTATE_RETUNE) {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300364 if (fe->ops.set_frontend)
365 fe->ops.set_frontend(fe, &fepriv->parameters);
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200366 fepriv->state = FESTATE_TUNED;
367 }
368 fepriv->delay = 3*HZ;
369 fepriv->quality = 0;
370 return;
371 }
372
373 /* get the frontend status */
374 if (fepriv->state & FESTATE_RETUNE) {
375 s = 0;
376 } else {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300377 if (fe->ops.read_status)
378 fe->ops.read_status(fe, &s);
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200379 if (s != fepriv->status) {
380 dvb_frontend_add_event(fe, s);
381 fepriv->status = s;
382 }
383 }
384
385 /* if we're not tuned, and we have a lock, move to the TUNED state */
386 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
387 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
388 fepriv->state = FESTATE_TUNED;
389
390 /* if we're tuned, then we have determined the correct inversion */
Patrick Boettcherdea74862006-05-14 05:01:31 -0300391 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200392 (fepriv->parameters.inversion == INVERSION_AUTO)) {
393 fepriv->parameters.inversion = fepriv->inversion;
394 }
395 return;
396 }
397
398 /* if we are tuned already, check we're still locked */
399 if (fepriv->state & FESTATE_TUNED) {
400 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
401
402 /* we're tuned, and the lock is still good... */
403 if (s & FE_HAS_LOCK) {
404 return;
405 } else { /* if we _WERE_ tuned, but now don't have a lock */
406 fepriv->state = FESTATE_ZIGZAG_FAST;
407 fepriv->started_auto_step = fepriv->auto_step;
408 fepriv->check_wrapped = 0;
409 }
410 }
411
412 /* don't actually do anything if we're in the LOSTLOCK state,
413 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
414 if ((fepriv->state & FESTATE_LOSTLOCK) &&
Patrick Boettcherdea74862006-05-14 05:01:31 -0300415 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200416 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
417 return;
418 }
419
420 /* don't do anything if we're in the DISEQC state, since this
421 * might be someone with a motorized dish controlled by DISEQC.
422 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
423 if (fepriv->state & FESTATE_DISEQC) {
424 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
425 return;
426 }
427
428 /* if we're in the RETUNE state, set everything up for a brand
429 * new scan, keeping the current inversion setting, as the next
430 * tune is _very_ likely to require the same */
431 if (fepriv->state & FESTATE_RETUNE) {
432 fepriv->lnb_drift = 0;
433 fepriv->auto_step = 0;
434 fepriv->auto_sub_step = 0;
435 fepriv->started_auto_step = 0;
436 fepriv->check_wrapped = 0;
437 }
438
439 /* fast zigzag. */
440 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
441 fepriv->delay = fepriv->min_delay;
442
443 /* peform a tune */
444 if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) {
445 /* OK, if we've run out of trials at the fast speed.
446 * Drop back to slow for the _next_ attempt */
447 fepriv->state = FESTATE_SEARCHING_SLOW;
448 fepriv->started_auto_step = fepriv->auto_step;
449 return;
450 }
451 fepriv->check_wrapped = 1;
452
453 /* if we've just retuned, enter the ZIGZAG_FAST state.
454 * This ensures we cannot return from an
455 * FE_SET_FRONTEND ioctl before the first frontend tune
456 * occurs */
457 if (fepriv->state & FESTATE_RETUNE) {
458 fepriv->state = FESTATE_TUNING_FAST;
459 }
460 }
461
462 /* slow zigzag */
463 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
464 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
465
466 /* Note: don't bother checking for wrapping; we stay in this
467 * state until we get a lock */
468 dvb_frontend_swzigzag_autotune(fe, 0);
469 }
470}
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
473{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700474 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (fepriv->exit)
477 return 1;
478
479 if (fepriv->dvbdev->writers == 1)
Johannes Stezenbachcc89c222005-06-23 22:02:39 -0700480 if (time_after(jiffies, fepriv->release_jiffies +
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200481 dvb_shutdown_timeout * HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 1;
483
484 return 0;
485}
486
487static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
488{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700489 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (fepriv->wakeup) {
492 fepriv->wakeup = 0;
493 return 1;
494 }
495 return dvb_frontend_is_exiting(fe);
496}
497
498static void dvb_frontend_wakeup(struct dvb_frontend *fe)
499{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700500 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 fepriv->wakeup = 1;
503 wake_up_interruptible(&fepriv->wait_queue);
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int dvb_frontend_thread(void *data)
507{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700508 struct dvb_frontend *fe = data;
509 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 unsigned long timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 fe_status_t s;
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200512 struct dvb_frontend_parameters *params;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300514 dprintk("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200516 fepriv->check_wrapped = 0;
517 fepriv->quality = 0;
518 fepriv->delay = 3*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 fepriv->wakeup = 0;
Andrew de Quincey04c56d02006-07-10 03:34:14 -0300521 fepriv->reinitialise = 0;
522
523 dvb_frontend_init(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700525 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 while (1) {
527 up(&fepriv->sem); /* is locked when we enter the thread... */
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300528restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
Rafael J. Wysockie42837b2007-10-18 03:04:45 -0700530 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
531 || freezing(current),
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300532 fepriv->delay);
533
534 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* got signal or quitting */
536 break;
537 }
538
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300539 if (try_to_freeze())
akpm@linux-foundation.org65916912007-02-08 14:36:57 -0300540 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 if (down_interruptible(&fepriv->sem))
543 break;
544
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300545 if (fepriv->reinitialise) {
546 dvb_frontend_init(fe);
Andrew de Quincey64454012006-04-06 14:32:23 -0300547 if (fepriv->tone != -1) {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300548 fe->ops.set_tone(fe, fepriv->tone);
Andrew de Quincey64454012006-04-06 14:32:23 -0300549 }
550 if (fepriv->voltage != -1) {
Patrick Boettcherdea74862006-05-14 05:01:31 -0300551 fe->ops.set_voltage(fe, fepriv->voltage);
Andrew de Quincey64454012006-04-06 14:32:23 -0300552 }
Andrew de Quincey86f40cc2006-03-30 15:53:35 -0300553 fepriv->reinitialise = 0;
554 }
555
Andrew de Quincey36cb5572006-01-09 15:25:07 -0200556 /* do an iteration of the tuning loop */
Manu Abrahamd772bd02006-06-24 11:18:58 -0300557 if (fe->ops.get_frontend_algo) {
558 if (fe->ops.get_frontend_algo(fe) == FE_ALGO_HW) {
559 /* have we been asked to retune? */
560 params = NULL;
561 if (fepriv->state & FESTATE_RETUNE) {
562 params = &fepriv->parameters;
563 fepriv->state = FESTATE_TUNED;
564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Manu Abrahamd772bd02006-06-24 11:18:58 -0300566 fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
567 if (s != fepriv->status) {
568 dvb_frontend_add_event(fe, s);
569 fepriv->status = s;
570 }
Manu Abraham70d90632006-06-29 22:05:23 -0300571 } else
572 dvb_frontend_swzigzag(fe);
Manu Abraham4a4edcc2006-06-25 05:46:26 -0300573 } else
574 dvb_frontend_swzigzag(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Oliver Endriss608f62d2007-08-25 13:17:53 -0300577 if (dvb_powerdown_on_sleep) {
578 if (fe->ops.set_voltage)
579 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
Patrick Boettcherdea74862006-05-14 05:01:31 -0300580 if (fe->ops.tuner_ops.sleep) {
581 fe->ops.tuner_ops.sleep(fe);
582 if (fe->ops.i2c_gate_ctrl)
583 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey7eef5dd2006-04-18 17:47:09 -0300584 }
Patrick Boettcherdea74862006-05-14 05:01:31 -0300585 if (fe->ops.sleep)
586 fe->ops.sleep(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300589 fepriv->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 mb();
591
592 dvb_frontend_wakeup(fe);
593 return 0;
594}
595
596static void dvb_frontend_stop(struct dvb_frontend *fe)
597{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700598 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300600 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 fepriv->exit = 1;
603 mb();
604
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300605 if (!fepriv->thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return;
607
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300608 kthread_stop(fepriv->thread);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -0300609
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300610 init_MUTEX (&fepriv->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 fepriv->state = FESTATE_IDLE;
612
613 /* paranoia check in case a signal arrived */
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300614 if (fepriv->thread)
615 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
616 fepriv->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
NooneImportant83b75b02005-11-08 21:35:27 -0800619s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
620{
621 return ((curtime.tv_usec < lasttime.tv_usec) ?
622 1000000 - lasttime.tv_usec + curtime.tv_usec :
623 curtime.tv_usec - lasttime.tv_usec);
624}
625EXPORT_SYMBOL(timeval_usec_diff);
626
627static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
628{
629 curtime->tv_usec += add_usec;
630 if (curtime->tv_usec >= 1000000) {
631 curtime->tv_usec -= 1000000;
632 curtime->tv_sec++;
633 }
634}
635
636/*
637 * Sleep until gettimeofday() > waketime + add_usec
638 * This needs to be as precise as possible, but as the delay is
639 * usually between 2ms and 32ms, it is done using a scheduled msleep
640 * followed by usleep (normally a busy-wait loop) for the remainder
641 */
642void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
643{
644 struct timeval lasttime;
645 s32 delta, newdelta;
646
647 timeval_usec_add(waketime, add_usec);
648
649 do_gettimeofday(&lasttime);
650 delta = timeval_usec_diff(lasttime, *waketime);
651 if (delta > 2500) {
652 msleep((delta - 1500) / 1000);
653 do_gettimeofday(&lasttime);
654 newdelta = timeval_usec_diff(lasttime, *waketime);
655 delta = (newdelta > delta) ? 0 : newdelta;
656 }
657 if (delta > 0)
658 udelay(delta);
659}
660EXPORT_SYMBOL(dvb_frontend_sleep_until);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662static int dvb_frontend_start(struct dvb_frontend *fe)
663{
664 int ret;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -0700665 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300666 struct task_struct *fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Harvey Harrison46b4f7c2008-04-08 23:20:00 -0300668 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300670 if (fepriv->thread) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (!fepriv->exit)
672 return 0;
673 else
674 dvb_frontend_stop (fe);
675 }
676
677 if (signal_pending(current))
678 return -EINTR;
679 if (down_interruptible (&fepriv->sem))
680 return -EINTR;
681
682 fepriv->state = FESTATE_IDLE;
683 fepriv->exit = 0;
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300684 fepriv->thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 mb();
686
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300687 fe_thread = kthread_run(dvb_frontend_thread, fe,
688 "kdvb-fe-%i", fe->dvb->num);
689 if (IS_ERR(fe_thread)) {
690 ret = PTR_ERR(fe_thread);
691 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 up(&fepriv->sem);
693 return ret;
694 }
Herbert Poetzl8eec1422007-02-08 14:32:43 -0300695 fepriv->thread = fe_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
697}
698
Oliver Endrissc471b332007-08-09 01:03:42 -0300699static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
700 u32 *freq_min, u32 *freq_max)
701{
702 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
703
704 if (fe->ops.info.frequency_max == 0)
705 *freq_max = fe->ops.tuner_ops.info.frequency_max;
706 else if (fe->ops.tuner_ops.info.frequency_max == 0)
707 *freq_max = fe->ops.info.frequency_max;
708 else
709 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
710
711 if (*freq_min == 0 || *freq_max == 0)
712 printk(KERN_WARNING "DVB: frontend %u frequency limits undefined - fix the driver\n",
713 fe->dvb->num);
714}
715
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300716static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
717 struct dvb_frontend_parameters *parms)
718{
Oliver Endrissc471b332007-08-09 01:03:42 -0300719 u32 freq_min;
720 u32 freq_max;
721
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300722 /* range check: frequency */
Oliver Endrissc471b332007-08-09 01:03:42 -0300723 dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
724 if ((freq_min && parms->frequency < freq_min) ||
725 (freq_max && parms->frequency > freq_max)) {
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300726 printk(KERN_WARNING "DVB: frontend %u frequency %u out of range (%u..%u)\n",
Oliver Endrissc471b332007-08-09 01:03:42 -0300727 fe->dvb->num, parms->frequency, freq_min, freq_max);
Oliver Endriss1fab46f2007-07-23 21:00:36 -0300728 return -EINVAL;
729 }
730
731 /* range check: symbol rate */
732 if (fe->ops.info.type == FE_QPSK) {
733 if ((fe->ops.info.symbol_rate_min &&
734 parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
735 (fe->ops.info.symbol_rate_max &&
736 parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
737 printk(KERN_WARNING "DVB: frontend %u symbol rate %u out of range (%u..%u)\n",
738 fe->dvb->num, parms->u.qpsk.symbol_rate,
739 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
740 return -EINVAL;
741 }
742
743 } else if (fe->ops.info.type == FE_QAM) {
744 if ((fe->ops.info.symbol_rate_min &&
745 parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
746 (fe->ops.info.symbol_rate_max &&
747 parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
748 printk(KERN_WARNING "DVB: frontend %u symbol rate %u out of range (%u..%u)\n",
749 fe->dvb->num, parms->u.qam.symbol_rate,
750 fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
751 return -EINVAL;
752 }
753 }
754
755 return 0;
756}
757
Steven Toth56f06802008-09-11 10:19:27 -0300758struct dtv_cmds_h dtv_cmds[] = {
759 [DTV_SEQ_UNDEFINED] = {
760 .name = "DTV_SEQ_UNDEFINED",
761 .cmd = DTV_SEQ_UNDEFINED,
Steven Toth6b73eea2008-09-04 01:12:25 -0300762 .set = 1,
763 },
Steven Toth56f06802008-09-11 10:19:27 -0300764 [DTV_SEQ_START] = {
765 .name = "DTV_SEQ_START",
766 .cmd = DTV_SEQ_START,
Steven Toth6b73eea2008-09-04 01:12:25 -0300767 .set = 1,
768 },
Steven Toth56f06802008-09-11 10:19:27 -0300769 [DTV_SEQ_CONTINUE] = {
770 .name = "DTV_SEQ_CONTINUE",
771 .cmd = DTV_SEQ_CONTINUE,
Steven Toth6b73eea2008-09-04 01:12:25 -0300772 .set = 1,
773 },
Steven Toth56f06802008-09-11 10:19:27 -0300774 [DTV_SEQ_COMPLETE] = {
775 .name = "DTV_SEQ_COMPLETE",
776 .cmd = DTV_SEQ_COMPLETE,
Steven Toth6b73eea2008-09-04 01:12:25 -0300777 .set = 1,
778 },
Steven Toth56f06802008-09-11 10:19:27 -0300779 [DTV_SEQ_TERMINATE] = {
780 .name = "DTV_SEQ_TERMINATE",
781 .cmd = DTV_SEQ_TERMINATE,
Steven Toth6b73eea2008-09-04 01:12:25 -0300782 .set = 1,
783 },
784
785 /* Set */
Steven Toth56f06802008-09-11 10:19:27 -0300786 [DTV_SET_FREQUENCY] = {
787 .name = "DTV_SET_FREQUENCY",
788 .cmd = DTV_SET_FREQUENCY,
Steven Toth6b73eea2008-09-04 01:12:25 -0300789 .set = 1,
790 },
Steven Toth56f06802008-09-11 10:19:27 -0300791 [DTV_SET_BANDWIDTH] = {
792 .name = "DTV_SET_BANDWIDTH",
793 .cmd = DTV_SET_BANDWIDTH,
Steven Toth6b73eea2008-09-04 01:12:25 -0300794 .set = 1,
795 },
Steven Toth56f06802008-09-11 10:19:27 -0300796 [DTV_SET_MODULATION] = {
797 .name = "DTV_SET_MODULATION",
798 .cmd = DTV_SET_MODULATION,
Steven Toth6b73eea2008-09-04 01:12:25 -0300799 .set = 1,
800 },
Steven Toth56f06802008-09-11 10:19:27 -0300801 [DTV_SET_INVERSION] = {
802 .name = "DTV_SET_INVERSION",
803 .cmd = DTV_SET_INVERSION,
Steven Toth6b73eea2008-09-04 01:12:25 -0300804 .set = 1,
805 },
Steven Toth56f06802008-09-11 10:19:27 -0300806 [DTV_SET_DISEQC_MASTER] = {
807 .name = "DTV_SET_DISEQC_MASTER",
808 .cmd = DTV_SET_DISEQC_MASTER,
Steven Toth6b73eea2008-09-04 01:12:25 -0300809 .set = 1,
810 .buffer = 1,
811 },
Steven Toth56f06802008-09-11 10:19:27 -0300812 [DTV_SET_SYMBOLRATE] = {
813 .name = "DTV_SET_SYMBOLRATE",
814 .cmd = DTV_SET_SYMBOLRATE,
Steven Toth6b73eea2008-09-04 01:12:25 -0300815 .set = 1,
816 },
Steven Toth56f06802008-09-11 10:19:27 -0300817 [DTV_SET_INNERFEC] = {
818 .name = "DTV_SET_INNERFEC",
819 .cmd = DTV_SET_INNERFEC,
Steven Toth6b73eea2008-09-04 01:12:25 -0300820 .set = 1,
821 },
Steven Toth56f06802008-09-11 10:19:27 -0300822 [DTV_SET_VOLTAGE] = {
823 .name = "DTV_SET_VOLTAGE",
824 .cmd = DTV_SET_VOLTAGE,
Steven Toth6b73eea2008-09-04 01:12:25 -0300825 .set = 1,
826 },
Steven Toth56f06802008-09-11 10:19:27 -0300827 [DTV_SET_TONE] = {
828 .name = "DTV_SET_TONE",
829 .cmd = DTV_SET_TONE,
Steven Toth6b73eea2008-09-04 01:12:25 -0300830 .set = 1,
831 },
Steven Toth56f06802008-09-11 10:19:27 -0300832 [DTV_SET_PILOT] = {
833 .name = "DTV_SET_PILOT",
834 .cmd = DTV_SET_PILOT,
Steven Toth6b73eea2008-09-04 01:12:25 -0300835 .set = 1,
836 },
Steven Toth56f06802008-09-11 10:19:27 -0300837 [DTV_SET_ROLLOFF] = {
838 .name = "DTV_SET_ROLLOFF",
839 .cmd = DTV_SET_ROLLOFF,
Steven Toth6b73eea2008-09-04 01:12:25 -0300840 .set = 1,
841 },
Steven Toth56f06802008-09-11 10:19:27 -0300842 [DTV_SET_DELIVERY_SYSTEM] = {
843 .name = "DTV_SET_DELIVERY_SYSTEM",
844 .cmd = DTV_SET_DELIVERY_SYSTEM,
Steven Toth6b73eea2008-09-04 01:12:25 -0300845 .set = 1,
846 },
Steven Toth56f06802008-09-11 10:19:27 -0300847 [DTV_SET_ISDB_SEGMENT_NUM] = {
848 .name = "DTV_SET_ISDB_SEGMENT_NUM",
849 .cmd = DTV_SET_ISDB_SEGMENT_NUM,
Steven Toth6b73eea2008-09-04 01:12:25 -0300850 .set = 1,
851 },
Steven Toth56f06802008-09-11 10:19:27 -0300852 [DTV_SET_ISDB_SEGMENT_WIDTH] = {
853 .name = "DTV_SET_ISDB_SEGMENT_WIDTH",
854 .cmd = DTV_SET_ISDB_SEGMENT_WIDTH,
Steven Toth6b73eea2008-09-04 01:12:25 -0300855 .set = 1,
856 },
857
858 /* Get */
Steven Toth56f06802008-09-11 10:19:27 -0300859 [DTV_GET_FREQUENCY] = {
860 .name = "DTV_GET_FREQUENCY",
861 .cmd = DTV_GET_FREQUENCY,
Steven Toth6b73eea2008-09-04 01:12:25 -0300862 .set = 0,
863 },
Steven Toth56f06802008-09-11 10:19:27 -0300864 [DTV_GET_BANDWIDTH] = {
865 .name = "DTV_GET_BANDWIDTH",
866 .cmd = DTV_GET_BANDWIDTH,
Steven Toth6b73eea2008-09-04 01:12:25 -0300867 .set = 0,
868 },
Steven Toth56f06802008-09-11 10:19:27 -0300869 [DTV_GET_MODULATION] = {
870 .name = "DTV_GET_MODULATION",
871 .cmd = DTV_GET_MODULATION,
Steven Toth6b73eea2008-09-04 01:12:25 -0300872 .set = 0,
873 },
Steven Toth56f06802008-09-11 10:19:27 -0300874 [DTV_GET_INVERSION] = {
875 .name = "DTV_GET_INVERSION",
876 .cmd = DTV_GET_INVERSION,
Steven Toth6b73eea2008-09-04 01:12:25 -0300877 .set = 0,
878 },
Steven Toth56f06802008-09-11 10:19:27 -0300879 [DTV_GET_DISEQC_SLAVE_REPLY] = {
880 .name = "DTV_GET_DISEQC_SLAVE_REPLY",
881 .cmd = DTV_GET_DISEQC_SLAVE_REPLY,
Steven Toth6b73eea2008-09-04 01:12:25 -0300882 .set = 0,
883 .buffer = 1,
884 },
Steven Toth56f06802008-09-11 10:19:27 -0300885 [DTV_GET_SYMBOLRATE] = {
886 .name = "DTV_GET_SYMBOLRATE",
887 .cmd = DTV_GET_SYMBOLRATE,
Steven Toth6b73eea2008-09-04 01:12:25 -0300888 .set = 0,
889 },
Steven Toth56f06802008-09-11 10:19:27 -0300890 [DTV_GET_INNERFEC] = {
891 .name = "DTV_GET_INNERFEC",
892 .cmd = DTV_GET_INNERFEC,
Steven Toth6b73eea2008-09-04 01:12:25 -0300893 .set = 0,
894 },
Steven Toth56f06802008-09-11 10:19:27 -0300895 [DTV_GET_VOLTAGE] = {
896 .name = "DTV_GET_VOLTAGE",
897 .cmd = DTV_GET_VOLTAGE,
Steven Toth6b73eea2008-09-04 01:12:25 -0300898 .set = 0,
899 },
Steven Toth56f06802008-09-11 10:19:27 -0300900 [DTV_GET_TONE] = {
901 .name = "DTV_GET_TONE",
902 .cmd = DTV_GET_TONE,
Steven Toth6b73eea2008-09-04 01:12:25 -0300903 .set = 0,
904 },
Steven Toth56f06802008-09-11 10:19:27 -0300905 [DTV_GET_PILOT] = {
906 .name = "DTV_GET_PILOT",
907 .cmd = DTV_GET_PILOT,
Steven Toth6b73eea2008-09-04 01:12:25 -0300908 .set = 0,
909 },
Steven Toth56f06802008-09-11 10:19:27 -0300910 [DTV_GET_ROLLOFF] = {
911 .name = "DTV_GET_ROLLOFF",
912 .cmd = DTV_GET_ROLLOFF,
Steven Toth6b73eea2008-09-04 01:12:25 -0300913 .set = 0,
914 },
Steven Toth56f06802008-09-11 10:19:27 -0300915 [DTV_GET_DELIVERY_SYSTEM] = {
916 .name = "DTV_GET_DELIVERY_SYSTEM",
917 .cmd = DTV_GET_DELIVERY_SYSTEM,
Steven Toth6b73eea2008-09-04 01:12:25 -0300918 .set = 0,
919 },
Steven Toth56f06802008-09-11 10:19:27 -0300920 [DTV_GET_ISDB_SEGMENT_NUM] = {
921 .name = "DTV_GET_ISDB_SEGMENT_NUM",
922 .cmd = DTV_GET_ISDB_SEGMENT_NUM,
Steven Toth6b73eea2008-09-04 01:12:25 -0300923 .set = 0,
924 },
Steven Toth56f06802008-09-11 10:19:27 -0300925 [DTV_GET_ISDB_SEGMENT_WIDTH] = {
926 .name = "DTV_GET_ISDB_SEGMENT_WIDTH",
927 .cmd = DTV_GET_ISDB_SEGMENT_WIDTH,
Steven Toth6b73eea2008-09-04 01:12:25 -0300928 .set = 0,
929 },
Steven Toth56f06802008-09-11 10:19:27 -0300930 [DTV_GET_ISDB_LAYERA_FEC] = {
931 .name = "DTV_GET_ISDB_LAYERA_FEC",
932 .cmd = DTV_GET_ISDB_LAYERA_FEC,
Steven Toth6b73eea2008-09-04 01:12:25 -0300933 .set = 0,
934 },
Steven Toth56f06802008-09-11 10:19:27 -0300935 [DTV_GET_ISDB_LAYERA_MODULATION] = {
936 .name = "DTV_GET_ISDB_LAYERA_MODULATION",
937 .cmd = DTV_GET_ISDB_LAYERA_MODULATION,
Steven Toth6b73eea2008-09-04 01:12:25 -0300938 .set = 0,
939 },
Steven Toth56f06802008-09-11 10:19:27 -0300940 [DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH] = {
941 .name = "DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH",
942 .cmd = DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH,
Steven Toth6b73eea2008-09-04 01:12:25 -0300943 .set = 0,
944 },
Steven Toth56f06802008-09-11 10:19:27 -0300945 [DTV_GET_ISDB_LAYERB_FEC] = {
946 .name = "DTV_GET_ISDB_LAYERB_FEC",
947 .cmd = DTV_GET_ISDB_LAYERB_FEC,
Steven Toth6b73eea2008-09-04 01:12:25 -0300948 .set = 0,
949 },
Steven Toth56f06802008-09-11 10:19:27 -0300950 [DTV_GET_ISDB_LAYERB_MODULATION] = {
951 .name = "DTV_GET_ISDB_LAYERB_MODULATION",
952 .cmd = DTV_GET_ISDB_LAYERB_MODULATION,
Steven Toth6b73eea2008-09-04 01:12:25 -0300953 .set = 0,
954 },
Steven Toth56f06802008-09-11 10:19:27 -0300955 [DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH] = {
956 .name = "DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH",
957 .cmd = DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH,
Steven Toth6b73eea2008-09-04 01:12:25 -0300958 .set = 0,
959 },
Steven Toth56f06802008-09-11 10:19:27 -0300960 [DTV_GET_ISDB_LAYERC_FEC] = {
961 .name = "DTV_GET_ISDB_LAYERC_FEC",
962 .cmd = DTV_GET_ISDB_LAYERC_FEC,
Steven Toth6b73eea2008-09-04 01:12:25 -0300963 .set = 0,
964 },
Steven Toth56f06802008-09-11 10:19:27 -0300965 [DTV_GET_ISDB_LAYERC_MODULATION] = {
966 .name = "DTV_GET_ISDB_LAYERC_MODULATION",
967 .cmd = DTV_GET_ISDB_LAYERC_MODULATION,
Steven Toth6b73eea2008-09-04 01:12:25 -0300968 .set = 0,
969 },
Steven Toth56f06802008-09-11 10:19:27 -0300970 [DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH] = {
971 .name = "DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH",
972 .cmd = DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH,
Steven Toth6b73eea2008-09-04 01:12:25 -0300973 .set = 0,
974 },
975};
976
Steven Toth56f06802008-09-11 10:19:27 -0300977void dtv_property_dump(dtv_property_t *tvp)
Steven Toth6b73eea2008-09-04 01:12:25 -0300978{
979 int i;
980
981 printk("%s() tvp.cmd = 0x%08x (%s)\n"
982 ,__FUNCTION__
983 ,tvp->cmd
Steven Toth56f06802008-09-11 10:19:27 -0300984 ,dtv_cmds[ tvp->cmd ].name);
Steven Toth6b73eea2008-09-04 01:12:25 -0300985
Steven Toth56f06802008-09-11 10:19:27 -0300986 if(dtv_cmds[ tvp->cmd ].buffer) {
Steven Toth6b73eea2008-09-04 01:12:25 -0300987
988 printk("%s() tvp.u.buffer.len = 0x%02x\n"
989 ,__FUNCTION__
990 ,tvp->u.buffer.len);
991
992 for(i = 0; i < tvp->u.buffer.len; i++)
993 printk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
994 ,__FUNCTION__
995 ,i
996 ,tvp->u.buffer.data[i]);
997
998 } else
999 printk("%s() tvp.u.data = 0x%08x\n", __FUNCTION__, tvp->u.data);
1000}
1001
1002int is_legacy_delivery_system(fe_delivery_system_t s)
1003{
1004 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
1005 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS))
1006 return 1;
1007
1008 return 0;
1009}
1010
Steven Toth56f06802008-09-11 10:19:27 -03001011int dtv_property_cache_submit(struct dvb_frontend *fe)
Steven Toth6b73eea2008-09-04 01:12:25 -03001012{
1013
1014 /* We have to do one of two things:
1015 * To support legacy devices using the new API we take values from
1016 * the tv_cache and generate a legacy truning structure.
1017 *
1018 * Or,
1019 *
1020 * To support advanced tuning devices with the new API we
1021 * notify the new advance driver type that a tuning operation is required
1022 * and let it pull values from the cache as is, we don't need to
1023 * pass structures.
1024 *
1025 * We'll use the modulation type to assess how this is handled. as the API
1026 * progresses we'll probably want to have a flag in dvb_frontend_ops
1027 * to allow the frontend driver to dictate how it likes to be tuned.
1028 *
1029 * Because of how this is attached to the ioctl handler for legacy support,
1030 * it's important to return an appropriate result code with atleast the following
1031 * three meanings:
1032 * < 0 = processing error
1033 * 0 = lecagy ioctl handler to submit a traditional set_frontend() call.
1034 * 1 = lecagy ioctl handler should NOT submit a traditional set_frontend() call.
1035 */
1036
1037 int r;
1038
Steven Toth56f06802008-09-11 10:19:27 -03001039 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Steven Toth6b73eea2008-09-04 01:12:25 -03001040 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1041 struct dvb_frontend_parameters p;
1042
1043 printk("%s()\n", __FUNCTION__);
1044
1045 /* For legacy delivery systems we don't need the delivery_system to be specified */
1046 if(is_legacy_delivery_system(c->delivery_system)) {
1047 switch(c->modulation) {
1048 case QPSK:
1049 printk("%s() Preparing QPSK req\n", __FUNCTION__);
1050 p.frequency = c->frequency;
1051 p.inversion = c->inversion;
1052 p.u.qpsk.symbol_rate = c->symbol_rate;
1053 p.u.qpsk.fec_inner = c->fec_inner;
1054 memcpy(&fepriv->parameters, &p,
1055 sizeof (struct dvb_frontend_parameters));
1056
1057 /* Call the traditional tuning mechanisms. */
1058
1059 r = 0;
1060 break;
1061 case QAM_16:
1062 case QAM_32:
1063 case QAM_64:
1064 case QAM_128:
1065 case QAM_256:
1066 case QAM_AUTO:
1067 printk("%s() Preparing QAM req\n", __FUNCTION__);
1068 p.frequency = c->frequency;
1069 p.inversion = c->inversion;
1070 p.u.qam.symbol_rate = c->symbol_rate;
1071 p.u.vsb.modulation = c->modulation;
1072 printk("%s() frequency = %d\n", __FUNCTION__, p.frequency);
1073 printk("%s() QAM = %d\n", __FUNCTION__, p.u.vsb.modulation);
1074 memcpy(&fepriv->parameters, &p,
1075 sizeof (struct dvb_frontend_parameters));
1076
1077 /* At this point we're fully formed for backwards
1078 * compatability and we need to return this
1079 * via the ioctl handler as SET_FRONTEND (arg).
1080 * We've already patched the new values into the
1081 * frontends tuning structures so the ioctl code just
1082 * continues as if a legacy tune structure was passed
1083 * from userspace.
1084 */
1085
1086 r = 0;
1087 break;
1088 case VSB_8:
1089 case VSB_16:
1090 printk("%s() Preparing VSB req\n", __FUNCTION__);
1091 p.frequency = c->frequency;
1092 p.u.vsb.modulation = c->modulation;
1093 memcpy(&fepriv->parameters, &p,
1094 sizeof (struct dvb_frontend_parameters));
1095
1096 /* Call the traditional tuning mechanisms. */
1097
1098 r = 0;
1099 break;
1100 /* TODO: Add any missing modulation types */
1101 default:
1102 r = -1;
1103 }
1104 } else {
1105 /* For advanced delivery systems / modulation types ...
1106 * we seed the lecacy dvb_frontend_parameters structure
1107 * so that the sanity checking code later in the IOCTL processing
1108 * can validate our basic frequency ranges, symbolrates, modulation
1109 * etc.
1110 */
1111 r = -1;
1112
1113 switch(c->modulation) {
1114 case _8PSK:
1115 case _16APSK:
1116 case NBC_QPSK:
1117 /* Just post a notification to the demod driver and let it pull
Steven Toth56f06802008-09-11 10:19:27 -03001118 * the specific values it wants from its dtv_property_cache.
Steven Toth6b73eea2008-09-04 01:12:25 -03001119 * It can decide how best to use those parameters.
1120 * IOCTL will call set_frontend (by default) due to zigzag
1121 * support etc.
1122 */
1123 if (fe->ops.set_params)
1124 r = fe->ops.set_params(fe);
1125
1126 p.frequency = c->frequency;
1127 p.inversion = c->inversion;
1128 p.u.qpsk.symbol_rate = c->symbol_rate;
1129 p.u.qpsk.fec_inner = c->fec_inner;
1130 memcpy(&fepriv->parameters, &p,
1131 sizeof (struct dvb_frontend_parameters));
1132
1133 r = 0;
1134 break;
1135 default:
1136 r = -1;
1137 }
1138
1139 if(c->delivery_system == SYS_ISDBT) {
1140 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1141 p.frequency = c->frequency;
1142 p.inversion = INVERSION_AUTO;
1143 p.u.ofdm.constellation = QAM_AUTO;
1144 p.u.ofdm.code_rate_HP = FEC_AUTO;
1145 p.u.ofdm.code_rate_LP = FEC_AUTO;
1146 p.u.ofdm.bandwidth = BANDWIDTH_AUTO;
1147 p.u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1148 p.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1149 p.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1150 memcpy(&fepriv->parameters, &p,
1151 sizeof (struct dvb_frontend_parameters));
1152
1153 r = 0;
1154 }
1155 }
1156 return r;
1157}
1158
Steven Toth13c97bf2008-09-04 21:19:43 -03001159static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1160 unsigned int cmd, void *parg);
1161static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1162 unsigned int cmd, void *parg);
1163
Steven Toth56f06802008-09-11 10:19:27 -03001164int dtv_property_process(struct dvb_frontend *fe, dtv_property_t *tvp,
Steven Toth13c97bf2008-09-04 21:19:43 -03001165 struct inode *inode, struct file *file)
Steven Toth6b73eea2008-09-04 01:12:25 -03001166{
1167 int r = 0;
Steven Toth13c97bf2008-09-04 21:19:43 -03001168 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Steven Toth6b73eea2008-09-04 01:12:25 -03001169 printk("%s()\n", __FUNCTION__);
Steven Toth56f06802008-09-11 10:19:27 -03001170 dtv_property_dump(tvp);
Steven Toth6b73eea2008-09-04 01:12:25 -03001171
1172 switch(tvp->cmd) {
Steven Toth56f06802008-09-11 10:19:27 -03001173 case DTV_SEQ_START:
1174 case DTV_SEQ_TERMINATE:
Steven Toth6b73eea2008-09-04 01:12:25 -03001175 /* Reset a cache of data specific to the frontend here. This does
1176 * not effect hardware.
1177 */
1178 printk("%s() Flushing property cache\n", __FUNCTION__);
Steven Toth56f06802008-09-11 10:19:27 -03001179 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
1180 fe->dtv_property_cache.state = DTV_SEQ_START;
1181 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
Steven Toth6b73eea2008-09-04 01:12:25 -03001182 break;
Steven Toth56f06802008-09-11 10:19:27 -03001183 case DTV_SEQ_COMPLETE:
Steven Toth6b73eea2008-09-04 01:12:25 -03001184 /* interpret the cache of data, build either a traditional frontend
1185 * tunerequest and submit it to a subset of the ioctl handler,
1186 * or, call a new undefined method on the frontend to deal with
1187 * all new tune requests.
1188 */
Steven Toth56f06802008-09-11 10:19:27 -03001189 fe->dtv_property_cache.state = DTV_SEQ_COMPLETE;
Steven Toth6b73eea2008-09-04 01:12:25 -03001190 printk("%s() Finalised property cache\n", __FUNCTION__);
Steven Toth56f06802008-09-11 10:19:27 -03001191 r |= dtv_property_cache_submit(fe);
Steven Toth13c97bf2008-09-04 21:19:43 -03001192 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1193 &fepriv->parameters);
Steven Toth6b73eea2008-09-04 01:12:25 -03001194 break;
Steven Toth56f06802008-09-11 10:19:27 -03001195 case DTV_SET_FREQUENCY:
1196 fe->dtv_property_cache.frequency = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001197 break;
Steven Toth56f06802008-09-11 10:19:27 -03001198 case DTV_GET_FREQUENCY:
1199 tvp->u.data = fe->dtv_property_cache.frequency;
Steven Toth6b73eea2008-09-04 01:12:25 -03001200 break;
Steven Toth56f06802008-09-11 10:19:27 -03001201 case DTV_SET_MODULATION:
1202 fe->dtv_property_cache.modulation = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001203 break;
Steven Toth56f06802008-09-11 10:19:27 -03001204 case DTV_GET_MODULATION:
1205 tvp->u.data = fe->dtv_property_cache.modulation;
Steven Toth6b73eea2008-09-04 01:12:25 -03001206 break;
Steven Toth56f06802008-09-11 10:19:27 -03001207 case DTV_SET_BANDWIDTH:
1208 fe->dtv_property_cache.bandwidth = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001209 break;
Steven Toth56f06802008-09-11 10:19:27 -03001210 case DTV_GET_BANDWIDTH:
1211 tvp->u.data = fe->dtv_property_cache.bandwidth;
Steven Toth6b73eea2008-09-04 01:12:25 -03001212 break;
Steven Toth56f06802008-09-11 10:19:27 -03001213 case DTV_SET_INVERSION:
1214 fe->dtv_property_cache.inversion = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001215 break;
Steven Toth56f06802008-09-11 10:19:27 -03001216 case DTV_GET_INVERSION:
1217 tvp->u.data = fe->dtv_property_cache.inversion;
Steven Toth6b73eea2008-09-04 01:12:25 -03001218 break;
Steven Toth56f06802008-09-11 10:19:27 -03001219 case DTV_SET_SYMBOLRATE:
1220 fe->dtv_property_cache.symbol_rate = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001221 break;
Steven Toth56f06802008-09-11 10:19:27 -03001222 case DTV_GET_SYMBOLRATE:
1223 tvp->u.data = fe->dtv_property_cache.symbol_rate;
Steven Toth6b73eea2008-09-04 01:12:25 -03001224 break;
Steven Toth56f06802008-09-11 10:19:27 -03001225 case DTV_SET_INNERFEC:
1226 fe->dtv_property_cache.fec_inner = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001227 break;
Steven Toth56f06802008-09-11 10:19:27 -03001228 case DTV_GET_INNERFEC:
1229 tvp->u.data = fe->dtv_property_cache.fec_inner;
Steven Toth6b73eea2008-09-04 01:12:25 -03001230 break;
Steven Toth56f06802008-09-11 10:19:27 -03001231 case DTV_SET_PILOT:
1232 fe->dtv_property_cache.pilot = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001233 break;
Steven Toth56f06802008-09-11 10:19:27 -03001234 case DTV_GET_PILOT:
1235 tvp->u.data = fe->dtv_property_cache.pilot;
Steven Toth6b73eea2008-09-04 01:12:25 -03001236 break;
Steven Toth56f06802008-09-11 10:19:27 -03001237 case DTV_SET_ROLLOFF:
1238 fe->dtv_property_cache.rolloff = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001239 break;
Steven Toth56f06802008-09-11 10:19:27 -03001240 case DTV_GET_ROLLOFF:
1241 tvp->u.data = fe->dtv_property_cache.rolloff;
Steven Toth6b73eea2008-09-04 01:12:25 -03001242 break;
Steven Toth56f06802008-09-11 10:19:27 -03001243 case DTV_SET_DELIVERY_SYSTEM:
1244 fe->dtv_property_cache.delivery_system = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001245 break;
Steven Toth56f06802008-09-11 10:19:27 -03001246 case DTV_GET_DELIVERY_SYSTEM:
1247 tvp->u.data = fe->dtv_property_cache.delivery_system;
Steven Toth6b73eea2008-09-04 01:12:25 -03001248 break;
1249
1250 /* ISDB-T Support here */
Steven Toth56f06802008-09-11 10:19:27 -03001251 case DTV_SET_ISDB_SEGMENT_NUM:
1252 fe->dtv_property_cache.isdb_segment_num = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001253 break;
Steven Toth56f06802008-09-11 10:19:27 -03001254 case DTV_GET_ISDB_SEGMENT_NUM:
1255 tvp->u.data = fe->dtv_property_cache.isdb_segment_num;
Steven Toth6b73eea2008-09-04 01:12:25 -03001256 break;
Steven Toth56f06802008-09-11 10:19:27 -03001257 case DTV_SET_ISDB_SEGMENT_WIDTH:
1258 fe->dtv_property_cache.isdb_segment_width = tvp->u.data;
Steven Toth6b73eea2008-09-04 01:12:25 -03001259 break;
Steven Toth56f06802008-09-11 10:19:27 -03001260 case DTV_GET_ISDB_SEGMENT_WIDTH:
1261 tvp->u.data = fe->dtv_property_cache.isdb_segment_width;
Steven Toth6b73eea2008-09-04 01:12:25 -03001262 break;
Steven Toth56f06802008-09-11 10:19:27 -03001263 case DTV_GET_ISDB_LAYERA_FEC:
1264 tvp->u.data = fe->dtv_property_cache.isdb_layera_fec;
Steven Toth6b73eea2008-09-04 01:12:25 -03001265 break;
Steven Toth56f06802008-09-11 10:19:27 -03001266 case DTV_GET_ISDB_LAYERA_MODULATION:
1267 tvp->u.data = fe->dtv_property_cache.isdb_layera_modulation;
Steven Toth6b73eea2008-09-04 01:12:25 -03001268 break;
Steven Toth56f06802008-09-11 10:19:27 -03001269 case DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH:
1270 tvp->u.data = fe->dtv_property_cache.isdb_layera_segment_width;
Steven Toth6b73eea2008-09-04 01:12:25 -03001271 break;
Steven Toth56f06802008-09-11 10:19:27 -03001272 case DTV_GET_ISDB_LAYERB_FEC:
1273 tvp->u.data = fe->dtv_property_cache.isdb_layerb_fec;
Steven Toth6b73eea2008-09-04 01:12:25 -03001274 break;
Steven Toth56f06802008-09-11 10:19:27 -03001275 case DTV_GET_ISDB_LAYERB_MODULATION:
1276 tvp->u.data = fe->dtv_property_cache.isdb_layerb_modulation;
Steven Toth6b73eea2008-09-04 01:12:25 -03001277 break;
Steven Toth56f06802008-09-11 10:19:27 -03001278 case DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH:
1279 tvp->u.data = fe->dtv_property_cache.isdb_layerb_segment_width;
Steven Toth6b73eea2008-09-04 01:12:25 -03001280 break;
Steven Toth56f06802008-09-11 10:19:27 -03001281 case DTV_GET_ISDB_LAYERC_FEC:
1282 tvp->u.data = fe->dtv_property_cache.isdb_layerc_fec;
Steven Toth6b73eea2008-09-04 01:12:25 -03001283 break;
Steven Toth56f06802008-09-11 10:19:27 -03001284 case DTV_GET_ISDB_LAYERC_MODULATION:
1285 tvp->u.data = fe->dtv_property_cache.isdb_layerc_modulation;
Steven Toth6b73eea2008-09-04 01:12:25 -03001286 break;
Steven Toth56f06802008-09-11 10:19:27 -03001287 case DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH:
1288 tvp->u.data = fe->dtv_property_cache.isdb_layerc_segment_width;
Steven Toth6b73eea2008-09-04 01:12:25 -03001289 break;
Steven Toth56f06802008-09-11 10:19:27 -03001290 case DTV_SET_VOLTAGE:
1291 fe->dtv_property_cache.voltage = tvp->u.data;
Steven Toth13c97bf2008-09-04 21:19:43 -03001292 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
Steven Toth56f06802008-09-11 10:19:27 -03001293 (void *)fe->dtv_property_cache.voltage);
Steven Toth13c97bf2008-09-04 21:19:43 -03001294 break;
Steven Toth56f06802008-09-11 10:19:27 -03001295 case DTV_GET_VOLTAGE:
1296 tvp->u.data = fe->dtv_property_cache.voltage;
Steven Toth13c97bf2008-09-04 21:19:43 -03001297 break;
Steven Toth56f06802008-09-11 10:19:27 -03001298 case DTV_SET_TONE:
1299 fe->dtv_property_cache.sectone = tvp->u.data;
Steven Toth13c97bf2008-09-04 21:19:43 -03001300 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
Steven Toth56f06802008-09-11 10:19:27 -03001301 (void *)fe->dtv_property_cache.sectone);
Steven Toth13c97bf2008-09-04 21:19:43 -03001302 break;
Steven Toth56f06802008-09-11 10:19:27 -03001303 case DTV_GET_TONE:
1304 tvp->u.data = fe->dtv_property_cache.sectone;
Steven Toth13c97bf2008-09-04 21:19:43 -03001305 break;
Steven Toth6b73eea2008-09-04 01:12:25 -03001306 }
1307
Steven Toth13c97bf2008-09-04 21:19:43 -03001308 return r;
Steven Toth6b73eea2008-09-04 01:12:25 -03001309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1312 unsigned int cmd, void *parg)
1313{
1314 struct dvb_device *dvbdev = file->private_data;
1315 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001316 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int err = -EOPNOTSUPP;
1318
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001319 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Adrian Bunk813ce472007-08-01 10:13:36 -03001321 if (fepriv->exit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return -ENODEV;
1323
1324 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1325 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1326 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1327 return -EPERM;
1328
1329 if (down_interruptible (&fepriv->sem))
1330 return -ERESTARTSYS;
1331
Steven Toth13c97bf2008-09-04 21:19:43 -03001332 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1333 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1334 else
1335 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1336
1337 up(&fepriv->sem);
1338 return err;
1339}
1340
1341static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1342 unsigned int cmd, void *parg)
1343{
1344 struct dvb_device *dvbdev = file->private_data;
1345 struct dvb_frontend *fe = dvbdev->priv;
1346 int err = -EOPNOTSUPP;
Steven Toth56f06802008-09-11 10:19:27 -03001347 dtv_property_t *tvp;
Steven Toth13c97bf2008-09-04 21:19:43 -03001348
1349 dprintk("%s\n", __func__);
1350
Steven Toth6b73eea2008-09-04 01:12:25 -03001351 if(cmd == FE_SET_PROPERTY) {
1352 printk("%s() FE_SET_PROPERTY\n", __FUNCTION__);
1353
1354 /* TODO: basic property validation here */
1355
1356 /* TODO: ioctl userdata out of range check here */
1357 tvp = parg;
Steven Toth56f06802008-09-11 10:19:27 -03001358 while(tvp->cmd != DTV_SEQ_UNDEFINED) {
1359 dtv_property_process(fe, tvp, inode, file);
1360 if( (tvp->cmd == DTV_SEQ_TERMINATE) || (tvp->cmd == DTV_SEQ_COMPLETE) )
Steven Toth6b73eea2008-09-04 01:12:25 -03001361 break;
1362 tvp++;
1363 }
1364
Steven Toth56f06802008-09-11 10:19:27 -03001365 if(fe->dtv_property_cache.state == DTV_SEQ_COMPLETE) {
Steven Toth6b73eea2008-09-04 01:12:25 -03001366 printk("%s() Property cache is full, tuning\n", __FUNCTION__);
Steven Toth6b73eea2008-09-04 01:12:25 -03001367 }
1368 err = 0;
1369 }
1370
Steven Toth13c97bf2008-09-04 21:19:43 -03001371 return err;
1372}
1373
1374static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1375 unsigned int cmd, void *parg)
1376{
1377 struct dvb_device *dvbdev = file->private_data;
1378 struct dvb_frontend *fe = dvbdev->priv;
1379 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1380 int err = -EOPNOTSUPP;
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 switch (cmd) {
1383 case FE_GET_INFO: {
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001384 struct dvb_frontend_info* info = parg;
Patrick Boettcherdea74862006-05-14 05:01:31 -03001385 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
Oliver Endrissc471b332007-08-09 01:03:42 -03001386 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1389 * do it, it is done for it. */
1390 info->caps |= FE_CAN_INVERSION_AUTO;
1391 err = 0;
1392 break;
1393 }
1394
Peter Beutner6757ccc2005-07-07 17:57:36 -07001395 case FE_READ_STATUS: {
1396 fe_status_t* status = parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Peter Beutner6757ccc2005-07-07 17:57:36 -07001398 /* if retune was requested but hasn't occured yet, prevent
1399 * that user get signal state from previous tuning */
1400 if(fepriv->state == FESTATE_RETUNE) {
1401 err=0;
1402 *status = 0;
1403 break;
1404 }
1405
Patrick Boettcherdea74862006-05-14 05:01:31 -03001406 if (fe->ops.read_status)
1407 err = fe->ops.read_status(fe, status);
Peter Beutner6757ccc2005-07-07 17:57:36 -07001408 break;
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 case FE_READ_BER:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001411 if (fe->ops.read_ber)
1412 err = fe->ops.read_ber(fe, (__u32*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414
1415 case FE_READ_SIGNAL_STRENGTH:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001416 if (fe->ops.read_signal_strength)
1417 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 break;
1419
1420 case FE_READ_SNR:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001421 if (fe->ops.read_snr)
1422 err = fe->ops.read_snr(fe, (__u16*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 break;
1424
1425 case FE_READ_UNCORRECTED_BLOCKS:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001426 if (fe->ops.read_ucblocks)
1427 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 break;
1429
1430
1431 case FE_DISEQC_RESET_OVERLOAD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001432 if (fe->ops.diseqc_reset_overload) {
1433 err = fe->ops.diseqc_reset_overload(fe);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 fepriv->state = FESTATE_DISEQC;
1435 fepriv->status = 0;
1436 }
1437 break;
1438
1439 case FE_DISEQC_SEND_MASTER_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001440 if (fe->ops.diseqc_send_master_cmd) {
1441 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 fepriv->state = FESTATE_DISEQC;
1443 fepriv->status = 0;
1444 }
1445 break;
1446
1447 case FE_DISEQC_SEND_BURST:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001448 if (fe->ops.diseqc_send_burst) {
1449 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 fepriv->state = FESTATE_DISEQC;
1451 fepriv->status = 0;
1452 }
1453 break;
1454
1455 case FE_SET_TONE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001456 if (fe->ops.set_tone) {
1457 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03001458 fepriv->tone = (fe_sec_tone_mode_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 fepriv->state = FESTATE_DISEQC;
1460 fepriv->status = 0;
1461 }
1462 break;
1463
1464 case FE_SET_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001465 if (fe->ops.set_voltage) {
1466 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
Andrew de Quincey64454012006-04-06 14:32:23 -03001467 fepriv->voltage = (fe_sec_voltage_t) parg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 fepriv->state = FESTATE_DISEQC;
1469 fepriv->status = 0;
1470 }
1471 break;
1472
1473 case FE_DISHNETWORK_SEND_LEGACY_CMD:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001474 if (fe->ops.dishnetwork_send_legacy_command) {
1475 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 fepriv->state = FESTATE_DISEQC;
1477 fepriv->status = 0;
Patrick Boettcherdea74862006-05-14 05:01:31 -03001478 } else if (fe->ops.set_voltage) {
NooneImportant83b75b02005-11-08 21:35:27 -08001479 /*
1480 * NOTE: This is a fallback condition. Some frontends
1481 * (stv0299 for instance) take longer than 8msec to
1482 * respond to a set_voltage command. Those switches
1483 * need custom routines to switch properly. For all
1484 * other frontends, the following shoule work ok.
1485 * Dish network legacy switches (as used by Dish500)
1486 * are controlled by sending 9-bit command words
1487 * spaced 8msec apart.
1488 * the actual command word is switch/port dependant
1489 * so it is up to the userspace application to send
1490 * the right command.
1491 * The command must always start with a '0' after
1492 * initialization, so parg is 8 bits and does not
1493 * include the initialization or start bit
1494 */
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001495 unsigned long swcmd = ((unsigned long) parg) << 1;
NooneImportant83b75b02005-11-08 21:35:27 -08001496 struct timeval nexttime;
1497 struct timeval tv[10];
1498 int i;
1499 u8 last = 1;
1500 if (dvb_frontend_debug)
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001501 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
NooneImportant83b75b02005-11-08 21:35:27 -08001502 do_gettimeofday(&nexttime);
1503 if (dvb_frontend_debug)
1504 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1505 /* before sending a command, initialize by sending
1506 * a 32ms 18V to the switch
1507 */
Patrick Boettcherdea74862006-05-14 05:01:31 -03001508 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08001509 dvb_frontend_sleep_until(&nexttime, 32000);
1510
1511 for (i = 0; i < 9; i++) {
1512 if (dvb_frontend_debug)
1513 do_gettimeofday(&tv[i + 1]);
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001514 if ((swcmd & 0x01) != last) {
NooneImportant83b75b02005-11-08 21:35:27 -08001515 /* set voltage to (last ? 13V : 18V) */
Patrick Boettcherdea74862006-05-14 05:01:31 -03001516 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
NooneImportant83b75b02005-11-08 21:35:27 -08001517 last = (last) ? 0 : 1;
1518 }
Hans Verkuilc6eb8ea2008-09-03 17:11:54 -03001519 swcmd = swcmd >> 1;
NooneImportant83b75b02005-11-08 21:35:27 -08001520 if (i != 8)
1521 dvb_frontend_sleep_until(&nexttime, 8000);
1522 }
1523 if (dvb_frontend_debug) {
1524 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001525 __func__, fe->dvb->num);
NooneImportant83b75b02005-11-08 21:35:27 -08001526 for (i = 1; i < 10; i++)
1527 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1528 }
1529 err = 0;
1530 fepriv->state = FESTATE_DISEQC;
1531 fepriv->status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533 break;
1534
1535 case FE_DISEQC_RECV_SLAVE_REPLY:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001536 if (fe->ops.diseqc_recv_slave_reply)
1537 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 break;
1539
1540 case FE_ENABLE_HIGH_LNB_VOLTAGE:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001541 if (fe->ops.enable_high_lnb_voltage)
1542 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 break;
1544
1545 case FE_SET_FRONTEND: {
1546 struct dvb_frontend_tune_settings fetunesettings;
1547
Steven Toth56f06802008-09-11 10:19:27 -03001548 if(fe->dtv_property_cache.state == DTV_SEQ_COMPLETE) {
Steven Toth6b73eea2008-09-04 01:12:25 -03001549 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1550 err = -EINVAL;
1551 break;
1552 }
1553 } else {
1554 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1555 err = -EINVAL;
1556 break;
1557 }
Oliver Endriss1fab46f2007-07-23 21:00:36 -03001558
Steven Toth6b73eea2008-09-04 01:12:25 -03001559 memcpy (&fepriv->parameters, parg,
1560 sizeof (struct dvb_frontend_parameters));
1561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1564 memcpy(&fetunesettings.parameters, parg,
1565 sizeof (struct dvb_frontend_parameters));
1566
1567 /* force auto frequency inversion if requested */
1568 if (dvb_force_auto_inversion) {
1569 fepriv->parameters.inversion = INVERSION_AUTO;
1570 fetunesettings.parameters.inversion = INVERSION_AUTO;
1571 }
Patrick Boettcherdea74862006-05-14 05:01:31 -03001572 if (fe->ops.info.type == FE_OFDM) {
Uwe Kleine-König1b3c3712007-02-17 19:23:03 +01001573 /* without hierarchical coding code_rate_LP is irrelevant,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 * so we tolerate the otherwise invalid FEC_NONE setting */
1575 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1576 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1577 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1578 }
1579
1580 /* get frontend-specific tuning settings */
Patrick Boettcherdea74862006-05-14 05:01:31 -03001581 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1583 fepriv->max_drift = fetunesettings.max_drift;
1584 fepriv->step_size = fetunesettings.step_size;
1585 } else {
1586 /* default values */
Patrick Boettcherdea74862006-05-14 05:01:31 -03001587 switch(fe->ops.info.type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 case FE_QPSK:
1589 fepriv->min_delay = HZ/20;
1590 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1591 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1592 break;
1593
1594 case FE_QAM:
1595 fepriv->min_delay = HZ/20;
1596 fepriv->step_size = 0; /* no zigzag */
1597 fepriv->max_drift = 0;
1598 break;
1599
1600 case FE_OFDM:
1601 fepriv->min_delay = HZ/20;
Patrick Boettcherdea74862006-05-14 05:01:31 -03001602 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1603 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 break;
1605 case FE_ATSC:
Manu Abraham4821fb12006-06-21 10:27:00 -03001606 fepriv->min_delay = HZ/20;
1607 fepriv->step_size = 0;
1608 fepriv->max_drift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 break;
1610 }
1611 }
1612 if (dvb_override_tune_delay > 0)
1613 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1614
1615 fepriv->state = FESTATE_RETUNE;
1616 dvb_frontend_wakeup(fe);
1617 dvb_frontend_add_event(fe, 0);
1618 fepriv->status = 0;
1619 err = 0;
1620 break;
1621 }
1622
1623 case FE_GET_EVENT:
1624 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1625 break;
1626
1627 case FE_GET_FRONTEND:
Patrick Boettcherdea74862006-05-14 05:01:31 -03001628 if (fe->ops.get_frontend) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
Patrick Boettcherdea74862006-05-14 05:01:31 -03001630 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
1632 break;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02001633
1634 case FE_SET_FRONTEND_TUNE_MODE:
Hans Verkuile18828e2006-01-09 15:25:28 -02001635 fepriv->tune_mode_flags = (unsigned long) parg;
Trent Piepho1b172e02006-06-29 13:16:04 -03001636 err = 0;
Andrew de Quincey36cb5572006-01-09 15:25:07 -02001637 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 };
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return err;
1641}
1642
Steven Toth6b73eea2008-09-04 01:12:25 -03001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1645{
1646 struct dvb_device *dvbdev = file->private_data;
1647 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001648 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001650 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 poll_wait (file, &fepriv->events.wait_queue, wait);
1653
1654 if (fepriv->events.eventw != fepriv->events.eventr)
1655 return (POLLIN | POLLRDNORM | POLLPRI);
1656
1657 return 0;
1658}
1659
1660static int dvb_frontend_open(struct inode *inode, struct file *file)
1661{
1662 struct dvb_device *dvbdev = file->private_data;
1663 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001664 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int ret;
1666
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001667 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Oliver Endriss48136e12007-08-25 12:00:23 -03001669 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1670 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
Steven Tothba7e6f32006-09-25 12:41:53 -03001671 return ret;
Steven Tothba7e6f32006-09-25 12:41:53 -03001672 }
1673
Oliver Endriss48136e12007-08-25 12:00:23 -03001674 if ((ret = dvb_generic_open (inode, file)) < 0)
1675 goto err1;
Andrew de Quincey04c56d02006-07-10 03:34:14 -03001676
Oliver Endriss48136e12007-08-25 12:00:23 -03001677 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
Andrew de Quincey04c56d02006-07-10 03:34:14 -03001678 /* normal tune mode when opened R/W */
1679 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1680 fepriv->tone = -1;
1681 fepriv->voltage = -1;
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 ret = dvb_frontend_start (fe);
1684 if (ret)
Oliver Endriss48136e12007-08-25 12:00:23 -03001685 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 /* empty event queue */
1688 fepriv->events.eventr = fepriv->events.eventw = 0;
1689 }
1690
1691 return ret;
Oliver Endriss48136e12007-08-25 12:00:23 -03001692
1693err2:
1694 dvb_generic_release(inode, file);
1695err1:
1696 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1697 fe->ops.ts_bus_ctrl(fe, 0);
1698 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
1701static int dvb_frontend_release(struct inode *inode, struct file *file)
1702{
1703 struct dvb_device *dvbdev = file->private_data;
1704 struct dvb_frontend *fe = dvbdev->priv;
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001705 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03001706 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001708 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1711 fepriv->release_jiffies = jiffies;
1712
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03001713 ret = dvb_generic_release (inode, file);
1714
Oliver Endriss48136e12007-08-25 12:00:23 -03001715 if (dvbdev->users == -1) {
1716 if (fepriv->exit == 1) {
1717 fops_put(file->f_op);
1718 file->f_op = NULL;
1719 wake_up(&dvbdev->wait_queue);
1720 }
1721 if (fe->ops.ts_bus_ctrl)
1722 fe->ops.ts_bus_ctrl(fe, 0);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03001723 }
Oliver Endriss48136e12007-08-25 12:00:23 -03001724
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03001725 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728static struct file_operations dvb_frontend_fops = {
1729 .owner = THIS_MODULE,
1730 .ioctl = dvb_generic_ioctl,
1731 .poll = dvb_frontend_poll,
1732 .open = dvb_frontend_open,
1733 .release = dvb_frontend_release
1734};
1735
1736int dvb_register_frontend(struct dvb_adapter* dvb,
1737 struct dvb_frontend* fe)
1738{
1739 struct dvb_frontend_private *fepriv;
1740 static const struct dvb_device dvbdev_template = {
1741 .users = ~0,
1742 .writers = 1,
1743 .readers = (~0)-1,
1744 .fops = &dvb_frontend_fops,
1745 .kernel_ioctl = dvb_frontend_ioctl
1746 };
1747
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001748 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Ingo Molnar3593cab2006-02-07 06:49:14 -02001750 if (mutex_lock_interruptible(&frontend_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 return -ERESTARTSYS;
1752
Panagiotis Issaris74081872006-01-11 19:40:56 -02001753 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (fe->frontend_priv == NULL) {
Ingo Molnar3593cab2006-02-07 06:49:14 -02001755 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return -ENOMEM;
1757 }
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001758 fepriv = fe->frontend_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 init_MUTEX (&fepriv->sem);
1761 init_waitqueue_head (&fepriv->wait_queue);
1762 init_waitqueue_head (&fepriv->events.wait_queue);
Matthias Kaehlcke03b76122007-07-30 14:58:10 -03001763 mutex_init(&fepriv->events.mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 fe->dvb = dvb;
1765 fepriv->inversion = INVERSION_OFF;
1766
1767 printk ("DVB: registering frontend %i (%s)...\n",
1768 fe->dvb->num,
Patrick Boettcherdea74862006-05-14 05:01:31 -03001769 fe->ops.info.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1772 fe, DVB_DEVICE_FRONTEND);
1773
Ingo Molnar3593cab2006-02-07 06:49:14 -02001774 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return 0;
1776}
1777EXPORT_SYMBOL(dvb_register_frontend);
1778
1779int dvb_unregister_frontend(struct dvb_frontend* fe)
1780{
Johannes Stezenbach0c53c702005-05-16 21:54:24 -07001781 struct dvb_frontend_private *fepriv = fe->frontend_priv;
Harvey Harrison46b4f7c2008-04-08 23:20:00 -03001782 dprintk ("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Markus Rechberger57861b42007-04-14 10:19:18 -03001784 mutex_lock(&frontend_mutex);
Markus Rechbergerca5be9c2007-04-14 10:18:58 -03001785 dvb_frontend_stop (fe);
Markus Rechberger57861b42007-04-14 10:19:18 -03001786 mutex_unlock(&frontend_mutex);
1787
1788 if (fepriv->dvbdev->users < -1)
1789 wait_event(fepriv->dvbdev->wait_queue,
1790 fepriv->dvbdev->users==-1);
1791
Ingo Molnar3593cab2006-02-07 06:49:14 -02001792 mutex_lock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 dvb_unregister_device (fepriv->dvbdev);
Andrew de Quinceyd9955062006-08-08 09:10:08 -03001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /* fe is invalid now */
1796 kfree(fepriv);
Ingo Molnar3593cab2006-02-07 06:49:14 -02001797 mutex_unlock(&frontend_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return 0;
1799}
1800EXPORT_SYMBOL(dvb_unregister_frontend);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03001801
Mauro Carvalho Chehab149ef722008-04-29 21:38:46 -03001802#ifdef CONFIG_MEDIA_ATTACH
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03001803void dvb_frontend_detach(struct dvb_frontend* fe)
1804{
1805 void *ptr;
1806
1807 if (fe->ops.release_sec) {
1808 fe->ops.release_sec(fe);
1809 symbol_put_addr(fe->ops.release_sec);
1810 }
1811 if (fe->ops.tuner_ops.release) {
1812 fe->ops.tuner_ops.release(fe);
1813 symbol_put_addr(fe->ops.tuner_ops.release);
1814 }
Michael Krufky2426a272007-12-21 11:34:45 -03001815 if (fe->ops.analog_ops.release) {
1816 fe->ops.analog_ops.release(fe);
1817 symbol_put_addr(fe->ops.analog_ops.release);
1818 }
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03001819 ptr = (void*)fe->ops.release;
1820 if (ptr) {
1821 fe->ops.release(fe);
1822 symbol_put_addr(ptr);
1823 }
1824}
1825#else
1826void dvb_frontend_detach(struct dvb_frontend* fe)
1827{
1828 if (fe->ops.release_sec)
1829 fe->ops.release_sec(fe);
1830 if (fe->ops.tuner_ops.release)
1831 fe->ops.tuner_ops.release(fe);
Michael Krufky2426a272007-12-21 11:34:45 -03001832 if (fe->ops.analog_ops.release)
1833 fe->ops.analog_ops.release(fe);
Andrew de Quinceyf52a8382006-08-08 09:10:09 -03001834 if (fe->ops.release)
1835 fe->ops.release(fe);
1836}
1837#endif
1838EXPORT_SYMBOL(dvb_frontend_detach);