dvb_frontend.c 69 KB

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