V4L/DVB (8998): s2api: restore DTV_UNDEFINED
[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 758struct dtv_cmds_h dtv_cmds[] = {
e7fee0f3
ST
759 [DTV_TUNE] = {
760 .name = "DTV_TUNE",
761 .cmd = DTV_TUNE,
6b73eeaf
ST
762 .set = 1,
763 },
e7fee0f3
ST
764 [DTV_CLEAR] = {
765 .name = "DTV_CLEAR",
766 .cmd = DTV_CLEAR,
6b73eeaf
ST
767 .set = 1,
768 },
769
770 /* Set */
56f0680a
ST
771 [DTV_SET_FREQUENCY] = {
772 .name = "DTV_SET_FREQUENCY",
773 .cmd = DTV_SET_FREQUENCY,
6b73eeaf
ST
774 .set = 1,
775 },
56f0680a
ST
776 [DTV_SET_BANDWIDTH] = {
777 .name = "DTV_SET_BANDWIDTH",
778 .cmd = DTV_SET_BANDWIDTH,
6b73eeaf
ST
779 .set = 1,
780 },
56f0680a
ST
781 [DTV_SET_MODULATION] = {
782 .name = "DTV_SET_MODULATION",
783 .cmd = DTV_SET_MODULATION,
6b73eeaf
ST
784 .set = 1,
785 },
56f0680a
ST
786 [DTV_SET_INVERSION] = {
787 .name = "DTV_SET_INVERSION",
788 .cmd = DTV_SET_INVERSION,
6b73eeaf
ST
789 .set = 1,
790 },
56f0680a
ST
791 [DTV_SET_DISEQC_MASTER] = {
792 .name = "DTV_SET_DISEQC_MASTER",
793 .cmd = DTV_SET_DISEQC_MASTER,
6b73eeaf
ST
794 .set = 1,
795 .buffer = 1,
796 },
177b868d
ST
797 [DTV_SET_SYMBOL_RATE] = {
798 .name = "DTV_SET_SYMBOL_RATE",
799 .cmd = DTV_SET_SYMBOL_RATE,
6b73eeaf
ST
800 .set = 1,
801 },
177b868d
ST
802 [DTV_SET_INNER_FEC] = {
803 .name = "DTV_SET_INNER_FEC",
804 .cmd = DTV_SET_INNER_FEC,
6b73eeaf
ST
805 .set = 1,
806 },
56f0680a
ST
807 [DTV_SET_VOLTAGE] = {
808 .name = "DTV_SET_VOLTAGE",
809 .cmd = DTV_SET_VOLTAGE,
6b73eeaf
ST
810 .set = 1,
811 },
56f0680a
ST
812 [DTV_SET_TONE] = {
813 .name = "DTV_SET_TONE",
814 .cmd = DTV_SET_TONE,
6b73eeaf
ST
815 .set = 1,
816 },
56f0680a
ST
817 [DTV_SET_PILOT] = {
818 .name = "DTV_SET_PILOT",
819 .cmd = DTV_SET_PILOT,
6b73eeaf
ST
820 .set = 1,
821 },
56f0680a
ST
822 [DTV_SET_ROLLOFF] = {
823 .name = "DTV_SET_ROLLOFF",
824 .cmd = DTV_SET_ROLLOFF,
6b73eeaf
ST
825 .set = 1,
826 },
56f0680a
ST
827 [DTV_SET_DELIVERY_SYSTEM] = {
828 .name = "DTV_SET_DELIVERY_SYSTEM",
829 .cmd = DTV_SET_DELIVERY_SYSTEM,
6b73eeaf
ST
830 .set = 1,
831 },
56f0680a
ST
832 [DTV_SET_ISDB_SEGMENT_NUM] = {
833 .name = "DTV_SET_ISDB_SEGMENT_NUM",
834 .cmd = DTV_SET_ISDB_SEGMENT_NUM,
6b73eeaf
ST
835 .set = 1,
836 },
56f0680a
ST
837 [DTV_SET_ISDB_SEGMENT_WIDTH] = {
838 .name = "DTV_SET_ISDB_SEGMENT_WIDTH",
839 .cmd = DTV_SET_ISDB_SEGMENT_WIDTH,
6b73eeaf
ST
840 .set = 1,
841 },
842
843 /* Get */
56f0680a
ST
844 [DTV_GET_FREQUENCY] = {
845 .name = "DTV_GET_FREQUENCY",
846 .cmd = DTV_GET_FREQUENCY,
6b73eeaf
ST
847 .set = 0,
848 },
56f0680a
ST
849 [DTV_GET_BANDWIDTH] = {
850 .name = "DTV_GET_BANDWIDTH",
851 .cmd = DTV_GET_BANDWIDTH,
6b73eeaf
ST
852 .set = 0,
853 },
56f0680a
ST
854 [DTV_GET_MODULATION] = {
855 .name = "DTV_GET_MODULATION",
856 .cmd = DTV_GET_MODULATION,
6b73eeaf
ST
857 .set = 0,
858 },
56f0680a
ST
859 [DTV_GET_INVERSION] = {
860 .name = "DTV_GET_INVERSION",
861 .cmd = DTV_GET_INVERSION,
6b73eeaf
ST
862 .set = 0,
863 },
56f0680a
ST
864 [DTV_GET_DISEQC_SLAVE_REPLY] = {
865 .name = "DTV_GET_DISEQC_SLAVE_REPLY",
866 .cmd = DTV_GET_DISEQC_SLAVE_REPLY,
6b73eeaf
ST
867 .set = 0,
868 .buffer = 1,
869 },
177b868d
ST
870 [DTV_GET_SYMBOL_RATE] = {
871 .name = "DTV_GET_SYMBOL_RATE",
872 .cmd = DTV_GET_SYMBOL_RATE,
6b73eeaf
ST
873 .set = 0,
874 },
177b868d
ST
875 [DTV_GET_INNER_FEC] = {
876 .name = "DTV_GET_INNER_FEC",
877 .cmd = DTV_GET_INNER_FEC,
6b73eeaf
ST
878 .set = 0,
879 },
56f0680a
ST
880 [DTV_GET_VOLTAGE] = {
881 .name = "DTV_GET_VOLTAGE",
882 .cmd = DTV_GET_VOLTAGE,
6b73eeaf
ST
883 .set = 0,
884 },
56f0680a
ST
885 [DTV_GET_TONE] = {
886 .name = "DTV_GET_TONE",
887 .cmd = DTV_GET_TONE,
6b73eeaf
ST
888 .set = 0,
889 },
56f0680a
ST
890 [DTV_GET_PILOT] = {
891 .name = "DTV_GET_PILOT",
892 .cmd = DTV_GET_PILOT,
6b73eeaf
ST
893 .set = 0,
894 },
56f0680a
ST
895 [DTV_GET_ROLLOFF] = {
896 .name = "DTV_GET_ROLLOFF",
897 .cmd = DTV_GET_ROLLOFF,
6b73eeaf
ST
898 .set = 0,
899 },
56f0680a
ST
900 [DTV_GET_DELIVERY_SYSTEM] = {
901 .name = "DTV_GET_DELIVERY_SYSTEM",
902 .cmd = DTV_GET_DELIVERY_SYSTEM,
6b73eeaf
ST
903 .set = 0,
904 },
56f0680a
ST
905 [DTV_GET_ISDB_SEGMENT_NUM] = {
906 .name = "DTV_GET_ISDB_SEGMENT_NUM",
907 .cmd = DTV_GET_ISDB_SEGMENT_NUM,
6b73eeaf
ST
908 .set = 0,
909 },
56f0680a
ST
910 [DTV_GET_ISDB_SEGMENT_WIDTH] = {
911 .name = "DTV_GET_ISDB_SEGMENT_WIDTH",
912 .cmd = DTV_GET_ISDB_SEGMENT_WIDTH,
6b73eeaf
ST
913 .set = 0,
914 },
56f0680a
ST
915 [DTV_GET_ISDB_LAYERA_FEC] = {
916 .name = "DTV_GET_ISDB_LAYERA_FEC",
917 .cmd = DTV_GET_ISDB_LAYERA_FEC,
6b73eeaf
ST
918 .set = 0,
919 },
56f0680a
ST
920 [DTV_GET_ISDB_LAYERA_MODULATION] = {
921 .name = "DTV_GET_ISDB_LAYERA_MODULATION",
922 .cmd = DTV_GET_ISDB_LAYERA_MODULATION,
6b73eeaf
ST
923 .set = 0,
924 },
56f0680a
ST
925 [DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH] = {
926 .name = "DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH",
927 .cmd = DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH,
6b73eeaf
ST
928 .set = 0,
929 },
56f0680a
ST
930 [DTV_GET_ISDB_LAYERB_FEC] = {
931 .name = "DTV_GET_ISDB_LAYERB_FEC",
932 .cmd = DTV_GET_ISDB_LAYERB_FEC,
6b73eeaf
ST
933 .set = 0,
934 },
56f0680a
ST
935 [DTV_GET_ISDB_LAYERB_MODULATION] = {
936 .name = "DTV_GET_ISDB_LAYERB_MODULATION",
937 .cmd = DTV_GET_ISDB_LAYERB_MODULATION,
6b73eeaf
ST
938 .set = 0,
939 },
56f0680a
ST
940 [DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH] = {
941 .name = "DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH",
942 .cmd = DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH,
6b73eeaf
ST
943 .set = 0,
944 },
56f0680a
ST
945 [DTV_GET_ISDB_LAYERC_FEC] = {
946 .name = "DTV_GET_ISDB_LAYERC_FEC",
947 .cmd = DTV_GET_ISDB_LAYERC_FEC,
6b73eeaf
ST
948 .set = 0,
949 },
56f0680a
ST
950 [DTV_GET_ISDB_LAYERC_MODULATION] = {
951 .name = "DTV_GET_ISDB_LAYERC_MODULATION",
952 .cmd = DTV_GET_ISDB_LAYERC_MODULATION,
6b73eeaf
ST
953 .set = 0,
954 },
56f0680a
ST
955 [DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH] = {
956 .name = "DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH",
957 .cmd = DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH,
6b73eeaf
ST
958 .set = 0,
959 },
960};
961
e7fee0f3 962void dtv_property_dump(struct dtv_property *tvp)
6b73eeaf
ST
963{
964 int i;
965
966 printk("%s() tvp.cmd = 0x%08x (%s)\n"
967 ,__FUNCTION__
968 ,tvp->cmd
56f0680a 969 ,dtv_cmds[ tvp->cmd ].name);
6b73eeaf 970
56f0680a 971 if(dtv_cmds[ tvp->cmd ].buffer) {
6b73eeaf
ST
972
973 printk("%s() tvp.u.buffer.len = 0x%02x\n"
974 ,__FUNCTION__
975 ,tvp->u.buffer.len);
976
977 for(i = 0; i < tvp->u.buffer.len; i++)
978 printk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
979 ,__FUNCTION__
980 ,i
981 ,tvp->u.buffer.data[i]);
982
983 } else
984 printk("%s() tvp.u.data = 0x%08x\n", __FUNCTION__, tvp->u.data);
985}
986
987int is_legacy_delivery_system(fe_delivery_system_t s)
988{
989 if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
990 (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS))
991 return 1;
992
993 return 0;
994}
995
56f0680a 996int dtv_property_cache_submit(struct dvb_frontend *fe)
6b73eeaf
ST
997{
998
999 /* We have to do one of two things:
1000 * To support legacy devices using the new API we take values from
1001 * the tv_cache and generate a legacy truning structure.
1002 *
1003 * Or,
1004 *
1005 * To support advanced tuning devices with the new API we
1006 * notify the new advance driver type that a tuning operation is required
1007 * and let it pull values from the cache as is, we don't need to
1008 * pass structures.
1009 *
1010 * We'll use the modulation type to assess how this is handled. as the API
1011 * progresses we'll probably want to have a flag in dvb_frontend_ops
1012 * to allow the frontend driver to dictate how it likes to be tuned.
1013 *
1014 * Because of how this is attached to the ioctl handler for legacy support,
1015 * it's important to return an appropriate result code with atleast the following
1016 * three meanings:
1017 * < 0 = processing error
1018 * 0 = lecagy ioctl handler to submit a traditional set_frontend() call.
1019 * 1 = lecagy ioctl handler should NOT submit a traditional set_frontend() call.
1020 */
1021
1022 int r;
1023
56f0680a 1024 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
6b73eeaf
ST
1025 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1026 struct dvb_frontend_parameters p;
1027
1028 printk("%s()\n", __FUNCTION__);
1029
1030 /* For legacy delivery systems we don't need the delivery_system to be specified */
1031 if(is_legacy_delivery_system(c->delivery_system)) {
e7fee0f3 1032 printk("%s() legacy, modulation = %d\n", __FUNCTION__, c->modulation);
6b73eeaf
ST
1033 switch(c->modulation) {
1034 case QPSK:
1035 printk("%s() Preparing QPSK req\n", __FUNCTION__);
1036 p.frequency = c->frequency;
1037 p.inversion = c->inversion;
1038 p.u.qpsk.symbol_rate = c->symbol_rate;
1039 p.u.qpsk.fec_inner = c->fec_inner;
1040 memcpy(&fepriv->parameters, &p,
1041 sizeof (struct dvb_frontend_parameters));
1042
1043 /* Call the traditional tuning mechanisms. */
1044
1045 r = 0;
1046 break;
1047 case QAM_16:
1048 case QAM_32:
1049 case QAM_64:
1050 case QAM_128:
1051 case QAM_256:
1052 case QAM_AUTO:
1053 printk("%s() Preparing QAM req\n", __FUNCTION__);
1054 p.frequency = c->frequency;
1055 p.inversion = c->inversion;
1056 p.u.qam.symbol_rate = c->symbol_rate;
1057 p.u.vsb.modulation = c->modulation;
1058 printk("%s() frequency = %d\n", __FUNCTION__, p.frequency);
1059 printk("%s() QAM = %d\n", __FUNCTION__, p.u.vsb.modulation);
1060 memcpy(&fepriv->parameters, &p,
1061 sizeof (struct dvb_frontend_parameters));
1062
1063 /* At this point we're fully formed for backwards
1064 * compatability and we need to return this
1065 * via the ioctl handler as SET_FRONTEND (arg).
1066 * We've already patched the new values into the
1067 * frontends tuning structures so the ioctl code just
1068 * continues as if a legacy tune structure was passed
1069 * from userspace.
1070 */
1071
1072 r = 0;
1073 break;
1074 case VSB_8:
1075 case VSB_16:
1076 printk("%s() Preparing VSB req\n", __FUNCTION__);
1077 p.frequency = c->frequency;
1078 p.u.vsb.modulation = c->modulation;
1079 memcpy(&fepriv->parameters, &p,
1080 sizeof (struct dvb_frontend_parameters));
1081
1082 /* Call the traditional tuning mechanisms. */
1083
1084 r = 0;
1085 break;
1086 /* TODO: Add any missing modulation types */
1087 default:
1088 r = -1;
1089 }
1090 } else {
1091 /* For advanced delivery systems / modulation types ...
1092 * we seed the lecacy dvb_frontend_parameters structure
1093 * so that the sanity checking code later in the IOCTL processing
1094 * can validate our basic frequency ranges, symbolrates, modulation
1095 * etc.
1096 */
1097 r = -1;
1098
1099 switch(c->modulation) {
1100 case _8PSK:
1101 case _16APSK:
1102 case NBC_QPSK:
1103 /* Just post a notification to the demod driver and let it pull
56f0680a 1104 * the specific values it wants from its dtv_property_cache.
6b73eeaf
ST
1105 * It can decide how best to use those parameters.
1106 * IOCTL will call set_frontend (by default) due to zigzag
1107 * support etc.
1108 */
1109 if (fe->ops.set_params)
1110 r = fe->ops.set_params(fe);
1111
1112 p.frequency = c->frequency;
1113 p.inversion = c->inversion;
1114 p.u.qpsk.symbol_rate = c->symbol_rate;
1115 p.u.qpsk.fec_inner = c->fec_inner;
1116 memcpy(&fepriv->parameters, &p,
1117 sizeof (struct dvb_frontend_parameters));
1118
1119 r = 0;
1120 break;
1121 default:
1122 r = -1;
1123 }
1124
1125 if(c->delivery_system == SYS_ISDBT) {
1126 /* Fake out a generic DVB-T request so we pass validation in the ioctl */
1127 p.frequency = c->frequency;
1128 p.inversion = INVERSION_AUTO;
1129 p.u.ofdm.constellation = QAM_AUTO;
1130 p.u.ofdm.code_rate_HP = FEC_AUTO;
1131 p.u.ofdm.code_rate_LP = FEC_AUTO;
1132 p.u.ofdm.bandwidth = BANDWIDTH_AUTO;
1133 p.u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
1134 p.u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
1135 p.u.ofdm.hierarchy_information = HIERARCHY_AUTO;
1136 memcpy(&fepriv->parameters, &p,
1137 sizeof (struct dvb_frontend_parameters));
1138
1139 r = 0;
1140 }
1141 }
1142 return r;
1143}
1144
13c97bf5
ST
1145static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1146 unsigned int cmd, void *parg);
1147static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1148 unsigned int cmd, void *parg);
1149
e7fee0f3 1150int dtv_property_process(struct dvb_frontend *fe, struct dtv_property *tvp,
13c97bf5 1151 struct inode *inode, struct file *file)
6b73eeaf
ST
1152{
1153 int r = 0;
13c97bf5 1154 struct dvb_frontend_private *fepriv = fe->frontend_priv;
6b73eeaf 1155 printk("%s()\n", __FUNCTION__);
56f0680a 1156 dtv_property_dump(tvp);
6b73eeaf
ST
1157
1158 switch(tvp->cmd) {
e7fee0f3 1159 case DTV_CLEAR:
6b73eeaf
ST
1160 /* Reset a cache of data specific to the frontend here. This does
1161 * not effect hardware.
1162 */
1163 printk("%s() Flushing property cache\n", __FUNCTION__);
56f0680a 1164 memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
e7fee0f3 1165 fe->dtv_property_cache.state = tvp->cmd;
56f0680a 1166 fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
6b73eeaf 1167 break;
e7fee0f3 1168 case DTV_TUNE:
6b73eeaf
ST
1169 /* interpret the cache of data, build either a traditional frontend
1170 * tunerequest and submit it to a subset of the ioctl handler,
1171 * or, call a new undefined method on the frontend to deal with
1172 * all new tune requests.
1173 */
e7fee0f3 1174 fe->dtv_property_cache.state = tvp->cmd;
6b73eeaf 1175 printk("%s() Finalised property cache\n", __FUNCTION__);
56f0680a 1176 r |= dtv_property_cache_submit(fe);
13c97bf5
ST
1177 r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
1178 &fepriv->parameters);
6b73eeaf 1179 break;
56f0680a
ST
1180 case DTV_SET_FREQUENCY:
1181 fe->dtv_property_cache.frequency = tvp->u.data;
6b73eeaf 1182 break;
56f0680a
ST
1183 case DTV_GET_FREQUENCY:
1184 tvp->u.data = fe->dtv_property_cache.frequency;
6b73eeaf 1185 break;
56f0680a
ST
1186 case DTV_SET_MODULATION:
1187 fe->dtv_property_cache.modulation = tvp->u.data;
6b73eeaf 1188 break;
56f0680a
ST
1189 case DTV_GET_MODULATION:
1190 tvp->u.data = fe->dtv_property_cache.modulation;
6b73eeaf 1191 break;
56f0680a
ST
1192 case DTV_SET_BANDWIDTH:
1193 fe->dtv_property_cache.bandwidth = tvp->u.data;
6b73eeaf 1194 break;
56f0680a
ST
1195 case DTV_GET_BANDWIDTH:
1196 tvp->u.data = fe->dtv_property_cache.bandwidth;
6b73eeaf 1197 break;
56f0680a
ST
1198 case DTV_SET_INVERSION:
1199 fe->dtv_property_cache.inversion = tvp->u.data;
6b73eeaf 1200 break;
56f0680a
ST
1201 case DTV_GET_INVERSION:
1202 tvp->u.data = fe->dtv_property_cache.inversion;
6b73eeaf 1203 break;
177b868d 1204 case DTV_SET_SYMBOL_RATE:
56f0680a 1205 fe->dtv_property_cache.symbol_rate = tvp->u.data;
6b73eeaf 1206 break;
177b868d 1207 case DTV_GET_SYMBOL_RATE:
56f0680a 1208 tvp->u.data = fe->dtv_property_cache.symbol_rate;
6b73eeaf 1209 break;
177b868d 1210 case DTV_SET_INNER_FEC:
56f0680a 1211 fe->dtv_property_cache.fec_inner = tvp->u.data;
6b73eeaf 1212 break;
177b868d 1213 case DTV_GET_INNER_FEC:
56f0680a 1214 tvp->u.data = fe->dtv_property_cache.fec_inner;
6b73eeaf 1215 break;
56f0680a
ST
1216 case DTV_SET_PILOT:
1217 fe->dtv_property_cache.pilot = tvp->u.data;
6b73eeaf 1218 break;
56f0680a
ST
1219 case DTV_GET_PILOT:
1220 tvp->u.data = fe->dtv_property_cache.pilot;
6b73eeaf 1221 break;
56f0680a
ST
1222 case DTV_SET_ROLLOFF:
1223 fe->dtv_property_cache.rolloff = tvp->u.data;
6b73eeaf 1224 break;
56f0680a
ST
1225 case DTV_GET_ROLLOFF:
1226 tvp->u.data = fe->dtv_property_cache.rolloff;
6b73eeaf 1227 break;
56f0680a
ST
1228 case DTV_SET_DELIVERY_SYSTEM:
1229 fe->dtv_property_cache.delivery_system = tvp->u.data;
6b73eeaf 1230 break;
56f0680a
ST
1231 case DTV_GET_DELIVERY_SYSTEM:
1232 tvp->u.data = fe->dtv_property_cache.delivery_system;
6b73eeaf
ST
1233 break;
1234
1235 /* ISDB-T Support here */
56f0680a
ST
1236 case DTV_SET_ISDB_SEGMENT_NUM:
1237 fe->dtv_property_cache.isdb_segment_num = tvp->u.data;
6b73eeaf 1238 break;
56f0680a
ST
1239 case DTV_GET_ISDB_SEGMENT_NUM:
1240 tvp->u.data = fe->dtv_property_cache.isdb_segment_num;
6b73eeaf 1241 break;
56f0680a
ST
1242 case DTV_SET_ISDB_SEGMENT_WIDTH:
1243 fe->dtv_property_cache.isdb_segment_width = tvp->u.data;
6b73eeaf 1244 break;
56f0680a
ST
1245 case DTV_GET_ISDB_SEGMENT_WIDTH:
1246 tvp->u.data = fe->dtv_property_cache.isdb_segment_width;
6b73eeaf 1247 break;
56f0680a
ST
1248 case DTV_GET_ISDB_LAYERA_FEC:
1249 tvp->u.data = fe->dtv_property_cache.isdb_layera_fec;
6b73eeaf 1250 break;
56f0680a
ST
1251 case DTV_GET_ISDB_LAYERA_MODULATION:
1252 tvp->u.data = fe->dtv_property_cache.isdb_layera_modulation;
6b73eeaf 1253 break;
56f0680a
ST
1254 case DTV_GET_ISDB_LAYERA_SEGMENT_WIDTH:
1255 tvp->u.data = fe->dtv_property_cache.isdb_layera_segment_width;
6b73eeaf 1256 break;
56f0680a
ST
1257 case DTV_GET_ISDB_LAYERB_FEC:
1258 tvp->u.data = fe->dtv_property_cache.isdb_layerb_fec;
6b73eeaf 1259 break;
56f0680a
ST
1260 case DTV_GET_ISDB_LAYERB_MODULATION:
1261 tvp->u.data = fe->dtv_property_cache.isdb_layerb_modulation;
6b73eeaf 1262 break;
56f0680a
ST
1263 case DTV_GET_ISDB_LAYERB_SEGMENT_WIDTH:
1264 tvp->u.data = fe->dtv_property_cache.isdb_layerb_segment_width;
6b73eeaf 1265 break;
56f0680a
ST
1266 case DTV_GET_ISDB_LAYERC_FEC:
1267 tvp->u.data = fe->dtv_property_cache.isdb_layerc_fec;
6b73eeaf 1268 break;
56f0680a
ST
1269 case DTV_GET_ISDB_LAYERC_MODULATION:
1270 tvp->u.data = fe->dtv_property_cache.isdb_layerc_modulation;
6b73eeaf 1271 break;
56f0680a
ST
1272 case DTV_GET_ISDB_LAYERC_SEGMENT_WIDTH:
1273 tvp->u.data = fe->dtv_property_cache.isdb_layerc_segment_width;
6b73eeaf 1274 break;
56f0680a
ST
1275 case DTV_SET_VOLTAGE:
1276 fe->dtv_property_cache.voltage = tvp->u.data;
13c97bf5 1277 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
56f0680a 1278 (void *)fe->dtv_property_cache.voltage);
13c97bf5 1279 break;
56f0680a
ST
1280 case DTV_GET_VOLTAGE:
1281 tvp->u.data = fe->dtv_property_cache.voltage;
13c97bf5 1282 break;
56f0680a
ST
1283 case DTV_SET_TONE:
1284 fe->dtv_property_cache.sectone = tvp->u.data;
13c97bf5 1285 r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
56f0680a 1286 (void *)fe->dtv_property_cache.sectone);
13c97bf5 1287 break;
56f0680a
ST
1288 case DTV_GET_TONE:
1289 tvp->u.data = fe->dtv_property_cache.sectone;
13c97bf5 1290 break;
6b73eeaf
ST
1291 }
1292
13c97bf5 1293 return r;
6b73eeaf
ST
1294}
1295
1da177e4
LT
1296static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
1297 unsigned int cmd, void *parg)
1298{
1299 struct dvb_device *dvbdev = file->private_data;
1300 struct dvb_frontend *fe = dvbdev->priv;
0c53c70f 1301 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
1302 int err = -EOPNOTSUPP;
1303
46b4f7c1 1304 dprintk ("%s\n", __func__);
1da177e4 1305
813ce47c 1306 if (fepriv->exit)
1da177e4
LT
1307 return -ENODEV;
1308
1309 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1310 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1311 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1312 return -EPERM;
1313
1314 if (down_interruptible (&fepriv->sem))
1315 return -ERESTARTSYS;
1316
13c97bf5
ST
1317 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1318 err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
1319 else
1320 err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
1321
1322 up(&fepriv->sem);
1323 return err;
1324}
1325
1326static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
1327 unsigned int cmd, void *parg)
1328{
1329 struct dvb_device *dvbdev = file->private_data;
1330 struct dvb_frontend *fe = dvbdev->priv;
1331 int err = -EOPNOTSUPP;
e7fee0f3
ST
1332
1333 struct dtv_properties *tvps = NULL;
1334 struct dtv_property *tvp = NULL;
1335 int i;
13c97bf5
ST
1336
1337 dprintk("%s\n", __func__);
1338
6b73eeaf
ST
1339 if(cmd == FE_SET_PROPERTY) {
1340 printk("%s() FE_SET_PROPERTY\n", __FUNCTION__);
1341
e7fee0f3 1342 tvps = (struct dtv_properties __user *)parg;
6b73eeaf 1343
e7fee0f3
ST
1344 printk("%s() properties.num = %d\n", __FUNCTION__, tvps->num);
1345 printk("%s() properties.props = %p\n", __FUNCTION__, tvps->props);
1346
1347 /* Put an arbitrary limit on the number of messages that can
1348 * be sent at once */
1349 if (tvps->num > DTV_IOCTL_MAX_MSGS)
1350 return -EINVAL;
1351
1352 tvp = (struct dtv_property *) kmalloc(tvps->num *
1353 sizeof(struct dtv_property), GFP_KERNEL);
1354 if (!tvp) {
1355 err = -ENOMEM;
1356 goto out;
6b73eeaf
ST
1357 }
1358
e7fee0f3
ST
1359 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1360 err = -EFAULT;
1361 goto out;
1362 }
1363
1364 for (i = 0; i < tvps->num; i++)
1365 dtv_property_process(fe, tvp + i, inode, file);
1366
1367 if(fe->dtv_property_cache.state == DTV_TUNE) {
6b73eeaf 1368 printk("%s() Property cache is full, tuning\n", __FUNCTION__);
6b73eeaf
ST
1369 }
1370 err = 0;
1371 }
e7fee0f3
ST
1372out:
1373 kfree(tvp);
13c97bf5
ST
1374 return err;
1375}
1376
1377static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
1378 unsigned int cmd, void *parg)
1379{
1380 struct dvb_device *dvbdev = file->private_data;
1381 struct dvb_frontend *fe = dvbdev->priv;
1382 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1383 int err = -EOPNOTSUPP;
1384
1da177e4
LT
1385 switch (cmd) {
1386 case FE_GET_INFO: {
0c53c70f 1387 struct dvb_frontend_info* info = parg;
dea74869 1388 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
c471b331 1389 dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
1da177e4
LT
1390
1391 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
1392 * do it, it is done for it. */
1393 info->caps |= FE_CAN_INVERSION_AUTO;
1394 err = 0;
1395 break;
1396 }
1397
6757ccc5
PB
1398 case FE_READ_STATUS: {
1399 fe_status_t* status = parg;
1400
1401 /* if retune was requested but hasn't occured yet, prevent
1402 * that user get signal state from previous tuning */
1403 if(fepriv->state == FESTATE_RETUNE) {
1404 err=0;
1405 *status = 0;
1406 break;
1407 }
1408
dea74869
PB
1409 if (fe->ops.read_status)
1410 err = fe->ops.read_status(fe, status);
1da177e4 1411 break;
6757ccc5 1412 }
1da177e4 1413 case FE_READ_BER:
dea74869
PB
1414 if (fe->ops.read_ber)
1415 err = fe->ops.read_ber(fe, (__u32*) parg);
1da177e4
LT
1416 break;
1417
1418 case FE_READ_SIGNAL_STRENGTH:
dea74869
PB
1419 if (fe->ops.read_signal_strength)
1420 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
1da177e4
LT
1421 break;
1422
1423 case FE_READ_SNR:
dea74869
PB
1424 if (fe->ops.read_snr)
1425 err = fe->ops.read_snr(fe, (__u16*) parg);
1da177e4
LT
1426 break;
1427
1428 case FE_READ_UNCORRECTED_BLOCKS:
dea74869
PB
1429 if (fe->ops.read_ucblocks)
1430 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
1da177e4
LT
1431 break;
1432
1433
1434 case FE_DISEQC_RESET_OVERLOAD:
dea74869
PB
1435 if (fe->ops.diseqc_reset_overload) {
1436 err = fe->ops.diseqc_reset_overload(fe);
1da177e4
LT
1437 fepriv->state = FESTATE_DISEQC;
1438 fepriv->status = 0;
1439 }
1440 break;
1441
1442 case FE_DISEQC_SEND_MASTER_CMD:
dea74869
PB
1443 if (fe->ops.diseqc_send_master_cmd) {
1444 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
1da177e4
LT
1445 fepriv->state = FESTATE_DISEQC;
1446 fepriv->status = 0;
1447 }
1448 break;
1449
1450 case FE_DISEQC_SEND_BURST:
dea74869
PB
1451 if (fe->ops.diseqc_send_burst) {
1452 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
1da177e4
LT
1453 fepriv->state = FESTATE_DISEQC;
1454 fepriv->status = 0;
1455 }
1456 break;
1457
1458 case FE_SET_TONE:
dea74869
PB
1459 if (fe->ops.set_tone) {
1460 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
64454016 1461 fepriv->tone = (fe_sec_tone_mode_t) parg;
1da177e4
LT
1462 fepriv->state = FESTATE_DISEQC;
1463 fepriv->status = 0;
1464 }
1465 break;
1466
1467 case FE_SET_VOLTAGE:
dea74869
PB
1468 if (fe->ops.set_voltage) {
1469 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
64454016 1470 fepriv->voltage = (fe_sec_voltage_t) parg;
1da177e4
LT
1471 fepriv->state = FESTATE_DISEQC;
1472 fepriv->status = 0;
1473 }
1474 break;
1475
1476 case FE_DISHNETWORK_SEND_LEGACY_CMD:
dea74869
PB
1477 if (fe->ops.dishnetwork_send_legacy_command) {
1478 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
1da177e4
LT
1479 fepriv->state = FESTATE_DISEQC;
1480 fepriv->status = 0;
dea74869 1481 } else if (fe->ops.set_voltage) {
83b75b04
N
1482 /*
1483 * NOTE: This is a fallback condition. Some frontends
1484 * (stv0299 for instance) take longer than 8msec to
1485 * respond to a set_voltage command. Those switches
1486 * need custom routines to switch properly. For all
1487 * other frontends, the following shoule work ok.
1488 * Dish network legacy switches (as used by Dish500)
1489 * are controlled by sending 9-bit command words
1490 * spaced 8msec apart.
1491 * the actual command word is switch/port dependant
1492 * so it is up to the userspace application to send
1493 * the right command.
1494 * The command must always start with a '0' after
1495 * initialization, so parg is 8 bits and does not
1496 * include the initialization or start bit
1497 */
c6eb8eaf 1498 unsigned long swcmd = ((unsigned long) parg) << 1;
83b75b04
N
1499 struct timeval nexttime;
1500 struct timeval tv[10];
1501 int i;
1502 u8 last = 1;
1503 if (dvb_frontend_debug)
c6eb8eaf 1504 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
83b75b04
N
1505 do_gettimeofday(&nexttime);
1506 if (dvb_frontend_debug)
1507 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
1508 /* before sending a command, initialize by sending
1509 * a 32ms 18V to the switch
1510 */
dea74869 1511 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
83b75b04
N
1512 dvb_frontend_sleep_until(&nexttime, 32000);
1513
1514 for (i = 0; i < 9; i++) {
1515 if (dvb_frontend_debug)
1516 do_gettimeofday(&tv[i + 1]);
c6eb8eaf 1517 if ((swcmd & 0x01) != last) {
83b75b04 1518 /* set voltage to (last ? 13V : 18V) */
dea74869 1519 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
83b75b04
N
1520 last = (last) ? 0 : 1;
1521 }
c6eb8eaf 1522 swcmd = swcmd >> 1;
83b75b04
N
1523 if (i != 8)
1524 dvb_frontend_sleep_until(&nexttime, 8000);
1525 }
1526 if (dvb_frontend_debug) {
1527 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
46b4f7c1 1528 __func__, fe->dvb->num);
83b75b04
N
1529 for (i = 1; i < 10; i++)
1530 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
1531 }
1532 err = 0;
1533 fepriv->state = FESTATE_DISEQC;
1534 fepriv->status = 0;
1da177e4
LT
1535 }
1536 break;
1537
1538 case FE_DISEQC_RECV_SLAVE_REPLY:
dea74869
PB
1539 if (fe->ops.diseqc_recv_slave_reply)
1540 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
1da177e4
LT
1541 break;
1542
1543 case FE_ENABLE_HIGH_LNB_VOLTAGE:
dea74869
PB
1544 if (fe->ops.enable_high_lnb_voltage)
1545 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
1da177e4
LT
1546 break;
1547
1548 case FE_SET_FRONTEND: {
1549 struct dvb_frontend_tune_settings fetunesettings;
1550
e7fee0f3 1551 if(fe->dtv_property_cache.state == DTV_TUNE) {
6b73eeaf
ST
1552 if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
1553 err = -EINVAL;
1554 break;
1555 }
1556 } else {
1557 if (dvb_frontend_check_parameters(fe, parg) < 0) {
1558 err = -EINVAL;
1559 break;
1560 }
1fab46f0 1561
6b73eeaf
ST
1562 memcpy (&fepriv->parameters, parg,
1563 sizeof (struct dvb_frontend_parameters));
1564 }
1da177e4
LT
1565
1566 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1567 memcpy(&fetunesettings.parameters, parg,
1568 sizeof (struct dvb_frontend_parameters));
1569
1570 /* force auto frequency inversion if requested */
1571 if (dvb_force_auto_inversion) {
1572 fepriv->parameters.inversion = INVERSION_AUTO;
1573 fetunesettings.parameters.inversion = INVERSION_AUTO;
1574 }
dea74869 1575 if (fe->ops.info.type == FE_OFDM) {
1b3c3714 1576 /* without hierarchical coding code_rate_LP is irrelevant,
1da177e4
LT
1577 * so we tolerate the otherwise invalid FEC_NONE setting */
1578 if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
1579 fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
1580 fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
1581 }
1582
1583 /* get frontend-specific tuning settings */
dea74869 1584 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1da177e4
LT
1585 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1586 fepriv->max_drift = fetunesettings.max_drift;
1587 fepriv->step_size = fetunesettings.step_size;
1588 } else {
1589 /* default values */
dea74869 1590 switch(fe->ops.info.type) {
1da177e4
LT
1591 case FE_QPSK:
1592 fepriv->min_delay = HZ/20;
1593 fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
1594 fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
1595 break;
1596
1597 case FE_QAM:
1598 fepriv->min_delay = HZ/20;
1599 fepriv->step_size = 0; /* no zigzag */
1600 fepriv->max_drift = 0;
1601 break;
1602
1603 case FE_OFDM:
1604 fepriv->min_delay = HZ/20;
dea74869
PB
1605 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1606 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1da177e4
LT
1607 break;
1608 case FE_ATSC:
4821fb1d
MA
1609 fepriv->min_delay = HZ/20;
1610 fepriv->step_size = 0;
1611 fepriv->max_drift = 0;
1da177e4
LT
1612 break;
1613 }
1614 }
1615 if (dvb_override_tune_delay > 0)
1616 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1617
1618 fepriv->state = FESTATE_RETUNE;
1619 dvb_frontend_wakeup(fe);
1620 dvb_frontend_add_event(fe, 0);
1621 fepriv->status = 0;
1622 err = 0;
1623 break;
1624 }
1625
1626 case FE_GET_EVENT:
1627 err = dvb_frontend_get_event (fe, parg, file->f_flags);
1628 break;
1629
1630 case FE_GET_FRONTEND:
dea74869 1631 if (fe->ops.get_frontend) {
1da177e4 1632 memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
dea74869 1633 err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
1da177e4
LT
1634 }
1635 break;
36cb557a
AQ
1636
1637 case FE_SET_FRONTEND_TUNE_MODE:
e18828e4 1638 fepriv->tune_mode_flags = (unsigned long) parg;
1b172e0c 1639 err = 0;
36cb557a 1640 break;
1da177e4
LT
1641 };
1642
1da177e4
LT
1643 return err;
1644}
1645
6b73eeaf 1646
1da177e4
LT
1647static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
1648{
1649 struct dvb_device *dvbdev = file->private_data;
1650 struct dvb_frontend *fe = dvbdev->priv;
0c53c70f 1651 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4 1652
46b4f7c1 1653 dprintk ("%s\n", __func__);
1da177e4
LT
1654
1655 poll_wait (file, &fepriv->events.wait_queue, wait);
1656
1657 if (fepriv->events.eventw != fepriv->events.eventr)
1658 return (POLLIN | POLLRDNORM | POLLPRI);
1659
1660 return 0;
1661}
1662
1663static int dvb_frontend_open(struct inode *inode, struct file *file)
1664{
1665 struct dvb_device *dvbdev = file->private_data;
1666 struct dvb_frontend *fe = dvbdev->priv;
0c53c70f 1667 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1da177e4
LT
1668 int ret;
1669
46b4f7c1 1670 dprintk ("%s\n", __func__);
1da177e4 1671
48136e1e
OE
1672 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
1673 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
ba7e6f3e 1674 return ret;
ba7e6f3e
ST
1675 }
1676
48136e1e
OE
1677 if ((ret = dvb_generic_open (inode, file)) < 0)
1678 goto err1;
04c56d0e 1679
48136e1e 1680 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
04c56d0e
AQ
1681 /* normal tune mode when opened R/W */
1682 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
1683 fepriv->tone = -1;
1684 fepriv->voltage = -1;
1685
1da177e4
LT
1686 ret = dvb_frontend_start (fe);
1687 if (ret)
48136e1e 1688 goto err2;
1da177e4
LT
1689
1690 /* empty event queue */
1691 fepriv->events.eventr = fepriv->events.eventw = 0;
1692 }
1693
1694 return ret;
48136e1e
OE
1695
1696err2:
1697 dvb_generic_release(inode, file);
1698err1:
1699 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
1700 fe->ops.ts_bus_ctrl(fe, 0);
1701 return ret;
1da177e4
LT
1702}
1703
1704static int dvb_frontend_release(struct inode *inode, struct file *file)
1705{
1706 struct dvb_device *dvbdev = file->private_data;
1707 struct dvb_frontend *fe = dvbdev->priv;
0c53c70f 1708 struct dvb_frontend_private *fepriv = fe->frontend_priv;
ca5be9cd 1709 int ret;
1da177e4 1710
46b4f7c1 1711 dprintk ("%s\n", __func__);
1da177e4
LT
1712
1713 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
1714 fepriv->release_jiffies = jiffies;
1715
ca5be9cd
MR
1716 ret = dvb_generic_release (inode, file);
1717
48136e1e
OE
1718 if (dvbdev->users == -1) {
1719 if (fepriv->exit == 1) {
1720 fops_put(file->f_op);
1721 file->f_op = NULL;
1722 wake_up(&dvbdev->wait_queue);
1723 }
1724 if (fe->ops.ts_bus_ctrl)
1725 fe->ops.ts_bus_ctrl(fe, 0);
ca5be9cd 1726 }
48136e1e 1727
ca5be9cd 1728 return ret;
1da177e4
LT
1729}
1730
1731static struct file_operations dvb_frontend_fops = {
1732 .owner = THIS_MODULE,
1733 .ioctl = dvb_generic_ioctl,
1734 .poll = dvb_frontend_poll,
1735 .open = dvb_frontend_open,
1736 .release = dvb_frontend_release
1737};
1738
1739int dvb_register_frontend(struct dvb_adapter* dvb,
1740 struct dvb_frontend* fe)
1741{
1742 struct dvb_frontend_private *fepriv;
1743 static const struct dvb_device dvbdev_template = {
1744 .users = ~0,
1745 .writers = 1,
1746 .readers = (~0)-1,
1747 .fops = &dvb_frontend_fops,
1748 .kernel_ioctl = dvb_frontend_ioctl
1749 };
1750
46b4f7c1 1751 dprintk ("%s\n", __func__);
1da177e4 1752
3593cab5 1753 if (mutex_lock_interruptible(&frontend_mutex))
1da177e4
LT
1754 return -ERESTARTSYS;
1755
7408187d 1756 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
1da177e4 1757 if (fe->frontend_priv == NULL) {
3593cab5 1758 mutex_unlock(&frontend_mutex);
1da177e4
LT
1759 return -ENOMEM;
1760 }
0c53c70f 1761 fepriv = fe->frontend_priv;
1da177e4
LT
1762
1763 init_MUTEX (&fepriv->sem);
1764 init_waitqueue_head (&fepriv->wait_queue);
1765 init_waitqueue_head (&fepriv->events.wait_queue);
03b76123 1766 mutex_init(&fepriv->events.mtx);
1da177e4
LT
1767 fe->dvb = dvb;
1768 fepriv->inversion = INVERSION_OFF;
1769
1770 printk ("DVB: registering frontend %i (%s)...\n",
1771 fe->dvb->num,
dea74869 1772 fe->ops.info.name);
1da177e4
LT
1773
1774 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
1775 fe, DVB_DEVICE_FRONTEND);
1776
3593cab5 1777 mutex_unlock(&frontend_mutex);
1da177e4
LT
1778 return 0;
1779}
1780EXPORT_SYMBOL(dvb_register_frontend);
1781
1782int dvb_unregister_frontend(struct dvb_frontend* fe)
1783{
0c53c70f 1784 struct dvb_frontend_private *fepriv = fe->frontend_priv;
46b4f7c1 1785 dprintk ("%s\n", __func__);
1da177e4 1786
57861b43 1787 mutex_lock(&frontend_mutex);
ca5be9cd 1788 dvb_frontend_stop (fe);
57861b43
MR
1789 mutex_unlock(&frontend_mutex);
1790
1791 if (fepriv->dvbdev->users < -1)
1792 wait_event(fepriv->dvbdev->wait_queue,
1793 fepriv->dvbdev->users==-1);
1794
3593cab5 1795 mutex_lock(&frontend_mutex);
1da177e4 1796 dvb_unregister_device (fepriv->dvbdev);
d9955060 1797
1da177e4
LT
1798 /* fe is invalid now */
1799 kfree(fepriv);
3593cab5 1800 mutex_unlock(&frontend_mutex);
1da177e4
LT
1801 return 0;
1802}
1803EXPORT_SYMBOL(dvb_unregister_frontend);
f52a838b 1804
149ef72d 1805#ifdef CONFIG_MEDIA_ATTACH
f52a838b
AQ
1806void dvb_frontend_detach(struct dvb_frontend* fe)
1807{
1808 void *ptr;
1809
1810 if (fe->ops.release_sec) {
1811 fe->ops.release_sec(fe);
1812 symbol_put_addr(fe->ops.release_sec);
1813 }
1814 if (fe->ops.tuner_ops.release) {
1815 fe->ops.tuner_ops.release(fe);
1816 symbol_put_addr(fe->ops.tuner_ops.release);
1817 }
2426a27e
MK
1818 if (fe->ops.analog_ops.release) {
1819 fe->ops.analog_ops.release(fe);
1820 symbol_put_addr(fe->ops.analog_ops.release);
1821 }
f52a838b
AQ
1822 ptr = (void*)fe->ops.release;
1823 if (ptr) {
1824 fe->ops.release(fe);
1825 symbol_put_addr(ptr);
1826 }
1827}
1828#else
1829void dvb_frontend_detach(struct dvb_frontend* fe)
1830{
1831 if (fe->ops.release_sec)
1832 fe->ops.release_sec(fe);
1833 if (fe->ops.tuner_ops.release)
1834 fe->ops.tuner_ops.release(fe);
2426a27e
MK
1835 if (fe->ops.analog_ops.release)
1836 fe->ops.analog_ops.release(fe);
f52a838b
AQ
1837 if (fe->ops.release)
1838 fe->ops.release(fe);
1839}
1840#endif
1841EXPORT_SYMBOL(dvb_frontend_detach);