[media] dvb-core: Fix ISDB-T defaults
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / dvb / dvb-core / dvb_frontend.c
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 /* Enables DVBv3 compatibility bits at the headers */
29 #define __DVB_CORE__
30
31 #include <linux/string.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/wait.h>
35 #include <linux/slab.h>
36 #include <linux/poll.h>
37 #include <linux/semaphore.h>
38 #include <linux/module.h>
39 #include <linux/list.h>
40 #include <linux/freezer.h>
41 #include <linux/jiffies.h>
42 #include <linux/kthread.h>
43 #include <asm/processor.h>
44
45 #include "dvb_frontend.h"
46 #include "dvbdev.h"
47 #include <linux/dvb/version.h>
48
49 static int dvb_frontend_debug;
50 static int dvb_shutdown_timeout;
51 static int dvb_force_auto_inversion;
52 static int dvb_override_tune_delay;
53 static int dvb_powerdown_on_sleep = 1;
54 static int dvb_mfe_wait_time = 5;
55
56 module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
57 MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
58 module_param(dvb_shutdown_timeout, int, 0644);
59 MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
60 module_param(dvb_force_auto_inversion, int, 0644);
61 MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
62 module_param(dvb_override_tune_delay, int, 0644);
63 MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
64 module_param(dvb_powerdown_on_sleep, int, 0644);
65 MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
66 module_param(dvb_mfe_wait_time, int, 0644);
67 MODULE_PARM_DESC(dvb_mfe_wait_time, "Wait up to <mfe_wait_time> seconds on open() for multi-frontend to become available (default:5 seconds)");
68
69 #define dprintk if (dvb_frontend_debug) printk
70
71 #define FESTATE_IDLE 1
72 #define FESTATE_RETUNE 2
73 #define FESTATE_TUNING_FAST 4
74 #define FESTATE_TUNING_SLOW 8
75 #define FESTATE_TUNED 16
76 #define FESTATE_ZIGZAG_FAST 32
77 #define FESTATE_ZIGZAG_SLOW 64
78 #define FESTATE_DISEQC 128
79 #define FESTATE_ERROR 256
80 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
81 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
82 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
83 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
84
85 #define FE_ALGO_HW 1
86 /*
87 * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
88 * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
89 * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
90 * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
91 * FESTATE_TUNED. The frontend has successfully locked on.
92 * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
93 * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
94 * FESTATE_DISEQC. A DISEQC command has just been issued.
95 * FESTATE_WAITFORLOCK. When we're waiting for a lock.
96 * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
97 * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
98 * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
99 */
100
101 #define DVB_FE_NO_EXIT 0
102 #define DVB_FE_NORMAL_EXIT 1
103 #define DVB_FE_DEVICE_REMOVED 2
104
105 static DEFINE_MUTEX(frontend_mutex);
106
107 struct dvb_frontend_private {
108
109 /* thread/frontend values */
110 struct dvb_device *dvbdev;
111 struct dvb_frontend_parameters parameters_in;
112 struct dvb_frontend_parameters parameters_out;
113 struct dvb_fe_events events;
114 struct semaphore sem;
115 struct list_head list_head;
116 wait_queue_head_t wait_queue;
117 struct task_struct *thread;
118 unsigned long release_jiffies;
119 unsigned int exit;
120 unsigned int wakeup;
121 fe_status_t status;
122 unsigned long tune_mode_flags;
123 unsigned int delay;
124 unsigned int reinitialise;
125 int tone;
126 int voltage;
127
128 /* swzigzag values */
129 unsigned int state;
130 unsigned int bending;
131 int lnb_drift;
132 unsigned int inversion;
133 unsigned int auto_step;
134 unsigned int auto_sub_step;
135 unsigned int started_auto_step;
136 unsigned int min_delay;
137 unsigned int max_drift;
138 unsigned int step_size;
139 int quality;
140 unsigned int check_wrapped;
141 enum dvbfe_search algo_status;
142 };
143
144 static void dvb_frontend_wakeup(struct dvb_frontend *fe);
145 static int dtv_get_frontend(struct dvb_frontend *fe,
146 struct dvb_frontend_parameters *p_out);
147
148 static bool has_get_frontend(struct dvb_frontend *fe)
149 {
150 return fe->ops.get_frontend;
151 }
152
153 /*
154 * Due to DVBv3 API calls, a delivery system should be mapped into one of
155 * the 4 DVBv3 delivery systems (FE_QPSK, FE_QAM, FE_OFDM or FE_ATSC),
156 * otherwise, a DVBv3 call will fail.
157 */
158 enum dvbv3_emulation_type {
159 DVBV3_UNKNOWN,
160 DVBV3_QPSK,
161 DVBV3_QAM,
162 DVBV3_OFDM,
163 DVBV3_ATSC,
164 };
165
166 static enum dvbv3_emulation_type dvbv3_type(u32 delivery_system)
167 {
168 switch (delivery_system) {
169 case SYS_DVBC_ANNEX_A:
170 case SYS_DVBC_ANNEX_C:
171 return DVBV3_QAM;
172 case SYS_DVBS:
173 case SYS_DVBS2:
174 case SYS_TURBO:
175 case SYS_ISDBS:
176 case SYS_DSS:
177 return DVBV3_QPSK;
178 case SYS_DVBT:
179 case SYS_DVBT2:
180 case SYS_ISDBT:
181 case SYS_DMBTH:
182 return DVBV3_OFDM;
183 case SYS_ATSC:
184 case SYS_DVBC_ANNEX_B:
185 return DVBV3_ATSC;
186 case SYS_UNDEFINED:
187 case SYS_ISDBC:
188 case SYS_DVBH:
189 case SYS_DAB:
190 case SYS_ATSCMH:
191 default:
192 /*
193 * Doesn't know how to emulate those types and/or
194 * there's no frontend driver from this type yet
195 * with some emulation code, so, we're not sure yet how
196 * to handle them, or they're not compatible with a DVBv3 call.
197 */
198 return DVBV3_UNKNOWN;
199 }
200 }
201
202 static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
203 {
204 struct dvb_frontend_private *fepriv = fe->frontend_priv;
205 struct dvb_fe_events *events = &fepriv->events;
206 struct dvb_frontend_event *e;
207 int wp;
208
209 dprintk ("%s\n", __func__);
210
211 if ((status & FE_HAS_LOCK) && has_get_frontend(fe))
212 dtv_get_frontend(fe, &fepriv->parameters_out);
213
214 mutex_lock(&events->mtx);
215
216 wp = (events->eventw + 1) % MAX_EVENT;
217 if (wp == events->eventr) {
218 events->overflow = 1;
219 events->eventr = (events->eventr + 1) % MAX_EVENT;
220 }
221
222 e = &events->events[events->eventw];
223 e->status = status;
224 e->parameters = fepriv->parameters_out;
225
226 events->eventw = wp;
227
228 mutex_unlock(&events->mtx);
229
230 wake_up_interruptible (&events->wait_queue);
231 }
232
233 static int dvb_frontend_get_event(struct dvb_frontend *fe,
234 struct dvb_frontend_event *event, int flags)
235 {
236 struct dvb_frontend_private *fepriv = fe->frontend_priv;
237 struct dvb_fe_events *events = &fepriv->events;
238
239 dprintk ("%s\n", __func__);
240
241 if (events->overflow) {
242 events->overflow = 0;
243 return -EOVERFLOW;
244 }
245
246 if (events->eventw == events->eventr) {
247 int ret;
248
249 if (flags & O_NONBLOCK)
250 return -EWOULDBLOCK;
251
252 up(&fepriv->sem);
253
254 ret = wait_event_interruptible (events->wait_queue,
255 events->eventw != events->eventr);
256
257 if (down_interruptible (&fepriv->sem))
258 return -ERESTARTSYS;
259
260 if (ret < 0)
261 return ret;
262 }
263
264 mutex_lock(&events->mtx);
265 *event = events->events[events->eventr];
266 events->eventr = (events->eventr + 1) % MAX_EVENT;
267 mutex_unlock(&events->mtx);
268
269 return 0;
270 }
271
272 static void dvb_frontend_clear_events(struct dvb_frontend *fe)
273 {
274 struct dvb_frontend_private *fepriv = fe->frontend_priv;
275 struct dvb_fe_events *events = &fepriv->events;
276
277 mutex_lock(&events->mtx);
278 events->eventr = events->eventw;
279 mutex_unlock(&events->mtx);
280 }
281
282 static void dvb_frontend_init(struct dvb_frontend *fe)
283 {
284 dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
285 fe->dvb->num,
286 fe->id,
287 fe->ops.info.name);
288
289 if (fe->ops.init)
290 fe->ops.init(fe);
291 if (fe->ops.tuner_ops.init) {
292 if (fe->ops.i2c_gate_ctrl)
293 fe->ops.i2c_gate_ctrl(fe, 1);
294 fe->ops.tuner_ops.init(fe);
295 if (fe->ops.i2c_gate_ctrl)
296 fe->ops.i2c_gate_ctrl(fe, 0);
297 }
298 }
299
300 void dvb_frontend_reinitialise(struct dvb_frontend *fe)
301 {
302 struct dvb_frontend_private *fepriv = fe->frontend_priv;
303
304 fepriv->reinitialise = 1;
305 dvb_frontend_wakeup(fe);
306 }
307 EXPORT_SYMBOL(dvb_frontend_reinitialise);
308
309 static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
310 {
311 int q2;
312
313 dprintk ("%s\n", __func__);
314
315 if (locked)
316 (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
317 else
318 (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
319
320 q2 = fepriv->quality - 128;
321 q2 *= q2;
322
323 fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
324 }
325
326 /**
327 * Performs automatic twiddling of frontend parameters.
328 *
329 * @param fe The frontend concerned.
330 * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
331 * @returns Number of complete iterations that have been performed.
332 */
333 static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
334 {
335 int autoinversion;
336 int ready = 0;
337 int fe_set_err = 0;
338 struct dvb_frontend_private *fepriv = fe->frontend_priv;
339 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
340 int original_inversion = c->inversion;
341 u32 original_frequency = c->frequency;
342
343 /* are we using autoinversion? */
344 autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
345 (c->inversion == INVERSION_AUTO));
346
347 /* setup parameters correctly */
348 while(!ready) {
349 /* calculate the lnb_drift */
350 fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
351
352 /* wrap the auto_step if we've exceeded the maximum drift */
353 if (fepriv->lnb_drift > fepriv->max_drift) {
354 fepriv->auto_step = 0;
355 fepriv->auto_sub_step = 0;
356 fepriv->lnb_drift = 0;
357 }
358
359 /* perform inversion and +/- zigzag */
360 switch(fepriv->auto_sub_step) {
361 case 0:
362 /* try with the current inversion and current drift setting */
363 ready = 1;
364 break;
365
366 case 1:
367 if (!autoinversion) break;
368
369 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
370 ready = 1;
371 break;
372
373 case 2:
374 if (fepriv->lnb_drift == 0) break;
375
376 fepriv->lnb_drift = -fepriv->lnb_drift;
377 ready = 1;
378 break;
379
380 case 3:
381 if (fepriv->lnb_drift == 0) break;
382 if (!autoinversion) break;
383
384 fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
385 fepriv->lnb_drift = -fepriv->lnb_drift;
386 ready = 1;
387 break;
388
389 default:
390 fepriv->auto_step++;
391 fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
392 break;
393 }
394
395 if (!ready) fepriv->auto_sub_step++;
396 }
397
398 /* if this attempt would hit where we started, indicate a complete
399 * iteration has occurred */
400 if ((fepriv->auto_step == fepriv->started_auto_step) &&
401 (fepriv->auto_sub_step == 0) && check_wrapped) {
402 return 1;
403 }
404
405 dprintk("%s: drift:%i inversion:%i auto_step:%i "
406 "auto_sub_step:%i started_auto_step:%i\n",
407 __func__, fepriv->lnb_drift, fepriv->inversion,
408 fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
409
410 /* set the frontend itself */
411 c->frequency += fepriv->lnb_drift;
412 if (autoinversion)
413 c->inversion = fepriv->inversion;
414 tmp = *c;
415 if (fe->ops.set_frontend)
416 fe_set_err = fe->ops.set_frontend(fe);
417 *c = tmp;
418 if (fe_set_err < 0) {
419 fepriv->state = FESTATE_ERROR;
420 return fe_set_err;
421 }
422
423 c->frequency = original_frequency;
424 c->inversion = original_inversion;
425
426 fepriv->auto_sub_step++;
427 return 0;
428 }
429
430 static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
431 {
432 fe_status_t s = 0;
433 int retval = 0;
434 struct dvb_frontend_private *fepriv = fe->frontend_priv;
435 struct dtv_frontend_properties *c = &fe->dtv_property_cache, tmp;
436
437 /* if we've got no parameters, just keep idling */
438 if (fepriv->state & FESTATE_IDLE) {
439 fepriv->delay = 3*HZ;
440 fepriv->quality = 0;
441 return;
442 }
443
444 /* in SCAN mode, we just set the frontend when asked and leave it alone */
445 if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
446 if (fepriv->state & FESTATE_RETUNE) {
447 tmp = *c;
448 if (fe->ops.set_frontend)
449 retval = fe->ops.set_frontend(fe);
450 *c = tmp;
451 if (retval < 0)
452 fepriv->state = FESTATE_ERROR;
453 else
454 fepriv->state = FESTATE_TUNED;
455 }
456 fepriv->delay = 3*HZ;
457 fepriv->quality = 0;
458 return;
459 }
460
461 /* get the frontend status */
462 if (fepriv->state & FESTATE_RETUNE) {
463 s = 0;
464 } else {
465 if (fe->ops.read_status)
466 fe->ops.read_status(fe, &s);
467 if (s != fepriv->status) {
468 dvb_frontend_add_event(fe, s);
469 fepriv->status = s;
470 }
471 }
472
473 /* if we're not tuned, and we have a lock, move to the TUNED state */
474 if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
475 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
476 fepriv->state = FESTATE_TUNED;
477
478 /* if we're tuned, then we have determined the correct inversion */
479 if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
480 (c->inversion == INVERSION_AUTO)) {
481 c->inversion = fepriv->inversion;
482 }
483 return;
484 }
485
486 /* if we are tuned already, check we're still locked */
487 if (fepriv->state & FESTATE_TUNED) {
488 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
489
490 /* we're tuned, and the lock is still good... */
491 if (s & FE_HAS_LOCK) {
492 return;
493 } else { /* if we _WERE_ tuned, but now don't have a lock */
494 fepriv->state = FESTATE_ZIGZAG_FAST;
495 fepriv->started_auto_step = fepriv->auto_step;
496 fepriv->check_wrapped = 0;
497 }
498 }
499
500 /* don't actually do anything if we're in the LOSTLOCK state,
501 * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
502 if ((fepriv->state & FESTATE_LOSTLOCK) &&
503 (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
504 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
505 return;
506 }
507
508 /* don't do anything if we're in the DISEQC state, since this
509 * might be someone with a motorized dish controlled by DISEQC.
510 * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
511 if (fepriv->state & FESTATE_DISEQC) {
512 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
513 return;
514 }
515
516 /* if we're in the RETUNE state, set everything up for a brand
517 * new scan, keeping the current inversion setting, as the next
518 * tune is _very_ likely to require the same */
519 if (fepriv->state & FESTATE_RETUNE) {
520 fepriv->lnb_drift = 0;
521 fepriv->auto_step = 0;
522 fepriv->auto_sub_step = 0;
523 fepriv->started_auto_step = 0;
524 fepriv->check_wrapped = 0;
525 }
526
527 /* fast zigzag. */
528 if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
529 fepriv->delay = fepriv->min_delay;
530
531 /* perform a tune */
532 retval = dvb_frontend_swzigzag_autotune(fe,
533 fepriv->check_wrapped);
534 if (retval < 0) {
535 return;
536 } else if (retval) {
537 /* OK, if we've run out of trials at the fast speed.
538 * Drop back to slow for the _next_ attempt */
539 fepriv->state = FESTATE_SEARCHING_SLOW;
540 fepriv->started_auto_step = fepriv->auto_step;
541 return;
542 }
543 fepriv->check_wrapped = 1;
544
545 /* if we've just retuned, enter the ZIGZAG_FAST state.
546 * This ensures we cannot return from an
547 * FE_SET_FRONTEND ioctl before the first frontend tune
548 * occurs */
549 if (fepriv->state & FESTATE_RETUNE) {
550 fepriv->state = FESTATE_TUNING_FAST;
551 }
552 }
553
554 /* slow zigzag */
555 if (fepriv->state & FESTATE_SEARCHING_SLOW) {
556 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
557
558 /* Note: don't bother checking for wrapping; we stay in this
559 * state until we get a lock */
560 dvb_frontend_swzigzag_autotune(fe, 0);
561 }
562 }
563
564 static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
565 {
566 struct dvb_frontend_private *fepriv = fe->frontend_priv;
567
568 if (fepriv->exit != DVB_FE_NO_EXIT)
569 return 1;
570
571 if (fepriv->dvbdev->writers == 1)
572 if (time_after_eq(jiffies, fepriv->release_jiffies +
573 dvb_shutdown_timeout * HZ))
574 return 1;
575
576 return 0;
577 }
578
579 static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
580 {
581 struct dvb_frontend_private *fepriv = fe->frontend_priv;
582
583 if (fepriv->wakeup) {
584 fepriv->wakeup = 0;
585 return 1;
586 }
587 return dvb_frontend_is_exiting(fe);
588 }
589
590 static void dvb_frontend_wakeup(struct dvb_frontend *fe)
591 {
592 struct dvb_frontend_private *fepriv = fe->frontend_priv;
593
594 fepriv->wakeup = 1;
595 wake_up_interruptible(&fepriv->wait_queue);
596 }
597
598 static int dvb_frontend_thread(void *data)
599 {
600 struct dvb_frontend *fe = data;
601 struct dvb_frontend_private *fepriv = fe->frontend_priv;
602 fe_status_t s;
603 enum dvbfe_algo algo;
604
605 bool re_tune = false;
606
607 dprintk("%s\n", __func__);
608
609 fepriv->check_wrapped = 0;
610 fepriv->quality = 0;
611 fepriv->delay = 3*HZ;
612 fepriv->status = 0;
613 fepriv->wakeup = 0;
614 fepriv->reinitialise = 0;
615
616 dvb_frontend_init(fe);
617
618 set_freezable();
619 while (1) {
620 up(&fepriv->sem); /* is locked when we enter the thread... */
621 restart:
622 wait_event_interruptible_timeout(fepriv->wait_queue,
623 dvb_frontend_should_wakeup(fe) || kthread_should_stop()
624 || freezing(current),
625 fepriv->delay);
626
627 if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
628 /* got signal or quitting */
629 fepriv->exit = DVB_FE_NORMAL_EXIT;
630 break;
631 }
632
633 if (try_to_freeze())
634 goto restart;
635
636 if (down_interruptible(&fepriv->sem))
637 break;
638
639 if (fepriv->reinitialise) {
640 dvb_frontend_init(fe);
641 if (fe->ops.set_tone && fepriv->tone != -1)
642 fe->ops.set_tone(fe, fepriv->tone);
643 if (fe->ops.set_voltage && fepriv->voltage != -1)
644 fe->ops.set_voltage(fe, fepriv->voltage);
645 fepriv->reinitialise = 0;
646 }
647
648 /* do an iteration of the tuning loop */
649 if (fe->ops.get_frontend_algo) {
650 algo = fe->ops.get_frontend_algo(fe);
651 switch (algo) {
652 case DVBFE_ALGO_HW:
653 dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
654
655 if (fepriv->state & FESTATE_RETUNE) {
656 dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
657 re_tune = true;
658 fepriv->state = FESTATE_TUNED;
659 }
660
661 if (fe->ops.tune)
662 fe->ops.tune(fe, re_tune, fepriv->tune_mode_flags, &fepriv->delay, &s);
663
664 if (s != fepriv->status && !(fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT)) {
665 dprintk("%s: state changed, adding current state\n", __func__);
666 dvb_frontend_add_event(fe, s);
667 fepriv->status = s;
668 }
669 break;
670 case DVBFE_ALGO_SW:
671 dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
672 dvb_frontend_swzigzag(fe);
673 break;
674 case DVBFE_ALGO_CUSTOM:
675 dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
676 if (fepriv->state & FESTATE_RETUNE) {
677 dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
678 fepriv->state = FESTATE_TUNED;
679 }
680 /* Case where we are going to search for a carrier
681 * User asked us to retune again for some reason, possibly
682 * requesting a search with a new set of parameters
683 */
684 if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
685 if (fe->ops.search) {
686 fepriv->algo_status = fe->ops.search(fe);
687 /* We did do a search as was requested, the flags are
688 * now unset as well and has the flags wrt to search.
689 */
690 } else {
691 fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
692 }
693 }
694 /* Track the carrier if the search was successful */
695 if (fepriv->algo_status != DVBFE_ALGO_SEARCH_SUCCESS) {
696 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
697 fepriv->delay = HZ / 2;
698 }
699 fepriv->parameters_out = fepriv->parameters_in;
700 fe->ops.read_status(fe, &s);
701 if (s != fepriv->status) {
702 dvb_frontend_add_event(fe, s); /* update event list */
703 fepriv->status = s;
704 if (!(s & FE_HAS_LOCK)) {
705 fepriv->delay = HZ / 10;
706 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
707 } else {
708 fepriv->delay = 60 * HZ;
709 }
710 }
711 break;
712 default:
713 dprintk("%s: UNDEFINED ALGO !\n", __func__);
714 break;
715 }
716 } else {
717 dvb_frontend_swzigzag(fe);
718 }
719 }
720
721 if (dvb_powerdown_on_sleep) {
722 if (fe->ops.set_voltage)
723 fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
724 if (fe->ops.tuner_ops.sleep) {
725 if (fe->ops.i2c_gate_ctrl)
726 fe->ops.i2c_gate_ctrl(fe, 1);
727 fe->ops.tuner_ops.sleep(fe);
728 if (fe->ops.i2c_gate_ctrl)
729 fe->ops.i2c_gate_ctrl(fe, 0);
730 }
731 if (fe->ops.sleep)
732 fe->ops.sleep(fe);
733 }
734
735 fepriv->thread = NULL;
736 if (kthread_should_stop())
737 fepriv->exit = DVB_FE_DEVICE_REMOVED;
738 else
739 fepriv->exit = DVB_FE_NO_EXIT;
740 mb();
741
742 dvb_frontend_wakeup(fe);
743 return 0;
744 }
745
746 static void dvb_frontend_stop(struct dvb_frontend *fe)
747 {
748 struct dvb_frontend_private *fepriv = fe->frontend_priv;
749
750 dprintk ("%s\n", __func__);
751
752 fepriv->exit = DVB_FE_NORMAL_EXIT;
753 mb();
754
755 if (!fepriv->thread)
756 return;
757
758 kthread_stop(fepriv->thread);
759
760 sema_init(&fepriv->sem, 1);
761 fepriv->state = FESTATE_IDLE;
762
763 /* paranoia check in case a signal arrived */
764 if (fepriv->thread)
765 printk("dvb_frontend_stop: warning: thread %p won't exit\n",
766 fepriv->thread);
767 }
768
769 s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
770 {
771 return ((curtime.tv_usec < lasttime.tv_usec) ?
772 1000000 - lasttime.tv_usec + curtime.tv_usec :
773 curtime.tv_usec - lasttime.tv_usec);
774 }
775 EXPORT_SYMBOL(timeval_usec_diff);
776
777 static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
778 {
779 curtime->tv_usec += add_usec;
780 if (curtime->tv_usec >= 1000000) {
781 curtime->tv_usec -= 1000000;
782 curtime->tv_sec++;
783 }
784 }
785
786 /*
787 * Sleep until gettimeofday() > waketime + add_usec
788 * This needs to be as precise as possible, but as the delay is
789 * usually between 2ms and 32ms, it is done using a scheduled msleep
790 * followed by usleep (normally a busy-wait loop) for the remainder
791 */
792 void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
793 {
794 struct timeval lasttime;
795 s32 delta, newdelta;
796
797 timeval_usec_add(waketime, add_usec);
798
799 do_gettimeofday(&lasttime);
800 delta = timeval_usec_diff(lasttime, *waketime);
801 if (delta > 2500) {
802 msleep((delta - 1500) / 1000);
803 do_gettimeofday(&lasttime);
804 newdelta = timeval_usec_diff(lasttime, *waketime);
805 delta = (newdelta > delta) ? 0 : newdelta;
806 }
807 if (delta > 0)
808 udelay(delta);
809 }
810 EXPORT_SYMBOL(dvb_frontend_sleep_until);
811
812 static int dvb_frontend_start(struct dvb_frontend *fe)
813 {
814 int ret;
815 struct dvb_frontend_private *fepriv = fe->frontend_priv;
816 struct task_struct *fe_thread;
817
818 dprintk ("%s\n", __func__);
819
820 if (fepriv->thread) {
821 if (fepriv->exit == DVB_FE_NO_EXIT)
822 return 0;
823 else
824 dvb_frontend_stop (fe);
825 }
826
827 if (signal_pending(current))
828 return -EINTR;
829 if (down_interruptible (&fepriv->sem))
830 return -EINTR;
831
832 fepriv->state = FESTATE_IDLE;
833 fepriv->exit = DVB_FE_NO_EXIT;
834 fepriv->thread = NULL;
835 mb();
836
837 fe_thread = kthread_run(dvb_frontend_thread, fe,
838 "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
839 if (IS_ERR(fe_thread)) {
840 ret = PTR_ERR(fe_thread);
841 printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
842 up(&fepriv->sem);
843 return ret;
844 }
845 fepriv->thread = fe_thread;
846 return 0;
847 }
848
849 static void dvb_frontend_get_frequency_limits(struct dvb_frontend *fe,
850 u32 *freq_min, u32 *freq_max)
851 {
852 *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
853
854 if (fe->ops.info.frequency_max == 0)
855 *freq_max = fe->ops.tuner_ops.info.frequency_max;
856 else if (fe->ops.tuner_ops.info.frequency_max == 0)
857 *freq_max = fe->ops.info.frequency_max;
858 else
859 *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
860
861 if (*freq_min == 0 || *freq_max == 0)
862 printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
863 fe->dvb->num,fe->id);
864 }
865
866 static int dvb_frontend_check_parameters(struct dvb_frontend *fe)
867 {
868 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
869 u32 freq_min;
870 u32 freq_max;
871
872 /* range check: frequency */
873 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
874 if ((freq_min && c->frequency < freq_min) ||
875 (freq_max && c->frequency > freq_max)) {
876 printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
877 fe->dvb->num, fe->id, c->frequency, freq_min, freq_max);
878 return -EINVAL;
879 }
880
881 /* range check: symbol rate */
882 switch (c->delivery_system) {
883 case SYS_DVBS:
884 case SYS_DVBS2:
885 case SYS_TURBO:
886 case SYS_DVBC_ANNEX_A:
887 case SYS_DVBC_ANNEX_C:
888 if ((fe->ops.info.symbol_rate_min &&
889 c->symbol_rate < fe->ops.info.symbol_rate_min) ||
890 (fe->ops.info.symbol_rate_max &&
891 c->symbol_rate > fe->ops.info.symbol_rate_max)) {
892 printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
893 fe->dvb->num, fe->id, c->symbol_rate,
894 fe->ops.info.symbol_rate_min,
895 fe->ops.info.symbol_rate_max);
896 return -EINVAL;
897 }
898 default:
899 break;
900 }
901
902 /*
903 * check for supported modulation
904 *
905 * This is currently hacky. Also, it only works for DVB-S & friends,
906 * and not all modulations has FE_CAN flags
907 */
908 switch (c->delivery_system) {
909 case SYS_DVBS:
910 case SYS_DVBS2:
911 case SYS_TURBO:
912 if ((c->modulation > QAM_AUTO ||
913 !((1 << (c->modulation + 10)) & fe->ops.info.caps))) {
914 printk(KERN_WARNING
915 "DVB: adapter %i frontend %i modulation %u not supported\n",
916 fe->dvb->num, fe->id, c->modulation);
917 return -EINVAL;
918 }
919 break;
920 default:
921 /* FIXME: it makes sense to validate othere delsys here */
922 break;
923 }
924
925 return 0;
926 }
927
928 static int dvb_frontend_clear_cache(struct dvb_frontend *fe)
929 {
930 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
931 int i;
932
933 memset(c, 0, sizeof(struct dtv_frontend_properties));
934
935 c->state = DTV_CLEAR;
936
937 c->delivery_system = fe->ops.delsys[0];
938 dprintk("%s() Clearing cache for delivery system %d\n", __func__,
939 c->delivery_system);
940
941 c->transmission_mode = TRANSMISSION_MODE_AUTO;
942 c->bandwidth_hz = 0; /* AUTO */
943 c->guard_interval = GUARD_INTERVAL_AUTO;
944 c->hierarchy = HIERARCHY_AUTO;
945 c->symbol_rate = 0;
946 c->code_rate_HP = FEC_AUTO;
947 c->code_rate_LP = FEC_AUTO;
948 c->fec_inner = FEC_AUTO;
949 c->rolloff = ROLLOFF_AUTO;
950 c->voltage = SEC_VOLTAGE_OFF;
951 c->sectone = SEC_TONE_OFF;
952 c->pilot = PILOT_AUTO;
953
954 c->isdbt_partial_reception = 0;
955 c->isdbt_sb_mode = 0;
956 c->isdbt_sb_subchannel = 0;
957 c->isdbt_sb_segment_idx = 0;
958 c->isdbt_sb_segment_count = 0;
959 c->isdbt_layer_enabled = 0;
960 for (i = 0; i < 3; i++) {
961 c->layer[i].fec = FEC_AUTO;
962 c->layer[i].modulation = QAM_AUTO;
963 c->layer[i].interleaving = 0;
964 c->layer[i].segment_count = 0;
965 }
966
967 c->isdbs_ts_id = 0;
968 c->dvbt2_plp_id = 0;
969
970 switch (c->delivery_system) {
971 case SYS_DVBS:
972 case SYS_DVBS2:
973 case SYS_TURBO:
974 c->modulation = QPSK; /* implied for DVB-S in legacy API */
975 c->rolloff = ROLLOFF_35;/* implied for DVB-S */
976 break;
977 case SYS_ATSC:
978 c->modulation = VSB_8;
979 break;
980 default:
981 c->modulation = QAM_AUTO;
982 break;
983 }
984
985 return 0;
986 }
987
988 #define _DTV_CMD(n, s, b) \
989 [n] = { \
990 .name = #n, \
991 .cmd = n, \
992 .set = s,\
993 .buffer = b \
994 }
995
996 static struct dtv_cmds_h dtv_cmds[DTV_MAX_COMMAND + 1] = {
997 _DTV_CMD(DTV_TUNE, 1, 0),
998 _DTV_CMD(DTV_CLEAR, 1, 0),
999
1000 /* Set */
1001 _DTV_CMD(DTV_FREQUENCY, 1, 0),
1002 _DTV_CMD(DTV_BANDWIDTH_HZ, 1, 0),
1003 _DTV_CMD(DTV_MODULATION, 1, 0),
1004 _DTV_CMD(DTV_INVERSION, 1, 0),
1005 _DTV_CMD(DTV_DISEQC_MASTER, 1, 1),
1006 _DTV_CMD(DTV_SYMBOL_RATE, 1, 0),
1007 _DTV_CMD(DTV_INNER_FEC, 1, 0),
1008 _DTV_CMD(DTV_VOLTAGE, 1, 0),
1009 _DTV_CMD(DTV_TONE, 1, 0),
1010 _DTV_CMD(DTV_PILOT, 1, 0),
1011 _DTV_CMD(DTV_ROLLOFF, 1, 0),
1012 _DTV_CMD(DTV_DELIVERY_SYSTEM, 1, 0),
1013 _DTV_CMD(DTV_HIERARCHY, 1, 0),
1014 _DTV_CMD(DTV_CODE_RATE_HP, 1, 0),
1015 _DTV_CMD(DTV_CODE_RATE_LP, 1, 0),
1016 _DTV_CMD(DTV_GUARD_INTERVAL, 1, 0),
1017 _DTV_CMD(DTV_TRANSMISSION_MODE, 1, 0),
1018
1019 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 1, 0),
1020 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 1, 0),
1021 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 1, 0),
1022 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 1, 0),
1023 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 1, 0),
1024 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 1, 0),
1025 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 1, 0),
1026 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 1, 0),
1027 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 1, 0),
1028 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 1, 0),
1029 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 1, 0),
1030 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 1, 0),
1031 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 1, 0),
1032 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 1, 0),
1033 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 1, 0),
1034 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 1, 0),
1035 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 1, 0),
1036 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 1, 0),
1037
1038 _DTV_CMD(DTV_ISDBT_PARTIAL_RECEPTION, 0, 0),
1039 _DTV_CMD(DTV_ISDBT_SOUND_BROADCASTING, 0, 0),
1040 _DTV_CMD(DTV_ISDBT_SB_SUBCHANNEL_ID, 0, 0),
1041 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_IDX, 0, 0),
1042 _DTV_CMD(DTV_ISDBT_SB_SEGMENT_COUNT, 0, 0),
1043 _DTV_CMD(DTV_ISDBT_LAYER_ENABLED, 0, 0),
1044 _DTV_CMD(DTV_ISDBT_LAYERA_FEC, 0, 0),
1045 _DTV_CMD(DTV_ISDBT_LAYERA_MODULATION, 0, 0),
1046 _DTV_CMD(DTV_ISDBT_LAYERA_SEGMENT_COUNT, 0, 0),
1047 _DTV_CMD(DTV_ISDBT_LAYERA_TIME_INTERLEAVING, 0, 0),
1048 _DTV_CMD(DTV_ISDBT_LAYERB_FEC, 0, 0),
1049 _DTV_CMD(DTV_ISDBT_LAYERB_MODULATION, 0, 0),
1050 _DTV_CMD(DTV_ISDBT_LAYERB_SEGMENT_COUNT, 0, 0),
1051 _DTV_CMD(DTV_ISDBT_LAYERB_TIME_INTERLEAVING, 0, 0),
1052 _DTV_CMD(DTV_ISDBT_LAYERC_FEC, 0, 0),
1053 _DTV_CMD(DTV_ISDBT_LAYERC_MODULATION, 0, 0),
1054 _DTV_CMD(DTV_ISDBT_LAYERC_SEGMENT_COUNT, 0, 0),
1055 _DTV_CMD(DTV_ISDBT_LAYERC_TIME_INTERLEAVING, 0, 0),
1056
1057 _DTV_CMD(DTV_ISDBS_TS_ID, 1, 0),
1058 _DTV_CMD(DTV_DVBT2_PLP_ID, 1, 0),
1059
1060 /* Get */
1061 _DTV_CMD(DTV_DISEQC_SLAVE_REPLY, 0, 1),
1062 _DTV_CMD(DTV_API_VERSION, 0, 0),
1063 _DTV_CMD(DTV_CODE_RATE_HP, 0, 0),
1064 _DTV_CMD(DTV_CODE_RATE_LP, 0, 0),
1065 _DTV_CMD(DTV_GUARD_INTERVAL, 0, 0),
1066 _DTV_CMD(DTV_TRANSMISSION_MODE, 0, 0),
1067 _DTV_CMD(DTV_HIERARCHY, 0, 0),
1068
1069 _DTV_CMD(DTV_ENUM_DELSYS, 0, 0),
1070 };
1071
1072 static void dtv_property_dump(struct dtv_property *tvp)
1073 {
1074 int i;
1075
1076 if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
1077 printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
1078 __func__, tvp->cmd);
1079 return;
1080 }
1081
1082 dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
1083 ,__func__
1084 ,tvp->cmd
1085 ,dtv_cmds[ tvp->cmd ].name);
1086
1087 if(dtv_cmds[ tvp->cmd ].buffer) {
1088
1089 dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
1090 ,__func__
1091 ,tvp->u.buffer.len);
1092
1093 for(i = 0; i < tvp->u.buffer.len; i++)
1094 dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
1095 ,__func__
1096 ,i
1097 ,tvp->u.buffer.data[i]);
1098
1099 } else
1100 dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
1101 }
1102
1103 /* Synchronise the legacy tuning parameters into the cache, so that demodulator
1104 * drivers can use a single set_frontend tuning function, regardless of whether
1105 * it's being used for the legacy or new API, reducing code and complexity.
1106 */
1107 static int dtv_property_cache_sync(struct dvb_frontend *fe,
1108 struct dtv_frontend_properties *c,
1109 const struct dvb_frontend_parameters *p)
1110 {
1111 c->frequency = p->frequency;
1112 c->inversion = p->inversion;
1113
1114 switch (dvbv3_type(c->delivery_system)) {
1115 case DVBV3_QPSK:
1116 dprintk("%s() Preparing QPSK req\n", __func__);
1117 c->symbol_rate = p->u.qpsk.symbol_rate;
1118 c->fec_inner = p->u.qpsk.fec_inner;
1119 break;
1120 case DVBV3_QAM:
1121 dprintk("%s() Preparing QAM req\n", __func__);
1122 c->symbol_rate = p->u.qam.symbol_rate;
1123 c->fec_inner = p->u.qam.fec_inner;
1124 c->modulation = p->u.qam.modulation;
1125 break;
1126 case DVBV3_OFDM:
1127 dprintk("%s() Preparing OFDM req\n", __func__);
1128 switch (p->u.ofdm.bandwidth) {
1129 case BANDWIDTH_10_MHZ:
1130 c->bandwidth_hz = 10000000;
1131 break;
1132 case BANDWIDTH_8_MHZ:
1133 c->bandwidth_hz = 8000000;
1134 break;
1135 case BANDWIDTH_7_MHZ:
1136 c->bandwidth_hz = 7000000;
1137 break;
1138 case BANDWIDTH_6_MHZ:
1139 c->bandwidth_hz = 6000000;
1140 break;
1141 case BANDWIDTH_5_MHZ:
1142 c->bandwidth_hz = 5000000;
1143 break;
1144 case BANDWIDTH_1_712_MHZ:
1145 c->bandwidth_hz = 1712000;
1146 break;
1147 case BANDWIDTH_AUTO:
1148 c->bandwidth_hz = 0;
1149 }
1150
1151 c->code_rate_HP = p->u.ofdm.code_rate_HP;
1152 c->code_rate_LP = p->u.ofdm.code_rate_LP;
1153 c->modulation = p->u.ofdm.constellation;
1154 c->transmission_mode = p->u.ofdm.transmission_mode;
1155 c->guard_interval = p->u.ofdm.guard_interval;
1156 c->hierarchy = p->u.ofdm.hierarchy_information;
1157 break;
1158 case DVBV3_ATSC:
1159 dprintk("%s() Preparing ATSC req\n", __func__);
1160 c->modulation = p->u.vsb.modulation;
1161 if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
1162 c->delivery_system = SYS_ATSC;
1163 else
1164 c->delivery_system = SYS_DVBC_ANNEX_B;
1165 break;
1166 case DVBV3_UNKNOWN:
1167 printk(KERN_ERR
1168 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1169 __func__, c->delivery_system);
1170 return -EINVAL;
1171 }
1172
1173 return 0;
1174 }
1175
1176 /* Ensure the cached values are set correctly in the frontend
1177 * legacy tuning structures, for the advanced tuning API.
1178 */
1179 static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
1180 struct dvb_frontend_parameters *p)
1181 {
1182 const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1183
1184 p->frequency = c->frequency;
1185 p->inversion = c->inversion;
1186
1187 switch (dvbv3_type(c->delivery_system)) {
1188 case DVBV3_UNKNOWN:
1189 printk(KERN_ERR
1190 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1191 __func__, c->delivery_system);
1192 return -EINVAL;
1193 case DVBV3_QPSK:
1194 dprintk("%s() Preparing QPSK req\n", __func__);
1195 p->u.qpsk.symbol_rate = c->symbol_rate;
1196 p->u.qpsk.fec_inner = c->fec_inner;
1197 break;
1198 case DVBV3_QAM:
1199 dprintk("%s() Preparing QAM req\n", __func__);
1200 p->u.qam.symbol_rate = c->symbol_rate;
1201 p->u.qam.fec_inner = c->fec_inner;
1202 p->u.qam.modulation = c->modulation;
1203 break;
1204 case DVBV3_OFDM:
1205 dprintk("%s() Preparing OFDM req\n", __func__);
1206
1207 switch (c->bandwidth_hz) {
1208 case 10000000:
1209 p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
1210 break;
1211 case 8000000:
1212 p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
1213 break;
1214 case 7000000:
1215 p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
1216 break;
1217 case 6000000:
1218 p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
1219 break;
1220 case 5000000:
1221 p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
1222 break;
1223 case 1712000:
1224 p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
1225 break;
1226 case 0:
1227 default:
1228 p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
1229 }
1230 p->u.ofdm.code_rate_HP = c->code_rate_HP;
1231 p->u.ofdm.code_rate_LP = c->code_rate_LP;
1232 p->u.ofdm.constellation = c->modulation;
1233 p->u.ofdm.transmission_mode = c->transmission_mode;
1234 p->u.ofdm.guard_interval = c->guard_interval;
1235 p->u.ofdm.hierarchy_information = c->hierarchy;
1236 break;
1237 case DVBV3_ATSC:
1238 dprintk("%s() Preparing VSB req\n", __func__);
1239 p->u.vsb.modulation = c->modulation;
1240 break;
1241 }
1242 return 0;
1243 }
1244
1245 /**
1246 * dtv_get_frontend - calls a callback for retrieving DTV parameters
1247 * @fe: struct dvb_frontend pointer
1248 * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
1249 * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
1250 *
1251 * This routine calls either the DVBv3 or DVBv5 get_frontend call.
1252 * If c is not null, it will update the DVBv5 cache struct pointed by it.
1253 * If p_out is not null, it will update the DVBv3 params pointed by it.
1254 */
1255 static int dtv_get_frontend(struct dvb_frontend *fe,
1256 struct dvb_frontend_parameters *p_out)
1257 {
1258 int r;
1259
1260 if (fe->ops.get_frontend) {
1261 r = fe->ops.get_frontend(fe);
1262 if (unlikely(r < 0))
1263 return r;
1264 if (p_out)
1265 dtv_property_legacy_params_sync(fe, p_out);
1266 return 0;
1267 }
1268
1269 /* As everything is in cache, get_frontend fops are always supported */
1270 return 0;
1271 }
1272
1273 static int dvb_frontend_ioctl_legacy(struct file *file,
1274 unsigned int cmd, void *parg);
1275 static int dvb_frontend_ioctl_properties(struct file *file,
1276 unsigned int cmd, void *parg);
1277
1278 static int dtv_property_process_get(struct dvb_frontend *fe,
1279 const struct dtv_frontend_properties *c,
1280 struct dtv_property *tvp,
1281 struct file *file)
1282 {
1283 int r, ncaps;
1284
1285 switch(tvp->cmd) {
1286 case DTV_ENUM_DELSYS:
1287 ncaps = 0;
1288 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1289 tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
1290 ncaps++;
1291 }
1292 tvp->u.buffer.len = ncaps;
1293 break;
1294 case DTV_FREQUENCY:
1295 tvp->u.data = c->frequency;
1296 break;
1297 case DTV_MODULATION:
1298 tvp->u.data = c->modulation;
1299 break;
1300 case DTV_BANDWIDTH_HZ:
1301 tvp->u.data = c->bandwidth_hz;
1302 break;
1303 case DTV_INVERSION:
1304 tvp->u.data = c->inversion;
1305 break;
1306 case DTV_SYMBOL_RATE:
1307 tvp->u.data = c->symbol_rate;
1308 break;
1309 case DTV_INNER_FEC:
1310 tvp->u.data = c->fec_inner;
1311 break;
1312 case DTV_PILOT:
1313 tvp->u.data = c->pilot;
1314 break;
1315 case DTV_ROLLOFF:
1316 tvp->u.data = c->rolloff;
1317 break;
1318 case DTV_DELIVERY_SYSTEM:
1319 tvp->u.data = c->delivery_system;
1320 break;
1321 case DTV_VOLTAGE:
1322 tvp->u.data = c->voltage;
1323 break;
1324 case DTV_TONE:
1325 tvp->u.data = c->sectone;
1326 break;
1327 case DTV_API_VERSION:
1328 tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
1329 break;
1330 case DTV_CODE_RATE_HP:
1331 tvp->u.data = c->code_rate_HP;
1332 break;
1333 case DTV_CODE_RATE_LP:
1334 tvp->u.data = c->code_rate_LP;
1335 break;
1336 case DTV_GUARD_INTERVAL:
1337 tvp->u.data = c->guard_interval;
1338 break;
1339 case DTV_TRANSMISSION_MODE:
1340 tvp->u.data = c->transmission_mode;
1341 break;
1342 case DTV_HIERARCHY:
1343 tvp->u.data = c->hierarchy;
1344 break;
1345
1346 /* ISDB-T Support here */
1347 case DTV_ISDBT_PARTIAL_RECEPTION:
1348 tvp->u.data = c->isdbt_partial_reception;
1349 break;
1350 case DTV_ISDBT_SOUND_BROADCASTING:
1351 tvp->u.data = c->isdbt_sb_mode;
1352 break;
1353 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1354 tvp->u.data = c->isdbt_sb_subchannel;
1355 break;
1356 case DTV_ISDBT_SB_SEGMENT_IDX:
1357 tvp->u.data = c->isdbt_sb_segment_idx;
1358 break;
1359 case DTV_ISDBT_SB_SEGMENT_COUNT:
1360 tvp->u.data = c->isdbt_sb_segment_count;
1361 break;
1362 case DTV_ISDBT_LAYER_ENABLED:
1363 tvp->u.data = c->isdbt_layer_enabled;
1364 break;
1365 case DTV_ISDBT_LAYERA_FEC:
1366 tvp->u.data = c->layer[0].fec;
1367 break;
1368 case DTV_ISDBT_LAYERA_MODULATION:
1369 tvp->u.data = c->layer[0].modulation;
1370 break;
1371 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1372 tvp->u.data = c->layer[0].segment_count;
1373 break;
1374 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1375 tvp->u.data = c->layer[0].interleaving;
1376 break;
1377 case DTV_ISDBT_LAYERB_FEC:
1378 tvp->u.data = c->layer[1].fec;
1379 break;
1380 case DTV_ISDBT_LAYERB_MODULATION:
1381 tvp->u.data = c->layer[1].modulation;
1382 break;
1383 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1384 tvp->u.data = c->layer[1].segment_count;
1385 break;
1386 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1387 tvp->u.data = c->layer[1].interleaving;
1388 break;
1389 case DTV_ISDBT_LAYERC_FEC:
1390 tvp->u.data = c->layer[2].fec;
1391 break;
1392 case DTV_ISDBT_LAYERC_MODULATION:
1393 tvp->u.data = c->layer[2].modulation;
1394 break;
1395 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1396 tvp->u.data = c->layer[2].segment_count;
1397 break;
1398 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1399 tvp->u.data = c->layer[2].interleaving;
1400 break;
1401 case DTV_ISDBS_TS_ID:
1402 tvp->u.data = c->isdbs_ts_id;
1403 break;
1404 case DTV_DVBT2_PLP_ID:
1405 tvp->u.data = c->dvbt2_plp_id;
1406 break;
1407 default:
1408 return -EINVAL;
1409 }
1410
1411 /* Allow the frontend to override outgoing properties */
1412 if (fe->ops.get_property) {
1413 r = fe->ops.get_property(fe, tvp);
1414 if (r < 0)
1415 return r;
1416 }
1417
1418 dtv_property_dump(tvp);
1419
1420 return 0;
1421 }
1422
1423 static int dtv_set_frontend(struct dvb_frontend *fe);
1424
1425 static bool is_dvbv3_delsys(u32 delsys)
1426 {
1427 bool status;
1428
1429 status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
1430 (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
1431
1432 return status;
1433 }
1434
1435 static int set_delivery_system(struct dvb_frontend *fe, u32 desired_system)
1436 {
1437 int ncaps, i;
1438 u32 delsys = SYS_UNDEFINED;
1439 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1440 enum dvbv3_emulation_type type;
1441
1442 if (desired_system == SYS_UNDEFINED) {
1443 /*
1444 * A DVBv3 call doesn't know what's the desired system.
1445 * So, don't change the current delivery system. Instead,
1446 * find the closest DVBv3 system that matches the delivery
1447 * system.
1448 */
1449 if (is_dvbv3_delsys(c->delivery_system)) {
1450 dprintk("%s() Using delivery system to %d\n",
1451 __func__, c->delivery_system);
1452 return 0;
1453 }
1454 type = dvbv3_type(c->delivery_system);
1455 switch (type) {
1456 case DVBV3_QPSK:
1457 desired_system = FE_QPSK;
1458 break;
1459 case DVBV3_QAM:
1460 desired_system = FE_QAM;
1461 break;
1462 case DVBV3_ATSC:
1463 desired_system = FE_ATSC;
1464 break;
1465 case DVBV3_OFDM:
1466 desired_system = FE_OFDM;
1467 break;
1468 default:
1469 dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
1470 __func__);
1471 return -EINVAL;
1472 }
1473 delsys = c->delivery_system;
1474 } else {
1475 /*
1476 * Check if the desired delivery system is supported
1477 */
1478 ncaps = 0;
1479 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1480 if (fe->ops.delsys[ncaps] == desired_system) {
1481 c->delivery_system = desired_system;
1482 dprintk("%s() Changing delivery system to %d\n",
1483 __func__, desired_system);
1484 return 0;
1485 }
1486 }
1487 type = dvbv3_type(desired_system);
1488
1489 /*
1490 * The delivery system is not supported. See if it can be
1491 * emulated.
1492 * The emulation only works if the desired system is one of the
1493 * DVBv3 delivery systems
1494 */
1495 if (!is_dvbv3_delsys(desired_system)) {
1496 dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1497 __func__);
1498 return -EINVAL;
1499 }
1500
1501 /*
1502 * Get the last non-DVBv3 delivery system that has the same type
1503 * of the desired system
1504 */
1505 ncaps = 0;
1506 while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
1507 if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
1508 !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
1509 delsys = fe->ops.delsys[ncaps];
1510 ncaps++;
1511 }
1512 /* There's nothing compatible with the desired delivery system */
1513 if (delsys == SYS_UNDEFINED) {
1514 dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1515 __func__);
1516 return -EINVAL;
1517 }
1518 c->delivery_system = delsys;
1519 }
1520
1521 /*
1522 * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
1523 * for older DVBv5 applications. The emulation will try to use
1524 * the auto mode for most things, and will assume that the desired
1525 * delivery system is the last one at the ops.delsys[] array
1526 */
1527 dprintk("%s() Using delivery system %d emulated as if it were a %d\n",
1528 __func__, delsys, desired_system);
1529
1530 /*
1531 * For now, handles ISDB-T calls. More code may be needed here for the
1532 * other emulated stuff
1533 */
1534 if (type == DVBV3_OFDM) {
1535 if (c->delivery_system == SYS_ISDBT) {
1536 dprintk("%s() Using defaults for SYS_ISDBT\n",
1537 __func__);
1538 if (!c->bandwidth_hz)
1539 c->bandwidth_hz = 6000000;
1540
1541 c->isdbt_partial_reception = 0;
1542 c->isdbt_sb_mode = 0;
1543 c->isdbt_sb_subchannel = 0;
1544 c->isdbt_sb_segment_idx = 0;
1545 c->isdbt_sb_segment_count = 0;
1546 c->isdbt_layer_enabled = 0;
1547 for (i = 0; i < 3; i++) {
1548 c->layer[i].fec = FEC_AUTO;
1549 c->layer[i].modulation = QAM_AUTO;
1550 c->layer[i].interleaving = 0;
1551 c->layer[i].segment_count = 0;
1552 }
1553 }
1554 }
1555 return 0;
1556 }
1557
1558 static int dtv_property_process_set(struct dvb_frontend *fe,
1559 struct dtv_property *tvp,
1560 struct file *file)
1561 {
1562 int r = 0;
1563 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1564 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1565 dtv_property_dump(tvp);
1566
1567 /* Allow the frontend to validate incoming properties */
1568 if (fe->ops.set_property) {
1569 r = fe->ops.set_property(fe, tvp);
1570 if (r < 0)
1571 return r;
1572 }
1573
1574 switch(tvp->cmd) {
1575 case DTV_CLEAR:
1576 /*
1577 * Reset a cache of data specific to the frontend here. This does
1578 * not effect hardware.
1579 */
1580 dvb_frontend_clear_cache(fe);
1581 break;
1582 case DTV_TUNE:
1583 /* interpret the cache of data, build either a traditional frontend
1584 * tunerequest so we can pass validation in the FE_SET_FRONTEND
1585 * ioctl.
1586 */
1587 c->state = tvp->cmd;
1588 dprintk("%s() Finalised property cache\n", __func__);
1589
1590 /* Needed, due to status update */
1591 dtv_property_legacy_params_sync(fe, &fepriv->parameters_in);
1592
1593 r = dtv_set_frontend(fe);
1594 break;
1595 case DTV_FREQUENCY:
1596 c->frequency = tvp->u.data;
1597 break;
1598 case DTV_MODULATION:
1599 c->modulation = tvp->u.data;
1600 break;
1601 case DTV_BANDWIDTH_HZ:
1602 c->bandwidth_hz = tvp->u.data;
1603 break;
1604 case DTV_INVERSION:
1605 c->inversion = tvp->u.data;
1606 break;
1607 case DTV_SYMBOL_RATE:
1608 c->symbol_rate = tvp->u.data;
1609 break;
1610 case DTV_INNER_FEC:
1611 c->fec_inner = tvp->u.data;
1612 break;
1613 case DTV_PILOT:
1614 c->pilot = tvp->u.data;
1615 break;
1616 case DTV_ROLLOFF:
1617 c->rolloff = tvp->u.data;
1618 break;
1619 case DTV_DELIVERY_SYSTEM:
1620 r = set_delivery_system(fe, tvp->u.data);
1621 break;
1622 case DTV_VOLTAGE:
1623 c->voltage = tvp->u.data;
1624 r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
1625 (void *)c->voltage);
1626 break;
1627 case DTV_TONE:
1628 c->sectone = tvp->u.data;
1629 r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
1630 (void *)c->sectone);
1631 break;
1632 case DTV_CODE_RATE_HP:
1633 c->code_rate_HP = tvp->u.data;
1634 break;
1635 case DTV_CODE_RATE_LP:
1636 c->code_rate_LP = tvp->u.data;
1637 break;
1638 case DTV_GUARD_INTERVAL:
1639 c->guard_interval = tvp->u.data;
1640 break;
1641 case DTV_TRANSMISSION_MODE:
1642 c->transmission_mode = tvp->u.data;
1643 break;
1644 case DTV_HIERARCHY:
1645 c->hierarchy = tvp->u.data;
1646 break;
1647
1648 /* ISDB-T Support here */
1649 case DTV_ISDBT_PARTIAL_RECEPTION:
1650 c->isdbt_partial_reception = tvp->u.data;
1651 break;
1652 case DTV_ISDBT_SOUND_BROADCASTING:
1653 c->isdbt_sb_mode = tvp->u.data;
1654 break;
1655 case DTV_ISDBT_SB_SUBCHANNEL_ID:
1656 c->isdbt_sb_subchannel = tvp->u.data;
1657 break;
1658 case DTV_ISDBT_SB_SEGMENT_IDX:
1659 c->isdbt_sb_segment_idx = tvp->u.data;
1660 break;
1661 case DTV_ISDBT_SB_SEGMENT_COUNT:
1662 c->isdbt_sb_segment_count = tvp->u.data;
1663 break;
1664 case DTV_ISDBT_LAYER_ENABLED:
1665 c->isdbt_layer_enabled = tvp->u.data;
1666 break;
1667 case DTV_ISDBT_LAYERA_FEC:
1668 c->layer[0].fec = tvp->u.data;
1669 break;
1670 case DTV_ISDBT_LAYERA_MODULATION:
1671 c->layer[0].modulation = tvp->u.data;
1672 break;
1673 case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
1674 c->layer[0].segment_count = tvp->u.data;
1675 break;
1676 case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
1677 c->layer[0].interleaving = tvp->u.data;
1678 break;
1679 case DTV_ISDBT_LAYERB_FEC:
1680 c->layer[1].fec = tvp->u.data;
1681 break;
1682 case DTV_ISDBT_LAYERB_MODULATION:
1683 c->layer[1].modulation = tvp->u.data;
1684 break;
1685 case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
1686 c->layer[1].segment_count = tvp->u.data;
1687 break;
1688 case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
1689 c->layer[1].interleaving = tvp->u.data;
1690 break;
1691 case DTV_ISDBT_LAYERC_FEC:
1692 c->layer[2].fec = tvp->u.data;
1693 break;
1694 case DTV_ISDBT_LAYERC_MODULATION:
1695 c->layer[2].modulation = tvp->u.data;
1696 break;
1697 case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
1698 c->layer[2].segment_count = tvp->u.data;
1699 break;
1700 case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
1701 c->layer[2].interleaving = tvp->u.data;
1702 break;
1703 case DTV_ISDBS_TS_ID:
1704 c->isdbs_ts_id = tvp->u.data;
1705 break;
1706 case DTV_DVBT2_PLP_ID:
1707 c->dvbt2_plp_id = tvp->u.data;
1708 break;
1709 default:
1710 return -EINVAL;
1711 }
1712
1713 return r;
1714 }
1715
1716 static int dvb_frontend_ioctl(struct file *file,
1717 unsigned int cmd, void *parg)
1718 {
1719 struct dvb_device *dvbdev = file->private_data;
1720 struct dvb_frontend *fe = dvbdev->priv;
1721 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1722 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1723 int err = -EOPNOTSUPP;
1724
1725 dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
1726
1727 if (fepriv->exit != DVB_FE_NO_EXIT)
1728 return -ENODEV;
1729
1730 if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
1731 (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
1732 cmd == FE_DISEQC_RECV_SLAVE_REPLY))
1733 return -EPERM;
1734
1735 if (down_interruptible (&fepriv->sem))
1736 return -ERESTARTSYS;
1737
1738 if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
1739 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1740 else {
1741 c->state = DTV_UNDEFINED;
1742 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1743 }
1744
1745 up(&fepriv->sem);
1746 return err;
1747 }
1748
1749 static int dvb_frontend_ioctl_properties(struct file *file,
1750 unsigned int cmd, void *parg)
1751 {
1752 struct dvb_device *dvbdev = file->private_data;
1753 struct dvb_frontend *fe = dvbdev->priv;
1754 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1755 int err = 0;
1756
1757 struct dtv_properties *tvps = NULL;
1758 struct dtv_property *tvp = NULL;
1759 int i;
1760
1761 dprintk("%s\n", __func__);
1762
1763 if(cmd == FE_SET_PROPERTY) {
1764 tvps = (struct dtv_properties __user *)parg;
1765
1766 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1767 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1768
1769 /* Put an arbitrary limit on the number of messages that can
1770 * be sent at once */
1771 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1772 return -EINVAL;
1773
1774 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1775 if (!tvp) {
1776 err = -ENOMEM;
1777 goto out;
1778 }
1779
1780 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1781 err = -EFAULT;
1782 goto out;
1783 }
1784
1785 for (i = 0; i < tvps->num; i++) {
1786 err = dtv_property_process_set(fe, tvp + i, file);
1787 if (err < 0)
1788 goto out;
1789 (tvp + i)->result = err;
1790 }
1791
1792 if (c->state == DTV_TUNE)
1793 dprintk("%s() Property cache is full, tuning\n", __func__);
1794
1795 } else
1796 if(cmd == FE_GET_PROPERTY) {
1797 tvps = (struct dtv_properties __user *)parg;
1798
1799 dprintk("%s() properties.num = %d\n", __func__, tvps->num);
1800 dprintk("%s() properties.props = %p\n", __func__, tvps->props);
1801
1802 /* Put an arbitrary limit on the number of messages that can
1803 * be sent at once */
1804 if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
1805 return -EINVAL;
1806
1807 tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
1808 if (!tvp) {
1809 err = -ENOMEM;
1810 goto out;
1811 }
1812
1813 if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
1814 err = -EFAULT;
1815 goto out;
1816 }
1817
1818 /*
1819 * Fills the cache out struct with the cache contents, plus
1820 * the data retrieved from get_frontend.
1821 */
1822 dtv_get_frontend(fe, NULL);
1823 for (i = 0; i < tvps->num; i++) {
1824 err = dtv_property_process_get(fe, c, tvp + i, file);
1825 if (err < 0)
1826 goto out;
1827 (tvp + i)->result = err;
1828 }
1829
1830 if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
1831 err = -EFAULT;
1832 goto out;
1833 }
1834
1835 } else
1836 err = -EOPNOTSUPP;
1837
1838 out:
1839 kfree(tvp);
1840 return err;
1841 }
1842
1843 static int dtv_set_frontend(struct dvb_frontend *fe)
1844 {
1845 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1846 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1847 struct dvb_frontend_tune_settings fetunesettings;
1848 u32 rolloff = 0;
1849
1850 if (dvb_frontend_check_parameters(fe) < 0)
1851 return -EINVAL;
1852
1853 /*
1854 * Initialize output parameters to match the values given by
1855 * the user. FE_SET_FRONTEND triggers an initial frontend event
1856 * with status = 0, which copies output parameters to userspace.
1857 *
1858 * This is still needed for DVBv5 calls, due to event state update.
1859 */
1860 fepriv->parameters_out = fepriv->parameters_in;
1861
1862 /*
1863 * Be sure that the bandwidth will be filled for all
1864 * non-satellite systems, as tuners need to know what
1865 * low pass/Nyquist half filter should be applied, in
1866 * order to avoid inter-channel noise.
1867 *
1868 * ISDB-T and DVB-T/T2 already sets bandwidth.
1869 * ATSC and DVB-C don't set, so, the core should fill it.
1870 *
1871 * On DVB-C Annex A and C, the bandwidth is a function of
1872 * the roll-off and symbol rate. Annex B defines different
1873 * roll-off factors depending on the modulation. Fortunately,
1874 * Annex B is only used with 6MHz, so there's no need to
1875 * calculate it.
1876 *
1877 * While not officially supported, a side effect of handling it at
1878 * the cache level is that a program could retrieve the bandwidth
1879 * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
1880 */
1881 switch (c->delivery_system) {
1882 case SYS_ATSC:
1883 case SYS_DVBC_ANNEX_B:
1884 c->bandwidth_hz = 6000000;
1885 break;
1886 case SYS_DVBC_ANNEX_A:
1887 rolloff = 115;
1888 break;
1889 case SYS_DVBC_ANNEX_C:
1890 rolloff = 113;
1891 break;
1892 default:
1893 break;
1894 }
1895 if (rolloff)
1896 c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
1897
1898 /* force auto frequency inversion if requested */
1899 if (dvb_force_auto_inversion)
1900 c->inversion = INVERSION_AUTO;
1901
1902 /*
1903 * without hierarchical coding code_rate_LP is irrelevant,
1904 * so we tolerate the otherwise invalid FEC_NONE setting
1905 */
1906 if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
1907 c->code_rate_LP = FEC_AUTO;
1908
1909 /* get frontend-specific tuning settings */
1910 memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
1911 if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
1912 fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
1913 fepriv->max_drift = fetunesettings.max_drift;
1914 fepriv->step_size = fetunesettings.step_size;
1915 } else {
1916 /* default values */
1917 switch (c->delivery_system) {
1918 case SYS_DVBC_ANNEX_A:
1919 case SYS_DVBC_ANNEX_C:
1920 fepriv->min_delay = HZ / 20;
1921 fepriv->step_size = c->symbol_rate / 16000;
1922 fepriv->max_drift = c->symbol_rate / 2000;
1923 break;
1924 case SYS_DVBT:
1925 case SYS_DVBT2:
1926 case SYS_ISDBT:
1927 case SYS_DMBTH:
1928 fepriv->min_delay = HZ / 20;
1929 fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
1930 fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
1931 break;
1932 default:
1933 /*
1934 * FIXME: This sounds wrong! if freqency_stepsize is
1935 * defined by the frontend, why not use it???
1936 */
1937 fepriv->min_delay = HZ / 20;
1938 fepriv->step_size = 0; /* no zigzag */
1939 fepriv->max_drift = 0;
1940 break;
1941 }
1942 }
1943 if (dvb_override_tune_delay > 0)
1944 fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
1945
1946 fepriv->state = FESTATE_RETUNE;
1947
1948 /* Request the search algorithm to search */
1949 fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
1950
1951 dvb_frontend_clear_events(fe);
1952 dvb_frontend_add_event(fe, 0);
1953 dvb_frontend_wakeup(fe);
1954 fepriv->status = 0;
1955
1956 return 0;
1957 }
1958
1959
1960 static int dvb_frontend_ioctl_legacy(struct file *file,
1961 unsigned int cmd, void *parg)
1962 {
1963 struct dvb_device *dvbdev = file->private_data;
1964 struct dvb_frontend *fe = dvbdev->priv;
1965 struct dvb_frontend_private *fepriv = fe->frontend_priv;
1966 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1967 int cb_err, err = -EOPNOTSUPP;
1968
1969 if (fe->dvb->fe_ioctl_override) {
1970 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
1971 DVB_FE_IOCTL_PRE);
1972 if (cb_err < 0)
1973 return cb_err;
1974 if (cb_err > 0)
1975 return 0;
1976 /* fe_ioctl_override returning 0 allows
1977 * dvb-core to continue handling the ioctl */
1978 }
1979
1980 switch (cmd) {
1981 case FE_GET_INFO: {
1982 struct dvb_frontend_info* info = parg;
1983 memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
1984 dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
1985
1986 /*
1987 * Associate the 4 delivery systems supported by DVBv3
1988 * API with their DVBv5 counterpart. For the other standards,
1989 * use the closest type, assuming that it would hopefully
1990 * work with a DVBv3 application.
1991 * It should be noticed that, on multi-frontend devices with
1992 * different types (terrestrial and cable, for example),
1993 * a pure DVBv3 application won't be able to use all delivery
1994 * systems. Yet, changing the DVBv5 cache to the other delivery
1995 * system should be enough for making it work.
1996 */
1997 switch (dvbv3_type(c->delivery_system)) {
1998 case DVBV3_QPSK:
1999 fe->ops.info.type = FE_QPSK;
2000 break;
2001 case DVBV3_ATSC:
2002 fe->ops.info.type = FE_ATSC;
2003 break;
2004 case DVBV3_QAM:
2005 fe->ops.info.type = FE_QAM;
2006 break;
2007 case DVBV3_OFDM:
2008 fe->ops.info.type = FE_OFDM;
2009 break;
2010 default:
2011 printk(KERN_ERR
2012 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2013 __func__, c->delivery_system);
2014 fe->ops.info.type = FE_OFDM;
2015 }
2016
2017 /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
2018 * do it, it is done for it. */
2019 info->caps |= FE_CAN_INVERSION_AUTO;
2020 err = 0;
2021 break;
2022 }
2023
2024 case FE_READ_STATUS: {
2025 fe_status_t* status = parg;
2026
2027 /* if retune was requested but hasn't occurred yet, prevent
2028 * that user get signal state from previous tuning */
2029 if (fepriv->state == FESTATE_RETUNE ||
2030 fepriv->state == FESTATE_ERROR) {
2031 err=0;
2032 *status = 0;
2033 break;
2034 }
2035
2036 if (fe->ops.read_status)
2037 err = fe->ops.read_status(fe, status);
2038 break;
2039 }
2040 case FE_READ_BER:
2041 if (fe->ops.read_ber)
2042 err = fe->ops.read_ber(fe, (__u32*) parg);
2043 break;
2044
2045 case FE_READ_SIGNAL_STRENGTH:
2046 if (fe->ops.read_signal_strength)
2047 err = fe->ops.read_signal_strength(fe, (__u16*) parg);
2048 break;
2049
2050 case FE_READ_SNR:
2051 if (fe->ops.read_snr)
2052 err = fe->ops.read_snr(fe, (__u16*) parg);
2053 break;
2054
2055 case FE_READ_UNCORRECTED_BLOCKS:
2056 if (fe->ops.read_ucblocks)
2057 err = fe->ops.read_ucblocks(fe, (__u32*) parg);
2058 break;
2059
2060
2061 case FE_DISEQC_RESET_OVERLOAD:
2062 if (fe->ops.diseqc_reset_overload) {
2063 err = fe->ops.diseqc_reset_overload(fe);
2064 fepriv->state = FESTATE_DISEQC;
2065 fepriv->status = 0;
2066 }
2067 break;
2068
2069 case FE_DISEQC_SEND_MASTER_CMD:
2070 if (fe->ops.diseqc_send_master_cmd) {
2071 err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
2072 fepriv->state = FESTATE_DISEQC;
2073 fepriv->status = 0;
2074 }
2075 break;
2076
2077 case FE_DISEQC_SEND_BURST:
2078 if (fe->ops.diseqc_send_burst) {
2079 err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
2080 fepriv->state = FESTATE_DISEQC;
2081 fepriv->status = 0;
2082 }
2083 break;
2084
2085 case FE_SET_TONE:
2086 if (fe->ops.set_tone) {
2087 err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
2088 fepriv->tone = (fe_sec_tone_mode_t) parg;
2089 fepriv->state = FESTATE_DISEQC;
2090 fepriv->status = 0;
2091 }
2092 break;
2093
2094 case FE_SET_VOLTAGE:
2095 if (fe->ops.set_voltage) {
2096 err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
2097 fepriv->voltage = (fe_sec_voltage_t) parg;
2098 fepriv->state = FESTATE_DISEQC;
2099 fepriv->status = 0;
2100 }
2101 break;
2102
2103 case FE_DISHNETWORK_SEND_LEGACY_CMD:
2104 if (fe->ops.dishnetwork_send_legacy_command) {
2105 err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
2106 fepriv->state = FESTATE_DISEQC;
2107 fepriv->status = 0;
2108 } else if (fe->ops.set_voltage) {
2109 /*
2110 * NOTE: This is a fallback condition. Some frontends
2111 * (stv0299 for instance) take longer than 8msec to
2112 * respond to a set_voltage command. Those switches
2113 * need custom routines to switch properly. For all
2114 * other frontends, the following should work ok.
2115 * Dish network legacy switches (as used by Dish500)
2116 * are controlled by sending 9-bit command words
2117 * spaced 8msec apart.
2118 * the actual command word is switch/port dependent
2119 * so it is up to the userspace application to send
2120 * the right command.
2121 * The command must always start with a '0' after
2122 * initialization, so parg is 8 bits and does not
2123 * include the initialization or start bit
2124 */
2125 unsigned long swcmd = ((unsigned long) parg) << 1;
2126 struct timeval nexttime;
2127 struct timeval tv[10];
2128 int i;
2129 u8 last = 1;
2130 if (dvb_frontend_debug)
2131 printk("%s switch command: 0x%04lx\n", __func__, swcmd);
2132 do_gettimeofday(&nexttime);
2133 if (dvb_frontend_debug)
2134 memcpy(&tv[0], &nexttime, sizeof(struct timeval));
2135 /* before sending a command, initialize by sending
2136 * a 32ms 18V to the switch
2137 */
2138 fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
2139 dvb_frontend_sleep_until(&nexttime, 32000);
2140
2141 for (i = 0; i < 9; i++) {
2142 if (dvb_frontend_debug)
2143 do_gettimeofday(&tv[i + 1]);
2144 if ((swcmd & 0x01) != last) {
2145 /* set voltage to (last ? 13V : 18V) */
2146 fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
2147 last = (last) ? 0 : 1;
2148 }
2149 swcmd = swcmd >> 1;
2150 if (i != 8)
2151 dvb_frontend_sleep_until(&nexttime, 8000);
2152 }
2153 if (dvb_frontend_debug) {
2154 printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
2155 __func__, fe->dvb->num);
2156 for (i = 1; i < 10; i++)
2157 printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
2158 }
2159 err = 0;
2160 fepriv->state = FESTATE_DISEQC;
2161 fepriv->status = 0;
2162 }
2163 break;
2164
2165 case FE_DISEQC_RECV_SLAVE_REPLY:
2166 if (fe->ops.diseqc_recv_slave_reply)
2167 err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
2168 break;
2169
2170 case FE_ENABLE_HIGH_LNB_VOLTAGE:
2171 if (fe->ops.enable_high_lnb_voltage)
2172 err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
2173 break;
2174
2175 case FE_SET_FRONTEND:
2176 /* Synchronise DVBv5 parameters from DVBv3 */
2177 memcpy (&fepriv->parameters_in, parg,
2178 sizeof (struct dvb_frontend_parameters));
2179
2180 err = set_delivery_system(fe, SYS_UNDEFINED);
2181 if (err)
2182 break;
2183
2184 err = dtv_property_cache_sync(fe, c, &fepriv->parameters_in);
2185 if (err)
2186 break;
2187 err = dtv_set_frontend(fe);
2188 break;
2189 case FE_GET_EVENT:
2190 err = dvb_frontend_get_event (fe, parg, file->f_flags);
2191 break;
2192
2193 case FE_GET_FRONTEND:
2194 err = dtv_get_frontend(fe, &fepriv->parameters_out);
2195 if (err >= 0)
2196 memcpy(parg, &fepriv->parameters_out,
2197 sizeof(struct dvb_frontend_parameters));
2198 break;
2199
2200 case FE_SET_FRONTEND_TUNE_MODE:
2201 fepriv->tune_mode_flags = (unsigned long) parg;
2202 err = 0;
2203 break;
2204 };
2205
2206 if (fe->dvb->fe_ioctl_override) {
2207 cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
2208 DVB_FE_IOCTL_POST);
2209 if (cb_err < 0)
2210 return cb_err;
2211 }
2212
2213 return err;
2214 }
2215
2216
2217 static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
2218 {
2219 struct dvb_device *dvbdev = file->private_data;
2220 struct dvb_frontend *fe = dvbdev->priv;
2221 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2222
2223 dprintk ("%s\n", __func__);
2224
2225 poll_wait (file, &fepriv->events.wait_queue, wait);
2226
2227 if (fepriv->events.eventw != fepriv->events.eventr)
2228 return (POLLIN | POLLRDNORM | POLLPRI);
2229
2230 return 0;
2231 }
2232
2233 static int dvb_frontend_open(struct inode *inode, struct file *file)
2234 {
2235 struct dvb_device *dvbdev = file->private_data;
2236 struct dvb_frontend *fe = dvbdev->priv;
2237 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2238 struct dvb_adapter *adapter = fe->dvb;
2239 int ret;
2240
2241 dprintk ("%s\n", __func__);
2242 if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
2243 return -ENODEV;
2244
2245 if (adapter->mfe_shared) {
2246 mutex_lock (&adapter->mfe_lock);
2247
2248 if (adapter->mfe_dvbdev == NULL)
2249 adapter->mfe_dvbdev = dvbdev;
2250
2251 else if (adapter->mfe_dvbdev != dvbdev) {
2252 struct dvb_device
2253 *mfedev = adapter->mfe_dvbdev;
2254 struct dvb_frontend
2255 *mfe = mfedev->priv;
2256 struct dvb_frontend_private
2257 *mfepriv = mfe->frontend_priv;
2258 int mferetry = (dvb_mfe_wait_time << 1);
2259
2260 mutex_unlock (&adapter->mfe_lock);
2261 while (mferetry-- && (mfedev->users != -1 ||
2262 mfepriv->thread != NULL)) {
2263 if(msleep_interruptible(500)) {
2264 if(signal_pending(current))
2265 return -EINTR;
2266 }
2267 }
2268
2269 mutex_lock (&adapter->mfe_lock);
2270 if(adapter->mfe_dvbdev != dvbdev) {
2271 mfedev = adapter->mfe_dvbdev;
2272 mfe = mfedev->priv;
2273 mfepriv = mfe->frontend_priv;
2274 if (mfedev->users != -1 ||
2275 mfepriv->thread != NULL) {
2276 mutex_unlock (&adapter->mfe_lock);
2277 return -EBUSY;
2278 }
2279 adapter->mfe_dvbdev = dvbdev;
2280 }
2281 }
2282 }
2283
2284 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
2285 if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
2286 goto err0;
2287
2288 /* If we took control of the bus, we need to force
2289 reinitialization. This is because many ts_bus_ctrl()
2290 functions strobe the RESET pin on the demod, and if the
2291 frontend thread already exists then the dvb_init() routine
2292 won't get called (which is what usually does initial
2293 register configuration). */
2294 fepriv->reinitialise = 1;
2295 }
2296
2297 if ((ret = dvb_generic_open (inode, file)) < 0)
2298 goto err1;
2299
2300 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2301 /* normal tune mode when opened R/W */
2302 fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
2303 fepriv->tone = -1;
2304 fepriv->voltage = -1;
2305
2306 ret = dvb_frontend_start (fe);
2307 if (ret)
2308 goto err2;
2309
2310 /* empty event queue */
2311 fepriv->events.eventr = fepriv->events.eventw = 0;
2312 }
2313
2314 if (adapter->mfe_shared)
2315 mutex_unlock (&adapter->mfe_lock);
2316 return ret;
2317
2318 err2:
2319 dvb_generic_release(inode, file);
2320 err1:
2321 if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
2322 fe->ops.ts_bus_ctrl(fe, 0);
2323 err0:
2324 if (adapter->mfe_shared)
2325 mutex_unlock (&adapter->mfe_lock);
2326 return ret;
2327 }
2328
2329 static int dvb_frontend_release(struct inode *inode, struct file *file)
2330 {
2331 struct dvb_device *dvbdev = file->private_data;
2332 struct dvb_frontend *fe = dvbdev->priv;
2333 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2334 int ret;
2335
2336 dprintk ("%s\n", __func__);
2337
2338 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
2339 fepriv->release_jiffies = jiffies;
2340 mb();
2341 }
2342
2343 ret = dvb_generic_release (inode, file);
2344
2345 if (dvbdev->users == -1) {
2346 wake_up(&fepriv->wait_queue);
2347 if (fepriv->exit != DVB_FE_NO_EXIT) {
2348 fops_put(file->f_op);
2349 file->f_op = NULL;
2350 wake_up(&dvbdev->wait_queue);
2351 }
2352 if (fe->ops.ts_bus_ctrl)
2353 fe->ops.ts_bus_ctrl(fe, 0);
2354 }
2355
2356 return ret;
2357 }
2358
2359 static const struct file_operations dvb_frontend_fops = {
2360 .owner = THIS_MODULE,
2361 .unlocked_ioctl = dvb_generic_ioctl,
2362 .poll = dvb_frontend_poll,
2363 .open = dvb_frontend_open,
2364 .release = dvb_frontend_release,
2365 .llseek = noop_llseek,
2366 };
2367
2368 int dvb_register_frontend(struct dvb_adapter* dvb,
2369 struct dvb_frontend* fe)
2370 {
2371 struct dvb_frontend_private *fepriv;
2372 static const struct dvb_device dvbdev_template = {
2373 .users = ~0,
2374 .writers = 1,
2375 .readers = (~0)-1,
2376 .fops = &dvb_frontend_fops,
2377 .kernel_ioctl = dvb_frontend_ioctl
2378 };
2379
2380 dprintk ("%s\n", __func__);
2381
2382 if (mutex_lock_interruptible(&frontend_mutex))
2383 return -ERESTARTSYS;
2384
2385 fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
2386 if (fe->frontend_priv == NULL) {
2387 mutex_unlock(&frontend_mutex);
2388 return -ENOMEM;
2389 }
2390 fepriv = fe->frontend_priv;
2391
2392 sema_init(&fepriv->sem, 1);
2393 init_waitqueue_head (&fepriv->wait_queue);
2394 init_waitqueue_head (&fepriv->events.wait_queue);
2395 mutex_init(&fepriv->events.mtx);
2396 fe->dvb = dvb;
2397 fepriv->inversion = INVERSION_OFF;
2398
2399 printk ("DVB: registering adapter %i frontend %i (%s)...\n",
2400 fe->dvb->num,
2401 fe->id,
2402 fe->ops.info.name);
2403
2404 dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
2405 fe, DVB_DEVICE_FRONTEND);
2406
2407 /*
2408 * Initialize the cache to the proper values according with the
2409 * first supported delivery system (ops->delsys[0])
2410 */
2411 dvb_frontend_clear_cache(fe);
2412
2413 mutex_unlock(&frontend_mutex);
2414 return 0;
2415 }
2416 EXPORT_SYMBOL(dvb_register_frontend);
2417
2418 int dvb_unregister_frontend(struct dvb_frontend* fe)
2419 {
2420 struct dvb_frontend_private *fepriv = fe->frontend_priv;
2421 dprintk ("%s\n", __func__);
2422
2423 mutex_lock(&frontend_mutex);
2424 dvb_frontend_stop (fe);
2425 mutex_unlock(&frontend_mutex);
2426
2427 if (fepriv->dvbdev->users < -1)
2428 wait_event(fepriv->dvbdev->wait_queue,
2429 fepriv->dvbdev->users==-1);
2430
2431 mutex_lock(&frontend_mutex);
2432 dvb_unregister_device (fepriv->dvbdev);
2433
2434 /* fe is invalid now */
2435 kfree(fepriv);
2436 mutex_unlock(&frontend_mutex);
2437 return 0;
2438 }
2439 EXPORT_SYMBOL(dvb_unregister_frontend);
2440
2441 #ifdef CONFIG_MEDIA_ATTACH
2442 void dvb_frontend_detach(struct dvb_frontend* fe)
2443 {
2444 void *ptr;
2445
2446 if (fe->ops.release_sec) {
2447 fe->ops.release_sec(fe);
2448 symbol_put_addr(fe->ops.release_sec);
2449 }
2450 if (fe->ops.tuner_ops.release) {
2451 fe->ops.tuner_ops.release(fe);
2452 symbol_put_addr(fe->ops.tuner_ops.release);
2453 }
2454 if (fe->ops.analog_ops.release) {
2455 fe->ops.analog_ops.release(fe);
2456 symbol_put_addr(fe->ops.analog_ops.release);
2457 }
2458 ptr = (void*)fe->ops.release;
2459 if (ptr) {
2460 fe->ops.release(fe);
2461 symbol_put_addr(ptr);
2462 }
2463 }
2464 #else
2465 void dvb_frontend_detach(struct dvb_frontend* fe)
2466 {
2467 if (fe->ops.release_sec)
2468 fe->ops.release_sec(fe);
2469 if (fe->ops.tuner_ops.release)
2470 fe->ops.tuner_ops.release(fe);
2471 if (fe->ops.analog_ops.release)
2472 fe->ops.analog_ops.release(fe);
2473 if (fe->ops.release)
2474 fe->ops.release(fe);
2475 }
2476 #endif
2477 EXPORT_SYMBOL(dvb_frontend_detach);