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