Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | #include <linux/string.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/sched.h> |
| 31 | #include <linux/wait.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/poll.h> |
| 34 | #include <linux/module.h> |
| 35 | #include <linux/moduleparam.h> |
| 36 | #include <linux/list.h> |
| 37 | #include <linux/suspend.h> |
Johannes Stezenbach | cc89c22 | 2005-06-23 22:02:39 -0700 | [diff] [blame] | 38 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/processor.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include "dvb_frontend.h" |
| 42 | #include "dvbdev.h" |
| 43 | |
| 44 | static int dvb_frontend_debug; |
| 45 | static int dvb_shutdown_timeout = 5; |
| 46 | static int dvb_force_auto_inversion; |
| 47 | static int dvb_override_tune_delay; |
| 48 | static int dvb_powerdown_on_sleep = 1; |
| 49 | |
| 50 | module_param_named(frontend_debug, dvb_frontend_debug, int, 0644); |
Johannes Stezenbach | f4f009a | 2005-05-16 21:54:21 -0700 | [diff] [blame] | 51 | MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off)."); |
Manu Abraham | 6baad3f | 2006-02-27 00:09:32 -0300 | [diff] [blame] | 52 | module_param(dvb_shutdown_timeout, int, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware"); |
Manu Abraham | 6baad3f | 2006-02-27 00:09:32 -0300 | [diff] [blame] | 54 | module_param(dvb_force_auto_inversion, int, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always"); |
Manu Abraham | 6baad3f | 2006-02-27 00:09:32 -0300 | [diff] [blame] | 56 | module_param(dvb_override_tune_delay, int, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt"); |
Manu Abraham | 6baad3f | 2006-02-27 00:09:32 -0300 | [diff] [blame] | 58 | module_param(dvb_powerdown_on_sleep, int, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB volatage off on sleep (default)"); |
| 60 | |
| 61 | #define dprintk if (dvb_frontend_debug) printk |
| 62 | |
| 63 | #define FESTATE_IDLE 1 |
| 64 | #define FESTATE_RETUNE 2 |
| 65 | #define FESTATE_TUNING_FAST 4 |
| 66 | #define FESTATE_TUNING_SLOW 8 |
| 67 | #define FESTATE_TUNED 16 |
| 68 | #define FESTATE_ZIGZAG_FAST 32 |
| 69 | #define FESTATE_ZIGZAG_SLOW 64 |
| 70 | #define FESTATE_DISEQC 128 |
| 71 | #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC) |
| 72 | #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST) |
| 73 | #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW) |
| 74 | #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW) |
| 75 | /* |
| 76 | * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling. |
| 77 | * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune. |
| 78 | * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress. |
| 79 | * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower. |
| 80 | * FESTATE_TUNED. The frontend has successfully locked on. |
| 81 | * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it. |
| 82 | * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower. |
| 83 | * FESTATE_DISEQC. A DISEQC command has just been issued. |
| 84 | * FESTATE_WAITFORLOCK. When we're waiting for a lock. |
| 85 | * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan. |
| 86 | * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan. |
| 87 | * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again. |
| 88 | */ |
| 89 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 90 | static DEFINE_MUTEX(frontend_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | struct dvb_frontend_private { |
| 93 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 94 | /* thread/frontend values */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | struct dvb_device *dvbdev; |
| 96 | struct dvb_frontend_parameters parameters; |
| 97 | struct dvb_fe_events events; |
| 98 | struct semaphore sem; |
| 99 | struct list_head list_head; |
| 100 | wait_queue_head_t wait_queue; |
| 101 | pid_t thread_pid; |
| 102 | unsigned long release_jiffies; |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 103 | unsigned int exit; |
| 104 | unsigned int wakeup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | fe_status_t status; |
Peter Beutner | 400b708 | 2006-01-09 15:32:43 -0200 | [diff] [blame] | 106 | unsigned long tune_mode_flags; |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 107 | unsigned int delay; |
| 108 | |
| 109 | /* swzigzag values */ |
| 110 | unsigned int state; |
| 111 | unsigned int bending; |
| 112 | int lnb_drift; |
| 113 | unsigned int inversion; |
| 114 | unsigned int auto_step; |
| 115 | unsigned int auto_sub_step; |
| 116 | unsigned int started_auto_step; |
| 117 | unsigned int min_delay; |
| 118 | unsigned int max_drift; |
| 119 | unsigned int step_size; |
| 120 | int quality; |
| 121 | unsigned int check_wrapped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | |
| 125 | static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status) |
| 126 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 127 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | struct dvb_fe_events *events = &fepriv->events; |
| 129 | struct dvb_frontend_event *e; |
| 130 | int wp; |
| 131 | |
| 132 | dprintk ("%s\n", __FUNCTION__); |
| 133 | |
| 134 | if (down_interruptible (&events->sem)) |
| 135 | return; |
| 136 | |
| 137 | wp = (events->eventw + 1) % MAX_EVENT; |
| 138 | |
| 139 | if (wp == events->eventr) { |
| 140 | events->overflow = 1; |
| 141 | events->eventr = (events->eventr + 1) % MAX_EVENT; |
| 142 | } |
| 143 | |
| 144 | e = &events->events[events->eventw]; |
| 145 | |
| 146 | memcpy (&e->parameters, &fepriv->parameters, |
| 147 | sizeof (struct dvb_frontend_parameters)); |
| 148 | |
| 149 | if (status & FE_HAS_LOCK) |
| 150 | if (fe->ops->get_frontend) |
| 151 | fe->ops->get_frontend(fe, &e->parameters); |
| 152 | |
| 153 | events->eventw = wp; |
| 154 | |
| 155 | up (&events->sem); |
| 156 | |
| 157 | e->status = status; |
| 158 | |
| 159 | wake_up_interruptible (&events->wait_queue); |
| 160 | } |
| 161 | |
| 162 | static int dvb_frontend_get_event(struct dvb_frontend *fe, |
| 163 | struct dvb_frontend_event *event, int flags) |
| 164 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 165 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | struct dvb_fe_events *events = &fepriv->events; |
| 167 | |
| 168 | dprintk ("%s\n", __FUNCTION__); |
| 169 | |
| 170 | if (events->overflow) { |
| 171 | events->overflow = 0; |
| 172 | return -EOVERFLOW; |
| 173 | } |
| 174 | |
| 175 | if (events->eventw == events->eventr) { |
| 176 | int ret; |
| 177 | |
| 178 | if (flags & O_NONBLOCK) |
| 179 | return -EWOULDBLOCK; |
| 180 | |
| 181 | up(&fepriv->sem); |
| 182 | |
| 183 | ret = wait_event_interruptible (events->wait_queue, |
| 184 | events->eventw != events->eventr); |
| 185 | |
| 186 | if (down_interruptible (&fepriv->sem)) |
| 187 | return -ERESTARTSYS; |
| 188 | |
| 189 | if (ret < 0) |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | if (down_interruptible (&events->sem)) |
| 194 | return -ERESTARTSYS; |
| 195 | |
| 196 | memcpy (event, &events->events[events->eventr], |
| 197 | sizeof(struct dvb_frontend_event)); |
| 198 | |
| 199 | events->eventr = (events->eventr + 1) % MAX_EVENT; |
| 200 | |
| 201 | up (&events->sem); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | static void dvb_frontend_init(struct dvb_frontend *fe) |
| 207 | { |
| 208 | dprintk ("DVB: initialising frontend %i (%s)...\n", |
| 209 | fe->dvb->num, |
| 210 | fe->ops->info.name); |
| 211 | |
| 212 | if (fe->ops->init) |
| 213 | fe->ops->init(fe); |
| 214 | } |
| 215 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 216 | static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 218 | int q2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 220 | dprintk ("%s\n", __FUNCTION__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 222 | if (locked) |
| 223 | (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256; |
| 224 | else |
| 225 | (fepriv->quality) = (fepriv->quality * 220 + 0) / 256; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 227 | q2 = fepriv->quality - 128; |
| 228 | q2 *= q2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 230 | fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Performs automatic twiddling of frontend parameters. |
| 235 | * |
| 236 | * @param fe The frontend concerned. |
| 237 | * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT |
| 238 | * @returns Number of complete iterations that have been performed. |
| 239 | */ |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 240 | static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | int autoinversion; |
| 243 | int ready = 0; |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 244 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | int original_inversion = fepriv->parameters.inversion; |
| 246 | u32 original_frequency = fepriv->parameters.frequency; |
| 247 | |
| 248 | /* are we using autoinversion? */ |
| 249 | autoinversion = ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) && |
| 250 | (fepriv->parameters.inversion == INVERSION_AUTO)); |
| 251 | |
| 252 | /* setup parameters correctly */ |
| 253 | while(!ready) { |
| 254 | /* calculate the lnb_drift */ |
| 255 | fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size; |
| 256 | |
| 257 | /* wrap the auto_step if we've exceeded the maximum drift */ |
| 258 | if (fepriv->lnb_drift > fepriv->max_drift) { |
| 259 | fepriv->auto_step = 0; |
| 260 | fepriv->auto_sub_step = 0; |
| 261 | fepriv->lnb_drift = 0; |
| 262 | } |
| 263 | |
| 264 | /* perform inversion and +/- zigzag */ |
| 265 | switch(fepriv->auto_sub_step) { |
| 266 | case 0: |
| 267 | /* try with the current inversion and current drift setting */ |
| 268 | ready = 1; |
| 269 | break; |
| 270 | |
| 271 | case 1: |
| 272 | if (!autoinversion) break; |
| 273 | |
| 274 | fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF; |
| 275 | ready = 1; |
| 276 | break; |
| 277 | |
| 278 | case 2: |
| 279 | if (fepriv->lnb_drift == 0) break; |
| 280 | |
| 281 | fepriv->lnb_drift = -fepriv->lnb_drift; |
| 282 | ready = 1; |
| 283 | break; |
| 284 | |
| 285 | case 3: |
| 286 | if (fepriv->lnb_drift == 0) break; |
| 287 | if (!autoinversion) break; |
| 288 | |
| 289 | fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF; |
| 290 | fepriv->lnb_drift = -fepriv->lnb_drift; |
| 291 | ready = 1; |
| 292 | break; |
| 293 | |
| 294 | default: |
| 295 | fepriv->auto_step++; |
| 296 | fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */ |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | if (!ready) fepriv->auto_sub_step++; |
| 301 | } |
| 302 | |
| 303 | /* if this attempt would hit where we started, indicate a complete |
| 304 | * iteration has occurred */ |
| 305 | if ((fepriv->auto_step == fepriv->started_auto_step) && |
| 306 | (fepriv->auto_sub_step == 0) && check_wrapped) { |
| 307 | return 1; |
| 308 | } |
| 309 | |
| 310 | dprintk("%s: drift:%i inversion:%i auto_step:%i " |
| 311 | "auto_sub_step:%i started_auto_step:%i\n", |
| 312 | __FUNCTION__, fepriv->lnb_drift, fepriv->inversion, |
| 313 | fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step); |
| 314 | |
| 315 | /* set the frontend itself */ |
| 316 | fepriv->parameters.frequency += fepriv->lnb_drift; |
| 317 | if (autoinversion) |
| 318 | fepriv->parameters.inversion = fepriv->inversion; |
| 319 | if (fe->ops->set_frontend) |
| 320 | fe->ops->set_frontend(fe, &fepriv->parameters); |
| 321 | |
| 322 | fepriv->parameters.frequency = original_frequency; |
| 323 | fepriv->parameters.inversion = original_inversion; |
| 324 | |
| 325 | fepriv->auto_sub_step++; |
| 326 | return 0; |
| 327 | } |
| 328 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 329 | static void dvb_frontend_swzigzag(struct dvb_frontend *fe) |
| 330 | { |
| 331 | fe_status_t s; |
| 332 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
| 333 | |
| 334 | /* if we've got no parameters, just keep idling */ |
| 335 | if (fepriv->state & FESTATE_IDLE) { |
| 336 | fepriv->delay = 3*HZ; |
| 337 | fepriv->quality = 0; |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | /* in SCAN mode, we just set the frontend when asked and leave it alone */ |
| 342 | if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) { |
| 343 | if (fepriv->state & FESTATE_RETUNE) { |
| 344 | if (fe->ops->set_frontend) |
| 345 | fe->ops->set_frontend(fe, &fepriv->parameters); |
| 346 | fepriv->state = FESTATE_TUNED; |
| 347 | } |
| 348 | fepriv->delay = 3*HZ; |
| 349 | fepriv->quality = 0; |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | /* get the frontend status */ |
| 354 | if (fepriv->state & FESTATE_RETUNE) { |
| 355 | s = 0; |
| 356 | } else { |
| 357 | if (fe->ops->read_status) |
| 358 | fe->ops->read_status(fe, &s); |
| 359 | if (s != fepriv->status) { |
| 360 | dvb_frontend_add_event(fe, s); |
| 361 | fepriv->status = s; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /* if we're not tuned, and we have a lock, move to the TUNED state */ |
| 366 | if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) { |
| 367 | dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); |
| 368 | fepriv->state = FESTATE_TUNED; |
| 369 | |
| 370 | /* if we're tuned, then we have determined the correct inversion */ |
| 371 | if ((!(fe->ops->info.caps & FE_CAN_INVERSION_AUTO)) && |
| 372 | (fepriv->parameters.inversion == INVERSION_AUTO)) { |
| 373 | fepriv->parameters.inversion = fepriv->inversion; |
| 374 | } |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | /* if we are tuned already, check we're still locked */ |
| 379 | if (fepriv->state & FESTATE_TUNED) { |
| 380 | dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); |
| 381 | |
| 382 | /* we're tuned, and the lock is still good... */ |
| 383 | if (s & FE_HAS_LOCK) { |
| 384 | return; |
| 385 | } else { /* if we _WERE_ tuned, but now don't have a lock */ |
| 386 | fepriv->state = FESTATE_ZIGZAG_FAST; |
| 387 | fepriv->started_auto_step = fepriv->auto_step; |
| 388 | fepriv->check_wrapped = 0; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /* don't actually do anything if we're in the LOSTLOCK state, |
| 393 | * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */ |
| 394 | if ((fepriv->state & FESTATE_LOSTLOCK) && |
| 395 | (fe->ops->info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) { |
| 396 | dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | /* don't do anything if we're in the DISEQC state, since this |
| 401 | * might be someone with a motorized dish controlled by DISEQC. |
| 402 | * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */ |
| 403 | if (fepriv->state & FESTATE_DISEQC) { |
| 404 | dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | /* if we're in the RETUNE state, set everything up for a brand |
| 409 | * new scan, keeping the current inversion setting, as the next |
| 410 | * tune is _very_ likely to require the same */ |
| 411 | if (fepriv->state & FESTATE_RETUNE) { |
| 412 | fepriv->lnb_drift = 0; |
| 413 | fepriv->auto_step = 0; |
| 414 | fepriv->auto_sub_step = 0; |
| 415 | fepriv->started_auto_step = 0; |
| 416 | fepriv->check_wrapped = 0; |
| 417 | } |
| 418 | |
| 419 | /* fast zigzag. */ |
| 420 | if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) { |
| 421 | fepriv->delay = fepriv->min_delay; |
| 422 | |
| 423 | /* peform a tune */ |
| 424 | if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) { |
| 425 | /* OK, if we've run out of trials at the fast speed. |
| 426 | * Drop back to slow for the _next_ attempt */ |
| 427 | fepriv->state = FESTATE_SEARCHING_SLOW; |
| 428 | fepriv->started_auto_step = fepriv->auto_step; |
| 429 | return; |
| 430 | } |
| 431 | fepriv->check_wrapped = 1; |
| 432 | |
| 433 | /* if we've just retuned, enter the ZIGZAG_FAST state. |
| 434 | * This ensures we cannot return from an |
| 435 | * FE_SET_FRONTEND ioctl before the first frontend tune |
| 436 | * occurs */ |
| 437 | if (fepriv->state & FESTATE_RETUNE) { |
| 438 | fepriv->state = FESTATE_TUNING_FAST; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | /* slow zigzag */ |
| 443 | if (fepriv->state & FESTATE_SEARCHING_SLOW) { |
| 444 | dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK); |
| 445 | |
| 446 | /* Note: don't bother checking for wrapping; we stay in this |
| 447 | * state until we get a lock */ |
| 448 | dvb_frontend_swzigzag_autotune(fe, 0); |
| 449 | } |
| 450 | } |
| 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | static int dvb_frontend_is_exiting(struct dvb_frontend *fe) |
| 453 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 454 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
| 456 | if (fepriv->exit) |
| 457 | return 1; |
| 458 | |
| 459 | if (fepriv->dvbdev->writers == 1) |
Johannes Stezenbach | cc89c22 | 2005-06-23 22:02:39 -0700 | [diff] [blame] | 460 | if (time_after(jiffies, fepriv->release_jiffies + |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 461 | dvb_shutdown_timeout * HZ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | return 1; |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | static int dvb_frontend_should_wakeup(struct dvb_frontend *fe) |
| 468 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 469 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
| 471 | if (fepriv->wakeup) { |
| 472 | fepriv->wakeup = 0; |
| 473 | return 1; |
| 474 | } |
| 475 | return dvb_frontend_is_exiting(fe); |
| 476 | } |
| 477 | |
| 478 | static void dvb_frontend_wakeup(struct dvb_frontend *fe) |
| 479 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 480 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
| 482 | fepriv->wakeup = 1; |
| 483 | wake_up_interruptible(&fepriv->wait_queue); |
| 484 | } |
| 485 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | static int dvb_frontend_thread(void *data) |
| 487 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 488 | struct dvb_frontend *fe = data; |
| 489 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | unsigned long timeout; |
| 491 | char name [15]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | fe_status_t s; |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 493 | struct dvb_frontend_parameters *params; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | |
| 495 | dprintk("%s\n", __FUNCTION__); |
| 496 | |
| 497 | snprintf (name, sizeof(name), "kdvb-fe-%i", fe->dvb->num); |
| 498 | |
Mauro Carvalho Chehab | afd1a0c | 2005-12-12 00:37:27 -0800 | [diff] [blame] | 499 | lock_kernel(); |
| 500 | daemonize(name); |
| 501 | sigfillset(¤t->blocked); |
| 502 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 504 | fepriv->check_wrapped = 0; |
| 505 | fepriv->quality = 0; |
| 506 | fepriv->delay = 3*HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | fepriv->status = 0; |
| 508 | dvb_frontend_init(fe); |
| 509 | fepriv->wakeup = 0; |
| 510 | |
| 511 | while (1) { |
| 512 | up(&fepriv->sem); /* is locked when we enter the thread... */ |
| 513 | |
| 514 | timeout = wait_event_interruptible_timeout(fepriv->wait_queue, |
| 515 | dvb_frontend_should_wakeup(fe), |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 516 | fepriv->delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | if (0 != dvb_frontend_is_exiting(fe)) { |
| 518 | /* got signal or quitting */ |
| 519 | break; |
| 520 | } |
| 521 | |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 522 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | if (down_interruptible(&fepriv->sem)) |
| 525 | break; |
| 526 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 527 | /* do an iteration of the tuning loop */ |
| 528 | if (fe->ops->tune) { |
| 529 | /* have we been asked to retune? */ |
| 530 | params = NULL; |
| 531 | if (fepriv->state & FESTATE_RETUNE) { |
| 532 | params = &fepriv->parameters; |
| 533 | fepriv->state = FESTATE_TUNED; |
| 534 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 536 | fe->ops->tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | if (s != fepriv->status) { |
| 538 | dvb_frontend_add_event(fe, s); |
| 539 | fepriv->status = s; |
| 540 | } |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 541 | } else { |
| 542 | dvb_frontend_swzigzag(fe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | if (dvb_shutdown_timeout) { |
| 547 | if (dvb_powerdown_on_sleep) |
| 548 | if (fe->ops->set_voltage) |
| 549 | fe->ops->set_voltage(fe, SEC_VOLTAGE_OFF); |
| 550 | if (fe->ops->sleep) |
| 551 | fe->ops->sleep(fe); |
| 552 | } |
| 553 | |
| 554 | fepriv->thread_pid = 0; |
| 555 | mb(); |
| 556 | |
| 557 | dvb_frontend_wakeup(fe); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static void dvb_frontend_stop(struct dvb_frontend *fe) |
| 562 | { |
| 563 | unsigned long ret; |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 564 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | |
| 566 | dprintk ("%s\n", __FUNCTION__); |
| 567 | |
| 568 | fepriv->exit = 1; |
| 569 | mb(); |
| 570 | |
| 571 | if (!fepriv->thread_pid) |
| 572 | return; |
| 573 | |
| 574 | /* check if the thread is really alive */ |
| 575 | if (kill_proc(fepriv->thread_pid, 0, 1) == -ESRCH) { |
| 576 | printk("dvb_frontend_stop: thread PID %d already died\n", |
| 577 | fepriv->thread_pid); |
| 578 | /* make sure the mutex was not held by the thread */ |
| 579 | init_MUTEX (&fepriv->sem); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | /* wake up the frontend thread, so it notices that fe->exit == 1 */ |
| 584 | dvb_frontend_wakeup(fe); |
| 585 | |
| 586 | /* wait until the frontend thread has exited */ |
| 587 | ret = wait_event_interruptible(fepriv->wait_queue,0 == fepriv->thread_pid); |
| 588 | if (-ERESTARTSYS != ret) { |
| 589 | fepriv->state = FESTATE_IDLE; |
| 590 | return; |
| 591 | } |
| 592 | fepriv->state = FESTATE_IDLE; |
| 593 | |
| 594 | /* paranoia check in case a signal arrived */ |
| 595 | if (fepriv->thread_pid) |
| 596 | printk("dvb_frontend_stop: warning: thread PID %d won't exit\n", |
| 597 | fepriv->thread_pid); |
| 598 | } |
| 599 | |
NooneImportant | 83b75b0 | 2005-11-08 21:35:27 -0800 | [diff] [blame] | 600 | s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime) |
| 601 | { |
| 602 | return ((curtime.tv_usec < lasttime.tv_usec) ? |
| 603 | 1000000 - lasttime.tv_usec + curtime.tv_usec : |
| 604 | curtime.tv_usec - lasttime.tv_usec); |
| 605 | } |
| 606 | EXPORT_SYMBOL(timeval_usec_diff); |
| 607 | |
| 608 | static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec) |
| 609 | { |
| 610 | curtime->tv_usec += add_usec; |
| 611 | if (curtime->tv_usec >= 1000000) { |
| 612 | curtime->tv_usec -= 1000000; |
| 613 | curtime->tv_sec++; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Sleep until gettimeofday() > waketime + add_usec |
| 619 | * This needs to be as precise as possible, but as the delay is |
| 620 | * usually between 2ms and 32ms, it is done using a scheduled msleep |
| 621 | * followed by usleep (normally a busy-wait loop) for the remainder |
| 622 | */ |
| 623 | void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec) |
| 624 | { |
| 625 | struct timeval lasttime; |
| 626 | s32 delta, newdelta; |
| 627 | |
| 628 | timeval_usec_add(waketime, add_usec); |
| 629 | |
| 630 | do_gettimeofday(&lasttime); |
| 631 | delta = timeval_usec_diff(lasttime, *waketime); |
| 632 | if (delta > 2500) { |
| 633 | msleep((delta - 1500) / 1000); |
| 634 | do_gettimeofday(&lasttime); |
| 635 | newdelta = timeval_usec_diff(lasttime, *waketime); |
| 636 | delta = (newdelta > delta) ? 0 : newdelta; |
| 637 | } |
| 638 | if (delta > 0) |
| 639 | udelay(delta); |
| 640 | } |
| 641 | EXPORT_SYMBOL(dvb_frontend_sleep_until); |
| 642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | static int dvb_frontend_start(struct dvb_frontend *fe) |
| 644 | { |
| 645 | int ret; |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 646 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | dprintk ("%s\n", __FUNCTION__); |
| 649 | |
| 650 | if (fepriv->thread_pid) { |
| 651 | if (!fepriv->exit) |
| 652 | return 0; |
| 653 | else |
| 654 | dvb_frontend_stop (fe); |
| 655 | } |
| 656 | |
| 657 | if (signal_pending(current)) |
| 658 | return -EINTR; |
| 659 | if (down_interruptible (&fepriv->sem)) |
| 660 | return -EINTR; |
| 661 | |
| 662 | fepriv->state = FESTATE_IDLE; |
| 663 | fepriv->exit = 0; |
| 664 | fepriv->thread_pid = 0; |
| 665 | mb(); |
| 666 | |
| 667 | ret = kernel_thread (dvb_frontend_thread, fe, 0); |
| 668 | |
| 669 | if (ret < 0) { |
| 670 | printk("dvb_frontend_start: failed to start kernel_thread (%d)\n", ret); |
| 671 | up(&fepriv->sem); |
| 672 | return ret; |
| 673 | } |
| 674 | fepriv->thread_pid = ret; |
| 675 | |
| 676 | return 0; |
| 677 | } |
| 678 | |
| 679 | static int dvb_frontend_ioctl(struct inode *inode, struct file *file, |
| 680 | unsigned int cmd, void *parg) |
| 681 | { |
| 682 | struct dvb_device *dvbdev = file->private_data; |
| 683 | struct dvb_frontend *fe = dvbdev->priv; |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 684 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | int err = -EOPNOTSUPP; |
| 686 | |
| 687 | dprintk ("%s\n", __FUNCTION__); |
| 688 | |
| 689 | if (!fe || fepriv->exit) |
| 690 | return -ENODEV; |
| 691 | |
| 692 | if ((file->f_flags & O_ACCMODE) == O_RDONLY && |
| 693 | (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT || |
| 694 | cmd == FE_DISEQC_RECV_SLAVE_REPLY)) |
| 695 | return -EPERM; |
| 696 | |
| 697 | if (down_interruptible (&fepriv->sem)) |
| 698 | return -ERESTARTSYS; |
| 699 | |
| 700 | switch (cmd) { |
| 701 | case FE_GET_INFO: { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 702 | struct dvb_frontend_info* info = parg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | memcpy(info, &fe->ops->info, sizeof(struct dvb_frontend_info)); |
| 704 | |
| 705 | /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't |
| 706 | * do it, it is done for it. */ |
| 707 | info->caps |= FE_CAN_INVERSION_AUTO; |
| 708 | err = 0; |
| 709 | break; |
| 710 | } |
| 711 | |
Peter Beutner | 6757ccc | 2005-07-07 17:57:36 -0700 | [diff] [blame] | 712 | case FE_READ_STATUS: { |
| 713 | fe_status_t* status = parg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Peter Beutner | 6757ccc | 2005-07-07 17:57:36 -0700 | [diff] [blame] | 715 | /* if retune was requested but hasn't occured yet, prevent |
| 716 | * that user get signal state from previous tuning */ |
| 717 | if(fepriv->state == FESTATE_RETUNE) { |
| 718 | err=0; |
| 719 | *status = 0; |
| 720 | break; |
| 721 | } |
| 722 | |
| 723 | if (fe->ops->read_status) |
| 724 | err = fe->ops->read_status(fe, status); |
| 725 | break; |
| 726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | case FE_READ_BER: |
| 728 | if (fe->ops->read_ber) |
| 729 | err = fe->ops->read_ber(fe, (__u32*) parg); |
| 730 | break; |
| 731 | |
| 732 | case FE_READ_SIGNAL_STRENGTH: |
| 733 | if (fe->ops->read_signal_strength) |
| 734 | err = fe->ops->read_signal_strength(fe, (__u16*) parg); |
| 735 | break; |
| 736 | |
| 737 | case FE_READ_SNR: |
| 738 | if (fe->ops->read_snr) |
| 739 | err = fe->ops->read_snr(fe, (__u16*) parg); |
| 740 | break; |
| 741 | |
| 742 | case FE_READ_UNCORRECTED_BLOCKS: |
| 743 | if (fe->ops->read_ucblocks) |
| 744 | err = fe->ops->read_ucblocks(fe, (__u32*) parg); |
| 745 | break; |
| 746 | |
| 747 | |
| 748 | case FE_DISEQC_RESET_OVERLOAD: |
| 749 | if (fe->ops->diseqc_reset_overload) { |
| 750 | err = fe->ops->diseqc_reset_overload(fe); |
| 751 | fepriv->state = FESTATE_DISEQC; |
| 752 | fepriv->status = 0; |
| 753 | } |
| 754 | break; |
| 755 | |
| 756 | case FE_DISEQC_SEND_MASTER_CMD: |
| 757 | if (fe->ops->diseqc_send_master_cmd) { |
| 758 | err = fe->ops->diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg); |
| 759 | fepriv->state = FESTATE_DISEQC; |
| 760 | fepriv->status = 0; |
| 761 | } |
| 762 | break; |
| 763 | |
| 764 | case FE_DISEQC_SEND_BURST: |
| 765 | if (fe->ops->diseqc_send_burst) { |
| 766 | err = fe->ops->diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg); |
| 767 | fepriv->state = FESTATE_DISEQC; |
| 768 | fepriv->status = 0; |
| 769 | } |
| 770 | break; |
| 771 | |
| 772 | case FE_SET_TONE: |
| 773 | if (fe->ops->set_tone) { |
| 774 | err = fe->ops->set_tone(fe, (fe_sec_tone_mode_t) parg); |
| 775 | fepriv->state = FESTATE_DISEQC; |
| 776 | fepriv->status = 0; |
| 777 | } |
| 778 | break; |
| 779 | |
| 780 | case FE_SET_VOLTAGE: |
| 781 | if (fe->ops->set_voltage) { |
| 782 | err = fe->ops->set_voltage(fe, (fe_sec_voltage_t) parg); |
| 783 | fepriv->state = FESTATE_DISEQC; |
| 784 | fepriv->status = 0; |
| 785 | } |
| 786 | break; |
| 787 | |
| 788 | case FE_DISHNETWORK_SEND_LEGACY_CMD: |
| 789 | if (fe->ops->dishnetwork_send_legacy_command) { |
Hans Verkuil | e18828e | 2006-01-09 15:25:28 -0200 | [diff] [blame] | 790 | err = fe->ops->dishnetwork_send_legacy_command(fe, (unsigned long) parg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | fepriv->state = FESTATE_DISEQC; |
| 792 | fepriv->status = 0; |
NooneImportant | 83b75b0 | 2005-11-08 21:35:27 -0800 | [diff] [blame] | 793 | } else if (fe->ops->set_voltage) { |
| 794 | /* |
| 795 | * NOTE: This is a fallback condition. Some frontends |
| 796 | * (stv0299 for instance) take longer than 8msec to |
| 797 | * respond to a set_voltage command. Those switches |
| 798 | * need custom routines to switch properly. For all |
| 799 | * other frontends, the following shoule work ok. |
| 800 | * Dish network legacy switches (as used by Dish500) |
| 801 | * are controlled by sending 9-bit command words |
| 802 | * spaced 8msec apart. |
| 803 | * the actual command word is switch/port dependant |
| 804 | * so it is up to the userspace application to send |
| 805 | * the right command. |
| 806 | * The command must always start with a '0' after |
| 807 | * initialization, so parg is 8 bits and does not |
| 808 | * include the initialization or start bit |
| 809 | */ |
Peter Beutner | 400b708 | 2006-01-09 15:32:43 -0200 | [diff] [blame] | 810 | unsigned long cmd = ((unsigned long) parg) << 1; |
NooneImportant | 83b75b0 | 2005-11-08 21:35:27 -0800 | [diff] [blame] | 811 | struct timeval nexttime; |
| 812 | struct timeval tv[10]; |
| 813 | int i; |
| 814 | u8 last = 1; |
| 815 | if (dvb_frontend_debug) |
Peter Beutner | 400b708 | 2006-01-09 15:32:43 -0200 | [diff] [blame] | 816 | printk("%s switch command: 0x%04lx\n", __FUNCTION__, cmd); |
NooneImportant | 83b75b0 | 2005-11-08 21:35:27 -0800 | [diff] [blame] | 817 | do_gettimeofday(&nexttime); |
| 818 | if (dvb_frontend_debug) |
| 819 | memcpy(&tv[0], &nexttime, sizeof(struct timeval)); |
| 820 | /* before sending a command, initialize by sending |
| 821 | * a 32ms 18V to the switch |
| 822 | */ |
| 823 | fe->ops->set_voltage(fe, SEC_VOLTAGE_18); |
| 824 | dvb_frontend_sleep_until(&nexttime, 32000); |
| 825 | |
| 826 | for (i = 0; i < 9; i++) { |
| 827 | if (dvb_frontend_debug) |
| 828 | do_gettimeofday(&tv[i + 1]); |
| 829 | if ((cmd & 0x01) != last) { |
| 830 | /* set voltage to (last ? 13V : 18V) */ |
| 831 | fe->ops->set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18); |
| 832 | last = (last) ? 0 : 1; |
| 833 | } |
| 834 | cmd = cmd >> 1; |
| 835 | if (i != 8) |
| 836 | dvb_frontend_sleep_until(&nexttime, 8000); |
| 837 | } |
| 838 | if (dvb_frontend_debug) { |
| 839 | printk("%s(%d): switch delay (should be 32k followed by all 8k\n", |
| 840 | __FUNCTION__, fe->dvb->num); |
| 841 | for (i = 1; i < 10; i++) |
| 842 | printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i])); |
| 843 | } |
| 844 | err = 0; |
| 845 | fepriv->state = FESTATE_DISEQC; |
| 846 | fepriv->status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | } |
| 848 | break; |
| 849 | |
| 850 | case FE_DISEQC_RECV_SLAVE_REPLY: |
| 851 | if (fe->ops->diseqc_recv_slave_reply) |
| 852 | err = fe->ops->diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg); |
| 853 | break; |
| 854 | |
| 855 | case FE_ENABLE_HIGH_LNB_VOLTAGE: |
| 856 | if (fe->ops->enable_high_lnb_voltage) |
Hans Verkuil | e18828e | 2006-01-09 15:25:28 -0200 | [diff] [blame] | 857 | err = fe->ops->enable_high_lnb_voltage(fe, (long) parg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | break; |
| 859 | |
| 860 | case FE_SET_FRONTEND: { |
| 861 | struct dvb_frontend_tune_settings fetunesettings; |
| 862 | |
| 863 | memcpy (&fepriv->parameters, parg, |
| 864 | sizeof (struct dvb_frontend_parameters)); |
| 865 | |
| 866 | memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings)); |
| 867 | memcpy(&fetunesettings.parameters, parg, |
| 868 | sizeof (struct dvb_frontend_parameters)); |
| 869 | |
| 870 | /* force auto frequency inversion if requested */ |
| 871 | if (dvb_force_auto_inversion) { |
| 872 | fepriv->parameters.inversion = INVERSION_AUTO; |
| 873 | fetunesettings.parameters.inversion = INVERSION_AUTO; |
| 874 | } |
| 875 | if (fe->ops->info.type == FE_OFDM) { |
| 876 | /* without hierachical coding code_rate_LP is irrelevant, |
| 877 | * so we tolerate the otherwise invalid FEC_NONE setting */ |
| 878 | if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE && |
| 879 | fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE) |
| 880 | fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO; |
| 881 | } |
| 882 | |
| 883 | /* get frontend-specific tuning settings */ |
| 884 | if (fe->ops->get_tune_settings && (fe->ops->get_tune_settings(fe, &fetunesettings) == 0)) { |
| 885 | fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000; |
| 886 | fepriv->max_drift = fetunesettings.max_drift; |
| 887 | fepriv->step_size = fetunesettings.step_size; |
| 888 | } else { |
| 889 | /* default values */ |
| 890 | switch(fe->ops->info.type) { |
| 891 | case FE_QPSK: |
| 892 | fepriv->min_delay = HZ/20; |
| 893 | fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000; |
| 894 | fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000; |
| 895 | break; |
| 896 | |
| 897 | case FE_QAM: |
| 898 | fepriv->min_delay = HZ/20; |
| 899 | fepriv->step_size = 0; /* no zigzag */ |
| 900 | fepriv->max_drift = 0; |
| 901 | break; |
| 902 | |
| 903 | case FE_OFDM: |
| 904 | fepriv->min_delay = HZ/20; |
| 905 | fepriv->step_size = fe->ops->info.frequency_stepsize * 2; |
| 906 | fepriv->max_drift = (fe->ops->info.frequency_stepsize * 2) + 1; |
| 907 | break; |
| 908 | case FE_ATSC: |
| 909 | printk("dvb-core: FE_ATSC not handled yet.\n"); |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | if (dvb_override_tune_delay > 0) |
| 914 | fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000; |
| 915 | |
| 916 | fepriv->state = FESTATE_RETUNE; |
| 917 | dvb_frontend_wakeup(fe); |
| 918 | dvb_frontend_add_event(fe, 0); |
| 919 | fepriv->status = 0; |
| 920 | err = 0; |
| 921 | break; |
| 922 | } |
| 923 | |
| 924 | case FE_GET_EVENT: |
| 925 | err = dvb_frontend_get_event (fe, parg, file->f_flags); |
| 926 | break; |
| 927 | |
| 928 | case FE_GET_FRONTEND: |
| 929 | if (fe->ops->get_frontend) { |
| 930 | memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters)); |
| 931 | err = fe->ops->get_frontend(fe, (struct dvb_frontend_parameters*) parg); |
| 932 | } |
| 933 | break; |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 934 | |
| 935 | case FE_SET_FRONTEND_TUNE_MODE: |
Hans Verkuil | e18828e | 2006-01-09 15:25:28 -0200 | [diff] [blame] | 936 | fepriv->tune_mode_flags = (unsigned long) parg; |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 937 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | }; |
| 939 | |
| 940 | up (&fepriv->sem); |
| 941 | return err; |
| 942 | } |
| 943 | |
| 944 | static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait) |
| 945 | { |
| 946 | struct dvb_device *dvbdev = file->private_data; |
| 947 | struct dvb_frontend *fe = dvbdev->priv; |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 948 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
| 950 | dprintk ("%s\n", __FUNCTION__); |
| 951 | |
| 952 | poll_wait (file, &fepriv->events.wait_queue, wait); |
| 953 | |
| 954 | if (fepriv->events.eventw != fepriv->events.eventr) |
| 955 | return (POLLIN | POLLRDNORM | POLLPRI); |
| 956 | |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | static int dvb_frontend_open(struct inode *inode, struct file *file) |
| 961 | { |
| 962 | struct dvb_device *dvbdev = file->private_data; |
| 963 | struct dvb_frontend *fe = dvbdev->priv; |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 964 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | int ret; |
| 966 | |
| 967 | dprintk ("%s\n", __FUNCTION__); |
| 968 | |
| 969 | if ((ret = dvb_generic_open (inode, file)) < 0) |
| 970 | return ret; |
| 971 | |
| 972 | if ((file->f_flags & O_ACCMODE) != O_RDONLY) { |
| 973 | ret = dvb_frontend_start (fe); |
| 974 | if (ret) |
| 975 | dvb_generic_release (inode, file); |
| 976 | |
| 977 | /* empty event queue */ |
| 978 | fepriv->events.eventr = fepriv->events.eventw = 0; |
Andrew de Quincey | 36cb557 | 2006-01-09 15:25:07 -0200 | [diff] [blame] | 979 | |
| 980 | /* normal tune mode when opened R/W */ |
| 981 | fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | return ret; |
| 985 | } |
| 986 | |
| 987 | static int dvb_frontend_release(struct inode *inode, struct file *file) |
| 988 | { |
| 989 | struct dvb_device *dvbdev = file->private_data; |
| 990 | struct dvb_frontend *fe = dvbdev->priv; |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 991 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | |
| 993 | dprintk ("%s\n", __FUNCTION__); |
| 994 | |
| 995 | if ((file->f_flags & O_ACCMODE) != O_RDONLY) |
| 996 | fepriv->release_jiffies = jiffies; |
| 997 | |
| 998 | return dvb_generic_release (inode, file); |
| 999 | } |
| 1000 | |
| 1001 | static struct file_operations dvb_frontend_fops = { |
| 1002 | .owner = THIS_MODULE, |
| 1003 | .ioctl = dvb_generic_ioctl, |
| 1004 | .poll = dvb_frontend_poll, |
| 1005 | .open = dvb_frontend_open, |
| 1006 | .release = dvb_frontend_release |
| 1007 | }; |
| 1008 | |
| 1009 | int dvb_register_frontend(struct dvb_adapter* dvb, |
| 1010 | struct dvb_frontend* fe) |
| 1011 | { |
| 1012 | struct dvb_frontend_private *fepriv; |
| 1013 | static const struct dvb_device dvbdev_template = { |
| 1014 | .users = ~0, |
| 1015 | .writers = 1, |
| 1016 | .readers = (~0)-1, |
| 1017 | .fops = &dvb_frontend_fops, |
| 1018 | .kernel_ioctl = dvb_frontend_ioctl |
| 1019 | }; |
| 1020 | |
| 1021 | dprintk ("%s\n", __FUNCTION__); |
| 1022 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1023 | if (mutex_lock_interruptible(&frontend_mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | return -ERESTARTSYS; |
| 1025 | |
Panagiotis Issaris | 7408187 | 2006-01-11 19:40:56 -0200 | [diff] [blame] | 1026 | fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | if (fe->frontend_priv == NULL) { |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1028 | mutex_unlock(&frontend_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | return -ENOMEM; |
| 1030 | } |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 1031 | fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | |
| 1033 | init_MUTEX (&fepriv->sem); |
| 1034 | init_waitqueue_head (&fepriv->wait_queue); |
| 1035 | init_waitqueue_head (&fepriv->events.wait_queue); |
| 1036 | init_MUTEX (&fepriv->events.sem); |
| 1037 | fe->dvb = dvb; |
| 1038 | fepriv->inversion = INVERSION_OFF; |
| 1039 | |
| 1040 | printk ("DVB: registering frontend %i (%s)...\n", |
| 1041 | fe->dvb->num, |
| 1042 | fe->ops->info.name); |
| 1043 | |
| 1044 | dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template, |
| 1045 | fe, DVB_DEVICE_FRONTEND); |
| 1046 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1047 | mutex_unlock(&frontend_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | return 0; |
| 1049 | } |
| 1050 | EXPORT_SYMBOL(dvb_register_frontend); |
| 1051 | |
| 1052 | int dvb_unregister_frontend(struct dvb_frontend* fe) |
| 1053 | { |
Johannes Stezenbach | 0c53c70 | 2005-05-16 21:54:24 -0700 | [diff] [blame] | 1054 | struct dvb_frontend_private *fepriv = fe->frontend_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | dprintk ("%s\n", __FUNCTION__); |
| 1056 | |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1057 | mutex_lock(&frontend_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | dvb_unregister_device (fepriv->dvbdev); |
| 1059 | dvb_frontend_stop (fe); |
| 1060 | if (fe->ops->release) |
| 1061 | fe->ops->release(fe); |
| 1062 | else |
| 1063 | printk("dvb_frontend: Demodulator (%s) does not have a release callback!\n", fe->ops->info.name); |
| 1064 | /* fe is invalid now */ |
| 1065 | kfree(fepriv); |
Ingo Molnar | 3593cab | 2006-02-07 06:49:14 -0200 | [diff] [blame] | 1066 | mutex_unlock(&frontend_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | return 0; |
| 1068 | } |
| 1069 | EXPORT_SYMBOL(dvb_unregister_frontend); |