dvb_frontend.c 73 KB

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