dvb_frontend.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575
  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. _DTV_CMD(DTV_ATSCMH_FIC_ERR, 0, 0),
  903. _DTV_CMD(DTV_ATSCMH_CRC_ERR, 0, 0),
  904. _DTV_CMD(DTV_ATSCMH_RS_ERR, 0, 0),
  905. };
  906. static void dtv_property_dump(struct dtv_property *tvp)
  907. {
  908. int i;
  909. if (tvp->cmd <= 0 || tvp->cmd > DTV_MAX_COMMAND) {
  910. printk(KERN_WARNING "%s: tvp.cmd = 0x%08x undefined\n",
  911. __func__, tvp->cmd);
  912. return;
  913. }
  914. dprintk("%s() tvp.cmd = 0x%08x (%s)\n"
  915. ,__func__
  916. ,tvp->cmd
  917. ,dtv_cmds[ tvp->cmd ].name);
  918. if(dtv_cmds[ tvp->cmd ].buffer) {
  919. dprintk("%s() tvp.u.buffer.len = 0x%02x\n"
  920. ,__func__
  921. ,tvp->u.buffer.len);
  922. for(i = 0; i < tvp->u.buffer.len; i++)
  923. dprintk("%s() tvp.u.buffer.data[0x%02x] = 0x%02x\n"
  924. ,__func__
  925. ,i
  926. ,tvp->u.buffer.data[i]);
  927. } else
  928. dprintk("%s() tvp.u.data = 0x%08x\n", __func__, tvp->u.data);
  929. }
  930. /* Synchronise the legacy tuning parameters into the cache, so that demodulator
  931. * drivers can use a single set_frontend tuning function, regardless of whether
  932. * it's being used for the legacy or new API, reducing code and complexity.
  933. */
  934. static int dtv_property_cache_sync(struct dvb_frontend *fe,
  935. struct dtv_frontend_properties *c,
  936. const struct dvb_frontend_parameters *p)
  937. {
  938. c->frequency = p->frequency;
  939. c->inversion = p->inversion;
  940. switch (dvbv3_type(c->delivery_system)) {
  941. case DVBV3_QPSK:
  942. dprintk("%s() Preparing QPSK req\n", __func__);
  943. c->symbol_rate = p->u.qpsk.symbol_rate;
  944. c->fec_inner = p->u.qpsk.fec_inner;
  945. break;
  946. case DVBV3_QAM:
  947. dprintk("%s() Preparing QAM req\n", __func__);
  948. c->symbol_rate = p->u.qam.symbol_rate;
  949. c->fec_inner = p->u.qam.fec_inner;
  950. c->modulation = p->u.qam.modulation;
  951. break;
  952. case DVBV3_OFDM:
  953. dprintk("%s() Preparing OFDM req\n", __func__);
  954. switch (p->u.ofdm.bandwidth) {
  955. case BANDWIDTH_10_MHZ:
  956. c->bandwidth_hz = 10000000;
  957. break;
  958. case BANDWIDTH_8_MHZ:
  959. c->bandwidth_hz = 8000000;
  960. break;
  961. case BANDWIDTH_7_MHZ:
  962. c->bandwidth_hz = 7000000;
  963. break;
  964. case BANDWIDTH_6_MHZ:
  965. c->bandwidth_hz = 6000000;
  966. break;
  967. case BANDWIDTH_5_MHZ:
  968. c->bandwidth_hz = 5000000;
  969. break;
  970. case BANDWIDTH_1_712_MHZ:
  971. c->bandwidth_hz = 1712000;
  972. break;
  973. case BANDWIDTH_AUTO:
  974. c->bandwidth_hz = 0;
  975. }
  976. c->code_rate_HP = p->u.ofdm.code_rate_HP;
  977. c->code_rate_LP = p->u.ofdm.code_rate_LP;
  978. c->modulation = p->u.ofdm.constellation;
  979. c->transmission_mode = p->u.ofdm.transmission_mode;
  980. c->guard_interval = p->u.ofdm.guard_interval;
  981. c->hierarchy = p->u.ofdm.hierarchy_information;
  982. break;
  983. case DVBV3_ATSC:
  984. dprintk("%s() Preparing ATSC req\n", __func__);
  985. c->modulation = p->u.vsb.modulation;
  986. if (c->delivery_system == SYS_ATSCMH)
  987. break;
  988. if ((c->modulation == VSB_8) || (c->modulation == VSB_16))
  989. c->delivery_system = SYS_ATSC;
  990. else
  991. c->delivery_system = SYS_DVBC_ANNEX_B;
  992. break;
  993. case DVBV3_UNKNOWN:
  994. printk(KERN_ERR
  995. "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
  996. __func__, c->delivery_system);
  997. return -EINVAL;
  998. }
  999. return 0;
  1000. }
  1001. /* Ensure the cached values are set correctly in the frontend
  1002. * legacy tuning structures, for the advanced tuning API.
  1003. */
  1004. static int dtv_property_legacy_params_sync(struct dvb_frontend *fe,
  1005. struct dvb_frontend_parameters *p)
  1006. {
  1007. const struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1008. p->frequency = c->frequency;
  1009. p->inversion = c->inversion;
  1010. switch (dvbv3_type(c->delivery_system)) {
  1011. case DVBV3_UNKNOWN:
  1012. printk(KERN_ERR
  1013. "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
  1014. __func__, c->delivery_system);
  1015. return -EINVAL;
  1016. case DVBV3_QPSK:
  1017. dprintk("%s() Preparing QPSK req\n", __func__);
  1018. p->u.qpsk.symbol_rate = c->symbol_rate;
  1019. p->u.qpsk.fec_inner = c->fec_inner;
  1020. break;
  1021. case DVBV3_QAM:
  1022. dprintk("%s() Preparing QAM req\n", __func__);
  1023. p->u.qam.symbol_rate = c->symbol_rate;
  1024. p->u.qam.fec_inner = c->fec_inner;
  1025. p->u.qam.modulation = c->modulation;
  1026. break;
  1027. case DVBV3_OFDM:
  1028. dprintk("%s() Preparing OFDM req\n", __func__);
  1029. switch (c->bandwidth_hz) {
  1030. case 10000000:
  1031. p->u.ofdm.bandwidth = BANDWIDTH_10_MHZ;
  1032. break;
  1033. case 8000000:
  1034. p->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
  1035. break;
  1036. case 7000000:
  1037. p->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
  1038. break;
  1039. case 6000000:
  1040. p->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
  1041. break;
  1042. case 5000000:
  1043. p->u.ofdm.bandwidth = BANDWIDTH_5_MHZ;
  1044. break;
  1045. case 1712000:
  1046. p->u.ofdm.bandwidth = BANDWIDTH_1_712_MHZ;
  1047. break;
  1048. case 0:
  1049. default:
  1050. p->u.ofdm.bandwidth = BANDWIDTH_AUTO;
  1051. }
  1052. p->u.ofdm.code_rate_HP = c->code_rate_HP;
  1053. p->u.ofdm.code_rate_LP = c->code_rate_LP;
  1054. p->u.ofdm.constellation = c->modulation;
  1055. p->u.ofdm.transmission_mode = c->transmission_mode;
  1056. p->u.ofdm.guard_interval = c->guard_interval;
  1057. p->u.ofdm.hierarchy_information = c->hierarchy;
  1058. break;
  1059. case DVBV3_ATSC:
  1060. dprintk("%s() Preparing VSB req\n", __func__);
  1061. p->u.vsb.modulation = c->modulation;
  1062. break;
  1063. }
  1064. return 0;
  1065. }
  1066. /**
  1067. * dtv_get_frontend - calls a callback for retrieving DTV parameters
  1068. * @fe: struct dvb_frontend pointer
  1069. * @c: struct dtv_frontend_properties pointer (DVBv5 cache)
  1070. * @p_out struct dvb_frontend_parameters pointer (DVBv3 FE struct)
  1071. *
  1072. * This routine calls either the DVBv3 or DVBv5 get_frontend call.
  1073. * If c is not null, it will update the DVBv5 cache struct pointed by it.
  1074. * If p_out is not null, it will update the DVBv3 params pointed by it.
  1075. */
  1076. static int dtv_get_frontend(struct dvb_frontend *fe,
  1077. struct dvb_frontend_parameters *p_out)
  1078. {
  1079. int r;
  1080. if (fe->ops.get_frontend) {
  1081. r = fe->ops.get_frontend(fe);
  1082. if (unlikely(r < 0))
  1083. return r;
  1084. if (p_out)
  1085. dtv_property_legacy_params_sync(fe, p_out);
  1086. return 0;
  1087. }
  1088. /* As everything is in cache, get_frontend fops are always supported */
  1089. return 0;
  1090. }
  1091. static int dvb_frontend_ioctl_legacy(struct file *file,
  1092. unsigned int cmd, void *parg);
  1093. static int dvb_frontend_ioctl_properties(struct file *file,
  1094. unsigned int cmd, void *parg);
  1095. static int dtv_property_process_get(struct dvb_frontend *fe,
  1096. const struct dtv_frontend_properties *c,
  1097. struct dtv_property *tvp,
  1098. struct file *file)
  1099. {
  1100. int r, ncaps;
  1101. switch(tvp->cmd) {
  1102. case DTV_ENUM_DELSYS:
  1103. ncaps = 0;
  1104. while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
  1105. tvp->u.buffer.data[ncaps] = fe->ops.delsys[ncaps];
  1106. ncaps++;
  1107. }
  1108. tvp->u.buffer.len = ncaps;
  1109. break;
  1110. case DTV_FREQUENCY:
  1111. tvp->u.data = c->frequency;
  1112. break;
  1113. case DTV_MODULATION:
  1114. tvp->u.data = c->modulation;
  1115. break;
  1116. case DTV_BANDWIDTH_HZ:
  1117. tvp->u.data = c->bandwidth_hz;
  1118. break;
  1119. case DTV_INVERSION:
  1120. tvp->u.data = c->inversion;
  1121. break;
  1122. case DTV_SYMBOL_RATE:
  1123. tvp->u.data = c->symbol_rate;
  1124. break;
  1125. case DTV_INNER_FEC:
  1126. tvp->u.data = c->fec_inner;
  1127. break;
  1128. case DTV_PILOT:
  1129. tvp->u.data = c->pilot;
  1130. break;
  1131. case DTV_ROLLOFF:
  1132. tvp->u.data = c->rolloff;
  1133. break;
  1134. case DTV_DELIVERY_SYSTEM:
  1135. tvp->u.data = c->delivery_system;
  1136. break;
  1137. case DTV_VOLTAGE:
  1138. tvp->u.data = c->voltage;
  1139. break;
  1140. case DTV_TONE:
  1141. tvp->u.data = c->sectone;
  1142. break;
  1143. case DTV_API_VERSION:
  1144. tvp->u.data = (DVB_API_VERSION << 8) | DVB_API_VERSION_MINOR;
  1145. break;
  1146. case DTV_CODE_RATE_HP:
  1147. tvp->u.data = c->code_rate_HP;
  1148. break;
  1149. case DTV_CODE_RATE_LP:
  1150. tvp->u.data = c->code_rate_LP;
  1151. break;
  1152. case DTV_GUARD_INTERVAL:
  1153. tvp->u.data = c->guard_interval;
  1154. break;
  1155. case DTV_TRANSMISSION_MODE:
  1156. tvp->u.data = c->transmission_mode;
  1157. break;
  1158. case DTV_HIERARCHY:
  1159. tvp->u.data = c->hierarchy;
  1160. break;
  1161. /* ISDB-T Support here */
  1162. case DTV_ISDBT_PARTIAL_RECEPTION:
  1163. tvp->u.data = c->isdbt_partial_reception;
  1164. break;
  1165. case DTV_ISDBT_SOUND_BROADCASTING:
  1166. tvp->u.data = c->isdbt_sb_mode;
  1167. break;
  1168. case DTV_ISDBT_SB_SUBCHANNEL_ID:
  1169. tvp->u.data = c->isdbt_sb_subchannel;
  1170. break;
  1171. case DTV_ISDBT_SB_SEGMENT_IDX:
  1172. tvp->u.data = c->isdbt_sb_segment_idx;
  1173. break;
  1174. case DTV_ISDBT_SB_SEGMENT_COUNT:
  1175. tvp->u.data = c->isdbt_sb_segment_count;
  1176. break;
  1177. case DTV_ISDBT_LAYER_ENABLED:
  1178. tvp->u.data = c->isdbt_layer_enabled;
  1179. break;
  1180. case DTV_ISDBT_LAYERA_FEC:
  1181. tvp->u.data = c->layer[0].fec;
  1182. break;
  1183. case DTV_ISDBT_LAYERA_MODULATION:
  1184. tvp->u.data = c->layer[0].modulation;
  1185. break;
  1186. case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
  1187. tvp->u.data = c->layer[0].segment_count;
  1188. break;
  1189. case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
  1190. tvp->u.data = c->layer[0].interleaving;
  1191. break;
  1192. case DTV_ISDBT_LAYERB_FEC:
  1193. tvp->u.data = c->layer[1].fec;
  1194. break;
  1195. case DTV_ISDBT_LAYERB_MODULATION:
  1196. tvp->u.data = c->layer[1].modulation;
  1197. break;
  1198. case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
  1199. tvp->u.data = c->layer[1].segment_count;
  1200. break;
  1201. case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
  1202. tvp->u.data = c->layer[1].interleaving;
  1203. break;
  1204. case DTV_ISDBT_LAYERC_FEC:
  1205. tvp->u.data = c->layer[2].fec;
  1206. break;
  1207. case DTV_ISDBT_LAYERC_MODULATION:
  1208. tvp->u.data = c->layer[2].modulation;
  1209. break;
  1210. case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
  1211. tvp->u.data = c->layer[2].segment_count;
  1212. break;
  1213. case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
  1214. tvp->u.data = c->layer[2].interleaving;
  1215. break;
  1216. case DTV_ISDBS_TS_ID:
  1217. tvp->u.data = c->isdbs_ts_id;
  1218. break;
  1219. case DTV_DVBT2_PLP_ID:
  1220. tvp->u.data = c->dvbt2_plp_id;
  1221. break;
  1222. /* ATSC-MH */
  1223. case DTV_ATSCMH_FIC_VER:
  1224. tvp->u.data = fe->dtv_property_cache.atscmh_fic_ver;
  1225. break;
  1226. case DTV_ATSCMH_PARADE_ID:
  1227. tvp->u.data = fe->dtv_property_cache.atscmh_parade_id;
  1228. break;
  1229. case DTV_ATSCMH_NOG:
  1230. tvp->u.data = fe->dtv_property_cache.atscmh_nog;
  1231. break;
  1232. case DTV_ATSCMH_TNOG:
  1233. tvp->u.data = fe->dtv_property_cache.atscmh_tnog;
  1234. break;
  1235. case DTV_ATSCMH_SGN:
  1236. tvp->u.data = fe->dtv_property_cache.atscmh_sgn;
  1237. break;
  1238. case DTV_ATSCMH_PRC:
  1239. tvp->u.data = fe->dtv_property_cache.atscmh_prc;
  1240. break;
  1241. case DTV_ATSCMH_RS_FRAME_MODE:
  1242. tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_mode;
  1243. break;
  1244. case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
  1245. tvp->u.data = fe->dtv_property_cache.atscmh_rs_frame_ensemble;
  1246. break;
  1247. case DTV_ATSCMH_RS_CODE_MODE_PRI:
  1248. tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_pri;
  1249. break;
  1250. case DTV_ATSCMH_RS_CODE_MODE_SEC:
  1251. tvp->u.data = fe->dtv_property_cache.atscmh_rs_code_mode_sec;
  1252. break;
  1253. case DTV_ATSCMH_SCCC_BLOCK_MODE:
  1254. tvp->u.data = fe->dtv_property_cache.atscmh_sccc_block_mode;
  1255. break;
  1256. case DTV_ATSCMH_SCCC_CODE_MODE_A:
  1257. tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_a;
  1258. break;
  1259. case DTV_ATSCMH_SCCC_CODE_MODE_B:
  1260. tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_b;
  1261. break;
  1262. case DTV_ATSCMH_SCCC_CODE_MODE_C:
  1263. tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_c;
  1264. break;
  1265. case DTV_ATSCMH_SCCC_CODE_MODE_D:
  1266. tvp->u.data = fe->dtv_property_cache.atscmh_sccc_code_mode_d;
  1267. break;
  1268. case DTV_ATSCMH_FIC_ERR:
  1269. tvp->u.data = fe->dtv_property_cache.atscmh_fic_err;
  1270. break;
  1271. case DTV_ATSCMH_CRC_ERR:
  1272. tvp->u.data = fe->dtv_property_cache.atscmh_crc_err;
  1273. break;
  1274. case DTV_ATSCMH_RS_ERR:
  1275. tvp->u.data = fe->dtv_property_cache.atscmh_rs_err;
  1276. break;
  1277. default:
  1278. return -EINVAL;
  1279. }
  1280. /* Allow the frontend to override outgoing properties */
  1281. if (fe->ops.get_property) {
  1282. r = fe->ops.get_property(fe, tvp);
  1283. if (r < 0)
  1284. return r;
  1285. }
  1286. dtv_property_dump(tvp);
  1287. return 0;
  1288. }
  1289. static int dtv_set_frontend(struct dvb_frontend *fe);
  1290. static bool is_dvbv3_delsys(u32 delsys)
  1291. {
  1292. bool status;
  1293. status = (delsys == SYS_DVBT) || (delsys == SYS_DVBC_ANNEX_A) ||
  1294. (delsys == SYS_DVBS) || (delsys == SYS_ATSC);
  1295. return status;
  1296. }
  1297. static int set_delivery_system(struct dvb_frontend *fe, u32 desired_system)
  1298. {
  1299. int ncaps, i;
  1300. u32 delsys = SYS_UNDEFINED;
  1301. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1302. enum dvbv3_emulation_type type;
  1303. /*
  1304. * It was reported that some old DVBv5 applications were
  1305. * filling delivery_system with SYS_UNDEFINED. If this happens,
  1306. * assume that the application wants to use the first supported
  1307. * delivery system.
  1308. */
  1309. if (c->delivery_system == SYS_UNDEFINED)
  1310. c->delivery_system = fe->ops.delsys[0];
  1311. if (desired_system == SYS_UNDEFINED) {
  1312. /*
  1313. * A DVBv3 call doesn't know what's the desired system.
  1314. * Also, DVBv3 applications don't know that ops.info->type
  1315. * could be changed, and they simply dies when it doesn't
  1316. * match.
  1317. * So, don't change the current delivery system, as it
  1318. * may be trying to do the wrong thing, like setting an
  1319. * ISDB-T frontend as DVB-T. Instead, find the closest
  1320. * DVBv3 system that matches the delivery system.
  1321. */
  1322. if (is_dvbv3_delsys(c->delivery_system)) {
  1323. dprintk("%s() Using delivery system to %d\n",
  1324. __func__, c->delivery_system);
  1325. return 0;
  1326. }
  1327. type = dvbv3_type(c->delivery_system);
  1328. switch (type) {
  1329. case DVBV3_QPSK:
  1330. desired_system = SYS_DVBS;
  1331. break;
  1332. case DVBV3_QAM:
  1333. desired_system = SYS_DVBC_ANNEX_A;
  1334. break;
  1335. case DVBV3_ATSC:
  1336. desired_system = SYS_ATSC;
  1337. break;
  1338. case DVBV3_OFDM:
  1339. desired_system = SYS_DVBT;
  1340. break;
  1341. default:
  1342. dprintk("%s(): This frontend doesn't support DVBv3 calls\n",
  1343. __func__);
  1344. return -EINVAL;
  1345. }
  1346. /*
  1347. * Get a delivery system that is compatible with DVBv3
  1348. * NOTE: in order for this to work with softwares like Kaffeine that
  1349. * uses a DVBv5 call for DVB-S2 and a DVBv3 call to go back to
  1350. * DVB-S, drivers that support both should put the SYS_DVBS entry
  1351. * before the SYS_DVBS2, otherwise it won't switch back to DVB-S.
  1352. * The real fix is that userspace applications should not use DVBv3
  1353. * and not trust on calling FE_SET_FRONTEND to switch the delivery
  1354. * system.
  1355. */
  1356. ncaps = 0;
  1357. while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
  1358. if (fe->ops.delsys[ncaps] == desired_system) {
  1359. delsys = desired_system;
  1360. break;
  1361. }
  1362. ncaps++;
  1363. }
  1364. if (delsys == SYS_UNDEFINED) {
  1365. dprintk("%s() Couldn't find a delivery system that matches %d\n",
  1366. __func__, desired_system);
  1367. }
  1368. } else {
  1369. /*
  1370. * This is a DVBv5 call. So, it likely knows the supported
  1371. * delivery systems.
  1372. */
  1373. /* Check if the desired delivery system is supported */
  1374. ncaps = 0;
  1375. while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
  1376. if (fe->ops.delsys[ncaps] == desired_system) {
  1377. c->delivery_system = desired_system;
  1378. dprintk("%s() Changing delivery system to %d\n",
  1379. __func__, desired_system);
  1380. return 0;
  1381. }
  1382. ncaps++;
  1383. }
  1384. type = dvbv3_type(desired_system);
  1385. /*
  1386. * The delivery system is not supported. See if it can be
  1387. * emulated.
  1388. * The emulation only works if the desired system is one of the
  1389. * DVBv3 delivery systems
  1390. */
  1391. if (!is_dvbv3_delsys(desired_system)) {
  1392. dprintk("%s() can't use a DVBv3 FE_SET_FRONTEND call on this frontend\n",
  1393. __func__);
  1394. return -EINVAL;
  1395. }
  1396. /*
  1397. * Get the last non-DVBv3 delivery system that has the same type
  1398. * of the desired system
  1399. */
  1400. ncaps = 0;
  1401. while (fe->ops.delsys[ncaps] && ncaps < MAX_DELSYS) {
  1402. if ((dvbv3_type(fe->ops.delsys[ncaps]) == type) &&
  1403. !is_dvbv3_delsys(fe->ops.delsys[ncaps]))
  1404. delsys = fe->ops.delsys[ncaps];
  1405. ncaps++;
  1406. }
  1407. /* There's nothing compatible with the desired delivery system */
  1408. if (delsys == SYS_UNDEFINED) {
  1409. dprintk("%s() Incompatible DVBv3 FE_SET_FRONTEND call for this frontend\n",
  1410. __func__);
  1411. return -EINVAL;
  1412. }
  1413. }
  1414. c->delivery_system = delsys;
  1415. /*
  1416. * The DVBv3 or DVBv5 call is requesting a different system. So,
  1417. * emulation is needed.
  1418. *
  1419. * Emulate newer delivery systems like ISDBT, DVBT and DMBTH
  1420. * for older DVBv5 applications. The emulation will try to use
  1421. * the auto mode for most things, and will assume that the desired
  1422. * delivery system is the last one at the ops.delsys[] array
  1423. */
  1424. dprintk("%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. dprintk("%s() Using defaults for SYS_ISDBT\n",
  1433. __func__);
  1434. if (!c->bandwidth_hz)
  1435. c->bandwidth_hz = 6000000;
  1436. c->isdbt_partial_reception = 0;
  1437. c->isdbt_sb_mode = 0;
  1438. c->isdbt_sb_subchannel = 0;
  1439. c->isdbt_sb_segment_idx = 0;
  1440. c->isdbt_sb_segment_count = 0;
  1441. c->isdbt_layer_enabled = 0;
  1442. for (i = 0; i < 3; i++) {
  1443. c->layer[i].fec = FEC_AUTO;
  1444. c->layer[i].modulation = QAM_AUTO;
  1445. c->layer[i].interleaving = 0;
  1446. c->layer[i].segment_count = 0;
  1447. }
  1448. }
  1449. }
  1450. dprintk("change delivery system on cache to %d\n", c->delivery_system);
  1451. return 0;
  1452. }
  1453. static int dtv_property_process_set(struct dvb_frontend *fe,
  1454. struct dtv_property *tvp,
  1455. struct file *file)
  1456. {
  1457. int r = 0;
  1458. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1459. /* Allow the frontend to validate incoming properties */
  1460. if (fe->ops.set_property) {
  1461. r = fe->ops.set_property(fe, tvp);
  1462. if (r < 0)
  1463. return r;
  1464. }
  1465. switch(tvp->cmd) {
  1466. case DTV_CLEAR:
  1467. /*
  1468. * Reset a cache of data specific to the frontend here. This does
  1469. * not effect hardware.
  1470. */
  1471. dvb_frontend_clear_cache(fe);
  1472. break;
  1473. case DTV_TUNE:
  1474. /* interpret the cache of data, build either a traditional frontend
  1475. * tunerequest so we can pass validation in the FE_SET_FRONTEND
  1476. * ioctl.
  1477. */
  1478. c->state = tvp->cmd;
  1479. dprintk("%s() Finalised property cache\n", __func__);
  1480. r = dtv_set_frontend(fe);
  1481. break;
  1482. case DTV_FREQUENCY:
  1483. c->frequency = tvp->u.data;
  1484. break;
  1485. case DTV_MODULATION:
  1486. c->modulation = tvp->u.data;
  1487. break;
  1488. case DTV_BANDWIDTH_HZ:
  1489. c->bandwidth_hz = tvp->u.data;
  1490. break;
  1491. case DTV_INVERSION:
  1492. c->inversion = tvp->u.data;
  1493. break;
  1494. case DTV_SYMBOL_RATE:
  1495. c->symbol_rate = tvp->u.data;
  1496. break;
  1497. case DTV_INNER_FEC:
  1498. c->fec_inner = tvp->u.data;
  1499. break;
  1500. case DTV_PILOT:
  1501. c->pilot = tvp->u.data;
  1502. break;
  1503. case DTV_ROLLOFF:
  1504. c->rolloff = tvp->u.data;
  1505. break;
  1506. case DTV_DELIVERY_SYSTEM:
  1507. r = set_delivery_system(fe, tvp->u.data);
  1508. break;
  1509. case DTV_VOLTAGE:
  1510. c->voltage = tvp->u.data;
  1511. r = dvb_frontend_ioctl_legacy(file, FE_SET_VOLTAGE,
  1512. (void *)c->voltage);
  1513. break;
  1514. case DTV_TONE:
  1515. c->sectone = tvp->u.data;
  1516. r = dvb_frontend_ioctl_legacy(file, FE_SET_TONE,
  1517. (void *)c->sectone);
  1518. break;
  1519. case DTV_CODE_RATE_HP:
  1520. c->code_rate_HP = tvp->u.data;
  1521. break;
  1522. case DTV_CODE_RATE_LP:
  1523. c->code_rate_LP = tvp->u.data;
  1524. break;
  1525. case DTV_GUARD_INTERVAL:
  1526. c->guard_interval = tvp->u.data;
  1527. break;
  1528. case DTV_TRANSMISSION_MODE:
  1529. c->transmission_mode = tvp->u.data;
  1530. break;
  1531. case DTV_HIERARCHY:
  1532. c->hierarchy = tvp->u.data;
  1533. break;
  1534. /* ISDB-T Support here */
  1535. case DTV_ISDBT_PARTIAL_RECEPTION:
  1536. c->isdbt_partial_reception = tvp->u.data;
  1537. break;
  1538. case DTV_ISDBT_SOUND_BROADCASTING:
  1539. c->isdbt_sb_mode = tvp->u.data;
  1540. break;
  1541. case DTV_ISDBT_SB_SUBCHANNEL_ID:
  1542. c->isdbt_sb_subchannel = tvp->u.data;
  1543. break;
  1544. case DTV_ISDBT_SB_SEGMENT_IDX:
  1545. c->isdbt_sb_segment_idx = tvp->u.data;
  1546. break;
  1547. case DTV_ISDBT_SB_SEGMENT_COUNT:
  1548. c->isdbt_sb_segment_count = tvp->u.data;
  1549. break;
  1550. case DTV_ISDBT_LAYER_ENABLED:
  1551. c->isdbt_layer_enabled = tvp->u.data;
  1552. break;
  1553. case DTV_ISDBT_LAYERA_FEC:
  1554. c->layer[0].fec = tvp->u.data;
  1555. break;
  1556. case DTV_ISDBT_LAYERA_MODULATION:
  1557. c->layer[0].modulation = tvp->u.data;
  1558. break;
  1559. case DTV_ISDBT_LAYERA_SEGMENT_COUNT:
  1560. c->layer[0].segment_count = tvp->u.data;
  1561. break;
  1562. case DTV_ISDBT_LAYERA_TIME_INTERLEAVING:
  1563. c->layer[0].interleaving = tvp->u.data;
  1564. break;
  1565. case DTV_ISDBT_LAYERB_FEC:
  1566. c->layer[1].fec = tvp->u.data;
  1567. break;
  1568. case DTV_ISDBT_LAYERB_MODULATION:
  1569. c->layer[1].modulation = tvp->u.data;
  1570. break;
  1571. case DTV_ISDBT_LAYERB_SEGMENT_COUNT:
  1572. c->layer[1].segment_count = tvp->u.data;
  1573. break;
  1574. case DTV_ISDBT_LAYERB_TIME_INTERLEAVING:
  1575. c->layer[1].interleaving = tvp->u.data;
  1576. break;
  1577. case DTV_ISDBT_LAYERC_FEC:
  1578. c->layer[2].fec = tvp->u.data;
  1579. break;
  1580. case DTV_ISDBT_LAYERC_MODULATION:
  1581. c->layer[2].modulation = tvp->u.data;
  1582. break;
  1583. case DTV_ISDBT_LAYERC_SEGMENT_COUNT:
  1584. c->layer[2].segment_count = tvp->u.data;
  1585. break;
  1586. case DTV_ISDBT_LAYERC_TIME_INTERLEAVING:
  1587. c->layer[2].interleaving = tvp->u.data;
  1588. break;
  1589. case DTV_ISDBS_TS_ID:
  1590. c->isdbs_ts_id = tvp->u.data;
  1591. break;
  1592. case DTV_DVBT2_PLP_ID:
  1593. c->dvbt2_plp_id = tvp->u.data;
  1594. break;
  1595. /* ATSC-MH */
  1596. case DTV_ATSCMH_PARADE_ID:
  1597. fe->dtv_property_cache.atscmh_parade_id = tvp->u.data;
  1598. break;
  1599. case DTV_ATSCMH_RS_FRAME_ENSEMBLE:
  1600. fe->dtv_property_cache.atscmh_rs_frame_ensemble = tvp->u.data;
  1601. break;
  1602. default:
  1603. return -EINVAL;
  1604. }
  1605. return r;
  1606. }
  1607. static int dvb_frontend_ioctl(struct file *file,
  1608. unsigned int cmd, void *parg)
  1609. {
  1610. struct dvb_device *dvbdev = file->private_data;
  1611. struct dvb_frontend *fe = dvbdev->priv;
  1612. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1613. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1614. int err = -EOPNOTSUPP;
  1615. dprintk("%s (%d)\n", __func__, _IOC_NR(cmd));
  1616. if (fepriv->exit != DVB_FE_NO_EXIT)
  1617. return -ENODEV;
  1618. if ((file->f_flags & O_ACCMODE) == O_RDONLY &&
  1619. (_IOC_DIR(cmd) != _IOC_READ || cmd == FE_GET_EVENT ||
  1620. cmd == FE_DISEQC_RECV_SLAVE_REPLY))
  1621. return -EPERM;
  1622. if (down_interruptible (&fepriv->sem))
  1623. return -ERESTARTSYS;
  1624. if ((cmd == FE_SET_PROPERTY) || (cmd == FE_GET_PROPERTY))
  1625. err = dvb_frontend_ioctl_properties(file, cmd, parg);
  1626. else {
  1627. c->state = DTV_UNDEFINED;
  1628. err = dvb_frontend_ioctl_legacy(file, cmd, parg);
  1629. }
  1630. up(&fepriv->sem);
  1631. return err;
  1632. }
  1633. static int dvb_frontend_ioctl_properties(struct file *file,
  1634. unsigned int cmd, void *parg)
  1635. {
  1636. struct dvb_device *dvbdev = file->private_data;
  1637. struct dvb_frontend *fe = dvbdev->priv;
  1638. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1639. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1640. int err = 0;
  1641. struct dtv_properties *tvps = NULL;
  1642. struct dtv_property *tvp = NULL;
  1643. int i;
  1644. dprintk("%s\n", __func__);
  1645. if(cmd == FE_SET_PROPERTY) {
  1646. tvps = (struct dtv_properties __user *)parg;
  1647. dprintk("%s() properties.num = %d\n", __func__, tvps->num);
  1648. dprintk("%s() properties.props = %p\n", __func__, tvps->props);
  1649. /* Put an arbitrary limit on the number of messages that can
  1650. * be sent at once */
  1651. if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
  1652. return -EINVAL;
  1653. tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
  1654. if (!tvp) {
  1655. err = -ENOMEM;
  1656. goto out;
  1657. }
  1658. if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
  1659. err = -EFAULT;
  1660. goto out;
  1661. }
  1662. for (i = 0; i < tvps->num; i++) {
  1663. err = dtv_property_process_set(fe, tvp + i, file);
  1664. if (err < 0)
  1665. goto out;
  1666. (tvp + i)->result = err;
  1667. }
  1668. if (c->state == DTV_TUNE)
  1669. dprintk("%s() Property cache is full, tuning\n", __func__);
  1670. } else
  1671. if(cmd == FE_GET_PROPERTY) {
  1672. tvps = (struct dtv_properties __user *)parg;
  1673. dprintk("%s() properties.num = %d\n", __func__, tvps->num);
  1674. dprintk("%s() properties.props = %p\n", __func__, tvps->props);
  1675. /* Put an arbitrary limit on the number of messages that can
  1676. * be sent at once */
  1677. if ((tvps->num == 0) || (tvps->num > DTV_IOCTL_MAX_MSGS))
  1678. return -EINVAL;
  1679. tvp = kmalloc(tvps->num * sizeof(struct dtv_property), GFP_KERNEL);
  1680. if (!tvp) {
  1681. err = -ENOMEM;
  1682. goto out;
  1683. }
  1684. if (copy_from_user(tvp, tvps->props, tvps->num * sizeof(struct dtv_property))) {
  1685. err = -EFAULT;
  1686. goto out;
  1687. }
  1688. /*
  1689. * Fills the cache out struct with the cache contents, plus
  1690. * the data retrieved from get_frontend, if the frontend
  1691. * is not idle. Otherwise, returns the cached content
  1692. */
  1693. if (fepriv->state != FESTATE_IDLE) {
  1694. err = dtv_get_frontend(fe, NULL);
  1695. if (err < 0)
  1696. goto out;
  1697. }
  1698. for (i = 0; i < tvps->num; i++) {
  1699. err = dtv_property_process_get(fe, c, tvp + i, file);
  1700. if (err < 0)
  1701. goto out;
  1702. (tvp + i)->result = err;
  1703. }
  1704. if (copy_to_user(tvps->props, tvp, tvps->num * sizeof(struct dtv_property))) {
  1705. err = -EFAULT;
  1706. goto out;
  1707. }
  1708. } else
  1709. err = -EOPNOTSUPP;
  1710. out:
  1711. kfree(tvp);
  1712. return err;
  1713. }
  1714. static int dtv_set_frontend(struct dvb_frontend *fe)
  1715. {
  1716. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1717. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1718. struct dvb_frontend_tune_settings fetunesettings;
  1719. u32 rolloff = 0;
  1720. if (dvb_frontend_check_parameters(fe) < 0)
  1721. return -EINVAL;
  1722. /*
  1723. * Initialize output parameters to match the values given by
  1724. * the user. FE_SET_FRONTEND triggers an initial frontend event
  1725. * with status = 0, which copies output parameters to userspace.
  1726. */
  1727. dtv_property_legacy_params_sync(fe, &fepriv->parameters_out);
  1728. /*
  1729. * Be sure that the bandwidth will be filled for all
  1730. * non-satellite systems, as tuners need to know what
  1731. * low pass/Nyquist half filter should be applied, in
  1732. * order to avoid inter-channel noise.
  1733. *
  1734. * ISDB-T and DVB-T/T2 already sets bandwidth.
  1735. * ATSC and DVB-C don't set, so, the core should fill it.
  1736. *
  1737. * On DVB-C Annex A and C, the bandwidth is a function of
  1738. * the roll-off and symbol rate. Annex B defines different
  1739. * roll-off factors depending on the modulation. Fortunately,
  1740. * Annex B is only used with 6MHz, so there's no need to
  1741. * calculate it.
  1742. *
  1743. * While not officially supported, a side effect of handling it at
  1744. * the cache level is that a program could retrieve the bandwidth
  1745. * via DTV_BANDWIDTH_HZ, which may be useful for test programs.
  1746. */
  1747. switch (c->delivery_system) {
  1748. case SYS_ATSC:
  1749. case SYS_DVBC_ANNEX_B:
  1750. c->bandwidth_hz = 6000000;
  1751. break;
  1752. case SYS_DVBC_ANNEX_A:
  1753. rolloff = 115;
  1754. break;
  1755. case SYS_DVBC_ANNEX_C:
  1756. rolloff = 113;
  1757. break;
  1758. default:
  1759. break;
  1760. }
  1761. if (rolloff)
  1762. c->bandwidth_hz = (c->symbol_rate * rolloff) / 100;
  1763. /* force auto frequency inversion if requested */
  1764. if (dvb_force_auto_inversion)
  1765. c->inversion = INVERSION_AUTO;
  1766. /*
  1767. * without hierarchical coding code_rate_LP is irrelevant,
  1768. * so we tolerate the otherwise invalid FEC_NONE setting
  1769. */
  1770. if (c->hierarchy == HIERARCHY_NONE && c->code_rate_LP == FEC_NONE)
  1771. c->code_rate_LP = FEC_AUTO;
  1772. /* get frontend-specific tuning settings */
  1773. memset(&fetunesettings, 0, sizeof(struct dvb_frontend_tune_settings));
  1774. if (fe->ops.get_tune_settings && (fe->ops.get_tune_settings(fe, &fetunesettings) == 0)) {
  1775. fepriv->min_delay = (fetunesettings.min_delay_ms * HZ) / 1000;
  1776. fepriv->max_drift = fetunesettings.max_drift;
  1777. fepriv->step_size = fetunesettings.step_size;
  1778. } else {
  1779. /* default values */
  1780. switch (c->delivery_system) {
  1781. case SYS_DVBS:
  1782. case SYS_DVBS2:
  1783. case SYS_ISDBS:
  1784. case SYS_TURBO:
  1785. case SYS_DVBC_ANNEX_A:
  1786. case SYS_DVBC_ANNEX_C:
  1787. fepriv->min_delay = HZ / 20;
  1788. fepriv->step_size = c->symbol_rate / 16000;
  1789. fepriv->max_drift = c->symbol_rate / 2000;
  1790. break;
  1791. case SYS_DVBT:
  1792. case SYS_DVBT2:
  1793. case SYS_ISDBT:
  1794. case SYS_DMBTH:
  1795. fepriv->min_delay = HZ / 20;
  1796. fepriv->step_size = fe->ops.info.frequency_stepsize * 2;
  1797. fepriv->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  1798. break;
  1799. default:
  1800. /*
  1801. * FIXME: This sounds wrong! if freqency_stepsize is
  1802. * defined by the frontend, why not use it???
  1803. */
  1804. fepriv->min_delay = HZ / 20;
  1805. fepriv->step_size = 0; /* no zigzag */
  1806. fepriv->max_drift = 0;
  1807. break;
  1808. }
  1809. }
  1810. if (dvb_override_tune_delay > 0)
  1811. fepriv->min_delay = (dvb_override_tune_delay * HZ) / 1000;
  1812. fepriv->state = FESTATE_RETUNE;
  1813. /* Request the search algorithm to search */
  1814. fepriv->algo_status |= DVBFE_ALGO_SEARCH_AGAIN;
  1815. dvb_frontend_clear_events(fe);
  1816. dvb_frontend_add_event(fe, 0);
  1817. dvb_frontend_wakeup(fe);
  1818. fepriv->status = 0;
  1819. return 0;
  1820. }
  1821. static int dvb_frontend_ioctl_legacy(struct file *file,
  1822. unsigned int cmd, void *parg)
  1823. {
  1824. struct dvb_device *dvbdev = file->private_data;
  1825. struct dvb_frontend *fe = dvbdev->priv;
  1826. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  1827. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  1828. int cb_err, err = -EOPNOTSUPP;
  1829. if (fe->dvb->fe_ioctl_override) {
  1830. cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
  1831. DVB_FE_IOCTL_PRE);
  1832. if (cb_err < 0)
  1833. return cb_err;
  1834. if (cb_err > 0)
  1835. return 0;
  1836. /* fe_ioctl_override returning 0 allows
  1837. * dvb-core to continue handling the ioctl */
  1838. }
  1839. switch (cmd) {
  1840. case FE_GET_INFO: {
  1841. struct dvb_frontend_info* info = parg;
  1842. memcpy(info, &fe->ops.info, sizeof(struct dvb_frontend_info));
  1843. dvb_frontend_get_frequency_limits(fe, &info->frequency_min, &info->frequency_max);
  1844. /*
  1845. * Associate the 4 delivery systems supported by DVBv3
  1846. * API with their DVBv5 counterpart. For the other standards,
  1847. * use the closest type, assuming that it would hopefully
  1848. * work with a DVBv3 application.
  1849. * It should be noticed that, on multi-frontend devices with
  1850. * different types (terrestrial and cable, for example),
  1851. * a pure DVBv3 application won't be able to use all delivery
  1852. * systems. Yet, changing the DVBv5 cache to the other delivery
  1853. * system should be enough for making it work.
  1854. */
  1855. switch (dvbv3_type(c->delivery_system)) {
  1856. case DVBV3_QPSK:
  1857. info->type = FE_QPSK;
  1858. break;
  1859. case DVBV3_ATSC:
  1860. info->type = FE_ATSC;
  1861. break;
  1862. case DVBV3_QAM:
  1863. info->type = FE_QAM;
  1864. break;
  1865. case DVBV3_OFDM:
  1866. info->type = FE_OFDM;
  1867. break;
  1868. default:
  1869. printk(KERN_ERR
  1870. "%s: doesn't know how to handle a DVBv3 call to delivery system %i\n",
  1871. __func__, c->delivery_system);
  1872. fe->ops.info.type = FE_OFDM;
  1873. }
  1874. dprintk("current delivery system on cache: %d, V3 type: %d\n",
  1875. c->delivery_system, fe->ops.info.type);
  1876. /* Force the CAN_INVERSION_AUTO bit on. If the frontend doesn't
  1877. * do it, it is done for it. */
  1878. info->caps |= FE_CAN_INVERSION_AUTO;
  1879. err = 0;
  1880. break;
  1881. }
  1882. case FE_READ_STATUS: {
  1883. fe_status_t* status = parg;
  1884. /* if retune was requested but hasn't occurred yet, prevent
  1885. * that user get signal state from previous tuning */
  1886. if (fepriv->state == FESTATE_RETUNE ||
  1887. fepriv->state == FESTATE_ERROR) {
  1888. err=0;
  1889. *status = 0;
  1890. break;
  1891. }
  1892. if (fe->ops.read_status)
  1893. err = fe->ops.read_status(fe, status);
  1894. break;
  1895. }
  1896. case FE_READ_BER:
  1897. if (fe->ops.read_ber)
  1898. err = fe->ops.read_ber(fe, (__u32*) parg);
  1899. break;
  1900. case FE_READ_SIGNAL_STRENGTH:
  1901. if (fe->ops.read_signal_strength)
  1902. err = fe->ops.read_signal_strength(fe, (__u16*) parg);
  1903. break;
  1904. case FE_READ_SNR:
  1905. if (fe->ops.read_snr)
  1906. err = fe->ops.read_snr(fe, (__u16*) parg);
  1907. break;
  1908. case FE_READ_UNCORRECTED_BLOCKS:
  1909. if (fe->ops.read_ucblocks)
  1910. err = fe->ops.read_ucblocks(fe, (__u32*) parg);
  1911. break;
  1912. case FE_DISEQC_RESET_OVERLOAD:
  1913. if (fe->ops.diseqc_reset_overload) {
  1914. err = fe->ops.diseqc_reset_overload(fe);
  1915. fepriv->state = FESTATE_DISEQC;
  1916. fepriv->status = 0;
  1917. }
  1918. break;
  1919. case FE_DISEQC_SEND_MASTER_CMD:
  1920. if (fe->ops.diseqc_send_master_cmd) {
  1921. err = fe->ops.diseqc_send_master_cmd(fe, (struct dvb_diseqc_master_cmd*) parg);
  1922. fepriv->state = FESTATE_DISEQC;
  1923. fepriv->status = 0;
  1924. }
  1925. break;
  1926. case FE_DISEQC_SEND_BURST:
  1927. if (fe->ops.diseqc_send_burst) {
  1928. err = fe->ops.diseqc_send_burst(fe, (fe_sec_mini_cmd_t) parg);
  1929. fepriv->state = FESTATE_DISEQC;
  1930. fepriv->status = 0;
  1931. }
  1932. break;
  1933. case FE_SET_TONE:
  1934. if (fe->ops.set_tone) {
  1935. err = fe->ops.set_tone(fe, (fe_sec_tone_mode_t) parg);
  1936. fepriv->tone = (fe_sec_tone_mode_t) parg;
  1937. fepriv->state = FESTATE_DISEQC;
  1938. fepriv->status = 0;
  1939. }
  1940. break;
  1941. case FE_SET_VOLTAGE:
  1942. if (fe->ops.set_voltage) {
  1943. err = fe->ops.set_voltage(fe, (fe_sec_voltage_t) parg);
  1944. fepriv->voltage = (fe_sec_voltage_t) parg;
  1945. fepriv->state = FESTATE_DISEQC;
  1946. fepriv->status = 0;
  1947. }
  1948. break;
  1949. case FE_DISHNETWORK_SEND_LEGACY_CMD:
  1950. if (fe->ops.dishnetwork_send_legacy_command) {
  1951. err = fe->ops.dishnetwork_send_legacy_command(fe, (unsigned long) parg);
  1952. fepriv->state = FESTATE_DISEQC;
  1953. fepriv->status = 0;
  1954. } else if (fe->ops.set_voltage) {
  1955. /*
  1956. * NOTE: This is a fallback condition. Some frontends
  1957. * (stv0299 for instance) take longer than 8msec to
  1958. * respond to a set_voltage command. Those switches
  1959. * need custom routines to switch properly. For all
  1960. * other frontends, the following should work ok.
  1961. * Dish network legacy switches (as used by Dish500)
  1962. * are controlled by sending 9-bit command words
  1963. * spaced 8msec apart.
  1964. * the actual command word is switch/port dependent
  1965. * so it is up to the userspace application to send
  1966. * the right command.
  1967. * The command must always start with a '0' after
  1968. * initialization, so parg is 8 bits and does not
  1969. * include the initialization or start bit
  1970. */
  1971. unsigned long swcmd = ((unsigned long) parg) << 1;
  1972. struct timeval nexttime;
  1973. struct timeval tv[10];
  1974. int i;
  1975. u8 last = 1;
  1976. if (dvb_frontend_debug)
  1977. printk("%s switch command: 0x%04lx\n", __func__, swcmd);
  1978. do_gettimeofday(&nexttime);
  1979. if (dvb_frontend_debug)
  1980. memcpy(&tv[0], &nexttime, sizeof(struct timeval));
  1981. /* before sending a command, initialize by sending
  1982. * a 32ms 18V to the switch
  1983. */
  1984. fe->ops.set_voltage(fe, SEC_VOLTAGE_18);
  1985. dvb_frontend_sleep_until(&nexttime, 32000);
  1986. for (i = 0; i < 9; i++) {
  1987. if (dvb_frontend_debug)
  1988. do_gettimeofday(&tv[i + 1]);
  1989. if ((swcmd & 0x01) != last) {
  1990. /* set voltage to (last ? 13V : 18V) */
  1991. fe->ops.set_voltage(fe, (last) ? SEC_VOLTAGE_13 : SEC_VOLTAGE_18);
  1992. last = (last) ? 0 : 1;
  1993. }
  1994. swcmd = swcmd >> 1;
  1995. if (i != 8)
  1996. dvb_frontend_sleep_until(&nexttime, 8000);
  1997. }
  1998. if (dvb_frontend_debug) {
  1999. printk("%s(%d): switch delay (should be 32k followed by all 8k\n",
  2000. __func__, fe->dvb->num);
  2001. for (i = 1; i < 10; i++)
  2002. printk("%d: %d\n", i, timeval_usec_diff(tv[i-1] , tv[i]));
  2003. }
  2004. err = 0;
  2005. fepriv->state = FESTATE_DISEQC;
  2006. fepriv->status = 0;
  2007. }
  2008. break;
  2009. case FE_DISEQC_RECV_SLAVE_REPLY:
  2010. if (fe->ops.diseqc_recv_slave_reply)
  2011. err = fe->ops.diseqc_recv_slave_reply(fe, (struct dvb_diseqc_slave_reply*) parg);
  2012. break;
  2013. case FE_ENABLE_HIGH_LNB_VOLTAGE:
  2014. if (fe->ops.enable_high_lnb_voltage)
  2015. err = fe->ops.enable_high_lnb_voltage(fe, (long) parg);
  2016. break;
  2017. case FE_SET_FRONTEND:
  2018. err = set_delivery_system(fe, SYS_UNDEFINED);
  2019. if (err)
  2020. break;
  2021. err = dtv_property_cache_sync(fe, c, parg);
  2022. if (err)
  2023. break;
  2024. err = dtv_set_frontend(fe);
  2025. break;
  2026. case FE_GET_EVENT:
  2027. err = dvb_frontend_get_event (fe, parg, file->f_flags);
  2028. break;
  2029. case FE_GET_FRONTEND:
  2030. err = dtv_get_frontend(fe, parg);
  2031. break;
  2032. case FE_SET_FRONTEND_TUNE_MODE:
  2033. fepriv->tune_mode_flags = (unsigned long) parg;
  2034. err = 0;
  2035. break;
  2036. };
  2037. if (fe->dvb->fe_ioctl_override) {
  2038. cb_err = fe->dvb->fe_ioctl_override(fe, cmd, parg,
  2039. DVB_FE_IOCTL_POST);
  2040. if (cb_err < 0)
  2041. return cb_err;
  2042. }
  2043. return err;
  2044. }
  2045. static unsigned int dvb_frontend_poll(struct file *file, struct poll_table_struct *wait)
  2046. {
  2047. struct dvb_device *dvbdev = file->private_data;
  2048. struct dvb_frontend *fe = dvbdev->priv;
  2049. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  2050. dprintk ("%s\n", __func__);
  2051. poll_wait (file, &fepriv->events.wait_queue, wait);
  2052. if (fepriv->events.eventw != fepriv->events.eventr)
  2053. return (POLLIN | POLLRDNORM | POLLPRI);
  2054. return 0;
  2055. }
  2056. static int dvb_frontend_open(struct inode *inode, struct file *file)
  2057. {
  2058. struct dvb_device *dvbdev = file->private_data;
  2059. struct dvb_frontend *fe = dvbdev->priv;
  2060. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  2061. struct dvb_adapter *adapter = fe->dvb;
  2062. int ret;
  2063. dprintk ("%s\n", __func__);
  2064. if (fepriv->exit == DVB_FE_DEVICE_REMOVED)
  2065. return -ENODEV;
  2066. if (adapter->mfe_shared) {
  2067. mutex_lock (&adapter->mfe_lock);
  2068. if (adapter->mfe_dvbdev == NULL)
  2069. adapter->mfe_dvbdev = dvbdev;
  2070. else if (adapter->mfe_dvbdev != dvbdev) {
  2071. struct dvb_device
  2072. *mfedev = adapter->mfe_dvbdev;
  2073. struct dvb_frontend
  2074. *mfe = mfedev->priv;
  2075. struct dvb_frontend_private
  2076. *mfepriv = mfe->frontend_priv;
  2077. int mferetry = (dvb_mfe_wait_time << 1);
  2078. mutex_unlock (&adapter->mfe_lock);
  2079. while (mferetry-- && (mfedev->users != -1 ||
  2080. mfepriv->thread != NULL)) {
  2081. if(msleep_interruptible(500)) {
  2082. if(signal_pending(current))
  2083. return -EINTR;
  2084. }
  2085. }
  2086. mutex_lock (&adapter->mfe_lock);
  2087. if(adapter->mfe_dvbdev != dvbdev) {
  2088. mfedev = adapter->mfe_dvbdev;
  2089. mfe = mfedev->priv;
  2090. mfepriv = mfe->frontend_priv;
  2091. if (mfedev->users != -1 ||
  2092. mfepriv->thread != NULL) {
  2093. mutex_unlock (&adapter->mfe_lock);
  2094. return -EBUSY;
  2095. }
  2096. adapter->mfe_dvbdev = dvbdev;
  2097. }
  2098. }
  2099. }
  2100. if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl) {
  2101. if ((ret = fe->ops.ts_bus_ctrl(fe, 1)) < 0)
  2102. goto err0;
  2103. /* If we took control of the bus, we need to force
  2104. reinitialization. This is because many ts_bus_ctrl()
  2105. functions strobe the RESET pin on the demod, and if the
  2106. frontend thread already exists then the dvb_init() routine
  2107. won't get called (which is what usually does initial
  2108. register configuration). */
  2109. fepriv->reinitialise = 1;
  2110. }
  2111. if ((ret = dvb_generic_open (inode, file)) < 0)
  2112. goto err1;
  2113. if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
  2114. /* normal tune mode when opened R/W */
  2115. fepriv->tune_mode_flags &= ~FE_TUNE_MODE_ONESHOT;
  2116. fepriv->tone = -1;
  2117. fepriv->voltage = -1;
  2118. ret = dvb_frontend_start (fe);
  2119. if (ret)
  2120. goto err2;
  2121. /* empty event queue */
  2122. fepriv->events.eventr = fepriv->events.eventw = 0;
  2123. }
  2124. if (adapter->mfe_shared)
  2125. mutex_unlock (&adapter->mfe_lock);
  2126. return ret;
  2127. err2:
  2128. dvb_generic_release(inode, file);
  2129. err1:
  2130. if (dvbdev->users == -1 && fe->ops.ts_bus_ctrl)
  2131. fe->ops.ts_bus_ctrl(fe, 0);
  2132. err0:
  2133. if (adapter->mfe_shared)
  2134. mutex_unlock (&adapter->mfe_lock);
  2135. return ret;
  2136. }
  2137. static int dvb_frontend_release(struct inode *inode, struct file *file)
  2138. {
  2139. struct dvb_device *dvbdev = file->private_data;
  2140. struct dvb_frontend *fe = dvbdev->priv;
  2141. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  2142. int ret;
  2143. dprintk ("%s\n", __func__);
  2144. if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
  2145. fepriv->release_jiffies = jiffies;
  2146. mb();
  2147. }
  2148. ret = dvb_generic_release (inode, file);
  2149. if (dvbdev->users == -1) {
  2150. wake_up(&fepriv->wait_queue);
  2151. if (fepriv->exit != DVB_FE_NO_EXIT) {
  2152. fops_put(file->f_op);
  2153. file->f_op = NULL;
  2154. wake_up(&dvbdev->wait_queue);
  2155. }
  2156. if (fe->ops.ts_bus_ctrl)
  2157. fe->ops.ts_bus_ctrl(fe, 0);
  2158. }
  2159. return ret;
  2160. }
  2161. static const struct file_operations dvb_frontend_fops = {
  2162. .owner = THIS_MODULE,
  2163. .unlocked_ioctl = dvb_generic_ioctl,
  2164. .poll = dvb_frontend_poll,
  2165. .open = dvb_frontend_open,
  2166. .release = dvb_frontend_release,
  2167. .llseek = noop_llseek,
  2168. };
  2169. int dvb_register_frontend(struct dvb_adapter* dvb,
  2170. struct dvb_frontend* fe)
  2171. {
  2172. struct dvb_frontend_private *fepriv;
  2173. static const struct dvb_device dvbdev_template = {
  2174. .users = ~0,
  2175. .writers = 1,
  2176. .readers = (~0)-1,
  2177. .fops = &dvb_frontend_fops,
  2178. .kernel_ioctl = dvb_frontend_ioctl
  2179. };
  2180. dprintk ("%s\n", __func__);
  2181. if (mutex_lock_interruptible(&frontend_mutex))
  2182. return -ERESTARTSYS;
  2183. fe->frontend_priv = kzalloc(sizeof(struct dvb_frontend_private), GFP_KERNEL);
  2184. if (fe->frontend_priv == NULL) {
  2185. mutex_unlock(&frontend_mutex);
  2186. return -ENOMEM;
  2187. }
  2188. fepriv = fe->frontend_priv;
  2189. sema_init(&fepriv->sem, 1);
  2190. init_waitqueue_head (&fepriv->wait_queue);
  2191. init_waitqueue_head (&fepriv->events.wait_queue);
  2192. mutex_init(&fepriv->events.mtx);
  2193. fe->dvb = dvb;
  2194. fepriv->inversion = INVERSION_OFF;
  2195. printk ("DVB: registering adapter %i frontend %i (%s)...\n",
  2196. fe->dvb->num,
  2197. fe->id,
  2198. fe->ops.info.name);
  2199. dvb_register_device (fe->dvb, &fepriv->dvbdev, &dvbdev_template,
  2200. fe, DVB_DEVICE_FRONTEND);
  2201. /*
  2202. * Initialize the cache to the proper values according with the
  2203. * first supported delivery system (ops->delsys[0])
  2204. */
  2205. fe->dtv_property_cache.delivery_system = fe->ops.delsys[0];
  2206. dvb_frontend_clear_cache(fe);
  2207. mutex_unlock(&frontend_mutex);
  2208. return 0;
  2209. }
  2210. EXPORT_SYMBOL(dvb_register_frontend);
  2211. int dvb_unregister_frontend(struct dvb_frontend* fe)
  2212. {
  2213. struct dvb_frontend_private *fepriv = fe->frontend_priv;
  2214. dprintk ("%s\n", __func__);
  2215. mutex_lock(&frontend_mutex);
  2216. dvb_frontend_stop (fe);
  2217. mutex_unlock(&frontend_mutex);
  2218. if (fepriv->dvbdev->users < -1)
  2219. wait_event(fepriv->dvbdev->wait_queue,
  2220. fepriv->dvbdev->users==-1);
  2221. mutex_lock(&frontend_mutex);
  2222. dvb_unregister_device (fepriv->dvbdev);
  2223. /* fe is invalid now */
  2224. kfree(fepriv);
  2225. mutex_unlock(&frontend_mutex);
  2226. return 0;
  2227. }
  2228. EXPORT_SYMBOL(dvb_unregister_frontend);
  2229. #ifdef CONFIG_MEDIA_ATTACH
  2230. void dvb_frontend_detach(struct dvb_frontend* fe)
  2231. {
  2232. void *ptr;
  2233. if (fe->ops.release_sec) {
  2234. fe->ops.release_sec(fe);
  2235. symbol_put_addr(fe->ops.release_sec);
  2236. }
  2237. if (fe->ops.tuner_ops.release) {
  2238. fe->ops.tuner_ops.release(fe);
  2239. symbol_put_addr(fe->ops.tuner_ops.release);
  2240. }
  2241. if (fe->ops.analog_ops.release) {
  2242. fe->ops.analog_ops.release(fe);
  2243. symbol_put_addr(fe->ops.analog_ops.release);
  2244. }
  2245. ptr = (void*)fe->ops.release;
  2246. if (ptr) {
  2247. fe->ops.release(fe);
  2248. symbol_put_addr(ptr);
  2249. }
  2250. }
  2251. #else
  2252. void dvb_frontend_detach(struct dvb_frontend* fe)
  2253. {
  2254. if (fe->ops.release_sec)
  2255. fe->ops.release_sec(fe);
  2256. if (fe->ops.tuner_ops.release)
  2257. fe->ops.tuner_ops.release(fe);
  2258. if (fe->ops.analog_ops.release)
  2259. fe->ops.analog_ops.release(fe);
  2260. if (fe->ops.release)
  2261. fe->ops.release(fe);
  2262. }
  2263. #endif
  2264. EXPORT_SYMBOL(dvb_frontend_detach);