bttv-driver.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. bttv - Bt848 frame grabber driver
  4. Copyright (C) 1996,97,98 Ralph Metzler <[email protected]>
  5. & Marcus Metzler <[email protected]>
  6. (c) 1999-2002 Gerd Knorr <[email protected]>
  7. some v4l2 code lines are taken from Justin's bttv2 driver which is
  8. (c) 2000 Justin Schoeman <[email protected]>
  9. V4L1 removal from:
  10. (c) 2005-2006 Nickolay V. Shmyrev <[email protected]>
  11. Fixes to be fully V4L2 compliant by
  12. (c) 2006 Mauro Carvalho Chehab <[email protected]>
  13. Cropping and overscan support
  14. Copyright (C) 2005, 2006 Michael H. Schimek <[email protected]>
  15. Sponsored by OPQ Systems AB
  16. */
  17. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/errno.h>
  23. #include <linux/fs.h>
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/kdev_t.h>
  28. #include "bttvp.h"
  29. #include <media/v4l2-common.h>
  30. #include <media/v4l2-ioctl.h>
  31. #include <media/v4l2-event.h>
  32. #include <media/i2c/tvaudio.h>
  33. #include <media/drv-intf/msp3400.h>
  34. #include <linux/dma-mapping.h>
  35. #include <asm/io.h>
  36. #include <asm/byteorder.h>
  37. #include <media/i2c/saa6588.h>
  38. #define BTTV_VERSION "0.9.19"
  39. unsigned int bttv_num; /* number of Bt848s in use */
  40. struct bttv *bttvs[BTTV_MAX];
  41. unsigned int bttv_debug;
  42. unsigned int bttv_verbose = 1;
  43. unsigned int bttv_gpio;
  44. /* config variables */
  45. #ifdef __BIG_ENDIAN
  46. static unsigned int bigendian=1;
  47. #else
  48. static unsigned int bigendian;
  49. #endif
  50. static unsigned int radio[BTTV_MAX];
  51. static unsigned int irq_debug;
  52. static unsigned int gbuffers = 8;
  53. static unsigned int gbufsize = 0x208000;
  54. static unsigned int reset_crop = 1;
  55. static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  56. static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  57. static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  58. static int debug_latency;
  59. static int disable_ir;
  60. static unsigned int fdsr;
  61. /* options */
  62. static unsigned int combfilter;
  63. static unsigned int lumafilter;
  64. static unsigned int automute = 1;
  65. static unsigned int chroma_agc;
  66. static unsigned int agc_crush = 1;
  67. static unsigned int whitecrush_upper = 0xCF;
  68. static unsigned int whitecrush_lower = 0x7F;
  69. static unsigned int vcr_hack;
  70. static unsigned int irq_iswitch;
  71. static unsigned int uv_ratio = 50;
  72. static unsigned int full_luma_range;
  73. static unsigned int coring;
  74. /* API features (turn on/off stuff for testing) */
  75. static unsigned int v4l2 = 1;
  76. /* insmod args */
  77. module_param(bttv_verbose, int, 0644);
  78. module_param(bttv_gpio, int, 0644);
  79. module_param(bttv_debug, int, 0644);
  80. module_param(irq_debug, int, 0644);
  81. module_param(debug_latency, int, 0644);
  82. module_param(disable_ir, int, 0444);
  83. module_param(fdsr, int, 0444);
  84. module_param(gbuffers, int, 0444);
  85. module_param(gbufsize, int, 0444);
  86. module_param(reset_crop, int, 0444);
  87. module_param(v4l2, int, 0644);
  88. module_param(bigendian, int, 0644);
  89. module_param(irq_iswitch, int, 0644);
  90. module_param(combfilter, int, 0444);
  91. module_param(lumafilter, int, 0444);
  92. module_param(automute, int, 0444);
  93. module_param(chroma_agc, int, 0444);
  94. module_param(agc_crush, int, 0444);
  95. module_param(whitecrush_upper, int, 0444);
  96. module_param(whitecrush_lower, int, 0444);
  97. module_param(vcr_hack, int, 0444);
  98. module_param(uv_ratio, int, 0444);
  99. module_param(full_luma_range, int, 0444);
  100. module_param(coring, int, 0444);
  101. module_param_array(radio, int, NULL, 0444);
  102. module_param_array(video_nr, int, NULL, 0444);
  103. module_param_array(radio_nr, int, NULL, 0444);
  104. module_param_array(vbi_nr, int, NULL, 0444);
  105. MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
  106. MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
  107. MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
  108. MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
  109. MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
  110. MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
  111. MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
  112. MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
  113. MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
  114. MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default is 1 (yes) for compatibility with older applications");
  115. MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
  116. MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
  117. MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
  118. MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
  119. MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
  120. MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
  121. MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
  122. MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
  123. MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
  124. MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
  125. MODULE_PARM_DESC(video_nr, "video device numbers");
  126. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  127. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  128. MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
  129. MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
  130. MODULE_LICENSE("GPL");
  131. MODULE_VERSION(BTTV_VERSION);
  132. #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
  133. #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
  134. #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
  135. #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
  136. #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
  137. #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
  138. #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
  139. #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
  140. #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
  141. #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
  142. /* ----------------------------------------------------------------------- */
  143. /* sysfs */
  144. static ssize_t card_show(struct device *cd,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. struct video_device *vfd = to_video_device(cd);
  148. struct bttv *btv = video_get_drvdata(vfd);
  149. return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
  150. }
  151. static DEVICE_ATTR_RO(card);
  152. /* ----------------------------------------------------------------------- */
  153. /* dvb auto-load setup */
  154. #if defined(CONFIG_MODULES) && defined(MODULE)
  155. static void request_module_async(struct work_struct *work)
  156. {
  157. request_module("dvb-bt8xx");
  158. }
  159. static void request_modules(struct bttv *dev)
  160. {
  161. INIT_WORK(&dev->request_module_wk, request_module_async);
  162. schedule_work(&dev->request_module_wk);
  163. }
  164. static void flush_request_modules(struct bttv *dev)
  165. {
  166. flush_work(&dev->request_module_wk);
  167. }
  168. #else
  169. #define request_modules(dev)
  170. #define flush_request_modules(dev) do {} while(0)
  171. #endif /* CONFIG_MODULES */
  172. /* ----------------------------------------------------------------------- */
  173. /* static data */
  174. /* special timing tables from conexant... */
  175. static u8 SRAM_Table[][60] =
  176. {
  177. /* PAL digital input over GPIO[7:0] */
  178. {
  179. 45, // 45 bytes following
  180. 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
  181. 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
  182. 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
  183. 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
  184. 0x37,0x00,0xAF,0x21,0x00
  185. },
  186. /* NTSC digital input over GPIO[7:0] */
  187. {
  188. 51, // 51 bytes following
  189. 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
  190. 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
  191. 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
  192. 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
  193. 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
  194. 0x00,
  195. },
  196. // TGB_NTSC392 // quartzsight
  197. // This table has been modified to be used for Fusion Rev D
  198. {
  199. 0x2A, // size of table = 42
  200. 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
  201. 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
  202. 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
  203. 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
  204. 0x20, 0x00
  205. }
  206. };
  207. /* minhdelayx1 first video pixel we can capture on a line and
  208. hdelayx1 start of active video, both relative to rising edge of
  209. /HRESET pulse (0H) in 1 / fCLKx1.
  210. swidth width of active video and
  211. totalwidth total line width, both in 1 / fCLKx1.
  212. sqwidth total line width in square pixels.
  213. vdelay start of active video in 2 * field lines relative to
  214. trailing edge of /VRESET pulse (VDELAY register).
  215. sheight height of active video in 2 * field lines.
  216. extraheight Added to sheight for cropcap.bounds.height only
  217. videostart0 ITU-R frame line number of the line corresponding
  218. to vdelay in the first field. */
  219. #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
  220. vdelay, sheight, extraheight, videostart0) \
  221. .cropcap.bounds.left = minhdelayx1, \
  222. /* * 2 because vertically we count field lines times two, */ \
  223. /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
  224. .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
  225. /* 4 is a safety margin at the end of the line. */ \
  226. .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
  227. .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
  228. MIN_VDELAY, \
  229. .cropcap.defrect.left = hdelayx1, \
  230. .cropcap.defrect.top = (videostart0) * 2, \
  231. .cropcap.defrect.width = swidth, \
  232. .cropcap.defrect.height = sheight, \
  233. .cropcap.pixelaspect.numerator = totalwidth, \
  234. .cropcap.pixelaspect.denominator = sqwidth,
  235. const struct bttv_tvnorm bttv_tvnorms[] = {
  236. /* PAL-BDGHI */
  237. /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
  238. /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
  239. {
  240. .v4l2_id = V4L2_STD_PAL,
  241. .name = "PAL",
  242. .Fsc = 35468950,
  243. .swidth = 924,
  244. .sheight = 576,
  245. .totalwidth = 1135,
  246. .adelay = 0x7f,
  247. .bdelay = 0x72,
  248. .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
  249. .scaledtwidth = 1135,
  250. .hdelayx1 = 186,
  251. .hactivex1 = 924,
  252. .vdelay = 0x20,
  253. .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
  254. .sram = 0,
  255. /* ITU-R frame line number of the first VBI line
  256. we can capture, of the first and second field.
  257. The last line is determined by cropcap.bounds. */
  258. .vbistart = { 7, 320 },
  259. CROPCAP(/* minhdelayx1 */ 68,
  260. /* hdelayx1 */ 186,
  261. /* Should be (768 * 1135 + 944 / 2) / 944.
  262. cropcap.defrect is used for image width
  263. checks, so we keep the old value 924. */
  264. /* swidth */ 924,
  265. /* totalwidth */ 1135,
  266. /* sqwidth */ 944,
  267. /* vdelay */ 0x20,
  268. /* sheight */ 576,
  269. /* bt878 (and bt848?) can capture another
  270. line below active video. */
  271. /* extraheight */ 2,
  272. /* videostart0 */ 23)
  273. },{
  274. .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
  275. .name = "NTSC",
  276. .Fsc = 28636363,
  277. .swidth = 768,
  278. .sheight = 480,
  279. .totalwidth = 910,
  280. .adelay = 0x68,
  281. .bdelay = 0x5d,
  282. .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
  283. .scaledtwidth = 910,
  284. .hdelayx1 = 128,
  285. .hactivex1 = 910,
  286. .vdelay = 0x1a,
  287. .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
  288. .sram = 1,
  289. .vbistart = { 10, 273 },
  290. CROPCAP(/* minhdelayx1 */ 68,
  291. /* hdelayx1 */ 128,
  292. /* Should be (640 * 910 + 780 / 2) / 780? */
  293. /* swidth */ 768,
  294. /* totalwidth */ 910,
  295. /* sqwidth */ 780,
  296. /* vdelay */ 0x1a,
  297. /* sheight */ 480,
  298. /* extraheight */ 0,
  299. /* videostart0 */ 23)
  300. },{
  301. .v4l2_id = V4L2_STD_SECAM,
  302. .name = "SECAM",
  303. .Fsc = 35468950,
  304. .swidth = 924,
  305. .sheight = 576,
  306. .totalwidth = 1135,
  307. .adelay = 0x7f,
  308. .bdelay = 0xb0,
  309. .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
  310. .scaledtwidth = 1135,
  311. .hdelayx1 = 186,
  312. .hactivex1 = 922,
  313. .vdelay = 0x20,
  314. .vbipack = 255,
  315. .sram = 0, /* like PAL, correct? */
  316. .vbistart = { 7, 320 },
  317. CROPCAP(/* minhdelayx1 */ 68,
  318. /* hdelayx1 */ 186,
  319. /* swidth */ 924,
  320. /* totalwidth */ 1135,
  321. /* sqwidth */ 944,
  322. /* vdelay */ 0x20,
  323. /* sheight */ 576,
  324. /* extraheight */ 0,
  325. /* videostart0 */ 23)
  326. },{
  327. .v4l2_id = V4L2_STD_PAL_Nc,
  328. .name = "PAL-Nc",
  329. .Fsc = 28636363,
  330. .swidth = 640,
  331. .sheight = 576,
  332. .totalwidth = 910,
  333. .adelay = 0x68,
  334. .bdelay = 0x5d,
  335. .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
  336. .scaledtwidth = 780,
  337. .hdelayx1 = 130,
  338. .hactivex1 = 734,
  339. .vdelay = 0x1a,
  340. .vbipack = 144,
  341. .sram = -1,
  342. .vbistart = { 7, 320 },
  343. CROPCAP(/* minhdelayx1 */ 68,
  344. /* hdelayx1 */ 130,
  345. /* swidth */ (640 * 910 + 780 / 2) / 780,
  346. /* totalwidth */ 910,
  347. /* sqwidth */ 780,
  348. /* vdelay */ 0x1a,
  349. /* sheight */ 576,
  350. /* extraheight */ 0,
  351. /* videostart0 */ 23)
  352. },{
  353. .v4l2_id = V4L2_STD_PAL_M,
  354. .name = "PAL-M",
  355. .Fsc = 28636363,
  356. .swidth = 640,
  357. .sheight = 480,
  358. .totalwidth = 910,
  359. .adelay = 0x68,
  360. .bdelay = 0x5d,
  361. .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
  362. .scaledtwidth = 780,
  363. .hdelayx1 = 135,
  364. .hactivex1 = 754,
  365. .vdelay = 0x1a,
  366. .vbipack = 144,
  367. .sram = -1,
  368. .vbistart = { 10, 273 },
  369. CROPCAP(/* minhdelayx1 */ 68,
  370. /* hdelayx1 */ 135,
  371. /* swidth */ (640 * 910 + 780 / 2) / 780,
  372. /* totalwidth */ 910,
  373. /* sqwidth */ 780,
  374. /* vdelay */ 0x1a,
  375. /* sheight */ 480,
  376. /* extraheight */ 0,
  377. /* videostart0 */ 23)
  378. },{
  379. .v4l2_id = V4L2_STD_PAL_N,
  380. .name = "PAL-N",
  381. .Fsc = 35468950,
  382. .swidth = 768,
  383. .sheight = 576,
  384. .totalwidth = 1135,
  385. .adelay = 0x7f,
  386. .bdelay = 0x72,
  387. .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
  388. .scaledtwidth = 944,
  389. .hdelayx1 = 186,
  390. .hactivex1 = 922,
  391. .vdelay = 0x20,
  392. .vbipack = 144,
  393. .sram = -1,
  394. .vbistart = { 7, 320 },
  395. CROPCAP(/* minhdelayx1 */ 68,
  396. /* hdelayx1 */ 186,
  397. /* swidth */ (768 * 1135 + 944 / 2) / 944,
  398. /* totalwidth */ 1135,
  399. /* sqwidth */ 944,
  400. /* vdelay */ 0x20,
  401. /* sheight */ 576,
  402. /* extraheight */ 0,
  403. /* videostart0 */ 23)
  404. },{
  405. .v4l2_id = V4L2_STD_NTSC_M_JP,
  406. .name = "NTSC-JP",
  407. .Fsc = 28636363,
  408. .swidth = 640,
  409. .sheight = 480,
  410. .totalwidth = 910,
  411. .adelay = 0x68,
  412. .bdelay = 0x5d,
  413. .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
  414. .scaledtwidth = 780,
  415. .hdelayx1 = 135,
  416. .hactivex1 = 754,
  417. .vdelay = 0x16,
  418. .vbipack = 144,
  419. .sram = -1,
  420. .vbistart = { 10, 273 },
  421. CROPCAP(/* minhdelayx1 */ 68,
  422. /* hdelayx1 */ 135,
  423. /* swidth */ (640 * 910 + 780 / 2) / 780,
  424. /* totalwidth */ 910,
  425. /* sqwidth */ 780,
  426. /* vdelay */ 0x16,
  427. /* sheight */ 480,
  428. /* extraheight */ 0,
  429. /* videostart0 */ 23)
  430. },{
  431. /* that one hopefully works with the strange timing
  432. * which video recorders produce when playing a NTSC
  433. * tape on a PAL TV ... */
  434. .v4l2_id = V4L2_STD_PAL_60,
  435. .name = "PAL-60",
  436. .Fsc = 35468950,
  437. .swidth = 924,
  438. .sheight = 480,
  439. .totalwidth = 1135,
  440. .adelay = 0x7f,
  441. .bdelay = 0x72,
  442. .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
  443. .scaledtwidth = 1135,
  444. .hdelayx1 = 186,
  445. .hactivex1 = 924,
  446. .vdelay = 0x1a,
  447. .vbipack = 255,
  448. .vtotal = 524,
  449. .sram = -1,
  450. .vbistart = { 10, 273 },
  451. CROPCAP(/* minhdelayx1 */ 68,
  452. /* hdelayx1 */ 186,
  453. /* swidth */ 924,
  454. /* totalwidth */ 1135,
  455. /* sqwidth */ 944,
  456. /* vdelay */ 0x1a,
  457. /* sheight */ 480,
  458. /* extraheight */ 0,
  459. /* videostart0 */ 23)
  460. }
  461. };
  462. static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
  463. /* ----------------------------------------------------------------------- */
  464. /* bttv format list
  465. packed pixel formats must come first */
  466. static const struct bttv_format formats[] = {
  467. {
  468. .fourcc = V4L2_PIX_FMT_GREY,
  469. .btformat = BT848_COLOR_FMT_Y8,
  470. .depth = 8,
  471. .flags = FORMAT_FLAGS_PACKED,
  472. },{
  473. .fourcc = V4L2_PIX_FMT_HI240,
  474. .btformat = BT848_COLOR_FMT_RGB8,
  475. .depth = 8,
  476. .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
  477. },{
  478. .fourcc = V4L2_PIX_FMT_RGB555,
  479. .btformat = BT848_COLOR_FMT_RGB15,
  480. .depth = 16,
  481. .flags = FORMAT_FLAGS_PACKED,
  482. },{
  483. .fourcc = V4L2_PIX_FMT_RGB555X,
  484. .btformat = BT848_COLOR_FMT_RGB15,
  485. .btswap = 0x03, /* byteswap */
  486. .depth = 16,
  487. .flags = FORMAT_FLAGS_PACKED,
  488. },{
  489. .fourcc = V4L2_PIX_FMT_RGB565,
  490. .btformat = BT848_COLOR_FMT_RGB16,
  491. .depth = 16,
  492. .flags = FORMAT_FLAGS_PACKED,
  493. },{
  494. .fourcc = V4L2_PIX_FMT_RGB565X,
  495. .btformat = BT848_COLOR_FMT_RGB16,
  496. .btswap = 0x03, /* byteswap */
  497. .depth = 16,
  498. .flags = FORMAT_FLAGS_PACKED,
  499. },{
  500. .fourcc = V4L2_PIX_FMT_BGR24,
  501. .btformat = BT848_COLOR_FMT_RGB24,
  502. .depth = 24,
  503. .flags = FORMAT_FLAGS_PACKED,
  504. },{
  505. .fourcc = V4L2_PIX_FMT_BGR32,
  506. .btformat = BT848_COLOR_FMT_RGB32,
  507. .depth = 32,
  508. .flags = FORMAT_FLAGS_PACKED,
  509. },{
  510. .fourcc = V4L2_PIX_FMT_RGB32,
  511. .btformat = BT848_COLOR_FMT_RGB32,
  512. .btswap = 0x0f, /* byte+word swap */
  513. .depth = 32,
  514. .flags = FORMAT_FLAGS_PACKED,
  515. },{
  516. .fourcc = V4L2_PIX_FMT_YUYV,
  517. .btformat = BT848_COLOR_FMT_YUY2,
  518. .depth = 16,
  519. .flags = FORMAT_FLAGS_PACKED,
  520. },{
  521. .fourcc = V4L2_PIX_FMT_UYVY,
  522. .btformat = BT848_COLOR_FMT_YUY2,
  523. .btswap = 0x03, /* byteswap */
  524. .depth = 16,
  525. .flags = FORMAT_FLAGS_PACKED,
  526. },{
  527. .fourcc = V4L2_PIX_FMT_YUV422P,
  528. .btformat = BT848_COLOR_FMT_YCrCb422,
  529. .depth = 16,
  530. .flags = FORMAT_FLAGS_PLANAR,
  531. .hshift = 1,
  532. .vshift = 0,
  533. },{
  534. .fourcc = V4L2_PIX_FMT_YUV420,
  535. .btformat = BT848_COLOR_FMT_YCrCb422,
  536. .depth = 12,
  537. .flags = FORMAT_FLAGS_PLANAR,
  538. .hshift = 1,
  539. .vshift = 1,
  540. },{
  541. .fourcc = V4L2_PIX_FMT_YVU420,
  542. .btformat = BT848_COLOR_FMT_YCrCb422,
  543. .depth = 12,
  544. .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
  545. .hshift = 1,
  546. .vshift = 1,
  547. },{
  548. .fourcc = V4L2_PIX_FMT_YUV411P,
  549. .btformat = BT848_COLOR_FMT_YCrCb411,
  550. .depth = 12,
  551. .flags = FORMAT_FLAGS_PLANAR,
  552. .hshift = 2,
  553. .vshift = 0,
  554. },{
  555. .fourcc = V4L2_PIX_FMT_YUV410,
  556. .btformat = BT848_COLOR_FMT_YCrCb411,
  557. .depth = 9,
  558. .flags = FORMAT_FLAGS_PLANAR,
  559. .hshift = 2,
  560. .vshift = 2,
  561. },{
  562. .fourcc = V4L2_PIX_FMT_YVU410,
  563. .btformat = BT848_COLOR_FMT_YCrCb411,
  564. .depth = 9,
  565. .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
  566. .hshift = 2,
  567. .vshift = 2,
  568. },{
  569. .fourcc = -1,
  570. .btformat = BT848_COLOR_FMT_RAW,
  571. .depth = 8,
  572. .flags = FORMAT_FLAGS_RAW,
  573. }
  574. };
  575. static const unsigned int FORMATS = ARRAY_SIZE(formats);
  576. /* ----------------------------------------------------------------------- */
  577. /* resource management */
  578. /*
  579. RESOURCE_ allocated by freed by
  580. VIDEO_READ bttv_read 1) bttv_read 2)
  581. VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
  582. VIDIOC_QBUF 1) bttv_release
  583. VIDIOCMCAPTURE 1)
  584. OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
  585. VIDIOC_OVERLAY on VIDIOC_OVERLAY off
  586. 3) bttv_release
  587. VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
  588. VIDIOC_QBUF 1) bttv_release
  589. bttv_read, bttv_poll 1) 4)
  590. 1) The resource must be allocated when we enter buffer prepare functions
  591. and remain allocated while buffers are in the DMA queue.
  592. 2) This is a single frame read.
  593. 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
  594. RESOURCE_OVERLAY is allocated.
  595. 4) This is a continuous read, implies VIDIOC_STREAMON.
  596. Note this driver permits video input and standard changes regardless if
  597. resources are allocated.
  598. */
  599. #define VBI_RESOURCES (RESOURCE_VBI)
  600. #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
  601. RESOURCE_VIDEO_STREAM | \
  602. RESOURCE_OVERLAY)
  603. static
  604. int check_alloc_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bit)
  605. {
  606. int xbits; /* mutual exclusive resources */
  607. if (fh->resources & bit)
  608. /* have it already allocated */
  609. return 1;
  610. xbits = bit;
  611. if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
  612. xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
  613. /* is it free? */
  614. if (btv->resources & xbits) {
  615. /* no, someone else uses it */
  616. goto fail;
  617. }
  618. if ((bit & VIDEO_RESOURCES)
  619. && 0 == (btv->resources & VIDEO_RESOURCES)) {
  620. /* Do crop - use current, don't - use default parameters. */
  621. __s32 top = btv->crop[!!fh->do_crop].rect.top;
  622. if (btv->vbi_end > top)
  623. goto fail;
  624. /* We cannot capture the same line as video and VBI data.
  625. Claim scan lines crop[].rect.top to bottom. */
  626. btv->crop_start = top;
  627. } else if (bit & VBI_RESOURCES) {
  628. __s32 end = fh->vbi_fmt.end;
  629. if (end > btv->crop_start)
  630. goto fail;
  631. /* Claim scan lines above fh->vbi_fmt.end. */
  632. btv->vbi_end = end;
  633. }
  634. /* it's free, grab it */
  635. fh->resources |= bit;
  636. btv->resources |= bit;
  637. return 1;
  638. fail:
  639. return 0;
  640. }
  641. static
  642. int check_btres(struct bttv_fh *fh, int bit)
  643. {
  644. return (fh->resources & bit);
  645. }
  646. static
  647. int locked_btres(struct bttv *btv, int bit)
  648. {
  649. return (btv->resources & bit);
  650. }
  651. /* Call with btv->lock down. */
  652. static void
  653. disclaim_vbi_lines(struct bttv *btv)
  654. {
  655. btv->vbi_end = 0;
  656. }
  657. /* Call with btv->lock down. */
  658. static void
  659. disclaim_video_lines(struct bttv *btv)
  660. {
  661. const struct bttv_tvnorm *tvnorm;
  662. u8 crop;
  663. tvnorm = &bttv_tvnorms[btv->tvnorm];
  664. btv->crop_start = tvnorm->cropcap.bounds.top
  665. + tvnorm->cropcap.bounds.height;
  666. /* VBI capturing ends at VDELAY, start of video capturing, no
  667. matter how many lines the VBI RISC program expects. When video
  668. capturing is off, it shall no longer "preempt" VBI capturing,
  669. so we set VDELAY to maximum. */
  670. crop = btread(BT848_E_CROP) | 0xc0;
  671. btwrite(crop, BT848_E_CROP);
  672. btwrite(0xfe, BT848_E_VDELAY_LO);
  673. btwrite(crop, BT848_O_CROP);
  674. btwrite(0xfe, BT848_O_VDELAY_LO);
  675. }
  676. static
  677. void free_btres_lock(struct bttv *btv, struct bttv_fh *fh, int bits)
  678. {
  679. if ((fh->resources & bits) != bits) {
  680. /* trying to free resources not allocated by us ... */
  681. pr_err("BUG! (btres)\n");
  682. }
  683. fh->resources &= ~bits;
  684. btv->resources &= ~bits;
  685. bits = btv->resources;
  686. if (0 == (bits & VIDEO_RESOURCES))
  687. disclaim_video_lines(btv);
  688. if (0 == (bits & VBI_RESOURCES))
  689. disclaim_vbi_lines(btv);
  690. }
  691. /* ----------------------------------------------------------------------- */
  692. /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
  693. /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
  694. PLL_X = Reference pre-divider (0=1, 1=2)
  695. PLL_C = Post divider (0=6, 1=4)
  696. PLL_I = Integer input
  697. PLL_F = Fractional input
  698. F_input = 28.636363 MHz:
  699. PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
  700. */
  701. static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
  702. {
  703. unsigned char fl, fh, fi;
  704. /* prevent overflows */
  705. fin/=4;
  706. fout/=4;
  707. fout*=12;
  708. fi=fout/fin;
  709. fout=(fout%fin)*256;
  710. fh=fout/fin;
  711. fout=(fout%fin)*256;
  712. fl=fout/fin;
  713. btwrite(fl, BT848_PLL_F_LO);
  714. btwrite(fh, BT848_PLL_F_HI);
  715. btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
  716. }
  717. static void set_pll(struct bttv *btv)
  718. {
  719. int i;
  720. if (!btv->pll.pll_crystal)
  721. return;
  722. if (btv->pll.pll_ofreq == btv->pll.pll_current) {
  723. dprintk("%d: PLL: no change required\n", btv->c.nr);
  724. return;
  725. }
  726. if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
  727. /* no PLL needed */
  728. if (btv->pll.pll_current == 0)
  729. return;
  730. if (bttv_verbose)
  731. pr_info("%d: PLL can sleep, using XTAL (%d)\n",
  732. btv->c.nr, btv->pll.pll_ifreq);
  733. btwrite(0x00,BT848_TGCTRL);
  734. btwrite(0x00,BT848_PLL_XCI);
  735. btv->pll.pll_current = 0;
  736. return;
  737. }
  738. if (bttv_verbose)
  739. pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
  740. btv->c.nr,
  741. btv->pll.pll_ifreq, btv->pll.pll_ofreq);
  742. set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
  743. for (i=0; i<10; i++) {
  744. /* Let other people run while the PLL stabilizes */
  745. msleep(10);
  746. if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
  747. btwrite(0,BT848_DSTATUS);
  748. } else {
  749. btwrite(0x08,BT848_TGCTRL);
  750. btv->pll.pll_current = btv->pll.pll_ofreq;
  751. if (bttv_verbose)
  752. pr_info("PLL set ok\n");
  753. return;
  754. }
  755. }
  756. btv->pll.pll_current = -1;
  757. if (bttv_verbose)
  758. pr_info("Setting PLL failed\n");
  759. return;
  760. }
  761. /* used to switch between the bt848's analog/digital video capture modes */
  762. static void bt848A_set_timing(struct bttv *btv)
  763. {
  764. int i, len;
  765. int table_idx = bttv_tvnorms[btv->tvnorm].sram;
  766. int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
  767. if (btv->input == btv->dig) {
  768. dprintk("%d: load digital timing table (table_idx=%d)\n",
  769. btv->c.nr,table_idx);
  770. /* timing change...reset timing generator address */
  771. btwrite(0x00, BT848_TGCTRL);
  772. btwrite(0x02, BT848_TGCTRL);
  773. btwrite(0x00, BT848_TGCTRL);
  774. len=SRAM_Table[table_idx][0];
  775. for(i = 1; i <= len; i++)
  776. btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
  777. btv->pll.pll_ofreq = 27000000;
  778. set_pll(btv);
  779. btwrite(0x11, BT848_TGCTRL);
  780. btwrite(0x41, BT848_DVSIF);
  781. } else {
  782. btv->pll.pll_ofreq = fsc;
  783. set_pll(btv);
  784. btwrite(0x0, BT848_DVSIF);
  785. }
  786. }
  787. /* ----------------------------------------------------------------------- */
  788. static void bt848_bright(struct bttv *btv, int bright)
  789. {
  790. int value;
  791. // printk("set bright: %d\n", bright); // DEBUG
  792. btv->bright = bright;
  793. /* We want -128 to 127 we get 0-65535 */
  794. value = (bright >> 8) - 128;
  795. btwrite(value & 0xff, BT848_BRIGHT);
  796. }
  797. static void bt848_hue(struct bttv *btv, int hue)
  798. {
  799. int value;
  800. btv->hue = hue;
  801. /* -128 to 127 */
  802. value = (hue >> 8) - 128;
  803. btwrite(value & 0xff, BT848_HUE);
  804. }
  805. static void bt848_contrast(struct bttv *btv, int cont)
  806. {
  807. int value,hibit;
  808. btv->contrast = cont;
  809. /* 0-511 */
  810. value = (cont >> 7);
  811. hibit = (value >> 6) & 4;
  812. btwrite(value & 0xff, BT848_CONTRAST_LO);
  813. btaor(hibit, ~4, BT848_E_CONTROL);
  814. btaor(hibit, ~4, BT848_O_CONTROL);
  815. }
  816. static void bt848_sat(struct bttv *btv, int color)
  817. {
  818. int val_u,val_v,hibits;
  819. btv->saturation = color;
  820. /* 0-511 for the color */
  821. val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
  822. val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
  823. hibits = (val_u >> 7) & 2;
  824. hibits |= (val_v >> 8) & 1;
  825. btwrite(val_u & 0xff, BT848_SAT_U_LO);
  826. btwrite(val_v & 0xff, BT848_SAT_V_LO);
  827. btaor(hibits, ~3, BT848_E_CONTROL);
  828. btaor(hibits, ~3, BT848_O_CONTROL);
  829. }
  830. /* ----------------------------------------------------------------------- */
  831. static int
  832. video_mux(struct bttv *btv, unsigned int input)
  833. {
  834. int mux,mask2;
  835. if (input >= bttv_tvcards[btv->c.type].video_inputs)
  836. return -EINVAL;
  837. /* needed by RemoteVideo MX */
  838. mask2 = bttv_tvcards[btv->c.type].gpiomask2;
  839. if (mask2)
  840. gpio_inout(mask2,mask2);
  841. if (input == btv->svhs) {
  842. btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
  843. btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
  844. } else {
  845. btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
  846. btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
  847. }
  848. mux = bttv_muxsel(btv, input);
  849. btaor(mux<<5, ~(3<<5), BT848_IFORM);
  850. dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
  851. /* card specific hook */
  852. if(bttv_tvcards[btv->c.type].muxsel_hook)
  853. bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
  854. return 0;
  855. }
  856. static char *audio_modes[] = {
  857. "audio: tuner", "audio: radio", "audio: extern",
  858. "audio: intern", "audio: mute"
  859. };
  860. static void
  861. audio_mux_gpio(struct bttv *btv, int input, int mute)
  862. {
  863. int gpio_val, signal, mute_gpio;
  864. gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
  865. bttv_tvcards[btv->c.type].gpiomask);
  866. signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
  867. /* automute */
  868. mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
  869. && !btv->has_radio_tuner);
  870. if (mute_gpio)
  871. gpio_val = bttv_tvcards[btv->c.type].gpiomute;
  872. else
  873. gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
  874. switch (btv->c.type) {
  875. case BTTV_BOARD_VOODOOTV_FM:
  876. case BTTV_BOARD_VOODOOTV_200:
  877. gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
  878. break;
  879. default:
  880. gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
  881. }
  882. if (bttv_gpio)
  883. bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
  884. }
  885. static int
  886. audio_mute(struct bttv *btv, int mute)
  887. {
  888. struct v4l2_ctrl *ctrl;
  889. audio_mux_gpio(btv, btv->audio_input, mute);
  890. if (btv->sd_msp34xx) {
  891. ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  892. if (ctrl)
  893. v4l2_ctrl_s_ctrl(ctrl, mute);
  894. }
  895. if (btv->sd_tvaudio) {
  896. ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  897. if (ctrl)
  898. v4l2_ctrl_s_ctrl(ctrl, mute);
  899. }
  900. if (btv->sd_tda7432) {
  901. ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
  902. if (ctrl)
  903. v4l2_ctrl_s_ctrl(ctrl, mute);
  904. }
  905. return 0;
  906. }
  907. static int
  908. audio_input(struct bttv *btv, int input)
  909. {
  910. audio_mux_gpio(btv, input, btv->mute);
  911. if (btv->sd_msp34xx) {
  912. u32 in;
  913. /* Note: the inputs tuner/radio/extern/intern are translated
  914. to msp routings. This assumes common behavior for all msp3400
  915. based TV cards. When this assumption fails, then the
  916. specific MSP routing must be added to the card table.
  917. For now this is sufficient. */
  918. switch (input) {
  919. case TVAUDIO_INPUT_RADIO:
  920. /* Some boards need the msp do to the radio demod */
  921. if (btv->radio_uses_msp_demodulator) {
  922. in = MSP_INPUT_DEFAULT;
  923. break;
  924. }
  925. in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
  926. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  927. break;
  928. case TVAUDIO_INPUT_EXTERN:
  929. in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
  930. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  931. break;
  932. case TVAUDIO_INPUT_INTERN:
  933. /* Yes, this is the same input as for RADIO. I doubt
  934. if this is ever used. The only board with an INTERN
  935. input is the BTTV_BOARD_AVERMEDIA98. I wonder how
  936. that was tested. My guess is that the whole INTERN
  937. input does not work. */
  938. in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
  939. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  940. break;
  941. case TVAUDIO_INPUT_TUNER:
  942. default:
  943. /* This is the only card that uses TUNER2, and afaik,
  944. is the only difference between the VOODOOTV_FM
  945. and VOODOOTV_200 */
  946. if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
  947. in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
  948. MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
  949. else
  950. in = MSP_INPUT_DEFAULT;
  951. break;
  952. }
  953. v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
  954. in, MSP_OUTPUT_DEFAULT, 0);
  955. }
  956. if (btv->sd_tvaudio) {
  957. v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
  958. input, 0, 0);
  959. }
  960. return 0;
  961. }
  962. static void
  963. bttv_crop_calc_limits(struct bttv_crop *c)
  964. {
  965. /* Scale factor min. 1:1, max. 16:1. Min. image size
  966. 48 x 32. Scaled width must be a multiple of 4. */
  967. if (1) {
  968. /* For bug compatibility with VIDIOCGCAP and image
  969. size checks in earlier driver versions. */
  970. c->min_scaled_width = 48;
  971. c->min_scaled_height = 32;
  972. } else {
  973. c->min_scaled_width =
  974. (max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
  975. c->min_scaled_height =
  976. max_t(unsigned int, 32, c->rect.height >> 4);
  977. }
  978. c->max_scaled_width = c->rect.width & ~3;
  979. c->max_scaled_height = c->rect.height;
  980. }
  981. static void
  982. bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
  983. {
  984. c->rect = bttv_tvnorms[norm].cropcap.defrect;
  985. bttv_crop_calc_limits(c);
  986. }
  987. /* Call with btv->lock down. */
  988. static int
  989. set_tvnorm(struct bttv *btv, unsigned int norm)
  990. {
  991. const struct bttv_tvnorm *tvnorm;
  992. v4l2_std_id id;
  993. BUG_ON(norm >= BTTV_TVNORMS);
  994. BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
  995. tvnorm = &bttv_tvnorms[norm];
  996. if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
  997. sizeof (tvnorm->cropcap))) {
  998. bttv_crop_reset(&btv->crop[0], norm);
  999. btv->crop[1] = btv->crop[0]; /* current = default */
  1000. if (0 == (btv->resources & VIDEO_RESOURCES)) {
  1001. btv->crop_start = tvnorm->cropcap.bounds.top
  1002. + tvnorm->cropcap.bounds.height;
  1003. }
  1004. }
  1005. btv->tvnorm = norm;
  1006. btwrite(tvnorm->adelay, BT848_ADELAY);
  1007. btwrite(tvnorm->bdelay, BT848_BDELAY);
  1008. btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
  1009. BT848_IFORM);
  1010. btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
  1011. btwrite(1, BT848_VBI_PACK_DEL);
  1012. bt848A_set_timing(btv);
  1013. switch (btv->c.type) {
  1014. case BTTV_BOARD_VOODOOTV_FM:
  1015. case BTTV_BOARD_VOODOOTV_200:
  1016. bttv_tda9880_setnorm(btv, gpio_read());
  1017. break;
  1018. }
  1019. id = tvnorm->v4l2_id;
  1020. bttv_call_all(btv, video, s_std, id);
  1021. return 0;
  1022. }
  1023. /* Call with btv->lock down. */
  1024. static void
  1025. set_input(struct bttv *btv, unsigned int input, unsigned int norm)
  1026. {
  1027. unsigned long flags;
  1028. btv->input = input;
  1029. if (irq_iswitch) {
  1030. spin_lock_irqsave(&btv->s_lock,flags);
  1031. if (btv->curr.frame_irq) {
  1032. /* active capture -> delayed input switch */
  1033. btv->new_input = input;
  1034. } else {
  1035. video_mux(btv,input);
  1036. }
  1037. spin_unlock_irqrestore(&btv->s_lock,flags);
  1038. } else {
  1039. video_mux(btv,input);
  1040. }
  1041. btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
  1042. TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
  1043. audio_input(btv, btv->audio_input);
  1044. set_tvnorm(btv, norm);
  1045. }
  1046. static void init_irqreg(struct bttv *btv)
  1047. {
  1048. /* clear status */
  1049. btwrite(0xfffffUL, BT848_INT_STAT);
  1050. if (bttv_tvcards[btv->c.type].no_video) {
  1051. /* i2c only */
  1052. btwrite(BT848_INT_I2CDONE,
  1053. BT848_INT_MASK);
  1054. } else {
  1055. /* full video */
  1056. btwrite((btv->triton1) |
  1057. (btv->gpioirq ? BT848_INT_GPINT : 0) |
  1058. BT848_INT_SCERR |
  1059. (fdsr ? BT848_INT_FDSR : 0) |
  1060. BT848_INT_RISCI | BT848_INT_OCERR |
  1061. BT848_INT_FMTCHG|BT848_INT_HLOCK|
  1062. BT848_INT_I2CDONE,
  1063. BT848_INT_MASK);
  1064. }
  1065. }
  1066. static void init_bt848(struct bttv *btv)
  1067. {
  1068. if (bttv_tvcards[btv->c.type].no_video) {
  1069. /* very basic init only */
  1070. init_irqreg(btv);
  1071. return;
  1072. }
  1073. btwrite(0x00, BT848_CAP_CTL);
  1074. btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
  1075. btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
  1076. /* set planar and packed mode trigger points and */
  1077. /* set rising edge of inverted GPINTR pin as irq trigger */
  1078. btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
  1079. BT848_GPIO_DMA_CTL_PLTP1_16|
  1080. BT848_GPIO_DMA_CTL_PLTP23_16|
  1081. BT848_GPIO_DMA_CTL_GPINTC|
  1082. BT848_GPIO_DMA_CTL_GPINTI,
  1083. BT848_GPIO_DMA_CTL);
  1084. btwrite(0x20, BT848_E_VSCALE_HI);
  1085. btwrite(0x20, BT848_O_VSCALE_HI);
  1086. v4l2_ctrl_handler_setup(&btv->ctrl_handler);
  1087. /* interrupt */
  1088. init_irqreg(btv);
  1089. }
  1090. static void bttv_reinit_bt848(struct bttv *btv)
  1091. {
  1092. unsigned long flags;
  1093. if (bttv_verbose)
  1094. pr_info("%d: reset, reinitialize\n", btv->c.nr);
  1095. spin_lock_irqsave(&btv->s_lock,flags);
  1096. btv->errors=0;
  1097. bttv_set_dma(btv,0);
  1098. spin_unlock_irqrestore(&btv->s_lock,flags);
  1099. init_bt848(btv);
  1100. btv->pll.pll_current = -1;
  1101. set_input(btv, btv->input, btv->tvnorm);
  1102. }
  1103. static int bttv_s_ctrl(struct v4l2_ctrl *c)
  1104. {
  1105. struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
  1106. int val;
  1107. switch (c->id) {
  1108. case V4L2_CID_BRIGHTNESS:
  1109. bt848_bright(btv, c->val);
  1110. break;
  1111. case V4L2_CID_HUE:
  1112. bt848_hue(btv, c->val);
  1113. break;
  1114. case V4L2_CID_CONTRAST:
  1115. bt848_contrast(btv, c->val);
  1116. break;
  1117. case V4L2_CID_SATURATION:
  1118. bt848_sat(btv, c->val);
  1119. break;
  1120. case V4L2_CID_COLOR_KILLER:
  1121. if (c->val) {
  1122. btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
  1123. btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
  1124. } else {
  1125. btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
  1126. btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
  1127. }
  1128. break;
  1129. case V4L2_CID_AUDIO_MUTE:
  1130. audio_mute(btv, c->val);
  1131. btv->mute = c->val;
  1132. break;
  1133. case V4L2_CID_AUDIO_VOLUME:
  1134. btv->volume_gpio(btv, c->val);
  1135. break;
  1136. case V4L2_CID_CHROMA_AGC:
  1137. val = c->val ? BT848_SCLOOP_CAGC : 0;
  1138. btwrite(val, BT848_E_SCLOOP);
  1139. btwrite(val, BT848_O_SCLOOP);
  1140. break;
  1141. case V4L2_CID_PRIVATE_COMBFILTER:
  1142. btv->opt_combfilter = c->val;
  1143. break;
  1144. case V4L2_CID_PRIVATE_LUMAFILTER:
  1145. if (c->val) {
  1146. btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
  1147. btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
  1148. } else {
  1149. btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
  1150. btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
  1151. }
  1152. break;
  1153. case V4L2_CID_PRIVATE_AUTOMUTE:
  1154. btv->opt_automute = c->val;
  1155. break;
  1156. case V4L2_CID_PRIVATE_AGC_CRUSH:
  1157. btwrite(BT848_ADC_RESERVED |
  1158. (c->val ? BT848_ADC_CRUSH : 0),
  1159. BT848_ADC);
  1160. break;
  1161. case V4L2_CID_PRIVATE_VCR_HACK:
  1162. btv->opt_vcr_hack = c->val;
  1163. break;
  1164. case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
  1165. btwrite(c->val, BT848_WC_UP);
  1166. break;
  1167. case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
  1168. btwrite(c->val, BT848_WC_DOWN);
  1169. break;
  1170. case V4L2_CID_PRIVATE_UV_RATIO:
  1171. btv->opt_uv_ratio = c->val;
  1172. bt848_sat(btv, btv->saturation);
  1173. break;
  1174. case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
  1175. btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
  1176. break;
  1177. case V4L2_CID_PRIVATE_CORING:
  1178. btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
  1179. break;
  1180. default:
  1181. return -EINVAL;
  1182. }
  1183. return 0;
  1184. }
  1185. /* ----------------------------------------------------------------------- */
  1186. static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
  1187. .s_ctrl = bttv_s_ctrl,
  1188. };
  1189. static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
  1190. .ops = &bttv_ctrl_ops,
  1191. .id = V4L2_CID_PRIVATE_COMBFILTER,
  1192. .name = "Comb Filter",
  1193. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1194. .min = 0,
  1195. .max = 1,
  1196. .step = 1,
  1197. .def = 1,
  1198. };
  1199. static struct v4l2_ctrl_config bttv_ctrl_automute = {
  1200. .ops = &bttv_ctrl_ops,
  1201. .id = V4L2_CID_PRIVATE_AUTOMUTE,
  1202. .name = "Auto Mute",
  1203. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1204. .min = 0,
  1205. .max = 1,
  1206. .step = 1,
  1207. .def = 1,
  1208. };
  1209. static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
  1210. .ops = &bttv_ctrl_ops,
  1211. .id = V4L2_CID_PRIVATE_LUMAFILTER,
  1212. .name = "Luma Decimation Filter",
  1213. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1214. .min = 0,
  1215. .max = 1,
  1216. .step = 1,
  1217. .def = 1,
  1218. };
  1219. static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
  1220. .ops = &bttv_ctrl_ops,
  1221. .id = V4L2_CID_PRIVATE_AGC_CRUSH,
  1222. .name = "AGC Crush",
  1223. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1224. .min = 0,
  1225. .max = 1,
  1226. .step = 1,
  1227. .def = 1,
  1228. };
  1229. static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
  1230. .ops = &bttv_ctrl_ops,
  1231. .id = V4L2_CID_PRIVATE_VCR_HACK,
  1232. .name = "VCR Hack",
  1233. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1234. .min = 0,
  1235. .max = 1,
  1236. .step = 1,
  1237. .def = 1,
  1238. };
  1239. static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
  1240. .ops = &bttv_ctrl_ops,
  1241. .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
  1242. .name = "Whitecrush Lower",
  1243. .type = V4L2_CTRL_TYPE_INTEGER,
  1244. .min = 0,
  1245. .max = 255,
  1246. .step = 1,
  1247. .def = 0x7f,
  1248. };
  1249. static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
  1250. .ops = &bttv_ctrl_ops,
  1251. .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
  1252. .name = "Whitecrush Upper",
  1253. .type = V4L2_CTRL_TYPE_INTEGER,
  1254. .min = 0,
  1255. .max = 255,
  1256. .step = 1,
  1257. .def = 0xcf,
  1258. };
  1259. static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
  1260. .ops = &bttv_ctrl_ops,
  1261. .id = V4L2_CID_PRIVATE_UV_RATIO,
  1262. .name = "UV Ratio",
  1263. .type = V4L2_CTRL_TYPE_INTEGER,
  1264. .min = 0,
  1265. .max = 100,
  1266. .step = 1,
  1267. .def = 50,
  1268. };
  1269. static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
  1270. .ops = &bttv_ctrl_ops,
  1271. .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
  1272. .name = "Full Luma Range",
  1273. .type = V4L2_CTRL_TYPE_BOOLEAN,
  1274. .min = 0,
  1275. .max = 1,
  1276. .step = 1,
  1277. };
  1278. static struct v4l2_ctrl_config bttv_ctrl_coring = {
  1279. .ops = &bttv_ctrl_ops,
  1280. .id = V4L2_CID_PRIVATE_CORING,
  1281. .name = "Coring",
  1282. .type = V4L2_CTRL_TYPE_INTEGER,
  1283. .min = 0,
  1284. .max = 3,
  1285. .step = 1,
  1286. };
  1287. /* ----------------------------------------------------------------------- */
  1288. void bttv_gpio_tracking(struct bttv *btv, char *comment)
  1289. {
  1290. unsigned int outbits, data;
  1291. outbits = btread(BT848_GPIO_OUT_EN);
  1292. data = btread(BT848_GPIO_DATA);
  1293. pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
  1294. btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
  1295. }
  1296. static void bttv_field_count(struct bttv *btv)
  1297. {
  1298. int need_count = 0;
  1299. if (btv->users)
  1300. need_count++;
  1301. if (need_count) {
  1302. /* start field counter */
  1303. btor(BT848_INT_VSYNC,BT848_INT_MASK);
  1304. } else {
  1305. /* stop field counter */
  1306. btand(~BT848_INT_VSYNC,BT848_INT_MASK);
  1307. btv->field_count = 0;
  1308. }
  1309. }
  1310. static const struct bttv_format*
  1311. format_by_fourcc(int fourcc)
  1312. {
  1313. unsigned int i;
  1314. for (i = 0; i < FORMATS; i++) {
  1315. if (-1 == formats[i].fourcc)
  1316. continue;
  1317. if (formats[i].fourcc == fourcc)
  1318. return formats+i;
  1319. }
  1320. return NULL;
  1321. }
  1322. /* ----------------------------------------------------------------------- */
  1323. /* misc helpers */
  1324. static int
  1325. bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
  1326. struct bttv_buffer *new)
  1327. {
  1328. struct bttv_buffer *old;
  1329. unsigned long flags;
  1330. dprintk("switch_overlay: enter [new=%p]\n", new);
  1331. if (new)
  1332. new->vb.state = VIDEOBUF_DONE;
  1333. spin_lock_irqsave(&btv->s_lock,flags);
  1334. old = btv->screen;
  1335. btv->screen = new;
  1336. btv->loop_irq |= 1;
  1337. bttv_set_dma(btv, 0x03);
  1338. spin_unlock_irqrestore(&btv->s_lock,flags);
  1339. if (NULL != old) {
  1340. dprintk("switch_overlay: old=%p state is %d\n",
  1341. old, old->vb.state);
  1342. bttv_dma_free(&fh->cap,btv, old);
  1343. kfree(old);
  1344. }
  1345. if (NULL == new)
  1346. free_btres_lock(btv,fh,RESOURCE_OVERLAY);
  1347. dprintk("switch_overlay: done\n");
  1348. return 0;
  1349. }
  1350. /* ----------------------------------------------------------------------- */
  1351. /* video4linux (1) interface */
  1352. static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
  1353. struct bttv_buffer *buf,
  1354. const struct bttv_format *fmt,
  1355. unsigned int width, unsigned int height,
  1356. enum v4l2_field field)
  1357. {
  1358. struct bttv_fh *fh = q->priv_data;
  1359. int redo_dma_risc = 0;
  1360. struct bttv_crop c;
  1361. int norm;
  1362. int rc;
  1363. /* check settings */
  1364. if (NULL == fmt)
  1365. return -EINVAL;
  1366. if (fmt->btformat == BT848_COLOR_FMT_RAW) {
  1367. width = RAW_BPL;
  1368. height = RAW_LINES*2;
  1369. if (width*height > buf->vb.bsize)
  1370. return -EINVAL;
  1371. buf->vb.size = buf->vb.bsize;
  1372. /* Make sure tvnorm and vbi_end remain consistent
  1373. until we're done. */
  1374. norm = btv->tvnorm;
  1375. /* In this mode capturing always starts at defrect.top
  1376. (default VDELAY), ignoring cropping parameters. */
  1377. if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
  1378. return -EINVAL;
  1379. }
  1380. c.rect = bttv_tvnorms[norm].cropcap.defrect;
  1381. } else {
  1382. norm = btv->tvnorm;
  1383. c = btv->crop[!!fh->do_crop];
  1384. if (width < c.min_scaled_width ||
  1385. width > c.max_scaled_width ||
  1386. height < c.min_scaled_height)
  1387. return -EINVAL;
  1388. switch (field) {
  1389. case V4L2_FIELD_TOP:
  1390. case V4L2_FIELD_BOTTOM:
  1391. case V4L2_FIELD_ALTERNATE:
  1392. /* btv->crop counts frame lines. Max. scale
  1393. factor is 16:1 for frames, 8:1 for fields. */
  1394. if (height * 2 > c.max_scaled_height)
  1395. return -EINVAL;
  1396. break;
  1397. default:
  1398. if (height > c.max_scaled_height)
  1399. return -EINVAL;
  1400. break;
  1401. }
  1402. buf->vb.size = (width * height * fmt->depth) >> 3;
  1403. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  1404. return -EINVAL;
  1405. }
  1406. /* alloc + fill struct bttv_buffer (if changed) */
  1407. if (buf->vb.width != width || buf->vb.height != height ||
  1408. buf->vb.field != field ||
  1409. buf->tvnorm != norm || buf->fmt != fmt ||
  1410. buf->crop.top != c.rect.top ||
  1411. buf->crop.left != c.rect.left ||
  1412. buf->crop.width != c.rect.width ||
  1413. buf->crop.height != c.rect.height) {
  1414. buf->vb.width = width;
  1415. buf->vb.height = height;
  1416. buf->vb.field = field;
  1417. buf->tvnorm = norm;
  1418. buf->fmt = fmt;
  1419. buf->crop = c.rect;
  1420. redo_dma_risc = 1;
  1421. }
  1422. /* alloc risc memory */
  1423. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  1424. redo_dma_risc = 1;
  1425. if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
  1426. goto fail;
  1427. }
  1428. if (redo_dma_risc)
  1429. if (0 != (rc = bttv_buffer_risc(btv,buf)))
  1430. goto fail;
  1431. buf->vb.state = VIDEOBUF_PREPARED;
  1432. return 0;
  1433. fail:
  1434. bttv_dma_free(q,btv,buf);
  1435. return rc;
  1436. }
  1437. static int
  1438. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  1439. {
  1440. struct bttv_fh *fh = q->priv_data;
  1441. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  1442. if (0 == *count)
  1443. *count = gbuffers;
  1444. if (*size * *count > gbuffers * gbufsize)
  1445. *count = (gbuffers * gbufsize) / *size;
  1446. return 0;
  1447. }
  1448. static int
  1449. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  1450. enum v4l2_field field)
  1451. {
  1452. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1453. struct bttv_fh *fh = q->priv_data;
  1454. return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
  1455. fh->width, fh->height, field);
  1456. }
  1457. static void
  1458. buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1459. {
  1460. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1461. struct bttv_fh *fh = q->priv_data;
  1462. struct bttv *btv = fh->btv;
  1463. buf->vb.state = VIDEOBUF_QUEUED;
  1464. list_add_tail(&buf->vb.queue,&btv->capture);
  1465. if (!btv->curr.frame_irq) {
  1466. btv->loop_irq |= 1;
  1467. bttv_set_dma(btv, 0x03);
  1468. }
  1469. }
  1470. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1471. {
  1472. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1473. struct bttv_fh *fh = q->priv_data;
  1474. bttv_dma_free(q,fh->btv,buf);
  1475. }
  1476. static const struct videobuf_queue_ops bttv_video_qops = {
  1477. .buf_setup = buffer_setup,
  1478. .buf_prepare = buffer_prepare,
  1479. .buf_queue = buffer_queue,
  1480. .buf_release = buffer_release,
  1481. };
  1482. static void radio_enable(struct bttv *btv)
  1483. {
  1484. /* Switch to the radio tuner */
  1485. if (!btv->has_radio_tuner) {
  1486. btv->has_radio_tuner = 1;
  1487. bttv_call_all(btv, tuner, s_radio);
  1488. btv->audio_input = TVAUDIO_INPUT_RADIO;
  1489. audio_input(btv, btv->audio_input);
  1490. }
  1491. }
  1492. static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
  1493. {
  1494. struct bttv_fh *fh = priv;
  1495. struct bttv *btv = fh->btv;
  1496. unsigned int i;
  1497. for (i = 0; i < BTTV_TVNORMS; i++)
  1498. if (id & bttv_tvnorms[i].v4l2_id)
  1499. break;
  1500. if (i == BTTV_TVNORMS)
  1501. return -EINVAL;
  1502. btv->std = id;
  1503. set_tvnorm(btv, i);
  1504. return 0;
  1505. }
  1506. static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
  1507. {
  1508. struct bttv_fh *fh = priv;
  1509. struct bttv *btv = fh->btv;
  1510. *id = btv->std;
  1511. return 0;
  1512. }
  1513. static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
  1514. {
  1515. struct bttv_fh *fh = f;
  1516. struct bttv *btv = fh->btv;
  1517. if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
  1518. *id &= V4L2_STD_625_50;
  1519. else
  1520. *id &= V4L2_STD_525_60;
  1521. return 0;
  1522. }
  1523. static int bttv_enum_input(struct file *file, void *priv,
  1524. struct v4l2_input *i)
  1525. {
  1526. struct bttv_fh *fh = priv;
  1527. struct bttv *btv = fh->btv;
  1528. if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
  1529. return -EINVAL;
  1530. i->type = V4L2_INPUT_TYPE_CAMERA;
  1531. i->audioset = 0;
  1532. if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
  1533. sprintf(i->name, "Television");
  1534. i->type = V4L2_INPUT_TYPE_TUNER;
  1535. i->tuner = 0;
  1536. } else if (i->index == btv->svhs) {
  1537. sprintf(i->name, "S-Video");
  1538. } else {
  1539. sprintf(i->name, "Composite%d", i->index);
  1540. }
  1541. if (i->index == btv->input) {
  1542. __u32 dstatus = btread(BT848_DSTATUS);
  1543. if (0 == (dstatus & BT848_DSTATUS_PRES))
  1544. i->status |= V4L2_IN_ST_NO_SIGNAL;
  1545. if (0 == (dstatus & BT848_DSTATUS_HLOC))
  1546. i->status |= V4L2_IN_ST_NO_H_LOCK;
  1547. }
  1548. i->std = BTTV_NORMS;
  1549. return 0;
  1550. }
  1551. static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
  1552. {
  1553. struct bttv_fh *fh = priv;
  1554. struct bttv *btv = fh->btv;
  1555. *i = btv->input;
  1556. return 0;
  1557. }
  1558. static int bttv_s_input(struct file *file, void *priv, unsigned int i)
  1559. {
  1560. struct bttv_fh *fh = priv;
  1561. struct bttv *btv = fh->btv;
  1562. if (i >= bttv_tvcards[btv->c.type].video_inputs)
  1563. return -EINVAL;
  1564. set_input(btv, i, btv->tvnorm);
  1565. return 0;
  1566. }
  1567. static int bttv_s_tuner(struct file *file, void *priv,
  1568. const struct v4l2_tuner *t)
  1569. {
  1570. struct bttv_fh *fh = priv;
  1571. struct bttv *btv = fh->btv;
  1572. if (t->index)
  1573. return -EINVAL;
  1574. bttv_call_all(btv, tuner, s_tuner, t);
  1575. if (btv->audio_mode_gpio) {
  1576. struct v4l2_tuner copy = *t;
  1577. btv->audio_mode_gpio(btv, &copy, 1);
  1578. }
  1579. return 0;
  1580. }
  1581. static int bttv_g_frequency(struct file *file, void *priv,
  1582. struct v4l2_frequency *f)
  1583. {
  1584. struct bttv_fh *fh = priv;
  1585. struct bttv *btv = fh->btv;
  1586. if (f->tuner)
  1587. return -EINVAL;
  1588. if (f->type == V4L2_TUNER_RADIO)
  1589. radio_enable(btv);
  1590. f->frequency = f->type == V4L2_TUNER_RADIO ?
  1591. btv->radio_freq : btv->tv_freq;
  1592. return 0;
  1593. }
  1594. static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
  1595. {
  1596. struct v4l2_frequency new_freq = *f;
  1597. bttv_call_all(btv, tuner, s_frequency, f);
  1598. /* s_frequency may clamp the frequency, so get the actual
  1599. frequency before assigning radio/tv_freq. */
  1600. bttv_call_all(btv, tuner, g_frequency, &new_freq);
  1601. if (new_freq.type == V4L2_TUNER_RADIO) {
  1602. radio_enable(btv);
  1603. btv->radio_freq = new_freq.frequency;
  1604. if (btv->has_tea575x) {
  1605. btv->tea.freq = btv->radio_freq;
  1606. snd_tea575x_set_freq(&btv->tea);
  1607. }
  1608. } else {
  1609. btv->tv_freq = new_freq.frequency;
  1610. }
  1611. }
  1612. static int bttv_s_frequency(struct file *file, void *priv,
  1613. const struct v4l2_frequency *f)
  1614. {
  1615. struct bttv_fh *fh = priv;
  1616. struct bttv *btv = fh->btv;
  1617. if (f->tuner)
  1618. return -EINVAL;
  1619. bttv_set_frequency(btv, f);
  1620. return 0;
  1621. }
  1622. static int bttv_log_status(struct file *file, void *f)
  1623. {
  1624. struct video_device *vdev = video_devdata(file);
  1625. struct bttv_fh *fh = f;
  1626. struct bttv *btv = fh->btv;
  1627. v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
  1628. bttv_call_all(btv, core, log_status);
  1629. return 0;
  1630. }
  1631. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1632. static int bttv_g_register(struct file *file, void *f,
  1633. struct v4l2_dbg_register *reg)
  1634. {
  1635. struct bttv_fh *fh = f;
  1636. struct bttv *btv = fh->btv;
  1637. /* bt848 has a 12-bit register space */
  1638. reg->reg &= 0xfff;
  1639. reg->val = btread(reg->reg);
  1640. reg->size = 1;
  1641. return 0;
  1642. }
  1643. static int bttv_s_register(struct file *file, void *f,
  1644. const struct v4l2_dbg_register *reg)
  1645. {
  1646. struct bttv_fh *fh = f;
  1647. struct bttv *btv = fh->btv;
  1648. /* bt848 has a 12-bit register space */
  1649. btwrite(reg->val, reg->reg & 0xfff);
  1650. return 0;
  1651. }
  1652. #endif
  1653. /* Given cropping boundaries b and the scaled width and height of a
  1654. single field or frame, which must not exceed hardware limits, this
  1655. function adjusts the cropping parameters c. */
  1656. static void
  1657. bttv_crop_adjust (struct bttv_crop * c,
  1658. const struct v4l2_rect * b,
  1659. __s32 width,
  1660. __s32 height,
  1661. enum v4l2_field field)
  1662. {
  1663. __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
  1664. __s32 max_left;
  1665. __s32 max_top;
  1666. if (width < c->min_scaled_width) {
  1667. /* Max. hor. scale factor 16:1. */
  1668. c->rect.width = width * 16;
  1669. } else if (width > c->max_scaled_width) {
  1670. /* Min. hor. scale factor 1:1. */
  1671. c->rect.width = width;
  1672. max_left = b->left + b->width - width;
  1673. max_left = min(max_left, (__s32) MAX_HDELAY);
  1674. if (c->rect.left > max_left)
  1675. c->rect.left = max_left;
  1676. }
  1677. if (height < c->min_scaled_height) {
  1678. /* Max. vert. scale factor 16:1, single fields 8:1. */
  1679. c->rect.height = height * 16;
  1680. } else if (frame_height > c->max_scaled_height) {
  1681. /* Min. vert. scale factor 1:1.
  1682. Top and height count field lines times two. */
  1683. c->rect.height = (frame_height + 1) & ~1;
  1684. max_top = b->top + b->height - c->rect.height;
  1685. if (c->rect.top > max_top)
  1686. c->rect.top = max_top;
  1687. }
  1688. bttv_crop_calc_limits(c);
  1689. }
  1690. /* Returns an error if scaling to a frame or single field with the given
  1691. width and height is not possible with the current cropping parameters
  1692. and width aligned according to width_mask. If adjust_size is TRUE the
  1693. function may adjust the width and/or height instead, rounding width
  1694. to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
  1695. also adjust the current cropping parameters to get closer to the
  1696. desired image size. */
  1697. static int
  1698. limit_scaled_size_lock (struct bttv_fh * fh,
  1699. __s32 * width,
  1700. __s32 * height,
  1701. enum v4l2_field field,
  1702. unsigned int width_mask,
  1703. unsigned int width_bias,
  1704. int adjust_size,
  1705. int adjust_crop)
  1706. {
  1707. struct bttv *btv = fh->btv;
  1708. const struct v4l2_rect *b;
  1709. struct bttv_crop *c;
  1710. __s32 min_width;
  1711. __s32 min_height;
  1712. __s32 max_width;
  1713. __s32 max_height;
  1714. int rc;
  1715. BUG_ON((int) width_mask >= 0 ||
  1716. width_bias >= (unsigned int) -width_mask);
  1717. /* Make sure tvnorm, vbi_end and the current cropping parameters
  1718. remain consistent until we're done. */
  1719. b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
  1720. /* Do crop - use current, don't - use default parameters. */
  1721. c = &btv->crop[!!fh->do_crop];
  1722. if (fh->do_crop
  1723. && adjust_size
  1724. && adjust_crop
  1725. && !locked_btres(btv, VIDEO_RESOURCES)) {
  1726. min_width = 48;
  1727. min_height = 32;
  1728. /* We cannot scale up. When the scaled image is larger
  1729. than crop.rect we adjust the crop.rect as required
  1730. by the V4L2 spec, hence cropcap.bounds are our limit. */
  1731. max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
  1732. max_height = b->height;
  1733. /* We cannot capture the same line as video and VBI data.
  1734. Note btv->vbi_end is really a minimum, see
  1735. bttv_vbi_try_fmt(). */
  1736. if (btv->vbi_end > b->top) {
  1737. max_height -= btv->vbi_end - b->top;
  1738. rc = -EBUSY;
  1739. if (min_height > max_height)
  1740. goto fail;
  1741. }
  1742. } else {
  1743. rc = -EBUSY;
  1744. if (btv->vbi_end > c->rect.top)
  1745. goto fail;
  1746. min_width = c->min_scaled_width;
  1747. min_height = c->min_scaled_height;
  1748. max_width = c->max_scaled_width;
  1749. max_height = c->max_scaled_height;
  1750. adjust_crop = 0;
  1751. }
  1752. min_width = (min_width - width_mask - 1) & width_mask;
  1753. max_width = max_width & width_mask;
  1754. /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
  1755. /* Min. scale factor is 1:1. */
  1756. max_height >>= !V4L2_FIELD_HAS_BOTH(field);
  1757. if (adjust_size) {
  1758. *width = clamp(*width, min_width, max_width);
  1759. *height = clamp(*height, min_height, max_height);
  1760. /* Round after clamping to avoid overflow. */
  1761. *width = (*width + width_bias) & width_mask;
  1762. if (adjust_crop) {
  1763. bttv_crop_adjust(c, b, *width, *height, field);
  1764. if (btv->vbi_end > c->rect.top) {
  1765. /* Move the crop window out of the way. */
  1766. c->rect.top = btv->vbi_end;
  1767. }
  1768. }
  1769. } else {
  1770. rc = -EINVAL;
  1771. if (*width < min_width ||
  1772. *height < min_height ||
  1773. *width > max_width ||
  1774. *height > max_height ||
  1775. 0 != (*width & ~width_mask))
  1776. goto fail;
  1777. }
  1778. rc = 0; /* success */
  1779. fail:
  1780. return rc;
  1781. }
  1782. /* Returns an error if the given overlay window dimensions are not
  1783. possible with the current cropping parameters. If adjust_size is
  1784. TRUE the function may adjust the window width and/or height
  1785. instead, however it always rounds the horizontal position and
  1786. width as btcx_align() does. If adjust_crop is TRUE the function
  1787. may also adjust the current cropping parameters to get closer
  1788. to the desired window size. */
  1789. static int
  1790. verify_window_lock(struct bttv_fh *fh, struct v4l2_window *win,
  1791. int adjust_size, int adjust_crop)
  1792. {
  1793. enum v4l2_field field;
  1794. unsigned int width_mask;
  1795. if (win->w.width < 48)
  1796. win->w.width = 48;
  1797. if (win->w.height < 32)
  1798. win->w.height = 32;
  1799. if (win->clipcount > 2048)
  1800. win->clipcount = 2048;
  1801. win->chromakey = 0;
  1802. win->global_alpha = 0;
  1803. field = win->field;
  1804. switch (field) {
  1805. case V4L2_FIELD_TOP:
  1806. case V4L2_FIELD_BOTTOM:
  1807. case V4L2_FIELD_INTERLACED:
  1808. break;
  1809. default:
  1810. field = V4L2_FIELD_ANY;
  1811. break;
  1812. }
  1813. if (V4L2_FIELD_ANY == field) {
  1814. __s32 height2;
  1815. height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
  1816. field = (win->w.height > height2)
  1817. ? V4L2_FIELD_INTERLACED
  1818. : V4L2_FIELD_TOP;
  1819. }
  1820. win->field = field;
  1821. if (NULL == fh->ovfmt)
  1822. return -EINVAL;
  1823. /* 4-byte alignment. */
  1824. width_mask = ~0;
  1825. switch (fh->ovfmt->depth) {
  1826. case 8:
  1827. case 24:
  1828. width_mask = ~3;
  1829. break;
  1830. case 16:
  1831. width_mask = ~1;
  1832. break;
  1833. case 32:
  1834. break;
  1835. default:
  1836. BUG();
  1837. }
  1838. win->w.width -= win->w.left & ~width_mask;
  1839. win->w.left = (win->w.left - width_mask - 1) & width_mask;
  1840. return limit_scaled_size_lock(fh, &win->w.width, &win->w.height,
  1841. field, width_mask,
  1842. /* width_bias: round down */ 0,
  1843. adjust_size, adjust_crop);
  1844. }
  1845. static int setup_window_lock(struct bttv_fh *fh, struct bttv *btv,
  1846. struct v4l2_window *win, int fixup)
  1847. {
  1848. struct v4l2_clip *clips = NULL;
  1849. int n,size,retval = 0;
  1850. if (NULL == fh->ovfmt)
  1851. return -EINVAL;
  1852. if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
  1853. return -EINVAL;
  1854. retval = verify_window_lock(fh, win,
  1855. /* adjust_size */ fixup,
  1856. /* adjust_crop */ fixup);
  1857. if (0 != retval)
  1858. return retval;
  1859. /* copy clips -- luckily v4l1 + v4l2 are binary
  1860. compatible here ...*/
  1861. n = win->clipcount;
  1862. size = sizeof(*clips)*(n+4);
  1863. clips = kmalloc(size,GFP_KERNEL);
  1864. if (NULL == clips)
  1865. return -ENOMEM;
  1866. if (n > 0)
  1867. memcpy(clips, win->clips, sizeof(struct v4l2_clip) * n);
  1868. /* clip against screen */
  1869. if (NULL != btv->fbuf.base)
  1870. n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
  1871. &win->w, clips, n);
  1872. btcx_sort_clips(clips,n);
  1873. /* 4-byte alignments */
  1874. switch (fh->ovfmt->depth) {
  1875. case 8:
  1876. case 24:
  1877. btcx_align(&win->w, clips, n, 3);
  1878. break;
  1879. case 16:
  1880. btcx_align(&win->w, clips, n, 1);
  1881. break;
  1882. case 32:
  1883. /* no alignment fixups needed */
  1884. break;
  1885. default:
  1886. BUG();
  1887. }
  1888. kfree(fh->ov.clips);
  1889. fh->ov.clips = clips;
  1890. fh->ov.nclips = n;
  1891. fh->ov.w = win->w;
  1892. fh->ov.field = win->field;
  1893. fh->ov.setup_ok = 1;
  1894. btv->init.ov.w.width = win->w.width;
  1895. btv->init.ov.w.height = win->w.height;
  1896. btv->init.ov.field = win->field;
  1897. /* update overlay if needed */
  1898. retval = 0;
  1899. if (check_btres(fh, RESOURCE_OVERLAY)) {
  1900. struct bttv_buffer *new;
  1901. new = videobuf_sg_alloc(sizeof(*new));
  1902. new->crop = btv->crop[!!fh->do_crop].rect;
  1903. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  1904. retval = bttv_switch_overlay(btv,fh,new);
  1905. }
  1906. return retval;
  1907. }
  1908. /* ----------------------------------------------------------------------- */
  1909. static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
  1910. {
  1911. struct videobuf_queue* q = NULL;
  1912. switch (fh->type) {
  1913. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1914. q = &fh->cap;
  1915. break;
  1916. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1917. q = &fh->vbi;
  1918. break;
  1919. default:
  1920. BUG();
  1921. }
  1922. return q;
  1923. }
  1924. static int bttv_resource(struct bttv_fh *fh)
  1925. {
  1926. int res = 0;
  1927. switch (fh->type) {
  1928. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  1929. res = RESOURCE_VIDEO_STREAM;
  1930. break;
  1931. case V4L2_BUF_TYPE_VBI_CAPTURE:
  1932. res = RESOURCE_VBI;
  1933. break;
  1934. default:
  1935. BUG();
  1936. }
  1937. return res;
  1938. }
  1939. static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
  1940. {
  1941. struct videobuf_queue *q = bttv_queue(fh);
  1942. int res = bttv_resource(fh);
  1943. if (check_btres(fh,res))
  1944. return -EBUSY;
  1945. if (videobuf_queue_is_busy(q))
  1946. return -EBUSY;
  1947. fh->type = type;
  1948. return 0;
  1949. }
  1950. static void
  1951. pix_format_set_size (struct v4l2_pix_format * f,
  1952. const struct bttv_format * fmt,
  1953. unsigned int width,
  1954. unsigned int height)
  1955. {
  1956. f->width = width;
  1957. f->height = height;
  1958. if (fmt->flags & FORMAT_FLAGS_PLANAR) {
  1959. f->bytesperline = width; /* Y plane */
  1960. f->sizeimage = (width * height * fmt->depth) >> 3;
  1961. } else {
  1962. f->bytesperline = (width * fmt->depth) >> 3;
  1963. f->sizeimage = height * f->bytesperline;
  1964. }
  1965. }
  1966. static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
  1967. struct v4l2_format *f)
  1968. {
  1969. struct bttv_fh *fh = priv;
  1970. pix_format_set_size(&f->fmt.pix, fh->fmt,
  1971. fh->width, fh->height);
  1972. f->fmt.pix.field = fh->cap.field;
  1973. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  1974. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1975. return 0;
  1976. }
  1977. static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
  1978. struct v4l2_format *f)
  1979. {
  1980. struct bttv_fh *fh = priv;
  1981. f->fmt.win.w = fh->ov.w;
  1982. f->fmt.win.field = fh->ov.field;
  1983. return 0;
  1984. }
  1985. static void bttv_get_width_mask_vid_cap(const struct bttv_format *fmt,
  1986. unsigned int *width_mask,
  1987. unsigned int *width_bias)
  1988. {
  1989. if (fmt->flags & FORMAT_FLAGS_PLANAR) {
  1990. *width_mask = ~15; /* width must be a multiple of 16 pixels */
  1991. *width_bias = 8; /* nearest */
  1992. } else {
  1993. *width_mask = ~3; /* width must be a multiple of 4 pixels */
  1994. *width_bias = 2; /* nearest */
  1995. }
  1996. }
  1997. static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
  1998. struct v4l2_format *f)
  1999. {
  2000. const struct bttv_format *fmt;
  2001. struct bttv_fh *fh = priv;
  2002. struct bttv *btv = fh->btv;
  2003. enum v4l2_field field;
  2004. __s32 width, height;
  2005. __s32 height2;
  2006. unsigned int width_mask, width_bias;
  2007. int rc;
  2008. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  2009. if (NULL == fmt)
  2010. return -EINVAL;
  2011. field = f->fmt.pix.field;
  2012. switch (field) {
  2013. case V4L2_FIELD_TOP:
  2014. case V4L2_FIELD_BOTTOM:
  2015. case V4L2_FIELD_ALTERNATE:
  2016. case V4L2_FIELD_INTERLACED:
  2017. break;
  2018. case V4L2_FIELD_SEQ_BT:
  2019. case V4L2_FIELD_SEQ_TB:
  2020. if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
  2021. field = V4L2_FIELD_SEQ_TB;
  2022. break;
  2023. }
  2024. fallthrough;
  2025. default: /* FIELD_ANY case */
  2026. height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
  2027. field = (f->fmt.pix.height > height2)
  2028. ? V4L2_FIELD_INTERLACED
  2029. : V4L2_FIELD_BOTTOM;
  2030. break;
  2031. }
  2032. width = f->fmt.pix.width;
  2033. height = f->fmt.pix.height;
  2034. bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
  2035. rc = limit_scaled_size_lock(fh, &width, &height, field,
  2036. width_mask, width_bias,
  2037. /* adjust_size */ 1,
  2038. /* adjust_crop */ 0);
  2039. if (0 != rc)
  2040. return rc;
  2041. /* update data for the application */
  2042. f->fmt.pix.field = field;
  2043. pix_format_set_size(&f->fmt.pix, fmt, width, height);
  2044. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  2045. return 0;
  2046. }
  2047. static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
  2048. struct v4l2_format *f)
  2049. {
  2050. struct bttv_fh *fh = priv;
  2051. verify_window_lock(fh, &f->fmt.win,
  2052. /* adjust_size */ 1,
  2053. /* adjust_crop */ 0);
  2054. return 0;
  2055. }
  2056. static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
  2057. struct v4l2_format *f)
  2058. {
  2059. int retval;
  2060. const struct bttv_format *fmt;
  2061. struct bttv_fh *fh = priv;
  2062. struct bttv *btv = fh->btv;
  2063. __s32 width, height;
  2064. unsigned int width_mask, width_bias;
  2065. enum v4l2_field field;
  2066. retval = bttv_switch_type(fh, f->type);
  2067. if (0 != retval)
  2068. return retval;
  2069. retval = bttv_try_fmt_vid_cap(file, priv, f);
  2070. if (0 != retval)
  2071. return retval;
  2072. width = f->fmt.pix.width;
  2073. height = f->fmt.pix.height;
  2074. field = f->fmt.pix.field;
  2075. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  2076. bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
  2077. retval = limit_scaled_size_lock(fh, &width, &height, f->fmt.pix.field,
  2078. width_mask, width_bias,
  2079. /* adjust_size */ 1,
  2080. /* adjust_crop */ 1);
  2081. if (0 != retval)
  2082. return retval;
  2083. f->fmt.pix.field = field;
  2084. /* update our state information */
  2085. fh->fmt = fmt;
  2086. fh->cap.field = f->fmt.pix.field;
  2087. fh->cap.last = V4L2_FIELD_NONE;
  2088. fh->width = f->fmt.pix.width;
  2089. fh->height = f->fmt.pix.height;
  2090. btv->init.fmt = fmt;
  2091. btv->init.width = f->fmt.pix.width;
  2092. btv->init.height = f->fmt.pix.height;
  2093. return 0;
  2094. }
  2095. static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
  2096. struct v4l2_format *f)
  2097. {
  2098. struct bttv_fh *fh = priv;
  2099. struct bttv *btv = fh->btv;
  2100. if (no_overlay > 0) {
  2101. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  2102. return -EINVAL;
  2103. }
  2104. return setup_window_lock(fh, btv, &f->fmt.win, 1);
  2105. }
  2106. static int bttv_querycap(struct file *file, void *priv,
  2107. struct v4l2_capability *cap)
  2108. {
  2109. struct bttv_fh *fh = priv;
  2110. struct bttv *btv = fh->btv;
  2111. if (0 == v4l2)
  2112. return -EINVAL;
  2113. strscpy(cap->driver, "bttv", sizeof(cap->driver));
  2114. strscpy(cap->card, btv->video_dev.name, sizeof(cap->card));
  2115. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  2116. V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
  2117. if (no_overlay <= 0)
  2118. cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
  2119. if (video_is_registered(&btv->vbi_dev))
  2120. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  2121. if (video_is_registered(&btv->radio_dev)) {
  2122. cap->capabilities |= V4L2_CAP_RADIO;
  2123. if (btv->has_tea575x)
  2124. cap->capabilities |= V4L2_CAP_HW_FREQ_SEEK;
  2125. }
  2126. /*
  2127. * No need to lock here: those vars are initialized during board
  2128. * probe and remains untouched during the rest of the driver lifecycle
  2129. */
  2130. if (btv->has_saa6588)
  2131. cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
  2132. if (btv->tuner_type != TUNER_ABSENT)
  2133. cap->capabilities |= V4L2_CAP_TUNER;
  2134. return 0;
  2135. }
  2136. static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
  2137. {
  2138. int index = -1, i;
  2139. for (i = 0; i < FORMATS; i++) {
  2140. if (formats[i].fourcc != -1)
  2141. index++;
  2142. if ((unsigned int)index == f->index)
  2143. break;
  2144. }
  2145. if (FORMATS == i)
  2146. return -EINVAL;
  2147. f->pixelformat = formats[i].fourcc;
  2148. return i;
  2149. }
  2150. static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
  2151. struct v4l2_fmtdesc *f)
  2152. {
  2153. int rc = bttv_enum_fmt_cap_ovr(f);
  2154. if (rc < 0)
  2155. return rc;
  2156. return 0;
  2157. }
  2158. static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
  2159. struct v4l2_fmtdesc *f)
  2160. {
  2161. int rc;
  2162. if (no_overlay > 0) {
  2163. pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  2164. return -EINVAL;
  2165. }
  2166. rc = bttv_enum_fmt_cap_ovr(f);
  2167. if (rc < 0)
  2168. return rc;
  2169. if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
  2170. return -EINVAL;
  2171. return 0;
  2172. }
  2173. static int bttv_g_fbuf(struct file *file, void *f,
  2174. struct v4l2_framebuffer *fb)
  2175. {
  2176. struct bttv_fh *fh = f;
  2177. struct bttv *btv = fh->btv;
  2178. *fb = btv->fbuf;
  2179. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2180. fb->flags = V4L2_FBUF_FLAG_PRIMARY;
  2181. if (fh->ovfmt)
  2182. fb->fmt.pixelformat = fh->ovfmt->fourcc;
  2183. return 0;
  2184. }
  2185. static int bttv_overlay(struct file *file, void *f, unsigned int on)
  2186. {
  2187. struct bttv_fh *fh = f;
  2188. struct bttv *btv = fh->btv;
  2189. struct bttv_buffer *new;
  2190. int retval = 0;
  2191. if (on) {
  2192. /* verify args */
  2193. if (unlikely(!btv->fbuf.base)) {
  2194. return -EINVAL;
  2195. }
  2196. if (unlikely(!fh->ov.setup_ok)) {
  2197. dprintk("%d: overlay: !setup_ok\n", btv->c.nr);
  2198. retval = -EINVAL;
  2199. }
  2200. if (retval)
  2201. return retval;
  2202. }
  2203. if (!check_alloc_btres_lock(btv, fh, RESOURCE_OVERLAY))
  2204. return -EBUSY;
  2205. if (on) {
  2206. fh->ov.tvnorm = btv->tvnorm;
  2207. new = videobuf_sg_alloc(sizeof(*new));
  2208. new->crop = btv->crop[!!fh->do_crop].rect;
  2209. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  2210. } else {
  2211. new = NULL;
  2212. }
  2213. /* switch over */
  2214. retval = bttv_switch_overlay(btv, fh, new);
  2215. return retval;
  2216. }
  2217. static int bttv_s_fbuf(struct file *file, void *f,
  2218. const struct v4l2_framebuffer *fb)
  2219. {
  2220. struct bttv_fh *fh = f;
  2221. struct bttv *btv = fh->btv;
  2222. const struct bttv_format *fmt;
  2223. int retval;
  2224. if (!capable(CAP_SYS_ADMIN) &&
  2225. !capable(CAP_SYS_RAWIO))
  2226. return -EPERM;
  2227. /* check args */
  2228. fmt = format_by_fourcc(fb->fmt.pixelformat);
  2229. if (NULL == fmt)
  2230. return -EINVAL;
  2231. if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
  2232. return -EINVAL;
  2233. retval = -EINVAL;
  2234. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2235. __s32 width = fb->fmt.width;
  2236. __s32 height = fb->fmt.height;
  2237. retval = limit_scaled_size_lock(fh, &width, &height,
  2238. V4L2_FIELD_INTERLACED,
  2239. /* width_mask */ ~3,
  2240. /* width_bias */ 2,
  2241. /* adjust_size */ 0,
  2242. /* adjust_crop */ 0);
  2243. if (0 != retval)
  2244. return retval;
  2245. }
  2246. /* ok, accept it */
  2247. btv->fbuf.base = fb->base;
  2248. btv->fbuf.fmt.width = fb->fmt.width;
  2249. btv->fbuf.fmt.height = fb->fmt.height;
  2250. if (0 != fb->fmt.bytesperline)
  2251. btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
  2252. else
  2253. btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
  2254. retval = 0;
  2255. fh->ovfmt = fmt;
  2256. btv->init.ovfmt = fmt;
  2257. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2258. fh->ov.w.left = 0;
  2259. fh->ov.w.top = 0;
  2260. fh->ov.w.width = fb->fmt.width;
  2261. fh->ov.w.height = fb->fmt.height;
  2262. btv->init.ov.w.width = fb->fmt.width;
  2263. btv->init.ov.w.height = fb->fmt.height;
  2264. kfree(fh->ov.clips);
  2265. fh->ov.clips = NULL;
  2266. fh->ov.nclips = 0;
  2267. if (check_btres(fh, RESOURCE_OVERLAY)) {
  2268. struct bttv_buffer *new;
  2269. new = videobuf_sg_alloc(sizeof(*new));
  2270. new->crop = btv->crop[!!fh->do_crop].rect;
  2271. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  2272. retval = bttv_switch_overlay(btv, fh, new);
  2273. }
  2274. }
  2275. return retval;
  2276. }
  2277. static int bttv_reqbufs(struct file *file, void *priv,
  2278. struct v4l2_requestbuffers *p)
  2279. {
  2280. struct bttv_fh *fh = priv;
  2281. return videobuf_reqbufs(bttv_queue(fh), p);
  2282. }
  2283. static int bttv_querybuf(struct file *file, void *priv,
  2284. struct v4l2_buffer *b)
  2285. {
  2286. struct bttv_fh *fh = priv;
  2287. return videobuf_querybuf(bttv_queue(fh), b);
  2288. }
  2289. static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  2290. {
  2291. struct bttv_fh *fh = priv;
  2292. struct bttv *btv = fh->btv;
  2293. int res = bttv_resource(fh);
  2294. if (!check_alloc_btres_lock(btv, fh, res))
  2295. return -EBUSY;
  2296. return videobuf_qbuf(bttv_queue(fh), b);
  2297. }
  2298. static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  2299. {
  2300. struct bttv_fh *fh = priv;
  2301. return videobuf_dqbuf(bttv_queue(fh), b,
  2302. file->f_flags & O_NONBLOCK);
  2303. }
  2304. static int bttv_streamon(struct file *file, void *priv,
  2305. enum v4l2_buf_type type)
  2306. {
  2307. struct bttv_fh *fh = priv;
  2308. struct bttv *btv = fh->btv;
  2309. int res = bttv_resource(fh);
  2310. if (!check_alloc_btres_lock(btv, fh, res))
  2311. return -EBUSY;
  2312. return videobuf_streamon(bttv_queue(fh));
  2313. }
  2314. static int bttv_streamoff(struct file *file, void *priv,
  2315. enum v4l2_buf_type type)
  2316. {
  2317. struct bttv_fh *fh = priv;
  2318. struct bttv *btv = fh->btv;
  2319. int retval;
  2320. int res = bttv_resource(fh);
  2321. retval = videobuf_streamoff(bttv_queue(fh));
  2322. if (retval < 0)
  2323. return retval;
  2324. free_btres_lock(btv, fh, res);
  2325. return 0;
  2326. }
  2327. static int bttv_g_parm(struct file *file, void *f,
  2328. struct v4l2_streamparm *parm)
  2329. {
  2330. struct bttv_fh *fh = f;
  2331. struct bttv *btv = fh->btv;
  2332. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2333. return -EINVAL;
  2334. parm->parm.capture.readbuffers = gbuffers;
  2335. v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
  2336. &parm->parm.capture.timeperframe);
  2337. return 0;
  2338. }
  2339. static int bttv_g_tuner(struct file *file, void *priv,
  2340. struct v4l2_tuner *t)
  2341. {
  2342. struct bttv_fh *fh = priv;
  2343. struct bttv *btv = fh->btv;
  2344. if (0 != t->index)
  2345. return -EINVAL;
  2346. t->rxsubchans = V4L2_TUNER_SUB_MONO;
  2347. t->capability = V4L2_TUNER_CAP_NORM;
  2348. bttv_call_all(btv, tuner, g_tuner, t);
  2349. strscpy(t->name, "Television", sizeof(t->name));
  2350. t->type = V4L2_TUNER_ANALOG_TV;
  2351. if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
  2352. t->signal = 0xffff;
  2353. if (btv->audio_mode_gpio)
  2354. btv->audio_mode_gpio(btv, t, 0);
  2355. return 0;
  2356. }
  2357. static int bttv_g_pixelaspect(struct file *file, void *priv,
  2358. int type, struct v4l2_fract *f)
  2359. {
  2360. struct bttv_fh *fh = priv;
  2361. struct bttv *btv = fh->btv;
  2362. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2363. return -EINVAL;
  2364. /* defrect and bounds are set via g_selection */
  2365. *f = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
  2366. return 0;
  2367. }
  2368. static int bttv_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
  2369. {
  2370. struct bttv_fh *fh = f;
  2371. struct bttv *btv = fh->btv;
  2372. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2373. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  2374. return -EINVAL;
  2375. switch (sel->target) {
  2376. case V4L2_SEL_TGT_CROP:
  2377. /*
  2378. * No fh->do_crop = 1; because btv->crop[1] may be
  2379. * inconsistent with fh->width or fh->height and apps
  2380. * do not expect a change here.
  2381. */
  2382. sel->r = btv->crop[!!fh->do_crop].rect;
  2383. break;
  2384. case V4L2_SEL_TGT_CROP_DEFAULT:
  2385. sel->r = bttv_tvnorms[btv->tvnorm].cropcap.defrect;
  2386. break;
  2387. case V4L2_SEL_TGT_CROP_BOUNDS:
  2388. sel->r = bttv_tvnorms[btv->tvnorm].cropcap.bounds;
  2389. break;
  2390. default:
  2391. return -EINVAL;
  2392. }
  2393. return 0;
  2394. }
  2395. static int bttv_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
  2396. {
  2397. struct bttv_fh *fh = f;
  2398. struct bttv *btv = fh->btv;
  2399. const struct v4l2_rect *b;
  2400. int retval;
  2401. struct bttv_crop c;
  2402. __s32 b_left;
  2403. __s32 b_top;
  2404. __s32 b_right;
  2405. __s32 b_bottom;
  2406. if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2407. sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  2408. return -EINVAL;
  2409. if (sel->target != V4L2_SEL_TGT_CROP)
  2410. return -EINVAL;
  2411. /* Make sure tvnorm, vbi_end and the current cropping
  2412. parameters remain consistent until we're done. Note
  2413. read() may change vbi_end in check_alloc_btres_lock(). */
  2414. retval = -EBUSY;
  2415. if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
  2416. return retval;
  2417. }
  2418. b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
  2419. b_left = b->left;
  2420. b_right = b_left + b->width;
  2421. b_bottom = b->top + b->height;
  2422. b_top = max(b->top, btv->vbi_end);
  2423. if (b_top + 32 >= b_bottom) {
  2424. return retval;
  2425. }
  2426. /* Min. scaled size 48 x 32. */
  2427. c.rect.left = clamp_t(s32, sel->r.left, b_left, b_right - 48);
  2428. c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
  2429. c.rect.width = clamp_t(s32, sel->r.width,
  2430. 48, b_right - c.rect.left);
  2431. c.rect.top = clamp_t(s32, sel->r.top, b_top, b_bottom - 32);
  2432. /* Top and height must be a multiple of two. */
  2433. c.rect.top = (c.rect.top + 1) & ~1;
  2434. c.rect.height = clamp_t(s32, sel->r.height,
  2435. 32, b_bottom - c.rect.top);
  2436. c.rect.height = (c.rect.height + 1) & ~1;
  2437. bttv_crop_calc_limits(&c);
  2438. sel->r = c.rect;
  2439. btv->crop[1] = c;
  2440. fh->do_crop = 1;
  2441. if (fh->width < c.min_scaled_width) {
  2442. fh->width = c.min_scaled_width;
  2443. btv->init.width = c.min_scaled_width;
  2444. } else if (fh->width > c.max_scaled_width) {
  2445. fh->width = c.max_scaled_width;
  2446. btv->init.width = c.max_scaled_width;
  2447. }
  2448. if (fh->height < c.min_scaled_height) {
  2449. fh->height = c.min_scaled_height;
  2450. btv->init.height = c.min_scaled_height;
  2451. } else if (fh->height > c.max_scaled_height) {
  2452. fh->height = c.max_scaled_height;
  2453. btv->init.height = c.max_scaled_height;
  2454. }
  2455. return 0;
  2456. }
  2457. static ssize_t bttv_read(struct file *file, char __user *data,
  2458. size_t count, loff_t *ppos)
  2459. {
  2460. struct bttv_fh *fh = file->private_data;
  2461. int retval = 0;
  2462. if (fh->btv->errors)
  2463. bttv_reinit_bt848(fh->btv);
  2464. dprintk("%d: read count=%d type=%s\n",
  2465. fh->btv->c.nr, (int)count, v4l2_type_names[fh->type]);
  2466. switch (fh->type) {
  2467. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2468. if (!check_alloc_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ)) {
  2469. /* VIDEO_READ in use by another fh,
  2470. or VIDEO_STREAM by any fh. */
  2471. return -EBUSY;
  2472. }
  2473. retval = videobuf_read_one(&fh->cap, data, count, ppos,
  2474. file->f_flags & O_NONBLOCK);
  2475. free_btres_lock(fh->btv, fh, RESOURCE_VIDEO_READ);
  2476. break;
  2477. case V4L2_BUF_TYPE_VBI_CAPTURE:
  2478. if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
  2479. return -EBUSY;
  2480. retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
  2481. file->f_flags & O_NONBLOCK);
  2482. break;
  2483. default:
  2484. BUG();
  2485. }
  2486. return retval;
  2487. }
  2488. static __poll_t bttv_poll(struct file *file, poll_table *wait)
  2489. {
  2490. struct bttv_fh *fh = file->private_data;
  2491. struct bttv_buffer *buf;
  2492. enum v4l2_field field;
  2493. __poll_t rc = 0;
  2494. __poll_t req_events = poll_requested_events(wait);
  2495. if (v4l2_event_pending(&fh->fh))
  2496. rc = EPOLLPRI;
  2497. else if (req_events & EPOLLPRI)
  2498. poll_wait(file, &fh->fh.wait, wait);
  2499. if (!(req_events & (EPOLLIN | EPOLLRDNORM)))
  2500. return rc;
  2501. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  2502. if (!check_alloc_btres_lock(fh->btv,fh,RESOURCE_VBI))
  2503. return rc | EPOLLERR;
  2504. return rc | videobuf_poll_stream(file, &fh->vbi, wait);
  2505. }
  2506. if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
  2507. /* streaming capture */
  2508. if (list_empty(&fh->cap.stream))
  2509. return rc | EPOLLERR;
  2510. buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
  2511. } else {
  2512. /* read() capture */
  2513. if (NULL == fh->cap.read_buf) {
  2514. /* need to capture a new frame */
  2515. if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
  2516. return rc | EPOLLERR;
  2517. fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
  2518. if (NULL == fh->cap.read_buf)
  2519. return rc | EPOLLERR;
  2520. fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
  2521. field = videobuf_next_field(&fh->cap);
  2522. if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
  2523. kfree (fh->cap.read_buf);
  2524. fh->cap.read_buf = NULL;
  2525. return rc | EPOLLERR;
  2526. }
  2527. fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
  2528. fh->cap.read_off = 0;
  2529. }
  2530. buf = (struct bttv_buffer*)fh->cap.read_buf;
  2531. }
  2532. poll_wait(file, &buf->vb.done, wait);
  2533. if (buf->vb.state == VIDEOBUF_DONE ||
  2534. buf->vb.state == VIDEOBUF_ERROR)
  2535. rc = rc | EPOLLIN|EPOLLRDNORM;
  2536. return rc;
  2537. }
  2538. static int bttv_open(struct file *file)
  2539. {
  2540. struct video_device *vdev = video_devdata(file);
  2541. struct bttv *btv = video_drvdata(file);
  2542. struct bttv_fh *fh;
  2543. enum v4l2_buf_type type = 0;
  2544. dprintk("open dev=%s\n", video_device_node_name(vdev));
  2545. if (vdev->vfl_type == VFL_TYPE_VIDEO) {
  2546. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2547. } else if (vdev->vfl_type == VFL_TYPE_VBI) {
  2548. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  2549. } else {
  2550. WARN_ON(1);
  2551. return -ENODEV;
  2552. }
  2553. dprintk("%d: open called (type=%s)\n",
  2554. btv->c.nr, v4l2_type_names[type]);
  2555. /* allocate per filehandle data */
  2556. fh = kmalloc(sizeof(*fh), GFP_KERNEL);
  2557. if (unlikely(!fh))
  2558. return -ENOMEM;
  2559. btv->users++;
  2560. file->private_data = fh;
  2561. *fh = btv->init;
  2562. v4l2_fh_init(&fh->fh, vdev);
  2563. fh->type = type;
  2564. fh->ov.setup_ok = 0;
  2565. videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
  2566. &btv->c.pci->dev, &btv->s_lock,
  2567. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  2568. V4L2_FIELD_INTERLACED,
  2569. sizeof(struct bttv_buffer),
  2570. fh, &btv->lock);
  2571. videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
  2572. &btv->c.pci->dev, &btv->s_lock,
  2573. V4L2_BUF_TYPE_VBI_CAPTURE,
  2574. V4L2_FIELD_SEQ_TB,
  2575. sizeof(struct bttv_buffer),
  2576. fh, &btv->lock);
  2577. set_tvnorm(btv,btv->tvnorm);
  2578. set_input(btv, btv->input, btv->tvnorm);
  2579. audio_mute(btv, btv->mute);
  2580. /* The V4L2 spec requires one global set of cropping parameters
  2581. which only change on request. These are stored in btv->crop[1].
  2582. However for compatibility with V4L apps and cropping unaware
  2583. V4L2 apps we now reset the cropping parameters as seen through
  2584. this fh, which is to say VIDIOC_G_SELECTION and scaling limit checks
  2585. will use btv->crop[0], the default cropping parameters for the
  2586. current video standard, and VIDIOC_S_FMT will not implicitly
  2587. change the cropping parameters until VIDIOC_S_SELECTION has been
  2588. called. */
  2589. fh->do_crop = !reset_crop; /* module parameter */
  2590. /* Likewise there should be one global set of VBI capture
  2591. parameters, but for compatibility with V4L apps and earlier
  2592. driver versions each fh has its own parameters. */
  2593. bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
  2594. bttv_field_count(btv);
  2595. v4l2_fh_add(&fh->fh);
  2596. return 0;
  2597. }
  2598. static int bttv_release(struct file *file)
  2599. {
  2600. struct bttv_fh *fh = file->private_data;
  2601. struct bttv *btv = fh->btv;
  2602. /* turn off overlay */
  2603. if (check_btres(fh, RESOURCE_OVERLAY))
  2604. bttv_switch_overlay(btv,fh,NULL);
  2605. /* stop video capture */
  2606. if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
  2607. videobuf_streamoff(&fh->cap);
  2608. free_btres_lock(btv,fh,RESOURCE_VIDEO_STREAM);
  2609. }
  2610. if (fh->cap.read_buf) {
  2611. buffer_release(&fh->cap,fh->cap.read_buf);
  2612. kfree(fh->cap.read_buf);
  2613. }
  2614. if (check_btres(fh, RESOURCE_VIDEO_READ)) {
  2615. free_btres_lock(btv, fh, RESOURCE_VIDEO_READ);
  2616. }
  2617. /* stop vbi capture */
  2618. if (check_btres(fh, RESOURCE_VBI)) {
  2619. videobuf_stop(&fh->vbi);
  2620. free_btres_lock(btv,fh,RESOURCE_VBI);
  2621. }
  2622. /* free stuff */
  2623. videobuf_mmap_free(&fh->cap);
  2624. videobuf_mmap_free(&fh->vbi);
  2625. file->private_data = NULL;
  2626. btv->users--;
  2627. bttv_field_count(btv);
  2628. if (!btv->users)
  2629. audio_mute(btv, btv->mute);
  2630. v4l2_fh_del(&fh->fh);
  2631. v4l2_fh_exit(&fh->fh);
  2632. kfree(fh);
  2633. return 0;
  2634. }
  2635. static int
  2636. bttv_mmap(struct file *file, struct vm_area_struct *vma)
  2637. {
  2638. struct bttv_fh *fh = file->private_data;
  2639. dprintk("%d: mmap type=%s 0x%lx+%ld\n",
  2640. fh->btv->c.nr, v4l2_type_names[fh->type],
  2641. vma->vm_start, vma->vm_end - vma->vm_start);
  2642. return videobuf_mmap_mapper(bttv_queue(fh),vma);
  2643. }
  2644. static const struct v4l2_file_operations bttv_fops =
  2645. {
  2646. .owner = THIS_MODULE,
  2647. .open = bttv_open,
  2648. .release = bttv_release,
  2649. .unlocked_ioctl = video_ioctl2,
  2650. .read = bttv_read,
  2651. .mmap = bttv_mmap,
  2652. .poll = bttv_poll,
  2653. };
  2654. static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
  2655. .vidioc_querycap = bttv_querycap,
  2656. .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
  2657. .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
  2658. .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
  2659. .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
  2660. .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
  2661. .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
  2662. .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
  2663. .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
  2664. .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
  2665. .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
  2666. .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
  2667. .vidioc_g_pixelaspect = bttv_g_pixelaspect,
  2668. .vidioc_reqbufs = bttv_reqbufs,
  2669. .vidioc_querybuf = bttv_querybuf,
  2670. .vidioc_qbuf = bttv_qbuf,
  2671. .vidioc_dqbuf = bttv_dqbuf,
  2672. .vidioc_s_std = bttv_s_std,
  2673. .vidioc_g_std = bttv_g_std,
  2674. .vidioc_enum_input = bttv_enum_input,
  2675. .vidioc_g_input = bttv_g_input,
  2676. .vidioc_s_input = bttv_s_input,
  2677. .vidioc_streamon = bttv_streamon,
  2678. .vidioc_streamoff = bttv_streamoff,
  2679. .vidioc_g_tuner = bttv_g_tuner,
  2680. .vidioc_s_tuner = bttv_s_tuner,
  2681. .vidioc_g_selection = bttv_g_selection,
  2682. .vidioc_s_selection = bttv_s_selection,
  2683. .vidioc_g_fbuf = bttv_g_fbuf,
  2684. .vidioc_s_fbuf = bttv_s_fbuf,
  2685. .vidioc_overlay = bttv_overlay,
  2686. .vidioc_g_parm = bttv_g_parm,
  2687. .vidioc_g_frequency = bttv_g_frequency,
  2688. .vidioc_s_frequency = bttv_s_frequency,
  2689. .vidioc_log_status = bttv_log_status,
  2690. .vidioc_querystd = bttv_querystd,
  2691. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  2692. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  2693. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2694. .vidioc_g_register = bttv_g_register,
  2695. .vidioc_s_register = bttv_s_register,
  2696. #endif
  2697. };
  2698. static struct video_device bttv_video_template = {
  2699. .fops = &bttv_fops,
  2700. .ioctl_ops = &bttv_ioctl_ops,
  2701. .tvnorms = BTTV_NORMS,
  2702. };
  2703. /* ----------------------------------------------------------------------- */
  2704. /* radio interface */
  2705. static int radio_open(struct file *file)
  2706. {
  2707. struct video_device *vdev = video_devdata(file);
  2708. struct bttv *btv = video_drvdata(file);
  2709. struct bttv_fh *fh;
  2710. dprintk("open dev=%s\n", video_device_node_name(vdev));
  2711. dprintk("%d: open called (radio)\n", btv->c.nr);
  2712. /* allocate per filehandle data */
  2713. fh = kmalloc(sizeof(*fh), GFP_KERNEL);
  2714. if (unlikely(!fh))
  2715. return -ENOMEM;
  2716. file->private_data = fh;
  2717. *fh = btv->init;
  2718. v4l2_fh_init(&fh->fh, vdev);
  2719. btv->radio_user++;
  2720. audio_mute(btv, btv->mute);
  2721. v4l2_fh_add(&fh->fh);
  2722. return 0;
  2723. }
  2724. static int radio_release(struct file *file)
  2725. {
  2726. struct bttv_fh *fh = file->private_data;
  2727. struct bttv *btv = fh->btv;
  2728. struct saa6588_command cmd;
  2729. file->private_data = NULL;
  2730. v4l2_fh_del(&fh->fh);
  2731. v4l2_fh_exit(&fh->fh);
  2732. kfree(fh);
  2733. btv->radio_user--;
  2734. bttv_call_all(btv, core, command, SAA6588_CMD_CLOSE, &cmd);
  2735. if (btv->radio_user == 0)
  2736. btv->has_radio_tuner = 0;
  2737. return 0;
  2738. }
  2739. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  2740. {
  2741. struct bttv_fh *fh = priv;
  2742. struct bttv *btv = fh->btv;
  2743. if (0 != t->index)
  2744. return -EINVAL;
  2745. strscpy(t->name, "Radio", sizeof(t->name));
  2746. t->type = V4L2_TUNER_RADIO;
  2747. radio_enable(btv);
  2748. bttv_call_all(btv, tuner, g_tuner, t);
  2749. if (btv->audio_mode_gpio)
  2750. btv->audio_mode_gpio(btv, t, 0);
  2751. if (btv->has_tea575x)
  2752. return snd_tea575x_g_tuner(&btv->tea, t);
  2753. return 0;
  2754. }
  2755. static int radio_s_tuner(struct file *file, void *priv,
  2756. const struct v4l2_tuner *t)
  2757. {
  2758. struct bttv_fh *fh = priv;
  2759. struct bttv *btv = fh->btv;
  2760. if (0 != t->index)
  2761. return -EINVAL;
  2762. radio_enable(btv);
  2763. bttv_call_all(btv, tuner, s_tuner, t);
  2764. return 0;
  2765. }
  2766. static int radio_s_hw_freq_seek(struct file *file, void *priv,
  2767. const struct v4l2_hw_freq_seek *a)
  2768. {
  2769. struct bttv_fh *fh = priv;
  2770. struct bttv *btv = fh->btv;
  2771. if (btv->has_tea575x)
  2772. return snd_tea575x_s_hw_freq_seek(file, &btv->tea, a);
  2773. return -ENOTTY;
  2774. }
  2775. static int radio_enum_freq_bands(struct file *file, void *priv,
  2776. struct v4l2_frequency_band *band)
  2777. {
  2778. struct bttv_fh *fh = priv;
  2779. struct bttv *btv = fh->btv;
  2780. if (btv->has_tea575x)
  2781. return snd_tea575x_enum_freq_bands(&btv->tea, band);
  2782. return -ENOTTY;
  2783. }
  2784. static ssize_t radio_read(struct file *file, char __user *data,
  2785. size_t count, loff_t *ppos)
  2786. {
  2787. struct bttv_fh *fh = file->private_data;
  2788. struct bttv *btv = fh->btv;
  2789. struct saa6588_command cmd;
  2790. cmd.block_count = count / 3;
  2791. cmd.nonblocking = file->f_flags & O_NONBLOCK;
  2792. cmd.buffer = data;
  2793. cmd.instance = file;
  2794. cmd.result = -ENODEV;
  2795. radio_enable(btv);
  2796. bttv_call_all(btv, core, command, SAA6588_CMD_READ, &cmd);
  2797. return cmd.result;
  2798. }
  2799. static __poll_t radio_poll(struct file *file, poll_table *wait)
  2800. {
  2801. struct bttv_fh *fh = file->private_data;
  2802. struct bttv *btv = fh->btv;
  2803. __poll_t req_events = poll_requested_events(wait);
  2804. struct saa6588_command cmd;
  2805. __poll_t res = 0;
  2806. if (v4l2_event_pending(&fh->fh))
  2807. res = EPOLLPRI;
  2808. else if (req_events & EPOLLPRI)
  2809. poll_wait(file, &fh->fh.wait, wait);
  2810. radio_enable(btv);
  2811. cmd.instance = file;
  2812. cmd.event_list = wait;
  2813. cmd.poll_mask = res;
  2814. bttv_call_all(btv, core, command, SAA6588_CMD_POLL, &cmd);
  2815. return cmd.poll_mask;
  2816. }
  2817. static const struct v4l2_file_operations radio_fops =
  2818. {
  2819. .owner = THIS_MODULE,
  2820. .open = radio_open,
  2821. .read = radio_read,
  2822. .release = radio_release,
  2823. .unlocked_ioctl = video_ioctl2,
  2824. .poll = radio_poll,
  2825. };
  2826. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2827. .vidioc_querycap = bttv_querycap,
  2828. .vidioc_log_status = bttv_log_status,
  2829. .vidioc_g_tuner = radio_g_tuner,
  2830. .vidioc_s_tuner = radio_s_tuner,
  2831. .vidioc_g_frequency = bttv_g_frequency,
  2832. .vidioc_s_frequency = bttv_s_frequency,
  2833. .vidioc_s_hw_freq_seek = radio_s_hw_freq_seek,
  2834. .vidioc_enum_freq_bands = radio_enum_freq_bands,
  2835. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  2836. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  2837. };
  2838. static struct video_device radio_template = {
  2839. .fops = &radio_fops,
  2840. .ioctl_ops = &radio_ioctl_ops,
  2841. };
  2842. /* ----------------------------------------------------------------------- */
  2843. /* some debug code */
  2844. static int bttv_risc_decode(u32 risc)
  2845. {
  2846. static char *instr[16] = {
  2847. [ BT848_RISC_WRITE >> 28 ] = "write",
  2848. [ BT848_RISC_SKIP >> 28 ] = "skip",
  2849. [ BT848_RISC_WRITEC >> 28 ] = "writec",
  2850. [ BT848_RISC_JUMP >> 28 ] = "jump",
  2851. [ BT848_RISC_SYNC >> 28 ] = "sync",
  2852. [ BT848_RISC_WRITE123 >> 28 ] = "write123",
  2853. [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
  2854. [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
  2855. };
  2856. static int incr[16] = {
  2857. [ BT848_RISC_WRITE >> 28 ] = 2,
  2858. [ BT848_RISC_JUMP >> 28 ] = 2,
  2859. [ BT848_RISC_SYNC >> 28 ] = 2,
  2860. [ BT848_RISC_WRITE123 >> 28 ] = 5,
  2861. [ BT848_RISC_SKIP123 >> 28 ] = 2,
  2862. [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
  2863. };
  2864. static char *bits[] = {
  2865. "be0", "be1", "be2", "be3/resync",
  2866. "set0", "set1", "set2", "set3",
  2867. "clr0", "clr1", "clr2", "clr3",
  2868. "irq", "res", "eol", "sol",
  2869. };
  2870. int i;
  2871. pr_cont("0x%08x [ %s", risc,
  2872. instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
  2873. for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
  2874. if (risc & (1 << (i + 12)))
  2875. pr_cont(" %s", bits[i]);
  2876. pr_cont(" count=%d ]\n", risc & 0xfff);
  2877. return incr[risc >> 28] ? incr[risc >> 28] : 1;
  2878. }
  2879. static void bttv_risc_disasm(struct bttv *btv,
  2880. struct btcx_riscmem *risc)
  2881. {
  2882. unsigned int i,j,n;
  2883. pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
  2884. btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
  2885. for (i = 0; i < (risc->size >> 2); i += n) {
  2886. pr_info("%s: 0x%lx: ",
  2887. btv->c.v4l2_dev.name,
  2888. (unsigned long)(risc->dma + (i<<2)));
  2889. n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
  2890. for (j = 1; j < n; j++)
  2891. pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
  2892. btv->c.v4l2_dev.name,
  2893. (unsigned long)(risc->dma + ((i+j)<<2)),
  2894. risc->cpu[i+j], j);
  2895. if (0 == risc->cpu[i])
  2896. break;
  2897. }
  2898. }
  2899. static void bttv_print_riscaddr(struct bttv *btv)
  2900. {
  2901. pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
  2902. pr_info(" vbi : o=%08llx e=%08llx\n",
  2903. btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
  2904. btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
  2905. pr_info(" cap : o=%08llx e=%08llx\n",
  2906. btv->curr.top
  2907. ? (unsigned long long)btv->curr.top->top.dma : 0,
  2908. btv->curr.bottom
  2909. ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
  2910. pr_info(" scr : o=%08llx e=%08llx\n",
  2911. btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
  2912. btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
  2913. bttv_risc_disasm(btv, &btv->main);
  2914. }
  2915. /* ----------------------------------------------------------------------- */
  2916. /* irq handler */
  2917. static char *irq_name[] = {
  2918. "FMTCHG", // format change detected (525 vs. 625)
  2919. "VSYNC", // vertical sync (new field)
  2920. "HSYNC", // horizontal sync
  2921. "OFLOW", // chroma/luma AGC overflow
  2922. "HLOCK", // horizontal lock changed
  2923. "VPRES", // video presence changed
  2924. "6", "7",
  2925. "I2CDONE", // hw irc operation finished
  2926. "GPINT", // gpio port triggered irq
  2927. "10",
  2928. "RISCI", // risc instruction triggered irq
  2929. "FBUS", // pixel data fifo dropped data (high pci bus latencies)
  2930. "FTRGT", // pixel data fifo overrun
  2931. "FDSR", // fifo data stream resyncronisation
  2932. "PPERR", // parity error (data transfer)
  2933. "RIPERR", // parity error (read risc instructions)
  2934. "PABORT", // pci abort
  2935. "OCERR", // risc instruction error
  2936. "SCERR", // syncronisation error
  2937. };
  2938. static void bttv_print_irqbits(u32 print, u32 mark)
  2939. {
  2940. unsigned int i;
  2941. pr_cont("bits:");
  2942. for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
  2943. if (print & (1 << i))
  2944. pr_cont(" %s", irq_name[i]);
  2945. if (mark & (1 << i))
  2946. pr_cont("*");
  2947. }
  2948. }
  2949. static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
  2950. {
  2951. pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
  2952. btv->c.nr,
  2953. (unsigned long)btv->main.dma,
  2954. (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
  2955. (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
  2956. (unsigned long)rc);
  2957. if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
  2958. pr_notice("%d: Oh, there (temporarily?) is no input signal. Ok, then this is harmless, don't worry ;)\n",
  2959. btv->c.nr);
  2960. return;
  2961. }
  2962. pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
  2963. btv->c.nr);
  2964. pr_notice("%d: Lets try to catch the culprit red-handed ...\n",
  2965. btv->c.nr);
  2966. dump_stack();
  2967. }
  2968. static int
  2969. bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
  2970. {
  2971. struct bttv_buffer *item;
  2972. memset(set,0,sizeof(*set));
  2973. /* capture request ? */
  2974. if (!list_empty(&btv->capture)) {
  2975. set->frame_irq = 1;
  2976. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  2977. if (V4L2_FIELD_HAS_TOP(item->vb.field))
  2978. set->top = item;
  2979. if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
  2980. set->bottom = item;
  2981. /* capture request for other field ? */
  2982. if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
  2983. (item->vb.queue.next != &btv->capture)) {
  2984. item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
  2985. /* Mike Isely <[email protected]> - Only check
  2986. * and set up the bottom field in the logic
  2987. * below. Don't ever do the top field. This
  2988. * of course means that if we set up the
  2989. * bottom field in the above code that we'll
  2990. * actually skip a field. But that's OK.
  2991. * Having processed only a single buffer this
  2992. * time, then the next time around the first
  2993. * available buffer should be for a top field.
  2994. * That will then cause us here to set up a
  2995. * top then a bottom field in the normal way.
  2996. * The alternative to this understanding is
  2997. * that we set up the second available buffer
  2998. * as a top field, but that's out of order
  2999. * since this driver always processes the top
  3000. * field first - the effect will be the two
  3001. * buffers being returned in the wrong order,
  3002. * with the second buffer also being delayed
  3003. * by one field time (owing to the fifo nature
  3004. * of videobuf). Worse still, we'll be stuck
  3005. * doing fields out of order now every time
  3006. * until something else causes a field to be
  3007. * dropped. By effectively forcing a field to
  3008. * drop this way then we always get back into
  3009. * sync within a single frame time. (Out of
  3010. * order fields can screw up deinterlacing
  3011. * algorithms.) */
  3012. if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
  3013. if (NULL == set->bottom &&
  3014. V4L2_FIELD_BOTTOM == item->vb.field) {
  3015. set->bottom = item;
  3016. }
  3017. if (NULL != set->top && NULL != set->bottom)
  3018. set->top_irq = 2;
  3019. }
  3020. }
  3021. }
  3022. /* screen overlay ? */
  3023. if (NULL != btv->screen) {
  3024. if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
  3025. if (NULL == set->top && NULL == set->bottom) {
  3026. set->top = btv->screen;
  3027. set->bottom = btv->screen;
  3028. }
  3029. } else {
  3030. if (V4L2_FIELD_TOP == btv->screen->vb.field &&
  3031. NULL == set->top) {
  3032. set->top = btv->screen;
  3033. }
  3034. if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
  3035. NULL == set->bottom) {
  3036. set->bottom = btv->screen;
  3037. }
  3038. }
  3039. }
  3040. dprintk("%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
  3041. btv->c.nr, set->top, set->bottom,
  3042. btv->screen, set->frame_irq, set->top_irq);
  3043. return 0;
  3044. }
  3045. static void
  3046. bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
  3047. struct bttv_buffer_set *curr, unsigned int state)
  3048. {
  3049. u64 ts = ktime_get_ns();
  3050. if (wakeup->top == wakeup->bottom) {
  3051. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3052. if (irq_debug > 1)
  3053. pr_debug("%d: wakeup: both=%p\n",
  3054. btv->c.nr, wakeup->top);
  3055. wakeup->top->vb.ts = ts;
  3056. wakeup->top->vb.field_count = btv->field_count;
  3057. wakeup->top->vb.state = state;
  3058. wake_up(&wakeup->top->vb.done);
  3059. }
  3060. } else {
  3061. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3062. if (irq_debug > 1)
  3063. pr_debug("%d: wakeup: top=%p\n",
  3064. btv->c.nr, wakeup->top);
  3065. wakeup->top->vb.ts = ts;
  3066. wakeup->top->vb.field_count = btv->field_count;
  3067. wakeup->top->vb.state = state;
  3068. wake_up(&wakeup->top->vb.done);
  3069. }
  3070. if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
  3071. if (irq_debug > 1)
  3072. pr_debug("%d: wakeup: bottom=%p\n",
  3073. btv->c.nr, wakeup->bottom);
  3074. wakeup->bottom->vb.ts = ts;
  3075. wakeup->bottom->vb.field_count = btv->field_count;
  3076. wakeup->bottom->vb.state = state;
  3077. wake_up(&wakeup->bottom->vb.done);
  3078. }
  3079. }
  3080. }
  3081. static void
  3082. bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
  3083. unsigned int state)
  3084. {
  3085. if (NULL == wakeup)
  3086. return;
  3087. wakeup->vb.ts = ktime_get_ns();
  3088. wakeup->vb.field_count = btv->field_count;
  3089. wakeup->vb.state = state;
  3090. wake_up(&wakeup->vb.done);
  3091. }
  3092. static void bttv_irq_timeout(struct timer_list *t)
  3093. {
  3094. struct bttv *btv = from_timer(btv, t, timeout);
  3095. struct bttv_buffer_set old,new;
  3096. struct bttv_buffer *ovbi;
  3097. struct bttv_buffer *item;
  3098. unsigned long flags;
  3099. if (bttv_verbose) {
  3100. pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
  3101. btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
  3102. btread(BT848_RISC_COUNT));
  3103. bttv_print_irqbits(btread(BT848_INT_STAT),0);
  3104. pr_cont("\n");
  3105. }
  3106. spin_lock_irqsave(&btv->s_lock,flags);
  3107. /* deactivate stuff */
  3108. memset(&new,0,sizeof(new));
  3109. old = btv->curr;
  3110. ovbi = btv->cvbi;
  3111. btv->curr = new;
  3112. btv->cvbi = NULL;
  3113. btv->loop_irq = 0;
  3114. bttv_buffer_activate_video(btv, &new);
  3115. bttv_buffer_activate_vbi(btv, NULL);
  3116. bttv_set_dma(btv, 0);
  3117. /* wake up */
  3118. bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
  3119. bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
  3120. /* cancel all outstanding capture / vbi requests */
  3121. while (!list_empty(&btv->capture)) {
  3122. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  3123. list_del(&item->vb.queue);
  3124. item->vb.state = VIDEOBUF_ERROR;
  3125. wake_up(&item->vb.done);
  3126. }
  3127. while (!list_empty(&btv->vcapture)) {
  3128. item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3129. list_del(&item->vb.queue);
  3130. item->vb.state = VIDEOBUF_ERROR;
  3131. wake_up(&item->vb.done);
  3132. }
  3133. btv->errors++;
  3134. spin_unlock_irqrestore(&btv->s_lock,flags);
  3135. }
  3136. static void
  3137. bttv_irq_wakeup_top(struct bttv *btv)
  3138. {
  3139. struct bttv_buffer *wakeup = btv->curr.top;
  3140. if (NULL == wakeup)
  3141. return;
  3142. spin_lock(&btv->s_lock);
  3143. btv->curr.top_irq = 0;
  3144. btv->curr.top = NULL;
  3145. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  3146. wakeup->vb.ts = ktime_get_ns();
  3147. wakeup->vb.field_count = btv->field_count;
  3148. wakeup->vb.state = VIDEOBUF_DONE;
  3149. wake_up(&wakeup->vb.done);
  3150. spin_unlock(&btv->s_lock);
  3151. }
  3152. static inline int is_active(struct btcx_riscmem *risc, u32 rc)
  3153. {
  3154. if (rc < risc->dma)
  3155. return 0;
  3156. if (rc > risc->dma + risc->size)
  3157. return 0;
  3158. return 1;
  3159. }
  3160. static void
  3161. bttv_irq_switch_video(struct bttv *btv)
  3162. {
  3163. struct bttv_buffer_set new;
  3164. struct bttv_buffer_set old;
  3165. dma_addr_t rc;
  3166. spin_lock(&btv->s_lock);
  3167. /* new buffer set */
  3168. bttv_irq_next_video(btv, &new);
  3169. rc = btread(BT848_RISC_COUNT);
  3170. if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
  3171. (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
  3172. btv->framedrop++;
  3173. if (debug_latency)
  3174. bttv_irq_debug_low_latency(btv, rc);
  3175. spin_unlock(&btv->s_lock);
  3176. return;
  3177. }
  3178. /* switch over */
  3179. old = btv->curr;
  3180. btv->curr = new;
  3181. btv->loop_irq &= ~1;
  3182. bttv_buffer_activate_video(btv, &new);
  3183. bttv_set_dma(btv, 0);
  3184. /* switch input */
  3185. if (UNSET != btv->new_input) {
  3186. video_mux(btv,btv->new_input);
  3187. btv->new_input = UNSET;
  3188. }
  3189. /* wake up finished buffers */
  3190. bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
  3191. spin_unlock(&btv->s_lock);
  3192. }
  3193. static void
  3194. bttv_irq_switch_vbi(struct bttv *btv)
  3195. {
  3196. struct bttv_buffer *new = NULL;
  3197. struct bttv_buffer *old;
  3198. u32 rc;
  3199. spin_lock(&btv->s_lock);
  3200. if (!list_empty(&btv->vcapture))
  3201. new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3202. old = btv->cvbi;
  3203. rc = btread(BT848_RISC_COUNT);
  3204. if (NULL != old && (is_active(&old->top, rc) ||
  3205. is_active(&old->bottom, rc))) {
  3206. btv->framedrop++;
  3207. if (debug_latency)
  3208. bttv_irq_debug_low_latency(btv, rc);
  3209. spin_unlock(&btv->s_lock);
  3210. return;
  3211. }
  3212. /* switch */
  3213. btv->cvbi = new;
  3214. btv->loop_irq &= ~4;
  3215. bttv_buffer_activate_vbi(btv, new);
  3216. bttv_set_dma(btv, 0);
  3217. bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
  3218. spin_unlock(&btv->s_lock);
  3219. }
  3220. static irqreturn_t bttv_irq(int irq, void *dev_id)
  3221. {
  3222. u32 stat,astat;
  3223. u32 dstat;
  3224. int count;
  3225. struct bttv *btv;
  3226. int handled = 0;
  3227. btv=(struct bttv *)dev_id;
  3228. count=0;
  3229. while (1) {
  3230. /* get/clear interrupt status bits */
  3231. stat=btread(BT848_INT_STAT);
  3232. astat=stat&btread(BT848_INT_MASK);
  3233. if (!astat)
  3234. break;
  3235. handled = 1;
  3236. btwrite(stat,BT848_INT_STAT);
  3237. /* get device status bits */
  3238. dstat=btread(BT848_DSTATUS);
  3239. if (irq_debug) {
  3240. pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
  3241. btv->c.nr, count, btv->field_count,
  3242. stat>>28, btread(BT848_RISC_COUNT));
  3243. bttv_print_irqbits(stat,astat);
  3244. if (stat & BT848_INT_HLOCK)
  3245. pr_cont(" HLOC => %s",
  3246. dstat & BT848_DSTATUS_HLOC
  3247. ? "yes" : "no");
  3248. if (stat & BT848_INT_VPRES)
  3249. pr_cont(" PRES => %s",
  3250. dstat & BT848_DSTATUS_PRES
  3251. ? "yes" : "no");
  3252. if (stat & BT848_INT_FMTCHG)
  3253. pr_cont(" NUML => %s",
  3254. dstat & BT848_DSTATUS_NUML
  3255. ? "625" : "525");
  3256. pr_cont("\n");
  3257. }
  3258. if (astat&BT848_INT_VSYNC)
  3259. btv->field_count++;
  3260. if ((astat & BT848_INT_GPINT) && btv->remote) {
  3261. bttv_input_irq(btv);
  3262. }
  3263. if (astat & BT848_INT_I2CDONE) {
  3264. btv->i2c_done = stat;
  3265. wake_up(&btv->i2c_queue);
  3266. }
  3267. if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
  3268. bttv_irq_switch_vbi(btv);
  3269. if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
  3270. bttv_irq_wakeup_top(btv);
  3271. if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
  3272. bttv_irq_switch_video(btv);
  3273. if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
  3274. /* trigger automute */
  3275. audio_mux_gpio(btv, btv->audio_input, btv->mute);
  3276. if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
  3277. pr_info("%d: %s%s @ %08x,",
  3278. btv->c.nr,
  3279. (astat & BT848_INT_SCERR) ? "SCERR" : "",
  3280. (astat & BT848_INT_OCERR) ? "OCERR" : "",
  3281. btread(BT848_RISC_COUNT));
  3282. bttv_print_irqbits(stat,astat);
  3283. pr_cont("\n");
  3284. if (bttv_debug)
  3285. bttv_print_riscaddr(btv);
  3286. }
  3287. if (fdsr && astat & BT848_INT_FDSR) {
  3288. pr_info("%d: FDSR @ %08x\n",
  3289. btv->c.nr, btread(BT848_RISC_COUNT));
  3290. if (bttv_debug)
  3291. bttv_print_riscaddr(btv);
  3292. }
  3293. count++;
  3294. if (count > 4) {
  3295. if (count > 8 || !(astat & BT848_INT_GPINT)) {
  3296. btwrite(0, BT848_INT_MASK);
  3297. pr_err("%d: IRQ lockup, cleared int mask [",
  3298. btv->c.nr);
  3299. } else {
  3300. pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
  3301. btv->c.nr);
  3302. btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
  3303. BT848_INT_MASK);
  3304. }
  3305. bttv_print_irqbits(stat,astat);
  3306. pr_cont("]\n");
  3307. }
  3308. }
  3309. btv->irq_total++;
  3310. if (handled)
  3311. btv->irq_me++;
  3312. return IRQ_RETVAL(handled);
  3313. }
  3314. /* ----------------------------------------------------------------------- */
  3315. /* initialization */
  3316. static void vdev_init(struct bttv *btv,
  3317. struct video_device *vfd,
  3318. const struct video_device *template,
  3319. const char *type_name)
  3320. {
  3321. *vfd = *template;
  3322. vfd->v4l2_dev = &btv->c.v4l2_dev;
  3323. vfd->release = video_device_release_empty;
  3324. video_set_drvdata(vfd, btv);
  3325. snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
  3326. btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
  3327. type_name, bttv_tvcards[btv->c.type].name);
  3328. if (btv->tuner_type == TUNER_ABSENT) {
  3329. v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
  3330. v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
  3331. v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
  3332. v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
  3333. }
  3334. }
  3335. static void bttv_unregister_video(struct bttv *btv)
  3336. {
  3337. video_unregister_device(&btv->video_dev);
  3338. video_unregister_device(&btv->vbi_dev);
  3339. video_unregister_device(&btv->radio_dev);
  3340. }
  3341. /* register video4linux devices */
  3342. static int bttv_register_video(struct bttv *btv)
  3343. {
  3344. if (no_overlay > 0)
  3345. pr_notice("Overlay support disabled\n");
  3346. /* video */
  3347. vdev_init(btv, &btv->video_dev, &bttv_video_template, "video");
  3348. btv->video_dev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
  3349. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  3350. if (btv->tuner_type != TUNER_ABSENT)
  3351. btv->video_dev.device_caps |= V4L2_CAP_TUNER;
  3352. if (no_overlay <= 0)
  3353. btv->video_dev.device_caps |= V4L2_CAP_VIDEO_OVERLAY;
  3354. if (video_register_device(&btv->video_dev, VFL_TYPE_VIDEO,
  3355. video_nr[btv->c.nr]) < 0)
  3356. goto err;
  3357. pr_info("%d: registered device %s\n",
  3358. btv->c.nr, video_device_node_name(&btv->video_dev));
  3359. if (device_create_file(&btv->video_dev.dev,
  3360. &dev_attr_card)<0) {
  3361. pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
  3362. goto err;
  3363. }
  3364. /* vbi */
  3365. vdev_init(btv, &btv->vbi_dev, &bttv_video_template, "vbi");
  3366. btv->vbi_dev.device_caps = V4L2_CAP_VBI_CAPTURE | V4L2_CAP_READWRITE |
  3367. V4L2_CAP_STREAMING;
  3368. if (btv->tuner_type != TUNER_ABSENT)
  3369. btv->vbi_dev.device_caps |= V4L2_CAP_TUNER;
  3370. if (video_register_device(&btv->vbi_dev, VFL_TYPE_VBI,
  3371. vbi_nr[btv->c.nr]) < 0)
  3372. goto err;
  3373. pr_info("%d: registered device %s\n",
  3374. btv->c.nr, video_device_node_name(&btv->vbi_dev));
  3375. if (!btv->has_radio)
  3376. return 0;
  3377. /* radio */
  3378. vdev_init(btv, &btv->radio_dev, &radio_template, "radio");
  3379. btv->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
  3380. if (btv->has_saa6588)
  3381. btv->radio_dev.device_caps |= V4L2_CAP_READWRITE |
  3382. V4L2_CAP_RDS_CAPTURE;
  3383. if (btv->has_tea575x)
  3384. btv->radio_dev.device_caps |= V4L2_CAP_HW_FREQ_SEEK;
  3385. btv->radio_dev.ctrl_handler = &btv->radio_ctrl_handler;
  3386. if (video_register_device(&btv->radio_dev, VFL_TYPE_RADIO,
  3387. radio_nr[btv->c.nr]) < 0)
  3388. goto err;
  3389. pr_info("%d: registered device %s\n",
  3390. btv->c.nr, video_device_node_name(&btv->radio_dev));
  3391. /* all done */
  3392. return 0;
  3393. err:
  3394. bttv_unregister_video(btv);
  3395. return -1;
  3396. }
  3397. /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
  3398. /* response on cards with no firmware is not enabled by OF */
  3399. static void pci_set_command(struct pci_dev *dev)
  3400. {
  3401. #if defined(__powerpc__)
  3402. unsigned int cmd;
  3403. pci_read_config_dword(dev, PCI_COMMAND, &cmd);
  3404. cmd = (cmd | PCI_COMMAND_MEMORY );
  3405. pci_write_config_dword(dev, PCI_COMMAND, cmd);
  3406. #endif
  3407. }
  3408. static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
  3409. {
  3410. struct v4l2_frequency init_freq = {
  3411. .tuner = 0,
  3412. .type = V4L2_TUNER_ANALOG_TV,
  3413. .frequency = 980,
  3414. };
  3415. int result;
  3416. unsigned char lat;
  3417. struct bttv *btv;
  3418. struct v4l2_ctrl_handler *hdl;
  3419. if (bttv_num == BTTV_MAX)
  3420. return -ENOMEM;
  3421. pr_info("Bt8xx card found (%d)\n", bttv_num);
  3422. bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
  3423. if (btv == NULL) {
  3424. pr_err("out of memory\n");
  3425. return -ENOMEM;
  3426. }
  3427. btv->c.nr = bttv_num;
  3428. snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
  3429. "bttv%d", btv->c.nr);
  3430. /* initialize structs / fill in defaults */
  3431. mutex_init(&btv->lock);
  3432. spin_lock_init(&btv->s_lock);
  3433. spin_lock_init(&btv->gpio_lock);
  3434. init_waitqueue_head(&btv->i2c_queue);
  3435. INIT_LIST_HEAD(&btv->c.subs);
  3436. INIT_LIST_HEAD(&btv->capture);
  3437. INIT_LIST_HEAD(&btv->vcapture);
  3438. timer_setup(&btv->timeout, bttv_irq_timeout, 0);
  3439. btv->i2c_rc = -1;
  3440. btv->tuner_type = UNSET;
  3441. btv->new_input = UNSET;
  3442. btv->has_radio=radio[btv->c.nr];
  3443. /* pci stuff (init, get irq/mmio, ... */
  3444. btv->c.pci = dev;
  3445. btv->id = dev->device;
  3446. if (pci_enable_device(dev)) {
  3447. pr_warn("%d: Can't enable device\n", btv->c.nr);
  3448. result = -EIO;
  3449. goto free_mem;
  3450. }
  3451. if (dma_set_mask(&dev->dev, DMA_BIT_MASK(32))) {
  3452. pr_warn("%d: No suitable DMA available\n", btv->c.nr);
  3453. result = -EIO;
  3454. goto free_mem;
  3455. }
  3456. if (!request_mem_region(pci_resource_start(dev,0),
  3457. pci_resource_len(dev,0),
  3458. btv->c.v4l2_dev.name)) {
  3459. pr_warn("%d: can't request iomem (0x%llx)\n",
  3460. btv->c.nr,
  3461. (unsigned long long)pci_resource_start(dev, 0));
  3462. result = -EBUSY;
  3463. goto free_mem;
  3464. }
  3465. pci_set_master(dev);
  3466. pci_set_command(dev);
  3467. result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
  3468. if (result < 0) {
  3469. pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
  3470. goto fail0;
  3471. }
  3472. hdl = &btv->ctrl_handler;
  3473. v4l2_ctrl_handler_init(hdl, 20);
  3474. btv->c.v4l2_dev.ctrl_handler = hdl;
  3475. v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
  3476. btv->revision = dev->revision;
  3477. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  3478. pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
  3479. bttv_num, btv->id, btv->revision, pci_name(dev),
  3480. btv->c.pci->irq, lat,
  3481. (unsigned long long)pci_resource_start(dev, 0));
  3482. schedule();
  3483. btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
  3484. if (NULL == btv->bt848_mmio) {
  3485. pr_err("%d: ioremap() failed\n", btv->c.nr);
  3486. result = -EIO;
  3487. goto fail1;
  3488. }
  3489. /* identify card */
  3490. bttv_idcard(btv);
  3491. /* disable irqs, register irq handler */
  3492. btwrite(0, BT848_INT_MASK);
  3493. result = request_irq(btv->c.pci->irq, bttv_irq,
  3494. IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
  3495. if (result < 0) {
  3496. pr_err("%d: can't get IRQ %d\n",
  3497. bttv_num, btv->c.pci->irq);
  3498. goto fail1;
  3499. }
  3500. if (0 != bttv_handle_chipset(btv)) {
  3501. result = -EIO;
  3502. goto fail2;
  3503. }
  3504. /* init options from insmod args */
  3505. btv->opt_combfilter = combfilter;
  3506. bttv_ctrl_combfilter.def = combfilter;
  3507. bttv_ctrl_lumafilter.def = lumafilter;
  3508. btv->opt_automute = automute;
  3509. bttv_ctrl_automute.def = automute;
  3510. bttv_ctrl_agc_crush.def = agc_crush;
  3511. btv->opt_vcr_hack = vcr_hack;
  3512. bttv_ctrl_vcr_hack.def = vcr_hack;
  3513. bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
  3514. bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
  3515. btv->opt_uv_ratio = uv_ratio;
  3516. bttv_ctrl_uv_ratio.def = uv_ratio;
  3517. bttv_ctrl_full_luma.def = full_luma_range;
  3518. bttv_ctrl_coring.def = coring;
  3519. /* fill struct bttv with some useful defaults */
  3520. btv->init.btv = btv;
  3521. btv->init.ov.w.width = 320;
  3522. btv->init.ov.w.height = 240;
  3523. btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  3524. btv->init.width = 320;
  3525. btv->init.height = 240;
  3526. btv->init.ov.w.width = 320;
  3527. btv->init.ov.w.height = 240;
  3528. btv->init.ov.field = V4L2_FIELD_INTERLACED;
  3529. btv->input = 0;
  3530. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3531. V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
  3532. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3533. V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
  3534. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3535. V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
  3536. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3537. V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
  3538. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3539. V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
  3540. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3541. V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
  3542. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3543. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  3544. if (btv->volume_gpio)
  3545. v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
  3546. V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
  3547. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
  3548. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
  3549. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
  3550. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
  3551. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
  3552. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
  3553. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
  3554. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
  3555. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
  3556. v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
  3557. /* initialize hardware */
  3558. if (bttv_gpio)
  3559. bttv_gpio_tracking(btv,"pre-init");
  3560. bttv_risc_init_main(btv);
  3561. init_bt848(btv);
  3562. /* gpio */
  3563. btwrite(0x00, BT848_GPIO_REG_INP);
  3564. btwrite(0x00, BT848_GPIO_OUT_EN);
  3565. if (bttv_verbose)
  3566. bttv_gpio_tracking(btv,"init");
  3567. /* needs to be done before i2c is registered */
  3568. bttv_init_card1(btv);
  3569. /* register i2c + gpio */
  3570. init_bttv_i2c(btv);
  3571. /* some card-specific stuff (needs working i2c) */
  3572. bttv_init_card2(btv);
  3573. bttv_init_tuner(btv);
  3574. if (btv->tuner_type != TUNER_ABSENT) {
  3575. bttv_set_frequency(btv, &init_freq);
  3576. btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
  3577. }
  3578. btv->std = V4L2_STD_PAL;
  3579. init_irqreg(btv);
  3580. if (!bttv_tvcards[btv->c.type].no_video)
  3581. v4l2_ctrl_handler_setup(hdl);
  3582. if (hdl->error) {
  3583. result = hdl->error;
  3584. goto fail2;
  3585. }
  3586. /* mute device */
  3587. audio_mute(btv, 1);
  3588. /* register video4linux + input */
  3589. if (!bttv_tvcards[btv->c.type].no_video) {
  3590. v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
  3591. v4l2_ctrl_radio_filter, false);
  3592. if (btv->radio_ctrl_handler.error) {
  3593. result = btv->radio_ctrl_handler.error;
  3594. goto fail2;
  3595. }
  3596. set_input(btv, 0, btv->tvnorm);
  3597. bttv_crop_reset(&btv->crop[0], btv->tvnorm);
  3598. btv->crop[1] = btv->crop[0]; /* current = default */
  3599. disclaim_vbi_lines(btv);
  3600. disclaim_video_lines(btv);
  3601. bttv_register_video(btv);
  3602. }
  3603. /* add subdevices and autoload dvb-bt8xx if needed */
  3604. if (bttv_tvcards[btv->c.type].has_dvb) {
  3605. bttv_sub_add_device(&btv->c, "dvb");
  3606. request_modules(btv);
  3607. }
  3608. if (!disable_ir) {
  3609. init_bttv_i2c_ir(btv);
  3610. bttv_input_init(btv);
  3611. }
  3612. /* everything is fine */
  3613. bttv_num++;
  3614. return 0;
  3615. fail2:
  3616. free_irq(btv->c.pci->irq,btv);
  3617. fail1:
  3618. v4l2_ctrl_handler_free(&btv->ctrl_handler);
  3619. v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
  3620. v4l2_device_unregister(&btv->c.v4l2_dev);
  3621. fail0:
  3622. if (btv->bt848_mmio)
  3623. iounmap(btv->bt848_mmio);
  3624. release_mem_region(pci_resource_start(btv->c.pci,0),
  3625. pci_resource_len(btv->c.pci,0));
  3626. pci_disable_device(btv->c.pci);
  3627. free_mem:
  3628. bttvs[btv->c.nr] = NULL;
  3629. kfree(btv);
  3630. return result;
  3631. }
  3632. static void bttv_remove(struct pci_dev *pci_dev)
  3633. {
  3634. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  3635. struct bttv *btv = to_bttv(v4l2_dev);
  3636. if (bttv_verbose)
  3637. pr_info("%d: unloading\n", btv->c.nr);
  3638. if (bttv_tvcards[btv->c.type].has_dvb)
  3639. flush_request_modules(btv);
  3640. /* shutdown everything (DMA+IRQs) */
  3641. btand(~15, BT848_GPIO_DMA_CTL);
  3642. btwrite(0, BT848_INT_MASK);
  3643. btwrite(~0x0, BT848_INT_STAT);
  3644. btwrite(0x0, BT848_GPIO_OUT_EN);
  3645. if (bttv_gpio)
  3646. bttv_gpio_tracking(btv,"cleanup");
  3647. /* tell gpio modules we are leaving ... */
  3648. btv->shutdown=1;
  3649. bttv_input_fini(btv);
  3650. bttv_sub_del_devices(&btv->c);
  3651. /* unregister i2c_bus + input */
  3652. fini_bttv_i2c(btv);
  3653. /* unregister video4linux */
  3654. bttv_unregister_video(btv);
  3655. /* free allocated memory */
  3656. v4l2_ctrl_handler_free(&btv->ctrl_handler);
  3657. v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
  3658. btcx_riscmem_free(btv->c.pci,&btv->main);
  3659. /* free resources */
  3660. free_irq(btv->c.pci->irq,btv);
  3661. del_timer_sync(&btv->timeout);
  3662. iounmap(btv->bt848_mmio);
  3663. release_mem_region(pci_resource_start(btv->c.pci,0),
  3664. pci_resource_len(btv->c.pci,0));
  3665. pci_disable_device(btv->c.pci);
  3666. v4l2_device_unregister(&btv->c.v4l2_dev);
  3667. bttvs[btv->c.nr] = NULL;
  3668. kfree(btv);
  3669. return;
  3670. }
  3671. static int __maybe_unused bttv_suspend(struct device *dev)
  3672. {
  3673. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  3674. struct bttv *btv = to_bttv(v4l2_dev);
  3675. struct bttv_buffer_set idle;
  3676. unsigned long flags;
  3677. dprintk("%d: suspend\n", btv->c.nr);
  3678. /* stop dma + irqs */
  3679. spin_lock_irqsave(&btv->s_lock,flags);
  3680. memset(&idle, 0, sizeof(idle));
  3681. btv->state.video = btv->curr;
  3682. btv->state.vbi = btv->cvbi;
  3683. btv->state.loop_irq = btv->loop_irq;
  3684. btv->curr = idle;
  3685. btv->loop_irq = 0;
  3686. bttv_buffer_activate_video(btv, &idle);
  3687. bttv_buffer_activate_vbi(btv, NULL);
  3688. bttv_set_dma(btv, 0);
  3689. btwrite(0, BT848_INT_MASK);
  3690. spin_unlock_irqrestore(&btv->s_lock,flags);
  3691. /* save bt878 state */
  3692. btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
  3693. btv->state.gpio_data = gpio_read();
  3694. btv->state.disabled = 1;
  3695. return 0;
  3696. }
  3697. static int __maybe_unused bttv_resume(struct device *dev)
  3698. {
  3699. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  3700. struct bttv *btv = to_bttv(v4l2_dev);
  3701. unsigned long flags;
  3702. dprintk("%d: resume\n", btv->c.nr);
  3703. btv->state.disabled = 0;
  3704. /* restore bt878 state */
  3705. bttv_reinit_bt848(btv);
  3706. gpio_inout(0xffffff, btv->state.gpio_enable);
  3707. gpio_write(btv->state.gpio_data);
  3708. /* restart dma */
  3709. spin_lock_irqsave(&btv->s_lock,flags);
  3710. btv->curr = btv->state.video;
  3711. btv->cvbi = btv->state.vbi;
  3712. btv->loop_irq = btv->state.loop_irq;
  3713. bttv_buffer_activate_video(btv, &btv->curr);
  3714. bttv_buffer_activate_vbi(btv, btv->cvbi);
  3715. bttv_set_dma(btv, 0);
  3716. spin_unlock_irqrestore(&btv->s_lock,flags);
  3717. return 0;
  3718. }
  3719. static const struct pci_device_id bttv_pci_tbl[] = {
  3720. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
  3721. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
  3722. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
  3723. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
  3724. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
  3725. {0,}
  3726. };
  3727. MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
  3728. static SIMPLE_DEV_PM_OPS(bttv_pm_ops,
  3729. bttv_suspend,
  3730. bttv_resume);
  3731. static struct pci_driver bttv_pci_driver = {
  3732. .name = "bttv",
  3733. .id_table = bttv_pci_tbl,
  3734. .probe = bttv_probe,
  3735. .remove = bttv_remove,
  3736. .driver.pm = &bttv_pm_ops,
  3737. };
  3738. static int __init bttv_init_module(void)
  3739. {
  3740. int ret;
  3741. bttv_num = 0;
  3742. pr_info("driver version %s loaded\n", BTTV_VERSION);
  3743. if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
  3744. gbuffers = 2;
  3745. if (gbufsize > BTTV_MAX_FBUF)
  3746. gbufsize = BTTV_MAX_FBUF;
  3747. gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
  3748. if (bttv_verbose)
  3749. pr_info("using %d buffers with %dk (%d pages) each for capture\n",
  3750. gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
  3751. bttv_check_chipset();
  3752. ret = bus_register(&bttv_sub_bus_type);
  3753. if (ret < 0) {
  3754. pr_warn("bus_register error: %d\n", ret);
  3755. return ret;
  3756. }
  3757. ret = pci_register_driver(&bttv_pci_driver);
  3758. if (ret < 0)
  3759. bus_unregister(&bttv_sub_bus_type);
  3760. return ret;
  3761. }
  3762. static void __exit bttv_cleanup_module(void)
  3763. {
  3764. pci_unregister_driver(&bttv_pci_driver);
  3765. bus_unregister(&bttv_sub_bus_type);
  3766. }
  3767. module_init(bttv_init_module);
  3768. module_exit(bttv_cleanup_module);