dvb_frontend.c 70 KB

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