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>
38 #include <linux/module.h>
39 #include <linux/list.h>
43 #include <asm/processor.h>
49 static int dvb_frontend_debug;
50 static int dvb_shutdown_timeout;
51 static int dvb_force_auto_inversion;
52 static int dvb_override_tune_delay;
53 static int dvb_powerdown_on_sleep = 1;
54 static int dvb_mfe_wait_time = 5;
57 MODULE_PARM_DESC(frontend_debug,
"Turn on/off frontend core debugging (default:off).");
59 MODULE_PARM_DESC(dvb_shutdown_timeout,
"wait <shutdown_timeout> seconds after close() before suspending hardware");
61 MODULE_PARM_DESC(dvb_force_auto_inversion,
"0: normal (default), 1: INVERSION_AUTO forced always");
63 MODULE_PARM_DESC(dvb_override_tune_delay,
"0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
65 MODULE_PARM_DESC(dvb_powerdown_on_sleep,
"0: do not power down, 1: turn LNB voltage off on sleep (default)");
67 MODULE_PARM_DESC(dvb_mfe_wait_time,
"Wait up to <mfe_wait_time> seconds on open() for multi-frontend to become available (default:5 seconds)");
69 #define FESTATE_IDLE 1
70 #define FESTATE_RETUNE 2
71 #define FESTATE_TUNING_FAST 4
72 #define FESTATE_TUNING_SLOW 8
73 #define FESTATE_TUNED 16
74 #define FESTATE_ZIGZAG_FAST 32
75 #define FESTATE_ZIGZAG_SLOW 64
76 #define FESTATE_DISEQC 128
77 #define FESTATE_ERROR 256
78 #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
79 #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
80 #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
81 #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
99 #define DVB_FE_NO_EXIT 0
100 #define DVB_FE_NORMAL_EXIT 1
101 #define DVB_FE_DEVICE_REMOVED 2
141 static void dvb_frontend_wakeup(
struct dvb_frontend *fe);
144 static int dtv_property_legacy_params_sync(
struct dvb_frontend *fe,
149 return fe->
ops.get_frontend !=
NULL;
167 switch (delivery_system) {
210 if ((status &
FE_HAS_LOCK) && has_get_frontend(fe))
216 if (wp == events->eventr) {
217 events->overflow = 1;
218 events->eventr = (events->eventr + 1) %
MAX_EVENT;
221 e = &events->events[events->eventw];
232 static int dvb_frontend_get_event(
struct dvb_frontend *fe,
236 struct dvb_fe_events *events = &fepriv->
events;
240 if (events->overflow) {
241 events->overflow = 0;
245 if (events->eventw == events->eventr) {
254 events->eventw != events->eventr);
264 *
event = events->events[events->eventr];
265 events->eventr = (events->eventr + 1) %
MAX_EVENT;
271 static void dvb_frontend_clear_events(
struct dvb_frontend *fe)
274 struct dvb_fe_events *events = &fepriv->
events;
277 events->eventr = events->eventw;
284 "%s: initialising adapter %i frontend %i (%s)...\n",
285 __func__, fe->
dvb->num, fe->
id, fe->
ops.info.name);
289 if (fe->
ops.tuner_ops.init) {
290 if (fe->
ops.i2c_gate_ctrl)
291 fe->
ops.i2c_gate_ctrl(fe, 1);
292 fe->
ops.tuner_ops.init(fe);
293 if (fe->
ops.i2c_gate_ctrl)
294 fe->
ops.i2c_gate_ctrl(fe, 0);
303 dvb_frontend_wakeup(fe);
332 static int dvb_frontend_swzigzag_autotune(
struct dvb_frontend *fe,
int check_wrapped)
366 if (!autoinversion)
break;
381 if (!autoinversion)
break;
404 dev_dbg(fe->
dvb->device,
"%s: drift:%i inversion:%i auto_step:%i " \
405 "auto_sub_step:%i started_auto_step:%i\n",
415 if (fe->
ops.set_frontend)
416 fe_set_err = fe->
ops.set_frontend(fe);
418 if (fe_set_err < 0) {
430 static void dvb_frontend_swzigzag(
struct dvb_frontend *fe)
448 if (fe->
ops.set_frontend)
449 retval = fe->
ops.set_frontend(fe);
465 if (fe->
ops.read_status)
466 fe->
ops.read_status(fe, &s);
467 if (s != fepriv->
status) {
468 dvb_frontend_add_event(fe, s);
475 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
488 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
491 if (s & FE_HAS_LOCK) {
504 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
512 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
532 retval = dvb_frontend_swzigzag_autotune(fe,
556 dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
560 dvb_frontend_swzigzag_autotune(fe, 0);
564 static int dvb_frontend_is_exiting(
struct dvb_frontend *fe)
571 if (fepriv->
dvbdev->writers == 1)
573 dvb_shutdown_timeout *
HZ))
579 static int dvb_frontend_should_wakeup(
struct dvb_frontend *fe)
587 return dvb_frontend_is_exiting(fe);
590 static void dvb_frontend_wakeup(
struct dvb_frontend *fe)
598 static int dvb_frontend_thread(
void *
data)
605 bool re_tune =
false;
616 dvb_frontend_init(fe);
640 dvb_frontend_init(fe);
641 if (fe->
ops.set_tone && fepriv->
tone != -1)
642 fe->
ops.set_tone(fe, fepriv->
tone);
643 if (fe->
ops.set_voltage && fepriv->
voltage != -1)
649 if (fe->
ops.get_frontend_algo) {
650 algo = fe->
ops.get_frontend_algo(fe);
653 dev_dbg(fe->
dvb->device,
"%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
656 dev_dbg(fe->
dvb->device,
"%s: Retune requested, FESTATE_RETUNE\n", __func__);
667 dev_dbg(fe->
dvb->device,
"%s: state changed, adding current state\n", __func__);
668 dvb_frontend_add_event(fe, s);
673 dev_dbg(fe->
dvb->device,
"%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
674 dvb_frontend_swzigzag(fe);
677 dev_dbg(fe->
dvb->device,
"%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->
state);
679 dev_dbg(fe->
dvb->device,
"%s: Retune requested, FESTAT_RETUNE\n", __func__);
687 if (fe->
ops.search) {
702 fe->
ops.read_status(fe, &s);
703 if (s != fepriv->
status) {
704 dvb_frontend_add_event(fe, s);
706 if (!(s & FE_HAS_LOCK)) {
715 dev_dbg(fe->
dvb->device,
"%s: UNDEFINED ALGO !\n", __func__);
719 dvb_frontend_swzigzag(fe);
723 if (dvb_powerdown_on_sleep) {
724 if (fe->
ops.set_voltage)
726 if (fe->
ops.tuner_ops.sleep) {
727 if (fe->
ops.i2c_gate_ctrl)
728 fe->
ops.i2c_gate_ctrl(fe, 1);
729 fe->
ops.tuner_ops.sleep(fe);
730 if (fe->
ops.i2c_gate_ctrl)
731 fe->
ops.i2c_gate_ctrl(fe, 0);
744 dvb_frontend_wakeup(fe);
762 sema_init(&fepriv->
sem, 1);
768 "dvb_frontend_stop: warning: thread %p won't exit\n",
780 static inline void timeval_usec_add(
struct timeval *curtime,
u32 add_usec)
783 if (curtime->
tv_usec >= 1000000) {
800 timeval_usec_add(waketime, add_usec);
805 msleep((delta - 1500) / 1000);
808 delta = (newdelta >
delta) ? 0 : newdelta;
827 dvb_frontend_stop (fe);
841 "kdvb-ad-%i-fe-%i", fe->
dvb->num,fe->
id);
842 if (IS_ERR(fe_thread)) {
843 ret = PTR_ERR(fe_thread);
845 "dvb_frontend_start: failed to start kthread (%d)\n",
850 fepriv->
thread = fe_thread;
854 static void dvb_frontend_get_frequency_limits(
struct dvb_frontend *fe,
855 u32 *freq_min,
u32 *freq_max)
857 *freq_min =
max(fe->
ops.info.frequency_min, fe->
ops.tuner_ops.info.frequency_min);
859 if (fe->
ops.info.frequency_max == 0)
860 *freq_max = fe->
ops.tuner_ops.info.frequency_max;
861 else if (fe->
ops.tuner_ops.info.frequency_max == 0)
862 *freq_max = fe->
ops.info.frequency_max;
864 *freq_max =
min(fe->
ops.info.frequency_max, fe->
ops.tuner_ops.info.frequency_max);
866 if (*freq_min == 0 || *freq_max == 0)
867 dev_warn(fe->
dvb->device,
"DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
868 fe->
dvb->num, fe->
id);
871 static int dvb_frontend_check_parameters(
struct dvb_frontend *fe)
878 dvb_frontend_get_frequency_limits(fe, &freq_min, &freq_max);
879 if ((freq_min && c->
frequency < freq_min) ||
881 dev_warn(fe->
dvb->device,
"DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
894 if ((fe->
ops.info.symbol_rate_min &&
896 (fe->
ops.info.symbol_rate_max &&
898 dev_warn(fe->
dvb->device,
"DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
900 fe->
ops.info.symbol_rate_min,
901 fe->
ops.info.symbol_rate_max);
911 static int dvb_frontend_clear_cache(
struct dvb_frontend *fe)
923 dev_dbg(fe->
dvb->device,
"%s: Clearing cache for delivery system %d\n",
945 for (i = 0; i < 3; i++) {
948 c->
layer[
i].interleaving = 0;
949 c->
layer[
i].segment_count = 0;
974 #define _DTV_CMD(n, s, b) \
1068 dev_warn(fe->
dvb->device,
"%s: tvp.cmd = 0x%08x undefined\n",
1069 __func__, tvp->
cmd);
1073 dev_dbg(fe->
dvb->device,
"%s: tvp.cmd = 0x%08x (%s)\n", __func__,
1077 dev_dbg(fe->
dvb->device,
"%s: tvp.u.buffer.len = 0x%02x\n",
1080 for(i = 0; i < tvp->
u.
buffer.len; i++)
1082 "%s: tvp.u.buffer.data[0x%02x] = 0x%02x\n",
1083 __func__, i, tvp->
u.
buffer.data[i]);
1085 dev_dbg(fe->
dvb->device,
"%s: tvp.u.data = 0x%08x\n", __func__,
1094 static int dtv_property_cache_sync(
struct dvb_frontend *fe,
1103 dev_dbg(fe->
dvb->device,
"%s: Preparing QPSK req\n", __func__);
1108 dev_dbg(fe->
dvb->device,
"%s: Preparing QAM req\n", __func__);
1114 dev_dbg(fe->
dvb->device,
"%s: Preparing OFDM req\n", __func__);
1116 switch (p->
u.
ofdm.bandwidth) {
1147 dev_dbg(fe->
dvb->device,
"%s: Preparing ATSC req\n", __func__);
1158 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1169 static int dtv_property_legacy_params_sync(
struct dvb_frontend *fe,
1180 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
1184 dev_dbg(fe->
dvb->device,
"%s: Preparing QPSK req\n", __func__);
1189 dev_dbg(fe->
dvb->device,
"%s: Preparing QAM req\n", __func__);
1195 dev_dbg(fe->
dvb->device,
"%s: Preparing OFDM req\n", __func__);
1227 dev_dbg(fe->
dvb->device,
"%s: Preparing VSB req\n", __func__);
1249 if (fe->
ops.get_frontend) {
1250 r = fe->
ops.get_frontend(fe);
1254 dtv_property_legacy_params_sync(fe, p_out);
1262 static int dvb_frontend_ioctl_legacy(
struct file *
file,
1263 unsigned int cmd,
void *parg);
1264 static int dvb_frontend_ioctl_properties(
struct file *
file,
1265 unsigned int cmd,
void *parg);
1267 static int dtv_property_process_get(
struct dvb_frontend *fe,
1278 tvp->
u.
buffer.data[ncaps] = fe->
ops.delsys[ncaps];
1456 if (fe->
ops.get_property) {
1457 r = fe->
ops.get_property(fe, tvp);
1462 dtv_property_dump(fe, tvp);
1469 static bool is_dvbv3_delsys(
u32 delsys)
1479 static int set_delivery_system(
struct dvb_frontend *fe,
u32 desired_system)
1508 "%s: Using delivery system to %d\n",
1527 dev_dbg(fe->
dvb->device,
"%s: This frontend doesn't support DVBv3 calls\n",
1543 if (fe->
ops.delsys[ncaps] == desired_system) {
1544 delsys = desired_system;
1550 dev_dbg(fe->
dvb->device,
"%s: Couldn't find a delivery system that matches %d\n",
1551 __func__, desired_system);
1562 if (fe->
ops.delsys[ncaps] == desired_system) {
1565 "%s: Changing delivery system to %d\n",
1566 __func__, desired_system);
1571 type = dvbv3_type(desired_system);
1579 if (!is_dvbv3_delsys(desired_system)) {
1581 "%s: can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
1592 if ((dvbv3_type(fe->
ops.delsys[ncaps]) == type) &&
1593 !is_dvbv3_delsys(fe->
ops.delsys[ncaps]))
1594 delsys = fe->
ops.delsys[ncaps];
1600 "%s: Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
1618 "%s: Using delivery system %d emulated as if it were a %d\n",
1619 __func__, delsys, desired_system);
1628 "%s: Using defaults for SYS_ISDBT\n",
1640 for (i = 0; i < 3; i++) {
1643 c->
layer[
i].interleaving = 0;
1644 c->
layer[
i].segment_count = 0;
1648 dev_dbg(fe->
dvb->device,
"%s: change delivery system on cache to %d\n",
1654 static int dtv_property_process_set(
struct dvb_frontend *fe,
1662 if (fe->
ops.set_property) {
1663 r = fe->
ops.set_property(fe, tvp);
1674 dvb_frontend_clear_cache(fe);
1682 dev_dbg(fe->
dvb->device,
"%s: Finalised property cache\n",
1685 r = dtv_set_frontend(fe);
1712 r = set_delivery_system(fe, tvp->
u.
data);
1815 if (fe->
ops.set_lna)
1816 r = fe->
ops.set_lna(fe);
1826 static int dvb_frontend_ioctl(
struct file *file,
1827 unsigned int cmd,
void *parg)
1848 err = dvb_frontend_ioctl_properties(file, cmd, parg);
1851 err = dvb_frontend_ioctl_legacy(file, cmd, parg);
1858 static int dvb_frontend_ioctl_properties(
struct file *file,
1859 unsigned int cmd,
void *parg)
1876 dev_dbg(fe->
dvb->device,
"%s: properties.num = %d\n", __func__, tvps->
num);
1877 dev_dbg(fe->
dvb->device,
"%s: properties.props = %p\n", __func__, tvps->
props);
1895 for (i = 0; i < tvps->
num; i++) {
1896 err = dtv_property_process_set(fe, tvp + i, file);
1903 dev_dbg(fe->
dvb->device,
"%s: Property cache is full, tuning\n", __func__);
1909 dev_dbg(fe->
dvb->device,
"%s: properties.num = %d\n", __func__, tvps->
num);
1910 dev_dbg(fe->
dvb->device,
"%s: properties.props = %p\n", __func__, tvps->
props);
1934 err = dtv_get_frontend(fe,
NULL);
1938 for (i = 0; i < tvps->
num; i++) {
1939 err = dtv_property_process_get(fe, c, tvp + i, file);
1965 if (dvb_frontend_check_parameters(fe) < 0)
2012 if (dvb_force_auto_inversion)
2024 if (fe->
ops.get_tune_settings && (fe->
ops.get_tune_settings(fe, &fetunesettings) == 0)) {
2025 fepriv->
min_delay = (fetunesettings.min_delay_ms *
HZ) / 1000;
2026 fepriv->
max_drift = fetunesettings.max_drift;
2027 fepriv->
step_size = fetunesettings.step_size;
2046 fepriv->
step_size = fe->
ops.info.frequency_stepsize * 2;
2047 fepriv->
max_drift = (fe->
ops.info.frequency_stepsize * 2) + 1;
2060 if (dvb_override_tune_delay > 0)
2061 fepriv->
min_delay = (dvb_override_tune_delay *
HZ) / 1000;
2068 dvb_frontend_clear_events(fe);
2069 dvb_frontend_add_event(fe, 0);
2070 dvb_frontend_wakeup(fe);
2077 static int dvb_frontend_ioctl_legacy(
struct file *file,
2078 unsigned int cmd,
void *parg)
2119 "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
2123 dev_dbg(fe->
dvb->device,
"%s: current delivery system on cache: %d, V3 type: %d\n",
2145 if (fe->
ops.read_status)
2146 err = fe->
ops.read_status(fe, status);
2151 if (fe->
ops.read_ber) {
2153 err = fe->
ops.read_ber(fe, (
__u32 *) parg);
2160 if (fe->
ops.read_signal_strength) {
2162 err = fe->
ops.read_signal_strength(fe, (
__u16 *) parg);
2169 if (fe->
ops.read_snr) {
2171 err = fe->
ops.read_snr(fe, (
__u16 *) parg);
2178 if (fe->
ops.read_ucblocks) {
2180 err = fe->
ops.read_ucblocks(fe, (
__u32 *) parg);
2187 if (fe->
ops.diseqc_reset_overload) {
2188 err = fe->
ops.diseqc_reset_overload(fe);
2195 if (fe->
ops.diseqc_send_master_cmd) {
2203 if (fe->
ops.diseqc_send_burst) {
2211 if (fe->
ops.set_tone) {
2220 if (fe->
ops.set_voltage) {
2229 if (fe->
ops.dishnetwork_send_legacy_command) {
2230 err = fe->
ops.dishnetwork_send_legacy_command(fe, (
unsigned long) parg);
2233 }
else if (fe->
ops.set_voltage) {
2250 unsigned long swcmd = ((
unsigned long) parg) << 1;
2255 if (dvb_frontend_debug)
2256 printk(
"%s switch command: 0x%04lx\n", __func__, swcmd);
2258 if (dvb_frontend_debug)
2266 for (i = 0; i < 9; i++) {
2267 if (dvb_frontend_debug)
2269 if ((swcmd & 0x01) != last) {
2272 last = (last) ? 0 : 1;
2278 if (dvb_frontend_debug) {
2279 printk(
"%s(%d): switch delay (should be 32k followed by all 8k\n",
2280 __func__, fe->
dvb->num);
2281 for (i = 1; i < 10; i++)
2291 if (fe->
ops.diseqc_recv_slave_reply)
2296 if (fe->
ops.enable_high_lnb_voltage)
2297 err = fe->
ops.enable_high_lnb_voltage(fe, (
long) parg);
2305 err = dtv_property_cache_sync(fe, c, parg);
2308 err = dtv_set_frontend(fe);
2311 err = dvb_frontend_get_event (fe, parg, file->
f_flags);
2315 err = dtv_get_frontend(fe, parg);
2336 poll_wait (file, &fepriv->
events.wait_queue, wait);
2344 static int dvb_frontend_open(
struct inode *
inode,
struct file *file)
2366 *mfe = mfedev->
priv;
2369 int mferetry = (dvb_mfe_wait_time << 1);
2372 while (mferetry-- && (mfedev->
users != -1 ||
2385 if (mfedev->
users != -1 ||
2395 if (dvbdev->
users == -1 && fe->
ops.ts_bus_ctrl) {
2396 if ((ret = fe->
ops.ts_bus_ctrl(fe, 1)) < 0)
2417 ret = dvb_frontend_start (fe);
2432 if (dvbdev->
users == -1 && fe->
ops.ts_bus_ctrl)
2433 fe->
ops.ts_bus_ctrl(fe, 0);
2440 static int dvb_frontend_release(
struct inode *inode,
struct file *file)
2456 if (dvbdev->
users == -1) {
2463 if (fe->
ops.ts_bus_ctrl)
2464 fe->
ops.ts_bus_ctrl(fe, 0);
2473 .poll = dvb_frontend_poll,
2474 .open = dvb_frontend_open,
2475 .release = dvb_frontend_release,
2483 dev_dbg(fe->
dvb->device,
"%s: adap=%d fe=%d\n", __func__, fe->
dvb->num,
2486 if (fe->
ops.tuner_ops.sleep)
2487 ret = fe->
ops.tuner_ops.sleep(fe);
2490 ret = fe->
ops.sleep(fe);
2501 dev_dbg(fe->
dvb->device,
"%s: adap=%d fe=%d\n", __func__, fe->
dvb->num,
2505 ret = fe->
ops.init(fe);
2507 if (fe->
ops.tuner_ops.init)
2508 ret = fe->
ops.tuner_ops.init(fe);
2511 dvb_frontend_wakeup(fe);
2521 static const struct dvb_device dvbdev_template = {
2525 .
fops = &dvb_frontend_fops,
2541 sema_init(&fepriv->
sem, 1);
2549 "DVB: registering adapter %i frontend %i (%s)...\n",
2550 fe->
dvb->num, fe->
id, fe->
ops.info.name);
2561 dvb_frontend_clear_cache(fe);
2574 dvb_frontend_stop (fe);
2577 if (fepriv->
dvbdev->users < -1)
2579 fepriv->
dvbdev->users==-1);
2591 #ifdef CONFIG_MEDIA_ATTACH
2596 if (fe->
ops.release_sec) {
2597 fe->
ops.release_sec(fe);
2600 if (fe->
ops.tuner_ops.release) {
2601 fe->
ops.tuner_ops.release(fe);
2604 if (fe->
ops.analog_ops.release) {
2605 fe->
ops.analog_ops.release(fe);
2608 ptr = (
void*)fe->
ops.release;
2610 fe->
ops.release(fe);
2617 if (fe->
ops.release_sec)
2618 fe->
ops.release_sec(fe);
2619 if (fe->
ops.tuner_ops.release)
2620 fe->
ops.tuner_ops.release(fe);
2621 if (fe->
ops.analog_ops.release)
2622 fe->
ops.analog_ops.release(fe);
2623 if (fe->
ops.release)
2624 fe->
ops.release(fe);