V4L/DVB (8995): S2API: tv_ / TV_ to dtv_ / DTV_ namespace changes
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / dvb / dvb-core / dvb_frontend.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4 35#include <linux/list.h>
7dfb7103 36#include <linux/freezer.h>
cc89c229 37#include <linux/jiffies.h>
8eec1429 38#include <linux/kthread.h>
1da177e4 39#include <asm/processor.h>
1da177e4
LT
40
41#include "dvb_frontend.h"
42#include "dvbdev.h"
43
44static int dvb_frontend_debug;
849be2cd 45static int dvb_shutdown_timeout;
1da177e4
LT
46static 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);
f4f009a7 51MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
6baad3f0 52module_param(dvb_shutdown_timeout, int, 0644);
1da177e4 53MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
6baad3f0 54module_param(dvb_force_auto_inversion, int, 0644);
1da177e4 55MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
6baad3f0 56module_param(dvb_override_tune_delay, int, 0644);
1da177e4 57MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
6baad3f0 58module_param(dvb_powerdown_on_sleep, int, 0644);
a8558577 59MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
1da177e4
LT
60
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)
0249ef16
MA
75
76#define FE_ALGO_HW 1
1da177e4
LT
77/*
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
3593cab5 92static DEFINE_MUTEX(frontend_mutex);
1da177e4
LT
93
94struct dvb_frontend_private {
95
36cb557a 96 /* thread/frontend values */
1da177e4
LT
97 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;
8eec1429 103 struct task_struct *thread;
1da177e4 104 unsigned long release_jiffies;
36cb557a
AQ
105 unsigned int exit;
106 unsigned int wakeup;
1da177e4 107 fe_status_t status;
400b7083 108 unsigned long tune_mode_flags;
36cb557a 109 unsigned int delay;
86f40cc3 110 unsigned int reinitialise;
64454016
AQ
111 int tone;
112 int voltage;
36cb557a
AQ
113
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;
1da177e4
LT
127};
128
86f40cc3 129static void dvb_frontend_wakeup(struct dvb_frontend *fe);
1da177e4
LT
130
131static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
132{
0c53c70f 133 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
134 struct dvb_fe_events *events = &fepriv->events;
135 struct dvb_frontend_event *e;
136 int wp;
137
46b4f7c1 138 dprintk ("%s\n", __func__);
1da177e4 139
03b76123 140 if (mutex_lock_interruptible (&events->mtx))
1da177e4
LT
141 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)
dea74869
PB
156 if (fe->ops.get_frontend)
157 fe->ops.get_frontend(fe, &e->parameters);
1da177e4
LT
158
159 events->eventw = wp;
160
03b76123 161 mutex_unlock(&events->mtx);
1da177e4
LT
162
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{
0c53c70f 171 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
172 struct dvb_fe_events *events = &fepriv->events;
173
46b4f7c1 174 dprintk ("%s\n", __func__);
1da177e4
LT
175
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
03b76123 199 if (mutex_lock_interruptible (&events->mtx))
1da177e4
LT
200 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
03b76123 207 mutex_unlock(&events->mtx);
1da177e4
LT
208
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,
dea74869
PB
216 fe->ops.info.name);
217
218 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);
7eef5dd6 224 }
1da177e4
LT
225}
226
86f40cc3
AQ
227void 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
36cb557a 236static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
1da177e4 237{
36cb557a 238 int q2;
1da177e4 239
46b4f7c1 240 dprintk ("%s\n", __func__);
1da177e4 241
36cb557a
AQ
242 if (locked)
243 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
244 else
245 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
1da177e4 246
36cb557a
AQ
247 q2 = fepriv->quality - 128;
248 q2 *= q2;
1da177e4 249
36cb557a 250 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
1da177e4
LT
251}
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 */
36cb557a 260static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
1da177e4
LT
261{
262 int autoinversion;
263 int ready = 0;
0c53c70f 264 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
265 int original_inversion = fepriv->parameters.inversion;
266 u32 original_frequency = fepriv->parameters.frequency;
267
268 /* are we using autoinversion? */
dea74869 269 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
1da177e4
LT
270 (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",
46b4f7c1 332 __func__, fepriv->lnb_drift, fepriv->inversion,
1da177e4
LT
333 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;
dea74869
PB
339 if (fe->ops.set_frontend)
340 fe->ops.set_frontend(fe, &fepriv->parameters);
1da177e4
LT
341
342 fepriv->parameters.frequency = original_frequency;
343 fepriv->parameters.inversion = original_inversion;
344
345 fepriv->auto_sub_step++;
346 return 0;
347}
348
36cb557a
AQ
349static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
350{
30d9464c 351 fe_status_t s = 0;
36cb557a
AQ
352 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) {
dea74869
PB
364 if (fe->ops.set_frontend)
365 fe->ops.set_frontend(fe, &fepriv->parameters);
36cb557a
AQ
366 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 {
dea74869
PB
377 if (fe->ops.read_status)
378 fe->ops.read_status(fe, &s);
36cb557a
AQ
379 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 */
dea74869 391 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
36cb557a
AQ
392 (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) &&
dea74869 415 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
36cb557a
AQ
416 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
1da177e4
LT
472static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
473{
0c53c70f 474 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
475
476 if (fepriv->exit)
477 return 1;
478
479 if (fepriv->dvbdev->writers == 1)
cc89c229 480 if (time_after(jiffies, fepriv->release_jiffies +
36cb557a 481 dvb_shutdown_timeout * HZ))
1da177e4
LT
482 return 1;
483
484 return 0;
485}
486
487static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
488{
0c53c70f 489 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
490
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{
0c53c70f 500 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
501
502 fepriv->wakeup = 1;
503 wake_up_interruptible(&fepriv->wait_queue);
504}
505
1da177e4
LT
506static int dvb_frontend_thread(void *data)
507{
0c53c70f
JS
508 struct dvb_frontend *fe = data;
509 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4 510 unsigned long timeout;
1da177e4 511 fe_status_t s;
36cb557a 512 struct dvb_frontend_parameters *params;
1da177e4 513
46b4f7c1 514 dprintk("%s\n", __func__);
1da177e4 515
36cb557a
AQ
516 fepriv->check_wrapped = 0;
517 fepriv->quality = 0;
518 fepriv->delay = 3*HZ;
1da177e4 519 fepriv->status = 0;
1da177e4 520 fepriv->wakeup = 0;
04c56d0e
AQ
521 fepriv->reinitialise = 0;
522
523 dvb_frontend_init(fe);
1da177e4 524
83144186 525 set_freezable();
1da177e4
LT
526 while (1) {
527 up(&fepriv->sem); /* is locked when we enter the thread... */
6591691b 528restart:
1da177e4 529 timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
e42837bc
RW
530 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
531 || freezing(current),
8eec1429
HP
532 fepriv->delay);
533
534 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
1da177e4
LT
535 /* got signal or quitting */
536 break;
537 }
538
8eec1429 539 if (try_to_freeze())
6591691b 540 goto restart;
1da177e4
LT
541
542 if (down_interruptible(&fepriv->sem))
543 break;
544
86f40cc3
AQ
545 if (fepriv->reinitialise) {
546 dvb_frontend_init(fe);
64454016 547 if (fepriv->tone != -1) {
dea74869 548 fe->ops.set_tone(fe, fepriv->tone);
64454016
AQ
549 }
550 if (fepriv->voltage != -1) {
dea74869 551 fe->ops.set_voltage(fe, fepriv->voltage);
64454016 552 }
86f40cc3
AQ
553 fepriv->reinitialise = 0;
554 }
555
36cb557a 556 /* do an iteration of the tuning loop */
d772bd03
MA
557 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 }
565
566 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 }
70d90635
MA
571 } else
572 dvb_frontend_swzigzag(fe);
4a4edcca
MA
573 } else
574 dvb_frontend_swzigzag(fe);
1da177e4
LT
575 }
576
608f62d6
OE
577 if (dvb_powerdown_on_sleep) {
578 if (fe->ops.set_voltage)
579 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
dea74869
PB
580 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);
7eef5dd6 584 }
dea74869
PB
585 if (fe->ops.sleep)
586 fe->ops.sleep(fe);
1da177e4
LT
587 }
588
8eec1429 589 fepriv->thread = NULL;
1da177e4
LT
590 mb();
591
592 dvb_frontend_wakeup(fe);
593 return 0;
594}
595
596static void dvb_frontend_stop(struct dvb_frontend *fe)
597{
0c53c70f 598 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4 599
46b4f7c1 600 dprintk ("%s\n", __func__);
1da177e4
LT
601
602 fepriv->exit = 1;
603 mb();
604
8eec1429 605 if (!fepriv->thread)
1da177e4
LT
606 return;
607
8eec1429 608 kthread_stop(fepriv->thread);
ca5be9cd 609
8eec1429 610 init_MUTEX (&fepriv->sem);
1da177e4
LT
611 fepriv->state = FESTATE_IDLE;
612
613 /* paranoia check in case a signal arrived */
8eec1429
HP
614 if (fepriv->thread)
615 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
616 fepriv->thread);
1da177e4
LT
617}
618
83b75b04
N
619s32 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
1da177e4
LT
662static int dvb_frontend_start(struct dvb_frontend *fe)
663{
664 int ret;
0c53c70f 665 struct dvb_frontend_private *fepriv = fe->frontend_priv;
8eec1429 666 struct task_struct *fe_thread;
1da177e4 667
46b4f7c1 668 dprintk ("%s\n", __func__);
1da177e4 669
8eec1429 670 if (fepriv->thread) {
1da177e4
LT
671 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;
8eec1429 684 fepriv->thread = NULL;
1da177e4
LT
685 mb();
686
8eec1429
HP
687 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);
1da177e4
LT
692 up(&fepriv->sem);
693 return ret;
694 }
8eec1429 695 fepriv->thread = fe_thread;
1da177e4
LT
696 return 0;
697}
698
c471b331
OE
699static 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
1fab46f0
OE
716static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
717 struct dvb_frontend_parameters *parms)
718{
c471b331
OE
719 u32 freq_min;
720 u32 freq_max;
721
1fab46f0 722 /* range check: frequency */
c471b331
OE
723 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)) {
1fab46f0 726 printk(KERN_WARNING "DVB: frontend %u frequency %u out of range (%u..%u)\n",
c471b331 727 fe->dvb->num, parms->frequency, freq_min, freq_max);
1fab46f0
OE
728 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
56f0680a
ST
758struct dtv_cmds_h dtv_cmds[] = {
759 [DTV_SEQ_UNDEFINED] = {
760 .name = "DTV_SEQ_UNDEFINED",
761 .cmd = DTV_SEQ_UNDEFINED,
6b73eeaf
ST
762 .set = 1,
763 },
56f0680a
ST
764 [DTV_SEQ_START] = {
765 .name = "DTV_SEQ_START",
766 .cmd = DTV_SEQ_START,
6b73eeaf
ST
767 .set = 1,
768 },
56f0680a
ST
769 [DTV_SEQ_CONTINUE] = {
770 .name = "DTV_SEQ_CONTINUE",
771 .cmd = DTV_SEQ_CONTINUE,
6b73eeaf
ST
772 .set = 1,
773 },
56f0680a
ST
774 [DTV_SEQ_COMPLETE] = {
775 .name = "DTV_SEQ_COMPLETE",
776 .cmd = DTV_SEQ_COMPLETE,
6b73eeaf
ST
777 .set = 1,
778 },
56f0680a
ST
779 [DTV_SEQ_TERMINATE] = {
780 .name = "DTV_SEQ_TERMINATE",
781 .cmd = DTV_SEQ_TERMINATE,
6b73eeaf
ST
782 .set = 1,
783 },
784
785 /* Set */
56f0680a
ST
786 [DTV_SET_FREQUENCY] = {
787 .name = "DTV_SET_FREQUENCY",
788 .cmd = DTV_SET_FREQUENCY,
6b73eeaf
ST
789 .set = 1,
790 },
56f0680a
ST
791 [DTV_SET_BANDWIDTH] = {
792 .name = "DTV_SET_BANDWIDTH",
793 .cmd = DTV_SET_BANDWIDTH,
6b73eeaf
ST
794 .set = 1,
795 },
56f0680a
ST
796 [DTV_SET_MODULATION] = {
797 .name = "DTV_SET_MODULATION",
798 .cmd = DTV_SET_MODULATION,
6b73eeaf
ST
799 .set = 1,
800 },
56f0680a
ST
801 [DTV_SET_INVERSION] = {
802 .name = "DTV_SET_INVERSION",
803 .cmd = DTV_SET_INVERSION,
6b73eeaf
ST
804 .set = 1,
805 },
56f0680a
ST
806 [DTV_SET_DISEQC_MASTER] = {
807 .name = "DTV_SET_DISEQC_MASTER",
808 .cmd = DTV_SET_DISEQC_MASTER,
6b73eeaf
ST
809 .set = 1,
810 .buffer = 1,
811 },
56f0680a
ST
812 [DTV_SET_SYMBOLRATE] = {
813 .name = "DTV_SET_SYMBOLRATE",
814 .cmd = DTV_SET_SYMBOLRATE,
6b73eeaf
ST
815 .set = 1,
816 },
56f0680a
ST
817 [DTV_SET_INNERFEC] = {
818 .name = "DTV_SET_INNERFEC",
819 .cmd = DTV_SET_INNERFEC,
6b73eeaf
ST
820 .set = 1,
821 },
56f0680a
ST
822 [DTV_SET_VOLTAGE] = {
823 .name = "DTV_SET_VOLTAGE",
824 .cmd = DTV_SET_VOLTAGE,
6b73eeaf
ST
825 .set = 1,
826 },
56f0680a
ST
827 [DTV_SET_TONE] = {
828 .name = "DTV_SET_TONE",
829 .cmd = DTV_SET_TONE,
6b73eeaf
ST
830 .set = 1,
831 },
56f0680a
ST
832 [DTV_SET_PILOT] = {
833 .name = "DTV_SET_PILOT",
834 .cmd = DTV_SET_PILOT,
6b73eeaf
ST
835 .set = 1,
836 },
56f0680a
ST
837 [DTV_SET_ROLLOFF] = {
838 .name = "DTV_SET_ROLLOFF",
839 .cmd = DTV_SET_ROLLOFF,
6b73eeaf
ST
840 .set = 1,
841 },
56f0680a
ST
842 [DTV_SET_DELIVERY_SYSTEM] = {
843 .name = "DTV_SET_DELIVERY_SYSTEM",
844 .cmd = DTV_SET_DELIVERY_SYSTEM,
6b73eeaf
ST
845 .set = 1,
846 },
56f0680a
ST
847 [DTV_SET_ISDB_SEGMENT_NUM] = {
848 .name = "DTV_SET_ISDB_SEGMENT_NUM",
849 .cmd = DTV_SET_ISDB_SEGMENT_NUM,
6b73eeaf
ST
850 .set = 1,
851 },
56f0680a
ST
852 [DTV_SET_ISDB_SEGMENT_WIDTH] = {
853 .name = "DTV_SET_ISDB_SEGMENT_WIDTH",
854 .cmd = DTV_SET_ISDB_SEGMENT_WIDTH,
6b73eeaf
ST
855 .set = 1,
856 },
857
858 /* Get */
56f0680a
ST
859 [DTV_GET_FREQUENCY] = {
860 .name = "DTV_GET_FREQUENCY",
861 .cmd = DTV_GET_FREQUENCY,
6b73eeaf
ST
862 .set = 0,
863 },
56f0680a
ST
864 [DTV_GET_BANDWIDTH] = {
865 .name = "DTV_GET_BANDWIDTH",
866 .cmd = DTV_GET_BANDWIDTH,
6b73eeaf
ST
867 .set = 0,
868 },
56f0680a
ST
869 [DTV_GET_MODULATION] = {
870 .name = "DTV_GET_MODULATION",
871 .cmd = DTV_GET_MODULATION,
6b73eeaf
ST
872 .set = 0,
873 },
56f0680a
ST
874 [DTV_GET_INVERSION] = {
875 .name = "DTV_GET_INVERSION",
876 .cmd = DTV_GET_INVERSION,
6b73eeaf
ST
877 .set = 0,
878 },
56f0680a
ST
879 [DTV_GET_DISEQC_SLAVE_REPLY] = {
880 .name = "DTV_GET_DISEQC_SLAVE_REPLY",
881 .cmd = DTV_GET_DISEQC_SLAVE_REPLY,
6b73eeaf
ST
882 .set = 0,
883 .buffer = 1,
884 },
56f0680a
ST
885 [DTV_GET_SYMBOLRATE] = {
886 .name = "DTV_GET_SYMBOLRATE",
887 .cmd = DTV_GET_SYMBOLRATE,
6b73eeaf
ST
888 .set = 0,
889 },
56f0680a
ST
890 [DTV_GET_INNERFEC] = {
891 .name = "DTV_GET_INNERFEC",
892 .cmd = DTV_GET_INNERFEC,
6b73eeaf
ST
893 .set = 0,
894 },
56f0680a
ST
895 [DTV_GET_VOLTAGE] = {
896 .name = "DTV_GET_VOLTAGE",
897 .cmd = DTV_GET_VOLTAGE,
6b73eeaf
ST
898 .set = 0,
899 },
56f0680a
ST
900 [DTV_GET_TONE] = {
901 .name = "DTV_GET_TONE",
902 .cmd = DTV_GET_TONE,
6b73eeaf
ST
903 .set = 0,
904 },
56f0680a
ST
905 [DTV_GET_PILOT] = {
906 .name = "DTV_GET_PILOT",
907 .cmd = DTV_GET_PILOT,
6b73eeaf
ST
908 .set = 0,
909 },
56f0680a
ST
910 [DTV_GET_ROLLOFF] = {
911 .name = "DTV_GET_ROLLOFF",
912 .cmd = DTV_GET_ROLLOFF,
6b73eeaf
ST
913 .set = 0,
914 },
56f0680a
ST
915 [DTV_GET_DELIVERY_SYSTEM] = {
916 .name = "DTV_GET_DELIVERY_SYSTEM",
917 .cmd = DTV_GET_DELIVERY_SYSTEM,
6b73eeaf
ST
918 .set = 0,
919 },
56f0680a
ST
920 [DTV_GET_ISDB_SEGMENT_NUM] = {
921 .name = "DTV_GET_ISDB_SEGMENT_NUM",
922 .cmd = DTV_GET_ISDB_SEGMENT_NUM,
6b73eeaf
ST
923 .set = 0,
924 },
56f0680a
ST
925 [DTV_GET_ISDB_SEGMENT_WIDTH] = {
926 .name = "DTV_GET_ISDB_SEGMENT_WIDTH",
927 .cmd = DTV_GET_ISDB_SEGMENT_WIDTH,
6b73eeaf
ST
928 .set = 0,
929 },
56f0680a
ST
930 [DTV_GET_ISDB_LAYERA_FEC] = {
931 .name = "DTV_GET_ISDB_LAYERA_FEC",
932 .cmd = DTV_GET_ISDB_LAYERA_FEC,
6b73eeaf
ST
933 .set = 0,
934 },
56f0680a
ST
935 [DTV_GET_ISDB_LAYERA_MODULATION] = {
936 .name = "DTV_GET_ISDB_LAYERA_MODULATION",
937 .cmd = DTV_GET_ISDB_LAYERA_MODULATION,
6b73eeaf
ST
938 .set = 0,
939 },
56f0680a
ST
940 [DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH] = {
941 .name = "DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH",
942 .cmd = DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH,
6b73eeaf
ST
943 .set = 0,
944 },
56f0680a
ST
945 [DTV_GET_ISDB_LAYERB_FEC] = {
946 .name = "DTV_GET_ISDB_LAYERB_FEC",
947 .cmd = DTV_GET_ISDB_LAYERB_FEC,
6b73eeaf
ST
948 .set = 0,
949 },
56f0680a
ST
950 [DTV_GET_ISDB_LAYERB_MODULATION] = {
951 .name = "DTV_GET_ISDB_LAYERB_MODULATION",
952 .cmd = DTV_GET_ISDB_LAYERB_MODULATION,
6b73eeaf
ST
953 .set = 0,
954 },
56f0680a
ST
955 [DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH] = {
956 .name = "DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH",
957 .cmd = DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH,
6b73eeaf
ST
958 .set = 0,
959 },
56f0680a
ST
960 [DTV_GET_ISDB_LAYERC_FEC] = {
961 .name = "DTV_GET_ISDB_LAYERC_FEC",
962 .cmd = DTV_GET_ISDB_LAYERC_FEC,
6b73eeaf
ST
963 .set = 0,
964 },
56f0680a
ST
965 [DTV_GET_ISDB_LAYERC_MODULATION] = {
966 .name = "DTV_GET_ISDB_LAYERC_MODULATION",
967 .cmd = DTV_GET_ISDB_LAYERC_MODULATION,
6b73eeaf
ST
968 .set = 0,
969 },
56f0680a
ST
970 [DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH] = {
971 .name = "DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH",
972 .cmd = DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH,
6b73eeaf
ST
973 .set = 0,
974 },
975};
976
56f0680a 977void dtv_property_dump(dtv_property_t *tvp)
6b73eeaf
ST
978{
979 int i;
980
981 printk("%s() tvp.cmd = 0x%08x (%s)\n"
982 ,__FUNCTION__
983 ,tvp->cmd
56f0680a 984 ,dtv_cmds[ tvp->cmd ].name);
6b73eeaf 985
56f0680a 986 if(dtv_cmds[ tvp->cmd ].buffer) {
6b73eeaf
ST
987
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
56f0680a 1011int dtv_property_cache_submit(struct dvb_frontend *fe)
6b73eeaf
ST
1012{
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
56f0680a 1039 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
6b73eeaf
ST
1040 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
56f0680a 1118 * the specific values it wants from its dtv_property_cache.
6b73eeaf
ST
1119 * 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
13c97bf5
ST
1159static 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
56f0680a 1164int dtv_property_process(struct dvb_frontend *fe, dtv_property_t *tvp,
13c97bf5 1165 struct inode *inode, struct file *file)
6b73eeaf
ST
1166{
1167 int r = 0;
13c97bf5 1168 struct dvb_frontend_private *fepriv = fe->frontend_priv;
6b73eeaf 1169 printk("%s()\n", __FUNCTION__);
56f0680a 1170 dtv_property_dump(tvp);
6b73eeaf
ST
1171
1172 switch(tvp->cmd) {
56f0680a
ST
1173 case DTV_SEQ_START:
1174 case DTV_SEQ_TERMINATE:
6b73eeaf
ST
1175 /* 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__);
56f0680a
ST
1179 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;
6b73eeaf 1182 break;
56f0680a 1183 case DTV_SEQ_COMPLETE:
6b73eeaf
ST
1184 /* 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 */
56f0680a 1189 fe->dtv_property_cache.state = DTV_SEQ_COMPLETE;
6b73eeaf 1190 printk("%s() Finalised property cache\n", __FUNCTION__);
56f0680a 1191 r |= dtv_property_cache_submit(fe);
13c97bf5
ST
1192 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1193 &fepriv->parameters);
6b73eeaf 1194 break;
56f0680a
ST
1195 case DTV_SET_FREQUENCY:
1196 fe->dtv_property_cache.frequency = tvp->u.data;
6b73eeaf 1197 break;
56f0680a
ST
1198 case DTV_GET_FREQUENCY:
1199 tvp->u.data = fe->dtv_property_cache.frequency;
6b73eeaf 1200 break;
56f0680a
ST
1201 case DTV_SET_MODULATION:
1202 fe->dtv_property_cache.modulation = tvp->u.data;
6b73eeaf 1203 break;
56f0680a
ST
1204 case DTV_GET_MODULATION:
1205 tvp->u.data = fe->dtv_property_cache.modulation;
6b73eeaf 1206 break;
56f0680a
ST
1207 case DTV_SET_BANDWIDTH:
1208 fe->dtv_property_cache.bandwidth = tvp->u.data;
6b73eeaf 1209 break;
56f0680a
ST
1210 case DTV_GET_BANDWIDTH:
1211 tvp->u.data = fe->dtv_property_cache.bandwidth;
6b73eeaf 1212 break;
56f0680a
ST
1213 case DTV_SET_INVERSION:
1214 fe->dtv_property_cache.inversion = tvp->u.data;
6b73eeaf 1215 break;
56f0680a
ST
1216 case DTV_GET_INVERSION:
1217 tvp->u.data = fe->dtv_property_cache.inversion;
6b73eeaf 1218 break;
56f0680a
ST
1219 case DTV_SET_SYMBOLRATE:
1220 fe->dtv_property_cache.symbol_rate = tvp->u.data;
6b73eeaf 1221 break;
56f0680a
ST
1222 case DTV_GET_SYMBOLRATE:
1223 tvp->u.data = fe->dtv_property_cache.symbol_rate;
6b73eeaf 1224 break;
56f0680a
ST
1225 case DTV_SET_INNERFEC:
1226 fe->dtv_property_cache.fec_inner = tvp->u.data;
6b73eeaf 1227 break;
56f0680a
ST
1228 case DTV_GET_INNERFEC:
1229 tvp->u.data = fe->dtv_property_cache.fec_inner;
6b73eeaf 1230 break;
56f0680a
ST
1231 case DTV_SET_PILOT:
1232 fe->dtv_property_cache.pilot = tvp->u.data;
6b73eeaf 1233 break;
56f0680a
ST
1234 case DTV_GET_PILOT:
1235 tvp->u.data = fe->dtv_property_cache.pilot;
6b73eeaf 1236 break;
56f0680a
ST
1237 case DTV_SET_ROLLOFF:
1238 fe->dtv_property_cache.rolloff = tvp->u.data;
6b73eeaf 1239 break;
56f0680a
ST
1240 case DTV_GET_ROLLOFF:
1241 tvp->u.data = fe->dtv_property_cache.rolloff;
6b73eeaf 1242 break;
56f0680a
ST
1243 case DTV_SET_DELIVERY_SYSTEM:
1244 fe->dtv_property_cache.delivery_system = tvp->u.data;
6b73eeaf 1245 break;
56f0680a
ST
1246 case DTV_GET_DELIVERY_SYSTEM:
1247 tvp->u.data = fe->dtv_property_cache.delivery_system;
6b73eeaf
ST
1248 break;
1249
1250 /* ISDB-T Support here */
56f0680a
ST
1251 case DTV_SET_ISDB_SEGMENT_NUM:
1252 fe->dtv_property_cache.isdb_segment_num = tvp->u.data;
6b73eeaf 1253 break;
56f0680a
ST
1254 case DTV_GET_ISDB_SEGMENT_NUM:
1255 tvp->u.data = fe->dtv_property_cache.isdb_segment_num;
6b73eeaf 1256 break;
56f0680a
ST
1257 case DTV_SET_ISDB_SEGMENT_WIDTH:
1258 fe->dtv_property_cache.isdb_segment_width = tvp->u.data;
6b73eeaf 1259 break;
56f0680a
ST
1260 case DTV_GET_ISDB_SEGMENT_WIDTH:
1261 tvp->u.data = fe->dtv_property_cache.isdb_segment_width;
6b73eeaf 1262 break;
56f0680a
ST
1263 case DTV_GET_ISDB_LAYERA_FEC:
1264 tvp->u.data = fe->dtv_property_cache.isdb_layera_fec;
6b73eeaf 1265 break;
56f0680a
ST
1266 case DTV_GET_ISDB_LAYERA_MODULATION:
1267 tvp->u.data = fe->dtv_property_cache.isdb_layera_modulation;
6b73eeaf 1268 break;
56f0680a
ST
1269 case DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH:
1270 tvp->u.data = fe->dtv_property_cache.isdb_layera_segment_width;
6b73eeaf 1271 break;
56f0680a
ST
1272 case DTV_GET_ISDB_LAYERB_FEC:
1273 tvp->u.data = fe->dtv_property_cache.isdb_layerb_fec;
6b73eeaf 1274 break;
56f0680a
ST
1275 case DTV_GET_ISDB_LAYERB_MODULATION:
1276 tvp->u.data = fe->dtv_property_cache.isdb_layerb_modulation;
6b73eeaf 1277 break;
56f0680a
ST
1278 case DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH:
1279 tvp->u.data = fe->dtv_property_cache.isdb_layerb_segment_width;
6b73eeaf 1280 break;
56f0680a
ST
1281 case DTV_GET_ISDB_LAYERC_FEC:
1282 tvp->u.data = fe->dtv_property_cache.isdb_layerc_fec;
6b73eeaf 1283 break;
56f0680a
ST
1284 case DTV_GET_ISDB_LAYERC_MODULATION:
1285 tvp->u.data = fe->dtv_property_cache.isdb_layerc_modulation;
6b73eeaf 1286 break;
56f0680a
ST
1287 case DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH:
1288 tvp->u.data = fe->dtv_property_cache.isdb_layerc_segment_width;
6b73eeaf 1289 break;
56f0680a
ST
1290 case DTV_SET_VOLTAGE:
1291 fe->dtv_property_cache.voltage = tvp->u.data;
13c97bf5 1292 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
56f0680a 1293 (void *)fe->dtv_property_cache.voltage);
13c97bf5 1294 break;
56f0680a
ST
1295 case DTV_GET_VOLTAGE:
1296 tvp->u.data = fe->dtv_property_cache.voltage;
13c97bf5 1297 break;
56f0680a
ST
1298 case DTV_SET_TONE:
1299 fe->dtv_property_cache.sectone = tvp->u.data;
13c97bf5 1300 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
56f0680a 1301 (void *)fe->dtv_property_cache.sectone);
13c97bf5 1302 break;
56f0680a
ST
1303 case DTV_GET_TONE:
1304 tvp->u.data = fe->dtv_property_cache.sectone;
13c97bf5 1305 break;
6b73eeaf
ST
1306 }
1307
13c97bf5 1308 return r;
6b73eeaf
ST
1309}
1310
1da177e4
LT
1311static 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;
0c53c70f 1316 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
1317 int err = -EOPNOTSUPP;
1318
46b4f7c1 1319 dprintk ("%s\n", __func__);
1da177e4 1320
813ce47c 1321 if (fepriv->exit)
1da177e4
LT
1322 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
13c97bf5
ST
1332 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;
56f0680a 1347 dtv_property_t *tvp;
13c97bf5
ST
1348
1349 dprintk("%s\n", __func__);
1350
6b73eeaf
ST
1351 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;
56f0680a
ST
1358 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) )
6b73eeaf
ST
1361 break;
1362 tvp++;
1363 }
1364
56f0680a 1365 if(fe->dtv_property_cache.state == DTV_SEQ_COMPLETE) {
6b73eeaf 1366 printk("%s() Property cache is full, tuning\n", __FUNCTION__);
6b73eeaf
ST
1367 }
1368 err = 0;
1369 }
1370
13c97bf5
ST
1371 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
1da177e4
LT
1382 switch (cmd) {
1383 case FE_GET_INFO: {
0c53c70f 1384 struct dvb_frontend_info* info = parg;
dea74869 1385 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
c471b331 1386 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1da177e4
LT
1387
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
6757ccc5
PB
1395 case FE_READ_STATUS: {
1396 fe_status_t* status = parg;
1397
1398 /* 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
dea74869
PB
1406 if (fe->ops.read_status)
1407 err = fe->ops.read_status(fe, status);
1da177e4 1408 break;
6757ccc5 1409 }
1da177e4 1410 case FE_READ_BER:
dea74869
PB
1411 if (fe->ops.read_ber)
1412 err = fe->ops.read_ber(fe, (__u32*) parg);
1da177e4
LT
1413 break;
1414
1415 case FE_READ_SIGNAL_STRENGTH:
dea74869
PB
1416 if (fe->ops.read_signal_strength)
1417 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1da177e4
LT
1418 break;
1419
1420 case FE_READ_SNR:
dea74869
PB
1421 if (fe->ops.read_snr)
1422 err = fe->ops.read_snr(fe, (__u16*) parg);
1da177e4
LT
1423 break;
1424
1425 case FE_READ_UNCORRECTED_BLOCKS:
dea74869
PB
1426 if (fe->ops.read_ucblocks)
1427 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1da177e4
LT
1428 break;
1429
1430
1431 case FE_DISEQC_RESET_OVERLOAD:
dea74869
PB
1432 if (fe->ops.diseqc_reset_overload) {
1433 err = fe->ops.diseqc_reset_overload(fe);
1da177e4
LT
1434 fepriv->state = FESTATE_DISEQC;
1435 fepriv->status = 0;
1436 }
1437 break;
1438
1439 case FE_DISEQC_SEND_MASTER_CMD:
dea74869
PB
1440 if (fe->ops.diseqc_send_master_cmd) {
1441 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1da177e4
LT
1442 fepriv->state = FESTATE_DISEQC;
1443 fepriv->status = 0;
1444 }
1445 break;
1446
1447 case FE_DISEQC_SEND_BURST:
dea74869
PB
1448 if (fe->ops.diseqc_send_burst) {
1449 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1da177e4
LT
1450 fepriv->state = FESTATE_DISEQC;
1451 fepriv->status = 0;
1452 }
1453 break;
1454
1455 case FE_SET_TONE:
dea74869
PB
1456 if (fe->ops.set_tone) {
1457 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
64454016 1458 fepriv->tone = (fe_sec_tone_mode_t) parg;
1da177e4
LT
1459 fepriv->state = FESTATE_DISEQC;
1460 fepriv->status = 0;
1461 }
1462 break;
1463
1464 case FE_SET_VOLTAGE:
dea74869
PB
1465 if (fe->ops.set_voltage) {
1466 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
64454016 1467 fepriv->voltage = (fe_sec_voltage_t) parg;
1da177e4
LT
1468 fepriv->state = FESTATE_DISEQC;
1469 fepriv->status = 0;
1470 }
1471 break;
1472
1473 case FE_DISHNETWORK_SEND_LEGACY_CMD:
dea74869
PB
1474 if (fe->ops.dishnetwork_send_legacy_command) {
1475 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1da177e4
LT
1476 fepriv->state = FESTATE_DISEQC;
1477 fepriv->status = 0;
dea74869 1478 } else if (fe->ops.set_voltage) {
83b75b04
N
1479 /*
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 */
c6eb8eaf 1495 unsigned long swcmd = ((unsigned long) parg) << 1;
83b75b04
N
1496 struct timeval nexttime;
1497 struct timeval tv[10];
1498 int i;
1499 u8 last = 1;
1500 if (dvb_frontend_debug)
c6eb8eaf 1501 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
83b75b04
N
1502 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 */
dea74869 1508 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
83b75b04
N
1509 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]);
c6eb8eaf 1514 if ((swcmd & 0x01) != last) {
83b75b04 1515 /* set voltage to (last ? 13V : 18V) */
dea74869 1516 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
83b75b04
N
1517 last = (last) ? 0 : 1;
1518 }
c6eb8eaf 1519 swcmd = swcmd >> 1;
83b75b04
N
1520 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",
46b4f7c1 1525 __func__, fe->dvb->num);
83b75b04
N
1526 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;
1da177e4
LT
1532 }
1533 break;
1534
1535 case FE_DISEQC_RECV_SLAVE_REPLY:
dea74869
PB
1536 if (fe->ops.diseqc_recv_slave_reply)
1537 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1da177e4
LT
1538 break;
1539
1540 case FE_ENABLE_HIGH_LNB_VOLTAGE:
dea74869
PB
1541 if (fe->ops.enable_high_lnb_voltage)
1542 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1da177e4
LT
1543 break;
1544
1545 case FE_SET_FRONTEND: {
1546 struct dvb_frontend_tune_settings fetunesettings;
1547
56f0680a 1548 if(fe->dtv_property_cache.state == DTV_SEQ_COMPLETE) {
6b73eeaf
ST
1549 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 }
1fab46f0 1558
6b73eeaf
ST
1559 memcpy (&fepriv->parameters, parg,
1560 sizeof (struct dvb_frontend_parameters));
1561 }
1da177e4
LT
1562
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 }
dea74869 1572 if (fe->ops.info.type == FE_OFDM) {
1b3c3714 1573 /* without hierarchical coding code_rate_LP is irrelevant,
1da177e4
LT
1574 * 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 */
dea74869 1581 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1da177e4
LT
1582 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 */
dea74869 1587 switch(fe->ops.info.type) {
1da177e4
LT
1588 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;
dea74869
PB
1602 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1603 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1da177e4
LT
1604 break;
1605 case FE_ATSC:
4821fb1d
MA
1606 fepriv->min_delay = HZ/20;
1607 fepriv->step_size = 0;
1608 fepriv->max_drift = 0;
1da177e4
LT
1609 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:
dea74869 1628 if (fe->ops.get_frontend) {
1da177e4 1629 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
dea74869 1630 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1da177e4
LT
1631 }
1632 break;
36cb557a
AQ
1633
1634 case FE_SET_FRONTEND_TUNE_MODE:
e18828e4 1635 fepriv->tune_mode_flags = (unsigned long) parg;
1b172e0c 1636 err = 0;
36cb557a 1637 break;
1da177e4
LT
1638 };
1639
1da177e4
LT
1640 return err;
1641}
1642
6b73eeaf 1643
1da177e4
LT
1644static 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;
0c53c70f 1648 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4 1649
46b4f7c1 1650 dprintk ("%s\n", __func__);
1da177e4
LT
1651
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;
0c53c70f 1664 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
1665 int ret;
1666
46b4f7c1 1667 dprintk ("%s\n", __func__);
1da177e4 1668
48136e1e
OE
1669 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1670 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
ba7e6f3e 1671 return ret;
ba7e6f3e
ST
1672 }
1673
48136e1e
OE
1674 if ((ret = dvb_generic_open (inode, file)) < 0)
1675 goto err1;
04c56d0e 1676
48136e1e 1677 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
04c56d0e
AQ
1678 /* 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
1da177e4
LT
1683 ret = dvb_frontend_start (fe);
1684 if (ret)
48136e1e 1685 goto err2;
1da177e4
LT
1686
1687 /* empty event queue */
1688 fepriv->events.eventr = fepriv->events.eventw = 0;
1689 }
1690
1691 return ret;
48136e1e
OE
1692
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;
1da177e4
LT
1699}
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;
0c53c70f 1705 struct dvb_frontend_private *fepriv = fe->frontend_priv;
ca5be9cd 1706 int ret;
1da177e4 1707
46b4f7c1 1708 dprintk ("%s\n", __func__);
1da177e4
LT
1709
1710 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1711 fepriv->release_jiffies = jiffies;
1712
ca5be9cd
MR
1713 ret = dvb_generic_release (inode, file);
1714
48136e1e
OE
1715 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);
ca5be9cd 1723 }
48136e1e 1724
ca5be9cd 1725 return ret;
1da177e4
LT
1726}
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
46b4f7c1 1748 dprintk ("%s\n", __func__);
1da177e4 1749
3593cab5 1750 if (mutex_lock_interruptible(&frontend_mutex))
1da177e4
LT
1751 return -ERESTARTSYS;
1752
7408187d 1753 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1da177e4 1754 if (fe->frontend_priv == NULL) {
3593cab5 1755 mutex_unlock(&frontend_mutex);
1da177e4
LT
1756 return -ENOMEM;
1757 }
0c53c70f 1758 fepriv = fe->frontend_priv;
1da177e4
LT
1759
1760 init_MUTEX (&fepriv->sem);
1761 init_waitqueue_head (&fepriv->wait_queue);
1762 init_waitqueue_head (&fepriv->events.wait_queue);
03b76123 1763 mutex_init(&fepriv->events.mtx);
1da177e4
LT
1764 fe->dvb = dvb;
1765 fepriv->inversion = INVERSION_OFF;
1766
1767 printk ("DVB: registering frontend %i (%s)...\n",
1768 fe->dvb->num,
dea74869 1769 fe->ops.info.name);
1da177e4
LT
1770
1771 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1772 fe, DVB_DEVICE_FRONTEND);
1773
3593cab5 1774 mutex_unlock(&frontend_mutex);
1da177e4
LT
1775 return 0;
1776}
1777EXPORT_SYMBOL(dvb_register_frontend);
1778
1779int dvb_unregister_frontend(struct dvb_frontend* fe)
1780{
0c53c70f 1781 struct dvb_frontend_private *fepriv = fe->frontend_priv;
46b4f7c1 1782 dprintk ("%s\n", __func__);
1da177e4 1783
57861b43 1784 mutex_lock(&frontend_mutex);
ca5be9cd 1785 dvb_frontend_stop (fe);
57861b43
MR
1786 mutex_unlock(&frontend_mutex);
1787
1788 if (fepriv->dvbdev->users < -1)
1789 wait_event(fepriv->dvbdev->wait_queue,
1790 fepriv->dvbdev->users==-1);
1791
3593cab5 1792 mutex_lock(&frontend_mutex);
1da177e4 1793 dvb_unregister_device (fepriv->dvbdev);
d9955060 1794
1da177e4
LT
1795 /* fe is invalid now */
1796 kfree(fepriv);
3593cab5 1797 mutex_unlock(&frontend_mutex);
1da177e4
LT
1798 return 0;
1799}
1800EXPORT_SYMBOL(dvb_unregister_frontend);
f52a838b 1801
149ef72d 1802#ifdef CONFIG_MEDIA_ATTACH
f52a838b
AQ
1803void 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 }
2426a27e
MK
1815 if (fe->ops.analog_ops.release) {
1816 fe->ops.analog_ops.release(fe);
1817 symbol_put_addr(fe->ops.analog_ops.release);
1818 }
f52a838b
AQ
1819 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);
2426a27e
MK
1832 if (fe->ops.analog_ops.release)
1833 fe->ops.analog_ops.release(fe);
f52a838b
AQ
1834 if (fe->ops.release)
1835 fe->ops.release(fe);
1836}
1837#endif
1838EXPORT_SYMBOL(dvb_frontend_detach);