dvb_frontend.c 75 KB

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