dvb_frontend.c 71 KB

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