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