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