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