dvb_frontend.c 30 KB

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