dvb_frontend.c 68 KB

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