dvb_frontend.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  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/list.h>
  35. #include <linux/freezer.h>
  36. #include <linux/jiffies.h>
  37. #include <linux/kthread.h>
  38. #include <asm/processor.h>
  39. #include "dvb_frontend.h"
  40. #include "dvbdev.h"
  41. #include <linux/dvb/version.h>
  42. static int dvb_frontend_debug;
  43. static int dvb_shutdown_timeout;
  44. static int dvb_force_auto_inversion;
  45. static int dvb_override_tune_delay;
  46. static int dvb_powerdown_on_sleep = 1;
  47. static int dvb_mfe_wait_time = 5;
  48. module_param_named(frontend_debug, dvb_frontend_debug, int, 0644);
  49. MODULE_PARM_DESC(frontend_debug, "Turn on/off frontend core debugging (default:off).");
  50. module_param(dvb_shutdown_timeout, int, 0644);
  51. MODULE_PARM_DESC(dvb_shutdown_timeout, "wait <shutdown_timeout> seconds after close() before suspending hardware");
  52. module_param(dvb_force_auto_inversion, int, 0644);
  53. MODULE_PARM_DESC(dvb_force_auto_inversion, "0: normal (default), 1: INVERSION_AUTO forced always");
  54. module_param(dvb_override_tune_delay, int, 0644);
  55. MODULE_PARM_DESC(dvb_override_tune_delay, "0: normal (default), >0 => delay in milliseconds to wait for lock after a tune attempt");
  56. module_param(dvb_powerdown_on_sleep, int, 0644);
  57. MODULE_PARM_DESC(dvb_powerdown_on_sleep, "0: do not power down, 1: turn LNB voltage off on sleep (default)");
  58. module_param(dvb_mfe_wait_time, int, 0644);
  59. 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)");
  60. #define dprintk if (dvb_frontend_debug) printk
  61. #define FESTATE_IDLE 1
  62. #define FESTATE_RETUNE 2
  63. #define FESTATE_TUNING_FAST 4
  64. #define FESTATE_TUNING_SLOW 8
  65. #define FESTATE_TUNED 16
  66. #define FESTATE_ZIGZAG_FAST 32
  67. #define FESTATE_ZIGZAG_SLOW 64
  68. #define FESTATE_DISEQC 128
  69. #define FESTATE_WAITFORLOCK (FESTATE_TUNING_FAST | FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW | FESTATE_DISEQC)
  70. #define FESTATE_SEARCHING_FAST (FESTATE_TUNING_FAST | FESTATE_ZIGZAG_FAST)
  71. #define FESTATE_SEARCHING_SLOW (FESTATE_TUNING_SLOW | FESTATE_ZIGZAG_SLOW)
  72. #define FESTATE_LOSTLOCK (FESTATE_ZIGZAG_FAST | FESTATE_ZIGZAG_SLOW)
  73. #define FE_ALGO_HW 1
  74. /*
  75. * FESTATE_IDLE. No tuning parameters have been supplied and the loop is idling.
  76. * FESTATE_RETUNE. Parameters have been supplied, but we have not yet performed the first tune.
  77. * FESTATE_TUNING_FAST. Tuning parameters have been supplied and fast zigzag scan is in progress.
  78. * FESTATE_TUNING_SLOW. Tuning parameters have been supplied. Fast zigzag failed, so we're trying again, but slower.
  79. * FESTATE_TUNED. The frontend has successfully locked on.
  80. * FESTATE_ZIGZAG_FAST. The lock has been lost, and a fast zigzag has been initiated to try and regain it.
  81. * FESTATE_ZIGZAG_SLOW. The lock has been lost. Fast zigzag has been failed, so we're trying again, but slower.
  82. * FESTATE_DISEQC. A DISEQC command has just been issued.
  83. * FESTATE_WAITFORLOCK. When we're waiting for a lock.
  84. * FESTATE_SEARCHING_FAST. When we're searching for a signal using a fast zigzag scan.
  85. * FESTATE_SEARCHING_SLOW. When we're searching for a signal using a slow zigzag scan.
  86. * FESTATE_LOSTLOCK. When the lock has been lost, and we're searching it again.
  87. */
  88. static DEFINE_MUTEX(frontend_mutex);
  89. struct dvb_frontend_private {
  90. /* thread/frontend values */
  91. struct dvb_device *dvbdev;
  92. struct dvb_frontend_parameters parameters;
  93. struct dvb_fe_events events;
  94. struct semaphore sem;
  95. struct list_head list_head;
  96. wait_queue_head_t wait_queue;
  97. struct task_struct *thread;
  98. unsigned long release_jiffies;
  99. unsigned int exit;
  100. unsigned int wakeup;
  101. fe_status_t status;
  102. unsigned long tune_mode_flags;
  103. unsigned int delay;
  104. unsigned int reinitialise;
  105. int tone;
  106. int voltage;
  107. /* swzigzag values */
  108. unsigned int state;
  109. unsigned int bending;
  110. int lnb_drift;
  111. unsigned int inversion;
  112. unsigned int auto_step;
  113. unsigned int auto_sub_step;
  114. unsigned int started_auto_step;
  115. unsigned int min_delay;
  116. unsigned int max_drift;
  117. unsigned int step_size;
  118. int quality;
  119. unsigned int check_wrapped;
  120. enum dvbfe_search algo_status;
  121. };
  122. static void dvb_frontend_wakeup(struct dvb_frontend *fe);
  123. static void dvb_frontend_add_event(struct dvb_frontend *fe, fe_status_t status)
  124. {
  125. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  126. struct dvb_fe_events *events = &fepriv->events;
  127. struct dvb_frontend_event *e;
  128. int wp;
  129. dprintk ("%s\n", __func__);
  130. if (mutex_lock_interruptible (&events->mtx))
  131. return;
  132. wp = (events->eventw + 1) % MAX_EVENT;
  133. if (wp == events->eventr) {
  134. events->overflow = 1;
  135. events->eventr = (events->eventr + 1) % MAX_EVENT;
  136. }
  137. e = &events->events[events->eventw];
  138. memcpy (&e->parameters, &fepriv->parameters,
  139. sizeof (struct dvb_frontend_parameters));
  140. if (status & FE_HAS_LOCK)
  141. if (fe->ops.get_frontend)
  142. fe->ops.get_frontend(fe, &e->parameters);
  143. events->eventw = wp;
  144. mutex_unlock(&events->mtx);
  145. e->status = status;
  146. wake_up_interruptible (&events->wait_queue);
  147. }
  148. static int dvb_frontend_get_event(struct dvb_frontend *fe,
  149. struct dvb_frontend_event *event, int flags)
  150. {
  151. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  152. struct dvb_fe_events *events = &fepriv->events;
  153. dprintk ("%s\n", __func__);
  154. if (events->overflow) {
  155. events->overflow = 0;
  156. return -EOVERFLOW;
  157. }
  158. if (events->eventw == events->eventr) {
  159. int ret;
  160. if (flags & O_NONBLOCK)
  161. return -EWOULDBLOCK;
  162. up(&fepriv->sem);
  163. ret = wait_event_interruptible (events->wait_queue,
  164. events->eventw != events->eventr);
  165. if (down_interruptible (&fepriv->sem))
  166. return -ERESTARTSYS;
  167. if (ret < 0)
  168. return ret;
  169. }
  170. if (mutex_lock_interruptible (&events->mtx))
  171. return -ERESTARTSYS;
  172. memcpy (event, &events->events[events->eventr],
  173. sizeof(struct dvb_frontend_event));
  174. events->eventr = (events->eventr + 1) % MAX_EVENT;
  175. mutex_unlock(&events->mtx);
  176. return 0;
  177. }
  178. static void dvb_frontend_init(struct dvb_frontend *fe)
  179. {
  180. dprintk ("DVB: initialising adapter %i frontend %i (%s)...\n",
  181. fe->dvb->num,
  182. fe->id,
  183. fe->ops.info.name);
  184. if (fe->ops.init)
  185. fe->ops.init(fe);
  186. if (fe->ops.tuner_ops.init) {
  187. if (fe->ops.i2c_gate_ctrl)
  188. fe->ops.i2c_gate_ctrl(fe, 1);
  189. fe->ops.tuner_ops.init(fe);
  190. if (fe->ops.i2c_gate_ctrl)
  191. fe->ops.i2c_gate_ctrl(fe, 0);
  192. }
  193. }
  194. void dvb_frontend_reinitialise(struct dvb_frontend *fe)
  195. {
  196. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  197. fepriv->reinitialise = 1;
  198. dvb_frontend_wakeup(fe);
  199. }
  200. EXPORT_SYMBOL(dvb_frontend_reinitialise);
  201. static void dvb_frontend_swzigzag_update_delay(struct dvb_frontend_private *fepriv, int locked)
  202. {
  203. int q2;
  204. dprintk ("%s\n", __func__);
  205. if (locked)
  206. (fepriv->quality) = (fepriv->quality * 220 + 36*256) / 256;
  207. else
  208. (fepriv->quality) = (fepriv->quality * 220 + 0) / 256;
  209. q2 = fepriv->quality - 128;
  210. q2 *= q2;
  211. fepriv->delay = fepriv->min_delay + q2 * HZ / (128*128);
  212. }
  213. /**
  214. * Performs automatic twiddling of frontend parameters.
  215. *
  216. * @param fe The frontend concerned.
  217. * @param check_wrapped Checks if an iteration has completed. DO NOT SET ON THE FIRST ATTEMPT
  218. * @returns Number of complete iterations that have been performed.
  219. */
  220. static int dvb_frontend_swzigzag_autotune(struct dvb_frontend *fe, int check_wrapped)
  221. {
  222. int autoinversion;
  223. int ready = 0;
  224. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  225. int original_inversion = fepriv->parameters.inversion;
  226. u32 original_frequency = fepriv->parameters.frequency;
  227. /* are we using autoinversion? */
  228. autoinversion = ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
  229. (fepriv->parameters.inversion == INVERSION_AUTO));
  230. /* setup parameters correctly */
  231. while(!ready) {
  232. /* calculate the lnb_drift */
  233. fepriv->lnb_drift = fepriv->auto_step * fepriv->step_size;
  234. /* wrap the auto_step if we've exceeded the maximum drift */
  235. if (fepriv->lnb_drift > fepriv->max_drift) {
  236. fepriv->auto_step = 0;
  237. fepriv->auto_sub_step = 0;
  238. fepriv->lnb_drift = 0;
  239. }
  240. /* perform inversion and +/- zigzag */
  241. switch(fepriv->auto_sub_step) {
  242. case 0:
  243. /* try with the current inversion and current drift setting */
  244. ready = 1;
  245. break;
  246. case 1:
  247. if (!autoinversion) break;
  248. fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
  249. ready = 1;
  250. break;
  251. case 2:
  252. if (fepriv->lnb_drift == 0) break;
  253. fepriv->lnb_drift = -fepriv->lnb_drift;
  254. ready = 1;
  255. break;
  256. case 3:
  257. if (fepriv->lnb_drift == 0) break;
  258. if (!autoinversion) break;
  259. fepriv->inversion = (fepriv->inversion == INVERSION_OFF) ? INVERSION_ON : INVERSION_OFF;
  260. fepriv->lnb_drift = -fepriv->lnb_drift;
  261. ready = 1;
  262. break;
  263. default:
  264. fepriv->auto_step++;
  265. fepriv->auto_sub_step = -1; /* it'll be incremented to 0 in a moment */
  266. break;
  267. }
  268. if (!ready) fepriv->auto_sub_step++;
  269. }
  270. /* if this attempt would hit where we started, indicate a complete
  271. * iteration has occurred */
  272. if ((fepriv->auto_step == fepriv->started_auto_step) &&
  273. (fepriv->auto_sub_step == 0) && check_wrapped) {
  274. return 1;
  275. }
  276. dprintk("%s: drift:%i inversion:%i auto_step:%i "
  277. "auto_sub_step:%i started_auto_step:%i\n",
  278. __func__, fepriv->lnb_drift, fepriv->inversion,
  279. fepriv->auto_step, fepriv->auto_sub_step, fepriv->started_auto_step);
  280. /* set the frontend itself */
  281. fepriv->parameters.frequency += fepriv->lnb_drift;
  282. if (autoinversion)
  283. fepriv->parameters.inversion = fepriv->inversion;
  284. if (fe->ops.set_frontend)
  285. fe->ops.set_frontend(fe, &fepriv->parameters);
  286. fepriv->parameters.frequency = original_frequency;
  287. fepriv->parameters.inversion = original_inversion;
  288. fepriv->auto_sub_step++;
  289. return 0;
  290. }
  291. static void dvb_frontend_swzigzag(struct dvb_frontend *fe)
  292. {
  293. fe_status_t s = 0;
  294. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  295. /* if we've got no parameters, just keep idling */
  296. if (fepriv->state & FESTATE_IDLE) {
  297. fepriv->delay = 3*HZ;
  298. fepriv->quality = 0;
  299. return;
  300. }
  301. /* in SCAN mode, we just set the frontend when asked and leave it alone */
  302. if (fepriv->tune_mode_flags & FE_TUNE_MODE_ONESHOT) {
  303. if (fepriv->state & FESTATE_RETUNE) {
  304. if (fe->ops.set_frontend)
  305. fe->ops.set_frontend(fe, &fepriv->parameters);
  306. fepriv->state = FESTATE_TUNED;
  307. }
  308. fepriv->delay = 3*HZ;
  309. fepriv->quality = 0;
  310. return;
  311. }
  312. /* get the frontend status */
  313. if (fepriv->state & FESTATE_RETUNE) {
  314. s = 0;
  315. } else {
  316. if (fe->ops.read_status)
  317. fe->ops.read_status(fe, &s);
  318. if (s != fepriv->status) {
  319. dvb_frontend_add_event(fe, s);
  320. fepriv->status = s;
  321. }
  322. }
  323. /* if we're not tuned, and we have a lock, move to the TUNED state */
  324. if ((fepriv->state & FESTATE_WAITFORLOCK) && (s & FE_HAS_LOCK)) {
  325. dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
  326. fepriv->state = FESTATE_TUNED;
  327. /* if we're tuned, then we have determined the correct inversion */
  328. if ((!(fe->ops.info.caps & FE_CAN_INVERSION_AUTO)) &&
  329. (fepriv->parameters.inversion == INVERSION_AUTO)) {
  330. fepriv->parameters.inversion = fepriv->inversion;
  331. }
  332. return;
  333. }
  334. /* if we are tuned already, check we're still locked */
  335. if (fepriv->state & FESTATE_TUNED) {
  336. dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
  337. /* we're tuned, and the lock is still good... */
  338. if (s & FE_HAS_LOCK) {
  339. return;
  340. } else { /* if we _WERE_ tuned, but now don't have a lock */
  341. fepriv->state = FESTATE_ZIGZAG_FAST;
  342. fepriv->started_auto_step = fepriv->auto_step;
  343. fepriv->check_wrapped = 0;
  344. }
  345. }
  346. /* don't actually do anything if we're in the LOSTLOCK state,
  347. * the frontend is set to FE_CAN_RECOVER, and the max_drift is 0 */
  348. if ((fepriv->state & FESTATE_LOSTLOCK) &&
  349. (fe->ops.info.caps & FE_CAN_RECOVER) && (fepriv->max_drift == 0)) {
  350. dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
  351. return;
  352. }
  353. /* don't do anything if we're in the DISEQC state, since this
  354. * might be someone with a motorized dish controlled by DISEQC.
  355. * If its actually a re-tune, there will be a SET_FRONTEND soon enough. */
  356. if (fepriv->state & FESTATE_DISEQC) {
  357. dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
  358. return;
  359. }
  360. /* if we're in the RETUNE state, set everything up for a brand
  361. * new scan, keeping the current inversion setting, as the next
  362. * tune is _very_ likely to require the same */
  363. if (fepriv->state & FESTATE_RETUNE) {
  364. fepriv->lnb_drift = 0;
  365. fepriv->auto_step = 0;
  366. fepriv->auto_sub_step = 0;
  367. fepriv->started_auto_step = 0;
  368. fepriv->check_wrapped = 0;
  369. }
  370. /* fast zigzag. */
  371. if ((fepriv->state & FESTATE_SEARCHING_FAST) || (fepriv->state & FESTATE_RETUNE)) {
  372. fepriv->delay = fepriv->min_delay;
  373. /* peform a tune */
  374. if (dvb_frontend_swzigzag_autotune(fe, fepriv->check_wrapped)) {
  375. /* OK, if we've run out of trials at the fast speed.
  376. * Drop back to slow for the _next_ attempt */
  377. fepriv->state = FESTATE_SEARCHING_SLOW;
  378. fepriv->started_auto_step = fepriv->auto_step;
  379. return;
  380. }
  381. fepriv->check_wrapped = 1;
  382. /* if we've just retuned, enter the ZIGZAG_FAST state.
  383. * This ensures we cannot return from an
  384. * FE_SET_FRONTEND ioctl before the first frontend tune
  385. * occurs */
  386. if (fepriv->state & FESTATE_RETUNE) {
  387. fepriv->state = FESTATE_TUNING_FAST;
  388. }
  389. }
  390. /* slow zigzag */
  391. if (fepriv->state & FESTATE_SEARCHING_SLOW) {
  392. dvb_frontend_swzigzag_update_delay(fepriv, s & FE_HAS_LOCK);
  393. /* Note: don't bother checking for wrapping; we stay in this
  394. * state until we get a lock */
  395. dvb_frontend_swzigzag_autotune(fe, 0);
  396. }
  397. }
  398. static int dvb_frontend_is_exiting(struct dvb_frontend *fe)
  399. {
  400. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  401. if (fepriv->exit)
  402. return 1;
  403. if (fepriv->dvbdev->writers == 1)
  404. if (time_after(jiffies, fepriv->release_jiffies +
  405. dvb_shutdown_timeout * HZ))
  406. return 1;
  407. return 0;
  408. }
  409. static int dvb_frontend_should_wakeup(struct dvb_frontend *fe)
  410. {
  411. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  412. if (fepriv->wakeup) {
  413. fepriv->wakeup = 0;
  414. return 1;
  415. }
  416. return dvb_frontend_is_exiting(fe);
  417. }
  418. static void dvb_frontend_wakeup(struct dvb_frontend *fe)
  419. {
  420. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  421. fepriv->wakeup = 1;
  422. wake_up_interruptible(&fepriv->wait_queue);
  423. }
  424. static int dvb_frontend_thread(void *data)
  425. {
  426. struct dvb_frontend *fe = data;
  427. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  428. unsigned long timeout;
  429. fe_status_t s;
  430. enum dvbfe_algo algo;
  431. struct dvb_frontend_parameters *params;
  432. dprintk("%s\n", __func__);
  433. fepriv->check_wrapped = 0;
  434. fepriv->quality = 0;
  435. fepriv->delay = 3*HZ;
  436. fepriv->status = 0;
  437. fepriv->wakeup = 0;
  438. fepriv->reinitialise = 0;
  439. dvb_frontend_init(fe);
  440. set_freezable();
  441. while (1) {
  442. up(&fepriv->sem); /* is locked when we enter the thread... */
  443. restart:
  444. timeout = wait_event_interruptible_timeout(fepriv->wait_queue,
  445. dvb_frontend_should_wakeup(fe) || kthread_should_stop()
  446. || freezing(current),
  447. fepriv->delay);
  448. if (kthread_should_stop() || dvb_frontend_is_exiting(fe)) {
  449. /* got signal or quitting */
  450. break;
  451. }
  452. if (try_to_freeze())
  453. goto restart;
  454. if (down_interruptible(&fepriv->sem))
  455. break;
  456. if (fepriv->reinitialise) {
  457. dvb_frontend_init(fe);
  458. if (fepriv->tone != -1) {
  459. fe->ops.set_tone(fe, fepriv->tone);
  460. }
  461. if (fepriv->voltage != -1) {
  462. fe->ops.set_voltage(fe, fepriv->voltage);
  463. }
  464. fepriv->reinitialise = 0;
  465. }
  466. /* do an iteration of the tuning loop */
  467. if (fe->ops.get_frontend_algo) {
  468. algo = fe->ops.get_frontend_algo(fe);
  469. switch (algo) {
  470. case DVBFE_ALGO_HW:
  471. dprintk("%s: Frontend ALGO = DVBFE_ALGO_HW\n", __func__);
  472. params = NULL; /* have we been asked to RETUNE ? */
  473. if (fepriv->state & FESTATE_RETUNE) {
  474. dprintk("%s: Retune requested, FESTATE_RETUNE\n", __func__);
  475. params = &fepriv->parameters;
  476. fepriv->state = FESTATE_TUNED;
  477. }
  478. if (fe->ops.tune)
  479. fe->ops.tune(fe, params, fepriv->tune_mode_flags, &fepriv->delay, &s);
  480. if (s != fepriv->status) {
  481. dprintk("%s: state changed, adding current state\n", __func__);
  482. dvb_frontend_add_event(fe, s);
  483. fepriv->status = s;
  484. }
  485. break;
  486. case DVBFE_ALGO_SW:
  487. dprintk("%s: Frontend ALGO = DVBFE_ALGO_SW\n", __func__);
  488. dvb_frontend_swzigzag(fe);
  489. break;
  490. case DVBFE_ALGO_CUSTOM:
  491. params = NULL; /* have we been asked to RETUNE ? */
  492. dprintk("%s: Frontend ALGO = DVBFE_ALGO_CUSTOM, state=%d\n", __func__, fepriv->state);
  493. if (fepriv->state & FESTATE_RETUNE) {
  494. dprintk("%s: Retune requested, FESTAT_RETUNE\n", __func__);
  495. fepriv->state = FESTATE_TUNED;
  496. }
  497. /* Case where we are going to search for a carrier
  498. * User asked us to retune again for some reason, possibly
  499. * requesting a search with a new set of parameters
  500. */
  501. if (fepriv->algo_status & DVBFE_ALGO_SEARCH_AGAIN) {
  502. if (fe->ops.search) {
  503. fepriv->algo_status = fe->ops.search(fe, &fepriv->parameters);
  504. /* We did do a search as was requested, the flags are
  505. * now unset as well and has the flags wrt to search.
  506. */
  507. } else {
  508. fepriv->algo_status &= ~DVBFE_ALGO_SEARCH_AGAIN;
  509. }
  510. }
  511. /* Track the carrier if the search was successful */
  512. if (fepriv->algo_status == DVBFE_ALGO_SEARCH_SUCCESS) {
  513. if (fe->ops.track)
  514. fe->ops.track(fe, &fepriv->parameters);
  515. } else {
  516. fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
  517. fepriv->delay = HZ / 2;
  518. }
  519. fe->ops.read_status(fe, &s);
  520. if (s != fepriv->status) {
  521. dvb_frontend_add_event(fe, s); /* update event list */
  522. fepriv->status = s;
  523. if (!(s & FE_HAS_LOCK)) {
  524. fepriv->delay = HZ / 10;
  525. fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
  526. } else {
  527. fepriv->delay = 60 * HZ;
  528. }
  529. }
  530. break;
  531. default:
  532. dprintk("%s: UNDEFINED ALGO !\n", __func__);
  533. break;
  534. }
  535. } else {
  536. dvb_frontend_swzigzag(fe);
  537. }
  538. }
  539. if (dvb_powerdown_on_sleep) {
  540. if (fe->ops.set_voltage)
  541. fe->ops.set_voltage(fe, SEC_VOLTAGE_OFF);
  542. if (fe->ops.tuner_ops.sleep) {
  543. if (fe->ops.i2c_gate_ctrl)
  544. fe->ops.i2c_gate_ctrl(fe, 1);
  545. fe->ops.tuner_ops.sleep(fe);
  546. if (fe->ops.i2c_gate_ctrl)
  547. fe->ops.i2c_gate_ctrl(fe, 0);
  548. }
  549. if (fe->ops.sleep)
  550. fe->ops.sleep(fe);
  551. }
  552. fepriv->thread = NULL;
  553. mb();
  554. dvb_frontend_wakeup(fe);
  555. return 0;
  556. }
  557. static void dvb_frontend_stop(struct dvb_frontend *fe)
  558. {
  559. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  560. dprintk ("%s\n", __func__);
  561. fepriv->exit = 1;
  562. mb();
  563. if (!fepriv->thread)
  564. return;
  565. kthread_stop(fepriv->thread);
  566. init_MUTEX (&fepriv->sem);
  567. fepriv->state = FESTATE_IDLE;
  568. /* paranoia check in case a signal arrived */
  569. if (fepriv->thread)
  570. printk("dvb_frontend_stop: warning: thread %p won't exit\n",
  571. fepriv->thread);
  572. }
  573. s32 timeval_usec_diff(struct timeval lasttime, struct timeval curtime)
  574. {
  575. return ((curtime.tv_usec < lasttime.tv_usec) ?
  576. 1000000 - lasttime.tv_usec + curtime.tv_usec :
  577. curtime.tv_usec - lasttime.tv_usec);
  578. }
  579. EXPORT_SYMBOL(timeval_usec_diff);
  580. static inline void timeval_usec_add(struct timeval *curtime, u32 add_usec)
  581. {
  582. curtime->tv_usec += add_usec;
  583. if (curtime->tv_usec >= 1000000) {
  584. curtime->tv_usec -= 1000000;
  585. curtime->tv_sec++;
  586. }
  587. }
  588. /*
  589. * Sleep until gettimeofday() > waketime + add_usec
  590. * This needs to be as precise as possible, but as the delay is
  591. * usually between 2ms and 32ms, it is done using a scheduled msleep
  592. * followed by usleep (normally a busy-wait loop) for the remainder
  593. */
  594. void dvb_frontend_sleep_until(struct timeval *waketime, u32 add_usec)
  595. {
  596. struct timeval lasttime;
  597. s32 delta, newdelta;
  598. timeval_usec_add(waketime, add_usec);
  599. do_gettimeofday(&lasttime);
  600. delta = timeval_usec_diff(lasttime, *waketime);
  601. if (delta > 2500) {
  602. msleep((delta - 1500) / 1000);
  603. do_gettimeofday(&lasttime);
  604. newdelta = timeval_usec_diff(lasttime, *waketime);
  605. delta = (newdelta > delta) ? 0 : newdelta;
  606. }
  607. if (delta > 0)
  608. udelay(delta);
  609. }
  610. EXPORT_SYMBOL(dvb_frontend_sleep_until);
  611. static int dvb_frontend_start(struct dvb_frontend *fe)
  612. {
  613. int ret;
  614. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  615. struct task_struct *fe_thread;
  616. dprintk ("%s\n", __func__);
  617. if (fepriv->thread) {
  618. if (!fepriv->exit)
  619. return 0;
  620. else
  621. dvb_frontend_stop (fe);
  622. }
  623. if (signal_pending(current))
  624. return -EINTR;
  625. if (down_interruptible (&fepriv->sem))
  626. return -EINTR;
  627. fepriv->state = FESTATE_IDLE;
  628. fepriv->exit = 0;
  629. fepriv->thread = NULL;
  630. mb();
  631. fe_thread = kthread_run(dvb_frontend_thread, fe,
  632. "kdvb-ad-%i-fe-%i", fe->dvb->num,fe->id);
  633. if (IS_ERR(fe_thread)) {
  634. ret = PTR_ERR(fe_thread);
  635. printk("dvb_frontend_start: failed to start kthread (%d)\n", ret);
  636. up(&fepriv->sem);
  637. return ret;
  638. }
  639. fepriv->thread = fe_thread;
  640. return 0;
  641. }
  642. static void dvb_frontend_get_frequeny_limits(struct dvb_frontend *fe,
  643. u32 *freq_min, u32 *freq_max)
  644. {
  645. *freq_min = max(fe->ops.info.frequency_min, fe->ops.tuner_ops.info.frequency_min);
  646. if (fe->ops.info.frequency_max == 0)
  647. *freq_max = fe->ops.tuner_ops.info.frequency_max;
  648. else if (fe->ops.tuner_ops.info.frequency_max == 0)
  649. *freq_max = fe->ops.info.frequency_max;
  650. else
  651. *freq_max = min(fe->ops.info.frequency_max, fe->ops.tuner_ops.info.frequency_max);
  652. if (*freq_min == 0 || *freq_max == 0)
  653. printk(KERN_WARNING "DVB: adapter %i frontend %u frequency limits undefined - fix the driver\n",
  654. fe->dvb->num,fe->id);
  655. }
  656. static int dvb_frontend_check_parameters(struct dvb_frontend *fe,
  657. struct dvb_frontend_parameters *parms)
  658. {
  659. u32 freq_min;
  660. u32 freq_max;
  661. /* range check: frequency */
  662. dvb_frontend_get_frequeny_limits(fe, &freq_min, &freq_max);
  663. if ((freq_min && parms->frequency < freq_min) ||
  664. (freq_max && parms->frequency > freq_max)) {
  665. printk(KERN_WARNING "DVB: adapter %i frontend %i frequency %u out of range (%u..%u)\n",
  666. fe->dvb->num, fe->id, parms->frequency, freq_min, freq_max);
  667. return -EINVAL;
  668. }
  669. /* range check: symbol rate */
  670. if (fe->ops.info.type == FE_QPSK) {
  671. if ((fe->ops.info.symbol_rate_min &&
  672. parms->u.qpsk.symbol_rate < fe->ops.info.symbol_rate_min) ||
  673. (fe->ops.info.symbol_rate_max &&
  674. parms->u.qpsk.symbol_rate > fe->ops.info.symbol_rate_max)) {
  675. printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
  676. fe->dvb->num, fe->id, parms->u.qpsk.symbol_rate,
  677. fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
  678. return -EINVAL;
  679. }
  680. } else if (fe->ops.info.type == FE_QAM) {
  681. if ((fe->ops.info.symbol_rate_min &&
  682. parms->u.qam.symbol_rate < fe->ops.info.symbol_rate_min) ||
  683. (fe->ops.info.symbol_rate_max &&
  684. parms->u.qam.symbol_rate > fe->ops.info.symbol_rate_max)) {
  685. printk(KERN_WARNING "DVB: adapter %i frontend %i symbol rate %u out of range (%u..%u)\n",
  686. fe->dvb->num, fe->id, parms->u.qam.symbol_rate,
  687. fe->ops.info.symbol_rate_min, fe->ops.info.symbol_rate_max);
  688. return -EINVAL;
  689. }
  690. }
  691. return 0;
  692. }
  693. struct dtv_cmds_h dtv_cmds[] = {
  694. [DTV_TUNE] = {
  695. .name = "DTV_TUNE",
  696. .cmd = DTV_TUNE,
  697. .set = 1,
  698. },
  699. [DTV_CLEAR] = {
  700. .name = "DTV_CLEAR",
  701. .cmd = DTV_CLEAR,
  702. .set = 1,
  703. },
  704. /* Set */
  705. [DTV_FREQUENCY] = {
  706. .name = "DTV_FREQUENCY",
  707. .cmd = DTV_FREQUENCY,
  708. .set = 1,
  709. },
  710. [DTV_BANDWIDTH_HZ] = {
  711. .name = "DTV_BANDWIDTH_HZ",
  712. .cmd = DTV_BANDWIDTH_HZ,
  713. .set = 1,
  714. },
  715. [DTV_MODULATION] = {
  716. .name = "DTV_MODULATION",
  717. .cmd = DTV_MODULATION,
  718. .set = 1,
  719. },
  720. [DTV_INVERSION] = {
  721. .name = "DTV_INVERSION",
  722. .cmd = DTV_INVERSION,
  723. .set = 1,
  724. },
  725. [DTV_DISEQC_MASTER] = {
  726. .name = "DTV_DISEQC_MASTER",
  727. .cmd = DTV_DISEQC_MASTER,
  728. .set = 1,
  729. .buffer = 1,
  730. },
  731. [DTV_SYMBOL_RATE] = {
  732. .name = "DTV_SYMBOL_RATE",
  733. .cmd = DTV_SYMBOL_RATE,
  734. .set = 1,
  735. },
  736. [DTV_INNER_FEC] = {
  737. .name = "DTV_INNER_FEC",
  738. .cmd = DTV_INNER_FEC,
  739. .set = 1,
  740. },
  741. [DTV_VOLTAGE] = {
  742. .name = "DTV_VOLTAGE",
  743. .cmd = DTV_VOLTAGE,
  744. .set = 1,
  745. },
  746. [DTV_TONE] = {
  747. .name = "DTV_TONE",
  748. .cmd = DTV_TONE,
  749. .set = 1,
  750. },
  751. [DTV_PILOT] = {
  752. .name = "DTV_PILOT",
  753. .cmd = DTV_PILOT,
  754. .set = 1,
  755. },
  756. [DTV_ROLLOFF] = {
  757. .name = "DTV_ROLLOFF",
  758. .cmd = DTV_ROLLOFF,
  759. .set = 1,
  760. },
  761. [DTV_DELIVERY_SYSTEM] = {
  762. .name = "DTV_DELIVERY_SYSTEM",
  763. .cmd = DTV_DELIVERY_SYSTEM,
  764. .set = 1,
  765. },
  766. [DTV_HIERARCHY] = {
  767. .name = "DTV_HIERARCHY",
  768. .cmd = DTV_HIERARCHY,
  769. .set = 1,
  770. },
  771. [DTV_CODE_RATE_HP] = {
  772. .name = "DTV_CODE_RATE_HP",
  773. .cmd = DTV_CODE_RATE_HP,
  774. .set = 1,
  775. },
  776. [DTV_CODE_RATE_LP] = {
  777. .name = "DTV_CODE_RATE_LP",
  778. .cmd = DTV_CODE_RATE_LP,
  779. .set = 1,
  780. },
  781. [DTV_GUARD_INTERVAL] = {
  782. .name = "DTV_GUARD_INTERVAL",
  783. .cmd = DTV_GUARD_INTERVAL,
  784. .set = 1,
  785. },
  786. [DTV_TRANSMISSION_MODE] = {
  787. .name = "DTV_TRANSMISSION_MODE",
  788. .cmd = DTV_TRANSMISSION_MODE,
  789. .set = 1,
  790. },
  791. /* Get */
  792. [DTV_DISEQC_SLAVE_REPLY] = {
  793. .name = "DTV_DISEQC_SLAVE_REPLY",
  794. .cmd = DTV_DISEQC_SLAVE_REPLY,
  795. .set = 0,
  796. .buffer = 1,
  797. },
  798. [DTV_API_VERSION] = {
  799. .name = "DTV_API_VERSION",
  800. .cmd = DTV_API_VERSION,
  801. .set = 0,
  802. },
  803. [DTV_CODE_RATE_HP] = {
  804. .name = "DTV_CODE_RATE_HP",
  805. .cmd = DTV_CODE_RATE_HP,
  806. .set = 0,
  807. },
  808. [DTV_CODE_RATE_LP] = {
  809. .name = "DTV_CODE_RATE_LP",
  810. .cmd = DTV_CODE_RATE_LP,
  811. .set = 0,
  812. },
  813. [DTV_GUARD_INTERVAL] = {
  814. .name = "DTV_GUARD_INTERVAL",
  815. .cmd = DTV_GUARD_INTERVAL,
  816. .set = 0,
  817. },
  818. [DTV_TRANSMISSION_MODE] = {
  819. .name = "DTV_TRANSMISSION_MODE",
  820. .cmd = DTV_TRANSMISSION_MODE,
  821. .set = 0,
  822. },
  823. [DTV_HIERARCHY] = {
  824. .name = "DTV_HIERARCHY",
  825. .cmd = DTV_HIERARCHY,
  826. .set = 0,
  827. },
  828. };
  829. void dtv_property_dump(struct dtv_property *tvp)
  830. {
  831. int i;
  832. if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
  833. printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
  834. __func__, tvp->cmd);
  835. return;
  836. }
  837. dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
  838. ,__func__
  839. ,tvp->cmd
  840. ,dtv_cmds[ tvp->cmd ].name);
  841. if(dtv_cmds[ tvp->cmd ].buffer) {
  842. dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
  843. ,__func__
  844. ,tvp->u.buffer.len);
  845. for(i = 0; i < tvp->u.buffer.len; i++)
  846. dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
  847. ,__func__
  848. ,i
  849. ,tvp->u.buffer.data[i]);
  850. } else
  851. dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
  852. }
  853. int is_legacy_delivery_system(fe_delivery_system_t s)
  854. {
  855. if((s == SYS_UNDEFINED) || (s == SYS_DVBC_ANNEX_AC) ||
  856. (s == SYS_DVBC_ANNEX_B) || (s == SYS_DVBT) || (s == SYS_DVBS) ||
  857. (s == SYS_ATSC))
  858. return 1;
  859. return 0;
  860. }
  861. /* Synchronise the legacy tuning parameters into the cache, so that demodulator
  862. * drivers can use a single set_frontend tuning function, regardless of whether
  863. * it's being used for the legacy or new API, reducing code and complexity.
  864. */
  865. void dtv_property_cache_sync(struct dvb_frontend *fe, struct dvb_frontend_parameters *p)
  866. {
  867. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  868. c->frequency = p->frequency;
  869. c->inversion = p->inversion;
  870. switch (fe->ops.info.type) {
  871. case FE_QPSK:
  872. c->modulation = QPSK; /* implied for DVB-S in legacy API */
  873. c->rolloff = ROLLOFF_35;/* implied for DVB-S */
  874. c->symbol_rate = p->u.qpsk.symbol_rate;
  875. c->fec_inner = p->u.qpsk.fec_inner;
  876. c->delivery_system = SYS_DVBS;
  877. break;
  878. case FE_QAM:
  879. c->symbol_rate = p->u.qam.symbol_rate;
  880. c->fec_inner = p->u.qam.fec_inner;
  881. c->modulation = p->u.qam.modulation;
  882. c->delivery_system = SYS_DVBC_ANNEX_AC;
  883. break;
  884. case FE_OFDM:
  885. if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
  886. c->bandwidth_hz = 6000000;
  887. else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
  888. c->bandwidth_hz = 7000000;
  889. else if (p->u.ofdm.bandwidth == BANDWIDTH_8_MHZ)
  890. c->bandwidth_hz = 8000000;
  891. else
  892. /* Including BANDWIDTH_AUTO */
  893. c->bandwidth_hz = 0;
  894. c->code_rate_HP = p->u.ofdm.code_rate_HP;
  895. c->code_rate_LP = p->u.ofdm.code_rate_LP;
  896. c->modulation = p->u.ofdm.constellation;
  897. c->transmission_mode = p->u.ofdm.transmission_mode;
  898. c->guard_interval = p->u.ofdm.guard_interval;
  899. c->hierarchy = p->u.ofdm.hierarchy_information;
  900. c->delivery_system = SYS_DVBT;
  901. break;
  902. case FE_ATSC:
  903. c->modulation = p->u.vsb.modulation;
  904. if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
  905. c->delivery_system = SYS_ATSC;
  906. else
  907. c->delivery_system = SYS_DVBC_ANNEX_B;
  908. break;
  909. }
  910. }
  911. /* Ensure the cached values are set correctly in the frontend
  912. * legacy tuning structures, for the advanced tuning API.
  913. */
  914. void dtv_property_legacy_params_sync(struct dvb_frontend *fe)
  915. {
  916. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  917. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  918. struct dvb_frontend_parameters *p = &fepriv->parameters;
  919. p->frequency = c->frequency;
  920. p->inversion = c->inversion;
  921. switch (fe->ops.info.type) {
  922. case FE_QPSK:
  923. dprintk("%s() Preparing QPSK req\n", __func__);
  924. p->u.qpsk.symbol_rate = c->symbol_rate;
  925. p->u.qpsk.fec_inner = c->fec_inner;
  926. c->delivery_system = SYS_DVBS;
  927. break;
  928. case FE_QAM:
  929. dprintk("%s() Preparing QAM req\n", __func__);
  930. p->u.qam.symbol_rate = c->symbol_rate;
  931. p->u.qam.fec_inner = c->fec_inner;
  932. p->u.qam.modulation = c->modulation;
  933. c->delivery_system = SYS_DVBC_ANNEX_AC;
  934. break;
  935. case FE_OFDM:
  936. dprintk("%s() Preparing OFDM req\n", __func__);
  937. if (c->bandwidth_hz == 6000000)
  938. p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
  939. else if (c->bandwidth_hz == 7000000)
  940. p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
  941. else if (c->bandwidth_hz == 8000000)
  942. p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
  943. else
  944. p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
  945. p->u.ofdm.code_rate_HP = c->code_rate_HP;
  946. p->u.ofdm.code_rate_LP = c->code_rate_LP;
  947. p->u.ofdm.constellation = c->modulation;
  948. p->u.ofdm.transmission_mode = c->transmission_mode;
  949. p->u.ofdm.guard_interval = c->guard_interval;
  950. p->u.ofdm.hierarchy_information = c->hierarchy;
  951. c->delivery_system = SYS_DVBT;
  952. break;
  953. case FE_ATSC:
  954. dprintk("%s() Preparing VSB req\n", __func__);
  955. p->u.vsb.modulation = c->modulation;
  956. if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
  957. c->delivery_system = SYS_ATSC;
  958. else
  959. c->delivery_system = SYS_DVBC_ANNEX_B;
  960. break;
  961. }
  962. }
  963. /* Ensure the cached values are set correctly in the frontend
  964. * legacy tuning structures, for the legacy tuning API.
  965. */
  966. void dtv_property_adv_params_sync(struct dvb_frontend *fe)
  967. {
  968. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  969. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  970. struct dvb_frontend_parameters *p = &fepriv->parameters;
  971. p->frequency = c->frequency;
  972. p->inversion = c->inversion;
  973. switch(c->modulation) {
  974. case PSK_8:
  975. case APSK_16:
  976. case APSK_32:
  977. case QPSK:
  978. p->u.qpsk.symbol_rate = c->symbol_rate;
  979. p->u.qpsk.fec_inner = c->fec_inner;
  980. break;
  981. default:
  982. break;
  983. }
  984. if(c->delivery_system == SYS_ISDBT) {
  985. /* Fake out a generic DVB-T request so we pass validation in the ioctl */
  986. p->frequency = c->frequency;
  987. p->inversion = INVERSION_AUTO;
  988. p->u.ofdm.constellation = QAM_AUTO;
  989. p->u.ofdm.code_rate_HP = FEC_AUTO;
  990. p->u.ofdm.code_rate_LP = FEC_AUTO;
  991. p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
  992. p->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
  993. p->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
  994. p->u.ofdm.hierarchy_information = HIERARCHY_AUTO;
  995. }
  996. }
  997. void dtv_property_cache_submit(struct dvb_frontend *fe)
  998. {
  999. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1000. /* For legacy delivery systems we don't need the delivery_system to
  1001. * be specified, but we populate the older structures from the cache
  1002. * so we can call set_frontend on older drivers.
  1003. */
  1004. if(is_legacy_delivery_system(c->delivery_system)) {
  1005. dprintk("%s() legacy, modulation = %d\n", __func__, c->modulation);
  1006. dtv_property_legacy_params_sync(fe);
  1007. } else {
  1008. dprintk("%s() adv, modulation = %d\n", __func__, c->modulation);
  1009. /* For advanced delivery systems / modulation types ...
  1010. * we seed the lecacy dvb_frontend_parameters structure
  1011. * so that the sanity checking code later in the IOCTL processing
  1012. * can validate our basic frequency ranges, symbolrates, modulation
  1013. * etc.
  1014. */
  1015. dtv_property_adv_params_sync(fe);
  1016. }
  1017. }
  1018. static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
  1019. unsigned int cmd, void *parg);
  1020. static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
  1021. unsigned int cmd, void *parg);
  1022. int dtv_property_process_get(struct dvb_frontend *fe, struct dtv_property *tvp,
  1023. struct inode *inode, struct file *file)
  1024. {
  1025. int r = 0;
  1026. dtv_property_dump(tvp);
  1027. /* Allow the frontend to validate incoming properties */
  1028. if (fe->ops.get_property)
  1029. r = fe->ops.get_property(fe, tvp);
  1030. if (r < 0)
  1031. return r;
  1032. switch(tvp->cmd) {
  1033. case DTV_FREQUENCY:
  1034. tvp->u.data = fe->dtv_property_cache.frequency;
  1035. break;
  1036. case DTV_MODULATION:
  1037. tvp->u.data = fe->dtv_property_cache.modulation;
  1038. break;
  1039. case DTV_BANDWIDTH_HZ:
  1040. tvp->u.data = fe->dtv_property_cache.bandwidth_hz;
  1041. break;
  1042. case DTV_INVERSION:
  1043. tvp->u.data = fe->dtv_property_cache.inversion;
  1044. break;
  1045. case DTV_SYMBOL_RATE:
  1046. tvp->u.data = fe->dtv_property_cache.symbol_rate;
  1047. break;
  1048. case DTV_INNER_FEC:
  1049. tvp->u.data = fe->dtv_property_cache.fec_inner;
  1050. break;
  1051. case DTV_PILOT:
  1052. tvp->u.data = fe->dtv_property_cache.pilot;
  1053. break;
  1054. case DTV_ROLLOFF:
  1055. tvp->u.data = fe->dtv_property_cache.rolloff;
  1056. break;
  1057. case DTV_DELIVERY_SYSTEM:
  1058. tvp->u.data = fe->dtv_property_cache.delivery_system;
  1059. break;
  1060. case DTV_VOLTAGE:
  1061. tvp->u.data = fe->dtv_property_cache.voltage;
  1062. break;
  1063. case DTV_TONE:
  1064. tvp->u.data = fe->dtv_property_cache.sectone;
  1065. break;
  1066. case DTV_API_VERSION:
  1067. tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
  1068. break;
  1069. case DTV_CODE_RATE_HP:
  1070. tvp->u.data = fe->dtv_property_cache.code_rate_HP;
  1071. break;
  1072. case DTV_CODE_RATE_LP:
  1073. tvp->u.data = fe->dtv_property_cache.code_rate_LP;
  1074. break;
  1075. case DTV_GUARD_INTERVAL:
  1076. tvp->u.data = fe->dtv_property_cache.guard_interval;
  1077. break;
  1078. case DTV_TRANSMISSION_MODE:
  1079. tvp->u.data = fe->dtv_property_cache.transmission_mode;
  1080. break;
  1081. case DTV_HIERARCHY:
  1082. tvp->u.data = fe->dtv_property_cache.hierarchy;
  1083. break;
  1084. default:
  1085. r = -1;
  1086. }
  1087. return r;
  1088. }
  1089. int dtv_property_process_set(struct dvb_frontend *fe, struct dtv_property *tvp,
  1090. struct inode *inode, struct file *file)
  1091. {
  1092. int r = 0;
  1093. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1094. dtv_property_dump(tvp);
  1095. /* Allow the frontend to validate incoming properties */
  1096. if (fe->ops.set_property)
  1097. r = fe->ops.set_property(fe, tvp);
  1098. if (r < 0)
  1099. return r;
  1100. switch(tvp->cmd) {
  1101. case DTV_CLEAR:
  1102. /* Reset a cache of data specific to the frontend here. This does
  1103. * not effect hardware.
  1104. */
  1105. dprintk("%s() Flushing property cache\n", __func__);
  1106. memset(&fe->dtv_property_cache, 0, sizeof(struct dtv_frontend_properties));
  1107. fe->dtv_property_cache.state = tvp->cmd;
  1108. fe->dtv_property_cache.delivery_system = SYS_UNDEFINED;
  1109. break;
  1110. case DTV_TUNE:
  1111. /* interpret the cache of data, build either a traditional frontend
  1112. * tunerequest so we can pass validation in the FE_SET_FRONTEND
  1113. * ioctl.
  1114. */
  1115. fe->dtv_property_cache.state = tvp->cmd;
  1116. dprintk("%s() Finalised property cache\n", __func__);
  1117. dtv_property_cache_submit(fe);
  1118. r |= dvb_frontend_ioctl_legacy(inode, file, FE_SET_FRONTEND,
  1119. &fepriv->parameters);
  1120. break;
  1121. case DTV_FREQUENCY:
  1122. fe->dtv_property_cache.frequency = tvp->u.data;
  1123. break;
  1124. case DTV_MODULATION:
  1125. fe->dtv_property_cache.modulation = tvp->u.data;
  1126. break;
  1127. case DTV_BANDWIDTH_HZ:
  1128. fe->dtv_property_cache.bandwidth_hz = tvp->u.data;
  1129. break;
  1130. case DTV_INVERSION:
  1131. fe->dtv_property_cache.inversion = tvp->u.data;
  1132. break;
  1133. case DTV_SYMBOL_RATE:
  1134. fe->dtv_property_cache.symbol_rate = tvp->u.data;
  1135. break;
  1136. case DTV_INNER_FEC:
  1137. fe->dtv_property_cache.fec_inner = tvp->u.data;
  1138. break;
  1139. case DTV_PILOT:
  1140. fe->dtv_property_cache.pilot = tvp->u.data;
  1141. break;
  1142. case DTV_ROLLOFF:
  1143. fe->dtv_property_cache.rolloff = tvp->u.data;
  1144. break;
  1145. case DTV_DELIVERY_SYSTEM:
  1146. fe->dtv_property_cache.delivery_system = tvp->u.data;
  1147. break;
  1148. case DTV_VOLTAGE:
  1149. fe->dtv_property_cache.voltage = tvp->u.data;
  1150. r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_VOLTAGE,
  1151. (void *)fe->dtv_property_cache.voltage);
  1152. break;
  1153. case DTV_TONE:
  1154. fe->dtv_property_cache.sectone = tvp->u.data;
  1155. r = dvb_frontend_ioctl_legacy(inode, file, FE_SET_TONE,
  1156. (void *)fe->dtv_property_cache.sectone);
  1157. break;
  1158. case DTV_CODE_RATE_HP:
  1159. fe->dtv_property_cache.code_rate_HP = tvp->u.data;
  1160. break;
  1161. case DTV_CODE_RATE_LP:
  1162. fe->dtv_property_cache.code_rate_LP = tvp->u.data;
  1163. break;
  1164. case DTV_GUARD_INTERVAL:
  1165. fe->dtv_property_cache.guard_interval = tvp->u.data;
  1166. break;
  1167. case DTV_TRANSMISSION_MODE:
  1168. fe->dtv_property_cache.transmission_mode = tvp->u.data;
  1169. break;
  1170. case DTV_HIERARCHY:
  1171. fe->dtv_property_cache.hierarchy = tvp->u.data;
  1172. break;
  1173. default:
  1174. r = -1;
  1175. }
  1176. return r;
  1177. }
  1178. static int dvb_frontend_ioctl(struct inode *inode, struct file *file,
  1179. unsigned int cmd, void *parg)
  1180. {
  1181. struct dvb_device *dvbdev = file->private_data;
  1182. struct dvb_frontend *fe = dvbdev->priv;
  1183. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1184. int err = -EOPNOTSUPP;
  1185. dprintk ("%s\n", __func__);
  1186. if (fepriv->exit)
  1187. return -ENODEV;
  1188. if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
  1189. (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
  1190. cmd == FE_DISEQC_RECV_SLAVE_REPLY))
  1191. return -EPERM;
  1192. if (down_interruptible (&fepriv->sem))
  1193. return -ERESTARTSYS;
  1194. if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
  1195. err = dvb_frontend_ioctl_properties(inode, file, cmd, parg);
  1196. else {
  1197. fe->dtv_property_cache.state = DTV_UNDEFINED;
  1198. err = dvb_frontend_ioctl_legacy(inode, file, cmd, parg);
  1199. }
  1200. up(&fepriv->sem);
  1201. return err;
  1202. }
  1203. static int dvb_frontend_ioctl_properties(struct inode *inode, struct file *file,
  1204. unsigned int cmd, void *parg)
  1205. {
  1206. struct dvb_device *dvbdev = file->private_data;
  1207. struct dvb_frontend *fe = dvbdev->priv;
  1208. int err = 0;
  1209. struct dtv_properties *tvps = NULL;
  1210. struct dtv_property *tvp = NULL;
  1211. int i;
  1212. dprintk("%s\n", __func__);
  1213. if(cmd == FE_SET_PROPERTY) {
  1214. tvps = (struct dtv_properties __user *)parg;
  1215. dprintk("%s() properties.num = %d\n", __func__, tvps->num);
  1216. dprintk("%s() properties.props = %p\n", __func__, tvps->props);
  1217. /* Put an arbitrary limit on the number of messages that can
  1218. * be sent at once */
  1219. if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
  1220. return -EINVAL;
  1221. tvp = (struct dtv_property *) kmalloc(tvps->num *
  1222. sizeof(struct dtv_property), GFP_KERNEL);
  1223. if (!tvp) {
  1224. err = -ENOMEM;
  1225. goto out;
  1226. }
  1227. if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
  1228. err = -EFAULT;
  1229. goto out;
  1230. }
  1231. for (i = 0; i < tvps->num; i++) {
  1232. (tvp + i)->result = dtv_property_process_set(fe, tvp + i, inode, file);
  1233. err |= (tvp + i)->result;
  1234. }
  1235. if(fe->dtv_property_cache.state == DTV_TUNE)
  1236. dprintk("%s() Property cache is full, tuning\n", __func__);
  1237. } else
  1238. if(cmd == FE_GET_PROPERTY) {
  1239. tvps = (struct dtv_properties __user *)parg;
  1240. dprintk("%s() properties.num = %d\n", __func__, tvps->num);
  1241. dprintk("%s() properties.props = %p\n", __func__, tvps->props);
  1242. /* Put an arbitrary limit on the number of messages that can
  1243. * be sent at once */
  1244. if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
  1245. return -EINVAL;
  1246. tvp = (struct dtv_property *) kmalloc(tvps->num *
  1247. sizeof(struct dtv_property), GFP_KERNEL);
  1248. if (!tvp) {
  1249. err = -ENOMEM;
  1250. goto out;
  1251. }
  1252. if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
  1253. err = -EFAULT;
  1254. goto out;
  1255. }
  1256. for (i = 0; i < tvps->num; i++) {
  1257. (tvp + i)->result = dtv_property_process_get(fe, tvp + i, inode, file);
  1258. err |= (tvp + i)->result;
  1259. }
  1260. if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
  1261. err = -EFAULT;
  1262. goto out;
  1263. }
  1264. } else
  1265. err = -EOPNOTSUPP;
  1266. out:
  1267. kfree(tvp);
  1268. return err;
  1269. }
  1270. static int dvb_frontend_ioctl_legacy(struct inode *inode, struct file *file,
  1271. unsigned int cmd, void *parg)
  1272. {
  1273. struct dvb_device *dvbdev = file->private_data;
  1274. struct dvb_frontend *fe = dvbdev->priv;
  1275. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1276. int err = -EOPNOTSUPP;
  1277. switch (cmd) {
  1278. case FE_GET_INFO: {
  1279. struct dvb_frontend_info* info = parg;
  1280. memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
  1281. dvb_frontend_get_frequeny_limits(fe, &info->frequency_min, &info->frequency_max);
  1282. /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
  1283. * do it, it is done for it. */
  1284. info->caps |= FE_CAN_INVERSION_AUTO;
  1285. err = 0;
  1286. break;
  1287. }
  1288. case FE_READ_STATUS: {
  1289. fe_status_t* status = parg;
  1290. /* if retune was requested but hasn't occured yet, prevent
  1291. * that user get signal state from previous tuning */
  1292. if(fepriv->state == FESTATE_RETUNE) {
  1293. err=0;
  1294. *status = 0;
  1295. break;
  1296. }
  1297. if (fe->ops.read_status)
  1298. err = fe->ops.read_status(fe, status);
  1299. break;
  1300. }
  1301. case FE_READ_BER:
  1302. if (fe->ops.read_ber)
  1303. err = fe->ops.read_ber(fe, (__u32*) parg);
  1304. break;
  1305. case FE_READ_SIGNAL_STRENGTH:
  1306. if (fe->ops.read_signal_strength)
  1307. err = fe->ops.read_signal_strength(fe, (__u16*) parg);
  1308. break;
  1309. case FE_READ_SNR:
  1310. if (fe->ops.read_snr)
  1311. err = fe->ops.read_snr(fe, (__u16*) parg);
  1312. break;
  1313. case FE_READ_UNCORRECTED_BLOCKS:
  1314. if (fe->ops.read_ucblocks)
  1315. err = fe->ops.read_ucblocks(fe, (__u32*) parg);
  1316. break;
  1317. case FE_DISEQC_RESET_OVERLOAD:
  1318. if (fe->ops.diseqc_reset_overload) {
  1319. err = fe->ops.diseqc_reset_overload(fe);
  1320. fepriv->state = FESTATE_DISEQC;
  1321. fepriv->status = 0;
  1322. }
  1323. break;
  1324. case FE_DISEQC_SEND_MASTER_CMD:
  1325. if (fe->ops.diseqc_send_master_cmd) {
  1326. err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
  1327. fepriv->state = FESTATE_DISEQC;
  1328. fepriv->status = 0;
  1329. }
  1330. break;
  1331. case FE_DISEQC_SEND_BURST:
  1332. if (fe->ops.diseqc_send_burst) {
  1333. err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
  1334. fepriv->state = FESTATE_DISEQC;
  1335. fepriv->status = 0;
  1336. }
  1337. break;
  1338. case FE_SET_TONE:
  1339. if (fe->ops.set_tone) {
  1340. err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
  1341. fepriv->tone = (fe_sec_tone_mode_t) parg;
  1342. fepriv->state = FESTATE_DISEQC;
  1343. fepriv->status = 0;
  1344. }
  1345. break;
  1346. case FE_SET_VOLTAGE:
  1347. if (fe->ops.set_voltage) {
  1348. err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
  1349. fepriv->voltage = (fe_sec_voltage_t) parg;
  1350. fepriv->state = FESTATE_DISEQC;
  1351. fepriv->status = 0;
  1352. }
  1353. break;
  1354. case FE_DISHNETWORK_SEND_LEGACY_CMD:
  1355. if (fe->ops.dishnetwork_send_legacy_command) {
  1356. err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
  1357. fepriv->state = FESTATE_DISEQC;
  1358. fepriv->status = 0;
  1359. } else if (fe->ops.set_voltage) {
  1360. /*
  1361. * NOTE: This is a fallback condition. Some frontends
  1362. * (stv0299 for instance) take longer than 8msec to
  1363. * respond to a set_voltage command. Those switches
  1364. * need custom routines to switch properly. For all
  1365. * other frontends, the following shoule work ok.
  1366. * Dish network legacy switches (as used by Dish500)
  1367. * are controlled by sending 9-bit command words
  1368. * spaced 8msec apart.
  1369. * the actual command word is switch/port dependant
  1370. * so it is up to the userspace application to send
  1371. * the right command.
  1372. * The command must always start with a '0' after
  1373. * initialization, so parg is 8 bits and does not
  1374. * include the initialization or start bit
  1375. */
  1376. unsigned long swcmd = ((unsigned long) parg) << 1;
  1377. struct timeval nexttime;
  1378. struct timeval tv[10];
  1379. int i;
  1380. u8 last = 1;
  1381. if (dvb_frontend_debug)
  1382. printk("%s switch command: 0x%04lx\n", __func__, swcmd);
  1383. do_gettimeofday(&nexttime);
  1384. if (dvb_frontend_debug)
  1385. memcpy(&tv[0], &nexttime, sizeof(struct timeval));
  1386. /* before sending a command, initialize by sending
  1387. * a 32ms 18V to the switch
  1388. */
  1389. fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
  1390. dvb_frontend_sleep_until(&nexttime, 32000);
  1391. for (i = 0; i < 9; i++) {
  1392. if (dvb_frontend_debug)
  1393. do_gettimeofday(&tv[i + 1]);
  1394. if ((swcmd & 0x01) != last) {
  1395. /* set voltage to (last ? 13V : 18V) */
  1396. fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
  1397. last = (last) ? 0 : 1;
  1398. }
  1399. swcmd = swcmd >> 1;
  1400. if (i != 8)
  1401. dvb_frontend_sleep_until(&nexttime, 8000);
  1402. }
  1403. if (dvb_frontend_debug) {
  1404. printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
  1405. __func__, fe->dvb->num);
  1406. for (i = 1; i < 10; i++)
  1407. printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
  1408. }
  1409. err = 0;
  1410. fepriv->state = FESTATE_DISEQC;
  1411. fepriv->status = 0;
  1412. }
  1413. break;
  1414. case FE_DISEQC_RECV_SLAVE_REPLY:
  1415. if (fe->ops.diseqc_recv_slave_reply)
  1416. err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
  1417. break;
  1418. case FE_ENABLE_HIGH_LNB_VOLTAGE:
  1419. if (fe->ops.enable_high_lnb_voltage)
  1420. err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
  1421. break;
  1422. case FE_SET_FRONTEND: {
  1423. struct dvb_frontend_tune_settings fetunesettings;
  1424. if(fe->dtv_property_cache.state == DTV_TUNE) {
  1425. if (dvb_frontend_check_parameters(fe, &fepriv->parameters) < 0) {
  1426. err = -EINVAL;
  1427. break;
  1428. }
  1429. } else {
  1430. if (dvb_frontend_check_parameters(fe, parg) < 0) {
  1431. err = -EINVAL;
  1432. break;
  1433. }
  1434. memcpy (&fepriv->parameters, parg,
  1435. sizeof (struct dvb_frontend_parameters));
  1436. dtv_property_cache_sync(fe, &fepriv->parameters);
  1437. }
  1438. memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
  1439. memcpy(&fetunesettings.parameters, parg,
  1440. sizeof (struct dvb_frontend_parameters));
  1441. /* force auto frequency inversion if requested */
  1442. if (dvb_force_auto_inversion) {
  1443. fepriv->parameters.inversion = INVERSION_AUTO;
  1444. fetunesettings.parameters.inversion = INVERSION_AUTO;
  1445. }
  1446. if (fe->ops.info.type == FE_OFDM) {
  1447. /* without hierarchical coding code_rate_LP is irrelevant,
  1448. * so we tolerate the otherwise invalid FEC_NONE setting */
  1449. if (fepriv->parameters.u.ofdm.hierarchy_information == HIERARCHY_NONE &&
  1450. fepriv->parameters.u.ofdm.code_rate_LP == FEC_NONE)
  1451. fepriv->parameters.u.ofdm.code_rate_LP = FEC_AUTO;
  1452. }
  1453. /* get frontend-specific tuning settings */
  1454. if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
  1455. fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
  1456. fepriv->max_drift = fetunesettings.max_drift;
  1457. fepriv->step_size = fetunesettings.step_size;
  1458. } else {
  1459. /* default values */
  1460. switch(fe->ops.info.type) {
  1461. case FE_QPSK:
  1462. fepriv->min_delay = HZ/20;
  1463. fepriv->step_size = fepriv->parameters.u.qpsk.symbol_rate / 16000;
  1464. fepriv->max_drift = fepriv->parameters.u.qpsk.symbol_rate / 2000;
  1465. break;
  1466. case FE_QAM:
  1467. fepriv->min_delay = HZ/20;
  1468. fepriv->step_size = 0; /* no zigzag */
  1469. fepriv->max_drift = 0;
  1470. break;
  1471. case FE_OFDM:
  1472. fepriv->min_delay = HZ/20;
  1473. fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
  1474. fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  1475. break;
  1476. case FE_ATSC:
  1477. fepriv->min_delay = HZ/20;
  1478. fepriv->step_size = 0;
  1479. fepriv->max_drift = 0;
  1480. break;
  1481. }
  1482. }
  1483. if (dvb_override_tune_delay > 0)
  1484. fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
  1485. fepriv->state = FESTATE_RETUNE;
  1486. dvb_frontend_wakeup(fe);
  1487. dvb_frontend_add_event(fe, 0);
  1488. fepriv->status = 0;
  1489. err = 0;
  1490. break;
  1491. }
  1492. case FE_GET_EVENT:
  1493. err = dvb_frontend_get_event (fe, parg, file->f_flags);
  1494. break;
  1495. case FE_GET_FRONTEND:
  1496. if (fe->ops.get_frontend) {
  1497. memcpy (parg, &fepriv->parameters, sizeof (struct dvb_frontend_parameters));
  1498. err = fe->ops.get_frontend(fe, (struct dvb_frontend_parameters*) parg);
  1499. }
  1500. break;
  1501. case FE_SET_FRONTEND_TUNE_MODE:
  1502. fepriv->tune_mode_flags = (unsigned long) parg;
  1503. err = 0;
  1504. break;
  1505. };
  1506. return err;
  1507. }
  1508. static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
  1509. {
  1510. struct dvb_device *dvbdev = file->private_data;
  1511. struct dvb_frontend *fe = dvbdev->priv;
  1512. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1513. dprintk ("%s\n", __func__);
  1514. poll_wait (file, &fepriv->events.wait_queue, wait);
  1515. if (fepriv->events.eventw != fepriv->events.eventr)
  1516. return (POLLIN | POLLRDNORM | POLLPRI);
  1517. return 0;
  1518. }
  1519. static int dvb_frontend_open(struct inode *inode, struct file *file)
  1520. {
  1521. struct dvb_device *dvbdev = file->private_data;
  1522. struct dvb_frontend *fe = dvbdev->priv;
  1523. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1524. struct dvb_adapter *adapter = fe->dvb;
  1525. int ret;
  1526. dprintk ("%s\n", __func__);
  1527. if (adapter->mfe_shared) {
  1528. mutex_lock (&adapter->mfe_lock);
  1529. if (adapter->mfe_dvbdev == NULL)
  1530. adapter->mfe_dvbdev = dvbdev;
  1531. else if (adapter->mfe_dvbdev != dvbdev) {
  1532. struct dvb_device
  1533. *mfedev = adapter->mfe_dvbdev;
  1534. struct dvb_frontend
  1535. *mfe = mfedev->priv;
  1536. struct dvb_frontend_private
  1537. *mfepriv = mfe->frontend_priv;
  1538. int mferetry = (dvb_mfe_wait_time << 1);
  1539. mutex_unlock (&adapter->mfe_lock);
  1540. while (mferetry-- && (mfedev->users != -1 ||
  1541. mfepriv->thread != NULL)) {
  1542. if(msleep_interruptible(500)) {
  1543. if(signal_pending(current))
  1544. return -EINTR;
  1545. }
  1546. }
  1547. mutex_lock (&adapter->mfe_lock);
  1548. if(adapter->mfe_dvbdev != dvbdev) {
  1549. mfedev = adapter->mfe_dvbdev;
  1550. mfe = mfedev->priv;
  1551. mfepriv = mfe->frontend_priv;
  1552. if (mfedev->users != -1 ||
  1553. mfepriv->thread != NULL) {
  1554. mutex_unlock (&adapter->mfe_lock);
  1555. return -EBUSY;
  1556. }
  1557. adapter->mfe_dvbdev = dvbdev;
  1558. }
  1559. }
  1560. }
  1561. if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
  1562. if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
  1563. goto err0;
  1564. }
  1565. if ((ret = dvb_generic_open (inode, file)) < 0)
  1566. goto err1;
  1567. if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
  1568. /* normal tune mode when opened R/W */
  1569. fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
  1570. fepriv->tone = -1;
  1571. fepriv->voltage = -1;
  1572. ret = dvb_frontend_start (fe);
  1573. if (ret)
  1574. goto err2;
  1575. /* empty event queue */
  1576. fepriv->events.eventr = fepriv->events.eventw = 0;
  1577. }
  1578. if (adapter->mfe_shared)
  1579. mutex_unlock (&adapter->mfe_lock);
  1580. return ret;
  1581. err2:
  1582. dvb_generic_release(inode, file);
  1583. err1:
  1584. if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
  1585. fe->ops.ts_bus_ctrl(fe, 0);
  1586. err0:
  1587. if (adapter->mfe_shared)
  1588. mutex_unlock (&adapter->mfe_lock);
  1589. return ret;
  1590. }
  1591. static int dvb_frontend_release(struct inode *inode, struct file *file)
  1592. {
  1593. struct dvb_device *dvbdev = file->private_data;
  1594. struct dvb_frontend *fe = dvbdev->priv;
  1595. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1596. int ret;
  1597. dprintk ("%s\n", __func__);
  1598. if ((file->f_flags & O_ACCMODE) != O_RDONLY)
  1599. fepriv->release_jiffies = jiffies;
  1600. ret = dvb_generic_release (inode, file);
  1601. if (dvbdev->users == -1) {
  1602. if (fepriv->exit == 1) {
  1603. fops_put(file->f_op);
  1604. file->f_op = NULL;
  1605. wake_up(&dvbdev->wait_queue);
  1606. }
  1607. if (fe->ops.ts_bus_ctrl)
  1608. fe->ops.ts_bus_ctrl(fe, 0);
  1609. }
  1610. return ret;
  1611. }
  1612. static struct file_operations dvb_frontend_fops = {
  1613. .owner = THIS_MODULE,
  1614. .ioctl = dvb_generic_ioctl,
  1615. .poll = dvb_frontend_poll,
  1616. .open = dvb_frontend_open,
  1617. .release = dvb_frontend_release
  1618. };
  1619. int dvb_register_frontend(struct dvb_adapter* dvb,
  1620. struct dvb_frontend* fe)
  1621. {
  1622. struct dvb_frontend_private *fepriv;
  1623. static const struct dvb_device dvbdev_template = {
  1624. .users = ~0,
  1625. .writers = 1,
  1626. .readers = (~0)-1,
  1627. .fops = &dvb_frontend_fops,
  1628. .kernel_ioctl = dvb_frontend_ioctl
  1629. };
  1630. dprintk ("%s\n", __func__);
  1631. if (mutex_lock_interruptible(&frontend_mutex))
  1632. return -ERESTARTSYS;
  1633. fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
  1634. if (fe->frontend_priv == NULL) {
  1635. mutex_unlock(&frontend_mutex);
  1636. return -ENOMEM;
  1637. }
  1638. fepriv = fe->frontend_priv;
  1639. init_MUTEX (&fepriv->sem);
  1640. init_waitqueue_head (&fepriv->wait_queue);
  1641. init_waitqueue_head (&fepriv->events.wait_queue);
  1642. mutex_init(&fepriv->events.mtx);
  1643. fe->dvb = dvb;
  1644. fepriv->inversion = INVERSION_OFF;
  1645. printk ("DVB: registering adapter %i frontend %i (%s)...\n",
  1646. fe->dvb->num,
  1647. fe->id,
  1648. fe->ops.info.name);
  1649. dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
  1650. fe, DVB_DEVICE_FRONTEND);
  1651. mutex_unlock(&frontend_mutex);
  1652. return 0;
  1653. }
  1654. EXPORT_SYMBOL(dvb_register_frontend);
  1655. int dvb_unregister_frontend(struct dvb_frontend* fe)
  1656. {
  1657. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1658. dprintk ("%s\n", __func__);
  1659. mutex_lock(&frontend_mutex);
  1660. dvb_frontend_stop (fe);
  1661. mutex_unlock(&frontend_mutex);
  1662. if (fepriv->dvbdev->users < -1)
  1663. wait_event(fepriv->dvbdev->wait_queue,
  1664. fepriv->dvbdev->users==-1);
  1665. mutex_lock(&frontend_mutex);
  1666. dvb_unregister_device (fepriv->dvbdev);
  1667. /* fe is invalid now */
  1668. kfree(fepriv);
  1669. mutex_unlock(&frontend_mutex);
  1670. return 0;
  1671. }
  1672. EXPORT_SYMBOL(dvb_unregister_frontend);
  1673. #ifdef CONFIG_MEDIA_ATTACH
  1674. void dvb_frontend_detach(struct dvb_frontend* fe)
  1675. {
  1676. void *ptr;
  1677. if (fe->ops.release_sec) {
  1678. fe->ops.release_sec(fe);
  1679. symbol_put_addr(fe->ops.release_sec);
  1680. }
  1681. if (fe->ops.tuner_ops.release) {
  1682. fe->ops.tuner_ops.release(fe);
  1683. symbol_put_addr(fe->ops.tuner_ops.release);
  1684. }
  1685. if (fe->ops.analog_ops.release) {
  1686. fe->ops.analog_ops.release(fe);
  1687. symbol_put_addr(fe->ops.analog_ops.release);
  1688. }
  1689. ptr = (void*)fe->ops.release;
  1690. if (ptr) {
  1691. fe->ops.release(fe);
  1692. symbol_put_addr(ptr);
  1693. }
  1694. }
  1695. #else
  1696. void dvb_frontend_detach(struct dvb_frontend* fe)
  1697. {
  1698. if (fe->ops.release_sec)
  1699. fe->ops.release_sec(fe);
  1700. if (fe->ops.tuner_ops.release)
  1701. fe->ops.tuner_ops.release(fe);
  1702. if (fe->ops.analog_ops.release)
  1703. fe->ops.analog_ops.release(fe);
  1704. if (fe->ops.release)
  1705. fe->ops.release(fe);
  1706. }
  1707. #endif
  1708. EXPORT_SYMBOL(dvb_frontend_detach);