dvb_frontend.c 81 KB

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